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