4 * This program gives Linux machines a reasonable secure way to boot single
5 * user. It forces the user to supply the root password before a shell is
6 * started. If there is a shadow password file and the encrypted root password
7 * is "x" the shadow password will be used.
9 * Copyright (C) 1998-2003 Miquel van Smoorenburg.
10 * Copyright (C) 2012 Karel Zak <kzak@redhat.com>
11 * Copyright (C) 2012 Werner Fink <werner@suse.de>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <sys/types.h>
42 #include <sys/ioctl.h>
47 #ifdef HAVE_LIBSELINUX
48 # include <selinux/selinux.h>
49 # include <selinux/get_context_list.h>
54 # include <sys/param.h>
55 # include <linux/serial.h>
59 #include "closestream.h"
62 #include "pathnames.h"
63 #ifdef USE_PLYMOUTH_SUPPORT
64 # include "plymouth-ctrl.h"
68 #include "sulogin-consoles.h"
71 static unsigned int timeout
;
73 static volatile uint32_t openfd
; /* Remember higher file descriptors */
75 static struct sigaction saved_sigint
;
76 static struct sigaction saved_sigtstp
;
77 static struct sigaction saved_sigquit
;
78 static struct sigaction saved_sighup
;
79 static struct sigaction saved_sigchld
;
81 static volatile sig_atomic_t alarm_rised
;
82 static volatile sig_atomic_t sigchild
;
84 #define SULOGIN_PASSWORD_BUFSIZ 128
91 # warning "WEXITED is missing, sulogin may not work as expected"
95 static int locked_account_password(const char * const passwd
)
97 if (passwd
&& (*passwd
== '*' || *passwd
== '!'))
103 * Fix the tty modes and set reasonable defaults.
105 static void tcinit(struct console
*con
)
107 int flags
= 0, mode
= 0;
108 struct termios
*tio
= &con
->tio
;
109 const int fd
= con
->fd
;
110 #if defined(TIOCGSERIAL)
111 struct serial_struct serinfo
;
113 #ifdef USE_PLYMOUTH_SUPPORT
115 int i
= (plymouth_command(MAGIC_PING
)) ? PLYMOUTH_TERMIOS_FLAGS_DELAY
: 0;
117 plymouth_command(MAGIC_QUIT
);
120 * With plymouth the termios flags become changed after this
121 * function had changed the termios.
123 memset(&lock
, 0, sizeof(struct termios
));
124 if (ioctl(fd
, TIOCGLCKTRMIOS
, &lock
) < 0)
126 if (!lock
.c_iflag
&& !lock
.c_oflag
&& !lock
.c_cflag
&& !lock
.c_lflag
)
130 memset(&lock
, 0, sizeof(struct termios
));
131 ioctl(fd
, TIOCSLCKTRMIOS
, &lock
);
135 #if defined(TIOCGSERIAL)
136 if (ioctl(fd
, TIOCGSERIAL
, &serinfo
) >= 0)
137 con
->flags
|= CON_SERIAL
;
140 # if defined(KDGKBMODE)
141 if (ioctl(fd
, KDGKBMODE
, &mode
) < 0)
142 con
->flags
|= CON_SERIAL
;
147 if (tcgetattr(fd
, tio
) < 0) {
149 #if defined(KDGKBMODE) || defined(TIOCGSERIAL)
150 if (con
->flags
& CON_SERIAL
) { /* Try to recover this */
152 # if defined(TIOCGSERIAL)
153 serinfo
.flags
|= ASYNC_SKIP_TEST
; /* Skip test of UART */
155 if (ioctl(fd
, TIOCSSERIAL
, &serinfo
) < 0)
157 if (ioctl(fd
, TIOCSERCONFIG
) < 0) /* Try to autoconfigure */
159 if (ioctl(fd
, TIOCGSERIAL
, &serinfo
) < 0)
160 goto tcgeterr
; /* Ouch */
162 if (tcgetattr(fd
, tio
) < 0) /* Retry to get tty attributes */
165 # if defined(TIOCGSERIAL)
171 FILE *fcerr
= fdopen(fd
, "w");
173 fprintf(fcerr
, _("tcgetattr failed"));
176 warn(_("tcgetattr failed"));
178 con
->flags
&= ~CON_SERIAL
;
180 con
->flags
|= CON_NOTTY
;
182 con
->flags
|= CON_EIO
;
189 /* Handle lines other than virtual consoles here */
190 #if defined(KDGKBMODE) || defined(TIOCGSERIAL)
191 if (con
->flags
& CON_SERIAL
)
194 speed_t ispeed
, ospeed
;
198 /* Flush input and output queues on modem lines */
199 tcflush(fd
, TCIOFLUSH
);
201 ispeed
= cfgetispeed(tio
);
202 ospeed
= cfgetospeed(tio
);
204 if (!ispeed
) ispeed
= TTYDEF_SPEED
;
205 if (!ospeed
) ospeed
= TTYDEF_SPEED
;
207 tio
->c_cflag
= CREAD
| CS8
| HUPCL
| (tio
->c_cflag
& CLOCAL
);
210 tio
->c_oflag
&= OPOST
| ONLCR
;
212 cfsetispeed(tio
, ispeed
);
213 cfsetospeed(tio
, ospeed
);
215 #ifdef HAVE_STRUCT_TERMIOS_C_LINE
218 tio
->c_cc
[VTIME
] = 0;
221 if (ioctl(fd
, TIOCGWINSZ
, &ws
) == 0) {
224 if (ws
.ws_row
== 0) {
228 if (ws
.ws_col
== 0) {
233 ignore_result( ioctl(fd
, TIOCSWINSZ
, &ws
) );
236 setlocale(LC_CTYPE
, "POSIX");
239 #if defined(IUTF8) && defined(KDGKBMODE)
240 /* Handle mode of current keyboard setup, e.g. for UTF-8 */
243 setlocale(LC_CTYPE
, "C.UTF-8");
244 flags
|= UL_TTY_UTF8
;
250 setlocale(LC_CTYPE
, "POSIX");
254 setlocale(LC_CTYPE
, "POSIX");
256 reset_virtual_console(tio
, flags
);
258 if (tcsetattr(fd
, TCSANOW
, tio
))
259 warn(_("tcsetattr failed"));
261 /* Enable blocking mode for read and write */
262 if ((flags
= fcntl(fd
, F_GETFL
, 0)) != -1)
263 ignore_result( fcntl(fd
, F_SETFL
, flags
& ~O_NONBLOCK
) );
267 * Finalize the tty modes on modem lines.
269 static void tcfinal(struct console
*con
)
271 struct termios
*tio
= &con
->tio
;
272 const int fd
= con
->fd
;
274 if (con
->flags
& CON_EIO
)
276 if ((con
->flags
& CON_SERIAL
) == 0) {
277 xsetenv("TERM", "linux", 1);
280 if (con
->flags
& CON_NOTTY
) {
281 xsetenv("TERM", "dumb", 1);
285 #if defined (__s390__) || defined (__s390x__)
286 xsetenv("TERM", "dumb", 1);
288 xsetenv("TERM", "vt102", 1);
290 tio
->c_iflag
|= (IXON
| IXOFF
);
291 tio
->c_lflag
|= (ICANON
| ISIG
| ECHO
|ECHOE
|ECHOK
|ECHOKE
);
292 tio
->c_oflag
|= OPOST
;
294 tio
->c_cc
[VINTR
] = CINTR
;
295 tio
->c_cc
[VQUIT
] = CQUIT
;
296 tio
->c_cc
[VERASE
] = con
->cp
.erase
;
297 tio
->c_cc
[VKILL
] = con
->cp
.kill
;
298 tio
->c_cc
[VEOF
] = CEOF
;
300 tio
->c_cc
[VSWTC
] = _POSIX_VDISABLE
;
301 #elif defined(VSWTCH)
302 tio
->c_cc
[VSWTCH
] = _POSIX_VDISABLE
;
304 tio
->c_cc
[VSTART
] = CSTART
;
305 tio
->c_cc
[VSTOP
] = CSTOP
;
306 tio
->c_cc
[VSUSP
] = CSUSP
;
307 tio
->c_cc
[VEOL
] = _POSIX_VDISABLE
;
309 if (con
->cp
.eol
== CR
) {
310 tio
->c_iflag
|= ICRNL
;
311 tio
->c_iflag
&= ~(INLCR
|IGNCR
);
312 tio
->c_oflag
|= ONLCR
;
313 tio
->c_oflag
&= ~(OCRNL
|ONLRET
);
316 switch (con
->cp
.parity
) {
319 tio
->c_cflag
&= ~(PARODD
| PARENB
);
320 tio
->c_iflag
&= ~(INPCK
| ISTRIP
);
322 case 1: /* odd parity */
323 tio
->c_cflag
|= PARODD
;
325 case 2: /* even parity */
326 tio
->c_cflag
|= PARENB
;
327 tio
->c_iflag
|= (INPCK
| ISTRIP
);
329 case (1 | 2): /* no parity bit */
330 tio
->c_cflag
&= ~CSIZE
;
335 /* Set line attributes */
336 tcsetattr(fd
, TCSANOW
, tio
);
342 static void alrm_handler(int sig
__attribute__((unused
)))
344 /* Timeout expired */
348 static void chld_handler(int sig
__attribute__((unused
)))
353 static void mask_signal(int signal
, void (*handler
)(int),
354 struct sigaction
*origaction
)
356 struct sigaction newaction
;
358 newaction
.sa_handler
= handler
;
359 sigemptyset(&newaction
.sa_mask
);
360 newaction
.sa_flags
= 0;
362 sigaction(signal
, &newaction
, origaction
);
365 static void unmask_signal(int signal
, struct sigaction
*sa
)
367 sigaction(signal
, sa
, NULL
);
371 * See if an encrypted password is valid. The encrypted password is checked for
372 * traditional-style DES and FreeBSD-style MD5 encryption.
374 static int valid(const char *pass
)
387 * up to 4 bytes for the signature e.g. $1$
389 for (s
= pass
+1; *s
&& *s
!= '$'; s
++);
394 if ((off
= (off_t
)(s
-pass
)) > 4 || off
< 3)
397 memset(id
, '\0', sizeof(id
));
398 strncpy(id
, pass
, off
);
401 * up to 16 bytes for the salt
403 for (; *s
&& *s
!= '$'; s
++);
408 if ((off_t
)(s
-pass
) > 16)
414 * the MD5 hash (128 bits or 16 bytes) encoded in base64 = 22 bytes
416 if ((strcmp(id
, "$1$") == 0) && (len
< 22 || len
> 24))
420 * the SHA-256 hash 43 bytes
422 if ((strcmp(id
, "$5$") == 0) && (len
< 42 || len
> 44))
426 * the SHA-512 hash 86 bytes
428 if ((strcmp(id
, "$6$") == 0) && (len
< 85 || len
> 87))
436 if (strlen(pass
) != 13)
439 for (s
= pass
; *s
; s
++) {
440 if ((*s
< '0' || *s
> '9') &&
441 (*s
< 'a' || *s
> 'z') &&
442 (*s
< 'A' || *s
> 'Z') &&
443 *s
!= '.' && *s
!= '/')
450 * Set a variable if the value is not NULL.
452 static inline void set(char **var
, char *val
)
459 * Get the root password entry.
461 static struct passwd
*getrootpwent(int try_manually
)
463 static struct passwd pwd
;
467 static char line
[2 * BUFSIZ
];
468 static char sline
[2 * BUFSIZ
];
472 * First, we try to get the password the standard way using normal
475 if ((pw
= getpwnam("root")) &&
476 !strcmp(pw
->pw_passwd
, "x") &&
477 (spw
= getspnam("root")))
478 pw
->pw_passwd
= spw
->sp_pwdp
;
480 if (pw
|| !try_manually
)
484 * If we come here, we could not retrieve the root password through
485 * library calls and we try to read the password and shadow files
488 pwd
.pw_name
= "root";
490 pwd
.pw_gecos
= "Super User";
496 if ((fp
= fopen(_PATH_PASSWD
, "r")) == NULL
) {
497 warn(_("cannot open %s"), _PATH_PASSWD
);
502 * Find root in the password file.
504 while ((p
= fgets(line
, sizeof(line
), fp
)) != NULL
) {
505 if (strncmp(line
, "root:", 5) != 0)
508 set(&pwd
.pw_passwd
, strsep(&p
, ":"));
511 set(&pwd
.pw_gecos
, strsep(&p
, ":"));
512 set(&pwd
.pw_dir
, strsep(&p
, ":"));
513 set(&pwd
.pw_shell
, strsep(&p
, "\n"));
520 * If the encrypted password is valid or not found, return.
523 warnx(_("%s: no entry for root\n"), _PATH_PASSWD
);
526 if (valid(pwd
.pw_passwd
))
530 * The password is invalid. If there is a shadow password, try it.
532 *pwd
.pw_passwd
= '\0';
533 if ((fp
= fopen(_PATH_SHADOW_PASSWD
, "r")) == NULL
) {
534 warn(_("cannot open %s"), _PATH_PASSWD
);
537 while ((p
= fgets(sline
, sizeof(sline
), fp
)) != NULL
) {
538 if (strncmp(sline
, "root:", 5) != 0)
541 set(&pwd
.pw_passwd
, strsep(&p
, ":"));
547 * If the password is still invalid, NULL it, and return.
550 warnx(_("%s: no entry for root"), _PATH_SHADOW_PASSWD
);
551 *pwd
.pw_passwd
= '\0';
553 /* locked account passwords are valid too */
554 if (!locked_account_password(pwd
.pw_passwd
) && !valid(pwd
.pw_passwd
)) {
555 warnx(_("%s: root password garbled"), _PATH_SHADOW_PASSWD
);
556 *pwd
.pw_passwd
= '\0';
562 * Ask by prompt for the password.
564 static void doprompt(const char *crypted
, struct console
*con
, int deny
)
568 if (con
->flags
& CON_SERIAL
) {
571 * For prompting: map NL in output to CR-NL
572 * otherwise we may see stairs in the output.
574 tty
.c_oflag
|= (ONLCR
| OPOST
);
575 tcsetattr(con
->fd
, TCSADRAIN
, &tty
);
578 con
->file
= fdopen(con
->fd
, "r+");
584 fprintf(con
->file
, _("\nCannot open access to console, the root account is locked.\n"
585 "See sulogin(8) man page for more details.\n\n"
586 "Press Enter to continue.\n"));
588 #if defined(USE_ONELINE)
589 if (crypted
[0] && !locked_account_password(crypted
))
590 fprintf(con
->file
, _("Give root password for login: "));
592 fprintf(con
->file
, _("Press Enter for login: "));
594 if (crypted
[0] && !locked_account_password(crypted
))
595 fprintf(con
->file
, _("Give root password for maintenance\n"));
597 fprintf(con
->file
, _("Press Enter for maintenance\n"));
598 fprintf(con
->file
, _("(or press Control-D to continue): "));
603 if (con
->flags
& CON_SERIAL
)
604 tcsetattr(con
->fd
, TCSADRAIN
, &con
->tio
);
608 * Make sure to have an own session and controlling terminal
610 static void setup(struct console
*con
)
613 const pid_t pid
= getpid(), pgrp
= getpgid(0), ppgrp
= getpgid(getppid());
616 if (con
->flags
& CON_NOTTY
)
618 if (con
->flags
& CON_EIO
)
621 ttypgrp
= tcgetpgrp(fd
);
624 * Only go through this trouble if the new
625 * tty doesn't fall in this process group.
627 if (pgrp
!= ttypgrp
&& ppgrp
!= ttypgrp
) {
628 if (pid
!= getsid(0)) {
629 if (pid
== getpgid(0))
630 setpgid(0, getpgid(getppid()));
634 mask_signal(SIGHUP
, SIG_IGN
, &saved_sighup
);
636 ioctl(STDIN_FILENO
, TIOCNOTTY
, (char *)1);
637 unmask_signal(SIGHUP
, &saved_sighup
);
638 if (fd
> STDIN_FILENO
) close(STDIN_FILENO
);
639 if (fd
> STDOUT_FILENO
) close(STDOUT_FILENO
);
640 if (fd
> STDERR_FILENO
) close(STDERR_FILENO
);
642 ioctl(fd
, TIOCSCTTY
, (char *)1);
643 tcsetpgrp(fd
, ppgrp
);
646 dup2(fd
, STDIN_FILENO
);
647 dup2(fd
, STDOUT_FILENO
);
648 dup2(fd
, STDERR_FILENO
);
649 con
->fd
= STDIN_FILENO
;
651 for (fd
= STDERR_FILENO
+1; fd
< 32; fd
++) {
652 if (openfd
& (1<<fd
)) {
660 * Ask for the password. Note that there is no default timeout as we normally
661 * skip this during boot.
663 static char *getpasswd(struct console
*con
)
667 static char pass
[SULOGIN_PASSWORD_BUFSIZ
], *ptr
;
673 const int fd
= con
->fd
;
675 if (con
->flags
& CON_EIO
)
683 tty
.c_iflag
&= ~(IUCLC
|IXON
|IXOFF
|IXANY
);
684 tty
.c_lflag
&= ~(ECHO
|ECHOE
|ECHOK
|ECHONL
|TOSTOP
|ISIG
);
686 if ((con
->flags
& CON_NOTTY
) == 0)
687 tc
= (tcsetattr(fd
, TCSAFLUSH
, &tty
) == 0);
689 sigemptyset(&sa
.sa_mask
);
690 sa
.sa_handler
= alrm_handler
;
692 sigaction(SIGALRM
, &sa
, NULL
);
698 cp
->eol
= *ptr
= '\0';
700 eightbit
= ((con
->flags
& CON_SERIAL
) == 0 || (tty
.c_cflag
& (PARODD
|PARENB
)) == 0);
702 while (cp
->eol
== '\0') {
705 if (read(fd
, &c
, 1) < 1) {
706 if (errno
== EINTR
|| errno
== EAGAIN
) {
718 con
->flags
|= CON_EIO
;
721 warn(_("cannot read %s"), con
->tty
);
731 else if (c
!= (ascval
= (c
& 0177))) {
733 for (bits
= 1, mask
= 1; mask
& 0177; mask
<<= 1) {
737 cp
->parity
|= ((bits
& 1) ? 1 : 2);
757 while (ptr
> &pass
[0])
764 if ((size_t)(ptr
- &pass
[0]) >= (sizeof(pass
) -1 )) {
765 fprintf(stderr
, "sulogin: input overrun at %s\n\r", con
->tty
);
776 tcsetattr(fd
, TCSAFLUSH
, &con
->tio
);
780 #ifdef HAVE_EXPLICIT_BZERO
782 explicit_bzero(pass
, sizeof(pass
));
788 * Password was OK, execute a shell.
790 static void sushell(struct passwd
*pwd
)
792 char shell
[PATH_MAX
];
795 char const *su_shell
;
798 * Set directory and shell.
800 if (chdir(pwd
->pw_dir
) != 0) {
801 warn(_("%s: change directory failed"), pwd
->pw_dir
);
802 printf(_("Logging in with home = \"/\".\n"));
805 warn(_("change directory to system root failed"));
808 if ((p
= getenv("SUSHELL")) != NULL
)
810 else if ((p
= getenv("sushell")) != NULL
)
813 if (pwd
->pw_shell
[0])
814 su_shell
= pwd
->pw_shell
;
816 su_shell
= "/bin/sh";
818 if ((p
= strrchr(su_shell
, '/')) == NULL
)
823 snprintf(shell
, sizeof(shell
), profile
? "-%s" : "%s", p
);
826 * Set some important environment variables.
828 if (getcwd(home
, sizeof(home
)) == NULL
)
831 xsetenv("HOME", home
, 1);
832 xsetenv("LOGNAME", "root", 1);
833 xsetenv("USER", "root", 1);
835 xsetenv("SHLVL","0",1);
838 * Try to execute a shell.
840 xsetenv("SHELL", su_shell
, 1);
841 unmask_signal(SIGINT
, &saved_sigint
);
842 unmask_signal(SIGTSTP
, &saved_sigtstp
);
843 unmask_signal(SIGQUIT
, &saved_sigquit
);
844 mask_signal(SIGHUP
, SIG_DFL
, NULL
);
846 #ifdef HAVE_LIBSELINUX
847 if (is_selinux_enabled() > 0) {
852 if (getseuserbyname("root", &seuser
, &level
) == 0) {
853 if (get_default_context_with_level(seuser
, level
, 0, &scon
) == 0) {
854 if (setexeccon(scon
) != 0)
855 warnx(_("setexeccon failed"));
863 execl(su_shell
, shell
, (char *)NULL
);
864 warn(_("failed to execute %s"), su_shell
);
866 xsetenv("SHELL", "/bin/sh", 1);
867 execl("/bin/sh", profile
? "-sh" : "sh", (char *)NULL
);
868 warn(_("failed to execute %s"), "/bin/sh");
871 static void usage(void)
874 fputs(USAGE_HEADER
, out
);
876 " %s [options] [tty device]\n"), program_invocation_short_name
);
878 fputs(USAGE_SEPARATOR
, out
);
879 fputs(_("Single-user login.\n"), out
);
881 fputs(USAGE_OPTIONS
, out
);
882 fputs(_(" -p, --login-shell start a login shell\n"
883 " -t, --timeout <seconds> max time to wait for a password (default: no limit)\n"
884 " -e, --force examine password files directly if getpwnam(3) fails\n"),
887 fputs(USAGE_SEPARATOR
, out
);
888 printf(USAGE_HELP_OPTIONS(26));
889 printf(USAGE_MAN_TAIL("sulogin(8)"));
894 int main(int argc
, char **argv
)
896 struct list_head
*ptr
, consoles
;
900 const struct timespec sigwait
= { .tv_sec
= 0, .tv_nsec
= 50000000 };
901 siginfo_t status
= { 0 };
903 int c
, reconnect
= 0;
908 static const struct option longopts
[] = {
909 { "login-shell", no_argument
, NULL
, 'p' },
910 { "timeout", required_argument
, NULL
, 't' },
911 { "force", no_argument
, NULL
, 'e' },
912 { "help", no_argument
, NULL
, 'h' },
913 { "version", no_argument
, NULL
, 'V' },
917 INIT_LIST_HEAD(&consoles
);
920 * If we are init we need to set up an own session.
922 if ((pid
= getpid()) == 1) {
924 ignore_result( ioctl(STDIN_FILENO
, TIOCSCTTY
, (char *) 1) );
927 setlocale(LC_ALL
, "");
928 bindtextdomain(PACKAGE
, LOCALEDIR
);
930 close_stdout_atexit();
933 * See if we have a timeout flag.
935 while ((c
= getopt_long(argc
, argv
, "ehpt:V", longopts
, NULL
)) != -1) {
938 timeout
= strtou32_or_err(optarg
, _("invalid timeout argument"));
948 static const char *features
[] = {
949 #ifdef USE_SULOGIN_EMERGENCY_MOUNT
954 print_version_with_features(EXIT_SUCCESS
, features
);
959 /* Do not exit! getopt prints a warning. */
965 errx(EXIT_FAILURE
, _("only superuser can run this program"));
967 mask_signal(SIGQUIT
, SIG_IGN
, &saved_sigquit
);
968 mask_signal(SIGTSTP
, SIG_IGN
, &saved_sigtstp
);
969 mask_signal(SIGINT
, SIG_IGN
, &saved_sigint
);
970 mask_signal(SIGHUP
, SIG_IGN
, &saved_sighup
);
973 emergency_do_mounts();
974 atexit( emergency_do_umounts
);
977 * See if we need to open an other tty device.
982 if (!tty
|| *tty
== '\0')
983 tty
= getenv("CONSOLE");
986 * Detect possible consoles, use stdin as fallback.
987 * If an optional tty is given, reconnect it to stdin.
989 reconnect
= detect_consoles(tty
, STDIN_FILENO
, &consoles
);
992 * If previous stdin was not the specified tty and therefore reconnected
993 * to the specified tty also reconnect stdout and stderr.
996 if (isatty(STDOUT_FILENO
) == 0)
997 dup2(STDOUT_FILENO
, STDIN_FILENO
);
998 if (isatty(STDERR_FILENO
) == 0)
999 dup2(STDOUT_FILENO
, STDERR_FILENO
);
1005 if (list_empty(&consoles
)) {
1008 err(EXIT_FAILURE
, _("cannot open console"));
1012 * Get the root password.
1014 if ((pwd
= getrootpwent(opt_e
)) == NULL
) {
1015 warnx(_("cannot open password database"));
1017 return EXIT_FAILURE
;
1021 * Ask for the password on the consoles.
1023 list_for_each(ptr
, &consoles
) {
1024 con
= list_entry(ptr
, struct console
, entry
);
1025 if (con
->id
>= CONMAX
)
1028 openfd
|= (1 << con
->fd
);
1032 if ((con
->fd
= open(con
->tty
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
)) < 0)
1034 openfd
|= (1 << con
->fd
);
1037 ptr
= (&consoles
)->next
;
1039 if (ptr
->next
== &consoles
) {
1040 con
= list_entry(ptr
, struct console
, entry
);
1045 mask_signal(SIGCHLD
, chld_handler
, &saved_sigchld
);
1047 con
= list_entry(ptr
, struct console
, entry
);
1048 if (con
->id
>= CONMAX
)
1050 if (con
->flags
& CON_EIO
)
1053 switch ((con
->pid
= fork())) {
1055 mask_signal(SIGCHLD
, SIG_DFL
, NULL
);
1056 dup2(con
->fd
, STDERR_FILENO
);
1060 const char *passwd
= pwd
->pw_passwd
;
1063 int deny
= !opt_e
&& locked_account_password(pwd
->pw_passwd
);
1065 doprompt(passwd
, con
, deny
);
1067 if ((answer
= getpasswd(con
)) == NULL
)
1070 #ifdef HAVE_EXPLICIT_BZERO
1071 explicit_bzero(answer
, SULOGIN_PASSWORD_BUFSIZ
);
1076 /* no password or locked account */
1077 if (!passwd
[0] || locked_account_password(passwd
))
1080 const char *cryptbuf
;
1081 cryptbuf
= crypt(answer
, passwd
);
1082 if (cryptbuf
== NULL
)
1083 warn(_("crypt failed"));
1084 else if (strcmp(cryptbuf
, pwd
->pw_passwd
) == 0)
1087 #ifdef HAVE_EXPLICIT_BZERO
1088 explicit_bzero(answer
, SULOGIN_PASSWORD_BUFSIZ
);
1091 /* sushell() unmask signals */
1094 mask_signal(SIGQUIT
, SIG_IGN
, &saved_sigquit
);
1095 mask_signal(SIGTSTP
, SIG_IGN
, &saved_sigtstp
);
1096 mask_signal(SIGINT
, SIG_IGN
, &saved_sigint
);
1098 fprintf(stderr
, _("cannot execute su shell\n\n"));
1101 fprintf(stderr
, _("Login incorrect\n\n"));
1105 warnx(_("Timed out\n\n"));
1108 * User pressed Control-D.
1112 warn(_("fork failed"));
1120 } while (ptr
!= &consoles
);
1126 ret
= waitid(P_ALL
, 0, &status
, WEXITED
);
1131 if (errno
== ECHILD
)
1137 errx(EXIT_FAILURE
, _("cannot wait on su shell\n\n"));
1141 list_for_each(ptr
, &consoles
) {
1142 con
= list_entry(ptr
, struct console
, entry
);
1148 if (con
->pid
== status
.si_pid
)
1151 kill(con
->pid
, SIGTERM
);
1157 sigaddset(&set
, SIGCHLD
);
1166 ret
= waitid(P_ALL
, 0, &status
, WEXITED
|WNOHANG
);
1169 if (errno
== ECHILD
)
1175 if (!ret
&& status
.si_pid
> 0) {
1176 list_for_each(ptr
, &consoles
) {
1177 con
= list_entry(ptr
, struct console
, entry
);
1183 if (con
->pid
== status
.si_pid
) {
1191 signum
= sigtimedwait(&set
, NULL
, &sigwait
);
1192 if (signum
!= SIGCHLD
&& signum
< 0 && errno
== EAGAIN
)
1197 mask_signal(SIGCHLD
, SIG_DFL
, NULL
);
1198 return EXIT_SUCCESS
;