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