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.
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static char sccsid[] = "@(#)login.c 8.4 (Berkeley) 4/2/94";
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
52 * login -h hostname (for telnetd, etc.)
53 * login -f name (for pre-authenticated login: datakit, xterm, etc.)
56 #include <sys/param.h>
60 #include <sys/resource.h>
66 #include <login_cap.h>
77 #include <security/pam_appl.h>
78 #include <security/openpam.h>
81 #include "pathnames.h"
83 static int auth_pam(void);
84 static void bail(int, int);
85 static void bail_internal(int, int, int);
86 static int export(const char *);
87 static void export_pam_environment(void);
88 static int motd(const char *);
89 static void badlogin(char *);
90 static char *getloginname(void);
91 static void pam_syslog(const char *);
92 static void pam_cleanup(void);
93 static void refused(const char *, const char *, int);
94 static const char *stypeof(char *);
95 static void sigint(int);
96 static void timedout(int);
97 static void bail_sig(int);
98 static void usage(void);
100 #define TTYGRPNAME "tty" /* group to own ttys */
101 #define DEFAULT_BACKOFF 3
102 #define DEFAULT_RETRIES 10
103 #define DEFAULT_PROMPT "login: "
104 #define DEFAULT_PASSWD_PROMPT "Password:"
105 #define TERM_UNKNOWN "su"
106 #define DEFAULT_WARN (2L * 7L * 86400L) /* Two weeks */
107 #define NO_SLEEP_EXIT 0
111 * This bounds the time given to login. Not a define so it can
112 * be patched on machines where it's too small.
114 static u_int timeout = 300;
116 /* Buffer for signal handling of timeout */
117 static jmp_buf timeout_buf;
122 static char *envinit[1]; /* empty environment list */
125 * Command line flags and arguments
127 static int fflag; /* -f: do not perform authentication */
128 static int hflag; /* -h: login from remote host */
129 static char *hostname; /* hostname from command line */
130 static int pflag; /* -p: preserve environment */
135 static char *username; /* user name */
136 static char *olduser; /* previous user name */
141 static char default_prompt[] = DEFAULT_PROMPT;
142 static const char *prompt;
143 static char default_passwd_prompt[] = DEFAULT_PASSWD_PROMPT;
144 static const char *passwd_prompt;
151 static pam_handle_t *pamh = NULL;
152 static struct pam_conv pamc = { openpam_ttyconv, NULL };
154 static int pam_silent = PAM_SILENT;
155 static int pam_cred_established;
156 static int pam_session_established;
159 main(int argc, char *argv[])
163 int retries, backoff;
164 int ask, ch, cnt, quietlog, rootlogin, rval;
169 char tname[sizeof(_PATH_TTY) + 10];
172 const char *shell = NULL;
173 login_cap_t *lc = NULL;
174 login_cap_t *lc_user = NULL;
176 sigset_t mask, omask;
179 char auditsuccess = 1;
182 sa.sa_flags = SA_RESTART;
183 (void)sigfillset(&sa.sa_mask);
184 sa.sa_handler = SIG_IGN;
185 (void)sigaction(SIGQUIT, &sa, NULL);
186 (void)sigaction(SIGINT, &sa, NULL);
187 (void)sigaction(SIGHUP, &sa, NULL);
188 if (setjmp(timeout_buf)) {
191 (void)fprintf(stderr, "Login timed out after %d seconds\n",
193 bail(NO_SLEEP_EXIT, 0);
195 sa.sa_handler = timedout;
196 (void)sigaction(SIGALRM, &sa, NULL);
197 (void)alarm(timeout);
198 (void)setpriority(PRIO_PROCESS, 0, 0);
200 openlog("login", LOG_ODELAY, LOG_AUTH);
206 while ((ch = getopt(argc, argv, "fh:p")) != -1)
213 errx(1, "-h option: %s", strerror(EPERM));
214 if (strlen(optarg) >= MAXHOSTNAMELEN)
215 errx(1, "-h option: %s: exceeds maximum "
216 "hostname size", optarg);
226 syslog(LOG_ERR, "invalid flag %c", ch);
233 username = strdup(*argv);
234 if (username == NULL)
241 setproctitle("-%s", getprogname());
248 ttyn = ttyname(STDIN_FILENO);
249 if (ttyn == NULL || *ttyn == '\0') {
250 (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
253 if (strncmp(ttyn, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
254 tty = ttyn + sizeof _PATH_DEV - 1;
259 * Get "login-retries" & "login-backoff" from default class
261 lc = login_getclass(NULL);
262 prompt = login_getcapstr(lc, "login_prompt",
263 default_prompt, default_prompt);
264 passwd_prompt = login_getcapstr(lc, "passwd_prompt",
265 default_passwd_prompt, default_passwd_prompt);
266 retries = login_getcapnum(lc, "login-retries",
267 DEFAULT_RETRIES, DEFAULT_RETRIES);
268 backoff = login_getcapnum(lc, "login-backoff",
269 DEFAULT_BACKOFF, DEFAULT_BACKOFF);
274 * Try to authenticate the user until we succeed or time out.
276 for (cnt = 0;; ask = 1) {
282 username = getloginname();
287 * Note if trying multiple user names; log failures for
288 * previous user name, but don't bother logging one failure
289 * for nonexistent name (mistyped username).
291 if (failures && strcmp(olduser, username) != 0) {
292 if (failures > (pwd ? 0 : 1))
297 * Load the PAM policy and set some variables
299 pam_err = pam_start("login", username, &pamc, &pamh);
300 if (pam_err != PAM_SUCCESS) {
301 pam_syslog("pam_start()");
303 au_login_fail("PAM Error", 1);
305 bail(NO_SLEEP_EXIT, 1);
307 pam_err = pam_set_item(pamh, PAM_TTY, tty);
308 if (pam_err != PAM_SUCCESS) {
309 pam_syslog("pam_set_item(PAM_TTY)");
311 au_login_fail("PAM Error", 1);
313 bail(NO_SLEEP_EXIT, 1);
315 pam_err = pam_set_item(pamh, PAM_RHOST, hostname);
316 if (pam_err != PAM_SUCCESS) {
317 pam_syslog("pam_set_item(PAM_RHOST)");
319 au_login_fail("PAM Error", 1);
321 bail(NO_SLEEP_EXIT, 1);
324 pwd = getpwnam(username);
325 if (pwd != NULL && pwd->pw_uid == 0)
329 * If the -f option was specified and the caller is
330 * root or the caller isn't changing their uid, don't
333 if (pwd != NULL && fflag &&
334 (uid == (uid_t)0 || uid == (uid_t)pwd->pw_uid)) {
335 /* already authenticated */
338 auditsuccess = 0; /* opened a terminal window only */
342 (void)setpriority(PRIO_PROCESS, 0, -4);
344 (void)setpriority(PRIO_PROCESS, 0, 0);
347 if (pwd && rval == 0)
353 * We are not exiting here, but this corresponds to a failed
354 * login event, so set exitstatus to 1.
357 au_login_fail("Login incorrect", 1);
360 (void)printf("Login incorrect\n");
366 * Allow up to 'retry' (10) attempts, but start
367 * backing off after 'backoff' (3) attempts.
369 if (++cnt > backoff) {
370 if (cnt >= retries) {
374 sleep((u_int)((cnt - backoff) * 5));
378 /* committed to login -- turn off timeout */
379 (void)alarm((u_int)0);
381 (void)sigemptyset(&mask);
382 (void)sigaddset(&mask, SIGHUP);
383 (void)sigaddset(&mask, SIGTERM);
384 (void)sigprocmask(SIG_BLOCK, &mask, &omask);
385 sa.sa_handler = bail_sig;
386 (void)sigaction(SIGHUP, &sa, NULL);
387 (void)sigaction(SIGTERM, &sa, NULL);
392 /* Audit successful login. */
398 * This needs to happen before login_getpwclass to support
399 * home directories on GSS-API authenticated NFS where the
400 * kerberos credentials need to be saved so that the kernel
401 * can authenticate to the NFS server.
403 pam_err = pam_setcred(pamh, pam_silent|PAM_ESTABLISH_CRED);
404 if (pam_err != PAM_SUCCESS) {
405 pam_syslog("pam_setcred()");
406 bail(NO_SLEEP_EXIT, 1);
408 pam_cred_established = 1;
411 * Establish the login class.
413 lc = login_getpwclass(pwd);
414 lc_user = login_getuserclass(pwd);
416 if (!(quietlog = login_getcapbool(lc_user, "hushlogin", 0)))
417 quietlog = login_getcapbool(lc, "hushlogin", 0);
420 * Switching needed for NFS with root access disabled.
422 * XXX: This change fails to modify the additional groups for the
423 * process, and as such, may restrict rights normally granted
424 * through those groups.
426 (void)setegid(pwd->pw_gid);
427 (void)seteuid(rootlogin ? 0 : pwd->pw_uid);
428 if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) {
429 if (login_getcapbool(lc, "requirehome", 0))
430 refused("Home directory not available", "HOMEDIR", 1);
432 refused("Cannot find root directory", "ROOTDIR", 1);
433 if (!quietlog || *pwd->pw_dir)
434 printf("No home directory.\nLogging in with home = \"/\".\n");
435 pwd->pw_dir = strdup("/");
436 if (pwd->pw_dir == NULL) {
437 syslog(LOG_NOTICE, "strdup(): %m");
444 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
449 shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
450 if (*pwd->pw_shell == '\0')
451 pwd->pw_shell = strdup(_PATH_BSHELL);
452 if (pwd->pw_shell == NULL) {
453 syslog(LOG_NOTICE, "strdup(): %m");
456 if (*shell == '\0') /* Not overridden */
457 shell = pwd->pw_shell;
458 if ((shell = strdup(shell)) == NULL) {
459 syslog(LOG_NOTICE, "strdup(): %m");
464 * Set device protections, depending on what terminal the
465 * user is logged in. This feature is used on Suns to give
466 * console users better privacy.
468 login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
471 * Clear flags of the tty. None should be set, and when the
472 * user sets them otherwise, this can cause the chown to fail.
473 * Since it isn't clear that flags are useful on character
474 * devices, we just clear them.
476 * We don't log in the case of EOPNOTSUPP because dev might be
477 * on NFS, which doesn't support chflags.
479 * We don't log in the EROFS because that means that /dev is on
480 * a read only file system and we assume that the permissions there
483 if (ttyn != tname && chflags(ttyn, 0))
484 if (errno != EOPNOTSUPP && errno != EROFS)
485 syslog(LOG_ERR, "chflags(%s): %m", ttyn);
486 if (ttyn != tname && chown(ttyn, pwd->pw_uid,
487 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid))
489 syslog(LOG_ERR, "chown(%s): %m", ttyn);
492 * Exclude cons/vt/ptys only, assume dialup otherwise
493 * TODO: Make dialup tty determination a library call
494 * for consistency (finger etc.)
496 if (hflag && isdialuptty(tty))
497 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
501 * Syslog each successful login, so we don't have to watch
502 * hundreds of wtmp or lastlogin files.
505 syslog(LOG_INFO, "login from %s on %s as %s",
506 hostname, tty, pwd->pw_name);
508 syslog(LOG_INFO, "login on %s as %s",
513 * If fflag is on, assume caller/authenticator has logged root
516 if (rootlogin && fflag == 0) {
518 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
519 username, tty, hostname);
521 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
526 * Destroy environment unless user has requested its
527 * preservation - but preserve TERM in all cases
529 term = getenv("TERM");
533 setenv("TERM", term, 0);
536 * PAM modules might add supplementary groups during pam_setcred().
538 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
539 syslog(LOG_ERR, "setusercontext() failed - exiting");
540 bail(NO_SLEEP_EXIT, 1);
543 pam_err = pam_setcred(pamh, pam_silent|PAM_REINITIALIZE_CRED);
544 if (pam_err != PAM_SUCCESS) {
545 pam_syslog("pam_setcred()");
546 bail(NO_SLEEP_EXIT, 1);
549 pam_err = pam_open_session(pamh, pam_silent);
550 if (pam_err != PAM_SUCCESS) {
551 pam_syslog("pam_open_session()");
552 bail(NO_SLEEP_EXIT, 1);
554 pam_session_established = 1;
557 * We must fork() before setuid() because we need to call
558 * pam_close_session() as root.
563 } else if (pid != 0) {
565 * Parent: wait for child to finish, then clean up
568 * If we get SIGHUP or SIGTERM, clean up the session
569 * and exit right away. This will make the terminal
570 * inaccessible and send SIGHUP to the foreground
574 setproctitle("-%s [pam]", getprogname());
575 (void)sigprocmask(SIG_SETMASK, &omask, NULL);
576 waitpid(pid, &status, 0);
577 (void)sigprocmask(SIG_BLOCK, &mask, NULL);
578 bail(NO_SLEEP_EXIT, 0);
582 * NOTICE: We are now in the child process!
586 * Add any environment variables the PAM modules may have set.
588 export_pam_environment();
591 * We're done with PAM now; our parent will deal with the rest.
597 * We don't need to be root anymore, so set the login name and
600 if (setlogin(username) != 0) {
601 syslog(LOG_ERR, "setlogin(%s): %m - exiting", username);
602 bail(NO_SLEEP_EXIT, 1);
604 if (setusercontext(lc, pwd, pwd->pw_uid,
605 LOGIN_SETALL & ~(LOGIN_SETLOGIN|LOGIN_SETGROUP)) != 0) {
606 syslog(LOG_ERR, "setusercontext() failed - exiting");
610 (void)setenv("SHELL", pwd->pw_shell, 1);
611 (void)setenv("HOME", pwd->pw_dir, 1);
612 /* Overwrite "term" from login.conf(5) for any known TERM */
613 if (term == NULL && (tp = stypeof(tty)) != NULL)
614 (void)setenv("TERM", tp, 1);
616 (void)setenv("TERM", TERM_UNKNOWN, 0);
617 (void)setenv("LOGNAME", username, 1);
618 (void)setenv("USER", username, 1);
619 (void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
624 cw = login_getcapstr(lc, "welcome", NULL, NULL);
625 if (cw != NULL && access(cw, F_OK) == 0)
628 motd(_PATH_MOTDFILE);
630 if (login_getcapbool(lc_user, "nocheckmail", 0) == 0 &&
631 login_getcapbool(lc, "nocheckmail", 0) == 0) {
634 /* $MAIL may have been set by class. */
637 asprintf(&cx, "%s/%s",
638 _PATH_MAILDIR, pwd->pw_name);
640 if (cx && stat(cx, &st) == 0 && st.st_size != 0)
641 (void)printf("You have %smail.\n",
642 (st.st_mtime > st.st_atime) ? "new " : "");
643 if (getenv("MAIL") == NULL)
648 login_close(lc_user);
651 sa.sa_handler = SIG_DFL;
652 (void)sigaction(SIGALRM, &sa, NULL);
653 (void)sigaction(SIGQUIT, &sa, NULL);
654 (void)sigaction(SIGINT, &sa, NULL);
655 (void)sigaction(SIGTERM, &sa, NULL);
656 (void)sigaction(SIGHUP, &sa, NULL);
657 sa.sa_handler = SIG_IGN;
658 (void)sigaction(SIGTSTP, &sa, NULL);
659 (void)sigprocmask(SIG_SETMASK, &omask, NULL);
662 * Login shells have a leading '-' in front of argv[0]
664 p = strrchr(pwd->pw_shell, '/');
665 if (asprintf(&arg0, "-%s", p ? p + 1 : pwd->pw_shell) >= MAXPATHLEN) {
666 syslog(LOG_ERR, "user: %s: shell exceeds maximum pathname size",
668 errx(1, "shell exceeds maximum pathname size");
669 } else if (arg0 == NULL) {
670 err(1, "asprintf()");
673 execlp(shell, arg0, (char *)0);
682 * Attempt to authenticate the user using PAM. Returns 0 if the user is
683 * authenticated, or 1 if not authenticated. If some sort of PAM system
684 * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
685 * function returns -1. This can be used as an indication that we should
686 * fall back to a different authentication mechanism.
691 const char *tmpl_user;
695 pam_err = pam_authenticate(pamh, pam_silent);
700 * With PAM we support the concept of a "template"
701 * user. The user enters a login name which is
702 * authenticated by PAM, usually via a remote service
703 * such as RADIUS or TACACS+. If authentication
704 * succeeds, a different but related "template" name
705 * is used for setting the credentials, shell, and
706 * home directory. The name the user enters need only
707 * exist on the remote authentication server, but the
708 * template name must be present in the local password
711 * This is supported by two various mechanisms in the
712 * individual modules. However, from the application's
713 * point of view, the template user is always passed
714 * back as a changed value of the PAM_USER item.
716 pam_err = pam_get_item(pamh, PAM_USER, &item);
717 if (pam_err == PAM_SUCCESS) {
718 tmpl_user = (const char *)item;
719 if (strcmp(username, tmpl_user) != 0)
720 pwd = getpwnam(tmpl_user);
722 pam_syslog("pam_get_item(PAM_USER)");
728 case PAM_USER_UNKNOWN:
734 pam_syslog("pam_authenticate()");
740 pam_err = pam_acct_mgmt(pamh, pam_silent);
744 case PAM_NEW_AUTHTOK_REQD:
745 pam_err = pam_chauthtok(pamh,
746 pam_silent|PAM_CHANGE_EXPIRED_AUTHTOK);
747 if (pam_err != PAM_SUCCESS) {
748 pam_syslog("pam_chauthtok()");
753 pam_syslog("pam_acct_mgmt()");
760 pam_end(pamh, pam_err);
767 * Export any environment variables PAM modules may have set
770 export_pam_environment(void)
775 pam_env = pam_getenvlist(pamh);
776 if (pam_env != NULL) {
777 for (pp = pam_env; *pp != NULL; pp++) {
785 * Perform sanity checks on an environment variable:
786 * - Make sure there is an '=' in the string.
787 * - Make sure the string doesn't run on too long.
788 * - Do not export certain variables. This list was taken from the
789 * Solaris pam_putenv(3) man page.
793 export(const char *s)
795 static const char *noexport[] = {
796 "SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH",
803 if (strlen(s) > 1024 || (p = strchr(s, '=')) == NULL)
805 if (strncmp(s, "LD_", 3) == 0)
807 for (pp = noexport; *pp != NULL; pp++) {
809 if (s[n] == '=' && strncmp(s, *pp, n) == 0)
813 (void)setenv(s, p + 1, 1);
822 (void)fprintf(stderr, "usage: login [-fp] [-h hostname] [username]\n");
827 * Prompt user and read login name from stdin.
835 nbuf = malloc(MAXLOGNAME);
839 (void)printf("%s", prompt);
840 for (p = nbuf; (ch = getchar()) != '\n'; ) {
843 bail(NO_SLEEP_EXIT, 0);
845 if (p < nbuf + MAXLOGNAME - 1)
851 if (nbuf[0] == '-') {
853 memmove(nbuf, nbuf + 1, strlen(nbuf));
855 pam_silent = PAM_SILENT;
861 * SIGINT handler for motd().
863 static volatile int motdinterrupt;
865 sigint(int signo __unused)
871 * Display the contents of a file (such as /etc/motd).
874 motd(const char *motdfile)
876 struct sigaction newint, oldint;
880 if ((f = fopen(motdfile, "r")) == NULL)
883 newint.sa_handler = sigint;
885 sigfillset(&newint.sa_mask);
886 sigaction(SIGINT, &newint, &oldint);
887 while ((ch = fgetc(f)) != EOF && !motdinterrupt)
889 sigaction(SIGINT, &oldint, NULL);
890 if (ch != EOF || ferror(f)) {
899 * SIGALRM handler, to enforce login prompt timeout.
901 * XXX This can potentially confuse the hell out of PAM. We should
902 * XXX instead implement a conversation function that returns
903 * XXX PAM_CONV_ERR when interrupted by a signal, and have the signal
904 * XXX handler just set a flag.
907 timedout(int signo __unused)
910 longjmp(timeout_buf, signo);
920 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
921 failures, failures > 1 ? "S" : "", hostname);
922 syslog(LOG_AUTHPRIV|LOG_NOTICE,
923 "%d LOGIN FAILURE%s FROM %s, %s",
924 failures, failures > 1 ? "S" : "", hostname, name);
926 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
927 failures, failures > 1 ? "S" : "", tty);
928 syslog(LOG_AUTHPRIV|LOG_NOTICE,
929 "%d LOGIN FAILURE%s ON %s, %s",
930 failures, failures > 1 ? "S" : "", tty, name);
940 if (ttyid != NULL && *ttyid != '\0') {
941 t = getttynam(ttyid);
942 if (t != NULL && t->ty_type != NULL)
949 refused(const char *msg, const char *rtype, int lout)
953 printf("%s.\n", msg);
955 syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s",
956 pwd->pw_name, rtype, hostname, tty);
958 syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) ON TTY %s",
959 pwd->pw_name, rtype, tty);
968 pam_syslog(const char *msg)
970 syslog(LOG_ERR, "%s: %s", msg, pam_strerror(pamh, pam_err));
981 if (pam_session_established) {
982 pam_err = pam_close_session(pamh, 0);
983 if (pam_err != PAM_SUCCESS)
984 pam_syslog("pam_close_session()");
986 pam_session_established = 0;
987 if (pam_cred_established) {
988 pam_err = pam_setcred(pamh, pam_silent|PAM_DELETE_CRED);
989 if (pam_err != PAM_SUCCESS)
990 pam_syslog("pam_setcred()");
992 pam_cred_established = 0;
993 pam_end(pamh, pam_err);
999 bail_internal(int sec, int eval, int signo)
1001 struct sigaction sa;
1004 #ifdef USE_BSM_AUDIT
1012 sa.sa_handler = SIG_DFL;
1014 (void)sigemptyset(&sa.sa_mask);
1015 (void)sigaction(signo, &sa, NULL);
1016 (void)sigaddset(&sa.sa_mask, signo);
1017 (void)sigprocmask(SIG_UNBLOCK, &sa.sa_mask, NULL);
1024 * Exit, optionally after sleeping a few seconds
1027 bail(int sec, int eval)
1029 bail_internal(sec, eval, 0);
1033 * Exit because of a signal.
1034 * This is not async-signal safe, so only call async-signal safe functions
1035 * while the signal is unmasked.
1040 bail_internal(NO_SLEEP_EXIT, 0, signo);