1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* |
---|
8 | OTR support (cf. http://www.cypherpunks.ca/otr/) |
---|
9 | |
---|
10 | (c) 2008-2011,2013 Sven Moritz Hallberg <pesco@khjk.org> |
---|
11 | funded by stonedcoder.org |
---|
12 | |
---|
13 | files used to store OTR data: |
---|
14 | <configdir>/<nick>.otr_keys |
---|
15 | <configdir>/<nick>.otr_fprints |
---|
16 | <configdir>/<nick>.otr_instags <- don't copy this one between hosts |
---|
17 | |
---|
18 | top-level todos: (search for TODO for more ;-)) |
---|
19 | integrate otr_load/otr_save with existing storage backends |
---|
20 | per-account policy settings |
---|
21 | per-user policy settings |
---|
22 | add a way to select recipient instance |
---|
23 | */ |
---|
24 | |
---|
25 | /* |
---|
26 | This program is free software; you can redistribute it and/or modify |
---|
27 | it under the terms of the GNU General Public License as published by |
---|
28 | the Free Software Foundation; either version 2 of the License, or |
---|
29 | (at your option) any later version. |
---|
30 | |
---|
31 | This program is distributed in the hope that it will be useful, |
---|
32 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
33 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
34 | GNU General Public License for more details. |
---|
35 | |
---|
36 | You should have received a copy of the GNU General Public License with |
---|
37 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
38 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
39 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
40 | */ |
---|
41 | |
---|
42 | #include "bitlbee.h" |
---|
43 | #include "irc.h" |
---|
44 | #include "otr.h" |
---|
45 | #include <sys/types.h> |
---|
46 | #include <sys/wait.h> |
---|
47 | #include <unistd.h> |
---|
48 | #include <assert.h> |
---|
49 | #include <signal.h> |
---|
50 | |
---|
51 | |
---|
52 | /** OTR interface routines for the OtrlMessageAppOps struct: **/ |
---|
53 | |
---|
54 | OtrlPolicy op_policy(void *opdata, ConnContext *context); |
---|
55 | |
---|
56 | void op_create_privkey(void *opdata, const char *accountname, const char *protocol); |
---|
57 | |
---|
58 | int op_is_logged_in(void *opdata, const char *accountname, const char *protocol, |
---|
59 | const char *recipient); |
---|
60 | |
---|
61 | void op_inject_message(void *opdata, const char *accountname, const char *protocol, |
---|
62 | const char *recipient, const char *message); |
---|
63 | |
---|
64 | void op_new_fingerprint(void *opdata, OtrlUserState us, const char *accountname, |
---|
65 | const char *protocol, const char *username, unsigned char fingerprint[20]); |
---|
66 | |
---|
67 | void op_write_fingerprints(void *opdata); |
---|
68 | |
---|
69 | void op_gone_secure(void *opdata, ConnContext *context); |
---|
70 | |
---|
71 | void op_gone_insecure(void *opdata, ConnContext *context); |
---|
72 | |
---|
73 | void op_still_secure(void *opdata, ConnContext *context, int is_reply); |
---|
74 | |
---|
75 | void op_log_message(void *opdata, const char *message); |
---|
76 | |
---|
77 | int op_max_message_size(void *opdata, ConnContext *context); |
---|
78 | |
---|
79 | const char *op_account_name(void *opdata, const char *account, const char *protocol); |
---|
80 | |
---|
81 | void op_create_instag(void *opdata, const char *account, const char *protocol); |
---|
82 | |
---|
83 | void op_convert_msg(void *opdata, ConnContext *ctx, OtrlConvertType typ, |
---|
84 | char **dst, const char *src); |
---|
85 | void op_convert_free(void *opdata, ConnContext *ctx, char *msg); |
---|
86 | |
---|
87 | void op_handle_smp_event(void *opdata, OtrlSMPEvent ev, ConnContext *ctx, |
---|
88 | unsigned short percent, char *question); |
---|
89 | |
---|
90 | void op_handle_msg_event(void *opdata, OtrlMessageEvent ev, ConnContext *ctx, |
---|
91 | const char *message, gcry_error_t err); |
---|
92 | |
---|
93 | const char *op_otr_error_message(void *opdata, ConnContext *ctx, |
---|
94 | OtrlErrorCode err_code); |
---|
95 | |
---|
96 | /** otr sub-command handlers: **/ |
---|
97 | |
---|
98 | static void cmd_otr(irc_t *irc, char **args); |
---|
99 | void cmd_otr_connect(irc_t *irc, char **args); |
---|
100 | void cmd_otr_disconnect(irc_t *irc, char **args); |
---|
101 | void cmd_otr_reconnect(irc_t *irc, char **args); |
---|
102 | void cmd_otr_smp(irc_t *irc, char **args); |
---|
103 | void cmd_otr_smpq(irc_t *irc, char **args); |
---|
104 | void cmd_otr_trust(irc_t *irc, char **args); |
---|
105 | void cmd_otr_info(irc_t *irc, char **args); |
---|
106 | void cmd_otr_keygen(irc_t *irc, char **args); |
---|
107 | void cmd_otr_forget(irc_t *irc, char **args); |
---|
108 | |
---|
109 | const command_t otr_commands[] = { |
---|
110 | { "connect", 1, &cmd_otr_connect, 0 }, |
---|
111 | { "disconnect", 1, &cmd_otr_disconnect, 0 }, |
---|
112 | { "reconnect", 1, &cmd_otr_reconnect, 0 }, |
---|
113 | { "smp", 2, &cmd_otr_smp, 0 }, |
---|
114 | { "smpq", 3, &cmd_otr_smpq, 0 }, |
---|
115 | { "trust", 6, &cmd_otr_trust, 0 }, |
---|
116 | { "info", 0, &cmd_otr_info, 0 }, |
---|
117 | { "keygen", 1, &cmd_otr_keygen, 0 }, |
---|
118 | { "forget", 2, &cmd_otr_forget, 0 }, |
---|
119 | { NULL } |
---|
120 | }; |
---|
121 | |
---|
122 | typedef struct { |
---|
123 | void *fst; |
---|
124 | void *snd; |
---|
125 | } pair_t; |
---|
126 | |
---|
127 | static OtrlMessageAppOps otr_ops; /* collects interface functions required by OTR */ |
---|
128 | |
---|
129 | |
---|
130 | /** misc. helpers/subroutines: **/ |
---|
131 | |
---|
132 | /* check whether we are already generating a key for a given account */ |
---|
133 | int keygen_in_progress(irc_t *irc, const char *handle, const char *protocol); |
---|
134 | |
---|
135 | /* start background process to generate a (new) key for a given account */ |
---|
136 | void otr_keygen(irc_t *irc, const char *handle, const char *protocol); |
---|
137 | |
---|
138 | /* main function for the forked keygen slave */ |
---|
139 | void keygen_child_main(OtrlUserState us, int infd, int outfd); |
---|
140 | |
---|
141 | /* mainloop handler for when a keygen finishes */ |
---|
142 | gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond); |
---|
143 | |
---|
144 | /* copy the contents of file a to file b, overwriting it if it exists */ |
---|
145 | void copyfile(const char *a, const char *b); |
---|
146 | |
---|
147 | /* read one line of input from a stream, excluding trailing newline */ |
---|
148 | void myfgets(char *s, int size, FILE *stream); |
---|
149 | |
---|
150 | /* some yes/no handlers */ |
---|
151 | void yes_keygen(void *data); |
---|
152 | void yes_forget_fingerprint(void *data); |
---|
153 | void yes_forget_context(void *data); |
---|
154 | void yes_forget_key(void *data); |
---|
155 | |
---|
156 | /* timeout handler that calls otrl_message_poll */ |
---|
157 | gboolean ev_message_poll(gpointer data, gint fd, b_input_condition cond); |
---|
158 | |
---|
159 | /* helper to make sure accountname and protocol match the incoming "opdata" */ |
---|
160 | struct im_connection *check_imc(void *opdata, const char *accountname, |
---|
161 | const char *protocol); |
---|
162 | |
---|
163 | /* determine the nick for a given handle/protocol pair |
---|
164 | returns "handle/protocol" if not found */ |
---|
165 | const char *peernick(irc_t *irc, const char *handle, const char *protocol); |
---|
166 | |
---|
167 | /* turn a hexadecimal digit into its numerical value */ |
---|
168 | int hexval(char a); |
---|
169 | |
---|
170 | /* determine the irc_user_t for a given handle/protocol pair |
---|
171 | returns NULL if not found */ |
---|
172 | irc_user_t *peeruser(irc_t *irc, const char *handle, const char *protocol); |
---|
173 | |
---|
174 | /* show an otr-related message to the user */ |
---|
175 | void display_otr_message(void *opdata, ConnContext *ctx, const char *fmt, ...); |
---|
176 | |
---|
177 | /* write an otr-related message to the system log */ |
---|
178 | void log_otr_message(void *opdata, const char *fmt, ...); |
---|
179 | |
---|
180 | /* combined handler for the 'otr smp' and 'otr smpq' commands */ |
---|
181 | void otr_smp_or_smpq(irc_t *irc, const char *nick, const char *question, |
---|
182 | const char *secret); |
---|
183 | |
---|
184 | /* update flags within the irc_user structure to reflect OTR status of context */ |
---|
185 | void otr_update_uflags(ConnContext *context, irc_user_t *u); |
---|
186 | |
---|
187 | /* update op/voice flag of given user according to encryption state and settings |
---|
188 | returns 0 if neither op_buddies nor voice_buddies is set to "encrypted", |
---|
189 | i.e. msgstate should be announced separately */ |
---|
190 | int otr_update_modeflags(irc_t *irc, irc_user_t *u); |
---|
191 | |
---|
192 | /* show general info about the OTR subsystem; called by 'otr info' */ |
---|
193 | void show_general_otr_info(irc_t *irc); |
---|
194 | |
---|
195 | /* show info about a given OTR context and subcontexts/instances. bestctx |
---|
196 | may be either NULL or preferred destination context (this is hilighted |
---|
197 | in the output as being the target for a message) */ |
---|
198 | void show_otr_context_info(irc_t *irc, ConnContext *ctx, ConnContext *bestctx); |
---|
199 | |
---|
200 | /* show the list of fingerprints associated with a given context */ |
---|
201 | void show_fingerprints(irc_t *irc, ConnContext *ctx); |
---|
202 | |
---|
203 | /* find a fingerprint by prefix (given as any number of hex strings) */ |
---|
204 | Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args); |
---|
205 | |
---|
206 | /* find a private key by fingerprint prefix (given as any number of hex strings) */ |
---|
207 | OtrlPrivKey *match_privkey(irc_t *irc, const char **args); |
---|
208 | |
---|
209 | /* check whether a string is safe to use in a path component */ |
---|
210 | int strsane(const char *s); |
---|
211 | |
---|
212 | /* close the OTR connection with the given buddy */ |
---|
213 | gboolean otr_disconnect_user(irc_t *irc, irc_user_t *u); |
---|
214 | |
---|
215 | /* close all active OTR connections */ |
---|
216 | void otr_disconnect_all(irc_t *irc); |
---|
217 | |
---|
218 | /* modifies string in-place, replacing \x03 with '?', |
---|
219 | as a quick way to prevent remote users from messing with irc colors */ |
---|
220 | static char *otr_filter_colors(char *msg); |
---|
221 | |
---|
222 | /* functions to be called for certain events */ |
---|
223 | static const struct irc_plugin otr_plugin; |
---|
224 | |
---|
225 | #define OTR_COLOR_TRUSTED "03" /* green */ |
---|
226 | #define OTR_COLOR_UNTRUSTED "05" /* red */ |
---|
227 | |
---|
228 | /*** routines declared in otr.h: ***/ |
---|
229 | |
---|
230 | #ifdef OTR_BI |
---|
231 | #define init_plugin otr_init |
---|
232 | #endif |
---|
233 | |
---|
234 | void init_plugin(void) |
---|
235 | { |
---|
236 | OTRL_INIT; |
---|
237 | |
---|
238 | /* fill global OtrlMessageAppOps */ |
---|
239 | otr_ops.policy = &op_policy; |
---|
240 | otr_ops.create_privkey = &op_create_privkey; |
---|
241 | otr_ops.is_logged_in = &op_is_logged_in; |
---|
242 | otr_ops.inject_message = &op_inject_message; |
---|
243 | otr_ops.update_context_list = NULL; |
---|
244 | otr_ops.new_fingerprint = &op_new_fingerprint; |
---|
245 | otr_ops.write_fingerprints = &op_write_fingerprints; |
---|
246 | otr_ops.gone_secure = &op_gone_secure; |
---|
247 | otr_ops.gone_insecure = &op_gone_insecure; |
---|
248 | otr_ops.still_secure = &op_still_secure; |
---|
249 | otr_ops.max_message_size = &op_max_message_size; |
---|
250 | otr_ops.account_name = &op_account_name; |
---|
251 | otr_ops.account_name_free = NULL; |
---|
252 | |
---|
253 | /* stuff added with libotr 4.0.0 */ |
---|
254 | otr_ops.received_symkey = NULL; /* we don't use the extra key */ |
---|
255 | otr_ops.otr_error_message = &op_otr_error_message; |
---|
256 | otr_ops.otr_error_message_free = NULL; |
---|
257 | otr_ops.resent_msg_prefix = NULL; /* default: [resent] */ |
---|
258 | otr_ops.resent_msg_prefix_free = NULL; |
---|
259 | otr_ops.handle_smp_event = &op_handle_smp_event; |
---|
260 | otr_ops.handle_msg_event = &op_handle_msg_event; |
---|
261 | otr_ops.create_instag = &op_create_instag; |
---|
262 | otr_ops.convert_msg = &op_convert_msg; |
---|
263 | otr_ops.convert_free = &op_convert_free; |
---|
264 | otr_ops.timer_control = NULL; /* we just poll */ |
---|
265 | |
---|
266 | root_command_add("otr", 1, cmd_otr, 0); |
---|
267 | register_irc_plugin(&otr_plugin); |
---|
268 | } |
---|
269 | |
---|
270 | #ifndef OTR_BI |
---|
271 | struct plugin_info *init_plugin_info(void) |
---|
272 | { |
---|
273 | static struct plugin_info info = { |
---|
274 | BITLBEE_ABI_VERSION_CODE, |
---|
275 | "otr", |
---|
276 | BITLBEE_VERSION, |
---|
277 | "Off-the-Record communication", |
---|
278 | NULL, |
---|
279 | NULL |
---|
280 | }; |
---|
281 | |
---|
282 | return &info; |
---|
283 | } |
---|
284 | #endif |
---|
285 | |
---|
286 | gboolean otr_irc_new(irc_t *irc) |
---|
287 | { |
---|
288 | set_t *s; |
---|
289 | GSList *l; |
---|
290 | |
---|
291 | irc->otr = g_new0(otr_t, 1); |
---|
292 | irc->otr->us = otrl_userstate_create(); |
---|
293 | |
---|
294 | s = set_add(&irc->b->set, "otr_color_encrypted", "true", set_eval_bool, irc); |
---|
295 | |
---|
296 | s = set_add(&irc->b->set, "otr_policy", "opportunistic", set_eval_list, irc); |
---|
297 | l = g_slist_prepend(NULL, "never"); |
---|
298 | l = g_slist_prepend(l, "opportunistic"); |
---|
299 | l = g_slist_prepend(l, "manual"); |
---|
300 | l = g_slist_prepend(l, "always"); |
---|
301 | s->eval_data = l; |
---|
302 | |
---|
303 | s = set_add(&irc->b->set, "otr_does_html", "true", set_eval_bool, irc); |
---|
304 | |
---|
305 | /* regularly call otrl_message_poll */ |
---|
306 | gint definterval = otrl_message_poll_get_default_interval(irc->otr->us); |
---|
307 | irc->otr->timer = b_timeout_add(definterval, ev_message_poll, irc->otr); |
---|
308 | |
---|
309 | return TRUE; |
---|
310 | } |
---|
311 | |
---|
312 | void otr_irc_free(irc_t *irc) |
---|
313 | { |
---|
314 | set_t *s; |
---|
315 | otr_t *otr = irc->otr; |
---|
316 | |
---|
317 | otr_disconnect_all(irc); |
---|
318 | b_event_remove(otr->timer); |
---|
319 | otrl_userstate_free(otr->us); |
---|
320 | |
---|
321 | s = set_find(&irc->b->set, "otr_policy"); |
---|
322 | g_slist_free(s->eval_data); |
---|
323 | |
---|
324 | if (otr->keygen) { |
---|
325 | kill(otr->keygen, SIGTERM); |
---|
326 | waitpid(otr->keygen, NULL, 0); |
---|
327 | /* TODO: remove stale keygen tempfiles */ |
---|
328 | } |
---|
329 | if (otr->to) { |
---|
330 | fclose(otr->to); |
---|
331 | } |
---|
332 | if (otr->from) { |
---|
333 | fclose(otr->from); |
---|
334 | } |
---|
335 | while (otr->todo) { |
---|
336 | kg_t *p = otr->todo; |
---|
337 | otr->todo = p->next; |
---|
338 | g_free(p); |
---|
339 | } |
---|
340 | g_free(otr); |
---|
341 | } |
---|
342 | |
---|
343 | void otr_load(irc_t *irc) |
---|
344 | { |
---|
345 | char s[512]; |
---|
346 | account_t *a; |
---|
347 | gcry_error_t e; |
---|
348 | gcry_error_t enoent = gcry_error_from_errno(ENOENT); |
---|
349 | int kg = 0; |
---|
350 | |
---|
351 | if (strsane(irc->user->nick)) { |
---|
352 | g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, irc->user->nick); |
---|
353 | e = otrl_privkey_read(irc->otr->us, s); |
---|
354 | if (e && e != enoent) { |
---|
355 | irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e)); |
---|
356 | } |
---|
357 | g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick); |
---|
358 | e = otrl_privkey_read_fingerprints(irc->otr->us, s, NULL, NULL); |
---|
359 | if (e && e != enoent) { |
---|
360 | irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e)); |
---|
361 | } |
---|
362 | g_snprintf(s, 511, "%s%s.otr_instags", global.conf->configdir, irc->user->nick); |
---|
363 | e = otrl_instag_read(irc->otr->us, s); |
---|
364 | if (e && e != enoent) { |
---|
365 | irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e)); |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | /* check for otr keys on all accounts */ |
---|
370 | for (a = irc->b->accounts; a; a = a->next) { |
---|
371 | kg = otr_check_for_key(a) || kg; |
---|
372 | } |
---|
373 | if (kg) { |
---|
374 | irc_rootmsg(irc, "Notice: " |
---|
375 | "The accounts above do not have OTR encryption keys associated with them, yet. " |
---|
376 | "These keys are now being generated in the background. " |
---|
377 | "You will be notified as they are completed. " |
---|
378 | "It is not necessary to wait; " |
---|
379 | "BitlBee can be used normally during key generation. " |
---|
380 | "You may safely ignore this message if you don't know what OTR is. ;)"); |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | void otr_save(irc_t *irc) |
---|
385 | { |
---|
386 | char s[512]; |
---|
387 | gcry_error_t e; |
---|
388 | |
---|
389 | if (strsane(irc->user->nick)) { |
---|
390 | g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick); |
---|
391 | e = otrl_privkey_write_fingerprints(irc->otr->us, s); |
---|
392 | if (e) { |
---|
393 | irc_rootmsg(irc, "otr save: %s: %s", s, gcry_strerror(e)); |
---|
394 | } |
---|
395 | chmod(s, 0600); |
---|
396 | } |
---|
397 | } |
---|
398 | |
---|
399 | void otr_remove(const char *nick) |
---|
400 | { |
---|
401 | char s[512]; |
---|
402 | |
---|
403 | if (strsane(nick)) { |
---|
404 | g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, nick); |
---|
405 | unlink(s); |
---|
406 | g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, nick); |
---|
407 | unlink(s); |
---|
408 | } |
---|
409 | } |
---|
410 | |
---|
411 | void otr_rename(const char *onick, const char *nnick) |
---|
412 | { |
---|
413 | char s[512], t[512]; |
---|
414 | |
---|
415 | if (strsane(nnick) && strsane(onick)) { |
---|
416 | g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, onick); |
---|
417 | g_snprintf(t, 511, "%s%s.otr_keys", global.conf->configdir, nnick); |
---|
418 | rename(s, t); |
---|
419 | g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, onick); |
---|
420 | g_snprintf(t, 511, "%s%s.otr_fprints", global.conf->configdir, nnick); |
---|
421 | rename(s, t); |
---|
422 | } |
---|
423 | } |
---|
424 | |
---|
425 | int otr_check_for_key(account_t *a) |
---|
426 | { |
---|
427 | irc_t *irc = a->bee->ui_data; |
---|
428 | OtrlPrivKey *k; |
---|
429 | |
---|
430 | /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */ |
---|
431 | if (a->prpl->options & PRPL_OPT_NOOTR) { |
---|
432 | return 0; |
---|
433 | } |
---|
434 | |
---|
435 | k = otrl_privkey_find(irc->otr->us, a->user, a->prpl->name); |
---|
436 | if (k) { |
---|
437 | irc_rootmsg(irc, "otr: %s/%s ready", a->user, a->prpl->name); |
---|
438 | return 0; |
---|
439 | } |
---|
440 | if (keygen_in_progress(irc, a->user, a->prpl->name)) { |
---|
441 | irc_rootmsg(irc, "otr: keygen for %s/%s already in progress", a->user, a->prpl->name); |
---|
442 | return 0; |
---|
443 | } else { |
---|
444 | irc_rootmsg(irc, "otr: starting background keygen for %s/%s", a->user, a->prpl->name); |
---|
445 | otr_keygen(irc, a->user, a->prpl->name); |
---|
446 | return 1; |
---|
447 | } |
---|
448 | } |
---|
449 | |
---|
450 | char *otr_filter_msg_in(irc_user_t *iu, char *msg, int flags) |
---|
451 | { |
---|
452 | int ignore_msg; |
---|
453 | char *newmsg = NULL; |
---|
454 | OtrlTLV *tlvs = NULL; |
---|
455 | irc_t *irc = iu->irc; |
---|
456 | struct im_connection *ic = iu->bu->ic; |
---|
457 | |
---|
458 | /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */ |
---|
459 | if (ic->acc->prpl->options & PRPL_OPT_NOOTR || |
---|
460 | iu->bu->flags & BEE_USER_NOOTR) { |
---|
461 | return msg; |
---|
462 | } |
---|
463 | |
---|
464 | ignore_msg = otrl_message_receiving(irc->otr->us, &otr_ops, ic, |
---|
465 | ic->acc->user, ic->acc->prpl->name, iu->bu->handle, msg, &newmsg, |
---|
466 | &tlvs, NULL, NULL, NULL); |
---|
467 | |
---|
468 | if (tlvs) { |
---|
469 | otrl_tlv_free(tlvs); |
---|
470 | } |
---|
471 | |
---|
472 | if (ignore_msg) { |
---|
473 | /* this was an internal OTR protocol message */ |
---|
474 | return NULL; |
---|
475 | } else if (!newmsg) { |
---|
476 | /* this was a non-OTR message */ |
---|
477 | return otr_filter_colors(msg); |
---|
478 | } else { |
---|
479 | /* we're done with the original msg, which will be caller-freed. */ |
---|
480 | return newmsg; |
---|
481 | } |
---|
482 | } |
---|
483 | |
---|
484 | char *otr_filter_msg_out(irc_user_t *iu, char *msg, int flags) |
---|
485 | { |
---|
486 | int st; |
---|
487 | char *otrmsg = NULL; |
---|
488 | ConnContext *ctx = NULL; |
---|
489 | irc_t *irc = iu->irc; |
---|
490 | struct im_connection *ic = iu->bu->ic; |
---|
491 | otrl_instag_t instag = OTRL_INSTAG_BEST; // XXX? |
---|
492 | |
---|
493 | /* NB: in libotr 4.0.0 OTRL_INSTAG_RECENT will cause a null-pointer deref |
---|
494 | * in otrl_message_sending with newly-added OTR contexts. |
---|
495 | */ |
---|
496 | |
---|
497 | /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */ |
---|
498 | if (ic->acc->prpl->options & OPT_NOOTR || |
---|
499 | iu->bu->flags & BEE_USER_NOOTR) { |
---|
500 | return msg; |
---|
501 | } |
---|
502 | |
---|
503 | st = otrl_message_sending(irc->otr->us, &otr_ops, ic, |
---|
504 | ic->acc->user, ic->acc->prpl->name, iu->bu->handle, instag, |
---|
505 | msg, NULL, &otrmsg, OTRL_FRAGMENT_SEND_ALL_BUT_LAST, &ctx, NULL, NULL); |
---|
506 | |
---|
507 | if (otrmsg && otrmsg != msg) { |
---|
508 | /* libotr wants us to replace our message */ |
---|
509 | /* NB: caller will free old msg */ |
---|
510 | msg = st ? NULL : g_strdup(otrmsg); |
---|
511 | otrl_message_free(otrmsg); |
---|
512 | } |
---|
513 | |
---|
514 | if (st) { |
---|
515 | irc_usernotice(iu, "otr: error handling outgoing message: %d", st); |
---|
516 | msg = NULL; /* do not send plaintext! */ |
---|
517 | } |
---|
518 | |
---|
519 | return msg; |
---|
520 | } |
---|
521 | |
---|
522 | static const struct irc_plugin otr_plugin = |
---|
523 | { |
---|
524 | otr_irc_new, |
---|
525 | otr_irc_free, |
---|
526 | otr_filter_msg_out, |
---|
527 | otr_filter_msg_in, |
---|
528 | otr_load, |
---|
529 | otr_save, |
---|
530 | otr_remove, |
---|
531 | }; |
---|
532 | |
---|
533 | static void cmd_otr(irc_t *irc, char **args) |
---|
534 | { |
---|
535 | const command_t *cmd; |
---|
536 | |
---|
537 | if (!args[0]) { |
---|
538 | return; |
---|
539 | } |
---|
540 | |
---|
541 | if (!args[1]) { |
---|
542 | return; |
---|
543 | } |
---|
544 | |
---|
545 | for (cmd = otr_commands; cmd->command; cmd++) { |
---|
546 | if (strcmp(cmd->command, args[1]) == 0) { |
---|
547 | break; |
---|
548 | } |
---|
549 | } |
---|
550 | |
---|
551 | if (!cmd->command) { |
---|
552 | irc_rootmsg(irc, "%s: unknown subcommand \"%s\", see \x02help otr\x02", |
---|
553 | args[0], args[1]); |
---|
554 | return; |
---|
555 | } |
---|
556 | |
---|
557 | if (!args[cmd->required_parameters + 1]) { |
---|
558 | irc_rootmsg(irc, "%s %s: not enough arguments (%d req.)", |
---|
559 | args[0], args[1], cmd->required_parameters); |
---|
560 | return; |
---|
561 | } |
---|
562 | |
---|
563 | cmd->execute(irc, args + 1); |
---|
564 | } |
---|
565 | |
---|
566 | |
---|
567 | /*** OTR "MessageAppOps" callbacks for global.otr_ui: ***/ |
---|
568 | |
---|
569 | OtrlPolicy op_policy(void *opdata, ConnContext *context) |
---|
570 | { |
---|
571 | struct im_connection *ic = check_imc(opdata, context->accountname, context->protocol); |
---|
572 | irc_t *irc = ic->bee->ui_data; |
---|
573 | const char *p; |
---|
574 | |
---|
575 | /* policy override during keygen: if we're missing the key for context but are currently |
---|
576 | generating it, then that's as much as we can do. => temporarily return NEVER. */ |
---|
577 | if (keygen_in_progress(irc, context->accountname, context->protocol) && |
---|
578 | !otrl_privkey_find(irc->otr->us, context->accountname, context->protocol)) { |
---|
579 | return OTRL_POLICY_NEVER; |
---|
580 | } |
---|
581 | |
---|
582 | p = set_getstr(&ic->bee->set, "otr_policy"); |
---|
583 | if (!strcmp(p, "never")) { |
---|
584 | return OTRL_POLICY_NEVER; |
---|
585 | } |
---|
586 | if (!strcmp(p, "opportunistic")) { |
---|
587 | return OTRL_POLICY_OPPORTUNISTIC; |
---|
588 | } |
---|
589 | if (!strcmp(p, "manual")) { |
---|
590 | return OTRL_POLICY_MANUAL; |
---|
591 | } |
---|
592 | if (!strcmp(p, "always")) { |
---|
593 | return OTRL_POLICY_ALWAYS; |
---|
594 | } |
---|
595 | |
---|
596 | return OTRL_POLICY_OPPORTUNISTIC; |
---|
597 | } |
---|
598 | |
---|
599 | void op_create_privkey(void *opdata, const char *accountname, |
---|
600 | const char *protocol) |
---|
601 | { |
---|
602 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
603 | irc_t *irc = ic->bee->ui_data; |
---|
604 | |
---|
605 | /* will fail silently if keygen already in progress */ |
---|
606 | otr_keygen(irc, accountname, protocol); |
---|
607 | } |
---|
608 | |
---|
609 | int op_is_logged_in(void *opdata, const char *accountname, |
---|
610 | const char *protocol, const char *recipient) |
---|
611 | { |
---|
612 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
613 | bee_user_t *bu; |
---|
614 | |
---|
615 | /* lookup the irc_user_t for the given recipient */ |
---|
616 | bu = bee_user_by_handle(ic->bee, ic, recipient); |
---|
617 | if (bu) { |
---|
618 | if (bu->flags & BEE_USER_ONLINE) { |
---|
619 | return 1; |
---|
620 | } else { |
---|
621 | return 0; |
---|
622 | } |
---|
623 | } else { |
---|
624 | return -1; |
---|
625 | } |
---|
626 | } |
---|
627 | |
---|
628 | void op_inject_message(void *opdata, const char *accountname, |
---|
629 | const char *protocol, const char *recipient, const char *message) |
---|
630 | { |
---|
631 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
632 | irc_t *irc = ic->bee->ui_data; |
---|
633 | |
---|
634 | if (strcmp(accountname, recipient) == 0) { |
---|
635 | /* huh? injecting messages to myself? */ |
---|
636 | irc_rootmsg(irc, "note to self: %s", message); |
---|
637 | } else { |
---|
638 | /* need to drop some consts here :-( */ |
---|
639 | /* TODO: get flags into op_inject_message?! */ |
---|
640 | ic->acc->prpl->buddy_msg(ic, (char *) recipient, (char *) message, 0); |
---|
641 | /* ignoring return value :-/ */ |
---|
642 | } |
---|
643 | } |
---|
644 | |
---|
645 | void op_new_fingerprint(void *opdata, OtrlUserState us, |
---|
646 | const char *accountname, const char *protocol, |
---|
647 | const char *username, unsigned char fingerprint[20]) |
---|
648 | { |
---|
649 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
650 | irc_t *irc = ic->bee->ui_data; |
---|
651 | irc_user_t *u = peeruser(irc, username, protocol); |
---|
652 | char hunam[45]; /* anybody looking? ;-) */ |
---|
653 | |
---|
654 | otrl_privkey_hash_to_human(hunam, fingerprint); |
---|
655 | if (u) { |
---|
656 | irc_usernotice(u, "new fingerprint: %s", hunam); |
---|
657 | } else { |
---|
658 | /* this case shouldn't normally happen */ |
---|
659 | irc_rootmsg(irc, "new fingerprint for %s/%s: %s", |
---|
660 | username, protocol, hunam); |
---|
661 | } |
---|
662 | } |
---|
663 | |
---|
664 | void op_write_fingerprints(void *opdata) |
---|
665 | { |
---|
666 | struct im_connection *ic = (struct im_connection *) opdata; |
---|
667 | irc_t *irc = ic->bee->ui_data; |
---|
668 | |
---|
669 | otr_save(irc); |
---|
670 | } |
---|
671 | |
---|
672 | void op_gone_secure(void *opdata, ConnContext *context) |
---|
673 | { |
---|
674 | struct im_connection *ic = |
---|
675 | check_imc(opdata, context->accountname, context->protocol); |
---|
676 | irc_user_t *u; |
---|
677 | irc_t *irc = ic->bee->ui_data; |
---|
678 | |
---|
679 | u = peeruser(irc, context->username, context->protocol); |
---|
680 | if (!u) { |
---|
681 | log_message(LOGLVL_ERROR, |
---|
682 | "BUG: otr.c: op_gone_secure: irc_user_t for %s/%s/%s not found!", |
---|
683 | context->username, context->protocol, context->accountname); |
---|
684 | return; |
---|
685 | } |
---|
686 | |
---|
687 | otr_update_uflags(context, u); |
---|
688 | if (!otr_update_modeflags(irc, u)) { |
---|
689 | char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!"; |
---|
690 | irc_usernotice(u, "conversation is now off the record (%s)", trust); |
---|
691 | } |
---|
692 | } |
---|
693 | |
---|
694 | void op_gone_insecure(void *opdata, ConnContext *context) |
---|
695 | { |
---|
696 | struct im_connection *ic = |
---|
697 | check_imc(opdata, context->accountname, context->protocol); |
---|
698 | irc_t *irc = ic->bee->ui_data; |
---|
699 | irc_user_t *u; |
---|
700 | |
---|
701 | u = peeruser(irc, context->username, context->protocol); |
---|
702 | if (!u) { |
---|
703 | log_message(LOGLVL_ERROR, |
---|
704 | "BUG: otr.c: op_gone_insecure: irc_user_t for %s/%s/%s not found!", |
---|
705 | context->username, context->protocol, context->accountname); |
---|
706 | return; |
---|
707 | } |
---|
708 | otr_update_uflags(context, u); |
---|
709 | if (!otr_update_modeflags(irc, u)) { |
---|
710 | irc_usernotice(u, "conversation is now in cleartext"); |
---|
711 | } |
---|
712 | } |
---|
713 | |
---|
714 | void op_still_secure(void *opdata, ConnContext *context, int is_reply) |
---|
715 | { |
---|
716 | struct im_connection *ic = |
---|
717 | check_imc(opdata, context->accountname, context->protocol); |
---|
718 | irc_t *irc = ic->bee->ui_data; |
---|
719 | irc_user_t *u; |
---|
720 | |
---|
721 | u = peeruser(irc, context->username, context->protocol); |
---|
722 | if (!u) { |
---|
723 | log_message(LOGLVL_ERROR, |
---|
724 | "BUG: otr.c: op_still_secure: irc_user_t for %s/%s/%s not found!", |
---|
725 | context->username, context->protocol, context->accountname); |
---|
726 | return; |
---|
727 | } |
---|
728 | |
---|
729 | otr_update_uflags(context, u); |
---|
730 | if (!otr_update_modeflags(irc, u)) { |
---|
731 | char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!"; |
---|
732 | irc_usernotice(u, "otr connection has been refreshed (%s)", trust); |
---|
733 | } |
---|
734 | } |
---|
735 | |
---|
736 | int op_max_message_size(void *opdata, ConnContext *context) |
---|
737 | { |
---|
738 | struct im_connection *ic = |
---|
739 | check_imc(opdata, context->accountname, context->protocol); |
---|
740 | |
---|
741 | return ic->acc->prpl->mms; |
---|
742 | } |
---|
743 | |
---|
744 | const char *op_account_name(void *opdata, const char *account, const char *protocol) |
---|
745 | { |
---|
746 | struct im_connection *ic = (struct im_connection *) opdata; |
---|
747 | irc_t *irc = ic->bee->ui_data; |
---|
748 | |
---|
749 | return peernick(irc, account, protocol); |
---|
750 | } |
---|
751 | |
---|
752 | void op_create_instag(void *opdata, const char *account, const char *protocol) |
---|
753 | { |
---|
754 | struct im_connection *ic = |
---|
755 | check_imc(opdata, account, protocol); |
---|
756 | irc_t *irc = ic->bee->ui_data; |
---|
757 | gcry_error_t e; |
---|
758 | char s[512]; |
---|
759 | |
---|
760 | g_snprintf(s, 511, "%s%s.otr_instags", global.conf->configdir, |
---|
761 | irc->user->nick); |
---|
762 | e = otrl_instag_generate(irc->otr->us, s, account, protocol); |
---|
763 | if (e) { |
---|
764 | irc_rootmsg(irc, "otr: %s/%s: otrl_instag_generate failed: %s", |
---|
765 | account, protocol, gcry_strerror(e)); |
---|
766 | } |
---|
767 | } |
---|
768 | |
---|
769 | static char *otr_filter_colors(char *msg) |
---|
770 | { |
---|
771 | return str_reject_chars(msg, "\x02\x03", '?'); |
---|
772 | } |
---|
773 | |
---|
774 | /* returns newly allocated string */ |
---|
775 | static char *otr_color_encrypted(char *msg, char *color, gboolean is_query) { |
---|
776 | char **lines; |
---|
777 | GString *out; |
---|
778 | int i; |
---|
779 | |
---|
780 | lines = g_strsplit(msg, "\n", -1); |
---|
781 | |
---|
782 | /* up to 4 extra chars per line (e.g., '\x03' + ("03"|"05") + ' ') */ |
---|
783 | out = g_string_sized_new(strlen(msg) + g_strv_length(lines) * 4); |
---|
784 | |
---|
785 | for (i = 0; lines[i]; i++) { |
---|
786 | char *line = lines[i]; |
---|
787 | |
---|
788 | if (i != 0) { |
---|
789 | g_string_append_c(out, '\n'); |
---|
790 | |
---|
791 | } else if (is_query && g_strncasecmp(line, "/me ", 4) == 0) { |
---|
792 | /* in a query window, keep "/me " uncolored at the beginning */ |
---|
793 | line += 4; |
---|
794 | g_string_append(out, "/me "); |
---|
795 | } |
---|
796 | |
---|
797 | g_string_append_c(out, '\x03'); |
---|
798 | g_string_append(out, color); |
---|
799 | |
---|
800 | /* comma in first place could mess with the color code */ |
---|
801 | if (line[0] == ',') { |
---|
802 | /* insert a space between color spec and message */ |
---|
803 | g_string_append_c(out, ' '); |
---|
804 | } |
---|
805 | |
---|
806 | g_string_append(out, otr_filter_colors(line)); |
---|
807 | } |
---|
808 | |
---|
809 | g_strfreev(lines); |
---|
810 | |
---|
811 | return g_string_free(out, FALSE); |
---|
812 | } |
---|
813 | |
---|
814 | void op_convert_msg(void *opdata, ConnContext *ctx, OtrlConvertType typ, |
---|
815 | char **dst, const char *src) |
---|
816 | { |
---|
817 | struct im_connection *ic = |
---|
818 | check_imc(opdata, ctx->accountname, ctx->protocol); |
---|
819 | irc_t *irc = ic->bee->ui_data; |
---|
820 | irc_user_t *iu = peeruser(irc, ctx->username, ctx->protocol); |
---|
821 | |
---|
822 | if (typ == OTRL_CONVERT_RECEIVING) { |
---|
823 | char *msg = g_strdup(src); |
---|
824 | |
---|
825 | /* HTML decoding */ |
---|
826 | if (set_getbool(&ic->bee->set, "otr_does_html") && |
---|
827 | !(ic->flags & OPT_DOES_HTML) && |
---|
828 | set_getbool(&ic->bee->set, "strip_html")) { |
---|
829 | strip_html(msg); |
---|
830 | |
---|
831 | /* msg is borrowed by *dst (unless the next if decides to color it) */ |
---|
832 | *dst = msg; |
---|
833 | } |
---|
834 | |
---|
835 | /* coloring */ |
---|
836 | if (set_getbool(&ic->bee->set, "otr_color_encrypted")) { |
---|
837 | const char *trust = ctx->active_fingerprint->trust; |
---|
838 | char *color = (trust && *trust) ? OTR_COLOR_TRUSTED : OTR_COLOR_UNTRUSTED; |
---|
839 | gboolean is_query = (irc_user_msgdest(iu) == irc->user->nick); |
---|
840 | |
---|
841 | /* the return value of otr_color_encrypted() is borrowed by *dst */ |
---|
842 | *dst = otr_color_encrypted(msg, color, is_query); |
---|
843 | |
---|
844 | /* this branch doesn't need msg */ |
---|
845 | g_free(msg); |
---|
846 | } |
---|
847 | } else { |
---|
848 | /* HTML encoding */ |
---|
849 | /* consider OTR plaintext to be HTML if otr_does_html is set */ |
---|
850 | if (ctx && ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED && |
---|
851 | set_getbool(&ic->bee->set, "otr_does_html") && |
---|
852 | (g_strncasecmp(src, "<html>", 6) != 0)) { |
---|
853 | *dst = escape_html(src); |
---|
854 | } |
---|
855 | } |
---|
856 | } |
---|
857 | |
---|
858 | void op_convert_free(void *opdata, ConnContext *ctx, char *msg) |
---|
859 | { |
---|
860 | g_free(msg); |
---|
861 | } |
---|
862 | |
---|
863 | /* Socialist Millionaires' Protocol */ |
---|
864 | void op_handle_smp_event(void *opdata, OtrlSMPEvent ev, ConnContext *ctx, |
---|
865 | unsigned short percent, char *question) |
---|
866 | { |
---|
867 | struct im_connection *ic = |
---|
868 | check_imc(opdata, ctx->accountname, ctx->protocol); |
---|
869 | irc_t *irc = ic->bee->ui_data; |
---|
870 | OtrlUserState us = irc->otr->us; |
---|
871 | irc_user_t *u = peeruser(irc, ctx->username, ctx->protocol); |
---|
872 | |
---|
873 | if (!u) { |
---|
874 | return; |
---|
875 | } |
---|
876 | |
---|
877 | switch (ev) { |
---|
878 | case OTRL_SMPEVENT_ASK_FOR_SECRET: |
---|
879 | irc_rootmsg(irc, "smp: initiated by %s" |
---|
880 | " - respond with \x02otr smp %s \"<secret>\"\x02", |
---|
881 | u->nick, u->nick); |
---|
882 | break; |
---|
883 | case OTRL_SMPEVENT_ASK_FOR_ANSWER: |
---|
884 | irc_rootmsg(irc, "smp: initiated by %s with question: \x02\"%s\"\x02", u->nick, |
---|
885 | question); |
---|
886 | irc_rootmsg(irc, "smp: respond with \x02otr smp %s \"<answer>\"\x02", |
---|
887 | u->nick); |
---|
888 | break; |
---|
889 | case OTRL_SMPEVENT_CHEATED: |
---|
890 | irc_rootmsg(irc, "smp %s: opponent violated protocol, aborting", |
---|
891 | u->nick); |
---|
892 | otrl_message_abort_smp(us, &otr_ops, u->bu->ic, ctx); |
---|
893 | otrl_sm_state_free(ctx->smstate); |
---|
894 | break; |
---|
895 | case OTRL_SMPEVENT_NONE: |
---|
896 | break; |
---|
897 | case OTRL_SMPEVENT_IN_PROGRESS: |
---|
898 | break; |
---|
899 | case OTRL_SMPEVENT_SUCCESS: |
---|
900 | if (ctx->smstate->received_question) { |
---|
901 | irc_rootmsg(irc, "smp %s: correct answer, you are trusted", |
---|
902 | u->nick); |
---|
903 | } else { |
---|
904 | irc_rootmsg(irc, "smp %s: secrets proved equal, fingerprint trusted", |
---|
905 | u->nick); |
---|
906 | } |
---|
907 | otrl_sm_state_free(ctx->smstate); |
---|
908 | break; |
---|
909 | case OTRL_SMPEVENT_FAILURE: |
---|
910 | if (ctx->smstate->received_question) { |
---|
911 | irc_rootmsg(irc, "smp %s: wrong answer, you are not trusted", |
---|
912 | u->nick); |
---|
913 | } else { |
---|
914 | irc_rootmsg(irc, "smp %s: secrets did not match, fingerprint not trusted", |
---|
915 | u->nick); |
---|
916 | } |
---|
917 | otrl_sm_state_free(ctx->smstate); |
---|
918 | break; |
---|
919 | case OTRL_SMPEVENT_ABORT: |
---|
920 | irc_rootmsg(irc, "smp: received abort from %s", u->nick); |
---|
921 | otrl_sm_state_free(ctx->smstate); |
---|
922 | break; |
---|
923 | case OTRL_SMPEVENT_ERROR: |
---|
924 | irc_rootmsg(irc, "smp %s: protocol error, aborting", |
---|
925 | u->nick); |
---|
926 | otrl_message_abort_smp(us, &otr_ops, u->bu->ic, ctx); |
---|
927 | otrl_sm_state_free(ctx->smstate); |
---|
928 | break; |
---|
929 | } |
---|
930 | } |
---|
931 | |
---|
932 | void op_handle_msg_event(void *opdata, OtrlMessageEvent ev, ConnContext *ctx, |
---|
933 | const char *message, gcry_error_t err) |
---|
934 | { |
---|
935 | switch (ev) { |
---|
936 | case OTRL_MSGEVENT_ENCRYPTION_REQUIRED: |
---|
937 | display_otr_message(opdata, ctx, |
---|
938 | "policy requires encryption - message not sent"); |
---|
939 | break; |
---|
940 | case OTRL_MSGEVENT_ENCRYPTION_ERROR: |
---|
941 | display_otr_message(opdata, ctx, |
---|
942 | "error during encryption - message not sent"); |
---|
943 | break; |
---|
944 | case OTRL_MSGEVENT_CONNECTION_ENDED: |
---|
945 | display_otr_message(opdata, ctx, |
---|
946 | "other end has disconnected OTR - " |
---|
947 | "close connection or reconnect!"); |
---|
948 | break; |
---|
949 | case OTRL_MSGEVENT_SETUP_ERROR: |
---|
950 | display_otr_message(opdata, ctx, |
---|
951 | "OTR connection failed: %s", gcry_strerror(err)); |
---|
952 | break; |
---|
953 | case OTRL_MSGEVENT_MSG_REFLECTED: |
---|
954 | display_otr_message(opdata, ctx, |
---|
955 | "received our own OTR message (!?)"); |
---|
956 | break; |
---|
957 | case OTRL_MSGEVENT_MSG_RESENT: |
---|
958 | display_otr_message(opdata, ctx, |
---|
959 | "the previous message was resent"); |
---|
960 | break; |
---|
961 | case OTRL_MSGEVENT_RCVDMSG_NOT_IN_PRIVATE: |
---|
962 | display_otr_message(opdata, ctx, |
---|
963 | "unexpected encrypted message received"); |
---|
964 | break; |
---|
965 | case OTRL_MSGEVENT_RCVDMSG_UNREADABLE: |
---|
966 | display_otr_message(opdata, ctx, |
---|
967 | "unreadable encrypted message received"); |
---|
968 | break; |
---|
969 | case OTRL_MSGEVENT_RCVDMSG_MALFORMED: |
---|
970 | display_otr_message(opdata, ctx, |
---|
971 | "malformed OTR message received"); |
---|
972 | break; |
---|
973 | case OTRL_MSGEVENT_LOG_HEARTBEAT_RCVD: |
---|
974 | if (global.conf->verbose) { |
---|
975 | log_otr_message(opdata, "%s/%s: heartbeat received", |
---|
976 | ctx->accountname, ctx->protocol); |
---|
977 | } |
---|
978 | break; |
---|
979 | case OTRL_MSGEVENT_LOG_HEARTBEAT_SENT: |
---|
980 | if (global.conf->verbose) { |
---|
981 | log_otr_message(opdata, "%s/%s: heartbeat sent", |
---|
982 | ctx->accountname, ctx->protocol); |
---|
983 | } |
---|
984 | break; |
---|
985 | case OTRL_MSGEVENT_RCVDMSG_GENERAL_ERR: |
---|
986 | display_otr_message(opdata, ctx, |
---|
987 | "OTR error message received: %s", message); |
---|
988 | break; |
---|
989 | case OTRL_MSGEVENT_RCVDMSG_UNENCRYPTED: |
---|
990 | display_otr_message(opdata, ctx, |
---|
991 | "unencrypted message received: %s", message); |
---|
992 | break; |
---|
993 | case OTRL_MSGEVENT_RCVDMSG_UNRECOGNIZED: |
---|
994 | display_otr_message(opdata, ctx, |
---|
995 | "unrecognized OTR message received"); |
---|
996 | break; |
---|
997 | case OTRL_MSGEVENT_RCVDMSG_FOR_OTHER_INSTANCE: |
---|
998 | display_otr_message(opdata, ctx, |
---|
999 | "OTR message for a different instance received"); |
---|
1000 | break; |
---|
1001 | default: |
---|
1002 | /* shouldn't happen */ |
---|
1003 | break; |
---|
1004 | } |
---|
1005 | } |
---|
1006 | |
---|
1007 | const char *op_otr_error_message(void *opdata, ConnContext *ctx, |
---|
1008 | OtrlErrorCode err_code) |
---|
1009 | { |
---|
1010 | switch (err_code) { |
---|
1011 | case OTRL_ERRCODE_ENCRYPTION_ERROR: |
---|
1012 | return "i failed to encrypt a message"; |
---|
1013 | case OTRL_ERRCODE_MSG_NOT_IN_PRIVATE: |
---|
1014 | return "you sent an encrypted message i didn't expect"; |
---|
1015 | case OTRL_ERRCODE_MSG_UNREADABLE: |
---|
1016 | return "could not read encrypted message"; |
---|
1017 | case OTRL_ERRCODE_MSG_MALFORMED: |
---|
1018 | return "you sent a malformed OTR message"; |
---|
1019 | default: |
---|
1020 | return "i suffered an unexpected OTR error"; |
---|
1021 | } |
---|
1022 | } |
---|
1023 | |
---|
1024 | |
---|
1025 | |
---|
1026 | /*** OTR sub-command handlers ***/ |
---|
1027 | |
---|
1028 | void cmd_otr_reconnect(irc_t *irc, char **args) |
---|
1029 | { |
---|
1030 | cmd_otr_disconnect(irc, args); |
---|
1031 | cmd_otr_connect(irc, args); |
---|
1032 | } |
---|
1033 | |
---|
1034 | void cmd_otr_disconnect(irc_t *irc, char **args) |
---|
1035 | { |
---|
1036 | irc_user_t *u; |
---|
1037 | |
---|
1038 | if (!strcmp("*", args[1])) { |
---|
1039 | otr_disconnect_all(irc); |
---|
1040 | irc_rootmsg(irc, "all conversations are now in cleartext"); |
---|
1041 | } else { |
---|
1042 | u = irc_user_by_name(irc, args[1]); |
---|
1043 | if (otr_disconnect_user(irc, u)) { |
---|
1044 | irc_usernotice(u, "conversation is now in cleartext"); |
---|
1045 | } else { |
---|
1046 | irc_rootmsg(irc, "%s: unknown user", args[1]); |
---|
1047 | } |
---|
1048 | } |
---|
1049 | } |
---|
1050 | |
---|
1051 | void cmd_otr_connect(irc_t *irc, char **args) |
---|
1052 | { |
---|
1053 | irc_user_t *u; |
---|
1054 | char *msg, *query = "?OTR?"; |
---|
1055 | |
---|
1056 | u = irc_user_by_name(irc, args[1]); |
---|
1057 | if (!u || !u->bu || !u->bu->ic) { |
---|
1058 | irc_rootmsg(irc, "%s: unknown user", args[1]); |
---|
1059 | return; |
---|
1060 | } |
---|
1061 | if (!(u->bu->flags & BEE_USER_ONLINE)) { |
---|
1062 | irc_rootmsg(irc, "%s is offline", args[1]); |
---|
1063 | return; |
---|
1064 | } |
---|
1065 | |
---|
1066 | /* passing this through the filter so it goes through libotr which |
---|
1067 | * will replace the simple query string with a proper one */ |
---|
1068 | msg = otr_filter_msg_out(u, query, 0); |
---|
1069 | |
---|
1070 | /* send the message */ |
---|
1071 | if (msg) { |
---|
1072 | u->bu->ic->acc->prpl->buddy_msg(u->bu->ic, u->bu->handle, msg, 0); /* XXX flags? */ |
---|
1073 | /* XXX error message? */ |
---|
1074 | |
---|
1075 | if (msg != query) { |
---|
1076 | g_free(msg); |
---|
1077 | } |
---|
1078 | } |
---|
1079 | } |
---|
1080 | |
---|
1081 | void cmd_otr_smp(irc_t *irc, char **args) |
---|
1082 | { |
---|
1083 | otr_smp_or_smpq(irc, args[1], NULL, args[2]); /* no question */ |
---|
1084 | } |
---|
1085 | |
---|
1086 | void cmd_otr_smpq(irc_t *irc, char **args) |
---|
1087 | { |
---|
1088 | otr_smp_or_smpq(irc, args[1], args[2], args[3]); |
---|
1089 | } |
---|
1090 | |
---|
1091 | void cmd_otr_trust(irc_t *irc, char **args) |
---|
1092 | { |
---|
1093 | irc_user_t *u; |
---|
1094 | ConnContext *ctx; |
---|
1095 | unsigned char raw[20]; |
---|
1096 | Fingerprint *fp; |
---|
1097 | int i, j; |
---|
1098 | |
---|
1099 | u = irc_user_by_name(irc, args[1]); |
---|
1100 | if (!u || !u->bu || !u->bu->ic) { |
---|
1101 | irc_rootmsg(irc, "%s: unknown user", args[1]); |
---|
1102 | return; |
---|
1103 | } |
---|
1104 | |
---|
1105 | ctx = otrl_context_find(irc->otr->us, u->bu->handle, |
---|
1106 | u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, |
---|
1107 | NULL); |
---|
1108 | if (!ctx) { |
---|
1109 | irc_rootmsg(irc, "%s: no otr context with user", args[1]); |
---|
1110 | return; |
---|
1111 | } |
---|
1112 | |
---|
1113 | /* convert given fingerprint to raw representation */ |
---|
1114 | for (i = 0; i < 5; i++) { |
---|
1115 | for (j = 0; j < 4; j++) { |
---|
1116 | char *p = args[2 + i] + (2 * j); |
---|
1117 | char *q = p + 1; |
---|
1118 | int x, y; |
---|
1119 | |
---|
1120 | if (!*p || !*q) { |
---|
1121 | irc_rootmsg(irc, "failed: truncated fingerprint block %d", i + 1); |
---|
1122 | return; |
---|
1123 | } |
---|
1124 | |
---|
1125 | x = hexval(*p); |
---|
1126 | y = hexval(*q); |
---|
1127 | if (x < 0) { |
---|
1128 | irc_rootmsg(irc, "failed: %d. hex digit of block %d out of range", 2 * j + 1, i + 1); |
---|
1129 | return; |
---|
1130 | } |
---|
1131 | if (y < 0) { |
---|
1132 | irc_rootmsg(irc, "failed: %d. hex digit of block %d out of range", 2 * j + 2, i + 1); |
---|
1133 | return; |
---|
1134 | } |
---|
1135 | |
---|
1136 | raw[i * 4 + j] = x * 16 + y; |
---|
1137 | } |
---|
1138 | } |
---|
1139 | fp = otrl_context_find_fingerprint(ctx, raw, 0, NULL); |
---|
1140 | if (!fp) { |
---|
1141 | irc_rootmsg(irc, "failed: no such fingerprint for %s", args[1]); |
---|
1142 | } else { |
---|
1143 | char *trust = args[7] ? args[7] : "affirmed"; |
---|
1144 | otrl_context_set_trust(fp, trust); |
---|
1145 | irc_rootmsg(irc, "fingerprint match, trust set to \"%s\"", trust); |
---|
1146 | if (u->flags & IRC_USER_OTR_ENCRYPTED) { |
---|
1147 | u->flags |= IRC_USER_OTR_TRUSTED; |
---|
1148 | } |
---|
1149 | otr_update_modeflags(irc, u); |
---|
1150 | } |
---|
1151 | } |
---|
1152 | |
---|
1153 | void cmd_otr_info(irc_t *irc, char **args) |
---|
1154 | { |
---|
1155 | if (!args[1]) { |
---|
1156 | show_general_otr_info(irc); |
---|
1157 | } else { |
---|
1158 | char *arg = g_strdup(args[1]); |
---|
1159 | char *myhandle, *handle = NULL, *protocol; |
---|
1160 | ConnContext *bestctx = NULL, *ctx; |
---|
1161 | |
---|
1162 | /* interpret arg as 'user/protocol/account' if possible */ |
---|
1163 | protocol = strchr(arg, '/'); |
---|
1164 | myhandle = NULL; |
---|
1165 | if (protocol) { |
---|
1166 | *(protocol++) = '\0'; |
---|
1167 | myhandle = strchr(protocol, '/'); |
---|
1168 | } |
---|
1169 | if (protocol && myhandle) { |
---|
1170 | *(myhandle++) = '\0'; |
---|
1171 | handle = arg; |
---|
1172 | ctx = otrl_context_find(irc->otr->us, handle, myhandle, protocol, OTRL_INSTAG_MASTER, 0, NULL, |
---|
1173 | NULL, NULL); |
---|
1174 | if (!ctx) { |
---|
1175 | irc_rootmsg(irc, "no such context"); |
---|
1176 | g_free(arg); |
---|
1177 | return; |
---|
1178 | } |
---|
1179 | } else { |
---|
1180 | irc_user_t *u = irc_user_by_name(irc, args[1]); |
---|
1181 | if (!u || !u->bu || !u->bu->ic) { |
---|
1182 | irc_rootmsg(irc, "%s: unknown user", args[1]); |
---|
1183 | g_free(arg); |
---|
1184 | return; |
---|
1185 | } |
---|
1186 | ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user, |
---|
1187 | u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); |
---|
1188 | if (!ctx) { |
---|
1189 | irc_rootmsg(irc, "no otr context with %s", args[1]); |
---|
1190 | g_free(arg); |
---|
1191 | return; |
---|
1192 | } |
---|
1193 | /* This does no harm if it returns NULL */ |
---|
1194 | bestctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user, |
---|
1195 | u->bu->ic->acc->prpl->name, OTRL_INSTAG_BEST, 0, NULL, NULL, NULL); |
---|
1196 | } |
---|
1197 | |
---|
1198 | /* show how we resolved the (nick) argument, if we did */ |
---|
1199 | if (handle != arg) { |
---|
1200 | irc_rootmsg(irc, "%s:", args[1]); |
---|
1201 | irc_rootmsg(irc, " they are: %s/%s", ctx->username, ctx->protocol); |
---|
1202 | irc_rootmsg(irc, " we are: %s/%s", ctx->accountname, ctx->protocol); |
---|
1203 | } |
---|
1204 | show_otr_context_info(irc, ctx, bestctx); |
---|
1205 | g_free(arg); |
---|
1206 | } |
---|
1207 | } |
---|
1208 | |
---|
1209 | void cmd_otr_keygen(irc_t *irc, char **args) |
---|
1210 | { |
---|
1211 | account_t *a; |
---|
1212 | |
---|
1213 | if ((a = account_get(irc->b, args[1])) == NULL) { |
---|
1214 | irc_rootmsg(irc, "Could not find account `%s'.", args[1]); |
---|
1215 | return; |
---|
1216 | } |
---|
1217 | |
---|
1218 | if (keygen_in_progress(irc, a->user, a->prpl->name)) { |
---|
1219 | irc_rootmsg(irc, "keygen for account `%s' already in progress", a->tag); |
---|
1220 | return; |
---|
1221 | } |
---|
1222 | |
---|
1223 | if (otrl_privkey_find(irc->otr->us, a->user, a->prpl->name)) { |
---|
1224 | char *s = g_strdup_printf("account `%s' already has a key, replace it?", a->tag); |
---|
1225 | query_add(irc, NULL, s, yes_keygen, NULL, NULL, a); |
---|
1226 | g_free(s); |
---|
1227 | } else { |
---|
1228 | otr_keygen(irc, a->user, a->prpl->name); |
---|
1229 | } |
---|
1230 | } |
---|
1231 | |
---|
1232 | void yes_forget_fingerprint(void *data) |
---|
1233 | { |
---|
1234 | pair_t *p = (pair_t *) data; |
---|
1235 | irc_t *irc = (irc_t *) p->fst; |
---|
1236 | Fingerprint *fp = (Fingerprint *) p->snd; |
---|
1237 | |
---|
1238 | g_free(p); |
---|
1239 | |
---|
1240 | if (fp == fp->context->active_fingerprint) { |
---|
1241 | irc_rootmsg(irc, "that fingerprint is active, terminate otr connection first"); |
---|
1242 | return; |
---|
1243 | } |
---|
1244 | |
---|
1245 | otrl_context_forget_fingerprint(fp, 0); |
---|
1246 | } |
---|
1247 | |
---|
1248 | void yes_forget_context(void *data) |
---|
1249 | { |
---|
1250 | pair_t *p = (pair_t *) data; |
---|
1251 | irc_t *irc = (irc_t *) p->fst; |
---|
1252 | ConnContext *ctx = (ConnContext *) p->snd; |
---|
1253 | |
---|
1254 | g_free(p); |
---|
1255 | |
---|
1256 | // XXX forget all contexts |
---|
1257 | |
---|
1258 | if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { |
---|
1259 | irc_rootmsg(irc, "active otr connection with %s, terminate it first", |
---|
1260 | peernick(irc, ctx->username, ctx->protocol)); |
---|
1261 | return; |
---|
1262 | } |
---|
1263 | |
---|
1264 | if (ctx->msgstate == OTRL_MSGSTATE_FINISHED) { |
---|
1265 | otrl_context_force_plaintext(ctx); |
---|
1266 | } |
---|
1267 | otrl_context_forget(ctx); |
---|
1268 | } |
---|
1269 | |
---|
1270 | void yes_forget_key(void *data) |
---|
1271 | { |
---|
1272 | OtrlPrivKey *key = (OtrlPrivKey *) data; |
---|
1273 | |
---|
1274 | otrl_privkey_forget(key); |
---|
1275 | /* Hm, libotr doesn't seem to offer a function for explicitly /writing/ |
---|
1276 | keyfiles. So the key will be back on the next load... */ |
---|
1277 | /* TODO: Actually erase forgotten keys from storage? */ |
---|
1278 | } |
---|
1279 | |
---|
1280 | void cmd_otr_forget(irc_t *irc, char **args) |
---|
1281 | { |
---|
1282 | if (!strcmp(args[1], "fingerprint")) { |
---|
1283 | irc_user_t *u; |
---|
1284 | ConnContext *ctx; |
---|
1285 | Fingerprint *fp; |
---|
1286 | char human[54]; |
---|
1287 | char *s; |
---|
1288 | pair_t *p; |
---|
1289 | |
---|
1290 | if (!args[3]) { |
---|
1291 | irc_rootmsg(irc, "otr %s %s: not enough arguments (2 req.)", args[0], args[1]); |
---|
1292 | return; |
---|
1293 | } |
---|
1294 | |
---|
1295 | /* TODO: allow context specs ("user/proto/account") in 'otr forget fingerprint'? */ |
---|
1296 | u = irc_user_by_name(irc, args[2]); |
---|
1297 | if (!u || !u->bu || !u->bu->ic) { |
---|
1298 | irc_rootmsg(irc, "%s: unknown user", args[2]); |
---|
1299 | return; |
---|
1300 | } |
---|
1301 | |
---|
1302 | ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user, |
---|
1303 | u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); |
---|
1304 | if (!ctx) { |
---|
1305 | irc_rootmsg(irc, "no otr context with %s", args[2]); |
---|
1306 | return; |
---|
1307 | } |
---|
1308 | |
---|
1309 | fp = match_fingerprint(irc, ctx, ((const char **) args) + 3); |
---|
1310 | if (!fp) { |
---|
1311 | /* match_fingerprint does error messages */ |
---|
1312 | return; |
---|
1313 | } |
---|
1314 | |
---|
1315 | if (fp == ctx->active_fingerprint) { |
---|
1316 | irc_rootmsg(irc, "that fingerprint is active, terminate otr connection first"); |
---|
1317 | return; |
---|
1318 | } |
---|
1319 | |
---|
1320 | otrl_privkey_hash_to_human(human, fp->fingerprint); |
---|
1321 | s = g_strdup_printf("about to forget fingerprint %s, are you sure?", human); |
---|
1322 | p = g_malloc(sizeof(pair_t)); |
---|
1323 | if (!p) { |
---|
1324 | return; |
---|
1325 | } |
---|
1326 | p->fst = irc; |
---|
1327 | p->snd = fp; |
---|
1328 | query_add(irc, NULL, s, yes_forget_fingerprint, NULL, NULL, p); |
---|
1329 | g_free(s); |
---|
1330 | } else if (!strcmp(args[1], "context")) { |
---|
1331 | irc_user_t *u; |
---|
1332 | ConnContext *ctx; |
---|
1333 | char *s; |
---|
1334 | pair_t *p; |
---|
1335 | |
---|
1336 | /* TODO: allow context specs ("user/proto/account") in 'otr forget contex'? */ |
---|
1337 | u = irc_user_by_name(irc, args[2]); |
---|
1338 | if (!u || !u->bu || !u->bu->ic) { |
---|
1339 | irc_rootmsg(irc, "%s: unknown user", args[2]); |
---|
1340 | return; |
---|
1341 | } |
---|
1342 | |
---|
1343 | ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user, |
---|
1344 | u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); |
---|
1345 | if (!ctx) { |
---|
1346 | irc_rootmsg(irc, "no otr context with %s", args[2]); |
---|
1347 | return; |
---|
1348 | } |
---|
1349 | |
---|
1350 | if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { |
---|
1351 | irc_rootmsg(irc, "active otr connection with %s, terminate it first", args[2]); |
---|
1352 | return; |
---|
1353 | } |
---|
1354 | |
---|
1355 | s = g_strdup_printf("about to forget otr data about %s, are you sure?", args[2]); |
---|
1356 | p = g_malloc(sizeof(pair_t)); |
---|
1357 | if (!p) { |
---|
1358 | return; |
---|
1359 | } |
---|
1360 | p->fst = irc; |
---|
1361 | p->snd = ctx; |
---|
1362 | query_add(irc, NULL, s, yes_forget_context, NULL, NULL, p); |
---|
1363 | g_free(s); |
---|
1364 | } else if (!strcmp(args[1], "key")) { |
---|
1365 | OtrlPrivKey *key; |
---|
1366 | char *s; |
---|
1367 | |
---|
1368 | key = match_privkey(irc, ((const char **) args) + 2); |
---|
1369 | if (!key) { |
---|
1370 | /* match_privkey does error messages */ |
---|
1371 | return; |
---|
1372 | } |
---|
1373 | |
---|
1374 | s = g_strdup_printf("about to forget the private key for %s/%s, are you sure?", |
---|
1375 | key->accountname, key->protocol); |
---|
1376 | query_add(irc, NULL, s, yes_forget_key, NULL, NULL, key); |
---|
1377 | g_free(s); |
---|
1378 | } else { |
---|
1379 | irc_rootmsg(irc, "otr %s: unknown subcommand \"%s\", see \x02help otr forget\x02", |
---|
1380 | args[0], args[1]); |
---|
1381 | } |
---|
1382 | } |
---|
1383 | |
---|
1384 | |
---|
1385 | /*** local helpers / subroutines: ***/ |
---|
1386 | |
---|
1387 | void log_otr_message(void *opdata, const char *fmt, ...) |
---|
1388 | { |
---|
1389 | va_list va; |
---|
1390 | |
---|
1391 | va_start(va, fmt); |
---|
1392 | char *msg = g_strdup_vprintf(fmt, va); |
---|
1393 | va_end(va); |
---|
1394 | |
---|
1395 | log_message(LOGLVL_INFO, "otr: %s", msg); |
---|
1396 | |
---|
1397 | g_free(msg); |
---|
1398 | } |
---|
1399 | |
---|
1400 | void display_otr_message(void *opdata, ConnContext *ctx, const char *fmt, ...) |
---|
1401 | { |
---|
1402 | char *msg_, *msg; |
---|
1403 | struct im_connection *ic = |
---|
1404 | check_imc(opdata, ctx->accountname, ctx->protocol); |
---|
1405 | irc_t *irc = ic->bee->ui_data; |
---|
1406 | irc_user_t *u = peeruser(irc, ctx->username, ctx->protocol); |
---|
1407 | va_list va; |
---|
1408 | |
---|
1409 | va_start(va, fmt); |
---|
1410 | msg_ = g_strdup_vprintf(fmt, va); |
---|
1411 | va_end(va); |
---|
1412 | |
---|
1413 | msg = word_wrap(msg_, IRC_WORD_WRAP); |
---|
1414 | |
---|
1415 | if (u) { |
---|
1416 | /* just show this as a regular message */ |
---|
1417 | irc_usermsg(u, "<<\002OTR\002>> %s", msg); |
---|
1418 | } else { |
---|
1419 | irc_rootmsg(irc, "[otr] %s", msg); |
---|
1420 | } |
---|
1421 | |
---|
1422 | g_free(msg_); |
---|
1423 | g_free(msg); |
---|
1424 | } |
---|
1425 | |
---|
1426 | /* combined handler for the 'otr smp' and 'otr smpq' commands */ |
---|
1427 | void otr_smp_or_smpq(irc_t *irc, const char *nick, const char *question, |
---|
1428 | const char *secret) |
---|
1429 | { |
---|
1430 | irc_user_t *u; |
---|
1431 | ConnContext *ctx; |
---|
1432 | otrl_instag_t instag = OTRL_INSTAG_BEST; // XXX |
---|
1433 | |
---|
1434 | u = irc_user_by_name(irc, nick); |
---|
1435 | if (!u || !u->bu || !u->bu->ic) { |
---|
1436 | irc_rootmsg(irc, "%s: unknown user", nick); |
---|
1437 | return; |
---|
1438 | } |
---|
1439 | if (!(u->bu->flags & BEE_USER_ONLINE)) { |
---|
1440 | irc_rootmsg(irc, "%s is offline", nick); |
---|
1441 | return; |
---|
1442 | } |
---|
1443 | |
---|
1444 | ctx = otrl_context_find(irc->otr->us, u->bu->handle, |
---|
1445 | u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, instag, 0, NULL, NULL, NULL); |
---|
1446 | if (!ctx || ctx->msgstate != OTRL_MSGSTATE_ENCRYPTED) { |
---|
1447 | irc_rootmsg(irc, "smp: otr inactive with %s, try \x02otr connect" |
---|
1448 | " %s\x02", nick, nick); |
---|
1449 | return; |
---|
1450 | } |
---|
1451 | |
---|
1452 | if (ctx->smstate->nextExpected != OTRL_SMP_EXPECT1) { |
---|
1453 | log_message(LOGLVL_INFO, |
---|
1454 | "SMP already in phase %d, sending abort before reinitiating", |
---|
1455 | ctx->smstate->nextExpected + 1); |
---|
1456 | otrl_message_abort_smp(irc->otr->us, &otr_ops, u->bu->ic, ctx); |
---|
1457 | otrl_sm_state_free(ctx->smstate); |
---|
1458 | } |
---|
1459 | |
---|
1460 | if (question) { |
---|
1461 | /* this was 'otr smpq', just initiate */ |
---|
1462 | irc_rootmsg(irc, "smp: initiating with %s...", u->nick); |
---|
1463 | otrl_message_initiate_smp_q(irc->otr->us, &otr_ops, u->bu->ic, ctx, |
---|
1464 | question, (unsigned char *) secret, strlen(secret)); |
---|
1465 | /* smp is now in EXPECT2 */ |
---|
1466 | } else { |
---|
1467 | /* this was 'otr smp', initiate or reply */ |
---|
1468 | /* warning: the following assumes that smstates are cleared whenever an SMP |
---|
1469 | is completed or aborted! */ |
---|
1470 | if (ctx->smstate->secret == NULL) { |
---|
1471 | irc_rootmsg(irc, "smp: initiating with %s...", u->nick); |
---|
1472 | otrl_message_initiate_smp(irc->otr->us, &otr_ops, |
---|
1473 | u->bu->ic, ctx, (unsigned char *) secret, strlen(secret)); |
---|
1474 | /* smp is now in EXPECT2 */ |
---|
1475 | } else { |
---|
1476 | /* if we're still in EXPECT1 but smstate is initialized, we must have |
---|
1477 | received the SMP1, so let's issue a response */ |
---|
1478 | irc_rootmsg(irc, "smp: responding to %s...", u->nick); |
---|
1479 | otrl_message_respond_smp(irc->otr->us, &otr_ops, |
---|
1480 | u->bu->ic, ctx, (unsigned char *) secret, strlen(secret)); |
---|
1481 | /* smp is now in EXPECT3 */ |
---|
1482 | } |
---|
1483 | } |
---|
1484 | } |
---|
1485 | |
---|
1486 | /* timeout handler that calls otrl_message_poll */ |
---|
1487 | gboolean ev_message_poll(gpointer data, gint fd, b_input_condition cond) |
---|
1488 | { |
---|
1489 | otr_t *otr = data; |
---|
1490 | |
---|
1491 | if (otr && otr->us) { |
---|
1492 | otrl_message_poll(otr->us, &otr_ops, NULL); |
---|
1493 | } |
---|
1494 | |
---|
1495 | return TRUE; /* cycle timer */ |
---|
1496 | } |
---|
1497 | |
---|
1498 | /* helper to assert that account and protocol names given to ops below always |
---|
1499 | match the im_connection passed through as opdata */ |
---|
1500 | struct im_connection *check_imc(void *opdata, const char *accountname, |
---|
1501 | const char *protocol) |
---|
1502 | { |
---|
1503 | struct im_connection *ic = (struct im_connection *) opdata; |
---|
1504 | |
---|
1505 | /* libotr 4.0.0 has a bug where it doesn't set opdata, so we catch |
---|
1506 | * that and try to find the desired connection in the global list. */ |
---|
1507 | if (!ic) { |
---|
1508 | GSList *l; |
---|
1509 | for (l = get_connections(); l; l = l->next) { |
---|
1510 | ic = l->data; |
---|
1511 | if (strcmp(accountname, ic->acc->user) == 0 && |
---|
1512 | strcmp(protocol, ic->acc->prpl->name) == 0) { |
---|
1513 | break; |
---|
1514 | } |
---|
1515 | } |
---|
1516 | g_return_val_if_fail(l, NULL); |
---|
1517 | if (!l) { |
---|
1518 | return NULL; |
---|
1519 | } |
---|
1520 | } |
---|
1521 | |
---|
1522 | if (strcmp(accountname, ic->acc->user) != 0) { |
---|
1523 | log_message(LOGLVL_WARNING, |
---|
1524 | "otr: internal account name mismatch: '%s' vs '%s'", |
---|
1525 | accountname, ic->acc->user); |
---|
1526 | } |
---|
1527 | if (strcmp(protocol, ic->acc->prpl->name) != 0) { |
---|
1528 | log_message(LOGLVL_WARNING, |
---|
1529 | "otr: internal protocol name mismatch: '%s' vs '%s'", |
---|
1530 | protocol, ic->acc->prpl->name); |
---|
1531 | } |
---|
1532 | |
---|
1533 | return ic; |
---|
1534 | } |
---|
1535 | |
---|
1536 | irc_user_t *peeruser(irc_t *irc, const char *handle, const char *protocol) |
---|
1537 | { |
---|
1538 | GSList *l; |
---|
1539 | |
---|
1540 | for (l = irc->b->users; l; l = l->next) { |
---|
1541 | bee_user_t *bu = l->data; |
---|
1542 | struct prpl *prpl; |
---|
1543 | if (!bu->ui_data || !bu->ic || !bu->handle) { |
---|
1544 | continue; |
---|
1545 | } |
---|
1546 | prpl = bu->ic->acc->prpl; |
---|
1547 | if (strcmp(prpl->name, protocol) == 0 |
---|
1548 | && prpl->handle_cmp(bu->handle, handle) == 0) { |
---|
1549 | return bu->ui_data; |
---|
1550 | } |
---|
1551 | } |
---|
1552 | |
---|
1553 | return NULL; |
---|
1554 | } |
---|
1555 | |
---|
1556 | int hexval(char a) |
---|
1557 | { |
---|
1558 | int x = g_ascii_tolower(a); |
---|
1559 | |
---|
1560 | if (x >= 'a' && x <= 'f') { |
---|
1561 | x = x - 'a' + 10; |
---|
1562 | } else if (x >= '0' && x <= '9') { |
---|
1563 | x = x - '0'; |
---|
1564 | } else { |
---|
1565 | return -1; |
---|
1566 | } |
---|
1567 | |
---|
1568 | return x; |
---|
1569 | } |
---|
1570 | |
---|
1571 | const char *peernick(irc_t *irc, const char *handle, const char *protocol) |
---|
1572 | { |
---|
1573 | static char fallback[512]; |
---|
1574 | |
---|
1575 | irc_user_t *u = peeruser(irc, handle, protocol); |
---|
1576 | |
---|
1577 | if (u) { |
---|
1578 | return u->nick; |
---|
1579 | } else { |
---|
1580 | g_snprintf(fallback, 511, "%s/%s", handle, protocol); |
---|
1581 | return fallback; |
---|
1582 | } |
---|
1583 | } |
---|
1584 | |
---|
1585 | void otr_update_uflags(ConnContext *context, irc_user_t *u) |
---|
1586 | { |
---|
1587 | const char *trust; |
---|
1588 | |
---|
1589 | if (context->active_fingerprint) { |
---|
1590 | u->flags |= IRC_USER_OTR_ENCRYPTED; |
---|
1591 | |
---|
1592 | trust = context->active_fingerprint->trust; |
---|
1593 | if (trust && trust[0]) { |
---|
1594 | u->flags |= IRC_USER_OTR_TRUSTED; |
---|
1595 | } else { |
---|
1596 | u->flags &= ~IRC_USER_OTR_TRUSTED; |
---|
1597 | } |
---|
1598 | } else { |
---|
1599 | u->flags &= ~IRC_USER_OTR_ENCRYPTED; |
---|
1600 | } |
---|
1601 | } |
---|
1602 | |
---|
1603 | int otr_update_modeflags(irc_t *irc, irc_user_t *u) |
---|
1604 | { |
---|
1605 | return 0; |
---|
1606 | } |
---|
1607 | |
---|
1608 | void show_fingerprints(irc_t *irc, ConnContext *ctx) |
---|
1609 | { |
---|
1610 | char human[45]; |
---|
1611 | Fingerprint *fp; |
---|
1612 | const char *trust; |
---|
1613 | int count = 0; |
---|
1614 | |
---|
1615 | /* Is this a subcontext? If so, only list the active fingerprint */ |
---|
1616 | if (ctx->m_context != ctx) { |
---|
1617 | fp = ctx->active_fingerprint; |
---|
1618 | } else { |
---|
1619 | fp = &ctx->fingerprint_root; |
---|
1620 | } |
---|
1621 | |
---|
1622 | while (fp) { |
---|
1623 | if (!fp->fingerprint) { |
---|
1624 | fp = fp->next; |
---|
1625 | continue; |
---|
1626 | } |
---|
1627 | count++; |
---|
1628 | otrl_privkey_hash_to_human(human, fp->fingerprint); |
---|
1629 | if (!fp->trust || fp->trust[0] == '\0') { |
---|
1630 | trust = "untrusted"; |
---|
1631 | } else { |
---|
1632 | trust = fp->trust; |
---|
1633 | } |
---|
1634 | if (fp == ctx->active_fingerprint) { |
---|
1635 | irc_rootmsg(irc, " \x02%s (%s)\x02", human, trust); |
---|
1636 | } else { |
---|
1637 | irc_rootmsg(irc, " %s (%s)", human, trust); |
---|
1638 | } |
---|
1639 | |
---|
1640 | /* Break if this is a subcontext - we only print active fp */ |
---|
1641 | if (ctx->m_context != ctx) { |
---|
1642 | break; |
---|
1643 | } |
---|
1644 | fp = fp->next; |
---|
1645 | } |
---|
1646 | if (count == 0) { |
---|
1647 | irc_rootmsg(irc, " (none)"); |
---|
1648 | } |
---|
1649 | } |
---|
1650 | |
---|
1651 | Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args) |
---|
1652 | { |
---|
1653 | Fingerprint *fp, *fp2; |
---|
1654 | char human[45]; |
---|
1655 | char prefix[45], *p; |
---|
1656 | int n; |
---|
1657 | int i, j; |
---|
1658 | |
---|
1659 | /* assemble the args into a prefix in standard "human" form */ |
---|
1660 | n = 0; |
---|
1661 | p = prefix; |
---|
1662 | for (i = 0; args[i]; i++) { |
---|
1663 | for (j = 0; args[i][j]; j++) { |
---|
1664 | char c = g_ascii_toupper(args[i][j]); |
---|
1665 | |
---|
1666 | if (n >= 40) { |
---|
1667 | irc_rootmsg(irc, "too many fingerprint digits given, expected at most 40"); |
---|
1668 | return NULL; |
---|
1669 | } |
---|
1670 | |
---|
1671 | if ((c >= 'A' && c <= 'F') || (c >= '0' && c <= '9')) { |
---|
1672 | *(p++) = c; |
---|
1673 | } else { |
---|
1674 | irc_rootmsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i + 1); |
---|
1675 | return NULL; |
---|
1676 | } |
---|
1677 | |
---|
1678 | n++; |
---|
1679 | if (n % 8 == 0) { |
---|
1680 | *(p++) = ' '; |
---|
1681 | } |
---|
1682 | } |
---|
1683 | } |
---|
1684 | *p = '\0'; |
---|
1685 | |
---|
1686 | /* find first fingerprint with the given prefix */ |
---|
1687 | n = strlen(prefix); |
---|
1688 | for (fp = &ctx->fingerprint_root; fp; fp = fp->next) { |
---|
1689 | if (!fp->fingerprint) { |
---|
1690 | continue; |
---|
1691 | } |
---|
1692 | otrl_privkey_hash_to_human(human, fp->fingerprint); |
---|
1693 | if (!strncmp(prefix, human, n)) { |
---|
1694 | break; |
---|
1695 | } |
---|
1696 | } |
---|
1697 | if (!fp) { |
---|
1698 | irc_rootmsg(irc, "%s: no match", prefix); |
---|
1699 | return NULL; |
---|
1700 | } |
---|
1701 | |
---|
1702 | /* make sure the match, if any, is unique */ |
---|
1703 | for (fp2 = fp->next; fp2; fp2 = fp2->next) { |
---|
1704 | if (!fp2->fingerprint) { |
---|
1705 | continue; |
---|
1706 | } |
---|
1707 | otrl_privkey_hash_to_human(human, fp2->fingerprint); |
---|
1708 | if (!strncmp(prefix, human, n)) { |
---|
1709 | break; |
---|
1710 | } |
---|
1711 | } |
---|
1712 | if (fp2) { |
---|
1713 | irc_rootmsg(irc, "%s: multiple matches", prefix); |
---|
1714 | return NULL; |
---|
1715 | } |
---|
1716 | |
---|
1717 | return fp; |
---|
1718 | } |
---|
1719 | |
---|
1720 | OtrlPrivKey *match_privkey(irc_t *irc, const char **args) |
---|
1721 | { |
---|
1722 | OtrlPrivKey *k, *k2; |
---|
1723 | char human[45]; |
---|
1724 | char prefix[45], *p; |
---|
1725 | int n; |
---|
1726 | int i, j; |
---|
1727 | |
---|
1728 | /* assemble the args into a prefix in standard "human" form */ |
---|
1729 | n = 0; |
---|
1730 | p = prefix; |
---|
1731 | for (i = 0; args[i]; i++) { |
---|
1732 | for (j = 0; args[i][j]; j++) { |
---|
1733 | char c = g_ascii_toupper(args[i][j]); |
---|
1734 | |
---|
1735 | if (n >= 40) { |
---|
1736 | irc_rootmsg(irc, "too many fingerprint digits given, expected at most 40"); |
---|
1737 | return NULL; |
---|
1738 | } |
---|
1739 | |
---|
1740 | if ((c >= 'A' && c <= 'F') || (c >= '0' && c <= '9')) { |
---|
1741 | *(p++) = c; |
---|
1742 | } else { |
---|
1743 | irc_rootmsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i + 1); |
---|
1744 | return NULL; |
---|
1745 | } |
---|
1746 | |
---|
1747 | n++; |
---|
1748 | if (n % 8 == 0) { |
---|
1749 | *(p++) = ' '; |
---|
1750 | } |
---|
1751 | } |
---|
1752 | } |
---|
1753 | *p = '\0'; |
---|
1754 | |
---|
1755 | /* remove trailing whitespace */ |
---|
1756 | g_strchomp(prefix); |
---|
1757 | |
---|
1758 | /* find first key which matches the given prefix */ |
---|
1759 | n = strlen(prefix); |
---|
1760 | for (k = irc->otr->us->privkey_root; k; k = k->next) { |
---|
1761 | p = otrl_privkey_fingerprint(irc->otr->us, human, k->accountname, k->protocol); |
---|
1762 | if (!p) { /* gah! :-P */ |
---|
1763 | continue; |
---|
1764 | } |
---|
1765 | if (!strncmp(prefix, human, n)) { |
---|
1766 | break; |
---|
1767 | } |
---|
1768 | } |
---|
1769 | if (!k) { |
---|
1770 | irc_rootmsg(irc, "%s: no match", prefix); |
---|
1771 | return NULL; |
---|
1772 | } |
---|
1773 | |
---|
1774 | /* make sure the match, if any, is unique */ |
---|
1775 | for (k2 = k->next; k2; k2 = k2->next) { |
---|
1776 | p = otrl_privkey_fingerprint(irc->otr->us, human, k2->accountname, k2->protocol); |
---|
1777 | if (!p) { /* gah! :-P */ |
---|
1778 | continue; |
---|
1779 | } |
---|
1780 | if (!strncmp(prefix, human, n)) { |
---|
1781 | break; |
---|
1782 | } |
---|
1783 | } |
---|
1784 | if (k2) { |
---|
1785 | irc_rootmsg(irc, "%s: multiple matches", prefix); |
---|
1786 | return NULL; |
---|
1787 | } |
---|
1788 | |
---|
1789 | return k; |
---|
1790 | } |
---|
1791 | |
---|
1792 | void show_general_otr_info(irc_t *irc) |
---|
1793 | { |
---|
1794 | ConnContext *ctx; |
---|
1795 | OtrlPrivKey *key; |
---|
1796 | char human[45]; |
---|
1797 | kg_t *kg; |
---|
1798 | |
---|
1799 | /* list all privkeys (including ones being generated) */ |
---|
1800 | irc_rootmsg(irc, "\x1fprivate keys:\x1f"); |
---|
1801 | for (key = irc->otr->us->privkey_root; key; key = key->next) { |
---|
1802 | const char *hash; |
---|
1803 | |
---|
1804 | switch (key->pubkey_type) { |
---|
1805 | case OTRL_PUBKEY_TYPE_DSA: |
---|
1806 | irc_rootmsg(irc, " %s/%s - DSA", key->accountname, key->protocol); |
---|
1807 | break; |
---|
1808 | default: |
---|
1809 | irc_rootmsg(irc, " %s/%s - type %d", key->accountname, key->protocol, |
---|
1810 | key->pubkey_type); |
---|
1811 | } |
---|
1812 | |
---|
1813 | /* No, it doesn't make much sense to search for the privkey again by |
---|
1814 | account/protocol, but libotr currently doesn't provide a direct routine |
---|
1815 | for hashing a given 'OtrlPrivKey'... */ |
---|
1816 | hash = otrl_privkey_fingerprint(irc->otr->us, human, key->accountname, key->protocol); |
---|
1817 | if (hash) { /* should always succeed */ |
---|
1818 | irc_rootmsg(irc, " %s", human); |
---|
1819 | } |
---|
1820 | } |
---|
1821 | if (irc->otr->sent_accountname) { |
---|
1822 | irc_rootmsg(irc, " %s/%s - DSA", irc->otr->sent_accountname, |
---|
1823 | irc->otr->sent_protocol); |
---|
1824 | irc_rootmsg(irc, " (being generated)"); |
---|
1825 | } |
---|
1826 | for (kg = irc->otr->todo; kg; kg = kg->next) { |
---|
1827 | irc_rootmsg(irc, " %s/%s - DSA", kg->accountname, kg->protocol); |
---|
1828 | irc_rootmsg(irc, " (queued)"); |
---|
1829 | } |
---|
1830 | if (key == irc->otr->us->privkey_root && |
---|
1831 | !irc->otr->sent_accountname && |
---|
1832 | kg == irc->otr->todo) { |
---|
1833 | irc_rootmsg(irc, " (none)"); |
---|
1834 | } |
---|
1835 | |
---|
1836 | /* list all contexts */ |
---|
1837 | /* XXX remove this, or split off as its own command */ |
---|
1838 | irc_rootmsg(irc, "%s", ""); |
---|
1839 | irc_rootmsg(irc, "\x1f" "connection contexts:\x1f (bold=currently encrypted)"); |
---|
1840 | |
---|
1841 | ctx = irc->otr->us->context_root; |
---|
1842 | while (ctx) { |
---|
1843 | ConnContext *subctx; |
---|
1844 | irc_user_t *u; |
---|
1845 | char *userstring; |
---|
1846 | char encrypted = 0; |
---|
1847 | |
---|
1848 | u = peeruser(irc, ctx->username, ctx->protocol); |
---|
1849 | if (u) { |
---|
1850 | userstring = g_strdup_printf("%s/%s/%s (%s)", |
---|
1851 | ctx->username, ctx->protocol, ctx->accountname, u->nick); |
---|
1852 | } else { |
---|
1853 | userstring = g_strdup_printf("%s/%s/%s", |
---|
1854 | ctx->username, ctx->protocol, ctx->accountname); |
---|
1855 | } |
---|
1856 | |
---|
1857 | subctx = ctx; |
---|
1858 | while (subctx && subctx->m_context == ctx) { |
---|
1859 | if (subctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { |
---|
1860 | encrypted = 1; |
---|
1861 | } |
---|
1862 | subctx = subctx->next; |
---|
1863 | } |
---|
1864 | |
---|
1865 | if(encrypted) { |
---|
1866 | irc_rootmsg(irc, " \x02%s\x02", userstring); |
---|
1867 | } else { |
---|
1868 | irc_rootmsg(irc, " %s", userstring); |
---|
1869 | } |
---|
1870 | |
---|
1871 | /* Skip subcontexts/instances from output */ |
---|
1872 | ctx = subctx; |
---|
1873 | |
---|
1874 | g_free(userstring); |
---|
1875 | } |
---|
1876 | |
---|
1877 | if (ctx == irc->otr->us->context_root) { |
---|
1878 | irc_rootmsg(irc, " (none)"); |
---|
1879 | } |
---|
1880 | } |
---|
1881 | |
---|
1882 | void show_otr_context_info(irc_t *irc, ConnContext *ctx, ConnContext *bestctx) |
---|
1883 | { |
---|
1884 | ConnContext *subctx; |
---|
1885 | int instcount = 0; |
---|
1886 | |
---|
1887 | subctx = ctx; |
---|
1888 | while (subctx && subctx->m_context == ctx) { |
---|
1889 | if (subctx->m_context == subctx) { |
---|
1890 | if (subctx == bestctx) { |
---|
1891 | irc_rootmsg(irc, " \x02master context (target):\x02"); |
---|
1892 | } else { |
---|
1893 | irc_rootmsg(irc, " master context:"); |
---|
1894 | } |
---|
1895 | irc_rootmsg(irc, " known fingerprints (bold = active for v1 or v2):"); |
---|
1896 | } else { |
---|
1897 | if (subctx == bestctx) { |
---|
1898 | irc_rootmsg(irc, " \x02instance %d (target):\x02", instcount); |
---|
1899 | } else { |
---|
1900 | irc_rootmsg(irc, " instance %d:", instcount); |
---|
1901 | } |
---|
1902 | irc_rootmsg(irc, " active fingerprint:"); |
---|
1903 | instcount++; |
---|
1904 | } |
---|
1905 | |
---|
1906 | show_fingerprints(irc, subctx); |
---|
1907 | |
---|
1908 | switch (subctx->msgstate) { |
---|
1909 | case OTRL_MSGSTATE_PLAINTEXT: |
---|
1910 | irc_rootmsg(irc, " connection state: cleartext"); |
---|
1911 | break; |
---|
1912 | case OTRL_MSGSTATE_ENCRYPTED: |
---|
1913 | irc_rootmsg(irc, " connection state: encrypted (v%d)", subctx->protocol_version); |
---|
1914 | break; |
---|
1915 | case OTRL_MSGSTATE_FINISHED: |
---|
1916 | irc_rootmsg(irc, " connection state: shut down"); |
---|
1917 | break; |
---|
1918 | default: |
---|
1919 | irc_rootmsg(irc, " connection state: %d", subctx->msgstate); |
---|
1920 | } |
---|
1921 | |
---|
1922 | subctx = subctx->next; |
---|
1923 | } |
---|
1924 | } |
---|
1925 | |
---|
1926 | int keygen_in_progress(irc_t *irc, const char *handle, const char *protocol) |
---|
1927 | { |
---|
1928 | kg_t *kg; |
---|
1929 | |
---|
1930 | if (!irc->otr->sent_accountname || !irc->otr->sent_protocol) { |
---|
1931 | return 0; |
---|
1932 | } |
---|
1933 | |
---|
1934 | /* are we currently working on this key? */ |
---|
1935 | if (!strcmp(handle, irc->otr->sent_accountname) && |
---|
1936 | !strcmp(protocol, irc->otr->sent_protocol)) { |
---|
1937 | return 1; |
---|
1938 | } |
---|
1939 | |
---|
1940 | /* do we have it queued for later? */ |
---|
1941 | for (kg = irc->otr->todo; kg; kg = kg->next) { |
---|
1942 | if (!strcmp(handle, kg->accountname) && |
---|
1943 | !strcmp(protocol, kg->protocol)) { |
---|
1944 | return 1; |
---|
1945 | } |
---|
1946 | } |
---|
1947 | |
---|
1948 | return 0; |
---|
1949 | } |
---|
1950 | |
---|
1951 | void otr_keygen(irc_t *irc, const char *handle, const char *protocol) |
---|
1952 | { |
---|
1953 | /* do nothing if a key for the requested account is already being generated */ |
---|
1954 | if (keygen_in_progress(irc, handle, protocol)) { |
---|
1955 | return; |
---|
1956 | } |
---|
1957 | |
---|
1958 | /* see if we already have a keygen child running. if not, start one and put a |
---|
1959 | handler on its output. */ |
---|
1960 | if (!irc->otr->keygen || waitpid(irc->otr->keygen, NULL, WNOHANG)) { |
---|
1961 | pid_t p; |
---|
1962 | int to[2], from[2]; |
---|
1963 | FILE *tof, *fromf; |
---|
1964 | |
---|
1965 | if (pipe(to) < 0 || pipe(from) < 0) { |
---|
1966 | irc_rootmsg(irc, "otr keygen: couldn't create pipe: %s", strerror(errno)); |
---|
1967 | return; |
---|
1968 | } |
---|
1969 | |
---|
1970 | tof = fdopen(to[1], "w"); |
---|
1971 | fromf = fdopen(from[0], "r"); |
---|
1972 | if (!tof || !fromf) { |
---|
1973 | irc_rootmsg(irc, "otr keygen: couldn't streamify pipe: %s", strerror(errno)); |
---|
1974 | return; |
---|
1975 | } |
---|
1976 | |
---|
1977 | p = fork(); |
---|
1978 | if (p < 0) { |
---|
1979 | irc_rootmsg(irc, "otr keygen: couldn't fork: %s", strerror(errno)); |
---|
1980 | return; |
---|
1981 | } |
---|
1982 | |
---|
1983 | if (!p) { |
---|
1984 | /* child process */ |
---|
1985 | signal(SIGTERM, exit); |
---|
1986 | keygen_child_main(irc->otr->us, to[0], from[1]); |
---|
1987 | exit(0); |
---|
1988 | } |
---|
1989 | |
---|
1990 | irc->otr->keygen = p; |
---|
1991 | irc->otr->to = tof; |
---|
1992 | irc->otr->from = fromf; |
---|
1993 | irc->otr->sent_accountname = NULL; |
---|
1994 | irc->otr->sent_protocol = NULL; |
---|
1995 | irc->otr->todo = NULL; |
---|
1996 | b_input_add(from[0], B_EV_IO_READ, keygen_finish_handler, irc); |
---|
1997 | } |
---|
1998 | |
---|
1999 | /* is the keygen slave currently working? */ |
---|
2000 | if (irc->otr->sent_accountname) { |
---|
2001 | /* enqueue our job for later transmission */ |
---|
2002 | kg_t **kg = &irc->otr->todo; |
---|
2003 | while (*kg) { |
---|
2004 | kg = &((*kg)->next); |
---|
2005 | } |
---|
2006 | *kg = g_new0(kg_t, 1); |
---|
2007 | (*kg)->accountname = g_strdup(handle); |
---|
2008 | (*kg)->protocol = g_strdup(protocol); |
---|
2009 | } else { |
---|
2010 | /* send our job over and remember it */ |
---|
2011 | fprintf(irc->otr->to, "%s\n%s\n", handle, protocol); |
---|
2012 | fflush(irc->otr->to); |
---|
2013 | irc->otr->sent_accountname = g_strdup(handle); |
---|
2014 | irc->otr->sent_protocol = g_strdup(protocol); |
---|
2015 | } |
---|
2016 | } |
---|
2017 | |
---|
2018 | void keygen_child_main(OtrlUserState us, int infd, int outfd) |
---|
2019 | { |
---|
2020 | FILE *input, *output; |
---|
2021 | char filename[128], accountname[512], protocol[512]; |
---|
2022 | gcry_error_t e; |
---|
2023 | int tempfd; |
---|
2024 | |
---|
2025 | input = fdopen(infd, "r"); |
---|
2026 | output = fdopen(outfd, "w"); |
---|
2027 | |
---|
2028 | while (!feof(input) && !ferror(input) && !feof(output) && !ferror(output)) { |
---|
2029 | myfgets(accountname, 512, input); |
---|
2030 | myfgets(protocol, 512, input); |
---|
2031 | |
---|
2032 | strncpy(filename, "/tmp/bitlbee-XXXXXX", 128); |
---|
2033 | tempfd = mkstemp(filename); |
---|
2034 | close(tempfd); |
---|
2035 | |
---|
2036 | e = otrl_privkey_generate(us, filename, accountname, protocol); |
---|
2037 | if (e) { |
---|
2038 | fprintf(output, "\n"); /* this means failure */ |
---|
2039 | fprintf(output, "otr keygen: %s\n", gcry_strerror(e)); |
---|
2040 | unlink(filename); |
---|
2041 | } else { |
---|
2042 | fprintf(output, "%s\n", filename); |
---|
2043 | fprintf(output, "otr keygen for %s/%s complete\n", accountname, protocol); |
---|
2044 | } |
---|
2045 | fflush(output); |
---|
2046 | } |
---|
2047 | |
---|
2048 | fclose(input); |
---|
2049 | fclose(output); |
---|
2050 | } |
---|
2051 | |
---|
2052 | gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond) |
---|
2053 | { |
---|
2054 | irc_t *irc = (irc_t *) data; |
---|
2055 | char filename[512], msg[512]; |
---|
2056 | |
---|
2057 | myfgets(filename, 512, irc->otr->from); |
---|
2058 | myfgets(msg, 512, irc->otr->from); |
---|
2059 | |
---|
2060 | irc_rootmsg(irc, "%s", msg); |
---|
2061 | if (filename[0]) { |
---|
2062 | if (strsane(irc->user->nick)) { |
---|
2063 | char *kf = g_strdup_printf("%s%s.otr_keys", global.conf->configdir, irc->user->nick); |
---|
2064 | char *tmp = g_strdup_printf("%s.new", kf); |
---|
2065 | copyfile(filename, tmp); |
---|
2066 | unlink(filename); |
---|
2067 | rename(tmp, kf); |
---|
2068 | otrl_privkey_read(irc->otr->us, kf); |
---|
2069 | g_free(kf); |
---|
2070 | g_free(tmp); |
---|
2071 | } else { |
---|
2072 | otrl_privkey_read(irc->otr->us, filename); |
---|
2073 | unlink(filename); |
---|
2074 | } |
---|
2075 | } |
---|
2076 | |
---|
2077 | /* forget this job */ |
---|
2078 | g_free(irc->otr->sent_accountname); |
---|
2079 | g_free(irc->otr->sent_protocol); |
---|
2080 | irc->otr->sent_accountname = NULL; |
---|
2081 | irc->otr->sent_protocol = NULL; |
---|
2082 | |
---|
2083 | /* see if there are any more in the queue */ |
---|
2084 | if (irc->otr->todo) { |
---|
2085 | kg_t *p = irc->otr->todo; |
---|
2086 | /* send the next one over */ |
---|
2087 | fprintf(irc->otr->to, "%s\n%s\n", p->accountname, p->protocol); |
---|
2088 | fflush(irc->otr->to); |
---|
2089 | irc->otr->sent_accountname = p->accountname; |
---|
2090 | irc->otr->sent_protocol = p->protocol; |
---|
2091 | irc->otr->todo = p->next; |
---|
2092 | g_free(p); |
---|
2093 | return TRUE; /* keep watching */ |
---|
2094 | } else { |
---|
2095 | /* okay, the slave is idle now, so kill him */ |
---|
2096 | fclose(irc->otr->from); |
---|
2097 | fclose(irc->otr->to); |
---|
2098 | irc->otr->from = irc->otr->to = NULL; |
---|
2099 | kill(irc->otr->keygen, SIGTERM); |
---|
2100 | waitpid(irc->otr->keygen, NULL, 0); |
---|
2101 | irc->otr->keygen = 0; |
---|
2102 | return FALSE; /* unregister ourselves */ |
---|
2103 | } |
---|
2104 | } |
---|
2105 | |
---|
2106 | void copyfile(const char *a, const char *b) |
---|
2107 | { |
---|
2108 | int fda, fdb; |
---|
2109 | int n; |
---|
2110 | char buf[1024]; |
---|
2111 | |
---|
2112 | fda = open(a, O_RDONLY); |
---|
2113 | fdb = open(b, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
---|
2114 | |
---|
2115 | while ((n = read(fda, buf, 1024)) > 0) { |
---|
2116 | write(fdb, buf, n); |
---|
2117 | } |
---|
2118 | |
---|
2119 | close(fda); |
---|
2120 | close(fdb); |
---|
2121 | } |
---|
2122 | |
---|
2123 | void myfgets(char *s, int size, FILE *stream) |
---|
2124 | { |
---|
2125 | if (!fgets(s, size, stream)) { |
---|
2126 | s[0] = '\0'; |
---|
2127 | } else { |
---|
2128 | int n = strlen(s); |
---|
2129 | if (n > 0 && s[n - 1] == '\n') { |
---|
2130 | s[n - 1] = '\0'; |
---|
2131 | } |
---|
2132 | } |
---|
2133 | } |
---|
2134 | |
---|
2135 | void yes_keygen(void *data) |
---|
2136 | { |
---|
2137 | account_t *acc = (account_t *) data; |
---|
2138 | irc_t *irc = acc->bee->ui_data; |
---|
2139 | |
---|
2140 | if (keygen_in_progress(irc, acc->user, acc->prpl->name)) { |
---|
2141 | irc_rootmsg(irc, "keygen for %s/%s already in progress", |
---|
2142 | acc->user, acc->prpl->name); |
---|
2143 | } else { |
---|
2144 | irc_rootmsg(irc, "starting background keygen for %s/%s", |
---|
2145 | acc->user, acc->prpl->name); |
---|
2146 | irc_rootmsg(irc, "you will be notified when it completes"); |
---|
2147 | otr_keygen(irc, acc->user, acc->prpl->name); |
---|
2148 | } |
---|
2149 | } |
---|
2150 | |
---|
2151 | /* check whether a string is safe to use in a path component */ |
---|
2152 | int strsane(const char *s) |
---|
2153 | { |
---|
2154 | return strpbrk(s, "/\\") == NULL; |
---|
2155 | } |
---|
2156 | |
---|
2157 | /* close the OTR connection with the given buddy */ |
---|
2158 | gboolean otr_disconnect_user(irc_t *irc, irc_user_t *u) |
---|
2159 | { |
---|
2160 | if (!u || !u->bu || !u->bu->ic) { |
---|
2161 | return FALSE; |
---|
2162 | } |
---|
2163 | |
---|
2164 | /* XXX we disconnect all instances; is that what we want? */ |
---|
2165 | otrl_message_disconnect_all_instances(irc->otr->us, &otr_ops, |
---|
2166 | u->bu->ic, u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, |
---|
2167 | u->bu->handle); |
---|
2168 | |
---|
2169 | u->flags &= ~IRC_USER_OTR_TRUSTED; |
---|
2170 | u->flags &= ~IRC_USER_OTR_ENCRYPTED; |
---|
2171 | otr_update_modeflags(irc, u); |
---|
2172 | |
---|
2173 | return TRUE; |
---|
2174 | } |
---|
2175 | |
---|
2176 | /* close all active OTR connections */ |
---|
2177 | void otr_disconnect_all(irc_t *irc) |
---|
2178 | { |
---|
2179 | irc_user_t *u; |
---|
2180 | ConnContext *ctx; |
---|
2181 | |
---|
2182 | for (ctx = irc->otr->us->context_root; ctx; ctx = ctx->next) { |
---|
2183 | if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { |
---|
2184 | u = peeruser(irc, ctx->username, ctx->protocol); |
---|
2185 | (void) otr_disconnect_user(irc, u); |
---|
2186 | } |
---|
2187 | } |
---|
2188 | } |
---|