xref: /original-bsd/usr.bin/login/login.c (revision f737e041)
1 /*-
2  * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)login.c	8.2 (Berkeley) 02/24/94";
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("%s\n\t%s  %s\n\n",
362 	    "Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
363 		    "The Regents of the University of California. ",
364 		    "All rights reserved.");
365 		motd();
366 		(void)snprintf(tbuf,
367 		    sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
368 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
369 			(void)printf("You have %smail.\n",
370 			    (st.st_mtime > st.st_atime) ? "new " : "");
371 	}
372 
373 	(void)signal(SIGALRM, SIG_DFL);
374 	(void)signal(SIGQUIT, SIG_DFL);
375 	(void)signal(SIGINT, SIG_DFL);
376 	(void)signal(SIGTSTP, SIG_IGN);
377 
378 	tbuf[0] = '-';
379 	(void)strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ?
380 	    p + 1 : pwd->pw_shell);
381 
382 	if (setlogin(pwd->pw_name) < 0)
383 		syslog(LOG_ERR, "setlogin() failure: %m");
384 
385 	/* Discard permissions last so can't get killed and drop core. */
386 	if (rootlogin)
387 		(void) setuid(0);
388 	else
389 		(void) setuid(pwd->pw_uid);
390 
391 	execlp(pwd->pw_shell, tbuf, 0);
392 	(void)fprintf(stderr, "%s: %s\n", pwd->pw_shell, strerror(errno));
393 	exit(1);
394 }
395 
396 #ifdef	KERBEROS
397 #define	NBUFSIZ		(UT_NAMESIZE + 1 + 5)	/* .root suffix */
398 #else
399 #define	NBUFSIZ		(UT_NAMESIZE + 1)
400 #endif
401 
402 void
403 getloginname()
404 {
405 	register int ch;
406 	register char *p;
407 	static char nbuf[NBUFSIZ];
408 
409 	for (;;) {
410 		(void)printf("login: ");
411 		for (p = nbuf; (ch = getchar()) != '\n'; ) {
412 			if (ch == EOF) {
413 				badlogin(username);
414 				exit(0);
415 			}
416 			if (p < nbuf + (NBUFSIZ - 1))
417 				*p++ = ch;
418 		}
419 		if (p > nbuf)
420 			if (nbuf[0] == '-')
421 				(void)fprintf(stderr,
422 				    "login names may not start with '-'.\n");
423 			else {
424 				*p = '\0';
425 				username = nbuf;
426 				break;
427 			}
428 	}
429 }
430 
431 int
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 void
443 motd()
444 {
445 	register int fd, nchars;
446 	sig_t oldint;
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 /* ARGSUSED */
460 void
461 sigint(signo)
462 	int signo;
463 {
464 	longjmp(motdinterrupt, 1);
465 }
466 
467 /* ARGSUSED */
468 void
469 timedout(signo)
470 	int signo;
471 {
472 	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
473 	exit(0);
474 }
475 
476 void
477 checknologin()
478 {
479 	register int fd, nchars;
480 	char tbuf[8192];
481 
482 	if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
483 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
484 			(void)write(fileno(stdout), tbuf, nchars);
485 		sleepexit(0);
486 	}
487 }
488 
489 void
490 dolastlog(quiet)
491 	int quiet;
492 {
493 	struct lastlog ll;
494 	int fd;
495 
496 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
497 		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
498 		if (!quiet) {
499 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
500 			    ll.ll_time != 0) {
501 				(void)printf("Last login: %.*s ",
502 				    24-5, (char *)ctime(&ll.ll_time));
503 				if (*ll.ll_host != '\0')
504 					(void)printf("from %.*s\n",
505 					    (int)sizeof(ll.ll_host),
506 					    ll.ll_host);
507 				else
508 					(void)printf("on %.*s\n",
509 					    (int)sizeof(ll.ll_line),
510 					    ll.ll_line);
511 			}
512 			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
513 		}
514 		bzero((void *)&ll, sizeof(ll));
515 		(void)time(&ll.ll_time);
516 		(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
517 		if (hostname)
518 			(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
519 		(void)write(fd, (char *)&ll, sizeof(ll));
520 		(void)close(fd);
521 	}
522 }
523 
524 void
525 badlogin(name)
526 	char *name;
527 {
528 	if (failures == 0)
529 		return;
530 	if (hostname) {
531 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
532 		    failures, failures > 1 ? "S" : "", hostname);
533 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
534 		    "%d LOGIN FAILURE%s FROM %s, %s",
535 		    failures, failures > 1 ? "S" : "", hostname, name);
536 	} else {
537 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
538 		    failures, failures > 1 ? "S" : "", tty);
539 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
540 		    "%d LOGIN FAILURE%s ON %s, %s",
541 		    failures, failures > 1 ? "S" : "", tty, name);
542 	}
543 }
544 
545 #undef	UNKNOWN
546 #define	UNKNOWN	"su"
547 
548 char *
549 stypeof(ttyid)
550 	char *ttyid;
551 {
552 	struct ttyent *t;
553 
554 	return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
555 }
556 
557 void
558 sleepexit(eval)
559 	int eval;
560 {
561 	(void)sleep((u_int)5);
562 	exit(eval);
563 }
564