2015-03-02 04:16:07 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2015 Albert Vaca <albertvaka@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kdeconnectconfig.h"
|
|
|
|
|
2015-03-09 05:17:54 +00:00
|
|
|
#include <KLocalizedString>
|
2015-03-02 04:16:07 +00:00
|
|
|
|
|
|
|
#include <QtCrypto>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QUuid>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QStandardPaths>
|
2015-03-04 21:29:04 +00:00
|
|
|
#include <QCoreApplication>
|
2015-03-02 04:16:07 +00:00
|
|
|
#include <QHostInfo>
|
2015-03-16 02:20:24 +00:00
|
|
|
#include <QSettings>
|
2015-07-05 14:23:53 +01:00
|
|
|
#include <QtNetwork/qsslcertificate.h>
|
2015-03-02 04:16:07 +00:00
|
|
|
|
|
|
|
#include "core_debug.h"
|
|
|
|
#include "dbushelper.h"
|
2015-03-24 11:26:37 +00:00
|
|
|
#include "daemon.h"
|
2015-03-02 04:16:07 +00:00
|
|
|
|
|
|
|
struct KdeConnectConfigPrivate {
|
|
|
|
|
|
|
|
// The Initializer object sets things up, and also does cleanup when it goes out of scope
|
2015-07-05 14:23:53 +01:00
|
|
|
// Note it's not being used anywhere. That's intended
|
2015-03-02 04:16:07 +00:00
|
|
|
QCA::Initializer mQcaInitializer;
|
|
|
|
|
|
|
|
QCA::PrivateKey privateKey;
|
2015-07-05 14:23:53 +01:00
|
|
|
QSslCertificate certificate; // Use QSslCertificate instead of QCA::Certificate due to compatibility with QSslSocket
|
2015-03-02 04:16:07 +00:00
|
|
|
|
2015-03-16 02:20:24 +00:00
|
|
|
QSettings* config;
|
2015-03-02 04:16:07 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
KdeConnectConfig* KdeConnectConfig::instance()
|
|
|
|
{
|
|
|
|
static KdeConnectConfig* kcc = new KdeConnectConfig();
|
|
|
|
return kcc;
|
|
|
|
}
|
|
|
|
|
|
|
|
KdeConnectConfig::KdeConnectConfig()
|
2015-03-09 05:17:54 +00:00
|
|
|
: d(new KdeConnectConfigPrivate)
|
|
|
|
{
|
2015-03-02 04:16:07 +00:00
|
|
|
//qCDebug(KDECONNECT_CORE) << "QCA supported capabilities:" << QCA::supportedFeatures().join(",");
|
|
|
|
if(!QCA::isSupported("rsa")) {
|
2015-03-24 11:26:37 +00:00
|
|
|
Daemon::instance()->reportError(
|
|
|
|
i18n("KDE Connect failed to start"),
|
2015-03-25 17:46:29 +00:00
|
|
|
i18n("Could not find support for RSA in your QCA installation. If your "
|
|
|
|
"distribution provides separate packages for QCA-ossl and QCA-gnupg, "
|
2015-03-09 05:17:54 +00:00
|
|
|
"make sure you have them installed and try again."));
|
2015-03-02 04:16:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Make sure base directory exists
|
|
|
|
QDir().mkpath(baseConfigDir().path());
|
|
|
|
|
|
|
|
//.config/kdeconnect/config
|
2015-03-16 02:20:24 +00:00
|
|
|
d->config = new QSettings(baseConfigDir().absoluteFilePath("config"), QSettings::IniFormat);
|
2015-03-02 04:16:07 +00:00
|
|
|
|
2015-03-16 02:20:24 +00:00
|
|
|
//Register my own id if not there yet
|
|
|
|
d->config->beginGroup("myself");
|
|
|
|
if (!d->config->contains("id")) {
|
2015-03-02 04:16:07 +00:00
|
|
|
QString uuid = QUuid::createUuid().toString();
|
|
|
|
DbusHelper::filterNonExportableCharacters(uuid);
|
2015-03-16 02:20:24 +00:00
|
|
|
d->config->setValue("id", uuid);
|
2015-03-02 04:16:07 +00:00
|
|
|
d->config->sync();
|
|
|
|
qCDebug(KDECONNECT_CORE) << "My id:" << uuid;
|
|
|
|
}
|
2015-03-16 02:20:24 +00:00
|
|
|
d->config->endGroup();
|
2015-03-02 04:16:07 +00:00
|
|
|
|
|
|
|
const QFile::Permissions strict = QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser;
|
|
|
|
|
|
|
|
QString keyPath = privateKeyPath();
|
|
|
|
QFile privKey(keyPath);
|
2015-03-09 05:17:54 +00:00
|
|
|
if (privKey.exists() && privKey.open(QIODevice::ReadOnly)) {
|
|
|
|
|
2015-03-02 04:16:07 +00:00
|
|
|
d->privateKey = QCA::PrivateKey::fromPEM(privKey.readAll());
|
2015-03-09 05:17:54 +00:00
|
|
|
|
|
|
|
} else {
|
2015-03-02 04:16:07 +00:00
|
|
|
|
|
|
|
d->privateKey = QCA::KeyGenerator().createRSA(2048);
|
|
|
|
|
2015-03-09 05:17:54 +00:00
|
|
|
if (!privKey.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
|
2015-03-24 11:26:37 +00:00
|
|
|
Daemon::instance()->reportError(QLatin1String("KDE Connect"), i18n("Could not store private key file: %1", keyPath));
|
2015-03-09 05:17:54 +00:00
|
|
|
} else {
|
|
|
|
privKey.setPermissions(strict);
|
|
|
|
privKey.write(d->privateKey.toPEM().toLatin1());
|
|
|
|
}
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 14:23:53 +01:00
|
|
|
QString certPath = certificatePath();
|
|
|
|
QFile cert(certPath);
|
|
|
|
if (cert.exists() && cert.open(QIODevice::ReadOnly)) {
|
|
|
|
|
|
|
|
// d->certificate = QCA::Certificate::fromPEMFile(certPath);
|
|
|
|
d->certificate = QSslCertificate::fromPath(certPath).value(0);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
QCA::CertificateOptions certificateOptions = QCA::CertificateOptions();
|
|
|
|
// TODO : Set serial number for certificate. Time millis or any constant number?
|
|
|
|
QCA::BigInteger bigInteger(10);
|
|
|
|
QDateTime startTime = QDateTime::currentDateTime();
|
|
|
|
QDateTime endTime = startTime.addYears(10);
|
|
|
|
QCA::CertificateInfo certificateInfo;
|
|
|
|
certificateInfo.insert(QCA::CommonName,d->config->value("id", "unknown id").toString());
|
|
|
|
certificateInfo.insert(QCA::Organization,"KDE");
|
|
|
|
certificateInfo.insert(QCA::OrganizationalUnit,"Kde connect");
|
|
|
|
certificateOptions.setFormat(QCA::PKCS10);
|
|
|
|
|
|
|
|
certificateOptions.setSerialNumber(bigInteger);
|
|
|
|
certificateOptions.setInfo(certificateInfo);
|
|
|
|
certificateOptions.setValidityPeriod(startTime, endTime);
|
|
|
|
certificateOptions.setFormat(QCA::PKCS10);
|
|
|
|
|
|
|
|
// d->certificate = QCA::Certificate(certificateOptions, d->privateKey);
|
|
|
|
d->certificate = QSslCertificate(QCA::Certificate(certificateOptions, d->privateKey).toPEM().toLatin1());
|
|
|
|
|
|
|
|
if (!cert.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
|
|
|
|
Daemon::instance()->reportError(QLatin1String("KDE Connect"), i18n("Could not store certificate file: %1", certPath));
|
|
|
|
} else {
|
|
|
|
cert.setPermissions(strict);
|
|
|
|
cert.write(d->certificate.toPem());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 04:16:07 +00:00
|
|
|
//Extra security check
|
|
|
|
if (QFile::permissions(keyPath) != strict) {
|
2015-03-09 05:17:54 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Warning: KDE Connect private key file has too open permissions " << keyPath;
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 02:20:24 +00:00
|
|
|
QString KdeConnectConfig::name()
|
|
|
|
{
|
2015-03-02 04:16:07 +00:00
|
|
|
QString defaultName = qgetenv("USER") + "@" + QHostInfo::localHostName();
|
2015-03-16 02:20:24 +00:00
|
|
|
d->config->beginGroup("myself");
|
|
|
|
QString name = d->config->value("name", defaultName).toString();
|
|
|
|
d->config->endGroup();
|
2015-03-02 04:16:07 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectConfig::setName(QString name)
|
|
|
|
{
|
2015-03-16 02:20:24 +00:00
|
|
|
d->config->beginGroup("myself");
|
|
|
|
d->config->setValue("name", name);
|
|
|
|
d->config->endGroup();
|
2015-03-02 04:16:07 +00:00
|
|
|
d->config->sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString KdeConnectConfig::deviceType()
|
|
|
|
{
|
|
|
|
return "desktop"; // TODO
|
|
|
|
}
|
|
|
|
|
2015-03-16 02:20:24 +00:00
|
|
|
QString KdeConnectConfig::deviceId()
|
|
|
|
{
|
|
|
|
d->config->beginGroup("myself");
|
|
|
|
QString id = d->config->value("id", "").toString();
|
|
|
|
d->config->endGroup();
|
|
|
|
return id;
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString KdeConnectConfig::privateKeyPath()
|
|
|
|
{
|
|
|
|
return baseConfigDir().absoluteFilePath("privateKey.pem");
|
|
|
|
}
|
|
|
|
|
|
|
|
QCA::PrivateKey KdeConnectConfig::privateKey()
|
|
|
|
{
|
|
|
|
return d->privateKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
QCA::PublicKey KdeConnectConfig::publicKey()
|
|
|
|
{
|
|
|
|
return d->privateKey.toPublicKey();
|
|
|
|
}
|
|
|
|
|
2015-07-05 14:23:53 +01:00
|
|
|
QString KdeConnectConfig::certificatePath()
|
|
|
|
{
|
|
|
|
return baseConfigDir().absoluteFilePath("certificate.pem");
|
|
|
|
}
|
|
|
|
|
|
|
|
QSslCertificate KdeConnectConfig::certificate()
|
|
|
|
{
|
|
|
|
return d->certificate;
|
|
|
|
}
|
|
|
|
|
2015-03-02 04:16:07 +00:00
|
|
|
QDir KdeConnectConfig::baseConfigDir()
|
|
|
|
{
|
|
|
|
QString configPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
|
|
|
|
QString kdeconnectConfigPath = QDir(configPath).absoluteFilePath("kdeconnect");
|
|
|
|
return QDir(kdeconnectConfigPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList KdeConnectConfig::trustedDevices()
|
|
|
|
{
|
2015-03-16 02:20:24 +00:00
|
|
|
d->config->beginGroup("trustedDevices");
|
|
|
|
const QStringList& list = d->config->childGroups();
|
|
|
|
d->config->endGroup();
|
2015-03-02 04:16:07 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2015-07-05 14:23:53 +01:00
|
|
|
void KdeConnectConfig::addTrustedDevice(QString id, QString name, QString type, QString publicKey, QString certificate)
|
2015-03-02 04:16:07 +00:00
|
|
|
{
|
2015-03-16 02:20:24 +00:00
|
|
|
d->config->beginGroup("trustedDevices");
|
|
|
|
d->config->beginGroup(id);
|
|
|
|
d->config->setValue("name", name);
|
|
|
|
d->config->setValue("type", type);
|
|
|
|
d->config->setValue("publicKey", publicKey);
|
2015-07-05 14:23:53 +01:00
|
|
|
d->config->setValue("certificate", certificate);
|
2015-03-16 02:20:24 +00:00
|
|
|
d->config->endGroup();
|
|
|
|
d->config->endGroup();
|
2015-03-02 04:16:07 +00:00
|
|
|
d->config->sync();
|
|
|
|
|
|
|
|
QDir().mkpath(deviceConfigDir(id).path());
|
|
|
|
}
|
|
|
|
|
|
|
|
KdeConnectConfig::DeviceInfo KdeConnectConfig::getTrustedDevice(QString id)
|
|
|
|
{
|
2015-03-16 02:20:24 +00:00
|
|
|
d->config->beginGroup("trustedDevices");
|
|
|
|
d->config->beginGroup(id);
|
2015-03-02 04:16:07 +00:00
|
|
|
|
|
|
|
KdeConnectConfig::DeviceInfo info;
|
2015-03-16 02:20:24 +00:00
|
|
|
info.deviceName = d->config->value("name", QLatin1String("unnamed")).toString();
|
|
|
|
info.deviceType = d->config->value("type", QLatin1String("unknown")).toString();
|
|
|
|
info.publicKey = d->config->value("publicKey", QString()).toString();
|
2015-07-05 14:23:53 +01:00
|
|
|
info.certificate = d->config->value("certificate", QString()).toString();
|
2015-03-02 04:16:07 +00:00
|
|
|
|
2015-03-16 02:20:24 +00:00
|
|
|
d->config->endGroup();
|
|
|
|
d->config->endGroup();
|
2015-03-02 04:16:07 +00:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectConfig::removeTrustedDevice(QString deviceId)
|
|
|
|
{
|
2015-03-16 02:20:24 +00:00
|
|
|
d->config->beginGroup("trustedDevices");
|
|
|
|
d->config->beginGroup(deviceId);
|
|
|
|
d->config->remove(QString());
|
|
|
|
d->config->endGroup();
|
|
|
|
d->config->endGroup();
|
|
|
|
d->config->sync();
|
2015-03-14 04:19:39 +00:00
|
|
|
//We do not remove the config files.
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 14:23:53 +01:00
|
|
|
// Utility functions to set and get a value
|
|
|
|
void KdeConnectConfig::setDeviceProperty(QString deviceId, QString key, QString value)
|
|
|
|
{
|
|
|
|
d->config->beginGroup("trustedDevices");
|
|
|
|
d->config->beginGroup(deviceId);
|
|
|
|
d->config->setValue(key, value);
|
|
|
|
d->config->endGroup();
|
|
|
|
d->config->endGroup();
|
|
|
|
d->config->sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString KdeConnectConfig::getDeviceProperty(QString deviceId, QString key, QString defaultValue)
|
|
|
|
{
|
|
|
|
QString value;
|
|
|
|
d->config->beginGroup("trustedDevices");
|
|
|
|
d->config->beginGroup(deviceId);
|
|
|
|
value = d->config->value(key, defaultValue).toString();
|
|
|
|
d->config->endGroup();
|
|
|
|
d->config->endGroup();
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-02 04:16:07 +00:00
|
|
|
QDir KdeConnectConfig::deviceConfigDir(QString deviceId)
|
|
|
|
{
|
|
|
|
QString deviceConfigPath = baseConfigDir().absoluteFilePath(deviceId);
|
|
|
|
return QDir(deviceConfigPath);
|
|
|
|
}
|
2015-03-14 04:19:39 +00:00
|
|
|
|
|
|
|
QDir KdeConnectConfig::pluginConfigDir(QString deviceId, QString pluginName)
|
|
|
|
{
|
|
|
|
QString deviceConfigPath = baseConfigDir().absoluteFilePath(deviceId);
|
|
|
|
QString pluginConfigDir = QDir(deviceConfigPath).absoluteFilePath(pluginName);
|
|
|
|
return QDir(pluginConfigDir);
|
|
|
|
}
|
|
|
|
|