1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2008 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* |
---|
8 | OTR support (cf. http://www.cypherpunks.ca/otr/) |
---|
9 | |
---|
10 | 2008, Sven Moritz Hallberg <pesco@khjk.org> |
---|
11 | (c) and funded by stonedcoder.org |
---|
12 | |
---|
13 | files used to store OTR data: |
---|
14 | <configdir>/<nick>.otr_keys |
---|
15 | <configdir>/<nick>.otr_fprints |
---|
16 | |
---|
17 | top-level todos: (search for TODO for more ;-)) |
---|
18 | integrate otr_load/otr_save with existing storage backends |
---|
19 | per-account policy settings |
---|
20 | per-user policy settings |
---|
21 | */ |
---|
22 | |
---|
23 | /* |
---|
24 | This program is free software; you can redistribute it and/or modify |
---|
25 | it under the terms of the GNU General Public License as published by |
---|
26 | the Free Software Foundation; either version 2 of the License, or |
---|
27 | (at your option) any later version. |
---|
28 | |
---|
29 | This program is distributed in the hope that it will be useful, |
---|
30 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
31 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
32 | GNU General Public License for more details. |
---|
33 | |
---|
34 | You should have received a copy of the GNU General Public License with |
---|
35 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
36 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
37 | Suite 330, Boston, MA 02111-1307 USA |
---|
38 | */ |
---|
39 | |
---|
40 | #include "bitlbee.h" |
---|
41 | #ifdef WITH_OTR |
---|
42 | #include "irc.h" |
---|
43 | #include "otr.h" |
---|
44 | #include <sys/types.h> |
---|
45 | #include <sys/wait.h> |
---|
46 | #include <unistd.h> |
---|
47 | |
---|
48 | |
---|
49 | /** OTR interface routines for the OtrlMessageAppOps struct: **/ |
---|
50 | |
---|
51 | OtrlPolicy op_policy(void *opdata, ConnContext *context); |
---|
52 | |
---|
53 | void op_create_privkey(void *opdata, const char *accountname, const char *protocol); |
---|
54 | |
---|
55 | int op_is_logged_in(void *opdata, const char *accountname, const char *protocol, |
---|
56 | const char *recipient); |
---|
57 | |
---|
58 | void op_inject_message(void *opdata, const char *accountname, const char *protocol, |
---|
59 | const char *recipient, const char *message); |
---|
60 | |
---|
61 | int op_display_otr_message(void *opdata, const char *accountname, const char *protocol, |
---|
62 | const char *username, const char *msg); |
---|
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 | |
---|
82 | /** otr sub-command handlers: **/ |
---|
83 | |
---|
84 | void cmd_otr_connect(irc_t *irc, char **args); |
---|
85 | void cmd_otr_disconnect(irc_t *irc, char **args); |
---|
86 | void cmd_otr_smp(irc_t *irc, char **args); |
---|
87 | void cmd_otr_trust(irc_t *irc, char **args); |
---|
88 | void cmd_otr_info(irc_t *irc, char **args); |
---|
89 | void cmd_otr_keygen(irc_t *irc, char **args); |
---|
90 | void cmd_otr_forget(irc_t *irc, char **args); |
---|
91 | |
---|
92 | const command_t otr_commands[] = { |
---|
93 | { "connect", 1, &cmd_otr_connect, 0 }, |
---|
94 | { "disconnect", 1, &cmd_otr_disconnect, 0 }, |
---|
95 | { "smp", 2, &cmd_otr_smp, 0 }, |
---|
96 | { "trust", 6, &cmd_otr_trust, 0 }, |
---|
97 | { "info", 0, &cmd_otr_info, 0 }, |
---|
98 | { "keygen", 1, &cmd_otr_keygen, 0 }, |
---|
99 | { "forget", 2, &cmd_otr_forget, 0 }, |
---|
100 | { NULL } |
---|
101 | }; |
---|
102 | |
---|
103 | |
---|
104 | /** misc. helpers/subroutines: **/ |
---|
105 | |
---|
106 | /* start background process to generate a (new) key for a given account */ |
---|
107 | void otr_keygen(irc_t *irc, const char *handle, const char *protocol); |
---|
108 | |
---|
109 | /* main function for the forked keygen slave */ |
---|
110 | void keygen_child_main(OtrlUserState us, int infd, int outfd); |
---|
111 | |
---|
112 | /* mainloop handler for when a keygen finishes */ |
---|
113 | gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond); |
---|
114 | |
---|
115 | /* copy the contents of file a to file b, overwriting it if it exists */ |
---|
116 | void copyfile(const char *a, const char *b); |
---|
117 | |
---|
118 | /* read one line of input from a stream, excluding trailing newline */ |
---|
119 | void myfgets(char *s, int size, FILE *stream); |
---|
120 | |
---|
121 | /* some yes/no handlers */ |
---|
122 | void yes_keygen(gpointer w, void *data); |
---|
123 | void yes_forget_fingerprint(gpointer w, void *data); |
---|
124 | void yes_forget_context(gpointer w, void *data); |
---|
125 | void yes_forget_key(gpointer w, void *data); |
---|
126 | |
---|
127 | /* helper to make sure accountname and protocol match the incoming "opdata" */ |
---|
128 | struct im_connection *check_imc(void *opdata, const char *accountname, |
---|
129 | const char *protocol); |
---|
130 | |
---|
131 | /* determine the nick for a given handle/protocol pair |
---|
132 | returns "handle/protocol" if not found */ |
---|
133 | const char *peernick(irc_t *irc, const char *handle, const char *protocol); |
---|
134 | |
---|
135 | /* turn a hexadecimal digit into its numerical value */ |
---|
136 | int hexval(char a); |
---|
137 | |
---|
138 | /* determine the user_t for a given handle/protocol pair |
---|
139 | returns NULL if not found */ |
---|
140 | user_t *peeruser(irc_t *irc, const char *handle, const char *protocol); |
---|
141 | |
---|
142 | /* handle SMP TLVs from a received message */ |
---|
143 | void otr_handle_smp(struct im_connection *ic, const char *handle, OtrlTLV *tlvs); |
---|
144 | |
---|
145 | /* update op/voice flag of given user according to encryption state and settings |
---|
146 | returns 0 if neither op_buddies nor voice_buddies is set to "encrypted", |
---|
147 | i.e. msgstate should be announced seperately */ |
---|
148 | int otr_update_modeflags(irc_t *irc, user_t *u); |
---|
149 | |
---|
150 | /* show general info about the OTR subsystem; called by 'otr info' */ |
---|
151 | void show_general_otr_info(irc_t *irc); |
---|
152 | |
---|
153 | /* show info about a given OTR context */ |
---|
154 | void show_otr_context_info(irc_t *irc, ConnContext *ctx); |
---|
155 | |
---|
156 | /* show the list of fingerprints associated with a given context */ |
---|
157 | void show_fingerprints(irc_t *irc, ConnContext *ctx); |
---|
158 | |
---|
159 | /* find a fingerprint by prefix (given as any number of hex strings) */ |
---|
160 | Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args); |
---|
161 | |
---|
162 | /* find a private key by fingerprint prefix (given as any number of hex strings) */ |
---|
163 | OtrlPrivKey *match_privkey(irc_t *irc, const char **args); |
---|
164 | |
---|
165 | |
---|
166 | /*** routines declared in otr.h: ***/ |
---|
167 | |
---|
168 | void otr_init(void) |
---|
169 | { |
---|
170 | OTRL_INIT; |
---|
171 | |
---|
172 | /* fill global OtrlMessageAppOps */ |
---|
173 | global.otr_ops.policy = &op_policy; |
---|
174 | global.otr_ops.create_privkey = &op_create_privkey; |
---|
175 | global.otr_ops.is_logged_in = &op_is_logged_in; |
---|
176 | global.otr_ops.inject_message = &op_inject_message; |
---|
177 | global.otr_ops.notify = NULL; |
---|
178 | global.otr_ops.display_otr_message = &op_display_otr_message; |
---|
179 | global.otr_ops.update_context_list = NULL; |
---|
180 | global.otr_ops.protocol_name = NULL; |
---|
181 | global.otr_ops.protocol_name_free = NULL; |
---|
182 | global.otr_ops.new_fingerprint = &op_new_fingerprint; |
---|
183 | global.otr_ops.write_fingerprints = &op_write_fingerprints; |
---|
184 | global.otr_ops.gone_secure = &op_gone_secure; |
---|
185 | global.otr_ops.gone_insecure = &op_gone_insecure; |
---|
186 | global.otr_ops.still_secure = &op_still_secure; |
---|
187 | global.otr_ops.log_message = &op_log_message; |
---|
188 | global.otr_ops.max_message_size = &op_max_message_size; |
---|
189 | global.otr_ops.account_name = &op_account_name; |
---|
190 | global.otr_ops.account_name_free = NULL; |
---|
191 | } |
---|
192 | |
---|
193 | void otr_load(irc_t *irc) |
---|
194 | { |
---|
195 | char s[512]; |
---|
196 | account_t *a; |
---|
197 | gcry_error_t e; |
---|
198 | gcry_error_t enoent = gcry_error_from_errno(ENOENT); |
---|
199 | |
---|
200 | log_message(LOGLVL_DEBUG, "otr_load '%s'", irc->nick); |
---|
201 | |
---|
202 | g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, irc->nick); |
---|
203 | e = otrl_privkey_read(irc->otr_us, s); |
---|
204 | if(e && e!=enoent) { |
---|
205 | irc_usermsg(irc, "otr load: %s: %s", s, gcry_strerror(e)); |
---|
206 | } |
---|
207 | g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->nick); |
---|
208 | e = otrl_privkey_read_fingerprints(irc->otr_us, s, NULL, NULL); |
---|
209 | if(e && e!=enoent) { |
---|
210 | irc_usermsg(irc, "otr load: %s: %s", s, gcry_strerror(e)); |
---|
211 | } |
---|
212 | |
---|
213 | /* check for otr keys on all accounts */ |
---|
214 | for(a=irc->accounts; a; a=a->next) { |
---|
215 | otr_check_for_key(a); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | void otr_save(irc_t *irc) |
---|
220 | { |
---|
221 | char s[512]; |
---|
222 | gcry_error_t e; |
---|
223 | |
---|
224 | log_message(LOGLVL_DEBUG, "otr_save '%s'", irc->nick); |
---|
225 | |
---|
226 | g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->nick); |
---|
227 | e = otrl_privkey_write_fingerprints(irc->otr_us, s); |
---|
228 | if(e) { |
---|
229 | irc_usermsg(irc, "otr save: %s: %s", s, gcry_strerror(e)); |
---|
230 | } |
---|
231 | chmod(s, 0600); |
---|
232 | } |
---|
233 | |
---|
234 | void otr_remove(const char *nick) |
---|
235 | { |
---|
236 | char s[512]; |
---|
237 | |
---|
238 | log_message(LOGLVL_DEBUG, "otr_remove '%s'", nick); |
---|
239 | |
---|
240 | g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, nick); |
---|
241 | unlink(s); |
---|
242 | g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, nick); |
---|
243 | unlink(s); |
---|
244 | } |
---|
245 | |
---|
246 | void otr_rename(const char *onick, const char *nnick) |
---|
247 | { |
---|
248 | char s[512], t[512]; |
---|
249 | |
---|
250 | log_message(LOGLVL_DEBUG, "otr_rename '%s' -> '%s'", onick, nnick); |
---|
251 | |
---|
252 | g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, onick); |
---|
253 | g_snprintf(t, 511, "%s%s.otr_keys", global.conf->configdir, nnick); |
---|
254 | rename(s,t); |
---|
255 | g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, onick); |
---|
256 | g_snprintf(t, 511, "%s%s.otr_fprints", global.conf->configdir, nnick); |
---|
257 | rename(s,t); |
---|
258 | } |
---|
259 | |
---|
260 | void otr_check_for_key(account_t *a) |
---|
261 | { |
---|
262 | irc_t *irc = a->irc; |
---|
263 | OtrlPrivKey *k; |
---|
264 | |
---|
265 | k = otrl_privkey_find(irc->otr_us, a->user, a->prpl->name); |
---|
266 | if(k) { |
---|
267 | irc_usermsg(irc, "otr: %s/%s ready", |
---|
268 | a->user, a->prpl->name); |
---|
269 | } else { |
---|
270 | otr_keygen(irc, a->user, a->prpl->name); |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | char *otr_handle_message(struct im_connection *ic, const char *handle, const char *msg) |
---|
275 | { |
---|
276 | int ignore_msg; |
---|
277 | char *newmsg = NULL; |
---|
278 | OtrlTLV *tlvs = NULL; |
---|
279 | char *colormsg; |
---|
280 | |
---|
281 | ignore_msg = otrl_message_receiving(ic->irc->otr_us, &global.otr_ops, ic, |
---|
282 | ic->acc->user, ic->acc->prpl->name, handle, msg, &newmsg, |
---|
283 | &tlvs, NULL, NULL); |
---|
284 | |
---|
285 | otr_handle_smp(ic, handle, tlvs); |
---|
286 | |
---|
287 | if(ignore_msg) { |
---|
288 | /* this was an internal OTR protocol message */ |
---|
289 | return NULL; |
---|
290 | } else if(!newmsg) { |
---|
291 | /* this was a non-OTR message */ |
---|
292 | return g_strdup(msg); |
---|
293 | } else { |
---|
294 | /* OTR has processed this message */ |
---|
295 | ConnContext *context = otrl_context_find(ic->irc->otr_us, handle, |
---|
296 | ic->acc->user, ic->acc->prpl->name, 0, NULL, NULL, NULL); |
---|
297 | if(context && context->msgstate == OTRL_MSGSTATE_ENCRYPTED && |
---|
298 | set_getbool(&ic->irc->set, "color_encrypted")) { |
---|
299 | /* color according to f'print trust */ |
---|
300 | char color; |
---|
301 | const char *trust = context->active_fingerprint->trust; |
---|
302 | if(trust && trust[0] != '\0') |
---|
303 | color='3'; /* green */ |
---|
304 | else |
---|
305 | color='5'; /* red */ |
---|
306 | colormsg = g_strdup_printf("\x03%c%s\x0F", color, newmsg); |
---|
307 | } else { |
---|
308 | colormsg = g_strdup(newmsg); |
---|
309 | } |
---|
310 | otrl_message_free(newmsg); |
---|
311 | return colormsg; |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | int otr_send_message(struct im_connection *ic, const char *handle, const char *msg, int flags) |
---|
316 | { |
---|
317 | int st; |
---|
318 | char *otrmsg = NULL; |
---|
319 | ConnContext *ctx = NULL; |
---|
320 | |
---|
321 | st = otrl_message_sending(ic->irc->otr_us, &global.otr_ops, ic, |
---|
322 | ic->acc->user, ic->acc->prpl->name, handle, |
---|
323 | msg, NULL, &otrmsg, NULL, NULL); |
---|
324 | if(st) { |
---|
325 | return st; |
---|
326 | } |
---|
327 | |
---|
328 | ctx = otrl_context_find(ic->irc->otr_us, |
---|
329 | handle, ic->acc->user, ic->acc->prpl->name, |
---|
330 | 1, NULL, NULL, NULL); |
---|
331 | |
---|
332 | if(otrmsg) { |
---|
333 | if(!ctx) { |
---|
334 | otrl_message_free(otrmsg); |
---|
335 | return 1; |
---|
336 | } |
---|
337 | st = otrl_message_fragment_and_send(&global.otr_ops, ic, ctx, |
---|
338 | otrmsg, OTRL_FRAGMENT_SEND_ALL, NULL); |
---|
339 | otrl_message_free(otrmsg); |
---|
340 | } else { |
---|
341 | /* note: otrl_message_sending handles policy, so that if REQUIRE_ENCRYPTION is set, |
---|
342 | this case does not occur */ |
---|
343 | st = ic->acc->prpl->buddy_msg( ic, (char *)handle, (char *)msg, flags ); |
---|
344 | } |
---|
345 | |
---|
346 | return st; |
---|
347 | } |
---|
348 | |
---|
349 | void cmd_otr(irc_t *irc, char **args) |
---|
350 | { |
---|
351 | const command_t *cmd; |
---|
352 | |
---|
353 | if(!args[0]) |
---|
354 | return; |
---|
355 | |
---|
356 | if(!args[1]) |
---|
357 | return; |
---|
358 | |
---|
359 | for(cmd=otr_commands; cmd->command; cmd++) { |
---|
360 | if(strcmp(cmd->command, args[1]) == 0) |
---|
361 | break; |
---|
362 | } |
---|
363 | |
---|
364 | if(!cmd->command) { |
---|
365 | irc_usermsg(irc, "%s: unknown subcommand \"%s\", see \x02help otr\x02", |
---|
366 | args[0], args[1]); |
---|
367 | return; |
---|
368 | } |
---|
369 | |
---|
370 | if(!args[cmd->required_parameters+1]) { |
---|
371 | irc_usermsg(irc, "%s %s: not enough arguments (%d req.)", |
---|
372 | args[0], args[1], cmd->required_parameters); |
---|
373 | return; |
---|
374 | } |
---|
375 | |
---|
376 | cmd->execute(irc, args+1); |
---|
377 | } |
---|
378 | |
---|
379 | |
---|
380 | /*** OTR "MessageAppOps" callbacks for global.otr_ui: ***/ |
---|
381 | |
---|
382 | OtrlPolicy op_policy(void *opdata, ConnContext *context) |
---|
383 | { |
---|
384 | struct im_connection *ic = check_imc(opdata, context->accountname, context->protocol); |
---|
385 | const char *p; |
---|
386 | |
---|
387 | p = set_getstr(&ic->irc->set, "otr_policy"); |
---|
388 | if(!strcmp(p, "never")) |
---|
389 | return OTRL_POLICY_NEVER; |
---|
390 | if(!strcmp(p, "opportunistic")) |
---|
391 | return OTRL_POLICY_OPPORTUNISTIC; |
---|
392 | if(!strcmp(p, "manual")) |
---|
393 | return OTRL_POLICY_MANUAL; |
---|
394 | if(!strcmp(p, "always")) |
---|
395 | return OTRL_POLICY_ALWAYS; |
---|
396 | |
---|
397 | return OTRL_POLICY_OPPORTUNISTIC; |
---|
398 | } |
---|
399 | |
---|
400 | void op_create_privkey(void *opdata, const char *accountname, |
---|
401 | const char *protocol) |
---|
402 | { |
---|
403 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
404 | char *s; |
---|
405 | |
---|
406 | log_message(LOGLVL_DEBUG, "op_create_privkey '%s' '%s'", accountname, protocol); |
---|
407 | |
---|
408 | s = g_strdup_printf("oops, no otr privkey for %s - generate one now?", |
---|
409 | accountname); |
---|
410 | query_add(ic->irc, ic, s, yes_keygen, NULL, ic->acc); |
---|
411 | } |
---|
412 | |
---|
413 | int op_is_logged_in(void *opdata, const char *accountname, |
---|
414 | const char *protocol, const char *recipient) |
---|
415 | { |
---|
416 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
417 | user_t *u; |
---|
418 | |
---|
419 | log_message(LOGLVL_DEBUG, "op_is_logged_in '%s' '%s' '%s'", accountname, protocol, recipient); |
---|
420 | |
---|
421 | /* lookup the user_t for the given recipient */ |
---|
422 | u = user_findhandle(ic, recipient); |
---|
423 | if(u) { |
---|
424 | if(u->online) |
---|
425 | return 1; |
---|
426 | else |
---|
427 | return 0; |
---|
428 | } else { |
---|
429 | return -1; |
---|
430 | } |
---|
431 | } |
---|
432 | |
---|
433 | void op_inject_message(void *opdata, const char *accountname, |
---|
434 | const char *protocol, const char *recipient, const char *message) |
---|
435 | { |
---|
436 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
437 | |
---|
438 | log_message(LOGLVL_DEBUG, "op_inject_message '%s' '%s' '%s' '%s'", accountname, protocol, recipient, message); |
---|
439 | |
---|
440 | if (strcmp(accountname, recipient) == 0) { |
---|
441 | /* huh? injecting messages to myself? */ |
---|
442 | irc_usermsg(ic->irc, "note to self: %s", message); |
---|
443 | } else { |
---|
444 | /* need to drop some consts here :-( */ |
---|
445 | /* TODO: get flags into op_inject_message?! */ |
---|
446 | ic->acc->prpl->buddy_msg(ic, (char *)recipient, (char *)message, 0); |
---|
447 | /* ignoring return value :-/ */ |
---|
448 | } |
---|
449 | } |
---|
450 | |
---|
451 | int op_display_otr_message(void *opdata, const char *accountname, |
---|
452 | const char *protocol, const char *username, const char *message) |
---|
453 | { |
---|
454 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
455 | char *msg = g_strdup(message); |
---|
456 | |
---|
457 | log_message(LOGLVL_DEBUG, "op_display_otr_message '%s' '%s' '%s' '%s'", accountname, protocol, username, message); |
---|
458 | |
---|
459 | strip_html(msg); |
---|
460 | irc_usermsg(ic->irc, "otr: %s", msg); |
---|
461 | |
---|
462 | g_free(msg); |
---|
463 | return 0; |
---|
464 | } |
---|
465 | |
---|
466 | void op_new_fingerprint(void *opdata, OtrlUserState us, |
---|
467 | const char *accountname, const char *protocol, |
---|
468 | const char *username, unsigned char fingerprint[20]) |
---|
469 | { |
---|
470 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
471 | char hunam[45]; /* anybody looking? ;-) */ |
---|
472 | |
---|
473 | otrl_privkey_hash_to_human(hunam, fingerprint); |
---|
474 | log_message(LOGLVL_DEBUG, "op_new_fingerprint '%s' '%s' '%s' '%s'", accountname, protocol, username, hunam); |
---|
475 | |
---|
476 | irc_usermsg(ic->irc, "new fingerprint for %s: %s", |
---|
477 | peernick(ic->irc, username, protocol), hunam); |
---|
478 | } |
---|
479 | |
---|
480 | void op_write_fingerprints(void *opdata) |
---|
481 | { |
---|
482 | struct im_connection *ic = (struct im_connection *)opdata; |
---|
483 | |
---|
484 | log_message(LOGLVL_DEBUG, "op_write_fingerprints"); |
---|
485 | |
---|
486 | otr_save(ic->irc); |
---|
487 | } |
---|
488 | |
---|
489 | void op_gone_secure(void *opdata, ConnContext *context) |
---|
490 | { |
---|
491 | struct im_connection *ic = |
---|
492 | check_imc(opdata, context->accountname, context->protocol); |
---|
493 | user_t *u; |
---|
494 | const char *trust; |
---|
495 | |
---|
496 | log_message(LOGLVL_DEBUG, "op_gone_secure '%s' '%s' '%s'", context->accountname, context->protocol, context->username); |
---|
497 | |
---|
498 | u = peeruser(ic->irc, context->username, context->protocol); |
---|
499 | if(!u) { |
---|
500 | log_message(LOGLVL_ERROR, |
---|
501 | "BUG: otr.c: op_gone_secure: user_t for %s/%s/%s not found!", |
---|
502 | context->username, context->protocol, context->accountname); |
---|
503 | return; |
---|
504 | } |
---|
505 | |
---|
506 | trust = context->active_fingerprint->trust; |
---|
507 | if(trust && trust[0]) |
---|
508 | u->encrypted = 2; |
---|
509 | else |
---|
510 | u->encrypted = 1; |
---|
511 | if(!otr_update_modeflags(ic->irc, u)) |
---|
512 | irc_usermsg(ic->irc, "conversation with %s is now off the record", u->nick); |
---|
513 | } |
---|
514 | |
---|
515 | void op_gone_insecure(void *opdata, ConnContext *context) |
---|
516 | { |
---|
517 | struct im_connection *ic = |
---|
518 | check_imc(opdata, context->accountname, context->protocol); |
---|
519 | user_t *u; |
---|
520 | |
---|
521 | log_message(LOGLVL_DEBUG, "op_gone_insecure '%s' '%s' '%s'", context->accountname, context->protocol, context->username); |
---|
522 | |
---|
523 | u = peeruser(ic->irc, context->username, context->protocol); |
---|
524 | if(!u) { |
---|
525 | log_message(LOGLVL_ERROR, |
---|
526 | "BUG: otr.c: op_gone_insecure: user_t for %s/%s/%s not found!", |
---|
527 | context->username, context->protocol, context->accountname); |
---|
528 | return; |
---|
529 | } |
---|
530 | u->encrypted = 0; |
---|
531 | if(!otr_update_modeflags(ic->irc, u)) |
---|
532 | irc_usermsg(ic->irc, "conversation with %s is now in the clear", u->nick); |
---|
533 | } |
---|
534 | |
---|
535 | void op_still_secure(void *opdata, ConnContext *context, int is_reply) |
---|
536 | { |
---|
537 | struct im_connection *ic = |
---|
538 | check_imc(opdata, context->accountname, context->protocol); |
---|
539 | user_t *u; |
---|
540 | |
---|
541 | log_message(LOGLVL_DEBUG, "op_still_secure '%s' '%s' '%s' is_reply=%d", |
---|
542 | context->accountname, context->protocol, context->username, is_reply); |
---|
543 | |
---|
544 | u = peeruser(ic->irc, context->username, context->protocol); |
---|
545 | if(!u) { |
---|
546 | log_message(LOGLVL_ERROR, |
---|
547 | "BUG: otr.c: op_still_secure: user_t for %s/%s/%s not found!", |
---|
548 | context->username, context->protocol, context->accountname); |
---|
549 | return; |
---|
550 | } |
---|
551 | if(context->active_fingerprint->trust[0]) |
---|
552 | u->encrypted = 2; |
---|
553 | else |
---|
554 | u->encrypted = 1; |
---|
555 | if(!otr_update_modeflags(ic->irc, u)) |
---|
556 | irc_usermsg(ic->irc, "otr connection with %s has been refreshed", u->nick); |
---|
557 | } |
---|
558 | |
---|
559 | void op_log_message(void *opdata, const char *message) |
---|
560 | { |
---|
561 | char *msg = g_strdup(message); |
---|
562 | |
---|
563 | strip_html(msg); |
---|
564 | log_message(LOGLVL_INFO, "otr: %s", msg); |
---|
565 | g_free(msg); |
---|
566 | } |
---|
567 | |
---|
568 | int op_max_message_size(void *opdata, ConnContext *context) |
---|
569 | { |
---|
570 | struct im_connection *ic = |
---|
571 | check_imc(opdata, context->accountname, context->protocol); |
---|
572 | |
---|
573 | return ic->acc->prpl->mms; |
---|
574 | } |
---|
575 | |
---|
576 | const char *op_account_name(void *opdata, const char *account, const char *protocol) |
---|
577 | { |
---|
578 | struct im_connection *ic = (struct im_connection *)opdata; |
---|
579 | |
---|
580 | log_message(LOGLVL_DEBUG, "op_account_name '%s' '%s'", account, protocol); |
---|
581 | |
---|
582 | return peernick(ic->irc, account, protocol); |
---|
583 | } |
---|
584 | |
---|
585 | |
---|
586 | /*** OTR sub-command handlers ***/ |
---|
587 | |
---|
588 | void cmd_otr_disconnect(irc_t *irc, char **args) |
---|
589 | { |
---|
590 | user_t *u; |
---|
591 | |
---|
592 | u = user_find(irc, args[1]); |
---|
593 | if(!u || !u->ic) { |
---|
594 | irc_usermsg(irc, "%s: unknown user", args[1]); |
---|
595 | return; |
---|
596 | } |
---|
597 | |
---|
598 | otrl_message_disconnect(irc->otr_us, &global.otr_ops, |
---|
599 | u->ic, u->ic->acc->user, u->ic->acc->prpl->name, u->handle); |
---|
600 | |
---|
601 | /* for some reason, libotr (3.1.0) doesn't do this itself: */ |
---|
602 | if(u->encrypted) { |
---|
603 | ConnContext *ctx; |
---|
604 | ctx = otrl_context_find(irc->otr_us, u->handle, u->ic->acc->user, |
---|
605 | u->ic->acc->prpl->name, 0, NULL, NULL, NULL); |
---|
606 | if(ctx) |
---|
607 | op_gone_insecure(u->ic, ctx); |
---|
608 | else /* huh? */ |
---|
609 | u->encrypted = 0; |
---|
610 | } |
---|
611 | } |
---|
612 | |
---|
613 | void cmd_otr_connect(irc_t *irc, char **args) |
---|
614 | { |
---|
615 | user_t *u; |
---|
616 | |
---|
617 | u = user_find(irc, args[1]); |
---|
618 | if(!u || !u->ic) { |
---|
619 | irc_usermsg(irc, "%s: unknown user", args[1]); |
---|
620 | return; |
---|
621 | } |
---|
622 | if(!u->online) { |
---|
623 | irc_usermsg(irc, "%s is offline", args[1]); |
---|
624 | return; |
---|
625 | } |
---|
626 | |
---|
627 | imc_buddy_msg(u->ic, u->handle, "?OTR?", 0); |
---|
628 | } |
---|
629 | |
---|
630 | void cmd_otr_smp(irc_t *irc, char **args) |
---|
631 | { |
---|
632 | user_t *u; |
---|
633 | ConnContext *ctx; |
---|
634 | |
---|
635 | u = user_find(irc, args[1]); |
---|
636 | if(!u || !u->ic) { |
---|
637 | irc_usermsg(irc, "%s: unknown user", args[1]); |
---|
638 | return; |
---|
639 | } |
---|
640 | if(!u->online) { |
---|
641 | irc_usermsg(irc, "%s is offline", args[1]); |
---|
642 | return; |
---|
643 | } |
---|
644 | |
---|
645 | ctx = otrl_context_find(irc->otr_us, u->handle, |
---|
646 | u->ic->acc->user, u->ic->acc->prpl->name, 1, NULL, NULL, NULL); |
---|
647 | if(!ctx) { |
---|
648 | /* huh? out of memory or what? */ |
---|
649 | return; |
---|
650 | } |
---|
651 | |
---|
652 | if(ctx->smstate->nextExpected != OTRL_SMP_EXPECT1) { |
---|
653 | log_message(LOGLVL_INFO, |
---|
654 | "SMP already in phase %d, sending abort before reinitiating", |
---|
655 | ctx->smstate->nextExpected+1); |
---|
656 | otrl_message_abort_smp(irc->otr_us, &global.otr_ops, u->ic, ctx); |
---|
657 | otrl_sm_state_free(ctx->smstate); |
---|
658 | } |
---|
659 | |
---|
660 | /* warning: the following assumes that smstates are cleared whenever an SMP |
---|
661 | is completed or aborted! */ |
---|
662 | if(ctx->smstate->secret == NULL) { |
---|
663 | irc_usermsg(irc, "smp: initiating with %s...", u->nick); |
---|
664 | otrl_message_initiate_smp(irc->otr_us, &global.otr_ops, |
---|
665 | u->ic, ctx, (unsigned char *)args[2], strlen(args[2])); |
---|
666 | /* smp is now in EXPECT2 */ |
---|
667 | } else { |
---|
668 | /* if we're still in EXPECT1 but smstate is initialized, we must have |
---|
669 | received the SMP1, so let's issue a response */ |
---|
670 | irc_usermsg(irc, "smp: responding to %s...", u->nick); |
---|
671 | otrl_message_respond_smp(irc->otr_us, &global.otr_ops, |
---|
672 | u->ic, ctx, (unsigned char *)args[2], strlen(args[2])); |
---|
673 | /* smp is now in EXPECT3 */ |
---|
674 | } |
---|
675 | } |
---|
676 | |
---|
677 | void cmd_otr_trust(irc_t *irc, char **args) |
---|
678 | { |
---|
679 | user_t *u; |
---|
680 | ConnContext *ctx; |
---|
681 | unsigned char raw[20]; |
---|
682 | Fingerprint *fp; |
---|
683 | int i,j; |
---|
684 | |
---|
685 | u = user_find(irc, args[1]); |
---|
686 | if(!u || !u->ic) { |
---|
687 | irc_usermsg(irc, "%s: unknown user", args[1]); |
---|
688 | return; |
---|
689 | } |
---|
690 | |
---|
691 | ctx = otrl_context_find(irc->otr_us, u->handle, |
---|
692 | u->ic->acc->user, u->ic->acc->prpl->name, 0, NULL, NULL, NULL); |
---|
693 | if(!ctx) { |
---|
694 | irc_usermsg(irc, "%s: no otr context with user", args[1]); |
---|
695 | return; |
---|
696 | } |
---|
697 | |
---|
698 | /* convert given fingerprint to raw representation */ |
---|
699 | for(i=0; i<5; i++) { |
---|
700 | for(j=0; j<4; j++) { |
---|
701 | char *p = args[2+i]+(2*j); |
---|
702 | char *q = p+1; |
---|
703 | int x, y; |
---|
704 | |
---|
705 | if(!*p || !*q) { |
---|
706 | irc_usermsg(irc, "failed: truncated fingerprint block %d", i+1); |
---|
707 | return; |
---|
708 | } |
---|
709 | |
---|
710 | x = hexval(*p); |
---|
711 | y = hexval(*q); |
---|
712 | if(x<0) { |
---|
713 | irc_usermsg(irc, "failed: %d. hex digit of block %d out of range", 2*j+1, i+1); |
---|
714 | return; |
---|
715 | } |
---|
716 | if(y<0) { |
---|
717 | irc_usermsg(irc, "failed: %d. hex digit of block %d out of range", 2*j+2, i+1); |
---|
718 | return; |
---|
719 | } |
---|
720 | |
---|
721 | raw[i*4+j] = x*16 + y; |
---|
722 | } |
---|
723 | } |
---|
724 | fp = otrl_context_find_fingerprint(ctx, raw, 0, NULL); |
---|
725 | if(!fp) { |
---|
726 | irc_usermsg(irc, "failed: no such fingerprint for %s", args[1]); |
---|
727 | } else { |
---|
728 | char *trust = args[7] ? args[7] : "affirmed"; |
---|
729 | otrl_context_set_trust(fp, trust); |
---|
730 | irc_usermsg(irc, "fingerprint match, trust set to \"%s\"", trust); |
---|
731 | if(u->encrypted) |
---|
732 | u->encrypted = 2; |
---|
733 | otr_update_modeflags(irc, u); |
---|
734 | } |
---|
735 | } |
---|
736 | |
---|
737 | void cmd_otr_info(irc_t *irc, char **args) |
---|
738 | { |
---|
739 | if(!args[1]) { |
---|
740 | show_general_otr_info(irc); |
---|
741 | } else { |
---|
742 | char *arg = g_strdup(args[1]); |
---|
743 | char *myhandle, *handle, *protocol; |
---|
744 | ConnContext *ctx; |
---|
745 | |
---|
746 | /* interpret arg as 'user/protocol/account' if possible */ |
---|
747 | protocol = strchr(arg, '/'); |
---|
748 | if(protocol) { |
---|
749 | *(protocol++) = '\0'; |
---|
750 | myhandle = strchr(protocol, '/'); |
---|
751 | } |
---|
752 | if(protocol && myhandle) { |
---|
753 | *(myhandle++) = '\0'; |
---|
754 | handle = arg; |
---|
755 | ctx = otrl_context_find(irc->otr_us, handle, myhandle, protocol, 0, NULL, NULL, NULL); |
---|
756 | if(!ctx) { |
---|
757 | irc_usermsg(irc, "no such context"); |
---|
758 | g_free(arg); |
---|
759 | return; |
---|
760 | } |
---|
761 | } else { |
---|
762 | user_t *u = user_find(irc, args[1]); |
---|
763 | if(!u || !u->ic) { |
---|
764 | irc_usermsg(irc, "%s: unknown user", args[1]); |
---|
765 | g_free(arg); |
---|
766 | return; |
---|
767 | } |
---|
768 | ctx = otrl_context_find(irc->otr_us, u->handle, u->ic->acc->user, |
---|
769 | u->ic->acc->prpl->name, 0, NULL, NULL, NULL); |
---|
770 | if(!ctx) { |
---|
771 | irc_usermsg(irc, "no otr context with %s", args[1]); |
---|
772 | g_free(arg); |
---|
773 | return; |
---|
774 | } |
---|
775 | } |
---|
776 | |
---|
777 | /* show how we resolved the (nick) argument, if we did */ |
---|
778 | if(handle!=arg) { |
---|
779 | irc_usermsg(irc, "%s is %s/%s; we are %s/%s to them", args[1], |
---|
780 | ctx->username, ctx->protocol, ctx->accountname, ctx->protocol); |
---|
781 | } |
---|
782 | show_otr_context_info(irc, ctx); |
---|
783 | g_free(arg); |
---|
784 | } |
---|
785 | } |
---|
786 | |
---|
787 | void cmd_otr_keygen(irc_t *irc, char **args) |
---|
788 | { |
---|
789 | int i, n; |
---|
790 | account_t *a; |
---|
791 | |
---|
792 | n = atoi(args[1]); |
---|
793 | if(n<0 || (!n && strcmp(args[1], "0"))) { |
---|
794 | irc_usermsg(irc, "%s: invalid account number", args[1]); |
---|
795 | return; |
---|
796 | } |
---|
797 | |
---|
798 | a = irc->accounts; |
---|
799 | for(i=0; i<n && a; i++, a=a->next); |
---|
800 | if(!a) { |
---|
801 | irc_usermsg(irc, "%s: no such account", args[1]); |
---|
802 | return; |
---|
803 | } |
---|
804 | |
---|
805 | if(otrl_privkey_find(irc->otr_us, a->user, a->prpl->name)) { |
---|
806 | char *s = g_strdup_printf("account %d already has a key, replace it?", n); |
---|
807 | query_add(irc, NULL, s, yes_keygen, NULL, a); |
---|
808 | } else { |
---|
809 | otr_keygen(irc, a->user, a->prpl->name); |
---|
810 | } |
---|
811 | } |
---|
812 | |
---|
813 | void yes_forget_fingerprint(gpointer w, void *data) |
---|
814 | { |
---|
815 | irc_t *irc = (irc_t *)w; |
---|
816 | Fingerprint *fp = (Fingerprint *)data; |
---|
817 | |
---|
818 | if(fp == fp->context->active_fingerprint) { |
---|
819 | irc_usermsg(irc, "that fingerprint is active, terminate otr connection first"); |
---|
820 | return; |
---|
821 | } |
---|
822 | |
---|
823 | otrl_context_forget_fingerprint(fp, 0); |
---|
824 | } |
---|
825 | |
---|
826 | void yes_forget_context(gpointer w, void *data) |
---|
827 | { |
---|
828 | irc_t *irc = (irc_t *)w; |
---|
829 | ConnContext *ctx = (ConnContext *)data; |
---|
830 | |
---|
831 | if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { |
---|
832 | irc_usermsg(irc, "active otr connection with %s, terminate it first", |
---|
833 | peernick(irc, ctx->username, ctx->protocol)); |
---|
834 | return; |
---|
835 | } |
---|
836 | |
---|
837 | if(ctx->msgstate == OTRL_MSGSTATE_FINISHED) |
---|
838 | otrl_context_force_plaintext(ctx); |
---|
839 | otrl_context_forget(ctx); |
---|
840 | } |
---|
841 | |
---|
842 | void yes_forget_key(gpointer w, void *data) |
---|
843 | { |
---|
844 | OtrlPrivKey *key = (OtrlPrivKey *)data; |
---|
845 | |
---|
846 | /* FIXME: For some reason which /completely eludes me/, this call keeps |
---|
847 | barfing on the gcry_sexp_release inside (invalid pointer free). */ |
---|
848 | otrl_privkey_forget(key); |
---|
849 | } |
---|
850 | |
---|
851 | void cmd_otr_forget(irc_t *irc, char **args) |
---|
852 | { |
---|
853 | if(!strcmp(args[1], "fingerprint")) |
---|
854 | { |
---|
855 | user_t *u; |
---|
856 | ConnContext *ctx; |
---|
857 | Fingerprint *fp; |
---|
858 | char human[54]; |
---|
859 | char *s; |
---|
860 | |
---|
861 | if(!args[3]) { |
---|
862 | irc_usermsg(irc, "otr %s %s: not enough arguments (2 req.)", args[0], args[1]); |
---|
863 | return; |
---|
864 | } |
---|
865 | |
---|
866 | /* TODO: allow context specs ("user/proto/account") in 'otr forget fingerprint'? */ |
---|
867 | u = user_find(irc, args[2]); |
---|
868 | if(!u || !u->ic) { |
---|
869 | irc_usermsg(irc, "%s: unknown user", args[2]); |
---|
870 | return; |
---|
871 | } |
---|
872 | |
---|
873 | ctx = otrl_context_find(irc->otr_us, u->handle, u->ic->acc->user, |
---|
874 | u->ic->acc->prpl->name, 0, NULL, NULL, NULL); |
---|
875 | if(!ctx) { |
---|
876 | irc_usermsg(irc, "no otr context with %s", args[2]); |
---|
877 | return; |
---|
878 | } |
---|
879 | |
---|
880 | fp = match_fingerprint(irc, ctx, ((const char **)args)+3); |
---|
881 | if(!fp) { |
---|
882 | /* match_fingerprint does error messages */ |
---|
883 | return; |
---|
884 | } |
---|
885 | |
---|
886 | if(fp == ctx->active_fingerprint) { |
---|
887 | irc_usermsg(irc, "that fingerprint is active, terminate otr connection first"); |
---|
888 | return; |
---|
889 | } |
---|
890 | |
---|
891 | otrl_privkey_hash_to_human(human, fp->fingerprint); |
---|
892 | s = g_strdup_printf("about to forget fingerprint %s, are you sure?", human); |
---|
893 | query_add(irc, NULL, s, yes_forget_fingerprint, NULL, fp); |
---|
894 | } |
---|
895 | |
---|
896 | else if(!strcmp(args[1], "context")) |
---|
897 | { |
---|
898 | user_t *u; |
---|
899 | ConnContext *ctx; |
---|
900 | char *s; |
---|
901 | |
---|
902 | /* TODO: allow context specs ("user/proto/account") in 'otr forget contex'? */ |
---|
903 | u = user_find(irc, args[2]); |
---|
904 | if(!u || !u->ic) { |
---|
905 | irc_usermsg(irc, "%s: unknown user", args[2]); |
---|
906 | return; |
---|
907 | } |
---|
908 | |
---|
909 | ctx = otrl_context_find(irc->otr_us, u->handle, u->ic->acc->user, |
---|
910 | u->ic->acc->prpl->name, 0, NULL, NULL, NULL); |
---|
911 | if(!ctx) { |
---|
912 | irc_usermsg(irc, "no otr context with %s", args[2]); |
---|
913 | return; |
---|
914 | } |
---|
915 | |
---|
916 | if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { |
---|
917 | irc_usermsg(irc, "active otr connection with %s, terminate it first", args[2]); |
---|
918 | return; |
---|
919 | } |
---|
920 | |
---|
921 | s = g_strdup_printf("about to forget otr data about %s, are you sure?", args[2]); |
---|
922 | query_add(irc, NULL, s, yes_forget_context, NULL, ctx); |
---|
923 | } |
---|
924 | |
---|
925 | else if(!strcmp(args[1], "key")) |
---|
926 | { |
---|
927 | OtrlPrivKey *key; |
---|
928 | char *s; |
---|
929 | |
---|
930 | key = match_privkey(irc, ((const char **)args)+2); |
---|
931 | if(!key) { |
---|
932 | /* match_privkey does error messages */ |
---|
933 | return; |
---|
934 | } |
---|
935 | |
---|
936 | /* TODO: Find out why 'otr forget key' barfs (cf. yes_forget_key) */ |
---|
937 | irc_usermsg(irc, "otr %s %s: not implemented, please edit \x02%s%s.otr_keys\x02 manually :-/", |
---|
938 | args[0], args[1], global.conf->configdir, irc->nick); |
---|
939 | return; |
---|
940 | |
---|
941 | s = g_strdup_printf("about to forget the private key for %s/%s, are you sure?", |
---|
942 | key->accountname, key->protocol); |
---|
943 | query_add(irc, NULL, s, yes_forget_key, NULL, key); |
---|
944 | } |
---|
945 | |
---|
946 | else |
---|
947 | { |
---|
948 | irc_usermsg(irc, "otr %s: unknown subcommand \"%s\", see \x02help otr forget\x02", |
---|
949 | args[0], args[1]); |
---|
950 | } |
---|
951 | } |
---|
952 | |
---|
953 | |
---|
954 | /*** local helpers / subroutines: ***/ |
---|
955 | |
---|
956 | /* Socialist Millionaires' Protocol */ |
---|
957 | void otr_handle_smp(struct im_connection *ic, const char *handle, OtrlTLV *tlvs) |
---|
958 | { |
---|
959 | irc_t *irc = ic->irc; |
---|
960 | OtrlUserState us = irc->otr_us; |
---|
961 | OtrlMessageAppOps *ops = &global.otr_ops; |
---|
962 | OtrlTLV *tlv = NULL; |
---|
963 | ConnContext *context; |
---|
964 | NextExpectedSMP nextMsg; |
---|
965 | user_t *u; |
---|
966 | |
---|
967 | u = user_findhandle(ic, handle); |
---|
968 | if(!u) return; |
---|
969 | context = otrl_context_find(us, handle, |
---|
970 | ic->acc->user, ic->acc->prpl->name, 1, NULL, NULL, NULL); |
---|
971 | if(!context) { |
---|
972 | /* huh? out of memory or what? */ |
---|
973 | return; |
---|
974 | } |
---|
975 | nextMsg = context->smstate->nextExpected; |
---|
976 | |
---|
977 | tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP1); |
---|
978 | if (tlv) { |
---|
979 | if (nextMsg != OTRL_SMP_EXPECT1) { |
---|
980 | irc_usermsg(irc, "smp %s: spurious SMP1 received, aborting", u->nick); |
---|
981 | otrl_message_abort_smp(us, ops, u->ic, context); |
---|
982 | otrl_sm_state_free(context->smstate); |
---|
983 | } else { |
---|
984 | irc_usermsg(irc, "smp: initiated by %s" |
---|
985 | " - respond with \x02otr smp %s <secret>\x02", |
---|
986 | u->nick, u->nick); |
---|
987 | /* smp stays in EXPECT1 until user responds */ |
---|
988 | } |
---|
989 | } |
---|
990 | tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP2); |
---|
991 | if (tlv) { |
---|
992 | if (nextMsg != OTRL_SMP_EXPECT2) { |
---|
993 | irc_usermsg(irc, "smp %s: spurious SMP2 received, aborting", u->nick); |
---|
994 | otrl_message_abort_smp(us, ops, u->ic, context); |
---|
995 | otrl_sm_state_free(context->smstate); |
---|
996 | } else { |
---|
997 | /* SMP2 received, otrl_message_receiving will have sent SMP3 */ |
---|
998 | context->smstate->nextExpected = OTRL_SMP_EXPECT4; |
---|
999 | } |
---|
1000 | } |
---|
1001 | tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP3); |
---|
1002 | if (tlv) { |
---|
1003 | if (nextMsg != OTRL_SMP_EXPECT3) { |
---|
1004 | irc_usermsg(irc, "smp %s: spurious SMP3 received, aborting", u->nick); |
---|
1005 | otrl_message_abort_smp(us, ops, u->ic, context); |
---|
1006 | otrl_sm_state_free(context->smstate); |
---|
1007 | } else { |
---|
1008 | /* SMP3 received, otrl_message_receiving will have sent SMP4 and set fp trust */ |
---|
1009 | const char *trust = context->active_fingerprint->trust; |
---|
1010 | if(!trust || trust[0]=='\0') { |
---|
1011 | irc_usermsg(irc, "smp %s: secrets did not match, fingerprint not trusted", |
---|
1012 | u->nick); |
---|
1013 | } else { |
---|
1014 | irc_usermsg(irc, "smp %s: secrets proved equal, fingerprint trusted", |
---|
1015 | u->nick); |
---|
1016 | } |
---|
1017 | otrl_sm_state_free(context->smstate); |
---|
1018 | /* smp is in back in EXPECT1 */ |
---|
1019 | } |
---|
1020 | } |
---|
1021 | tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP4); |
---|
1022 | if (tlv) { |
---|
1023 | if (nextMsg != OTRL_SMP_EXPECT4) { |
---|
1024 | irc_usermsg(irc, "smp %s: spurious SMP4 received, aborting", u->nick); |
---|
1025 | otrl_message_abort_smp(us, ops, u->ic, context); |
---|
1026 | otrl_sm_state_free(context->smstate); |
---|
1027 | } else { |
---|
1028 | /* SMP4 received, otrl_message_receiving will have set fp trust */ |
---|
1029 | const char *trust = context->active_fingerprint->trust; |
---|
1030 | if(!trust || trust[0]=='\0') { |
---|
1031 | irc_usermsg(irc, "smp %s: secrets did not match, fingerprint not trusted", |
---|
1032 | u->nick); |
---|
1033 | } else { |
---|
1034 | irc_usermsg(irc, "smp %s: secrets proved equal, fingerprint trusted", |
---|
1035 | u->nick); |
---|
1036 | } |
---|
1037 | otrl_sm_state_free(context->smstate); |
---|
1038 | /* smp is in back in EXPECT1 */ |
---|
1039 | } |
---|
1040 | } |
---|
1041 | tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP_ABORT); |
---|
1042 | if (tlv) { |
---|
1043 | irc_usermsg(irc, "smp: received abort from %s", u->nick); |
---|
1044 | otrl_sm_state_free(context->smstate); |
---|
1045 | /* smp is in back in EXPECT1 */ |
---|
1046 | } |
---|
1047 | } |
---|
1048 | |
---|
1049 | /* helper to assert that account and protocol names given to ops below always |
---|
1050 | match the im_connection passed through as opdata */ |
---|
1051 | struct im_connection *check_imc(void *opdata, const char *accountname, |
---|
1052 | const char *protocol) |
---|
1053 | { |
---|
1054 | struct im_connection *ic = (struct im_connection *)opdata; |
---|
1055 | |
---|
1056 | if (strcmp(accountname, ic->acc->user) != 0) { |
---|
1057 | log_message(LOGLVL_WARNING, |
---|
1058 | "otr: internal account name mismatch: '%s' vs '%s'", |
---|
1059 | accountname, ic->acc->user); |
---|
1060 | } |
---|
1061 | if (strcmp(protocol, ic->acc->prpl->name) != 0) { |
---|
1062 | log_message(LOGLVL_WARNING, |
---|
1063 | "otr: internal protocol name mismatch: '%s' vs '%s'", |
---|
1064 | protocol, ic->acc->prpl->name); |
---|
1065 | } |
---|
1066 | |
---|
1067 | return ic; |
---|
1068 | } |
---|
1069 | |
---|
1070 | user_t *peeruser(irc_t *irc, const char *handle, const char *protocol) |
---|
1071 | { |
---|
1072 | user_t *u; |
---|
1073 | |
---|
1074 | log_message(LOGLVL_DEBUG, "peeruser '%s' '%s'", handle, protocol); |
---|
1075 | |
---|
1076 | for(u=irc->users; u; u=u->next) { |
---|
1077 | struct prpl *prpl; |
---|
1078 | if(!u->ic || !u->handle) |
---|
1079 | continue; |
---|
1080 | prpl = u->ic->acc->prpl; |
---|
1081 | if(strcmp(prpl->name, protocol) == 0 |
---|
1082 | && prpl->handle_cmp(u->handle, handle) == 0) { |
---|
1083 | return u; |
---|
1084 | } |
---|
1085 | } |
---|
1086 | |
---|
1087 | return NULL; |
---|
1088 | } |
---|
1089 | |
---|
1090 | int hexval(char a) |
---|
1091 | { |
---|
1092 | int x=tolower(a); |
---|
1093 | |
---|
1094 | if(x>='a' && x<='f') |
---|
1095 | x = x - 'a' + 10; |
---|
1096 | else if(x>='0' && x<='9') |
---|
1097 | x = x - '0'; |
---|
1098 | else |
---|
1099 | return -1; |
---|
1100 | |
---|
1101 | return x; |
---|
1102 | } |
---|
1103 | |
---|
1104 | const char *peernick(irc_t *irc, const char *handle, const char *protocol) |
---|
1105 | { |
---|
1106 | static char fallback[512]; |
---|
1107 | |
---|
1108 | user_t *u = peeruser(irc, handle, protocol); |
---|
1109 | if(u) { |
---|
1110 | return u->nick; |
---|
1111 | } else { |
---|
1112 | g_snprintf(fallback, 511, "%s/%s", handle, protocol); |
---|
1113 | return fallback; |
---|
1114 | } |
---|
1115 | } |
---|
1116 | |
---|
1117 | int otr_update_modeflags(irc_t *irc, user_t *u) |
---|
1118 | { |
---|
1119 | char *vb = set_getstr(&irc->set, "voice_buddies"); |
---|
1120 | char *hb = set_getstr(&irc->set, "halfop_buddies"); |
---|
1121 | char *ob = set_getstr(&irc->set, "op_buddies"); |
---|
1122 | int encrypted = u->encrypted; |
---|
1123 | int trusted = u->encrypted > 1; |
---|
1124 | char flags[7]; |
---|
1125 | int nflags=0; |
---|
1126 | char *p = flags; |
---|
1127 | char *from; |
---|
1128 | int i; |
---|
1129 | |
---|
1130 | if(!strcmp(vb, "encrypted")) { |
---|
1131 | *(p++) = encrypted ? '+' : '-'; |
---|
1132 | *(p++) = 'v'; |
---|
1133 | nflags++; |
---|
1134 | } else if(!strcmp(vb, "trusted")) { |
---|
1135 | *(p++) = trusted ? '+' : '-'; |
---|
1136 | *(p++) = 'v'; |
---|
1137 | nflags++; |
---|
1138 | } |
---|
1139 | if(!strcmp(hb, "encrypted")) { |
---|
1140 | *(p++) = encrypted ? '+' : '-'; |
---|
1141 | *(p++) = 'h'; |
---|
1142 | nflags++; |
---|
1143 | } else if(!strcmp(hb, "trusted")) { |
---|
1144 | *(p++) = trusted ? '+' : '-'; |
---|
1145 | *(p++) = 'h'; |
---|
1146 | nflags++; |
---|
1147 | } |
---|
1148 | if(!strcmp(ob, "encrypted")) { |
---|
1149 | *(p++) = encrypted ? '+' : '-'; |
---|
1150 | *(p++) = 'o'; |
---|
1151 | nflags++; |
---|
1152 | } else if(!strcmp(ob, "trusted")) { |
---|
1153 | *(p++) = trusted ? '+' : '-'; |
---|
1154 | *(p++) = 'o'; |
---|
1155 | nflags++; |
---|
1156 | } |
---|
1157 | *p = '\0'; |
---|
1158 | |
---|
1159 | p = g_malloc(nflags * (strlen(u->nick)+1) + 1); |
---|
1160 | *p = '\0'; |
---|
1161 | if(!p) |
---|
1162 | return 0; |
---|
1163 | for(i=0; i<nflags; i++) { |
---|
1164 | strcat(p, " "); |
---|
1165 | strcat(p, u->nick); |
---|
1166 | } |
---|
1167 | if(set_getbool(&irc->set, "simulate_netsplit")) |
---|
1168 | from = g_strdup(irc->myhost); |
---|
1169 | else |
---|
1170 | from = g_strdup_printf("%s!%s@%s", irc->mynick, irc->mynick, irc->myhost); |
---|
1171 | irc_write(irc, ":%s MODE %s %s%s", from, irc->channel, flags, p); |
---|
1172 | g_free(from); |
---|
1173 | g_free(p); |
---|
1174 | |
---|
1175 | return 1; |
---|
1176 | } |
---|
1177 | |
---|
1178 | void show_fingerprints(irc_t *irc, ConnContext *ctx) |
---|
1179 | { |
---|
1180 | char human[45]; |
---|
1181 | Fingerprint *fp; |
---|
1182 | const char *trust; |
---|
1183 | int count=0; |
---|
1184 | |
---|
1185 | for(fp=&ctx->fingerprint_root; fp; fp=fp->next) { |
---|
1186 | if(!fp->fingerprint) |
---|
1187 | continue; |
---|
1188 | count++; |
---|
1189 | otrl_privkey_hash_to_human(human, fp->fingerprint); |
---|
1190 | if(!fp->trust || fp->trust[0] == '\0') { |
---|
1191 | trust="untrusted"; |
---|
1192 | } else { |
---|
1193 | trust=fp->trust; |
---|
1194 | } |
---|
1195 | if(fp == ctx->active_fingerprint) { |
---|
1196 | irc_usermsg(irc, " \x02%s (%s)\x02", human, trust); |
---|
1197 | } else { |
---|
1198 | irc_usermsg(irc, " %s (%s)", human, trust); |
---|
1199 | } |
---|
1200 | } |
---|
1201 | if(count==0) |
---|
1202 | irc_usermsg(irc, " no fingerprints"); |
---|
1203 | } |
---|
1204 | |
---|
1205 | Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args) |
---|
1206 | { |
---|
1207 | Fingerprint *fp, *fp2; |
---|
1208 | char human[45]; |
---|
1209 | char prefix[45], *p; |
---|
1210 | int n; |
---|
1211 | int i,j; |
---|
1212 | |
---|
1213 | /* assemble the args into a prefix in standard "human" form */ |
---|
1214 | n=0; |
---|
1215 | p=prefix; |
---|
1216 | for(i=0; args[i]; i++) { |
---|
1217 | for(j=0; args[i][j]; j++) { |
---|
1218 | char c = toupper(args[i][j]); |
---|
1219 | |
---|
1220 | if(n>=40) { |
---|
1221 | irc_usermsg(irc, "too many fingerprint digits given, expected at most 40"); |
---|
1222 | return NULL; |
---|
1223 | } |
---|
1224 | |
---|
1225 | if( (c>='A' && c<='F') || (c>='0' && c<='9') ) { |
---|
1226 | *(p++) = c; |
---|
1227 | } else { |
---|
1228 | irc_usermsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i+1); |
---|
1229 | return NULL; |
---|
1230 | } |
---|
1231 | |
---|
1232 | n++; |
---|
1233 | if(n%8 == 0) |
---|
1234 | *(p++) = ' '; |
---|
1235 | } |
---|
1236 | } |
---|
1237 | *p = '\0'; |
---|
1238 | log_message(LOGLVL_DEBUG, "match_fingerprint '%s'", prefix); |
---|
1239 | log_message(LOGLVL_DEBUG, "n=%d strlen(prefix)=%d", n, strlen(prefix)); |
---|
1240 | |
---|
1241 | /* find first fingerprint with the given prefix */ |
---|
1242 | n = strlen(prefix); |
---|
1243 | for(fp=&ctx->fingerprint_root; fp; fp=fp->next) { |
---|
1244 | if(!fp->fingerprint) |
---|
1245 | continue; |
---|
1246 | otrl_privkey_hash_to_human(human, fp->fingerprint); |
---|
1247 | if(!strncmp(prefix, human, n)) |
---|
1248 | break; |
---|
1249 | } |
---|
1250 | if(!fp) { |
---|
1251 | irc_usermsg(irc, "%s: no match", prefix); |
---|
1252 | return NULL; |
---|
1253 | } |
---|
1254 | |
---|
1255 | /* make sure the match, if any, is unique */ |
---|
1256 | for(fp2=fp->next; fp2; fp2=fp2->next) { |
---|
1257 | if(!fp2->fingerprint) |
---|
1258 | continue; |
---|
1259 | otrl_privkey_hash_to_human(human, fp2->fingerprint); |
---|
1260 | if(!strncmp(prefix, human, n)) |
---|
1261 | break; |
---|
1262 | } |
---|
1263 | if(fp2) { |
---|
1264 | irc_usermsg(irc, "%s: multiple matches", prefix); |
---|
1265 | return NULL; |
---|
1266 | } |
---|
1267 | |
---|
1268 | return fp; |
---|
1269 | } |
---|
1270 | |
---|
1271 | OtrlPrivKey *match_privkey(irc_t *irc, const char **args) |
---|
1272 | { |
---|
1273 | OtrlPrivKey *k, *k2; |
---|
1274 | char human[45]; |
---|
1275 | char prefix[45], *p; |
---|
1276 | int n; |
---|
1277 | int i,j; |
---|
1278 | |
---|
1279 | /* assemble the args into a prefix in standard "human" form */ |
---|
1280 | n=0; |
---|
1281 | p=prefix; |
---|
1282 | for(i=0; args[i]; i++) { |
---|
1283 | for(j=0; args[i][j]; j++) { |
---|
1284 | char c = toupper(args[i][j]); |
---|
1285 | |
---|
1286 | if(n>=40) { |
---|
1287 | irc_usermsg(irc, "too many fingerprint digits given, expected at most 40"); |
---|
1288 | return NULL; |
---|
1289 | } |
---|
1290 | |
---|
1291 | if( (c>='A' && c<='F') || (c>='0' && c<='9') ) { |
---|
1292 | *(p++) = c; |
---|
1293 | } else { |
---|
1294 | irc_usermsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i+1); |
---|
1295 | return NULL; |
---|
1296 | } |
---|
1297 | |
---|
1298 | n++; |
---|
1299 | if(n%8 == 0) |
---|
1300 | *(p++) = ' '; |
---|
1301 | } |
---|
1302 | } |
---|
1303 | *p = '\0'; |
---|
1304 | log_message(LOGLVL_DEBUG, "match_privkey '%s'", prefix); |
---|
1305 | log_message(LOGLVL_DEBUG, "n=%d strlen(prefix)=%d", n, strlen(prefix)); |
---|
1306 | |
---|
1307 | /* find first key which matches the given prefix */ |
---|
1308 | n = strlen(prefix); |
---|
1309 | for(k=irc->otr_us->privkey_root; k; k=k->next) { |
---|
1310 | p = otrl_privkey_fingerprint(irc->otr_us, human, k->accountname, k->protocol); |
---|
1311 | if(!p) /* gah! :-P */ |
---|
1312 | continue; |
---|
1313 | if(!strncmp(prefix, human, n)) |
---|
1314 | break; |
---|
1315 | } |
---|
1316 | if(!k) { |
---|
1317 | irc_usermsg(irc, "%s: no match", prefix); |
---|
1318 | return NULL; |
---|
1319 | } |
---|
1320 | |
---|
1321 | /* make sure the match, if any, is unique */ |
---|
1322 | for(k2=k->next; k2; k2=k2->next) { |
---|
1323 | p = otrl_privkey_fingerprint(irc->otr_us, human, k2->accountname, k2->protocol); |
---|
1324 | if(!p) /* gah! :-P */ |
---|
1325 | continue; |
---|
1326 | if(!strncmp(prefix, human, n)) |
---|
1327 | break; |
---|
1328 | } |
---|
1329 | if(k2) { |
---|
1330 | irc_usermsg(irc, "%s: multiple matches", prefix); |
---|
1331 | return NULL; |
---|
1332 | } |
---|
1333 | |
---|
1334 | return k; |
---|
1335 | } |
---|
1336 | |
---|
1337 | void show_general_otr_info(irc_t *irc) |
---|
1338 | { |
---|
1339 | ConnContext *ctx; |
---|
1340 | OtrlPrivKey *key; |
---|
1341 | char human[45]; |
---|
1342 | |
---|
1343 | /* list all privkeys */ |
---|
1344 | irc_usermsg(irc, "\x1fprivate keys:\x1f"); |
---|
1345 | for(key=irc->otr_us->privkey_root; key; key=key->next) { |
---|
1346 | const char *hash; |
---|
1347 | |
---|
1348 | switch(key->pubkey_type) { |
---|
1349 | case OTRL_PUBKEY_TYPE_DSA: |
---|
1350 | irc_usermsg(irc, " %s/%s - DSA", key->accountname, key->protocol); |
---|
1351 | break; |
---|
1352 | default: |
---|
1353 | irc_usermsg(irc, " %s/%s - type %d", key->accountname, key->protocol, |
---|
1354 | key->pubkey_type); |
---|
1355 | } |
---|
1356 | |
---|
1357 | /* No, it doesn't make much sense to search for the privkey again by |
---|
1358 | account/protocol, but libotr currently doesn't provide a direct routine |
---|
1359 | for hashing a given 'OtrlPrivKey'... */ |
---|
1360 | hash = otrl_privkey_fingerprint(irc->otr_us, human, key->accountname, key->protocol); |
---|
1361 | if(hash) /* should always succeed */ |
---|
1362 | irc_usermsg(irc, " %s", human); |
---|
1363 | } |
---|
1364 | |
---|
1365 | /* list all contexts */ |
---|
1366 | irc_usermsg(irc, "%s", ""); |
---|
1367 | irc_usermsg(irc, "\x1f" "connection contexts:\x1f (bold=currently encrypted)"); |
---|
1368 | for(ctx=irc->otr_us->context_root; ctx; ctx=ctx->next) {\ |
---|
1369 | user_t *u; |
---|
1370 | char *userstring; |
---|
1371 | |
---|
1372 | u = peeruser(irc, ctx->username, ctx->protocol); |
---|
1373 | if(u) |
---|
1374 | userstring = g_strdup_printf("%s/%s/%s (%s)", |
---|
1375 | ctx->username, ctx->protocol, ctx->accountname, u->nick); |
---|
1376 | else |
---|
1377 | userstring = g_strdup_printf("%s/%s/%s", |
---|
1378 | ctx->username, ctx->protocol, ctx->accountname); |
---|
1379 | |
---|
1380 | if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { |
---|
1381 | irc_usermsg(irc, " \x02%s\x02", userstring); |
---|
1382 | } else { |
---|
1383 | irc_usermsg(irc, " %s", userstring); |
---|
1384 | } |
---|
1385 | |
---|
1386 | g_free(userstring); |
---|
1387 | } |
---|
1388 | } |
---|
1389 | |
---|
1390 | void show_otr_context_info(irc_t *irc, ConnContext *ctx) |
---|
1391 | { |
---|
1392 | switch(ctx->otr_offer) { |
---|
1393 | case OFFER_NOT: |
---|
1394 | irc_usermsg(irc, " otr offer status: none sent"); |
---|
1395 | break; |
---|
1396 | case OFFER_SENT: |
---|
1397 | irc_usermsg(irc, " otr offer status: awaiting reply"); |
---|
1398 | break; |
---|
1399 | case OFFER_ACCEPTED: |
---|
1400 | irc_usermsg(irc, " otr offer status: accepted our offer"); |
---|
1401 | break; |
---|
1402 | case OFFER_REJECTED: |
---|
1403 | irc_usermsg(irc, " otr offer status: ignored our offer"); |
---|
1404 | break; |
---|
1405 | default: |
---|
1406 | irc_usermsg(irc, " otr offer status: %d", ctx->otr_offer); |
---|
1407 | } |
---|
1408 | |
---|
1409 | switch(ctx->msgstate) { |
---|
1410 | case OTRL_MSGSTATE_PLAINTEXT: |
---|
1411 | irc_usermsg(irc, " connection state: cleartext"); |
---|
1412 | break; |
---|
1413 | case OTRL_MSGSTATE_ENCRYPTED: |
---|
1414 | irc_usermsg(irc, " connection state: encrypted (v%d)", ctx->protocol_version); |
---|
1415 | break; |
---|
1416 | case OTRL_MSGSTATE_FINISHED: |
---|
1417 | irc_usermsg(irc, " connection state: shut down"); |
---|
1418 | break; |
---|
1419 | default: |
---|
1420 | irc_usermsg(irc, " connection state: %d", ctx->msgstate); |
---|
1421 | } |
---|
1422 | |
---|
1423 | irc_usermsg(irc, " fingerprints: (bold=active)"); |
---|
1424 | show_fingerprints(irc, ctx); |
---|
1425 | } |
---|
1426 | |
---|
1427 | void otr_keygen(irc_t *irc, const char *handle, const char *protocol) |
---|
1428 | { |
---|
1429 | irc_usermsg(irc, "generating new private key for %s/%s...", handle, protocol); |
---|
1430 | |
---|
1431 | /* see if we already have a keygen child running. if not, start one and put a |
---|
1432 | handler on its output. */ |
---|
1433 | if(!irc->otr_keygen || waitpid(irc->otr_keygen, NULL, WNOHANG)) { |
---|
1434 | pid_t p; |
---|
1435 | int to[2], from[2]; |
---|
1436 | FILE *tof, *fromf; |
---|
1437 | |
---|
1438 | if(pipe(to) < 0 || pipe(from) < 0) { |
---|
1439 | irc_usermsg(irc, "otr keygen: couldn't create pipe: %s", strerror(errno)); |
---|
1440 | return; |
---|
1441 | } |
---|
1442 | |
---|
1443 | tof = fdopen(to[1], "w"); |
---|
1444 | fromf = fdopen(from[0], "r"); |
---|
1445 | if(!tof || !fromf) { |
---|
1446 | irc_usermsg(irc, "otr keygen: couldn't streamify pipe: %s", strerror(errno)); |
---|
1447 | return; |
---|
1448 | } |
---|
1449 | |
---|
1450 | p = fork(); |
---|
1451 | if(p<0) { |
---|
1452 | irc_usermsg(irc, "otr keygen: couldn't fork: %s", strerror(errno)); |
---|
1453 | return; |
---|
1454 | } |
---|
1455 | |
---|
1456 | if(!p) { |
---|
1457 | /* child process */ |
---|
1458 | signal(SIGTERM, exit); |
---|
1459 | keygen_child_main(irc->otr_us, to[0], from[1]); |
---|
1460 | exit(0); |
---|
1461 | } |
---|
1462 | |
---|
1463 | irc->otr_keygen = p; |
---|
1464 | irc->otr_to = tof; |
---|
1465 | irc->otr_from = fromf; |
---|
1466 | irc->otr_ntodo = 0; |
---|
1467 | b_input_add(from[0], GAIM_INPUT_READ, keygen_finish_handler, irc); |
---|
1468 | } |
---|
1469 | |
---|
1470 | /* send accountname and protocol for the new key to our keygen slave */ |
---|
1471 | fprintf(irc->otr_to, "%s\n%s\n", handle, protocol); |
---|
1472 | fflush(irc->otr_to); |
---|
1473 | irc->otr_ntodo++; |
---|
1474 | } |
---|
1475 | |
---|
1476 | void keygen_child_main(OtrlUserState us, int infd, int outfd) |
---|
1477 | { |
---|
1478 | FILE *input, *output; |
---|
1479 | char filename[128], accountname[512], protocol[512]; |
---|
1480 | gcry_error_t e; |
---|
1481 | int tempfd; |
---|
1482 | |
---|
1483 | input = fdopen(infd, "r"); |
---|
1484 | output = fdopen(outfd, "w"); |
---|
1485 | |
---|
1486 | while(!feof(input) && !ferror(input) && !feof(output) && !ferror(output)) { |
---|
1487 | myfgets(accountname, 512, input); |
---|
1488 | myfgets(protocol, 512, input); |
---|
1489 | |
---|
1490 | strncpy(filename, "/tmp/bitlbee-XXXXXX", 128); |
---|
1491 | tempfd = mkstemp(filename); |
---|
1492 | close(tempfd); |
---|
1493 | |
---|
1494 | e = otrl_privkey_generate(us, filename, accountname, protocol); |
---|
1495 | if(e) { |
---|
1496 | fprintf(output, "\n"); /* this means failure */ |
---|
1497 | fprintf(output, "otr keygen: %s\n", gcry_strerror(e)); |
---|
1498 | unlink(filename); |
---|
1499 | } else { |
---|
1500 | fprintf(output, "%s\n", filename); |
---|
1501 | fprintf(output, "otr keygen for %s/%s complete\n", accountname, protocol); |
---|
1502 | } |
---|
1503 | fflush(output); |
---|
1504 | } |
---|
1505 | |
---|
1506 | fclose(input); |
---|
1507 | fclose(output); |
---|
1508 | } |
---|
1509 | |
---|
1510 | gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond) |
---|
1511 | { |
---|
1512 | irc_t *irc = (irc_t *)data; |
---|
1513 | char filename[512], msg[512]; |
---|
1514 | |
---|
1515 | log_message(LOGLVL_DEBUG, "keygen_finish_handler cond=%d", cond); |
---|
1516 | |
---|
1517 | myfgets(filename, 512, irc->otr_from); |
---|
1518 | myfgets(msg, 512, irc->otr_from); |
---|
1519 | |
---|
1520 | log_message(LOGLVL_DEBUG, "filename='%s'", filename); |
---|
1521 | log_message(LOGLVL_DEBUG, "msg='%s'", msg); |
---|
1522 | |
---|
1523 | irc_usermsg(irc, "%s", msg); |
---|
1524 | if(filename[0]) { |
---|
1525 | char *kf = g_strdup_printf("%s%s.otr_keys", global.conf->configdir, irc->nick); |
---|
1526 | char *tmp = g_strdup_printf("%s.new", kf); |
---|
1527 | copyfile(filename, tmp); |
---|
1528 | unlink(filename); |
---|
1529 | rename(tmp,kf); |
---|
1530 | otrl_privkey_read(irc->otr_us, kf); |
---|
1531 | g_free(kf); |
---|
1532 | g_free(tmp); |
---|
1533 | } |
---|
1534 | |
---|
1535 | irc->otr_ntodo--; |
---|
1536 | if(irc->otr_ntodo < 1) { |
---|
1537 | /* okay, we think the slave is idle now, so kill him */ |
---|
1538 | log_message(LOGLVL_DEBUG, "all keys done. die, slave!"); |
---|
1539 | fclose(irc->otr_from); |
---|
1540 | fclose(irc->otr_to); |
---|
1541 | kill(irc->otr_keygen, SIGTERM); |
---|
1542 | waitpid(irc->otr_keygen, NULL, 0); |
---|
1543 | irc->otr_keygen = 0; |
---|
1544 | return FALSE; /* unregister ourselves */ |
---|
1545 | } else { |
---|
1546 | return TRUE; /* slave still working, keep watching */ |
---|
1547 | } |
---|
1548 | } |
---|
1549 | |
---|
1550 | void copyfile(const char *a, const char *b) |
---|
1551 | { |
---|
1552 | int fda, fdb; |
---|
1553 | int n; |
---|
1554 | char buf[1024]; |
---|
1555 | |
---|
1556 | log_message(LOGLVL_DEBUG, "copyfile '%s' '%s'", a, b); |
---|
1557 | |
---|
1558 | fda = open(a, O_RDONLY); |
---|
1559 | fdb = open(b, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
---|
1560 | |
---|
1561 | while((n=read(fda, buf, 1024)) > 0) |
---|
1562 | write(fdb, buf, n); |
---|
1563 | |
---|
1564 | close(fda); |
---|
1565 | close(fdb); |
---|
1566 | } |
---|
1567 | |
---|
1568 | void myfgets(char *s, int size, FILE *stream) |
---|
1569 | { |
---|
1570 | if(!fgets(s, size, stream)) { |
---|
1571 | s[0] = '\0'; |
---|
1572 | } else { |
---|
1573 | int n = strlen(s); |
---|
1574 | if(n>0 && s[n-1] == '\n') |
---|
1575 | s[n-1] = '\0'; |
---|
1576 | } |
---|
1577 | } |
---|
1578 | |
---|
1579 | void yes_keygen(gpointer w, void *data) |
---|
1580 | { |
---|
1581 | account_t *acc = (account_t *)data; |
---|
1582 | |
---|
1583 | otr_keygen(acc->irc, acc->user, acc->prpl->name); |
---|
1584 | } |
---|
1585 | |
---|
1586 | |
---|
1587 | #else /* WITH_OTR undefined */ |
---|
1588 | |
---|
1589 | void cmd_otr(irc_t *irc, char **args) |
---|
1590 | { |
---|
1591 | irc_usermsg(irc, "otr: n/a, compiled without OTR support"); |
---|
1592 | } |
---|
1593 | |
---|
1594 | #endif |
---|