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>
87 #include <login_cap.h>
97 #include <security/pam_appl.h>
98 #include <security/openpam.h>
100 #define PAM_END() do { \
102 if (pamh != NULL) { \
103 local_ret = pam_setcred(pamh, PAM_DELETE_CRED); \
104 if (local_ret != PAM_SUCCESS) \
105 syslog(LOG_ERR, "pam_setcred: %s", \
106 pam_strerror(pamh, local_ret)); \
108 local_ret = pam_close_session(pamh, 0); \
109 if (local_ret != PAM_SUCCESS) \
110 syslog(LOG_ERR, "pam_close_session: %s",\
111 pam_strerror(pamh, local_ret)); \
113 local_ret = pam_end(pamh, local_ret); \
114 if (local_ret != PAM_SUCCESS) \
115 syslog(LOG_ERR, "pam_end: %s", \
116 pam_strerror(pamh, local_ret)); \
121 #define PAM_SET_ITEM(what, item) do { \
123 local_ret = pam_set_item(pamh, what, item); \
124 if (local_ret != PAM_SUCCESS) { \
125 syslog(LOG_ERR, "pam_set_item(" #what "): %s", \
126 pam_strerror(pamh, local_ret)); \
127 errx(1, "pam_set_item(" #what "): %s", \
128 pam_strerror(pamh, local_ret)); \
133 enum tristate { UNSET, YES, NO };
135 static pam_handle_t *pamh = NULL;
136 static char **environ_pam;
138 static char *ontty(void);
139 static int chshell(const char *);
140 static void usage(void) __dead2;
141 static void export_pam_environment(void);
142 static int ok_to_export(const char *);
144 extern char **environ;
147 main(int argc, char *argv[])
149 static char *cleanenv;
151 struct pam_conv conv = { openpam_ttyconv, NULL };
159 pid_t child_pid, child_pgrp, pid;
160 int asme, ch, asthem, fastlogin, prio, i, retcode,
161 statusp, setmaclabel;
163 char *username, *class, shellbuf[MAXPATHLEN];
164 const char *p, *user, *shell, *mytty, **nargv;
165 struct sigaction sa, sa_int, sa_quit, sa_pipe;
168 shell = class = cleanenv = NULL;
169 asme = asthem = fastlogin = statusp = 0;
174 while ((ch = getopt(argc, argv, "-flmsc:")) != -1)
201 user = argv[optind++];
207 if (strlen(user) > MAXLOGNAME - 1)
208 errx(1, "username too long");
211 * Try to provide more helpful debugging output if su(1) is running
212 * non-setuid, or was run from a file system not mounted setuid.
215 errx(1, "not running setuid");
217 nargv = malloc(sizeof(char *) * (size_t)(argc + 4));
219 errx(1, "malloc failure");
221 nargv[argc + 3] = NULL;
222 for (i = argc; i >= optind; i--)
223 nargv[i + 3] = argv[i];
224 np.a = &nargv[i + 3];
229 prio = getpriority(PRIO_PROCESS, 0);
233 setpriority(PRIO_PROCESS, 0, -2);
234 openlog("su", LOG_CONS, LOG_AUTH);
236 /* get current login name, real uid and shell */
238 username = getlogin();
239 pwd = getpwnam(username);
240 if (username == NULL || pwd == NULL || pwd->pw_uid != ruid)
241 pwd = getpwuid(ruid);
243 errx(1, "who are you?");
245 username = strdup(pwd->pw_name);
246 if (username == NULL)
247 err(1, "strdup failure");
250 if (pwd->pw_shell != NULL && *pwd->pw_shell != '\0') {
251 /* must copy - pwd memory is recycled */
252 shell = strncpy(shellbuf, pwd->pw_shell,
254 shellbuf[sizeof(shellbuf) - 1] = '\0';
257 shell = _PATH_BSHELL;
262 /* Do the whole PAM startup thing */
263 retcode = pam_start("su", user, &conv, &pamh);
264 if (retcode != PAM_SUCCESS) {
265 syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, retcode));
266 errx(1, "pam_start: %s", pam_strerror(pamh, retcode));
269 PAM_SET_ITEM(PAM_RUSER, username);
271 mytty = ttyname(STDERR_FILENO);
274 PAM_SET_ITEM(PAM_TTY, mytty);
276 retcode = pam_authenticate(pamh, 0);
277 if (retcode != PAM_SUCCESS) {
278 syslog(LOG_AUTH|LOG_WARNING, "BAD SU %s to %s on %s",
279 username, user, mytty);
282 retcode = pam_get_item(pamh, PAM_USER, (const void **)&p);
283 if (retcode == PAM_SUCCESS)
286 syslog(LOG_ERR, "pam_get_item(PAM_USER): %s",
287 pam_strerror(pamh, retcode));
288 pwd = getpwnam(user);
290 errx(1, "unknown login: %s", user);
292 retcode = pam_acct_mgmt(pamh, 0);
293 if (retcode == PAM_NEW_AUTHTOK_REQD) {
294 retcode = pam_chauthtok(pamh,
295 PAM_CHANGE_EXPIRED_AUTHTOK);
296 if (retcode != PAM_SUCCESS) {
297 syslog(LOG_ERR, "pam_chauthtok: %s",
298 pam_strerror(pamh, retcode));
302 if (retcode != PAM_SUCCESS) {
303 syslog(LOG_ERR, "pam_acct_mgmt: %s",
304 pam_strerror(pamh, retcode));
308 /* get target login information */
310 lc = login_getpwclass(pwd);
313 errx(1, "only root may use -c");
314 lc = login_getclass(class);
316 errx(1, "unknown class: %s", class);
319 /* if asme and non-standard target shell, must be root */
321 if (ruid != 0 && !chshell(pwd->pw_shell))
322 errx(1, "permission denied (shell)");
324 else if (pwd->pw_shell && *pwd->pw_shell) {
325 shell = pwd->pw_shell;
329 shell = _PATH_BSHELL;
333 /* if we're forking a csh, we want to slightly muck the args */
334 if (iscsh == UNSET) {
335 p = strrchr(shell, '/');
340 iscsh = strcmp(p, "csh") ? (strcmp(p, "tcsh") ? NO : YES) : YES;
342 setpriority(PRIO_PROCESS, 0, prio);
344 /* Switch to home directory */
346 if (chdir(pwd->pw_dir) < 0)
347 errx(1, "no directory");
351 * PAM modules might add supplementary groups in pam_setcred(), so
352 * initialize them first.
354 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) < 0)
355 err(1, "setusercontext");
357 retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED);
358 if (retcode != PAM_SUCCESS) {
359 syslog(LOG_ERR, "pam_setcred: %s",
360 pam_strerror(pamh, retcode));
361 errx(1, "failed to establish credentials.");
364 retcode = pam_open_session(pamh, 0);
365 if (retcode != PAM_SUCCESS) {
366 syslog(LOG_ERR, "pam_open_session: %s",
367 pam_strerror(pamh, retcode));
368 errx(1, "failed to open session.");
373 * We must fork() before setuid() because we need to call
374 * pam_setcred(pamh, PAM_DELETE_CRED) as root.
376 sa.sa_flags = SA_RESTART;
377 sa.sa_handler = SIG_IGN;
378 sigemptyset(&sa.sa_mask);
379 sigaction(SIGINT, &sa, &sa_int);
380 sigaction(SIGQUIT, &sa, &sa_quit);
381 sigaction(SIGPIPE, &sa, &sa_pipe);
382 sa.sa_handler = SIG_DFL;
383 sigaction(SIGTSTP, &sa, NULL);
385 if (pipe(fds) == -1) {
392 sa.sa_handler = SIG_IGN;
393 sigaction(SIGTTOU, &sa, NULL);
395 setpgid(child_pid, child_pid);
396 tcsetpgrp(STDERR_FILENO, child_pid);
398 sigaction(SIGPIPE, &sa_pipe, NULL);
399 while ((pid = waitpid(child_pid, &statusp, WUNTRACED)) != -1) {
400 if (WIFSTOPPED(statusp)) {
401 kill(getpid(), SIGSTOP);
402 child_pgrp = getpgid(child_pid);
403 tcsetpgrp(1, child_pgrp);
404 kill(child_pid, SIGCONT);
410 tcsetpgrp(STDERR_FILENO, getpgrp());
414 exit(WEXITSTATUS(statusp));
420 read(fds[0], &temp, 1);
422 sigaction(SIGPIPE, &sa_pipe, NULL);
423 sigaction(SIGINT, &sa_int, NULL);
424 sigaction(SIGQUIT, &sa_quit, NULL);
427 * Set all user context except for: Environmental variables
428 * Umask Login records (wtmp, etc) Path
430 setwhat = LOGIN_SETALL & ~(LOGIN_SETENV | LOGIN_SETUMASK |
431 LOGIN_SETLOGIN | LOGIN_SETPATH | LOGIN_SETGROUP |
434 * If -s is present, also set the MAC label.
437 setwhat |= LOGIN_SETMAC;
439 * Don't touch resource/priority settings if -m has been used
440 * or -l and -c hasn't, and we're not su'ing to root.
442 if ((asme || (!asthem && class == NULL)) && pwd->pw_uid)
443 setwhat &= ~(LOGIN_SETPRIORITY | LOGIN_SETRESOURCES);
444 if (setusercontext(lc, pwd, pwd->pw_uid, setwhat) < 0)
445 err(1, "setusercontext");
453 if (asthem || pwd->pw_uid)
454 setenv("USER", pwd->pw_name, 1);
455 setenv("HOME", pwd->pw_dir, 1);
456 setenv("SHELL", shell, 1);
460 * Add any environmental variables that the
461 * PAM modules may have set.
463 environ_pam = pam_getenvlist(pamh);
465 export_pam_environment();
467 /* set the su'd user's environment & umask */
468 setusercontext(lc, pwd, pwd->pw_uid,
469 LOGIN_SETPATH | LOGIN_SETUMASK |
472 setenv("TERM", p, 1);
483 /* csh strips the first character... */
484 *np.a = asthem ? "-su" : iscsh == YES ? "_su" : "su";
487 syslog(LOG_NOTICE, "%s to %s%s", username, user,
496 export_pam_environment(void)
500 for (pp = environ_pam; *pp != NULL; pp++) {
501 if (ok_to_export(*pp))
508 * Sanity checks on PAM environmental variables:
509 * - Make sure there is an '=' in the string.
510 * - Make sure the string doesn't run on too long.
511 * - Do not export certain variables. This list was taken from the
512 * Solaris pam_putenv(3) man page.
513 * Note that if the user is chrooted, PAM may have a better idea than we
514 * do of where her home directory is.
517 ok_to_export(const char *s)
519 static const char *noexport[] = {
520 "SHELL", /* "HOME", */ "LOGNAME", "MAIL", "CDPATH",
526 if (strlen(s) > 1024 || strchr(s, '=') == NULL)
528 if (strncmp(s, "LD_", 3) == 0)
530 for (pp = noexport; *pp != NULL; pp++) {
532 if (s[n] == '=' && strncmp(s, *pp, n) == 0)
542 fprintf(stderr, "usage: su [-] [-flms] [-c class] [login [args]]\n");
548 chshell(const char *sh)
555 while ((cp = getusershell()) != NULL && !r)
556 r = (strcmp(cp, sh) == 0);
565 static char buf[MAXPATHLEN + 4];
568 p = ttyname(STDERR_FILENO);
570 snprintf(buf, sizeof(buf), " on %s", p);