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