2012-02-04 18:49:42 -04: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 13:36:13 -04: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>
|
|
|
|
|
|
|
|
#include <apps/netutils/uiplib.h>
|
|
|
|
#include <apps/netutils/ftpd.h>
|
|
|
|
|
2012-02-04 17:02:45 -04:00
|
|
|
#include "ftpd.h"
|
|
|
|
|
2012-02-04 18:49:42 -04:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2012-02-04 17:02:45 -04:00
|
|
|
|
|
|
|
static const struct fptd_account_s g_ftpdaccounts[] =
|
|
|
|
{
|
2012-02-04 18:49:42 -04:00
|
|
|
{ FTPD_ACCOUNTFLAG_SYSTEM, "root", "abc123", NULL },
|
2012-02-04 17:02:45 -04: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 13:36:13 -04: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 18:49:42 -04:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
2012-02-05 13:36:13 -04:00
|
|
|
* Name: fptd_netinit
|
2012-02-04 18:49:42 -04:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-05 13:36:13 -04:00
|
|
|
static void fptd_netinit(void)
|
2012-02-04 18:49:42 -04:00
|
|
|
{
|
|
|
|
#ifndef CONFIG_EXAMPLES_FTPD_NONETINIT
|
|
|
|
struct in_addr addr;
|
|
|
|
#ifdef CONFIG_EXAMPLE_FTPD_NOMAC
|
|
|
|
uint8_t mac[IFHWADDRLEN];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLE_FTPD_NOMAC
|
|
|
|
mac[0] = 0x00;
|
|
|
|
mac[1] = 0xe0;
|
|
|
|
mac[2] = 0xb0;
|
|
|
|
mac[3] = 0x0b;
|
|
|
|
mac[4] = 0xba;
|
|
|
|
mac[5] = 0xbe;
|
|
|
|
uip_setmacaddr("eth0", mac);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set up our host address */
|
|
|
|
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLE_FTPD_IPADDR);
|
|
|
|
uip_sethostaddr("eth0", &addr);
|
|
|
|
|
|
|
|
/* Set up the default router address */
|
|
|
|
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLE_FTPD_DRIPADDR);
|
|
|
|
uip_setdraddr("eth0", &addr);
|
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLE_FTPD_NETMASK);
|
|
|
|
uip_setnetmask("eth0", &addr);
|
|
|
|
#endif /* CONFIG_EXAMPLES_FTPD_NONETINIT */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_accounts
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-04 17:02:45 -04:00
|
|
|
static void ftpd_accounts(FTPD_SESSION handle)
|
|
|
|
{
|
2012-02-05 13:36:13 -04:00
|
|
|
FAR const struct fptd_account_s *account;
|
2012-02-04 17:02:45 -04:00
|
|
|
int i;
|
|
|
|
|
2012-02-05 13:36:13 -04:00
|
|
|
printf("Adding accounts:\n");
|
2012-02-04 17:02:45 -04:00
|
|
|
for (i = 0; i < NACCOUNTS; i++)
|
|
|
|
{
|
|
|
|
account = &g_ftpdaccounts[i];
|
2012-02-05 13:36:13 -04:00
|
|
|
|
2012-02-05 15:32:42 -04:00
|
|
|
printf("%d. %s account: USER=%s PASSWORD=%s HOME=%s\n", i+1,
|
2012-02-05 13:36:13 -04:00
|
|
|
(account->flags & FTPD_ACCOUNTFLAG_SYSTEM) != 0 ? "Root" : "User",
|
2012-02-05 15:32:42 -04:00
|
|
|
(!account->user) ? "(none)" : account->user,
|
|
|
|
(!account->password) ? "(none)" : account->password,
|
|
|
|
(!account->home) ? "(none)" : account->home);
|
2012-02-05 13:36:13 -04:00
|
|
|
|
|
|
|
ftpd_adduser(handle, account->flags, account->user,
|
|
|
|
account->password, account->home);
|
2012-02-04 17:02:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-04 18:49:42 -04:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_daemon
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftpd_daemon(int s_argc, char **s_argv)
|
2012-02-04 17:02:45 -04:00
|
|
|
{
|
|
|
|
FTPD_SESSION handle;
|
|
|
|
int ret;
|
|
|
|
|
2012-02-05 13:36:13 -04: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 17:02:45 -04:00
|
|
|
/* Open FTPD */
|
|
|
|
|
|
|
|
handle = ftpd_open();
|
|
|
|
if (!handle)
|
|
|
|
{
|
2012-02-05 13:36:13 -04: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 17:02:45 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure acounts */
|
|
|
|
|
|
|
|
(void)ftpd_accounts(handle);
|
|
|
|
|
2012-02-05 13:36:13 -04:00
|
|
|
/* Then drive the FTPD server. */
|
2012-02-04 17:02:45 -04:00
|
|
|
|
2012-02-05 13:36:13 -04:00
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
while (g_ftpdglob.stop == 0)
|
|
|
|
#else
|
|
|
|
for (;;)
|
|
|
|
#endif
|
2012-02-04 17:02:45 -04:00
|
|
|
{
|
2012-02-05 13:36:13 -04:00
|
|
|
/* If ftpd_session returns success, it means that a new FTP session
|
|
|
|
* has been started.
|
|
|
|
*/
|
|
|
|
|
2012-02-05 18:18:14 -04:00
|
|
|
ret = ftpd_session(handle, 5000);
|
2012-02-18 14:13:30 -04: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 17:02:45 -04:00
|
|
|
}
|
|
|
|
|
2012-02-05 13:36:13 -04:00
|
|
|
/* Close the FTPD server and exit (we can get here only if
|
|
|
|
* CONFIG_NSH_BUILTIN_APPS is defined).
|
|
|
|
*/
|
2012-02-04 17:02:45 -04:00
|
|
|
|
2012-02-05 13:36:13 -04: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 17:02:45 -04:00
|
|
|
ftpd_close(handle);
|
2012-02-05 13:36:13 -04:00
|
|
|
#endif
|
2012-02-04 17:02:45 -04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2012-02-04 18:49:42 -04:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
2012-02-05 13:36:13 -04:00
|
|
|
* Name: user_start/ftpd_start
|
2012-02-04 18:49:42 -04:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int MAIN_NAME(int s_argc, char **s_argv)
|
|
|
|
{
|
|
|
|
/* Check if we have already initialized the network */
|
|
|
|
|
2012-02-05 13:36:13 -04:00
|
|
|
if (!g_ftpdglob.initialized)
|
2012-02-04 18:49:42 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Bring up the network */
|
|
|
|
|
2012-02-05 13:36:13 -04: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");
|
|
|
|
g_ftpdglob.pid = TASK_CREATE("FTP daemon", CONFIG_EXAMPLES_FTPD_PRIO,
|
|
|
|
CONFIG_EXAMPLES_FTPD_STACKSIZE,
|
|
|
|
ftpd_daemon, NULL);
|
|
|
|
if (g_ftpdglob.pid < 0)
|
2012-02-04 18:49:42 -04:00
|
|
|
{
|
2012-02-05 13:36:13 -04:00
|
|
|
printf("Failed to start the FTP daemon: %d\n", errno);
|
2012-02-04 18:49:42 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2012-02-05 13:36:13 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("FTP daemon [%d] is running\n", g_ftpdglob.pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2012-02-04 18:49:42 -04:00
|
|
|
|
2012-02-05 13:36:13 -04:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpd_stop
|
|
|
|
****************************************************************************/
|
2012-02-04 18:49:42 -04:00
|
|
|
|
2012-02-05 13:36:13 -04:00
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
int ftpd_stop(int s_argc, char **s_argv)
|
|
|
|
{
|
2012-02-05 18:46:56 -04:00
|
|
|
if (!g_ftpdglob.initialized || !g_ftpdglob.running)
|
2012-02-04 18:49:42 -04:00
|
|
|
{
|
2012-02-05 13:36:13 -04:00
|
|
|
printf("The FTP daemon not running\n");
|
2012-02-04 18:49:42 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-02-05 13:36:13 -04:00
|
|
|
printf("Stopping the FTP daemon, pid=%d\n", g_ftpdglob.pid);
|
|
|
|
g_ftpdglob.stop = true;
|
2012-02-04 18:49:42 -04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2012-02-05 13:36:13 -04:00
|
|
|
#endif
|