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