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