You can now configure a login for Telnet NSH session -- from Darcy Gong

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5231 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2012-10-12 16:59:17 +00:00
parent f90c8836c0
commit 9c58ee4a6f
6 changed files with 208 additions and 23 deletions

@ -291,19 +291,18 @@ config NSH_CONSOLE
console front-end is selected (/dev/console). console front-end is selected (/dev/console).
Normally, the serial console device is a UART and RS-232 Normally, the serial console device is a UART and RS-232
interface. However, if CONFIG_USBDEV is defined, then a USB interface. However, if USBDEV is defined, then a USB
serial device may, instead, be used if the one of serial device may, instead, be used if the one of
the following are defined: the following are defined:
CONFIG_PL2303 and CONFIG_PL2303_CONSOLE - Sets up the PL2303 and PL2303_CONSOLE - Set up the Prolifics PL2303
Prolifics PL2303 emulation as a console device at /dev/console. emulation as a console device at /dev/console.
CONFIG_CDCACM and CONFIG_CDCACM_CONSOLE - Sets up the CDCACM and CDCACM_CONSOLE - Set up the CDC/ACM serial
CDC/ACM serial device as a console device at dev/console. device as a console device at dev/console.
CONFIG_NSH_USBCONSOLE and CONFIG_NSH_USBCONDEV - Sets up the NSH_USBCONSOLE and NSH_USBCONDEV - Sets up some other USB
some other USB serial device as the NSH console (not necessarily serial device as the NSH console (not necessarily dev/console).
dev/console).
config NSH_USBCONSOLE config NSH_USBCONSOLE
bool "Use a USB console" bool "Use a USB console"
@ -311,20 +310,20 @@ config NSH_USBCONSOLE
depends on NSH_CONSOLE && USBDEV depends on NSH_CONSOLE && USBDEV
---help--- ---help---
If defined, then the an arbitrary USB device may be used If defined, then the an arbitrary USB device may be used
to as the NSH console. In this case, CONFIG_NSH_USBCONDEV to as the NSH console. In this case, NSH_USBCONDEV must
must be defined to indicate which USB device to use as be defined to indicate which USB device to use as the
the console. console.
config NSH_USBCONDEV config NSH_USBCONDEV
string "USB console device" string "USB console device"
default "/dev/ttyACM0" default "/dev/ttyACM0"
depends on NSH_USBCONSOLE depends on NSH_USBCONSOLE
---help--- ---help---
If CONFIG_NSH_USBCONSOLE is set to 'y', then CONFIG_NSH_USBCONDEV If NSH_USBCONSOLE is set to 'y', then NSH_USBCONDEV must
must also be set to select the USB device used to support also be set to select the USB device used to support the
the NSH console. This should be set to the quoted name of a NSH console. This should be set to the quoted name of a
readable/write-able USB driver such as: readable/write-able USB driver such as:
CONFIG_NSH_USBCONDEV="/dev/ttyACM0". NSH_USBCONDEV="/dev/ttyACM0".
config UBSDEV_MINOR config UBSDEV_MINOR
int "USB console device minor number" int "USB console device minor number"
@ -448,6 +447,35 @@ config NSH_IOBUFFER_SIZE
---help--- ---help---
Determines the size of the I/O buffer to use for sending/ Determines the size of the I/O buffer to use for sending/
receiving TELNET commands/reponses. Default: 512 receiving TELNET commands/reponses. Default: 512
config NSH_TELNET_LOGIN
bool "Telnet Login"
default n
---help---
If defined, then the Telnet user will be required to provide a
username and password to start the NSH shell.
if NSH_TELNET_LOGIN
config NSH_TELNET_USERNAME
string "Login Username"
default "admin"
---help---
Login user name. Default: "admin"
config NSH_TELNET_PASSWORD
string "Login Password"
default "nuttx"
---help---
Login password: Default: "nuttx"
config NSH_TELNET_FAILCOUNT
int "Login Retry Count"
default 3
---help---
Number of login retry attempts.
endif
endif endif
config NSH_DHCPC config NSH_DHCPC

@ -164,10 +164,19 @@
* Default: SCHED_PRIORITY_DEFAULT * Default: SCHED_PRIORITY_DEFAULT
* CONFIG_NSH_TELNETD_DAEMONSTACKSIZE - Stack size allocated for the * CONFIG_NSH_TELNETD_DAEMONSTACKSIZE - Stack size allocated for the
* Telnet daemon. Default: 2048 * Telnet daemon. Default: 2048
* CONFIG_NSH_TELNETD_CLIENTPRIO- Priority of the Telnet client. * CONFIG_NSH_TELNETD_CLIENTPRIO - Priority of the Telnet client.
* Default: SCHED_PRIORITY_DEFAULT * Default: SCHED_PRIORITY_DEFAULT
* CONFIG_NSH_TELNETD_CLIENTSTACKSIZE - Stack size allocated for the * CONFIG_NSH_TELNETD_CLIENTSTACKSIZE - Stack size allocated for the
* Telnet client. Default: 2048 * Telnet client. Default: 2048
* CONFIG_NSH_TELNET_LOGIN - Support a simple Telnet login.
*
* If CONFIG_NSH_TELNET_LOGIN is defined, then these additional
* options may be specified:
*
* CONFIG_NSH_TELNET_USERNAME - Login user name. Default: "admin"
* CONFIG_NSH_TELNET_PASSWORD - Login password: Default: "nuttx"
* CONFIG_NSH_TELNET_FAILCOUNT - Number of login retry attempts.
* Default 3.
*/ */
#ifndef CONFIG_NSH_TELNETD_PORT #ifndef CONFIG_NSH_TELNETD_PORT
@ -190,6 +199,22 @@
# define CONFIG_NSH_TELNETD_CLIENTSTACKSIZE 2048 # define CONFIG_NSH_TELNETD_CLIENTSTACKSIZE 2048
#endif #endif
#ifdef CONFIG_NSH_TELNET_LOGIN
# ifndef CONFIG_NSH_TELNET_USERNAME
# define CONFIG_NSH_TELNET_USERNAME "admin"
# endif
# ifndef CONFIG_NSH_TELNET_PASSWORD
# define CONFIG_NSH_TELNET_PASSWORD "nuttx"
# endif
# ifndef CONFIG_NSH_TELNET_FAILCOUNT
# define CONFIG_NSH_TELNET_FAILCOUNT 3
# endif
#endif /* CONFIG_NSH_TELNET_LOGIN */
/* Verify support for ROMFS /etc directory support options */ /* Verify support for ROMFS /etc directory support options */
#ifdef CONFIG_NSH_ROMFSETC #ifdef CONFIG_NSH_ROMFSETC
@ -362,6 +387,14 @@ typedef int (*cmd_t)(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
****************************************************************************/ ****************************************************************************/
extern const char g_nshgreeting[]; extern const char g_nshgreeting[];
#if defined(CONFIG_NSH_TELNET_LOGIN) && defined(CONFIG_NSH_TELNET)
extern const char g_telnetgreeting[];
extern const char g_userprompt[];
extern const char g_passwordprompt[];
extern const char g_loginsuccess[];
extern const char g_badcredentials[];
extern const char g_loginfailure[];
#endif
extern const char g_nshprompt[]; extern const char g_nshprompt[];
extern const char g_nshsyntax[]; extern const char g_nshsyntax[];
extern const char g_fmtargrequired[]; extern const char g_fmtargrequired[];

@ -75,12 +75,17 @@ struct serialsave_s
static FAR struct nsh_vtbl_s *nsh_consoleclone(FAR struct nsh_vtbl_s *vtbl); static FAR struct nsh_vtbl_s *nsh_consoleclone(FAR struct nsh_vtbl_s *vtbl);
#endif #endif
static void nsh_consolerelease(FAR struct nsh_vtbl_s *vtbl); static void nsh_consolerelease(FAR struct nsh_vtbl_s *vtbl);
static ssize_t nsh_consolewrite(FAR struct nsh_vtbl_s *vtbl, FAR const void *buffer, size_t nbytes); static ssize_t nsh_consolewrite(FAR struct nsh_vtbl_s *vtbl,
static int nsh_consoleoutput(FAR struct nsh_vtbl_s *vtbl, const char *fmt, ...); FAR const void *buffer, size_t nbytes);
static int nsh_consoleoutput(FAR struct nsh_vtbl_s *vtbl,
FAR const char *fmt, ...);
static FAR char *nsh_consolelinebuffer(FAR struct nsh_vtbl_s *vtbl); static FAR char *nsh_consolelinebuffer(FAR struct nsh_vtbl_s *vtbl);
static void nsh_consoleredirect(FAR struct nsh_vtbl_s *vtbl, int fd, FAR uint8_t *save); static void nsh_consoleredirect(FAR struct nsh_vtbl_s *vtbl, int fd,
static void nsh_consoleundirect(FAR struct nsh_vtbl_s *vtbl, FAR uint8_t *save); FAR uint8_t *save);
static void nsh_consoleexit(FAR struct nsh_vtbl_s *vtbl, int exitstatus); static void nsh_consoleundirect(FAR struct nsh_vtbl_s *vtbl,
FAR uint8_t *save);
static void nsh_consoleexit(FAR struct nsh_vtbl_s *vtbl, int exitstatus)
noreturn_function;
/**************************************************************************** /****************************************************************************
* Private Data * Private Data

@ -109,7 +109,7 @@ struct nsh_vtbl_s
FAR char *(*linebuffer)(FAR struct nsh_vtbl_s *vtbl); FAR char *(*linebuffer)(FAR struct nsh_vtbl_s *vtbl);
void (*redirect)(FAR struct nsh_vtbl_s *vtbl, int fd, FAR uint8_t *save); void (*redirect)(FAR struct nsh_vtbl_s *vtbl, int fd, FAR uint8_t *save);
void (*undirect)(FAR struct nsh_vtbl_s *vtbl, FAR uint8_t *save); void (*undirect)(FAR struct nsh_vtbl_s *vtbl, FAR uint8_t *save);
void (*exit)(FAR struct nsh_vtbl_s *vtbl, int exitstatus); void (*exit)(FAR struct nsh_vtbl_s *vtbl, int exitstatus) noreturn_function;
/* Parser state data */ /* Parser state data */

@ -395,6 +395,17 @@ const char g_nshgreeting[] = "\nNuttShell (NSH) NuttX-" CONFIG_VERSION_STR
const char g_nshgreeting[] = "\nNuttShell (NSH)\n"; const char g_nshgreeting[] = "\nNuttShell (NSH)\n";
#endif #endif
/* Telnet login prompts */
#if defined(CONFIG_NSH_TELNET_LOGIN) && defined(CONFIG_NSH_TELNET)
const char g_telnetgreeting[] = "\nWelcome to NuttShell(NSH) Telnet Server...\n";
const char g_userprompt[] = "login: ";
const char g_passwordprompt[] = "password: ";
const char g_loginsuccess[] = "\nUser Logged-in!\n";
const char g_badcredentials[] = "\nInvalid username or password\n";
const char g_loginfailure[] = "Login failed!\n";
#endif
/* The NSH prompt */ /* The NSH prompt */
const char g_nshprompt[] = "nsh> "; const char g_nshprompt[] = "nsh> ";

@ -43,6 +43,7 @@
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#include <debug.h> #include <debug.h>
#include <string.h>
#include <apps/netutils/telnetd.h> #include <apps/netutils/telnetd.h>
@ -55,6 +56,18 @@
* Pre-processor Definitions * Pre-processor Definitions
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_NSH_TELNET_LOGIN
# define TELNET_IAC 255
# define TELNET_WILL 251
# define TELNET_WONT 252
# define TELNET_DO 253
# define TELNET_DONT 254
# define TELNET_USE_ECHO 1
# define TELNET_NOTUSE_ECHO 0
#endif /* CONFIG_NSH_TELNET_LOGIN */
/**************************************************************************** /****************************************************************************
* Private Types * Private Types
****************************************************************************/ ****************************************************************************/
@ -75,6 +88,91 @@
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: nsh_telnetecho
****************************************************************************/
#ifdef CONFIG_NSH_TELNET_LOGIN
void nsh_telnetecho(struct console_stdio_s *pstate, uint8_t is_use)
{
uint8_t optbuf[4];
optbuf[0] = TELNET_IAC;
optbuf[1] = (is_use == TELNET_USE_ECHO) ? TELNET_WILL : TELNET_DO;
optbuf[2] = 1;
optbuf[3] = 0;
fputs((char *)optbuf, pstate->cn_outstream);
fflush(pstate->cn_outstream);
}
#endif
/****************************************************************************
* Name: nsh_telnetlogin
****************************************************************************/
#ifdef CONFIG_NSH_TELNET_LOGIN
int nsh_telnetlogin(struct console_stdio_s *pstate)
{
char username[16];
char password[16];
uint8_t i;
/* Present the NSH Telnet greeting */
fputs(g_telnetgreeting, pstate->cn_outstream);
fflush(pstate->cn_outstream);
/* Loop for the configured number of retries */
for(i = 0; i < CONFIG_NSH_TELNET_FAILCOUNT; i++)
{
/* Ask for the login username */
fputs(g_userprompt, pstate->cn_outstream);
fflush(pstate->cn_outstream);
if (fgets(pstate->cn_line, CONFIG_NSH_LINELEN, INSTREAM(pstate)) != NULL)
{
strcpy(username, pstate->cn_line);
username[strlen(pstate->cn_line) - 1] = 0;
}
/* Ask for the login password */
fputs(g_passwordprompt, pstate->cn_outstream);
fflush(pstate->cn_outstream);
nsh_telnetecho(pstate, TELNET_NOTUSE_ECHO);
if (fgets(pstate->cn_line, CONFIG_NSH_LINELEN, INSTREAM(pstate)) != NULL)
{
/* Verify the username and password */
strcpy(password,pstate->cn_line);
password[strlen(pstate->cn_line) - 1] = 0;
if (strcmp(password, CONFIG_NSH_TELNET_PASSWORD) == 0 &&
strcmp(username, CONFIG_NSH_TELNET_USERNAME) == 0)
{
fputs(g_loginsuccess, pstate->cn_outstream);
fflush(pstate->cn_outstream);
nsh_telnetecho(pstate, TELNET_USE_ECHO);
return OK;
}
else
{
fputs(g_badcredentials, pstate->cn_outstream);
fflush(pstate->cn_outstream);
}
}
nsh_telnetecho(pstate, TELNET_USE_ECHO);
}
/* Too many failed login attempts */
fputs(g_loginfailure, pstate->cn_outstream);
fflush(pstate->cn_outstream);
return -1;
}
#endif /* CONFIG_NSH_TELNET_LOGIN */
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@ -90,7 +188,17 @@ int nsh_telnetmain(int argc, char *argv[])
dbg("Session [%d] Started\n", getpid()); dbg("Session [%d] Started\n", getpid());
/* Present a greeting */ /* Login User and Password Check */
#ifdef CONFIG_NSH_TELNET_LOGIN
if (nsh_telnetlogin(pstate) != OK)
{
nsh_exit(&pstate->cn_vtbl, 1);
return -1; /* nsh_exit does not return */
}
#endif /* CONFIG_NSH_TELNET_LOGIN */
/* Present the NSH greeting */
fputs(g_nshgreeting, pstate->cn_outstream); fputs(g_nshgreeting, pstate->cn_outstream);
fflush(pstate->cn_outstream); fflush(pstate->cn_outstream);