2012-02-04 23:49:42 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/ftpd/ftpd_main.c
|
2012-02-04 23:49:42 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-02-04 23:49:42 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-02-04 23:49:42 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-02-04 23:49:42 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-03 16:42:08 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
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>
|
|
|
|
|
2023-08-15 19:12:50 +02:00
|
|
|
#include <sys/param.h>
|
2012-02-05 18:36:13 +01:00
|
|
|
#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 },
|
|
|
|
};
|
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-03-28 13:46:07 +02:00
|
|
|
/* To minimize the probability of name collisions, all FTPD example
|
2012-02-05 18:36:13 +01:00
|
|
|
* 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
|
|
|
|
****************************************************************************/
|
2021-01-13 19:01:00 +01:00
|
|
|
|
2012-02-04 23:49:42 +01:00
|
|
|
/****************************************************************************
|
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
|
|
|
|
|
2021-01-13 19:01:00 +01:00
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
2012-02-04 23:49:42 +01:00
|
|
|
|
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");
|
2023-08-15 19:12:50 +02:00
|
|
|
for (i = 0; i < nitems(g_ftpdaccounts); i++)
|
2012-02-04 22:02:45 +01:00
|
|
|
{
|
|
|
|
account = &g_ftpdaccounts[i];
|
2012-02-05 18:36:13 +01:00
|
|
|
|
2021-01-13 19:01:00 +01:00
|
|
|
printf("%d. %s account: USER=%s PASSWORD=%s HOME=%s\n", i + 1,
|
|
|
|
(account->flags & FTPD_ACCOUNTFLAG_SYSTEM) ? "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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 03:14:26 +01:00
|
|
|
static void hint(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: ftpd [-46] \n\
|
|
|
|
\t-4 Use IPv4\n\
|
|
|
|
\t-6 Use IPv6\n\
|
|
|
|
");
|
|
|
|
}
|
|
|
|
|
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;
|
2023-03-23 03:14:26 +01:00
|
|
|
int ret = EXIT_FAILURE;
|
|
|
|
int option;
|
|
|
|
int family = AF_UNSPEC;
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
/* The FTPD daemon has been started */
|
|
|
|
|
|
|
|
g_ftpdglob.running = true;
|
|
|
|
printf("FTP daemon [%d] started\n", g_ftpdglob.pid);
|
|
|
|
|
2023-03-23 03:14:26 +01:00
|
|
|
while ((option = getopt(s_argc, &s_argv[1], "46")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case '4':
|
|
|
|
family = AF_INET;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
family = AF_INET6;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (family == AF_UNSPEC)
|
|
|
|
{
|
|
|
|
hint();
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((optind + 1) < s_argc)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Too many arguments\n", s_argv[1]);
|
|
|
|
hint();
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-02-04 22:02:45 +01:00
|
|
|
/* Open FTPD */
|
|
|
|
|
2023-03-23 03:14:26 +01:00
|
|
|
handle = ftpd_open(CONFIG_EXAMPLES_FTPD_PORT, family);
|
2017-11-07 00:41:30 +01:00
|
|
|
|
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);
|
2023-03-23 03:14:26 +01:00
|
|
|
goto out;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Configure accounts */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpd_accounts(handle);
|
2012-02-04 22:02:45 +01:00
|
|
|
|
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
|
|
|
while (g_ftpdglob.stop == 0)
|
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)
|
|
|
|
{
|
2021-01-13 19:01:00 +01:00
|
|
|
printf("FTP daemon [%d] ftpd_session returned %d\n",
|
|
|
|
g_ftpdglob.pid, ret);
|
2012-02-18 19:13:30 +01:00
|
|
|
}
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2019-10-07 10:37:56 +02:00
|
|
|
/* Close the FTPD server and exit. */
|
2012-02-04 22:02:45 +01:00
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
printf("FTP daemon [%d] stopping\n", g_ftpdglob.pid);
|
2023-03-23 03:14:26 +01:00
|
|
|
ftpd_close(handle);
|
|
|
|
ret = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
out:
|
2012-02-05 18:36:13 +01:00
|
|
|
g_ftpdglob.running = false;
|
|
|
|
g_ftpdglob.stop = false;
|
|
|
|
g_ftpdglob.pid = -1;
|
2019-10-07 10:37:56 +02:00
|
|
|
|
2023-03-23 03:14:26 +01:00
|
|
|
return ret;
|
2012-02-04 22:02:45 +01:00
|
|
|
}
|
|
|
|
|
2012-02-04 23:49:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
2021-01-13 19:01:00 +01:00
|
|
|
|
2012-02-04 23:49:42 +01:00
|
|
|
/****************************************************************************
|
2012-08-30 22:13:50 +02:00
|
|
|
* Name: ftpd_main
|
2012-02-04 23:49:42 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
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
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
g_ftpdglob.stop = false;
|
|
|
|
g_ftpdglob.running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then start the new daemon (if it is not already running) */
|
|
|
|
|
|
|
|
if (g_ftpdglob.stop && g_ftpdglob.running)
|
|
|
|
{
|
|
|
|
printf("Waiting for FTP daemon [%d] to stop\n", g_ftpdglob.pid);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2021-01-13 19:01:00 +01:00
|
|
|
|
2012-02-05 18:36:13 +01:00
|
|
|
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,
|
2023-03-23 03:14:26 +01:00
|
|
|
ftpd_daemon, argv);
|
2012-02-05 18:36:13 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 19:01:00 +01:00
|
|
|
else
|
2012-02-05 18:36:13 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2018-09-03 17:29:56 +02:00
|
|
|
int ftpd_stop_main(int s_argc, char **s_argv)
|
2012-02-05 18:36:13 +01:00
|
|
|
{
|
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;
|
|
|
|
}
|