xref: /csrg-svn/usr.bin/login/login.c.1 (revision 6329)
1*6329Swnjstatic	char *sccsid = "@(#)login.c.1	4.15 82/03/27";
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
242822Swnjchar	nolog[] =	"/etc/nologin";
252822Swnjchar	qlog[]  =	".hushlogin";
262822Swnjchar	securetty[] =	"/etc/securetty";
271043Sbillchar	maildir[30] =	"/usr/spool/mail/";
281043Sbillchar	lastlog[] =	"/usr/adm/lastlog";
291043Sbillstruct	passwd nouser = {"", "nope"};
301043Sbillstruct	sgttyb ttyb;
311043Sbillstruct	utmp utmp;
321043Sbillchar	minusnam[16] = "-";
336005Swnj
341043Sbillchar	homedir[64] = "HOME=";
351043Sbillchar	shell[64] = "SHELL=";
361043Sbillchar	term[64] = "TERM=";
372822Swnjchar	user[20] = "USER=";
386005Swnjchar	*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
436005Swnjchar	*envinit[] =
446005Swnj    {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", term, user, 0};
456005Swnj
461043Sbillstruct	passwd *pwd;
471043Sbillstruct	passwd *getpwnam();
486005Swnjchar	*strcat(), *rindex(), *index();
491043Sbillint	setpwent();
501043Sbillchar	*ttyname();
511043Sbillchar	*crypt();
521043Sbillchar	*getpass();
531043Sbillchar	*rindex();
541043Sbillchar	*stypeof();
551043Sbillextern	char **environ;
561043Sbill
571365Sbill#define	CTRL(c)	('c'&037)
581365Sbill#define	CERASE	'#'
591365Sbill#define	CEOT	CTRL(d)
601365Sbill#define	CKILL	'@'
611365Sbill#define	CQUIT	034		/* FS, cntl shift L */
621365Sbill#define	CINTR	0177		/* DEL */
631365Sbill#define	CSTOP	CTRL(s)
641365Sbill#define	CSTART	CTRL(q)
651365Sbill#define	CBRK	0377
661365Sbillstruct	tchars tc = {
671365Sbill	CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
681365Sbill};
691365Sbillstruct	ltchars ltc = {
701546Sbill	CTRL(z), CTRL(y), CTRL(r), CTRL(o), CTRL(w), CTRL(v)
711365Sbill};
721365Sbill
736005Swnjint	rflag;
746197Srootchar	rusername[NMAX+1], lusername[NMAX+1];
756005Swnjchar	rpassword[NMAX+1];
766197Srootchar	*rhost;
776005Swnj
781043Sbillmain(argc, argv)
791043Sbillchar **argv;
801043Sbill{
811043Sbill	register char *namep;
821043Sbill	int t, f, c;
832822Swnj	int invalid;
842822Swnj	int quietlog;
852822Swnj	int i;
862822Swnj	FILE *nlfd;
871043Sbill	char *ttyn;
886197Sroot	int ldisc = 0, zero = 0;
896197Sroot	FILE *hostf; int first = 1;
901043Sbill
911043Sbill	alarm(60);
921043Sbill	signal(SIGQUIT, SIG_IGN);
931043Sbill	signal(SIGINT, SIG_IGN);
941043Sbill	nice(-100);
951043Sbill	nice(20);
961043Sbill	nice(0);
97*6329Swnj	if (argc > 1 && !strcmp(argv[1], "-r")) {
986005Swnj		rflag++;
99*6329Swnj		rhost = argv[2];
1006197Sroot		argc = 1;
101*6329Swnj		getstr(rusername, sizeof (rusername), "remuser");
102*6329Swnj		getstr(lusername, sizeof (lusername), "locuser");
1036005Swnj		getstr(term+5, sizeof(term)-5, "Terminal type");
104*6329Swnj		if (getuid())
105*6329Swnj			goto abnormal;
1066197Sroot		setpwent();
1076197Sroot		pwd = getpwnam(lusername);
108*6329Swnj		endpwent();
1096197Sroot		if (pwd == NULL) {
110*6329Swnj			if (strcmp(rusername, lusername))
111*6329Swnj				printf("%s: No such user\r\n", lusername);
112*6329Swnj			goto abnormal;
1136197Sroot		}
1146197Sroot		hostf = fopen("/etc/hosts.equiv", "r");
1156197Sroot	again:
1166197Sroot		if (hostf) {
1176197Sroot		  char ahost[32];
1186197Sroot		  while (fgets(ahost, sizeof (ahost), hostf)) {
1196197Sroot			char *user;
1206197Sroot			if (index(ahost, '\n'))
1216197Sroot				*index(ahost, '\n') = 0;
1226197Sroot			user = index(ahost, ' ');
1236197Sroot			if (user)
1246197Sroot				*user++ = 0;
1256197Sroot			if (!strcmp(rhost, ahost) &&
126*6329Swnj			    !strcmp(rusername, user ? user : lusername)) {
127*6329Swnj				fclose(hostf);
1286197Sroot				goto normal;
129*6329Swnj			}
1306197Sroot		  }
1316197Sroot		  fclose(hostf);
1326197Sroot		}
1336197Sroot		if (first == 1) {
1346197Sroot			first = 0;
1356197Sroot			if (chdir(pwd->pw_dir) < 0)
1366197Sroot				goto again;
1376197Sroot			hostf = fopen(".rhosts", "r");
1386197Sroot			goto again;
1396197Sroot		}
140*6329Swnjabnormal:
1416197Sroot		rhost = 0;
1426197Sroot		rflag = -1;
1436005Swnj	}
1446197Srootnormal:
1456197Sroot	ioctl(0, TIOCLSET, &zero);
1461547Sbill	ioctl(0, TIOCNXCL, 0);
147*6329Swnj	ioctl(0, FIONBIO, &zero);
148*6329Swnj	ioctl(0, FIOASYNC, &zero);
1491043Sbill	gtty(0, &ttyb);
1506005Swnj	if (rflag) {
1516005Swnj		char *cp = index(term, '/');
1526005Swnj		if (cp) {
1536005Swnj			int i;
1546005Swnj			*cp++ = 0;
1556005Swnj			for (i = 0; i < NSPEEDS; i++)
1566005Swnj				if (!strcmp(speeds[i], cp)) {
1576005Swnj					ttyb.sg_ispeed = ttyb.sg_ospeed = i;
1586005Swnj					break;
1596005Swnj				}
1606005Swnj		}
1616005Swnj		ttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS;
1626005Swnj	}
1632822Swnj	ttyb.sg_erase = CERASE;
1642822Swnj	ttyb.sg_kill = CKILL;
1651043Sbill	stty(0, &ttyb);
1661365Sbill	ioctl(0, TIOCSETC, &tc);
1671365Sbill	ioctl(0, TIOCSLTC, &ltc);
1681043Sbill	for (t=3; t<20; t++)
1691043Sbill		close(t);
1701043Sbill	ttyn = ttyname(0);
1712822Swnj	if (ttyn==(char *)0)
1721043Sbill		ttyn = "/dev/tty??";
1732822Swnj	do {
1742822Swnj		ldisc = 0;
1752822Swnj		ioctl(0, TIOCSETD, &ldisc);
1762822Swnj		invalid = FALSE;
1772822Swnj		SCPYN(utmp.ut_name, "");
1782822Swnj		if (argc>1) {
1792822Swnj			SCPYN(utmp.ut_name, argv[1]);
1802822Swnj			argc = 0;
1811043Sbill		}
182*6329Swnj		if (rflag) {
1836197Sroot			strcpy(utmp.ut_name, lusername);
184*6329Swnj			if (rflag == -1)
185*6329Swnj				rflag = 0;
186*6329Swnj		} else
1876197Sroot			while (utmp.ut_name[0] == '\0') {
1886197Sroot				namep = utmp.ut_name;
1896197Sroot				{ char hostname[32];
1906197Sroot				  gethostname(hostname, sizeof (hostname));
1916197Sroot				  printf("%s login: ", hostname); }
1926197Sroot				while ((c = getchar()) != '\n') {
1936197Sroot					if (c == ' ')
1946197Sroot						c = '_';
1956197Sroot					if (c == EOF)
1966197Sroot						exit(0);
1976197Sroot					if (namep < utmp.ut_name+NMAX)
1986197Sroot						*namep++ = c;
1996197Sroot				}
2002822Swnj			}
2016197Sroot		if (rhost == 0) {
2026197Sroot			setpwent();
2036197Sroot			if ((pwd = getpwnam(utmp.ut_name)) == NULL)
2046197Sroot				pwd = &nouser;
2056197Sroot			endpwent();
2062822Swnj		}
2072822Swnj		if (!strcmp(pwd->pw_shell, "/bin/csh")) {
2082822Swnj			ldisc = NTTYDISC;
2092822Swnj			ioctl(0, TIOCSETD, &ldisc);
2102822Swnj		}
2116197Sroot		if (rhost == 0) {
2126197Sroot			if (*pwd->pw_passwd != '\0') {
2136197Sroot				char *pp;
2146197Sroot				nice(-4);
2156197Sroot				if (rflag == 0)
2166197Sroot					pp = getpass("Password:");
2176197Sroot				else
2186197Sroot					pp = rpassword;
2196197Sroot				namep = crypt(pp,pwd->pw_passwd);
2206197Sroot				nice(4);
2216197Sroot				if (strcmp(namep, pwd->pw_passwd))
2226197Sroot					invalid = TRUE;
2236197Sroot			}
2242822Swnj		}
2252822Swnj		if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) {
2262822Swnj			/* logins are disabled except for root */
2272822Swnj			while ((c = getc(nlfd)) != EOF)
2282822Swnj				putchar(c);
2292822Swnj			fflush(stdout);
2302822Swnj			sleep(5);
2312822Swnj			exit(0);
2322822Swnj		}
2332822Swnj		if (!invalid && pwd->pw_uid == 0 &&
2342822Swnj		    !rootterm(ttyn+sizeof("/dev/")-1)) {
235*6329Swnj			logerr("ROOT LOGIN REFUSED %s",
236*6329Swnj			    ttyn+sizeof("/dev/")-1);
2372822Swnj			invalid = TRUE;
2382822Swnj		}
2392822Swnj		if (invalid) {
2401043Sbill			printf("Login incorrect\n");
241*6329Swnj			if (ttyn[sizeof("/dev/tty")-1] == 'd')
242*6329Swnj				logerr("BADDIALUP %s %s\n",
243*6329Swnj				    ttyn+sizeof("/dev/")-1, utmp.ut_name);
2441043Sbill		}
2452822Swnj		if (*pwd->pw_shell == '\0')
2462822Swnj			pwd->pw_shell = "/bin/sh";
2472822Swnj		i = strlen(pwd->pw_shell);
2482822Swnj		if (chdir(pwd->pw_dir) < 0 && !invalid ) {
2492822Swnj			if (chdir("/") < 0) {
2502822Swnj				printf("No directory!\n");
2512822Swnj				invalid = TRUE;
2522822Swnj			} else {
2532822Swnj				printf("No directory!  Logging in with home=/\n");
2542822Swnj				pwd->pw_dir = "/";
2552822Swnj			}
2561043Sbill		}
2576005Swnj		if (rflag && invalid)
2586005Swnj			exit(1);
2592822Swnj	} while (invalid);
2601043Sbill
2616005Swnj
2621043Sbill	time(&utmp.ut_time);
2631043Sbill	t = ttyslot();
2641043Sbill	if (t>0 && (f = open("/etc/utmp", 1)) >= 0) {
2651043Sbill		lseek(f, (long)(t*sizeof(utmp)), 0);
2661043Sbill		SCPYN(utmp.ut_line, rindex(ttyn, '/')+1);
2671043Sbill		write(f, (char *)&utmp, sizeof(utmp));
2681043Sbill		close(f);
2691043Sbill	}
2701043Sbill	if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) {
2711043Sbill		lseek(f, 0L, 2);
2721043Sbill		write(f, (char *)&utmp, sizeof(utmp));
2731043Sbill		close(f);
2741043Sbill	}
275*6329Swnj	quietlog = 0;
2762822Swnj	if (access(qlog, 0) == 0)
277*6329Swnj		quietlog = 1;
278*6329Swnj	if ((f = open(lastlog, 2)) >= 0) {
2792822Swnj		struct lastlog ll;
2802822Swnj
2812822Swnj		lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
2822822Swnj		if (read(f, (char *) &ll, sizeof ll) == sizeof ll &&
2832822Swnj		    ll.ll_time != 0) {
284*6329Swnj			if (quietlog == 0)
2852822Swnj			printf("Last login: %.*s on %.*s\n"
2862822Swnj			    , 24-5
2872822Swnj			    , (char *) ctime(&ll.ll_time)
2882822Swnj			    , sizeof(ll.ll_line)
2892822Swnj			    , ll.ll_line
2902822Swnj			);
2912822Swnj		}
2922822Swnj		lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
2932822Swnj		time(&ll.ll_time);
2942822Swnj		SCPYN(ll.ll_line, rindex(ttyn, '/')+1);
2952822Swnj		write(f, (char *) &ll, sizeof ll);
2962822Swnj		close(f);
2972822Swnj	}
2981043Sbill	chown(ttyn, pwd->pw_uid, pwd->pw_gid);
2991043Sbill	setgid(pwd->pw_gid);
3006014Swnj	inigrp(utmp.ut_name, pwd->pw_gid);
3011043Sbill	setuid(pwd->pw_uid);
3021043Sbill	environ = envinit;
3031043Sbill	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
3041043Sbill	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
305*6329Swnj	if (term[strlen("TERM=")] == 0)
3066005Swnj		strncat(term, stypeof(ttyn), sizeof(term)-6);
3072822Swnj	strncat(user, pwd->pw_name, sizeof(user)-6);
3081043Sbill	if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
3091043Sbill		namep = pwd->pw_shell;
3101043Sbill	else
3111043Sbill		namep++;
3121043Sbill	strcat(minusnam, namep);
3131043Sbill	alarm(0);
3146197Sroot	umask(022);
315*6329Swnj	if (ttyn[sizeof("/dev/tty")-1] == 'd')
316*6329Swnj		logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name);
317*6329Swnj	if (!quietlog) {
3182822Swnj		showmotd();
3192822Swnj		strcat(maildir, pwd->pw_name);
3202822Swnj		if (access(maildir,4)==0) {
3212822Swnj			struct stat statb;
3222822Swnj			stat(maildir, &statb);
3232822Swnj			if (statb.st_size)
3242822Swnj				printf("You have mail.\n");
3252822Swnj		}
3262822Swnj	}
3272822Swnj
3281043Sbill	signal(SIGQUIT, SIG_DFL);
3291043Sbill	signal(SIGINT, SIG_DFL);
3303935Sroot	signal(SIGTSTP, SIG_IGN);
3311043Sbill	execlp(pwd->pw_shell, minusnam, 0);
3322822Swnj	perror(pwd->pw_shell);
3331043Sbill	printf("No shell\n");
3341043Sbill	exit(0);
3351043Sbill}
3361043Sbill
3371043Sbillint	stopmotd;
3381043Sbillcatch()
3391043Sbill{
3401043Sbill	signal(SIGINT, SIG_IGN);
3411043Sbill	stopmotd++;
3421043Sbill}
3431043Sbill
3442822Swnj/*
3452822Swnj * return true if OK for root to login on this terminal
3462822Swnj */
3472822Swnjrootterm(tty)
3482822Swnj	char	*tty;
3492822Swnj{
3502822Swnj	register FILE *fd;
3512822Swnj	char	buf[100];
3522822Swnj
3532822Swnj	if ((fd = fopen(securetty, "r")) == NULL)
3542822Swnj		return(1);
3552822Swnj	while (fgets(buf, sizeof buf, fd) != NULL) {
3562822Swnj		buf[strlen(buf)-1] = '\0';
3572822Swnj		if (strcmp(tty, buf) == 0) {
3582822Swnj			fclose(fd);
3592822Swnj			return(1);
3602822Swnj		}
3612822Swnj	}
3622822Swnj	fclose(fd);
3632822Swnj	return(0);
3642822Swnj}
3652822Swnj
3661043Sbillshowmotd()
3671043Sbill{
3681043Sbill	FILE *mf;
3691043Sbill	register c;
3701043Sbill
3711043Sbill	signal(SIGINT, catch);
3722822Swnj	if ((mf = fopen("/etc/motd","r")) != NULL) {
3732822Swnj		while ((c = getc(mf)) != EOF && stopmotd == 0)
3741043Sbill			putchar(c);
3751043Sbill		fclose(mf);
3761043Sbill	}
3771043Sbill	signal(SIGINT, SIG_IGN);
3781043Sbill}
3791043Sbill
3802822Swnj#undef	UNKNOWN
3811043Sbill#define UNKNOWN "su"
3821043Sbill
3831043Sbillchar *
3841043Sbillstypeof(ttyid)
3851043Sbillchar	*ttyid;
3861043Sbill{
3871043Sbill	static char	typebuf[16];
3881043Sbill	char		buf[50];
3891043Sbill	register FILE	*f;
3901043Sbill	register char	*p, *t, *q;
3911043Sbill
3921043Sbill	if (ttyid == NULL)
3931043Sbill		return (UNKNOWN);
3941043Sbill	f = fopen("/etc/ttytype", "r");
3951043Sbill	if (f == NULL)
3961043Sbill		return (UNKNOWN);
3971043Sbill	/* split off end of name */
3981043Sbill	for (p = q = ttyid; *p != 0; p++)
3991043Sbill		if (*p == '/')
4001043Sbill			q = p + 1;
4011043Sbill
4021043Sbill	/* scan the file */
4031043Sbill	while (fgets(buf, sizeof buf, f) != NULL)
4041043Sbill	{
4052822Swnj		for (t=buf; *t!=' ' && *t != '\t'; t++)
4061043Sbill			;
4071043Sbill		*t++ = 0;
4082822Swnj		while (*t == ' ' || *t == '\t')
4092822Swnj			t++;
4101043Sbill		for (p=t; *p>' '; p++)
4111043Sbill			;
4121043Sbill		*p = 0;
4131043Sbill		if (strcmp(q,t)==0) {
4141043Sbill			strcpy(typebuf, buf);
4151043Sbill			fclose(f);
4161043Sbill			return (typebuf);
4171043Sbill		}
4181043Sbill	}
4191043Sbill	fclose (f);
4201043Sbill	return (UNKNOWN);
4211043Sbill}
4226005Swnj
4236005Swnjgetstr(buf, cnt, err)
4246005Swnj	char *buf;
4256005Swnj	int cnt;
4266005Swnj	char *err;
4276005Swnj{
4286005Swnj	char c;
4296005Swnj
4306005Swnj	do {
4316005Swnj		if (read(0, &c, 1) != 1)
4326005Swnj			exit(1);
4336005Swnj		if (--cnt < 0) {
4346005Swnj			printf("%s too long\r\n", err);
4356005Swnj			exit(1);
4366005Swnj		}
4376005Swnj		*buf++ = c;
4386005Swnj	} while (c != 0);
4396005Swnj}
440*6329Swnj
441*6329Swnjlogerr(fmt, a1, a2, a3)
442*6329Swnj	char *fmt, *a1, *a2, *a3;
443*6329Swnj{
444*6329Swnj
445*6329Swnj}
446