1 /* vi: set sw=4 ts=4: */
3 * Simple FTP daemon, based on vsftpd 2.0.7 (written by Chris Evans)
5 * Author: Adam Tkac <vonsch@gmail.com>
7 * Only subset of FTP protocol is implemented but vast majority of clients
8 * should not have any problem. You have to run this daemon via inetd.
11 * -w - enable FTP write commands
13 * TODO: implement "421 Timeout" thingy (alarm(60) while waiting for a cmd).
18 #include <netinet/tcp.h>
20 #define FTP_DATACONN 150
21 #define FTP_NOOPOK 200
22 #define FTP_TYPEOK 200
23 #define FTP_PORTOK 200
24 #define FTP_STRUOK 200
25 #define FTP_MODEOK 200
26 #define FTP_ALLOOK 202
27 #define FTP_STATOK 211
28 #define FTP_STATFILE_OK 213
30 #define FTP_SYSTOK 215
32 #define FTP_GOODBYE 221
33 #define FTP_TRANSFEROK 226
34 #define FTP_PASVOK 227
35 #define FTP_LOGINOK 230
37 #define FTP_RMDIROK 250
38 #define FTP_DELEOK 250
39 #define FTP_RENAMEOK 250
41 #define FTP_MKDIROK 257
42 #define FTP_GIVEPWORD 331
43 #define FTP_RESTOK 350
44 #define FTP_RNFROK 350
45 #define FTP_BADSENDCONN 425
46 #define FTP_BADSENDNET 426
47 #define FTP_BADSENDFILE 451
48 #define FTP_BADCMD 500
49 #define FTP_COMMANDNOTIMPL 502
50 #define FTP_NEEDUSER 503
51 #define FTP_NEEDRNFR 503
52 #define FTP_BADSTRU 504
53 #define FTP_BADMODE 504
54 #define FTP_LOGINERR 530
55 #define FTP_FILEFAIL 550
56 #define FTP_NOPERM 550
57 #define FTP_UPLOADFAIL 553
60 #define STR(s) STR1(s)
66 #define mk_const4(a,b,c,d) (((a * 0x100 + b) * 0x100 + c) * 0x100 + d)
67 #define mk_const3(a,b,c) ((a * 0x100 + b) * 0x100 + c)
68 const_ALLO = mk_const4('A', 'L', 'L', 'O'),
69 const_APPE = mk_const4('A', 'P', 'P', 'E'),
70 const_CDUP = mk_const4('C', 'D', 'U', 'P'),
71 const_CWD = mk_const3('C', 'W', 'D'),
72 const_DELE = mk_const4('D', 'E', 'L', 'E'),
73 const_HELP = mk_const4('H', 'E', 'L', 'P'),
74 const_LIST = mk_const4('L', 'I', 'S', 'T'),
75 const_MKD = mk_const3('M', 'K', 'D'),
76 const_MODE = mk_const4('M', 'O', 'D', 'E'),
77 const_NLST = mk_const4('N', 'L', 'S', 'T'),
78 const_NOOP = mk_const4('N', 'O', 'O', 'P'),
79 const_PASS = mk_const4('P', 'A', 'S', 'S'),
80 const_PASV = mk_const4('P', 'A', 'S', 'V'),
81 const_PORT = mk_const4('P', 'O', 'R', 'T'),
82 const_PWD = mk_const3('P', 'W', 'D'),
83 const_QUIT = mk_const4('Q', 'U', 'I', 'T'),
84 const_REST = mk_const4('R', 'E', 'S', 'T'),
85 const_RETR = mk_const4('R', 'E', 'T', 'R'),
86 const_RMD = mk_const3('R', 'M', 'D'),
87 const_RNFR = mk_const4('R', 'N', 'F', 'R'),
88 const_RNTO = mk_const4('R', 'N', 'T', 'O'),
89 const_STAT = mk_const4('S', 'T', 'A', 'T'),
90 const_STOR = mk_const4('S', 'T', 'O', 'R'),
91 const_STOU = mk_const4('S', 'T', 'O', 'U'),
92 const_STRU = mk_const4('S', 'T', 'R', 'U'),
93 const_SYST = mk_const4('S', 'Y', 'S', 'T'),
94 const_TYPE = mk_const4('T', 'Y', 'P', 'E'),
95 const_USER = mk_const4('U', 'S', 'E', 'R'),
99 char *p_control_line_buf;
100 len_and_sockaddr *local_addr;
101 len_and_sockaddr *port_addr;
107 #if ENABLE_FEATURE_FTP_WRITE
112 #define G (*(struct globals*)&bb_common_bufsiz1)
113 #define INIT_G() do { } while (0)
117 escape_text(const char *prepend, const char *str, char from, const char *to, char append)
119 size_t retlen, remainlen, chunklen, tolen;
124 remainlen = strlen(str);
127 /* Simply alloc strlen(str)*strlen(to). "to" is max 2 so it's ok */
128 retlen = strlen(prepend);
129 ret = xmalloc(retlen + remainlen * tolen + 1 + 1);
130 strcpy(ret, prepend);
133 found = strchrnul(remain, from);
134 chunklen = found - remain;
136 /* Copy chunk which doesn't contain 'from' to ret */
137 memcpy(&ret[retlen], remain, chunklen);
140 if (*found != '\0') {
141 /* Now copy 'to' instead of 'from' */
142 memcpy(&ret[retlen], to, tolen);
147 ret[retlen] = append;
148 ret[retlen+1] = '\0';
156 replace_char(char *str, char from, char to)
158 while ((str = strchr(str, from)) != NULL)
163 cmdio_write(unsigned status, const char *str)
168 /* FTP allegedly uses telnet protocol for command link.
169 * In telnet, 0xff is an escape char, and needs to be escaped: */
170 response = escape_text(utoa(status), str, '\xff', "\xff\xff", '\r');
172 /* ?! does FTP send embedded LFs as NULs? wow */
173 len = strlen(response);
174 replace_char(response, '\n', '\0');
176 response[len++] = '\n'; /* tack on trailing '\n' */
177 xwrite(STDOUT_FILENO, response, len);
182 cmdio_write_ok(int status)
184 fdprintf(STDOUT_FILENO, "%u Operation successful\r\n", status);
187 /* TODO: output strerr(errno) if errno != 0? */
189 cmdio_write_error(int status)
191 fdprintf(STDOUT_FILENO, "%u Error\r\n", status);
195 cmdio_write_raw(const char *p_text)
197 xwrite_str(STDOUT_FILENO, p_text);
200 /* Simple commands */
205 char *cwd, *response;
207 cwd = xrealloc_getcwd_or_warn(NULL);
211 /* We have to promote each " to "" */
212 response = escape_text(" \"", cwd, '\"', "\"\"", '\"');
214 cmdio_write(FTP_PWDOK, response);
221 if (!G.ftp_arg || chdir(G.ftp_arg) != 0) {
222 cmdio_write_error(FTP_FILEFAIL);
225 cmdio_write_ok(FTP_CWDOK);
231 G.ftp_arg = (char*)"..";
239 // && ( ((G.ftp_arg[0] | 0x20) == 'i' && G.ftp_arg[1] == '\0')
240 // || !strcasecmp(G.ftp_arg, "L8")
241 // || !strcasecmp(G.ftp_arg, "L 8")
244 // cmdio_write_ok(FTP_TYPEOK);
246 // cmdio_write_error(FTP_BADCMD);
253 cmdio_write_raw(STR(FTP_STATOK)"-FTP server status:\r\n"
255 STR(FTP_STATOK)" Ok\r\n");
261 cmdio_write_raw(STR(FTP_HELP)"-Commands:\r\n"
262 "ALLO CDUP CWD HELP LIST\r\n"
263 "MODE NLST NOOP PASS PASV PORT PWD QUIT\r\n"
264 "REST RETR STAT STRU SYST TYPE USER\r\n"
265 #if ENABLE_FEATURE_FTP_WRITE
266 "APPE DELE MKD RMD RNFR RNTO STOR STOU\r\n"
268 STR(FTP_HELP)" Ok\r\n");
271 /* Download commands */
274 init_data_sock_params(int sock_fd)
276 struct linger linger;
280 memset(&linger, 0, sizeof(linger));
282 linger.l_linger = 32767;
284 setsockopt(sock_fd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
285 setsockopt(sock_fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
289 ftpdataio_get_pasv_fd(void)
293 remote_fd = accept(G.pasv_listen_fd, NULL, 0);
296 cmdio_write_error(FTP_BADSENDCONN);
300 init_data_sock_params(remote_fd);
305 ftpdataio_get_port_fd(void)
309 /* Do we want to die or print error to client? */
310 remote_fd = xconnect_stream(G.port_addr);
312 init_data_sock_params(remote_fd);
317 ftpdataio_dispose_transfer_fd(void)
319 /* This close() blocks because we set SO_LINGER */
320 if (G.data_fd > STDOUT_FILENO) {
321 if (close(G.data_fd) < 0) {
322 /* Do it again without blocking. */
323 struct linger linger;
325 memset(&linger, 0, sizeof(linger));
326 setsockopt(G.data_fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
336 return (G.port_addr != NULL);
342 return (G.pasv_listen_fd > STDOUT_FILENO);
346 get_remote_transfer_fd(const char *p_status_msg)
351 remote_fd = ftpdataio_get_pasv_fd();
353 remote_fd = ftpdataio_get_port_fd();
358 cmdio_write(FTP_DATACONN, p_status_msg);
363 data_transfer_checks_ok(void)
365 if (!pasv_active() && !port_active()) {
366 cmdio_write_raw(STR(FTP_BADSENDCONN)" Use PORT or PASV first\r\n");
374 port_pasv_cleanup(void)
378 if (G.pasv_listen_fd > STDOUT_FILENO)
379 close(G.pasv_listen_fd);
380 G.pasv_listen_fd = -1;
386 int bind_retries = 10;
388 enum { min_port = 1024, max_port = 65535 };
389 char *addr, *response;
393 G.pasv_listen_fd = xsocket(G.local_addr->u.sa.sa_family, SOCK_STREAM, 0);
394 setsockopt_reuseaddr(G.pasv_listen_fd);
396 /* TODO bind() with port == 0 and then call getsockname */
397 while (--bind_retries) {
398 port = rand() % max_port;
399 if (port < min_port) {
403 set_nport(G.local_addr, htons(port));
404 /* We don't want to use xbind, it'll die if port is in use */
405 if (bind(G.pasv_listen_fd, &G.local_addr->u.sa, G.local_addr->len) != 0) {
406 /* do we want check if errno == EADDRINUSE ? */
409 xlisten(G.pasv_listen_fd, 1);
414 bb_error_msg_and_die("can't create pasv socket");
416 addr = xmalloc_sockaddr2dotted_noport(&G.local_addr->u.sa);
417 replace_char(addr, '.', ',');
419 response = xasprintf(" Entering Passive Mode (%s,%u,%u)",
420 addr, (int)(port >> 8), (int)(port & 255));
423 cmdio_write(FTP_PASVOK, response);
431 char *raw, *port_part;
432 len_and_sockaddr *lsa = NULL;
439 * xatou16 will accept wrong input,
440 * xatou16 will exit instead of generating error to peer
443 port_part = strrchr(raw, ',');
444 if (port_part == NULL)
446 port = xatou16(&port_part[1]);
449 port_part = strrchr(raw, ',');
450 if (port_part == NULL)
452 port |= xatou16(&port_part[1]) << 8;
455 replace_char(raw, ',', '.');
456 lsa = xdotted2sockaddr(raw, port);
460 cmdio_write_error(FTP_BADCMD);
465 cmdio_write_ok(FTP_PORTOK);
471 /* When ftp_arg == NULL simply restart from beginning */
472 G.restart_pos = G.ftp_arg ? xatoi_u(G.ftp_arg) : 0;
473 cmdio_write_ok(FTP_RESTOK);
480 int trans_ret, retval;
483 off_t offset = G.restart_pos;
488 if (!data_transfer_checks_ok())
491 /* O_NONBLOCK is useful if file happens to be a device node */
492 opened_file = G.ftp_arg ? open(G.ftp_arg, O_RDONLY | O_NONBLOCK) : -1;
493 if (opened_file < 0) {
494 cmdio_write_error(FTP_FILEFAIL);
498 retval = fstat(opened_file, &statbuf);
499 if (retval < 0 || !S_ISREG(statbuf.st_mode)) {
500 /* Note - pretend open failed */
501 cmdio_write_error(FTP_FILEFAIL);
505 /* Now deactive O_NONBLOCK, otherwise we have a problem on DMAPI filesystems
508 ndelay_off(opened_file);
510 /* Set the download offset (from REST) if any */
512 xlseek(opened_file, offset, SEEK_SET);
514 response = xasprintf(
515 " Opening BINARY mode data connection for %s (%"OFF_FMT"u bytes)",
516 G.ftp_arg, statbuf.st_size);
517 remote_fd = get_remote_transfer_fd(response);
520 goto port_pasv_cleanup_out;
522 trans_ret = bb_copyfd_eof(opened_file, remote_fd);
523 ftpdataio_dispose_transfer_fd();
525 cmdio_write_error(FTP_BADSENDFILE);
527 cmdio_write_ok(FTP_TRANSFEROK);
529 port_pasv_cleanup_out:
539 statbuf_getperms(const struct stat *statbuf)
542 enum { r = 'r', w = 'w', x = 'x', s = 's', S = 'S' };
545 memset(perms, '-', 10);
548 switch (statbuf->st_mode & S_IFMT) {
549 case S_IFREG: perms[0] = '-'; break;
550 case S_IFDIR: perms[0] = 'd'; break;
551 case S_IFLNK: perms[0] = 'l'; break;
552 case S_IFIFO: perms[0] = 'p'; break;
553 case S_IFSOCK: perms[0] = s; break;
554 case S_IFCHR: perms[0] = 'c'; break;
555 case S_IFBLK: perms[0] = 'b'; break;
558 if (statbuf->st_mode & S_IRUSR) perms[1] = r;
559 if (statbuf->st_mode & S_IWUSR) perms[2] = w;
560 if (statbuf->st_mode & S_IXUSR) perms[3] = x;
561 if (statbuf->st_mode & S_IRGRP) perms[4] = r;
562 if (statbuf->st_mode & S_IWGRP) perms[5] = w;
563 if (statbuf->st_mode & S_IXGRP) perms[6] = x;
564 if (statbuf->st_mode & S_IROTH) perms[7] = r;
565 if (statbuf->st_mode & S_IWOTH) perms[8] = w;
566 if (statbuf->st_mode & S_IXOTH) perms[9] = x;
567 if (statbuf->st_mode & S_ISUID) perms[3] = (perms[3] == x) ? s : S;
568 if (statbuf->st_mode & S_ISGID) perms[6] = (perms[6] == x) ? s : S;
569 if (statbuf->st_mode & S_ISVTX) perms[9] = (perms[9] == x) ? 't' : 'T';
577 write_filestats(int fd, const char *filename,
578 const struct stat *statbuf)
581 char *stats, *lnkname = NULL, *perms;
586 const char *format = "%b %d %H:%M";
588 name = bb_get_last_path_component_nostrip(filename);
590 if (statbuf != NULL) {
591 size = statbuf->st_size;
593 if (S_ISLNK(statbuf->st_mode))
594 /* Damn symlink... */
595 lnkname = xmalloc_readlink(filename);
597 tm = gmtime(&statbuf->st_mtime);
598 retval = strftime(timestr, sizeof(timestr), format, tm);
600 bb_error_msg_and_die("strftime");
602 timestr[sizeof(timestr) - 1] = '\0';
604 perms = statbuf_getperms(statbuf);
606 stats = xasprintf("%s %u\tftp ftp %"OFF_FMT"u\t%s %s",
607 perms, (int) statbuf->st_nlink,
608 size, timestr, name);
612 stats = xstrdup(name);
614 xwrite_str(fd, stats);
616 if (lnkname != NULL) {
617 xwrite_str(fd, " -> ");
618 xwrite_str(fd, lnkname);
621 xwrite_str(fd, "\r\n");
625 write_dirstats(int fd, const char *dname, int details)
628 struct dirent *dirent;
632 dir = xopendir(dname);
635 dirent = readdir(dir);
639 /* Ignore . and .. */
640 if (dirent->d_name[0] == '.') {
641 if (dirent->d_name[1] == '\0'
642 || (dirent->d_name[1] == '.' && dirent->d_name[2] == '\0')
649 filename = xasprintf("%s/%s", dname, dirent->d_name);
650 if (lstat(filename, &statbuf) != 0) {
655 filename = xstrdup(dirent->d_name);
657 write_filestats(fd, filename, details ? &statbuf : NULL);
665 handle_dir_common(int full_details, int stat_cmd)
670 if (!stat_cmd && !data_transfer_checks_ok())
675 cmdio_write_raw(STR(FTP_STATFILE_OK)"-Status follows:\r\n");
677 fd = get_remote_transfer_fd(" Here comes the directory listing");
683 if (lstat(G.ftp_arg, &statbuf) != 0) {
684 /* Dir doesn't exist => return ok to client */
687 if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
688 write_filestats(fd, G.ftp_arg, &statbuf);
689 else if (S_ISDIR(statbuf.st_mode))
690 write_dirstats(fd, G.ftp_arg, full_details);
692 write_dirstats(fd, ".", full_details);
695 /* Well, if we can't open directory/file it doesn't matter */
697 ftpdataio_dispose_transfer_fd();
699 cmdio_write_ok(FTP_TRANSFEROK);
701 cmdio_write_ok(FTP_STATFILE_OK);
707 handle_dir_common(1, 0);
713 handle_dir_common(0, 0);
717 handle_stat_file(void)
719 handle_dir_common(1, 1);
722 /* Upload commands */
724 #if ENABLE_FEATURE_FTP_WRITE
728 if (!G.ftp_arg || mkdir(G.ftp_arg, 0777) != 0) {
729 cmdio_write_error(FTP_FILEFAIL);
732 cmdio_write_ok(FTP_MKDIROK);
738 if (!G.ftp_arg || rmdir(G.ftp_arg) != 0) {
739 cmdio_write_error(FTP_FILEFAIL);
742 cmdio_write_ok(FTP_RMDIROK);
748 if (!G.ftp_arg || unlink(G.ftp_arg) != 0) {
749 cmdio_write_error(FTP_FILEFAIL);
752 cmdio_write_ok(FTP_DELEOK);
758 free(G.rnfr_filename);
759 G.rnfr_filename = xstrdup(G.ftp_arg);
760 cmdio_write_ok(FTP_RNFROK);
768 /* If we didn't get a RNFR, throw a wobbly */
769 if (G.rnfr_filename == NULL || G.ftp_arg == NULL) {
770 cmdio_write_raw(STR(FTP_NEEDRNFR)" RNFR required first\r\n");
774 retval = rename(G.rnfr_filename, G.ftp_arg);
775 free(G.rnfr_filename);
776 G.rnfr_filename = NULL;
779 cmdio_write_error(FTP_FILEFAIL);
782 cmdio_write_ok(FTP_RENAMEOK);
786 handle_upload_common(int is_append, int is_unique)
788 char *tempname = NULL;
794 offset = G.restart_pos;
797 if (!data_transfer_checks_ok())
802 tempname = xstrdup(" FILE: uniq.XXXXXX");
803 local_file_fd = mkstemp(tempname + 7);
804 } else if (G.ftp_arg) {
805 int flags = O_WRONLY | O_CREAT | O_TRUNC;
807 flags = O_WRONLY | O_CREAT | O_APPEND;
809 flags = O_WRONLY | O_CREAT;
810 local_file_fd = open(G.ftp_arg, flags, 0666);
812 if (local_file_fd < 0) {
813 cmdio_write_error(FTP_UPLOADFAIL);
817 /* TODO: paranoia: fstat it, refuse to do anything if it's not a regular file */
820 xlseek(local_file_fd, offset, SEEK_SET);
822 remote_fd = get_remote_transfer_fd(tempname ? tempname : " Ok to send data");
828 trans_ret = bb_copyfd_eof(remote_fd, local_file_fd);
829 ftpdataio_dispose_transfer_fd();
832 cmdio_write_error(FTP_BADSENDFILE);
834 cmdio_write_ok(FTP_TRANSFEROK);
838 close(local_file_fd);
844 handle_upload_common(0, 0);
851 handle_upload_common(1, 0);
858 handle_upload_common(0, 1);
860 #endif /* ENABLE_FEATURE_FTP_WRITE */
863 cmdio_get_cmd_and_arg(void)
870 len = 8 * 1024; /* Paranoia. Peer may send 1 gigabyte long cmd... */
871 G.ftp_cmd = cmd = xmalloc_reads(STDIN_FILENO, NULL, &len);
875 len = strlen(cmd) - 1;
876 while (len >= 0 && cmd[len] == '\r') {
881 G.ftp_arg = strchr(cmd, ' ');
882 if (G.ftp_arg != NULL) {
888 cmdval = (cmdval << 8) + ((unsigned char)*cmd++ & (unsigned char)~0x20);
893 int ftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
894 int ftpd_main(int argc UNUSED_PARAM, char **argv)
898 G.local_addr = get_sock_lsa(STDIN_FILENO);
900 /* This is confusing:
901 * bb_error_msg_and_die("stdin is not a socket");
904 /* Help text says that ftpd must be used as inetd service,
905 * which is by far the most usual cause of get_sock_lsa
909 G.opts = getopt32(argv, "v" USE_FEATURE_FTP_WRITE("w"));
911 openlog(applet_name, LOG_PID, LOG_DAEMON);
912 logmode |= LOGMODE_SYSLOG;
913 if (!(G.opts & OPT_v))
914 logmode = LOGMODE_SYSLOG;
917 xchdir(argv[optind]);
921 //umask(077); - admin can set umask before starting us
923 /* Signals. We'll always take -EPIPE rather than a rude signal, thanks */
924 signal(SIGPIPE, SIG_IGN);
926 /* Set up options on the command socket (do we need these all? why?) */
927 setsockopt(STDIN_FILENO, IPPROTO_TCP, TCP_NODELAY, &const_int_1, sizeof(const_int_1));
928 setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
929 setsockopt(STDIN_FILENO, SOL_SOCKET, SO_OOBINLINE, &const_int_1, sizeof(const_int_1));
931 cmdio_write_ok(FTP_GREET);
933 #ifdef IF_WE_WANT_TO_REQUIRE_LOGIN
935 smallint user_was_specified = 0;
937 uint32_t cmdval = cmdio_get_cmd_and_arg();
939 if (cmdval == const_USER) {
940 if (G.ftp_arg == NULL || strcasecmp(G.ftp_arg, "anonymous") != 0)
941 cmdio_write_raw(STR(FTP_LOGINERR)" Server is anonymous only\r\n");
943 user_was_specified = 1;
944 cmdio_write_raw(STR(FTP_GIVEPWORD)" Please specify the password\r\n");
946 } else if (cmdval == const_PASS) {
947 if (user_was_specified)
949 cmdio_write_raw(STR(FTP_NEEDUSER)" Login with USER\r\n");
950 } else if (cmdval == const_QUIT) {
951 cmdio_write_ok(FTP_GOODBYE);
954 cmdio_write_raw(STR(FTP_LOGINERR)" Login with USER and PASS\r\n");
958 cmdio_write_ok(FTP_LOGINOK);
961 /* RFC-959 Section 5.1
962 * The following commands and options MUST be supported by every
963 * server-FTP and user-FTP, except in cases where the underlying
964 * file system or operating system does not allow or support
965 * a particular command.
966 * Type: ASCII Non-print, IMAGE, LOCAL 8
968 * Structure: File, Record*
969 * (Record structure is REQUIRED only for hosts whose file
970 * systems support record structure).
972 * USER, PASS, ACCT, [bbox: ACCT not supported]
977 * CWD, CDUP, RMD, MKD, PWD,
983 * "The argument field is a Telnet string identifying the user's account.
984 * The command is not necessarily related to the USER command, as some
985 * sites may require an account for login and others only for specific
986 * access, such as storing files. In the latter case the command may
987 * arrive at any time.
988 * There are reply codes to differentiate these cases for the automation:
989 * when account information is required for login, the response to
990 * a successful PASSword command is reply code 332. On the other hand,
991 * if account information is NOT required for login, the reply to
992 * a successful PASSword command is 230; and if the account information
993 * is needed for a command issued later in the dialogue, the server
994 * should return a 332 or 532 reply depending on whether it stores
995 * (pending receipt of the ACCounT command) or discards the command,
1000 uint32_t cmdval = cmdio_get_cmd_and_arg();
1002 if (cmdval == const_QUIT) {
1003 cmdio_write_ok(FTP_GOODBYE);
1006 if (cmdval == const_PWD)
1008 else if (cmdval == const_CWD)
1010 else if (cmdval == const_CDUP) /* cd .. */
1012 else if (cmdval == const_PASV)
1014 else if (cmdval == const_RETR)
1016 else if (cmdval == const_NOOP)
1017 cmdio_write_ok(FTP_NOOPOK);
1018 else if (cmdval == const_SYST)
1019 cmdio_write_raw(STR(FTP_SYSTOK)" UNIX Type: L8\r\n");
1020 else if (cmdval == const_HELP)
1022 else if (cmdval == const_LIST) /* ls -l */
1024 else if (cmdval == const_TYPE)
1026 cmdio_write_ok(FTP_TYPEOK);
1027 else if (cmdval == const_PORT)
1029 else if (cmdval == const_REST)
1031 else if (cmdval == const_NLST) /* "name list", bare ls */
1033 else if (cmdval == const_STRU) {
1035 // && (G.ftp_arg[0] | 0x20) == 'f'
1036 // && G.ftp_arg[1] == '\0'
1038 cmdio_write_ok(FTP_STRUOK);
1040 // cmdio_write_raw(STR(FTP_BADSTRU)" Bad STRU command\r\n");
1041 } else if (cmdval == const_MODE) {
1043 // && (G.ftp_arg[0] | 0x20) == 's'
1044 // && G.ftp_arg[1] == '\0'
1046 cmdio_write_ok(FTP_MODEOK);
1048 // cmdio_write_raw(STR(FTP_BADMODE)" Bad MODE command\r\n");
1050 else if (cmdval == const_ALLO)
1051 cmdio_write_ok(FTP_ALLOOK);
1052 else if (cmdval == const_STAT) {
1053 if (G.ftp_arg == NULL)
1057 } else if (cmdval == const_USER) {
1058 /* FTP_LOGINERR confuses clients: */
1059 /* cmdio_write_raw(STR(FTP_LOGINERR)" Can't change to another user\r\n"); */
1060 cmdio_write_ok(FTP_GIVEPWORD);
1061 } else if (cmdval == const_PASS)
1062 cmdio_write_ok(FTP_LOGINOK);
1063 #if ENABLE_FEATURE_FTP_WRITE
1064 else if (G.opts & OPT_w) {
1065 if (cmdval == const_STOR)
1067 else if (cmdval == const_MKD)
1069 else if (cmdval == const_RMD)
1071 else if (cmdval == const_DELE)
1073 else if (cmdval == const_RNFR) /* "rename from" */
1075 else if (cmdval == const_RNTO) /* "rename to" */
1077 else if (cmdval == const_APPE)
1079 else if (cmdval == const_STOU) /* "store unique" */
1084 else if (cmdval == const_STOR
1085 || cmdval == const_MKD
1086 || cmdval == const_RMD
1087 || cmdval == const_DELE
1088 || cmdval == const_RNFR
1089 || cmdval == const_RNTO
1090 || cmdval == const_APPE
1091 || cmdval == const_STOU
1093 cmdio_write_raw(STR(FTP_NOPERM)" Permission denied\r\n");
1097 /* Which unsupported commands were seen in the wild?
1098 * (doesn't necessarily mean "we must support them")
1099 * wget 1.11.4: SIZE - todo
1100 * lftp 3.6.3: FEAT - is it useful?
1101 * MDTM - works fine without it anyway
1102 * IPv6-style PASV: "EPSV 2" - todo
1104 cmdio_write_raw(STR(FTP_BADCMD)" Unknown command\r\n");