xref: /freebsd/usr.bin/login/login.c (revision 1f5367caeebf97b2f74cb34d5a71dd85c76e6de4)
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")) != EOF)
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_getclass(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
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", !rootlogin))
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 
573 #ifdef LOGIN_CAP
574 	warntime = login_getcaptime(lc, "warnexpire",
575 				    DEFAULT_WARN, DEFAULT_WARN);
576 #else
577 	warntime = DEFAULT_WARN;
578 #endif
579 
580 	if (pwd->pw_expire) {
581 		if (tp.tv_sec >= pwd->pw_expire) {
582 			refused("Sorry -- your account has expired",
583 				"EXPIRED", 1);
584 		} else if (pwd->pw_expire - tp.tv_sec < warntime && !quietlog)
585 		    (void)printf("Warning: your account expires on %s",
586 				 ctime(&pwd->pw_expire));
587 	}
588 
589 #ifdef LOGIN_CAP
590 	if (lc != NULL) {
591 		if (hostname) {
592 			struct hostent *hp = gethostbyname(full_hostname);
593 
594 			if (hp == NULL)
595 				optarg = NULL;
596 			else {
597 				struct in_addr in;
598 				memmove(&in, hp->h_addr, sizeof(in));
599 				optarg = strdup(inet_ntoa(in));
600 			}
601 			if (!auth_hostok(lc, full_hostname, optarg))
602 				refused("Permission denied", "HOST", 1);
603 		}
604 
605 		if (!auth_ttyok(lc, tty))
606 			refused("Permission denied", "TTY", 1);
607 
608 		if (!auth_timeok(lc, time(NULL)))
609 			refused("Logins not available right now", "TIME", 1);
610 	}
611         shell=login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
612 #else /* !LOGIN_CAP */
613        shell=pwd->pw_shell;
614 #endif /* LOGIN_CAP */
615 	if (*pwd->pw_shell == '\0')
616 		pwd->pw_shell = _PATH_BSHELL;
617 	if (*shell == '\0')   /* Not overridden */
618 		shell = pwd->pw_shell;
619 	if ((shell = strdup(shell)) == NULL) {
620 		syslog(LOG_NOTICE, "memory allocation error");
621 		sleepexit(1);
622 	}
623 
624 #ifdef LOGIN_ACCESS
625 	if (login_access(pwd->pw_name, hostname ? full_hostname : tty) == 0)
626 		refused("Permission denied", "ACCESS", 1);
627 #endif /* LOGIN_ACCESS */
628 
629 	/* Nothing else left to fail -- really log in. */
630 	memset((void *)&utmp, 0, sizeof(utmp));
631 	(void)time(&utmp.ut_time);
632 	(void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
633 	if (hostname)
634 		(void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
635 	(void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
636 	login(&utmp);
637 
638 	dolastlog(quietlog);
639 
640 	/*
641 	 * Set device protections, depending on what terminal the
642 	 * user is logged in. This feature is used on Suns to give
643 	 * console users better privacy.
644 	 */
645 	login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
646 
647 	(void)chown(ttyn, pwd->pw_uid,
648 		    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
649 
650 	/*
651 	 * Preserve TERM if it happens to be already set.
652 	 */
653 	if ((term = getenv("TERM")) != NULL)
654 		term = strdup(term);
655 
656 	/*
657 	 * Exclude cons/vt/ptys only, assume dialup otherwise
658 	 * TODO: Make dialup tty determination a library call
659 	 * for consistency (finger etc.)
660 	 */
661 	if (hostname==NULL && strchr("vpqstPQST", tty[sizeof("tty")-1]) == NULL)
662 		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
663 
664 #ifdef KERBEROS
665 	if (!quietlog && notickets == 1 && !noticketsdontcomplain)
666 		(void)printf("Warning: no Kerberos tickets issued.\n");
667 #endif
668 
669 #ifdef LOGALL
670 	/*
671 	 * Syslog each successful login, so we don't have to watch hundreds
672 	 * of wtmp or lastlogin files.
673 	 */
674 	if (hostname)
675 		syslog(LOG_INFO, "login from %s on %s as %s",
676 		       full_hostname, tty, pwd->pw_name);
677 	else
678 		syslog(LOG_INFO, "login on %s as %s",
679 		       tty, pwd->pw_name);
680 #endif
681 
682 	/*
683 	 * If fflag is on, assume caller/authenticator has logged root login.
684 	 */
685 	if (rootlogin && fflag == 0)
686 	{
687 		if (hostname)
688 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
689 			       username, tty, full_hostname);
690 		else
691 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
692 			       username, tty);
693 	}
694 
695 	/*
696 	 * Destroy environment unless user has requested its preservation.
697 	 * We need to do this before setusercontext() because that may
698 	 * set or reset some environment variables.
699 	 */
700 	if (!pflag)
701 		environ = envinit;
702 
703 	/*
704 	 * We don't need to be root anymore, so
705 	 * set the user and session context
706 	 */
707 #ifdef LOGIN_CAP
708 	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
709                 syslog(LOG_ERR, "setusercontext() failed - exiting");
710 		exit(1);
711 	}
712 #else
713      	if (setlogin(pwd->pw_name) < 0)
714                 syslog(LOG_ERR, "setlogin() failure: %m");
715 
716 	(void)setgid(pwd->pw_gid);
717 	initgroups(username, pwd->pw_gid);
718 	(void)setuid(rootlogin ? 0 : pwd->pw_uid);
719 #endif
720 
721 	(void)setenv("SHELL", pwd->pw_shell, 1);
722 	(void)setenv("HOME", pwd->pw_dir, 1);
723 	if (term != NULL && *term != '\0')
724 		(void)setenv("TERM", term, 1);	/* Preset overrides */
725 	else {
726 		(void)setenv("TERM", stypeof(tty), 0);	/* Fallback doesn't */
727 	}
728 	(void)setenv("LOGNAME", pwd->pw_name, 1);
729 	(void)setenv("USER", pwd->pw_name, 1);
730 	(void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
731 #ifdef KERBEROS
732 	if (krbtkfile_env)
733 		(void)setenv("KRBTKFILE", krbtkfile_env, 1);
734 #endif
735 #if LOGIN_CAP_AUTH
736 	auth_env();
737 #endif
738 
739 #ifdef LOGIN_CAP
740 	if (!quietlog) {
741 		char	*cw;
742 
743 		cw = login_getcapstr(lc, "copyright", NULL, NULL);
744 		if (cw != NULL && access(cw, F_OK) == 0)
745 			motd(cw);
746 		else
747 		    (void)printf("%s\n\t%s %s\n",
748 	"Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
749 	"The Regents of the University of California. ",
750 	"All rights reserved.");
751 
752 		(void)printf("\n");
753 
754 		cw = login_getcapstr(lc, "welcome", NULL, NULL);
755 		if (cw == NULL || access(cw, F_OK) != 0)
756 			cw = _PATH_MOTDFILE;
757 		motd(cw);
758 
759 		cw = getenv("MAIL");	/* $MAIL may have been set by class */
760 		if (cw != NULL) {
761 			strncpy(tbuf, cw, sizeof(tbuf));
762 			tbuf[sizeof(tbuf)-1] = '\0';
763 		} else
764 			snprintf(tbuf, sizeof(tbuf), "%s/%s",
765 				 _PATH_MAILDIR, pwd->pw_name);
766 #else
767 	if (!quietlog) {
768 		    (void)printf("%s\n\t%s %s\n",
769 	"Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
770 	"The Regents of the University of California. ",
771 	"All rights reserved.");
772 		motd(_PATH_MOTDFILE);
773 		snprintf(tbuf, sizeof(tbuf), "%s/%s",
774 			 _PATH_MAILDIR, pwd->pw_name);
775 #endif
776 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
777 			(void)printf("You have %smail.\n",
778 				     (st.st_mtime > st.st_atime) ? "new " : "");
779 	}
780 
781 #ifdef LOGIN_CAP
782 	login_close(lc);
783 #endif
784 
785 	(void)signal(SIGALRM, SIG_DFL);
786 	(void)signal(SIGQUIT, SIG_DFL);
787 	(void)signal(SIGINT, SIG_DFL);
788 	(void)signal(SIGTSTP, SIG_IGN);
789 
790 	if (changepass) {
791 		if (system(_PATH_CHPASS) != 0)
792 			sleepexit(1);
793 	}
794 
795 	/*
796 	 * Login shells have a leading '-' in front of argv[0]
797 	 */
798 	tbuf[0] = '-';
799 	(void)strcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ? p + 1 : pwd->pw_shell);
800 
801 	execlp(shell, tbuf, 0);
802 	err(1, "%s", shell);
803 }
804 
805 
806 /*
807  * Allow for authentication style and/or kerberos instance
808  * */
809 
810 #define	NBUFSIZ		UT_NAMESIZE + 64
811 
812 void
813 getloginname()
814 {
815 	int ch;
816 	char *p;
817 	static char nbuf[NBUFSIZ];
818 
819 	for (;;) {
820 		(void)printf("login: ");
821 		for (p = nbuf; (ch = getchar()) != '\n'; ) {
822 			if (ch == EOF) {
823 				badlogin(username);
824 				exit(0);
825 			}
826 			if (p < nbuf + (NBUFSIZ - 1))
827 				*p++ = ch;
828 		}
829 		if (p > nbuf)
830 			if (nbuf[0] == '-')
831 				(void)fprintf(stderr,
832 				    "login names may not start with '-'.\n");
833 			else {
834 				*p = '\0';
835 				username = nbuf;
836 				break;
837 			}
838 	}
839 }
840 
841 int
842 rootterm(ttyn)
843 	char *ttyn;
844 {
845 	struct ttyent *t;
846 
847 	return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
848 }
849 
850 volatile int motdinterrupt;
851 
852 /* ARGSUSED */
853 void
854 sigint(signo)
855 	int signo;
856 {
857 	motdinterrupt = 1;
858 }
859 
860 void
861 motd(motdfile)
862 	char *motdfile;
863 {
864 	int fd, nchars;
865 	sig_t oldint;
866 	char tbuf[256];
867 
868 	if ((fd = open(motdfile, O_RDONLY, 0)) < 0)
869 		return;
870 	motdinterrupt = 0;
871 	oldint = signal(SIGINT, sigint);
872 	while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0 && !motdinterrupt)
873 		(void)write(fileno(stdout), tbuf, nchars);
874 	(void)signal(SIGINT, oldint);
875 	(void)close(fd);
876 }
877 
878 /* ARGSUSED */
879 void
880 timedout(signo)
881 	int signo;
882 {
883 	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
884 	exit(0);
885 }
886 
887 #ifndef LOGIN_CAP
888 void
889 checknologin()
890 {
891 	int fd, nchars;
892 	char tbuf[8192];
893 
894 	if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
895 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
896 			(void)write(fileno(stdout), tbuf, nchars);
897 		sleepexit(0);
898 	}
899 }
900 #endif
901 
902 void
903 dolastlog(quiet)
904 	int quiet;
905 {
906 	struct lastlog ll;
907 	int fd;
908 
909 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
910 		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
911 		if (!quiet) {
912 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
913 			    ll.ll_time != 0) {
914 				(void)printf("Last login: %.*s ",
915 				    24-5, (char *)ctime(&ll.ll_time));
916 				if (*ll.ll_host != '\0')
917 					(void)printf("from %.*s\n",
918 					    (int)sizeof(ll.ll_host),
919 					    ll.ll_host);
920 				else
921 					(void)printf("on %.*s\n",
922 					    (int)sizeof(ll.ll_line),
923 					    ll.ll_line);
924 			}
925 			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
926 		}
927 		memset((void *)&ll, 0, sizeof(ll));
928 		(void)time(&ll.ll_time);
929 		(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
930 		if (hostname)
931 			(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
932 		(void)write(fd, (char *)&ll, sizeof(ll));
933 		(void)close(fd);
934 	}
935 }
936 
937 void
938 badlogin(name)
939 	char *name;
940 {
941 
942 	if (failures == 0)
943 		return;
944 	if (hostname) {
945 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
946 		    failures, failures > 1 ? "S" : "", full_hostname);
947 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
948 		    "%d LOGIN FAILURE%s FROM %s, %s",
949 		    failures, failures > 1 ? "S" : "", full_hostname, name);
950 	} else {
951 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
952 		    failures, failures > 1 ? "S" : "", tty);
953 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
954 		    "%d LOGIN FAILURE%s ON %s, %s",
955 		    failures, failures > 1 ? "S" : "", tty, name);
956 	}
957 }
958 
959 #undef	UNKNOWN
960 #define	UNKNOWN	"su"
961 
962 char *
963 stypeof(ttyid)
964 	char *ttyid;
965 {
966 
967 	struct ttyent *t;
968 
969 	return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
970 }
971 
972 void
973 refused(msg, rtype, lout)
974 	char *msg;
975 	char *rtype;
976 	int lout;
977 {
978 
979 	if (msg != NULL)
980 	    printf("%s.\n", msg);
981 	if (hostname)
982 		syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s",
983 		       pwd->pw_name, rtype, full_hostname, tty);
984 	else
985 		syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) ON TTY %s",
986 		       pwd->pw_name, rtype, tty);
987 	if (lout)
988 		sleepexit(1);
989 }
990 
991 void
992 sleepexit(eval)
993 	int eval;
994 {
995 
996 	(void)sleep(5);
997 	exit(eval);
998 }
999