kutils: replace KConfig with KSettings in KPasswdStoreImpl

KConfig uses internal lock for synchronization, KSettings does not and
since the password stores are giant-locked now it is faster to not lock in
the underlaying implementation

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2023-06-14 16:40:32 +03:00
parent 6a18754f1b
commit c38afd7556
2 changed files with 19 additions and 16 deletions

View file

@ -18,7 +18,7 @@
#include "kpasswdstoreimpl.h" #include "kpasswdstoreimpl.h"
#include "kstandarddirs.h" #include "kstandarddirs.h"
#include "kconfiggroup.h" #include "ksettings.h"
#include "kpassworddialog.h" #include "kpassworddialog.h"
#include "knewpassworddialog.h" #include "knewpassworddialog.h"
#include "kmessagebox.h" #include "kmessagebox.h"
@ -78,10 +78,9 @@ KPasswdStoreImpl::KPasswdStoreImpl(const QString &id)
m_opensslblocklen = EVP_CIPHER_block_size(EVP_aes_256_cbc()); m_opensslblocklen = EVP_CIPHER_block_size(EVP_aes_256_cbc());
#endif #endif
KConfig kconfig("kpasswdstorerc", KConfig::SimpleConfig); KSettings ksettings("kpasswdstorerc", KSettings::SimpleConfig);
KConfigGroup kconfiggroup = kconfig.group("KPasswdStore"); m_retries = ksettings.value("KPasswdStore/Retries", kpasswdstore_passretries).toUInt();
m_retries = kconfiggroup.readEntry("Retries", kpasswdstore_passretries); m_timeout = (ksettings.value("KPasswdStore/Timeout", kpasswdstore_passtimeout).toUInt() * 60000);
m_timeout = (kconfiggroup.readEntry("Timeout", kpasswdstore_passtimeout) * 60000);
} }
KPasswdStoreImpl::~KPasswdStoreImpl() KPasswdStoreImpl::~KPasswdStoreImpl()
@ -143,8 +142,11 @@ QString KPasswdStoreImpl::getPasswd(const QByteArray &key, const qlonglong windo
} }
bool ok = false; bool ok = false;
KConfig kconfig(m_passwdstore); KSettings ksettings(m_passwdstore, KSettings::SimpleConfig);
const QString passwd = kconfig.group(m_storeid).readEntry(key.constData(), QString()); QString storekey = m_storeid;
storekey.append(QLatin1Char('/'));
storekey.append(QString::fromLatin1(key.constData(), key.size()));
const QString passwd = ksettings.value(storekey).toString();
if (passwd.isEmpty()) { if (passwd.isEmpty()) {
return QString(); return QString();
} }
@ -167,10 +169,11 @@ bool KPasswdStoreImpl::storePasswd(const QByteArray &key, const QString &passwd,
} }
bool ok = false; bool ok = false;
KConfig kconfig(m_passwdstore); KSettings ksettings(m_passwdstore, KSettings::SimpleConfig);
KConfigGroup kconfiggroup = kconfig.group(m_storeid); QString storekey = m_storeid;
kconfiggroup.writeEntry(key.constData(), encryptPasswd(passwd, &ok)); storekey.append(QLatin1Char('/'));
kconfiggroup.sync(); storekey.append(QString::fromLatin1(key.constData(), key.size()));
ksettings.setValue(storekey, encryptPasswd(passwd, &ok));
return ok; return ok;
} }
@ -189,9 +192,10 @@ bool KPasswdStoreImpl::ensurePasswd(const qlonglong windowid, const bool showerr
// the only reason to encrypt and decrypt passwords is to obscure them // the only reason to encrypt and decrypt passwords is to obscure them
// for the naked eye, if one can overwrite, delete or otherwise alter // for the naked eye, if one can overwrite, delete or otherwise alter
// the password store then there are more possibilities for havoc // the password store then there are more possibilities for havoc
KConfig kconfig(m_passwdstore); KSettings ksettings(m_passwdstore, KSettings::SimpleConfig);
KConfigGroup kconfiggroup = kconfig.group("KPasswdStore"); QString storekey = QString::fromLatin1("KPasswdStore/");
const QString storepasswdhash = kconfiggroup.readEntry(m_storeid, QString()); storekey.append(m_storeid);
const QString storepasswdhash = ksettings.value(storekey).toString();
if (storepasswdhash.isEmpty()) { if (storepasswdhash.isEmpty()) {
KNewPasswordDialog knewpasswddialog(widgetForWindowID(windowid)); KNewPasswordDialog knewpasswddialog(widgetForWindowID(windowid));
knewpasswddialog.setPrompt(i18n("Enter a password for <b>%1</b> password storage", m_storeid)); knewpasswddialog.setPrompt(i18n("Enter a password for <b>%1</b> password storage", m_storeid));
@ -239,7 +243,7 @@ bool KPasswdStoreImpl::ensurePasswd(const qlonglong windowid, const bool showerr
} }
if (storepasswdhash.isEmpty()) { if (storepasswdhash.isEmpty()) {
kconfiggroup.writeEntry(m_storeid, passhash); ksettings.setValue(storekey, passhash);
return true; return true;
} }
if (passhash != storepasswdhash) { if (passhash != storepasswdhash) {

View file

@ -117,7 +117,6 @@ void KPasswdStore::setCacheOnly(const bool cacheonly)
bool KPasswdStore::cacheOnly() const bool KPasswdStore::cacheOnly() const
{ {
d->ensureInterface();
QDBusReply<bool> result = d->interface->call("cacheOnly", d->cookie, d->storeid); QDBusReply<bool> result = d->interface->call("cacheOnly", d->cookie, d->storeid);
return result.value(); return result.value();
} }