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