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