xref: /csrg-svn/usr.bin/login/login.c.1 (revision 6878)
1*6878Smckusickstatic	char *sccsid = "@(#)login.c.1	4.17 82/05/19";
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];
76*6878Smckusickchar	name[NMAX+1];
776197Srootchar	*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;
896197Sroot	int ldisc = 0, zero = 0;
906197Sroot	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);
986329Swnj	if (argc > 1 && !strcmp(argv[1], "-r")) {
996005Swnj		rflag++;
1006329Swnj		rhost = argv[2];
1016197Sroot		argc = 1;
1026329Swnj		getstr(rusername, sizeof (rusername), "remuser");
1036329Swnj		getstr(lusername, sizeof (lusername), "locuser");
1046005Swnj		getstr(term+5, sizeof(term)-5, "Terminal type");
1056329Swnj		if (getuid())
1066329Swnj			goto abnormal;
1076197Sroot		setpwent();
1086197Sroot		pwd = getpwnam(lusername);
1096329Swnj		endpwent();
1106197Sroot		if (pwd == NULL) {
1116329Swnj			if (strcmp(rusername, lusername))
1126329Swnj				printf("%s: No such user\r\n", lusername);
1136329Swnj			goto abnormal;
1146197Sroot		}
1156466Swnj		hostf = pwd->pw_uid ? fopen("/etc/hosts.equiv", "r") : 0;
1166197Sroot	again:
1176197Sroot		if (hostf) {
1186197Sroot		  char ahost[32];
1196197Sroot		  while (fgets(ahost, sizeof (ahost), hostf)) {
1206197Sroot			char *user;
1216197Sroot			if (index(ahost, '\n'))
1226197Sroot				*index(ahost, '\n') = 0;
1236197Sroot			user = index(ahost, ' ');
1246197Sroot			if (user)
1256197Sroot				*user++ = 0;
1266197Sroot			if (!strcmp(rhost, ahost) &&
1276329Swnj			    !strcmp(rusername, user ? user : lusername)) {
1286329Swnj				fclose(hostf);
1296197Sroot				goto normal;
1306329Swnj			}
1316197Sroot		  }
1326197Sroot		  fclose(hostf);
1336197Sroot		}
1346197Sroot		if (first == 1) {
1356197Sroot			first = 0;
1366197Sroot			if (chdir(pwd->pw_dir) < 0)
1376197Sroot				goto again;
1386197Sroot			hostf = fopen(".rhosts", "r");
1396197Sroot			goto again;
1406197Sroot		}
1416329Swnjabnormal:
1426197Sroot		rhost = 0;
1436197Sroot		rflag = -1;
1446005Swnj	}
1456197Srootnormal:
1466197Sroot	ioctl(0, TIOCLSET, &zero);
1471547Sbill	ioctl(0, TIOCNXCL, 0);
1486329Swnj	ioctl(0, FIONBIO, &zero);
1496329Swnj	ioctl(0, FIOASYNC, &zero);
1501043Sbill	gtty(0, &ttyb);
1516005Swnj	if (rflag) {
1526005Swnj		char *cp = index(term, '/');
1536005Swnj		if (cp) {
1546005Swnj			int i;
1556005Swnj			*cp++ = 0;
1566005Swnj			for (i = 0; i < NSPEEDS; i++)
1576005Swnj				if (!strcmp(speeds[i], cp)) {
1586005Swnj					ttyb.sg_ispeed = ttyb.sg_ospeed = i;
1596005Swnj					break;
1606005Swnj				}
1616005Swnj		}
1626005Swnj		ttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS;
1636005Swnj	}
1642822Swnj	ttyb.sg_erase = CERASE;
1652822Swnj	ttyb.sg_kill = CKILL;
1661043Sbill	stty(0, &ttyb);
1671365Sbill	ioctl(0, TIOCSETC, &tc);
1681365Sbill	ioctl(0, TIOCSLTC, &ltc);
1691043Sbill	for (t=3; t<20; t++)
1701043Sbill		close(t);
1711043Sbill	ttyn = ttyname(0);
1722822Swnj	if (ttyn==(char *)0)
1731043Sbill		ttyn = "/dev/tty??";
1742822Swnj	do {
1752822Swnj		ldisc = 0;
1762822Swnj		ioctl(0, TIOCSETD, &ldisc);
1772822Swnj		invalid = FALSE;
1782822Swnj		SCPYN(utmp.ut_name, "");
1792822Swnj		if (argc>1) {
1802822Swnj			SCPYN(utmp.ut_name, argv[1]);
1812822Swnj			argc = 0;
1821043Sbill		}
1836329Swnj		if (rflag) {
1846197Sroot			strcpy(utmp.ut_name, lusername);
1856329Swnj			if (rflag == -1)
1866329Swnj				rflag = 0;
1876329Swnj		} else
1886197Sroot			while (utmp.ut_name[0] == '\0') {
1896197Sroot				namep = utmp.ut_name;
1906197Sroot				{ char hostname[32];
1916197Sroot				  gethostname(hostname, sizeof (hostname));
1926197Sroot				  printf("%s login: ", hostname); }
1936197Sroot				while ((c = getchar()) != '\n') {
1946197Sroot					if (c == ' ')
1956197Sroot						c = '_';
1966197Sroot					if (c == EOF)
1976197Sroot						exit(0);
1986197Sroot					if (namep < utmp.ut_name+NMAX)
1996197Sroot						*namep++ = c;
2006197Sroot				}
2012822Swnj			}
2026197Sroot		if (rhost == 0) {
2036197Sroot			setpwent();
2046197Sroot			if ((pwd = getpwnam(utmp.ut_name)) == NULL)
2056197Sroot				pwd = &nouser;
2066197Sroot			endpwent();
2072822Swnj		}
2082822Swnj		if (!strcmp(pwd->pw_shell, "/bin/csh")) {
2092822Swnj			ldisc = NTTYDISC;
2102822Swnj			ioctl(0, TIOCSETD, &ldisc);
2112822Swnj		}
2126197Sroot		if (rhost == 0) {
2136197Sroot			if (*pwd->pw_passwd != '\0') {
2146197Sroot				char *pp;
2156197Sroot				nice(-4);
2166197Sroot				if (rflag == 0)
2176197Sroot					pp = getpass("Password:");
2186197Sroot				else
2196197Sroot					pp = rpassword;
2206197Sroot				namep = crypt(pp,pwd->pw_passwd);
2216197Sroot				nice(4);
2226197Sroot				if (strcmp(namep, pwd->pw_passwd))
2236197Sroot					invalid = TRUE;
2246197Sroot			}
2252822Swnj		}
2262822Swnj		if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) {
2272822Swnj			/* logins are disabled except for root */
2282822Swnj			while ((c = getc(nlfd)) != EOF)
2292822Swnj				putchar(c);
2302822Swnj			fflush(stdout);
2312822Swnj			sleep(5);
2322822Swnj			exit(0);
2332822Swnj		}
2342822Swnj		if (!invalid && pwd->pw_uid == 0 &&
2352822Swnj		    !rootterm(ttyn+sizeof("/dev/")-1)) {
2366329Swnj			logerr("ROOT LOGIN REFUSED %s",
2376329Swnj			    ttyn+sizeof("/dev/")-1);
2382822Swnj			invalid = TRUE;
2392822Swnj		}
2402822Swnj		if (invalid) {
2411043Sbill			printf("Login incorrect\n");
2426329Swnj			if (ttyn[sizeof("/dev/tty")-1] == 'd')
2436329Swnj				logerr("BADDIALUP %s %s\n",
2446329Swnj				    ttyn+sizeof("/dev/")-1, utmp.ut_name);
2451043Sbill		}
2462822Swnj		if (*pwd->pw_shell == '\0')
2472822Swnj			pwd->pw_shell = "/bin/sh";
2482822Swnj		i = strlen(pwd->pw_shell);
2492822Swnj		if (chdir(pwd->pw_dir) < 0 && !invalid ) {
2502822Swnj			if (chdir("/") < 0) {
2512822Swnj				printf("No directory!\n");
2522822Swnj				invalid = TRUE;
2532822Swnj			} else {
2542822Swnj				printf("No directory!  Logging in with home=/\n");
2552822Swnj				pwd->pw_dir = "/";
2562822Swnj			}
2571043Sbill		}
2586005Swnj		if (rflag && invalid)
2596005Swnj			exit(1);
2602822Swnj	} while (invalid);
2611043Sbill
2626005Swnj
2631043Sbill	time(&utmp.ut_time);
2641043Sbill	t = ttyslot();
2651043Sbill	if (t>0 && (f = open("/etc/utmp", 1)) >= 0) {
2661043Sbill		lseek(f, (long)(t*sizeof(utmp)), 0);
2671043Sbill		SCPYN(utmp.ut_line, rindex(ttyn, '/')+1);
2681043Sbill		write(f, (char *)&utmp, sizeof(utmp));
2691043Sbill		close(f);
2701043Sbill	}
2711043Sbill	if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) {
2721043Sbill		lseek(f, 0L, 2);
2731043Sbill		write(f, (char *)&utmp, sizeof(utmp));
2741043Sbill		close(f);
2751043Sbill	}
2766329Swnj	quietlog = 0;
2772822Swnj	if (access(qlog, 0) == 0)
2786329Swnj		quietlog = 1;
2796329Swnj	if ((f = open(lastlog, 2)) >= 0) {
2802822Swnj		struct lastlog ll;
2812822Swnj
2822822Swnj		lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
2832822Swnj		if (read(f, (char *) &ll, sizeof ll) == sizeof ll &&
2842822Swnj		    ll.ll_time != 0) {
2856329Swnj			if (quietlog == 0)
2862822Swnj			printf("Last login: %.*s on %.*s\n"
2872822Swnj			    , 24-5
2882822Swnj			    , (char *) ctime(&ll.ll_time)
2892822Swnj			    , sizeof(ll.ll_line)
2902822Swnj			    , ll.ll_line
2912822Swnj			);
2922822Swnj		}
2932822Swnj		lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
2942822Swnj		time(&ll.ll_time);
2952822Swnj		SCPYN(ll.ll_line, rindex(ttyn, '/')+1);
2962822Swnj		write(f, (char *) &ll, sizeof ll);
2972822Swnj		close(f);
2982822Swnj	}
2991043Sbill	chown(ttyn, pwd->pw_uid, pwd->pw_gid);
3001043Sbill	setgid(pwd->pw_gid);
301*6878Smckusick	strncpy(name, utmp.ut_name, NMAX);
302*6878Smckusick	name[NMAX] = '\0';
303*6878Smckusick	inigrp(name, pwd->pw_gid);
3041043Sbill	setuid(pwd->pw_uid);
3051043Sbill	environ = envinit;
3061043Sbill	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
3071043Sbill	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
3086329Swnj	if (term[strlen("TERM=")] == 0)
3096005Swnj		strncat(term, stypeof(ttyn), sizeof(term)-6);
3102822Swnj	strncat(user, pwd->pw_name, sizeof(user)-6);
3111043Sbill	if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
3121043Sbill		namep = pwd->pw_shell;
3131043Sbill	else
3141043Sbill		namep++;
3151043Sbill	strcat(minusnam, namep);
3161043Sbill	alarm(0);
3176197Sroot	umask(022);
3186329Swnj	if (ttyn[sizeof("/dev/tty")-1] == 'd')
3196329Swnj		logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name);
3206329Swnj	if (!quietlog) {
3212822Swnj		showmotd();
3222822Swnj		strcat(maildir, pwd->pw_name);
3232822Swnj		if (access(maildir,4)==0) {
3242822Swnj			struct stat statb;
3252822Swnj			stat(maildir, &statb);
3262822Swnj			if (statb.st_size)
3272822Swnj				printf("You have mail.\n");
3282822Swnj		}
3292822Swnj	}
3302822Swnj
3311043Sbill	signal(SIGQUIT, SIG_DFL);
3321043Sbill	signal(SIGINT, SIG_DFL);
3333935Sroot	signal(SIGTSTP, SIG_IGN);
3341043Sbill	execlp(pwd->pw_shell, minusnam, 0);
3352822Swnj	perror(pwd->pw_shell);
3361043Sbill	printf("No shell\n");
3371043Sbill	exit(0);
3381043Sbill}
3391043Sbill
3401043Sbillint	stopmotd;
3411043Sbillcatch()
3421043Sbill{
3436466Swnj
3441043Sbill	signal(SIGINT, SIG_IGN);
3451043Sbill	stopmotd++;
3461043Sbill}
3471043Sbill
3482822Swnjrootterm(tty)
3496466Swnj	char *tty;
3502822Swnj{
3512822Swnj	register FILE *fd;
3526466Swnj	char buf[100];
3532822Swnj
3546466Swnj	if (rflag)
3556466Swnj		return(1);
3562822Swnj	if ((fd = fopen(securetty, "r")) == NULL)
3572822Swnj		return(1);
3582822Swnj	while (fgets(buf, sizeof buf, fd) != NULL) {
3592822Swnj		buf[strlen(buf)-1] = '\0';
3602822Swnj		if (strcmp(tty, buf) == 0) {
3612822Swnj			fclose(fd);
3622822Swnj			return(1);
3632822Swnj		}
3642822Swnj	}
3652822Swnj	fclose(fd);
3662822Swnj	return(0);
3672822Swnj}
3682822Swnj
3691043Sbillshowmotd()
3701043Sbill{
3711043Sbill	FILE *mf;
3721043Sbill	register c;
3731043Sbill
3741043Sbill	signal(SIGINT, catch);
3752822Swnj	if ((mf = fopen("/etc/motd","r")) != NULL) {
3762822Swnj		while ((c = getc(mf)) != EOF && stopmotd == 0)
3771043Sbill			putchar(c);
3781043Sbill		fclose(mf);
3791043Sbill	}
3801043Sbill	signal(SIGINT, SIG_IGN);
3811043Sbill}
3821043Sbill
3832822Swnj#undef	UNKNOWN
3841043Sbill#define UNKNOWN "su"
3851043Sbill
3861043Sbillchar *
3871043Sbillstypeof(ttyid)
3881043Sbillchar	*ttyid;
3891043Sbill{
3901043Sbill	static char	typebuf[16];
3911043Sbill	char		buf[50];
3921043Sbill	register FILE	*f;
3931043Sbill	register char	*p, *t, *q;
3941043Sbill
3951043Sbill	if (ttyid == NULL)
3961043Sbill		return (UNKNOWN);
3971043Sbill	f = fopen("/etc/ttytype", "r");
3981043Sbill	if (f == NULL)
3991043Sbill		return (UNKNOWN);
4001043Sbill	/* split off end of name */
4011043Sbill	for (p = q = ttyid; *p != 0; p++)
4021043Sbill		if (*p == '/')
4031043Sbill			q = p + 1;
4041043Sbill
4051043Sbill	/* scan the file */
4061043Sbill	while (fgets(buf, sizeof buf, f) != NULL)
4071043Sbill	{
4082822Swnj		for (t=buf; *t!=' ' && *t != '\t'; t++)
4091043Sbill			;
4101043Sbill		*t++ = 0;
4112822Swnj		while (*t == ' ' || *t == '\t')
4122822Swnj			t++;
4131043Sbill		for (p=t; *p>' '; p++)
4141043Sbill			;
4151043Sbill		*p = 0;
4161043Sbill		if (strcmp(q,t)==0) {
4171043Sbill			strcpy(typebuf, buf);
4181043Sbill			fclose(f);
4191043Sbill			return (typebuf);
4201043Sbill		}
4211043Sbill	}
4221043Sbill	fclose (f);
4231043Sbill	return (UNKNOWN);
4241043Sbill}
4256005Swnj
4266005Swnjgetstr(buf, cnt, err)
4276005Swnj	char *buf;
4286005Swnj	int cnt;
4296005Swnj	char *err;
4306005Swnj{
4316005Swnj	char c;
4326005Swnj
4336005Swnj	do {
4346005Swnj		if (read(0, &c, 1) != 1)
4356005Swnj			exit(1);
4366005Swnj		if (--cnt < 0) {
4376005Swnj			printf("%s too long\r\n", err);
4386005Swnj			exit(1);
4396005Swnj		}
4406005Swnj		*buf++ = c;
4416005Swnj	} while (c != 0);
4426005Swnj}
4436329Swnj
4446329Swnjlogerr(fmt, a1, a2, a3)
4456329Swnj	char *fmt, *a1, *a2, *a3;
4466329Swnj{
4476329Swnj
4486329Swnj}
449