apps/nshlib: Add useradd, userdel, and passwd commends

This commit is contained in:
Gregory Nutt 2016-01-20 09:37:25 -06:00
parent 5aa177e03b
commit fe05a8444f
7 changed files with 193 additions and 9 deletions

View File

@ -1512,4 +1512,6 @@
netlib_get_ipv4netmask(). From Pelle Windestam (2016-01-14).
* apps/fsutils/passwd: Utility library for accessing a password
file like /etc/passwd (2016-01-19).
* apps/nshlib/nsh_passwdcmds.c: Add useradd, userdel, and passwd
commands (2019-01-20).

View File

@ -338,6 +338,11 @@ config NSH_DISABLE_NSLOOKUP
default n
depends on LIBC_NETDB && NETDB_DNSCLIENT
config NSH_DISABLE_PASSWD
bool "Disable passwd"
default y
depends on FSUTILS_PASSWD
config NSH_DISABLE_POWEROFF
bool "Disable poweroff"
default n if !DEFAULT_SMALL && !BOARDCTL_RESET
@ -429,6 +434,16 @@ config NSH_DISABLE_URLENCODE
default n if !DEFAULT_SMALL
depends on NETUTILS_CODECS && CODECS_URLCODE
config NSH_DISABLE_USERADD
bool "Disable useradd"
default y
depends on FSUTILS_PASSWD
config NSH_DISABLE_USERDEL
bool "Disable userdel"
default y
depends on FSUTILS_PASSWD
config NSH_DISABLE_USLEEP
bool "Disable usleep"
default n

View File

@ -111,6 +111,11 @@ ifeq ($(CONFIG_NETUTILS_CODECS),y)
CSRCS += nsh_codeccmd.c
endif
ifeq ($(CONFIG_FSUTILS_PASSWD),y)
CSRCS += nsh_passwdcmds.c
endif
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))

View File

@ -829,6 +829,10 @@ o nslookup <host-name>
Lookup and print the IP address associated with <host-name>
o passwd <username> <password>
Set the password for the existing user <username> to <password>
o ping [-c <count>] [-i <interval>] <ip-address>
ping6 [-c <count>] [-i <interval>] <ip-address>
@ -1085,6 +1089,14 @@ o uname [-a | -imnoprsv]
-p Print "unknown"
o useradd <username> <password>
Add a new user with <username> and <password>
o userdel <username>
Delete the user with the name <username>
o usleep <usec>
Pause execution (sleep) of <usec> microseconds.
@ -1167,6 +1179,7 @@ Command Dependencies on Configuration Settings
mv (((!CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_WRITABLE) || !CONFIG_DISABLE_PSEUDOFS_OPERATIONS) && CONFIG_NFILE_DESCRIPTORS > 0) (see note 4)
nfsmount !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NET && CONFIG_NFS
nslookup CONFIG_LIBC_NETDB && CONFIG_NETDB_DNSCLIENT
password !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FS_WRITABLE && CONFIG_FSUTILS_PASSWD
ping CONFIG_NET && CONFIG_NET_ICMP && CONFIG_NET_ICMP_PING && !CONFIG_DISABLE_SIGNALS
ping6 CONFIG_NET && CONFIG_NET_ICMPv6 && CONFIG_NET_ICMPv6_PING && !CONFIG_DISABLE_SIGNALS
poweroff CONFIG_BOARDCTL_POWEROFF
@ -1188,6 +1201,8 @@ Command Dependencies on Configuration Settings
unset !CONFIG_DISABLE_ENVIRON
urldecode CONFIG_NETUTILS_CODECS && CONFIG_CODECS_URLCODE
urlencode CONFIG_NETUTILS_CODECS && CONFIG_CODECS_URLCODE
useradd !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FS_WRITABLE && CONFIG_FSUTILS_PASSWD
userdel !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FS_WRITABLE && CONFIG_FSUTILS_PASSWD
usleep !CONFIG_DISABLE_SIGNALS
get CONFIG_NET && CONFIG_NET_TCP && CONFIG_NFILE_DESCRIPTORS > 0
xd ---
@ -1219,13 +1234,14 @@ also allow it to squeeze into very small memory footprints.
CONFIG_NSH_DISABLE_MKFIFO, CONFIG_NSH_DISABLE_MKRD, CONFIG_NSH_DISABLE_MH,
CONFIG_NSH_DISABLE_MODCMDS, CONFIG_NSH_DISABLE_MOUNT, CONFIG_NSH_DISABLE_MW,
CONFIG_NSH_DISABLE_MV, CONFIG_NSH_DISABLE_NFSMOUNT, CONFIG_NSH_DISABLE_NSLOOKUP,
CONFIG_NSH_DISABLE_POWEROFF, CONFIG_NSH_DISABLE_PS, CONFIG_NSH_DISABLE_PING,
CONFIG_NSH_DISABLE_PING6, CONFIG_NSH_DISABLE_PUT, CONFIG_NSH_DISABLE_PWD,
CONFIG_NSH_DISABLE_REBOOT, CONFIG_NSH_DISABLE_RM, CONFIG_NSH_DISABLE_RMDIR,
CONFIG_NSH_DISABLE_SET, CONFIG_NSH_DISABLE_SH, CONFIG_NSH_DISABLE_SHUTDOWN,
CONFIG_NSH_DISABLE_SLEEP, CONFIG_NSH_DISABLE_TEST, CONFIG_NSH_DIABLE_TIME,
CONFIG_NSH_DISABLE_UMOUNT, CONFIG_NSH_DISABLE_UNSET, CONFIG_NSH_DISABLE_URLDECODE,
CONFIG_NSH_DISABLE_URLENCODE, CONFIG_NSH_DISABLE_USLEEP, CONFIG_NSH_DISABLE_WGET,
CONFIG_NSH_DISABLE_PASSWD, CONFIG_NSH_DISABLE_PING, CONFIG_NSH_DISABLE_PING6,
CONFIG_NSH_DISABLE_POWEROFF, CONFIG_NSH_DISABLE_PS, CONFIG_NSH_DISABLE_PUT,
CONFIG_NSH_DISABLE_PWD, CONFIG_NSH_DISABLE_REBOOT, CONFIG_NSH_DISABLE_RM,
CONFIG_NSH_DISABLE_RMDIR, CONFIG_NSH_DISABLE_SET, CONFIG_NSH_DISABLE_SH,
CONFIG_NSH_DISABLE_SHUTDOWN, CONFIG_NSH_DISABLE_SLEEP, CONFIG_NSH_DISABLE_TEST,
CONFIG_NSH_DIABLE_TIME, CONFIG_NSH_DISABLE_UMOUNT, CONFIG_NSH_DISABLE_UNSET,
CONFIG_NSH_DISABLE_URLDECODE, CONFIG_NSH_DISABLE_URLENCODE, CONFIG_NSH_DISABLE_USERADD,
CONFIG_NSH_DISABLE_USERDEL, CONFIG_NSH_DISABLE_USLEEP, CONFIG_NSH_DISABLE_WGET,
CONFIG_NSH_DISABLE_XD
Verbose help output can be suppressed by defining CONFIG_NSH_HELP_TERSE. In that

View File

@ -1,7 +1,7 @@
/****************************************************************************
* apps/nshlib/nsh.h
*
* Copyright (C) 2007-2014 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -1035,11 +1035,22 @@ int cmd_lsmod(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
int cmd_mkfatfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
# endif
# endif /* CONFIG_FS_FAT */
#if defined(CONFIG_FS_SMARTFS) && defined(CONFIG_FSUTILS_MKSMARTFS)
# if defined(CONFIG_FS_SMARTFS) && defined(CONFIG_FSUTILS_MKSMARTFS)
# ifndef CONFIG_NSH_DISABLE_MKSMARTFS
int cmd_mksmartfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
# endif
# endif /* CONFIG_FS_SMARTFS */
# if defined(CONFIG_FSUTILS_PASSWD) && defined(CONFIG_FS_WRITABLE)
# ifndef CONFIG_NSH_DISABLE_USERADD
int cmd_useradd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
# endif
# ifndef CONFIG_NSH_DISABLE_USERDEL
int cmd_userdel(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
# endif
# ifndef CONFIG_NSH_DISABLE_PASSWD
int cmd_passwd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
# endif
# endif
# endif /* !CONFIG_DISABLE_MOUNTPOINT */
# if !defined(CONFIG_DISABLE_ENVIRON)
# ifndef CONFIG_NSH_DISABLE_CD

View File

@ -351,6 +351,13 @@ static const struct cmdmap_s g_cmdmap[] =
{ "nslookup", cmd_nslookup, 2, 2, "<host-name>" },
#endif
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && \
defined(CONFIG_FS_WRITABLE) && defined(CONFIG_FSUTILS_PASSWD)
# ifndef CONFIG_NSH_DISABLE_PASSWD
{ "passwd", cmd_passwd, 3, 3, "<username> <password>" },
# endif
#endif
#if defined(CONFIG_NET) && defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && !defined(CONFIG_DISABLE_SIGNALS)
# ifndef CONFIG_NSH_DISABLE_PING
{ "ping", cmd_ping, 2, 6, "[-c <count>] [-i <interval>] <ip-address>" },
@ -472,6 +479,16 @@ static const struct cmdmap_s g_cmdmap[] =
# endif
#endif
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && \
defined(CONFIG_FS_WRITABLE) && defined(CONFIG_FSUTILS_PASSWD)
# ifndef CONFIG_NSH_DISABLE_USERADD
{ "useradd", cmd_useradd, 3, 3, "<username> <password>" },
# endif
# ifndef CONFIG_NSH_DISABLE_USERDEL
{ "userdel", cmd_userdel, 2, 2, "<username>" },
# endif
#endif
#ifndef CONFIG_DISABLE_SIGNALS
# ifndef CONFIG_NSH_DISABLE_USLEEP
{ "usleep", cmd_usleep, 2, 2, "<usec>" },

118
nshlib/nsh_passwdcmds.c Normal file
View File

@ -0,0 +1,118 @@
/****************************************************************************
* apps/nshlib/nsh_passwdcmds.c
*
* Copyright (C) 2016 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 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 <apps/fsutils/passwd.h>
#include "nsh.h"
#include "nsh_console.h"
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && \
defined(CONFIG_FS_WRITABLE) && defined(CONFIG_FSUTILS_PASSWD)
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: cmd_useradd
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_USERADD
int cmd_useradd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
int ret;
ret = passwd_adduser(argv[1], argv[2]);
if (ret < 0)
{
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "passwd_adduser",
NSH_ERRNO_OF(-ret));
return ERROR;
}
return OK;
}
#endif /* !CONFIG_NSH_DISABLE_USERADD */
/****************************************************************************
* Name: cmd_userdel
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_USERDEL
int cmd_userdel(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
int ret;
ret = passwd_deluser(argv[1]);
if (ret < 0)
{
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "passwd_deluser",
NSH_ERRNO_OF(-ret));
return ERROR;
}
return OK;
}
#endif /* !CONFIG_NSH_DISABLE_USERDEL */
/****************************************************************************
* Name: cmd_useradd
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_PASSWD
int cmd_passwd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
int ret;
ret = passwd_update(argv[1], argv[2]);
if (ret < 0)
{
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "passwd_update",
NSH_ERRNO_OF(-ret));
return ERROR;
}
return OK;
}
#endif /* !CONFIG_NSH_DISABLE_USERADD */
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 &&
* CONFIG_FS_WRITABLE && CONFIG_FSUTILS_PASSWD */