source: otr.c @ aea22cd

Last change on this file since aea22cd was aea22cd, checked in by unknown <pesco@…>, at 2011-10-03T15:48:01Z

otr: only skip coloring of leading /me if the message will go to a query window

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