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

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • 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 
28 namespace KAuth
29 {
30 
31 PolicyKitBackend::PolicyKitBackend()
32  : AuthBackend()
33 {
34  setCapabilities(AuthorizeFromClientCapability);
35 }
36 
37 Action::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 
47 void 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 
57 Action::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 
72 QByteArray PolicyKitBackend::callerID() const
73 {
74  QByteArray a;
75  QDataStream s(&a, QIODevice::WriteOnly);
76  s << QCoreApplication::applicationPid();
77 
78  return a;
79 }
80 
81 AuthBackend::ExtraCallerIDVerificationMethod Polkit1Backend::extraCallerIDVerificationMethod() const
82 {
83  return VerifyAgainstDBusServicePid;
84 }
85 
86 bool 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 
96 void 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 
109 Q_EXPORT_PLUGIN2(kauth_backend, KAuth::PolicyKitBackend)
KAuth::PolicyKitBackend::isCallerAuthorized
virtual bool isCallerAuthorized(const QString &action, QByteArray callerID)
Definition: PolicyKitBackend.cpp:86
qint64
KAuth::Action::AuthRequired
The user could obtain the authorization after authentication.
Definition: kauthaction.h:83
QByteArray
KAuth::PolicyKitBackend::authorizeAction
virtual Action::AuthStatus authorizeAction(const QString &)
Definition: PolicyKitBackend.cpp:37
QDataStream
KAuth::AuthBackend::AuthorizeFromClientCapability
Definition: AuthBackend.h:39
KAuth::AuthBackend::actionStatusChanged
void actionStatusChanged(const QString &action, Action::AuthStatus status)
KAuth::AuthBackend::VerifyAgainstDBusServicePid
Definition: AuthBackend.h:49
PolicyKitBackend.h
pid
static pid_t pid
Definition: k3resolvermanager.cpp:249
KAuth::PolicyKitBackend::actionStatus
virtual Action::AuthStatus actionStatus(const QString &)
Definition: PolicyKitBackend.cpp:57
QHash::iterator
KAuth::PolicyKitBackend::setupAction
virtual void setupAction(const QString &)
Definition: PolicyKitBackend.cpp:47
KAuth::Action::Denied
The authorization has been denied by the authorization backend.
Definition: kauthaction.h:79
KAuth::AuthBackend::ExtraCallerIDVerificationMethod
ExtraCallerIDVerificationMethod
Definition: AuthBackend.h:46
KFileSystemType::Unknown
Definition: kfilesystemtype_p.h:29
KAuth::Action::Authorized
The authorization has been granted by the authorization backend.
Definition: kauthaction.h:82
QCoreApplication::applicationPid
qint64 applicationPid()
QString
KAuth::Action::AuthStatus
AuthStatus
The three values returned by authorization methods.
Definition: kauthaction.h:78
KAuth::PolicyKitBackend::PolicyKitBackend
PolicyKitBackend()
Definition: PolicyKitBackend.cpp:31
KAuth::PolicyKitBackend
Definition: PolicyKitBackend.h:32
KAuth::PolicyKitBackend::callerID
virtual QByteArray callerID() const
Definition: PolicyKitBackend.cpp:72
KAuth::AuthBackend::setCapabilities
void setCapabilities(Capabilities capabilities)
Definition: AuthBackend.cpp:52
KAuth::Polkit1Backend::extraCallerIDVerificationMethod
virtual ExtraCallerIDVerificationMethod extraCallerIDVerificationMethod() const
Definition: PolicyKitBackend.cpp:81
KAuth::AuthBackend
Definition: AuthBackend.h:31
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:12 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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