2016-01-22 21:30:26 +01:00
|
|
|
/* This is FTP server implementation is based on RFC 959
|
|
|
|
* (https://tools.ietf.org/html/rfc959) and suggested implementation details
|
|
|
|
* from https://cr.yp.to/ftp/filesystem.html
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
#include "ftp.h"
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <ctype.h>
|
2015-08-19 20:25:34 +02:00
|
|
|
#include <dirent.h>
|
2014-11-23 23:39:00 +01:00
|
|
|
#include <errno.h>
|
2014-11-25 09:30:55 +01:00
|
|
|
#include <fcntl.h>
|
2016-01-22 02:25:22 +01:00
|
|
|
#include <inttypes.h>
|
2014-11-23 23:39:00 +01:00
|
|
|
#include <malloc.h>
|
2016-06-14 09:42:51 +02:00
|
|
|
#include <math.h>
|
2016-01-15 19:13:28 +01:00
|
|
|
#include <netdb.h>
|
2014-11-23 23:39:00 +01:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <stdarg.h>
|
2015-08-19 20:25:34 +02:00
|
|
|
#include <stdio.h>
|
2014-11-23 23:39:00 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
2015-08-19 20:25:34 +02:00
|
|
|
#include <sys/stat.h>
|
2016-06-14 08:26:44 +02:00
|
|
|
#include <sys/statvfs.h>
|
2016-01-22 02:25:22 +01:00
|
|
|
#include <time.h>
|
2014-11-23 23:39:00 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#ifdef _3DS
|
|
|
|
#include <3ds.h>
|
2015-08-19 20:25:34 +02:00
|
|
|
#define lstat stat
|
2016-01-15 23:45:10 +01:00
|
|
|
#else
|
2016-01-16 00:47:18 +01:00
|
|
|
#include <stdbool.h>
|
2016-01-15 23:45:10 +01:00
|
|
|
#define BIT(x) (1<<(x))
|
2014-11-23 23:39:00 +01:00
|
|
|
#endif
|
|
|
|
#include "console.h"
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
#define POLL_UNKNOWN (~(POLLIN|POLLPRI|POLLOUT))
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-12 23:24:01 +01:00
|
|
|
#define XFER_BUFFERSIZE 32768
|
|
|
|
#define SOCK_BUFFERSIZE 32768
|
|
|
|
#define FILE_BUFFERSIZE 65536
|
2016-01-22 02:25:22 +01:00
|
|
|
#define CMD_BUFFERSIZE 4096
|
2016-01-12 23:24:01 +01:00
|
|
|
#define SOCU_ALIGN 0x1000
|
|
|
|
#define SOCU_BUFFERSIZE 0x100000
|
2014-11-25 09:30:55 +01:00
|
|
|
#define LISTEN_PORT 5000
|
2014-11-23 23:39:00 +01:00
|
|
|
#ifdef _3DS
|
2014-11-25 09:30:55 +01:00
|
|
|
#define DATA_PORT (LISTEN_PORT+1)
|
2014-11-23 23:39:00 +01:00
|
|
|
#else
|
2014-11-25 09:30:55 +01:00
|
|
|
#define DATA_PORT 0 /* ephemeral port */
|
2014-11-23 23:39:00 +01:00
|
|
|
#endif
|
|
|
|
|
2014-11-25 08:22:09 +01:00
|
|
|
typedef struct ftp_session_t ftp_session_t;
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
#define FTP_DECLARE(x) static int x(ftp_session_t *session, const char *args)
|
2016-01-22 02:25:22 +01:00
|
|
|
FTP_DECLARE(ABOR);
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(ALLO);
|
|
|
|
FTP_DECLARE(APPE);
|
|
|
|
FTP_DECLARE(CDUP);
|
|
|
|
FTP_DECLARE(CWD);
|
|
|
|
FTP_DECLARE(DELE);
|
|
|
|
FTP_DECLARE(FEAT);
|
2016-01-22 02:25:22 +01:00
|
|
|
FTP_DECLARE(HELP);
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(LIST);
|
2016-01-22 23:38:22 +01:00
|
|
|
FTP_DECLARE(MDTM);
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(MKD);
|
|
|
|
FTP_DECLARE(MODE);
|
|
|
|
FTP_DECLARE(NLST);
|
|
|
|
FTP_DECLARE(NOOP);
|
2015-08-19 20:25:34 +02:00
|
|
|
FTP_DECLARE(OPTS);
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(PASS);
|
|
|
|
FTP_DECLARE(PASV);
|
|
|
|
FTP_DECLARE(PORT);
|
|
|
|
FTP_DECLARE(PWD);
|
|
|
|
FTP_DECLARE(QUIT);
|
|
|
|
FTP_DECLARE(REST);
|
|
|
|
FTP_DECLARE(RETR);
|
|
|
|
FTP_DECLARE(RMD);
|
|
|
|
FTP_DECLARE(RNFR);
|
|
|
|
FTP_DECLARE(RNTO);
|
2016-06-22 05:17:33 +02:00
|
|
|
FTP_DECLARE(SIZE);
|
2016-01-22 02:25:22 +01:00
|
|
|
FTP_DECLARE(STAT);
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(STOR);
|
|
|
|
FTP_DECLARE(STOU);
|
|
|
|
FTP_DECLARE(STRU);
|
|
|
|
FTP_DECLARE(SYST);
|
|
|
|
FTP_DECLARE(TYPE);
|
|
|
|
FTP_DECLARE(USER);
|
|
|
|
|
|
|
|
/*! session state */
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
COMMAND_STATE, /*!< waiting for a command */
|
|
|
|
DATA_CONNECT_STATE, /*!< waiting for connection after PASV command */
|
|
|
|
DATA_TRANSFER_STATE, /*!< data transfer in progress */
|
|
|
|
} session_state_t;
|
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
/*! ftp_session_set_state flags */
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
CLOSE_PASV = BIT(0), /*!< Close the pasv_fd */
|
|
|
|
CLOSE_DATA = BIT(1), /*!< Close the data_fd */
|
|
|
|
} set_state_flags_t;
|
|
|
|
|
|
|
|
/*! ftp_session_t flags */
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
SESSION_BINARY = BIT(0), /*!< data transfers in binary mode */
|
|
|
|
SESSION_PASV = BIT(1), /*!< have pasv_addr ready for data transfer command */
|
|
|
|
SESSION_PORT = BIT(2), /*!< have peer_addr ready for data transfer command */
|
|
|
|
SESSION_RECV = BIT(3), /*!< data transfer in source mode */
|
|
|
|
SESSION_SEND = BIT(4), /*!< data transfer in sink mode */
|
|
|
|
SESSION_RENAME = BIT(5), /*!< last command was RNFR and buffer contains path */
|
2016-01-16 00:47:18 +01:00
|
|
|
SESSION_NLST = BIT(6), /*!< list command is NLST */
|
2016-01-22 02:25:22 +01:00
|
|
|
SESSION_URGENT = BIT(7), /*!< in telnet urgent mode */
|
2016-01-15 23:45:10 +01:00
|
|
|
} session_flags_t;
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
/*! ftp session */
|
2014-11-25 08:22:09 +01:00
|
|
|
struct ftp_session_t
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
|
|
|
char cwd[4096]; /*!< current working directory */
|
2016-01-22 02:25:22 +01:00
|
|
|
char lwd[4096]; /*!< list working directory */
|
2014-11-23 23:39:00 +01:00
|
|
|
struct sockaddr_in peer_addr; /*!< peer address for data connection */
|
|
|
|
struct sockaddr_in pasv_addr; /*!< listen address for PASV connection */
|
|
|
|
int cmd_fd; /*!< socket for command connection */
|
|
|
|
int pasv_fd; /*!< listen socket for PASV */
|
|
|
|
int data_fd; /*!< socket for data transfer */
|
2016-01-22 23:38:22 +01:00
|
|
|
time_t timestamp; /*!< time from last command */
|
2016-01-15 23:45:10 +01:00
|
|
|
session_flags_t flags; /*!< session flags */
|
2014-11-23 23:39:00 +01:00
|
|
|
session_state_t state; /*!< session state */
|
|
|
|
ftp_session_t *next; /*!< link to next session */
|
|
|
|
ftp_session_t *prev; /*!< link to prev session */
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
loop_status_t (*transfer)(ftp_session_t*); /*! data transfer callback */
|
2016-01-12 23:24:01 +01:00
|
|
|
char buffer[XFER_BUFFERSIZE]; /*! persistent data between callbacks */
|
|
|
|
char tmp_buffer[XFER_BUFFERSIZE]; /*! persistent data between callbacks */
|
|
|
|
char file_buffer[FILE_BUFFERSIZE]; /*! stdio file buffer */
|
2016-01-22 02:25:22 +01:00
|
|
|
char cmd_buffer[CMD_BUFFERSIZE]; /*! command buffer */
|
2016-01-12 23:24:01 +01:00
|
|
|
size_t bufferpos; /*! persistent buffer position between callbacks */
|
|
|
|
size_t buffersize; /*! persistent buffer size between callbacks */
|
2016-01-22 02:25:22 +01:00
|
|
|
size_t cmd_buffersize;
|
2016-01-12 23:24:01 +01:00
|
|
|
uint64_t filepos; /*! persistent file position between callbacks */
|
|
|
|
uint64_t filesize; /*! persistent file size between callbacks */
|
2016-01-15 23:45:10 +01:00
|
|
|
FILE *fp; /*! persistent open file pointer between callbacks */
|
|
|
|
DIR *dp; /*! persistent open directory pointer between callbacks */
|
2014-11-23 23:39:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*! ftp command descriptor */
|
|
|
|
typedef struct ftp_command
|
|
|
|
{
|
|
|
|
const char *name; /*!< command name */
|
|
|
|
int (*handler)(ftp_session_t*, const char*); /*!< command callback */
|
|
|
|
} ftp_command_t;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! ftp command list */
|
2014-11-23 23:39:00 +01:00
|
|
|
static ftp_command_t ftp_commands[] =
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! ftp command */
|
2014-11-23 23:39:00 +01:00
|
|
|
#define FTP_COMMAND(x) { #x, x, }
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! ftp alias */
|
2014-11-23 23:39:00 +01:00
|
|
|
#define FTP_ALIAS(x,y) { #x, y, }
|
2016-01-22 02:25:22 +01:00
|
|
|
FTP_COMMAND(ABOR),
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_COMMAND(ALLO),
|
|
|
|
FTP_COMMAND(APPE),
|
|
|
|
FTP_COMMAND(CDUP),
|
|
|
|
FTP_COMMAND(CWD),
|
|
|
|
FTP_COMMAND(DELE),
|
|
|
|
FTP_COMMAND(FEAT),
|
2016-01-22 02:25:22 +01:00
|
|
|
FTP_COMMAND(HELP),
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_COMMAND(LIST),
|
2016-01-22 23:38:22 +01:00
|
|
|
FTP_COMMAND(MDTM),
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_COMMAND(MKD),
|
|
|
|
FTP_COMMAND(MODE),
|
|
|
|
FTP_COMMAND(NLST),
|
|
|
|
FTP_COMMAND(NOOP),
|
2015-08-19 20:25:34 +02:00
|
|
|
FTP_COMMAND(OPTS),
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_COMMAND(PASS),
|
|
|
|
FTP_COMMAND(PASV),
|
|
|
|
FTP_COMMAND(PORT),
|
|
|
|
FTP_COMMAND(PWD),
|
|
|
|
FTP_COMMAND(QUIT),
|
|
|
|
FTP_COMMAND(REST),
|
|
|
|
FTP_COMMAND(RETR),
|
|
|
|
FTP_COMMAND(RMD),
|
|
|
|
FTP_COMMAND(RNFR),
|
|
|
|
FTP_COMMAND(RNTO),
|
2016-06-22 05:17:33 +02:00
|
|
|
FTP_COMMAND(SIZE),
|
2016-01-22 02:25:22 +01:00
|
|
|
FTP_COMMAND(STAT),
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_COMMAND(STOR),
|
|
|
|
FTP_COMMAND(STOU),
|
|
|
|
FTP_COMMAND(STRU),
|
|
|
|
FTP_COMMAND(SYST),
|
|
|
|
FTP_COMMAND(TYPE),
|
|
|
|
FTP_COMMAND(USER),
|
|
|
|
FTP_ALIAS(XCUP, CDUP),
|
2016-01-22 02:25:22 +01:00
|
|
|
FTP_ALIAS(XCWD, CWD),
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_ALIAS(XMKD, MKD),
|
|
|
|
FTP_ALIAS(XPWD, PWD),
|
|
|
|
FTP_ALIAS(XRMD, RMD),
|
|
|
|
};
|
|
|
|
/*! number of ftp commands */
|
|
|
|
static const size_t num_ftp_commands = sizeof(ftp_commands)/sizeof(ftp_commands[0]);
|
|
|
|
|
2016-06-15 10:04:00 +02:00
|
|
|
static void update_free_space(void);
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
/*! compare ftp command descriptors
|
|
|
|
*
|
|
|
|
* @param[in] p1 left side of comparison (ftp_command_t*)
|
|
|
|
* @param[in] p2 right side of comparison (ftp_command_t*)
|
|
|
|
*
|
|
|
|
* @returns <0 if p1 < p2
|
|
|
|
* @returns 0 if p1 == p2
|
|
|
|
* @returns >0 if p1 > p2
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ftp_command_cmp(const void *p1,
|
|
|
|
const void *p2)
|
|
|
|
{
|
|
|
|
ftp_command_t *c1 = (ftp_command_t*)p1;
|
|
|
|
ftp_command_t *c2 = (ftp_command_t*)p2;
|
|
|
|
|
|
|
|
/* ordered by command name */
|
|
|
|
return strcasecmp(c1->name, c2->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _3DS
|
|
|
|
/*! SOC service buffer */
|
2016-01-12 23:24:01 +01:00
|
|
|
static u32 *SOCU_buffer = NULL;
|
2016-08-11 05:53:05 +02:00
|
|
|
|
|
|
|
/*! Whether LCD is powered */
|
|
|
|
static bool lcd_power = true;
|
|
|
|
|
|
|
|
/*! aptHook cookie */
|
|
|
|
static aptHookCookie cookie;
|
2014-11-23 23:39:00 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*! server listen address */
|
|
|
|
static struct sockaddr_in serv_addr;
|
|
|
|
/*! listen file descriptor */
|
|
|
|
static int listenfd = -1;
|
2016-01-12 23:24:01 +01:00
|
|
|
#ifdef _3DS
|
2014-11-25 08:22:09 +01:00
|
|
|
/*! current data port */
|
2015-08-19 20:25:34 +02:00
|
|
|
static in_port_t data_port = DATA_PORT;
|
2016-01-12 23:24:01 +01:00
|
|
|
#endif
|
2014-11-23 23:39:00 +01:00
|
|
|
/*! list of ftp sessions */
|
2015-08-19 20:25:34 +02:00
|
|
|
static ftp_session_t *sessions = NULL;
|
2016-01-12 23:24:01 +01:00
|
|
|
/*! socket buffersize */
|
|
|
|
static int sock_buffersize = SOCK_BUFFERSIZE;
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! server start time */
|
|
|
|
static time_t start_time = 0;
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2014-11-25 08:22:09 +01:00
|
|
|
/*! Allocate a new data port
|
|
|
|
*
|
|
|
|
* @returns next data port
|
|
|
|
*/
|
|
|
|
static in_port_t
|
|
|
|
next_data_port(void)
|
|
|
|
{
|
|
|
|
#ifdef _3DS
|
|
|
|
if(++data_port >= 10000)
|
|
|
|
data_port = DATA_PORT;
|
|
|
|
return data_port;
|
|
|
|
#else
|
|
|
|
return 0; /* ephemeral port */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-11-25 09:30:55 +01:00
|
|
|
/*! set a socket to non-blocking
|
|
|
|
*
|
|
|
|
* @param[in] fd socket
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ftp_set_socket_nonblocking(int fd)
|
|
|
|
{
|
|
|
|
int rc, flags;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* get the socket flags */
|
2014-11-25 09:30:55 +01:00
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
if(flags == -1)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "fcntl: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-25 09:30:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* add O_NONBLOCK to the socket flags */
|
2014-11-25 09:30:55 +01:00
|
|
|
rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "fcntl: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-25 09:30:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:24:01 +01:00
|
|
|
/*! set socket options
|
|
|
|
*
|
|
|
|
* @param[in] fd socket
|
2016-01-15 23:45:10 +01:00
|
|
|
*
|
|
|
|
* @returns failure
|
2016-01-12 23:24:01 +01:00
|
|
|
*/
|
2016-01-15 23:45:10 +01:00
|
|
|
static int
|
2016-01-12 23:24:01 +01:00
|
|
|
ftp_set_socket_options(int fd)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* increase receive buffer size */
|
2016-01-12 23:24:01 +01:00
|
|
|
rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
|
|
|
|
&sock_buffersize, sizeof(sock_buffersize));
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-16 23:00:31 +01:00
|
|
|
console_print(RED "setsockopt: SO_RCVBUF %d %s\n" RESET, errno, strerror(errno));
|
2016-01-15 23:45:10 +01:00
|
|
|
return -1;
|
2016-01-12 23:24:01 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* increase send buffer size */
|
2016-01-12 23:24:01 +01:00
|
|
|
rc = setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
|
|
|
|
&sock_buffersize, sizeof(sock_buffersize));
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-16 23:00:31 +01:00
|
|
|
console_print(RED "setsockopt: SO_SNDBUF %d %s\n" RESET, errno, strerror(errno));
|
2016-01-15 23:45:10 +01:00
|
|
|
return -1;
|
2016-01-12 23:24:01 +01:00
|
|
|
}
|
2016-01-15 23:45:10 +01:00
|
|
|
|
|
|
|
return 0;
|
2016-01-12 23:24:01 +01:00
|
|
|
}
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
/*! close a socket
|
|
|
|
*
|
|
|
|
* @param[in] fd socket to close
|
|
|
|
* @param[in] connected whether this socket is connected
|
|
|
|
*/
|
|
|
|
static void
|
2016-01-16 00:28:59 +01:00
|
|
|
ftp_closesocket(int fd,
|
|
|
|
bool connected)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
socklen_t addrlen = sizeof(addr);
|
2016-01-19 00:45:04 +01:00
|
|
|
struct pollfd pollinfo;
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
if(connected)
|
|
|
|
{
|
|
|
|
/* get peer address and print */
|
|
|
|
rc = getpeername(fd, (struct sockaddr*)&addr, &addrlen);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "getpeername: %d %s\n" RESET, errno, strerror(errno));
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(YELLOW "closing connection to fd=%d\n" RESET, fd);
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
else
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(YELLOW "closing connection to %s:%u\n" RESET,
|
2014-11-23 23:39:00 +01:00
|
|
|
inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
|
|
|
|
|
|
|
|
/* shutdown connection */
|
2016-01-19 00:45:04 +01:00
|
|
|
rc = shutdown(fd, SHUT_WR);
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc != 0)
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "shutdown: %d %s\n" RESET, errno, strerror(errno));
|
2016-01-19 00:45:04 +01:00
|
|
|
|
|
|
|
/* wait for client to close connection */
|
|
|
|
pollinfo.fd = fd;
|
|
|
|
pollinfo.events = POLLIN;
|
|
|
|
pollinfo.revents = 0;
|
|
|
|
rc = poll(&pollinfo, 1, 250);
|
|
|
|
if(rc < 0)
|
|
|
|
console_print(RED "poll: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* close socket */
|
2015-08-19 20:25:34 +02:00
|
|
|
rc = close(fd);
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc != 0)
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "close: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*! close command socket on ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ftp_session_close_cmd(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
/* close command socket */
|
2016-01-15 23:45:10 +01:00
|
|
|
if(session->cmd_fd >= 0)
|
2016-01-16 00:28:59 +01:00
|
|
|
ftp_closesocket(session->cmd_fd, true);
|
2014-11-23 23:39:00 +01:00
|
|
|
session->cmd_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! close listen socket on ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ftp_session_close_pasv(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
/* close pasv socket */
|
2016-01-15 23:45:10 +01:00
|
|
|
if(session->pasv_fd >= 0)
|
|
|
|
{
|
|
|
|
console_print(YELLOW "stop listening on %s:%u\n" RESET,
|
|
|
|
inet_ntoa(session->pasv_addr.sin_addr),
|
|
|
|
ntohs(session->pasv_addr.sin_port));
|
|
|
|
|
2016-01-16 00:28:59 +01:00
|
|
|
ftp_closesocket(session->pasv_fd, false);
|
2016-01-15 23:45:10 +01:00
|
|
|
}
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
session->pasv_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! close data socket on ftp session
|
|
|
|
*
|
2016-01-22 02:25:22 +01:00
|
|
|
* @param[in] session ftp session
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
static void
|
|
|
|
ftp_session_close_data(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
/* close data connection */
|
2016-01-22 02:25:22 +01:00
|
|
|
if(session->data_fd >= 0 && session->data_fd != session->cmd_fd)
|
2016-01-16 00:28:59 +01:00
|
|
|
ftp_closesocket(session->data_fd, true);
|
2014-11-23 23:39:00 +01:00
|
|
|
session->data_fd = -1;
|
|
|
|
|
|
|
|
/* clear send/recv flags */
|
|
|
|
session->flags &= ~(SESSION_RECV|SESSION_SEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! close open file for ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ftp_session_close_file(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
if(session->fp != NULL)
|
|
|
|
{
|
|
|
|
rc = fclose(session->fp);
|
|
|
|
if(rc != 0)
|
|
|
|
console_print(RED "fclose: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
}
|
|
|
|
|
2016-01-19 00:46:03 +01:00
|
|
|
session->fp = NULL;
|
|
|
|
session->filepos = 0;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*! open file for reading for ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*
|
|
|
|
* @returns -1 for error
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ftp_session_open_file_read(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
/* open file in read mode */
|
2016-01-12 23:24:01 +01:00
|
|
|
session->fp = fopen(session->buffer, "rb");
|
|
|
|
if(session->fp == NULL)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
2016-01-12 23:24:01 +01:00
|
|
|
console_print(RED "fopen '%s': %d %s\n" RESET, session->buffer, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:24:01 +01:00
|
|
|
/* it's okay if this fails */
|
|
|
|
errno = 0;
|
|
|
|
rc = setvbuf(session->fp, session->file_buffer, _IOFBF, FILE_BUFFERSIZE);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
|
|
|
console_print(RED "setvbuf: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
}
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
/* get the file size */
|
2016-01-12 23:24:01 +01:00
|
|
|
rc = fstat(fileno(session->fp), &st);
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc != 0)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(RED "fstat '%s': %d %s\n" RESET, session->buffer, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
session->filesize = st.st_size;
|
|
|
|
|
2016-01-19 00:46:03 +01:00
|
|
|
if(session->filepos != 0)
|
|
|
|
{
|
|
|
|
rc = fseek(session->fp, session->filepos, SEEK_SET);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
|
|
|
console_print(RED "fseek '%s': %d %s\n" RESET, session->buffer, errno, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! read from an open file for ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*
|
|
|
|
* @returns bytes read
|
|
|
|
*/
|
|
|
|
static ssize_t
|
|
|
|
ftp_session_read_file(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
ssize_t rc;
|
|
|
|
|
|
|
|
/* read file at current position */
|
2016-01-12 23:24:01 +01:00
|
|
|
rc = fread(session->buffer, 1, sizeof(session->buffer), session->fp);
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc < 0)
|
|
|
|
{
|
2016-01-12 23:24:01 +01:00
|
|
|
console_print(RED "fread: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust file position */
|
|
|
|
session->filepos += rc;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! open file for writing for ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
2016-01-16 00:28:59 +01:00
|
|
|
* @param[in] append whether to append
|
2014-11-23 23:39:00 +01:00
|
|
|
*
|
|
|
|
* @returns -1 for error
|
|
|
|
*
|
|
|
|
* @note truncates file
|
|
|
|
*/
|
|
|
|
static int
|
2016-01-16 00:28:59 +01:00
|
|
|
ftp_session_open_file_write(ftp_session_t *session,
|
|
|
|
bool append)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
2016-01-16 00:28:59 +01:00
|
|
|
int rc;
|
|
|
|
const char *mode = "wb";
|
|
|
|
|
|
|
|
if(append)
|
|
|
|
mode = "ab";
|
2016-01-19 00:46:03 +01:00
|
|
|
else if(session->filepos != 0)
|
|
|
|
mode = "r+b";
|
2016-01-12 23:24:01 +01:00
|
|
|
|
2016-01-19 00:46:03 +01:00
|
|
|
/* open file in write mode */
|
2016-01-16 00:28:59 +01:00
|
|
|
session->fp = fopen(session->buffer, mode);
|
2016-01-12 23:24:01 +01:00
|
|
|
if(session->fp == NULL)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
2016-01-12 23:24:01 +01:00
|
|
|
console_print(RED "fopen '%s': %d %s\n" RESET, session->buffer, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-20 04:26:41 +02:00
|
|
|
update_free_space();
|
|
|
|
|
2016-01-12 23:24:01 +01:00
|
|
|
/* it's okay if this fails */
|
|
|
|
errno = 0;
|
|
|
|
rc = setvbuf(session->fp, session->file_buffer, _IOFBF, FILE_BUFFERSIZE);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
|
|
|
console_print(RED "setvbuf: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* check if this had REST but not APPE */
|
2016-01-19 00:46:03 +01:00
|
|
|
if(session->filepos != 0 && !append)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* seek to the REST offset */
|
2016-01-19 00:46:03 +01:00
|
|
|
rc = fseek(session->fp, session->filepos, SEEK_SET);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
|
|
|
console_print(RED "fseek '%s': %d %s\n" RESET, session->buffer, errno, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! write to an open file for ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*
|
|
|
|
* @returns bytes written
|
|
|
|
*/
|
|
|
|
static ssize_t
|
|
|
|
ftp_session_write_file(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
ssize_t rc;
|
|
|
|
|
|
|
|
/* write to file at current position */
|
2016-01-12 23:24:01 +01:00
|
|
|
rc = fwrite(session->buffer + session->bufferpos,
|
|
|
|
1, session->buffersize - session->bufferpos,
|
|
|
|
session->fp);
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc < 0)
|
|
|
|
{
|
2016-01-12 23:24:01 +01:00
|
|
|
console_print(RED "fwrite: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if(rc == 0)
|
2016-01-12 23:24:01 +01:00
|
|
|
console_print(RED "fwrite: wrote 0 bytes\n" RESET);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
/* adjust file position */
|
|
|
|
session->filepos += rc;
|
|
|
|
|
2016-06-15 10:04:00 +02:00
|
|
|
update_free_space();
|
2014-11-23 23:39:00 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! close current working directory for ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ftp_session_close_cwd(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* close open directory pointer */
|
2016-01-15 23:45:10 +01:00
|
|
|
if(session->dp != NULL)
|
|
|
|
{
|
|
|
|
rc = closedir(session->dp);
|
|
|
|
if(rc != 0)
|
|
|
|
console_print(RED "closedir: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
session->dp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! open current working directory for ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*
|
|
|
|
* @return -1 for failure
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ftp_session_open_cwd(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
/* open current working directory */
|
|
|
|
session->dp = opendir(session->cwd);
|
|
|
|
if(session->dp == NULL)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(RED "opendir '%s': %d %s\n" RESET, session->cwd, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! set state for ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] state state to set
|
2016-01-15 23:45:10 +01:00
|
|
|
* @param[in] flags flags
|
2014-11-23 23:39:00 +01:00
|
|
|
*/
|
|
|
|
static void
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(ftp_session_t *session,
|
|
|
|
session_state_t state,
|
|
|
|
set_state_flags_t flags)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
|
|
|
session->state = state;
|
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
/* close pasv and data sockets */
|
|
|
|
if(flags & CLOSE_PASV)
|
|
|
|
ftp_session_close_pasv(session);
|
|
|
|
if(flags & CLOSE_DATA)
|
|
|
|
ftp_session_close_data(session);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
if(state == COMMAND_STATE)
|
|
|
|
{
|
|
|
|
/* close file/cwd */
|
|
|
|
ftp_session_close_file(session);
|
|
|
|
ftp_session_close_cwd(session);
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! transfer loop
|
|
|
|
*
|
|
|
|
* Try to transfer as much data as the sockets will allow without blocking
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*/
|
2014-11-25 09:30:55 +01:00
|
|
|
static void
|
|
|
|
ftp_session_transfer(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
rc = session->transfer(session);
|
|
|
|
} while(rc == 0);
|
|
|
|
}
|
|
|
|
|
2016-01-22 21:30:26 +01:00
|
|
|
/*! encode a path
|
2016-01-22 02:25:22 +01:00
|
|
|
*
|
2016-01-22 21:30:26 +01:00
|
|
|
* @param[in] path path to encode
|
|
|
|
* @param[in,out] len path length
|
|
|
|
* @param[in] quotes whether to encode quotes
|
2016-01-22 02:25:22 +01:00
|
|
|
*
|
2016-01-22 21:30:26 +01:00
|
|
|
* @returns encoded path
|
2016-01-22 02:25:22 +01:00
|
|
|
*
|
2016-01-22 21:30:26 +01:00
|
|
|
* @note The caller must free the returned path
|
2016-01-22 02:25:22 +01:00
|
|
|
*/
|
|
|
|
static char*
|
2016-01-22 21:30:26 +01:00
|
|
|
encode_path(const char *path,
|
|
|
|
size_t *len,
|
|
|
|
bool quotes)
|
2016-01-22 02:25:22 +01:00
|
|
|
{
|
2016-01-22 21:30:26 +01:00
|
|
|
bool enc = false;
|
2016-01-22 02:25:22 +01:00
|
|
|
size_t i, diff = 0;
|
2016-01-22 21:30:26 +01:00
|
|
|
char *out, *p = (char*)path;
|
2016-01-22 02:25:22 +01:00
|
|
|
|
2016-01-22 21:30:26 +01:00
|
|
|
/* check for \n that needs to be encoded */
|
|
|
|
if(memchr(p, '\n', *len) != NULL)
|
|
|
|
enc = true;
|
2016-01-22 02:25:22 +01:00
|
|
|
|
|
|
|
if(quotes)
|
|
|
|
{
|
2016-01-22 21:30:26 +01:00
|
|
|
/* check for " that needs to be encoded */
|
|
|
|
p = (char*)path;
|
2016-01-22 02:25:22 +01:00
|
|
|
do
|
|
|
|
{
|
2016-01-22 21:30:26 +01:00
|
|
|
p = memchr(p, '"', path + *len - p);
|
2016-01-22 02:25:22 +01:00
|
|
|
if(p != NULL)
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
++diff;
|
|
|
|
}
|
|
|
|
} while(p != NULL);
|
|
|
|
}
|
|
|
|
|
2016-01-22 21:30:26 +01:00
|
|
|
/* check if an encode was needed */
|
|
|
|
if(!enc && diff == 0)
|
|
|
|
return strdup(path);
|
2016-01-22 02:25:22 +01:00
|
|
|
|
2016-01-22 21:30:26 +01:00
|
|
|
/* allocate space for encoded path */
|
2016-01-22 02:25:22 +01:00
|
|
|
p = out = (char*)malloc(*len + diff);
|
|
|
|
if(out == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2016-01-22 21:30:26 +01:00
|
|
|
/* copy the path while performing encoding */
|
2016-01-22 02:25:22 +01:00
|
|
|
for(i = 0; i < *len; ++i)
|
|
|
|
{
|
2016-01-22 21:30:26 +01:00
|
|
|
if(*path == '\n')
|
2016-01-22 02:25:22 +01:00
|
|
|
{
|
2016-01-22 21:30:26 +01:00
|
|
|
/* encoded \n is \0 */
|
2016-01-22 02:25:22 +01:00
|
|
|
*p++ = 0;
|
|
|
|
}
|
2016-01-22 21:30:26 +01:00
|
|
|
else if(quotes && *path == '"')
|
2016-01-22 02:25:22 +01:00
|
|
|
{
|
2016-01-22 21:30:26 +01:00
|
|
|
/* encoded " is "" */
|
|
|
|
*p++ = '"';
|
2016-01-22 02:25:22 +01:00
|
|
|
*p++ = '"';
|
|
|
|
}
|
|
|
|
else
|
2016-01-22 21:30:26 +01:00
|
|
|
*p++ = *path;
|
|
|
|
++path;
|
2016-01-22 02:25:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*len += diff;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-01-22 21:30:26 +01:00
|
|
|
/*! decode a path
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
2016-01-29 21:05:25 +01:00
|
|
|
* @param[in] len command length
|
2016-01-22 21:30:26 +01:00
|
|
|
*/
|
|
|
|
static void
|
2016-01-29 21:05:25 +01:00
|
|
|
decode_path(ftp_session_t *session,
|
|
|
|
size_t len)
|
2016-01-22 21:30:26 +01:00
|
|
|
{
|
2016-01-29 21:05:25 +01:00
|
|
|
size_t i;
|
2016-01-22 21:30:26 +01:00
|
|
|
|
|
|
|
/* decode \0 from the first command */
|
2016-01-29 21:05:25 +01:00
|
|
|
for(i = 0; i < len; ++i)
|
2016-01-22 21:30:26 +01:00
|
|
|
{
|
2016-01-29 21:05:25 +01:00
|
|
|
/* this is an encoded \n */
|
|
|
|
if(session->cmd_buffer[i] == 0)
|
|
|
|
session->cmd_buffer[i] = '\n';
|
2016-01-22 21:30:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! send a response on the command socket
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] buffer buffer to send
|
|
|
|
* @param[in] len buffer length
|
|
|
|
*
|
|
|
|
* @returns bytes sent to peer
|
|
|
|
*/
|
|
|
|
static ssize_t
|
|
|
|
ftp_send_response_buffer(ftp_session_t *session,
|
|
|
|
const char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
ssize_t rc, to_send;
|
|
|
|
|
|
|
|
/* send response */
|
|
|
|
to_send = len;
|
|
|
|
console_print(GREEN "%s" RESET, buffer);
|
|
|
|
rc = send(session->cmd_fd, buffer, to_send, 0);
|
|
|
|
if(rc < 0)
|
|
|
|
console_print(RED "send: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
else if(rc != to_send)
|
|
|
|
console_print(RED "only sent %u/%u bytes\n" RESET,
|
|
|
|
(unsigned int)rc, (unsigned int)to_send);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
__attribute__((format(printf,3,4)))
|
|
|
|
/*! send ftp response to ftp session's peer
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] code response code
|
|
|
|
* @param[in] fmt format string
|
|
|
|
* @param[in] ... format arguments
|
|
|
|
*
|
2016-01-22 02:25:22 +01:00
|
|
|
* returns bytes sent to peer
|
2014-11-23 23:39:00 +01:00
|
|
|
*/
|
|
|
|
static ssize_t
|
|
|
|
ftp_send_response(ftp_session_t *session,
|
|
|
|
int code,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
2014-11-25 09:30:55 +01:00
|
|
|
static char buffer[CMD_BUFFERSIZE];
|
2016-01-22 02:25:22 +01:00
|
|
|
ssize_t rc;
|
2014-11-23 23:39:00 +01:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
/* print response code and message to buffer */
|
|
|
|
va_start(ap, fmt);
|
2016-01-22 02:25:22 +01:00
|
|
|
if(code > 0)
|
2015-08-19 20:25:34 +02:00
|
|
|
rc = sprintf(buffer, "%d ", code);
|
|
|
|
else
|
2016-01-22 02:25:22 +01:00
|
|
|
rc = sprintf(buffer, "%d-", -code);
|
2014-11-23 23:39:00 +01:00
|
|
|
rc += vsnprintf(buffer+rc, sizeof(buffer)-rc, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if(rc >= sizeof(buffer))
|
|
|
|
{
|
|
|
|
/* couldn't fit message; just send code */
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(RED "%s: buffersize too small\n" RESET, __func__);
|
2016-01-22 02:25:22 +01:00
|
|
|
if(code > 0)
|
|
|
|
rc = sprintf(buffer, "%d \r\n", code);
|
|
|
|
else
|
|
|
|
rc = sprintf(buffer, "%d-\r\n", -code);
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
return ftp_send_response_buffer(session, buffer, rc);
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*! destroy ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
2016-01-22 02:25:22 +01:00
|
|
|
*
|
|
|
|
* @returns the next session in the list
|
2014-11-23 23:39:00 +01:00
|
|
|
*/
|
|
|
|
static ftp_session_t*
|
|
|
|
ftp_session_destroy(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
ftp_session_t *next = session->next;
|
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
/* close all sockets/files */
|
|
|
|
ftp_session_close_cmd(session);
|
|
|
|
ftp_session_close_pasv(session);
|
|
|
|
ftp_session_close_data(session);
|
|
|
|
ftp_session_close_file(session);
|
|
|
|
ftp_session_close_cwd(session);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
/* unlink from sessions list */
|
|
|
|
if(session->next)
|
|
|
|
session->next->prev = session->prev;
|
|
|
|
if(session == sessions)
|
|
|
|
sessions = session->next;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->prev->next = session->next;
|
|
|
|
if(session == sessions->prev)
|
|
|
|
sessions->prev = session->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* deallocate */
|
|
|
|
free(session);
|
|
|
|
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! allocate new ftp session
|
|
|
|
*
|
|
|
|
* @param[in] listen_fd socket to accept connection from
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ftp_session_new(int listen_fd)
|
|
|
|
{
|
|
|
|
ssize_t rc;
|
|
|
|
int new_fd;
|
|
|
|
ftp_session_t *session;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
socklen_t addrlen = sizeof(addr);
|
2016-01-15 19:13:28 +01:00
|
|
|
char host[NI_MAXHOST+1];
|
|
|
|
char serv[NI_MAXSERV+1];
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
/* accept connection */
|
|
|
|
new_fd = accept(listen_fd, (struct sockaddr*)&addr, &addrlen);
|
|
|
|
if(new_fd < 0)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "accept: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-15 19:13:28 +01:00
|
|
|
memset(host, 0, sizeof(host));
|
|
|
|
memset(serv, 0, sizeof(serv));
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* reverse dns lookup */
|
2016-01-15 19:13:28 +01:00
|
|
|
rc = getnameinfo((struct sockaddr*)&addr, addrlen,
|
2016-01-23 10:38:03 +01:00
|
|
|
host, sizeof(host), serv, sizeof(serv), 0);
|
2016-01-15 19:13:28 +01:00
|
|
|
if(rc != 0)
|
|
|
|
console_print(CYAN "accepted connection from %s:%u\n" RESET,
|
|
|
|
inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
|
|
|
|
else
|
|
|
|
console_print(CYAN "accepted connection from %s:%s\n" RESET,
|
|
|
|
host, serv);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
/* allocate a new session */
|
2016-01-19 00:46:03 +01:00
|
|
|
session = (ftp_session_t*)calloc(1, sizeof(ftp_session_t));
|
2014-11-23 23:39:00 +01:00
|
|
|
if(session == NULL)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(RED "failed to allocate session\n" RESET);
|
2016-01-16 00:28:59 +01:00
|
|
|
ftp_closesocket(new_fd, true);
|
2014-11-23 23:39:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize session */
|
|
|
|
strcpy(session->cwd, "/");
|
|
|
|
session->peer_addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
session->cmd_fd = new_fd;
|
|
|
|
session->pasv_fd = -1;
|
|
|
|
session->data_fd = -1;
|
|
|
|
session->state = COMMAND_STATE;
|
|
|
|
|
|
|
|
/* link to the sessions list */
|
|
|
|
if(sessions == NULL)
|
|
|
|
{
|
|
|
|
sessions = session;
|
|
|
|
session->prev = session;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sessions->prev->next = session;
|
|
|
|
session->prev = sessions->prev;
|
|
|
|
sessions->prev = session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy socket address to pasv address */
|
|
|
|
addrlen = sizeof(session->pasv_addr);
|
|
|
|
rc = getsockname(new_fd, (struct sockaddr*)&session->pasv_addr, &addrlen);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "getsockname: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_send_response(session, 451, "Failed to get connection info\r\n");
|
|
|
|
ftp_session_destroy(session);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->cmd_fd = new_fd;
|
|
|
|
|
|
|
|
/* send initiator response */
|
2016-01-15 19:13:56 +01:00
|
|
|
rc = ftp_send_response(session, 220, "Hello!\r\n");
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc <= 0)
|
|
|
|
ftp_session_destroy(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! accept PASV connection for ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*
|
|
|
|
* @returns -1 for failure
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ftp_session_accept(ftp_session_t *session)
|
|
|
|
{
|
2014-11-25 09:30:55 +01:00
|
|
|
int rc, new_fd;
|
2014-11-23 23:39:00 +01:00
|
|
|
struct sockaddr_in addr;
|
|
|
|
socklen_t addrlen = sizeof(addr);
|
|
|
|
|
|
|
|
if(session->flags & SESSION_PASV)
|
|
|
|
{
|
|
|
|
/* clear PASV flag */
|
|
|
|
session->flags &= ~SESSION_PASV;
|
|
|
|
|
|
|
|
/* tell the peer that we're ready to accept the connection */
|
|
|
|
ftp_send_response(session, 150, "Ready\r\n");
|
|
|
|
|
|
|
|
/* accept connection from peer */
|
|
|
|
new_fd = accept(session->pasv_fd, (struct sockaddr*)&addr, &addrlen);
|
|
|
|
if(new_fd < 0)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "accept: %d %s\n" RESET, errno, strerror(errno));
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2014-11-25 09:30:55 +01:00
|
|
|
ftp_send_response(session, 425, "Failed to establish connection\r\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* set the socket to non-blocking */
|
2014-11-25 09:30:55 +01:00
|
|
|
rc = ftp_set_socket_nonblocking(new_fd);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-16 00:28:59 +01:00
|
|
|
ftp_closesocket(new_fd, true);
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_send_response(session, 425, "Failed to establish connection\r\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "accepted connection from %s:%u\n" RESET,
|
2014-11-23 23:39:00 +01:00
|
|
|
inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we are ready to transfer data */
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, DATA_TRANSFER_STATE, CLOSE_PASV);
|
2014-11-23 23:39:00 +01:00
|
|
|
session->data_fd = new_fd;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* peer didn't send PASV command */
|
|
|
|
ftp_send_response(session, 503, "Bad sequence of commands\r\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! connect to peer for ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*
|
|
|
|
* @returns -1 for failure
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ftp_session_connect(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2014-11-25 09:30:55 +01:00
|
|
|
/* clear PORT flag */
|
|
|
|
session->flags &= ~SESSION_PORT;
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2014-11-25 09:30:55 +01:00
|
|
|
/* create a new socket */
|
|
|
|
session->data_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(session->data_fd < 0)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "socket: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-25 09:30:55 +01:00
|
|
|
return -1;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
2014-11-25 09:30:55 +01:00
|
|
|
|
2016-01-12 23:24:01 +01:00
|
|
|
/* set socket options */
|
2016-01-15 23:45:10 +01:00
|
|
|
rc = ftp_set_socket_options(session->data_fd);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-16 00:28:59 +01:00
|
|
|
ftp_closesocket(session->data_fd, false);
|
2016-01-15 23:45:10 +01:00
|
|
|
session->data_fd = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
2016-01-12 23:24:01 +01:00
|
|
|
|
2016-06-20 04:35:39 +02:00
|
|
|
/* set socket to non-blocking */
|
|
|
|
rc = ftp_set_socket_nonblocking(session->data_fd);
|
|
|
|
if(rc != 0)
|
|
|
|
return -1;
|
|
|
|
|
2014-11-25 09:30:55 +01:00
|
|
|
/* connect to peer */
|
|
|
|
rc = connect(session->data_fd, (struct sockaddr*)&session->peer_addr,
|
|
|
|
sizeof(session->peer_addr));
|
|
|
|
if(rc != 0)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
2016-06-20 04:35:39 +02:00
|
|
|
if(errno != EINPROGRESS)
|
|
|
|
{
|
|
|
|
console_print(RED "connect: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
ftp_closesocket(session->data_fd, false);
|
|
|
|
session->data_fd = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
2016-06-20 04:35:39 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
console_print(CYAN "connected to %s:%u\n" RESET,
|
|
|
|
inet_ntoa(session->peer_addr.sin_addr),
|
|
|
|
ntohs(session->peer_addr.sin_port));
|
2014-11-25 09:30:55 +01:00
|
|
|
|
2016-06-20 04:35:39 +02:00
|
|
|
ftp_session_set_state(session, DATA_TRANSFER_STATE, CLOSE_PASV);
|
|
|
|
ftp_send_response(session, 150, "Ready\r\n");
|
|
|
|
}
|
2014-11-25 09:30:55 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*! read command for ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
2016-01-22 02:25:22 +01:00
|
|
|
* @param[in] events poll events
|
2014-11-23 23:39:00 +01:00
|
|
|
*/
|
|
|
|
static void
|
2016-01-22 02:25:22 +01:00
|
|
|
ftp_session_read_command(ftp_session_t *session,
|
|
|
|
int events)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
char *buffer, *args, *next = NULL;
|
|
|
|
size_t i, len;
|
|
|
|
int atmark;
|
2014-11-23 23:39:00 +01:00
|
|
|
ssize_t rc;
|
|
|
|
ftp_command_t key, *command;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* check out-of-band data */
|
|
|
|
if(events & POLLPRI)
|
|
|
|
{
|
|
|
|
session->flags |= SESSION_URGENT;
|
|
|
|
|
|
|
|
/* check if we are at the urgent marker */
|
|
|
|
atmark = sockatmark(session->cmd_fd);
|
|
|
|
if(atmark < 0)
|
|
|
|
{
|
|
|
|
console_print(RED "sockatmark: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
ftp_session_close_cmd(session);
|
|
|
|
return;
|
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
if(!atmark)
|
|
|
|
{
|
|
|
|
/* discard in-band data */
|
|
|
|
rc = recv(session->cmd_fd, session->cmd_buffer, sizeof(session->cmd_buffer), 0);
|
|
|
|
if(rc < 0 && errno != EWOULDBLOCK)
|
|
|
|
{
|
|
|
|
console_print(RED "recv: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
ftp_session_close_cmd(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* retrieve the urgent data */
|
|
|
|
rc = recv(session->cmd_fd, session->cmd_buffer, sizeof(session->cmd_buffer), MSG_OOB);
|
|
|
|
if(rc < 0)
|
|
|
|
{
|
|
|
|
/* EWOULDBLOCK means out-of-band data is on the way */
|
|
|
|
if(errno == EWOULDBLOCK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* error retrieving out-of-band data */
|
|
|
|
console_print(RED "recv (oob): %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
ftp_session_close_cmd(session);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset the command buffer */
|
|
|
|
session->cmd_buffersize = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prepare to receive data */
|
|
|
|
buffer = session->cmd_buffer + session->cmd_buffersize;
|
|
|
|
len = sizeof(session->cmd_buffer) - session->cmd_buffersize;
|
|
|
|
if(len == 0)
|
|
|
|
{
|
|
|
|
/* error retrieving command */
|
|
|
|
console_print(RED "Exceeded command buffer size\n" RESET);
|
|
|
|
ftp_session_close_cmd(session);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* retrieve command data */
|
|
|
|
rc = recv(session->cmd_fd, buffer, len, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc < 0)
|
|
|
|
{
|
|
|
|
/* error retrieving command */
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "recv: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_session_close_cmd(session);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(rc == 0)
|
|
|
|
{
|
|
|
|
/* peer closed connection */
|
|
|
|
ftp_session_close_cmd(session);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
session->cmd_buffersize += rc;
|
|
|
|
len = sizeof(session->cmd_buffer) - session->cmd_buffersize;
|
|
|
|
|
|
|
|
if(session->flags & SESSION_URGENT)
|
2014-11-24 06:24:57 +01:00
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* look for telnet data mark */
|
|
|
|
for(i = 0; i < session->cmd_buffersize; ++i)
|
|
|
|
{
|
|
|
|
if((unsigned char)session->cmd_buffer[i] == 0xF2)
|
|
|
|
{
|
|
|
|
/* ignore all data that precedes the data mark */
|
|
|
|
if(i < session->cmd_buffersize - 1)
|
|
|
|
memmove(session->cmd_buffer, session->cmd_buffer + i + 1, len - i - 1);
|
|
|
|
session->cmd_buffersize -= i + 1;
|
|
|
|
session->flags &= ~SESSION_URGENT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-24 06:24:57 +01:00
|
|
|
}
|
2016-01-22 02:25:22 +01:00
|
|
|
|
|
|
|
/* loop through commands */
|
|
|
|
while(true)
|
2014-11-24 07:16:21 +01:00
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* must have at least enough data for the delimiter */
|
2016-01-22 21:30:26 +01:00
|
|
|
if(session->cmd_buffersize < 1)
|
2016-01-22 02:25:22 +01:00
|
|
|
return;
|
|
|
|
|
2016-01-22 21:30:26 +01:00
|
|
|
/* look for \r\n or \n delimiter */
|
|
|
|
for(i = 0; i < session->cmd_buffersize; ++i)
|
2016-01-22 02:25:22 +01:00
|
|
|
{
|
2016-01-22 21:30:26 +01:00
|
|
|
if(i < session->cmd_buffersize-1
|
|
|
|
&& session->cmd_buffer[i] == '\r'
|
2016-01-22 02:25:22 +01:00
|
|
|
&& session->cmd_buffer[i+1] == '\n')
|
|
|
|
{
|
2016-01-22 21:30:26 +01:00
|
|
|
/* we found a \r\n delimiter */
|
2016-01-22 02:25:22 +01:00
|
|
|
session->cmd_buffer[i] = 0;
|
|
|
|
next = &session->cmd_buffer[i+2];
|
|
|
|
break;
|
|
|
|
}
|
2016-01-22 21:30:26 +01:00
|
|
|
else if(session->cmd_buffer[i] == '\n')
|
|
|
|
{
|
|
|
|
/* we found a \n delimiter */
|
|
|
|
session->cmd_buffer[i] = 0;
|
|
|
|
next = &session->cmd_buffer[i+1];
|
|
|
|
break;
|
|
|
|
}
|
2016-01-22 02:25:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if a delimiter was found */
|
2016-01-22 21:30:26 +01:00
|
|
|
if(i == session->cmd_buffersize)
|
2016-01-22 02:25:22 +01:00
|
|
|
return;
|
|
|
|
|
2016-01-22 21:30:26 +01:00
|
|
|
/* decode the command */
|
2016-01-29 21:05:25 +01:00
|
|
|
decode_path(session, i);
|
2016-01-22 02:25:22 +01:00
|
|
|
|
|
|
|
/* split command from arguments */
|
|
|
|
args = buffer = session->cmd_buffer;
|
|
|
|
while(*args && !isspace((int)*args))
|
|
|
|
++args;
|
|
|
|
if(*args)
|
|
|
|
*args++ = 0;
|
|
|
|
|
|
|
|
/* look up the command */
|
|
|
|
key.name = buffer;
|
|
|
|
command = bsearch(&key, ftp_commands,
|
|
|
|
num_ftp_commands, sizeof(ftp_command_t),
|
|
|
|
ftp_command_cmp);
|
|
|
|
|
2016-01-22 23:38:22 +01:00
|
|
|
/* update command timestamp */
|
|
|
|
session->timestamp = time(NULL);
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* execute the command */
|
|
|
|
if(command == NULL)
|
|
|
|
{
|
2016-01-29 21:08:14 +01:00
|
|
|
/* send header */
|
|
|
|
ftp_send_response(session, 502, "Invalid command \"");
|
|
|
|
|
|
|
|
/* send command */
|
|
|
|
len = strlen(buffer);
|
|
|
|
buffer = encode_path(buffer, &len, false);
|
|
|
|
if(buffer != NULL)
|
|
|
|
ftp_send_response_buffer(session, buffer, len);
|
|
|
|
else
|
|
|
|
ftp_send_response_buffer(session, key.name, strlen(key.name));
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
/* send args (if any) */
|
|
|
|
if(*args != 0)
|
|
|
|
{
|
2016-06-20 04:44:05 +02:00
|
|
|
ftp_send_response_buffer(session, " ", 1);
|
|
|
|
|
2016-01-29 21:08:14 +01:00
|
|
|
len = strlen(args);
|
|
|
|
buffer = encode_path(args, &len, false);
|
|
|
|
if(buffer != NULL)
|
|
|
|
ftp_send_response_buffer(session, buffer, len);
|
|
|
|
else
|
|
|
|
ftp_send_response_buffer(session, args, strlen(args));
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send footer */
|
|
|
|
ftp_send_response_buffer(session, "\"\r\n", 3);
|
2016-01-22 02:25:22 +01:00
|
|
|
}
|
|
|
|
else if(session->state != COMMAND_STATE)
|
|
|
|
{
|
|
|
|
/* only some commands are available during data transfer */
|
|
|
|
if(strcasecmp(command->name, "ABOR") != 0
|
|
|
|
&& strcasecmp(command->name, "STAT") != 0
|
|
|
|
&& strcasecmp(command->name, "QUIT") != 0)
|
|
|
|
{
|
|
|
|
ftp_send_response(session, 503, "Invalid command during transfer\r\n");
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
ftp_session_close_cmd(session);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
command->handler(session, args);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* clear RENAME flag for all commands except RNTO */
|
|
|
|
if(strcasecmp(command->name, "RNTO") != 0)
|
|
|
|
session->flags &= ~SESSION_RENAME;
|
|
|
|
|
|
|
|
command->handler(session, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove executed command from the command buffer */
|
|
|
|
len = session->cmd_buffer + session->cmd_buffersize - next;
|
|
|
|
if(len > 0)
|
|
|
|
memmove(session->cmd_buffer, next, len);
|
|
|
|
session->cmd_buffersize = len;
|
2014-11-24 07:16:21 +01:00
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! poll sockets for ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*
|
|
|
|
* @returns next session
|
|
|
|
*/
|
|
|
|
static ftp_session_t*
|
|
|
|
ftp_session_poll(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
int rc;
|
2016-01-22 02:25:22 +01:00
|
|
|
struct pollfd pollinfo[2];
|
|
|
|
nfds_t nfds = 1;
|
|
|
|
|
|
|
|
/* the first pollfd is the command socket */
|
|
|
|
pollinfo[0].fd = session->cmd_fd;
|
|
|
|
pollinfo[0].events = POLLIN | POLLPRI;
|
|
|
|
pollinfo[0].revents = 0;
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
switch(session->state)
|
|
|
|
{
|
|
|
|
case COMMAND_STATE:
|
|
|
|
/* we are waiting to read a command */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_CONNECT_STATE:
|
2016-06-20 04:35:39 +02:00
|
|
|
if(session->flags & SESSION_PASV)
|
|
|
|
{
|
|
|
|
/* we are waiting for a PASV connection */
|
|
|
|
pollinfo[1].fd = session->pasv_fd;
|
|
|
|
pollinfo[1].events = POLLIN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* we are waiting to complete a PORT connection */
|
|
|
|
pollinfo[1].fd = session->data_fd;
|
|
|
|
pollinfo[1].events = POLLOUT;
|
|
|
|
}
|
2016-01-22 02:25:22 +01:00
|
|
|
pollinfo[1].revents = 0;
|
|
|
|
nfds = 2;
|
2014-11-23 23:39:00 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_TRANSFER_STATE:
|
|
|
|
/* we need to transfer data */
|
2016-01-22 02:25:22 +01:00
|
|
|
pollinfo[1].fd = session->data_fd;
|
2014-11-23 23:39:00 +01:00
|
|
|
if(session->flags & SESSION_RECV)
|
2016-01-22 02:25:22 +01:00
|
|
|
pollinfo[1].events = POLLIN;
|
2014-11-23 23:39:00 +01:00
|
|
|
else
|
2016-01-22 02:25:22 +01:00
|
|
|
pollinfo[1].events = POLLOUT;
|
|
|
|
pollinfo[1].revents = 0;
|
|
|
|
nfds = 2;
|
2014-11-23 23:39:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* poll the selected sockets */
|
|
|
|
rc = poll(pollinfo, nfds, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc < 0)
|
2016-01-15 23:45:10 +01:00
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "poll: %d %s\n" RESET, errno, strerror(errno));
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_close_cmd(session);
|
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
else if(rc > 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* check the command socket */
|
|
|
|
if(pollinfo[0].revents != 0)
|
|
|
|
{
|
|
|
|
/* handle command */
|
|
|
|
if(pollinfo[0].revents & POLL_UNKNOWN)
|
|
|
|
console_print(YELLOW "cmd_fd: revents=0x%08X\n" RESET, pollinfo[0].revents);
|
|
|
|
|
|
|
|
/* we need to read a new command */
|
|
|
|
if(pollinfo[0].revents & (POLLERR|POLLHUP))
|
|
|
|
ftp_session_close_cmd(session);
|
|
|
|
else if(pollinfo[0].revents & (POLLIN | POLLPRI))
|
|
|
|
ftp_session_read_command(session, pollinfo[0].revents);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the data/pasv socket */
|
|
|
|
if(nfds > 1 && pollinfo[1].revents != 0)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
|
|
|
switch(session->state)
|
|
|
|
{
|
|
|
|
case COMMAND_STATE:
|
2016-01-22 02:25:22 +01:00
|
|
|
/* this shouldn't happen? */
|
2014-11-23 23:39:00 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_CONNECT_STATE:
|
2016-01-22 02:25:22 +01:00
|
|
|
if(pollinfo[1].revents & POLL_UNKNOWN)
|
|
|
|
console_print(YELLOW "pasv_fd: revents=0x%08X\n" RESET, pollinfo[1].revents);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
/* we need to accept the PASV connection */
|
2016-01-22 02:25:22 +01:00
|
|
|
if(pollinfo[1].revents & (POLLERR|POLLHUP))
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_send_response(session, 426, "Data connection failed\r\n");
|
|
|
|
}
|
2016-01-22 02:25:22 +01:00
|
|
|
else if(pollinfo[1].revents & POLLIN)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
|
|
|
if(ftp_session_accept(session) != 0)
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
2016-06-20 04:35:39 +02:00
|
|
|
else if(pollinfo[1].revents & POLLOUT)
|
|
|
|
{
|
|
|
|
|
|
|
|
console_print(CYAN "connected to %s:%u\n" RESET,
|
|
|
|
inet_ntoa(session->peer_addr.sin_addr),
|
|
|
|
ntohs(session->peer_addr.sin_port));
|
|
|
|
|
|
|
|
ftp_session_set_state(session, DATA_TRANSFER_STATE, CLOSE_PASV);
|
|
|
|
ftp_send_response(session, 150, "Ready\r\n");
|
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_TRANSFER_STATE:
|
2016-01-22 02:25:22 +01:00
|
|
|
if(pollinfo[1].revents & POLL_UNKNOWN)
|
|
|
|
console_print(YELLOW "data_fd: revents=0x%08X\n" RESET, pollinfo[1].revents);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
/* we need to transfer data */
|
2016-01-22 02:25:22 +01:00
|
|
|
if(pollinfo[1].revents & (POLLERR|POLLHUP))
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_send_response(session, 426, "Data connection failed\r\n");
|
|
|
|
}
|
2016-01-22 02:25:22 +01:00
|
|
|
else if(pollinfo[1].revents & (POLLIN|POLLOUT))
|
2014-11-25 09:30:55 +01:00
|
|
|
ftp_session_transfer(session);
|
2014-11-23 23:39:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* still connected to peer; return next session */
|
|
|
|
if(session->cmd_fd >= 0)
|
|
|
|
return session->next;
|
|
|
|
|
|
|
|
/* disconnected from peer; destroy it and return next session */
|
|
|
|
return ftp_session_destroy(session);
|
|
|
|
}
|
|
|
|
|
2016-06-15 10:04:00 +02:00
|
|
|
/* Update free space in status bar */
|
|
|
|
static void
|
|
|
|
update_free_space(void)
|
|
|
|
{
|
2016-06-20 04:26:41 +02:00
|
|
|
#ifdef _3DS
|
2016-06-14 08:26:44 +02:00
|
|
|
#define KiB (1024.0)
|
|
|
|
#define MiB (1024.0*KiB)
|
|
|
|
#define GiB (1024.0*MiB)
|
|
|
|
char buffer[16];
|
|
|
|
struct statvfs st;
|
|
|
|
double bytes_free;
|
2016-06-15 10:04:00 +02:00
|
|
|
int rc, len;
|
2016-06-14 08:26:44 +02:00
|
|
|
|
|
|
|
rc = statvfs("sdmc:/", &st);
|
|
|
|
if(rc != 0)
|
|
|
|
console_print(RED "statvfs: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
else
|
2016-06-15 10:04:00 +02:00
|
|
|
{
|
|
|
|
bytes_free = (double)st.f_bsize * st.f_bfree;
|
|
|
|
|
|
|
|
if (bytes_free < 1000.0)
|
|
|
|
len = snprintf(buffer, sizeof(buffer), "%.0lfB", bytes_free);
|
|
|
|
else if(bytes_free < 10.0*KiB)
|
|
|
|
len = snprintf(buffer, sizeof(buffer), "%.2lfKiB", floor((bytes_free*100.0)/KiB)/100.0);
|
|
|
|
else if(bytes_free < 100.0*KiB)
|
|
|
|
len = snprintf(buffer, sizeof(buffer), "%.1lfKiB", floor((bytes_free*10.0)/KiB)/10.0);
|
|
|
|
else if(bytes_free < 1000.0*KiB)
|
|
|
|
len = snprintf(buffer, sizeof(buffer), "%.0lfKiB", floor(bytes_free/KiB));
|
|
|
|
else if(bytes_free < 10.0*MiB)
|
|
|
|
len = snprintf(buffer, sizeof(buffer), "%.2lfMiB", floor((bytes_free*100.0)/MiB)/100.0);
|
|
|
|
else if(bytes_free < 100.0*MiB)
|
|
|
|
len = snprintf(buffer, sizeof(buffer), "%.1lfMiB", floor((bytes_free*10.0)/MiB)/10.0);
|
|
|
|
else if(bytes_free < 1000.0*MiB)
|
|
|
|
len = snprintf(buffer, sizeof(buffer), "%.0lfMiB", floor(bytes_free/MiB));
|
|
|
|
else if(bytes_free < 10.0*GiB)
|
|
|
|
len = snprintf(buffer, sizeof(buffer), "%.2lfGiB", floor((bytes_free*100.0)/GiB)/100.0);
|
|
|
|
else if(bytes_free < 100.0*GiB)
|
|
|
|
len = snprintf(buffer, sizeof(buffer), "%.1lfGiB", floor((bytes_free*10.0)/GiB)/10.0);
|
|
|
|
else
|
|
|
|
len = snprintf(buffer, sizeof(buffer), "%.0lfGiB", floor(bytes_free/GiB));
|
|
|
|
|
|
|
|
console_set_status("\x1b[0;%dH" GREEN "%s", 50-len, buffer);
|
|
|
|
}
|
|
|
|
#endif
|
2016-06-20 04:26:41 +02:00
|
|
|
}
|
2016-06-14 08:26:44 +02:00
|
|
|
|
2016-06-15 10:04:00 +02:00
|
|
|
/*! Update status bar */
|
|
|
|
static int
|
|
|
|
update_status(void)
|
|
|
|
{
|
|
|
|
#ifdef _3DS
|
2016-06-14 08:26:44 +02:00
|
|
|
console_set_status("\n" GREEN STATUS_STRING " "
|
2016-06-15 10:04:00 +02:00
|
|
|
CYAN "%s:%u" RESET,
|
2016-06-14 08:26:44 +02:00
|
|
|
inet_ntoa(serv_addr.sin_addr),
|
2016-06-15 10:04:00 +02:00
|
|
|
ntohs(serv_addr.sin_port));
|
|
|
|
update_free_space();
|
2016-06-14 08:26:44 +02:00
|
|
|
#else
|
|
|
|
char hostname[128];
|
|
|
|
socklen_t addrlen = sizeof(serv_addr);
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = getsockname(listenfd, (struct sockaddr*)&serv_addr, &addrlen);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
|
|
|
console_print(RED "getsockname: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = gethostname(hostname, sizeof(hostname));
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
|
|
|
console_print(RED "gethostname: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
console_set_status(GREEN STATUS_STRING " "
|
|
|
|
YELLOW "IP:" CYAN "%s "
|
|
|
|
YELLOW "Port:" CYAN "%u"
|
|
|
|
RESET,
|
|
|
|
hostname,
|
|
|
|
ntohs(serv_addr.sin_port));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-11 05:53:05 +02:00
|
|
|
#ifdef _3DS
|
|
|
|
/*! Handle apt events
|
|
|
|
*
|
|
|
|
* @param[in] type Event type
|
|
|
|
* @param[in] closure Callback closure
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
apt_hook(APT_HookType type,
|
|
|
|
void *closure)
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case APTHOOK_ONSUSPEND:
|
|
|
|
case APTHOOK_ONSLEEP:
|
|
|
|
/* turn on backlight, or you can't see the home menu! */
|
|
|
|
if(R_SUCCEEDED(gspLcdInit()))
|
|
|
|
{
|
|
|
|
GSPLCD_PowerOnBacklight(GSPLCD_SCREEN_BOTH);
|
|
|
|
gspLcdExit();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case APTHOOK_ONRESTORE:
|
|
|
|
case APTHOOK_ONWAKEUP:
|
|
|
|
/* restore backlight power state */
|
|
|
|
if(R_SUCCEEDED(gspLcdInit()))
|
|
|
|
{
|
|
|
|
(lcd_power ? GSPLCD_PowerOnBacklight : GSPLCD_PowerOffBacklight)(GSPLCD_SCREEN_BOTH);
|
|
|
|
gspLcdExit();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
/*! initialize ftp subsystem */
|
|
|
|
int
|
|
|
|
ftp_init(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
start_time = time(NULL);
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
#ifdef _3DS
|
2016-01-16 07:22:26 +01:00
|
|
|
Result ret = 0;
|
|
|
|
u32 wifi = 0;
|
|
|
|
bool loop;
|
|
|
|
|
2016-08-11 05:53:05 +02:00
|
|
|
/* register apt hook */
|
|
|
|
aptHook(&cookie, apt_hook, NULL);
|
|
|
|
|
2016-01-16 07:22:26 +01:00
|
|
|
console_print(GREEN "Waiting for wifi...\n" RESET);
|
|
|
|
|
|
|
|
/* wait for wifi to be available */
|
|
|
|
while((loop = aptMainLoop()) && !wifi && (ret == 0 || ret == 0xE0A09D2E))
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
ret = 0;
|
|
|
|
|
2016-01-16 07:22:26 +01:00
|
|
|
hidScanInput();
|
|
|
|
if(hidKeysDown() & KEY_B)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* user canceled */
|
2016-01-16 07:22:26 +01:00
|
|
|
loop = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* update the wifi status */
|
2016-01-16 07:22:26 +01:00
|
|
|
ret = ACU_GetWifiStatus(&wifi);
|
|
|
|
if(ret != 0)
|
|
|
|
wifi = 0;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* check if there was a wifi error */
|
2016-01-16 07:22:26 +01:00
|
|
|
if(ret != 0)
|
|
|
|
console_print(RED "ACU_GetWifiStatus returns 0x%lx\n" RESET, ret);
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* check if we need to exit */
|
2016-01-16 07:22:26 +01:00
|
|
|
if(!loop || ret != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
console_print(GREEN "Ready!\n" RESET);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-16 23:00:31 +01:00
|
|
|
#ifdef ENABLE_LOGGING
|
2015-01-08 06:30:22 +01:00
|
|
|
/* open log file */
|
2016-01-26 06:46:52 +01:00
|
|
|
FILE *fp = freopen("/ftpd.log", "wb", stderr);
|
2015-01-08 06:30:22 +01:00
|
|
|
if(fp == NULL)
|
|
|
|
{
|
|
|
|
console_print(RED "freopen: 0x%08X\n" RESET, errno);
|
2016-01-16 23:00:31 +01:00
|
|
|
goto log_fail;
|
2015-01-08 06:30:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* truncate log file */
|
|
|
|
if(ftruncate(fileno(fp), 0) != 0)
|
|
|
|
{
|
|
|
|
console_print(RED "ftruncate: 0x%08X\n" RESET, errno);
|
|
|
|
goto ftruncate_fail;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
/* allocate buffer for SOC service */
|
2016-01-12 23:24:01 +01:00
|
|
|
SOCU_buffer = (u32*)memalign(SOCU_ALIGN, SOCU_BUFFERSIZE);
|
|
|
|
if(SOCU_buffer == NULL)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(RED "memalign: failed to allocate\n" RESET);
|
|
|
|
goto memalign_fail;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize SOC service */
|
2016-01-12 23:24:01 +01:00
|
|
|
ret = socInit(SOCU_buffer, SOCU_BUFFERSIZE);
|
2014-11-23 23:39:00 +01:00
|
|
|
if(ret != 0)
|
|
|
|
{
|
2016-01-12 23:24:01 +01:00
|
|
|
console_print(RED "socInit: 0x%08X\n" RESET, (unsigned int)ret);
|
2015-01-08 06:26:01 +01:00
|
|
|
goto soc_fail;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* allocate socket to listen for clients */
|
|
|
|
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(listenfd < 0)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "socket: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_exit();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get address to listen on */
|
|
|
|
serv_addr.sin_family = AF_INET;
|
|
|
|
#ifdef _3DS
|
|
|
|
serv_addr.sin_addr.s_addr = gethostid();
|
|
|
|
serv_addr.sin_port = htons(LISTEN_PORT);
|
|
|
|
#else
|
|
|
|
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
serv_addr.sin_port = htons(LISTEN_PORT);
|
2016-01-12 23:24:01 +01:00
|
|
|
#endif
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
/* reuse address */
|
|
|
|
{
|
|
|
|
int yes = 1;
|
|
|
|
rc = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "setsockopt: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_exit();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bind socket to listen address */
|
|
|
|
rc = bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "bind: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_exit();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* listen on socket */
|
|
|
|
rc = listen(listenfd, 5);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "listen: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_exit();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print server address */
|
2016-06-14 08:26:44 +02:00
|
|
|
rc = update_status();
|
|
|
|
if(rc != 0)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
2016-06-14 08:26:44 +02:00
|
|
|
ftp_exit();
|
|
|
|
return -1;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-01-08 06:26:01 +01:00
|
|
|
|
|
|
|
#ifdef _3DS
|
|
|
|
soc_fail:
|
2016-01-12 23:24:01 +01:00
|
|
|
free(SOCU_buffer);
|
2016-04-09 18:54:34 +02:00
|
|
|
SOCU_buffer = NULL;
|
2015-01-08 06:26:01 +01:00
|
|
|
|
|
|
|
memalign_fail:
|
2015-01-08 06:30:22 +01:00
|
|
|
#ifdef ENABLE_LOGGING
|
|
|
|
ftruncate_fail:
|
|
|
|
if(fclose(stderr) != 0)
|
|
|
|
console_print(RED "fclose: 0x%08X\n" RESET, errno);
|
|
|
|
|
2016-01-16 23:00:31 +01:00
|
|
|
log_fail:
|
2015-08-19 20:25:34 +02:00
|
|
|
#endif
|
2015-01-08 06:26:01 +01:00
|
|
|
return -1;
|
|
|
|
#endif
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*! deinitialize ftp subsystem */
|
|
|
|
void
|
|
|
|
ftp_exit(void)
|
|
|
|
{
|
|
|
|
#ifdef _3DS
|
|
|
|
Result ret;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* clean up all sessions */
|
|
|
|
while(sessions != NULL)
|
|
|
|
ftp_session_destroy(sessions);
|
|
|
|
|
|
|
|
/* stop listening for new clients */
|
|
|
|
if(listenfd >= 0)
|
2016-01-16 00:28:59 +01:00
|
|
|
ftp_closesocket(listenfd, false);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
#ifdef _3DS
|
|
|
|
/* deinitialize SOC service */
|
2016-01-26 03:03:48 +01:00
|
|
|
console_render();
|
2016-01-16 23:00:31 +01:00
|
|
|
console_print(CYAN "Waiting for socExit()...\n" RESET);
|
2016-04-09 18:54:34 +02:00
|
|
|
|
|
|
|
if(SOCU_buffer != NULL)
|
|
|
|
{
|
|
|
|
ret = socExit();
|
|
|
|
if(ret != 0)
|
|
|
|
console_print(RED "socExit: 0x%08X\n" RESET, (unsigned int)ret);
|
|
|
|
free(SOCU_buffer);
|
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2015-01-08 06:30:22 +01:00
|
|
|
#ifdef ENABLE_LOGGING
|
|
|
|
/* close log file */
|
|
|
|
if(fclose(stderr) != 0)
|
|
|
|
console_print(RED "fclose: 0x%08X\n" RESET, errno);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! ftp look
|
|
|
|
*
|
|
|
|
* @returns whether to keep looping
|
|
|
|
*/
|
2016-01-16 07:38:58 +01:00
|
|
|
loop_status_t
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_loop(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct pollfd pollinfo;
|
|
|
|
ftp_session_t *session;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we will poll for new client connections */
|
2014-11-23 23:39:00 +01:00
|
|
|
pollinfo.fd = listenfd;
|
|
|
|
pollinfo.events = POLLIN;
|
|
|
|
pollinfo.revents = 0;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* poll for a new client */
|
2014-11-23 23:39:00 +01:00
|
|
|
rc = poll(&pollinfo, 1, 0);
|
|
|
|
if(rc < 0)
|
2015-08-19 20:25:34 +02:00
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* wifi got disabled */
|
2016-01-16 07:38:58 +01:00
|
|
|
if(errno == ENETDOWN)
|
|
|
|
return LOOP_RESTART;
|
|
|
|
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "poll: %d %s\n" RESET, errno, strerror(errno));
|
2016-01-16 07:38:58 +01:00
|
|
|
return LOOP_EXIT;
|
2015-08-19 20:25:34 +02:00
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
else if(rc > 0)
|
|
|
|
{
|
|
|
|
if(pollinfo.revents & POLLIN)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we got a new client */
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_session_new(listenfd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(YELLOW "listenfd: revents=0x%08X\n" RESET, pollinfo.revents);
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* poll each session */
|
2014-11-23 23:39:00 +01:00
|
|
|
session = sessions;
|
|
|
|
while(session != NULL)
|
|
|
|
session = ftp_session_poll(session);
|
|
|
|
|
|
|
|
#ifdef _3DS
|
2016-01-22 02:25:22 +01:00
|
|
|
/* check if the user wants to exit */
|
2014-11-23 23:39:00 +01:00
|
|
|
hidScanInput();
|
2016-08-11 05:53:05 +02:00
|
|
|
u32 down = hidKeysDown();
|
|
|
|
|
|
|
|
if(down & KEY_B)
|
2016-01-16 07:38:58 +01:00
|
|
|
return LOOP_EXIT;
|
2016-08-11 05:53:05 +02:00
|
|
|
|
|
|
|
/* check if the user wants to toggle the LCD power */
|
|
|
|
if(down & KEY_START)
|
|
|
|
{
|
|
|
|
lcd_power = !lcd_power;
|
|
|
|
apt_hook(APTHOOK_ONRESTORE, NULL);
|
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
#endif
|
|
|
|
|
2016-01-16 07:38:58 +01:00
|
|
|
return LOOP_CONTINUE;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! change to parent directory
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
static void
|
|
|
|
cd_up(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
char *slash = NULL, *p;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* remove basename from cwd */
|
2014-11-23 23:39:00 +01:00
|
|
|
for(p = session->cwd; *p; ++p)
|
|
|
|
{
|
|
|
|
if(*p == '/')
|
|
|
|
slash = p;
|
|
|
|
}
|
|
|
|
*slash = 0;
|
|
|
|
if(strlen(session->cwd) == 0)
|
|
|
|
strcat(session->cwd, "/");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! validate a path
|
|
|
|
*
|
|
|
|
* @param[in] args path to validate
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
static int
|
|
|
|
validate_path(const char *args)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
/* make sure no path components are '..' */
|
|
|
|
p = args;
|
|
|
|
while((p = strstr(p, "/..")) != NULL)
|
|
|
|
{
|
|
|
|
if(p[3] == 0 || p[3] == '/')
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure there are no '//' */
|
|
|
|
if(strstr(args, "//") != NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! get a path relative to cwd
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] cwd working directory
|
|
|
|
* @param[in] args path to make
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*
|
|
|
|
* @note the output goes to session->buffer
|
|
|
|
*/
|
2014-11-25 08:42:25 +01:00
|
|
|
static int
|
2014-11-23 23:39:00 +01:00
|
|
|
build_path(ftp_session_t *session,
|
2016-01-22 02:25:22 +01:00
|
|
|
const char *cwd,
|
2014-11-23 23:39:00 +01:00
|
|
|
const char *args)
|
|
|
|
{
|
2014-11-25 08:42:25 +01:00
|
|
|
int rc;
|
|
|
|
char *p;
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
memset(session->buffer, 0, sizeof(session->buffer));
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* make sure the input is a valid path */
|
2014-11-25 08:42:25 +01:00
|
|
|
if(validate_path(args) != 0)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
if(args[0] == '/')
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* this is an absolute path */
|
2014-11-25 08:42:25 +01:00
|
|
|
if(strlen(args) > sizeof(session->buffer)-1)
|
|
|
|
{
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
strncpy(session->buffer, args, sizeof(session->buffer));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* this is a relative path */
|
|
|
|
if(strcmp(cwd, "/") == 0)
|
2014-11-25 08:42:25 +01:00
|
|
|
rc = snprintf(session->buffer, sizeof(session->buffer), "/%s",
|
|
|
|
args);
|
2014-11-25 08:24:24 +01:00
|
|
|
else
|
2014-11-25 08:42:25 +01:00
|
|
|
rc = snprintf(session->buffer, sizeof(session->buffer), "%s/%s",
|
2016-01-22 02:25:22 +01:00
|
|
|
cwd, args);
|
2014-11-25 08:42:25 +01:00
|
|
|
|
|
|
|
if(rc >= sizeof(session->buffer))
|
|
|
|
{
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
2014-11-25 08:42:25 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* remove trailing / */
|
2014-11-25 08:42:25 +01:00
|
|
|
p = session->buffer + strlen(session->buffer);
|
|
|
|
while(p > session->buffer && *--p == '/')
|
|
|
|
*p = 0;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* if we ended with an empty path, it is the root directory */
|
2014-11-25 08:42:25 +01:00
|
|
|
if(strlen(session->buffer) == 0)
|
|
|
|
strcpy(session->buffer, "/");
|
|
|
|
|
|
|
|
return 0;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! transfer a directory listing
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*
|
|
|
|
* @returns whether to call again
|
|
|
|
*/
|
|
|
|
static loop_status_t
|
2014-11-23 23:39:00 +01:00
|
|
|
list_transfer(ftp_session_t *session)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
ssize_t rc;
|
|
|
|
size_t len;
|
2016-01-22 23:38:22 +01:00
|
|
|
uint64_t mtime;
|
|
|
|
time_t t_mtime;
|
|
|
|
struct tm *tm;
|
2016-01-22 02:25:22 +01:00
|
|
|
char *buffer;
|
|
|
|
struct stat st;
|
|
|
|
struct dirent *dent;
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* check if we sent all available data */
|
2014-11-23 23:39:00 +01:00
|
|
|
if(session->bufferpos == session->buffersize)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* check if this was a STAT */
|
|
|
|
if(session->data_fd == session->cmd_fd)
|
|
|
|
rc = 213;
|
|
|
|
else
|
|
|
|
rc = 226;
|
|
|
|
|
|
|
|
/* check if this was for a file */
|
|
|
|
if(session->dp == NULL)
|
|
|
|
{
|
|
|
|
/* we already sent the file's listing */
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
ftp_send_response(session, rc, "OK\r\n");
|
|
|
|
return LOOP_EXIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the next directory entry */
|
|
|
|
dent = readdir(session->dp);
|
2014-11-23 23:39:00 +01:00
|
|
|
if(dent == NULL)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we have exhausted the directory listing */
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2016-01-22 02:25:22 +01:00
|
|
|
ftp_send_response(session, rc, "OK\r\n");
|
|
|
|
return LOOP_EXIT;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* TODO I think we are supposed to return entries for . and .. */
|
2014-11-23 23:39:00 +01:00
|
|
|
if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_CONTINUE;
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* check if this was a NLST */
|
2016-01-16 00:47:18 +01:00
|
|
|
if(session->flags & SESSION_NLST)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* NLST gives the whole path name */
|
|
|
|
session->buffersize = 0;
|
|
|
|
if(build_path(session, session->lwd, dent->d_name) == 0)
|
|
|
|
{
|
2016-01-22 21:30:26 +01:00
|
|
|
/* encode \n in path */
|
2016-01-22 02:25:22 +01:00
|
|
|
len = strlen(session->buffer);
|
2016-01-22 21:30:26 +01:00
|
|
|
buffer = encode_path(session->buffer, &len, false);
|
2016-01-22 02:25:22 +01:00
|
|
|
if(buffer != NULL)
|
|
|
|
{
|
|
|
|
/* copy to the session buffer to send */
|
|
|
|
memcpy(session->buffer, buffer, len);
|
|
|
|
free(buffer);
|
|
|
|
session->buffer[len++] = '\r';
|
|
|
|
session->buffer[len++] = '\n';
|
|
|
|
session->buffersize = len;
|
|
|
|
}
|
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
2016-01-16 00:47:18 +01:00
|
|
|
else
|
|
|
|
{
|
2016-01-16 23:32:59 +01:00
|
|
|
#ifdef _3DS
|
2016-01-22 02:25:22 +01:00
|
|
|
/* the sdmc directory entry already has the type and size, so no need to do a slow stat */
|
2016-04-09 18:55:55 +02:00
|
|
|
u32 magic = *(u32*)session->dp->dirData->dirStruct;
|
2016-01-16 23:32:59 +01:00
|
|
|
|
2016-04-09 18:55:55 +02:00
|
|
|
if(magic == SDMC_DIRITER_MAGIC)
|
|
|
|
{
|
|
|
|
sdmc_dir_t *dir = (sdmc_dir_t*)session->dp->dirData->dirStruct;
|
|
|
|
FS_DirectoryEntry *entry = &dir->entry_data[dir->index];
|
2016-01-16 23:32:59 +01:00
|
|
|
|
2016-04-09 18:55:55 +02:00
|
|
|
if(entry->attributes & FS_ATTRIBUTE_DIRECTORY)
|
|
|
|
st.st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
|
|
|
|
else
|
|
|
|
st.st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
|
2016-01-22 23:38:22 +01:00
|
|
|
|
2016-04-09 18:55:55 +02:00
|
|
|
if(!(entry->attributes & FS_ATTRIBUTE_READ_ONLY))
|
|
|
|
st.st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
|
|
|
|
|
|
|
|
st.st_size = entry->fileSize;
|
|
|
|
|
|
|
|
if((rc = build_path(session, session->lwd, dent->d_name)) != 0)
|
|
|
|
console_print(RED "build_path: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
else if((rc = sdmc_getmtime(session->buffer, &mtime)) != 0)
|
|
|
|
{
|
|
|
|
console_print(RED "sdmc_getmtime '%s': 0x%x\n" RESET, session->buffer, rc);
|
|
|
|
mtime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-01-22 23:38:22 +01:00
|
|
|
{
|
2016-04-09 18:55:55 +02:00
|
|
|
/* lstat the entry */
|
|
|
|
if((rc = build_path(session, session->lwd, dent->d_name)) != 0)
|
|
|
|
console_print(RED "build_path: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
else if((rc = lstat(session->buffer, &st)) != 0)
|
|
|
|
console_print(RED "stat '%s': %d %s\n" RESET, session->buffer, errno, strerror(errno));
|
|
|
|
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
|
|
|
/* an error occurred */
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
ftp_send_response(session, 550, "unavailable\r\n");
|
|
|
|
return LOOP_EXIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
mtime = st.st_mtime;
|
2016-01-22 23:38:22 +01:00
|
|
|
}
|
2016-01-16 23:32:59 +01:00
|
|
|
#else
|
2016-01-22 02:25:22 +01:00
|
|
|
/* lstat the entry */
|
|
|
|
if((rc = build_path(session, session->lwd, dent->d_name)) != 0)
|
|
|
|
console_print(RED "build_path: %d %s\n" RESET, errno, strerror(errno));
|
|
|
|
else if((rc = lstat(session->buffer, &st)) != 0)
|
|
|
|
console_print(RED "stat '%s': %d %s\n" RESET, session->buffer, errno, strerror(errno));
|
|
|
|
|
2016-01-16 00:47:18 +01:00
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* an error occurred */
|
2016-01-16 00:47:18 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
ftp_send_response(session, 550, "unavailable\r\n");
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_EXIT;
|
2016-01-16 00:47:18 +01:00
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 23:38:22 +01:00
|
|
|
mtime = st.st_mtime;
|
|
|
|
#endif
|
2016-01-22 21:30:26 +01:00
|
|
|
/* encode \n in path */
|
2016-01-22 02:25:22 +01:00
|
|
|
len = strlen(dent->d_name);
|
2016-01-22 21:30:26 +01:00
|
|
|
buffer = encode_path(dent->d_name, &len, false);
|
2016-01-22 02:25:22 +01:00
|
|
|
if(buffer != NULL)
|
|
|
|
{
|
|
|
|
/* copy to the session buffer to send */
|
|
|
|
session->buffersize =
|
|
|
|
sprintf(session->buffer,
|
2016-04-09 18:55:55 +02:00
|
|
|
"%c%c%c%c%c%c%c%c%c%c 1 3DS 3DS %lld ",
|
2016-01-26 02:10:53 +01:00
|
|
|
S_ISREG(st.st_mode) ? '-' :
|
|
|
|
S_ISDIR(st.st_mode) ? 'd' :
|
|
|
|
S_ISLNK(st.st_mode) ? 'l' :
|
|
|
|
S_ISCHR(st.st_mode) ? 'c' :
|
|
|
|
S_ISBLK(st.st_mode) ? 'b' :
|
|
|
|
S_ISFIFO(st.st_mode) ? 'p' :
|
|
|
|
S_ISSOCK(st.st_mode) ? 's' : '?',
|
2016-04-09 18:55:55 +02:00
|
|
|
st.st_mode & S_IRUSR ? 'r' : '-',
|
|
|
|
st.st_mode & S_IWUSR ? 'w' : '-',
|
|
|
|
st.st_mode & S_IXUSR ? 'x' : '-',
|
|
|
|
st.st_mode & S_IRGRP ? 'r' : '-',
|
|
|
|
st.st_mode & S_IWGRP ? 'w' : '-',
|
|
|
|
st.st_mode & S_IXGRP ? 'x' : '-',
|
|
|
|
st.st_mode & S_IROTH ? 'r' : '-',
|
|
|
|
st.st_mode & S_IWOTH ? 'w' : '-',
|
|
|
|
st.st_mode & S_IXOTH ? 'x' : '-',
|
2016-02-03 20:56:47 +01:00
|
|
|
(signed long long)st.st_size);
|
2016-01-22 23:38:22 +01:00
|
|
|
|
|
|
|
t_mtime = mtime;
|
|
|
|
tm = gmtime(&t_mtime);
|
|
|
|
if(tm != NULL)
|
|
|
|
{
|
|
|
|
const char *fmt = "%b %e %Y ";
|
|
|
|
if(session->timestamp > mtime && session->timestamp - mtime < (60*60*24*365/2))
|
|
|
|
fmt = "%b %e %H:%M ";
|
|
|
|
session->buffersize +=
|
|
|
|
strftime(session->buffer + session->buffersize,
|
|
|
|
sizeof(session->buffer) - session->buffersize,
|
|
|
|
fmt, tm);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->buffersize +=
|
|
|
|
sprintf(session->buffer + session->buffersize, "Jan 1 1970 ");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
if(session->buffersize + len + 2 > sizeof(session->buffer))
|
|
|
|
{
|
|
|
|
/* buffer will overflow */
|
|
|
|
free(buffer);
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
ftp_send_response(session, 425, "%s\r\n", strerror(EOVERFLOW));
|
|
|
|
return LOOP_EXIT;
|
|
|
|
}
|
|
|
|
memcpy(session->buffer+session->buffersize, buffer, len);
|
|
|
|
free(buffer);
|
|
|
|
len = session->buffersize + len;
|
|
|
|
session->buffer[len++] = '\r';
|
|
|
|
session->buffer[len++] = '\n';
|
|
|
|
session->buffersize = len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
session->buffersize = 0;
|
2016-01-16 00:47:18 +01:00
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
session->bufferpos = 0;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* send any pending data */
|
2014-11-23 23:39:00 +01:00
|
|
|
rc = send(session->data_fd, session->buffer + session->bufferpos,
|
|
|
|
session->buffersize - session->bufferpos, 0);
|
|
|
|
if(rc <= 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* error sending data */
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc < 0)
|
2014-11-25 09:30:55 +01:00
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
if(errno == EWOULDBLOCK)
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_EXIT;
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "send: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-25 09:30:55 +01:00
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
else
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(YELLOW "send: %d %s\n" RESET, ECONNRESET, strerror(ECONNRESET));
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_send_response(session, 426, "Connection broken during transfer\r\n");
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_EXIT;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we can try to send more data */
|
2014-11-23 23:39:00 +01:00
|
|
|
session->bufferpos += rc;
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_CONTINUE;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! send a file to the client
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*
|
|
|
|
* @returns whether to call again
|
|
|
|
*/
|
|
|
|
static loop_status_t
|
2014-11-23 23:39:00 +01:00
|
|
|
retrieve_transfer(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
ssize_t rc;
|
|
|
|
|
|
|
|
if(session->bufferpos == session->buffersize)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we have sent all the data so read some more */
|
2014-11-23 23:39:00 +01:00
|
|
|
rc = ftp_session_read_file(session);
|
|
|
|
if(rc <= 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* can't read any more data */
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc < 0)
|
|
|
|
ftp_send_response(session, 451, "Failed to read file\r\n");
|
|
|
|
else
|
|
|
|
ftp_send_response(session, 226, "OK\r\n");
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_EXIT;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we read some data so reset the session buffer to send */
|
2014-11-23 23:39:00 +01:00
|
|
|
session->bufferpos = 0;
|
|
|
|
session->buffersize = rc;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* send any pending data */
|
2014-11-23 23:39:00 +01:00
|
|
|
rc = send(session->data_fd, session->buffer + session->bufferpos,
|
|
|
|
session->buffersize - session->bufferpos, 0);
|
|
|
|
if(rc <= 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* error sending data */
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc < 0)
|
2014-11-25 09:30:55 +01:00
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
if(errno == EWOULDBLOCK)
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_EXIT;
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "send: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-25 09:30:55 +01:00
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
else
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(YELLOW "send: %d %s\n" RESET, ECONNRESET, strerror(ECONNRESET));
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_send_response(session, 426, "Connection broken during transfer\r\n");
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_EXIT;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we can try to send more data */
|
2014-11-23 23:39:00 +01:00
|
|
|
session->bufferpos += rc;
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_CONTINUE;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! send a file to the client
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
*
|
|
|
|
* @returns whether to call again
|
|
|
|
*/
|
|
|
|
static loop_status_t
|
2014-11-23 23:39:00 +01:00
|
|
|
store_transfer(ftp_session_t *session)
|
|
|
|
{
|
|
|
|
ssize_t rc;
|
|
|
|
|
|
|
|
if(session->bufferpos == session->buffersize)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we have written all the received data, so try to get some more */
|
2014-11-23 23:39:00 +01:00
|
|
|
rc = recv(session->data_fd, session->buffer, sizeof(session->buffer), 0);
|
|
|
|
if(rc <= 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* can't read any more data */
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc < 0)
|
2014-11-25 09:30:55 +01:00
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
if(errno == EWOULDBLOCK)
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_EXIT;
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "recv: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-25 09:30:55 +01:00
|
|
|
}
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
if(rc == 0)
|
|
|
|
ftp_send_response(session, 226, "OK\r\n");
|
|
|
|
else
|
|
|
|
ftp_send_response(session, 426, "Connection broken during transfer\r\n");
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_EXIT;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we received some data so reset the session buffer to write */
|
2014-11-23 23:39:00 +01:00
|
|
|
session->bufferpos = 0;
|
|
|
|
session->buffersize = rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ftp_session_write_file(session);
|
|
|
|
if(rc <= 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* error writing data */
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_send_response(session, 451, "Failed to write file\r\n");
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_EXIT;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we can try to receive more data */
|
2014-11-23 23:39:00 +01:00
|
|
|
session->bufferpos += rc;
|
2016-01-22 02:25:22 +01:00
|
|
|
return LOOP_CONTINUE;
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 00:28:59 +01:00
|
|
|
/*! ftp_xfer_file mode */
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
XFER_FILE_RETR, /*!< Retrieve a file */
|
|
|
|
XFER_FILE_STOR, /*!< Store a file */
|
|
|
|
XFER_FILE_APPE, /*!< Append a file */
|
|
|
|
} xfer_file_mode_t;
|
|
|
|
|
|
|
|
/*! Transfer a file
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args ftp arguments
|
|
|
|
* @param[in] mode transfer mode
|
|
|
|
*
|
|
|
|
* @returns failure
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ftp_xfer_file(ftp_session_t *session,
|
|
|
|
const char *args,
|
|
|
|
xfer_file_mode_t mode)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* build the path of the file to transfer */
|
|
|
|
if(build_path(session, session->cwd, args) != 0)
|
2016-01-16 00:28:59 +01:00
|
|
|
{
|
|
|
|
rc = errno;
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
return ftp_send_response(session, 553, "%s\r\n", strerror(rc));
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* open the file for retrieving or storing */
|
2016-01-16 00:28:59 +01:00
|
|
|
if(mode == XFER_FILE_RETR)
|
|
|
|
rc = ftp_session_open_file_read(session);
|
|
|
|
else
|
|
|
|
rc = ftp_session_open_file_write(session, mode == XFER_FILE_APPE);
|
|
|
|
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* error opening the file */
|
2016-01-16 00:28:59 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
return ftp_send_response(session, 450, "failed to open file\r\n");
|
|
|
|
}
|
|
|
|
|
2016-06-20 04:35:39 +02:00
|
|
|
if(session->flags & (SESSION_PORT|SESSION_PASV))
|
2016-01-16 00:28:59 +01:00
|
|
|
{
|
2016-06-20 04:35:39 +02:00
|
|
|
ftp_session_set_state(session, DATA_CONNECT_STATE, CLOSE_DATA);
|
2016-01-16 00:28:59 +01:00
|
|
|
|
2016-06-20 04:35:39 +02:00
|
|
|
if(session->flags & SESSION_PORT)
|
2016-01-16 00:28:59 +01:00
|
|
|
{
|
2016-06-20 04:35:39 +02:00
|
|
|
/* setup connection */
|
|
|
|
rc = ftp_session_connect(session);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
|
|
|
/* error connecting */
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
return ftp_send_response(session, 425, "can't open data connection\r\n");
|
|
|
|
}
|
2016-01-16 00:28:59 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* set up the transfer */
|
2016-01-16 00:28:59 +01:00
|
|
|
session->flags &= ~(SESSION_RECV|SESSION_SEND);
|
|
|
|
if(mode == XFER_FILE_RETR)
|
|
|
|
{
|
|
|
|
session->flags |= SESSION_SEND;
|
|
|
|
session->transfer = retrieve_transfer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->flags |= SESSION_RECV;
|
|
|
|
session->transfer = store_transfer;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->bufferpos = 0;
|
|
|
|
session->buffersize = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
return ftp_send_response(session, 503, "Bad sequence of commands\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-16 00:47:18 +01:00
|
|
|
/*! ftp_xfer_dir mode */
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
XFER_DIR_LIST, /*!< Long list */
|
|
|
|
XFER_DIR_NLST, /*!< Short list */
|
2016-01-22 02:25:22 +01:00
|
|
|
XFER_DIR_STAT, /*!< Stat command */
|
2016-01-16 00:47:18 +01:00
|
|
|
} xfer_dir_mode_t;
|
|
|
|
|
|
|
|
/*! Transfer a directory
|
|
|
|
*
|
2016-01-26 02:11:53 +01:00
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args ftp arguments
|
|
|
|
* @param[in] mode transfer mode
|
|
|
|
* @param[in] workaround whether to workaround LIST -a
|
2016-01-16 00:47:18 +01:00
|
|
|
*
|
|
|
|
* @returns failure
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ftp_xfer_dir(ftp_session_t *session,
|
|
|
|
const char *args,
|
2016-01-26 02:11:53 +01:00
|
|
|
xfer_dir_mode_t mode,
|
|
|
|
bool workaround)
|
2016-01-16 00:47:18 +01:00
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
ssize_t rc;
|
|
|
|
size_t len;
|
|
|
|
struct stat st;
|
|
|
|
const char *base;
|
|
|
|
char *buffer;
|
|
|
|
|
|
|
|
/* set up the transfer */
|
|
|
|
session->flags &= ~(SESSION_RECV|SESSION_NLST);
|
|
|
|
session->flags |= SESSION_SEND;
|
2016-01-16 00:47:18 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
session->transfer = list_transfer;
|
|
|
|
session->buffersize = 0;
|
|
|
|
session->bufferpos = 0;
|
|
|
|
|
|
|
|
if(strlen(args) > 0)
|
2016-01-16 00:47:18 +01:00
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* an argument was provided */
|
|
|
|
if(build_path(session, session->cwd, args) != 0)
|
|
|
|
{
|
|
|
|
/* error building path */
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
return ftp_send_response(session, 550, "%s\r\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if this is a directory */
|
2016-01-26 02:11:53 +01:00
|
|
|
session->dp = opendir(session->buffer);
|
2016-01-22 02:25:22 +01:00
|
|
|
if(session->dp == NULL)
|
|
|
|
{
|
|
|
|
/* not a directory; check if it is a file */
|
2016-01-26 02:11:53 +01:00
|
|
|
rc = stat(session->buffer, &st);
|
2016-01-22 02:25:22 +01:00
|
|
|
if(rc != 0)
|
|
|
|
{
|
|
|
|
/* error getting stat */
|
2016-01-26 02:11:53 +01:00
|
|
|
rc = errno;
|
|
|
|
|
|
|
|
/* work around broken clients that think LIST -a is a thing */
|
|
|
|
if(workaround && mode == XFER_DIR_LIST)
|
|
|
|
{
|
|
|
|
if(args[0] == '-' && args[1] == 'a')
|
|
|
|
{
|
|
|
|
if(args[2] == 0)
|
|
|
|
buffer = strdup(args+2);
|
|
|
|
else
|
|
|
|
buffer = strdup(args+3);
|
|
|
|
|
|
|
|
if(buffer != NULL)
|
|
|
|
{
|
|
|
|
rc = ftp_xfer_dir(session, buffer, mode, false);
|
|
|
|
free(buffer);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-01-26 02:14:15 +01:00
|
|
|
rc = ENOMEM;
|
2016-01-26 02:11:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2016-01-26 02:11:53 +01:00
|
|
|
return ftp_send_response(session, 550, "%s\r\n", strerror(rc));
|
2016-01-22 02:25:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* get the base name */
|
2016-01-26 02:11:53 +01:00
|
|
|
base = strrchr(session->buffer, '/') + 1;
|
2016-01-22 02:25:22 +01:00
|
|
|
|
2016-01-22 21:30:26 +01:00
|
|
|
/* encode \n in path */
|
2016-01-22 02:25:22 +01:00
|
|
|
len = strlen(base);
|
2016-01-22 21:30:26 +01:00
|
|
|
buffer = encode_path(base, &len, false);
|
2016-01-22 02:25:22 +01:00
|
|
|
if(buffer != NULL)
|
|
|
|
{
|
|
|
|
/* copy to the session buffer to send */
|
|
|
|
session->buffersize =
|
|
|
|
sprintf(session->buffer,
|
2016-02-03 20:56:47 +01:00
|
|
|
"-rwxrwxrwx 1 3DS 3DS %lld Jan 1 1970 ",
|
|
|
|
(signed long long)st.st_size);
|
2016-01-22 02:25:22 +01:00
|
|
|
if(session->buffersize + len + 2 > sizeof(session->buffer))
|
|
|
|
{
|
|
|
|
/* buffer will overflow */
|
|
|
|
free(buffer);
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
ftp_send_response(session, 425, "%s\r\n", strerror(EOVERFLOW));
|
|
|
|
return LOOP_EXIT;
|
|
|
|
}
|
|
|
|
memcpy(session->buffer + session->buffersize, buffer, len);
|
|
|
|
free(buffer);
|
|
|
|
len = session->buffersize + len;
|
|
|
|
session->buffer[len++] = '\r';
|
|
|
|
session->buffer[len++] = '\n';
|
|
|
|
session->buffersize = len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
session->buffersize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* it was a directory, so set it as the lwd */
|
|
|
|
strcpy(session->lwd, session->buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(ftp_session_open_cwd(session) != 0)
|
|
|
|
{
|
|
|
|
/* no argument, but opening cwd failed */
|
2016-01-16 00:47:18 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2016-01-22 02:25:22 +01:00
|
|
|
return ftp_send_response(session, 550, "%s\r\n", strerror(errno));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* set the lwd as the cwd */
|
|
|
|
strcpy(session->lwd, session->cwd);
|
2016-01-16 00:47:18 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 04:35:39 +02:00
|
|
|
if(session->flags & (SESSION_PORT|SESSION_PASV))
|
2016-01-16 00:47:18 +01:00
|
|
|
{
|
2016-06-20 04:35:39 +02:00
|
|
|
ftp_session_set_state(session, DATA_CONNECT_STATE, CLOSE_DATA);
|
2016-01-16 00:47:18 +01:00
|
|
|
|
2016-06-20 04:35:39 +02:00
|
|
|
if(session->flags & SESSION_PORT)
|
2016-01-22 02:25:22 +01:00
|
|
|
{
|
2016-06-20 04:35:39 +02:00
|
|
|
/* setup connection */
|
|
|
|
rc = ftp_session_connect(session);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
|
|
|
/* error connecting */
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
return ftp_send_response(session, 425, "can't open data connection\r\n");
|
|
|
|
}
|
2016-01-22 02:25:22 +01:00
|
|
|
}
|
2016-01-16 00:47:18 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-22 02:25:22 +01:00
|
|
|
else if(mode == XFER_DIR_STAT)
|
|
|
|
{
|
|
|
|
/* this is a little different; we have to send the data over the command socket */
|
|
|
|
ftp_session_set_state(session, DATA_TRANSFER_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
session->data_fd = session->cmd_fd;
|
|
|
|
session->flags |= SESSION_SEND;
|
|
|
|
return ftp_send_response(session, -213, "Status\r\n");
|
|
|
|
}
|
2016-01-16 00:47:18 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we must have got LIST or NLST without a preceding PORT or PASV */
|
2016-01-16 00:47:18 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
return ftp_send_response(session, 503, "Bad sequence of commands\r\n");
|
|
|
|
}
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* *
|
|
|
|
* F T P C O M M A N D S *
|
|
|
|
* *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int ABOR(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief abort a transfer
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
|
|
|
FTP_DECLARE(ABOR)
|
|
|
|
{
|
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
|
|
|
|
|
|
|
if(session->state == COMMAND_STATE)
|
|
|
|
return ftp_send_response(session, 225, "No transfer to abort\r\n");
|
|
|
|
|
|
|
|
/* abort the transfer */
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
|
|
|
|
/* send response for this request */
|
|
|
|
ftp_send_response(session, 225, "Aborted\r\n");
|
|
|
|
|
|
|
|
/* send response for transfer */
|
|
|
|
return ftp_send_response(session, 425, "Transfer aborted\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! @fn static int ALLO(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief allocate space
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(ALLO)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
return ftp_send_response(session, 202, "superfluous command\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int APPE(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief append data to a file
|
|
|
|
*
|
|
|
|
* @note requires a PASV or PORT connection
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(APPE)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* open the file in append mode */
|
2016-01-16 00:28:59 +01:00
|
|
|
return ftp_xfer_file(session, args, XFER_FILE_APPE);
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int CDUP(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief CWD to parent directory
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(CDUP)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* change to parent directory */
|
2014-11-23 23:39:00 +01:00
|
|
|
cd_up(session);
|
|
|
|
|
|
|
|
return ftp_send_response(session, 200, "OK\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int CWD(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief change working directory
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(CWD)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
struct stat st;
|
|
|
|
int rc;
|
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* .. is equivalent to CDUP */
|
2014-11-23 23:39:00 +01:00
|
|
|
if(strcmp(args, "..") == 0)
|
|
|
|
{
|
|
|
|
cd_up(session);
|
|
|
|
return ftp_send_response(session, 200, "OK\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* build the new cwd path */
|
|
|
|
if(build_path(session, session->cwd, args) != 0)
|
2014-11-25 08:42:25 +01:00
|
|
|
return ftp_send_response(session, 553, "%s\r\n", strerror(errno));
|
2016-01-16 00:47:18 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* get the path status */
|
|
|
|
rc = stat(session->buffer, &st);
|
|
|
|
if(rc != 0)
|
2014-11-23 23:39:00 +01:00
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
console_print(RED "stat '%s': %d %s\n" RESET, session->buffer, errno, strerror(errno));
|
|
|
|
return ftp_send_response(session, 550, "unavailable\r\n");
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* make sure it is a directory */
|
|
|
|
if(!S_ISDIR(st.st_mode))
|
|
|
|
return ftp_send_response(session, 553, "not a directory\r\n");
|
|
|
|
|
|
|
|
/* copy the path into the cwd */
|
2014-11-23 23:39:00 +01:00
|
|
|
strncpy(session->cwd, session->buffer, sizeof(session->cwd));
|
2016-01-16 00:47:18 +01:00
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
return ftp_send_response(session, 200, "OK\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int DELE(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief delete a file
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(DELE)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
int rc;
|
2014-11-24 06:54:12 +01:00
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* build the file path */
|
|
|
|
if(build_path(session, session->cwd, args) != 0)
|
2014-11-25 08:42:25 +01:00
|
|
|
return ftp_send_response(session, 553, "%s\r\n", strerror(errno));
|
2014-11-24 06:54:12 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* try to unlink the path */
|
2014-11-24 06:54:12 +01:00
|
|
|
rc = unlink(session->buffer);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* error unlinking the file */
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(RED "unlink: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-24 06:54:12 +01:00
|
|
|
return ftp_send_response(session, 550, "failed to delete file\r\n");
|
|
|
|
}
|
|
|
|
|
2016-06-15 10:04:00 +02:00
|
|
|
update_free_space();
|
2014-11-24 06:54:12 +01:00
|
|
|
return ftp_send_response(session, 250, "OK\r\n");
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int FEAT(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief list server features
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(FEAT)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 23:38:22 +01:00
|
|
|
/* list our features */
|
|
|
|
return ftp_send_response(session, -211, "\r\n"
|
|
|
|
" MDTM\r\n"
|
2016-06-22 05:17:33 +02:00
|
|
|
" PASV\r\n"
|
|
|
|
" SIZE\r\n"
|
2016-01-22 23:38:22 +01:00
|
|
|
" UTF8\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"211 End\r\n");
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int HELP(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief print server help
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
|
|
|
FTP_DECLARE(HELP)
|
|
|
|
{
|
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
|
|
|
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
|
|
|
|
|
|
|
/* list our accepted commands */
|
|
|
|
return ftp_send_response(session, -214,
|
|
|
|
"The following commands are recognized\r\n"
|
2016-01-22 23:38:22 +01:00
|
|
|
" ABOR ALLO APPE CDUP CWD DELE FEAT HELP LIST MDTM MKD MODE NLST NOOP\r\n"
|
|
|
|
" OPTS PASS PASV PORT PWD QUIT REST RETR RMD RNFR RNTO STAT STOR STOU\r\n"
|
|
|
|
" STRU SYST TYPE USER XCUP XCWD XMKD XPWD XRMD\r\n"
|
2016-01-22 02:25:22 +01:00
|
|
|
"214 End\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! @fn static int LIST(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief retrieve a directory listing
|
|
|
|
*
|
|
|
|
* @note Requires a PORT or PASV connection
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(LIST)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* open the path in LIST mode */
|
2016-01-26 02:11:53 +01:00
|
|
|
return ftp_xfer_dir(session, args, XFER_DIR_LIST, true);
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 23:38:22 +01:00
|
|
|
/*! @fn static int MDTM(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief get last modification time
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
|
|
|
FTP_DECLARE(MDTM)
|
|
|
|
{
|
2016-01-26 02:12:19 +01:00
|
|
|
int rc;
|
|
|
|
#ifdef _3DS
|
|
|
|
uint64_t mtime;
|
|
|
|
#else
|
|
|
|
struct stat st;
|
|
|
|
#endif
|
|
|
|
time_t t_mtime;
|
|
|
|
struct tm *tm;
|
2016-01-22 23:38:22 +01:00
|
|
|
|
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
|
|
|
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
|
|
|
|
|
|
|
/* build the path */
|
|
|
|
if(build_path(session, session->cwd, args) != 0)
|
|
|
|
return ftp_send_response(session, 553, "%s\r\n", strerror(errno));
|
|
|
|
|
2016-01-26 02:12:19 +01:00
|
|
|
#ifdef _3DS
|
2016-01-22 23:38:22 +01:00
|
|
|
rc = sdmc_getmtime(session->buffer, &mtime);
|
|
|
|
if(rc != 0)
|
|
|
|
return ftp_send_response(session, 550, "Error getting mtime\r\n");
|
|
|
|
t_mtime = mtime;
|
2016-01-26 02:12:19 +01:00
|
|
|
#else
|
|
|
|
rc = stat(session->buffer, &st);
|
|
|
|
if(rc != 0)
|
|
|
|
return ftp_send_response(session, 550, "Error getting mtime\r\n");
|
|
|
|
t_mtime = st.st_mtime;
|
|
|
|
#endif
|
|
|
|
|
2016-01-22 23:38:22 +01:00
|
|
|
tm = gmtime(&t_mtime);
|
|
|
|
if(tm == NULL)
|
|
|
|
return ftp_send_response(session, 550, "Error getting mtime\r\n");
|
|
|
|
|
|
|
|
session->buffersize = strftime(session->buffer, sizeof(session->buffer), "%Y%m%d%H%M%S", tm);
|
|
|
|
if(session->buffersize == 0)
|
|
|
|
return ftp_send_response(session, 550, "Error getting mtime\r\n");
|
|
|
|
session->buffer[session->buffersize] = 0;
|
|
|
|
|
|
|
|
return ftp_send_response(session, 213, "%s\r\n", session->buffer);
|
|
|
|
}
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int MKD(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief create a directory
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(MKD)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
int rc;
|
2014-11-24 06:48:43 +01:00
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* build the path */
|
|
|
|
if(build_path(session, session->cwd, args) != 0)
|
2014-11-25 08:42:25 +01:00
|
|
|
return ftp_send_response(session, 553, "%s\r\n", strerror(errno));
|
2014-11-24 06:48:43 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* try to create the directory */
|
2014-11-24 06:48:43 +01:00
|
|
|
rc = mkdir(session->buffer, 0755);
|
2016-01-15 23:45:10 +01:00
|
|
|
if(rc != 0 && errno != EEXIST)
|
2014-11-24 06:48:43 +01:00
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* mkdir failure */
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(RED "mkdir: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-24 06:48:43 +01:00
|
|
|
return ftp_send_response(session, 550, "failed to create directory\r\n");
|
|
|
|
}
|
|
|
|
|
2016-06-15 10:04:00 +02:00
|
|
|
update_free_space();
|
2014-11-24 06:48:43 +01:00
|
|
|
return ftp_send_response(session, 250, "OK\r\n");
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int MODE(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief set transfer mode
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(MODE)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we only accept S (stream) mode */
|
2014-11-23 23:39:00 +01:00
|
|
|
if(strcasecmp(args, "S") == 0)
|
|
|
|
return ftp_send_response(session, 200, "OK\r\n");
|
|
|
|
|
|
|
|
return ftp_send_response(session, 504, "unavailable\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int NLST(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief retrieve a name list
|
|
|
|
*
|
|
|
|
* @note Requires a PASV or PORT connection
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(NLST)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* open the path in NLST mode */
|
2016-01-26 02:11:53 +01:00
|
|
|
return ftp_xfer_dir(session, args, XFER_DIR_NLST, false);
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int NOOP(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief no-op
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(NOOP)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2016-01-22 02:25:22 +01:00
|
|
|
|
|
|
|
/* this is a no-op */
|
2014-11-23 23:39:00 +01:00
|
|
|
return ftp_send_response(session, 200, "OK\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int OPTS(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief set options
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2015-08-19 20:25:34 +02:00
|
|
|
FTP_DECLARE(OPTS)
|
|
|
|
{
|
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2015-08-19 20:25:34 +02:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we only accept the following options */
|
2015-08-19 20:25:34 +02:00
|
|
|
if(strcasecmp(args, "UTF8") == 0
|
|
|
|
|| strcasecmp(args, "UTF8 ON") == 0
|
|
|
|
|| strcasecmp(args, "UTF8 NLST") == 0)
|
|
|
|
return ftp_send_response(session, 200, "OK\r\n");
|
|
|
|
|
|
|
|
return ftp_send_response(session, 504, "invalid argument\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int PASS(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief provide password
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(PASS)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we accept any password */
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2014-11-24 06:28:54 +01:00
|
|
|
return ftp_send_response(session, 230, "OK\r\n");
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int PASV(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief request an address to connect to
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(PASV)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char buffer[INET_ADDRSTRLEN + 10];
|
|
|
|
char *p;
|
|
|
|
in_port_t port;
|
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* reset the state */
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2014-11-23 23:39:00 +01:00
|
|
|
session->flags &= ~(SESSION_PASV|SESSION_PORT);
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* create a socket to listen on */
|
2014-11-23 23:39:00 +01:00
|
|
|
session->pasv_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(session->pasv_fd < 0)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "socket: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
return ftp_send_response(session, 451, "\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* set the socket options */
|
2016-01-15 23:45:10 +01:00
|
|
|
rc = ftp_set_socket_options(session->pasv_fd);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* failed to set socket options */
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_close_pasv(session);
|
|
|
|
return ftp_send_response(session, 451, "\r\n");
|
|
|
|
}
|
2016-01-12 23:24:01 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* grab a new port */
|
2014-11-25 08:22:09 +01:00
|
|
|
session->pasv_addr.sin_port = htons(next_data_port());
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
#ifdef _3DS
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(YELLOW "binding to %s:%u\n" RESET,
|
2014-11-23 23:39:00 +01:00
|
|
|
inet_ntoa(session->pasv_addr.sin_addr),
|
|
|
|
ntohs(session->pasv_addr.sin_port));
|
|
|
|
#endif
|
2016-01-22 02:25:22 +01:00
|
|
|
|
|
|
|
/* bind to the port */
|
2014-11-23 23:39:00 +01:00
|
|
|
rc = bind(session->pasv_fd, (struct sockaddr*)&session->pasv_addr,
|
|
|
|
sizeof(session->pasv_addr));
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* failed to bind */
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "bind: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_session_close_pasv(session);
|
|
|
|
return ftp_send_response(session, 451, "\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* listen on the socket */
|
2016-01-15 23:45:10 +01:00
|
|
|
rc = listen(session->pasv_fd, 1);
|
2014-11-23 23:39:00 +01:00
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* failed to listen */
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "listen: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_session_close_pasv(session);
|
|
|
|
return ftp_send_response(session, 451, "\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _3DS
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* get the socket address since we requested an ephemeral port */
|
2014-11-23 23:39:00 +01:00
|
|
|
socklen_t addrlen = sizeof(session->pasv_addr);
|
|
|
|
rc = getsockname(session->pasv_fd, (struct sockaddr*)&session->pasv_addr,
|
|
|
|
&addrlen);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* failed to get socket address */
|
2015-08-19 20:25:34 +02:00
|
|
|
console_print(RED "getsockname: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_session_close_pasv(session);
|
|
|
|
return ftp_send_response(session, 451, "\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we are now listening on the socket */
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(YELLOW "listening on %s:%u\n" RESET,
|
2014-11-23 23:39:00 +01:00
|
|
|
inet_ntoa(session->pasv_addr.sin_addr),
|
|
|
|
ntohs(session->pasv_addr.sin_port));
|
|
|
|
session->flags |= SESSION_PASV;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* print the address in the ftp format */
|
2014-11-23 23:39:00 +01:00
|
|
|
port = ntohs(session->pasv_addr.sin_port);
|
|
|
|
strcpy(buffer, inet_ntoa(session->pasv_addr.sin_addr));
|
|
|
|
sprintf(buffer+strlen(buffer), ",%u,%u",
|
|
|
|
port >> 8, port & 0xFF);
|
|
|
|
for(p = buffer; *p; ++p)
|
|
|
|
{
|
|
|
|
if(*p == '.')
|
|
|
|
*p = ',';
|
|
|
|
}
|
|
|
|
|
|
|
|
return ftp_send_response(session, 227, "%s\r\n", buffer);
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int PORT(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief provide an address for the server to connect to
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(PORT)
|
|
|
|
{
|
|
|
|
char *addrstr, *p, *portstr;
|
|
|
|
int commas = 0, rc;
|
|
|
|
short port = 0;
|
|
|
|
unsigned long val;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* reset the state */
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
2014-11-23 23:39:00 +01:00
|
|
|
session->flags &= ~(SESSION_PASV|SESSION_PORT);
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* dup the args since they are const and we need to change it */
|
2014-11-23 23:39:00 +01:00
|
|
|
addrstr = strdup(args);
|
|
|
|
if(addrstr == NULL)
|
|
|
|
return ftp_send_response(session, 425, "%s\r\n", strerror(ENOMEM));
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* replace a,b,c,d,e,f with a.b.c.d\0e.f */
|
2014-11-23 23:39:00 +01:00
|
|
|
for(p = addrstr; *p; ++p)
|
|
|
|
{
|
|
|
|
if(*p == ',')
|
|
|
|
{
|
|
|
|
if(commas != 3)
|
|
|
|
*p = '.';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*p = 0;
|
|
|
|
portstr = p+1;
|
|
|
|
}
|
|
|
|
++commas;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* make sure we got the right number of values */
|
2014-11-23 23:39:00 +01:00
|
|
|
if(commas != 5)
|
|
|
|
{
|
|
|
|
free(addrstr);
|
|
|
|
return ftp_send_response(session, 501, "%s\r\n", strerror(EINVAL));
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* parse the address */
|
2014-11-23 23:39:00 +01:00
|
|
|
rc = inet_aton(addrstr, &addr.sin_addr);
|
|
|
|
if(rc == 0)
|
|
|
|
{
|
|
|
|
free(addrstr);
|
|
|
|
return ftp_send_response(session, 501, "%s\r\n", strerror(EINVAL));
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* parse the port */
|
2014-11-23 23:39:00 +01:00
|
|
|
val = 0;
|
|
|
|
port = 0;
|
|
|
|
for(p = portstr; *p; ++p)
|
|
|
|
{
|
|
|
|
if(!isdigit((int)*p))
|
|
|
|
{
|
|
|
|
if(p == portstr || *p != '.' || val > 0xFF)
|
|
|
|
{
|
|
|
|
free(addrstr);
|
|
|
|
return ftp_send_response(session, 501, "%s\r\n", strerror(EINVAL));
|
|
|
|
}
|
|
|
|
port <<= 8;
|
|
|
|
port += val;
|
|
|
|
val = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val *= 10;
|
|
|
|
val += *p - '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* validate the port */
|
2014-11-23 23:39:00 +01:00
|
|
|
if(val > 0xFF || port > 0xFF)
|
|
|
|
{
|
|
|
|
free(addrstr);
|
|
|
|
return ftp_send_response(session, 501, "%s\r\n", strerror(EINVAL));
|
|
|
|
}
|
|
|
|
port <<= 8;
|
|
|
|
port += val;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* fill in the address port and family */
|
2014-11-23 23:39:00 +01:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
|
|
|
|
free(addrstr);
|
|
|
|
|
|
|
|
memcpy(&session->peer_addr, &addr, sizeof(addr));
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we are ready to connect to the client */
|
2014-11-23 23:39:00 +01:00
|
|
|
session->flags |= SESSION_PORT;
|
|
|
|
return ftp_send_response(session, 200, "OK\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int PWD(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief print working directory
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(PWD)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
static char buffer[CMD_BUFFERSIZE];
|
|
|
|
size_t len = sizeof(buffer), i;
|
|
|
|
char *path;
|
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 21:30:26 +01:00
|
|
|
/* encode the cwd */
|
2016-01-22 02:25:22 +01:00
|
|
|
len = strlen(session->cwd);
|
2016-01-22 21:30:26 +01:00
|
|
|
path = encode_path(session->cwd, &len, true);
|
2016-01-22 02:25:22 +01:00
|
|
|
if(path != NULL)
|
|
|
|
{
|
2016-01-22 21:28:55 +01:00
|
|
|
i = sprintf(buffer, "257 \"");
|
2016-01-22 02:25:22 +01:00
|
|
|
if(i + len + 3 > sizeof(buffer))
|
|
|
|
{
|
|
|
|
/* buffer will overflow */
|
|
|
|
free(path);
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, CLOSE_PASV | CLOSE_DATA);
|
|
|
|
ftp_send_response(session, 550, "unavailable\r\n");
|
|
|
|
return ftp_send_response(session, 425, "%s\r\n", strerror(EOVERFLOW));
|
|
|
|
}
|
|
|
|
memcpy(buffer+i, path, len);
|
|
|
|
free(path);
|
|
|
|
len += i;
|
|
|
|
buffer[len++] = '"';
|
|
|
|
buffer[len++] = '\r';
|
|
|
|
buffer[len++] = '\n';
|
|
|
|
|
|
|
|
return ftp_send_response_buffer(session, buffer, len);
|
|
|
|
}
|
|
|
|
return ftp_send_response(session, 425, "%s\r\n", strerror(ENOMEM));
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int QUIT(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief terminate ftp session
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(QUIT)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* disconnect from the client */
|
2014-11-23 23:39:00 +01:00
|
|
|
ftp_send_response(session, 221, "disconnecting\r\n");
|
|
|
|
ftp_session_close_cmd(session);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int REST(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief restart a transfer
|
|
|
|
*
|
|
|
|
* @note sets file position for a subsequent STOR operation
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(REST)
|
|
|
|
{
|
2016-01-19 00:46:03 +01:00
|
|
|
const char *p;
|
|
|
|
uint64_t pos = 0;
|
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* make sure an argument is provided */
|
2016-01-19 00:46:03 +01:00
|
|
|
if(args == NULL)
|
|
|
|
return ftp_send_response(session, 504, "invalid argument\r\n");
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* parse the offset */
|
2016-01-19 00:46:03 +01:00
|
|
|
for(p = args; *p; ++p)
|
|
|
|
{
|
|
|
|
if(!isdigit((int)*p))
|
|
|
|
return ftp_send_response(session, 504, "invalid argument\r\n");
|
|
|
|
|
|
|
|
if(UINT64_MAX / 10 < pos)
|
|
|
|
return ftp_send_response(session, 504, "invalid argument\r\n");
|
|
|
|
|
|
|
|
pos *= 10;
|
|
|
|
|
|
|
|
if(UINT64_MAX - (*p - '0') < pos)
|
|
|
|
return ftp_send_response(session, 504, "invalid argument\r\n");
|
|
|
|
|
|
|
|
pos += (*p - '0');
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* set the restart offset */
|
2016-01-19 00:46:03 +01:00
|
|
|
session->filepos = pos;
|
|
|
|
return ftp_send_response(session, 200, "OK\r\n");
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int RETR(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief retrieve a file
|
|
|
|
*
|
|
|
|
* @note Requires a PASV or PORT connection
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(RETR)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* open the file to retrieve */
|
2016-01-16 00:28:59 +01:00
|
|
|
return ftp_xfer_file(session, args, XFER_FILE_RETR);
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int RMD(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief remove a directory
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(RMD)
|
|
|
|
{
|
2015-08-19 20:25:34 +02:00
|
|
|
int rc;
|
2014-11-24 07:01:42 +01:00
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* build the path to remove */
|
|
|
|
if(build_path(session, session->cwd, args) != 0)
|
2014-11-25 08:42:25 +01:00
|
|
|
return ftp_send_response(session, 553, "%s\r\n", strerror(errno));
|
2014-11-24 07:01:42 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* remove the directory */
|
2014-11-24 07:01:42 +01:00
|
|
|
rc = rmdir(session->buffer);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* rmdir error */
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(RED "rmdir: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-24 07:01:42 +01:00
|
|
|
return ftp_send_response(session, 550, "failed to delete directory\r\n");
|
|
|
|
}
|
|
|
|
|
2016-06-15 10:04:00 +02:00
|
|
|
update_free_space();
|
2014-11-24 07:01:42 +01:00
|
|
|
return ftp_send_response(session, 250, "OK\r\n");
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int RNFR(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief rename from
|
|
|
|
*
|
|
|
|
* @note Must be followed by RNTO
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(RNFR)
|
|
|
|
{
|
2014-11-24 07:16:21 +01:00
|
|
|
int rc;
|
|
|
|
struct stat st;
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* build the path to rename from */
|
|
|
|
if(build_path(session, session->cwd, args) != 0)
|
2014-11-25 08:42:25 +01:00
|
|
|
return ftp_send_response(session, 553, "%s\r\n", strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* make sure the path exists */
|
2014-11-24 07:16:21 +01:00
|
|
|
rc = lstat(session->buffer, &st);
|
|
|
|
if(rc != 0)
|
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* error getting path status */
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(RED "lstat: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-24 07:16:21 +01:00
|
|
|
return ftp_send_response(session, 450, "no such file or directory\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we are ready for RNTO */
|
2014-11-23 23:39:00 +01:00
|
|
|
session->flags |= SESSION_RENAME;
|
2014-11-24 07:16:21 +01:00
|
|
|
return ftp_send_response(session, 350, "OK\r\n");
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int RNTO(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief rename to
|
|
|
|
*
|
|
|
|
* @note Must be preceded by RNFR
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(RNTO)
|
|
|
|
{
|
2016-01-12 23:24:01 +01:00
|
|
|
int rc;
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* make sure the previous command was RNFR */
|
2014-11-23 23:39:00 +01:00
|
|
|
if(!(session->flags & SESSION_RENAME))
|
|
|
|
return ftp_send_response(session, 503, "Bad sequence of commands\r\n");
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* clear the rename state */
|
2014-11-23 23:39:00 +01:00
|
|
|
session->flags &= ~SESSION_RENAME;
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* copy the RNFR path */
|
2016-01-12 23:24:01 +01:00
|
|
|
memcpy(session->tmp_buffer, session->buffer, XFER_BUFFERSIZE);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* build the path to rename to */
|
|
|
|
if(build_path(session, session->cwd, args) != 0)
|
2014-11-25 08:42:25 +01:00
|
|
|
return ftp_send_response(session, 554, "%s\r\n", strerror(errno));
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* rename the file */
|
2016-01-12 23:24:01 +01:00
|
|
|
rc = rename(session->tmp_buffer, session->buffer);
|
2014-11-25 08:22:09 +01:00
|
|
|
if(rc != 0)
|
2014-11-24 07:16:21 +01:00
|
|
|
{
|
2016-01-22 02:25:22 +01:00
|
|
|
/* rename failure */
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(RED "rename: %d %s\n" RESET, errno, strerror(errno));
|
2014-11-24 07:16:21 +01:00
|
|
|
return ftp_send_response(session, 550, "failed to rename file/directory\r\n");
|
|
|
|
}
|
|
|
|
|
2016-06-15 10:04:00 +02:00
|
|
|
update_free_space();
|
2014-11-24 07:16:21 +01:00
|
|
|
return ftp_send_response(session, 250, "OK\r\n");
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-06-22 05:17:33 +02:00
|
|
|
/*! @fn static int SIZE(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief get file size
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
|
|
|
FTP_DECLARE(SIZE)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
|
|
|
|
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
|
|
|
|
|
|
|
/* build the path to stat */
|
|
|
|
if(build_path(session, session->cwd, args) != 0)
|
|
|
|
return ftp_send_response(session, 553, "%s\r\n", strerror(errno));
|
|
|
|
|
|
|
|
rc = stat(session->buffer, &st);
|
|
|
|
if(rc != 0 || !S_ISREG(st.st_mode))
|
|
|
|
return ftp_send_response(session, 550, "Could not get file size.\r\n");
|
|
|
|
|
|
|
|
return ftp_send_response(session, 213, "%" PRIu64 "\r\n",
|
|
|
|
(uint64_t)st.st_size);
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int STAT(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief get status
|
|
|
|
*
|
|
|
|
* @note If no argument is supplied, and a transfer is occurring, get the
|
|
|
|
* current transfer status. If no argument is supplied, and no transfer
|
|
|
|
* is occurring, get the server status. If an argument is supplied, this
|
|
|
|
* is equivalent to LIST, except the data is sent over the command
|
|
|
|
* socket.
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
|
|
|
FTP_DECLARE(STAT)
|
|
|
|
{
|
|
|
|
time_t uptime = time(NULL) - start_time;
|
|
|
|
int hours = uptime / 3600;
|
|
|
|
int minutes = (uptime / 60) % 60;
|
|
|
|
int seconds = uptime % 60;
|
|
|
|
|
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
|
|
|
|
|
|
|
if(session->state == DATA_CONNECT_STATE)
|
|
|
|
{
|
|
|
|
/* we are waiting to connect to the client */
|
|
|
|
return ftp_send_response(session, -211, "FTP server status\r\n"
|
|
|
|
" Waiting for data connection\r\n"
|
|
|
|
"211 End\r\n");
|
|
|
|
}
|
|
|
|
else if(session->state == DATA_TRANSFER_STATE)
|
|
|
|
{
|
|
|
|
/* we are in the middle of a transfer */
|
|
|
|
return ftp_send_response(session, -211, "FTP server status\r\n"
|
|
|
|
" Transferred %" PRIu64 " bytes\r\n"
|
|
|
|
"211 End\r\n",
|
|
|
|
session->filepos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strlen(args) == 0)
|
|
|
|
{
|
|
|
|
/* no argument provided, send the server status */
|
|
|
|
return ftp_send_response(session, -211, "FTP server status\r\n"
|
|
|
|
" Uptime: %02d:%02d:%02d\r\n"
|
|
|
|
"211 End\r\n",
|
|
|
|
hours, minutes, seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* argument provided, open the path in STAT mode */
|
2016-01-26 02:11:53 +01:00
|
|
|
return ftp_xfer_dir(session, args, XFER_DIR_STAT, false);
|
2016-01-22 02:25:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*! @fn static int STOR(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief store a file
|
|
|
|
*
|
|
|
|
* @note Requires a PASV or PORT connection
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(STOR)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* open the file to store */
|
2016-01-16 00:28:59 +01:00
|
|
|
return ftp_xfer_file(session, args, XFER_FILE_STOR);
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int STOU(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief store a unique file
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(STOU)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we do not support this yet */
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
return ftp_send_response(session, 502, "unavailable\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int STRU(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief set file structure
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(STRU)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we only support F (no structure) mode */
|
2014-11-23 23:39:00 +01:00
|
|
|
if(strcasecmp(args, "F") == 0)
|
|
|
|
return ftp_send_response(session, 200, "OK\r\n");
|
|
|
|
|
|
|
|
return ftp_send_response(session, 504, "unavailable\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int SYST(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief identify system
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(SYST)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we are UNIX compliant with 8-bit characters */
|
2014-11-23 23:39:00 +01:00
|
|
|
return ftp_send_response(session, 215, "UNIX Type: L8\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int TYPE(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief set transfer mode
|
|
|
|
*
|
|
|
|
* @note transfer mode is always binary
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(TYPE)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we always transfer in binary mode */
|
2014-11-23 23:39:00 +01:00
|
|
|
return ftp_send_response(session, 200, "OK\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/*! @fn static int USER(ftp_session_t *session, const char *args)
|
|
|
|
*
|
|
|
|
* @brief provide user name
|
|
|
|
*
|
|
|
|
* @param[in] session ftp session
|
|
|
|
* @param[in] args arguments
|
|
|
|
*
|
|
|
|
* @returns error
|
|
|
|
*/
|
2014-11-23 23:39:00 +01:00
|
|
|
FTP_DECLARE(USER)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
console_print(CYAN "%s %s\n" RESET, __func__, args ? args : "");
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-15 23:45:10 +01:00
|
|
|
ftp_session_set_state(session, COMMAND_STATE, 0);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2016-01-22 02:25:22 +01:00
|
|
|
/* we accept any user name */
|
2014-11-24 06:28:54 +01:00
|
|
|
return ftp_send_response(session, 230, "OK\r\n");
|
2014-11-23 23:39:00 +01:00
|
|
|
}
|