]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
wpa_passphrase: Disable terminal echo when reading from stdin
authorAbhiram V <abhi.raa.man.v@gmail.com>
Mon, 21 Nov 2022 16:30:27 +0000 (22:00 +0530)
committerJouni Malinen <j@w1.fi>
Fri, 25 Nov 2022 16:35:53 +0000 (18:35 +0200)
Disable terminal echo using tcgetattr() and tcsetattr() when reading a
passphrase from stdin.

Signed-off-by: Abhiram V <abhi.raa.man.v@gmail.com>
wpa_supplicant/wpa_passphrase.c

index d9c07e673d0b1094d1db6dbf5e0e0ba33f45a46f..cfab4f1b55512d40d686d89cecbaaf35dbdc5a31 100644 (file)
@@ -7,6 +7,7 @@
  */
 
 #include "includes.h"
+#include <termios.h>
 
 #include "common.h"
 #include "crypto/sha1.h"
@@ -14,6 +15,7 @@
 
 int main(int argc, char *argv[])
 {
+       struct termios term;
        unsigned char psk[32];
        int i;
        char *ssid, *passphrase, buf[64], *pos;
@@ -31,11 +33,28 @@ int main(int argc, char *argv[])
        if (argc > 2) {
                passphrase = argv[2];
        } else {
+               bool ctrl_echo;
+
                fprintf(stderr, "# reading passphrase from stdin\n");
+               if (tcgetattr(STDIN_FILENO, &term) < 0) {
+                       perror("tcgetattr");
+                       return 1;
+               }
+               ctrl_echo = term.c_lflag & ECHO;
+               term.c_lflag &= ~ECHO;
+               if (ctrl_echo && tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) {
+                       perror("tcsetattr:error disabling echo");
+                       return 1;
+               }
                if (fgets(buf, sizeof(buf), stdin) == NULL) {
                        fprintf(stderr, "Failed to read passphrase\n");
                        return 1;
                }
+               term.c_lflag |= ECHO;
+               if (ctrl_echo && tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) {
+                       perror("tcsetattr:error enabling echo");
+                       return 1;
+               }
                buf[sizeof(buf) - 1] = '\0';
                pos = buf;
                while (*pos != '\0') {