From 854e60b80cf0f2bbb86f063a593038c3f671bd80 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sat, 23 Apr 2011 15:04:04 -0500 Subject: [PATCH 10/18] More robuse call_gpg invocation --- src/core/protocol.c | 2 +- src/core/rosters.c | 2 +- src/core/tools.c | 23 +++++++++++++++++++---- src/core/tools.h | 3 ++- src/core/xmpp-servers.c | 4 ++-- 5 files changed, 25 insertions(+), 9 deletions(-) --- a/src/core/protocol.c +++ b/src/core/protocol.c @@ -61,7 +61,7 @@ if((pgp_keyid = settings_get_str("xmpp_pgp"))) { LmMessageNode *x; - char *signature = call_gpg("-ab", str, NULL, 0); + char *signature = call_gpg("-ab", str, NULL, 0, 1); x = lm_message_node_add_child(lmsg->node, "x", signature); lm_message_node_set_attribute(x, "xmlns", "jabber:x:signed"); --- a/src/core/rosters.c +++ b/src/core/rosters.c @@ -515,7 +515,7 @@ strcat(send_to_gpg, signature->value); strcat(send_to_gpg, "----- END PGP SIGNATURE-----\n"); - from_gpg = call_gpg("--verify", send_to_gpg, send_status, 1); + from_gpg = call_gpg("--verify", send_to_gpg, send_status, 1, 0); free(send_to_gpg); /* If there is a good signature, grab the key ID */ --- a/src/core/tools.c +++ b/src/core/tools.c @@ -33,8 +33,9 @@ static const char *utf8_charset = "UTF-8"; -char *call_gpg(char *switches, char *input, char *input2, int in_data) { - int pipefd[2], tmp_fd, tmp2_fd = 0; +char *call_gpg(char *switches, char *input, char *input2, \ + int get_stderr, int snip_data) { + int pipefd[2], tmp_fd, tmp2_fd = 0, in_data = !snip_data; FILE *cstream; char *cmd, *tmp_path, *tmp2_path = NULL, *output = NULL; size_t output_size = 0; @@ -65,7 +66,8 @@ if(write(tmp2_fd, input2, strlen(input2)) < 0) goto pgp_error; } - cmd = malloc(sizeof("gpg -u '' --passphrase-fd '' -qo - --batch --no-tty '' '' 2>&1") \ + cmd = malloc(sizeof("gpg -u '' --passphrase-fd '' -qo - --batch " \ + "--no-tty '' '' 2>/dev/null") \ +strlen(switches)+8+strlen(tmp_path)+ \ (tmp2_path ? strlen(tmp2_path) : 0)); if(keyid) { @@ -84,7 +86,11 @@ strcat(cmd, "'"); } - strcat(cmd, " 2>&1"); + if(get_stderr) { + strcat(cmd, " 2>&1"); + } else { + strcat(cmd, " 2>/dev/null"); + } fflush(NULL); cstream = popen(cmd, "r"); @@ -106,6 +112,15 @@ } } + /* Get last line if not snipping */ + if(!snip_data && strlen(buf2) > 0) { + output = realloc(output, output_size+strlen(buf2)+1); + if(!output) goto pgp_error; + if(output_size < 1) output[0] = '\0'; + output_size += strlen(buf2); + strcat(output, buf2); + } + pclose(cstream); /* TODO: check exit code */ close(tmp_fd); --- a/src/core/tools.h +++ b/src/core/tools.h @@ -2,7 +2,8 @@ #define __TOOLS_H __BEGIN_DECLS -char *call_gpg(char *switches, char *input, char *input2, int in_data); +char *call_gpg(char *switches, char *input, char *input2, \ + int get_stderr, int snip_data); char *xmpp_recode_out(const char *); char *xmpp_recode_in(const char *); --- a/src/core/xmpp-servers.c +++ b/src/core/xmpp-servers.c @@ -119,7 +119,7 @@ if(settings_get_str("xmpp_pgp")) strcat(switches, "s"); strcat(switches, "R "); strcat(switches, encrypt_to); - encrypted = call_gpg(switches, recoded, NULL, 0); + encrypted = call_gpg(switches, recoded, NULL, 0, 1); x = lm_message_node_add_child(lmsg->node, "x", encrypted); lm_message_node_set_attribute(x, "xmlns", "jabber:x:encrypted"); @@ -130,7 +130,7 @@ recoded = g_strdup("[This message is encrypted.]"); } else if(settings_get_str("xmpp_pgp")) { LmMessageNode *x; - char *msigned = call_gpg("-ab", recoded, NULL, 0); + char *msigned = call_gpg("-ab", recoded, NULL, 0, 1); x = lm_message_node_add_child(lmsg->node, "x", msigned); lm_message_node_set_attribute(x, "xmlns", "jabber:x:signed"); free(msigned);