xref: /original-bsd/usr.bin/login/login.c.1 (revision f0fd5f8a)
1static	char *sccsid = "@(#)login.c.1	4.19 82/11/14";
2/*
3 * login [ name ]
4 * login -r
5 */
6
7#include <sys/types.h>
8#include <sgtty.h>
9#include <utmp.h>
10#include <signal.h>
11#include <pwd.h>
12#include <stdio.h>
13#include <sys/stat.h>
14#include <lastlog.h>
15
16#define	SCPYN(a, b)	strncpy(a, b, sizeof(a))
17
18#define NMAX	sizeof(utmp.ut_name)
19#define LMAX	sizeof(utmp.ut_line)
20
21#define	FALSE	0
22#define	TRUE	-1
23
24char	nolog[] =	"/etc/nologin";
25char	qlog[]  =	".hushlogin";
26char	securetty[] =	"/etc/securetty";
27char	maildir[30] =	"/usr/spool/mail/";
28char	lastlog[] =	"/usr/adm/lastlog";
29struct	passwd nouser = {"", "nope"};
30struct	sgttyb ttyb;
31struct	utmp utmp;
32char	minusnam[16] = "-";
33
34char	homedir[64] = "HOME=";
35char	shell[64] = "SHELL=";
36char	term[64] = "TERM=";
37char	user[20] = "USER=";
38char	*speeds[] =
39    { "0", "50", "75", "110", "134", "150", "200", "300",
40      "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" };
41#define	NSPEEDS	(sizeof (speeds) / sizeof (speeds[0]))
42
43char	*envinit[] =
44    {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", term, user, 0};
45
46struct	passwd *pwd;
47struct	passwd *getpwnam();
48char	*strcat(), *rindex(), *index();
49int	setpwent();
50char	*ttyname();
51char	*crypt();
52char	*getpass();
53char	*rindex();
54char	*stypeof();
55extern	char **environ;
56
57#define	CTRL(c)	('c'&037)
58#define	CERASE	'#'
59#define	CEOT	CTRL(d)
60#define	CKILL	'@'
61#define	CQUIT	034		/* FS, cntl shift L */
62#define	CINTR	0177		/* DEL */
63#define	CSTOP	CTRL(s)
64#define	CSTART	CTRL(q)
65#define	CBRK	0377
66struct	tchars tc = {
67	CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
68};
69struct	ltchars ltc = {
70	CTRL(z), CTRL(y), CTRL(r), CTRL(o), CTRL(w), CTRL(v)
71};
72
73int	rflag;
74char	rusername[NMAX+1], lusername[NMAX+1];
75char	rpassword[NMAX+1];
76char	name[NMAX+1];
77char	*rhost;
78
79main(argc, argv)
80char **argv;
81{
82	register char *namep;
83	int t, f, c;
84	int invalid;
85	int quietlog;
86	int i;
87	FILE *nlfd;
88	char *ttyn;
89	int ldisc = 0, zero = 0;
90	FILE *hostf; int first = 1;
91
92	alarm(60);
93	signal(SIGQUIT, SIG_IGN);
94	signal(SIGINT, SIG_IGN);
95	nice(-100);
96	nice(20);
97	nice(0);
98	if (argc > 1 && !strcmp(argv[1], "-r")) {
99		rflag++;
100		rhost = argv[2];
101		argc = 1;
102		getstr(rusername, sizeof (rusername), "remuser");
103		getstr(lusername, sizeof (lusername), "locuser");
104		getstr(term+5, sizeof(term)-5, "Terminal type");
105		if (getuid())
106			goto abnormal;
107		setpwent();
108		pwd = getpwnam(lusername);
109		endpwent();
110		if (pwd == NULL) {
111			if (strcmp(rusername, lusername))
112				printf("%s: No such user\r\n", lusername);
113			goto abnormal;
114		}
115		hostf = pwd->pw_uid ? fopen("/etc/hosts.equiv", "r") : 0;
116	again:
117		if (hostf) {
118		  char ahost[32];
119		  while (fgets(ahost, sizeof (ahost), hostf)) {
120			char *user;
121			if (index(ahost, '\n'))
122				*index(ahost, '\n') = 0;
123			user = index(ahost, ' ');
124			if (user)
125				*user++ = 0;
126			if (!strcmp(rhost, ahost) &&
127			    !strcmp(rusername, user ? user : lusername)) {
128				fclose(hostf);
129				goto normal;
130			}
131		  }
132		  fclose(hostf);
133		}
134		if (first == 1) {
135			first = 0;
136			if (chdir(pwd->pw_dir) < 0)
137				goto again;
138			hostf = fopen(".rhosts", "r");
139			goto again;
140		}
141abnormal:
142		rhost = 0;
143		rflag = -1;
144	}
145normal:
146	ioctl(0, TIOCLSET, &zero);
147	ioctl(0, TIOCNXCL, 0);
148	ioctl(0, FIONBIO, &zero);
149	ioctl(0, FIOASYNC, &zero);
150	gtty(0, &ttyb);
151	if (rflag) {
152		char *cp = index(term, '/');
153		if (cp) {
154			int i;
155			*cp++ = 0;
156			for (i = 0; i < NSPEEDS; i++)
157				if (!strcmp(speeds[i], cp)) {
158					ttyb.sg_ispeed = ttyb.sg_ospeed = i;
159					break;
160				}
161		}
162		ttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS;
163	}
164	ttyb.sg_erase = CERASE;
165	ttyb.sg_kill = CKILL;
166	stty(0, &ttyb);
167	ioctl(0, TIOCSETC, &tc);
168	ioctl(0, TIOCSLTC, &ltc);
169	for (t=3; t<20; t++)
170		close(t);
171	ttyn = ttyname(0);
172	if (ttyn==(char *)0)
173		ttyn = "/dev/tty??";
174	do {
175		ldisc = 0;
176		ioctl(0, TIOCSETD, &ldisc);
177		invalid = FALSE;
178		SCPYN(utmp.ut_name, "");
179		if (argc>1) {
180			SCPYN(utmp.ut_name, argv[1]);
181			argc = 0;
182		}
183		if (rflag) {
184			strcpy(utmp.ut_name, lusername);
185			if (rflag == -1)
186				rflag = 0;
187		} else
188			while (utmp.ut_name[0] == '\0') {
189				namep = utmp.ut_name;
190				{ char hostname[32];
191				  gethostname(hostname, sizeof (hostname));
192				  printf("%s login: ", hostname); }
193				while ((c = getchar()) != '\n') {
194					if (c == ' ')
195						c = '_';
196					if (c == EOF)
197						exit(0);
198					if (namep < utmp.ut_name+NMAX)
199						*namep++ = c;
200				}
201			}
202		if (rhost == 0) {
203			setpwent();
204			if ((pwd = getpwnam(utmp.ut_name)) == NULL)
205				pwd = &nouser;
206			endpwent();
207		}
208		if (!strcmp(pwd->pw_shell, "/bin/csh")) {
209			ldisc = NTTYDISC;
210			ioctl(0, TIOCSETD, &ldisc);
211		}
212		if (rhost == 0) {
213			if (*pwd->pw_passwd != '\0') {
214				char *pp;
215				nice(-4);
216				if (rflag == 0)
217					pp = getpass("Password:");
218				else
219					pp = rpassword;
220				namep = crypt(pp,pwd->pw_passwd);
221				nice(4);
222				if (strcmp(namep, pwd->pw_passwd))
223					invalid = TRUE;
224			}
225		}
226		if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) {
227			/* logins are disabled except for root */
228			while ((c = getc(nlfd)) != EOF)
229				putchar(c);
230			fflush(stdout);
231			sleep(5);
232			exit(0);
233		}
234		if (!invalid && pwd->pw_uid == 0 &&
235		    !rootterm(ttyn+sizeof("/dev/")-1)) {
236			logerr("ROOT LOGIN REFUSED %s",
237			    ttyn+sizeof("/dev/")-1);
238			invalid = TRUE;
239		}
240		if (invalid) {
241			printf("Login incorrect\n");
242			if (ttyn[sizeof("/dev/tty")-1] == 'd')
243				logerr("BADDIALUP %s %s\n",
244				    ttyn+sizeof("/dev/")-1, utmp.ut_name);
245		}
246		if (*pwd->pw_shell == '\0')
247			pwd->pw_shell = "/bin/sh";
248		i = strlen(pwd->pw_shell);
249		if (chdir(pwd->pw_dir) < 0 && !invalid ) {
250			if (chdir("/") < 0) {
251				printf("No directory!\n");
252				invalid = TRUE;
253			} else {
254				printf("No directory!  Logging in with home=/\n");
255				pwd->pw_dir = "/";
256			}
257		}
258		if (rflag && invalid)
259			exit(1);
260	} while (invalid);
261
262
263	time(&utmp.ut_time);
264	t = ttyslot();
265	if (t>0 && (f = open("/etc/utmp", 1)) >= 0) {
266		lseek(f, (long)(t*sizeof(utmp)), 0);
267		SCPYN(utmp.ut_line, rindex(ttyn, '/')+1);
268		write(f, (char *)&utmp, sizeof(utmp));
269		close(f);
270	}
271	if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) {
272		lseek(f, 0L, 2);
273		write(f, (char *)&utmp, sizeof(utmp));
274		close(f);
275	}
276	quietlog = 0;
277	if (access(qlog, 0) == 0)
278		quietlog = 1;
279	if ((f = open(lastlog, 2)) >= 0) {
280		struct lastlog ll;
281
282		lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
283		if (read(f, (char *) &ll, sizeof ll) == sizeof ll &&
284		    ll.ll_time != 0) {
285			if (quietlog == 0)
286			printf("Last login: %.*s on %.*s\n"
287			    , 24-5
288			    , (char *) ctime(&ll.ll_time)
289			    , sizeof(ll.ll_line)
290			    , ll.ll_line
291			);
292		}
293		lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
294		time(&ll.ll_time);
295		SCPYN(ll.ll_line, rindex(ttyn, '/')+1);
296		write(f, (char *) &ll, sizeof ll);
297		close(f);
298	}
299	chown(ttyn, pwd->pw_uid, pwd->pw_gid);
300	chmod(ttyn, 622);
301	setgid(pwd->pw_gid);
302	strncpy(name, utmp.ut_name, NMAX);
303	name[NMAX] = '\0';
304	initgroups(name, pwd->pw_gid);
305	setuid(pwd->pw_uid);
306	environ = envinit;
307	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
308	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
309	if (term[strlen("TERM=")] == 0)
310		strncat(term, stypeof(ttyn), sizeof(term)-6);
311	strncat(user, pwd->pw_name, sizeof(user)-6);
312	if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
313		namep = pwd->pw_shell;
314	else
315		namep++;
316	strcat(minusnam, namep);
317	alarm(0);
318	umask(022);
319	if (ttyn[sizeof("/dev/tty")-1] == 'd')
320		logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name);
321	if (!quietlog) {
322		showmotd();
323		strcat(maildir, pwd->pw_name);
324		if (access(maildir,4)==0) {
325			struct stat statb;
326			stat(maildir, &statb);
327			if (statb.st_size)
328				printf("You have mail.\n");
329		}
330	}
331
332	signal(SIGQUIT, SIG_DFL);
333	signal(SIGINT, SIG_DFL);
334	signal(SIGTSTP, SIG_IGN);
335	execlp(pwd->pw_shell, minusnam, 0);
336	perror(pwd->pw_shell);
337	printf("No shell\n");
338	exit(0);
339}
340
341int	stopmotd;
342catch()
343{
344
345	signal(SIGINT, SIG_IGN);
346	stopmotd++;
347}
348
349rootterm(tty)
350	char *tty;
351{
352	register FILE *fd;
353	char buf[100];
354
355	if (rflag)
356		return(1);
357	if ((fd = fopen(securetty, "r")) == NULL)
358		return(1);
359	while (fgets(buf, sizeof buf, fd) != NULL) {
360		buf[strlen(buf)-1] = '\0';
361		if (strcmp(tty, buf) == 0) {
362			fclose(fd);
363			return(1);
364		}
365	}
366	fclose(fd);
367	return(0);
368}
369
370showmotd()
371{
372	FILE *mf;
373	register c;
374
375	signal(SIGINT, catch);
376	if ((mf = fopen("/etc/motd","r")) != NULL) {
377		while ((c = getc(mf)) != EOF && stopmotd == 0)
378			putchar(c);
379		fclose(mf);
380	}
381	signal(SIGINT, SIG_IGN);
382}
383
384#undef	UNKNOWN
385#define UNKNOWN "su"
386
387char *
388stypeof(ttyid)
389char	*ttyid;
390{
391	static char	typebuf[16];
392	char		buf[50];
393	register FILE	*f;
394	register char	*p, *t, *q;
395
396	if (ttyid == NULL)
397		return (UNKNOWN);
398	f = fopen("/etc/ttytype", "r");
399	if (f == NULL)
400		return (UNKNOWN);
401	/* split off end of name */
402	for (p = q = ttyid; *p != 0; p++)
403		if (*p == '/')
404			q = p + 1;
405
406	/* scan the file */
407	while (fgets(buf, sizeof buf, f) != NULL)
408	{
409		for (t=buf; *t!=' ' && *t != '\t'; t++)
410			;
411		*t++ = 0;
412		while (*t == ' ' || *t == '\t')
413			t++;
414		for (p=t; *p>' '; p++)
415			;
416		*p = 0;
417		if (strcmp(q,t)==0) {
418			strcpy(typebuf, buf);
419			fclose(f);
420			return (typebuf);
421		}
422	}
423	fclose (f);
424	return (UNKNOWN);
425}
426
427getstr(buf, cnt, err)
428	char *buf;
429	int cnt;
430	char *err;
431{
432	char c;
433
434	do {
435		if (read(0, &c, 1) != 1)
436			exit(1);
437		if (--cnt < 0) {
438			printf("%s too long\r\n", err);
439			exit(1);
440		}
441		*buf++ = c;
442	} while (c != 0);
443}
444
445logerr(fmt, a1, a2, a3)
446	char *fmt, *a1, *a2, *a3;
447{
448
449}
450