From 1e09a055ca2ac7b8738d7ade2b8f656682e2c24d Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Wed, 16 Oct 2019 10:17:34 +0200 Subject: [PATCH] Create random-bytes file So far only with dummy value. We will need an identifier that we can hash together with a message ID and put in as the origin-id. So when we receive message we can unsplit it and see if it was sent from this client. Regards https://github.com/profanity-im/profanity/issues/1207 --- src/config/files.h | 1 + src/xmpp/connection.c | 53 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/config/files.h b/src/config/files.h index f7dfa29a..975b4509 100644 --- a/src/config/files.h +++ b/src/config/files.h @@ -43,6 +43,7 @@ #define FILE_PLUGIN_SETTINGS "plugin_settings" #define FILE_PLUGIN_THEMES "plugin_themes" #define FILE_CAPSCACHE "capscache" +#define FILE_RND_INST_BYTES "randbytes" #define DIR_THEMES "themes" #define DIR_ICONS "icons" diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 46ae1ed8..5632f51d 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -38,6 +38,9 @@ #include #include +#include +#include + #ifdef HAVE_LIBMESODE #include #endif @@ -47,6 +50,7 @@ #endif #include "log.h" +#include "config/files.h" #include "config/preferences.h" #include "event/server_events.h" #include "xmpp/connection.h" @@ -69,6 +73,7 @@ typedef struct prof_conn_t { } ProfConnection; static ProfConnection conn; +static gchar *random_bytes; static xmpp_log_t* _xmpp_get_file_logger(void); static void _xmpp_file_logger(void *const userdata, const xmpp_log_level_t level, const char *const area, const char *const msg); @@ -81,6 +86,9 @@ TLSCertificate* _xmppcert_to_profcert(xmpp_tlscert_t *xmpptlscert); static int _connection_certfail_cb(xmpp_tlscert_t *xmpptlscert, const char *const errormsg); #endif +static void _random_bytes_init(); +static void _random_bytes_close(); + void connection_init(void) { @@ -95,6 +103,8 @@ connection_init(void) conn.features_by_jid = NULL; conn.available_resources = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)resource_destroy); conn.requested_features = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); + + _random_bytes_init(); } void @@ -113,6 +123,8 @@ connection_shutdown(void) free(conn.xmpp_log); conn.xmpp_log = NULL; + + _random_bytes_close(); } jabber_conn_status_t @@ -605,3 +617,44 @@ _xmpp_file_logger(void *const userdata, const xmpp_log_level_t xmpp_level, const sv_ev_xmpp_stanza(msg); } } + +static void _random_bytes_init() +{ + char *rndbytes_loc; + GKeyFile *rndbytes; + + rndbytes_loc = files_get_data_path(FILE_RND_INST_BYTES); + + if (g_file_test(rndbytes_loc, G_FILE_TEST_EXISTS)) { + g_chmod(rndbytes_loc, S_IRUSR | S_IWUSR); + } + + rndbytes = g_key_file_new(); + g_key_file_load_from_file(rndbytes, rndbytes_loc, G_KEY_FILE_KEEP_COMMENTS, NULL); + + if (g_key_file_has_group(rndbytes, "instance")) { + random_bytes = g_key_file_get_string(rndbytes, "instance", "random_bytes", NULL); + } else { + random_bytes = g_strdup("ASDF"); + g_key_file_set_string(rndbytes, "instance", "random_bytes", random_bytes); + + gsize g_data_size; + gchar *g_accounts_data = g_key_file_to_data(rndbytes, &g_data_size, NULL); + + gchar *base = g_path_get_basename(rndbytes_loc); + gchar *true_loc = get_file_or_linked(rndbytes_loc, base); + g_file_set_contents(true_loc, g_accounts_data, g_data_size, NULL); + + g_free(base); + free(true_loc); + g_free(g_accounts_data); + } + + free(rndbytes_loc); + g_key_file_free(rndbytes); +} + +static void _random_bytes_close() +{ + g_free(random_bytes); +}