source: otr.c @ 522a00f

Last change on this file since 522a00f was 522a00f, checked in by Sven Moritz Hallberg <sm@…>, at 2008-02-15T09:27:26Z

remove thread-based keygen
replace it with a process-based stub

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