File indexing completed on 2024-05-12 05:48:10

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0003 
0004 #include <QCoreApplication>
0005 #include <QDBusConnection>
0006 #include <QDBusConnectionInterface>
0007 #include <QDBusContext>
0008 #include <QDBusMetaType>
0009 
0010 #include <KIO/JobUiDelegateExtension>
0011 #include <KIO/JobUiDelegateFactory>
0012 
0013 #include "../dbustypes.h"
0014 #include "auth.h"
0015 #include "busobject.h"
0016 #include "chmodcommand.h"
0017 #include "chowncommand.h"
0018 #include "copycommand.h"
0019 #include "delcommand.h"
0020 #include "file.h"
0021 #include "getcommand.h"
0022 #include "listdircommand.h"
0023 #include "mkdircommand.h"
0024 #include "putcommand.h"
0025 #include "renamecommand.h"
0026 #include "statcommand.h"
0027 
0028 static QUrl stringToUrl(const QString &stringUrl)
0029 {
0030     QUrl url(stringUrl);
0031     if (url.scheme() == QLatin1String("admin")) {
0032         url.setScheme(QStringLiteral("file"));
0033     }
0034     return url;
0035 }
0036 
0037 class Helper : public QObject, protected QDBusContext
0038 {
0039     Q_OBJECT
0040     Q_CLASSINFO("D-Bus Interface", "org.kde.kio.admin")
0041 public Q_SLOTS:
0042     QDBusObjectPath listDir(const QString &stringUrl)
0043     {
0044         if (!isAuthorized()) {
0045             sendErrorReply(QDBusError::AccessDenied);
0046             return {};
0047         }
0048 
0049         static uint64_t counter = 0;
0050         counter++;
0051         Q_ASSERT(counter != 0);
0052 
0053         const QDBusObjectPath objPath(QStringLiteral("/org/kde/kio/admin/listDir/%1").arg(QString::number(counter)));
0054         auto command = new ListDirCommand(stringToUrl(stringUrl), message().service(), objPath);
0055         connection().registerObject(objPath.path(), command, QDBusConnection::ExportAllSlots);
0056         return objPath;
0057     }
0058 
0059     QDBusObjectPath stat(const QString &stringUrl)
0060     {
0061         if (!isAuthorized()) {
0062             sendErrorReply(QDBusError::AccessDenied);
0063             return {};
0064         }
0065 
0066         static uint64_t counter = 0;
0067         counter++;
0068         Q_ASSERT(counter != 0);
0069 
0070         const QDBusObjectPath objPath(QStringLiteral("/org/kde/kio/admin/stat/%1").arg(QString::number(counter)));
0071         auto command = new StatCommand(stringToUrl(stringUrl), message().service(), objPath);
0072         ;
0073         connection().registerObject(objPath.path(), command, QDBusConnection::ExportAllSlots);
0074         return objPath;
0075     }
0076 
0077     QDBusObjectPath get(const QString &stringUrl)
0078     {
0079         if (!isAuthorized()) {
0080             sendErrorReply(QDBusError::AccessDenied);
0081             return {};
0082         }
0083 
0084         static uint64_t counter = 0;
0085         counter++;
0086         Q_ASSERT(counter != 0);
0087 
0088         const QDBusObjectPath objPath(QStringLiteral("/org/kde/kio/admin/get/%1").arg(QString::number(counter)));
0089         auto command = new GetCommand(stringToUrl(stringUrl), message().service(), objPath);
0090         connection().registerObject(objPath.path(), command, QDBusConnection::ExportAllSlots);
0091         return objPath;
0092     }
0093 
0094     QDBusObjectPath put(const QString &stringUrl, int permissions, int flags)
0095     {
0096         if (!isAuthorized()) {
0097             sendErrorReply(QDBusError::AccessDenied);
0098             return {};
0099         }
0100 
0101         static uint64_t counter = 0;
0102         counter++;
0103         Q_ASSERT(counter != 0);
0104 
0105         const QDBusObjectPath objPath(QStringLiteral("/org/kde/kio/admin/put/%1").arg(QString::number(counter)));
0106         auto command = new PutCommand(stringToUrl(stringUrl), permissions, KIO::JobFlags(flags), message().service(), objPath);
0107         connection().registerObject(objPath.path(), command, QDBusConnection::ExportAllSlots);
0108         return objPath;
0109     }
0110 
0111     QDBusObjectPath copy(const QString &stringUrlSrc, const QString &stringUrlDst, int permissions, int flags)
0112     {
0113         if (!isAuthorized()) {
0114             sendErrorReply(QDBusError::AccessDenied);
0115             return {};
0116         }
0117 
0118         static uint64_t counter = 0;
0119         counter++;
0120         Q_ASSERT(counter != 0);
0121 
0122         const QDBusObjectPath objPath(QStringLiteral("/org/kde/kio/admin/copy/%1").arg(QString::number(counter)));
0123         auto command = new CopyCommand(stringToUrl(stringUrlSrc), stringToUrl(stringUrlDst), permissions, KIO::JobFlags(flags), message().service(), objPath);
0124         connection().registerObject(objPath.path(), command, QDBusConnection::ExportAllSlots);
0125         return objPath;
0126     }
0127 
0128     QDBusObjectPath del(const QString &stringUrl)
0129     {
0130         if (!isAuthorized()) {
0131             sendErrorReply(QDBusError::AccessDenied);
0132             return {};
0133         }
0134 
0135         static uint64_t counter = 0;
0136         counter++;
0137         Q_ASSERT(counter != 0);
0138 
0139         const QDBusObjectPath objPath(QStringLiteral("/org/kde/kio/admin/del/%1").arg(QString::number(counter)));
0140         auto command = new DelCommand(stringToUrl(stringUrl), message().service(), objPath);
0141         connection().registerObject(objPath.path(), command, QDBusConnection::ExportAllSlots);
0142         return objPath;
0143     }
0144 
0145     QDBusObjectPath mkdir(const QString &stringUrl, int permissions)
0146     {
0147         if (!isAuthorized()) {
0148             sendErrorReply(QDBusError::AccessDenied);
0149             return {};
0150         }
0151 
0152         static uint64_t counter = 0;
0153         counter++;
0154         Q_ASSERT(counter != 0);
0155 
0156         const QDBusObjectPath objPath(QStringLiteral("/org/kde/kio/admin/mkdir/%1").arg(QString::number(counter)));
0157         auto command = new MkdirCommand(stringToUrl(stringUrl), permissions, message().service(), objPath);
0158         connection().registerObject(objPath.path(), command, QDBusConnection::ExportAllSlots);
0159         return objPath;
0160     }
0161 
0162     QDBusObjectPath chmod(const QString &stringUrl, int permissions)
0163     {
0164         if (!isAuthorized()) {
0165             sendErrorReply(QDBusError::AccessDenied);
0166             return {};
0167         }
0168 
0169         static uint64_t counter = 0;
0170         counter++;
0171         Q_ASSERT(counter != 0);
0172 
0173         const QDBusObjectPath objPath(QStringLiteral("/org/kde/kio/admin/chmod/%1").arg(QString::number(counter)));
0174         auto command = new ChmodCommand(stringToUrl(stringUrl), permissions, message().service(), objPath);
0175         connection().registerObject(objPath.path(), command, QDBusConnection::ExportAllSlots);
0176         return objPath;
0177     }
0178 
0179     QDBusObjectPath chown(const QString &stringUrl, const QString &user, const QString &group)
0180     {
0181         if (!isAuthorized()) {
0182             sendErrorReply(QDBusError::AccessDenied);
0183             return {};
0184         }
0185 
0186         static uint64_t counter = 0;
0187         counter++;
0188         Q_ASSERT(counter != 0);
0189 
0190         const QDBusObjectPath objPath(QStringLiteral("/org/kde/kio/admin/chown/%1").arg(QString::number(counter)));
0191         auto command = new ChownCommand(stringToUrl(stringUrl), user, group, message().service(), objPath);
0192         connection().registerObject(objPath.path(), command, QDBusConnection::ExportAllSlots);
0193         return objPath;
0194     }
0195 
0196     QDBusObjectPath rename(const QString &stringUrlSrc, const QString &stringUrlDst, int flags)
0197     {
0198         if (!isAuthorized()) {
0199             sendErrorReply(QDBusError::AccessDenied);
0200             return {};
0201         }
0202 
0203         static uint64_t counter = 0;
0204         counter++;
0205         Q_ASSERT(counter != 0);
0206 
0207         const QDBusObjectPath objPath(QStringLiteral("/org/kde/kio/admin/rename/%1").arg(QString::number(counter)));
0208         auto command = new RenameCommand(stringToUrl(stringUrlSrc), stringToUrl(stringUrlDst), KIO::JobFlags(flags), message().service(), objPath);
0209         connection().registerObject(objPath.path(), command, QDBusConnection::ExportAllSlots);
0210         return objPath;
0211     }
0212 
0213     QDBusObjectPath file(const QString &stringUrl, int openMode)
0214     {
0215         if (!isAuthorized()) {
0216             sendErrorReply(QDBusError::AccessDenied);
0217             return {};
0218         }
0219 
0220         static uint64_t counter = 0;
0221         counter++;
0222         Q_ASSERT(counter != 0);
0223 
0224         const QDBusObjectPath objPath(QStringLiteral("/org/kde/kio/admin/file/%1").arg(QString::number(counter)));
0225         auto command = new File(stringToUrl(stringUrl), static_cast<QIODevice::OpenMode>(openMode), message().service(), objPath);
0226         connection().registerObject(objPath.path(), command, QDBusConnection::ExportAllSlots);
0227         return objPath;
0228     }
0229 
0230 private:
0231     bool isAuthorized()
0232     {
0233         return ::isAuthorized(this);
0234     }
0235 };
0236 
0237 int main(int argc, char *argv[])
0238 {
0239     QCoreApplication app(argc, argv);
0240     app.setQuitLockEnabled(false);
0241 
0242     qRegisterMetaType<KIO::UDSEntryList>("KIO::UDSEntryList");
0243     qDBusRegisterMetaType<KIO::UDSEntryList>();
0244 
0245     qRegisterMetaType<KIO::UDSEntry>("KIO::UDSEntry");
0246     qDBusRegisterMetaType<KIO::UDSEntry>();
0247 
0248 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0249     KIO::setDefaultJobUiDelegateFactoryV2(nullptr);
0250 #else
0251     KIO::setDefaultJobUiDelegateFactory(nullptr);
0252 #endif
0253     KIO::setDefaultJobUiDelegateExtension(nullptr);
0254 
0255     Helper helper;
0256 
0257     if (!QDBusConnection::systemBus().registerObject(QStringLiteral("/"), &helper, QDBusConnection::ExportAllSlots)) {
0258         qWarning() << "Failed to register the daemon object" << QDBusConnection::systemBus().lastError().message();
0259         return 1;
0260     }
0261     if (!QDBusConnection::systemBus().registerService(QStringLiteral("org.kde.kio.admin"))) {
0262         qWarning() << "Failed to register the service" << QDBusConnection::systemBus().lastError().message();
0263         return 1;
0264     }
0265 
0266     return app.exec();
0267 }
0268 
0269 #include "main.moc"