From 0a93b5b96a3bfcca52a8288e9847f06e495b461e Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Fri, 2 Dec 2022 16:55:49 +0900 Subject: [PATCH] tests: Simplify fake-pinentry to use the option only. * tests/openpgp/fake-pinentry.c (parse_pinentry_user_data): New. (main): Don't use PINENTRY_USER_DATA env var. -- Since environment variable is unreliable, use the option only. Signed-off-by: NIIBE Yutaka --- tests/openpgp/fake-pinentry.c | 72 ++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/tests/openpgp/fake-pinentry.c b/tests/openpgp/fake-pinentry.c index 02c4e0d13..18b60574b 100644 --- a/tests/openpgp/fake-pinentry.c +++ b/tests/openpgp/fake-pinentry.c @@ -196,26 +196,20 @@ option_value (const char *line, const char *name) return NULL; } -int -main (int argc, char **argv) +static int +parse_pinentry_user_data (const char *args, + char **r_passphrase) { - char *args; - char *option_user_data = NULL; char *logfile; char *passphrasefile; char *passphrase; - /* We get our options via PINENTRY_USER_DATA. */ - (void) argc, (void) argv; + *r_passphrase = NULL; - setvbuf (stdin, NULL, _IOLBF, BUFSIZ); - setvbuf (stdout, NULL, _IOLBF, BUFSIZ); + if (log_stream) + fclose (log_stream); + log_stream = NULL; - args = getenv ("PINENTRY_USER_DATA"); - if (! args) - args = ""; - - restart: logfile = option_value (args, "--logfile"); if (logfile) { @@ -230,7 +224,7 @@ main (int argc, char **argv) if (! log_stream) { perror (logfile); - return 1; + return -1; } } @@ -249,20 +243,31 @@ main (int argc, char **argv) { reply ("# Passphrasefile '%s' is empty. Terminating.\n", passphrasefile); - return 1; + return -1; } rstrip (passphrase); } else - { - passphrase = skip_options (args); - if (*passphrase == 0) - passphrase = "no PINENTRY_USER_DATA -- using default passphrase"; - } + passphrase = strdup (skip_options (args)); - reply ("# fake-pinentry(%u) started. Passphrase='%s'.\n", - (unsigned int)getpid (), passphrase); + *r_passphrase = passphrase; + return 0; +} + + +int +main (int argc, char **argv) +{ + char *passphrase = NULL; + + /* We get our options via PINENTRY_USER_DATA. */ + (void) argc, (void) argv; + + setvbuf (stdin, NULL, _IOLBF, BUFSIZ); + setvbuf (stdout, NULL, _IOLBF, BUFSIZ); + + reply ("# fake-pinentry(%u) started.\n", (unsigned int)getpid ()); reply ("OK - what's up?\n"); while (! feof (stdin)) @@ -280,7 +285,12 @@ main (int argc, char **argv) #define OPT_USER_DATA "OPTION pinentry-user-data=" if (strncmp (buffer, "GETPIN", 6) == 0) - reply ("D %s\n", passphrase); + { + if (passphrase) + reply ("D %s\n", passphrase); + else + reply ("D deafult\n"); + } else if (strncmp (buffer, "BYE", 3) == 0) { reply ("OK\n"); @@ -288,13 +298,12 @@ main (int argc, char **argv) } else if (strncmp (buffer, OPT_USER_DATA, strlen (OPT_USER_DATA)) == 0) { - /* Prefer interactive data to the one from environment variable. */ - if (log_stream) - fclose (log_stream); - log_stream = NULL; - free (option_user_data); - option_user_data = args = strdup (buffer + strlen (OPT_USER_DATA)); - goto restart; + if (parse_pinentry_user_data (buffer + strlen (OPT_USER_DATA), + &passphrase) < 0) + { + /* Failure. */ + return 1; + } } reply ("OK\n"); @@ -306,6 +315,7 @@ main (int argc, char **argv) if (log_stream) fclose (log_stream); - free (option_user_data); + if (passphrase) + free (passphrase); return 0; }