2013-06-06 04:57:06 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2013 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 "daemon.h"
|
2013-07-24 17:42:33 +01:00
|
|
|
|
2014-11-10 14:46:49 +00:00
|
|
|
#include <QDir>
|
2013-08-07 10:29:56 +01:00
|
|
|
#include <QUuid>
|
2014-01-16 16:34:08 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
2013-06-25 17:08:34 +01:00
|
|
|
#include <QDBusConnection>
|
2013-08-07 10:29:56 +01:00
|
|
|
#include <QNetworkSession>
|
|
|
|
#include <QNetworkConfigurationManager>
|
2014-06-14 14:22:40 +01:00
|
|
|
#include <QtCrypto>
|
2014-09-22 00:23:27 +01:00
|
|
|
#include <QStandardPaths>
|
2014-08-11 17:55:38 +01:00
|
|
|
#include <QNetworkAccessManager>
|
2013-07-02 01:46:41 +01:00
|
|
|
|
2013-08-31 12:04:00 +01:00
|
|
|
#include <KConfig>
|
2013-07-02 01:46:41 +01:00
|
|
|
#include <KConfigGroup>
|
2014-09-22 01:37:10 +01:00
|
|
|
#include <KSharedConfig>
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2014-09-21 23:59:34 +01:00
|
|
|
#include "core_debug.h"
|
2014-10-10 19:26:50 +01:00
|
|
|
#include "dbushelper.h"
|
2013-11-06 21:16:55 +00:00
|
|
|
#include "networkpackage.h"
|
|
|
|
#include "backends/lan/lanlinkprovider.h"
|
|
|
|
#include "backends/loopback/loopbacklinkprovider.h"
|
2014-06-14 14:22:40 +01:00
|
|
|
#include "device.h"
|
|
|
|
#include "networkpackage.h"
|
|
|
|
#include "backends/devicelink.h"
|
|
|
|
#include "backends/linkprovider.h"
|
|
|
|
|
2014-09-21 23:59:34 +01:00
|
|
|
#include <QDebug>
|
2014-06-14 14:22:40 +01:00
|
|
|
struct DaemonPrivate
|
|
|
|
{
|
|
|
|
//Different ways to find devices and connect to them
|
|
|
|
QSet<LinkProvider*> mLinkProviders;
|
|
|
|
|
|
|
|
//Every known device
|
|
|
|
QMap<QString, Device*> mDevices;
|
|
|
|
|
|
|
|
// The Initializer object sets things up, and also does cleanup when it goes out of scope
|
|
|
|
// Note it's not being used anywhere. That's inteneded
|
|
|
|
QCA::Initializer mQcaInitializer;
|
|
|
|
};
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2014-06-14 14:22:40 +01:00
|
|
|
Daemon::Daemon(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, d(new DaemonPrivate)
|
2013-06-06 04:57:06 +01:00
|
|
|
{
|
2014-11-04 18:12:29 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "KdeConnect daemon starting";
|
2014-08-11 17:53:08 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
|
|
|
|
|
2013-07-23 15:11:54 +01:00
|
|
|
if (!config->group("myself").hasKey("id")) {
|
2013-08-12 15:09:52 +01:00
|
|
|
QString uuid = QUuid::createUuid().toString();
|
2014-10-10 19:26:50 +01:00
|
|
|
DbusHelper::filterNonExportableCharacters(uuid);
|
2013-08-12 15:09:52 +01:00
|
|
|
config->group("myself").writeEntry("id", uuid);
|
2014-04-14 20:08:41 +01:00
|
|
|
config->sync();
|
2014-09-21 22:54:27 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "My id:" << uuid;
|
2013-07-23 15:11:54 +01:00
|
|
|
}
|
|
|
|
|
2014-09-21 22:54:27 +01:00
|
|
|
//qCDebug(KDECONNECT_CORE) << "QCA supported capabilities:" << QCA::supportedFeatures().join(",");
|
2013-10-05 14:55:25 +01:00
|
|
|
if(!QCA::isSupported("rsa")) {
|
2014-10-10 19:47:35 +01:00
|
|
|
//TODO: Display this in a notification or another visible way
|
2014-11-10 14:53:01 +00:00
|
|
|
qCCritical(KDECONNECT_CORE) << "Error: KDE Connect could not find support for RSA in your QCA installation, if your distribution provides"
|
2013-10-05 14:55:25 +01:00
|
|
|
<< "separate packages for QCA-ossl and QCA-gnupg plugins, make sure you have them installed and try again";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-16 20:47:21 +00:00
|
|
|
const QFile::Permissions strict = QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser;
|
2015-03-01 23:42:50 +00:00
|
|
|
|
|
|
|
const QString privateKeyPath = Device::privateKeyPath();
|
|
|
|
if (!QFile::exists(privateKeyPath))
|
2014-01-16 16:34:08 +00:00
|
|
|
{
|
2015-03-01 23:42:50 +00:00
|
|
|
QDir::root().mkpath(QFileInfo(privateKeyPath).dir().path());
|
2014-09-21 22:54:27 +01:00
|
|
|
|
2014-01-16 16:34:08 +00:00
|
|
|
QFile privKey(privateKeyPath);
|
2014-09-21 22:54:27 +01:00
|
|
|
|
2014-01-16 16:34:08 +00:00
|
|
|
if (!privKey.open(QIODevice::ReadWrite | QIODevice::Truncate))
|
|
|
|
{
|
2014-11-10 14:53:01 +00:00
|
|
|
qCCritical(KDECONNECT_CORE) << "Error: KDE Connect could not create private keys file: " << privateKeyPath;
|
2014-01-16 16:34:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-11-10 14:46:49 +00:00
|
|
|
|
2014-01-16 20:47:21 +00:00
|
|
|
if (!privKey.setPermissions(strict))
|
2014-01-16 16:34:08 +00:00
|
|
|
{
|
2014-09-21 22:54:27 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Error: KDE Connect could not set permissions for private file: " << privateKeyPath;
|
2014-01-16 16:34:08 +00:00
|
|
|
}
|
|
|
|
|
2015-03-01 23:42:50 +00:00
|
|
|
privKey.write(QCA::KeyGenerator().createRSA(2048).toPEM().toLatin1());
|
2014-01-16 16:34:08 +00:00
|
|
|
}
|
2014-09-23 18:45:29 +01:00
|
|
|
|
2015-03-01 23:42:50 +00:00
|
|
|
if (QFile::permissions(privateKeyPath) != strict)
|
2014-01-16 16:34:08 +00:00
|
|
|
{
|
2015-03-01 23:42:50 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Error: KDE Connect detects wrong permissions for private file " << privateKeyPath;
|
2013-08-30 18:10:43 +01:00
|
|
|
}
|
|
|
|
|
2014-08-11 17:55:38 +01:00
|
|
|
//Register on DBus
|
|
|
|
QDBusConnection::sessionBus().registerService("org.kde.kdeconnect");
|
|
|
|
QDBusConnection::sessionBus().registerObject("/modules/kdeconnect", this, QDBusConnection::ExportScriptableContents);
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2013-08-13 04:07:32 +01:00
|
|
|
//Load backends (hardcoded by now, should be plugins in a future)
|
2014-06-14 14:22:40 +01:00
|
|
|
d->mLinkProviders.insert(new LanLinkProvider());
|
|
|
|
//d->mLinkProviders.insert(new LoopbackLinkProvider());
|
2013-07-02 00:50:32 +01:00
|
|
|
|
2013-07-02 01:46:41 +01:00
|
|
|
//Read remebered paired devices
|
2013-09-03 15:01:28 +01:00
|
|
|
const KConfigGroup& known = config->group("trusted_devices");
|
2013-07-02 01:46:41 +01:00
|
|
|
const QStringList& list = known.groupList();
|
2013-07-03 02:52:44 +01:00
|
|
|
Q_FOREACH(const QString& id, list) {
|
2014-01-22 22:31:27 +00:00
|
|
|
Device* device = new Device(this, id);
|
2013-09-26 16:49:40 +01:00
|
|
|
connect(device, SIGNAL(reachableStatusChanged()),
|
|
|
|
this, SLOT(onDeviceReachableStatusChanged()));
|
2014-06-14 14:22:40 +01:00
|
|
|
d->mDevices[id] = device;
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_EMIT deviceAdded(id);
|
2013-07-02 01:46:41 +01:00
|
|
|
}
|
2014-09-23 18:45:29 +01:00
|
|
|
|
2014-08-11 17:55:38 +01:00
|
|
|
//Listen to new devices
|
2014-06-14 14:22:40 +01:00
|
|
|
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
|
2013-08-12 15:09:52 +01:00
|
|
|
connect(a, SIGNAL(onConnectionReceived(NetworkPackage, DeviceLink*)),
|
|
|
|
this, SLOT(onNewDeviceLink(NetworkPackage, DeviceLink*)));
|
2013-06-06 04:57:06 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
setDiscoveryEnabled(true);
|
2013-06-17 11:23:08 +01:00
|
|
|
|
2014-08-11 17:55:38 +01:00
|
|
|
//Listen to connectivity changes
|
|
|
|
QNetworkConfigurationManager* manager = new QNetworkConfigurationManager();
|
|
|
|
QNetworkSession* network = new QNetworkSession(manager->defaultConfiguration());
|
|
|
|
connect(manager, SIGNAL(configurationAdded(QNetworkConfiguration)),
|
|
|
|
this, SLOT(forceOnNetworkChange()));
|
|
|
|
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
|
|
|
|
connect(network, SIGNAL(stateChanged(QNetworkSession::State)),
|
|
|
|
a, SLOT(onNetworkChange(QNetworkSession::State)));
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:12:29 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "KdeConnect daemon started";
|
2013-06-17 11:23:08 +01:00
|
|
|
}
|
2013-09-26 16:49:40 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
void Daemon::setDiscoveryEnabled(bool b)
|
2013-06-27 01:26:06 +01:00
|
|
|
{
|
2014-06-14 14:22:40 +01:00
|
|
|
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
|
2013-08-07 10:29:56 +01:00
|
|
|
if (b)
|
|
|
|
a->onStart();
|
|
|
|
else
|
|
|
|
a->onStop();
|
2013-06-27 01:26:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-09-26 16:49:40 +01:00
|
|
|
|
2013-08-07 10:29:56 +01:00
|
|
|
void Daemon::forceOnNetworkChange()
|
|
|
|
{
|
2014-06-14 14:22:40 +01:00
|
|
|
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
|
2013-08-07 10:29:56 +01:00
|
|
|
a->onNetworkChange(QNetworkSession::Connected);
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 01:26:06 +01:00
|
|
|
|
2014-04-12 20:47:28 +01:00
|
|
|
QStringList Daemon::devices(bool onlyReachable, bool onlyVisible) const
|
2013-08-13 22:23:32 +01:00
|
|
|
{
|
|
|
|
QStringList ret;
|
2014-06-14 14:22:40 +01:00
|
|
|
Q_FOREACH(Device* device, d->mDevices) {
|
2014-01-16 11:14:05 +00:00
|
|
|
if (onlyReachable && !device->isReachable()) continue;
|
|
|
|
if (onlyVisible && !device->isPaired()) continue;
|
|
|
|
ret.append(device->id());
|
2013-08-13 22:23:32 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-23 15:11:54 +01:00
|
|
|
void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink* dl)
|
2013-06-18 01:14:22 +01:00
|
|
|
{
|
2013-11-18 02:31:59 +00:00
|
|
|
|
2013-07-23 15:11:54 +01:00
|
|
|
const QString& id = identityPackage.get<QString>("deviceId");
|
2013-07-03 02:52:44 +01:00
|
|
|
|
2014-09-21 22:54:27 +01:00
|
|
|
//qCDebug(KDECONNECT_CORE) << "Device discovered" << id << "via" << dl->provider()->name();
|
2013-07-23 15:11:54 +01:00
|
|
|
|
2014-06-14 14:22:40 +01:00
|
|
|
if (d->mDevices.contains(id)) {
|
2014-09-21 22:54:27 +01:00
|
|
|
//qCDebug(KDECONNECT_CORE) << "It is a known device";
|
2014-06-14 14:22:40 +01:00
|
|
|
Device* device = d->mDevices[id];
|
2013-10-01 02:11:22 +01:00
|
|
|
device->addLink(identityPackage, dl);
|
2013-07-03 02:52:44 +01:00
|
|
|
} else {
|
2014-09-21 22:54:27 +01:00
|
|
|
//qCDebug(KDECONNECT_CORE) << "It is a new device";
|
2013-07-04 18:17:22 +01:00
|
|
|
|
2014-01-22 22:31:27 +00:00
|
|
|
Device* device = new Device(this, identityPackage, dl);
|
2013-08-13 22:23:32 +01:00
|
|
|
connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceReachableStatusChanged()));
|
2014-06-14 14:22:40 +01:00
|
|
|
d->mDevices[id] = device;
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_EMIT deviceAdded(id);
|
|
|
|
}
|
|
|
|
|
2013-08-15 21:45:33 +01:00
|
|
|
Q_EMIT deviceVisibilityChanged(id, true);
|
2013-09-02 12:26:26 +01:00
|
|
|
|
2013-08-13 22:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Daemon::onDeviceReachableStatusChanged()
|
|
|
|
{
|
|
|
|
|
|
|
|
Device* device = (Device*)sender();
|
|
|
|
QString id = device->id();
|
|
|
|
|
2013-08-31 12:04:00 +01:00
|
|
|
Q_EMIT deviceVisibilityChanged(id, device->isReachable());
|
2013-08-13 22:23:32 +01:00
|
|
|
|
2014-09-21 22:54:27 +01:00
|
|
|
//qCDebug(KDECONNECT_CORE) << "Device" << device->name() << "reachable status changed:" << device->isReachable();
|
2013-09-09 17:28:52 +01:00
|
|
|
|
2013-08-31 12:04:00 +01:00
|
|
|
if (!device->isReachable()) {
|
2013-08-13 22:23:32 +01:00
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
if (!device->isPaired()) {
|
2014-09-21 22:54:27 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Destroying device" << device->name();
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_EMIT deviceRemoved(id);
|
2014-06-14 14:22:40 +01:00
|
|
|
d->mDevices.remove(id);
|
2013-08-13 22:23:32 +01:00
|
|
|
device->deleteLater();
|
|
|
|
}
|
|
|
|
|
2013-06-18 01:14:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-06 04:57:06 +01:00
|
|
|
Daemon::~Daemon()
|
|
|
|
{
|
2013-07-24 17:42:33 +01:00
|
|
|
|
2013-06-06 04:57:06 +01:00
|
|
|
}
|
2013-06-17 11:23:08 +01:00
|
|
|
|