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