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