Package: irssi-plugin-xmpp / 0.53-1~bpo8+1

singpolyma-0004-Move-call_gpg-into-tools-make-more-robust.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
From 74660ff12004b3e6e8c9320082c6f4e0d3513399 Mon Sep 17 00:00:00 2001
From: Stephen Paul Weber <singpolyma@singpolyma.net>
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 <stdio.h>
-
 #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 <stdio.h>
+
 #include <string.h>
 
 #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(G_CONST_RETURN 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 *);