2009-03-08 15:01:28 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Simple FTP daemon, based on vsftpd 2.0.7 (written by Chris Evans)
|
|
|
|
*
|
|
|
|
* Author: Adam Tkac <vonsch@gmail.com>
|
|
|
|
*
|
2009-03-09 18:31:08 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
|
|
|
*
|
2009-03-08 15:01:28 +05:30
|
|
|
* Only subset of FTP protocol is implemented but vast majority of clients
|
2009-03-18 06:15:00 +05:30
|
|
|
* should not have any problem.
|
|
|
|
*
|
|
|
|
* You have to run this daemon via inetd.
|
2009-03-08 15:01:28 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
2009-03-09 05:16:48 +05:30
|
|
|
#include <syslog.h>
|
2009-03-08 15:01:28 +05:30
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
2009-03-09 09:43:59 +05:30
|
|
|
#define FTP_DATACONN 150
|
|
|
|
#define FTP_NOOPOK 200
|
|
|
|
#define FTP_TYPEOK 200
|
|
|
|
#define FTP_PORTOK 200
|
|
|
|
#define FTP_STRUOK 200
|
|
|
|
#define FTP_MODEOK 200
|
|
|
|
#define FTP_ALLOOK 202
|
|
|
|
#define FTP_STATOK 211
|
|
|
|
#define FTP_STATFILE_OK 213
|
|
|
|
#define FTP_HELP 214
|
|
|
|
#define FTP_SYSTOK 215
|
|
|
|
#define FTP_GREET 220
|
|
|
|
#define FTP_GOODBYE 221
|
|
|
|
#define FTP_TRANSFEROK 226
|
|
|
|
#define FTP_PASVOK 227
|
2009-03-09 18:31:08 +05:30
|
|
|
/*#define FTP_EPRTOK 228*/
|
|
|
|
#define FTP_EPSVOK 229
|
2009-03-09 09:43:59 +05:30
|
|
|
#define FTP_LOGINOK 230
|
|
|
|
#define FTP_CWDOK 250
|
|
|
|
#define FTP_RMDIROK 250
|
|
|
|
#define FTP_DELEOK 250
|
|
|
|
#define FTP_RENAMEOK 250
|
|
|
|
#define FTP_PWDOK 257
|
|
|
|
#define FTP_MKDIROK 257
|
|
|
|
#define FTP_GIVEPWORD 331
|
|
|
|
#define FTP_RESTOK 350
|
|
|
|
#define FTP_RNFROK 350
|
2009-03-16 21:49:53 +05:30
|
|
|
#define FTP_TIMEOUT 421
|
2009-03-09 09:43:59 +05:30
|
|
|
#define FTP_BADSENDCONN 425
|
|
|
|
#define FTP_BADSENDNET 426
|
|
|
|
#define FTP_BADSENDFILE 451
|
|
|
|
#define FTP_BADCMD 500
|
|
|
|
#define FTP_COMMANDNOTIMPL 502
|
|
|
|
#define FTP_NEEDUSER 503
|
|
|
|
#define FTP_NEEDRNFR 503
|
|
|
|
#define FTP_BADSTRU 504
|
|
|
|
#define FTP_BADMODE 504
|
|
|
|
#define FTP_LOGINERR 530
|
|
|
|
#define FTP_FILEFAIL 550
|
|
|
|
#define FTP_NOPERM 550
|
|
|
|
#define FTP_UPLOADFAIL 553
|
|
|
|
|
|
|
|
#define STR1(s) #s
|
|
|
|
#define STR(s) STR1(s)
|
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
/* Convert a constant to 3-digit string, packed into uint32_t */
|
2009-03-08 15:01:28 +05:30
|
|
|
enum {
|
2009-03-09 21:16:07 +05:30
|
|
|
/* Shift for Nth decimal digit */
|
2009-03-17 02:24:45 +05:30
|
|
|
SHIFT2 = 0 * BB_LITTLE_ENDIAN + 24 * BB_BIG_ENDIAN,
|
|
|
|
SHIFT1 = 8 * BB_LITTLE_ENDIAN + 16 * BB_BIG_ENDIAN,
|
|
|
|
SHIFT0 = 16 * BB_LITTLE_ENDIAN + 8 * BB_BIG_ENDIAN,
|
|
|
|
/* And for 4th position (space) */
|
|
|
|
SHIFTsp = 24 * BB_LITTLE_ENDIAN + 0 * BB_BIG_ENDIAN,
|
2009-03-09 21:16:07 +05:30
|
|
|
};
|
|
|
|
#define STRNUM32(s) (uint32_t)(0 \
|
|
|
|
| (('0' + ((s) / 1 % 10)) << SHIFT0) \
|
|
|
|
| (('0' + ((s) / 10 % 10)) << SHIFT1) \
|
|
|
|
| (('0' + ((s) / 100 % 10)) << SHIFT2) \
|
|
|
|
)
|
2009-03-17 02:24:45 +05:30
|
|
|
#define STRNUM32sp(s) (uint32_t)(0 \
|
|
|
|
| (' ' << SHIFTsp) \
|
|
|
|
| (('0' + ((s) / 1 % 10)) << SHIFT0) \
|
|
|
|
| (('0' + ((s) / 10 % 10)) << SHIFT1) \
|
|
|
|
| (('0' + ((s) / 100 % 10)) << SHIFT2) \
|
|
|
|
)
|
|
|
|
|
|
|
|
#define MSG_OK "Operation successful\r\n"
|
|
|
|
#define MSG_ERR "Error\r\n"
|
2009-03-09 21:16:07 +05:30
|
|
|
|
2009-03-08 15:01:28 +05:30
|
|
|
struct globals {
|
|
|
|
int pasv_listen_fd;
|
2009-03-18 19:42:22 +05:30
|
|
|
#if !BB_MMU
|
|
|
|
int root_fd;
|
|
|
|
#endif
|
2009-03-16 21:49:53 +05:30
|
|
|
int local_file_fd;
|
2009-03-17 10:41:51 +05:30
|
|
|
unsigned end_time;
|
|
|
|
unsigned timeout;
|
2009-03-17 17:53:24 +05:30
|
|
|
unsigned verbose;
|
2009-03-16 21:49:53 +05:30
|
|
|
off_t local_file_pos;
|
2009-03-08 15:01:28 +05:30
|
|
|
off_t restart_pos;
|
2009-03-16 21:49:53 +05:30
|
|
|
len_and_sockaddr *local_addr;
|
|
|
|
len_and_sockaddr *port_addr;
|
2009-03-09 10:08:37 +05:30
|
|
|
char *ftp_cmd;
|
2009-03-09 07:53:45 +05:30
|
|
|
char *ftp_arg;
|
2009-03-08 15:01:28 +05:30
|
|
|
#if ENABLE_FEATURE_FTP_WRITE
|
|
|
|
char *rnfr_filename;
|
|
|
|
#endif
|
2009-03-17 02:24:45 +05:30
|
|
|
/* We need these aligned to uint32_t */
|
|
|
|
char msg_ok [(sizeof("NNN " MSG_OK ) + 3) & 0xfffc];
|
|
|
|
char msg_err[(sizeof("NNN " MSG_ERR) + 3) & 0xfffc];
|
2009-03-08 15:01:28 +05:30
|
|
|
};
|
|
|
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
2009-03-17 02:24:45 +05:30
|
|
|
#define INIT_G() do { \
|
2009-03-17 10:41:51 +05:30
|
|
|
/* Moved to main */ \
|
|
|
|
/*strcpy(G.msg_ok + 4, MSG_OK );*/ \
|
|
|
|
/*strcpy(G.msg_err + 4, MSG_ERR);*/ \
|
2009-03-17 02:24:45 +05:30
|
|
|
} while (0)
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 10:08:37 +05:30
|
|
|
|
2009-03-08 15:01:28 +05:30
|
|
|
static char *
|
2009-03-09 21:16:07 +05:30
|
|
|
escape_text(const char *prepend, const char *str, unsigned escapee)
|
2009-03-08 15:01:28 +05:30
|
|
|
{
|
2009-03-09 21:16:07 +05:30
|
|
|
unsigned retlen, remainlen, chunklen;
|
2009-03-08 15:01:28 +05:30
|
|
|
char *ret, *found;
|
2009-03-09 21:16:07 +05:30
|
|
|
char append;
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
append = (char)escapee;
|
|
|
|
escapee >>= 8;
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
remainlen = strlen(str);
|
2009-03-09 10:08:37 +05:30
|
|
|
retlen = strlen(prepend);
|
2009-03-09 21:16:07 +05:30
|
|
|
ret = xmalloc(retlen + remainlen * 2 + 1 + 1);
|
2009-03-09 10:08:37 +05:30
|
|
|
strcpy(ret, prepend);
|
2009-03-08 15:01:28 +05:30
|
|
|
|
|
|
|
for (;;) {
|
2009-03-09 21:16:07 +05:30
|
|
|
found = strchrnul(str, escapee);
|
|
|
|
chunklen = found - str + 1;
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
/* Copy chunk up to and including escapee (or NUL) to ret */
|
|
|
|
memcpy(ret + retlen, str, chunklen);
|
2009-03-09 10:08:37 +05:30
|
|
|
retlen += chunklen;
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
if (*found == '\0') {
|
|
|
|
/* It wasn't escapee, it was NUL! */
|
|
|
|
ret[retlen - 1] = append; /* replace NUL */
|
|
|
|
ret[retlen] = '\0'; /* add NUL */
|
2009-03-08 15:01:28 +05:30
|
|
|
break;
|
|
|
|
}
|
2009-03-09 21:16:07 +05:30
|
|
|
ret[retlen++] = escapee; /* duplicate escapee */
|
|
|
|
str = found + 1;
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
/* Returns strlen as a bonus */
|
|
|
|
static unsigned
|
2009-03-08 15:01:28 +05:30
|
|
|
replace_char(char *str, char from, char to)
|
|
|
|
{
|
2009-03-09 21:16:07 +05:30
|
|
|
char *p = str;
|
|
|
|
while (*p) {
|
|
|
|
if (*p == from)
|
|
|
|
*p = to;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return p - str;
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
|
2009-03-17 17:53:24 +05:30
|
|
|
static void
|
|
|
|
verbose_log(const char *str)
|
|
|
|
{
|
|
|
|
bb_error_msg("%.*s", (int)strcspn(str, "\r\n"), str);
|
|
|
|
}
|
|
|
|
|
2009-03-16 20:23:54 +05:30
|
|
|
/* NB: status_str is char[4] packed into uint32_t */
|
2009-03-08 15:01:28 +05:30
|
|
|
static void
|
2009-03-09 21:16:07 +05:30
|
|
|
cmdio_write(uint32_t status_str, const char *str)
|
2009-03-08 15:01:28 +05:30
|
|
|
{
|
2009-03-09 10:08:37 +05:30
|
|
|
char *response;
|
2009-03-09 07:53:45 +05:30
|
|
|
int len;
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-18 06:15:00 +05:30
|
|
|
/* FTP uses telnet protocol for command link.
|
2009-03-09 07:53:45 +05:30
|
|
|
* In telnet, 0xff is an escape char, and needs to be escaped: */
|
2009-03-16 20:23:54 +05:30
|
|
|
response = escape_text((char *) &status_str, str, (0xff << 8) + '\r');
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-18 06:15:00 +05:30
|
|
|
/* FTP sends embedded LFs as NULs */
|
2009-03-09 21:16:07 +05:30
|
|
|
len = replace_char(response, '\n', '\0');
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 07:53:45 +05:30
|
|
|
response[len++] = '\n'; /* tack on trailing '\n' */
|
2009-03-09 09:43:59 +05:30
|
|
|
xwrite(STDOUT_FILENO, response, len);
|
2009-03-17 17:53:24 +05:30
|
|
|
if (G.verbose > 1)
|
|
|
|
verbose_log(response);
|
2009-03-09 07:53:45 +05:30
|
|
|
free(response);
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
|
2009-03-09 05:16:48 +05:30
|
|
|
static void
|
2009-03-17 02:24:45 +05:30
|
|
|
cmdio_write_ok(unsigned status)
|
2009-03-09 05:16:48 +05:30
|
|
|
{
|
2009-03-17 02:24:45 +05:30
|
|
|
*(uint32_t *) G.msg_ok = status;
|
|
|
|
xwrite(STDOUT_FILENO, G.msg_ok, sizeof("NNN " MSG_OK) - 1);
|
2009-03-17 17:53:24 +05:30
|
|
|
if (G.verbose > 1)
|
|
|
|
verbose_log(G.msg_ok);
|
2009-03-09 05:16:48 +05:30
|
|
|
}
|
2009-03-17 02:24:45 +05:30
|
|
|
#define WRITE_OK(a) cmdio_write_ok(STRNUM32sp(a))
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-09 09:43:59 +05:30
|
|
|
/* TODO: output strerr(errno) if errno != 0? */
|
2009-03-08 15:01:28 +05:30
|
|
|
static void
|
2009-03-17 02:24:45 +05:30
|
|
|
cmdio_write_error(unsigned status)
|
2009-03-08 15:01:28 +05:30
|
|
|
{
|
2009-03-17 02:24:45 +05:30
|
|
|
*(uint32_t *) G.msg_err = status;
|
|
|
|
xwrite(STDOUT_FILENO, G.msg_err, sizeof("NNN " MSG_ERR) - 1);
|
2009-03-17 17:53:24 +05:30
|
|
|
if (G.verbose > 1)
|
|
|
|
verbose_log(G.msg_err);
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
2009-03-17 02:24:45 +05:30
|
|
|
#define WRITE_ERR(a) cmdio_write_error(STRNUM32sp(a))
|
2009-03-08 15:01:28 +05:30
|
|
|
|
|
|
|
static void
|
|
|
|
cmdio_write_raw(const char *p_text)
|
|
|
|
{
|
2009-03-09 09:43:59 +05:30
|
|
|
xwrite_str(STDOUT_FILENO, p_text);
|
2009-03-17 17:53:24 +05:30
|
|
|
if (G.verbose > 1)
|
|
|
|
verbose_log(p_text);
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
|
2009-03-16 21:49:53 +05:30
|
|
|
static void
|
|
|
|
timeout_handler(int sig UNUSED_PARAM)
|
|
|
|
{
|
|
|
|
off_t pos;
|
|
|
|
int sv_errno = errno;
|
|
|
|
|
2009-03-17 10:41:51 +05:30
|
|
|
if ((int)(monotonic_sec() - G.end_time) >= 0)
|
2009-03-16 21:49:53 +05:30
|
|
|
goto timed_out;
|
|
|
|
|
|
|
|
if (!G.local_file_fd)
|
|
|
|
goto timed_out;
|
|
|
|
|
|
|
|
pos = xlseek(G.local_file_fd, 0, SEEK_CUR);
|
|
|
|
if (pos == G.local_file_pos)
|
|
|
|
goto timed_out;
|
|
|
|
G.local_file_pos = pos;
|
|
|
|
|
|
|
|
alarm(G.timeout);
|
|
|
|
errno = sv_errno;
|
|
|
|
return;
|
|
|
|
|
|
|
|
timed_out:
|
|
|
|
cmdio_write_raw(STR(FTP_TIMEOUT)" Timeout\r\n");
|
|
|
|
/* TODO: do we need to abort (as opposed to usual shutdown) data transfer? */
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-03-09 08:45:05 +05:30
|
|
|
/* Simple commands */
|
|
|
|
|
2009-03-09 07:53:45 +05:30
|
|
|
static void
|
|
|
|
handle_pwd(void)
|
2009-03-08 15:01:28 +05:30
|
|
|
{
|
2009-03-09 10:08:37 +05:30
|
|
|
char *cwd, *response;
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 07:53:45 +05:30
|
|
|
cwd = xrealloc_getcwd_or_warn(NULL);
|
|
|
|
if (cwd == NULL)
|
|
|
|
cwd = xstrdup("");
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-09 07:53:45 +05:30
|
|
|
/* We have to promote each " to "" */
|
2009-03-09 21:16:07 +05:30
|
|
|
response = escape_text(" \"", cwd, ('"' << 8) + '"');
|
2009-03-09 07:53:45 +05:30
|
|
|
free(cwd);
|
2009-03-09 21:16:07 +05:30
|
|
|
cmdio_write(STRNUM32(FTP_PWDOK), response);
|
2009-03-09 07:53:45 +05:30
|
|
|
free(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_cwd(void)
|
|
|
|
{
|
|
|
|
if (!G.ftp_arg || chdir(G.ftp_arg) != 0) {
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_FILEFAIL);
|
2009-03-09 07:53:45 +05:30
|
|
|
return;
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_CWDOK);
|
2009-03-09 07:53:45 +05:30
|
|
|
}
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 07:53:45 +05:30
|
|
|
static void
|
|
|
|
handle_cdup(void)
|
|
|
|
{
|
|
|
|
G.ftp_arg = (char*)"..";
|
|
|
|
handle_cwd();
|
|
|
|
}
|
|
|
|
|
2009-03-09 08:45:05 +05:30
|
|
|
static void
|
|
|
|
handle_stat(void)
|
|
|
|
{
|
2009-03-18 19:55:28 +05:30
|
|
|
cmdio_write_raw(STR(FTP_STATOK)"-Server status:\r\n"
|
2009-03-09 18:31:08 +05:30
|
|
|
" TYPE: BINARY\r\n"
|
2009-03-09 09:43:59 +05:30
|
|
|
STR(FTP_STATOK)" Ok\r\n");
|
2009-03-09 08:45:05 +05:30
|
|
|
}
|
|
|
|
|
2009-03-17 18:10:34 +05:30
|
|
|
/* Examples of HELP and FEAT:
|
2009-03-15 21:24:58 +05:30
|
|
|
# nc -vvv ftp.kernel.org 21
|
|
|
|
ftp.kernel.org (130.239.17.4:21) open
|
|
|
|
220 Welcome to ftp.kernel.org.
|
|
|
|
FEAT
|
|
|
|
211-Features:
|
|
|
|
EPRT
|
|
|
|
EPSV
|
|
|
|
MDTM
|
|
|
|
PASV
|
|
|
|
REST STREAM
|
|
|
|
SIZE
|
|
|
|
TVFS
|
|
|
|
UTF8
|
|
|
|
211 End
|
|
|
|
HELP
|
|
|
|
214-The following commands are recognized.
|
|
|
|
ABOR ACCT ALLO APPE CDUP CWD DELE EPRT EPSV FEAT HELP LIST MDTM MKD
|
|
|
|
MODE NLST NOOP OPTS PASS PASV PORT PWD QUIT REIN REST RETR RMD RNFR
|
|
|
|
RNTO SITE SIZE SMNT STAT STOR STOU STRU SYST TYPE USER XCUP XCWD XMKD
|
|
|
|
XPWD XRMD
|
|
|
|
214 Help OK.
|
|
|
|
*/
|
2009-03-09 07:53:45 +05:30
|
|
|
static void
|
2009-03-17 18:10:34 +05:30
|
|
|
handle_feat(unsigned status)
|
2009-03-09 07:53:45 +05:30
|
|
|
{
|
2009-03-17 18:10:34 +05:30
|
|
|
cmdio_write(status, "-Features:");
|
|
|
|
cmdio_write_raw(" EPSV\r\n"
|
|
|
|
" PASV\r\n"
|
|
|
|
" REST STREAM\r\n"
|
2009-03-18 06:15:00 +05:30
|
|
|
" MDTM\r\n"
|
2009-03-17 18:10:34 +05:30
|
|
|
" SIZE\r\n");
|
|
|
|
cmdio_write(status, " Ok");
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
|
2009-03-09 08:45:05 +05:30
|
|
|
/* Download commands */
|
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
static inline int
|
|
|
|
port_active(void)
|
|
|
|
{
|
|
|
|
return (G.port_addr != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
pasv_active(void)
|
|
|
|
{
|
|
|
|
return (G.pasv_listen_fd > STDOUT_FILENO);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
port_pasv_cleanup(void)
|
|
|
|
{
|
|
|
|
free(G.port_addr);
|
|
|
|
G.port_addr = NULL;
|
|
|
|
if (G.pasv_listen_fd > STDOUT_FILENO)
|
|
|
|
close(G.pasv_listen_fd);
|
|
|
|
G.pasv_listen_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* On error, emits error code to the peer */
|
2009-03-08 15:01:28 +05:30
|
|
|
static int
|
|
|
|
ftpdataio_get_pasv_fd(void)
|
|
|
|
{
|
|
|
|
int remote_fd;
|
|
|
|
|
|
|
|
remote_fd = accept(G.pasv_listen_fd, NULL, 0);
|
|
|
|
|
|
|
|
if (remote_fd < 0) {
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_BADSENDCONN);
|
2009-03-08 15:01:28 +05:30
|
|
|
return remote_fd;
|
|
|
|
}
|
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
setsockopt(remote_fd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
|
2009-03-08 15:01:28 +05:30
|
|
|
return remote_fd;
|
|
|
|
}
|
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
/* Clears port/pasv data.
|
|
|
|
* This means we dont waste resources, for example, keeping
|
|
|
|
* PASV listening socket open when it is no longer needed.
|
|
|
|
* On error, emits error code to the peer (or exits).
|
|
|
|
* On success, emits p_status_msg to the peer.
|
|
|
|
*/
|
2009-03-08 15:01:28 +05:30
|
|
|
static int
|
|
|
|
get_remote_transfer_fd(const char *p_status_msg)
|
|
|
|
{
|
|
|
|
int remote_fd;
|
|
|
|
|
|
|
|
if (pasv_active())
|
2009-03-15 21:24:58 +05:30
|
|
|
/* On error, emits error code to the peer */
|
2009-03-08 15:01:28 +05:30
|
|
|
remote_fd = ftpdataio_get_pasv_fd();
|
|
|
|
else
|
2009-03-15 21:24:58 +05:30
|
|
|
/* Exits on error */
|
2009-03-09 21:16:07 +05:30
|
|
|
remote_fd = xconnect_stream(G.port_addr);
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
port_pasv_cleanup();
|
|
|
|
|
2009-03-08 15:01:28 +05:30
|
|
|
if (remote_fd < 0)
|
|
|
|
return remote_fd;
|
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
cmdio_write(STRNUM32(FTP_DATACONN), p_status_msg);
|
2009-03-08 15:01:28 +05:30
|
|
|
return remote_fd;
|
|
|
|
}
|
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
/* If there were neither PASV nor PORT, emits error code to the peer */
|
2009-03-08 15:01:28 +05:30
|
|
|
static int
|
2009-03-15 21:24:58 +05:30
|
|
|
port_or_pasv_was_seen(void)
|
2009-03-08 15:01:28 +05:30
|
|
|
{
|
|
|
|
if (!pasv_active() && !port_active()) {
|
2009-03-18 19:55:28 +05:30
|
|
|
cmdio_write_raw(STR(FTP_BADSENDCONN)" Use PORT/PASV first\r\n");
|
2009-03-08 15:01:28 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
/* Exits on error */
|
2009-03-09 18:31:08 +05:30
|
|
|
static unsigned
|
|
|
|
bind_for_passive_mode(void)
|
2009-03-09 05:16:48 +05:30
|
|
|
{
|
2009-03-09 21:16:07 +05:30
|
|
|
int fd;
|
2009-03-09 18:31:08 +05:30
|
|
|
unsigned port;
|
2009-03-09 07:53:45 +05:30
|
|
|
|
|
|
|
port_pasv_cleanup();
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
G.pasv_listen_fd = fd = xsocket(G.local_addr->u.sa.sa_family, SOCK_STREAM, 0);
|
|
|
|
setsockopt_reuseaddr(fd);
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-09 18:31:08 +05:30
|
|
|
set_nport(G.local_addr, 0);
|
2009-03-09 21:16:07 +05:30
|
|
|
xbind(fd, &G.local_addr->u.sa, G.local_addr->len);
|
|
|
|
xlisten(fd, 1);
|
|
|
|
getsockname(fd, &G.local_addr->u.sa, &G.local_addr->len);
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-09 18:31:08 +05:30
|
|
|
port = get_nport(&G.local_addr->u.sa);
|
|
|
|
port = ntohs(port);
|
|
|
|
return port;
|
|
|
|
}
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
/* Exits on error */
|
2009-03-09 18:31:08 +05:30
|
|
|
static void
|
|
|
|
handle_pasv(void)
|
|
|
|
{
|
|
|
|
unsigned port;
|
|
|
|
char *addr, *response;
|
|
|
|
|
|
|
|
port = bind_for_passive_mode();
|
|
|
|
|
|
|
|
if (G.local_addr->u.sa.sa_family == AF_INET)
|
|
|
|
addr = xmalloc_sockaddr2dotted_noport(&G.local_addr->u.sa);
|
|
|
|
else /* seen this in the wild done by other ftp servers: */
|
|
|
|
addr = xstrdup("0.0.0.0");
|
2009-03-09 07:53:45 +05:30
|
|
|
replace_char(addr, '.', ',');
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-18 19:55:28 +05:30
|
|
|
response = xasprintf(STR(FTP_PASVOK)" PASV ok (%s,%u,%u)\r\n",
|
2009-03-09 07:53:45 +05:30
|
|
|
addr, (int)(port >> 8), (int)(port & 255));
|
|
|
|
free(addr);
|
2009-03-09 18:31:08 +05:30
|
|
|
cmdio_write_raw(response);
|
|
|
|
free(response);
|
|
|
|
}
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
/* Exits on error */
|
2009-03-09 18:31:08 +05:30
|
|
|
static void
|
|
|
|
handle_epsv(void)
|
|
|
|
{
|
|
|
|
unsigned port;
|
|
|
|
char *response;
|
|
|
|
|
|
|
|
port = bind_for_passive_mode();
|
2009-03-18 19:55:28 +05:30
|
|
|
response = xasprintf(STR(FTP_EPSVOK)" EPSV ok (|||%u|)\r\n", port);
|
2009-03-09 18:31:08 +05:30
|
|
|
cmdio_write_raw(response);
|
2009-03-09 05:16:48 +05:30
|
|
|
free(response);
|
|
|
|
}
|
|
|
|
|
2009-03-17 01:24:06 +05:30
|
|
|
/* libbb candidate */
|
|
|
|
static
|
|
|
|
len_and_sockaddr* get_peer_lsa(int fd)
|
|
|
|
{
|
|
|
|
len_and_sockaddr *lsa;
|
|
|
|
socklen_t len = 0;
|
|
|
|
|
|
|
|
if (getpeername(fd, NULL, &len) != 0)
|
|
|
|
return NULL;
|
|
|
|
lsa = xzalloc(LSA_LEN_SIZE + len);
|
|
|
|
lsa->len = len;
|
|
|
|
getpeername(fd, &lsa->u.sa, &lsa->len);
|
|
|
|
return lsa;
|
|
|
|
}
|
|
|
|
|
2009-03-09 05:16:48 +05:30
|
|
|
static void
|
|
|
|
handle_port(void)
|
|
|
|
{
|
2009-03-16 20:23:54 +05:30
|
|
|
unsigned port, port_hi;
|
|
|
|
char *raw, *comma;
|
2009-03-17 01:24:06 +05:30
|
|
|
#ifdef WHY_BOTHER_WE_CAN_ASSUME_IP_MATCHES
|
2009-03-16 20:23:54 +05:30
|
|
|
socklen_t peer_ipv4_len;
|
|
|
|
struct sockaddr_in peer_ipv4;
|
|
|
|
struct in_addr port_ipv4_sin_addr;
|
2009-03-17 01:24:06 +05:30
|
|
|
#endif
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-09 07:53:45 +05:30
|
|
|
port_pasv_cleanup();
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-09 07:53:45 +05:30
|
|
|
raw = G.ftp_arg;
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-16 20:23:54 +05:30
|
|
|
/* PORT command format makes sense only over IPv4 */
|
2009-03-17 01:24:06 +05:30
|
|
|
if (!raw
|
|
|
|
#ifdef WHY_BOTHER_WE_CAN_ASSUME_IP_MATCHES
|
|
|
|
|| G.local_addr->u.sa.sa_family != AF_INET
|
|
|
|
#endif
|
|
|
|
) {
|
2009-03-16 20:23:54 +05:30
|
|
|
bail:
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_BADCMD);
|
2009-03-16 20:23:54 +05:30
|
|
|
return;
|
|
|
|
}
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-16 20:23:54 +05:30
|
|
|
comma = strrchr(raw, ',');
|
|
|
|
if (comma == NULL)
|
|
|
|
goto bail;
|
|
|
|
*comma = '\0';
|
|
|
|
port = bb_strtou(&comma[1], NULL, 10);
|
|
|
|
if (errno || port > 0xff)
|
2009-03-09 05:16:48 +05:30
|
|
|
goto bail;
|
|
|
|
|
2009-03-16 20:23:54 +05:30
|
|
|
comma = strrchr(raw, ',');
|
|
|
|
if (comma == NULL)
|
2009-03-09 05:16:48 +05:30
|
|
|
goto bail;
|
2009-03-16 20:23:54 +05:30
|
|
|
*comma = '\0';
|
|
|
|
port_hi = bb_strtou(&comma[1], NULL, 10);
|
|
|
|
if (errno || port_hi > 0xff)
|
|
|
|
goto bail;
|
|
|
|
port |= port_hi << 8;
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-17 01:24:06 +05:30
|
|
|
#ifdef WHY_BOTHER_WE_CAN_ASSUME_IP_MATCHES
|
2009-03-09 07:53:45 +05:30
|
|
|
replace_char(raw, ',', '.');
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-16 20:23:54 +05:30
|
|
|
/* We are verifying that PORT's IP matches getpeername().
|
|
|
|
* Otherwise peer can make us open data connections
|
|
|
|
* to other hosts (security problem!)
|
|
|
|
* This code would be too simplistic:
|
|
|
|
* lsa = xdotted2sockaddr(raw, port);
|
|
|
|
* if (lsa == NULL) goto bail;
|
|
|
|
*/
|
|
|
|
if (!inet_aton(raw, &port_ipv4_sin_addr))
|
|
|
|
goto bail;
|
|
|
|
peer_ipv4_len = sizeof(peer_ipv4);
|
|
|
|
if (getpeername(STDIN_FILENO, &peer_ipv4, &peer_ipv4_len) != 0)
|
|
|
|
goto bail;
|
|
|
|
if (memcmp(&port_ipv4_sin_addr, &peer_ipv4.sin_addr, sizeof(struct in_addr)) != 0)
|
|
|
|
goto bail;
|
2009-03-15 21:24:58 +05:30
|
|
|
|
2009-03-16 20:23:54 +05:30
|
|
|
G.port_addr = xdotted2sockaddr(raw, port);
|
2009-03-17 01:24:06 +05:30
|
|
|
#else
|
|
|
|
G.port_addr = get_peer_lsa(STDIN_FILENO);
|
2009-03-17 02:31:41 +05:30
|
|
|
set_nport(G.port_addr, htons(port));
|
2009-03-17 01:24:06 +05:30
|
|
|
#endif
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_PORTOK);
|
2009-03-09 05:16:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_rest(void)
|
|
|
|
{
|
|
|
|
/* When ftp_arg == NULL simply restart from beginning */
|
|
|
|
G.restart_pos = G.ftp_arg ? xatoi_u(G.ftp_arg) : 0;
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_RESTOK);
|
2009-03-09 05:16:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_retr(void)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
2009-03-15 21:24:58 +05:30
|
|
|
off_t bytes_transferred;
|
2009-03-09 05:16:48 +05:30
|
|
|
int remote_fd;
|
2009-03-15 21:24:58 +05:30
|
|
|
int local_file_fd;
|
2009-03-09 05:16:48 +05:30
|
|
|
off_t offset = G.restart_pos;
|
|
|
|
char *response;
|
|
|
|
|
|
|
|
G.restart_pos = 0;
|
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
if (!port_or_pasv_was_seen())
|
|
|
|
return; /* port_or_pasv_was_seen emitted error response */
|
2009-03-09 05:16:48 +05:30
|
|
|
|
|
|
|
/* O_NONBLOCK is useful if file happens to be a device node */
|
2009-03-15 21:24:58 +05:30
|
|
|
local_file_fd = G.ftp_arg ? open(G.ftp_arg, O_RDONLY | O_NONBLOCK) : -1;
|
|
|
|
if (local_file_fd < 0) {
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_FILEFAIL);
|
2009-03-09 05:16:48 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
if (fstat(local_file_fd, &statbuf) != 0 || !S_ISREG(statbuf.st_mode)) {
|
2009-03-09 05:16:48 +05:30
|
|
|
/* Note - pretend open failed */
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_FILEFAIL);
|
2009-03-09 05:16:48 +05:30
|
|
|
goto file_close_out;
|
|
|
|
}
|
2009-03-16 21:49:53 +05:30
|
|
|
G.local_file_fd = local_file_fd;
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
/* Now deactive O_NONBLOCK, otherwise we have a problem
|
|
|
|
* on DMAPI filesystems such as XFS DMAPI.
|
2009-03-09 05:16:48 +05:30
|
|
|
*/
|
2009-03-15 21:24:58 +05:30
|
|
|
ndelay_off(local_file_fd);
|
2009-03-09 05:16:48 +05:30
|
|
|
|
|
|
|
/* Set the download offset (from REST) if any */
|
|
|
|
if (offset != 0)
|
2009-03-15 21:24:58 +05:30
|
|
|
xlseek(local_file_fd, offset, SEEK_SET);
|
2009-03-09 05:16:48 +05:30
|
|
|
|
|
|
|
response = xasprintf(
|
2009-03-18 19:55:28 +05:30
|
|
|
" Opening BINARY connection for %s (%"OFF_FMT"u bytes)",
|
2009-03-09 05:16:48 +05:30
|
|
|
G.ftp_arg, statbuf.st_size);
|
|
|
|
remote_fd = get_remote_transfer_fd(response);
|
|
|
|
free(response);
|
|
|
|
if (remote_fd < 0)
|
2009-03-15 21:24:58 +05:30
|
|
|
goto file_close_out;
|
|
|
|
|
|
|
|
bytes_transferred = bb_copyfd_eof(local_file_fd, remote_fd);
|
2009-03-09 21:16:07 +05:30
|
|
|
close(remote_fd);
|
2009-03-15 21:24:58 +05:30
|
|
|
if (bytes_transferred < 0)
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_BADSENDFILE);
|
2009-03-09 05:16:48 +05:30
|
|
|
else
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_TRANSFEROK);
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-09 07:53:45 +05:30
|
|
|
file_close_out:
|
2009-03-15 21:24:58 +05:30
|
|
|
close(local_file_fd);
|
2009-03-16 21:49:53 +05:30
|
|
|
G.local_file_fd = 0;
|
2009-03-09 05:16:48 +05:30
|
|
|
}
|
|
|
|
|
2009-03-09 08:45:05 +05:30
|
|
|
/* List commands */
|
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
static int
|
|
|
|
popen_ls(const char *opt)
|
2009-03-08 15:01:28 +05:30
|
|
|
{
|
2009-03-09 21:16:07 +05:30
|
|
|
char *cwd;
|
2009-03-18 19:42:22 +05:30
|
|
|
const char *argv[] = {
|
|
|
|
"ftpd",
|
|
|
|
opt,
|
|
|
|
BB_MMU ? "--" : NULL,
|
|
|
|
G.ftp_arg,
|
|
|
|
NULL
|
|
|
|
};
|
2009-03-09 21:16:07 +05:30
|
|
|
struct fd_pair outfd;
|
|
|
|
pid_t pid;
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
cwd = xrealloc_getcwd_or_warn(NULL);
|
|
|
|
xpiped_pair(outfd);
|
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
/*fflush(NULL); - so far we dont use stdio on output */
|
2009-03-18 19:42:22 +05:30
|
|
|
pid = BB_MMU ? fork() : vfork();
|
|
|
|
if (pid < 0)
|
|
|
|
bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
/* child */
|
|
|
|
#if !BB_MMU
|
|
|
|
if (fchdir(G.root_fd) != 0)
|
|
|
|
_exit(127);
|
|
|
|
close(G.root_fd);
|
|
|
|
#endif
|
|
|
|
/* NB: close _first_, then move fd! */
|
2009-03-09 21:16:07 +05:30
|
|
|
close(outfd.rd);
|
|
|
|
xmove_fd(outfd.wr, STDOUT_FILENO);
|
2009-03-18 19:42:22 +05:30
|
|
|
/* Opening /dev/null in chroot is hard.
|
|
|
|
* Just making sure STDIN_FILENO is opened
|
|
|
|
* to something harmless. Paranoia,
|
|
|
|
* ls won't read it anyway */
|
2009-03-09 21:16:07 +05:30
|
|
|
close(STDIN_FILENO);
|
2009-03-18 19:42:22 +05:30
|
|
|
dup(STDOUT_FILENO); /* copy will become STDIN_FILENO */
|
|
|
|
#if !BB_MMU
|
|
|
|
/* ftpd ls helper chdirs to argv[2],
|
|
|
|
* preventing peer from seeing real root we are in now
|
|
|
|
*/
|
|
|
|
argv[2] = cwd;
|
|
|
|
/* + 1: we must use relative path here if in chroot.
|
|
|
|
* For example, execv("/proc/self/exe") will fail, since
|
|
|
|
* it looks for "/proc/self/exe" _relative to chroot!_ */
|
|
|
|
execv(CONFIG_BUSYBOX_EXEC_PATH + 1, (char**) argv);
|
2009-03-09 21:16:07 +05:30
|
|
|
_exit(127);
|
2009-03-18 19:42:22 +05:30
|
|
|
#else
|
|
|
|
memset(&G, 0, sizeof(G));
|
|
|
|
exit(ls_main(ARRAY_SIZE(argv) - 1, (char**) argv));
|
|
|
|
#endif
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
2009-03-15 21:24:58 +05:30
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
/* parent */
|
|
|
|
close(outfd.wr);
|
|
|
|
free(cwd);
|
|
|
|
return outfd.rd;
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
enum {
|
|
|
|
USE_CTRL_CONN = 1,
|
|
|
|
LONG_LISTING = 2,
|
|
|
|
};
|
|
|
|
|
2009-03-08 15:01:28 +05:30
|
|
|
static void
|
2009-03-09 21:16:07 +05:30
|
|
|
handle_dir_common(int opts)
|
2009-03-08 15:01:28 +05:30
|
|
|
{
|
2009-03-09 21:16:07 +05:30
|
|
|
FILE *ls_fp;
|
|
|
|
char *line;
|
|
|
|
int ls_fd;
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
if (!(opts & USE_CTRL_CONN) && !port_or_pasv_was_seen())
|
|
|
|
return; /* port_or_pasv_was_seen emitted error response */
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-16 20:23:54 +05:30
|
|
|
/* -n prevents user/groupname display,
|
|
|
|
* which can be problematic in chroot */
|
2009-03-15 21:24:58 +05:30
|
|
|
ls_fd = popen_ls((opts & LONG_LISTING) ? "-l" : "-1");
|
2009-03-09 21:16:07 +05:30
|
|
|
ls_fp = fdopen(ls_fd, "r");
|
|
|
|
if (!ls_fp) /* never happens. paranoia */
|
|
|
|
bb_perror_msg_and_die("fdopen");
|
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
if (opts & USE_CTRL_CONN) {
|
2009-03-09 21:16:07 +05:30
|
|
|
/* STAT <filename> */
|
2009-03-18 19:55:28 +05:30
|
|
|
cmdio_write_raw(STR(FTP_STATFILE_OK)"-File status:\r\n");
|
2009-03-09 21:16:07 +05:30
|
|
|
while (1) {
|
|
|
|
line = xmalloc_fgetline(ls_fp);
|
|
|
|
if (!line)
|
|
|
|
break;
|
2009-03-18 21:32:54 +05:30
|
|
|
/* Hack: 0 results in no status at all */
|
|
|
|
/* Note: it's ok that we don't prepend space,
|
|
|
|
* ftp.kernel.org doesn't do that too */
|
|
|
|
cmdio_write(0, line);
|
2009-03-09 21:16:07 +05:30
|
|
|
free(line);
|
|
|
|
}
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_STATFILE_OK);
|
2009-03-08 15:01:28 +05:30
|
|
|
} else {
|
2009-03-09 21:16:07 +05:30
|
|
|
/* LIST/NLST [<filename>] */
|
2009-03-18 19:55:28 +05:30
|
|
|
int remote_fd = get_remote_transfer_fd(" Directory listing");
|
2009-03-09 21:16:07 +05:30
|
|
|
if (remote_fd >= 0) {
|
|
|
|
while (1) {
|
|
|
|
line = xmalloc_fgetline(ls_fp);
|
|
|
|
if (!line)
|
|
|
|
break;
|
2009-03-15 21:24:58 +05:30
|
|
|
/* I've seen clients complaining when they
|
|
|
|
* are fed with ls output with bare '\n'.
|
|
|
|
* Pity... that would be much simpler.
|
|
|
|
*/
|
2009-03-17 18:33:06 +05:30
|
|
|
/* TODO: need to s/LF/NUL/g here */
|
2009-03-09 21:16:07 +05:30
|
|
|
xwrite_str(remote_fd, line);
|
|
|
|
xwrite(remote_fd, "\r\n", 2);
|
|
|
|
free(line);
|
|
|
|
}
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
2009-03-09 21:16:07 +05:30
|
|
|
close(remote_fd);
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_TRANSFEROK);
|
2009-03-09 21:16:07 +05:30
|
|
|
}
|
|
|
|
fclose(ls_fp); /* closes ls_fd too */
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
static void
|
|
|
|
handle_list(void)
|
|
|
|
{
|
2009-03-15 21:24:58 +05:30
|
|
|
handle_dir_common(LONG_LISTING);
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
2009-03-09 05:16:48 +05:30
|
|
|
static void
|
|
|
|
handle_nlst(void)
|
|
|
|
{
|
2009-03-17 18:33:06 +05:30
|
|
|
/* NLST returns list of names, "\r\n" terminated without regard
|
|
|
|
* to the current binary flag. Names may start with "/",
|
|
|
|
* then they represent full names (we don't produce such names),
|
|
|
|
* otherwise names are relative to current directory.
|
|
|
|
* Embedded "\n" are replaced by NULs. This is safe since names
|
|
|
|
* can never contain NUL.
|
|
|
|
*/
|
2009-03-09 21:16:07 +05:30
|
|
|
handle_dir_common(0);
|
2009-03-09 05:16:48 +05:30
|
|
|
}
|
|
|
|
static void
|
|
|
|
handle_stat_file(void)
|
|
|
|
{
|
2009-03-15 21:24:58 +05:30
|
|
|
handle_dir_common(LONG_LISTING + USE_CTRL_CONN);
|
2009-03-09 05:16:48 +05:30
|
|
|
}
|
|
|
|
|
2009-03-18 06:15:00 +05:30
|
|
|
/* This can be extended to handle MLST, as all info is available
|
|
|
|
* in struct stat for that:
|
|
|
|
* MLST file_name
|
|
|
|
* 250-Listing file_name
|
|
|
|
* type=file;size=4161;modify=19970214165800; /dir/dir/file_name
|
|
|
|
* 250 End
|
|
|
|
* Nano-doc:
|
|
|
|
* MLST [<file or dir name, "." assumed if not given>]
|
|
|
|
* Returned name should be either the same as requested, or fully qualified.
|
|
|
|
* If there was no parameter, return "" or (preferred) fully-qualified name.
|
|
|
|
* Returned "facts" (case is not important):
|
|
|
|
* size - size in octets
|
|
|
|
* modify - last modification time
|
|
|
|
* type - entry type (file,dir,OS.unix=block)
|
|
|
|
* (+ cdir and pdir types for MLSD)
|
|
|
|
* unique - unique id of file/directory (inode#)
|
|
|
|
* perm -
|
|
|
|
* a: can be appended to (APPE)
|
|
|
|
* d: can be deleted (RMD/DELE)
|
|
|
|
* f: can be renamed (RNFR)
|
|
|
|
* r: can be read (RETR)
|
|
|
|
* w: can be written (STOR)
|
|
|
|
* e: can CWD into this dir
|
|
|
|
* l: this dir can be listed (dir only!)
|
|
|
|
* c: can create files in this dir
|
|
|
|
* m: can create dirs in this dir (MKD)
|
|
|
|
* p: can delete files in this dir
|
|
|
|
* UNIX.mode - unix file mode
|
|
|
|
*/
|
2009-03-09 18:31:08 +05:30
|
|
|
static void
|
2009-03-18 06:15:00 +05:30
|
|
|
handle_size_or_mdtm(int need_size)
|
2009-03-09 18:31:08 +05:30
|
|
|
{
|
|
|
|
struct stat statbuf;
|
2009-03-18 06:15:00 +05:30
|
|
|
struct tm broken_out;
|
|
|
|
char buf[(sizeof("NNN %"OFF_FMT"u\r\n") + sizeof(off_t) * 3)
|
|
|
|
| sizeof("NNN YYYYMMDDhhmmss\r\n")
|
|
|
|
];
|
2009-03-09 18:31:08 +05:30
|
|
|
|
|
|
|
if (!G.ftp_arg
|
|
|
|
|| stat(G.ftp_arg, &statbuf) != 0
|
|
|
|
|| !S_ISREG(statbuf.st_mode)
|
|
|
|
) {
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_FILEFAIL);
|
2009-03-09 18:31:08 +05:30
|
|
|
return;
|
|
|
|
}
|
2009-03-18 06:15:00 +05:30
|
|
|
if (need_size) {
|
|
|
|
sprintf(buf, STR(FTP_STATFILE_OK)" %"OFF_FMT"u\r\n", statbuf.st_size);
|
|
|
|
} else {
|
|
|
|
gmtime_r(&statbuf.st_mtime, &broken_out);
|
|
|
|
sprintf(buf, STR(FTP_STATFILE_OK)" %04u%02u%02u%02u%02u%02u\r\n",
|
|
|
|
broken_out.tm_year + 1900,
|
|
|
|
broken_out.tm_mon,
|
|
|
|
broken_out.tm_mday,
|
|
|
|
broken_out.tm_hour,
|
|
|
|
broken_out.tm_min,
|
|
|
|
broken_out.tm_sec);
|
|
|
|
}
|
2009-03-09 18:31:08 +05:30
|
|
|
cmdio_write_raw(buf);
|
|
|
|
}
|
|
|
|
|
2009-03-09 08:45:05 +05:30
|
|
|
/* Upload commands */
|
2009-03-09 05:16:48 +05:30
|
|
|
|
2009-03-08 15:01:28 +05:30
|
|
|
#if ENABLE_FEATURE_FTP_WRITE
|
|
|
|
static void
|
|
|
|
handle_mkd(void)
|
|
|
|
{
|
2009-03-09 05:16:48 +05:30
|
|
|
if (!G.ftp_arg || mkdir(G.ftp_arg, 0777) != 0) {
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_FILEFAIL);
|
2009-03-08 15:01:28 +05:30
|
|
|
return;
|
|
|
|
}
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_MKDIROK);
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_rmd(void)
|
|
|
|
{
|
2009-03-09 05:16:48 +05:30
|
|
|
if (!G.ftp_arg || rmdir(G.ftp_arg) != 0) {
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_FILEFAIL);
|
2009-03-09 05:16:48 +05:30
|
|
|
return;
|
|
|
|
}
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_RMDIROK);
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_dele(void)
|
|
|
|
{
|
2009-03-09 05:16:48 +05:30
|
|
|
if (!G.ftp_arg || unlink(G.ftp_arg) != 0) {
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_FILEFAIL);
|
2009-03-09 05:16:48 +05:30
|
|
|
return;
|
|
|
|
}
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_DELEOK);
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_rnfr(void)
|
|
|
|
{
|
|
|
|
free(G.rnfr_filename);
|
2009-03-09 05:16:48 +05:30
|
|
|
G.rnfr_filename = xstrdup(G.ftp_arg);
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_RNFROK);
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_rnto(void)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
/* If we didn't get a RNFR, throw a wobbly */
|
2009-03-09 05:16:48 +05:30
|
|
|
if (G.rnfr_filename == NULL || G.ftp_arg == NULL) {
|
2009-03-18 19:55:28 +05:30
|
|
|
cmdio_write_raw(STR(FTP_NEEDRNFR)" Use RNFR first\r\n");
|
2009-03-08 15:01:28 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = rename(G.rnfr_filename, G.ftp_arg);
|
|
|
|
free(G.rnfr_filename);
|
2009-03-09 05:16:48 +05:30
|
|
|
G.rnfr_filename = NULL;
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 05:16:48 +05:30
|
|
|
if (retval) {
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_FILEFAIL);
|
2009-03-09 05:16:48 +05:30
|
|
|
return;
|
|
|
|
}
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_RENAMEOK);
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
|
2009-03-09 07:53:45 +05:30
|
|
|
static void
|
|
|
|
handle_upload_common(int is_append, int is_unique)
|
|
|
|
{
|
2009-03-16 20:23:54 +05:30
|
|
|
struct stat statbuf;
|
|
|
|
char *tempname;
|
2009-03-15 21:24:58 +05:30
|
|
|
off_t bytes_transferred;
|
2009-03-16 20:23:54 +05:30
|
|
|
off_t offset;
|
2009-03-09 07:53:45 +05:30
|
|
|
int local_file_fd;
|
|
|
|
int remote_fd;
|
|
|
|
|
|
|
|
offset = G.restart_pos;
|
|
|
|
G.restart_pos = 0;
|
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
if (!port_or_pasv_was_seen())
|
|
|
|
return; /* port_or_pasv_was_seen emitted error response */
|
2009-03-09 07:53:45 +05:30
|
|
|
|
2009-03-16 20:23:54 +05:30
|
|
|
tempname = NULL;
|
2009-03-09 07:53:45 +05:30
|
|
|
local_file_fd = -1;
|
|
|
|
if (is_unique) {
|
2009-03-09 09:43:59 +05:30
|
|
|
tempname = xstrdup(" FILE: uniq.XXXXXX");
|
|
|
|
local_file_fd = mkstemp(tempname + 7);
|
2009-03-09 07:53:45 +05:30
|
|
|
} else if (G.ftp_arg) {
|
|
|
|
int flags = O_WRONLY | O_CREAT | O_TRUNC;
|
|
|
|
if (is_append)
|
|
|
|
flags = O_WRONLY | O_CREAT | O_APPEND;
|
|
|
|
if (offset)
|
|
|
|
flags = O_WRONLY | O_CREAT;
|
|
|
|
local_file_fd = open(G.ftp_arg, flags, 0666);
|
|
|
|
}
|
2009-03-16 20:23:54 +05:30
|
|
|
|
|
|
|
if (local_file_fd < 0
|
|
|
|
|| fstat(local_file_fd, &statbuf) != 0
|
|
|
|
|| !S_ISREG(statbuf.st_mode)
|
|
|
|
) {
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_UPLOADFAIL);
|
2009-03-16 20:23:54 +05:30
|
|
|
if (local_file_fd >= 0)
|
|
|
|
goto close_local_and_bail;
|
2009-03-09 07:53:45 +05:30
|
|
|
return;
|
|
|
|
}
|
2009-03-16 21:49:53 +05:30
|
|
|
G.local_file_fd = local_file_fd;
|
2009-03-09 07:53:45 +05:30
|
|
|
|
|
|
|
if (offset)
|
|
|
|
xlseek(local_file_fd, offset, SEEK_SET);
|
|
|
|
|
2009-03-09 09:43:59 +05:30
|
|
|
remote_fd = get_remote_transfer_fd(tempname ? tempname : " Ok to send data");
|
2009-03-09 07:53:45 +05:30
|
|
|
free(tempname);
|
|
|
|
|
|
|
|
if (remote_fd < 0)
|
2009-03-16 20:23:54 +05:30
|
|
|
goto close_local_and_bail;
|
2009-03-09 07:53:45 +05:30
|
|
|
|
2009-03-15 21:24:58 +05:30
|
|
|
bytes_transferred = bb_copyfd_eof(remote_fd, local_file_fd);
|
|
|
|
close(remote_fd);
|
|
|
|
if (bytes_transferred < 0)
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_ERR(FTP_BADSENDFILE);
|
2009-03-09 07:53:45 +05:30
|
|
|
else
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_TRANSFEROK);
|
2009-03-16 20:23:54 +05:30
|
|
|
|
|
|
|
close_local_and_bail:
|
2009-03-09 07:53:45 +05:30
|
|
|
close(local_file_fd);
|
2009-03-16 21:49:53 +05:30
|
|
|
G.local_file_fd = 0;
|
2009-03-09 07:53:45 +05:30
|
|
|
}
|
|
|
|
|
2009-03-08 15:01:28 +05:30
|
|
|
static void
|
2009-03-09 05:16:48 +05:30
|
|
|
handle_stor(void)
|
2009-03-08 15:01:28 +05:30
|
|
|
{
|
2009-03-09 05:16:48 +05:30
|
|
|
handle_upload_common(0, 0);
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_appe(void)
|
|
|
|
{
|
2009-03-09 07:53:45 +05:30
|
|
|
G.restart_pos = 0;
|
2009-03-08 15:01:28 +05:30
|
|
|
handle_upload_common(1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_stou(void)
|
|
|
|
{
|
2009-03-09 07:53:45 +05:30
|
|
|
G.restart_pos = 0;
|
2009-03-08 15:01:28 +05:30
|
|
|
handle_upload_common(0, 1);
|
|
|
|
}
|
2009-03-09 05:16:48 +05:30
|
|
|
#endif /* ENABLE_FEATURE_FTP_WRITE */
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 07:53:45 +05:30
|
|
|
static uint32_t
|
|
|
|
cmdio_get_cmd_and_arg(void)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
uint32_t cmdval;
|
|
|
|
char *cmd;
|
|
|
|
|
2009-03-16 21:49:53 +05:30
|
|
|
alarm(G.timeout);
|
|
|
|
|
2009-03-09 10:08:37 +05:30
|
|
|
free(G.ftp_cmd);
|
2009-03-09 07:53:45 +05:30
|
|
|
len = 8 * 1024; /* Paranoia. Peer may send 1 gigabyte long cmd... */
|
2009-03-18 23:02:44 +05:30
|
|
|
G.ftp_cmd = cmd = xmalloc_fgets_str_len(stdin, "\r\n", &len);
|
2009-03-09 07:53:45 +05:30
|
|
|
if (!cmd)
|
|
|
|
exit(0);
|
|
|
|
|
2009-03-18 23:02:44 +05:30
|
|
|
/* De-escape telnet: 0xff,0xff => 0xff */
|
|
|
|
/* RFC959 says that ABOR, STAT, QUIT may be sent even during
|
|
|
|
* data transfer, and may be preceded by telnet's "Interrupt Process"
|
|
|
|
* code (two-byte sequence 255,244) and then by telnet "Synch" code
|
|
|
|
* 255,242 (byte 242 is sent with TCP URG bit using send(MSG_OOB)
|
|
|
|
* and may generate SIGURG on our side. See RFC854).
|
|
|
|
* So far we don't support that (may install SIGURG handler if we'd want to),
|
|
|
|
* but we need to at least remove 255,xxx pairs. lftp sends those. */
|
|
|
|
/* Then de-escape FTP: NUL => '\n' */
|
|
|
|
/* Testing for \xff:
|
|
|
|
* Create file named '\xff': echo Hello >`echo -ne "\xff"`
|
|
|
|
* Try to get it: ftpget -v 127.0.0.1 Eff `echo -ne "\xff\xff"`
|
|
|
|
* (need "\xff\xff" until ftpget applet is fixed to do escaping :)
|
|
|
|
* Testing for embedded LF:
|
|
|
|
* LF_HERE=`echo -ne "LF\nHERE"`
|
|
|
|
* echo Hello >"$LF_HERE"
|
|
|
|
* ftpget -v 127.0.0.1 LF_HERE "$LF_HERE"
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
int dst, src;
|
|
|
|
|
|
|
|
/* Strip "\r\n" if it is there */
|
|
|
|
if (len != 0 && cmd[len - 1] == '\n') {
|
|
|
|
len--;
|
|
|
|
if (len != 0 && cmd[len - 1] == '\r')
|
|
|
|
len--;
|
|
|
|
cmd[len] = '\0';
|
|
|
|
}
|
|
|
|
src = strchrnul(cmd, 0xff) - cmd;
|
|
|
|
/* 99,99% there are neither NULs nor 255s and src == len */
|
|
|
|
if (src < len) {
|
|
|
|
dst = src;
|
|
|
|
do {
|
|
|
|
if ((unsigned char)(cmd[src]) == 255) {
|
|
|
|
src++;
|
|
|
|
/* 255,xxx - skip 255 */
|
|
|
|
if ((unsigned char)(cmd[src]) != 255) {
|
|
|
|
/* 255,!255 - skip both */
|
|
|
|
src++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* 255,255 - retain one 255 */
|
|
|
|
}
|
|
|
|
/* NUL => '\n' */
|
|
|
|
cmd[dst++] = cmd[src] ? cmd[src] : '\n';
|
|
|
|
src++;
|
|
|
|
} while (src < len);
|
|
|
|
cmd[dst] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2009-03-17 17:53:24 +05:30
|
|
|
|
|
|
|
if (G.verbose > 1)
|
2009-03-17 18:10:34 +05:30
|
|
|
verbose_log(cmd);
|
2009-03-09 07:53:45 +05:30
|
|
|
|
|
|
|
G.ftp_arg = strchr(cmd, ' ');
|
2009-03-16 20:23:54 +05:30
|
|
|
if (G.ftp_arg != NULL)
|
|
|
|
*G.ftp_arg++ = '\0';
|
|
|
|
|
|
|
|
/* Uppercase and pack into uint32_t first word of the command */
|
2009-03-09 07:53:45 +05:30
|
|
|
cmdval = 0;
|
|
|
|
while (*cmd)
|
|
|
|
cmdval = (cmdval << 8) + ((unsigned char)*cmd++ & (unsigned char)~0x20);
|
|
|
|
|
|
|
|
return cmdval;
|
|
|
|
}
|
|
|
|
|
2009-03-16 20:23:54 +05:30
|
|
|
#define mk_const4(a,b,c,d) (((a * 0x100 + b) * 0x100 + c) * 0x100 + d)
|
|
|
|
#define mk_const3(a,b,c) ((a * 0x100 + b) * 0x100 + c)
|
|
|
|
enum {
|
|
|
|
const_ALLO = mk_const4('A', 'L', 'L', 'O'),
|
|
|
|
const_APPE = mk_const4('A', 'P', 'P', 'E'),
|
|
|
|
const_CDUP = mk_const4('C', 'D', 'U', 'P'),
|
|
|
|
const_CWD = mk_const3('C', 'W', 'D'),
|
|
|
|
const_DELE = mk_const4('D', 'E', 'L', 'E'),
|
|
|
|
const_EPSV = mk_const4('E', 'P', 'S', 'V'),
|
2009-03-17 18:10:34 +05:30
|
|
|
const_FEAT = mk_const4('F', 'E', 'A', 'T'),
|
2009-03-16 20:23:54 +05:30
|
|
|
const_HELP = mk_const4('H', 'E', 'L', 'P'),
|
|
|
|
const_LIST = mk_const4('L', 'I', 'S', 'T'),
|
2009-03-18 06:15:00 +05:30
|
|
|
const_MDTM = mk_const4('M', 'D', 'T', 'M'),
|
2009-03-16 20:23:54 +05:30
|
|
|
const_MKD = mk_const3('M', 'K', 'D'),
|
|
|
|
const_MODE = mk_const4('M', 'O', 'D', 'E'),
|
|
|
|
const_NLST = mk_const4('N', 'L', 'S', 'T'),
|
|
|
|
const_NOOP = mk_const4('N', 'O', 'O', 'P'),
|
|
|
|
const_PASS = mk_const4('P', 'A', 'S', 'S'),
|
|
|
|
const_PASV = mk_const4('P', 'A', 'S', 'V'),
|
|
|
|
const_PORT = mk_const4('P', 'O', 'R', 'T'),
|
|
|
|
const_PWD = mk_const3('P', 'W', 'D'),
|
|
|
|
const_QUIT = mk_const4('Q', 'U', 'I', 'T'),
|
|
|
|
const_REST = mk_const4('R', 'E', 'S', 'T'),
|
|
|
|
const_RETR = mk_const4('R', 'E', 'T', 'R'),
|
|
|
|
const_RMD = mk_const3('R', 'M', 'D'),
|
|
|
|
const_RNFR = mk_const4('R', 'N', 'F', 'R'),
|
|
|
|
const_RNTO = mk_const4('R', 'N', 'T', 'O'),
|
|
|
|
const_SIZE = mk_const4('S', 'I', 'Z', 'E'),
|
|
|
|
const_STAT = mk_const4('S', 'T', 'A', 'T'),
|
|
|
|
const_STOR = mk_const4('S', 'T', 'O', 'R'),
|
|
|
|
const_STOU = mk_const4('S', 'T', 'O', 'U'),
|
|
|
|
const_STRU = mk_const4('S', 'T', 'R', 'U'),
|
|
|
|
const_SYST = mk_const4('S', 'Y', 'S', 'T'),
|
|
|
|
const_TYPE = mk_const4('T', 'Y', 'P', 'E'),
|
|
|
|
const_USER = mk_const4('U', 'S', 'E', 'R'),
|
|
|
|
|
2009-03-18 19:42:22 +05:30
|
|
|
#if !BB_MMU
|
2009-03-16 20:23:54 +05:30
|
|
|
OPT_l = (1 << 0),
|
|
|
|
OPT_1 = (1 << 1),
|
2009-03-18 19:42:22 +05:30
|
|
|
#endif
|
|
|
|
OPT_v = (1 << ((!BB_MMU) * 2 + 0)),
|
|
|
|
OPT_S = (1 << ((!BB_MMU) * 2 + 1)),
|
|
|
|
OPT_w = (1 << ((!BB_MMU) * 2 + 2)) * ENABLE_FEATURE_FTP_WRITE,
|
2009-03-16 20:23:54 +05:30
|
|
|
};
|
|
|
|
|
2009-03-08 15:01:28 +05:30
|
|
|
int ftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2009-03-18 19:42:22 +05:30
|
|
|
#if !BB_MMU
|
2009-03-09 21:16:07 +05:30
|
|
|
int ftpd_main(int argc, char **argv)
|
2009-03-18 19:42:22 +05:30
|
|
|
#else
|
|
|
|
int ftpd_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
#endif
|
2009-03-08 15:01:28 +05:30
|
|
|
{
|
2009-03-17 10:41:51 +05:30
|
|
|
unsigned abs_timeout;
|
2009-03-09 21:16:07 +05:30
|
|
|
smallint opts;
|
|
|
|
|
2009-03-16 21:49:53 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2009-03-17 10:41:51 +05:30
|
|
|
abs_timeout = 1 * 60 * 60;
|
2009-03-16 21:49:53 +05:30
|
|
|
G.timeout = 2 * 60;
|
2009-03-17 17:53:24 +05:30
|
|
|
opt_complementary = "t+:T+:vv";
|
2009-03-18 19:42:22 +05:30
|
|
|
#if BB_MMU
|
|
|
|
opts = getopt32(argv, "vS" USE_FEATURE_FTP_WRITE("w") "t:T:", &G.timeout, &abs_timeout, &G.verbose);
|
|
|
|
#else
|
2009-03-17 17:53:24 +05:30
|
|
|
opts = getopt32(argv, "l1vS" USE_FEATURE_FTP_WRITE("w") "t:T:", &G.timeout, &abs_timeout, &G.verbose);
|
2009-03-09 21:16:07 +05:30
|
|
|
if (opts & (OPT_l|OPT_1)) {
|
|
|
|
/* Our secret backdoor to ls */
|
2009-03-16 20:23:54 +05:30
|
|
|
/* TODO: pass -n too? */
|
2009-03-17 10:41:51 +05:30
|
|
|
/* --group-directories-first would be nice, but ls don't do that yet */
|
2009-03-09 21:16:07 +05:30
|
|
|
xchdir(argv[2]);
|
|
|
|
argv[2] = (char*)"--";
|
2009-03-18 19:42:22 +05:30
|
|
|
memset(&G, 0, sizeof(G));
|
2009-03-09 21:16:07 +05:30
|
|
|
return ls_main(argc, argv);
|
|
|
|
}
|
2009-03-18 19:42:22 +05:30
|
|
|
#endif
|
2009-03-17 17:53:24 +05:30
|
|
|
if (abs_timeout | G.timeout) {
|
|
|
|
if (abs_timeout == 0)
|
|
|
|
abs_timeout = INT_MAX;
|
|
|
|
G.end_time = monotonic_sec() + abs_timeout;
|
|
|
|
if (G.timeout > abs_timeout)
|
|
|
|
G.timeout = abs_timeout;
|
|
|
|
}
|
2009-03-17 10:41:51 +05:30
|
|
|
strcpy(G.msg_ok + 4, MSG_OK );
|
|
|
|
strcpy(G.msg_err + 4, MSG_ERR);
|
2009-03-08 15:01:28 +05:30
|
|
|
|
|
|
|
G.local_addr = get_sock_lsa(STDIN_FILENO);
|
|
|
|
if (!G.local_addr) {
|
|
|
|
/* This is confusing:
|
|
|
|
* bb_error_msg_and_die("stdin is not a socket");
|
|
|
|
* Better: */
|
|
|
|
bb_show_usage();
|
|
|
|
/* Help text says that ftpd must be used as inetd service,
|
|
|
|
* which is by far the most usual cause of get_sock_lsa
|
|
|
|
* failure */
|
|
|
|
}
|
|
|
|
|
2009-03-09 21:16:07 +05:30
|
|
|
if (!(opts & OPT_v))
|
2009-03-11 20:37:44 +05:30
|
|
|
logmode = LOGMODE_NONE;
|
|
|
|
if (opts & OPT_S) {
|
|
|
|
/* LOG_NDELAY is needed since we may chroot later */
|
|
|
|
openlog(applet_name, LOG_PID | LOG_NDELAY, LOG_DAEMON);
|
|
|
|
logmode |= LOGMODE_SYSLOG;
|
|
|
|
}
|
2009-03-17 17:53:24 +05:30
|
|
|
if (logmode)
|
|
|
|
applet_name = xasprintf("%s[%u]", applet_name, (int)getpid());
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-18 19:42:22 +05:30
|
|
|
#if !BB_MMU
|
|
|
|
G.root_fd = xopen("/", O_RDONLY | O_DIRECTORY);
|
|
|
|
#endif
|
2009-03-09 21:16:07 +05:30
|
|
|
|
2009-03-08 15:01:28 +05:30
|
|
|
if (argv[optind]) {
|
|
|
|
xchdir(argv[optind]);
|
|
|
|
chroot(".");
|
|
|
|
}
|
|
|
|
|
2009-03-09 05:16:48 +05:30
|
|
|
//umask(077); - admin can set umask before starting us
|
2009-03-08 15:01:28 +05:30
|
|
|
|
|
|
|
/* Signals. We'll always take -EPIPE rather than a rude signal, thanks */
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
2009-03-09 05:16:48 +05:30
|
|
|
/* Set up options on the command socket (do we need these all? why?) */
|
2009-03-08 15:01:28 +05:30
|
|
|
setsockopt(STDIN_FILENO, IPPROTO_TCP, TCP_NODELAY, &const_int_1, sizeof(const_int_1));
|
|
|
|
setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
|
2009-03-18 21:32:54 +05:30
|
|
|
/* Telnet protocol over command link may send "urgent" data,
|
|
|
|
* we prefer it to be received in the "normal" data stream: */
|
2009-03-08 15:01:28 +05:30
|
|
|
setsockopt(STDIN_FILENO, SOL_SOCKET, SO_OOBINLINE, &const_int_1, sizeof(const_int_1));
|
|
|
|
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_GREET);
|
2009-03-16 21:49:53 +05:30
|
|
|
signal(SIGALRM, timeout_handler);
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 05:16:48 +05:30
|
|
|
#ifdef IF_WE_WANT_TO_REQUIRE_LOGIN
|
|
|
|
{
|
|
|
|
smallint user_was_specified = 0;
|
|
|
|
while (1) {
|
|
|
|
uint32_t cmdval = cmdio_get_cmd_and_arg();
|
|
|
|
|
|
|
|
if (cmdval == const_USER) {
|
|
|
|
if (G.ftp_arg == NULL || strcasecmp(G.ftp_arg, "anonymous") != 0)
|
2009-03-09 09:43:59 +05:30
|
|
|
cmdio_write_raw(STR(FTP_LOGINERR)" Server is anonymous only\r\n");
|
2009-03-09 05:16:48 +05:30
|
|
|
else {
|
|
|
|
user_was_specified = 1;
|
2009-03-09 09:43:59 +05:30
|
|
|
cmdio_write_raw(STR(FTP_GIVEPWORD)" Please specify the password\r\n");
|
2009-03-09 05:16:48 +05:30
|
|
|
}
|
|
|
|
} else if (cmdval == const_PASS) {
|
|
|
|
if (user_was_specified)
|
|
|
|
break;
|
2009-03-09 09:43:59 +05:30
|
|
|
cmdio_write_raw(STR(FTP_NEEDUSER)" Login with USER\r\n");
|
2009-03-09 05:16:48 +05:30
|
|
|
} else if (cmdval == const_QUIT) {
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_GOODBYE);
|
2009-03-09 05:16:48 +05:30
|
|
|
return 0;
|
|
|
|
} else {
|
2009-03-09 09:43:59 +05:30
|
|
|
cmdio_write_raw(STR(FTP_LOGINERR)" Login with USER and PASS\r\n");
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_LOGINOK);
|
2009-03-09 05:16:48 +05:30
|
|
|
#endif
|
2009-03-08 15:01:28 +05:30
|
|
|
|
2009-03-09 05:16:48 +05:30
|
|
|
/* RFC-959 Section 5.1
|
|
|
|
* The following commands and options MUST be supported by every
|
|
|
|
* server-FTP and user-FTP, except in cases where the underlying
|
|
|
|
* file system or operating system does not allow or support
|
|
|
|
* a particular command.
|
|
|
|
* Type: ASCII Non-print, IMAGE, LOCAL 8
|
|
|
|
* Mode: Stream
|
|
|
|
* Structure: File, Record*
|
|
|
|
* (Record structure is REQUIRED only for hosts whose file
|
|
|
|
* systems support record structure).
|
|
|
|
* Commands:
|
|
|
|
* USER, PASS, ACCT, [bbox: ACCT not supported]
|
|
|
|
* PORT, PASV,
|
|
|
|
* TYPE, MODE, STRU,
|
|
|
|
* RETR, STOR, APPE,
|
|
|
|
* RNFR, RNTO, DELE,
|
|
|
|
* CWD, CDUP, RMD, MKD, PWD,
|
|
|
|
* LIST, NLST,
|
|
|
|
* SYST, STAT,
|
|
|
|
* HELP, NOOP, QUIT.
|
|
|
|
*/
|
|
|
|
/* ACCOUNT (ACCT)
|
2009-03-09 09:48:00 +05:30
|
|
|
* "The argument field is a Telnet string identifying the user's account.
|
2009-03-09 05:16:48 +05:30
|
|
|
* The command is not necessarily related to the USER command, as some
|
|
|
|
* sites may require an account for login and others only for specific
|
|
|
|
* access, such as storing files. In the latter case the command may
|
|
|
|
* arrive at any time.
|
|
|
|
* There are reply codes to differentiate these cases for the automation:
|
|
|
|
* when account information is required for login, the response to
|
|
|
|
* a successful PASSword command is reply code 332. On the other hand,
|
|
|
|
* if account information is NOT required for login, the reply to
|
|
|
|
* a successful PASSword command is 230; and if the account information
|
|
|
|
* is needed for a command issued later in the dialogue, the server
|
|
|
|
* should return a 332 or 532 reply depending on whether it stores
|
|
|
|
* (pending receipt of the ACCounT command) or discards the command,
|
2009-03-09 09:48:00 +05:30
|
|
|
* respectively."
|
2009-03-09 05:16:48 +05:30
|
|
|
*/
|
2009-03-08 15:01:28 +05:30
|
|
|
|
|
|
|
while (1) {
|
|
|
|
uint32_t cmdval = cmdio_get_cmd_and_arg();
|
|
|
|
|
|
|
|
if (cmdval == const_QUIT) {
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_GOODBYE);
|
2009-03-08 15:01:28 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2009-03-09 18:31:08 +05:30
|
|
|
else if (cmdval == const_USER)
|
2009-03-18 06:15:00 +05:30
|
|
|
/* This would mean "ok, now give me PASS". */
|
|
|
|
/*WRITE_OK(FTP_GIVEPWORD);*/
|
|
|
|
/* vsftpd can be configured to not require that,
|
|
|
|
* and this also saves one roundtrip:
|
|
|
|
*/
|
|
|
|
WRITE_OK(FTP_LOGINOK);
|
2009-03-09 18:31:08 +05:30
|
|
|
else if (cmdval == const_PASS)
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_LOGINOK);
|
2009-03-09 18:31:08 +05:30
|
|
|
else if (cmdval == const_NOOP)
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_NOOPOK);
|
2009-03-09 18:31:08 +05:30
|
|
|
else if (cmdval == const_TYPE)
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_TYPEOK);
|
2009-03-09 18:31:08 +05:30
|
|
|
else if (cmdval == const_STRU)
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_STRUOK);
|
2009-03-09 18:31:08 +05:30
|
|
|
else if (cmdval == const_MODE)
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_MODEOK);
|
2009-03-09 18:31:08 +05:30
|
|
|
else if (cmdval == const_ALLO)
|
2009-03-17 02:24:45 +05:30
|
|
|
WRITE_OK(FTP_ALLOOK);
|
2009-03-09 18:31:08 +05:30
|
|
|
else if (cmdval == const_SYST)
|
|
|
|
cmdio_write_raw(STR(FTP_SYSTOK)" UNIX Type: L8\r\n");
|
|
|
|
else if (cmdval == const_PWD)
|
2009-03-08 15:01:28 +05:30
|
|
|
handle_pwd();
|
|
|
|
else if (cmdval == const_CWD)
|
|
|
|
handle_cwd();
|
2009-03-09 05:16:48 +05:30
|
|
|
else if (cmdval == const_CDUP) /* cd .. */
|
2009-03-08 15:01:28 +05:30
|
|
|
handle_cdup();
|
2009-03-17 18:10:34 +05:30
|
|
|
/* HELP is nearly useless, but we can reuse FEAT for it */
|
2009-03-18 06:15:00 +05:30
|
|
|
/* lftp uses FEAT */
|
2009-03-17 18:10:34 +05:30
|
|
|
else if (cmdval == const_HELP || cmdval == const_FEAT)
|
2009-03-18 06:15:00 +05:30
|
|
|
handle_feat(cmdval == const_HELP
|
|
|
|
? STRNUM32(FTP_HELP)
|
|
|
|
: STRNUM32(FTP_STATOK)
|
|
|
|
);
|
2009-03-09 05:16:48 +05:30
|
|
|
else if (cmdval == const_LIST) /* ls -l */
|
2009-03-08 15:01:28 +05:30
|
|
|
handle_list();
|
2009-03-09 05:16:48 +05:30
|
|
|
else if (cmdval == const_NLST) /* "name list", bare ls */
|
2009-03-08 15:01:28 +05:30
|
|
|
handle_nlst();
|
2009-03-18 06:15:00 +05:30
|
|
|
/* SIZE is crucial for wget's download indicator etc */
|
|
|
|
/* Mozilla, lftp use MDTM (presumably for caching) */
|
|
|
|
else if (cmdval == const_SIZE || cmdval == const_MDTM)
|
|
|
|
handle_size_or_mdtm(cmdval == const_SIZE);
|
2009-03-09 05:16:48 +05:30
|
|
|
else if (cmdval == const_STAT) {
|
|
|
|
if (G.ftp_arg == NULL)
|
|
|
|
handle_stat();
|
|
|
|
else
|
|
|
|
handle_stat_file();
|
2009-03-09 18:31:08 +05:30
|
|
|
}
|
|
|
|
else if (cmdval == const_PASV)
|
|
|
|
handle_pasv();
|
|
|
|
else if (cmdval == const_EPSV)
|
|
|
|
handle_epsv();
|
|
|
|
else if (cmdval == const_RETR)
|
|
|
|
handle_retr();
|
|
|
|
else if (cmdval == const_PORT)
|
|
|
|
handle_port();
|
|
|
|
else if (cmdval == const_REST)
|
|
|
|
handle_rest();
|
2009-03-08 15:01:28 +05:30
|
|
|
#if ENABLE_FEATURE_FTP_WRITE
|
2009-03-09 21:16:07 +05:30
|
|
|
else if (opts & OPT_w) {
|
2009-03-08 15:01:28 +05:30
|
|
|
if (cmdval == const_STOR)
|
|
|
|
handle_stor();
|
|
|
|
else if (cmdval == const_MKD)
|
|
|
|
handle_mkd();
|
|
|
|
else if (cmdval == const_RMD)
|
|
|
|
handle_rmd();
|
|
|
|
else if (cmdval == const_DELE)
|
|
|
|
handle_dele();
|
2009-03-09 05:16:48 +05:30
|
|
|
else if (cmdval == const_RNFR) /* "rename from" */
|
2009-03-08 15:01:28 +05:30
|
|
|
handle_rnfr();
|
2009-03-09 05:16:48 +05:30
|
|
|
else if (cmdval == const_RNTO) /* "rename to" */
|
2009-03-08 15:01:28 +05:30
|
|
|
handle_rnto();
|
|
|
|
else if (cmdval == const_APPE)
|
|
|
|
handle_appe();
|
2009-03-09 05:16:48 +05:30
|
|
|
else if (cmdval == const_STOU) /* "store unique" */
|
2009-03-08 15:01:28 +05:30
|
|
|
handle_stou();
|
|
|
|
}
|
|
|
|
#endif
|
2009-03-09 05:16:48 +05:30
|
|
|
#if 0
|
2009-03-08 15:01:28 +05:30
|
|
|
else if (cmdval == const_STOR
|
|
|
|
|| cmdval == const_MKD
|
|
|
|
|| cmdval == const_RMD
|
|
|
|
|| cmdval == const_DELE
|
|
|
|
|| cmdval == const_RNFR
|
|
|
|
|| cmdval == const_RNTO
|
|
|
|
|| cmdval == const_APPE
|
|
|
|
|| cmdval == const_STOU
|
|
|
|
) {
|
2009-03-09 09:43:59 +05:30
|
|
|
cmdio_write_raw(STR(FTP_NOPERM)" Permission denied\r\n");
|
2009-03-09 05:16:48 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
2009-03-09 08:21:46 +05:30
|
|
|
/* Which unsupported commands were seen in the wild?
|
2009-03-09 05:16:48 +05:30
|
|
|
* (doesn't necessarily mean "we must support them")
|
2009-03-18 06:15:00 +05:30
|
|
|
* foo 1.2.3: XXXX - comment
|
2009-03-09 05:16:48 +05:30
|
|
|
*/
|
2009-03-09 09:43:59 +05:30
|
|
|
cmdio_write_raw(STR(FTP_BADCMD)" Unknown command\r\n");
|
2009-03-08 15:01:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|