From 830dd34402558934826e6b303410182929ffd68d Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Thu, 23 Jan 2014 21:00:24 +0100 Subject: [PATCH] PrivateKey field in KConfig is now PrivateKeyPath It was causing errors on pre-existent installations, that used the field as if it contained the key. Relaxed permissions check, as it was silently return if they were wrong and this is very confusing for a user (that should open the log to see the error) --- kded/daemon.cpp | 13 +++++++------ kded/device.cpp | 24 ++++++++++++------------ kded/device.h | 3 +-- kded/kdeconnectd.cpp | 20 ++++++++++++++++++++ kded/kded.cpp | 5 ++--- kded/kded.h | 2 +- kded/plugins/sftp/mounter.cpp | 2 +- 7 files changed, 44 insertions(+), 25 deletions(-) diff --git a/kded/daemon.cpp b/kded/daemon.cpp index d71783113..c4ee42746 100644 --- a/kded/daemon.cpp +++ b/kded/daemon.cpp @@ -57,7 +57,7 @@ Daemon::Daemon(QObject *parent) : QObject(parent) } const QFile::Permissions strict = QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser; - if (!config->group("myself").hasKey("privateKey")) + if (!config->group("myself").hasKey("privateKeyPath")) { const QString privateKeyPath = KStandardDirs::locateLocal("appdata", "key.pem", true, KComponentData("kdeconnect", "kdeconnect")); @@ -72,20 +72,21 @@ Daemon::Daemon(QObject *parent) : QObject(parent) if (!privKey.setPermissions(strict)) { kWarning(kdeconnect_kded()) << "Error: KDE Connect could not set permissions for private file: " << privateKeyPath; - return; + //return; } //http://delta.affinix.com/docs/qca/rsatest_8cpp-example.html privKey.write(QCA::KeyGenerator().createRSA(2048).toPEM().toAscii()); privKey.close(); - config->group("myself").writeEntry("privateKey", privateKeyPath); + config->group("myself").writeEntry("privateKeyPath", privateKeyPath); } - if (QFile::permissions(config->group("myself").readEntry("privateKey")) != strict) + if (QFile::permissions(config->group("myself").readEntry("privateKeyPath")) != strict) { - kWarning(kdeconnect_kded()) << "Error: KDE Connect detects wrong permissions for private file " << config->group("myself").readEntry("privateKey"); - return; + kWarning(kdeconnect_kded()) << "Error: KDE Connect detects wrong permissions for private file " << config->group("myself").readEntry("privateKeyPath"); + //FIXME: Do not silently fail, because user won't notice the problem + //return; } //Debugging diff --git a/kded/device.cpp b/kded/device.cpp index aaedcf109..0d43e5d77 100644 --- a/kded/device.cpp +++ b/kded/device.cpp @@ -36,11 +36,11 @@ Device::Device(QObject* parent, const QString& id) const QString& key = data.readEntry("publicKey", QString()); m_publicKey = QCA::RSAPublicKey::fromPEM(key); - QFile privKey(config->group("myself").readEntry("privateKey")); - if (privKey.open(QIODevice::ReadOnly)) - { - m_privateKey = QCA::PrivateKey::fromPEM(privKey.readAll()); - } + //TODO: It is redundant to have our own private key in every instance of Device, move this to a signleton somewhere (Daemon?) + const QString privateKeyPath = KStandardDirs::locateLocal("appdata", "key.pem", true, KComponentData("kdeconnect", "kdeconnect")); + QFile privKey(privateKeyPath); + privKey.open(QIODevice::ReadOnly); + m_privateKey = QCA::PrivateKey::fromPEM(privKey.readAll()); //Register in bus QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors); @@ -54,11 +54,11 @@ Device::Device(QObject* parent, const NetworkPackage& identityPackage, DeviceLin , m_pairStatus(Device::NotPaired) , m_protocolVersion(identityPackage.get("protocolVersion")) { - QFile privKey(KSharedConfig::openConfig("kdeconnectrc")->group("myself").readEntry("privateKey")); - if (privKey.open(QIODevice::ReadOnly)) - { - m_privateKey = QCA::PrivateKey::fromPEM(privKey.readAll()); - } + //TODO: It is redundant to have our own private key in every instance of Device, move this to a signleton somewhere (Daemon?) + const QString privateKeyPath = KStandardDirs::locateLocal("appdata", "key.pem", true, KComponentData("kdeconnect", "kdeconnect")); + QFile privKey(privateKeyPath); + privKey.open(QIODevice::ReadOnly); + m_privateKey = QCA::PrivateKey::fromPEM(privKey.readAll()); addLink(identityPackage, dl); @@ -258,9 +258,9 @@ void Device::removeLink(DeviceLink* link) } } -QString Device::privateKey() const +QString Device::privateKeyPath() const { - return KSharedConfig::openConfig("kdeconnectrc")->group("myself").readEntry("privateKey"); + return KSharedConfig::openConfig("kdeconnectrc")->group("myself").readEntry("privateKeyPath"); } bool Device::sendPackage(NetworkPackage& np) diff --git a/kded/device.h b/kded/device.h index 901350e28..f576d06ac 100644 --- a/kded/device.h +++ b/kded/device.h @@ -83,7 +83,7 @@ public: void addLink(const NetworkPackage& identityPackage, DeviceLink*); void removeLink(DeviceLink*); - QString privateKey() const; + QString privateKeyPath() const; Q_SCRIPTABLE bool isPaired() const { return m_pairStatus==Device::Paired; } Q_SCRIPTABLE bool pairRequested() const { return m_pairStatus==Device::Requested; } @@ -120,7 +120,6 @@ Q_SIGNALS: Q_SCRIPTABLE void unpaired(); private: - //TODO: Replace device id by public key const QString m_deviceId; QString m_deviceName; DeviceType m_deviceType; diff --git a/kded/kdeconnectd.cpp b/kded/kdeconnectd.cpp index 592925607..e99e8f845 100644 --- a/kded/kdeconnectd.cpp +++ b/kded/kdeconnectd.cpp @@ -1,7 +1,27 @@ +/** + * Copyright 2014 Yuri Samoilenko + * + * 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 . + */ #include #include #include +#include #include #include diff --git a/kded/kded.cpp b/kded/kded.cpp index 588af75fe..a15afcccf 100644 --- a/kded/kded.cpp +++ b/kded/kded.cpp @@ -1,5 +1,5 @@ /** - * Copyright 2013 Albert Vaca + * Copyright 2014 Yuri Samoilenko * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -20,7 +20,6 @@ #include "kded.h" -#include #include #include @@ -62,7 +61,7 @@ bool Kded::start() m_daemon->setProgram(daemon); m_daemon->setOutputChannelMode(KProcess::SeparateChannels); m_daemon->start(); - if (!m_daemon->waitForStarted(10000)) + if (!m_daemon->waitForStarted(2000)) //FIXME: KDEDs should be non-blocking, do we really need to wait for it to start? { kError(kdeconnect_kded()) << "Can't start " << daemon; return false; diff --git a/kded/kded.h b/kded/kded.h index da247526b..5914d9688 100644 --- a/kded/kded.h +++ b/kded/kded.h @@ -1,5 +1,5 @@ /** - * Copyright 2013 Albert Vaca + * Copyright 2014 Yuri Samoilenko * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as diff --git a/kded/plugins/sftp/mounter.cpp b/kded/plugins/sftp/mounter.cpp index 57336b6d1..6d2e16fae 100644 --- a/kded/plugins/sftp/mounter.cpp +++ b/kded/plugins/sftp/mounter.cpp @@ -116,7 +116,7 @@ void Mounter::onPakcageReceived(const NetworkPackage& np) << "-p" << np.get("port") << "-d" << "-f" - << "-o" << "IdentityFile=" + m_sftp->device()->privateKey(); + << "-o" << "IdentityFile=" + m_sftp->device()->privateKeyPath(); m_proc->setProgram(program, arguments);