xref: /original-bsd/usr.bin/login/login.c (revision 08eb28af)
1 /*-
2  * Copyright (c) 1980, 1987, 1988, 1991 The Regents of the University
3  * of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1980, 1987, 1988, 1991 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)login.c	5.69 (Berkeley) 05/10/91";
16 #endif /* not lint */
17 
18 /*
19  * login [ name ]
20  * login -h hostname	(for telnetd, etc.)
21  * login -f name	(for pre-authenticated login: datakit, xterm, etc.)
22  */
23 
24 #include <sys/param.h>
25 #include <sys/stat.h>
26 #include <sys/time.h>
27 #include <sys/resource.h>
28 #include <sys/file.h>
29 
30 #include <utmp.h>
31 #include <signal.h>
32 #include <errno.h>
33 #include <ttyent.h>
34 #include <syslog.h>
35 #include <grp.h>
36 #include <pwd.h>
37 #include <setjmp.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <tzfile.h>
41 #include "pathnames.h"
42 
43 #define	TTYGRPNAME	"tty"		/* name of group to own ttys */
44 
45 /*
46  * This bounds the time given to login.  Not a define so it can
47  * be patched on machines where it's too small.
48  */
49 int	timeout = 300;
50 #ifdef KERBEROS
51 int	notickets = 1;
52 int	rootlogin = 0;
53 #define	ROOTLOGIN	(rootlogin || (pwd && pwd->pw_uid == 0))
54 #else
55 #define	ROOTLOGIN	(pwd && pwd->pw_uid == 0)
56 #endif
57 
58 struct	passwd *pwd;
59 int	failures;
60 char	term[64], *envinit[1], *hostname, *username, *tty;
61 
62 main(argc, argv)
63 	int argc;
64 	char **argv;
65 {
66 	extern int optind;
67 	extern char *optarg, **environ;
68 	struct timeval tp;
69 	struct group *gr;
70 	register int ch;
71 	register char *p;
72 	int ask, fflag, hflag, pflag, cnt, uid;
73 	int quietlog, rval;
74 	char *domain, *salt, *ttyn;
75 	char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
76 	char localhost[MAXHOSTNAMELEN];
77 	char *ctime(), *ttyname(), *stypeof(), *crypt(), *getpass();
78 	time_t time();
79 	off_t lseek();
80 	void timedout();
81 
82 	(void)signal(SIGALRM, timedout);
83 	(void)alarm((u_int)timeout);
84 	(void)signal(SIGQUIT, SIG_IGN);
85 	(void)signal(SIGINT, SIG_IGN);
86 	(void)setpriority(PRIO_PROCESS, 0, 0);
87 
88 	openlog("login", LOG_ODELAY, LOG_AUTH);
89 
90 	/*
91 	 * -p is used by getty to tell login not to destroy the environment
92  	 * -f is used to skip a second login authentication
93 	 * -h is used by other servers to pass the name of the remote
94 	 *    host to login so that it may be placed in utmp and wtmp
95 	 */
96 	domain = NULL;
97 	if (gethostname(localhost, sizeof(localhost)) < 0)
98 		syslog(LOG_ERR, "couldn't get local hostname: %m");
99 	else
100 		domain = index(localhost, '.');
101 
102 	fflag = hflag = pflag = 0;
103 	uid = getuid();
104 	while ((ch = getopt(argc, argv, "fh:p")) != EOF)
105 		switch (ch) {
106 		case 'f':
107 			fflag = 1;
108 			break;
109 		case 'h':
110 			if (uid) {
111 				(void)fprintf(stderr,
112 				    "login: -h for super-user only.\n");
113 				exit(1);
114 			}
115 			hflag = 1;
116 			if (domain && (p = index(optarg, '.')) &&
117 			    strcasecmp(p, domain) == 0)
118 				*p = 0;
119 			hostname = optarg;
120 			break;
121 		case 'p':
122 			pflag = 1;
123 			break;
124 		case '?':
125 		default:
126 			if (!uid)
127 				syslog(LOG_ERR, "invalid flag %c", ch);
128 			(void)fprintf(stderr,
129 			    "usage: login [-fp] [username]\n");
130 			exit(1);
131 		}
132 	argc -= optind;
133 	argv += optind;
134 	if (*argv) {
135 		username = *argv;
136 		if (strlen(username) > UT_NAMESIZE)
137 			username[UT_NAMESIZE] = '\0';
138 		ask = 0;
139 	} else
140 		ask = 1;
141 
142 	for (cnt = getdtablesize(); cnt > 2; cnt--)
143 		close(cnt);
144 
145 	ttyn = ttyname(0);
146 	if (ttyn == NULL || *ttyn == '\0') {
147 		(void)sprintf(tname, "%s??", _PATH_TTY);
148 		ttyn = tname;
149 	}
150 	if (tty = rindex(ttyn, '/'))
151 		++tty;
152 	else
153 		tty = ttyn;
154 
155 	for (cnt = 0;; ask = 1) {
156 		if (ask) {
157 			fflag = 0;
158 			getloginname();
159 		}
160 		/*
161 		 * Note if trying multiple user names; log failures for
162 		 * previous user name, but don't bother logging one failure
163 		 * for nonexistent name (mistyped username).
164 		 */
165 		if (failures && strcmp(tbuf, username)) {
166 			if (failures > (pwd ? 0 : 1))
167 				badlogin(tbuf);
168 			failures = 0;
169 		}
170 		(void)strcpy(tbuf, username);
171 
172 		if (pwd = getpwnam(username))
173 			salt = pwd->pw_passwd;
174 		else
175 			salt = "xx";
176 
177 		/*
178 		 * if we have a valid account name, and it doesn't have a
179 		 * password, or the -f option was specified and the caller
180 		 * is root or the caller isn't changing their uid, don't
181 		 * authenticate.
182 		 */
183 		if (pwd && (*pwd->pw_passwd == '\0' ||
184 		    fflag && (uid == 0 || uid == pwd->pw_uid)))
185 			break;
186 		fflag = 0;
187 
188 		/*
189 		 * If trying to log in as root, but with insecure terminal,
190 		 * refuse the login attempt.
191 		 */
192 		if (ROOTLOGIN && !rootterm(tty)) {
193 			(void)fprintf(stderr,
194 			    "%s login refused on this terminal.\n",
195 			    pwd->pw_name);
196 			if (hostname)
197 				syslog(LOG_NOTICE,
198 				    "LOGIN %s REFUSED FROM %s ON TTY %s",
199 				    pwd->pw_name, hostname, tty);
200 			else
201 				syslog(LOG_NOTICE,
202 				    "LOGIN %s REFUSED ON TTY %s",
203 				     pwd->pw_name, tty);
204 			continue;
205 		}
206 
207 		(void)setpriority(PRIO_PROCESS, 0, -4);
208 
209 		p = getpass("Password:");
210 
211 		if (pwd) {
212 #ifdef KERBEROS
213 			rval = klogin(pwd, localhost, p);
214 			if (rval == 1)
215 				rval = strcmp(crypt(p, salt), pwd->pw_passwd);
216 #else
217 			rval = strcmp(crypt(p, salt), pwd->pw_passwd);
218 #endif
219 		}
220 		bzero(p, strlen(p));
221 
222 		(void)setpriority(PRIO_PROCESS, 0, 0);
223 
224 		if (pwd && !rval)
225 			break;
226 
227 		(void)printf("Login incorrect\n");
228 		failures++;
229 		/* we allow 10 tries, but after 3 we start backing off */
230 		if (++cnt > 3) {
231 			if (cnt >= 10) {
232 				badlogin(username);
233 				sleepexit(1);
234 			}
235 			sleep((u_int)((cnt - 3) * 5));
236 		}
237 	}
238 
239 	/* committed to login -- turn off timeout */
240 	(void)alarm((u_int)0);
241 
242 	/* paranoia... */
243 	endpwent();
244 
245 	/* if user not super-user, check for disabled logins */
246 	if (!(ROOTLOGIN))
247 		checknologin();
248 
249 	if (chdir(pwd->pw_dir) < 0) {
250 		(void)printf("No directory %s!\n", pwd->pw_dir);
251 		if (chdir("/"))
252 			exit(0);
253 		pwd->pw_dir = "/";
254 		(void)printf("Logging in with home = \"/\".\n");
255 	}
256 
257 	quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
258 
259 	if (pwd->pw_change || pwd->pw_expire)
260 		(void)gettimeofday(&tp, (struct timezone *)NULL);
261 	if (pwd->pw_change)
262 		if (tp.tv_sec >= pwd->pw_change) {
263 			(void)printf("Sorry -- your password has expired.\n");
264 			sleepexit(1);
265 		}
266 		else if (pwd->pw_change - tp.tv_sec <
267 		    2 * DAYSPERWEEK * SECSPERDAY && !quietlog)
268 			(void)printf("Warning: your password expires on %s",
269 			    ctime(&pwd->pw_expire));
270 	if (pwd->pw_expire)
271 		if (tp.tv_sec >= pwd->pw_expire) {
272 			(void)printf("Sorry -- your account has expired.\n");
273 			sleepexit(1);
274 		}
275 		else if (pwd->pw_expire - tp.tv_sec <
276 		    2 * DAYSPERWEEK * SECSPERDAY && !quietlog)
277 			(void)printf("Warning: your account expires on %s",
278 			    ctime(&pwd->pw_expire));
279 
280 	/* nothing else left to fail -- really log in */
281 	{
282 		struct utmp utmp;
283 
284 		bzero((void *)&utmp, sizeof(utmp));
285 		(void)time(&utmp.ut_time);
286 		strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
287 		if (hostname)
288 			strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
289 		strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
290 		login(&utmp);
291 	}
292 
293 	dolastlog(quietlog);
294 
295 	(void)chown(ttyn, pwd->pw_uid,
296 	    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
297 	(void)setgid(pwd->pw_gid);
298 
299 	initgroups(username, pwd->pw_gid);
300 
301 	if (*pwd->pw_shell == '\0')
302 		pwd->pw_shell = _PATH_BSHELL;
303 
304 	/* destroy environment unless user has requested preservation */
305 	if (!pflag)
306 		environ = envinit;
307 	(void)setenv("HOME", pwd->pw_dir, 1);
308 	(void)setenv("SHELL", pwd->pw_shell, 1);
309 	if (term[0] == '\0')
310 		strncpy(term, stypeof(tty), sizeof(term));
311 	(void)setenv("TERM", term, 0);
312 	(void)setenv("USER", pwd->pw_name, 1);
313 	(void)setenv("PATH", _PATH_DEFPATH, 0);
314 
315 	if (tty[sizeof("tty")-1] == 'd')
316 		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
317 	/* if fflag is on, assume caller/authenticator has logged root login */
318 	if (ROOTLOGIN && fflag == 0)
319 		if (hostname)
320 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
321 			    username, tty, hostname);
322 		else
323 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty);
324 
325 #ifdef KERBEROS
326 	if (!quietlog && notickets == 1)
327 		(void)printf("Warning: no Kerberos tickets issued.\n");
328 #endif
329 
330 	if (!quietlog) {
331 		struct stat st;
332 
333 		printf(
334 "Copyright (c) 1980,1983,1986,1988,1990,1991 The Regents of the University\n%s",
335 "of California.  All rights reserved.\n\n");
336 		motd();
337 		(void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
338 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
339 			(void)printf("You have %smail.\n",
340 			    (st.st_mtime > st.st_atime) ? "new " : "");
341 	}
342 
343 	(void)signal(SIGALRM, SIG_DFL);
344 	(void)signal(SIGQUIT, SIG_DFL);
345 	(void)signal(SIGINT, SIG_DFL);
346 	(void)signal(SIGTSTP, SIG_IGN);
347 
348 	tbuf[0] = '-';
349 	strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ?
350 	    p + 1 : pwd->pw_shell);
351 
352 	if (setlogin(pwd->pw_name) < 0)
353 		syslog(LOG_ERR, "setlogin() failure: %m");
354 
355 	/* discard permissions last so can't get killed and drop core */
356 	if (ROOTLOGIN)
357 		(void) setuid(0);
358 	else
359 		(void)setuid(pwd->pw_uid);
360 
361 	execlp(pwd->pw_shell, tbuf, 0);
362 	(void)fprintf(stderr, "login: no shell: %s.\n", strerror(errno));
363 	exit(0);
364 }
365 
366 #ifdef	KERBEROS
367 #define	NBUFSIZ		(UT_NAMESIZE + 1 + 5) /* .root suffix */
368 #else
369 #define	NBUFSIZ		(UT_NAMESIZE + 1)
370 #endif
371 
372 getloginname()
373 {
374 	register int ch;
375 	register char *p, *instance;
376 	static char nbuf[NBUFSIZ];
377 
378 	for (;;) {
379 		(void)printf("login: ");
380 		for (p = nbuf; (ch = getchar()) != '\n'; ) {
381 			if (ch == EOF) {
382 				badlogin(username);
383 				exit(0);
384 			}
385 			if (p < nbuf + (NBUFSIZ - 1))
386 				*p++ = ch;
387 		}
388 		if (p > nbuf)
389 			if (nbuf[0] == '-')
390 				(void)fprintf(stderr,
391 				    "login names may not start with '-'.\n");
392 			else {
393 				*p = '\0';
394 				username = nbuf;
395 				break;
396 			}
397 	}
398 #ifdef	KERBEROS
399 	if ((instance = index(nbuf, '.')) != NULL) {
400 		if (strncmp(instance, ".root", 5) == 0) {
401 			rootlogin++;
402 		}
403 		*instance = '\0';
404 		if ((instance - nbuf) > UT_NAMESIZE)
405 			nbuf[UT_NAMESIZE] = '\0';
406 	} else
407 		rootlogin = 0;
408 #endif
409 }
410 
411 void
412 timedout()
413 {
414 	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
415 	exit(0);
416 }
417 
418 rootterm(ttyn)
419 	char *ttyn;
420 {
421 	struct ttyent *t;
422 
423 	return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE);
424 }
425 
426 jmp_buf motdinterrupt;
427 
428 motd()
429 {
430 	register int fd, nchars;
431 	sig_t oldint;
432 	void sigint();
433 	char tbuf[8192];
434 
435 	if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0)
436 		return;
437 	oldint = signal(SIGINT, sigint);
438 	if (setjmp(motdinterrupt) == 0)
439 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
440 			(void)write(fileno(stdout), tbuf, nchars);
441 	(void)signal(SIGINT, oldint);
442 	(void)close(fd);
443 }
444 
445 void
446 sigint()
447 {
448 	longjmp(motdinterrupt, 1);
449 }
450 
451 checknologin()
452 {
453 	register int fd, nchars;
454 	char tbuf[8192];
455 
456 	if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
457 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
458 			(void)write(fileno(stdout), tbuf, nchars);
459 		sleepexit(0);
460 	}
461 }
462 
463 dolastlog(quiet)
464 	int quiet;
465 {
466 	struct lastlog ll;
467 	int fd;
468 	char *ctime();
469 
470 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
471 		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
472 		if (!quiet) {
473 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
474 			    ll.ll_time != 0) {
475 				(void)printf("Last login: %.*s ",
476 				    24-5, (char *)ctime(&ll.ll_time));
477 				if (*ll.ll_host != '\0')
478 					(void)printf("from %.*s\n",
479 					    sizeof(ll.ll_host), ll.ll_host);
480 				else
481 					(void)printf("on %.*s\n",
482 					    sizeof(ll.ll_line), ll.ll_line);
483 			}
484 			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
485 		}
486 		bzero((void *)&ll, sizeof(ll));
487 		(void)time(&ll.ll_time);
488 		strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
489 		if (hostname)
490 			strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
491 		(void)write(fd, (char *)&ll, sizeof(ll));
492 		(void)close(fd);
493 	}
494 }
495 
496 badlogin(name)
497 	char *name;
498 {
499 	if (failures == 0)
500 		return;
501 	if (hostname) {
502 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
503 		    failures, failures > 1 ? "S" : "", hostname);
504 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
505 		    "%d LOGIN FAILURE%s FROM %s, %s",
506 		    failures, failures > 1 ? "S" : "", hostname, name);
507 	} else {
508 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
509 		    failures, failures > 1 ? "S" : "", tty);
510 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
511 		    "%d LOGIN FAILURE%s ON %s, %s",
512 		    failures, failures > 1 ? "S" : "", tty, name);
513 	}
514 }
515 
516 #undef	UNKNOWN
517 #define	UNKNOWN	"su"
518 
519 char *
520 stypeof(ttyid)
521 	char *ttyid;
522 {
523 	struct ttyent *t;
524 
525 	return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
526 }
527 
528 sleepexit(eval)
529 	int eval;
530 {
531 	sleep((u_int)5);
532 	exit(eval);
533 }
534