login.c revision 23985
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
3423246Swosch#if 0
351590Srgrimesstatic char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
3823246Swosch#endif
391590Srgrimes
401590Srgrimes#ifndef lint
411590Srgrimesstatic char sccsid[] = "@(#)login.c	8.4 (Berkeley) 4/2/94";
421590Srgrimes#endif /* not lint */
431590Srgrimes
441590Srgrimes/*
451590Srgrimes * login [ name ]
461590Srgrimes * login -h hostname	(for telnetd, etc.)
471590Srgrimes * login -f name	(for pre-authenticated login: datakit, xterm, etc.)
481590Srgrimes */
491590Srgrimes
5023246Swosch#include <sys/copyright.h>
511590Srgrimes#include <sys/param.h>
521590Srgrimes#include <sys/stat.h>
531590Srgrimes#include <sys/time.h>
541590Srgrimes#include <sys/resource.h>
551590Srgrimes#include <sys/file.h>
5616423Sache#include <netinet/in.h>
5716423Sache#include <arpa/inet.h>
581590Srgrimes
591590Srgrimes#include <err.h>
601590Srgrimes#include <errno.h>
611590Srgrimes#include <grp.h>
6216423Sache#include <netdb.h>
631590Srgrimes#include <pwd.h>
641590Srgrimes#include <setjmp.h>
651590Srgrimes#include <signal.h>
661590Srgrimes#include <stdio.h>
671590Srgrimes#include <stdlib.h>
681590Srgrimes#include <string.h>
691590Srgrimes#include <syslog.h>
701590Srgrimes#include <ttyent.h>
711590Srgrimes#include <unistd.h>
721590Srgrimes#include <utmp.h>
731590Srgrimes
7421528Sdavidn#ifdef LOGIN_CAP
7521528Sdavidn#include <login_cap.h>
7621528Sdavidn#else /* Undef AUTH as well */
7721528Sdavidn#undef LOGIN_CAP_AUTH
7821528Sdavidn#endif
7921528Sdavidn
8023985Sdavidn/*
8123985Sdavidn * If LOGIN_CAP_AUTH is activated:
8221528Sdavidn * kerberose & skey logins are runtime selected via login
8321528Sdavidn * login_getstyle() and authentication types for login classes
8421528Sdavidn * The actual login itself is handled via /usr/libexec/login_<style>
8521528Sdavidn * Valid styles are determined by the auth-type=style,style entries
8621528Sdavidn * in the login class.
8721528Sdavidn */
8821528Sdavidn#ifdef LOGIN_CAP_AUTH
8921528Sdavidn#undef KERBEROS
9021528Sdavidn#undef SKEY
9123985Sdavidn#endif /* LOGIN_CAP_AUTH */
9223985Sdavidn
933702Spst#ifdef	SKEY
943702Spst#include <skey.h>
9521528Sdavidn#endif /* SKEY */
963702Spst
971590Srgrimes#include "pathnames.h"
981590Srgrimes
991590Srgrimesvoid	 badlogin __P((char *));
1001590Srgrimesvoid	 checknologin __P((void));
1011590Srgrimesvoid	 dolastlog __P((int));
1021590Srgrimesvoid	 getloginname __P((void));
10321528Sdavidnvoid	 motd __P((char *));
1041590Srgrimesint	 rootterm __P((char *));
1051590Srgrimesvoid	 sigint __P((int));
1061590Srgrimesvoid	 sleepexit __P((int));
10723985Sdavidnvoid	 refused __P((char *,char *,int));
1081590Srgrimeschar	*stypeof __P((char *));
1091590Srgrimesvoid	 timedout __P((int));
1102224Sguidovoid     login_fbtab __P((char *, uid_t, gid_t));
1111590Srgrimes#ifdef KERBEROS
1121590Srgrimesint	 klogin __P((struct passwd *, char *, char *, char *));
1131590Srgrimes#endif
1141590Srgrimes
1151590Srgrimesextern void login __P((struct utmp *));
1161590Srgrimes
1171590Srgrimes#define	TTYGRPNAME	"tty"		/* name of group to own ttys */
11823985Sdavidn#define	DEFAULT_BACKOFF	3
11923985Sdavidn#define	DEFAULT_RETRIES	10
1201590Srgrimes
1211590Srgrimes/*
1221590Srgrimes * This bounds the time given to login.  Not a define so it can
1231590Srgrimes * be patched on machines where it's too small.
1241590Srgrimes */
1251590Srgrimesu_int	timeout = 300;
1261590Srgrimes
1271590Srgrimes#ifdef KERBEROS
1281590Srgrimesint	notickets = 1;
1295627Swollmanint	noticketsdontcomplain = 1;
1301590Srgrimeschar	*instance;
1311590Srgrimeschar	*krbtkfile_env;
1321590Srgrimesint	authok;
1331590Srgrimes#endif
1341590Srgrimes
1351590Srgrimesstruct	passwd *pwd;
1361590Srgrimesint	failures;
13723985Sdavidnchar	*term, *envinit[1], *hostname, *username, *tty;
13816423Sachechar    full_hostname[MAXHOSTNAMELEN];
1391590Srgrimes
1401590Srgrimesint
1411590Srgrimesmain(argc, argv)
1421590Srgrimes	int argc;
1431590Srgrimes	char *argv[];
1441590Srgrimes{
1451590Srgrimes	extern char **environ;
1461590Srgrimes	struct group *gr;
1471590Srgrimes	struct stat st;
1481590Srgrimes	struct timeval tp;
1491590Srgrimes	struct utmp utmp;
15023985Sdavidn	int rootok, retries, backoff;
1511590Srgrimes	int ask, ch, cnt, fflag, hflag, pflag, quietlog, rootlogin, rval;
1524878Sugen	int changepass;
15323985Sdavidn	time_t warntime;
1541590Srgrimes	uid_t uid;
1553205Spst	char *domain, *p, *ep, *salt, *ttyn;
1561590Srgrimes	char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
1571590Srgrimes	char localhost[MAXHOSTNAMELEN];
15823985Sdavidn	char *shell = NULL;
15921528Sdavidn#ifdef LOGIN_CAP
16021528Sdavidn	login_cap_t *lc = NULL;
16121528Sdavidn#ifdef LOGIN_CAP_AUTH
16221528Sdavidn	char *style, *authtype;
16321528Sdavidn	char *auth_method = NULL;
16421528Sdavidn	char *instance = NULL;
16521528Sdavidn	int authok;
16623985Sdavidn#endif /* LOGIN_CAP_AUTH */
16723985Sdavidn#endif /* LOGIN_CAP */
16821528Sdavidn#ifdef SKEY
1693205Spst	int permit_passwd = 0;
17021528Sdavidn#endif /* SKEY */
1711590Srgrimes
1721590Srgrimes	(void)signal(SIGALRM, timedout);
1731590Srgrimes	(void)alarm(timeout);
1741590Srgrimes	(void)signal(SIGQUIT, SIG_IGN);
1751590Srgrimes	(void)signal(SIGINT, SIG_IGN);
1761590Srgrimes	(void)setpriority(PRIO_PROCESS, 0, 0);
1771590Srgrimes
1781590Srgrimes	openlog("login", LOG_ODELAY, LOG_AUTH);
1791590Srgrimes
1801590Srgrimes	/*
1811590Srgrimes	 * -p is used by getty to tell login not to destroy the environment
1821590Srgrimes	 * -f is used to skip a second login authentication
1831590Srgrimes	 * -h is used by other servers to pass the name of the remote
1841590Srgrimes	 *    host to login so that it may be placed in utmp and wtmp
1851590Srgrimes	 */
1863205Spst	*full_hostname = '\0';
1871590Srgrimes	domain = NULL;
18823985Sdavidn	term = NULL;
1891590Srgrimes	if (gethostname(localhost, sizeof(localhost)) < 0)
1901590Srgrimes		syslog(LOG_ERR, "couldn't get local hostname: %m");
1911590Srgrimes	else
1921590Srgrimes		domain = strchr(localhost, '.');
1931590Srgrimes
1941590Srgrimes	fflag = hflag = pflag = 0;
1951590Srgrimes	uid = getuid();
1961590Srgrimes	while ((ch = getopt(argc, argv, "fh:p")) != EOF)
1971590Srgrimes		switch (ch) {
1981590Srgrimes		case 'f':
1991590Srgrimes			fflag = 1;
2001590Srgrimes			break;
2011590Srgrimes		case 'h':
2021590Srgrimes			if (uid)
2031590Srgrimes				errx(1, "-h option: %s", strerror(EPERM));
2041590Srgrimes			hflag = 1;
2053205Spst			strncpy(full_hostname, optarg, sizeof(full_hostname)-1);
2061590Srgrimes			if (domain && (p = strchr(optarg, '.')) &&
2071590Srgrimes			    strcasecmp(p, domain) == 0)
2081590Srgrimes				*p = 0;
20916423Sache			if (strlen(optarg) > UT_HOSTSIZE) {
21016423Sache				struct hostent *hp = gethostbyname(optarg);
21116423Sache
21216423Sache				if (hp != NULL) {
21316423Sache					struct in_addr in;
21416423Sache
21516423Sache					memmove(&in, hp->h_addr, sizeof(in));
21616423Sache					optarg = strdup(inet_ntoa(in));
21716423Sache				} else
21816423Sache					optarg = "invalid hostname";
21916423Sache			}
2201590Srgrimes			hostname = optarg;
2211590Srgrimes			break;
2221590Srgrimes		case 'p':
2231590Srgrimes			pflag = 1;
2241590Srgrimes			break;
2251590Srgrimes		case '?':
2261590Srgrimes		default:
2271590Srgrimes			if (!uid)
2281590Srgrimes				syslog(LOG_ERR, "invalid flag %c", ch);
2291590Srgrimes			(void)fprintf(stderr,
2301590Srgrimes			    "usage: login [-fp] [-h hostname] [username]\n");
2311590Srgrimes			exit(1);
2321590Srgrimes		}
2331590Srgrimes	argc -= optind;
2341590Srgrimes	argv += optind;
2351590Srgrimes
2361590Srgrimes	if (*argv) {
2371590Srgrimes		username = *argv;
2381590Srgrimes		ask = 0;
2391590Srgrimes	} else
2401590Srgrimes		ask = 1;
2411590Srgrimes
2421590Srgrimes	for (cnt = getdtablesize(); cnt > 2; cnt--)
2431590Srgrimes		(void)close(cnt);
2441590Srgrimes
2451590Srgrimes	ttyn = ttyname(STDIN_FILENO);
2461590Srgrimes	if (ttyn == NULL || *ttyn == '\0') {
2471590Srgrimes		(void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
2481590Srgrimes		ttyn = tname;
2491590Srgrimes	}
25023985Sdavidn	if ((tty = strrchr(ttyn, '/')) != NULL)
2511590Srgrimes		++tty;
2521590Srgrimes	else
2531590Srgrimes		tty = ttyn;
2541590Srgrimes
25521528Sdavidn#ifdef LOGIN_CAP_AUTH
25621528Sdavidn	authtype = hostname ? "rlogin" : "login";
25721528Sdavidn#endif
25823985Sdavidn#ifdef LOGIN_CAP
25923985Sdavidn	/*
26023985Sdavidn	 * Get "login-retries" & "login-backoff" from default class
26123985Sdavidn	 */
26223985Sdavidn	lc = login_getclass(NULL);
26323985Sdavidn	retries = login_getcapnum(lc, "login-retries", DEFAULT_RETRIES, DEFAULT_RETRIES);
26423985Sdavidn	backoff = login_getcapnum(lc, "login-backoff", DEFAULT_BACKOFF, DEFAULT_BACKOFF);
26523985Sdavidn	login_close(lc);
26623985Sdavidn	lc = NULL;
26723985Sdavidn#else
26823985Sdavidn	retries = DEFAULT_RETRIES;
26923985Sdavidn	backoff = DEFAULT_BACKOFF;
27023985Sdavidn#endif
27121528Sdavidn
2721590Srgrimes	for (cnt = 0;; ask = 1) {
2731590Srgrimes		if (ask) {
2741590Srgrimes			fflag = 0;
2751590Srgrimes			getloginname();
2761590Srgrimes		}
2771590Srgrimes		rootlogin = 0;
27821528Sdavidn		rootok = rootterm(tty); /* Default (auth may change) */
27921528Sdavidn#ifdef LOGIN_CAP_AUTH
28021528Sdavidn		authok = 0;
28121528Sdavidn		if (auth_method = strchr(username, ':')) {
28221528Sdavidn			*auth_method = '\0';
28321528Sdavidn			auth_method++;
28421528Sdavidn			if (*auth_method == '\0')
28521528Sdavidn				auth_method = NULL;
28621528Sdavidn		}
28721528Sdavidn		/*
28821528Sdavidn		 * We need to do this regardless of whether
28921528Sdavidn		 * kerberos is available.
29021528Sdavidn		 */
2911590Srgrimes		if ((instance = strchr(username, '.')) != NULL) {
2921590Srgrimes			if (strncmp(instance, ".root", 5) == 0)
2931590Srgrimes				rootlogin = 1;
2941590Srgrimes			*instance++ = '\0';
2951590Srgrimes		} else
2961590Srgrimes			instance = "";
29721528Sdavidn#else /* !LOGIN_CAP_AUTH */
29821528Sdavidn#ifdef KERBEROS
29921528Sdavidn		if ((instance = strchr(username, '.')) != NULL) {
30021528Sdavidn			if (strncmp(instance, ".root", 5) == 0)
30121528Sdavidn				rootlogin = 1;
30221528Sdavidn			*instance++ = '\0';
30321528Sdavidn		} else
30421528Sdavidn			instance = "";
30521528Sdavidn#endif /* KERBEROS */
30621528Sdavidn#endif /* LOGIN_CAP_AUTH */
30721528Sdavidn
3081590Srgrimes		if (strlen(username) > UT_NAMESIZE)
3091590Srgrimes			username[UT_NAMESIZE] = '\0';
3101590Srgrimes
3111590Srgrimes		/*
3121590Srgrimes		 * Note if trying multiple user names; log failures for
3131590Srgrimes		 * previous user name, but don't bother logging one failure
3141590Srgrimes		 * for nonexistent name (mistyped username).
3151590Srgrimes		 */
3161590Srgrimes		if (failures && strcmp(tbuf, username)) {
3171590Srgrimes			if (failures > (pwd ? 0 : 1))
3181590Srgrimes				badlogin(tbuf);
3191590Srgrimes			failures = 0;
3201590Srgrimes		}
3211590Srgrimes		(void)strcpy(tbuf, username);
3221590Srgrimes
32323985Sdavidn		if ((pwd = getpwnam(username)) != NULL)
32423985Sdavidn			salt = pwd->pw_passwd;
32523985Sdavidn		else
32623985Sdavidn			salt = "xx";
32723985Sdavidn
32821528Sdavidn#ifdef LOGIN_CAP
32923985Sdavidn		/*
33023985Sdavidn		 * Establish the class now, before we might goto
33121528Sdavidn		 * within the next block. pwd can be NULL since it
33221528Sdavidn		 * falls back to the "default" class if it is.
33321528Sdavidn		 */
33421528Sdavidn		lc = login_getclass(pwd);
33521528Sdavidn#endif /* LOGIN_CAP */
3361590Srgrimes
3371590Srgrimes		/*
3381590Srgrimes		 * if we have a valid account name, and it doesn't have a
3391590Srgrimes		 * password, or the -f option was specified and the caller
3401590Srgrimes		 * is root or the caller isn't changing their uid, don't
3411590Srgrimes		 * authenticate.
3421590Srgrimes		 */
34323985Sdavidn		rval = 1;
34421528Sdavidn		if (pwd != NULL) {
3453205Spst			if (pwd->pw_uid == 0)
3463205Spst				rootlogin = 1;
3473205Spst
34823985Sdavidn			if (fflag && (uid == (uid_t)0 ||
34923985Sdavidn				      uid == (uid_t)pwd->pw_uid)) {
3503205Spst				/* already authenticated */
3513205Spst				break;
3523205Spst			} else if (pwd->pw_passwd[0] == '\0') {
3533205Spst				/* pretend password okay */
3543205Spst				rval = 0;
3553205Spst				goto ttycheck;
3563205Spst			}
3573205Spst		}
3583205Spst
3591590Srgrimes		fflag = 0;
3601590Srgrimes
3611590Srgrimes		(void)setpriority(PRIO_PROCESS, 0, -4);
3621590Srgrimes
36321528Sdavidn#ifdef LOGIN_CAP_AUTH
36421528Sdavidn		/*
36523985Sdavidn		 * This hands off authorization to an authorization program,
36621528Sdavidn		 * depending on the styles available for the "auth-login",
36723985Sdavidn		 * auth-rlogin (or default) authorization styles.
36821528Sdavidn		 * We do this regardless of whether an account exists so that
36921528Sdavidn		 * the remote user cannot tell a "real" from an invented
37021528Sdavidn		 * account name. If we don't have an account we just fall
37121528Sdavidn		 * back to the first method for the "default" class.
37221528Sdavidn		 */
37323985Sdavidn		if (!(style = login_getstyle(lc, auth_method, authtype))) {
37423985Sdavidn
37523985Sdavidn			/*
37623985Sdavidn			 * No available authorization method
37723985Sdavidn			 */
37823985Sdavidn			rval = 1;
37923985Sdavidn			(void)printf("No auth method available for %s.\n",
38023985Sdavidn				     authtype);
38121528Sdavidn		} else {
38223985Sdavidn
38323985Sdavidn			/*
38423985Sdavidn			 * Put back the kerberos instance, if any was given.
38521528Sdavidn			 * Don't worry about the non-kerberos case here, since
38621528Sdavidn			 * if kerberos is not available or not selected and an
38721528Sdavidn			 * instance is given at the login prompt, su or rlogin -l,
38821528Sdavidn			 * then anything else should fail as well.
38921528Sdavidn			 */
39021528Sdavidn			if (*instance)
39121528Sdavidn				*(instance - 1) = '.';
39223985Sdavidn
39323985Sdavidn			rval = authenticate(username,
39423985Sdavidn					    lc ? lc->lc_class : "default",
39523985Sdavidn					    style, authtype);
39621528Sdavidn			/* Junk it again */
39721528Sdavidn			if (*instance)
39821528Sdavidn				*(instance - 1) = '\0';
39921528Sdavidn		}
40021528Sdavidn
40121528Sdavidn		if (!rval) {
40223985Sdavidn			char * approvp;
40323985Sdavidn
40421528Sdavidn			/*
40521528Sdavidn			 * If authentication succeeds, run any approval
40621528Sdavidn			 * program, if applicable for this class.
40721528Sdavidn			 */
40823985Sdavidn			approvep = login_getcapstr(lc, "approve", NULL, NULL);
40921528Sdavidn			rval = 1; /* Assume bad login again */
41023985Sdavidn
41123985Sdavidn			if (approvep==NULL ||
41223985Sdavidn			    auth_script(approvep, approvep, username,
41323985Sdavidn					lc->lc_class, 0) == 0) {
41423985Sdavidn				int     r;
41523985Sdavidn
41623985Sdavidn				r = auth_scan(AUTH_OKAY);
41723985Sdavidn				/*
41823985Sdavidn				 * See what the authorize program says
41923985Sdavidn				 */
42021528Sdavidn				if (r != AUTH_NONE) {
42121528Sdavidn					rval = 0;
42223985Sdavidn
42321528Sdavidn					if (!rootok && (r & AUTH_ROOTOKAY))
42421528Sdavidn						rootok = 1; /* root approved */
42523985Sdavidn					else
42623985Sdavidn						rootlogin = 0;
42723985Sdavidn
42821528Sdavidn					if (!authok && (r & AUTH_SECURE))
42921528Sdavidn						authok = 1; /* secure */
43021528Sdavidn				}
43121528Sdavidn			}
43221528Sdavidn		}
43321528Sdavidn#else /* !LOGIN_CAP_AUTH */
43421528Sdavidn#ifdef SKEY
43523985Sdavidn		permit_passwd = skeyaccess(username, tty,
43623985Sdavidn					   hostname ? full_hostname : NULL,
43723985Sdavidn					   NULL);
4383205Spst		p = skey_getpass("Password:", pwd, permit_passwd);
4393205Spst		ep = skey_crypt(p, salt, pwd, permit_passwd);
44021528Sdavidn#else /* !SKEY */
4411590Srgrimes		p = getpass("Password:");
4423205Spst		ep = crypt(p, salt);
44321528Sdavidn#endif/* SKEY */
44423985Sdavidn
44523985Sdavidn		if (pwd) {
4461590Srgrimes#ifdef KERBEROS
4477800Swollman#ifdef SKEY
44823985Sdavidn			/*
44923985Sdavidn			 * Do not allow user to type in kerberos password
4507800Swollman			 * over the net (actually, this is ok for encrypted
4517800Swollman			 * links, but we have no way of determining if the
4527800Swollman			 * link is encrypted.
4537800Swollman			 */
4547893Srgrimes			if (!permit_passwd) {
45523985Sdavidn				rval = 1;		/* failed */
4567800Swollman			} else
45721528Sdavidn#endif /* SKEY */
4581590Srgrimes			rval = klogin(pwd, instance, localhost, p);
4591590Srgrimes			if (rval != 0 && rootlogin && pwd->pw_uid != 0)
4601590Srgrimes				rootlogin = 0;
4611590Srgrimes			if (rval == 0)
46221528Sdavidn				authok = 1; /* kerberos authenticated ok */
46321528Sdavidn			else if (rval == 1) /* fallback to unix passwd */
4643205Spst				rval = strcmp(ep, pwd->pw_passwd);
46521528Sdavidn#else /* !KERBEROS */
46623985Sdavidn			rval = strcmp(ep, pwd->pw_passwd);
46721528Sdavidn#endif /* KERBEROS */
46823985Sdavidn		}
46923985Sdavidn
47021528Sdavidn		/* clear entered password */
4711590Srgrimes		memset(p, 0, strlen(p));
47221528Sdavidn#endif /* LOGIN_CAP_AUTH */
4731590Srgrimes
4741590Srgrimes		(void)setpriority(PRIO_PROCESS, 0, 0);
47523985Sdavidn
47621528Sdavidn#ifdef LOGIN_CAP
47721528Sdavidn		if (rval)
47821528Sdavidn			auth_rmfiles();
47921528Sdavidn#endif
4803205Spst	ttycheck:
4811590Srgrimes		/*
4821590Srgrimes		 * If trying to log in as root without Kerberos,
4831590Srgrimes		 * but with insecure terminal, refuse the login attempt.
4841590Srgrimes		 */
48521528Sdavidn#if defined(KERBEROS) || defined(LOGIN_CAP_AUTH)
4861590Srgrimes		if (authok == 0)
4871590Srgrimes#endif
48821528Sdavidn		if (pwd && !rval && rootlogin && !rootok) {
48923985Sdavidn			/* use same message as for authentication failure */
49023985Sdavidn			/* (void)fprintf(stderr, "%s login refused on this terminal.\n", pwd->pw_name); */
49123985Sdavidn			refused(NULL, "NOROOT", 0);
49223985Sdavidn			/* preserve backoff behaviour even for failed root */
49323985Sdavidn			/* continue; */
4941590Srgrimes		}
49523985Sdavidn		/* valid password & authenticated */
49623985Sdavidn		else if (pwd && !rval)
4971590Srgrimes			break;
4981590Srgrimes
4991590Srgrimes		(void)printf("Login incorrect\n");
5001590Srgrimes		failures++;
50123985Sdavidn
50223985Sdavidn		/*
50323985Sdavidn		 * we allow up to 'retry' (10) tries,
50423985Sdavidn		 * but after 'backoff' (3) we start backing off
50523985Sdavidn		 */
50623985Sdavidn		if (++cnt > backoff) {
50723985Sdavidn			if (cnt >= retries) {
5081590Srgrimes				badlogin(username);
5091590Srgrimes				sleepexit(1);
5101590Srgrimes			}
5111590Srgrimes			sleep((u_int)((cnt - 3) * 5));
5121590Srgrimes		}
5131590Srgrimes	}
5141590Srgrimes
5151590Srgrimes	/* committed to login -- turn off timeout */
5161590Srgrimes	(void)alarm((u_int)0);
5171590Srgrimes
5181590Srgrimes	endpwent();
5191590Srgrimes
5201590Srgrimes	/* if user not super-user, check for disabled logins */
52121528Sdavidn#ifdef LOGIN_CAP
5221590Srgrimes	if (!rootlogin)
52321528Sdavidn		auth_checknologin(lc);
52421528Sdavidn#else
52521528Sdavidn	if (!rootlogin)
5261590Srgrimes		checknologin();
52721528Sdavidn#endif
5281590Srgrimes
52921528Sdavidn#ifdef LOGIN_CAP
53021528Sdavidn	quietlog = login_getcapbool(lc, "hushlogin", 0);
53121528Sdavidn#else
53221528Sdavidn	quietlog = 0;
53321528Sdavidn#endif
53421528Sdavidn	if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) {
53521528Sdavidn#ifdef LOGIN_CAP
53623985Sdavidn		if (login_getcapbool(lc, "requirehome", !rootlogin))
53723985Sdavidn			refused("Home directory not available", "HOMEDIR", 1);
53823985Sdavidn#endif
53921528Sdavidn		if (chdir("/") < 0) {
54023985Sdavidn			refused("Cannot find root directory", "ROOTDIR", 1);
5411590Srgrimes		pwd->pw_dir = "/";
54223985Sdavidn		if (!quietlog || *pwd->pw_dir)
54323985Sdavidn			printf("No home directory.\nLogging in with home = \"/\".\n");
5441590Srgrimes	}
54521528Sdavidn	if (!quietlog)
54621528Sdavidn		quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
5471590Srgrimes
5481590Srgrimes	if (pwd->pw_change || pwd->pw_expire)
5491590Srgrimes		(void)gettimeofday(&tp, (struct timezone *)NULL);
5502532Sjkh
55121528Sdavidn#define DEFAULT_WARN  (2L * 7L & 86400L)  /* Two weeks */
55221528Sdavidn
55323985Sdavidn#ifdef LOGIN_CAP
55423985Sdavidn	warntime = login_getcaptime(lc, "warnpassword",
55523985Sdavidn				    DEFAULT_WARN, DEFAULT_WARN);
55623985Sdavidn#else
55723985Sdavidn	warntime = DEFAULT_WARN;
55823985Sdavidn#endif
55923985Sdavidn
5604878Sugen	changepass=0;
56121528Sdavidn	if (pwd->pw_change) {
5621590Srgrimes		if (tp.tv_sec >= pwd->pw_change) {
5631590Srgrimes			(void)printf("Sorry -- your password has expired.\n");
5644878Sugen			changepass=1;
56523985Sdavidn			syslog(LOG_INFO,
56623985Sdavidn			       "%s Password expired - forcing change",
56723985Sdavidn			       pwd->pw_name);
56823985Sdavidn		} else if (pwd->pw_change - tp.tv_sec < warntime && !quietlog)
56923985Sdavidn		    (void)printf("Warning: your password expires on %s",
57023985Sdavidn				 ctime(&pwd->pw_change));
57123985Sdavidn		}
57223985Sdavidn	}
57323985Sdavidn
57421528Sdavidn#ifdef LOGIN_CAP
57523985Sdavidn	warntime = login_getcaptime(lc, "warnexpire",
57623985Sdavidn				    DEFAULT_WARN, DEFAULT_WARN);
57721528Sdavidn#else
57823985Sdavidn	warntime = DEFAULT_WARN;
57921528Sdavidn#endif
58023985Sdavidn
58121528Sdavidn	if (pwd->pw_expire) {
5821590Srgrimes		if (tp.tv_sec >= pwd->pw_expire) {
58323985Sdavidn			refused("Sorry -- your account has expired",
58423985Sdavidn				"EXPIRED", 1);
58523985Sdavidn		} else if (pwd->pw_expire - tp.tv_sec < warntime && !quietlog)
58623985Sdavidn		    (void)printf("Warning: your account expires on %s",
58723985Sdavidn				 ctime(&pwd->pw_expire));
58821528Sdavidn	}
5891590Srgrimes
59021528Sdavidn#ifdef LOGIN_CAP
59121528Sdavidn	if (lc != NULL) {
59221528Sdavidn		if (hostname) {
59321528Sdavidn			struct hostent *hp = gethostbyname(full_hostname);
59421528Sdavidn
59521528Sdavidn			if (hp == NULL)
59621528Sdavidn				optarg = NULL;
59721528Sdavidn			else {
59821528Sdavidn				struct in_addr in;
59921528Sdavidn				memmove(&in, hp->h_addr, sizeof(in));
60021528Sdavidn				optarg = strdup(inet_ntoa(in));
60121528Sdavidn			}
60223985Sdavidn			if (!auth_hostok(lc, full_hostname, optarg))
60323985Sdavidn				refused("Permission denied", "HOST", 1);
60421528Sdavidn		}
60521528Sdavidn
60623985Sdavidn		if (!auth_ttyok(lc, tty))
60723985Sdavidn			refused("Permission denied", "TTY", 1);
60821528Sdavidn
60923985Sdavidn		if (!auth_timeok(lc, time(NULL)))
61023985Sdavidn			refused("Logins not available right now", "TIME", 1);
61121528Sdavidn	}
61223985Sdavidn        shell=login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
61321528Sdavidn#else /* !LOGIN_CAP */
61423985Sdavidn       shell=pwd->pw_shell;
61521528Sdavidn#endif /* LOGIN_CAP */
61623985Sdavidn	if (*pwd->pw_shell == '\0')
61723985Sdavidn		pwd->pw_shell = _PATH_BSHELL;
61823985Sdavidn	if (*shell == '\0')   /* Not overridden */
61923985Sdavidn		shell = pwd->pw_shell;
62023985Sdavidn	if ((shell = strdup(shell)) == NULL) {
62123985Sdavidn		syslog(LOG_NOTICE, "memory allocation error");
62223985Sdavidn		sleepexit(1);
62323985Sdavidn	}
62421528Sdavidn
62521528Sdavidn#ifdef LOGIN_ACCESS
62623985Sdavidn	if (login_access(pwd->pw_name, hostname ? full_hostname : tty) == 0)
62723985Sdavidn		refused("Permission denied", "ACCESS", 1);
62821528Sdavidn#endif /* LOGIN_ACCESS */
62921528Sdavidn
6301590Srgrimes	/* Nothing else left to fail -- really log in. */
6311590Srgrimes	memset((void *)&utmp, 0, sizeof(utmp));
6321590Srgrimes	(void)time(&utmp.ut_time);
6331590Srgrimes	(void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
6341590Srgrimes	if (hostname)
6351590Srgrimes		(void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
6361590Srgrimes	(void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
6371590Srgrimes	login(&utmp);
6381590Srgrimes
6391590Srgrimes	dolastlog(quietlog);
6401590Srgrimes
6412224Sguido	/*
6422224Sguido	 * Set device protections, depending on what terminal the
6432224Sguido	 * user is logged in. This feature is used on Suns to give
6442224Sguido	 * console users better privacy.
6452224Sguido	 */
6462224Sguido	login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
6472224Sguido
64823985Sdavidn	(void)chown(ttyn, pwd->pw_uid,
64923985Sdavidn		    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
6501590Srgrimes
65123985Sdavidn	/*
65223985Sdavidn	 * Preserve TERM if it happens to be already set.
65323985Sdavidn	 */
65423985Sdavidn	term = getenv("TERM");
6551590Srgrimes
65623985Sdavidn	/*
65723985Sdavidn	 * Exclude cons/vt/ptys only, assume dialup otherwise
65823985Sdavidn	 * TODO: Make dialup tty determination a library call
65923985Sdavidn	 * for consistency (finger etc.)
66023985Sdavidn	 */
66121528Sdavidn	if (hostname==NULL && strchr("vpqstPQST", tty[sizeof("tty")-1]) == NULL)
6621590Srgrimes		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
6631590Srgrimes
6641590Srgrimes#ifdef KERBEROS
6655627Swollman	if (!quietlog && notickets == 1 && !noticketsdontcomplain)
6661590Srgrimes		(void)printf("Warning: no Kerberos tickets issued.\n");
6671590Srgrimes#endif
6681590Srgrimes
6693205Spst#ifdef LOGALL
6703205Spst	/*
6713205Spst	 * Syslog each successful login, so we don't have to watch hundreds
6723205Spst	 * of wtmp or lastlogin files.
6733205Spst	 */
67423985Sdavidn	if (hostname)
67523985Sdavidn		syslog(LOG_INFO, "login from %s on %s as %s",
67623985Sdavidn		       full_hostname, tty, pwd->pw_name);
67723985Sdavidn	else
67823985Sdavidn		syslog(LOG_INFO, "login on %s as %s",
67923985Sdavidn		       tty, pwd->pw_name);
68021528Sdavidn#endif
68121528Sdavidn
68223985Sdavidn	/*
68323985Sdavidn	 * If fflag is on, assume caller/authenticator has logged root login.
68423985Sdavidn	 */
68523985Sdavidn	if (rootlogin && fflag == 0)
68623985Sdavidn	{
68723985Sdavidn		if (hostname)
68823985Sdavidn			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
68923985Sdavidn			       username, tty, full_hostname);
69023985Sdavidn		else
69123985Sdavidn			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
69223985Sdavidn			       username, tty);
69323985Sdavidn	}
69423985Sdavidn
69523985Sdavidn	/*
69623985Sdavidn	 * Destroy environment unless user has requested its preservation.
69723985Sdavidn	 * We need to do this before setusercontext() because that may
69823985Sdavidn	 * set or reset some environment variables.
69923985Sdavidn	 */
70021528Sdavidn	if (!pflag)
70121528Sdavidn		environ = envinit;
70221528Sdavidn
70323985Sdavidn	/*
70423985Sdavidn	 * We don't need to be root anymore, so
70521528Sdavidn	 * set the user and session context
70621528Sdavidn	 */
70721528Sdavidn#ifdef LOGIN_CAP
70821528Sdavidn	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
70921528Sdavidn                syslog(LOG_ERR, "setusercontext() failed - exiting");
71021528Sdavidn		exit(1);
7113205Spst	}
71221528Sdavidn#else
71321528Sdavidn     	if (setlogin(pwd->pw_name) < 0)
71421528Sdavidn                syslog(LOG_ERR, "setlogin() failure: %m");
71521528Sdavidn
71621528Sdavidn	(void)setgid(pwd->pw_gid);
71721528Sdavidn	initgroups(username, pwd->pw_gid);
71821528Sdavidn	(void)setuid(rootlogin ? 0 : pwd->pw_uid);
7193205Spst#endif
7203205Spst
72123148Sache	(void)setenv("SHELL", pwd->pw_shell, 1);
72221528Sdavidn	(void)setenv("HOME", pwd->pw_dir, 1);
72323985Sdavidn	if (term != NULL && *term != '\0')
72421528Sdavidn		(void)setenv("TERM", term, 1);	/* Preset overrides */
72521528Sdavidn	else {
72623985Sdavidn		(void)setenv("TERM", stypeof(tty), 0);	/* Fallback doesn't */
72721528Sdavidn	}
72821528Sdavidn	(void)setenv("LOGNAME", pwd->pw_name, 1);
72921528Sdavidn	(void)setenv("USER", pwd->pw_name, 1);
73021528Sdavidn	(void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
73121528Sdavidn#ifdef KERBEROS
73221528Sdavidn	if (krbtkfile_env)
73321528Sdavidn		(void)setenv("KRBTKFILE", krbtkfile_env, 1);
73421528Sdavidn#endif
73523985Sdavidn#if LOGIN_CAP_AUTH
73623985Sdavidn	auth_env();
73721528Sdavidn#endif
73821528Sdavidn
73923985Sdavidn#ifdef LOGIN_CAP
7401590Srgrimes	if (!quietlog) {
74123985Sdavidn		char	*cw;
74223985Sdavidn
74323985Sdavidn		cw = login_getcapstr(lc, "copyright", NULL, NULL);
74421528Sdavidn		if (cw != NULL && access(cw, F_OK) == 0)
74521528Sdavidn			motd(cw);
74621528Sdavidn		else
74723985Sdavidn		    (void)printf("%s\n\t%s %s\n",
74823985Sdavidn	"Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
74923985Sdavidn	"The Regents of the University of California. ",
75023985Sdavidn	"All rights reserved.");
75123985Sdavidn
75223985Sdavidn		(void)printf("\n");
75323985Sdavidn
75421528Sdavidn		cw = login_getcapstr(lc, "welcome", NULL, NULL);
75521528Sdavidn		if (cw == NULL || access(cw, F_OK) != 0)
75621528Sdavidn			cw = _PATH_MOTDFILE;
75721528Sdavidn		motd(cw);
75823985Sdavidn
75921528Sdavidn		cw = getenv("MAIL");	/* $MAIL may have been set by class */
76021528Sdavidn		if (cw != NULL) {
76121528Sdavidn			strncpy(tbuf, cw, sizeof(tbuf));
76221528Sdavidn			tbuf[sizeof(tbuf)-1] = '\0';
76321528Sdavidn		} else
76423985Sdavidn			snprintf(tbuf, sizeof(tbuf), "%s/%s",
76523985Sdavidn				 _PATH_MAILDIR, pwd->pw_name);
76621528Sdavidn#else
76723985Sdavidn	if (!quietlog) {
76823985Sdavidn		    (void)printf("%s\n\t%s %s\n",
76923985Sdavidn	"Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
77023985Sdavidn	"The Regents of the University of California. ",
77123985Sdavidn	"All rights reserved.");
77221528Sdavidn		motd(_PATH_MOTDFILE);
77323985Sdavidn		snprintf(tbuf, sizeof(tbuf), "%s/%s",
77423985Sdavidn			 _PATH_MAILDIR, pwd->pw_name);
77521528Sdavidn#endif
7761590Srgrimes		if (stat(tbuf, &st) == 0 && st.st_size != 0)
77723985Sdavidn			(void)printf("You have %smail.\n",
77823985Sdavidn				     (st.st_mtime > st.st_atime) ? "new " : "");
7791590Srgrimes	}
7801590Srgrimes
78121528Sdavidn#ifdef LOGIN_CAP
78221528Sdavidn	login_close(lc);
7833205Spst#endif
7843205Spst
7851590Srgrimes	(void)signal(SIGALRM, SIG_DFL);
7861590Srgrimes	(void)signal(SIGQUIT, SIG_DFL);
7871590Srgrimes	(void)signal(SIGINT, SIG_DFL);
7881590Srgrimes	(void)signal(SIGTSTP, SIG_IGN);
7891590Srgrimes
7904878Sugen	if (changepass) {
79121528Sdavidn		if (system(_PATH_CHPASS) != 0)
7924878Sugen			sleepexit(1);
7934878Sugen	}
7944878Sugen
79523985Sdavidn	/*
79623985Sdavidn	 * Login shells have a leading '-' in front of argv[0]
79723985Sdavidn	 */
79823985Sdavidn	tbuf[0] = '-';
79923985Sdavidn	(void)strcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ? p + 1 : pwd->pw_shell);
80023985Sdavidn
80121528Sdavidn	execlp(shell, tbuf, 0);
80221528Sdavidn	err(1, "%s", shell);
8031590Srgrimes}
8041590Srgrimes
8051590Srgrimes
80623985Sdavidn/*
80723985Sdavidn * Allow for authentication style and/or kerberos instance
80823985Sdavidn * */
80921528Sdavidn
81021528Sdavidn#define	NBUFSIZ		UT_NAMESIZE + 64
81121528Sdavidn
8121590Srgrimesvoid
8131590Srgrimesgetloginname()
8141590Srgrimes{
8151590Srgrimes	int ch;
8161590Srgrimes	char *p;
8171590Srgrimes	static char nbuf[NBUFSIZ];
8181590Srgrimes
8191590Srgrimes	for (;;) {
8201590Srgrimes		(void)printf("login: ");
8211590Srgrimes		for (p = nbuf; (ch = getchar()) != '\n'; ) {
8221590Srgrimes			if (ch == EOF) {
8231590Srgrimes				badlogin(username);
8241590Srgrimes				exit(0);
8251590Srgrimes			}
8261590Srgrimes			if (p < nbuf + (NBUFSIZ - 1))
8271590Srgrimes				*p++ = ch;
8281590Srgrimes		}
8291590Srgrimes		if (p > nbuf)
8301590Srgrimes			if (nbuf[0] == '-')
8311590Srgrimes				(void)fprintf(stderr,
8321590Srgrimes				    "login names may not start with '-'.\n");
8331590Srgrimes			else {
8341590Srgrimes				*p = '\0';
8351590Srgrimes				username = nbuf;
8361590Srgrimes				break;
8371590Srgrimes			}
8381590Srgrimes	}
8391590Srgrimes}
8401590Srgrimes
8411590Srgrimesint
8421590Srgrimesrootterm(ttyn)
8431590Srgrimes	char *ttyn;
8441590Srgrimes{
8451590Srgrimes	struct ttyent *t;
84623985Sdavidn
8471590Srgrimes	return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
8481590Srgrimes}
8491590Srgrimes
85021528Sdavidnvolatile int motdinterrupt;
8511590Srgrimes
85221528Sdavidn/* ARGSUSED */
8531590Srgrimesvoid
85421528Sdavidnsigint(signo)
85521528Sdavidn	int signo;
8561590Srgrimes{
85721528Sdavidn	motdinterrupt = 1;
85821528Sdavidn}
85921528Sdavidn
86021528Sdavidnvoid
86121528Sdavidnmotd(motdfile)
86221528Sdavidn	char *motdfile;
86321528Sdavidn{
8641590Srgrimes	int fd, nchars;
8651590Srgrimes	sig_t oldint;
86621528Sdavidn	char tbuf[256];
8671590Srgrimes
86821528Sdavidn	if ((fd = open(motdfile, O_RDONLY, 0)) < 0)
8691590Srgrimes		return;
87021528Sdavidn	motdinterrupt = 0;
8711590Srgrimes	oldint = signal(SIGINT, sigint);
87221528Sdavidn	while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0 && !motdinterrupt)
87321528Sdavidn		(void)write(fileno(stdout), tbuf, nchars);
8741590Srgrimes	(void)signal(SIGINT, oldint);
8751590Srgrimes	(void)close(fd);
8761590Srgrimes}
8771590Srgrimes
8781590Srgrimes/* ARGSUSED */
8791590Srgrimesvoid
8801590Srgrimestimedout(signo)
8811590Srgrimes	int signo;
8821590Srgrimes{
8831590Srgrimes	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
8841590Srgrimes	exit(0);
8851590Srgrimes}
8861590Srgrimes
88721528Sdavidn#ifndef LOGIN_CAP
8881590Srgrimesvoid
8891590Srgrimeschecknologin()
8901590Srgrimes{
8911590Srgrimes	int fd, nchars;
8921590Srgrimes	char tbuf[8192];
8931590Srgrimes
8941590Srgrimes	if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
8951590Srgrimes		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
8961590Srgrimes			(void)write(fileno(stdout), tbuf, nchars);
8971590Srgrimes		sleepexit(0);
8981590Srgrimes	}
8991590Srgrimes}
90021528Sdavidn#endif
9011590Srgrimes
9021590Srgrimesvoid
9031590Srgrimesdolastlog(quiet)
9041590Srgrimes	int quiet;
9051590Srgrimes{
9061590Srgrimes	struct lastlog ll;
9071590Srgrimes	int fd;
9081590Srgrimes
9091590Srgrimes	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
9101590Srgrimes		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
9111590Srgrimes		if (!quiet) {
9121590Srgrimes			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
9131590Srgrimes			    ll.ll_time != 0) {
9141590Srgrimes				(void)printf("Last login: %.*s ",
9151590Srgrimes				    24-5, (char *)ctime(&ll.ll_time));
9161590Srgrimes				if (*ll.ll_host != '\0')
9171590Srgrimes					(void)printf("from %.*s\n",
9181590Srgrimes					    (int)sizeof(ll.ll_host),
9191590Srgrimes					    ll.ll_host);
9201590Srgrimes				else
9211590Srgrimes					(void)printf("on %.*s\n",
9221590Srgrimes					    (int)sizeof(ll.ll_line),
9231590Srgrimes					    ll.ll_line);
9241590Srgrimes			}
9251590Srgrimes			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
9261590Srgrimes		}
9271590Srgrimes		memset((void *)&ll, 0, sizeof(ll));
9281590Srgrimes		(void)time(&ll.ll_time);
9291590Srgrimes		(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
9301590Srgrimes		if (hostname)
9311590Srgrimes			(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
9321590Srgrimes		(void)write(fd, (char *)&ll, sizeof(ll));
9331590Srgrimes		(void)close(fd);
9341590Srgrimes	}
9351590Srgrimes}
9361590Srgrimes
9371590Srgrimesvoid
9381590Srgrimesbadlogin(name)
9391590Srgrimes	char *name;
9401590Srgrimes{
9411590Srgrimes
9421590Srgrimes	if (failures == 0)
9431590Srgrimes		return;
9441590Srgrimes	if (hostname) {
9451590Srgrimes		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
94616423Sache		    failures, failures > 1 ? "S" : "", full_hostname);
9471590Srgrimes		syslog(LOG_AUTHPRIV|LOG_NOTICE,
9481590Srgrimes		    "%d LOGIN FAILURE%s FROM %s, %s",
94916423Sache		    failures, failures > 1 ? "S" : "", full_hostname, name);
9501590Srgrimes	} else {
9511590Srgrimes		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
9521590Srgrimes		    failures, failures > 1 ? "S" : "", tty);
9531590Srgrimes		syslog(LOG_AUTHPRIV|LOG_NOTICE,
9541590Srgrimes		    "%d LOGIN FAILURE%s ON %s, %s",
9551590Srgrimes		    failures, failures > 1 ? "S" : "", tty, name);
9561590Srgrimes	}
9571590Srgrimes}
9581590Srgrimes
9591590Srgrimes#undef	UNKNOWN
9601590Srgrimes#define	UNKNOWN	"su"
9611590Srgrimes
9621590Srgrimeschar *
9631590Srgrimesstypeof(ttyid)
9641590Srgrimes	char *ttyid;
9651590Srgrimes{
96623985Sdavidn
9671590Srgrimes	struct ttyent *t;
96823985Sdavidn
9691590Srgrimes	return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
9701590Srgrimes}
9711590Srgrimes
9721590Srgrimesvoid
97323985Sdavidnrefused(msg, rtype, lout)
97423985Sdavidn	char *msg;
97523985Sdavidn	char *rtype;
97623985Sdavidn	int lout;
97723985Sdavidn{
97823985Sdavidn
97923985Sdavidn	if (msg != NULL)
98023985Sdavidn	    printf("%s.\n", msg);
98123985Sdavidn	if (hostname)
98223985Sdavidn		syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s",
98323985Sdavidn		       pwd->pw_name, rtype, full_hostname, tty);
98423985Sdavidn	else
98523985Sdavidn		syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) ON TTY %s",
98623985Sdavidn		       pwd->pw_name, rtype, tty);
98723985Sdavidn	if (lout)
98823985Sdavidn		sleepexit(1);
98923985Sdavidn}
99023985Sdavidn
99123985Sdavidnvoid
9921590Srgrimessleepexit(eval)
9931590Srgrimes	int eval;
9941590Srgrimes{
99523985Sdavidn
9961590Srgrimes	(void)sleep(5);
9971590Srgrimes	exit(eval);
9981590Srgrimes}
999