source: otr.c @ fa8c775

Last change on this file since fa8c775 was 5ebff60, checked in by dequis <dx@…>, at 2015-02-20T22:50:54Z

Reindent everything to K&R style with tabs

Used uncrustify, with the configuration file in ./doc/uncrustify.cfg

Commit author set to "Indent <please@…>" so that it's easier to
skip while doing git blame.

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