2 * privs.h - header for privileged operations
3 * Copyright (C) 1993 Thomas Koenig
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. The name of the author(s) may not be used to endorse or promote
11 * products derived from this software without specific prior written
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 /* Relinquish privileges temporarily for a setuid or setgid program
40 * with the option of getting them back later. This is done by swapping
41 * the real and effective userid BSD style. Call RELINQUISH_PRIVS once
42 * at the beginning of the main program. This will cause all operations
43 * to be executed with the real userid. When you need the privileges
44 * of the setuid/setgid invocation, call PRIV_START; when you no longer
45 * need it, call PRIV_END. Note that it is an error to call PRIV_START
46 * and not PRIV_END within the same function.
48 * Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running
49 * as root, and you want to drop back the effective userid to a
50 * and the effective group id to b, with the option to get them back
53 * If you no longer need root privileges, but those of some other
54 * userid/groupid, you can call REDUCE_PRIV(a,b) when your effective
57 * Problems: Do not use return between PRIV_START and PRIV_END; this
58 * will cause the program to continue running in an unprivileged
61 * It is NOT safe to call exec(), system() or popen() with a user-
62 * supplied program (i.e. without carefully checking PATH and any
63 * library load paths) with relinquished privileges; the called program
64 * can acquire them just as easily. Set both effective and real userid
65 * to the real userid before calling any of them.
71 uid_t real_uid, effective_uid;
76 gid_t real_gid, effective_gid;
78 #define RELINQUISH_PRIVS { \
79 real_uid = getuid(); \
80 effective_uid = geteuid(); \
81 real_gid = getgid(); \
82 effective_gid = getegid(); \
83 setreuid(effective_uid, real_uid); \
84 setregid(effective_gid, real_gid); \
87 #define RELINQUISH_PRIVS_ROOT(a,b) { \
89 effective_uid = geteuid(); \
91 effective_gid = getegid(); \
92 setregid(effective_gid, real_gid); \
93 setreuid(effective_uid, real_uid); \
97 setreuid(real_uid, effective_uid); \
98 setregid(real_gid, effective_gid);
101 setregid(effective_gid, real_gid); \
102 setreuid(effective_uid, real_uid); \
105 #define REDUCE_PRIV(a,b) {\
106 setreuid(real_uid, effective_uid); \
107 setregid(real_gid, effective_gid); \
108 effective_uid = (a); \
109 effective_gid = (b); \
110 setregid(effective_gid, real_gid); \
111 setreuid(effective_uid, real_uid); \