1/* vi: ts=8 sts=4 sw=4
2 *
3 * This file is part of the KDE project, module kdesu.
4 * Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
5 *
6 * This is free software; you can use this library under the GNU Library
7 * General Public License, version 2. See the file "COPYING.LIB" for the
8 * exact licensing terms.
9 *
10 * kcookie.cpp: KDE authentication cookies.
11 */
12
13#include "kcookie.h"
14
15#include <stdlib.h>
16
17#include <QtCore/QString>
18#include <QtCore/QStringList>
19#include <QtCore/QBool>
20#include <QtCore/Q_PID>
21
22#include <kdebug.h>
23
24extern int kdesuDebugArea();
25
26namespace KDESu {
27namespace KDESuPrivate {
28
29class KCookie::KCookiePrivate
30{
31public:
32 QByteArray m_Display;
33#ifdef Q_WS_X11
34 QByteArray m_DisplayAuth;
35#endif
36};
37
38
39
40KCookie::KCookie()
41 : d( new KCookiePrivate )
42{
43#ifdef Q_WS_X11
44 getXCookie();
45#endif
46}
47
48KCookie::~KCookie()
49{
50 delete d;
51}
52
53QByteArray KCookie::display() const
54{
55 return d->m_Display;
56}
57
58#ifdef Q_WS_X11
59QByteArray KCookie::displayAuth() const
60{
61 return d->m_DisplayAuth;
62}
63#endif
64
65void KCookie::getXCookie()
66{
67#ifdef Q_WS_X11
68 d->m_Display = qgetenv("DISPLAY");
69#else
70 d->m_Display = qgetenv("QWS_DISPLAY");
71#endif
72 if (d->m_Display.isEmpty())
73 {
74 kError(kdesuDebugArea()) << k_lineinfo << "$DISPLAY is not set.";
75 return;
76 }
77#ifdef Q_WS_X11 // No need to mess with X Auth stuff
78 QByteArray disp = d->m_Display;
79 if (disp.startsWith("localhost:")) // krazy:exclude=strings
80 disp.remove(0, 9);
81
82 QProcess proc;
83 proc.start("xauth", QStringList() << "list" << disp);
84 if (!proc.waitForStarted())
85 {
86 kError(kdesuDebugArea()) << k_lineinfo << "Could not run xauth.";
87 return;
88 }
89 proc.waitForReadyRead(100);
90 QByteArray output = proc.readLine().simplified();
91 if (output.isEmpty())
92 {
93 kWarning(kdesuDebugArea()) << "No X authentication info set for display" << d->m_Display;
94 return;
95 }
96 QList<QByteArray> lst = output.split(' ');
97 if (lst.count() != 3)
98 {
99 kError(kdesuDebugArea()) << k_lineinfo << "parse error.";
100 return;
101 }
102 d->m_DisplayAuth = (lst[1] + ' ' + lst[2]);
103 proc.waitForFinished(100); // give QProcess a chance to clean up gracefully
104#endif
105}
106
107}}
108