xref: /original-bsd/usr.bin/login/login.c (revision d4efd688)
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.75 (Berkeley) 11/19/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 int	rootlogin;
51 #ifdef KERBEROS
52 int	notickets = 1;
53 char	*instance;
54 char	*krbtkfile_env;
55 int	authok;
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 option: %s\n", strerror(EPERM));
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] [-h hostname] [username]\n");
130 			exit(1);
131 		}
132 	argc -= optind;
133 	argv += optind;
134 	if (*argv) {
135 		username = *argv;
136 		ask = 0;
137 	} else
138 		ask = 1;
139 
140 	for (cnt = getdtablesize(); cnt > 2; cnt--)
141 		close(cnt);
142 
143 	ttyn = ttyname(0);
144 	if (ttyn == NULL || *ttyn == '\0') {
145 		(void)sprintf(tname, "%s??", _PATH_TTY);
146 		ttyn = tname;
147 	}
148 	if (tty = rindex(ttyn, '/'))
149 		++tty;
150 	else
151 		tty = ttyn;
152 
153 	for (cnt = 0;; ask = 1) {
154 		if (ask) {
155 			fflag = 0;
156 			getloginname();
157 		}
158 #ifdef	KERBEROS
159 		if ((instance = index(username, '.')) != NULL) {
160 			if (strncmp(instance, ".root", 5) == 0)
161 				rootlogin++;
162 			*instance++ = '\0';
163 		} else {
164 			rootlogin = 0;
165 			instance = "";
166 		}
167 #endif
168 		if (strlen(username) > UT_NAMESIZE)
169 			username[UT_NAMESIZE] = '\0';
170 
171 		/*
172 		 * Note if trying multiple user names; log failures for
173 		 * previous user name, but don't bother logging one failure
174 		 * for nonexistent name (mistyped username).
175 		 */
176 		if (failures && strcmp(tbuf, username)) {
177 			if (failures > (pwd ? 0 : 1))
178 				badlogin(tbuf);
179 			failures = 0;
180 		}
181 		(void)strcpy(tbuf, username);
182 
183 		if (pwd = getpwnam(username))
184 			salt = pwd->pw_passwd;
185 		else
186 			salt = "xx";
187 
188 		/*
189 		 * if we have a valid account name, and it doesn't have a
190 		 * password, or the -f option was specified and the caller
191 		 * is root or the caller isn't changing their uid, don't
192 		 * authenticate.
193 		 */
194 		if (pwd && (*pwd->pw_passwd == '\0' ||
195 		    fflag && (uid == 0 || uid == pwd->pw_uid)))
196 			break;
197 		fflag = 0;
198 		if (pwd && pwd->pw_uid == 0)
199 			rootlogin = 1;
200 		else
201 			rootlogin = 0;
202 
203 		(void)setpriority(PRIO_PROCESS, 0, -4);
204 
205 		p = getpass("Password:");
206 
207 		if (pwd) {
208 #ifdef KERBEROS
209 			rval = klogin(pwd, instance, localhost, p);
210 			if (rval == 0)
211 				authok = 1;
212 			else if (rval == 1) {
213 				if (pwd->pw_uid != 0)
214 					rootlogin = 0;
215 				rval = strcmp(crypt(p, salt), pwd->pw_passwd);
216 			}
217 #else
218 			rval = strcmp(crypt(p, salt), pwd->pw_passwd);
219 #endif
220 		}
221 		bzero(p, strlen(p));
222 
223 		(void)setpriority(PRIO_PROCESS, 0, 0);
224 
225 		/*
226 		 * If trying to log in as root without Kerberos,
227 		 * but with insecure terminal, refuse the login attempt.
228 		 */
229 #ifdef KERBEROS
230 		if (authok == 0)
231 #endif
232 		if (pwd && rootlogin && !rootterm(tty)) {
233 			(void)fprintf(stderr,
234 			    "%s login refused on this terminal.\n",
235 			    pwd->pw_name);
236 			if (hostname)
237 				syslog(LOG_NOTICE,
238 				    "LOGIN %s REFUSED FROM %s ON TTY %s",
239 				    pwd->pw_name, hostname, tty);
240 			else
241 				syslog(LOG_NOTICE,
242 				    "LOGIN %s REFUSED ON TTY %s",
243 				     pwd->pw_name, tty);
244 			continue;
245 		}
246 
247 		if (pwd && !rval)
248 			break;
249 
250 		(void)printf("Login incorrect\n");
251 		failures++;
252 		/* we allow 10 tries, but after 3 we start backing off */
253 		if (++cnt > 3) {
254 			if (cnt >= 10) {
255 				badlogin(username);
256 				sleepexit(1);
257 			}
258 			sleep((u_int)((cnt - 3) * 5));
259 		}
260 	}
261 
262 	/* committed to login -- turn off timeout */
263 	(void)alarm((u_int)0);
264 
265 	endpwent();
266 
267 	/* if user not super-user, check for disabled logins */
268 	if (!rootlogin)
269 		checknologin();
270 
271 	if (chdir(pwd->pw_dir) < 0) {
272 		(void)printf("No home directory %s!\n", pwd->pw_dir);
273 		if (chdir("/"))
274 			exit(0);
275 		pwd->pw_dir = "/";
276 		(void)printf("Logging in with home = \"/\".\n");
277 	}
278 
279 	quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
280 
281 	if (pwd->pw_change || pwd->pw_expire)
282 		(void)gettimeofday(&tp, (struct timezone *)NULL);
283 	if (pwd->pw_change)
284 		if (tp.tv_sec >= pwd->pw_change) {
285 			(void)printf("Sorry -- your password has expired.\n");
286 			sleepexit(1);
287 		} else if (pwd->pw_change - tp.tv_sec <
288 		    2 * DAYSPERWEEK * SECSPERDAY && !quietlog)
289 			(void)printf("Warning: your password expires on %s",
290 			    ctime(&pwd->pw_change));
291 	if (pwd->pw_expire)
292 		if (tp.tv_sec >= pwd->pw_expire) {
293 			(void)printf("Sorry -- your account has expired.\n");
294 			sleepexit(1);
295 		} else if (pwd->pw_expire - tp.tv_sec <
296 		    2 * DAYSPERWEEK * SECSPERDAY && !quietlog)
297 			(void)printf("Warning: your account expires on %s",
298 			    ctime(&pwd->pw_expire));
299 
300 	/* nothing else left to fail -- really log in */
301 	{
302 		struct utmp utmp;
303 
304 		bzero((void *)&utmp, sizeof(utmp));
305 		(void)time(&utmp.ut_time);
306 		strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
307 		if (hostname)
308 			strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
309 		strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
310 		login(&utmp);
311 	}
312 
313 	dolastlog(quietlog);
314 
315 	(void)chown(ttyn, pwd->pw_uid,
316 	    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
317 	(void)setgid(pwd->pw_gid);
318 
319 	initgroups(username, pwd->pw_gid);
320 
321 	if (*pwd->pw_shell == '\0')
322 		pwd->pw_shell = _PATH_BSHELL;
323 
324 	/* destroy environment unless user has requested preservation */
325 	if (!pflag)
326 		environ = envinit;
327 	(void)setenv("HOME", pwd->pw_dir, 1);
328 	(void)setenv("SHELL", pwd->pw_shell, 1);
329 	if (term[0] == '\0')
330 		strncpy(term, stypeof(tty), sizeof(term));
331 	(void)setenv("TERM", term, 0);
332 	(void)setenv("LOGNAME", pwd->pw_name, 1);
333 	(void)setenv("USER", pwd->pw_name, 1);
334 	(void)setenv("PATH", _PATH_DEFPATH, 0);
335 #ifdef KERBEROS
336 	if (krbtkfile_env)
337 		(void)setenv("KRBTKFILE", krbtkfile_env, 1);
338 #endif
339 
340 	if (tty[sizeof("tty")-1] == 'd')
341 		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
342 	/* if fflag is on, assume caller/authenticator has logged root login */
343 	if (rootlogin && fflag == 0)
344 		if (hostname)
345 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
346 			    username, tty, hostname);
347 		else
348 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty);
349 
350 #ifdef KERBEROS
351 	if (!quietlog && notickets == 1)
352 		(void)printf("Warning: no Kerberos tickets issued.\n");
353 #endif
354 
355 	if (!quietlog) {
356 		struct stat st;
357 
358 		printf(
359 "Copyright (c) 1980,1983,1986,1988,1990,1991 The Regents of the University\n%s",
360 "of California.  All rights reserved.\n\n");
361 		motd();
362 		(void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
363 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
364 			(void)printf("You have %smail.\n",
365 			    (st.st_mtime > st.st_atime) ? "new " : "");
366 	}
367 
368 	(void)signal(SIGALRM, SIG_DFL);
369 	(void)signal(SIGQUIT, SIG_DFL);
370 	(void)signal(SIGINT, SIG_DFL);
371 	(void)signal(SIGTSTP, SIG_IGN);
372 
373 	tbuf[0] = '-';
374 	strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ?
375 	    p + 1 : pwd->pw_shell);
376 
377 	if (setlogin(pwd->pw_name) < 0)
378 		syslog(LOG_ERR, "setlogin() failure: %m");
379 
380 	/* discard permissions last so can't get killed and drop core */
381 	if (rootlogin)
382 		(void) setuid(0);
383 	else
384 		(void) setuid(pwd->pw_uid);
385 
386 	execlp(pwd->pw_shell, tbuf, 0);
387 	(void)fprintf(stderr, "%s: %s\n", pwd->pw_shell, strerror(errno));
388 	exit(1);
389 }
390 
391 #ifdef	KERBEROS
392 #define	NBUFSIZ		(UT_NAMESIZE + 1 + 5) /* .root suffix */
393 #else
394 #define	NBUFSIZ		(UT_NAMESIZE + 1)
395 #endif
396 
397 getloginname()
398 {
399 	register int ch;
400 	register char *p;
401 	static char nbuf[NBUFSIZ];
402 
403 	for (;;) {
404 		(void)printf("login: ");
405 		for (p = nbuf; (ch = getchar()) != '\n'; ) {
406 			if (ch == EOF) {
407 				badlogin(username);
408 				exit(0);
409 			}
410 			if (p < nbuf + (NBUFSIZ - 1))
411 				*p++ = ch;
412 		}
413 		if (p > nbuf)
414 			if (nbuf[0] == '-')
415 				(void)fprintf(stderr,
416 				    "login names may not start with '-'.\n");
417 			else {
418 				*p = '\0';
419 				username = nbuf;
420 				break;
421 			}
422 	}
423 }
424 
425 void
426 timedout()
427 {
428 	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
429 	exit(0);
430 }
431 
432 rootterm(ttyn)
433 	char *ttyn;
434 {
435 	struct ttyent *t;
436 
437 	return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE);
438 }
439 
440 jmp_buf motdinterrupt;
441 
442 motd()
443 {
444 	register int fd, nchars;
445 	sig_t oldint;
446 	void sigint();
447 	char tbuf[8192];
448 
449 	if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0)
450 		return;
451 	oldint = signal(SIGINT, sigint);
452 	if (setjmp(motdinterrupt) == 0)
453 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
454 			(void)write(fileno(stdout), tbuf, nchars);
455 	(void)signal(SIGINT, oldint);
456 	(void)close(fd);
457 }
458 
459 void
460 sigint()
461 {
462 	longjmp(motdinterrupt, 1);
463 }
464 
465 checknologin()
466 {
467 	register int fd, nchars;
468 	char tbuf[8192];
469 
470 	if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
471 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
472 			(void)write(fileno(stdout), tbuf, nchars);
473 		sleepexit(0);
474 	}
475 }
476 
477 dolastlog(quiet)
478 	int quiet;
479 {
480 	struct lastlog ll;
481 	int fd;
482 	char *ctime();
483 
484 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
485 		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
486 		if (!quiet) {
487 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
488 			    ll.ll_time != 0) {
489 				(void)printf("Last login: %.*s ",
490 				    24-5, (char *)ctime(&ll.ll_time));
491 				if (*ll.ll_host != '\0')
492 					(void)printf("from %.*s\n",
493 					    sizeof(ll.ll_host), ll.ll_host);
494 				else
495 					(void)printf("on %.*s\n",
496 					    sizeof(ll.ll_line), ll.ll_line);
497 			}
498 			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
499 		}
500 		bzero((void *)&ll, sizeof(ll));
501 		(void)time(&ll.ll_time);
502 		strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
503 		if (hostname)
504 			strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
505 		(void)write(fd, (char *)&ll, sizeof(ll));
506 		(void)close(fd);
507 	}
508 }
509 
510 badlogin(name)
511 	char *name;
512 {
513 	if (failures == 0)
514 		return;
515 	if (hostname) {
516 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
517 		    failures, failures > 1 ? "S" : "", hostname);
518 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
519 		    "%d LOGIN FAILURE%s FROM %s, %s",
520 		    failures, failures > 1 ? "S" : "", hostname, name);
521 	} else {
522 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
523 		    failures, failures > 1 ? "S" : "", tty);
524 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
525 		    "%d LOGIN FAILURE%s ON %s, %s",
526 		    failures, failures > 1 ? "S" : "", tty, name);
527 	}
528 }
529 
530 #undef	UNKNOWN
531 #define	UNKNOWN	"su"
532 
533 char *
534 stypeof(ttyid)
535 	char *ttyid;
536 {
537 	struct ttyent *t;
538 
539 	return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
540 }
541 
542 sleepexit(eval)
543 	int eval;
544 {
545 	sleep((u_int)5);
546 	exit(eval);
547 }
548