xref: /csrg-svn/usr.bin/login/login.c (revision 40009)
119843Sdist /*
236515Sbostic  * Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
336515Sbostic  * All rights reserved.
436515Sbostic  *
536515Sbostic  * Redistribution and use in source and binary forms are permitted
636515Sbostic  * provided that the above copyright notice and this paragraph are
736515Sbostic  * duplicated in all such forms and that any documentation,
836515Sbostic  * advertising materials, and other materials related to such
936515Sbostic  * distribution and use acknowledge that the software was developed
1036515Sbostic  * by the University of California, Berkeley.  The name of the
1136515Sbostic  * University may not be used to endorse or promote products derived
1236515Sbostic  * from this software without specific prior written permission.
1336515Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1436515Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1536515Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1619843Sdist  */
1719843Sdist 
1812678Ssam #ifndef lint
1919843Sdist char copyright[] =
2036515Sbostic "@(#) Copyright (c) 1980, 1987, 1988 The Regents of the University of California.\n\
2119843Sdist  All rights reserved.\n";
2236515Sbostic #endif /* not lint */
2312678Ssam 
2419843Sdist #ifndef lint
25*40009Ssklower static char sccsid[] = "@(#)login.c	5.49 (Berkeley) 02/02/90";
2636515Sbostic #endif /* not lint */
2719843Sdist 
281043Sbill /*
291043Sbill  * login [ name ]
3031695Skarels  * login -h hostname	(for telnetd, etc.)
3131695Skarels  * login -f name	(for pre-authenticated login: datakit, xterm, etc.)
321043Sbill  */
331043Sbill 
3412984Ssam #include <sys/param.h>
3537692Sbostic #include <ufs/quota.h>
3612687Ssam #include <sys/stat.h>
3712687Ssam #include <sys/time.h>
3812687Ssam #include <sys/resource.h>
3916453Sroot #include <sys/file.h>
4039271Skfall #include <sgtty.h>
4112687Ssam 
421043Sbill #include <utmp.h>
431043Sbill #include <signal.h>
4412678Ssam #include <errno.h>
4516453Sroot #include <ttyent.h>
4616453Sroot #include <syslog.h>
4726862Smckusick #include <grp.h>
4836460Sbostic #include <pwd.h>
4936515Sbostic #include <setjmp.h>
5036460Sbostic #include <stdio.h>
5136460Sbostic #include <strings.h>
5237203Sbostic #include <tzfile.h>
5337203Sbostic #include "pathnames.h"
541043Sbill 
5537203Sbostic #ifdef	KERBEROS
5638726Skfall #include <krb.h>
5738034Skfall #include <netdb.h>
5838034Skfall char		realm[REALM_SZ];
5938034Skfall int		kerror = KSUCCESS, notickets = 1;
6038034Skfall KTEXT_ST	ticket;
6138034Skfall AUTH_DAT	authdata;
6238034Skfall char		savehost[MAXHOSTNAMELEN];
6338726Skfall char		tkfile[MAXPATHLEN];
6438034Skfall unsigned long	faddr;
6538034Skfall struct	hostent	*hp;
6638034Skfall #define	PRINCIPAL_NAME	pwd->pw_name
6738034Skfall #define	PRINCIPAL_INST	""
6838034Skfall #define	INITIAL_TICKET	"krbtgt"
6938034Skfall #define	VERIFY_SERVICE	"rcmd"
7037203Sbostic #endif
7137203Sbostic 
7236460Sbostic #define	TTYGRPNAME	"tty"		/* name of group to own ttys */
7326862Smckusick 
7412687Ssam /*
7536460Sbostic  * This bounds the time given to login.  Not a define so it can
7636460Sbostic  * be patched on machines where it's too small.
7712687Ssam  */
7831509Sbostic int	timeout = 300;
796005Swnj 
8036647Skarels struct	passwd *pwd;
8136647Skarels int	failures;
8236647Skarels char	term[64], *hostname, *username, *tty;
836005Swnj 
8436647Skarels struct	sgttyb sgttyb;
8536647Skarels struct	tchars tc = {
8613074Ssam 	CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
871365Sbill };
8836647Skarels struct	ltchars ltc = {
8913074Ssam 	CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT
9013074Ssam };
911365Sbill 
9236880Sbostic char *months[] =
9336880Sbostic 	{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
9436880Sbostic 	  "Sep", "Oct", "Nov", "Dec" };
9536880Sbostic 
961043Sbill main(argc, argv)
9736460Sbostic 	int argc;
9836460Sbostic 	char **argv;
991043Sbill {
10036460Sbostic 	extern int errno, optind;
10136460Sbostic 	extern char *optarg, **environ;
10236880Sbostic 	struct timeval tp;
10336880Sbostic 	struct tm *ttp;
10436460Sbostic 	struct group *gr;
10536460Sbostic 	register int ch;
10636647Skarels 	register char *p;
10739271Skfall 	int ask, fflag, hflag, pflag, cnt, uid;
10836460Sbostic 	int quietlog, passwd_req, ioctlval, timedout();
10936647Skarels 	char *domain, *salt, *envinit[1], *ttyn, *pp;
11037692Sbostic 	char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
11138034Skfall 	char localhost[MAXHOSTNAMELEN];
11236880Sbostic 	char *ctime(), *ttyname(), *stypeof(), *crypt(), *getpass();
11336460Sbostic 	time_t time();
11436460Sbostic 	off_t lseek();
1151043Sbill 
11636460Sbostic 	(void)signal(SIGALRM, timedout);
11736460Sbostic 	(void)alarm((u_int)timeout);
11836460Sbostic 	(void)signal(SIGQUIT, SIG_IGN);
11936460Sbostic 	(void)signal(SIGINT, SIG_IGN);
12036460Sbostic 	(void)setpriority(PRIO_PROCESS, 0, 0);
12136460Sbostic 	(void)quota(Q_SETUID, 0, 0, 0);
12236460Sbostic 
12312687Ssam 	/*
12418549Ssam 	 * -p is used by getty to tell login not to destroy the environment
12531695Skarels  	 * -f is used to skip a second login authentication
12636523Skarels 	 * -h is used by other servers to pass the name of the remote
12736523Skarels 	 *    host to login so that it may be placed in utmp and wtmp
12812687Ssam 	 */
12938034Skfall 	domain = NULL;
13038034Skfall 	if (gethostname(localhost, sizeof(localhost)) < 0)
13138034Skfall 		syslog(LOG_ERR, "couldn't get local hostname: %m");
13238034Skfall 	else
13338034Skfall 		domain = index(localhost, '.');
13436460Sbostic 
13539271Skfall 	openlog("login", LOG_ODELAY, LOG_AUTH);
13639271Skfall 
13737203Sbostic 	fflag = hflag = pflag = 0;
13836460Sbostic 	passwd_req = 1;
13939271Skfall 	uid = getuid();
14037203Sbostic 	while ((ch = getopt(argc, argv, "fh:p")) != EOF)
14136515Sbostic 		switch (ch) {
14236460Sbostic 		case 'f':
14336460Sbostic 			fflag = 1;
14436460Sbostic 			break;
14536460Sbostic 		case 'h':
14639271Skfall 			if (uid) {
14737203Sbostic 				(void)fprintf(stderr,
14836460Sbostic 				    "login: -h for super-user only.\n");
14932313Sbostic 				exit(1);
15031695Skarels 			}
15136460Sbostic 			hflag = 1;
15236460Sbostic 			if (domain && (p = index(optarg, '.')) &&
15336553Sbostic 			    strcasecmp(p, domain) == 0)
15436460Sbostic 				*p = 0;
15536460Sbostic 			hostname = optarg;
15636460Sbostic 			break;
15736460Sbostic 		case 'p':
15818549Ssam 			pflag = 1;
15936460Sbostic 			break;
16036460Sbostic 		case '?':
16136460Sbostic 		default:
16239271Skfall 			if (!uid)
16339271Skfall 				syslog(LOG_ERR, "invalid flag %c", ch);
16437203Sbostic 			(void)fprintf(stderr,
16537203Sbostic 			    "usage: login [-fp] [username]\n");
16636460Sbostic 			exit(1);
16718549Ssam 		}
16836460Sbostic 	argc -= optind;
16936460Sbostic 	argv += optind;
17036549Sbostic 	if (*argv) {
17136647Skarels 		username = *argv;
17236549Sbostic 		ask = 0;
17336647Skarels 	} else
17436549Sbostic 		ask = 1;
17536460Sbostic 
17636460Sbostic 	ioctlval = 0;
17736460Sbostic 	(void)ioctl(0, TIOCLSET, &ioctlval);
17836460Sbostic 	(void)ioctl(0, TIOCNXCL, 0);
17936515Sbostic 	(void)fcntl(0, F_SETFL, ioctlval);
18036515Sbostic 	(void)ioctl(0, TIOCGETP, &sgttyb);
18136515Sbostic 	sgttyb.sg_erase = CERASE;
18236515Sbostic 	sgttyb.sg_kill = CKILL;
18336460Sbostic 	(void)ioctl(0, TIOCSLTC, &ltc);
18436460Sbostic 	(void)ioctl(0, TIOCSETC, &tc);
18536515Sbostic 	(void)ioctl(0, TIOCSETP, &sgttyb);
18636460Sbostic 
18736460Sbostic 	for (cnt = getdtablesize(); cnt > 2; cnt--)
18836460Sbostic 		close(cnt);
18936460Sbostic 
1901043Sbill 	ttyn = ttyname(0);
19137692Sbostic 	if (ttyn == NULL || *ttyn == '\0') {
19237692Sbostic 		(void)sprintf(tname, "%s??", _PATH_TTY);
19337692Sbostic 		ttyn = tname;
19437692Sbostic 	}
19536460Sbostic 	if (tty = rindex(ttyn, '/'))
19636460Sbostic 		++tty;
19736460Sbostic 	else
19816453Sroot 		tty = ttyn;
19936460Sbostic 
20036549Sbostic 	for (cnt = 0;; ask = 1) {
20138602Sbostic 		ioctlval = TTYDISC;
20236460Sbostic 		(void)ioctl(0, TIOCSETD, &ioctlval);
20336460Sbostic 
20436549Sbostic 		if (ask) {
20536515Sbostic 			fflag = 0;
20636460Sbostic 			getloginname();
20736515Sbostic 		}
20836647Skarels 		/*
20939271Skfall 		 * Note if trying multiple user names; log failures for
21039271Skfall 		 * previous user name, but don't bother logging one failure
21136647Skarels 		 * for nonexistent name (mistyped username).
21236647Skarels 		 */
21336647Skarels 		if (failures && strcmp(tbuf, username)) {
21436647Skarels 			if (failures > (pwd ? 0 : 1))
21536549Sbostic 				badlogin(tbuf);
21636647Skarels 			failures = 0;
21736549Sbostic 		}
21836647Skarels 		(void)strcpy(tbuf, username);
21936515Sbostic 		if (pwd = getpwnam(username))
22036515Sbostic 			salt = pwd->pw_passwd;
22136515Sbostic 		else
22236515Sbostic 			salt = "xx";
22336460Sbostic 
22436460Sbostic 		/* if user not super-user, check for disabled logins */
22536515Sbostic 		if (pwd == NULL || pwd->pw_uid)
22636460Sbostic 			checknologin();
22736460Sbostic 
22812687Ssam 		/*
22936515Sbostic 		 * Disallow automatic login to root; if not invoked by
23036515Sbostic 		 * root, disallow if the uid's differ.
23112687Ssam 		 */
23236515Sbostic 		if (fflag && pwd) {
23338726Skfall 			passwd_req =
23438726Skfall #ifndef	KERBEROS
23538726Skfall 			     pwd->pw_uid == 0 ||
23638726Skfall #endif
23736515Sbostic 			    (uid && uid != pwd->pw_uid);
23831695Skarels 		}
23936460Sbostic 
24012687Ssam 		/*
24136523Skarels 		 * If no pre-authentication and a password exists
24236460Sbostic 		 * for this user, prompt for one and verify it.
24312687Ssam 		 */
24436608Skfall 		if (!passwd_req || (pwd && !*pwd->pw_passwd))
24536460Sbostic 			break;
24612687Ssam 
24739271Skfall 		/*
24839271Skfall 		 * If trying to log in as root, but with insecure terminal,
24939271Skfall 		 * refuse the login attempt.
25039271Skfall 		 */
25139271Skfall 		if (pwd->pw_uid == 0 && !rootterm(tty)) {
25239271Skfall 			(void)fprintf(stderr,
25339271Skfall 			    "%s login refused on this terminal.\n",
25439271Skfall 			    pwd->pw_name);
25539271Skfall 			if (hostname)
25639271Skfall 				syslog(LOG_NOTICE,
25739271Skfall 				    "LOGIN %s REFUSED FROM %s ON TTY %s",
25839271Skfall 				    pwd->pw_name, hostname, tty);
25939271Skfall 			else
26039271Skfall 				syslog(LOG_NOTICE,
26139271Skfall 				    "LOGIN %s REFUSED ON TTY %s",
26239271Skfall 				     pwd->pw_name, tty);
26339271Skfall 			continue;
26439271Skfall 		}
26539271Skfall 
26636460Sbostic 		setpriority(PRIO_PROCESS, 0, -4);
26736608Skfall 		pp = getpass("Password:");
26836608Skfall 		p = crypt(pp, salt);
26936460Sbostic 		setpriority(PRIO_PROCESS, 0, 0);
27036608Skfall 
27137203Sbostic #ifdef	KERBEROS
27237203Sbostic 
27337203Sbostic 		/*
27438755Skfall 		 * If not present in pw file, act as old login would.
27537203Sbostic 		 * If we aren't Kerberos-authenticated, try the normal
27637203Sbostic 		 * pw file for a password.  If that's ok, log the user
27737203Sbostic 		 * in without issueing any tickets.
27837203Sbostic 		 */
27937203Sbostic 
28038034Skfall 		if (pwd && (krb_get_lrealm(realm,1) == KSUCCESS)) {
28138726Skfall 
28237203Sbostic 			/*
28338726Skfall 			 * get TGT for local realm
28439271Skfall 			 * convention: store tickets in file associated
28539271Skfall 			 * with tty name, which should be available
28637203Sbostic 			 */
28738726Skfall 			(void)sprintf(tkfile, "%s_%s", TKT_ROOT, tty);
28838726Skfall 			kerror = INTK_ERR;
28938726Skfall 			if (setenv("KRBTKFILE", tkfile, 1) < 0)
29038726Skfall 				syslog(LOG_ERR, "couldn't set tkfile environ");
29138726Skfall 			else {
29238755Skfall 				(void) unlink(tkfile);
29338726Skfall 				kerror = krb_get_pw_in_tkt(
29438755Skfall 					PRINCIPAL_NAME,	/* user */
29538755Skfall 					PRINCIPAL_INST,	/* (null) */
29638755Skfall 					realm,
29738755Skfall 					INITIAL_TICKET, realm,
29838755Skfall 					DEFAULT_TKT_LIFE,
29938726Skfall 					pp);
30038726Skfall 			}
30138034Skfall 			/*
30238034Skfall 			 * If we got a TGT, get a local "rcmd" ticket and
30338034Skfall 			 * check it so as to ensure that we are not
30438034Skfall 			 * talking to a bogus Kerberos server
30538726Skfall 			 *
30638726Skfall 			 * There are 2 cases where we still allow a login:
30738726Skfall 			 *	1> the VERIFY_SERVICE doesn't exist in the KDC
30838726Skfall 			 *	2> local host has no srvtab, as (hopefully)
30938726Skfall 			 *	   indicated by a return value of RD_AP_UNDEC
31038726Skfall 			 *	   from krb_rd_req()
31138034Skfall 			 */
31237203Sbostic 			if (kerror == INTK_OK) {
31339271Skfall 				if (chown(tkfile, pwd->pw_uid, pwd->pw_gid) < 0)
31439271Skfall 					syslog(LOG_ERR, "chown tkfile: %m");
31538034Skfall 				(void) strncpy(savehost,
31638034Skfall 					krb_get_phost(localhost),
31738034Skfall 					sizeof(savehost));
31838034Skfall 				savehost[sizeof(savehost)-1] = NULL;
31938034Skfall 				kerror = krb_mk_req(&ticket, VERIFY_SERVICE,
32038034Skfall 					savehost, realm, 33);
32138726Skfall 				/*
32238755Skfall 				 * if the "VERIFY_SERVICE" doesn't exist in
32338755Skfall 				 * the KDC for this host, still allow login,
32438726Skfall 				 * but log the error condition
32538726Skfall 				 */
32638034Skfall 				if (kerror == KDC_PR_UNKNOWN) {
32738726Skfall 					syslog(LOG_NOTICE,
32838034Skfall 					    "Warning: TGT not verified (%s)",
32938034Skfall 						krb_err_txt[kerror]);
33038034Skfall 					bzero(pp, strlen(pp));
33138034Skfall 					notickets = 0;
33238034Skfall 					break;		/* ok */
33338034Skfall 				} else if (kerror != KSUCCESS) {
33438034Skfall 					printf("Unable to use TGT: (%s)\n",
33538034Skfall 						krb_err_txt[kerror]);
33638726Skfall 					syslog(LOG_NOTICE,
33738034Skfall 					    "Unable to use TGT: (%s)",
33838034Skfall 						krb_err_txt[kerror]);
33938034Skfall 					dest_tkt();
34038034Skfall 					/* fall thru: no login */
34138034Skfall 				} else {
34238034Skfall 					if (!(hp = gethostbyname(localhost))) {
34338755Skfall 						syslog(LOG_ERR,
34438755Skfall 						    "couldn't get local host address");
34538034Skfall 					} else {
34638034Skfall 					    bcopy((char *) hp->h_addr,
34738034Skfall 						(char *) &faddr, sizeof(faddr));
34838034Skfall 					    if ((kerror = krb_rd_req(&ticket,
34938034Skfall 						VERIFY_SERVICE, savehost, faddr,
35038034Skfall 						&authdata, "")) != KSUCCESS) {
35138726Skfall 
35238726Skfall 						if (kerror = RD_AP_UNDEC) {
35338726Skfall 							syslog(LOG_NOTICE,
35438726Skfall 							  "krb_rd_req: (%s)\n",
35538726Skfall 							  krb_err_txt[kerror]);
35638726Skfall 							bzero(pp, strlen(pp));
35738726Skfall 							notickets = 0;
35838726Skfall 							break;	/* ok */
35938726Skfall 						} else {
36038726Skfall 						    printf("Unable to verify %s ticket: (%s)\n",
36138726Skfall 							VERIFY_SERVICE,
36238034Skfall 							krb_err_txt[kerror]);
36338726Skfall 						    syslog(LOG_NOTICE,
36438726Skfall 						    "couldn't verify %s ticket: %s",
36538726Skfall 							VERIFY_SERVICE,
36638034Skfall 							krb_err_txt[kerror]);
36738726Skfall 						}
36838726Skfall 						/* fall thru: no login */
36938034Skfall 					    } else {
37038034Skfall 						bzero(pp, strlen(pp));
37138034Skfall 						notickets = 0;	/* got ticket */
37238034Skfall 						break;		/* ok */
37338034Skfall 					    }
37438034Skfall 					}
37538034Skfall 				}
37638034Skfall 
37738755Skfall 			} else {
37838755Skfall 				(void) unlink(tkfile);
37939271Skfall 				if ((kerror != INTK_BADPW) &&
38039271Skfall 				    kerror != KDC_PR_UNKNOWN)
38138755Skfall 					syslog(LOG_ERR,
38238755Skfall 						"Kerberos intkt error: %s",
38338755Skfall 						krb_err_txt[kerror]);
38437203Sbostic 			}
38537203Sbostic 		}
38638726Skfall 
38737203Sbostic #endif
38836608Skfall 		(void) bzero(pp, strlen(pp));
38936515Sbostic 		if (pwd && !strcmp(p, pwd->pw_passwd))
39036460Sbostic 			break;
39136460Sbostic 
39237203Sbostic 		(void)printf("Login incorrect\n");
39336647Skarels 		failures++;
39436549Sbostic 		/* we allow 10 tries, but after 3 we start backing off */
39536549Sbostic 		if (++cnt > 3) {
39636549Sbostic 			if (cnt >= 10) {
39736549Sbostic 				badlogin(username);
39836549Sbostic 				(void)ioctl(0, TIOCHPCL, (struct sgttyb *)NULL);
39936549Sbostic 				sleepexit(1);
40036549Sbostic 			}
40136549Sbostic 			sleep((u_int)((cnt - 3) * 5));
4022822Swnj 		}
40336460Sbostic 	}
4041043Sbill 
40536460Sbostic 	/* committed to login -- turn off timeout */
40636460Sbostic 	(void)alarm((u_int)0);
40736460Sbostic 
40837692Sbostic 	/* paranoia... */
40937692Sbostic 	endpwent();
41037692Sbostic 
41136460Sbostic 	if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0 && errno != EINVAL) {
41236460Sbostic 		switch(errno) {
41336460Sbostic 		case EUSERS:
41437203Sbostic 			(void)fprintf(stderr,
41536460Sbostic 		"Too many users logged on already.\nTry again later.\n");
41636460Sbostic 			break;
41736460Sbostic 		case EPROCLIM:
41837203Sbostic 			(void)fprintf(stderr,
41936460Sbostic 			    "You have too many processes running.\n");
42036460Sbostic 			break;
42136460Sbostic 		default:
42236460Sbostic 			perror("quota (Q_SETUID)");
4232822Swnj 		}
42436460Sbostic 		sleepexit(0);
4252822Swnj 	}
42636460Sbostic 
42736515Sbostic 	if (chdir(pwd->pw_dir) < 0) {
42837203Sbostic 		(void)printf("No directory %s!\n", pwd->pw_dir);
42936515Sbostic 		if (chdir("/"))
43036515Sbostic 			exit(0);
43136515Sbostic 		pwd->pw_dir = "/";
43237203Sbostic 		(void)printf("Logging in with home = \"/\".\n");
43336515Sbostic 	}
43436515Sbostic 
43537203Sbostic 	quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
43637203Sbostic 
43737203Sbostic #ifdef KERBEROS
43837203Sbostic 	if (notickets && !quietlog)
43937203Sbostic 		(void)printf("Warning: no Kerberos tickets issued\n");
44037203Sbostic #endif
44137203Sbostic 
44236880Sbostic 	if (pwd->pw_change || pwd->pw_expire)
44336880Sbostic 		(void)gettimeofday(&tp, (struct timezone *)NULL);
44436880Sbostic 	if (pwd->pw_change)
44536880Sbostic 		if (tp.tv_sec >= pwd->pw_change) {
44637203Sbostic 			(void)printf("Sorry -- your password has expired.\n");
44736880Sbostic 			sleepexit(1);
44836880Sbostic 		}
44938216Sbostic 		else if (pwd->pw_change - tp.tv_sec <
45038216Sbostic 		    2 * DAYSPERWEEK * SECSPERDAY && !quietlog) {
45136880Sbostic 			ttp = localtime(&pwd->pw_change);
45237203Sbostic 			(void)printf("Warning: your password expires on %s %d, %d\n",
45338216Sbostic 			    months[ttp->tm_mon], ttp->tm_mday,
45438216Sbostic 			    TM_YEAR_BASE + ttp->tm_year);
45536880Sbostic 		}
45636880Sbostic 	if (pwd->pw_expire)
45736880Sbostic 		if (tp.tv_sec >= pwd->pw_expire) {
45837203Sbostic 			(void)printf("Sorry -- your account has expired.\n");
45936880Sbostic 			sleepexit(1);
46036880Sbostic 		}
46138216Sbostic 		else if (pwd->pw_expire - tp.tv_sec <
46238216Sbostic 		    2 * DAYSPERWEEK * SECSPERDAY && !quietlog) {
46336880Sbostic 			ttp = localtime(&pwd->pw_expire);
46437203Sbostic 			(void)printf("Warning: your account expires on %s %d, %d\n",
46538216Sbostic 			    months[ttp->tm_mon], ttp->tm_mday,
46638216Sbostic 			    TM_YEAR_BASE + ttp->tm_year);
46736880Sbostic 		}
46836880Sbostic 
46936515Sbostic 	/* nothing else left to fail -- really log in */
47036460Sbostic 	{
47136460Sbostic 		struct utmp utmp;
47236460Sbostic 
47336647Skarels 		bzero((char *)&utmp, sizeof(utmp));
47436460Sbostic 		(void)time(&utmp.ut_time);
47536460Sbostic 		strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
47636591Sbostic 		if (hostname)
47736591Sbostic 			strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
47836460Sbostic 		strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
47936460Sbostic 		login(&utmp);
48036460Sbostic 	}
48136460Sbostic 
48236549Sbostic 	dolastlog(quietlog);
48336460Sbostic 
48437203Sbostic 	if (!hflag) {					/* XXX */
48536460Sbostic 		static struct winsize win = { 0, 0, 0, 0 };
48636460Sbostic 
48736460Sbostic 		(void)ioctl(0, TIOCSWINSZ, &win);
48836460Sbostic 	}
48936460Sbostic 
49036460Sbostic 	(void)chown(ttyn, pwd->pw_uid,
49136460Sbostic 	    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
49236460Sbostic 	(void)chmod(ttyn, 0620);
49336460Sbostic 	(void)setgid(pwd->pw_gid);
49436460Sbostic 
49536460Sbostic 	initgroups(username, pwd->pw_gid);
49636460Sbostic 
49712678Ssam 	quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0);
49830606Sbostic 
49936515Sbostic 	if (*pwd->pw_shell == '\0')
50037203Sbostic 		pwd->pw_shell = _PATH_BSHELL;
50136515Sbostic 
50236460Sbostic 	/* destroy environment unless user has requested preservation */
50318549Ssam 	if (!pflag)
50418549Ssam 		environ = envinit;
50536515Sbostic 	(void)setenv("HOME", pwd->pw_dir, 1);
50636515Sbostic 	(void)setenv("SHELL", pwd->pw_shell, 1);
50718549Ssam 	if (term[0] == '\0')
50836647Skarels 		strncpy(term, stypeof(tty), sizeof(term));
50936515Sbostic 	(void)setenv("TERM", term, 0);
51036515Sbostic 	(void)setenv("USER", pwd->pw_name, 1);
51137252Sbostic 	(void)setenv("PATH", _PATH_DEFPATH, 0);
51218549Ssam 
51316453Sroot 	if (tty[sizeof("tty")-1] == 'd')
51418549Ssam 		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
51518549Ssam 	if (pwd->pw_uid == 0)
51636460Sbostic 		if (hostname)
51736647Skarels 			syslog(LOG_NOTICE, "ROOT LOGIN ON %s FROM %s",
51836549Sbostic 			    tty, hostname);
51925230Smckusick 		else
52036647Skarels 			syslog(LOG_NOTICE, "ROOT LOGIN ON %s", tty);
52136460Sbostic 
5226329Swnj 	if (!quietlog) {
52317664Sserge 		struct stat st;
52418549Ssam 
52536460Sbostic 		motd();
52637203Sbostic 		(void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
52736460Sbostic 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
52837203Sbostic 			(void)printf("You have %smail.\n",
52936460Sbostic 			    (st.st_mtime > st.st_atime) ? "new " : "");
5302822Swnj 	}
53136460Sbostic 
53236460Sbostic 	(void)signal(SIGALRM, SIG_DFL);
53336460Sbostic 	(void)signal(SIGQUIT, SIG_DFL);
53436460Sbostic 	(void)signal(SIGINT, SIG_DFL);
53536460Sbostic 	(void)signal(SIGTSTP, SIG_IGN);
53636460Sbostic 
53736460Sbostic 	tbuf[0] = '-';
53836460Sbostic 	strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ?
53936460Sbostic 	    p + 1 : pwd->pw_shell);
54037203Sbostic 
541*40009Ssklower 	if (setlogin(pwd->pw_name) < 0)
542*40009Ssklower 		syslog(LOG_ERR, "setlogin() failure: %m");
54337692Sbostic 
54437203Sbostic 	/* discard permissions last so can't get killed and drop core */
54537203Sbostic 	(void)setuid(pwd->pw_uid);
54637203Sbostic 
54736460Sbostic 	execlp(pwd->pw_shell, tbuf, 0);
54837203Sbostic 	(void)fprintf(stderr, "login: no shell: %s.\n", strerror(errno));
5491043Sbill 	exit(0);
5501043Sbill }
5511043Sbill 
55236460Sbostic getloginname()
55312687Ssam {
55436460Sbostic 	register int ch;
55536460Sbostic 	register char *p;
55636460Sbostic 	static char nbuf[UT_NAMESIZE + 1];
55712687Ssam 
55836460Sbostic 	for (;;) {
55937203Sbostic 		(void)printf("login: ");
56036515Sbostic 		for (p = nbuf; (ch = getchar()) != '\n'; ) {
56136549Sbostic 			if (ch == EOF) {
56236549Sbostic 				badlogin(username);
56312687Ssam 				exit(0);
56436549Sbostic 			}
56536515Sbostic 			if (p < nbuf + UT_NAMESIZE)
56636460Sbostic 				*p++ = ch;
56712687Ssam 		}
56836460Sbostic 		if (p > nbuf)
56936460Sbostic 			if (nbuf[0] == '-')
57037203Sbostic 				(void)fprintf(stderr,
57136460Sbostic 				    "login names may not start with '-'.\n");
57236460Sbostic 			else {
57336460Sbostic 				*p = '\0';
57436460Sbostic 				username = nbuf;
57536515Sbostic 				break;
57636460Sbostic 			}
57712687Ssam 	}
57812687Ssam }
57912687Ssam 
58012687Ssam timedout()
58112687Ssam {
58237203Sbostic 	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
58312687Ssam 	exit(0);
58412687Ssam }
58512687Ssam 
58636647Skarels rootterm(ttyn)
58736647Skarels 	char *ttyn;
5881043Sbill {
58936460Sbostic 	struct ttyent *t;
5906466Swnj 
59136647Skarels 	return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE);
5921043Sbill }
5931043Sbill 
59436515Sbostic jmp_buf motdinterrupt;
59536515Sbostic 
59636460Sbostic motd()
5972822Swnj {
59836515Sbostic 	register int fd, nchars;
59938726Skfall 	sig_t oldint;
60038726Skfall 	int sigint();
60136515Sbostic 	char tbuf[8192];
6022822Swnj 
60337203Sbostic 	if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0)
60436460Sbostic 		return;
60536460Sbostic 	oldint = signal(SIGINT, sigint);
60636515Sbostic 	if (setjmp(motdinterrupt) == 0)
60736515Sbostic 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
60836515Sbostic 			(void)write(fileno(stdout), tbuf, nchars);
60936460Sbostic 	(void)signal(SIGINT, oldint);
61036515Sbostic 	(void)close(fd);
61136460Sbostic }
61236460Sbostic 
61336460Sbostic sigint()
61436460Sbostic {
61536515Sbostic 	longjmp(motdinterrupt, 1);
61636460Sbostic }
61736460Sbostic 
61836460Sbostic checknologin()
61936460Sbostic {
62036460Sbostic 	register int fd, nchars;
62136515Sbostic 	char tbuf[8192];
62236460Sbostic 
62337203Sbostic 	if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
62436460Sbostic 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
62536460Sbostic 			(void)write(fileno(stdout), tbuf, nchars);
62636460Sbostic 		sleepexit(0);
6272822Swnj 	}
6282822Swnj }
6292822Swnj 
63036549Sbostic dolastlog(quiet)
63136460Sbostic 	int quiet;
6321043Sbill {
63336460Sbostic 	struct lastlog ll;
63436460Sbostic 	int fd;
63536880Sbostic 	char *ctime();
6361043Sbill 
63737203Sbostic 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
63836515Sbostic 		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
63936460Sbostic 		if (!quiet) {
64036460Sbostic 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
64136460Sbostic 			    ll.ll_time != 0) {
64237203Sbostic 				(void)printf("Last login: %.*s ",
64336460Sbostic 				    24-5, (char *)ctime(&ll.ll_time));
64436460Sbostic 				if (*ll.ll_host != '\0')
64537203Sbostic 					(void)printf("from %.*s\n",
64636460Sbostic 					    sizeof(ll.ll_host), ll.ll_host);
64736460Sbostic 				else
64837203Sbostic 					(void)printf("on %.*s\n",
64936460Sbostic 					    sizeof(ll.ll_line), ll.ll_line);
65036460Sbostic 			}
65136515Sbostic 			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
65236460Sbostic 		}
65336647Skarels 		bzero((char *)&ll, sizeof(ll));
65436460Sbostic 		(void)time(&ll.ll_time);
65536460Sbostic 		strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
65636591Sbostic 		if (hostname)
65736591Sbostic 			strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
65836460Sbostic 		(void)write(fd, (char *)&ll, sizeof(ll));
65936460Sbostic 		(void)close(fd);
6601043Sbill 	}
6611043Sbill }
6621043Sbill 
66336549Sbostic badlogin(name)
66436549Sbostic 	char *name;
66536549Sbostic {
66636647Skarels 	if (failures == 0)
66736549Sbostic 		return;
66836549Sbostic 	if (hostname)
66936647Skarels 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s, %s",
67036647Skarels 		    failures, failures > 1 ? "S" : "", hostname, name);
67136549Sbostic 	else
67236647Skarels 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s, %s",
67336647Skarels 		    failures, failures > 1 ? "S" : "", tty, name);
67436549Sbostic }
67536549Sbostic 
6762822Swnj #undef	UNKNOWN
67736460Sbostic #define	UNKNOWN	"su"
6781043Sbill 
6791043Sbill char *
68036647Skarels stypeof(ttyid)
68136647Skarels 	char *ttyid;
6821043Sbill {
68336460Sbostic 	struct ttyent *t;
6841043Sbill 
68536647Skarels 	return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
6861043Sbill }
6876005Swnj 
6886005Swnj getstr(buf, cnt, err)
68936460Sbostic 	char *buf, *err;
6906005Swnj 	int cnt;
6916005Swnj {
69236460Sbostic 	char ch;
6936005Swnj 
6946005Swnj 	do {
69536460Sbostic 		if (read(0, &ch, sizeof(ch)) != sizeof(ch))
6966005Swnj 			exit(1);
6976005Swnj 		if (--cnt < 0) {
69837203Sbostic 			(void)fprintf(stderr, "%s too long\r\n", err);
69936460Sbostic 			sleepexit(1);
7006005Swnj 		}
70136460Sbostic 		*buf++ = ch;
70236460Sbostic 	} while (ch);
7036005Swnj }
7046329Swnj 
70536460Sbostic sleepexit(eval)
70636460Sbostic 	int eval;
70726862Smckusick {
70836460Sbostic 	sleep((u_int)5);
70936460Sbostic 	exit(eval);
71026862Smckusick }
711