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