NetworkManagerQt

secretagent.cpp
1/*
2 SPDX-FileCopyrightText: 2011 Ilia Kats <ilia-kats@gmx.net>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "secretagent.h"
8#include "manager_p.h"
9#include "nmdebug.h"
10#include "secretagent_p.h"
11
12#undef signals
13#include <NetworkManager.h>
14#include <libnm/NetworkManager.h>
15
16#define signals Q_SIGNALS
17
18#include <QDBusConnection>
19#include <QDBusMetaType>
20
21#include "agentmanagerinterface.h"
22#include "secretagentadaptor.h"
23
24NetworkManager::SecretAgentPrivate::SecretAgentPrivate(const QString &id,
27 : q_ptr(parent)
28 , agent(parent)
29#ifdef NMQT_STATIC
30 , agentManager(NetworkManagerPrivate::DBUS_SERVICE, QLatin1String(NM_DBUS_PATH_AGENT_MANAGER), QDBusConnection::sessionBus(), parent)
31 , watcher(NetworkManagerPrivate::DBUS_SERVICE, QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, parent)
32#else
33 , agentManager(NetworkManagerPrivate::DBUS_SERVICE, QLatin1String(NM_DBUS_PATH_AGENT_MANAGER), QDBusConnection::systemBus(), parent)
34 , watcher(NetworkManagerPrivate::DBUS_SERVICE, QDBusConnection::systemBus(), QDBusServiceWatcher::WatchForOwnerChange, parent)
35#endif
36 , agentId(id)
38{
39 Q_Q(SecretAgent);
40
41 qRegisterMetaType<NMVariantMapMap>("NMVariantMapMap");
42 qDBusRegisterMetaType<NMVariantMapMap>();
43
44 // Similarly to NetworkManagerPrivate(), we register two listeners here:
45 // one for ServiceRegistered and one for InterfacesAdded.
46 agentManager.connection().connect(NetworkManagerPrivate::DBUS_SERVICE,
47 "/org/freedesktop",
48 NetworkManagerPrivate::FDO_DBUS_OBJECT_MANAGER,
49 QLatin1String("InterfacesAdded"),
50 q,
51 SLOT(dbusInterfacesAdded(QDBusObjectPath, QVariantMap)));
52
53 QObject::connect(&watcher, &QDBusServiceWatcher::serviceRegistered, parent, [this] { this->daemonRegistered(); });
54
55 agentManager.connection().registerObject(QLatin1String(NM_DBUS_PATH_SECRET_AGENT), &agent, QDBusConnection::ExportAllSlots);
56
57 registerAgent(capabilities);
58}
59
60NetworkManager::SecretAgentPrivate::~SecretAgentPrivate()
61{
62 agentManager.Unregister();
63}
64
65void NetworkManager::SecretAgentPrivate::dbusInterfacesAdded(const QDBusObjectPath &path, const QVariantMap &interfaces)
66{
67 Q_UNUSED(path);
68 if (!interfaces.contains(QString::fromLatin1(agentManager.staticInterfaceName()))) {
69 return;
70 }
71
72 registerAgent(capabilities);
73}
74
75void NetworkManager::SecretAgentPrivate::daemonRegistered()
76{
77 registerAgent(capabilities);
78}
79
80void NetworkManager::SecretAgentPrivate::registerAgent()
81{
83}
84
85void NetworkManager::SecretAgentPrivate::registerAgent(NetworkManager::SecretAgent::Capabilities capabilities)
86{
87 agentManager.RegisterWithCapabilities(agentId, capabilities);
88}
89
91 : QObject(parent)
92 , QDBusContext()
93 , d_ptr(new NetworkManager::SecretAgentPrivate(id, NetworkManager::SecretAgent::Capability::NoCapability, this))
94{
95}
96
98 : QObject(parent)
99 , QDBusContext()
100 , d_ptr(new NetworkManager::SecretAgentPrivate(id, capabilities, this))
101{
102}
103
104NetworkManager::SecretAgent::~SecretAgent()
105{
106 Q_D(SecretAgent);
107 delete d;
108}
109
110void NetworkManager::SecretAgent::sendError(NetworkManager::SecretAgent::Error error, const QString &explanation, const QDBusMessage &callMessage) const
111{
112 Q_D(const SecretAgent);
113
114 QString errorString;
115 switch (error) {
116 case NotAuthorized:
117 errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".NotAuthorized");
118 break;
119 case InvalidConnection:
120 errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".InvalidConnection");
121 break;
122 case UserCanceled:
123 errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".UserCanceled");
124 break;
125 case AgentCanceled:
126 errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".AgentCanceled");
127 break;
128 case InternalError:
129 errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".InternalError");
130 break;
131 case NoSecrets:
132 errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".NoSecrets");
133 break;
134 default:
135 errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".Unknown");
136 break;
137 }
138
139 QDBusMessage reply;
140 if (callMessage.type() == QDBusMessage::InvalidMessage) {
141 reply = message().createErrorReply(errorString, explanation);
142 } else {
143 reply = callMessage.createErrorReply(errorString, explanation);
144 }
145
146 if (!d->agentManager.connection().send(reply)) {
147 qCDebug(NMQT) << "Failed to put error message on DBus queue" << errorString << explanation;
148 }
149}
150
151#include "moc_secretagent.cpp"
Implementation of a private D-Bus interface used by secret agents that store and provide secrets to N...
Definition secretagent.h:30
SecretAgent(const QString &id, QObject *parent=nullptr)
Registers a SecretAgent with the id on NetworkManager Optionally add a capabilities argument.
void sendError(Error error, const QString &explanation, const QDBusMessage &callMessage=QDBusMessage()) const
Send to NetworkManager the error the subclass has found, the explanation is useful for debugging purp...
Capability
Capabilities to pass to secret agents.
Definition secretagent.h:60
@ NoCapability
No capability.
Definition secretagent.h:61
Capabilities capabilities()
This class allows querying the underlying system to discover the available network interfaces and rea...
Definition accesspoint.h:21
QDBusMessage createErrorReply(QDBusError::ErrorType type, const QString &msg) const const
MessageType type() const const
void serviceRegistered(const QString &serviceName)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString fromLatin1(QByteArrayView str)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.