Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Simple FTP daemon, based on vsftpd 2.0.7 (written by Chris Evans) |
| 4 | * |
| 5 | * Author: Adam Tkac <vonsch@gmail.com> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 8 | * |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 9 | * Only subset of FTP protocol is implemented but vast majority of clients |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 10 | * should not have any problem. |
| 11 | * |
| 12 | * You have to run this daemon via inetd. |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 13 | */ |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 14 | //config:config FTPD |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 15 | //config: bool "ftpd (30 kb)" |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 16 | //config: default y |
| 17 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 18 | //config: Simple FTP daemon. You have to run it via inetd. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 19 | //config: |
Denys Vlasenko | 3148e0c | 2016-11-23 09:07:44 +0100 | [diff] [blame] | 20 | //config:config FEATURE_FTPD_WRITE |
Denys Vlasenko | 68b653b | 2017-07-27 10:53:09 +0200 | [diff] [blame] | 21 | //config: bool "Enable -w (upload commands)" |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 22 | //config: default y |
| 23 | //config: depends on FTPD |
| 24 | //config: help |
Denys Vlasenko | 68b653b | 2017-07-27 10:53:09 +0200 | [diff] [blame] | 25 | //config: Enable -w option. "ftpd -w" will accept upload commands |
| 26 | //config: such as STOR, STOU, APPE, DELE, MKD, RMD, rename commands. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 27 | //config: |
| 28 | //config:config FEATURE_FTPD_ACCEPT_BROKEN_LIST |
| 29 | //config: bool "Enable workaround for RFC-violating clients" |
| 30 | //config: default y |
| 31 | //config: depends on FTPD |
| 32 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 33 | //config: Some ftp clients (among them KDE's Konqueror) issue illegal |
| 34 | //config: "LIST -l" requests. This option works around such problems. |
| 35 | //config: It might prevent you from listing files starting with "-" and |
| 36 | //config: it increases the code size by ~40 bytes. |
| 37 | //config: Most other ftp servers seem to behave similar to this. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 38 | //config: |
Denys Vlasenko | 3148e0c | 2016-11-23 09:07:44 +0100 | [diff] [blame] | 39 | //config:config FEATURE_FTPD_AUTHENTICATION |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 40 | //config: bool "Enable authentication" |
| 41 | //config: default y |
| 42 | //config: depends on FTPD |
| 43 | //config: help |
Denys Vlasenko | 68b653b | 2017-07-27 10:53:09 +0200 | [diff] [blame] | 44 | //config: Require login, and change to logged in user's UID:GID before |
| 45 | //config: accessing any files. Option "-a USER" allows "anonymous" |
| 46 | //config: logins (treats them as if USER logged in). |
| 47 | //config: |
| 48 | //config: If this option is not selected, ftpd runs with the rights |
| 49 | //config: of the user it was started under, and does not require login. |
| 50 | //config: Take care to not launch it under root. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 51 | |
| 52 | //applet:IF_FTPD(APPLET(ftpd, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 53 | |
| 54 | //kbuild:lib-$(CONFIG_FTPD) += ftpd.o |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 55 | |
Denys Vlasenko | 115c35d | 2011-03-08 03:01:10 +0100 | [diff] [blame] | 56 | //usage:#define ftpd_trivial_usage |
Andrey Mozzhuhin | 2181fb4 | 2017-01-24 23:02:04 +0100 | [diff] [blame] | 57 | //usage: "[-wvS]"IF_FEATURE_FTPD_AUTHENTICATION(" [-a USER]")" [-t N] [-T N] [DIR]" |
Denys Vlasenko | 115c35d | 2011-03-08 03:01:10 +0100 | [diff] [blame] | 58 | //usage:#define ftpd_full_usage "\n\n" |
Andrey Mozzhuhin | 2181fb4 | 2017-01-24 23:02:04 +0100 | [diff] [blame] | 59 | //usage: IF_NOT_FEATURE_FTPD_AUTHENTICATION( |
| 60 | //usage: "Anonymous FTP server. Accesses by clients occur under ftpd's UID.\n" |
| 61 | //usage: ) |
| 62 | //usage: IF_FEATURE_FTPD_AUTHENTICATION( |
| 63 | //usage: "FTP server. " |
| 64 | //usage: ) |
| 65 | //usage: "Chroots to DIR, if this fails (run by non-root), cds to it.\n" |
| 66 | //usage: "Should be used as inetd service, inetd.conf line:\n" |
Denys Vlasenko | 115c35d | 2011-03-08 03:01:10 +0100 | [diff] [blame] | 67 | //usage: " 21 stream tcp nowait root ftpd ftpd /files/to/serve\n" |
Andrey Mozzhuhin | 2181fb4 | 2017-01-24 23:02:04 +0100 | [diff] [blame] | 68 | //usage: "Can be run from tcpsvd:\n" |
Denys Vlasenko | 115c35d | 2011-03-08 03:01:10 +0100 | [diff] [blame] | 69 | //usage: " tcpsvd -vE 0.0.0.0 21 ftpd /files/to/serve\n" |
Denys Vlasenko | 115c35d | 2011-03-08 03:01:10 +0100 | [diff] [blame] | 70 | //usage: "\n -w Allow upload" |
Andrey Mozzhuhin | 2181fb4 | 2017-01-24 23:02:04 +0100 | [diff] [blame] | 71 | //usage: IF_FEATURE_FTPD_AUTHENTICATION( |
| 72 | //usage: "\n -a USER Enable 'anonymous' login and map it to USER" |
| 73 | //usage: ) |
Denys Vlasenko | b13b618 | 2017-01-25 04:52:45 +0100 | [diff] [blame] | 74 | //usage: "\n -v Log errors to stderr. -vv: verbose log" |
| 75 | //usage: "\n -S Log errors to syslog. -SS: verbose log" |
| 76 | //usage: "\n -t,-T N Idle and absolute timeout" |
Denys Vlasenko | 115c35d | 2011-03-08 03:01:10 +0100 | [diff] [blame] | 77 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 78 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 79 | #include "common_bufsiz.h" |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 80 | #include <syslog.h> |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 81 | #include <netinet/tcp.h> |
| 82 | |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 83 | #define FTP_DATACONN 150 |
| 84 | #define FTP_NOOPOK 200 |
| 85 | #define FTP_TYPEOK 200 |
| 86 | #define FTP_PORTOK 200 |
| 87 | #define FTP_STRUOK 200 |
| 88 | #define FTP_MODEOK 200 |
| 89 | #define FTP_ALLOOK 202 |
| 90 | #define FTP_STATOK 211 |
| 91 | #define FTP_STATFILE_OK 213 |
| 92 | #define FTP_HELP 214 |
| 93 | #define FTP_SYSTOK 215 |
| 94 | #define FTP_GREET 220 |
| 95 | #define FTP_GOODBYE 221 |
| 96 | #define FTP_TRANSFEROK 226 |
| 97 | #define FTP_PASVOK 227 |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 98 | /*#define FTP_EPRTOK 228*/ |
| 99 | #define FTP_EPSVOK 229 |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 100 | #define FTP_LOGINOK 230 |
| 101 | #define FTP_CWDOK 250 |
| 102 | #define FTP_RMDIROK 250 |
| 103 | #define FTP_DELEOK 250 |
| 104 | #define FTP_RENAMEOK 250 |
| 105 | #define FTP_PWDOK 257 |
| 106 | #define FTP_MKDIROK 257 |
| 107 | #define FTP_GIVEPWORD 331 |
| 108 | #define FTP_RESTOK 350 |
| 109 | #define FTP_RNFROK 350 |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 110 | #define FTP_TIMEOUT 421 |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 111 | #define FTP_BADSENDCONN 425 |
| 112 | #define FTP_BADSENDNET 426 |
| 113 | #define FTP_BADSENDFILE 451 |
| 114 | #define FTP_BADCMD 500 |
| 115 | #define FTP_COMMANDNOTIMPL 502 |
| 116 | #define FTP_NEEDUSER 503 |
| 117 | #define FTP_NEEDRNFR 503 |
| 118 | #define FTP_BADSTRU 504 |
| 119 | #define FTP_BADMODE 504 |
| 120 | #define FTP_LOGINERR 530 |
| 121 | #define FTP_FILEFAIL 550 |
| 122 | #define FTP_NOPERM 550 |
| 123 | #define FTP_UPLOADFAIL 553 |
| 124 | |
| 125 | #define STR1(s) #s |
| 126 | #define STR(s) STR1(s) |
| 127 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 128 | /* Convert a constant to 3-digit string, packed into uint32_t */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 129 | enum { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 130 | /* Shift for Nth decimal digit */ |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 131 | SHIFT2 = 0 * BB_LITTLE_ENDIAN + 24 * BB_BIG_ENDIAN, |
| 132 | SHIFT1 = 8 * BB_LITTLE_ENDIAN + 16 * BB_BIG_ENDIAN, |
| 133 | SHIFT0 = 16 * BB_LITTLE_ENDIAN + 8 * BB_BIG_ENDIAN, |
| 134 | /* And for 4th position (space) */ |
| 135 | SHIFTsp = 24 * BB_LITTLE_ENDIAN + 0 * BB_BIG_ENDIAN, |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 136 | }; |
| 137 | #define STRNUM32(s) (uint32_t)(0 \ |
| 138 | | (('0' + ((s) / 1 % 10)) << SHIFT0) \ |
| 139 | | (('0' + ((s) / 10 % 10)) << SHIFT1) \ |
| 140 | | (('0' + ((s) / 100 % 10)) << SHIFT2) \ |
| 141 | ) |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 142 | #define STRNUM32sp(s) (uint32_t)(0 \ |
| 143 | | (' ' << SHIFTsp) \ |
| 144 | | (('0' + ((s) / 1 % 10)) << SHIFT0) \ |
| 145 | | (('0' + ((s) / 10 % 10)) << SHIFT1) \ |
| 146 | | (('0' + ((s) / 100 % 10)) << SHIFT2) \ |
| 147 | ) |
| 148 | |
| 149 | #define MSG_OK "Operation successful\r\n" |
| 150 | #define MSG_ERR "Error\r\n" |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 151 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 152 | struct globals { |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 153 | int pasv_listen_fd; |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 154 | #if !BB_MMU |
| 155 | int root_fd; |
| 156 | #endif |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 157 | int local_file_fd; |
Denis Vlasenko | e6c94a6 | 2009-03-17 05:11:51 +0000 | [diff] [blame] | 158 | unsigned end_time; |
| 159 | unsigned timeout; |
Denis Vlasenko | edb0de4 | 2009-03-17 12:23:24 +0000 | [diff] [blame] | 160 | unsigned verbose; |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 161 | off_t local_file_pos; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 162 | off_t restart_pos; |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 163 | len_and_sockaddr *local_addr; |
| 164 | len_and_sockaddr *port_addr; |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 165 | char *ftp_cmd; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 166 | char *ftp_arg; |
Denys Vlasenko | 3148e0c | 2016-11-23 09:07:44 +0100 | [diff] [blame] | 167 | #if ENABLE_FEATURE_FTPD_WRITE |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 168 | char *rnfr_filename; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 169 | #endif |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 170 | /* We need these aligned to uint32_t */ |
| 171 | char msg_ok [(sizeof("NNN " MSG_OK ) + 3) & 0xfffc]; |
| 172 | char msg_err[(sizeof("NNN " MSG_ERR) + 3) & 0xfffc]; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 173 | } FIX_ALIASING; |
Denys Vlasenko | 565af23 | 2018-02-04 18:32:21 +0100 | [diff] [blame] | 174 | #define G (*ptr_to_globals) |
| 175 | /* ^^^ about 75 bytes smaller code than this: */ |
| 176 | //#define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 177 | #define INIT_G() do { \ |
Denys Vlasenko | 565af23 | 2018-02-04 18:32:21 +0100 | [diff] [blame] | 178 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 179 | /*setup_common_bufsiz();*/ \ |
| 180 | \ |
Denis Vlasenko | e6c94a6 | 2009-03-17 05:11:51 +0000 | [diff] [blame] | 181 | /* Moved to main */ \ |
| 182 | /*strcpy(G.msg_ok + 4, MSG_OK );*/ \ |
| 183 | /*strcpy(G.msg_err + 4, MSG_ERR);*/ \ |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 184 | } while (0) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 185 | |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 186 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 187 | static char * |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 188 | escape_text(const char *prepend, const char *str, unsigned escapee) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 189 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 190 | unsigned retlen, remainlen, chunklen; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 191 | char *ret, *found; |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 192 | char append; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 193 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 194 | append = (char)escapee; |
| 195 | escapee >>= 8; |
| 196 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 197 | remainlen = strlen(str); |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 198 | retlen = strlen(prepend); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 199 | ret = xmalloc(retlen + remainlen * 2 + 1 + 1); |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 200 | strcpy(ret, prepend); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 201 | |
| 202 | for (;;) { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 203 | found = strchrnul(str, escapee); |
| 204 | chunklen = found - str + 1; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 205 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 206 | /* Copy chunk up to and including escapee (or NUL) to ret */ |
| 207 | memcpy(ret + retlen, str, chunklen); |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 208 | retlen += chunklen; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 209 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 210 | if (*found == '\0') { |
| 211 | /* It wasn't escapee, it was NUL! */ |
| 212 | ret[retlen - 1] = append; /* replace NUL */ |
| 213 | ret[retlen] = '\0'; /* add NUL */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 214 | break; |
| 215 | } |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 216 | ret[retlen++] = escapee; /* duplicate escapee */ |
| 217 | str = found + 1; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 218 | } |
| 219 | return ret; |
| 220 | } |
| 221 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 222 | /* Returns strlen as a bonus */ |
| 223 | static unsigned |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 224 | replace_char(char *str, char from, char to) |
| 225 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 226 | char *p = str; |
| 227 | while (*p) { |
| 228 | if (*p == from) |
| 229 | *p = to; |
| 230 | p++; |
| 231 | } |
| 232 | return p - str; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Denis Vlasenko | edb0de4 | 2009-03-17 12:23:24 +0000 | [diff] [blame] | 235 | static void |
| 236 | verbose_log(const char *str) |
| 237 | { |
| 238 | bb_error_msg("%.*s", (int)strcspn(str, "\r\n"), str); |
| 239 | } |
| 240 | |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 241 | /* NB: status_str is char[4] packed into uint32_t */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 242 | static void |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 243 | cmdio_write(uint32_t status_str, const char *str) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 244 | { |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 245 | char *response; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 246 | int len; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 247 | |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 248 | /* FTP uses telnet protocol for command link. |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 249 | * In telnet, 0xff is an escape char, and needs to be escaped: */ |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 250 | response = escape_text((char *) &status_str, str, (0xff << 8) + '\r'); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 251 | |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 252 | /* FTP sends embedded LFs as NULs */ |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 253 | len = replace_char(response, '\n', '\0'); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 254 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 255 | response[len++] = '\n'; /* tack on trailing '\n' */ |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 256 | xwrite(STDOUT_FILENO, response, len); |
Denis Vlasenko | edb0de4 | 2009-03-17 12:23:24 +0000 | [diff] [blame] | 257 | if (G.verbose > 1) |
| 258 | verbose_log(response); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 259 | free(response); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static void |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 263 | cmdio_write_ok(unsigned status) |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 264 | { |
Denys Vlasenko | f75a7c0 | 2018-02-04 23:55:14 +0100 | [diff] [blame^] | 265 | *(bb__aliased_uint32_t *) G.msg_ok = status; |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 266 | xwrite(STDOUT_FILENO, G.msg_ok, sizeof("NNN " MSG_OK) - 1); |
Denis Vlasenko | edb0de4 | 2009-03-17 12:23:24 +0000 | [diff] [blame] | 267 | if (G.verbose > 1) |
| 268 | verbose_log(G.msg_ok); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 269 | } |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 270 | #define WRITE_OK(a) cmdio_write_ok(STRNUM32sp(a)) |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 271 | |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 272 | /* TODO: output strerr(errno) if errno != 0? */ |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 273 | static void |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 274 | cmdio_write_error(unsigned status) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 275 | { |
Denys Vlasenko | f75a7c0 | 2018-02-04 23:55:14 +0100 | [diff] [blame^] | 276 | *(bb__aliased_uint32_t *) G.msg_err = status; |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 277 | xwrite(STDOUT_FILENO, G.msg_err, sizeof("NNN " MSG_ERR) - 1); |
Denys Vlasenko | 115c35d | 2011-03-08 03:01:10 +0100 | [diff] [blame] | 278 | if (G.verbose > 0) |
Denis Vlasenko | edb0de4 | 2009-03-17 12:23:24 +0000 | [diff] [blame] | 279 | verbose_log(G.msg_err); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 280 | } |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 281 | #define WRITE_ERR(a) cmdio_write_error(STRNUM32sp(a)) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 282 | |
| 283 | static void |
| 284 | cmdio_write_raw(const char *p_text) |
| 285 | { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 286 | xwrite_str(STDOUT_FILENO, p_text); |
Denis Vlasenko | edb0de4 | 2009-03-17 12:23:24 +0000 | [diff] [blame] | 287 | if (G.verbose > 1) |
| 288 | verbose_log(p_text); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 291 | static void |
| 292 | timeout_handler(int sig UNUSED_PARAM) |
| 293 | { |
| 294 | off_t pos; |
| 295 | int sv_errno = errno; |
| 296 | |
Denis Vlasenko | e6c94a6 | 2009-03-17 05:11:51 +0000 | [diff] [blame] | 297 | if ((int)(monotonic_sec() - G.end_time) >= 0) |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 298 | goto timed_out; |
| 299 | |
| 300 | if (!G.local_file_fd) |
| 301 | goto timed_out; |
| 302 | |
| 303 | pos = xlseek(G.local_file_fd, 0, SEEK_CUR); |
| 304 | if (pos == G.local_file_pos) |
| 305 | goto timed_out; |
| 306 | G.local_file_pos = pos; |
| 307 | |
| 308 | alarm(G.timeout); |
| 309 | errno = sv_errno; |
| 310 | return; |
| 311 | |
| 312 | timed_out: |
| 313 | cmdio_write_raw(STR(FTP_TIMEOUT)" Timeout\r\n"); |
| 314 | /* TODO: do we need to abort (as opposed to usual shutdown) data transfer? */ |
| 315 | exit(1); |
| 316 | } |
| 317 | |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 318 | /* Simple commands */ |
| 319 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 320 | static void |
| 321 | handle_pwd(void) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 322 | { |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 323 | char *cwd, *response; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 324 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 325 | cwd = xrealloc_getcwd_or_warn(NULL); |
| 326 | if (cwd == NULL) |
| 327 | cwd = xstrdup(""); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 328 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 329 | /* We have to promote each " to "" */ |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 330 | response = escape_text(" \"", cwd, ('"' << 8) + '"'); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 331 | free(cwd); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 332 | cmdio_write(STRNUM32(FTP_PWDOK), response); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 333 | free(response); |
| 334 | } |
| 335 | |
| 336 | static void |
| 337 | handle_cwd(void) |
| 338 | { |
| 339 | if (!G.ftp_arg || chdir(G.ftp_arg) != 0) { |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 340 | WRITE_ERR(FTP_FILEFAIL); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 341 | return; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 342 | } |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 343 | WRITE_OK(FTP_CWDOK); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 344 | } |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 345 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 346 | static void |
| 347 | handle_cdup(void) |
| 348 | { |
| 349 | G.ftp_arg = (char*)".."; |
| 350 | handle_cwd(); |
| 351 | } |
| 352 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 353 | static void |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 354 | handle_stat(void) |
| 355 | { |
Denis Vlasenko | e3b840c | 2009-03-18 14:25:28 +0000 | [diff] [blame] | 356 | cmdio_write_raw(STR(FTP_STATOK)"-Server status:\r\n" |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 357 | " TYPE: BINARY\r\n" |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 358 | STR(FTP_STATOK)" Ok\r\n"); |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Denis Vlasenko | 1a82555 | 2009-03-17 12:40:34 +0000 | [diff] [blame] | 361 | /* Examples of HELP and FEAT: |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 362 | # nc -vvv ftp.kernel.org 21 |
| 363 | ftp.kernel.org (130.239.17.4:21) open |
| 364 | 220 Welcome to ftp.kernel.org. |
| 365 | FEAT |
| 366 | 211-Features: |
| 367 | EPRT |
| 368 | EPSV |
| 369 | MDTM |
| 370 | PASV |
| 371 | REST STREAM |
| 372 | SIZE |
| 373 | TVFS |
| 374 | UTF8 |
| 375 | 211 End |
| 376 | HELP |
| 377 | 214-The following commands are recognized. |
| 378 | ABOR ACCT ALLO APPE CDUP CWD DELE EPRT EPSV FEAT HELP LIST MDTM MKD |
| 379 | MODE NLST NOOP OPTS PASS PASV PORT PWD QUIT REIN REST RETR RMD RNFR |
| 380 | RNTO SITE SIZE SMNT STAT STOR STOU STRU SYST TYPE USER XCUP XCWD XMKD |
| 381 | XPWD XRMD |
| 382 | 214 Help OK. |
| 383 | */ |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 384 | static void |
Denis Vlasenko | 1a82555 | 2009-03-17 12:40:34 +0000 | [diff] [blame] | 385 | handle_feat(unsigned status) |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 386 | { |
Denis Vlasenko | 1a82555 | 2009-03-17 12:40:34 +0000 | [diff] [blame] | 387 | cmdio_write(status, "-Features:"); |
| 388 | cmdio_write_raw(" EPSV\r\n" |
| 389 | " PASV\r\n" |
| 390 | " REST STREAM\r\n" |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 391 | " MDTM\r\n" |
Denis Vlasenko | 1a82555 | 2009-03-17 12:40:34 +0000 | [diff] [blame] | 392 | " SIZE\r\n"); |
| 393 | cmdio_write(status, " Ok"); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 396 | /* Download commands */ |
| 397 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 398 | static inline int |
| 399 | port_active(void) |
| 400 | { |
| 401 | return (G.port_addr != NULL); |
| 402 | } |
| 403 | |
| 404 | static inline int |
| 405 | pasv_active(void) |
| 406 | { |
| 407 | return (G.pasv_listen_fd > STDOUT_FILENO); |
| 408 | } |
| 409 | |
| 410 | static void |
| 411 | port_pasv_cleanup(void) |
| 412 | { |
| 413 | free(G.port_addr); |
| 414 | G.port_addr = NULL; |
| 415 | if (G.pasv_listen_fd > STDOUT_FILENO) |
| 416 | close(G.pasv_listen_fd); |
| 417 | G.pasv_listen_fd = -1; |
| 418 | } |
| 419 | |
| 420 | /* On error, emits error code to the peer */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 421 | static int |
| 422 | ftpdataio_get_pasv_fd(void) |
| 423 | { |
| 424 | int remote_fd; |
| 425 | |
| 426 | remote_fd = accept(G.pasv_listen_fd, NULL, 0); |
| 427 | |
| 428 | if (remote_fd < 0) { |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 429 | WRITE_ERR(FTP_BADSENDCONN); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 430 | return remote_fd; |
| 431 | } |
| 432 | |
Denys Vlasenko | c52cbea | 2015-08-24 19:48:03 +0200 | [diff] [blame] | 433 | setsockopt_keepalive(remote_fd); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 434 | return remote_fd; |
| 435 | } |
| 436 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 437 | /* Clears port/pasv data. |
| 438 | * This means we dont waste resources, for example, keeping |
| 439 | * PASV listening socket open when it is no longer needed. |
| 440 | * On error, emits error code to the peer (or exits). |
| 441 | * On success, emits p_status_msg to the peer. |
| 442 | */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 443 | static int |
| 444 | get_remote_transfer_fd(const char *p_status_msg) |
| 445 | { |
| 446 | int remote_fd; |
| 447 | |
| 448 | if (pasv_active()) |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 449 | /* On error, emits error code to the peer */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 450 | remote_fd = ftpdataio_get_pasv_fd(); |
| 451 | else |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 452 | /* Exits on error */ |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 453 | remote_fd = xconnect_stream(G.port_addr); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 454 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 455 | port_pasv_cleanup(); |
| 456 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 457 | if (remote_fd < 0) |
| 458 | return remote_fd; |
| 459 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 460 | cmdio_write(STRNUM32(FTP_DATACONN), p_status_msg); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 461 | return remote_fd; |
| 462 | } |
| 463 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 464 | /* If there were neither PASV nor PORT, emits error code to the peer */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 465 | static int |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 466 | port_or_pasv_was_seen(void) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 467 | { |
| 468 | if (!pasv_active() && !port_active()) { |
Denis Vlasenko | e3b840c | 2009-03-18 14:25:28 +0000 | [diff] [blame] | 469 | cmdio_write_raw(STR(FTP_BADSENDCONN)" Use PORT/PASV first\r\n"); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | return 1; |
| 474 | } |
| 475 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 476 | /* Exits on error */ |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 477 | static unsigned |
| 478 | bind_for_passive_mode(void) |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 479 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 480 | int fd; |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 481 | unsigned port; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 482 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 483 | port_pasv_cleanup(); |
| 484 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 485 | G.pasv_listen_fd = fd = xsocket(G.local_addr->u.sa.sa_family, SOCK_STREAM, 0); |
| 486 | setsockopt_reuseaddr(fd); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 487 | |
Denys Vlasenko | ca18311 | 2011-04-07 17:52:20 +0200 | [diff] [blame] | 488 | set_nport(&G.local_addr->u.sa, 0); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 489 | xbind(fd, &G.local_addr->u.sa, G.local_addr->len); |
| 490 | xlisten(fd, 1); |
| 491 | getsockname(fd, &G.local_addr->u.sa, &G.local_addr->len); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 492 | |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 493 | port = get_nport(&G.local_addr->u.sa); |
| 494 | port = ntohs(port); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 495 | return port; |
| 496 | } |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 497 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 498 | /* Exits on error */ |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 499 | static void |
| 500 | handle_pasv(void) |
| 501 | { |
| 502 | unsigned port; |
| 503 | char *addr, *response; |
| 504 | |
| 505 | port = bind_for_passive_mode(); |
| 506 | |
| 507 | if (G.local_addr->u.sa.sa_family == AF_INET) |
| 508 | addr = xmalloc_sockaddr2dotted_noport(&G.local_addr->u.sa); |
| 509 | else /* seen this in the wild done by other ftp servers: */ |
| 510 | addr = xstrdup("0.0.0.0"); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 511 | replace_char(addr, '.', ','); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 512 | |
Denis Vlasenko | e3b840c | 2009-03-18 14:25:28 +0000 | [diff] [blame] | 513 | response = xasprintf(STR(FTP_PASVOK)" PASV ok (%s,%u,%u)\r\n", |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 514 | addr, (int)(port >> 8), (int)(port & 255)); |
| 515 | free(addr); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 516 | cmdio_write_raw(response); |
| 517 | free(response); |
| 518 | } |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 519 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 520 | /* Exits on error */ |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 521 | static void |
| 522 | handle_epsv(void) |
| 523 | { |
| 524 | unsigned port; |
| 525 | char *response; |
| 526 | |
| 527 | port = bind_for_passive_mode(); |
Denis Vlasenko | e3b840c | 2009-03-18 14:25:28 +0000 | [diff] [blame] | 528 | response = xasprintf(STR(FTP_EPSVOK)" EPSV ok (|||%u|)\r\n", port); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 529 | cmdio_write_raw(response); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 530 | free(response); |
| 531 | } |
| 532 | |
| 533 | static void |
| 534 | handle_port(void) |
| 535 | { |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 536 | unsigned port, port_hi; |
| 537 | char *raw, *comma; |
Denis Vlasenko | 43bb7bb | 2009-03-16 19:54:06 +0000 | [diff] [blame] | 538 | #ifdef WHY_BOTHER_WE_CAN_ASSUME_IP_MATCHES |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 539 | socklen_t peer_ipv4_len; |
| 540 | struct sockaddr_in peer_ipv4; |
| 541 | struct in_addr port_ipv4_sin_addr; |
Denis Vlasenko | 43bb7bb | 2009-03-16 19:54:06 +0000 | [diff] [blame] | 542 | #endif |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 543 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 544 | port_pasv_cleanup(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 545 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 546 | raw = G.ftp_arg; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 547 | |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 548 | /* PORT command format makes sense only over IPv4 */ |
Denis Vlasenko | 43bb7bb | 2009-03-16 19:54:06 +0000 | [diff] [blame] | 549 | if (!raw |
| 550 | #ifdef WHY_BOTHER_WE_CAN_ASSUME_IP_MATCHES |
| 551 | || G.local_addr->u.sa.sa_family != AF_INET |
| 552 | #endif |
| 553 | ) { |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 554 | bail: |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 555 | WRITE_ERR(FTP_BADCMD); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 556 | return; |
| 557 | } |
| 558 | |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 559 | comma = strrchr(raw, ','); |
| 560 | if (comma == NULL) |
| 561 | goto bail; |
| 562 | *comma = '\0'; |
| 563 | port = bb_strtou(&comma[1], NULL, 10); |
| 564 | if (errno || port > 0xff) |
| 565 | goto bail; |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 566 | |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 567 | comma = strrchr(raw, ','); |
| 568 | if (comma == NULL) |
| 569 | goto bail; |
| 570 | *comma = '\0'; |
| 571 | port_hi = bb_strtou(&comma[1], NULL, 10); |
| 572 | if (errno || port_hi > 0xff) |
| 573 | goto bail; |
| 574 | port |= port_hi << 8; |
| 575 | |
Denis Vlasenko | 43bb7bb | 2009-03-16 19:54:06 +0000 | [diff] [blame] | 576 | #ifdef WHY_BOTHER_WE_CAN_ASSUME_IP_MATCHES |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 577 | replace_char(raw, ',', '.'); |
| 578 | |
| 579 | /* We are verifying that PORT's IP matches getpeername(). |
| 580 | * Otherwise peer can make us open data connections |
| 581 | * to other hosts (security problem!) |
| 582 | * This code would be too simplistic: |
| 583 | * lsa = xdotted2sockaddr(raw, port); |
| 584 | * if (lsa == NULL) goto bail; |
| 585 | */ |
| 586 | if (!inet_aton(raw, &port_ipv4_sin_addr)) |
| 587 | goto bail; |
| 588 | peer_ipv4_len = sizeof(peer_ipv4); |
| 589 | if (getpeername(STDIN_FILENO, &peer_ipv4, &peer_ipv4_len) != 0) |
| 590 | goto bail; |
| 591 | if (memcmp(&port_ipv4_sin_addr, &peer_ipv4.sin_addr, sizeof(struct in_addr)) != 0) |
| 592 | goto bail; |
| 593 | |
| 594 | G.port_addr = xdotted2sockaddr(raw, port); |
Denis Vlasenko | 43bb7bb | 2009-03-16 19:54:06 +0000 | [diff] [blame] | 595 | #else |
| 596 | G.port_addr = get_peer_lsa(STDIN_FILENO); |
Denys Vlasenko | ca18311 | 2011-04-07 17:52:20 +0200 | [diff] [blame] | 597 | set_nport(&G.port_addr->u.sa, htons(port)); |
Denis Vlasenko | 43bb7bb | 2009-03-16 19:54:06 +0000 | [diff] [blame] | 598 | #endif |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 599 | WRITE_OK(FTP_PORTOK); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static void |
| 603 | handle_rest(void) |
| 604 | { |
| 605 | /* When ftp_arg == NULL simply restart from beginning */ |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 606 | G.restart_pos = G.ftp_arg ? xatoi_positive(G.ftp_arg) : 0; |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 607 | WRITE_OK(FTP_RESTOK); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static void |
| 611 | handle_retr(void) |
| 612 | { |
| 613 | struct stat statbuf; |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 614 | off_t bytes_transferred; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 615 | int remote_fd; |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 616 | int local_file_fd; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 617 | off_t offset = G.restart_pos; |
| 618 | char *response; |
| 619 | |
| 620 | G.restart_pos = 0; |
| 621 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 622 | if (!port_or_pasv_was_seen()) |
| 623 | return; /* port_or_pasv_was_seen emitted error response */ |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 624 | |
| 625 | /* O_NONBLOCK is useful if file happens to be a device node */ |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 626 | local_file_fd = G.ftp_arg ? open(G.ftp_arg, O_RDONLY | O_NONBLOCK) : -1; |
| 627 | if (local_file_fd < 0) { |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 628 | WRITE_ERR(FTP_FILEFAIL); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 629 | return; |
| 630 | } |
| 631 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 632 | if (fstat(local_file_fd, &statbuf) != 0 || !S_ISREG(statbuf.st_mode)) { |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 633 | /* Note - pretend open failed */ |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 634 | WRITE_ERR(FTP_FILEFAIL); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 635 | goto file_close_out; |
| 636 | } |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 637 | G.local_file_fd = local_file_fd; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 638 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 639 | /* Now deactive O_NONBLOCK, otherwise we have a problem |
| 640 | * on DMAPI filesystems such as XFS DMAPI. |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 641 | */ |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 642 | ndelay_off(local_file_fd); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 643 | |
| 644 | /* Set the download offset (from REST) if any */ |
| 645 | if (offset != 0) |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 646 | xlseek(local_file_fd, offset, SEEK_SET); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 647 | |
| 648 | response = xasprintf( |
Denis Vlasenko | e3b840c | 2009-03-18 14:25:28 +0000 | [diff] [blame] | 649 | " Opening BINARY connection for %s (%"OFF_FMT"u bytes)", |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 650 | G.ftp_arg, statbuf.st_size); |
| 651 | remote_fd = get_remote_transfer_fd(response); |
| 652 | free(response); |
| 653 | if (remote_fd < 0) |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 654 | goto file_close_out; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 655 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 656 | bytes_transferred = bb_copyfd_eof(local_file_fd, remote_fd); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 657 | close(remote_fd); |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 658 | if (bytes_transferred < 0) |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 659 | WRITE_ERR(FTP_BADSENDFILE); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 660 | else |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 661 | WRITE_OK(FTP_TRANSFEROK); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 662 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 663 | file_close_out: |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 664 | close(local_file_fd); |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 665 | G.local_file_fd = 0; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 668 | /* List commands */ |
| 669 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 670 | static int |
| 671 | popen_ls(const char *opt) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 672 | { |
Denys Vlasenko | 33f9dc0 | 2010-01-17 22:32:22 +0100 | [diff] [blame] | 673 | const char *argv[5]; |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 674 | struct fd_pair outfd; |
| 675 | pid_t pid; |
Denys Vlasenko | 27c290f | 2014-06-27 12:37:00 +0200 | [diff] [blame] | 676 | |
Denys Vlasenko | 33f9dc0 | 2010-01-17 22:32:22 +0100 | [diff] [blame] | 677 | argv[0] = "ftpd"; |
Denys Vlasenko | 7666fa1 | 2014-09-15 23:35:58 +0200 | [diff] [blame] | 678 | argv[1] = opt; /* "-lA" or "-1A" */ |
Denys Vlasenko | 33f9dc0 | 2010-01-17 22:32:22 +0100 | [diff] [blame] | 679 | argv[2] = "--"; |
Denys Vlasenko | 33f9dc0 | 2010-01-17 22:32:22 +0100 | [diff] [blame] | 680 | argv[3] = G.ftp_arg; |
| 681 | argv[4] = NULL; |
| 682 | |
Stefan Seyfried | f3fc9ac | 2010-01-18 02:08:30 +0100 | [diff] [blame] | 683 | /* Improve compatibility with non-RFC conforming FTP clients |
Denys Vlasenko | 238c83b | 2010-04-21 18:05:10 -0400 | [diff] [blame] | 684 | * which send e.g. "LIST -l", "LIST -la", "LIST -aL". |
Stefan Seyfried | f3fc9ac | 2010-01-18 02:08:30 +0100 | [diff] [blame] | 685 | * See https://bugs.kde.org/show_bug.cgi?id=195578 */ |
| 686 | if (ENABLE_FEATURE_FTPD_ACCEPT_BROKEN_LIST |
Denys Vlasenko | 238c83b | 2010-04-21 18:05:10 -0400 | [diff] [blame] | 687 | && G.ftp_arg && G.ftp_arg[0] == '-' |
Stefan Seyfried | f3fc9ac | 2010-01-18 02:08:30 +0100 | [diff] [blame] | 688 | ) { |
| 689 | const char *tmp = strchr(G.ftp_arg, ' '); |
| 690 | if (tmp) /* skip the space */ |
| 691 | tmp++; |
| 692 | argv[3] = tmp; |
| 693 | } |
| 694 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 695 | xpiped_pair(outfd); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 696 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 697 | /*fflush_all(); - so far we dont use stdio on output */ |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 698 | pid = BB_MMU ? xfork() : xvfork(); |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 699 | if (pid == 0) { |
Denys Vlasenko | 27c290f | 2014-06-27 12:37:00 +0200 | [diff] [blame] | 700 | #if !BB_MMU |
| 701 | int cur_fd; |
| 702 | #endif |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 703 | /* child */ |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 704 | /* NB: close _first_, then move fd! */ |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 705 | close(outfd.rd); |
| 706 | xmove_fd(outfd.wr, STDOUT_FILENO); |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 707 | /* Opening /dev/null in chroot is hard. |
| 708 | * Just making sure STDIN_FILENO is opened |
| 709 | * to something harmless. Paranoia, |
| 710 | * ls won't read it anyway */ |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 711 | close(STDIN_FILENO); |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 712 | dup(STDOUT_FILENO); /* copy will become STDIN_FILENO */ |
Denys Vlasenko | 33f9dc0 | 2010-01-17 22:32:22 +0100 | [diff] [blame] | 713 | #if BB_MMU |
| 714 | /* memset(&G, 0, sizeof(G)); - ls_main does it */ |
Denys Vlasenko | b13b618 | 2017-01-25 04:52:45 +0100 | [diff] [blame] | 715 | exit(ls_main(/*argc_unused*/ 0, (char**) argv)); |
Denys Vlasenko | 33f9dc0 | 2010-01-17 22:32:22 +0100 | [diff] [blame] | 716 | #else |
Denys Vlasenko | 27c290f | 2014-06-27 12:37:00 +0200 | [diff] [blame] | 717 | cur_fd = xopen(".", O_RDONLY | O_DIRECTORY); |
Denys Vlasenko | a6ae999 | 2014-06-27 12:24:39 +0200 | [diff] [blame] | 718 | /* On NOMMU, we want to execute a child - copy of ourself |
| 719 | * in order to unblock parent after vfork. |
| 720 | * In chroot we usually can't re-exec. Thus we escape |
| 721 | * out of the chroot back to original root. |
| 722 | */ |
| 723 | if (G.root_fd >= 0) { |
| 724 | if (fchdir(G.root_fd) != 0 || chroot(".") != 0) |
| 725 | _exit(127); |
| 726 | /*close(G.root_fd); - close_on_exec_on() took care of this */ |
| 727 | } |
| 728 | /* Child expects directory to list on fd #3 */ |
| 729 | xmove_fd(cur_fd, 3); |
| 730 | execv(bb_busybox_exec_path, (char**) argv); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 731 | _exit(127); |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 732 | #endif |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 733 | } |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 734 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 735 | /* parent */ |
| 736 | close(outfd.wr); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 737 | return outfd.rd; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 740 | enum { |
| 741 | USE_CTRL_CONN = 1, |
| 742 | LONG_LISTING = 2, |
| 743 | }; |
| 744 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 745 | static void |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 746 | handle_dir_common(int opts) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 747 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 748 | FILE *ls_fp; |
| 749 | char *line; |
| 750 | int ls_fd; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 751 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 752 | if (!(opts & USE_CTRL_CONN) && !port_or_pasv_was_seen()) |
| 753 | return; /* port_or_pasv_was_seen emitted error response */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 754 | |
Denys Vlasenko | 7666fa1 | 2014-09-15 23:35:58 +0200 | [diff] [blame] | 755 | ls_fd = popen_ls((opts & LONG_LISTING) ? "-lA" : "-1A"); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 756 | ls_fp = xfdopen_for_read(ls_fd); |
Denys Vlasenko | 0b0ccd4 | 2014-06-26 12:07:48 +0200 | [diff] [blame] | 757 | /* FIXME: filenames with embedded newlines are mishandled */ |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 758 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 759 | if (opts & USE_CTRL_CONN) { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 760 | /* STAT <filename> */ |
Denis Vlasenko | e3b840c | 2009-03-18 14:25:28 +0000 | [diff] [blame] | 761 | cmdio_write_raw(STR(FTP_STATFILE_OK)"-File status:\r\n"); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 762 | while (1) { |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 763 | line = xmalloc_fgetline(ls_fp); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 764 | if (!line) |
| 765 | break; |
Denis Vlasenko | fce4a94 | 2009-03-18 16:02:54 +0000 | [diff] [blame] | 766 | /* Hack: 0 results in no status at all */ |
| 767 | /* Note: it's ok that we don't prepend space, |
| 768 | * ftp.kernel.org doesn't do that too */ |
| 769 | cmdio_write(0, line); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 770 | free(line); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 771 | } |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 772 | WRITE_OK(FTP_STATFILE_OK); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 773 | } else { |
| 774 | /* LIST/NLST [<filename>] */ |
Denis Vlasenko | e3b840c | 2009-03-18 14:25:28 +0000 | [diff] [blame] | 775 | int remote_fd = get_remote_transfer_fd(" Directory listing"); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 776 | if (remote_fd >= 0) { |
| 777 | while (1) { |
Denys Vlasenko | 0b0ccd4 | 2014-06-26 12:07:48 +0200 | [diff] [blame] | 778 | unsigned len; |
| 779 | |
| 780 | line = xmalloc_fgets(ls_fp); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 781 | if (!line) |
| 782 | break; |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 783 | /* I've seen clients complaining when they |
| 784 | * are fed with ls output with bare '\n'. |
Denys Vlasenko | 0b0ccd4 | 2014-06-26 12:07:48 +0200 | [diff] [blame] | 785 | * Replace trailing "\n\0" with "\r\n". |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 786 | */ |
Denys Vlasenko | 0b0ccd4 | 2014-06-26 12:07:48 +0200 | [diff] [blame] | 787 | len = strlen(line); |
| 788 | if (len != 0) /* paranoia check */ |
| 789 | line[len - 1] = '\r'; |
| 790 | line[len] = '\n'; |
| 791 | xwrite(remote_fd, line, len + 1); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 792 | free(line); |
| 793 | } |
| 794 | } |
| 795 | close(remote_fd); |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 796 | WRITE_OK(FTP_TRANSFEROK); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 797 | } |
| 798 | fclose(ls_fp); /* closes ls_fd too */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 799 | } |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 800 | static void |
| 801 | handle_list(void) |
| 802 | { |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 803 | handle_dir_common(LONG_LISTING); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 804 | } |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 805 | static void |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 806 | handle_nlst(void) |
| 807 | { |
Denis Vlasenko | 3a7a1eb | 2009-03-17 13:03:06 +0000 | [diff] [blame] | 808 | /* NLST returns list of names, "\r\n" terminated without regard |
| 809 | * to the current binary flag. Names may start with "/", |
| 810 | * then they represent full names (we don't produce such names), |
| 811 | * otherwise names are relative to current directory. |
| 812 | * Embedded "\n" are replaced by NULs. This is safe since names |
| 813 | * can never contain NUL. |
| 814 | */ |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 815 | handle_dir_common(0); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 816 | } |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 817 | static void |
| 818 | handle_stat_file(void) |
| 819 | { |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 820 | handle_dir_common(LONG_LISTING + USE_CTRL_CONN); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 823 | /* This can be extended to handle MLST, as all info is available |
| 824 | * in struct stat for that: |
| 825 | * MLST file_name |
| 826 | * 250-Listing file_name |
| 827 | * type=file;size=4161;modify=19970214165800; /dir/dir/file_name |
| 828 | * 250 End |
| 829 | * Nano-doc: |
| 830 | * MLST [<file or dir name, "." assumed if not given>] |
| 831 | * Returned name should be either the same as requested, or fully qualified. |
| 832 | * If there was no parameter, return "" or (preferred) fully-qualified name. |
| 833 | * Returned "facts" (case is not important): |
| 834 | * size - size in octets |
| 835 | * modify - last modification time |
| 836 | * type - entry type (file,dir,OS.unix=block) |
| 837 | * (+ cdir and pdir types for MLSD) |
| 838 | * unique - unique id of file/directory (inode#) |
| 839 | * perm - |
| 840 | * a: can be appended to (APPE) |
| 841 | * d: can be deleted (RMD/DELE) |
| 842 | * f: can be renamed (RNFR) |
| 843 | * r: can be read (RETR) |
| 844 | * w: can be written (STOR) |
| 845 | * e: can CWD into this dir |
| 846 | * l: this dir can be listed (dir only!) |
| 847 | * c: can create files in this dir |
| 848 | * m: can create dirs in this dir (MKD) |
| 849 | * p: can delete files in this dir |
| 850 | * UNIX.mode - unix file mode |
| 851 | */ |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 852 | static void |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 853 | handle_size_or_mdtm(int need_size) |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 854 | { |
| 855 | struct stat statbuf; |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 856 | struct tm broken_out; |
| 857 | char buf[(sizeof("NNN %"OFF_FMT"u\r\n") + sizeof(off_t) * 3) |
| 858 | | sizeof("NNN YYYYMMDDhhmmss\r\n") |
| 859 | ]; |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 860 | |
| 861 | if (!G.ftp_arg |
| 862 | || stat(G.ftp_arg, &statbuf) != 0 |
| 863 | || !S_ISREG(statbuf.st_mode) |
| 864 | ) { |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 865 | WRITE_ERR(FTP_FILEFAIL); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 866 | return; |
| 867 | } |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 868 | if (need_size) { |
| 869 | sprintf(buf, STR(FTP_STATFILE_OK)" %"OFF_FMT"u\r\n", statbuf.st_size); |
| 870 | } else { |
| 871 | gmtime_r(&statbuf.st_mtime, &broken_out); |
| 872 | sprintf(buf, STR(FTP_STATFILE_OK)" %04u%02u%02u%02u%02u%02u\r\n", |
| 873 | broken_out.tm_year + 1900, |
Denys Vlasenko | 3bb8587 | 2012-07-11 00:16:08 +0200 | [diff] [blame] | 874 | broken_out.tm_mon + 1, |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 875 | broken_out.tm_mday, |
| 876 | broken_out.tm_hour, |
| 877 | broken_out.tm_min, |
| 878 | broken_out.tm_sec); |
| 879 | } |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 880 | cmdio_write_raw(buf); |
| 881 | } |
| 882 | |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 883 | /* Upload commands */ |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 884 | |
Denys Vlasenko | 3148e0c | 2016-11-23 09:07:44 +0100 | [diff] [blame] | 885 | #if ENABLE_FEATURE_FTPD_WRITE |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 886 | static void |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 887 | handle_mkd(void) |
| 888 | { |
| 889 | if (!G.ftp_arg || mkdir(G.ftp_arg, 0777) != 0) { |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 890 | WRITE_ERR(FTP_FILEFAIL); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 891 | return; |
| 892 | } |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 893 | WRITE_OK(FTP_MKDIROK); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | static void |
| 897 | handle_rmd(void) |
| 898 | { |
| 899 | if (!G.ftp_arg || rmdir(G.ftp_arg) != 0) { |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 900 | WRITE_ERR(FTP_FILEFAIL); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 901 | return; |
| 902 | } |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 903 | WRITE_OK(FTP_RMDIROK); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static void |
| 907 | handle_dele(void) |
| 908 | { |
| 909 | if (!G.ftp_arg || unlink(G.ftp_arg) != 0) { |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 910 | WRITE_ERR(FTP_FILEFAIL); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 911 | return; |
| 912 | } |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 913 | WRITE_OK(FTP_DELEOK); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | static void |
| 917 | handle_rnfr(void) |
| 918 | { |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 919 | free(G.rnfr_filename); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 920 | G.rnfr_filename = xstrdup(G.ftp_arg); |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 921 | WRITE_OK(FTP_RNFROK); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | static void |
| 925 | handle_rnto(void) |
| 926 | { |
| 927 | int retval; |
| 928 | |
| 929 | /* If we didn't get a RNFR, throw a wobbly */ |
| 930 | if (G.rnfr_filename == NULL || G.ftp_arg == NULL) { |
Denis Vlasenko | e3b840c | 2009-03-18 14:25:28 +0000 | [diff] [blame] | 931 | cmdio_write_raw(STR(FTP_NEEDRNFR)" Use RNFR first\r\n"); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 932 | return; |
| 933 | } |
| 934 | |
| 935 | retval = rename(G.rnfr_filename, G.ftp_arg); |
| 936 | free(G.rnfr_filename); |
| 937 | G.rnfr_filename = NULL; |
| 938 | |
| 939 | if (retval) { |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 940 | WRITE_ERR(FTP_FILEFAIL); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 941 | return; |
| 942 | } |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 943 | WRITE_OK(FTP_RENAMEOK); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | static void |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 947 | handle_upload_common(int is_append, int is_unique) |
| 948 | { |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 949 | struct stat statbuf; |
| 950 | char *tempname; |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 951 | off_t bytes_transferred; |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 952 | off_t offset; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 953 | int local_file_fd; |
| 954 | int remote_fd; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 955 | |
| 956 | offset = G.restart_pos; |
| 957 | G.restart_pos = 0; |
| 958 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 959 | if (!port_or_pasv_was_seen()) |
| 960 | return; /* port_or_pasv_was_seen emitted error response */ |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 961 | |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 962 | tempname = NULL; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 963 | local_file_fd = -1; |
| 964 | if (is_unique) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 965 | tempname = xstrdup(" FILE: uniq.XXXXXX"); |
| 966 | local_file_fd = mkstemp(tempname + 7); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 967 | } else if (G.ftp_arg) { |
| 968 | int flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 969 | if (is_append) |
| 970 | flags = O_WRONLY | O_CREAT | O_APPEND; |
| 971 | if (offset) |
| 972 | flags = O_WRONLY | O_CREAT; |
| 973 | local_file_fd = open(G.ftp_arg, flags, 0666); |
| 974 | } |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 975 | |
| 976 | if (local_file_fd < 0 |
| 977 | || fstat(local_file_fd, &statbuf) != 0 |
| 978 | || !S_ISREG(statbuf.st_mode) |
| 979 | ) { |
Denys Vlasenko | e1db338 | 2012-07-11 00:26:24 +0200 | [diff] [blame] | 980 | free(tempname); |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 981 | WRITE_ERR(FTP_UPLOADFAIL); |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 982 | if (local_file_fd >= 0) |
| 983 | goto close_local_and_bail; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 984 | return; |
| 985 | } |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 986 | G.local_file_fd = local_file_fd; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 987 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 988 | if (offset) |
| 989 | xlseek(local_file_fd, offset, SEEK_SET); |
| 990 | |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 991 | remote_fd = get_remote_transfer_fd(tempname ? tempname : " Ok to send data"); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 992 | free(tempname); |
| 993 | |
| 994 | if (remote_fd < 0) |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 995 | goto close_local_and_bail; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 996 | |
Denis Vlasenko | fc58ba1 | 2009-03-15 15:54:58 +0000 | [diff] [blame] | 997 | bytes_transferred = bb_copyfd_eof(remote_fd, local_file_fd); |
| 998 | close(remote_fd); |
| 999 | if (bytes_transferred < 0) |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 1000 | WRITE_ERR(FTP_BADSENDFILE); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 1001 | else |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 1002 | WRITE_OK(FTP_TRANSFEROK); |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 1003 | |
| 1004 | close_local_and_bail: |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 1005 | close(local_file_fd); |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 1006 | G.local_file_fd = 0; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static void |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1010 | handle_stor(void) |
| 1011 | { |
| 1012 | handle_upload_common(0, 0); |
| 1013 | } |
| 1014 | |
| 1015 | static void |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1016 | handle_appe(void) |
| 1017 | { |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 1018 | G.restart_pos = 0; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1019 | handle_upload_common(1, 0); |
| 1020 | } |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1021 | |
| 1022 | static void |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1023 | handle_stou(void) |
| 1024 | { |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 1025 | G.restart_pos = 0; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1026 | handle_upload_common(0, 1); |
| 1027 | } |
Denys Vlasenko | 3148e0c | 2016-11-23 09:07:44 +0100 | [diff] [blame] | 1028 | #endif /* ENABLE_FEATURE_FTPD_WRITE */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1029 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 1030 | static uint32_t |
| 1031 | cmdio_get_cmd_and_arg(void) |
| 1032 | { |
Denys Vlasenko | 4a1884d | 2010-04-03 14:59:12 +0200 | [diff] [blame] | 1033 | int len; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 1034 | uint32_t cmdval; |
| 1035 | char *cmd; |
| 1036 | |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 1037 | alarm(G.timeout); |
| 1038 | |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 1039 | free(G.ftp_cmd); |
Denys Vlasenko | 4a1884d | 2010-04-03 14:59:12 +0200 | [diff] [blame] | 1040 | { |
| 1041 | /* Paranoia. Peer may send 1 gigabyte long cmd... */ |
| 1042 | /* Using separate len_on_stk instead of len optimizes |
| 1043 | * code size (allows len to be in CPU register) */ |
| 1044 | size_t len_on_stk = 8 * 1024; |
| 1045 | G.ftp_cmd = cmd = xmalloc_fgets_str_len(stdin, "\r\n", &len_on_stk); |
| 1046 | if (!cmd) |
| 1047 | exit(0); |
| 1048 | len = len_on_stk; |
| 1049 | } |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 1050 | |
Denis Vlasenko | 9f57cf6 | 2009-03-18 17:32:44 +0000 | [diff] [blame] | 1051 | /* De-escape telnet: 0xff,0xff => 0xff */ |
| 1052 | /* RFC959 says that ABOR, STAT, QUIT may be sent even during |
| 1053 | * data transfer, and may be preceded by telnet's "Interrupt Process" |
| 1054 | * code (two-byte sequence 255,244) and then by telnet "Synch" code |
| 1055 | * 255,242 (byte 242 is sent with TCP URG bit using send(MSG_OOB) |
| 1056 | * and may generate SIGURG on our side. See RFC854). |
| 1057 | * So far we don't support that (may install SIGURG handler if we'd want to), |
| 1058 | * but we need to at least remove 255,xxx pairs. lftp sends those. */ |
| 1059 | /* Then de-escape FTP: NUL => '\n' */ |
| 1060 | /* Testing for \xff: |
| 1061 | * Create file named '\xff': echo Hello >`echo -ne "\xff"` |
| 1062 | * Try to get it: ftpget -v 127.0.0.1 Eff `echo -ne "\xff\xff"` |
| 1063 | * (need "\xff\xff" until ftpget applet is fixed to do escaping :) |
| 1064 | * Testing for embedded LF: |
| 1065 | * LF_HERE=`echo -ne "LF\nHERE"` |
| 1066 | * echo Hello >"$LF_HERE" |
| 1067 | * ftpget -v 127.0.0.1 LF_HERE "$LF_HERE" |
| 1068 | */ |
| 1069 | { |
| 1070 | int dst, src; |
Denis Vlasenko | fce4a94 | 2009-03-18 16:02:54 +0000 | [diff] [blame] | 1071 | |
Denis Vlasenko | 9f57cf6 | 2009-03-18 17:32:44 +0000 | [diff] [blame] | 1072 | /* Strip "\r\n" if it is there */ |
| 1073 | if (len != 0 && cmd[len - 1] == '\n') { |
| 1074 | len--; |
| 1075 | if (len != 0 && cmd[len - 1] == '\r') |
| 1076 | len--; |
| 1077 | cmd[len] = '\0'; |
| 1078 | } |
| 1079 | src = strchrnul(cmd, 0xff) - cmd; |
| 1080 | /* 99,99% there are neither NULs nor 255s and src == len */ |
| 1081 | if (src < len) { |
| 1082 | dst = src; |
| 1083 | do { |
| 1084 | if ((unsigned char)(cmd[src]) == 255) { |
| 1085 | src++; |
| 1086 | /* 255,xxx - skip 255 */ |
| 1087 | if ((unsigned char)(cmd[src]) != 255) { |
| 1088 | /* 255,!255 - skip both */ |
| 1089 | src++; |
| 1090 | continue; |
| 1091 | } |
| 1092 | /* 255,255 - retain one 255 */ |
| 1093 | } |
| 1094 | /* NUL => '\n' */ |
| 1095 | cmd[dst++] = cmd[src] ? cmd[src] : '\n'; |
| 1096 | src++; |
| 1097 | } while (src < len); |
| 1098 | cmd[dst] = '\0'; |
| 1099 | } |
| 1100 | } |
Denis Vlasenko | edb0de4 | 2009-03-17 12:23:24 +0000 | [diff] [blame] | 1101 | |
| 1102 | if (G.verbose > 1) |
Denis Vlasenko | 1a82555 | 2009-03-17 12:40:34 +0000 | [diff] [blame] | 1103 | verbose_log(cmd); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 1104 | |
| 1105 | G.ftp_arg = strchr(cmd, ' '); |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 1106 | if (G.ftp_arg != NULL) |
| 1107 | *G.ftp_arg++ = '\0'; |
| 1108 | |
| 1109 | /* Uppercase and pack into uint32_t first word of the command */ |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 1110 | cmdval = 0; |
| 1111 | while (*cmd) |
| 1112 | cmdval = (cmdval << 8) + ((unsigned char)*cmd++ & (unsigned char)~0x20); |
| 1113 | |
| 1114 | return cmdval; |
| 1115 | } |
| 1116 | |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 1117 | #define mk_const4(a,b,c,d) (((a * 0x100 + b) * 0x100 + c) * 0x100 + d) |
| 1118 | #define mk_const3(a,b,c) ((a * 0x100 + b) * 0x100 + c) |
| 1119 | enum { |
| 1120 | const_ALLO = mk_const4('A', 'L', 'L', 'O'), |
| 1121 | const_APPE = mk_const4('A', 'P', 'P', 'E'), |
| 1122 | const_CDUP = mk_const4('C', 'D', 'U', 'P'), |
| 1123 | const_CWD = mk_const3('C', 'W', 'D'), |
| 1124 | const_DELE = mk_const4('D', 'E', 'L', 'E'), |
| 1125 | const_EPSV = mk_const4('E', 'P', 'S', 'V'), |
Denis Vlasenko | 1a82555 | 2009-03-17 12:40:34 +0000 | [diff] [blame] | 1126 | const_FEAT = mk_const4('F', 'E', 'A', 'T'), |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 1127 | const_HELP = mk_const4('H', 'E', 'L', 'P'), |
| 1128 | const_LIST = mk_const4('L', 'I', 'S', 'T'), |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 1129 | const_MDTM = mk_const4('M', 'D', 'T', 'M'), |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 1130 | const_MKD = mk_const3('M', 'K', 'D'), |
| 1131 | const_MODE = mk_const4('M', 'O', 'D', 'E'), |
| 1132 | const_NLST = mk_const4('N', 'L', 'S', 'T'), |
| 1133 | const_NOOP = mk_const4('N', 'O', 'O', 'P'), |
| 1134 | const_PASS = mk_const4('P', 'A', 'S', 'S'), |
| 1135 | const_PASV = mk_const4('P', 'A', 'S', 'V'), |
| 1136 | const_PORT = mk_const4('P', 'O', 'R', 'T'), |
| 1137 | const_PWD = mk_const3('P', 'W', 'D'), |
Denys Vlasenko | de3cae1 | 2014-06-25 16:23:59 +0200 | [diff] [blame] | 1138 | /* Same as PWD. Reportedly used by windows ftp client */ |
| 1139 | const_XPWD = mk_const4('X', 'P', 'W', 'D'), |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 1140 | const_QUIT = mk_const4('Q', 'U', 'I', 'T'), |
| 1141 | const_REST = mk_const4('R', 'E', 'S', 'T'), |
| 1142 | const_RETR = mk_const4('R', 'E', 'T', 'R'), |
| 1143 | const_RMD = mk_const3('R', 'M', 'D'), |
| 1144 | const_RNFR = mk_const4('R', 'N', 'F', 'R'), |
| 1145 | const_RNTO = mk_const4('R', 'N', 'T', 'O'), |
| 1146 | const_SIZE = mk_const4('S', 'I', 'Z', 'E'), |
| 1147 | const_STAT = mk_const4('S', 'T', 'A', 'T'), |
| 1148 | const_STOR = mk_const4('S', 'T', 'O', 'R'), |
| 1149 | const_STOU = mk_const4('S', 'T', 'O', 'U'), |
| 1150 | const_STRU = mk_const4('S', 'T', 'R', 'U'), |
| 1151 | const_SYST = mk_const4('S', 'Y', 'S', 'T'), |
| 1152 | const_TYPE = mk_const4('T', 'Y', 'P', 'E'), |
| 1153 | const_USER = mk_const4('U', 'S', 'E', 'R'), |
| 1154 | |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 1155 | #if !BB_MMU |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 1156 | OPT_l = (1 << 0), |
| 1157 | OPT_1 = (1 << 1), |
Denys Vlasenko | 7666fa1 | 2014-09-15 23:35:58 +0200 | [diff] [blame] | 1158 | OPT_A = (1 << 2), |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 1159 | #endif |
Denys Vlasenko | 7666fa1 | 2014-09-15 23:35:58 +0200 | [diff] [blame] | 1160 | OPT_v = (1 << ((!BB_MMU) * 3 + 0)), |
| 1161 | OPT_S = (1 << ((!BB_MMU) * 3 + 1)), |
Denys Vlasenko | 3148e0c | 2016-11-23 09:07:44 +0100 | [diff] [blame] | 1162 | OPT_w = (1 << ((!BB_MMU) * 3 + 2)) * ENABLE_FEATURE_FTPD_WRITE, |
Denis Vlasenko | f2160b6 | 2009-03-16 14:53:54 +0000 | [diff] [blame] | 1163 | }; |
| 1164 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1165 | int ftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 1166 | int ftpd_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1167 | { |
Denys Vlasenko | 3148e0c | 2016-11-23 09:07:44 +0100 | [diff] [blame] | 1168 | #if ENABLE_FEATURE_FTPD_AUTHENTICATION |
Denys Vlasenko | 9472e8a | 2015-03-31 21:46:26 +0200 | [diff] [blame] | 1169 | struct passwd *pw = NULL; |
Andrey Mozzhuhin | 2181fb4 | 2017-01-24 23:02:04 +0100 | [diff] [blame] | 1170 | char *anon_opt = NULL; |
Denys Vlasenko | 9472e8a | 2015-03-31 21:46:26 +0200 | [diff] [blame] | 1171 | #endif |
Denis Vlasenko | e6c94a6 | 2009-03-17 05:11:51 +0000 | [diff] [blame] | 1172 | unsigned abs_timeout; |
Denys Vlasenko | 035b4d7 | 2009-06-05 20:42:40 +0200 | [diff] [blame] | 1173 | unsigned verbose_S; |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 1174 | smallint opts; |
| 1175 | |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 1176 | INIT_G(); |
| 1177 | |
Denis Vlasenko | e6c94a6 | 2009-03-17 05:11:51 +0000 | [diff] [blame] | 1178 | abs_timeout = 1 * 60 * 60; |
Denys Vlasenko | 035b4d7 | 2009-06-05 20:42:40 +0200 | [diff] [blame] | 1179 | verbose_S = 0; |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 1180 | G.timeout = 2 * 60; |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 1181 | #if BB_MMU |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 1182 | opts = getopt32(argv, "^" "vS" |
| 1183 | IF_FEATURE_FTPD_WRITE("w") "t:+T:+" IF_FEATURE_FTPD_AUTHENTICATION("a:") |
| 1184 | "\0" "vv:SS", |
Andrey Mozzhuhin | 2181fb4 | 2017-01-24 23:02:04 +0100 | [diff] [blame] | 1185 | &G.timeout, &abs_timeout, IF_FEATURE_FTPD_AUTHENTICATION(&anon_opt,) |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 1186 | &G.verbose, &verbose_S |
| 1187 | ); |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 1188 | #else |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 1189 | opts = getopt32(argv, "^" "l1AvS" |
| 1190 | IF_FEATURE_FTPD_WRITE("w") "t:+T:+" IF_FEATURE_FTPD_AUTHENTICATION("a:") |
| 1191 | "\0" "vv:SS", |
Andrey Mozzhuhin | 2181fb4 | 2017-01-24 23:02:04 +0100 | [diff] [blame] | 1192 | &G.timeout, &abs_timeout, IF_FEATURE_FTPD_AUTHENTICATION(&anon_opt,) |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 1193 | &G.verbose, &verbose_S |
| 1194 | ); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 1195 | if (opts & (OPT_l|OPT_1)) { |
| 1196 | /* Our secret backdoor to ls */ |
Denys Vlasenko | a6ae999 | 2014-06-27 12:24:39 +0200 | [diff] [blame] | 1197 | if (fchdir(3) != 0) |
| 1198 | _exit(127); |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 1199 | /* memset(&G, 0, sizeof(G)); - ls_main does it */ |
Denys Vlasenko | b13b618 | 2017-01-25 04:52:45 +0100 | [diff] [blame] | 1200 | return ls_main(/*argc_unused*/ 0, argv); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 1201 | } |
Denis Vlasenko | 5b492ee | 2009-03-18 14:12:22 +0000 | [diff] [blame] | 1202 | #endif |
Denys Vlasenko | 035b4d7 | 2009-06-05 20:42:40 +0200 | [diff] [blame] | 1203 | if (G.verbose < verbose_S) |
| 1204 | G.verbose = verbose_S; |
Denis Vlasenko | edb0de4 | 2009-03-17 12:23:24 +0000 | [diff] [blame] | 1205 | if (abs_timeout | G.timeout) { |
| 1206 | if (abs_timeout == 0) |
| 1207 | abs_timeout = INT_MAX; |
| 1208 | G.end_time = monotonic_sec() + abs_timeout; |
| 1209 | if (G.timeout > abs_timeout) |
| 1210 | G.timeout = abs_timeout; |
| 1211 | } |
Denis Vlasenko | e6c94a6 | 2009-03-17 05:11:51 +0000 | [diff] [blame] | 1212 | strcpy(G.msg_ok + 4, MSG_OK ); |
| 1213 | strcpy(G.msg_err + 4, MSG_ERR); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1214 | |
| 1215 | G.local_addr = get_sock_lsa(STDIN_FILENO); |
| 1216 | if (!G.local_addr) { |
| 1217 | /* This is confusing: |
| 1218 | * bb_error_msg_and_die("stdin is not a socket"); |
| 1219 | * Better: */ |
| 1220 | bb_show_usage(); |
| 1221 | /* Help text says that ftpd must be used as inetd service, |
| 1222 | * which is by far the most usual cause of get_sock_lsa |
| 1223 | * failure */ |
| 1224 | } |
| 1225 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 1226 | if (!(opts & OPT_v)) |
Denis Vlasenko | 4221e90 | 2009-03-11 15:07:44 +0000 | [diff] [blame] | 1227 | logmode = LOGMODE_NONE; |
| 1228 | if (opts & OPT_S) { |
| 1229 | /* LOG_NDELAY is needed since we may chroot later */ |
| 1230 | openlog(applet_name, LOG_PID | LOG_NDELAY, LOG_DAEMON); |
| 1231 | logmode |= LOGMODE_SYSLOG; |
| 1232 | } |
Denis Vlasenko | edb0de4 | 2009-03-17 12:23:24 +0000 | [diff] [blame] | 1233 | if (logmode) |
| 1234 | applet_name = xasprintf("%s[%u]", applet_name, (int)getpid()); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1235 | |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1236 | //umask(077); - admin can set umask before starting us |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1237 | |
Denys Vlasenko | 64b7449 | 2015-01-26 15:45:48 +0100 | [diff] [blame] | 1238 | /* Signals */ |
| 1239 | bb_signals(0 |
| 1240 | /* We'll always take EPIPE rather than a rude signal, thanks */ |
| 1241 | + (1 << SIGPIPE) |
| 1242 | /* LIST command spawns chilren. Prevent zombies */ |
| 1243 | + (1 << SIGCHLD) |
| 1244 | , SIG_IGN); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1245 | |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1246 | /* Set up options on the command socket (do we need these all? why?) */ |
Denys Vlasenko | c52cbea | 2015-08-24 19:48:03 +0200 | [diff] [blame] | 1247 | setsockopt_1(STDIN_FILENO, IPPROTO_TCP, TCP_NODELAY); |
| 1248 | setsockopt_keepalive(STDIN_FILENO); |
Denis Vlasenko | fce4a94 | 2009-03-18 16:02:54 +0000 | [diff] [blame] | 1249 | /* Telnet protocol over command link may send "urgent" data, |
| 1250 | * we prefer it to be received in the "normal" data stream: */ |
Denys Vlasenko | c52cbea | 2015-08-24 19:48:03 +0200 | [diff] [blame] | 1251 | setsockopt_1(STDIN_FILENO, SOL_SOCKET, SO_OOBINLINE); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1252 | |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 1253 | WRITE_OK(FTP_GREET); |
Denis Vlasenko | 20c8216 | 2009-03-16 16:19:53 +0000 | [diff] [blame] | 1254 | signal(SIGALRM, timeout_handler); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1255 | |
Denys Vlasenko | 3148e0c | 2016-11-23 09:07:44 +0100 | [diff] [blame] | 1256 | #if ENABLE_FEATURE_FTPD_AUTHENTICATION |
Denys Vlasenko | 9472e8a | 2015-03-31 21:46:26 +0200 | [diff] [blame] | 1257 | while (1) { |
| 1258 | uint32_t cmdval = cmdio_get_cmd_and_arg(); |
Andrey Mozzhuhin | 2181fb4 | 2017-01-24 23:02:04 +0100 | [diff] [blame] | 1259 | if (cmdval == const_USER) { |
| 1260 | if (anon_opt && strcmp(G.ftp_arg, "anonymous") == 0) { |
| 1261 | pw = getpwnam(anon_opt); |
| 1262 | if (pw) |
| 1263 | break; /* does not even ask for password */ |
| 1264 | } |
Denys Vlasenko | 9472e8a | 2015-03-31 21:46:26 +0200 | [diff] [blame] | 1265 | pw = getpwnam(G.ftp_arg); |
| 1266 | cmdio_write_raw(STR(FTP_GIVEPWORD)" Please specify password\r\n"); |
| 1267 | } else if (cmdval == const_PASS) { |
| 1268 | if (check_password(pw, G.ftp_arg) > 0) { |
| 1269 | break; /* login success */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1270 | } |
Denys Vlasenko | 9472e8a | 2015-03-31 21:46:26 +0200 | [diff] [blame] | 1271 | cmdio_write_raw(STR(FTP_LOGINERR)" Login failed\r\n"); |
| 1272 | pw = NULL; |
| 1273 | } else if (cmdval == const_QUIT) { |
| 1274 | WRITE_OK(FTP_GOODBYE); |
| 1275 | return 0; |
| 1276 | } else { |
| 1277 | cmdio_write_raw(STR(FTP_LOGINERR)" Login with USER and PASS\r\n"); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1278 | } |
| 1279 | } |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 1280 | WRITE_OK(FTP_LOGINOK); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1281 | #endif |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1282 | |
Morten Kvistgaard | feac9b6 | 2014-08-05 21:57:18 +0200 | [diff] [blame] | 1283 | /* Do this after auth, else /etc/passwd is not accessible */ |
| 1284 | #if !BB_MMU |
| 1285 | G.root_fd = -1; |
| 1286 | #endif |
| 1287 | argv += optind; |
| 1288 | if (argv[0]) { |
Denys Vlasenko | f7ad927 | 2015-10-13 13:49:53 +0200 | [diff] [blame] | 1289 | const char *basedir = argv[0]; |
Morten Kvistgaard | feac9b6 | 2014-08-05 21:57:18 +0200 | [diff] [blame] | 1290 | #if !BB_MMU |
| 1291 | G.root_fd = xopen("/", O_RDONLY | O_DIRECTORY); |
| 1292 | close_on_exec_on(G.root_fd); |
| 1293 | #endif |
Denys Vlasenko | f7ad927 | 2015-10-13 13:49:53 +0200 | [diff] [blame] | 1294 | if (chroot(basedir) == 0) |
| 1295 | basedir = "/"; |
| 1296 | #if !BB_MMU |
| 1297 | else { |
| 1298 | close(G.root_fd); |
| 1299 | G.root_fd = -1; |
| 1300 | } |
| 1301 | #endif |
| 1302 | /* |
| 1303 | * If chroot failed, assume that we aren't root, |
| 1304 | * and at least chdir to the specified DIR |
| 1305 | * (older versions were dying with error message). |
| 1306 | * If chroot worked, move current dir to new "/": |
| 1307 | */ |
| 1308 | xchdir(basedir); |
Morten Kvistgaard | feac9b6 | 2014-08-05 21:57:18 +0200 | [diff] [blame] | 1309 | } |
| 1310 | |
Denys Vlasenko | 3148e0c | 2016-11-23 09:07:44 +0100 | [diff] [blame] | 1311 | #if ENABLE_FEATURE_FTPD_AUTHENTICATION |
Denys Vlasenko | 9472e8a | 2015-03-31 21:46:26 +0200 | [diff] [blame] | 1312 | change_identity(pw); |
| 1313 | #endif |
| 1314 | |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1315 | /* RFC-959 Section 5.1 |
| 1316 | * The following commands and options MUST be supported by every |
| 1317 | * server-FTP and user-FTP, except in cases where the underlying |
| 1318 | * file system or operating system does not allow or support |
| 1319 | * a particular command. |
| 1320 | * Type: ASCII Non-print, IMAGE, LOCAL 8 |
| 1321 | * Mode: Stream |
| 1322 | * Structure: File, Record* |
| 1323 | * (Record structure is REQUIRED only for hosts whose file |
| 1324 | * systems support record structure). |
| 1325 | * Commands: |
| 1326 | * USER, PASS, ACCT, [bbox: ACCT not supported] |
| 1327 | * PORT, PASV, |
| 1328 | * TYPE, MODE, STRU, |
| 1329 | * RETR, STOR, APPE, |
| 1330 | * RNFR, RNTO, DELE, |
| 1331 | * CWD, CDUP, RMD, MKD, PWD, |
| 1332 | * LIST, NLST, |
| 1333 | * SYST, STAT, |
| 1334 | * HELP, NOOP, QUIT. |
| 1335 | */ |
| 1336 | /* ACCOUNT (ACCT) |
Denis Vlasenko | 3455285 | 2009-03-09 04:18:00 +0000 | [diff] [blame] | 1337 | * "The argument field is a Telnet string identifying the user's account. |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1338 | * The command is not necessarily related to the USER command, as some |
| 1339 | * sites may require an account for login and others only for specific |
| 1340 | * access, such as storing files. In the latter case the command may |
| 1341 | * arrive at any time. |
| 1342 | * There are reply codes to differentiate these cases for the automation: |
| 1343 | * when account information is required for login, the response to |
| 1344 | * a successful PASSword command is reply code 332. On the other hand, |
| 1345 | * if account information is NOT required for login, the reply to |
| 1346 | * a successful PASSword command is 230; and if the account information |
| 1347 | * is needed for a command issued later in the dialogue, the server |
| 1348 | * should return a 332 or 532 reply depending on whether it stores |
| 1349 | * (pending receipt of the ACCounT command) or discards the command, |
Denis Vlasenko | 3455285 | 2009-03-09 04:18:00 +0000 | [diff] [blame] | 1350 | * respectively." |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1351 | */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1352 | |
| 1353 | while (1) { |
| 1354 | uint32_t cmdval = cmdio_get_cmd_and_arg(); |
| 1355 | |
| 1356 | if (cmdval == const_QUIT) { |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 1357 | WRITE_OK(FTP_GOODBYE); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1358 | return 0; |
| 1359 | } |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 1360 | else if (cmdval == const_USER) |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 1361 | /* This would mean "ok, now give me PASS". */ |
| 1362 | /*WRITE_OK(FTP_GIVEPWORD);*/ |
| 1363 | /* vsftpd can be configured to not require that, |
| 1364 | * and this also saves one roundtrip: |
| 1365 | */ |
| 1366 | WRITE_OK(FTP_LOGINOK); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 1367 | else if (cmdval == const_PASS) |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 1368 | WRITE_OK(FTP_LOGINOK); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 1369 | else if (cmdval == const_NOOP) |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 1370 | WRITE_OK(FTP_NOOPOK); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 1371 | else if (cmdval == const_TYPE) |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 1372 | WRITE_OK(FTP_TYPEOK); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 1373 | else if (cmdval == const_STRU) |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 1374 | WRITE_OK(FTP_STRUOK); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 1375 | else if (cmdval == const_MODE) |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 1376 | WRITE_OK(FTP_MODEOK); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 1377 | else if (cmdval == const_ALLO) |
Denis Vlasenko | fbf5846 | 2009-03-16 20:54:45 +0000 | [diff] [blame] | 1378 | WRITE_OK(FTP_ALLOOK); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 1379 | else if (cmdval == const_SYST) |
| 1380 | cmdio_write_raw(STR(FTP_SYSTOK)" UNIX Type: L8\r\n"); |
Denys Vlasenko | de3cae1 | 2014-06-25 16:23:59 +0200 | [diff] [blame] | 1381 | else if (cmdval == const_PWD || cmdval == const_XPWD) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1382 | handle_pwd(); |
| 1383 | else if (cmdval == const_CWD) |
| 1384 | handle_cwd(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1385 | else if (cmdval == const_CDUP) /* cd .. */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1386 | handle_cdup(); |
Denis Vlasenko | 1a82555 | 2009-03-17 12:40:34 +0000 | [diff] [blame] | 1387 | /* HELP is nearly useless, but we can reuse FEAT for it */ |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 1388 | /* lftp uses FEAT */ |
Denis Vlasenko | 1a82555 | 2009-03-17 12:40:34 +0000 | [diff] [blame] | 1389 | else if (cmdval == const_HELP || cmdval == const_FEAT) |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 1390 | handle_feat(cmdval == const_HELP |
| 1391 | ? STRNUM32(FTP_HELP) |
| 1392 | : STRNUM32(FTP_STATOK) |
| 1393 | ); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1394 | else if (cmdval == const_LIST) /* ls -l */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1395 | handle_list(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1396 | else if (cmdval == const_NLST) /* "name list", bare ls */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1397 | handle_nlst(); |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 1398 | /* SIZE is crucial for wget's download indicator etc */ |
| 1399 | /* Mozilla, lftp use MDTM (presumably for caching) */ |
| 1400 | else if (cmdval == const_SIZE || cmdval == const_MDTM) |
| 1401 | handle_size_or_mdtm(cmdval == const_SIZE); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1402 | else if (cmdval == const_STAT) { |
| 1403 | if (G.ftp_arg == NULL) |
| 1404 | handle_stat(); |
| 1405 | else |
| 1406 | handle_stat_file(); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 1407 | } |
| 1408 | else if (cmdval == const_PASV) |
| 1409 | handle_pasv(); |
| 1410 | else if (cmdval == const_EPSV) |
| 1411 | handle_epsv(); |
| 1412 | else if (cmdval == const_RETR) |
| 1413 | handle_retr(); |
| 1414 | else if (cmdval == const_PORT) |
| 1415 | handle_port(); |
| 1416 | else if (cmdval == const_REST) |
| 1417 | handle_rest(); |
Denys Vlasenko | 3148e0c | 2016-11-23 09:07:44 +0100 | [diff] [blame] | 1418 | #if ENABLE_FEATURE_FTPD_WRITE |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 1419 | else if (opts & OPT_w) { |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1420 | if (cmdval == const_STOR) |
| 1421 | handle_stor(); |
| 1422 | else if (cmdval == const_MKD) |
| 1423 | handle_mkd(); |
| 1424 | else if (cmdval == const_RMD) |
| 1425 | handle_rmd(); |
| 1426 | else if (cmdval == const_DELE) |
| 1427 | handle_dele(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1428 | else if (cmdval == const_RNFR) /* "rename from" */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1429 | handle_rnfr(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1430 | else if (cmdval == const_RNTO) /* "rename to" */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1431 | handle_rnto(); |
| 1432 | else if (cmdval == const_APPE) |
| 1433 | handle_appe(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1434 | else if (cmdval == const_STOU) /* "store unique" */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1435 | handle_stou(); |
Denys Vlasenko | 8507e1f | 2009-06-04 19:03:20 +0200 | [diff] [blame] | 1436 | else |
| 1437 | goto bad_cmd; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1438 | } |
| 1439 | #endif |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1440 | #if 0 |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1441 | else if (cmdval == const_STOR |
| 1442 | || cmdval == const_MKD |
| 1443 | || cmdval == const_RMD |
| 1444 | || cmdval == const_DELE |
| 1445 | || cmdval == const_RNFR |
| 1446 | || cmdval == const_RNTO |
| 1447 | || cmdval == const_APPE |
| 1448 | || cmdval == const_STOU |
| 1449 | ) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 1450 | cmdio_write_raw(STR(FTP_NOPERM)" Permission denied\r\n"); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1451 | } |
| 1452 | #endif |
| 1453 | else { |
Denis Vlasenko | 51c9bb1 | 2009-03-09 02:51:46 +0000 | [diff] [blame] | 1454 | /* Which unsupported commands were seen in the wild? |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1455 | * (doesn't necessarily mean "we must support them") |
Denis Vlasenko | 1432cb4 | 2009-03-18 00:45:00 +0000 | [diff] [blame] | 1456 | * foo 1.2.3: XXXX - comment |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1457 | */ |
Denys Vlasenko | 3148e0c | 2016-11-23 09:07:44 +0100 | [diff] [blame] | 1458 | #if ENABLE_FEATURE_FTPD_WRITE |
Denys Vlasenko | 8507e1f | 2009-06-04 19:03:20 +0200 | [diff] [blame] | 1459 | bad_cmd: |
| 1460 | #endif |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 1461 | cmdio_write_raw(STR(FTP_BADCMD)" Unknown command\r\n"); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1462 | } |
| 1463 | } |
| 1464 | } |