KDESu

kcookie.cpp
1/*
2 This file is part of the KDE project, module kdesu.
3 SPDX-FileCopyrightText: 1999, 2000 Geert Jansen <jansen@kde.org>
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
19extern int kdesuDebugArea();
20
21namespace KDESu
22{
23namespace KDESuPrivate
24{
25class KCookiePrivate
26{
27public:
28 QByteArray display;
29#if HAVE_X11
30 QByteArray displayAuth;
31#endif
32};
33
34KCookie::KCookie()
35 : d(new KCookiePrivate)
36{
37#if HAVE_X11
38 getXCookie();
39#endif
40}
41
42KCookie::~KCookie() = default;
43
44QByteArray KCookie::display() const
45{
46 return d->display;
47}
48
49#if HAVE_X11
50QByteArray KCookie::displayAuth() const
51{
52 return d->displayAuth;
53}
54#endif
55
56void 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
bool isEmpty() const const
QByteArray & remove(qsizetype pos, qsizetype len)
QByteArray simplified() const const
QList< QByteArray > split(char sep) const const
bool startsWith(QByteArrayView bv) const const
QByteArray readLine(qint64 maxSize)
qsizetype count() const const
void start(OpenMode mode)
bool waitForFinished(int msecs)
virtual bool waitForReadyRead(int msecs) override
bool waitForStarted(int msecs)
QString findExecutable(const QString &executableName, const QStringList &paths)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.