2015-03-02 04:16:07 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2015 Albert Vaca <albertvaka@gmail.com>
|
2015-03-02 04:16:07 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2015-03-02 04:16:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kdeconnectconfig.h"
|
|
|
|
|
2015-03-09 05:17:54 +00:00
|
|
|
#include <KLocalizedString>
|
2015-03-02 04:16:07 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QCoreApplication>
|
2015-03-02 04:16:07 +00:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
2020-03-21 20:13:41 +00:00
|
|
|
#include <QHostInfo>
|
2015-03-16 02:20:24 +00:00
|
|
|
#include <QSettings>
|
2015-07-14 13:04:04 +01:00
|
|
|
#include <QSslCertificate>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QStandardPaths>
|
2019-06-18 02:21:31 +01:00
|
|
|
#include <QThread>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QUuid>
|
2015-03-02 04:16:07 +00:00
|
|
|
|
|
|
|
#include "core_debug.h"
|
2015-03-24 11:26:37 +00:00
|
|
|
#include "daemon.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "dbushelper.h"
|
2023-06-27 12:10:59 +01:00
|
|
|
#include "deviceinfo.h"
|
|
|
|
#include "pluginloader.h"
|
2023-07-12 17:54:16 +01:00
|
|
|
#include "sslhelper.h"
|
2015-03-02 04:16:07 +00:00
|
|
|
|
2019-03-11 12:37:15 +00:00
|
|
|
const QFile::Permissions strictPermissions = QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser;
|
|
|
|
|
2015-03-02 04:16:07 +00:00
|
|
|
struct KdeConnectConfigPrivate {
|
2023-07-12 17:54:16 +01:00
|
|
|
EVP_PKEY *m_privateKey;
|
|
|
|
QSslCertificate m_certificate;
|
2015-03-02 04:16:07 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QSettings *m_config;
|
|
|
|
QSettings *m_trustedDevices;
|
2015-03-02 04:16:07 +00:00
|
|
|
|
2022-04-12 06:40:03 +01:00
|
|
|
#ifdef Q_OS_MAC
|
2022-09-10 22:23:52 +01:00
|
|
|
QString m_privateDBusAddress; // Private DBus Address cache
|
2019-06-18 02:21:31 +01:00
|
|
|
#endif
|
2015-03-02 04:16:07 +00:00
|
|
|
};
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static QString getDefaultDeviceName()
|
|
|
|
{
|
2020-09-24 16:01:03 +01:00
|
|
|
return QHostInfo::localHostName();
|
2020-08-02 19:57:41 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KdeConnectConfig &KdeConnectConfig::instance()
|
2015-03-02 04:16:07 +00:00
|
|
|
{
|
2019-09-08 16:09:52 +01:00
|
|
|
static KdeConnectConfig kcc;
|
2015-03-02 04:16:07 +00:00
|
|
|
return kcc;
|
|
|
|
}
|
|
|
|
|
|
|
|
KdeConnectConfig::KdeConnectConfig()
|
2015-03-09 05:17:54 +00:00
|
|
|
: d(new KdeConnectConfigPrivate)
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
// Make sure base directory exists
|
2015-03-02 04:16:07 +00:00
|
|
|
QDir().mkpath(baseConfigDir().path());
|
|
|
|
|
|
|
|
//.config/kdeconnect/config
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config = new QSettings(baseConfigDir().absoluteFilePath(QStringLiteral("config")), QSettings::IniFormat);
|
|
|
|
d->m_trustedDevices = new QSettings(baseConfigDir().absoluteFilePath(QStringLiteral("trusted_devices")), QSettings::IniFormat);
|
2015-03-02 04:16:07 +00:00
|
|
|
|
2023-07-12 17:54:16 +01:00
|
|
|
loadOrGeneratePrivateKeyAndCertificate(privateKeyPath(), certificatePath());
|
2020-08-02 19:57:41 +01:00
|
|
|
|
|
|
|
if (name().isEmpty()) {
|
|
|
|
setName(getDefaultDeviceName());
|
|
|
|
}
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 02:20:24 +00:00
|
|
|
QString KdeConnectConfig::name()
|
|
|
|
{
|
2020-08-02 19:57:41 +01:00
|
|
|
return d->m_config->value(QStringLiteral("name")).toString();
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void KdeConnectConfig::setName(const QString &name)
|
2015-03-02 04:16:07 +00:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config->setValue(QStringLiteral("name"), name);
|
|
|
|
d->m_config->sync();
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
DeviceType KdeConnectConfig::deviceType()
|
2015-03-02 04:16:07 +00:00
|
|
|
{
|
2020-04-19 21:40:49 +01:00
|
|
|
const QByteArrayList platforms = qgetenv("PLASMA_PLATFORM").split(':');
|
|
|
|
|
|
|
|
if (platforms.contains("phone")) {
|
2023-06-27 12:10:59 +01:00
|
|
|
return DeviceType::Phone;
|
2020-04-19 21:40:49 +01:00
|
|
|
} else if (platforms.contains("tablet")) {
|
2023-06-27 12:10:59 +01:00
|
|
|
return DeviceType::Tablet;
|
2022-09-10 22:23:52 +01:00
|
|
|
} else if (platforms.contains("mediacenter")) {
|
2023-06-27 12:10:59 +01:00
|
|
|
return DeviceType::Tv;
|
2020-04-19 21:40:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO non-Plasma mobile platforms
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
return DeviceType::Desktop;
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 02:20:24 +00:00
|
|
|
QString KdeConnectConfig::deviceId()
|
|
|
|
{
|
2018-12-25 00:52:16 +00:00
|
|
|
return d->m_certificate.subjectInfo(QSslCertificate::CommonName).constFirst();
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString KdeConnectConfig::privateKeyPath()
|
|
|
|
{
|
2016-11-26 14:38:08 +00:00
|
|
|
return baseConfigDir().absoluteFilePath(QStringLiteral("privateKey.pem"));
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 14:23:53 +01:00
|
|
|
QString KdeConnectConfig::certificatePath()
|
|
|
|
{
|
2016-11-26 14:38:08 +00:00
|
|
|
return baseConfigDir().absoluteFilePath(QStringLiteral("certificate.pem"));
|
2015-07-05 14:23:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSslCertificate KdeConnectConfig::certificate()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
return d->m_certificate;
|
2015-07-05 14:23:53 +01:00
|
|
|
}
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
DeviceInfo KdeConnectConfig::deviceInfo()
|
|
|
|
{
|
2023-04-17 20:20:51 +01:00
|
|
|
const auto incoming = PluginLoader::instance()->incomingCapabilities();
|
|
|
|
const auto outgoing = PluginLoader::instance()->outgoingCapabilities();
|
2023-06-27 12:10:59 +01:00
|
|
|
return DeviceInfo(deviceId(),
|
|
|
|
certificate(),
|
|
|
|
name(),
|
|
|
|
deviceType(),
|
|
|
|
NetworkPacket::s_protocolVersion,
|
2023-04-17 20:20:51 +01:00
|
|
|
QSet(incoming.begin(), incoming.end()),
|
|
|
|
QSet(outgoing.begin(), outgoing.end()));
|
2023-06-27 12:10:59 +01:00
|
|
|
}
|
|
|
|
|
2015-03-02 04:16:07 +00:00
|
|
|
QDir KdeConnectConfig::baseConfigDir()
|
|
|
|
{
|
2019-08-01 15:01:50 +01:00
|
|
|
QString configPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
|
2016-11-26 14:38:08 +00:00
|
|
|
QString kdeconnectConfigPath = QDir(configPath).absoluteFilePath(QStringLiteral("kdeconnect"));
|
2015-03-02 04:16:07 +00:00
|
|
|
return QDir(kdeconnectConfigPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList KdeConnectConfig::trustedDevices()
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
const QStringList &list = d->m_trustedDevices->childGroups();
|
2015-03-02 04:16:07 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
void KdeConnectConfig::addTrustedDevice(const DeviceInfo &deviceInfo)
|
2015-03-02 04:16:07 +00:00
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
d->m_trustedDevices->beginGroup(deviceInfo.id);
|
|
|
|
d->m_trustedDevices->setValue(QStringLiteral("name"), deviceInfo.name);
|
|
|
|
d->m_trustedDevices->setValue(QStringLiteral("type"), deviceInfo.type.toString());
|
|
|
|
QString certString = QString::fromLatin1(deviceInfo.certificate.toPem());
|
|
|
|
d->m_trustedDevices->setValue(QStringLiteral("certificate"), certString);
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_trustedDevices->endGroup();
|
|
|
|
d->m_trustedDevices->sync();
|
2015-03-02 04:16:07 +00:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
QDir().mkpath(deviceConfigDir(deviceInfo.id).path());
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
void KdeConnectConfig::updateTrustedDeviceInfo(const DeviceInfo &deviceInfo)
|
|
|
|
{
|
|
|
|
if (!trustedDevices().contains(deviceInfo.id)) {
|
|
|
|
// do not store values for untrusted devices (it would make them trusted)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->m_trustedDevices->beginGroup(deviceInfo.id);
|
|
|
|
d->m_trustedDevices->setValue(QStringLiteral("name"), deviceInfo.name);
|
|
|
|
d->m_trustedDevices->setValue(QStringLiteral("type"), deviceInfo.type.toString());
|
|
|
|
d->m_trustedDevices->endGroup();
|
|
|
|
d->m_trustedDevices->sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSslCertificate KdeConnectConfig::getTrustedDeviceCertificate(const QString &id)
|
|
|
|
{
|
|
|
|
d->m_trustedDevices->beginGroup(id);
|
|
|
|
QString certString = d->m_trustedDevices->value(QStringLiteral("certificate"), QString()).toString();
|
|
|
|
d->m_trustedDevices->endGroup();
|
|
|
|
return QSslCertificate(certString.toLatin1());
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceInfo KdeConnectConfig::getTrustedDevice(const QString &id)
|
2015-03-02 04:16:07 +00:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_trustedDevices->beginGroup(id);
|
2015-03-02 04:16:07 +00:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
QString certString = d->m_trustedDevices->value(QStringLiteral("certificate"), QString()).toString();
|
|
|
|
QSslCertificate certificate(certString.toLatin1());
|
|
|
|
QString name = d->m_trustedDevices->value(QStringLiteral("name"), QLatin1String("unnamed")).toString();
|
|
|
|
QString typeString = d->m_trustedDevices->value(QStringLiteral("type"), QLatin1String("unknown")).toString();
|
|
|
|
DeviceType type = DeviceType::FromString(typeString);
|
2015-03-02 04:16:07 +00:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_trustedDevices->endGroup();
|
2023-06-27 12:10:59 +01:00
|
|
|
|
|
|
|
return DeviceInfo(id, certificate, name, type);
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void KdeConnectConfig::removeTrustedDevice(const QString &deviceId)
|
2015-03-02 04:16:07 +00:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_trustedDevices->remove(deviceId);
|
|
|
|
d->m_trustedDevices->sync();
|
2022-09-10 22:23:52 +01: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
|
2022-09-10 22:23:52 +01:00
|
|
|
void KdeConnectConfig::setDeviceProperty(const QString &deviceId, const QString &key, const QString &value)
|
2015-07-05 14:23:53 +01:00
|
|
|
{
|
2020-08-02 12:57:58 +01:00
|
|
|
// do not store values for untrusted devices (it would make them trusted)
|
|
|
|
if (!trustedDevices().contains(deviceId))
|
|
|
|
return;
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_trustedDevices->beginGroup(deviceId);
|
|
|
|
d->m_trustedDevices->setValue(key, value);
|
|
|
|
d->m_trustedDevices->endGroup();
|
|
|
|
d->m_trustedDevices->sync();
|
2015-07-05 14:23:53 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QString KdeConnectConfig::getDeviceProperty(const QString &deviceId, const QString &key, const QString &defaultValue)
|
2015-07-05 14:23:53 +01:00
|
|
|
{
|
|
|
|
QString value;
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_trustedDevices->beginGroup(deviceId);
|
|
|
|
value = d->m_trustedDevices->value(key, defaultValue).toString();
|
|
|
|
d->m_trustedDevices->endGroup();
|
2015-07-05 14:23:53 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void KdeConnectConfig::setCustomDevices(const QStringList &addresses)
|
2020-08-18 15:55:48 +01:00
|
|
|
{
|
|
|
|
d->m_config->setValue(QStringLiteral("customDevices"), addresses);
|
|
|
|
d->m_config->sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList KdeConnectConfig::customDevices() const
|
|
|
|
{
|
|
|
|
return d->m_config->value(QStringLiteral("customDevices")).toStringList();
|
|
|
|
}
|
2015-07-05 14:23:53 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QDir KdeConnectConfig::deviceConfigDir(const QString &deviceId)
|
2015-03-02 04:16:07 +00:00
|
|
|
{
|
|
|
|
QString deviceConfigPath = baseConfigDir().absoluteFilePath(deviceId);
|
|
|
|
return QDir(deviceConfigPath);
|
|
|
|
}
|
2015-03-14 04:19:39 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QDir KdeConnectConfig::pluginConfigDir(const QString &deviceId, const QString &pluginName)
|
2015-03-14 04:19:39 +00:00
|
|
|
{
|
|
|
|
QString deviceConfigPath = baseConfigDir().absoluteFilePath(deviceId);
|
|
|
|
QString pluginConfigDir = QDir(deviceConfigPath).absoluteFilePath(pluginName);
|
|
|
|
return QDir(pluginConfigDir);
|
|
|
|
}
|
|
|
|
|
2023-07-12 17:54:16 +01:00
|
|
|
bool KdeConnectConfig::loadPrivateKey(const QString &keyPath)
|
2018-12-25 00:33:08 +00:00
|
|
|
{
|
|
|
|
QFile privKey(keyPath);
|
2019-05-08 22:06:29 +01:00
|
|
|
if (privKey.exists() && privKey.open(QIODevice::ReadOnly)) {
|
2023-07-12 17:54:16 +01:00
|
|
|
d->m_privateKey = SslHelper::pemToRsaPrivateKey(privKey.readAll());
|
|
|
|
if (d->m_privateKey == nullptr) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << "Private key from" << keyPath << "is not valid!";
|
2019-03-11 12:37:15 +00:00
|
|
|
}
|
2018-12-25 00:33:08 +00:00
|
|
|
}
|
2023-07-12 17:54:16 +01:00
|
|
|
return (d->m_privateKey == nullptr);
|
2018-12-25 00:33:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 17:54:16 +01:00
|
|
|
bool KdeConnectConfig::loadCertificate(const QString &certPath)
|
2019-03-11 12:37:15 +00:00
|
|
|
{
|
|
|
|
QFile cert(certPath);
|
2019-05-08 22:06:29 +01:00
|
|
|
if (cert.exists() && cert.open(QIODevice::ReadOnly)) {
|
2023-07-12 17:54:16 +01:00
|
|
|
auto loadedCerts = QSslCertificate::fromData(cert.readAll());
|
2019-03-11 12:37:15 +00:00
|
|
|
if (loadedCerts.empty()) {
|
2019-05-08 22:06:29 +01:00
|
|
|
qCWarning(KDECONNECT_CORE) << "Certificate from" << certPath << "is not valid";
|
2019-03-11 12:37:15 +00:00
|
|
|
} else {
|
|
|
|
d->m_certificate = loadedCerts.at(0);
|
|
|
|
}
|
|
|
|
}
|
2023-07-12 17:54:16 +01:00
|
|
|
return d->m_certificate.isNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectConfig::loadOrGeneratePrivateKeyAndCertificate(const QString &keyPath, const QString &certPath)
|
|
|
|
{
|
|
|
|
bool needsToGenerateKey = loadPrivateKey(keyPath);
|
|
|
|
bool needsToGenerateCert = needsToGenerateKey || loadCertificate(certPath);
|
2018-12-25 00:33:08 +00:00
|
|
|
|
2023-07-12 17:54:16 +01:00
|
|
|
if (needsToGenerateKey) {
|
|
|
|
generatePrivateKey(keyPath);
|
|
|
|
}
|
2019-03-11 12:37:15 +00:00
|
|
|
if (needsToGenerateCert) {
|
|
|
|
generateCertificate(certPath);
|
|
|
|
}
|
2015-07-19 15:55:28 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Extra security check
|
2023-07-12 17:54:16 +01:00
|
|
|
if (QFile::permissions(keyPath) != strictPermissions) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << "Warning: KDE Connect private key file has too open permissions " << keyPath;
|
|
|
|
}
|
2019-03-11 12:37:15 +00:00
|
|
|
if (QFile::permissions(certPath) != strictPermissions) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << "Warning: KDE Connect certificate file has too open permissions " << certPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void KdeConnectConfig::generatePrivateKey(const QString &keyPath)
|
2019-05-08 22:06:29 +01:00
|
|
|
{
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Generating private key";
|
|
|
|
|
2023-07-12 17:54:16 +01:00
|
|
|
d->m_privateKey = SslHelper::generateRsaPrivateKey();
|
|
|
|
QByteArray keyPem = SslHelper::privateKeyToPEM(d->m_privateKey);
|
2019-05-08 22:06:29 +01:00
|
|
|
|
|
|
|
QFile privKey(keyPath);
|
2023-07-12 17:54:16 +01:00
|
|
|
bool error = false;
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!privKey.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
|
2019-05-08 22:06:29 +01:00
|
|
|
error = true;
|
|
|
|
} else {
|
|
|
|
privKey.setPermissions(strictPermissions);
|
2023-07-12 17:54:16 +01:00
|
|
|
int written = privKey.write(keyPem);
|
2019-05-08 22:06:29 +01:00
|
|
|
if (written <= 0) {
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
Daemon::instance()->reportError(QStringLiteral("KDE Connect"), i18n("Could not store private key file: %1", keyPath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void KdeConnectConfig::generateCertificate(const QString &certPath)
|
2019-03-11 12:37:15 +00:00
|
|
|
{
|
2019-05-08 22:06:29 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Generating certificate";
|
|
|
|
|
2019-03-11 12:37:15 +00:00
|
|
|
QString uuid = QUuid::createUuid().toString();
|
2019-08-14 16:36:19 +01:00
|
|
|
DBusHelper::filterNonExportableCharacters(uuid);
|
2019-03-11 12:37:15 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "My id:" << uuid;
|
|
|
|
|
2023-07-12 17:54:16 +01:00
|
|
|
X509 *certificate = SslHelper::generateSelfSignedCertificate(d->m_privateKey, uuid);
|
|
|
|
QByteArray pemCertificate = SslHelper::certificateToPEM(certificate);
|
|
|
|
X509_free(certificate);
|
|
|
|
d->m_certificate = QSslCertificate(pemCertificate);
|
2019-03-11 12:37:15 +00:00
|
|
|
|
|
|
|
QFile cert(certPath);
|
2023-07-12 17:54:16 +01:00
|
|
|
bool error = false;
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!cert.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
|
2019-03-11 12:37:15 +00:00
|
|
|
error = true;
|
|
|
|
} else {
|
|
|
|
cert.setPermissions(strictPermissions);
|
2023-07-12 17:54:16 +01:00
|
|
|
int written = cert.write(pemCertificate);
|
2019-03-11 12:37:15 +00:00
|
|
|
if (written <= 0) {
|
|
|
|
error = true;
|
2018-12-25 00:33:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-11 12:37:15 +00:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
Daemon::instance()->reportError(QStringLiteral("KDE Connect"), i18n("Could not store certificate file: %1", certPath));
|
|
|
|
}
|
2018-12-25 00:33:08 +00:00
|
|
|
}
|
2019-06-18 02:21:31 +01:00
|
|
|
|
2022-04-12 06:40:03 +01:00
|
|
|
#ifdef Q_OS_MAC
|
2019-06-18 02:21:31 +01:00
|
|
|
QString KdeConnectConfig::privateDBusAddressPath()
|
|
|
|
{
|
|
|
|
return baseConfigDir().absoluteFilePath(QStringLiteral("private_dbus_address"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString KdeConnectConfig::privateDBusAddress()
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (d->m_privateDBusAddress.length() != 0)
|
|
|
|
return d->m_privateDBusAddress;
|
2019-06-18 02:21:31 +01:00
|
|
|
|
|
|
|
QString dbusAddressPath = privateDBusAddressPath();
|
|
|
|
QFile dbusAddressFile(dbusAddressPath);
|
|
|
|
|
|
|
|
if (!dbusAddressFile.open(QFile::ReadOnly | QFile::Text)) {
|
|
|
|
qCCritical(KDECONNECT_CORE) << "Private DBus enabled but error read private dbus address conf";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextStream in(&dbusAddressFile);
|
|
|
|
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Waiting for private dbus";
|
|
|
|
|
|
|
|
int retry = 0;
|
|
|
|
QString addr = in.readLine();
|
2022-09-10 22:23:52 +01:00
|
|
|
while (addr.length() == 0 && retry < 5) {
|
2019-06-18 02:21:31 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Retry reading private DBus address after 3s";
|
|
|
|
QThread::sleep(3);
|
2022-09-10 22:23:52 +01:00
|
|
|
retry++;
|
|
|
|
addr = in.readLine(); // Read until first not empty line
|
2019-06-18 02:21:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (addr.length() == 0) {
|
|
|
|
qCCritical(KDECONNECT_CORE) << "Private DBus enabled but read private dbus address failed";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Private dbus address: " << addr;
|
2019-09-08 16:09:52 +01:00
|
|
|
|
2019-06-18 02:21:31 +01:00
|
|
|
d->m_privateDBusAddress = addr;
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
#endif
|