2012-02-04 22:02:45 +01:00
|
|
|
/****************************************************************************
|
2021-05-31 13:49:09 +02:00
|
|
|
* apps/netutils/ftpd/ftpd.c
|
2012-02-04 22:02:45 +01:00
|
|
|
*
|
2020-01-12 01:32:40 +01:00
|
|
|
* Copyright (C) 2012, 2015, 2020 Gregory Nutt. All rights reserved.
|
2012-02-04 22:02:45 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Includes original code as well as logic adapted from hwport_ftpd, written
|
|
|
|
* by Jaehyuk Cho <minzkn@minzkn.com> which is released under a BSD license.
|
|
|
|
*
|
2012-02-05 18:36:13 +01:00
|
|
|
* Copyright (C) hwport.com. All rights reserved.
|
|
|
|
* Author: Jaehyuk Cho <mailto:minzkn@minzkn.com>
|
2012-02-04 22:02:45 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
2012-02-05 18:36:13 +01:00
|
|
|
#include <sys/stat.h>
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-11-13 08:54:34 +01:00
|
|
|
#include <stdint.h>
|
2012-02-04 22:02:45 +01:00
|
|
|
#include <stdio.h>
|
2012-02-05 18:36:13 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
2017-02-16 22:58:15 +01:00
|
|
|
#include <strings.h>
|
2012-02-05 18:36:13 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <libgen.h>
|
2021-05-31 13:49:09 +02:00
|
|
|
#include <assert.h>
|
2012-02-04 22:02:45 +01:00
|
|
|
#include <errno.h>
|
2012-02-05 18:36:13 +01:00
|
|
|
#include <debug.h>
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2022-09-03 13:32:49 +02:00
|
|
|
#ifdef CONFIG_FTPD_LOGIN_PASSWD
|
2023-06-10 18:44:11 +02:00
|
|
|
# include "fsutils/passwd.h"
|
2022-09-03 13:32:49 +02:00
|
|
|
#endif
|
2022-03-28 13:46:07 +02:00
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/ftpd.h"
|
2012-02-05 18:36:13 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
#include "ftpd.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
/* Account functions */
|
|
|
|
|
|
|
|
static FAR struct ftpd_account_s *ftpd_account_new(FAR const char *user,
|
2022-03-28 13:46:07 +02:00
|
|
|
uint8_t accountflags);
|
2012-02-04 22:02:45 +01:00
|
|
|
static void ftpd_account_free(FAR struct ftpd_account_s *account);
|
2022-03-28 13:46:07 +02:00
|
|
|
static int ftpd_account_setpassword(FAR struct ftpd_account_s *account,
|
|
|
|
FAR const char *passwd);
|
|
|
|
static int ftpd_account_add(FAR struct ftpd_server_s *server,
|
|
|
|
FAR struct ftpd_account_s *account);
|
|
|
|
static int ftpd_account_sethome(FAR struct ftpd_account_s *account,
|
|
|
|
FAR const char *home);
|
2012-02-04 22:02:45 +01:00
|
|
|
static FAR struct ftpd_account_s *
|
2022-03-28 13:46:07 +02:00
|
|
|
ftpd_account_search_user(FAR struct ftpd_session_s *session,
|
|
|
|
FAR const char *user);
|
|
|
|
static bool ftpd_account_login(FAR struct ftpd_session_s *session,
|
|
|
|
FAR const char *user, FAR const char *passwd);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
/* Parsing functions */
|
|
|
|
|
|
|
|
static FAR char *ftpd_strtok(bool skipspace, FAR const char *delimiters,
|
2022-03-28 13:46:07 +02:00
|
|
|
FAR char **str);
|
2012-02-04 22:02:45 +01:00
|
|
|
static FAR char *ftpd_strtok_alloc(bool skipspace,
|
2022-03-28 13:46:07 +02:00
|
|
|
FAR const char *delimiters,
|
|
|
|
FAR const char **str);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
/* Socket helpers */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
static int ftpd_rxpoll(int sd, int timeout);
|
|
|
|
static int ftpd_txpoll(int sd, int timeout);
|
|
|
|
static int ftpd_accept(int sd, FAR void *addr, FAR socklen_t *addrlen,
|
|
|
|
int timeout);
|
2012-02-04 22:02:45 +01:00
|
|
|
static ssize_t ftpd_recv(int sd, FAR void *data, size_t size, int timeout);
|
|
|
|
static ssize_t ftpd_send(int sd, FAR const void *data, size_t size,
|
2022-03-28 13:46:07 +02:00
|
|
|
int timeout);
|
2022-07-17 08:05:23 +02:00
|
|
|
static ssize_t ftpd_response(int sd, int timeout, FAR const char *fmt, ...)
|
2022-12-12 20:06:32 +01:00
|
|
|
printf_like(3, 4);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
static int ftpd_dataopen(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_dataclose(FAR struct ftpd_session_s *session);
|
2020-01-12 01:32:40 +01:00
|
|
|
static FAR struct ftpd_server_s *ftpd_openserver(int port,
|
|
|
|
sa_family_t family);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
/* Path helpers */
|
|
|
|
|
|
|
|
static int ftpd_pathignore(FAR struct ftpd_pathnode_s *currpath);
|
|
|
|
static void ftpd_nodefree(FAR struct ftpd_pathnode_s *node);
|
|
|
|
static FAR struct ftpd_pathnode_s *ftpd_path2node(FAR const char *path);
|
|
|
|
static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node,
|
2022-03-28 13:46:07 +02:00
|
|
|
bool strip);
|
2012-02-04 22:02:45 +01:00
|
|
|
static FAR struct ftpd_pathnode_s *
|
2022-03-28 13:46:07 +02:00
|
|
|
ftpd_nodeappend(FAR struct ftpd_pathnode_s *head,
|
|
|
|
FAR struct ftpd_pathnode_s *node, bool override);
|
|
|
|
static int ftpd_getpath(FAR struct ftpd_session_s *session,
|
|
|
|
FAR const char *path, FAR char **abspath,
|
|
|
|
FAR char **workpath);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Command helpers */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
static int ftpd_changedir(FAR struct ftpd_session_s *session,
|
|
|
|
FAR const char *rempath);
|
2012-02-04 22:02:45 +01:00
|
|
|
static off_t ftpd_offsatoi(FAR const char *filename, off_t offset);
|
|
|
|
static int ftpd_stream(FAR struct ftpd_session_s *session, int cmdtype);
|
|
|
|
static uint8_t ftpd_listoption(FAR char **param);
|
2022-03-28 13:46:07 +02:00
|
|
|
static int ftpd_listbuffer(FAR struct ftpd_session_s *session,
|
|
|
|
FAR char *path, FAR struct stat *st,
|
|
|
|
FAR char *buffer, size_t buflen,
|
|
|
|
unsigned int opton);
|
|
|
|
static int fptd_listscan(FAR struct ftpd_session_s *session,
|
|
|
|
FAR char *path, unsigned int opton);
|
|
|
|
static int ftpd_list(FAR struct ftpd_session_s *session,
|
|
|
|
unsigned int opton);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
/* Command handlers */
|
|
|
|
|
|
|
|
static int ftpd_command_user(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_pass(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_syst(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_type(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_mode(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_abor(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_quit(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_noop(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_port(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_eprt(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_pwd(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_cwd(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_cdup(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_rmd(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_mkd(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_dele(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_pasv(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_epsv(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_list(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_nlst(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_acct(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_size(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_stru(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_rnfr(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_rnto(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_retr(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_stor(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_appe(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_rest(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_mdtm(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_opts(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_site(FAR struct ftpd_session_s *session);
|
|
|
|
static int ftpd_command_help(FAR struct ftpd_session_s *session);
|
|
|
|
|
|
|
|
static int ftpd_command(FAR struct ftpd_session_s *session);
|
|
|
|
|
|
|
|
/* Worker thread */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
static int ftpd_startworker(pthread_startroutine_t handler, FAR void *arg,
|
|
|
|
size_t stacksize);
|
2012-02-04 22:02:45 +01:00
|
|
|
static void ftpd_freesession(FAR struct ftpd_session_s *session);
|
|
|
|
static void ftpd_workersetup(FAR struct ftpd_session_s *session);
|
|
|
|
static FAR void *ftpd_worker(FAR void *arg);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct ftpd_cmd_s g_ftpdcmdtab[] =
|
|
|
|
{
|
|
|
|
{"USER", ftpd_command_user, 0}, /* USER <SP> <username> <CRLF> */
|
|
|
|
{"PASS", ftpd_command_pass, 0}, /* PASS <SP> <password> <CRLF> */
|
|
|
|
{"SYST", ftpd_command_syst, FTPD_CMDFLAG_LOGIN}, /* SYST <CRLF> */
|
|
|
|
{"TYPE", ftpd_command_type, FTPD_CMDFLAG_LOGIN}, /* TYPE <SP> <type-code> <CRLF> */
|
|
|
|
{"MODE", ftpd_command_mode, FTPD_CMDFLAG_LOGIN}, /* MODE <SP> <mode-code> <CRLF> */
|
|
|
|
{"ABOR", ftpd_command_abor, FTPD_CMDFLAG_LOGIN}, /* ABOR <CRLF> */
|
|
|
|
{"QUIT", ftpd_command_quit, 0}, /* QUIT <CRLF> */
|
|
|
|
{"NOOP", ftpd_command_noop, FTPD_CMDFLAG_LOGIN}, /* NOOP <CRLF> */
|
|
|
|
{"PORT", ftpd_command_port, FTPD_CMDFLAG_LOGIN}, /* PORT <SP> <host-port> <CRLF> */
|
|
|
|
{"EPRT", ftpd_command_eprt, FTPD_CMDFLAG_LOGIN}, /* EPRT <SP> <d> <net-prt> <d> <net-addr> <d> <tcp-port> <d> <CRLF> */
|
|
|
|
{"PWD" , ftpd_command_pwd , FTPD_CMDFLAG_LOGIN}, /* PWD <CRLF> */
|
|
|
|
{"XPWD", ftpd_command_pwd , FTPD_CMDFLAG_LOGIN}, /* XPWD <CRLF> */
|
|
|
|
{"CWD" , ftpd_command_cwd , FTPD_CMDFLAG_LOGIN}, /* CWD <SP> <pathname> <CRLF> */
|
|
|
|
{"XCWD", ftpd_command_cwd , FTPD_CMDFLAG_LOGIN}, /* XCWD <SP> <pathname> <CRLF> */
|
|
|
|
{"CDUP", ftpd_command_cdup, FTPD_CMDFLAG_LOGIN}, /* CDUP <CRLF> */
|
|
|
|
{"XCUP", ftpd_command_cdup, FTPD_CMDFLAG_LOGIN}, /* XCUP <CRLF> */
|
|
|
|
{"RMD" , ftpd_command_rmd , FTPD_CMDFLAG_LOGIN}, /* RMD <SP> <pathname> <CRLF> */
|
|
|
|
{"XRMD", ftpd_command_rmd , FTPD_CMDFLAG_LOGIN}, /* XRMD <SP> <pathname> <CRLF> */
|
|
|
|
{"MKD" , ftpd_command_mkd , FTPD_CMDFLAG_LOGIN}, /* MKD <SP> <pathname> <CRLF> */
|
|
|
|
{"XMKD", ftpd_command_mkd , FTPD_CMDFLAG_LOGIN}, /* XMKD <SP> <pathname> <CRLF> */
|
|
|
|
{"DELE", ftpd_command_dele, FTPD_CMDFLAG_LOGIN}, /* DELE <SP> <pathname> <CRLF> */
|
|
|
|
{"PASV", ftpd_command_pasv, FTPD_CMDFLAG_LOGIN}, /* PASV <CRLF> */
|
2014-04-14 00:24:28 +02:00
|
|
|
{"EPSV", ftpd_command_epsv, FTPD_CMDFLAG_LOGIN}, /* EPSV <SP> <net-prt> <CRLF> OR EPSV <SP> ALL <CRLF> */
|
2012-02-04 22:02:45 +01:00
|
|
|
{"LPSV", ftpd_command_epsv, FTPD_CMDFLAG_LOGIN}, /* LPSV ??? */
|
|
|
|
{"LIST", ftpd_command_list, FTPD_CMDFLAG_LOGIN}, /* LIST [<SP> <pathname>] <CRLF> */
|
|
|
|
{"NLST", ftpd_command_nlst, FTPD_CMDFLAG_LOGIN}, /* NLST [<SP> <pathname>] <CRLF> */
|
|
|
|
{"ACCT", ftpd_command_acct, FTPD_CMDFLAG_LOGIN}, /* ACCT <SP> <account-information> <CRLF> */
|
|
|
|
{"SIZE", ftpd_command_size, FTPD_CMDFLAG_LOGIN}, /* SIZE <SP> <pathname> <CRLF> */
|
|
|
|
{"STRU", ftpd_command_stru, FTPD_CMDFLAG_LOGIN}, /* STRU <SP> <structure-code> <CRLF> */
|
|
|
|
{"RNFR", ftpd_command_rnfr, FTPD_CMDFLAG_LOGIN}, /* RNFR <SP> <pathname> <CRLF> */
|
|
|
|
{"RNTO", ftpd_command_rnto, FTPD_CMDFLAG_LOGIN}, /* RNTO <SP> <pathname> <CRLF> */
|
|
|
|
{"RETR", ftpd_command_retr, FTPD_CMDFLAG_LOGIN}, /* RETR <SP> <pathname> <CRLF> */
|
|
|
|
{"STOR", ftpd_command_stor, FTPD_CMDFLAG_LOGIN}, /* STOR <SP> <pathname> <CRLF> */
|
|
|
|
{"APPE", ftpd_command_appe, FTPD_CMDFLAG_LOGIN}, /* APPE <SP> <pathname> <CRLF> */
|
|
|
|
{"REST", ftpd_command_rest, FTPD_CMDFLAG_LOGIN}, /* REST <SP> <marker> <CRLF> */
|
|
|
|
{"MDTM", ftpd_command_mdtm, FTPD_CMDFLAG_LOGIN}, /* MDTM <SP> <pathname> <CRLF> */
|
|
|
|
{"OPTS", ftpd_command_opts, FTPD_CMDFLAG_LOGIN}, /* OPTS <SP> <option> <value> <CRLF> */
|
|
|
|
{"SITE", ftpd_command_site, FTPD_CMDFLAG_LOGIN}, /* SITE <SP> <string> <CRLF> */
|
|
|
|
{"HELP", ftpd_command_help, FTPD_CMDFLAG_LOGIN}, /* HELP [<SP> <string>] <CRLF> */
|
2012-02-05 18:36:13 +01:00
|
|
|
#if 0
|
2012-02-04 22:02:45 +01:00
|
|
|
{"SMNT", ftpd_command_smnt, FTPD_CMDFLAG_LOGIN}, /* SMNT <SP> <pathname> <CRLF> */
|
|
|
|
{"REIN", ftpd_command_rein, FTPD_CMDFLAG_LOGIN}, /* REIN <CRLF> */
|
|
|
|
{"STOU", ftpd_command_stou, FTPD_CMDFLAG_LOGIN}, /* STOU <CRLF> */
|
|
|
|
{"STAT", ftpd_command_stat, FTPD_CMDFLAG_LOGIN}, /* STAT [<SP> <pathname>] <CRLF> */
|
|
|
|
{"ALLO", ftpd_command_stat, FTPD_CMDFLAG_LOGIN}, /* ALLO <SP> <decimal-integer> [<SP> R <SP> <decimal-integer>] <CRLF> */
|
|
|
|
#endif
|
2014-04-14 00:24:28 +02:00
|
|
|
{NULL, (ftpd_cmdhandler_t)0, 0}
|
2012-02-04 22:02:45 +01:00
|
|
|
};
|
|
|
|
|
2012-02-05 23:46:56 +01:00
|
|
|
static const char g_cdup[] = "..";
|
|
|
|
static const char g_respfmt1[] = "%03u%c%s\r\n"; /* Integer, character, string */
|
|
|
|
static const char g_respfmt2[] = "%03u%c%s%s\r\n"; /* Integer, character, two strings */
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
static const char *g_monthtab[] =
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
|
|
|
|
"Oct", "Nov", "Dec"
|
2012-02-04 22:02:45 +01:00
|
|
|
};
|
2012-02-05 23:46:56 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
static const char *g_ftpdhelp[] =
|
|
|
|
{
|
|
|
|
"The following commands are recognized (* =>'s unimplemented):",
|
|
|
|
"CWD XCWD CDUP XCUP SMNT* QUIT PORT PASV",
|
|
|
|
"EPRT* EPSV* ALLO* RNFR RNTO DELE MDTM RMD",
|
|
|
|
"XRMD MKD XMKD PWD XPWD SIZE SYST HELP",
|
|
|
|
"NOOP FEAT* OPTS AUTH* CCC* CONF* ENC* MIC*",
|
|
|
|
"PBSZ* PROT* TYPE STRU* MODE* RETR STOR STOU*",
|
|
|
|
"APPE REST ABOR USER PASS ACCT* REIN* LIST",
|
|
|
|
"NLST STAT* SITE* MLSD* MLST*",
|
|
|
|
"Direct comments to " CONFIG_FTPD_VENDORID,
|
2020-01-12 01:32:40 +01:00
|
|
|
NULL
|
2012-02-04 22:02:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_account_new
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct ftpd_account_s *ftpd_account_new(FAR const char *user,
|
|
|
|
uint8_t accountflags)
|
|
|
|
{
|
|
|
|
FAR struct ftpd_account_s *ret;
|
|
|
|
size_t usersize;
|
|
|
|
size_t allocsize;
|
|
|
|
|
|
|
|
/* Get the size of the allocation */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (user == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
usersize = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
usersize = strlen(user) + 1;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
allocsize = sizeof(struct ftpd_account_s) + usersize;
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
/* Allocate the account and user string */
|
|
|
|
|
|
|
|
ret = (struct ftpd_account_s *)zalloc(allocsize);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (ret == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Failed to allocate account\n");
|
2012-02-04 22:02:45 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the account and user string */
|
|
|
|
|
|
|
|
ret->flags = accountflags;
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (user != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ret->user = (FAR char *)&ret[1];
|
2023-02-09 12:01:32 +01:00
|
|
|
strlcpy(ret->user, user, usersize);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2012-02-05 23:18:14 +01:00
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_account_free
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ftpd_account_free(FAR struct ftpd_account_s *account)
|
|
|
|
{
|
|
|
|
struct ftpd_account_s *prev;
|
|
|
|
DEBUGASSERT(account);
|
|
|
|
|
|
|
|
/* Back up to the first entry in the list */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
while (account->blink != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
account = account->blink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then free the entire list */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
while (account != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
prev = account;
|
|
|
|
account = account->flink;
|
|
|
|
|
|
|
|
/* Free the home path and the password */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (prev->home != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(prev->home);
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (prev->password != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(prev->password);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then free the container itself */
|
|
|
|
|
|
|
|
free(prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-02-05 23:18:14 +01:00
|
|
|
* Name: ftpd_account_setpassword
|
2012-02-04 22:02:45 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-05 23:18:14 +01:00
|
|
|
static int ftpd_account_setpassword(FAR struct ftpd_account_s *account,
|
2022-03-28 13:46:07 +02:00
|
|
|
FAR const char *passwd)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
FAR char *temp;
|
|
|
|
DEBUGASSERT(account);
|
|
|
|
|
|
|
|
/* Make of copy of the password string (if it is non-null) */
|
|
|
|
|
|
|
|
temp = NULL;
|
2022-03-28 13:46:07 +02:00
|
|
|
if (passwd != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
temp = strdup(passwd);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (temp == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free any existing password string */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (account->password != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(account->password);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the new password */
|
|
|
|
|
|
|
|
account->password = temp;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_account_add
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_account_add(FAR struct ftpd_server_s *server,
|
|
|
|
FAR struct ftpd_account_s *account)
|
|
|
|
{
|
|
|
|
FAR struct ftpd_account_s *head;
|
|
|
|
FAR struct ftpd_account_s *tail;
|
|
|
|
DEBUGASSERT(server && account);
|
|
|
|
|
|
|
|
/* Find the beginning of the list */
|
|
|
|
|
|
|
|
head = account;
|
2022-03-28 13:46:07 +02:00
|
|
|
while (head->blink != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
head = head->blink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the tail of the list */
|
|
|
|
|
|
|
|
tail = account;
|
2022-03-28 13:46:07 +02:00
|
|
|
while (tail->flink != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
tail = tail->flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the case where the list is empty */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (server->head == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
server->head = head;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
head->blink = server->tail;
|
|
|
|
server->tail->flink = head;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->tail = tail;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_account_sethome
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_account_sethome(FAR struct ftpd_account_s *account,
|
|
|
|
FAR const char *home)
|
|
|
|
{
|
|
|
|
FAR char *temp;
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
DEBUGASSERT(account != NULL);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
/* Make a copy of the home path string (unless it is NULL) */
|
|
|
|
|
|
|
|
temp = NULL;
|
2022-03-28 13:46:07 +02:00
|
|
|
if (home != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
temp = strdup(home);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (temp == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free any existing home path string */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (account->home != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(account->home);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And set the new home path string */
|
|
|
|
|
|
|
|
account->home = temp;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_account_search_user
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct ftpd_account_s *
|
|
|
|
ftpd_account_search_user(FAR struct ftpd_session_s *session,
|
2012-02-05 23:18:14 +01:00
|
|
|
FAR const char *user)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
FAR struct ftpd_account_s *newaccount = NULL;
|
2022-04-04 12:36:53 +02:00
|
|
|
FAR const struct ftpd_account_s *account;
|
2014-04-14 00:24:28 +02:00
|
|
|
uint8_t accountflags;
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
account = session->head;
|
2022-03-28 13:46:07 +02:00
|
|
|
while (account != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
accountflags = account->flags;
|
|
|
|
|
|
|
|
/* Check if the account has a user */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (account->user == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
/* No.. The account has no user, was a user name provided? */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (user == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
/* No.. create the account */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
newaccount = ftpd_account_new(NULL, accountflags);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (newaccount != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
if (ftpd_account_setpassword(newaccount,
|
|
|
|
account->password) < 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ftpd_account_free(newaccount);
|
|
|
|
newaccount = NULL;
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
else if (ftpd_account_sethome(newaccount,
|
|
|
|
account->home) < 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ftpd_account_free(newaccount);
|
|
|
|
newaccount = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Was a user name provided? */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
else if (user != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
/* Check if matches the user name on the account */
|
|
|
|
|
2012-02-05 23:18:14 +01:00
|
|
|
if (strcmp(user, (FAR const char *)account->user) == 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
/* Yes.. create the account */
|
|
|
|
|
|
|
|
newaccount = ftpd_account_new(account->user, accountflags);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (newaccount != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
if (ftpd_account_setpassword(newaccount,
|
|
|
|
account->password) != 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ftpd_account_free(newaccount);
|
|
|
|
newaccount = NULL;
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
else if (ftpd_account_sethome(newaccount,
|
|
|
|
account->home) != 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ftpd_account_free(newaccount);
|
|
|
|
newaccount = NULL;
|
|
|
|
}
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try the next account */
|
|
|
|
|
|
|
|
account = account->flink;
|
|
|
|
}
|
|
|
|
|
2012-02-05 23:18:14 +01:00
|
|
|
return newaccount;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_account_login
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
static bool ftpd_account_login(FAR struct ftpd_session_s *session,
|
|
|
|
FAR const char *user, FAR const char *passwd)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
FAR char *home = NULL;
|
|
|
|
uint8_t flags;
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
#if defined(CONFIG_FTPD_LOGIN_PASSWD)
|
|
|
|
if (user != NULL && passwd != NULL &&
|
|
|
|
PASSWORD_VERIFY_MATCH(passwd_verify(user, passwd)))
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
flags = FTPD_ACCOUNTFLAG_ADMIN;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2022-03-28 13:46:07 +02:00
|
|
|
else
|
|
|
|
#endif
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
FAR struct ftpd_account_s *account = NULL;
|
|
|
|
bool pwvalid;
|
|
|
|
|
|
|
|
account = ftpd_account_search_user(session, user);
|
|
|
|
if (account == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (account->password == NULL)
|
|
|
|
{
|
|
|
|
if (passwd == NULL)
|
|
|
|
{
|
|
|
|
pwvalid = true;
|
|
|
|
}
|
|
|
|
else if (passwd[0] == '\0')
|
|
|
|
{
|
|
|
|
pwvalid = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pwvalid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (passwd == NULL)
|
|
|
|
{
|
|
|
|
pwvalid = false;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2022-03-28 13:46:07 +02:00
|
|
|
else if (strcmp(passwd, (FAR const char *)account->password) == 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
pwvalid = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pwvalid = false;
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (!pwvalid)
|
|
|
|
{
|
|
|
|
ftpd_account_free(account);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (account->home != NULL)
|
|
|
|
{
|
|
|
|
home = strdup(account->home);
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = account->flags;
|
|
|
|
|
2012-02-05 23:18:14 +01:00
|
|
|
ftpd_account_free(account);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (home == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
home = getenv("HOME");
|
2022-03-28 13:46:07 +02:00
|
|
|
if (home == NULL)
|
|
|
|
{
|
|
|
|
home = strdup("/");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
home = strdup(home);
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if ((flags & FTPD_ACCOUNTFLAG_ADMIN) != 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
/* admin user */
|
|
|
|
|
|
|
|
session->home = strdup("/");
|
2022-03-28 13:46:07 +02:00
|
|
|
session->work = home;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
else
|
2020-01-12 01:32:40 +01:00
|
|
|
{
|
|
|
|
/* normal user */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
session->home = home;
|
2020-01-12 01:32:40 +01:00
|
|
|
session->work = strdup("/");
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
return true;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_strtok
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR char *ftpd_strtok(bool skipspace, FAR const char *delimiters,
|
|
|
|
FAR char **str)
|
|
|
|
{
|
|
|
|
FAR const char *dptr;
|
|
|
|
FAR char *sptr;
|
|
|
|
FAR char *ret;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
sptr = *str;
|
|
|
|
|
|
|
|
/* Skip any leading spaces */
|
|
|
|
|
|
|
|
if (skipspace)
|
|
|
|
{
|
|
|
|
while (isspace(*sptr))
|
|
|
|
{
|
|
|
|
sptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sptr;
|
|
|
|
|
|
|
|
/* The following is an implementation of strtok. It does not modify the
|
|
|
|
* original string as strtok does, however.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (*sptr != '\0')
|
|
|
|
{
|
|
|
|
dptr = delimiters;
|
2012-02-05 20:32:42 +01:00
|
|
|
while (*sptr != *dptr && *dptr != '\0')
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
dptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*sptr == *dptr)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr++;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
/* Save the place where we will resuming searching */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
*str = sptr;
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_strtok_alloc
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-22 09:53:36 +02:00
|
|
|
static FAR char *ftpd_strtok_alloc(bool skipspace,
|
|
|
|
FAR const char *delimiters,
|
2012-02-04 22:02:45 +01:00
|
|
|
FAR const char **str)
|
|
|
|
{
|
|
|
|
FAR const char *sptr;
|
|
|
|
FAR const char *left;
|
|
|
|
FAR const char *right;
|
|
|
|
FAR const char *dptr;
|
|
|
|
FAR char *ret;
|
|
|
|
size_t tokenlen;
|
|
|
|
|
|
|
|
sptr = *str;
|
|
|
|
|
|
|
|
if (skipspace)
|
|
|
|
{
|
|
|
|
while (isspace(*sptr))
|
|
|
|
{
|
|
|
|
sptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
right = sptr;
|
|
|
|
left = sptr;
|
|
|
|
|
|
|
|
/* The the following logic is similar to strtok(), but only bounds the
|
|
|
|
* token of interest between left (the first character of the substring)
|
|
|
|
* and right (the character after the end of the substring).
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (*sptr != '\0')
|
|
|
|
{
|
|
|
|
dptr = delimiters;
|
2012-02-05 18:36:13 +01:00
|
|
|
while (*sptr != *dptr && *dptr != '\0')
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
dptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*sptr == *dptr)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr++;
|
|
|
|
|
|
|
|
/* Adjust the right pointer but ignoring any trailing spaces if
|
|
|
|
* 'skipspace' is selected.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!skipspace || !isspace(*sptr))
|
|
|
|
{
|
|
|
|
right = sptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
/* Allocate memory large enough to hold the entire sub-string (including
|
|
|
|
* the NUL terminator.
|
|
|
|
*/
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
tokenlen = (size_t)(right - left);
|
|
|
|
ret = (FAR char *)malloc(tokenlen + 1);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (ret != NULL)
|
2020-01-12 01:32:40 +01:00
|
|
|
{
|
|
|
|
if (tokenlen > 0)
|
|
|
|
{
|
|
|
|
memcpy(ret, left, tokenlen);
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ret[tokenlen] = '\0';
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
/* Save the place where we will resuming searching */
|
|
|
|
|
|
|
|
*str = sptr;
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_rxpoll
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_rxpoll(int sd, int timeout)
|
|
|
|
{
|
|
|
|
struct pollfd fds[1];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set up the poll */
|
|
|
|
|
|
|
|
fds[0].fd = sd;
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
fds[0].revents = 0;
|
|
|
|
|
|
|
|
/* Perform the poll. */
|
|
|
|
|
|
|
|
ret = poll(fds, 1, timeout);
|
|
|
|
|
|
|
|
/* Handle the result of the poll. On success, poll returns the number
|
|
|
|
* of structures that have nonzero revents fields. A value of 0 indicates
|
|
|
|
* that the call timed out and no file descriptors were ready. On error,
|
|
|
|
* -1 is returned, and errno is set appropriately:
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
int errval = errno;
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("poll() failed: %d\n", errval);
|
2012-02-04 22:02:45 +01:00
|
|
|
return -errval;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_txpoll
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_txpoll(int sd, int timeout)
|
|
|
|
{
|
|
|
|
struct pollfd fds[1];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set up the poll */
|
|
|
|
|
|
|
|
fds[0].fd = sd;
|
|
|
|
fds[0].events = POLLOUT;
|
|
|
|
fds[0].revents = 0;
|
|
|
|
|
|
|
|
/* Perform the poll. */
|
|
|
|
|
|
|
|
ret = poll(fds, 1, timeout);
|
|
|
|
|
|
|
|
/* Handle the result of the poll. On success, poll returns the number
|
|
|
|
* of structures that have nonzero revents fields. A value of 0 indicates
|
|
|
|
* that the call timed out and no file descriptors were ready. On error,
|
|
|
|
* -1 is returned, and errno is set appropriately:
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("poll() timed out\n");
|
2012-02-04 22:02:45 +01:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
int errval = errno;
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("poll() failed: %d\n", errval);
|
2012-02-04 22:02:45 +01:00
|
|
|
return -errval;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_accept
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_accept(int sd, FAR void *addr, FAR socklen_t *addrlen,
|
|
|
|
int timeout)
|
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
int acceptsd;
|
2012-02-04 22:02:45 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Handle any requested timeout */
|
|
|
|
|
|
|
|
if (timeout >= 0)
|
|
|
|
{
|
|
|
|
ret = ftpd_rxpoll(sd, timeout);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-05-22 09:53:36 +02:00
|
|
|
/* Only report interesting,
|
|
|
|
* infrequent errors (not the common timeout)
|
|
|
|
*/
|
2012-02-18 19:13:30 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_NET
|
|
|
|
if (ret != -ETIMEDOUT)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpd_rxpoll() failed: %d\n", ret);
|
2012-02-18 19:13:30 +01:00
|
|
|
}
|
|
|
|
#endif
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Accept the connection -- waiting if necessary */
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
acceptsd = accept(sd, (FAR struct sockaddr *)addr, addrlen);
|
|
|
|
if (acceptsd < 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
int errval = errno;
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: accept() failed: %d\n", errval);
|
2012-02-04 22:02:45 +01:00
|
|
|
return -errval;
|
|
|
|
}
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
return acceptsd;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_recv
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t ftpd_recv(int sd, FAR void *data, size_t size, int timeout)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Handle any requested timetout */
|
|
|
|
|
|
|
|
if (timeout >= 0)
|
|
|
|
{
|
|
|
|
status = ftpd_rxpoll(sd, timeout);
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("ftpd_rxpoll: %d\n", status);
|
2012-02-04 22:02:45 +01:00
|
|
|
return (ssize_t)status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 09:53:36 +02:00
|
|
|
/* Receive the data... waiting if necessary.
|
|
|
|
* The client side will break the connection after the file has been sent.
|
|
|
|
* Zero (end-of-file) should be received in this case.
|
2012-02-18 19:13:30 +01:00
|
|
|
*/
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
ret = recv(sd, data, size, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-02-16 00:37:37 +01:00
|
|
|
int errval = errno;
|
|
|
|
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: recv() failed: %d\n", errval);
|
2012-02-18 19:13:30 +01:00
|
|
|
return -errval;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_send
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
static ssize_t ftpd_send(int sd, FAR const void *data, size_t size,
|
|
|
|
int timeout)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ssize_t ret;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
/* Handle any requested timetout */
|
|
|
|
|
|
|
|
if (timeout >= 0)
|
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
int status = ftpd_txpoll(sd, timeout);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (status < 0)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("ftpd_rxpoll: %d\n", status);
|
2012-02-04 22:02:45 +01:00
|
|
|
return (ssize_t)status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then send the data (waiting if necessary) */
|
|
|
|
|
|
|
|
ret = send(sd, data, size, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-10-04 11:45:20 +02:00
|
|
|
int errval = errno;
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: send() failed: %d\n", errval);
|
2012-02-04 22:02:45 +01:00
|
|
|
return -errval;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_response
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t ftpd_response(int sd, int timeout, FAR const char *fmt, ...)
|
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
FAR char *buffer;
|
2012-02-04 22:02:45 +01:00
|
|
|
ssize_t bytessent;
|
|
|
|
va_list ap;
|
2023-04-12 17:42:28 +02:00
|
|
|
int ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2023-04-12 17:42:28 +02:00
|
|
|
ret = vasprintf(&buffer, fmt, ap);
|
2012-02-04 22:02:45 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
2023-04-12 17:42:28 +02:00
|
|
|
if (ret < 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytessent = ftpd_send(sd, buffer, strlen(buffer), timeout);
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
return bytessent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_dataopen
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_dataopen(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
int sd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (session->data.sd < 0)
|
|
|
|
{
|
|
|
|
/* PORT session */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (session->data.addr.ss.ss_family == AF_INET6)
|
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
session->data.sd = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
session->data.sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
#else
|
2012-02-05 18:36:13 +01:00
|
|
|
session->data.sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2012-02-04 22:02:45 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (session->data.sd < 0)
|
|
|
|
{
|
|
|
|
int errval = errno;
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: socket() failed: %d\n", errval);
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 451, ' ', "Socket error !");
|
2012-02-04 22:02:45 +01:00
|
|
|
return -errval;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->data.addrlen = (socklen_t)sizeof(session->data.addr);
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = connect(session->data.sd,
|
|
|
|
(FAR const struct sockaddr *)(&session->data.addr),
|
2012-02-05 18:36:13 +01:00
|
|
|
session->data.addrlen);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errval = errno;
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: connect() failed: %d\n", errval);
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 451, ' ', "Connect error !");
|
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return -errval;
|
|
|
|
}
|
|
|
|
|
2014-07-11 20:30:23 +02:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
struct linger ling;
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
memset(&ling, 0, sizeof(ling));
|
2020-01-12 01:32:40 +01:00
|
|
|
ling.l_onoff = 1;
|
2012-02-04 22:02:45 +01:00
|
|
|
ling.l_linger = 4;
|
2020-01-12 01:32:40 +01:00
|
|
|
setsockopt(session->data.sd, SOL_SOCKET, SO_LINGER, &ling,
|
|
|
|
sizeof(ling));
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
/* PASV session */
|
|
|
|
|
|
|
|
session->data.addrlen = sizeof(session->data.addr);
|
2020-05-22 09:53:36 +02:00
|
|
|
sd = ftpd_accept(session->data.sd,
|
|
|
|
(struct sockaddr *)(&session->data.addr),
|
|
|
|
&session->data.addrlen, -1);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (sd < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpd_accept() failed: %d\n", sd);
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 451, ' ', "Accept error !");
|
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return sd;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(session->data.sd);
|
|
|
|
session->data.sd = sd;
|
|
|
|
|
2014-07-11 20:30:23 +02:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2020-05-22 09:53:36 +02:00
|
|
|
{
|
|
|
|
struct linger ling;
|
|
|
|
|
|
|
|
memset(&ling, 0, sizeof(ling));
|
|
|
|
ling.l_onoff = 1;
|
|
|
|
ling.l_linger = 4;
|
|
|
|
setsockopt(session->data.sd, SOL_SOCKET, SO_LINGER,
|
|
|
|
&ling, sizeof(ling));
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_dataclose
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_dataclose(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
if (session->data.sd >= 0)
|
|
|
|
{
|
|
|
|
close(session->data.sd);
|
|
|
|
session->data.sd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_openserver
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-22 09:53:36 +02:00
|
|
|
static FAR struct ftpd_server_s *ftpd_openserver(int port,
|
|
|
|
sa_family_t family)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
FAR struct ftpd_server_s *server;
|
|
|
|
socklen_t addrlen;
|
|
|
|
FAR const void *addr;
|
2014-04-14 00:24:28 +02:00
|
|
|
#if defined(SOMAXCONN)
|
2012-02-04 22:02:45 +01:00
|
|
|
int backlog = SOMAXCONN;
|
|
|
|
#else
|
|
|
|
int backlog = 5;
|
|
|
|
#endif
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Allocate the server instance */
|
|
|
|
|
|
|
|
server = (FAR struct ftpd_server_s *)zalloc(sizeof(struct ftpd_server_s));
|
2022-03-28 13:46:07 +02:00
|
|
|
if (server == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Failed to allocate server\n");
|
2012-02-05 18:36:13 +01:00
|
|
|
return NULL;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the server instance */
|
|
|
|
|
2021-01-13 19:01:00 +01:00
|
|
|
server->sd = -1;
|
|
|
|
server->head = NULL;
|
|
|
|
server->tail = NULL;
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
/* Create the server listen socket */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2017-11-07 00:41:30 +01:00
|
|
|
if (family == AF_INET6)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
server->addr.in6.sin6_family = family;
|
|
|
|
server->addr.in6.sin6_addr = in6addr_any;
|
|
|
|
server->addr.in6.sin6_port = htons(port);
|
|
|
|
|
|
|
|
addrlen = (socklen_t)sizeof(server->addr.in6);
|
|
|
|
addr = (FAR void *)(&server->addr.in6);
|
|
|
|
|
|
|
|
server->sd = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2017-11-07 00:41:30 +01:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (family == AF_INET)
|
|
|
|
{
|
|
|
|
server->addr.in4.sin_family = family;
|
|
|
|
server->addr.in4.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
server->addr.in4.sin_port = htons(port);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
addrlen = (socklen_t)sizeof(server->addr.in4);
|
|
|
|
addr = (FAR void *)(&server->addr.in4);
|
|
|
|
|
|
|
|
server->sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
nerr("ERROR: Unsupported family\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
if (server->sd < 0)
|
|
|
|
{
|
|
|
|
ftpd_close((FTPD_SESSION)server);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-22 09:53:36 +02:00
|
|
|
{
|
|
|
|
int reuse = 1;
|
|
|
|
setsockopt(server->sd, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
&reuse, sizeof(reuse));
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
/* Bind the socket to the address */
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
ret = bind(server->sd, (FAR const struct sockaddr *)addr, addrlen);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ftpd_close((FTPD_SESSION)server);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Listen on the socket */
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
ret = listen(server->sd, backlog);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ftpd_close((FTPD_SESSION)server);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (FTPD_SESSION)server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_pathignore
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_pathignore(FAR struct ftpd_pathnode_s *currpath)
|
|
|
|
{
|
|
|
|
FAR struct ftpd_pathnode_s *node;
|
|
|
|
size_t namelen;
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
namelen = currpath->name == NULL ? 0 : strlen(currpath->name);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
if (namelen == 0)
|
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
if (currpath->blink)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
currpath->ignore = true;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(currpath->name, "..") == 0)
|
|
|
|
{
|
|
|
|
currpath->ignore = true;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
node = currpath->blink;
|
2022-03-28 13:46:07 +02:00
|
|
|
while (node != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
if (!node->ignore)
|
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
namelen = node->name == NULL ? 0 : strlen(node->name);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
if (namelen > 0)
|
|
|
|
{
|
|
|
|
node->ignore = true;
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
node = node->blink;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(currpath->name, ".") == 0)
|
|
|
|
{
|
|
|
|
currpath->ignore = true;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-08-13 23:52:59 +02:00
|
|
|
* Name: ftpd_nodefree
|
2012-02-04 22:02:45 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ftpd_nodefree(FAR struct ftpd_pathnode_s *node)
|
|
|
|
{
|
|
|
|
FAR struct ftpd_pathnode_s *prev;
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
while (node != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
prev = node;
|
|
|
|
node = node->flink;
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (prev->name != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(prev->name);
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
free(prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_path2node
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct ftpd_pathnode_s *ftpd_path2node(FAR const char *path)
|
|
|
|
{
|
|
|
|
FAR struct ftpd_pathnode_s *head = NULL;
|
|
|
|
FAR struct ftpd_pathnode_s *tail = NULL;
|
|
|
|
FAR struct ftpd_pathnode_s *newnode;
|
|
|
|
FAR char *name;
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (path == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
while (path[0] != '\0')
|
|
|
|
{
|
|
|
|
name = ftpd_strtok_alloc(false, "/\\", &path);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (name == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path[0] != '\0')
|
|
|
|
{
|
|
|
|
path++;
|
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
newnode = (FAR struct ftpd_pathnode_s *)
|
|
|
|
malloc(sizeof(struct ftpd_pathnode_s));
|
2022-03-28 13:46:07 +02:00
|
|
|
if (newnode == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(name);
|
|
|
|
ftpd_nodefree(head);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
newnode->blink = tail;
|
|
|
|
newnode->flink = NULL;
|
|
|
|
newnode->ignore = false;
|
|
|
|
newnode->name = name;
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (tail == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
head = newnode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tail->flink = newnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
tail = newnode;
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_pathignore(newnode);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_node2path
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node,
|
2022-03-28 13:46:07 +02:00
|
|
|
bool strip)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
FAR struct ftpd_pathnode_s *node1;
|
|
|
|
FAR struct ftpd_pathnode_s *node2;
|
|
|
|
FAR char *path;
|
2023-03-05 16:55:15 +01:00
|
|
|
size_t allocsize;
|
|
|
|
size_t namelen;
|
|
|
|
size_t next;
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (node == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
allocsize = 0;
|
|
|
|
node1 = node;
|
2022-03-28 13:46:07 +02:00
|
|
|
while (node1 != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
if (strip)
|
|
|
|
{
|
|
|
|
if (node1->ignore)
|
|
|
|
{
|
|
|
|
node1 = node1->flink;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node2 = node1->flink;
|
2022-03-28 13:46:07 +02:00
|
|
|
while (strip && node2 != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
if (!node2->ignore)
|
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
break;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
node2 = node2->flink;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
namelen = node1->name == NULL ? 0 : strlen(node1->name);
|
|
|
|
if (node2 == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
if (namelen <= 0)
|
|
|
|
{
|
|
|
|
allocsize += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-05 16:55:15 +01:00
|
|
|
allocsize += namelen + 1;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
allocsize += namelen + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
node1 = node1->flink;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
path = (FAR char *)malloc(allocsize);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (path == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-03-05 16:55:15 +01:00
|
|
|
next = 0;
|
2012-02-04 22:02:45 +01:00
|
|
|
node1 = node;
|
2022-03-28 13:46:07 +02:00
|
|
|
while (node1 != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
if (strip)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
if (node1->ignore)
|
|
|
|
{
|
|
|
|
node1 = node1->flink;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node2 = node1->flink;
|
2022-03-28 13:46:07 +02:00
|
|
|
while (strip && node2 != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
if (!node2->ignore)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
node2 = node2->flink;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
namelen = node1->name == NULL ? 0 : strlen(node1->name);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (node2 == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
if (namelen <= 0)
|
|
|
|
{
|
2023-03-05 16:55:15 +01:00
|
|
|
snprintf(&path[next], allocsize - next, "/");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-05 16:55:15 +01:00
|
|
|
snprintf(&path[next], allocsize - next, "%s", node1->name);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-05 16:55:15 +01:00
|
|
|
snprintf(&path[next], allocsize - next, "%s%s", node1->name, "/");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
node1 = node1->flink;
|
2023-03-05 16:55:15 +01:00
|
|
|
next += strlen(&path[next]);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_nodeappend
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct ftpd_pathnode_s *
|
|
|
|
ftpd_nodeappend(FAR struct ftpd_pathnode_s *head,
|
|
|
|
FAR struct ftpd_pathnode_s *node, bool override)
|
|
|
|
{
|
|
|
|
FAR struct ftpd_pathnode_s *temp;
|
|
|
|
|
|
|
|
if (override)
|
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
if (node != NULL && node->name != NULL && strlen(node->name) <= 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ftpd_nodefree(head);
|
2014-04-14 00:24:28 +02:00
|
|
|
head = NULL;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (head == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
if (node != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
node->blink = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
head = node;
|
|
|
|
node = NULL;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (node != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
temp = head;
|
|
|
|
while (temp->flink)
|
|
|
|
{
|
|
|
|
temp = temp->flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->blink = temp;
|
|
|
|
temp->flink = node;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
/* clear ignore */
|
|
|
|
|
|
|
|
temp = head;
|
2022-03-28 13:46:07 +02:00
|
|
|
while (temp != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
temp->ignore = false;
|
|
|
|
temp = temp->flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restrip */
|
|
|
|
|
|
|
|
temp = head;
|
2022-03-28 13:46:07 +02:00
|
|
|
while (temp != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_pathignore(temp);
|
2012-02-04 22:02:45 +01:00
|
|
|
temp = temp->flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-02-05 18:36:13 +01:00
|
|
|
* Name: ftpd_getpath
|
2012-02-04 22:02:45 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
static int ftpd_getpath(FAR struct ftpd_session_s *session,
|
|
|
|
FAR const char *path, FAR char **abspath,
|
|
|
|
FAR char **workpath)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
FAR struct ftpd_pathnode_s *abspath_node;
|
|
|
|
FAR struct ftpd_pathnode_s *worknode;
|
|
|
|
FAR struct ftpd_pathnode_s *appendnode;
|
|
|
|
FAR char *abspath_local;
|
|
|
|
FAR char *workpath_local;
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (abspath != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
*abspath = NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (workpath != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
*workpath = NULL;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
worknode = ftpd_path2node(session->work == NULL ? "" : session->work);
|
|
|
|
if (worknode == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
appendnode = ftpd_path2node(path);
|
2012-02-04 22:02:45 +01:00
|
|
|
worknode = ftpd_nodeappend(worknode, appendnode, true);
|
|
|
|
workpath_local = ftpd_node2path(worknode, 1);
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (workpath_local == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ftpd_nodefree(worknode);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
abspath_node = ftpd_path2node(!session->home ? "" : session->home);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (abspath_node == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(workpath_local);
|
|
|
|
ftpd_nodefree(worknode);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
appendnode = ftpd_path2node(workpath_local);
|
|
|
|
abspath_node = ftpd_nodeappend(abspath_node, appendnode, false);
|
|
|
|
abspath_local = ftpd_node2path(abspath_node, 1);
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (abspath_local == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(workpath_local);
|
|
|
|
ftpd_nodefree(abspath_node);
|
|
|
|
ftpd_nodefree(worknode);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (workpath == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(workpath_local);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*workpath = workpath_local;
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (abspath == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(abspath_local);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*abspath = abspath_local;
|
|
|
|
}
|
|
|
|
|
|
|
|
ftpd_nodefree(abspath_node);
|
|
|
|
ftpd_nodefree(worknode);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_changedir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_changedir(FAR struct ftpd_session_s *session,
|
2012-02-05 18:36:13 +01:00
|
|
|
FAR const char *rempath)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
FAR char *abspath;
|
|
|
|
FAR char *workpath;
|
|
|
|
struct stat st;
|
|
|
|
int ret;
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = ftpd_getpath(session, rempath, (FAR char **)(&abspath),
|
|
|
|
(FAR char **)(&workpath));
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ',
|
|
|
|
"Can not change directory !");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = stat(abspath, &st);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
free(workpath);
|
|
|
|
free(abspath);
|
2022-04-03 14:17:20 +02:00
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt2, 550, ' ', rempath,
|
|
|
|
": No such file or directory");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode) == 0)
|
|
|
|
{
|
|
|
|
free(workpath);
|
|
|
|
free(abspath);
|
2022-04-03 14:17:20 +02:00
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt2, 550, ' ', rempath,
|
|
|
|
": No such file or directory");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(abspath);
|
|
|
|
if (session->work)
|
|
|
|
{
|
|
|
|
free(session->work);
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
session->work = workpath;
|
|
|
|
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 250, ' ', "CWD command successful");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_offsatoi
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static off_t ftpd_offsatoi(FAR const char *filename, off_t offset)
|
|
|
|
{
|
|
|
|
off_t ret;
|
|
|
|
off_t temp;
|
|
|
|
FILE *outstream;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
outstream = fopen(filename, "r");
|
2022-03-28 13:46:07 +02:00
|
|
|
if (outstream == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
int errval = errno;
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Failed to open %s: %d\n", filename, errval);
|
2012-02-04 22:02:45 +01:00
|
|
|
return -errval;
|
|
|
|
}
|
|
|
|
|
2014-04-14 00:24:28 +02:00
|
|
|
ret = 0;
|
|
|
|
temp = 0;
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
if (offset == (off_t)(-1))
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
for (; ; )
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ch = getc(outstream);
|
|
|
|
if (ch == EOF)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
ret++;
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
|
|
|
/* ret is ascii mode size */
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (offset < temp)
|
|
|
|
{
|
|
|
|
ch = getc(outstream);
|
|
|
|
if (ch == EOF)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret++;
|
|
|
|
temp++;
|
|
|
|
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
temp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ret is binary mode offset */
|
|
|
|
}
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
fclose(outstream);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_stream
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_stream(FAR struct ftpd_session_s *session, int cmdtype)
|
|
|
|
{
|
|
|
|
FAR char *abspath;
|
|
|
|
FAR char *path;
|
|
|
|
bool isnew;
|
|
|
|
int oflags;
|
2012-02-05 18:36:13 +01:00
|
|
|
FAR char *buffer;
|
2012-02-04 22:02:45 +01:00
|
|
|
size_t buflen;
|
|
|
|
size_t wantsize;
|
|
|
|
ssize_t rdbytes;
|
|
|
|
ssize_t wrbytes;
|
2012-02-16 00:37:37 +01:00
|
|
|
int errval = 0;
|
2012-02-04 22:02:45 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ftpd_getpath(session, session->param, &abspath, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 550, ' ', "Stream error !");
|
2012-02-04 22:02:45 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
path = abspath;
|
|
|
|
|
|
|
|
ret = ftpd_dataopen(session);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmdtype)
|
|
|
|
{
|
|
|
|
case 0: /* retr */
|
|
|
|
oflags = O_RDONLY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* stor */
|
|
|
|
oflags = O_CREAT | O_WRONLY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* appe */
|
|
|
|
oflags = O_CREAT | O_WRONLY | O_APPEND;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
oflags = O_RDONLY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(O_LARGEFILE)
|
|
|
|
oflags |= O_LARGEFILE;
|
|
|
|
#endif
|
2012-02-15 23:41:17 +01:00
|
|
|
|
|
|
|
/* Are we creating the file? */
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
if ((oflags & O_CREAT) != 0)
|
|
|
|
{
|
|
|
|
int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
|
|
|
|
|
|
|
|
if (session->restartpos <= 0)
|
|
|
|
{
|
|
|
|
oflags |= O_TRUNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
isnew = true;
|
|
|
|
session->fd = open(path, oflags | O_EXCL, mode);
|
|
|
|
if (session->fd < 0)
|
|
|
|
{
|
|
|
|
isnew = false;
|
|
|
|
session->fd = open(path, oflags, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-15 23:41:17 +01:00
|
|
|
/* No.. we are opening an existing file */
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
isnew = false;
|
|
|
|
session->fd = open(path, oflags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session->fd < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ', "Can not open file !");
|
2012-02-04 22:02:45 +01:00
|
|
|
goto errout_with_data;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
/* Restart position */
|
|
|
|
|
|
|
|
if (session->restartpos > 0)
|
|
|
|
{
|
2012-02-16 00:37:37 +01:00
|
|
|
off_t seekoffs = (off_t)-1;
|
2012-02-04 22:02:45 +01:00
|
|
|
off_t seekpos;
|
|
|
|
|
|
|
|
/* Get the seek position */
|
|
|
|
|
|
|
|
if (session->type == FTPD_SESSIONTYPE_A)
|
|
|
|
{
|
|
|
|
seekpos = ftpd_offsatoi(path, session->restartpos);
|
|
|
|
if (seekpos < 0)
|
|
|
|
{
|
2020-11-27 06:08:45 +01:00
|
|
|
nerr("ERROR: ftpd_offsatoi failed: %jd\n", (intmax_t)seekpos);
|
2012-02-16 00:37:37 +01:00
|
|
|
errval = -seekpos;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
seekpos = session->restartpos;
|
|
|
|
if (seekpos < 0)
|
|
|
|
{
|
2020-11-27 06:08:45 +01:00
|
|
|
nerr("ERROR: Bad restartpos: %jd\n", (intmax_t)seekpos);
|
2012-02-16 00:37:37 +01:00
|
|
|
errval = EINVAL;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Seek to the request position */
|
|
|
|
|
|
|
|
if (seekpos >= 0)
|
|
|
|
{
|
|
|
|
seekoffs = lseek(session->fd, seekpos, SEEK_SET);
|
|
|
|
if (seekoffs < 0)
|
|
|
|
{
|
2012-02-16 00:37:37 +01:00
|
|
|
errval = errno;
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: lseek failed: %d\n", errval);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-16 00:37:37 +01:00
|
|
|
/* Report errors. If an error occurred, seekoffs will be negative and
|
|
|
|
* errval will hold the (positive) error code.
|
|
|
|
*/
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
if (seekoffs < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ', "Can not seek file !");
|
2012-02-16 00:37:37 +01:00
|
|
|
ret = -errval;
|
2012-02-04 22:02:45 +01:00
|
|
|
goto errout_with_session;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send success message */
|
|
|
|
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 150, ' ', "Opening data connection");
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpd_response failed: %d\n", ret);
|
2012-02-04 22:02:45 +01:00
|
|
|
goto errout_with_session;
|
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
for (; ; )
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2012-02-16 00:37:37 +01:00
|
|
|
/* Read from the source (file or TCP connection) */
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
if (session->type == FTPD_SESSIONTYPE_A)
|
|
|
|
{
|
|
|
|
buffer = &session->data.buffer[session->data.buflen >> 2];
|
|
|
|
wantsize = session->data.buflen >> 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer = session->data.buffer;
|
|
|
|
wantsize = session->data.buflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmdtype == 0)
|
|
|
|
{
|
2021-01-13 19:01:00 +01:00
|
|
|
/* Read from the file. */
|
2012-02-16 00:37:37 +01:00
|
|
|
|
2012-02-15 23:41:17 +01:00
|
|
|
rdbytes = read(session->fd, session->data.buffer, wantsize);
|
2012-02-16 00:37:37 +01:00
|
|
|
if (rdbytes < 0)
|
|
|
|
{
|
|
|
|
errval = errno;
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
/* Read from the TCP connection, ftpd_recve returns the negated
|
|
|
|
* error condition.
|
2012-02-16 00:37:37 +01:00
|
|
|
*/
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
rdbytes = ftpd_recv(session->data.sd, session->data.buffer,
|
|
|
|
wantsize, session->rxtimeout);
|
2012-02-16 00:37:37 +01:00
|
|
|
if (rdbytes < 0)
|
|
|
|
{
|
|
|
|
errval = -rdbytes;
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* A negative value of rdbytes indicates a read error. errval has the
|
2012-02-16 00:37:37 +01:00
|
|
|
* (positive) error code associated with the failure.
|
|
|
|
*/
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
if (rdbytes < 0)
|
|
|
|
{
|
2022-10-04 11:45:20 +02:00
|
|
|
nerr("ERROR: Read failed: rdbytes=%zu errval=%d\n",
|
2020-05-22 09:53:36 +02:00
|
|
|
rdbytes, errval);
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ', "Data read error !");
|
2012-02-16 00:37:37 +01:00
|
|
|
ret = -errval;
|
2012-02-04 22:02:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-02-16 00:37:37 +01:00
|
|
|
/* A value of rdbytes == 0 means that we have read the entire source
|
|
|
|
* stream.
|
|
|
|
*/
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
if (rdbytes == 0)
|
|
|
|
{
|
2012-02-16 00:37:37 +01:00
|
|
|
/* End-of-file */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 226, ' ', "Transfer complete");
|
2012-02-16 00:37:37 +01:00
|
|
|
|
|
|
|
/* Return success */
|
|
|
|
|
|
|
|
ret = 0;
|
2012-02-04 22:02:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-02-16 00:37:37 +01:00
|
|
|
/* Write to the destination (file or TCP connection) */
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
if (session->type == FTPD_SESSIONTYPE_A)
|
|
|
|
{
|
|
|
|
/* Change to ascii */
|
|
|
|
|
|
|
|
size_t offset = 0;
|
|
|
|
buflen = 0;
|
|
|
|
while (offset < ((size_t)rdbytes))
|
|
|
|
{
|
|
|
|
if (session->data.buffer[offset] == '\n')
|
|
|
|
{
|
|
|
|
buffer[buflen++] = '\r';
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
buffer[buflen++] = session->data.buffer[offset++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer = session->data.buffer;
|
|
|
|
buflen = (size_t)rdbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmdtype == 0)
|
|
|
|
{
|
2012-02-16 00:37:37 +01:00
|
|
|
/* Write to the TCP connection */
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
wrbytes = ftpd_send(session->data.sd, buffer, buflen,
|
|
|
|
session->txtimeout);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (wrbytes < 0)
|
|
|
|
{
|
2012-02-16 00:37:37 +01:00
|
|
|
errval = -wrbytes;
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpd_send failed: %d\n", errval);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
int remaining;
|
|
|
|
int nwritten;
|
|
|
|
FAR char *next;
|
|
|
|
|
|
|
|
remaining = buflen;
|
|
|
|
next = buffer;
|
|
|
|
|
2012-02-16 00:37:37 +01:00
|
|
|
/* Write to the file */
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
do
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
nwritten = write(session->fd, next, remaining);
|
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
errval = errno;
|
|
|
|
nerr("ERROR: write() failed: %d\n", errval);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining -= nwritten;
|
|
|
|
next += nwritten;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2017-11-07 00:41:30 +01:00
|
|
|
while (remaining > 0);
|
|
|
|
|
|
|
|
wrbytes = next - buffer;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2012-02-16 00:37:37 +01:00
|
|
|
/* If the number of bytes returned by the write is not equal to the
|
|
|
|
* number that we wanted to write, then an error (or at least an
|
|
|
|
* unhandled condition) has occurred. errval should should hold
|
|
|
|
* the (positive) error code.
|
|
|
|
*/
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
if (wrbytes != ((ssize_t)buflen))
|
|
|
|
{
|
2022-10-04 11:45:20 +02:00
|
|
|
nerr("ERROR: Write failed: wrbytes=%zu errval=%d\n",
|
2020-01-12 01:32:40 +01:00
|
|
|
wrbytes, errval);
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ', "Data send error !");
|
2022-04-03 14:17:20 +02:00
|
|
|
ret = -errval;
|
|
|
|
break;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
errout_with_session:;
|
|
|
|
close(session->fd);
|
|
|
|
session->fd = -1;
|
|
|
|
|
|
|
|
if (isnew && ret < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
unlink(path);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_data:;
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
errout_with_path:
|
|
|
|
free(abspath);
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_listoption
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint8_t ftpd_listoption(FAR char **param)
|
|
|
|
{
|
|
|
|
FAR char *ptr = *param;
|
|
|
|
uint8_t ret = 0;
|
|
|
|
|
|
|
|
while (*ptr == '-')
|
|
|
|
{
|
|
|
|
while (*ptr != '\0' && !isspace(*ptr))
|
|
|
|
{
|
|
|
|
switch (*ptr)
|
|
|
|
{
|
|
|
|
case 'a':
|
|
|
|
case 'A':
|
|
|
|
ret |= FTPD_LISTOPTION_A;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
ret |= FTPD_LISTOPTION_L;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
|
|
|
ret |= FTPD_LISTOPTION_F;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
case 'R':
|
|
|
|
ret |= FTPD_LISTOPTION_R;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret |= FTPD_LISTOPTION_UNKNOWN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*ptr != '\0')
|
|
|
|
{
|
|
|
|
while (*ptr != '\0' && isspace(*ptr))
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*param = ptr;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fptd_listscan
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-22 09:53:36 +02:00
|
|
|
static int ftpd_listbuffer(FAR struct ftpd_session_s *session,
|
|
|
|
FAR char *path,
|
2012-02-05 18:36:13 +01:00
|
|
|
FAR struct stat *st, FAR char *buffer,
|
|
|
|
size_t buflen, unsigned int opton)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2022-09-03 13:32:49 +02:00
|
|
|
UNUSED(session);
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
FAR char *name;
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
name = basename(path);
|
|
|
|
|
|
|
|
if ((opton & FTPD_LISTOPTION_L) != 0)
|
|
|
|
{
|
|
|
|
FAR const char *str;
|
|
|
|
struct tm tm;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
if (S_ISREG(st->st_mode) != 0)
|
|
|
|
{
|
|
|
|
str = "-";
|
|
|
|
}
|
|
|
|
else if (S_ISDIR(st->st_mode) != 0)
|
|
|
|
{
|
|
|
|
str = "d";
|
|
|
|
}
|
|
|
|
else if (S_ISCHR(st->st_mode) != 0)
|
|
|
|
{
|
|
|
|
str = "c";
|
|
|
|
}
|
|
|
|
else if (S_ISBLK(st->st_mode) != 0)
|
|
|
|
{
|
|
|
|
str = "b";
|
|
|
|
}
|
|
|
|
else if (S_ISFIFO(st->st_mode) != 0)
|
|
|
|
{
|
|
|
|
str = "p";
|
|
|
|
}
|
|
|
|
else if (S_ISLNK(st->st_mode) != 0)
|
|
|
|
{
|
|
|
|
str = "l";
|
|
|
|
}
|
|
|
|
else if (S_ISSOCK(st->st_mode) != 0)
|
|
|
|
{
|
|
|
|
str = "s";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = "-";
|
|
|
|
}
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%s", str);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
/* User */
|
|
|
|
|
|
|
|
str = ((st->st_mode & S_IRUSR) != 0) ? "r" : "-";
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%s", str);
|
|
|
|
|
|
|
|
str = ((st->st_mode & S_IWUSR) != 0) ? "w" : "-";
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%s", str);
|
|
|
|
|
|
|
|
if ((st->st_mode & S_ISUID) != 0 && (st->st_mode & S_IXUSR) != 0)
|
|
|
|
{
|
|
|
|
str = "s";
|
|
|
|
}
|
|
|
|
else if ((st->st_mode & S_ISUID) != 0)
|
|
|
|
{
|
|
|
|
str = "S";
|
|
|
|
}
|
|
|
|
else if ((st->st_mode & S_IXUSR) != 0)
|
|
|
|
{
|
|
|
|
str = "x";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = "-";
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%s", str);
|
|
|
|
|
|
|
|
/* group */
|
|
|
|
|
|
|
|
str = ((st->st_mode & S_IRGRP) != 0) ? "r" : "-";
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%s", str);
|
|
|
|
|
|
|
|
str = ((st->st_mode & S_IWGRP) != 0) ? "w" : "-";
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%s", str);
|
|
|
|
|
|
|
|
if ((st->st_mode & S_ISGID) != 0 && (st->st_mode & S_IXGRP) != 0)
|
|
|
|
{
|
|
|
|
str = "s";
|
|
|
|
}
|
|
|
|
else if ((st->st_mode & S_ISGID) != 0)
|
|
|
|
{
|
|
|
|
str = "S";
|
|
|
|
}
|
|
|
|
else if ((st->st_mode & S_IXGRP) != 0)
|
|
|
|
{
|
|
|
|
str = "x";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = "-";
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%s", str);
|
|
|
|
|
|
|
|
/* other */
|
|
|
|
|
|
|
|
str = ((st->st_mode & S_IROTH) != 0) ? "r" : "-";
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%s", str);
|
|
|
|
|
|
|
|
str = ((st->st_mode & S_IWOTH) != 0) ? "w" : "-";
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%s", str);
|
|
|
|
|
|
|
|
if ((st->st_mode & S_ISVTX) != 0 && (st->st_mode & S_IXOTH) != 0)
|
|
|
|
{
|
|
|
|
str = "t";
|
|
|
|
}
|
|
|
|
else if ((st->st_mode & S_ISVTX) != 0)
|
|
|
|
{
|
|
|
|
str = "T";
|
|
|
|
}
|
|
|
|
else if ((st->st_mode & S_IXOTH) != 0)
|
|
|
|
{
|
|
|
|
str = "x";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = "-";
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%s", str);
|
|
|
|
|
2022-02-14 04:37:30 +01:00
|
|
|
#ifdef __NuttX__
|
2012-02-05 23:18:14 +01:00
|
|
|
/* Fake nlink, user id, and group id */
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%4u %8u %8u",
|
|
|
|
1, 1001, 512);
|
2012-02-05 18:36:13 +01:00
|
|
|
#else
|
2012-02-05 23:18:14 +01:00
|
|
|
/* nlink */
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%4u",
|
|
|
|
st->st_nlink);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2012-02-05 23:18:14 +01:00
|
|
|
/* user id */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
offset += snprintf(&buffer[offset], buflen - offset,
|
|
|
|
" %8u", st->st_uid);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2012-02-05 23:18:14 +01:00
|
|
|
/* group id */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, " %8u",
|
|
|
|
st->st_gid);
|
2012-02-05 18:36:13 +01:00
|
|
|
#endif
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
/* size */
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
offset += snprintf(&buffer[offset], buflen - offset,
|
2020-11-13 08:54:34 +01:00
|
|
|
" %8ju", (uintmax_t)st->st_size);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
/* time */
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
memcpy(&tm, localtime((FAR const time_t *)&st->st_mtime), sizeof(tm));
|
2012-02-04 22:02:45 +01:00
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, " %s %2u",
|
|
|
|
g_monthtab[tm.tm_mon], tm.tm_mday);
|
|
|
|
now = time(0);
|
2012-02-05 18:36:13 +01:00
|
|
|
if ((now - st->st_mtime) > (time_t)(60 * 60 * 24 * 180))
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, " %5u",
|
|
|
|
tm.tm_year + 1900);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, " %02u:%02u",
|
|
|
|
tm.tm_hour, tm.tm_min);
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
/* basename */
|
|
|
|
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, " %s", name);
|
|
|
|
|
|
|
|
/* linkname */
|
|
|
|
|
2022-02-14 04:37:30 +01:00
|
|
|
#ifndef __NuttX__
|
2012-02-04 22:02:45 +01:00
|
|
|
if (S_ISLNK(st->st_mode) != 0)
|
|
|
|
{
|
|
|
|
FAR char *temp;
|
|
|
|
int namelen;
|
|
|
|
|
|
|
|
temp = (FAR char *)malloc(PATH_MAX + 1);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (temp != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
namelen = readlink(path, temp, PATH_MAX);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (namelen != -1)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
temp[namelen] = '\0';
|
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
offset += snprintf(&buffer[offset], buflen - offset,
|
|
|
|
" -> %s", temp);
|
2012-02-04 22:02:45 +01:00
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
}
|
2012-02-05 18:36:13 +01:00
|
|
|
#endif
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
/* end */
|
|
|
|
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "\r\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* basename */
|
|
|
|
|
|
|
|
offset += snprintf(&buffer[offset], buflen - offset, "%s\r\n", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fptd_listscan
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int fptd_listscan(FAR struct ftpd_session_s *session, FAR char *path,
|
|
|
|
unsigned int opton)
|
|
|
|
{
|
|
|
|
FAR char *temp;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
struct stat st;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = stat(path, &st);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISDIR(st.st_mode))
|
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
ret = ftpd_listbuffer(session, path, &st, session->data.buffer,
|
2014-04-14 00:24:28 +02:00
|
|
|
session->data.buflen, opton);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->data.sd, session->txtimeout,
|
2012-02-05 18:36:13 +01:00
|
|
|
"%s", (FAR char *)session->data.buffer);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir = opendir(path);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (dir == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
int errval = errno;
|
2021-01-13 19:01:00 +01:00
|
|
|
nerr("ERROR: opendir() failed: %d\n", errval);
|
2012-02-04 22:02:45 +01:00
|
|
|
return -errval;
|
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
for (; ; )
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
entry = readdir(dir);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (entry == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->d_name[0] == '.')
|
|
|
|
{
|
|
|
|
if ((opton & FTPD_LISTOPTION_A) == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2023-04-12 17:42:28 +02:00
|
|
|
ret = asprintf(&temp, "%s/%s", path, entry->d_name);
|
|
|
|
if (ret < 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = stat(temp, &st);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
free(temp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ftpd_listbuffer(session, temp, &st, session->data.buffer,
|
2014-04-14 00:24:28 +02:00
|
|
|
session->data.buflen, opton);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->data.sd, session->txtimeout,
|
|
|
|
"%s", session->data.buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(temp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
closedir(dir);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_list
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_list(FAR struct ftpd_session_s *session, unsigned int opton)
|
|
|
|
{
|
|
|
|
FAR char *abspath;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ftpd_getpath(session, session->param, &abspath, NULL);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
ret = fptd_listscan(session, abspath, opton);
|
|
|
|
free(abspath);
|
|
|
|
}
|
|
|
|
|
2014-11-25 22:26:30 +01:00
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_user
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_user(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2014-04-14 00:24:28 +02:00
|
|
|
/* Clear session status */
|
2012-02-05 23:46:56 +01:00
|
|
|
|
|
|
|
session->flags = 0;
|
|
|
|
session->restartpos = 0;
|
|
|
|
|
|
|
|
/* Free session strings */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->user != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(session->user);
|
2012-02-05 23:46:56 +01:00
|
|
|
session->user = NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->renamefrom != NULL)
|
2012-02-05 23:46:56 +01:00
|
|
|
{
|
|
|
|
free(session->renamefrom);
|
|
|
|
session->renamefrom = NULL;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2012-02-05 23:46:56 +01:00
|
|
|
/* Set up the new user */
|
2012-02-05 23:18:14 +01:00
|
|
|
|
|
|
|
session->user = strdup(session->param);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->user == NULL)
|
2012-02-05 23:18:14 +01:00
|
|
|
{
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 451, ' ', "Memory exhausted !");
|
2012-02-05 23:18:14 +01:00
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-05 23:18:14 +01:00
|
|
|
session->flags |= FTPD_SESSIONFLAG_USER;
|
|
|
|
|
|
|
|
/* If there is no account information, then no login is required. */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->head == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
FAR char *home;
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
home = getenv("HOME");
|
|
|
|
session->loggedin = false;
|
|
|
|
session->home = strdup(home == NULL ? "/" : home);
|
|
|
|
session->work = strdup("/");
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 230, ' ', "Login successful.");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
/* Try to login with no password. This will work if no password is
|
2012-02-05 23:18:14 +01:00
|
|
|
* required for the account.
|
|
|
|
*/
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
session->loggedin = ftpd_account_login(session, session->param, NULL);
|
|
|
|
if (session->loggedin)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 230, ' ', "Login successful.");
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
session->loggedin = false;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-05 23:18:14 +01:00
|
|
|
/* A password is required */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt2, 331, ' ', "Password required for ",
|
2012-02-04 22:02:45 +01:00
|
|
|
session->user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_pass
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_pass(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->user == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 530, ' ', "Please login with USER !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
session->loggedin = ftpd_account_login(session, session->user,
|
|
|
|
session->param);
|
|
|
|
if (session->loggedin)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 230, ' ', "Login successful.");
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
session->loggedin = false;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 530, ' ', "Login incorrect !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_syst
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_syst(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 215, ' ', "UNIX Type: L8");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_type
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_type(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
size_t parmlen = strlen(session->param);
|
|
|
|
|
|
|
|
if (parmlen == 1)
|
|
|
|
{
|
|
|
|
switch (toupper(session->param[0]))
|
|
|
|
{
|
|
|
|
case 'A':
|
|
|
|
{
|
|
|
|
session->type = FTPD_SESSIONTYPE_A;
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 200, ' ', "Type set to A");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'I':
|
|
|
|
{
|
|
|
|
session->type = FTPD_SESSIONTYPE_I;
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 200, ' ', "Type set to I");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'L':
|
|
|
|
{
|
|
|
|
session->type = FTPD_SESSIONTYPE_L8;
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 200, ' ',
|
2012-02-04 22:02:45 +01:00
|
|
|
"Type set to L (byte size 8)");
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
session->type = FTPD_SESSIONTYPE_NONE;
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 501, ' ', "Type unknown !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (parmlen == 3)
|
|
|
|
{
|
|
|
|
if (toupper(session->param[0]) == 'L' && session->param[1] == ' ')
|
|
|
|
{
|
|
|
|
if (session->param[2] == '8')
|
|
|
|
{
|
|
|
|
session->type = FTPD_SESSIONTYPE_L8;
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2020-01-12 01:32:40 +01:00
|
|
|
g_respfmt1, 200, ' ',
|
|
|
|
"Type set to L 8");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->type = FTPD_SESSIONTYPE_NONE;
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2020-01-12 01:32:40 +01:00
|
|
|
g_respfmt1, 504, ' ',
|
|
|
|
"Byte size must be 8 !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
session->type = FTPD_SESSIONTYPE_NONE;
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 500, ' ', "TYPE not understood !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_mode
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_mode(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 502, ' ',
|
2012-02-04 22:02:45 +01:00
|
|
|
"MODE command not implemented !");
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_abor
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_abor(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 426, ' ',
|
2012-02-04 22:02:45 +01:00
|
|
|
"Transfer aborted. Data connection closed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_quit
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_quit(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 221, ' ', "Good-bye");
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2012-02-05 23:46:56 +01:00
|
|
|
/* Return a negative value to force the server to disconnect */
|
|
|
|
|
|
|
|
return -1;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_noop
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_noop(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 200, ' ',
|
2012-02-04 22:02:45 +01:00
|
|
|
"NOOP command successful");
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_port
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_port(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2012-02-04 22:02:45 +01:00
|
|
|
uint8_t value[6];
|
|
|
|
unsigned int utemp;
|
|
|
|
int temp;
|
|
|
|
FAR char *str;
|
|
|
|
int index;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
while (index < 6)
|
|
|
|
{
|
|
|
|
/* Get the next value from the comma-delimited string */
|
|
|
|
|
|
|
|
str = ftpd_strtok(true, ",", &session->param);
|
|
|
|
if (*str == '\0')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ftpd_strtok differs from the real strtok in that it does not NUL-
|
|
|
|
* terminate the strings.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (session->param[0] != '\0')
|
|
|
|
{
|
|
|
|
session->param[0] = '\0';
|
|
|
|
session->param++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next value from the list */
|
|
|
|
|
|
|
|
temp = atoi(str);
|
|
|
|
if (temp < 0 || temp > 255)
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 501, ' ',
|
2012-02-04 22:02:45 +01:00
|
|
|
"Illegal PORT command");
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpd_response failed: %d\n", ret);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
value[index++] = (uint8_t)temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index < 6)
|
|
|
|
{
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 501, ' ', "Illegal PORT command");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
#if 1 /* Follow param */
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
memset(&session->data.addr, 0, sizeof(session->data.addr));
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
session->data.addr.in4.sin_family = AF_INET;
|
|
|
|
|
|
|
|
utemp = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | (value[3]);
|
|
|
|
session->data.addr.in4.sin_addr.s_addr = htonl((long)utemp);
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
utemp = (value[4] << 8) | (value[5]);
|
|
|
|
session->data.addr.in4.sin_port = htons((short)utemp);
|
|
|
|
|
|
|
|
#else /* Follow command socket address */
|
|
|
|
|
|
|
|
session->data.addrlen = sizeof(session->data.addr);
|
|
|
|
ret = getpeername(session->cmd.sd, (struct sockaddr *)&session->data.addr,
|
|
|
|
&session->data.addrlen);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
if (session->data.addr.ss.ss_family != AF_INET)
|
|
|
|
{
|
|
|
|
memset(&session->data.addr, 0, sizeof(session->data.addr));
|
|
|
|
|
|
|
|
session->data.addr.in4.sin_family = AF_INET;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
utemp = (value[0] << 24) | (value[1] << 16) |
|
|
|
|
(value[2] << 8) | (value[3]);
|
2012-02-04 22:02:45 +01:00
|
|
|
session->data.addr.in4.sin_addr.s_addr = htonl(utemp);
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
utemp = (value[4] << 8) | (value[5]);
|
|
|
|
session->data.addr.in4.sin_port = htons(utemp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(&session->data.addr, 0, sizeof(session->data.addr));
|
|
|
|
|
|
|
|
session->data.addr.in4.sin_family = AF_INET;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
utemp = (value[0] << 24) | (value[1] << 16) |
|
|
|
|
(value[2] << 8) | (value[3]);
|
2012-02-04 22:02:45 +01:00
|
|
|
session->data.addr.in4.sin_addr.s_addr = htonl(utemp);
|
|
|
|
}
|
|
|
|
|
|
|
|
utemp = (value[4] << 8) | (value[5]);
|
|
|
|
session->data.addr.in4.sin_port = htons(utemp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 200, ' ',
|
2012-02-04 22:02:45 +01:00
|
|
|
"PORT command successful");
|
2017-11-07 00:41:30 +01:00
|
|
|
#else
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 502, ' ',
|
|
|
|
"PORT command not implemented");
|
|
|
|
#endif
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_eprt
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_eprt(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
FAR const char *str;
|
|
|
|
FAR char *field[3];
|
|
|
|
sa_family_t family;
|
|
|
|
size_t left;
|
|
|
|
size_t right;
|
|
|
|
int count;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
left = 0;
|
|
|
|
right = strlen(session->param);
|
|
|
|
|
2014-11-25 22:26:30 +01:00
|
|
|
if (right < 1)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
/* no message ? */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 502, ' ',
|
|
|
|
"EPRT command not implemented !");
|
2012-02-04 22:02:45 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
right--;
|
|
|
|
|
|
|
|
while (session->param[left] != '\0')
|
|
|
|
{
|
|
|
|
if (session->param[left] == '|')
|
|
|
|
{
|
|
|
|
left++;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
left++;
|
|
|
|
}
|
|
|
|
|
2014-11-25 22:26:30 +01:00
|
|
|
if (right < 1 || left > right)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
/* Invalid format */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 502, ' ',
|
|
|
|
"EPRT command not implemented !");
|
2012-02-04 22:02:45 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
count = 3;
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
|
|
|
field[index] = NULL;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
str = (FAR const char *)&session->param[left];
|
|
|
|
for (index = 0; index < count && *str != '\0'; index++)
|
|
|
|
{
|
|
|
|
field[index] = ftpd_strtok_alloc(true, ",|)", &str);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (field[index] == NULL)
|
2020-01-12 01:32:40 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
if (*str != '\0')
|
|
|
|
{
|
|
|
|
str++;
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (index < count)
|
|
|
|
{
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
if (field[index] != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(field[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
2022-03-28 13:46:07 +02:00
|
|
|
g_respfmt1, 502, ' ', "EPRT command not implemented !");
|
2020-01-12 01:32:40 +01:00
|
|
|
return -EINVAL;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
memset(&session->data.addr, 0, sizeof(session->data.addr));
|
|
|
|
family = atoi(field[0]);
|
|
|
|
#ifndef CONFIG_NET_IPv6
|
|
|
|
if (family == 1)
|
|
|
|
{
|
|
|
|
family = AF_INET;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
session->data.addr.in4.sin_family = family;
|
2020-01-02 13:09:50 +01:00
|
|
|
inet_pton(family, field[1], &session->data.addr.in4.sin_addr);
|
2012-02-04 22:02:45 +01:00
|
|
|
session->data.addr.in4.sin_port = htons((short)atoi(field[2]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (family == 2)
|
|
|
|
{
|
|
|
|
family = AF_INET6;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
session->data.addr.in6.sin6_family = family;
|
2020-01-02 13:09:50 +01:00
|
|
|
inet_pton(family, field[1], &session->data.addr.in6.sin6_addr);
|
2012-02-04 22:02:45 +01:00
|
|
|
session->data.addr.in6.sin6_port = htons((short)atoi(field[2]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Unrecognized family: %d\n", family);
|
2012-02-04 22:02:45 +01:00
|
|
|
family = AF_UNSPEC;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
for (index = 0; index < count; index++)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
if (field[index] != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(field[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (family == AF_UNSPEC)
|
|
|
|
{
|
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
2022-03-28 13:46:07 +02:00
|
|
|
g_respfmt1, 502, ' ', "EPRT command not implemented !");
|
2012-02-04 22:02:45 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 200, ' ', "EPRT command successful");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_pwd
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_pwd(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
FAR const char *workpath;
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
workpath = session->work == NULL ? "" : session->work;
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
"%03u%c\"%s\" is current directory.\r\n",
|
|
|
|
257, ' ', workpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_cwd
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_cwd(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
return ftpd_changedir(session, session->param);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_cdup
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_cdup(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
return ftpd_changedir(session, g_cdup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_rmd
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_rmd(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
FAR char *abspath;
|
|
|
|
FAR char *workpath;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ftpd_getpath(session, session->param, &abspath, &workpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ',
|
|
|
|
"Can not remove directory !");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(session->home, abspath) == 0)
|
|
|
|
{
|
|
|
|
free(abspath);
|
|
|
|
free(workpath);
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ',
|
|
|
|
"Can not remove home directory !");
|
|
|
|
return -EINVAL;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(session->work, workpath) == 0)
|
|
|
|
{
|
|
|
|
free(abspath);
|
|
|
|
free(workpath);
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ',
|
|
|
|
"Can not remove current directory !");
|
|
|
|
return -EINVAL;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
ret = rmdir(abspath);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = -errno;
|
2012-02-04 22:02:45 +01:00
|
|
|
free(abspath);
|
|
|
|
free(workpath);
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ',
|
|
|
|
"Can not remove directory !");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
free(abspath);
|
|
|
|
free(workpath);
|
|
|
|
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 250, ' ',
|
2012-02-04 22:02:45 +01:00
|
|
|
"RMD command successful");
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_mkd
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_mkd(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
FAR char *abspath;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ftpd_getpath(session, session->param, &abspath, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ',
|
|
|
|
"Can not make directory !");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = mkdir(abspath, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
|
|
|
|
S_IXGRP | S_IROTH | S_IXOTH);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = -errno;
|
2012-02-04 22:02:45 +01:00
|
|
|
free(abspath);
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ', "Can not make directory !");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
free(abspath);
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 250, ' ', "MKD command successful");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_dele
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_dele(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
FAR char *abspath;
|
|
|
|
FAR char *workpath;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ftpd_getpath(session, session->param, &abspath, &workpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ', "Can not delete file !");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(session->home, abspath) == 0)
|
|
|
|
{
|
|
|
|
free(abspath);
|
|
|
|
free(workpath);
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ',
|
|
|
|
"Can not delete home directory !");
|
|
|
|
return -EINVAL;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(session->work, workpath) == 0)
|
|
|
|
{
|
|
|
|
free(abspath);
|
|
|
|
free(workpath);
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ',
|
|
|
|
"Can not delete current directory !");
|
|
|
|
return -EINVAL;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
ret = unlink(abspath);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = -errno;
|
2012-02-04 22:02:45 +01:00
|
|
|
free(abspath);
|
|
|
|
free(workpath);
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ', "Can not delete file !");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
free(abspath);
|
|
|
|
free(workpath);
|
|
|
|
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 250, ' ', "DELE command successful");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_pasv
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_pasv(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2012-02-04 22:02:45 +01:00
|
|
|
unsigned int value[6];
|
|
|
|
unsigned int temp;
|
2012-02-05 18:36:13 +01:00
|
|
|
int ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
session->data.addrlen = sizeof(session->data.addr);
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
session->data.sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (session->data.sd < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2020-01-12 01:32:40 +01:00
|
|
|
g_respfmt1, 425, ' ',
|
|
|
|
"PASV socket create fail !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = getsockname(session->cmd.sd,
|
|
|
|
(FAR struct sockaddr *)&session->data.addr,
|
2012-02-04 22:02:45 +01:00
|
|
|
&session->data.addrlen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 425, ' ', "PASV getsockname fail !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2020-01-12 01:32:40 +01:00
|
|
|
if (session->data.addr.ss.ss_family == AF_INET6)
|
|
|
|
{
|
|
|
|
/* Convert ipv6 to ipv4 */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
if ((IN6_IS_ADDR_V4MAPPED(&session->data.addr.in6.sin6_addr) != 0) ||
|
|
|
|
(IN6_IS_ADDR_V4COMPAT(&session->data.addr.in6.sin6_addr) != 0))
|
|
|
|
{
|
|
|
|
/* convert ipv6 to ipv4 */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2021-01-13 19:00:25 +01:00
|
|
|
struct in_addr in4addr;
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
in4addr.s_addr = session->data.addr.in6.sin6_addr.s6_addr32[3];
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
memset(&session->data.addr, 0, sizeof(session->data.addr));
|
|
|
|
session->data.addr.in4.sin_family = AF_INET;
|
|
|
|
session->data.addr.in4.sin_addr.s_addr = in4addr.s_addr;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
session->data.addr.in4.sin_port = 0;
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = bind(session->data.sd,
|
|
|
|
(FAR const struct sockaddr *)&session->data.addr,
|
2012-02-05 18:36:13 +01:00
|
|
|
session->data.addrlen);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 425, ' ', "PASV bind fail !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = getsockname(session->data.sd,
|
|
|
|
(FAR struct sockaddr *)&session->data.addr,
|
2012-02-04 22:02:45 +01:00
|
|
|
&session->data.addrlen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 425, ' ', "PASV getsockname fail !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2014-04-14 00:24:28 +02:00
|
|
|
ret = listen(session->data.sd, 1);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 425, ' ', "PASV listen fail !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ntohl(session->data.addr.in4.sin_addr.s_addr) == INADDR_ANY)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 425, ' ',
|
2012-02-04 22:02:45 +01:00
|
|
|
"Can not open passive connection");
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = ntohl(session->data.addr.in4.sin_addr.s_addr);
|
|
|
|
value[0] = (temp >> 24) & 0xff;
|
|
|
|
value[1] = (temp >> 16) & 0xff;
|
|
|
|
value[2] = (temp >> 8) & 0xff;
|
|
|
|
value[3] = (temp) & 0xff;
|
|
|
|
|
|
|
|
temp = (unsigned int)ntohs(session->data.addr.in4.sin_port);
|
|
|
|
value[4] = (temp >> 8) & 0xff;
|
|
|
|
value[5] = (temp) & 0xff;
|
|
|
|
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
"%03u%cEntering passive mode (%u,%u,%u,%u,%u,%u).\r\n",
|
|
|
|
227, ' ',
|
|
|
|
value[0], value[1], value[2],
|
|
|
|
value[3], value[4], value[5]);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2017-11-07 00:41:30 +01:00
|
|
|
#else
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 502, ' ',
|
|
|
|
"PASV command not implemented");
|
|
|
|
#endif
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_epsv
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
static int ftpd_command_epsv(FAR struct ftpd_session_s *session)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
int ret;
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
session->data.addrlen = sizeof(session->data.addr);
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2012-02-05 18:36:13 +01:00
|
|
|
session->data.sd = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (session->data.sd < 0)
|
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2012-02-05 18:36:13 +01:00
|
|
|
session->data.sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2017-11-07 00:41:30 +01:00
|
|
|
#endif
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-14 00:24:28 +02:00
|
|
|
#if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
|
2012-02-04 22:02:45 +01:00
|
|
|
int ipv6only = 0;
|
2020-01-12 01:32:40 +01:00
|
|
|
setsockopt(session->data.sd, IPPROTO_IPV6, IPV6_V6ONLY,
|
|
|
|
&ipv6only, sizeof(ipv6only));
|
2014-04-14 00:24:28 +02:00
|
|
|
#endif
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
#else
|
2012-02-05 18:36:13 +01:00
|
|
|
session->data.sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2012-02-04 22:02:45 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (session->data.sd < 0)
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 500, ' ', "EPSV socket create fail !");
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = getsockname(session->cmd.sd,
|
|
|
|
(FAR struct sockaddr *)&session->data.addr,
|
|
|
|
&session->data.addrlen);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 500, ' ', "EPSV getsockname fail !");
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (session->data.addr.ss.ss_family == AF_INET6)
|
|
|
|
{
|
|
|
|
session->data.addr.in6.sin6_port = htons(0);
|
|
|
|
}
|
2017-11-07 00:41:30 +01:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (session->data.addr.ss.ss_family == AF_INET)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
session->data.addr.in4.sin_port = htons(0);
|
|
|
|
}
|
2017-11-07 00:41:30 +01:00
|
|
|
else
|
2014-04-14 00:24:28 +02:00
|
|
|
#endif
|
2017-11-07 00:41:30 +01:00
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2020-05-22 09:53:36 +02:00
|
|
|
g_respfmt1, 500, ' ',
|
|
|
|
"EPSV family not supported!");
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2017-11-07 00:41:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = bind(session->data.sd,
|
|
|
|
(FAR const struct sockaddr *)&session->data.addr,
|
2012-02-04 22:02:45 +01:00
|
|
|
session->data.addrlen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 500, ' ', "EPSV bind fail !");
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = getsockname(session->data.sd,
|
|
|
|
(FAR struct sockaddr *)&session->data.addr,
|
2012-02-04 22:02:45 +01:00
|
|
|
&session->data.addrlen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 500, ' ', "EPSV getsockname fail !");
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = listen(session->data.sd, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2022-03-28 13:46:07 +02:00
|
|
|
g_respfmt1, 500, ' ', "EPSV listen fail !");
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (session->data.addr.ss.ss_family == AF_INET6)
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2020-01-12 01:32:40 +01:00
|
|
|
"%03u%cEntering Extended Passive Mode "
|
|
|
|
"(|||%u|).\r\n",
|
2012-02-04 22:02:45 +01:00
|
|
|
229, ' ',
|
|
|
|
ntohs(session->data.addr.in6.sin6_port));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#else
|
|
|
|
if (session->data.addr.ss.ss_family == AF_INET)
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2020-01-12 01:32:40 +01:00
|
|
|
"%03u%cEntering Extended Passive Mode "
|
2020-05-22 09:35:11 +02:00
|
|
|
"(|||%u|).\r\n",
|
|
|
|
229, ' ',
|
2012-02-04 22:02:45 +01:00
|
|
|
ntohs(session->data.addr.in4.sin_port));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 502, ' ',
|
2012-02-05 18:36:13 +01:00
|
|
|
"EPSV command not implemented !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2012-02-05 18:36:13 +01:00
|
|
|
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_list
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_list(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
uint8_t opton = FTPD_LISTOPTION_L;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ftpd_dataopen(session);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 150, ' ',
|
2012-02-04 22:02:45 +01:00
|
|
|
"Opening ASCII mode data connection for file list");
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
opton |= ftpd_listoption((char **)(&session->param));
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_list(session, opton);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 226, ' ', "Transfer complete");
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_nlst
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_nlst(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
uint8_t opton = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ftpd_dataopen(session);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 150, ' ',
|
2012-02-04 22:02:45 +01:00
|
|
|
"Opening ASCII mode data connection for file list");
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
opton |= ftpd_listoption((char **)(&session->param));
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_list(session, opton);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 226, ' ', "Transfer complete");
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_acct
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_acct(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2020-01-12 01:32:40 +01:00
|
|
|
g_respfmt1, 502, ' ',
|
|
|
|
"ACCT command not implemented !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_size
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_size(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
FAR char *abspath;
|
|
|
|
FAR char *path;
|
|
|
|
struct stat st;
|
|
|
|
FAR FILE *outstream;
|
|
|
|
off_t offset;
|
|
|
|
int ch;
|
2012-02-05 18:36:13 +01:00
|
|
|
int status;
|
2012-02-04 22:02:45 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ftpd_getpath(session, session->param, &abspath, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ', "Unknown size !");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
path = abspath;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
ret = 0;
|
|
|
|
switch (session->type)
|
|
|
|
{
|
|
|
|
case FTPD_SESSIONTYPE_NONE:
|
|
|
|
case FTPD_SESSIONTYPE_L8:
|
|
|
|
case FTPD_SESSIONTYPE_I:
|
|
|
|
{
|
|
|
|
status = stat(path, &st);
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt2, 550, ' ', session->param,
|
2012-02-04 22:02:45 +01:00
|
|
|
": not a regular file.");
|
|
|
|
}
|
|
|
|
else if (!S_ISREG(st.st_mode))
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt2, 550, ' ', session->param,
|
2012-02-04 22:02:45 +01:00
|
|
|
": not a regular file.");
|
|
|
|
}
|
|
|
|
else
|
2014-04-14 00:24:28 +02:00
|
|
|
{
|
2012-02-04 22:02:45 +01:00
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2020-01-12 01:32:40 +01:00
|
|
|
"%03u%c%llu\r\n", 213, ' ',
|
|
|
|
(unsigned long long)st.st_size);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-04-14 00:24:28 +02:00
|
|
|
case FTPD_SESSIONTYPE_A:
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
status = stat(path, &st);
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = -errno;
|
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt2, 550, ' ', session->param,
|
|
|
|
": not a regular file.");
|
|
|
|
goto errout_with_abspath;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
else if (!S_ISREG(st.st_mode))
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = -EPERM;
|
2021-01-13 19:01:00 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt2, 550, ' ', session->param,
|
|
|
|
": not a regular file.");
|
2020-01-12 01:32:40 +01:00
|
|
|
goto errout_with_abspath;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
outstream = fopen(path, "r");
|
2022-03-28 13:46:07 +02:00
|
|
|
if (outstream == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = -errno;
|
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt2, 550, ' ', session->param,
|
|
|
|
": Can not open file !");
|
|
|
|
goto errout_with_abspath;
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
offset = 0;
|
2020-01-12 01:32:40 +01:00
|
|
|
for (; ; )
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ch = getc(outstream);
|
|
|
|
if (ch == EOF)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (ch == 'c')
|
|
|
|
{
|
|
|
|
offset++;
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
fclose(outstream);
|
2012-02-04 22:02:45 +01:00
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2020-01-12 01:32:40 +01:00
|
|
|
"%03u%c%llu\r\n", 213, ' ',
|
|
|
|
(unsigned long long)offset);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2020-01-12 01:32:40 +01:00
|
|
|
g_respfmt1, 504, ' ',
|
|
|
|
"SIZE not implemented for type");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
errout_with_abspath:
|
2012-02-04 22:02:45 +01:00
|
|
|
free(abspath);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_stru
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_stru(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2020-01-12 01:32:40 +01:00
|
|
|
g_respfmt1, 502, ' ',
|
|
|
|
"STRU command not implemented !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-08-13 23:52:59 +02:00
|
|
|
* Name: ftpd_command_rnfr
|
2012-02-04 22:02:45 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_rnfr(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
FAR char *abspath;
|
|
|
|
struct stat st;
|
|
|
|
int ret;
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->renamefrom != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(session->renamefrom);
|
|
|
|
session->renamefrom = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ftpd_getpath(session, session->param, &abspath, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ', "RNFR error !");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = stat(abspath, &st);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
free(abspath);
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt2, 550, ' ', session->param,
|
2012-02-04 22:02:45 +01:00
|
|
|
": No such file or directory.");
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
session->renamefrom = abspath;
|
|
|
|
session->flags |= FTPD_SESSIONFLAG_RENAMEFROM;
|
|
|
|
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 350, ' ', "RNFR successful");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_rnto
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_rnto(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
FAR char *abspath;
|
|
|
|
int ret;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->renamefrom == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2021-01-13 19:01:00 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ', "RNTO error !");
|
|
|
|
return -EINVAL;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = ftpd_getpath(session, session->param, &abspath, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ', "RNTO error !");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
ret = rename(session->renamefrom, abspath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = -errno;
|
2012-02-04 22:02:45 +01:00
|
|
|
free(abspath);
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt2, 550, ' ', session->param,
|
|
|
|
": Rename error.");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(abspath);
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 250, ' ', "Rename successful");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_retr
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_retr(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
return ftpd_stream(session, 0);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_stor
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_stor(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
return ftpd_stream(session, 1);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_appe
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_appe(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
return ftpd_stream(session, 2);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_rest
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_rest(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_HAVE_LONG_LONG
|
2020-01-12 01:32:40 +01:00
|
|
|
session->restartpos = (off_t)atoll(session->param);
|
2014-04-14 00:24:28 +02:00
|
|
|
#else
|
2020-01-12 01:32:40 +01:00
|
|
|
session->restartpos = (off_t)atoi(session->param);
|
2012-02-04 22:02:45 +01:00
|
|
|
#endif
|
2020-01-12 01:32:40 +01:00
|
|
|
session->flags |= FTPD_SESSIONFLAG_RESTARTPOS;
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 320, ' ', "Restart position ready");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_mdtm
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_mdtm(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
FAR char *abspath;
|
|
|
|
FAR char *path;
|
|
|
|
struct stat st;
|
|
|
|
struct tm tm;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ftpd_getpath(session, session->param, &abspath, NULL);
|
2020-01-12 01:32:40 +01:00
|
|
|
if (ret < 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt1, 550, ' ', "Unknown size !");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
path = abspath;
|
|
|
|
|
|
|
|
ret = stat(path, &st);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
ret = -errno;
|
2012-02-04 22:02:45 +01:00
|
|
|
free(abspath);
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt2, 550, ' ', session->param,
|
|
|
|
": not a plain file.");
|
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
if (!S_ISREG(st.st_mode))
|
|
|
|
{
|
|
|
|
free(abspath);
|
|
|
|
ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
g_respfmt2, 550, ' ', session->param,
|
|
|
|
": not a plain file.");
|
|
|
|
return -EISDIR;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
free(abspath);
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
gmtime_r(&st.st_mtime, &tm);
|
2012-02-04 22:02:45 +01:00
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
"%03u%c%04u%02u%02u%02u%02u%02u\r\n", 213, ' ',
|
|
|
|
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
|
|
|
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_opts
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_opts(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
FAR char *str;
|
|
|
|
FAR char *option;
|
|
|
|
FAR char *value;
|
|
|
|
bool remote = false;
|
|
|
|
bool local = false;
|
|
|
|
|
|
|
|
/* token: name and value */
|
|
|
|
|
|
|
|
str = session->param;
|
|
|
|
option = ftpd_strtok(true, " \t", &str);
|
|
|
|
|
|
|
|
/* Unlike the "real" strtok, ftpd_strtok does not NUL-terminate
|
|
|
|
* the returned string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*str != '\0')
|
|
|
|
{
|
|
|
|
*str = '\0';
|
|
|
|
str++;
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
value = str;
|
|
|
|
|
|
|
|
if (strcasecmp(option, "UTF8") == 0 || strcasecmp(option, "UTF-8") == 0)
|
|
|
|
{
|
|
|
|
FAR char *lang;
|
|
|
|
|
|
|
|
if (value[0] == '\0' || strcasecmp(value, "ON") == 0 ||
|
|
|
|
strcasecmp(value, "ENABLE") == 0 || strcasecmp(value, "TRUE") == 0)
|
|
|
|
{
|
2020-01-12 01:32:40 +01:00
|
|
|
remote = true;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
remote = false;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lang = getenv("LANG");
|
2022-03-28 13:46:07 +02:00
|
|
|
if (lang != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
if (strcasestr(lang, "UTF8") || strcasestr(lang, "UTF-8"))
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
local = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
local = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if 1 /* OPTION: UTF-8 is default */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
local = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (remote != local)
|
|
|
|
{
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 504, ' ', "UIF-8 disabled");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 200, ' ', "OK, UTF-8 enabled");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
"%03u%c%s%s%s\r\n", 501, ' ', "OPTS: ", option,
|
|
|
|
" not understood");
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_site
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_site(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2020-01-12 01:32:40 +01:00
|
|
|
g_respfmt1, 502, ' ',
|
|
|
|
"SITE command not implemented !");
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command_help
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command_help(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
index = 0;
|
2022-03-28 13:46:07 +02:00
|
|
|
while (g_ftpdhelp[index] != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2022-03-28 13:46:07 +02:00
|
|
|
if (index == 0 || g_ftpdhelp[index + 1] == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 214,
|
2012-02-05 18:36:13 +01:00
|
|
|
!g_ftpdhelp[index + 1] ? ' ' : '-',
|
2012-02-04 22:02:45 +01:00
|
|
|
g_ftpdhelp[index]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
|
|
|
"%c%s\r\n", ' ', g_ftpdhelp[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_command
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_command(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
/* Search the command table for a matching command */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
for (index = 0; g_ftpdcmdtab[index].command != NULL; index++)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
/* Does the command string match this entry? */
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
if (strcmp(session->command, g_ftpdcmdtab[index].command) == 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
/* Yes.. is a login required to execute this command? */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
if ((g_ftpdcmdtab[index].flags & FTPD_CMDFLAG_LOGIN) != 0)
|
|
|
|
{
|
|
|
|
/* Yes... Check if the user is logged in */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (!session->loggedin && (session->head != NULL))
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt1, 530, ' ',
|
2012-02-04 22:02:45 +01:00
|
|
|
"Please login with USER and PASS !");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if there is a handler for the command */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (g_ftpdcmdtab[index].handler != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
/* Yess.. invoke the command handler. */
|
|
|
|
|
|
|
|
return g_ftpdcmdtab[index].handler(session);
|
|
|
|
}
|
|
|
|
|
2012-02-05 23:46:56 +01:00
|
|
|
/* No... this command is not in the command table. Break out of
|
|
|
|
* the loop and send the 500 message.
|
|
|
|
*/
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There is nothing in the command table matching this command */
|
|
|
|
|
|
|
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
2012-02-05 23:46:56 +01:00
|
|
|
g_respfmt2, 500, ' ', session->command,
|
2012-02-04 22:02:45 +01:00
|
|
|
" not understood");
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Worker Thread
|
|
|
|
****************************************************************************/
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_startworker
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpd_startworker(pthread_startroutine_t handler, FAR void *arg,
|
|
|
|
size_t stacksize)
|
|
|
|
{
|
|
|
|
pthread_t threadid;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize the thread attributes */
|
|
|
|
|
|
|
|
ret = pthread_attr_init(&attr);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: pthread_attr_init() failed: %d\n", ret);
|
2012-02-04 22:02:45 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
/* The set the thread stack size */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
ret = pthread_attr_setstacksize(&attr, stacksize);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: pthread_attr_setstacksize() failed: %d\n", ret);
|
2012-02-04 22:02:45 +01:00
|
|
|
goto errout_with_attr;
|
|
|
|
}
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
/* And create the thread */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
ret = pthread_create(&threadid, &attr, handler, arg);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: pthread_create() failed: %d\n", ret);
|
2012-02-04 22:02:45 +01:00
|
|
|
goto errout_with_attr;
|
|
|
|
}
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
/* Put the thread in the detached stated */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
ret = pthread_detach(threadid);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: pthread_detach() failed: %d\n", ret);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_attr:
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
errout:
|
|
|
|
return -ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_freesession
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ftpd_freesession(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
|
|
|
/* Free resources */
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->renamefrom != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(session->renamefrom);
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->work != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(session->work);
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->home != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(session->home);
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->user != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
free(session->user);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2016-12-04 15:39:40 +01:00
|
|
|
if (session->fd >= 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
close(session->fd);
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->data.buffer != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
free(session->data.buffer);
|
|
|
|
}
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_dataclose(session);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->cmd.buffer != NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
free(session->cmd.buffer);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2016-12-04 15:39:40 +01:00
|
|
|
if (session->cmd.sd >= 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
close(session->cmd.sd);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_workersetup
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ftpd_workersetup(FAR struct ftpd_session_s *session)
|
|
|
|
{
|
2022-09-03 13:32:49 +02:00
|
|
|
UNUSED(session);
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
#if defined(CONFIG_NET_HAVE_IPTOS) || defined(CONFIG_NET_HAVE_OOBINLINE)
|
|
|
|
int temp;
|
2012-02-05 18:36:13 +01:00
|
|
|
#endif
|
2014-07-11 20:30:23 +02:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2012-02-04 22:02:45 +01:00
|
|
|
struct linger ling;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_HAVE_IPTOS
|
|
|
|
temp = IPTOS_LOWDELAY;
|
2020-01-02 13:09:50 +01:00
|
|
|
setsockopt(session->cmd.sd, IPPROTO_IP, IP_TOS, &temp, sizeof(temp));
|
2012-02-04 22:02:45 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_HAVE_OOBINLINE
|
|
|
|
temp = 1;
|
2020-01-02 13:09:50 +01:00
|
|
|
setsockopt(session->cmd.sd, SOL_SOCKET, SO_OOBINLINE, &temp, sizeof(temp));
|
2012-02-04 22:02:45 +01:00
|
|
|
#endif
|
|
|
|
|
2014-07-11 20:30:23 +02:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2020-01-02 13:09:50 +01:00
|
|
|
memset(&ling, 0, sizeof(ling));
|
2012-02-04 22:02:45 +01:00
|
|
|
ling.l_onoff = 1;
|
|
|
|
ling.l_linger = 4;
|
2020-01-02 13:09:50 +01:00
|
|
|
setsockopt(session->cmd.sd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
|
2012-02-04 22:02:45 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_worker
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR void *ftpd_worker(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct ftpd_session_s *session = (FAR struct ftpd_session_s *)arg;
|
|
|
|
ssize_t recvbytes;
|
|
|
|
size_t offset;
|
|
|
|
uint8_t ch;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Worker started\n");
|
2012-02-04 22:02:45 +01:00
|
|
|
DEBUGASSERT(session);
|
|
|
|
|
|
|
|
/* Configure the session sockets */
|
|
|
|
|
|
|
|
ftpd_workersetup(session);
|
|
|
|
|
|
|
|
/* Send the welcoming message */
|
|
|
|
|
|
|
|
ret = ftpd_response(session->cmd.sd, session->txtimeout,
|
2022-04-03 14:17:20 +02:00
|
|
|
g_respfmt1, 220, ' ', CONFIG_FTPD_SERVERID);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpd_response() failed: %d\n", ret);
|
2012-02-04 22:02:45 +01:00
|
|
|
ftpd_freesession(session);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then loop processing FTP commands */
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
for (; ; )
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
/* Receive the next command */
|
|
|
|
|
|
|
|
recvbytes = ftpd_recv(session->cmd.sd, session->cmd.buffer,
|
2012-02-05 18:36:13 +01:00
|
|
|
session->cmd.buflen - 1, session->rxtimeout);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2014-04-14 00:24:28 +02:00
|
|
|
/* recbytes < 0 is a receive failure (posibily a timeout);
|
2012-02-04 22:02:45 +01:00
|
|
|
* recbytes == 0 indicates that we have lost the connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (recvbytes <= 0)
|
|
|
|
{
|
|
|
|
/* Break out of the server loop */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Make sure that the received string is NUL terminated */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
session->cmd.buffer[recvbytes] = '\0';
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
/* TELNET protocol (RFC854)
|
|
|
|
* IAC 255(FFH) interpret as command:
|
|
|
|
* IP 244(F4H) interrupt process--permanently
|
|
|
|
* DM 242(F2H) data mark--for connect. cleaning
|
|
|
|
*/
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
while (recvbytes > 0)
|
|
|
|
{
|
|
|
|
ch = session->cmd.buffer[offset];
|
2020-01-12 01:32:40 +01:00
|
|
|
if (ch != 0xff && ch != 0xf4 && ch != 0xf2)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
ftpd_send(session->cmd.sd, &session->cmd.buffer[offset], 1,
|
|
|
|
session->txtimeout);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
offset++;
|
|
|
|
recvbytes--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just continue if there was nothing of interest in the packet */
|
|
|
|
|
|
|
|
if (recvbytes <= 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make command message */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
session->command = &session->cmd.buffer[offset];
|
2012-02-04 22:02:45 +01:00
|
|
|
while (session->cmd.buffer[offset] != '\0')
|
|
|
|
{
|
|
|
|
if (session->cmd.buffer[offset] == '\r' &&
|
|
|
|
session->cmd.buffer[offset + ((ssize_t)1)] == '\n')
|
|
|
|
{
|
|
|
|
session->cmd.buffer[offset] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2014-04-14 00:24:28 +02:00
|
|
|
offset++;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse command and param tokens */
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
session->param = session->command;
|
|
|
|
session->command = ftpd_strtok(true, " \t", &session->param);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
/* Unlike the "real" strtok, ftpd_strtok does not NUL-terminate
|
|
|
|
* the returned string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (session->param[0] != '\0')
|
|
|
|
{
|
|
|
|
session->param[0] = '\0';
|
|
|
|
session->param++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dispatch the FTP command */
|
|
|
|
|
|
|
|
ret = ftpd_command(session);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Disconnected by the command handler: %d\n", ret);
|
2012-02-04 22:02:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ftpd_freesession(session);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create an instance of the FTPD server and return a handle that can be
|
|
|
|
* used to run the server.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2022-09-03 16:42:08 +02:00
|
|
|
* port - The port that the server will listen to.
|
|
|
|
* family - The type of INET family to use when opening the socket.
|
|
|
|
* AF_INET and AF_INET6 are supported.
|
2012-02-04 22:02:45 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, a non-NULL handle is returned that can be used to reference
|
|
|
|
* the server instance.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-03 16:42:08 +02:00
|
|
|
FTPD_SESSION ftpd_open(int port, sa_family_t family)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
FAR struct ftpd_server_s *server;
|
|
|
|
|
2022-09-03 16:42:08 +02:00
|
|
|
server = ftpd_openserver(port, family);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
return (FTPD_SESSION)server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_adduser
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add one FTP user.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* handle - A handle previously returned by ftpd_open
|
2020-05-22 09:53:36 +02:00
|
|
|
* accountflags - The characteristics of this user
|
|
|
|
* (see FTPD_ACCOUNTFLAGS_* definitions).
|
2012-02-04 22:02:45 +01:00
|
|
|
* user - The user login name. May be NULL indicating that no login is
|
|
|
|
* required.
|
|
|
|
* passwd - The user password. May be NULL indicating that no password
|
|
|
|
* is required.
|
|
|
|
* home - The user home directory. May be NULL.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. A negated errno value is return on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftpd_adduser(FTPD_SESSION handle, uint8_t accountflags,
|
|
|
|
FAR const char *user, FAR const char *passwd,
|
|
|
|
FAR const char *home)
|
|
|
|
{
|
|
|
|
FAR struct ftpd_server_s *server;
|
|
|
|
FAR struct ftpd_account_s *newaccount;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(handle);
|
|
|
|
|
|
|
|
newaccount = ftpd_account_new(user, accountflags);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (newaccount == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2020-02-23 05:51:44 +01:00
|
|
|
nerr("ERROR: Failed to allocate memory to the account\n");
|
2012-02-04 22:02:45 +01:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2012-02-05 23:18:14 +01:00
|
|
|
ret = ftpd_account_setpassword(newaccount, passwd);
|
2012-02-04 22:02:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpd_account_setpassword failed: %d\n", ret);
|
2012-02-04 22:02:45 +01:00
|
|
|
goto errout_with_account;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
ret = ftpd_account_sethome(newaccount, home);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpd_account_sethome failed: %d\n", ret);
|
2012-02-04 22:02:45 +01:00
|
|
|
goto errout_with_account;
|
|
|
|
}
|
|
|
|
|
|
|
|
server = (FAR struct ftpd_server_s *)handle;
|
|
|
|
ret = ftpd_account_add(server, newaccount);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpd_account_add failed: %d\n", ret);
|
2012-02-04 22:02:45 +01:00
|
|
|
goto errout_with_account;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_account:
|
|
|
|
ftpd_account_free(newaccount);
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_session
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Execute the FTPD server. This thread does not return until either (1)
|
|
|
|
* the timeout expires with no connection, (2) some other error occurs, or
|
|
|
|
* (2) a connection was accepted and an FTP worker thread was started to
|
|
|
|
* service the session.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* handle - A handle previously returned by ftpd_open
|
|
|
|
* timeout - A time in milliseconds to wait for a connection. If this
|
|
|
|
* time elapses with no connected, the -ETIMEDOUT error will be returned.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned if the FTP worker was started. On failure, a negated
|
|
|
|
* errno value is returned to indicate why the servier terminated.
|
|
|
|
* -ETIMEDOUT indicates that the user-provided timeout elapsed with no
|
|
|
|
* connection.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftpd_session(FTPD_SESSION handle, int timeout)
|
|
|
|
{
|
|
|
|
FAR struct ftpd_server_s *server;
|
|
|
|
FAR struct ftpd_session_s *session;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(handle);
|
|
|
|
|
|
|
|
server = (FAR struct ftpd_server_s *)handle;
|
|
|
|
|
|
|
|
/* Allocate a session */
|
|
|
|
|
2020-01-12 01:32:40 +01:00
|
|
|
session = (FAR struct ftpd_session_s *)
|
|
|
|
zalloc(sizeof(struct ftpd_session_s));
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Failed to allocate session\n");
|
2012-02-04 22:02:45 +01:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the session */
|
|
|
|
|
|
|
|
session->server = server;
|
|
|
|
session->head = server->head;
|
2022-03-28 13:46:07 +02:00
|
|
|
session->loggedin = false;
|
2012-02-04 22:02:45 +01:00
|
|
|
session->flags = 0;
|
2014-04-14 00:24:28 +02:00
|
|
|
session->txtimeout = -1;
|
|
|
|
session->rxtimeout = -1;
|
2022-03-28 13:46:07 +02:00
|
|
|
session->cmd.sd = -1;
|
|
|
|
session->cmd.addrlen = sizeof(session->cmd.addr);
|
|
|
|
session->cmd.buflen = CONFIG_FTPD_CMDBUFFERSIZE;
|
2012-02-04 22:02:45 +01:00
|
|
|
session->cmd.buffer = NULL;
|
2012-02-05 18:36:13 +01:00
|
|
|
session->command = NULL;
|
2012-02-04 22:02:45 +01:00
|
|
|
session->param = NULL;
|
|
|
|
session->data.sd = -1;
|
|
|
|
session->data.addrlen = sizeof(session->data.addr);
|
|
|
|
session->data.buflen = CONFIG_FTPD_DATABUFFERSIZE;
|
|
|
|
session->data.buffer = NULL;
|
|
|
|
session->restartpos = 0;
|
|
|
|
session->fd = -1;
|
|
|
|
session->user = NULL;
|
|
|
|
session->type = FTPD_SESSIONTYPE_NONE;
|
|
|
|
session->home = NULL;
|
|
|
|
session->work = NULL;
|
|
|
|
session->renamefrom = NULL;
|
|
|
|
|
|
|
|
/* Allocate a command buffer */
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
session->cmd.buffer = (FAR char *)malloc(session->cmd.buflen);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->cmd.buffer == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Failed to allocate command buffer\n");
|
2012-02-04 22:02:45 +01:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a data buffer */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
session->data.buffer = (FAR char *)malloc(session->data.buflen);
|
2022-03-28 13:46:07 +02:00
|
|
|
if (session->data.buffer == NULL)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Failed to allocate data buffer\n");
|
2012-02-04 22:02:45 +01:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Accept a connection */
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
session->cmd.sd = ftpd_accept(server->sd, (FAR void *)&session->cmd.addr,
|
2012-02-04 22:02:45 +01:00
|
|
|
&session->cmd.addrlen, timeout);
|
2012-02-05 18:36:13 +01:00
|
|
|
if (session->cmd.sd < 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2020-05-22 09:53:36 +02:00
|
|
|
/* Only report interesting,
|
|
|
|
* infrequent errors (not the common timeout)
|
|
|
|
*/
|
2012-02-18 19:13:30 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_NET
|
|
|
|
if (session->cmd.sd != -ETIMEDOUT)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpd_accept() failed: %d\n", session->cmd.sd);
|
2012-02-18 19:13:30 +01:00
|
|
|
}
|
|
|
|
#endif
|
2020-01-12 01:32:40 +01:00
|
|
|
|
2012-02-05 20:32:42 +01:00
|
|
|
ret = session->cmd.sd;
|
2012-02-04 22:02:45 +01:00
|
|
|
goto errout_with_session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And create a worker thread to service the session */
|
|
|
|
|
|
|
|
ret = ftpd_startworker(ftpd_worker, (FAR void *)session,
|
|
|
|
CONFIG_FTPD_WORKERSTACKSIZE);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpd_startworker() failed: %d\n", ret);
|
2012-02-04 22:02:45 +01:00
|
|
|
goto errout_with_session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Successfully connected an launched the worker thread */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
errout_with_session:
|
|
|
|
ftpd_freesession(session);
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Close and destroy the handle created by ftpd_open.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* handle - A handle previously returned by ftpd_open
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void ftpd_close(FTPD_SESSION handle)
|
|
|
|
{
|
|
|
|
struct ftpd_server_s *server;
|
|
|
|
DEBUGASSERT(handle);
|
|
|
|
|
|
|
|
server = (struct ftpd_server_s *)handle;
|
2021-01-13 18:55:48 +01:00
|
|
|
if (server->head != NULL)
|
|
|
|
{
|
|
|
|
ftpd_account_free(server->head);
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
if (server->sd >= 0)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
close(server->sd);
|
|
|
|
server->sd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(server);
|
|
|
|
}
|