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