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