2022-09-25 17:41:18 +03:00
|
|
|
/* This file is part of the KDE libraries
|
|
|
|
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kauthorization.h"
|
2022-12-11 10:18:46 +01:00
|
|
|
#include "klocale.h"
|
2023-09-08 02:59:47 +03:00
|
|
|
#include "klockfile.h"
|
|
|
|
#include "kglobal.h"
|
|
|
|
#include "kstandarddirs.h"
|
2022-12-11 10:18:46 +01:00
|
|
|
#include "kdebug.h"
|
2022-09-25 17:41:18 +03:00
|
|
|
|
2023-09-08 02:59:47 +03:00
|
|
|
#include <QDir>
|
2022-09-25 17:41:18 +03:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QDBusAbstractAdaptor>
|
|
|
|
#include <QDBusInterface>
|
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusConnectionInterface>
|
|
|
|
|
2022-12-11 10:18:46 +01:00
|
|
|
#include <syslog.h>
|
2022-09-25 17:41:18 +03:00
|
|
|
|
2022-12-11 10:09:33 +02:00
|
|
|
// see kdebug.areas
|
2023-07-03 11:16:50 +03:00
|
|
|
static const int s_kauthorizationarea = 185;
|
2023-09-09 04:10:56 +03:00
|
|
|
static const qint64 s_servicetimeout = 30000;
|
2022-12-11 10:09:33 +02:00
|
|
|
|
2022-09-25 17:41:18 +03:00
|
|
|
void kAuthMessageHandler(QtMsgType type, const char *msg)
|
|
|
|
{
|
2022-12-11 12:23:37 +02:00
|
|
|
// NOTE: cannot use KDebug because if it triggers a warning the program will dead-lock
|
2022-12-11 10:18:46 +01:00
|
|
|
switch (type) {
|
|
|
|
case QtDebugMsg: {
|
|
|
|
::syslog(LOG_DEBUG, "%s", msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QtWarningMsg: {
|
|
|
|
::syslog(LOG_WARNING, "%s", msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QtCriticalMsg: {
|
|
|
|
::syslog(LOG_CRIT, "%s", msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QtFatalMsg: {
|
|
|
|
::syslog(LOG_ERR, "%s", msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 17:41:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class KAuthorizationAdaptor: public QDBusAbstractAdaptor
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_CLASSINFO("D-Bus Interface", "org.kde.kauthorization")
|
|
|
|
public:
|
2022-09-26 21:18:54 +03:00
|
|
|
KAuthorizationAdaptor(QObject *parent);
|
2022-09-25 17:41:18 +03:00
|
|
|
|
|
|
|
public Q_SLOTS:
|
|
|
|
Q_SCRIPTABLE int execute(const QString &method, const QVariantMap &arguments);
|
|
|
|
Q_SCRIPTABLE void stop();
|
2022-12-11 11:34:53 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void delayedStop();
|
2022-09-25 17:41:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
KAuthorizationAdaptor::KAuthorizationAdaptor(QObject *parent)
|
|
|
|
: QDBusAbstractAdaptor(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int KAuthorizationAdaptor::execute(const QString &method, const QVariantMap &arguments)
|
|
|
|
{
|
|
|
|
const QByteArray bytemethod = method.toLatin1();
|
|
|
|
int result = KAuthorization::HelperError;
|
|
|
|
const bool success = QMetaObject::invokeMethod(
|
|
|
|
parent(), bytemethod.constData(), Qt::DirectConnection,
|
|
|
|
Q_RETURN_ARG(int, result), Q_ARG(QVariantMap, arguments)
|
|
|
|
);
|
|
|
|
if (!success) {
|
2023-07-03 11:16:50 +03:00
|
|
|
kWarning(s_kauthorizationarea) << "Invalid method" << method;
|
2022-12-11 11:34:53 +02:00
|
|
|
delayedStop();
|
2022-09-25 17:41:18 +03:00
|
|
|
return KAuthorization::MethodError;
|
|
|
|
}
|
2022-12-11 11:34:53 +02:00
|
|
|
delayedStop();
|
2022-09-25 17:41:18 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KAuthorizationAdaptor::stop()
|
|
|
|
{
|
|
|
|
qApp->quit();
|
|
|
|
}
|
|
|
|
|
2022-12-11 11:34:53 +02:00
|
|
|
void KAuthorizationAdaptor::delayedStop()
|
|
|
|
{
|
2023-09-08 00:08:21 +03:00
|
|
|
QTimer::singleShot(100, this, SLOT(stop()));
|
2022-12-11 11:34:53 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 20:25:05 +03:00
|
|
|
|
2022-12-12 08:19:15 +02:00
|
|
|
KAuthorization::KAuthorization(const char* const helper, QObject *parent)
|
2022-12-08 01:29:04 +02:00
|
|
|
: QObject(parent ? parent : qApp)
|
2022-09-25 17:41:18 +03:00
|
|
|
{
|
2022-12-12 08:19:15 +02:00
|
|
|
Q_ASSERT(helper);
|
|
|
|
setObjectName(QString::fromLatin1(helper));
|
2022-09-25 17:41:18 +03:00
|
|
|
new KAuthorizationAdaptor(this);
|
|
|
|
}
|
|
|
|
|
2022-12-12 08:19:15 +02:00
|
|
|
KAuthorization::~KAuthorization()
|
|
|
|
{
|
|
|
|
const QString helper = objectName();
|
|
|
|
if (!helper.isEmpty()) {
|
|
|
|
QDBusConnection::systemBus().unregisterService(helper);
|
|
|
|
QDBusConnection::systemBus().unregisterObject(QString::fromLatin1("/KAuthorization"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:41:18 +03:00
|
|
|
bool KAuthorization::isAuthorized(const QString &helper)
|
|
|
|
{
|
2024-03-14 22:15:01 +02:00
|
|
|
kDebug(s_kauthorizationarea) << "Checking if" << helper << "is authorized";
|
2023-09-09 21:16:04 +03:00
|
|
|
KLockFile authorizationlock(helper);
|
2023-09-08 02:59:47 +03:00
|
|
|
authorizationlock.lock();
|
2022-09-25 17:41:18 +03:00
|
|
|
QDBusInterface kauthorizationinterface(
|
2022-12-12 08:19:15 +02:00
|
|
|
helper, QString::fromLatin1("/KAuthorization"), QString::fromLatin1("org.kde.kauthorization"),
|
2022-09-25 17:41:18 +03:00
|
|
|
QDBusConnection::systemBus()
|
|
|
|
);
|
2022-12-11 11:34:53 +02:00
|
|
|
QDBusReply<void> reply = kauthorizationinterface.call(QString::fromLatin1("stop"));
|
|
|
|
bool result = true;
|
2022-09-25 17:41:18 +03:00
|
|
|
if (!reply.isValid()) {
|
2023-07-03 11:16:50 +03:00
|
|
|
kWarning(s_kauthorizationarea) << reply.error().message();
|
2022-12-11 11:34:53 +02:00
|
|
|
result = false;
|
2022-09-25 17:41:18 +03:00
|
|
|
}
|
2023-07-03 11:16:50 +03:00
|
|
|
kDebug(s_kauthorizationarea) << "Result is" << result;
|
2022-09-25 17:41:18 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int KAuthorization::execute(const QString &helper, const QString &method, const QVariantMap &arguments)
|
|
|
|
{
|
2023-07-03 11:16:50 +03:00
|
|
|
kDebug(s_kauthorizationarea) << "Executing" << helper << "method" << method;
|
2023-09-09 21:16:04 +03:00
|
|
|
KLockFile authorizationlock(helper);
|
2023-09-08 02:59:47 +03:00
|
|
|
authorizationlock.lock();
|
2022-09-25 17:41:18 +03:00
|
|
|
QDBusInterface kauthorizationinterface(
|
2022-12-12 08:19:15 +02:00
|
|
|
helper, QString::fromLatin1("/KAuthorization"), QString::fromLatin1("org.kde.kauthorization"),
|
2022-09-25 17:41:18 +03:00
|
|
|
QDBusConnection::systemBus()
|
|
|
|
);
|
2023-09-09 04:10:56 +03:00
|
|
|
QDBusReply<int> reply = kauthorizationinterface.call(QString::fromLatin1("execute"), method, arguments);
|
2022-12-11 11:34:53 +02:00
|
|
|
int result = KAuthorization::NoError;
|
2022-09-25 17:41:18 +03:00
|
|
|
if (!reply.isValid()) {
|
2022-12-11 11:34:53 +02:00
|
|
|
result = KAuthorization::DBusError;
|
2023-07-03 11:16:50 +03:00
|
|
|
kWarning(s_kauthorizationarea) << reply.error().message();
|
2022-12-11 11:34:53 +02:00
|
|
|
if (reply.error().type() == QDBusError::AccessDenied) {
|
|
|
|
result = KAuthorization::AuthorizationError;
|
|
|
|
}
|
2022-09-25 17:41:18 +03:00
|
|
|
} else {
|
|
|
|
result = reply.value();
|
|
|
|
}
|
2023-07-03 11:16:50 +03:00
|
|
|
kDebug(s_kauthorizationarea) << "Result is" << result;
|
2022-09-25 17:41:18 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString KAuthorization::errorString(const int status)
|
|
|
|
{
|
|
|
|
switch (status) {
|
|
|
|
case KAuthorization::NoError: {
|
|
|
|
return i18n("No error");
|
|
|
|
}
|
|
|
|
case KAuthorization::HelperError: {
|
|
|
|
return i18n("Helper error");
|
|
|
|
}
|
|
|
|
case KAuthorization::DBusError: {
|
|
|
|
return i18n("D-Bus error");
|
|
|
|
}
|
|
|
|
case KAuthorization::MethodError: {
|
|
|
|
return i18n("Method error");
|
|
|
|
}
|
|
|
|
case KAuthorization::AuthorizationError: {
|
|
|
|
return i18n("Authorization error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status > KAuthorization::NoError) {
|
|
|
|
return i18n("Custom error");
|
|
|
|
}
|
|
|
|
return i18n("Unknown error");
|
|
|
|
}
|
|
|
|
|
|
|
|
void KAuthorization::helperMain(const char* const helper, KAuthorization *object)
|
|
|
|
{
|
2022-12-11 10:18:46 +01:00
|
|
|
::openlog(helper, 0, LOG_USER);
|
|
|
|
|
2022-09-25 17:41:18 +03:00
|
|
|
qInstallMsgHandler(kAuthMessageHandler);
|
|
|
|
|
|
|
|
if (!QDBusConnection::systemBus().registerService(QString::fromLatin1(helper))) {
|
|
|
|
qApp->exit(1);
|
|
|
|
return;
|
|
|
|
}
|
2022-12-12 08:19:15 +02:00
|
|
|
if (!QDBusConnection::systemBus().registerObject(QString::fromLatin1("/KAuthorization"), object)) {
|
2022-09-25 17:41:18 +03:00
|
|
|
qApp->exit(2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// in case the process executing the helper crashes
|
2023-09-09 04:10:56 +03:00
|
|
|
QTimer::singleShot(s_servicetimeout, qApp, SLOT(quit()));
|
2022-09-25 17:41:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "kauthorization.moc"
|
|
|
|
#include "moc_kauthorization.cpp"
|