Debian Codegraph @

src/core/tools.c

raw symbols ◀ ▶
Source reconstructed at patch state 8 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, *tmp2_path = NULL, *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_path = tempnam(NULL, "irssi-xmpp-gpg"))) goto pgp_error;
59 if((tmp2_fd = open(tmp2_path, O_WRONLY|O_CREAT|O_EXCL, \
60 S_IRUSR|S_IWUSR)) < 0)
61 goto pgp_error;
62
63 if(write(tmp2_fd, input2, strlen(input2)) < 0) goto pgp_error;
64 }
65
66 cmd = malloc(sizeof("gpg -u '' --passphrase-fd '' --trust-model always" \
67 " -qo - --batch --no-tty - ''") \
68 +strlen(switches)+8+ \
69 (tmp2_path ? strlen(tmp2_path) : 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, " --trust-model always -qo - --batch --no-tty - ");
82
83 if(tmp2_path) {
84 strcat(cmd, "'");
85 strcat(cmd, tmp2_path);
86 strcat(cmd, "'");
87 }
88
89 fflush(NULL);
90 childpid = popenRWE(rwepipe, cmd);
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(tmp2_fd) close(tmp2_fd);
141 if(tmp2_path) free(tmp2_path);
142 if(keyid) close(pipefd[0]);
143 free(cmd);
144
145 return output;
146 pgp_error:
147 output = NULL;
148 goto done;
149 }
150
151
152 char *call_gpg(char *switches, char *input, char *input2, \
153 int get_stderr, int snip_data, unsigned round) {
154 return call_gpg_round(switches, input, input2, get_stderr,
155 snip_data, 3);
156 }
157
158
159
160 static gboolean
161 xmpp_get_local_charset(const char **charset)
162 {
163 *charset = settings_get_str("term_charset");
164 if (is_valid_charset(*charset))
165 return (g_ascii_strcasecmp(*charset, utf8_charset) == 0);
166 return g_get_charset(charset);
167 }
168
169 char *
170 xmpp_recode_out(const char *str)
171 {
172 const char *charset;
173 char *recoded, *stripped;
174
175 if (str == NULL || *str == '\0')
176 return NULL;
177 recoded = stripped = NULL;
178 signal_emit("xmpp formats strip codes", 2, str, &stripped);
179 if (stripped != NULL)
180 str = stripped;
181 if (!xmpp_get_local_charset(&charset) && charset != NULL)
182 recoded = g_convert_with_fallback(str, -1, utf8_charset,
183 charset, NULL, NULL, NULL, NULL);
184 recoded = recoded != NULL ? recoded : g_strdup(str);
185 g_free(stripped);
186 return recoded;
187 }
188
189 char *
190 xmpp_recode_in(const char *str)
191 {
192 const char *charset;
193 char *recoded, *to = NULL;
194
195 if (str == NULL || *str == '\0')
196 return NULL;
197 if (xmpp_get_local_charset(&charset) || charset == NULL)
198 return g_strdup(str);
199 if (settings_get_bool("recode_transliterate") &&
200 g_ascii_strcasecmp(charset, "//TRANSLIT") != 0)
201 charset = to = g_strconcat(charset ,"//TRANSLIT", (void *)NULL);
202 recoded = g_convert_with_fallback(str, -1, charset, utf8_charset, NULL,
203 NULL, NULL, NULL);
204 g_free(to);
205 return (recoded != NULL) ? recoded : g_strdup(str);
206 }
207
208 char *
209 xmpp_find_resource_sep(const char *jid)
210 {
211 return jid == NULL ? NULL : g_utf8_strchr(jid, -1, '/');
212 }
213
214 char *
215 xmpp_extract_resource(const char *jid)
216 {
217 char *pos;
218
219 g_return_val_if_fail(jid != NULL, NULL);
220 pos = xmpp_find_resource_sep(jid);
221 return (pos != NULL) ? g_strdup(pos + 1) : NULL;
222 }
223
224 char *
225 xmpp_strip_resource(const char *jid)
226 {
227 char *pos;
228
229 g_return_val_if_fail(jid != NULL, NULL);
230 pos = xmpp_find_resource_sep(jid);
231 return (pos != NULL) ? g_strndup(jid, pos - jid) : g_strdup(jid);
232 }
233
234 char *
235 xmpp_extract_user(const char *jid)
236 {
237 char *pos;
238
239 g_return_val_if_fail(jid != NULL, NULL);
240 pos = g_utf8_strchr(jid, -1, '@');
241 return (pos != NULL) ? g_strndup(jid, pos - jid) :
242 xmpp_strip_resource(jid);
243 }
244
245 char *
246 xmpp_extract_domain(const char *jid)
247 {
248 char *pos1, *pos2;
249
250 pos1 = g_utf8_strchr(jid, -1, '@');
251 pos2 = xmpp_find_resource_sep(jid);
252 if (pos1 == NULL)
253 return NULL;
254 if (pos2 != NULL && pos2 < pos1)
255 return g_strdup(pos1 + 1);
256 return (pos2 != NULL) ?
257 g_strndup(pos1 + 1, pos2 - pos1 - 1) : g_strdup(pos1 + 1);
258 }
259
260 gboolean
261 xmpp_have_domain(const char *jid)
262 {
263 char *pos;
264
265 g_return_val_if_fail(jid != NULL, FALSE);
266 pos = g_utf8_strchr(jid, -1, '@');
267 return (pos != NULL && *(pos+1) != '\0');
268 }
269
270 gboolean
271 xmpp_have_resource(const char *jid)
272 {
273 char *pos;
274
275 g_return_val_if_fail(jid != NULL, FALSE);
276 pos = xmpp_find_resource_sep(jid);
277 return (pos != NULL && *(pos+1) != '\0');
278 }
279
280 gboolean
281 xmpp_priority_out_of_bound(const int priority)
282 {
283 return (XMPP_PRIORITY_MIN <= priority
284 && priority <= XMPP_PRIORITY_MAX) ? FALSE : TRUE;
285 }
286
287 gboolean
288 xmpp_presence_changed(const int show, const int old_show, const char *status,
289 const char *old_status, const int priority, const int old_priority)
290 {
291 return (show != old_show)
292 || (status == NULL && old_status != NULL)
293 || (status != NULL && old_status == NULL)
294 || (status != NULL && old_status != NULL
295 && strcmp(status, old_status) != 0)
296 || (priority != old_priority);
297 }