1 /* $NetBSD: privs.h,v 1.7 2003/10/21 09:03:25 wiz Exp $ */
4 * privs.h - header for privileged operations
5 * Copyright (C) 1993 Thomas Koenig
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. The name of the author(s) may not be used to endorse or promote
13 * products derived from this software without specific prior written
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * From: OpenBSD: privs.h,v 1.4 1997/03/01 23:40:12 millert Exp
35 /* Relinquish privileges temporarily for a setuid or setgid program
36 * with the option of getting them back later. This is done by
37 * using POSIX saved user and groups ids. Call RELINQUISH_PRIVS once
38 * at the beginning of the main program. This will cause all operations
39 * to be executed with the real userid. When you need the privileges
40 * of the setuid/setgid invocation, call PRIV_START; when you no longer
41 * need it, call PRIV_END. Note that it is an error to call PRIV_START
42 * and not PRIV_END within the same function.
44 * Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running
45 * as root, and you want to drop back the effective userid to a
46 * and the effective group id to b, with the option to get them back
49 * Problems: Do not use return between PRIV_START and PRIV_END; this
50 * will cause the program to continue running in an unprivileged
53 * It is NOT safe to call exec(), system() or popen() with a user-
54 * supplied program (i.e. without carefully checking PATH and any
55 * library load paths) with relinquished privileges; the called program
56 * can acquire them just as easily. Set both effective and real userid
57 * to the real userid before calling any of them.
63 uid_t real_uid
, effective_uid
;
68 gid_t real_gid
, effective_gid
;
70 #define RELINQUISH_PRIVS do { \
71 real_uid = getuid(); \
72 effective_uid = geteuid(); \
73 real_gid = getgid(); \
74 effective_gid = getegid(); \
76 } while (/*CONSTCOND*/ 0)
78 #define RELINQUISH_PRIVS_ROOT(a, b) do { \
80 effective_uid = geteuid(); \
82 effective_gid = getegid(); \
84 } while (/*CONSTCOND*/ 0)
86 #define PRIV_START do { \
87 if (seteuid(effective_uid) == -1) \
88 perr("Cannot get user privs"); \
89 if (setegid(effective_gid) == -1) \
90 perr("Cannot get group privs"); \
91 } while (/*CONSTCOND*/ 0)
93 #define PRIV_END do { \
94 if (setegid(real_gid) == -1) \
95 perr("Cannot relinguish group privs"); \
96 if (seteuid(real_uid) == -1) \
97 perr("Cannot relinguish user privs"); \
98 } while (/*CONSTCOND*/ 0)
100 #endif /* _PRIV_H_ */