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