| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "msgdispatcher.h" |
| 5 | |
| 6 | #include "common/icryptolayer.h" |
| 7 | #include "iappregistry.h" |
| 8 | #include <QByteArray> |
| 9 | #include <QDBusConnection> |
| 10 | #include <QDBusError> |
| 11 | #include <QString> |
| 12 | #include <format> |
| 13 | #include <spdlog/spdlog.h> |
| 14 | #include <stdexcept> |
| 15 | #include <utility> |
| 16 | |
| 17 | DBusSignalDispatcher::DBusSignalDispatcher( |
| 18 | std::shared_ptr<ICryptoLayer> cryptoLayer, |
| 19 | std::shared_ptr<IAppRegistry> appRegistry, QObject *parent) |
| 20 | : QObject(parent) |
| 21 | , cryptoLayer_(std::move(cryptoLayer)) |
| 22 | , appRegistry_(std::move(appRegistry)) |
| 23 | { |
| 24 | QDBusConnection dbusConnection = QDBusConnection::systemBus(); |
| 25 | if (!dbusConnection.isConnected()) |
| 26 | throw std::runtime_error("Could not connect to D-Bus system bus"); |
| 27 | |
| 28 | if (!dbusConnection.registerObject(QStringLiteral(DBUS_HELPER_PMON_PATH), |
| 29 | QStringLiteral(DBUS_HELPER_PMON_INTERFACE), |
| 30 | this, |
| 31 | QDBusConnection::ExportScriptableSignals | |
| 32 | QDBusConnection::ExportScriptableSlots)) |
| 33 | throw std::runtime_error( |
| 34 | std::format("Could not register D-Bus object on path {} " |
| 35 | "using the interface {}\n.Last D-Bus error: {}", |
| 36 | DBUS_HELPER_PMON_PATH, DBUS_HELPER_PMON_INTERFACE, |
| 37 | dbusConnection.lastError().message().toStdString())); |
| 38 | } |
| 39 | |
| 40 | DBusSignalDispatcher::~DBusSignalDispatcher() |
| 41 | { |
| 42 | QDBusConnection dbusConnection = QDBusConnection::systemBus(); |
| 43 | dbusConnection.unregisterObject(QStringLiteral(DBUS_HELPER_PMON_INTERFACE)); |
| 44 | } |
| 45 | |
| 46 | void DBusSignalDispatcher::sendExec(QByteArray const &data) |
| 47 | { |
| 48 | auto signature = cryptoLayer_->signature(data); |
| 49 | emit appExec(data, signature); |
| 50 | } |
| 51 | |
| 52 | void DBusSignalDispatcher::sendExit(QByteArray const &data) |
| 53 | { |
| 54 | auto signature = cryptoLayer_->signature(data); |
| 55 | emit appExit(data, signature); |
| 56 | } |
| 57 | |
| 58 | void DBusSignalDispatcher::watchApp(QByteArray const &data, |
| 59 | QByteArray const &signature) |
| 60 | { |
| 61 | if (cryptoLayer_->verify(data, signature)) |
| 62 | appRegistry_->add(data.toStdString()); |
| 63 | else |
| 64 | SPDLOG_DEBUG("Failed to verify received data from D-Bus"); |
| 65 | } |
| 66 | |
| 67 | void DBusSignalDispatcher::forgetApp(QByteArray const &data, |
| 68 | QByteArray const &signature) |
| 69 | { |
| 70 | if (cryptoLayer_->verify(data, signature)) |
| 71 | appRegistry_->remove(data.toStdString()); |
| 72 | else |
| 73 | SPDLOG_DEBUG("Failed to verify received data from D-Bus"); |
| 74 | } |
| 75 | |
| 76 | MsgDispatcher::MsgDispatcher(std::shared_ptr<ICryptoLayer> cryptoLayer, |
| 77 | std::shared_ptr<IAppRegistry> appRegistry, |
| 78 | QObject *parent) |
| 79 | : QObject(parent) |
| 80 | , dbusSignalDispatcher_(std::move(cryptoLayer), std::move(appRegistry), this) |
| 81 | { |
| 82 | connect(this, &MsgDispatcher::appExec, &dbusSignalDispatcher_, |
| 83 | &DBusSignalDispatcher::sendExec); |
| 84 | connect(this, &MsgDispatcher::appExit, &dbusSignalDispatcher_, |
| 85 | &DBusSignalDispatcher::sendExit); |
| 86 | } |
| 87 | |
| 88 | void MsgDispatcher::sendExec(std::string const &app) |
| 89 | { |
| 90 | emit appExec({app.c_str()}); |
| 91 | } |
| 92 | |
| 93 | void MsgDispatcher::sendExit(std::string const &app) |
| 94 | { |
| 95 | emit appExit({app.c_str()}); |
| 96 | } |