| 1 |
From 74660ff12004b3e6e8c9320082c6f4e0d3513399 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Stephen Paul Weber <[email protected]> |
| 3 |
Date: Thu, 21 Apr 2011 22:07:24 -0500 |
| 4 |
Subject: [PATCH 04/18] Move call_gpg into tools, make more robust |
| 5 |
|
| 6 |
Move to tools.[ch] |
| 7 |
Allow passing in two bits of data. |
| 8 |
Only get password if we need it. |
| 9 |
Allow caller to specify in_data (that is, that we should return all output) |
| 10 |
--- |
| 11 |
src/core/protocol.c | 63 +---------------------------------- |
| 12 |
src/core/tools.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 13 |
src/core/tools.h | 2 ++ |
| 14 |
3 files changed, 98 insertions(+), 62 deletions(-) |
| 15 |
|
| 16 |
--- a/src/core/protocol.c |
| 17 |
+++ b/src/core/protocol.c |
| 18 |
@@ -15,11 +15,6 @@ |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
-#define _POSIX_SOURCE 1 |
| 23 |
-#define _BSD_SOURCE 1 |
| 24 |
-#define _SVID_SOURCE 1 |
| 25 |
-#include <stdio.h> |
| 26 |
- |
| 27 |
#include "module.h" |
| 28 |
#include "signals.h" |
| 29 |
#include "settings.h" |
| 30 |
@@ -30,62 +25,6 @@ |
| 31 |
|
| 32 |
char *pgp_passwd = NULL; |
| 33 |
|
| 34 |
-char *call_gpg(char *keyid, char *switches, char *input) { |
| 35 |
- int pipefd[2], tmp_fd, in_data = 0; |
| 36 |
- FILE *cstream; |
| 37 |
- char *cmd, *tmp_path, *output = NULL; |
| 38 |
- size_t output_size = 0; |
| 39 |
- char buf[100], buf2[100] = ""; |
| 40 |
- |
| 41 |
- if(pipe(pipefd)) goto pgp_error; |
| 42 |
- if(!pgp_passwd) pgp_passwd = get_password("OpenPGP Password:"); |
| 43 |
- |
| 44 |
- if(write(pipefd[1], pgp_passwd, strlen(pgp_passwd)) < 1) goto pgp_error; |
| 45 |
- if(close(pipefd[1])) goto pgp_error; |
| 46 |
- |
| 47 |
- if(!(tmp_path = tempnam(NULL, "irssi-xmpp-gpg"))) goto pgp_error; |
| 48 |
- if((tmp_fd = open(tmp_path, O_WRONLY|O_CREAT|O_EXCL, \ |
| 49 |
- S_IRUSR|S_IWUSR)) < 0) |
| 50 |
- goto pgp_error; |
| 51 |
- |
| 52 |
- if(write(tmp_fd, input, strlen(input)) < 0) goto pgp_error; |
| 53 |
- |
| 54 |
- cmd = malloc(sizeof("gpg -u '' -qo - --no-tty --passphrase-fd '' ''") \ |
| 55 |
- +strlen(switches)+6+strlen(tmp_path)); |
| 56 |
- sprintf(cmd, "gpg -u '%s' %s -qo - --no-tty --passphrase-fd '%d' '%s'", \ |
| 57 |
- keyid, switches, pipefd[0], tmp_path); |
| 58 |
- fflush(NULL); |
| 59 |
- cstream = popen(cmd, "r"); |
| 60 |
- |
| 61 |
- while(fgets(buf, sizeof(buf)-1, cstream)) { |
| 62 |
- if(strlen(buf2) > 0) { |
| 63 |
- output = realloc(output, output_size+strlen(buf2)+1); |
| 64 |
- if(!output) goto pgp_error; |
| 65 |
- if(output_size < 1) output[0] = '\0'; |
| 66 |
- output_size += strlen(buf2); |
| 67 |
- strcat(output, buf2); |
| 68 |
- } |
| 69 |
- |
| 70 |
- if(!in_data && buf[0] == '\n') { |
| 71 |
- in_data = 1; |
| 72 |
- continue; |
| 73 |
- } else if(in_data) { |
| 74 |
- strcpy(buf2, buf); |
| 75 |
- } |
| 76 |
- } |
| 77 |
- |
| 78 |
- pclose(cstream); /* TODO: check exit code */ |
| 79 |
- |
| 80 |
- close(tmp_fd); |
| 81 |
- close(pipefd[0]); |
| 82 |
- free(tmp_path); |
| 83 |
- free(cmd); |
| 84 |
- |
| 85 |
- return output; |
| 86 |
-pgp_error: |
| 87 |
- return NULL; |
| 88 |
-} |
| 89 |
- |
| 90 |
static void |
| 91 |
sig_set_presence(XMPP_SERVER_REC *server, const int show, const char *status, |
| 92 |
const int priority) |
| 93 |
@@ -122,7 +61,7 @@ |
| 94 |
|
| 95 |
if((pgp_keyid = settings_get_str("xmpp_pgp"))) { |
| 96 |
LmMessageNode *x; |
| 97 |
- char *signature = call_gpg(pgp_keyid, "-ab", str); |
| 98 |
+ char *signature = call_gpg("-ab", str, NULL, 0); |
| 99 |
|
| 100 |
x = lm_message_node_add_child(lmsg->node, "x", signature); |
| 101 |
lm_message_node_set_attribute(x, "xmlns", "jabber:x:signed"); |
| 102 |
--- a/src/core/tools.c |
| 103 |
+++ b/src/core/tools.c |
| 104 |
@@ -15,18 +15,113 @@ |
| 105 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 106 |
*/ |
| 107 |
|
| 108 |
+#define _POSIX_SOURCE 1 |
| 109 |
+#define _BSD_SOURCE 1 |
| 110 |
+#define _SVID_SOURCE 1 |
| 111 |
+#include <stdio.h> |
| 112 |
+ |
| 113 |
#include <string.h> |
| 114 |
|
| 115 |
#include "module.h" |
| 116 |
#include "recode.h" |
| 117 |
#include "settings.h" |
| 118 |
#include "signals.h" |
| 119 |
+#include "xmpp-servers.h" |
| 120 |
|
| 121 |
#define XMPP_PRIORITY_MIN -128 |
| 122 |
#define XMPP_PRIORITY_MAX 127 |
| 123 |
|
| 124 |
static const char *utf8_charset = "UTF-8"; |
| 125 |
|
| 126 |
+char *call_gpg(char *switches, char *input, char *input2, int in_data) { |
| 127 |
+ int pipefd[2], tmp_fd, tmp2_fd = 0; |
| 128 |
+ FILE *cstream; |
| 129 |
+ char *cmd, *tmp_path, *tmp2_path = NULL, *output = NULL; |
| 130 |
+ size_t output_size = 0; |
| 131 |
+ char buf[100], buf2[100] = ""; |
| 132 |
+ const char *keyid = settings_get_str("xmpp_pgp"); |
| 133 |
+ |
| 134 |
+ if(keyid) { /* If no keyID, then we don't need a password */ |
| 135 |
+ if(pipe(pipefd)) goto pgp_error; |
| 136 |
+ if(!pgp_passwd) pgp_passwd = get_password("OpenPGP Password:"); |
| 137 |
+ |
| 138 |
+ if(write(pipefd[1], pgp_passwd, strlen(pgp_passwd)) < 1) goto pgp_error; |
| 139 |
+ if(close(pipefd[1])) goto pgp_error; |
| 140 |
+ } |
| 141 |
+ |
| 142 |
+ if(!(tmp_path = tempnam(NULL, "irssi-xmpp-gpg"))) goto pgp_error; |
| 143 |
+ if((tmp_fd = open(tmp_path, O_WRONLY|O_CREAT|O_EXCL, \ |
| 144 |
+ S_IRUSR|S_IWUSR)) < 0) |
| 145 |
+ goto pgp_error; |
| 146 |
+ |
| 147 |
+ if(write(tmp_fd, input, strlen(input)) < 0) goto pgp_error; |
| 148 |
+ |
| 149 |
+ if(input2) { |
| 150 |
+ if(!(tmp2_path = tempnam(NULL, "irssi-xmpp-gpg"))) goto pgp_error; |
| 151 |
+ if((tmp2_fd = open(tmp2_path, O_WRONLY|O_CREAT|O_EXCL, \ |
| 152 |
+ S_IRUSR|S_IWUSR)) < 0) |
| 153 |
+ goto pgp_error; |
| 154 |
+ |
| 155 |
+ if(write(tmp2_fd, input2, strlen(input2)) < 0) goto pgp_error; |
| 156 |
+ } |
| 157 |
+ |
| 158 |
+ cmd = malloc(sizeof("gpg -u '' --passphrase-fd '' -qo - --batch --no-tty '' '' 2>&1") \ |
| 159 |
+ +strlen(switches)+8+strlen(tmp_path)+ \ |
| 160 |
+ (tmp2_path ? strlen(tmp2_path) : 0)); |
| 161 |
+ if(keyid) { |
| 162 |
+ sprintf(cmd, "gpg -u '%s' --passphrase-fd '%d' ", keyid, pipefd[0]); |
| 163 |
+ } else { |
| 164 |
+ strcpy(cmd, "gpg "); |
| 165 |
+ } |
| 166 |
+ strcat(cmd, switches); |
| 167 |
+ strcat(cmd, " -qo - --batch --no-tty '"); |
| 168 |
+ strcat(cmd, tmp_path); |
| 169 |
+ strcat(cmd, "' "); |
| 170 |
+ |
| 171 |
+ if(tmp2_path) { |
| 172 |
+ strcat(cmd, "'"); |
| 173 |
+ strcat(cmd, tmp2_path); |
| 174 |
+ strcat(cmd, "'"); |
| 175 |
+ } |
| 176 |
+ |
| 177 |
+ strcat(cmd, " 2>&1"); |
| 178 |
+ |
| 179 |
+ fflush(NULL); |
| 180 |
+ cstream = popen(cmd, "r"); |
| 181 |
+ |
| 182 |
+ while(fgets(buf, sizeof(buf)-1, cstream)) { |
| 183 |
+ if(strlen(buf2) > 0) { |
| 184 |
+ output = realloc(output, output_size+strlen(buf2)+1); |
| 185 |
+ if(!output) goto pgp_error; |
| 186 |
+ if(output_size < 1) output[0] = '\0'; |
| 187 |
+ output_size += strlen(buf2); |
| 188 |
+ strcat(output, buf2); |
| 189 |
+ } |
| 190 |
+ |
| 191 |
+ if(!in_data && buf[0] == '\n') { |
| 192 |
+ in_data = 1; |
| 193 |
+ continue; |
| 194 |
+ } else if(in_data) { |
| 195 |
+ strcpy(buf2, buf); |
| 196 |
+ } |
| 197 |
+ } |
| 198 |
+ |
| 199 |
+ pclose(cstream); /* TODO: check exit code */ |
| 200 |
+ |
| 201 |
+ close(tmp_fd); |
| 202 |
+ free(tmp_path); |
| 203 |
+ if(tmp2_fd) close(tmp2_fd); |
| 204 |
+ if(tmp2_path) free(tmp2_path); |
| 205 |
+ if(keyid) close(pipefd[0]); |
| 206 |
+ free(cmd); |
| 207 |
+ |
| 208 |
+ return output; |
| 209 |
+pgp_error: |
| 210 |
+ return NULL; |
| 211 |
+} |
| 212 |
+ |
| 213 |
+ |
| 214 |
+ |
| 215 |
static gboolean |
| 216 |
xmpp_get_local_charset(const char **charset) |
| 217 |
{ |
| 218 |
--- a/src/core/tools.h |
| 219 |
+++ b/src/core/tools.h |
| 220 |
@@ -2,6 +2,8 @@ |
| 221 |
#define __TOOLS_H |
| 222 |
|
| 223 |
__BEGIN_DECLS |
| 224 |
+char *call_gpg(char *switches, char *input, char *input2, int in_data); |
| 225 |
+ |
| 226 |
char *xmpp_recode_out(const char *); |
| 227 |
char *xmpp_recode_in(const char *); |
| 228 |
|