sulogin: Dorp incorrect comment about suid-ness
[oweals/busybox.git] / loginutils / sulogin.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini sulogin implementation for busybox
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6  */
7 //config:config SULOGIN
8 //config:       bool "sulogin"
9 //config:       default y
10 //config:       select FEATURE_SYSLOG
11 //config:       help
12 //config:         sulogin is invoked when the system goes into single user
13 //config:         mode (this is done through an entry in inittab).
14
15 //applet:IF_SULOGIN(APPLET(sulogin, BB_DIR_SBIN, BB_SUID_DROP))
16
17 //kbuild:lib-$(CONFIG_SULOGIN) += sulogin.o
18
19 //usage:#define sulogin_trivial_usage
20 //usage:       "[-t N] [TTY]"
21 //usage:#define sulogin_full_usage "\n\n"
22 //usage:       "Single user login\n"
23 //usage:     "\n        -t N    Timeout"
24
25 #include "libbb.h"
26 #include <syslog.h>
27
28 int sulogin_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
29 int sulogin_main(int argc UNUSED_PARAM, char **argv)
30 {
31         int timeout = 0;
32         struct passwd *pwd;
33         const char *shell;
34
35         logmode = LOGMODE_BOTH;
36         openlog(applet_name, 0, LOG_AUTH);
37
38         opt_complementary = "t+"; /* -t N */
39         getopt32(argv, "t:", &timeout);
40         argv += optind;
41
42         if (argv[0]) {
43                 close(0);
44                 close(1);
45                 dup(xopen(argv[0], O_RDWR));
46                 close(2);
47                 dup(0);
48         }
49
50         /* Malicious use like "sulogin /dev/sda"? */
51         if (!isatty(0) || !isatty(1) || !isatty(2)) {
52                 logmode = LOGMODE_SYSLOG;
53                 bb_error_msg_and_die("not a tty");
54         }
55
56         /* Clear dangerous stuff, set PATH */
57         sanitize_env_if_suid();
58
59         pwd = getpwuid(0);
60         if (!pwd) {
61                 goto auth_error;
62         }
63
64         while (1) {
65                 int r;
66
67                 r = ask_and_check_password_extended(pwd, timeout,
68                         "Give root password for system maintenance\n"
69                         "(or type Control-D for normal startup):"
70                 );
71                 if (r < 0) {
72                         /* ^D, ^C, timeout, or read error */
73                         bb_info_msg("Normal startup");
74                         return 0;
75                 }
76                 if (r > 0) {
77                         break;
78                 }
79                 bb_do_delay(LOGIN_FAIL_DELAY);
80                 bb_info_msg("Login incorrect");
81         }
82
83         bb_info_msg("System Maintenance Mode");
84
85         IF_SELINUX(renew_current_security_context());
86
87         shell = getenv("SUSHELL");
88         if (!shell)
89                 shell = getenv("sushell");
90         if (!shell)
91                 shell = pwd->pw_shell;
92
93         /* Exec login shell with no additional parameters. Never returns. */
94         run_shell(shell, 1, NULL, NULL);
95
96  auth_error:
97         bb_error_msg_and_die("no password entry for root");
98 }