diff --git a/Makefile.am b/Makefile.am index 28605b6..9c45fb5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -41,6 +41,7 @@ pidgin_otrng_la_SOURCES = otrng-plugin.c \ prekey-plugin-shared.c \ prekey-discovery.c \ prekey-discovery-jabber.c \ + prekeys.c \ plugin-all.c \ ui.c \ dialogs.c \ diff --git a/persistance.c b/persistance.c index 64ac1b1..bbe774d 100644 --- a/persistance.c +++ b/persistance.c @@ -187,3 +187,36 @@ void persistance_read_prekey_profile(otrng_global_state_s *otrng_state) { fclose(fp); } } + +int persistance_write_prekey_messages(otrng_global_state_s *otrng_state) { + FILE *fp = NULL; + gchar *f = g_build_filename(purple_user_dir(), PREKEYS_FILE_NAME, NULL); + if (!f) { + return -1; + } + + fp = open_file_write_mode(f); + + if (!otrng_global_state_prekey_messages_write_to(otrng_state, fp)) { + fclose(fp); + return -1; + } + + return 0; +} + +void persistance_read_prekey_messages(otrng_global_state_s *otrng_state) { + gchar *f = g_build_filename(purple_user_dir(), PREKEYS_FILE_NAME, NULL); + if (!f) { + return; + } + + FILE *fp = g_fopen(f, "rb"); + g_free(f); + + if (fp) { + otrng_global_state_prekeys_read_from( + otrng_state, fp, protocol_and_account_to_purple_conversation); + fclose(fp); + } +} diff --git a/persistance.h b/persistance.h index f8cd392..2c08eab 100644 --- a/persistance.h +++ b/persistance.h @@ -28,6 +28,7 @@ #define PRIVKEY_FILE_NAME_V4 "otr4.private_key" #define CLIENT_PROFILE_FILE_NAME "otr4.client_profile" #define PREKEY_PROFILE_FILE_NAME "otr4.prekey_profile" +#define PREKEYS_FILE_NAME "otr4.prekey_messages" int persistance_write_privkey_v4_FILEp(otrng_global_state_s *otrng_state); @@ -41,4 +42,8 @@ int persistance_write_prekey_profile_FILEp(otrng_global_state_s *otrng_state); void persistance_read_prekey_profile(otrng_global_state_s *otrng_state); +int persistance_write_prekey_messages(otrng_global_state_s *otrng_state); + +void persistance_read_prekey_messages(otrng_global_state_s *otrng_state); + #endif diff --git a/plugin-all.c b/plugin-all.c index 9e1829f..b6676f2 100644 --- a/plugin-all.c +++ b/plugin-all.c @@ -61,6 +61,7 @@ #include "persistance.h" #include "plugin-all.h" #include "prekey-plugin.h" +#include "prekeys.h" #include "profiles.h" #ifdef USING_GTK @@ -163,14 +164,6 @@ static void otrng_plugin_read_expired_prekey_profile(FILE *profiles_filep) { } } -static void otrng_plugin_read_prekeys(FILE *prekeys_filep) { - if (otrng_failed(otrng_global_state_prekeys_read_from( - otrng_state, prekeys_filep, - protocol_and_account_to_purple_conversation))) { - return; - } -} - static void otrng_plugin_fingerprint_store_create() { otrng_fingerprints_table = g_hash_table_new_full( g_str_hash, g_str_equal, g_free, g_destroy_plugin_fingerprint); @@ -2017,7 +2010,6 @@ static int otrng_plugin_init_userstate(void) { gchar *instagfile = NULL; gchar *exp_client_profile_filename = NULL; gchar *exp_prekey_profile_filename = NULL; - gchar *prekeysfile = NULL; forging_key_file = g_build_filename(purple_user_dir(), FORGING_KEY_FILE_NAME, NULL); @@ -2028,18 +2020,15 @@ static int otrng_plugin_init_userstate(void) { g_build_filename(purple_user_dir(), EXP_CLIENT_PROFILE_FILE_NAME, NULL); exp_prekey_profile_filename = g_build_filename(purple_user_dir(), EXP_PREKEY_PROFILE_FILE_NAME, NULL); - prekeysfile = g_build_filename(purple_user_dir(), PREKEYS_FILE_NAME, NULL); if (!forging_key_file || !privkeyfile3 || !storefile || !instagfile || - !exp_client_profile_filename || !exp_prekey_profile_filename || - !prekeysfile) { + !exp_client_profile_filename || !exp_prekey_profile_filename) { g_free(forging_key_file); g_free(privkeyfile3); g_free(storefile); g_free(instagfile); g_free(exp_client_profile_filename); g_free(exp_prekey_profile_filename); - g_free(prekeysfile); return 1; } @@ -2050,18 +2039,18 @@ static int otrng_plugin_init_userstate(void) { FILE *instagf = g_fopen(instagfile, "rb"); FILE *exp_client_profile_f = g_fopen(exp_client_profile_filename, "rb"); FILE *exp_prekey_profile_filep = g_fopen(exp_prekey_profile_filename, "rb"); - FILE *prekeyf = g_fopen(prekeysfile, "rb"); g_free(forging_key_file); g_free(privkeyfile3); g_free(storefile); + g_free(instagfile); g_free(exp_client_profile_filename); g_free(exp_prekey_profile_filename); - g_free(prekeysfile); otrng_client_callbacks_s *callbacks = otrng_plugin_client_callbacks_new(); long_term_keys_set_callbacks(callbacks); profiles_set_callbacks(callbacks); + prekeys_set_callbacks(callbacks); otrng_state = otrng_global_state_new(callbacks, otrng_false); @@ -2085,9 +2074,6 @@ static int otrng_plugin_init_userstate(void) { /* Read prekey profile */ otrng_plugin_read_expired_prekey_profile(exp_prekey_profile_filep); - /* Read prekey messages */ - otrng_plugin_read_prekeys(prekeyf); - if (priv3f) { fclose(priv3f); } @@ -2104,10 +2090,6 @@ static int otrng_plugin_init_userstate(void) { fclose(instagf); } - if (prekeyf) { - fclose(prekeyf); - } - return 0; } diff --git a/po/POTFILES.in b/po/POTFILES.in index 07da877..7746df7 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -14,6 +14,8 @@ persistance.c persistance.h plugin-all.c plugin-all.h +prekeys.c +prekeys.h prekey-plugin.c prekey-plugin.h prekey-plugin-account.c diff --git a/prekey-discovery-jabber.c b/prekey-discovery-jabber.c index 6a09449..2bc8530 100644 --- a/prekey-discovery-jabber.c +++ b/prekey-discovery-jabber.c @@ -233,6 +233,9 @@ int otrng_plugin_jabber_lookup_prekey_servers_for(PurpleAccount *account, const char *who, PrekeyServerResult result_cb, void *context) { + if (account == NULL || who == NULL) { + return 0; + } PurplePlugin *prpl = purple_plugins_find_with_id("prpl-jabber"); if (prpl == NULL) { return 0; @@ -260,7 +263,7 @@ int otrng_plugin_jabber_lookup_prekey_servers_for(PurpleAccount *account, send_iq(pc, server, NS_DISCO_ITEMS, iq_handle); free(server); - free(nwho); + g_free(nwho); return 1; } diff --git a/prekey-plugin-account.c b/prekey-plugin-account.c index 1c520b7..1038b49 100644 --- a/prekey-plugin-account.c +++ b/prekey-plugin-account.c @@ -104,9 +104,6 @@ get_prekey_client_for_publishing(PurpleAccount *account, otrng_client_s *client, void low_prekey_messages_in_storage_cb(otrng_client_s *client, char *server_identity, void *ctx) { - otrng_debug_fprintf(stderr, - "[%s] Prekey Server: Publishing prekey messages.\n", - client->client_id.account); // TODO: @ola // Once ensure_state can handle prekey messages, it should be called here // And then trigger a maybe_publish later @@ -124,41 +121,14 @@ int build_prekey_publication_message_cb( return 0; } - FILE *prekeyf = NULL; - gchar *prekeysfile = - g_build_filename(purple_user_dir(), PREKEYS_FILE_NAME, NULL); - if (!prekeysfile) { - fprintf(stderr, _("Out of memory building filenames!\n")); - otrng_debug_exit("build_prekey_publication_message_cb"); - return 0; - } - - prekeyf = g_fopen(prekeysfile, "w+b"); - g_free(prekeysfile); - -#ifndef WIN32 - mode_t mask = umask(0077); - umask(mask); -#endif /* WIN32 */ - - if (!prekeyf) { - fprintf(stderr, _("Could not write prekey messages file\n")); - otrng_debug_exit("build_prekey_publication_message_cb"); - return 0; - } - otrng_client_ensure_correct_state(client); - // TODO: @ola continue here - we should not create prekey messages here - // instead, they should be done in the orchestration part - - msg->num_prekey_messages = client->prekey_msgs_num_to_publish; - msg->prekey_messages = otrng_client_build_prekey_messages( - msg->num_prekey_messages, client, &msg->ecdh_keys, &msg->dh_keys); + otrng_prekey_client_add_prekey_messages_for_publication(client, msg); - if (msg->num_prekey_messages > 0 && !msg->prekey_messages) { - otrng_debug_exit("build_prekey_publication_message_cb"); - return 0; + if (msg->num_prekey_messages > 0) { + otrng_debug_fprintf(stderr, + "[%s] Prekey Server: Publishing %d Prekey Messages\n", + client->client_id.account, msg->num_prekey_messages); } otrng_client_profile_s *client_profile = @@ -183,17 +153,8 @@ int build_prekey_publication_message_cb( client->client_id.account); msg->prekey_profile = otrng_xmalloc_z(sizeof(otrng_prekey_profile_s)); otrng_prekey_profile_copy(msg->prekey_profile, prekey_profile); - - // TODO: this shouldn't really be necessary now - *msg->prekey_profile_key = *prekey_profile->keys->priv; - } - - if (!otrng_global_state_prekey_messages_write_to(otrng_state, prekeyf)) { - otrng_debug_exit("build_prekey_publication_message_cb"); - return 0; } - fclose(prekeyf); otrng_debug_exit("build_prekey_publication_message_cb"); return 1; } diff --git a/prekeys.c b/prekeys.c new file mode 100644 index 0000000..2f21445 --- /dev/null +++ b/prekeys.c @@ -0,0 +1,42 @@ +/* + * Off-the-Record Messaging plugin for pidgin + * Copyright (C) 2004-2018 Ian Goldberg, Rob Smits, + * Chris Alexander, Willy Lew, + * Nikita Borisov + * + * The pidgin-otrng contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "persistance.h" +#include "pidgin-helpers.h" +#include "prekeys.h" + +extern otrng_global_state_s *otrng_state; + +static void load_prekey_messages(otrng_client_s *client) { + persistance_read_prekey_messages(otrng_state); +} + +static void store_prekey_messages(otrng_client_s *client) { + persistance_write_prekey_messages(otrng_state); +} + +void prekeys_set_callbacks(otrng_client_callbacks_s *callbacks) { + callbacks->load_prekey_messages = load_prekey_messages; + callbacks->store_prekey_messages = store_prekey_messages; +} diff --git a/prekeys.h b/prekeys.h new file mode 100644 index 0000000..2d2faa6 --- /dev/null +++ b/prekeys.h @@ -0,0 +1,33 @@ +/* + * Off-the-Record Messaging plugin for pidgin + * Copyright (C) 2004-2018 Ian Goldberg, Rob Smits, + * Chris Alexander, Willy Lew, + * Nikita Borisov + * + * The pidgin-otrng contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __OTRNG_PREKEYS_H__ +#define __OTRNG_PREKEYS_H__ + +#include + +#include +#include + +void prekeys_set_callbacks(otrng_client_callbacks_s *callbacks); + +#endif