| 1 |
From c8d2c9316ef968e8d83ceb1b9ca1ee90298efe77 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Stephen Paul Weber <[email protected]> |
| 3 |
Date: Sun, 24 Apr 2011 16:33:48 -0500 |
| 4 |
Subject: [PATCH 17/18] Many fewer temp files |
| 5 |
|
| 6 |
--- |
| 7 |
src/core/Makefile | 1 + |
| 8 |
src/core/tools.c | 43 +++++++++++++++++++------------------------ |
| 9 |
2 files changed, 20 insertions(+), 24 deletions(-) |
| 10 |
|
| 11 |
--- a/src/core/Makefile |
| 12 |
+++ b/src/core/Makefile |
| 13 |
@@ -11,6 +11,7 @@ |
| 14 |
rosters-tools.c \ |
| 15 |
stanzas.c \ |
| 16 |
tools.c \ |
| 17 |
+ popenRWE.c \ |
| 18 |
xep/chatstates.c \ |
| 19 |
xep/composing.c \ |
| 20 |
xep/datetime.c \ |
| 21 |
--- a/src/core/tools.c |
| 22 |
+++ b/src/core/tools.c |
| 23 |
@@ -27,6 +27,7 @@ |
| 24 |
#include "settings.h" |
| 25 |
#include "signals.h" |
| 26 |
#include "xmpp-servers.h" |
| 27 |
+#include "popenRWE.h" |
| 28 |
|
| 29 |
#define XMPP_PRIORITY_MIN -128 |
| 30 |
#define XMPP_PRIORITY_MAX 127 |
| 31 |
@@ -35,9 +36,9 @@ |
| 32 |
|
| 33 |
char *call_gpg(char *switches, char *input, char *input2, \ |
| 34 |
int get_stderr, int snip_data) { |
| 35 |
- int pipefd[2], tmp_fd, tmp2_fd = 0, in_data = !snip_data; |
| 36 |
- FILE *cstream; |
| 37 |
- char *cmd, *tmp_path, *tmp2_path = NULL, *output = NULL; |
| 38 |
+ int pipefd[2], rwepipe[3], childpid, tmp2_fd = 0, in_data = !snip_data; |
| 39 |
+ FILE* cstream; |
| 40 |
+ char *cmd, *tmp2_path = NULL, *output = NULL; |
| 41 |
size_t output_size = 0; |
| 42 |
char buf[100], buf2[100] = ""; |
| 43 |
const char *keyid = settings_get_str("xmpp_pgp"); |
| 44 |
@@ -45,19 +46,13 @@ |
| 45 |
if(keyid) { /* If no keyID, then we don't need a password */ |
| 46 |
if(pipe(pipefd)) goto pgp_error; |
| 47 |
if(!pgp_passwd) pgp_passwd = get_password("OpenPGP Password:"); |
| 48 |
+ if(!pgp_passwd) goto pgp_error; |
| 49 |
|
| 50 |
if(write(pipefd[1], pgp_passwd, strlen(pgp_passwd)) < 1) goto pgp_error; |
| 51 |
if(close(pipefd[1])) goto pgp_error; |
| 52 |
} |
| 53 |
|
| 54 |
- if(!(tmp_path = tempnam(NULL, "irssi-xmpp-gpg"))) goto pgp_error; |
| 55 |
- if((tmp_fd = open(tmp_path, O_WRONLY|O_CREAT|O_EXCL, \ |
| 56 |
- S_IRUSR|S_IWUSR)) < 0) |
| 57 |
- goto pgp_error; |
| 58 |
- |
| 59 |
- if(write(tmp_fd, input, strlen(input)) < 0) goto pgp_error; |
| 60 |
- |
| 61 |
- if(input2) { |
| 62 |
+ if(input2) { /* NOTE for security it might be better if this were a named pipe */ |
| 63 |
if(!(tmp2_path = tempnam(NULL, "irssi-xmpp-gpg"))) goto pgp_error; |
| 64 |
if((tmp2_fd = open(tmp2_path, O_WRONLY|O_CREAT|O_EXCL, \ |
| 65 |
S_IRUSR|S_IWUSR)) < 0) |
| 66 |
@@ -67,8 +62,8 @@ |
| 67 |
} |
| 68 |
|
| 69 |
cmd = malloc(sizeof("gpg -u '' --passphrase-fd '' --trust-model always" \ |
| 70 |
- " -qo - --batch --no-tty '' '' 2>/dev/null") \ |
| 71 |
- +strlen(switches)+8+strlen(tmp_path)+ \ |
| 72 |
+ " -qo - --batch --no-tty - ''") \ |
| 73 |
+ +strlen(switches)+8+ \ |
| 74 |
(tmp2_path ? strlen(tmp2_path) : 0)); |
| 75 |
if(keyid) { |
| 76 |
sprintf(cmd, "gpg -u '%s' --passphrase-fd '%d' ", keyid, pipefd[0]); |
| 77 |
@@ -76,9 +71,7 @@ |
| 78 |
strcpy(cmd, "gpg "); |
| 79 |
} |
| 80 |
strcat(cmd, switches); |
| 81 |
- strcat(cmd, " --trust-model always -qo - --batch --no-tty '"); |
| 82 |
- strcat(cmd, tmp_path); |
| 83 |
- strcat(cmd, "' "); |
| 84 |
+ strcat(cmd, " --trust-model always -qo - --batch --no-tty - "); |
| 85 |
|
| 86 |
if(tmp2_path) { |
| 87 |
strcat(cmd, "'"); |
| 88 |
@@ -86,14 +79,18 @@ |
| 89 |
strcat(cmd, "'"); |
| 90 |
} |
| 91 |
|
| 92 |
+ fflush(NULL); |
| 93 |
+ childpid = popenRWE(rwepipe, cmd); |
| 94 |
+ |
| 95 |
+ if(write(rwepipe[0], input, strlen(input)) < 0) goto pgp_error; |
| 96 |
+ if(close(rwepipe[0])) goto pgp_error; |
| 97 |
+ |
| 98 |
if(get_stderr) { |
| 99 |
- strcat(cmd, " 2>&1"); |
| 100 |
+ cstream = fdopen(rwepipe[2], "r"); |
| 101 |
} else { |
| 102 |
- strcat(cmd, " 2>/dev/null"); |
| 103 |
+ cstream = fdopen(rwepipe[1], "r"); |
| 104 |
} |
| 105 |
- |
| 106 |
- fflush(NULL); |
| 107 |
- cstream = popen(cmd, "r"); |
| 108 |
+ if(!cstream) goto pgp_error; |
| 109 |
|
| 110 |
while(fgets(buf, sizeof(buf)-1, cstream)) { |
| 111 |
if(strlen(buf2) > 0) { |
| 112 |
@@ -121,14 +118,12 @@ |
| 113 |
strcat(output, buf2); |
| 114 |
} |
| 115 |
|
| 116 |
- if(pclose(cstream) == 512) { /* TODO: more check exit code */ |
| 117 |
+ if(pcloseRWE(childpid, rwepipe) == 512) { /* TODO: more check exit code */ |
| 118 |
g_free(pgp_passwd); |
| 119 |
pgp_passwd = NULL; |
| 120 |
output = call_gpg(switches, input, input2, get_stderr, snip_data); |
| 121 |
} |
| 122 |
|
| 123 |
- close(tmp_fd); |
| 124 |
- free(tmp_path); |
| 125 |
if(tmp2_fd) close(tmp2_fd); |
| 126 |
if(tmp2_path) free(tmp2_path); |
| 127 |
if(keyid) close(pipefd[0]); |