1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Introduce a shared implementation for keyfile loading

Instead of copy&pasting the same code over again, use a common
implementation.

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
This commit is contained in:
Steffen Jaeckel 2023-08-29 13:46:07 +02:00
parent 3ebc1bee2e
commit fa714f7981
13 changed files with 200 additions and 314 deletions

View File

@ -50,6 +50,7 @@
#include <curl/easy.h>
#include <glib.h>
#include <gio/gio.h>
#include <glib/gstdio.h>
#ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
@ -61,6 +62,7 @@
#include "log.h"
#include "common.h"
#include "config/files.h"
#ifdef HAVE_GIT_VERSION
#include "gitversion.h"
@ -73,6 +75,7 @@ struct curl_data_t
};
static size_t _data_callback(void* ptr, size_t size, size_t nmemb, void* data);
static gchar* _get_file_or_linked(gchar* loc);
/**
* Frees the memory allocated for a gchar* string.
@ -127,6 +130,74 @@ auto_free_char(char** str)
free(*str);
}
static gboolean
_load_keyfile(prof_keyfile_t* keyfile)
{
GError* error = NULL;
keyfile->keyfile = g_key_file_new();
if (g_key_file_load_from_file(keyfile->keyfile, keyfile->filename, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error)) {
return TRUE;
} else if (error->code != G_FILE_ERROR_NOENT) {
log_warning("[Keyfile] error loading %s: %s", keyfile->filename, error->message);
g_error_free(error);
} else {
log_warning("[Keyfile] no such file: %s", keyfile->filename);
g_error_free(error);
}
return FALSE;
}
gboolean
load_data_keyfile(prof_keyfile_t* keyfile, const char* filename)
{
auto_gchar gchar* loc = files_get_data_path(filename);
return load_custom_keyfile(keyfile, _get_file_or_linked(loc));
}
gboolean
load_config_keyfile(prof_keyfile_t* keyfile, const char* filename)
{
auto_gchar gchar* loc = files_get_config_path(filename);
return load_custom_keyfile(keyfile, _get_file_or_linked(loc));
}
gboolean
load_custom_keyfile(prof_keyfile_t* keyfile, gchar* filename)
{
keyfile->filename = filename;
if (g_file_test(keyfile->filename, G_FILE_TEST_EXISTS)) {
g_chmod(keyfile->filename, S_IRUSR | S_IWUSR);
}
return _load_keyfile(keyfile);
}
gboolean
save_keyfile(prof_keyfile_t* keyfile)
{
GError* error = NULL;
if (!g_key_file_save_to_file(keyfile->keyfile, keyfile->filename, &error)) {
log_error("[Keyfile]: saving file %s failed! %s", keyfile->filename, error->message);
g_error_free(error);
return FALSE;
}
g_chmod(keyfile->filename, S_IRUSR | S_IWUSR);
return TRUE;
}
void
free_keyfile(prof_keyfile_t* keyfile)
{
log_debug("[Keyfile]: free %s", STR_MAYBE_NULL(keyfile->filename));
if (keyfile->keyfile)
g_key_file_free(keyfile->keyfile);
keyfile->keyfile = NULL;
g_free(keyfile->filename);
keyfile->filename = NULL;
}
gboolean
string_to_verbosity(const char* cmd, int* verbosity, gchar** err_msg)
{
@ -325,9 +396,10 @@ _data_callback(void* ptr, size_t size, size_t nmemb, void* data)
return realsize;
}
gchar*
get_file_or_linked(gchar* loc, gchar* basedir)
static gchar*
_get_file_or_linked(gchar* loc)
{
auto_gchar gchar* basedir = g_path_get_dirname(loc);
gchar* true_loc = g_strdup(loc);
// check for symlink

View File

@ -110,6 +110,23 @@ void auto_free_guchar(guchar** str);
#define STR_MAYBE_NULL(p) (p)
#endif
typedef struct prof_keyfile_t
{
gchar* filename;
GKeyFile* keyfile;
} prof_keyfile_t;
gboolean
load_data_keyfile(prof_keyfile_t* keyfile, const char* filename);
gboolean
load_config_keyfile(prof_keyfile_t* keyfile, const char* filename);
gboolean
load_custom_keyfile(prof_keyfile_t* keyfile, gchar* filename);
gboolean
save_keyfile(prof_keyfile_t* keyfile);
void
free_keyfile(prof_keyfile_t* keyfile);
/* Our own define of MB_CUR_MAX but this time at compile time */
#define PROF_MB_CUR_MAX 8
@ -157,7 +174,6 @@ int utf8_display_len(const char* const str);
char* release_get_latest(void);
gboolean release_is_new(char* found_version);
gchar* get_file_or_linked(gchar* loc, gchar* basedir);
char* strip_arg_quotes(const char* const input);
gboolean is_notify_enabled(void);

View File

@ -53,7 +53,7 @@
#include "xmpp/xmpp.h"
#include "xmpp/jid.h"
static gchar* accounts_loc;
static prof_keyfile_t accounts_prof_keyfile;
static GKeyFile* accounts;
static Autocomplete all_ac;
@ -67,14 +67,8 @@ accounts_load(void)
log_info("Loading accounts");
all_ac = autocomplete_new();
enabled_ac = autocomplete_new();
accounts_loc = files_get_data_path(FILE_ACCOUNTS);
if (g_file_test(accounts_loc, G_FILE_TEST_EXISTS)) {
g_chmod(accounts_loc, S_IRUSR | S_IWUSR);
}
accounts = g_key_file_new();
g_key_file_load_from_file(accounts, accounts_loc, G_KEY_FILE_KEEP_COMMENTS, NULL);
load_data_keyfile(&accounts_prof_keyfile, FILE_ACCOUNTS);
accounts = accounts_prof_keyfile.keyfile;
// create the logins searchable list for autocompletion
gsize naccounts;
@ -93,7 +87,8 @@ accounts_close(void)
{
autocomplete_free(all_ac);
autocomplete_free(enabled_ac);
g_key_file_free(accounts);
free_keyfile(&accounts_prof_keyfile);
accounts = NULL;
}
char*
@ -959,11 +954,5 @@ accounts_get_login_status(const char* const account_name)
static void
_save_accounts(void)
{
gsize g_data_size;
auto_gchar gchar* g_accounts_data = g_key_file_to_data(accounts, &g_data_size, NULL);
auto_gchar gchar* base = g_path_get_dirname(accounts_loc);
auto_gchar gchar* true_loc = get_file_or_linked(accounts_loc, base);
g_file_set_contents(true_loc, g_accounts_data, g_data_size, NULL);
g_chmod(accounts_loc, S_IRUSR | S_IWUSR);
save_keyfile(&accounts_prof_keyfile);
}

View File

@ -161,8 +161,7 @@ gchar*
files_get_data_path(const char* const location)
{
auto_gchar gchar* xdg_data = _files_get_xdg_data_home();
gchar* result = g_strdup_printf("%s/profanity/%s", xdg_data, location);
return result;
return g_strdup_printf("%s/profanity/%s", xdg_data, location);
}
gchar*

View File

@ -69,7 +69,7 @@
#define INPBLOCK_DEFAULT 1000
static gchar* prefs_loc;
static prof_keyfile_t prefs_prof_keyfile;
static GKeyFile* prefs;
gint log_maxsize = 0;
@ -234,37 +234,20 @@ _prefs_close(void)
void
prefs_reload(void)
{
/*
* Current function contains copy-paste, but we wanted to avoid config_file
* manipulation from prefs_load/prefs_close
*/
_prefs_close();
g_key_file_free(prefs);
prefs = NULL;
prefs = g_key_file_new();
g_key_file_load_from_file(prefs, prefs_loc, G_KEY_FILE_KEEP_COMMENTS, NULL);
_prefs_load();
auto_gchar gchar* loc = g_strdup(prefs_prof_keyfile.filename);
prefs_close();
prefs_load(loc);
}
void
prefs_load(const char* config_file)
{
if (config_file == NULL) {
prefs_loc = files_get_config_path(FILE_PROFRC);
load_config_keyfile(&prefs_prof_keyfile, FILE_PROFRC);
} else {
prefs_loc = g_strdup(config_file);
load_custom_keyfile(&prefs_prof_keyfile, g_strdup(config_file));
}
if (g_file_test(prefs_loc, G_FILE_TEST_EXISTS)) {
g_chmod(prefs_loc, S_IRUSR | S_IWUSR);
}
prefs = g_key_file_new();
g_key_file_load_from_file(prefs, prefs_loc, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
prefs = prefs_prof_keyfile.keyfile;
_prefs_load();
}
@ -280,11 +263,8 @@ prefs_close(void)
{
_prefs_close();
g_key_file_free(prefs);
free_keyfile(&prefs_prof_keyfile);
prefs = NULL;
g_free(prefs_loc);
prefs_loc = NULL;
}
gchar*
@ -1689,13 +1669,7 @@ prefs_free_aliases(GList* aliases)
static void
_save_prefs(void)
{
gsize g_data_size;
auto_gchar gchar* g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL);
auto_gchar gchar* base = g_path_get_dirname(prefs_loc);
auto_gchar gchar* true_loc = get_file_or_linked(prefs_loc, base);
g_file_set_contents(true_loc, g_prefs_data, g_data_size, NULL);
g_chmod(prefs_loc, S_IRUSR | S_IWUSR);
save_keyfile(&prefs_prof_keyfile);
}
// get the preference group for a specific preference

View File

@ -47,7 +47,7 @@
#include "config/tlscerts.h"
#include "tools/autocomplete.h"
static char* tlscerts_loc;
static prof_keyfile_t tlscerts_prof_keyfile;
static GKeyFile* tlscerts;
static void _save_tlscerts(void);
@ -60,14 +60,8 @@ void
tlscerts_init(void)
{
log_info("Loading TLS certificates");
tlscerts_loc = files_get_data_path(FILE_TLSCERTS);
if (g_file_test(tlscerts_loc, G_FILE_TEST_EXISTS)) {
g_chmod(tlscerts_loc, S_IRUSR | S_IWUSR);
}
tlscerts = g_key_file_new();
g_key_file_load_from_file(tlscerts, tlscerts_loc, G_KEY_FILE_KEEP_COMMENTS, NULL);
load_data_keyfile(&tlscerts_prof_keyfile, FILE_TLSCERTS);
tlscerts = tlscerts_prof_keyfile.keyfile;
certs_ac = autocomplete_new();
gsize len = 0;
@ -365,7 +359,7 @@ tlscerts_free(TLSCertificate* cert)
void
tlscerts_close(void)
{
g_key_file_free(tlscerts);
free_keyfile(&tlscerts_prof_keyfile);
tlscerts = NULL;
free(current_fp);
@ -377,8 +371,5 @@ tlscerts_close(void)
static void
_save_tlscerts(void)
{
gsize g_data_size;
auto_gchar gchar* g_tlscerts_data = g_key_file_to_data(tlscerts, &g_data_size, NULL);
g_file_set_contents(tlscerts_loc, g_tlscerts_data, g_data_size, NULL);
g_chmod(tlscerts_loc, S_IRUSR | S_IWUSR);
save_keyfile(&tlscerts_prof_keyfile);
}

View File

@ -103,15 +103,11 @@ struct omemo_context_t
GHashTable* pre_key_store;
GHashTable* signed_pre_key_store;
identity_key_store_t identity_key_store;
GString* identity_filename;
GKeyFile* identity_keyfile;
GString* trust_filename;
GKeyFile* trust_keyfile;
GString* sessions_filename;
GKeyFile* sessions_keyfile;
prof_keyfile_t identity;
prof_keyfile_t trust;
prof_keyfile_t sessions;
prof_keyfile_t knowndevices;
GHashTable* known_devices;
GString* known_devices_filename;
GKeyFile* known_devices_keyfile;
GHashTable* fingerprint_ac;
};
@ -144,8 +140,6 @@ omemo_close(void)
void
omemo_on_connect(ProfAccount* account)
{
GError* error = NULL;
if (signal_context_create(&omemo_ctx.signal, &omemo_ctx) != 0) {
cons_show("Error initializing OMEMO context");
return;
@ -236,64 +230,26 @@ omemo_on_connect(ProfAccount* account)
return;
}
omemo_ctx.identity_filename = g_string_new(omemo_dir);
g_string_append(omemo_ctx.identity_filename, "/identity.txt");
omemo_ctx.trust_filename = g_string_new(omemo_dir);
g_string_append(omemo_ctx.trust_filename, "/trust.txt");
omemo_ctx.sessions_filename = g_string_new(omemo_dir);
g_string_append(omemo_ctx.sessions_filename, "/sessions.txt");
omemo_ctx.known_devices_filename = g_string_new(omemo_dir);
g_string_append(omemo_ctx.known_devices_filename, "/known_devices.txt");
if (!load_custom_keyfile(&omemo_ctx.identity, g_strdup_printf("%s/%s", omemo_dir, "identity.txt")) || !_load_identity()) {
return;
}
if (!load_custom_keyfile(&omemo_ctx.trust, g_strdup_printf("%s/%s", omemo_dir, "trust.txt"))) {
return;
}
_load_trust();
if (!load_custom_keyfile(&omemo_ctx.sessions, g_strdup_printf("%s/%s", omemo_dir, "sessions.txt"))) {
return;
}
_load_sessions();
if (!load_custom_keyfile(&omemo_ctx.knowndevices, g_strdup_printf("%s/%s", omemo_dir, "known_devices.txt"))) {
return;
}
_load_known_devices();
omemo_devicelist_subscribe();
omemo_ctx.identity_keyfile = g_key_file_new();
omemo_ctx.trust_keyfile = g_key_file_new();
omemo_ctx.sessions_keyfile = g_key_file_new();
omemo_ctx.known_devices_keyfile = g_key_file_new();
if (g_key_file_load_from_file(omemo_ctx.identity_keyfile, omemo_ctx.identity_filename->str, G_KEY_FILE_KEEP_COMMENTS, &error)) {
if (!_load_identity()) {
return;
}
} else if (error->code != G_FILE_ERROR_NOENT) {
log_warning("[OMEMO] error loading identity from: %s, %s", omemo_ctx.identity_filename->str, error->message);
g_error_free(error);
return;
}
error = NULL;
if (g_key_file_load_from_file(omemo_ctx.trust_keyfile, omemo_ctx.trust_filename->str, G_KEY_FILE_KEEP_COMMENTS, &error)) {
_load_trust();
} else if (error->code != G_FILE_ERROR_NOENT) {
log_warning("[OMEMO] error loading trust from: %s, %s", omemo_ctx.trust_filename->str, error->message);
g_error_free(error);
} else {
log_warning("[OMEMO] no such file: %s", omemo_ctx.trust_filename->str);
g_error_free(error);
}
error = NULL;
if (g_key_file_load_from_file(omemo_ctx.sessions_keyfile, omemo_ctx.sessions_filename->str, G_KEY_FILE_KEEP_COMMENTS, &error)) {
_load_sessions();
} else if (error->code != G_FILE_ERROR_NOENT) {
log_warning("[OMEMO] error loading sessions from: %s, %s", omemo_ctx.sessions_filename->str, error->message);
g_error_free(error);
} else {
log_warning("[OMEMO] no such file: %s", omemo_ctx.trust_filename->str);
g_error_free(error);
}
error = NULL;
if (g_key_file_load_from_file(omemo_ctx.known_devices_keyfile, omemo_ctx.known_devices_filename->str, G_KEY_FILE_KEEP_COMMENTS, &error)) {
_load_known_devices();
} else if (error->code != G_FILE_ERROR_NOENT) {
log_warning("[OMEMO] error loading known devices from: %s, %s", omemo_ctx.known_devices_filename->str, error->message);
g_error_free(error);
} else {
log_warning("[OMEMO] no such file: %s", omemo_ctx.trust_filename->str);
g_error_free(error);
}
}
void
@ -309,15 +265,10 @@ omemo_on_disconnect(void)
glib_hash_table_free(omemo_ctx.device_list_handler);
glib_hash_table_free(omemo_ctx.device_list);
g_string_free(omemo_ctx.identity_filename, TRUE);
g_key_file_free(omemo_ctx.identity_keyfile);
g_string_free(omemo_ctx.trust_filename, TRUE);
g_key_file_free(omemo_ctx.trust_keyfile);
g_string_free(omemo_ctx.sessions_filename, TRUE);
g_key_file_free(omemo_ctx.sessions_keyfile);
glib_hash_table_free(omemo_ctx.session_store);
g_string_free(omemo_ctx.known_devices_filename, TRUE);
g_key_file_free(omemo_ctx.known_devices_keyfile);
free_keyfile(&omemo_ctx.knowndevices);
free_keyfile(&omemo_ctx.sessions);
free_keyfile(&omemo_ctx.trust);
free_keyfile(&omemo_ctx.identity);
signal_protocol_store_context_destroy(omemo_ctx.store);
ec_public_key* pub = ratchet_identity_key_pair_get_public(omemo_ctx.identity_key_pair);
@ -341,7 +292,7 @@ omemo_generate_crypto_materials(ProfAccount* account)
/* Device ID */
gcry_randomize(&omemo_ctx.device_id, 4, GCRY_VERY_STRONG_RANDOM);
omemo_ctx.device_id &= 0x7fffffff;
g_key_file_set_uint64(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_DEVICE_ID, omemo_ctx.device_id);
g_key_file_set_uint64(omemo_ctx.identity.keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_DEVICE_ID, omemo_ctx.device_id);
log_info("[OMEMO] device id: %d", omemo_ctx.device_id);
/* Identity key */
@ -349,15 +300,15 @@ omemo_generate_crypto_materials(ProfAccount* account)
ec_public_key_serialize(&omemo_ctx.identity_key_store.public, ratchet_identity_key_pair_get_public(omemo_ctx.identity_key_pair));
auto_gchar gchar* identity_key_public = g_base64_encode(signal_buffer_data(omemo_ctx.identity_key_store.public), signal_buffer_len(omemo_ctx.identity_key_store.public));
g_key_file_set_string(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_IDENTITY_KEY_PUBLIC, identity_key_public);
g_key_file_set_string(omemo_ctx.identity.keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_IDENTITY_KEY_PUBLIC, identity_key_public);
ec_private_key_serialize(&omemo_ctx.identity_key_store.private, ratchet_identity_key_pair_get_private(omemo_ctx.identity_key_pair));
auto_gchar gchar* identity_key_private = g_base64_encode(signal_buffer_data(omemo_ctx.identity_key_store.private), signal_buffer_len(omemo_ctx.identity_key_store.private));
g_key_file_set_string(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_IDENTITY_KEY_PRIVATE, identity_key_private);
g_key_file_set_string(omemo_ctx.identity.keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_IDENTITY_KEY_PRIVATE, identity_key_private);
/* Registration ID */
signal_protocol_key_helper_generate_registration_id(&omemo_ctx.registration_id, 0, omemo_ctx.signal);
g_key_file_set_uint64(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_REGISTRATION_ID, omemo_ctx.registration_id);
g_key_file_set_uint64(omemo_ctx.identity.keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_REGISTRATION_ID, omemo_ctx.registration_id);
/* Pre keys */
_generate_pre_keys(100);
@ -599,59 +550,43 @@ omemo_set_device_list(const char* const from, GList* device_list)
GKeyFile*
omemo_identity_keyfile(void)
{
return omemo_ctx.identity_keyfile;
return omemo_ctx.identity.keyfile;
}
void
omemo_identity_keyfile_save(void)
{
GError* error = NULL;
if (!g_key_file_save_to_file(omemo_ctx.identity_keyfile, omemo_ctx.identity_filename->str, &error)) {
log_error("[OMEMO] error saving identity to: %s, %s", omemo_ctx.identity_filename->str, error->message);
}
save_keyfile(&omemo_ctx.identity);
}
GKeyFile*
omemo_trust_keyfile(void)
{
return omemo_ctx.trust_keyfile;
return omemo_ctx.trust.keyfile;
}
void
omemo_trust_keyfile_save(void)
{
GError* error = NULL;
if (!g_key_file_save_to_file(omemo_ctx.trust_keyfile, omemo_ctx.trust_filename->str, &error)) {
log_error("[OMEMO] error saving trust to: %s, %s", omemo_ctx.trust_filename->str, error->message);
}
save_keyfile(&omemo_ctx.trust);
}
GKeyFile*
omemo_sessions_keyfile(void)
{
return omemo_ctx.sessions_keyfile;
return omemo_ctx.sessions.keyfile;
}
void
omemo_sessions_keyfile_save(void)
{
GError* error = NULL;
if (!g_key_file_save_to_file(omemo_ctx.sessions_keyfile, omemo_ctx.sessions_filename->str, &error)) {
log_error("[OMEMO] error saving sessions to: %s, %s", omemo_ctx.sessions_filename->str, error->message);
}
save_keyfile(&omemo_ctx.sessions);
}
void
omemo_known_devices_keyfile_save(void)
{
GError* error = NULL;
if (!g_key_file_save_to_file(omemo_ctx.known_devices_keyfile, omemo_ctx.known_devices_filename->str, &error)) {
log_error("[OMEMO] error saving known devices to: %s, %s", omemo_ctx.known_devices_filename->str, error->message);
}
save_keyfile(&omemo_ctx.knowndevices);
}
void
@ -694,7 +629,7 @@ omemo_start_device_session(const char* const jid, uint32_t device_id,
}
if (!contains_session(&jid_address, omemo_ctx.session_store)) {
log_debug("[OMEMO] There is no Session for %s ( %d) , building session.", jid_address.name, jid_address.device_id);
log_debug("[OMEMO] There is no Session for %s ( %d) ,? building session.", jid_address.name, jid_address.device_id);
int res;
address = malloc(sizeof(signal_protocol_address));
@ -1330,7 +1265,7 @@ omemo_untrust(const char* const jid, const char* const fingerprint_formatted)
/* Remove from keyfile */
auto_gchar gchar* device_id_str = g_strdup_printf("%d", device_id);
g_key_file_remove_key(omemo_ctx.trust_keyfile, jid, device_id_str, NULL);
g_key_file_remove_key(omemo_ctx.trust.keyfile, jid, device_id_str, NULL);
omemo_trust_keyfile_save();
}
@ -1501,7 +1436,7 @@ _load_identity(void)
/* Device ID */
error = NULL;
omemo_ctx.device_id = g_key_file_get_uint64(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_DEVICE_ID, &error);
omemo_ctx.device_id = g_key_file_get_uint64(omemo_ctx.identity.keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_DEVICE_ID, &error);
if (error != NULL) {
log_error("[OMEMO] cannot load device id: %s", error->message);
return FALSE;
@ -1510,7 +1445,7 @@ _load_identity(void)
/* Registration ID */
error = NULL;
omemo_ctx.registration_id = g_key_file_get_uint64(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_REGISTRATION_ID, &error);
omemo_ctx.registration_id = g_key_file_get_uint64(omemo_ctx.identity.keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_REGISTRATION_ID, &error);
if (error != NULL) {
log_error("[OMEMO] cannot load registration id: %s", error->message);
return FALSE;
@ -1518,7 +1453,7 @@ _load_identity(void)
/* Identity key */
error = NULL;
auto_gchar gchar* identity_key_public_b64 = g_key_file_get_string(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_IDENTITY_KEY_PUBLIC, &error);
auto_gchar gchar* identity_key_public_b64 = g_key_file_get_string(omemo_ctx.identity.keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_IDENTITY_KEY_PUBLIC, &error);
if (!identity_key_public_b64) {
log_error("[OMEMO] cannot load identity public key: %s", error->message);
return FALSE;
@ -1529,7 +1464,7 @@ _load_identity(void)
omemo_ctx.identity_key_store.public = signal_buffer_create(identity_key_public, identity_key_public_len);
error = NULL;
auto_gchar gchar* identity_key_private_b64 = g_key_file_get_string(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_IDENTITY_KEY_PRIVATE, &error);
auto_gchar gchar* identity_key_private_b64 = g_key_file_get_string(omemo_ctx.identity.keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_IDENTITY_KEY_PRIVATE, &error);
if (!identity_key_private_b64) {
log_error("[OMEMO] cannot load identity private key: %s", error->message);
return FALSE;
@ -1549,10 +1484,10 @@ _load_identity(void)
int i;
/* Pre keys */
i = 0;
keys = g_key_file_get_keys(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_PREKEYS, NULL, NULL);
keys = g_key_file_get_keys(omemo_ctx.identity.keyfile, OMEMO_STORE_GROUP_PREKEYS, NULL, NULL);
if (keys) {
for (i = 0; keys[i] != NULL; i++) {
auto_gchar gchar* pre_key_b64 = g_key_file_get_string(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_PREKEYS, keys[i], NULL);
auto_gchar gchar* pre_key_b64 = g_key_file_get_string(omemo_ctx.identity.keyfile, OMEMO_STORE_GROUP_PREKEYS, keys[i], NULL);
size_t pre_key_len;
auto_guchar guchar* pre_key = g_base64_decode(pre_key_b64, &pre_key_len);
signal_buffer* buffer = signal_buffer_create(pre_key, pre_key_len);
@ -1569,10 +1504,10 @@ _load_identity(void)
/* Signed pre keys */
i = 0;
keys = g_key_file_get_keys(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_SIGNED_PREKEYS, NULL, NULL);
keys = g_key_file_get_keys(omemo_ctx.identity.keyfile, OMEMO_STORE_GROUP_SIGNED_PREKEYS, NULL, NULL);
if (keys) {
for (i = 0; keys[i] != NULL; i++) {
auto_gchar gchar* signed_pre_key_b64 = g_key_file_get_string(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_SIGNED_PREKEYS, keys[i], NULL);
auto_gchar gchar* signed_pre_key_b64 = g_key_file_get_string(omemo_ctx.identity.keyfile, OMEMO_STORE_GROUP_SIGNED_PREKEYS, keys[i], NULL);
size_t signed_pre_key_len;
auto_guchar guchar* signed_pre_key = g_base64_decode(signed_pre_key_b64, &signed_pre_key_len);
signal_buffer* buffer = signal_buffer_create(signed_pre_key, signed_pre_key_len);
@ -1597,7 +1532,7 @@ _load_identity(void)
static void
_load_trust(void)
{
auto_gcharv gchar** groups = g_key_file_get_groups(omemo_ctx.trust_keyfile, NULL);
auto_gcharv gchar** groups = g_key_file_get_groups(omemo_ctx.trust.keyfile, NULL);
if (!groups) {
return;
@ -1612,9 +1547,9 @@ _load_trust(void)
g_hash_table_insert(omemo_ctx.identity_key_store.trusted, strdup(groups[i]), trusted);
}
auto_gcharv gchar** keys = g_key_file_get_keys(omemo_ctx.trust_keyfile, groups[i], NULL, NULL);
auto_gcharv gchar** keys = g_key_file_get_keys(omemo_ctx.trust.keyfile, groups[i], NULL, NULL);
for (int j = 0; keys[j] != NULL; j++) {
auto_gchar gchar* key_b64 = g_key_file_get_string(omemo_ctx.trust_keyfile, groups[i], keys[j], NULL);
auto_gchar gchar* key_b64 = g_key_file_get_string(omemo_ctx.trust.keyfile, groups[i], keys[j], NULL);
size_t key_len;
auto_guchar guchar* key = g_base64_decode(key_b64, &key_len);
signal_buffer* buffer = signal_buffer_create(key, key_len);
@ -1627,7 +1562,7 @@ _load_trust(void)
static void
_load_sessions(void)
{
auto_gcharv gchar** groups = g_key_file_get_groups(omemo_ctx.sessions_keyfile, NULL);
auto_gcharv gchar** groups = g_key_file_get_groups(omemo_ctx.sessions.keyfile, NULL);
if (!groups) {
return;
@ -1642,10 +1577,10 @@ _load_sessions(void)
g_hash_table_insert(omemo_ctx.session_store, strdup(groups[i]), device_store);
}
auto_gcharv gchar** keys = g_key_file_get_keys(omemo_ctx.sessions_keyfile, groups[i], NULL, NULL);
auto_gcharv gchar** keys = g_key_file_get_keys(omemo_ctx.sessions.keyfile, groups[i], NULL, NULL);
for (int j = 0; keys[j] != NULL; j++) {
uint32_t id = strtoul(keys[j], NULL, 10);
auto_gchar gchar* record_b64 = g_key_file_get_string(omemo_ctx.sessions_keyfile, groups[i], keys[j], NULL);
auto_gchar gchar* record_b64 = g_key_file_get_string(omemo_ctx.sessions.keyfile, groups[i], keys[j], NULL);
size_t record_len;
auto_guchar guchar* record = g_base64_decode(record_b64, &record_len);
signal_buffer* buffer = signal_buffer_create(record, record_len);
@ -1657,7 +1592,7 @@ _load_sessions(void)
static void
_load_known_devices(void)
{
auto_gcharv gchar** groups = g_key_file_get_groups(omemo_ctx.known_devices_keyfile, NULL);
auto_gcharv gchar** groups = g_key_file_get_groups(omemo_ctx.knowndevices.keyfile, NULL);
if (!groups) {
return;
@ -1672,10 +1607,10 @@ _load_known_devices(void)
g_hash_table_insert(omemo_ctx.known_devices, strdup(groups[i]), known_identities);
}
auto_gcharv gchar** keys = g_key_file_get_keys(omemo_ctx.known_devices_keyfile, groups[i], NULL, NULL);
auto_gcharv gchar** keys = g_key_file_get_keys(omemo_ctx.knowndevices.keyfile, groups[i], NULL, NULL);
for (int j = 0; keys[j] != NULL; j++) {
uint32_t device_id = strtoul(keys[j], NULL, 10);
auto_gchar gchar* fingerprint = g_key_file_get_string(omemo_ctx.known_devices_keyfile, groups[i], keys[j], NULL);
auto_gchar gchar* fingerprint = g_key_file_get_string(omemo_ctx.knowndevices.keyfile, groups[i], keys[j], NULL);
g_hash_table_insert(known_identities, strdup(fingerprint), GINT_TO_POINTER(device_id));
}
}
@ -1695,7 +1630,7 @@ _cache_device_identity(const char* const jid, uint32_t device_id, ec_public_key*
g_hash_table_insert(known_identities, strdup(fingerprint), GINT_TO_POINTER(device_id));
auto_gchar gchar* device_id_str = g_strdup_printf("%d", device_id);
g_key_file_set_string(omemo_ctx.known_devices_keyfile, jid, device_id_str, fingerprint);
g_key_file_set_string(omemo_ctx.knowndevices.keyfile, jid, device_id_str, fingerprint);
omemo_known_devices_keyfile_save();
Autocomplete ac = g_hash_table_lookup(omemo_ctx.fingerprint_ac, jid);

View File

@ -63,7 +63,7 @@
static const char* libversion = NULL;
static GHashTable* pubkeys;
static gchar* pubsloc;
static prof_keyfile_t pubkeys_prof_keyfile;
static GKeyFile* pubkeyfile;
static char* passphrase;
@ -136,13 +136,8 @@ p_gpg_close(void)
pubkeys = NULL;
}
if (pubkeyfile) {
g_key_file_free(pubkeyfile);
free_keyfile(&pubkeys_prof_keyfile);
pubkeyfile = NULL;
}
free(pubsloc);
pubsloc = NULL;
autocomplete_free(key_ac);
key_ac = NULL;
@ -161,19 +156,14 @@ p_gpg_close(void)
void
p_gpg_on_connect(const char* const barejid)
{
pubsloc = files_file_in_account_data_path(DIR_PGP, barejid, "pubkeys");
gchar* pubsloc = files_file_in_account_data_path(DIR_PGP, barejid, "pubkeys");
if (!pubsloc) {
log_error("Could not create directory for account %s.", barejid);
cons_show_error("Could not create directory for account %s.", barejid);
return;
}
if (g_file_test(pubsloc, G_FILE_TEST_EXISTS)) {
g_chmod(pubsloc, S_IRUSR | S_IWUSR);
}
pubkeyfile = g_key_file_new();
g_key_file_load_from_file(pubkeyfile, pubsloc, G_KEY_FILE_KEEP_COMMENTS, NULL);
load_custom_keyfile(&pubkeys_prof_keyfile, pubsloc);
pubkeyfile = pubkeys_prof_keyfile.keyfile;
// load each keyid
gsize len = 0;
@ -218,28 +208,9 @@ p_gpg_on_connect(const char* const barejid)
void
p_gpg_on_disconnect(void)
{
if (pubkeys) {
g_hash_table_destroy(pubkeys);
p_gpg_close();
pubkeys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)_p_gpg_free_pubkeyid);
}
if (pubkeyfile) {
g_key_file_free(pubkeyfile);
pubkeyfile = NULL;
}
free(pubsloc);
pubsloc = NULL;
if (passphrase) {
free(passphrase);
passphrase = NULL;
}
if (passphrase_attempt) {
free(passphrase_attempt);
passphrase_attempt = NULL;
}
key_ac = autocomplete_new();
}
gboolean
@ -973,8 +944,5 @@ _add_header_footer(const char* const str, const char* const header, const char*
static void
_save_pubkeys(void)
{
gsize g_data_size;
auto_gchar gchar* g_pubkeys_data = g_key_file_to_data(pubkeyfile, &g_data_size, NULL);
g_file_set_contents(pubsloc, g_pubkeys_data, g_data_size, NULL);
g_chmod(pubsloc, S_IRUSR | S_IWUSR);
save_keyfile(&pubkeys_prof_keyfile);
}

View File

@ -46,6 +46,7 @@
#include "config/files.h"
#include "config/conflists.h"
static prof_keyfile_t settings_prof_keyfile;
static GKeyFile* settings;
static void _save_settings(void);
@ -53,25 +54,14 @@ static void _save_settings(void);
void
plugin_settings_init(void)
{
auto_gchar gchar* settings_file = files_get_data_path(FILE_PLUGIN_SETTINGS);
if (g_file_test(settings_file, G_FILE_TEST_EXISTS)) {
g_chmod(settings_file, S_IRUSR | S_IWUSR);
}
settings = g_key_file_new();
g_key_file_load_from_file(settings, settings_file, G_KEY_FILE_KEEP_COMMENTS, NULL);
gsize g_data_size;
auto_gchar gchar* g_data = g_key_file_to_data(settings, &g_data_size, NULL);
g_file_set_contents(settings_file, g_data, g_data_size, NULL);
g_chmod(settings_file, S_IRUSR | S_IWUSR);
load_data_keyfile(&settings_prof_keyfile, FILE_PLUGIN_SETTINGS);
settings = settings_prof_keyfile.keyfile;
}
void
plugin_settings_close(void)
{
g_key_file_free(settings);
free_keyfile(&settings_prof_keyfile);
settings = NULL;
}
@ -172,12 +162,5 @@ plugin_settings_string_list_clear(const char* const group, const char* const key
static void
_save_settings(void)
{
gsize g_data_size;
auto_gchar gchar* g_data = g_key_file_to_data(settings, &g_data_size, NULL);
auto_gchar gchar* fileloc = files_get_data_path(FILE_PLUGIN_SETTINGS);
auto_gchar gchar* base = g_path_get_dirname(fileloc);
auto_gchar gchar* true_loc = get_file_or_linked(fileloc, base);
g_file_set_contents(true_loc, g_data, g_data_size, NULL);
g_chmod(fileloc, S_IRUSR | S_IWUSR);
save_keyfile(&settings_prof_keyfile);
}

View File

@ -44,30 +44,20 @@
#include "config/theme.h"
#include "config/files.h"
static prof_keyfile_t themes_prof_keyfile;
static GKeyFile* themes;
void
plugin_themes_init(void)
{
auto_gchar gchar* themes_file = files_get_data_path(FILE_PLUGIN_THEMES);
if (g_file_test(themes_file, G_FILE_TEST_EXISTS)) {
g_chmod(themes_file, S_IRUSR | S_IWUSR);
}
themes = g_key_file_new();
g_key_file_load_from_file(themes, themes_file, G_KEY_FILE_KEEP_COMMENTS, NULL);
gsize g_data_size;
auto_gchar gchar* g_data = g_key_file_to_data(themes, &g_data_size, NULL);
g_file_set_contents(themes_file, g_data, g_data_size, NULL);
g_chmod(themes_file, S_IRUSR | S_IWUSR);
load_data_keyfile(&themes_prof_keyfile, FILE_PLUGIN_THEMES);
themes = themes_prof_keyfile.keyfile;
}
void
plugin_themes_close(void)
{
g_key_file_free(themes);
free_keyfile(&themes_prof_keyfile);
themes = NULL;
}

View File

@ -46,32 +46,21 @@
#include "xmpp/xmpp.h"
static prof_keyfile_t bookmark_ignore_prof_keyfile;
static GKeyFile* bookmark_ignore_keyfile = NULL;
static gchar* account_jid = NULL;
static void
_bookmark_ignore_load()
{
auto_gchar gchar* bi_loc = files_get_data_path(FILE_BOOKMARK_AUTOJOIN_IGNORE);
if (g_file_test(bi_loc, G_FILE_TEST_EXISTS)) {
g_chmod(bi_loc, S_IRUSR | S_IWUSR);
}
bookmark_ignore_keyfile = g_key_file_new();
g_key_file_load_from_file(bookmark_ignore_keyfile, bi_loc, G_KEY_FILE_KEEP_COMMENTS, NULL);
load_data_keyfile(&bookmark_ignore_prof_keyfile, FILE_BOOKMARK_AUTOJOIN_IGNORE);
bookmark_ignore_keyfile = bookmark_ignore_prof_keyfile.keyfile;
}
static void
_bookmark_save()
{
gsize g_data_size;
auto_gchar gchar* g_bookmark_ignore_data = g_key_file_to_data(bookmark_ignore_keyfile, &g_data_size, NULL);
auto_gchar gchar* bi_loc = files_get_data_path(FILE_BOOKMARK_AUTOJOIN_IGNORE);
g_file_set_contents(bi_loc, g_bookmark_ignore_data, g_data_size, NULL);
g_chmod(bi_loc, S_IRUSR | S_IWUSR);
save_keyfile(&bookmark_ignore_prof_keyfile);
}
void
@ -86,9 +75,10 @@ bookmark_ignore_on_connect(const char* const barejid)
void
bookmark_ignore_on_disconnect()
{
g_key_file_free(bookmark_ignore_keyfile);
free_keyfile(&bookmark_ignore_prof_keyfile);
bookmark_ignore_keyfile = NULL;
g_free(account_jid);
account_jid = NULL;
}
gboolean

View File

@ -55,6 +55,7 @@
#include "xmpp/capabilities.h"
static gchar* cache_loc;
static prof_keyfile_t caps_prof_keyfile;
static GKeyFile* cache;
static GHashTable* jid_to_ver;
@ -72,14 +73,8 @@ void
caps_init(void)
{
log_info("Loading capabilities cache");
cache_loc = files_get_data_path(FILE_CAPSCACHE);
if (g_file_test(cache_loc, G_FILE_TEST_EXISTS)) {
g_chmod(cache_loc, S_IRUSR | S_IWUSR);
}
cache = g_key_file_new();
g_key_file_load_from_file(cache, cache_loc, G_KEY_FILE_KEEP_COMMENTS, NULL);
load_data_keyfile(&caps_prof_keyfile, FILE_CAPSCACHE);
cache = caps_prof_keyfile.keyfile;
jid_to_ver = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
jid_to_caps = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)caps_destroy);
@ -347,7 +342,7 @@ caps_reset_ver(void)
void
caps_close(void)
{
g_key_file_free(cache);
free_keyfile(&caps_prof_keyfile);
cache = NULL;
g_hash_table_destroy(jid_to_ver);
g_hash_table_destroy(jid_to_caps);
@ -456,8 +451,5 @@ caps_destroy(EntityCapabilities* caps)
static void
_save_cache(void)
{
gsize g_data_size;
auto_gchar gchar* g_cache_data = g_key_file_to_data(cache, &g_data_size, NULL);
g_file_set_contents(cache_loc, g_cache_data, g_data_size, NULL);
g_chmod(cache_loc, S_IRUSR | S_IWUSR);
save_keyfile(&caps_prof_keyfile);
}

View File

@ -1102,31 +1102,18 @@ _xmpp_file_logger(void* const userdata, const xmpp_log_level_t xmpp_level, const
static void
_random_bytes_init(void)
{
auto_gchar gchar* rndbytes_loc = files_get_data_path(FILE_PROFANITY_IDENTIFIER);
GKeyFile* rndbytes;
prof_keyfile_t keyfile;
load_data_keyfile(&keyfile, FILE_PROFANITY_IDENTIFIER);
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, "identifier")) {
profanity_instance_id = g_key_file_get_string(rndbytes, "identifier", "random_bytes", NULL);
if (g_key_file_has_group(keyfile.keyfile, "identifier")) {
profanity_instance_id = g_key_file_get_string(keyfile.keyfile, "identifier", "random_bytes", NULL);
} else {
profanity_instance_id = get_random_string(10);
g_key_file_set_string(rndbytes, "identifier", "random_bytes", profanity_instance_id);
gsize g_data_size;
auto_gchar gchar* g_accounts_data = g_key_file_to_data(rndbytes, &g_data_size, NULL);
auto_gchar gchar* base = g_path_get_dirname(rndbytes_loc);
auto_gchar gchar* true_loc = get_file_or_linked(rndbytes_loc, base);
g_file_set_contents(true_loc, g_accounts_data, g_data_size, NULL);
g_key_file_set_string(keyfile.keyfile, "identifier", "random_bytes", profanity_instance_id);
save_keyfile(&keyfile);
}
g_key_file_free(rndbytes);
free_keyfile(&keyfile);
}
static void