43/* 44 * login [ name ] 45 * login -h hostname (for telnetd, etc.) 46 * login -f name (for pre-authenticated login: datakit, xterm, etc.) 47 */ 48 49#include <sys/copyright.h> 50#include <sys/param.h> 51#include <sys/file.h> 52#include <sys/socket.h> 53#include <sys/stat.h> 54#include <sys/time.h> 55#include <sys/resource.h> 56#include <sys/wait.h> 57#include <netinet/in.h> 58#include <arpa/inet.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 <netdb.h> 66#include <pwd.h> 67#include <setjmp.h> 68#include <signal.h> 69#include <stdio.h> 70#include <stdlib.h> 71#include <string.h> 72#include <syslog.h> 73#include <ttyent.h> 74#include <unistd.h> 75#include <utmp.h> 76 77#ifndef NO_PAM 78#include <security/pam_appl.h> 79#include <security/pam_misc.h> 80#endif 81 82#include "login.h" 83#include "pathnames.h" 84 85/* wrapper for KAME-special getnameinfo() */ 86#ifndef NI_WITHSCOPEID 87#define NI_WITHSCOPEID 0 88#endif 89 90static int auth_traditional __P((void)); 91static void badlogin __P((char *)); 92static void dolastlog __P((int)); 93static void getloginname __P((void)); 94static void motd __P((const char *)); 95static void refused __P((const char *,const char *,int)); 96static int rootterm __P((char *)); 97static void sigint __P((int)); 98static void sleepexit __P((int)); 99static const char *stypeof __P((char *)); 100static void timedout __P((int)); 101static void usage __P((void)); 102 103#ifndef NO_PAM 104static int auth_pam __P((void)); 105static int export_pam_environment __P((void)); 106static int ok_to_export __P((const char *)); 107 108static pam_handle_t *pamh = NULL; 109static char **environ_pam; 110 111#define PAM_END { \ 112 if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) \ 113 syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); \ 114 if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS) \ 115 syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e)); \ 116 if ((e = pam_end(pamh, e)) != PAM_SUCCESS) \ 117 syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); \ 118} 119#endif /* NO_PAM */ 120 121#define TTYGRPNAME "tty" /* group to own ttys */ 122#define DEFAULT_BACKOFF 3 123#define DEFAULT_RETRIES 10 124#define DEFAULT_PROMPT "login: " 125#define DEFAULT_PASSWD_PROMPT "Password:" 126#define INVALID_HOST "invalid hostname" 127#define UNKNOWN "su" 128#define DEFAULT_WARN (2L * 7L * 86400L) /* Two weeks */ 129#define NBUFSIZ UT_NAMESIZE + 64 130 131/* 132 * This bounds the time given to login. Not a define so it can 133 * be patched on machines where it's too small. 134 */ 135u_int timeout = 300; 136 137/* Buffer for signal handling of timeout */ 138jmp_buf timeout_buf; 139 140struct passwd *pwd; 141int failures; 142char *term, *envinit[1], *hostname, *passwd_prompt, *prompt, *tty, *username; 143char full_hostname[MAXHOSTNAMELEN]; 144 145int 146main(argc, argv) 147 int argc; 148 char *argv[]; 149{ 150 struct group *gr; 151 struct stat st; 152 struct timeval tp; 153 struct utmp utmp; 154 int rootok, retries, backoff; 155 int ask, ch, cnt, fflag, hflag, pflag, quietlog, rootlogin, rval; 156 time_t warntime; 157 uid_t uid, euid; 158 gid_t egid; 159 char *p, *ttyn; 160 char tbuf[MAXPATHLEN + 2]; 161 char tname[sizeof(_PATH_TTY) + 10]; 162 char *shell = NULL; 163 static char default_prompt[] = DEFAULT_PROMPT; 164 static char default_passwd_prompt[] = DEFAULT_PASSWD_PROMPT; 165 static char invalid_host[] = INVALID_HOST; 166 login_cap_t *lc = NULL; 167#ifndef NO_PAM 168 pid_t pid; 169 int e; 170#endif /* NO_PAM */ 171 172 (void)signal(SIGQUIT, SIG_IGN); 173 (void)signal(SIGINT, SIG_IGN); 174 (void)signal(SIGHUP, SIG_IGN); 175 if (setjmp(timeout_buf)) { 176 if (failures) 177 badlogin(tbuf); 178 (void)fprintf(stderr, "Login timed out after %d seconds\n", 179 timeout); 180 exit(0); 181 } 182 (void)signal(SIGALRM, timedout); 183 (void)alarm(timeout); 184 (void)setpriority(PRIO_PROCESS, 0, 0); 185 186 openlog("login", LOG_ODELAY, LOG_AUTH); 187 188 /* 189 * -p is used by getty to tell login not to destroy the environment 190 * -f is used to skip a second login authentication 191 * -h is used by other servers to pass the name of the remote 192 * host to login so that it may be placed in utmp and wtmp 193 */ 194 *full_hostname = '\0'; 195 term = NULL; 196 197 fflag = hflag = pflag = 0; 198 uid = getuid(); 199 euid = geteuid(); 200 egid = getegid(); 201 while ((ch = getopt(argc, argv, "fh:p")) != -1) 202 switch (ch) { 203 case 'f': 204 fflag = 1; 205 break; 206 case 'h': 207 if (uid) 208 errx(1, "-h option: %s", strerror(EPERM)); 209 hflag = 1; 210 if (strlcpy(full_hostname, optarg, 211 sizeof(full_hostname)) >= sizeof(full_hostname)) 212 errx(1, "-h option: %s: exceeds maximum " 213 "hostname size", optarg); 214 215 trimdomain(optarg, UT_HOSTSIZE); 216 217 if (strlen(optarg) > UT_HOSTSIZE) { 218 struct addrinfo hints, *res; 219 int ga_err; 220 221 memset(&hints, 0, sizeof(hints)); 222 hints.ai_family = AF_UNSPEC; 223 ga_err = getaddrinfo(optarg, NULL, &hints, 224 &res); 225 if (ga_err == 0) { 226 char hostbuf[MAXHOSTNAMELEN]; 227 228 getnameinfo(res->ai_addr, 229 res->ai_addrlen, 230 hostbuf, 231 sizeof(hostbuf), NULL, 0, 232 NI_NUMERICHOST| 233 NI_WITHSCOPEID); 234 optarg = strdup(hostbuf); 235 if (optarg == NULL) { 236 syslog(LOG_NOTICE, 237 "strdup(): %m"); 238 sleepexit(1); 239 } 240 } else 241 optarg = invalid_host; 242 if (res != NULL) 243 freeaddrinfo(res); 244 } 245 hostname = optarg; 246 break; 247 case 'p': 248 pflag = 1; 249 break; 250 case '?': 251 default: 252 if (!uid) 253 syslog(LOG_ERR, "invalid flag %c", ch); 254 usage(); 255 } 256 argc -= optind; 257 argv += optind; 258 259 if (*argv) { 260 username = *argv; 261 ask = 0; 262 } else 263 ask = 1; 264 265 for (cnt = getdtablesize(); cnt > 2; cnt--) 266 (void)close(cnt); 267 268 ttyn = ttyname(STDIN_FILENO); 269 if (ttyn == NULL || *ttyn == '\0') { 270 (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY); 271 ttyn = tname; 272 } 273 if ((tty = strrchr(ttyn, '/')) != NULL) 274 ++tty; 275 else 276 tty = ttyn; 277 278 /* 279 * Get "login-retries" & "login-backoff" from default class 280 */ 281 lc = login_getclass(NULL); 282 prompt = login_getcapstr(lc, "prompt", default_prompt, default_prompt); 283 passwd_prompt = login_getcapstr(lc, "passwd_prompt", 284 default_passwd_prompt, default_passwd_prompt); 285 retries = login_getcapnum(lc, "login-retries", DEFAULT_RETRIES, 286 DEFAULT_RETRIES); 287 backoff = login_getcapnum(lc, "login-backoff", DEFAULT_BACKOFF, 288 DEFAULT_BACKOFF); 289 login_close(lc); 290 lc = NULL; 291 292 for (cnt = 0;; ask = 1) { 293 if (ask) { 294 fflag = 0; 295 getloginname(); 296 } 297 rootlogin = 0; 298 rootok = rootterm(tty); /* Default (auth may change) */ 299 300 if (strlen(username) > UT_NAMESIZE) 301 username[UT_NAMESIZE] = '\0'; 302 303 /* 304 * Note if trying multiple user names; log failures for 305 * previous user name, but don't bother logging one failure 306 * for nonexistent name (mistyped username). 307 */ 308 if (failures && strcmp(tbuf, username)) { 309 if (failures > (pwd ? 0 : 1)) 310 badlogin(tbuf); 311 } 312 (void)strlcpy(tbuf, username, sizeof(tbuf)); 313 314 pwd = getpwnam(username); 315 316 /* 317 * if we have a valid account name, and it doesn't have a 318 * password, or the -f option was specified and the caller 319 * is root or the caller isn't changing their uid, don't 320 * authenticate. 321 */ 322 if (pwd != NULL) { 323 if (pwd->pw_uid == 0) 324 rootlogin = 1; 325 326 if (fflag && (uid == (uid_t)0 || 327 uid == (uid_t)pwd->pw_uid)) { 328 /* already authenticated */ 329 break; 330 } else if (pwd->pw_passwd[0] == '\0') { 331 if (!rootlogin || rootok) { 332 /* pretend password okay */ 333 rval = 0; 334 goto ttycheck; 335 } 336 } 337 } 338 339 fflag = 0; 340 341 (void)setpriority(PRIO_PROCESS, 0, -4); 342 343#ifndef NO_PAM 344 /* 345 * Try to authenticate using PAM. If a PAM system error 346 * occurs, perhaps because of a botched configuration, 347 * then fall back to using traditional Unix authentication. 348 */ 349 if ((rval = auth_pam()) == -1) 350#endif /* NO_PAM */ 351 rval = auth_traditional(); 352 353 (void)setpriority(PRIO_PROCESS, 0, 0); 354 355 /* 356 * PAM authentication may have changed "pwd" to the 357 * entry for the template user. Check again to see if 358 * this is a root login after all. 359 */ 360 if (pwd != NULL && pwd->pw_uid == 0) 361 rootlogin = 1; 362 363 ttycheck: 364 /* 365 * If trying to log in as root without Kerberos, 366 * but with insecure terminal, refuse the login attempt. 367 */ 368 if (pwd && !rval) { 369 if (rootlogin && !rootok) 370 refused(NULL, "NOROOT", 0); 371 else /* valid password & authenticated */ 372 break; 373 } 374 375 (void)printf("Login incorrect\n"); 376 failures++; 377 378 /* 379 * we allow up to 'retry' (10) tries, 380 * but after 'backoff' (3) we start backing off 381 */ 382 if (++cnt > backoff) { 383 if (cnt >= retries) { 384 badlogin(username); 385 sleepexit(1); 386 } 387 sleep((u_int)((cnt - backoff) * 5)); 388 } 389 } 390 391 /* committed to login -- turn off timeout */ 392 (void)alarm((u_int)0); 393 (void)signal(SIGHUP, SIG_DFL); 394 395 endpwent(); 396 397 /* 398 * Establish the login class. 399 */ 400 lc = login_getpwclass(pwd); 401 402 quietlog = login_getcapbool(lc, "hushlogin", 0); 403 /* 404 * Switching needed for NFS with root access disabled. 405 * 406 * XXX: This change fails to modify the additional groups for the 407 * process, and as such, may restrict rights normally granted 408 * through those groups. 409 */ 410 (void)setegid(pwd->pw_gid); 411 (void)seteuid(rootlogin ? 0 : pwd->pw_uid); 412 if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) { 413 if (login_getcapbool(lc, "requirehome", 0)) 414 refused("Home directory not available", "HOMEDIR", 1); 415 if (chdir("/") < 0) 416 refused("Cannot find root directory", "ROOTDIR", 1); 417 if (!quietlog || *pwd->pw_dir) 418 printf("No home directory.\nLogging in with home = \"/\".\n"); 419 pwd->pw_dir = strdup("/"); 420 if (pwd->pw_dir == NULL) { 421 syslog(LOG_NOTICE, "strdup(): %m"); 422 sleepexit(1); 423 } 424 } 425 (void)seteuid(euid); 426 (void)setegid(egid); 427 if (!quietlog) 428 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0; 429 430 if (pwd->pw_change || pwd->pw_expire) 431 (void)gettimeofday(&tp, (struct timezone *)NULL); 432 433 warntime = login_getcaptime(lc, "warnexpire", DEFAULT_WARN, 434 DEFAULT_WARN); 435 436 if (pwd->pw_expire) { 437 if (tp.tv_sec >= pwd->pw_expire) { 438 refused("Sorry -- your account has expired", "EXPIRED", 439 1); 440 } else if (pwd->pw_expire - tp.tv_sec < warntime && !quietlog) 441 (void)printf("Warning: your account expires on %s", 442 ctime(&pwd->pw_expire)); 443 } 444 445 if (lc != NULL) { 446 if (hostname) { 447 struct addrinfo hints, *res; 448 int ga_err; 449 450 memset(&hints, 0, sizeof(hints)); 451 hints.ai_family = AF_UNSPEC; 452 ga_err = getaddrinfo(full_hostname, NULL, &hints, 453 &res); 454 if (ga_err == 0) { 455 char hostbuf[MAXHOSTNAMELEN]; 456 457 getnameinfo(res->ai_addr, res->ai_addrlen, 458 hostbuf, sizeof(hostbuf), NULL, 0, 459 NI_NUMERICHOST|NI_WITHSCOPEID); 460 if ((optarg = strdup(hostbuf)) == NULL) { 461 syslog(LOG_NOTICE, "strdup(): %m"); 462 sleepexit(1); 463 } 464 } else 465 optarg = NULL; 466 if (res != NULL) 467 freeaddrinfo(res); 468 if (!auth_hostok(lc, full_hostname, optarg)) 469 refused("Permission denied", "HOST", 1); 470 } 471 472 if (!auth_ttyok(lc, tty)) 473 refused("Permission denied", "TTY", 1); 474 475 if (!auth_timeok(lc, time(NULL))) 476 refused("Logins not available right now", "TIME", 1); 477 } 478 shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell); 479 if (*pwd->pw_shell == '\0') 480 pwd->pw_shell = strdup(_PATH_BSHELL); 481 if (pwd->pw_shell == NULL) { 482 syslog(LOG_NOTICE, "strdup(): %m"); 483 sleepexit(1); 484 } 485 if (*shell == '\0') /* Not overridden */ 486 shell = pwd->pw_shell; 487 if ((shell = strdup(shell)) == NULL) { 488 syslog(LOG_NOTICE, "strdup(): %m"); 489 sleepexit(1); 490 } 491 492#ifdef LOGIN_ACCESS 493 if (login_access(pwd->pw_name, hostname ? full_hostname : tty) == 0) 494 refused("Permission denied", "ACCESS", 1); 495#endif /* LOGIN_ACCESS */ 496 497 /* Nothing else left to fail -- really log in. */ 498 memset((void *)&utmp, 0, sizeof(utmp)); 499 (void)time(&utmp.ut_time); 500 (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name)); 501 if (hostname) 502 (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host)); 503 (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line)); 504 login(&utmp); 505 506 dolastlog(quietlog); 507 508 /* 509 * Set device protections, depending on what terminal the 510 * user is logged in. This feature is used on Suns to give 511 * console users better privacy. 512 */ 513 login_fbtab(tty, pwd->pw_uid, pwd->pw_gid); 514 515 /* 516 * Clear flags of the tty. None should be set, and when the 517 * user sets them otherwise, this can cause the chown to fail. 518 * Since it isn't clear that flags are useful on character 519 * devices, we just clear them. 520 */ 521 if (chflags(ttyn, 0) && errno != EOPNOTSUPP) 522 syslog(LOG_ERR, "chmod(%s): %m", ttyn); 523 if (chown(ttyn, pwd->pw_uid, 524 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid)) 525 syslog(LOG_ERR, "chmod(%s): %m", ttyn); 526 527 528 /* 529 * Preserve TERM if it happens to be already set. 530 */ 531 if ((term = getenv("TERM")) != NULL) { 532 if ((term = strdup(term)) == NULL) { 533 syslog(LOG_NOTICE, 534 "strdup(): %m"); 535 sleepexit(1); 536 } 537 } 538 539 /* 540 * Exclude cons/vt/ptys only, assume dialup otherwise 541 * TODO: Make dialup tty determination a library call 542 * for consistency (finger etc.) 543 */ 544 if (hostname==NULL && isdialuptty(tty)) 545 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name); 546 547#ifdef LOGALL 548 /* 549 * Syslog each successful login, so we don't have to watch hundreds 550 * of wtmp or lastlogin files. 551 */ 552 if (hostname) 553 syslog(LOG_INFO, "login from %s on %s as %s", 554 full_hostname, tty, pwd->pw_name); 555 else 556 syslog(LOG_INFO, "login on %s as %s", 557 tty, pwd->pw_name); 558#endif 559 560 /* 561 * If fflag is on, assume caller/authenticator has logged root login. 562 */ 563 if (rootlogin && fflag == 0) 564 { 565 if (hostname) 566 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s", 567 username, tty, full_hostname); 568 else 569 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", 570 username, tty); 571 } 572 573 /* 574 * Destroy environment unless user has requested its preservation. 575 * We need to do this before setusercontext() because that may 576 * set or reset some environment variables. 577 */ 578 if (!pflag) 579 environ = envinit; 580 581 /* 582 * PAM modules might add supplementary groups during pam_setcred(). 583 */ 584 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) { 585 syslog(LOG_ERR, "setusercontext() failed - exiting"); 586 exit(1); 587 } 588 589#ifndef NO_PAM 590 if (pamh) { 591 if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) { 592 syslog(LOG_ERR, "pam_open_session: %s", 593 pam_strerror(pamh, e)); 594 } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) 595 != PAM_SUCCESS) { 596 syslog(LOG_ERR, "pam_setcred: %s", 597 pam_strerror(pamh, e)); 598 } 599 600 /* 601 * Add any environmental variables that the 602 * PAM modules may have set. 603 * Call *after* opening session! 604 */ 605 if (pamh) { 606 environ_pam = pam_getenvlist(pamh); 607 if (environ_pam) 608 export_pam_environment(); 609 } 610 611 /* 612 * We must fork() before setuid() because we need to call 613 * pam_close_session() as root. 614 */ 615 pid = fork(); 616 if (pid < 0) { 617 err(1, "fork"); 618 PAM_END; 619 exit(0); 620 } else if (pid) { 621 /* parent - wait for child to finish, then cleanup 622 session */ 623 wait(NULL); 624 PAM_END; 625 exit(0); 626 } else { 627 if ((e = pam_end(pamh, PAM_DATA_SILENT)) != PAM_SUCCESS) 628 syslog(LOG_ERR, "pam_end: %s", 629 pam_strerror(pamh, e)); 630 } 631 } 632#endif /* NO_PAM */ 633 634 /* 635 * We don't need to be root anymore, so 636 * set the user and session context 637 */ 638 if (setlogin(username) != 0) { 639 syslog(LOG_ERR, "setlogin(%s): %m - exiting", username); 640 exit(1); 641 } 642 if (setusercontext(lc, pwd, pwd->pw_uid, 643 LOGIN_SETALL & ~(LOGIN_SETLOGIN|LOGIN_SETGROUP)) != 0) { 644 syslog(LOG_ERR, "setusercontext() failed - exiting"); 645 exit(1); 646 } 647 648 (void)setenv("SHELL", pwd->pw_shell, 1); 649 (void)setenv("HOME", pwd->pw_dir, 1); 650 if (term != NULL && *term != '\0') 651 (void)setenv("TERM", term, 1); /* Preset overrides */ 652 else { 653 (void)setenv("TERM", stypeof(tty), 0); /* Fallback doesn't */ 654 } 655 (void)setenv("LOGNAME", username, 1); 656 (void)setenv("USER", username, 1); 657 (void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0); 658 659 if (!quietlog) { 660 const char *cw; 661 662 cw = login_getcapstr(lc, "copyright", NULL, NULL); 663 if (cw != NULL && access(cw, F_OK) == 0) 664 motd(cw); 665 else 666 (void)printf("%s\n\t%s %s\n", 667 "Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994", 668 "The Regents of the University of California. ", 669 "All rights reserved."); 670 671 (void)printf("\n"); 672 673 cw = login_getcapstr(lc, "welcome", NULL, NULL); 674 if (cw == NULL || access(cw, F_OK) != 0) 675 cw = _PATH_MOTDFILE; 676 motd(cw); 677 678 if (login_getcapbool(lc, "nocheckmail", 0) == 0) { 679 /* $MAIL may have been set by class. */ 680 cw = getenv("MAIL"); 681 if (cw != NULL) 682 strlcpy(tbuf, cw, sizeof(tbuf)); 683 else 684 snprintf(tbuf, sizeof(tbuf), "%s/%s", 685 _PATH_MAILDIR, pwd->pw_name); 686 if (stat(tbuf, &st) == 0 && st.st_size != 0) 687 (void)printf("You have %smail.\n", 688 (st.st_mtime > st.st_atime) ? "new " : ""); 689 } 690 } 691 692 login_close(lc); 693 694 (void)signal(SIGALRM, SIG_DFL); 695 (void)signal(SIGQUIT, SIG_DFL); 696 (void)signal(SIGINT, SIG_DFL); 697 (void)signal(SIGTSTP, SIG_IGN); 698 699 /* 700 * Login shells have a leading '-' in front of argv[0] 701 */ 702 if ((u_int)snprintf(tbuf, sizeof(tbuf), "-%s", 703 (p = strrchr(pwd->pw_shell, '/')) ? p + 1 : pwd->pw_shell) >= 704 sizeof(tbuf)) { 705 syslog(LOG_ERR, "user: %s: shell exceeds maximum pathname size", 706 username); 707 errx(1, "shell exceeds maximum pathname size"); 708 } 709 710 execlp(shell, tbuf, (char *)0); 711 err(1, "%s", shell); 712} 713 714static int 715auth_traditional() 716{ 717 int rval; 718 char *p; 719 const char *ep; 720 const char *salt; 721 722 rval = 1; 723 salt = pwd != NULL ? pwd->pw_passwd : "xx"; 724 725 p = getpass(passwd_prompt); 726 ep = crypt(p, salt); 727 728 if (pwd) { 729 if (!p[0] && pwd->pw_passwd[0]) 730 ep = ":"; 731 if (strcmp(ep, pwd->pw_passwd) == 0) 732 rval = 0; 733 } 734 735 /* clear entered password */ 736 memset(p, 0, strlen(p)); 737 return rval; 738} 739 740#ifndef NO_PAM 741/* 742 * Attempt to authenticate the user using PAM. Returns 0 if the user is 743 * authenticated, or 1 if not authenticated. If some sort of PAM system 744 * error occurs (e.g., the "/etc/pam.conf" file is missing) then this 745 * function returns -1. This can be used as an indication that we should 746 * fall back to a different authentication mechanism. 747 */ 748static int 749auth_pam() 750{ 751 const char *tmpl_user; 752 const void *item; 753 int rval; 754 int e; 755 static struct pam_conv conv = { misc_conv, NULL }; 756 757 if ((e = pam_start("login", username, &conv, &pamh)) != PAM_SUCCESS) { 758 syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e)); 759 return -1; 760 } 761 if ((e = pam_set_item(pamh, PAM_TTY, tty)) != PAM_SUCCESS) { 762 syslog(LOG_ERR, "pam_set_item(PAM_TTY): %s", 763 pam_strerror(pamh, e)); 764 return -1; 765 } 766 if (hostname != NULL && 767 (e = pam_set_item(pamh, PAM_RHOST, full_hostname)) != PAM_SUCCESS) { 768 syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s", 769 pam_strerror(pamh, e)); 770 return -1; 771 } 772 e = pam_authenticate(pamh, 0); 773 switch (e) { 774 775 case PAM_SUCCESS: 776 /* 777 * With PAM we support the concept of a "template" 778 * user. The user enters a login name which is 779 * authenticated by PAM, usually via a remote service 780 * such as RADIUS or TACACS+. If authentication 781 * succeeds, a different but related "template" name 782 * is used for setting the credentials, shell, and 783 * home directory. The name the user enters need only 784 * exist on the remote authentication server, but the 785 * template name must be present in the local password 786 * database. 787 * 788 * This is supported by two various mechanisms in the 789 * individual modules. However, from the application's 790 * point of view, the template user is always passed 791 * back as a changed value of the PAM_USER item. 792 */ 793 if ((e = pam_get_item(pamh, PAM_USER, &item)) == 794 PAM_SUCCESS) { 795 tmpl_user = (const char *) item; 796 if (strcmp(username, tmpl_user) != 0) 797 pwd = getpwnam(tmpl_user); 798 } else 799 syslog(LOG_ERR, "Couldn't get PAM_USER: %s", 800 pam_strerror(pamh, e)); 801 rval = 0; 802 break; 803 804 case PAM_AUTH_ERR: 805 case PAM_USER_UNKNOWN: 806 case PAM_MAXTRIES: 807 rval = 1; 808 break; 809 810 default: 811 syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e)); 812 rval = -1; 813 break; 814 } 815 816 if (rval == 0) { 817 e = pam_acct_mgmt(pamh, 0); 818 if (e == PAM_NEW_AUTHTOK_REQD) { 819 e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK); 820 if (e != PAM_SUCCESS) { 821 syslog(LOG_ERR, "pam_chauthtok: %s", 822 pam_strerror(pamh, e)); 823 rval = 1; 824 } 825 } else if (e != PAM_SUCCESS) { 826 rval = 1; 827 } 828 } 829 830 if (rval != 0) { 831 if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 832 syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 833 } 834 pamh = NULL; 835 } 836 return rval; 837} 838 839static int 840export_pam_environment() 841{ 842 char **pp; 843 844 for (pp = environ_pam; *pp != NULL; pp++) { 845 if (ok_to_export(*pp)) 846 (void) putenv(*pp); 847 free(*pp); 848 } 849 return PAM_SUCCESS; 850} 851 852/* 853 * Sanity checks on PAM environmental variables: 854 * - Make sure there is an '=' in the string. 855 * - Make sure the string doesn't run on too long. 856 * - Do not export certain variables. This list was taken from the 857 * Solaris pam_putenv(3) man page. 858 */ 859static int 860ok_to_export(s) 861 const char *s; 862{ 863 static const char *noexport[] = { 864 "SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH", 865 "IFS", "PATH", NULL 866 }; 867 const char **pp; 868 size_t n; 869 870 if (strlen(s) > 1024 || strchr(s, '=') == NULL) 871 return 0; 872 if (strncmp(s, "LD_", 3) == 0) 873 return 0; 874 for (pp = noexport; *pp != NULL; pp++) { 875 n = strlen(*pp); 876 if (s[n] == '=' && strncmp(s, *pp, n) == 0) 877 return 0; 878 } 879 return 1; 880} 881#endif /* NO_PAM */ 882 883static void 884usage() 885{ 886 887 (void)fprintf(stderr, "usage: login [-fp] [-h hostname] [username]\n"); 888 exit(1); 889} 890 891/* 892 * Allow for authentication style and/or kerberos instance 893 */ 894 895void 896getloginname() 897{ 898 int ch; 899 char *p; 900 static char nbuf[NBUFSIZ]; 901 902 for (;;) { 903 (void)printf("%s", prompt); 904 for (p = nbuf; (ch = getchar()) != '\n'; ) { 905 if (ch == EOF) { 906 badlogin(username); 907 exit(0); 908 } 909 if (p < nbuf + (NBUFSIZ - 1)) 910 *p++ = ch; 911 } 912 if (p > nbuf) { 913 if (nbuf[0] == '-') 914 (void)fprintf(stderr, 915 "login names may not start with '-'.\n"); 916 else { 917 *p = '\0'; 918 username = nbuf; 919 break; 920 } 921 } 922 } 923} 924 925int 926rootterm(ttyn) 927 char *ttyn; 928{ 929 struct ttyent *t; 930 931 return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE); 932} 933 934volatile int motdinterrupt; 935 936void 937sigint(signo) 938 int signo __unused; 939{ 940 motdinterrupt = 1; 941} 942 943void 944motd(motdfile) 945 const char *motdfile; 946{ 947 int fd, nchars; 948 sig_t oldint; 949 char tbuf[256]; 950 951 if ((fd = open(motdfile, O_RDONLY, 0)) < 0) 952 return; 953 motdinterrupt = 0; 954 oldint = signal(SIGINT, sigint); 955 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0 && !motdinterrupt) 956 (void)write(fileno(stdout), tbuf, nchars); 957 (void)signal(SIGINT, oldint); 958 (void)close(fd); 959} 960 961/* ARGSUSED */ 962void 963timedout(signo) 964 int signo; 965{ 966 967 longjmp(timeout_buf, signo); 968} 969 970 971void 972dolastlog(quiet) 973 int quiet; 974{ 975 struct lastlog ll; 976 int fd; 977 978 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) { 979 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); 980 if (!quiet) { 981 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) && 982 ll.ll_time != 0) { 983 (void)printf("Last login: %.*s ", 984 24-5, (char *)ctime(&ll.ll_time)); 985 if (*ll.ll_host != '\0') 986 (void)printf("from %.*s\n", 987 (int)sizeof(ll.ll_host), 988 ll.ll_host); 989 else 990 (void)printf("on %.*s\n", 991 (int)sizeof(ll.ll_line), 992 ll.ll_line); 993 } 994 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); 995 } 996 memset((void *)&ll, 0, sizeof(ll)); 997 (void)time(&ll.ll_time); 998 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); 999 if (hostname) 1000 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host)); 1001 (void)write(fd, (char *)&ll, sizeof(ll)); 1002 (void)close(fd); 1003 } else { 1004 syslog(LOG_ERR, "cannot open %s: %m", _PATH_LASTLOG); 1005 } 1006} 1007 1008void 1009badlogin(name) 1010 char *name; 1011{ 1012 1013 if (failures == 0) 1014 return; 1015 if (hostname) { 1016 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s", 1017 failures, failures > 1 ? "S" : "", full_hostname); 1018 syslog(LOG_AUTHPRIV|LOG_NOTICE, 1019 "%d LOGIN FAILURE%s FROM %s, %s", 1020 failures, failures > 1 ? "S" : "", full_hostname, name); 1021 } else { 1022 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s", 1023 failures, failures > 1 ? "S" : "", tty); 1024 syslog(LOG_AUTHPRIV|LOG_NOTICE, 1025 "%d LOGIN FAILURE%s ON %s, %s", 1026 failures, failures > 1 ? "S" : "", tty, name); 1027 } 1028 failures = 0; 1029} 1030 1031const char * 1032stypeof(ttyid) 1033 char *ttyid; 1034{ 1035 struct ttyent *t; 1036 1037 if (ttyid != NULL && *ttyid != '\0') { 1038 t = getttynam(ttyid); 1039 if (t != NULL && t->ty_type != NULL) 1040 return (t->ty_type); 1041 } 1042 return (UNKNOWN); 1043} 1044 1045void 1046refused(msg, rtype, lout) 1047 const char *msg; 1048 const char *rtype; 1049 int lout; 1050{ 1051 1052 if (msg != NULL) 1053 printf("%s.\n", msg); 1054 if (hostname) 1055 syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s", 1056 pwd->pw_name, rtype, full_hostname, tty); 1057 else 1058 syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) ON TTY %s", 1059 pwd->pw_name, rtype, tty); 1060 if (lout) 1061 sleepexit(1); 1062} 1063 1064void 1065sleepexit(eval) 1066 int eval; 1067{ 1068 1069 (void)sleep(5); 1070 exit(eval); 1071}
|