2022-04-04 19:44:39 +03:00
|
|
|
/* This file is part of the KDE libraries
|
|
|
|
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
2022-04-06 06:42:21 +03:00
|
|
|
*/
|
2022-04-04 19:44:39 +03:00
|
|
|
|
|
|
|
#include "kpasswdstore.h"
|
|
|
|
|
|
|
|
#include <QApplication>
|
2022-04-06 06:42:21 +03:00
|
|
|
#include <QDBusInterface>
|
|
|
|
#include <QDBusReply>
|
2022-04-04 19:44:39 +03:00
|
|
|
#include <QCryptographicHash>
|
|
|
|
|
2022-04-06 06:42:21 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2022-04-04 19:44:39 +03:00
|
|
|
|
2022-04-06 06:42:21 +03:00
|
|
|
static QByteArray getCookie()
|
2022-04-04 19:44:39 +03:00
|
|
|
{
|
2022-04-06 06:42:21 +03:00
|
|
|
// TODO: config knob for this, eavesdropping will be piece of cake
|
|
|
|
return QByteArray::number(::getuid());
|
|
|
|
#if 0
|
|
|
|
return QByteArray::number(::getpid());
|
|
|
|
return qRandomUuid();
|
2022-04-04 19:44:39 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
KPasswdStore::KPasswdStore(QObject *parent)
|
|
|
|
: QObject(parent),
|
2022-04-06 06:42:21 +03:00
|
|
|
m_interface("org.kde.kded", "/modules/kpasswdstore", "org.kde.kpasswdstore"),
|
|
|
|
m_cookie(getCookie()),
|
|
|
|
m_storeid(QApplication::applicationName())
|
2022-04-04 19:44:39 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KPasswdStore::~KPasswdStore()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void KPasswdStore::setStoreID(const QString &id)
|
|
|
|
{
|
2022-04-06 06:42:21 +03:00
|
|
|
m_storeid = id;
|
2022-04-04 19:44:39 +03:00
|
|
|
}
|
|
|
|
|
2022-04-05 00:35:59 +03:00
|
|
|
bool KPasswdStore::openStore(const qlonglong windowid)
|
|
|
|
{
|
2022-04-06 06:42:21 +03:00
|
|
|
QDBusReply<bool> result = m_interface.call("openStore", m_cookie, m_storeid, windowid);
|
|
|
|
return result.value();
|
2022-04-05 00:35:59 +03:00
|
|
|
}
|
|
|
|
|
2022-04-04 19:44:39 +03:00
|
|
|
void KPasswdStore::setCacheOnly(const bool cacheonly)
|
|
|
|
{
|
2022-04-06 06:42:21 +03:00
|
|
|
m_interface.call("setCacheOnly", m_cookie, m_storeid, cacheonly);
|
2022-04-04 19:44:39 +03:00
|
|
|
}
|
|
|
|
|
2022-04-04 23:11:42 +03:00
|
|
|
bool KPasswdStore::cacheOnly() const
|
|
|
|
{
|
2022-04-06 06:42:21 +03:00
|
|
|
QDBusReply<bool> result = m_interface.call("cacheOnly", m_cookie, m_storeid);
|
|
|
|
return result.value();
|
2022-04-04 23:11:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool KPasswdStore::hasPasswd(const QByteArray &key, const qlonglong windowid)
|
|
|
|
{
|
|
|
|
return !getPasswd(key, windowid).isEmpty();
|
|
|
|
}
|
|
|
|
|
2022-04-05 00:35:59 +03:00
|
|
|
QString KPasswdStore::getPasswd(const QByteArray &key, const qlonglong windowid)
|
2022-04-04 19:44:39 +03:00
|
|
|
{
|
2022-04-06 06:42:21 +03:00
|
|
|
QDBusReply<QString> result = m_interface.call("getPasswd", m_cookie, m_storeid, key, windowid);
|
|
|
|
return result.value();
|
2022-04-04 19:44:39 +03:00
|
|
|
}
|
|
|
|
|
2022-04-04 23:11:42 +03:00
|
|
|
bool KPasswdStore::storePasswd(const QByteArray &key, const QString &passwd, const qlonglong windowid)
|
2022-04-04 19:44:39 +03:00
|
|
|
{
|
2022-04-06 06:42:21 +03:00
|
|
|
QDBusReply<bool> result = m_interface.call("storePasswd", m_cookie, m_storeid, key, passwd, windowid);
|
|
|
|
return result.value();
|
2022-04-04 19:44:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray KPasswdStore::makeKey(const QString &string)
|
|
|
|
{
|
2022-04-06 06:42:21 +03:00
|
|
|
#if QT_VERSION >= 0x041200
|
|
|
|
return QCryptographicHash::hash(string.toUtf8(), QCryptographicHash::KAT).toHex();
|
|
|
|
#else
|
|
|
|
return QCryptographicHash::hash(string.toUtf8(), QCryptographicHash::Sha256).toHex();
|
|
|
|
#endif
|
2022-04-04 20:08:54 +03:00
|
|
|
}
|