Skip to content

Commit 9bb730f

Browse files
no1wudixiaoxiang781216
authored andcommitted
nsh: Don't echo password during login
Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
1 parent 2e49d78 commit 9bb730f

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

nshlib/nsh_login.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <string.h>
2828
#include <ctype.h>
2929
#include <unistd.h>
30+
#include <termios.h>
3031

3132
#include "fsutils/passwd.h"
3233
#ifdef CONFIG_NSH_CLE
@@ -150,6 +151,9 @@ int nsh_login(FAR struct console_stdio_s *pstate)
150151
#endif
151152
int ret;
152153
int i;
154+
#ifdef CONFIG_SERIAL_TERMIOS
155+
struct termios cfg;
156+
#endif
153157

154158
#ifdef CONFIG_NSH_PLATFORM_SKIP_LOGIN
155159
if (platform_skip_login() == OK)
@@ -193,9 +197,37 @@ int nsh_login(FAR struct console_stdio_s *pstate)
193197

194198
write(OUTFD(pstate), g_passwordprompt, strlen(g_passwordprompt));
195199

200+
/* Disable ECHO if its a tty device */
201+
202+
#ifdef CONFIG_SERIAL_TERMIOS
203+
if (isatty(INFD(pstate)))
204+
{
205+
if (tcgetattr(INFD(pstate), &cfg) == 0)
206+
{
207+
cfg.c_iflag &= ~ECHO;
208+
tcsetattr(INFD(pstate), TCSANOW, &cfg);
209+
}
210+
}
211+
#endif
212+
196213
password[0] = '\0';
197-
if (readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
198-
INFD(pstate), -1) > 0)
214+
ret = readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
215+
INFD(pstate), -1);
216+
217+
/* Enable echo again after password */
218+
219+
#ifdef CONFIG_SERIAL_TERMIOS
220+
if (isatty(INFD(pstate)))
221+
{
222+
if (tcgetattr(INFD(pstate), &cfg) == 0)
223+
{
224+
cfg.c_iflag |= ECHO;
225+
tcsetattr(INFD(pstate), TCSANOW, &cfg);
226+
}
227+
}
228+
#endif
229+
230+
if (ret > 0)
199231
{
200232
/* Parse out the password */
201233

0 commit comments

Comments
 (0)