From 0411946bef40d584ae77ed5a84b8ef1dbb152cec Mon Sep 17 00:00:00 2001 From: Jan Losinski Date: Tue, 24 Feb 2015 22:19:53 +0100 Subject: [PATCH 01/10] Repair broken passphrase retry The exit code handling of the gpg process to detect wrong passphrases was a bit adventurous. It checked for the raw status value from waitpid instead of the WEXITSTATUS() macro. The code that handles the waitpid also does not check for any errors. This is now fixed by evaluating the waitpid return value and the use of the WEXITSTATUS() macro. Signed-off-by: Jan Losinski --- src/core/popenRWE.c | 4 +++- src/core/tools.c | 21 +++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) --- a/src/core/popenRWE.c +++ b/src/core/popenRWE.c @@ -88,6 +88,8 @@ close(rwepipe[0]); close(rwepipe[1]); close(rwepipe[2]); - rc = waitpid(pid, &status, 0); + do { + rc = waitpid(pid, &status, 0); + } while (rc != 0 && ! WIFEXITED(status)); return status; } --- a/src/core/tools.c +++ b/src/core/tools.c @@ -21,6 +21,7 @@ #include #include +#include #include "module.h" #include "recode.h" @@ -34,8 +35,8 @@ static const char *utf8_charset = "UTF-8"; -char *call_gpg(char *switches, char *input, char *input2, \ - int get_stderr, int snip_data) { +char *call_gpg_round(char *switches, char *input, char *input2, \ + int get_stderr, int snip_data, unsigned round) { int pipefd[2], rwepipe[3], childpid, tmp2_fd = 0, in_data = !snip_data; FILE* cstream; char *cmd, *tmp2_path = NULL, *output = NULL; @@ -124,10 +125,15 @@ strcat(output, buf2); } - if(pcloseRWE(childpid, rwepipe) == 512) { /* TODO: more check exit code */ + // http://www.gnu-darwin.org/www001/src/ports/security/libgpg-error/work/libgpg-error-1.5/src/err-codes.h.in + // 11 GPG_ERR_BAD_PASSPHRASE Bad passphrase + // 31 GPG_ERR_INV_PASSPHRASE Invalid passphrase + int exit_status = WEXITSTATUS(pcloseRWE(childpid, rwepipe)); + if(round > 0 && (exit_status == 11 || exit_status == 31)) { g_free(pgp_passwd); pgp_passwd = NULL; - output = call_gpg(switches, input, input2, get_stderr, snip_data); + output = call_gpg_round(switches, input, input2, get_stderr, + snip_data, round--); } if(tmp2_fd) close(tmp2_fd); @@ -141,6 +147,13 @@ } +char *call_gpg(char *switches, char *input, char *input2, \ + int get_stderr, int snip_data, unsigned round) { + return call_gpg_round(switches, input, input2, get_stderr, + snip_data, 3); +} + + static gboolean xmpp_get_local_charset(const char **charset)