source: otr.c @ d858d21

Last change on this file since d858d21 was d858d21, checked in by Sven Moritz Hallberg <sm@…>, at 2008-02-17T02:12:42Z

remove debugging output

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