login.c revision 30564
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
4127605Scharnier#if 0
421590Srgrimesstatic char sccsid[] = "@(#)login.c	8.4 (Berkeley) 4/2/94";
4327605Scharnier#endif
4427605Scharnierstatic const char rcsid[] =
4530564Sjoerg	"$Id: login.c,v 1.29 1997/09/28 08:49:22 markm Exp $";
461590Srgrimes#endif /* not lint */
471590Srgrimes
481590Srgrimes/*
491590Srgrimes * login [ name ]
501590Srgrimes * login -h hostname	(for telnetd, etc.)
511590Srgrimes * login -f name	(for pre-authenticated login: datakit, xterm, etc.)
521590Srgrimes */
531590Srgrimes
5423246Swosch#include <sys/copyright.h>
551590Srgrimes#include <sys/param.h>
561590Srgrimes#include <sys/stat.h>
571590Srgrimes#include <sys/time.h>
581590Srgrimes#include <sys/resource.h>
591590Srgrimes#include <sys/file.h>
6016423Sache#include <netinet/in.h>
6116423Sache#include <arpa/inet.h>
621590Srgrimes
631590Srgrimes#include <err.h>
641590Srgrimes#include <errno.h>
651590Srgrimes#include <grp.h>
6616423Sache#include <netdb.h>
671590Srgrimes#include <pwd.h>
681590Srgrimes#include <setjmp.h>
691590Srgrimes#include <signal.h>
701590Srgrimes#include <stdio.h>
711590Srgrimes#include <stdlib.h>
721590Srgrimes#include <string.h>
731590Srgrimes#include <syslog.h>
741590Srgrimes#include <ttyent.h>
751590Srgrimes#include <unistd.h>
761590Srgrimes#include <utmp.h>
771590Srgrimes
7821528Sdavidn#ifdef LOGIN_CAP
7921528Sdavidn#include <login_cap.h>
8021528Sdavidn#else /* Undef AUTH as well */
8121528Sdavidn#undef LOGIN_CAP_AUTH
8221528Sdavidn#endif
8321528Sdavidn
8423985Sdavidn/*
8523985Sdavidn * If LOGIN_CAP_AUTH is activated:
8621528Sdavidn * kerberose & skey logins are runtime selected via login
8721528Sdavidn * login_getstyle() and authentication types for login classes
8821528Sdavidn * The actual login itself is handled via /usr/libexec/login_<style>
8921528Sdavidn * Valid styles are determined by the auth-type=style,style entries
9021528Sdavidn * in the login class.
9121528Sdavidn */
9221528Sdavidn#ifdef LOGIN_CAP_AUTH
9321528Sdavidn#undef KERBEROS
9421528Sdavidn#undef SKEY
9523985Sdavidn#endif /* LOGIN_CAP_AUTH */
9623985Sdavidn
973702Spst#ifdef	SKEY
983702Spst#include <skey.h>
9921528Sdavidn#endif /* SKEY */
1003702Spst
1011590Srgrimes#include "pathnames.h"
1021590Srgrimes
1031590Srgrimesvoid	 badlogin __P((char *));
1041590Srgrimesvoid	 checknologin __P((void));
1051590Srgrimesvoid	 dolastlog __P((int));
1061590Srgrimesvoid	 getloginname __P((void));
10721528Sdavidnvoid	 motd __P((char *));
1081590Srgrimesint	 rootterm __P((char *));
1091590Srgrimesvoid	 sigint __P((int));
1101590Srgrimesvoid	 sleepexit __P((int));
11123985Sdavidnvoid	 refused __P((char *,char *,int));
1121590Srgrimeschar	*stypeof __P((char *));
1131590Srgrimesvoid	 timedout __P((int));
11429922Smarkmint	 login_access __P((char *, char *));
1152224Sguidovoid     login_fbtab __P((char *, uid_t, gid_t));
1161590Srgrimes#ifdef KERBEROS
1171590Srgrimesint	 klogin __P((struct passwd *, char *, char *, char *));
1181590Srgrimes#endif
1191590Srgrimes
1201590Srgrimesextern void login __P((struct utmp *));
12127605Scharnierstatic void usage __P((void));
1221590Srgrimes
1231590Srgrimes#define	TTYGRPNAME	"tty"		/* name of group to own ttys */
12423985Sdavidn#define	DEFAULT_BACKOFF	3
12523985Sdavidn#define	DEFAULT_RETRIES	10
1261590Srgrimes
1271590Srgrimes/*
1281590Srgrimes * This bounds the time given to login.  Not a define so it can
1291590Srgrimes * be patched on machines where it's too small.
1301590Srgrimes */
1311590Srgrimesu_int	timeout = 300;
1321590Srgrimes
1331590Srgrimes#ifdef KERBEROS
1341590Srgrimesint	notickets = 1;
1355627Swollmanint	noticketsdontcomplain = 1;
1361590Srgrimeschar	*instance;
1371590Srgrimeschar	*krbtkfile_env;
1381590Srgrimesint	authok;
1391590Srgrimes#endif
1401590Srgrimes
1411590Srgrimesstruct	passwd *pwd;
1421590Srgrimesint	failures;
14323985Sdavidnchar	*term, *envinit[1], *hostname, *username, *tty;
14416423Sachechar    full_hostname[MAXHOSTNAMELEN];
1451590Srgrimes
1461590Srgrimesint
1471590Srgrimesmain(argc, argv)
1481590Srgrimes	int argc;
1491590Srgrimes	char *argv[];
1501590Srgrimes{
1511590Srgrimes	extern char **environ;
1521590Srgrimes	struct group *gr;
1531590Srgrimes	struct stat st;
1541590Srgrimes	struct timeval tp;
1551590Srgrimes	struct utmp utmp;
15623985Sdavidn	int rootok, retries, backoff;
1571590Srgrimes	int ask, ch, cnt, fflag, hflag, pflag, quietlog, rootlogin, rval;
1584878Sugen	int changepass;
15923985Sdavidn	time_t warntime;
1601590Srgrimes	uid_t uid;
1613205Spst	char *domain, *p, *ep, *salt, *ttyn;
1621590Srgrimes	char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
1631590Srgrimes	char localhost[MAXHOSTNAMELEN];
16423985Sdavidn	char *shell = NULL;
16521528Sdavidn#ifdef LOGIN_CAP
16621528Sdavidn	login_cap_t *lc = NULL;
16721528Sdavidn#ifdef LOGIN_CAP_AUTH
16821528Sdavidn	char *style, *authtype;
16921528Sdavidn	char *auth_method = NULL;
17021528Sdavidn	char *instance = NULL;
17121528Sdavidn	int authok;
17223985Sdavidn#endif /* LOGIN_CAP_AUTH */
17323985Sdavidn#endif /* LOGIN_CAP */
17421528Sdavidn#ifdef SKEY
1753205Spst	int permit_passwd = 0;
17621528Sdavidn#endif /* SKEY */
1771590Srgrimes
1781590Srgrimes	(void)signal(SIGALRM, timedout);
1791590Srgrimes	(void)alarm(timeout);
1801590Srgrimes	(void)signal(SIGQUIT, SIG_IGN);
1811590Srgrimes	(void)signal(SIGINT, SIG_IGN);
1821590Srgrimes	(void)setpriority(PRIO_PROCESS, 0, 0);
1831590Srgrimes
1841590Srgrimes	openlog("login", LOG_ODELAY, LOG_AUTH);
1851590Srgrimes
1861590Srgrimes	/*
1871590Srgrimes	 * -p is used by getty to tell login not to destroy the environment
1881590Srgrimes	 * -f is used to skip a second login authentication
1891590Srgrimes	 * -h is used by other servers to pass the name of the remote
1901590Srgrimes	 *    host to login so that it may be placed in utmp and wtmp
1911590Srgrimes	 */
1923205Spst	*full_hostname = '\0';
1931590Srgrimes	domain = NULL;
19423985Sdavidn	term = NULL;
1951590Srgrimes	if (gethostname(localhost, sizeof(localhost)) < 0)
1961590Srgrimes		syslog(LOG_ERR, "couldn't get local hostname: %m");
1971590Srgrimes	else
1981590Srgrimes		domain = strchr(localhost, '.');
1991590Srgrimes
2001590Srgrimes	fflag = hflag = pflag = 0;
2011590Srgrimes	uid = getuid();
20224360Simp	while ((ch = getopt(argc, argv, "fh:p")) != -1)
2031590Srgrimes		switch (ch) {
2041590Srgrimes		case 'f':
2051590Srgrimes			fflag = 1;
2061590Srgrimes			break;
2071590Srgrimes		case 'h':
2081590Srgrimes			if (uid)
2091590Srgrimes				errx(1, "-h option: %s", strerror(EPERM));
2101590Srgrimes			hflag = 1;
2113205Spst			strncpy(full_hostname, optarg, sizeof(full_hostname)-1);
2121590Srgrimes			if (domain && (p = strchr(optarg, '.')) &&
2131590Srgrimes			    strcasecmp(p, domain) == 0)
2141590Srgrimes				*p = 0;
21516423Sache			if (strlen(optarg) > UT_HOSTSIZE) {
21616423Sache				struct hostent *hp = gethostbyname(optarg);
21716423Sache
21816423Sache				if (hp != NULL) {
21916423Sache					struct in_addr in;
22016423Sache
22116423Sache					memmove(&in, hp->h_addr, sizeof(in));
22216423Sache					optarg = strdup(inet_ntoa(in));
22316423Sache				} else
22416423Sache					optarg = "invalid hostname";
22516423Sache			}
2261590Srgrimes			hostname = optarg;
2271590Srgrimes			break;
2281590Srgrimes		case 'p':
2291590Srgrimes			pflag = 1;
2301590Srgrimes			break;
2311590Srgrimes		case '?':
2321590Srgrimes		default:
2331590Srgrimes			if (!uid)
2341590Srgrimes				syslog(LOG_ERR, "invalid flag %c", ch);
23527605Scharnier			usage();
2361590Srgrimes		}
2371590Srgrimes	argc -= optind;
2381590Srgrimes	argv += optind;
2391590Srgrimes
2401590Srgrimes	if (*argv) {
2411590Srgrimes		username = *argv;
2421590Srgrimes		ask = 0;
2431590Srgrimes	} else
2441590Srgrimes		ask = 1;
2451590Srgrimes
2461590Srgrimes	for (cnt = getdtablesize(); cnt > 2; cnt--)
2471590Srgrimes		(void)close(cnt);
2481590Srgrimes
2491590Srgrimes	ttyn = ttyname(STDIN_FILENO);
2501590Srgrimes	if (ttyn == NULL || *ttyn == '\0') {
2511590Srgrimes		(void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
2521590Srgrimes		ttyn = tname;
2531590Srgrimes	}
25423985Sdavidn	if ((tty = strrchr(ttyn, '/')) != NULL)
2551590Srgrimes		++tty;
2561590Srgrimes	else
2571590Srgrimes		tty = ttyn;
2581590Srgrimes
25921528Sdavidn#ifdef LOGIN_CAP_AUTH
26021528Sdavidn	authtype = hostname ? "rlogin" : "login";
26121528Sdavidn#endif
26223985Sdavidn#ifdef LOGIN_CAP
26323985Sdavidn	/*
26423985Sdavidn	 * Get "login-retries" & "login-backoff" from default class
26523985Sdavidn	 */
26623985Sdavidn	lc = login_getclass(NULL);
26723985Sdavidn	retries = login_getcapnum(lc, "login-retries", DEFAULT_RETRIES, DEFAULT_RETRIES);
26823985Sdavidn	backoff = login_getcapnum(lc, "login-backoff", DEFAULT_BACKOFF, DEFAULT_BACKOFF);
26923985Sdavidn	login_close(lc);
27023985Sdavidn	lc = NULL;
27123985Sdavidn#else
27223985Sdavidn	retries = DEFAULT_RETRIES;
27323985Sdavidn	backoff = DEFAULT_BACKOFF;
27423985Sdavidn#endif
27521528Sdavidn
2761590Srgrimes	for (cnt = 0;; ask = 1) {
2771590Srgrimes		if (ask) {
2781590Srgrimes			fflag = 0;
2791590Srgrimes			getloginname();
2801590Srgrimes		}
2811590Srgrimes		rootlogin = 0;
28221528Sdavidn		rootok = rootterm(tty); /* Default (auth may change) */
28321528Sdavidn#ifdef LOGIN_CAP_AUTH
28421528Sdavidn		authok = 0;
28521528Sdavidn		if (auth_method = strchr(username, ':')) {
28621528Sdavidn			*auth_method = '\0';
28721528Sdavidn			auth_method++;
28821528Sdavidn			if (*auth_method == '\0')
28921528Sdavidn				auth_method = NULL;
29021528Sdavidn		}
29121528Sdavidn		/*
29221528Sdavidn		 * We need to do this regardless of whether
29321528Sdavidn		 * kerberos is available.
29421528Sdavidn		 */
2951590Srgrimes		if ((instance = strchr(username, '.')) != NULL) {
2961590Srgrimes			if (strncmp(instance, ".root", 5) == 0)
2971590Srgrimes				rootlogin = 1;
2981590Srgrimes			*instance++ = '\0';
2991590Srgrimes		} else
3001590Srgrimes			instance = "";
30121528Sdavidn#else /* !LOGIN_CAP_AUTH */
30221528Sdavidn#ifdef KERBEROS
30321528Sdavidn		if ((instance = strchr(username, '.')) != NULL) {
30421528Sdavidn			if (strncmp(instance, ".root", 5) == 0)
30521528Sdavidn				rootlogin = 1;
30621528Sdavidn			*instance++ = '\0';
30721528Sdavidn		} else
30821528Sdavidn			instance = "";
30921528Sdavidn#endif /* KERBEROS */
31021528Sdavidn#endif /* LOGIN_CAP_AUTH */
31121528Sdavidn
3121590Srgrimes		if (strlen(username) > UT_NAMESIZE)
3131590Srgrimes			username[UT_NAMESIZE] = '\0';
3141590Srgrimes
3151590Srgrimes		/*
3161590Srgrimes		 * Note if trying multiple user names; log failures for
3171590Srgrimes		 * previous user name, but don't bother logging one failure
3181590Srgrimes		 * for nonexistent name (mistyped username).
3191590Srgrimes		 */
3201590Srgrimes		if (failures && strcmp(tbuf, username)) {
3211590Srgrimes			if (failures > (pwd ? 0 : 1))
3221590Srgrimes				badlogin(tbuf);
3231590Srgrimes			failures = 0;
3241590Srgrimes		}
32527605Scharnier		(void)strncpy(tbuf, username, sizeof tbuf-1);
32627605Scharnier		tbuf[sizeof tbuf-1] = '\0';
3271590Srgrimes
32823985Sdavidn		if ((pwd = getpwnam(username)) != NULL)
32923985Sdavidn			salt = pwd->pw_passwd;
33023985Sdavidn		else
33123985Sdavidn			salt = "xx";
33223985Sdavidn
33321528Sdavidn#ifdef LOGIN_CAP
33423985Sdavidn		/*
33523985Sdavidn		 * Establish the class now, before we might goto
33621528Sdavidn		 * within the next block. pwd can be NULL since it
33721528Sdavidn		 * falls back to the "default" class if it is.
33821528Sdavidn		 */
33925671Sdavidn		lc = login_getpwclass(pwd);
34021528Sdavidn#endif /* LOGIN_CAP */
3411590Srgrimes
3421590Srgrimes		/*
3431590Srgrimes		 * if we have a valid account name, and it doesn't have a
3441590Srgrimes		 * password, or the -f option was specified and the caller
3451590Srgrimes		 * is root or the caller isn't changing their uid, don't
3461590Srgrimes		 * authenticate.
3471590Srgrimes		 */
34823985Sdavidn		rval = 1;
34921528Sdavidn		if (pwd != NULL) {
3503205Spst			if (pwd->pw_uid == 0)
3513205Spst				rootlogin = 1;
3523205Spst
35323985Sdavidn			if (fflag && (uid == (uid_t)0 ||
35423985Sdavidn				      uid == (uid_t)pwd->pw_uid)) {
3553205Spst				/* already authenticated */
3563205Spst				break;
3573205Spst			} else if (pwd->pw_passwd[0] == '\0') {
35824321Sdavidn				if (!rootlogin || rootok) {
35924251Sdavidn					/* pretend password okay */
36024251Sdavidn					rval = 0;
36124251Sdavidn					goto ttycheck;
36224251Sdavidn				}
3633205Spst			}
3643205Spst		}
3653205Spst
3661590Srgrimes		fflag = 0;
3671590Srgrimes
3681590Srgrimes		(void)setpriority(PRIO_PROCESS, 0, -4);
3691590Srgrimes
37021528Sdavidn#ifdef LOGIN_CAP_AUTH
37121528Sdavidn		/*
37223985Sdavidn		 * This hands off authorization to an authorization program,
37321528Sdavidn		 * depending on the styles available for the "auth-login",
37423985Sdavidn		 * auth-rlogin (or default) authorization styles.
37521528Sdavidn		 * We do this regardless of whether an account exists so that
37621528Sdavidn		 * the remote user cannot tell a "real" from an invented
37721528Sdavidn		 * account name. If we don't have an account we just fall
37821528Sdavidn		 * back to the first method for the "default" class.
37921528Sdavidn		 */
38023985Sdavidn		if (!(style = login_getstyle(lc, auth_method, authtype))) {
38123985Sdavidn
38223985Sdavidn			/*
38323985Sdavidn			 * No available authorization method
38423985Sdavidn			 */
38523985Sdavidn			rval = 1;
38623985Sdavidn			(void)printf("No auth method available for %s.\n",
38723985Sdavidn				     authtype);
38821528Sdavidn		} else {
38923985Sdavidn
39023985Sdavidn			/*
39123985Sdavidn			 * Put back the kerberos instance, if any was given.
39221528Sdavidn			 * Don't worry about the non-kerberos case here, since
39321528Sdavidn			 * if kerberos is not available or not selected and an
39421528Sdavidn			 * instance is given at the login prompt, su or rlogin -l,
39521528Sdavidn			 * then anything else should fail as well.
39621528Sdavidn			 */
39721528Sdavidn			if (*instance)
39821528Sdavidn				*(instance - 1) = '.';
39923985Sdavidn
40023985Sdavidn			rval = authenticate(username,
40123985Sdavidn					    lc ? lc->lc_class : "default",
40223985Sdavidn					    style, authtype);
40321528Sdavidn			/* Junk it again */
40421528Sdavidn			if (*instance)
40521528Sdavidn				*(instance - 1) = '\0';
40621528Sdavidn		}
40721528Sdavidn
40821528Sdavidn		if (!rval) {
40923985Sdavidn			char * approvp;
41023985Sdavidn
41121528Sdavidn			/*
41221528Sdavidn			 * If authentication succeeds, run any approval
41321528Sdavidn			 * program, if applicable for this class.
41421528Sdavidn			 */
41523985Sdavidn			approvep = login_getcapstr(lc, "approve", NULL, NULL);
41621528Sdavidn			rval = 1; /* Assume bad login again */
41723985Sdavidn
41823985Sdavidn			if (approvep==NULL ||
41923985Sdavidn			    auth_script(approvep, approvep, username,
42023985Sdavidn					lc->lc_class, 0) == 0) {
42123985Sdavidn				int     r;
42223985Sdavidn
42323985Sdavidn				r = auth_scan(AUTH_OKAY);
42423985Sdavidn				/*
42523985Sdavidn				 * See what the authorize program says
42623985Sdavidn				 */
42721528Sdavidn				if (r != AUTH_NONE) {
42821528Sdavidn					rval = 0;
42923985Sdavidn
43021528Sdavidn					if (!rootok && (r & AUTH_ROOTOKAY))
43121528Sdavidn						rootok = 1; /* root approved */
43223985Sdavidn					else
43323985Sdavidn						rootlogin = 0;
43423985Sdavidn
43521528Sdavidn					if (!authok && (r & AUTH_SECURE))
43621528Sdavidn						authok = 1; /* secure */
43721528Sdavidn				}
43821528Sdavidn			}
43921528Sdavidn		}
44021528Sdavidn#else /* !LOGIN_CAP_AUTH */
44121528Sdavidn#ifdef SKEY
44223985Sdavidn		permit_passwd = skeyaccess(username, tty,
44323985Sdavidn					   hostname ? full_hostname : NULL,
44423985Sdavidn					   NULL);
4453205Spst		p = skey_getpass("Password:", pwd, permit_passwd);
4463205Spst		ep = skey_crypt(p, salt, pwd, permit_passwd);
44721528Sdavidn#else /* !SKEY */
4481590Srgrimes		p = getpass("Password:");
4493205Spst		ep = crypt(p, salt);
45021528Sdavidn#endif/* SKEY */
45123985Sdavidn
45223985Sdavidn		if (pwd) {
4531590Srgrimes#ifdef KERBEROS
4547800Swollman#ifdef SKEY
45523985Sdavidn			/*
45623985Sdavidn			 * Do not allow user to type in kerberos password
4577800Swollman			 * over the net (actually, this is ok for encrypted
4587800Swollman			 * links, but we have no way of determining if the
4597800Swollman			 * link is encrypted.
4607800Swollman			 */
4617893Srgrimes			if (!permit_passwd) {
46223985Sdavidn				rval = 1;		/* failed */
4637800Swollman			} else
46421528Sdavidn#endif /* SKEY */
4651590Srgrimes			rval = klogin(pwd, instance, localhost, p);
4661590Srgrimes			if (rval != 0 && rootlogin && pwd->pw_uid != 0)
4671590Srgrimes				rootlogin = 0;
4681590Srgrimes			if (rval == 0)
46921528Sdavidn				authok = 1; /* kerberos authenticated ok */
47021528Sdavidn			else if (rval == 1) /* fallback to unix passwd */
4713205Spst				rval = strcmp(ep, pwd->pw_passwd);
47221528Sdavidn#else /* !KERBEROS */
47323985Sdavidn			rval = strcmp(ep, pwd->pw_passwd);
47421528Sdavidn#endif /* KERBEROS */
47523985Sdavidn		}
47623985Sdavidn
47721528Sdavidn		/* clear entered password */
4781590Srgrimes		memset(p, 0, strlen(p));
47921528Sdavidn#endif /* LOGIN_CAP_AUTH */
4801590Srgrimes
4811590Srgrimes		(void)setpriority(PRIO_PROCESS, 0, 0);
48223985Sdavidn
48325671Sdavidn#ifdef LOGIN_CAP_AUTH
48421528Sdavidn		if (rval)
48521528Sdavidn			auth_rmfiles();
48621528Sdavidn#endif
4873205Spst	ttycheck:
4881590Srgrimes		/*
4891590Srgrimes		 * If trying to log in as root without Kerberos,
4901590Srgrimes		 * but with insecure terminal, refuse the login attempt.
4911590Srgrimes		 */
49224222Sdavidn		if (pwd && !rval) {
49321528Sdavidn#if defined(KERBEROS) || defined(LOGIN_CAP_AUTH)
49424222Sdavidn			if (authok == 0 && rootlogin && !rootok)
49524222Sdavidn#else
49624222Sdavidn			if (rootlogin && !rootok)
4971590Srgrimes#endif
49824222Sdavidn				refused(NULL, "NOROOT", 0);
49924222Sdavidn			else	/* valid password & authenticated */
50024222Sdavidn				break;
5011590Srgrimes		}
5021590Srgrimes
5031590Srgrimes		(void)printf("Login incorrect\n");
5041590Srgrimes		failures++;
50523985Sdavidn
50623985Sdavidn		/*
50723985Sdavidn		 * we allow up to 'retry' (10) tries,
50823985Sdavidn		 * but after 'backoff' (3) we start backing off
50923985Sdavidn		 */
51023985Sdavidn		if (++cnt > backoff) {
51123985Sdavidn			if (cnt >= retries) {
5121590Srgrimes				badlogin(username);
5131590Srgrimes				sleepexit(1);
5141590Srgrimes			}
5151590Srgrimes			sleep((u_int)((cnt - 3) * 5));
5161590Srgrimes		}
5171590Srgrimes	}
5181590Srgrimes
5191590Srgrimes	/* committed to login -- turn off timeout */
5201590Srgrimes	(void)alarm((u_int)0);
5211590Srgrimes
5221590Srgrimes	endpwent();
5231590Srgrimes
5241590Srgrimes	/* if user not super-user, check for disabled logins */
52521528Sdavidn#ifdef LOGIN_CAP
5261590Srgrimes	if (!rootlogin)
52721528Sdavidn		auth_checknologin(lc);
52821528Sdavidn#else
52921528Sdavidn	if (!rootlogin)
5301590Srgrimes		checknologin();
53121528Sdavidn#endif
5321590Srgrimes
53321528Sdavidn#ifdef LOGIN_CAP
53421528Sdavidn	quietlog = login_getcapbool(lc, "hushlogin", 0);
53521528Sdavidn#else
53621528Sdavidn	quietlog = 0;
53721528Sdavidn#endif
53821528Sdavidn	if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) {
53921528Sdavidn#ifdef LOGIN_CAP
54026021Spst		if (login_getcapbool(lc, "requirehome", 0))
54123985Sdavidn			refused("Home directory not available", "HOMEDIR", 1);
54223985Sdavidn#endif
54324485Sdavidn		if (chdir("/") < 0)
54423985Sdavidn			refused("Cannot find root directory", "ROOTDIR", 1);
5451590Srgrimes		pwd->pw_dir = "/";
54623985Sdavidn		if (!quietlog || *pwd->pw_dir)
54723985Sdavidn			printf("No home directory.\nLogging in with home = \"/\".\n");
5481590Srgrimes	}
54921528Sdavidn	if (!quietlog)
55021528Sdavidn		quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
5511590Srgrimes
5521590Srgrimes	if (pwd->pw_change || pwd->pw_expire)
5531590Srgrimes		(void)gettimeofday(&tp, (struct timezone *)NULL);
5542532Sjkh
55530564Sjoerg#define DEFAULT_WARN  (2L * 7L * 86400L)  /* Two weeks */
55621528Sdavidn
55723985Sdavidn#ifdef LOGIN_CAP
55823985Sdavidn	warntime = login_getcaptime(lc, "warnpassword",
55923985Sdavidn				    DEFAULT_WARN, DEFAULT_WARN);
56023985Sdavidn#else
56123985Sdavidn	warntime = DEFAULT_WARN;
56223985Sdavidn#endif
56323985Sdavidn
5644878Sugen	changepass=0;
56521528Sdavidn	if (pwd->pw_change) {
5661590Srgrimes		if (tp.tv_sec >= pwd->pw_change) {
5671590Srgrimes			(void)printf("Sorry -- your password has expired.\n");
5684878Sugen			changepass=1;
56923985Sdavidn			syslog(LOG_INFO,
57023985Sdavidn			       "%s Password expired - forcing change",
57123985Sdavidn			       pwd->pw_name);
57223985Sdavidn		} else if (pwd->pw_change - tp.tv_sec < warntime && !quietlog)
57323985Sdavidn		    (void)printf("Warning: your password expires on %s",
57423985Sdavidn				 ctime(&pwd->pw_change));
57523985Sdavidn	}
57623985Sdavidn
57721528Sdavidn#ifdef LOGIN_CAP
57823985Sdavidn	warntime = login_getcaptime(lc, "warnexpire",
57923985Sdavidn				    DEFAULT_WARN, DEFAULT_WARN);
58021528Sdavidn#else
58123985Sdavidn	warntime = DEFAULT_WARN;
58221528Sdavidn#endif
58323985Sdavidn
58421528Sdavidn	if (pwd->pw_expire) {
5851590Srgrimes		if (tp.tv_sec >= pwd->pw_expire) {
58623985Sdavidn			refused("Sorry -- your account has expired",
58723985Sdavidn				"EXPIRED", 1);
58823985Sdavidn		} else if (pwd->pw_expire - tp.tv_sec < warntime && !quietlog)
58923985Sdavidn		    (void)printf("Warning: your account expires on %s",
59023985Sdavidn				 ctime(&pwd->pw_expire));
59121528Sdavidn	}
5921590Srgrimes
59321528Sdavidn#ifdef LOGIN_CAP
59421528Sdavidn	if (lc != NULL) {
59521528Sdavidn		if (hostname) {
59621528Sdavidn			struct hostent *hp = gethostbyname(full_hostname);
59721528Sdavidn
59821528Sdavidn			if (hp == NULL)
59921528Sdavidn				optarg = NULL;
60021528Sdavidn			else {
60121528Sdavidn				struct in_addr in;
60221528Sdavidn				memmove(&in, hp->h_addr, sizeof(in));
60321528Sdavidn				optarg = strdup(inet_ntoa(in));
60421528Sdavidn			}
60523985Sdavidn			if (!auth_hostok(lc, full_hostname, optarg))
60623985Sdavidn				refused("Permission denied", "HOST", 1);
60721528Sdavidn		}
60821528Sdavidn
60923985Sdavidn		if (!auth_ttyok(lc, tty))
61023985Sdavidn			refused("Permission denied", "TTY", 1);
61121528Sdavidn
61223985Sdavidn		if (!auth_timeok(lc, time(NULL)))
61323985Sdavidn			refused("Logins not available right now", "TIME", 1);
61421528Sdavidn	}
61523985Sdavidn        shell=login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
61621528Sdavidn#else /* !LOGIN_CAP */
61723985Sdavidn       shell=pwd->pw_shell;
61821528Sdavidn#endif /* LOGIN_CAP */
61923985Sdavidn	if (*pwd->pw_shell == '\0')
62023985Sdavidn		pwd->pw_shell = _PATH_BSHELL;
62123985Sdavidn	if (*shell == '\0')   /* Not overridden */
62223985Sdavidn		shell = pwd->pw_shell;
62323985Sdavidn	if ((shell = strdup(shell)) == NULL) {
62423985Sdavidn		syslog(LOG_NOTICE, "memory allocation error");
62523985Sdavidn		sleepexit(1);
62623985Sdavidn	}
62721528Sdavidn
62821528Sdavidn#ifdef LOGIN_ACCESS
62923985Sdavidn	if (login_access(pwd->pw_name, hostname ? full_hostname : tty) == 0)
63023985Sdavidn		refused("Permission denied", "ACCESS", 1);
63121528Sdavidn#endif /* LOGIN_ACCESS */
63221528Sdavidn
6331590Srgrimes	/* Nothing else left to fail -- really log in. */
6341590Srgrimes	memset((void *)&utmp, 0, sizeof(utmp));
6351590Srgrimes	(void)time(&utmp.ut_time);
6361590Srgrimes	(void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
6371590Srgrimes	if (hostname)
6381590Srgrimes		(void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
6391590Srgrimes	(void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
6401590Srgrimes	login(&utmp);
6411590Srgrimes
6421590Srgrimes	dolastlog(quietlog);
6431590Srgrimes
6442224Sguido	/*
6452224Sguido	 * Set device protections, depending on what terminal the
6462224Sguido	 * user is logged in. This feature is used on Suns to give
6472224Sguido	 * console users better privacy.
6482224Sguido	 */
6492224Sguido	login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
6502224Sguido
65123985Sdavidn	(void)chown(ttyn, pwd->pw_uid,
65223985Sdavidn		    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
6531590Srgrimes
65423985Sdavidn	/*
65523985Sdavidn	 * Preserve TERM if it happens to be already set.
65623985Sdavidn	 */
65724222Sdavidn	if ((term = getenv("TERM")) != NULL)
65824222Sdavidn		term = strdup(term);
6591590Srgrimes
66023985Sdavidn	/*
66123985Sdavidn	 * Exclude cons/vt/ptys only, assume dialup otherwise
66223985Sdavidn	 * TODO: Make dialup tty determination a library call
66323985Sdavidn	 * for consistency (finger etc.)
66423985Sdavidn	 */
66524894Sdavidn	if (hostname==NULL && isdialuptty(tty))
6661590Srgrimes		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
6671590Srgrimes
6681590Srgrimes#ifdef KERBEROS
6695627Swollman	if (!quietlog && notickets == 1 && !noticketsdontcomplain)
6701590Srgrimes		(void)printf("Warning: no Kerberos tickets issued.\n");
6711590Srgrimes#endif
6721590Srgrimes
6733205Spst#ifdef LOGALL
6743205Spst	/*
6753205Spst	 * Syslog each successful login, so we don't have to watch hundreds
6763205Spst	 * of wtmp or lastlogin files.
6773205Spst	 */
67823985Sdavidn	if (hostname)
67923985Sdavidn		syslog(LOG_INFO, "login from %s on %s as %s",
68023985Sdavidn		       full_hostname, tty, pwd->pw_name);
68123985Sdavidn	else
68223985Sdavidn		syslog(LOG_INFO, "login on %s as %s",
68323985Sdavidn		       tty, pwd->pw_name);
68421528Sdavidn#endif
68521528Sdavidn
68623985Sdavidn	/*
68723985Sdavidn	 * If fflag is on, assume caller/authenticator has logged root login.
68823985Sdavidn	 */
68923985Sdavidn	if (rootlogin && fflag == 0)
69023985Sdavidn	{
69123985Sdavidn		if (hostname)
69223985Sdavidn			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
69323985Sdavidn			       username, tty, full_hostname);
69423985Sdavidn		else
69523985Sdavidn			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
69623985Sdavidn			       username, tty);
69723985Sdavidn	}
69823985Sdavidn
69923985Sdavidn	/*
70023985Sdavidn	 * Destroy environment unless user has requested its preservation.
70123985Sdavidn	 * We need to do this before setusercontext() because that may
70223985Sdavidn	 * set or reset some environment variables.
70323985Sdavidn	 */
70421528Sdavidn	if (!pflag)
70521528Sdavidn		environ = envinit;
70621528Sdavidn
70723985Sdavidn	/*
70823985Sdavidn	 * We don't need to be root anymore, so
70921528Sdavidn	 * set the user and session context
71021528Sdavidn	 */
71121528Sdavidn#ifdef LOGIN_CAP
71221528Sdavidn	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
71321528Sdavidn                syslog(LOG_ERR, "setusercontext() failed - exiting");
71421528Sdavidn		exit(1);
7153205Spst	}
71621528Sdavidn#else
71721528Sdavidn     	if (setlogin(pwd->pw_name) < 0)
71821528Sdavidn                syslog(LOG_ERR, "setlogin() failure: %m");
71921528Sdavidn
72021528Sdavidn	(void)setgid(pwd->pw_gid);
72121528Sdavidn	initgroups(username, pwd->pw_gid);
72221528Sdavidn	(void)setuid(rootlogin ? 0 : pwd->pw_uid);
7233205Spst#endif
7243205Spst
72523148Sache	(void)setenv("SHELL", pwd->pw_shell, 1);
72621528Sdavidn	(void)setenv("HOME", pwd->pw_dir, 1);
72723985Sdavidn	if (term != NULL && *term != '\0')
72821528Sdavidn		(void)setenv("TERM", term, 1);	/* Preset overrides */
72921528Sdavidn	else {
73023985Sdavidn		(void)setenv("TERM", stypeof(tty), 0);	/* Fallback doesn't */
73121528Sdavidn	}
73221528Sdavidn	(void)setenv("LOGNAME", pwd->pw_name, 1);
73321528Sdavidn	(void)setenv("USER", pwd->pw_name, 1);
73421528Sdavidn	(void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
73521528Sdavidn#ifdef KERBEROS
73621528Sdavidn	if (krbtkfile_env)
73721528Sdavidn		(void)setenv("KRBTKFILE", krbtkfile_env, 1);
73821528Sdavidn#endif
73923985Sdavidn#if LOGIN_CAP_AUTH
74023985Sdavidn	auth_env();
74121528Sdavidn#endif
74221528Sdavidn
74323985Sdavidn#ifdef LOGIN_CAP
7441590Srgrimes	if (!quietlog) {
74523985Sdavidn		char	*cw;
74623985Sdavidn
74723985Sdavidn		cw = login_getcapstr(lc, "copyright", NULL, NULL);
74821528Sdavidn		if (cw != NULL && access(cw, F_OK) == 0)
74921528Sdavidn			motd(cw);
75021528Sdavidn		else
75123985Sdavidn		    (void)printf("%s\n\t%s %s\n",
75223985Sdavidn	"Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
75323985Sdavidn	"The Regents of the University of California. ",
75423985Sdavidn	"All rights reserved.");
75523985Sdavidn
75623985Sdavidn		(void)printf("\n");
75723985Sdavidn
75821528Sdavidn		cw = login_getcapstr(lc, "welcome", NULL, NULL);
75921528Sdavidn		if (cw == NULL || access(cw, F_OK) != 0)
76021528Sdavidn			cw = _PATH_MOTDFILE;
76121528Sdavidn		motd(cw);
76223985Sdavidn
76321528Sdavidn		cw = getenv("MAIL");	/* $MAIL may have been set by class */
76421528Sdavidn		if (cw != NULL) {
76521528Sdavidn			strncpy(tbuf, cw, sizeof(tbuf));
76621528Sdavidn			tbuf[sizeof(tbuf)-1] = '\0';
76721528Sdavidn		} else
76823985Sdavidn			snprintf(tbuf, sizeof(tbuf), "%s/%s",
76923985Sdavidn				 _PATH_MAILDIR, pwd->pw_name);
77021528Sdavidn#else
77123985Sdavidn	if (!quietlog) {
77223985Sdavidn		    (void)printf("%s\n\t%s %s\n",
77323985Sdavidn	"Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
77423985Sdavidn	"The Regents of the University of California. ",
77523985Sdavidn	"All rights reserved.");
77621528Sdavidn		motd(_PATH_MOTDFILE);
77723985Sdavidn		snprintf(tbuf, sizeof(tbuf), "%s/%s",
77823985Sdavidn			 _PATH_MAILDIR, pwd->pw_name);
77921528Sdavidn#endif
7801590Srgrimes		if (stat(tbuf, &st) == 0 && st.st_size != 0)
78123985Sdavidn			(void)printf("You have %smail.\n",
78223985Sdavidn				     (st.st_mtime > st.st_atime) ? "new " : "");
7831590Srgrimes	}
7841590Srgrimes
78521528Sdavidn#ifdef LOGIN_CAP
78621528Sdavidn	login_close(lc);
7873205Spst#endif
7883205Spst
7891590Srgrimes	(void)signal(SIGALRM, SIG_DFL);
7901590Srgrimes	(void)signal(SIGQUIT, SIG_DFL);
7911590Srgrimes	(void)signal(SIGINT, SIG_DFL);
7921590Srgrimes	(void)signal(SIGTSTP, SIG_IGN);
7931590Srgrimes
7944878Sugen	if (changepass) {
79521528Sdavidn		if (system(_PATH_CHPASS) != 0)
7964878Sugen			sleepexit(1);
7974878Sugen	}
7984878Sugen
79923985Sdavidn	/*
80023985Sdavidn	 * Login shells have a leading '-' in front of argv[0]
80123985Sdavidn	 */
80223985Sdavidn	tbuf[0] = '-';
80323985Sdavidn	(void)strcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ? p + 1 : pwd->pw_shell);
80423985Sdavidn
80521528Sdavidn	execlp(shell, tbuf, 0);
80621528Sdavidn	err(1, "%s", shell);
8071590Srgrimes}
8081590Srgrimes
80927605Scharnierstatic void
81027605Scharnierusage()
81127605Scharnier{
81227605Scharnier	(void)fprintf(stderr, "usage: login [-fp] [-h hostname] [username]\n");
81327605Scharnier	exit(1);
81427605Scharnier}
8151590Srgrimes
81623985Sdavidn/*
81723985Sdavidn * Allow for authentication style and/or kerberos instance
81823985Sdavidn * */
81921528Sdavidn
82021528Sdavidn#define	NBUFSIZ		UT_NAMESIZE + 64
82121528Sdavidn
8221590Srgrimesvoid
8231590Srgrimesgetloginname()
8241590Srgrimes{
8251590Srgrimes	int ch;
8261590Srgrimes	char *p;
8271590Srgrimes	static char nbuf[NBUFSIZ];
8281590Srgrimes
8291590Srgrimes	for (;;) {
8301590Srgrimes		(void)printf("login: ");
8311590Srgrimes		for (p = nbuf; (ch = getchar()) != '\n'; ) {
8321590Srgrimes			if (ch == EOF) {
8331590Srgrimes				badlogin(username);
8341590Srgrimes				exit(0);
8351590Srgrimes			}
8361590Srgrimes			if (p < nbuf + (NBUFSIZ - 1))
8371590Srgrimes				*p++ = ch;
8381590Srgrimes		}
8391590Srgrimes		if (p > nbuf)
8401590Srgrimes			if (nbuf[0] == '-')
8411590Srgrimes				(void)fprintf(stderr,
8421590Srgrimes				    "login names may not start with '-'.\n");
8431590Srgrimes			else {
8441590Srgrimes				*p = '\0';
8451590Srgrimes				username = nbuf;
8461590Srgrimes				break;
8471590Srgrimes			}
8481590Srgrimes	}
8491590Srgrimes}
8501590Srgrimes
8511590Srgrimesint
8521590Srgrimesrootterm(ttyn)
8531590Srgrimes	char *ttyn;
8541590Srgrimes{
8551590Srgrimes	struct ttyent *t;
85623985Sdavidn
8571590Srgrimes	return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
8581590Srgrimes}
8591590Srgrimes
86021528Sdavidnvolatile int motdinterrupt;
8611590Srgrimes
86221528Sdavidn/* ARGSUSED */
8631590Srgrimesvoid
86421528Sdavidnsigint(signo)
86521528Sdavidn	int signo;
8661590Srgrimes{
86721528Sdavidn	motdinterrupt = 1;
86821528Sdavidn}
86921528Sdavidn
87021528Sdavidnvoid
87121528Sdavidnmotd(motdfile)
87221528Sdavidn	char *motdfile;
87321528Sdavidn{
8741590Srgrimes	int fd, nchars;
8751590Srgrimes	sig_t oldint;
87621528Sdavidn	char tbuf[256];
8771590Srgrimes
87821528Sdavidn	if ((fd = open(motdfile, O_RDONLY, 0)) < 0)
8791590Srgrimes		return;
88021528Sdavidn	motdinterrupt = 0;
8811590Srgrimes	oldint = signal(SIGINT, sigint);
88221528Sdavidn	while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0 && !motdinterrupt)
88321528Sdavidn		(void)write(fileno(stdout), tbuf, nchars);
8841590Srgrimes	(void)signal(SIGINT, oldint);
8851590Srgrimes	(void)close(fd);
8861590Srgrimes}
8871590Srgrimes
8881590Srgrimes/* ARGSUSED */
8891590Srgrimesvoid
8901590Srgrimestimedout(signo)
8911590Srgrimes	int signo;
8921590Srgrimes{
8931590Srgrimes	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
8941590Srgrimes	exit(0);
8951590Srgrimes}
8961590Srgrimes
89721528Sdavidn#ifndef LOGIN_CAP
8981590Srgrimesvoid
8991590Srgrimeschecknologin()
9001590Srgrimes{
9011590Srgrimes	int fd, nchars;
9021590Srgrimes	char tbuf[8192];
9031590Srgrimes
9041590Srgrimes	if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
9051590Srgrimes		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
9061590Srgrimes			(void)write(fileno(stdout), tbuf, nchars);
9071590Srgrimes		sleepexit(0);
9081590Srgrimes	}
9091590Srgrimes}
91021528Sdavidn#endif
9111590Srgrimes
9121590Srgrimesvoid
9131590Srgrimesdolastlog(quiet)
9141590Srgrimes	int quiet;
9151590Srgrimes{
9161590Srgrimes	struct lastlog ll;
9171590Srgrimes	int fd;
9181590Srgrimes
9191590Srgrimes	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
9201590Srgrimes		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
9211590Srgrimes		if (!quiet) {
9221590Srgrimes			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
9231590Srgrimes			    ll.ll_time != 0) {
9241590Srgrimes				(void)printf("Last login: %.*s ",
9251590Srgrimes				    24-5, (char *)ctime(&ll.ll_time));
9261590Srgrimes				if (*ll.ll_host != '\0')
9271590Srgrimes					(void)printf("from %.*s\n",
9281590Srgrimes					    (int)sizeof(ll.ll_host),
9291590Srgrimes					    ll.ll_host);
9301590Srgrimes				else
9311590Srgrimes					(void)printf("on %.*s\n",
9321590Srgrimes					    (int)sizeof(ll.ll_line),
9331590Srgrimes					    ll.ll_line);
9341590Srgrimes			}
9351590Srgrimes			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
9361590Srgrimes		}
9371590Srgrimes		memset((void *)&ll, 0, sizeof(ll));
9381590Srgrimes		(void)time(&ll.ll_time);
9391590Srgrimes		(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
9401590Srgrimes		if (hostname)
9411590Srgrimes			(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
9421590Srgrimes		(void)write(fd, (char *)&ll, sizeof(ll));
9431590Srgrimes		(void)close(fd);
9441590Srgrimes	}
9451590Srgrimes}
9461590Srgrimes
9471590Srgrimesvoid
9481590Srgrimesbadlogin(name)
9491590Srgrimes	char *name;
9501590Srgrimes{
9511590Srgrimes
9521590Srgrimes	if (failures == 0)
9531590Srgrimes		return;
9541590Srgrimes	if (hostname) {
9551590Srgrimes		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
95616423Sache		    failures, failures > 1 ? "S" : "", full_hostname);
9571590Srgrimes		syslog(LOG_AUTHPRIV|LOG_NOTICE,
9581590Srgrimes		    "%d LOGIN FAILURE%s FROM %s, %s",
95916423Sache		    failures, failures > 1 ? "S" : "", full_hostname, name);
9601590Srgrimes	} else {
9611590Srgrimes		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
9621590Srgrimes		    failures, failures > 1 ? "S" : "", tty);
9631590Srgrimes		syslog(LOG_AUTHPRIV|LOG_NOTICE,
9641590Srgrimes		    "%d LOGIN FAILURE%s ON %s, %s",
9651590Srgrimes		    failures, failures > 1 ? "S" : "", tty, name);
9661590Srgrimes	}
9671590Srgrimes}
9681590Srgrimes
9691590Srgrimes#undef	UNKNOWN
9701590Srgrimes#define	UNKNOWN	"su"
9711590Srgrimes
9721590Srgrimeschar *
9731590Srgrimesstypeof(ttyid)
9741590Srgrimes	char *ttyid;
9751590Srgrimes{
97623985Sdavidn
9771590Srgrimes	struct ttyent *t;
97823985Sdavidn
9791590Srgrimes	return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
9801590Srgrimes}
9811590Srgrimes
9821590Srgrimesvoid
98323985Sdavidnrefused(msg, rtype, lout)
98423985Sdavidn	char *msg;
98523985Sdavidn	char *rtype;
98623985Sdavidn	int lout;
98723985Sdavidn{
98823985Sdavidn
98923985Sdavidn	if (msg != NULL)
99023985Sdavidn	    printf("%s.\n", msg);
99123985Sdavidn	if (hostname)
99223985Sdavidn		syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s",
99323985Sdavidn		       pwd->pw_name, rtype, full_hostname, tty);
99423985Sdavidn	else
99523985Sdavidn		syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) ON TTY %s",
99623985Sdavidn		       pwd->pw_name, rtype, tty);
99723985Sdavidn	if (lout)
99823985Sdavidn		sleepexit(1);
99923985Sdavidn}
100023985Sdavidn
100123985Sdavidnvoid
10021590Srgrimessleepexit(eval)
10031590Srgrimes	int eval;
10041590Srgrimes{
100523985Sdavidn
10061590Srgrimes	(void)sleep(5);
10071590Srgrimes	exit(eval);
10081590Srgrimes}
1009