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