diff options
| author | Morten Kvistgaard <MK@pch-engineering.dk> | 2014-08-05 21:57:18 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2014-08-05 21:57:18 +0200 |
| commit | feac9b607dc68ea63992a46b3b8361f00f663cdc (patch) | |
| tree | ed5bca82370298e195f5b5e3d84d40f9e7470a32 | |
| parent | 09a0e2223f68a266749043bf33c84faeb5cee8a0 (diff) | |
| download | busybox-w32-feac9b607dc68ea63992a46b3b8361f00f663cdc.tar.gz busybox-w32-feac9b607dc68ea63992a46b3b8361f00f663cdc.tar.bz2 busybox-w32-feac9b607dc68ea63992a46b3b8361f00f663cdc.zip | |
ftpd: add optional support for authentication
function old new delta
cmdio_get_cmd_and_arg - 237 +237
get_passwd - 97 +97
check_password - 82 +82
ftpd_main 2297 2178 -119
ask_and_check_password_extended 206 84 -122
------------------------------------------------------------------------------
(add/remove: 3/0 grow/shrink: 0/2 up/down: 416/-241) Total: 175 bytes
Signed-off-by: Morten Kvistgaard <MK@pch-engineering.dk>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | include/libbb.h | 1 | ||||
| -rw-r--r-- | libbb/Kbuild.src | 1 | ||||
| -rw-r--r-- | libbb/correct_password.c | 96 | ||||
| -rw-r--r-- | networking/Config.src | 7 | ||||
| -rw-r--r-- | networking/ftpd.c | 47 |
5 files changed, 100 insertions, 52 deletions
diff --git a/include/libbb.h b/include/libbb.h index 858084bc5..ff223dd0f 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -1316,6 +1316,7 @@ int sd_listen_fds(void); | |||
| 1316 | #define SETUP_ENV_NO_CHDIR (1 << 4) | 1316 | #define SETUP_ENV_NO_CHDIR (1 << 4) |
| 1317 | void setup_environment(const char *shell, int flags, const struct passwd *pw) FAST_FUNC; | 1317 | void setup_environment(const char *shell, int flags, const struct passwd *pw) FAST_FUNC; |
| 1318 | void nuke_str(char *str) FAST_FUNC; | 1318 | void nuke_str(char *str) FAST_FUNC; |
| 1319 | int check_password(const struct passwd *pw, const char *plaintext) FAST_FUNC; | ||
| 1319 | int ask_and_check_password_extended(const struct passwd *pw, int timeout, const char *prompt) FAST_FUNC; | 1320 | int ask_and_check_password_extended(const struct passwd *pw, int timeout, const char *prompt) FAST_FUNC; |
| 1320 | int ask_and_check_password(const struct passwd *pw) FAST_FUNC; | 1321 | int ask_and_check_password(const struct passwd *pw) FAST_FUNC; |
| 1321 | /* Returns a malloced string */ | 1322 | /* Returns a malloced string */ |
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src index 62680bd52..0a9e803d7 100644 --- a/libbb/Kbuild.src +++ b/libbb/Kbuild.src | |||
| @@ -150,6 +150,7 @@ lib-$(CONFIG_VLOCK) += pw_encrypt.o correct_password.o | |||
| 150 | lib-$(CONFIG_SU) += pw_encrypt.o correct_password.o | 150 | lib-$(CONFIG_SU) += pw_encrypt.o correct_password.o |
| 151 | lib-$(CONFIG_LOGIN) += pw_encrypt.o correct_password.o | 151 | lib-$(CONFIG_LOGIN) += pw_encrypt.o correct_password.o |
| 152 | lib-$(CONFIG_FEATURE_HTTPD_AUTH_MD5) += pw_encrypt.o | 152 | lib-$(CONFIG_FEATURE_HTTPD_AUTH_MD5) += pw_encrypt.o |
| 153 | lib-$(CONFIG_FEATURE_FTP_AUTHENTICATION) += pw_encrypt.o | ||
| 153 | 154 | ||
| 154 | lib-$(CONFIG_DF) += find_mount_point.o | 155 | lib-$(CONFIG_DF) += find_mount_point.o |
| 155 | lib-$(CONFIG_MKFS_MINIX) += find_mount_point.o | 156 | lib-$(CONFIG_MKFS_MINIX) += find_mount_point.o |
diff --git a/libbb/correct_password.c b/libbb/correct_password.c index acadf3914..513c93028 100644 --- a/libbb/correct_password.c +++ b/libbb/correct_password.c | |||
| @@ -30,6 +30,63 @@ | |||
| 30 | 30 | ||
| 31 | #include "libbb.h" | 31 | #include "libbb.h" |
| 32 | 32 | ||
| 33 | #define SHADOW_BUFSIZE 256 | ||
| 34 | |||
| 35 | /* Retrieve encrypted password string for pw. | ||
| 36 | * If pw == NULL, return a string which fails password check against any | ||
| 37 | * password. | ||
| 38 | */ | ||
| 39 | #if !ENABLE_FEATURE_SHADOWPASSWDS | ||
| 40 | #define get_passwd(pw, buffer) get_passwd(pw) | ||
| 41 | #endif | ||
| 42 | static const char *get_passwd(const struct passwd *pw, char buffer[SHADOW_BUFSIZE]) | ||
| 43 | { | ||
| 44 | const char *pass; | ||
| 45 | |||
| 46 | if (!pw) | ||
| 47 | return "aa"; /* "aa" will never match */ | ||
| 48 | |||
| 49 | pass = pw->pw_passwd; | ||
| 50 | #if ENABLE_FEATURE_SHADOWPASSWDS | ||
| 51 | /* Using _r function to avoid pulling in static buffers */ | ||
| 52 | if ((pass[0] == 'x' || pass[0] == '*') && !pass[1]) { | ||
| 53 | struct spwd spw; | ||
| 54 | int r; | ||
| 55 | /* getspnam_r may return 0 yet set result to NULL. | ||
| 56 | * At least glibc 2.4 does this. Be extra paranoid here. */ | ||
| 57 | struct spwd *result = NULL; | ||
| 58 | r = getspnam_r(pw->pw_name, &spw, buffer, SHADOW_BUFSIZE, &result); | ||
| 59 | pass = (r || !result) ? "aa" : result->sp_pwdp; | ||
| 60 | } | ||
| 61 | #endif | ||
| 62 | return pass; | ||
| 63 | } | ||
| 64 | |||
| 65 | /* | ||
| 66 | * Return 1 if PW has an empty password. | ||
| 67 | * Return 1 if the user gives the correct password for entry PW, | ||
| 68 | * 0 if not. | ||
| 69 | * NULL pw means "just fake it for login with bad username" | ||
| 70 | */ | ||
| 71 | int FAST_FUNC check_password(const struct passwd *pw, const char *plaintext) | ||
| 72 | { | ||
| 73 | IF_FEATURE_SHADOWPASSWDS(char buffer[SHADOW_BUFSIZE];) | ||
| 74 | char *encrypted; | ||
| 75 | const char *pw_pass; | ||
| 76 | int r; | ||
| 77 | |||
| 78 | pw_pass = get_passwd(pw, buffer); | ||
| 79 | if (!pw_pass[0]) { /* empty password field? */ | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | encrypted = pw_encrypt(plaintext, /*salt:*/ pw_pass, 1); | ||
| 84 | r = (strcmp(encrypted, pw_pass) == 0); | ||
| 85 | free(encrypted); | ||
| 86 | return r; | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 33 | /* Ask the user for a password. | 90 | /* Ask the user for a password. |
| 34 | * Return 1 without asking if PW has an empty password. | 91 | * Return 1 without asking if PW has an empty password. |
| 35 | * Return -1 on EOF, error while reading input, or timeout. | 92 | * Return -1 on EOF, error while reading input, or timeout. |
| @@ -41,42 +98,23 @@ | |||
| 41 | int FAST_FUNC ask_and_check_password_extended(const struct passwd *pw, | 98 | int FAST_FUNC ask_and_check_password_extended(const struct passwd *pw, |
| 42 | int timeout, const char *prompt) | 99 | int timeout, const char *prompt) |
| 43 | { | 100 | { |
| 44 | char *unencrypted, *encrypted; | 101 | IF_FEATURE_SHADOWPASSWDS(char buffer[SHADOW_BUFSIZE];) |
| 45 | const char *correct; | 102 | char *plaintext; |
| 103 | const char *pw_pass; | ||
| 46 | int r; | 104 | int r; |
| 47 | /* fake salt. crypt() can choke otherwise. */ | ||
| 48 | correct = "aa"; | ||
| 49 | if (!pw) { | ||
| 50 | /* "aa" will never match */ | ||
| 51 | goto fake_it; | ||
| 52 | } | ||
| 53 | correct = pw->pw_passwd; | ||
| 54 | #if ENABLE_FEATURE_SHADOWPASSWDS | ||
| 55 | /* Using _r function to avoid pulling in static buffers */ | ||
| 56 | if ((correct[0] == 'x' || correct[0] == '*') && !correct[1]) { | ||
| 57 | struct spwd spw; | ||
| 58 | char buffer[256]; | ||
| 59 | /* getspnam_r may return 0 yet set result to NULL. | ||
| 60 | * At least glibc 2.4 does this. Be extra paranoid here. */ | ||
| 61 | struct spwd *result = NULL; | ||
| 62 | r = getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result); | ||
| 63 | correct = (r || !result) ? "aa" : result->sp_pwdp; | ||
| 64 | } | ||
| 65 | #endif | ||
| 66 | 105 | ||
| 67 | if (!correct[0]) /* empty password field? */ | 106 | pw_pass = get_passwd(pw, buffer); |
| 107 | if (!pw_pass[0]) /* empty password field? */ | ||
| 68 | return 1; | 108 | return 1; |
| 69 | 109 | ||
| 70 | fake_it: | 110 | plaintext = bb_ask(STDIN_FILENO, timeout, prompt); |
| 71 | unencrypted = bb_ask(STDIN_FILENO, timeout, prompt); | 111 | if (!plaintext) { |
| 72 | if (!unencrypted) { | ||
| 73 | /* EOF (such as ^D) or error (such as ^C) or timeout */ | 112 | /* EOF (such as ^D) or error (such as ^C) or timeout */ |
| 74 | return -1; | 113 | return -1; |
| 75 | } | 114 | } |
| 76 | encrypted = pw_encrypt(unencrypted, correct, 1); | 115 | |
| 77 | r = (strcmp(encrypted, correct) == 0); | 116 | r = check_password(pw, plaintext); |
| 78 | free(encrypted); | 117 | nuke_str(plaintext); |
| 79 | nuke_str(unencrypted); | ||
| 80 | return r; | 118 | return r; |
| 81 | } | 119 | } |
| 82 | 120 | ||
diff --git a/networking/Config.src b/networking/Config.src index fbad7ecb2..e56646917 100644 --- a/networking/Config.src +++ b/networking/Config.src | |||
| @@ -134,6 +134,13 @@ config FEATURE_FTPD_ACCEPT_BROKEN_LIST | |||
| 134 | it increases the code size by ~40 bytes. | 134 | it increases the code size by ~40 bytes. |
| 135 | Most other ftp servers seem to behave similar to this. | 135 | Most other ftp servers seem to behave similar to this. |
| 136 | 136 | ||
| 137 | config FEATURE_FTP_AUTHENTICATION | ||
| 138 | bool "Enable authentication" | ||
| 139 | default y | ||
| 140 | depends on FTPD | ||
| 141 | help | ||
| 142 | Enable basic system login as seen in telnet etc. | ||
| 143 | |||
| 137 | config FTPGET | 144 | config FTPGET |
| 138 | bool "ftpget" | 145 | bool "ftpget" |
| 139 | default y | 146 | default y |
diff --git a/networking/ftpd.c b/networking/ftpd.c index 2d2a3a44c..9fcc3e963 100644 --- a/networking/ftpd.c +++ b/networking/ftpd.c | |||
| @@ -1172,18 +1172,6 @@ int ftpd_main(int argc UNUSED_PARAM, char **argv) | |||
| 1172 | if (logmode) | 1172 | if (logmode) |
| 1173 | applet_name = xasprintf("%s[%u]", applet_name, (int)getpid()); | 1173 | applet_name = xasprintf("%s[%u]", applet_name, (int)getpid()); |
| 1174 | 1174 | ||
| 1175 | #if !BB_MMU | ||
| 1176 | G.root_fd = -1; | ||
| 1177 | #endif | ||
| 1178 | argv += optind; | ||
| 1179 | if (argv[0]) { | ||
| 1180 | #if !BB_MMU | ||
| 1181 | G.root_fd = xopen("/", O_RDONLY | O_DIRECTORY); | ||
| 1182 | close_on_exec_on(G.root_fd); | ||
| 1183 | #endif | ||
| 1184 | xchroot(argv[0]); | ||
| 1185 | } | ||
| 1186 | |||
| 1187 | //umask(077); - admin can set umask before starting us | 1175 | //umask(077); - admin can set umask before starting us |
| 1188 | 1176 | ||
| 1189 | /* Signals. We'll always take -EPIPE rather than a rude signal, thanks */ | 1177 | /* Signals. We'll always take -EPIPE rather than a rude signal, thanks */ |
| @@ -1199,23 +1187,22 @@ int ftpd_main(int argc UNUSED_PARAM, char **argv) | |||
| 1199 | WRITE_OK(FTP_GREET); | 1187 | WRITE_OK(FTP_GREET); |
| 1200 | signal(SIGALRM, timeout_handler); | 1188 | signal(SIGALRM, timeout_handler); |
| 1201 | 1189 | ||
| 1202 | #ifdef IF_WE_WANT_TO_REQUIRE_LOGIN | 1190 | #if ENABLE_FEATURE_FTP_AUTHENTICATION |
| 1203 | { | 1191 | { |
| 1204 | smallint user_was_specified = 0; | 1192 | struct passwd *pw = NULL; |
| 1193 | |||
| 1205 | while (1) { | 1194 | while (1) { |
| 1206 | uint32_t cmdval = cmdio_get_cmd_and_arg(); | 1195 | uint32_t cmdval = cmdio_get_cmd_and_arg(); |
| 1207 | 1196 | ||
| 1208 | if (cmdval == const_USER) { | 1197 | if (cmdval == const_USER) { |
| 1209 | if (G.ftp_arg == NULL || strcasecmp(G.ftp_arg, "anonymous") != 0) | 1198 | pw = getpwnam(G.ftp_arg); |
| 1210 | cmdio_write_raw(STR(FTP_LOGINERR)" Server is anonymous only\r\n"); | 1199 | cmdio_write_raw(STR(FTP_GIVEPWORD)" Please specify password\r\n"); |
| 1211 | else { | ||
| 1212 | user_was_specified = 1; | ||
| 1213 | cmdio_write_raw(STR(FTP_GIVEPWORD)" Please specify the password\r\n"); | ||
| 1214 | } | ||
| 1215 | } else if (cmdval == const_PASS) { | 1200 | } else if (cmdval == const_PASS) { |
| 1216 | if (user_was_specified) | 1201 | if (check_password(pw, G.ftp_arg) > 0) { |
| 1217 | break; | 1202 | break; /* login success */ |
| 1218 | cmdio_write_raw(STR(FTP_NEEDUSER)" Login with USER\r\n"); | 1203 | } |
| 1204 | cmdio_write_raw(STR(FTP_LOGINERR)" Login failed\r\n"); | ||
| 1205 | pw = NULL; | ||
| 1219 | } else if (cmdval == const_QUIT) { | 1206 | } else if (cmdval == const_QUIT) { |
| 1220 | WRITE_OK(FTP_GOODBYE); | 1207 | WRITE_OK(FTP_GOODBYE); |
| 1221 | return 0; | 1208 | return 0; |
| @@ -1223,10 +1210,24 @@ int ftpd_main(int argc UNUSED_PARAM, char **argv) | |||
| 1223 | cmdio_write_raw(STR(FTP_LOGINERR)" Login with USER and PASS\r\n"); | 1210 | cmdio_write_raw(STR(FTP_LOGINERR)" Login with USER and PASS\r\n"); |
| 1224 | } | 1211 | } |
| 1225 | } | 1212 | } |
| 1213 | change_identity(pw); | ||
| 1226 | } | 1214 | } |
| 1227 | WRITE_OK(FTP_LOGINOK); | 1215 | WRITE_OK(FTP_LOGINOK); |
| 1228 | #endif | 1216 | #endif |
| 1229 | 1217 | ||
| 1218 | /* Do this after auth, else /etc/passwd is not accessible */ | ||
| 1219 | #if !BB_MMU | ||
| 1220 | G.root_fd = -1; | ||
| 1221 | #endif | ||
| 1222 | argv += optind; | ||
| 1223 | if (argv[0]) { | ||
| 1224 | #if !BB_MMU | ||
| 1225 | G.root_fd = xopen("/", O_RDONLY | O_DIRECTORY); | ||
| 1226 | close_on_exec_on(G.root_fd); | ||
| 1227 | #endif | ||
| 1228 | xchroot(argv[0]); | ||
| 1229 | } | ||
| 1230 | |||
| 1230 | /* RFC-959 Section 5.1 | 1231 | /* RFC-959 Section 5.1 |
| 1231 | * The following commands and options MUST be supported by every | 1232 | * The following commands and options MUST be supported by every |
| 1232 | * server-FTP and user-FTP, except in cases where the underlying | 1233 | * server-FTP and user-FTP, except in cases where the underlying |
