From 9bb730fb6b75145d9748cfe3995a7904a4be2ae8 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Fri, 3 Mar 2023 14:19:24 +0800 Subject: [PATCH] nsh: Don't echo password during login Signed-off-by: Huang Qi --- nshlib/nsh_login.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/nshlib/nsh_login.c b/nshlib/nsh_login.c index 7edb9676b..c1feb0d54 100644 --- a/nshlib/nsh_login.c +++ b/nshlib/nsh_login.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "fsutils/passwd.h" #ifdef CONFIG_NSH_CLE @@ -150,6 +151,9 @@ int nsh_login(FAR struct console_stdio_s *pstate) #endif int ret; int i; +#ifdef CONFIG_SERIAL_TERMIOS + struct termios cfg; +#endif #ifdef CONFIG_NSH_PLATFORM_SKIP_LOGIN if (platform_skip_login() == OK) @@ -193,9 +197,37 @@ int nsh_login(FAR struct console_stdio_s *pstate) write(OUTFD(pstate), g_passwordprompt, strlen(g_passwordprompt)); + /* Disable ECHO if its a tty device */ + +#ifdef CONFIG_SERIAL_TERMIOS + if (isatty(INFD(pstate))) + { + if (tcgetattr(INFD(pstate), &cfg) == 0) + { + cfg.c_iflag &= ~ECHO; + tcsetattr(INFD(pstate), TCSANOW, &cfg); + } + } +#endif + password[0] = '\0'; - if (readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN, - INFD(pstate), -1) > 0) + ret = readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN, + INFD(pstate), -1); + + /* Enable echo again after password */ + +#ifdef CONFIG_SERIAL_TERMIOS + if (isatty(INFD(pstate))) + { + if (tcgetattr(INFD(pstate), &cfg) == 0) + { + cfg.c_iflag |= ECHO; + tcsetattr(INFD(pstate), TCSANOW, &cfg); + } + } +#endif + + if (ret > 0) { /* Parse out the password */