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 * Licensed under GPLv2, see file LICENSE in this tarball for details.
9 * Only subset of FTP protocol is implemented but vast majority of clients
10 * should not have any problem. You have to run this daemon via inetd.
15 #include <netinet/tcp.h>
17 #define FTP_DATACONN 150
18 #define FTP_NOOPOK 200
19 #define FTP_TYPEOK 200
20 #define FTP_PORTOK 200
21 #define FTP_STRUOK 200
22 #define FTP_MODEOK 200
23 #define FTP_ALLOOK 202
24 #define FTP_STATOK 211
25 #define FTP_STATFILE_OK 213
27 #define FTP_SYSTOK 215
29 #define FTP_GOODBYE 221
30 #define FTP_TRANSFEROK 226
31 #define FTP_PASVOK 227
32 /*#define FTP_EPRTOK 228*/
33 #define FTP_EPSVOK 229
34 #define FTP_LOGINOK 230
36 #define FTP_RMDIROK 250
37 #define FTP_DELEOK 250
38 #define FTP_RENAMEOK 250
40 #define FTP_MKDIROK 257
41 #define FTP_GIVEPWORD 331
42 #define FTP_RESTOK 350
43 #define FTP_RNFROK 350
44 #define FTP_TIMEOUT 421
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)
62 /* Convert a constant to 3-digit string, packed into uint32_t */
64 /* Shift for Nth decimal digit */
65 SHIFT2 = 0 * BB_LITTLE_ENDIAN + 24 * BB_BIG_ENDIAN,
66 SHIFT1 = 8 * BB_LITTLE_ENDIAN + 16 * BB_BIG_ENDIAN,
67 SHIFT0 = 16 * BB_LITTLE_ENDIAN + 8 * BB_BIG_ENDIAN,
69 #define STRNUM32(s) (uint32_t)(0 \
70 | (('0' + ((s) / 1 % 10)) << SHIFT0) \
71 | (('0' + ((s) / 10 % 10)) << SHIFT1) \
72 | (('0' + ((s) / 100 % 10)) << SHIFT2) \
84 len_and_sockaddr *local_addr;
85 len_and_sockaddr *port_addr;
88 #if ENABLE_FEATURE_FTP_WRITE
92 #define G (*(struct globals*)&bb_common_bufsiz1)
93 #define INIT_G() do { } while (0)
97 escape_text(const char *prepend, const char *str, unsigned escapee)
99 unsigned retlen, remainlen, chunklen;
103 append = (char)escapee;
106 remainlen = strlen(str);
107 retlen = strlen(prepend);
108 ret = xmalloc(retlen + remainlen * 2 + 1 + 1);
109 strcpy(ret, prepend);
112 found = strchrnul(str, escapee);
113 chunklen = found - str + 1;
115 /* Copy chunk up to and including escapee (or NUL) to ret */
116 memcpy(ret + retlen, str, chunklen);
119 if (*found == '\0') {
120 /* It wasn't escapee, it was NUL! */
121 ret[retlen - 1] = append; /* replace NUL */
122 ret[retlen] = '\0'; /* add NUL */
125 ret[retlen++] = escapee; /* duplicate escapee */
131 /* Returns strlen as a bonus */
133 replace_char(char *str, char from, char to)
144 /* NB: status_str is char[4] packed into uint32_t */
146 cmdio_write(uint32_t status_str, const char *str)
151 /* FTP allegedly uses telnet protocol for command link.
152 * In telnet, 0xff is an escape char, and needs to be escaped: */
153 response = escape_text((char *) &status_str, str, (0xff << 8) + '\r');
155 /* ?! does FTP send embedded LFs as NULs? wow */
156 len = replace_char(response, '\n', '\0');
158 response[len++] = '\n'; /* tack on trailing '\n' */
159 xwrite(STDOUT_FILENO, response, len);
164 cmdio_write_ok(int status)
166 fdprintf(STDOUT_FILENO, "%u Operation successful\r\n", status);
169 /* TODO: output strerr(errno) if errno != 0? */
171 cmdio_write_error(int status)
173 fdprintf(STDOUT_FILENO, "%u Error\r\n", status);
177 cmdio_write_raw(const char *p_text)
179 xwrite_str(STDOUT_FILENO, p_text);
183 timeout_handler(int sig UNUSED_PARAM)
186 int sv_errno = errno;
188 if (monotonic_sec() - G.start_time > G.abs_timeout)
191 if (!G.local_file_fd)
194 pos = xlseek(G.local_file_fd, 0, SEEK_CUR);
195 if (pos == G.local_file_pos)
197 G.local_file_pos = pos;
204 cmdio_write_raw(STR(FTP_TIMEOUT)" Timeout\r\n");
205 /* TODO: do we need to abort (as opposed to usual shutdown) data transfer? */
209 /* Simple commands */
214 char *cwd, *response;
216 cwd = xrealloc_getcwd_or_warn(NULL);
220 /* We have to promote each " to "" */
221 response = escape_text(" \"", cwd, ('"' << 8) + '"');
223 cmdio_write(STRNUM32(FTP_PWDOK), response);
230 if (!G.ftp_arg || chdir(G.ftp_arg) != 0) {
231 cmdio_write_error(FTP_FILEFAIL);
234 cmdio_write_ok(FTP_CWDOK);
240 G.ftp_arg = (char*)"..";
247 cmdio_write_raw(STR(FTP_STATOK)"-FTP server status:\r\n"
249 STR(FTP_STATOK)" Ok\r\n");
252 /* TODO: implement FEAT. Example:
253 # nc -vvv ftp.kernel.org 21
254 ftp.kernel.org (130.239.17.4:21) open
255 220 Welcome to ftp.kernel.org.
268 214-The following commands are recognized.
269 ABOR ACCT ALLO APPE CDUP CWD DELE EPRT EPSV FEAT HELP LIST MDTM MKD
270 MODE NLST NOOP OPTS PASS PASV PORT PWD QUIT REIN REST RETR RMD RNFR
271 RNTO SITE SIZE SMNT STAT STOR STOU STRU SYST TYPE USER XCUP XCWD XMKD
278 cmdio_write_raw(STR(FTP_HELP)"-Commands:\r\n"
279 " ALLO CDUP CWD EPSV HELP LIST\r\n"
280 " MODE NLST NOOP PASS PASV PORT PWD QUIT\r\n"
281 " REST RETR SIZE STAT STRU SYST TYPE USER\r\n"
282 #if ENABLE_FEATURE_FTP_WRITE
283 " APPE DELE MKD RMD RNFR RNTO STOR STOU\r\n"
285 STR(FTP_HELP)" Ok\r\n");
288 /* Download commands */
293 return (G.port_addr != NULL);
299 return (G.pasv_listen_fd > STDOUT_FILENO);
303 port_pasv_cleanup(void)
307 if (G.pasv_listen_fd > STDOUT_FILENO)
308 close(G.pasv_listen_fd);
309 G.pasv_listen_fd = -1;
312 /* On error, emits error code to the peer */
314 ftpdataio_get_pasv_fd(void)
318 remote_fd = accept(G.pasv_listen_fd, NULL, 0);
321 cmdio_write_error(FTP_BADSENDCONN);
325 setsockopt(remote_fd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
329 /* Clears port/pasv data.
330 * This means we dont waste resources, for example, keeping
331 * PASV listening socket open when it is no longer needed.
332 * On error, emits error code to the peer (or exits).
333 * On success, emits p_status_msg to the peer.
336 get_remote_transfer_fd(const char *p_status_msg)
341 /* On error, emits error code to the peer */
342 remote_fd = ftpdataio_get_pasv_fd();
345 remote_fd = xconnect_stream(G.port_addr);
352 cmdio_write(STRNUM32(FTP_DATACONN), p_status_msg);
356 /* If there were neither PASV nor PORT, emits error code to the peer */
358 port_or_pasv_was_seen(void)
360 if (!pasv_active() && !port_active()) {
361 cmdio_write_raw(STR(FTP_BADSENDCONN)" Use PORT or PASV first\r\n");
370 bind_for_passive_mode(void)
377 G.pasv_listen_fd = fd = xsocket(G.local_addr->u.sa.sa_family, SOCK_STREAM, 0);
378 setsockopt_reuseaddr(fd);
380 set_nport(G.local_addr, 0);
381 xbind(fd, &G.local_addr->u.sa, G.local_addr->len);
383 getsockname(fd, &G.local_addr->u.sa, &G.local_addr->len);
385 port = get_nport(&G.local_addr->u.sa);
395 char *addr, *response;
397 port = bind_for_passive_mode();
399 if (G.local_addr->u.sa.sa_family == AF_INET)
400 addr = xmalloc_sockaddr2dotted_noport(&G.local_addr->u.sa);
401 else /* seen this in the wild done by other ftp servers: */
402 addr = xstrdup("0.0.0.0");
403 replace_char(addr, '.', ',');
405 response = xasprintf(STR(FTP_PASVOK)" Entering Passive Mode (%s,%u,%u)\r\n",
406 addr, (int)(port >> 8), (int)(port & 255));
408 cmdio_write_raw(response);
419 port = bind_for_passive_mode();
420 response = xasprintf(STR(FTP_EPSVOK)" EPSV Ok (|||%u|)\r\n", port);
421 cmdio_write_raw(response);
428 unsigned port, port_hi;
430 socklen_t peer_ipv4_len;
431 struct sockaddr_in peer_ipv4;
432 struct in_addr port_ipv4_sin_addr;
438 /* PORT command format makes sense only over IPv4 */
439 if (!raw || G.local_addr->u.sa.sa_family != AF_INET) {
441 cmdio_write_error(FTP_BADCMD);
445 comma = strrchr(raw, ',');
449 port = bb_strtou(&comma[1], NULL, 10);
450 if (errno || port > 0xff)
453 comma = strrchr(raw, ',');
457 port_hi = bb_strtou(&comma[1], NULL, 10);
458 if (errno || port_hi > 0xff)
460 port |= port_hi << 8;
462 replace_char(raw, ',', '.');
464 /* We are verifying that PORT's IP matches getpeername().
465 * Otherwise peer can make us open data connections
466 * to other hosts (security problem!)
467 * This code would be too simplistic:
468 * lsa = xdotted2sockaddr(raw, port);
469 * if (lsa == NULL) goto bail;
471 if (!inet_aton(raw, &port_ipv4_sin_addr))
473 peer_ipv4_len = sizeof(peer_ipv4);
474 if (getpeername(STDIN_FILENO, &peer_ipv4, &peer_ipv4_len) != 0)
476 if (memcmp(&port_ipv4_sin_addr, &peer_ipv4.sin_addr, sizeof(struct in_addr)) != 0)
479 G.port_addr = xdotted2sockaddr(raw, port);
480 cmdio_write_ok(FTP_PORTOK);
486 /* When ftp_arg == NULL simply restart from beginning */
487 G.restart_pos = G.ftp_arg ? xatoi_u(G.ftp_arg) : 0;
488 cmdio_write_ok(FTP_RESTOK);
495 off_t bytes_transferred;
498 off_t offset = G.restart_pos;
503 if (!port_or_pasv_was_seen())
504 return; /* port_or_pasv_was_seen emitted error response */
506 /* O_NONBLOCK is useful if file happens to be a device node */
507 local_file_fd = G.ftp_arg ? open(G.ftp_arg, O_RDONLY | O_NONBLOCK) : -1;
508 if (local_file_fd < 0) {
509 cmdio_write_error(FTP_FILEFAIL);
513 if (fstat(local_file_fd, &statbuf) != 0 || !S_ISREG(statbuf.st_mode)) {
514 /* Note - pretend open failed */
515 cmdio_write_error(FTP_FILEFAIL);
518 G.local_file_fd = local_file_fd;
520 /* Now deactive O_NONBLOCK, otherwise we have a problem
521 * on DMAPI filesystems such as XFS DMAPI.
523 ndelay_off(local_file_fd);
525 /* Set the download offset (from REST) if any */
527 xlseek(local_file_fd, offset, SEEK_SET);
529 response = xasprintf(
530 " Opening BINARY mode data connection for %s (%"OFF_FMT"u bytes)",
531 G.ftp_arg, statbuf.st_size);
532 remote_fd = get_remote_transfer_fd(response);
537 bytes_transferred = bb_copyfd_eof(local_file_fd, remote_fd);
539 if (bytes_transferred < 0)
540 cmdio_write_error(FTP_BADSENDFILE);
542 cmdio_write_ok(FTP_TRANSFEROK);
545 close(local_file_fd);
552 popen_ls(const char *opt)
555 const char *argv[5] = { "ftpd", opt, NULL, G.ftp_arg, NULL };
556 struct fd_pair outfd;
559 cwd = xrealloc_getcwd_or_warn(NULL);
562 /*fflush(NULL); - so far we dont use stdio on output */
565 case -1: /* failure */
566 bb_perror_msg_and_die("vfork");
568 /* NB: close _first_, then move fds! */
570 xmove_fd(outfd.wr, STDOUT_FILENO);
572 /* xopen("/dev/null", O_RDONLY); - chroot may lack it! */
573 if (fchdir(G.proc_self_fd) == 0) {
574 close(G.proc_self_fd);
576 /* ftpd ls helper chdirs to argv[2],
577 * preventing peer from seeing /proc/self
579 execv("exe", (char**) argv);
596 handle_dir_common(int opts)
602 if (!(opts & USE_CTRL_CONN) && !port_or_pasv_was_seen())
603 return; /* port_or_pasv_was_seen emitted error response */
605 /* -n prevents user/groupname display,
606 * which can be problematic in chroot */
607 ls_fd = popen_ls((opts & LONG_LISTING) ? "-l" : "-1");
608 ls_fp = fdopen(ls_fd, "r");
609 if (!ls_fp) /* never happens. paranoia */
610 bb_perror_msg_and_die("fdopen");
612 if (opts & USE_CTRL_CONN) {
613 /* STAT <filename> */
614 cmdio_write_raw(STR(FTP_STATFILE_OK)"-Status follows:\r\n");
616 line = xmalloc_fgetline(ls_fp);
619 cmdio_write(0, line); /* hack: 0 results in no status at all */
622 cmdio_write_ok(FTP_STATFILE_OK);
624 /* LIST/NLST [<filename>] */
625 int remote_fd = get_remote_transfer_fd(" Here comes the directory listing");
626 if (remote_fd >= 0) {
628 line = xmalloc_fgetline(ls_fp);
631 /* I've seen clients complaining when they
632 * are fed with ls output with bare '\n'.
633 * Pity... that would be much simpler.
635 xwrite_str(remote_fd, line);
636 xwrite(remote_fd, "\r\n", 2);
641 cmdio_write_ok(FTP_TRANSFEROK);
643 fclose(ls_fp); /* closes ls_fd too */
648 handle_dir_common(LONG_LISTING);
653 handle_dir_common(0);
656 handle_stat_file(void)
658 handle_dir_common(LONG_LISTING + USE_CTRL_CONN);
665 char buf[sizeof(STR(FTP_STATFILE_OK)" %"OFF_FMT"u\r\n") + sizeof(off_t)*3];
668 || stat(G.ftp_arg, &statbuf) != 0
669 || !S_ISREG(statbuf.st_mode)
671 cmdio_write_error(FTP_FILEFAIL);
674 sprintf(buf, STR(FTP_STATFILE_OK)" %"OFF_FMT"u\r\n", statbuf.st_size);
675 cmdio_write_raw(buf);
678 /* Upload commands */
680 #if ENABLE_FEATURE_FTP_WRITE
684 if (!G.ftp_arg || mkdir(G.ftp_arg, 0777) != 0) {
685 cmdio_write_error(FTP_FILEFAIL);
688 cmdio_write_ok(FTP_MKDIROK);
694 if (!G.ftp_arg || rmdir(G.ftp_arg) != 0) {
695 cmdio_write_error(FTP_FILEFAIL);
698 cmdio_write_ok(FTP_RMDIROK);
704 if (!G.ftp_arg || unlink(G.ftp_arg) != 0) {
705 cmdio_write_error(FTP_FILEFAIL);
708 cmdio_write_ok(FTP_DELEOK);
714 free(G.rnfr_filename);
715 G.rnfr_filename = xstrdup(G.ftp_arg);
716 cmdio_write_ok(FTP_RNFROK);
724 /* If we didn't get a RNFR, throw a wobbly */
725 if (G.rnfr_filename == NULL || G.ftp_arg == NULL) {
726 cmdio_write_raw(STR(FTP_NEEDRNFR)" RNFR required first\r\n");
730 retval = rename(G.rnfr_filename, G.ftp_arg);
731 free(G.rnfr_filename);
732 G.rnfr_filename = NULL;
735 cmdio_write_error(FTP_FILEFAIL);
738 cmdio_write_ok(FTP_RENAMEOK);
742 handle_upload_common(int is_append, int is_unique)
746 off_t bytes_transferred;
751 offset = G.restart_pos;
754 if (!port_or_pasv_was_seen())
755 return; /* port_or_pasv_was_seen emitted error response */
760 tempname = xstrdup(" FILE: uniq.XXXXXX");
761 local_file_fd = mkstemp(tempname + 7);
762 } else if (G.ftp_arg) {
763 int flags = O_WRONLY | O_CREAT | O_TRUNC;
765 flags = O_WRONLY | O_CREAT | O_APPEND;
767 flags = O_WRONLY | O_CREAT;
768 local_file_fd = open(G.ftp_arg, flags, 0666);
771 if (local_file_fd < 0
772 || fstat(local_file_fd, &statbuf) != 0
773 || !S_ISREG(statbuf.st_mode)
775 cmdio_write_error(FTP_UPLOADFAIL);
776 if (local_file_fd >= 0)
777 goto close_local_and_bail;
780 G.local_file_fd = local_file_fd;
783 xlseek(local_file_fd, offset, SEEK_SET);
785 remote_fd = get_remote_transfer_fd(tempname ? tempname : " Ok to send data");
789 goto close_local_and_bail;
791 bytes_transferred = bb_copyfd_eof(remote_fd, local_file_fd);
793 if (bytes_transferred < 0)
794 cmdio_write_error(FTP_BADSENDFILE);
796 cmdio_write_ok(FTP_TRANSFEROK);
798 close_local_and_bail:
799 close(local_file_fd);
806 handle_upload_common(0, 0);
813 handle_upload_common(1, 0);
820 handle_upload_common(0, 1);
822 #endif /* ENABLE_FEATURE_FTP_WRITE */
825 cmdio_get_cmd_and_arg(void)
834 len = 8 * 1024; /* Paranoia. Peer may send 1 gigabyte long cmd... */
835 G.ftp_cmd = cmd = xmalloc_reads(STDIN_FILENO, NULL, &len);
839 /* Trailing '\n' is already stripped, strip '\r' */
840 len = strlen(cmd) - 1;
841 while ((ssize_t)len >= 0 && cmd[len] == '\r') {
846 G.ftp_arg = strchr(cmd, ' ');
847 if (G.ftp_arg != NULL)
850 /* Uppercase and pack into uint32_t first word of the command */
853 cmdval = (cmdval << 8) + ((unsigned char)*cmd++ & (unsigned char)~0x20);
858 #define mk_const4(a,b,c,d) (((a * 0x100 + b) * 0x100 + c) * 0x100 + d)
859 #define mk_const3(a,b,c) ((a * 0x100 + b) * 0x100 + c)
861 const_ALLO = mk_const4('A', 'L', 'L', 'O'),
862 const_APPE = mk_const4('A', 'P', 'P', 'E'),
863 const_CDUP = mk_const4('C', 'D', 'U', 'P'),
864 const_CWD = mk_const3('C', 'W', 'D'),
865 const_DELE = mk_const4('D', 'E', 'L', 'E'),
866 const_EPSV = mk_const4('E', 'P', 'S', 'V'),
867 const_HELP = mk_const4('H', 'E', 'L', 'P'),
868 const_LIST = mk_const4('L', 'I', 'S', 'T'),
869 const_MKD = mk_const3('M', 'K', 'D'),
870 const_MODE = mk_const4('M', 'O', 'D', 'E'),
871 const_NLST = mk_const4('N', 'L', 'S', 'T'),
872 const_NOOP = mk_const4('N', 'O', 'O', 'P'),
873 const_PASS = mk_const4('P', 'A', 'S', 'S'),
874 const_PASV = mk_const4('P', 'A', 'S', 'V'),
875 const_PORT = mk_const4('P', 'O', 'R', 'T'),
876 const_PWD = mk_const3('P', 'W', 'D'),
877 const_QUIT = mk_const4('Q', 'U', 'I', 'T'),
878 const_REST = mk_const4('R', 'E', 'S', 'T'),
879 const_RETR = mk_const4('R', 'E', 'T', 'R'),
880 const_RMD = mk_const3('R', 'M', 'D'),
881 const_RNFR = mk_const4('R', 'N', 'F', 'R'),
882 const_RNTO = mk_const4('R', 'N', 'T', 'O'),
883 const_SIZE = mk_const4('S', 'I', 'Z', 'E'),
884 const_STAT = mk_const4('S', 'T', 'A', 'T'),
885 const_STOR = mk_const4('S', 'T', 'O', 'R'),
886 const_STOU = mk_const4('S', 'T', 'O', 'U'),
887 const_STRU = mk_const4('S', 'T', 'R', 'U'),
888 const_SYST = mk_const4('S', 'Y', 'S', 'T'),
889 const_TYPE = mk_const4('T', 'Y', 'P', 'E'),
890 const_USER = mk_const4('U', 'S', 'E', 'R'),
899 int ftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
900 int ftpd_main(int argc, char **argv)
906 G.start_time = monotonic_sec();
907 G.abs_timeout = 1 * 60 * 60;
909 opt_complementary = "t+:T+";
910 opts = getopt32(argv, "l1vS" USE_FEATURE_FTP_WRITE("w") "t:T:", &G.timeout, &G.abs_timeout);
912 if (opts & (OPT_l|OPT_1)) {
913 /* Our secret backdoor to ls */
914 memset(&G, 0, sizeof(G));
915 /* TODO: pass -n too? */
917 argv[2] = (char*)"--";
918 return ls_main(argc, argv);
922 G.local_addr = get_sock_lsa(STDIN_FILENO);
924 /* This is confusing:
925 * bb_error_msg_and_die("stdin is not a socket");
928 /* Help text says that ftpd must be used as inetd service,
929 * which is by far the most usual cause of get_sock_lsa
934 logmode = LOGMODE_NONE;
936 /* LOG_NDELAY is needed since we may chroot later */
937 openlog(applet_name, LOG_PID | LOG_NDELAY, LOG_DAEMON);
938 logmode |= LOGMODE_SYSLOG;
941 G.proc_self_fd = xopen("/proc/self", O_RDONLY | O_DIRECTORY);
944 xchdir(argv[optind]);
948 //umask(077); - admin can set umask before starting us
950 /* Signals. We'll always take -EPIPE rather than a rude signal, thanks */
951 signal(SIGPIPE, SIG_IGN);
953 /* Set up options on the command socket (do we need these all? why?) */
954 setsockopt(STDIN_FILENO, IPPROTO_TCP, TCP_NODELAY, &const_int_1, sizeof(const_int_1));
955 setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
956 setsockopt(STDIN_FILENO, SOL_SOCKET, SO_OOBINLINE, &const_int_1, sizeof(const_int_1));
958 cmdio_write_ok(FTP_GREET);
959 signal(SIGALRM, timeout_handler);
961 #ifdef IF_WE_WANT_TO_REQUIRE_LOGIN
963 smallint user_was_specified = 0;
965 uint32_t cmdval = cmdio_get_cmd_and_arg();
967 if (cmdval == const_USER) {
968 if (G.ftp_arg == NULL || strcasecmp(G.ftp_arg, "anonymous") != 0)
969 cmdio_write_raw(STR(FTP_LOGINERR)" Server is anonymous only\r\n");
971 user_was_specified = 1;
972 cmdio_write_raw(STR(FTP_GIVEPWORD)" Please specify the password\r\n");
974 } else if (cmdval == const_PASS) {
975 if (user_was_specified)
977 cmdio_write_raw(STR(FTP_NEEDUSER)" Login with USER\r\n");
978 } else if (cmdval == const_QUIT) {
979 cmdio_write_ok(FTP_GOODBYE);
982 cmdio_write_raw(STR(FTP_LOGINERR)" Login with USER and PASS\r\n");
986 cmdio_write_ok(FTP_LOGINOK);
989 /* RFC-959 Section 5.1
990 * The following commands and options MUST be supported by every
991 * server-FTP and user-FTP, except in cases where the underlying
992 * file system or operating system does not allow or support
993 * a particular command.
994 * Type: ASCII Non-print, IMAGE, LOCAL 8
996 * Structure: File, Record*
997 * (Record structure is REQUIRED only for hosts whose file
998 * systems support record structure).
1000 * USER, PASS, ACCT, [bbox: ACCT not supported]
1005 * CWD, CDUP, RMD, MKD, PWD,
1011 * "The argument field is a Telnet string identifying the user's account.
1012 * The command is not necessarily related to the USER command, as some
1013 * sites may require an account for login and others only for specific
1014 * access, such as storing files. In the latter case the command may
1015 * arrive at any time.
1016 * There are reply codes to differentiate these cases for the automation:
1017 * when account information is required for login, the response to
1018 * a successful PASSword command is reply code 332. On the other hand,
1019 * if account information is NOT required for login, the reply to
1020 * a successful PASSword command is 230; and if the account information
1021 * is needed for a command issued later in the dialogue, the server
1022 * should return a 332 or 532 reply depending on whether it stores
1023 * (pending receipt of the ACCounT command) or discards the command,
1028 uint32_t cmdval = cmdio_get_cmd_and_arg();
1030 if (cmdval == const_QUIT) {
1031 cmdio_write_ok(FTP_GOODBYE);
1034 else if (cmdval == const_USER)
1035 cmdio_write_ok(FTP_GIVEPWORD);
1036 else if (cmdval == const_PASS)
1037 cmdio_write_ok(FTP_LOGINOK);
1038 else if (cmdval == const_NOOP)
1039 cmdio_write_ok(FTP_NOOPOK);
1040 else if (cmdval == const_TYPE)
1041 cmdio_write_ok(FTP_TYPEOK);
1042 else if (cmdval == const_STRU)
1043 cmdio_write_ok(FTP_STRUOK);
1044 else if (cmdval == const_MODE)
1045 cmdio_write_ok(FTP_MODEOK);
1046 else if (cmdval == const_ALLO)
1047 cmdio_write_ok(FTP_ALLOOK);
1048 else if (cmdval == const_SYST)
1049 cmdio_write_raw(STR(FTP_SYSTOK)" UNIX Type: L8\r\n");
1050 else if (cmdval == const_PWD)
1052 else if (cmdval == const_CWD)
1054 else if (cmdval == const_CDUP) /* cd .. */
1056 else if (cmdval == const_HELP)
1058 else if (cmdval == const_LIST) /* ls -l */
1060 else if (cmdval == const_NLST) /* "name list", bare ls */
1062 else if (cmdval == const_SIZE)
1064 else if (cmdval == const_STAT) {
1065 if (G.ftp_arg == NULL)
1070 else if (cmdval == const_PASV)
1072 else if (cmdval == const_EPSV)
1074 else if (cmdval == const_RETR)
1076 else if (cmdval == const_PORT)
1078 else if (cmdval == const_REST)
1080 #if ENABLE_FEATURE_FTP_WRITE
1081 else if (opts & OPT_w) {
1082 if (cmdval == const_STOR)
1084 else if (cmdval == const_MKD)
1086 else if (cmdval == const_RMD)
1088 else if (cmdval == const_DELE)
1090 else if (cmdval == const_RNFR) /* "rename from" */
1092 else if (cmdval == const_RNTO) /* "rename to" */
1094 else if (cmdval == const_APPE)
1096 else if (cmdval == const_STOU) /* "store unique" */
1101 else if (cmdval == const_STOR
1102 || cmdval == const_MKD
1103 || cmdval == const_RMD
1104 || cmdval == const_DELE
1105 || cmdval == const_RNFR
1106 || cmdval == const_RNTO
1107 || cmdval == const_APPE
1108 || cmdval == const_STOU
1110 cmdio_write_raw(STR(FTP_NOPERM)" Permission denied\r\n");
1114 /* Which unsupported commands were seen in the wild?
1115 * (doesn't necessarily mean "we must support them")
1116 * lftp 3.6.3: FEAT - is it useful?
1117 * MDTM - works fine without it anyway
1119 cmdio_write_raw(STR(FTP_BADCMD)" Unknown command\r\n");