]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/agetpass.[ch]: add function ro read from pipe
authorTomas Halman <tomas@halman.net>
Thu, 25 Jan 2024 09:56:10 +0000 (10:56 +0100)
committerSerge Hallyn <serge@hallyn.com>
Thu, 1 Feb 2024 04:16:02 +0000 (22:16 -0600)
Add alternative function to agetpass for reading password
from stdin or pipe.

Signed-off-by: Tomas Halman <tomas@halman.net>
lib/agetpass.c
lib/agetpass.h

index b06c3d81d79a4821ff0604fc8987963756dfaff2..11809ab74b08de8b8da790649c5f32741ded2506 100644 (file)
@@ -33,6 +33,7 @@
  * SYNOPSIS
  *     [[gnu::malloc(erase_pass)]]
  *     char *agetpass(const char *prompt);
+ *     char *agetpass_stdin();
  *
  *     void erase_pass(char *pass);
  *
  *     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)
index 1ee6a10702bc0de2c9bb70fa7ea2bc49368bc3db..4a4444e1dd25c5515cdc062575c8ede70ca9c2c1 100644 (file)
@@ -17,6 +17,7 @@
 void erase_pass(char *pass);
 ATTR_MALLOC(erase_pass)
 char *agetpass(const char *prompt);
+char *agetpass_stdin();
 
 
 #endif  // include guard