1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 #include <sys/resource.h>
16 #include <sys/types.h>
23 #define SULOGIN_PROMPT "\nGive root password for system maintenance\n" \
24 "(or type Control-D for normal startup):"
26 static const char * const forbid[] = {
35 "LD_TRACE_LOADED_OBJECTS",
37 "LD_AOUT_LIBRARY_PATH",
46 static void catchalarm(int ATTRIBUTE_UNUSED junk)
52 int sulogin_main(int argc, char **argv)
55 char *device = (char *) 0;
56 const char *name = "root";
59 #define pass bb_common_bufsiz1
63 const char * const *p;
64 #if ENABLE_FEATURE_SHADOWPASSWDS
65 struct spwd *spwd = NULL;
68 openlog("sulogin", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
70 if (strncmp(argv[1], "-t", 2) == 0) {
71 if (strcmp(argv[1], "-t") == 0) {
73 timeout = atoi(argv[2]);
90 if (open(device, O_RDWR) >= 0) {
94 syslog(LOG_WARNING, "cannot open %s\n", device);
99 if (access(bb_path_passwd_file, 0) == -1) {
100 syslog(LOG_WARNING, "No password file\n");
101 bb_error_msg_and_die("No password file\n");
103 if (!isatty(0) || !isatty(1) || !isatty(2)) {
108 /* Clear out anything dangerous from the environment */
109 for (p = forbid; *p; p++)
113 signal(SIGALRM, catchalarm);
114 if (!(pwd = getpwnam(name))) {
115 syslog(LOG_WARNING, "No password entry for `root'\n");
116 bb_error_msg_and_die("No password entry for `root'\n");
119 #if ENABLE_FEATURE_SHADOWPASSWDS
121 if (pwd && ((strcmp(pwd->pw_passwd, "x") == 0)
122 || (strcmp(pwd->pw_passwd, "*") == 0))) {
124 spwd = getspnam(name);
126 pwent.pw_passwd = spwd->sp_pwdp;
131 cp = bb_askpass(timeout, SULOGIN_PROMPT);
135 syslog(LOG_INFO, "Normal startup\n");
138 safe_strncpy(pass, cp, sizeof(pass));
139 memset(cp, 0, strlen(cp));
141 if (strcmp(pw_encrypt(pass, pwent.pw_passwd), pwent.pw_passwd) == 0) {
144 bb_do_delay(FAIL_DELAY);
145 puts("Login incorrect");
147 syslog(LOG_WARNING, "Incorrect root password\n");
149 memset(pass, 0, strlen(pass));
150 signal(SIGALRM, SIG_DFL);
151 puts("Entering System Maintenance Mode\n");
153 syslog(LOG_INFO, "System Maintenance Mode\n");
156 renew_current_security_context();
159 run_shell(pwent.pw_shell, 1, 0, 0);