[5ebff60] | 1 | /********************************************************************\ |
---|
[e2b15bb] | 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/) |
---|
[5ebff60] | 9 | |
---|
[81265e0] | 10 | (c) 2008-2011,2013 Sven Moritz Hallberg <pesco@khjk.org> |
---|
| 11 | funded by stonedcoder.org |
---|
[5ebff60] | 12 | |
---|
[e2b15bb] | 13 | files used to store OTR data: |
---|
| 14 | <configdir>/<nick>.otr_keys |
---|
| 15 | <configdir>/<nick>.otr_fprints |
---|
[e4752a6] | 16 | <configdir>/<nick>.otr_instags <- don't copy this one between hosts |
---|
[5ebff60] | 17 | |
---|
[e2b15bb] | 18 | top-level todos: (search for TODO for more ;-)) |
---|
| 19 | integrate otr_load/otr_save with existing storage backends |
---|
| 20 | per-account policy settings |
---|
| 21 | per-user policy settings |
---|
[e4752a6] | 22 | add a way to select recipient instance |
---|
[e2b15bb] | 23 | */ |
---|
| 24 | |
---|
| 25 | /* |
---|
| 26 | This program is free software; you can redistribute it and/or modify |
---|
| 27 | it under the terms of the GNU General Public License as published by |
---|
| 28 | the Free Software Foundation; either version 2 of the License, or |
---|
| 29 | (at your option) any later version. |
---|
| 30 | |
---|
| 31 | This program is distributed in the hope that it will be useful, |
---|
| 32 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 33 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 34 | GNU General Public License for more details. |
---|
| 35 | |
---|
| 36 | You should have received a copy of the GNU General Public License with |
---|
| 37 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
[6f10697] | 38 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 39 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[e2b15bb] | 40 | */ |
---|
| 41 | |
---|
[764c7d1] | 42 | #include "bitlbee.h" |
---|
| 43 | #include "irc.h" |
---|
| 44 | #include "otr.h" |
---|
| 45 | #include <sys/types.h> |
---|
[27db433] | 46 | #include <sys/wait.h> |
---|
[764c7d1] | 47 | #include <unistd.h> |
---|
[dc9797f] | 48 | #include <assert.h> |
---|
[6738a67] | 49 | #include <signal.h> |
---|
[764c7d1] | 50 | |
---|
| 51 | |
---|
| 52 | /** OTR interface routines for the OtrlMessageAppOps struct: **/ |
---|
| 53 | |
---|
| 54 | OtrlPolicy op_policy(void *opdata, ConnContext *context); |
---|
| 55 | |
---|
| 56 | void op_create_privkey(void *opdata, const char *accountname, const char *protocol); |
---|
| 57 | |
---|
| 58 | int op_is_logged_in(void *opdata, const char *accountname, const char *protocol, |
---|
[5ebff60] | 59 | const char *recipient); |
---|
[764c7d1] | 60 | |
---|
| 61 | void op_inject_message(void *opdata, const char *accountname, const char *protocol, |
---|
[5ebff60] | 62 | const char *recipient, const char *message); |
---|
[764c7d1] | 63 | |
---|
| 64 | void op_new_fingerprint(void *opdata, OtrlUserState us, const char *accountname, |
---|
[5ebff60] | 65 | const char *protocol, const char *username, unsigned char fingerprint[20]); |
---|
[764c7d1] | 66 | |
---|
| 67 | void op_write_fingerprints(void *opdata); |
---|
| 68 | |
---|
| 69 | void op_gone_secure(void *opdata, ConnContext *context); |
---|
| 70 | |
---|
| 71 | void op_gone_insecure(void *opdata, ConnContext *context); |
---|
| 72 | |
---|
| 73 | void op_still_secure(void *opdata, ConnContext *context, int is_reply); |
---|
| 74 | |
---|
| 75 | void op_log_message(void *opdata, const char *message); |
---|
| 76 | |
---|
[a13855a] | 77 | int op_max_message_size(void *opdata, ConnContext *context); |
---|
[764c7d1] | 78 | |
---|
[a13855a] | 79 | const char *op_account_name(void *opdata, const char *account, const char *protocol); |
---|
[764c7d1] | 80 | |
---|
[e65039a] | 81 | void op_create_instag(void *opdata, const char *account, const char *protocol); |
---|
| 82 | |
---|
[5d2bc9d] | 83 | void op_convert_msg(void *opdata, ConnContext *ctx, OtrlConvertType typ, |
---|
[5ebff60] | 84 | char **dst, const char *src); |
---|
[5d2bc9d] | 85 | void op_convert_free(void *opdata, ConnContext *ctx, char *msg); |
---|
| 86 | |
---|
[352a6b0] | 87 | void op_handle_smp_event(void *opdata, OtrlSMPEvent ev, ConnContext *ctx, |
---|
[5ebff60] | 88 | unsigned short percent, char *question); |
---|
[764c7d1] | 89 | |
---|
[090c9b7] | 90 | void op_handle_msg_event(void *opdata, OtrlMessageEvent ev, ConnContext *ctx, |
---|
[5ebff60] | 91 | const char *message, gcry_error_t err); |
---|
[090c9b7] | 92 | |
---|
[6d9f0ba] | 93 | const char *op_otr_error_message(void *opdata, ConnContext *ctx, |
---|
[5ebff60] | 94 | OtrlErrorCode err_code); |
---|
[6d9f0ba] | 95 | |
---|
[764c7d1] | 96 | /** otr sub-command handlers: **/ |
---|
| 97 | |
---|
[8358691] | 98 | static void cmd_otr(irc_t *irc, char **args); |
---|
[8521b02] | 99 | void cmd_otr_connect(irc_t *irc, char **args); |
---|
| 100 | void cmd_otr_disconnect(irc_t *irc, char **args); |
---|
[69ef042] | 101 | void cmd_otr_reconnect(irc_t *irc, char **args); |
---|
[5a71d9c] | 102 | void cmd_otr_smp(irc_t *irc, char **args); |
---|
[99a01b9] | 103 | void cmd_otr_smpq(irc_t *irc, char **args); |
---|
[5a71d9c] | 104 | void cmd_otr_trust(irc_t *irc, char **args); |
---|
[764c7d1] | 105 | void cmd_otr_info(irc_t *irc, char **args); |
---|
[94e7eb3] | 106 | void cmd_otr_keygen(irc_t *irc, char **args); |
---|
[c595308] | 107 | void cmd_otr_forget(irc_t *irc, char **args); |
---|
[764c7d1] | 108 | |
---|
| 109 | const command_t otr_commands[] = { |
---|
[8521b02] | 110 | { "connect", 1, &cmd_otr_connect, 0 }, |
---|
| 111 | { "disconnect", 1, &cmd_otr_disconnect, 0 }, |
---|
[69ef042] | 112 | { "reconnect", 1, &cmd_otr_reconnect, 0 }, |
---|
[8521b02] | 113 | { "smp", 2, &cmd_otr_smp, 0 }, |
---|
[99a01b9] | 114 | { "smpq", 3, &cmd_otr_smpq, 0 }, |
---|
[8521b02] | 115 | { "trust", 6, &cmd_otr_trust, 0 }, |
---|
| 116 | { "info", 0, &cmd_otr_info, 0 }, |
---|
[94e7eb3] | 117 | { "keygen", 1, &cmd_otr_keygen, 0 }, |
---|
[c595308] | 118 | { "forget", 2, &cmd_otr_forget, 0 }, |
---|
[764c7d1] | 119 | { NULL } |
---|
| 120 | }; |
---|
| 121 | |
---|
[6738a67] | 122 | typedef struct { |
---|
| 123 | void *fst; |
---|
| 124 | void *snd; |
---|
[5ebff60] | 125 | } pair_t; |
---|
[6738a67] | 126 | |
---|
[0c85c08] | 127 | static OtrlMessageAppOps otr_ops; /* collects interface functions required by OTR */ |
---|
| 128 | |
---|
[764c7d1] | 129 | |
---|
| 130 | /** misc. helpers/subroutines: **/ |
---|
| 131 | |
---|
[dc9797f] | 132 | /* check whether we are already generating a key for a given account */ |
---|
| 133 | int keygen_in_progress(irc_t *irc, const char *handle, const char *protocol); |
---|
| 134 | |
---|
[522a00f] | 135 | /* start background process to generate a (new) key for a given account */ |
---|
[764c7d1] | 136 | void otr_keygen(irc_t *irc, const char *handle, const char *protocol); |
---|
[c595308] | 137 | |
---|
[27db433] | 138 | /* main function for the forked keygen slave */ |
---|
[12cc58b] | 139 | void keygen_child_main(OtrlUserState us, int infd, int outfd); |
---|
[27db433] | 140 | |
---|
[522a00f] | 141 | /* mainloop handler for when a keygen finishes */ |
---|
[764c7d1] | 142 | gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond); |
---|
[c595308] | 143 | |
---|
[27db433] | 144 | /* copy the contents of file a to file b, overwriting it if it exists */ |
---|
| 145 | void copyfile(const char *a, const char *b); |
---|
| 146 | |
---|
| 147 | /* read one line of input from a stream, excluding trailing newline */ |
---|
| 148 | void myfgets(char *s, int size, FILE *stream); |
---|
| 149 | |
---|
[c595308] | 150 | /* some yes/no handlers */ |
---|
[6738a67] | 151 | void yes_keygen(void *data); |
---|
| 152 | void yes_forget_fingerprint(void *data); |
---|
| 153 | void yes_forget_context(void *data); |
---|
| 154 | void yes_forget_key(void *data); |
---|
[764c7d1] | 155 | |
---|
[22ec21d] | 156 | /* timeout handler that calls otrl_message_poll */ |
---|
| 157 | gboolean ev_message_poll(gpointer data, gint fd, b_input_condition cond); |
---|
| 158 | |
---|
[764c7d1] | 159 | /* helper to make sure accountname and protocol match the incoming "opdata" */ |
---|
| 160 | struct im_connection *check_imc(void *opdata, const char *accountname, |
---|
[5ebff60] | 161 | const char *protocol); |
---|
[764c7d1] | 162 | |
---|
[5a71d9c] | 163 | /* determine the nick for a given handle/protocol pair |
---|
| 164 | returns "handle/protocol" if not found */ |
---|
[764c7d1] | 165 | const char *peernick(irc_t *irc, const char *handle, const char *protocol); |
---|
| 166 | |
---|
[8521b02] | 167 | /* turn a hexadecimal digit into its numerical value */ |
---|
| 168 | int hexval(char a); |
---|
| 169 | |
---|
[ad2d8bc] | 170 | /* determine the irc_user_t for a given handle/protocol pair |
---|
[5a71d9c] | 171 | returns NULL if not found */ |
---|
[ad2d8bc] | 172 | irc_user_t *peeruser(irc_t *irc, const char *handle, const char *protocol); |
---|
[5a71d9c] | 173 | |
---|
[090c9b7] | 174 | /* show an otr-related message to the user */ |
---|
| 175 | void display_otr_message(void *opdata, ConnContext *ctx, const char *fmt, ...); |
---|
| 176 | |
---|
| 177 | /* write an otr-related message to the system log */ |
---|
| 178 | void log_otr_message(void *opdata, const char *fmt, ...); |
---|
| 179 | |
---|
[99a01b9] | 180 | /* combined handler for the 'otr smp' and 'otr smpq' commands */ |
---|
[7c91392] | 181 | void otr_smp_or_smpq(irc_t *irc, const char *nick, const char *question, |
---|
[5ebff60] | 182 | const char *secret); |
---|
[99a01b9] | 183 | |
---|
[a0c6fc5] | 184 | /* update flags within the irc_user structure to reflect OTR status of context */ |
---|
| 185 | void otr_update_uflags(ConnContext *context, irc_user_t *u); |
---|
| 186 | |
---|
[5a71d9c] | 187 | /* update op/voice flag of given user according to encryption state and settings |
---|
| 188 | returns 0 if neither op_buddies nor voice_buddies is set to "encrypted", |
---|
[e88fe7da] | 189 | i.e. msgstate should be announced separately */ |
---|
[ad2d8bc] | 190 | int otr_update_modeflags(irc_t *irc, irc_user_t *u); |
---|
[5a71d9c] | 191 | |
---|
[8521b02] | 192 | /* show general info about the OTR subsystem; called by 'otr info' */ |
---|
| 193 | void show_general_otr_info(irc_t *irc); |
---|
| 194 | |
---|
[2c81d15] | 195 | /* show info about a given OTR context and subcontexts/instances. bestctx |
---|
| 196 | may be either NULL or preferred destination context (this is hilighted |
---|
| 197 | in the output as being the target for a message) */ |
---|
| 198 | void show_otr_context_info(irc_t *irc, ConnContext *ctx, ConnContext *bestctx); |
---|
[8521b02] | 199 | |
---|
[764c7d1] | 200 | /* show the list of fingerprints associated with a given context */ |
---|
| 201 | void show_fingerprints(irc_t *irc, ConnContext *ctx); |
---|
| 202 | |
---|
[c595308] | 203 | /* find a fingerprint by prefix (given as any number of hex strings) */ |
---|
| 204 | Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args); |
---|
| 205 | |
---|
[5f4eede] | 206 | /* find a private key by fingerprint prefix (given as any number of hex strings) */ |
---|
| 207 | OtrlPrivKey *match_privkey(irc_t *irc, const char **args); |
---|
| 208 | |
---|
[c7b94ef] | 209 | /* check whether a string is safe to use in a path component */ |
---|
| 210 | int strsane(const char *s); |
---|
| 211 | |
---|
[c239fff] | 212 | /* close the OTR connection with the given buddy */ |
---|
| 213 | gboolean otr_disconnect_user(irc_t *irc, irc_user_t *u); |
---|
| 214 | |
---|
| 215 | /* close all active OTR connections */ |
---|
| 216 | void otr_disconnect_all(irc_t *irc); |
---|
| 217 | |
---|
[86fd261] | 218 | /* modifies string in-place, replacing \x03 with '?', |
---|
| 219 | as a quick way to prevent remote users from messing with irc colors */ |
---|
| 220 | static char *otr_filter_colors(char *msg); |
---|
| 221 | |
---|
[0c85c08] | 222 | /* functions to be called for certain events */ |
---|
| 223 | static const struct irc_plugin otr_plugin; |
---|
| 224 | |
---|
[654112d4] | 225 | #define OTR_COLOR_TRUSTED "03" /* green */ |
---|
| 226 | #define OTR_COLOR_UNTRUSTED "05" /* red */ |
---|
| 227 | |
---|
[764c7d1] | 228 | /*** routines declared in otr.h: ***/ |
---|
| 229 | |
---|
[858ea01] | 230 | #ifdef OTR_BI |
---|
| 231 | #define init_plugin otr_init |
---|
| 232 | #endif |
---|
| 233 | |
---|
| 234 | void init_plugin(void) |
---|
[764c7d1] | 235 | { |
---|
| 236 | OTRL_INIT; |
---|
[5ebff60] | 237 | |
---|
[764c7d1] | 238 | /* fill global OtrlMessageAppOps */ |
---|
[0c85c08] | 239 | otr_ops.policy = &op_policy; |
---|
| 240 | otr_ops.create_privkey = &op_create_privkey; |
---|
| 241 | otr_ops.is_logged_in = &op_is_logged_in; |
---|
| 242 | otr_ops.inject_message = &op_inject_message; |
---|
| 243 | otr_ops.update_context_list = NULL; |
---|
| 244 | otr_ops.new_fingerprint = &op_new_fingerprint; |
---|
| 245 | otr_ops.write_fingerprints = &op_write_fingerprints; |
---|
| 246 | otr_ops.gone_secure = &op_gone_secure; |
---|
| 247 | otr_ops.gone_insecure = &op_gone_insecure; |
---|
| 248 | otr_ops.still_secure = &op_still_secure; |
---|
| 249 | otr_ops.max_message_size = &op_max_message_size; |
---|
| 250 | otr_ops.account_name = &op_account_name; |
---|
| 251 | otr_ops.account_name_free = NULL; |
---|
[81265e0] | 252 | |
---|
| 253 | /* stuff added with libotr 4.0.0 */ |
---|
| 254 | otr_ops.received_symkey = NULL; /* we don't use the extra key */ |
---|
[6d9f0ba] | 255 | otr_ops.otr_error_message = &op_otr_error_message; |
---|
[81265e0] | 256 | otr_ops.otr_error_message_free = NULL; |
---|
[6d9f0ba] | 257 | otr_ops.resent_msg_prefix = NULL; /* default: [resent] */ |
---|
[81265e0] | 258 | otr_ops.resent_msg_prefix_free = NULL; |
---|
[352a6b0] | 259 | otr_ops.handle_smp_event = &op_handle_smp_event; |
---|
[090c9b7] | 260 | otr_ops.handle_msg_event = &op_handle_msg_event; |
---|
[e65039a] | 261 | otr_ops.create_instag = &op_create_instag; |
---|
[5d2bc9d] | 262 | otr_ops.convert_msg = &op_convert_msg; |
---|
| 263 | otr_ops.convert_free = &op_convert_free; |
---|
[5ebff60] | 264 | otr_ops.timer_control = NULL; /* we just poll */ |
---|
| 265 | |
---|
| 266 | root_command_add("otr", 1, cmd_otr, 0); |
---|
| 267 | register_irc_plugin(&otr_plugin); |
---|
[764c7d1] | 268 | } |
---|
| 269 | |
---|
[d28fe1c4] | 270 | #ifndef OTR_BI |
---|
| 271 | struct plugin_info *init_plugin_info(void) |
---|
| 272 | { |
---|
| 273 | static struct plugin_info info = { |
---|
| 274 | BITLBEE_ABI_VERSION_CODE, |
---|
| 275 | "otr", |
---|
| 276 | BITLBEE_VERSION, |
---|
| 277 | "Off-the-Record communication", |
---|
| 278 | NULL, |
---|
| 279 | NULL |
---|
| 280 | }; |
---|
| 281 | |
---|
| 282 | return &info; |
---|
| 283 | } |
---|
| 284 | #endif |
---|
| 285 | |
---|
[0c85c08] | 286 | gboolean otr_irc_new(irc_t *irc) |
---|
[dc9797f] | 287 | { |
---|
[0c85c08] | 288 | set_t *s; |
---|
| 289 | GSList *l; |
---|
[5ebff60] | 290 | |
---|
[0c85c08] | 291 | irc->otr = g_new0(otr_t, 1); |
---|
| 292 | irc->otr->us = otrl_userstate_create(); |
---|
[5ebff60] | 293 | |
---|
| 294 | s = set_add(&irc->b->set, "otr_color_encrypted", "true", set_eval_bool, irc); |
---|
| 295 | |
---|
| 296 | s = set_add(&irc->b->set, "otr_policy", "opportunistic", set_eval_list, irc); |
---|
| 297 | l = g_slist_prepend(NULL, "never"); |
---|
| 298 | l = g_slist_prepend(l, "opportunistic"); |
---|
| 299 | l = g_slist_prepend(l, "manual"); |
---|
| 300 | l = g_slist_prepend(l, "always"); |
---|
[0c85c08] | 301 | s->eval_data = l; |
---|
[1082395] | 302 | |
---|
[5ebff60] | 303 | s = set_add(&irc->b->set, "otr_does_html", "true", set_eval_bool, irc); |
---|
| 304 | |
---|
[22ec21d] | 305 | /* regularly call otrl_message_poll */ |
---|
| 306 | gint definterval = otrl_message_poll_get_default_interval(irc->otr->us); |
---|
[c347a12] | 307 | irc->otr->timer = b_timeout_add(definterval, ev_message_poll, irc->otr); |
---|
[22ec21d] | 308 | |
---|
[0c85c08] | 309 | return TRUE; |
---|
[dc9797f] | 310 | } |
---|
| 311 | |
---|
[0c85c08] | 312 | void otr_irc_free(irc_t *irc) |
---|
[dc9797f] | 313 | { |
---|
[098a75b] | 314 | set_t *s; |
---|
[0c85c08] | 315 | otr_t *otr = irc->otr; |
---|
[5ebff60] | 316 | |
---|
[c239fff] | 317 | otr_disconnect_all(irc); |
---|
[c347a12] | 318 | b_event_remove(otr->timer); |
---|
[dc9797f] | 319 | otrl_userstate_free(otr->us); |
---|
[098a75b] | 320 | |
---|
| 321 | s = set_find(&irc->b->set, "otr_policy"); |
---|
| 322 | g_slist_free(s->eval_data); |
---|
| 323 | |
---|
[5ebff60] | 324 | if (otr->keygen) { |
---|
[dc9797f] | 325 | kill(otr->keygen, SIGTERM); |
---|
| 326 | waitpid(otr->keygen, NULL, 0); |
---|
| 327 | /* TODO: remove stale keygen tempfiles */ |
---|
| 328 | } |
---|
[5ebff60] | 329 | if (otr->to) { |
---|
[dc9797f] | 330 | fclose(otr->to); |
---|
[5ebff60] | 331 | } |
---|
| 332 | if (otr->from) { |
---|
[dc9797f] | 333 | fclose(otr->from); |
---|
[5ebff60] | 334 | } |
---|
| 335 | while (otr->todo) { |
---|
[dc9797f] | 336 | kg_t *p = otr->todo; |
---|
| 337 | otr->todo = p->next; |
---|
| 338 | g_free(p); |
---|
| 339 | } |
---|
| 340 | g_free(otr); |
---|
| 341 | } |
---|
| 342 | |
---|
[764c7d1] | 343 | void otr_load(irc_t *irc) |
---|
| 344 | { |
---|
| 345 | char s[512]; |
---|
| 346 | account_t *a; |
---|
| 347 | gcry_error_t e; |
---|
[522a00f] | 348 | gcry_error_t enoent = gcry_error_from_errno(ENOENT); |
---|
[5ebff60] | 349 | int kg = 0; |
---|
[764c7d1] | 350 | |
---|
[5ebff60] | 351 | if (strsane(irc->user->nick)) { |
---|
[c7b94ef] | 352 | g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, irc->user->nick); |
---|
| 353 | e = otrl_privkey_read(irc->otr->us, s); |
---|
[5ebff60] | 354 | if (e && e != enoent) { |
---|
[c7b94ef] | 355 | irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e)); |
---|
| 356 | } |
---|
| 357 | g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick); |
---|
| 358 | e = otrl_privkey_read_fingerprints(irc->otr->us, s, NULL, NULL); |
---|
[5ebff60] | 359 | if (e && e != enoent) { |
---|
[c7b94ef] | 360 | irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e)); |
---|
| 361 | } |
---|
[e65039a] | 362 | g_snprintf(s, 511, "%s%s.otr_instags", global.conf->configdir, irc->user->nick); |
---|
| 363 | e = otrl_instag_read(irc->otr->us, s); |
---|
[5ebff60] | 364 | if (e && e != enoent) { |
---|
[e65039a] | 365 | irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e)); |
---|
| 366 | } |
---|
[764c7d1] | 367 | } |
---|
[5ebff60] | 368 | |
---|
[764c7d1] | 369 | /* check for otr keys on all accounts */ |
---|
[5ebff60] | 370 | for (a = irc->b->accounts; a; a = a->next) { |
---|
[3064ea4] | 371 | kg = otr_check_for_key(a) || kg; |
---|
| 372 | } |
---|
[5ebff60] | 373 | if (kg) { |
---|
[e67e513] | 374 | irc_rootmsg(irc, "Notice: " |
---|
[5ebff60] | 375 | "The accounts above do not have OTR encryption keys associated with them, yet. " |
---|
| 376 | "These keys are now being generated in the background. " |
---|
| 377 | "You will be notified as they are completed. " |
---|
| 378 | "It is not necessary to wait; " |
---|
| 379 | "BitlBee can be used normally during key generation. " |
---|
| 380 | "You may safely ignore this message if you don't know what OTR is. ;)"); |
---|
[764c7d1] | 381 | } |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | void otr_save(irc_t *irc) |
---|
| 385 | { |
---|
| 386 | char s[512]; |
---|
[3c80a9d] | 387 | gcry_error_t e; |
---|
[764c7d1] | 388 | |
---|
[5ebff60] | 389 | if (strsane(irc->user->nick)) { |
---|
[c7b94ef] | 390 | g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick); |
---|
| 391 | e = otrl_privkey_write_fingerprints(irc->otr->us, s); |
---|
[5ebff60] | 392 | if (e) { |
---|
[c7b94ef] | 393 | irc_rootmsg(irc, "otr save: %s: %s", s, gcry_strerror(e)); |
---|
| 394 | } |
---|
| 395 | chmod(s, 0600); |
---|
[3c80a9d] | 396 | } |
---|
[764c7d1] | 397 | } |
---|
| 398 | |
---|
| 399 | void otr_remove(const char *nick) |
---|
| 400 | { |
---|
| 401 | char s[512]; |
---|
[5ebff60] | 402 | |
---|
| 403 | if (strsane(nick)) { |
---|
[c7b94ef] | 404 | g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, nick); |
---|
| 405 | unlink(s); |
---|
| 406 | g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, nick); |
---|
| 407 | unlink(s); |
---|
| 408 | } |
---|
[764c7d1] | 409 | } |
---|
| 410 | |
---|
| 411 | void otr_rename(const char *onick, const char *nnick) |
---|
| 412 | { |
---|
| 413 | char s[512], t[512]; |
---|
[5ebff60] | 414 | |
---|
| 415 | if (strsane(nnick) && strsane(onick)) { |
---|
[c7b94ef] | 416 | g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, onick); |
---|
| 417 | g_snprintf(t, 511, "%s%s.otr_keys", global.conf->configdir, nnick); |
---|
[5ebff60] | 418 | rename(s, t); |
---|
[c7b94ef] | 419 | g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, onick); |
---|
| 420 | g_snprintf(t, 511, "%s%s.otr_fprints", global.conf->configdir, nnick); |
---|
[5ebff60] | 421 | rename(s, t); |
---|
[c7b94ef] | 422 | } |
---|
[764c7d1] | 423 | } |
---|
| 424 | |
---|
[3064ea4] | 425 | int otr_check_for_key(account_t *a) |
---|
[764c7d1] | 426 | { |
---|
[ad2d8bc] | 427 | irc_t *irc = a->bee->ui_data; |
---|
[a13855a] | 428 | OtrlPrivKey *k; |
---|
[5ebff60] | 429 | |
---|
[1dd3470] | 430 | /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */ |
---|
[5ebff60] | 431 | if (a->prpl->options & OPT_NOOTR) { |
---|
[1dd3470] | 432 | return 0; |
---|
| 433 | } |
---|
[5ebff60] | 434 | |
---|
[dc9797f] | 435 | k = otrl_privkey_find(irc->otr->us, a->user, a->prpl->name); |
---|
[5ebff60] | 436 | if (k) { |
---|
[e67e513] | 437 | irc_rootmsg(irc, "otr: %s/%s ready", a->user, a->prpl->name); |
---|
[3064ea4] | 438 | return 0; |
---|
[5ebff60] | 439 | } |
---|
| 440 | if (keygen_in_progress(irc, a->user, a->prpl->name)) { |
---|
[e67e513] | 441 | irc_rootmsg(irc, "otr: keygen for %s/%s already in progress", a->user, a->prpl->name); |
---|
[3064ea4] | 442 | return 0; |
---|
[764c7d1] | 443 | } else { |
---|
[e67e513] | 444 | irc_rootmsg(irc, "otr: starting background keygen for %s/%s", a->user, a->prpl->name); |
---|
[764c7d1] | 445 | otr_keygen(irc, a->user, a->prpl->name); |
---|
[3064ea4] | 446 | return 1; |
---|
[764c7d1] | 447 | } |
---|
| 448 | } |
---|
| 449 | |
---|
[934db064] | 450 | char *otr_filter_msg_in(irc_user_t *iu, char *msg, int flags) |
---|
[764c7d1] | 451 | { |
---|
| 452 | int ignore_msg; |
---|
| 453 | char *newmsg = NULL; |
---|
| 454 | OtrlTLV *tlvs = NULL; |
---|
[934db064] | 455 | irc_t *irc = iu->irc; |
---|
| 456 | struct im_connection *ic = iu->bu->ic; |
---|
[5ebff60] | 457 | |
---|
[1dd3470] | 458 | /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */ |
---|
[09f7ec1] | 459 | if (ic->acc->prpl->options & OPT_NOOTR || |
---|
| 460 | iu->bu->flags & BEE_USER_NOOTR) { |
---|
[934db064] | 461 | return msg; |
---|
[1dd3470] | 462 | } |
---|
[5ebff60] | 463 | |
---|
[0c85c08] | 464 | ignore_msg = otrl_message_receiving(irc->otr->us, &otr_ops, ic, |
---|
[5ebff60] | 465 | ic->acc->user, ic->acc->prpl->name, iu->bu->handle, msg, &newmsg, |
---|
| 466 | &tlvs, NULL, NULL, NULL); |
---|
[764c7d1] | 467 | |
---|
[098a75b] | 468 | if (tlvs) { |
---|
| 469 | otrl_tlv_free(tlvs); |
---|
| 470 | } |
---|
| 471 | |
---|
[5ebff60] | 472 | if (ignore_msg) { |
---|
[764c7d1] | 473 | /* this was an internal OTR protocol message */ |
---|
| 474 | return NULL; |
---|
[5ebff60] | 475 | } else if (!newmsg) { |
---|
[764c7d1] | 476 | /* this was a non-OTR message */ |
---|
[86fd261] | 477 | return otr_filter_colors(msg); |
---|
[764c7d1] | 478 | } else { |
---|
[da44b08] | 479 | /* we're done with the original msg, which will be caller-freed. */ |
---|
[5d2bc9d] | 480 | return newmsg; |
---|
[764c7d1] | 481 | } |
---|
| 482 | } |
---|
| 483 | |
---|
[934db064] | 484 | char *otr_filter_msg_out(irc_user_t *iu, char *msg, int flags) |
---|
[5ebff60] | 485 | { |
---|
[5a71d9c] | 486 | int st; |
---|
| 487 | char *otrmsg = NULL; |
---|
| 488 | ConnContext *ctx = NULL; |
---|
[934db064] | 489 | irc_t *irc = iu->irc; |
---|
| 490 | struct im_connection *ic = iu->bu->ic; |
---|
[329f9fe] | 491 | otrl_instag_t instag = OTRL_INSTAG_BEST; // XXX? |
---|
[5ebff60] | 492 | |
---|
[329f9fe] | 493 | /* NB: in libotr 4.0.0 OTRL_INSTAG_RECENT will cause a null-pointer deref |
---|
| 494 | * in otrl_message_sending with newly-added OTR contexts. |
---|
| 495 | */ |
---|
[1dd3470] | 496 | |
---|
| 497 | /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */ |
---|
[09f7ec1] | 498 | if (ic->acc->prpl->options & OPT_NOOTR || |
---|
| 499 | iu->bu->flags & BEE_USER_NOOTR) { |
---|
[934db064] | 500 | return msg; |
---|
[1dd3470] | 501 | } |
---|
[1082395] | 502 | |
---|
[0c85c08] | 503 | st = otrl_message_sending(irc->otr->us, &otr_ops, ic, |
---|
[5ebff60] | 504 | ic->acc->user, ic->acc->prpl->name, iu->bu->handle, instag, |
---|
| 505 | msg, NULL, &otrmsg, OTRL_FRAGMENT_SEND_ALL_BUT_LAST, &ctx, NULL, NULL); |
---|
[367ea3c] | 506 | |
---|
[5ebff60] | 507 | if (otrmsg && otrmsg != msg) { |
---|
[367ea3c] | 508 | /* libotr wants us to replace our message */ |
---|
| 509 | /* NB: caller will free old msg */ |
---|
[098a75b] | 510 | msg = st ? NULL : g_strdup(otrmsg); |
---|
[367ea3c] | 511 | otrl_message_free(otrmsg); |
---|
[764c7d1] | 512 | } |
---|
[5ebff60] | 513 | |
---|
| 514 | if (st) { |
---|
[367ea3c] | 515 | irc_usernotice(iu, "otr: error handling outgoing message: %d", st); |
---|
[5ebff60] | 516 | msg = NULL; /* do not send plaintext! */ |
---|
[cc17b76] | 517 | } |
---|
[367ea3c] | 518 | |
---|
| 519 | return msg; |
---|
[764c7d1] | 520 | } |
---|
| 521 | |
---|
[0c85c08] | 522 | static const struct irc_plugin otr_plugin = |
---|
| 523 | { |
---|
| 524 | otr_irc_new, |
---|
| 525 | otr_irc_free, |
---|
[934db064] | 526 | otr_filter_msg_out, |
---|
| 527 | otr_filter_msg_in, |
---|
[2dcaf9a] | 528 | otr_load, |
---|
| 529 | otr_save, |
---|
| 530 | otr_remove, |
---|
[0c85c08] | 531 | }; |
---|
| 532 | |
---|
[8358691] | 533 | static void cmd_otr(irc_t *irc, char **args) |
---|
[764c7d1] | 534 | { |
---|
| 535 | const command_t *cmd; |
---|
[5ebff60] | 536 | |
---|
| 537 | if (!args[0]) { |
---|
[764c7d1] | 538 | return; |
---|
[5ebff60] | 539 | } |
---|
| 540 | |
---|
| 541 | if (!args[1]) { |
---|
[764c7d1] | 542 | return; |
---|
[5ebff60] | 543 | } |
---|
| 544 | |
---|
| 545 | for (cmd = otr_commands; cmd->command; cmd++) { |
---|
| 546 | if (strcmp(cmd->command, args[1]) == 0) { |
---|
[764c7d1] | 547 | break; |
---|
[5ebff60] | 548 | } |
---|
[764c7d1] | 549 | } |
---|
[5ebff60] | 550 | |
---|
| 551 | if (!cmd->command) { |
---|
[e67e513] | 552 | irc_rootmsg(irc, "%s: unknown subcommand \"%s\", see \x02help otr\x02", |
---|
[5ebff60] | 553 | args[0], args[1]); |
---|
[764c7d1] | 554 | return; |
---|
| 555 | } |
---|
[5ebff60] | 556 | |
---|
| 557 | if (!args[cmd->required_parameters + 1]) { |
---|
[e67e513] | 558 | irc_rootmsg(irc, "%s %s: not enough arguments (%d req.)", |
---|
[5ebff60] | 559 | args[0], args[1], cmd->required_parameters); |
---|
[764c7d1] | 560 | return; |
---|
| 561 | } |
---|
[5ebff60] | 562 | |
---|
| 563 | cmd->execute(irc, args + 1); |
---|
[764c7d1] | 564 | } |
---|
| 565 | |
---|
| 566 | |
---|
| 567 | /*** OTR "MessageAppOps" callbacks for global.otr_ui: ***/ |
---|
| 568 | |
---|
| 569 | OtrlPolicy op_policy(void *opdata, ConnContext *context) |
---|
| 570 | { |
---|
[e2b15bb] | 571 | struct im_connection *ic = check_imc(opdata, context->accountname, context->protocol); |
---|
[ad2d8bc] | 572 | irc_t *irc = ic->bee->ui_data; |
---|
[e2b15bb] | 573 | const char *p; |
---|
[5ebff60] | 574 | |
---|
[dc9797f] | 575 | /* policy override during keygen: if we're missing the key for context but are currently |
---|
| 576 | generating it, then that's as much as we can do. => temporarily return NEVER. */ |
---|
[5ebff60] | 577 | if (keygen_in_progress(irc, context->accountname, context->protocol) && |
---|
| 578 | !otrl_privkey_find(irc->otr->us, context->accountname, context->protocol)) { |
---|
[dc9797f] | 579 | return OTRL_POLICY_NEVER; |
---|
[5ebff60] | 580 | } |
---|
[e2b15bb] | 581 | |
---|
[ad2d8bc] | 582 | p = set_getstr(&ic->bee->set, "otr_policy"); |
---|
[5ebff60] | 583 | if (!strcmp(p, "never")) { |
---|
[e2b15bb] | 584 | return OTRL_POLICY_NEVER; |
---|
[5ebff60] | 585 | } |
---|
| 586 | if (!strcmp(p, "opportunistic")) { |
---|
[e2b15bb] | 587 | return OTRL_POLICY_OPPORTUNISTIC; |
---|
[5ebff60] | 588 | } |
---|
| 589 | if (!strcmp(p, "manual")) { |
---|
[e2b15bb] | 590 | return OTRL_POLICY_MANUAL; |
---|
[5ebff60] | 591 | } |
---|
| 592 | if (!strcmp(p, "always")) { |
---|
[e2b15bb] | 593 | return OTRL_POLICY_ALWAYS; |
---|
[5ebff60] | 594 | } |
---|
| 595 | |
---|
[764c7d1] | 596 | return OTRL_POLICY_OPPORTUNISTIC; |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | void op_create_privkey(void *opdata, const char *accountname, |
---|
[5ebff60] | 600 | const char *protocol) |
---|
[764c7d1] | 601 | { |
---|
| 602 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
[ad2d8bc] | 603 | irc_t *irc = ic->bee->ui_data; |
---|
[5ebff60] | 604 | |
---|
[dc9797f] | 605 | /* will fail silently if keygen already in progress */ |
---|
[ad2d8bc] | 606 | otr_keygen(irc, accountname, protocol); |
---|
[764c7d1] | 607 | } |
---|
| 608 | |
---|
| 609 | int op_is_logged_in(void *opdata, const char *accountname, |
---|
[5ebff60] | 610 | const char *protocol, const char *recipient) |
---|
[764c7d1] | 611 | { |
---|
| 612 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
[ad2d8bc] | 613 | bee_user_t *bu; |
---|
[764c7d1] | 614 | |
---|
[ad2d8bc] | 615 | /* lookup the irc_user_t for the given recipient */ |
---|
| 616 | bu = bee_user_by_handle(ic->bee, ic, recipient); |
---|
[5ebff60] | 617 | if (bu) { |
---|
| 618 | if (bu->flags & BEE_USER_ONLINE) { |
---|
[764c7d1] | 619 | return 1; |
---|
[5ebff60] | 620 | } else { |
---|
[764c7d1] | 621 | return 0; |
---|
[5ebff60] | 622 | } |
---|
[764c7d1] | 623 | } else { |
---|
| 624 | return -1; |
---|
| 625 | } |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | void op_inject_message(void *opdata, const char *accountname, |
---|
[5ebff60] | 629 | const char *protocol, const char *recipient, const char *message) |
---|
[764c7d1] | 630 | { |
---|
| 631 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
[ad2d8bc] | 632 | irc_t *irc = ic->bee->ui_data; |
---|
[764c7d1] | 633 | |
---|
| 634 | if (strcmp(accountname, recipient) == 0) { |
---|
| 635 | /* huh? injecting messages to myself? */ |
---|
[e67e513] | 636 | irc_rootmsg(irc, "note to self: %s", message); |
---|
[764c7d1] | 637 | } else { |
---|
| 638 | /* need to drop some consts here :-( */ |
---|
| 639 | /* TODO: get flags into op_inject_message?! */ |
---|
[5ebff60] | 640 | ic->acc->prpl->buddy_msg(ic, (char *) recipient, (char *) message, 0); |
---|
[764c7d1] | 641 | /* ignoring return value :-/ */ |
---|
| 642 | } |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | void op_new_fingerprint(void *opdata, OtrlUserState us, |
---|
[5ebff60] | 646 | const char *accountname, const char *protocol, |
---|
| 647 | const char *username, unsigned char fingerprint[20]) |
---|
[764c7d1] | 648 | { |
---|
| 649 | struct im_connection *ic = check_imc(opdata, accountname, protocol); |
---|
[ad2d8bc] | 650 | irc_t *irc = ic->bee->ui_data; |
---|
[409c2de] | 651 | irc_user_t *u = peeruser(irc, username, protocol); |
---|
[5ebff60] | 652 | char hunam[45]; /* anybody looking? ;-) */ |
---|
| 653 | |
---|
[764c7d1] | 654 | otrl_privkey_hash_to_human(hunam, fingerprint); |
---|
[5ebff60] | 655 | if (u) { |
---|
[409c2de] | 656 | irc_usernotice(u, "new fingerprint: %s", hunam); |
---|
| 657 | } else { |
---|
| 658 | /* this case shouldn't normally happen */ |
---|
| 659 | irc_rootmsg(irc, "new fingerprint for %s/%s: %s", |
---|
[5ebff60] | 660 | username, protocol, hunam); |
---|
[409c2de] | 661 | } |
---|
[764c7d1] | 662 | } |
---|
| 663 | |
---|
| 664 | void op_write_fingerprints(void *opdata) |
---|
| 665 | { |
---|
[5ebff60] | 666 | struct im_connection *ic = (struct im_connection *) opdata; |
---|
[ad2d8bc] | 667 | irc_t *irc = ic->bee->ui_data; |
---|
[764c7d1] | 668 | |
---|
[ad2d8bc] | 669 | otr_save(irc); |
---|
[764c7d1] | 670 | } |
---|
| 671 | |
---|
| 672 | void op_gone_secure(void *opdata, ConnContext *context) |
---|
| 673 | { |
---|
| 674 | struct im_connection *ic = |
---|
[5ebff60] | 675 | check_imc(opdata, context->accountname, context->protocol); |
---|
[ad2d8bc] | 676 | irc_user_t *u; |
---|
| 677 | irc_t *irc = ic->bee->ui_data; |
---|
[764c7d1] | 678 | |
---|
[ad2d8bc] | 679 | u = peeruser(irc, context->username, context->protocol); |
---|
[5ebff60] | 680 | if (!u) { |
---|
[5a71d9c] | 681 | log_message(LOGLVL_ERROR, |
---|
[5ebff60] | 682 | "BUG: otr.c: op_gone_secure: irc_user_t for %s/%s/%s not found!", |
---|
| 683 | context->username, context->protocol, context->accountname); |
---|
[5a71d9c] | 684 | return; |
---|
| 685 | } |
---|
[5ebff60] | 686 | |
---|
[a0c6fc5] | 687 | otr_update_uflags(context, u); |
---|
[5ebff60] | 688 | if (!otr_update_modeflags(irc, u)) { |
---|
[f1cf01c] | 689 | char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!"; |
---|
[409c2de] | 690 | irc_usernotice(u, "conversation is now off the record (%s)", trust); |
---|
[f1cf01c] | 691 | } |
---|
[764c7d1] | 692 | } |
---|
| 693 | |
---|
| 694 | void op_gone_insecure(void *opdata, ConnContext *context) |
---|
| 695 | { |
---|
| 696 | struct im_connection *ic = |
---|
[5ebff60] | 697 | check_imc(opdata, context->accountname, context->protocol); |
---|
[ad2d8bc] | 698 | irc_t *irc = ic->bee->ui_data; |
---|
| 699 | irc_user_t *u; |
---|
[764c7d1] | 700 | |
---|
[ad2d8bc] | 701 | u = peeruser(irc, context->username, context->protocol); |
---|
[5ebff60] | 702 | if (!u) { |
---|
[5a71d9c] | 703 | log_message(LOGLVL_ERROR, |
---|
[5ebff60] | 704 | "BUG: otr.c: op_gone_insecure: irc_user_t for %s/%s/%s not found!", |
---|
| 705 | context->username, context->protocol, context->accountname); |
---|
[5a71d9c] | 706 | return; |
---|
| 707 | } |
---|
[a0c6fc5] | 708 | otr_update_uflags(context, u); |
---|
[5ebff60] | 709 | if (!otr_update_modeflags(irc, u)) { |
---|
[409c2de] | 710 | irc_usernotice(u, "conversation is now in cleartext"); |
---|
[5ebff60] | 711 | } |
---|
[764c7d1] | 712 | } |
---|
| 713 | |
---|
| 714 | void op_still_secure(void *opdata, ConnContext *context, int is_reply) |
---|
| 715 | { |
---|
| 716 | struct im_connection *ic = |
---|
[5ebff60] | 717 | check_imc(opdata, context->accountname, context->protocol); |
---|
[ad2d8bc] | 718 | irc_t *irc = ic->bee->ui_data; |
---|
| 719 | irc_user_t *u; |
---|
[764c7d1] | 720 | |
---|
[ad2d8bc] | 721 | u = peeruser(irc, context->username, context->protocol); |
---|
[5ebff60] | 722 | if (!u) { |
---|
[5a71d9c] | 723 | log_message(LOGLVL_ERROR, |
---|
[5ebff60] | 724 | "BUG: otr.c: op_still_secure: irc_user_t for %s/%s/%s not found!", |
---|
| 725 | context->username, context->protocol, context->accountname); |
---|
[5a71d9c] | 726 | return; |
---|
| 727 | } |
---|
[a0c6fc5] | 728 | |
---|
| 729 | otr_update_uflags(context, u); |
---|
[5ebff60] | 730 | if (!otr_update_modeflags(irc, u)) { |
---|
[f1cf01c] | 731 | char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!"; |
---|
[409c2de] | 732 | irc_usernotice(u, "otr connection has been refreshed (%s)", trust); |
---|
[f1cf01c] | 733 | } |
---|
[764c7d1] | 734 | } |
---|
| 735 | |
---|
[a13855a] | 736 | int op_max_message_size(void *opdata, ConnContext *context) |
---|
| 737 | { |
---|
[5a71d9c] | 738 | struct im_connection *ic = |
---|
[5ebff60] | 739 | check_imc(opdata, context->accountname, context->protocol); |
---|
| 740 | |
---|
[5a71d9c] | 741 | return ic->acc->prpl->mms; |
---|
[a13855a] | 742 | } |
---|
| 743 | |
---|
| 744 | const char *op_account_name(void *opdata, const char *account, const char *protocol) |
---|
| 745 | { |
---|
[5ebff60] | 746 | struct im_connection *ic = (struct im_connection *) opdata; |
---|
[ad2d8bc] | 747 | irc_t *irc = ic->bee->ui_data; |
---|
[a13855a] | 748 | |
---|
[ad2d8bc] | 749 | return peernick(irc, account, protocol); |
---|
[a13855a] | 750 | } |
---|
| 751 | |
---|
[e65039a] | 752 | void op_create_instag(void *opdata, const char *account, const char *protocol) |
---|
| 753 | { |
---|
| 754 | struct im_connection *ic = |
---|
[5ebff60] | 755 | check_imc(opdata, account, protocol); |
---|
[e65039a] | 756 | irc_t *irc = ic->bee->ui_data; |
---|
| 757 | gcry_error_t e; |
---|
| 758 | char s[512]; |
---|
[5ebff60] | 759 | |
---|
[e65039a] | 760 | g_snprintf(s, 511, "%s%s.otr_instags", global.conf->configdir, |
---|
[5ebff60] | 761 | irc->user->nick); |
---|
[e65039a] | 762 | e = otrl_instag_generate(irc->otr->us, s, account, protocol); |
---|
[5ebff60] | 763 | if (e) { |
---|
[e65039a] | 764 | irc_rootmsg(irc, "otr: %s/%s: otrl_instag_generate failed: %s", |
---|
[5ebff60] | 765 | account, protocol, gcry_strerror(e)); |
---|
[e65039a] | 766 | } |
---|
| 767 | } |
---|
| 768 | |
---|
[47ab9a9] | 769 | static char *otr_filter_colors(char *msg) |
---|
| 770 | { |
---|
| 771 | return str_reject_chars(msg, "\x02\x03", '?'); |
---|
[86fd261] | 772 | } |
---|
| 773 | |
---|
[654112d4] | 774 | /* returns newly allocated string */ |
---|
| 775 | static char *otr_color_encrypted(char *msg, char *color, gboolean is_query) { |
---|
| 776 | char **lines; |
---|
| 777 | GString *out; |
---|
| 778 | int i; |
---|
| 779 | |
---|
| 780 | lines = g_strsplit(msg, "\n", -1); |
---|
| 781 | |
---|
| 782 | /* up to 4 extra chars per line (e.g., '\x03' + ("03"|"05") + ' ') */ |
---|
| 783 | out = g_string_sized_new(strlen(msg) + g_strv_length(lines) * 4); |
---|
| 784 | |
---|
| 785 | for (i = 0; lines[i]; i++) { |
---|
| 786 | char *line = lines[i]; |
---|
| 787 | |
---|
| 788 | if (i != 0) { |
---|
| 789 | g_string_append_c(out, '\n'); |
---|
| 790 | |
---|
| 791 | } else if (is_query && g_strncasecmp(line, "/me ", 4) == 0) { |
---|
| 792 | /* in a query window, keep "/me " uncolored at the beginning */ |
---|
| 793 | line += 4; |
---|
| 794 | g_string_append(out, "/me "); |
---|
| 795 | } |
---|
| 796 | |
---|
| 797 | g_string_append_c(out, '\x03'); |
---|
| 798 | g_string_append(out, color); |
---|
| 799 | |
---|
| 800 | /* comma in first place could mess with the color code */ |
---|
| 801 | if (line[0] == ',') { |
---|
| 802 | /* insert a space between color spec and message */ |
---|
| 803 | g_string_append_c(out, ' '); |
---|
| 804 | } |
---|
| 805 | |
---|
[86fd261] | 806 | g_string_append(out, otr_filter_colors(line)); |
---|
[654112d4] | 807 | } |
---|
| 808 | |
---|
| 809 | g_strfreev(lines); |
---|
| 810 | |
---|
| 811 | return g_string_free(out, FALSE); |
---|
| 812 | } |
---|
| 813 | |
---|
[5d2bc9d] | 814 | void op_convert_msg(void *opdata, ConnContext *ctx, OtrlConvertType typ, |
---|
[5ebff60] | 815 | char **dst, const char *src) |
---|
[5d2bc9d] | 816 | { |
---|
| 817 | struct im_connection *ic = |
---|
[5ebff60] | 818 | check_imc(opdata, ctx->accountname, ctx->protocol); |
---|
[5d2bc9d] | 819 | irc_t *irc = ic->bee->ui_data; |
---|
| 820 | irc_user_t *iu = peeruser(irc, ctx->username, ctx->protocol); |
---|
| 821 | |
---|
[5ebff60] | 822 | if (typ == OTRL_CONVERT_RECEIVING) { |
---|
[5d2bc9d] | 823 | char *msg = g_strdup(src); |
---|
| 824 | |
---|
| 825 | /* HTML decoding */ |
---|
[5ebff60] | 826 | if (set_getbool(&ic->bee->set, "otr_does_html") && |
---|
| 827 | !(ic->flags & OPT_DOES_HTML) && |
---|
| 828 | set_getbool(&ic->bee->set, "strip_html")) { |
---|
[5d2bc9d] | 829 | strip_html(msg); |
---|
[654112d4] | 830 | |
---|
| 831 | /* msg is borrowed by *dst (unless the next if decides to color it) */ |
---|
[5d2bc9d] | 832 | *dst = msg; |
---|
| 833 | } |
---|
| 834 | |
---|
| 835 | /* coloring */ |
---|
[5ebff60] | 836 | if (set_getbool(&ic->bee->set, "otr_color_encrypted")) { |
---|
[5d2bc9d] | 837 | const char *trust = ctx->active_fingerprint->trust; |
---|
[654112d4] | 838 | char *color = (trust && *trust) ? OTR_COLOR_TRUSTED : OTR_COLOR_UNTRUSTED; |
---|
| 839 | gboolean is_query = (irc_user_msgdest(iu) == irc->user->nick); |
---|
[5d2bc9d] | 840 | |
---|
[654112d4] | 841 | /* the return value of otr_color_encrypted() is borrowed by *dst */ |
---|
| 842 | *dst = otr_color_encrypted(msg, color, is_query); |
---|
[5d2bc9d] | 843 | |
---|
[654112d4] | 844 | /* this branch doesn't need msg */ |
---|
| 845 | g_free(msg); |
---|
[5d2bc9d] | 846 | } |
---|
| 847 | } else { |
---|
| 848 | /* HTML encoding */ |
---|
| 849 | /* consider OTR plaintext to be HTML if otr_does_html is set */ |
---|
[5ebff60] | 850 | if (ctx && ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED && |
---|
| 851 | set_getbool(&ic->bee->set, "otr_does_html") && |
---|
| 852 | (g_strncasecmp(src, "<html>", 6) != 0)) { |
---|
[5d2bc9d] | 853 | *dst = escape_html(src); |
---|
| 854 | } |
---|
| 855 | } |
---|
| 856 | } |
---|
| 857 | |
---|
| 858 | void op_convert_free(void *opdata, ConnContext *ctx, char *msg) |
---|
| 859 | { |
---|
| 860 | g_free(msg); |
---|
| 861 | } |
---|
[5ebff60] | 862 | |
---|
[352a6b0] | 863 | /* Socialist Millionaires' Protocol */ |
---|
| 864 | void op_handle_smp_event(void *opdata, OtrlSMPEvent ev, ConnContext *ctx, |
---|
[5ebff60] | 865 | unsigned short percent, char *question) |
---|
[352a6b0] | 866 | { |
---|
| 867 | struct im_connection *ic = |
---|
[5ebff60] | 868 | check_imc(opdata, ctx->accountname, ctx->protocol); |
---|
[352a6b0] | 869 | irc_t *irc = ic->bee->ui_data; |
---|
| 870 | OtrlUserState us = irc->otr->us; |
---|
| 871 | irc_user_t *u = peeruser(irc, ctx->username, ctx->protocol); |
---|
| 872 | |
---|
[5ebff60] | 873 | if (!u) { |
---|
| 874 | return; |
---|
| 875 | } |
---|
[352a6b0] | 876 | |
---|
[5ebff60] | 877 | switch (ev) { |
---|
[352a6b0] | 878 | case OTRL_SMPEVENT_ASK_FOR_SECRET: |
---|
| 879 | irc_rootmsg(irc, "smp: initiated by %s" |
---|
[5ebff60] | 880 | " - respond with \x02otr smp %s <secret>\x02", |
---|
| 881 | u->nick, u->nick); |
---|
[352a6b0] | 882 | break; |
---|
| 883 | case OTRL_SMPEVENT_ASK_FOR_ANSWER: |
---|
| 884 | irc_rootmsg(irc, "smp: initiated by %s with question: \x02\"%s\"\x02", u->nick, |
---|
[5ebff60] | 885 | question); |
---|
[352a6b0] | 886 | irc_rootmsg(irc, "smp: respond with \x02otr smp %s <answer>\x02", |
---|
[5ebff60] | 887 | u->nick); |
---|
[352a6b0] | 888 | break; |
---|
| 889 | case OTRL_SMPEVENT_CHEATED: |
---|
| 890 | irc_rootmsg(irc, "smp %s: opponent violated protocol, aborting", |
---|
[5ebff60] | 891 | u->nick); |
---|
[352a6b0] | 892 | otrl_message_abort_smp(us, &otr_ops, u->bu->ic, ctx); |
---|
| 893 | otrl_sm_state_free(ctx->smstate); |
---|
| 894 | break; |
---|
| 895 | case OTRL_SMPEVENT_NONE: |
---|
| 896 | break; |
---|
| 897 | case OTRL_SMPEVENT_IN_PROGRESS: |
---|
| 898 | break; |
---|
| 899 | case OTRL_SMPEVENT_SUCCESS: |
---|
[5ebff60] | 900 | if (ctx->smstate->received_question) { |
---|
[352a6b0] | 901 | irc_rootmsg(irc, "smp %s: correct answer, you are trusted", |
---|
[5ebff60] | 902 | u->nick); |
---|
[352a6b0] | 903 | } else { |
---|
| 904 | irc_rootmsg(irc, "smp %s: secrets proved equal, fingerprint trusted", |
---|
[5ebff60] | 905 | u->nick); |
---|
[352a6b0] | 906 | } |
---|
| 907 | otrl_sm_state_free(ctx->smstate); |
---|
| 908 | break; |
---|
| 909 | case OTRL_SMPEVENT_FAILURE: |
---|
[5ebff60] | 910 | if (ctx->smstate->received_question) { |
---|
[352a6b0] | 911 | irc_rootmsg(irc, "smp %s: wrong answer, you are not trusted", |
---|
[5ebff60] | 912 | u->nick); |
---|
[352a6b0] | 913 | } else { |
---|
| 914 | irc_rootmsg(irc, "smp %s: secrets did not match, fingerprint not trusted", |
---|
[5ebff60] | 915 | u->nick); |
---|
[352a6b0] | 916 | } |
---|
| 917 | otrl_sm_state_free(ctx->smstate); |
---|
| 918 | break; |
---|
| 919 | case OTRL_SMPEVENT_ABORT: |
---|
[5ebff60] | 920 | irc_rootmsg(irc, "smp: received abort from %s", u->nick); |
---|
[352a6b0] | 921 | otrl_sm_state_free(ctx->smstate); |
---|
| 922 | break; |
---|
| 923 | case OTRL_SMPEVENT_ERROR: |
---|
| 924 | irc_rootmsg(irc, "smp %s: protocol error, aborting", |
---|
[5ebff60] | 925 | u->nick); |
---|
[352a6b0] | 926 | otrl_message_abort_smp(us, &otr_ops, u->bu->ic, ctx); |
---|
| 927 | otrl_sm_state_free(ctx->smstate); |
---|
| 928 | break; |
---|
| 929 | } |
---|
| 930 | } |
---|
| 931 | |
---|
[090c9b7] | 932 | void op_handle_msg_event(void *opdata, OtrlMessageEvent ev, ConnContext *ctx, |
---|
[5ebff60] | 933 | const char *message, gcry_error_t err) |
---|
[090c9b7] | 934 | { |
---|
[5ebff60] | 935 | switch (ev) { |
---|
[090c9b7] | 936 | case OTRL_MSGEVENT_ENCRYPTION_REQUIRED: |
---|
| 937 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 938 | "policy requires encryption - message not sent"); |
---|
[090c9b7] | 939 | break; |
---|
| 940 | case OTRL_MSGEVENT_ENCRYPTION_ERROR: |
---|
| 941 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 942 | "error during encryption - message not sent"); |
---|
[090c9b7] | 943 | break; |
---|
| 944 | case OTRL_MSGEVENT_CONNECTION_ENDED: |
---|
| 945 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 946 | "other end has disconnected OTR - " |
---|
| 947 | "close connection or reconnect!"); |
---|
[090c9b7] | 948 | break; |
---|
| 949 | case OTRL_MSGEVENT_SETUP_ERROR: |
---|
| 950 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 951 | "OTR connection failed: %s", gcry_strerror(err)); |
---|
[090c9b7] | 952 | break; |
---|
| 953 | case OTRL_MSGEVENT_MSG_REFLECTED: |
---|
| 954 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 955 | "received our own OTR message (!?)"); |
---|
[090c9b7] | 956 | break; |
---|
| 957 | case OTRL_MSGEVENT_MSG_RESENT: |
---|
| 958 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 959 | "the previous message was resent"); |
---|
[090c9b7] | 960 | break; |
---|
| 961 | case OTRL_MSGEVENT_RCVDMSG_NOT_IN_PRIVATE: |
---|
| 962 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 963 | "unexpected encrypted message received"); |
---|
[090c9b7] | 964 | break; |
---|
| 965 | case OTRL_MSGEVENT_RCVDMSG_UNREADABLE: |
---|
| 966 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 967 | "unreadable encrypted message received"); |
---|
[090c9b7] | 968 | break; |
---|
| 969 | case OTRL_MSGEVENT_RCVDMSG_MALFORMED: |
---|
| 970 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 971 | "malformed OTR message received"); |
---|
[090c9b7] | 972 | break; |
---|
[51f937e] | 973 | case OTRL_MSGEVENT_LOG_HEARTBEAT_RCVD: |
---|
[5ebff60] | 974 | if (global.conf->verbose) { |
---|
[51f937e] | 975 | log_otr_message(opdata, "%s/%s: heartbeat received", |
---|
[5ebff60] | 976 | ctx->accountname, ctx->protocol); |
---|
[51f937e] | 977 | } |
---|
| 978 | break; |
---|
| 979 | case OTRL_MSGEVENT_LOG_HEARTBEAT_SENT: |
---|
[5ebff60] | 980 | if (global.conf->verbose) { |
---|
[51f937e] | 981 | log_otr_message(opdata, "%s/%s: heartbeat sent", |
---|
[5ebff60] | 982 | ctx->accountname, ctx->protocol); |
---|
[51f937e] | 983 | } |
---|
| 984 | break; |
---|
[090c9b7] | 985 | case OTRL_MSGEVENT_RCVDMSG_GENERAL_ERR: |
---|
| 986 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 987 | "OTR error message received: %s", message); |
---|
[090c9b7] | 988 | break; |
---|
| 989 | case OTRL_MSGEVENT_RCVDMSG_UNENCRYPTED: |
---|
| 990 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 991 | "unencrypted message received: %s", message); |
---|
[090c9b7] | 992 | break; |
---|
| 993 | case OTRL_MSGEVENT_RCVDMSG_UNRECOGNIZED: |
---|
| 994 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 995 | "unrecognized OTR message received"); |
---|
[090c9b7] | 996 | break; |
---|
[e4752a6] | 997 | case OTRL_MSGEVENT_RCVDMSG_FOR_OTHER_INSTANCE: |
---|
| 998 | display_otr_message(opdata, ctx, |
---|
[5ebff60] | 999 | "OTR message for a different instance received"); |
---|
[e4752a6] | 1000 | break; |
---|
[090c9b7] | 1001 | default: |
---|
[51f937e] | 1002 | /* shouldn't happen */ |
---|
[090c9b7] | 1003 | break; |
---|
| 1004 | } |
---|
| 1005 | } |
---|
| 1006 | |
---|
[6d9f0ba] | 1007 | const char *op_otr_error_message(void *opdata, ConnContext *ctx, |
---|
[5ebff60] | 1008 | OtrlErrorCode err_code) |
---|
[6d9f0ba] | 1009 | { |
---|
[5ebff60] | 1010 | switch (err_code) { |
---|
[6d9f0ba] | 1011 | case OTRL_ERRCODE_ENCRYPTION_ERROR: |
---|
| 1012 | return "i failed to encrypt a message"; |
---|
| 1013 | case OTRL_ERRCODE_MSG_NOT_IN_PRIVATE: |
---|
| 1014 | return "you sent an encrypted message i didn't expect"; |
---|
| 1015 | case OTRL_ERRCODE_MSG_UNREADABLE: |
---|
| 1016 | return "could not read encrypted message"; |
---|
| 1017 | case OTRL_ERRCODE_MSG_MALFORMED: |
---|
| 1018 | return "you sent a malformed OTR message"; |
---|
| 1019 | default: |
---|
| 1020 | return "i suffered an unexpected OTR error"; |
---|
| 1021 | } |
---|
| 1022 | } |
---|
| 1023 | |
---|
[5d2bc9d] | 1024 | |
---|
[764c7d1] | 1025 | |
---|
| 1026 | /*** OTR sub-command handlers ***/ |
---|
| 1027 | |
---|
[69ef042] | 1028 | void cmd_otr_reconnect(irc_t *irc, char **args) |
---|
| 1029 | { |
---|
| 1030 | cmd_otr_disconnect(irc, args); |
---|
| 1031 | cmd_otr_connect(irc, args); |
---|
| 1032 | } |
---|
| 1033 | |
---|
[8521b02] | 1034 | void cmd_otr_disconnect(irc_t *irc, char **args) |
---|
[764c7d1] | 1035 | { |
---|
[ad2d8bc] | 1036 | irc_user_t *u; |
---|
[764c7d1] | 1037 | |
---|
[5ebff60] | 1038 | if (!strcmp("*", args[1])) { |
---|
[c239fff] | 1039 | otr_disconnect_all(irc); |
---|
| 1040 | irc_rootmsg(irc, "all conversations are now in cleartext"); |
---|
| 1041 | } else { |
---|
| 1042 | u = irc_user_by_name(irc, args[1]); |
---|
[5ebff60] | 1043 | if (otr_disconnect_user(irc, u)) { |
---|
[c239fff] | 1044 | irc_usernotice(u, "conversation is now in cleartext"); |
---|
[5ebff60] | 1045 | } else { |
---|
[c239fff] | 1046 | irc_rootmsg(irc, "%s: unknown user", args[1]); |
---|
[5ebff60] | 1047 | } |
---|
[c595308] | 1048 | } |
---|
[764c7d1] | 1049 | } |
---|
| 1050 | |
---|
[8521b02] | 1051 | void cmd_otr_connect(irc_t *irc, char **args) |
---|
[764c7d1] | 1052 | { |
---|
[ad2d8bc] | 1053 | irc_user_t *u; |
---|
[820a2a7] | 1054 | char *msg, *query = "?OTR?"; |
---|
[764c7d1] | 1055 | |
---|
[ad2d8bc] | 1056 | u = irc_user_by_name(irc, args[1]); |
---|
[5ebff60] | 1057 | if (!u || !u->bu || !u->bu->ic) { |
---|
[e67e513] | 1058 | irc_rootmsg(irc, "%s: unknown user", args[1]); |
---|
[764c7d1] | 1059 | return; |
---|
| 1060 | } |
---|
[5ebff60] | 1061 | if (!(u->bu->flags & BEE_USER_ONLINE)) { |
---|
[e67e513] | 1062 | irc_rootmsg(irc, "%s is offline", args[1]); |
---|
[764c7d1] | 1063 | return; |
---|
| 1064 | } |
---|
[5ebff60] | 1065 | |
---|
[fbcb481] | 1066 | /* passing this through the filter so it goes through libotr which |
---|
| 1067 | * will replace the simple query string with a proper one */ |
---|
[820a2a7] | 1068 | msg = otr_filter_msg_out(u, query, 0); |
---|
| 1069 | |
---|
| 1070 | /* send the message */ |
---|
[5ebff60] | 1071 | if (msg) { |
---|
[820a2a7] | 1072 | u->bu->ic->acc->prpl->buddy_msg(u->bu->ic, u->bu->handle, msg, 0); /* XXX flags? */ |
---|
| 1073 | /* XXX error message? */ |
---|
| 1074 | |
---|
[5ebff60] | 1075 | if (msg != query) { |
---|
[820a2a7] | 1076 | g_free(msg); |
---|
[5ebff60] | 1077 | } |
---|
[820a2a7] | 1078 | } |
---|
[764c7d1] | 1079 | } |
---|
| 1080 | |
---|
[5a71d9c] | 1081 | void cmd_otr_smp(irc_t *irc, char **args) |
---|
[764c7d1] | 1082 | { |
---|
[5ebff60] | 1083 | otr_smp_or_smpq(irc, args[1], NULL, args[2]); /* no question */ |
---|
[99a01b9] | 1084 | } |
---|
[764c7d1] | 1085 | |
---|
[99a01b9] | 1086 | void cmd_otr_smpq(irc_t *irc, char **args) |
---|
| 1087 | { |
---|
[7c91392] | 1088 | otr_smp_or_smpq(irc, args[1], args[2], args[3]); |
---|
[764c7d1] | 1089 | } |
---|
| 1090 | |
---|
[5a71d9c] | 1091 | void cmd_otr_trust(irc_t *irc, char **args) |
---|
| 1092 | { |
---|
[ad2d8bc] | 1093 | irc_user_t *u; |
---|
[5a71d9c] | 1094 | ConnContext *ctx; |
---|
| 1095 | unsigned char raw[20]; |
---|
| 1096 | Fingerprint *fp; |
---|
[5ebff60] | 1097 | int i, j; |
---|
| 1098 | |
---|
[ad2d8bc] | 1099 | u = irc_user_by_name(irc, args[1]); |
---|
[5ebff60] | 1100 | if (!u || !u->bu || !u->bu->ic) { |
---|
[e67e513] | 1101 | irc_rootmsg(irc, "%s: unknown user", args[1]); |
---|
[5a71d9c] | 1102 | return; |
---|
| 1103 | } |
---|
[5ebff60] | 1104 | |
---|
[ad2d8bc] | 1105 | ctx = otrl_context_find(irc->otr->us, u->bu->handle, |
---|
[5ebff60] | 1106 | u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, |
---|
| 1107 | NULL); |
---|
| 1108 | if (!ctx) { |
---|
[e67e513] | 1109 | irc_rootmsg(irc, "%s: no otr context with user", args[1]); |
---|
[5a71d9c] | 1110 | return; |
---|
| 1111 | } |
---|
[5ebff60] | 1112 | |
---|
[5a71d9c] | 1113 | /* convert given fingerprint to raw representation */ |
---|
[5ebff60] | 1114 | for (i = 0; i < 5; i++) { |
---|
| 1115 | for (j = 0; j < 4; j++) { |
---|
| 1116 | char *p = args[2 + i] + (2 * j); |
---|
| 1117 | char *q = p + 1; |
---|
[5a71d9c] | 1118 | int x, y; |
---|
[5ebff60] | 1119 | |
---|
| 1120 | if (!*p || !*q) { |
---|
| 1121 | irc_rootmsg(irc, "failed: truncated fingerprint block %d", i + 1); |
---|
[5a71d9c] | 1122 | return; |
---|
| 1123 | } |
---|
[5ebff60] | 1124 | |
---|
[5a71d9c] | 1125 | x = hexval(*p); |
---|
| 1126 | y = hexval(*q); |
---|
[5ebff60] | 1127 | if (x < 0) { |
---|
| 1128 | irc_rootmsg(irc, "failed: %d. hex digit of block %d out of range", 2 * j + 1, i + 1); |
---|
[5a71d9c] | 1129 | return; |
---|
| 1130 | } |
---|
[5ebff60] | 1131 | if (y < 0) { |
---|
| 1132 | irc_rootmsg(irc, "failed: %d. hex digit of block %d out of range", 2 * j + 2, i + 1); |
---|
[5a71d9c] | 1133 | return; |
---|
| 1134 | } |
---|
| 1135 | |
---|
[5ebff60] | 1136 | raw[i * 4 + j] = x * 16 + y; |
---|
[5a71d9c] | 1137 | } |
---|
| 1138 | } |
---|
| 1139 | fp = otrl_context_find_fingerprint(ctx, raw, 0, NULL); |
---|
[5ebff60] | 1140 | if (!fp) { |
---|
[e67e513] | 1141 | irc_rootmsg(irc, "failed: no such fingerprint for %s", args[1]); |
---|
[5a71d9c] | 1142 | } else { |
---|
| 1143 | char *trust = args[7] ? args[7] : "affirmed"; |
---|
| 1144 | otrl_context_set_trust(fp, trust); |
---|
[e67e513] | 1145 | irc_rootmsg(irc, "fingerprint match, trust set to \"%s\"", trust); |
---|
[5ebff60] | 1146 | if (u->flags & IRC_USER_OTR_ENCRYPTED) { |
---|
[ad2d8bc] | 1147 | u->flags |= IRC_USER_OTR_TRUSTED; |
---|
[5ebff60] | 1148 | } |
---|
[5a71d9c] | 1149 | otr_update_modeflags(irc, u); |
---|
| 1150 | } |
---|
| 1151 | } |
---|
| 1152 | |
---|
[8521b02] | 1153 | void cmd_otr_info(irc_t *irc, char **args) |
---|
[764c7d1] | 1154 | { |
---|
[5ebff60] | 1155 | if (!args[1]) { |
---|
[8521b02] | 1156 | show_general_otr_info(irc); |
---|
[764c7d1] | 1157 | } else { |
---|
[8521b02] | 1158 | char *arg = g_strdup(args[1]); |
---|
[5ebff60] | 1159 | char *myhandle, *handle = NULL, *protocol; |
---|
[2c81d15] | 1160 | ConnContext *bestctx = NULL, *ctx; |
---|
[5ebff60] | 1161 | |
---|
[8521b02] | 1162 | /* interpret arg as 'user/protocol/account' if possible */ |
---|
| 1163 | protocol = strchr(arg, '/'); |
---|
[9e64011] | 1164 | myhandle = NULL; |
---|
[5ebff60] | 1165 | if (protocol) { |
---|
[8521b02] | 1166 | *(protocol++) = '\0'; |
---|
| 1167 | myhandle = strchr(protocol, '/'); |
---|
[764c7d1] | 1168 | } |
---|
[5ebff60] | 1169 | if (protocol && myhandle) { |
---|
[8521b02] | 1170 | *(myhandle++) = '\0'; |
---|
| 1171 | handle = arg; |
---|
[5ebff60] | 1172 | ctx = otrl_context_find(irc->otr->us, handle, myhandle, protocol, OTRL_INSTAG_MASTER, 0, NULL, |
---|
| 1173 | NULL, NULL); |
---|
| 1174 | if (!ctx) { |
---|
[e67e513] | 1175 | irc_rootmsg(irc, "no such context"); |
---|
[8521b02] | 1176 | g_free(arg); |
---|
| 1177 | return; |
---|
| 1178 | } |
---|
[764c7d1] | 1179 | } else { |
---|
[ad2d8bc] | 1180 | irc_user_t *u = irc_user_by_name(irc, args[1]); |
---|
[5ebff60] | 1181 | if (!u || !u->bu || !u->bu->ic) { |
---|
[e67e513] | 1182 | irc_rootmsg(irc, "%s: unknown user", args[1]); |
---|
[8521b02] | 1183 | g_free(arg); |
---|
| 1184 | return; |
---|
[5a71d9c] | 1185 | } |
---|
[ad2d8bc] | 1186 | ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user, |
---|
[5ebff60] | 1187 | u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); |
---|
| 1188 | if (!ctx) { |
---|
[e67e513] | 1189 | irc_rootmsg(irc, "no otr context with %s", args[1]); |
---|
[8521b02] | 1190 | g_free(arg); |
---|
| 1191 | return; |
---|
| 1192 | } |
---|
[2c81d15] | 1193 | /* This does no harm if it returns NULL */ |
---|
| 1194 | bestctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user, |
---|
| 1195 | u->bu->ic->acc->prpl->name, OTRL_INSTAG_BEST, 0, NULL, NULL, NULL); |
---|
[8521b02] | 1196 | } |
---|
[5ebff60] | 1197 | |
---|
[8521b02] | 1198 | /* show how we resolved the (nick) argument, if we did */ |
---|
[5ebff60] | 1199 | if (handle != arg) { |
---|
[2c81d15] | 1200 | irc_rootmsg(irc, "%s:", args[1]); |
---|
| 1201 | irc_rootmsg(irc, " they are: %s/%s", ctx->username, ctx->protocol); |
---|
| 1202 | irc_rootmsg(irc, " we are: %s/%s", ctx->accountname, ctx->protocol); |
---|
[764c7d1] | 1203 | } |
---|
[2c81d15] | 1204 | show_otr_context_info(irc, ctx, bestctx); |
---|
[8521b02] | 1205 | g_free(arg); |
---|
[764c7d1] | 1206 | } |
---|
| 1207 | } |
---|
| 1208 | |
---|
[94e7eb3] | 1209 | void cmd_otr_keygen(irc_t *irc, char **args) |
---|
| 1210 | { |
---|
| 1211 | account_t *a; |
---|
[5ebff60] | 1212 | |
---|
[eabe6d4] | 1213 | if ((a = account_get(irc->b, args[1])) == NULL) { |
---|
| 1214 | irc_rootmsg(irc, "Could not find account `%s'.", args[1]); |
---|
[94e7eb3] | 1215 | return; |
---|
| 1216 | } |
---|
[5ebff60] | 1217 | |
---|
| 1218 | if (keygen_in_progress(irc, a->user, a->prpl->name)) { |
---|
[eabe6d4] | 1219 | irc_rootmsg(irc, "keygen for account `%s' already in progress", a->tag); |
---|
[dc9797f] | 1220 | return; |
---|
| 1221 | } |
---|
[5ebff60] | 1222 | |
---|
| 1223 | if (otrl_privkey_find(irc->otr->us, a->user, a->prpl->name)) { |
---|
[eabe6d4] | 1224 | char *s = g_strdup_printf("account `%s' already has a key, replace it?", a->tag); |
---|
[ad2d8bc] | 1225 | query_add(irc, NULL, s, yes_keygen, NULL, NULL, a); |
---|
[82e8fe8] | 1226 | g_free(s); |
---|
[94e7eb3] | 1227 | } else { |
---|
| 1228 | otr_keygen(irc, a->user, a->prpl->name); |
---|
| 1229 | } |
---|
| 1230 | } |
---|
| 1231 | |
---|
[6738a67] | 1232 | void yes_forget_fingerprint(void *data) |
---|
[c595308] | 1233 | { |
---|
[5ebff60] | 1234 | pair_t *p = (pair_t *) data; |
---|
| 1235 | irc_t *irc = (irc_t *) p->fst; |
---|
| 1236 | Fingerprint *fp = (Fingerprint *) p->snd; |
---|
[6738a67] | 1237 | |
---|
| 1238 | g_free(p); |
---|
[5ebff60] | 1239 | |
---|
| 1240 | if (fp == fp->context->active_fingerprint) { |
---|
[e67e513] | 1241 | irc_rootmsg(irc, "that fingerprint is active, terminate otr connection first"); |
---|
[c595308] | 1242 | return; |
---|
| 1243 | } |
---|
[5ebff60] | 1244 | |
---|
[c595308] | 1245 | otrl_context_forget_fingerprint(fp, 0); |
---|
| 1246 | } |
---|
| 1247 | |
---|
[6738a67] | 1248 | void yes_forget_context(void *data) |
---|
[c595308] | 1249 | { |
---|
[5ebff60] | 1250 | pair_t *p = (pair_t *) data; |
---|
| 1251 | irc_t *irc = (irc_t *) p->fst; |
---|
| 1252 | ConnContext *ctx = (ConnContext *) p->snd; |
---|
[6738a67] | 1253 | |
---|
| 1254 | g_free(p); |
---|
[f93e01c] | 1255 | |
---|
| 1256 | // XXX forget all contexts |
---|
[5ebff60] | 1257 | |
---|
| 1258 | if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { |
---|
[e67e513] | 1259 | irc_rootmsg(irc, "active otr connection with %s, terminate it first", |
---|
[5ebff60] | 1260 | peernick(irc, ctx->username, ctx->protocol)); |
---|
[c595308] | 1261 | return; |
---|
| 1262 | } |
---|
[5ebff60] | 1263 | |
---|
| 1264 | if (ctx->msgstate == OTRL_MSGSTATE_FINISHED) { |
---|
[c595308] | 1265 | otrl_context_force_plaintext(ctx); |
---|
[5ebff60] | 1266 | } |
---|
[c595308] | 1267 | otrl_context_forget(ctx); |
---|
| 1268 | } |
---|
| 1269 | |
---|
[6738a67] | 1270 | void yes_forget_key(void *data) |
---|
[d0faf62] | 1271 | { |
---|
[5ebff60] | 1272 | OtrlPrivKey *key = (OtrlPrivKey *) data; |
---|
| 1273 | |
---|
[d0faf62] | 1274 | otrl_privkey_forget(key); |
---|
[37bff51] | 1275 | /* Hm, libotr doesn't seem to offer a function for explicitly /writing/ |
---|
| 1276 | keyfiles. So the key will be back on the next load... */ |
---|
| 1277 | /* TODO: Actually erase forgotten keys from storage? */ |
---|
[d0faf62] | 1278 | } |
---|
| 1279 | |
---|
[c595308] | 1280 | void cmd_otr_forget(irc_t *irc, char **args) |
---|
| 1281 | { |
---|
[5ebff60] | 1282 | if (!strcmp(args[1], "fingerprint")) { |
---|
[ad2d8bc] | 1283 | irc_user_t *u; |
---|
[c595308] | 1284 | ConnContext *ctx; |
---|
| 1285 | Fingerprint *fp; |
---|
| 1286 | char human[54]; |
---|
| 1287 | char *s; |
---|
[6738a67] | 1288 | pair_t *p; |
---|
[5ebff60] | 1289 | |
---|
| 1290 | if (!args[3]) { |
---|
[e67e513] | 1291 | irc_rootmsg(irc, "otr %s %s: not enough arguments (2 req.)", args[0], args[1]); |
---|
[c595308] | 1292 | return; |
---|
| 1293 | } |
---|
[5ebff60] | 1294 | |
---|
[e2b15bb] | 1295 | /* TODO: allow context specs ("user/proto/account") in 'otr forget fingerprint'? */ |
---|
[ad2d8bc] | 1296 | u = irc_user_by_name(irc, args[2]); |
---|
[5ebff60] | 1297 | if (!u || !u->bu || !u->bu->ic) { |
---|
[e67e513] | 1298 | irc_rootmsg(irc, "%s: unknown user", args[2]); |
---|
[c595308] | 1299 | return; |
---|
| 1300 | } |
---|
[5ebff60] | 1301 | |
---|
[ad2d8bc] | 1302 | ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user, |
---|
[5ebff60] | 1303 | u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); |
---|
| 1304 | if (!ctx) { |
---|
[e67e513] | 1305 | irc_rootmsg(irc, "no otr context with %s", args[2]); |
---|
[c595308] | 1306 | return; |
---|
| 1307 | } |
---|
[5ebff60] | 1308 | |
---|
| 1309 | fp = match_fingerprint(irc, ctx, ((const char **) args) + 3); |
---|
| 1310 | if (!fp) { |
---|
[c595308] | 1311 | /* match_fingerprint does error messages */ |
---|
| 1312 | return; |
---|
| 1313 | } |
---|
[5ebff60] | 1314 | |
---|
| 1315 | if (fp == ctx->active_fingerprint) { |
---|
[e67e513] | 1316 | irc_rootmsg(irc, "that fingerprint is active, terminate otr connection first"); |
---|
[c595308] | 1317 | return; |
---|
| 1318 | } |
---|
[5ebff60] | 1319 | |
---|
[c595308] | 1320 | otrl_privkey_hash_to_human(human, fp->fingerprint); |
---|
| 1321 | s = g_strdup_printf("about to forget fingerprint %s, are you sure?", human); |
---|
[6738a67] | 1322 | p = g_malloc(sizeof(pair_t)); |
---|
[5ebff60] | 1323 | if (!p) { |
---|
[6738a67] | 1324 | return; |
---|
[5ebff60] | 1325 | } |
---|
[6738a67] | 1326 | p->fst = irc; |
---|
| 1327 | p->snd = fp; |
---|
[ad2d8bc] | 1328 | query_add(irc, NULL, s, yes_forget_fingerprint, NULL, NULL, p); |
---|
[82e8fe8] | 1329 | g_free(s); |
---|
[5ebff60] | 1330 | } else if (!strcmp(args[1], "context")) { |
---|
[ad2d8bc] | 1331 | irc_user_t *u; |
---|
[c595308] | 1332 | ConnContext *ctx; |
---|
| 1333 | char *s; |
---|
[6738a67] | 1334 | pair_t *p; |
---|
[5ebff60] | 1335 | |
---|
[e2b15bb] | 1336 | /* TODO: allow context specs ("user/proto/account") in 'otr forget contex'? */ |
---|
[ad2d8bc] | 1337 | u = irc_user_by_name(irc, args[2]); |
---|
[5ebff60] | 1338 | if (!u || !u->bu || !u->bu->ic) { |
---|
[e67e513] | 1339 | irc_rootmsg(irc, "%s: unknown user", args[2]); |
---|
[c595308] | 1340 | return; |
---|
| 1341 | } |
---|
[5ebff60] | 1342 | |
---|
[ad2d8bc] | 1343 | ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user, |
---|
[5ebff60] | 1344 | u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); |
---|
| 1345 | if (!ctx) { |
---|
[e67e513] | 1346 | irc_rootmsg(irc, "no otr context with %s", args[2]); |
---|
[c595308] | 1347 | return; |
---|
| 1348 | } |
---|
[5ebff60] | 1349 | |
---|
| 1350 | if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { |
---|
[e67e513] | 1351 | irc_rootmsg(irc, "active otr connection with %s, terminate it first", args[2]); |
---|
[c595308] | 1352 | return; |
---|
| 1353 | } |
---|
[5ebff60] | 1354 | |
---|
[c595308] | 1355 | s = g_strdup_printf("about to forget otr data about %s, are you sure?", args[2]); |
---|
[6738a67] | 1356 | p = g_malloc(sizeof(pair_t)); |
---|
[5ebff60] | 1357 | if (!p) { |
---|
[6738a67] | 1358 | return; |
---|
[5ebff60] | 1359 | } |
---|
[6738a67] | 1360 | p->fst = irc; |
---|
| 1361 | p->snd = ctx; |
---|
[ad2d8bc] | 1362 | query_add(irc, NULL, s, yes_forget_context, NULL, NULL, p); |
---|
[82e8fe8] | 1363 | g_free(s); |
---|
[5ebff60] | 1364 | } else if (!strcmp(args[1], "key")) { |
---|
[d0faf62] | 1365 | OtrlPrivKey *key; |
---|
| 1366 | char *s; |
---|
[5ebff60] | 1367 | |
---|
| 1368 | key = match_privkey(irc, ((const char **) args) + 2); |
---|
| 1369 | if (!key) { |
---|
[d0faf62] | 1370 | /* match_privkey does error messages */ |
---|
| 1371 | return; |
---|
| 1372 | } |
---|
[5ebff60] | 1373 | |
---|
[d0faf62] | 1374 | s = g_strdup_printf("about to forget the private key for %s/%s, are you sure?", |
---|
[5ebff60] | 1375 | key->accountname, key->protocol); |
---|
[ad2d8bc] | 1376 | query_add(irc, NULL, s, yes_forget_key, NULL, NULL, key); |
---|
[d0faf62] | 1377 | g_free(s); |
---|
[5ebff60] | 1378 | } else { |
---|
[e67e513] | 1379 | irc_rootmsg(irc, "otr %s: unknown subcommand \"%s\", see \x02help otr forget\x02", |
---|
[5ebff60] | 1380 | args[0], args[1]); |
---|
[c595308] | 1381 | } |
---|
| 1382 | } |
---|
| 1383 | |
---|
[764c7d1] | 1384 | |
---|
| 1385 | /*** local helpers / subroutines: ***/ |
---|
| 1386 | |
---|
[090c9b7] | 1387 | void log_otr_message(void *opdata, const char *fmt, ...) |
---|
| 1388 | { |
---|
| 1389 | va_list va; |
---|
| 1390 | |
---|
| 1391 | va_start(va, fmt); |
---|
| 1392 | char *msg = g_strdup_vprintf(fmt, va); |
---|
| 1393 | va_end(va); |
---|
[5ebff60] | 1394 | |
---|
[090c9b7] | 1395 | log_message(LOGLVL_INFO, "otr: %s", msg); |
---|
[098a75b] | 1396 | |
---|
| 1397 | g_free(msg); |
---|
[090c9b7] | 1398 | } |
---|
| 1399 | |
---|
| 1400 | void display_otr_message(void *opdata, ConnContext *ctx, const char *fmt, ...) |
---|
| 1401 | { |
---|
| 1402 | struct im_connection *ic = |
---|
[5ebff60] | 1403 | check_imc(opdata, ctx->accountname, ctx->protocol); |
---|
[090c9b7] | 1404 | irc_t *irc = ic->bee->ui_data; |
---|
| 1405 | irc_user_t *u = peeruser(irc, ctx->username, ctx->protocol); |
---|
| 1406 | va_list va; |
---|
| 1407 | |
---|
| 1408 | va_start(va, fmt); |
---|
| 1409 | char *msg = g_strdup_vprintf(fmt, va); |
---|
| 1410 | va_end(va); |
---|
| 1411 | |
---|
[5ebff60] | 1412 | if (u) { |
---|
[0a5d03c] | 1413 | /* just show this as a regular message */ |
---|
| 1414 | irc_usermsg(u, "<<\002OTR\002>> %s", msg); |
---|
[090c9b7] | 1415 | } else { |
---|
| 1416 | irc_rootmsg(irc, "[otr] %s", msg); |
---|
| 1417 | } |
---|
| 1418 | |
---|
| 1419 | g_free(msg); |
---|
| 1420 | } |
---|
| 1421 | |
---|
[99a01b9] | 1422 | /* combined handler for the 'otr smp' and 'otr smpq' commands */ |
---|
[7c91392] | 1423 | void otr_smp_or_smpq(irc_t *irc, const char *nick, const char *question, |
---|
[5ebff60] | 1424 | const char *secret) |
---|
[99a01b9] | 1425 | { |
---|
| 1426 | irc_user_t *u; |
---|
| 1427 | ConnContext *ctx; |
---|
[329f9fe] | 1428 | otrl_instag_t instag = OTRL_INSTAG_BEST; // XXX |
---|
[99a01b9] | 1429 | |
---|
| 1430 | u = irc_user_by_name(irc, nick); |
---|
[5ebff60] | 1431 | if (!u || !u->bu || !u->bu->ic) { |
---|
[e67e513] | 1432 | irc_rootmsg(irc, "%s: unknown user", nick); |
---|
[99a01b9] | 1433 | return; |
---|
| 1434 | } |
---|
[5ebff60] | 1435 | if (!(u->bu->flags & BEE_USER_ONLINE)) { |
---|
[e67e513] | 1436 | irc_rootmsg(irc, "%s is offline", nick); |
---|
[99a01b9] | 1437 | return; |
---|
| 1438 | } |
---|
[5ebff60] | 1439 | |
---|
[99a01b9] | 1440 | ctx = otrl_context_find(irc->otr->us, u->bu->handle, |
---|
[5ebff60] | 1441 | u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, instag, 0, NULL, NULL, NULL); |
---|
| 1442 | if (!ctx || ctx->msgstate != OTRL_MSGSTATE_ENCRYPTED) { |
---|
[e67e513] | 1443 | irc_rootmsg(irc, "smp: otr inactive with %s, try \x02otr connect" |
---|
[5ebff60] | 1444 | " %s\x02", nick, nick); |
---|
[99a01b9] | 1445 | return; |
---|
| 1446 | } |
---|
| 1447 | |
---|
[5ebff60] | 1448 | if (ctx->smstate->nextExpected != OTRL_SMP_EXPECT1) { |
---|
[99a01b9] | 1449 | log_message(LOGLVL_INFO, |
---|
[5ebff60] | 1450 | "SMP already in phase %d, sending abort before reinitiating", |
---|
| 1451 | ctx->smstate->nextExpected + 1); |
---|
[99a01b9] | 1452 | otrl_message_abort_smp(irc->otr->us, &otr_ops, u->bu->ic, ctx); |
---|
| 1453 | otrl_sm_state_free(ctx->smstate); |
---|
| 1454 | } |
---|
[944d7a5] | 1455 | |
---|
[5ebff60] | 1456 | if (question) { |
---|
[9cc653c] | 1457 | /* this was 'otr smpq', just initiate */ |
---|
[e67e513] | 1458 | irc_rootmsg(irc, "smp: initiating with %s...", u->nick); |
---|
[9cc653c] | 1459 | otrl_message_initiate_smp_q(irc->otr->us, &otr_ops, u->bu->ic, ctx, |
---|
[5ebff60] | 1460 | question, (unsigned char *) secret, strlen(secret)); |
---|
[9cc653c] | 1461 | /* smp is now in EXPECT2 */ |
---|
| 1462 | } else { |
---|
| 1463 | /* this was 'otr smp', initiate or reply */ |
---|
| 1464 | /* warning: the following assumes that smstates are cleared whenever an SMP |
---|
[5ebff60] | 1465 | is completed or aborted! */ |
---|
| 1466 | if (ctx->smstate->secret == NULL) { |
---|
[e67e513] | 1467 | irc_rootmsg(irc, "smp: initiating with %s...", u->nick); |
---|
[99a01b9] | 1468 | otrl_message_initiate_smp(irc->otr->us, &otr_ops, |
---|
[5ebff60] | 1469 | u->bu->ic, ctx, (unsigned char *) secret, strlen(secret)); |
---|
[9cc653c] | 1470 | /* smp is now in EXPECT2 */ |
---|
| 1471 | } else { |
---|
| 1472 | /* if we're still in EXPECT1 but smstate is initialized, we must have |
---|
| 1473 | received the SMP1, so let's issue a response */ |
---|
[e67e513] | 1474 | irc_rootmsg(irc, "smp: responding to %s...", u->nick); |
---|
[9cc653c] | 1475 | otrl_message_respond_smp(irc->otr->us, &otr_ops, |
---|
[5ebff60] | 1476 | u->bu->ic, ctx, (unsigned char *) secret, strlen(secret)); |
---|
[9cc653c] | 1477 | /* smp is now in EXPECT3 */ |
---|
[99a01b9] | 1478 | } |
---|
| 1479 | } |
---|
| 1480 | } |
---|
| 1481 | |
---|
[22ec21d] | 1482 | /* timeout handler that calls otrl_message_poll */ |
---|
| 1483 | gboolean ev_message_poll(gpointer data, gint fd, b_input_condition cond) |
---|
| 1484 | { |
---|
| 1485 | otr_t *otr = data; |
---|
| 1486 | |
---|
[5ebff60] | 1487 | if (otr && otr->us) { |
---|
[c347a12] | 1488 | otrl_message_poll(otr->us, &otr_ops, NULL); |
---|
[5ebff60] | 1489 | } |
---|
[22ec21d] | 1490 | |
---|
[5ebff60] | 1491 | return TRUE; /* cycle timer */ |
---|
[22ec21d] | 1492 | } |
---|
| 1493 | |
---|
[764c7d1] | 1494 | /* helper to assert that account and protocol names given to ops below always |
---|
| 1495 | match the im_connection passed through as opdata */ |
---|
| 1496 | struct im_connection *check_imc(void *opdata, const char *accountname, |
---|
[5ebff60] | 1497 | const char *protocol) |
---|
[764c7d1] | 1498 | { |
---|
[5ebff60] | 1499 | struct im_connection *ic = (struct im_connection *) opdata; |
---|
[764c7d1] | 1500 | |
---|
[fa9478e] | 1501 | /* libotr 4.0.0 has a bug where it doesn't set opdata, so we catch |
---|
| 1502 | * that and try to find the desired connection in the global list. */ |
---|
[5ebff60] | 1503 | if (!ic) { |
---|
[fa9478e] | 1504 | GSList *l; |
---|
[5ebff60] | 1505 | for (l = get_connections(); l; l = l->next) { |
---|
[fa9478e] | 1506 | ic = l->data; |
---|
[5ebff60] | 1507 | if (strcmp(accountname, ic->acc->user) == 0 && |
---|
| 1508 | strcmp(protocol, ic->acc->prpl->name) == 0) { |
---|
[fa9478e] | 1509 | break; |
---|
[5ebff60] | 1510 | } |
---|
[fa9478e] | 1511 | } |
---|
| 1512 | assert(l != NULL); /* a match should always be found */ |
---|
[5ebff60] | 1513 | if (!l) { |
---|
[37ed402] | 1514 | return NULL; |
---|
[5ebff60] | 1515 | } |
---|
[fa9478e] | 1516 | } |
---|
| 1517 | |
---|
[764c7d1] | 1518 | if (strcmp(accountname, ic->acc->user) != 0) { |
---|
| 1519 | log_message(LOGLVL_WARNING, |
---|
[5ebff60] | 1520 | "otr: internal account name mismatch: '%s' vs '%s'", |
---|
| 1521 | accountname, ic->acc->user); |
---|
[764c7d1] | 1522 | } |
---|
| 1523 | if (strcmp(protocol, ic->acc->prpl->name) != 0) { |
---|
| 1524 | log_message(LOGLVL_WARNING, |
---|
[5ebff60] | 1525 | "otr: internal protocol name mismatch: '%s' vs '%s'", |
---|
| 1526 | protocol, ic->acc->prpl->name); |
---|
[764c7d1] | 1527 | } |
---|
[5ebff60] | 1528 | |
---|
[764c7d1] | 1529 | return ic; |
---|
| 1530 | } |
---|
| 1531 | |
---|
[ad2d8bc] | 1532 | irc_user_t *peeruser(irc_t *irc, const char *handle, const char *protocol) |
---|
[764c7d1] | 1533 | { |
---|
[ad2d8bc] | 1534 | GSList *l; |
---|
[5ebff60] | 1535 | |
---|
| 1536 | for (l = irc->b->users; l; l = l->next) { |
---|
[ad2d8bc] | 1537 | bee_user_t *bu = l->data; |
---|
[764c7d1] | 1538 | struct prpl *prpl; |
---|
[5ebff60] | 1539 | if (!bu->ui_data || !bu->ic || !bu->handle) { |
---|
[8521b02] | 1540 | continue; |
---|
[5ebff60] | 1541 | } |
---|
[ad2d8bc] | 1542 | prpl = bu->ic->acc->prpl; |
---|
[5ebff60] | 1543 | if (strcmp(prpl->name, protocol) == 0 |
---|
| 1544 | && prpl->handle_cmp(bu->handle, handle) == 0) { |
---|
[ad2d8bc] | 1545 | return bu->ui_data; |
---|
[764c7d1] | 1546 | } |
---|
| 1547 | } |
---|
[5ebff60] | 1548 | |
---|
[5a71d9c] | 1549 | return NULL; |
---|
| 1550 | } |
---|
| 1551 | |
---|
[8521b02] | 1552 | int hexval(char a) |
---|
| 1553 | { |
---|
[5ebff60] | 1554 | int x = g_ascii_tolower(a); |
---|
| 1555 | |
---|
| 1556 | if (x >= 'a' && x <= 'f') { |
---|
[8521b02] | 1557 | x = x - 'a' + 10; |
---|
[5ebff60] | 1558 | } else if (x >= '0' && x <= '9') { |
---|
[8521b02] | 1559 | x = x - '0'; |
---|
[5ebff60] | 1560 | } else { |
---|
[8521b02] | 1561 | return -1; |
---|
[5ebff60] | 1562 | } |
---|
| 1563 | |
---|
[8521b02] | 1564 | return x; |
---|
| 1565 | } |
---|
| 1566 | |
---|
[5a71d9c] | 1567 | const char *peernick(irc_t *irc, const char *handle, const char *protocol) |
---|
| 1568 | { |
---|
| 1569 | static char fallback[512]; |
---|
[5ebff60] | 1570 | |
---|
[ad2d8bc] | 1571 | irc_user_t *u = peeruser(irc, handle, protocol); |
---|
[5ebff60] | 1572 | |
---|
| 1573 | if (u) { |
---|
[5a71d9c] | 1574 | return u->nick; |
---|
| 1575 | } else { |
---|
| 1576 | g_snprintf(fallback, 511, "%s/%s", handle, protocol); |
---|
| 1577 | return fallback; |
---|
| 1578 | } |
---|
| 1579 | } |
---|
| 1580 | |
---|
[a0c6fc5] | 1581 | void otr_update_uflags(ConnContext *context, irc_user_t *u) |
---|
| 1582 | { |
---|
| 1583 | const char *trust; |
---|
| 1584 | |
---|
[5ebff60] | 1585 | if (context->active_fingerprint) { |
---|
[a0c6fc5] | 1586 | u->flags |= IRC_USER_OTR_ENCRYPTED; |
---|
| 1587 | |
---|
| 1588 | trust = context->active_fingerprint->trust; |
---|
[5ebff60] | 1589 | if (trust && trust[0]) { |
---|
[a0c6fc5] | 1590 | u->flags |= IRC_USER_OTR_TRUSTED; |
---|
[5ebff60] | 1591 | } else { |
---|
[a0c6fc5] | 1592 | u->flags &= ~IRC_USER_OTR_TRUSTED; |
---|
[5ebff60] | 1593 | } |
---|
[a0c6fc5] | 1594 | } else { |
---|
| 1595 | u->flags &= ~IRC_USER_OTR_ENCRYPTED; |
---|
| 1596 | } |
---|
| 1597 | } |
---|
| 1598 | |
---|
[ad2d8bc] | 1599 | int otr_update_modeflags(irc_t *irc, irc_user_t *u) |
---|
[5a71d9c] | 1600 | { |
---|
[1dc00fe] | 1601 | return 0; |
---|
[764c7d1] | 1602 | } |
---|
| 1603 | |
---|
| 1604 | void show_fingerprints(irc_t *irc, ConnContext *ctx) |
---|
| 1605 | { |
---|
| 1606 | char human[45]; |
---|
| 1607 | Fingerprint *fp; |
---|
| 1608 | const char *trust; |
---|
[5ebff60] | 1609 | int count = 0; |
---|
| 1610 | |
---|
[2c81d15] | 1611 | /* Is this a subcontext? If so, only list the active fingerprint */ |
---|
| 1612 | if (ctx->m_context != ctx) { |
---|
| 1613 | fp = ctx->active_fingerprint; |
---|
| 1614 | } else { |
---|
| 1615 | fp = &ctx->fingerprint_root; |
---|
| 1616 | } |
---|
| 1617 | |
---|
| 1618 | while (fp) { |
---|
[5ebff60] | 1619 | if (!fp->fingerprint) { |
---|
[2c81d15] | 1620 | fp = fp->next; |
---|
[764c7d1] | 1621 | continue; |
---|
[5ebff60] | 1622 | } |
---|
[764c7d1] | 1623 | count++; |
---|
| 1624 | otrl_privkey_hash_to_human(human, fp->fingerprint); |
---|
[5ebff60] | 1625 | if (!fp->trust || fp->trust[0] == '\0') { |
---|
| 1626 | trust = "untrusted"; |
---|
[764c7d1] | 1627 | } else { |
---|
[5ebff60] | 1628 | trust = fp->trust; |
---|
[764c7d1] | 1629 | } |
---|
[5ebff60] | 1630 | if (fp == ctx->active_fingerprint) { |
---|
[2c81d15] | 1631 | irc_rootmsg(irc, " \x02%s (%s)\x02", human, trust); |
---|
[764c7d1] | 1632 | } else { |
---|
[2c81d15] | 1633 | irc_rootmsg(irc, " %s (%s)", human, trust); |
---|
[764c7d1] | 1634 | } |
---|
[2c81d15] | 1635 | |
---|
| 1636 | /* Break if this is a subcontext - we only print active fp */ |
---|
| 1637 | if (ctx->m_context != ctx) { |
---|
| 1638 | break; |
---|
| 1639 | } |
---|
| 1640 | fp = fp->next; |
---|
[764c7d1] | 1641 | } |
---|
[5ebff60] | 1642 | if (count == 0) { |
---|
[2c81d15] | 1643 | irc_rootmsg(irc, " (none)"); |
---|
[5ebff60] | 1644 | } |
---|
[8521b02] | 1645 | } |
---|
| 1646 | |
---|
[c595308] | 1647 | Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args) |
---|
| 1648 | { |
---|
| 1649 | Fingerprint *fp, *fp2; |
---|
| 1650 | char human[45]; |
---|
| 1651 | char prefix[45], *p; |
---|
| 1652 | int n; |
---|
[5ebff60] | 1653 | int i, j; |
---|
| 1654 | |
---|
[c595308] | 1655 | /* assemble the args into a prefix in standard "human" form */ |
---|
[5ebff60] | 1656 | n = 0; |
---|
| 1657 | p = prefix; |
---|
| 1658 | for (i = 0; args[i]; i++) { |
---|
| 1659 | for (j = 0; args[i][j]; j++) { |
---|
[6b13103] | 1660 | char c = g_ascii_toupper(args[i][j]); |
---|
[5ebff60] | 1661 | |
---|
| 1662 | if (n >= 40) { |
---|
[e67e513] | 1663 | irc_rootmsg(irc, "too many fingerprint digits given, expected at most 40"); |
---|
[c595308] | 1664 | return NULL; |
---|
| 1665 | } |
---|
[5ebff60] | 1666 | |
---|
| 1667 | if ((c >= 'A' && c <= 'F') || (c >= '0' && c <= '9')) { |
---|
[c595308] | 1668 | *(p++) = c; |
---|
| 1669 | } else { |
---|
[5ebff60] | 1670 | irc_rootmsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i + 1); |
---|
[c595308] | 1671 | return NULL; |
---|
| 1672 | } |
---|
[5ebff60] | 1673 | |
---|
[c595308] | 1674 | n++; |
---|
[5ebff60] | 1675 | if (n % 8 == 0) { |
---|
[c595308] | 1676 | *(p++) = ' '; |
---|
[5ebff60] | 1677 | } |
---|
[c595308] | 1678 | } |
---|
| 1679 | } |
---|
| 1680 | *p = '\0'; |
---|
[5ebff60] | 1681 | |
---|
[c595308] | 1682 | /* find first fingerprint with the given prefix */ |
---|
| 1683 | n = strlen(prefix); |
---|
[5ebff60] | 1684 | for (fp = &ctx->fingerprint_root; fp; fp = fp->next) { |
---|
| 1685 | if (!fp->fingerprint) { |
---|
[c595308] | 1686 | continue; |
---|
[5ebff60] | 1687 | } |
---|
[c595308] | 1688 | otrl_privkey_hash_to_human(human, fp->fingerprint); |
---|
[5ebff60] | 1689 | if (!strncmp(prefix, human, n)) { |
---|
[c595308] | 1690 | break; |
---|
[5ebff60] | 1691 | } |
---|
[c595308] | 1692 | } |
---|
[5ebff60] | 1693 | if (!fp) { |
---|
[e67e513] | 1694 | irc_rootmsg(irc, "%s: no match", prefix); |
---|
[c595308] | 1695 | return NULL; |
---|
| 1696 | } |
---|
[5ebff60] | 1697 | |
---|
[c595308] | 1698 | /* make sure the match, if any, is unique */ |
---|
[5ebff60] | 1699 | for (fp2 = fp->next; fp2; fp2 = fp2->next) { |
---|
| 1700 | if (!fp2->fingerprint) { |
---|
[c595308] | 1701 | continue; |
---|
[5ebff60] | 1702 | } |
---|
[c595308] | 1703 | otrl_privkey_hash_to_human(human, fp2->fingerprint); |
---|
[5ebff60] | 1704 | if (!strncmp(prefix, human, n)) { |
---|
[c595308] | 1705 | break; |
---|
[5ebff60] | 1706 | } |
---|
[c595308] | 1707 | } |
---|
[5ebff60] | 1708 | if (fp2) { |
---|
[e67e513] | 1709 | irc_rootmsg(irc, "%s: multiple matches", prefix); |
---|
[c595308] | 1710 | return NULL; |
---|
| 1711 | } |
---|
[5ebff60] | 1712 | |
---|
[c595308] | 1713 | return fp; |
---|
| 1714 | } |
---|
| 1715 | |
---|
[5f4eede] | 1716 | OtrlPrivKey *match_privkey(irc_t *irc, const char **args) |
---|
| 1717 | { |
---|
| 1718 | OtrlPrivKey *k, *k2; |
---|
| 1719 | char human[45]; |
---|
| 1720 | char prefix[45], *p; |
---|
| 1721 | int n; |
---|
[5ebff60] | 1722 | int i, j; |
---|
| 1723 | |
---|
[5f4eede] | 1724 | /* assemble the args into a prefix in standard "human" form */ |
---|
[5ebff60] | 1725 | n = 0; |
---|
| 1726 | p = prefix; |
---|
| 1727 | for (i = 0; args[i]; i++) { |
---|
| 1728 | for (j = 0; args[i][j]; j++) { |
---|
[6b13103] | 1729 | char c = g_ascii_toupper(args[i][j]); |
---|
[5ebff60] | 1730 | |
---|
| 1731 | if (n >= 40) { |
---|
[e67e513] | 1732 | irc_rootmsg(irc, "too many fingerprint digits given, expected at most 40"); |
---|
[5f4eede] | 1733 | return NULL; |
---|
| 1734 | } |
---|
[5ebff60] | 1735 | |
---|
| 1736 | if ((c >= 'A' && c <= 'F') || (c >= '0' && c <= '9')) { |
---|
[5f4eede] | 1737 | *(p++) = c; |
---|
| 1738 | } else { |
---|
[5ebff60] | 1739 | irc_rootmsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i + 1); |
---|
[5f4eede] | 1740 | return NULL; |
---|
| 1741 | } |
---|
[5ebff60] | 1742 | |
---|
[5f4eede] | 1743 | n++; |
---|
[5ebff60] | 1744 | if (n % 8 == 0) { |
---|
[5f4eede] | 1745 | *(p++) = ' '; |
---|
[5ebff60] | 1746 | } |
---|
[5f4eede] | 1747 | } |
---|
| 1748 | } |
---|
| 1749 | *p = '\0'; |
---|
[5ebff60] | 1750 | |
---|
[0b1ae59] | 1751 | /* remove trailing whitespace */ |
---|
| 1752 | g_strchomp(prefix); |
---|
| 1753 | |
---|
[5f4eede] | 1754 | /* find first key which matches the given prefix */ |
---|
| 1755 | n = strlen(prefix); |
---|
[5ebff60] | 1756 | for (k = irc->otr->us->privkey_root; k; k = k->next) { |
---|
[dc9797f] | 1757 | p = otrl_privkey_fingerprint(irc->otr->us, human, k->accountname, k->protocol); |
---|
[5ebff60] | 1758 | if (!p) { /* gah! :-P */ |
---|
[5f4eede] | 1759 | continue; |
---|
[5ebff60] | 1760 | } |
---|
| 1761 | if (!strncmp(prefix, human, n)) { |
---|
[5f4eede] | 1762 | break; |
---|
[5ebff60] | 1763 | } |
---|
[5f4eede] | 1764 | } |
---|
[5ebff60] | 1765 | if (!k) { |
---|
[e67e513] | 1766 | irc_rootmsg(irc, "%s: no match", prefix); |
---|
[5f4eede] | 1767 | return NULL; |
---|
| 1768 | } |
---|
[5ebff60] | 1769 | |
---|
[5f4eede] | 1770 | /* make sure the match, if any, is unique */ |
---|
[5ebff60] | 1771 | for (k2 = k->next; k2; k2 = k2->next) { |
---|
[dc9797f] | 1772 | p = otrl_privkey_fingerprint(irc->otr->us, human, k2->accountname, k2->protocol); |
---|
[5ebff60] | 1773 | if (!p) { /* gah! :-P */ |
---|
[5f4eede] | 1774 | continue; |
---|
[5ebff60] | 1775 | } |
---|
| 1776 | if (!strncmp(prefix, human, n)) { |
---|
[5f4eede] | 1777 | break; |
---|
[5ebff60] | 1778 | } |
---|
[5f4eede] | 1779 | } |
---|
[5ebff60] | 1780 | if (k2) { |
---|
[e67e513] | 1781 | irc_rootmsg(irc, "%s: multiple matches", prefix); |
---|
[5f4eede] | 1782 | return NULL; |
---|
| 1783 | } |
---|
[5ebff60] | 1784 | |
---|
[5f4eede] | 1785 | return k; |
---|
| 1786 | } |
---|
| 1787 | |
---|
[8521b02] | 1788 | void show_general_otr_info(irc_t *irc) |
---|
| 1789 | { |
---|
| 1790 | ConnContext *ctx; |
---|
| 1791 | OtrlPrivKey *key; |
---|
| 1792 | char human[45]; |
---|
[3064ea4] | 1793 | kg_t *kg; |
---|
[8521b02] | 1794 | |
---|
[3064ea4] | 1795 | /* list all privkeys (including ones being generated) */ |
---|
[e67e513] | 1796 | irc_rootmsg(irc, "\x1fprivate keys:\x1f"); |
---|
[5ebff60] | 1797 | for (key = irc->otr->us->privkey_root; key; key = key->next) { |
---|
[8521b02] | 1798 | const char *hash; |
---|
[5ebff60] | 1799 | |
---|
| 1800 | switch (key->pubkey_type) { |
---|
[8521b02] | 1801 | case OTRL_PUBKEY_TYPE_DSA: |
---|
[e67e513] | 1802 | irc_rootmsg(irc, " %s/%s - DSA", key->accountname, key->protocol); |
---|
[8521b02] | 1803 | break; |
---|
| 1804 | default: |
---|
[e67e513] | 1805 | irc_rootmsg(irc, " %s/%s - type %d", key->accountname, key->protocol, |
---|
[5ebff60] | 1806 | key->pubkey_type); |
---|
[8521b02] | 1807 | } |
---|
| 1808 | |
---|
| 1809 | /* No, it doesn't make much sense to search for the privkey again by |
---|
| 1810 | account/protocol, but libotr currently doesn't provide a direct routine |
---|
| 1811 | for hashing a given 'OtrlPrivKey'... */ |
---|
[dc9797f] | 1812 | hash = otrl_privkey_fingerprint(irc->otr->us, human, key->accountname, key->protocol); |
---|
[5ebff60] | 1813 | if (hash) { /* should always succeed */ |
---|
[e67e513] | 1814 | irc_rootmsg(irc, " %s", human); |
---|
[5ebff60] | 1815 | } |
---|
[8521b02] | 1816 | } |
---|
[5ebff60] | 1817 | if (irc->otr->sent_accountname) { |
---|
[e67e513] | 1818 | irc_rootmsg(irc, " %s/%s - DSA", irc->otr->sent_accountname, |
---|
[5ebff60] | 1819 | irc->otr->sent_protocol); |
---|
[e67e513] | 1820 | irc_rootmsg(irc, " (being generated)"); |
---|
[1221ef0] | 1821 | } |
---|
[5ebff60] | 1822 | for (kg = irc->otr->todo; kg; kg = kg->next) { |
---|
[e67e513] | 1823 | irc_rootmsg(irc, " %s/%s - DSA", kg->accountname, kg->protocol); |
---|
| 1824 | irc_rootmsg(irc, " (queued)"); |
---|
[3064ea4] | 1825 | } |
---|
[5ebff60] | 1826 | if (key == irc->otr->us->privkey_root && |
---|
| 1827 | !irc->otr->sent_accountname && |
---|
| 1828 | kg == irc->otr->todo) { |
---|
[e67e513] | 1829 | irc_rootmsg(irc, " (none)"); |
---|
[5ebff60] | 1830 | } |
---|
[8521b02] | 1831 | |
---|
| 1832 | /* list all contexts */ |
---|
[e4752a6] | 1833 | /* XXX remove this, or split off as its own command */ |
---|
[e67e513] | 1834 | irc_rootmsg(irc, "%s", ""); |
---|
| 1835 | irc_rootmsg(irc, "\x1f" "connection contexts:\x1f (bold=currently encrypted)"); |
---|
[2c81d15] | 1836 | |
---|
| 1837 | ctx = irc->otr->us->context_root; |
---|
| 1838 | while (ctx) { |
---|
| 1839 | ConnContext *subctx; |
---|
[ad2d8bc] | 1840 | irc_user_t *u; |
---|
[8521b02] | 1841 | char *userstring; |
---|
[2c81d15] | 1842 | char encrypted = 0; |
---|
[5ebff60] | 1843 | |
---|
[8521b02] | 1844 | u = peeruser(irc, ctx->username, ctx->protocol); |
---|
[5ebff60] | 1845 | if (u) { |
---|
[8521b02] | 1846 | userstring = g_strdup_printf("%s/%s/%s (%s)", |
---|
[5ebff60] | 1847 | ctx->username, ctx->protocol, ctx->accountname, u->nick); |
---|
| 1848 | } else { |
---|
[8521b02] | 1849 | userstring = g_strdup_printf("%s/%s/%s", |
---|
[5ebff60] | 1850 | ctx->username, ctx->protocol, ctx->accountname); |
---|
| 1851 | } |
---|
| 1852 | |
---|
[2c81d15] | 1853 | subctx = ctx; |
---|
| 1854 | while (subctx && subctx->m_context == ctx) { |
---|
| 1855 | if (subctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { |
---|
| 1856 | encrypted = 1; |
---|
| 1857 | } |
---|
| 1858 | subctx = subctx->next; |
---|
| 1859 | } |
---|
| 1860 | |
---|
| 1861 | if(encrypted) { |
---|
[e67e513] | 1862 | irc_rootmsg(irc, " \x02%s\x02", userstring); |
---|
[8521b02] | 1863 | } else { |
---|
[e67e513] | 1864 | irc_rootmsg(irc, " %s", userstring); |
---|
[8521b02] | 1865 | } |
---|
[5ebff60] | 1866 | |
---|
[2c81d15] | 1867 | /* Skip subcontexts/instances from output */ |
---|
| 1868 | ctx = subctx; |
---|
| 1869 | |
---|
[8521b02] | 1870 | g_free(userstring); |
---|
| 1871 | } |
---|
[2c81d15] | 1872 | |
---|
[5ebff60] | 1873 | if (ctx == irc->otr->us->context_root) { |
---|
[e67e513] | 1874 | irc_rootmsg(irc, " (none)"); |
---|
[5ebff60] | 1875 | } |
---|
[8521b02] | 1876 | } |
---|
| 1877 | |
---|
[2c81d15] | 1878 | void show_otr_context_info(irc_t *irc, ConnContext *ctx, ConnContext *bestctx) |
---|
[8521b02] | 1879 | { |
---|
[2c81d15] | 1880 | ConnContext *subctx; |
---|
| 1881 | int instcount = 0; |
---|
| 1882 | |
---|
| 1883 | subctx = ctx; |
---|
| 1884 | while (subctx && subctx->m_context == ctx) { |
---|
| 1885 | if (subctx->m_context == subctx) { |
---|
| 1886 | if (subctx == bestctx) { |
---|
| 1887 | irc_rootmsg(irc, " \x02master context (target):\x02"); |
---|
| 1888 | } else { |
---|
| 1889 | irc_rootmsg(irc, " master context:"); |
---|
| 1890 | } |
---|
| 1891 | irc_rootmsg(irc, " known fingerprints (bold = active for v1 or v2):"); |
---|
| 1892 | } else { |
---|
| 1893 | if (subctx == bestctx) { |
---|
| 1894 | irc_rootmsg(irc, " \x02instance %d (target):\x02", instcount); |
---|
| 1895 | } else { |
---|
| 1896 | irc_rootmsg(irc, " instance %d:", instcount); |
---|
| 1897 | } |
---|
| 1898 | irc_rootmsg(irc, " active fingerprint:"); |
---|
| 1899 | instcount++; |
---|
| 1900 | } |
---|
[f93e01c] | 1901 | |
---|
[2c81d15] | 1902 | show_fingerprints(irc, subctx); |
---|
[8521b02] | 1903 | |
---|
[2c81d15] | 1904 | switch (subctx->msgstate) { |
---|
| 1905 | case OTRL_MSGSTATE_PLAINTEXT: |
---|
| 1906 | irc_rootmsg(irc, " connection state: cleartext"); |
---|
| 1907 | break; |
---|
| 1908 | case OTRL_MSGSTATE_ENCRYPTED: |
---|
| 1909 | irc_rootmsg(irc, " connection state: encrypted (v%d)", subctx->protocol_version); |
---|
| 1910 | break; |
---|
| 1911 | case OTRL_MSGSTATE_FINISHED: |
---|
| 1912 | irc_rootmsg(irc, " connection state: shut down"); |
---|
| 1913 | break; |
---|
| 1914 | default: |
---|
| 1915 | irc_rootmsg(irc, " connection state: %d", subctx->msgstate); |
---|
| 1916 | } |
---|
[8521b02] | 1917 | |
---|
[2c81d15] | 1918 | subctx = subctx->next; |
---|
| 1919 | } |
---|
[764c7d1] | 1920 | } |
---|
| 1921 | |
---|
[dc9797f] | 1922 | int keygen_in_progress(irc_t *irc, const char *handle, const char *protocol) |
---|
| 1923 | { |
---|
| 1924 | kg_t *kg; |
---|
[5ebff60] | 1925 | |
---|
| 1926 | if (!irc->otr->sent_accountname || !irc->otr->sent_protocol) { |
---|
[dc9797f] | 1927 | return 0; |
---|
[5ebff60] | 1928 | } |
---|
[dc9797f] | 1929 | |
---|
| 1930 | /* are we currently working on this key? */ |
---|
[5ebff60] | 1931 | if (!strcmp(handle, irc->otr->sent_accountname) && |
---|
| 1932 | !strcmp(protocol, irc->otr->sent_protocol)) { |
---|
[dc9797f] | 1933 | return 1; |
---|
[5ebff60] | 1934 | } |
---|
| 1935 | |
---|
[dc9797f] | 1936 | /* do we have it queued for later? */ |
---|
[5ebff60] | 1937 | for (kg = irc->otr->todo; kg; kg = kg->next) { |
---|
| 1938 | if (!strcmp(handle, kg->accountname) && |
---|
| 1939 | !strcmp(protocol, kg->protocol)) { |
---|
[dc9797f] | 1940 | return 1; |
---|
[5ebff60] | 1941 | } |
---|
[dc9797f] | 1942 | } |
---|
[5ebff60] | 1943 | |
---|
[dc9797f] | 1944 | return 0; |
---|
| 1945 | } |
---|
| 1946 | |
---|
[764c7d1] | 1947 | void otr_keygen(irc_t *irc, const char *handle, const char *protocol) |
---|
| 1948 | { |
---|
[dc9797f] | 1949 | /* do nothing if a key for the requested account is already being generated */ |
---|
[5ebff60] | 1950 | if (keygen_in_progress(irc, handle, protocol)) { |
---|
[dc9797f] | 1951 | return; |
---|
[5ebff60] | 1952 | } |
---|
[764c7d1] | 1953 | |
---|
[522a00f] | 1954 | /* see if we already have a keygen child running. if not, start one and put a |
---|
[27db433] | 1955 | handler on its output. */ |
---|
[5ebff60] | 1956 | if (!irc->otr->keygen || waitpid(irc->otr->keygen, NULL, WNOHANG)) { |
---|
[27db433] | 1957 | pid_t p; |
---|
| 1958 | int to[2], from[2]; |
---|
| 1959 | FILE *tof, *fromf; |
---|
[5ebff60] | 1960 | |
---|
| 1961 | if (pipe(to) < 0 || pipe(from) < 0) { |
---|
[e67e513] | 1962 | irc_rootmsg(irc, "otr keygen: couldn't create pipe: %s", strerror(errno)); |
---|
[27db433] | 1963 | return; |
---|
| 1964 | } |
---|
[5ebff60] | 1965 | |
---|
[27db433] | 1966 | tof = fdopen(to[1], "w"); |
---|
| 1967 | fromf = fdopen(from[0], "r"); |
---|
[5ebff60] | 1968 | if (!tof || !fromf) { |
---|
[e67e513] | 1969 | irc_rootmsg(irc, "otr keygen: couldn't streamify pipe: %s", strerror(errno)); |
---|
[27db433] | 1970 | return; |
---|
| 1971 | } |
---|
[5ebff60] | 1972 | |
---|
[27db433] | 1973 | p = fork(); |
---|
[5ebff60] | 1974 | if (p < 0) { |
---|
[e67e513] | 1975 | irc_rootmsg(irc, "otr keygen: couldn't fork: %s", strerror(errno)); |
---|
[27db433] | 1976 | return; |
---|
| 1977 | } |
---|
[5ebff60] | 1978 | |
---|
| 1979 | if (!p) { |
---|
[27db433] | 1980 | /* child process */ |
---|
| 1981 | signal(SIGTERM, exit); |
---|
[12cc58b] | 1982 | keygen_child_main(irc->otr->us, to[0], from[1]); |
---|
[27db433] | 1983 | exit(0); |
---|
| 1984 | } |
---|
[5ebff60] | 1985 | |
---|
[dc9797f] | 1986 | irc->otr->keygen = p; |
---|
| 1987 | irc->otr->to = tof; |
---|
| 1988 | irc->otr->from = fromf; |
---|
| 1989 | irc->otr->sent_accountname = NULL; |
---|
| 1990 | irc->otr->sent_protocol = NULL; |
---|
| 1991 | irc->otr->todo = NULL; |
---|
[ad2d8bc] | 1992 | b_input_add(from[0], B_EV_IO_READ, keygen_finish_handler, irc); |
---|
[27db433] | 1993 | } |
---|
[5ebff60] | 1994 | |
---|
[dc9797f] | 1995 | /* is the keygen slave currently working? */ |
---|
[5ebff60] | 1996 | if (irc->otr->sent_accountname) { |
---|
[dc9797f] | 1997 | /* enqueue our job for later transmission */ |
---|
| 1998 | kg_t **kg = &irc->otr->todo; |
---|
[5ebff60] | 1999 | while (*kg) { |
---|
| 2000 | kg = &((*kg)->next); |
---|
| 2001 | } |
---|
[dc9797f] | 2002 | *kg = g_new0(kg_t, 1); |
---|
[ba5add7] | 2003 | (*kg)->accountname = g_strdup(handle); |
---|
| 2004 | (*kg)->protocol = g_strdup(protocol); |
---|
[dc9797f] | 2005 | } else { |
---|
| 2006 | /* send our job over and remember it */ |
---|
| 2007 | fprintf(irc->otr->to, "%s\n%s\n", handle, protocol); |
---|
| 2008 | fflush(irc->otr->to); |
---|
[ba5add7] | 2009 | irc->otr->sent_accountname = g_strdup(handle); |
---|
| 2010 | irc->otr->sent_protocol = g_strdup(protocol); |
---|
[dc9797f] | 2011 | } |
---|
[27db433] | 2012 | } |
---|
[522a00f] | 2013 | |
---|
[12cc58b] | 2014 | void keygen_child_main(OtrlUserState us, int infd, int outfd) |
---|
[27db433] | 2015 | { |
---|
| 2016 | FILE *input, *output; |
---|
| 2017 | char filename[128], accountname[512], protocol[512]; |
---|
| 2018 | gcry_error_t e; |
---|
| 2019 | int tempfd; |
---|
[5ebff60] | 2020 | |
---|
[27db433] | 2021 | input = fdopen(infd, "r"); |
---|
| 2022 | output = fdopen(outfd, "w"); |
---|
[5ebff60] | 2023 | |
---|
| 2024 | while (!feof(input) && !ferror(input) && !feof(output) && !ferror(output)) { |
---|
[27db433] | 2025 | myfgets(accountname, 512, input); |
---|
| 2026 | myfgets(protocol, 512, input); |
---|
[5ebff60] | 2027 | |
---|
[27db433] | 2028 | strncpy(filename, "/tmp/bitlbee-XXXXXX", 128); |
---|
| 2029 | tempfd = mkstemp(filename); |
---|
| 2030 | close(tempfd); |
---|
| 2031 | |
---|
| 2032 | e = otrl_privkey_generate(us, filename, accountname, protocol); |
---|
[5ebff60] | 2033 | if (e) { |
---|
[27db433] | 2034 | fprintf(output, "\n"); /* this means failure */ |
---|
| 2035 | fprintf(output, "otr keygen: %s\n", gcry_strerror(e)); |
---|
| 2036 | unlink(filename); |
---|
| 2037 | } else { |
---|
| 2038 | fprintf(output, "%s\n", filename); |
---|
| 2039 | fprintf(output, "otr keygen for %s/%s complete\n", accountname, protocol); |
---|
| 2040 | } |
---|
| 2041 | fflush(output); |
---|
| 2042 | } |
---|
[5ebff60] | 2043 | |
---|
[27db433] | 2044 | fclose(input); |
---|
| 2045 | fclose(output); |
---|
[764c7d1] | 2046 | } |
---|
| 2047 | |
---|
| 2048 | gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond) |
---|
| 2049 | { |
---|
[5ebff60] | 2050 | irc_t *irc = (irc_t *) data; |
---|
[27db433] | 2051 | char filename[512], msg[512]; |
---|
| 2052 | |
---|
[dc9797f] | 2053 | myfgets(filename, 512, irc->otr->from); |
---|
| 2054 | myfgets(msg, 512, irc->otr->from); |
---|
[5ebff60] | 2055 | |
---|
[e67e513] | 2056 | irc_rootmsg(irc, "%s", msg); |
---|
[5ebff60] | 2057 | if (filename[0]) { |
---|
| 2058 | if (strsane(irc->user->nick)) { |
---|
[c7b94ef] | 2059 | char *kf = g_strdup_printf("%s%s.otr_keys", global.conf->configdir, irc->user->nick); |
---|
| 2060 | char *tmp = g_strdup_printf("%s.new", kf); |
---|
| 2061 | copyfile(filename, tmp); |
---|
| 2062 | unlink(filename); |
---|
[5ebff60] | 2063 | rename(tmp, kf); |
---|
[c7b94ef] | 2064 | otrl_privkey_read(irc->otr->us, kf); |
---|
| 2065 | g_free(kf); |
---|
| 2066 | g_free(tmp); |
---|
| 2067 | } else { |
---|
| 2068 | otrl_privkey_read(irc->otr->us, filename); |
---|
| 2069 | unlink(filename); |
---|
| 2070 | } |
---|
[27db433] | 2071 | } |
---|
[5ebff60] | 2072 | |
---|
[dc9797f] | 2073 | /* forget this job */ |
---|
[ba5add7] | 2074 | g_free(irc->otr->sent_accountname); |
---|
| 2075 | g_free(irc->otr->sent_protocol); |
---|
[dc9797f] | 2076 | irc->otr->sent_accountname = NULL; |
---|
| 2077 | irc->otr->sent_protocol = NULL; |
---|
[5ebff60] | 2078 | |
---|
[dc9797f] | 2079 | /* see if there are any more in the queue */ |
---|
[5ebff60] | 2080 | if (irc->otr->todo) { |
---|
[dc9797f] | 2081 | kg_t *p = irc->otr->todo; |
---|
| 2082 | /* send the next one over */ |
---|
| 2083 | fprintf(irc->otr->to, "%s\n%s\n", p->accountname, p->protocol); |
---|
| 2084 | fflush(irc->otr->to); |
---|
| 2085 | irc->otr->sent_accountname = p->accountname; |
---|
| 2086 | irc->otr->sent_protocol = p->protocol; |
---|
| 2087 | irc->otr->todo = p->next; |
---|
| 2088 | g_free(p); |
---|
| 2089 | return TRUE; /* keep watching */ |
---|
| 2090 | } else { |
---|
| 2091 | /* okay, the slave is idle now, so kill him */ |
---|
| 2092 | fclose(irc->otr->from); |
---|
| 2093 | fclose(irc->otr->to); |
---|
[2dcaf9a] | 2094 | irc->otr->from = irc->otr->to = NULL; |
---|
[dc9797f] | 2095 | kill(irc->otr->keygen, SIGTERM); |
---|
| 2096 | waitpid(irc->otr->keygen, NULL, 0); |
---|
| 2097 | irc->otr->keygen = 0; |
---|
[27db433] | 2098 | return FALSE; /* unregister ourselves */ |
---|
| 2099 | } |
---|
| 2100 | } |
---|
| 2101 | |
---|
| 2102 | void copyfile(const char *a, const char *b) |
---|
| 2103 | { |
---|
| 2104 | int fda, fdb; |
---|
| 2105 | int n; |
---|
| 2106 | char buf[1024]; |
---|
[5ebff60] | 2107 | |
---|
[27db433] | 2108 | fda = open(a, O_RDONLY); |
---|
| 2109 | fdb = open(b, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
---|
[5ebff60] | 2110 | |
---|
| 2111 | while ((n = read(fda, buf, 1024)) > 0) { |
---|
[27db433] | 2112 | write(fdb, buf, n); |
---|
[5ebff60] | 2113 | } |
---|
| 2114 | |
---|
[27db433] | 2115 | close(fda); |
---|
[5ebff60] | 2116 | close(fdb); |
---|
[27db433] | 2117 | } |
---|
| 2118 | |
---|
| 2119 | void myfgets(char *s, int size, FILE *stream) |
---|
| 2120 | { |
---|
[5ebff60] | 2121 | if (!fgets(s, size, stream)) { |
---|
[27db433] | 2122 | s[0] = '\0'; |
---|
| 2123 | } else { |
---|
| 2124 | int n = strlen(s); |
---|
[5ebff60] | 2125 | if (n > 0 && s[n - 1] == '\n') { |
---|
| 2126 | s[n - 1] = '\0'; |
---|
| 2127 | } |
---|
[27db433] | 2128 | } |
---|
[764c7d1] | 2129 | } |
---|
| 2130 | |
---|
[6738a67] | 2131 | void yes_keygen(void *data) |
---|
[764c7d1] | 2132 | { |
---|
[5ebff60] | 2133 | account_t *acc = (account_t *) data; |
---|
[ad2d8bc] | 2134 | irc_t *irc = acc->bee->ui_data; |
---|
[5ebff60] | 2135 | |
---|
| 2136 | if (keygen_in_progress(irc, acc->user, acc->prpl->name)) { |
---|
[e67e513] | 2137 | irc_rootmsg(irc, "keygen for %s/%s already in progress", |
---|
[5ebff60] | 2138 | acc->user, acc->prpl->name); |
---|
[dc9797f] | 2139 | } else { |
---|
[e67e513] | 2140 | irc_rootmsg(irc, "starting background keygen for %s/%s", |
---|
[5ebff60] | 2141 | acc->user, acc->prpl->name); |
---|
[e67e513] | 2142 | irc_rootmsg(irc, "you will be notified when it completes"); |
---|
[ad2d8bc] | 2143 | otr_keygen(irc, acc->user, acc->prpl->name); |
---|
[dc9797f] | 2144 | } |
---|
[764c7d1] | 2145 | } |
---|
[3231485] | 2146 | |
---|
[c7b94ef] | 2147 | /* check whether a string is safe to use in a path component */ |
---|
| 2148 | int strsane(const char *s) |
---|
| 2149 | { |
---|
| 2150 | return strpbrk(s, "/\\") == NULL; |
---|
| 2151 | } |
---|
| 2152 | |
---|
[c239fff] | 2153 | /* close the OTR connection with the given buddy */ |
---|
| 2154 | gboolean otr_disconnect_user(irc_t *irc, irc_user_t *u) |
---|
| 2155 | { |
---|
[5ebff60] | 2156 | if (!u || !u->bu || !u->bu->ic) { |
---|
[c239fff] | 2157 | return FALSE; |
---|
[5ebff60] | 2158 | } |
---|
[c239fff] | 2159 | |
---|
| 2160 | /* XXX we disconnect all instances; is that what we want? */ |
---|
| 2161 | otrl_message_disconnect_all_instances(irc->otr->us, &otr_ops, |
---|
[5ebff60] | 2162 | u->bu->ic, u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, |
---|
| 2163 | u->bu->handle); |
---|
| 2164 | |
---|
[c239fff] | 2165 | u->flags &= ~IRC_USER_OTR_TRUSTED; |
---|
| 2166 | u->flags &= ~IRC_USER_OTR_ENCRYPTED; |
---|
| 2167 | otr_update_modeflags(irc, u); |
---|
| 2168 | |
---|
| 2169 | return TRUE; |
---|
| 2170 | } |
---|
| 2171 | |
---|
| 2172 | /* close all active OTR connections */ |
---|
| 2173 | void otr_disconnect_all(irc_t *irc) |
---|
| 2174 | { |
---|
| 2175 | irc_user_t *u; |
---|
| 2176 | ConnContext *ctx; |
---|
| 2177 | |
---|
[5ebff60] | 2178 | for (ctx = irc->otr->us->context_root; ctx; ctx = ctx->next) { |
---|
| 2179 | if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { |
---|
[c239fff] | 2180 | u = peeruser(irc, ctx->username, ctx->protocol); |
---|
| 2181 | (void) otr_disconnect_user(irc, u); |
---|
| 2182 | } |
---|
| 2183 | } |
---|
| 2184 | } |
---|