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