1/*	$NetBSD$	*/
2/* $OpenBSD: uidswap.c,v 1.35 2006/08/03 03:34:42 deraadt Exp $ */
3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 *                    All rights reserved
7 * Code for uid-swapping.
8 *
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose.  Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 */
15
16#include "includes.h"
17__RCSID("$NetBSD: uidswap.c,v 1.4 2006/09/28 21:22:15 christos Exp $");
18#include <sys/param.h>
19#include <errno.h>
20#include <pwd.h>
21#include <string.h>
22#include <unistd.h>
23#include <stdarg.h>
24
25#include "log.h"
26#include "uidswap.h"
27
28/*
29 * Note: all these functions must work in all of the following cases:
30 *    1. euid=0, ruid=0
31 *    2. euid=0, ruid!=0
32 *    3. euid!=0, ruid!=0
33 * Additionally, they must work regardless of whether the system has
34 * POSIX saved uids or not.
35 */
36
37/* Lets assume that posix saved ids also work with seteuid, even though that
38   is not part of the posix specification. */
39
40/* Saved effective uid. */
41static int	privileged = 0;
42static int	temporarily_use_uid_effective = 0;
43static uid_t	saved_euid = 0;
44static gid_t	saved_egid;
45static gid_t	saved_egroups[NGROUPS_MAX], user_groups[NGROUPS_MAX];
46static int	saved_egroupslen = -1, user_groupslen = -1;
47
48/*
49 * Temporarily changes to the given uid.  If the effective user
50 * id is not root, this does nothing.  This call cannot be nested.
51 */
52void
53temporarily_use_uid(struct passwd *pw)
54{
55	/* Save the current euid, and egroups. */
56	saved_euid = geteuid();
57	saved_egid = getegid();
58	debug("temporarily_use_uid: %u/%u (e=%u/%u)",
59	    (u_int)pw->pw_uid, (u_int)pw->pw_gid,
60	    (u_int)saved_euid, (u_int)saved_egid);
61	if (saved_euid != 0) {
62		privileged = 0;
63		return;
64	}
65	privileged = 1;
66	temporarily_use_uid_effective = 1;
67	saved_egroupslen = getgroups(NGROUPS_MAX, saved_egroups);
68	if (saved_egroupslen < 0)
69		fatal("getgroups: %.100s", strerror(errno));
70
71	/* set and save the user's groups */
72	if (user_groupslen == -1) {
73		if (initgroups(pw->pw_name, pw->pw_gid) < 0)
74			fatal("initgroups: %s: %.100s", pw->pw_name,
75			    strerror(errno));
76		user_groupslen = getgroups(NGROUPS_MAX, user_groups);
77		if (user_groupslen < 0)
78			fatal("getgroups: %.100s", strerror(errno));
79	}
80	/* Set the effective uid to the given (unprivileged) uid. */
81	if (setgroups(user_groupslen, user_groups) < 0)
82		fatal("setgroups: %.100s", strerror(errno));
83	if (setegid(pw->pw_gid) < 0)
84		fatal("setegid %u: %.100s", (u_int)pw->pw_gid,
85		    strerror(errno));
86	if (seteuid(pw->pw_uid) == -1)
87		fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
88		    strerror(errno));
89}
90
91/*
92 * Restores to the original (privileged) uid.
93 */
94void
95restore_uid(void)
96{
97	/* it's a no-op unless privileged */
98	if (!privileged) {
99		debug("restore_uid: (unprivileged)");
100		return;
101	}
102	if (!temporarily_use_uid_effective)
103		fatal("restore_uid: temporarily_use_uid not effective");
104	debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
105	/* Set the effective uid back to the saved privileged uid. */
106	if (seteuid(saved_euid) < 0)
107		fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
108	if (setgroups(saved_egroupslen, saved_egroups) < 0)
109		fatal("setgroups: %.100s", strerror(errno));
110	if (setegid(saved_egid) < 0)
111		fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
112	temporarily_use_uid_effective = 0;
113}
114
115/*
116 * Permanently sets all uids to the given uid.  This cannot be
117 * called while temporarily_use_uid is effective.
118 */
119void
120permanently_set_uid(struct passwd *pw)
121{
122	if (pw == NULL)
123		fatal("permanently_set_uid: no user given");
124	if (temporarily_use_uid_effective)
125		fatal("permanently_set_uid: temporarily_use_uid effective");
126	debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid,
127	    (u_int)pw->pw_gid);
128
129	if (setgid(pw->pw_gid) < 0)
130		fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
131	if (setegid(pw->pw_gid) < 0)
132		fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
133
134	if (setuid(pw->pw_uid) < 0)
135		fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
136	if (seteuid(pw->pw_uid) < 0)
137		fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
138
139	/* Verify GID drop was successful */
140	if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) {
141		fatal("%s: egid incorrect gid:%u egid:%u (should be %u)",
142		    __func__, (u_int)getgid(), (u_int)getegid(),
143		    (u_int)pw->pw_gid);
144	}
145
146	/* Verify UID drop was successful */
147	if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) {
148		fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
149		    __func__, (u_int)getuid(), (u_int)geteuid(),
150		    (u_int)pw->pw_uid);
151	}
152}
153
154void
155permanently_drop_suid(uid_t uid)
156{
157	debug("permanently_drop_suid: %u", (u_int)uid);
158	if (seteuid(uid) < 0)
159		fatal("seteuid %u: %.100s", (u_int)uid, strerror(errno));
160	if (setuid(uid) < 0)
161		fatal("setuid %u: %.100s", (u_int)uid, strerror(errno));
162
163	/* Verify UID drop was successful */
164	if (getuid() != uid || geteuid() != uid) {
165		fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
166		    __func__, (u_int)getuid(), (u_int)geteuid(), (u_int)uid);
167	}
168}
169