1
From c8d2c9316ef968e8d83ceb1b9ca1ee90298efe77 Mon Sep 17 00:00:00 2001
2
From: Stephen Paul Weber <singpolyma@singpolyma.net>
3
Date: Sun, 24 Apr 2011 16:33:48 -0500
4
Subject: [PATCH 17/18] Many fewer temp files
7
src/core/Makefile | 1 +
8
src/core/tools.c | 43 +++++++++++++++++++------------------------
9
2 files changed, 20 insertions(+), 24 deletions(-)
11
--- a/src/core/Makefile
12
+++ b/src/core/Makefile
21
--- a/src/core/tools.c
22
+++ b/src/core/tools.c
26
#include "xmpp-servers.h"
27
+#include "popenRWE.h"
29
#define XMPP_PRIORITY_MIN -128
30
#define XMPP_PRIORITY_MAX 127
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;
37
- char *cmd, *tmp_path, *tmp2_path = NULL, *output = NULL;
38
+ int pipefd[2], rwepipe[3], childpid, tmp2_fd = 0, in_data = !snip_data;
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");
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;
50
if(write(pipefd[1], pgp_passwd, strlen(pgp_passwd)) < 1) goto pgp_error;
51
if(close(pipefd[1])) goto pgp_error;
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)
59
- if(write(tmp_fd, input, strlen(input)) < 0) goto pgp_error;
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)
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));
76
sprintf(cmd, "gpg -u '%s' --passphrase-fd '%d' ", keyid, pipefd[0]);
80
strcat(cmd, switches);
81
- strcat(cmd, " --trust-model always -qo - --batch --no-tty '");
82
- strcat(cmd, tmp_path);
84
+ strcat(cmd, " --trust-model always -qo - --batch --no-tty - ");
93
+ childpid = popenRWE(rwepipe, cmd);
95
+ if(write(rwepipe[0], input, strlen(input)) < 0) goto pgp_error;
96
+ if(close(rwepipe[0])) goto pgp_error;
99
- strcat(cmd, " 2>&1");
100
+ cstream = fdopen(rwepipe[2], "r");
102
- strcat(cmd, " 2>/dev/null");
103
+ cstream = fdopen(rwepipe[1], "r");
107
- cstream = popen(cmd, "r");
108
+ if(!cstream) goto pgp_error;
110
while(fgets(buf, sizeof(buf)-1, cstream)) {
111
if(strlen(buf2) > 0) {
112
@@ -121,14 +118,12 @@
113
strcat(output, buf2);
116
- if(pclose(cstream) == 512) { /* TODO: more check exit code */
117
+ if(pcloseRWE(childpid, rwepipe) == 512) { /* TODO: more check exit code */
120
output = call_gpg(switches, input, input2, get_stderr, snip_data);
125
if(tmp2_fd) close(tmp2_fd);
126
if(tmp2_path) free(tmp2_path);
127
if(keyid) close(pipefd[0]);