Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 2 | /* |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 3 | * Mini sulogin implementation for busybox |
| 4 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 6 | */ |
| 7 | |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 8 | #include <syslog.h> |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 9 | |
| 10 | #include "busybox.h" |
| 11 | |
"Vladimir N. Oleynik" | 6f347ef | 2005-10-15 10:23:55 +0000 | [diff] [blame] | 12 | static const char * const forbid[] = { |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 13 | "ENV", |
| 14 | "BASH_ENV", |
| 15 | "HOME", |
| 16 | "IFS", |
| 17 | "PATH", |
| 18 | "SHELL", |
| 19 | "LD_LIBRARY_PATH", |
| 20 | "LD_PRELOAD", |
| 21 | "LD_TRACE_LOADED_OBJECTS", |
| 22 | "LD_BIND_NOW", |
| 23 | "LD_AOUT_LIBRARY_PATH", |
| 24 | "LD_AOUT_PRELOAD", |
| 25 | "LD_NOWARN", |
| 26 | "LD_KEEPDIR", |
| 27 | (char *) 0 |
| 28 | }; |
| 29 | |
| 30 | |
Bernhard Reutner-Fischer | 3038557 | 2006-01-31 17:57:48 +0000 | [diff] [blame] | 31 | static void catchalarm(int ATTRIBUTE_UNUSED junk) |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 32 | { |
| 33 | exit(EXIT_FAILURE); |
| 34 | } |
| 35 | |
| 36 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 37 | int sulogin_main(int argc, char **argv) |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 38 | { |
| 39 | char *cp; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 40 | int timeout = 0; |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 41 | char *timeout_arg; |
"Vladimir N. Oleynik" | 6f347ef | 2005-10-15 10:23:55 +0000 | [diff] [blame] | 42 | const char * const *p; |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 43 | struct passwd *pwd; |
| 44 | struct spwd *spwd; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 9852d5a | 2006-09-09 14:00:58 +0000 | [diff] [blame] | 46 | logmode = LOGMODE_BOTH; |
| 47 | openlog(bb_applet_name, 0, LOG_AUTH); |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame^] | 49 | if (getopt32 (argc, argv, "t:", &timeout_arg)) { |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 50 | if (safe_strtoi(timeout_arg, &timeout)) { |
| 51 | timeout = 0; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 54 | |
| 55 | if (argv[optind]) { |
| 56 | close(0); |
| 57 | close(1); |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 58 | dup(xopen(argv[optind], O_RDWR)); |
Rob Landley | 6967494 | 2006-09-11 00:34:01 +0000 | [diff] [blame] | 59 | close(2); |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 60 | dup(0); |
| 61 | } |
| 62 | |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 63 | if (!isatty(0) || !isatty(1) || !isatty(2)) { |
Denis Vlasenko | 9852d5a | 2006-09-09 14:00:58 +0000 | [diff] [blame] | 64 | logmode = LOGMODE_SYSLOG; |
| 65 | bb_error_msg_and_die("not a tty"); |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 66 | } |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 67 | |
| 68 | /* Clear out anything dangerous from the environment */ |
| 69 | for (p = forbid; *p; p++) |
| 70 | unsetenv(*p); |
| 71 | |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 72 | signal(SIGALRM, catchalarm); |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 73 | |
| 74 | if (!(pwd = getpwuid(0))) { |
Denis Vlasenko | 9852d5a | 2006-09-09 14:00:58 +0000 | [diff] [blame] | 75 | goto auth_error; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 76 | } |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 77 | |
| 78 | if (ENABLE_FEATURE_SHADOWPASSWDS) { |
| 79 | if (!(spwd = getspnam(pwd->pw_name))) { |
Denis Vlasenko | 9852d5a | 2006-09-09 14:00:58 +0000 | [diff] [blame] | 80 | goto auth_error; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 81 | } |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 82 | pwd->pw_passwd = spwd->sp_pwdp; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 83 | } |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 84 | |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 85 | while (1) { |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 86 | /* cp points to a static buffer that is zeroed every time */ |
Denis Vlasenko | 9852d5a | 2006-09-09 14:00:58 +0000 | [diff] [blame] | 87 | cp = bb_askpass(timeout, |
| 88 | "Give root password for system maintenance\n" |
| 89 | "(or type Control-D for normal startup):"); |
| 90 | |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 91 | if (!cp || !*cp) { |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 92 | bb_info_msg("Normal startup"); |
Denis Vlasenko | 9852d5a | 2006-09-09 14:00:58 +0000 | [diff] [blame] | 93 | return 0; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 94 | } |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 95 | if (strcmp(pw_encrypt(cp, pwd->pw_passwd), pwd->pw_passwd) == 0) { |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 96 | break; |
| 97 | } |
Rob Landley | 84cb767 | 2006-01-06 20:59:09 +0000 | [diff] [blame] | 98 | bb_do_delay(FAIL_DELAY); |
Denis Vlasenko | 9852d5a | 2006-09-09 14:00:58 +0000 | [diff] [blame] | 99 | bb_error_msg("login incorrect"); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 100 | } |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 101 | memset(cp, 0, strlen(cp)); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 102 | signal(SIGALRM, SIG_DFL); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 103 | |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 104 | bb_info_msg("System Maintenance Mode"); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 105 | |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 106 | USE_SELINUX(renew_current_security_context()); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 107 | |
Rob Landley | dd93abe | 2006-09-08 17:22:05 +0000 | [diff] [blame] | 108 | run_shell(pwd->pw_shell, 1, 0, 0); |
| 109 | /* never returns */ |
Denis Vlasenko | 9852d5a | 2006-09-09 14:00:58 +0000 | [diff] [blame] | 110 | |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 111 | auth_error: |
Denis Vlasenko | 9852d5a | 2006-09-09 14:00:58 +0000 | [diff] [blame] | 112 | bb_error_msg_and_die("no password entry for `root'"); |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 113 | } |