diff mbox series

wpa_passphrase: Disable terminal echo when reading from stdin

Message ID 20221121163027.11210-1-abhi.raa.man.v@gmail.com
State Accepted
Headers show
Series wpa_passphrase: Disable terminal echo when reading from stdin | expand

Commit Message

Abhiram V Nov. 21, 2022, 4:30 p.m. UTC
Disable terminal echo using tcgetattr() and tcsetattr() when reading
passphrase from stdin.

Signed-off-by: Abhiram V <abhi.raa.man.v@gmail.com>
---
 wpa_supplicant/wpa_passphrase.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

Comments

Jouni Malinen Nov. 25, 2022, 5:24 p.m. UTC | #1
On Mon, Nov 21, 2022 at 10:00:27PM +0530, Abhiram V wrote:
> Disable terminal echo using tcgetattr() and tcsetattr() when reading
> passphrase from stdin.

Thanks, applied with ECHO flag re-enabling fixed:

> +		term.c_lflag &= ~ECHO;

> +		term.c_cflag |= ECHO;

Wrong control flag used here..
diff mbox series

Patch

diff --git a/wpa_supplicant/wpa_passphrase.c b/wpa_supplicant/wpa_passphrase.c
index d9c07e673..4f185da5b 100644
--- a/wpa_supplicant/wpa_passphrase.c
+++ b/wpa_supplicant/wpa_passphrase.c
@@ -10,10 +10,12 @@ 
 
 #include "common.h"
 #include "crypto/sha1.h"
+#include <termios.h>
 
 
 int main(int argc, char *argv[])
 {
+	struct termios term;
 	unsigned char psk[32];
 	int i;
 	char *ssid, *passphrase, buf[64], *pos;
@@ -32,10 +34,24 @@  int main(int argc, char *argv[])
 		passphrase = argv[2];
 	} else {
 		fprintf(stderr, "# reading passphrase from stdin\n");
+		if (tcgetattr(STDIN_FILENO, &term) < 0) {
+			perror("tcgetattr");
+			return 1;
+		}
+		term.c_lflag &= ~ECHO;
+		if (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_cflag |= ECHO;
+		if (tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) {
+			perror("tcsetattr:error enabling echo");
+			return 1;
+		}
 		buf[sizeof(buf) - 1] = '\0';
 		pos = buf;
 		while (*pos != '\0') {