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