2012-02-04 23:49:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* examples/telnetd/shell.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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 of the Institute 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 INSTITUTE 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 INSTITUTE 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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
2012-02-05 18:36:13 +01:00
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/netlib.h"
|
|
|
|
#include "netutils/ftpd.h"
|
2012-02-05 18:36:13 +01:00
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
#include "ftpd.h"
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* If FTP is used and both IPv6 and IPv4 are enabled, then we need to
|
|
|
|
* pick one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
# define ADDR_FAMILY AF_INET6
|
|
|
|
#else
|
|
|
|
# define ADDR_FAMILY AF_INET
|
|
|
|
#endif
|
|
|
|
|
2012-02-04 23:49:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2012-02-04 22:02:45 +01:00
|
|
|
|
|
|
|
static const struct fptd_account_s g_ftpdaccounts[] =
|
|
|
|
{
|
2012-02-04 23:49:42 +01:00
|
|
|
{ FTPD_ACCOUNTFLAG_SYSTEM, "root", "abc123", NULL },
|
2012-02-04 22:02:45 +01:00
|
|
|
{ FTPD_ACCOUNTFLAG_GUEST, "ftp", NULL, NULL },
|
|
|
|
{ FTPD_ACCOUNTFLAG_GUEST, "anonymous", NULL, NULL },
|
|
|
|
};
|
|
|
|
#define NACCOUNTS (sizeof(g_ftpdaccounts) / sizeof(struct fptd_account_s))
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* To minimize the probability of name collisitions, all FTPD example
|
|
|
|
* global data is maintained in a single instance of a structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct ftpd_globals_s g_ftpdglob;
|
|
|
|
|
2012-02-04 23:49:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
2012-02-05 18:36:13 +01:00
|
|
|
* Name: fptd_netinit
|
2012-02-04 23:49:42 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
static void fptd_netinit(void)
|
2012-02-04 23:49:42 +01:00
|
|
|
{
|
|
|
|
#ifndef CONFIG_EXAMPLES_FTPD_NONETINIT
|
|
|
|
struct in_addr addr;
|
2012-10-04 19:36:07 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_FTPD_NOMAC
|
2012-02-04 23:49:42 +01:00
|
|
|
uint8_t mac[IFHWADDRLEN];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_FTPD_NOMAC
|
2012-02-04 23:49:42 +01:00
|
|
|
mac[0] = 0x00;
|
|
|
|
mac[1] = 0xe0;
|
2012-07-20 00:32:19 +02:00
|
|
|
mac[2] = 0xde;
|
|
|
|
mac[3] = 0xad;
|
|
|
|
mac[4] = 0xbe;
|
|
|
|
mac[5] = 0xef;
|
2014-07-03 00:52:02 +02:00
|
|
|
netlib_setmacaddr("eth0", mac);
|
2012-02-04 23:49:42 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set up our host address */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_FTPD_IPADDR);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4addr("eth0", &addr);
|
2012-02-04 23:49:42 +01:00
|
|
|
|
|
|
|
/* Set up the default router address */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_FTPD_DRIPADDR);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_dripv4addr("eth0", &addr);
|
2012-02-04 23:49:42 +01:00
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_FTPD_NETMASK);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4netmask("eth0", &addr);
|
2017-05-19 17:30:26 +02:00
|
|
|
|
|
|
|
/* New versions of netlib_set_ipvXaddr will not bring the network up,
|
2017-05-19 17:50:26 +02:00
|
|
|
* So ensure the network is really up at this point.
|
|
|
|
*/
|
2017-05-19 17:30:26 +02:00
|
|
|
|
|
|
|
netlib_ifup("eth0");
|
|
|
|
|
2012-02-04 23:49:42 +01:00
|
|
|
#endif /* CONFIG_EXAMPLES_FTPD_NONETINIT */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_accounts
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
static void ftpd_accounts(FTPD_SESSION handle)
|
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
FAR const struct fptd_account_s *account;
|
2012-02-04 22:02:45 +01:00
|
|
|
int i;
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
printf("Adding accounts:\n");
|
2012-02-04 22:02:45 +01:00
|
|
|
for (i = 0; i < NACCOUNTS; i++)
|
|
|
|
{
|
|
|
|
account = &g_ftpdaccounts[i];
|
2012-02-05 18:36:13 +01:00
|
|
|
|
2012-02-05 20:32:42 +01:00
|
|
|
printf("%d. %s account: USER=%s PASSWORD=%s HOME=%s\n", i+1,
|
2012-02-05 18:36:13 +01:00
|
|
|
(account->flags & FTPD_ACCOUNTFLAG_SYSTEM) != 0 ? "Root" : "User",
|
2012-02-05 20:32:42 +01:00
|
|
|
(!account->user) ? "(none)" : account->user,
|
|
|
|
(!account->password) ? "(none)" : account->password,
|
|
|
|
(!account->home) ? "(none)" : account->home);
|
2012-02-05 18:36:13 +01:00
|
|
|
|
|
|
|
ftpd_adduser(handle, account->flags, account->user,
|
|
|
|
account->password, account->home);
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-04 23:49:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_daemon
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftpd_daemon(int s_argc, char **s_argv)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
FTPD_SESSION handle;
|
|
|
|
int ret;
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
/* The FTPD daemon has been started */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
g_ftpdglob.running = true;
|
|
|
|
#endif
|
|
|
|
printf("FTP daemon [%d] started\n", g_ftpdglob.pid);
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
/* Open FTPD */
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
#if ADDR_FAMILY == AF_INET6
|
|
|
|
handle = ftpd_open(AF_INET6);
|
|
|
|
#else
|
|
|
|
handle = ftpd_open(AF_INET);
|
|
|
|
#endif
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
if (!handle)
|
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
printf("FTP daemon [%d] failed to open FTPD\n", g_ftpdglob.pid);
|
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
g_ftpdglob.running = false;
|
|
|
|
g_ftpdglob.stop = false;
|
|
|
|
#endif
|
|
|
|
g_ftpdglob.pid = -1;
|
2012-02-04 22:02:45 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure acounts */
|
|
|
|
|
|
|
|
(void)ftpd_accounts(handle);
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
/* Then drive the FTPD server. */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
while (g_ftpdglob.stop == 0)
|
|
|
|
#else
|
|
|
|
for (;;)
|
|
|
|
#endif
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
/* If ftpd_session returns success, it means that a new FTP session
|
|
|
|
* has been started.
|
|
|
|
*/
|
|
|
|
|
2012-02-05 23:18:14 +01:00
|
|
|
ret = ftpd_session(handle, 5000);
|
2012-02-18 19:13:30 +01:00
|
|
|
|
|
|
|
/* If any interesting happened (i.e., any thing other than a timeout),
|
|
|
|
* then report the interesting event.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret != -ETIMEDOUT)
|
|
|
|
{
|
|
|
|
printf("FTP daemon [%d] ftpd_session returned %d\n", g_ftpdglob.pid, ret);
|
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
/* Close the FTPD server and exit (we can get here only if
|
|
|
|
* CONFIG_NSH_BUILTIN_APPS is defined).
|
|
|
|
*/
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
printf("FTP daemon [%d] stopping\n", g_ftpdglob.pid);
|
|
|
|
g_ftpdglob.running = false;
|
|
|
|
g_ftpdglob.stop = false;
|
|
|
|
g_ftpdglob.pid = -1;
|
2012-02-04 22:02:45 +01:00
|
|
|
ftpd_close(handle);
|
2012-02-05 18:36:13 +01:00
|
|
|
#endif
|
2012-02-04 22:02:45 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2012-02-04 23:49:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
2012-08-30 22:13:50 +02:00
|
|
|
* Name: ftpd_main
|
2012-02-04 23:49:42 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2018-08-23 19:06:15 +02:00
|
|
|
#ifdef BUILD_MODULE
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2014-09-06 17:19:15 +02:00
|
|
|
#else
|
2012-08-30 22:13:50 +02:00
|
|
|
int ftpd_main(int s_argc, char **s_argv)
|
2014-09-06 17:19:15 +02:00
|
|
|
#endif
|
2012-02-04 23:49:42 +01:00
|
|
|
{
|
|
|
|
/* Check if we have already initialized the network */
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
if (!g_ftpdglob.initialized)
|
2012-02-04 23:49:42 +01:00
|
|
|
{
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-04 23:49:42 +01:00
|
|
|
/* Bring up the network */
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
printf("Initializing the network\n");
|
|
|
|
fptd_netinit();
|
|
|
|
|
|
|
|
/* Initialize daemon state */
|
|
|
|
|
|
|
|
g_ftpdglob.initialized = true;
|
|
|
|
g_ftpdglob.pid = -1;
|
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
g_ftpdglob.stop = false;
|
|
|
|
g_ftpdglob.running = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then start the new daemon (if it is not already running) */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
if (g_ftpdglob.stop && g_ftpdglob.running)
|
|
|
|
{
|
|
|
|
printf("Waiting for FTP daemon [%d] to stop\n", g_ftpdglob.pid);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (!g_ftpdglob.running)
|
|
|
|
{
|
|
|
|
printf("Starting the FTP daemon\n");
|
2014-09-01 21:21:15 +02:00
|
|
|
g_ftpdglob.pid = task_create("FTP daemon", CONFIG_EXAMPLES_FTPD_PRIO,
|
2014-04-14 00:24:28 +02:00
|
|
|
CONFIG_EXAMPLES_FTPD_STACKSIZE,
|
2012-02-05 18:36:13 +01:00
|
|
|
ftpd_daemon, NULL);
|
|
|
|
if (g_ftpdglob.pid < 0)
|
2012-02-04 23:49:42 +01:00
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
printf("Failed to start the FTP daemon: %d\n", errno);
|
2012-02-04 23:49:42 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2012-02-05 18:36:13 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("FTP daemon [%d] is running\n", g_ftpdglob.pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2012-02-04 23:49:42 +01:00
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_stop
|
|
|
|
****************************************************************************/
|
2012-02-04 23:49:42 +01:00
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
int ftpd_stop(int s_argc, char **s_argv)
|
|
|
|
{
|
2012-02-05 23:46:56 +01:00
|
|
|
if (!g_ftpdglob.initialized || !g_ftpdglob.running)
|
2012-02-04 23:49:42 +01:00
|
|
|
{
|
2012-02-05 18:36:13 +01:00
|
|
|
printf("The FTP daemon not running\n");
|
2012-02-04 23:49:42 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
printf("Stopping the FTP daemon, pid=%d\n", g_ftpdglob.pid);
|
|
|
|
g_ftpdglob.stop = true;
|
2012-02-04 23:49:42 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2012-02-05 18:36:13 +01:00
|
|
|
#endif
|