Home | History | Annotate | Line # | Download | only in login
login.c revision 1.101
      1 /*	$NetBSD: login.c,v 1.101 2012/04/23 20:57:04 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\
     35  The Regents of the University of California.  All rights reserved.");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)login.c	8.4 (Berkeley) 4/2/94";
     41 #endif
     42 __RCSID("$NetBSD: login.c,v 1.101 2012/04/23 20:57:04 christos Exp $");
     43 #endif /* not lint */
     44 
     45 /*
     46  * login [ name ]
     47  * login -h hostname	(for telnetd, etc.)
     48  * login -f name	(for pre-authenticated login: datakit, xterm, etc.)
     49  */
     50 
     51 #include <sys/param.h>
     52 #include <sys/stat.h>
     53 #include <sys/time.h>
     54 #include <sys/resource.h>
     55 #include <sys/file.h>
     56 #include <sys/wait.h>
     57 #include <sys/socket.h>
     58 
     59 #include <err.h>
     60 #include <errno.h>
     61 #include <grp.h>
     62 #include <pwd.h>
     63 #include <setjmp.h>
     64 #include <signal.h>
     65 #include <stdio.h>
     66 #include <stdlib.h>
     67 #include <string.h>
     68 #include <syslog.h>
     69 #include <time.h>
     70 #include <ttyent.h>
     71 #include <tzfile.h>
     72 #include <unistd.h>
     73 #include <sysexits.h>
     74 #ifdef SUPPORT_UTMP
     75 #include <utmp.h>
     76 #endif
     77 #ifdef SUPPORT_UTMPX
     78 #include <utmpx.h>
     79 #endif
     80 #include <util.h>
     81 #ifdef SKEY
     82 #include <skey.h>
     83 #endif
     84 #ifdef KERBEROS5
     85 #include <krb5/krb5.h>
     86 #include <krb5/com_err.h>
     87 #endif
     88 #ifdef LOGIN_CAP
     89 #include <login_cap.h>
     90 #endif
     91 #include <vis.h>
     92 
     93 #include "pathnames.h"
     94 #include "common.h"
     95 
     96 #ifdef KERBEROS5
     97 int login_krb5_forwardable_tgt = 0;
     98 static int login_krb5_get_tickets = 1;
     99 static int login_krb5_retain_ccache = 0;
    100 #endif
    101 
    102 static void	 checknologin(char *);
    103 #ifdef KERBEROS5
    104 int	 k5login(struct passwd *, char *, char *, char *);
    105 void	 k5destroy(void);
    106 int	 k5_read_creds(const char *);
    107 int	 k5_write_creds(void);
    108 #endif
    109 #if defined(KERBEROS5)
    110 static void	 dofork(void);
    111 #endif
    112 static void	 usage(void);
    113 
    114 #define	TTYGRPNAME	"tty"		/* name of group to own ttys */
    115 
    116 #define DEFAULT_BACKOFF 3
    117 #define DEFAULT_RETRIES 10
    118 
    119 #if defined(KERBEROS5)
    120 int	has_ccache = 0;
    121 int	notickets = 1;
    122 extern krb5_context kcontext;
    123 extern int	have_forward;
    124 extern char	*krb5tkfile_env;
    125 extern int	krb5_configured;
    126 #endif
    127 
    128 static char	*instance;
    129 
    130 #if defined(KERBEROS5)
    131 #define	KERBEROS_CONFIGURED	krb5_configured
    132 #endif
    133 
    134 extern char **environ;
    135 
    136 int
    137 main(int argc, char *argv[])
    138 {
    139 	struct group *gr;
    140 	struct stat st;
    141 	int ask, ch, cnt, fflag, hflag, pflag, sflag, quietlog, rootlogin, rval;
    142 	int Fflag;
    143 	uid_t uid, saved_uid;
    144 	gid_t saved_gid, saved_gids[NGROUPS_MAX];
    145 	int nsaved_gids;
    146 	char *domain, *p, *ttyn;
    147 	const char *pwprompt;
    148 	char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
    149 	char localhost[MAXHOSTNAMELEN + 1];
    150 	int need_chpass, require_chpass;
    151 	int login_retries = DEFAULT_RETRIES,
    152 	    login_backoff = DEFAULT_BACKOFF;
    153 	time_t pw_warntime = _PASSWORD_WARNDAYS * SECSPERDAY;
    154 #ifdef KERBEROS5
    155 	krb5_error_code kerror;
    156 #endif
    157 #if defined(KERBEROS5)
    158 	int got_tickets = 0;
    159 #endif
    160 #ifdef LOGIN_CAP
    161 	char *shell = NULL;
    162 	login_cap_t *lc = NULL;
    163 #endif
    164 
    165 	tbuf[0] = '\0';
    166 	rval = 0;
    167 	pwprompt = NULL;
    168 	nested = NULL;
    169 	need_chpass = require_chpass = 0;
    170 
    171 	(void)signal(SIGALRM, timedout);
    172 	(void)alarm(timeout);
    173 	(void)signal(SIGQUIT, SIG_IGN);
    174 	(void)signal(SIGINT, SIG_IGN);
    175 	(void)setpriority(PRIO_PROCESS, 0, 0);
    176 
    177 	openlog("login", 0, LOG_AUTH);
    178 
    179 	/*
    180 	 * -p is used by getty to tell login not to destroy the environment
    181 	 * -f is used to skip a second login authentication
    182 	 * -h is used by other servers to pass the name of the remote host to
    183 	 *    login so that it may be placed in utmp/utmpx and wtmp/wtmpx
    184 	 * -a in addition to -h, a server may supply -a to pass the actual
    185 	 *    server address.
    186 	 * -s is used to force use of S/Key or equivalent.
    187 	 */
    188 	domain = NULL;
    189 	if (gethostname(localhost, sizeof(localhost)) < 0)
    190 		syslog(LOG_ERR, "couldn't get local hostname: %m");
    191 	else
    192 		domain = strchr(localhost, '.');
    193 	localhost[sizeof(localhost) - 1] = '\0';
    194 
    195 	Fflag = fflag = hflag = pflag = sflag = 0;
    196 	have_ss = 0;
    197 #ifdef KERBEROS5
    198 	have_forward = 0;
    199 #endif
    200 	uid = getuid();
    201 	while ((ch = getopt(argc, argv, "a:Ffh:ps")) != -1)
    202 		switch (ch) {
    203 		case 'a':
    204 			if (uid)
    205 				errx(EXIT_FAILURE, "-a option: %s", strerror(EPERM));
    206 			decode_ss(optarg);
    207 #ifdef notdef
    208 			(void)sockaddr_snprintf(optarg,
    209 			    sizeof(struct sockaddr_storage), "%a", (void *)&ss);
    210 #endif
    211 			break;
    212 		case 'F':
    213 			Fflag = 1;
    214 			/* FALLTHROUGH */
    215 		case 'f':
    216 			fflag = 1;
    217 			break;
    218 		case 'h':
    219 			if (uid)
    220 				errx(EXIT_FAILURE, "-h option: %s", strerror(EPERM));
    221 			hflag = 1;
    222 #ifdef notdef
    223 			if (domain && (p = strchr(optarg, '.')) != NULL &&
    224 			    strcasecmp(p, domain) == 0)
    225 				*p = '\0';
    226 #endif
    227 			hostname = optarg;
    228 			break;
    229 		case 'p':
    230 			pflag = 1;
    231 			break;
    232 		case 's':
    233 			sflag = 1;
    234 			break;
    235 		default:
    236 		case '?':
    237 			usage();
    238 			break;
    239 		}
    240 
    241 	setproctitle(NULL);
    242 	argc -= optind;
    243 	argv += optind;
    244 
    245 	if (*argv) {
    246 		username = instance = *argv;
    247 		ask = 0;
    248 	} else
    249 		ask = 1;
    250 
    251 #ifdef F_CLOSEM
    252 	(void)fcntl(3, F_CLOSEM, 0);
    253 #else
    254 	for (cnt = getdtablesize(); cnt > 2; cnt--)
    255 		(void)close(cnt);
    256 #endif
    257 
    258 	ttyn = ttyname(STDIN_FILENO);
    259 	if (ttyn == NULL || *ttyn == '\0') {
    260 		(void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
    261 		ttyn = tname;
    262 	}
    263 	if ((tty = strstr(ttyn, "/pts/")) != NULL)
    264 		++tty;
    265 	else if ((tty = strrchr(ttyn, '/')) != NULL)
    266 		++tty;
    267 	else
    268 		tty = ttyn;
    269 
    270 	if (issetugid()) {
    271 		nested = strdup(user_from_uid(getuid(), 0));
    272 		if (nested == NULL) {
    273 			syslog(LOG_ERR, "strdup: %m");
    274 			sleepexit(EXIT_FAILURE);
    275 		}
    276 	}
    277 
    278 #ifdef LOGIN_CAP
    279 	/* Get "login-retries" and "login-backoff" from default class */
    280 	if ((lc = login_getclass(NULL)) != NULL) {
    281 		login_retries = (int)login_getcapnum(lc, "login-retries",
    282 		    DEFAULT_RETRIES, DEFAULT_RETRIES);
    283 		login_backoff = (int)login_getcapnum(lc, "login-backoff",
    284 		    DEFAULT_BACKOFF, DEFAULT_BACKOFF);
    285 		login_close(lc);
    286 		lc = NULL;
    287 	}
    288 #endif
    289 
    290 #ifdef KERBEROS5
    291 	kerror = krb5_init_context(&kcontext);
    292 	if (kerror) {
    293 		/*
    294 		 * If Kerberos is not configured, that is, we are
    295 		 * not using Kerberos, do not log the error message.
    296 		 * However, if Kerberos is configured,  and the
    297 		 * context init fails for some other reason, we need
    298 		 * to issue a no tickets warning to the user when the
    299 		 * login succeeds.
    300 		 */
    301 		if (kerror != ENXIO) {	/* XXX NetBSD-local Heimdal hack */
    302 			syslog(LOG_NOTICE,
    303 			    "%s when initializing Kerberos context",
    304 			    error_message(kerror));
    305 			krb5_configured = 1;
    306 		}
    307 		login_krb5_get_tickets = 0;
    308 	}
    309 #endif /* KERBEROS5 */
    310 
    311 	for (cnt = 0;; ask = 1) {
    312 		char *ptr;
    313 #if defined(KERBEROS5)
    314 		if (login_krb5_get_tickets)
    315 			k5destroy();
    316 #endif
    317 		if (ask) {
    318 			fflag = 0;
    319 			instance = getloginname();
    320 		}
    321 		rootlogin = 0;
    322 		ptr = instance;
    323 #ifdef KERBEROS5
    324 		if ((instance = strchr(instance, '/')) != NULL)
    325 			*instance++ = '\0';
    326 		else
    327 			instance = __UNCONST("");
    328 #endif
    329 		username = trimloginname(ptr);
    330 		/*
    331 		 * Note if trying multiple user names; log failures for
    332 		 * previous user name, but don't bother logging one failure
    333 		 * for nonexistent name (mistyped username).
    334 		 */
    335 		if (failures && strcmp(tbuf, username)) {
    336 			if (failures > (pwd ? 0 : 1))
    337 				badlogin(tbuf);
    338 			failures = 0;
    339 		}
    340 		(void)strlcpy(tbuf, username, sizeof(tbuf));
    341 
    342 		pwd = getpwnam(username);
    343 
    344 #ifdef LOGIN_CAP
    345 		/*
    346 		 * Establish the class now, before we might goto
    347 		 * within the next block. pwd can be NULL since it
    348 		 * falls back to the "default" class if it is.
    349 		 */
    350 		lc = login_getclass(pwd ? pwd->pw_class : NULL);
    351 #endif
    352 		/*
    353 		 * if we have a valid account name, and it doesn't have a
    354 		 * password, or the -f option was specified and the caller
    355 		 * is root or the caller isn't changing their uid, don't
    356 		 * authenticate.
    357 		 */
    358 		if (pwd) {
    359 			if (pwd->pw_uid == 0)
    360 				rootlogin = 1;
    361 
    362 			if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
    363 				/* already authenticated */
    364 #ifdef KERBEROS5
    365 				if (login_krb5_get_tickets && Fflag)
    366 					k5_read_creds(username);
    367 #endif
    368 				break;
    369 			} else if (pwd->pw_passwd[0] == '\0') {
    370 				/* pretend password okay */
    371 				rval = 0;
    372 				goto ttycheck;
    373 			}
    374 		}
    375 
    376 		fflag = 0;
    377 
    378 		(void)setpriority(PRIO_PROCESS, 0, -4);
    379 
    380 #ifdef SKEY
    381 		if (skey_haskey(username) == 0) {
    382 			static char skprompt[80];
    383 			const char *skinfo = skey_keyinfo(username);
    384 
    385 			(void)snprintf(skprompt, sizeof(skprompt),
    386 			    "Password [ %s ]:",
    387 			    skinfo ? skinfo : "error getting challenge");
    388 			pwprompt = skprompt;
    389 		} else
    390 #endif
    391 			pwprompt = "Password:";
    392 
    393 		p = getpass(pwprompt);
    394 
    395 		if (pwd == NULL) {
    396 			rval = 1;
    397 			goto skip;
    398 		}
    399 #ifdef KERBEROS5
    400 		if (login_krb5_get_tickets &&
    401 		    k5login(pwd, instance, localhost, p) == 0) {
    402 			rval = 0;
    403 			got_tickets = 1;
    404 		}
    405 #endif
    406 #if defined(KERBEROS5)
    407 		if (got_tickets)
    408 			goto skip;
    409 #endif
    410 #ifdef SKEY
    411 		if (skey_haskey(username) == 0 &&
    412 		    skey_passcheck(username, p) != -1) {
    413 			rval = 0;
    414 			goto skip;
    415 		}
    416 #endif
    417 		if (!sflag && *pwd->pw_passwd != '\0' &&
    418 		    !strcmp(crypt(p, pwd->pw_passwd), pwd->pw_passwd)) {
    419 			rval = 0;
    420 			require_chpass = 1;
    421 			goto skip;
    422 		}
    423 		rval = 1;
    424 
    425 	skip:
    426 		memset(p, 0, strlen(p));
    427 
    428 		(void)setpriority(PRIO_PROCESS, 0, 0);
    429 
    430 	ttycheck:
    431 		/*
    432 		 * If trying to log in as root without Kerberos,
    433 		 * but with insecure terminal, refuse the login attempt.
    434 		 */
    435 		if (pwd && !rval && rootlogin && !rootterm(tty)) {
    436 			(void)printf("Login incorrect or refused on this "
    437 			    "terminal.\n");
    438 			if (hostname)
    439 				syslog(LOG_NOTICE,
    440 				    "LOGIN %s REFUSED FROM %s ON TTY %s",
    441 				    pwd->pw_name, hostname, tty);
    442 			else
    443 				syslog(LOG_NOTICE,
    444 				    "LOGIN %s REFUSED ON TTY %s",
    445 				     pwd->pw_name, tty);
    446 			continue;
    447 		}
    448 
    449 		if (pwd && !rval)
    450 			break;
    451 
    452 		(void)printf("Login incorrect or refused on this "
    453 		    "terminal.\n");
    454 		failures++;
    455 		cnt++;
    456 		/*
    457 		 * We allow login_retries tries, but after login_backoff
    458 		 * we start backing off.  These default to 10 and 3
    459 		 * respectively.
    460 		 */
    461 		if (cnt > login_backoff) {
    462 			if (cnt >= login_retries) {
    463 				badlogin(username);
    464 				sleepexit(EXIT_FAILURE);
    465 			}
    466 			sleep((u_int)((cnt - login_backoff) * 5));
    467 		}
    468 	}
    469 
    470 	/* committed to login -- turn off timeout */
    471 	(void)alarm((u_int)0);
    472 
    473 	endpwent();
    474 
    475 	/* if user not super-user, check for disabled logins */
    476 #ifdef LOGIN_CAP
    477 	if (!login_getcapbool(lc, "ignorenologin", rootlogin))
    478 		checknologin(login_getcapstr(lc, "nologin", NULL, NULL));
    479 #else
    480 	if (!rootlogin)
    481 		checknologin(NULL);
    482 #endif
    483 
    484 #ifdef LOGIN_CAP
    485 	quietlog = login_getcapbool(lc, "hushlogin", 0);
    486 #else
    487 	quietlog = 0;
    488 #endif
    489 	/* Temporarily give up special privileges so we can change */
    490 	/* into NFS-mounted homes that are exported for non-root */
    491 	/* access and have mode 7x0 */
    492 	saved_uid = geteuid();
    493 	saved_gid = getegid();
    494 	nsaved_gids = getgroups(NGROUPS_MAX, saved_gids);
    495 
    496 	(void)setegid(pwd->pw_gid);
    497 	initgroups(username, pwd->pw_gid);
    498 	(void)seteuid(pwd->pw_uid);
    499 
    500 	if (chdir(pwd->pw_dir) < 0) {
    501 #ifdef LOGIN_CAP
    502 		if (login_getcapbool(lc, "requirehome", 0)) {
    503 			(void)printf("Home directory %s required\n",
    504 			    pwd->pw_dir);
    505 			sleepexit(EXIT_FAILURE);
    506 		}
    507 #endif
    508 		(void)printf("No home directory %s!\n", pwd->pw_dir);
    509 		if (chdir("/") == -1)
    510 			exit(EXIT_FAILURE);
    511 		pwd->pw_dir = __UNCONST("/");
    512 		(void)printf("Logging in with home = \"/\".\n");
    513 	}
    514 
    515 	if (!quietlog)
    516 		quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
    517 
    518 	/* regain special privileges */
    519 	(void)seteuid(saved_uid);
    520 	setgroups(nsaved_gids, saved_gids);
    521 	(void)setegid(saved_gid);
    522 
    523 #ifdef LOGIN_CAP
    524 	pw_warntime = login_getcaptime(lc, "password-warn",
    525 		_PASSWORD_WARNDAYS * SECSPERDAY,
    526 		_PASSWORD_WARNDAYS * SECSPERDAY);
    527 #endif
    528 
    529 	(void)gettimeofday(&now, NULL);
    530 	if (pwd->pw_expire) {
    531 		if (now.tv_sec >= pwd->pw_expire) {
    532 			(void)printf("Sorry -- your account has expired.\n");
    533 			sleepexit(EXIT_FAILURE);
    534 		} else if (pwd->pw_expire - now.tv_sec < pw_warntime &&
    535 		    !quietlog)
    536 			(void)printf("Warning: your account expires on %s",
    537 			    ctime(&pwd->pw_expire));
    538 	}
    539 	if (pwd->pw_change) {
    540 		if (pwd->pw_change == _PASSWORD_CHGNOW)
    541 			need_chpass = 1;
    542 		else if (now.tv_sec >= pwd->pw_change) {
    543 			(void)printf("Sorry -- your password has expired.\n");
    544 			sleepexit(EXIT_FAILURE);
    545 		} else if (pwd->pw_change - now.tv_sec < pw_warntime &&
    546 		    !quietlog)
    547 			(void)printf("Warning: your password expires on %s",
    548 			    ctime(&pwd->pw_change));
    549 
    550 	}
    551 	/* Nothing else left to fail -- really log in. */
    552 	update_db(quietlog, rootlogin, fflag);
    553 
    554 	(void)chown(ttyn, pwd->pw_uid,
    555 	    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
    556 
    557 	if (ttyaction(ttyn, "login", pwd->pw_name))
    558 		(void)printf("Warning: ttyaction failed.\n");
    559 
    560 #if defined(KERBEROS5)
    561 	/* Fork so that we can call kdestroy */
    562 	if (! login_krb5_retain_ccache && has_ccache)
    563 		dofork();
    564 #endif
    565 
    566 	/* Destroy environment unless user has requested its preservation. */
    567 	if (!pflag)
    568 		environ = envinit;
    569 
    570 #ifdef LOGIN_CAP
    571 	if (nested == NULL && setusercontext(lc, pwd, pwd->pw_uid,
    572 	    LOGIN_SETLOGIN) != 0) {
    573 		syslog(LOG_ERR, "setusercontext failed");
    574 		exit(EXIT_FAILURE);
    575 	}
    576 	if (setusercontext(lc, pwd, pwd->pw_uid,
    577 	    (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETLOGIN))) != 0) {
    578 		syslog(LOG_ERR, "setusercontext failed");
    579 		exit(EXIT_FAILURE);
    580 	}
    581 #else
    582 	(void)setgid(pwd->pw_gid);
    583 
    584 	initgroups(username, pwd->pw_gid);
    585 
    586 	if (nested == NULL && setlogin(pwd->pw_name) < 0)
    587 		syslog(LOG_ERR, "setlogin() failure: %m");
    588 
    589 	/* Discard permissions last so can't get killed and drop core. */
    590 	if (rootlogin)
    591 		(void)setuid(0);
    592 	else
    593 		(void)setuid(pwd->pw_uid);
    594 #endif
    595 
    596 	if (*pwd->pw_shell == '\0')
    597 		pwd->pw_shell = __UNCONST(_PATH_BSHELL);
    598 #ifdef LOGIN_CAP
    599 	if ((shell = login_getcapstr(lc, "shell", NULL, NULL)) != NULL) {
    600 		if ((shell = strdup(shell)) == NULL) {
    601 			syslog(LOG_ERR, "Cannot alloc mem");
    602 			sleepexit(EXIT_FAILURE);
    603 		}
    604 		pwd->pw_shell = shell;
    605 	}
    606 #endif
    607 
    608 	(void)setenv("HOME", pwd->pw_dir, 1);
    609 	(void)setenv("SHELL", pwd->pw_shell, 1);
    610 	if (term[0] == '\0') {
    611 		const char *tt = stypeof(tty);
    612 #ifdef LOGIN_CAP
    613 		if (tt == NULL)
    614 			tt = login_getcapstr(lc, "term", NULL, NULL);
    615 #endif
    616 		/* unknown term -> "su" */
    617 		(void)strlcpy(term, tt != NULL ? tt : "su", sizeof(term));
    618 	}
    619 	(void)setenv("TERM", term, 0);
    620 	(void)setenv("LOGNAME", pwd->pw_name, 1);
    621 	(void)setenv("USER", pwd->pw_name, 1);
    622 
    623 #ifdef LOGIN_CAP
    624 	setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH);
    625 #else
    626 	(void)setenv("PATH", _PATH_DEFPATH, 0);
    627 #endif
    628 
    629 #ifdef KERBEROS5
    630 	if (krb5tkfile_env)
    631 		(void)setenv("KRB5CCNAME", krb5tkfile_env, 1);
    632 #endif
    633 
    634 	if (tty[sizeof("tty")-1] == 'd')
    635 		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
    636 
    637 	/* If fflag is on, assume caller/authenticator has logged root login. */
    638 	if (rootlogin && fflag == 0) {
    639 		if (hostname)
    640 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
    641 			    username, tty, hostname);
    642 		else
    643 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
    644 			    username, tty);
    645 	}
    646 
    647 #if defined(KERBEROS5)
    648 	if (KERBEROS_CONFIGURED && !quietlog && notickets == 1)
    649 		(void)printf("Warning: no Kerberos tickets issued.\n");
    650 #endif
    651 
    652 	if (!quietlog) {
    653 		const char *fname;
    654 #ifdef LOGIN_CAP
    655 		fname = login_getcapstr(lc, "copyright", NULL, NULL);
    656 		if (fname != NULL && access(fname, F_OK) == 0)
    657 			motd(fname);
    658 		else
    659 #endif
    660 			(void)printf("%s", copyrightstr);
    661 
    662 #ifdef LOGIN_CAP
    663 		fname = login_getcapstr(lc, "welcome", NULL, NULL);
    664 		if (fname == NULL || access(fname, F_OK) != 0)
    665 #endif
    666 			fname = _PATH_MOTDFILE;
    667 		motd(fname);
    668 
    669 		(void)snprintf(tbuf,
    670 		    sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
    671 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
    672 			(void)printf("You have %smail.\n",
    673 			    (st.st_mtime > st.st_atime) ? "new " : "");
    674 	}
    675 
    676 #ifdef LOGIN_CAP
    677 	login_close(lc);
    678 #endif
    679 
    680 	(void)signal(SIGALRM, SIG_DFL);
    681 	(void)signal(SIGQUIT, SIG_DFL);
    682 	(void)signal(SIGINT, SIG_DFL);
    683 	(void)signal(SIGTSTP, SIG_IGN);
    684 
    685 	tbuf[0] = '-';
    686 	(void)strlcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ?
    687 	    p + 1 : pwd->pw_shell, sizeof(tbuf) - 1);
    688 
    689 	/* Wait to change password until we're unprivileged */
    690 	if (need_chpass) {
    691 		if (!require_chpass)
    692 			(void)printf(
    693 "Warning: your password has expired. Please change it as soon as possible.\n");
    694 		else {
    695 			int	status;
    696 
    697 			(void)printf(
    698 		    "Your password has expired. Please choose a new one.\n");
    699 			switch (fork()) {
    700 			case -1:
    701 				warn("fork");
    702 				sleepexit(EXIT_FAILURE);
    703 			case 0:
    704 				execl(_PATH_BINPASSWD, "passwd", NULL);
    705 				_exit(EXIT_FAILURE);
    706 			default:
    707 				if (wait(&status) == -1 ||
    708 				    WEXITSTATUS(status))
    709 					sleepexit(EXIT_FAILURE);
    710 			}
    711 		}
    712 	}
    713 
    714 #ifdef KERBEROS5
    715 	if (login_krb5_get_tickets)
    716 		k5_write_creds();
    717 #endif
    718 	execlp(pwd->pw_shell, tbuf, NULL);
    719 	err(EXIT_FAILURE, "%s", pwd->pw_shell);
    720 }
    721 
    722 #if defined(KERBEROS5)
    723 /*
    724  * This routine handles cleanup stuff, and the like.
    725  * It exists only in the child process.
    726  */
    727 static void
    728 dofork(void)
    729 {
    730 	pid_t child, wchild;
    731 
    732 	switch (child = fork()) {
    733 	case 0:
    734 		return; /* Child process */
    735 	case -1:
    736 		err(EXIT_FAILURE, "Can't fork");
    737 		/*NOTREACHED*/
    738 	default:
    739 		break;
    740 	}
    741 
    742 	/*
    743 	 * Setup stuff?  This would be things we could do in parallel
    744 	 * with login
    745 	 */
    746 	if (chdir("/") == -1)	/* Let's not keep the fs busy... */
    747 		err(EXIT_FAILURE, "Can't chdir to `/'");
    748 
    749 	/* If we're the parent, watch the child until it dies */
    750 	while ((wchild = wait(NULL)) != child)
    751 		if (wchild == -1)
    752 			err(EXIT_FAILURE, "Can't wait");
    753 
    754 	/* Cleanup stuff */
    755 	/* Run kdestroy to destroy tickets */
    756 	if (login_krb5_get_tickets)
    757 		k5destroy();
    758 
    759 	/* Leave */
    760 	exit(EXIT_SUCCESS);
    761 }
    762 #endif
    763 
    764 static void
    765 checknologin(char *fname)
    766 {
    767 	int fd, nchars;
    768 	char tbuf[8192];
    769 
    770 	if ((fd = open(fname ? fname : _PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
    771 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
    772 			(void)write(fileno(stdout), tbuf, nchars);
    773 		sleepexit(EXIT_SUCCESS);
    774 	}
    775 }
    776 
    777 static void
    778 usage(void)
    779 {
    780 	(void)fprintf(stderr,
    781 	    "Usage: %s [-Ffps] [-a address] [-h hostname] [username]\n",
    782 	    getprogname());
    783 	exit(EXIT_FAILURE);
    784 }
    785