xref: /csrg-svn/usr.bin/login/login.c (revision 43659)
143653Sbostic /*-
236515Sbostic  * Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
336515Sbostic  * All rights reserved.
436515Sbostic  *
543653Sbostic  * %sccs.include.redist.c%
619843Sdist  */
719843Sdist 
812678Ssam #ifndef lint
919843Sdist char copyright[] =
1036515Sbostic "@(#) Copyright (c) 1980, 1987, 1988 The Regents of the University of California.\n\
1119843Sdist  All rights reserved.\n";
1236515Sbostic #endif /* not lint */
1312678Ssam 
1419843Sdist #ifndef lint
15*43659Sbostic static char sccsid[] = "@(#)login.c	5.57 (Berkeley) 06/24/90";
1636515Sbostic #endif /* not lint */
1719843Sdist 
181043Sbill /*
191043Sbill  * login [ name ]
2031695Skarels  * login -h hostname	(for telnetd, etc.)
2131695Skarels  * login -f name	(for pre-authenticated login: datakit, xterm, etc.)
221043Sbill  */
231043Sbill 
2412984Ssam #include <sys/param.h>
2512687Ssam #include <sys/stat.h>
2612687Ssam #include <sys/time.h>
2712687Ssam #include <sys/resource.h>
2816453Sroot #include <sys/file.h>
2939271Skfall #include <sgtty.h>
3012687Ssam 
311043Sbill #include <utmp.h>
321043Sbill #include <signal.h>
3312678Ssam #include <errno.h>
3416453Sroot #include <ttyent.h>
3516453Sroot #include <syslog.h>
3626862Smckusick #include <grp.h>
3736460Sbostic #include <pwd.h>
3836515Sbostic #include <setjmp.h>
3936460Sbostic #include <stdio.h>
4042063Sbostic #include <string.h>
4137203Sbostic #include <tzfile.h>
4237203Sbostic #include "pathnames.h"
431043Sbill 
4436460Sbostic #define	TTYGRPNAME	"tty"		/* name of group to own ttys */
4526862Smckusick 
4612687Ssam /*
4736460Sbostic  * This bounds the time given to login.  Not a define so it can
4836460Sbostic  * be patched on machines where it's too small.
4912687Ssam  */
5031509Sbostic int	timeout = 300;
5143653Sbostic #ifdef KERBEROS
5243653Sbostic int	notickets = 1;
5343653Sbostic #endif
546005Swnj 
5536647Skarels struct	passwd *pwd;
5636647Skarels int	failures;
5740490Sbostic char	term[64], *envinit[1], *hostname, *username, *tty;
586005Swnj 
5936647Skarels struct	sgttyb sgttyb;
6036647Skarels struct	tchars tc = {
6113074Ssam 	CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
621365Sbill };
6336647Skarels struct	ltchars ltc = {
6443653Sbostic 	CSUSP, CDSUSP, CRPRNT, CDISCARD, CWERASE, CLNEXT
6513074Ssam };
661365Sbill 
6736880Sbostic char *months[] =
6836880Sbostic 	{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
6936880Sbostic 	  "Sep", "Oct", "Nov", "Dec" };
7036880Sbostic 
711043Sbill main(argc, argv)
7236460Sbostic 	int argc;
7336460Sbostic 	char **argv;
741043Sbill {
7543653Sbostic 	extern int optind;
7636460Sbostic 	extern char *optarg, **environ;
7736880Sbostic 	struct timeval tp;
7836880Sbostic 	struct tm *ttp;
7936460Sbostic 	struct group *gr;
8036460Sbostic 	register int ch;
8136647Skarels 	register char *p;
8239271Skfall 	int ask, fflag, hflag, pflag, cnt, uid;
8343653Sbostic 	int quietlog, passwd_req, ioctlval, rval;
8443653Sbostic 	char *domain, *salt, *ttyn;
8537692Sbostic 	char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
8638034Skfall 	char localhost[MAXHOSTNAMELEN];
8736880Sbostic 	char *ctime(), *ttyname(), *stypeof(), *crypt(), *getpass();
8836460Sbostic 	time_t time();
8936460Sbostic 	off_t lseek();
9043653Sbostic 	void timedout();
911043Sbill 
9236460Sbostic 	(void)signal(SIGALRM, timedout);
9336460Sbostic 	(void)alarm((u_int)timeout);
9436460Sbostic 	(void)signal(SIGQUIT, SIG_IGN);
9536460Sbostic 	(void)signal(SIGINT, SIG_IGN);
9636460Sbostic 	(void)setpriority(PRIO_PROCESS, 0, 0);
9736460Sbostic 
9842502Sbostic 	openlog("login", LOG_ODELAY, LOG_AUTH);
9942502Sbostic 
10012687Ssam 	/*
10118549Ssam 	 * -p is used by getty to tell login not to destroy the environment
10231695Skarels  	 * -f is used to skip a second login authentication
10336523Skarels 	 * -h is used by other servers to pass the name of the remote
10436523Skarels 	 *    host to login so that it may be placed in utmp and wtmp
10512687Ssam 	 */
10638034Skfall 	domain = NULL;
10738034Skfall 	if (gethostname(localhost, sizeof(localhost)) < 0)
10838034Skfall 		syslog(LOG_ERR, "couldn't get local hostname: %m");
10938034Skfall 	else
11038034Skfall 		domain = index(localhost, '.');
11136460Sbostic 
11237203Sbostic 	fflag = hflag = pflag = 0;
11336460Sbostic 	passwd_req = 1;
11439271Skfall 	uid = getuid();
11537203Sbostic 	while ((ch = getopt(argc, argv, "fh:p")) != EOF)
11636515Sbostic 		switch (ch) {
11736460Sbostic 		case 'f':
11836460Sbostic 			fflag = 1;
11936460Sbostic 			break;
12036460Sbostic 		case 'h':
12139271Skfall 			if (uid) {
12237203Sbostic 				(void)fprintf(stderr,
12336460Sbostic 				    "login: -h for super-user only.\n");
12432313Sbostic 				exit(1);
12531695Skarels 			}
12636460Sbostic 			hflag = 1;
12736460Sbostic 			if (domain && (p = index(optarg, '.')) &&
12836553Sbostic 			    strcasecmp(p, domain) == 0)
12936460Sbostic 				*p = 0;
13036460Sbostic 			hostname = optarg;
13136460Sbostic 			break;
13236460Sbostic 		case 'p':
13318549Ssam 			pflag = 1;
13436460Sbostic 			break;
13536460Sbostic 		case '?':
13636460Sbostic 		default:
13739271Skfall 			if (!uid)
13839271Skfall 				syslog(LOG_ERR, "invalid flag %c", ch);
13937203Sbostic 			(void)fprintf(stderr,
14037203Sbostic 			    "usage: login [-fp] [username]\n");
14136460Sbostic 			exit(1);
14218549Ssam 		}
14336460Sbostic 	argc -= optind;
14436460Sbostic 	argv += optind;
14536549Sbostic 	if (*argv) {
14636647Skarels 		username = *argv;
14743653Sbostic 		if (strlen(username) > UT_NAMESIZE)
14843653Sbostic 			username[UT_NAMESIZE] = '\0';
14936549Sbostic 		ask = 0;
15036647Skarels 	} else
15136549Sbostic 		ask = 1;
15236460Sbostic 
15336460Sbostic 	ioctlval = 0;
15436460Sbostic 	(void)ioctl(0, TIOCLSET, &ioctlval);
15536460Sbostic 	(void)ioctl(0, TIOCNXCL, 0);
15636515Sbostic 	(void)fcntl(0, F_SETFL, ioctlval);
15736515Sbostic 	(void)ioctl(0, TIOCGETP, &sgttyb);
15836515Sbostic 	sgttyb.sg_erase = CERASE;
15936515Sbostic 	sgttyb.sg_kill = CKILL;
16036460Sbostic 	(void)ioctl(0, TIOCSLTC, &ltc);
16136460Sbostic 	(void)ioctl(0, TIOCSETC, &tc);
16236515Sbostic 	(void)ioctl(0, TIOCSETP, &sgttyb);
16336460Sbostic 
16436460Sbostic 	for (cnt = getdtablesize(); cnt > 2; cnt--)
16536460Sbostic 		close(cnt);
16636460Sbostic 
1671043Sbill 	ttyn = ttyname(0);
16837692Sbostic 	if (ttyn == NULL || *ttyn == '\0') {
16937692Sbostic 		(void)sprintf(tname, "%s??", _PATH_TTY);
17037692Sbostic 		ttyn = tname;
17137692Sbostic 	}
17236460Sbostic 	if (tty = rindex(ttyn, '/'))
17336460Sbostic 		++tty;
17436460Sbostic 	else
17516453Sroot 		tty = ttyn;
17636460Sbostic 
17736549Sbostic 	for (cnt = 0;; ask = 1) {
17838602Sbostic 		ioctlval = TTYDISC;
17936460Sbostic 		(void)ioctl(0, TIOCSETD, &ioctlval);
18036460Sbostic 
18136549Sbostic 		if (ask) {
18236515Sbostic 			fflag = 0;
18336460Sbostic 			getloginname();
18436515Sbostic 		}
18536647Skarels 		/*
18639271Skfall 		 * Note if trying multiple user names; log failures for
18739271Skfall 		 * previous user name, but don't bother logging one failure
18836647Skarels 		 * for nonexistent name (mistyped username).
18936647Skarels 		 */
19036647Skarels 		if (failures && strcmp(tbuf, username)) {
19136647Skarels 			if (failures > (pwd ? 0 : 1))
19236549Sbostic 				badlogin(tbuf);
19336647Skarels 			failures = 0;
19436549Sbostic 		}
19536647Skarels 		(void)strcpy(tbuf, username);
196*43659Sbostic 
19736515Sbostic 		if (pwd = getpwnam(username))
19836515Sbostic 			salt = pwd->pw_passwd;
199*43659Sbostic 		else
200*43659Sbostic 			salt = "xx";
20136460Sbostic 
20236460Sbostic 		/* if user not super-user, check for disabled logins */
203*43659Sbostic 		if (!pwd || pwd->pw_uid)
20436460Sbostic 			checknologin();
20536460Sbostic 
20612687Ssam 		/*
20736515Sbostic 		 * Disallow automatic login to root; if not invoked by
20836515Sbostic 		 * root, disallow if the uid's differ.
20912687Ssam 		 */
210*43659Sbostic 		if (pwd && fflag) {
21138726Skfall 			passwd_req =
21238726Skfall #ifndef	KERBEROS
21338726Skfall 			     pwd->pw_uid == 0 ||
21438726Skfall #endif
21536515Sbostic 			    (uid && uid != pwd->pw_uid);
21631695Skarels 		}
21736460Sbostic 
21812687Ssam 		/*
21936523Skarels 		 * If no pre-authentication and a password exists
22036460Sbostic 		 * for this user, prompt for one and verify it.
22112687Ssam 		 */
222*43659Sbostic 		if (pwd && (!passwd_req || !*pwd->pw_passwd))
22336460Sbostic 			break;
22412687Ssam 
22539271Skfall 		/*
22639271Skfall 		 * If trying to log in as root, but with insecure terminal,
22739271Skfall 		 * refuse the login attempt.
22839271Skfall 		 */
229*43659Sbostic 		if (pwd && pwd->pw_uid == 0 && !rootterm(tty)) {
23039271Skfall 			(void)fprintf(stderr,
23139271Skfall 			    "%s login refused on this terminal.\n",
23239271Skfall 			    pwd->pw_name);
23339271Skfall 			if (hostname)
23439271Skfall 				syslog(LOG_NOTICE,
23539271Skfall 				    "LOGIN %s REFUSED FROM %s ON TTY %s",
23639271Skfall 				    pwd->pw_name, hostname, tty);
23739271Skfall 			else
23839271Skfall 				syslog(LOG_NOTICE,
23939271Skfall 				    "LOGIN %s REFUSED ON TTY %s",
24039271Skfall 				     pwd->pw_name, tty);
24139271Skfall 			continue;
24239271Skfall 		}
24339271Skfall 
24443653Sbostic 		(void)setpriority(PRIO_PROCESS, 0, -4);
245*43659Sbostic 
24643653Sbostic 		p = getpass("Password:");
24736608Skfall 
248*43659Sbostic 		if (pwd) {
24943653Sbostic #ifdef KERBEROS
250*43659Sbostic 			rval = klogin(pwd, localhost, p);
251*43659Sbostic 			if (rval == 1)
252*43659Sbostic 				rval = strcmp(crypt(p, salt), pwd->pw_passwd);
253*43659Sbostic #else
25443653Sbostic 			rval = strcmp(crypt(p, salt), pwd->pw_passwd);
25537203Sbostic #endif
256*43659Sbostic 		}
25743653Sbostic 		bzero(p, strlen(p));
258*43659Sbostic 
259*43659Sbostic 		(void)setpriority(PRIO_PROCESS, 0, 0);
260*43659Sbostic 
261*43659Sbostic 		if (pwd && !rval)
26236460Sbostic 			break;
26336460Sbostic 
264*43659Sbostic 		(void)printf("Login incorrect\n");
26536647Skarels 		failures++;
26636549Sbostic 		/* we allow 10 tries, but after 3 we start backing off */
26736549Sbostic 		if (++cnt > 3) {
26836549Sbostic 			if (cnt >= 10) {
26936549Sbostic 				badlogin(username);
27036549Sbostic 				(void)ioctl(0, TIOCHPCL, (struct sgttyb *)NULL);
27136549Sbostic 				sleepexit(1);
27236549Sbostic 			}
27336549Sbostic 			sleep((u_int)((cnt - 3) * 5));
2742822Swnj 		}
27536460Sbostic 	}
2761043Sbill 
27736460Sbostic 	/* committed to login -- turn off timeout */
27836460Sbostic 	(void)alarm((u_int)0);
27936460Sbostic 
28037692Sbostic 	/* paranoia... */
28137692Sbostic 	endpwent();
28237692Sbostic 
28336515Sbostic 	if (chdir(pwd->pw_dir) < 0) {
28437203Sbostic 		(void)printf("No directory %s!\n", pwd->pw_dir);
28536515Sbostic 		if (chdir("/"))
28636515Sbostic 			exit(0);
28736515Sbostic 		pwd->pw_dir = "/";
28837203Sbostic 		(void)printf("Logging in with home = \"/\".\n");
28936515Sbostic 	}
29036515Sbostic 
29137203Sbostic 	quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
29237203Sbostic 
29336880Sbostic 	if (pwd->pw_change || pwd->pw_expire)
29436880Sbostic 		(void)gettimeofday(&tp, (struct timezone *)NULL);
29536880Sbostic 	if (pwd->pw_change)
29636880Sbostic 		if (tp.tv_sec >= pwd->pw_change) {
29737203Sbostic 			(void)printf("Sorry -- your password has expired.\n");
29836880Sbostic 			sleepexit(1);
29936880Sbostic 		}
30038216Sbostic 		else if (pwd->pw_change - tp.tv_sec <
30138216Sbostic 		    2 * DAYSPERWEEK * SECSPERDAY && !quietlog) {
30236880Sbostic 			ttp = localtime(&pwd->pw_change);
30337203Sbostic 			(void)printf("Warning: your password expires on %s %d, %d\n",
30438216Sbostic 			    months[ttp->tm_mon], ttp->tm_mday,
30538216Sbostic 			    TM_YEAR_BASE + ttp->tm_year);
30636880Sbostic 		}
30736880Sbostic 	if (pwd->pw_expire)
30836880Sbostic 		if (tp.tv_sec >= pwd->pw_expire) {
30937203Sbostic 			(void)printf("Sorry -- your account has expired.\n");
31036880Sbostic 			sleepexit(1);
31136880Sbostic 		}
31238216Sbostic 		else if (pwd->pw_expire - tp.tv_sec <
31338216Sbostic 		    2 * DAYSPERWEEK * SECSPERDAY && !quietlog) {
31436880Sbostic 			ttp = localtime(&pwd->pw_expire);
31537203Sbostic 			(void)printf("Warning: your account expires on %s %d, %d\n",
31638216Sbostic 			    months[ttp->tm_mon], ttp->tm_mday,
31738216Sbostic 			    TM_YEAR_BASE + ttp->tm_year);
31836880Sbostic 		}
31936880Sbostic 
32036515Sbostic 	/* nothing else left to fail -- really log in */
32136460Sbostic 	{
32236460Sbostic 		struct utmp utmp;
32336460Sbostic 
32443653Sbostic 		bzero((void *)&utmp, sizeof(utmp));
32536460Sbostic 		(void)time(&utmp.ut_time);
32636460Sbostic 		strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
32736591Sbostic 		if (hostname)
32836591Sbostic 			strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
32936460Sbostic 		strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
33036460Sbostic 		login(&utmp);
33136460Sbostic 	}
33236460Sbostic 
33336549Sbostic 	dolastlog(quietlog);
33436460Sbostic 
33537203Sbostic 	if (!hflag) {					/* XXX */
33636460Sbostic 		static struct winsize win = { 0, 0, 0, 0 };
33736460Sbostic 
33836460Sbostic 		(void)ioctl(0, TIOCSWINSZ, &win);
33936460Sbostic 	}
34036460Sbostic 
34136460Sbostic 	(void)chown(ttyn, pwd->pw_uid,
34236460Sbostic 	    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
34336460Sbostic 	(void)chmod(ttyn, 0620);
34436460Sbostic 	(void)setgid(pwd->pw_gid);
34536460Sbostic 
34636460Sbostic 	initgroups(username, pwd->pw_gid);
34736460Sbostic 
34836515Sbostic 	if (*pwd->pw_shell == '\0')
34937203Sbostic 		pwd->pw_shell = _PATH_BSHELL;
35036515Sbostic 
35136460Sbostic 	/* destroy environment unless user has requested preservation */
35218549Ssam 	if (!pflag)
35318549Ssam 		environ = envinit;
35436515Sbostic 	(void)setenv("HOME", pwd->pw_dir, 1);
35536515Sbostic 	(void)setenv("SHELL", pwd->pw_shell, 1);
35618549Ssam 	if (term[0] == '\0')
35736647Skarels 		strncpy(term, stypeof(tty), sizeof(term));
35836515Sbostic 	(void)setenv("TERM", term, 0);
35936515Sbostic 	(void)setenv("USER", pwd->pw_name, 1);
36037252Sbostic 	(void)setenv("PATH", _PATH_DEFPATH, 0);
36118549Ssam 
36216453Sroot 	if (tty[sizeof("tty")-1] == 'd')
36318549Ssam 		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
36418549Ssam 	if (pwd->pw_uid == 0)
36536460Sbostic 		if (hostname)
36636647Skarels 			syslog(LOG_NOTICE, "ROOT LOGIN ON %s FROM %s",
36736549Sbostic 			    tty, hostname);
36825230Smckusick 		else
36936647Skarels 			syslog(LOG_NOTICE, "ROOT LOGIN ON %s", tty);
37036460Sbostic 
37143653Sbostic #ifdef KERBEROS
37243653Sbostic 	if (!quietlog && notickets == 1)
37343653Sbostic 		(void)printf("Warning: no Kerberos tickets issued.\n");
37443653Sbostic #endif
37543653Sbostic 
3766329Swnj 	if (!quietlog) {
37717664Sserge 		struct stat st;
37818549Ssam 
37936460Sbostic 		motd();
38037203Sbostic 		(void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
38136460Sbostic 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
38237203Sbostic 			(void)printf("You have %smail.\n",
38336460Sbostic 			    (st.st_mtime > st.st_atime) ? "new " : "");
3842822Swnj 	}
38536460Sbostic 
38636460Sbostic 	(void)signal(SIGALRM, SIG_DFL);
38736460Sbostic 	(void)signal(SIGQUIT, SIG_DFL);
38836460Sbostic 	(void)signal(SIGINT, SIG_DFL);
38936460Sbostic 	(void)signal(SIGTSTP, SIG_IGN);
39036460Sbostic 
39136460Sbostic 	tbuf[0] = '-';
39236460Sbostic 	strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ?
39336460Sbostic 	    p + 1 : pwd->pw_shell);
39437203Sbostic 
39540009Ssklower 	if (setlogin(pwd->pw_name) < 0)
39640009Ssklower 		syslog(LOG_ERR, "setlogin() failure: %m");
39737692Sbostic 
39837203Sbostic 	/* discard permissions last so can't get killed and drop core */
39937203Sbostic 	(void)setuid(pwd->pw_uid);
40037203Sbostic 
40136460Sbostic 	execlp(pwd->pw_shell, tbuf, 0);
40237203Sbostic 	(void)fprintf(stderr, "login: no shell: %s.\n", strerror(errno));
4031043Sbill 	exit(0);
4041043Sbill }
4051043Sbill 
40636460Sbostic getloginname()
40712687Ssam {
40836460Sbostic 	register int ch;
40936460Sbostic 	register char *p;
41036460Sbostic 	static char nbuf[UT_NAMESIZE + 1];
41112687Ssam 
41236460Sbostic 	for (;;) {
41337203Sbostic 		(void)printf("login: ");
41436515Sbostic 		for (p = nbuf; (ch = getchar()) != '\n'; ) {
41536549Sbostic 			if (ch == EOF) {
41636549Sbostic 				badlogin(username);
41712687Ssam 				exit(0);
41836549Sbostic 			}
41936515Sbostic 			if (p < nbuf + UT_NAMESIZE)
42036460Sbostic 				*p++ = ch;
42112687Ssam 		}
42236460Sbostic 		if (p > nbuf)
42336460Sbostic 			if (nbuf[0] == '-')
42437203Sbostic 				(void)fprintf(stderr,
42536460Sbostic 				    "login names may not start with '-'.\n");
42636460Sbostic 			else {
42736460Sbostic 				*p = '\0';
42836460Sbostic 				username = nbuf;
42936515Sbostic 				break;
43036460Sbostic 			}
43112687Ssam 	}
43212687Ssam }
43312687Ssam 
43443653Sbostic void
43512687Ssam timedout()
43612687Ssam {
43737203Sbostic 	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
43812687Ssam 	exit(0);
43912687Ssam }
44012687Ssam 
44136647Skarels rootterm(ttyn)
44236647Skarels 	char *ttyn;
4431043Sbill {
44436460Sbostic 	struct ttyent *t;
4456466Swnj 
44636647Skarels 	return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE);
4471043Sbill }
4481043Sbill 
44936515Sbostic jmp_buf motdinterrupt;
45036515Sbostic 
45136460Sbostic motd()
4522822Swnj {
45336515Sbostic 	register int fd, nchars;
45438726Skfall 	sig_t oldint;
45538726Skfall 	int sigint();
45636515Sbostic 	char tbuf[8192];
4572822Swnj 
45837203Sbostic 	if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0)
45936460Sbostic 		return;
46036460Sbostic 	oldint = signal(SIGINT, sigint);
46136515Sbostic 	if (setjmp(motdinterrupt) == 0)
46236515Sbostic 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
46336515Sbostic 			(void)write(fileno(stdout), tbuf, nchars);
46436460Sbostic 	(void)signal(SIGINT, oldint);
46536515Sbostic 	(void)close(fd);
46636460Sbostic }
46736460Sbostic 
46836460Sbostic sigint()
46936460Sbostic {
47036515Sbostic 	longjmp(motdinterrupt, 1);
47136460Sbostic }
47236460Sbostic 
47336460Sbostic checknologin()
47436460Sbostic {
47536460Sbostic 	register int fd, nchars;
47636515Sbostic 	char tbuf[8192];
47736460Sbostic 
47837203Sbostic 	if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
47936460Sbostic 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
48036460Sbostic 			(void)write(fileno(stdout), tbuf, nchars);
48136460Sbostic 		sleepexit(0);
4822822Swnj 	}
4832822Swnj }
4842822Swnj 
48536549Sbostic dolastlog(quiet)
48636460Sbostic 	int quiet;
4871043Sbill {
48836460Sbostic 	struct lastlog ll;
48936460Sbostic 	int fd;
49036880Sbostic 	char *ctime();
4911043Sbill 
49237203Sbostic 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
49336515Sbostic 		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
49436460Sbostic 		if (!quiet) {
49536460Sbostic 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
49636460Sbostic 			    ll.ll_time != 0) {
49737203Sbostic 				(void)printf("Last login: %.*s ",
49836460Sbostic 				    24-5, (char *)ctime(&ll.ll_time));
49936460Sbostic 				if (*ll.ll_host != '\0')
50037203Sbostic 					(void)printf("from %.*s\n",
50136460Sbostic 					    sizeof(ll.ll_host), ll.ll_host);
50236460Sbostic 				else
50337203Sbostic 					(void)printf("on %.*s\n",
50436460Sbostic 					    sizeof(ll.ll_line), ll.ll_line);
50536460Sbostic 			}
50636515Sbostic 			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
50736460Sbostic 		}
50843653Sbostic 		bzero((void *)&ll, sizeof(ll));
50936460Sbostic 		(void)time(&ll.ll_time);
51036460Sbostic 		strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
51136591Sbostic 		if (hostname)
51236591Sbostic 			strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
51336460Sbostic 		(void)write(fd, (char *)&ll, sizeof(ll));
51436460Sbostic 		(void)close(fd);
5151043Sbill 	}
5161043Sbill }
5171043Sbill 
51836549Sbostic badlogin(name)
51936549Sbostic 	char *name;
52036549Sbostic {
52136647Skarels 	if (failures == 0)
52236549Sbostic 		return;
52336549Sbostic 	if (hostname)
52436647Skarels 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s, %s",
52536647Skarels 		    failures, failures > 1 ? "S" : "", hostname, name);
52636549Sbostic 	else
52736647Skarels 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s, %s",
52836647Skarels 		    failures, failures > 1 ? "S" : "", tty, name);
52936549Sbostic }
53036549Sbostic 
5312822Swnj #undef	UNKNOWN
53236460Sbostic #define	UNKNOWN	"su"
5331043Sbill 
5341043Sbill char *
53536647Skarels stypeof(ttyid)
53636647Skarels 	char *ttyid;
5371043Sbill {
53836460Sbostic 	struct ttyent *t;
5391043Sbill 
54036647Skarels 	return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
5411043Sbill }
5426005Swnj 
54336460Sbostic sleepexit(eval)
54436460Sbostic 	int eval;
54526862Smckusick {
54636460Sbostic 	sleep((u_int)5);
54736460Sbostic 	exit(eval);
54826862Smckusick }
549