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