| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "msgreceiver.h" |
| 5 | |
| 6 | #include "common/icryptolayer.h" |
| 7 | #include "isysfswriter.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 | MsgReceiver::MsgReceiver(std::shared_ptr<ICryptoLayer> cryptoLayer, |
| 18 | std::unique_ptr<ISysfsWriter> &&sysfsWriter, |
| 19 | QObject *parent) |
| 20 | : QObject(parent) |
| 21 | , cryptoLayer_(std::move(cryptoLayer)) |
| 22 | , sysfsWriter_(std::move(sysfsWriter)) |
| 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( |
| 29 | QStringLiteral(DBUS_HELPER_SYSCTL_PATH), |
| 30 | QStringLiteral(DBUS_HELPER_SYSCTL_INTERFACE), this, |
| 31 | QDBusConnection::ExportScriptableSlots)) |
| 32 | throw std::runtime_error( |
| 33 | std::format("Could not register D-Bus object on path {} " |
| 34 | "using the interface {}\n.Last D-Bus error: {}", |
| 35 | DBUS_HELPER_SYSCTL_PATH, DBUS_HELPER_SYSCTL_INTERFACE, |
| 36 | dbusConnection.lastError().message().toStdString())); |
| 37 | } |
| 38 | |
| 39 | MsgReceiver::~MsgReceiver() |
| 40 | { |
| 41 | QDBusConnection dbusConnection = QDBusConnection::systemBus(); |
| 42 | dbusConnection.unregisterObject(QStringLiteral(DBUS_HELPER_SYSCTL_INTERFACE)); |
| 43 | } |
| 44 | |
| 45 | void MsgReceiver::apply(QByteArray const &data, QByteArray const &signature) |
| 46 | { |
| 47 | if (cryptoLayer_->verify(data, signature)) { |
| 48 | auto cmdList = data.split('\0'); |
| 49 | for (int i = 0; i + 1 < cmdList.size(); i += 2) |
| 50 | sysfsWriter_->write(cmdList[i].data(), cmdList[i + 1].data()); |
| 51 | } |
| 52 | else |
| 53 | SPDLOG_DEBUG("Failed to verify received data from D-Bus"); |
| 54 | } |