1 /* 2 * Copyright (c) 2002, 2005 Networks Associates Technologies, Inc. 3 * All rights reserved. 4 * 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. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 */ 31 /*- 32 * Copyright (c) 1988, 1993, 1994 33 * The Regents of the University of California. All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 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. 46 * 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 57 * SUCH DAMAGE. 58 */ 59 60 #ifndef lint 61 static const char copyright[] = 62 "@(#) Copyright (c) 1988, 1993, 1994\n\ 63 The Regents of the University of California. All rights reserved.\n"; 64 #endif /* not lint */ 65 66 #if 0 67 #ifndef lint 68 static char sccsid[] = "@(#)su.c 8.3 (Berkeley) 4/2/94"; 69 #endif /* not lint */ 70 #endif 71 72 #include <sys/cdefs.h> 73 __FBSDID("$FreeBSD$"); 74 75 #include <sys/param.h> 76 #include <sys/time.h> 77 #include <sys/resource.h> 78 #include <sys/wait.h> 79 80 #ifdef USE_BSM_AUDIT 81 #include <bsm/libbsm.h> 82 #include <bsm/audit_uevents.h> 83 #endif 84 85 #include <err.h> 86 #include <errno.h> 87 #include <grp.h> 88 #include <login_cap.h> 89 #include <paths.h> 90 #include <pwd.h> 91 #include <signal.h> 92 #include <stdio.h> 93 #include <stdlib.h> 94 #include <string.h> 95 #include <syslog.h> 96 #include <unistd.h> 97 #include <stdarg.h> 98 99 #include <security/pam_appl.h> 100 #include <security/openpam.h> 101 102 #define PAM_END() do { \ 103 int local_ret; \ 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)); \ 109 if (asthem) { \ 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)); \ 114 } \ 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)); \ 119 } \ 120 } while (0) 121 122 123 #define PAM_SET_ITEM(what, item) do { \ 124 int local_ret; \ 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)); \ 131 /* NOTREACHED */ \ 132 } \ 133 } while (0) 134 135 enum tristate { UNSET, YES, NO }; 136 137 static pam_handle_t *pamh = NULL; 138 static char **environ_pam; 139 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 *); 145 146 extern char **environ; 147 148 int 149 main(int argc, char *argv[]) 150 { 151 static char *cleanenv; 152 struct passwd *pwd; 153 struct pam_conv conv = { openpam_ttyconv, NULL }; 154 enum tristate iscsh; 155 login_cap_t *lc; 156 union { 157 const char **a; 158 char * const *b; 159 } np; 160 uid_t ruid; 161 pid_t child_pid, child_pgrp, pid; 162 int asme, ch, asthem, fastlogin, prio, i, retcode, 163 statusp, setmaclabel; 164 u_int setwhat; 165 char *username, *class, shellbuf[MAXPATHLEN]; 166 const char *p, *user, *shell, *mytty, **nargv; 167 const void *v; 168 struct sigaction sa, sa_int, sa_quit, sa_pipe; 169 int temp, fds[2]; 170 #ifdef USE_BSM_AUDIT 171 const char *aerr; 172 au_id_t auid; 173 #endif 174 175 shell = class = cleanenv = NULL; 176 asme = asthem = fastlogin = statusp = 0; 177 user = "root"; 178 iscsh = UNSET; 179 setmaclabel = 0; 180 181 while ((ch = getopt(argc, argv, "-flmsc:")) != -1) 182 switch ((char)ch) { 183 case 'f': 184 fastlogin = 1; 185 break; 186 case '-': 187 case 'l': 188 asme = 0; 189 asthem = 1; 190 break; 191 case 'm': 192 asme = 1; 193 asthem = 0; 194 break; 195 case 's': 196 setmaclabel = 1; 197 break; 198 case 'c': 199 class = optarg; 200 break; 201 case '?': 202 default: 203 usage(); 204 /* NOTREACHED */ 205 } 206 207 if (optind < argc) 208 user = argv[optind++]; 209 210 if (user == NULL) 211 usage(); 212 /* NOTREACHED */ 213 214 /* 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. 217 */ 218 if (geteuid() != 0) 219 errx(1, "not running setuid"); 220 221 #ifdef USE_BSM_AUDIT 222 if (getauid(&auid) < 0 && errno != ENOSYS) { 223 syslog(LOG_AUTH | LOG_ERR, "getauid: %s", strerror(errno)); 224 errx(1, "Permission denied"); 225 } 226 #endif 227 if (strlen(user) > MAXLOGNAME - 1) { 228 #ifdef USE_BSM_AUDIT 229 if (audit_submit(AUE_su, auid, 230 EPERM, 1, "username too long: '%s'", user)) 231 errx(1, "Permission denied"); 232 #endif 233 errx(1, "username too long"); 234 } 235 236 nargv = malloc(sizeof(char *) * (size_t)(argc + 4)); 237 if (nargv == NULL) 238 errx(1, "malloc failure"); 239 240 nargv[argc + 3] = NULL; 241 for (i = argc; i >= optind; i--) 242 nargv[i + 3] = argv[i]; 243 np.a = &nargv[i + 3]; 244 245 argv += optind; 246 247 errno = 0; 248 prio = getpriority(PRIO_PROCESS, 0); 249 if (errno) 250 prio = 0; 251 252 setpriority(PRIO_PROCESS, 0, -2); 253 openlog("su", LOG_CONS, LOG_AUTH); 254 255 /* get current login name, real uid and shell */ 256 ruid = getuid(); 257 username = getlogin(); 258 pwd = getpwnam(username); 259 if (username == NULL || pwd == NULL || pwd->pw_uid != ruid) 260 pwd = getpwuid(ruid); 261 if (pwd == NULL) { 262 #ifdef USE_BSM_AUDIT 263 if (audit_submit(AUE_su, auid, EPERM, 1, 264 "unable to determine invoking subject: '%s'", username)) 265 errx(1, "Permission denied"); 266 #endif 267 errx(1, "who are you?"); 268 } 269 270 username = strdup(pwd->pw_name); 271 if (username == NULL) 272 err(1, "strdup failure"); 273 274 if (asme) { 275 if (pwd->pw_shell != NULL && *pwd->pw_shell != '\0') { 276 /* must copy - pwd memory is recycled */ 277 shell = strncpy(shellbuf, pwd->pw_shell, 278 sizeof(shellbuf)); 279 shellbuf[sizeof(shellbuf) - 1] = '\0'; 280 } 281 else { 282 shell = _PATH_BSHELL; 283 iscsh = NO; 284 } 285 } 286 287 /* Do the whole PAM startup thing */ 288 retcode = pam_start("su", user, &conv, &pamh); 289 if (retcode != PAM_SUCCESS) { 290 syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, retcode)); 291 errx(1, "pam_start: %s", pam_strerror(pamh, retcode)); 292 } 293 294 PAM_SET_ITEM(PAM_RUSER, username); 295 296 mytty = ttyname(STDERR_FILENO); 297 if (!mytty) 298 mytty = "tty"; 299 PAM_SET_ITEM(PAM_TTY, mytty); 300 301 retcode = pam_authenticate(pamh, 0); 302 if (retcode != PAM_SUCCESS) { 303 #ifdef USE_BSM_AUDIT 304 if (audit_submit(AUE_su, auid, EPERM, 1, "bad su %s to %s on %s", 305 username, user, mytty)) 306 errx(1, "Permission denied"); 307 #endif 308 syslog(LOG_AUTH|LOG_WARNING, "BAD SU %s to %s on %s", 309 username, user, mytty); 310 errx(1, "Sorry"); 311 } 312 #ifdef USE_BSM_AUDIT 313 if (audit_submit(AUE_su, auid, 0, 0, "successful authentication")) 314 errx(1, "Permission denied"); 315 #endif 316 retcode = pam_get_item(pamh, PAM_USER, &v); 317 if (retcode == PAM_SUCCESS) 318 user = v; 319 else 320 syslog(LOG_ERR, "pam_get_item(PAM_USER): %s", 321 pam_strerror(pamh, retcode)); 322 pwd = getpwnam(user); 323 if (pwd == NULL) { 324 #ifdef USE_BSM_AUDIT 325 if (audit_submit(AUE_su, auid, EPERM, 1, 326 "unknown subject: %s", user)) 327 errx(1, "Permission denied"); 328 #endif 329 errx(1, "unknown login: %s", user); 330 } 331 332 retcode = pam_acct_mgmt(pamh, 0); 333 if (retcode == PAM_NEW_AUTHTOK_REQD) { 334 retcode = pam_chauthtok(pamh, 335 PAM_CHANGE_EXPIRED_AUTHTOK); 336 if (retcode != PAM_SUCCESS) { 337 #ifdef USE_BSM_AUDIT 338 aerr = pam_strerror(pamh, retcode); 339 if (aerr == NULL) 340 aerr = "Unknown PAM error"; 341 if (audit_submit(AUE_su, auid, EPERM, 1, 342 "pam_chauthtok: %s", aerr)) 343 errx(1, "Permission denied"); 344 #endif 345 syslog(LOG_ERR, "pam_chauthtok: %s", 346 pam_strerror(pamh, retcode)); 347 errx(1, "Sorry"); 348 } 349 } 350 if (retcode != PAM_SUCCESS) { 351 #ifdef USE_BSM_AUDIT 352 if (audit_submit(AUE_su, auid, EPERM, 1, "pam_acct_mgmt: %s", 353 pam_strerror(pamh, retcode))) 354 errx(1, "Permission denied"); 355 #endif 356 syslog(LOG_ERR, "pam_acct_mgmt: %s", 357 pam_strerror(pamh, retcode)); 358 errx(1, "Sorry"); 359 } 360 361 /* get target login information */ 362 if (class == NULL) 363 lc = login_getpwclass(pwd); 364 else { 365 if (ruid != 0) { 366 #ifdef USE_BSM_AUDIT 367 if (audit_submit(AUE_su, auid, EPERM, 1, 368 "only root may use -c")) 369 errx(1, "Permission denied"); 370 #endif 371 errx(1, "only root may use -c"); 372 } 373 lc = login_getclass(class); 374 if (lc == NULL) 375 errx(1, "unknown class: %s", class); 376 } 377 378 /* if asme and non-standard target shell, must be root */ 379 if (asme) { 380 if (ruid != 0 && !chshell(pwd->pw_shell)) 381 errx(1, "permission denied (shell)"); 382 } 383 else if (pwd->pw_shell && *pwd->pw_shell) { 384 shell = pwd->pw_shell; 385 iscsh = UNSET; 386 } 387 else { 388 shell = _PATH_BSHELL; 389 iscsh = NO; 390 } 391 392 /* if we're forking a csh, we want to slightly muck the args */ 393 if (iscsh == UNSET) { 394 p = strrchr(shell, '/'); 395 if (p) 396 ++p; 397 else 398 p = shell; 399 iscsh = strcmp(p, "csh") ? (strcmp(p, "tcsh") ? NO : YES) : YES; 400 } 401 setpriority(PRIO_PROCESS, 0, prio); 402 403 /* 404 * PAM modules might add supplementary groups in pam_setcred(), so 405 * initialize them first. 406 */ 407 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) < 0) 408 err(1, "setusercontext"); 409 410 retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED); 411 if (retcode != PAM_SUCCESS) { 412 syslog(LOG_ERR, "pam_setcred: %s", 413 pam_strerror(pamh, retcode)); 414 errx(1, "failed to establish credentials."); 415 } 416 if (asthem) { 417 retcode = pam_open_session(pamh, 0); 418 if (retcode != PAM_SUCCESS) { 419 syslog(LOG_ERR, "pam_open_session: %s", 420 pam_strerror(pamh, retcode)); 421 errx(1, "failed to open session."); 422 } 423 } 424 425 /* 426 * We must fork() before setuid() because we need to call 427 * pam_setcred(pamh, PAM_DELETE_CRED) as root. 428 */ 429 sa.sa_flags = SA_RESTART; 430 sa.sa_handler = SIG_IGN; 431 sigemptyset(&sa.sa_mask); 432 sigaction(SIGINT, &sa, &sa_int); 433 sigaction(SIGQUIT, &sa, &sa_quit); 434 sigaction(SIGPIPE, &sa, &sa_pipe); 435 sa.sa_handler = SIG_DFL; 436 sigaction(SIGTSTP, &sa, NULL); 437 statusp = 1; 438 if (pipe(fds) == -1) { 439 PAM_END(); 440 err(1, "pipe"); 441 } 442 child_pid = fork(); 443 switch (child_pid) { 444 default: 445 sa.sa_handler = SIG_IGN; 446 sigaction(SIGTTOU, &sa, NULL); 447 close(fds[0]); 448 setpgid(child_pid, child_pid); 449 if (tcgetpgrp(STDERR_FILENO) == getpgrp()) 450 tcsetpgrp(STDERR_FILENO, child_pid); 451 close(fds[1]); 452 sigaction(SIGPIPE, &sa_pipe, NULL); 453 while ((pid = waitpid(child_pid, &statusp, WUNTRACED)) != -1) { 454 if (WIFSTOPPED(statusp)) { 455 child_pgrp = getpgid(child_pid); 456 if (tcgetpgrp(STDERR_FILENO) == child_pgrp) 457 tcsetpgrp(STDERR_FILENO, getpgrp()); 458 kill(getpid(), SIGSTOP); 459 if (tcgetpgrp(STDERR_FILENO) == getpgrp()) { 460 child_pgrp = getpgid(child_pid); 461 tcsetpgrp(STDERR_FILENO, child_pgrp); 462 } 463 kill(child_pid, SIGCONT); 464 statusp = 1; 465 continue; 466 } 467 break; 468 } 469 tcsetpgrp(STDERR_FILENO, getpgrp()); 470 if (pid == -1) 471 err(1, "waitpid"); 472 PAM_END(); 473 exit(WEXITSTATUS(statusp)); 474 case -1: 475 PAM_END(); 476 err(1, "fork"); 477 case 0: 478 close(fds[1]); 479 read(fds[0], &temp, 1); 480 close(fds[0]); 481 sigaction(SIGPIPE, &sa_pipe, NULL); 482 sigaction(SIGINT, &sa_int, NULL); 483 sigaction(SIGQUIT, &sa_quit, NULL); 484 485 /* 486 * Set all user context except for: Environmental variables 487 * Umask Login records (wtmp, etc) Path 488 */ 489 setwhat = LOGIN_SETALL & ~(LOGIN_SETENV | LOGIN_SETUMASK | 490 LOGIN_SETLOGIN | LOGIN_SETPATH | LOGIN_SETGROUP | 491 LOGIN_SETMAC); 492 /* 493 * If -s is present, also set the MAC label. 494 */ 495 if (setmaclabel) 496 setwhat |= LOGIN_SETMAC; 497 /* 498 * Don't touch resource/priority settings if -m has been used 499 * or -l and -c hasn't, and we're not su'ing to root. 500 */ 501 if ((asme || (!asthem && class == NULL)) && pwd->pw_uid) 502 setwhat &= ~(LOGIN_SETPRIORITY | LOGIN_SETRESOURCES); 503 if (setusercontext(lc, pwd, pwd->pw_uid, setwhat) < 0) 504 err(1, "setusercontext"); 505 506 if (!asme) { 507 if (asthem) { 508 p = getenv("TERM"); 509 environ = &cleanenv; 510 } 511 512 if (asthem || pwd->pw_uid) 513 setenv("USER", pwd->pw_name, 1); 514 setenv("HOME", pwd->pw_dir, 1); 515 setenv("SHELL", shell, 1); 516 517 if (asthem) { 518 /* 519 * Add any environmental variables that the 520 * PAM modules may have set. 521 */ 522 environ_pam = pam_getenvlist(pamh); 523 if (environ_pam) 524 export_pam_environment(); 525 526 /* set the su'd user's environment & umask */ 527 setusercontext(lc, pwd, pwd->pw_uid, 528 LOGIN_SETPATH | LOGIN_SETUMASK | 529 LOGIN_SETENV); 530 if (p) 531 setenv("TERM", p, 1); 532 533 p = pam_getenv(pamh, "HOME"); 534 if (chdir(p ? p : pwd->pw_dir) < 0) 535 errx(1, "no directory"); 536 } 537 } 538 login_close(lc); 539 540 if (iscsh == YES) { 541 if (fastlogin) 542 *np.a-- = "-f"; 543 if (asme) 544 *np.a-- = "-m"; 545 } 546 /* csh strips the first character... */ 547 *np.a = asthem ? "-su" : iscsh == YES ? "_su" : "su"; 548 549 if (ruid != 0) 550 syslog(LOG_NOTICE, "%s to %s%s", username, user, 551 ontty()); 552 553 execv(shell, np.b); 554 err(1, "%s", shell); 555 } 556 } 557 558 static void 559 export_pam_environment(void) 560 { 561 char **pp; 562 char *p; 563 564 for (pp = environ_pam; *pp != NULL; pp++) { 565 if (ok_to_export(*pp)) { 566 p = strchr(*pp, '='); 567 *p = '\0'; 568 setenv(*pp, p + 1, 1); 569 } 570 free(*pp); 571 } 572 } 573 574 /* 575 * Sanity checks on PAM environmental variables: 576 * - Make sure there is an '=' in the string. 577 * - Make sure the string doesn't run on too long. 578 * - Do not export certain variables. This list was taken from the 579 * Solaris pam_putenv(3) man page. 580 * Note that if the user is chrooted, PAM may have a better idea than we 581 * do of where her home directory is. 582 */ 583 static int 584 ok_to_export(const char *s) 585 { 586 static const char *noexport[] = { 587 "SHELL", /* "HOME", */ "LOGNAME", "MAIL", "CDPATH", 588 "IFS", "PATH", NULL 589 }; 590 const char **pp; 591 size_t n; 592 593 if (strlen(s) > 1024 || strchr(s, '=') == NULL) 594 return 0; 595 if (strncmp(s, "LD_", 3) == 0) 596 return 0; 597 for (pp = noexport; *pp != NULL; pp++) { 598 n = strlen(*pp); 599 if (s[n] == '=' && strncmp(s, *pp, n) == 0) 600 return 0; 601 } 602 return 1; 603 } 604 605 static void 606 usage(void) 607 { 608 609 fprintf(stderr, "usage: su [-] [-flms] [-c class] [login [args]]\n"); 610 exit(1); 611 /* NOTREACHED */ 612 } 613 614 static int 615 chshell(const char *sh) 616 { 617 int r; 618 char *cp; 619 620 r = 0; 621 setusershell(); 622 while ((cp = getusershell()) != NULL && !r) 623 r = (strcmp(cp, sh) == 0); 624 endusershell(); 625 return r; 626 } 627 628 static char * 629 ontty(void) 630 { 631 char *p; 632 static char buf[MAXPATHLEN + 4]; 633 634 buf[0] = 0; 635 p = ttyname(STDERR_FILENO); 636 if (p) 637 snprintf(buf, sizeof(buf), " on %s", p); 638 return buf; 639 } 640