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 | 32 | | |||
30 | #include "externalcommandhelper_interface.h" | 33 | #include "externalcommandhelper_interface.h" | ||
31 | 34 | | |||
32 | #include <QCryptographicHash> | 35 | #include <QtDBus> | ||
33 | #include <QDBusConnection> | | |||
34 | #include <QDBusInterface> | | |||
35 | #include <QDBusReply> | | |||
36 | #include <QEventLoop> | 36 | #include <QEventLoop> | ||
37 | #include <QtGlobal> | | |||
38 | #include <QStandardPaths> | 37 | #include <QStandardPaths> | ||
39 | #include <QString> | 38 | #include <QString> | ||
40 | #include <QStringList> | 39 | #include <QStringList> | ||
41 | #include <QTimer> | | |||
42 | #include <QThread> | 40 | #include <QThread> | ||
41 | #include <QTimer> | ||||
43 | #include <QVariant> | 42 | #include <QVariant> | ||
44 | 43 | | |||
45 | #include <KAuth> | | |||
46 | #include <KJob> | 44 | #include <KJob> | ||
47 | #include <KLocalizedString> | 45 | #include <KLocalizedString> | ||
48 | 46 | | |||
47 | #include <PolkitQt1/Authority> | ||||
48 | | ||||
49 | struct ExternalCommandPrivate | 49 | struct ExternalCommandPrivate | ||
50 | { | 50 | { | ||
51 | Report *m_Report; | 51 | Report *m_Report; | ||
52 | QString m_Command; | 52 | QString m_Command; | ||
53 | QStringList m_Args; | 53 | QStringList m_Args; | ||
54 | int m_ExitCode; | 54 | int m_ExitCode; | ||
55 | QByteArray m_Output; | 55 | QByteArray m_Output; | ||
56 | QByteArray m_Input; | 56 | QByteArray m_Input; | ||
57 | DBusThread *m_thread; | 57 | DBusThread *m_thread; | ||
58 | QProcess::ProcessChannelMode processChannelMode; | 58 | QProcess::ProcessChannelMode processChannelMode; | ||
59 | }; | 59 | }; | ||
60 | 60 | | |||
61 | KAuth::ExecuteJob* ExternalCommand::m_job; | 61 | | ||
62 | bool ExternalCommand::helperStarted = false; | 62 | bool ExternalCommand::helperStarted = false; | ||
63 | QWidget* ExternalCommand::parent; | 63 | QWidget* ExternalCommand::parent; | ||
64 | 64 | Auth::PolkitQt1Backend* ExternalCommand::m_authJob; | |||
65 | 65 | | |||
66 | /** Creates a new ExternalCommand instance without Report. | 66 | /** Creates a new ExternalCommand instance without Report. | ||
67 | @param cmd the command to run | 67 | @param cmd the command to run | ||
68 | @param args the arguments to pass to the command | 68 | @param args the arguments to pass to the command | ||
69 | */ | 69 | */ | ||
70 | ExternalCommand::ExternalCommand(const QString& cmd, const QStringList& args, const QProcess::ProcessChannelMode processChannelMode) : | 70 | ExternalCommand::ExternalCommand(const QString& cmd, const QStringList& args, const QProcess::ProcessChannelMode processChannelMode) : | ||
71 | d(std::make_unique<ExternalCommandPrivate>()) | 71 | d(std::make_unique<ExternalCommandPrivate>()) | ||
72 | { | 72 | { | ||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Line(s) | 121 | { | |||
133 | 133 | | |||
134 | if ( qEnvironmentVariableIsSet( "KPMCORE_DEBUG" )) | 134 | if ( qEnvironmentVariableIsSet( "KPMCORE_DEBUG" )) | ||
135 | qDebug() << xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" "))); | 135 | qDebug() << xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" "))); | ||
136 | 136 | | |||
137 | QString cmd = QStandardPaths::findExecutable(command()); | 137 | QString cmd = QStandardPaths::findExecutable(command()); | ||
138 | if (cmd.isEmpty()) | 138 | if (cmd.isEmpty()) | ||
139 | cmd = QStandardPaths::findExecutable(command(), { QStringLiteral("/sbin/"), QStringLiteral("/usr/sbin/"), QStringLiteral("/usr/local/sbin/") }); | 139 | cmd = QStandardPaths::findExecutable(command(), { QStringLiteral("/sbin/"), QStringLiteral("/usr/sbin/"), QStringLiteral("/usr/local/sbin/") }); | ||
140 | 140 | | |||
141 | auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), | 141 | auto interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), | ||
142 | QStringLiteral("/Helper"), QDBusConnection::systemBus(), this); | 142 | QStringLiteral("/Helper"), QDBusConnection::systemBus(), this); | ||
143 | 143 | | |||
144 | interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days | 144 | interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days | ||
145 | 145 | | |||
146 | bool rval = false; | 146 | bool rval = false; | ||
147 | 147 | | |||
148 | QDBusPendingCall pcall = interface->start(cmd, args(), d->m_Input, d->processChannelMode); | 148 | QDBusPendingCall pcall = interface->start(cmd, args(), d->m_Input, d->processChannelMode); | ||
149 | 149 | | |||
Show All 27 Lines | 176 | { | |||
177 | bool rval = true; | 177 | bool rval = true; | ||
178 | const qint64 blockSize = 10 * 1024 * 1024; // number of bytes per block to copy | 178 | const qint64 blockSize = 10 * 1024 * 1024; // number of bytes per block to copy | ||
179 | 179 | | |||
180 | if (!QDBusConnection::systemBus().isConnected()) { | 180 | if (!QDBusConnection::systemBus().isConnected()) { | ||
181 | qWarning() << QDBusConnection::systemBus().lastError().message(); | 181 | qWarning() << QDBusConnection::systemBus().lastError().message(); | ||
182 | return false; | 182 | return false; | ||
183 | } | 183 | } | ||
184 | 184 | | |||
185 | // TODO KF6:Use new signal-slot syntax | 185 | 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); | 186 | QStringLiteral("/Helper"), QDBusConnection::systemBus(), this); | ||
191 | interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days | 187 | interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days | ||
192 | 188 | | |||
193 | QDBusPendingCall pcall = interface->copyblocks(source.path(), source.firstByte(), source.length(), | 189 | QDBusPendingCall pcall = interface->copyblocks(source.path(), source.firstByte(), source.length(), | ||
194 | target.path(), target.firstByte(), blockSize); | 190 | target.path(), target.firstByte(), blockSize); | ||
195 | 191 | | |||
196 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this); | 192 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this); | ||
197 | QEventLoop loop; | 193 | QEventLoop loop; | ||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Line(s) | 264 | /** Runs the command. | |||
269 | @param timeout timeout to use for waiting when starting and when waiting for the process to finish | 265 | @param timeout timeout to use for waiting when starting and when waiting for the process to finish | ||
270 | @return true on success | 266 | @return true on success | ||
271 | */ | 267 | */ | ||
272 | bool ExternalCommand::run(int timeout) | 268 | bool ExternalCommand::run(int timeout) | ||
273 | { | 269 | { | ||
274 | return start(timeout) /* && exitStatus() == 0*/; | 270 | return start(timeout) /* && exitStatus() == 0*/; | ||
275 | } | 271 | } | ||
276 | 272 | | |||
277 | void ExternalCommand::onReadOutput() | 273 | //void ExternalCommand::onReadOutput() | ||
278 | { | 274 | //{ | ||
279 | // const QByteArray s = readAllStandardOutput(); | 275 | // const QByteArray s = readAllStandardOutput(); | ||
280 | // | 276 | // | ||
281 | // if(m_Output.length() > 10*1024*1024) { // prevent memory overflow for badly corrupted file systems | 277 | // if(m_Output.length() > 10*1024*1024) { // prevent memory overflow for badly corrupted file systems | ||
282 | // if (report()) | 278 | // if (report()) | ||
283 | // report()->line() << xi18nc("@info:status", "(Command is printing too much output)"); | 279 | // report()->line() << xi18nc("@info:status", "(Command is printing too much output)"); | ||
284 | // return; | 280 | // return; | ||
285 | // } | 281 | // } | ||
286 | // | 282 | // | ||
287 | // m_Output += s; | 283 | // m_Output += s; | ||
288 | // | 284 | // | ||
289 | // if (report()) | 285 | // if (report()) | ||
290 | // *report() << QString::fromLocal8Bit(s); | 286 | // *report() << QString::fromLocal8Bit(s); | ||
291 | } | 287 | //} | ||
292 | 288 | | |||
293 | void ExternalCommand::setCommand(const QString& cmd) | 289 | void ExternalCommand::setCommand(const QString& cmd) | ||
294 | { | 290 | { | ||
295 | d->m_Command = cmd; | 291 | d->m_Command = cmd; | ||
296 | } | 292 | } | ||
297 | 293 | | |||
298 | const QString& ExternalCommand::command() const | 294 | const QString& ExternalCommand::command() const | ||
299 | { | 295 | { | ||
Show All 35 Lines | 330 | { | |||
335 | return d->m_Report; | 331 | return d->m_Report; | ||
336 | } | 332 | } | ||
337 | 333 | | |||
338 | void ExternalCommand::setExitCode(int i) | 334 | void ExternalCommand::setExitCode(int i) | ||
339 | { | 335 | { | ||
340 | d->m_ExitCode = i; | 336 | d->m_ExitCode = i; | ||
341 | } | 337 | } | ||
342 | 338 | | |||
343 | /**< Dummy function for QTimer when needed. */ | 339 | // Dummy function for QTimer | ||
344 | void ExternalCommand::quit() | 340 | void ExternalCommand::quit() | ||
345 | { | 341 | { | ||
346 | 342 | | |||
347 | } | 343 | } | ||
348 | 344 | | |||
349 | bool ExternalCommand::startHelper() | 345 | bool ExternalCommand::startHelper() | ||
350 | { | 346 | { | ||
351 | if (!QDBusConnection::systemBus().isConnected()) { | 347 | if (!QDBusConnection::systemBus().isConnected()) { | ||
352 | qWarning() << QDBusConnection::systemBus().lastError().message(); | 348 | qWarning() << QDBusConnection::systemBus().lastError().message(); | ||
353 | return false; | 349 | return false; | ||
354 | } | 350 | } | ||
355 | 351 | | |||
356 | QDBusInterface iface(QStringLiteral("org.kde.kpmcore.helperinterface"), QStringLiteral("/Helper"), QStringLiteral("org.kde.kpmcore.externalcommand"), QDBusConnection::systemBus()); | 352 | QDBusInterface iface(QStringLiteral("org.kde.kpmcore.helperinterface"), QStringLiteral("/Helper"), QStringLiteral("org.kde.kpmcore.externalcommand"), QDBusConnection::systemBus()); | ||
353 | | ||||
357 | if (iface.isValid()) { | 354 | if (iface.isValid()) { | ||
358 | exit(0); | 355 | exit(0); | ||
359 | } | 356 | } | ||
360 | 357 | | |||
361 | d->m_thread = new DBusThread; | 358 | d->m_thread = new DBusThread; | ||
362 | d->m_thread->start(); | 359 | d->m_thread->start(); | ||
363 | 360 | | |||
364 | KAuth::Action action = KAuth::Action(QStringLiteral("org.kde.kpmcore.externalcommand.init")); | 361 | ////////////////////////////////////// | ||
365 | action.setHelperId(QStringLiteral("org.kde.kpmcore.externalcommand")); | 362 | // Authorize using Polkit backend /// | ||
366 | action.setTimeout(10 * 24 * 3600 * 1000); // 10 days | 363 | //////////////////////////////////// | ||
367 | action.setParentWidget(parent); | 364 | | ||
368 | QVariantMap arguments; | 365 | // initialize KDE Polkit daemon | ||
369 | action.setArguments(arguments); | 366 | m_authJob->initPolkitAgent(QStringLiteral("org.kde.kpmcore.externalcommand.init"), parent); | ||
370 | m_job = action.execute(); | 367 | | ||
371 | m_job->start(); | 368 | bool isActionAuthorized = m_authJob->authorizeAction(QStringLiteral("org.kde.kpmcore.externalcommand.init"), m_authJob->callerID()); | ||
372 | 369 | | |||
373 | // Wait until ExternalCommand Helper is ready (helper sends newData signal just before it enters event loop) | 370 | auto authResult = m_authJob->actionStatus(QStringLiteral("org.kde.kpmcore.externalcommand.init"), m_authJob->callerID()); | ||
371 | | ||||
372 | // Wait until ExternalCommand Helper is ready and sends signal(Connect to newData signal) | ||||
374 | QEventLoop loop; | 373 | QEventLoop loop; | ||
375 | auto exitLoop = [&] () { loop.exit(); }; | 374 | auto exitLoop = [&] () { loop.exit(); }; | ||
376 | auto conn = QObject::connect(m_job, &KAuth::ExecuteJob::newData, exitLoop); | 375 | | ||
377 | QObject::connect(m_job, &KJob::finished, [=] () { if(m_job->error()) exitLoop(); } ); | 376 | ExternalCommand cmd; | ||
377 | auto conn = QObject::connect(&cmd, &ExternalCommand::newData, exitLoop); | ||||
stikonas: Fix this, this will give you infinite recursion. | |||||
378 | | ||||
378 | loop.exec(); | 379 | loop.exec(); | ||
380 | | ||||
379 | QObject::disconnect(conn); | 381 | QObject::disconnect(conn); | ||
380 | 382 | | |||
383 | if (!isActionAuthorized || authResult == PolkitQt1::Authority::No || authResult == PolkitQt1::Authority::Unknown) { | ||||
384 | qDebug() << "Unable to obtain Administrative privileges, the action can not be executed!!"; | ||||
385 | } | ||||
386 | | ||||
381 | helperStarted = true; | 387 | helperStarted = true; | ||
382 | return true; | 388 | return true; | ||
383 | } | 389 | } | ||
384 | 390 | | |||
385 | void ExternalCommand::stopHelper() | 391 | void ExternalCommand::stopHelper() | ||
386 | { | 392 | { | ||
387 | auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), | 393 | auto interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), | ||
388 | QStringLiteral("/Helper"), QDBusConnection::systemBus()); | 394 | QStringLiteral("/Helper"), QDBusConnection::systemBus()); | ||
389 | interface->exit(); | 395 | interface->exit(); | ||
390 | 396 | | |||
391 | } | 397 | } | ||
392 | 398 | | |||
399 | void ExternalCommand::emitNewData(int percent) | ||||
400 | { | ||||
401 | Q_UNUSED(percent) | ||||
402 | emit newData(); | ||||
403 | } | ||||
404 | | ||||
405 | void ExternalCommand::emitNewData(QString message) | ||||
406 | { | ||||
407 | Q_UNUSED(message) | ||||
408 | emit newData(); | ||||
409 | } | ||||
410 | | ||||
393 | void DBusThread::run() | 411 | void DBusThread::run() | ||
394 | { | 412 | { | ||
395 | if (!QDBusConnection::systemBus().registerService(QStringLiteral("org.kde.kpmcore.applicationinterface")) || | 413 | if (!QDBusConnection::systemBus().registerService(QStringLiteral("org.kde.kpmcore.applicationinterface")) || | ||
396 | !QDBusConnection::systemBus().registerObject(QStringLiteral("/Application"), this, QDBusConnection::ExportAllSlots)) { | 414 | !QDBusConnection::systemBus().registerObject(QStringLiteral("/Application"), this, QDBusConnection::ExportAllSlots)) { | ||
397 | qWarning() << QDBusConnection::systemBus().lastError().message(); | 415 | qWarning() << QDBusConnection::systemBus().lastError().message(); | ||
398 | return; | 416 | return; | ||
399 | } | 417 | } | ||
400 | 418 | | |||
401 | QEventLoop loop; | 419 | QEventLoop loop; | ||
402 | loop.exec(); | 420 | loop.exec(); | ||
403 | } | 421 | } |
Fix this, this will give you infinite recursion.