1 /* vi: set sw=4 ts=4: */
10 #include <sys/resource.h>
13 #include <sys/types.h>
20 #define SULOGIN_PROMPT "\nGive root password for system maintenance\n" \
21 "(or type Control-D for normal startup):"
23 static const char * const forbid[] = {
32 "LD_TRACE_LOADED_OBJECTS",
34 "LD_AOUT_LIBRARY_PATH",
43 static void catchalarm(int ATTRIBUTE_UNUSED junk)
49 int sulogin_main(int argc, char **argv)
52 char *device = (char *) 0;
53 const char *name = "root";
56 #define pass bb_common_bufsiz1
60 const char * const *p;
61 #if ENABLE_FEATURE_SHADOWPASSWDS
62 struct spwd *spwd = NULL;
65 openlog("sulogin", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
67 if (strncmp(argv[1], "-t", 2) == 0) {
68 if (strcmp(argv[1], "-t") == 0) {
70 timeout = atoi(argv[2]);
87 if (open(device, O_RDWR) >= 0) {
91 syslog(LOG_WARNING, "cannot open %s\n", device);
96 if (access(bb_path_passwd_file, 0) == -1) {
97 syslog(LOG_WARNING, "No password file\n");
98 bb_error_msg_and_die("No password file\n");
100 if (!isatty(0) || !isatty(1) || !isatty(2)) {
105 /* Clear out anything dangerous from the environment */
106 for (p = forbid; *p; p++)
110 signal(SIGALRM, catchalarm);
111 if (!(pwd = getpwnam(name))) {
112 syslog(LOG_WARNING, "No password entry for `root'\n");
113 bb_error_msg_and_die("No password entry for `root'\n");
116 #if ENABLE_FEATURE_SHADOWPASSWDS
118 if (pwd && ((strcmp(pwd->pw_passwd, "x") == 0)
119 || (strcmp(pwd->pw_passwd, "*") == 0))) {
121 spwd = getspnam(name);
123 pwent.pw_passwd = spwd->sp_pwdp;
128 cp = bb_askpass(timeout, SULOGIN_PROMPT);
132 syslog(LOG_INFO, "Normal startup\n");
135 safe_strncpy(pass, cp, sizeof(pass));
136 memset(cp, 0, strlen(cp));
138 if (strcmp(pw_encrypt(pass, pwent.pw_passwd), pwent.pw_passwd) == 0) {
141 bb_do_delay(FAIL_DELAY);
142 puts("Login incorrect");
144 syslog(LOG_WARNING, "Incorrect root password\n");
146 memset(pass, 0, strlen(pass));
147 signal(SIGALRM, SIG_DFL);
148 puts("Entering System Maintenance Mode\n");
150 syslog(LOG_INFO, "System Maintenance Mode\n");
153 renew_current_security_context();
156 run_shell(pwent.pw_shell, 1, 0, 0);