| 1 |
/* |
| 2 |
* Copyright (C) 2020 Aetf <[email protected]> |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 2 or (at your option) |
| 7 |
* version 3 of the License. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#ifndef KEEPASSXC_FDOSECRETS_DBUSMGR_H |
| 19 |
#define KEEPASSXC_FDOSECRETS_DBUSMGR_H |
| 20 |
|
| 21 |
#include "fdosecrets/dbus/DBusClient.h" |
| 22 |
|
| 23 |
#include <QDBusConnection> |
| 24 |
#include <QDBusServiceWatcher> |
| 25 |
#include <QDBusVirtualObject> |
| 26 |
#include <QDebug> |
| 27 |
#include <QtDBus> |
| 28 |
|
| 29 |
class TestFdoSecrets; |
| 30 |
|
| 31 |
namespace FdoSecrets |
| 32 |
{ |
| 33 |
class Collection; |
| 34 |
class Service; |
| 35 |
class PromptBase; |
| 36 |
class Session; |
| 37 |
class Item; |
| 38 |
class DBusObject; |
| 39 |
class DBusResult; |
| 40 |
|
| 41 |
/** |
| 42 |
* DBusMgr takes care of the interaction between dbus and business logic objects (DBusObject). It handles the |
| 43 |
* following |
| 44 |
* - Registering/unregistering service name |
| 45 |
* - Registering/unregistering paths |
| 46 |
* - Relay signals from DBusObject to dbus |
| 47 |
* - Manage per-client states, mapping from dbus caller address to Client |
| 48 |
* - Deliver method calls from dbus to DBusObject |
| 49 |
* |
| 50 |
* Special note in implementation of method delivery: |
| 51 |
* There are two sets of vocabulary classes in use for method delivery. |
| 52 |
* The Qt DBus system uses QDBusVariant/QDBusObjectPath and other primitive types in QDBusMessage::arguments(), |
| 53 |
* i.e. the on-the-wire types. |
| 54 |
* The DBusObject invokable methods uses QVariant/DBusObject* and other primitive types in parameters (parameter |
| 55 |
* types). FdoSecrets::typeToWireType establishes the mapping from parameter types to on-the-wire types. The |
| 56 |
* conversion between types is done with the help of QMetaType convert. |
| 57 |
* |
| 58 |
* The method delivery sequence: |
| 59 |
* - DBusMgr::handleMessage unifies method call and property access into the same form |
| 60 |
* - DBusMgr::activateObject finds the target object and calls the method by doing the following |
| 61 |
* * check the object exists and the interface matches |
| 62 |
* * find the cached method information MethodData |
| 63 |
* * DBusMgr::prepareInputParams check and convert input arguments in QDBusMessage::arguments() to types expected |
| 64 |
* by DBusObject |
| 65 |
* * prepare output argument storage |
| 66 |
* * call the method |
| 67 |
* * convert types to what Qt DBus expects |
| 68 |
* |
| 69 |
* The MethodData is pre-computed using Qt meta object system by finding methods with signature matching a certain |
| 70 |
* pattern: |
| 71 |
* Q_INVOKABLE DBusResult methodName(const DBusClientPtr& client, |
| 72 |
* const X& input1, |
| 73 |
* const Y& input2, |
| 74 |
* Z& output1, |
| 75 |
* ZZ& output2) |
| 76 |
* Note that the first parameter of client is optional. |
| 77 |
*/ |
| 78 |
class DBusMgr : public QDBusVirtualObject |
| 79 |
{ |
| 80 |
Q_OBJECT |
| 81 |
public: |
| 82 |
explicit DBusMgr(); |
| 83 |
|
| 84 |
/** |
| 85 |
* @brief Must be called after all dbus types are registered |
| 86 |
*/ |
| 87 |
void populateMethodCache(); |
| 88 |
|
| 89 |
~DBusMgr() override; |
| 90 |
|
| 91 |
QString introspect(const QString& path) const override; |
| 92 |
bool handleMessage(const QDBusMessage& message, const QDBusConnection& connection) override; |
| 93 |
|
| 94 |
/** |
| 95 |
* @return current connected clients |
| 96 |
*/ |
| 97 |
QList<DBusClientPtr> clients() const; |
| 98 |
|
| 99 |
/** |
| 100 |
* @return whether the org.freedesktop.secrets service is owned by others |
| 101 |
*/ |
| 102 |
bool serviceOccupied() const; |
| 103 |
|
| 104 |
/** |
| 105 |
* Check the running secret service and return info about it |
| 106 |
* @return html string suitable to be shown in the UI |
| 107 |
*/ |
| 108 |
QString reportExistingService() const; |
| 109 |
|
| 110 |
// expose on dbus and handle signals |
| 111 |
bool registerObject(Service* service); |
| 112 |
bool registerObject(Collection* coll); |
| 113 |
bool registerObject(Session* sess); |
| 114 |
bool registerObject(Item* item); |
| 115 |
bool registerObject(PromptBase* prompt); |
| 116 |
|
| 117 |
void unregisterObject(DBusObject* obj); |
| 118 |
|
| 119 |
// and the signals are handled together with collection's primary path |
| 120 |
bool registerAlias(Collection* coll, const QString& alias); |
| 121 |
void unregisterAlias(const QString& alias); |
| 122 |
|
| 123 |
/** |
| 124 |
* Return the object path of the pointed DBusObject, or "/" if the pointer is null |
| 125 |
* @tparam T |
| 126 |
* @param object |
| 127 |
* @return |
| 128 |
*/ |
| 129 |
template <typename T> static QDBusObjectPath objectPathSafe(T* object) |
| 130 |
{ |
| 131 |
if (object) { |
| 132 |
return object->objectPath(); |
| 133 |
} |
| 134 |
return QDBusObjectPath(QStringLiteral("/")); |
| 135 |
} |
| 136 |
template <typename T> static QDBusObjectPath objectPathSafe(QPointer<T> object) |
| 137 |
{ |
| 138 |
return objectPathSafe(object.data()); |
| 139 |
} |
| 140 |
static QDBusObjectPath objectPathSafe(std::nullptr_t) |
| 141 |
{ |
| 142 |
return QDBusObjectPath(QStringLiteral("/")); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Convert a list of DBusObjects to object path |
| 147 |
* @tparam T |
| 148 |
* @param objects |
| 149 |
* @return |
| 150 |
*/ |
| 151 |
template <typename T> static QList<QDBusObjectPath> objectsToPath(QList<T*> objects) |
| 152 |
{ |
| 153 |
QList<QDBusObjectPath> res; |
| 154 |
res.reserve(objects.size()); |
| 155 |
for (auto object : objects) { |
| 156 |
res.append(objectPathSafe(object)); |
| 157 |
} |
| 158 |
return res; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Convert an object path to a pointer of the object |
| 163 |
* @tparam T |
| 164 |
* @param path |
| 165 |
* @return the pointer of the object, or nullptr if path is "/" |
| 166 |
*/ |
| 167 |
template <typename T> T* pathToObject(const QDBusObjectPath& path) const |
| 168 |
{ |
| 169 |
if (path.path() == QStringLiteral("/")) { |
| 170 |
return nullptr; |
| 171 |
} |
| 172 |
auto obj = qobject_cast<T*>(m_objects.value(path.path(), nullptr)); |
| 173 |
if (!obj) { |
| 174 |
qDebug() << "object not found at path" << path.path(); |
| 175 |
qDebug() << m_objects; |
| 176 |
} |
| 177 |
return obj; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Convert a list of object paths to a list of objects. |
| 182 |
* "/" paths (i.e. nullptrs) will be skipped in the resulting list |
| 183 |
* @tparam T |
| 184 |
* @param paths |
| 185 |
* @return |
| 186 |
*/ |
| 187 |
template <typename T> QList<T*> pathsToObject(const QList<QDBusObjectPath>& paths) const |
| 188 |
{ |
| 189 |
QList<T*> res; |
| 190 |
res.reserve(paths.size()); |
| 191 |
for (const auto& path : paths) { |
| 192 |
auto object = pathToObject<T>(path); |
| 193 |
if (object) { |
| 194 |
res.append(object); |
| 195 |
} |
| 196 |
} |
| 197 |
return res; |
| 198 |
} |
| 199 |
|
| 200 |
// Force client to be a specific object, used for testing |
| 201 |
void overrideClient(const DBusClientPtr& fake); |
| 202 |
|
| 203 |
signals: |
| 204 |
void clientConnected(const DBusClientPtr& client); |
| 205 |
void clientDisconnected(const DBusClientPtr& client); |
| 206 |
void error(const QString& msg); |
| 207 |
|
| 208 |
private slots: |
| 209 |
void emitCollectionCreated(Collection* coll); |
| 210 |
void emitCollectionChanged(Collection* coll); |
| 211 |
void emitCollectionDeleted(Collection* coll); |
| 212 |
void emitItemCreated(Item* item); |
| 213 |
void emitItemChanged(Item* item); |
| 214 |
void emitItemDeleted(Item* item); |
| 215 |
void emitPromptCompleted(bool dismissed, QVariant result); |
| 216 |
|
| 217 |
void dbusServiceUnregistered(const QString& service); |
| 218 |
|
| 219 |
private: |
| 220 |
QDBusConnection m_conn; |
| 221 |
|
| 222 |
bool serviceInfo(const QString& addr, PeerInfo& info) const; |
| 223 |
|
| 224 |
bool sendDBusSignal(const QString& path, |
| 225 |
const QString& interface, |
| 226 |
const QString& name, |
| 227 |
const QVariantList& arguments); |
| 228 |
bool sendDBus(const QDBusMessage& reply); |
| 229 |
|
| 230 |
// object path registration |
| 231 |
QHash<QString, QPointer<DBusObject>> m_objects{}; |
| 232 |
enum class PathType |
| 233 |
{ |
| 234 |
Service, |
| 235 |
Collection, |
| 236 |
Aliases, |
| 237 |
Prompt, |
| 238 |
Session, |
| 239 |
Item, |
| 240 |
Unknown, |
| 241 |
}; |
| 242 |
struct ParsedPath |
| 243 |
{ |
| 244 |
PathType type; |
| 245 |
QString id; |
| 246 |
// only used when type == Item |
| 247 |
QString parentId; |
| 248 |
explicit ParsedPath(PathType type = PathType::Unknown, QString id = "", QString parentId = "") |
| 249 |
: type(type) |
| 250 |
, id(std::move(id)) |
| 251 |
, parentId(std::move(parentId)) |
| 252 |
{ |
| 253 |
} |
| 254 |
}; |
| 255 |
static ParsedPath parsePath(const QString& path); |
| 256 |
bool registerObject(const QString& path, DBusObject* obj, bool primary = true); |
| 257 |
|
| 258 |
// method dispatching |
| 259 |
struct MethodData |
| 260 |
{ |
| 261 |
int slotIdx{-1}; |
| 262 |
QByteArray signature{}; |
| 263 |
QVector<int> inputTypes{}; |
| 264 |
QVector<int> outputTypes{}; |
| 265 |
QVector<int> outputTargetTypes{}; |
| 266 |
bool isProperty{false}; |
| 267 |
bool needsCallingClient{false}; |
| 268 |
}; |
| 269 |
QHash<QString, MethodData> m_cachedMethods{}; |
| 270 |
void populateMethodCache(const QMetaObject& mo); |
| 271 |
|
| 272 |
enum class RequestType |
| 273 |
{ |
| 274 |
Method, |
| 275 |
PropertyGet, |
| 276 |
PropertyGetAll, |
| 277 |
}; |
| 278 |
struct RequestedMethod |
| 279 |
{ |
| 280 |
QString interface; |
| 281 |
QString member; |
| 282 |
QString signature; |
| 283 |
QVariantList args; |
| 284 |
RequestType type; |
| 285 |
}; |
| 286 |
static bool rewriteRequestForProperty(RequestedMethod& req); |
| 287 |
bool activateObject(const DBusClientPtr& client, |
| 288 |
const QString& path, |
| 289 |
const RequestedMethod& req, |
| 290 |
const QDBusMessage& msg); |
| 291 |
bool objectPropertyGetAll(const DBusClientPtr& client, |
| 292 |
DBusObject* obj, |
| 293 |
const QString& interface, |
| 294 |
const QDBusMessage& msg); |
| 295 |
static bool deliverMethod(const DBusClientPtr& client, |
| 296 |
DBusObject* obj, |
| 297 |
const MethodData& method, |
| 298 |
const QVariantList& args, |
| 299 |
DBusResult& ret, |
| 300 |
QVariantList& outputArgs); |
| 301 |
|
| 302 |
// client management |
| 303 |
friend class DBusClient; |
| 304 |
|
| 305 |
DBusClientPtr findClient(const QString& addr); |
| 306 |
DBusClientPtr createClient(const QString& addr); |
| 307 |
|
| 308 |
/** |
| 309 |
* @brief This gets called from DBusClient::disconnectDBus |
| 310 |
* @param client |
| 311 |
*/ |
| 312 |
void removeClient(DBusClient* client); |
| 313 |
|
| 314 |
QDBusServiceWatcher m_watcher{}; |
| 315 |
// mapping from the unique dbus peer address to client object |
| 316 |
QHash<QString, DBusClientPtr> m_clients{}; |
| 317 |
|
| 318 |
DBusClientPtr m_overrideClient; |
| 319 |
|
| 320 |
friend class ::TestFdoSecrets; |
| 321 |
}; |
| 322 |
} // namespace FdoSecrets |
| 323 |
|
| 324 |
#endif // KEEPASSXC_FDOSECRETS_DBUSMGR_H |