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