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