From 054b24d5837cb32f94b6b659620caca2b567e4f6 Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Thu, 15 Sep 2022 18:19:19 +0200 Subject: [PATCH] tools: replace getpass() Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2124030 Resolves: https://gitlab.freedesktop.org/realmd/adcli/-/issues/10 --- tools/tools.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/tools/tools.c b/tools/tools.c index c78548b..7e382ae 100644 --- a/tools/tools.c +++ b/tools/tools.c @@ -36,6 +36,7 @@ #include #include #include +#include static char *adcli_temp_directory = NULL; @@ -208,6 +209,47 @@ command_usage (void) printf ("\nSee 'adcli --help' for more information\n"); } +static char *get_password (const char *prompt) +{ + int ret; + struct termios termios; + struct termios orig_termios; + char *buf = NULL; + size_t buf_len = 0; + + ret = tcgetattr (fileno (stdin), &termios); + if (ret != 0 ) { + return NULL; + } + + orig_termios = termios; + termios.c_lflag &= ~ECHO; + + ret = tcsetattr (fileno (stdin), TCSAFLUSH, &termios); + if (ret != 0) { + return NULL; + } + + fprintf (stdout, "%s", prompt); + fflush (stdout); + + ret = getline (&buf, &buf_len, stdin); + tcsetattr (fileno (stdin), TCSAFLUSH, &orig_termios); + if (ret <= 0) { + free (buf); + return NULL; + } + + if (buf[ret - 1] == '\n') { + /* remove new-line character from the end of the buffer and + * echo it to stdout */ + buf[ret - 1] = '\0'; + fprintf (stdout, "\n"); + } + + return buf; +} + char * adcli_prompt_password_func (adcli_login_type login_type, const char *name, @@ -221,7 +263,7 @@ adcli_prompt_password_func (adcli_login_type login_type, if (asprintf (&prompt, "Password for %s: ", name) < 0) return_val_if_reached (NULL); - password = getpass (prompt); + password = get_password (prompt); free (prompt); if (password == NULL) @@ -229,6 +271,7 @@ adcli_prompt_password_func (adcli_login_type login_type, result = strdup (password); adcli_mem_clear (password, strlen (password)); + free (password); return result; } -- 2.33.0