1 /* vi: set sw=4 ts=4: */
10 #include <sys/resource.h>
12 #include <sys/types.h>
19 #define SULOGIN_PROMPT "\nGive root password for system maintenance\n" \
20 "(or type Control-D for normal startup):"
22 static const char * const forbid[] = {
31 "LD_TRACE_LOADED_OBJECTS",
33 "LD_AOUT_LIBRARY_PATH",
42 static void catchalarm(int ATTRIBUTE_UNUSED junk)
48 int sulogin_main(int argc, char **argv)
51 char *device = (char *) 0;
52 const char *name = "root";
55 #define pass bb_common_bufsiz1
59 const char * const *p;
60 #if ENABLE_FEATURE_SHADOWPASSWDS
61 struct spwd *spwd = NULL;
64 openlog("sulogin", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
66 if (strncmp(argv[1], "-t", 2) == 0) {
67 if (strcmp(argv[1], "-t") == 0) {
69 timeout = atoi(argv[2]);
86 if (open(device, O_RDWR) >= 0) {
90 syslog(LOG_WARNING, "cannot open %s\n", device);
95 if (access(bb_path_passwd_file, 0) == -1) {
96 syslog(LOG_WARNING, "No password file\n");
97 bb_error_msg_and_die("No password file\n");
99 if (!isatty(0) || !isatty(1) || !isatty(2)) {
104 /* Clear out anything dangerous from the environment */
105 for (p = forbid; *p; p++)
109 signal(SIGALRM, catchalarm);
110 if (!(pwd = getpwnam(name))) {
111 syslog(LOG_WARNING, "No password entry for `root'\n");
112 bb_error_msg_and_die("No password entry for `root'\n");
115 #if ENABLE_FEATURE_SHADOWPASSWDS
117 if (pwd && ((strcmp(pwd->pw_passwd, "x") == 0)
118 || (strcmp(pwd->pw_passwd, "*") == 0))) {
120 spwd = getspnam(name);
122 pwent.pw_passwd = spwd->sp_pwdp;
127 cp = bb_askpass(timeout, SULOGIN_PROMPT);
131 syslog(LOG_INFO, "Normal startup\n");
134 safe_strncpy(pass, cp, sizeof(pass));
135 memset(cp, 0, strlen(cp));
137 if (strcmp(pw_encrypt(pass, pwent.pw_passwd), pwent.pw_passwd) == 0) {
140 bb_do_delay(FAIL_DELAY);
141 puts("Login incorrect");
143 syslog(LOG_WARNING, "Incorrect root password\n");
145 memset(pass, 0, strlen(pass));
146 signal(SIGALRM, SIG_DFL);
147 puts("Entering System Maintenance Mode\n");
149 syslog(LOG_INFO, "System Maintenance Mode\n");
152 renew_current_security_context();
155 run_shell(pwent.pw_shell, 1, 0, 0);