]> git.saurik.com Git - apple/system_cmds.git/blame - at.tproj/privs.h
system_cmds-433.8.tar.gz
[apple/system_cmds.git] / at.tproj / privs.h
CommitLineData
2fc1e207
A
1/*
2 * privs.h - header for privileged operations
3 * Copyright (C) 1993 Thomas Koenig
1815bff5
A
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.
2fc1e207 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1815bff5
A
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 *
2fc1e207 25 * $FreeBSD: src/usr.bin/at/privs.h,v 1.8 2001/09/04 16:15:51 ru Exp $
1815bff5
A
26 */
27
28#ifndef _PRIVS_H
29#define _PRIVS_H
30
31#include <unistd.h>
32
2fc1e207
A
33/* Relinquish privileges temporarily for a setuid or setgid program
34 * with the option of getting them back later. This is done by
35 * utilizing POSIX saved user and group IDs. Call RELINQUISH_PRIVS once
36 * at the beginning of the main program. This will cause all operations
1815bff5 37 * to be executed with the real userid. When you need the privileges
2fc1e207 38 * of the setuid/setgid invocation, call PRIV_START; when you no longer
1815bff5
A
39 * need it, call PRIV_END. Note that it is an error to call PRIV_START
40 * and not PRIV_END within the same function.
41 *
2fc1e207 42 * Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running
1815bff5
A
43 * as root, and you want to drop back the effective userid to a
44 * and the effective group id to b, with the option to get them back
45 * later.
46 *
47 * If you no longer need root privileges, but those of some other
2fc1e207 48 * userid/groupid, you can call REDUCE_PRIV(a,b) when your effective
1815bff5
A
49 * is the user's.
50 *
51 * Problems: Do not use return between PRIV_START and PRIV_END; this
52 * will cause the program to continue running in an unprivileged
53 * state.
54 *
55 * It is NOT safe to call exec(), system() or popen() with a user-
56 * supplied program (i.e. without carefully checking PATH and any
57 * library load paths) with relinquished privileges; the called program
2fc1e207 58 * can acquire them just as easily. Set both effective and real userid
1815bff5
A
59 * to the real userid before calling any of them.
60 */
61
62#ifndef MAIN
63extern
64#endif
65uid_t real_uid, effective_uid;
66
2fc1e207
A
67#ifndef MAIN
68extern
69#endif
70gid_t real_gid, effective_gid;
71
1815bff5
A
72#define RELINQUISH_PRIVS { \
73 real_uid = getuid(); \
74 effective_uid = geteuid(); \
2fc1e207
A
75 real_gid = getgid(); \
76 effective_gid = getegid(); \
77 setegid(real_gid); \
1815bff5
A
78 seteuid(real_uid); \
79}
80
2fc1e207 81#define RELINQUISH_PRIVS_ROOT(a, b) { \
1815bff5
A
82 real_uid = (a); \
83 effective_uid = geteuid(); \
2fc1e207
A
84 real_gid = (b); \
85 effective_gid = getegid(); \
86 setegid(real_gid); \
1815bff5
A
87 seteuid(real_uid); \
88}
89
90#define PRIV_START { \
2fc1e207
A
91 if (seteuid(0)<0) perr("cannot regain privs"); \
92 if (setegid(effective_gid)<0) perr("cannot reset gid"); \
93 if (seteuid(effective_uid)<0) perr("cannot reset uid"); \
94}
1815bff5 95
2fc1e207
A
96#define PRIV_END { \
97 if (seteuid(0)<0) perr("cannot regain privs"); \
98 if (setegid(real_gid)<0) perr("cannot reset gid"); \
99 if (seteuid(real_uid)<0) perr("cannot reset uid"); \
1815bff5
A
100}
101
2fc1e207
A
102#define REDUCE_PRIV(a, b) { \
103 PRIV_START \
104 effective_uid = (a); \
105 effective_gid = (b); \
106 setregid((gid_t)-1, effective_gid); \
107 setreuid((uid_t)-1, effective_uid); \
108 PRIV_END \
1815bff5
A
109}
110#endif