source: otr.c @ f03a498

Last change on this file since f03a498 was f03a498, checked in by unknown <pesco@…>, at 2011-09-21T20:09:18Z

nix html-escaping in otr plugin

  • Property mode set to 100644
File size: 49.5 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) {
280                irc_usermsg(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) {
285                irc_usermsg(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) {
293                irc_usermsg(irc, "Notice: "
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) {
[522a00f]311                irc_usermsg(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) {
[dc9797f]350                irc_usermsg(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)) {
[3064ea4]353                irc_usermsg(irc, "otr: keygen for %s/%s already in progress", a->user, a->prpl->name);
354                return 0;
[764c7d1]355        } else {
[3064ea4]356                irc_usermsg(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;
367        char *colormsg;
[934db064]368        irc_t *irc = iu->irc;
369        struct im_connection *ic = iu->bu->ic;
[764c7d1]370       
[1dd3470]371        /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */
372        if(ic->acc->prpl->options & OPT_NOOTR) {
[934db064]373                return msg;
[1dd3470]374        }
375       
[0c85c08]376        ignore_msg = otrl_message_receiving(irc->otr->us, &otr_ops, ic,
[934db064]377                ic->acc->user, ic->acc->prpl->name, iu->bu->handle, msg, &newmsg,
[764c7d1]378                &tlvs, NULL, NULL);
379
[934db064]380        otr_handle_smp(ic, iu->bu->handle, tlvs);
[764c7d1]381       
382        if(ignore_msg) {
383                /* this was an internal OTR protocol message */
384                return NULL;
385        } else if(!newmsg) {
386                /* this was a non-OTR message */
387                return g_strdup(msg);
388        } else {
389                /* OTR has processed this message */
[934db064]390                ConnContext *context = otrl_context_find(irc->otr->us, iu->bu->handle,
[764c7d1]391                        ic->acc->user, ic->acc->prpl->name, 0, NULL, NULL, NULL);
[1082395]392
393                if(context && context->msgstate == OTRL_MSGSTATE_ENCRYPTED) {
394                        /* HTML decoding */
395                        /* perform any necessary stripping that the top level would miss */
396                        if(set_getbool(&ic->bee->set, "otr_does_html") &&
397                           !(ic->flags & OPT_DOES_HTML) &&
398                           set_getbool(&ic->bee->set, "strip_html")) {
399                                strip_html(newmsg);
400                        }
401
402                        /* coloring */
403                        if(set_getbool(&ic->bee->set, "otr_color_encrypted")) {
404                                /* color according to f'print trust */
405                                int color;
406                                const char *trust = context->active_fingerprint->trust;
407                                if(trust && trust[0] != '\0')
408                                        color=3;   /* green */
409                                else
410                                        color=5;   /* red */
411
412                                if(newmsg[0] == ',') {
413                                        /* could be a problem with the color code */
414                                        /* insert a space between color spec and message */
415                                        colormsg = g_strdup_printf("\x03%.2d %s\x0F", color, newmsg);
416                                } else {
417                                        colormsg = g_strdup_printf("\x03%.2d%s\x0F", color, newmsg);
418                                }
[fc34fb5]419                        }
[764c7d1]420                } else {
421                        colormsg = g_strdup(newmsg);
422                }
[1082395]423
[764c7d1]424                otrl_message_free(newmsg);
425                return colormsg;
426        }
427}
428
[934db064]429char *otr_filter_msg_out(irc_user_t *iu, char *msg, int flags)
[764c7d1]430{       
[5a71d9c]431        int st;
432        char *otrmsg = NULL;
[f03a498]433        char *emsg = msg;           /* the message as we hand it to libotr */
[5a71d9c]434        ConnContext *ctx = NULL;
[934db064]435        irc_t *irc = iu->irc;
436        struct im_connection *ic = iu->bu->ic;
[1dd3470]437
438        /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */
439        if(ic->acc->prpl->options & OPT_NOOTR) {
[934db064]440                return msg;
[1dd3470]441        }
[1082395]442
[f03a498]443        ctx = otrl_context_find(irc->otr->us,
444                        iu->bu->handle, ic->acc->user, ic->acc->prpl->name,
445                        1, NULL, NULL, NULL);
446
[1082395]447        /* HTML encoding */
448        /* consider OTR plaintext to be HTML if otr_does_html is set */
[f03a498]449        if(ctx && ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED &&
450           set_getbool(&ic->bee->set, "otr_does_html") &&
[1082395]451           (g_strncasecmp(msg, "<html>", 6) != 0)) {
[f03a498]452                emsg = escape_html(msg);
[1082395]453        }
[5a71d9c]454       
[0c85c08]455        st = otrl_message_sending(irc->otr->us, &otr_ops, ic,
[934db064]456                ic->acc->user, ic->acc->prpl->name, iu->bu->handle,
[f03a498]457                emsg, NULL, &otrmsg, NULL, NULL);
458        if(emsg != msg) {
459                g_free(emsg);   /* we're done with this one */
460        }
[764c7d1]461        if(st) {
[934db064]462                return NULL;
[764c7d1]463        }
464
465        if(otrmsg) {
466                if(!ctx) {
467                        otrl_message_free(otrmsg);
[934db064]468                        return NULL;
[764c7d1]469                }
[0c85c08]470                st = otrl_message_fragment_and_send(&otr_ops, ic, ctx,
[764c7d1]471                        otrmsg, OTRL_FRAGMENT_SEND_ALL, NULL);
472                otrl_message_free(otrmsg);
473        } else {
[e2b15bb]474                /* note: otrl_message_sending handles policy, so that if REQUIRE_ENCRYPTION is set,
475                   this case does not occur */
[934db064]476                return msg;
[764c7d1]477        }
478       
[934db064]479        /* TODO: Error reporting should be done here now (if st!=0), probably. */
480       
481        return NULL;
[764c7d1]482}
483
[0c85c08]484static const struct irc_plugin otr_plugin =
485{
486        otr_irc_new,
487        otr_irc_free,
[934db064]488        otr_filter_msg_out,
489        otr_filter_msg_in,
[2dcaf9a]490        otr_load,
491        otr_save,
492        otr_remove,
[0c85c08]493};
494
[8358691]495static void cmd_otr(irc_t *irc, char **args)
[764c7d1]496{
497        const command_t *cmd;
498       
499        if(!args[0])
500                return;
501       
502        if(!args[1])
503                return;
504       
505        for(cmd=otr_commands; cmd->command; cmd++) {
506                if(strcmp(cmd->command, args[1]) == 0)
507                        break;
508        }
509       
510        if(!cmd->command) {
[c595308]511                irc_usermsg(irc, "%s: unknown subcommand \"%s\", see \x02help otr\x02",
[764c7d1]512                        args[0], args[1]);
513                return;
514        }
515       
516        if(!args[cmd->required_parameters+1]) {
517                irc_usermsg(irc, "%s %s: not enough arguments (%d req.)",
518                        args[0], args[1], cmd->required_parameters);
519                return;
520        }
521       
522        cmd->execute(irc, args+1);
523}
524
525
526/*** OTR "MessageAppOps" callbacks for global.otr_ui: ***/
527
528OtrlPolicy op_policy(void *opdata, ConnContext *context)
529{
[e2b15bb]530        struct im_connection *ic = check_imc(opdata, context->accountname, context->protocol);
[ad2d8bc]531        irc_t *irc = ic->bee->ui_data;
[e2b15bb]532        const char *p;
[dc9797f]533       
534        /* policy override during keygen: if we're missing the key for context but are currently
535           generating it, then that's as much as we can do. => temporarily return NEVER. */
[ad2d8bc]536        if(keygen_in_progress(irc, context->accountname, context->protocol) &&
537           !otrl_privkey_find(irc->otr->us, context->accountname, context->protocol))
[dc9797f]538                return OTRL_POLICY_NEVER;
[e2b15bb]539
[ad2d8bc]540        p = set_getstr(&ic->bee->set, "otr_policy");
[e2b15bb]541        if(!strcmp(p, "never"))
542                return OTRL_POLICY_NEVER;
543        if(!strcmp(p, "opportunistic"))
544                return OTRL_POLICY_OPPORTUNISTIC;
545        if(!strcmp(p, "manual"))
546                return OTRL_POLICY_MANUAL;
547        if(!strcmp(p, "always"))
548                return OTRL_POLICY_ALWAYS;
549       
[764c7d1]550        return OTRL_POLICY_OPPORTUNISTIC;
551}
552
553void op_create_privkey(void *opdata, const char *accountname,
554        const char *protocol)
555{
556        struct im_connection *ic = check_imc(opdata, accountname, protocol);
[ad2d8bc]557        irc_t *irc = ic->bee->ui_data;
[764c7d1]558       
[dc9797f]559        /* will fail silently if keygen already in progress */
[ad2d8bc]560        otr_keygen(irc, accountname, protocol);
[764c7d1]561}
562
563int op_is_logged_in(void *opdata, const char *accountname,
564        const char *protocol, const char *recipient)
565{
566        struct im_connection *ic = check_imc(opdata, accountname, protocol);
[ad2d8bc]567        bee_user_t *bu;
[764c7d1]568
[ad2d8bc]569        /* lookup the irc_user_t for the given recipient */
570        bu = bee_user_by_handle(ic->bee, ic, recipient);
571        if(bu) {
572                if(bu->flags & BEE_USER_ONLINE)
[764c7d1]573                        return 1;
574                else
575                        return 0;
576        } else {
577                return -1;
578        }
579}
580
581void op_inject_message(void *opdata, const char *accountname,
582        const char *protocol, const char *recipient, const char *message)
583{
584        struct im_connection *ic = check_imc(opdata, accountname, protocol);
[ad2d8bc]585        irc_t *irc = ic->bee->ui_data;
[764c7d1]586
587        if (strcmp(accountname, recipient) == 0) {
588                /* huh? injecting messages to myself? */
[ad2d8bc]589                irc_usermsg(irc, "note to self: %s", message);
[764c7d1]590        } else {
591                /* need to drop some consts here :-( */
592                /* TODO: get flags into op_inject_message?! */
593                ic->acc->prpl->buddy_msg(ic, (char *)recipient, (char *)message, 0);
594                /* ignoring return value :-/ */
595        }
596}
597
598int op_display_otr_message(void *opdata, const char *accountname,
[5a71d9c]599        const char *protocol, const char *username, const char *message)
[764c7d1]600{
601        struct im_connection *ic = check_imc(opdata, accountname, protocol);
[5a71d9c]602        char *msg = g_strdup(message);
[ad2d8bc]603        irc_t *irc = ic->bee->ui_data;
[764c7d1]604
[5a71d9c]605        strip_html(msg);
[ad2d8bc]606        irc_usermsg(irc, "otr: %s", msg);
[764c7d1]607
[5a71d9c]608        g_free(msg);
[764c7d1]609        return 0;
610}
611
612void op_new_fingerprint(void *opdata, OtrlUserState us,
613        const char *accountname, const char *protocol,
614        const char *username, unsigned char fingerprint[20])
615{
616        struct im_connection *ic = check_imc(opdata, accountname, protocol);
[ad2d8bc]617        irc_t *irc = ic->bee->ui_data;
[764c7d1]618        char hunam[45];         /* anybody looking? ;-) */
619       
620        otrl_privkey_hash_to_human(hunam, fingerprint);
[ad2d8bc]621        irc_usermsg(irc, "new fingerprint for %s: %s",
622                peernick(irc, username, protocol), hunam);
[764c7d1]623}
624
625void op_write_fingerprints(void *opdata)
626{
627        struct im_connection *ic = (struct im_connection *)opdata;
[ad2d8bc]628        irc_t *irc = ic->bee->ui_data;
[764c7d1]629
[ad2d8bc]630        otr_save(irc);
[764c7d1]631}
632
633void op_gone_secure(void *opdata, ConnContext *context)
634{
635        struct im_connection *ic =
636                check_imc(opdata, context->accountname, context->protocol);
[ad2d8bc]637        irc_user_t *u;
638        irc_t *irc = ic->bee->ui_data;
[764c7d1]639
[ad2d8bc]640        u = peeruser(irc, context->username, context->protocol);
[5a71d9c]641        if(!u) {
642                log_message(LOGLVL_ERROR,
[ad2d8bc]643                        "BUG: otr.c: op_gone_secure: irc_user_t for %s/%s/%s not found!",
[8521b02]644                        context->username, context->protocol, context->accountname);
[5a71d9c]645                return;
646        }
[c595308]647       
[a0c6fc5]648        otr_update_uflags(context, u);
[f1cf01c]649        if(!otr_update_modeflags(irc, u)) {
650                char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!";
651                irc_usermsg(irc, "conversation with %s is now off the record (%s)", u->nick, trust);
652        }
[764c7d1]653}
654
655void op_gone_insecure(void *opdata, ConnContext *context)
656{
657        struct im_connection *ic =
658                check_imc(opdata, context->accountname, context->protocol);
[ad2d8bc]659        irc_t *irc = ic->bee->ui_data;
660        irc_user_t *u;
[764c7d1]661
[ad2d8bc]662        u = peeruser(irc, context->username, context->protocol);
[5a71d9c]663        if(!u) {
664                log_message(LOGLVL_ERROR,
[ad2d8bc]665                        "BUG: otr.c: op_gone_insecure: irc_user_t for %s/%s/%s not found!",
[8521b02]666                        context->username, context->protocol, context->accountname);
[5a71d9c]667                return;
668        }
[a0c6fc5]669        otr_update_uflags(context, u);
[ad2d8bc]670        if(!otr_update_modeflags(irc, u))
671                irc_usermsg(irc, "conversation with %s is now in the clear", u->nick);
[764c7d1]672}
673
674void op_still_secure(void *opdata, ConnContext *context, int is_reply)
675{
676        struct im_connection *ic =
677                check_imc(opdata, context->accountname, context->protocol);
[ad2d8bc]678        irc_t *irc = ic->bee->ui_data;
679        irc_user_t *u;
[764c7d1]680
[ad2d8bc]681        u = peeruser(irc, context->username, context->protocol);
[5a71d9c]682        if(!u) {
683                log_message(LOGLVL_ERROR,
[ad2d8bc]684                        "BUG: otr.c: op_still_secure: irc_user_t for %s/%s/%s not found!",
[8521b02]685                        context->username, context->protocol, context->accountname);
[5a71d9c]686                return;
687        }
[a0c6fc5]688
689        otr_update_uflags(context, u);
[f1cf01c]690        if(!otr_update_modeflags(irc, u)) {
691                char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!";
692                irc_usermsg(irc, "otr connection with %s has been refreshed (%s)", u->nick, trust);
693        }
[764c7d1]694}
695
696void op_log_message(void *opdata, const char *message)
697{
[5a71d9c]698        char *msg = g_strdup(message);
699       
700        strip_html(msg);
701        log_message(LOGLVL_INFO, "otr: %s", msg);
702        g_free(msg);
[764c7d1]703}
704
[a13855a]705int op_max_message_size(void *opdata, ConnContext *context)
706{
[5a71d9c]707        struct im_connection *ic =
708                check_imc(opdata, context->accountname, context->protocol);
709
710        return ic->acc->prpl->mms;
[a13855a]711}
712
713const char *op_account_name(void *opdata, const char *account, const char *protocol)
714{
715        struct im_connection *ic = (struct im_connection *)opdata;
[ad2d8bc]716        irc_t *irc = ic->bee->ui_data;
[a13855a]717
[ad2d8bc]718        return peernick(irc, account, protocol);
[a13855a]719}
720
[764c7d1]721
722/*** OTR sub-command handlers ***/
723
[69ef042]724void cmd_otr_reconnect(irc_t *irc, char **args)
725{
726        cmd_otr_disconnect(irc, args);
727        cmd_otr_connect(irc, args);
728}
729
[8521b02]730void cmd_otr_disconnect(irc_t *irc, char **args)
[764c7d1]731{
[ad2d8bc]732        irc_user_t *u;
[764c7d1]733
[ad2d8bc]734        u = irc_user_by_name(irc, args[1]);
735        if(!u || !u->bu || !u->bu->ic) {
[764c7d1]736                irc_usermsg(irc, "%s: unknown user", args[1]);
737                return;
738        }
739       
[0c85c08]740        otrl_message_disconnect(irc->otr->us, &otr_ops,
[ad2d8bc]741                u->bu->ic, u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, u->bu->handle);
[c595308]742       
743        /* for some reason, libotr (3.1.0) doesn't do this itself: */
[ad2d8bc]744        if(u->flags & IRC_USER_OTR_ENCRYPTED) {
[c595308]745                ConnContext *ctx;
[ad2d8bc]746                ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user,
747                        u->bu->ic->acc->prpl->name, 0, NULL, NULL, NULL);
[c595308]748                if(ctx)
[ad2d8bc]749                        op_gone_insecure(u->bu->ic, ctx);
[c595308]750                else /* huh? */
[ad2d8bc]751                        u->flags &= ( IRC_USER_OTR_ENCRYPTED | IRC_USER_OTR_TRUSTED );
[c595308]752        }
[764c7d1]753}
754
[8521b02]755void cmd_otr_connect(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) {
[764c7d1]761                irc_usermsg(irc, "%s: unknown user", args[1]);
762                return;
763        }
[ad2d8bc]764        if(!(u->bu->flags & BEE_USER_ONLINE)) {
[764c7d1]765                irc_usermsg(irc, "%s is offline", args[1]);
766                return;
767        }
768       
[ca0ce03]769        bee_user_msg(irc->b, u->bu, "?OTR?v2?", 0);
[764c7d1]770}
771
[5a71d9c]772void cmd_otr_smp(irc_t *irc, char **args)
[764c7d1]773{
[7c91392]774        otr_smp_or_smpq(irc, args[1], NULL, args[2]);   /* no question */
[99a01b9]775}
[764c7d1]776
[99a01b9]777void cmd_otr_smpq(irc_t *irc, char **args)
778{
[7c91392]779        otr_smp_or_smpq(irc, args[1], args[2], args[3]);
[764c7d1]780}
781
[5a71d9c]782void cmd_otr_trust(irc_t *irc, char **args)
783{
[ad2d8bc]784        irc_user_t *u;
[5a71d9c]785        ConnContext *ctx;
786        unsigned char raw[20];
787        Fingerprint *fp;
788        int i,j;
789       
[ad2d8bc]790        u = irc_user_by_name(irc, args[1]);
791        if(!u || !u->bu || !u->bu->ic) {
[5a71d9c]792                irc_usermsg(irc, "%s: unknown user", args[1]);
793                return;
794        }
795       
[ad2d8bc]796        ctx = otrl_context_find(irc->otr->us, u->bu->handle,
797                u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, 0, NULL, NULL, NULL);
[5a71d9c]798        if(!ctx) {
799                irc_usermsg(irc, "%s: no otr context with user", args[1]);
800                return;
801        }
802       
803        /* convert given fingerprint to raw representation */
804        for(i=0; i<5; i++) {
805                for(j=0; j<4; j++) {
806                        char *p = args[2+i]+(2*j);
807                        char *q = p+1;
808                        int x, y;
809                       
810                        if(!*p || !*q) {
811                                irc_usermsg(irc, "failed: truncated fingerprint block %d", i+1);
812                                return;
813                        }
814                       
815                        x = hexval(*p);
816                        y = hexval(*q);
817                        if(x<0) {
818                                irc_usermsg(irc, "failed: %d. hex digit of block %d out of range", 2*j+1, i+1);
819                                return;
820                        }
821                        if(y<0) {
822                                irc_usermsg(irc, "failed: %d. hex digit of block %d out of range", 2*j+2, i+1);
823                                return;
824                        }
825
826                        raw[i*4+j] = x*16 + y;
827                }
828        }
829        fp = otrl_context_find_fingerprint(ctx, raw, 0, NULL);
830        if(!fp) {
831                irc_usermsg(irc, "failed: no such fingerprint for %s", args[1]);
832        } else {
833                char *trust = args[7] ? args[7] : "affirmed";
834                otrl_context_set_trust(fp, trust);
835                irc_usermsg(irc, "fingerprint match, trust set to \"%s\"", trust);
[ad2d8bc]836                if(u->flags & IRC_USER_OTR_ENCRYPTED)
837                        u->flags |= IRC_USER_OTR_TRUSTED;
[5a71d9c]838                otr_update_modeflags(irc, u);
839        }
840}
841
[8521b02]842void cmd_otr_info(irc_t *irc, char **args)
[764c7d1]843{
[8521b02]844        if(!args[1]) {
845                show_general_otr_info(irc);
[764c7d1]846        } else {
[8521b02]847                char *arg = g_strdup(args[1]);
[9730d72]848                char *myhandle, *handle=NULL, *protocol;
[764c7d1]849                ConnContext *ctx;
[8521b02]850               
851                /* interpret arg as 'user/protocol/account' if possible */
852                protocol = strchr(arg, '/');
[9e64011]853                myhandle = NULL;
[8521b02]854                if(protocol) {
855                        *(protocol++) = '\0';
856                        myhandle = strchr(protocol, '/');
[764c7d1]857                }
[8521b02]858                if(protocol && myhandle) {
859                        *(myhandle++) = '\0';
860                        handle = arg;
[dc9797f]861                        ctx = otrl_context_find(irc->otr->us, handle, myhandle, protocol, 0, NULL, NULL, NULL);
[8521b02]862                        if(!ctx) {
[c595308]863                                irc_usermsg(irc, "no such context");
[8521b02]864                                g_free(arg);
865                                return;
866                        }
[764c7d1]867                } else {
[ad2d8bc]868                        irc_user_t *u = irc_user_by_name(irc, args[1]);
869                        if(!u || !u->bu || !u->bu->ic) {
[8521b02]870                                irc_usermsg(irc, "%s: unknown user", args[1]);
871                                g_free(arg);
872                                return;
[5a71d9c]873                        }
[ad2d8bc]874                        ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user,
875                                u->bu->ic->acc->prpl->name, 0, NULL, NULL, NULL);
[8521b02]876                        if(!ctx) {
877                                irc_usermsg(irc, "no otr context with %s", args[1]);
878                                g_free(arg);
879                                return;
880                        }
881                }
882       
883                /* show how we resolved the (nick) argument, if we did */
884                if(handle!=arg) {
885                        irc_usermsg(irc, "%s is %s/%s; we are %s/%s to them", args[1],
886                                ctx->username, ctx->protocol, ctx->accountname, ctx->protocol);
[764c7d1]887                }
[8521b02]888                show_otr_context_info(irc, ctx);
889                g_free(arg);
[764c7d1]890        }
891}
892
[94e7eb3]893void cmd_otr_keygen(irc_t *irc, char **args)
894{
895        int i, n;
896        account_t *a;
897       
898        n = atoi(args[1]);
899        if(n<0 || (!n && strcmp(args[1], "0"))) {
900                irc_usermsg(irc, "%s: invalid account number", args[1]);
901                return;
902        }
903       
[ad2d8bc]904        a = irc->b->accounts;
[94e7eb3]905        for(i=0; i<n && a; i++, a=a->next);
906        if(!a) {
907                irc_usermsg(irc, "%s: no such account", args[1]);
908                return;
909        }
910       
[dc9797f]911        if(keygen_in_progress(irc, a->user, a->prpl->name)) {
912                irc_usermsg(irc, "keygen for account %d already in progress", n);
913                return;
914        }
915       
916        if(otrl_privkey_find(irc->otr->us, a->user, a->prpl->name)) {
[94e7eb3]917                char *s = g_strdup_printf("account %d already has a key, replace it?", n);
[ad2d8bc]918                query_add(irc, NULL, s, yes_keygen, NULL, NULL, a);
[82e8fe8]919                g_free(s);
[94e7eb3]920        } else {
921                otr_keygen(irc, a->user, a->prpl->name);
922        }
923}
924
[6738a67]925void yes_forget_fingerprint(void *data)
[c595308]926{
[6738a67]927        pair_t *p = (pair_t *)data;
928        irc_t *irc = (irc_t *)p->fst;
929        Fingerprint *fp = (Fingerprint *)p->snd;
930
931        g_free(p);
[c595308]932       
933        if(fp == fp->context->active_fingerprint) {
[5f4eede]934                irc_usermsg(irc, "that fingerprint is active, terminate otr connection first");
[c595308]935                return;
936        }
937               
938        otrl_context_forget_fingerprint(fp, 0);
939}
940
[6738a67]941void yes_forget_context(void *data)
[c595308]942{
[6738a67]943        pair_t *p = (pair_t *)data;
944        irc_t *irc = (irc_t *)p->fst;
945        ConnContext *ctx = (ConnContext *)p->snd;
946
947        g_free(p);
[c595308]948       
949        if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) {
[5f4eede]950                irc_usermsg(irc, "active otr connection with %s, terminate it first",
951                        peernick(irc, ctx->username, ctx->protocol));
[c595308]952                return;
953        }
954               
955        if(ctx->msgstate == OTRL_MSGSTATE_FINISHED)
956                otrl_context_force_plaintext(ctx);
957        otrl_context_forget(ctx);
958}
959
[6738a67]960void yes_forget_key(void *data)
[d0faf62]961{
962        OtrlPrivKey *key = (OtrlPrivKey *)data;
963       
964        otrl_privkey_forget(key);
[37bff51]965        /* Hm, libotr doesn't seem to offer a function for explicitly /writing/
966           keyfiles. So the key will be back on the next load... */
967        /* TODO: Actually erase forgotten keys from storage? */
[d0faf62]968}
969
[c595308]970void cmd_otr_forget(irc_t *irc, char **args)
971{
972        if(!strcmp(args[1], "fingerprint"))
973        {
[ad2d8bc]974                irc_user_t *u;
[c595308]975                ConnContext *ctx;
976                Fingerprint *fp;
977                char human[54];
978                char *s;
[6738a67]979                pair_t *p;
[c595308]980               
981                if(!args[3]) {
982                        irc_usermsg(irc, "otr %s %s: not enough arguments (2 req.)", args[0], args[1]);
983                        return;
984                }
985               
[e2b15bb]986                /* TODO: allow context specs ("user/proto/account") in 'otr forget fingerprint'? */
[ad2d8bc]987                u = irc_user_by_name(irc, args[2]);
988                if(!u || !u->bu || !u->bu->ic) {
[c595308]989                        irc_usermsg(irc, "%s: unknown user", args[2]);
990                        return;
991                }
992               
[ad2d8bc]993                ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user,
994                        u->bu->ic->acc->prpl->name, 0, NULL, NULL, NULL);
[c595308]995                if(!ctx) {
996                        irc_usermsg(irc, "no otr context with %s", args[2]);
997                        return;
998                }
999               
1000                fp = match_fingerprint(irc, ctx, ((const char **)args)+3);
1001                if(!fp) {
1002                        /* match_fingerprint does error messages */
1003                        return;
1004                }
1005               
1006                if(fp == ctx->active_fingerprint) {
1007                        irc_usermsg(irc, "that fingerprint is active, terminate otr connection first");
1008                        return;
1009                }
1010               
1011                otrl_privkey_hash_to_human(human, fp->fingerprint);
1012                s = g_strdup_printf("about to forget fingerprint %s, are you sure?", human);
[6738a67]1013                p = g_malloc(sizeof(pair_t));
1014                if(!p)
1015                        return;
1016                p->fst = irc;
1017                p->snd = fp;
[ad2d8bc]1018                query_add(irc, NULL, s, yes_forget_fingerprint, NULL, NULL, p);
[82e8fe8]1019                g_free(s);
[c595308]1020        }
1021       
1022        else if(!strcmp(args[1], "context"))
1023        {
[ad2d8bc]1024                irc_user_t *u;
[c595308]1025                ConnContext *ctx;
1026                char *s;
[6738a67]1027                pair_t *p;
[c595308]1028               
[e2b15bb]1029                /* TODO: allow context specs ("user/proto/account") in 'otr forget contex'? */
[ad2d8bc]1030                u = irc_user_by_name(irc, args[2]);
1031                if(!u || !u->bu || !u->bu->ic) {
[c595308]1032                        irc_usermsg(irc, "%s: unknown user", args[2]);
1033                        return;
1034                }
1035               
[ad2d8bc]1036                ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user,
1037                        u->bu->ic->acc->prpl->name, 0, NULL, NULL, NULL);
[c595308]1038                if(!ctx) {
1039                        irc_usermsg(irc, "no otr context with %s", args[2]);
1040                        return;
1041                }
1042               
1043                if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) {
1044                        irc_usermsg(irc, "active otr connection with %s, terminate it first", args[2]);
1045                        return;
1046                }
1047               
1048                s = g_strdup_printf("about to forget otr data about %s, are you sure?", args[2]);
[6738a67]1049                p = g_malloc(sizeof(pair_t));
1050                if(!p)
1051                        return;
1052                p->fst = irc;
1053                p->snd = ctx;
[ad2d8bc]1054                query_add(irc, NULL, s, yes_forget_context, NULL, NULL, p);
[82e8fe8]1055                g_free(s);
[c595308]1056        }
1057       
[d0faf62]1058        else if(!strcmp(args[1], "key"))
1059        {
1060                OtrlPrivKey *key;
1061                char *s;
1062               
1063                key = match_privkey(irc, ((const char **)args)+2);
1064                if(!key) {
1065                        /* match_privkey does error messages */
1066                        return;
1067                }
1068               
1069                s = g_strdup_printf("about to forget the private key for %s/%s, are you sure?",
1070                        key->accountname, key->protocol);
[ad2d8bc]1071                query_add(irc, NULL, s, yes_forget_key, NULL, NULL, key);
[d0faf62]1072                g_free(s);
1073        }
1074       
[c595308]1075        else
1076        {
1077                irc_usermsg(irc, "otr %s: unknown subcommand \"%s\", see \x02help otr forget\x02",
1078                        args[0], args[1]);
1079        }
1080}
1081
[764c7d1]1082
1083/*** local helpers / subroutines: ***/
1084
1085/* Socialist Millionaires' Protocol */
1086void otr_handle_smp(struct im_connection *ic, const char *handle, OtrlTLV *tlvs)
1087{
[ad2d8bc]1088        irc_t *irc = ic->bee->ui_data;
[dc9797f]1089        OtrlUserState us = irc->otr->us;
[0c85c08]1090        OtrlMessageAppOps *ops = &otr_ops;
[764c7d1]1091        OtrlTLV *tlv = NULL;
1092        ConnContext *context;
1093        NextExpectedSMP nextMsg;
[ad2d8bc]1094        irc_user_t *u;
1095        bee_user_t *bu;
[764c7d1]1096
[ad2d8bc]1097        bu = bee_user_by_handle(ic->bee, ic, handle);
1098        if(!bu || !(u = bu->ui_data)) return;
[764c7d1]1099        context = otrl_context_find(us, handle,
1100                ic->acc->user, ic->acc->prpl->name, 1, NULL, NULL, NULL);
[3c80a9d]1101        if(!context) {
1102                /* huh? out of memory or what? */
[0e078a7]1103                irc_usermsg(irc, "smp: failed to get otr context for %s", u->nick);
1104                otrl_message_abort_smp(us, ops, u->bu->ic, context);
1105                otrl_sm_state_free(context->smstate);
[3c80a9d]1106                return;
1107        }
[764c7d1]1108        nextMsg = context->smstate->nextExpected;
1109
[f26db4a8]1110        if (context->smstate->sm_prog_state == OTRL_SMP_PROG_CHEATED) {
1111                irc_usermsg(irc, "smp %s: opponent violated protocol, aborting",
1112                        u->nick);
1113                otrl_message_abort_smp(us, ops, u->bu->ic, context);
1114                otrl_sm_state_free(context->smstate);
1115                return;
1116        }
1117
[76e2f62]1118        tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP1Q);
1119        if (tlv) {
1120                if (nextMsg != OTRL_SMP_EXPECT1) {
1121                        irc_usermsg(irc, "smp %s: spurious SMP1Q received, aborting", u->nick);
1122                        otrl_message_abort_smp(us, ops, u->bu->ic, context);
1123                        otrl_sm_state_free(context->smstate);
1124                } else {
1125                        char *question = g_strndup((char *)tlv->data, tlv->len);
[faa75c0e]1126                        irc_usermsg(irc, "smp: initiated by %s with question: \x02\"%s\"\x02", u->nick,
[76e2f62]1127                                question);
1128                        irc_usermsg(irc, "smp: respond with \x02otr smp %s <answer>\x02",
1129                                u->nick);
1130                        g_free(question);
1131                        /* smp stays in EXPECT1 until user responds */
1132                }
1133        }
[764c7d1]1134        tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP1);
1135        if (tlv) {
1136                if (nextMsg != OTRL_SMP_EXPECT1) {
1137                        irc_usermsg(irc, "smp %s: spurious SMP1 received, aborting", u->nick);
[ad2d8bc]1138                        otrl_message_abort_smp(us, ops, u->bu->ic, context);
[764c7d1]1139                        otrl_sm_state_free(context->smstate);
1140                } else {
1141                        irc_usermsg(irc, "smp: initiated by %s"
1142                                " - respond with \x02otr smp %s <secret>\x02",
1143                                u->nick, u->nick);
1144                        /* smp stays in EXPECT1 until user responds */
1145                }
1146        }
1147        tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP2);
1148        if (tlv) {
1149                if (nextMsg != OTRL_SMP_EXPECT2) {
1150                        irc_usermsg(irc, "smp %s: spurious SMP2 received, aborting", u->nick);
[ad2d8bc]1151                        otrl_message_abort_smp(us, ops, u->bu->ic, context);
[764c7d1]1152                        otrl_sm_state_free(context->smstate);
1153                } else {
1154                        /* SMP2 received, otrl_message_receiving will have sent SMP3 */
1155                        context->smstate->nextExpected = OTRL_SMP_EXPECT4;
1156                }
1157        }
1158        tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP3);
1159        if (tlv) {
1160                if (nextMsg != OTRL_SMP_EXPECT3) {
1161                        irc_usermsg(irc, "smp %s: spurious SMP3 received, aborting", u->nick);
[ad2d8bc]1162                        otrl_message_abort_smp(us, ops, u->bu->ic, context);
[764c7d1]1163                        otrl_sm_state_free(context->smstate);
1164                } else {
[3ad8036]1165                        /* SMP3 received, otrl_message_receiving will have sent SMP4 */
[944d7a5]1166                        if(context->smstate->sm_prog_state == OTRL_SMP_PROG_SUCCEEDED) {
[3ad8036]1167                                if(context->smstate->received_question) {
1168                                        irc_usermsg(irc, "smp %s: correct answer, you are trusted",
1169                                                u->nick);
1170                                } else {
1171                                        irc_usermsg(irc, "smp %s: secrets proved equal, fingerprint trusted",
1172                                                u->nick);
1173                                }
[764c7d1]1174                        } else {
[3ad8036]1175                                if(context->smstate->received_question) {
1176                                        irc_usermsg(irc, "smp %s: wrong answer, you are not trusted",
1177                                                u->nick);
1178                                } else {
1179                                        irc_usermsg(irc, "smp %s: secrets did not match, fingerprint not trusted",
1180                                                u->nick);
1181                                }
[764c7d1]1182                        }
1183                        otrl_sm_state_free(context->smstate);
1184                        /* smp is in back in EXPECT1 */
1185                }
1186        }
1187        tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP4);
1188        if (tlv) {
1189                if (nextMsg != OTRL_SMP_EXPECT4) {
1190                        irc_usermsg(irc, "smp %s: spurious SMP4 received, aborting", u->nick);
[ad2d8bc]1191                        otrl_message_abort_smp(us, ops, u->bu->ic, context);
[764c7d1]1192                        otrl_sm_state_free(context->smstate);
1193                } else {
1194                        /* SMP4 received, otrl_message_receiving will have set fp trust */
[944d7a5]1195                        if(context->smstate->sm_prog_state == OTRL_SMP_PROG_SUCCEEDED) {
1196                                irc_usermsg(irc, "smp %s: secrets proved equal, fingerprint trusted",
[764c7d1]1197                                        u->nick);
1198                        } else {
[944d7a5]1199                                irc_usermsg(irc, "smp %s: secrets did not match, fingerprint not trusted",
[764c7d1]1200                                        u->nick);
1201                        }
1202                        otrl_sm_state_free(context->smstate);
1203                        /* smp is in back in EXPECT1 */
1204                }
1205        }
1206        tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP_ABORT);
1207        if (tlv) {
1208                irc_usermsg(irc, "smp: received abort from %s", u->nick);
1209                otrl_sm_state_free(context->smstate);
1210                /* smp is in back in EXPECT1 */
1211        }
1212}
1213
[99a01b9]1214/* combined handler for the 'otr smp' and 'otr smpq' commands */
[7c91392]1215void otr_smp_or_smpq(irc_t *irc, const char *nick, const char *question,
[99a01b9]1216                const char *secret)
1217{
1218        irc_user_t *u;
1219        ConnContext *ctx;
1220
1221        u = irc_user_by_name(irc, nick);
1222        if(!u || !u->bu || !u->bu->ic) {
1223                irc_usermsg(irc, "%s: unknown user", nick);
1224                return;
1225        }
1226        if(!(u->bu->flags & BEE_USER_ONLINE)) {
1227                irc_usermsg(irc, "%s is offline", nick);
1228                return;
1229        }
1230       
1231        ctx = otrl_context_find(irc->otr->us, u->bu->handle,
[0e078a7]1232                u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, 0, NULL, NULL, NULL);
1233        if(!ctx || ctx->msgstate != OTRL_MSGSTATE_ENCRYPTED) {
1234                irc_usermsg(irc, "smp: otr inactive with %s, try \x02otr connect"
1235                                " %s\x02", nick, nick);
[99a01b9]1236                return;
1237        }
1238
1239        if(ctx->smstate->nextExpected != OTRL_SMP_EXPECT1) {
1240                log_message(LOGLVL_INFO,
1241                        "SMP already in phase %d, sending abort before reinitiating",
1242                        ctx->smstate->nextExpected+1);
1243                otrl_message_abort_smp(irc->otr->us, &otr_ops, u->bu->ic, ctx);
1244                otrl_sm_state_free(ctx->smstate);
1245        }
[944d7a5]1246
[9cc653c]1247        if(question) {
1248                /* this was 'otr smpq', just initiate */
[99a01b9]1249                irc_usermsg(irc, "smp: initiating with %s...", u->nick);
[9cc653c]1250                otrl_message_initiate_smp_q(irc->otr->us, &otr_ops, u->bu->ic, ctx,
1251                        question, (unsigned char *)secret, strlen(secret));
1252                /* smp is now in EXPECT2 */
1253        } else {
1254                /* this was 'otr smp', initiate or reply */
1255                /* warning: the following assumes that smstates are cleared whenever an SMP
1256                   is completed or aborted! */ 
1257                if(ctx->smstate->secret == NULL) {
1258                        irc_usermsg(irc, "smp: initiating with %s...", u->nick);
[99a01b9]1259                        otrl_message_initiate_smp(irc->otr->us, &otr_ops,
1260                                u->bu->ic, ctx, (unsigned char *)secret, strlen(secret));
[9cc653c]1261                        /* smp is now in EXPECT2 */
1262                } else {
1263                        /* if we're still in EXPECT1 but smstate is initialized, we must have
1264                           received the SMP1, so let's issue a response */
1265                        irc_usermsg(irc, "smp: responding to %s...", u->nick);
1266                        otrl_message_respond_smp(irc->otr->us, &otr_ops,
1267                                u->bu->ic, ctx, (unsigned char *)secret, strlen(secret));
1268                        /* smp is now in EXPECT3 */
[99a01b9]1269                }
1270        }
1271}
1272
[764c7d1]1273/* helper to assert that account and protocol names given to ops below always
1274   match the im_connection passed through as opdata */
1275struct im_connection *check_imc(void *opdata, const char *accountname,
1276        const char *protocol)
1277{
1278        struct im_connection *ic = (struct im_connection *)opdata;
1279
1280        if (strcmp(accountname, ic->acc->user) != 0) {
1281                log_message(LOGLVL_WARNING,
1282                        "otr: internal account name mismatch: '%s' vs '%s'",
1283                        accountname, ic->acc->user);
1284        }
1285        if (strcmp(protocol, ic->acc->prpl->name) != 0) {
1286                log_message(LOGLVL_WARNING,
1287                        "otr: internal protocol name mismatch: '%s' vs '%s'",
1288                        protocol, ic->acc->prpl->name);
1289        }
1290       
1291        return ic;
1292}
1293
[ad2d8bc]1294irc_user_t *peeruser(irc_t *irc, const char *handle, const char *protocol)
[764c7d1]1295{
[ad2d8bc]1296        GSList *l;
[764c7d1]1297       
[ad2d8bc]1298        for(l=irc->b->users; l; l = l->next) {
1299                bee_user_t *bu = l->data;
[764c7d1]1300                struct prpl *prpl;
[ad2d8bc]1301                if(!bu->ui_data || !bu->ic || !bu->handle)
[8521b02]1302                        continue;
[ad2d8bc]1303                prpl = bu->ic->acc->prpl;
[764c7d1]1304                if(strcmp(prpl->name, protocol) == 0
[ad2d8bc]1305                        && prpl->handle_cmp(bu->handle, handle) == 0) {
1306                        return bu->ui_data;
[764c7d1]1307                }
1308        }
1309       
[5a71d9c]1310        return NULL;
1311}
1312
[8521b02]1313int hexval(char a)
1314{
1315        int x=tolower(a);
1316       
1317        if(x>='a' && x<='f')
1318                x = x - 'a' + 10;
1319        else if(x>='0' && x<='9')
1320                x = x - '0';
1321        else
1322                return -1;
1323       
1324        return x;
1325}
1326
[5a71d9c]1327const char *peernick(irc_t *irc, const char *handle, const char *protocol)
1328{
1329        static char fallback[512];
1330       
[ad2d8bc]1331        irc_user_t *u = peeruser(irc, handle, protocol);
[5a71d9c]1332        if(u) {
1333                return u->nick;
1334        } else {
1335                g_snprintf(fallback, 511, "%s/%s", handle, protocol);
1336                return fallback;
1337        }
1338}
1339
[a0c6fc5]1340void otr_update_uflags(ConnContext *context, irc_user_t *u)
1341{
1342        const char *trust;
1343
1344        if(context->active_fingerprint) {
1345                u->flags |= IRC_USER_OTR_ENCRYPTED;
1346
1347                trust = context->active_fingerprint->trust;
1348                if(trust && trust[0])
1349                        u->flags |= IRC_USER_OTR_TRUSTED;
1350                else
1351                        u->flags &= ~IRC_USER_OTR_TRUSTED;
1352        } else {
1353                u->flags &= ~IRC_USER_OTR_ENCRYPTED;
1354        }
1355}
1356
[ad2d8bc]1357int otr_update_modeflags(irc_t *irc, irc_user_t *u)
[5a71d9c]1358{
[1dc00fe]1359        return 0;
[764c7d1]1360}
1361
1362void show_fingerprints(irc_t *irc, ConnContext *ctx)
1363{
1364        char human[45];
1365        Fingerprint *fp;
1366        const char *trust;
1367        int count=0;
1368       
1369        for(fp=&ctx->fingerprint_root; fp; fp=fp->next) {
1370                if(!fp->fingerprint)
1371                        continue;
1372                count++;
1373                otrl_privkey_hash_to_human(human, fp->fingerprint);
1374                if(!fp->trust || fp->trust[0] == '\0') {
1375                        trust="untrusted";
1376                } else {
1377                        trust=fp->trust;
1378                }
1379                if(fp == ctx->active_fingerprint) {
[fd9fa52]1380                        irc_usermsg(irc, "    \x02%s (%s)\x02", human, trust);
[764c7d1]1381                } else {
[fd9fa52]1382                        irc_usermsg(irc, "    %s (%s)", human, trust);
[764c7d1]1383                }
1384        }
1385        if(count==0)
[fd9fa52]1386                irc_usermsg(irc, "    (none)");
[8521b02]1387}
1388
[c595308]1389Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args)
1390{
1391        Fingerprint *fp, *fp2;
1392        char human[45];
1393        char prefix[45], *p;
1394        int n;
1395        int i,j;
1396       
1397        /* assemble the args into a prefix in standard "human" form */
1398        n=0;
1399        p=prefix;
1400        for(i=0; args[i]; i++) {
1401                for(j=0; args[i][j]; j++) {
1402                        char c = toupper(args[i][j]);
1403                       
1404                        if(n>=40) {
1405                                irc_usermsg(irc, "too many fingerprint digits given, expected at most 40");
1406                                return NULL;
1407                        }
1408                       
1409                        if( (c>='A' && c<='F') || (c>='0' && c<='9') ) {
1410                                *(p++) = c;
1411                        } else {
1412                                irc_usermsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i+1);
1413                                return NULL;
1414                        }
1415                       
1416                        n++;
1417                        if(n%8 == 0)
1418                                *(p++) = ' ';
1419                }
1420        }
1421        *p = '\0';
1422       
1423        /* find first fingerprint with the given prefix */
1424        n = strlen(prefix);
1425        for(fp=&ctx->fingerprint_root; fp; fp=fp->next) {
1426                if(!fp->fingerprint)
1427                        continue;
1428                otrl_privkey_hash_to_human(human, fp->fingerprint);
1429                if(!strncmp(prefix, human, n))
1430                        break;
1431        }
1432        if(!fp) {
1433                irc_usermsg(irc, "%s: no match", prefix);
1434                return NULL;
1435        }
1436       
1437        /* make sure the match, if any, is unique */
1438        for(fp2=fp->next; fp2; fp2=fp2->next) {
1439                if(!fp2->fingerprint)
1440                        continue;
1441                otrl_privkey_hash_to_human(human, fp2->fingerprint);
1442                if(!strncmp(prefix, human, n))
1443                        break;
1444        }
1445        if(fp2) {
1446                irc_usermsg(irc, "%s: multiple matches", prefix);
1447                return NULL;
1448        }
1449       
1450        return fp;
1451}
1452
[5f4eede]1453OtrlPrivKey *match_privkey(irc_t *irc, const char **args)
1454{
1455        OtrlPrivKey *k, *k2;
1456        char human[45];
1457        char prefix[45], *p;
1458        int n;
1459        int i,j;
1460       
1461        /* assemble the args into a prefix in standard "human" form */
1462        n=0;
1463        p=prefix;
1464        for(i=0; args[i]; i++) {
1465                for(j=0; args[i][j]; j++) {
1466                        char c = toupper(args[i][j]);
1467                       
1468                        if(n>=40) {
1469                                irc_usermsg(irc, "too many fingerprint digits given, expected at most 40");
1470                                return NULL;
1471                        }
1472                       
1473                        if( (c>='A' && c<='F') || (c>='0' && c<='9') ) {
1474                                *(p++) = c;
1475                        } else {
1476                                irc_usermsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i+1);
1477                                return NULL;
1478                        }
1479                       
1480                        n++;
1481                        if(n%8 == 0)
1482                                *(p++) = ' ';
1483                }
1484        }
1485        *p = '\0';
1486       
1487        /* find first key which matches the given prefix */
1488        n = strlen(prefix);
[dc9797f]1489        for(k=irc->otr->us->privkey_root; k; k=k->next) {
1490                p = otrl_privkey_fingerprint(irc->otr->us, human, k->accountname, k->protocol);
[5f4eede]1491                if(!p) /* gah! :-P */
1492                        continue;
1493                if(!strncmp(prefix, human, n))
1494                        break;
1495        }
1496        if(!k) {
1497                irc_usermsg(irc, "%s: no match", prefix);
1498                return NULL;
1499        }
1500       
1501        /* make sure the match, if any, is unique */
1502        for(k2=k->next; k2; k2=k2->next) {
[dc9797f]1503                p = otrl_privkey_fingerprint(irc->otr->us, human, k2->accountname, k2->protocol);
[5f4eede]1504                if(!p) /* gah! :-P */
1505                        continue;
1506                if(!strncmp(prefix, human, n))
1507                        break;
1508        }
1509        if(k2) {
1510                irc_usermsg(irc, "%s: multiple matches", prefix);
1511                return NULL;
1512        }
1513       
1514        return k;
1515}
1516
[8521b02]1517void show_general_otr_info(irc_t *irc)
1518{
1519        ConnContext *ctx;
1520        OtrlPrivKey *key;
1521        char human[45];
[3064ea4]1522        kg_t *kg;
[8521b02]1523
[3064ea4]1524        /* list all privkeys (including ones being generated) */
[8521b02]1525        irc_usermsg(irc, "\x1fprivate keys:\x1f");
[dc9797f]1526        for(key=irc->otr->us->privkey_root; key; key=key->next) {
[8521b02]1527                const char *hash;
1528               
1529                switch(key->pubkey_type) {
1530                case OTRL_PUBKEY_TYPE_DSA:
1531                        irc_usermsg(irc, "  %s/%s - DSA", key->accountname, key->protocol);
1532                        break;
1533                default:
1534                        irc_usermsg(irc, "  %s/%s - type %d", key->accountname, key->protocol,
1535                                key->pubkey_type);
1536                }
1537
1538                /* No, it doesn't make much sense to search for the privkey again by
1539                   account/protocol, but libotr currently doesn't provide a direct routine
1540                   for hashing a given 'OtrlPrivKey'... */
[dc9797f]1541                hash = otrl_privkey_fingerprint(irc->otr->us, human, key->accountname, key->protocol);
[8521b02]1542                if(hash) /* should always succeed */
1543                        irc_usermsg(irc, "    %s", human);
1544        }
[1221ef0]1545        if(irc->otr->sent_accountname) {
1546                irc_usermsg(irc, "  %s/%s - DSA", irc->otr->sent_accountname,
1547                        irc->otr->sent_protocol);
1548                irc_usermsg(irc, "    (being generated)");
1549        }
[3064ea4]1550        for(kg=irc->otr->todo; kg; kg=kg->next) {
1551                irc_usermsg(irc, "  %s/%s - DSA", kg->accountname, kg->protocol);
[1221ef0]1552                irc_usermsg(irc, "    (queued)");
[3064ea4]1553        }
[1221ef0]1554        if(key == irc->otr->us->privkey_root &&
1555           !irc->otr->sent_accountname &&
1556           kg == irc->otr->todo)
[3064ea4]1557                irc_usermsg(irc, "  (none)");
[8521b02]1558
1559        /* list all contexts */
1560        irc_usermsg(irc, "%s", "");
1561        irc_usermsg(irc, "\x1f" "connection contexts:\x1f (bold=currently encrypted)");
[dc9797f]1562        for(ctx=irc->otr->us->context_root; ctx; ctx=ctx->next) {\
[ad2d8bc]1563                irc_user_t *u;
[8521b02]1564                char *userstring;
1565               
1566                u = peeruser(irc, ctx->username, ctx->protocol);
1567                if(u)
1568                        userstring = g_strdup_printf("%s/%s/%s (%s)",
1569                                ctx->username, ctx->protocol, ctx->accountname, u->nick);
1570                else
1571                        userstring = g_strdup_printf("%s/%s/%s",
1572                                ctx->username, ctx->protocol, ctx->accountname);
1573               
1574                if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) {
1575                        irc_usermsg(irc, \x02%s\x02", userstring);
1576                } else {
1577                        irc_usermsg(irc, "  %s", userstring);
1578                }
1579               
1580                g_free(userstring);
1581        }
[3064ea4]1582        if(ctx == irc->otr->us->context_root)
1583                irc_usermsg(irc, "  (none)");
[8521b02]1584}
1585
1586void show_otr_context_info(irc_t *irc, ConnContext *ctx)
1587{
1588        switch(ctx->otr_offer) {
1589        case OFFER_NOT:
1590                irc_usermsg(irc, "  otr offer status: none sent");
1591                break;
1592        case OFFER_SENT:
1593                irc_usermsg(irc, "  otr offer status: awaiting reply");
1594                break;
1595        case OFFER_ACCEPTED:
1596                irc_usermsg(irc, "  otr offer status: accepted our offer");
1597                break;
1598        case OFFER_REJECTED:
1599                irc_usermsg(irc, "  otr offer status: ignored our offer");
1600                break;
1601        default:
1602                irc_usermsg(irc, "  otr offer status: %d", ctx->otr_offer);
1603        }
1604
1605        switch(ctx->msgstate) {
1606        case OTRL_MSGSTATE_PLAINTEXT:
1607                irc_usermsg(irc, "  connection state: cleartext");
1608                break;
1609        case OTRL_MSGSTATE_ENCRYPTED:
1610                irc_usermsg(irc, "  connection state: encrypted (v%d)", ctx->protocol_version);
1611                break;
1612        case OTRL_MSGSTATE_FINISHED:
1613                irc_usermsg(irc, "  connection state: shut down");
1614                break;
1615        default:
1616                irc_usermsg(irc, "  connection state: %d", ctx->msgstate);
1617        }
1618
[6c91e6e]1619        irc_usermsg(irc, "  fingerprints: (bold=active)");     
[8521b02]1620        show_fingerprints(irc, ctx);
[764c7d1]1621}
1622
[dc9797f]1623int keygen_in_progress(irc_t *irc, const char *handle, const char *protocol)
1624{
1625        kg_t *kg;
1626       
1627        if(!irc->otr->sent_accountname || !irc->otr->sent_protocol)
1628                return 0;
1629
1630        /* are we currently working on this key? */
1631        if(!strcmp(handle, irc->otr->sent_accountname) &&
1632           !strcmp(protocol, irc->otr->sent_protocol))
1633                return 1;
1634       
1635        /* do we have it queued for later? */
1636        for(kg=irc->otr->todo; kg; kg=kg->next) {
1637                if(!strcmp(handle, kg->accountname) &&
1638                   !strcmp(protocol, kg->protocol))
1639                        return 1;
1640        }
1641       
1642        return 0;
1643}
1644
[764c7d1]1645void otr_keygen(irc_t *irc, const char *handle, const char *protocol)
1646{
[dc9797f]1647        /* do nothing if a key for the requested account is already being generated */
1648        if(keygen_in_progress(irc, handle, protocol))
1649                return;
[764c7d1]1650
[522a00f]1651        /* see if we already have a keygen child running. if not, start one and put a
[27db433]1652           handler on its output. */
[dc9797f]1653        if(!irc->otr->keygen || waitpid(irc->otr->keygen, NULL, WNOHANG)) {
[27db433]1654                pid_t p;
1655                int to[2], from[2];
1656                FILE *tof, *fromf;
1657               
1658                if(pipe(to) < 0 || pipe(from) < 0) {
1659                        irc_usermsg(irc, "otr keygen: couldn't create pipe: %s", strerror(errno));
1660                        return;
1661                }
1662               
1663                tof = fdopen(to[1], "w");
1664                fromf = fdopen(from[0], "r");
1665                if(!tof || !fromf) {
1666                        irc_usermsg(irc, "otr keygen: couldn't streamify pipe: %s", strerror(errno));
1667                        return;
1668                }
1669               
1670                p = fork();
1671                if(p<0) {
1672                        irc_usermsg(irc, "otr keygen: couldn't fork: %s", strerror(errno));
1673                        return;
1674                }
1675               
1676                if(!p) {
1677                        /* child process */
1678                        signal(SIGTERM, exit);
[12cc58b]1679                        keygen_child_main(irc->otr->us, to[0], from[1]);
[27db433]1680                        exit(0);
1681                }
1682               
[dc9797f]1683                irc->otr->keygen = p;
1684                irc->otr->to = tof;
1685                irc->otr->from = fromf;
1686                irc->otr->sent_accountname = NULL;
1687                irc->otr->sent_protocol = NULL;
1688                irc->otr->todo = NULL;
[ad2d8bc]1689                b_input_add(from[0], B_EV_IO_READ, keygen_finish_handler, irc);
[27db433]1690        }
[764c7d1]1691       
[dc9797f]1692        /* is the keygen slave currently working? */
1693        if(irc->otr->sent_accountname) {
1694                /* enqueue our job for later transmission */
1695                kg_t **kg = &irc->otr->todo;
1696                while(*kg)
1697                        kg=&((*kg)->next);
1698                *kg = g_new0(kg_t, 1);
[ba5add7]1699                (*kg)->accountname = g_strdup(handle);
1700                (*kg)->protocol = g_strdup(protocol);
[dc9797f]1701        } else {
1702                /* send our job over and remember it */
1703                fprintf(irc->otr->to, "%s\n%s\n", handle, protocol);
1704                fflush(irc->otr->to);
[ba5add7]1705                irc->otr->sent_accountname = g_strdup(handle);
1706                irc->otr->sent_protocol = g_strdup(protocol);
[dc9797f]1707        }
[27db433]1708}
[522a00f]1709
[12cc58b]1710void keygen_child_main(OtrlUserState us, int infd, int outfd)
[27db433]1711{
1712        FILE *input, *output;
1713        char filename[128], accountname[512], protocol[512];
1714        gcry_error_t e;
1715        int tempfd;
1716       
1717        input = fdopen(infd, "r");
1718        output = fdopen(outfd, "w");
1719       
1720        while(!feof(input) && !ferror(input) && !feof(output) && !ferror(output)) {
1721                myfgets(accountname, 512, input);
1722                myfgets(protocol, 512, input);
[522a00f]1723               
[27db433]1724                strncpy(filename, "/tmp/bitlbee-XXXXXX", 128);
1725                tempfd = mkstemp(filename);
1726                close(tempfd);
1727
1728                e = otrl_privkey_generate(us, filename, accountname, protocol);
1729                if(e) {
1730                        fprintf(output, "\n");  /* this means failure */
1731                        fprintf(output, "otr keygen: %s\n", gcry_strerror(e));
1732                        unlink(filename);
1733                } else {
1734                        fprintf(output, "%s\n", filename);
1735                        fprintf(output, "otr keygen for %s/%s complete\n", accountname, protocol);
1736                }
1737                fflush(output);
1738        }
[764c7d1]1739       
[27db433]1740        fclose(input);
1741        fclose(output);
[764c7d1]1742}
1743
1744gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond)
1745{
[27db433]1746        irc_t *irc = (irc_t *)data;
1747        char filename[512], msg[512];
1748
[dc9797f]1749        myfgets(filename, 512, irc->otr->from);
1750        myfgets(msg, 512, irc->otr->from);
[27db433]1751       
1752        irc_usermsg(irc, "%s", msg);
1753        if(filename[0]) {
[ad2d8bc]1754                char *kf = g_strdup_printf("%s%s.otr_keys", global.conf->configdir, irc->user->nick);
[27db433]1755                char *tmp = g_strdup_printf("%s.new", kf);
1756                copyfile(filename, tmp);
1757                unlink(filename);
1758                rename(tmp,kf);
[dc9797f]1759                otrl_privkey_read(irc->otr->us, kf);
[27db433]1760                g_free(kf);
1761                g_free(tmp);
1762        }
[dc9797f]1763       
1764        /* forget this job */
[ba5add7]1765        g_free(irc->otr->sent_accountname);
1766        g_free(irc->otr->sent_protocol);
[dc9797f]1767        irc->otr->sent_accountname = NULL;
1768        irc->otr->sent_protocol = NULL;
1769       
1770        /* see if there are any more in the queue */
1771        if(irc->otr->todo) {
1772                kg_t *p = irc->otr->todo;
1773                /* send the next one over */
1774                fprintf(irc->otr->to, "%s\n%s\n", p->accountname, p->protocol);
1775                fflush(irc->otr->to);
1776                irc->otr->sent_accountname = p->accountname;
1777                irc->otr->sent_protocol = p->protocol;
1778                irc->otr->todo = p->next;
1779                g_free(p);
1780                return TRUE;   /* keep watching */
1781        } else {
1782                /* okay, the slave is idle now, so kill him */
1783                fclose(irc->otr->from);
1784                fclose(irc->otr->to);
[2dcaf9a]1785                irc->otr->from = irc->otr->to = NULL;
[dc9797f]1786                kill(irc->otr->keygen, SIGTERM);
1787                waitpid(irc->otr->keygen, NULL, 0);
1788                irc->otr->keygen = 0;
[27db433]1789                return FALSE;  /* unregister ourselves */
1790        }
1791}
1792
1793void copyfile(const char *a, const char *b)
1794{
1795        int fda, fdb;
1796        int n;
1797        char buf[1024];
1798       
1799        fda = open(a, O_RDONLY);
1800        fdb = open(b, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1801       
1802        while((n=read(fda, buf, 1024)) > 0)
1803                write(fdb, buf, n);
1804       
1805        close(fda);
1806        close(fdb);     
1807}
1808
1809void myfgets(char *s, int size, FILE *stream)
1810{
1811        if(!fgets(s, size, stream)) {
1812                s[0] = '\0';
1813        } else {
1814                int n = strlen(s);
1815                if(n>0 && s[n-1] == '\n')
1816                        s[n-1] = '\0';
1817        }
[764c7d1]1818}
1819
[6738a67]1820void yes_keygen(void *data)
[764c7d1]1821{
1822        account_t *acc = (account_t *)data;
[ad2d8bc]1823        irc_t *irc = acc->bee->ui_data;
[764c7d1]1824       
[ad2d8bc]1825        if(keygen_in_progress(irc, acc->user, acc->prpl->name)) {
1826                irc_usermsg(irc, "keygen for %s/%s already in progress",
[dc9797f]1827                        acc->user, acc->prpl->name);
1828        } else {
[ad2d8bc]1829                irc_usermsg(irc, "starting background keygen for %s/%s",
[3064ea4]1830                        acc->user, acc->prpl->name);
[ad2d8bc]1831                irc_usermsg(irc, "you will be notified when it completes");
1832                otr_keygen(irc, acc->user, acc->prpl->name);
[dc9797f]1833        }
[764c7d1]1834}
Note: See TracBrowser for help on using the repository browser.