]> git.saurik.com Git - apple/system_cmds.git/blob - login.tproj/login.c
system_cmds-496.tar.gz
[apple/system_cmds.git] / login.tproj / login.c
1 /*-
2 * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technologies, Inc.
5 * All rights reserved.
6 *
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
11 * Portions copyright (c) 1999-2007 Apple Inc. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41
42 #if 0
43 #ifndef lint
44 static char sccsid[] = "@(#)login.c 8.4 (Berkeley) 4/2/94";
45 #endif
46 #endif
47
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD: src/usr.bin/login/login.c,v 1.106 2007/07/04 00:00:40 scf Exp $");
50
51 /*
52 * login [ name ]
53 * login -h hostname (for telnetd, etc.)
54 * login -f name (for pre-authenticated login: datakit, xterm, etc.)
55 */
56
57 #ifndef __APPLE__
58 #include <sys/copyright.h>
59 #include <TargetConditionals.h>
60 #endif
61 #include <sys/param.h>
62 #include <sys/file.h>
63 #include <sys/stat.h>
64 #include <sys/time.h>
65 #include <sys/resource.h>
66 #include <sys/wait.h>
67
68 #include <err.h>
69 #include <errno.h>
70 #include <grp.h>
71 #ifdef __APPLE__
72 #include <util.h>
73 #else
74 #include <libutil.h>
75 #endif
76 #ifdef LOGIN_CAP
77 #include <login_cap.h>
78 #endif
79 #include <pwd.h>
80 #include <setjmp.h>
81 #include <signal.h>
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <syslog.h>
86 #include <ttyent.h>
87 #include <unistd.h>
88 #ifdef __APPLE__
89 #include <utmpx.h>
90 #ifdef USE_PAM
91 #else /* !USE_PAM */
92 #ifndef _UTX_USERSIZE
93 #define _UTX_USERSIZE MAXLOGNAME
94 #endif
95 #endif /* USE_PAM */
96 #endif /* __APPLE__ */
97
98 #include <sys/types.h>
99 #include <sys/socket.h>
100 #include <netinet/in.h>
101 #include <arpa/inet.h>
102 #include <netdb.h>
103
104 #ifdef USE_BSM_AUDIT
105 #include <bsm/libbsm.h>
106 #include <bsm/audit_uevents.h>
107 #endif
108
109 #ifdef __APPLE__
110 #include <mach/mach_types.h>
111 #include <mach/task.h>
112 #include <mach/mach_init.h>
113 #include <servers/bootstrap.h>
114
115 #include <sys/file.h>
116 #include <tzfile.h>
117 #endif /* __APPLE__ */
118
119 #ifdef USE_PAM
120 #include <security/pam_appl.h>
121 #include <security/openpam.h>
122 #endif /* USE_PAM */
123
124 #include "login.h"
125 #include "pathnames.h"
126
127 #ifdef USE_PAM
128 static int auth_pam(void);
129 #endif /* USE_PAM */
130 static void bail(int, int);
131 #ifdef USE_PAM
132 static int export(const char *);
133 static void export_pam_environment(void);
134 #endif /* USE_PAM */
135 static int motd(const char *);
136 static void badlogin(char *);
137 static char *getloginname(void);
138 #ifdef USE_PAM
139 static void pam_syslog(const char *);
140 static void pam_cleanup(void);
141 #endif /* USE_PAM */
142 static void refused(const char *, const char *, int);
143 static const char *stypeof(char *);
144 static void sigint(int);
145 static void timedout(int);
146 static void usage(void);
147
148 #ifdef __APPLE__
149 static void dolastlog(int);
150 static void handle_sighup(int);
151
152 #ifndef USE_PAM
153 static void checknologin(void);
154 static int rootterm(const char *);
155 #endif /* !USE_PAM */
156 #endif /* __APPLE__ */
157
158 #define TTYGRPNAME "tty" /* group to own ttys */
159 #define DEFAULT_BACKOFF 3
160 #define DEFAULT_RETRIES 10
161 #define DEFAULT_PROMPT "login: "
162 #define DEFAULT_PASSWD_PROMPT "Password:"
163 #define TERM_UNKNOWN "su"
164 #define DEFAULT_WARN (2L * 7L * 86400L) /* Two weeks */
165 #define NO_SLEEP_EXIT 0
166 #define SLEEP_EXIT 5
167
168 /*
169 * This bounds the time given to login. Not a define so it can
170 * be patched on machines where it's too small.
171 */
172 static u_int timeout = 300;
173
174 /* Buffer for signal handling of timeout */
175 static jmp_buf timeout_buf;
176
177 struct passwd *pwd;
178 static int failures;
179
180 static char *envinit[1]; /* empty environment list */
181
182 /*
183 * Command line flags and arguments
184 */
185 static int fflag; /* -f: do not perform authentication */
186 #ifdef __APPLE__
187 static int lflag; /* -l: login session to the commmand that follows username */
188 #endif
189 static int hflag; /* -h: login from remote host */
190 static char *hostname; /* hostname from command line */
191 static int pflag; /* -p: preserve environment */
192
193 /*
194 * User name
195 */
196 static char *username; /* user name */
197 static char *olduser; /* previous user name */
198
199 /*
200 * Prompts
201 */
202 static char default_prompt[] = DEFAULT_PROMPT;
203 static const char *prompt;
204 static char default_passwd_prompt[] = DEFAULT_PASSWD_PROMPT;
205 static const char *passwd_prompt;
206
207 static char *tty;
208
209 /*
210 * PAM data
211 */
212 #ifdef USE_PAM
213 static pam_handle_t *pamh = NULL;
214 static struct pam_conv pamc = { openpam_ttyconv, NULL };
215 static int pam_err;
216 static int pam_silent = PAM_SILENT;
217 static int pam_cred_established;
218 static int pam_session_established;
219 #endif /* USE_PAM */
220
221 #ifdef __APPLE__
222 pid_t pid;
223
224 #ifdef USE_PAM
225 static struct lastlogx lastlog;
226 #endif /* USE_PAM */
227
228 #ifdef USE_BSM_AUDIT
229 au_tid_t tid;
230 #endif /* USE_BSM_AUDIT */
231 #endif /* __APPLE__ */
232
233 int
234 main(int argc, char *argv[])
235 {
236 struct group *gr;
237 struct stat st;
238 int retries, backoff;
239 int ask, ch, cnt, quietlog = 0, rootlogin, rval;
240 uid_t uid, euid;
241 gid_t egid;
242 char *term;
243 char *p, *ttyn;
244 char tname[sizeof(_PATH_TTY) + 10];
245 char *arg0;
246 const char *tp;
247 #ifdef __APPLE__
248 int prio;
249 #ifdef USE_PAM
250 const char *name = "login"; /* PAM config */
251 #else
252 struct utmpx utmp;
253 #endif /* USE_PAM */
254 const char *shell = NULL;
255 #endif /* !__APPLE__ */
256 #ifdef LOGIN_CAP
257 login_cap_t *lc = NULL;
258 login_cap_t *lc_user = NULL;
259 #endif /* LOGIN_CAP */
260 #ifndef __APPLE__
261 pid_t pid;
262 #endif
263 #ifdef USE_BSM_AUDIT
264 char auditsuccess = 1;
265 #endif
266
267 (void)signal(SIGQUIT, SIG_IGN);
268 (void)signal(SIGINT, SIG_IGN);
269 (void)signal(SIGHUP, SIG_IGN);
270 if (setjmp(timeout_buf)) {
271 if (failures)
272 badlogin(username);
273 (void)fprintf(stderr, "Login timed out after %d seconds\n",
274 timeout);
275 bail(NO_SLEEP_EXIT, 0);
276 }
277 (void)signal(SIGALRM, timedout);
278 (void)alarm(timeout);
279 #ifdef __APPLE__
280 prio = getpriority(PRIO_PROCESS, 0);
281 #endif
282 (void)setpriority(PRIO_PROCESS, 0, 0);
283
284 openlog("login", LOG_ODELAY, LOG_AUTH);
285
286 uid = getuid();
287 euid = geteuid();
288 egid = getegid();
289
290 #ifdef __APPLE__
291 while ((ch = getopt(argc, argv, "1fh:lpq")) != -1)
292 #else
293 while ((ch = getopt(argc, argv, "fh:p")) != -1)
294 #endif
295 switch (ch) {
296 case 'f':
297 fflag = 1;
298 break;
299 case 'h':
300 if (uid != 0)
301 errx(1, "-h option: %s", strerror(EPERM));
302 if (strlen(optarg) >= MAXHOSTNAMELEN)
303 errx(1, "-h option: %s: exceeds maximum "
304 "hostname size", optarg);
305 hflag = 1;
306 hostname = optarg;
307 break;
308 case 'p':
309 pflag = 1;
310 break;
311 #ifdef __APPLE__
312 case '1':
313 break;
314 case 'l':
315 lflag = 1;
316 break;
317 case 'q':
318 quietlog = 1;
319 break;
320 #endif
321 case '?':
322 default:
323 if (uid == 0)
324 syslog(LOG_ERR, "invalid flag %c", ch);
325 usage();
326 }
327 argc -= optind;
328 argv += optind;
329
330 if (argc > 0) {
331 username = strdup(*argv);
332 if (username == NULL)
333 err(1, "strdup()");
334 ask = 0;
335 #ifdef __APPLE__
336 argv++;
337 #endif /* __APPLE__ */
338 } else {
339 ask = 1;
340 }
341
342 #ifndef __APPLE__
343 setproctitle("-%s", getprogname());
344 #endif /* !__APPLE__ */
345
346 for (cnt = getdtablesize(); cnt > 2; cnt--)
347 (void)close(cnt);
348
349 /*
350 * Get current TTY
351 */
352 ttyn = ttyname(STDIN_FILENO);
353 if (ttyn == NULL || *ttyn == '\0') {
354 (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
355 ttyn = tname;
356 }
357 if ((tty = strrchr(ttyn, '/')) != NULL)
358 ++tty;
359 else
360 tty = ttyn;
361
362 #ifdef LOGIN_CAP
363 /*
364 * Get "login-retries" & "login-backoff" from default class
365 */
366 lc = login_getclass(NULL);
367 prompt = login_getcapstr(lc, "login_prompt",
368 default_prompt, default_prompt);
369 passwd_prompt = login_getcapstr(lc, "passwd_prompt",
370 default_passwd_prompt, default_passwd_prompt);
371 retries = login_getcapnum(lc, "login-retries",
372 DEFAULT_RETRIES, DEFAULT_RETRIES);
373 backoff = login_getcapnum(lc, "login-backoff",
374 DEFAULT_BACKOFF, DEFAULT_BACKOFF);
375 login_close(lc);
376 lc = NULL;
377 #else /* !LOGIN_CAP */
378 prompt = default_prompt;
379 passwd_prompt = default_passwd_prompt;
380 retries = DEFAULT_RETRIES;
381 backoff = DEFAULT_BACKOFF;
382 #endif /* !LOGIN_CAP */
383
384 #ifdef __APPLE__
385 #ifdef USE_BSM_AUDIT
386 /* Set the terminal id */
387 audit_set_terminal_id(&tid);
388 if (fstat(STDIN_FILENO, &st) < 0) {
389 fprintf(stderr, "login: Unable to stat terminal\n");
390 au_login_fail("Unable to stat terminal", 1);
391 exit(-1);
392 }
393 if (S_ISCHR(st.st_mode)) {
394 tid.port = st.st_rdev;
395 } else {
396 tid.port = 0;
397 }
398 #endif /* USE_BSM_AUDIT */
399 #endif /* __APPLE__ */
400
401 /*
402 * Try to authenticate the user until we succeed or time out.
403 */
404 for (cnt = 0;; ask = 1) {
405 if (ask) {
406 fflag = 0;
407 if (olduser != NULL)
408 free(olduser);
409 olduser = username;
410 username = getloginname();
411 }
412 rootlogin = 0;
413
414 #ifdef __APPLE__
415 if (strlen(username) > _UTX_USERSIZE)
416 username[_UTX_USERSIZE] = '\0';
417 #endif /* __APPLE__ */
418
419 /*
420 * Note if trying multiple user names; log failures for
421 * previous user name, but don't bother logging one failure
422 * for nonexistent name (mistyped username).
423 */
424 if (failures && strcmp(olduser, username) != 0) {
425 if (failures > (pwd ? 0 : 1))
426 badlogin(olduser);
427 }
428
429 #ifdef __APPLE__
430 #ifdef USE_PAM
431 /* get lastlog info before PAM make a new entry */
432 if (!quietlog)
433 getlastlogxbyname(username, &lastlog);
434 #endif /* USE_PAM */
435 #endif /* __APPLE__ */
436
437 pwd = getpwnam(username);
438
439 #ifdef USE_PAM
440 /*
441 * Load the PAM policy and set some variables
442 */
443 #ifdef __APPLE__
444 if (fflag && (pwd != NULL) && (pwd->pw_uid == uid)) {
445 name = "login.term";
446 }
447 #endif
448 pam_err = pam_start(name, username, &pamc, &pamh);
449 if (pam_err != PAM_SUCCESS) {
450 pam_syslog("pam_start()");
451 #ifdef USE_BSM_AUDIT
452 au_login_fail("PAM Error", 1);
453 #endif
454 bail(NO_SLEEP_EXIT, 1);
455 }
456 pam_err = pam_set_item(pamh, PAM_TTY, tty);
457 if (pam_err != PAM_SUCCESS) {
458 pam_syslog("pam_set_item(PAM_TTY)");
459 #ifdef USE_BSM_AUDIT
460 au_login_fail("PAM Error", 1);
461 #endif
462 bail(NO_SLEEP_EXIT, 1);
463 }
464 pam_err = pam_set_item(pamh, PAM_RHOST, hostname);
465 if (pam_err != PAM_SUCCESS) {
466 pam_syslog("pam_set_item(PAM_RHOST)");
467 #ifdef USE_BSM_AUDIT
468 au_login_fail("PAM Error", 1);
469 #endif
470 bail(NO_SLEEP_EXIT, 1);
471 }
472 #endif /* USE_PAM */
473
474 if (pwd != NULL && pwd->pw_uid == 0)
475 rootlogin = 1;
476
477 /*
478 * If the -f option was specified and the caller is
479 * root or the caller isn't changing their uid, don't
480 * authenticate.
481 */
482 if (pwd != NULL && fflag &&
483 (uid == (uid_t)0 || uid == (uid_t)pwd->pw_uid)) {
484 /* already authenticated */
485 rval = 0;
486 #ifdef USE_BSM_AUDIT
487 auditsuccess = 0; /* opened a terminal window only */
488 #endif
489
490 #ifdef __APPLE__
491 #ifndef USE_PAM
492 /* If the account doesn't have a password, authenticate. */
493 } else if (pwd != NULL && pwd->pw_passwd[0] == '\0') {
494 rval = 0;
495 #endif /* !USE_PAM */
496 #endif /* __APPLE__ */
497 } else {
498 fflag = 0;
499 (void)setpriority(PRIO_PROCESS, 0, -4);
500 #ifdef USE_PAM
501 rval = auth_pam();
502 #else
503 {
504 char* salt = pwd ? pwd->pw_passwd : "xx";
505 char* p = getpass(passwd_prompt);
506 rval = strcmp(crypt(p, salt), pwd->pw_passwd);
507 memset(p, 0, strlen(p));
508 }
509 #endif
510 (void)setpriority(PRIO_PROCESS, 0, 0);
511 }
512
513 #ifdef __APPLE__
514 #ifndef USE_PAM
515 /*
516 * If trying to log in as root but with insecure terminal,
517 * refuse the login attempt.
518 */
519 if (pwd && rootlogin && !rootterm(tty)) {
520 refused("root login refused on this terminal", "ROOTTERM", 0);
521 #ifdef USE_BSM_AUDIT
522 au_login_fail("Login refused on terminal", 0);
523 #endif
524 continue;
525 }
526 #endif /* !USE_PAM */
527 #endif /* __APPLE__ */
528
529 if (pwd && rval == 0)
530 break;
531
532 #ifdef USE_PAM
533 pam_cleanup();
534 #endif /* USE_PAM */
535
536 /*
537 * We are not exiting here, but this corresponds to a failed
538 * login event, so set exitstatus to 1.
539 */
540 #ifdef USE_BSM_AUDIT
541 au_login_fail("Login incorrect", 1);
542 #endif
543
544 (void)printf("Login incorrect\n");
545 failures++;
546
547 pwd = NULL;
548
549 /*
550 * Allow up to 'retry' (10) attempts, but start
551 * backing off after 'backoff' (3) attempts.
552 */
553 if (++cnt > backoff) {
554 if (cnt >= retries) {
555 badlogin(username);
556 bail(SLEEP_EXIT, 1);
557 }
558 sleep((u_int)((cnt - backoff) * 5));
559 }
560 }
561
562 /* committed to login -- turn off timeout */
563 (void)alarm((u_int)0);
564 (void)signal(SIGHUP, SIG_DFL);
565
566 endpwent();
567
568 #ifdef __APPLE__
569 if (!pwd) {
570 fprintf(stderr, "login: Unable to find user: %s\n", username);
571 exit(1);
572 }
573
574 #ifndef USE_PAM
575 /* if user not super-user, check for disabled logins */
576 if (!rootlogin)
577 checknologin();
578 #endif /* !USE_PAM */
579 #endif /* APPLE */
580
581 #ifdef USE_BSM_AUDIT
582 /* Audit successful login. */
583 if (auditsuccess)
584 au_login_success();
585 #endif
586
587 #ifdef LOGIN_CAP
588 /*
589 * Establish the login class.
590 */
591 lc = login_getpwclass(pwd);
592 lc_user = login_getuserclass(pwd);
593
594 if (!(quietlog = login_getcapbool(lc_user, "hushlogin", 0)))
595 quietlog = login_getcapbool(lc, "hushlogin", 0);
596 #endif /* LOGIN_CAP */
597
598 #ifndef __APPLE__
599 /*
600 * Switching needed for NFS with root access disabled.
601 *
602 * XXX: This change fails to modify the additional groups for the
603 * process, and as such, may restrict rights normally granted
604 * through those groups.
605 */
606 (void)setegid(pwd->pw_gid);
607 (void)seteuid(rootlogin ? 0 : pwd->pw_uid);
608
609 if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) {
610 #ifdef LOGIN_CAP
611 if (login_getcapbool(lc, "requirehome", 0))
612 refused("Home directory not available", "HOMEDIR", 1);
613 #endif /* LOGIN_CAP */
614 if (chdir("/") < 0)
615 refused("Cannot find root directory", "ROOTDIR", 1);
616 if (!quietlog || *pwd->pw_dir)
617 printf("No home directory.\nLogging in with home = \"/\".\n");
618 pwd->pw_dir = strdup("/");
619 if (pwd->pw_dir == NULL) {
620 syslog(LOG_NOTICE, "strdup(): %m");
621 bail(SLEEP_EXIT, 1);
622 }
623 }
624
625 (void)seteuid(euid);
626 (void)setegid(egid);
627 #endif /* !__APPLE__ */
628 if (!quietlog) {
629 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
630 #ifdef USE_PAM
631 if (!quietlog)
632 pam_silent = 0;
633 #endif /* USE_PAM */
634 }
635
636 #ifdef __APPLE__
637 /* Nothing else left to fail -- really log in. */
638 #ifndef USE_PAM
639 memset((void *)&utmp, 0, sizeof(utmp));
640 (void)gettimeofday(&utmp.ut_tv, NULL);
641 (void)strncpy(utmp.ut_user, username, sizeof(utmp.ut_user));
642 if (hostname)
643 (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
644 (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
645 utmp.ut_type = USER_PROCESS | UTMPX_AUTOFILL_MASK;
646 utmp.ut_pid = getpid();
647 pututxline(&utmp);
648 #endif /* USE_PAM */
649
650 shell = "";
651 #endif /* !__APPLE__ */
652 #ifdef LOGIN_CAP
653 shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
654 #endif /* !LOGIN_CAP */
655 if (*pwd->pw_shell == '\0')
656 pwd->pw_shell = strdup(_PATH_BSHELL);
657 if (pwd->pw_shell == NULL) {
658 syslog(LOG_NOTICE, "strdup(): %m");
659 bail(SLEEP_EXIT, 1);
660 }
661 if (*shell == '\0') /* Not overridden */
662 shell = pwd->pw_shell;
663 if ((shell = strdup(shell)) == NULL) {
664 syslog(LOG_NOTICE, "strdup(): %m");
665 bail(SLEEP_EXIT, 1);
666 }
667
668 #if defined(__APPLE__) && TARGET_OS_EMBEDDED
669 /* on embedded, allow a shell to live in /var/debug_mount/bin/sh */
670 #define _PATH_DEBUGSHELL "/var/debug_mount/bin/sh"
671 if (stat(pwd->pw_shell, &st) != 0) {
672 if (stat(_PATH_DEBUGSHELL, &st) == 0) {
673 pwd->pw_shell = strdup(_PATH_DEBUGSHELL);
674 }
675 }
676 #endif
677
678 #ifdef __APPLE__
679 dolastlog(quietlog);
680 #endif
681
682 #ifndef __APPLE__
683 /*
684 * Set device protections, depending on what terminal the
685 * user is logged in. This feature is used on Suns to give
686 * console users better privacy.
687 */
688 login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
689 #endif /* !__APPLE__ */
690
691 /*
692 * Clear flags of the tty. None should be set, and when the
693 * user sets them otherwise, this can cause the chown to fail.
694 * Since it isn't clear that flags are useful on character
695 * devices, we just clear them.
696 *
697 * We don't log in the case of EOPNOTSUPP because dev might be
698 * on NFS, which doesn't support chflags.
699 *
700 * We don't log in the EROFS because that means that /dev is on
701 * a read only file system and we assume that the permissions there
702 * are sane.
703 */
704 if (ttyn != tname && chflags(ttyn, 0))
705 #ifdef __APPLE__
706 if (errno != EOPNOTSUPP && errno != ENOTSUP && errno != EROFS)
707 #else
708 if (errno != EOPNOTSUPP && errno != EROFS)
709 #endif
710 syslog(LOG_ERR, "chflags(%s): %m", ttyn);
711 if (ttyn != tname && chown(ttyn, pwd->pw_uid,
712 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid))
713 if (errno != EROFS)
714 syslog(LOG_ERR, "chown(%s): %m", ttyn);
715
716 #ifdef __APPLE__
717 (void)chmod(ttyn, 0620);
718 #endif /* __APPLE__ */
719
720 #ifndef __APPLE__
721 /*
722 * Exclude cons/vt/ptys only, assume dialup otherwise
723 * TODO: Make dialup tty determination a library call
724 * for consistency (finger etc.)
725 */
726 if (hflag && isdialuptty(tty))
727 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
728 #endif /* !__APPLE__ */
729
730 #ifdef LOGALL
731 /*
732 * Syslog each successful login, so we don't have to watch
733 * hundreds of wtmp or lastlogin files.
734 */
735 if (hflag)
736 syslog(LOG_INFO, "login from %s on %s as %s",
737 hostname, tty, pwd->pw_name);
738 else
739 syslog(LOG_INFO, "login on %s as %s",
740 tty, pwd->pw_name);
741 #endif
742
743 /*
744 * If fflag is on, assume caller/authenticator has logged root
745 * login.
746 */
747 if (rootlogin && fflag == 0) {
748 if (hflag)
749 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
750 username, tty, hostname);
751 else
752 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
753 username, tty);
754 }
755
756 /*
757 * Destroy environment unless user has requested its
758 * preservation - but preserve TERM in all cases
759 */
760 term = getenv("TERM");
761 if (!pflag)
762 environ = envinit;
763 if (term != NULL)
764 setenv("TERM", term, 0);
765
766 #ifndef __APPLE__
767 /*
768 * PAM modules might add supplementary groups during pam_setcred().
769 */
770 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
771 syslog(LOG_ERR, "setusercontext() failed - exiting");
772 bail(NO_SLEEP_EXIT, 1);
773 }
774 #endif /* !__APPLE__ */
775 #ifdef USE_PAM
776 pam_err = pam_setcred(pamh, pam_silent|PAM_ESTABLISH_CRED);
777 if (pam_err != PAM_SUCCESS) {
778 pam_syslog("pam_setcred()");
779 bail(NO_SLEEP_EXIT, 1);
780 }
781 pam_cred_established = 1;
782
783 pam_err = pam_open_session(pamh, pam_silent);
784 if (pam_err != PAM_SUCCESS) {
785 pam_syslog("pam_open_session()");
786 bail(NO_SLEEP_EXIT, 1);
787 }
788 pam_session_established = 1;
789 #endif /* USE_PAM */
790
791 #ifdef __APPLE__
792 /* <rdar://problem/5377791>
793 Install a signal handler that will forward SIGHUP to the
794 child and process group. The parent should not exit on
795 SIGHUP so that the tty ownership can be reset. */
796 (void)signal(SIGHUP, handle_sighup);
797 #endif /* __APPLE__ */
798
799 /*
800 * We must fork() before setuid() because we need to call
801 * pam_close_session() as root.
802 */
803 pid = fork();
804 if (pid < 0) {
805 err(1, "fork");
806 } else if (pid != 0) {
807 /*
808 * Parent: wait for child to finish, then clean up
809 * session.
810 */
811 int status;
812 #ifndef __APPLE__
813 setproctitle("-%s [pam]", getprogname());
814 #endif /* !__APPLE__ */
815 #ifdef __APPLE__
816 /* Our SIGHUP handler may interrupt the wait */
817 int res;
818 do {
819 res = waitpid(pid, &status, 0);
820 } while (res == -1 && errno == EINTR);
821 #else
822 waitpid(pid, &status, 0);
823 #endif
824 #ifdef __APPLE__
825 chown(ttyn, 0, 0);
826 chmod(ttyn, 0666);
827 #endif /* __APPLE__ */
828 bail(NO_SLEEP_EXIT, 0);
829 }
830
831 /*
832 * NOTICE: We are now in the child process!
833 */
834
835 #ifdef __APPLE__
836 /* Restore the default SIGHUP handler for the child. */
837 (void)signal(SIGHUP, SIG_DFL);
838 #endif /* __APPLE__ */
839
840 #ifdef USE_PAM
841 /*
842 * Add any environment variables the PAM modules may have set.
843 */
844 export_pam_environment();
845
846 /*
847 * We're done with PAM now; our parent will deal with the rest.
848 */
849 pam_end(pamh, 0);
850 pamh = NULL;
851 #endif /* USE_PAM */
852
853 /*
854 * We don't need to be root anymore, so set the login name and
855 * the UID.
856 */
857 if (setlogin(username) != 0) {
858 syslog(LOG_ERR, "setlogin(%s): %m - exiting", username);
859 bail(NO_SLEEP_EXIT, 1);
860 }
861 #ifdef __APPLE__
862 /* <rdar://problem/6041650> restore process priority if not changing uids */
863 if (uid == (uid_t)pwd->pw_uid) {
864 (void)setpriority(PRIO_PROCESS, 0, prio);
865 }
866
867 (void)setgid(pwd->pw_gid);
868 if (initgroups(username, pwd->pw_gid) == -1)
869 syslog(LOG_ERR, "login: initgroups() failed");
870 (void) setuid(rootlogin ? 0 : pwd->pw_uid);
871 #else /* !__APPLE__ */
872 if (setusercontext(lc, pwd, pwd->pw_uid,
873 LOGIN_SETALL & ~(LOGIN_SETLOGIN|LOGIN_SETGROUP)) != 0) {
874 syslog(LOG_ERR, "setusercontext() failed - exiting");
875 exit(1);
876 }
877 #endif /* !__APPLE__ */
878
879 #ifdef __APPLE__
880 /* We test for the home directory after pam_open_session(3)
881 * as the home directory may have been mounted by a session
882 * module, and after changing uid as the home directory may
883 * be NFS with root access disabled. */
884 if (!lflag) {
885 /* First do a stat in case the homedir is automounted */
886 stat(pwd->pw_dir,&st);
887 if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) {
888 printf("No home directory: %s\n", pwd->pw_dir);
889 if (chdir("/") < 0) {
890 refused("Cannot find root directory", "ROOTDIR", 0);
891 exit(1);
892 }
893 pwd->pw_dir = strdup("/");
894 if (pwd->pw_dir == NULL) {
895 syslog(LOG_NOTICE, "strdup(): %m");
896 exit(1);
897 }
898 }
899 }
900 #endif /* __APPLE__ */
901 if (pwd->pw_shell) {
902 (void)setenv("SHELL", pwd->pw_shell, 1);
903 } else {
904 syslog(LOG_ERR, "pwd->pw_shell not set - exiting", username);
905 bail(NO_SLEEP_EXIT, 1);
906 }
907 if (pwd->pw_dir) {
908 (void)setenv("HOME", pwd->pw_dir, 1);
909 } else {
910 (void)setenv("HOME", "/", 1);
911 }
912 /* Overwrite "term" from login.conf(5) for any known TERM */
913 if (term == NULL && (tp = stypeof(tty)) != NULL)
914 (void)setenv("TERM", tp, 1);
915 else
916 (void)setenv("TERM", TERM_UNKNOWN, 0);
917 (void)setenv("LOGNAME", username, 1);
918 (void)setenv("USER", username, 1);
919 (void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
920
921 #ifdef __APPLE__
922 /* Re-enable crash reporter */
923 do {
924 kern_return_t kr;
925 mach_port_t bp, ep, mts;
926 thread_state_flavor_t flavor = 0;
927
928 #if defined(__ppc__)
929 flavor = PPC_THREAD_STATE64;
930 #elif defined(__i386__) || defined(__x86_64__)
931 flavor = x86_THREAD_STATE;
932 #elif defined(__arm__)
933 flavor = ARM_THREAD_STATE;
934 #else
935 #error unsupported architecture
936 #endif
937
938 mts = mach_task_self();
939
940 kr = task_get_bootstrap_port(mts, &bp);
941 if (kr != KERN_SUCCESS) {
942 syslog(LOG_ERR, "task_get_bootstrap_port() failure: %s (%d)",
943 bootstrap_strerror(kr), kr);
944 break;
945 }
946
947 const char* bs = "com.apple.ReportCrash";
948 kr = bootstrap_look_up(bp, (char*)bs, &ep);
949 if (kr != KERN_SUCCESS) {
950 syslog(LOG_ERR, "bootstrap_look_up(%s) failure: %s (%d)",
951 bs, bootstrap_strerror(kr), kr);
952 break;
953 }
954
955 kr = task_set_exception_ports(mts, EXC_MASK_CRASH, ep, EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, flavor);
956 if (kr != KERN_SUCCESS) {
957 syslog(LOG_ERR, "task_set_exception_ports() failure: %d", kr);
958 break;
959 }
960 } while (0);
961 #endif __APPLE__
962
963 if (!quietlog) {
964 #ifdef LOGIN_CAP
965 const char *cw;
966
967 cw = login_getcapstr(lc, "copyright", NULL, NULL);
968 if (cw == NULL || motd(cw) == -1)
969 (void)printf("%s", copyright);
970
971 (void)printf("\n");
972
973 cw = login_getcapstr(lc, "welcome", NULL, NULL);
974 if (cw != NULL && access(cw, F_OK) == 0)
975 motd(cw);
976 else
977 motd(_PATH_MOTDFILE);
978
979 if (login_getcapbool(lc_user, "nocheckmail", 0) == 0 &&
980 login_getcapbool(lc, "nocheckmail", 0) == 0) {
981 #else /* !LOGIN_CAP */
982 motd(_PATH_MOTDFILE);
983 {
984 #endif /* !LOGIN_CAP */
985 char *cx;
986
987 /* $MAIL may have been set by class. */
988 cx = getenv("MAIL");
989 if (cx == NULL) {
990 asprintf(&cx, "%s/%s",
991 _PATH_MAILDIR, pwd->pw_name);
992 }
993 if (cx && stat(cx, &st) == 0 && st.st_size != 0)
994 (void)printf("You have %smail.\n",
995 (st.st_mtime > st.st_atime) ? "new " : "");
996 if (getenv("MAIL") == NULL)
997 free(cx);
998 }
999 }
1000
1001 #ifdef LOGIN_CAP
1002 login_close(lc_user);
1003 login_close(lc);
1004 #endif /* LOGIN_CAP */
1005
1006 (void)signal(SIGALRM, SIG_DFL);
1007 (void)signal(SIGQUIT, SIG_DFL);
1008 (void)signal(SIGINT, SIG_DFL);
1009 (void)signal(SIGTSTP, SIG_IGN);
1010
1011 #ifdef __APPLE__
1012 if (fflag && *argv) pwd->pw_shell = *argv;
1013 #endif /* __APPLE__ */
1014
1015 /*
1016 * Login shells have a leading '-' in front of argv[0]
1017 */
1018 p = strrchr(pwd->pw_shell, '/');
1019 #ifdef __APPLE__
1020 if (asprintf(&arg0, "%s%s", lflag ? "" : "-", p ? p + 1 : pwd->pw_shell) >= MAXPATHLEN) {
1021 #else /* __APPLE__ */
1022 if (asprintf(&arg0, "-%s", p ? p + 1 : pwd->pw_shell) >= MAXPATHLEN) {
1023 #endif /* __APPLE__ */
1024 syslog(LOG_ERR, "user: %s: shell exceeds maximum pathname size",
1025 username);
1026 errx(1, "shell exceeds maximum pathname size");
1027 } else if (arg0 == NULL) {
1028 err(1, "asprintf()");
1029 }
1030
1031 #ifdef __APPLE__
1032 if (fflag && *argv) {
1033 *argv = arg0;
1034 execvp(pwd->pw_shell, argv);
1035 err(1, "%s", arg0);
1036 }
1037 #endif /* __APPLE__ */
1038 execlp(shell, arg0, (char *)0);
1039 err(1, "%s", shell);
1040
1041 /*
1042 * That's it, folks!
1043 */
1044 }
1045
1046 #ifdef USE_PAM
1047 /*
1048 * Attempt to authenticate the user using PAM. Returns 0 if the user is
1049 * authenticated, or 1 if not authenticated. If some sort of PAM system
1050 * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
1051 * function returns -1. This can be used as an indication that we should
1052 * fall back to a different authentication mechanism.
1053 */
1054 static int
1055 auth_pam(void)
1056 {
1057 const char *tmpl_user;
1058 const void *item;
1059 int rval;
1060
1061 pam_err = pam_authenticate(pamh, pam_silent);
1062 switch (pam_err) {
1063
1064 case PAM_SUCCESS:
1065 /*
1066 * With PAM we support the concept of a "template"
1067 * user. The user enters a login name which is
1068 * authenticated by PAM, usually via a remote service
1069 * such as RADIUS or TACACS+. If authentication
1070 * succeeds, a different but related "template" name
1071 * is used for setting the credentials, shell, and
1072 * home directory. The name the user enters need only
1073 * exist on the remote authentication server, but the
1074 * template name must be present in the local password
1075 * database.
1076 *
1077 * This is supported by two various mechanisms in the
1078 * individual modules. However, from the application's
1079 * point of view, the template user is always passed
1080 * back as a changed value of the PAM_USER item.
1081 */
1082 pam_err = pam_get_item(pamh, PAM_USER, &item);
1083 if (pam_err == PAM_SUCCESS) {
1084 tmpl_user = (const char *)item;
1085 if (strcmp(username, tmpl_user) != 0)
1086 pwd = getpwnam(tmpl_user);
1087 } else {
1088 pam_syslog("pam_get_item(PAM_USER)");
1089 }
1090 rval = 0;
1091 break;
1092
1093 case PAM_AUTH_ERR:
1094 case PAM_USER_UNKNOWN:
1095 case PAM_MAXTRIES:
1096 rval = 1;
1097 break;
1098
1099 default:
1100 pam_syslog("pam_authenticate()");
1101 rval = -1;
1102 break;
1103 }
1104
1105 if (rval == 0) {
1106 pam_err = pam_acct_mgmt(pamh, pam_silent);
1107 switch (pam_err) {
1108 case PAM_SUCCESS:
1109 break;
1110 case PAM_NEW_AUTHTOK_REQD:
1111 pam_err = pam_chauthtok(pamh,
1112 pam_silent|PAM_CHANGE_EXPIRED_AUTHTOK);
1113 if (pam_err != PAM_SUCCESS) {
1114 pam_syslog("pam_chauthtok()");
1115 rval = 1;
1116 }
1117 break;
1118 default:
1119 pam_syslog("pam_acct_mgmt()");
1120 rval = 1;
1121 break;
1122 }
1123 }
1124
1125 if (rval != 0) {
1126 pam_end(pamh, pam_err);
1127 pamh = NULL;
1128 }
1129 return (rval);
1130 }
1131
1132 /*
1133 * Export any environment variables PAM modules may have set
1134 */
1135 static void
1136 export_pam_environment()
1137 {
1138 char **pam_env;
1139 char **pp;
1140
1141 pam_env = pam_getenvlist(pamh);
1142 if (pam_env != NULL) {
1143 for (pp = pam_env; *pp != NULL; pp++) {
1144 (void)export(*pp);
1145 free(*pp);
1146 }
1147 }
1148 }
1149
1150 /*
1151 * Perform sanity checks on an environment variable:
1152 * - Make sure there is an '=' in the string.
1153 * - Make sure the string doesn't run on too long.
1154 * - Do not export certain variables. This list was taken from the
1155 * Solaris pam_putenv(3) man page.
1156 * Then export it.
1157 */
1158 static int
1159 export(const char *s)
1160 {
1161 static const char *noexport[] = {
1162 "SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH",
1163 "IFS", "PATH", NULL
1164 };
1165 char *p;
1166 const char **pp;
1167 size_t n;
1168
1169 if (strlen(s) > 1024 || (p = strchr(s, '=')) == NULL)
1170 return (0);
1171 if (strncmp(s, "LD_", 3) == 0)
1172 return (0);
1173 for (pp = noexport; *pp != NULL; pp++) {
1174 n = strlen(*pp);
1175 if (s[n] == '=' && strncmp(s, *pp, n) == 0)
1176 return (0);
1177 }
1178 *p = '\0';
1179 (void)setenv(s, p + 1, 1);
1180 *p = '=';
1181 return (1);
1182 }
1183 #endif /* USE_PAM */
1184
1185 static void
1186 usage()
1187 {
1188 #ifdef __APPLE__
1189 (void)fprintf(stderr, "usage: login [-pq] [-h hostname] [username]\n");
1190 (void)fprintf(stderr, " login -f [-lpq] [-h hostname] [username [prog [arg ...]]]\n");
1191 #else
1192 (void)fprintf(stderr, "usage: login [-fp] [-h hostname] [username]\n");
1193 #endif
1194 exit(1);
1195 }
1196
1197 /*
1198 * Prompt user and read login name from stdin.
1199 */
1200 static char *
1201 getloginname()
1202 {
1203 char *nbuf, *p;
1204 int ch;
1205
1206 nbuf = malloc(MAXLOGNAME);
1207 if (nbuf == NULL)
1208 err(1, "malloc()");
1209 do {
1210 (void)printf("%s", prompt);
1211 for (p = nbuf; (ch = getchar()) != '\n'; ) {
1212 if (ch == EOF) {
1213 badlogin(username);
1214 bail(NO_SLEEP_EXIT, 0);
1215 }
1216 if (p < nbuf + MAXLOGNAME - 1)
1217 *p++ = ch;
1218 }
1219 } while (p == nbuf);
1220
1221 *p = '\0';
1222 if (nbuf[0] == '-') {
1223 #ifdef USE_PAM
1224 pam_silent = 0;
1225 #endif /* USE_PAM */
1226 memmove(nbuf, nbuf + 1, strlen(nbuf));
1227 } else {
1228 #ifdef USE_PAM
1229 pam_silent = PAM_SILENT;
1230 #endif /* USE_PAM */
1231 }
1232 return nbuf;
1233 }
1234
1235 #ifdef __APPLE__
1236 #ifndef USE_PAM
1237 static int
1238 rootterm(const char* ttyn)
1239 {
1240 struct ttyent *t;
1241 return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
1242 }
1243 #endif /* !USE_PAM */
1244 #endif /* __APPLE__ */
1245
1246 /*
1247 * SIGINT handler for motd().
1248 */
1249 static volatile int motdinterrupt;
1250 static void
1251 sigint(int signo __unused)
1252 {
1253 motdinterrupt = 1;
1254 }
1255
1256 /*
1257 * Display the contents of a file (such as /etc/motd).
1258 */
1259 static int
1260 motd(const char *motdfile)
1261 {
1262 sig_t oldint;
1263 FILE *f;
1264 int ch;
1265
1266 if ((f = fopen(motdfile, "r")) == NULL)
1267 return (-1);
1268 motdinterrupt = 0;
1269 oldint = signal(SIGINT, sigint);
1270 while ((ch = fgetc(f)) != EOF && !motdinterrupt)
1271 putchar(ch);
1272 signal(SIGINT, oldint);
1273 if (ch != EOF || ferror(f)) {
1274 fclose(f);
1275 return (-1);
1276 }
1277 fclose(f);
1278 return (0);
1279 }
1280
1281 /*
1282 * SIGHUP handler
1283 * Forwards the SIGHUP to the child process and current process group.
1284 */
1285 static void
1286 handle_sighup(int signo)
1287 {
1288 if (pid > 0) {
1289 /* close the controlling terminal */
1290 close(STDIN_FILENO);
1291 close(STDOUT_FILENO);
1292 close(STDERR_FILENO);
1293 /* Ignore SIGHUP to avoid tail-recursion on signaling
1294 the current process group (of which we are a member). */
1295 (void)signal(SIGHUP, SIG_IGN);
1296 /* Forward the signal to the current process group. */
1297 (void)kill(0, signo);
1298 /* Forward the signal to the child if not a member of the current
1299 * process group <rdar://problem/6244808>. */
1300 if (getpgid(pid) != getpgrp()) {
1301 (void)kill(pid, signo);
1302 }
1303 }
1304 }
1305
1306 /*
1307 * SIGALRM handler, to enforce login prompt timeout.
1308 *
1309 * XXX This can potentially confuse the hell out of PAM. We should
1310 * XXX instead implement a conversation function that returns
1311 * XXX PAM_CONV_ERR when interrupted by a signal, and have the signal
1312 * XXX handler just set a flag.
1313 */
1314 static void
1315 timedout(int signo __unused)
1316 {
1317
1318 longjmp(timeout_buf, signo);
1319 }
1320
1321 #ifdef __APPLE__
1322 #ifndef USE_PAM
1323 void
1324 checknologin()
1325 {
1326 int fd, nchars;
1327 char tbuf[8192];
1328
1329 if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
1330 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
1331 (void)write(fileno(stdout), tbuf, nchars);
1332 #ifdef USE_BSM_AUDIT
1333 au_login_fail("No login", 0);
1334 #endif
1335 sleep(5);
1336 exit(0);
1337 }
1338 }
1339 #endif /* !USE_PAM */
1340
1341 void
1342 dolastlog(quiet)
1343 int quiet;
1344 {
1345 #ifdef USE_PAM
1346 if (quiet)
1347 return;
1348 if (*lastlog.ll_line) {
1349 (void)printf("Last login: %.*s ",
1350 24-5, (char *)ctime(&lastlog.ll_tv.tv_sec));
1351 if (*lastlog.ll_host != '\0')
1352 (void)printf("from %.*s\n",
1353 (int)sizeof(lastlog.ll_host),
1354 lastlog.ll_host);
1355 else
1356 (void)printf("on %.*s\n",
1357 (int)sizeof(lastlog.ll_line),
1358 lastlog.ll_line);
1359 }
1360 #else /* !USE_PAM */
1361 struct lastlogx ll;
1362
1363 if(!quiet && getlastlogx(pwd->pw_uid, &ll) != NULL) {
1364 (void)printf("Last login: %.*s ",
1365 24-5, (char *)ctime(&ll.ll_tv.tv_sec));
1366 if (*ll.ll_host != '\0')
1367 (void)printf("from %.*s\n",
1368 (int)sizeof(ll.ll_host),
1369 ll.ll_host);
1370 else
1371 (void)printf("on %.*s\n",
1372 (int)sizeof(ll.ll_line),
1373 ll.ll_line);
1374 }
1375 #endif /* USE_PAM */
1376 }
1377 #endif /* __APPLE__ */
1378
1379 static void
1380 badlogin(char *name)
1381 {
1382
1383 if (failures == 0)
1384 return;
1385 if (hflag) {
1386 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
1387 failures, failures > 1 ? "S" : "", hostname);
1388 syslog(LOG_AUTHPRIV|LOG_NOTICE,
1389 "%d LOGIN FAILURE%s FROM %s, %s",
1390 failures, failures > 1 ? "S" : "", hostname, name);
1391 } else {
1392 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
1393 failures, failures > 1 ? "S" : "", tty);
1394 syslog(LOG_AUTHPRIV|LOG_NOTICE,
1395 "%d LOGIN FAILURE%s ON %s, %s",
1396 failures, failures > 1 ? "S" : "", tty, name);
1397 }
1398 failures = 0;
1399 }
1400
1401 const char *
1402 stypeof(char *ttyid)
1403 {
1404 struct ttyent *t;
1405
1406 if (ttyid != NULL && *ttyid != '\0') {
1407 t = getttynam(ttyid);
1408 if (t != NULL && t->ty_type != NULL)
1409 return (t->ty_type);
1410 }
1411 return (NULL);
1412 }
1413
1414 static void
1415 refused(const char *msg, const char *rtype, int lout)
1416 {
1417
1418 if (msg != NULL)
1419 printf("%s.\n", msg);
1420 if (hflag)
1421 syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s",
1422 pwd->pw_name, rtype, hostname, tty);
1423 else
1424 syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) ON TTY %s",
1425 pwd->pw_name, rtype, tty);
1426 if (lout)
1427 bail(SLEEP_EXIT, 1);
1428 }
1429
1430 #ifdef USE_PAM
1431 /*
1432 * Log a PAM error
1433 */
1434 static void
1435 pam_syslog(const char *msg)
1436 {
1437 syslog(LOG_ERR, "%s: %s", msg, pam_strerror(pamh, pam_err));
1438 }
1439
1440 /*
1441 * Shut down PAM
1442 */
1443 static void
1444 pam_cleanup()
1445 {
1446
1447 if (pamh != NULL) {
1448 if (pam_session_established) {
1449 pam_err = pam_close_session(pamh, 0);
1450 if (pam_err != PAM_SUCCESS)
1451 pam_syslog("pam_close_session()");
1452 }
1453 pam_session_established = 0;
1454 if (pam_cred_established) {
1455 pam_err = pam_setcred(pamh, pam_silent|PAM_DELETE_CRED);
1456 if (pam_err != PAM_SUCCESS)
1457 pam_syslog("pam_setcred()");
1458 }
1459 pam_cred_established = 0;
1460 pam_end(pamh, pam_err);
1461 pamh = NULL;
1462 }
1463 }
1464 #endif /* USE_PAM */
1465
1466 /*
1467 * Exit, optionally after sleeping a few seconds
1468 */
1469 void
1470 bail(int sec, int eval)
1471 {
1472
1473 #ifdef USE_PAM
1474 pam_cleanup();
1475 #endif /* USE_PAM */
1476 #ifdef USE_BSM_AUDIT
1477 if (pwd != NULL)
1478 audit_logout();
1479 #endif
1480 (void)sleep(sec);
1481 exit(eval);
1482 }