From 3fff9d762184e076cf6654f587d1284b3d58bea6 Mon Sep 17 00:00:00 2001 From: Tomas Halman Date: Thu, 25 Jan 2024 10:56:10 +0100 Subject: [PATCH] lib/agetpass.[ch]: add function ro read from pipe Add alternative function to agetpass for reading password from stdin or pipe. Signed-off-by: Tomas Halman --- lib/agetpass.c | 22 +++++++++++++++++++--- lib/agetpass.h | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/agetpass.c b/lib/agetpass.c index b06c3d81d..11809ab74 100644 --- a/lib/agetpass.c +++ b/lib/agetpass.c @@ -33,6 +33,7 @@ * SYNOPSIS * [[gnu::malloc(erase_pass)]] * char *agetpass(const char *prompt); + * char *agetpass_stdin(); * * void erase_pass(char *pass); * @@ -65,6 +66,10 @@ * erased by calling erase_pass(), to avoid possibly leaking the * password. * + * agetpass_stdin() + * This function is the same as previous one (agetpass). Just the + * password is read from stdin and terminal is not required. + * * erase_pass() * This function first clears the password, by calling * explicit_bzero(3) (or an equivalent call), and then frees the @@ -93,8 +98,8 @@ */ -char * -agetpass(const char *prompt) +static char * +agetpass_internal(const char *prompt, int flags) { char *pass; size_t len; @@ -111,7 +116,7 @@ agetpass(const char *prompt) if (pass == NULL) return NULL; - if (readpassphrase(prompt, pass, PASS_MAX + 2, RPP_REQUIRE_TTY) == NULL) + if (readpassphrase(prompt, pass, PASS_MAX + 2, flags) == NULL) goto fail; len = strlen(pass); @@ -127,6 +132,17 @@ fail: return NULL; } +char * +agetpass(const char *prompt) +{ + return agetpass_internal(prompt, RPP_REQUIRE_TTY); +} + +char * +agetpass_stdin() +{ + return agetpass_internal(NULL, RPP_STDIN); +} void erase_pass(char *pass) diff --git a/lib/agetpass.h b/lib/agetpass.h index 1ee6a1070..4a4444e1d 100644 --- a/lib/agetpass.h +++ b/lib/agetpass.h @@ -17,6 +17,7 @@ void erase_pass(char *pass); ATTR_MALLOC(erase_pass) char *agetpass(const char *prompt); +char *agetpass_stdin(); #endif // include guard -- 2.47.2