xref: /freebsd/usr.bin/login/login.c (revision 6acc486b3fd1fa853c71b28949cbd3c6130ea6eb)
1 /*-
2  * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)login.c	8.4 (Berkeley) 4/2/94";
42 #endif /* not lint */
43 
44 /*
45  * login [ name ]
46  * login -h hostname	(for telnetd, etc.)
47  * login -f name	(for pre-authenticated login: datakit, xterm, etc.)
48  */
49 
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/time.h>
53 #include <sys/resource.h>
54 #include <sys/file.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 
58 #include <err.h>
59 #include <errno.h>
60 #include <grp.h>
61 #include <netdb.h>
62 #include <pwd.h>
63 #include <setjmp.h>
64 #include <signal.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <syslog.h>
69 #include <ttyent.h>
70 #include <unistd.h>
71 #include <utmp.h>
72 
73 #ifdef LOGIN_CAP
74 #include <login_cap.h>
75 #else /* Undef AUTH as well */
76 #undef LOGIN_CAP_AUTH
77 #endif
78 
79 /* If LOGIN_CAP_AUTH is activated:
80  * kerberose & skey logins are runtime selected via login
81  * login_getstyle() and authentication types for login classes
82  * The actual login itself is handled via /usr/libexec/login_<style>
83  * Valid styles are determined by the auth-type=style,style entries
84  * in the login class.
85  */
86 #ifdef LOGIN_CAP_AUTH
87 #undef KERBEROS
88 #undef SKEY
89 #else
90 #ifdef	SKEY
91 #include <skey.h>
92 #endif /* SKEY */
93 #endif /* LOGIN_CAP_AUTH */
94 
95 #include "pathnames.h"
96 
97 void	 badlogin __P((char *));
98 void	 checknologin __P((void));
99 void	 dolastlog __P((int));
100 void	 getloginname __P((void));
101 void	 motd __P((char *));
102 int	 rootterm __P((char *));
103 void	 sigint __P((int));
104 void	 sleepexit __P((int));
105 char	*stypeof __P((char *));
106 void	 timedout __P((int));
107 void     login_fbtab __P((char *, uid_t, gid_t));
108 #ifdef KERBEROS
109 int	 klogin __P((struct passwd *, char *, char *, char *));
110 #endif
111 
112 extern void login __P((struct utmp *));
113 
114 #define	TTYGRPNAME	"tty"		/* name of group to own ttys */
115 
116 /*
117  * This bounds the time given to login.  Not a define so it can
118  * be patched on machines where it's too small.
119  */
120 u_int	timeout = 300;
121 
122 #ifdef KERBEROS
123 int	notickets = 1;
124 int	noticketsdontcomplain = 1;
125 char	*instance;
126 char	*krbtkfile_env;
127 int	authok;
128 #endif
129 
130 struct	passwd *pwd;
131 int	failures;
132 char	term[64], *envinit[1], *hostname, *username, *tty;
133 char    full_hostname[MAXHOSTNAMELEN];
134 
135 int
136 main(argc, argv)
137 	int argc;
138 	char *argv[];
139 {
140 	extern char **environ;
141 	struct group *gr;
142 	struct stat st;
143 	struct timeval tp;
144 	struct utmp utmp;
145 	int rootok;
146 	int ask, ch, cnt, fflag, hflag, pflag, quietlog, rootlogin, rval;
147 	int changepass;
148 	uid_t uid;
149 	char *domain, *p, *ep, *salt, *ttyn;
150 	char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
151 	char localhost[MAXHOSTNAMELEN];
152 	char shell[MAXPATHLEN];
153 #ifdef LOGIN_CAP
154 	login_cap_t *lc = NULL;
155 #ifdef LOGIN_CAP_AUTH
156 	char *style, *authtype;
157 	char *auth_method = NULL;
158 	char *instance = NULL;
159 	int authok;
160 #else /* !LOGIN_CAP_AUTH */
161 #ifdef SKEY
162 	int permit_passwd = 0;
163 #endif /* SKEY */
164 #endif /* LOGIN_CAP_AUTH */
165 #endif /* LOGIN_CAP */
166 
167 	(void)signal(SIGALRM, timedout);
168 	(void)alarm(timeout);
169 	(void)signal(SIGQUIT, SIG_IGN);
170 	(void)signal(SIGINT, SIG_IGN);
171 	(void)setpriority(PRIO_PROCESS, 0, 0);
172 
173 	openlog("login", LOG_ODELAY, LOG_AUTH);
174 
175 	/*
176 	 * -p is used by getty to tell login not to destroy the environment
177 	 * -f is used to skip a second login authentication
178 	 * -h is used by other servers to pass the name of the remote
179 	 *    host to login so that it may be placed in utmp and wtmp
180 	 */
181 	*full_hostname = '\0';
182 	domain = NULL;
183 	if (gethostname(localhost, sizeof(localhost)) < 0)
184 		syslog(LOG_ERR, "couldn't get local hostname: %m");
185 	else
186 		domain = strchr(localhost, '.');
187 
188 	fflag = hflag = pflag = 0;
189 	uid = getuid();
190 	while ((ch = getopt(argc, argv, "fh:p")) != EOF)
191 		switch (ch) {
192 		case 'f':
193 			fflag = 1;
194 			break;
195 		case 'h':
196 			if (uid)
197 				errx(1, "-h option: %s", strerror(EPERM));
198 			hflag = 1;
199 			strncpy(full_hostname, optarg, sizeof(full_hostname)-1);
200 			if (domain && (p = strchr(optarg, '.')) &&
201 			    strcasecmp(p, domain) == 0)
202 				*p = 0;
203 			if (strlen(optarg) > UT_HOSTSIZE) {
204 				struct hostent *hp = gethostbyname(optarg);
205 
206 				if (hp != NULL) {
207 					struct in_addr in;
208 
209 					memmove(&in, hp->h_addr, sizeof(in));
210 					optarg = strdup(inet_ntoa(in));
211 				} else
212 					optarg = "invalid hostname";
213 			}
214 			hostname = optarg;
215 			break;
216 		case 'p':
217 			pflag = 1;
218 			break;
219 		case '?':
220 		default:
221 			if (!uid)
222 				syslog(LOG_ERR, "invalid flag %c", ch);
223 			(void)fprintf(stderr,
224 			    "usage: login [-fp] [-h hostname] [username]\n");
225 			exit(1);
226 		}
227 	argc -= optind;
228 	argv += optind;
229 
230 	if (*argv) {
231 		username = *argv;
232 		ask = 0;
233 	} else
234 		ask = 1;
235 
236 	for (cnt = getdtablesize(); cnt > 2; cnt--)
237 		(void)close(cnt);
238 
239 	ttyn = ttyname(STDIN_FILENO);
240 	if (ttyn == NULL || *ttyn == '\0') {
241 		(void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
242 		ttyn = tname;
243 	}
244 	if (tty = strrchr(ttyn, '/'))
245 		++tty;
246 	else
247 		tty = ttyn;
248 
249 #ifdef LOGIN_CAP_AUTH
250 	authtype = hostname ? "rlogin" : "login";
251 #endif
252 
253 	for (cnt = 0;; ask = 1) {
254 		if (ask) {
255 			fflag = 0;
256 			getloginname();
257 		}
258 		rootlogin = 0;
259 		rootok = rootterm(tty); /* Default (auth may change) */
260 #ifdef LOGIN_CAP_AUTH
261 		authok = 0;
262 		if (auth_method = strchr(username, ':')) {
263 			*auth_method = '\0';
264 			auth_method++;
265 			if (*auth_method == '\0')
266 				auth_method = NULL;
267 		}
268 		/*
269 		 * We need to do this regardless of whether
270 		 * kerberos is available.
271 		 */
272 		if ((instance = strchr(username, '.')) != NULL) {
273 			if (strncmp(instance, ".root", 5) == 0)
274 				rootlogin = 1;
275 			*instance++ = '\0';
276 		} else
277 			instance = "";
278 #else /* !LOGIN_CAP_AUTH */
279 #ifdef KERBEROS
280 		if ((instance = strchr(username, '.')) != NULL) {
281 			if (strncmp(instance, ".root", 5) == 0)
282 				rootlogin = 1;
283 			*instance++ = '\0';
284 		} else
285 			instance = "";
286 #endif /* KERBEROS */
287 #endif /* LOGIN_CAP_AUTH */
288 
289 		if (strlen(username) > UT_NAMESIZE)
290 			username[UT_NAMESIZE] = '\0';
291 
292 		/*
293 		 * Note if trying multiple user names; log failures for
294 		 * previous user name, but don't bother logging one failure
295 		 * for nonexistent name (mistyped username).
296 		 */
297 		if (failures && strcmp(tbuf, username)) {
298 			if (failures > (pwd ? 0 : 1))
299 				badlogin(tbuf);
300 			failures = 0;
301 		}
302 		(void)strcpy(tbuf, username);
303 
304 		pwd = getpwnam(username);
305 #ifdef LOGIN_CAP
306 		/* Establish the class now, before we might goto
307 		 * within the next block. pwd can be NULL since it
308 		 * falls back to the "default" class if it is.
309 		 */
310 		lc = login_getclass(pwd);
311 #endif /* LOGIN_CAP */
312 
313 		/*
314 		 * if we have a valid account name, and it doesn't have a
315 		 * password, or the -f option was specified and the caller
316 		 * is root or the caller isn't changing their uid, don't
317 		 * authenticate.
318 		 */
319 		if (pwd != NULL) {
320 			salt = pwd->pw_passwd;
321 			if (pwd->pw_uid == 0)
322 				rootlogin = 1;
323 
324 			if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
325 				/* already authenticated */
326 				break;
327 			} else if (pwd->pw_passwd[0] == '\0') {
328 				/* pretend password okay */
329 				rval = 0;
330 				goto ttycheck;
331 			}
332 		}
333 		else
334 			salt = "xx";
335 
336 		fflag = 0;
337 
338 		(void)setpriority(PRIO_PROCESS, 0, -4);
339 
340 #ifdef LOGIN_CAP_AUTH
341 		/*
342 		 * This hands off authorisation to an authorisation program,
343 		 * depending on the styles available for the "auth-login",
344 		 * auth-rlogin (or default) authorisation styles.
345 		 * We do this regardless of whether an account exists so that
346 		 * the remote user cannot tell a "real" from an invented
347 		 * account name. If we don't have an account we just fall
348 		 * back to the first method for the "default" class.
349 		 */
350 		if ((style = login_getstyle(lc, auth_method, authtype)) == NULL) {
351 			rval = 1; /* No available authorisation method */
352 			(void)printf("No auth method available for %s.\n", authtype);
353 		} else {
354 			/* Put back the kerberos instance, if any was given.
355 			 * Don't worry about the non-kerberos case here, since
356 			 * if kerberos is not available or not selected and an
357 			 * instance is given at the login prompt, su or rlogin -l,
358 			 * then anything else should fail as well.
359 			 */
360 			if (*instance)
361 				*(instance - 1) = '.';
362 			rval = authenticate(username, lc ? lc->lc_class : "default", style, authtype);
363 			/* Junk it again */
364 			if (*instance)
365 				*(instance - 1) = '\0';
366 		}
367 
368 		if (!rval) {
369 			/*
370 			 * If authentication succeeds, run any approval
371 			 * program, if applicable for this class.
372 			 */
373 			char *approvep = login_getcapstr(lc, "approve", NULL, NULL);
374 			rval = 1; /* Assume bad login again */
375 			if (approvep==NULL || auth_script(approvep, approvep, username, lc->lc_class, 0) == 0) {
376 				int     r = auth_scan(AUTH_OKAY);
377 				/* See what the authorise program says */
378 				if (r != AUTH_NONE) {
379 					rval = 0;
380 					if (!rootok && (r & AUTH_ROOTOKAY))
381 						rootok = 1; /* root approved */
382 					else rootlogin = 0;
383 					if (!authok && (r & AUTH_SECURE))
384 						authok = 1; /* secure */
385 				}
386 			}
387 		}
388 #else /* !LOGIN_CAP_AUTH */
389 #ifdef SKEY
390 		permit_passwd = skeyaccess(username, tty, hostname ? full_hostname : NULL, NULL);
391 		p = skey_getpass("Password:", pwd, permit_passwd);
392 		ep = skey_crypt(p, salt, pwd, permit_passwd);
393 #else /* !SKEY */
394 		p = getpass("Password:");
395 		ep = crypt(p, salt);
396 #endif/* SKEY */
397 #ifdef KERBEROS
398 #ifdef SKEY
399 		if (pwd) {
400 			/* Do not allow user to type in kerberos password
401 			 * over the net (actually, this is ok for encrypted
402 			 * links, but we have no way of determining if the
403 			 * link is encrypted.
404 			 */
405 			if (!permit_passwd) {
406 				rval = 1;		/* force failure */
407 			} else
408 #endif /* SKEY */
409 			rval = klogin(pwd, instance, localhost, p);
410 			if (rval != 0 && rootlogin && pwd->pw_uid != 0)
411 				rootlogin = 0;
412 			if (rval == 0)
413 				authok = 1; /* kerberos authenticated ok */
414 			else if (rval == 1) /* fallback to unix passwd */
415 				rval = strcmp(ep, pwd->pw_passwd);
416 #ifdef SKEY
417 		}
418 #endif /* SKEY */
419 #else /* !KERBEROS */
420 		rval = strcmp(ep, pwd->pw_passwd);
421 #endif /* KERBEROS */
422 		/* clear entered password */
423 		memset(p, 0, strlen(p));
424 #endif /* LOGIN_CAP_AUTH */
425 
426 		(void)setpriority(PRIO_PROCESS, 0, 0);
427 #ifdef LOGIN_CAP
428 		if (rval)
429 			auth_rmfiles();
430 #endif
431 	ttycheck:
432 		/*
433 		 * If trying to log in as root without Kerberos,
434 		 * but with insecure terminal, refuse the login attempt.
435 		 */
436 #if defined(KERBEROS) || defined(LOGIN_CAP_AUTH)
437 		if (authok == 0)
438 #endif
439 		if (pwd && !rval && rootlogin && !rootok) {
440 			(void)fprintf(stderr, "%s login refused on this terminal.\n", pwd->pw_name);
441 			if (hostname)
442 				syslog(LOG_NOTICE, "LOGIN %s REFUSED FROM %s ON TTY %s", pwd->pw_name, full_hostname, tty);
443 			else
444 				syslog(LOG_NOTICE, "LOGIN %s REFUSED ON TTY %s", pwd->pw_name, tty);
445 			continue;
446 		}
447 
448 		if (pwd && !rval) /* valid password & authenticated */
449 			break;
450 
451 		(void)printf("Login incorrect\n");
452 		failures++;
453 		/* we allow 10 tries, but after 3 we start backing off */
454 		if (++cnt > 3) {
455 			if (cnt >= 10) {
456 				badlogin(username);
457 				sleepexit(1);
458 			}
459 			sleep((u_int)((cnt - 3) * 5));
460 		}
461 	}
462 
463 	/* committed to login -- turn off timeout */
464 	(void)alarm((u_int)0);
465 
466 	endpwent();
467 
468 	/* if user not super-user, check for disabled logins */
469 #ifdef LOGIN_CAP
470 	if (!rootlogin)
471 		auth_checknologin(lc);
472 #else
473 	if (!rootlogin)
474 		checknologin();
475 #endif
476 
477 #ifdef LOGIN_CAP
478 	quietlog = login_getcapbool(lc, "hushlogin", 0);
479 #else
480 	quietlog = 0;
481 #endif
482 	if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) {
483 #ifdef LOGIN_CAP
484 		if (login_getcapbool(lc, "requirehome", !rootlogin) || chdir("/") < 0) {
485 			(void)printf("No home directory %s!\n", pwd->pw_dir);
486 			sleepexit(1);
487 		}
488 #else
489 		if (chdir("/") < 0) {
490 			(void)printf("No home directory %s!\n", pwd->pw_dir);
491 			sleepexit(1);
492 		}
493 #endif
494 		pwd->pw_dir = "/";
495 		if (!quietlog)
496 			(void)printf("No home directory.\nLogging in with home = \"/\".\n");
497 	}
498 	if (!quietlog)
499 		quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
500 
501 	if (pwd->pw_change || pwd->pw_expire)
502 		(void)gettimeofday(&tp, (struct timezone *)NULL);
503 
504 #define DEFAULT_WARN  (2L * 7L & 86400L)  /* Two weeks */
505 
506 	changepass=0;
507 	if (pwd->pw_change) {
508 		if (tp.tv_sec >= pwd->pw_change) {
509 			(void)printf("Sorry -- your password has expired.\n");
510 			syslog(LOG_NOTICE, "%s Password expired - forcing change", pwd->pw_name);
511 			changepass=1;
512 #ifdef LOGIN_CAP
513 		} else {
514 			time_t warntime = (time_t)login_getcaptime(lc, "warnpassword", DEFAULT_WARN, DEFAULT_WARN);
515 			if (pwd->pw_change - tp.tv_sec < warntime && !quietlog)
516 				(void)printf("Warning: your password expires on %s", ctime(&pwd->pw_change));
517 		}
518 #else
519 		} else if (pwd->pw_change - tp.tv_sec < DEFAULT_WARN && !quietlog) {
520 			(void)printf("Warning: your password expires on %s", ctime(&pwd->pw_change));
521 		}
522 #endif
523 	}
524 	if (pwd->pw_expire) {
525 		if (tp.tv_sec >= pwd->pw_expire) {
526 			(void)printf("Sorry -- your account has expired.\n");
527 			syslog(LOG_NOTICE, "%s Account expired - login refused", pwd->pw_name);
528 			sleepexit(1);
529 #ifdef LOGIN_CAP
530 		} else {
531 			time_t warntime = (time_t)login_getcaptime(lc, "warnexpire", DEFAULT_WARN, DEFAULT_WARN);
532 			if (pwd->pw_expire - tp.tv_sec < warntime && !quietlog)
533 				(void)printf("Warning: your account expires on %s",
534 					    ctime(&pwd->pw_expire));
535 		}
536 #else
537 		} else if (pwd->pw_expire - tp.tv_sec < DEFAULT_WARN && !quietlog) {
538 			(void)printf("Warning: your account expires on %s",
539 				    ctime(&pwd->pw_expire));
540 		}
541 #endif
542 	}
543 
544 #ifdef LOGIN_CAP
545 	if (lc != NULL) {
546 		char  *msg = NULL;
547 
548 		if (hostname) {
549 			struct hostent *hp = gethostbyname(full_hostname);
550 
551 			if (hp == NULL)
552 				optarg = NULL;
553 			else {
554 				struct in_addr in;
555 				memmove(&in, hp->h_addr, sizeof(in));
556 				optarg = strdup(inet_ntoa(in));
557 			}
558 			if (!auth_hostok(lc, full_hostname, optarg)) {
559 				syslog(LOG_NOTICE, "%s LOGIN REFUSED (HOST) FROM %s", pwd->pw_name, full_hostname);
560 				msg = "Permission denied";
561 			}
562 		}
563 
564 		if (msg == NULL && !auth_ttyok(lc, tty)) {
565 			syslog(LOG_NOTICE, "%s LOGIN REFUSED (TTY) ON %s", pwd->pw_name, tty);
566 			msg = "Permission denied";
567 		}
568 
569 		if (msg == NULL && !auth_timeok(lc, time(NULL))) {
570 			syslog(LOG_NOTICE, "%s LOGIN REFUSED (TIME) %s %s", pwd->pw_name, hostname?"FROM":"ON", hostname?full_hostname:tty);
571 			msg = "Logins not available right now";
572 		}
573 
574 		if (msg != NULL) {
575 			printf("%s.\n", msg);
576 			sleepexit(1);
577 		}
578 	}
579 	strncpy(shell, login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell), sizeof shell);
580 #else /* !LOGIN_CAP */
581 	strncpy(shell, pwd->pw_shell, sizeof shell);
582 #endif /* LOGIN_CAP */
583 	shell[sizeof shell - 1] = '\0';
584 
585 #ifdef LOGIN_ACCESS
586 	if (login_access(pwd->pw_name, hostname ? full_hostname : tty) == 0) {
587 		printf("Permission denied\n");
588 		syslog(LOG_NOTICE, "%s LOGIN REFUSED (ACCESS) %s %s", pwd->pw_name, hostname?"FROM":"ON", hostname?full_hostname:tty);
589 		sleepexit(1);
590 	}
591 #endif /* LOGIN_ACCESS */
592 
593 	/* Nothing else left to fail -- really log in. */
594 	memset((void *)&utmp, 0, sizeof(utmp));
595 	(void)time(&utmp.ut_time);
596 	(void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
597 	if (hostname)
598 		(void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
599 	(void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
600 	login(&utmp);
601 
602 	dolastlog(quietlog);
603 
604 	/*
605 	 * Set device protections, depending on what terminal the
606 	 * user is logged in. This feature is used on Suns to give
607 	 * console users better privacy.
608 	 */
609 	login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
610 
611 	(void)chown(ttyn, pwd->pw_uid, (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
612 
613 	/* Preserve TERM if it happens to be already set */
614 	if ((p = getenv("TERM")) != NULL) {
615 		(void)strncpy(term, p, sizeof(term));
616 		term[sizeof(term)-1] = '\0';
617 	}
618 
619 	/* Exclude cons/vt/ptys only, assume dialup otherwise */
620 	if (hostname==NULL && strchr("vpqstPQST", tty[sizeof("tty")-1]) == NULL)
621 		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
622 
623 	/* If fflag is on, assume caller/authenticator has logged root login. */
624 	if (rootlogin && fflag == 0)
625 		syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s%s%s", username, tty, hostname?" FROM ":"", hostname?full_hostname:"");
626 
627 #ifdef KERBEROS
628 	if (!quietlog && notickets == 1 && !noticketsdontcomplain)
629 		(void)printf("Warning: no Kerberos tickets issued.\n");
630 #endif
631 
632 #ifdef LOGALL
633 	/*
634 	 * Syslog each successful login, so we don't have to watch hundreds
635 	 * of wtmp or lastlogin files.
636 	 */
637 	syslog(LOG_INFO, "login %s %s as %s", hostname?"from":"on", hostname?full_hostname:tty, pwd->pw_name);
638 #endif
639 
640 	/* Destroy environment unless user has requested its preservation. */
641 	if (!pflag)
642 		environ = envinit;
643 
644 	/* We don't need to be root anymore, so
645 	 * set the user and session context
646 	 */
647 #ifdef LOGIN_CAP
648 	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
649                 syslog(LOG_ERR, "setusercontext() failed - exiting");
650 		exit(1);
651 	}
652 #else
653      	if (setlogin(pwd->pw_name) < 0)
654                 syslog(LOG_ERR, "setlogin() failure: %m");
655 
656 	(void)setgid(pwd->pw_gid);
657 	initgroups(username, pwd->pw_gid);
658 	(void)setuid(rootlogin ? 0 : pwd->pw_uid);
659 #endif
660 
661 	if (*pwd->pw_shell == '\0')
662 		pwd->pw_shell = _PATH_BSHELL;
663 	(void)setenv("SHELL", pwd->pw_shell, 1);
664 	(void)setenv("HOME", pwd->pw_dir, 1);
665 	if (term[0] != '\0')
666 		(void)setenv("TERM", term, 1);	/* Preset overrides */
667 	else {
668 		(void)strncpy(term, stypeof(tty), sizeof(term));
669 		term[sizeof(term)-1] = '\0';
670 		(void)setenv("TERM", term, 0);	/* Fallback doesn't */
671 	}
672 	(void)setenv("LOGNAME", pwd->pw_name, 1);
673 	(void)setenv("USER", pwd->pw_name, 1);
674 	(void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
675 #if LOGIN_CAP_AUTH
676 	auth_env();
677 #else
678 #ifdef KERBEROS
679 	if (krbtkfile_env)
680 		(void)setenv("KRBTKFILE", krbtkfile_env, 1);
681 #endif
682 #endif
683 
684 	if (!quietlog) {
685 #ifdef LOGIN_CAP
686 		char *cw = login_getcapstr(lc, "copyright", NULL, NULL);
687 		if (cw != NULL && access(cw, F_OK) == 0)
688 			motd(cw);
689 		else
690 #endif
691 		(void)printf("Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994\n"
692 			     "\tThe Regents of the University of California.  All rights reserved.\n");
693 		(void)printf("\n");
694 #ifdef LOGIN_CAP
695 		cw = login_getcapstr(lc, "welcome", NULL, NULL);
696 		if (cw == NULL || access(cw, F_OK) != 0)
697 			cw = _PATH_MOTDFILE;
698 		motd(cw);
699 		cw = getenv("MAIL");	/* $MAIL may have been set by class */
700 		if (cw != NULL) {
701 			strncpy(tbuf, cw, sizeof(tbuf));
702 			tbuf[sizeof(tbuf)-1] = '\0';
703 		} else
704 			snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
705 #else
706 		motd(_PATH_MOTDFILE);
707 		snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
708 #endif
709 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
710 			(void)printf("You have %smail.\n", (st.st_mtime > st.st_atime) ? "new " : "");
711 	}
712 
713 	/* Login shells have a leading '-' in front of argv[0] */
714 	tbuf[0] = '-';
715 	(void)strcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ? p + 1 : pwd->pw_shell);
716 
717 #ifdef LOGIN_CAP
718 	login_close(lc);
719 #endif
720 
721 	(void)signal(SIGALRM, SIG_DFL);
722 	(void)signal(SIGQUIT, SIG_DFL);
723 	(void)signal(SIGINT, SIG_DFL);
724 	(void)signal(SIGTSTP, SIG_IGN);
725 
726 	if (changepass) {
727 		if (system(_PATH_CHPASS) != 0)
728 			sleepexit(1);
729 	}
730 
731 	execlp(shell, tbuf, 0);
732 	err(1, "%s", shell);
733 }
734 
735 
736 /* Allow for authentication style and/or kerberos instance */
737 
738 #define	NBUFSIZ		UT_NAMESIZE + 64
739 
740 void
741 getloginname()
742 {
743 	int ch;
744 	char *p;
745 	static char nbuf[NBUFSIZ];
746 
747 	for (;;) {
748 		(void)printf("login: ");
749 		for (p = nbuf; (ch = getchar()) != '\n'; ) {
750 			if (ch == EOF) {
751 				badlogin(username);
752 				exit(0);
753 			}
754 			if (p < nbuf + (NBUFSIZ - 1))
755 				*p++ = ch;
756 		}
757 		if (p > nbuf)
758 			if (nbuf[0] == '-')
759 				(void)fprintf(stderr,
760 				    "login names may not start with '-'.\n");
761 			else {
762 				*p = '\0';
763 				username = nbuf;
764 				break;
765 			}
766 	}
767 }
768 
769 int
770 rootterm(ttyn)
771 	char *ttyn;
772 {
773 	struct ttyent *t;
774 	return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
775 }
776 
777 volatile int motdinterrupt;
778 
779 /* ARGSUSED */
780 void
781 sigint(signo)
782 	int signo;
783 {
784 	motdinterrupt = 1;
785 }
786 
787 void
788 motd(motdfile)
789 	char *motdfile;
790 {
791 	int fd, nchars;
792 	sig_t oldint;
793 	char tbuf[256];
794 
795 	if ((fd = open(motdfile, O_RDONLY, 0)) < 0)
796 		return;
797 	motdinterrupt = 0;
798 	oldint = signal(SIGINT, sigint);
799 	while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0 && !motdinterrupt)
800 		(void)write(fileno(stdout), tbuf, nchars);
801 	(void)signal(SIGINT, oldint);
802 	(void)close(fd);
803 }
804 
805 /* ARGSUSED */
806 void
807 timedout(signo)
808 	int signo;
809 {
810 	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
811 	exit(0);
812 }
813 
814 #ifndef LOGIN_CAP
815 void
816 checknologin()
817 {
818 	int fd, nchars;
819 	char tbuf[8192];
820 
821 	if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
822 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
823 			(void)write(fileno(stdout), tbuf, nchars);
824 		sleepexit(0);
825 	}
826 }
827 #endif
828 
829 void
830 dolastlog(quiet)
831 	int quiet;
832 {
833 	struct lastlog ll;
834 	int fd;
835 
836 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
837 		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
838 		if (!quiet) {
839 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
840 			    ll.ll_time != 0) {
841 				(void)printf("Last login: %.*s ",
842 				    24-5, (char *)ctime(&ll.ll_time));
843 				if (*ll.ll_host != '\0')
844 					(void)printf("from %.*s\n",
845 					    (int)sizeof(ll.ll_host),
846 					    ll.ll_host);
847 				else
848 					(void)printf("on %.*s\n",
849 					    (int)sizeof(ll.ll_line),
850 					    ll.ll_line);
851 			}
852 			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
853 		}
854 		memset((void *)&ll, 0, sizeof(ll));
855 		(void)time(&ll.ll_time);
856 		(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
857 		if (hostname)
858 			(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
859 		(void)write(fd, (char *)&ll, sizeof(ll));
860 		(void)close(fd);
861 	}
862 }
863 
864 void
865 badlogin(name)
866 	char *name;
867 {
868 
869 	if (failures == 0)
870 		return;
871 	if (hostname) {
872 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
873 		    failures, failures > 1 ? "S" : "", full_hostname);
874 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
875 		    "%d LOGIN FAILURE%s FROM %s, %s",
876 		    failures, failures > 1 ? "S" : "", full_hostname, name);
877 	} else {
878 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
879 		    failures, failures > 1 ? "S" : "", tty);
880 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
881 		    "%d LOGIN FAILURE%s ON %s, %s",
882 		    failures, failures > 1 ? "S" : "", tty, name);
883 	}
884 }
885 
886 #undef	UNKNOWN
887 #define	UNKNOWN	"su"
888 
889 char *
890 stypeof(ttyid)
891 	char *ttyid;
892 {
893 	struct ttyent *t;
894 	return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
895 }
896 
897 void
898 sleepexit(eval)
899 	int eval;
900 {
901 	(void)sleep(5);
902 	exit(eval);
903 }
904 
905 
906