blob: 29589d185caaed059904c9f3d5e8bc9138da3b19 [file] [log] [blame]
Denis Vlasenkobf9d1792009-03-08 09:31:28 +00001/* 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 Vlasenko9b2fbda2009-03-09 13:01:08 +00007 * Licensed under GPLv2, see file LICENSE in this tarball for details.
8 *
Denis Vlasenkobf9d1792009-03-08 09:31:28 +00009 * 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 Vlasenkoffb4bb32009-03-09 02:23:45 +000014 *
15 * TODO: implement "421 Timeout" thingy (alarm(60) while waiting for a cmd).
Denis Vlasenkobf9d1792009-03-08 09:31:28 +000016 */
17
18#include "libbb.h"
Denis Vlasenko5e4fda02009-03-08 23:46:48 +000019#include <syslog.h>
Denis Vlasenkobf9d1792009-03-08 09:31:28 +000020#include <netinet/tcp.h>
21
Denis Vlasenkof1a11b52009-03-09 04:13:59 +000022#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 Vlasenko9b2fbda2009-03-09 13:01:08 +000037/*#define FTP_EPRTOK 228*/
38#define FTP_EPSVOK 229
Denis Vlasenkof1a11b52009-03-09 04:13:59 +000039#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 Vlasenkoc41cba52009-03-09 15:46:07 +000066/* Convert a constant to 3-digit string, packed into uint32_t */
Denis Vlasenkobf9d1792009-03-08 09:31:28 +000067enum {
Denis Vlasenkoc41cba52009-03-09 15:46:07 +000068 /* 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
79enum {
80 OPT_l = (1 << 0),
81 OPT_1 = (1 << 1),
82 OPT_v = (1 << 2),
83 OPT_w = (1 << 3),
Denis Vlasenko5e4fda02009-03-08 23:46:48 +000084
Denis Vlasenkobf9d1792009-03-08 09:31:28 +000085#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 Vlasenko9b2fbda2009-03-09 13:01:08 +000092 const_EPSV = mk_const4('E', 'P', 'S', 'V'),
Denis Vlasenkobf9d1792009-03-08 09:31:28 +000093 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 Vlasenko9b2fbda2009-03-09 13:01:08 +0000109 const_SIZE = mk_const4('S', 'I', 'Z', 'E'),
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000110 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
119struct 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 Vlasenkoc41cba52009-03-09 15:46:07 +0000124 int proc_self_fd;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000125 off_t restart_pos;
Denis Vlasenko57a3b172009-03-09 04:38:37 +0000126 char *ftp_cmd;
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000127 char *ftp_arg;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000128#if ENABLE_FEATURE_FTP_WRITE
129 char *rnfr_filename;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000130#endif
131};
132#define G (*(struct globals*)&bb_common_bufsiz1)
133#define INIT_G() do { } while (0)
134
Denis Vlasenko57a3b172009-03-09 04:38:37 +0000135
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000136static char *
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000137escape_text(const char *prepend, const char *str, unsigned escapee)
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000138{
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000139 unsigned retlen, remainlen, chunklen;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000140 char *ret, *found;
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000141 char append;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000142
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000143 append = (char)escapee;
144 escapee >>= 8;
145
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000146 remainlen = strlen(str);
Denis Vlasenko57a3b172009-03-09 04:38:37 +0000147 retlen = strlen(prepend);
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000148 ret = xmalloc(retlen + remainlen * 2 + 1 + 1);
Denis Vlasenko57a3b172009-03-09 04:38:37 +0000149 strcpy(ret, prepend);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000150
151 for (;;) {
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000152 found = strchrnul(str, escapee);
153 chunklen = found - str + 1;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000154
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000155 /* Copy chunk up to and including escapee (or NUL) to ret */
156 memcpy(ret + retlen, str, chunklen);
Denis Vlasenko57a3b172009-03-09 04:38:37 +0000157 retlen += chunklen;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000158
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000159 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 Vlasenkobf9d1792009-03-08 09:31:28 +0000163 break;
164 }
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000165 ret[retlen++] = escapee; /* duplicate escapee */
166 str = found + 1;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000167 }
168 return ret;
169}
170
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000171/* Returns strlen as a bonus */
172static unsigned
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000173replace_char(char *str, char from, char to)
174{
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000175 char *p = str;
176 while (*p) {
177 if (*p == from)
178 *p = to;
179 p++;
180 }
181 return p - str;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000182}
183
184static void
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000185cmdio_write(uint32_t status_str, const char *str)
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000186{
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000187 union {
188 char buf[4];
189 uint32_t v32;
190 } u;
Denis Vlasenko57a3b172009-03-09 04:38:37 +0000191 char *response;
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000192 int len;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000193
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000194 u.v32 = status_str;
195
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000196 /* FTP allegedly uses telnet protocol for command link.
197 * In telnet, 0xff is an escape char, and needs to be escaped: */
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000198 response = escape_text(u.buf, str, (0xff << 8) + '\r');
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000199
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000200 /* ?! does FTP send embedded LFs as NULs? wow */
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000201 len = replace_char(response, '\n', '\0');
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000202
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000203 response[len++] = '\n'; /* tack on trailing '\n' */
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000204 xwrite(STDOUT_FILENO, response, len);
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000205 free(response);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000206}
207
208static void
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000209cmdio_write_ok(int status)
210{
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000211 fdprintf(STDOUT_FILENO, "%u Operation successful\r\n", status);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000212}
213
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000214/* TODO: output strerr(errno) if errno != 0? */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000215static void
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000216cmdio_write_error(int status)
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000217{
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000218 fdprintf(STDOUT_FILENO, "%u Error\r\n", status);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000219}
220
221static void
222cmdio_write_raw(const char *p_text)
223{
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000224 xwrite_str(STDOUT_FILENO, p_text);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000225}
226
Denis Vlasenko9e959202009-03-09 03:15:05 +0000227/* Simple commands */
228
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000229static void
230handle_pwd(void)
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000231{
Denis Vlasenko57a3b172009-03-09 04:38:37 +0000232 char *cwd, *response;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000233
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000234 cwd = xrealloc_getcwd_or_warn(NULL);
235 if (cwd == NULL)
236 cwd = xstrdup("");
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000237
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000238 /* We have to promote each " to "" */
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000239 response = escape_text(" \"", cwd, ('"' << 8) + '"');
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000240 free(cwd);
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000241 cmdio_write(STRNUM32(FTP_PWDOK), response);
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000242 free(response);
243}
244
245static void
246handle_cwd(void)
247{
248 if (!G.ftp_arg || chdir(G.ftp_arg) != 0) {
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000249 cmdio_write_error(FTP_FILEFAIL);
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000250 return;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000251 }
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000252 cmdio_write_ok(FTP_CWDOK);
253}
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000254
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000255static void
256handle_cdup(void)
257{
258 G.ftp_arg = (char*)"..";
259 handle_cwd();
260}
261
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000262static void
Denis Vlasenko9e959202009-03-09 03:15:05 +0000263handle_stat(void)
264{
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000265 cmdio_write_raw(STR(FTP_STATOK)"-FTP server status:\r\n"
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000266 " TYPE: BINARY\r\n"
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000267 STR(FTP_STATOK)" Ok\r\n");
Denis Vlasenko9e959202009-03-09 03:15:05 +0000268}
269
270static void
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000271handle_help(void)
272{
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000273 cmdio_write_raw(STR(FTP_HELP)"-Commands:\r\n"
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000274 " 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 Vlasenkoffb4bb32009-03-09 02:23:45 +0000277#if ENABLE_FEATURE_FTP_WRITE
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000278 " APPE DELE MKD RMD RNFR RNTO STOR STOU\r\n"
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000279#endif
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000280 STR(FTP_HELP)" Ok\r\n");
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000281}
282
Denis Vlasenko9e959202009-03-09 03:15:05 +0000283/* Download commands */
284
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000285static int
286ftpdataio_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 Vlasenkof1a11b52009-03-09 04:13:59 +0000293 cmdio_write_error(FTP_BADSENDCONN);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000294 return remote_fd;
295 }
296
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000297 setsockopt(remote_fd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000298 return remote_fd;
299}
300
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000301static inline int
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000302port_active(void)
303{
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000304 return (G.port_addr != NULL);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000305}
306
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000307static inline int
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000308pasv_active(void)
309{
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000310 return (G.pasv_listen_fd > STDOUT_FILENO);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000311}
312
313static int
314get_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 Vlasenkoc41cba52009-03-09 15:46:07 +0000321 remote_fd = xconnect_stream(G.port_addr);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000322
323 if (remote_fd < 0)
324 return remote_fd;
325
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000326 cmdio_write(STRNUM32(FTP_DATACONN), p_status_msg);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000327 return remote_fd;
328}
329
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000330static int
331data_transfer_checks_ok(void)
332{
333 if (!pasv_active() && !port_active()) {
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000334 cmdio_write_raw(STR(FTP_BADSENDCONN)" Use PORT or PASV first\r\n");
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000335 return 0;
336 }
337
338 return 1;
339}
340
341static void
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000342port_pasv_cleanup(void)
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000343{
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000344 free(G.port_addr);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000345 G.port_addr = NULL;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000346 if (G.pasv_listen_fd > STDOUT_FILENO)
347 close(G.pasv_listen_fd);
348 G.pasv_listen_fd = -1;
349}
350
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000351static unsigned
352bind_for_passive_mode(void)
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000353{
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000354 int fd;
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000355 unsigned port;
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000356
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000357 port_pasv_cleanup();
358
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000359 G.pasv_listen_fd = fd = xsocket(G.local_addr->u.sa.sa_family, SOCK_STREAM, 0);
360 setsockopt_reuseaddr(fd);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000361
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000362 set_nport(G.local_addr, 0);
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000363 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 Vlasenko5e4fda02009-03-08 23:46:48 +0000366
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000367 port = get_nport(&G.local_addr->u.sa);
368 port = ntohs(port);
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000369 return port;
370}
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000371
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000372static void
373handle_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 Vlasenkoffb4bb32009-03-09 02:23:45 +0000384 replace_char(addr, '.', ',');
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000385
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000386 response = xasprintf(STR(FTP_PASVOK)" Entering Passive Mode (%s,%u,%u)\r\n",
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000387 addr, (int)(port >> 8), (int)(port & 255));
388 free(addr);
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000389 cmdio_write_raw(response);
390 free(response);
391}
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000392
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000393static void
394handle_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 Vlasenko5e4fda02009-03-08 23:46:48 +0000402 free(response);
403}
404
405static void
406handle_port(void)
407{
408 unsigned short port;
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000409 char *raw, *port_part;
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000410 len_and_sockaddr *lsa = NULL;
411
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000412 port_pasv_cleanup();
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000413
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000414 raw = G.ftp_arg;
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000415
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000416 /* buglets:
417 * xatou16 will accept wrong input,
418 * xatou16 will exit instead of generating error to peer
419 */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000420
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000421 port_part = strrchr(raw, ',');
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000422 if (port_part == NULL)
423 goto bail;
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000424 port = xatou16(&port_part[1]);
425 *port_part = '\0';
426
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000427 port_part = strrchr(raw, ',');
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000428 if (port_part == NULL)
429 goto bail;
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000430 port |= xatou16(&port_part[1]) << 8;
431 *port_part = '\0';
432
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000433 replace_char(raw, ',', '.');
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000434 lsa = xdotted2sockaddr(raw, port);
435
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000436 if (lsa == NULL) {
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000437 bail:
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000438 cmdio_write_error(FTP_BADCMD);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000439 return;
440 }
441
442 G.port_addr = lsa;
443 cmdio_write_ok(FTP_PORTOK);
444}
445
446static void
447handle_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
454static void
455handle_retr(void)
456{
457 struct stat statbuf;
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000458 int trans_ret;
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000459 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 Vlasenkoc41cba52009-03-09 15:46:07 +0000467 return; /* data_transfer_checks_ok emitted error response */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000468
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 Vlasenkof1a11b52009-03-09 04:13:59 +0000472 cmdio_write_error(FTP_FILEFAIL);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000473 return;
474 }
475
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000476 if (fstat(opened_file, &statbuf) != 0 || !S_ISREG(statbuf.st_mode)) {
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000477 /* Note - pretend open failed */
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000478 cmdio_write_error(FTP_FILEFAIL);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000479 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 Vlasenkof1a11b52009-03-09 04:13:59 +0000492 " Opening BINARY mode data connection for %s (%"OFF_FMT"u bytes)",
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000493 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 Vlasenkoc41cba52009-03-09 15:46:07 +0000500 close(remote_fd);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000501 if (trans_ret < 0)
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000502 cmdio_write_error(FTP_BADSENDFILE);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000503 else
504 cmdio_write_ok(FTP_TRANSFEROK);
505
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000506 port_pasv_cleanup_out:
507 port_pasv_cleanup();
508
509 file_close_out:
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000510 close(opened_file);
511}
512
Denis Vlasenko9e959202009-03-09 03:15:05 +0000513/* List commands */
514
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000515static int
516popen_ls(const char *opt)
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000517{
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000518 char *cwd;
519 const char *argv[5] = { "ftpd", opt, NULL, G.ftp_arg, NULL };
520 struct fd_pair outfd;
521 pid_t pid;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000522
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000523 cwd = xrealloc_getcwd_or_warn(NULL);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000524
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000525 xpiped_pair(outfd);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000526
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000527 fflush(NULL);
528 pid = vfork();
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000529
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000530 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 Vlasenkobf9d1792009-03-08 09:31:28 +0000546 }
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000547 _exit(127);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000548 }
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000549 /* parent */
550 close(outfd.wr);
551 free(cwd);
552 return outfd.rd;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000553}
554
555static void
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000556handle_dir_common(int opts)
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000557{
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000558 FILE *ls_fp;
559 char *line;
560 int ls_fd;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000561
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000562 if (!(opts & 1) && !data_transfer_checks_ok())
563 return; /* data_transfer_checks_ok emitted error response */
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000564
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000565 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 Vlasenkof1a11b52009-03-09 04:13:59 +0000572 cmdio_write_raw(STR(FTP_STATFILE_OK)"-Status follows:\r\n");
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000573 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 Vlasenkobf9d1792009-03-08 09:31:28 +0000579 }
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000580 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 Vlasenkoffb4bb32009-03-09 02:23:45 +0000595 port_pasv_cleanup();
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000596 cmdio_write_ok(FTP_TRANSFEROK);
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000597 }
598 fclose(ls_fp); /* closes ls_fd too */
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000599}
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000600static void
601handle_list(void)
602{
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000603 handle_dir_common(2);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000604}
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000605static void
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000606handle_nlst(void)
607{
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000608 handle_dir_common(0);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000609}
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000610static void
611handle_stat_file(void)
612{
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000613 handle_dir_common(3);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000614}
615
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000616static void
617handle_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 Vlasenko9e959202009-03-09 03:15:05 +0000633/* Upload commands */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000634
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000635#if ENABLE_FEATURE_FTP_WRITE
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000636static void
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000637handle_mkd(void)
638{
639 if (!G.ftp_arg || mkdir(G.ftp_arg, 0777) != 0) {
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000640 cmdio_write_error(FTP_FILEFAIL);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000641 return;
642 }
643 cmdio_write_ok(FTP_MKDIROK);
644}
645
646static void
647handle_rmd(void)
648{
649 if (!G.ftp_arg || rmdir(G.ftp_arg) != 0) {
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000650 cmdio_write_error(FTP_FILEFAIL);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000651 return;
652 }
653 cmdio_write_ok(FTP_RMDIROK);
654}
655
656static void
657handle_dele(void)
658{
659 if (!G.ftp_arg || unlink(G.ftp_arg) != 0) {
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000660 cmdio_write_error(FTP_FILEFAIL);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000661 return;
662 }
663 cmdio_write_ok(FTP_DELEOK);
664}
665
666static void
667handle_rnfr(void)
668{
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000669 free(G.rnfr_filename);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000670 G.rnfr_filename = xstrdup(G.ftp_arg);
671 cmdio_write_ok(FTP_RNFROK);
672}
673
674static void
675handle_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 Vlasenkof1a11b52009-03-09 04:13:59 +0000681 cmdio_write_raw(STR(FTP_NEEDRNFR)" RNFR required first\r\n");
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000682 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 Vlasenkof1a11b52009-03-09 04:13:59 +0000690 cmdio_write_error(FTP_FILEFAIL);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000691 return;
692 }
693 cmdio_write_ok(FTP_RENAMEOK);
694}
695
696static void
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000697handle_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 Vlasenkof1a11b52009-03-09 04:13:59 +0000713 tempname = xstrdup(" FILE: uniq.XXXXXX");
714 local_file_fd = mkstemp(tempname + 7);
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000715 } 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 Vlasenkof1a11b52009-03-09 04:13:59 +0000724 cmdio_write_error(FTP_UPLOADFAIL);
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000725 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 Vlasenkof1a11b52009-03-09 04:13:59 +0000733 remote_fd = get_remote_transfer_fd(tempname ? tempname : " Ok to send data");
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000734 free(tempname);
735
736 if (remote_fd < 0)
737 goto bail;
738
739 trans_ret = bb_copyfd_eof(remote_fd, local_file_fd);
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000740 close(remote_fd);
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000741
742 if (trans_ret < 0)
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000743 cmdio_write_error(FTP_BADSENDFILE);
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000744 else
745 cmdio_write_ok(FTP_TRANSFEROK);
746
747 bail:
748 port_pasv_cleanup();
749 close(local_file_fd);
750}
751
752static void
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000753handle_stor(void)
754{
755 handle_upload_common(0, 0);
756}
757
758static void
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000759handle_appe(void)
760{
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000761 G.restart_pos = 0;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000762 handle_upload_common(1, 0);
763}
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000764
765static void
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000766handle_stou(void)
767{
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000768 G.restart_pos = 0;
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000769 handle_upload_common(0, 1);
770}
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000771#endif /* ENABLE_FEATURE_FTP_WRITE */
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000772
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000773static uint32_t
774cmdio_get_cmd_and_arg(void)
775{
776 size_t len;
777 uint32_t cmdval;
778 char *cmd;
779
Denis Vlasenko57a3b172009-03-09 04:38:37 +0000780 free(G.ftp_cmd);
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000781 len = 8 * 1024; /* Paranoia. Peer may send 1 gigabyte long cmd... */
Denis Vlasenko57a3b172009-03-09 04:38:37 +0000782 G.ftp_cmd = cmd = xmalloc_reads(STDIN_FILENO, NULL, &len);
Denis Vlasenkoffb4bb32009-03-09 02:23:45 +0000783 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 Vlasenkobf9d1792009-03-08 09:31:28 +0000804int ftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000805int ftpd_main(int argc, char **argv)
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000806{
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000807 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 Vlasenkobf9d1792009-03-08 09:31:28 +0000818 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 Vlasenko5b28d312009-03-09 17:22:16 +0000831 /* LOG_NDELAY is needed since we may chroot later */
832 openlog(applet_name, LOG_PID | LOG_NDELAY, LOG_DAEMON);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000833 logmode |= LOGMODE_SYSLOG;
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000834 if (!(opts & OPT_v))
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000835 logmode = LOGMODE_SYSLOG;
836
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000837 G.proc_self_fd = xopen("/proc/self", O_RDONLY | O_DIRECTORY);
838
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000839 if (argv[optind]) {
840 xchdir(argv[optind]);
841 chroot(".");
842 }
843
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000844 //umask(077); - admin can set umask before starting us
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000845
846 /* Signals. We'll always take -EPIPE rather than a rude signal, thanks */
847 signal(SIGPIPE, SIG_IGN);
848
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000849 /* Set up options on the command socket (do we need these all? why?) */
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000850 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 Vlasenkod42eb812009-03-09 04:22:52 +0000854 cmdio_write_ok(FTP_GREET);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000855
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000856#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 Vlasenkobf9d1792009-03-08 09:31:28 +0000861
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000862 if (cmdval == const_USER) {
863 if (G.ftp_arg == NULL || strcasecmp(G.ftp_arg, "anonymous") != 0)
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000864 cmdio_write_raw(STR(FTP_LOGINERR)" Server is anonymous only\r\n");
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000865 else {
866 user_was_specified = 1;
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000867 cmdio_write_raw(STR(FTP_GIVEPWORD)" Please specify the password\r\n");
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000868 }
869 } else if (cmdval == const_PASS) {
870 if (user_was_specified)
871 break;
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000872 cmdio_write_raw(STR(FTP_NEEDUSER)" Login with USER\r\n");
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000873 } else if (cmdval == const_QUIT) {
Denis Vlasenko34552852009-03-09 04:18:00 +0000874 cmdio_write_ok(FTP_GOODBYE);
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000875 return 0;
876 } else {
Denis Vlasenkof1a11b52009-03-09 04:13:59 +0000877 cmdio_write_raw(STR(FTP_LOGINERR)" Login with USER and PASS\r\n");
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000878 }
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000879 }
880 }
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000881 cmdio_write_ok(FTP_LOGINOK);
882#endif
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000883
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000884 /* 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 Vlasenko34552852009-03-09 04:18:00 +0000906 * "The argument field is a Telnet string identifying the user's account.
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000907 * 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 Vlasenko34552852009-03-09 04:18:00 +0000919 * respectively."
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000920 */
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000921
922 while (1) {
923 uint32_t cmdval = cmdio_get_cmd_and_arg();
924
925 if (cmdval == const_QUIT) {
Denis Vlasenko34552852009-03-09 04:18:00 +0000926 cmdio_write_ok(FTP_GOODBYE);
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000927 return 0;
928 }
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000929 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 Vlasenkobf9d1792009-03-08 09:31:28 +0000946 handle_pwd();
947 else if (cmdval == const_CWD)
948 handle_cwd();
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000949 else if (cmdval == const_CDUP) /* cd .. */
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000950 handle_cdup();
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000951 else if (cmdval == const_HELP)
952 handle_help();
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000953 else if (cmdval == const_LIST) /* ls -l */
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000954 handle_list();
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000955 else if (cmdval == const_NLST) /* "name list", bare ls */
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000956 handle_nlst();
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000957 else if (cmdval == const_SIZE)
958 handle_size();
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000959 else if (cmdval == const_STAT) {
960 if (G.ftp_arg == NULL)
961 handle_stat();
962 else
963 handle_stat_file();
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000964 }
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 Vlasenkobf9d1792009-03-08 09:31:28 +0000975#if ENABLE_FEATURE_FTP_WRITE
Denis Vlasenkoc41cba52009-03-09 15:46:07 +0000976 else if (opts & OPT_w) {
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000977 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 Vlasenko5e4fda02009-03-08 23:46:48 +0000985 else if (cmdval == const_RNFR) /* "rename from" */
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000986 handle_rnfr();
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000987 else if (cmdval == const_RNTO) /* "rename to" */
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000988 handle_rnto();
989 else if (cmdval == const_APPE)
990 handle_appe();
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000991 else if (cmdval == const_STOU) /* "store unique" */
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000992 handle_stou();
993 }
994#endif
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000995#if 0
Denis Vlasenkobf9d1792009-03-08 09:31:28 +0000996 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 Vlasenkof1a11b52009-03-09 04:13:59 +00001005 cmdio_write_raw(STR(FTP_NOPERM)" Permission denied\r\n");
Denis Vlasenko5e4fda02009-03-08 23:46:48 +00001006 }
1007#endif
1008 else {
Denis Vlasenko51c9bb12009-03-09 02:51:46 +00001009 /* Which unsupported commands were seen in the wild?
Denis Vlasenko5e4fda02009-03-08 23:46:48 +00001010 * (doesn't necessarily mean "we must support them")
Denis Vlasenkof1a11b52009-03-09 04:13:59 +00001011 * lftp 3.6.3: FEAT - is it useful?
1012 * MDTM - works fine without it anyway
Denis Vlasenko5e4fda02009-03-08 23:46:48 +00001013 */
Denis Vlasenkof1a11b52009-03-09 04:13:59 +00001014 cmdio_write_raw(STR(FTP_BADCMD)" Unknown command\r\n");
Denis Vlasenkobf9d1792009-03-08 09:31:28 +00001015 }
1016 }
1017}