source: otr.c @ 4f161e3

Last change on this file since 4f161e3 was eabe6d4, checked in by dequis <dx@…>, at 2015-01-16T19:50:24Z

cmd_otr_keygen: improve argument parsing

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