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