xref: /csrg-svn/usr.bin/login/login.c (revision 17664)
112678Ssam #ifndef lint
2*17664Sserge static	char *sccsid = "@(#)login.c	4.35 (Berkeley) 85/01/08";
312678Ssam #endif
412678Ssam 
51043Sbill /*
61043Sbill  * login [ name ]
712687Ssam  * login -r hostname (for rlogind)
812687Ssam  * login -h hostname (for telnetd, etc.)
91043Sbill  */
101043Sbill 
1112984Ssam #include <sys/param.h>
1212687Ssam #include <sys/quota.h>
1312687Ssam #include <sys/stat.h>
1412687Ssam #include <sys/time.h>
1512687Ssam #include <sys/resource.h>
1616453Sroot #include <sys/file.h>
1712687Ssam 
181043Sbill #include <sgtty.h>
191043Sbill #include <utmp.h>
201043Sbill #include <signal.h>
211043Sbill #include <pwd.h>
221043Sbill #include <stdio.h>
231043Sbill #include <lastlog.h>
2412678Ssam #include <errno.h>
2516453Sroot #include <ttyent.h>
2616453Sroot #include <syslog.h>
271043Sbill 
2816453Sroot #define	SCMPN(a, b)	strncmp(a, b, sizeof(a))
292822Swnj #define	SCPYN(a, b)	strncpy(a, b, sizeof(a))
302822Swnj 
316197Sroot #define NMAX	sizeof(utmp.ut_name)
321043Sbill 
332822Swnj #define	FALSE	0
342822Swnj #define	TRUE	-1
352822Swnj 
362822Swnj char	nolog[] =	"/etc/nologin";
372822Swnj char	qlog[]  =	".hushlogin";
381043Sbill char	maildir[30] =	"/usr/spool/mail/";
391043Sbill char	lastlog[] =	"/usr/adm/lastlog";
409867Ssam struct	passwd nouser = {"", "nope", -1, -1, -1, "", "", "", "" };
411043Sbill struct	sgttyb ttyb;
421043Sbill struct	utmp utmp;
431043Sbill char	minusnam[16] = "-";
4412687Ssam /*
4512687Ssam  * This bounds the time given to login.  We initialize it here
4612687Ssam  * so it can be patched on machines where it's too small.
4712687Ssam  */
4812687Ssam int	timeout = 60;
496005Swnj 
501043Sbill char	homedir[64] = "HOME=";
511043Sbill char	shell[64] = "SHELL=";
521043Sbill char	term[64] = "TERM=";
532822Swnj char	user[20] = "USER=";
546005Swnj 
556005Swnj char	*envinit[] =
5612687Ssam     { homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", term, user, 0 };
576005Swnj 
581043Sbill struct	passwd *pwd;
596005Swnj char	*strcat(), *rindex(), *index();
6012687Ssam int	timedout();
611043Sbill char	*ttyname();
621043Sbill char	*crypt();
631043Sbill char	*getpass();
641043Sbill char	*stypeof();
651043Sbill extern	char **environ;
6612678Ssam extern	int errno;
671043Sbill 
6813074Ssam struct	tchars tc = {
6913074Ssam 	CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
701365Sbill };
7113074Ssam struct	ltchars ltc = {
7213074Ssam 	CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT
7313074Ssam };
741365Sbill 
756005Swnj int	rflag;
766197Sroot char	rusername[NMAX+1], lusername[NMAX+1];
776005Swnj char	rpassword[NMAX+1];
786878Smckusick char	name[NMAX+1];
796197Sroot char	*rhost;
806005Swnj 
811043Sbill main(argc, argv)
8212687Ssam 	char *argv[];
831043Sbill {
841043Sbill 	register char *namep;
8516453Sroot 	int t, f, c;
8612687Ssam 	int invalid, quietlog;
872822Swnj 	FILE *nlfd;
8816453Sroot 	char *ttyn, *tty;
896197Sroot 	int ldisc = 0, zero = 0;
901043Sbill 
9112687Ssam 	signal(SIGALRM, timedout);
9212687Ssam 	alarm(timeout);
931043Sbill 	signal(SIGQUIT, SIG_IGN);
941043Sbill 	signal(SIGINT, SIG_IGN);
9512687Ssam 	setpriority(PRIO_PROCESS, 0, 0);
9612678Ssam 	quota(Q_SETUID, 0, 0, 0);
9712687Ssam 	/*
9812687Ssam 	 * -r is used by rlogind to cause the autologin protocol;
9912687Ssam 	 * -h is used by other servers to pass the name of the
10012687Ssam 	 * remote host to login so that it may be placed in utmp and wtmp
10112687Ssam 	 */
10212687Ssam 	if (argc > 1) {
10312687Ssam 		if (strcmp(argv[1], "-r") == 0) {
10412687Ssam 			rflag = doremotelogin(argv[2]);
10512687Ssam 			SCPYN(utmp.ut_host, argv[2]);
10612687Ssam 			argc = 0;
1076197Sroot 		}
10812687Ssam 		if (strcmp(argv[1], "-h") == 0 && getuid() == 0) {
10912687Ssam 			SCPYN(utmp.ut_host, argv[2]);
11012687Ssam 			argc = 0;
1116197Sroot 		}
1126005Swnj 	}
11313074Ssam 	ioctl(0, TIOCLSET, &zero);
1141547Sbill 	ioctl(0, TIOCNXCL, 0);
1156329Swnj 	ioctl(0, FIONBIO, &zero);
1166329Swnj 	ioctl(0, FIOASYNC, &zero);
11713074Ssam 	ioctl(0, TIOCGETP, &ttyb);
11812687Ssam 	/*
11912687Ssam 	 * If talking to an rlogin process,
12012687Ssam 	 * propagate the terminal type and
12112687Ssam 	 * baud rate across the network.
12212687Ssam 	 */
12312687Ssam 	if (rflag)
12412687Ssam 		doremoteterm(term, &ttyb);
12513074Ssam 	ioctl(0, TIOCSLTC, &ltc);
12613074Ssam 	ioctl(0, TIOCSETC, &tc);
12713074Ssam 	ioctl(0, TIOCSETP, &ttyb);
12812687Ssam 	for (t = getdtablesize(); t > 3; t--)
1291043Sbill 		close(t);
1301043Sbill 	ttyn = ttyname(0);
13116453Sroot 	if (ttyn == (char *)0)
1321043Sbill 		ttyn = "/dev/tty??";
13316453Sroot 	tty = rindex(ttyn, '/');
13416453Sroot 	if (tty == NULL)
13516453Sroot 		tty = ttyn;
13616453Sroot 	else
13716453Sroot 		tty++;
13816453Sroot 	openlog("login", 0, 0);
13916453Sroot 	t = 0;
1402822Swnj 	do {
1412822Swnj 		ldisc = 0;
1422822Swnj 		ioctl(0, TIOCSETD, &ldisc);
1432822Swnj 		invalid = FALSE;
1442822Swnj 		SCPYN(utmp.ut_name, "");
14512687Ssam 		/*
14612687Ssam 		 * Name specified, take it.
14712687Ssam 		 */
14812687Ssam 		if (argc > 1) {
1492822Swnj 			SCPYN(utmp.ut_name, argv[1]);
1502822Swnj 			argc = 0;
1511043Sbill 		}
15212687Ssam 		/*
15312687Ssam 		 * If remote login take given name,
15412687Ssam 		 * otherwise prompt user for something.
15512687Ssam 		 */
1566329Swnj 		if (rflag) {
1579867Ssam 			SCPYN(utmp.ut_name, lusername);
15812687Ssam 			/* autologin failed, prompt for passwd */
1596329Swnj 			if (rflag == -1)
1606329Swnj 				rflag = 0;
16113470Ssam 		} else
16212687Ssam 			getloginname(&utmp);
1632822Swnj 		if (!strcmp(pwd->pw_shell, "/bin/csh")) {
1642822Swnj 			ldisc = NTTYDISC;
1652822Swnj 			ioctl(0, TIOCSETD, &ldisc);
1662822Swnj 		}
16712687Ssam 		/*
16812687Ssam 		 * If no remote login authentication and
16912687Ssam 		 * a password exists for this user, prompt
17012687Ssam 		 * for one and verify it.
17112687Ssam 		 */
17212687Ssam 		if (!rflag && *pwd->pw_passwd != '\0') {
17312687Ssam 			char *pp;
17412687Ssam 
17512687Ssam 			setpriority(PRIO_PROCESS, 0, -4);
17612687Ssam 			pp = getpass("Password:");
17712687Ssam 			namep = crypt(pp, pwd->pw_passwd);
17812687Ssam 			setpriority(PRIO_PROCESS, 0, 0);
17912687Ssam 			if (strcmp(namep, pwd->pw_passwd))
18012687Ssam 				invalid = TRUE;
1812822Swnj 		}
18212687Ssam 		/*
18312687Ssam 		 * If user not super-user, check for logins disabled.
18412687Ssam 		 */
1852822Swnj 		if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) {
1862822Swnj 			while ((c = getc(nlfd)) != EOF)
1872822Swnj 				putchar(c);
1882822Swnj 			fflush(stdout);
1892822Swnj 			sleep(5);
1902822Swnj 			exit(0);
1912822Swnj 		}
19212687Ssam 		/*
19312687Ssam 		 * If valid so far and root is logging in,
19412687Ssam 		 * see if root logins on this terminal are permitted.
19512687Ssam 		 */
19616453Sroot 		if (!invalid && pwd->pw_uid == 0 && !rootterm(tty)) {
19716453Sroot 			syslog(LOG_INFO, "ROOT LOGIN REFUSED %s", tty);
1982822Swnj 			invalid = TRUE;
1992822Swnj 		}
2002822Swnj 		if (invalid) {
2011043Sbill 			printf("Login incorrect\n");
20216453Sroot 			if (++t >= 5) {
20316453Sroot 				syslog(LOG_INFO,
20416453Sroot 				    "REPEATED LOGIN FAILURES %s, %s",
20516453Sroot 					tty, utmp.ut_name);
20616453Sroot 				ioctl(0, TIOCHPCL, (struct sgttyb *) 0);
20716453Sroot 				close(0);
20816453Sroot 				close(1);
20916453Sroot 				close(2);
21016453Sroot 				sleep(10);
21116453Sroot 				exit(1);
21216453Sroot 			}
2131043Sbill 		}
2142822Swnj 		if (*pwd->pw_shell == '\0')
2152822Swnj 			pwd->pw_shell = "/bin/sh";
2162822Swnj 		if (chdir(pwd->pw_dir) < 0 && !invalid ) {
2172822Swnj 			if (chdir("/") < 0) {
2182822Swnj 				printf("No directory!\n");
2192822Swnj 				invalid = TRUE;
2202822Swnj 			} else {
22112687Ssam 				printf("No directory! %s\n",
22212687Ssam 				   "Logging in with home=/");
2232822Swnj 				pwd->pw_dir = "/";
2242822Swnj 			}
2251043Sbill 		}
22612687Ssam 		/*
22712687Ssam 		 * Remote login invalid must have been because
22812687Ssam 		 * of a restriction of some sort, no extra chances.
22912687Ssam 		 */
2306005Swnj 		if (rflag && invalid)
2316005Swnj 			exit(1);
2322822Swnj 	} while (invalid);
23312687Ssam /* committed to login turn off timeout */
23412687Ssam 	alarm(0);
2351043Sbill 
23612678Ssam 	if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0) {
23712678Ssam 		if (errno == EUSERS)
23812678Ssam 			printf("%s.\n%s.\n",
23912678Ssam 			   "Too many users logged on already",
24012678Ssam 			   "Try again later");
24112678Ssam 		else if (errno == EPROCLIM)
24212678Ssam 			printf("You have too many processes running.\n");
24312678Ssam 		else
244*17664Sserge 			perror("quota (Q_SETUID)");
24512678Ssam 		sleep(5);
24612678Ssam 		exit(0);
24712678Ssam 	}
2481043Sbill 	time(&utmp.ut_time);
2491043Sbill 	t = ttyslot();
25016453Sroot 	if (t > 0 && (f = open("/etc/utmp", O_WRONLY)) >= 0) {
2511043Sbill 		lseek(f, (long)(t*sizeof(utmp)), 0);
25216453Sroot 		SCPYN(utmp.ut_line, tty);
2531043Sbill 		write(f, (char *)&utmp, sizeof(utmp));
2541043Sbill 		close(f);
2551043Sbill 	}
25616453Sroot 	if ((f = open("/usr/adm/wtmp", O_WRONLY|O_APPEND)) >= 0) {
2571043Sbill 		write(f, (char *)&utmp, sizeof(utmp));
2581043Sbill 		close(f);
2591043Sbill 	}
26016453Sroot 	quietlog = access(qlog, F_OK) == 0;
26116453Sroot 	if ((f = open(lastlog, O_RDWR)) >= 0) {
2622822Swnj 		struct lastlog ll;
2632822Swnj 
2642822Swnj 		lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
2652822Swnj 		if (read(f, (char *) &ll, sizeof ll) == sizeof ll &&
26612687Ssam 		    ll.ll_time != 0 && !quietlog) {
26712687Ssam 			printf("Last login: %.*s ",
26812687Ssam 			    24-5, (char *)ctime(&ll.ll_time));
26912687Ssam 			if (*ll.ll_host != '\0')
27012687Ssam 				printf("from %.*s\n",
27112687Ssam 				    sizeof (ll.ll_host), ll.ll_host);
27212687Ssam 			else
27312687Ssam 				printf("on %.*s\n",
27412687Ssam 				    sizeof (ll.ll_line), ll.ll_line);
2752822Swnj 		}
2762822Swnj 		lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
2772822Swnj 		time(&ll.ll_time);
27816453Sroot 		SCPYN(ll.ll_line, tty);
27912687Ssam 		SCPYN(ll.ll_host, utmp.ut_host);
2802822Swnj 		write(f, (char *) &ll, sizeof ll);
2812822Swnj 		close(f);
2822822Swnj 	}
2831043Sbill 	chown(ttyn, pwd->pw_uid, pwd->pw_gid);
2849867Ssam 	chmod(ttyn, 0622);
2851043Sbill 	setgid(pwd->pw_gid);
2866878Smckusick 	strncpy(name, utmp.ut_name, NMAX);
2876878Smckusick 	name[NMAX] = '\0';
2889224Ssam 	initgroups(name, pwd->pw_gid);
28912678Ssam 	quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0);
2901043Sbill 	setuid(pwd->pw_uid);
2911043Sbill 	environ = envinit;
2921043Sbill 	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
2931043Sbill 	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
294*17664Sserge 	if (term[sizeof("TERM=")-1] == 0)
29516453Sroot 		strncat(term, stypeof(tty), sizeof(term)-6);
2962822Swnj 	strncat(user, pwd->pw_name, sizeof(user)-6);
2971043Sbill 	if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
2981043Sbill 		namep = pwd->pw_shell;
2991043Sbill 	else
3001043Sbill 		namep++;
3011043Sbill 	strcat(minusnam, namep);
30216453Sroot 	if (tty[sizeof("tty")-1] == 'd')
30316453Sroot 		syslog(LOG_INFO, "DIALUP %s %s", tty, pwd->pw_name);
3046329Swnj 	if (!quietlog) {
305*17664Sserge 		struct stat st;
3062822Swnj 		showmotd();
3072822Swnj 		strcat(maildir, pwd->pw_name);
308*17664Sserge 		if (stat(maildir, &st) == 0 && st.st_size != 0)
309*17664Sserge 			printf("You have %smail.\n",
310*17664Sserge 				(st.st_mtime > st.st_atime) ? "new" : "");
3112822Swnj 	}
31212687Ssam 	signal(SIGALRM, SIG_DFL);
3131043Sbill 	signal(SIGQUIT, SIG_DFL);
3141043Sbill 	signal(SIGINT, SIG_DFL);
3153935Sroot 	signal(SIGTSTP, SIG_IGN);
3161043Sbill 	execlp(pwd->pw_shell, minusnam, 0);
3172822Swnj 	perror(pwd->pw_shell);
3181043Sbill 	printf("No shell\n");
3191043Sbill 	exit(0);
3201043Sbill }
3211043Sbill 
32212687Ssam getloginname(up)
32312687Ssam 	register struct utmp *up;
32412687Ssam {
32512687Ssam 	register char *namep;
32612712Ssam 	char c;
32712687Ssam 
32812687Ssam 	while (up->ut_name[0] == '\0') {
32914897Sedward 		namep = up->ut_name;
33012712Ssam 		printf("login: ");
33112687Ssam 		while ((c = getchar()) != '\n') {
33212687Ssam 			if (c == ' ')
33312687Ssam 				c = '_';
33412687Ssam 			if (c == EOF)
33512687Ssam 				exit(0);
33612687Ssam 			if (namep < up->ut_name+NMAX)
33712687Ssam 				*namep++ = c;
33812687Ssam 		}
33912687Ssam 	}
34014897Sedward 	strncpy(lusername, up->ut_name, NMAX);
34114897Sedward 	lusername[NMAX] = 0;
34214897Sedward 	if ((pwd = getpwnam(lusername)) == NULL)
34312687Ssam 		pwd = &nouser;
34412687Ssam }
34512687Ssam 
34612687Ssam timedout()
34712687Ssam {
34812687Ssam 
34912687Ssam 	printf("Login timed out after %d seconds\n", timeout);
35012687Ssam 	exit(0);
35112687Ssam }
35212687Ssam 
3531043Sbill int	stopmotd;
3541043Sbill catch()
3551043Sbill {
3566466Swnj 
3571043Sbill 	signal(SIGINT, SIG_IGN);
3581043Sbill 	stopmotd++;
3591043Sbill }
3601043Sbill 
3612822Swnj rootterm(tty)
3626466Swnj 	char *tty;
3632822Swnj {
36416453Sroot 	register struct ttyent *t;
3652822Swnj 
36616453Sroot 	if ((t = getttynam(tty)) != NULL) {
36716453Sroot 		if (t->ty_status & TTY_SECURE)
36816453Sroot 			return (1);
3692822Swnj 	}
37016453Sroot 	return (0);
3712822Swnj }
3722822Swnj 
3731043Sbill showmotd()
3741043Sbill {
3751043Sbill 	FILE *mf;
3761043Sbill 	register c;
3771043Sbill 
3781043Sbill 	signal(SIGINT, catch);
37916453Sroot 	if ((mf = fopen("/etc/motd", "r")) != NULL) {
3802822Swnj 		while ((c = getc(mf)) != EOF && stopmotd == 0)
3811043Sbill 			putchar(c);
3821043Sbill 		fclose(mf);
3831043Sbill 	}
3841043Sbill 	signal(SIGINT, SIG_IGN);
3851043Sbill }
3861043Sbill 
3872822Swnj #undef	UNKNOWN
3881043Sbill #define UNKNOWN "su"
3891043Sbill 
3901043Sbill char *
3911043Sbill stypeof(ttyid)
39212687Ssam 	char *ttyid;
3931043Sbill {
39416453Sroot 	register struct ttyent *t;
3951043Sbill 
39616453Sroot 	if (ttyid == NULL || (t = getttynam(ttyid)) == NULL)
3971043Sbill 		return (UNKNOWN);
39816453Sroot 	return (t->ty_type);
3991043Sbill }
4006005Swnj 
40112687Ssam doremotelogin(host)
40212687Ssam 	char *host;
40312687Ssam {
40412687Ssam 	FILE *hostf;
40512687Ssam 	int first = 1;
40612687Ssam 
40712687Ssam 	getstr(rusername, sizeof (rusername), "remuser");
40812687Ssam 	getstr(lusername, sizeof (lusername), "locuser");
40912687Ssam 	getstr(term+5, sizeof(term)-5, "Terminal type");
41013470Ssam 	if (getuid()) {
41113470Ssam 		pwd = &nouser;
41212687Ssam 		goto bad;
41313470Ssam 	}
41412687Ssam 	pwd = getpwnam(lusername);
41513470Ssam 	if (pwd == NULL) {
41613470Ssam 		pwd = &nouser;
41712687Ssam 		goto bad;
41813470Ssam 	}
41912687Ssam 	hostf = pwd->pw_uid ? fopen("/etc/hosts.equiv", "r") : 0;
42012687Ssam again:
42112687Ssam 	if (hostf) {
42212687Ssam 		char ahost[32];
42312687Ssam 
42412687Ssam 		while (fgets(ahost, sizeof (ahost), hostf)) {
42512687Ssam 			char *user;
42612687Ssam 
42712687Ssam 			if ((user = index(ahost, '\n')) != 0)
42812687Ssam 				*user++ = '\0';
42912687Ssam 			if ((user = index(ahost, ' ')) != 0)
43012687Ssam 				*user++ = '\0';
43112687Ssam 			if (!strcmp(host, ahost) &&
43212687Ssam 			    !strcmp(rusername, user ? user : lusername)) {
43312687Ssam 				fclose(hostf);
43412687Ssam 				return (1);
43512687Ssam 			}
43612687Ssam 		}
43712687Ssam 		fclose(hostf);
43812687Ssam 	}
43912687Ssam 	if (first == 1) {
44012687Ssam 		char *rhosts = ".rhosts";
44112687Ssam 		struct stat sbuf;
44212687Ssam 
44312687Ssam 		first = 0;
44412687Ssam 		if (chdir(pwd->pw_dir) < 0)
44512687Ssam 			goto again;
44612687Ssam 		if (lstat(rhosts, &sbuf) < 0)
44712687Ssam 			goto again;
44812687Ssam 		if ((sbuf.st_mode & S_IFMT) == S_IFLNK) {
44912687Ssam 			printf("login: .rhosts is a soft link.\r\n");
45012687Ssam 			goto bad;
45112687Ssam 		}
45212687Ssam 		hostf = fopen(rhosts, "r");
45312687Ssam 		fstat(fileno(hostf), &sbuf);
45412687Ssam 		if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) {
45512687Ssam 			printf("login: Bad .rhosts ownership.\r\n");
45612687Ssam 			fclose(hostf);
45712687Ssam 			goto bad;
45812687Ssam 		}
45912687Ssam 		goto again;
46012687Ssam 	}
46112687Ssam bad:
46212687Ssam 	return (-1);
46312687Ssam }
46412687Ssam 
4656005Swnj getstr(buf, cnt, err)
4666005Swnj 	char *buf;
4676005Swnj 	int cnt;
4686005Swnj 	char *err;
4696005Swnj {
4706005Swnj 	char c;
4716005Swnj 
4726005Swnj 	do {
4736005Swnj 		if (read(0, &c, 1) != 1)
4746005Swnj 			exit(1);
4756005Swnj 		if (--cnt < 0) {
4766005Swnj 			printf("%s too long\r\n", err);
4776005Swnj 			exit(1);
4786005Swnj 		}
4796005Swnj 		*buf++ = c;
4806005Swnj 	} while (c != 0);
4816005Swnj }
4826329Swnj 
48312687Ssam char	*speeds[] =
48412687Ssam     { "0", "50", "75", "110", "134", "150", "200", "300",
48512687Ssam       "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" };
48612687Ssam #define	NSPEEDS	(sizeof (speeds) / sizeof (speeds[0]))
48712687Ssam 
48812687Ssam doremoteterm(term, tp)
48912687Ssam 	char *term;
49012687Ssam 	struct sgttyb *tp;
49112687Ssam {
49212687Ssam 	char *cp = index(term, '/');
49312687Ssam 	register int i;
49412687Ssam 
49512687Ssam 	if (cp) {
49612687Ssam 		*cp++ = 0;
49712687Ssam 		for (i = 0; i < NSPEEDS; i++)
49812687Ssam 			if (!strcmp(speeds[i], cp)) {
49912687Ssam 				tp->sg_ispeed = tp->sg_ospeed = i;
50012687Ssam 				break;
50112687Ssam 			}
50212687Ssam 	}
50312687Ssam 	tp->sg_flags = ECHO|CRMOD|ANYP|XTABS;
50412687Ssam }
505