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 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 static const char copyright[] =
66 "@(#) Copyright (c) 1988, 1993, 1994\n\
67 The Regents of the University of California. All rights reserved.\n";
72 static char sccsid[] = "@(#)su.c 8.3 (Berkeley) 4/2/94";
76 #include <sys/cdefs.h>
77 __FBSDID("$FreeBSD$");
79 #include <sys/param.h>
81 #include <sys/resource.h>
85 #include <bsm/libbsm.h>
86 #include <bsm/audit_uevents.h>
92 #include <login_cap.h>
103 #include <security/pam_appl.h>
104 #include <security/openpam.h>
106 #define PAM_END() do { \
108 if (pamh != NULL) { \
109 local_ret = pam_setcred(pamh, PAM_DELETE_CRED); \
110 if (local_ret != PAM_SUCCESS) \
111 syslog(LOG_ERR, "pam_setcred: %s", \
112 pam_strerror(pamh, local_ret)); \
114 local_ret = pam_close_session(pamh, 0); \
115 if (local_ret != PAM_SUCCESS) \
116 syslog(LOG_ERR, "pam_close_session: %s",\
117 pam_strerror(pamh, local_ret)); \
119 local_ret = pam_end(pamh, local_ret); \
120 if (local_ret != PAM_SUCCESS) \
121 syslog(LOG_ERR, "pam_end: %s", \
122 pam_strerror(pamh, local_ret)); \
127 #define PAM_SET_ITEM(what, item) do { \
129 local_ret = pam_set_item(pamh, what, item); \
130 if (local_ret != PAM_SUCCESS) { \
131 syslog(LOG_ERR, "pam_set_item(" #what "): %s", \
132 pam_strerror(pamh, local_ret)); \
133 errx(1, "pam_set_item(" #what "): %s", \
134 pam_strerror(pamh, local_ret)); \
139 enum tristate { UNSET, YES, NO };
141 static pam_handle_t *pamh = NULL;
142 static char **environ_pam;
144 static char *ontty(void);
145 static int chshell(const char *);
146 static void usage(void) __dead2;
147 static void export_pam_environment(void);
148 static int ok_to_export(const char *);
150 extern char **environ;
153 main(int argc, char *argv[])
155 static char *cleanenv;
156 struct passwd *pwd = NULL;
157 struct pam_conv conv = { openpam_ttyconv, NULL };
165 pid_t child_pid, child_pgrp, pid;
166 int asme, ch, asthem, fastlogin, prio, i, retcode,
167 statusp, setmaclabel;
169 char *username, *class, shellbuf[MAXPATHLEN];
170 const char *p, *user, *shell, *mytty, **nargv;
172 struct sigaction sa, sa_int, sa_quit, sa_pipe;
179 shell = class = cleanenv = NULL;
180 asme = asthem = fastlogin = statusp = 0;
185 while ((ch = getopt(argc, argv, "-flmsc:")) != -1)
212 user = argv[optind++];
219 * Try to provide more helpful debugging output if su(1) is running
220 * non-setuid, or was run from a file system not mounted setuid.
223 errx(1, "not running setuid");
226 if (getauid(&auid) < 0 && errno != ENOSYS) {
227 syslog(LOG_AUTH | LOG_ERR, "getauid: %s", strerror(errno));
228 errx(1, "Permission denied");
231 if (strlen(user) > MAXLOGNAME - 1) {
233 if (audit_submit(AUE_su, auid,
234 EPERM, 1, "username too long: '%s'", user))
235 errx(1, "Permission denied");
237 errx(1, "username too long");
240 nargv = malloc(sizeof(char *) * (size_t)(argc + 4));
242 errx(1, "malloc failure");
244 nargv[argc + 3] = NULL;
245 for (i = argc; i >= optind; i--)
246 nargv[i + 3] = argv[i];
247 np.a = &nargv[i + 3];
252 prio = getpriority(PRIO_PROCESS, 0);
256 setpriority(PRIO_PROCESS, 0, -2);
257 openlog("su", LOG_CONS, LOG_AUTH);
259 /* get current login name, real uid and shell */
261 username = getlogin();
262 if (username != NULL)
263 pwd = getpwnam(username);
264 if (pwd == NULL || pwd->pw_uid != ruid)
265 pwd = getpwuid(ruid);
268 if (audit_submit(AUE_su, auid, EPERM, 1,
269 "unable to determine invoking subject: '%s'", username))
270 errx(1, "Permission denied");
272 errx(1, "who are you?");
275 username = strdup(pwd->pw_name);
276 if (username == NULL)
277 err(1, "strdup failure");
280 if (pwd->pw_shell != NULL && *pwd->pw_shell != '\0') {
281 /* must copy - pwd memory is recycled */
282 shell = strncpy(shellbuf, pwd->pw_shell,
284 shellbuf[sizeof(shellbuf) - 1] = '\0';
287 shell = _PATH_BSHELL;
292 /* Do the whole PAM startup thing */
293 retcode = pam_start("su", user, &conv, &pamh);
294 if (retcode != PAM_SUCCESS) {
295 syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, retcode));
296 errx(1, "pam_start: %s", pam_strerror(pamh, retcode));
299 PAM_SET_ITEM(PAM_RUSER, username);
301 mytty = ttyname(STDERR_FILENO);
304 PAM_SET_ITEM(PAM_TTY, mytty);
306 retcode = pam_authenticate(pamh, 0);
307 if (retcode != PAM_SUCCESS) {
309 if (audit_submit(AUE_su, auid, EPERM, 1, "bad su %s to %s on %s",
310 username, user, mytty))
311 errx(1, "Permission denied");
313 syslog(LOG_AUTH|LOG_WARNING, "BAD SU %s to %s on %s",
314 username, user, mytty);
318 if (audit_submit(AUE_su, auid, 0, 0, "successful authentication"))
319 errx(1, "Permission denied");
321 retcode = pam_get_item(pamh, PAM_USER, &v);
322 if (retcode == PAM_SUCCESS)
325 syslog(LOG_ERR, "pam_get_item(PAM_USER): %s",
326 pam_strerror(pamh, retcode));
327 pwd = getpwnam(user);
330 if (audit_submit(AUE_su, auid, EPERM, 1,
331 "unknown subject: %s", user))
332 errx(1, "Permission denied");
334 errx(1, "unknown login: %s", user);
337 retcode = pam_acct_mgmt(pamh, 0);
338 if (retcode == PAM_NEW_AUTHTOK_REQD) {
339 retcode = pam_chauthtok(pamh,
340 PAM_CHANGE_EXPIRED_AUTHTOK);
341 if (retcode != PAM_SUCCESS) {
343 aerr = pam_strerror(pamh, retcode);
345 aerr = "Unknown PAM error";
346 if (audit_submit(AUE_su, auid, EPERM, 1,
347 "pam_chauthtok: %s", aerr))
348 errx(1, "Permission denied");
350 syslog(LOG_ERR, "pam_chauthtok: %s",
351 pam_strerror(pamh, retcode));
355 if (retcode != PAM_SUCCESS) {
357 if (audit_submit(AUE_su, auid, EPERM, 1, "pam_acct_mgmt: %s",
358 pam_strerror(pamh, retcode)))
359 errx(1, "Permission denied");
361 syslog(LOG_ERR, "pam_acct_mgmt: %s",
362 pam_strerror(pamh, retcode));
366 /* get target login information */
368 lc = login_getpwclass(pwd);
372 if (audit_submit(AUE_su, auid, EPERM, 1,
373 "only root may use -c"))
374 errx(1, "Permission denied");
376 errx(1, "only root may use -c");
378 lc = login_getclass(class);
380 errx(1, "unknown class: %s", class);
383 /* if asme and non-standard target shell, must be root */
385 if (ruid != 0 && !chshell(pwd->pw_shell))
386 errx(1, "permission denied (shell)");
388 else if (pwd->pw_shell && *pwd->pw_shell) {
389 shell = pwd->pw_shell;
393 shell = _PATH_BSHELL;
397 /* if we're forking a csh, we want to slightly muck the args */
398 if (iscsh == UNSET) {
399 p = strrchr(shell, '/');
404 iscsh = strcmp(p, "csh") ? (strcmp(p, "tcsh") ? NO : YES) : YES;
406 setpriority(PRIO_PROCESS, 0, prio);
409 * PAM modules might add supplementary groups in pam_setcred(), so
410 * initialize them first.
412 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) < 0)
413 err(1, "setusercontext");
415 retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED);
416 if (retcode != PAM_SUCCESS) {
417 syslog(LOG_ERR, "pam_setcred: %s",
418 pam_strerror(pamh, retcode));
419 errx(1, "failed to establish credentials.");
422 retcode = pam_open_session(pamh, 0);
423 if (retcode != PAM_SUCCESS) {
424 syslog(LOG_ERR, "pam_open_session: %s",
425 pam_strerror(pamh, retcode));
426 errx(1, "failed to open session.");
431 * We must fork() before setuid() because we need to call
432 * pam_setcred(pamh, PAM_DELETE_CRED) as root.
434 sa.sa_flags = SA_RESTART;
435 sa.sa_handler = SIG_IGN;
436 sigemptyset(&sa.sa_mask);
437 sigaction(SIGINT, &sa, &sa_int);
438 sigaction(SIGQUIT, &sa, &sa_quit);
439 sigaction(SIGPIPE, &sa, &sa_pipe);
440 sa.sa_handler = SIG_DFL;
441 sigaction(SIGTSTP, &sa, NULL);
443 if (pipe(fds) == -1) {
450 sa.sa_handler = SIG_IGN;
451 sigaction(SIGTTOU, &sa, NULL);
453 setpgid(child_pid, child_pid);
454 if (tcgetpgrp(STDERR_FILENO) == getpgrp())
455 tcsetpgrp(STDERR_FILENO, child_pid);
457 sigaction(SIGPIPE, &sa_pipe, NULL);
458 while ((pid = waitpid(child_pid, &statusp, WUNTRACED)) != -1) {
459 if (WIFSTOPPED(statusp)) {
460 child_pgrp = getpgid(child_pid);
461 if (tcgetpgrp(STDERR_FILENO) == child_pgrp)
462 tcsetpgrp(STDERR_FILENO, getpgrp());
463 kill(getpid(), SIGSTOP);
464 if (tcgetpgrp(STDERR_FILENO) == getpgrp()) {
465 child_pgrp = getpgid(child_pid);
466 tcsetpgrp(STDERR_FILENO, child_pgrp);
468 kill(child_pid, SIGCONT);
474 tcsetpgrp(STDERR_FILENO, getpgrp());
478 exit(WEXITSTATUS(statusp));
484 read(fds[0], &temp, 1);
486 sigaction(SIGPIPE, &sa_pipe, NULL);
487 sigaction(SIGINT, &sa_int, NULL);
488 sigaction(SIGQUIT, &sa_quit, NULL);
491 * Set all user context except for: Environmental variables
492 * Umask Login records (wtmp, etc) Path
494 setwhat = LOGIN_SETALL & ~(LOGIN_SETENV | LOGIN_SETUMASK |
495 LOGIN_SETLOGIN | LOGIN_SETPATH | LOGIN_SETGROUP |
498 * If -s is present, also set the MAC label.
501 setwhat |= LOGIN_SETMAC;
503 * Don't touch resource/priority settings if -m has been used
504 * or -l and -c hasn't, and we're not su'ing to root.
506 if ((asme || (!asthem && class == NULL)) && pwd->pw_uid)
507 setwhat &= ~(LOGIN_SETPRIORITY | LOGIN_SETRESOURCES);
508 if (setusercontext(lc, pwd, pwd->pw_uid, setwhat) < 0)
509 err(1, "setusercontext");
517 if (asthem || pwd->pw_uid)
518 setenv("USER", pwd->pw_name, 1);
519 setenv("HOME", pwd->pw_dir, 1);
520 setenv("SHELL", shell, 1);
524 * Add any environmental variables that the
525 * PAM modules may have set.
527 environ_pam = pam_getenvlist(pamh);
529 export_pam_environment();
531 /* set the su'd user's environment & umask */
532 setusercontext(lc, pwd, pwd->pw_uid,
533 LOGIN_SETPATH | LOGIN_SETUMASK |
536 setenv("TERM", p, 1);
538 p = pam_getenv(pamh, "HOME");
539 if (chdir(p ? p : pwd->pw_dir) < 0)
540 errx(1, "no directory");
551 /* csh strips the first character... */
552 *np.a = asthem ? "-su" : iscsh == YES ? "_su" : "su";
555 syslog(LOG_NOTICE, "%s to %s%s", username, user,
564 export_pam_environment(void)
569 for (pp = environ_pam; *pp != NULL; pp++) {
570 if (ok_to_export(*pp)) {
571 p = strchr(*pp, '=');
573 setenv(*pp, p + 1, 1);
580 * Sanity checks on PAM environmental variables:
581 * - Make sure there is an '=' in the string.
582 * - Make sure the string doesn't run on too long.
583 * - Do not export certain variables. This list was taken from the
584 * Solaris pam_putenv(3) man page.
585 * Note that if the user is chrooted, PAM may have a better idea than we
586 * do of where her home directory is.
589 ok_to_export(const char *s)
591 static const char *noexport[] = {
592 "SHELL", /* "HOME", */ "LOGNAME", "MAIL", "CDPATH",
598 if (strlen(s) > 1024 || strchr(s, '=') == NULL)
600 if (strncmp(s, "LD_", 3) == 0)
602 for (pp = noexport; *pp != NULL; pp++) {
604 if (s[n] == '=' && strncmp(s, *pp, n) == 0)
614 fprintf(stderr, "usage: su [-] [-flms] [-c class] [login [args]]\n");
620 chshell(const char *sh)
627 while ((cp = getusershell()) != NULL && !r)
628 r = (strcmp(cp, sh) == 0);
637 static char buf[MAXPATHLEN + 4];
640 p = ttyname(STDERR_FILENO);
642 snprintf(buf, sizeof(buf), " on %s", p);