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