Home | History | Annotate | Line # | Download | only in login
login.c revision 1.24
      1 /*	$NetBSD: login.c,v 1.24 1997/08/16 13:50:46 lukem 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT(
     39 "@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\
     40 	The Regents of the University of California.  All rights reserved.\n");
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 #if 0
     45 static char sccsid[] = "@(#)login.c	8.4 (Berkeley) 4/2/94";
     46 #endif
     47 __RCSID("$NetBSD: login.c,v 1.24 1997/08/16 13:50:46 lukem Exp $");
     48 #endif /* not lint */
     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/param.h>
     57 #include <sys/stat.h>
     58 #include <sys/time.h>
     59 #include <sys/resource.h>
     60 #include <sys/file.h>
     61 
     62 #include <err.h>
     63 #include <errno.h>
     64 #include <grp.h>
     65 #include <pwd.h>
     66 #include <setjmp.h>
     67 #include <signal.h>
     68 #include <stdio.h>
     69 #include <skey.h>
     70 #include <stdlib.h>
     71 #include <string.h>
     72 #include <syslog.h>
     73 #include <ttyent.h>
     74 #include <tzfile.h>
     75 #include <unistd.h>
     76 #include <utmp.h>
     77 #include <util.h>
     78 
     79 #include "pathnames.h"
     80 
     81 void	 badlogin __P((char *));
     82 void	 checknologin __P((void));
     83 void	 dolastlog __P((int));
     84 void	 getloginname __P((void));
     85 int	 main __P((int, char *[]));
     86 void	 motd __P((void));
     87 int	 rootterm __P((char *));
     88 void	 sigint __P((int));
     89 void	 sleepexit __P((int));
     90 char	*stypeof __P((char *));
     91 void	 timedout __P((int));
     92 int	 pwcheck __P((char *, char *, char *, char *));
     93 #if defined(KERBEROS) || defined(KERBEROS5)
     94 int	 klogin __P((struct passwd *, char *, char *, char *));
     95 void	 kdestroy __P((void));
     96 void	 dofork __P((void));
     97 #endif
     98 
     99 extern void login __P((struct utmp *));
    100 
    101 #define	TTYGRPNAME	"tty"		/* name of group to own ttys */
    102 
    103 /*
    104  * This bounds the time given to login.  Not a define so it can
    105  * be patched on machines where it's too small.
    106  */
    107 u_int	timeout = 300;
    108 
    109 #if defined(KERBEROS) || defined(KERBEROS5)
    110 int	notickets = 1;
    111 char	*instance;
    112 char	*krbtkfile_env;
    113 int	authok;
    114 #endif
    115 
    116 struct	passwd *pwd;
    117 int	failures;
    118 char	term[64], *envinit[1], *hostname, *username, *tty;
    119 #ifdef SKEY
    120 int	used_skey = 0;
    121 int 	require_skey = 0;
    122 char	skeypw[] = "s/key";
    123 #endif
    124 
    125 int
    126 main(argc, argv)
    127 	int argc;
    128 	char *argv[];
    129 {
    130 	extern char **environ;
    131 	struct group *gr;
    132 	struct stat st;
    133 	struct timeval tp;
    134 	struct utmp utmp;
    135 	int ask, ch, cnt, fflag, hflag, pflag, quietlog, rootlogin, rval;
    136 	uid_t uid;
    137 	char *domain, *p, *salt, *ttyn, *pwprompt;
    138 	char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
    139 	char localhost[MAXHOSTNAMELEN];
    140 	int need_chpass;
    141 
    142 	tbuf[0] = '\0';
    143 	rval = 0;
    144 	pwprompt = NULL;
    145 	need_chpass = 0;
    146 
    147 	(void)signal(SIGALRM, timedout);
    148 	(void)alarm(timeout);
    149 	(void)signal(SIGQUIT, SIG_IGN);
    150 	(void)signal(SIGINT, SIG_IGN);
    151 	(void)setpriority(PRIO_PROCESS, 0, 0);
    152 
    153 	openlog("login", LOG_ODELAY, LOG_AUTH);
    154 
    155 	/*
    156 	 * -p is used by getty to tell login not to destroy the environment
    157 	 * -f is used to skip a second login authentication
    158 	 * -h is used by other servers to pass the name of the remote
    159 	 *    host to login so that it may be placed in utmp and wtmp
    160 	 * -s is used to force use of S/Key or equivalent.
    161 	 */
    162 	domain = NULL;
    163 	if (gethostname(localhost, sizeof(localhost)) < 0)
    164 		syslog(LOG_ERR, "couldn't get local hostname: %m");
    165 	else
    166 		domain = strchr(localhost, '.');
    167 
    168 	fflag = hflag = pflag = 0;
    169 	uid = getuid();
    170 	while ((ch = getopt(argc, argv, "fh:ps")) != -1)
    171 		switch (ch) {
    172 		case 'f':
    173 			fflag = 1;
    174 			break;
    175 		case 'h':
    176 			if (uid)
    177 				errx(1, "-h option: %s", strerror(EPERM));
    178 			hflag = 1;
    179 			if (domain && (p = strchr(optarg, '.')) != NULL &&
    180 			    strcasecmp(p, domain) == 0)
    181 				*p = 0;
    182 			hostname = optarg;
    183 			break;
    184 		case 'p':
    185 			pflag = 1;
    186 			break;
    187 		case 's':
    188 #ifdef SKEY
    189 			/*
    190 			 * If -s given twice, use S/Key or no login
    191 			 * otherwise just insist on S/Key if user has one.
    192 			 */
    193 			require_skey++;
    194 #endif
    195 			break;
    196 		case '?':
    197 		default:
    198 			if (!uid)
    199 				syslog(LOG_ERR, "invalid flag %c", ch);
    200 			(void)fprintf(stderr,
    201 			    "usage: login [-fp] [-h hostname] [username]\n");
    202 			exit(1);
    203 		}
    204 	argc -= optind;
    205 	argv += optind;
    206 
    207 	if (*argv) {
    208 		username = *argv;
    209 		ask = 0;
    210 	} else
    211 		ask = 1;
    212 
    213 	for (cnt = getdtablesize(); cnt > 2; cnt--)
    214 		(void)close(cnt);
    215 
    216 	ttyn = ttyname(STDIN_FILENO);
    217 	if (ttyn == NULL || *ttyn == '\0') {
    218 		(void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
    219 		ttyn = tname;
    220 	}
    221 	if ((tty = strrchr(ttyn, '/')) != NULL)
    222 		++tty;
    223 	else
    224 		tty = ttyn;
    225 
    226 	for (cnt = 0;; ask = 1) {
    227 #ifdef SKEY
    228 		used_skey = 0;
    229 #endif
    230 #if defined(KERBEROS) || defined(KERBEROS5)
    231 	        kdestroy();
    232 #endif
    233 		if (ask) {
    234 			fflag = 0;
    235 			getloginname();
    236 		}
    237 		rootlogin = 0;
    238 #ifdef	KERBEROS
    239 		if ((instance = strchr(username, '.')) != NULL) {
    240 			if (strncmp(instance, ".root", 5) == 0)
    241 				rootlogin = 1;
    242 			*instance++ = '\0';
    243 		} else
    244 			instance = "";
    245 #endif
    246 #ifdef KERBEROS5
    247 		if ((instance = strchr(username, '/')) != NULL) {
    248 			if (strncmp(instance, "/root", 5) == 0)
    249 				rootlogin = 1;
    250 			*instance++ = '\0';
    251 		} else
    252 			instance = "";
    253 #endif
    254 		if (strlen(username) > MAXLOGNAME)
    255 			username[MAXLOGNAME] = '\0';
    256 
    257 		/*
    258 		 * Note if trying multiple user names; log failures for
    259 		 * previous user name, but don't bother logging one failure
    260 		 * for nonexistent name (mistyped username).
    261 		 */
    262 		if (failures && strcmp(tbuf, username)) {
    263 			if (failures > (pwd ? 0 : 1))
    264 				badlogin(tbuf);
    265 			failures = 0;
    266 		}
    267 		(void)strncpy(tbuf, username, sizeof(tbuf) - 1);
    268 
    269 		if ((pwd = getpwnam(username)) != NULL)
    270 			salt = pwd->pw_passwd;
    271 		else
    272 			salt = "xx";
    273 
    274 		/*
    275 		 * if we have a valid account name, and it doesn't have a
    276 		 * password, or the -f option was specified and the caller
    277 		 * is root or the caller isn't changing their uid, don't
    278 		 * authenticate.
    279 		 */
    280 		if (pwd) {
    281 			if (pwd->pw_uid == 0)
    282 				rootlogin = 1;
    283 
    284 			if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
    285 				/* already authenticated */
    286 				break;
    287 			} else if (pwd->pw_passwd[0] == '\0') {
    288 				/* pretend password okay */
    289 				rval = 0;
    290 				goto ttycheck;
    291 			}
    292 		}
    293 
    294 		fflag = 0;
    295 
    296 		(void)setpriority(PRIO_PROCESS, 0, -4);
    297 
    298 #ifdef SKEY
    299 		p = NULL;
    300 		pwprompt = "Password:";
    301 
    302 		if (require_skey > 1)		/* -s -s */
    303 			p = skeypw;
    304 		else if (skey_haskey(username) == 0) {
    305 			if (require_skey > 0)	/* -s */
    306 				p = skeypw;
    307 			else {
    308 				static char skprompt[80];
    309 				char *skinfo = skey_keyinfo(username);
    310 
    311 				(void)snprintf(skprompt, sizeof(skprompt)-1,
    312 				    "Password [%s]:", skinfo);
    313 				pwprompt = skprompt;
    314 			}
    315 		}
    316 		if (p == NULL)
    317 #endif
    318 			p = getpass(pwprompt);
    319 
    320 		if (pwd) {
    321 #if defined(KERBEROS) || defined(KERBEROS5)
    322 			rval = klogin(pwd, instance, localhost, p);
    323 			if (rval != 0 && rootlogin && pwd->pw_uid != 0)
    324 				rootlogin = 0;
    325 			if (rval == 0)
    326 				authok = 1;
    327 			else if (rval == 1) {
    328 				if (pwd->pw_uid != 0)
    329 					rootlogin = 0;
    330 				rval = pwcheck(username, p, salt, pwd->pw_passwd);
    331 			}
    332 #else
    333 			rval = pwcheck(username, p, salt, pwd->pw_passwd);
    334 #endif
    335 		}
    336 #ifdef SKEY
    337 		if (used_skey == 0 && p != skeypw)
    338 #endif
    339 			memset(p, 0, strlen(p));
    340 
    341 		(void)setpriority(PRIO_PROCESS, 0, 0);
    342 
    343 	ttycheck:
    344 		/*
    345 		 * If trying to log in as root without Kerberos,
    346 		 * but with insecure terminal, refuse the login attempt.
    347 		 */
    348 #if defined(KERBEROS) || defined(KERBEROS5)
    349 		if (authok == 0)
    350 #endif
    351 		if (pwd && !rval && rootlogin && !rootterm(tty)) {
    352 			(void)fprintf(stderr,
    353 			    "%s login refused on this terminal.\n",
    354 			    pwd->pw_name);
    355 			if (hostname)
    356 				syslog(LOG_NOTICE,
    357 				    "LOGIN %s REFUSED FROM %s ON TTY %s",
    358 				    pwd->pw_name, hostname, tty);
    359 			else
    360 				syslog(LOG_NOTICE,
    361 				    "LOGIN %s REFUSED ON TTY %s",
    362 				     pwd->pw_name, tty);
    363 			continue;
    364 		}
    365 
    366 		if (pwd && !rval)
    367 			break;
    368 
    369 		(void)printf("Login incorrect\n");
    370 		failures++;
    371 		/* we allow 10 tries, but after 3 we start backing off */
    372 		if (++cnt > 3) {
    373 			if (cnt >= 10) {
    374 				badlogin(username);
    375 				sleepexit(1);
    376 			}
    377 			sleep((u_int)((cnt - 3) * 5));
    378 		}
    379 	}
    380 
    381 	/* committed to login -- turn off timeout */
    382 	(void)alarm((u_int)0);
    383 
    384 	endpwent();
    385 
    386 	/* if user not super-user, check for disabled logins */
    387 	if (!rootlogin)
    388 		checknologin();
    389 
    390 	if (chdir(pwd->pw_dir) < 0) {
    391 		(void)printf("No home directory %s!\n", pwd->pw_dir);
    392 		if (chdir("/"))
    393 			exit(0);
    394 		pwd->pw_dir = "/";
    395 		(void)printf("Logging in with home = \"/\".\n");
    396 	}
    397 
    398 	quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
    399 
    400 	if (pwd->pw_change || pwd->pw_expire)
    401 		(void)gettimeofday(&tp, (struct timezone *)NULL);
    402 	if (pwd->pw_change)
    403 		if (pwd->pw_change == _PASSWORD_CHGNOW)
    404 			need_chpass = 1;
    405 		else if (tp.tv_sec >= pwd->pw_change) {
    406 			(void)printf("Sorry -- your password has expired.\n");
    407 			sleepexit(1);
    408 		} else if (pwd->pw_change - tp.tv_sec <
    409 		    _PASSWORD_WARNDAYS * SECSPERDAY && !quietlog)
    410 			(void)printf("Warning: your password expires on %s",
    411 			    ctime(&pwd->pw_change));
    412 	if (pwd->pw_expire)
    413 		if (tp.tv_sec >= pwd->pw_expire) {
    414 			(void)printf("Sorry -- your account has expired.\n");
    415 			sleepexit(1);
    416 		} else if (pwd->pw_expire - tp.tv_sec <
    417 		    _PASSWORD_WARNDAYS * SECSPERDAY && !quietlog)
    418 			(void)printf("Warning: your account expires on %s",
    419 			    ctime(&pwd->pw_expire));
    420 
    421 	/* Nothing else left to fail -- really log in. */
    422 	memset((void *)&utmp, 0, sizeof(utmp));
    423 	(void)time(&utmp.ut_time);
    424 	(void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
    425 	if (hostname)
    426 		(void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
    427 	(void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
    428 	login(&utmp);
    429 
    430 	dolastlog(quietlog);
    431 
    432 	(void)chown(ttyn, pwd->pw_uid,
    433 	    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
    434 
    435 	if (ttyaction(ttyn, "login", pwd->pw_name))
    436 		(void)printf("Warning: ttyaction failed.\n");
    437 
    438 #if defined(KERBEROS) || defined(KERBEROS5)
    439 	/* Fork so that we can call kdestroy */
    440 	if (krbtkfile_env)
    441 	    dofork();
    442 #endif
    443 	(void)setgid(pwd->pw_gid);
    444 
    445 	initgroups(username, pwd->pw_gid);
    446 
    447 	if (*pwd->pw_shell == '\0')
    448 		pwd->pw_shell = _PATH_BSHELL;
    449 
    450 	/* Destroy environment unless user has requested its preservation. */
    451 	if (!pflag)
    452 		environ = envinit;
    453 	(void)setenv("HOME", pwd->pw_dir, 1);
    454 	(void)setenv("SHELL", pwd->pw_shell, 1);
    455 	if (term[0] == '\0')
    456 		(void)strncpy(term, stypeof(tty), sizeof(term));
    457 	(void)setenv("TERM", term, 0);
    458 	(void)setenv("LOGNAME", pwd->pw_name, 1);
    459 	(void)setenv("USER", pwd->pw_name, 1);
    460 	(void)setenv("PATH", _PATH_DEFPATH, 0);
    461 #ifdef KERBEROS
    462 	if (krbtkfile_env)
    463 		(void)setenv("KRBTKFILE", krbtkfile_env, 1);
    464 #endif
    465 #ifdef KERBEROS5
    466 	if (krbtkfile_env)
    467 		(void)setenv("KRB5CCNAME", krbtkfile_env, 1);
    468 #endif
    469 
    470 	if (tty[sizeof("tty")-1] == 'd')
    471 		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
    472 
    473 	/* If fflag is on, assume caller/authenticator has logged root login. */
    474 	if (rootlogin && fflag == 0)
    475 		if (hostname)
    476 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
    477 			    username, tty, hostname);
    478 		else
    479 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty);
    480 
    481 #if defined(KERBEROS) || defined(KERBEROS5)
    482 	if (!quietlog && notickets == 1)
    483 		(void)printf("Warning: no Kerberos tickets issued.\n");
    484 #endif
    485 
    486 	if (!quietlog) {
    487 		(void)printf("%s\n\t%s  %s\n\n",
    488 	    "Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
    489 		    "The Regents of the University of California. ",
    490 		    "All rights reserved.");
    491 		motd();
    492 		(void)snprintf(tbuf,
    493 		    sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
    494 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
    495 			(void)printf("You have %smail.\n",
    496 			    (st.st_mtime > st.st_atime) ? "new " : "");
    497 	}
    498 
    499 	(void)signal(SIGALRM, SIG_DFL);
    500 	(void)signal(SIGQUIT, SIG_DFL);
    501 	(void)signal(SIGINT, SIG_DFL);
    502 	(void)signal(SIGTSTP, SIG_IGN);
    503 
    504 	tbuf[0] = '-';
    505 	(void)strncpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ?
    506 	    p + 1 : pwd->pw_shell, sizeof(tbuf) - 2);
    507 
    508 	if (setlogin(pwd->pw_name) < 0)
    509 		syslog(LOG_ERR, "setlogin() failure: %m");
    510 
    511 	/* Discard permissions last so can't get killed and drop core. */
    512 	if (rootlogin)
    513 		(void)setuid(0);
    514 	else
    515 		(void)setuid(pwd->pw_uid);
    516 
    517 	/* Wait to change password until we're unprivileged */
    518 	if (need_chpass) {
    519 #ifdef SKEY
    520 		/* If the user logged on using S/Key, don't force
    521 		 * a password change
    522 		 */
    523 		if (used_skey) {
    524 			(void)printf(
    525 "Warning: your password has expired. Please change it as soon as possible.\n");
    526 		} else
    527 #endif
    528 		(void)printf(
    529 		    "Your password has expired. Please choose a new one.\n");
    530 		if (system(_PATH_BINPASSWD) != 0)
    531 			sleepexit(1);
    532 	}
    533 
    534 	execlp(pwd->pw_shell, tbuf, 0);
    535 	err(1, "%s", pwd->pw_shell);
    536 }
    537 
    538 int
    539 pwcheck(user, p, salt, passwd)
    540 	char *user, *p, *salt, *passwd;
    541 {
    542 	int sts = strcmp(crypt(p, salt), passwd);
    543 
    544 #ifdef SKEY
    545 	if (sts) {				/* wasn't passwd */
    546 		if (skey_haskey(user)) {
    547 			if (strcasecmp(p, skeypw) == 0)
    548 				return 1;
    549 		} else {
    550 			if (strcasecmp(p, skeypw) == 0) {
    551 				sts = skey_authenticate(user);
    552 			} else {
    553 				if ((sts = skey_passcheck(user, p)) != -1) {
    554 					sts = 0; /* ok */
    555 				}
    556 			}
    557 		}
    558 		if (sts == 0)
    559 			used_skey = 1;
    560 	}
    561 #endif
    562 	return sts;
    563 }
    564 
    565 #if defined(KERBEROS) || defined(KERBEROS5)
    566 #define	NBUFSIZ		(MAXLOGNAME + 1 + 5)	/* .root suffix */
    567 #else
    568 #define	NBUFSIZ		(MAXLOGNAME + 1)
    569 #endif
    570 
    571 #if defined(KERBEROS) || defined(KERBEROS5)
    572 /*
    573  * This routine handles cleanup stuff, and the like.
    574  * It exists only in the child process.
    575  */
    576 #include <sys/wait.h>
    577 void
    578 dofork()
    579 {
    580     int child;
    581 
    582     if (!(child = fork()))
    583 	    return; /* Child process */
    584 
    585     /* Setup stuff?  This would be things we could do in parallel with login */
    586     (void) chdir("/");	/* Let's not keep the fs busy... */
    587 
    588     /* If we're the parent, watch the child until it dies */
    589     while (wait(0) != child)
    590 	    ;
    591 
    592     /* Cleanup stuff */
    593     /* Run kdestroy to destroy tickets */
    594     kdestroy();
    595 
    596     /* Leave */
    597     exit(0);
    598 }
    599 #endif
    600 
    601 void
    602 getloginname()
    603 {
    604 	int ch;
    605 	char *p;
    606 	static char nbuf[NBUFSIZ];
    607 
    608 	for (;;) {
    609 		(void)printf("login: ");
    610 		for (p = nbuf; (ch = getchar()) != '\n'; ) {
    611 			if (ch == EOF) {
    612 				badlogin(username);
    613 				exit(0);
    614 			}
    615 			if (p < nbuf + (NBUFSIZ - 1))
    616 				*p++ = ch;
    617 		}
    618 		if (p > nbuf)
    619 			if (nbuf[0] == '-')
    620 				(void)fprintf(stderr,
    621 				    "login names may not start with '-'.\n");
    622 			else {
    623 				*p = '\0';
    624 				username = nbuf;
    625 				break;
    626 			}
    627 	}
    628 }
    629 
    630 int
    631 rootterm(ttyn)
    632 	char *ttyn;
    633 {
    634 	struct ttyent *t;
    635 
    636 	return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
    637 }
    638 
    639 jmp_buf motdinterrupt;
    640 
    641 void
    642 motd()
    643 {
    644 	int fd, nchars;
    645 	sig_t oldint;
    646 	char tbuf[8192];
    647 
    648 	if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0)
    649 		return;
    650 	oldint = signal(SIGINT, sigint);
    651 	if (setjmp(motdinterrupt) == 0)
    652 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
    653 			(void)write(fileno(stdout), tbuf, nchars);
    654 	(void)signal(SIGINT, oldint);
    655 	(void)close(fd);
    656 }
    657 
    658 /* ARGSUSED */
    659 void
    660 sigint(signo)
    661 	int signo;
    662 {
    663 	longjmp(motdinterrupt, 1);
    664 }
    665 
    666 /* ARGSUSED */
    667 void
    668 timedout(signo)
    669 	int signo;
    670 {
    671 	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
    672 	exit(0);
    673 }
    674 
    675 void
    676 checknologin()
    677 {
    678 	int fd, nchars;
    679 	char tbuf[8192];
    680 
    681 	if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
    682 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
    683 			(void)write(fileno(stdout), tbuf, nchars);
    684 		sleepexit(0);
    685 	}
    686 }
    687 
    688 void
    689 dolastlog(quiet)
    690 	int quiet;
    691 {
    692 	struct lastlog ll;
    693 	int fd;
    694 
    695 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
    696 		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
    697 		if (!quiet) {
    698 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
    699 			    ll.ll_time != 0) {
    700 				(void)printf("Last login: %.*s ",
    701 				    24-5, (char *)ctime(&ll.ll_time));
    702 				if (*ll.ll_host != '\0')
    703 					(void)printf("from %.*s\n",
    704 					    (int)sizeof(ll.ll_host),
    705 					    ll.ll_host);
    706 				else
    707 					(void)printf("on %.*s\n",
    708 					    (int)sizeof(ll.ll_line),
    709 					    ll.ll_line);
    710 			}
    711 			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
    712 		}
    713 		memset((void *)&ll, 0, sizeof(ll));
    714 		(void)time(&ll.ll_time);
    715 		(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
    716 		if (hostname)
    717 			(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
    718 		(void)write(fd, (char *)&ll, sizeof(ll));
    719 		(void)close(fd);
    720 	}
    721 }
    722 
    723 void
    724 badlogin(name)
    725 	char *name;
    726 {
    727 	if (failures == 0)
    728 		return;
    729 	if (hostname) {
    730 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
    731 		    failures, failures > 1 ? "S" : "", hostname);
    732 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
    733 		    "%d LOGIN FAILURE%s FROM %s, %s",
    734 		    failures, failures > 1 ? "S" : "", hostname, name);
    735 	} else {
    736 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
    737 		    failures, failures > 1 ? "S" : "", tty);
    738 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
    739 		    "%d LOGIN FAILURE%s ON %s, %s",
    740 		    failures, failures > 1 ? "S" : "", tty, name);
    741 	}
    742 }
    743 
    744 #undef	UNKNOWN
    745 #define	UNKNOWN	"su"
    746 
    747 char *
    748 stypeof(ttyid)
    749 	char *ttyid;
    750 {
    751 	struct ttyent *t;
    752 
    753 	return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
    754 }
    755 
    756 void
    757 sleepexit(eval)
    758 	int eval;
    759 {
    760 	(void)sleep(5);
    761 	exit(eval);
    762 }
    763