xref: /dragonfly/usr.bin/login/login.c (revision c9f721c2)
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  * @(#)login.c	8.4 (Berkeley) 4/2/94
34  * $FreeBSD: src/usr.bin/login/login.c,v 1.51.2.15 2003/04/29 14:10:41 des Exp $
35  * $DragonFly: src/usr.bin/login/login.c,v 1.4 2003/11/03 19:31:30 eirikn Exp $
36  */
37 
38 #if 0
39 static char copyright[] =
40 "@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\
41 	The Regents of the University of California.  All rights reserved.\n";
42 #endif
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/socket.h>
54 #include <sys/time.h>
55 #include <sys/resource.h>
56 #include <sys/file.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
59 
60 #include <err.h>
61 #include <errno.h>
62 #include <grp.h>
63 #include <libutil.h>
64 #include <login_cap.h>
65 #include <netdb.h>
66 #include <pwd.h>
67 #include <setjmp.h>
68 #include <signal.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <syslog.h>
73 #include <ttyent.h>
74 #include <unistd.h>
75 #include <utmp.h>
76 
77 #ifdef USE_PAM
78 #include <security/pam_appl.h>
79 #include <security/pam_misc.h>
80 #include <sys/wait.h>
81 #endif /* USE_PAM */
82 
83 #include "pathnames.h"
84 
85 /* wrapper for KAME-special getnameinfo() */
86 #ifndef NI_WITHSCOPEID
87 #define	NI_WITHSCOPEID	0
88 #endif
89 
90 void	 badlogin(char *);
91 void	 checknologin(void);
92 void	 dolastlog(int);
93 void	 getloginname(void);
94 void	 motd(char *);
95 int	 rootterm(char *);
96 void	 sigint(int);
97 void	 sleepexit(int);
98 void	 refused(char *,char *,int);
99 char	*stypeof(char *);
100 void	 timedout(int);
101 int	 login_access(char *, char *);
102 void     login_fbtab(char *, uid_t, gid_t);
103 
104 #ifdef USE_PAM
105 static int auth_pam(void);
106 static int export_pam_environment(void);
107 static int ok_to_export(const char *);
108 
109 static pam_handle_t *pamh = NULL;
110 static char **environ_pam;
111 
112 #define PAM_END { \
113 	if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) \
114 		syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); \
115 	if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS) \
116 		syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e)); \
117 	if ((e = pam_end(pamh, e)) != PAM_SUCCESS) \
118 		syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); \
119 }
120 #endif /* USE_PAM */
121 static int auth_traditional(void);
122 extern void login(struct utmp *);
123 static void usage(void);
124 
125 #define	TTYGRPNAME	"tty"		/* name of group to own ttys */
126 #define	DEFAULT_BACKOFF	3
127 #define	DEFAULT_RETRIES	10
128 #define	DEFAULT_PROMPT		"login: "
129 #define	DEFAULT_PASSWD_PROMPT	"Password:"
130 
131 /*
132  * This bounds the time given to login.  Not a define so it can
133  * be patched on machines where it's too small.
134  */
135 u_int	timeout = 300;
136 
137 /* Buffer for signal handling of timeout */
138 jmp_buf timeout_buf;
139 
140 struct	passwd *pwd;
141 int	failures;
142 char	*term, *envinit[1], *hostname, *passwd_prompt, *prompt, *tty, *username;
143 char    full_hostname[MAXHOSTNAMELEN];
144 
145 int
146 main(int argc, char **argv)
147 {
148 	extern char **environ;
149 	struct group *gr;
150 	struct stat st;
151 	struct timeval tp;
152 	struct utmp utmp;
153 	int rootok, retries, backoff;
154 	int ask, ch, cnt, fflag, hflag, pflag, quietlog, rootlogin, rval;
155 	int changepass;
156 	time_t warntime;
157 	uid_t uid, euid;
158 	gid_t egid;
159 	char *p, *ttyn;
160 	char tbuf[MAXPATHLEN + 2];
161 	char tname[sizeof(_PATH_TTY) + 10];
162 	char *shell = NULL;
163 	login_cap_t *lc = NULL;
164 #ifdef USE_PAM
165 	pid_t pid;
166 	int e;
167 #endif /* USE_PAM */
168 
169 	(void)signal(SIGQUIT, SIG_IGN);
170 	(void)signal(SIGINT, SIG_IGN);
171 	(void)signal(SIGHUP, SIG_IGN);
172 	if (setjmp(timeout_buf)) {
173 		if (failures)
174 			badlogin(tbuf);
175 		(void)fprintf(stderr, "Login timed out after %d seconds\n",
176 		    timeout);
177 		exit(0);
178 	}
179 	(void)signal(SIGALRM, timedout);
180 	(void)alarm(timeout);
181 	(void)setpriority(PRIO_PROCESS, 0, 0);
182 
183 	openlog("login", LOG_ODELAY, LOG_AUTH);
184 
185 	/*
186 	 * -p is used by getty to tell login not to destroy the environment
187 	 * -f is used to skip a second login authentication
188 	 * -h is used by other servers to pass the name of the remote
189 	 *    host to login so that it may be placed in utmp and wtmp
190 	 */
191 	*full_hostname = '\0';
192 	term = NULL;
193 
194 	fflag = hflag = pflag = 0;
195 	uid = getuid();
196 	euid = geteuid();
197 	egid = getegid();
198 	while ((ch = getopt(argc, argv, "fh:p")) != -1)
199 		switch (ch) {
200 		case 'f':
201 			fflag = 1;
202 			break;
203 		case 'h':
204 			if (uid)
205 				errx(1, "-h option: %s", strerror(EPERM));
206 			hflag = 1;
207 			if (strlcpy(full_hostname, optarg,
208 			    sizeof(full_hostname)) >= sizeof(full_hostname))
209 				errx(1, "-h option: %s: exceeds maximum "
210 				    "hostname size", optarg);
211 
212 			trimdomain(optarg, UT_HOSTSIZE);
213 
214 			if (strlen(optarg) > UT_HOSTSIZE) {
215 				struct addrinfo hints, *res;
216 				int ga_err;
217 
218 				memset(&hints, 0, sizeof(hints));
219 				hints.ai_family = AF_UNSPEC;
220 				ga_err = getaddrinfo(optarg, NULL, &hints,
221 				    &res);
222 				if (ga_err == 0) {
223 					char hostbuf[MAXHOSTNAMELEN];
224 
225 					getnameinfo(res->ai_addr,
226 					    res->ai_addrlen,
227 					    hostbuf,
228 					    sizeof(hostbuf), NULL, 0,
229 					    NI_NUMERICHOST|
230 					    NI_WITHSCOPEID);
231 					optarg = strdup(hostbuf);
232 					if (optarg == NULL) {
233 						syslog(LOG_NOTICE,
234 						    "strdup(): %m");
235 						sleepexit(1);
236 					}
237 				} else
238 					optarg = "invalid hostname";
239 				if (res != NULL)
240 					freeaddrinfo(res);
241 			}
242 			hostname = optarg;
243 			break;
244 		case 'p':
245 			pflag = 1;
246 			break;
247 		case '?':
248 		default:
249 			if (!uid)
250 				syslog(LOG_ERR, "invalid flag %c", ch);
251 			usage();
252 		}
253 	argc -= optind;
254 	argv += optind;
255 
256 	if (*argv) {
257 		username = *argv;
258 		ask = 0;
259 	} else
260 		ask = 1;
261 
262 	setproctitle("-%s", getprogname());
263 
264 	for (cnt = getdtablesize(); cnt > 2; cnt--)
265 		(void)close(cnt);
266 
267 	ttyn = ttyname(STDIN_FILENO);
268 	if (ttyn == NULL || *ttyn == '\0') {
269 		(void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
270 		ttyn = tname;
271 	}
272 	if ((tty = strrchr(ttyn, '/')) != NULL)
273 		++tty;
274 	else
275 		tty = ttyn;
276 
277 	/*
278 	 * Get "login-retries" & "login-backoff" from default class
279 	 */
280 	lc = login_getclass(NULL);
281 	prompt = login_getcapstr(lc, "login_prompt",
282 	    DEFAULT_PROMPT, DEFAULT_PROMPT);
283 	passwd_prompt = login_getcapstr(lc, "passwd_prompt",
284 	    DEFAULT_PASSWD_PROMPT, DEFAULT_PASSWD_PROMPT);
285 	retries = login_getcapnum(lc, "login-retries", DEFAULT_RETRIES,
286 	    DEFAULT_RETRIES);
287 	backoff = login_getcapnum(lc, "login-backoff", DEFAULT_BACKOFF,
288 	    DEFAULT_BACKOFF);
289 	login_close(lc);
290 	lc = NULL;
291 
292 	for (cnt = 0;; ask = 1) {
293 		if (ask) {
294 			fflag = 0;
295 			getloginname();
296 		}
297 		rootlogin = 0;
298 		rootok = rootterm(tty); /* Default (auth may change) */
299 
300 		if (strlen(username) > UT_NAMESIZE)
301 			username[UT_NAMESIZE] = '\0';
302 
303 		/*
304 		 * Note if trying multiple user names; log failures for
305 		 * previous user name, but don't bother logging one failure
306 		 * for nonexistent name (mistyped username).
307 		 */
308 		if (failures && strcmp(tbuf, username)) {
309 			if (failures > (pwd ? 0 : 1))
310 				badlogin(tbuf);
311 		}
312 		(void)strlcpy(tbuf, username, sizeof(tbuf));
313 
314 		pwd = getpwnam(username);
315 
316 		/*
317 		 * if we have a valid account name, and it doesn't have a
318 		 * password, or the -f option was specified and the caller
319 		 * is root or the caller isn't changing their uid, don't
320 		 * authenticate.
321 		 */
322 		if (pwd != NULL) {
323 			if (pwd->pw_uid == 0)
324 				rootlogin = 1;
325 
326 			if (fflag && (uid == (uid_t)0 ||
327 			    uid == (uid_t)pwd->pw_uid)) {
328 				/* already authenticated */
329 				break;
330 			} else if (pwd->pw_passwd[0] == '\0') {
331 				if (!rootlogin || rootok) {
332 					/* pretend password okay */
333 					rval = 0;
334 					goto ttycheck;
335 				}
336 			}
337 		}
338 
339 		fflag = 0;
340 
341 		(void)setpriority(PRIO_PROCESS, 0, -4);
342 
343 #ifdef USE_PAM
344 		/*
345 		 * Try to authenticate using PAM.  If a PAM system error
346 		 * occurs, perhaps because of a botched configuration,
347 		 * then fall back to using traditional Unix authentication.
348 		 */
349 		if ((rval = auth_pam()) == -1)
350 #endif /* USE_PAM */
351 			rval = auth_traditional();
352 
353 		(void)setpriority(PRIO_PROCESS, 0, 0);
354 
355 #ifdef USE_PAM
356 		/*
357 		 * PAM authentication may have changed "pwd" to the
358 		 * entry for the template user.  Check again to see if
359 		 * this is a root login after all.
360 		 */
361 		if (pwd != NULL && pwd->pw_uid == 0)
362 			rootlogin = 1;
363 #endif /* USE_PAM */
364 
365 	ttycheck:
366 		/*
367 		 * If trying to log in as root without Kerberos,
368 		 * but with insecure terminal, refuse the login attempt.
369 		 */
370 		if (pwd && !rval) {
371 			if (rootlogin && !rootok)
372 				refused(NULL, "NOROOT", 0);
373 			else	/* valid password & authenticated */
374 				break;
375 		}
376 
377 		(void)printf("Login incorrect\n");
378 		failures++;
379 
380 		/*
381 		 * we allow up to 'retry' (10) tries,
382 		 * but after 'backoff' (3) we start backing off
383 		 */
384 		if (++cnt > backoff) {
385 			if (cnt >= retries) {
386 				badlogin(username);
387 				sleepexit(1);
388 			}
389 			sleep((u_int)((cnt - backoff) * 5));
390 		}
391 	}
392 
393 	/* committed to login -- turn off timeout */
394 	(void)alarm((u_int)0);
395 	(void)signal(SIGHUP, SIG_DFL);
396 
397 	endpwent();
398 
399 	/*
400 	 * Establish the login class.
401 	 */
402 	lc = login_getpwclass(pwd);
403 
404 	/* if user not super-user, check for disabled logins */
405 	if (!rootlogin)
406 		auth_checknologin(lc);
407 
408 	quietlog = login_getcapbool(lc, "hushlogin", 0);
409 	/* Switching needed for NFS with root access disabled */
410 	(void)setegid(pwd->pw_gid);
411 	(void)seteuid(rootlogin ? 0 : pwd->pw_uid);
412 	if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) {
413 		if (login_getcapbool(lc, "requirehome", 0))
414 			refused("Home directory not available", "HOMEDIR", 1);
415 		if (chdir("/") < 0)
416 			refused("Cannot find root directory", "ROOTDIR", 1);
417 		if (!quietlog || *pwd->pw_dir)
418 			printf("No home directory.\nLogging in with home = \"/\".\n");
419 		pwd->pw_dir = "/";
420 	}
421 	(void)seteuid(euid);
422 	(void)setegid(egid);
423 	if (!quietlog)
424 		quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
425 
426 	if (pwd->pw_change || pwd->pw_expire)
427 		(void)gettimeofday(&tp, (struct timezone *)NULL);
428 
429 #define	DEFAULT_WARN  (2L * 7L * 86400L)  /* Two weeks */
430 
431 	warntime = login_getcaptime(lc, "warnexpire", DEFAULT_WARN,
432 	    DEFAULT_WARN);
433 
434 	if (pwd->pw_expire) {
435 		if (tp.tv_sec >= pwd->pw_expire) {
436 			refused("Sorry -- your account has expired", "EXPIRED",
437 			    1);
438 		} else if (pwd->pw_expire - tp.tv_sec < warntime && !quietlog)
439 			(void)printf("Warning: your account expires on %s",
440 			    ctime(&pwd->pw_expire));
441 	}
442 
443 	warntime = login_getcaptime(lc, "warnpassword", DEFAULT_WARN,
444 	    DEFAULT_WARN);
445 
446 	changepass = 0;
447 	if (pwd->pw_change) {
448 		if (tp.tv_sec >= pwd->pw_change) {
449 			(void)printf("Sorry -- your password has expired.\n");
450 			changepass = 1;
451 			syslog(LOG_INFO, "%s Password expired - forcing change",
452 			    pwd->pw_name);
453 		} else if (pwd->pw_change - tp.tv_sec < warntime && !quietlog)
454 			(void)printf("Warning: your password expires on %s",
455 			    ctime(&pwd->pw_change));
456 	}
457 
458 	if (lc != NULL) {
459 		if (hostname) {
460 			struct addrinfo hints, *res;
461 			int ga_err;
462 
463 			memset(&hints, 0, sizeof(hints));
464 			hints.ai_family = AF_UNSPEC;
465 			ga_err = getaddrinfo(full_hostname, NULL, &hints,
466 					     &res);
467 			if (ga_err == 0) {
468 				char hostbuf[MAXHOSTNAMELEN];
469 
470 				getnameinfo(res->ai_addr, res->ai_addrlen,
471 				    hostbuf, sizeof(hostbuf), NULL, 0,
472 				    NI_NUMERICHOST|NI_WITHSCOPEID);
473 				if ((optarg = strdup(hostbuf)) == NULL) {
474 					syslog(LOG_NOTICE, "strdup(): %m");
475 					sleepexit(1);
476 				}
477 			} else
478 				optarg = NULL;
479 			if (res != NULL)
480 				freeaddrinfo(res);
481 			if (!auth_hostok(lc, full_hostname, optarg))
482 				refused("Permission denied", "HOST", 1);
483 		}
484 
485 		if (!auth_ttyok(lc, tty))
486 			refused("Permission denied", "TTY", 1);
487 
488 		if (!auth_timeok(lc, time(NULL)))
489 			refused("Logins not available right now", "TIME", 1);
490 	}
491         shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
492 	if (*pwd->pw_shell == '\0')
493 		pwd->pw_shell = _PATH_BSHELL;
494 	if (*shell == '\0')   /* Not overridden */
495 		shell = pwd->pw_shell;
496 	if ((shell = strdup(shell)) == NULL) {
497 		syslog(LOG_NOTICE, "strdup(): %m");
498 		sleepexit(1);
499 	}
500 
501 #ifdef LOGIN_ACCESS
502 	if (login_access(pwd->pw_name, hostname ? full_hostname : tty) == 0)
503 		refused("Permission denied", "ACCESS", 1);
504 #endif /* LOGIN_ACCESS */
505 
506 	/* Nothing else left to fail -- really log in. */
507 	memset((void *)&utmp, 0, sizeof(utmp));
508 	(void)time(&utmp.ut_time);
509 	(void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
510 	if (hostname)
511 		(void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
512 	(void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
513 	login(&utmp);
514 
515 	dolastlog(quietlog);
516 
517 	/*
518 	 * Set device protections, depending on what terminal the
519 	 * user is logged in. This feature is used on Suns to give
520 	 * console users better privacy.
521 	 */
522 	login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
523 
524 	/*
525 	 * Clear flags of the tty.  None should be set, and when the
526 	 * user sets them otherwise, this can cause the chown to fail.
527 	 * Since it isn't clear that flags are useful on character
528 	 * devices, we just clear them.
529 	 */
530 	if (ttyn != tname && chflags(ttyn, 0) && errno != EOPNOTSUPP)
531 		syslog(LOG_ERR, "chflags(%s): %m", ttyn);
532 	if (ttyn != tname && chown(ttyn, pwd->pw_uid,
533 	    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid))
534 		syslog(LOG_ERR, "chmod(%s): %m", ttyn);
535 
536 
537 	/*
538 	 * Preserve TERM if it happens to be already set.
539 	 */
540 	if ((term = getenv("TERM")) != NULL) {
541 		if ((term = strdup(term)) == NULL) {
542 			syslog(LOG_NOTICE,
543 			    "strdup(): %m");
544 			sleepexit(1);
545 		}
546 	}
547 
548 	/*
549 	 * Exclude cons/vt/ptys only, assume dialup otherwise
550 	 * TODO: Make dialup tty determination a library call
551 	 * for consistency (finger etc.)
552 	 */
553 	if (hostname==NULL && isdialuptty(tty))
554 		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
555 
556 #ifdef LOGALL
557 	/*
558 	 * Syslog each successful login, so we don't have to watch hundreds
559 	 * of wtmp or lastlogin files.
560 	 */
561 	if (hostname)
562 		syslog(LOG_INFO, "login from %s on %s as %s",
563 		       full_hostname, tty, pwd->pw_name);
564 	else
565 		syslog(LOG_INFO, "login on %s as %s",
566 		       tty, pwd->pw_name);
567 #endif
568 
569 	/*
570 	 * If fflag is on, assume caller/authenticator has logged root login.
571 	 */
572 	if (rootlogin && fflag == 0)
573 	{
574 		if (hostname)
575 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
576 			    username, tty, full_hostname);
577 		else
578 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
579 			    username, tty);
580 	}
581 
582 	/*
583 	 * Destroy environment unless user has requested its preservation.
584 	 * We need to do this before setusercontext() because that may
585 	 * set or reset some environment variables.
586 	 */
587 	if (!pflag)
588 		environ = envinit;
589 
590 	/*
591 	 * PAM modules might add supplementary groups during pam_setcred().
592 	 */
593 	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
594                 syslog(LOG_ERR, "setusercontext() failed - exiting");
595 		exit(1);
596 	}
597 
598 #ifdef USE_PAM
599 	if (pamh) {
600 		if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
601 			syslog(LOG_ERR, "pam_open_session: %s",
602 			    pam_strerror(pamh, e));
603 		} else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED))
604 		    != PAM_SUCCESS) {
605 			syslog(LOG_ERR, "pam_setcred: %s",
606 			    pam_strerror(pamh, e));
607 		}
608 
609 	        /*
610 	         * Add any environmental variables that the
611 	         * PAM modules may have set.
612 		 * Call *after* opening session!
613 		 */
614 		if (pamh) {
615 		  environ_pam = pam_getenvlist(pamh);
616 		  if (environ_pam)
617 			export_pam_environment();
618 		}
619 
620 		/*
621 		 * We must fork() before setuid() because we need to call
622 		 * pam_close_session() as root.
623 		 */
624 		pid = fork();
625 		if (pid < 0) {
626 			err(1, "fork");
627 			PAM_END;
628 			exit(0);
629 		} else if (pid) {
630 			/* parent - wait for child to finish, then cleanup
631 			   session */
632 			setproctitle("-%s [pam]", getprogname());
633 			wait(NULL);
634 			PAM_END;
635 			exit(0);
636 		} else {
637 			if ((e = pam_end(pamh, PAM_DATA_SILENT)) != PAM_SUCCESS)
638 				syslog(LOG_ERR, "pam_end: %s",
639 				    pam_strerror(pamh, e));
640 		}
641 	}
642 #endif /* USE_PAM */
643 
644 	/*
645 	 * We don't need to be root anymore, so
646 	 * set the user and session context
647 	 */
648 	if (setlogin(username) != 0) {
649                 syslog(LOG_ERR, "setlogin(%s): %m - exiting", username);
650 		exit(1);
651 	}
652 	if (setusercontext(lc, pwd, pwd->pw_uid,
653 	    LOGIN_SETALL & ~(LOGIN_SETLOGIN|LOGIN_SETGROUP)) != 0) {
654                 syslog(LOG_ERR, "setusercontext() failed - exiting");
655 		exit(1);
656 	}
657 
658 	(void)setenv("SHELL", pwd->pw_shell, 1);
659 	(void)setenv("HOME", pwd->pw_dir, 1);
660 	if (term != NULL && *term != '\0')
661 		(void)setenv("TERM", term, 1);		/* Preset overrides */
662 	else {
663 		(void)setenv("TERM", stypeof(tty), 0);	/* Fallback doesn't */
664 	}
665 	(void)setenv("LOGNAME", username, 1);
666 	(void)setenv("USER", username, 1);
667 	(void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
668 
669 	if (!quietlog) {
670 		char	*cw;
671 
672 		cw = login_getcapstr(lc, "copyright", NULL, NULL);
673 		if (cw != NULL && access(cw, F_OK) == 0)
674 			motd(cw);
675 		else
676 		    (void)printf("%s\n\t%s %s\n",
677 	"Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
678 	"The Regents of the University of California. ",
679 	"All rights reserved.");
680 
681 		(void)printf("\n");
682 
683 		cw = login_getcapstr(lc, "welcome", NULL, NULL);
684 		if (cw == NULL || access(cw, F_OK) != 0)
685 			cw = _PATH_MOTDFILE;
686 		motd(cw);
687 
688 		cw = getenv("MAIL");	/* $MAIL may have been set by class */
689 		if (cw != NULL)
690 			strlcpy(tbuf, cw, sizeof(tbuf));
691 		else
692 			snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILDIR,
693 			    pwd->pw_name);
694 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
695 			(void)printf("You have %smail.\n",
696 			    (st.st_mtime > st.st_atime) ? "new " : "");
697 	}
698 
699 	login_close(lc);
700 
701 	(void)signal(SIGALRM, SIG_DFL);
702 	(void)signal(SIGQUIT, SIG_DFL);
703 	(void)signal(SIGINT, SIG_DFL);
704 	(void)signal(SIGTSTP, SIG_IGN);
705 
706 	if (changepass) {
707 		if (system(_PATH_CHPASS) != 0)
708 			sleepexit(1);
709 	}
710 
711 	/*
712 	 * Login shells have a leading '-' in front of argv[0]
713 	 */
714 	if (snprintf(tbuf, sizeof(tbuf), "-%s",
715 	    (p = strrchr(pwd->pw_shell, '/')) ? p + 1 : pwd->pw_shell) >=
716 	    sizeof(tbuf)) {
717 		syslog(LOG_ERR, "user: %s: shell exceeds maximum pathname size",
718 		    username);
719 		errx(1, "shell exceeds maximum pathname size");
720 	}
721 
722 	execlp(shell, tbuf, (char *)0);
723 	err(1, "%s", shell);
724 }
725 
726 static int
727 auth_traditional(void)
728 {
729 	int rval;
730 	char *p;
731 	char *ep;
732 	char *salt;
733 
734 	rval = 1;
735 	salt = pwd != NULL ? pwd->pw_passwd : "xx";
736 
737 	p = getpass(passwd_prompt);
738 	ep = crypt(p, salt);
739 
740 	if (pwd) {
741 		if (!p[0] && pwd->pw_passwd[0])
742 			ep = ":";
743 		if (strcmp(ep, pwd->pw_passwd) == 0)
744 			rval = 0;
745 	}
746 
747 	/* clear entered password */
748 	memset(p, 0, strlen(p));
749 	return rval;
750 }
751 
752 #ifdef USE_PAM
753 /*
754  * Attempt to authenticate the user using PAM.  Returns 0 if the user is
755  * authenticated, or 1 if not authenticated.  If some sort of PAM system
756  * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
757  * function returns -1.  This can be used as an indication that we should
758  * fall back to a different authentication mechanism.
759  */
760 static int
761 auth_pam(void)
762 {
763 	const char *tmpl_user;
764 	const void *item;
765 	int rval;
766 	int e;
767 	static struct pam_conv conv = { misc_conv, NULL };
768 
769 	if ((e = pam_start("login", username, &conv, &pamh)) != PAM_SUCCESS) {
770 		syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e));
771 		return -1;
772 	}
773 	if ((e = pam_set_item(pamh, PAM_TTY, tty)) != PAM_SUCCESS) {
774 		syslog(LOG_ERR, "pam_set_item(PAM_TTY): %s",
775 		    pam_strerror(pamh, e));
776 		return -1;
777 	}
778 	if (hostname != NULL &&
779 	    (e = pam_set_item(pamh, PAM_RHOST, full_hostname)) != PAM_SUCCESS) {
780 		syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
781 		    pam_strerror(pamh, e));
782 		return -1;
783 	}
784 	e = pam_authenticate(pamh, 0);
785 	switch (e) {
786 
787 	case PAM_SUCCESS:
788 		/*
789 		 * With PAM we support the concept of a "template"
790 		 * user.  The user enters a login name which is
791 		 * authenticated by PAM, usually via a remote service
792 		 * such as RADIUS or TACACS+.  If authentication
793 		 * succeeds, a different but related "template" name
794 		 * is used for setting the credentials, shell, and
795 		 * home directory.  The name the user enters need only
796 		 * exist on the remote authentication server, but the
797 		 * template name must be present in the local password
798 		 * database.
799 		 *
800 		 * This is supported by two various mechanisms in the
801 		 * individual modules.  However, from the application's
802 		 * point of view, the template user is always passed
803 		 * back as a changed value of the PAM_USER item.
804 		 */
805 		if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
806 		    PAM_SUCCESS) {
807 			tmpl_user = (const char *) item;
808 			if (strcmp(username, tmpl_user) != 0)
809 				pwd = getpwnam(tmpl_user);
810 		} else
811 			syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
812 			    pam_strerror(pamh, e));
813 		rval = 0;
814 		break;
815 
816 	case PAM_AUTH_ERR:
817 	case PAM_USER_UNKNOWN:
818 	case PAM_MAXTRIES:
819 		rval = 1;
820 		break;
821 
822 	default:
823 		syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
824 		rval = -1;
825 		break;
826 	}
827 
828 	if (rval == 0) {
829 		e = pam_acct_mgmt(pamh, 0);
830 		if (e == PAM_NEW_AUTHTOK_REQD) {
831 			e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
832 			if (e != PAM_SUCCESS) {
833 				syslog(LOG_ERR, "pam_chauthtok: %s",
834 				    pam_strerror(pamh, e));
835 				rval = 1;
836 			}
837 		} else if (e != PAM_SUCCESS) {
838 			rval = 1;
839 		}
840 	}
841 
842 	if (rval != 0) {
843 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
844 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
845 		}
846 		pamh = NULL;
847 	}
848 	return rval;
849 }
850 
851 static int
852 export_pam_environment(void)
853 {
854 	char	**pp;
855 
856 	for (pp = environ_pam; *pp != NULL; pp++) {
857 		if (ok_to_export(*pp))
858 			(void) putenv(*pp);
859 		free(*pp);
860 	}
861 	return PAM_SUCCESS;
862 }
863 
864 /*
865  * Sanity checks on PAM environmental variables:
866  * - Make sure there is an '=' in the string.
867  * - Make sure the string doesn't run on too long.
868  * - Do not export certain variables.  This list was taken from the
869  *   Solaris pam_putenv(3) man page.
870  */
871 static int
872 ok_to_export(const char *s)
873 {
874 	static const char *noexport[] = {
875 		"SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH",
876 		"IFS", "PATH", NULL
877 	};
878 	const char **pp;
879 	size_t n;
880 
881 	if (strlen(s) > 1024 || strchr(s, '=') == NULL)
882 		return 0;
883 	if (strncmp(s, "LD_", 3) == 0)
884 		return 0;
885 	for (pp = noexport; *pp != NULL; pp++) {
886 		n = strlen(*pp);
887 		if (s[n] == '=' && strncmp(s, *pp, n) == 0)
888 			return 0;
889 	}
890 	return 1;
891 }
892 #endif /* USE_PAM */
893 
894 static void
895 usage(void)
896 {
897 
898 	(void)fprintf(stderr, "usage: login [-fp] [-h hostname] [username]\n");
899 	exit(1);
900 }
901 
902 /*
903  * Allow for authentication style and/or kerberos instance
904  */
905 
906 #define	NBUFSIZ		UT_NAMESIZE + 64
907 
908 void
909 getloginname(void)
910 {
911 	int ch;
912 	char *p;
913 	static char nbuf[NBUFSIZ];
914 
915 	for (;;) {
916 		(void)printf("%s", prompt);
917 		for (p = nbuf; (ch = getchar()) != '\n'; ) {
918 			if (ch == EOF) {
919 				badlogin(username);
920 				exit(0);
921 			}
922 			if (p < nbuf + (NBUFSIZ - 1))
923 				*p++ = ch;
924 		}
925 		if (p > nbuf) {
926 			if (nbuf[0] == '-')
927 				(void)fprintf(stderr,
928 				    "login names may not start with '-'.\n");
929 			else {
930 				*p = '\0';
931 				username = nbuf;
932 				break;
933 			}
934 		}
935 	}
936 }
937 
938 int
939 rootterm(char *ttyn)
940 {
941 	struct ttyent *t;
942 
943 	return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
944 }
945 
946 volatile int motdinterrupt;
947 
948 void
949 sigint(int signo __unused)
950 {
951 	motdinterrupt = 1;
952 }
953 
954 void
955 motd(char *motdfile)
956 {
957 	int fd, nchars;
958 	sig_t oldint;
959 	char tbuf[256];
960 
961 	if ((fd = open(motdfile, O_RDONLY, 0)) < 0)
962 		return;
963 	motdinterrupt = 0;
964 	oldint = signal(SIGINT, sigint);
965 	while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0 && !motdinterrupt)
966 		(void)write(fileno(stdout), tbuf, nchars);
967 	(void)signal(SIGINT, oldint);
968 	(void)close(fd);
969 }
970 
971 /* ARGSUSED */
972 void
973 timedout(int signo)
974 {
975 
976 	longjmp(timeout_buf, signo);
977 }
978 
979 
980 void
981 dolastlog(int quiet)
982 {
983 	struct lastlog ll;
984 	int fd;
985 
986 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
987 		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
988 		if (!quiet) {
989 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
990 			    ll.ll_time != 0) {
991 				(void)printf("Last login: %.*s ",
992 				    24-5, (char *)ctime(&ll.ll_time));
993 				if (*ll.ll_host != '\0')
994 					(void)printf("from %.*s\n",
995 					    (int)sizeof(ll.ll_host),
996 					    ll.ll_host);
997 				else
998 					(void)printf("on %.*s\n",
999 					    (int)sizeof(ll.ll_line),
1000 					    ll.ll_line);
1001 			}
1002 			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
1003 		}
1004 		memset((void *)&ll, 0, sizeof(ll));
1005 		(void)time(&ll.ll_time);
1006 		(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1007 		if (hostname)
1008 			(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
1009 		(void)write(fd, (char *)&ll, sizeof(ll));
1010 		(void)close(fd);
1011 	} else {
1012 		syslog(LOG_ERR, "cannot open %s: %m", _PATH_LASTLOG);
1013 	}
1014 }
1015 
1016 void
1017 badlogin(char *name)
1018 {
1019 
1020 	if (failures == 0)
1021 		return;
1022 	if (hostname) {
1023 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
1024 		    failures, failures > 1 ? "S" : "", full_hostname);
1025 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
1026 		    "%d LOGIN FAILURE%s FROM %s, %s",
1027 		    failures, failures > 1 ? "S" : "", full_hostname, name);
1028 	} else {
1029 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
1030 		    failures, failures > 1 ? "S" : "", tty);
1031 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
1032 		    "%d LOGIN FAILURE%s ON %s, %s",
1033 		    failures, failures > 1 ? "S" : "", tty, name);
1034 	}
1035 	failures = 0;
1036 }
1037 
1038 #undef	UNKNOWN
1039 #define	UNKNOWN	"su"
1040 
1041 char *
1042 stypeof(char *ttyid)
1043 {
1044 	struct ttyent *t;
1045 
1046 	if (ttyid != NULL && *ttyid != '\0') {
1047 		t = getttynam(ttyid);
1048 		if (t != NULL && t->ty_type != NULL)
1049 			return (t->ty_type);
1050 	}
1051 	return (UNKNOWN);
1052 }
1053 
1054 void
1055 refused(char *msg, char *rtype, int lout)
1056 {
1057 
1058 	if (msg != NULL)
1059 	    printf("%s.\n", msg);
1060 	if (hostname)
1061 		syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s",
1062 		    pwd->pw_name, rtype, full_hostname, tty);
1063 	else
1064 		syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) ON TTY %s",
1065 		    pwd->pw_name, rtype, tty);
1066 	if (lout)
1067 		sleepexit(1);
1068 }
1069 
1070 void
1071 sleepexit(int eval)
1072 {
1073 
1074 	(void)sleep(5);
1075 	exit(eval);
1076 }
1077