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