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