1 /*- 2 * Copyright (c) 1980, 1987, 1988, 1991, 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 #if 0 42 #ifndef lint 43 static char sccsid[] = "@(#)login.c 8.4 (Berkeley) 4/2/94"; 44 #endif 45 #endif 46 47 #include <sys/cdefs.h> 48 __FBSDID("$FreeBSD$"); 49 50 /* 51 * login [ name ] 52 * login -h hostname (for telnetd, etc.) 53 * login -f name (for pre-authenticated login: datakit, xterm, etc.) 54 */ 55 56 #include <sys/param.h> 57 #include <sys/file.h> 58 #include <sys/stat.h> 59 #include <sys/time.h> 60 #include <sys/resource.h> 61 #include <sys/wait.h> 62 63 #include <err.h> 64 #include <errno.h> 65 #include <grp.h> 66 #include <libutil.h> 67 #include <login_cap.h> 68 #include <pwd.h> 69 #include <setjmp.h> 70 #include <signal.h> 71 #include <stdio.h> 72 #include <stdlib.h> 73 #include <string.h> 74 #include <syslog.h> 75 #include <ttyent.h> 76 #include <unistd.h> 77 78 #include <security/pam_appl.h> 79 #include <security/openpam.h> 80 81 #include "login.h" 82 #include "pathnames.h" 83 84 static int auth_pam(void); 85 static void bail(int, int); 86 static int export(const char *); 87 static void export_pam_environment(void); 88 static int motd(const char *); 89 static void badlogin(char *); 90 static char *getloginname(void); 91 static void pam_syslog(const char *); 92 static void pam_cleanup(void); 93 static void refused(const char *, const char *, int); 94 static const char *stypeof(char *); 95 static void sigint(int); 96 static void timedout(int); 97 static void usage(void); 98 99 #define TTYGRPNAME "tty" /* group to own ttys */ 100 #define DEFAULT_BACKOFF 3 101 #define DEFAULT_RETRIES 10 102 #define DEFAULT_PROMPT "login: " 103 #define DEFAULT_PASSWD_PROMPT "Password:" 104 #define TERM_UNKNOWN "su" 105 #define DEFAULT_WARN (2L * 7L * 86400L) /* Two weeks */ 106 #define NO_SLEEP_EXIT 0 107 #define SLEEP_EXIT 5 108 109 /* 110 * This bounds the time given to login. Not a define so it can 111 * be patched on machines where it's too small. 112 */ 113 static u_int timeout = 300; 114 115 /* Buffer for signal handling of timeout */ 116 static jmp_buf timeout_buf; 117 118 struct passwd *pwd; 119 static int failures; 120 121 static char *envinit[1]; /* empty environment list */ 122 123 /* 124 * Command line flags and arguments 125 */ 126 static int fflag; /* -f: do not perform authentication */ 127 static int hflag; /* -h: login from remote host */ 128 static char *hostname; /* hostname from command line */ 129 static int pflag; /* -p: preserve environment */ 130 131 /* 132 * User name 133 */ 134 static char *username; /* user name */ 135 static char *olduser; /* previous user name */ 136 137 /* 138 * Prompts 139 */ 140 static char default_prompt[] = DEFAULT_PROMPT; 141 static const char *prompt; 142 static char default_passwd_prompt[] = DEFAULT_PASSWD_PROMPT; 143 static const char *passwd_prompt; 144 145 static char *tty; 146 147 /* 148 * PAM data 149 */ 150 static pam_handle_t *pamh = NULL; 151 static struct pam_conv pamc = { openpam_ttyconv, NULL }; 152 static int pam_err; 153 static int pam_silent = PAM_SILENT; 154 static int pam_cred_established; 155 static int pam_session_established; 156 157 int 158 main(int argc, char *argv[]) 159 { 160 struct group *gr; 161 struct stat st; 162 int retries, backoff; 163 int ask, ch, cnt, quietlog, rootlogin, rval; 164 uid_t uid, euid; 165 gid_t egid; 166 char *term; 167 char *p, *ttyn; 168 char tname[sizeof(_PATH_TTY) + 10]; 169 char *arg0; 170 const char *tp; 171 const char *shell = NULL; 172 login_cap_t *lc = NULL; 173 login_cap_t *lc_user = NULL; 174 pid_t pid; 175 #ifdef USE_BSM_AUDIT 176 char auditsuccess = 1; 177 #endif 178 179 (void)signal(SIGQUIT, SIG_IGN); 180 (void)signal(SIGINT, SIG_IGN); 181 (void)signal(SIGHUP, SIG_IGN); 182 if (setjmp(timeout_buf)) { 183 if (failures) 184 badlogin(username); 185 (void)fprintf(stderr, "Login timed out after %d seconds\n", 186 timeout); 187 bail(NO_SLEEP_EXIT, 0); 188 } 189 (void)signal(SIGALRM, timedout); 190 (void)alarm(timeout); 191 (void)setpriority(PRIO_PROCESS, 0, 0); 192 193 openlog("login", LOG_ODELAY, LOG_AUTH); 194 195 uid = getuid(); 196 euid = geteuid(); 197 egid = getegid(); 198 199 while ((ch = getopt(argc, argv, "fh:p")) != -1) 200 switch (ch) { 201 case 'f': 202 fflag = 1; 203 break; 204 case 'h': 205 if (uid != 0) 206 errx(1, "-h option: %s", strerror(EPERM)); 207 if (strlen(optarg) >= MAXHOSTNAMELEN) 208 errx(1, "-h option: %s: exceeds maximum " 209 "hostname size", optarg); 210 hflag = 1; 211 hostname = optarg; 212 break; 213 case 'p': 214 pflag = 1; 215 break; 216 case '?': 217 default: 218 if (uid == 0) 219 syslog(LOG_ERR, "invalid flag %c", ch); 220 usage(); 221 } 222 argc -= optind; 223 argv += optind; 224 225 if (argc > 0) { 226 username = strdup(*argv); 227 if (username == NULL) 228 err(1, "strdup()"); 229 ask = 0; 230 } else { 231 ask = 1; 232 } 233 234 setproctitle("-%s", getprogname()); 235 236 for (cnt = getdtablesize(); cnt > 2; cnt--) 237 (void)close(cnt); 238 239 /* 240 * Get current TTY 241 */ 242 ttyn = ttyname(STDIN_FILENO); 243 if (ttyn == NULL || *ttyn == '\0') { 244 (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY); 245 ttyn = tname; 246 } 247 if (strncmp(ttyn, _PATH_DEV, sizeof _PATH_DEV - 1) == 0) 248 tty = ttyn + sizeof _PATH_DEV - 1; 249 else 250 tty = ttyn; 251 252 /* 253 * Get "login-retries" & "login-backoff" from default class 254 */ 255 lc = login_getclass(NULL); 256 prompt = login_getcapstr(lc, "login_prompt", 257 default_prompt, default_prompt); 258 passwd_prompt = login_getcapstr(lc, "passwd_prompt", 259 default_passwd_prompt, default_passwd_prompt); 260 retries = login_getcapnum(lc, "login-retries", 261 DEFAULT_RETRIES, DEFAULT_RETRIES); 262 backoff = login_getcapnum(lc, "login-backoff", 263 DEFAULT_BACKOFF, DEFAULT_BACKOFF); 264 login_close(lc); 265 lc = NULL; 266 267 /* 268 * Try to authenticate the user until we succeed or time out. 269 */ 270 for (cnt = 0;; ask = 1) { 271 if (ask) { 272 fflag = 0; 273 if (olduser != NULL) 274 free(olduser); 275 olduser = username; 276 username = getloginname(); 277 } 278 rootlogin = 0; 279 280 /* 281 * Note if trying multiple user names; log failures for 282 * previous user name, but don't bother logging one failure 283 * for nonexistent name (mistyped username). 284 */ 285 if (failures && strcmp(olduser, username) != 0) { 286 if (failures > (pwd ? 0 : 1)) 287 badlogin(olduser); 288 } 289 290 /* 291 * Load the PAM policy and set some variables 292 */ 293 pam_err = pam_start("login", username, &pamc, &pamh); 294 if (pam_err != PAM_SUCCESS) { 295 pam_syslog("pam_start()"); 296 #ifdef USE_BSM_AUDIT 297 au_login_fail("PAM Error", 1); 298 #endif 299 bail(NO_SLEEP_EXIT, 1); 300 } 301 pam_err = pam_set_item(pamh, PAM_TTY, tty); 302 if (pam_err != PAM_SUCCESS) { 303 pam_syslog("pam_set_item(PAM_TTY)"); 304 #ifdef USE_BSM_AUDIT 305 au_login_fail("PAM Error", 1); 306 #endif 307 bail(NO_SLEEP_EXIT, 1); 308 } 309 pam_err = pam_set_item(pamh, PAM_RHOST, hostname); 310 if (pam_err != PAM_SUCCESS) { 311 pam_syslog("pam_set_item(PAM_RHOST)"); 312 #ifdef USE_BSM_AUDIT 313 au_login_fail("PAM Error", 1); 314 #endif 315 bail(NO_SLEEP_EXIT, 1); 316 } 317 318 pwd = getpwnam(username); 319 if (pwd != NULL && pwd->pw_uid == 0) 320 rootlogin = 1; 321 322 /* 323 * If the -f option was specified and the caller is 324 * root or the caller isn't changing their uid, don't 325 * authenticate. 326 */ 327 if (pwd != NULL && fflag && 328 (uid == (uid_t)0 || uid == (uid_t)pwd->pw_uid)) { 329 /* already authenticated */ 330 rval = 0; 331 #ifdef USE_BSM_AUDIT 332 auditsuccess = 0; /* opened a terminal window only */ 333 #endif 334 } else { 335 fflag = 0; 336 (void)setpriority(PRIO_PROCESS, 0, -4); 337 rval = auth_pam(); 338 (void)setpriority(PRIO_PROCESS, 0, 0); 339 } 340 341 if (pwd && rval == 0) 342 break; 343 344 pam_cleanup(); 345 346 /* 347 * We are not exiting here, but this corresponds to a failed 348 * login event, so set exitstatus to 1. 349 */ 350 #ifdef USE_BSM_AUDIT 351 au_login_fail("Login incorrect", 1); 352 #endif 353 354 (void)printf("Login incorrect\n"); 355 failures++; 356 357 pwd = NULL; 358 359 /* 360 * Allow up to 'retry' (10) attempts, but start 361 * backing off after 'backoff' (3) attempts. 362 */ 363 if (++cnt > backoff) { 364 if (cnt >= retries) { 365 badlogin(username); 366 bail(SLEEP_EXIT, 1); 367 } 368 sleep((u_int)((cnt - backoff) * 5)); 369 } 370 } 371 372 /* committed to login -- turn off timeout */ 373 (void)alarm((u_int)0); 374 (void)signal(SIGHUP, SIG_DFL); 375 376 endpwent(); 377 378 #ifdef USE_BSM_AUDIT 379 /* Audit successful login. */ 380 if (auditsuccess) 381 au_login_success(); 382 #endif 383 384 /* 385 * Establish the login class. 386 */ 387 lc = login_getpwclass(pwd); 388 lc_user = login_getuserclass(pwd); 389 390 if (!(quietlog = login_getcapbool(lc_user, "hushlogin", 0))) 391 quietlog = login_getcapbool(lc, "hushlogin", 0); 392 393 /* 394 * Switching needed for NFS with root access disabled. 395 * 396 * XXX: This change fails to modify the additional groups for the 397 * process, and as such, may restrict rights normally granted 398 * through those groups. 399 */ 400 (void)setegid(pwd->pw_gid); 401 (void)seteuid(rootlogin ? 0 : pwd->pw_uid); 402 if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) { 403 if (login_getcapbool(lc, "requirehome", 0)) 404 refused("Home directory not available", "HOMEDIR", 1); 405 if (chdir("/") < 0) 406 refused("Cannot find root directory", "ROOTDIR", 1); 407 if (!quietlog || *pwd->pw_dir) 408 printf("No home directory.\nLogging in with home = \"/\".\n"); 409 pwd->pw_dir = strdup("/"); 410 if (pwd->pw_dir == NULL) { 411 syslog(LOG_NOTICE, "strdup(): %m"); 412 bail(SLEEP_EXIT, 1); 413 } 414 } 415 (void)seteuid(euid); 416 (void)setegid(egid); 417 if (!quietlog) { 418 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0; 419 if (!quietlog) 420 pam_silent = 0; 421 } 422 423 shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell); 424 if (*pwd->pw_shell == '\0') 425 pwd->pw_shell = strdup(_PATH_BSHELL); 426 if (pwd->pw_shell == NULL) { 427 syslog(LOG_NOTICE, "strdup(): %m"); 428 bail(SLEEP_EXIT, 1); 429 } 430 if (*shell == '\0') /* Not overridden */ 431 shell = pwd->pw_shell; 432 if ((shell = strdup(shell)) == NULL) { 433 syslog(LOG_NOTICE, "strdup(): %m"); 434 bail(SLEEP_EXIT, 1); 435 } 436 437 /* 438 * Set device protections, depending on what terminal the 439 * user is logged in. This feature is used on Suns to give 440 * console users better privacy. 441 */ 442 login_fbtab(tty, pwd->pw_uid, pwd->pw_gid); 443 444 /* 445 * Clear flags of the tty. None should be set, and when the 446 * user sets them otherwise, this can cause the chown to fail. 447 * Since it isn't clear that flags are useful on character 448 * devices, we just clear them. 449 * 450 * We don't log in the case of EOPNOTSUPP because dev might be 451 * on NFS, which doesn't support chflags. 452 * 453 * We don't log in the EROFS because that means that /dev is on 454 * a read only file system and we assume that the permissions there 455 * are sane. 456 */ 457 if (ttyn != tname && chflags(ttyn, 0)) 458 if (errno != EOPNOTSUPP && errno != EROFS) 459 syslog(LOG_ERR, "chflags(%s): %m", ttyn); 460 if (ttyn != tname && chown(ttyn, pwd->pw_uid, 461 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid)) 462 if (errno != EROFS) 463 syslog(LOG_ERR, "chown(%s): %m", ttyn); 464 465 /* 466 * Exclude cons/vt/ptys only, assume dialup otherwise 467 * TODO: Make dialup tty determination a library call 468 * for consistency (finger etc.) 469 */ 470 if (hflag && isdialuptty(tty)) 471 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name); 472 473 #ifdef LOGALL 474 /* 475 * Syslog each successful login, so we don't have to watch 476 * hundreds of wtmp or lastlogin files. 477 */ 478 if (hflag) 479 syslog(LOG_INFO, "login from %s on %s as %s", 480 hostname, tty, pwd->pw_name); 481 else 482 syslog(LOG_INFO, "login on %s as %s", 483 tty, pwd->pw_name); 484 #endif 485 486 /* 487 * If fflag is on, assume caller/authenticator has logged root 488 * login. 489 */ 490 if (rootlogin && fflag == 0) { 491 if (hflag) 492 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s", 493 username, tty, hostname); 494 else 495 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", 496 username, tty); 497 } 498 499 /* 500 * Destroy environment unless user has requested its 501 * preservation - but preserve TERM in all cases 502 */ 503 term = getenv("TERM"); 504 if (!pflag) 505 environ = envinit; 506 if (term != NULL) 507 setenv("TERM", term, 0); 508 509 /* 510 * PAM modules might add supplementary groups during pam_setcred(). 511 */ 512 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) { 513 syslog(LOG_ERR, "setusercontext() failed - exiting"); 514 bail(NO_SLEEP_EXIT, 1); 515 } 516 517 pam_err = pam_setcred(pamh, pam_silent|PAM_ESTABLISH_CRED); 518 if (pam_err != PAM_SUCCESS) { 519 pam_syslog("pam_setcred()"); 520 bail(NO_SLEEP_EXIT, 1); 521 } 522 pam_cred_established = 1; 523 524 pam_err = pam_open_session(pamh, pam_silent); 525 if (pam_err != PAM_SUCCESS) { 526 pam_syslog("pam_open_session()"); 527 bail(NO_SLEEP_EXIT, 1); 528 } 529 pam_session_established = 1; 530 531 /* 532 * We must fork() before setuid() because we need to call 533 * pam_close_session() as root. 534 */ 535 pid = fork(); 536 if (pid < 0) { 537 err(1, "fork"); 538 } else if (pid != 0) { 539 /* 540 * Parent: wait for child to finish, then clean up 541 * session. 542 */ 543 int status; 544 setproctitle("-%s [pam]", getprogname()); 545 waitpid(pid, &status, 0); 546 bail(NO_SLEEP_EXIT, 0); 547 } 548 549 /* 550 * NOTICE: We are now in the child process! 551 */ 552 553 /* 554 * Add any environment variables the PAM modules may have set. 555 */ 556 export_pam_environment(); 557 558 /* 559 * We're done with PAM now; our parent will deal with the rest. 560 */ 561 pam_end(pamh, 0); 562 pamh = NULL; 563 564 /* 565 * We don't need to be root anymore, so set the login name and 566 * the UID. 567 */ 568 if (setlogin(username) != 0) { 569 syslog(LOG_ERR, "setlogin(%s): %m - exiting", username); 570 bail(NO_SLEEP_EXIT, 1); 571 } 572 if (setusercontext(lc, pwd, pwd->pw_uid, 573 LOGIN_SETALL & ~(LOGIN_SETLOGIN|LOGIN_SETGROUP)) != 0) { 574 syslog(LOG_ERR, "setusercontext() failed - exiting"); 575 exit(1); 576 } 577 578 (void)setenv("SHELL", pwd->pw_shell, 1); 579 (void)setenv("HOME", pwd->pw_dir, 1); 580 /* Overwrite "term" from login.conf(5) for any known TERM */ 581 if (term == NULL && (tp = stypeof(tty)) != NULL) 582 (void)setenv("TERM", tp, 1); 583 else 584 (void)setenv("TERM", TERM_UNKNOWN, 0); 585 (void)setenv("LOGNAME", username, 1); 586 (void)setenv("USER", username, 1); 587 (void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0); 588 589 if (!quietlog) { 590 const char *cw; 591 592 cw = login_getcapstr(lc, "welcome", NULL, NULL); 593 if (cw != NULL && access(cw, F_OK) == 0) 594 motd(cw); 595 else 596 motd(_PATH_MOTDFILE); 597 598 if (login_getcapbool(lc_user, "nocheckmail", 0) == 0 && 599 login_getcapbool(lc, "nocheckmail", 0) == 0) { 600 char *cx; 601 602 /* $MAIL may have been set by class. */ 603 cx = getenv("MAIL"); 604 if (cx == NULL) { 605 asprintf(&cx, "%s/%s", 606 _PATH_MAILDIR, pwd->pw_name); 607 } 608 if (cx && stat(cx, &st) == 0 && st.st_size != 0) 609 (void)printf("You have %smail.\n", 610 (st.st_mtime > st.st_atime) ? "new " : ""); 611 if (getenv("MAIL") == NULL) 612 free(cx); 613 } 614 } 615 616 login_close(lc_user); 617 login_close(lc); 618 619 (void)signal(SIGALRM, SIG_DFL); 620 (void)signal(SIGQUIT, SIG_DFL); 621 (void)signal(SIGINT, SIG_DFL); 622 (void)signal(SIGTSTP, SIG_IGN); 623 624 /* 625 * Login shells have a leading '-' in front of argv[0] 626 */ 627 p = strrchr(pwd->pw_shell, '/'); 628 if (asprintf(&arg0, "-%s", p ? p + 1 : pwd->pw_shell) >= MAXPATHLEN) { 629 syslog(LOG_ERR, "user: %s: shell exceeds maximum pathname size", 630 username); 631 errx(1, "shell exceeds maximum pathname size"); 632 } else if (arg0 == NULL) { 633 err(1, "asprintf()"); 634 } 635 636 execlp(shell, arg0, (char *)0); 637 err(1, "%s", shell); 638 639 /* 640 * That's it, folks! 641 */ 642 } 643 644 /* 645 * Attempt to authenticate the user using PAM. Returns 0 if the user is 646 * authenticated, or 1 if not authenticated. If some sort of PAM system 647 * error occurs (e.g., the "/etc/pam.conf" file is missing) then this 648 * function returns -1. This can be used as an indication that we should 649 * fall back to a different authentication mechanism. 650 */ 651 static int 652 auth_pam(void) 653 { 654 const char *tmpl_user; 655 const void *item; 656 int rval; 657 658 pam_err = pam_authenticate(pamh, pam_silent); 659 switch (pam_err) { 660 661 case PAM_SUCCESS: 662 /* 663 * With PAM we support the concept of a "template" 664 * user. The user enters a login name which is 665 * authenticated by PAM, usually via a remote service 666 * such as RADIUS or TACACS+. If authentication 667 * succeeds, a different but related "template" name 668 * is used for setting the credentials, shell, and 669 * home directory. The name the user enters need only 670 * exist on the remote authentication server, but the 671 * template name must be present in the local password 672 * database. 673 * 674 * This is supported by two various mechanisms in the 675 * individual modules. However, from the application's 676 * point of view, the template user is always passed 677 * back as a changed value of the PAM_USER item. 678 */ 679 pam_err = pam_get_item(pamh, PAM_USER, &item); 680 if (pam_err == PAM_SUCCESS) { 681 tmpl_user = (const char *)item; 682 if (strcmp(username, tmpl_user) != 0) 683 pwd = getpwnam(tmpl_user); 684 } else { 685 pam_syslog("pam_get_item(PAM_USER)"); 686 } 687 rval = 0; 688 break; 689 690 case PAM_AUTH_ERR: 691 case PAM_USER_UNKNOWN: 692 case PAM_MAXTRIES: 693 rval = 1; 694 break; 695 696 default: 697 pam_syslog("pam_authenticate()"); 698 rval = -1; 699 break; 700 } 701 702 if (rval == 0) { 703 pam_err = pam_acct_mgmt(pamh, pam_silent); 704 switch (pam_err) { 705 case PAM_SUCCESS: 706 break; 707 case PAM_NEW_AUTHTOK_REQD: 708 pam_err = pam_chauthtok(pamh, 709 pam_silent|PAM_CHANGE_EXPIRED_AUTHTOK); 710 if (pam_err != PAM_SUCCESS) { 711 pam_syslog("pam_chauthtok()"); 712 rval = 1; 713 } 714 break; 715 default: 716 pam_syslog("pam_acct_mgmt()"); 717 rval = 1; 718 break; 719 } 720 } 721 722 if (rval != 0) { 723 pam_end(pamh, pam_err); 724 pamh = NULL; 725 } 726 return (rval); 727 } 728 729 /* 730 * Export any environment variables PAM modules may have set 731 */ 732 static void 733 export_pam_environment(void) 734 { 735 char **pam_env; 736 char **pp; 737 738 pam_env = pam_getenvlist(pamh); 739 if (pam_env != NULL) { 740 for (pp = pam_env; *pp != NULL; pp++) { 741 (void)export(*pp); 742 free(*pp); 743 } 744 } 745 } 746 747 /* 748 * Perform sanity checks on an environment variable: 749 * - Make sure there is an '=' in the string. 750 * - Make sure the string doesn't run on too long. 751 * - Do not export certain variables. This list was taken from the 752 * Solaris pam_putenv(3) man page. 753 * Then export it. 754 */ 755 static int 756 export(const char *s) 757 { 758 static const char *noexport[] = { 759 "SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH", 760 "IFS", "PATH", NULL 761 }; 762 char *p; 763 const char **pp; 764 size_t n; 765 766 if (strlen(s) > 1024 || (p = strchr(s, '=')) == NULL) 767 return (0); 768 if (strncmp(s, "LD_", 3) == 0) 769 return (0); 770 for (pp = noexport; *pp != NULL; pp++) { 771 n = strlen(*pp); 772 if (s[n] == '=' && strncmp(s, *pp, n) == 0) 773 return (0); 774 } 775 *p = '\0'; 776 (void)setenv(s, p + 1, 1); 777 *p = '='; 778 return (1); 779 } 780 781 static void 782 usage(void) 783 { 784 785 (void)fprintf(stderr, "usage: login [-fp] [-h hostname] [username]\n"); 786 exit(1); 787 } 788 789 /* 790 * Prompt user and read login name from stdin. 791 */ 792 static char * 793 getloginname(void) 794 { 795 char *nbuf, *p; 796 int ch; 797 798 nbuf = malloc(MAXLOGNAME); 799 if (nbuf == NULL) 800 err(1, "malloc()"); 801 do { 802 (void)printf("%s", prompt); 803 for (p = nbuf; (ch = getchar()) != '\n'; ) { 804 if (ch == EOF) { 805 badlogin(username); 806 bail(NO_SLEEP_EXIT, 0); 807 } 808 if (p < nbuf + MAXLOGNAME - 1) 809 *p++ = ch; 810 } 811 } while (p == nbuf); 812 813 *p = '\0'; 814 if (nbuf[0] == '-') { 815 pam_silent = 0; 816 memmove(nbuf, nbuf + 1, strlen(nbuf)); 817 } else { 818 pam_silent = PAM_SILENT; 819 } 820 return nbuf; 821 } 822 823 /* 824 * SIGINT handler for motd(). 825 */ 826 static volatile int motdinterrupt; 827 static void 828 sigint(int signo __unused) 829 { 830 motdinterrupt = 1; 831 } 832 833 /* 834 * Display the contents of a file (such as /etc/motd). 835 */ 836 static int 837 motd(const char *motdfile) 838 { 839 sig_t oldint; 840 FILE *f; 841 int ch; 842 843 if ((f = fopen(motdfile, "r")) == NULL) 844 return (-1); 845 motdinterrupt = 0; 846 oldint = signal(SIGINT, sigint); 847 while ((ch = fgetc(f)) != EOF && !motdinterrupt) 848 putchar(ch); 849 signal(SIGINT, oldint); 850 if (ch != EOF || ferror(f)) { 851 fclose(f); 852 return (-1); 853 } 854 fclose(f); 855 return (0); 856 } 857 858 /* 859 * SIGALRM handler, to enforce login prompt timeout. 860 * 861 * XXX This can potentially confuse the hell out of PAM. We should 862 * XXX instead implement a conversation function that returns 863 * XXX PAM_CONV_ERR when interrupted by a signal, and have the signal 864 * XXX handler just set a flag. 865 */ 866 static void 867 timedout(int signo __unused) 868 { 869 870 longjmp(timeout_buf, signo); 871 } 872 873 static void 874 badlogin(char *name) 875 { 876 877 if (failures == 0) 878 return; 879 if (hflag) { 880 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s", 881 failures, failures > 1 ? "S" : "", hostname); 882 syslog(LOG_AUTHPRIV|LOG_NOTICE, 883 "%d LOGIN FAILURE%s FROM %s, %s", 884 failures, failures > 1 ? "S" : "", hostname, name); 885 } else { 886 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s", 887 failures, failures > 1 ? "S" : "", tty); 888 syslog(LOG_AUTHPRIV|LOG_NOTICE, 889 "%d LOGIN FAILURE%s ON %s, %s", 890 failures, failures > 1 ? "S" : "", tty, name); 891 } 892 failures = 0; 893 } 894 895 const char * 896 stypeof(char *ttyid) 897 { 898 struct ttyent *t; 899 900 if (ttyid != NULL && *ttyid != '\0') { 901 t = getttynam(ttyid); 902 if (t != NULL && t->ty_type != NULL) 903 return (t->ty_type); 904 } 905 return (NULL); 906 } 907 908 static void 909 refused(const char *msg, const char *rtype, int lout) 910 { 911 912 if (msg != NULL) 913 printf("%s.\n", msg); 914 if (hflag) 915 syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s", 916 pwd->pw_name, rtype, hostname, tty); 917 else 918 syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) ON TTY %s", 919 pwd->pw_name, rtype, tty); 920 if (lout) 921 bail(SLEEP_EXIT, 1); 922 } 923 924 /* 925 * Log a PAM error 926 */ 927 static void 928 pam_syslog(const char *msg) 929 { 930 syslog(LOG_ERR, "%s: %s", msg, pam_strerror(pamh, pam_err)); 931 } 932 933 /* 934 * Shut down PAM 935 */ 936 static void 937 pam_cleanup(void) 938 { 939 940 if (pamh != NULL) { 941 if (pam_session_established) { 942 pam_err = pam_close_session(pamh, 0); 943 if (pam_err != PAM_SUCCESS) 944 pam_syslog("pam_close_session()"); 945 } 946 pam_session_established = 0; 947 if (pam_cred_established) { 948 pam_err = pam_setcred(pamh, pam_silent|PAM_DELETE_CRED); 949 if (pam_err != PAM_SUCCESS) 950 pam_syslog("pam_setcred()"); 951 } 952 pam_cred_established = 0; 953 pam_end(pamh, pam_err); 954 pamh = NULL; 955 } 956 } 957 958 /* 959 * Exit, optionally after sleeping a few seconds 960 */ 961 void 962 bail(int sec, int eval) 963 { 964 965 pam_cleanup(); 966 #ifdef USE_BSM_AUDIT 967 if (pwd != NULL) 968 audit_logout(); 969 #endif 970 (void)sleep(sec); 971 exit(eval); 972 } 973