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