Changeset View
Changeset View
Standalone View
Standalone View
src/util/externalcommand.cpp
1 | /************************************************************************* | 1 | /************************************************************************* | ||
---|---|---|---|---|---|
2 | * Copyright (C) 2008 by Volker Lanz <vl@fidra.de> * | 2 | * Copyright (C) 2008 by Volker Lanz <vl@fidra.de> * | ||
3 | * Copyright (C) 2016-2018 by Andrius Štikonas <andrius@stikonas.eu> * | 3 | * Copyright (C) 2016-2018 by Andrius Štikonas <andrius@stikonas.eu> * | ||
4 | * Copyright (C) 2019 by Shubham <aryan100jangid@gmail.com> * | ||||
4 | * * | 5 | * * | ||
5 | * This program is free software; you can redistribute it and/or * | 6 | * This program is free software; you can redistribute it and/or * | ||
6 | * modify it under the terms of the GNU General Public License as * | 7 | * modify it under the terms of the GNU General Public License as * | ||
7 | * published by the Free Software Foundation; either version 3 of * | 8 | * published by the Free Software Foundation; either version 3 of * | ||
8 | * the License, or (at your option) any later version. * | 9 | * the License, or (at your option) any later version. * | ||
9 | * * | 10 | * * | ||
10 | * This program is distributed in the hope that it will be useful, * | 11 | * This program is distributed in the hope that it will be useful, * | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
13 | * GNU General Public License for more details. * | 14 | * GNU General Public License for more details. * | ||
14 | * * | 15 | * * | ||
15 | * You should have received a copy of the GNU General Public License * | 16 | * You should have received a copy of the GNU General Public License * | ||
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.* | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>.* | ||
17 | *************************************************************************/ | 18 | *************************************************************************/ | ||
18 | 19 | | |||
19 | #include "backend/corebackendmanager.h" | 20 | #include "backend/corebackendmanager.h" | ||
20 | #include "core/device.h" | 21 | #include "core/device.h" | ||
21 | #include "core/copysource.h" | 22 | #include "core/copysource.h" | ||
22 | #include "core/copytarget.h" | 23 | #include "core/copytarget.h" | ||
23 | #include "core/copytargetbytearray.h" | 24 | #include "core/copytargetbytearray.h" | ||
24 | #include "core/copysourcedevice.h" | 25 | #include "core/copysourcedevice.h" | ||
25 | #include "core/copytargetdevice.h" | 26 | #include "core/copytargetdevice.h" | ||
26 | #include "util/globallog.h" | 27 | #include "util/globallog.h" | ||
27 | #include "util/externalcommand.h" | 28 | #include "util/externalcommand.h" | ||
29 | #include "util/externalcommand_polkitbackend.h" | ||||
30 | #include "util/externalcommandhelper.h" | ||||
28 | #include "util/report.h" | 31 | #include "util/report.h" | ||
29 | | ||||
30 | #include "externalcommandhelper_interface.h" | 32 | #include "externalcommandhelper_interface.h" | ||
31 | 33 | | |||
32 | #include <QCryptographicHash> | 34 | #include <QtDBus> | ||
33 | #include <QDBusConnection> | | |||
34 | #include <QDBusInterface> | | |||
35 | #include <QDBusReply> | | |||
36 | #include <QEventLoop> | 35 | #include <QEventLoop> | ||
37 | #include <QtGlobal> | | |||
38 | #include <QStandardPaths> | 36 | #include <QStandardPaths> | ||
39 | #include <QString> | 37 | #include <QString> | ||
40 | #include <QStringList> | 38 | #include <QStringList> | ||
41 | #include <QTimer> | | |||
42 | #include <QThread> | 39 | #include <QThread> | ||
40 | #include <QTimer> | ||||
43 | #include <QVariant> | 41 | #include <QVariant> | ||
44 | 42 | | |||
45 | #include <KAuth> | | |||
46 | #include <KJob> | 43 | #include <KJob> | ||
47 | #include <KLocalizedString> | 44 | #include <KLocalizedString> | ||
48 | 45 | | |||
46 | #include <PolkitQt1/Authority> | ||||
47 | | ||||
49 | struct ExternalCommandPrivate | 48 | struct ExternalCommandPrivate | ||
50 | { | 49 | { | ||
51 | Report *m_Report; | 50 | Report *m_Report; | ||
52 | QString m_Command; | 51 | QString m_Command; | ||
53 | QStringList m_Args; | 52 | QStringList m_Args; | ||
54 | int m_ExitCode; | 53 | int m_ExitCode; | ||
55 | QByteArray m_Output; | 54 | QByteArray m_Output; | ||
56 | QByteArray m_Input; | 55 | QByteArray m_Input; | ||
57 | DBusThread *m_thread; | 56 | DBusThread *m_thread; | ||
58 | QProcess::ProcessChannelMode processChannelMode; | 57 | QProcess::ProcessChannelMode processChannelMode; | ||
59 | }; | 58 | }; | ||
60 | 59 | | |||
61 | KAuth::ExecuteJob* ExternalCommand::m_job; | | |||
62 | bool ExternalCommand::helperStarted = false; | 60 | bool ExternalCommand::helperStarted = false; | ||
63 | QWidget* ExternalCommand::parent; | 61 | QWidget* ExternalCommand::parent; | ||
64 | 62 | Auth::PolkitQt1Backend* ExternalCommand::m_authJob; | |||
65 | 63 | | |||
66 | /** Creates a new ExternalCommand instance without Report. | 64 | /** Creates a new ExternalCommand instance without Report. | ||
67 | @param cmd the command to run | 65 | @param cmd the command to run | ||
68 | @param args the arguments to pass to the command | 66 | @param args the arguments to pass to the command | ||
69 | */ | 67 | */ | ||
70 | ExternalCommand::ExternalCommand(const QString& cmd, const QStringList& args, const QProcess::ProcessChannelMode processChannelMode) : | 68 | ExternalCommand::ExternalCommand(const QString& cmd, const QStringList& args, const QProcess::ProcessChannelMode processChannelMode) : | ||
71 | d(std::make_unique<ExternalCommandPrivate>()) | 69 | d(std::make_unique<ExternalCommandPrivate>()) | ||
72 | { | 70 | { | ||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Line(s) | 119 | { | |||
133 | 131 | | |||
134 | if ( qEnvironmentVariableIsSet( "KPMCORE_DEBUG" )) | 132 | if ( qEnvironmentVariableIsSet( "KPMCORE_DEBUG" )) | ||
135 | qDebug() << xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" "))); | 133 | qDebug() << xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" "))); | ||
136 | 134 | | |||
137 | QString cmd = QStandardPaths::findExecutable(command()); | 135 | QString cmd = QStandardPaths::findExecutable(command()); | ||
138 | if (cmd.isEmpty()) | 136 | if (cmd.isEmpty()) | ||
139 | cmd = QStandardPaths::findExecutable(command(), { QStringLiteral("/sbin/"), QStringLiteral("/usr/sbin/"), QStringLiteral("/usr/local/sbin/") }); | 137 | cmd = QStandardPaths::findExecutable(command(), { QStringLiteral("/sbin/"), QStringLiteral("/usr/sbin/"), QStringLiteral("/usr/local/sbin/") }); | ||
140 | 138 | | |||
141 | auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), | 139 | auto interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), | ||
142 | QStringLiteral("/Helper"), QDBusConnection::systemBus(), this); | 140 | QStringLiteral("/Helper"), QDBusConnection::systemBus(), this); | ||
143 | 141 | | |||
144 | interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days | 142 | interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days | ||
145 | 143 | | |||
146 | bool rval = false; | 144 | bool rval = false; | ||
147 | 145 | | |||
148 | QDBusPendingCall pcall = interface->start(cmd, args(), d->m_Input, d->processChannelMode); | 146 | QDBusPendingCall pcall = interface->start(cmd, args(), d->m_Input, d->processChannelMode); | ||
149 | 147 | | |||
Show All 27 Lines | 174 | { | |||
177 | bool rval = true; | 175 | bool rval = true; | ||
178 | const qint64 blockSize = 10 * 1024 * 1024; // number of bytes per block to copy | 176 | const qint64 blockSize = 10 * 1024 * 1024; // number of bytes per block to copy | ||
179 | 177 | | |||
180 | if (!QDBusConnection::systemBus().isConnected()) { | 178 | if (!QDBusConnection::systemBus().isConnected()) { | ||
181 | qWarning() << QDBusConnection::systemBus().lastError().message(); | 179 | qWarning() << QDBusConnection::systemBus().lastError().message(); | ||
182 | return false; | 180 | return false; | ||
183 | } | 181 | } | ||
184 | 182 | | |||
185 | // TODO KF6:Use new signal-slot syntax | 183 | auto interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), | ||
186 | connect(m_job, SIGNAL(percent(KJob*, unsigned long)), this, SLOT(emitProgress(KJob*, unsigned long))); | | |||
187 | connect(m_job, &KAuth::ExecuteJob::newData, this, &ExternalCommand::emitReport); | | |||
188 | | ||||
189 | auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), | | |||
190 | QStringLiteral("/Helper"), QDBusConnection::systemBus(), this); | 184 | QStringLiteral("/Helper"), QDBusConnection::systemBus(), this); | ||
191 | interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days | 185 | interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days | ||
192 | 186 | | |||
193 | QDBusPendingCall pcall = interface->copyblocks(source.path(), source.firstByte(), source.length(), | 187 | QDBusPendingCall pcall = interface->copyblocks(source.path(), source.firstByte(), source.length(), | ||
194 | target.path(), target.firstByte(), blockSize); | 188 | target.path(), target.firstByte(), blockSize); | ||
195 | 189 | | |||
196 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this); | 190 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this); | ||
197 | QEventLoop loop; | 191 | QEventLoop loop; | ||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Line(s) | 262 | /** Runs the command. | |||
269 | @param timeout timeout to use for waiting when starting and when waiting for the process to finish | 263 | @param timeout timeout to use for waiting when starting and when waiting for the process to finish | ||
270 | @return true on success | 264 | @return true on success | ||
271 | */ | 265 | */ | ||
272 | bool ExternalCommand::run(int timeout) | 266 | bool ExternalCommand::run(int timeout) | ||
273 | { | 267 | { | ||
274 | return start(timeout) /* && exitStatus() == 0*/; | 268 | return start(timeout) /* && exitStatus() == 0*/; | ||
275 | } | 269 | } | ||
276 | 270 | | |||
277 | void ExternalCommand::onReadOutput() | 271 | //void ExternalCommand::onReadOutput() | ||
278 | { | 272 | //{ | ||
279 | // const QByteArray s = readAllStandardOutput(); | 273 | // const QByteArray s = readAllStandardOutput(); | ||
280 | // | 274 | // | ||
281 | // if(m_Output.length() > 10*1024*1024) { // prevent memory overflow for badly corrupted file systems | 275 | // if(m_Output.length() > 10*1024*1024) { // prevent memory overflow for badly corrupted file systems | ||
282 | // if (report()) | 276 | // if (report()) | ||
283 | // report()->line() << xi18nc("@info:status", "(Command is printing too much output)"); | 277 | // report()->line() << xi18nc("@info:status", "(Command is printing too much output)"); | ||
284 | // return; | 278 | // return; | ||
285 | // } | 279 | // } | ||
286 | // | 280 | // | ||
287 | // m_Output += s; | 281 | // m_Output += s; | ||
288 | // | 282 | // | ||
289 | // if (report()) | 283 | // if (report()) | ||
290 | // *report() << QString::fromLocal8Bit(s); | 284 | // *report() << QString::fromLocal8Bit(s); | ||
291 | } | 285 | //} | ||
292 | 286 | | |||
293 | void ExternalCommand::setCommand(const QString& cmd) | 287 | void ExternalCommand::setCommand(const QString& cmd) | ||
294 | { | 288 | { | ||
295 | d->m_Command = cmd; | 289 | d->m_Command = cmd; | ||
296 | } | 290 | } | ||
297 | 291 | | |||
298 | const QString& ExternalCommand::command() const | 292 | const QString& ExternalCommand::command() const | ||
299 | { | 293 | { | ||
Show All 35 Lines | 328 | { | |||
335 | return d->m_Report; | 329 | return d->m_Report; | ||
336 | } | 330 | } | ||
337 | 331 | | |||
338 | void ExternalCommand::setExitCode(int i) | 332 | void ExternalCommand::setExitCode(int i) | ||
339 | { | 333 | { | ||
340 | d->m_ExitCode = i; | 334 | d->m_ExitCode = i; | ||
341 | } | 335 | } | ||
342 | 336 | | |||
343 | /**< Dummy function for QTimer when needed. */ | 337 | // Dummy function for QTimer | ||
344 | void ExternalCommand::quit() | 338 | void ExternalCommand::quit() | ||
345 | { | 339 | { | ||
346 | 340 | | |||
347 | } | 341 | } | ||
348 | 342 | | |||
349 | bool ExternalCommand::startHelper() | 343 | bool ExternalCommand::startHelper() | ||
350 | { | 344 | { | ||
351 | if (!QDBusConnection::systemBus().isConnected()) { | 345 | if (!QDBusConnection::systemBus().isConnected()) { | ||
352 | qWarning() << QDBusConnection::systemBus().lastError().message(); | 346 | qWarning() << QDBusConnection::systemBus().lastError().message(); | ||
353 | return false; | 347 | return false; | ||
354 | } | 348 | } | ||
355 | 349 | | |||
356 | QDBusInterface iface(QStringLiteral("org.kde.kpmcore.helperinterface"), QStringLiteral("/Helper"), QStringLiteral("org.kde.kpmcore.externalcommand"), QDBusConnection::systemBus()); | 350 | QDBusInterface iface(QStringLiteral("org.kde.kpmcore.helperinterface"), QStringLiteral("/Helper"), QStringLiteral("org.kde.kpmcore.externalcommand"), QDBusConnection::systemBus()); | ||
351 | | ||||
357 | if (iface.isValid()) { | 352 | if (iface.isValid()) { | ||
358 | exit(0); | 353 | exit(0); | ||
359 | } | 354 | } | ||
360 | 355 | | |||
361 | d->m_thread = new DBusThread; | 356 | d->m_thread = new DBusThread; | ||
362 | d->m_thread->start(); | 357 | d->m_thread->start(); | ||
363 | 358 | | |||
364 | KAuth::Action action = KAuth::Action(QStringLiteral("org.kde.kpmcore.externalcommand.init")); | 359 | ////////////////////////////////////// | ||
365 | action.setHelperId(QStringLiteral("org.kde.kpmcore.externalcommand")); | 360 | // Authorize using Polkit backend /// | ||
366 | action.setTimeout(10 * 24 * 3600 * 1000); // 10 days | 361 | //////////////////////////////////// | ||
367 | action.setParentWidget(parent); | 362 | | ||
368 | QVariantMap arguments; | 363 | // initialize KDE Polkit daemon | ||
369 | action.setArguments(arguments); | 364 | m_authJob->initPolkitAgent(QStringLiteral("org.kde.kpmcore.externalcommand.init"), parent); | ||
370 | m_job = action.execute(); | 365 | | ||
371 | m_job->start(); | 366 | bool isActionAuthorized = m_authJob->authorizeAction(QStringLiteral("org.kde.kpmcore.externalcommand.init"), m_authJob->callerID()); | ||
367 | | ||||
368 | auto authResult = m_authJob->actionStatus(QStringLiteral("org.kde.kpmcore.externalcommand.init"), m_authJob->callerID()); | ||||
369 | | ||||
372 | 370 | | |||
373 | // Wait until ExternalCommand Helper is ready (helper sends newData signal just before it enters event loop) | 371 | // Wait until ExternalCommand Helper is ready and sends signal | ||
374 | QEventLoop loop; | 372 | QEventLoop loop; | ||
375 | auto exitLoop = [&] () { loop.exit(); }; | 373 | auto exitLoop = [&] () { loop.exit(); }; | ||
376 | auto conn = QObject::connect(m_job, &KAuth::ExecuteJob::newData, exitLoop); | 374 | | ||
377 | QObject::connect(m_job, &KJob::finished, [=] () { if(m_job->error()) exitLoop(); } ); | | |||
378 | loop.exec(); | 375 | loop.exec(); | ||
379 | QObject::disconnect(conn); | 376 | | ||
377 | if (!isActionAuthorized || authResult == PolkitQt1::Authority::No || authResult == PolkitQt1::Authority::Unknown) { | ||||
378 | qDebug() << "Unable to obtain Administrative privileges, the action can not be executed!!"; | ||||
379 | } | ||||
380 | 380 | | |||
381 | helperStarted = true; | 381 | helperStarted = true; | ||
382 | return true; | 382 | return true; | ||
383 | } | 383 | } | ||
384 | 384 | | |||
385 | void ExternalCommand::stopHelper() | 385 | void ExternalCommand::stopHelper() | ||
386 | { | 386 | { | ||
387 | auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), | 387 | auto interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), | ||
388 | QStringLiteral("/Helper"), QDBusConnection::systemBus()); | 388 | QStringLiteral("/Helper"), QDBusConnection::systemBus()); | ||
389 | interface->exit(); | 389 | interface->exit(); | ||
390 | | ||||
391 | } | 390 | } | ||
392 | 391 | | |||
393 | void DBusThread::run() | 392 | void DBusThread::run() | ||
394 | { | 393 | { | ||
395 | if (!QDBusConnection::systemBus().registerService(QStringLiteral("org.kde.kpmcore.applicationinterface")) || | 394 | if (!QDBusConnection::systemBus().registerService(QStringLiteral("org.kde.kpmcore.applicationinterface")) || | ||
396 | !QDBusConnection::systemBus().registerObject(QStringLiteral("/Application"), this, QDBusConnection::ExportAllSlots)) { | 395 | !QDBusConnection::systemBus().registerObject(QStringLiteral("/Application"), this, QDBusConnection::ExportAllSlots)) { | ||
397 | qWarning() << QDBusConnection::systemBus().lastError().message(); | 396 | qWarning() << QDBusConnection::systemBus().lastError().message(); | ||
398 | return; | 397 | return; | ||
399 | } | 398 | } | ||
400 | 399 | | |||
401 | QEventLoop loop; | 400 | QEventLoop loop; | ||
402 | loop.exec(); | 401 | loop.exec(); | ||
403 | } | 402 | } |