loginutils/*: convert to new-style "one file" applets
[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:/* Needs to be run by root or be suid root - needs to change uid and gid: */
16 //applet:IF_SULOGIN(APPLET(sulogin, BB_DIR_SBIN, BB_SUID_DROP))
17
18 //kbuild:lib-$(CONFIG_SULOGIN) += sulogin.o
19
20 //usage:#define sulogin_trivial_usage
21 //usage:       "[-t N] [TTY]"
22 //usage:#define sulogin_full_usage "\n\n"
23 //usage:       "Single user login\n"
24 //usage:     "\n        -t N    Timeout"
25
26 #include "libbb.h"
27 #include <syslog.h>
28
29 int sulogin_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
30 int sulogin_main(int argc UNUSED_PARAM, char **argv)
31 {
32         int timeout = 0;
33         struct passwd *pwd;
34         const char *shell;
35
36         logmode = LOGMODE_BOTH;
37         openlog(applet_name, 0, LOG_AUTH);
38
39         opt_complementary = "t+"; /* -t N */
40         getopt32(argv, "t:", &timeout);
41         argv += optind;
42
43         if (argv[0]) {
44                 close(0);
45                 close(1);
46                 dup(xopen(argv[0], O_RDWR));
47                 close(2);
48                 dup(0);
49         }
50
51         /* Malicious use like "sulogin /dev/sda"? */
52         if (!isatty(0) || !isatty(1) || !isatty(2)) {
53                 logmode = LOGMODE_SYSLOG;
54                 bb_error_msg_and_die("not a tty");
55         }
56
57         /* Clear dangerous stuff, set PATH */
58         sanitize_env_if_suid();
59
60         pwd = getpwuid(0);
61         if (!pwd) {
62                 goto auth_error;
63         }
64
65         while (1) {
66                 int r;
67
68                 r = ask_and_check_password_extended(pwd, timeout,
69                         "Give root password for system maintenance\n"
70                         "(or type Control-D for normal startup):"
71                 );
72                 if (r < 0) {
73                         /* ^D, ^C, timeout, or read error */
74                         bb_info_msg("Normal startup");
75                         return 0;
76                 }
77                 if (r > 0) {
78                         break;
79                 }
80                 bb_do_delay(LOGIN_FAIL_DELAY);
81                 bb_info_msg("Login incorrect");
82         }
83
84         bb_info_msg("System Maintenance Mode");
85
86         IF_SELINUX(renew_current_security_context());
87
88         shell = getenv("SUSHELL");
89         if (!shell)
90                 shell = getenv("sushell");
91         if (!shell)
92                 shell = pwd->pw_shell;
93
94         /* Exec login shell with no additional parameters. Never returns. */
95         run_shell(shell, 1, NULL, NULL);
96
97  auth_error:
98         bb_error_msg_and_die("no password entry for root");
99 }