]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/at/privs.h
This commit was generated by cvs2svn to compensate for changes in r53140,
[FreeBSD/FreeBSD.git] / usr.bin / at / privs.h
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(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.
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 /* Relinquish privileges temporarily for a setuid or setgid program
38  * with the option of getting them back later.  This is done by swapping
39  * the real and effective userid BSD style.  Call RELINQUISH_PRIVS once
40  * at the beginning of the main program.  This will cause all operatons
41  * to be executed with the real userid.  When you need the privileges
42  * of the setuid/setgid invocation, call PRIV_START; when you no longer
43  * need it, call PRIV_END.  Note that it is an error to call PRIV_START
44  * and not PRIV_END within the same function.
45  *
46  * Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running
47  * as root, and you want to drop back the effective userid to a
48  * and the effective group id to b, with the option to get them back
49  * later.
50  *
51  * If you no longer need root privileges, but those of some other
52  * userid/groupid, you can call REDUCE_PRIV(a,b) when your effective
53  * is the user's.
54  *
55  * Problems: Do not use return between PRIV_START and PRIV_END; this
56  * will cause the program to continue running in an unprivileged
57  * state.
58  *
59  * It is NOT safe to call exec(), system() or popen() with a user-
60  * supplied program (i.e. without carefully checking PATH and any
61  * library load paths) with relinquished privileges; the called program
62  * can aquire them just as easily.  Set both effective and real userid
63  * to the real userid before calling any of them.
64  */
65
66 #ifndef MAIN
67 extern
68 #endif
69 uid_t real_uid, effective_uid;
70
71 #ifndef MAIN 
72 extern
73 #endif
74 gid_t real_gid, effective_gid;
75
76 #define RELINQUISH_PRIVS { \
77                               real_uid = getuid(); \
78                               effective_uid = geteuid(); \
79                               real_gid = getgid(); \
80                               effective_gid = getegid(); \
81                               setreuid(effective_uid, real_uid); \
82                               setregid(effective_gid, real_gid); \
83                           }
84
85 #define RELINQUISH_PRIVS_ROOT(a,b) { \
86                               real_uid = (a); \
87                               effective_uid = geteuid(); \
88                               real_gid = (b); \
89                               effective_gid = getegid(); \
90                               setregid(effective_gid, real_gid); \
91                               setreuid(effective_uid, real_uid); \
92                           }
93
94 #define PRIV_START {\
95                     setreuid(real_uid, effective_uid); \
96                     setregid(real_gid, effective_gid);
97
98 #define PRIV_END \
99                     setregid(effective_gid, real_gid); \
100                     setreuid(effective_uid, real_uid); \
101                     }
102
103 #define REDUCE_PRIV(a,b) {\
104                         setreuid(real_uid, effective_uid); \
105                         setregid(real_gid, effective_gid); \
106                         effective_uid = (a); \
107                         effective_gid = (b); \
108                         setregid(effective_gid, real_gid); \
109                         setreuid(effective_uid, real_uid); \
110                     }
111 #endif