1 /* vi: set sw=4 ts=4: */
11 #include <sys/resource.h>
14 #include <sys/types.h>
22 #define SULOGIN_PROMPT "\nGive root password for system maintenance\n" \
23 "(or type Control-D for normal startup):"
25 static const char *forbid[] = {
34 "LD_TRACE_LOADED_OBJECTS",
36 "LD_AOUT_LIBRARY_PATH",
45 static void catchalarm(int junk)
51 extern int sulogin_main(int argc, char **argv)
54 char *device = (char *) 0;
55 const char *name = "root";
57 static char pass[BUFSIZ];
58 struct termios termio;
63 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
64 struct spwd *spwd = NULL;
65 #endif /* CONFIG_FEATURE_SHADOWPASSWDS */
67 tcgetattr(0, &termio);
68 /* set control chars */
69 termio.c_cc[VINTR] = 3; /* C-c */
70 termio.c_cc[VQUIT] = 28; /* C-\ */
71 termio.c_cc[VERASE] = 127; /* C-? */
72 termio.c_cc[VKILL] = 21; /* C-u */
73 termio.c_cc[VEOF] = 4; /* C-d */
74 termio.c_cc[VSTART] = 17; /* C-q */
75 termio.c_cc[VSTOP] = 19; /* C-s */
76 termio.c_cc[VSUSP] = 26; /* C-z */
77 /* use line dicipline 0 */
80 termio.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
81 termio.c_cflag |= CREAD|HUPCL|CLOCAL;
83 termio.c_iflag = ICRNL | IXON | IXOFF;
85 termio.c_oflag = OPOST | ONLCR;
87 termio.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
88 tcsetattr(0, TCSANOW, &termio);
89 openlog("sulogin", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
91 if (strncmp(argv[1], "-t", 2) == 0) {
92 if (strcmp(argv[1], "-t") == 0) {
94 timeout = atoi(argv[2]);
111 if (open(device, O_RDWR) >= 0) {
115 syslog(LOG_WARNING, "cannot open %s\n", device);
120 if (access(bb_path_passwd_file, 0) == -1) {
121 syslog(LOG_WARNING, "No password file\n");
122 bb_error_msg_and_die("No password file\n");
124 if (!isatty(0) || !isatty(1) || !isatty(2)) {
129 /* Clear out anything dangerous from the environment */
130 for (p = forbid; *p; p++)
134 signal(SIGALRM, catchalarm);
136 if (!(pwd = getpwnam(name))) {
137 syslog(LOG_WARNING, "No password entry for `root'\n");
138 bb_error_msg_and_die("No password entry for `root'\n");
141 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
143 if (pwd && ((strcmp(pwd->pw_passwd, "x") == 0)
144 || (strcmp(pwd->pw_passwd, "*") == 0))) {
146 spwd = getspnam(name);
148 pwent.pw_passwd = spwd->sp_pwdp;
151 #endif /* CONFIG_FEATURE_SHADOWPASSWDS */
153 cp = getpass(SULOGIN_PROMPT);
157 syslog(LOG_INFO, "Normal startup\n");
160 safe_strncpy(pass, cp, sizeof(pass));
161 bzero(cp, strlen(cp));
163 if (strcmp(pw_encrypt(pass, pwent.pw_passwd), pwent.pw_passwd) == 0) {
168 while (difftime(now, start) < FAIL_DELAY) {
172 puts("Login incorrect");
174 syslog(LOG_WARNING, "Incorrect root password\n");
176 bzero(pass, strlen(pass));
178 signal(SIGALRM, SIG_DFL);
179 puts("Entering System Maintenance Mode\n");
181 syslog(LOG_INFO, "System Maintenance Mode\n");
182 run_shell(pwent.pw_shell, 1, 0, 0);