1 /* 2 * Copyright (c) 1988, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 2002 Networks Associates Technologies, Inc. 5 * All rights reserved. 6 * 7 * Portions of this software were developed for the FreeBSD Project by 8 * ThinkSec AS and NAI Labs, the Security Research Division of Network 9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 10 * ("CBOSS"), as part of the DARPA CHATS research program. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 */ 40 41 #ifndef lint 42 static const char copyright[] = 43 "@(#) Copyright (c) 1988, 1993, 1994\n\ 44 The Regents of the University of California. All rights reserved.\n"; 45 #endif /* not lint */ 46 47 #ifndef lint 48 #if 0 49 static char sccsid[] = "@(#)su.c 8.3 (Berkeley) 4/2/94"; 50 #endif 51 static const char rcsid[] = 52 "$FreeBSD$"; 53 #endif /* not lint */ 54 55 #include <sys/param.h> 56 #include <sys/time.h> 57 #include <sys/resource.h> 58 #include <sys/wait.h> 59 60 #include <err.h> 61 #include <errno.h> 62 #include <grp.h> 63 #include <libutil.h> 64 #include <login_cap.h> 65 #include <paths.h> 66 #include <pwd.h> 67 #include <signal.h> 68 #include <stdio.h> 69 #include <stdlib.h> 70 #include <string.h> 71 #include <syslog.h> 72 #include <unistd.h> 73 74 #include <security/pam_appl.h> 75 #include <security/openpam.h> 76 77 #define PAM_END() do { \ 78 int local_ret; \ 79 if (pamh != NULL && creds_set) { \ 80 local_ret = pam_setcred(pamh, PAM_DELETE_CRED); \ 81 if (local_ret != PAM_SUCCESS) \ 82 syslog(LOG_ERR, "pam_setcred: %s", \ 83 pam_strerror(pamh, local_ret)); \ 84 local_ret = pam_end(pamh, local_ret); \ 85 if (local_ret != PAM_SUCCESS) \ 86 syslog(LOG_ERR, "pam_end: %s", \ 87 pam_strerror(pamh, local_ret)); \ 88 } \ 89 } while (0) 90 91 92 #define PAM_SET_ITEM(what, item) do { \ 93 int local_ret; \ 94 local_ret = pam_set_item(pamh, what, item); \ 95 if (local_ret != PAM_SUCCESS) { \ 96 syslog(LOG_ERR, "pam_set_item(" #what "): %s", \ 97 pam_strerror(pamh, local_ret)); \ 98 errx(1, "pam_set_item(" #what "): %s", \ 99 pam_strerror(pamh, local_ret)); \ 100 } \ 101 } while (0) 102 103 enum tristate { UNSET, YES, NO }; 104 105 static pam_handle_t *pamh = NULL; 106 static int creds_set = 0; 107 static char **environ_pam; 108 109 static char *ontty(void); 110 static int chshell(char *); 111 static void usage(void); 112 static int export_pam_environment(void); 113 static int ok_to_export(const char *); 114 115 extern char **environ; 116 117 int 118 main(int argc, char *argv[]) 119 { 120 struct passwd *pwd; 121 struct pam_conv conv = { openpam_ttyconv, NULL }; 122 enum tristate iscsh; 123 login_cap_t *lc; 124 union { 125 const char **a; 126 char * const *b; 127 } np; 128 uid_t ruid; 129 int asme, ch, asthem, fastlogin, prio, i, setwhat, retcode, 130 statusp, child_pid, child_pgrp, ret_pid, setmaclabel; 131 char *username, *cleanenv, *class, shellbuf[MAXPATHLEN]; 132 const char *p, *user, *shell, *mytty, **nargv; 133 struct sigaction sa, sa_int, sa_quit; 134 135 shell = class = cleanenv = NULL; 136 asme = asthem = fastlogin = statusp = 0; 137 user = "root"; 138 iscsh = UNSET; 139 setmaclabel = 0; 140 141 while ((ch = getopt(argc, argv, "-flmsc:")) != -1) 142 switch ((char)ch) { 143 case 'f': 144 fastlogin = 1; 145 break; 146 case '-': 147 case 'l': 148 asme = 0; 149 asthem = 1; 150 break; 151 case 'm': 152 asme = 1; 153 asthem = 0; 154 break; 155 case 's': 156 setmaclabel = 1; 157 break; 158 case 'c': 159 class = optarg; 160 break; 161 case '?': 162 default: 163 usage(); 164 } 165 166 if (optind < argc) 167 user = argv[optind++]; 168 169 if (user == NULL) 170 usage(); 171 172 if (strlen(user) > MAXLOGNAME - 1) 173 errx(1, "username too long"); 174 175 nargv = malloc(sizeof(char *) * (argc + 4)); 176 if (nargv == NULL) 177 errx(1, "malloc failure"); 178 179 nargv[argc + 3] = NULL; 180 for (i = argc; i >= optind; i--) 181 nargv[i + 3] = argv[i]; 182 np.a = &nargv[i + 3]; 183 184 argv += optind; 185 186 errno = 0; 187 prio = getpriority(PRIO_PROCESS, 0); 188 if (errno) 189 prio = 0; 190 191 setpriority(PRIO_PROCESS, 0, -2); 192 openlog("su", LOG_CONS, LOG_AUTH); 193 194 /* get current login name, real uid and shell */ 195 ruid = getuid(); 196 username = getlogin(); 197 pwd = getpwnam(username); 198 if (username == NULL || pwd == NULL || pwd->pw_uid != ruid) 199 pwd = getpwuid(ruid); 200 if (pwd == NULL) 201 errx(1, "who are you?"); 202 203 username = strdup(pwd->pw_name); 204 if (username == NULL) 205 err(1, "strdup failure"); 206 207 if (asme) { 208 if (pwd->pw_shell != NULL && *pwd->pw_shell != '\0') { 209 /* must copy - pwd memory is recycled */ 210 shell = strncpy(shellbuf, pwd->pw_shell, 211 sizeof(shellbuf)); 212 shellbuf[sizeof(shellbuf) - 1] = '\0'; 213 } 214 else { 215 shell = _PATH_BSHELL; 216 iscsh = NO; 217 } 218 } 219 220 /* Do the whole PAM startup thing */ 221 retcode = pam_start("su", user, &conv, &pamh); 222 if (retcode != PAM_SUCCESS) { 223 syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, retcode)); 224 errx(1, "pam_start: %s", pam_strerror(pamh, retcode)); 225 } 226 227 PAM_SET_ITEM(PAM_RUSER, username); 228 229 mytty = ttyname(STDERR_FILENO); 230 if (!mytty) 231 mytty = "tty"; 232 PAM_SET_ITEM(PAM_TTY, mytty); 233 234 retcode = pam_authenticate(pamh, 0); 235 if (retcode != PAM_SUCCESS) { 236 #if 0 237 syslog(LOG_ERR, "pam_authenticate: %s", 238 pam_strerror(pamh, retcode)); 239 #endif 240 syslog(LOG_AUTH|LOG_WARNING, "BAD SU %s to %s on %s", 241 username, user, mytty); 242 errx(1, "Sorry"); 243 } 244 retcode = pam_get_item(pamh, PAM_USER, (const void **)&p); 245 if (retcode == PAM_SUCCESS) 246 user = p; 247 else 248 syslog(LOG_ERR, "pam_get_item(PAM_USER): %s", 249 pam_strerror(pamh, retcode)); 250 251 retcode = pam_acct_mgmt(pamh, 0); 252 if (retcode == PAM_NEW_AUTHTOK_REQD) { 253 retcode = pam_chauthtok(pamh, 254 PAM_CHANGE_EXPIRED_AUTHTOK); 255 if (retcode != PAM_SUCCESS) { 256 syslog(LOG_ERR, "pam_chauthtok: %s", 257 pam_strerror(pamh, retcode)); 258 errx(1, "Sorry"); 259 } 260 } 261 if (retcode != PAM_SUCCESS) { 262 syslog(LOG_ERR, "pam_acct_mgmt: %s", 263 pam_strerror(pamh, retcode)); 264 errx(1, "Sorry"); 265 } 266 267 /* get target login information, default to root */ 268 pwd = getpwnam(user); 269 if (pwd == NULL) 270 errx(1, "unknown login: %s", user); 271 if (class == NULL) 272 lc = login_getpwclass(pwd); 273 else { 274 if (ruid != 0) 275 errx(1, "only root may use -c"); 276 lc = login_getclass(class); 277 if (lc == NULL) 278 errx(1, "unknown class: %s", class); 279 } 280 281 /* if asme and non-standard target shell, must be root */ 282 if (asme) { 283 if (ruid != 0 && !chshell(pwd->pw_shell)) 284 errx(1, "permission denied (shell)."); 285 } 286 else if (pwd->pw_shell && *pwd->pw_shell) { 287 shell = pwd->pw_shell; 288 iscsh = UNSET; 289 } 290 else { 291 shell = _PATH_BSHELL; 292 iscsh = NO; 293 } 294 295 /* if we're forking a csh, we want to slightly muck the args */ 296 if (iscsh == UNSET) { 297 p = strrchr(shell, '/'); 298 if (p) 299 ++p; 300 else 301 p = shell; 302 iscsh = strcmp(p, "csh") ? (strcmp(p, "tcsh") ? NO : YES) : YES; 303 } 304 setpriority(PRIO_PROCESS, 0, prio); 305 306 /* 307 * PAM modules might add supplementary groups in pam_setcred(), so 308 * initialize them first. 309 */ 310 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) < 0) 311 err(1, "setusercontext"); 312 313 retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED); 314 if (retcode != PAM_SUCCESS) 315 syslog(LOG_ERR, "pam_setcred(pamh, PAM_ESTABLISH_CRED): %s", 316 pam_strerror(pamh, retcode)); 317 else 318 creds_set = 1; 319 320 /* 321 * We must fork() before setuid() because we need to call 322 * pam_setcred(pamh, PAM_DELETE_CRED) as root. 323 */ 324 sa.sa_flags = SA_RESTART; 325 sa.sa_handler = SIG_IGN; 326 sigemptyset(&sa.sa_mask); 327 sigaction(SIGINT, &sa, &sa_int); 328 sigaction(SIGQUIT, &sa, &sa_quit); 329 sa.sa_handler = SIG_DFL; 330 sigaction(SIGTSTP, &sa, NULL); 331 statusp = 1; 332 child_pid = fork(); 333 switch (child_pid) { 334 default: 335 while ((ret_pid = waitpid(child_pid, &statusp, WUNTRACED)) != -1) { 336 if (WIFSTOPPED(statusp)) { 337 kill(getpid(), SIGSTOP); 338 child_pgrp = getpgid(child_pid); 339 if (tcgetpgrp(1) == getpgrp()) { 340 tcsetpgrp(1, child_pgrp); 341 kill(child_pid, SIGCONT); 342 } 343 statusp = 1; 344 continue; 345 } 346 break; 347 } 348 if (ret_pid == -1) 349 err(1, "waitpid"); 350 PAM_END(); 351 exit(statusp); 352 case -1: 353 err(1, "fork"); 354 PAM_END(); 355 exit(1); 356 case 0: 357 sigaction(SIGINT, &sa_int, NULL); 358 sigaction(SIGQUIT, &sa_quit, NULL); 359 /* 360 * Set all user context except for: Environmental variables 361 * Umask Login records (wtmp, etc) Path 362 */ 363 setwhat = LOGIN_SETALL & ~(LOGIN_SETENV | LOGIN_SETUMASK | 364 LOGIN_SETLOGIN | LOGIN_SETPATH | LOGIN_SETGROUP | 365 LOGIN_SETMAC); 366 /* 367 * If -s is present, also set the MAC label. 368 */ 369 if (setmaclabel) 370 setwhat |= LOGIN_SETMAC; 371 /* 372 * Don't touch resource/priority settings if -m has been used 373 * or -l and -c hasn't, and we're not su'ing to root. 374 */ 375 if ((asme || (!asthem && class == NULL)) && pwd->pw_uid) 376 setwhat &= ~(LOGIN_SETPRIORITY | LOGIN_SETRESOURCES); 377 if (setusercontext(lc, pwd, pwd->pw_uid, setwhat) < 0) 378 err(1, "setusercontext"); 379 380 if (!asme) { 381 if (asthem) { 382 p = getenv("TERM"); 383 environ = &cleanenv; 384 385 /* 386 * Add any environmental variables that the 387 * PAM modules may have set. 388 */ 389 environ_pam = pam_getenvlist(pamh); 390 if (environ_pam) 391 export_pam_environment(); 392 393 /* set the su'd user's environment & umask */ 394 setusercontext(lc, pwd, pwd->pw_uid, 395 LOGIN_SETPATH | LOGIN_SETUMASK | 396 LOGIN_SETENV); 397 if (p) 398 setenv("TERM", p, 1); 399 if (chdir(pwd->pw_dir) < 0) 400 errx(1, "no directory"); 401 } 402 if (asthem || pwd->pw_uid) 403 setenv("USER", pwd->pw_name, 1); 404 setenv("HOME", pwd->pw_dir, 1); 405 setenv("SHELL", shell, 1); 406 } 407 login_close(lc); 408 409 if (iscsh == YES) { 410 if (fastlogin) 411 *np.a-- = "-f"; 412 if (asme) 413 *np.a-- = "-m"; 414 } 415 /* csh strips the first character... */ 416 *np.a = asthem ? "-su" : iscsh == YES ? "_su" : "su"; 417 418 if (ruid != 0) 419 syslog(LOG_NOTICE, "%s to %s%s", username, user, 420 ontty()); 421 422 execv(shell, np.b); 423 err(1, "%s", shell); 424 } 425 } 426 427 static int 428 export_pam_environment(void) 429 { 430 char **pp; 431 432 for (pp = environ_pam; *pp != NULL; pp++) { 433 if (ok_to_export(*pp)) 434 putenv(*pp); 435 free(*pp); 436 } 437 return PAM_SUCCESS; 438 } 439 440 /* 441 * Sanity checks on PAM environmental variables: 442 * - Make sure there is an '=' in the string. 443 * - Make sure the string doesn't run on too long. 444 * - Do not export certain variables. This list was taken from the 445 * Solaris pam_putenv(3) man page. 446 */ 447 static int 448 ok_to_export(const char *s) 449 { 450 static const char *noexport[] = { 451 "SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH", 452 "IFS", "PATH", NULL 453 }; 454 const char **pp; 455 size_t n; 456 457 if (strlen(s) > 1024 || strchr(s, '=') == NULL) 458 return 0; 459 if (strncmp(s, "LD_", 3) == 0) 460 return 0; 461 for (pp = noexport; *pp != NULL; pp++) { 462 n = strlen(*pp); 463 if (s[n] == '=' && strncmp(s, *pp, n) == 0) 464 return 0; 465 } 466 return 1; 467 } 468 469 static void 470 usage(void) 471 { 472 473 fprintf(stderr, "usage: su [-] [-flms] [-c class] [login [args]]\n"); 474 exit(1); 475 } 476 477 static int 478 chshell(char *sh) 479 { 480 int r; 481 char *cp; 482 483 r = 0; 484 setusershell(); 485 do { 486 cp = getusershell(); 487 r = strcmp(cp, sh); 488 } while (!r && cp != NULL); 489 endusershell(); 490 return r; 491 } 492 493 static char * 494 ontty(void) 495 { 496 char *p; 497 static char buf[MAXPATHLEN + 4]; 498 499 buf[0] = 0; 500 p = ttyname(STDERR_FILENO); 501 if (p) 502 snprintf(buf, sizeof(buf), " on %s", p); 503 return buf; 504 } 505