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