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.
11 * Portions copyright (c) 1999-2007 Apple Inc. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
44 static char sccsid
[] = "@(#)login.c 8.4 (Berkeley) 4/2/94";
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 $");
53 * login -h hostname (for telnetd, etc.)
54 * login -f name (for pre-authenticated login: datakit, xterm, etc.)
58 #include <sys/copyright.h>
61 #include <TargetConditionals.h>
63 #include <sys/param.h>
67 #include <sys/resource.h>
79 #include <login_cap.h>
95 #define _UTX_USERSIZE MAXLOGNAME
98 #endif /* __APPLE__ */
100 #include <sys/types.h>
101 #include <sys/socket.h>
102 #include <netinet/in.h>
103 #include <arpa/inet.h>
107 #include <bsm/libbsm.h>
108 #include <bsm/audit.h>
109 #include <bsm/audit_session.h>
110 #include <bsm/audit_uevents.h>
114 #include <mach/mach_types.h>
115 #include <mach/task.h>
116 #include <mach/mach_init.h>
117 #include <servers/bootstrap.h>
119 #include <sys/file.h>
121 #endif /* __APPLE__ */
124 #include <security/pam_appl.h>
125 #include <security/openpam.h>
129 #include "pathnames.h"
132 static int auth_pam(int skip_auth
);
134 static void bail(int, int);
136 static int export(const char *);
137 static void export_pam_environment(void);
139 static int motd(const char *);
140 static void badlogin(char *);
141 static char *getloginname(void);
143 static void pam_syslog(const char *);
144 static void pam_cleanup(void);
146 static void refused(const char *, const char *, int);
147 static const char *stypeof(char *);
148 static void sigint(int);
149 static void timedout(int);
150 static void usage(void);
153 static void dolastlog(int);
154 static void handle_sighup(int);
157 static void checknologin(void);
158 static int rootterm(const char *);
159 #endif /* !USE_PAM */
160 #endif /* __APPLE__ */
162 #define TTYGRPNAME "tty" /* group to own ttys */
163 #define DEFAULT_BACKOFF 3
164 #define DEFAULT_RETRIES 10
165 #define DEFAULT_PROMPT "login: "
166 #define DEFAULT_PASSWD_PROMPT "Password:"
167 #define TERM_UNKNOWN "su"
168 #define DEFAULT_WARN (2L * 7L * 86400L) /* Two weeks */
169 #define NO_SLEEP_EXIT 0
173 * This bounds the time given to login. Not a define so it can
174 * be patched on machines where it's too small.
176 static u_int timeout
= 300;
178 /* Buffer for signal handling of timeout */
179 static jmp_buf timeout_buf
;
184 static char *envinit
[1]; /* empty environment list */
187 * Command line flags and arguments
189 static int fflag
; /* -f: do not perform authentication */
191 static int lflag
; /* -l: login session to the commmand that follows username */
193 static int hflag
; /* -h: login from remote host */
194 static char *hostname
; /* hostname from command line */
195 static int pflag
; /* -p: preserve environment */
200 static char *username
; /* user name */
201 static char *olduser
; /* previous user name */
206 static char default_prompt
[] = DEFAULT_PROMPT
;
207 static const char *prompt
;
208 static char default_passwd_prompt
[] = DEFAULT_PASSWD_PROMPT
;
209 static const char *passwd_prompt
;
217 static pam_handle_t
*pamh
= NULL
;
218 static struct pam_conv pamc
= { openpam_ttyconv
, NULL
};
220 static int pam_silent
= PAM_SILENT
;
221 static int pam_cred_established
;
222 static int pam_session_established
;
229 static struct lastlogx lastlog
;
233 extern au_tid_addr_t tid
;
234 #endif /* USE_BSM_AUDIT */
235 #endif /* __APPLE__ */
238 main(int argc
, char *argv
[])
242 int retries
, backoff
;
243 int ask
, ch
, cnt
, quietlog
= 0, rootlogin
, rval
;
248 char tname
[sizeof(_PATH_TTY
) + 10];
254 const char *name
= "login"; /* PAM config */
258 const char *shell
= NULL
;
259 #endif /* !__APPLE__ */
261 login_cap_t
*lc
= NULL
;
262 login_cap_t
*lc_user
= NULL
;
263 #endif /* LOGIN_CAP */
268 char auditsuccess
= 1;
271 (void)signal(SIGQUIT
, SIG_IGN
);
272 (void)signal(SIGINT
, SIG_IGN
);
273 (void)signal(SIGHUP
, SIG_IGN
);
274 if (setjmp(timeout_buf
)) {
277 (void)fprintf(stderr
, "Login timed out after %d seconds\n",
279 bail(NO_SLEEP_EXIT
, 0);
281 (void)signal(SIGALRM
, timedout
);
282 (void)alarm(timeout
);
284 prio
= getpriority(PRIO_PROCESS
, 0);
286 (void)setpriority(PRIO_PROCESS
, 0, 0);
288 openlog("login", LOG_ODELAY
, LOG_AUTH
);
295 while ((ch
= getopt(argc
, argv
, "1fh:lpq")) != -1)
297 while ((ch
= getopt(argc
, argv
, "fh:p")) != -1)
305 errx(1, "-h option: %s", strerror(EPERM
));
306 if (strlen(optarg
) >= MAXHOSTNAMELEN
)
307 errx(1, "-h option: %s: exceeds maximum "
308 "hostname size", optarg
);
328 syslog(LOG_ERR
, "invalid flag %c", ch
);
335 username
= strdup(*argv
);
336 if (username
== NULL
)
341 #endif /* __APPLE__ */
347 setproctitle("-%s", getprogname());
348 #endif /* !__APPLE__ */
350 for (cnt
= getdtablesize(); cnt
> 2; cnt
--)
356 ttyn
= ttyname(STDIN_FILENO
);
357 if (ttyn
== NULL
|| *ttyn
== '\0') {
358 (void)snprintf(tname
, sizeof(tname
), "%s??", _PATH_TTY
);
361 if ((tty
= strrchr(ttyn
, '/')) != NULL
)
368 * Get "login-retries" & "login-backoff" from default class
370 lc
= login_getclass(NULL
);
371 prompt
= login_getcapstr(lc
, "login_prompt",
372 default_prompt
, default_prompt
);
373 passwd_prompt
= login_getcapstr(lc
, "passwd_prompt",
374 default_passwd_prompt
, default_passwd_prompt
);
375 retries
= login_getcapnum(lc
, "login-retries",
376 DEFAULT_RETRIES
, DEFAULT_RETRIES
);
377 backoff
= login_getcapnum(lc
, "login-backoff",
378 DEFAULT_BACKOFF
, DEFAULT_BACKOFF
);
381 #else /* !LOGIN_CAP */
382 prompt
= default_prompt
;
383 passwd_prompt
= default_passwd_prompt
;
384 retries
= DEFAULT_RETRIES
;
385 backoff
= DEFAULT_BACKOFF
;
386 #endif /* !LOGIN_CAP */
390 /* Set the terminal id */
392 audit_set_terminal_id(&old_tid
);
393 tid
.at_type
= AU_IPv4
;
394 tid
.at_addr
[0] = old_tid
.machine
;
395 if (fstat(STDIN_FILENO
, &st
) < 0) {
396 fprintf(stderr
, "login: Unable to stat terminal\n");
397 au_login_fail("Unable to stat terminal", 1);
400 if (S_ISCHR(st
.st_mode
)) {
401 tid
.at_port
= st
.st_rdev
;
405 #endif /* USE_BSM_AUDIT */
406 #endif /* __APPLE__ */
409 * Try to authenticate the user until we succeed or time out.
411 for (cnt
= 0;; ask
= 1) {
417 username
= getloginname();
422 if (strlen(username
) > _UTX_USERSIZE
)
423 username
[_UTX_USERSIZE
] = '\0';
424 #endif /* __APPLE__ */
427 * Note if trying multiple user names; log failures for
428 * previous user name, but don't bother logging one failure
429 * for nonexistent name (mistyped username).
431 if (failures
&& strcmp(olduser
, username
) != 0) {
432 if (failures
> (pwd
? 0 : 1))
438 /* get lastlog info before PAM make a new entry */
440 getlastlogxbyname(username
, &lastlog
);
442 #endif /* __APPLE__ */
444 pwd
= getpwnam(username
);
448 * Load the PAM policy and set some variables
451 if (fflag
&& (pwd
!= NULL
) && (pwd
->pw_uid
== uid
)) {
455 pam_err
= pam_start(name
, username
, &pamc
, &pamh
);
456 if (pam_err
!= PAM_SUCCESS
) {
457 pam_syslog("pam_start()");
459 au_login_fail("PAM Error", 1);
461 bail(NO_SLEEP_EXIT
, 1);
463 pam_err
= pam_set_item(pamh
, PAM_TTY
, tty
);
464 if (pam_err
!= PAM_SUCCESS
) {
465 pam_syslog("pam_set_item(PAM_TTY)");
467 au_login_fail("PAM Error", 1);
469 bail(NO_SLEEP_EXIT
, 1);
471 pam_err
= pam_set_item(pamh
, PAM_RHOST
, hostname
);
472 if (pam_err
!= PAM_SUCCESS
) {
473 pam_syslog("pam_set_item(PAM_RHOST)");
475 au_login_fail("PAM Error", 1);
477 bail(NO_SLEEP_EXIT
, 1);
481 if (pwd
!= NULL
&& pwd
->pw_uid
== 0)
485 * If the -f option was specified and the caller is
486 * root or the caller isn't changing their uid, don't
489 if (pwd
!= NULL
&& fflag
&&
490 (uid
== (uid_t
)0 || uid
== (uid_t
)pwd
->pw_uid
)) {
492 rval
= auth_pam(fflag
);
497 auditsuccess
= 0; /* opened a terminal window only */
502 /* If the account doesn't have a password, authenticate. */
503 } else if (pwd
!= NULL
&& pwd
->pw_passwd
[0] == '\0') {
505 #endif /* !USE_PAM */
506 #endif /* __APPLE__ */
509 (void)setpriority(PRIO_PROCESS
, 0, -4);
511 rval
= auth_pam(fflag
);
514 char* salt
= pwd
->pw_passwd
;
515 char* p
= getpass(passwd_prompt
);
516 rval
= strcmp(crypt(p
, salt
), salt
);
517 memset(p
, 0, strlen(p
));
520 (void)setpriority(PRIO_PROCESS
, 0, 0);
528 * If trying to log in as root but with insecure terminal,
529 * refuse the login attempt.
531 if (pwd
&& rootlogin
&& !rootterm(tty
)) {
532 refused("root login refused on this terminal", "ROOTTERM", 0);
534 au_login_fail("Login refused on terminal", 0);
538 #endif /* !USE_PAM */
539 #endif /* __APPLE__ */
541 if (pwd
&& rval
== 0)
549 * We are not exiting here, but this corresponds to a failed
550 * login event, so set exitstatus to 1.
553 au_login_fail("Login incorrect", 1);
556 (void)printf("Login incorrect\n");
562 * Allow up to 'retry' (10) attempts, but start
563 * backing off after 'backoff' (3) attempts.
565 if (++cnt
> backoff
) {
566 if (cnt
>= retries
) {
570 sleep((u_int
)((cnt
- backoff
) * 5));
574 /* committed to login -- turn off timeout */
575 (void)alarm((u_int
)0);
576 (void)signal(SIGHUP
, SIG_DFL
);
582 fprintf(stderr
, "login: Unable to find user: %s\n", username
);
587 /* if user not super-user, check for disabled logins */
590 #endif /* !USE_PAM */
594 /* Audit successful login. */
596 au_login_success(fflag
);
601 * Establish the login class.
603 lc
= login_getpwclass(pwd
);
604 lc_user
= login_getuserclass(pwd
);
606 if (!(quietlog
= login_getcapbool(lc_user
, "hushlogin", 0)))
607 quietlog
= login_getcapbool(lc
, "hushlogin", 0);
608 #endif /* LOGIN_CAP */
612 * Switching needed for NFS with root access disabled.
614 * XXX: This change fails to modify the additional groups for the
615 * process, and as such, may restrict rights normally granted
616 * through those groups.
618 (void)setegid(pwd
->pw_gid
);
619 (void)seteuid(rootlogin
? 0 : pwd
->pw_uid
);
621 if (!*pwd
->pw_dir
|| chdir(pwd
->pw_dir
) < 0) {
623 if (login_getcapbool(lc
, "requirehome", 0))
624 refused("Home directory not available", "HOMEDIR", 1);
625 #endif /* LOGIN_CAP */
627 refused("Cannot find root directory", "ROOTDIR", 1);
628 if (!quietlog
|| *pwd
->pw_dir
)
629 printf("No home directory.\nLogging in with home = \"/\".\n");
630 pwd
->pw_dir
= strdup("/");
631 if (pwd
->pw_dir
== NULL
) {
632 syslog(LOG_NOTICE
, "strdup(): %m");
639 #endif /* !__APPLE__ */
641 quietlog
= access(_PATH_HUSHLOGIN
, F_OK
) == 0;
649 /* Nothing else left to fail -- really log in. */
651 memset((void *)&utmp
, 0, sizeof(utmp
));
652 (void)gettimeofday(&utmp
.ut_tv
, NULL
);
653 (void)strncpy(utmp
.ut_user
, username
, sizeof(utmp
.ut_user
));
655 (void)strncpy(utmp
.ut_host
, hostname
, sizeof(utmp
.ut_host
));
656 (void)strncpy(utmp
.ut_line
, tty
, sizeof(utmp
.ut_line
));
657 utmp
.ut_type
= USER_PROCESS
| UTMPX_AUTOFILL_MASK
;
658 utmp
.ut_pid
= getpid();
663 #endif /* !__APPLE__ */
665 shell
= login_getcapstr(lc
, "shell", pwd
->pw_shell
, pwd
->pw_shell
);
666 #endif /* !LOGIN_CAP */
667 if (*pwd
->pw_shell
== '\0')
668 pwd
->pw_shell
= strdup(_PATH_BSHELL
);
669 if (pwd
->pw_shell
== NULL
) {
670 syslog(LOG_NOTICE
, "strdup(): %m");
674 #if defined(__APPLE__) && TARGET_OS_EMBEDDED
675 /* on embedded, allow a shell to live in /var/debug_mount/bin/sh */
676 #define _PATH_DEBUGSHELL "/var/debug_mount/bin/sh"
677 if (stat(pwd
->pw_shell
, &st
) != 0) {
678 if (stat(_PATH_DEBUGSHELL
, &st
) == 0) {
679 pwd
->pw_shell
= strdup(_PATH_DEBUGSHELL
);
684 if (*shell
== '\0') /* Not overridden */
685 shell
= pwd
->pw_shell
;
686 if ((shell
= strdup(shell
)) == NULL
) {
687 syslog(LOG_NOTICE
, "strdup(): %m");
697 * Set device protections, depending on what terminal the
698 * user is logged in. This feature is used on Suns to give
699 * console users better privacy.
701 login_fbtab(tty
, pwd
->pw_uid
, pwd
->pw_gid
);
702 #endif /* !__APPLE__ */
705 * Clear flags of the tty. None should be set, and when the
706 * user sets them otherwise, this can cause the chown to fail.
707 * Since it isn't clear that flags are useful on character
708 * devices, we just clear them.
710 * We don't log in the case of EOPNOTSUPP because dev might be
711 * on NFS, which doesn't support chflags.
713 * We don't log in the EROFS because that means that /dev is on
714 * a read only file system and we assume that the permissions there
717 if (ttyn
!= tname
&& chflags(ttyn
, 0))
719 if (errno
!= EOPNOTSUPP
&& errno
!= ENOTSUP
&& errno
!= EROFS
)
721 if (errno
!= EOPNOTSUPP
&& errno
!= EROFS
)
723 syslog(LOG_ERR
, "chflags(%s): %m", ttyn
);
724 if (ttyn
!= tname
&& chown(ttyn
, pwd
->pw_uid
,
725 (gr
= getgrnam(TTYGRPNAME
)) ? gr
->gr_gid
: pwd
->pw_gid
))
727 syslog(LOG_ERR
, "chown(%s): %m", ttyn
);
730 (void)chmod(ttyn
, 0620);
731 #endif /* __APPLE__ */
735 * Exclude cons/vt/ptys only, assume dialup otherwise
736 * TODO: Make dialup tty determination a library call
737 * for consistency (finger etc.)
739 if (hflag
&& isdialuptty(tty
))
740 syslog(LOG_INFO
, "DIALUP %s, %s", tty
, pwd
->pw_name
);
741 #endif /* !__APPLE__ */
745 * Syslog each successful login, so we don't have to watch
746 * hundreds of wtmp or lastlogin files.
749 syslog(LOG_INFO
, "login from %s on %s as %s",
750 hostname
, tty
, pwd
->pw_name
);
752 syslog(LOG_INFO
, "login on %s as %s",
757 * If fflag is on, assume caller/authenticator has logged root
760 if (rootlogin
&& fflag
== 0) {
762 syslog(LOG_NOTICE
, "ROOT LOGIN (%s) ON %s FROM %s",
763 username
, tty
, hostname
);
765 syslog(LOG_NOTICE
, "ROOT LOGIN (%s) ON %s",
770 * Destroy environment unless user has requested its
771 * preservation - but preserve TERM in all cases
773 term
= getenv("TERM");
777 setenv("TERM", term
, 0);
781 * PAM modules might add supplementary groups during pam_setcred().
783 if (setusercontext(lc
, pwd
, pwd
->pw_uid
, LOGIN_SETGROUP
) != 0) {
784 syslog(LOG_ERR
, "setusercontext() failed - exiting");
785 bail(NO_SLEEP_EXIT
, 1);
787 #endif /* !__APPLE__ */
790 pam_err
= pam_setcred(pamh
, pam_silent
|PAM_ESTABLISH_CRED
);
791 if (pam_err
!= PAM_SUCCESS
) {
792 pam_syslog("pam_setcred()");
793 bail(NO_SLEEP_EXIT
, 1);
795 pam_cred_established
= 1;
798 pam_err
= pam_open_session(pamh
, pam_silent
);
799 if (pam_err
!= PAM_SUCCESS
) {
800 pam_syslog("pam_open_session()");
801 bail(NO_SLEEP_EXIT
, 1);
803 pam_session_established
= 1;
807 /* <rdar://problem/5377791>
808 Install a signal handler that will forward SIGHUP to the
809 child and process group. The parent should not exit on
810 SIGHUP so that the tty ownership can be reset. */
811 (void)signal(SIGHUP
, handle_sighup
);
812 #endif /* __APPLE__ */
815 * We must fork() before setuid() because we need to call
816 * pam_close_session() as root.
821 } else if (pid
!= 0) {
823 * Parent: wait for child to finish, then clean up
828 setproctitle("-%s [pam]", getprogname());
829 #endif /* !__APPLE__ */
831 /* Our SIGHUP handler may interrupt the wait */
834 res
= waitpid(pid
, &status
, 0);
835 } while (res
== -1 && errno
== EINTR
);
837 waitpid(pid
, &status
, 0);
842 #endif /* __APPLE__ */
843 bail(NO_SLEEP_EXIT
, 0);
847 * NOTICE: We are now in the child process!
851 /* Restore the default SIGHUP handler for the child. */
852 (void)signal(SIGHUP
, SIG_DFL
);
853 #endif /* __APPLE__ */
857 * Add any environment variables the PAM modules may have set.
859 export_pam_environment();
862 * We're done with PAM now; our parent will deal with the rest.
869 * We don't need to be root anymore, so set the login name and
872 if (setlogin(username
) != 0) {
873 syslog(LOG_ERR
, "setlogin(%s): %m - exiting", username
);
874 bail(NO_SLEEP_EXIT
, 1);
877 /* <rdar://problem/6041650> restore process priority if not changing uids */
878 if (uid
== (uid_t
)pwd
->pw_uid
) {
879 (void)setpriority(PRIO_PROCESS
, 0, prio
);
882 (void)setgid(pwd
->pw_gid
);
883 if (initgroups(username
, pwd
->pw_gid
) == -1)
884 syslog(LOG_ERR
, "login: initgroups() failed");
885 (void) setuid(rootlogin
? 0 : pwd
->pw_uid
);
886 #else /* !__APPLE__ */
887 if (setusercontext(lc
, pwd
, pwd
->pw_uid
,
888 LOGIN_SETALL
& ~(LOGIN_SETLOGIN
|LOGIN_SETGROUP
)) != 0) {
889 syslog(LOG_ERR
, "setusercontext() failed - exiting");
892 #endif /* !__APPLE__ */
895 /* We test for the home directory after pam_open_session(3)
896 * as the home directory may have been mounted by a session
897 * module, and after changing uid as the home directory may
898 * be NFS with root access disabled. */
900 /* First do a stat in case the homedir is automounted */
901 stat(pwd
->pw_dir
,&st
);
902 if (!*pwd
->pw_dir
|| chdir(pwd
->pw_dir
) < 0) {
903 printf("No home directory: %s\n", pwd
->pw_dir
);
904 if (chdir("/") < 0) {
905 refused("Cannot find root directory", "ROOTDIR", 0);
908 pwd
->pw_dir
= strdup("/");
909 if (pwd
->pw_dir
== NULL
) {
910 syslog(LOG_NOTICE
, "strdup(): %m");
915 #endif /* __APPLE__ */
917 (void)setenv("SHELL", pwd
->pw_shell
, 1);
919 syslog(LOG_ERR
, "pwd->pw_shell not set - exiting");
920 bail(NO_SLEEP_EXIT
, 1);
923 (void)setenv("HOME", pwd
->pw_dir
, 1);
925 (void)setenv("HOME", "/", 1);
927 /* Overwrite "term" from login.conf(5) for any known TERM */
928 if (term
== NULL
&& (tp
= stypeof(tty
)) != NULL
)
929 (void)setenv("TERM", tp
, 1);
931 (void)setenv("TERM", TERM_UNKNOWN
, 0);
932 (void)setenv("LOGNAME", username
, 1);
933 (void)setenv("USER", username
, 1);
934 (void)setenv("PATH", rootlogin
? _PATH_STDPATH
: _PATH_DEFPATH
, 0);
937 /* Re-enable crash reporter */
940 mach_port_t bp
, ep
, mts
;
941 thread_state_flavor_t flavor
= 0;
944 flavor
= PPC_THREAD_STATE64
;
945 #elif defined(__i386__) || defined(__x86_64__)
946 flavor
= x86_THREAD_STATE
;
947 #elif defined(__arm__)
948 flavor
= ARM_THREAD_STATE
;
950 #error unsupported architecture
953 mts
= mach_task_self();
955 kr
= task_get_bootstrap_port(mts
, &bp
);
956 if (kr
!= KERN_SUCCESS
) {
957 syslog(LOG_ERR
, "task_get_bootstrap_port() failure: %s (%d)",
958 bootstrap_strerror(kr
), kr
);
962 const char* bs
= "com.apple.ReportCrash";
963 kr
= bootstrap_look_up(bp
, (char*)bs
, &ep
);
964 if (kr
!= KERN_SUCCESS
) {
965 syslog(LOG_ERR
, "bootstrap_look_up(%s) failure: %s (%d)",
966 bs
, bootstrap_strerror(kr
), kr
);
970 kr
= task_set_exception_ports(mts
, EXC_MASK_CRASH
, ep
, EXCEPTION_STATE_IDENTITY
| MACH_EXCEPTION_CODES
, flavor
);
971 if (kr
!= KERN_SUCCESS
) {
972 syslog(LOG_ERR
, "task_set_exception_ports() failure: %d", kr
);
976 #endif /* __APPLE__ */
982 cw
= login_getcapstr(lc
, "copyright", NULL
, NULL
);
983 if (cw
== NULL
|| motd(cw
) == -1)
984 (void)printf("%s", copyright
);
988 cw
= login_getcapstr(lc
, "welcome", NULL
, NULL
);
989 if (cw
!= NULL
&& access(cw
, F_OK
) == 0)
992 motd(_PATH_MOTDFILE
);
994 if (login_getcapbool(lc_user
, "nocheckmail", 0) == 0 &&
995 login_getcapbool(lc
, "nocheckmail", 0) == 0) {
996 #else /* !LOGIN_CAP */
997 motd(_PATH_MOTDFILE
);
999 #endif /* !LOGIN_CAP */
1002 /* $MAIL may have been set by class. */
1003 cx
= getenv("MAIL");
1005 asprintf(&cx
, "%s/%s",
1006 _PATH_MAILDIR
, pwd
->pw_name
);
1008 if (cx
&& stat(cx
, &st
) == 0 && st
.st_size
!= 0)
1009 (void)printf("You have %smail.\n",
1010 (st
.st_mtime
> st
.st_atime
) ? "new " : "");
1011 if (getenv("MAIL") == NULL
)
1017 login_close(lc_user
);
1019 #endif /* LOGIN_CAP */
1021 (void)signal(SIGALRM
, SIG_DFL
);
1022 (void)signal(SIGQUIT
, SIG_DFL
);
1023 (void)signal(SIGINT
, SIG_DFL
);
1024 (void)signal(SIGTSTP
, SIG_IGN
);
1027 if (fflag
&& *argv
) pwd
->pw_shell
= *argv
;
1028 #endif /* __APPLE__ */
1031 * Login shells have a leading '-' in front of argv[0]
1033 p
= strrchr(pwd
->pw_shell
, '/');
1035 if (asprintf(&arg0
, "%s%s", lflag
? "" : "-", p
? p
+ 1 : pwd
->pw_shell
) >= MAXPATHLEN
) {
1036 #else /* __APPLE__ */
1037 if (asprintf(&arg0
, "-%s", p
? p
+ 1 : pwd
->pw_shell
) >= MAXPATHLEN
) {
1038 #endif /* __APPLE__ */
1039 syslog(LOG_ERR
, "user: %s: shell exceeds maximum pathname size",
1041 errx(1, "shell exceeds maximum pathname size");
1042 } else if (arg0
== NULL
) {
1043 err(1, "asprintf()");
1047 if (fflag
&& *argv
) {
1049 execvp(pwd
->pw_shell
, argv
);
1052 #endif /* __APPLE__ */
1053 execlp(shell
, arg0
, (char *)0);
1054 err(1, "%s", shell
);
1063 * Attempt to authenticate the user using PAM. Returns 0 if the user is
1064 * authenticated, or 1 if not authenticated. If some sort of PAM system
1065 * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
1066 * function returns -1. This can be used as an indication that we should
1067 * fall back to a different authentication mechanism.
1070 auth_pam(int skip_auth
)
1072 const char *tmpl_user
;
1080 pam_err
= pam_authenticate(pamh
, pam_silent
);
1085 * With PAM we support the concept of a "template"
1086 * user. The user enters a login name which is
1087 * authenticated by PAM, usually via a remote service
1088 * such as RADIUS or TACACS+. If authentication
1089 * succeeds, a different but related "template" name
1090 * is used for setting the credentials, shell, and
1091 * home directory. The name the user enters need only
1092 * exist on the remote authentication server, but the
1093 * template name must be present in the local password
1096 * This is supported by two various mechanisms in the
1097 * individual modules. However, from the application's
1098 * point of view, the template user is always passed
1099 * back as a changed value of the PAM_USER item.
1101 pam_err
= pam_get_item(pamh
, PAM_USER
, &item
);
1102 if (pam_err
== PAM_SUCCESS
) {
1103 tmpl_user
= (const char *)item
;
1104 if (strcmp(username
, tmpl_user
) != 0)
1105 pwd
= getpwnam(tmpl_user
);
1107 pam_syslog("pam_get_item(PAM_USER)");
1113 case PAM_USER_UNKNOWN
:
1119 pam_syslog("pam_authenticate()");
1126 pam_err
= pam_acct_mgmt(pamh
, pam_silent
);
1130 case PAM_NEW_AUTHTOK_REQD
:
1133 pam_err
= pam_chauthtok(pamh
,
1134 pam_silent
|PAM_CHANGE_EXPIRED_AUTHTOK
);
1135 if (pam_err
!= PAM_SUCCESS
) {
1136 pam_syslog("pam_chauthtok()");
1142 pam_syslog("pam_acct_mgmt()");
1146 pam_syslog("pam_acct_mgmt()");
1153 pam_end(pamh
, pam_err
);
1160 * Export any environment variables PAM modules may have set
1163 export_pam_environment()
1168 pam_env
= pam_getenvlist(pamh
);
1169 if (pam_env
!= NULL
) {
1170 for (pp
= pam_env
; *pp
!= NULL
; pp
++) {
1178 * Perform sanity checks on an environment variable:
1179 * - Make sure there is an '=' in the string.
1180 * - Make sure the string doesn't run on too long.
1181 * - Do not export certain variables. This list was taken from the
1182 * Solaris pam_putenv(3) man page.
1186 export(const char *s
)
1188 static const char *noexport
[] = {
1189 "SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH",
1196 if (strlen(s
) > 1024 || (p
= strchr(s
, '=')) == NULL
)
1198 if (strncmp(s
, "LD_", 3) == 0)
1200 for (pp
= noexport
; *pp
!= NULL
; pp
++) {
1202 if (s
[n
] == '=' && strncmp(s
, *pp
, n
) == 0)
1206 (void)setenv(s
, p
+ 1, 1);
1210 #endif /* USE_PAM */
1216 (void)fprintf(stderr
, "usage: login [-pq] [-h hostname] [username]\n");
1217 (void)fprintf(stderr
, " login -f [-lpq] [-h hostname] [username [prog [arg ...]]]\n");
1219 (void)fprintf(stderr
, "usage: login [-fp] [-h hostname] [username]\n");
1225 * Prompt user and read login name from stdin.
1233 nbuf
= malloc(MAXLOGNAME
);
1237 (void)printf("%s", prompt
);
1238 for (p
= nbuf
; (ch
= getchar()) != '\n'; ) {
1241 bail(NO_SLEEP_EXIT
, 0);
1243 if (p
< nbuf
+ MAXLOGNAME
- 1)
1246 } while (p
== nbuf
);
1249 if (nbuf
[0] == '-') {
1252 #endif /* USE_PAM */
1253 memmove(nbuf
, nbuf
+ 1, strlen(nbuf
));
1256 pam_silent
= PAM_SILENT
;
1257 #endif /* USE_PAM */
1265 rootterm(const char* ttyn
)
1268 return ((t
= getttynam(ttyn
)) && t
->ty_status
& TTY_SECURE
);
1270 #endif /* !USE_PAM */
1271 #endif /* __APPLE__ */
1274 * SIGINT handler for motd().
1276 static volatile int motdinterrupt
;
1278 sigint(int signo __unused
)
1284 * Display the contents of a file (such as /etc/motd).
1287 motd(const char *motdfile
)
1293 if ((f
= fopen(motdfile
, "r")) == NULL
)
1296 oldint
= signal(SIGINT
, sigint
);
1297 while ((ch
= fgetc(f
)) != EOF
&& !motdinterrupt
)
1299 signal(SIGINT
, oldint
);
1300 if (ch
!= EOF
|| ferror(f
)) {
1310 * Forwards the SIGHUP to the child process and current process group.
1313 handle_sighup(int signo
)
1316 /* close the controlling terminal */
1317 close(STDIN_FILENO
);
1318 close(STDOUT_FILENO
);
1319 close(STDERR_FILENO
);
1320 /* Ignore SIGHUP to avoid tail-recursion on signaling
1321 the current process group (of which we are a member). */
1322 (void)signal(SIGHUP
, SIG_IGN
);
1323 /* Forward the signal to the current process group. */
1324 (void)kill(0, signo
);
1325 /* Forward the signal to the child if not a member of the current
1326 * process group <rdar://problem/6244808>. */
1327 if (getpgid(pid
) != getpgrp()) {
1328 (void)kill(pid
, signo
);
1334 * SIGALRM handler, to enforce login prompt timeout.
1336 * XXX This can potentially confuse the hell out of PAM. We should
1337 * XXX instead implement a conversation function that returns
1338 * XXX PAM_CONV_ERR when interrupted by a signal, and have the signal
1339 * XXX handler just set a flag.
1342 timedout(int signo __unused
)
1345 longjmp(timeout_buf
, signo
);
1356 if ((fd
= open(_PATH_NOLOGIN
, O_RDONLY
, 0)) >= 0) {
1357 while ((nchars
= read(fd
, tbuf
, sizeof(tbuf
))) > 0)
1358 (void)write(fileno(stdout
), tbuf
, nchars
);
1359 #ifdef USE_BSM_AUDIT
1360 au_login_fail("No login", 0);
1366 #endif /* !USE_PAM */
1375 if (*lastlog
.ll_line
) {
1376 (void)printf("Last login: %.*s ",
1377 24-5, (char *)ctime(&lastlog
.ll_tv
.tv_sec
));
1378 if (*lastlog
.ll_host
!= '\0')
1379 (void)printf("from %.*s\n",
1380 (int)sizeof(lastlog
.ll_host
),
1383 (void)printf("on %.*s\n",
1384 (int)sizeof(lastlog
.ll_line
),
1387 #else /* !USE_PAM */
1390 if(!quiet
&& getlastlogx(pwd
->pw_uid
, &ll
) != NULL
) {
1391 (void)printf("Last login: %.*s ",
1392 24-5, (char *)ctime(&ll
.ll_tv
.tv_sec
));
1393 if (*ll
.ll_host
!= '\0')
1394 (void)printf("from %.*s\n",
1395 (int)sizeof(ll
.ll_host
),
1398 (void)printf("on %.*s\n",
1399 (int)sizeof(ll
.ll_line
),
1402 #endif /* USE_PAM */
1404 #endif /* __APPLE__ */
1407 badlogin(char *name
)
1413 syslog(LOG_NOTICE
, "%d LOGIN FAILURE%s FROM %s",
1414 failures
, failures
> 1 ? "S" : "", hostname
);
1415 syslog(LOG_AUTHPRIV
|LOG_NOTICE
,
1416 "%d LOGIN FAILURE%s FROM %s, %s",
1417 failures
, failures
> 1 ? "S" : "", hostname
, name
);
1419 syslog(LOG_NOTICE
, "%d LOGIN FAILURE%s ON %s",
1420 failures
, failures
> 1 ? "S" : "", tty
);
1421 syslog(LOG_AUTHPRIV
|LOG_NOTICE
,
1422 "%d LOGIN FAILURE%s ON %s, %s",
1423 failures
, failures
> 1 ? "S" : "", tty
, name
);
1429 stypeof(char *ttyid
)
1433 if (ttyid
!= NULL
&& *ttyid
!= '\0') {
1434 t
= getttynam(ttyid
);
1435 if (t
!= NULL
&& t
->ty_type
!= NULL
)
1436 return (t
->ty_type
);
1442 refused(const char *msg
, const char *rtype
, int lout
)
1446 printf("%s.\n", msg
);
1448 syslog(LOG_NOTICE
, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s",
1449 pwd
->pw_name
, rtype
, hostname
, tty
);
1451 syslog(LOG_NOTICE
, "LOGIN %s REFUSED (%s) ON TTY %s",
1452 pwd
->pw_name
, rtype
, tty
);
1454 bail(SLEEP_EXIT
, 1);
1462 pam_syslog(const char *msg
)
1464 syslog(LOG_ERR
, "%s: %s", msg
, pam_strerror(pamh
, pam_err
));
1475 if (pam_session_established
) {
1476 pam_err
= pam_close_session(pamh
, 0);
1477 if (pam_err
!= PAM_SUCCESS
)
1478 pam_syslog("pam_close_session()");
1480 pam_session_established
= 0;
1481 if (pam_cred_established
) {
1482 pam_err
= pam_setcred(pamh
, pam_silent
|PAM_DELETE_CRED
);
1483 if (pam_err
!= PAM_SUCCESS
)
1484 pam_syslog("pam_setcred()");
1486 pam_cred_established
= 0;
1487 pam_end(pamh
, pam_err
);
1491 #endif /* USE_PAM */
1494 * Exit, optionally after sleeping a few seconds
1497 bail(int sec
, int eval
)
1502 #endif /* USE_PAM */
1503 #ifdef USE_BSM_AUDIT