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