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