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 <tomas@halman.net>
This commit is contained in:
Tomas Halman
2024-01-25 10:56:10 +01:00
committed by Serge Hallyn
parent 341d80c2c7
commit 3fff9d7621
2 changed files with 20 additions and 3 deletions

View File

@@ -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)

View 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