Debian Codegraph @

src/core/tools.c

raw symbols ◀ ▶
Source reconstructed at patch state 11 of 15 — not the installed (fully patched) source.
15 Debian patches touch this file
1 /*
2 * Copyright (C) 2007 Colin DIDIER
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _POSIX_SOURCE 1
19 #define _BSD_SOURCE 1
20 #define _SVID_SOURCE 1
21 #include <stdio.h>
22
23 #include <string.h>
24 #include <sys/wait.h>
25
26 #include "module.h"
27 #include "recode.h"
28 #include "settings.h"
29 #include "signals.h"
30 #include "xmpp-servers.h"
31 #include "popenRWE.h"
32
33 #define XMPP_PRIORITY_MIN -128
34 #define XMPP_PRIORITY_MAX 127
35
36 static const char *utf8_charset = "UTF-8";
37
38 char *call_gpg_round(char *switches, char *input, char *input2, \
39 int get_stderr, int snip_data, unsigned round) {
40 int pipefd[2], rwepipe[3], childpid, tmp2_fd = 0, in_data = !snip_data;
41 FILE* cstream;
42 char *cmd, *output = NULL;
43 size_t output_size = 0;
44 char buf[100], buf2[100] = "";
45 const char *keyid = settings_get_str("xmpp_pgp");
46 int send_password = keyid && !settings_get_str("xmpp_pgp_agent");
47
48 /* If no keyID, then we don't need a password */
49 if(send_password) {
50 if(pipe(pipefd)) goto pgp_error;
51 if(!pgp_passwd) pgp_passwd = get_password("OpenPGP Password:");
52 if(!pgp_passwd) goto pgp_error;
53
54 if(write(pipefd[1], pgp_passwd, strlen(pgp_passwd)) < 0) goto pgp_error;
55 if(close(pipefd[1])) goto pgp_error;
56 }
57
58 if(input2) { /* NOTE for security it might be better if this were a named pipe */
59 if((tmp2_fd = mkstemp("irssi-xmpp-gpgXXXXXX")) == -1) goto pgp_error;
60 if(write(tmp2_fd, input2, strlen(input2)) < 0) goto pgp_error;
61 }
62
63 cmd = malloc(sizeof("gpg --enable-special-filenames -u ''" \
64 "--passphrase-fd '' --trust-model always" \
65 " -qo - --batch --no-tty - '' -&") \
66 +1+strlen(switches)+5+ /* 5 is for passphrase-fd */ \
67 (keyid ? strlen(keyid) : 0)+ \
68 (keyid ? strlen(keyid) : 0)+ \
69 (tmp2_fd ? 5 : 0));
70 if(keyid) {
71 strcpy(cmd, "gpg -u '");
72 strcat(cmd, keyid);
73 strcat(cmd, "' ");
74 if(!settings_get_str("xmpp_pgp_agent")) {
75 sprintf(cmd+strlen(cmd), "--passphrase-fd '%d' ", pipefd[0]);
76 }
77 } else {
78 strcpy(cmd, "gpg ");
79 }
80 strcat(cmd, switches);
81 strcat(cmd, " --enable-special-filenames --trust-model always -qo -" \
82 " --batch --no-tty - ");
83
84 if(tmp2_fd) {
85 sprintf(cmd+strlen(cmd), "-&%d", tmp2_fd);
86 }
87
88 fflush(NULL);
89 childpid = popenRWE(rwepipe, cmd);
90
91 if(write(rwepipe[0], input, strlen(input)) < 0) goto pgp_error;
92 if(close(rwepipe[0])) goto pgp_error;
93
94 if(get_stderr) {
95 cstream = fdopen(rwepipe[2], "r");
96 } else {
97 cstream = fdopen(rwepipe[1], "r");
98 }
99 if(!cstream) goto pgp_error;
100
101 while(fgets(buf, sizeof(buf)-1, cstream)) {
102 if(strlen(buf2) > 0) {
103 output = realloc(output, output_size+strlen(buf2)+1);
104 if(!output) goto pgp_error;
105 if(output_size < 1) output[0] = '\0';
106 output_size += strlen(buf2);
107 strcat(output, buf2);
108 }
109
110 if(!in_data && buf[0] == '\n') {
111 in_data = 1;
112 continue;
113 } else if(in_data) {
114 strcpy(buf2, buf);
115 }
116 }
117
118 /* Get last line if not snipping */
119 if(!snip_data && strlen(buf2) > 0) {
120 output = realloc(output, output_size+strlen(buf2)+1);
121 if(!output) goto pgp_error;
122 if(output_size < 1) output[0] = '\0';
123 output_size += strlen(buf2);
124 strcat(output, buf2);
125 }
126
127 // http://www.gnu-darwin.org/www001/src/ports/security/libgpg-error/work/libgpg-error-1.5/src/err-codes.h.in
128 // 11 GPG_ERR_BAD_PASSPHRASE Bad passphrase
129 // 31 GPG_ERR_INV_PASSPHRASE Invalid passphrase
130 int exit_status = WEXITSTATUS(pcloseRWE(childpid, rwepipe));
131 if(round > 0 && (exit_status == 11 || exit_status == 31)) {
132 g_free(pgp_passwd);
133 pgp_passwd = NULL;
134 output = call_gpg_round(switches, input, input2, get_stderr,
135 snip_data, round--);
136 }
137
138 done:
139 if(tmp2_fd) close(tmp2_fd);
140 if(send_password) close(pipefd[0]);
141 free(cmd);
142
143 return output;
144 pgp_error:
145 output = NULL;
146 goto done;
147 }
148
149
150 char *call_gpg(char *switches, char *input, char *input2, \
151 int get_stderr, int snip_data, unsigned round) {
152 return call_gpg_round(switches, input, input2, get_stderr,
153 snip_data, 3);
154 }
155
156
157
158 static gboolean
159 xmpp_get_local_charset(const char **charset)
160 {
161 *charset = settings_get_str("term_charset");
162 if (is_valid_charset(*charset))
163 return (g_ascii_strcasecmp(*charset, utf8_charset) == 0);
164 return g_get_charset(charset);
165 }
166
167 char *
168 xmpp_recode_out(const char *str)
169 {
170 const char *charset;
171 char *recoded, *stripped;
172
173 if (str == NULL || *str == '\0')
174 return NULL;
175 recoded = stripped = NULL;
176 signal_emit("xmpp formats strip codes", 2, str, &stripped);
177 if (stripped != NULL)
178 str = stripped;
179 if (!xmpp_get_local_charset(&charset) && charset != NULL)
180 recoded = g_convert_with_fallback(str, -1, utf8_charset,
181 charset, NULL, NULL, NULL, NULL);
182 recoded = recoded != NULL ? recoded : g_strdup(str);
183 g_free(stripped);
184 return recoded;
185 }
186
187 char *
188 xmpp_recode_in(const char *str)
189 {
190 const char *charset;
191 char *recoded, *to = NULL;
192
193 if (str == NULL || *str == '\0')
194 return NULL;
195 if (xmpp_get_local_charset(&charset) || charset == NULL)
196 return g_strdup(str);
197 if (settings_get_bool("recode_transliterate") &&
198 g_ascii_strcasecmp(charset, "//TRANSLIT") != 0)
199 charset = to = g_strconcat(charset ,"//TRANSLIT", (void *)NULL);
200 recoded = g_convert_with_fallback(str, -1, charset, utf8_charset, NULL,
201 NULL, NULL, NULL);
202 g_free(to);
203 return (recoded != NULL) ? recoded : g_strdup(str);
204 }
205
206 char *
207 xmpp_find_resource_sep(const char *jid)
208 {
209 return jid == NULL ? NULL : g_utf8_strchr(jid, -1, '/');
210 }
211
212 char *
213 xmpp_extract_resource(const char *jid)
214 {
215 char *pos;
216
217 g_return_val_if_fail(jid != NULL, NULL);
218 pos = xmpp_find_resource_sep(jid);
219 return (pos != NULL) ? g_strdup(pos + 1) : NULL;
220 }
221
222 char *
223 xmpp_strip_resource(const char *jid)
224 {
225 char *pos;
226
227 g_return_val_if_fail(jid != NULL, NULL);
228 pos = xmpp_find_resource_sep(jid);
229 return (pos != NULL) ? g_strndup(jid, pos - jid) : g_strdup(jid);
230 }
231
232 char *
233 xmpp_extract_user(const char *jid)
234 {
235 char *pos;
236
237 g_return_val_if_fail(jid != NULL, NULL);
238 pos = g_utf8_strchr(jid, -1, '@');
239 return (pos != NULL) ? g_strndup(jid, pos - jid) :
240 xmpp_strip_resource(jid);
241 }
242
243 char *
244 xmpp_extract_domain(const char *jid)
245 {
246 char *pos1, *pos2;
247
248 pos1 = g_utf8_strchr(jid, -1, '@');
249 pos2 = xmpp_find_resource_sep(jid);
250 if (pos1 == NULL)
251 return NULL;
252 if (pos2 != NULL && pos2 < pos1)
253 return g_strdup(pos1 + 1);
254 return (pos2 != NULL) ?
255 g_strndup(pos1 + 1, pos2 - pos1 - 1) : g_strdup(pos1 + 1);
256 }
257
258 gboolean
259 xmpp_have_domain(const char *jid)
260 {
261 char *pos;
262
263 g_return_val_if_fail(jid != NULL, FALSE);
264 pos = g_utf8_strchr(jid, -1, '@');
265 return (pos != NULL && *(pos+1) != '\0');
266 }
267
268 gboolean
269 xmpp_have_resource(const char *jid)
270 {
271 char *pos;
272
273 g_return_val_if_fail(jid != NULL, FALSE);
274 pos = xmpp_find_resource_sep(jid);
275 return (pos != NULL && *(pos+1) != '\0');
276 }
277
278 gboolean
279 xmpp_priority_out_of_bound(const int priority)
280 {
281 return (XMPP_PRIORITY_MIN <= priority
282 && priority <= XMPP_PRIORITY_MAX) ? FALSE : TRUE;
283 }
284
285 gboolean
286 xmpp_presence_changed(const int show, const int old_show, const char *status,
287 const char *old_status, const int priority, const int old_priority)
288 {
289 return (show != old_show)
290 || (status == NULL && old_status != NULL)
291 || (status != NULL && old_status == NULL)
292 || (status != NULL && old_status != NULL
293 && strcmp(status, old_status) != 0)
294 || (priority != old_priority);
295 }