Index: src/core/slavebase.h =================================================================== --- src/core/slavebase.h +++ src/core/slavebase.h @@ -935,6 +935,11 @@ */ int waitForHostInfo(QHostInfo &info); + /** + * Checks with job if privilege operation is allowed. + */ + bool isPrivilegeOperationAllowed(); + protected: /** * Name of the protocol supported by this slave Index: src/core/slavebase.cpp =================================================================== --- src/core/slavebase.cpp +++ src/core/slavebase.cpp @@ -1457,3 +1457,11 @@ return result; } + +bool SlaveBase::isPrivilegeOperationAllowed() +{ + QByteArray buffer = "0"; + send(MSG_PRIVILEGE_EXEC); + waitForAnswer(MSG_PRIVILEGE_EXEC, 0, buffer); + return buffer.toInt(); +} Index: src/core/slaveinterface.h =================================================================== --- src/core/slaveinterface.h +++ src/core/slaveinterface.h @@ -84,7 +84,8 @@ MSG_DEL_AUTH_KEY, ///< @deprecated MSG_OPENED, MSG_WRITTEN, - MSG_HOST_INFO_REQ + MSG_HOST_INFO_REQ, + MSG_PRIVILEGE_EXEC // add new ones here once a release is done, to avoid breaking binary compatibility }; @@ -144,6 +145,8 @@ void open(); void written(KIO::filesize_t); + void privilegeOperationRequested(); + /////////// // Info sent by the slave ////////// Index: src/core/slaveinterface.cpp =================================================================== --- src/core/slaveinterface.cpp +++ src/core/slaveinterface.cpp @@ -319,6 +319,9 @@ HostInfo::lookupHost(hostName, this, SLOT(slotHostInfo(QHostInfo))); break; } + case MSG_PRIVILEGE_EXEC: + emit privilegeOperationRequested(); + break; default: qCWarning(KIO_CORE) << "Slave sends unknown command (" << _cmd << "), dropping slave"; return false; Index: src/ioslaves/file/file.h =================================================================== --- src/ioslaves/file/file.h +++ src/ioslaves/file/file.h @@ -39,6 +39,8 @@ #include #endif +#include "file_p.h" + #include Q_DECLARE_LOGGING_CATEGORY(KIO_FILE) @@ -100,6 +102,9 @@ void fileSystemFreeSpace(const QUrl &url); // KF6 TODO: Turn into virtual method in SlaveBase + bool execWithElevatedPrivilege(ActionType action, const QVariant &arg1, + const QVariant &arg2 = QVariant(), + const QVariant &arg3 = QVariant()); private: mutable QHash mUsercache; mutable QHash mGroupcache; Index: src/ioslaves/file/file_p.h =================================================================== --- /dev/null +++ src/ioslaves/file/file_p.h @@ -0,0 +1,37 @@ +/*** + Copyright (C) 2017 by Chinmoy Ranjan Pradhan + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +***/ + +#ifndef FILE_P_H +#define FILE_P_H + +enum ActionType { + CHMOD = 1, + CHOWN, + DEL, + MKDIR, + OPEN, + OPENDIR, + RENAME, + RMDIR, + SYMLINK, + UTIME, +}; + +#endif Index: src/ioslaves/file/file_unix.cpp =================================================================== --- src/ioslaves/file/file_unix.cpp +++ src/ioslaves/file/file_unix.cpp @@ -39,6 +39,8 @@ #include #include +#include + //sendfile has different semantics in different platforms #if defined HAVE_SENDFILE && defined Q_OS_LINUX #define USE_SENDFILE 1 @@ -650,3 +652,40 @@ finished(); } + +bool FileProtocol::execWithElevatedPrivilege(ActionType action, const QVariant &arg1, + const QVariant &arg2, const QVariant &arg3) +{ + if (!(errno == EACCES || errno == EPERM) + || !isPrivilegeOperationAllowed()) { + return false; + } + + QByteArray helperArgs; + QDataStream out(&helperArgs, QIODevice::WriteOnly); + out << action << arg1 << arg2 << arg3; + + QVariantMap argv; + argv.insert(QStringLiteral("arguments"), helperArgs); + + KAuth::Action execAction(QStringLiteral("org.kde.kio.file.exec")); + execAction.setHelperId(QStringLiteral("org.kde.kio.file")); + execAction.setArguments(argv); + + // if we are unit testing let's pretend to execute the action. + if (metaData(QStringLiteral("UnitTesting")) == QLatin1String("true")) { + const QString metaData = execAction.name() + "," + + QString::number(execAction.isValid()) + "," + + execAction.helperId() + "," + + QString::number(execAction.status()) + ","; + setMetaData(QStringLiteral("TestData"), metaData); + return true; + } + + auto reply = execAction.execute(); + if (reply->exec()) { + return true; + } + + return false; +} Index: src/ioslaves/file/file_win.cpp =================================================================== --- src/ioslaves/file/file_win.cpp +++ src/ioslaves/file/file_win.cpp @@ -374,3 +374,9 @@ finished(); } + +bool FileProtocol::execWithElevatedPrivilege(int, ActionType, const QVariant &, + const QVariant &, const QVariant &) +{ + return false; +}