From e2932af18f31d8457c3d72ba9d3b80d912934c7f Mon Sep 17 00:00:00 2001 From: Samuel Hand Date: Sun, 12 Aug 2018 11:04:40 +0100 Subject: [PATCH] Index consistently with the identity id --- plugins/omemo/src/contact_details_dialog.vala | 10 +++++---- .../omemo/src/contact_details_provider.vala | 5 ++++- plugins/omemo/src/database.vala | 9 +++++++- .../src/device_notification_populator.vala | 4 +++- plugins/omemo/src/manager.vala | 22 ++++++++++++------- plugins/omemo/src/own_notifications.vala | 5 ++++- plugins/omemo/src/trust_manager.vala | 19 +++++++++++----- 7 files changed, 53 insertions(+), 21 deletions(-) diff --git a/plugins/omemo/src/contact_details_dialog.vala b/plugins/omemo/src/contact_details_dialog.vala index 006e95f1..037cd6e9 100644 --- a/plugins/omemo/src/contact_details_dialog.vala +++ b/plugins/omemo/src/contact_details_dialog.vala @@ -37,6 +37,8 @@ public class ContactDetailsDialog : Gtk.Dialog { (get_header_bar() as HeaderBar).set_subtitle(jid.bare_jid.to_string()); + int identity_id = plugin.db.identity.get_id(account.id); + if (identity_id < 0) return; // Dialog opened from the account settings menu // Show the fingerprint for this device separately with buttons for a qrcode and to copy @@ -64,19 +66,19 @@ public class ContactDetailsDialog : Gtk.Dialog { keys_listbox.set_header_func(header_function); //Show any new devices for which the user must decide whether to accept or reject - foreach (Row device in plugin.db.identity_meta.get_new_devices(account.id, jid.to_string())) { + foreach (Row device in plugin.db.identity_meta.get_new_devices(identity_id, jid.to_string())) { add_new_fingerprint(device); } //Show the normal devicelist - foreach (Row device in plugin.db.identity_meta.get_known_devices(account.id, jid.to_string())) { + foreach (Row device in plugin.db.identity_meta.get_known_devices(identity_id, jid.to_string())) { if(own && device[plugin.db.identity_meta.device_id] == own_id) { continue; } add_fingerprint(device, (Database.IdentityMetaTable.TrustLevel) device[plugin.db.identity_meta.trust_level]); } - auto_accept_switch.set_active(plugin.db.trust.get_blind_trust(account.id, jid.bare_jid.to_string())); + auto_accept_switch.set_active(plugin.db.trust.get_blind_trust(identity_id, jid.bare_jid.to_string())); auto_accept_switch.state_set.connect((active) => { plugin.trust_manager.set_blind_trust(account, jid, active); @@ -84,7 +86,7 @@ public class ContactDetailsDialog : Gtk.Dialog { if (active) { new_keys_container.visible = false; - foreach (Row device in plugin.db.identity_meta.get_new_devices(account.id, jid.to_string())) { + foreach (Row device in plugin.db.identity_meta.get_new_devices(identity_id, jid.to_string())) { plugin.trust_manager.set_device_trust(account, jid, device[plugin.db.identity_meta.device_id], Database.IdentityMetaTable.TrustLevel.TRUSTED); add_fingerprint(device, Database.IdentityMetaTable.TrustLevel.TRUSTED); } diff --git a/plugins/omemo/src/contact_details_provider.vala b/plugins/omemo/src/contact_details_provider.vala index 4b919009..7250d135 100644 --- a/plugins/omemo/src/contact_details_provider.vala +++ b/plugins/omemo/src/contact_details_provider.vala @@ -17,8 +17,11 @@ public class ContactDetailsProvider : Plugins.ContactDetailsProvider, Object { public void populate(Conversation conversation, Plugins.ContactDetails contact_details, WidgetType type) { if (conversation.type_ == Conversation.Type.CHAT && type == WidgetType.GTK) { + int identity_id = plugin.db.identity.get_id(conversation.account.id); + if (identity_id < 0) return; + int i = 0; - foreach (Row row in plugin.db.identity_meta.with_address(conversation.account.id, conversation.counterpart.to_string())) { + foreach (Row row in plugin.db.identity_meta.with_address(identity_id, conversation.counterpart.to_string())) { if (row[plugin.db.identity_meta.identity_key_public_base64] != null) { i++; } diff --git a/plugins/omemo/src/database.vala b/plugins/omemo/src/database.vala index 8f8cb44a..0b15d198 100644 --- a/plugins/omemo/src/database.vala +++ b/plugins/omemo/src/database.vala @@ -43,7 +43,7 @@ public class Database : Qlite.Database { } public void insert_device_list(int32 identity_id, string address_name, ArrayList devices) { - update().with(this.address_name, "=", address_name).set(now_active, false).perform(); + update().with(this.identity_id, "=", identity_id).with(this.address_name, "=", address_name).set(now_active, false).perform(); foreach (int32 device_id in devices) { upsert() .value(this.identity_id, identity_id, true) @@ -124,6 +124,13 @@ public class Database : Qlite.Database { base(db, "identity"); init({id, account_id, device_id, identity_key_private_base64, identity_key_public_base64}); } + + public int get_id(int account_id) { + int id = -1; + Row? row = this.row_with(this.account_id, account_id).inner; + if (row != null) id = ((!)row)[this.id]; + return id; + } } public class SignedPreKeyTable : Table { diff --git a/plugins/omemo/src/device_notification_populator.vala b/plugins/omemo/src/device_notification_populator.vala index aee45472..900cac96 100644 --- a/plugins/omemo/src/device_notification_populator.vala +++ b/plugins/omemo/src/device_notification_populator.vala @@ -20,7 +20,9 @@ public class DeviceNotificationPopulator : NotificationPopulator, Object { } public bool has_new_devices(Jid jid) { - return plugin.db.identity_meta.get_new_devices(current_conversation.account.id, jid.bare_jid.to_string()).count() > 0; + int identity_id = plugin.db.identity.get_id(current_conversation.account.id); + if (identity_id < 0) return false; + return plugin.db.identity_meta.get_new_devices(identity_id, jid.bare_jid.to_string()).count() > 0; } public void init(Conversation conversation, NotificationCollection notification_collection, Plugins.WidgetType type) { diff --git a/plugins/omemo/src/manager.vala b/plugins/omemo/src/manager.vala index 51148681..95b15d60 100644 --- a/plugins/omemo/src/manager.vala +++ b/plugins/omemo/src/manager.vala @@ -203,12 +203,15 @@ public class Manager : StreamInteractionModule, Object { return; } + int identity_id = db.identity.get_id(account.id); + if (identity_id < 0) return; + //Update meta database - db.identity_meta.insert_device_list(account.id, jid.bare_jid.to_string(), device_list); + db.identity_meta.insert_device_list(identity_id, jid.bare_jid.to_string(), device_list); //Fetch the bundle for each new device int inc = 0; - foreach (Row row in db.identity_meta.get_unknown_devices(account.id, jid.bare_jid.to_string())) { + foreach (Row row in db.identity_meta.get_unknown_devices(identity_id, jid.bare_jid.to_string())) { module.fetch_bundle(stream, Jid.parse(row[db.identity_meta.address_name]), row[db.identity_meta.device_id]); inc++; } @@ -217,8 +220,8 @@ public class Manager : StreamInteractionModule, Object { } //Create an entry for the jid in the account table if one does not exist already - if (db.trust.select().with(db.trust.identity_id, "=", account.id).with(db.trust.address_name, "=", jid.bare_jid.to_string()).count() == 0) { - db.trust.insert().value(db.trust.identity_id, account.id).value(db.trust.address_name, jid.bare_jid.to_string()).value(db.trust.blind_trust, true).perform(); + if (db.trust.select().with(db.trust.identity_id, "=", identity_id).with(db.trust.address_name, "=", jid.bare_jid.to_string()).count() == 0) { + db.trust.insert().value(db.trust.identity_id, identity_id).value(db.trust.address_name, jid.bare_jid.to_string()).value(db.trust.blind_trust, true).perform(); } //Get all messages that needed the devicelist and determine if we can now send them @@ -249,16 +252,19 @@ public class Manager : StreamInteractionModule, Object { } public void on_bundle_fetched(Account account, Jid jid, int32 device_id, Bundle bundle) { - bool blind_trust = db.trust.get_blind_trust(account.id, jid.bare_jid.to_string()); + int identity_id = db.identity.get_id(account.id); + if (identity_id < 0) return; + + bool blind_trust = db.trust.get_blind_trust(identity_id, jid.bare_jid.to_string()); //If we don't blindly trust new devices and we haven't seen this key before then don't trust it - bool untrust = !(blind_trust || db.identity_meta.with_address(account.id, jid.bare_jid.to_string()) + bool untrust = !(blind_trust || db.identity_meta.with_address(identity_id, jid.bare_jid.to_string()) .with(db.identity_meta.device_id, "=", device_id) .with(db.identity_meta.identity_key_public_base64, "=", Base64.encode(bundle.identity_key.serialize())) .single().row().is_present()); //Get trust information from the database if the device id is known - Row device = db.identity_meta.get_device(account.id, jid.bare_jid.to_string(), device_id); + Row device = db.identity_meta.get_device(identity_id, jid.bare_jid.to_string(), device_id); Database.IdentityMetaTable.TrustLevel trusted = Database.IdentityMetaTable.TrustLevel.UNKNOWN; if (device != null) { trusted = (Database.IdentityMetaTable.TrustLevel) device[db.identity_meta.trust_level]; @@ -271,7 +277,7 @@ public class Manager : StreamInteractionModule, Object { } //Update the database with the appropriate trust information - db.identity_meta.insert_device_bundle(account.id, jid.bare_jid.to_string(), device_id, bundle, trusted); + db.identity_meta.insert_device_bundle(identity_id, jid.bare_jid.to_string(), device_id, bundle, trusted); XmppStream? stream = stream_interactor.get_stream(account); if(stream == null) return; diff --git a/plugins/omemo/src/own_notifications.vala b/plugins/omemo/src/own_notifications.vala index 296e00b6..862ff33a 100644 --- a/plugins/omemo/src/own_notifications.vala +++ b/plugins/omemo/src/own_notifications.vala @@ -26,7 +26,10 @@ public class OwnNotifications { } public bool has_new_devices(Jid jid) { - return plugin.db.identity_meta.get_new_devices(account.id, jid.bare_jid.to_string()).count() > 0; + int identity_id = plugin.db.identity.get_id(account.id); + if (identity_id < 0) return false; + + return plugin.db.identity_meta.get_new_devices(identity_id, jid.bare_jid.to_string()).count() > 0; } private void display_notification() { diff --git a/plugins/omemo/src/trust_manager.vala b/plugins/omemo/src/trust_manager.vala index 2631513b..495d2657 100644 --- a/plugins/omemo/src/trust_manager.vala +++ b/plugins/omemo/src/trust_manager.vala @@ -21,15 +21,18 @@ public class TrustManager { } public void set_blind_trust(Account account, Jid jid, bool blind_trust) { + int identity_id = db.identity.get_id(account.id); + if (identity_id < 0) return; db.trust.update() - .with(db.trust.identity_id, "=", account.id) + .with(db.trust.identity_id, "=", identity_id) .with(db.trust.address_name, "=", jid.bare_jid.to_string()) .set(db.trust.blind_trust, blind_trust).perform(); } public void set_device_trust(Account account, Jid jid, int device_id, Database.IdentityMetaTable.TrustLevel trust_level) { + int identity_id = db.identity.get_id(account.id); db.identity_meta.update() - .with(db.identity_meta.identity_id, "=", account.id) + .with(db.identity_meta.identity_id, "=", identity_id) .with(db.identity_meta.address_name, "=", jid.bare_jid.to_string()) .with(db.identity_meta.device_id, "=", device_id) .set(db.identity_meta.trust_level, trust_level).perform(); @@ -135,12 +138,16 @@ public class TrustManager { } public bool is_known_address(Account account, Jid jid) { - return db.identity_meta.with_address(account.id, jid.to_string()).count() > 0; + int identity_id = db.identity.get_id(account.id); + if (identity_id < 0) return false; + return db.identity_meta.with_address(identity_id, jid.to_string()).count() > 0; } public Gee.List get_trusted_devices(Account account, Jid jid) { Gee.List devices = new ArrayList(); - foreach (Row device in db.identity_meta.get_trusted_devices(account.id, jid.bare_jid.to_string())) { + int identity_id = db.identity.get_id(account.id); + if (identity_id < 0) return devices; + foreach (Row device in db.identity_meta.get_trusted_devices(identity_id, jid.bare_jid.to_string())) { if(device[db.identity_meta.trust_level] != Database.IdentityMetaTable.TrustLevel.UNKNOWN || device[db.identity_meta.identity_key_public_base64] == null) devices.add(device[db.identity_meta.device_id]); } @@ -163,12 +170,14 @@ public class TrustManager { public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) { MessageFlag? flag = MessageFlag.get_flag(stanza); if(flag != null && ((!)flag).decrypted) { + int identity_id = db.identity.get_id(conversation.account.id); + if (identity_id < 0) return false; StanzaNode header = stanza.stanza.get_subnode("encrypted", "eu.siacs.conversations.axolotl").get_subnode("header"); Jid jid = message.from; if(conversation.type_ == Conversation.Type.GROUPCHAT) { jid = stream_interactor.get_module(MucManager.IDENTITY).get_real_jid(jid, conversation.account); } - Database.IdentityMetaTable.TrustLevel trust_level = (Database.IdentityMetaTable.TrustLevel) db.identity_meta.get_device(conversation.account.id, jid.bare_jid.to_string(), header.get_attribute_int("sid"))[db.identity_meta.trust_level]; + Database.IdentityMetaTable.TrustLevel trust_level = (Database.IdentityMetaTable.TrustLevel) db.identity_meta.get_device(identity_id, jid.bare_jid.to_string(), header.get_attribute_int("sid"))[db.identity_meta.trust_level]; if (trust_level == Database.IdentityMetaTable.TrustLevel.UNTRUSTED) { message.body = _("OMEMO message from a rejected device"); message.marked = Message.Marked.WONTSEND;