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

KDECore

  • kdecore
  • auth
  • backends
  • policykit
PolicyKitBackend.cpp
Go to the documentation of this file.
1/*
2* Copyright (C) 2008 Nicola Gigante <nicola.gigante@gmail.com>
3* Copyright (C) 2009 Dario Freddi <drf@kde.org>
4*
5* This program is free software; you can redistribute it and/or modify
6* it under the terms of the GNU Lesser General Public License as published by
7* the Free Software Foundation; either version 2.1 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU Lesser General Public License
16* along with this program; if not, write to the
17* Free Software Foundation, Inc.,
18* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA .
19*/
20
21#include "PolicyKitBackend.h"
22
23#include <QtCore/qplugin.h>
24#include <syslog.h>
25#include <polkit-qt/context.h>
26#include <polkit-qt/auth.h>
27
28namespace KAuth
29{
30
31PolicyKitBackend::PolicyKitBackend()
32 : AuthBackend()
33{
34 setCapabilities(AuthorizeFromClientCapability);
35}
36
37Action::AuthStatus PolicyKitBackend::authorizeAction(const QString &action)
38{
39 switch (PolkitQt::Auth::computeAndObtainAuth(action)) {
40 case PolkitQt::Auth::Yes:
41 return Action::Authorized;
42 default:
43 return Action::Denied;
44 }
45}
46
47void PolicyKitBackend::setupAction(const QString &action)
48{
49 connect(PolkitQt::Context::instance(), SIGNAL(configChanged()),
50 this, SLOT(checkForResultChanged()));
51 connect(PolkitQt::Context::instance(), SIGNAL(consoleKitDBChanged()),
52 this, SLOT(checkForResultChanged()));
53
54 m_cachedResults[action] = actionStatus(action);
55}
56
57Action::AuthStatus PolicyKitBackend::actionStatus(const QString &action)
58{
59 PolkitQt::Auth::Result r = PolkitQt::Auth::isCallerAuthorized(action, QCoreApplication::applicationPid(),
60 false);
61 switch (r) {
62 case PolkitQt::Auth::Yes:
63 return Action::Authorized;
64 case PolkitQt::Auth::No:
65 case PolkitQt::Auth::Unknown:
66 return Action::Denied;
67 default:
68 return Action::AuthRequired;
69 }
70}
71
72QByteArray PolicyKitBackend::callerID() const
73{
74 QByteArray a;
75 QDataStream s(&a, QIODevice::WriteOnly);
76 s << QCoreApplication::applicationPid();
77
78 return a;
79}
80
81AuthBackend::ExtraCallerIDVerificationMethod Polkit1Backend::extraCallerIDVerificationMethod() const
82{
83 return VerifyAgainstDBusServicePid;
84}
85
86bool PolicyKitBackend::isCallerAuthorized(const QString &action, QByteArray callerID)
87{
88 QDataStream s(&callerID, QIODevice::ReadOnly);
89 qint64 pid;
90
91 s >> pid;
92
93 return (PolkitQt::Auth::isCallerAuthorized(action, pid, false) == PolkitQt::Auth::Yes);
94}
95
96void PolicyKitBackend::checkForResultChanged()
97{
98 QHash<QString, Action::AuthStatus>::iterator i;
99 for (i = m_cachedResults.begin(); i != m_cachedResults.end(); ++i) {
100 if (i.value() != actionStatus(i.key())) {
101 i.value() = actionStatus(i.key());
102 emit actionStatusChanged(i.key(), i.value());
103 }
104 }
105}
106
107} // namespace Auth
108
109Q_EXPORT_PLUGIN2(kauth_backend, KAuth::PolicyKitBackend)
PolicyKitBackend.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::VerifyAgainstDBusServicePid
@ VerifyAgainstDBusServicePid
Definition: AuthBackend.h:49
KAuth::AuthBackend::actionStatusChanged
void actionStatusChanged(const QString &action, Action::AuthStatus status)
KAuth::AuthBackend::AuthorizeFromClientCapability
@ AuthorizeFromClientCapability
Definition: AuthBackend.h:39
KAuth::PolicyKitBackend
Definition: PolicyKitBackend.h:33
KAuth::PolicyKitBackend::PolicyKitBackend
PolicyKitBackend()
Definition: PolicyKitBackend.cpp:31
KAuth::PolicyKitBackend::setupAction
virtual void setupAction(const QString &)
Definition: PolicyKitBackend.cpp:47
KAuth::PolicyKitBackend::isCallerAuthorized
virtual bool isCallerAuthorized(const QString &action, QByteArray callerID)
Definition: PolicyKitBackend.cpp:86
KAuth::PolicyKitBackend::actionStatus
virtual Action::AuthStatus actionStatus(const QString &)
Definition: PolicyKitBackend.cpp:57
KAuth::PolicyKitBackend::authorizeAction
virtual Action::AuthStatus authorizeAction(const QString &)
Definition: PolicyKitBackend.cpp:37
KAuth::PolicyKitBackend::callerID
virtual QByteArray callerID() const
Definition: PolicyKitBackend.cpp:72
KAuth::Polkit1Backend::extraCallerIDVerificationMethod
virtual ExtraCallerIDVerificationMethod extraCallerIDVerificationMethod() const
Definition: PolicyKitBackend.cpp:81
QHash
Definition: ksycocafactory.h:28
QString
qint64
pid
static pid_t pid
Definition: k3resolvermanager.cpp:249
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