From 774dbf93deccdf620bd28a8c241bc85c8e87361e Mon Sep 17 00:00:00 2001 From: Kevin Kofler Date: Sun, 13 May 2018 10:58:41 +0200 Subject: [PATCH] KWalletPasswords, GnomeKeyringPasswords: Implement migration from QupZilla Implement migration of passwords stored in KWallet or GNOME Keyring from QupZilla to Falkon. The logic is the same as for the profiles: If no "Falkon" folder exists, but a "QupZilla" folder exists, we read all the data from the "QupZilla" folder and write it to the "Falkon" folder. (In GNOME Keyring, those are not actually folders, but application attributes on the entries, but the logic is the same.) In GnomeKeyringPasswordBackend, a storeEntry helper method is introduced to avoid duplicating code. Reviewed By: drosca Differential Revision: https://phabricator.kde.org/D12846 --- .../gnomekeyringpasswordbackend.cpp | 69 +++++++++++++------ .../kwalletpasswordbackend.cpp | 26 ++++++- 2 files changed, 71 insertions(+), 24 deletions(-) diff --git a/src/plugins/GnomeKeyringPasswords/gnomekeyringpasswordbackend.cpp b/src/plugins/GnomeKeyringPasswords/gnomekeyringpasswordbackend.cpp index 9b877f238..e027b319a 100644 --- a/src/plugins/GnomeKeyringPasswords/gnomekeyringpasswordbackend.cpp +++ b/src/plugins/GnomeKeyringPasswords/gnomekeyringpasswordbackend.cpp @@ -73,6 +73,31 @@ static GnomeKeyringAttributeList* createAttributes(const PasswordEntry &entry) return attributes; } +static void storeEntry(PasswordEntry &entry) +{ + guint32 itemId; + GnomeKeyringAttributeList* attributes = createAttributes(entry); + + QByteArray pass = entry.password.toUtf8(); + QByteArray host = entry.host.toUtf8(); + + GnomeKeyringResult result = gnome_keyring_item_create_sync(GNOME_KEYRING_DEFAULT, + GNOME_KEYRING_ITEM_GENERIC_SECRET, + host.constData(), + attributes, + pass.constData(), + TRUE, // Update if exists + &itemId); + + gnome_keyring_attribute_list_free(attributes); + + if (result != GNOME_KEYRING_RESULT_OK) { + qWarning() << "GnomeKeyringPasswordBackend::addEntry Cannot add entry to keyring!"; + } + + entry.id = itemId; +} + GnomeKeyringPasswordBackend::GnomeKeyringPasswordBackend() : PasswordBackend() , m_loaded(false) @@ -118,27 +143,7 @@ void GnomeKeyringPasswordBackend::addEntry(const PasswordEntry &entry) PasswordEntry stored = entry; stored.updated = QDateTime::currentDateTime().toTime_t(); - guint32 itemId; - GnomeKeyringAttributeList* attributes = createAttributes(stored); - - QByteArray pass = stored.password.toUtf8(); - QByteArray host = stored.host.toUtf8(); - - GnomeKeyringResult result = gnome_keyring_item_create_sync(GNOME_KEYRING_DEFAULT, - GNOME_KEYRING_ITEM_GENERIC_SECRET, - host.constData(), - attributes, - pass.constData(), - TRUE, // Update if exists - &itemId); - - gnome_keyring_attribute_list_free(attributes); - - if (result != GNOME_KEYRING_RESULT_OK) { - qWarning() << "GnomeKeyringPasswordBackend::addEntry Cannot add entry to keyring!"; - } - - stored.id = itemId; + storeEntry(stored); m_allEntries.append(stored); } @@ -263,6 +268,22 @@ void GnomeKeyringPasswordBackend::initialize() return; } + bool migrate = false; + if (result == GNOME_KEYRING_RESULT_NO_MATCH) { + result = gnome_keyring_find_itemsv_sync(GNOME_KEYRING_ITEM_GENERIC_SECRET, &found, + "application", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING, "QupZilla", + NULL); + + if (result != GNOME_KEYRING_RESULT_OK && result != GNOME_KEYRING_RESULT_NO_MATCH) { + qWarning() << "GnomeKeyringPasswordBackend::initialize Cannot read items from keyring!"; + return; + } + + if (result == GNOME_KEYRING_RESULT_OK) { + migrate = true; + } + } + GList* tmp = found; while (tmp) { @@ -273,5 +294,11 @@ void GnomeKeyringPasswordBackend::initialize() gnome_keyring_found_list_free(found); + if (migrate) { + for (PasswordEntry &entry : m_allEntries) { + storeEntry(entry); + } + } + m_loaded = true; } diff --git a/src/plugins/KWalletPasswords/kwalletpasswordbackend.cpp b/src/plugins/KWalletPasswords/kwalletpasswordbackend.cpp index fe4caeb04..e3d898690 100644 --- a/src/plugins/KWalletPasswords/kwalletpasswordbackend.cpp +++ b/src/plugins/KWalletPasswords/kwalletpasswordbackend.cpp @@ -164,14 +164,23 @@ void KWalletPasswordBackend::initialize() return; } + bool migrate = !m_wallet->hasFolder("Falkon") && m_wallet->hasFolder("QupZilla"); + if (!m_wallet->hasFolder("Falkon") && !m_wallet->createFolder("Falkon")) { qWarning() << "KWalletPasswordBackend::initialize Cannot create folder \"Falkon\"!"; return; } - if (!m_wallet->setFolder("Falkon")) { - qWarning() << "KWalletPasswordBackend::initialize Cannot set folder \"Falkon\"!"; - return; + if (migrate) { + if (!m_wallet->setFolder("QupZilla")) { + qWarning() << "KWalletPasswordBackend::initialize Cannot set folder \"QupZilla\"!"; + return; + } + } else { + if (!m_wallet->setFolder("Falkon")) { + qWarning() << "KWalletPasswordBackend::initialize Cannot set folder \"Falkon\"!"; + return; + } } QMap entries; @@ -188,6 +197,17 @@ void KWalletPasswordBackend::initialize() } ++i; } + + if (migrate) { + if (!m_wallet->setFolder("Falkon")) { + qWarning() << "KWalletPasswordBackend::initialize Cannot set folder \"Falkon\"!"; + return; + } + + foreach (const PasswordEntry &entry, m_allEntries) { + m_wallet->writeEntry(entry.id.toString(), encodeEntry(entry)); + } + } } KWalletPasswordBackend::~KWalletPasswordBackend()