KDESu

kcookie.cpp
1 /*
2  This file is part of the KDE project, module kdesu.
3  SPDX-FileCopyrightText: 1999, 2000 Geert Jansen <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 
7  kcookie.cpp: KDE authentication cookies.
8 */
9 
10 #include "kcookie_p.h"
11 
12 #include <ksu_debug.h>
13 
14 #include <QProcess>
15 #include <QStandardPaths>
16 #include <QString>
17 #include <QStringList>
18 
19 extern int kdesuDebugArea();
20 
21 namespace KDESu
22 {
23 namespace KDESuPrivate
24 {
25 class KCookiePrivate
26 {
27 public:
28  QByteArray display;
29 #if HAVE_X11
30  QByteArray displayAuth;
31 #endif
32 };
33 
34 KCookie::KCookie()
35  : d(new KCookiePrivate)
36 {
37 #if HAVE_X11
38  getXCookie();
39 #endif
40 }
41 
42 KCookie::~KCookie() = default;
43 
44 QByteArray KCookie::display() const
45 {
46  return d->display;
47 }
48 
49 #if HAVE_X11
50 QByteArray KCookie::displayAuth() const
51 {
52  return d->displayAuth;
53 }
54 #endif
55 
56 void KCookie::getXCookie()
57 {
58 #if HAVE_X11
59  d->display = qgetenv("DISPLAY");
60  if (d->display.isEmpty()) {
61  // maybe we are on Wayland?
62  d->display = qgetenv("WAYLAND_DISPLAY");
63  if (!d->display.isEmpty()) {
64  // don't go into the xauth code path
65  return;
66  }
67  }
68 #else
69  d->display = qgetenv("QWS_DISPLAY");
70 #endif
71  if (d->display.isEmpty()) {
72  qCCritical(KSU_LOG) << "[" << __FILE__ << ":" << __LINE__ << "] "
73  << "$DISPLAY is not set.";
74  return;
75  }
76 #if HAVE_X11 // No need to mess with X Auth stuff
77  QByteArray disp = d->display;
78  if (disp.startsWith("localhost:")) { // krazy:exclude=strings
79  disp.remove(0, 9);
80  }
81 
82  const QString xauthExec = QStandardPaths::findExecutable(QStringLiteral("xauth"));
83  if (xauthExec.isEmpty()) {
84  qCCritical(KSU_LOG) << "[" << __FILE__ << ":" << __LINE__ << "] "
85  << "Could not run xauth, not found in path";
86  return;
87  }
88 
89  QProcess proc;
90  proc.start(xauthExec, QStringList{QStringLiteral("list"), QString::fromUtf8(disp)});
91  if (!proc.waitForStarted()) {
92  qCCritical(KSU_LOG) << "[" << __FILE__ << ":" << __LINE__ << "] "
93  << "Could not run xauth. Found in path:" << xauthExec;
94  return;
95  }
96  proc.waitForReadyRead(100);
97 
98  QByteArray output = proc.readLine().simplified();
99  if (output.isEmpty()) {
100  qCWarning(KSU_LOG) << "No X authentication info set for display" << d->display;
101  return;
102  }
103 
104  QList<QByteArray> lst = output.split(' ');
105  if (lst.count() != 3) {
106  qCCritical(KSU_LOG) << "[" << __FILE__ << ":" << __LINE__ << "] "
107  << "parse error.";
108  return;
109  }
110  d->displayAuth = (lst[1] + ' ' + lst[2]);
111  proc.waitForFinished(100); // give QProcess a chance to clean up gracefully
112 #endif
113 }
114 
115 } // namespace KDESuPrivate
116 } // namespace KDESu
void start(const QString &program, const QStringList &arguments, QIODevice::OpenMode mode)
QString fromUtf8(const char *str, int size)
bool waitForFinished(int msecs)
int count(const T &value) const const
QList< QByteArray > split(char sep) const const
QString findExecutable(const QString &executableName, const QStringList &paths)
QByteArray & remove(int pos, int len)
bool isEmpty() const const
bool waitForStarted(int msecs)
bool startsWith(const QByteArray &ba) const const
qint64 readLine(char *data, qint64 maxSize)
virtual bool waitForReadyRead(int msecs) override
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Jun 26 2022 03:48:58 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.