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