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