• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • auth
  • backends
  • polkit-1
Polkit1Backend.cpp
Go to the documentation of this file.
1/*
2* Copyright (C) 2008 Nicola Gigante <nicola.gigante@gmail.com>
3* Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com>
4* Copyright (C) 2009-2010 Dario Freddi <drf@kde.org>
5*
6* This program is free software; you can redistribute it and/or modify
7* it under the terms of the GNU Lesser General Public License as published by
8* the Free Software Foundation; either version 2.1 of the License, or
9* (at your option) any later version.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU General Public License for more details.
15*
16* You should have received a copy of the GNU Lesser General Public License
17* along with this program; if not, write to the
18* Free Software Foundation, Inc.,
19* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA .
20*/
21
22#include "Polkit1Backend.h"
23
24#include <QtCore/qplugin.h>
25#include <QtCore/QCoreApplication>
26#include <QtCore/QTimer>
27
28#include <QtGui/QApplication>
29#include <QtGui/QWidget>
30
31#include <QtDBus/QDBusConnection>
32#include <QtDBus/QDBusConnectionInterface>
33
34#include <kdebug.h>
35
36#include <PolkitQt1/Authority>
37#include <PolkitQt1/Subject>
38
39namespace KAuth
40{
41
42PolkitResultEventLoop::PolkitResultEventLoop(QObject* parent)
43 : QEventLoop(parent)
44{
45}
46
47PolkitResultEventLoop::~PolkitResultEventLoop()
48{
49}
50
51void PolkitResultEventLoop::requestQuit(const PolkitQt1::Authority::Result& result)
52{
53 m_result = result;
54 quit();
55}
56
57PolkitQt1::Authority::Result PolkitResultEventLoop::result() const
58{
59 return m_result;
60}
61
62Polkit1Backend::Polkit1Backend()
63 : AuthBackend()
64 , m_flyingActions(false)
65{
66 setCapabilities(AuthorizeFromHelperCapability | CheckActionExistenceCapability | PreAuthActionCapability);
67
68 // Setup useful signals
69 connect(PolkitQt1::Authority::instance(), SIGNAL(configChanged()),
70 this, SLOT(checkForResultChanged()));
71 connect(PolkitQt1::Authority::instance(), SIGNAL(consoleKitDBChanged()),
72 this, SLOT(checkForResultChanged()));
73 connect(PolkitQt1::Authority::instance(), SIGNAL(enumerateActionsFinished(PolkitQt1::ActionDescription::List)),
74 this, SLOT(updateCachedActions(PolkitQt1::ActionDescription::List)));
75
76 // Cache existing action IDs as soon as possible
77 m_flyingActions = true;
78 PolkitQt1::Authority::instance()->enumerateActions();
79}
80
81Polkit1Backend::~Polkit1Backend()
82{
83
84}
85
86void Polkit1Backend::preAuthAction(const QString& action, QWidget* parent)
87{
88 kDebug();
89 // If a parent was not specified, skip this
90 if (!parent) {
91 kDebug() << "Parent widget does not exist, skipping";
92 return;
93 }
94
95 // Are we running our KDE auth agent?
96 if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.kde.Polkit1AuthAgent"))) {
97 // Check if we actually are entitled to use GUI capabilities
98 if (qApp == 0 || QApplication::type() == QApplication::Tty) {
99 kDebug() << "Not streaming parent as we are on a TTY application";
100 }
101
102 // Retrieve the dialog root window Id
103 qulonglong wId = parent->effectiveWinId();
104
105 // Send it over the bus to our agent
106 QDBusMessage methodCall =
107 QDBusMessage::createMethodCall(QLatin1String("org.kde.Polkit1AuthAgent"), QLatin1String("/org/kde/Polkit1AuthAgent"), QLatin1String("org.kde.Polkit1AuthAgent"),
108 QLatin1String("setWIdForAction"));
109
110 methodCall << action;
111 methodCall << wId;
112
113 QDBusPendingCall call = QDBusConnection::sessionBus().asyncCall(methodCall);
114 call.waitForFinished();
115
116 if (call.isError()) {
117 kWarning() << "ERROR while streaming the parent!!" << call.error();
118 }
119 } else {
120 kDebug() << "KDE polkit agent appears too old or not registered on the bus";
121 }
122}
123
124void Polkit1Backend::updateCachedActions(const PolkitQt1::ActionDescription::List& actions)
125{
126 m_knownActions.clear();
127 foreach (const PolkitQt1::ActionDescription& action, actions) {
128 m_knownActions << action.actionId();
129 }
130 m_flyingActions = false;
131}
132
133Action::AuthStatus Polkit1Backend::authorizeAction(const QString &action)
134{
135 Q_UNUSED(action)
136 // Always return Yes here, we'll authorize inside isCallerAuthorized
137 return Action::Authorized;
138}
139
140void Polkit1Backend::setupAction(const QString &action)
141{
142 m_cachedResults[action] = actionStatus(action);
143}
144
145Action::AuthStatus Polkit1Backend::actionStatus(const QString &action)
146{
147 PolkitQt1::SystemBusNameSubject subject(QString::fromUtf8(callerID()));
148 PolkitQt1::Authority::Result r = PolkitQt1::Authority::instance()->checkAuthorizationSync(action, subject,
149 PolkitQt1::Authority::None);
150 switch (r) {
151 case PolkitQt1::Authority::Yes:
152 return Action::Authorized;
153 case PolkitQt1::Authority::No:
154 case PolkitQt1::Authority::Unknown:
155 return Action::Denied;
156 default:
157 return Action::AuthRequired;
158 }
159}
160
161QByteArray Polkit1Backend::callerID() const
162{
163 return QDBusConnection::systemBus().baseService().toUtf8();
164}
165
166AuthBackend::ExtraCallerIDVerificationMethod Polkit1Backend::extraCallerIDVerificationMethod() const
167{
168 return VerifyAgainstDBusServiceName;
169}
170
171bool Polkit1Backend::isCallerAuthorized(const QString &action, QByteArray callerID)
172{
173 PolkitQt1::SystemBusNameSubject subject(QString::fromUtf8(callerID));
174 PolkitQt1::Authority *authority = PolkitQt1::Authority::instance();
175
176 PolkitResultEventLoop e;
177 connect(authority, SIGNAL(checkAuthorizationFinished(PolkitQt1::Authority::Result)),
178 &e, SLOT(requestQuit(PolkitQt1::Authority::Result)));
179 authority->checkAuthorization(action, subject, PolkitQt1::Authority::AllowUserInteraction);
180 e.exec();
181
182 switch (e.result()) {
183 case PolkitQt1::Authority::Yes:
184 return true;
185 default:
186 return false;
187 }
188
189 return false;
190}
191
192void Polkit1Backend::checkForResultChanged()
193{
194 foreach(const QString &action, m_cachedResults.keys()) {
195 if (m_cachedResults[action] != actionStatus(action)) {
196 m_cachedResults[action] = actionStatus(action);
197 emit actionStatusChanged(action, m_cachedResults[action]);
198 }
199 }
200
201 // Force updating known actions
202 PolkitQt1::Authority::instance()->enumerateActions();
203 m_flyingActions = true;
204}
205
206bool Polkit1Backend::actionExists(const QString& action)
207{
208 // Any flying actions?
209 if (m_flyingActions) {
210 int tries = 0;
211 while (m_flyingActions && tries < 10) {
212 // Wait max 2 seconds
213 QEventLoop e;
214 QTimer::singleShot(200, &e, SLOT(quit()));
215 e.exec();
216 ++tries;
217 }
218 }
219
220 return m_knownActions.contains(action);
221}
222
223} // namespace Auth
224
225Q_EXPORT_PLUGIN2(kauth_backend, KAuth::Polkit1Backend)
Polkit1Backend.h
KAuth::Action::AuthStatus
AuthStatus
The three values returned by authorization methods.
Definition: kauthaction.h:78
KAuth::Action::Denied
@ Denied
The authorization has been denied by the authorization backend.
Definition: kauthaction.h:79
KAuth::Action::Authorized
@ Authorized
The authorization has been granted by the authorization backend.
Definition: kauthaction.h:82
KAuth::Action::AuthRequired
@ AuthRequired
The user could obtain the authorization after authentication.
Definition: kauthaction.h:83
KAuth::AuthBackend
Definition: AuthBackend.h:32
KAuth::AuthBackend::setCapabilities
void setCapabilities(Capabilities capabilities)
Definition: AuthBackend.cpp:52
KAuth::AuthBackend::ExtraCallerIDVerificationMethod
ExtraCallerIDVerificationMethod
Definition: AuthBackend.h:46
KAuth::AuthBackend::VerifyAgainstDBusServiceName
@ VerifyAgainstDBusServiceName
Definition: AuthBackend.h:48
KAuth::AuthBackend::actionStatusChanged
void actionStatusChanged(const QString &action, Action::AuthStatus status)
KAuth::AuthBackend::PreAuthActionCapability
@ PreAuthActionCapability
Definition: AuthBackend.h:42
KAuth::AuthBackend::AuthorizeFromHelperCapability
@ AuthorizeFromHelperCapability
Definition: AuthBackend.h:40
KAuth::AuthBackend::CheckActionExistenceCapability
@ CheckActionExistenceCapability
Definition: AuthBackend.h:41
KAuth::Polkit1Backend
Definition: Polkit1Backend.h:39
KAuth::Polkit1Backend::isCallerAuthorized
virtual bool isCallerAuthorized(const QString &action, QByteArray callerID)
Definition: Polkit1Backend.cpp:171
KAuth::Polkit1Backend::preAuthAction
virtual void preAuthAction(const QString &action, QWidget *parent)
Definition: Polkit1Backend.cpp:86
KAuth::Polkit1Backend::authorizeAction
virtual Action::AuthStatus authorizeAction(const QString &)
Definition: Polkit1Backend.cpp:133
KAuth::Polkit1Backend::setupAction
virtual void setupAction(const QString &)
Definition: Polkit1Backend.cpp:140
KAuth::Polkit1Backend::actionExists
virtual bool actionExists(const QString &action)
Definition: Polkit1Backend.cpp:206
KAuth::Polkit1Backend::extraCallerIDVerificationMethod
virtual ExtraCallerIDVerificationMethod extraCallerIDVerificationMethod() const
Definition: PolicyKitBackend.cpp:81
KAuth::Polkit1Backend::~Polkit1Backend
virtual ~Polkit1Backend()
Definition: Polkit1Backend.cpp:81
KAuth::Polkit1Backend::callerID
virtual QByteArray callerID() const
Definition: Polkit1Backend.cpp:161
KAuth::Polkit1Backend::actionStatus
virtual Action::AuthStatus actionStatus(const QString &)
Definition: Polkit1Backend.cpp:145
KAuth::Polkit1Backend::Polkit1Backend
Polkit1Backend()
Definition: Polkit1Backend.cpp:62
KAuth::PolkitResultEventLoop
Definition: Polkit1Backend.h:66
KAuth::PolkitResultEventLoop::requestQuit
void requestQuit(const PolkitQt1::Authority::Result &result)
Definition: Polkit1Backend.cpp:51
KAuth::PolkitResultEventLoop::result
PolkitQt1::Authority::Result result() const
Definition: Polkit1Backend.cpp:57
KAuth::PolkitResultEventLoop::PolkitResultEventLoop
PolkitResultEventLoop(QObject *parent=0)
Definition: Polkit1Backend.cpp:42
KAuth::PolkitResultEventLoop::~PolkitResultEventLoop
virtual ~PolkitResultEventLoop()
Definition: Polkit1Backend.cpp:47
QObject
QString
kDebug
#define kDebug
Definition: kdebug.h:316
kWarning
#define kWarning
Definition: kdebug.h:322
kdebug.h
KAuth
Definition: AuthBackend.cpp:24
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal