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