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