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)
116 replace_text(const char *str, const char from, const char *to)
118 size_t retlen, remainlen, chunklen, tolen;
123 remainlen = strlen(str);
126 /* Simply alloc strlen(str)*strlen(to). "to" is max 2 so it's ok */
127 ret = xmalloc(remainlen * tolen + 1);
131 found = strchr(remain, from);
133 chunklen = found - remain;
135 /* Copy chunk which doesn't contain 'from' to ret */
136 memcpy(&ret[retlen], remain, chunklen);
139 /* Now copy 'to' instead of 'from' */
140 memcpy(&ret[retlen], to, tolen);
146 * The last chunk. We are already sure that we have enough space
147 * so we can use strcpy.
149 strcpy(&ret[retlen], remain);
157 replace_char(char *str, char from, char to)
159 while ((str = strchr(str, from)) != NULL)
164 cmdio_write(unsigned status, const char *str)
166 char *escaped_str, *response;
169 /* FTP allegedly uses telnet protocol for command link.
170 * In telnet, 0xff is an escape char, and needs to be escaped: */
171 escaped_str = replace_text(str, '\xff', "\xff\xff");
173 response = xasprintf("%u%s\r", status, escaped_str);
176 /* ?! does FTP send embedded LFs as NULs? wow */
177 len = strlen(response);
178 replace_char(response, '\n', '\0');
180 response[len++] = '\n'; /* tack on trailing '\n' */
181 xwrite(STDOUT_FILENO, response, len);
186 cmdio_write_ok(int status)
188 fdprintf(STDOUT_FILENO, "%u Operation successful\r\n", status);
191 /* TODO: output strerr(errno) if errno != 0? */
193 cmdio_write_error(int status)
195 fdprintf(STDOUT_FILENO, "%u Error\r\n", status);
199 cmdio_write_raw(const char *p_text)
201 xwrite_str(STDOUT_FILENO, p_text);
204 /* Simple commands */
209 char *cwd, *promoted_cwd, *response;
211 cwd = xrealloc_getcwd_or_warn(NULL);
215 /* We have to promote each " to "" */
216 promoted_cwd = replace_text(cwd, '\"', "\"\"");
218 response = xasprintf(" \"%s\"", promoted_cwd);
220 cmdio_write(FTP_PWDOK, response);
227 if (!G.ftp_arg || chdir(G.ftp_arg) != 0) {
228 cmdio_write_error(FTP_FILEFAIL);
231 cmdio_write_ok(FTP_CWDOK);
237 G.ftp_arg = (char*)"..";
245 // && ( ((G.ftp_arg[0] | 0x20) == 'i' && G.ftp_arg[1] == '\0')
246 // || !strcasecmp(G.ftp_arg, "L8")
247 // || !strcasecmp(G.ftp_arg, "L 8")
250 // cmdio_write_ok(FTP_TYPEOK);
252 // cmdio_write_error(FTP_BADCMD);
259 cmdio_write_raw(STR(FTP_STATOK)"-FTP server status:\r\n"
261 STR(FTP_STATOK)" Ok\r\n");
267 cmdio_write_raw(STR(FTP_HELP)"-Commands:\r\n"
268 "ALLO CDUP CWD HELP LIST\r\n"
269 "MODE NLST NOOP PASS PASV PORT PWD QUIT\r\n"
270 "REST RETR STAT STRU SYST TYPE USER\r\n"
271 #if ENABLE_FEATURE_FTP_WRITE
272 "APPE DELE MKD RMD RNFR RNTO STOR STOU\r\n"
274 STR(FTP_HELP)" Ok\r\n");
277 /* Download commands */
280 init_data_sock_params(int sock_fd)
282 struct linger linger;
286 memset(&linger, 0, sizeof(linger));
288 linger.l_linger = 32767;
290 setsockopt(sock_fd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
291 setsockopt(sock_fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
295 ftpdataio_get_pasv_fd(void)
299 remote_fd = accept(G.pasv_listen_fd, NULL, 0);
302 cmdio_write_error(FTP_BADSENDCONN);
306 init_data_sock_params(remote_fd);
311 ftpdataio_get_port_fd(void)
315 /* Do we want to die or print error to client? */
316 remote_fd = xconnect_stream(G.port_addr);
318 init_data_sock_params(remote_fd);
323 ftpdataio_dispose_transfer_fd(void)
325 /* This close() blocks because we set SO_LINGER */
326 if (G.data_fd > STDOUT_FILENO) {
327 if (close(G.data_fd) < 0) {
328 /* Do it again without blocking. */
329 struct linger linger;
331 memset(&linger, 0, sizeof(linger));
332 setsockopt(G.data_fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
342 return (G.port_addr != NULL);
348 return (G.pasv_listen_fd > STDOUT_FILENO);
352 get_remote_transfer_fd(const char *p_status_msg)
357 remote_fd = ftpdataio_get_pasv_fd();
359 remote_fd = ftpdataio_get_port_fd();
364 cmdio_write(FTP_DATACONN, p_status_msg);
369 data_transfer_checks_ok(void)
371 if (!pasv_active() && !port_active()) {
372 cmdio_write_raw(STR(FTP_BADSENDCONN)" Use PORT or PASV first\r\n");
380 port_pasv_cleanup(void)
384 if (G.pasv_listen_fd > STDOUT_FILENO)
385 close(G.pasv_listen_fd);
386 G.pasv_listen_fd = -1;
392 int bind_retries = 10;
394 enum { min_port = 1024, max_port = 65535 };
395 char *addr, *response;
399 G.pasv_listen_fd = xsocket(G.local_addr->u.sa.sa_family, SOCK_STREAM, 0);
400 setsockopt_reuseaddr(G.pasv_listen_fd);
402 /* TODO bind() with port == 0 and then call getsockname */
403 while (--bind_retries) {
404 port = rand() % max_port;
405 if (port < min_port) {
409 set_nport(G.local_addr, htons(port));
410 /* We don't want to use xbind, it'll die if port is in use */
411 if (bind(G.pasv_listen_fd, &G.local_addr->u.sa, G.local_addr->len) != 0) {
412 /* do we want check if errno == EADDRINUSE ? */
415 xlisten(G.pasv_listen_fd, 1);
420 bb_error_msg_and_die("can't create pasv socket");
422 addr = xmalloc_sockaddr2dotted_noport(&G.local_addr->u.sa);
423 replace_char(addr, '.', ',');
425 response = xasprintf(" Entering Passive Mode (%s,%u,%u)",
426 addr, (int)(port >> 8), (int)(port & 255));
429 cmdio_write(FTP_PASVOK, response);
437 char *raw, *port_part;
438 len_and_sockaddr *lsa = NULL;
445 * xatou16 will accept wrong input,
446 * xatou16 will exit instead of generating error to peer
449 port_part = strrchr(raw, ',');
450 if (port_part == NULL)
452 port = xatou16(&port_part[1]);
455 port_part = strrchr(raw, ',');
456 if (port_part == NULL)
458 port |= xatou16(&port_part[1]) << 8;
461 replace_char(raw, ',', '.');
462 lsa = xdotted2sockaddr(raw, port);
466 cmdio_write_error(FTP_BADCMD);
471 cmdio_write_ok(FTP_PORTOK);
477 /* When ftp_arg == NULL simply restart from beginning */
478 G.restart_pos = G.ftp_arg ? xatoi_u(G.ftp_arg) : 0;
479 cmdio_write_ok(FTP_RESTOK);
486 int trans_ret, retval;
489 off_t offset = G.restart_pos;
494 if (!data_transfer_checks_ok())
497 /* O_NONBLOCK is useful if file happens to be a device node */
498 opened_file = G.ftp_arg ? open(G.ftp_arg, O_RDONLY | O_NONBLOCK) : -1;
499 if (opened_file < 0) {
500 cmdio_write_error(FTP_FILEFAIL);
504 retval = fstat(opened_file, &statbuf);
505 if (retval < 0 || !S_ISREG(statbuf.st_mode)) {
506 /* Note - pretend open failed */
507 cmdio_write_error(FTP_FILEFAIL);
511 /* Now deactive O_NONBLOCK, otherwise we have a problem on DMAPI filesystems
514 ndelay_off(opened_file);
516 /* Set the download offset (from REST) if any */
518 xlseek(opened_file, offset, SEEK_SET);
520 response = xasprintf(
521 " Opening BINARY mode data connection for %s (%"OFF_FMT"u bytes)",
522 G.ftp_arg, statbuf.st_size);
523 remote_fd = get_remote_transfer_fd(response);
526 goto port_pasv_cleanup_out;
528 trans_ret = bb_copyfd_eof(opened_file, remote_fd);
529 ftpdataio_dispose_transfer_fd();
531 cmdio_write_error(FTP_BADSENDFILE);
533 cmdio_write_ok(FTP_TRANSFEROK);
535 port_pasv_cleanup_out:
545 statbuf_getperms(const struct stat *statbuf)
548 enum { r = 'r', w = 'w', x = 'x', s = 's', S = 'S' };
551 memset(perms, '-', 10);
554 switch (statbuf->st_mode & S_IFMT) {
555 case S_IFREG: perms[0] = '-'; break;
556 case S_IFDIR: perms[0] = 'd'; break;
557 case S_IFLNK: perms[0] = 'l'; break;
558 case S_IFIFO: perms[0] = 'p'; break;
559 case S_IFSOCK: perms[0] = s; break;
560 case S_IFCHR: perms[0] = 'c'; break;
561 case S_IFBLK: perms[0] = 'b'; break;
564 if (statbuf->st_mode & S_IRUSR) perms[1] = r;
565 if (statbuf->st_mode & S_IWUSR) perms[2] = w;
566 if (statbuf->st_mode & S_IXUSR) perms[3] = x;
567 if (statbuf->st_mode & S_IRGRP) perms[4] = r;
568 if (statbuf->st_mode & S_IWGRP) perms[5] = w;
569 if (statbuf->st_mode & S_IXGRP) perms[6] = x;
570 if (statbuf->st_mode & S_IROTH) perms[7] = r;
571 if (statbuf->st_mode & S_IWOTH) perms[8] = w;
572 if (statbuf->st_mode & S_IXOTH) perms[9] = x;
573 if (statbuf->st_mode & S_ISUID) perms[3] = (perms[3] == x) ? s : S;
574 if (statbuf->st_mode & S_ISGID) perms[6] = (perms[6] == x) ? s : S;
575 if (statbuf->st_mode & S_ISVTX) perms[9] = (perms[9] == x) ? 't' : 'T';
583 write_filestats(int fd, const char *filename,
584 const struct stat *statbuf)
587 char *stats, *lnkname = NULL, *perms;
592 const char *format = "%b %d %H:%M";
594 name = bb_get_last_path_component_nostrip(filename);
596 if (statbuf != NULL) {
597 size = statbuf->st_size;
599 if (S_ISLNK(statbuf->st_mode))
600 /* Damn symlink... */
601 lnkname = xmalloc_readlink(filename);
603 tm = gmtime(&statbuf->st_mtime);
604 retval = strftime(timestr, sizeof(timestr), format, tm);
606 bb_error_msg_and_die("strftime");
608 timestr[sizeof(timestr) - 1] = '\0';
610 perms = statbuf_getperms(statbuf);
612 stats = xasprintf("%s %u\tftp ftp %"OFF_FMT"u\t%s %s",
613 perms, (int) statbuf->st_nlink,
614 size, timestr, name);
618 stats = xstrdup(name);
620 xwrite_str(fd, stats);
622 if (lnkname != NULL) {
623 xwrite_str(fd, " -> ");
624 xwrite_str(fd, lnkname);
627 xwrite_str(fd, "\r\n");
631 write_dirstats(int fd, const char *dname, int details)
634 struct dirent *dirent;
638 dir = xopendir(dname);
641 dirent = readdir(dir);
645 /* Ignore . and .. */
646 if (dirent->d_name[0] == '.') {
647 if (dirent->d_name[1] == '\0'
648 || (dirent->d_name[1] == '.' && dirent->d_name[2] == '\0')
655 filename = xasprintf("%s/%s", dname, dirent->d_name);
656 if (lstat(filename, &statbuf) != 0) {
661 filename = xstrdup(dirent->d_name);
663 write_filestats(fd, filename, details ? &statbuf : NULL);
671 handle_dir_common(int full_details, int stat_cmd)
676 if (!stat_cmd && !data_transfer_checks_ok())
681 cmdio_write_raw(STR(FTP_STATFILE_OK)"-Status follows:\r\n");
683 fd = get_remote_transfer_fd(" Here comes the directory listing");
689 if (lstat(G.ftp_arg, &statbuf) != 0) {
690 /* Dir doesn't exist => return ok to client */
693 if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
694 write_filestats(fd, G.ftp_arg, &statbuf);
695 else if (S_ISDIR(statbuf.st_mode))
696 write_dirstats(fd, G.ftp_arg, full_details);
698 write_dirstats(fd, ".", full_details);
701 /* Well, if we can't open directory/file it doesn't matter */
703 ftpdataio_dispose_transfer_fd();
705 cmdio_write_ok(FTP_TRANSFEROK);
707 cmdio_write_ok(FTP_STATFILE_OK);
713 handle_dir_common(1, 0);
719 handle_dir_common(0, 0);
723 handle_stat_file(void)
725 handle_dir_common(1, 1);
728 /* Upload commands */
730 #if ENABLE_FEATURE_FTP_WRITE
734 if (!G.ftp_arg || mkdir(G.ftp_arg, 0777) != 0) {
735 cmdio_write_error(FTP_FILEFAIL);
738 cmdio_write_ok(FTP_MKDIROK);
744 if (!G.ftp_arg || rmdir(G.ftp_arg) != 0) {
745 cmdio_write_error(FTP_FILEFAIL);
748 cmdio_write_ok(FTP_RMDIROK);
754 if (!G.ftp_arg || unlink(G.ftp_arg) != 0) {
755 cmdio_write_error(FTP_FILEFAIL);
758 cmdio_write_ok(FTP_DELEOK);
764 free(G.rnfr_filename);
765 G.rnfr_filename = xstrdup(G.ftp_arg);
766 cmdio_write_ok(FTP_RNFROK);
774 /* If we didn't get a RNFR, throw a wobbly */
775 if (G.rnfr_filename == NULL || G.ftp_arg == NULL) {
776 cmdio_write_raw(STR(FTP_NEEDRNFR)" RNFR required first\r\n");
780 retval = rename(G.rnfr_filename, G.ftp_arg);
781 free(G.rnfr_filename);
782 G.rnfr_filename = NULL;
785 cmdio_write_error(FTP_FILEFAIL);
788 cmdio_write_ok(FTP_RENAMEOK);
792 handle_upload_common(int is_append, int is_unique)
794 char *tempname = NULL;
800 offset = G.restart_pos;
803 if (!data_transfer_checks_ok())
808 tempname = xstrdup(" FILE: uniq.XXXXXX");
809 local_file_fd = mkstemp(tempname + 7);
810 } else if (G.ftp_arg) {
811 int flags = O_WRONLY | O_CREAT | O_TRUNC;
813 flags = O_WRONLY | O_CREAT | O_APPEND;
815 flags = O_WRONLY | O_CREAT;
816 local_file_fd = open(G.ftp_arg, flags, 0666);
818 if (local_file_fd < 0) {
819 cmdio_write_error(FTP_UPLOADFAIL);
823 /* TODO: paranoia: fstat it, refuse to do anything if it's not a regular file */
826 xlseek(local_file_fd, offset, SEEK_SET);
828 remote_fd = get_remote_transfer_fd(tempname ? tempname : " Ok to send data");
834 trans_ret = bb_copyfd_eof(remote_fd, local_file_fd);
835 ftpdataio_dispose_transfer_fd();
838 cmdio_write_error(FTP_BADSENDFILE);
840 cmdio_write_ok(FTP_TRANSFEROK);
844 close(local_file_fd);
850 handle_upload_common(0, 0);
857 handle_upload_common(1, 0);
864 handle_upload_common(0, 1);
866 #endif /* ENABLE_FEATURE_FTP_WRITE */
869 cmdio_get_cmd_and_arg(void)
876 len = 8 * 1024; /* Paranoia. Peer may send 1 gigabyte long cmd... */
877 G.ftp_cmp = cmd = xmalloc_reads(STDIN_FILENO, NULL, &len);
881 len = strlen(cmd) - 1;
882 while (len >= 0 && cmd[len] == '\r') {
887 G.ftp_arg = strchr(cmd, ' ');
888 if (G.ftp_arg != NULL) {
894 cmdval = (cmdval << 8) + ((unsigned char)*cmd++ & (unsigned char)~0x20);
899 int ftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
900 int ftpd_main(int argc UNUSED_PARAM, char **argv)
904 G.local_addr = get_sock_lsa(STDIN_FILENO);
906 /* This is confusing:
907 * bb_error_msg_and_die("stdin is not a socket");
910 /* Help text says that ftpd must be used as inetd service,
911 * which is by far the most usual cause of get_sock_lsa
915 G.opts = getopt32(argv, "v" USE_FEATURE_FTP_WRITE("w"));
917 openlog(applet_name, LOG_PID, LOG_DAEMON);
918 logmode |= LOGMODE_SYSLOG;
919 if (!(G.opts & OPT_v))
920 logmode = LOGMODE_SYSLOG;
923 xchdir(argv[optind]);
927 //umask(077); - admin can set umask before starting us
929 /* Signals. We'll always take -EPIPE rather than a rude signal, thanks */
930 signal(SIGPIPE, SIG_IGN);
932 /* Set up options on the command socket (do we need these all? why?) */
933 setsockopt(STDIN_FILENO, IPPROTO_TCP, TCP_NODELAY, &const_int_1, sizeof(const_int_1));
934 setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
935 setsockopt(STDIN_FILENO, SOL_SOCKET, SO_OOBINLINE, &const_int_1, sizeof(const_int_1));
937 cmdio_write_raw(STR(FTP_GREET)" Welcome\r\n");
939 #ifdef IF_WE_WANT_TO_REQUIRE_LOGIN
941 smallint user_was_specified = 0;
943 uint32_t cmdval = cmdio_get_cmd_and_arg();
945 if (cmdval == const_USER) {
946 if (G.ftp_arg == NULL || strcasecmp(G.ftp_arg, "anonymous") != 0)
947 cmdio_write_raw(STR(FTP_LOGINERR)" Server is anonymous only\r\n");
949 user_was_specified = 1;
950 cmdio_write_raw(STR(FTP_GIVEPWORD)" Please specify the password\r\n");
952 } else if (cmdval == const_PASS) {
953 if (user_was_specified)
955 cmdio_write_raw(STR(FTP_NEEDUSER)" Login with USER\r\n");
956 } else if (cmdval == const_QUIT) {
957 cmdio_write_raw(STR(FTP_GOODBYE)" Goodbye\r\n");
960 cmdio_write_raw(STR(FTP_LOGINERR)" Login with USER and PASS\r\n");
964 cmdio_write_ok(FTP_LOGINOK);
967 /* RFC-959 Section 5.1
968 * The following commands and options MUST be supported by every
969 * server-FTP and user-FTP, except in cases where the underlying
970 * file system or operating system does not allow or support
971 * a particular command.
972 * Type: ASCII Non-print, IMAGE, LOCAL 8
974 * Structure: File, Record*
975 * (Record structure is REQUIRED only for hosts whose file
976 * systems support record structure).
978 * USER, PASS, ACCT, [bbox: ACCT not supported]
983 * CWD, CDUP, RMD, MKD, PWD,
989 * The argument field is a Telnet string identifying the user's account.
990 * The command is not necessarily related to the USER command, as some
991 * sites may require an account for login and others only for specific
992 * access, such as storing files. In the latter case the command may
993 * arrive at any time.
994 * There are reply codes to differentiate these cases for the automation:
995 * when account information is required for login, the response to
996 * a successful PASSword command is reply code 332. On the other hand,
997 * if account information is NOT required for login, the reply to
998 * a successful PASSword command is 230; and if the account information
999 * is needed for a command issued later in the dialogue, the server
1000 * should return a 332 or 532 reply depending on whether it stores
1001 * (pending receipt of the ACCounT command) or discards the command,
1006 uint32_t cmdval = cmdio_get_cmd_and_arg();
1008 if (cmdval == const_QUIT) {
1009 cmdio_write_raw(STR(FTP_GOODBYE)" Goodbye\r\n");
1012 if (cmdval == const_PWD)
1014 else if (cmdval == const_CWD)
1016 else if (cmdval == const_CDUP) /* cd .. */
1018 else if (cmdval == const_PASV)
1020 else if (cmdval == const_RETR)
1022 else if (cmdval == const_NOOP)
1023 cmdio_write_ok(FTP_NOOPOK);
1024 else if (cmdval == const_SYST)
1025 cmdio_write_raw(STR(FTP_SYSTOK)" UNIX Type: L8\r\n");
1026 else if (cmdval == const_HELP)
1028 else if (cmdval == const_LIST) /* ls -l */
1030 else if (cmdval == const_TYPE)
1032 cmdio_write_ok(FTP_TYPEOK);
1033 else if (cmdval == const_PORT)
1035 else if (cmdval == const_REST)
1037 else if (cmdval == const_NLST) /* "name list", bare ls */
1039 else if (cmdval == const_STRU) {
1041 // && (G.ftp_arg[0] | 0x20) == 'f'
1042 // && G.ftp_arg[1] == '\0'
1044 cmdio_write_ok(FTP_STRUOK);
1046 // cmdio_write_raw(STR(FTP_BADSTRU)" Bad STRU command\r\n");
1047 } else if (cmdval == const_MODE) {
1049 // && (G.ftp_arg[0] | 0x20) == 's'
1050 // && G.ftp_arg[1] == '\0'
1052 cmdio_write_ok(FTP_MODEOK);
1054 // cmdio_write_raw(STR(FTP_BADMODE)" Bad MODE command\r\n");
1056 else if (cmdval == const_ALLO)
1057 cmdio_write_ok(FTP_ALLOOK);
1058 else if (cmdval == const_STAT) {
1059 if (G.ftp_arg == NULL)
1063 } else if (cmdval == const_USER) {
1064 /* FTP_LOGINERR confuses clients: */
1065 /* cmdio_write_raw(STR(FTP_LOGINERR)" Can't change to another user\r\n"); */
1066 cmdio_write_ok(FTP_GIVEPWORD);
1067 } else if (cmdval == const_PASS)
1068 cmdio_write_ok(FTP_LOGINOK);
1069 #if ENABLE_FEATURE_FTP_WRITE
1070 else if (G.opts & OPT_w) {
1071 if (cmdval == const_STOR)
1073 else if (cmdval == const_MKD)
1075 else if (cmdval == const_RMD)
1077 else if (cmdval == const_DELE)
1079 else if (cmdval == const_RNFR) /* "rename from" */
1081 else if (cmdval == const_RNTO) /* "rename to" */
1083 else if (cmdval == const_APPE)
1085 else if (cmdval == const_STOU) /* "store unique" */
1090 else if (cmdval == const_STOR
1091 || cmdval == const_MKD
1092 || cmdval == const_RMD
1093 || cmdval == const_DELE
1094 || cmdval == const_RNFR
1095 || cmdval == const_RNTO
1096 || cmdval == const_APPE
1097 || cmdval == const_STOU
1099 cmdio_write_raw(STR(FTP_NOPERM)" Permission denied\r\n");
1103 /* Which unsupported commands were seen in the wild?
1104 * (doesn't necessarily mean "we must support them")
1105 * wget 1.11.4: SIZE - todo
1106 * lftp 3.6.3: FEAT - is it useful?
1107 * MDTM - works fine without it anyway
1108 * IPv6-style PASV: "EPSV 2" - todo
1110 cmdio_write_raw(STR(FTP_BADCMD)" Unknown command\r\n");