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