xref: /freebsd/usr.bin/at/privs.h (revision b89321a57ddc992e95585bf896d1871fae5b88c1)
1 /*
2  *  privs.h - header for privileged operations
3  *  Copyright (C) 1993  Thomas Koenig
4  *
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.
13  *
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 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.
24  */
25 
26 #ifndef _PRIVS_H
27 #define _PRIVS_H
28 
29 #ifndef _USE_BSD
30 #define _USE_BSD 1
31 #include <unistd.h>
32 #undef _USE_BSD
33 #else
34 #include <unistd.h>
35 #endif
36 
37 #ifdef __FreeBSD__
38 /*
39  * setre[ug]id() not change r[ug]id for FreeBSD, but check it incorrectly
40  * for this program
41  */
42 #define setreuid(r, e) seteuid(e)
43 #define setregid(r, e) setegid(e)
44 #endif
45 
46 /* Relinquish privileges temporarily for a setuid or setgid program
47  * with the option of getting them back later.  This is done by swapping
48  * the real and effective userid BSD style.  Call RELINQUISH_PRIVS once
49  * at the beginning of the main program.  This will cause all operatons
50  * to be executed with the real userid.  When you need the privileges
51  * of the setuid/setgid invocation, call PRIV_START; when you no longer
52  * need it, call PRIV_END.  Note that it is an error to call PRIV_START
53  * and not PRIV_END within the same function.
54  *
55  * Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running
56  * as root, and you want to drop back the effective userid to a
57  * and the effective group id to b, with the option to get them back
58  * later.
59  *
60  * If you no longer need root privileges, but those of some other
61  * userid/groupid, you can call REDUCE_PRIV(a,b) when your effective
62  * is the user's.
63  *
64  * Problems: Do not use return between PRIV_START and PRIV_END; this
65  * will cause the program to continue running in an unprivileged
66  * state.
67  *
68  * It is NOT safe to call exec(), system() or popen() with a user-
69  * supplied program (i.e. without carefully checking PATH and any
70  * library load paths) with relinquished privileges; the called program
71  * can aquire them just as easily.  Set both effective and real userid
72  * to the real userid before calling any of them.
73  */
74 
75 #ifndef MAIN
76 extern
77 #endif
78 uid_t real_uid, effective_uid;
79 
80 #ifndef MAIN
81 extern
82 #endif
83 gid_t real_gid, effective_gid;
84 
85 #define RELINQUISH_PRIVS { \
86 			      real_uid = getuid(); \
87 			      effective_uid = geteuid(); \
88 			      real_gid = getgid(); \
89 			      effective_gid = getegid(); \
90 			      setregid(effective_gid, real_gid); \
91 			      setreuid(effective_uid, real_uid); \
92 		          }
93 
94 #define RELINQUISH_PRIVS_ROOT(a,b) { \
95 			      real_uid = (a); \
96 			      effective_uid = geteuid(); \
97 			      real_gid = (b); \
98 			      effective_gid = getegid(); \
99 			      setregid(effective_gid, real_gid); \
100 			      setreuid(effective_uid, real_uid); \
101 		          }
102 
103 #define PRIV_START {\
104 		    setreuid(real_uid, effective_uid); \
105 		    setregid(real_gid, effective_gid);
106 
107 #define PRIV_END \
108 		    setregid(effective_gid, real_gid); \
109 		    setreuid(effective_uid, real_uid); \
110 		    }
111 
112 #define REDUCE_PRIV(a,b) {\
113 			setreuid(real_uid, effective_uid); \
114 			setregid(real_gid, effective_gid); \
115 			effective_uid = (a); \
116 			effective_gid = (b); \
117 			setregid(effective_gid, real_gid); \
118 			setreuid(effective_uid, real_uid); \
119 		    }
120 #endif
121