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