2 * Copyright (c) 1994-1996,1998-2008 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * Sponsored in part by the Defense Advanced Research Projects
17 * Agency (DARPA) and Air Force Research Laboratory, Air Force
18 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23 #include <sys/types.h>
24 #include <sys/param.h>
34 #endif /* STDC_HEADERS */
38 # ifdef HAVE_STRINGS_H
41 #endif /* HAVE_STRING_H */
44 #endif /* HAVE_UNISTD_H */
48 #ifdef HAVE_LOGIN_CAP_H
49 # include <login_cap.h>
55 __unused static const char rcsid[] = "$Sudo: set_perms.c,v 1.44 2008/03/06 17:19:56 millert Exp $";
59 # define ROOT_UID 65535
67 static void runas_setup __P((void));
68 static void runas_setgroups __P((void));
69 static void restore_groups __P((void));
71 static int current_perm = -1;
75 * Set real and effective and saved uids and gids based on perm.
76 * We always retain a saved uid of 0 unless we are headed for an exec().
77 * We only flip the effective gid since it only changes for PERM_SUDOERS.
78 * This version of set_perms() works fine with the "stay_setuid" option.
86 if (perm == current_perm)
91 if (setresuid(ROOT_UID, ROOT_UID, ROOT_UID)) {
92 errstr = "setresuid(ROOT_UID, ROOT_UID, ROOT_UID)";
95 (void) setresgid(-1, user_gid, -1);
96 if (current_perm == PERM_RUNAS)
101 (void) setresgid(-1, user_gid, -1);
102 if (setresuid(user_uid, user_uid, ROOT_UID)) {
103 errstr = "setresuid(user_uid, user_uid, ROOT_UID)";
109 /* headed for exec() */
110 (void) setgid(user_gid);
111 if (setresuid(user_uid, user_uid, user_uid)) {
112 errstr = "setresuid(user_uid, user_uid, user_uid)";
119 (void) setresgid(-1, runas_gr ?
120 runas_gr->gr_gid : runas_pw->pw_gid, -1);
121 if (setresuid(-1, runas_pw ? runas_pw->pw_uid :
123 errstr = "unable to change to runas uid";
128 case PERM_FULL_RUNAS:
129 /* headed for exec(), assume euid == ROOT_UID */
131 if (setresuid(def_stay_setuid ?
132 user_uid : runas_pw->pw_uid,
133 runas_pw->pw_uid, runas_pw->pw_uid)) {
134 errstr = "unable to change to runas uid";
140 /* assume euid == ROOT_UID, ruid == user */
141 if (setresgid(-1, SUDOERS_GID, -1))
142 error(1, "unable to change to sudoers gid");
145 * If SUDOERS_UID == ROOT_UID and SUDOERS_MODE
146 * is group readable we use a non-zero
147 * uid in order to avoid NFS lossage.
148 * Using uid 1 is a bit bogus but should
151 if (SUDOERS_UID == ROOT_UID) {
152 if ((SUDOERS_MODE & 040) && setresuid(ROOT_UID, 1, ROOT_UID)) {
153 errstr = "setresuid(ROOT_UID, 1, ROOT_UID)";
157 if (setresuid(ROOT_UID, SUDOERS_UID, ROOT_UID)) {
158 errstr = "setresuid(ROOT_UID, SUDOERS_UID, ROOT_UID)";
164 if (setresuid(ROOT_UID, timestamp_uid, ROOT_UID)) {
165 errstr = "setresuid(ROOT_UID, timestamp_uid, ROOT_UID)";
174 errorx(1, "%s: %s", errstr,
175 errno == EAGAIN ? "too many processes" : strerror(errno));
179 # ifdef HAVE_SETREUID
182 * Set real and effective uids and gids based on perm.
183 * We always retain a real or effective uid of ROOT_UID unless
184 * we are headed for an exec().
185 * This version of set_perms() works fine with the "stay_setuid" option.
193 if (perm == current_perm)
198 if (setreuid(-1, ROOT_UID)) {
199 errstr = "setreuid(-1, ROOT_UID)";
202 if (setuid(ROOT_UID)) {
203 errstr = "setuid(ROOT_UID)";
206 (void) setregid(-1, user_gid);
207 if (current_perm == PERM_RUNAS)
212 (void) setregid(-1, user_gid);
213 if (setreuid(ROOT_UID, user_uid)) {
214 errstr = "setreuid(ROOT_UID, user_uid)";
220 /* headed for exec() */
221 (void) setgid(user_gid);
222 if (setreuid(user_uid, user_uid)) {
223 errstr = "setreuid(user_uid, user_uid)";
230 (void) setregid(-1, runas_gr ?
231 runas_gr->gr_gid : runas_pw->pw_gid);
233 runas_pw ? runas_pw->pw_uid : user_uid)) {
234 errstr = "unable to change to runas uid";
239 case PERM_FULL_RUNAS:
240 /* headed for exec(), assume euid == ROOT_UID */
242 if (setreuid(def_stay_setuid ? user_uid :
243 runas_pw->pw_uid, runas_pw->pw_uid)) {
244 errstr = "unable to change to runas uid";
250 /* assume euid == ROOT_UID, ruid == user */
251 if (setregid(-1, SUDOERS_GID))
252 error(1, "unable to change to sudoers gid");
255 * If SUDOERS_UID == ROOT_UID and SUDOERS_MODE
256 * is group readable we use a non-zero
257 * uid in order to avoid NFS lossage.
258 * Using uid 1 is a bit bogus but should
261 if (SUDOERS_UID == ROOT_UID) {
262 if ((SUDOERS_MODE & 040) && setreuid(ROOT_UID, 1)) {
263 errstr = "setreuid(ROOT_UID, 1)";
267 if (setreuid(ROOT_UID, SUDOERS_UID)) {
268 errstr = "setreuid(ROOT_UID, SUDOERS_UID)";
274 if (setreuid(ROOT_UID, timestamp_uid)) {
275 errstr = "setreuid(ROOT_UID, timestamp_uid)";
284 errorx(1, "%s: %s", errstr,
285 errno == EAGAIN ? "too many processes" : strerror(errno));
288 # else /* !HAVE_SETRESUID && !HAVE_SETREUID */
292 * Set real and effective uids and gids based on perm.
293 * NOTE: does not support the "stay_setuid" option.
301 if (perm == current_perm)
305 * Since we only have setuid() and seteuid() and semantics
306 * for these calls differ on various systems, we set
307 * real and effective uids to ROOT_UID initially to be safe.
309 if (seteuid(ROOT_UID)) {
310 errstr = "seteuid(ROOT_UID)";
313 if (setuid(ROOT_UID)) {
314 errstr = "setuid(ROOT_UID)";
321 (void) setegid(user_gid);
322 if (current_perm == PERM_RUNAS)
327 (void) setegid(user_gid);
328 if (seteuid(user_uid)) {
329 errstr = "seteuid(user_uid)";
335 /* headed for exec() */
336 (void) setgid(user_gid);
337 if (setuid(user_uid)) {
338 errstr = "setuid(user_uid)";
345 (void) setegid(runas_gr ?
346 runas_gr->gr_gid : runas_pw->pw_gid);
347 if (seteuid(runas_pw ? runas_pw->pw_uid : user_uid)) {
348 errstr = "unable to change to runas uid";
353 case PERM_FULL_RUNAS:
354 /* headed for exec() */
356 if (setuid(runas_pw->pw_uid)) {
357 errstr = "unable to change to runas uid";
363 if (setegid(SUDOERS_GID))
364 error(1, "unable to change to sudoers gid");
367 * If SUDOERS_UID == ROOT_UID and SUDOERS_MODE
368 * is group readable we use a non-zero
369 * uid in order to avoid NFS lossage.
370 * Using uid 1 is a bit bogus but should
373 if (SUDOERS_UID == ROOT_UID) {
374 if ((SUDOERS_MODE & 040) && seteuid(1)) {
375 errstr = "seteuid(1)";
379 if (seteuid(SUDOERS_UID)) {
380 errstr = "seteuid(SUDOERS_UID)";
386 if (seteuid(timestamp_uid)) {
387 errstr = "seteuid(timestamp_uid)";
396 errorx(1, "%s: %s", errstr,
397 errno == EAGAIN ? "too many processes" : strerror(errno));
400 # else /* !HAVE_SETRESUID && !HAVE_SETREUID && !HAVE_SETEUID */
403 * Set uids and gids based on perm via setuid() and setgid().
404 * NOTE: does not support the "stay_setuid" or timestampowner options.
405 * Also, SUDOERS_UID and SUDOERS_GID are not used.
413 if (perm == current_perm)
418 if (setuid(ROOT_UID)) {
419 errstr = "setuid(ROOT_UID)";
422 if (current_perm == PERM_RUNAS)
427 (void) setgid(user_gid);
428 if (setuid(user_uid)) {
429 errstr = "setuid(user_uid)";
434 case PERM_FULL_RUNAS:
436 if (setuid(runas_pw->pw_uid)) {
437 errstr = "unable to change to runas uid";
446 /* Unsupported since we can't set euid. */
453 errorx(1, "%s: %s", errstr,
454 errno == EAGAIN ? "too many processes" : strerror(errno));
456 # endif /* HAVE_SETEUID */
457 # endif /* HAVE_SETREUID */
458 #endif /* HAVE_SETRESUID */
460 #ifdef HAVE_INITGROUPS
464 static int ngroups = -1;
465 static GETGROUPS_T *groups;
468 if (def_preserve_groups)
472 * Use stashed copy of runas groups if available, else initgroups and stash.
475 pw = runas_pw ? runas_pw : sudo_user.pw;
476 if (initgroups(pw->pw_name, pw->pw_gid) < 0)
477 log_error(USE_ERRNO|MSG_ONLY, "can't set runas group vector");
478 if ((ngroups = getgroups(0, NULL)) < 0)
479 log_error(USE_ERRNO|MSG_ONLY, "can't get runas ngroups");
480 groups = emalloc2(ngroups, sizeof(GETGROUPS_T));
481 if (getgroups(ngroups, groups) < 0)
482 log_error(USE_ERRNO|MSG_ONLY, "can't get runas group vector");
484 if (setgroups(ngroups, groups) < 0)
485 log_error(USE_ERRNO|MSG_ONLY, "can't set runas group vector");
492 if (setgroups(user_ngroups, user_groups) < 0)
493 log_error(USE_ERRNO|MSG_ONLY, "can't reset user group vector");
510 #endif /* HAVE_INITGROUPS */
516 #ifdef HAVE_LOGIN_CAP_H
518 extern login_cap_t *lc;
521 if (runas_pw->pw_name != NULL) {
522 gid = runas_gr ? runas_gr->gr_gid : runas_pw->pw_gid;
523 #ifdef HAVE_GETUSERATTR
524 aix_setlimits(runas_pw->pw_name);
527 pam_prep_user(runas_pw);
528 #endif /* HAVE_PAM */
530 #ifdef HAVE_LOGIN_CAP_H
531 if (def_use_loginclass) {
533 * We only use setusercontext() set the nice value and rlimits.
535 flags = LOGIN_SETRESOURCES|LOGIN_SETPRIORITY;
536 if (!def_preserve_groups)
537 SET(flags, LOGIN_SETGROUP);
538 else if (setgid(gid))
539 warning("cannot set gid to runas gid");
540 if (setusercontext(lc, runas_pw, runas_pw->pw_uid, flags)) {
541 if (runas_pw->pw_uid != ROOT_UID)
542 error(1, "unable to set user context");
544 warning("unable to set user context");
547 #endif /* HAVE_LOGIN_CAP_H */
549 warning("cannot set gid to runas gid");
551 * Initialize group vector