xref: /csrg-svn/usr.bin/login/login.c (revision 1547)
1*1547Sbill static	char *sccsid = "@(#)login.c	4.4 (Berkeley) 10/19/80";
21043Sbill /*
31043Sbill  * login [ name ]
41043Sbill  */
51043Sbill 
61043Sbill #include <sys/types.h>
71043Sbill #include <sgtty.h>
81043Sbill #include <utmp.h>
91043Sbill #include <signal.h>
101043Sbill #include <pwd.h>
111043Sbill #include <stdio.h>
121043Sbill #include <sys/stat.h>
131043Sbill #include <lastlog.h>
141043Sbill #define SCPYN(a, b)	strncpy(a, b, sizeof(a))
151043Sbill 
161043Sbill #define NMAX sizeof(utmp.ut_name)
171043Sbill #define LMAX sizeof(utmp.ut_line)
181043Sbill 
191043Sbill char	user[20];
201043Sbill char	maildir[30] =	"/usr/spool/mail/";
211043Sbill char	lastlog[] =	"/usr/adm/lastlog";
221043Sbill struct	passwd nouser = {"", "nope"};
231043Sbill struct	sgttyb ttyb;
241043Sbill struct	utmp utmp;
251043Sbill char	minusnam[16] = "-";
261043Sbill char	homedir[64] = "HOME=";
271043Sbill char	shell[64] = "SHELL=";
281043Sbill char	term[64] = "TERM=";
291043Sbill char	*envinit[] = {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", term, user,0};
301043Sbill struct	passwd *pwd;
311043Sbill 
321043Sbill struct	passwd *getpwnam();
331043Sbill char	*strcat();
341043Sbill int	setpwent();
351043Sbill char	*ttyname();
361043Sbill char	*crypt();
371043Sbill char	*getpass();
381043Sbill char	*rindex();
391043Sbill char	*stypeof();
401043Sbill extern	char **environ;
411043Sbill 
421365Sbill #define	CTRL(c)	('c'&037)
431365Sbill #define	CERASE	'#'
441365Sbill #define	CEOT	CTRL(d)
451365Sbill #define	CKILL	'@'
461365Sbill #define	CQUIT	034		/* FS, cntl shift L */
471365Sbill #define	CINTR	0177		/* DEL */
481365Sbill #define	CSTOP	CTRL(s)
491365Sbill #define	CSTART	CTRL(q)
501365Sbill #define	CBRK	0377
511365Sbill struct	tchars tc = {
521365Sbill 	CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
531365Sbill };
541365Sbill struct	ltchars ltc = {
551546Sbill 	CTRL(z), CTRL(y), CTRL(r), CTRL(o), CTRL(w), CTRL(v)
561365Sbill };
571365Sbill 
581043Sbill main(argc, argv)
591043Sbill char **argv;
601043Sbill {
611043Sbill 	register char *namep;
621043Sbill 	int t, f, c;
631043Sbill 	char *ttyn;
641043Sbill 	int ldisc = 0;
651043Sbill 
661043Sbill 	alarm(60);
671043Sbill 	signal(SIGQUIT, SIG_IGN);
681043Sbill 	signal(SIGINT, SIG_IGN);
691043Sbill 	nice(-100);
701043Sbill 	nice(20);
711043Sbill 	nice(0);
721043Sbill 	ioctl(0, TIOCLSET, 0);
73*1547Sbill 	ioctl(0, TIOCNXCL, 0);
741043Sbill 	gtty(0, &ttyb);
751043Sbill 	ttyb.sg_erase = '#';
761043Sbill 	ttyb.sg_kill = '@';
771043Sbill 	stty(0, &ttyb);
781365Sbill 	ioctl(0, TIOCSETC, &tc);
791365Sbill 	ioctl(0, TIOCSLTC, &ltc);
801043Sbill 	for (t=3; t<20; t++)
811043Sbill 		close(t);
821043Sbill 	ttyn = ttyname(0);
831043Sbill 	if (ttyn==0)
841043Sbill 		ttyn = "/dev/tty??";
851043Sbill 
861043Sbill     loop:
87*1547Sbill 	ldisc = 0;
88*1547Sbill 	ioctl(0, TIOCSETD, &ldisc);
891043Sbill 	SCPYN(utmp.ut_name, "");
901043Sbill 	if (argc>1) {
911043Sbill 		SCPYN(utmp.ut_name, argv[1]);
921043Sbill 		argc = 0;
931043Sbill 	}
941043Sbill 	while (utmp.ut_name[0] == '\0') {
951043Sbill 		namep = utmp.ut_name;
961043Sbill 		printf("login: ");
971043Sbill 		while ((c = getchar()) != '\n') {
981043Sbill 			if(c == ' ')
991043Sbill 				c = '_';
1001043Sbill 			if (c == EOF)
1011043Sbill 				exit(0);
1021043Sbill 			if (namep < utmp.ut_name+NMAX)
1031043Sbill 				*namep++ = c;
1041043Sbill 		}
1051043Sbill 	}
1061043Sbill 	setpwent();
1071043Sbill 	if ((pwd = getpwnam(utmp.ut_name)) == NULL)
1081043Sbill 		pwd = &nouser;
1091043Sbill 	endpwent();
110*1547Sbill 	if (!strcmp(pwd->pw_shell, "/bin/csh")) {
111*1547Sbill 		ldisc = NTTYDISC;
112*1547Sbill 		ioctl(0, TIOCSETD, &ldisc);
113*1547Sbill 	}
1141043Sbill 	if (*pwd->pw_passwd != '\0') {
115*1547Sbill 		nice(-4);
1161043Sbill 		namep = crypt(getpass("Password:"),pwd->pw_passwd);
117*1547Sbill 		nice(4);
1181043Sbill 		if (strcmp(namep, pwd->pw_passwd)) {
1191043Sbill bad:
1201043Sbill 			printf("Login incorrect\n");
1211043Sbill 			if (ttyn[LMAX] == 'd') {
1221043Sbill 				FILE *console = fopen("/dev/console", "w");
1231043Sbill 				if (console != NULL) {
1241043Sbill 					fprintf(console, "\r\nBADDIALUP %s %s\r\n", ttyn+5, utmp.ut_name);
1251043Sbill 					fclose(console);
1261043Sbill 				}
1271043Sbill 			}
1281043Sbill 			goto loop;
1291043Sbill 		}
1301043Sbill 	}
1311043Sbill 	sprintf(user, "USER=%.*s", NMAX, pwd->pw_name);
1321043Sbill 	if (pwd->pw_uid == 0 && ttyn[5] != 'c')
1331043Sbill 		goto bad;
1341043Sbill 	if (ttyn[LMAX] == 'd') {
1351043Sbill 		FILE *console = fopen("/dev/console", "w");
1361043Sbill 		if (console != NULL) {
1371043Sbill 			fprintf(console, "\r\nDIALUP %s %s\r\n", ttyn+5, pwd->pw_name);
1381043Sbill 			fclose(console);
1391043Sbill 		}
1401043Sbill 	}
1411043Sbill 	if((f = open(lastlog, 2)) >= 0) {
1421043Sbill 		struct lastlog ll;
1431043Sbill 
1441043Sbill 		lseek(f, pwd->pw_uid * sizeof (struct lastlog), 0);
1451043Sbill 		if (read(f, (char *) &ll, sizeof ll) == sizeof ll && ll.ll_time != 0) {
1461043Sbill 			register char *ep = (char *) ctime(&ll.ll_time);
1471043Sbill 			printf("Last login: ");
1481043Sbill 			ep[24 - 5] = 0;
1491043Sbill 			printf("%s on %.*s\n", ep, LMAX, ll.ll_line);
1501043Sbill 		}
1511043Sbill 		lseek(f, pwd->pw_uid * sizeof (struct lastlog), 0);
1521043Sbill 		time(&ll.ll_time);
1531043Sbill 		strcpyn(ll.ll_line, ttyn+5, LMAX);
1541043Sbill 		write(f, (char *) &ll, sizeof ll);
1551043Sbill 		close(f);
1561043Sbill 	}
1571043Sbill 	if(chdir(pwd->pw_dir) < 0) {
1581043Sbill 		printf("No directory\n");
1591043Sbill 		goto loop;
1601043Sbill 	}
1611043Sbill 	time(&utmp.ut_time);
1621043Sbill 	t = ttyslot();
1631043Sbill 	if (t>0 && (f = open("/etc/utmp", 1)) >= 0) {
1641043Sbill 		lseek(f, (long)(t*sizeof(utmp)), 0);
1651043Sbill 		SCPYN(utmp.ut_line, rindex(ttyn, '/')+1);
1661043Sbill 		write(f, (char *)&utmp, sizeof(utmp));
1671043Sbill 		close(f);
1681043Sbill 	}
1691043Sbill 	if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) {
1701043Sbill 		lseek(f, 0L, 2);
1711043Sbill 		write(f, (char *)&utmp, sizeof(utmp));
1721043Sbill 		close(f);
1731043Sbill 	}
1741043Sbill 	chown(ttyn, pwd->pw_uid, pwd->pw_gid);
1751043Sbill 	setgid(pwd->pw_gid);
1761043Sbill 	setuid(pwd->pw_uid);
1771043Sbill 	if (*pwd->pw_shell == '\0')
1781043Sbill 		pwd->pw_shell = "/bin/sh";
1791043Sbill 	environ = envinit;
1801043Sbill 	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
1811043Sbill 	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
1821043Sbill 	strncat(term, stypeof(ttyn), sizeof(term)-6);
1831043Sbill 	if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
1841043Sbill 		namep = pwd->pw_shell;
1851043Sbill 	else
1861043Sbill 		namep++;
1871043Sbill 	strcat(minusnam, namep);
1881043Sbill 	alarm(0);
1891043Sbill 	umask(022);
1901043Sbill 	showmotd();
1911043Sbill 	strcat(maildir, pwd->pw_name);
1921043Sbill 	if(access(maildir,4)==0) {
1931043Sbill 		struct stat statb;
1941043Sbill 		stat(maildir, &statb);
1951043Sbill 		if (statb.st_size)
1961043Sbill 			printf("You have mail.\n");
1971043Sbill 	}
1981043Sbill 	signal(SIGQUIT, SIG_DFL);
1991043Sbill 	signal(SIGINT, SIG_DFL);
2001043Sbill 	execlp(pwd->pw_shell, minusnam, 0);
2011043Sbill 	printf("No shell\n");
2021043Sbill 	exit(0);
2031043Sbill }
2041043Sbill 
2051043Sbill int	stopmotd;
2061043Sbill catch()
2071043Sbill {
2081043Sbill 	signal(SIGINT, SIG_IGN);
2091043Sbill 	stopmotd++;
2101043Sbill }
2111043Sbill 
2121043Sbill showmotd()
2131043Sbill {
2141043Sbill 	FILE *mf;
2151043Sbill 	register c;
2161043Sbill 
2171043Sbill 	signal(SIGINT, catch);
2181043Sbill 	if((mf = fopen("/etc/motd","r")) != NULL) {
2191043Sbill 		while((c = getc(mf)) != EOF && stopmotd == 0)
2201043Sbill 			putchar(c);
2211043Sbill 		fclose(mf);
2221043Sbill 	}
2231043Sbill 	signal(SIGINT, SIG_IGN);
2241043Sbill }
2251043Sbill 
2261043Sbill #define UNKNOWN "su"
2271043Sbill 
2281043Sbill char *
2291043Sbill stypeof(ttyid)
2301043Sbill char	*ttyid;
2311043Sbill {
2321043Sbill 	static char	typebuf[16];
2331043Sbill 	char		buf[50];
2341043Sbill 	register FILE	*f;
2351043Sbill 	register char	*p, *t, *q;
2361043Sbill 
2371043Sbill 	if (ttyid == NULL)
2381043Sbill 		return (UNKNOWN);
2391043Sbill 	f = fopen("/etc/ttytype", "r");
2401043Sbill 	if (f == NULL)
2411043Sbill 		return (UNKNOWN);
2421043Sbill 	/* split off end of name */
2431043Sbill 	for (p = q = ttyid; *p != 0; p++)
2441043Sbill 		if (*p == '/')
2451043Sbill 			q = p + 1;
2461043Sbill 
2471043Sbill 	/* scan the file */
2481043Sbill 	while (fgets(buf, sizeof buf, f) != NULL)
2491043Sbill 	{
2501043Sbill 		for (t=buf; *t!=' '; t++)
2511043Sbill 			;
2521043Sbill 		*t++ = 0;
2531043Sbill 		for (p=t; *p>' '; p++)
2541043Sbill 			;
2551043Sbill 		*p = 0;
2561043Sbill 		if (strcmp(q,t)==0) {
2571043Sbill 			strcpy(typebuf, buf);
2581043Sbill 			fclose(f);
2591043Sbill 			return (typebuf);
2601043Sbill 		}
2611043Sbill 	}
2621043Sbill 	fclose (f);
2631043Sbill 	return (UNKNOWN);
2641043Sbill }
265