Debian Codegraph @

src/core/tools.c

raw symbols ◀ ▶
Source reconstructed at patch state 10 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
47 /* If no keyID, then we don't need a password */
48 if(keyid && !settings_get_str("xmpp_pgp_agent")) {
49 if(pipe(pipefd)) goto pgp_error;
50 if(!pgp_passwd) pgp_passwd = get_password("OpenPGP Password:");
51 if(!pgp_passwd) goto pgp_error;
52
53 if(write(pipefd[1], pgp_passwd, strlen(pgp_passwd)) < 1) goto pgp_error;
54 if(close(pipefd[1])) goto pgp_error;
55 }
56
57 if(input2) { /* NOTE for security it might be better if this were a named pipe */
58 if((tmp2_fd = mkstemp("irssi-xmpp-gpgXXXXXX")) == -1) goto pgp_error;
59 if(write(tmp2_fd, input2, strlen(input2)) < 0) goto pgp_error;
60 }
61
62 cmd = malloc(sizeof("gpg --enable-special-filenames -u ''" \
63 "--passphrase-fd '' --trust-model always" \
64 " -qo - --batch --no-tty - '' -&") \
65 +1+strlen(switches)+5+ /* 5 is for passphrase-fd */ \
66 (keyid ? strlen(keyid) : 0)+ \
67 (keyid ? strlen(keyid) : 0)+ \
68 (tmp2_fd ? 5 : 0));
69 if(keyid) {
70 strcpy(cmd, "gpg -u '");
71 strcat(cmd, keyid);
72 strcat(cmd, "' ");
73 if(!settings_get_str("xmpp_pgp_agent")) {
74 sprintf(cmd+strlen(cmd), "--passphrase-fd '%d' ", pipefd[0]);
75 }
76 } else {
77 strcpy(cmd, "gpg ");
78 }
79 strcat(cmd, switches);
80 strcat(cmd, " --enable-special-filenames --trust-model always -qo -" \
81 " --batch --no-tty - ");
82
83 if(tmp2_fd) {
84 sprintf(cmd+strlen(cmd), "-&%d", tmp2_fd);
85 }
86
87 fflush(NULL);
88 childpid = popenRWE(rwepipe, cmd);
89
90 if(write(rwepipe[0], input, strlen(input)) < 0) goto pgp_error;
91 if(close(rwepipe[0])) goto pgp_error;
92
93 if(get_stderr) {
94 cstream = fdopen(rwepipe[2], "r");
95 } else {
96 cstream = fdopen(rwepipe[1], "r");
97 }
98 if(!cstream) goto pgp_error;
99
100 while(fgets(buf, sizeof(buf)-1, cstream)) {
101 if(strlen(buf2) > 0) {
102 output = realloc(output, output_size+strlen(buf2)+1);
103 if(!output) goto pgp_error;
104 if(output_size < 1) output[0] = '\0';
105 output_size += strlen(buf2);
106 strcat(output, buf2);
107 }
108
109 if(!in_data && buf[0] == '\n') {
110 in_data = 1;
111 continue;
112 } else if(in_data) {
113 strcpy(buf2, buf);
114 }
115 }
116
117 /* Get last line if not snipping */
118 if(!snip_data && strlen(buf2) > 0) {
119 output = realloc(output, output_size+strlen(buf2)+1);
120 if(!output) goto pgp_error;
121 if(output_size < 1) output[0] = '\0';
122 output_size += strlen(buf2);
123 strcat(output, buf2);
124 }
125
126 // http://www.gnu-darwin.org/www001/src/ports/security/libgpg-error/work/libgpg-error-1.5/src/err-codes.h.in
127 // 11 GPG_ERR_BAD_PASSPHRASE Bad passphrase
128 // 31 GPG_ERR_INV_PASSPHRASE Invalid passphrase
129 int exit_status = WEXITSTATUS(pcloseRWE(childpid, rwepipe));
130 if(round > 0 && (exit_status == 11 || exit_status == 31)) {
131 g_free(pgp_passwd);
132 pgp_passwd = NULL;
133 output = call_gpg_round(switches, input, input2, get_stderr,
134 snip_data, round--);
135 }
136
137 done:
138 if(tmp2_fd) close(tmp2_fd);
139 if(keyid) close(pipefd[0]);
140 free(cmd);
141
142 return output;
143 pgp_error:
144 output = NULL;
145 goto done;
146 }
147
148
149 char *call_gpg(char *switches, char *input, char *input2, \
150 int get_stderr, int snip_data, unsigned round) {
151 return call_gpg_round(switches, input, input2, get_stderr,
152 snip_data, 3);
153 }
154
155
156
157 static gboolean
158 xmpp_get_local_charset(const char **charset)
159 {
160 *charset = settings_get_str("term_charset");
161 if (is_valid_charset(*charset))
162 return (g_ascii_strcasecmp(*charset, utf8_charset) == 0);
163 return g_get_charset(charset);
164 }
165
166 char *
167 xmpp_recode_out(const char *str)
168 {
169 const char *charset;
170 char *recoded, *stripped;
171
172 if (str == NULL || *str == '\0')
173 return NULL;
174 recoded = stripped = NULL;
175 signal_emit("xmpp formats strip codes", 2, str, &stripped);
176 if (stripped != NULL)
177 str = stripped;
178 if (!xmpp_get_local_charset(&charset) && charset != NULL)
179 recoded = g_convert_with_fallback(str, -1, utf8_charset,
180 charset, NULL, NULL, NULL, NULL);
181 recoded = recoded != NULL ? recoded : g_strdup(str);
182 g_free(stripped);
183 return recoded;
184 }
185
186 char *
187 xmpp_recode_in(const char *str)
188 {
189 const char *charset;
190 char *recoded, *to = NULL;
191
192 if (str == NULL || *str == '\0')
193 return NULL;
194 if (xmpp_get_local_charset(&charset) || charset == NULL)
195 return g_strdup(str);
196 if (settings_get_bool("recode_transliterate") &&
197 g_ascii_strcasecmp(charset, "//TRANSLIT") != 0)
198 charset = to = g_strconcat(charset ,"//TRANSLIT", (void *)NULL);
199 recoded = g_convert_with_fallback(str, -1, charset, utf8_charset, NULL,
200 NULL, NULL, NULL);
201 g_free(to);
202 return (recoded != NULL) ? recoded : g_strdup(str);
203 }
204
205 char *
206 xmpp_find_resource_sep(const char *jid)
207 {
208 return jid == NULL ? NULL : g_utf8_strchr(jid, -1, '/');
209 }
210
211 char *
212 xmpp_extract_resource(const char *jid)
213 {
214 char *pos;
215
216 g_return_val_if_fail(jid != NULL, NULL);
217 pos = xmpp_find_resource_sep(jid);
218 return (pos != NULL) ? g_strdup(pos + 1) : NULL;
219 }
220
221 char *
222 xmpp_strip_resource(const char *jid)
223 {
224 char *pos;
225
226 g_return_val_if_fail(jid != NULL, NULL);
227 pos = xmpp_find_resource_sep(jid);
228 return (pos != NULL) ? g_strndup(jid, pos - jid) : g_strdup(jid);
229 }
230
231 char *
232 xmpp_extract_user(const char *jid)
233 {
234 char *pos;
235
236 g_return_val_if_fail(jid != NULL, NULL);
237 pos = g_utf8_strchr(jid, -1, '@');
238 return (pos != NULL) ? g_strndup(jid, pos - jid) :
239 xmpp_strip_resource(jid);
240 }
241
242 char *
243 xmpp_extract_domain(const char *jid)
244 {
245 char *pos1, *pos2;
246
247 pos1 = g_utf8_strchr(jid, -1, '@');
248 pos2 = xmpp_find_resource_sep(jid);
249 if (pos1 == NULL)
250 return NULL;
251 if (pos2 != NULL && pos2 < pos1)
252 return g_strdup(pos1 + 1);
253 return (pos2 != NULL) ?
254 g_strndup(pos1 + 1, pos2 - pos1 - 1) : g_strdup(pos1 + 1);
255 }
256
257 gboolean
258 xmpp_have_domain(const char *jid)
259 {
260 char *pos;
261
262 g_return_val_if_fail(jid != NULL, FALSE);
263 pos = g_utf8_strchr(jid, -1, '@');
264 return (pos != NULL && *(pos+1) != '\0');
265 }
266
267 gboolean
268 xmpp_have_resource(const char *jid)
269 {
270 char *pos;
271
272 g_return_val_if_fail(jid != NULL, FALSE);
273 pos = xmpp_find_resource_sep(jid);
274 return (pos != NULL && *(pos+1) != '\0');
275 }
276
277 gboolean
278 xmpp_priority_out_of_bound(const int priority)
279 {
280 return (XMPP_PRIORITY_MIN <= priority
281 && priority <= XMPP_PRIORITY_MAX) ? FALSE : TRUE;
282 }
283
284 gboolean
285 xmpp_presence_changed(const int show, const int old_show, const char *status,
286 const char *old_status, const int priority, const int old_priority)
287 {
288 return (show != old_show)
289 || (status == NULL && old_status != NULL)
290 || (status != NULL && old_status == NULL)
291 || (status != NULL && old_status != NULL
292 && strcmp(status, old_status) != 0)
293 || (priority != old_priority);
294 }