From 8796b8736a7221d502721a18b193798f7e7940db Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Thu, 20 Mar 2008 19:16:28 -0400 Subject: [PATCH] Don't check if the file is cached in pwmd_open_nb_finalize(). The server settings may require a password for the next OPEN or SAVE command. A couple of cleanups. --- libpwmd.c | 218 ++++++++++++++++++++++++++++---------------------------------- 1 file changed, 97 insertions(+), 121 deletions(-) diff --git a/libpwmd.c b/libpwmd.c index e3a4df12..0e854cd8 100644 --- a/libpwmd.c +++ b/libpwmd.c @@ -71,7 +71,6 @@ static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd); static gpg_error_t global_error; #endif - const char *pwmd_strerror(gpg_error_t e) { gpg_err_code_t code = gpg_err_code(e); @@ -389,14 +388,8 @@ pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc) *rc = parse_assuan_line(pwm); } - if (!*rc) { - while (assuan_pending_line(pwm->ctx)) { - *rc = parse_assuan_line(pwm); - - if (*rc) - break; - } - } + while (!*rc && assuan_pending_line(pwm->ctx)) + *rc = parse_assuan_line(pwm); return pwm->state; } @@ -643,6 +636,9 @@ static void catchsig(int sig) } } +/* + * Borrowed from libassuan. + */ static char *percent_escape(const char *atext) { const unsigned char *s; @@ -691,7 +687,8 @@ gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...) *result = NULL; va_start(ap, cmd); /* - * C99 + * C99 allows the dst pointer to be null which will calculate the length + * of the result and return it. */ len = vsnprintf(NULL, 0, cmd, ap); buf = (char *)xmalloc(len + 1); @@ -744,7 +741,7 @@ getpin_again: error = do_getpin(pwm, result); /* - * Since there was input cancel any timeout. + * Since there was input cancel any timeout setting. */ alarm(0); @@ -768,7 +765,6 @@ getpin_again: gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw) { - char *result; gpg_error_t error; #ifndef USE_PINENTRY @@ -785,11 +781,6 @@ gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw) goto fail; } - error = pwmd_command(pwm, &result, "ISCACHED %s", pw->filename); - - if (error) - goto fail; - if (pwm->filename) xfree(pwm->filename); @@ -862,127 +853,118 @@ static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename, goto gotpassword; } - if (*error == EPWMD_CACHE_NOT_FOUND) { #ifdef USE_PINENTRY + /* + * Get the password from pinentry. + */ + if (pwm->use_pinentry) { /* - * Get the password from pinentry. + * Nonblocking is wanted. fork() then return a file descriptor + * that the client can use to read() from. */ - if (pwm->use_pinentry) { - /* - * Nonblocking is wanted. fork() then return a file descriptor - * that the client can use to read() from. - */ - if (nb) { - int p[2]; - pid_t pid; - pwmd_nb_status_t pw; - - if (pipe(p) == -1) { - *error = gpg_error_from_syserror(); - return -1; - } + if (nb) { + int p[2]; + pid_t pid; + pwmd_nb_status_t pw; + + if (pipe(p) == -1) { + *error = gpg_error_from_syserror(); + return -1; + } - pid = fork(); + pid = fork(); - switch (pid) { - case 0: - close(p[0]); - strncpy(pw.filename, filename, sizeof(pw.filename)); - pw.filename[sizeof(pw.filename)-1] = 0; - pw.fd = p[0]; + switch (pid) { + case 0: + close(p[0]); + strncpy(pw.filename, filename, sizeof(pw.filename)); + pw.filename[sizeof(pw.filename)-1] = 0; + pw.fd = p[0]; - if (timeout > 0) { - gpwm = pwm; - gtimeout = timeout; - gelapsed = 0; - } + if (timeout > 0) { + gpwm = pwm; + gtimeout = timeout; + gelapsed = 0; + } getpin_nb_again: - *error = getpin(pwm, &password, &pin_try, 0); - - if (*error) { + *error = getpin(pwm, &password, &pin_try, 0); + + if (*error) { getpin_nb_fail: - if (pwm->pctx) - pinentry_disconnect(pwm); + if (pwm->pctx) + pinentry_disconnect(pwm); - if (gtimeout && gelapsed >= gtimeout) - *error = GPG_ERR_TIMEOUT; + if (gtimeout && gelapsed >= gtimeout) + *error = GPG_ERR_TIMEOUT; - pw.error = *error; - write(p[1], &pw, sizeof(pw)); - close(p[1]); - _exit(1); - } + pw.error = *error; + write(p[1], &pw, sizeof(pw)); + close(p[1]); + _exit(1); + } - /* - * Don't count the time it takes to open the file - * which may have many iterations. - */ - signal(SIGALRM, SIG_DFL); - *error = do_open_command(pwm, filename, password); + /* + * Don't count the time it takes to open the file + * which may have many iterations. + */ + signal(SIGALRM, SIG_DFL); + *error = do_open_command(pwm, filename, password); - if (timeout) - signal(SIGALRM, catchsig); + if (timeout) + signal(SIGALRM, catchsig); - if (pwm->pctx && *error == EPWMD_BADKEY) { - if (pin_try-- > 0) - goto getpin_nb_again; + if (pwm->pctx && *error == EPWMD_BADKEY) { + if (pin_try-- > 0) + goto getpin_nb_again; - goto getpin_nb_fail; - } + goto getpin_nb_fail; + } - pinentry_disconnect(pwm); - pw.error = 0; - write(p[1], &pw, sizeof(pw)); - close(p[1]); - _exit(0); - break; - case -1: - *error = gpg_error_from_syserror(); - close(p[0]); - close(p[1]); - return -1; - default: - break; - } - - close(p[1]); - return p[0]; + pinentry_disconnect(pwm); + pw.error = 0; + write(p[1], &pw, sizeof(pw)); + close(p[1]); + _exit(0); + break; + case -1: + *error = gpg_error_from_syserror(); + close(p[0]); + close(p[1]); + return -1; + default: + break; } -getpin_again: - *error = getpin(pwm, &password, &pin_try, 1); + close(p[1]); + return p[0]; + } - if (*error) { - if (pwm->pctx) - pinentry_disconnect(pwm); +getpin_again: + *error = getpin(pwm, &password, &pin_try, 1); - if (pwm->pin_error) { - *error = pwm->pin_error; - pwm->pin_error = 0; - } + if (*error) { + if (pwm->pctx) + pinentry_disconnect(pwm); - return 1; + if (pwm->pin_error) { + *error = pwm->pin_error; + pwm->pin_error = 0; } - } - else { -#endif - /* - * Not using pinentry and the file was not found - * in the cache. - */ -#if 0 - if (pwm->password == NULL) { - *error = EPWMD_KEY; - return 1; - } -#endif - password = pwm->password; -#ifdef USE_PINENTRY + return 1; } + } + else { #endif + /* + * Not using pinentry and the file was not found + * in the cache. + */ + password = pwm->password; +#ifdef USE_PINENTRY } +#endif } else if (*error) return nb ? -1 : 1; @@ -1102,7 +1084,7 @@ static gpg_error_t do_save_command(pwm_t *pwm, char *password) gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw) { - gpg_error_t error; + gpg_error_t rc; #ifndef USE_PINENTRY return GPG_ERR_NOT_IMPLEMENTED; @@ -1112,15 +1094,9 @@ gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw) return GPG_ERR_INV_ARG; close(pw->fd); - - if (pw->error) { - error = pw->error; - memset(pw, 0, sizeof(pwmd_nb_status_t)); - return error; - } - + rc = pw->error; memset(pw, 0, sizeof(pwmd_nb_status_t)); - return 0; + return rc; } static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb) -- 2.11.4.GIT