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