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>
59 #include <TargetConditionals.h>
61 #include <sys/param.h>
65 #include <sys/resource.h>
77 #include <login_cap.h>
93 #define _UTX_USERSIZE MAXLOGNAME
96 #endif /* __APPLE__ */
98 #include <sys/types.h>
99 #include <sys/socket.h>
100 #include <netinet/in.h>
101 #include <arpa/inet.h>
105 #include <bsm/libbsm.h>
106 #include <bsm/audit_uevents.h>
110 #include <mach/mach_types.h>
111 #include <mach/task.h>
112 #include <mach/mach_init.h>
113 #include <servers/bootstrap.h>
115 #include <sys/file.h>
117 #endif /* __APPLE__ */
120 #include <security/pam_appl.h>
121 #include <security/openpam.h>
125 #include "pathnames.h"
128 static int auth_pam(void);
130 static void bail(int, int);
132 static int export(const char *);
133 static void export_pam_environment(void);
135 static int motd(const char *);
136 static void badlogin(char *);
137 static char *getloginname(void);
139 static void pam_syslog(const char *);
140 static void pam_cleanup(void);
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);
149 static void dolastlog(int);
150 static void handle_sighup(int);
153 static void checknologin(void);
154 static int rootterm(const char *);
155 #endif /* !USE_PAM */
156 #endif /* __APPLE__ */
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
169 * This bounds the time given to login. Not a define so it can
170 * be patched on machines where it's too small.
172 static u_int timeout
= 300;
174 /* Buffer for signal handling of timeout */
175 static jmp_buf timeout_buf
;
180 static char *envinit
[1]; /* empty environment list */
183 * Command line flags and arguments
185 static int fflag
; /* -f: do not perform authentication */
187 static int lflag
; /* -l: login session to the commmand that follows username */
189 static int hflag
; /* -h: login from remote host */
190 static char *hostname
; /* hostname from command line */
191 static int pflag
; /* -p: preserve environment */
196 static char *username
; /* user name */
197 static char *olduser
; /* previous user name */
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
;
213 static pam_handle_t
*pamh
= NULL
;
214 static struct pam_conv pamc
= { openpam_ttyconv
, NULL
};
216 static int pam_silent
= PAM_SILENT
;
217 static int pam_cred_established
;
218 static int pam_session_established
;
225 static struct lastlogx lastlog
;
230 #endif /* USE_BSM_AUDIT */
231 #endif /* __APPLE__ */
234 main(int argc
, char *argv
[])
238 int retries
, backoff
;
239 int ask
, ch
, cnt
, quietlog
= 0, rootlogin
, rval
;
244 char tname
[sizeof(_PATH_TTY
) + 10];
250 const char *name
= "login"; /* PAM config */
254 const char *shell
= NULL
;
255 #endif /* !__APPLE__ */
257 login_cap_t
*lc
= NULL
;
258 login_cap_t
*lc_user
= NULL
;
259 #endif /* LOGIN_CAP */
264 char auditsuccess
= 1;
267 (void)signal(SIGQUIT
, SIG_IGN
);
268 (void)signal(SIGINT
, SIG_IGN
);
269 (void)signal(SIGHUP
, SIG_IGN
);
270 if (setjmp(timeout_buf
)) {
273 (void)fprintf(stderr
, "Login timed out after %d seconds\n",
275 bail(NO_SLEEP_EXIT
, 0);
277 (void)signal(SIGALRM
, timedout
);
278 (void)alarm(timeout
);
280 prio
= getpriority(PRIO_PROCESS
, 0);
282 (void)setpriority(PRIO_PROCESS
, 0, 0);
284 openlog("login", LOG_ODELAY
, LOG_AUTH
);
291 while ((ch
= getopt(argc
, argv
, "1fh:lpq")) != -1)
293 while ((ch
= getopt(argc
, argv
, "fh:p")) != -1)
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
);
324 syslog(LOG_ERR
, "invalid flag %c", ch
);
331 username
= strdup(*argv
);
332 if (username
== NULL
)
337 #endif /* __APPLE__ */
343 setproctitle("-%s", getprogname());
344 #endif /* !__APPLE__ */
346 for (cnt
= getdtablesize(); cnt
> 2; cnt
--)
352 ttyn
= ttyname(STDIN_FILENO
);
353 if (ttyn
== NULL
|| *ttyn
== '\0') {
354 (void)snprintf(tname
, sizeof(tname
), "%s??", _PATH_TTY
);
357 if ((tty
= strrchr(ttyn
, '/')) != NULL
)
364 * Get "login-retries" & "login-backoff" from default class
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
);
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 */
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);
393 if (S_ISCHR(st
.st_mode
)) {
394 tid
.port
= st
.st_rdev
;
398 #endif /* USE_BSM_AUDIT */
399 #endif /* __APPLE__ */
402 * Try to authenticate the user until we succeed or time out.
404 for (cnt
= 0;; ask
= 1) {
410 username
= getloginname();
415 if (strlen(username
) > _UTX_USERSIZE
)
416 username
[_UTX_USERSIZE
] = '\0';
417 #endif /* __APPLE__ */
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).
424 if (failures
&& strcmp(olduser
, username
) != 0) {
425 if (failures
> (pwd
? 0 : 1))
431 /* get lastlog info before PAM make a new entry */
433 getlastlogxbyname(username
, &lastlog
);
435 #endif /* __APPLE__ */
437 pwd
= getpwnam(username
);
441 * Load the PAM policy and set some variables
444 if (fflag
&& (pwd
!= NULL
) && (pwd
->pw_uid
== uid
)) {
448 pam_err
= pam_start(name
, username
, &pamc
, &pamh
);
449 if (pam_err
!= PAM_SUCCESS
) {
450 pam_syslog("pam_start()");
452 au_login_fail("PAM Error", 1);
454 bail(NO_SLEEP_EXIT
, 1);
456 pam_err
= pam_set_item(pamh
, PAM_TTY
, tty
);
457 if (pam_err
!= PAM_SUCCESS
) {
458 pam_syslog("pam_set_item(PAM_TTY)");
460 au_login_fail("PAM Error", 1);
462 bail(NO_SLEEP_EXIT
, 1);
464 pam_err
= pam_set_item(pamh
, PAM_RHOST
, hostname
);
465 if (pam_err
!= PAM_SUCCESS
) {
466 pam_syslog("pam_set_item(PAM_RHOST)");
468 au_login_fail("PAM Error", 1);
470 bail(NO_SLEEP_EXIT
, 1);
474 if (pwd
!= NULL
&& pwd
->pw_uid
== 0)
478 * If the -f option was specified and the caller is
479 * root or the caller isn't changing their uid, don't
482 if (pwd
!= NULL
&& fflag
&&
483 (uid
== (uid_t
)0 || uid
== (uid_t
)pwd
->pw_uid
)) {
484 /* already authenticated */
487 auditsuccess
= 0; /* opened a terminal window only */
492 /* If the account doesn't have a password, authenticate. */
493 } else if (pwd
!= NULL
&& pwd
->pw_passwd
[0] == '\0') {
495 #endif /* !USE_PAM */
496 #endif /* __APPLE__ */
499 (void)setpriority(PRIO_PROCESS
, 0, -4);
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
));
510 (void)setpriority(PRIO_PROCESS
, 0, 0);
516 * If trying to log in as root but with insecure terminal,
517 * refuse the login attempt.
519 if (pwd
&& rootlogin
&& !rootterm(tty
)) {
520 refused("root login refused on this terminal", "ROOTTERM", 0);
522 au_login_fail("Login refused on terminal", 0);
526 #endif /* !USE_PAM */
527 #endif /* __APPLE__ */
529 if (pwd
&& rval
== 0)
537 * We are not exiting here, but this corresponds to a failed
538 * login event, so set exitstatus to 1.
541 au_login_fail("Login incorrect", 1);
544 (void)printf("Login incorrect\n");
550 * Allow up to 'retry' (10) attempts, but start
551 * backing off after 'backoff' (3) attempts.
553 if (++cnt
> backoff
) {
554 if (cnt
>= retries
) {
558 sleep((u_int
)((cnt
- backoff
) * 5));
562 /* committed to login -- turn off timeout */
563 (void)alarm((u_int
)0);
564 (void)signal(SIGHUP
, SIG_DFL
);
570 fprintf(stderr
, "login: Unable to find user: %s\n", username
);
575 /* if user not super-user, check for disabled logins */
578 #endif /* !USE_PAM */
582 /* Audit successful login. */
589 * Establish the login class.
591 lc
= login_getpwclass(pwd
);
592 lc_user
= login_getuserclass(pwd
);
594 if (!(quietlog
= login_getcapbool(lc_user
, "hushlogin", 0)))
595 quietlog
= login_getcapbool(lc
, "hushlogin", 0);
596 #endif /* LOGIN_CAP */
600 * Switching needed for NFS with root access disabled.
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.
606 (void)setegid(pwd
->pw_gid
);
607 (void)seteuid(rootlogin
? 0 : pwd
->pw_uid
);
609 if (!*pwd
->pw_dir
|| chdir(pwd
->pw_dir
) < 0) {
611 if (login_getcapbool(lc
, "requirehome", 0))
612 refused("Home directory not available", "HOMEDIR", 1);
613 #endif /* LOGIN_CAP */
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");
627 #endif /* !__APPLE__ */
629 quietlog
= access(_PATH_HUSHLOGIN
, F_OK
) == 0;
637 /* Nothing else left to fail -- really log in. */
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
));
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();
651 #endif /* !__APPLE__ */
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");
661 if (*shell
== '\0') /* Not overridden */
662 shell
= pwd
->pw_shell
;
663 if ((shell
= strdup(shell
)) == NULL
) {
664 syslog(LOG_NOTICE
, "strdup(): %m");
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
);
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.
688 login_fbtab(tty
, pwd
->pw_uid
, pwd
->pw_gid
);
689 #endif /* !__APPLE__ */
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.
697 * We don't log in the case of EOPNOTSUPP because dev might be
698 * on NFS, which doesn't support chflags.
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
704 if (ttyn
!= tname
&& chflags(ttyn
, 0))
706 if (errno
!= EOPNOTSUPP
&& errno
!= ENOTSUP
&& errno
!= EROFS
)
708 if (errno
!= EOPNOTSUPP
&& errno
!= EROFS
)
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
))
714 syslog(LOG_ERR
, "chown(%s): %m", ttyn
);
717 (void)chmod(ttyn
, 0620);
718 #endif /* __APPLE__ */
722 * Exclude cons/vt/ptys only, assume dialup otherwise
723 * TODO: Make dialup tty determination a library call
724 * for consistency (finger etc.)
726 if (hflag
&& isdialuptty(tty
))
727 syslog(LOG_INFO
, "DIALUP %s, %s", tty
, pwd
->pw_name
);
728 #endif /* !__APPLE__ */
732 * Syslog each successful login, so we don't have to watch
733 * hundreds of wtmp or lastlogin files.
736 syslog(LOG_INFO
, "login from %s on %s as %s",
737 hostname
, tty
, pwd
->pw_name
);
739 syslog(LOG_INFO
, "login on %s as %s",
744 * If fflag is on, assume caller/authenticator has logged root
747 if (rootlogin
&& fflag
== 0) {
749 syslog(LOG_NOTICE
, "ROOT LOGIN (%s) ON %s FROM %s",
750 username
, tty
, hostname
);
752 syslog(LOG_NOTICE
, "ROOT LOGIN (%s) ON %s",
757 * Destroy environment unless user has requested its
758 * preservation - but preserve TERM in all cases
760 term
= getenv("TERM");
764 setenv("TERM", term
, 0);
768 * PAM modules might add supplementary groups during pam_setcred().
770 if (setusercontext(lc
, pwd
, pwd
->pw_uid
, LOGIN_SETGROUP
) != 0) {
771 syslog(LOG_ERR
, "setusercontext() failed - exiting");
772 bail(NO_SLEEP_EXIT
, 1);
774 #endif /* !__APPLE__ */
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);
781 pam_cred_established
= 1;
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);
788 pam_session_established
= 1;
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__ */
800 * We must fork() before setuid() because we need to call
801 * pam_close_session() as root.
806 } else if (pid
!= 0) {
808 * Parent: wait for child to finish, then clean up
813 setproctitle("-%s [pam]", getprogname());
814 #endif /* !__APPLE__ */
816 /* Our SIGHUP handler may interrupt the wait */
819 res
= waitpid(pid
, &status
, 0);
820 } while (res
== -1 && errno
== EINTR
);
822 waitpid(pid
, &status
, 0);
827 #endif /* __APPLE__ */
828 bail(NO_SLEEP_EXIT
, 0);
832 * NOTICE: We are now in the child process!
836 /* Restore the default SIGHUP handler for the child. */
837 (void)signal(SIGHUP
, SIG_DFL
);
838 #endif /* __APPLE__ */
842 * Add any environment variables the PAM modules may have set.
844 export_pam_environment();
847 * We're done with PAM now; our parent will deal with the rest.
854 * We don't need to be root anymore, so set the login name and
857 if (setlogin(username
) != 0) {
858 syslog(LOG_ERR
, "setlogin(%s): %m - exiting", username
);
859 bail(NO_SLEEP_EXIT
, 1);
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
);
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");
877 #endif /* !__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. */
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);
893 pwd
->pw_dir
= strdup("/");
894 if (pwd
->pw_dir
== NULL
) {
895 syslog(LOG_NOTICE
, "strdup(): %m");
900 #endif /* __APPLE__ */
902 (void)setenv("SHELL", pwd
->pw_shell
, 1);
904 syslog(LOG_ERR
, "pwd->pw_shell not set - exiting", username
);
905 bail(NO_SLEEP_EXIT
, 1);
908 (void)setenv("HOME", pwd
->pw_dir
, 1);
910 (void)setenv("HOME", "/", 1);
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);
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);
922 /* Re-enable crash reporter */
925 mach_port_t bp
, ep
, mts
;
926 thread_state_flavor_t flavor
= 0;
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
;
935 #error unsupported architecture
938 mts
= mach_task_self();
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
);
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
);
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
);
967 cw
= login_getcapstr(lc
, "copyright", NULL
, NULL
);
968 if (cw
== NULL
|| motd(cw
) == -1)
969 (void)printf("%s", copyright
);
973 cw
= login_getcapstr(lc
, "welcome", NULL
, NULL
);
974 if (cw
!= NULL
&& access(cw
, F_OK
) == 0)
977 motd(_PATH_MOTDFILE
);
979 if (login_getcapbool(lc_user
, "nocheckmail", 0) == 0 &&
980 login_getcapbool(lc
, "nocheckmail", 0) == 0) {
981 #else /* !LOGIN_CAP */
982 motd(_PATH_MOTDFILE
);
984 #endif /* !LOGIN_CAP */
987 /* $MAIL may have been set by class. */
990 asprintf(&cx
, "%s/%s",
991 _PATH_MAILDIR
, pwd
->pw_name
);
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
)
1002 login_close(lc_user
);
1004 #endif /* LOGIN_CAP */
1006 (void)signal(SIGALRM
, SIG_DFL
);
1007 (void)signal(SIGQUIT
, SIG_DFL
);
1008 (void)signal(SIGINT
, SIG_DFL
);
1009 (void)signal(SIGTSTP
, SIG_IGN
);
1012 if (fflag
&& *argv
) pwd
->pw_shell
= *argv
;
1013 #endif /* __APPLE__ */
1016 * Login shells have a leading '-' in front of argv[0]
1018 p
= strrchr(pwd
->pw_shell
, '/');
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",
1026 errx(1, "shell exceeds maximum pathname size");
1027 } else if (arg0
== NULL
) {
1028 err(1, "asprintf()");
1032 if (fflag
&& *argv
) {
1034 execvp(pwd
->pw_shell
, argv
);
1037 #endif /* __APPLE__ */
1038 execlp(shell
, arg0
, (char *)0);
1039 err(1, "%s", shell
);
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.
1057 const char *tmpl_user
;
1061 pam_err
= pam_authenticate(pamh
, pam_silent
);
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
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.
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
);
1088 pam_syslog("pam_get_item(PAM_USER)");
1094 case PAM_USER_UNKNOWN
:
1100 pam_syslog("pam_authenticate()");
1106 pam_err
= pam_acct_mgmt(pamh
, pam_silent
);
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()");
1119 pam_syslog("pam_acct_mgmt()");
1126 pam_end(pamh
, pam_err
);
1133 * Export any environment variables PAM modules may have set
1136 export_pam_environment()
1141 pam_env
= pam_getenvlist(pamh
);
1142 if (pam_env
!= NULL
) {
1143 for (pp
= pam_env
; *pp
!= NULL
; pp
++) {
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.
1159 export(const char *s
)
1161 static const char *noexport
[] = {
1162 "SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH",
1169 if (strlen(s
) > 1024 || (p
= strchr(s
, '=')) == NULL
)
1171 if (strncmp(s
, "LD_", 3) == 0)
1173 for (pp
= noexport
; *pp
!= NULL
; pp
++) {
1175 if (s
[n
] == '=' && strncmp(s
, *pp
, n
) == 0)
1179 (void)setenv(s
, p
+ 1, 1);
1183 #endif /* USE_PAM */
1189 (void)fprintf(stderr
, "usage: login [-pq] [-h hostname] [username]\n");
1190 (void)fprintf(stderr
, " login -f [-lpq] [-h hostname] [username [prog [arg ...]]]\n");
1192 (void)fprintf(stderr
, "usage: login [-fp] [-h hostname] [username]\n");
1198 * Prompt user and read login name from stdin.
1206 nbuf
= malloc(MAXLOGNAME
);
1210 (void)printf("%s", prompt
);
1211 for (p
= nbuf
; (ch
= getchar()) != '\n'; ) {
1214 bail(NO_SLEEP_EXIT
, 0);
1216 if (p
< nbuf
+ MAXLOGNAME
- 1)
1219 } while (p
== nbuf
);
1222 if (nbuf
[0] == '-') {
1225 #endif /* USE_PAM */
1226 memmove(nbuf
, nbuf
+ 1, strlen(nbuf
));
1229 pam_silent
= PAM_SILENT
;
1230 #endif /* USE_PAM */
1238 rootterm(const char* ttyn
)
1241 return ((t
= getttynam(ttyn
)) && t
->ty_status
& TTY_SECURE
);
1243 #endif /* !USE_PAM */
1244 #endif /* __APPLE__ */
1247 * SIGINT handler for motd().
1249 static volatile int motdinterrupt
;
1251 sigint(int signo __unused
)
1257 * Display the contents of a file (such as /etc/motd).
1260 motd(const char *motdfile
)
1266 if ((f
= fopen(motdfile
, "r")) == NULL
)
1269 oldint
= signal(SIGINT
, sigint
);
1270 while ((ch
= fgetc(f
)) != EOF
&& !motdinterrupt
)
1272 signal(SIGINT
, oldint
);
1273 if (ch
!= EOF
|| ferror(f
)) {
1283 * Forwards the SIGHUP to the child process and current process group.
1286 handle_sighup(int signo
)
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
);
1307 * SIGALRM handler, to enforce login prompt timeout.
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.
1315 timedout(int signo __unused
)
1318 longjmp(timeout_buf
, signo
);
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);
1339 #endif /* !USE_PAM */
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
),
1356 (void)printf("on %.*s\n",
1357 (int)sizeof(lastlog
.ll_line
),
1360 #else /* !USE_PAM */
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
),
1371 (void)printf("on %.*s\n",
1372 (int)sizeof(ll
.ll_line
),
1375 #endif /* USE_PAM */
1377 #endif /* __APPLE__ */
1380 badlogin(char *name
)
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
);
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
);
1402 stypeof(char *ttyid
)
1406 if (ttyid
!= NULL
&& *ttyid
!= '\0') {
1407 t
= getttynam(ttyid
);
1408 if (t
!= NULL
&& t
->ty_type
!= NULL
)
1409 return (t
->ty_type
);
1415 refused(const char *msg
, const char *rtype
, int lout
)
1419 printf("%s.\n", msg
);
1421 syslog(LOG_NOTICE
, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s",
1422 pwd
->pw_name
, rtype
, hostname
, tty
);
1424 syslog(LOG_NOTICE
, "LOGIN %s REFUSED (%s) ON TTY %s",
1425 pwd
->pw_name
, rtype
, tty
);
1427 bail(SLEEP_EXIT
, 1);
1435 pam_syslog(const char *msg
)
1437 syslog(LOG_ERR
, "%s: %s", msg
, pam_strerror(pamh
, pam_err
));
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()");
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()");
1459 pam_cred_established
= 0;
1460 pam_end(pamh
, pam_err
);
1464 #endif /* USE_PAM */
1467 * Exit, optionally after sleeping a few seconds
1470 bail(int sec
, int eval
)
1475 #endif /* USE_PAM */
1476 #ifdef USE_BSM_AUDIT