xref: /csrg-svn/usr.bin/login/login.c.1 (revision 6197)
1*6197Srootstatic	char *sccsid = "@(#)login.c.1	4.14 82/03/15";
21043Sbill/*
31043Sbill * login [ name ]
46005Swnj * login -r
5*6197Sroot * login -r [ rhost ]
61043Sbill */
71043Sbill
81043Sbill#include <sys/types.h>
91043Sbill#include <sgtty.h>
101043Sbill#include <utmp.h>
111043Sbill#include <signal.h>
121043Sbill#include <pwd.h>
131043Sbill#include <stdio.h>
141043Sbill#include <sys/stat.h>
151043Sbill#include <lastlog.h>
161043Sbill
172822Swnj#define	SCPYN(a, b)	strncpy(a, b, sizeof(a))
182822Swnj
19*6197Sroot#define NMAX	sizeof(utmp.ut_name)
20*6197Sroot#define LMAX	sizeof(utmp.ut_line)
211043Sbill
222822Swnj#define	FALSE	0
232822Swnj#define	TRUE	-1
242822Swnj
252822Swnjchar	nolog[] =	"/etc/nologin";
262822Swnjchar	qlog[]  =	".hushlogin";
272822Swnjchar	securetty[] =	"/etc/securetty";
281043Sbillchar	maildir[30] =	"/usr/spool/mail/";
291043Sbillchar	lastlog[] =	"/usr/adm/lastlog";
301043Sbillstruct	passwd nouser = {"", "nope"};
311043Sbillstruct	sgttyb ttyb;
321043Sbillstruct	utmp utmp;
331043Sbillchar	minusnam[16] = "-";
346005Swnj
351043Sbillchar	homedir[64] = "HOME=";
361043Sbillchar	shell[64] = "SHELL=";
371043Sbillchar	term[64] = "TERM=";
382822Swnjchar	user[20] = "USER=";
396005Swnjchar	*speeds[] =
406005Swnj    { "0", "50", "75", "110", "134", "150", "200", "300",
416005Swnj      "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" };
426005Swnj#define	NSPEEDS	(sizeof (speeds) / sizeof (speeds[0]))
436005Swnj
446005Swnjchar	*envinit[] =
456005Swnj    {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", term, user, 0};
466005Swnj
471043Sbillstruct	passwd *pwd;
481043Sbillstruct	passwd *getpwnam();
496005Swnjchar	*strcat(), *rindex(), *index();
501043Sbillint	setpwent();
511043Sbillchar	*ttyname();
521043Sbillchar	*crypt();
531043Sbillchar	*getpass();
541043Sbillchar	*rindex();
551043Sbillchar	*stypeof();
561043Sbillextern	char **environ;
571043Sbill
581365Sbill#define	CTRL(c)	('c'&037)
591365Sbill#define	CERASE	'#'
601365Sbill#define	CEOT	CTRL(d)
611365Sbill#define	CKILL	'@'
621365Sbill#define	CQUIT	034		/* FS, cntl shift L */
631365Sbill#define	CINTR	0177		/* DEL */
641365Sbill#define	CSTOP	CTRL(s)
651365Sbill#define	CSTART	CTRL(q)
661365Sbill#define	CBRK	0377
671365Sbillstruct	tchars tc = {
681365Sbill	CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
691365Sbill};
701365Sbillstruct	ltchars ltc = {
711546Sbill	CTRL(z), CTRL(y), CTRL(r), CTRL(o), CTRL(w), CTRL(v)
721365Sbill};
731365Sbill
746005Swnjint	rflag;
75*6197Srootchar	rusername[NMAX+1], lusername[NMAX+1];
766005Swnjchar	rpassword[NMAX+1];
77*6197Srootchar	*rhost;
786005Swnj
791043Sbillmain(argc, argv)
801043Sbillchar **argv;
811043Sbill{
821043Sbill	register char *namep;
831043Sbill	int t, f, c;
842822Swnj	int invalid;
852822Swnj	int quietlog;
862822Swnj	int i;
872822Swnj	FILE *nlfd;
881043Sbill	char *ttyn;
89*6197Sroot	int ldisc = 0, zero = 0;
90*6197Sroot	FILE *hostf; int first = 1;
911043Sbill
921043Sbill	alarm(60);
931043Sbill	signal(SIGQUIT, SIG_IGN);
941043Sbill	signal(SIGINT, SIG_IGN);
951043Sbill	nice(-100);
961043Sbill	nice(20);
971043Sbill	nice(0);
986005Swnj	if (argc > 0 && !strcmp(argv[1], "-r")) {
996005Swnj		rflag++;
100*6197Sroot		if (argc > 1)
101*6197Sroot			rhost = argv[2];
102*6197Sroot		argc = 1;
103*6197Sroot		if (rhost) {
104*6197Sroot			getstr(rusername, sizeof (rusername), "remuser");
105*6197Sroot			getstr(lusername, sizeof (lusername), "locuser");
106*6197Sroot		} else {
107*6197Sroot			getstr(lusername, sizeof (lusername), "Username");
108*6197Sroot			getstr(rpassword, sizeof (rpassword), "Password");
109*6197Sroot		}
1106005Swnj		getstr(term+5, sizeof(term)-5, "Terminal type");
111*6197Sroot		if (rhost == 0)
112*6197Sroot			goto normal;
113*6197Sroot		if (getuid()) {
114*6197Sroot			rflag = 0;
115*6197Sroot			goto normal;
116*6197Sroot		}
117*6197Sroot		setpwent();
118*6197Sroot		pwd = getpwnam(lusername);
119*6197Sroot		if (pwd == NULL) {
120*6197Sroot			fprintf(stderr, "Login incorrect.\n");
121*6197Sroot			exit(1);
122*6197Sroot		}
123*6197Sroot		endpwent();
124*6197Sroot		hostf = fopen("/etc/hosts.equiv", "r");
125*6197Sroot	again:
126*6197Sroot		if (hostf) {
127*6197Sroot		  char ahost[32];
128*6197Sroot		  while (fgets(ahost, sizeof (ahost), hostf)) {
129*6197Sroot			char *user;
130*6197Sroot			if (index(ahost, '\n'))
131*6197Sroot				*index(ahost, '\n') = 0;
132*6197Sroot			user = index(ahost, ' ');
133*6197Sroot			if (user)
134*6197Sroot				*user++ = 0;
135*6197Sroot			if (!strcmp(rhost, ahost) &&
136*6197Sroot			    !strcmp(rusername, user ? user : lusername))
137*6197Sroot				goto normal;
138*6197Sroot		  }
139*6197Sroot		  fclose(hostf);
140*6197Sroot		}
141*6197Sroot		if (first == 1) {
142*6197Sroot			first = 0;
143*6197Sroot			if (chdir(pwd->pw_dir) < 0)
144*6197Sroot				goto again;
145*6197Sroot			hostf = fopen(".rhosts", "r");
146*6197Sroot			goto again;
147*6197Sroot		}
148*6197Sroot		rhost = 0;
149*6197Sroot		rflag = -1;
1506005Swnj	}
151*6197Srootnormal:
152*6197Sroot	ioctl(0, TIOCLSET, &zero);
1531547Sbill	ioctl(0, TIOCNXCL, 0);
1541043Sbill	gtty(0, &ttyb);
1556005Swnj	if (rflag) {
1566005Swnj		char *cp = index(term, '/');
1576005Swnj		if (cp) {
1586005Swnj			int i;
1596005Swnj			*cp++ = 0;
1606005Swnj			for (i = 0; i < NSPEEDS; i++)
1616005Swnj				if (!strcmp(speeds[i], cp)) {
1626005Swnj					ttyb.sg_ispeed = ttyb.sg_ospeed = i;
1636005Swnj					break;
1646005Swnj				}
1656005Swnj		}
1666005Swnj		ttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS;
1676005Swnj	}
168*6197Sroot	if (rflag == -1)
169*6197Sroot		rflag = 0;
1702822Swnj	ttyb.sg_erase = CERASE;
1712822Swnj	ttyb.sg_kill = CKILL;
1721043Sbill	stty(0, &ttyb);
1731365Sbill	ioctl(0, TIOCSETC, &tc);
1741365Sbill	ioctl(0, TIOCSLTC, &ltc);
1751043Sbill	for (t=3; t<20; t++)
1761043Sbill		close(t);
1771043Sbill	ttyn = ttyname(0);
1782822Swnj	if (ttyn==(char *)0)
1791043Sbill		ttyn = "/dev/tty??";
1802822Swnj	do {
1812822Swnj		ldisc = 0;
1822822Swnj		ioctl(0, TIOCSETD, &ldisc);
1832822Swnj		invalid = FALSE;
1842822Swnj		SCPYN(utmp.ut_name, "");
1852822Swnj		if (argc>1) {
1862822Swnj			SCPYN(utmp.ut_name, argv[1]);
1872822Swnj			argc = 0;
1881043Sbill		}
1896005Swnj		if (rflag)
190*6197Sroot			strcpy(utmp.ut_name, lusername);
1916005Swnj		else
192*6197Sroot			while (utmp.ut_name[0] == '\0') {
193*6197Sroot				namep = utmp.ut_name;
194*6197Sroot				{ char hostname[32];
195*6197Sroot				  gethostname(hostname, sizeof (hostname));
196*6197Sroot				  printf("%s login: ", hostname); }
197*6197Sroot				while ((c = getchar()) != '\n') {
198*6197Sroot					if (c == ' ')
199*6197Sroot						c = '_';
200*6197Sroot					if (c == EOF)
201*6197Sroot						exit(0);
202*6197Sroot					if (namep < utmp.ut_name+NMAX)
203*6197Sroot						*namep++ = c;
204*6197Sroot				}
2052822Swnj			}
206*6197Sroot		if (rhost == 0) {
207*6197Sroot			setpwent();
208*6197Sroot			if ((pwd = getpwnam(utmp.ut_name)) == NULL)
209*6197Sroot				pwd = &nouser;
210*6197Sroot			endpwent();
2112822Swnj		}
2122822Swnj		if (!strcmp(pwd->pw_shell, "/bin/csh")) {
2132822Swnj			ldisc = NTTYDISC;
2142822Swnj			ioctl(0, TIOCSETD, &ldisc);
2152822Swnj		}
216*6197Sroot		if (rhost == 0) {
217*6197Sroot			if (*pwd->pw_passwd != '\0') {
218*6197Sroot				char *pp;
219*6197Sroot				nice(-4);
220*6197Sroot				if (rflag == 0)
221*6197Sroot					pp = getpass("Password:");
222*6197Sroot				else
223*6197Sroot					pp = rpassword;
224*6197Sroot				namep = crypt(pp,pwd->pw_passwd);
225*6197Sroot				nice(4);
226*6197Sroot				if (strcmp(namep, pwd->pw_passwd))
227*6197Sroot					invalid = TRUE;
228*6197Sroot			}
2292822Swnj		}
2302822Swnj		if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) {
2312822Swnj			/* logins are disabled except for root */
2322822Swnj			while ((c = getc(nlfd)) != EOF)
2332822Swnj				putchar(c);
2342822Swnj			fflush(stdout);
2352822Swnj			sleep(5);
2362822Swnj			exit(0);
2372822Swnj		}
2382822Swnj		if (!invalid && pwd->pw_uid == 0 &&
2392822Swnj		    !rootterm(ttyn+sizeof("/dev/")-1)) {
2402822Swnj			FILE *console = fopen("/dev/console", "w");
2412822Swnj			if (console != NULL) {
2422822Swnj				fprintf(console, "\r\nROOT LOGIN REFUSED %s\r\n"
2432822Swnj				    , ttyn+sizeof("/dev/")-1
2442822Swnj				);
2452822Swnj				fclose(console);
2462822Swnj			}
2472822Swnj			invalid = TRUE;
2482822Swnj		}
2492822Swnj		if (invalid) {
2501043Sbill			printf("Login incorrect\n");
2512822Swnj			if (ttyn[sizeof("/dev/tty")-1] == 'd') {
2521043Sbill				FILE *console = fopen("/dev/console", "w");
2531043Sbill				if (console != NULL) {
2542822Swnj					fprintf(console, "\r\nBADDIALUP %s %s\r\n"
2552822Swnj					    , ttyn+sizeof("/dev/")-1
2562822Swnj					    , utmp.ut_name);
2571043Sbill					fclose(console);
2581043Sbill				}
2591043Sbill			}
2601043Sbill		}
2612822Swnj		if (*pwd->pw_shell == '\0')
2622822Swnj			pwd->pw_shell = "/bin/sh";
2632822Swnj		i = strlen(pwd->pw_shell);
2642822Swnj		if (chdir(pwd->pw_dir) < 0 && !invalid ) {
2652822Swnj			if (chdir("/") < 0) {
2662822Swnj				printf("No directory!\n");
2672822Swnj				invalid = TRUE;
2682822Swnj			} else {
2692822Swnj				printf("No directory!  Logging in with home=/\n");
2702822Swnj				pwd->pw_dir = "/";
2712822Swnj			}
2721043Sbill		}
2736005Swnj		if (rflag && invalid)
2746005Swnj			exit(1);
2752822Swnj	} while (invalid);
2761043Sbill
2776005Swnj
2781043Sbill	time(&utmp.ut_time);
2791043Sbill	t = ttyslot();
2801043Sbill	if (t>0 && (f = open("/etc/utmp", 1)) >= 0) {
2811043Sbill		lseek(f, (long)(t*sizeof(utmp)), 0);
2821043Sbill		SCPYN(utmp.ut_line, rindex(ttyn, '/')+1);
2831043Sbill		write(f, (char *)&utmp, sizeof(utmp));
2841043Sbill		close(f);
2851043Sbill	}
2861043Sbill	if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) {
2871043Sbill		lseek(f, 0L, 2);
2881043Sbill		write(f, (char *)&utmp, sizeof(utmp));
2891043Sbill		close(f);
2901043Sbill	}
2912822Swnj	quietlog = FALSE;
2922822Swnj	if (access(qlog, 0) == 0)
2932822Swnj		quietlog = TRUE;
2942822Swnj	if ( !quietlog && (f = open(lastlog, 2)) >= 0 ) {
2952822Swnj		struct lastlog ll;
2962822Swnj
2972822Swnj		lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
2982822Swnj		if (read(f, (char *) &ll, sizeof ll) == sizeof ll &&
2992822Swnj		    ll.ll_time != 0) {
3002822Swnj			printf("Last login: %.*s on %.*s\n"
3012822Swnj			    , 24-5
3022822Swnj			    , (char *) ctime(&ll.ll_time)
3032822Swnj			    , sizeof(ll.ll_line)
3042822Swnj			    , ll.ll_line
3052822Swnj			);
3062822Swnj		}
3072822Swnj		lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
3082822Swnj		time(&ll.ll_time);
3092822Swnj		SCPYN(ll.ll_line, rindex(ttyn, '/')+1);
3102822Swnj		write(f, (char *) &ll, sizeof ll);
3112822Swnj		close(f);
3122822Swnj	}
3131043Sbill	chown(ttyn, pwd->pw_uid, pwd->pw_gid);
3141043Sbill	setgid(pwd->pw_gid);
3156014Swnj	inigrp(utmp.ut_name, pwd->pw_gid);
3161043Sbill	setuid(pwd->pw_uid);
3171043Sbill	environ = envinit;
3181043Sbill	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
3191043Sbill	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
3206005Swnj	if (rflag == 0)
3216005Swnj		strncat(term, stypeof(ttyn), sizeof(term)-6);
3222822Swnj	strncat(user, pwd->pw_name, sizeof(user)-6);
3231043Sbill	if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
3241043Sbill		namep = pwd->pw_shell;
3251043Sbill	else
3261043Sbill		namep++;
3271043Sbill	strcat(minusnam, namep);
3281043Sbill	alarm(0);
329*6197Sroot	umask(022);
3302822Swnj	if (ttyn[sizeof("/dev/tty")-1] == 'd') {
3312822Swnj		FILE *console = fopen("/dev/console", "w");
3322822Swnj		if (console != NULL) {
3332822Swnj			fprintf(console, "\r\nDIALUP %s %s\r\n"
3342822Swnj			    , ttyn+sizeof("/dev/")-1
3352822Swnj			    , pwd->pw_name
3362822Swnj			);
3372822Swnj			fclose(console);
3382822Swnj		}
3391043Sbill	}
3402822Swnj	if ( !quietlog ) {
3412822Swnj		showmotd();
3422822Swnj		strcat(maildir, pwd->pw_name);
3432822Swnj		if (access(maildir,4)==0) {
3442822Swnj			struct stat statb;
3452822Swnj			stat(maildir, &statb);
3462822Swnj			if (statb.st_size)
3472822Swnj				printf("You have mail.\n");
3482822Swnj		}
3492822Swnj	}
3502822Swnj
3511043Sbill	signal(SIGQUIT, SIG_DFL);
3521043Sbill	signal(SIGINT, SIG_DFL);
3533935Sroot	signal(SIGTSTP, SIG_IGN);
3541043Sbill	execlp(pwd->pw_shell, minusnam, 0);
3552822Swnj	perror(pwd->pw_shell);
3561043Sbill	printf("No shell\n");
3571043Sbill	exit(0);
3581043Sbill}
3591043Sbill
3601043Sbillint	stopmotd;
3611043Sbillcatch()
3621043Sbill{
3631043Sbill	signal(SIGINT, SIG_IGN);
3641043Sbill	stopmotd++;
3651043Sbill}
3661043Sbill
3672822Swnj/*
3682822Swnj * return true if OK for root to login on this terminal
3692822Swnj */
3702822Swnjrootterm(tty)
3712822Swnj	char	*tty;
3722822Swnj{
3732822Swnj	register FILE *fd;
3742822Swnj	char	buf[100];
3752822Swnj
3762822Swnj	if ((fd = fopen(securetty, "r")) == NULL)
3772822Swnj		return(1);
3782822Swnj	while (fgets(buf, sizeof buf, fd) != NULL) {
3792822Swnj		buf[strlen(buf)-1] = '\0';
3802822Swnj		if (strcmp(tty, buf) == 0) {
3812822Swnj			fclose(fd);
3822822Swnj			return(1);
3832822Swnj		}
3842822Swnj	}
3852822Swnj	fclose(fd);
3862822Swnj	return(0);
3872822Swnj}
3882822Swnj
3891043Sbillshowmotd()
3901043Sbill{
3911043Sbill	FILE *mf;
3921043Sbill	register c;
3931043Sbill
3941043Sbill	signal(SIGINT, catch);
3952822Swnj	if ((mf = fopen("/etc/motd","r")) != NULL) {
3962822Swnj		while ((c = getc(mf)) != EOF && stopmotd == 0)
3971043Sbill			putchar(c);
3981043Sbill		fclose(mf);
3991043Sbill	}
4001043Sbill	signal(SIGINT, SIG_IGN);
4011043Sbill}
4021043Sbill
4032822Swnj#undef	UNKNOWN
4041043Sbill#define UNKNOWN "su"
4051043Sbill
4061043Sbillchar *
4071043Sbillstypeof(ttyid)
4081043Sbillchar	*ttyid;
4091043Sbill{
4101043Sbill	static char	typebuf[16];
4111043Sbill	char		buf[50];
4121043Sbill	register FILE	*f;
4131043Sbill	register char	*p, *t, *q;
4141043Sbill
4151043Sbill	if (ttyid == NULL)
4161043Sbill		return (UNKNOWN);
4171043Sbill	f = fopen("/etc/ttytype", "r");
4181043Sbill	if (f == NULL)
4191043Sbill		return (UNKNOWN);
4201043Sbill	/* split off end of name */
4211043Sbill	for (p = q = ttyid; *p != 0; p++)
4221043Sbill		if (*p == '/')
4231043Sbill			q = p + 1;
4241043Sbill
4251043Sbill	/* scan the file */
4261043Sbill	while (fgets(buf, sizeof buf, f) != NULL)
4271043Sbill	{
4282822Swnj		for (t=buf; *t!=' ' && *t != '\t'; t++)
4291043Sbill			;
4301043Sbill		*t++ = 0;
4312822Swnj		while (*t == ' ' || *t == '\t')
4322822Swnj			t++;
4331043Sbill		for (p=t; *p>' '; p++)
4341043Sbill			;
4351043Sbill		*p = 0;
4361043Sbill		if (strcmp(q,t)==0) {
4371043Sbill			strcpy(typebuf, buf);
4381043Sbill			fclose(f);
4391043Sbill			return (typebuf);
4401043Sbill		}
4411043Sbill	}
4421043Sbill	fclose (f);
4431043Sbill	return (UNKNOWN);
4441043Sbill}
4456005Swnj
4466005Swnjgetstr(buf, cnt, err)
4476005Swnj	char *buf;
4486005Swnj	int cnt;
4496005Swnj	char *err;
4506005Swnj{
4516005Swnj	char c;
4526005Swnj
4536005Swnj	do {
4546005Swnj		if (read(0, &c, 1) != 1)
4556005Swnj			exit(1);
4566005Swnj		if (--cnt < 0) {
4576005Swnj			printf("%s too long\r\n", err);
4586005Swnj			exit(1);
4596005Swnj		}
4606005Swnj		*buf++ = c;
4616005Swnj	} while (c != 0);
4626005Swnj}
463