Skip to content

Commit 75ee4d9

Browse files
Abhishekmishra2808acassis
authored andcommitted
nshlib: Add su, id, and whoami identity commands
Add NSH commands for switching effective credentials and inspecting session identity. Update login and prompt to reflect effective user. Signed-off-by: Abhishek Mishra <mishra.abhishek2808@gmail.com>
1 parent 65854ea commit 75ee4d9

11 files changed

Lines changed: 625 additions & 2 deletions

File tree

nshlib/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ if(CONFIG_NSH_LIBRARY)
4242
nsh_syscmds.c
4343
nsh_dbgcmds.c)
4444

45+
if(CONFIG_SCHED_USER_IDENTITY)
46+
list(APPEND CSRCS nsh_identity.c)
47+
endif()
48+
4549
list(APPEND CSRCS nsh_session.c)
4650

4751
if(CONFIG_NSH_CONSOLE_LOGIN)

nshlib/Kconfig

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ config NSH_PROMPT_STRING
6969
Provide the shell prompt string with size limit NSH_PROMPT_MAX.
7070
default is "nsh> ".
7171

72+
config NSH_PROMPT_STRING_ROOT
73+
string "Prompt string for effective root (euid=0)"
74+
default ""
75+
depends on SCHED_USER_IDENTITY
76+
---help---
77+
If non-empty, NSH uses this prompt when the effective UID is zero.
78+
If empty, the prompt from NSH_PROMPT_STRING (or ENV/HOSTNAME) is used.
79+
Set explicitly for multi-user shells (for example, "nsh# ").
80+
81+
config NSH_PROMPT_STRING_USER
82+
string "Prompt string for non-root effective UID"
83+
default ""
84+
depends on SCHED_USER_IDENTITY
85+
---help---
86+
If non-empty, NSH uses this prompt when the effective UID is non-zero.
87+
If empty, the prompt from NSH_PROMPT_STRING (or ENV/HOSTNAME) is used.
88+
Set explicitly for multi-user shells (for example, "nsh$ ").
89+
7290
config NSH_PROMPT_MAX
7391
int "Maximum Size of Prompt String"
7492
default NAME_MAX
@@ -351,6 +369,21 @@ config NSH_DISABLE_CHOWN
351369
default DEFAULT_SMALL
352370
depends on FS_PERMISSION
353371

372+
config NSH_DISABLE_SU
373+
bool "Disable su"
374+
default DEFAULT_SMALL
375+
depends on SCHED_USER_IDENTITY
376+
377+
config NSH_DISABLE_ID
378+
bool "Disable id"
379+
default DEFAULT_SMALL
380+
depends on SCHED_USER_IDENTITY
381+
382+
config NSH_DISABLE_WHOAMI
383+
bool "Disable whoami"
384+
default DEFAULT_SMALL
385+
depends on SCHED_USER_IDENTITY
386+
354387
config NSH_DISABLE_CP
355388
bool "Disable cp"
356389
default DEFAULT_SMALL
@@ -1259,6 +1292,19 @@ config NSH_LOGIN_FAILCOUNT
12591292
---help---
12601293
Number of login retry attempts.
12611294

1295+
config NSH_LOGIN_SETUID
1296+
bool "Set user identity after successful login"
1297+
default y
1298+
depends on SCHED_USER_IDENTITY
1299+
---help---
1300+
After a successful NSH login, look up the authenticated user name
1301+
in the passwd database and call setuid()/setgid() so that the shell
1302+
session runs with that user's credentials.
1303+
1304+
When CONFIG_LIBC_PASSWD_FILE is enabled, any user listed in the
1305+
passwd file may be selected. Otherwise only the built-in "root"
1306+
account is supported.
1307+
12621308
config NSH_PLATFORM_CHALLENGE
12631309
bool "Platform challenge"
12641310
default n

nshlib/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ CSRCS = nsh_init.c nsh_parse.c nsh_console.c nsh_script.c nsh_system.c
2828
CSRCS += nsh_command.c nsh_fscmds.c nsh_proccmds.c nsh_mmcmds.c
2929
CSRCS += nsh_timcmds.c nsh_envcmds.c nsh_syscmds.c nsh_dbgcmds.c nsh_prompt.c
3030

31+
ifeq ($(CONFIG_SCHED_USER_IDENTITY),y)
32+
CSRCS += nsh_identity.c
33+
endif
34+
3135
CSRCS += nsh_session.c
3236
ifeq ($(CONFIG_NSH_CONSOLE_LOGIN),y)
3337
CSRCS += nsh_login.c

nshlib/nsh.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ extern const char g_userprompt[];
750750
extern const char g_passwordprompt[];
751751
extern const char g_loginsuccess[];
752752
extern const char g_badcredentials[];
753+
extern const char g_badidentity[];
753754
extern const char g_loginfailure[];
754755
#endif
755756
extern const char g_fmtsyntax[];
@@ -969,6 +970,18 @@ int cmd_irqinfo(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
969970
#if defined(CONFIG_FS_PERMISSION) && !defined(CONFIG_NSH_DISABLE_CHOWN)
970971
int cmd_chown(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
971972
#endif
973+
#ifdef CONFIG_SCHED_USER_IDENTITY
974+
int nsh_setuser_identity(FAR const char *username);
975+
# ifndef CONFIG_NSH_DISABLE_SU
976+
int cmd_su(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
977+
# endif
978+
# ifndef CONFIG_NSH_DISABLE_ID
979+
int cmd_id(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
980+
# endif
981+
# ifndef CONFIG_NSH_DISABLE_WHOAMI
982+
int cmd_whoami(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
983+
# endif
984+
#endif
972985
#ifndef CONFIG_NSH_DISABLE_CP
973986
int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
974987
#endif

nshlib/nsh_command.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ static const struct cmdmap_s g_cmdmap[] =
256256
CMD_MAP("free", cmd_free, 1, 1, NULL),
257257
#endif
258258

259+
#if defined(CONFIG_SCHED_USER_IDENTITY) && !defined(CONFIG_NSH_DISABLE_ID)
260+
CMD_MAP("id", cmd_id, 1, 1, NULL),
261+
#endif
262+
259263
#ifdef CONFIG_DEBUG_MM
260264
# ifndef CONFIG_NSH_DISABLE_MEMDUMP
261265
CMD_MAP("memdump", cmd_memdump,
@@ -571,6 +575,10 @@ static const struct cmdmap_s g_cmdmap[] =
571575
#endif
572576
#endif /* CONFIG_NSH_DISABLE_SET */
573577

578+
#if defined(CONFIG_SCHED_USER_IDENTITY) && !defined(CONFIG_NSH_DISABLE_SU)
579+
CMD_MAP("su", cmd_su, 1, 2, "[<username>]"),
580+
#endif
581+
574582
#ifndef CONFIG_NSH_DISABLE_SHUTDOWN
575583
#if defined(CONFIG_BOARDCTL_POWEROFF) && defined(CONFIG_BOARDCTL_RESET)
576584
CMD_MAP("shutdown", cmd_shutdown, 1, 2, "[--reboot]"),
@@ -635,6 +643,10 @@ static const struct cmdmap_s g_cmdmap[] =
635643
# endif
636644
#endif
637645

646+
#if defined(CONFIG_SCHED_USER_IDENTITY) && !defined(CONFIG_NSH_DISABLE_WHOAMI)
647+
CMD_MAP("whoami", cmd_whoami, 1, 1, NULL),
648+
#endif
649+
638650
#ifndef CONFIG_NSH_DISABLE_UNAME
639651
# ifdef CONFIG_NET
640652
CMD_MAP("uname", cmd_uname, 1, 7, "[-a | -imnoprsv]"),

nshlib/nsh_fscmds.c

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@
4545
#include <errno.h>
4646
#include <nuttx/debug.h>
4747

48+
#ifdef CONFIG_LIBC_PASSWD_FILE
49+
# include <pwd.h>
50+
#endif
51+
#ifdef CONFIG_LIBC_GROUP_FILE
52+
# include <grp.h>
53+
#endif
54+
4855
#include "nsh.h"
4956

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

341348
return (strcmp(dir, ".") == 0 || strcmp(dir, "..") == 0);
342349
}
350+
351+
#ifdef CONFIG_SCHED_USER_IDENTITY
352+
/****************************************************************************
353+
* Name: nsh_ls_printowner
354+
****************************************************************************/
355+
356+
static void nsh_ls_printowner(FAR struct nsh_vtbl_s *vtbl, uid_t uid,
357+
gid_t gid)
358+
{
359+
#ifdef CONFIG_LIBC_PASSWD_FILE
360+
FAR struct passwd *pwd;
361+
362+
pwd = getpwuid(uid);
363+
if (pwd != NULL && pwd->pw_name != NULL)
364+
{
365+
nsh_output(vtbl, "%8s", pwd->pw_name);
366+
}
367+
else
368+
#endif
369+
{
370+
nsh_output(vtbl, "%8d", (int)uid);
371+
}
372+
373+
#ifdef CONFIG_LIBC_GROUP_FILE
374+
FAR struct group *grp;
375+
376+
grp = getgrgid(gid);
377+
if (grp != NULL && grp->gr_name != NULL)
378+
{
379+
nsh_output(vtbl, "%8s", grp->gr_name);
380+
}
381+
else
382+
#endif
383+
{
384+
nsh_output(vtbl, "%8d", (int)gid);
385+
}
386+
}
387+
#endif /* CONFIG_SCHED_USER_IDENTITY */
343388
#endif
344389

345390
/****************************************************************************
@@ -511,8 +556,7 @@ static int ls_handler(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
511556
#ifdef CONFIG_SCHED_USER_IDENTITY
512557
if ((lsflags & LSFLAGS_UID_GID) != 0)
513558
{
514-
nsh_output(vtbl, "%8d", buf.st_uid);
515-
nsh_output(vtbl, "%8d", buf.st_gid);
559+
nsh_ls_printowner(vtbl, buf.st_uid, buf.st_gid);
516560
}
517561
#endif
518562

0 commit comments

Comments
 (0)