xref: /csrg-svn/usr.bin/login/login.c (revision 11813)
1*11813Sleres static	char *sccsid = "@(#)login.c	4.21 83/03/31";
21043Sbill /*
31043Sbill  * login [ name ]
46005Swnj  * login -r
51043Sbill  */
61043Sbill 
71043Sbill #include <sys/types.h>
81043Sbill #include <sgtty.h>
91043Sbill #include <utmp.h>
101043Sbill #include <signal.h>
111043Sbill #include <pwd.h>
121043Sbill #include <stdio.h>
131043Sbill #include <sys/stat.h>
141043Sbill #include <lastlog.h>
151043Sbill 
162822Swnj #define	SCPYN(a, b)	strncpy(a, b, sizeof(a))
172822Swnj 
186197Sroot #define NMAX	sizeof(utmp.ut_name)
196197Sroot #define LMAX	sizeof(utmp.ut_line)
201043Sbill 
212822Swnj #define	FALSE	0
222822Swnj #define	TRUE	-1
232822Swnj 
242822Swnj char	nolog[] =	"/etc/nologin";
252822Swnj char	qlog[]  =	".hushlogin";
262822Swnj char	securetty[] =	"/etc/securetty";
271043Sbill char	maildir[30] =	"/usr/spool/mail/";
281043Sbill char	lastlog[] =	"/usr/adm/lastlog";
299867Ssam struct	passwd nouser = {"", "nope", -1, -1, -1, "", "", "", "" };
301043Sbill struct	sgttyb ttyb;
311043Sbill struct	utmp utmp;
321043Sbill char	minusnam[16] = "-";
336005Swnj 
341043Sbill char	homedir[64] = "HOME=";
351043Sbill char	shell[64] = "SHELL=";
361043Sbill char	term[64] = "TERM=";
372822Swnj char	user[20] = "USER=";
386005Swnj char	*speeds[] =
396005Swnj     { "0", "50", "75", "110", "134", "150", "200", "300",
406005Swnj       "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" };
416005Swnj #define	NSPEEDS	(sizeof (speeds) / sizeof (speeds[0]))
426005Swnj 
436005Swnj char	*envinit[] =
446005Swnj     {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", term, user, 0};
456005Swnj 
461043Sbill struct	passwd *pwd;
471043Sbill struct	passwd *getpwnam();
486005Swnj char	*strcat(), *rindex(), *index();
491043Sbill int	setpwent();
501043Sbill char	*ttyname();
511043Sbill char	*crypt();
521043Sbill char	*getpass();
531043Sbill char	*rindex();
541043Sbill char	*stypeof();
551043Sbill extern	char **environ;
561043Sbill 
579867Ssam struct	ttychars tc = {
589867Ssam 	CERASE,	CKILL,	CINTR,	CQUIT,	CSTART,
599867Ssam 	CSTOP,	CEOF,	CBRK,	CSUSP,	CDSUSP,
609867Ssam 	CRPRNT,	CFLUSH,	CWERASE,CLNEXT
611365Sbill };
621365Sbill 
636005Swnj int	rflag;
646197Sroot char	rusername[NMAX+1], lusername[NMAX+1];
656005Swnj char	rpassword[NMAX+1];
666878Smckusick char	name[NMAX+1];
676197Sroot char	*rhost;
686005Swnj 
691043Sbill main(argc, argv)
701043Sbill char **argv;
711043Sbill {
721043Sbill 	register char *namep;
731043Sbill 	int t, f, c;
742822Swnj 	int invalid;
752822Swnj 	int quietlog;
762822Swnj 	int i;
772822Swnj 	FILE *nlfd;
781043Sbill 	char *ttyn;
796197Sroot 	int ldisc = 0, zero = 0;
806197Sroot 	FILE *hostf; int first = 1;
811043Sbill 
821043Sbill 	alarm(60);
831043Sbill 	signal(SIGQUIT, SIG_IGN);
841043Sbill 	signal(SIGINT, SIG_IGN);
851043Sbill 	nice(-100);
861043Sbill 	nice(20);
871043Sbill 	nice(0);
886329Swnj 	if (argc > 1 && !strcmp(argv[1], "-r")) {
896005Swnj 		rflag++;
906329Swnj 		rhost = argv[2];
916197Sroot 		argc = 1;
926329Swnj 		getstr(rusername, sizeof (rusername), "remuser");
936329Swnj 		getstr(lusername, sizeof (lusername), "locuser");
946005Swnj 		getstr(term+5, sizeof(term)-5, "Terminal type");
956329Swnj 		if (getuid())
966329Swnj 			goto abnormal;
976197Sroot 		setpwent();
986197Sroot 		pwd = getpwnam(lusername);
996329Swnj 		endpwent();
1006197Sroot 		if (pwd == NULL) {
1016329Swnj 			if (strcmp(rusername, lusername))
1026329Swnj 				printf("%s: No such user\r\n", lusername);
1036329Swnj 			goto abnormal;
1046197Sroot 		}
1056466Swnj 		hostf = pwd->pw_uid ? fopen("/etc/hosts.equiv", "r") : 0;
1066197Sroot 	again:
1076197Sroot 		if (hostf) {
108*11813Sleres 			char ahost[32];
109*11813Sleres 
110*11813Sleres 		        while (fgets(ahost, sizeof (ahost), hostf)) {
111*11813Sleres 				char *user;
112*11813Sleres 
113*11813Sleres 				if (index(ahost, '\n'))
114*11813Sleres 					*index(ahost, '\n') = 0;
115*11813Sleres 				user = index(ahost, ' ');
116*11813Sleres 				if (user)
117*11813Sleres 					*user++ = 0;
118*11813Sleres 				if (!strcmp(rhost, ahost) &&
119*11813Sleres 				    !strcmp(rusername, user ?
120*11813Sleres 				    user : lusername)) {
121*11813Sleres 					fclose(hostf);
122*11813Sleres 					goto normal;
123*11813Sleres 				}
1246329Swnj 			}
125*11813Sleres 			fclose(hostf);
1266197Sroot 		}
1276197Sroot 		if (first == 1) {
128*11813Sleres 			char *rhosts = ".rhosts";
129*11813Sleres 			struct stat sbuf;
130*11813Sleres 
1316197Sroot 			first = 0;
1326197Sroot 			if (chdir(pwd->pw_dir) < 0)
1336197Sroot 				goto again;
134*11813Sleres 			if (lstat(rhosts, &sbuf) < 0)
135*11813Sleres 				goto again;
136*11813Sleres 			if ((sbuf.st_mode & S_IFMT) == S_IFLNK) {
137*11813Sleres 				printf("login: .rhosts is a soft link.\r\n");
138*11813Sleres 			        fclose(hostf);
139*11813Sleres 				goto abnormal;
140*11813Sleres 			}
141*11813Sleres 			hostf = fopen(rhosts, "r");
142*11813Sleres 			fstat(fileno(hostf), &sbuf);
143*11813Sleres 			if ((int) sbuf.st_uid != pwd->pw_uid &&
144*11813Sleres 			    (int) sbuf.st_uid != 0) {
145*11813Sleres 				printf("login: Bad .rhosts ownership.\r\n");
146*11813Sleres 			        fclose(hostf);
147*11813Sleres 				goto abnormal;
148*11813Sleres 			}
149*11813Sleres 		goto again;
1506197Sroot 		}
1516329Swnj abnormal:
1526197Sroot 		rhost = 0;
1536197Sroot 		rflag = -1;
1546005Swnj 	}
1556197Sroot normal:
1569867Ssam 	ioctl(0, TIOCLSET, &zero);	/* XXX */
1571547Sbill 	ioctl(0, TIOCNXCL, 0);
1586329Swnj 	ioctl(0, FIONBIO, &zero);
1596329Swnj 	ioctl(0, FIOASYNC, &zero);
1609867Ssam 	ioctl(0, TIOCGETP, &ttyb);	/* XXX */
1616005Swnj 	if (rflag) {
1626005Swnj 		char *cp = index(term, '/');
1636005Swnj 		if (cp) {
1646005Swnj 			int i;
1656005Swnj 			*cp++ = 0;
1666005Swnj 			for (i = 0; i < NSPEEDS; i++)
1676005Swnj 				if (!strcmp(speeds[i], cp)) {
1686005Swnj 					ttyb.sg_ispeed = ttyb.sg_ospeed = i;
1696005Swnj 					break;
1706005Swnj 				}
1716005Swnj 		}
1726005Swnj 		ttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS;
1736005Swnj 	}
1749867Ssam 	ioctl(0, TIOCSETP, &ttyb);	/* XXX */
1759867Ssam 	ioctl(0, TIOCCSET, &tc);
1761043Sbill 	for (t=3; t<20; t++)
1771043Sbill 		close(t);
1781043Sbill 	ttyn = ttyname(0);
1792822Swnj 	if (ttyn==(char *)0)
1801043Sbill 		ttyn = "/dev/tty??";
1812822Swnj 	do {
1822822Swnj 		ldisc = 0;
1832822Swnj 		ioctl(0, TIOCSETD, &ldisc);
1842822Swnj 		invalid = FALSE;
1852822Swnj 		SCPYN(utmp.ut_name, "");
1862822Swnj 		if (argc>1) {
1872822Swnj 			SCPYN(utmp.ut_name, argv[1]);
1882822Swnj 			argc = 0;
1891043Sbill 		}
1906329Swnj 		if (rflag) {
1919867Ssam 			SCPYN(utmp.ut_name, lusername);
1926329Swnj 			if (rflag == -1)
1936329Swnj 				rflag = 0;
1946329Swnj 		} else
1956197Sroot 			while (utmp.ut_name[0] == '\0') {
1966197Sroot 				namep = utmp.ut_name;
1976197Sroot 				{ char hostname[32];
1986197Sroot 				  gethostname(hostname, sizeof (hostname));
1996197Sroot 				  printf("%s login: ", hostname); }
2006197Sroot 				while ((c = getchar()) != '\n') {
2016197Sroot 					if (c == ' ')
2026197Sroot 						c = '_';
2036197Sroot 					if (c == EOF)
2046197Sroot 						exit(0);
2056197Sroot 					if (namep < utmp.ut_name+NMAX)
2066197Sroot 						*namep++ = c;
2076197Sroot 				}
2082822Swnj 			}
2096197Sroot 		if (rhost == 0) {
2106197Sroot 			setpwent();
2116197Sroot 			if ((pwd = getpwnam(utmp.ut_name)) == NULL)
2126197Sroot 				pwd = &nouser;
2136197Sroot 			endpwent();
2142822Swnj 		}
2152822Swnj 		if (!strcmp(pwd->pw_shell, "/bin/csh")) {
2162822Swnj 			ldisc = NTTYDISC;
2172822Swnj 			ioctl(0, TIOCSETD, &ldisc);
2182822Swnj 		}
2196197Sroot 		if (rhost == 0) {
2206197Sroot 			if (*pwd->pw_passwd != '\0') {
2216197Sroot 				char *pp;
2226197Sroot 				nice(-4);
2236197Sroot 				if (rflag == 0)
2246197Sroot 					pp = getpass("Password:");
2256197Sroot 				else
2266197Sroot 					pp = rpassword;
2276197Sroot 				namep = crypt(pp,pwd->pw_passwd);
2286197Sroot 				nice(4);
2296197Sroot 				if (strcmp(namep, pwd->pw_passwd))
2306197Sroot 					invalid = TRUE;
2316197Sroot 			}
2322822Swnj 		}
2332822Swnj 		if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) {
2342822Swnj 			/* logins are disabled except for root */
2352822Swnj 			while ((c = getc(nlfd)) != EOF)
2362822Swnj 				putchar(c);
2372822Swnj 			fflush(stdout);
2382822Swnj 			sleep(5);
2392822Swnj 			exit(0);
2402822Swnj 		}
2412822Swnj 		if (!invalid && pwd->pw_uid == 0 &&
2422822Swnj 		    !rootterm(ttyn+sizeof("/dev/")-1)) {
2436329Swnj 			logerr("ROOT LOGIN REFUSED %s",
2446329Swnj 			    ttyn+sizeof("/dev/")-1);
2452822Swnj 			invalid = TRUE;
2462822Swnj 		}
2472822Swnj 		if (invalid) {
2481043Sbill 			printf("Login incorrect\n");
2496329Swnj 			if (ttyn[sizeof("/dev/tty")-1] == 'd')
2506329Swnj 				logerr("BADDIALUP %s %s\n",
2516329Swnj 				    ttyn+sizeof("/dev/")-1, utmp.ut_name);
2521043Sbill 		}
2532822Swnj 		if (*pwd->pw_shell == '\0')
2542822Swnj 			pwd->pw_shell = "/bin/sh";
2552822Swnj 		i = strlen(pwd->pw_shell);
2562822Swnj 		if (chdir(pwd->pw_dir) < 0 && !invalid ) {
2572822Swnj 			if (chdir("/") < 0) {
2582822Swnj 				printf("No directory!\n");
2592822Swnj 				invalid = TRUE;
2602822Swnj 			} else {
2612822Swnj 				printf("No directory!  Logging in with home=/\n");
2622822Swnj 				pwd->pw_dir = "/";
2632822Swnj 			}
2641043Sbill 		}
2656005Swnj 		if (rflag && invalid)
2666005Swnj 			exit(1);
2672822Swnj 	} while (invalid);
2681043Sbill 
2696005Swnj 
2701043Sbill 	time(&utmp.ut_time);
2711043Sbill 	t = ttyslot();
2721043Sbill 	if (t>0 && (f = open("/etc/utmp", 1)) >= 0) {
2731043Sbill 		lseek(f, (long)(t*sizeof(utmp)), 0);
2741043Sbill 		SCPYN(utmp.ut_line, rindex(ttyn, '/')+1);
2751043Sbill 		write(f, (char *)&utmp, sizeof(utmp));
2761043Sbill 		close(f);
2771043Sbill 	}
2781043Sbill 	if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) {
2791043Sbill 		lseek(f, 0L, 2);
2801043Sbill 		write(f, (char *)&utmp, sizeof(utmp));
2811043Sbill 		close(f);
2821043Sbill 	}
2836329Swnj 	quietlog = 0;
2842822Swnj 	if (access(qlog, 0) == 0)
2856329Swnj 		quietlog = 1;
2866329Swnj 	if ((f = open(lastlog, 2)) >= 0) {
2872822Swnj 		struct lastlog ll;
2882822Swnj 
2892822Swnj 		lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
2902822Swnj 		if (read(f, (char *) &ll, sizeof ll) == sizeof ll &&
2912822Swnj 		    ll.ll_time != 0) {
2926329Swnj 			if (quietlog == 0)
2932822Swnj 			printf("Last login: %.*s on %.*s\n"
2942822Swnj 			    , 24-5
2952822Swnj 			    , (char *) ctime(&ll.ll_time)
2962822Swnj 			    , sizeof(ll.ll_line)
2972822Swnj 			    , ll.ll_line
2982822Swnj 			);
2992822Swnj 		}
3002822Swnj 		lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
3012822Swnj 		time(&ll.ll_time);
3022822Swnj 		SCPYN(ll.ll_line, rindex(ttyn, '/')+1);
3032822Swnj 		write(f, (char *) &ll, sizeof ll);
3042822Swnj 		close(f);
3052822Swnj 	}
3061043Sbill 	chown(ttyn, pwd->pw_uid, pwd->pw_gid);
3079867Ssam 	chmod(ttyn, 0622);
3081043Sbill 	setgid(pwd->pw_gid);
3096878Smckusick 	strncpy(name, utmp.ut_name, NMAX);
3106878Smckusick 	name[NMAX] = '\0';
3119224Ssam 	initgroups(name, pwd->pw_gid);
3121043Sbill 	setuid(pwd->pw_uid);
3131043Sbill 	environ = envinit;
3141043Sbill 	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
3151043Sbill 	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
3166329Swnj 	if (term[strlen("TERM=")] == 0)
3176005Swnj 		strncat(term, stypeof(ttyn), sizeof(term)-6);
3182822Swnj 	strncat(user, pwd->pw_name, sizeof(user)-6);
3191043Sbill 	if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
3201043Sbill 		namep = pwd->pw_shell;
3211043Sbill 	else
3221043Sbill 		namep++;
3231043Sbill 	strcat(minusnam, namep);
3241043Sbill 	alarm(0);
3256197Sroot 	umask(022);
3266329Swnj 	if (ttyn[sizeof("/dev/tty")-1] == 'd')
3276329Swnj 		logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name);
3286329Swnj 	if (!quietlog) {
3292822Swnj 		showmotd();
3302822Swnj 		strcat(maildir, pwd->pw_name);
3312822Swnj 		if (access(maildir,4)==0) {
3322822Swnj 			struct stat statb;
3332822Swnj 			stat(maildir, &statb);
3342822Swnj 			if (statb.st_size)
3352822Swnj 				printf("You have mail.\n");
3362822Swnj 		}
3372822Swnj 	}
3382822Swnj 
3391043Sbill 	signal(SIGQUIT, SIG_DFL);
3401043Sbill 	signal(SIGINT, SIG_DFL);
3413935Sroot 	signal(SIGTSTP, SIG_IGN);
3421043Sbill 	execlp(pwd->pw_shell, minusnam, 0);
3432822Swnj 	perror(pwd->pw_shell);
3441043Sbill 	printf("No shell\n");
3451043Sbill 	exit(0);
3461043Sbill }
3471043Sbill 
3481043Sbill int	stopmotd;
3491043Sbill catch()
3501043Sbill {
3516466Swnj 
3521043Sbill 	signal(SIGINT, SIG_IGN);
3531043Sbill 	stopmotd++;
3541043Sbill }
3551043Sbill 
3562822Swnj rootterm(tty)
3576466Swnj 	char *tty;
3582822Swnj {
3592822Swnj 	register FILE *fd;
3606466Swnj 	char buf[100];
3612822Swnj 
3626466Swnj 	if (rflag)
3636466Swnj 		return(1);
3642822Swnj 	if ((fd = fopen(securetty, "r")) == NULL)
3652822Swnj 		return(1);
3662822Swnj 	while (fgets(buf, sizeof buf, fd) != NULL) {
3672822Swnj 		buf[strlen(buf)-1] = '\0';
3682822Swnj 		if (strcmp(tty, buf) == 0) {
3692822Swnj 			fclose(fd);
3702822Swnj 			return(1);
3712822Swnj 		}
3722822Swnj 	}
3732822Swnj 	fclose(fd);
3742822Swnj 	return(0);
3752822Swnj }
3762822Swnj 
3771043Sbill showmotd()
3781043Sbill {
3791043Sbill 	FILE *mf;
3801043Sbill 	register c;
3811043Sbill 
3821043Sbill 	signal(SIGINT, catch);
3832822Swnj 	if ((mf = fopen("/etc/motd","r")) != NULL) {
3842822Swnj 		while ((c = getc(mf)) != EOF && stopmotd == 0)
3851043Sbill 			putchar(c);
3861043Sbill 		fclose(mf);
3871043Sbill 	}
3881043Sbill 	signal(SIGINT, SIG_IGN);
3891043Sbill }
3901043Sbill 
3912822Swnj #undef	UNKNOWN
3921043Sbill #define UNKNOWN "su"
3931043Sbill 
3941043Sbill char *
3951043Sbill stypeof(ttyid)
3961043Sbill char	*ttyid;
3971043Sbill {
3981043Sbill 	static char	typebuf[16];
3991043Sbill 	char		buf[50];
4001043Sbill 	register FILE	*f;
4011043Sbill 	register char	*p, *t, *q;
4021043Sbill 
4031043Sbill 	if (ttyid == NULL)
4041043Sbill 		return (UNKNOWN);
4051043Sbill 	f = fopen("/etc/ttytype", "r");
4061043Sbill 	if (f == NULL)
4071043Sbill 		return (UNKNOWN);
4081043Sbill 	/* split off end of name */
4091043Sbill 	for (p = q = ttyid; *p != 0; p++)
4101043Sbill 		if (*p == '/')
4111043Sbill 			q = p + 1;
4121043Sbill 
4131043Sbill 	/* scan the file */
4141043Sbill 	while (fgets(buf, sizeof buf, f) != NULL)
4151043Sbill 	{
4162822Swnj 		for (t=buf; *t!=' ' && *t != '\t'; t++)
4171043Sbill 			;
4181043Sbill 		*t++ = 0;
4192822Swnj 		while (*t == ' ' || *t == '\t')
4202822Swnj 			t++;
4211043Sbill 		for (p=t; *p>' '; p++)
4221043Sbill 			;
4231043Sbill 		*p = 0;
4241043Sbill 		if (strcmp(q,t)==0) {
4251043Sbill 			strcpy(typebuf, buf);
4261043Sbill 			fclose(f);
4271043Sbill 			return (typebuf);
4281043Sbill 		}
4291043Sbill 	}
4301043Sbill 	fclose (f);
4311043Sbill 	return (UNKNOWN);
4321043Sbill }
4336005Swnj 
4346005Swnj getstr(buf, cnt, err)
4356005Swnj 	char *buf;
4366005Swnj 	int cnt;
4376005Swnj 	char *err;
4386005Swnj {
4396005Swnj 	char c;
4406005Swnj 
4416005Swnj 	do {
4426005Swnj 		if (read(0, &c, 1) != 1)
4436005Swnj 			exit(1);
4446005Swnj 		if (--cnt < 0) {
4456005Swnj 			printf("%s too long\r\n", err);
4466005Swnj 			exit(1);
4476005Swnj 		}
4486005Swnj 		*buf++ = c;
4496005Swnj 	} while (c != 0);
4506005Swnj }
4516329Swnj 
4526329Swnj logerr(fmt, a1, a2, a3)
4536329Swnj 	char *fmt, *a1, *a2, *a3;
4546329Swnj {
4556329Swnj 
4566329Swnj }
457