1 /* vi: set sw=4 ts=4: */
3 * Mini sulogin implementation for busybox
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 #define SULOGIN_PROMPT "Give root password for system maintenance\n" \
14 "(or type Control-D for normal startup):"
16 static const char * const forbid[] = {
25 "LD_TRACE_LOADED_OBJECTS",
27 "LD_AOUT_LIBRARY_PATH",
36 static void catchalarm(int ATTRIBUTE_UNUSED junk)
42 int sulogin_main(int argc, char **argv)
47 const char * const *p;
51 if (ENABLE_FEATURE_SYSLOG) {
52 logmode = LOGMODE_BOTH;
53 openlog(bb_applet_name, LOG_CONS | LOG_NOWAIT, LOG_AUTH);
56 if (bb_getopt_ulflags (argc, argv, "t:", &timeout_arg)) {
57 if (safe_strtoi(timeout_arg, &timeout)) {
66 dup(xopen(argv[optind], O_RDWR));
70 if (!isatty(0) || !isatty(1) || !isatty(2)) {
71 bb_error_msg_and_die("Not a tty");
74 /* Clear out anything dangerous from the environment */
75 for (p = forbid; *p; p++)
78 signal(SIGALRM, catchalarm);
80 if (!(pwd = getpwuid(0))) {
84 if (ENABLE_FEATURE_SHADOWPASSWDS) {
85 if (!(spwd = getspnam(pwd->pw_name))) {
88 pwd->pw_passwd = spwd->sp_pwdp;
92 /* cp points to a static buffer that is zeroed every time */
93 cp = bb_askpass(timeout, SULOGIN_PROMPT);
95 bb_info_msg("Normal startup");
98 if (strcmp(pw_encrypt(cp, pwd->pw_passwd), pwd->pw_passwd) == 0) {
101 bb_do_delay(FAIL_DELAY);
102 bb_error_msg("Login incorrect");
104 memset(cp, 0, strlen(cp));
105 signal(SIGALRM, SIG_DFL);
107 bb_info_msg("System Maintenance Mode");
109 USE_SELINUX(renew_current_security_context());
111 run_shell(pwd->pw_shell, 1, 0, 0);
114 bb_error_msg_and_die("No password entry for `root'");