2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Code for uid-swapping.
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
21 * Note: all these functions must work in all of the following cases:
25 * Additionally, they must work regardless of whether the system has
26 * POSIX saved uids or not.
29 #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
30 /* Lets assume that posix saved ids also work with seteuid, even though that
31 is not part of the posix specification. */
32 #define SAVED_IDS_WORK_WITH_SETEUID
33 /* Saved effective uid. */
34 static uid_t saved_euid = 0;
35 static gid_t saved_egid = 0;
38 /* Saved effective uid. */
39 static int privileged = 0;
40 static int temporarily_use_uid_effective = 0;
41 static gid_t *saved_egroups = NULL, *user_groups = NULL;
42 static int saved_egroupslen = -1, user_groupslen = -1;
45 * Temporarily changes to the given uid. If the effective user
46 * id is not root, this does nothing. This call cannot be nested.
49 temporarily_use_uid(struct passwd *pw)
51 /* Save the current euid, and egroups. */
52 #ifdef SAVED_IDS_WORK_WITH_SETEUID
53 saved_euid = geteuid();
54 saved_egid = getegid();
55 debug("temporarily_use_uid: %u/%u (e=%u/%u)",
56 (u_int)pw->pw_uid, (u_int)pw->pw_gid,
57 (u_int)saved_euid, (u_int)saved_egid);
59 if (saved_euid != 0) {
69 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
72 temporarily_use_uid_effective = 1;
74 saved_egroupslen = getgroups(0, NULL);
75 if (saved_egroupslen < 0)
76 fatal("getgroups: %.100s", strerror(errno));
77 if (saved_egroupslen > 0) {
78 saved_egroups = xrealloc(saved_egroups,
79 saved_egroupslen * sizeof(gid_t));
80 if (getgroups(saved_egroupslen, saved_egroups) < 0)
81 fatal("getgroups: %.100s", strerror(errno));
82 } else { /* saved_egroupslen == 0 */
83 if (saved_egroups != NULL)
87 /* set and save the user's groups */
88 if (user_groupslen == -1) {
89 if (initgroups(pw->pw_name, pw->pw_gid) < 0)
90 fatal("initgroups: %s: %.100s", pw->pw_name,
93 user_groupslen = getgroups(0, NULL);
94 if (user_groupslen < 0)
95 fatal("getgroups: %.100s", strerror(errno));
96 if (user_groupslen > 0) {
97 user_groups = xrealloc(user_groups,
98 user_groupslen * sizeof(gid_t));
99 if (getgroups(user_groupslen, user_groups) < 0)
100 fatal("getgroups: %.100s", strerror(errno));
101 } else { /* user_groupslen == 0 */
106 /* Set the effective uid to the given (unprivileged) uid. */
107 if (setgroups(user_groupslen, user_groups) < 0)
108 fatal("setgroups: %.100s", strerror(errno));
109 #ifndef SAVED_IDS_WORK_WITH_SETEUID
110 /* Propagate the privileged gid to all of our gids. */
111 if (setgid(getegid()) < 0)
112 debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno));
113 /* Propagate the privileged uid to all of our uids. */
114 if (setuid(geteuid()) < 0)
115 debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
116 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
117 if (setegid(pw->pw_gid) < 0)
118 fatal("setegid %u: %.100s", (u_int)pw->pw_gid,
120 if (seteuid(pw->pw_uid) == -1)
121 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
126 * Restores to the original (privileged) uid.
131 /* it's a no-op unless privileged */
133 debug("restore_uid: (unprivileged)");
136 if (!temporarily_use_uid_effective)
137 fatal("restore_uid: temporarily_use_uid not effective");
139 #ifdef SAVED_IDS_WORK_WITH_SETEUID
140 debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
141 /* Set the effective uid back to the saved privileged uid. */
142 if (seteuid(saved_euid) < 0)
143 fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
144 if (setegid(saved_egid) < 0)
145 fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
146 #else /* SAVED_IDS_WORK_WITH_SETEUID */
148 * We are unable to restore the real uid to its unprivileged value.
149 * Propagate the real uid (usually more privileged) to effective uid
154 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
156 if (setgroups(saved_egroupslen, saved_egroups) < 0)
157 fatal("setgroups: %.100s", strerror(errno));
158 temporarily_use_uid_effective = 0;
162 * Permanently sets all uids to the given uid. This cannot be
163 * called while temporarily_use_uid is effective.
166 permanently_set_uid(struct passwd *pw)
168 uid_t old_uid = getuid();
169 gid_t old_gid = getgid();
171 if (temporarily_use_uid_effective)
172 fatal("permanently_set_uid: temporarily_use_uid effective");
173 debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid,
176 #if defined(HAVE_SETRESGID) && !defined(BROKEN_SETRESGID)
177 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0)
178 fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
179 #elif defined(HAVE_SETREGID) && !defined(BROKEN_SETREGID)
180 if (setregid(pw->pw_gid, pw->pw_gid) < 0)
181 fatal("setregid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
183 if (setegid(pw->pw_gid) < 0)
184 fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
185 if (setgid(pw->pw_gid) < 0)
186 fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
189 #if defined(HAVE_SETRESUID) && !defined(BROKEN_SETRESUID)
190 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0)
191 fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
192 #elif defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
193 if (setreuid(pw->pw_uid, pw->pw_uid) < 0)
194 fatal("setreuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
196 # ifndef SETEUID_BREAKS_SETUID
197 if (seteuid(pw->pw_uid) < 0)
198 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
200 if (setuid(pw->pw_uid) < 0)
201 fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
205 /* Try restoration of GID if changed (test clearing of saved gid) */
206 if (old_gid != pw->pw_gid && pw->pw_uid != 0 &&
207 (setgid(old_gid) != -1 || setegid(old_gid) != -1))
208 fatal("%s: was able to restore old [e]gid", __func__);
211 /* Verify GID drop was successful */
212 if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) {
213 fatal("%s: egid incorrect gid:%u egid:%u (should be %u)",
214 __func__, (u_int)getgid(), (u_int)getegid(),
219 /* Try restoration of UID if changed (test clearing of saved uid) */
220 if (old_uid != pw->pw_uid &&
221 (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
222 fatal("%s: was able to restore old [e]uid", __func__);
225 /* Verify UID drop was successful */
226 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) {
227 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
228 __func__, (u_int)getuid(), (u_int)geteuid(),