Debian Codegraph @

src/core/tools.c

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