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