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