KAuth

helpersupport.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Nicola Gigante <[email protected]>
3  SPDX-FileCopyrightText: 2009 Dario Freddi <[email protected]>
4  SPDX-FileCopyrightText: 2020 Harald Sitter <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.1-or-later
7 */
8 
9 #include "helpersupport.h"
10 
11 #include <cstdlib>
12 
13 #ifndef Q_OS_WIN
14 #include <pwd.h>
15 #include <sys/types.h>
16 #include <syslog.h>
17 #include <unistd.h>
18 #else
19 // Quick hack to replace syslog (just write to stderr)
20 // TODO: should probably use ReportEvent
21 #define LOG_ERR 3
22 #define LOG_WARNING 4
23 #define LOG_DEBUG 7
24 #define LOG_INFO 6
25 #define LOG_USER (1 << 3)
26 static inline void openlog(const char *, int, int)
27 {
28 }
29 static inline void closelog()
30 {
31 }
32 #define syslog(level, ...) fprintf(stderr, __VA_ARGS__)
33 
34 #endif
35 
36 #include <QCoreApplication>
37 #include <QTimer>
38 #if defined(Q_OS_UNIX) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
39 #include <QTextCodec>
40 #endif
41 
42 #include "BackendsManager.h"
43 
44 namespace KAuth
45 {
46 namespace HelperSupport
47 {
48 void helperDebugHandler(QtMsgType type, const QMessageLogContext &context, const QString &msgStr);
49 }
50 
51 static bool remote_dbg = false;
52 
53 #ifdef Q_OS_UNIX
54 static void fixEnvironment()
55 {
56  // try correct HOME
57  const char *home = "HOME";
58  if (getenv(home) == nullptr) {
59  struct passwd *pw = getpwuid(getuid());
60 
61  if (pw != nullptr) {
62  int overwrite = 0;
63  setenv(home, pw->pw_dir, overwrite);
64  }
65  }
66 }
67 #endif
68 
69 int HelperSupport::helperMain(int argc, char **argv, const char *id, QObject *responder)
70 {
71 #ifdef Q_OS_UNIX
72  fixEnvironment();
73 
74  // With Qt6, it's UTF-8 by default
75 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
76  // As we don't inherit lang, the locale could be something that doesn't support UTF-8. Force it
77  auto utf8Codec = QTextCodec::codecForName("UTF-8");
78  if (utf8Codec) {
80  }
81 #endif
82 #endif
83 
84 #ifdef Q_OS_OSX
85  openlog(id, LOG_CONS | LOG_PID, LOG_USER);
86  int logLevel = LOG_WARNING;
87 #else
88  openlog(id, 0, LOG_USER);
89  int logLevel = LOG_DEBUG;
90 #endif
91  qInstallMessageHandler(&HelperSupport::helperDebugHandler);
92 
93  // NOTE: The helper proxy might use dbus, and we should have the qapp
94  // before using dbus.
95  QCoreApplication app(argc, argv);
96 
97  if (!BackendsManager::helperProxy()->initHelper(QString::fromLatin1(id))) {
98  syslog(logLevel, "Helper initialization failed");
99  return -1;
100  }
101 
102  // closelog();
103  remote_dbg = true;
104 
105  BackendsManager::helperProxy()->setHelperResponder(responder);
106 
107  // Attach the timer
108  QTimer *timer = new QTimer(nullptr);
109  responder->setProperty("__KAuth_Helper_Shutdown_Timer", QVariant::fromValue(timer));
110  timer->setInterval(10000);
111  timer->start();
113  app.exec(); // krazy:exclude=crashy
114 
115  return 0;
116 }
117 
118 void HelperSupport::helperDebugHandler(QtMsgType type, const QMessageLogContext &context, const QString &msgStr)
119 {
120  Q_UNUSED(context); // can be used to find out about file, line, function name
121  QByteArray msg = msgStr.toLocal8Bit();
122  if (!remote_dbg) {
123  int level = LOG_DEBUG;
124  switch (type) {
125  case QtDebugMsg:
126  level = LOG_DEBUG;
127  break;
128  case QtWarningMsg:
129  level = LOG_WARNING;
130  break;
131  case QtCriticalMsg:
132  case QtFatalMsg:
133  level = LOG_ERR;
134  break;
135  case QtInfoMsg:
136  level = LOG_INFO;
137  break;
138  }
139  syslog(level, "%s", msg.constData());
140  } else {
141  BackendsManager::helperProxy()->sendDebugMessage(type, msg.constData());
142  }
143 
144  // Anyway I should follow the rule:
145  if (type == QtFatalMsg) {
146  exit(-1);
147  }
148 }
149 
151 {
152  BackendsManager::helperProxy()->sendProgressStep(step);
153 }
154 
155 void HelperSupport::progressStep(const QVariantMap &data)
156 {
157  BackendsManager::helperProxy()->sendProgressStepData(data);
158 }
159 
161 {
162  return BackendsManager::helperProxy()->hasToStopAction();
163 }
164 
166 {
167  return BackendsManager::helperProxy()->callerUid();
168 }
169 
170 } // namespace Auth
QVariant fromValue(const T &value)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setCodecForLocale(QTextCodec *c)
void start(int msec)
void timeout()
QTextCodec * codecForName(const QByteArray &name)
Definition: action.cpp:18
bool setProperty(const char *name, const QVariant &value)
KAUTHCORE_EXPORT void progressStep(int step)
Send a progressStep signal to the caller application.
KAUTHCORE_EXPORT int callerUid()
Obtains the caller user id if available.
const char * constData() const const
KAUTHCORE_EXPORT bool isStopped()
Check if the caller asked the helper to stop the execution.
QString fromLatin1(const char *str, int size)
KAUTHCORE_EXPORT int helperMain(int argc, char **argv, const char *id, QObject *responder)
Method that implements the main function of the helper tool.
QByteArray toLocal8Bit() const const
void setInterval(int msec)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 6 2023 04:14:27 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.