source: otr.c @ 81265e0

Last change on this file since 81265e0 was 81265e0, checked in by Sven M. Hallberg <pesco@…>, at 2013-08-01T15:47:48Z

make otr compile with libotr 4.0.0, minimal functionality

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