4 * This program is derived from 4.3 BSD software and is subject to the
5 * copyright notice below.
7 * Copyright (C) 2011 Karel Zak <kzak@redhat.com>
8 * Rewritten to PAM-only version.
10 * Michael Glad (glad@daimi.dk)
11 * Computer Science Department, Aarhus University, Denmark
14 * Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
15 * All rights reserved.
17 * Redistribution and use in source and binary forms are permitted
18 * provided that the above copyright notice and this paragraph are
19 * duplicated in all such forms and that any documentation,
20 * advertising materials, and other materials related to such
21 * distribution and use acknowledge that the software was developed
22 * by the University of California, Berkeley. The name of the
23 * University may not be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
27 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 #include <sys/param.h>
38 #include <sys/resource.h>
42 #include <sys/ioctl.h>
50 #include <sys/syslog.h>
51 #include <sys/sysmacros.h>
52 #include <linux/major.h>
55 #include <security/pam_appl.h>
56 #include <security/pam_misc.h>
57 #include <sys/sendfile.h>
60 # include <libaudit.h>
64 #include "setproctitle.h"
65 #include "pathnames.h"
70 #include "fileutils.h"
72 #include "logindefs.h"
74 #define is_pam_failure(_rc) ((_rc) != PAM_SUCCESS)
76 #define LOGIN_MAX_TRIES 3
77 #define LOGIN_EXIT_TIMEOUT 5
78 #define LOGIN_TIMEOUT 60
81 # define TTY_MODE 0620
83 # define TTY_MODE 0600
86 #define TTYGRPNAME "tty" /* name of group to own ttys */
87 #define VCS_PATH_MAX 64
90 * Login control struct
92 struct login_context
{
93 const char *tty_path
; /* ttyname() return value */
94 const char *tty_name
; /* tty_path without /dev prefix */
95 const char *tty_number
; /* end of the tty_path */
96 mode_t tty_mode
; /* chmod() mode */
98 char *username
; /* from command line or PAM */
100 struct passwd
*pwd
; /* user info */
102 pam_handle_t
*pamh
; /* PAM handler */
103 struct pam_conv conv
; /* PAM conversation */
105 #ifdef LOGIN_CHOWN_VCS
106 char vcsn
[VCS_PATH_MAX
]; /* virtual console name */
107 char vcsan
[VCS_PATH_MAX
];
110 char thishost
[MAXHOSTNAMELEN
+ 1]; /* this machine */
111 char *thisdomain
; /* this machine domain */
112 char *hostname
; /* remote machine */
113 char hostaddress
[16]; /* remote address */
116 int quiet
; /* 1 is hush file exists */
118 unsigned int remote
:1, /* login -h */
119 nohost
:1, /* login -H */
120 noauth
:1, /* login -f */
121 keep_env
:1; /* login -p */
125 * This bounds the time given to login. Not a define so it can
126 * be patched on machines where it's too small.
128 static int timeout
= LOGIN_TIMEOUT
;
129 static int child_pid
= 0;
130 static volatile int got_sig
= 0;
132 #ifdef LOGIN_CHOWN_VCS
133 /* true if the filedescriptor fd is a console tty, very Linux specific */
134 static int is_consoletty(int fd
)
138 if ((fstat(fd
, &stb
) >= 0)
139 && (major(stb
.st_rdev
) == TTY_MAJOR
)
140 && (minor(stb
.st_rdev
) < 64)) {
149 * Robert Ambrose writes:
150 * A couple of my users have a problem with login processes hanging around
151 * soaking up pts's. What they seem to hung up on is trying to write out the
152 * message 'Login timed out after %d seconds' when the connection has already
154 * What I did was add a second timeout while trying to write the message so
155 * the process just exits if the second timeout expires.
157 static void __attribute__ ((__noreturn__
))
158 timedout2(int sig
__attribute__ ((__unused__
)))
165 tcsetattr(0, TCSANOW
, &ti
);
166 exit(EXIT_SUCCESS
); /* %% */
169 static void timedout(int sig
__attribute__ ((__unused__
)))
171 signal(SIGALRM
, timedout2
);
173 /* TRANSLATORS: The standard value for %d is 60. */
174 warnx(_("timed out after %d seconds"), timeout
);
175 signal(SIGALRM
, SIG_IGN
);
181 * This handler allows to inform a shell about signals to login. If you have
182 * (root) permissions you can kill all login childrent by one signal to login
185 * Also, parent who is session leader is able (before setsid() in child) to
186 * inform child when controlling tty goes away (e.g. modem hangup, SIGHUP).
188 static void sig_handler(int signal
)
191 kill(-child_pid
, signal
);
194 if (signal
== SIGTERM
)
195 kill(-child_pid
, SIGHUP
); /* because the shell often ignores SIGTERM */
199 * Let use delay for all exit() calls when user is not authenticated or
200 * session fully initialized (loginpam_session()).
202 static void __attribute__ ((__noreturn__
)) sleepexit(int eval
)
204 sleep(getlogindefs_num("FAIL_DELAY", LOGIN_EXIT_TIMEOUT
));
208 static const char *get_thishost(struct login_context
*cxt
, const char **domain
)
210 if (!*cxt
->thishost
) {
211 if (gethostname(cxt
->thishost
, sizeof(cxt
->thishost
))) {
216 cxt
->thishost
[sizeof(cxt
->thishost
) -1] = '\0';
217 cxt
->thisdomain
= strchr(cxt
->thishost
, '.');
219 *cxt
->thisdomain
++ = '\0';
223 *domain
= cxt
->thisdomain
;
224 return cxt
->thishost
;
228 * Output the /etc/motd file
230 * motd() determines the name of a login announcement file and outputs it to
231 * the user's terminal at login time. The MOTD_FILE configuration option is a
232 * colon-delimited list of filenames. The empty MOTD_FILE option disables motd
235 static void motd(void)
237 char *motdlist
, *motdfile
;
240 mb
= getlogindefs_str("MOTD_FILE", _PATH_MOTDFILE
);
244 motdlist
= xstrdup(mb
);
246 for (motdfile
= strtok(motdlist
, ":"); motdfile
;
247 motdfile
= strtok(NULL
, ":")) {
252 if (stat(motdfile
, &st
) || !st
.st_size
)
254 fd
= open(motdfile
, O_RDONLY
, 0);
258 sendfile(fileno(stdout
), fd
, NULL
, st
.st_size
);
266 * Nice and simple code provided by Linus Torvalds 16-Feb-93
267 * Nonblocking stuff by Maciej W. Rozycki, macro@ds2.pg.gda.pl, 1999.
269 * He writes: "Login performs open() on a tty in a blocking mode.
270 * In some cases it may make login wait in open() for carrier infinitely,
271 * for example if the line is a simplistic case of a three-wire serial
272 * connection. I believe login should open the line in the non-blocking mode
273 * leaving the decision to make a connection to getty (where it actually
276 static void open_tty(const char *tty
)
280 fd
= open(tty
, O_RDWR
| O_NONBLOCK
);
282 syslog(LOG_ERR
, _("FATAL: can't reopen tty: %m"));
283 sleepexit(EXIT_FAILURE
);
288 syslog(LOG_ERR
, _("FATAL: %s is not a terminal"), tty
);
289 sleepexit(EXIT_FAILURE
);
292 flags
= fcntl(fd
, F_GETFL
);
293 flags
&= ~O_NONBLOCK
;
294 fcntl(fd
, F_SETFL
, flags
);
296 for (i
= 0; i
< fd
; i
++)
298 for (i
= 0; i
< 3; i
++)
305 #define chown_err(_what, _uid, _gid) \
306 syslog(LOG_ERR, _("chown (%s, %lu, %lu) failed: %m"), \
307 (_what), (unsigned long) (_uid), (unsigned long) (_gid))
309 #define chmod_err(_what, _mode) \
310 syslog(LOG_ERR, _("chmod (%s, %u) failed: %m"), (_what), (_mode))
312 static void chown_tty(struct login_context
*cxt
)
315 uid_t uid
= cxt
->pwd
->pw_uid
;
316 gid_t gid
= cxt
->pwd
->pw_gid
;
318 grname
= getlogindefs_str("TTYGROUP", TTYGRPNAME
);
319 if (grname
&& *grname
) {
320 if (*grname
>= 0 && *grname
<= 9) /* group by ID */
321 gid
= getlogindefs_num("TTYGROUP", gid
);
322 else { /* group by name */
323 struct group
*gr
= getgrnam(grname
);
329 if (fchown(0, uid
, gid
)) /* tty */
330 chown_err(cxt
->tty_name
, uid
, gid
);
331 if (fchmod(0, cxt
->tty_mode
))
332 chmod_err(cxt
->tty_name
, cxt
->tty_mode
);
334 #ifdef LOGIN_CHOWN_VCS
335 if (is_consoletty(0)) {
336 if (chown(cxt
->vcsn
, uid
, gid
)) /* vcs */
337 chown_err(cxt
->vcsn
, uid
, gid
);
338 if (chmod(cxt
->vcsn
, cxt
->tty_mode
))
339 chmod_err(cxt
->vcsn
, cxt
->tty_mode
);
341 if (chown(cxt
->vcsan
, uid
, gid
)) /* vcsa */
342 chown_err(cxt
->vcsan
, uid
, gid
);
343 if (chmod(cxt
->vcsan
, cxt
->tty_mode
))
344 chmod_err(cxt
->vcsan
, cxt
->tty_mode
);
350 * Reads the currect terminal path and initialize cxt->tty_* variables.
352 static void init_tty(struct login_context
*cxt
)
356 struct termios tt
, ttt
;
358 cxt
->tty_mode
= (mode_t
) getlogindefs_num("TTYPERM", TTY_MODE
);
360 cxt
->tty_path
= ttyname(0); /* libc calls istty() here */
363 * In case login is suid it was possible to use a hardlink as stdin
364 * and exploit races for a local root exploit. (Wojciech Purczynski).
366 * More precisely, the problem is ttyn := ttyname(0); ...; chown(ttyn);
367 * here ttyname() might return "/tmp/x", a hardlink to a pseudotty.
368 * All of this is a problem only when login is suid, which it isnt.
370 if (!cxt
->tty_path
|| !*cxt
->tty_path
||
371 lstat(cxt
->tty_path
, &st
) != 0 || !S_ISCHR(st
.st_mode
) ||
372 (st
.st_nlink
> 1 && strncmp(cxt
->tty_path
, "/dev/", 5)) ||
373 access(cxt
->tty_path
, R_OK
| W_OK
) != 0) {
375 syslog(LOG_ERR
, _("FATAL: bad tty"));
376 sleepexit(EXIT_FAILURE
);
379 if (strncmp(cxt
->tty_path
, "/dev/", 5) == 0)
380 cxt
->tty_name
= cxt
->tty_path
+ 5;
382 cxt
->tty_name
= cxt
->tty_path
;
384 for (p
= cxt
->tty_name
; p
&& *p
; p
++) {
391 #ifdef LOGIN_CHOWN_VCS
392 /* find names of Virtual Console devices, for later mode change */
393 snprintf(cxt
->vcsn
, sizeof(cxt
->vcsn
), "/dev/vcs%s", cxt
->tty_number
);
394 snprintf(cxt
->vcsan
, sizeof(cxt
->vcsan
), "/dev/vcsa%s", cxt
->tty_number
);
399 ttt
.c_cflag
&= ~HUPCL
;
401 if ((fchown(0, 0, 0) || fchmod(0, cxt
->tty_mode
)) && errno
!= EROFS
) {
403 syslog(LOG_ERR
, _("FATAL: %s: change permissions failed: %m"),
405 sleepexit(EXIT_FAILURE
);
408 /* Kill processes left on this tty */
409 tcsetattr(0, TCSAFLUSH
, &ttt
);
411 signal(SIGHUP
, SIG_IGN
); /* so vhangup() wont kill us */
413 signal(SIGHUP
, SIG_DFL
);
415 /* open stdin,stdout,stderr to the tty */
416 open_tty(cxt
->tty_path
);
418 /* restore tty modes */
419 tcsetattr(0, TCSAFLUSH
, &tt
);
424 * Log failed login attempts in _PATH_BTMP if that exists.
425 * Must be called only with username the name of an actual user.
426 * The most common login failure is to give password instead of username.
428 static void log_btmp(struct login_context
*cxt
)
433 memset(&ut
, 0, sizeof(ut
));
436 cxt
->username
? cxt
->username
: "(unknown)",
440 strncpy(ut
.ut_id
, cxt
->tty_number
, sizeof(ut
.ut_id
));
442 xstrncpy(ut
.ut_line
, cxt
->tty_name
, sizeof(ut
.ut_line
));
444 #if defined(_HAVE_UT_TV) /* in <utmpbits.h> included by <utmp.h> */
445 gettimeofday(&tv
, NULL
);
446 ut
.ut_tv
.tv_sec
= tv
.tv_sec
;
447 ut
.ut_tv
.tv_usec
= tv
.tv_usec
;
452 ut
.ut_time
= t
; /* ut_time is not always a time_t */
456 ut
.ut_type
= LOGIN_PROCESS
; /* XXX doesn't matter */
457 ut
.ut_pid
= cxt
->pid
;
460 xstrncpy(ut
.ut_host
, cxt
->hostname
, sizeof(ut
.ut_host
));
461 if (*cxt
->hostaddress
)
462 memcpy(&ut
.ut_addr_v6
, cxt
->hostaddress
,
463 sizeof(ut
.ut_addr_v6
));
466 updwtmp(_PATH_BTMP
, &ut
);
471 static void log_audit(struct login_context
*cxt
, int status
)
474 struct passwd
*pwd
= cxt
->pwd
;
476 audit_fd
= audit_open();
479 if (!pwd
&& cxt
->username
)
480 pwd
= getpwnam(cxt
->username
);
482 audit_log_acct_message(audit_fd
,
486 cxt
->username
? cxt
->username
: "(unknown)",
487 pwd
? pwd
->pw_uid
: (unsigned int) -1,
495 #else /* !HAVE_LIBAUDIT */
496 # define log_audit(cxt, status)
497 #endif /* HAVE_LIBAUDIT */
499 static void log_lastlog(struct login_context
*cxt
)
508 fd
= open(_PATH_LASTLOG
, O_RDWR
, 0);
512 lseek(fd
, (off_t
) cxt
->pwd
->pw_uid
* sizeof(ll
), SEEK_SET
);
515 * Print last log message
518 if (read(fd
, (char *)&ll
, sizeof(ll
)) == sizeof(ll
) &&
520 time_t ll_time
= (time_t) ll
.ll_time
;
522 printf(_("Last login: %.*s "), 24 - 5, ctime(&ll_time
));
523 if (*ll
.ll_host
!= '\0')
524 printf(_("from %.*s\n"),
525 (int)sizeof(ll
.ll_host
), ll
.ll_host
);
527 printf(_("on %.*s\n"),
528 (int)sizeof(ll
.ll_line
), ll
.ll_line
);
530 lseek(fd
, (off_t
) cxt
->pwd
->pw_uid
* sizeof(ll
), SEEK_SET
);
533 memset((char *)&ll
, 0, sizeof(ll
));
536 ll
.ll_time
= t
; /* ll_time is always 32bit */
539 xstrncpy(ll
.ll_line
, cxt
->tty_name
, sizeof(ll
.ll_line
));
541 xstrncpy(ll
.ll_host
, cxt
->hostname
, sizeof(ll
.ll_host
));
543 if (write_all(fd
, (char *)&ll
, sizeof(ll
)))
544 warn(_("write lastlog failed"));
550 * Update wtmp and utmp logs
552 static void log_utmp(struct login_context
*cxt
)
558 utmpname(_PATH_UTMP
);
563 * login sometimes overwrites the runlevel entry in /var/run/utmp,
564 * confusing sysvinit. I added a test for the entry type, and the
565 * problem was gone. (In a runlevel entry, st_pid is not really a pid
566 * but some number calculated from the previous and current runlevel).
567 * -- Michael Riepe <michael@stud.uni-hannover.de>
569 while ((utp
= getutent()))
570 if (utp
->ut_pid
== cxt
->pid
571 && utp
->ut_type
>= INIT_PROCESS
572 && utp
->ut_type
<= DEAD_PROCESS
)
575 /* If we can't find a pre-existing entry by pid, try by line.
576 * BSD network daemons may rely on this.
580 ut
.ut_type
= LOGIN_PROCESS
;
581 strncpy(ut
.ut_line
, cxt
->tty_name
, sizeof(ut
.ut_line
));
582 utp
= getutline(&ut
);
586 memcpy(&ut
, utp
, sizeof(ut
));
588 /* some gettys/telnetds don't initialize utmp... */
589 memset(&ut
, 0, sizeof(ut
));
591 if (ut
.ut_id
[0] == 0)
592 strncpy(ut
.ut_id
, cxt
->tty_number
, sizeof(ut
.ut_id
));
594 strncpy(ut
.ut_user
, cxt
->username
, sizeof(ut
.ut_user
));
595 xstrncpy(ut
.ut_line
, cxt
->tty_name
, sizeof(ut
.ut_line
));
597 #ifdef _HAVE_UT_TV /* in <utmpbits.h> included by <utmp.h> */
598 gettimeofday(&tv
, NULL
);
599 ut
.ut_tv
.tv_sec
= tv
.tv_sec
;
600 ut
.ut_tv
.tv_usec
= tv
.tv_usec
;
605 ut
.ut_time
= t
; /* ut_time is not always a time_t */
606 /* glibc2 #defines it as ut_tv.tv_sec */
609 ut
.ut_type
= USER_PROCESS
;
610 ut
.ut_pid
= cxt
->pid
;
612 xstrncpy(ut
.ut_host
, cxt
->hostname
, sizeof(ut
.ut_host
));
613 if (*cxt
->hostaddress
)
614 memcpy(&ut
.ut_addr_v6
, cxt
->hostaddress
,
615 sizeof(ut
.ut_addr_v6
));
621 updwtmp(_PATH_WTMP
, &ut
);
624 static void log_syslog(struct login_context
*cxt
)
626 struct passwd
*pwd
= cxt
->pwd
;
628 if (!strncmp(cxt
->tty_name
, "ttyS", 4))
629 syslog(LOG_INFO
, _("DIALUP AT %s BY %s"),
630 cxt
->tty_name
, pwd
->pw_name
);
634 syslog(LOG_NOTICE
, _("ROOT LOGIN ON %s FROM %s"),
635 cxt
->tty_name
, cxt
->hostname
);
637 syslog(LOG_NOTICE
, _("ROOT LOGIN ON %s"), cxt
->tty_name
);
640 syslog(LOG_INFO
, _("LOGIN ON %s BY %s FROM %s"),
641 cxt
->tty_name
, pwd
->pw_name
, cxt
->hostname
);
643 syslog(LOG_INFO
, _("LOGIN ON %s BY %s"), cxt
->tty_name
,
648 static struct passwd
*get_passwd_entry(const char *username
,
652 struct passwd
*res
= NULL
;
656 if (!pwdbuf
|| !username
)
659 #ifdef _SC_GETPW_R_SIZE_MAX
661 long xsz
= sysconf(_SC_GETPW_R_SIZE_MAX
);
666 *pwdbuf
= xrealloc(*pwdbuf
, sz
);
668 x
= getpwnam_r(username
, pwd
, *pwdbuf
, sz
, &res
);
676 /* encapsulate stupid "void **" pam_get_item() API */
677 static int loginpam_get_username(pam_handle_t
*pamh
, char **name
)
679 const void *item
= (void *)*name
;
681 rc
= pam_get_item(pamh
, PAM_USER
, &item
);
682 *name
= (char *)item
;
686 static void loginpam_err(pam_handle_t
*pamh
, int retcode
)
688 const char *msg
= pam_strerror(pamh
, retcode
);
691 fprintf(stderr
, "\n%s\n", msg
);
692 syslog(LOG_ERR
, "%s", msg
);
694 pam_end(pamh
, retcode
);
695 sleepexit(EXIT_FAILURE
);
699 * Composes "<host> login: " string; or returns "login: " is -H is given
701 static const char *loginpam_get_prompt(struct login_context
*cxt
)
704 char *prompt
, *dflt_prompt
= _("login: ");
707 if (cxt
->nohost
|| !(host
= get_thishost(cxt
, NULL
)))
710 sz
= strlen(host
) + 1 + strlen(dflt_prompt
) + 1;
712 prompt
= xmalloc(sz
);
713 snprintf(prompt
, sz
, "%s %s", host
, dflt_prompt
);
718 static pam_handle_t
*init_loginpam(struct login_context
*cxt
)
720 pam_handle_t
*pamh
= NULL
;
724 * username is initialized to NULL and if specified on the command line
725 * it is set. Therefore, we are safe not setting it to anything
727 rc
= pam_start(cxt
->remote
? "remote" : "login",
728 cxt
->username
, &cxt
->conv
, &pamh
);
729 if (rc
!= PAM_SUCCESS
) {
730 warnx(_("PAM failure, aborting: %s"), pam_strerror(pamh
, rc
));
731 syslog(LOG_ERR
, _("Couldn't initialize PAM: %s"),
732 pam_strerror(pamh
, rc
));
733 sleepexit(EXIT_FAILURE
);
736 /* hostname & tty are either set to NULL or their correct values,
737 * depending on how much we know
739 rc
= pam_set_item(pamh
, PAM_RHOST
, cxt
->hostname
);
740 if (is_pam_failure(rc
))
741 loginpam_err(pamh
, rc
);
743 rc
= pam_set_item(pamh
, PAM_TTY
, cxt
->tty_name
);
744 if (is_pam_failure(rc
))
745 loginpam_err(pamh
, rc
);
748 * Andrew.Taylor@cal.montage.ca: Provide a user prompt to PAM so that
749 * the "login: " prompt gets localized. Unfortunately, PAM doesn't have
750 * an interface to specify the "Password: " string (yet).
752 rc
= pam_set_item(pamh
, PAM_USER_PROMPT
, loginpam_get_prompt(cxt
));
753 if (is_pam_failure(rc
))
754 loginpam_err(pamh
, rc
);
756 /* we need't the original username. We have to follow PAM. */
758 cxt
->username
= NULL
;
764 static void loginpam_auth(struct login_context
*cxt
)
766 int rc
, failcount
= 0, show_unknown
, retries
;
767 const char *hostname
= cxt
->hostname
? cxt
->hostname
:
768 cxt
->tty_name
? cxt
->tty_name
: "<unknown>";
769 pam_handle_t
*pamh
= cxt
->pamh
;
771 /* if we didn't get a user on the command line, set it to NULL */
772 loginpam_get_username(pamh
, &cxt
->username
);
774 show_unknown
= getlogindefs_bool("LOG_UNKFAIL_ENAB", 0);
775 retries
= getlogindefs_num("LOGIN_RETRIES", LOGIN_MAX_TRIES
);
778 * There may be better ways to deal with some of these conditions, but
779 * at least this way I don't think we'll be giving away information...
781 * Perhaps someday we can trust that all PAM modules will pay attention
782 * to failure count and get rid of LOGIN_MAX_TRIES?
784 rc
= pam_authenticate(pamh
, 0);
786 while ((++failcount
< retries
) &&
787 ((rc
== PAM_AUTH_ERR
) ||
788 (rc
== PAM_USER_UNKNOWN
) ||
789 (rc
== PAM_CRED_INSUFFICIENT
) ||
790 (rc
== PAM_AUTHINFO_UNAVAIL
))) {
792 if (rc
== PAM_USER_UNKNOWN
&& !show_unknown
)
794 * logging unknown usernames may be a security issue if
795 * an user enter her password instead of her login name
797 cxt
->username
= NULL
;
799 loginpam_get_username(pamh
, &cxt
->username
);
802 _("FAILED LOGIN %d FROM %s FOR %s, %s"),
804 cxt
->username
? cxt
->username
: "(unknown)",
805 pam_strerror(pamh
, rc
));
810 fprintf(stderr
, _("Login incorrect\n\n"));
812 pam_set_item(pamh
, PAM_USER
, NULL
);
813 rc
= pam_authenticate(pamh
, 0);
816 if (is_pam_failure(rc
)) {
818 if (rc
== PAM_USER_UNKNOWN
&& !show_unknown
)
819 cxt
->username
= NULL
;
821 loginpam_get_username(pamh
, &cxt
->username
);
823 if (rc
== PAM_MAXTRIES
)
825 _("TOO MANY LOGIN TRIES (%d) FROM %s FOR %s, %s"),
827 cxt
->username
? cxt
->username
: "(unknown)",
828 pam_strerror(pamh
, rc
));
831 _("FAILED LOGIN SESSION FROM %s FOR %s, %s"),
833 cxt
->username
? cxt
->username
: "(unknown)",
834 pam_strerror(pamh
, rc
));
839 fprintf(stderr
, _("\nLogin incorrect\n"));
841 sleepexit(EXIT_SUCCESS
);
845 static void loginpam_acct(struct login_context
*cxt
)
848 pam_handle_t
*pamh
= cxt
->pamh
;
850 rc
= pam_acct_mgmt(pamh
, 0);
852 if (rc
== PAM_NEW_AUTHTOK_REQD
)
853 rc
= pam_chauthtok(pamh
, PAM_CHANGE_EXPIRED_AUTHTOK
);
855 if (is_pam_failure(rc
))
856 loginpam_err(pamh
, rc
);
859 * Grab the user information out of the password file for future usage
860 * First get the username that we are actually using, though.
862 rc
= loginpam_get_username(pamh
, &cxt
->username
);
863 if (is_pam_failure(rc
))
864 loginpam_err(pamh
, rc
);
866 if (!cxt
->username
|| !*cxt
->username
) {
867 warnx(_("\nSession setup problem, abort."));
868 syslog(LOG_ERR
, _("NULL user name in %s:%d. Abort."),
869 __FUNCTION__
, __LINE__
);
870 pam_end(pamh
, PAM_SYSTEM_ERR
);
871 sleepexit(EXIT_FAILURE
);
876 * Note that position of the pam_setcred() call is discussable:
878 * - the PAM docs recommends pam_setcred() before pam_open_session()
879 * - but the original RFC http://www.opengroup.org/rfc/mirror-rfc/rfc86.0.txt
880 * uses pam_setcred() after pam_open_session()
882 * The old login versions (before year 2011) followed the RFC. This is probably
883 * not optimal, because there could be dependence between some session modules
884 * and user's credentials.
886 * The best is probably to follow openssh and call pam_setcred() before and
887 * after pam_open_session(). -- kzak@redhat.com (18-Nov-2011)
890 static void loginpam_session(struct login_context
*cxt
)
893 pam_handle_t
*pamh
= cxt
->pamh
;
895 rc
= pam_setcred(pamh
, PAM_ESTABLISH_CRED
);
896 if (is_pam_failure(rc
))
897 loginpam_err(pamh
, rc
);
899 rc
= pam_open_session(pamh
, 0);
900 if (is_pam_failure(rc
)) {
901 pam_setcred(cxt
->pamh
, PAM_DELETE_CRED
);
902 loginpam_err(pamh
, rc
);
905 rc
= pam_setcred(pamh
, PAM_REINITIALIZE_CRED
);
906 if (is_pam_failure(rc
)) {
907 pam_close_session(pamh
, 0);
908 loginpam_err(pamh
, rc
);
913 * We need to check effective UID/GID. For example $HOME could be on root
914 * squashed NFS or on NFS with UID mapping and access(2) uses real UID/GID.
915 * The open(2) seems as the surest solution.
916 * -- kzak@redhat.com (10-Apr-2009)
918 static int effective_access(const char *path
, int mode
)
920 int fd
= open(path
, mode
);
923 return fd
== -1 ? -1 : 0;
927 * Check per accout or global hush-login setting.
929 * Hushed mode is enabled:
931 * a) if global (e.g. /etc/hushlogins) hush file exists:
932 * 1) for ALL ACCOUNTS if the file is empty
933 * 2) for the current user if the username or shell are found in the file
935 * b) if ~/.hushlogin file exists
937 * The ~/.hushlogin is ignored if the global hush file exists.
939 * The HUSHLOGIN_FILE login.def variable overwrites the default hush filename.
941 * Note that shadow-utils login(1) does not support "a1)". The "a1)" is
942 * necessary if you want to use PAM for "Last login" message.
944 * -- Karel Zak <kzak@redhat.com> (26-Aug-2011)
947 * Per-account check requires some explanation: As root we may not be able to
948 * read the directory of the user if it is on an NFS mounted filesystem. We
949 * temporarily set our effective uid to the user-uid making sure that we keep
950 * root privs. in the real uid.
952 * A portable solution would require a fork(), but we rely on Linux having the
955 static int get_hushlogin_status(struct passwd
*pwd
)
957 const char *files
[] = { _PATH_HUSHLOGINS
, _PATH_HUSHLOGIN
, NULL
};
962 file
= getlogindefs_str("HUSHLOGIN_FILE", NULL
);
965 return 0; /* empty HUSHLOGIN_FILE defined */
971 for (i
= 0; files
[i
]; i
++) {
976 /* Global hush-file*/
981 if (stat(file
, &st
) != 0)
982 continue; /* file does not exist */
985 return 1; /* for all accounts */
987 f
= fopen(file
, "r");
989 continue; /* ignore errors... */
991 while (ok
== 0 && fgets(buf
, sizeof(buf
), f
)) {
992 buf
[strlen(buf
) - 1] = '\0';
993 ok
= !strcmp(buf
, *buf
== '/' ? pwd
->pw_shell
:
998 return 1; /* found username/shell */
1000 return 0; /* ignore per-account files */
1003 /* Per-account setting */
1004 if (strlen(pwd
->pw_dir
) + sizeof(file
) + 2 > sizeof(buf
))
1007 uid_t ruid
= getuid();
1008 gid_t egid
= getegid();
1010 sprintf(buf
, "%s/%s", pwd
->pw_dir
, file
);
1011 setregid(-1, pwd
->pw_gid
);
1012 setreuid(0, pwd
->pw_uid
);
1013 ok
= effective_access(buf
, O_RDONLY
) == 0;
1014 setuid(0); /* setreuid doesn't do it alone! */
1019 return 1; /* enabled by user */
1027 * Detach the controlling terminal, fork, restore syslog stuff and create a new
1030 static void fork_session(struct login_context
*cxt
)
1032 struct sigaction sa
, oldsa_hup
, oldsa_term
;
1034 signal(SIGALRM
, SIG_DFL
);
1035 signal(SIGQUIT
, SIG_DFL
);
1036 signal(SIGTSTP
, SIG_IGN
);
1038 memset(&sa
, 0, sizeof(sa
));
1039 sa
.sa_handler
= SIG_IGN
;
1040 sigaction(SIGINT
, &sa
, NULL
);
1042 sigaction(SIGHUP
, &sa
, &oldsa_hup
); /* ignore when TIOCNOTTY */
1045 * detach the controlling tty
1046 * -- we needn't the tty in parent who waits for child only.
1047 * The child calls setsid() that detach from the tty as well.
1049 ioctl(0, TIOCNOTTY
, NULL
);
1052 * We have care about SIGTERM, because leave PAM session without
1053 * pam_close_session() is pretty bad thing.
1055 sa
.sa_handler
= sig_handler
;
1056 sigaction(SIGHUP
, &sa
, NULL
);
1057 sigaction(SIGTERM
, &sa
, &oldsa_term
);
1062 * We must fork before setuid() because we need to call
1063 * pam_close_session() as root.
1066 if (child_pid
< 0) {
1070 warn(_("fork failed"));
1072 pam_setcred(cxt
->pamh
, PAM_DELETE_CRED
);
1073 pam_end(cxt
->pamh
, pam_close_session(cxt
->pamh
, 0));
1074 sleepexit(EXIT_FAILURE
);
1079 * parent - wait for child to finish, then cleanup session
1084 sa
.sa_handler
= SIG_IGN
;
1085 sigaction(SIGQUIT
, &sa
, NULL
);
1086 sigaction(SIGINT
, &sa
, NULL
);
1088 /* wait as long as any child is there */
1089 while (wait(NULL
) == -1 && errno
== EINTR
) ;
1090 openlog("login", LOG_ODELAY
, LOG_AUTHPRIV
);
1092 pam_setcred(cxt
->pamh
, PAM_DELETE_CRED
);
1093 pam_end(cxt
->pamh
, pam_close_session(cxt
->pamh
, 0));
1100 sigaction(SIGHUP
, &oldsa_hup
, NULL
); /* restore old state */
1101 sigaction(SIGTERM
, &oldsa_term
, NULL
);
1106 * Problem: if the user's shell is a shell like ash that doesn't do
1107 * setsid() or setpgrp(), then a ctrl-\, sending SIGQUIT to every
1108 * process in the pgrp, will kill us.
1111 /* start new session */
1114 /* make sure we have a controlling tty */
1115 open_tty(cxt
->tty_path
);
1116 openlog("login", LOG_ODELAY
, LOG_AUTHPRIV
); /* reopen */
1119 * TIOCSCTTY: steal tty from other process group.
1121 if (ioctl(0, TIOCSCTTY
, 1))
1122 syslog(LOG_ERR
, _("TIOCSCTTY failed: %m"));
1123 signal(SIGINT
, SIG_DFL
);
1127 * Initialize $TERM, $HOME, ...
1129 static void init_environ(struct login_context
*cxt
)
1131 struct passwd
*pwd
= cxt
->pwd
;
1132 char *termenv
= NULL
, **env
;
1136 termenv
= getenv("TERM");
1137 termenv
= termenv
? xstrdup(termenv
) : "dumb";
1139 /* destroy environment unless user has requested preservation (-p) */
1140 if (!cxt
->keep_env
) {
1141 environ
= (char **) xmalloc(sizeof(char *));
1142 memset(environ
, 0, sizeof(char *));
1145 setenv("HOME", pwd
->pw_dir
, 0); /* legal to override */
1146 setenv("USER", pwd
->pw_name
, 1);
1147 setenv("SHELL", pwd
->pw_shell
, 1);
1148 setenv("TERM", termenv
, 1);
1151 logindefs_setenv("PATH", "ENV_PATH", _PATH_DEFPATH
);
1153 else if (logindefs_setenv("PATH", "ENV_ROOTPATH", NULL
) != 0)
1154 logindefs_setenv("PATH", "ENV_SUPATH", _PATH_DEFPATH_ROOT
);
1156 /* mailx will give a funny error msg if you forget this one */
1157 len
= snprintf(tmp
, sizeof(tmp
), "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
1158 if (len
> 0 && (size_t) len
+ 1 <= sizeof(tmp
))
1159 setenv("MAIL", tmp
, 0);
1161 /* LOGNAME is not documented in login(1) but HP-UX 6.5 does it. We'll
1162 * not allow modifying it.
1164 setenv("LOGNAME", pwd
->pw_name
, 1);
1166 env
= pam_getenvlist(cxt
->pamh
);
1167 for (i
= 0; env
&& env
[i
]; i
++)
1172 * Called for -h option, initialize cxt->{hostname,hostaddress}
1174 static void init_remote_info(struct login_context
*cxt
, char *remotehost
)
1178 struct addrinfo hints
, *info
= NULL
;
1182 get_thishost(cxt
, &domain
);
1184 if (domain
&& (p
= strchr(remotehost
, '.')) &&
1185 strcasecmp(p
+ 1, domain
) == 0)
1188 cxt
->hostname
= xstrdup(remotehost
);
1190 memset(&hints
, 0, sizeof(hints
));
1191 hints
.ai_flags
= AI_ADDRCONFIG
;
1192 cxt
->hostaddress
[0] = 0;
1194 if (getaddrinfo(cxt
->hostname
, NULL
, &hints
, &info
) == 0 && info
) {
1195 if (info
->ai_family
== AF_INET
) {
1196 struct sockaddr_in
*sa
=
1197 (struct sockaddr_in
*) info
->ai_addr
;
1199 memcpy(cxt
->hostaddress
, &(sa
->sin_addr
), sizeof(sa
->sin_addr
));
1201 } else if (info
->ai_family
== AF_INET6
) {
1202 struct sockaddr_in6
*sa
=
1203 (struct sockaddr_in6
*) info
->ai_addr
;
1205 memcpy(cxt
->hostaddress
, &(sa
->sin6_addr
), sizeof(sa
->sin6_addr
));
1211 int main(int argc
, char **argv
)
1215 char *childArgv
[10];
1220 char *pwdbuf
= NULL
;
1221 struct passwd
*pwd
= NULL
, _pwd
;
1223 struct login_context cxt
= {
1224 .tty_mode
= TTY_MODE
, /* tty chmod() */
1225 .pid
= getpid(), /* PID */
1226 .conv
= { misc_conv
, NULL
} /* PAM conversation function */
1229 timeout
= getlogindefs_num("LOGIN_TIMEOUT", LOGIN_TIMEOUT
);
1231 signal(SIGALRM
, timedout
);
1232 siginterrupt(SIGALRM
, 1); /* we have to interrupt syscalls like ioclt() */
1233 alarm((unsigned int)timeout
);
1234 signal(SIGQUIT
, SIG_IGN
);
1235 signal(SIGINT
, SIG_IGN
);
1237 setlocale(LC_ALL
, "");
1238 bindtextdomain(PACKAGE
, LOCALEDIR
);
1239 textdomain(PACKAGE
);
1241 setpriority(PRIO_PROCESS
, 0, 0);
1242 initproctitle(argc
, argv
);
1245 * -p is used by getty to tell login not to destroy the environment
1246 * -f is used to skip a second login authentication
1247 * -h is used by other servers to pass the name of the remote
1248 * host to login so that it may be placed in utmp and wtmp
1250 while ((c
= getopt(argc
, argv
, "fHh:pV")) != -1)
1263 _("login: -h for super-user only.\n"));
1266 init_remote_info(&cxt
, optarg
);
1274 printf(UTIL_LINUX_VERSION
);
1275 return EXIT_SUCCESS
;
1278 fprintf(stderr
, _("usage: login [ -p ] [ -h host ] [ -H ] [ -f username | username ]\n"));
1286 cxt
.username
= xstrdup(p
);
1288 /* wipe name - some people mistype their password here */
1289 /* (of course we are too late, but perhaps this helps a little ..) */
1294 for (cnt
= get_fd_tabsize() - 1; cnt
> 2; cnt
--)
1297 setpgrp(); /* set pgid to pid this means that setsid() will fail */
1299 openlog("login", LOG_ODELAY
, LOG_AUTHPRIV
);
1302 init_loginpam(&cxt
);
1304 /* login -f, then the user has already been authenticated */
1305 cxt
.noauth
= cxt
.noauth
&& getuid() == 0 ? 1 : 0;
1308 loginpam_auth(&cxt
);
1311 * Authentication may be skipped (for example, during krlogin, rlogin,
1312 * etc...), but it doesn't mean that we can skip other account checks.
1313 * The account could be disabled or password expired (althought
1314 * kerberos ticket is valid). -- kzak@redhat.com (22-Feb-2006)
1316 loginpam_acct(&cxt
);
1318 if (!(cxt
.pwd
= get_passwd_entry(cxt
.username
, &pwdbuf
, &_pwd
))) {
1319 warnx(_("\nSession setup problem, abort."));
1320 syslog(LOG_ERR
, _("Invalid user name \"%s\" in %s:%d. Abort."),
1321 cxt
.username
, __FUNCTION__
, __LINE__
);
1322 pam_end(cxt
.pamh
, PAM_SYSTEM_ERR
);
1323 sleepexit(EXIT_FAILURE
);
1327 cxt
.username
= pwd
->pw_name
;
1330 * Initialize the supplementary group list. This should be done before
1331 * pam_setcred because the PAM modules might add groups during
1334 * For root we don't call initgroups, instead we call setgroups with
1335 * group 0. This avoids the need to step through the whole group file,
1336 * which can cause problems if NIS, NIS+, LDAP or something similar
1337 * is used and the machine has network problems.
1339 retcode
= pwd
->pw_uid
? initgroups(cxt
.username
, pwd
->pw_gid
) : /* user */
1340 setgroups(0, NULL
); /* root */
1342 syslog(LOG_ERR
, _("groups initialization failed: %m"));
1343 warnx(_("\nSession setup problem, abort."));
1344 pam_end(cxt
.pamh
, PAM_SYSTEM_ERR
);
1345 sleepexit(EXIT_FAILURE
);
1349 * Open PAM session (after successful authentication and account check)
1351 loginpam_session(&cxt
);
1353 /* committed to login -- turn off timeout */
1354 alarm((unsigned int)0);
1358 cxt
.quiet
= get_hushlogin_status(pwd
);
1366 if (setgid(pwd
->pw_gid
) < 0 && pwd
->pw_gid
) {
1367 syslog(LOG_ALERT
, _("setgid() failed"));
1371 if (pwd
->pw_shell
== NULL
|| *pwd
->pw_shell
== '\0')
1372 pwd
->pw_shell
= _PATH_BSHELL
;
1374 init_environ(&cxt
); /* init $HOME, $TERM ... */
1376 setproctitle("login", cxt
.username
);
1383 #ifdef LOGIN_STAT_MAIL
1385 * This turns out to be a bad idea: when the mail spool
1386 * is NFS mounted, and the NFS connection hangs, the
1387 * login hangs, even root cannot login.
1388 * Checking for mail should be done from the shell.
1394 mail
= getenv("MAIL");
1395 if (mail
&& stat(mail
, &st
) == 0 && st
.st_size
!= 0) {
1396 if (st
.st_mtime
> st
.st_atime
)
1397 printf(_("You have new mail.\n"));
1399 printf(_("You have mail.\n"));
1406 * Detach the controlling terminal, fork() and create, new session
1407 * and reinilizalize syslog stuff.
1411 /* discard permissions last so can't get killed and drop core */
1412 if (setuid(pwd
->pw_uid
) < 0 && pwd
->pw_uid
) {
1413 syslog(LOG_ALERT
, _("setuid() failed"));
1417 /* wait until here to change directory! */
1418 if (chdir(pwd
->pw_dir
) < 0) {
1419 warn(_("%s: change directory failed"), pwd
->pw_dir
);
1421 if (!getlogindefs_bool("DEFAULT_HOME", 1))
1426 printf(_("Logging in with home = \"/\".\n"));
1429 /* if the shell field has a space: treat it like a shell script */
1430 if (strchr(pwd
->pw_shell
, ' ')) {
1431 buff
= xmalloc(strlen(pwd
->pw_shell
) + 6);
1433 strcpy(buff
, "exec ");
1434 strcat(buff
, pwd
->pw_shell
);
1435 childArgv
[childArgc
++] = "/bin/sh";
1436 childArgv
[childArgc
++] = "-sh";
1437 childArgv
[childArgc
++] = "-c";
1438 childArgv
[childArgc
++] = buff
;
1440 char tbuf
[PATH_MAX
+ 2], *p
;
1443 xstrncpy(tbuf
+ 1, ((p
= strrchr(pwd
->pw_shell
, '/')) ?
1444 p
+ 1 : pwd
->pw_shell
), sizeof(tbuf
) - 1);
1446 childArgv
[childArgc
++] = pwd
->pw_shell
;
1447 childArgv
[childArgc
++] = xstrdup(tbuf
);
1450 childArgv
[childArgc
++] = NULL
;
1452 execvp(childArgv
[0], childArgv
+ 1);
1454 if (!strcmp(childArgv
[0], "/bin/sh"))
1455 warn(_("couldn't exec shell script"));
1457 warn(_("no shell"));