From 74660ff12004b3e6e8c9320082c6f4e0d3513399 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Thu, 21 Apr 2011 22:07:24 -0500 Subject: [PATCH 04/18] Move call_gpg into tools, make more robust Move to tools.[ch] Allow passing in two bits of data. Only get password if we need it. Allow caller to specify in_data (that is, that we should return all output) --- src/core/protocol.c | 63 +---------------------------------- src/core/tools.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/tools.h | 2 ++ 3 files changed, 98 insertions(+), 62 deletions(-) --- a/src/core/protocol.c +++ b/src/core/protocol.c @@ -15,11 +15,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#define _POSIX_SOURCE 1 -#define _BSD_SOURCE 1 -#define _SVID_SOURCE 1 -#include - #include "module.h" #include "signals.h" #include "settings.h" @@ -30,62 +25,6 @@ char *pgp_passwd = NULL; -char *call_gpg(char *keyid, char *switches, char *input) { - int pipefd[2], tmp_fd, in_data = 0; - FILE *cstream; - char *cmd, *tmp_path, *output = NULL; - size_t output_size = 0; - char buf[100], buf2[100] = ""; - - if(pipe(pipefd)) goto pgp_error; - if(!pgp_passwd) pgp_passwd = get_password("OpenPGP Password:"); - - if(write(pipefd[1], pgp_passwd, strlen(pgp_passwd)) < 1) goto pgp_error; - if(close(pipefd[1])) goto pgp_error; - - if(!(tmp_path = tempnam(NULL, "irssi-xmpp-gpg"))) goto pgp_error; - if((tmp_fd = open(tmp_path, O_WRONLY|O_CREAT|O_EXCL, \ - S_IRUSR|S_IWUSR)) < 0) - goto pgp_error; - - if(write(tmp_fd, input, strlen(input)) < 0) goto pgp_error; - - cmd = malloc(sizeof("gpg -u '' -qo - --no-tty --passphrase-fd '' ''") \ - +strlen(switches)+6+strlen(tmp_path)); - sprintf(cmd, "gpg -u '%s' %s -qo - --no-tty --passphrase-fd '%d' '%s'", \ - keyid, switches, pipefd[0], tmp_path); - fflush(NULL); - cstream = popen(cmd, "r"); - - while(fgets(buf, sizeof(buf)-1, cstream)) { - if(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); - } - - if(!in_data && buf[0] == '\n') { - in_data = 1; - continue; - } else if(in_data) { - strcpy(buf2, buf); - } - } - - pclose(cstream); /* TODO: check exit code */ - - close(tmp_fd); - close(pipefd[0]); - free(tmp_path); - free(cmd); - - return output; -pgp_error: - return NULL; -} - static void sig_set_presence(XMPP_SERVER_REC *server, const int show, const char *status, const int priority) @@ -122,7 +61,7 @@ if((pgp_keyid = settings_get_str("xmpp_pgp"))) { LmMessageNode *x; - char *signature = call_gpg(pgp_keyid, "-ab", str); + char *signature = call_gpg("-ab", str, NULL, 0); x = lm_message_node_add_child(lmsg->node, "x", signature); lm_message_node_set_attribute(x, "xmlns", "jabber:x:signed"); --- a/src/core/tools.c +++ b/src/core/tools.c @@ -15,18 +15,113 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#define _POSIX_SOURCE 1 +#define _BSD_SOURCE 1 +#define _SVID_SOURCE 1 +#include + #include #include "module.h" #include "recode.h" #include "settings.h" #include "signals.h" +#include "xmpp-servers.h" #define XMPP_PRIORITY_MIN -128 #define XMPP_PRIORITY_MAX 127 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; + FILE *cstream; + char *cmd, *tmp_path, *tmp2_path = NULL, *output = NULL; + size_t output_size = 0; + char buf[100], buf2[100] = ""; + const char *keyid = settings_get_str("xmpp_pgp"); + + if(keyid) { /* If no keyID, then we don't need a password */ + if(pipe(pipefd)) goto pgp_error; + if(!pgp_passwd) pgp_passwd = get_password("OpenPGP Password:"); + + if(write(pipefd[1], pgp_passwd, strlen(pgp_passwd)) < 1) goto pgp_error; + if(close(pipefd[1])) goto pgp_error; + } + + if(!(tmp_path = tempnam(NULL, "irssi-xmpp-gpg"))) goto pgp_error; + if((tmp_fd = open(tmp_path, O_WRONLY|O_CREAT|O_EXCL, \ + S_IRUSR|S_IWUSR)) < 0) + goto pgp_error; + + if(write(tmp_fd, input, strlen(input)) < 0) goto pgp_error; + + if(input2) { + if(!(tmp2_path = tempnam(NULL, "irssi-xmpp-gpg"))) goto pgp_error; + if((tmp2_fd = open(tmp2_path, O_WRONLY|O_CREAT|O_EXCL, \ + S_IRUSR|S_IWUSR)) < 0) + goto pgp_error; + + if(write(tmp2_fd, input2, strlen(input2)) < 0) goto pgp_error; + } + + cmd = malloc(sizeof("gpg -u '' --passphrase-fd '' -qo - --batch --no-tty '' '' 2>&1") \ + +strlen(switches)+8+strlen(tmp_path)+ \ + (tmp2_path ? strlen(tmp2_path) : 0)); + if(keyid) { + sprintf(cmd, "gpg -u '%s' --passphrase-fd '%d' ", keyid, pipefd[0]); + } else { + strcpy(cmd, "gpg "); + } + strcat(cmd, switches); + strcat(cmd, " -qo - --batch --no-tty '"); + strcat(cmd, tmp_path); + strcat(cmd, "' "); + + if(tmp2_path) { + strcat(cmd, "'"); + strcat(cmd, tmp2_path); + strcat(cmd, "'"); + } + + strcat(cmd, " 2>&1"); + + fflush(NULL); + cstream = popen(cmd, "r"); + + while(fgets(buf, sizeof(buf)-1, cstream)) { + if(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); + } + + if(!in_data && buf[0] == '\n') { + in_data = 1; + continue; + } else if(in_data) { + strcpy(buf2, buf); + } + } + + pclose(cstream); /* TODO: check exit code */ + + close(tmp_fd); + free(tmp_path); + if(tmp2_fd) close(tmp2_fd); + if(tmp2_path) free(tmp2_path); + if(keyid) close(pipefd[0]); + free(cmd); + + return output; +pgp_error: + return NULL; +} + + + static gboolean xmpp_get_local_charset(const char **charset) { --- a/src/core/tools.h +++ b/src/core/tools.h @@ -2,6 +2,8 @@ #define __TOOLS_H __BEGIN_DECLS +char *call_gpg(char *switches, char *input, char *input2, int in_data); + char *xmpp_recode_out(const char *); char *xmpp_recode_in(const char *);