Debian Codegraph @

src/core/tools.c

raw symbols ◀ ▶
Source reconstructed at patch state 7 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 if(tmp2_fd) close(tmp2_fd);
140 if(tmp2_path) free(tmp2_path);
141 if(keyid) close(pipefd[0]);
142 free(cmd);
143
144 return output;
145 pgp_error:
146 return NULL;
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 }