From d85afdf102295b5f510ae3528eacc5b56593e250 Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Sat, 13 Aug 2016 22:16:29 -0400 Subject: [PATCH] pwmc: Rework pinentry logic for passphrase inquires. Now uses the pinentry when no keyfile was specified for an inquire keyword rather than always prompting on the terminal. --- src/pwmc.c | 100 ++++++++++++++++++++----------------------------------------- 1 file changed, 33 insertions(+), 67 deletions(-) diff --git a/src/pwmc.c b/src/pwmc.c index 98ad6376..20625d3f 100644 --- a/src/pwmc.c +++ b/src/pwmc.c @@ -339,8 +339,15 @@ inquire_cb (void *user, const char *keyword, gpg_error_t rc, || (sign && !sign_keyfile)) { char *tmp; + int local; + /* Try to use the local pinentry between inquires (new/sign/passphrase). + * If --no-pinentry was specified then the passphrase is read from the + * terminal as usual. */ + pwmd_getopt (pwm, PWMD_OPTION_LOCAL_PINENTRY, &local); + pwmd_setopt (pwm, PWMD_OPTION_LOCAL_PINENTRY, 1); rc = pwmd_password (pwm, keyword, &tmp, &inq->len); + pwmd_setopt (pwm, PWMD_OPTION_LOCAL_PINENTRY, local); if (rc && gpg_err_code (rc) != GPG_ERR_EOF) return rc; @@ -1092,6 +1099,7 @@ open_command (char *line) struct inquire_s *inq = NULL; const char *file = line; gpg_error_t rc; + int local; while (file && isspace (*file)) file++; @@ -1117,22 +1125,22 @@ open_command (char *line) if (rc) return rc; + pwmd_getopt (pwm, PWMD_OPTION_LOCAL_PINENTRY, &local); + if (keyfile) { rc = pwmd_setopt (pwm, PWMD_OPTION_OVERRIDE_INQUIRE, 1); if (!rc) - rc = pwmd_setopt (pwm, PWMD_OPTION_NO_PINENTRY, 1); + rc = pwmd_setopt (pwm, PWMD_OPTION_LOCAL_PINENTRY, 1); } else - { - rc = pwmd_setopt (pwm, PWMD_OPTION_OVERRIDE_INQUIRE, 0); - if (!rc) - rc = pwmd_setopt (pwm, PWMD_OPTION_NO_PINENTRY, no_pinentry); - } + rc = pwmd_setopt (pwm, PWMD_OPTION_OVERRIDE_INQUIRE, 0); if (!rc) rc = pwmd_open (pwm, file, inquire_cb, inq); + pwmd_setopt (pwm, PWMD_OPTION_LOCAL_PINENTRY, local); + #ifdef HAVE_LIBREADLINE if (interactive) reset_keyfiles (); @@ -1208,12 +1216,7 @@ set_command (const char *line) else keyfile = pwmd_strdup (value); - if (!rc) - { - rc = pwmd_setopt (pwm, PWMD_OPTION_NO_PINENTRY, 1); - if (!rc) - rc = pwmd_setopt (pwm, PWMD_OPTION_OVERRIDE_INQUIRE, 1); - } + rc = pwmd_setopt (pwm, PWMD_OPTION_OVERRIDE_INQUIRE, 1); } else if (!local_pin && !no_pinentry) { @@ -1221,11 +1224,7 @@ set_command (const char *line) pwmd_socket_type (pwm, &t); if (t == PWMD_SOCKET_LOCAL) - { - rc = pwmd_setopt (pwm, PWMD_OPTION_NO_PINENTRY, 0); - if (!rc) - rc = pwmd_setopt (pwm, PWMD_OPTION_OVERRIDE_INQUIRE, 0); - } + rc = pwmd_setopt (pwm, PWMD_OPTION_OVERRIDE_INQUIRE, 0); } } else if (!strcmp(name, "pinentry-timeout")) @@ -1270,49 +1269,11 @@ set_command (const char *line) } static gpg_error_t -save_command (const char *line) -{ - struct inquire_s *inq = NULL; - gpg_error_t rc; - -#ifdef HAVE_LIBREADLINE - rc = set_inquire (interactive ? STDIN_FILENO : -1, NULL, &inq); -#else - rc = set_inquire (-1, NULL, &inq); -#endif - if (rc) - return rc; - - if (new_keyfile || keyfile || sign_keyfile) - { - rc = pwmd_setopt (pwm, PWMD_OPTION_OVERRIDE_INQUIRE, 1); - if (!rc) - rc = pwmd_setopt (pwm, PWMD_OPTION_NO_PINENTRY, 1); - } - else - { - rc = pwmd_setopt (pwm, PWMD_OPTION_OVERRIDE_INQUIRE, 0); - if (!rc) - rc = pwmd_setopt (pwm, PWMD_OPTION_NO_PINENTRY, no_pinentry); - } - - if (!rc) - rc = pwmd_save (pwm, line, inquire_cb, inq); - -#ifdef HAVE_LIBREADLINE - if (interactive) - reset_keyfiles (); -#endif - - free_inquire (inq); - return rc; -} - -static gpg_error_t do_save_passwd_command (const char *line, int save) { struct inquire_s *inq = NULL; gpg_error_t rc; + int local; #ifdef HAVE_LIBREADLINE rc = set_inquire (interactive ? STDIN_FILENO : -1, NULL, &inq); @@ -1322,18 +1283,16 @@ do_save_passwd_command (const char *line, int save) if (rc) return rc; + pwmd_getopt (pwm, PWMD_OPTION_LOCAL_PINENTRY, &local); + if (new_keyfile || keyfile || sign_keyfile) { rc = pwmd_setopt (pwm, PWMD_OPTION_OVERRIDE_INQUIRE, 1); if (!rc) - rc = pwmd_setopt (pwm, PWMD_OPTION_NO_PINENTRY, 1); + pwmd_setopt (pwm, PWMD_OPTION_LOCAL_PINENTRY, 1); } else - { - rc = pwmd_setopt (pwm, PWMD_OPTION_OVERRIDE_INQUIRE, 0); - if (!rc) - rc = pwmd_setopt (pwm, PWMD_OPTION_NO_PINENTRY, no_pinentry); - } + rc = pwmd_setopt (pwm, PWMD_OPTION_OVERRIDE_INQUIRE, 0); if (!rc) { @@ -1343,6 +1302,8 @@ do_save_passwd_command (const char *line, int save) rc = pwmd_passwd (pwm, line, inquire_cb, inq); } + pwmd_setopt (pwm, PWMD_OPTION_LOCAL_PINENTRY, local); + #ifdef HAVE_LIBREADLINE if (interactive) reset_keyfiles (); @@ -1353,6 +1314,12 @@ do_save_passwd_command (const char *line, int save) } static gpg_error_t +save_command (const char *line) +{ + return do_save_passwd_command (line, 1); +} + +static gpg_error_t parse_dotcommand (const char *line, char **result, size_t * len, struct inquire_s *inq) { @@ -1995,6 +1962,10 @@ main (int argc, char *argv[]) if (rc) goto done; + rc = pwmd_setopt (pwm, PWMD_OPTION_LOCAL_PINENTRY, local_pin); + if (rc) + goto done; + pwmd_setopt (pwm, PWMD_OPTION_PINENTRY_TRIES, tries); if (!quiet) fprintf (stderr, N_("Connecting ...\n")); @@ -2092,11 +2063,6 @@ main (int argc, char *argv[]) goto done; } - rc = pwmd_setopt (pwm, PWMD_OPTION_LOCAL_PINENTRY, - (local_pin || keyfile || new_keyfile || sign_keyfile)); - if (rc) - goto done; - if (pinentry_path) { rc = pwmd_setopt (pwm, PWMD_OPTION_PINENTRY_PATH, pinentry_path); -- 2.11.4.GIT