From 5f015e32b263de6ebcaf9a3c6c2ffcad238410a0 Mon Sep 17 00:00:00 2001 From: Paul Fariello Date: Mon, 15 Apr 2019 22:09:47 +0200 Subject: [PATCH 1/5] Add OMEMO policy There is 3 policy: - manual: OMEMO session are only started manually - automatic: OMEMO session are only started if they have been started manually before - always: OMEMO session are always started unless they have been ended manually before Closes #1040 and fixes #1052 --- src/command/cmd_ac.c | 14 ++++++++ src/command/cmd_defs.c | 5 +++ src/command/cmd_funcs.c | 39 ++++++++++++++++++-- src/command/cmd_funcs.h | 1 + src/config/account.c | 15 +++++++- src/config/account.h | 6 +++- src/config/accounts.c | 67 +++++++++++++++++++++++++++++++++- src/config/accounts.h | 2 ++ src/config/preferences.c | 5 +++ src/config/preferences.h | 1 + src/omemo/omemo.c | 77 ++++++++++++++++++++++++++++++++-------- src/omemo/omemo.h | 8 ++++- src/ui/chatwin.c | 10 ++++++ src/ui/console.c | 4 +++ src/ui/core.c | 2 +- src/ui/mucwin.c | 19 ++++++++++ src/ui/ui.h | 1 + 17 files changed, 253 insertions(+), 23 deletions(-) diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c index 0cc28bb3..a2d73671 100644 --- a/src/command/cmd_ac.c +++ b/src/command/cmd_ac.c @@ -164,6 +164,7 @@ static Autocomplete otr_log_ac; static Autocomplete otr_policy_ac; static Autocomplete omemo_ac; static Autocomplete omemo_log_ac; +static Autocomplete omemo_policy_ac; static Autocomplete connect_property_ac; static Autocomplete tls_property_ac; static Autocomplete alias_ac; @@ -591,12 +592,18 @@ cmd_ac_init(void) autocomplete_add(omemo_ac, "untrust"); autocomplete_add(omemo_ac, "fingerprint"); autocomplete_add(omemo_ac, "clear_device_list"); + autocomplete_add(omemo_ac, "policy"); omemo_log_ac = autocomplete_new(); autocomplete_add(omemo_log_ac, "on"); autocomplete_add(omemo_log_ac, "off"); autocomplete_add(omemo_log_ac, "redact"); + omemo_policy_ac = autocomplete_new(); + autocomplete_add(omemo_policy_ac, "manual"); + autocomplete_add(omemo_policy_ac, "automatic"); + autocomplete_add(omemo_policy_ac, "always"); + connect_property_ac = autocomplete_new(); autocomplete_add(connect_property_ac, "server"); autocomplete_add(connect_property_ac, "port"); @@ -1080,6 +1087,7 @@ cmd_ac_reset(ProfWin *window) autocomplete_reset(otr_policy_ac); autocomplete_reset(omemo_ac); autocomplete_reset(omemo_log_ac); + autocomplete_reset(omemo_policy_ac); autocomplete_reset(connect_property_ac); autocomplete_reset(tls_property_ac); autocomplete_reset(alias_ac); @@ -1209,6 +1217,7 @@ cmd_ac_uninit(void) autocomplete_free(otr_policy_ac); autocomplete_free(omemo_ac); autocomplete_free(omemo_log_ac); + autocomplete_free(omemo_policy_ac); autocomplete_free(connect_property_ac); autocomplete_free(tls_property_ac); autocomplete_free(alias_ac); @@ -2191,6 +2200,11 @@ _omemo_autocomplete(ProfWin *window, const char *const input, gboolean previous) return found; } + found = autocomplete_param_with_ac(input, "/omemo policy", omemo_policy_ac, TRUE, previous); + if (found) { + return found; + } + found = autocomplete_param_with_ac(input, "/omemo", omemo_ac, TRUE, previous); if (found) { return found; diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index ee86aaba..358c8d40 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -2342,6 +2342,7 @@ static struct cmd_t command_defs[] = { "untrust", cmd_omemo_untrust }, { "fingerprint", cmd_omemo_fingerprint }, { "char", cmd_omemo_char }, + { "policy", cmd_omemo_policy }, { "clear_device_list", cmd_omemo_clear_device_list }) CMD_NOMAINFUNC CMD_TAGS( @@ -2355,6 +2356,7 @@ static struct cmd_t command_defs[] = "/omemo end", "/omemo fingerprint []", "/omemo char ", + "/omemo policy manual|automatic|always", "/omemo clear_device_list") CMD_DESC( "OMEMO commands to manage keys, and perform encryption during chat sessions.") @@ -2366,6 +2368,9 @@ static struct cmd_t command_defs[] = { "log redact", "Log OMEMO encrypted messages, but replace the contents with [redacted]. This is the default." }, { "fingerprint []", "Show contact fingerprints, or current recipient if omitted." }, { "char ", "Set the character to be displayed next to OMEMO encrypted messages." }, + { "policy manual", "Set the global OMEMO policy to manual, OMEMO sessions must be started manually." }, + { "policy automatic", "Set the global OMEMO policy to opportunistic, an OMEMO session will be attempted upon starting a conversation." }, + { "policy always", "Set the global OMEMO policy to always, an error will be displayed if an OMEMO session cannot be initiated upon starting a conversation." }, { "clear_device_list", "Clear your own device list on server side. Each client will reannounce itself when connected back."}) CMD_EXAMPLES( "/omemo gen", diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 3bba5cb5..abd83492 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -2152,7 +2152,7 @@ cmd_msg(ProfWin *window, const char *const command, gchar **args) #ifdef HAVE_OMEMO #ifndef HAVE_LIBOTR - if (omemo_is_trusted_jid(barejid)) { + if (omemo_automatic_start(barejid)) { omemo_start_session(barejid); chatwin->is_omemo = TRUE; } @@ -2167,10 +2167,10 @@ cmd_msg(ProfWin *window, const char *const command, gchar **args) #ifdef HAVE_OMEMO #ifdef HAVE_LIBOTR - if (omemo_is_trusted_jid(barejid) && otr_is_secure(barejid)) { + if (omemo_automatic_start(barejid) && otr_is_secure(barejid)) { win_println(window, THEME_DEFAULT, '!', "Chat could be either OMEMO or OTR encrypted. Use '/omemo start %s' or '/otr start %s' to start a session.", usr, usr); return TRUE; - } else if (omemo_is_trusted_jid(barejid)) { + } else if (omemo_automatic_start(barejid)) { omemo_start_session(barejid); chatwin->is_omemo = TRUE; } @@ -8005,6 +8005,7 @@ cmd_omemo_start(ProfWin *window, const char *const command, gchar **args) return TRUE; } + accounts_add_omemo_state(session_get_account_name(), barejid, TRUE); omemo_start_session(barejid); chatwin->is_omemo = TRUE; } else { @@ -8026,6 +8027,7 @@ cmd_omemo_start(ProfWin *window, const char *const command, gchar **args) return TRUE; } + accounts_add_omemo_state(session_get_account_name(), chatwin->barejid, TRUE); omemo_start_session(chatwin->barejid); chatwin->is_omemo = TRUE; } else if (window->type == WIN_MUC) { @@ -8033,6 +8035,7 @@ cmd_omemo_start(ProfWin *window, const char *const command, gchar **args) assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK); if (muc_anonymity_type(mucwin->roomjid) == MUC_ANONYMITY_TYPE_NONANONYMOUS) { + accounts_add_omemo_state(session_get_account_name(), mucwin->roomjid, TRUE); omemo_start_muc_sessions(mucwin->roomjid); mucwin->is_omemo = TRUE; } else { @@ -8119,6 +8122,7 @@ cmd_omemo_end(ProfWin *window, const char *const command, gchar **args) } chatwin->is_omemo = FALSE; + accounts_add_omemo_state(session_get_account_name(), chatwin->barejid, FALSE); } else if (window->type == WIN_MUC) { ProfMucWin *mucwin = (ProfMucWin*)window; assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK); @@ -8129,6 +8133,7 @@ cmd_omemo_end(ProfWin *window, const char *const command, gchar **args) } mucwin->is_omemo = FALSE; + accounts_add_omemo_state(session_get_account_name(), mucwin->roomjid, FALSE); } else { win_println(window, THEME_DEFAULT, '-', "You must be in a regular chat window to start an OMEMO session."); return TRUE; @@ -8366,3 +8371,31 @@ cmd_omemo_clear_device_list(ProfWin *window, const char *const command, gchar ** return TRUE; #endif } + +gboolean +cmd_omemo_policy(ProfWin *window, const char *const command, gchar **args) +{ +#ifdef HAVE_OMEMO + if (args[1] == NULL) { + char *policy = prefs_get_string(PREF_OMEMO_POLICY); + cons_show("OMEMO policy is now set to: %s", policy); + prefs_free_string(policy); + return TRUE; + } + + char *choice = args[1]; + if ((g_strcmp0(choice, "manual") != 0) && + (g_strcmp0(choice, "automatic") != 0) && + (g_strcmp0(choice, "always") != 0)) { + cons_show("OMEMO policy can be set to: manual, automatic or always."); + return TRUE; + } + + prefs_set_string(PREF_OMEMO_POLICY, choice); + cons_show("OMEMO policy is now set to: %s", choice); + return TRUE; +#else + cons_show("This version of Profanity has not been built with OMEMO support enabled"); + return TRUE; +#endif +} diff --git a/src/command/cmd_funcs.h b/src/command/cmd_funcs.h index 249b50fe..8f8d60a9 100644 --- a/src/command/cmd_funcs.h +++ b/src/command/cmd_funcs.h @@ -222,6 +222,7 @@ gboolean cmd_omemo_end(ProfWin *window, const char *const command, gchar **args) gboolean cmd_omemo_fingerprint(ProfWin *window, const char *const command, gchar **args); gboolean cmd_omemo_trust(ProfWin *window, const char *const command, gchar **args); gboolean cmd_omemo_untrust(ProfWin *window, const char *const command, gchar **args); +gboolean cmd_omemo_policy(ProfWin *window, const char *const command, gchar **args); gboolean cmd_omemo_clear_device_list(ProfWin *window, const char *const command, gchar **args); #endif diff --git a/src/config/account.c b/src/config/account.c index 93ba5078..0c23c585 100644 --- a/src/config/account.c +++ b/src/config/account.c @@ -52,7 +52,8 @@ account_new(const gchar *const name, const gchar *const jid, int priority_away, int priority_xa, int priority_dnd, const gchar *const muc_service, const gchar *const muc_nick, const gchar *const otr_policy, GList *otr_manual, GList *otr_opportunistic, - GList *otr_always, const gchar *const pgp_keyid, const char *const startscript, + GList *otr_always, const gchar *const omemo_policy, GList *omemo_enabled, + GList *omemo_disabled, const gchar *const pgp_keyid, const char *const startscript, const char *const theme, gchar *tls_policy) { ProfAccount *new_account = malloc(sizeof(ProfAccount)); @@ -139,6 +140,15 @@ account_new(const gchar *const name, const gchar *const jid, new_account->otr_opportunistic = otr_opportunistic; new_account->otr_always = otr_always; + if (omemo_policy) { + new_account->omemo_policy = strdup(omemo_policy); + } else { + new_account->omemo_policy = NULL; + } + + new_account->omemo_enabled = omemo_enabled; + new_account->omemo_disabled = omemo_disabled; + if (pgp_keyid != NULL) { new_account->pgp_keyid = strdup(pgp_keyid); } else { @@ -232,6 +242,7 @@ account_free(ProfAccount *account) free(account->muc_service); free(account->muc_nick); free(account->otr_policy); + free(account->omemo_policy); free(account->pgp_keyid); free(account->startscript); free(account->theme); @@ -239,6 +250,8 @@ account_free(ProfAccount *account) g_list_free_full(account->otr_manual, g_free); g_list_free_full(account->otr_opportunistic, g_free); g_list_free_full(account->otr_always, g_free); + g_list_free_full(account->omemo_enabled, g_free); + g_list_free_full(account->omemo_disabled, g_free); free(account); } diff --git a/src/config/account.h b/src/config/account.h index 68264c47..17b57a46 100644 --- a/src/config/account.h +++ b/src/config/account.h @@ -59,6 +59,9 @@ typedef struct prof_account_t { GList *otr_manual; GList *otr_opportunistic; GList *otr_always; + gchar *omemo_policy; + GList *omemo_enabled; + GList *omemo_disabled; gchar *pgp_keyid; gchar *startscript; gchar *theme; @@ -72,7 +75,8 @@ ProfAccount* account_new(const gchar *const name, const gchar *const jid, int priority_away, int priority_xa, int priority_dnd, const gchar *const muc_service, const gchar *const muc_nick, const gchar *const otr_policy, GList *otr_manual, GList *otr_opportunistic, - GList *otr_always, const gchar *const pgp_keyid, const char *const startscript, + GList *otr_always, const gchar *const omemo_policy, GList *omemo_enabled, + GList *omemo_disabled, const gchar *const pgp_keyid, const char *const startscript, const char *const theme, gchar *tls_policy); char* account_create_connect_jid(ProfAccount *account); gboolean account_eval_password(ProfAccount *account); diff --git a/src/config/accounts.c b/src/config/accounts.c index 1c6441db..fa8b9c8f 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -275,6 +275,31 @@ accounts_get_account(const char *const name) g_strfreev(always); } + gchar *omemo_policy = NULL; + if (g_key_file_has_key(accounts, name, "omemo.policy", NULL)) { + omemo_policy = g_key_file_get_string(accounts, name, "omemo.policy", NULL); + } + + GList *omemo_enabled = NULL; + gchar **enabled_list = g_key_file_get_string_list(accounts, name, "omemo.enabled", &length, NULL); + if (enabled_list) { + int i = 0; + for (i = 0; i < length; i++) { + omemo_enabled = g_list_append(omemo_enabled, strdup(enabled_list[i])); + } + g_strfreev(enabled_list); + } + + GList *omemo_disabled = NULL; + gchar **disabled_list = g_key_file_get_string_list(accounts, name, "omemo.disabled", &length, NULL); + if (disabled_list) { + int i = 0; + for (i = 0; i < length; i++) { + omemo_disabled = g_list_append(omemo_disabled, strdup(disabled_list[i])); + } + g_strfreev(disabled_list); + } + gchar *pgp_keyid = NULL; if (g_key_file_has_key(accounts, name, "pgp.keyid", NULL)) { pgp_keyid = g_key_file_get_string(accounts, name, "pgp.keyid", NULL); @@ -304,7 +329,8 @@ accounts_get_account(const char *const name) server, port, resource, last_presence, login_presence, priority_online, priority_chat, priority_away, priority_xa, priority_dnd, muc_service, muc_nick, otr_policy, otr_manual, - otr_opportunistic, otr_always, pgp_keyid, startscript, theme, tls_policy); + otr_opportunistic, otr_always, omemo_policy, omemo_enabled, + omemo_disabled, pgp_keyid, startscript, theme, tls_policy); g_free(jid); g_free(password); @@ -316,6 +342,7 @@ accounts_get_account(const char *const name) g_free(muc_service); g_free(muc_nick); g_free(otr_policy); + g_free(omemo_policy); g_free(pgp_keyid); g_free(startscript); g_free(theme); @@ -385,6 +412,9 @@ accounts_rename(const char *const account_name, const char *const new_name) "otr.manual", "otr.opportunistic", "otr.always", + "omemo.policy", + "omemo.enabled", + "omemo.disabled", "pgp.keyid", "last.activity", "script.start", @@ -633,6 +663,32 @@ accounts_add_otr_policy(const char *const account_name, const char *const contac } } +void +accounts_add_omemo_state(const char *const account_name, const char *const contact_jid, gboolean enabled) +{ + if (accounts_account_exists(account_name)) { + if (enabled) { + conf_string_list_add(accounts, account_name, "omemo.enabled", contact_jid); + conf_string_list_remove(accounts, account_name, "omemo.disabled", contact_jid); + } else { + conf_string_list_add(accounts, account_name, "omemo.disabled", contact_jid); + conf_string_list_remove(accounts, account_name, "omemo.enabled", contact_jid); + } + + _save_accounts(); + } +} + +void +accounts_clear_omemo_state(const char *const account_name, const char *const contact_jid) +{ + if (accounts_account_exists(account_name)) { + conf_string_list_remove(accounts, account_name, "omemo.enabled", contact_jid); + conf_string_list_remove(accounts, account_name, "omemo.disabled", contact_jid); + _save_accounts(); + } +} + void accounts_set_muc_service(const char *const account_name, const char *const value) { @@ -660,6 +716,15 @@ accounts_set_otr_policy(const char *const account_name, const char *const value) } } +void +accounts_set_omemo_policy(const char *const account_name, const char *const value) +{ + if (accounts_account_exists(account_name)) { + g_key_file_set_string(accounts, account_name, "omemo.policy", value); + _save_accounts(); + } +} + void accounts_set_tls_policy(const char *const account_name, const char *const value) { diff --git a/src/config/accounts.h b/src/config/accounts.h index d41fb53e..c1e7a5ae 100644 --- a/src/config/accounts.h +++ b/src/config/accounts.h @@ -96,5 +96,7 @@ void accounts_clear_theme(const char *const account_name); void accounts_clear_muc(const char *const account_name); void accounts_clear_resource(const char *const account_name); void accounts_add_otr_policy(const char *const account_name, const char *const contact_jid, const char *const policy); +void accounts_add_omemo_state(const char *const account_name, const char *const contact_jid, gboolean enabled); +void accounts_clear_omemo_state(const char *const account_name, const char *const contact_jid); #endif diff --git a/src/config/preferences.c b/src/config/preferences.c index 65e7a64d..19d54304 100644 --- a/src/config/preferences.c +++ b/src/config/preferences.c @@ -1686,6 +1686,7 @@ _get_group(preference_t pref) case PREF_PLUGINS_SOURCEPATH: return PREF_GROUP_PLUGINS; case PREF_OMEMO_LOG: + case PREF_OMEMO_POLICY: return PREF_GROUP_OMEMO; default: return NULL; @@ -1903,6 +1904,8 @@ _get_key(preference_t pref) return "statusbar.room"; case PREF_OMEMO_LOG: return "log"; + case PREF_OMEMO_POLICY: + return "policy"; default: return NULL; } @@ -2023,6 +2026,8 @@ _get_default_string(preference_t pref) return "room"; case PREF_OMEMO_LOG: return "redact"; + case PREF_OMEMO_POLICY: + return "automatic"; default: return NULL; } diff --git a/src/config/preferences.h b/src/config/preferences.h index a4d82967..614c3f0f 100644 --- a/src/config/preferences.h +++ b/src/config/preferences.h @@ -149,6 +149,7 @@ typedef enum { PREF_STATUSBAR_CHAT, PREF_STATUSBAR_ROOM, PREF_OMEMO_LOG, + PREF_OMEMO_POLICY, } preference_t; typedef struct prof_alias_t { diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c index b85029ee..06174da5 100644 --- a/src/omemo/omemo.c +++ b/src/omemo/omemo.c @@ -14,6 +14,7 @@ #include "config/account.h" #include "config/files.h" +#include "config/preferences.h" #include "log.h" #include "omemo/crypto.h" #include "omemo/omemo.h" @@ -921,21 +922,6 @@ omemo_known_device_identities(const char *const jid) return g_hash_table_get_keys(known_identities); } -gboolean -omemo_is_trusted_jid(const char *const jid) -{ - GHashTable *trusted = g_hash_table_lookup(omemo_ctx.identity_key_store.trusted, jid); - if (!trusted) { - return FALSE; - } - - if (g_hash_table_size(trusted) > 0) { - return TRUE; - } - - return FALSE; -} - gboolean omemo_is_trusted_identity(const char *const jid, const char *const fingerprint) { @@ -1197,6 +1183,67 @@ omemo_fingerprint_autocomplete_reset(void) autocomplete_reset(omemo_ctx.fingerprint_ac); } +gboolean +omemo_automatic_start(const char *const recipient) +{ + gboolean result; + char *account_name = session_get_account_name(); + ProfAccount *account = accounts_get_account(account_name); + prof_omemopolicy_t policy; + + if (account->omemo_policy) { + // check default account setting + if (g_strcmp0(account->omemo_policy, "manual") == 0) { + policy = PROF_OMEMOPOLICY_MANUAL; + } + if (g_strcmp0(account->omemo_policy, "opportunistic") == 0) { + policy = PROF_OMEMOPOLICY_AUTOMATIC; + } + if (g_strcmp0(account->omemo_policy, "always") == 0) { + policy = PROF_OMEMOPOLICY_ALWAYS; + } + } else { + // check global setting + char *pref_omemo_policy = prefs_get_string(PREF_OMEMO_POLICY); + + // pref defaults to manual + policy = PROF_OMEMOPOLICY_AUTOMATIC; + + if (strcmp(pref_omemo_policy, "manual") == 0) { + policy = PROF_OMEMOPOLICY_MANUAL; + } else if (strcmp(pref_omemo_policy, "always") == 0) { + policy = PROF_OMEMOPOLICY_ALWAYS; + } + + prefs_free_string(pref_omemo_policy); + } + + switch (policy) { + case PROF_OMEMOPOLICY_MANUAL: + result = FALSE; + break; + case PROF_OMEMOPOLICY_AUTOMATIC: + if (g_list_find_custom(account->omemo_enabled, recipient, (GCompareFunc)g_strcmp0)) { + result = TRUE; + } else if (g_list_find_custom(account->omemo_disabled, recipient, (GCompareFunc)g_strcmp0)) { + result = FALSE; + } else { + return FALSE; + } + break; + case PROF_OMEMOPOLICY_ALWAYS: + if (g_list_find_custom(account->omemo_disabled, recipient, (GCompareFunc)g_strcmp0)) { + result = FALSE; + } else { + return TRUE; + } + break; + } + + account_free(account); + return result; +} + static gboolean _load_identity(void) { diff --git a/src/omemo/omemo.h b/src/omemo/omemo.h index 166a5292..8268491c 100644 --- a/src/omemo/omemo.h +++ b/src/omemo/omemo.h @@ -6,6 +6,12 @@ #define OMEMO_ERR_UNSUPPORTED_CRYPTO -10000 #define OMEMO_ERR_GCRYPT -20000 +typedef enum { + PROF_OMEMOPOLICY_MANUAL, + PROF_OMEMOPOLICY_AUTOMATIC, + PROF_OMEMOPOLICY_ALWAYS +} prof_omemopolicy_t; + typedef struct omemo_context_t omemo_context; typedef struct omemo_key { @@ -40,10 +46,10 @@ char *omemo_own_fingerprint(gboolean formatted); void omemo_trust(const char *const jid, const char *const fingerprint); void omemo_untrust(const char *const jid, const char *const fingerprint); GList *omemo_known_device_identities(const char *const jid); -gboolean omemo_is_trusted_jid(const char *const jid); gboolean omemo_is_trusted_identity(const char *const jid, const char *const fingerprint); char *omemo_fingerprint_autocomplete(const char *const search_str, gboolean previous); void omemo_fingerprint_autocomplete_reset(void); +gboolean omemo_automatic_start(const char *const recipient); void omemo_start_sessions(void); void omemo_start_session(const char *const barejid); diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c index 5064b194..8e17df8e 100644 --- a/src/ui/chatwin.c +++ b/src/ui/chatwin.c @@ -50,6 +50,9 @@ #ifdef HAVE_LIBOTR #include "otr/otr.h" #endif +#ifdef HAVE_OMEMO +#include "omemo/omemo.h" +#endif static void _chatwin_history(ProfChatWin *chatwin, const char *const contact); @@ -73,6 +76,13 @@ chatwin_new(const char *const barejid) } } +#ifdef HAVE_OMEMO + if (omemo_automatic_start(barejid)) { + omemo_start_session(barejid); + chatwin->is_omemo = TRUE; + } +#endif + return chatwin; } diff --git a/src/ui/console.c b/src/ui/console.c index 260658c8..812a9184 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -2004,6 +2004,10 @@ cons_show_omemo_prefs(void) cons_show("OMEMO preferences:"); cons_show(""); + char *policy_value = prefs_get_string(PREF_OMEMO_POLICY); + cons_show("OMEMO policy (/omemo policy) : %s", policy_value); + prefs_free_string(policy_value); + char *log_value = prefs_get_string(PREF_OMEMO_LOG); if (strcmp(log_value, "on") == 0) { cons_show("OMEMO logging (/omemo log) : ON"); diff --git a/src/ui/core.c b/src/ui/core.c index b87e2335..c758ba47 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -763,7 +763,7 @@ ui_room_join(const char *const roomjid, gboolean focus) { ProfMucWin *mucwin = wins_get_muc(roomjid); if (mucwin == NULL) { - mucwin = (ProfMucWin*)wins_new_muc(roomjid); + mucwin = mucwin_new(roomjid); } ProfWin *window = (ProfWin*)mucwin; diff --git a/src/ui/mucwin.c b/src/ui/mucwin.c index cdac610f..fc485b3d 100644 --- a/src/ui/mucwin.c +++ b/src/ui/mucwin.c @@ -44,6 +44,25 @@ #include "ui/window.h" #include "ui/win_types.h" #include "ui/window_list.h" +#ifdef HAVE_OMEMO +#include "omemo/omemo.h" +#endif + +ProfMucWin* +mucwin_new(const char *const barejid) +{ + ProfWin *window = wins_new_muc(barejid); + ProfMucWin *mucwin = (ProfMucWin *)window; + +#ifdef HAVE_OMEMO + if (omemo_automatic_start(barejid)) { + omemo_start_muc_sessions(barejid); + mucwin->is_omemo = TRUE; + } +#endif + + return mucwin; +} void mucwin_role_change(ProfMucWin *mucwin, const char *const role, const char *const actor, const char *const reason) diff --git a/src/ui/ui.h b/src/ui/ui.h index b94fe475..350a11bb 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -149,6 +149,7 @@ void chatwin_set_outgoing_char(ProfChatWin *chatwin, const char *const ch); void chatwin_unset_outgoing_char(ProfChatWin *chatwin); // MUC window +ProfMucWin* mucwin_new(const char *const barejid); void mucwin_role_change(ProfMucWin *mucwin, const char *const role, const char *const actor, const char *const reason); void mucwin_affiliation_change(ProfMucWin *mucwin, const char *const affiliation, const char *const actor, const char *const reason); From 7df7e05639bd72c0db8e3f18390ce4e9eb616bff Mon Sep 17 00:00:00 2001 From: Paul Fariello Date: Tue, 16 Apr 2019 20:12:05 +0320 Subject: [PATCH 2/5] Fix unit test after account_new API change --- tests/unittests/config/stub_accounts.c | 1 + tests/unittests/omemo/stub_omemo.c | 2 +- tests/unittests/test_cmd_account.c | 14 +++++++------- tests/unittests/test_cmd_connect.c | 8 ++++---- tests/unittests/test_cmd_join.c | 8 ++++---- tests/unittests/test_cmd_rooms.c | 4 ++-- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/tests/unittests/config/stub_accounts.c b/tests/unittests/config/stub_accounts.c index e1a33b0c..b164497e 100644 --- a/tests/unittests/config/stub_accounts.c +++ b/tests/unittests/config/stub_accounts.c @@ -203,3 +203,4 @@ char* accounts_get_last_activity(const char *const account_name) { return NULL; } +void accounts_add_omemo_state(const char *const account_name, const char *const contact_jid, gboolean enabled) {} diff --git a/tests/unittests/omemo/stub_omemo.c b/tests/unittests/omemo/stub_omemo.c index baf2a05b..4b3548a4 100644 --- a/tests/unittests/omemo/stub_omemo.c +++ b/tests/unittests/omemo/stub_omemo.c @@ -22,7 +22,7 @@ omemo_format_fingerprint(const char *const fingerprint) void omemo_generate_crypto_materials(ProfAccount *account) {} gboolean -omemo_is_trusted_jid(const char *const jid) +omemo_automatic_start(const char *const jid) { return TRUE; } diff --git a/tests/unittests/test_cmd_account.c b/tests/unittests/test_cmd_account.c index a1fff9b9..056a73a1 100644 --- a/tests/unittests/test_cmd_account.c +++ b/tests/unittests/test_cmd_account.c @@ -33,7 +33,7 @@ void cmd_account_shows_usage_when_not_connected_and_no_args(void **state) void cmd_account_shows_account_when_connected_and_no_args(void **state) { ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL, NULL, - TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); gchar *args[] = { NULL }; will_return(connection_get_status, JABBER_CONNECTED); @@ -93,7 +93,7 @@ void cmd_account_show_shows_account_when_exists(void **state) { gchar *args[] = { "show", "account_name", NULL }; ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL, NULL, - TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); expect_any(accounts_get_account, name); will_return(accounts_get_account, account); @@ -409,7 +409,7 @@ void cmd_account_set_password_sets_password(void **state) { gchar *args[] = { "set", "a_account", "password", "a_password", NULL }; ProfAccount *account = account_new("a_account", NULL, NULL, NULL, - TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); expect_any(accounts_account_exists, account_name); @@ -432,7 +432,7 @@ void cmd_account_set_eval_password_sets_eval_password(void **state) { gchar *args[] = { "set", "a_account", "eval_password", "a_password", NULL }; ProfAccount *account = account_new("a_account", NULL, NULL, NULL, - TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); expect_any(accounts_account_exists, account_name); will_return(accounts_account_exists, TRUE); @@ -453,7 +453,7 @@ void cmd_account_set_eval_password_sets_eval_password(void **state) void cmd_account_set_password_when_eval_password_set(void **state) { gchar *args[] = { "set", "a_account", "password", "a_password", NULL }; ProfAccount *account = account_new("a_account", NULL, NULL, "a_password", - TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); expect_any(accounts_account_exists, account_name); will_return(accounts_account_exists, TRUE); @@ -470,7 +470,7 @@ void cmd_account_set_password_when_eval_password_set(void **state) { void cmd_account_set_eval_password_when_password_set(void **state) { gchar *args[] = { "set", "a_account", "eval_password", "a_password", NULL }; ProfAccount *account = account_new("a_account", NULL, "a_password", NULL, - TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); expect_any(accounts_account_exists, account_name); will_return(accounts_account_exists, TRUE); @@ -800,7 +800,7 @@ void cmd_account_set_priority_updates_presence_when_account_connected_with_prese #ifdef HAVE_LIBGPGME ProfAccount *account = account_new("a_account", "a_jid", NULL, NULL, TRUE, NULL, 5222, "a_resource", - NULL, NULL, 10, 10, 10, 10, 10, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + NULL, NULL, 10, 10, 10, 10, 10, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); will_return(session_get_account_name, "a_account"); expect_any(accounts_get_account, name); diff --git a/tests/unittests/test_cmd_connect.c b/tests/unittests/test_cmd_connect.c index c7331483..63b7bf7d 100644 --- a/tests/unittests/test_cmd_connect.c +++ b/tests/unittests/test_cmd_connect.c @@ -116,7 +116,7 @@ void cmd_connect_lowercases_argument_with_account(void **state) { gchar *args[] = { "Jabber_org", NULL }; ProfAccount *account = account_new("Jabber_org", "me@jabber.org", "password", NULL, - TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); will_return(connection_get_status, JABBER_DISCONNECTED); @@ -136,7 +136,7 @@ void cmd_connect_asks_password_when_not_in_account(void **state) { gchar *args[] = { "jabber_org", NULL }; ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL, NULL, - TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); will_return(connection_get_status, JABBER_DISCONNECTED); @@ -383,7 +383,7 @@ void cmd_connect_shows_message_when_connecting_with_account(void **state) { gchar *args[] = { "jabber_org", NULL }; ProfAccount *account = account_new("jabber_org", "user@jabber.org", "password", NULL, - TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); will_return(connection_get_status, JABBER_DISCONNECTED); @@ -403,7 +403,7 @@ void cmd_connect_connects_with_account(void **state) { gchar *args[] = { "jabber_org", NULL }; ProfAccount *account = account_new("jabber_org", "me@jabber.org", "password", NULL, - TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); will_return(connection_get_status, JABBER_DISCONNECTED); diff --git a/tests/unittests/test_cmd_join.c b/tests/unittests/test_cmd_join.c index dc24a009..19b6da94 100644 --- a/tests/unittests/test_cmd_join.c +++ b/tests/unittests/test_cmd_join.c @@ -65,7 +65,7 @@ void cmd_join_uses_account_mucservice_when_no_service_specified(void **state) char *expected_room = "room@conference.server.org"; gchar *args[] = { room, "nick", nick, NULL }; ProfAccount *account = account_new(account_name, "user@server.org", NULL, NULL, - TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, account_service, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, account_service, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); muc_init(); @@ -90,7 +90,7 @@ void cmd_join_uses_supplied_nick(void **state) char *nick = "bob"; gchar *args[] = { room, "nick", nick, NULL }; ProfAccount *account = account_new(account_name, "user@server.org", NULL, NULL, - TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); muc_init(); @@ -115,7 +115,7 @@ void cmd_join_uses_account_nick_when_not_supplied(void **state) char *account_nick = "a_nick"; gchar *args[] = { room, NULL }; ProfAccount *account = account_new(account_name, "user@server.org", NULL, NULL, - TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, NULL, account_nick, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, NULL, account_nick, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); muc_init(); @@ -143,7 +143,7 @@ void cmd_join_uses_password_when_supplied(void **state) char *expected_room = "room@a_service"; gchar *args[] = { room, "password", password, NULL }; ProfAccount *account = account_new(account_name, "user@server.org", NULL, NULL, - TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, account_service, account_nick, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, account_service, account_nick, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); muc_init(); diff --git a/tests/unittests/test_cmd_rooms.c b/tests/unittests/test_cmd_rooms.c index b04e5618..102941b9 100644 --- a/tests/unittests/test_cmd_rooms.c +++ b/tests/unittests/test_cmd_rooms.c @@ -46,7 +46,7 @@ void cmd_rooms_uses_account_default_when_no_arg(void **state) gchar *args[] = { NULL }; ProfAccount *account = account_new("testaccount", NULL, NULL, NULL, TRUE, NULL, 0, NULL, NULL, NULL, - 0, 0, 0, 0, 0, strdup("default_conf_server"), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + 0, 0, 0, 0, 0, strdup("default_conf_server"), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); will_return(connection_get_status, JABBER_CONNECTED); will_return(session_get_account_name, "account_name"); @@ -84,7 +84,7 @@ void cmd_rooms_filter_arg_used_when_passed(void **state) gchar *args[] = { "filter", "text", NULL }; ProfAccount *account = account_new("testaccount", NULL, NULL, NULL, TRUE, NULL, 0, NULL, NULL, NULL, - 0, 0, 0, 0, 0, strdup("default_conf_server"), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + 0, 0, 0, 0, 0, strdup("default_conf_server"), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); will_return(connection_get_status, JABBER_CONNECTED); will_return(session_get_account_name, "account_name"); From 3ef9123ccbefdc6376110122506a8227dd530e3b Mon Sep 17 00:00:00 2001 From: Paul Fariello Date: Wed, 17 Apr 2019 08:58:07 +0200 Subject: [PATCH 3/5] Fix OTR unit test after account_new API change --- tests/unittests/test_cmd_otr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittests/test_cmd_otr.c b/tests/unittests/test_cmd_otr.c index 314a64b2..ffb6f785 100644 --- a/tests/unittests/test_cmd_otr.c +++ b/tests/unittests/test_cmd_otr.c @@ -179,7 +179,7 @@ void cmd_otr_gen_generates_key_for_connected_account(void **state) gchar *args[] = { "gen", NULL }; char *account_name = "myaccount"; ProfAccount *account = account_new(account_name, "me@jabber.org", NULL, NULL, - TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); will_return(connection_get_status, JABBER_CONNECTED); will_return(session_get_account_name, account_name); From a6eb0d2c43bb3916e6a77f54a8ba070962bcf0a1 Mon Sep 17 00:00:00 2001 From: Paul Fariello Date: Wed, 17 Apr 2019 11:47:54 +0200 Subject: [PATCH 4/5] Handle missing real jid in OMEMO encrypted MUC This should never happens since we should ensure MUC is Non-Anonymous before enabling OMEMO. But we should neither segfault if this happens. --- src/omemo/omemo.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c index 06174da5..e74abe9d 100644 --- a/src/omemo/omemo.c +++ b/src/omemo/omemo.c @@ -364,9 +364,13 @@ omemo_start_muc_sessions(const char *const roomjid) GList *iter; for (iter = roster; iter != NULL; iter = iter->next) { Occupant *occupant = (Occupant *)iter->data; - Jid *jid = jid_create(occupant->jid); - omemo_start_session(jid->barejid); - jid_destroy(jid); + if (occupant->jid != NULL) { + Jid *jid = jid_create(occupant->jid); + omemo_start_session(jid->barejid); + jid_destroy(jid); + } else { + log_error("OMEMO: cannot get real jid for %s in %s", occupant->nick, roomjid); + } } g_list_free(roster); } From 21ae946896d5c1489f3b8d4341dc90bf33ace984 Mon Sep 17 00:00:00 2001 From: Paul Fariello Date: Wed, 17 Apr 2019 12:41:19 +0200 Subject: [PATCH 5/5] Handle MUC anonymous state when auto starting OMEMO When auto joining a MUC we don't have access to required information so we just don't start OMEMO at this time. Once we receive disco info we then try to start OMEMO. --- src/ui/mucwin.c | 2 +- src/xmpp/iq.c | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/ui/mucwin.c b/src/ui/mucwin.c index fc485b3d..b56cd994 100644 --- a/src/ui/mucwin.c +++ b/src/ui/mucwin.c @@ -55,7 +55,7 @@ mucwin_new(const char *const barejid) ProfMucWin *mucwin = (ProfMucWin *)window; #ifdef HAVE_OMEMO - if (omemo_automatic_start(barejid)) { + if (muc_anonymity_type(mucwin->roomjid) == MUC_ANONYMITY_TYPE_NONANONYMOUS && omemo_automatic_start(barejid)) { omemo_start_muc_sessions(barejid); mucwin->is_omemo = TRUE; } diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index d6e4c153..7800ef3c 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -72,6 +72,10 @@ #include "xmpp/roster.h" #include "xmpp/muc.h" +#ifdef HAVE_OMEMO +#include "omemo/omemo.h" +#endif + typedef struct p_room_info_data_t { char *room; gboolean display; @@ -2100,8 +2104,16 @@ _room_info_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata muc_set_features(cb_data->room, features); ProfMucWin *mucwin = wins_get_muc(cb_data->room); - if (mucwin && cb_data->display) { - mucwin_room_disco_info(mucwin, identities, features); + if (mucwin) { +#ifdef HAVE_OMEMO + if (muc_anonymity_type(mucwin->roomjid) == MUC_ANONYMITY_TYPE_NONANONYMOUS && omemo_automatic_start(cb_data->room)) { + omemo_start_muc_sessions(cb_data->room); + mucwin->is_omemo = TRUE; + } +#endif + if (cb_data->display) { + mucwin_room_disco_info(mucwin, identities, features); + } } g_slist_free_full(features, free);