| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "helper.h" |
| 5 | |
| 6 | #include "common/cryptolayer.h" |
| 7 | #include "common/logger.h" |
| 8 | #include "helperids.h" |
| 9 | #include "pmon/appregistry.h" |
| 10 | #include "pmon/msgdispatcher.h" |
| 11 | #include "pmon/nlprocexecmonitor.h" |
| 12 | #include "pmon/nlprocexecsocket.h" |
| 13 | #include "pmon/procpidsolver.h" |
| 14 | #include "polkit.h" |
| 15 | #include "sysctl/msgreceiver.h" |
| 16 | #include "sysctl/sysfswriter.h" |
| 17 | #include <QByteArray> |
| 18 | #include <QCoreApplication> |
| 19 | #include <QDBusConnection> |
| 20 | #include <QDBusError> |
| 21 | #include <QString> |
| 22 | #include <exception> |
| 23 | #include <filesystem> |
| 24 | |
| 25 | Helper::Helper(QObject *parent) noexcept |
| 26 | : QDBusAbstractAdaptor(parent) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | Helper::~Helper() = default; |
| 31 | |
| 32 | QDBusVariant Helper::start(QByteArray const &appPublicKey, int autoExitTimeout, |
| 33 | QDBusMessage const &message) |
| 34 | { |
| 35 | if (!started()) { |
| 36 | setupLogger(std::filesystem::temp_directory_path() / LOG_HELPER_FILE_NAME); |
| 37 | |
| 38 | SPDLOG_DEBUG("----- Helper started -----"); |
| 39 | |
| 40 | if (!(isAuthorized(message) && initCrypto(appPublicKey) && |
| 41 | initProcessMonitor() && initMsgReceiver())) { |
| 42 | exitHelper(); |
| 43 | return QDBusVariant(false); |
| 44 | } |
| 45 | |
| 46 | autoExitTimer_.setInterval(autoExitTimeout); |
| 47 | autoExitTimer_.setSingleShot(true); |
| 48 | connect(&autoExitTimer_, &QTimer::timeout, this, &Helper::autoExitTimeout); |
| 49 | autoExitTimer_.start(); |
| 50 | } |
| 51 | |
| 52 | return QDBusVariant(cryptoLayer_->publicKey()); |
| 53 | } |
| 54 | |
| 55 | bool Helper::started() const |
| 56 | { |
| 57 | return autoExitTimer_.isActive(); |
| 58 | } |
| 59 | |
| 60 | void Helper::exit(QByteArray const &signature) |
| 61 | { |
| 62 | if (cryptoLayer_->verify({"exit"}, signature)) { |
| 63 | autoExitTimer_.stop(); |
| 64 | exitHelper(); |
| 65 | } |
| 66 | else |
| 67 | SPDLOG_DEBUG("Failed to verify 'exit' call from D-Bus"); |
| 68 | } |
| 69 | |
| 70 | void Helper::exitHelper() |
| 71 | { |
| 72 | endProcessMonitor(); |
| 73 | QTimer::singleShot(0, QCoreApplication::instance(), &QCoreApplication::quit); |
| 74 | } |
| 75 | |
| 76 | void Helper::autoExitTimeout() |
| 77 | { |
| 78 | SPDLOG_WARN("Auto exit timeout. Killing helper instance..."); |
| 79 | exitHelper(); |
| 80 | } |
| 81 | |
| 82 | bool Helper::isAuthorized(QDBusMessage const &message) const |
| 83 | { |
| 84 | auto subject = Polkit::BusNameSubject{message.service().toStdString()}; |
| 85 | return Polkit::checkAuthorizationSync(POLKIT_HELPER_ACTION, subject) == |
| 86 | Polkit::AuthResult::Yes; |
| 87 | } |
| 88 | |
| 89 | bool Helper::initCrypto(QByteArray const &appPublicKey) |
| 90 | { |
| 91 | try { |
| 92 | cryptoLayer_ = std::make_shared<CryptoLayer>(); |
| 93 | cryptoLayer_->init(); |
| 94 | cryptoLayer_->usePublicKey(appPublicKey); |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | catch (std::exception const &e) { |
| 99 | SPDLOG_DEBUG(e.what()); |
| 100 | } |
| 101 | |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | void Helper::delayAutoExit() |
| 106 | { |
| 107 | if (autoExitTimer_.isActive()) |
| 108 | autoExitTimer_.start(); |
| 109 | } |
| 110 | |
| 111 | bool Helper::initProcessMonitor() |
| 112 | { |
| 113 | bool success = false; |
| 114 | |
| 115 | try { |
| 116 | appRegistry_ = std::make_shared<AppRegistry>(); |
| 117 | processMonitor_ = std::make_unique<NLProcExecMonitor>( |
| 118 | appRegistry_, std::make_unique<ProcPIDSolver>(), |
| 119 | std::make_unique<MsgDispatcher>(cryptoLayer_, appRegistry_)); |
| 120 | |
| 121 | auto pMonInit = [&]() { processMonitor_->start(); }; |
| 122 | pMonThread_ = std::make_unique<std::thread>(pMonInit); |
| 123 | |
| 124 | success = true; |
| 125 | } |
| 126 | catch (NLProcExecSocket::BindError &e) { |
| 127 | SPDLOG_WARN("Bind socket error: {}", e.what()); |
| 128 | SPDLOG_WARN("Do you have root permissions?"); |
| 129 | } |
| 130 | catch (std::exception const &e) { |
| 131 | SPDLOG_DEBUG(e.what()); |
| 132 | } |
| 133 | |
| 134 | return success; |
| 135 | } |
| 136 | |
| 137 | void Helper::endProcessMonitor() |
| 138 | { |
| 139 | if (pMonThread_ != nullptr) { |
| 140 | processMonitor_->stop(); |
| 141 | pMonThread_->join(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | bool Helper::initMsgReceiver() |
| 146 | { |
| 147 | try { |
| 148 | msgReceiver_ = std::make_unique<MsgReceiver>( |
| 149 | cryptoLayer_, std::make_unique<SysfsWriter>()); |
| 150 | |
| 151 | return true; |
| 152 | } |
| 153 | catch (std::exception const &e) { |
| 154 | SPDLOG_DEBUG(e.what()); |
| 155 | } |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | bool initDBusForHelperService(QObject *obj) |
| 161 | { |
| 162 | QDBusConnection bus = QDBusConnection::systemBus(); |
| 163 | if (!bus.isConnected()) { |
| 164 | SPDLOG_DEBUG("Could not connect to D-Bus system bus"); |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | if (!bus.registerObject(QStringLiteral(DBUS_HELPER_PATH), obj)) { |
| 169 | SPDLOG_DEBUG("Could not register D-Bus object on path {} " |
| 170 | "using the interface {}\n.Last D-Bus error: {}", |
| 171 | DBUS_HELPER_PATH, DBUS_HELPER_INTERFACE, |
| 172 | bus.lastError().message().toStdString()); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | if (!bus.registerService(QStringLiteral(DBUS_HELPER_SERVICE))) { |
| 177 | SPDLOG_DEBUG("Could not register D-Bus service {}.\nLast D-Bus error: {}", |
| 178 | DBUS_HELPER_SERVICE, bus.lastError().message().toStdString()); |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | bool endDBusForHelperService() |
| 186 | { |
| 187 | QDBusConnection bus = QDBusConnection::systemBus(); |
| 188 | bus.unregisterObject(QStringLiteral(DBUS_HELPER_PATH)); |
| 189 | auto success = bus.unregisterService(QStringLiteral(DBUS_HELPER_SERVICE)); |
| 190 | if (!success) { |
| 191 | SPDLOG_DEBUG("D-Bus error unregistering service {}: {}", |
| 192 | DBUS_HELPER_SERVICE, bus.lastError().message().toStdString()); |
| 193 | } |
| 194 | |
| 195 | return success; |
| 196 | } |
| 197 | |
| 198 | int main(int argc, char **argv) |
| 199 | { |
| 200 | QCoreApplication app(argc, argv); |
| 201 | new Helper(&app); |
| 202 | |
| 203 | if (!initDBusForHelperService(&app)) |
| 204 | exit(1); |
| 205 | |
| 206 | app.exec(); |
| 207 | |
| 208 | if (!endDBusForHelperService()) |
| 209 | exit(1); |
| 210 | |
| 211 | return 0; |
| 212 | } |