- move bbconfig into alphabetical order
[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 tarball for details.
6  */
7
8 #include <syslog.h>
9
10 #include "busybox.h"
11
12 static const char * const forbid[] = {
13         "ENV",
14         "BASH_ENV",
15         "HOME",
16         "IFS",
17         "PATH",
18         "SHELL",
19         "LD_LIBRARY_PATH",
20         "LD_PRELOAD",
21         "LD_TRACE_LOADED_OBJECTS",
22         "LD_BIND_NOW",
23         "LD_AOUT_LIBRARY_PATH",
24         "LD_AOUT_PRELOAD",
25         "LD_NOWARN",
26         "LD_KEEPDIR",
27         (char *) 0
28 };
29
30
31 static void catchalarm(int ATTRIBUTE_UNUSED junk)
32 {
33         exit(EXIT_FAILURE);
34 }
35
36
37 int sulogin_main(int argc, char **argv);
38 int sulogin_main(int argc, char **argv)
39 {
40         char *cp;
41         int timeout = 0;
42         char *timeout_arg;
43         const char * const *p;
44         struct passwd *pwd;
45         const char *shell;
46
47         logmode = LOGMODE_BOTH;
48         openlog(applet_name, 0, LOG_AUTH);
49
50         if (getopt32(argc, argv, "t:", &timeout_arg)) {
51                 timeout = xatoi_u(timeout_arg);
52         }
53
54         if (argv[optind]) {
55                 close(0);
56                 close(1);
57                 dup(xopen(argv[optind], O_RDWR));
58                 close(2);
59                 dup(0);
60         }
61
62         if (!isatty(0) || !isatty(1) || !isatty(2)) {
63                 logmode = LOGMODE_SYSLOG;
64                 bb_error_msg_and_die("not a tty");
65         }
66
67         /* Clear out anything dangerous from the environment */
68         for (p = forbid; *p; p++)
69                 unsetenv(*p);
70
71         signal(SIGALRM, catchalarm);
72
73         pwd = getpwuid(0);
74         if (!pwd) {
75                 goto auth_error;
76         }
77
78 #if ENABLE_FEATURE_SHADOWPASSWDS
79         {
80                 struct spwd *spwd = getspnam(pwd->pw_name);
81                 if (!spwd) {
82                         goto auth_error;
83                 }
84                 pwd->pw_passwd = spwd->sp_pwdp;
85         }
86 #endif
87
88         while (1) {
89                 /* cp points to a static buffer that is zeroed every time */
90                 cp = bb_askpass(timeout,
91                                 "Give root password for system maintenance\n"
92                                 "(or type Control-D for normal startup):");
93
94                 if (!cp || !*cp) {
95                         bb_info_msg("Normal startup");
96                         return 0;
97                 }
98                 if (strcmp(pw_encrypt(cp, pwd->pw_passwd), pwd->pw_passwd) == 0) {
99                         break;
100                 }
101                 bb_do_delay(FAIL_DELAY);
102                 bb_error_msg("login incorrect");
103         }
104         memset(cp, 0, strlen(cp));
105         signal(SIGALRM, SIG_DFL);
106
107         bb_info_msg("System Maintenance Mode");
108
109         USE_SELINUX(renew_current_security_context());
110
111         shell = getenv("SUSHELL");
112         if (!shell) shell = getenv("sushell");
113         if (!shell) {
114                 shell = "/bin/sh";
115                 if (pwd->pw_shell[0])
116                         shell = pwd->pw_shell;
117         }
118         run_shell(shell, 1, 0, 0);
119         /* never returns */
120
121 auth_error:
122         bb_error_msg_and_die("no password entry for 'root'");
123 }