HAMMER - Try to improve backend stalls due to heavy write activity.
[dragonfly.git] / usr.bin / at / privs.h
blobb7e9a0c21fc14db7e0badc3a0e3bf0f8f744a321
1 /*
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
7 * are met:
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
12 * permission.
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.
25 * $FreeBSD: src/usr.bin/at/privs.h,v 1.8 2001/09/04 16:15:51 ru Exp $
26 * $DragonFly: src/usr.bin/at/privs.h,v 1.4 2007/08/26 16:12:27 pavalos Exp $
29 #ifndef _PRIVS_H
30 #define _PRIVS_H
32 #include <unistd.h>
34 /* Relinquish privileges temporarily for a setuid or setgid program
35 * with the option of getting them back later. This is done by
36 * utilizing POSIX saved user and group IDs. Call RELINQUISH_PRIVS once
37 * at the beginning of the main program. This will cause all operations
38 * to be executed with the real userid. When you need the privileges
39 * of the setuid/setgid invocation, call PRIV_START; when you no longer
40 * need it, call PRIV_END. Note that it is an error to call PRIV_START
41 * and not PRIV_END within the same function.
43 * Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running
44 * as root, and you want to drop back the effective userid to a
45 * and the effective group id to b, with the option to get them back
46 * later.
48 * If you no longer need root privileges, but those of some other
49 * userid/groupid, you can call REDUCE_PRIV(a,b) when your effective
50 * is the user's.
52 * Problems: Do not use return between PRIV_START and PRIV_END; this
53 * will cause the program to continue running in an unprivileged
54 * state.
56 * It is NOT safe to call exec(), system() or popen() with a user-
57 * supplied program (i.e. without carefully checking PATH and any
58 * library load paths) with relinquished privileges; the called program
59 * can acquire them just as easily. Set both effective and real userid
60 * to the real userid before calling any of them.
63 extern uid_t real_uid, effective_uid;
64 extern gid_t real_gid, effective_gid;
66 #define RELINQUISH_PRIVS { \
67 real_uid = getuid(); \
68 effective_uid = geteuid(); \
69 real_gid = getgid(); \
70 effective_gid = getegid(); \
71 seteuid(real_uid); \
72 setegid(real_gid); \
75 #define RELINQUISH_PRIVS_ROOT(a, b) { \
76 real_uid = (a); \
77 effective_uid = geteuid(); \
78 real_gid = (b); \
79 effective_gid = getegid(); \
80 setegid(real_gid); \
81 seteuid(real_uid); \
84 #define PRIV_START { \
85 seteuid(effective_uid); \
86 setegid(effective_gid); \
89 #define PRIV_END { \
90 setegid(real_gid); \
91 seteuid(real_uid); \
94 #define REDUCE_PRIV(a, b) { \
95 PRIV_START \
96 effective_uid = (a); \
97 effective_gid = (b); \
98 setreuid((uid_t)-1, effective_uid); \
99 setregid((gid_t)-1, effective_gid); \
100 PRIV_END \
102 #endif