File indexing completed on 2024-03-03 04:00:33

0001 /*
0002     SPDX-FileCopyrightText: 2008 Nicola Gigante <nicola.gigante@gmail.com>
0003     SPDX-FileCopyrightText: 2009 Dario Freddi <drf@kde.org>
0004     SPDX-FileCopyrightText: 2020 Harald Sitter <sitter@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 #include "helpersupport.h"
0010 
0011 #include <cstdlib>
0012 
0013 #ifndef Q_OS_WIN
0014 #include <pwd.h>
0015 #include <sys/types.h>
0016 #include <syslog.h>
0017 #include <unistd.h>
0018 #else
0019 // Quick hack to replace syslog (just write to stderr)
0020 // TODO: should probably use ReportEvent
0021 #define LOG_ERR 3
0022 #define LOG_WARNING 4
0023 #define LOG_DEBUG 7
0024 #define LOG_INFO 6
0025 #define LOG_USER (1 << 3)
0026 static inline void openlog(const char *, int, int)
0027 {
0028 }
0029 static inline void closelog()
0030 {
0031 }
0032 #define syslog(level, ...) fprintf(stderr, __VA_ARGS__)
0033 
0034 #endif
0035 
0036 #include <QCoreApplication>
0037 #include <QTimer>
0038 
0039 #include "BackendsManager.h"
0040 
0041 namespace KAuth
0042 {
0043 namespace HelperSupport
0044 {
0045 void helperDebugHandler(QtMsgType type, const QMessageLogContext &context, const QString &msgStr);
0046 }
0047 
0048 static bool remote_dbg = false;
0049 
0050 #ifdef Q_OS_UNIX
0051 static void fixEnvironment()
0052 {
0053     // try correct HOME
0054     const char *home = "HOME";
0055     if (getenv(home) == nullptr) {
0056         struct passwd *pw = getpwuid(getuid());
0057 
0058         if (pw != nullptr) {
0059             int overwrite = 0;
0060             setenv(home, pw->pw_dir, overwrite);
0061         }
0062     }
0063 }
0064 #endif
0065 
0066 int HelperSupport::helperMain(int argc, char **argv, const char *id, QObject *responder)
0067 {
0068 #ifdef Q_OS_UNIX
0069     fixEnvironment();
0070 #endif
0071 
0072 #ifdef Q_OS_OSX
0073     openlog(id, LOG_CONS | LOG_PID, LOG_USER);
0074     int logLevel = LOG_WARNING;
0075 #else
0076     openlog(id, 0, LOG_USER);
0077     int logLevel = LOG_DEBUG;
0078 #endif
0079     qInstallMessageHandler(&HelperSupport::helperDebugHandler);
0080 
0081     // NOTE: The helper proxy might use dbus, and we should have the qapp
0082     //       before using dbus.
0083     QCoreApplication app(argc, argv);
0084 
0085     if (!BackendsManager::helperProxy()->initHelper(QString::fromLatin1(id))) {
0086         syslog(logLevel, "Helper initialization failed");
0087         return -1;
0088     }
0089 
0090     // closelog();
0091     remote_dbg = true;
0092 
0093     BackendsManager::helperProxy()->setHelperResponder(responder);
0094 
0095     // Attach the timer
0096     QTimer *timer = new QTimer(nullptr);
0097     responder->setProperty("__KAuth_Helper_Shutdown_Timer", QVariant::fromValue(timer));
0098     timer->setInterval(10000);
0099     timer->start();
0100     QObject::connect(timer, &QTimer::timeout, &app, &QCoreApplication::quit);
0101     app.exec(); // krazy:exclude=crashy
0102 
0103     return 0;
0104 }
0105 
0106 void HelperSupport::helperDebugHandler(QtMsgType type, const QMessageLogContext &context, const QString &msgStr)
0107 {
0108     Q_UNUSED(context); // can be used to find out about file, line, function name
0109     QByteArray msg = msgStr.toLocal8Bit();
0110     if (!remote_dbg) {
0111         int level = LOG_DEBUG;
0112         switch (type) {
0113         case QtDebugMsg:
0114             level = LOG_DEBUG;
0115             break;
0116         case QtWarningMsg:
0117             level = LOG_WARNING;
0118             break;
0119         case QtCriticalMsg:
0120         case QtFatalMsg:
0121             level = LOG_ERR;
0122             break;
0123         case QtInfoMsg:
0124             level = LOG_INFO;
0125             break;
0126         }
0127         syslog(level, "%s", msg.constData());
0128     } else {
0129         BackendsManager::helperProxy()->sendDebugMessage(type, msg.constData());
0130     }
0131 
0132     // Anyway I should follow the rule:
0133     if (type == QtFatalMsg) {
0134         exit(-1);
0135     }
0136 }
0137 
0138 void HelperSupport::progressStep(int step)
0139 {
0140     BackendsManager::helperProxy()->sendProgressStep(step);
0141 }
0142 
0143 void HelperSupport::progressStep(const QVariantMap &data)
0144 {
0145     BackendsManager::helperProxy()->sendProgressStepData(data);
0146 }
0147 
0148 bool HelperSupport::isStopped()
0149 {
0150     return BackendsManager::helperProxy()->hasToStopAction();
0151 }
0152 
0153 int HelperSupport::callerUid()
0154 {
0155     return BackendsManager::helperProxy()->callerUid();
0156 }
0157 
0158 } // namespace Auth