Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Simple FTP daemon, based on vsftpd 2.0.7 (written by Chris Evans) |
| 4 | * |
| 5 | * Author: Adam Tkac <vonsch@gmail.com> |
| 6 | * |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 8 | * |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 9 | * Only subset of FTP protocol is implemented but vast majority of clients |
| 10 | * should not have any problem. You have to run this daemon via inetd. |
| 11 | * |
| 12 | * Options: |
| 13 | * -w - enable FTP write commands |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 14 | * |
| 15 | * TODO: implement "421 Timeout" thingy (alarm(60) while waiting for a cmd). |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include "libbb.h" |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 19 | #include <syslog.h> |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 20 | #include <netinet/tcp.h> |
| 21 | |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 22 | #define FTP_DATACONN 150 |
| 23 | #define FTP_NOOPOK 200 |
| 24 | #define FTP_TYPEOK 200 |
| 25 | #define FTP_PORTOK 200 |
| 26 | #define FTP_STRUOK 200 |
| 27 | #define FTP_MODEOK 200 |
| 28 | #define FTP_ALLOOK 202 |
| 29 | #define FTP_STATOK 211 |
| 30 | #define FTP_STATFILE_OK 213 |
| 31 | #define FTP_HELP 214 |
| 32 | #define FTP_SYSTOK 215 |
| 33 | #define FTP_GREET 220 |
| 34 | #define FTP_GOODBYE 221 |
| 35 | #define FTP_TRANSFEROK 226 |
| 36 | #define FTP_PASVOK 227 |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 37 | /*#define FTP_EPRTOK 228*/ |
| 38 | #define FTP_EPSVOK 229 |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 39 | #define FTP_LOGINOK 230 |
| 40 | #define FTP_CWDOK 250 |
| 41 | #define FTP_RMDIROK 250 |
| 42 | #define FTP_DELEOK 250 |
| 43 | #define FTP_RENAMEOK 250 |
| 44 | #define FTP_PWDOK 257 |
| 45 | #define FTP_MKDIROK 257 |
| 46 | #define FTP_GIVEPWORD 331 |
| 47 | #define FTP_RESTOK 350 |
| 48 | #define FTP_RNFROK 350 |
| 49 | #define FTP_BADSENDCONN 425 |
| 50 | #define FTP_BADSENDNET 426 |
| 51 | #define FTP_BADSENDFILE 451 |
| 52 | #define FTP_BADCMD 500 |
| 53 | #define FTP_COMMANDNOTIMPL 502 |
| 54 | #define FTP_NEEDUSER 503 |
| 55 | #define FTP_NEEDRNFR 503 |
| 56 | #define FTP_BADSTRU 504 |
| 57 | #define FTP_BADMODE 504 |
| 58 | #define FTP_LOGINERR 530 |
| 59 | #define FTP_FILEFAIL 550 |
| 60 | #define FTP_NOPERM 550 |
| 61 | #define FTP_UPLOADFAIL 553 |
| 62 | |
| 63 | #define STR1(s) #s |
| 64 | #define STR(s) STR1(s) |
| 65 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 66 | /* Convert a constant to 3-digit string, packed into uint32_t */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 67 | enum { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 68 | /* Shift for Nth decimal digit */ |
| 69 | SHIFT0 = 16 * BB_LITTLE_ENDIAN + 8 * BB_BIG_ENDIAN, |
| 70 | SHIFT1 = 8 * BB_LITTLE_ENDIAN + 16 * BB_BIG_ENDIAN, |
| 71 | SHIFT2 = 0 * BB_LITTLE_ENDIAN + 24 * BB_BIG_ENDIAN, |
| 72 | }; |
| 73 | #define STRNUM32(s) (uint32_t)(0 \ |
| 74 | | (('0' + ((s) / 1 % 10)) << SHIFT0) \ |
| 75 | | (('0' + ((s) / 10 % 10)) << SHIFT1) \ |
| 76 | | (('0' + ((s) / 100 % 10)) << SHIFT2) \ |
| 77 | ) |
| 78 | |
| 79 | enum { |
| 80 | OPT_l = (1 << 0), |
| 81 | OPT_1 = (1 << 1), |
| 82 | OPT_v = (1 << 2), |
| 83 | OPT_w = (1 << 3), |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 84 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 85 | #define mk_const4(a,b,c,d) (((a * 0x100 + b) * 0x100 + c) * 0x100 + d) |
| 86 | #define mk_const3(a,b,c) ((a * 0x100 + b) * 0x100 + c) |
| 87 | const_ALLO = mk_const4('A', 'L', 'L', 'O'), |
| 88 | const_APPE = mk_const4('A', 'P', 'P', 'E'), |
| 89 | const_CDUP = mk_const4('C', 'D', 'U', 'P'), |
| 90 | const_CWD = mk_const3('C', 'W', 'D'), |
| 91 | const_DELE = mk_const4('D', 'E', 'L', 'E'), |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 92 | const_EPSV = mk_const4('E', 'P', 'S', 'V'), |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 93 | const_HELP = mk_const4('H', 'E', 'L', 'P'), |
| 94 | const_LIST = mk_const4('L', 'I', 'S', 'T'), |
| 95 | const_MKD = mk_const3('M', 'K', 'D'), |
| 96 | const_MODE = mk_const4('M', 'O', 'D', 'E'), |
| 97 | const_NLST = mk_const4('N', 'L', 'S', 'T'), |
| 98 | const_NOOP = mk_const4('N', 'O', 'O', 'P'), |
| 99 | const_PASS = mk_const4('P', 'A', 'S', 'S'), |
| 100 | const_PASV = mk_const4('P', 'A', 'S', 'V'), |
| 101 | const_PORT = mk_const4('P', 'O', 'R', 'T'), |
| 102 | const_PWD = mk_const3('P', 'W', 'D'), |
| 103 | const_QUIT = mk_const4('Q', 'U', 'I', 'T'), |
| 104 | const_REST = mk_const4('R', 'E', 'S', 'T'), |
| 105 | const_RETR = mk_const4('R', 'E', 'T', 'R'), |
| 106 | const_RMD = mk_const3('R', 'M', 'D'), |
| 107 | const_RNFR = mk_const4('R', 'N', 'F', 'R'), |
| 108 | const_RNTO = mk_const4('R', 'N', 'T', 'O'), |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 109 | const_SIZE = mk_const4('S', 'I', 'Z', 'E'), |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 110 | const_STAT = mk_const4('S', 'T', 'A', 'T'), |
| 111 | const_STOR = mk_const4('S', 'T', 'O', 'R'), |
| 112 | const_STOU = mk_const4('S', 'T', 'O', 'U'), |
| 113 | const_STRU = mk_const4('S', 'T', 'R', 'U'), |
| 114 | const_SYST = mk_const4('S', 'Y', 'S', 'T'), |
| 115 | const_TYPE = mk_const4('T', 'Y', 'P', 'E'), |
| 116 | const_USER = mk_const4('U', 'S', 'E', 'R'), |
| 117 | }; |
| 118 | |
| 119 | struct globals { |
| 120 | char *p_control_line_buf; |
| 121 | len_and_sockaddr *local_addr; |
| 122 | len_and_sockaddr *port_addr; |
| 123 | int pasv_listen_fd; |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 124 | int proc_self_fd; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 125 | off_t restart_pos; |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 126 | char *ftp_cmd; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 127 | char *ftp_arg; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 128 | #if ENABLE_FEATURE_FTP_WRITE |
| 129 | char *rnfr_filename; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 130 | #endif |
| 131 | }; |
| 132 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 133 | #define INIT_G() do { } while (0) |
| 134 | |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 135 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 136 | static char * |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 137 | escape_text(const char *prepend, const char *str, unsigned escapee) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 138 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 139 | unsigned retlen, remainlen, chunklen; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 140 | char *ret, *found; |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 141 | char append; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 142 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 143 | append = (char)escapee; |
| 144 | escapee >>= 8; |
| 145 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 146 | remainlen = strlen(str); |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 147 | retlen = strlen(prepend); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 148 | ret = xmalloc(retlen + remainlen * 2 + 1 + 1); |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 149 | strcpy(ret, prepend); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 150 | |
| 151 | for (;;) { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 152 | found = strchrnul(str, escapee); |
| 153 | chunklen = found - str + 1; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 154 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 155 | /* Copy chunk up to and including escapee (or NUL) to ret */ |
| 156 | memcpy(ret + retlen, str, chunklen); |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 157 | retlen += chunklen; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 158 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 159 | if (*found == '\0') { |
| 160 | /* It wasn't escapee, it was NUL! */ |
| 161 | ret[retlen - 1] = append; /* replace NUL */ |
| 162 | ret[retlen] = '\0'; /* add NUL */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 163 | break; |
| 164 | } |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 165 | ret[retlen++] = escapee; /* duplicate escapee */ |
| 166 | str = found + 1; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 167 | } |
| 168 | return ret; |
| 169 | } |
| 170 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 171 | /* Returns strlen as a bonus */ |
| 172 | static unsigned |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 173 | replace_char(char *str, char from, char to) |
| 174 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 175 | char *p = str; |
| 176 | while (*p) { |
| 177 | if (*p == from) |
| 178 | *p = to; |
| 179 | p++; |
| 180 | } |
| 181 | return p - str; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static void |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 185 | cmdio_write(uint32_t status_str, const char *str) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 186 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 187 | union { |
| 188 | char buf[4]; |
| 189 | uint32_t v32; |
| 190 | } u; |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 191 | char *response; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 192 | int len; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 193 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 194 | u.v32 = status_str; |
| 195 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 196 | /* FTP allegedly uses telnet protocol for command link. |
| 197 | * In telnet, 0xff is an escape char, and needs to be escaped: */ |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 198 | response = escape_text(u.buf, str, (0xff << 8) + '\r'); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 199 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 200 | /* ?! does FTP send embedded LFs as NULs? wow */ |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 201 | len = replace_char(response, '\n', '\0'); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 202 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 203 | response[len++] = '\n'; /* tack on trailing '\n' */ |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 204 | xwrite(STDOUT_FILENO, response, len); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 205 | free(response); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static void |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 209 | cmdio_write_ok(int status) |
| 210 | { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 211 | fdprintf(STDOUT_FILENO, "%u Operation successful\r\n", status); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 214 | /* TODO: output strerr(errno) if errno != 0? */ |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 215 | static void |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 216 | cmdio_write_error(int status) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 217 | { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 218 | fdprintf(STDOUT_FILENO, "%u Error\r\n", status); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static void |
| 222 | cmdio_write_raw(const char *p_text) |
| 223 | { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 224 | xwrite_str(STDOUT_FILENO, p_text); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 227 | /* Simple commands */ |
| 228 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 229 | static void |
| 230 | handle_pwd(void) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 231 | { |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 232 | char *cwd, *response; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 233 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 234 | cwd = xrealloc_getcwd_or_warn(NULL); |
| 235 | if (cwd == NULL) |
| 236 | cwd = xstrdup(""); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 237 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 238 | /* We have to promote each " to "" */ |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 239 | response = escape_text(" \"", cwd, ('"' << 8) + '"'); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 240 | free(cwd); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 241 | cmdio_write(STRNUM32(FTP_PWDOK), response); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 242 | free(response); |
| 243 | } |
| 244 | |
| 245 | static void |
| 246 | handle_cwd(void) |
| 247 | { |
| 248 | if (!G.ftp_arg || chdir(G.ftp_arg) != 0) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 249 | cmdio_write_error(FTP_FILEFAIL); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 250 | return; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 251 | } |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 252 | cmdio_write_ok(FTP_CWDOK); |
| 253 | } |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 254 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 255 | static void |
| 256 | handle_cdup(void) |
| 257 | { |
| 258 | G.ftp_arg = (char*)".."; |
| 259 | handle_cwd(); |
| 260 | } |
| 261 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 262 | static void |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 263 | handle_stat(void) |
| 264 | { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 265 | cmdio_write_raw(STR(FTP_STATOK)"-FTP server status:\r\n" |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 266 | " TYPE: BINARY\r\n" |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 267 | STR(FTP_STATOK)" Ok\r\n"); |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static void |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 271 | handle_help(void) |
| 272 | { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 273 | cmdio_write_raw(STR(FTP_HELP)"-Commands:\r\n" |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 274 | " ALLO CDUP CWD EPSV HELP LIST\r\n" |
| 275 | " MODE NLST NOOP PASS PASV PORT PWD QUIT\r\n" |
| 276 | " REST RETR SIZE STAT STRU SYST TYPE USER\r\n" |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 277 | #if ENABLE_FEATURE_FTP_WRITE |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 278 | " APPE DELE MKD RMD RNFR RNTO STOR STOU\r\n" |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 279 | #endif |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 280 | STR(FTP_HELP)" Ok\r\n"); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 283 | /* Download commands */ |
| 284 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 285 | static int |
| 286 | ftpdataio_get_pasv_fd(void) |
| 287 | { |
| 288 | int remote_fd; |
| 289 | |
| 290 | remote_fd = accept(G.pasv_listen_fd, NULL, 0); |
| 291 | |
| 292 | if (remote_fd < 0) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 293 | cmdio_write_error(FTP_BADSENDCONN); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 294 | return remote_fd; |
| 295 | } |
| 296 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 297 | setsockopt(remote_fd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 298 | return remote_fd; |
| 299 | } |
| 300 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 301 | static inline int |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 302 | port_active(void) |
| 303 | { |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 304 | return (G.port_addr != NULL); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 307 | static inline int |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 308 | pasv_active(void) |
| 309 | { |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 310 | return (G.pasv_listen_fd > STDOUT_FILENO); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static int |
| 314 | get_remote_transfer_fd(const char *p_status_msg) |
| 315 | { |
| 316 | int remote_fd; |
| 317 | |
| 318 | if (pasv_active()) |
| 319 | remote_fd = ftpdataio_get_pasv_fd(); |
| 320 | else |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 321 | remote_fd = xconnect_stream(G.port_addr); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 322 | |
| 323 | if (remote_fd < 0) |
| 324 | return remote_fd; |
| 325 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 326 | cmdio_write(STRNUM32(FTP_DATACONN), p_status_msg); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 327 | return remote_fd; |
| 328 | } |
| 329 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 330 | static int |
| 331 | data_transfer_checks_ok(void) |
| 332 | { |
| 333 | if (!pasv_active() && !port_active()) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 334 | cmdio_write_raw(STR(FTP_BADSENDCONN)" Use PORT or PASV first\r\n"); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | return 1; |
| 339 | } |
| 340 | |
| 341 | static void |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 342 | port_pasv_cleanup(void) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 343 | { |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 344 | free(G.port_addr); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 345 | G.port_addr = NULL; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 346 | if (G.pasv_listen_fd > STDOUT_FILENO) |
| 347 | close(G.pasv_listen_fd); |
| 348 | G.pasv_listen_fd = -1; |
| 349 | } |
| 350 | |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 351 | static unsigned |
| 352 | bind_for_passive_mode(void) |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 353 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 354 | int fd; |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 355 | unsigned port; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 356 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 357 | port_pasv_cleanup(); |
| 358 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 359 | G.pasv_listen_fd = fd = xsocket(G.local_addr->u.sa.sa_family, SOCK_STREAM, 0); |
| 360 | setsockopt_reuseaddr(fd); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 361 | |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 362 | set_nport(G.local_addr, 0); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 363 | xbind(fd, &G.local_addr->u.sa, G.local_addr->len); |
| 364 | xlisten(fd, 1); |
| 365 | getsockname(fd, &G.local_addr->u.sa, &G.local_addr->len); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 366 | |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 367 | port = get_nport(&G.local_addr->u.sa); |
| 368 | port = ntohs(port); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 369 | return port; |
| 370 | } |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 371 | |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 372 | static void |
| 373 | handle_pasv(void) |
| 374 | { |
| 375 | unsigned port; |
| 376 | char *addr, *response; |
| 377 | |
| 378 | port = bind_for_passive_mode(); |
| 379 | |
| 380 | if (G.local_addr->u.sa.sa_family == AF_INET) |
| 381 | addr = xmalloc_sockaddr2dotted_noport(&G.local_addr->u.sa); |
| 382 | else /* seen this in the wild done by other ftp servers: */ |
| 383 | addr = xstrdup("0.0.0.0"); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 384 | replace_char(addr, '.', ','); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 385 | |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 386 | response = xasprintf(STR(FTP_PASVOK)" Entering Passive Mode (%s,%u,%u)\r\n", |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 387 | addr, (int)(port >> 8), (int)(port & 255)); |
| 388 | free(addr); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 389 | cmdio_write_raw(response); |
| 390 | free(response); |
| 391 | } |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 392 | |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 393 | static void |
| 394 | handle_epsv(void) |
| 395 | { |
| 396 | unsigned port; |
| 397 | char *response; |
| 398 | |
| 399 | port = bind_for_passive_mode(); |
| 400 | response = xasprintf(STR(FTP_EPSVOK)" EPSV Ok (|||%u|)\r\n", port); |
| 401 | cmdio_write_raw(response); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 402 | free(response); |
| 403 | } |
| 404 | |
| 405 | static void |
| 406 | handle_port(void) |
| 407 | { |
| 408 | unsigned short port; |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 409 | char *raw, *port_part; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 410 | len_and_sockaddr *lsa = NULL; |
| 411 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 412 | port_pasv_cleanup(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 413 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 414 | raw = G.ftp_arg; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 415 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 416 | /* buglets: |
| 417 | * xatou16 will accept wrong input, |
| 418 | * xatou16 will exit instead of generating error to peer |
| 419 | */ |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 420 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 421 | port_part = strrchr(raw, ','); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 422 | if (port_part == NULL) |
| 423 | goto bail; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 424 | port = xatou16(&port_part[1]); |
| 425 | *port_part = '\0'; |
| 426 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 427 | port_part = strrchr(raw, ','); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 428 | if (port_part == NULL) |
| 429 | goto bail; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 430 | port |= xatou16(&port_part[1]) << 8; |
| 431 | *port_part = '\0'; |
| 432 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 433 | replace_char(raw, ',', '.'); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 434 | lsa = xdotted2sockaddr(raw, port); |
| 435 | |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 436 | if (lsa == NULL) { |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 437 | bail: |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 438 | cmdio_write_error(FTP_BADCMD); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
| 442 | G.port_addr = lsa; |
| 443 | cmdio_write_ok(FTP_PORTOK); |
| 444 | } |
| 445 | |
| 446 | static void |
| 447 | handle_rest(void) |
| 448 | { |
| 449 | /* When ftp_arg == NULL simply restart from beginning */ |
| 450 | G.restart_pos = G.ftp_arg ? xatoi_u(G.ftp_arg) : 0; |
| 451 | cmdio_write_ok(FTP_RESTOK); |
| 452 | } |
| 453 | |
| 454 | static void |
| 455 | handle_retr(void) |
| 456 | { |
| 457 | struct stat statbuf; |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 458 | int trans_ret; |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 459 | int remote_fd; |
| 460 | int opened_file; |
| 461 | off_t offset = G.restart_pos; |
| 462 | char *response; |
| 463 | |
| 464 | G.restart_pos = 0; |
| 465 | |
| 466 | if (!data_transfer_checks_ok()) |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 467 | return; /* data_transfer_checks_ok emitted error response */ |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 468 | |
| 469 | /* O_NONBLOCK is useful if file happens to be a device node */ |
| 470 | opened_file = G.ftp_arg ? open(G.ftp_arg, O_RDONLY | O_NONBLOCK) : -1; |
| 471 | if (opened_file < 0) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 472 | cmdio_write_error(FTP_FILEFAIL); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 476 | if (fstat(opened_file, &statbuf) != 0 || !S_ISREG(statbuf.st_mode)) { |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 477 | /* Note - pretend open failed */ |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 478 | cmdio_write_error(FTP_FILEFAIL); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 479 | goto file_close_out; |
| 480 | } |
| 481 | |
| 482 | /* Now deactive O_NONBLOCK, otherwise we have a problem on DMAPI filesystems |
| 483 | * such as XFS DMAPI. |
| 484 | */ |
| 485 | ndelay_off(opened_file); |
| 486 | |
| 487 | /* Set the download offset (from REST) if any */ |
| 488 | if (offset != 0) |
| 489 | xlseek(opened_file, offset, SEEK_SET); |
| 490 | |
| 491 | response = xasprintf( |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 492 | " Opening BINARY mode data connection for %s (%"OFF_FMT"u bytes)", |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 493 | G.ftp_arg, statbuf.st_size); |
| 494 | remote_fd = get_remote_transfer_fd(response); |
| 495 | free(response); |
| 496 | if (remote_fd < 0) |
| 497 | goto port_pasv_cleanup_out; |
| 498 | |
| 499 | trans_ret = bb_copyfd_eof(opened_file, remote_fd); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 500 | close(remote_fd); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 501 | if (trans_ret < 0) |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 502 | cmdio_write_error(FTP_BADSENDFILE); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 503 | else |
| 504 | cmdio_write_ok(FTP_TRANSFEROK); |
| 505 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 506 | port_pasv_cleanup_out: |
| 507 | port_pasv_cleanup(); |
| 508 | |
| 509 | file_close_out: |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 510 | close(opened_file); |
| 511 | } |
| 512 | |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 513 | /* List commands */ |
| 514 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 515 | static int |
| 516 | popen_ls(const char *opt) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 517 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 518 | char *cwd; |
| 519 | const char *argv[5] = { "ftpd", opt, NULL, G.ftp_arg, NULL }; |
| 520 | struct fd_pair outfd; |
| 521 | pid_t pid; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 522 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 523 | cwd = xrealloc_getcwd_or_warn(NULL); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 524 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 525 | xpiped_pair(outfd); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 526 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 527 | fflush(NULL); |
| 528 | pid = vfork(); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 529 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 530 | switch (pid) { |
| 531 | case -1: /* failure */ |
| 532 | bb_perror_msg_and_die("vfork"); |
| 533 | case 0: /* child */ |
| 534 | /* NB: close _first_, then move fds! */ |
| 535 | close(outfd.rd); |
| 536 | xmove_fd(outfd.wr, STDOUT_FILENO); |
| 537 | close(STDIN_FILENO); |
| 538 | /* xopen("/dev/null", O_RDONLY); - chroot may lack it! */ |
| 539 | if (fchdir(G.proc_self_fd) == 0) { |
| 540 | close(G.proc_self_fd); |
| 541 | argv[2] = cwd; |
| 542 | /* ftpd ls helper chdirs to argv[2], |
| 543 | * preventing peer from seeing /proc/self |
| 544 | */ |
| 545 | execv("exe", (char**) argv); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 546 | } |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 547 | _exit(127); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 548 | } |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 549 | /* parent */ |
| 550 | close(outfd.wr); |
| 551 | free(cwd); |
| 552 | return outfd.rd; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static void |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 556 | handle_dir_common(int opts) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 557 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 558 | FILE *ls_fp; |
| 559 | char *line; |
| 560 | int ls_fd; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 561 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 562 | if (!(opts & 1) && !data_transfer_checks_ok()) |
| 563 | return; /* data_transfer_checks_ok emitted error response */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 564 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 565 | ls_fd = popen_ls((opts & 2) ? "-l" : "-1"); |
| 566 | ls_fp = fdopen(ls_fd, "r"); |
| 567 | if (!ls_fp) /* never happens. paranoia */ |
| 568 | bb_perror_msg_and_die("fdopen"); |
| 569 | |
| 570 | if (opts & 1) { |
| 571 | /* STAT <filename> */ |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 572 | cmdio_write_raw(STR(FTP_STATFILE_OK)"-Status follows:\r\n"); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 573 | while (1) { |
| 574 | line = xmalloc_fgetline(ls_fp); |
| 575 | if (!line) |
| 576 | break; |
| 577 | cmdio_write(0, line); /* hack: 0 results in no status at all */ |
| 578 | free(line); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 579 | } |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 580 | cmdio_write_ok(FTP_STATFILE_OK); |
| 581 | } else { |
| 582 | /* LIST/NLST [<filename>] */ |
| 583 | int remote_fd = get_remote_transfer_fd(" Here comes the directory listing"); |
| 584 | if (remote_fd >= 0) { |
| 585 | while (1) { |
| 586 | line = xmalloc_fgetline(ls_fp); |
| 587 | if (!line) |
| 588 | break; |
| 589 | xwrite_str(remote_fd, line); |
| 590 | xwrite(remote_fd, "\r\n", 2); |
| 591 | free(line); |
| 592 | } |
| 593 | } |
| 594 | close(remote_fd); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 595 | port_pasv_cleanup(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 596 | cmdio_write_ok(FTP_TRANSFEROK); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 597 | } |
| 598 | fclose(ls_fp); /* closes ls_fd too */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 599 | } |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 600 | static void |
| 601 | handle_list(void) |
| 602 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 603 | handle_dir_common(2); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 604 | } |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 605 | static void |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 606 | handle_nlst(void) |
| 607 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 608 | handle_dir_common(0); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 609 | } |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 610 | static void |
| 611 | handle_stat_file(void) |
| 612 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 613 | handle_dir_common(3); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 616 | static void |
| 617 | handle_size(void) |
| 618 | { |
| 619 | struct stat statbuf; |
| 620 | char buf[sizeof(STR(FTP_STATFILE_OK)" %"OFF_FMT"u\r\n") + sizeof(off_t)*3]; |
| 621 | |
| 622 | if (!G.ftp_arg |
| 623 | || stat(G.ftp_arg, &statbuf) != 0 |
| 624 | || !S_ISREG(statbuf.st_mode) |
| 625 | ) { |
| 626 | cmdio_write_error(FTP_FILEFAIL); |
| 627 | return; |
| 628 | } |
| 629 | sprintf(buf, STR(FTP_STATFILE_OK)" %"OFF_FMT"u\r\n", statbuf.st_size); |
| 630 | cmdio_write_raw(buf); |
| 631 | } |
| 632 | |
Denis Vlasenko | 9e95920 | 2009-03-09 03:15:05 +0000 | [diff] [blame] | 633 | /* Upload commands */ |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 634 | |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 635 | #if ENABLE_FEATURE_FTP_WRITE |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 636 | static void |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 637 | handle_mkd(void) |
| 638 | { |
| 639 | if (!G.ftp_arg || mkdir(G.ftp_arg, 0777) != 0) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 640 | cmdio_write_error(FTP_FILEFAIL); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 641 | return; |
| 642 | } |
| 643 | cmdio_write_ok(FTP_MKDIROK); |
| 644 | } |
| 645 | |
| 646 | static void |
| 647 | handle_rmd(void) |
| 648 | { |
| 649 | if (!G.ftp_arg || rmdir(G.ftp_arg) != 0) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 650 | cmdio_write_error(FTP_FILEFAIL); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | cmdio_write_ok(FTP_RMDIROK); |
| 654 | } |
| 655 | |
| 656 | static void |
| 657 | handle_dele(void) |
| 658 | { |
| 659 | if (!G.ftp_arg || unlink(G.ftp_arg) != 0) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 660 | cmdio_write_error(FTP_FILEFAIL); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 661 | return; |
| 662 | } |
| 663 | cmdio_write_ok(FTP_DELEOK); |
| 664 | } |
| 665 | |
| 666 | static void |
| 667 | handle_rnfr(void) |
| 668 | { |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 669 | free(G.rnfr_filename); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 670 | G.rnfr_filename = xstrdup(G.ftp_arg); |
| 671 | cmdio_write_ok(FTP_RNFROK); |
| 672 | } |
| 673 | |
| 674 | static void |
| 675 | handle_rnto(void) |
| 676 | { |
| 677 | int retval; |
| 678 | |
| 679 | /* If we didn't get a RNFR, throw a wobbly */ |
| 680 | if (G.rnfr_filename == NULL || G.ftp_arg == NULL) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 681 | cmdio_write_raw(STR(FTP_NEEDRNFR)" RNFR required first\r\n"); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 682 | return; |
| 683 | } |
| 684 | |
| 685 | retval = rename(G.rnfr_filename, G.ftp_arg); |
| 686 | free(G.rnfr_filename); |
| 687 | G.rnfr_filename = NULL; |
| 688 | |
| 689 | if (retval) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 690 | cmdio_write_error(FTP_FILEFAIL); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 691 | return; |
| 692 | } |
| 693 | cmdio_write_ok(FTP_RENAMEOK); |
| 694 | } |
| 695 | |
| 696 | static void |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 697 | handle_upload_common(int is_append, int is_unique) |
| 698 | { |
| 699 | char *tempname = NULL; |
| 700 | int trans_ret; |
| 701 | int local_file_fd; |
| 702 | int remote_fd; |
| 703 | off_t offset; |
| 704 | |
| 705 | offset = G.restart_pos; |
| 706 | G.restart_pos = 0; |
| 707 | |
| 708 | if (!data_transfer_checks_ok()) |
| 709 | return; |
| 710 | |
| 711 | local_file_fd = -1; |
| 712 | if (is_unique) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 713 | tempname = xstrdup(" FILE: uniq.XXXXXX"); |
| 714 | local_file_fd = mkstemp(tempname + 7); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 715 | } else if (G.ftp_arg) { |
| 716 | int flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 717 | if (is_append) |
| 718 | flags = O_WRONLY | O_CREAT | O_APPEND; |
| 719 | if (offset) |
| 720 | flags = O_WRONLY | O_CREAT; |
| 721 | local_file_fd = open(G.ftp_arg, flags, 0666); |
| 722 | } |
| 723 | if (local_file_fd < 0) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 724 | cmdio_write_error(FTP_UPLOADFAIL); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 725 | return; |
| 726 | } |
| 727 | |
| 728 | /* TODO: paranoia: fstat it, refuse to do anything if it's not a regular file */ |
| 729 | |
| 730 | if (offset) |
| 731 | xlseek(local_file_fd, offset, SEEK_SET); |
| 732 | |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 733 | remote_fd = get_remote_transfer_fd(tempname ? tempname : " Ok to send data"); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 734 | free(tempname); |
| 735 | |
| 736 | if (remote_fd < 0) |
| 737 | goto bail; |
| 738 | |
| 739 | trans_ret = bb_copyfd_eof(remote_fd, local_file_fd); |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 740 | close(remote_fd); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 741 | |
| 742 | if (trans_ret < 0) |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 743 | cmdio_write_error(FTP_BADSENDFILE); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 744 | else |
| 745 | cmdio_write_ok(FTP_TRANSFEROK); |
| 746 | |
| 747 | bail: |
| 748 | port_pasv_cleanup(); |
| 749 | close(local_file_fd); |
| 750 | } |
| 751 | |
| 752 | static void |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 753 | handle_stor(void) |
| 754 | { |
| 755 | handle_upload_common(0, 0); |
| 756 | } |
| 757 | |
| 758 | static void |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 759 | handle_appe(void) |
| 760 | { |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 761 | G.restart_pos = 0; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 762 | handle_upload_common(1, 0); |
| 763 | } |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 764 | |
| 765 | static void |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 766 | handle_stou(void) |
| 767 | { |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 768 | G.restart_pos = 0; |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 769 | handle_upload_common(0, 1); |
| 770 | } |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 771 | #endif /* ENABLE_FEATURE_FTP_WRITE */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 772 | |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 773 | static uint32_t |
| 774 | cmdio_get_cmd_and_arg(void) |
| 775 | { |
| 776 | size_t len; |
| 777 | uint32_t cmdval; |
| 778 | char *cmd; |
| 779 | |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 780 | free(G.ftp_cmd); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 781 | len = 8 * 1024; /* Paranoia. Peer may send 1 gigabyte long cmd... */ |
Denis Vlasenko | 57a3b17 | 2009-03-09 04:38:37 +0000 | [diff] [blame] | 782 | G.ftp_cmd = cmd = xmalloc_reads(STDIN_FILENO, NULL, &len); |
Denis Vlasenko | ffb4bb3 | 2009-03-09 02:23:45 +0000 | [diff] [blame] | 783 | if (!cmd) |
| 784 | exit(0); |
| 785 | |
| 786 | len = strlen(cmd) - 1; |
| 787 | while (len >= 0 && cmd[len] == '\r') { |
| 788 | cmd[len] = '\0'; |
| 789 | len--; |
| 790 | } |
| 791 | |
| 792 | G.ftp_arg = strchr(cmd, ' '); |
| 793 | if (G.ftp_arg != NULL) { |
| 794 | *G.ftp_arg = '\0'; |
| 795 | G.ftp_arg++; |
| 796 | } |
| 797 | cmdval = 0; |
| 798 | while (*cmd) |
| 799 | cmdval = (cmdval << 8) + ((unsigned char)*cmd++ & (unsigned char)~0x20); |
| 800 | |
| 801 | return cmdval; |
| 802 | } |
| 803 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 804 | int ftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 805 | int ftpd_main(int argc, char **argv) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 806 | { |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 807 | smallint opts; |
| 808 | |
| 809 | opts = getopt32(argv, "l1v" USE_FEATURE_FTP_WRITE("w")); |
| 810 | |
| 811 | if (opts & (OPT_l|OPT_1)) { |
| 812 | /* Our secret backdoor to ls */ |
| 813 | xchdir(argv[2]); |
| 814 | argv[2] = (char*)"--"; |
| 815 | return ls_main(argc, argv); |
| 816 | } |
| 817 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 818 | INIT_G(); |
| 819 | |
| 820 | G.local_addr = get_sock_lsa(STDIN_FILENO); |
| 821 | if (!G.local_addr) { |
| 822 | /* This is confusing: |
| 823 | * bb_error_msg_and_die("stdin is not a socket"); |
| 824 | * Better: */ |
| 825 | bb_show_usage(); |
| 826 | /* Help text says that ftpd must be used as inetd service, |
| 827 | * which is by far the most usual cause of get_sock_lsa |
| 828 | * failure */ |
| 829 | } |
| 830 | |
Denis Vlasenko | 5b28d31 | 2009-03-09 17:22:16 +0000 | [diff] [blame^] | 831 | /* LOG_NDELAY is needed since we may chroot later */ |
| 832 | openlog(applet_name, LOG_PID | LOG_NDELAY, LOG_DAEMON); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 833 | logmode |= LOGMODE_SYSLOG; |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 834 | if (!(opts & OPT_v)) |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 835 | logmode = LOGMODE_SYSLOG; |
| 836 | |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 837 | G.proc_self_fd = xopen("/proc/self", O_RDONLY | O_DIRECTORY); |
| 838 | |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 839 | if (argv[optind]) { |
| 840 | xchdir(argv[optind]); |
| 841 | chroot("."); |
| 842 | } |
| 843 | |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 844 | //umask(077); - admin can set umask before starting us |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 845 | |
| 846 | /* Signals. We'll always take -EPIPE rather than a rude signal, thanks */ |
| 847 | signal(SIGPIPE, SIG_IGN); |
| 848 | |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 849 | /* Set up options on the command socket (do we need these all? why?) */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 850 | setsockopt(STDIN_FILENO, IPPROTO_TCP, TCP_NODELAY, &const_int_1, sizeof(const_int_1)); |
| 851 | setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); |
| 852 | setsockopt(STDIN_FILENO, SOL_SOCKET, SO_OOBINLINE, &const_int_1, sizeof(const_int_1)); |
| 853 | |
Denis Vlasenko | d42eb81 | 2009-03-09 04:22:52 +0000 | [diff] [blame] | 854 | cmdio_write_ok(FTP_GREET); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 855 | |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 856 | #ifdef IF_WE_WANT_TO_REQUIRE_LOGIN |
| 857 | { |
| 858 | smallint user_was_specified = 0; |
| 859 | while (1) { |
| 860 | uint32_t cmdval = cmdio_get_cmd_and_arg(); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 861 | |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 862 | if (cmdval == const_USER) { |
| 863 | if (G.ftp_arg == NULL || strcasecmp(G.ftp_arg, "anonymous") != 0) |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 864 | cmdio_write_raw(STR(FTP_LOGINERR)" Server is anonymous only\r\n"); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 865 | else { |
| 866 | user_was_specified = 1; |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 867 | cmdio_write_raw(STR(FTP_GIVEPWORD)" Please specify the password\r\n"); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 868 | } |
| 869 | } else if (cmdval == const_PASS) { |
| 870 | if (user_was_specified) |
| 871 | break; |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 872 | cmdio_write_raw(STR(FTP_NEEDUSER)" Login with USER\r\n"); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 873 | } else if (cmdval == const_QUIT) { |
Denis Vlasenko | 3455285 | 2009-03-09 04:18:00 +0000 | [diff] [blame] | 874 | cmdio_write_ok(FTP_GOODBYE); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 875 | return 0; |
| 876 | } else { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 877 | cmdio_write_raw(STR(FTP_LOGINERR)" Login with USER and PASS\r\n"); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 878 | } |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 881 | cmdio_write_ok(FTP_LOGINOK); |
| 882 | #endif |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 883 | |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 884 | /* RFC-959 Section 5.1 |
| 885 | * The following commands and options MUST be supported by every |
| 886 | * server-FTP and user-FTP, except in cases where the underlying |
| 887 | * file system or operating system does not allow or support |
| 888 | * a particular command. |
| 889 | * Type: ASCII Non-print, IMAGE, LOCAL 8 |
| 890 | * Mode: Stream |
| 891 | * Structure: File, Record* |
| 892 | * (Record structure is REQUIRED only for hosts whose file |
| 893 | * systems support record structure). |
| 894 | * Commands: |
| 895 | * USER, PASS, ACCT, [bbox: ACCT not supported] |
| 896 | * PORT, PASV, |
| 897 | * TYPE, MODE, STRU, |
| 898 | * RETR, STOR, APPE, |
| 899 | * RNFR, RNTO, DELE, |
| 900 | * CWD, CDUP, RMD, MKD, PWD, |
| 901 | * LIST, NLST, |
| 902 | * SYST, STAT, |
| 903 | * HELP, NOOP, QUIT. |
| 904 | */ |
| 905 | /* ACCOUNT (ACCT) |
Denis Vlasenko | 3455285 | 2009-03-09 04:18:00 +0000 | [diff] [blame] | 906 | * "The argument field is a Telnet string identifying the user's account. |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 907 | * The command is not necessarily related to the USER command, as some |
| 908 | * sites may require an account for login and others only for specific |
| 909 | * access, such as storing files. In the latter case the command may |
| 910 | * arrive at any time. |
| 911 | * There are reply codes to differentiate these cases for the automation: |
| 912 | * when account information is required for login, the response to |
| 913 | * a successful PASSword command is reply code 332. On the other hand, |
| 914 | * if account information is NOT required for login, the reply to |
| 915 | * a successful PASSword command is 230; and if the account information |
| 916 | * is needed for a command issued later in the dialogue, the server |
| 917 | * should return a 332 or 532 reply depending on whether it stores |
| 918 | * (pending receipt of the ACCounT command) or discards the command, |
Denis Vlasenko | 3455285 | 2009-03-09 04:18:00 +0000 | [diff] [blame] | 919 | * respectively." |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 920 | */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 921 | |
| 922 | while (1) { |
| 923 | uint32_t cmdval = cmdio_get_cmd_and_arg(); |
| 924 | |
| 925 | if (cmdval == const_QUIT) { |
Denis Vlasenko | 3455285 | 2009-03-09 04:18:00 +0000 | [diff] [blame] | 926 | cmdio_write_ok(FTP_GOODBYE); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 927 | return 0; |
| 928 | } |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 929 | else if (cmdval == const_USER) |
| 930 | cmdio_write_ok(FTP_GIVEPWORD); |
| 931 | else if (cmdval == const_PASS) |
| 932 | cmdio_write_ok(FTP_LOGINOK); |
| 933 | else if (cmdval == const_NOOP) |
| 934 | cmdio_write_ok(FTP_NOOPOK); |
| 935 | else if (cmdval == const_TYPE) |
| 936 | cmdio_write_ok(FTP_TYPEOK); |
| 937 | else if (cmdval == const_STRU) |
| 938 | cmdio_write_ok(FTP_STRUOK); |
| 939 | else if (cmdval == const_MODE) |
| 940 | cmdio_write_ok(FTP_MODEOK); |
| 941 | else if (cmdval == const_ALLO) |
| 942 | cmdio_write_ok(FTP_ALLOOK); |
| 943 | else if (cmdval == const_SYST) |
| 944 | cmdio_write_raw(STR(FTP_SYSTOK)" UNIX Type: L8\r\n"); |
| 945 | else if (cmdval == const_PWD) |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 946 | handle_pwd(); |
| 947 | else if (cmdval == const_CWD) |
| 948 | handle_cwd(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 949 | else if (cmdval == const_CDUP) /* cd .. */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 950 | handle_cdup(); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 951 | else if (cmdval == const_HELP) |
| 952 | handle_help(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 953 | else if (cmdval == const_LIST) /* ls -l */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 954 | handle_list(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 955 | else if (cmdval == const_NLST) /* "name list", bare ls */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 956 | handle_nlst(); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 957 | else if (cmdval == const_SIZE) |
| 958 | handle_size(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 959 | else if (cmdval == const_STAT) { |
| 960 | if (G.ftp_arg == NULL) |
| 961 | handle_stat(); |
| 962 | else |
| 963 | handle_stat_file(); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 964 | } |
| 965 | else if (cmdval == const_PASV) |
| 966 | handle_pasv(); |
| 967 | else if (cmdval == const_EPSV) |
| 968 | handle_epsv(); |
| 969 | else if (cmdval == const_RETR) |
| 970 | handle_retr(); |
| 971 | else if (cmdval == const_PORT) |
| 972 | handle_port(); |
| 973 | else if (cmdval == const_REST) |
| 974 | handle_rest(); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 975 | #if ENABLE_FEATURE_FTP_WRITE |
Denis Vlasenko | c41cba5 | 2009-03-09 15:46:07 +0000 | [diff] [blame] | 976 | else if (opts & OPT_w) { |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 977 | if (cmdval == const_STOR) |
| 978 | handle_stor(); |
| 979 | else if (cmdval == const_MKD) |
| 980 | handle_mkd(); |
| 981 | else if (cmdval == const_RMD) |
| 982 | handle_rmd(); |
| 983 | else if (cmdval == const_DELE) |
| 984 | handle_dele(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 985 | else if (cmdval == const_RNFR) /* "rename from" */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 986 | handle_rnfr(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 987 | else if (cmdval == const_RNTO) /* "rename to" */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 988 | handle_rnto(); |
| 989 | else if (cmdval == const_APPE) |
| 990 | handle_appe(); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 991 | else if (cmdval == const_STOU) /* "store unique" */ |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 992 | handle_stou(); |
| 993 | } |
| 994 | #endif |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 995 | #if 0 |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 996 | else if (cmdval == const_STOR |
| 997 | || cmdval == const_MKD |
| 998 | || cmdval == const_RMD |
| 999 | || cmdval == const_DELE |
| 1000 | || cmdval == const_RNFR |
| 1001 | || cmdval == const_RNTO |
| 1002 | || cmdval == const_APPE |
| 1003 | || cmdval == const_STOU |
| 1004 | ) { |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 1005 | cmdio_write_raw(STR(FTP_NOPERM)" Permission denied\r\n"); |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1006 | } |
| 1007 | #endif |
| 1008 | else { |
Denis Vlasenko | 51c9bb1 | 2009-03-09 02:51:46 +0000 | [diff] [blame] | 1009 | /* Which unsupported commands were seen in the wild? |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1010 | * (doesn't necessarily mean "we must support them") |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 1011 | * lftp 3.6.3: FEAT - is it useful? |
| 1012 | * MDTM - works fine without it anyway |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 1013 | */ |
Denis Vlasenko | f1a11b5 | 2009-03-09 04:13:59 +0000 | [diff] [blame] | 1014 | cmdio_write_raw(STR(FTP_BADCMD)" Unknown command\r\n"); |
Denis Vlasenko | bf9d179 | 2009-03-08 09:31:28 +0000 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | } |