privs.h revision 1.2 1 1.1 cgd /*
2 1.1 cgd * privs.h - header for privileged operations
3 1.1 cgd * Copyright (c) 1993 by Thomas Koenig
4 1.1 cgd * All rights reserved.
5 1.1 cgd *
6 1.1 cgd * Redistribution and use in source and binary forms, with or without
7 1.1 cgd * modification, are permitted provided that the following conditions
8 1.1 cgd * are met:
9 1.1 cgd * 1. Redistributions of source code must retain the above copyright
10 1.1 cgd * notice, this list of conditions and the following disclaimer.
11 1.1 cgd * 2. The name of the author(s) may not be used to endorse or promote
12 1.1 cgd * products derived from this software without specific prior written
13 1.1 cgd * permission.
14 1.1 cgd *
15 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 cgd * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 cgd *
26 1.2 cgd * $Id: privs.h,v 1.2 1994/04/16 03:41:59 cgd Exp $
27 1.1 cgd */
28 1.1 cgd
29 1.1 cgd #ifndef _PRIVS_H
30 1.1 cgd #define _PRIVS_H
31 1.1 cgd
32 1.1 cgd #include <unistd.h>
33 1.1 cgd
34 1.1 cgd /* Relinquish privileges temporarily for a setuid program
35 1.1 cgd * with the option of getting them back later. This is done by swapping
36 1.1 cgd * the real and effective userid BSD style. Call RELINQUISH_PRIVS once
37 1.1 cgd * at the beginning of the main program. This will cause all operatons
38 1.1 cgd * to be executed with the real userid. When you need the privileges
39 1.1 cgd * of the setuid invocation, call PRIV_START; when you no longer
40 1.1 cgd * need it, call PRIV_END. Note that it is an error to call PRIV_START
41 1.1 cgd * and not PRIV_END within the same function.
42 1.1 cgd *
43 1.1 cgd * Use RELINQUISH_PRIVS_ROOT(a) if your program started out running
44 1.1 cgd * as root, and you want to drop back the effective userid to a
45 1.1 cgd * and the effective group id to b, with the option to get them back
46 1.1 cgd * later.
47 1.1 cgd *
48 1.1 cgd * If you no longer need root privileges, but those of some other
49 1.2 cgd * userid, you can call REDUCE_PRIV(a) when your effective
50 1.1 cgd * is the user's.
51 1.1 cgd *
52 1.1 cgd * Problems: Do not use return between PRIV_START and PRIV_END; this
53 1.1 cgd * will cause the program to continue running in an unprivileged
54 1.1 cgd * state.
55 1.1 cgd *
56 1.1 cgd * It is NOT safe to call exec(), system() or popen() with a user-
57 1.1 cgd * supplied program (i.e. without carefully checking PATH and any
58 1.1 cgd * library load paths) with relinquished privileges; the called program
59 1.1 cgd * can aquire them just as easily. Set both effective and real userid
60 1.1 cgd * to the real userid before calling any of them.
61 1.1 cgd */
62 1.1 cgd
63 1.1 cgd #ifndef MAIN
64 1.1 cgd extern
65 1.1 cgd #endif
66 1.1 cgd uid_t real_uid, effective_uid;
67 1.1 cgd
68 1.1 cgd #define RELINQUISH_PRIVS { \
69 1.1 cgd real_uid = getuid(); \
70 1.1 cgd effective_uid = geteuid(); \
71 1.2 cgd seteuid(real_uid); \
72 1.1 cgd }
73 1.1 cgd
74 1.1 cgd #define RELINQUISH_PRIVS_ROOT(a) { \
75 1.1 cgd real_uid = (a); \
76 1.1 cgd effective_uid = geteuid(); \
77 1.2 cgd seteuid(real_uid); \
78 1.1 cgd }
79 1.1 cgd
80 1.1 cgd #define PRIV_START { \
81 1.2 cgd seteuid(effective_uid);
82 1.1 cgd
83 1.1 cgd #define PRIV_END \
84 1.2 cgd seteuid(real_uid); \
85 1.1 cgd }
86 1.1 cgd
87 1.1 cgd #define REDUCE_PRIV(a) { \
88 1.2 cgd seteuid(effective_uid); \
89 1.2 cgd real_uid = effective_uid = (a); \
90 1.2 cgd setuid(real_uid); \
91 1.1 cgd }
92 1.1 cgd #endif
93