This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 3a28933ca nshlib: Don't show passwd in telnet login
3a28933ca is described below

commit 3a28933ca88119ddf472ceed88bfebaa16ab6b90
Author: Huang Qi <huang...@xiaomi.com>
AuthorDate: Sat Apr 22 10:15:42 2023 +0800

    nshlib: Don't show passwd in telnet login
    
    Current implementation is broken, in this patch ECHO is
    disabled by termios.
    
    This patch works with https://github.com/apache/nuttx/pull/8950
    
    Signed-off-by: Huang Qi <huang...@xiaomi.com>
---
 nshlib/nsh_telnetlogin.c | 50 ++++++++++++++++++++++++++++--------------------
 1 file changed, 29 insertions(+), 21 deletions(-)

diff --git a/nshlib/nsh_telnetlogin.c b/nshlib/nsh_telnetlogin.c
index c2ec13a53..3167fb93c 100644
--- a/nshlib/nsh_telnetlogin.c
+++ b/nshlib/nsh_telnetlogin.c
@@ -27,6 +27,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <unistd.h>
+#include <termios.h>
 
 #include "fsutils/passwd.h"
 
@@ -53,21 +54,6 @@
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: nsh_telnetecho
- ****************************************************************************/
-
-static void nsh_telnetecho(FAR struct console_stdio_s *pstate,
-                           uint8_t is_use)
-{
-  uint8_t optbuf[4];
-  optbuf[0] = TELNET_IAC;
-  optbuf[1] = (is_use == TELNET_USE_ECHO) ? TELNET_WILL : TELNET_DO;
-  optbuf[2] = 1;
-  optbuf[3] = 0;
-  write(OUTFD(pstate), optbuf, strlen((FAR const char *)optbuf));
-}
-
 /****************************************************************************
  * Name: nsh_telnettoken
  ****************************************************************************/
@@ -166,7 +152,9 @@ int nsh_telnetlogin(FAR struct console_stdio_s *pstate)
 #ifdef CONFIG_NSH_PLATFORM_CHALLENGE
   char challenge[128];
 #endif
+  struct termios cfg;
   int i;
+  int ret;
 
 #ifdef CONFIG_NSH_PLATFORM_SKIP_LOGIN
   if (platform_skip_login() == OK)
@@ -211,11 +199,34 @@ int nsh_telnetlogin(FAR struct console_stdio_s *pstate)
       /* Ask for the login password */
 
       write(OUTFD(pstate), g_passwordprompt, strlen(g_passwordprompt));
-      nsh_telnetecho(pstate, TELNET_NOTUSE_ECHO);
+
+      /* Disable ECHO if its a tty device */
+
+      if (isatty(INFD(pstate)))
+        {
+          if (tcgetattr(INFD(pstate), &cfg) == 0)
+            {
+              cfg.c_lflag &= ~ECHO;
+              tcsetattr(INFD(pstate), TCSANOW, &cfg);
+            }
+        }
 
       password[0] = '\0';
-      if (readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
-                      INFD(pstate), OUTFD(pstate)) >= 0)
+      ret = readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
+                      INFD(pstate), OUTFD(pstate));
+
+      /* Enable echo again after password */
+
+      if (isatty(INFD(pstate)))
+        {
+          if (tcgetattr(INFD(pstate), &cfg) == 0)
+            {
+              cfg.c_lflag |= ECHO;
+              tcsetattr(INFD(pstate), TCSANOW, &cfg);
+            }
+        }
+
+      if (ret >= 0)
         {
           /* Parse out the password */
 
@@ -242,7 +253,6 @@ int nsh_telnetlogin(FAR struct console_stdio_s *pstate)
 #endif
             {
               write(OUTFD(pstate), g_loginsuccess, strlen(g_loginsuccess));
-              nsh_telnetecho(pstate, TELNET_USE_ECHO);
               return OK;
             }
           else
@@ -254,8 +264,6 @@ int nsh_telnetlogin(FAR struct console_stdio_s *pstate)
 #endif
             }
         }
-
-      nsh_telnetecho(pstate, TELNET_USE_ECHO);
     }
 
   /* Too many failed login attempts */

Reply via email to