source: otr.c @ 4dda9e4

Last change on this file since 4dda9e4 was 51f937e, checked in by unknown <pesco@…>, at 2013-08-03T13:36:53Z

log OTR heartbeats if set verbose

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