diff options
author | Rob Landley <rob@landley.net> | 2006-09-08 17:22:05 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2006-09-08 17:22:05 +0000 |
commit | dd93abeaffa79efcf3028489b7638f2c723020e8 (patch) | |
tree | bde0da9796751ebd8c868faba142564dd555e044 | |
parent | ef08184d9e0c4217aa7aed1c604c68d2a66b90e5 (diff) | |
download | busybox-w32-dd93abeaffa79efcf3028489b7638f2c723020e8.tar.gz busybox-w32-dd93abeaffa79efcf3028489b7638f2c723020e8.tar.bz2 busybox-w32-dd93abeaffa79efcf3028489b7638f2c723020e8.zip |
Shrinkage/cleanup from Tito.
-rw-r--r-- | loginutils/sulogin.c | 132 |
1 files changed, 45 insertions, 87 deletions
diff --git a/loginutils/sulogin.c b/loginutils/sulogin.c index 763a9913a..15f3fb260 100644 --- a/loginutils/sulogin.c +++ b/loginutils/sulogin.c | |||
@@ -1,26 +1,16 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | 2 | /* |
3 | * Mini sulogin implementation for busybox | ||
4 | * | ||
3 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
4 | */ | 6 | */ |
5 | 7 | ||
6 | #include <fcntl.h> | ||
7 | #include <signal.h> | ||
8 | #include <stdio.h> | ||
9 | #include <stdlib.h> | ||
10 | #include <string.h> | ||
11 | #include <syslog.h> | 8 | #include <syslog.h> |
12 | #include <unistd.h> | ||
13 | #include <utmp.h> | ||
14 | #include <sys/resource.h> | ||
15 | #include <sys/stat.h> | ||
16 | #include <sys/types.h> | ||
17 | #include <ctype.h> | ||
18 | #include <time.h> | ||
19 | 9 | ||
20 | #include "busybox.h" | 10 | #include "busybox.h" |
21 | 11 | ||
22 | 12 | ||
23 | #define SULOGIN_PROMPT "\nGive root password for system maintenance\n" \ | 13 | #define SULOGIN_PROMPT "Give root password for system maintenance\n" \ |
24 | "(or type Control-D for normal startup):" | 14 | "(or type Control-D for normal startup):" |
25 | 15 | ||
26 | static const char * const forbid[] = { | 16 | static const char * const forbid[] = { |
@@ -52,57 +42,33 @@ static void catchalarm(int ATTRIBUTE_UNUSED junk) | |||
52 | int sulogin_main(int argc, char **argv) | 42 | int sulogin_main(int argc, char **argv) |
53 | { | 43 | { |
54 | char *cp; | 44 | char *cp; |
55 | char *device = NULL; | ||
56 | const char *name = "root"; | ||
57 | int timeout = 0; | 45 | int timeout = 0; |
46 | char *timeout_arg; | ||
47 | const char * const *p; | ||
48 | struct passwd *pwd; | ||
49 | struct spwd *spwd; | ||
58 | 50 | ||
59 | #define pass bb_common_bufsiz1 | 51 | if (ENABLE_FEATURE_SYSLOG) { |
52 | logmode = LOGMODE_BOTH; | ||
53 | openlog(bb_applet_name, LOG_CONS | LOG_NOWAIT, LOG_AUTH); | ||
54 | } | ||
60 | 55 | ||
61 | struct passwd pwent; | 56 | if (bb_getopt_ulflags (argc, argv, "t:", &timeout_arg)) { |
62 | struct passwd *pwd; | 57 | if (safe_strtoi(timeout_arg, &timeout)) { |
63 | const char * const *p; | 58 | timeout = 0; |
64 | #if ENABLE_FEATURE_SHADOWPASSWDS | ||
65 | struct spwd *spwd = NULL; | ||
66 | #endif | ||
67 | |||
68 | openlog("sulogin", LOG_PID | LOG_NOWAIT, LOG_AUTH); | ||
69 | logmode = LOGMODE_BOTH; | ||
70 | if (argc > 1) { | ||
71 | if (strncmp(argv[1], "-t", 2) == 0) { | ||
72 | if (argv[1][2] == '\0') { /* -t NN */ | ||
73 | if (argc > 2) { | ||
74 | timeout = atoi(argv[2]); | ||
75 | if (argc > 3) { | ||
76 | device = argv[3]; | ||
77 | } | ||
78 | } | ||
79 | } else { /* -tNNN */ | ||
80 | timeout = atoi(&argv[1][2]); | ||
81 | if (argc > 2) { | ||
82 | device = argv[2]; | ||
83 | } | ||
84 | } | ||
85 | } else { | ||
86 | device = argv[1]; | ||
87 | } | ||
88 | if (device) { | ||
89 | close(0); | ||
90 | close(1); | ||
91 | close(2); | ||
92 | if (open(device, O_RDWR) == 0) { | ||
93 | dup(0); | ||
94 | dup(0); | ||
95 | } else { | ||
96 | /* Well, it will go only to syslog :) */ | ||
97 | bb_perror_msg_and_die("Cannot open %s", device); | ||
98 | } | ||
99 | } | 59 | } |
100 | } | 60 | } |
101 | if (!isatty(0) || !isatty(1) || !isatty(2)) { | 61 | |
102 | exit(EXIT_FAILURE); | 62 | if (argv[optind]) { |
63 | close(0); | ||
64 | close(1); | ||
65 | close(2); | ||
66 | dup(xopen(argv[optind], O_RDWR)); | ||
67 | dup(0); | ||
103 | } | 68 | } |
104 | if (access(bb_path_passwd_file, 0) == -1) { | 69 | |
105 | bb_error_msg_and_die("No password file"); | 70 | if (!isatty(0) || !isatty(1) || !isatty(2)) { |
71 | bb_error_msg_and_die("Not a tty"); | ||
106 | } | 72 | } |
107 | 73 | ||
108 | /* Clear out anything dangerous from the environment */ | 74 | /* Clear out anything dangerous from the environment */ |
@@ -110,48 +76,40 @@ int sulogin_main(int argc, char **argv) | |||
110 | unsetenv(*p); | 76 | unsetenv(*p); |
111 | 77 | ||
112 | signal(SIGALRM, catchalarm); | 78 | signal(SIGALRM, catchalarm); |
113 | if (!(pwd = getpwnam(name))) { | 79 | |
114 | bb_error_msg_and_die("No password entry for `root'"); | 80 | if (!(pwd = getpwuid(0))) { |
115 | } | 81 | goto AUTH_ERROR; |
116 | pwent = *pwd; | 82 | } |
117 | #if ENABLE_FEATURE_SHADOWPASSWDS | 83 | |
118 | spwd = NULL; | 84 | if (ENABLE_FEATURE_SHADOWPASSWDS) { |
119 | if (pwd && ((strcmp(pwd->pw_passwd, "x") == 0) | 85 | if (!(spwd = getspnam(pwd->pw_name))) { |
120 | || (strcmp(pwd->pw_passwd, "*") == 0))) { | 86 | goto AUTH_ERROR; |
121 | endspent(); | ||
122 | spwd = getspnam(name); | ||
123 | if (spwd) { | ||
124 | pwent.pw_passwd = spwd->sp_pwdp; | ||
125 | } | 87 | } |
88 | pwd->pw_passwd = spwd->sp_pwdp; | ||
126 | } | 89 | } |
127 | #endif | 90 | |
128 | while (1) { | 91 | while (1) { |
92 | /* cp points to a static buffer that is zeroed every time */ | ||
129 | cp = bb_askpass(timeout, SULOGIN_PROMPT); | 93 | cp = bb_askpass(timeout, SULOGIN_PROMPT); |
130 | if (!cp || !*cp) { | 94 | if (!cp || !*cp) { |
131 | puts("\n"); /* Why only on error path? */ | 95 | bb_info_msg("Normal startup"); |
132 | fflush(stdout); | ||
133 | /* Why only to syslog? */ | ||
134 | syslog(LOG_INFO, "Normal startup"); | ||
135 | exit(EXIT_SUCCESS); | 96 | exit(EXIT_SUCCESS); |
136 | } else { | ||
137 | safe_strncpy(pass, cp, sizeof(pass)); | ||
138 | memset(cp, 0, strlen(cp)); | ||
139 | } | 97 | } |
140 | if (strcmp(pw_encrypt(pass, pwent.pw_passwd), pwent.pw_passwd) == 0) { | 98 | if (strcmp(pw_encrypt(cp, pwd->pw_passwd), pwd->pw_passwd) == 0) { |
141 | break; | 99 | break; |
142 | } | 100 | } |
143 | bb_do_delay(FAIL_DELAY); | 101 | bb_do_delay(FAIL_DELAY); |
144 | bb_error_msg("Incorrect root password"); | 102 | bb_error_msg("Login incorrect"); |
145 | } | 103 | } |
146 | memset(pass, 0, strlen(pass)); | 104 | memset(cp, 0, strlen(cp)); |
147 | signal(SIGALRM, SIG_DFL); | 105 | signal(SIGALRM, SIG_DFL); |
148 | bb_info_msg("Entering System Maintenance Mode"); | ||
149 | 106 | ||
150 | #if ENABLE_SELINUX | 107 | bb_info_msg("System Maintenance Mode"); |
151 | renew_current_security_context(); | ||
152 | #endif | ||
153 | 108 | ||
154 | run_shell(pwent.pw_shell, 1, 0, 0); | 109 | USE_SELINUX(renew_current_security_context()); |
155 | 110 | ||
156 | return 0; | 111 | run_shell(pwd->pw_shell, 1, 0, 0); |
112 | /* never returns */ | ||
113 | AUTH_ERROR: | ||
114 | bb_error_msg_and_die("No password entry for `root'"); | ||
157 | } | 115 | } |