Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions nshlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ if(CONFIG_NSH_LIBRARY)
nsh_syscmds.c
nsh_dbgcmds.c)

if(CONFIG_SCHED_USER_IDENTITY)
list(APPEND CSRCS nsh_identity.c)
endif()

list(APPEND CSRCS nsh_session.c)

if(CONFIG_NSH_CONSOLE_LOGIN)
Expand Down
46 changes: 46 additions & 0 deletions nshlib/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ config NSH_PROMPT_STRING
Provide the shell prompt string with size limit NSH_PROMPT_MAX.
default is "nsh> ".

config NSH_PROMPT_STRING_ROOT
string "Prompt string for effective root (euid=0)"
default ""
depends on SCHED_USER_IDENTITY
---help---
If non-empty, NSH uses this prompt when the effective UID is zero.
If empty, the prompt from NSH_PROMPT_STRING (or ENV/HOSTNAME) is used.
Set explicitly for multi-user shells (for example, "nsh# ").

config NSH_PROMPT_STRING_USER
string "Prompt string for non-root effective UID"
default ""
depends on SCHED_USER_IDENTITY
---help---
If non-empty, NSH uses this prompt when the effective UID is non-zero.
If empty, the prompt from NSH_PROMPT_STRING (or ENV/HOSTNAME) is used.
Set explicitly for multi-user shells (for example, "nsh$ ").

config NSH_PROMPT_MAX
int "Maximum Size of Prompt String"
default NAME_MAX
Expand Down Expand Up @@ -351,6 +369,21 @@ config NSH_DISABLE_CHOWN
default DEFAULT_SMALL
depends on FS_PERMISSION

config NSH_DISABLE_SU
bool "Disable su"
default DEFAULT_SMALL
depends on SCHED_USER_IDENTITY

config NSH_DISABLE_ID
bool "Disable id"
default DEFAULT_SMALL
depends on SCHED_USER_IDENTITY

config NSH_DISABLE_WHOAMI
bool "Disable whoami"
default DEFAULT_SMALL
depends on SCHED_USER_IDENTITY

config NSH_DISABLE_CP
bool "Disable cp"
default DEFAULT_SMALL
Expand Down Expand Up @@ -1259,6 +1292,19 @@ config NSH_LOGIN_FAILCOUNT
---help---
Number of login retry attempts.

config NSH_LOGIN_SETUID
bool "Set user identity after successful login"
default y
depends on SCHED_USER_IDENTITY
---help---
After a successful NSH login, look up the authenticated user name
in the passwd database and call setuid()/setgid() so that the shell
session runs with that user's credentials.

When CONFIG_LIBC_PASSWD_FILE is enabled, any user listed in the
passwd file may be selected. Otherwise only the built-in "root"
account is supported.

config NSH_PLATFORM_CHALLENGE
bool "Platform challenge"
default n
Expand Down
4 changes: 4 additions & 0 deletions nshlib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ CSRCS = nsh_init.c nsh_parse.c nsh_console.c nsh_script.c nsh_system.c
CSRCS += nsh_command.c nsh_fscmds.c nsh_proccmds.c nsh_mmcmds.c
CSRCS += nsh_timcmds.c nsh_envcmds.c nsh_syscmds.c nsh_dbgcmds.c nsh_prompt.c

ifeq ($(CONFIG_SCHED_USER_IDENTITY),y)
CSRCS += nsh_identity.c
endif

CSRCS += nsh_session.c
ifeq ($(CONFIG_NSH_CONSOLE_LOGIN),y)
CSRCS += nsh_login.c
Expand Down
13 changes: 13 additions & 0 deletions nshlib/nsh.h
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ extern const char g_userprompt[];
extern const char g_passwordprompt[];
extern const char g_loginsuccess[];
extern const char g_badcredentials[];
extern const char g_badidentity[];
extern const char g_loginfailure[];
#endif
extern const char g_fmtsyntax[];
Expand Down Expand Up @@ -969,6 +970,18 @@ int cmd_irqinfo(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
#if defined(CONFIG_FS_PERMISSION) && !defined(CONFIG_NSH_DISABLE_CHOWN)
int cmd_chown(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
#endif
#ifdef CONFIG_SCHED_USER_IDENTITY
int nsh_setuser_identity(FAR const char *username);
# ifndef CONFIG_NSH_DISABLE_SU
int cmd_su(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
# endif
# ifndef CONFIG_NSH_DISABLE_ID
int cmd_id(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
# endif
# ifndef CONFIG_NSH_DISABLE_WHOAMI
int cmd_whoami(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
# endif
#endif
#ifndef CONFIG_NSH_DISABLE_CP
int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
#endif
Expand Down
12 changes: 12 additions & 0 deletions nshlib/nsh_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ static const struct cmdmap_s g_cmdmap[] =
CMD_MAP("free", cmd_free, 1, 1, NULL),
#endif

#if defined(CONFIG_SCHED_USER_IDENTITY) && !defined(CONFIG_NSH_DISABLE_ID)
CMD_MAP("id", cmd_id, 1, 1, NULL),
#endif

#ifdef CONFIG_DEBUG_MM
# ifndef CONFIG_NSH_DISABLE_MEMDUMP
CMD_MAP("memdump", cmd_memdump,
Expand Down Expand Up @@ -571,6 +575,10 @@ static const struct cmdmap_s g_cmdmap[] =
#endif
#endif /* CONFIG_NSH_DISABLE_SET */

#if defined(CONFIG_SCHED_USER_IDENTITY) && !defined(CONFIG_NSH_DISABLE_SU)
CMD_MAP("su", cmd_su, 1, 2, "[<username>]"),
#endif

#ifndef CONFIG_NSH_DISABLE_SHUTDOWN
#if defined(CONFIG_BOARDCTL_POWEROFF) && defined(CONFIG_BOARDCTL_RESET)
CMD_MAP("shutdown", cmd_shutdown, 1, 2, "[--reboot]"),
Expand Down Expand Up @@ -635,6 +643,10 @@ static const struct cmdmap_s g_cmdmap[] =
# endif
#endif

#if defined(CONFIG_SCHED_USER_IDENTITY) && !defined(CONFIG_NSH_DISABLE_WHOAMI)
CMD_MAP("whoami", cmd_whoami, 1, 1, NULL),
#endif

#ifndef CONFIG_NSH_DISABLE_UNAME
# ifdef CONFIG_NET
CMD_MAP("uname", cmd_uname, 1, 7, "[-a | -imnoprsv]"),
Expand Down
48 changes: 46 additions & 2 deletions nshlib/nsh_fscmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
#include <errno.h>
#include <nuttx/debug.h>

#ifdef CONFIG_LIBC_PASSWD_FILE
# include <pwd.h>
#endif
#ifdef CONFIG_LIBC_GROUP_FILE
# include <grp.h>
#endif

#include "nsh.h"

#if !defined(CONFIG_DISABLE_MOUNTPOINT)
Expand Down Expand Up @@ -340,6 +347,44 @@ static inline int ls_specialdir(FAR const char *dir)

return (strcmp(dir, ".") == 0 || strcmp(dir, "..") == 0);
}

#ifdef CONFIG_SCHED_USER_IDENTITY
/****************************************************************************
* Name: nsh_ls_printowner
****************************************************************************/

static void nsh_ls_printowner(FAR struct nsh_vtbl_s *vtbl, uid_t uid,
gid_t gid)
{
#ifdef CONFIG_LIBC_PASSWD_FILE
FAR struct passwd *pwd;

pwd = getpwuid(uid);
if (pwd != NULL && pwd->pw_name != NULL)
{
nsh_output(vtbl, "%8s", pwd->pw_name);
}
else
#endif
{
nsh_output(vtbl, "%8d", (int)uid);
}

#ifdef CONFIG_LIBC_GROUP_FILE
FAR struct group *grp;

grp = getgrgid(gid);
if (grp != NULL && grp->gr_name != NULL)
{
nsh_output(vtbl, "%8s", grp->gr_name);
}
else
#endif
{
nsh_output(vtbl, "%8d", (int)gid);
}
}
#endif /* CONFIG_SCHED_USER_IDENTITY */
#endif

/****************************************************************************
Expand Down Expand Up @@ -511,8 +556,7 @@ static int ls_handler(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
#ifdef CONFIG_SCHED_USER_IDENTITY
if ((lsflags & LSFLAGS_UID_GID) != 0)
{
nsh_output(vtbl, "%8d", buf.st_uid);
nsh_output(vtbl, "%8d", buf.st_gid);
nsh_ls_printowner(vtbl, buf.st_uid, buf.st_gid);
}
#endif

Expand Down
Loading
Loading