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