2 * Copyright (c) 2002, 2005 Networks Associates Technologies, Inc.
5 * Portions of this software were developed for the FreeBSD Project by
6 * ThinkSec AS and NAI Labs, the Security Research Division of Network
7 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
8 * ("CBOSS"), as part of the DARPA CHATS research program.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright (c) 1988, 1993, 1994
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 static const char copyright[] =
62 "@(#) Copyright (c) 1988, 1993, 1994\n\
63 The Regents of the University of California. All rights reserved.\n";
68 static char sccsid[] = "@(#)su.c 8.3 (Berkeley) 4/2/94";
72 #include <sys/cdefs.h>
73 __FBSDID("$FreeBSD$");
75 #include <sys/param.h>
77 #include <sys/resource.h>
81 #include <bsm/libbsm.h>
82 #include <bsm/audit_uevents.h>
88 #include <login_cap.h>
99 #include <security/pam_appl.h>
100 #include <security/openpam.h>
102 #define PAM_END() do { \
104 if (pamh != NULL) { \
105 local_ret = pam_setcred(pamh, PAM_DELETE_CRED); \
106 if (local_ret != PAM_SUCCESS) \
107 syslog(LOG_ERR, "pam_setcred: %s", \
108 pam_strerror(pamh, local_ret)); \
110 local_ret = pam_close_session(pamh, 0); \
111 if (local_ret != PAM_SUCCESS) \
112 syslog(LOG_ERR, "pam_close_session: %s",\
113 pam_strerror(pamh, local_ret)); \
115 local_ret = pam_end(pamh, local_ret); \
116 if (local_ret != PAM_SUCCESS) \
117 syslog(LOG_ERR, "pam_end: %s", \
118 pam_strerror(pamh, local_ret)); \
123 #define PAM_SET_ITEM(what, item) do { \
125 local_ret = pam_set_item(pamh, what, item); \
126 if (local_ret != PAM_SUCCESS) { \
127 syslog(LOG_ERR, "pam_set_item(" #what "): %s", \
128 pam_strerror(pamh, local_ret)); \
129 errx(1, "pam_set_item(" #what "): %s", \
130 pam_strerror(pamh, local_ret)); \
135 enum tristate { UNSET, YES, NO };
137 static pam_handle_t *pamh = NULL;
138 static char **environ_pam;
140 static char *ontty(void);
141 static int chshell(const char *);
142 static void usage(void) __dead2;
143 static void export_pam_environment(void);
144 static int ok_to_export(const char *);
146 extern char **environ;
149 main(int argc, char *argv[])
151 static char *cleanenv;
152 struct passwd *pwd = NULL;
153 struct pam_conv conv = { openpam_ttyconv, NULL };
161 pid_t child_pid, child_pgrp, pid;
162 int asme, ch, asthem, fastlogin, prio, i, retcode,
163 statusp, setmaclabel;
165 char *username, *class, shellbuf[MAXPATHLEN];
166 const char *p, *user, *shell, *mytty, **nargv;
168 struct sigaction sa, sa_int, sa_quit, sa_pipe;
175 shell = class = cleanenv = NULL;
176 asme = asthem = fastlogin = statusp = 0;
181 while ((ch = getopt(argc, argv, "-flmsc:")) != -1)
208 user = argv[optind++];
215 * Try to provide more helpful debugging output if su(1) is running
216 * non-setuid, or was run from a file system not mounted setuid.
219 errx(1, "not running setuid");
222 if (getauid(&auid) < 0 && errno != ENOSYS) {
223 syslog(LOG_AUTH | LOG_ERR, "getauid: %s", strerror(errno));
224 errx(1, "Permission denied");
227 if (strlen(user) > MAXLOGNAME - 1) {
229 if (audit_submit(AUE_su, auid,
230 EPERM, 1, "username too long: '%s'", user))
231 errx(1, "Permission denied");
233 errx(1, "username too long");
236 nargv = malloc(sizeof(char *) * (size_t)(argc + 4));
238 errx(1, "malloc failure");
240 nargv[argc + 3] = NULL;
241 for (i = argc; i >= optind; i--)
242 nargv[i + 3] = argv[i];
243 np.a = &nargv[i + 3];
248 prio = getpriority(PRIO_PROCESS, 0);
252 setpriority(PRIO_PROCESS, 0, -2);
253 openlog("su", LOG_CONS, LOG_AUTH);
255 /* get current login name, real uid and shell */
257 username = getlogin();
258 if (username != NULL)
259 pwd = getpwnam(username);
260 if (pwd == NULL || pwd->pw_uid != ruid)
261 pwd = getpwuid(ruid);
264 if (audit_submit(AUE_su, auid, EPERM, 1,
265 "unable to determine invoking subject: '%s'", username))
266 errx(1, "Permission denied");
268 errx(1, "who are you?");
271 username = strdup(pwd->pw_name);
272 if (username == NULL)
273 err(1, "strdup failure");
276 if (pwd->pw_shell != NULL && *pwd->pw_shell != '\0') {
277 /* must copy - pwd memory is recycled */
278 shell = strncpy(shellbuf, pwd->pw_shell,
280 shellbuf[sizeof(shellbuf) - 1] = '\0';
283 shell = _PATH_BSHELL;
288 /* Do the whole PAM startup thing */
289 retcode = pam_start("su", user, &conv, &pamh);
290 if (retcode != PAM_SUCCESS) {
291 syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, retcode));
292 errx(1, "pam_start: %s", pam_strerror(pamh, retcode));
295 PAM_SET_ITEM(PAM_RUSER, username);
297 mytty = ttyname(STDERR_FILENO);
300 PAM_SET_ITEM(PAM_TTY, mytty);
302 retcode = pam_authenticate(pamh, 0);
303 if (retcode != PAM_SUCCESS) {
305 if (audit_submit(AUE_su, auid, EPERM, 1, "bad su %s to %s on %s",
306 username, user, mytty))
307 errx(1, "Permission denied");
309 syslog(LOG_AUTH|LOG_WARNING, "BAD SU %s to %s on %s",
310 username, user, mytty);
314 if (audit_submit(AUE_su, auid, 0, 0, "successful authentication"))
315 errx(1, "Permission denied");
317 retcode = pam_get_item(pamh, PAM_USER, &v);
318 if (retcode == PAM_SUCCESS)
321 syslog(LOG_ERR, "pam_get_item(PAM_USER): %s",
322 pam_strerror(pamh, retcode));
323 pwd = getpwnam(user);
326 if (audit_submit(AUE_su, auid, EPERM, 1,
327 "unknown subject: %s", user))
328 errx(1, "Permission denied");
330 errx(1, "unknown login: %s", user);
333 retcode = pam_acct_mgmt(pamh, 0);
334 if (retcode == PAM_NEW_AUTHTOK_REQD) {
335 retcode = pam_chauthtok(pamh,
336 PAM_CHANGE_EXPIRED_AUTHTOK);
337 if (retcode != PAM_SUCCESS) {
339 aerr = pam_strerror(pamh, retcode);
341 aerr = "Unknown PAM error";
342 if (audit_submit(AUE_su, auid, EPERM, 1,
343 "pam_chauthtok: %s", aerr))
344 errx(1, "Permission denied");
346 syslog(LOG_ERR, "pam_chauthtok: %s",
347 pam_strerror(pamh, retcode));
351 if (retcode != PAM_SUCCESS) {
353 if (audit_submit(AUE_su, auid, EPERM, 1, "pam_acct_mgmt: %s",
354 pam_strerror(pamh, retcode)))
355 errx(1, "Permission denied");
357 syslog(LOG_ERR, "pam_acct_mgmt: %s",
358 pam_strerror(pamh, retcode));
362 /* get target login information */
364 lc = login_getpwclass(pwd);
368 if (audit_submit(AUE_su, auid, EPERM, 1,
369 "only root may use -c"))
370 errx(1, "Permission denied");
372 errx(1, "only root may use -c");
374 lc = login_getclass(class);
376 err(1, "login_getclass");
377 if (lc->lc_class == NULL || strcmp(class, lc->lc_class) != 0)
378 errx(1, "unknown class: %s", class);
381 /* if asme and non-standard target shell, must be root */
383 if (ruid != 0 && !chshell(pwd->pw_shell))
384 errx(1, "permission denied (shell)");
386 else if (pwd->pw_shell && *pwd->pw_shell) {
387 shell = pwd->pw_shell;
391 shell = _PATH_BSHELL;
395 /* if we're forking a csh, we want to slightly muck the args */
396 if (iscsh == UNSET) {
397 p = strrchr(shell, '/');
402 iscsh = strcmp(p, "csh") ? (strcmp(p, "tcsh") ? NO : YES) : YES;
404 setpriority(PRIO_PROCESS, 0, prio);
407 * PAM modules might add supplementary groups in pam_setcred(), so
408 * initialize them first.
410 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) < 0)
411 err(1, "setusercontext");
413 retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED);
414 if (retcode != PAM_SUCCESS) {
415 syslog(LOG_ERR, "pam_setcred: %s",
416 pam_strerror(pamh, retcode));
417 errx(1, "failed to establish credentials.");
420 retcode = pam_open_session(pamh, 0);
421 if (retcode != PAM_SUCCESS) {
422 syslog(LOG_ERR, "pam_open_session: %s",
423 pam_strerror(pamh, retcode));
424 errx(1, "failed to open session.");
429 * We must fork() before setuid() because we need to call
430 * pam_setcred(pamh, PAM_DELETE_CRED) as root.
432 sa.sa_flags = SA_RESTART;
433 sa.sa_handler = SIG_IGN;
434 sigemptyset(&sa.sa_mask);
435 sigaction(SIGINT, &sa, &sa_int);
436 sigaction(SIGQUIT, &sa, &sa_quit);
437 sigaction(SIGPIPE, &sa, &sa_pipe);
438 sa.sa_handler = SIG_DFL;
439 sigaction(SIGTSTP, &sa, NULL);
441 if (pipe(fds) == -1) {
448 sa.sa_handler = SIG_IGN;
449 sigaction(SIGTTOU, &sa, NULL);
451 setpgid(child_pid, child_pid);
452 if (tcgetpgrp(STDERR_FILENO) == getpgrp())
453 tcsetpgrp(STDERR_FILENO, child_pid);
455 sigaction(SIGPIPE, &sa_pipe, NULL);
456 while ((pid = waitpid(child_pid, &statusp, WUNTRACED)) != -1) {
457 if (WIFSTOPPED(statusp)) {
458 child_pgrp = getpgid(child_pid);
459 if (tcgetpgrp(STDERR_FILENO) == child_pgrp)
460 tcsetpgrp(STDERR_FILENO, getpgrp());
461 kill(getpid(), SIGSTOP);
462 if (tcgetpgrp(STDERR_FILENO) == getpgrp()) {
463 child_pgrp = getpgid(child_pid);
464 tcsetpgrp(STDERR_FILENO, child_pgrp);
466 kill(child_pid, SIGCONT);
472 tcsetpgrp(STDERR_FILENO, getpgrp());
476 exit(WEXITSTATUS(statusp));
482 read(fds[0], &temp, 1);
484 sigaction(SIGPIPE, &sa_pipe, NULL);
485 sigaction(SIGINT, &sa_int, NULL);
486 sigaction(SIGQUIT, &sa_quit, NULL);
489 * Set all user context except for: Environmental variables
490 * Umask Login records (wtmp, etc) Path
492 setwhat = LOGIN_SETALL & ~(LOGIN_SETENV | LOGIN_SETUMASK |
493 LOGIN_SETLOGIN | LOGIN_SETPATH | LOGIN_SETGROUP |
496 * If -s is present, also set the MAC label.
499 setwhat |= LOGIN_SETMAC;
501 * Don't touch resource/priority settings if -m has been used
502 * or -l and -c hasn't, and we're not su'ing to root.
504 if ((asme || (!asthem && class == NULL)) && pwd->pw_uid)
505 setwhat &= ~(LOGIN_SETPRIORITY | LOGIN_SETRESOURCES);
506 if (setusercontext(lc, pwd, pwd->pw_uid, setwhat) < 0)
507 err(1, "setusercontext");
515 if (asthem || pwd->pw_uid)
516 setenv("USER", pwd->pw_name, 1);
517 setenv("HOME", pwd->pw_dir, 1);
518 setenv("SHELL", shell, 1);
522 * Add any environmental variables that the
523 * PAM modules may have set.
525 environ_pam = pam_getenvlist(pamh);
527 export_pam_environment();
529 /* set the su'd user's environment & umask */
530 setusercontext(lc, pwd, pwd->pw_uid,
531 LOGIN_SETPATH | LOGIN_SETUMASK |
534 setenv("TERM", p, 1);
536 p = pam_getenv(pamh, "HOME");
537 if (chdir(p ? p : pwd->pw_dir) < 0)
538 errx(1, "no directory");
549 /* csh strips the first character... */
550 *np.a = asthem ? "-su" : iscsh == YES ? "_su" : "su";
553 syslog(LOG_NOTICE, "%s to %s%s", username, user,
562 export_pam_environment(void)
567 for (pp = environ_pam; *pp != NULL; pp++) {
568 if (ok_to_export(*pp)) {
569 p = strchr(*pp, '=');
571 setenv(*pp, p + 1, 1);
578 * Sanity checks on PAM environmental variables:
579 * - Make sure there is an '=' in the string.
580 * - Make sure the string doesn't run on too long.
581 * - Do not export certain variables. This list was taken from the
582 * Solaris pam_putenv(3) man page.
583 * Note that if the user is chrooted, PAM may have a better idea than we
584 * do of where her home directory is.
587 ok_to_export(const char *s)
589 static const char *noexport[] = {
590 "SHELL", /* "HOME", */ "LOGNAME", "MAIL", "CDPATH",
596 if (strlen(s) > 1024 || strchr(s, '=') == NULL)
598 if (strncmp(s, "LD_", 3) == 0)
600 for (pp = noexport; *pp != NULL; pp++) {
602 if (s[n] == '=' && strncmp(s, *pp, n) == 0)
612 fprintf(stderr, "usage: su [-] [-flms] [-c class] [login [args]]\n");
618 chshell(const char *sh)
625 while ((cp = getusershell()) != NULL && !r)
626 r = (strcmp(cp, sh) == 0);
635 static char buf[MAXPATHLEN + 4];
638 p = ttyname(STDERR_FILENO);
640 snprintf(buf, sizeof(buf), " on %s", p);