xref: /freebsd/usr.bin/login/login.c (revision d8a7b3477f660316e69dcf3c0cbc93cf71c9a3d0)
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 				/* pretend password okay */
354 				rval = 0;
355 				goto ttycheck;
356 			}
357 		}
358 
359 		fflag = 0;
360 
361 		(void)setpriority(PRIO_PROCESS, 0, -4);
362 
363 #ifdef LOGIN_CAP_AUTH
364 		/*
365 		 * This hands off authorization to an authorization program,
366 		 * depending on the styles available for the "auth-login",
367 		 * auth-rlogin (or default) authorization styles.
368 		 * We do this regardless of whether an account exists so that
369 		 * the remote user cannot tell a "real" from an invented
370 		 * account name. If we don't have an account we just fall
371 		 * back to the first method for the "default" class.
372 		 */
373 		if (!(style = login_getstyle(lc, auth_method, authtype))) {
374 
375 			/*
376 			 * No available authorization method
377 			 */
378 			rval = 1;
379 			(void)printf("No auth method available for %s.\n",
380 				     authtype);
381 		} else {
382 
383 			/*
384 			 * Put back the kerberos instance, if any was given.
385 			 * Don't worry about the non-kerberos case here, since
386 			 * if kerberos is not available or not selected and an
387 			 * instance is given at the login prompt, su or rlogin -l,
388 			 * then anything else should fail as well.
389 			 */
390 			if (*instance)
391 				*(instance - 1) = '.';
392 
393 			rval = authenticate(username,
394 					    lc ? lc->lc_class : "default",
395 					    style, authtype);
396 			/* Junk it again */
397 			if (*instance)
398 				*(instance - 1) = '\0';
399 		}
400 
401 		if (!rval) {
402 			char * approvp;
403 
404 			/*
405 			 * If authentication succeeds, run any approval
406 			 * program, if applicable for this class.
407 			 */
408 			approvep = login_getcapstr(lc, "approve", NULL, NULL);
409 			rval = 1; /* Assume bad login again */
410 
411 			if (approvep==NULL ||
412 			    auth_script(approvep, approvep, username,
413 					lc->lc_class, 0) == 0) {
414 				int     r;
415 
416 				r = auth_scan(AUTH_OKAY);
417 				/*
418 				 * See what the authorize program says
419 				 */
420 				if (r != AUTH_NONE) {
421 					rval = 0;
422 
423 					if (!rootok && (r & AUTH_ROOTOKAY))
424 						rootok = 1; /* root approved */
425 					else
426 						rootlogin = 0;
427 
428 					if (!authok && (r & AUTH_SECURE))
429 						authok = 1; /* secure */
430 				}
431 			}
432 		}
433 #else /* !LOGIN_CAP_AUTH */
434 #ifdef SKEY
435 		permit_passwd = skeyaccess(username, tty,
436 					   hostname ? full_hostname : NULL,
437 					   NULL);
438 		p = skey_getpass("Password:", pwd, permit_passwd);
439 		ep = skey_crypt(p, salt, pwd, permit_passwd);
440 #else /* !SKEY */
441 		p = getpass("Password:");
442 		ep = crypt(p, salt);
443 #endif/* SKEY */
444 
445 		if (pwd) {
446 #ifdef KERBEROS
447 #ifdef SKEY
448 			/*
449 			 * Do not allow user to type in kerberos password
450 			 * over the net (actually, this is ok for encrypted
451 			 * links, but we have no way of determining if the
452 			 * link is encrypted.
453 			 */
454 			if (!permit_passwd) {
455 				rval = 1;		/* failed */
456 			} else
457 #endif /* SKEY */
458 			rval = klogin(pwd, instance, localhost, p);
459 			if (rval != 0 && rootlogin && pwd->pw_uid != 0)
460 				rootlogin = 0;
461 			if (rval == 0)
462 				authok = 1; /* kerberos authenticated ok */
463 			else if (rval == 1) /* fallback to unix passwd */
464 				rval = strcmp(ep, pwd->pw_passwd);
465 #else /* !KERBEROS */
466 			rval = strcmp(ep, pwd->pw_passwd);
467 #endif /* KERBEROS */
468 		}
469 
470 		/* clear entered password */
471 		memset(p, 0, strlen(p));
472 #endif /* LOGIN_CAP_AUTH */
473 
474 		(void)setpriority(PRIO_PROCESS, 0, 0);
475 
476 #ifdef LOGIN_CAP
477 		if (rval)
478 			auth_rmfiles();
479 #endif
480 	ttycheck:
481 		/*
482 		 * If trying to log in as root without Kerberos,
483 		 * but with insecure terminal, refuse the login attempt.
484 		 */
485 		if (pwd && !rval) {
486 #if defined(KERBEROS) || defined(LOGIN_CAP_AUTH)
487 			if (authok == 0 && rootlogin && !rootok)
488 #else
489 			if (rootlogin && !rootok)
490 #endif
491 				refused(NULL, "NOROOT", 0);
492 			else	/* valid password & authenticated */
493 				break;
494 		}
495 
496 		(void)printf("Login incorrect\n");
497 		failures++;
498 
499 		/*
500 		 * we allow up to 'retry' (10) tries,
501 		 * but after 'backoff' (3) we start backing off
502 		 */
503 		if (++cnt > backoff) {
504 			if (cnt >= retries) {
505 				badlogin(username);
506 				sleepexit(1);
507 			}
508 			sleep((u_int)((cnt - 3) * 5));
509 		}
510 	}
511 
512 	/* committed to login -- turn off timeout */
513 	(void)alarm((u_int)0);
514 
515 	endpwent();
516 
517 	/* if user not super-user, check for disabled logins */
518 #ifdef LOGIN_CAP
519 	if (!rootlogin)
520 		auth_checknologin(lc);
521 #else
522 	if (!rootlogin)
523 		checknologin();
524 #endif
525 
526 #ifdef LOGIN_CAP
527 	quietlog = login_getcapbool(lc, "hushlogin", 0);
528 #else
529 	quietlog = 0;
530 #endif
531 	if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) {
532 #ifdef LOGIN_CAP
533 		if (login_getcapbool(lc, "requirehome", !rootlogin))
534 			refused("Home directory not available", "HOMEDIR", 1);
535 #endif
536 		if (chdir("/") < 0) {
537 			refused("Cannot find root directory", "ROOTDIR", 1);
538 		pwd->pw_dir = "/";
539 		if (!quietlog || *pwd->pw_dir)
540 			printf("No home directory.\nLogging in with home = \"/\".\n");
541 	}
542 	if (!quietlog)
543 		quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
544 
545 	if (pwd->pw_change || pwd->pw_expire)
546 		(void)gettimeofday(&tp, (struct timezone *)NULL);
547 
548 #define DEFAULT_WARN  (2L * 7L & 86400L)  /* Two weeks */
549 
550 #ifdef LOGIN_CAP
551 	warntime = login_getcaptime(lc, "warnpassword",
552 				    DEFAULT_WARN, DEFAULT_WARN);
553 #else
554 	warntime = DEFAULT_WARN;
555 #endif
556 
557 	changepass=0;
558 	if (pwd->pw_change) {
559 		if (tp.tv_sec >= pwd->pw_change) {
560 			(void)printf("Sorry -- your password has expired.\n");
561 			changepass=1;
562 			syslog(LOG_INFO,
563 			       "%s Password expired - forcing change",
564 			       pwd->pw_name);
565 		} else if (pwd->pw_change - tp.tv_sec < warntime && !quietlog)
566 		    (void)printf("Warning: your password expires on %s",
567 				 ctime(&pwd->pw_change));
568 		}
569 	}
570 
571 #ifdef LOGIN_CAP
572 	warntime = login_getcaptime(lc, "warnexpire",
573 				    DEFAULT_WARN, DEFAULT_WARN);
574 #else
575 	warntime = DEFAULT_WARN;
576 #endif
577 
578 	if (pwd->pw_expire) {
579 		if (tp.tv_sec >= pwd->pw_expire) {
580 			refused("Sorry -- your account has expired",
581 				"EXPIRED", 1);
582 		} else if (pwd->pw_expire - tp.tv_sec < warntime && !quietlog)
583 		    (void)printf("Warning: your account expires on %s",
584 				 ctime(&pwd->pw_expire));
585 	}
586 
587 #ifdef LOGIN_CAP
588 	if (lc != NULL) {
589 		if (hostname) {
590 			struct hostent *hp = gethostbyname(full_hostname);
591 
592 			if (hp == NULL)
593 				optarg = NULL;
594 			else {
595 				struct in_addr in;
596 				memmove(&in, hp->h_addr, sizeof(in));
597 				optarg = strdup(inet_ntoa(in));
598 			}
599 			if (!auth_hostok(lc, full_hostname, optarg))
600 				refused("Permission denied", "HOST", 1);
601 		}
602 
603 		if (!auth_ttyok(lc, tty))
604 			refused("Permission denied", "TTY", 1);
605 
606 		if (!auth_timeok(lc, time(NULL)))
607 			refused("Logins not available right now", "TIME", 1);
608 	}
609         shell=login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
610 #else /* !LOGIN_CAP */
611        shell=pwd->pw_shell;
612 #endif /* LOGIN_CAP */
613 	if (*pwd->pw_shell == '\0')
614 		pwd->pw_shell = _PATH_BSHELL;
615 	if (*shell == '\0')   /* Not overridden */
616 		shell = pwd->pw_shell;
617 	if ((shell = strdup(shell)) == NULL) {
618 		syslog(LOG_NOTICE, "memory allocation error");
619 		sleepexit(1);
620 	}
621 
622 #ifdef LOGIN_ACCESS
623 	if (login_access(pwd->pw_name, hostname ? full_hostname : tty) == 0)
624 		refused("Permission denied", "ACCESS", 1);
625 #endif /* LOGIN_ACCESS */
626 
627 	/* Nothing else left to fail -- really log in. */
628 	memset((void *)&utmp, 0, sizeof(utmp));
629 	(void)time(&utmp.ut_time);
630 	(void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
631 	if (hostname)
632 		(void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
633 	(void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
634 	login(&utmp);
635 
636 	dolastlog(quietlog);
637 
638 	/*
639 	 * Set device protections, depending on what terminal the
640 	 * user is logged in. This feature is used on Suns to give
641 	 * console users better privacy.
642 	 */
643 	login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
644 
645 	(void)chown(ttyn, pwd->pw_uid,
646 		    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
647 
648 	/*
649 	 * Preserve TERM if it happens to be already set.
650 	 */
651 	if ((term = getenv("TERM")) != NULL)
652 		term = strdup(term);
653 
654 	/*
655 	 * Exclude cons/vt/ptys only, assume dialup otherwise
656 	 * TODO: Make dialup tty determination a library call
657 	 * for consistency (finger etc.)
658 	 */
659 	if (hostname==NULL && strchr("vpqstPQST", tty[sizeof("tty")-1]) == NULL)
660 		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
661 
662 #ifdef KERBEROS
663 	if (!quietlog && notickets == 1 && !noticketsdontcomplain)
664 		(void)printf("Warning: no Kerberos tickets issued.\n");
665 #endif
666 
667 #ifdef LOGALL
668 	/*
669 	 * Syslog each successful login, so we don't have to watch hundreds
670 	 * of wtmp or lastlogin files.
671 	 */
672 	if (hostname)
673 		syslog(LOG_INFO, "login from %s on %s as %s",
674 		       full_hostname, tty, pwd->pw_name);
675 	else
676 		syslog(LOG_INFO, "login on %s as %s",
677 		       tty, pwd->pw_name);
678 #endif
679 
680 	/*
681 	 * If fflag is on, assume caller/authenticator has logged root login.
682 	 */
683 	if (rootlogin && fflag == 0)
684 	{
685 		if (hostname)
686 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
687 			       username, tty, full_hostname);
688 		else
689 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
690 			       username, tty);
691 	}
692 
693 	/*
694 	 * Destroy environment unless user has requested its preservation.
695 	 * We need to do this before setusercontext() because that may
696 	 * set or reset some environment variables.
697 	 */
698 	if (!pflag)
699 		environ = envinit;
700 
701 	/*
702 	 * We don't need to be root anymore, so
703 	 * set the user and session context
704 	 */
705 #ifdef LOGIN_CAP
706 	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
707                 syslog(LOG_ERR, "setusercontext() failed - exiting");
708 		exit(1);
709 	}
710 #else
711      	if (setlogin(pwd->pw_name) < 0)
712                 syslog(LOG_ERR, "setlogin() failure: %m");
713 
714 	(void)setgid(pwd->pw_gid);
715 	initgroups(username, pwd->pw_gid);
716 	(void)setuid(rootlogin ? 0 : pwd->pw_uid);
717 #endif
718 
719 	(void)setenv("SHELL", pwd->pw_shell, 1);
720 	(void)setenv("HOME", pwd->pw_dir, 1);
721 	if (term != NULL && *term != '\0')
722 		(void)setenv("TERM", term, 1);	/* Preset overrides */
723 	else {
724 		(void)setenv("TERM", stypeof(tty), 0);	/* Fallback doesn't */
725 	}
726 	(void)setenv("LOGNAME", pwd->pw_name, 1);
727 	(void)setenv("USER", pwd->pw_name, 1);
728 	(void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
729 #ifdef KERBEROS
730 	if (krbtkfile_env)
731 		(void)setenv("KRBTKFILE", krbtkfile_env, 1);
732 #endif
733 #if LOGIN_CAP_AUTH
734 	auth_env();
735 #endif
736 
737 #ifdef LOGIN_CAP
738 	if (!quietlog) {
739 		char	*cw;
740 
741 		cw = login_getcapstr(lc, "copyright", NULL, NULL);
742 		if (cw != NULL && access(cw, F_OK) == 0)
743 			motd(cw);
744 		else
745 		    (void)printf("%s\n\t%s %s\n",
746 	"Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
747 	"The Regents of the University of California. ",
748 	"All rights reserved.");
749 
750 		(void)printf("\n");
751 
752 		cw = login_getcapstr(lc, "welcome", NULL, NULL);
753 		if (cw == NULL || access(cw, F_OK) != 0)
754 			cw = _PATH_MOTDFILE;
755 		motd(cw);
756 
757 		cw = getenv("MAIL");	/* $MAIL may have been set by class */
758 		if (cw != NULL) {
759 			strncpy(tbuf, cw, sizeof(tbuf));
760 			tbuf[sizeof(tbuf)-1] = '\0';
761 		} else
762 			snprintf(tbuf, sizeof(tbuf), "%s/%s",
763 				 _PATH_MAILDIR, pwd->pw_name);
764 #else
765 	if (!quietlog) {
766 		    (void)printf("%s\n\t%s %s\n",
767 	"Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
768 	"The Regents of the University of California. ",
769 	"All rights reserved.");
770 		motd(_PATH_MOTDFILE);
771 		snprintf(tbuf, sizeof(tbuf), "%s/%s",
772 			 _PATH_MAILDIR, pwd->pw_name);
773 #endif
774 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
775 			(void)printf("You have %smail.\n",
776 				     (st.st_mtime > st.st_atime) ? "new " : "");
777 	}
778 
779 #ifdef LOGIN_CAP
780 	login_close(lc);
781 #endif
782 
783 	(void)signal(SIGALRM, SIG_DFL);
784 	(void)signal(SIGQUIT, SIG_DFL);
785 	(void)signal(SIGINT, SIG_DFL);
786 	(void)signal(SIGTSTP, SIG_IGN);
787 
788 	if (changepass) {
789 		if (system(_PATH_CHPASS) != 0)
790 			sleepexit(1);
791 	}
792 
793 	/*
794 	 * Login shells have a leading '-' in front of argv[0]
795 	 */
796 	tbuf[0] = '-';
797 	(void)strcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ? p + 1 : pwd->pw_shell);
798 
799 	execlp(shell, tbuf, 0);
800 	err(1, "%s", shell);
801 }
802 
803 
804 /*
805  * Allow for authentication style and/or kerberos instance
806  * */
807 
808 #define	NBUFSIZ		UT_NAMESIZE + 64
809 
810 void
811 getloginname()
812 {
813 	int ch;
814 	char *p;
815 	static char nbuf[NBUFSIZ];
816 
817 	for (;;) {
818 		(void)printf("login: ");
819 		for (p = nbuf; (ch = getchar()) != '\n'; ) {
820 			if (ch == EOF) {
821 				badlogin(username);
822 				exit(0);
823 			}
824 			if (p < nbuf + (NBUFSIZ - 1))
825 				*p++ = ch;
826 		}
827 		if (p > nbuf)
828 			if (nbuf[0] == '-')
829 				(void)fprintf(stderr,
830 				    "login names may not start with '-'.\n");
831 			else {
832 				*p = '\0';
833 				username = nbuf;
834 				break;
835 			}
836 	}
837 }
838 
839 int
840 rootterm(ttyn)
841 	char *ttyn;
842 {
843 	struct ttyent *t;
844 
845 	return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
846 }
847 
848 volatile int motdinterrupt;
849 
850 /* ARGSUSED */
851 void
852 sigint(signo)
853 	int signo;
854 {
855 	motdinterrupt = 1;
856 }
857 
858 void
859 motd(motdfile)
860 	char *motdfile;
861 {
862 	int fd, nchars;
863 	sig_t oldint;
864 	char tbuf[256];
865 
866 	if ((fd = open(motdfile, O_RDONLY, 0)) < 0)
867 		return;
868 	motdinterrupt = 0;
869 	oldint = signal(SIGINT, sigint);
870 	while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0 && !motdinterrupt)
871 		(void)write(fileno(stdout), tbuf, nchars);
872 	(void)signal(SIGINT, oldint);
873 	(void)close(fd);
874 }
875 
876 /* ARGSUSED */
877 void
878 timedout(signo)
879 	int signo;
880 {
881 	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
882 	exit(0);
883 }
884 
885 #ifndef LOGIN_CAP
886 void
887 checknologin()
888 {
889 	int fd, nchars;
890 	char tbuf[8192];
891 
892 	if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
893 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
894 			(void)write(fileno(stdout), tbuf, nchars);
895 		sleepexit(0);
896 	}
897 }
898 #endif
899 
900 void
901 dolastlog(quiet)
902 	int quiet;
903 {
904 	struct lastlog ll;
905 	int fd;
906 
907 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
908 		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
909 		if (!quiet) {
910 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
911 			    ll.ll_time != 0) {
912 				(void)printf("Last login: %.*s ",
913 				    24-5, (char *)ctime(&ll.ll_time));
914 				if (*ll.ll_host != '\0')
915 					(void)printf("from %.*s\n",
916 					    (int)sizeof(ll.ll_host),
917 					    ll.ll_host);
918 				else
919 					(void)printf("on %.*s\n",
920 					    (int)sizeof(ll.ll_line),
921 					    ll.ll_line);
922 			}
923 			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
924 		}
925 		memset((void *)&ll, 0, sizeof(ll));
926 		(void)time(&ll.ll_time);
927 		(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
928 		if (hostname)
929 			(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
930 		(void)write(fd, (char *)&ll, sizeof(ll));
931 		(void)close(fd);
932 	}
933 }
934 
935 void
936 badlogin(name)
937 	char *name;
938 {
939 
940 	if (failures == 0)
941 		return;
942 	if (hostname) {
943 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
944 		    failures, failures > 1 ? "S" : "", full_hostname);
945 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
946 		    "%d LOGIN FAILURE%s FROM %s, %s",
947 		    failures, failures > 1 ? "S" : "", full_hostname, name);
948 	} else {
949 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
950 		    failures, failures > 1 ? "S" : "", tty);
951 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
952 		    "%d LOGIN FAILURE%s ON %s, %s",
953 		    failures, failures > 1 ? "S" : "", tty, name);
954 	}
955 }
956 
957 #undef	UNKNOWN
958 #define	UNKNOWN	"su"
959 
960 char *
961 stypeof(ttyid)
962 	char *ttyid;
963 {
964 
965 	struct ttyent *t;
966 
967 	return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
968 }
969 
970 void
971 refused(msg, rtype, lout)
972 	char *msg;
973 	char *rtype;
974 	int lout;
975 {
976 
977 	if (msg != NULL)
978 	    printf("%s.\n", msg);
979 	if (hostname)
980 		syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s",
981 		       pwd->pw_name, rtype, full_hostname, tty);
982 	else
983 		syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) ON TTY %s",
984 		       pwd->pw_name, rtype, tty);
985 	if (lout)
986 		sleepexit(1);
987 }
988 
989 void
990 sleepexit(eval)
991 	int eval;
992 {
993 
994 	(void)sleep(5);
995 	exit(eval);
996 }
997