| 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 |
#include "DBusMgr.h" |
| 19 |
|
| 20 |
#include "fdosecrets/objects/Collection.h" |
| 21 |
#include "fdosecrets/objects/Item.h" |
| 22 |
#include "fdosecrets/objects/Prompt.h" |
| 23 |
#include "fdosecrets/objects/Service.h" |
| 24 |
#include "fdosecrets/objects/Session.h" |
| 25 |
|
| 26 |
#include "core/Entry.h" |
| 27 |
#include "core/Tools.h" |
| 28 |
|
| 29 |
namespace FdoSecrets |
| 30 |
{ |
| 31 |
static const auto IntrospectionService = R"xml( |
| 32 |
<interface name="org.freedesktop.Secret.Service"> |
| 33 |
<property name="Collections" type="ao" access="read"/> |
| 34 |
<signal name="CollectionCreated"> |
| 35 |
<arg name="collection" type="o" direction="out"/> |
| 36 |
</signal> |
| 37 |
<signal name="CollectionDeleted"> |
| 38 |
<arg name="collection" type="o" direction="out"/> |
| 39 |
</signal> |
| 40 |
<signal name="CollectionChanged"> |
| 41 |
<arg name="collection" type="o" direction="out"/> |
| 42 |
</signal> |
| 43 |
<method name="OpenSession"> |
| 44 |
<arg type="v" direction="out"/> |
| 45 |
<arg name="algorithm" type="s" direction="in"/> |
| 46 |
<arg name="input" type="v" direction="in"/> |
| 47 |
<arg name="result" type="o" direction="out"/> |
| 48 |
</method> |
| 49 |
<method name="CreateCollection"> |
| 50 |
<arg type="o" direction="out"/> |
| 51 |
<arg name="properties" type="a{sv}" direction="in"/> |
| 52 |
<annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="QVariantMap"/> |
| 53 |
<arg name="alias" type="s" direction="in"/> |
| 54 |
<arg name="prompt" type="o" direction="out"/> |
| 55 |
</method> |
| 56 |
<method name="SearchItems"> |
| 57 |
<arg type="ao" direction="out"/> |
| 58 |
<arg name="attributes" type="a{ss}" direction="in"/> |
| 59 |
<annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="StringStringMap"/> |
| 60 |
<arg name="locked" type="ao" direction="out"/> |
| 61 |
</method> |
| 62 |
<method name="Unlock"> |
| 63 |
<arg type="ao" direction="out"/> |
| 64 |
<arg name="paths" type="ao" direction="in"/> |
| 65 |
<arg name="prompt" type="o" direction="out"/> |
| 66 |
</method> |
| 67 |
<method name="Lock"> |
| 68 |
<arg type="ao" direction="out"/> |
| 69 |
<arg name="paths" type="ao" direction="in"/> |
| 70 |
<arg name="prompt" type="o" direction="out"/> |
| 71 |
</method> |
| 72 |
<method name="GetSecrets"> |
| 73 |
<arg type="a{o(oayays)}" direction="out"/> |
| 74 |
<annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="ObjectPathSecretMap"/> |
| 75 |
<arg name="items" type="ao" direction="in"/> |
| 76 |
<arg name="session" type="o" direction="in"/> |
| 77 |
</method> |
| 78 |
<method name="ReadAlias"> |
| 79 |
<arg type="o" direction="out"/> |
| 80 |
<arg name="name" type="s" direction="in"/> |
| 81 |
</method> |
| 82 |
<method name="SetAlias"> |
| 83 |
<arg name="name" type="s" direction="in"/> |
| 84 |
<arg name="collection" type="o" direction="in"/> |
| 85 |
</method> |
| 86 |
</interface> |
| 87 |
)xml"; |
| 88 |
|
| 89 |
static const auto IntrospectionCollection = R"xml( |
| 90 |
<interface name="org.freedesktop.Secret.Collection"> |
| 91 |
<property name="Items" type="ao" access="read"/> |
| 92 |
<property name="Label" type="s" access="readwrite"/> |
| 93 |
<property name="Locked" type="b" access="read"/> |
| 94 |
<property name="Created" type="t" access="read"/> |
| 95 |
<property name="Modified" type="t" access="read"/> |
| 96 |
<signal name="ItemCreated"> |
| 97 |
<arg name="item" type="o" direction="out"/> |
| 98 |
</signal> |
| 99 |
<signal name="ItemDeleted"> |
| 100 |
<arg name="item" type="o" direction="out"/> |
| 101 |
</signal> |
| 102 |
<signal name="ItemChanged"> |
| 103 |
<arg name="item" type="o" direction="out"/> |
| 104 |
</signal> |
| 105 |
<method name="Delete"> |
| 106 |
<arg type="o" direction="out"/> |
| 107 |
</method> |
| 108 |
<method name="SearchItems"> |
| 109 |
<arg type="ao" direction="out"/> |
| 110 |
<arg name="attributes" type="a{ss}" direction="in"/> |
| 111 |
<annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="StringStringMap"/> |
| 112 |
</method> |
| 113 |
<method name="CreateItem"> |
| 114 |
<arg type="o" direction="out"/> |
| 115 |
<arg name="properties" type="a{sv}" direction="in"/> |
| 116 |
<annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="QVariantMap"/> |
| 117 |
<arg name="secret" type="(oayays)" direction="in"/> |
| 118 |
<annotation name="org.qtproject.QtDBus.QtTypeName.In1" value="FdoSecrets::wire::Secret"/> |
| 119 |
<arg name="replace" type="b" direction="in"/> |
| 120 |
<arg name="prompt" type="o" direction="out"/> |
| 121 |
</method> |
| 122 |
</interface> |
| 123 |
)xml"; |
| 124 |
|
| 125 |
static const auto IntrospectionItem = R"xml( |
| 126 |
<interface name="org.freedesktop.Secret.Item"> |
| 127 |
<property name="Locked" type="b" access="read"/> |
| 128 |
<property name="Attributes" type="a{ss}" access="readwrite"> |
| 129 |
<annotation name="org.qtproject.QtDBus.QtTypeName" value="StringStringMap"/> |
| 130 |
</property> |
| 131 |
<property name="Label" type="s" access="readwrite"/> |
| 132 |
<property name="Created" type="t" access="read"/> |
| 133 |
<property name="Modified" type="t" access="read"/> |
| 134 |
<method name="Delete"> |
| 135 |
<arg type="o" direction="out"/> |
| 136 |
</method> |
| 137 |
<method name="GetSecret"> |
| 138 |
<arg type="(oayays)" direction="out"/> |
| 139 |
<annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="FdoSecrets::wire::Secret"/> |
| 140 |
<arg name="session" type="o" direction="in"/> |
| 141 |
</method> |
| 142 |
<method name="SetSecret"> |
| 143 |
<arg name="secret" type="(oayays)" direction="in"/> |
| 144 |
<annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="FdoSecrets::wire::Secret"/> |
| 145 |
</method> |
| 146 |
</interface> |
| 147 |
)xml"; |
| 148 |
|
| 149 |
static const auto IntrospectionSession = R"xml( |
| 150 |
<interface name="org.freedesktop.Secret.Session"> |
| 151 |
<method name="Close"> |
| 152 |
</method> |
| 153 |
</interface> |
| 154 |
)xml"; |
| 155 |
|
| 156 |
static const auto IntrospectionPrompt = R"xml( |
| 157 |
<interface name="org.freedesktop.Secret.Prompt"> |
| 158 |
<signal name="Completed"> |
| 159 |
<arg name="dismissed" type="b" direction="out"/> |
| 160 |
<arg name="result" type="v" direction="out"/> |
| 161 |
</signal> |
| 162 |
<method name="Prompt"> |
| 163 |
<arg name="windowId" type="s" direction="in"/> |
| 164 |
</method> |
| 165 |
<method name="Dismiss"> |
| 166 |
</method> |
| 167 |
</interface> |
| 168 |
)xml"; |
| 169 |
|
| 170 |
// read /proc, each field except pid may be empty |
| 171 |
ProcInfo readProc(uint pid) |
| 172 |
{ |
| 173 |
ProcInfo info{}; |
| 174 |
info.pid = pid; |
| 175 |
|
| 176 |
// The /proc/pid/exe link is more reliable than /proc/pid/cmdline |
| 177 |
// It's still weak and if the application does a prctl(PR_SET_DUMPABLE, 0) this link cannot be accessed. |
| 178 |
QFileInfo exe(QStringLiteral("/proc/%1/exe").arg(pid)); |
| 179 |
info.exePath = exe.canonicalFilePath(); |
| 180 |
|
| 181 |
// /proc/pid/cmdline gives full command line |
| 182 |
QFile cmdline(QStringLiteral("/proc/%1/cmdline").arg(pid)); |
| 183 |
if (cmdline.open(QFile::ReadOnly)) { |
| 184 |
info.command = QString::fromLocal8Bit(cmdline.readAll().replace('\0', ' ')).trimmed(); |
| 185 |
} |
| 186 |
|
| 187 |
// /proc/pid/stat gives ppid, name |
| 188 |
QFile stat(QStringLiteral("/proc/%1/stat").arg(pid)); |
| 189 |
if (stat.open(QIODevice::ReadOnly)) { |
| 190 |
auto line = stat.readAll(); |
| 191 |
// find comm field without looking in what's inside as it's user controlled |
| 192 |
auto commStart = line.indexOf('('); |
| 193 |
auto commEnd = line.lastIndexOf(')'); |
| 194 |
if (commStart != -1 && commEnd != -1 && commStart < commEnd) { |
| 195 |
// start past '(', and subtract 2 from total length |
| 196 |
info.name = QString::fromLocal8Bit(line.mid(commStart + 1, commEnd + 1 - commStart - 2)); |
| 197 |
|
| 198 |
auto parts = line.mid(commEnd + 1).trimmed().split(' '); |
| 199 |
if (parts.size() >= 2) { |
| 200 |
info.ppid = parts[1].toUInt(); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return info; |
| 206 |
} |
| 207 |
|
| 208 |
DBusMgr::DBusMgr() |
| 209 |
: m_conn(QDBusConnection::sessionBus()) |
| 210 |
{ |
| 211 |
// remove client when it disappears on the bus |
| 212 |
m_watcher.setWatchMode(QDBusServiceWatcher::WatchForUnregistration); |
| 213 |
connect(&m_watcher, &QDBusServiceWatcher::serviceUnregistered, this, &DBusMgr::dbusServiceUnregistered); |
| 214 |
m_watcher.setConnection(m_conn); |
| 215 |
} |
| 216 |
|
| 217 |
void DBusMgr::populateMethodCache() |
| 218 |
{ |
| 219 |
// these are the methods we expose on DBus |
| 220 |
populateMethodCache(Service::staticMetaObject); |
| 221 |
populateMethodCache(Collection::staticMetaObject); |
| 222 |
populateMethodCache(Item::staticMetaObject); |
| 223 |
populateMethodCache(PromptBase::staticMetaObject); |
| 224 |
populateMethodCache(Session::staticMetaObject); |
| 225 |
} |
| 226 |
|
| 227 |
DBusMgr::~DBusMgr() = default; |
| 228 |
|
| 229 |
void DBusMgr::overrideClient(const DBusClientPtr& fake) |
| 230 |
{ |
| 231 |
m_overrideClient = fake; |
| 232 |
} |
| 233 |
|
| 234 |
QList<DBusClientPtr> DBusMgr::clients() const |
| 235 |
{ |
| 236 |
return m_clients.values(); |
| 237 |
} |
| 238 |
|
| 239 |
bool DBusMgr::serviceInfo(const QString& addr, PeerInfo& info) const |
| 240 |
{ |
| 241 |
info.address = addr; |
| 242 |
auto pid = m_conn.interface()->servicePid(addr); |
| 243 |
if (!pid.isValid()) { |
| 244 |
return false; |
| 245 |
} |
| 246 |
info.pid = pid.value(); |
| 247 |
|
| 248 |
// get the whole hierarchy |
| 249 |
uint ppid = info.pid; |
| 250 |
while (ppid != 0) { |
| 251 |
auto proc = readProc(ppid); |
| 252 |
info.hierarchy.append(proc); |
| 253 |
ppid = proc.ppid; |
| 254 |
} |
| 255 |
|
| 256 |
// check if the exe file is valid |
| 257 |
// (assuming for now that an accessible file is valid) |
| 258 |
info.valid = QFileInfo::exists(info.exePath()); |
| 259 |
|
| 260 |
// ask again to double-check and protect against pid reuse |
| 261 |
auto newPid = m_conn.interface()->servicePid(addr); |
| 262 |
if (!newPid.isValid() || newPid.value() != pid.value()) { |
| 263 |
// either the peer already gone or the pid changed to something else |
| 264 |
return false; |
| 265 |
} |
| 266 |
return true; |
| 267 |
} |
| 268 |
|
| 269 |
bool DBusMgr::sendDBusSignal(const QString& path, |
| 270 |
const QString& interface, |
| 271 |
const QString& name, |
| 272 |
const QVariantList& arguments) |
| 273 |
{ |
| 274 |
auto msg = QDBusMessage::createSignal(path, interface, name); |
| 275 |
msg.setArguments(arguments); |
| 276 |
return sendDBus(msg); |
| 277 |
} |
| 278 |
|
| 279 |
bool DBusMgr::sendDBus(const QDBusMessage& reply) |
| 280 |
{ |
| 281 |
bool ok = m_conn.send(reply); |
| 282 |
if (!ok) { |
| 283 |
qDebug() << "Failed to send on DBus:" << reply; |
| 284 |
emit error(tr("Failed to send reply on DBus")); |
| 285 |
} |
| 286 |
return ok; |
| 287 |
} |
| 288 |
|
| 289 |
// `this` object is registered at multiple paths: |
| 290 |
// /org/freedesktop/secrets |
| 291 |
// /org/freedesktop/secrets/collection/xxx |
| 292 |
// /org/freedesktop/secrets/collection/xxx/yyy |
| 293 |
// /org/freedesktop/secrets/aliases/xxx |
| 294 |
// /org/freedesktop/secrets/session/xxx |
| 295 |
// /org/freedesktop/secrets/prompt/xxx |
| 296 |
// |
| 297 |
// The path validation is left to Qt, this method only do the minimum |
| 298 |
// required to differentiate the paths. |
| 299 |
DBusMgr::ParsedPath DBusMgr::parsePath(const QString& path) |
| 300 |
{ |
| 301 |
Q_ASSERT(path.startsWith('/')); |
| 302 |
Q_ASSERT(path == "/" || !path.endsWith('/')); |
| 303 |
|
| 304 |
static const QString DBusPathSecrets = DBUS_PATH_SECRETS; |
| 305 |
|
| 306 |
if (!path.startsWith(DBusPathSecrets)) { |
| 307 |
return ParsedPath{}; |
| 308 |
} |
| 309 |
auto parts = path.mid(DBusPathSecrets.size()).split('/'); |
| 310 |
// the first part is always empty |
| 311 |
if (parts.isEmpty() || parts.first() != "") { |
| 312 |
return ParsedPath{}; |
| 313 |
} |
| 314 |
parts.takeFirst(); |
| 315 |
|
| 316 |
if (parts.isEmpty()) { |
| 317 |
return ParsedPath{PathType::Service}; |
| 318 |
} else if (parts.size() == 2) { |
| 319 |
if (parts.at(0) == "collection") { |
| 320 |
return ParsedPath{PathType::Collection, parts.at(1)}; |
| 321 |
} else if (parts.at(0) == "aliases") { |
| 322 |
return ParsedPath{PathType::Aliases, parts.at(1)}; |
| 323 |
} else if (parts.at(0) == "prompt") { |
| 324 |
return ParsedPath{PathType::Prompt, parts.at(1)}; |
| 325 |
} else if (parts.at(0) == "session") { |
| 326 |
return ParsedPath{PathType::Session, parts.at(1)}; |
| 327 |
} |
| 328 |
} else if (parts.size() == 3) { |
| 329 |
if (parts.at(0) == "collection") { |
| 330 |
return ParsedPath{PathType::Item, parts.at(2), parts.at(1)}; |
| 331 |
} |
| 332 |
} |
| 333 |
return ParsedPath{}; |
| 334 |
} |
| 335 |
|
| 336 |
QString DBusMgr::introspect(const QString& path) const |
| 337 |
{ |
| 338 |
auto parsed = parsePath(path); |
| 339 |
switch (parsed.type) { |
| 340 |
case PathType::Service: |
| 341 |
return IntrospectionService; |
| 342 |
case PathType::Collection: |
| 343 |
case PathType::Aliases: |
| 344 |
return IntrospectionCollection; |
| 345 |
case PathType::Prompt: |
| 346 |
return IntrospectionPrompt; |
| 347 |
case PathType::Session: |
| 348 |
return IntrospectionSession; |
| 349 |
case PathType::Item: |
| 350 |
return IntrospectionItem; |
| 351 |
case PathType::Unknown: |
| 352 |
default: |
| 353 |
return ""; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
bool DBusMgr::serviceOccupied() const |
| 358 |
{ |
| 359 |
auto reply = m_conn.interface()->isServiceRegistered(DBUS_SERVICE_SECRET); |
| 360 |
if (!reply.isValid()) { |
| 361 |
return false; |
| 362 |
} |
| 363 |
if (reply.value()) { |
| 364 |
auto pid = m_conn.interface()->servicePid(DBUS_SERVICE_SECRET); |
| 365 |
if (pid.isValid() && pid.value() != qApp->applicationPid()) { |
| 366 |
return true; |
| 367 |
} |
| 368 |
} |
| 369 |
return false; |
| 370 |
} |
| 371 |
|
| 372 |
QString DBusMgr::reportExistingService() const |
| 373 |
{ |
| 374 |
auto pidStr = tr("Unknown", "Unknown PID"); |
| 375 |
auto exeStr = tr("Unknown", "Unknown executable path"); |
| 376 |
|
| 377 |
PeerInfo info{}; |
| 378 |
if (serviceInfo(DBUS_SERVICE_SECRET, info)) { |
| 379 |
pidStr = QString::number(info.pid); |
| 380 |
if (!info.exePath().isEmpty()) { |
| 381 |
exeStr = info.exePath(); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
auto otherService = tr("<i>PID: %1, Executable: %2</i>", "<i>PID: 1234, Executable: /path/to/exe</i>") |
| 386 |
.arg(pidStr, exeStr.toHtmlEscaped()); |
| 387 |
return tr("Another secret service is running (%1).<br/>" |
| 388 |
"Please stop/remove it before re-enabling the Secret Service Integration.") |
| 389 |
.arg(otherService); |
| 390 |
} |
| 391 |
|
| 392 |
bool DBusMgr::registerObject(const QString& path, DBusObject* obj, bool primary) |
| 393 |
{ |
| 394 |
if (!m_conn.registerVirtualObject(path, this)) { |
| 395 |
qDebug() << "failed to register" << obj << "at" << path; |
| 396 |
return false; |
| 397 |
} |
| 398 |
connect(obj, &DBusObject::destroyed, this, &DBusMgr::unregisterObject); |
| 399 |
m_objects.insert(path, obj); |
| 400 |
if (primary) { |
| 401 |
obj->setObjectPath(path); |
| 402 |
} |
| 403 |
return true; |
| 404 |
} |
| 405 |
|
| 406 |
bool DBusMgr::registerObject(Service* service) |
| 407 |
{ |
| 408 |
if (!m_conn.registerService(DBUS_SERVICE_SECRET)) { |
| 409 |
const auto existing = reportExistingService(); |
| 410 |
qDebug() << "Failed to register DBus service at " << DBUS_SERVICE_SECRET; |
| 411 |
qDebug() << existing; |
| 412 |
emit error(tr("Failed to register DBus service at %1.<br/>").arg(DBUS_SERVICE_SECRET) + existing); |
| 413 |
return false; |
| 414 |
} |
| 415 |
connect(service, &DBusObject::destroyed, this, [this]() { m_conn.unregisterService(DBUS_SERVICE_SECRET); }); |
| 416 |
|
| 417 |
if (!registerObject(DBUS_PATH_SECRETS, service)) { |
| 418 |
qDebug() << "Failed to register service on DBus at path" << DBUS_PATH_SECRETS; |
| 419 |
emit error(tr("Failed to register service on DBus at path '%1'").arg(DBUS_PATH_SECRETS)); |
| 420 |
return false; |
| 421 |
} |
| 422 |
|
| 423 |
connect(service, &Service::collectionCreated, this, &DBusMgr::emitCollectionCreated); |
| 424 |
connect(service, &Service::collectionChanged, this, &DBusMgr::emitCollectionChanged); |
| 425 |
connect(service, &Service::collectionDeleted, this, &DBusMgr::emitCollectionDeleted); |
| 426 |
|
| 427 |
return true; |
| 428 |
} |
| 429 |
|
| 430 |
bool DBusMgr::registerObject(Collection* coll) |
| 431 |
{ |
| 432 |
auto name = encodePath(coll->name()); |
| 433 |
auto path = DBUS_PATH_TEMPLATE_COLLECTION.arg(DBUS_PATH_SECRETS, name); |
| 434 |
if (!registerObject(path, coll)) { |
| 435 |
// try again with a suffix |
| 436 |
name.append(QString("_%1").arg(Tools::uuidToHex(QUuid::createUuid()).left(4))); |
| 437 |
path = DBUS_PATH_TEMPLATE_COLLECTION.arg(DBUS_PATH_SECRETS, name); |
| 438 |
|
| 439 |
if (!registerObject(path, coll)) { |
| 440 |
qDebug() << "Failed to register database on DBus under name" << name; |
| 441 |
emit error(tr("Failed to register database on DBus under the name '%1'").arg(name)); |
| 442 |
return false; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
connect(coll, &Collection::itemCreated, this, &DBusMgr::emitItemCreated); |
| 447 |
connect(coll, &Collection::itemChanged, this, &DBusMgr::emitItemChanged); |
| 448 |
connect(coll, &Collection::itemDeleted, this, &DBusMgr::emitItemDeleted); |
| 449 |
|
| 450 |
return true; |
| 451 |
} |
| 452 |
|
| 453 |
bool DBusMgr::registerObject(Session* sess) |
| 454 |
{ |
| 455 |
auto path = DBUS_PATH_TEMPLATE_SESSION.arg(DBUS_PATH_SECRETS, sess->id()); |
| 456 |
if (!registerObject(path, sess)) { |
| 457 |
emit error(tr("Failed to register session on DBus at path '%1'").arg(path)); |
| 458 |
return false; |
| 459 |
} |
| 460 |
return true; |
| 461 |
} |
| 462 |
|
| 463 |
bool DBusMgr::registerObject(Item* item) |
| 464 |
{ |
| 465 |
auto path = DBUS_PATH_TEMPLATE_ITEM.arg(item->collection()->objectPath().path(), item->backend()->uuidToHex()); |
| 466 |
if (!registerObject(path, item)) { |
| 467 |
emit error(tr("Failed to register item on DBus at path '%1'").arg(path)); |
| 468 |
return false; |
| 469 |
} |
| 470 |
return true; |
| 471 |
} |
| 472 |
|
| 473 |
bool DBusMgr::registerObject(PromptBase* prompt) |
| 474 |
{ |
| 475 |
auto path = DBUS_PATH_TEMPLATE_PROMPT.arg(DBUS_PATH_SECRETS, Tools::uuidToHex(QUuid::createUuid())); |
| 476 |
if (!registerObject(path, prompt)) { |
| 477 |
emit error(tr("Failed to register prompt object on DBus at path '%1'").arg(path)); |
| 478 |
return false; |
| 479 |
} |
| 480 |
|
| 481 |
connect(prompt, &PromptBase::completed, this, &DBusMgr::emitPromptCompleted); |
| 482 |
|
| 483 |
return true; |
| 484 |
} |
| 485 |
|
| 486 |
void DBusMgr::unregisterObject(DBusObject* obj) |
| 487 |
{ |
| 488 |
auto count = m_objects.remove(obj->objectPath().path()); |
| 489 |
if (count > 0) { |
| 490 |
m_conn.unregisterObject(obj->objectPath().path()); |
| 491 |
obj->setObjectPath("/"); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
bool DBusMgr::registerAlias(Collection* coll, const QString& alias) |
| 496 |
{ |
| 497 |
auto path = DBUS_PATH_TEMPLATE_ALIAS.arg(DBUS_PATH_SECRETS, alias); |
| 498 |
if (!registerObject(path, coll, false)) { |
| 499 |
qDebug() << "Failed to register database on DBus under alias" << alias; |
| 500 |
// usually this is reported back directly on dbus, so no need to show in UI |
| 501 |
return false; |
| 502 |
} |
| 503 |
// alias signals are handled together with collections' primary path in emitCollection* |
| 504 |
// but we need to handle object destroy here |
| 505 |
connect(coll, &DBusObject::destroyed, this, [this, alias]() { unregisterAlias(alias); }); |
| 506 |
return true; |
| 507 |
} |
| 508 |
|
| 509 |
void DBusMgr::unregisterAlias(const QString& alias) |
| 510 |
{ |
| 511 |
auto path = DBUS_PATH_TEMPLATE_ALIAS.arg(DBUS_PATH_SECRETS, alias); |
| 512 |
// DBusMgr::unregisterObject only handles primary path |
| 513 |
m_objects.remove(path); |
| 514 |
m_conn.unregisterObject(path); |
| 515 |
} |
| 516 |
|
| 517 |
void DBusMgr::emitCollectionCreated(Collection* coll) |
| 518 |
{ |
| 519 |
QVariantList args; |
| 520 |
args += QVariant::fromValue(coll->objectPath()); |
| 521 |
sendDBusSignal(DBUS_PATH_SECRETS, DBUS_INTERFACE_SECRET_SERVICE, QStringLiteral("CollectionCreated"), args); |
| 522 |
} |
| 523 |
|
| 524 |
void DBusMgr::emitCollectionChanged(Collection* coll) |
| 525 |
{ |
| 526 |
QVariantList args; |
| 527 |
args += QVariant::fromValue(coll->objectPath()); |
| 528 |
sendDBusSignal(DBUS_PATH_SECRETS, DBUS_INTERFACE_SECRET_SERVICE, "CollectionChanged", args); |
| 529 |
} |
| 530 |
|
| 531 |
void DBusMgr::emitCollectionDeleted(Collection* coll) |
| 532 |
{ |
| 533 |
QVariantList args; |
| 534 |
args += QVariant::fromValue(coll->objectPath()); |
| 535 |
sendDBusSignal(DBUS_PATH_SECRETS, DBUS_INTERFACE_SECRET_SERVICE, QStringLiteral("CollectionDeleted"), args); |
| 536 |
} |
| 537 |
|
| 538 |
void DBusMgr::emitItemCreated(Item* item) |
| 539 |
{ |
| 540 |
auto coll = item->collection(); |
| 541 |
QVariantList args; |
| 542 |
args += QVariant::fromValue(item->objectPath()); |
| 543 |
// send on primary path |
| 544 |
sendDBusSignal( |
| 545 |
coll->objectPath().path(), DBUS_INTERFACE_SECRET_COLLECTION, QStringLiteral("ItemCreated"), args); |
| 546 |
// also send on all alias path |
| 547 |
for (const auto& alias : coll->aliases()) { |
| 548 |
auto path = DBUS_PATH_TEMPLATE_ALIAS.arg(DBUS_PATH_SECRETS, alias); |
| 549 |
sendDBusSignal(path, DBUS_INTERFACE_SECRET_COLLECTION, QStringLiteral("ItemCreated"), args); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
void DBusMgr::emitItemChanged(Item* item) |
| 554 |
{ |
| 555 |
auto coll = item->collection(); |
| 556 |
QVariantList args; |
| 557 |
args += QVariant::fromValue(item->objectPath()); |
| 558 |
// send on primary path |
| 559 |
sendDBusSignal( |
| 560 |
coll->objectPath().path(), DBUS_INTERFACE_SECRET_COLLECTION, QStringLiteral("ItemChanged"), args); |
| 561 |
// also send on all alias path |
| 562 |
for (const auto& alias : coll->aliases()) { |
| 563 |
auto path = DBUS_PATH_TEMPLATE_ALIAS.arg(DBUS_PATH_SECRETS, alias); |
| 564 |
sendDBusSignal(path, DBUS_INTERFACE_SECRET_COLLECTION, QStringLiteral("ItemChanged"), args); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
void DBusMgr::emitItemDeleted(Item* item) |
| 569 |
{ |
| 570 |
auto coll = item->collection(); |
| 571 |
QVariantList args; |
| 572 |
args += QVariant::fromValue(item->objectPath()); |
| 573 |
// send on primary path |
| 574 |
sendDBusSignal( |
| 575 |
coll->objectPath().path(), DBUS_INTERFACE_SECRET_COLLECTION, QStringLiteral("ItemDeleted"), args); |
| 576 |
// also send on all alias path |
| 577 |
for (const auto& alias : coll->aliases()) { |
| 578 |
auto path = DBUS_PATH_TEMPLATE_ALIAS.arg(DBUS_PATH_SECRETS, alias); |
| 579 |
sendDBusSignal(path, DBUS_INTERFACE_SECRET_COLLECTION, QStringLiteral("ItemDeleted"), args); |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
void DBusMgr::emitPromptCompleted(bool dismissed, QVariant result) |
| 584 |
{ |
| 585 |
auto prompt = qobject_cast<PromptBase*>(sender()); |
| 586 |
if (!prompt) { |
| 587 |
qDebug() << "Wrong sender in emitPromptCompleted"; |
| 588 |
return; |
| 589 |
} |
| 590 |
|
| 591 |
// make sure the result contains a valid value, otherwise QDBusVariant refuses to marshall it. |
| 592 |
if (!result.isValid()) { |
| 593 |
result = QString{}; |
| 594 |
} |
| 595 |
|
| 596 |
QVariantList args; |
| 597 |
args += QVariant::fromValue(dismissed); |
| 598 |
args += QVariant::fromValue(QDBusVariant(result)); |
| 599 |
sendDBusSignal(prompt->objectPath().path(), DBUS_INTERFACE_SECRET_PROMPT, QStringLiteral("Completed"), args); |
| 600 |
} |
| 601 |
|
| 602 |
DBusClientPtr DBusMgr::findClient(const QString& addr) |
| 603 |
{ |
| 604 |
if (m_overrideClient) { |
| 605 |
return m_overrideClient; |
| 606 |
} |
| 607 |
|
| 608 |
auto it = m_clients.find(addr); |
| 609 |
if (it == m_clients.end()) { |
| 610 |
auto client = createClient(addr); |
| 611 |
if (!client) { |
| 612 |
return {}; |
| 613 |
} |
| 614 |
it = m_clients.insert(addr, client); |
| 615 |
} |
| 616 |
return it.value(); |
| 617 |
} |
| 618 |
|
| 619 |
DBusClientPtr DBusMgr::createClient(const QString& addr) |
| 620 |
{ |
| 621 |
PeerInfo info{}; |
| 622 |
if (!serviceInfo(addr, info)) { |
| 623 |
return {}; |
| 624 |
} |
| 625 |
|
| 626 |
auto client = DBusClientPtr(new DBusClient(this, info)); |
| 627 |
|
| 628 |
emit clientConnected(client); |
| 629 |
m_watcher.addWatchedService(addr); |
| 630 |
|
| 631 |
return client; |
| 632 |
} |
| 633 |
|
| 634 |
void DBusMgr::removeClient(DBusClient* client) |
| 635 |
{ |
| 636 |
if (!client) { |
| 637 |
return; |
| 638 |
} |
| 639 |
|
| 640 |
auto it = m_clients.find(client->address()); |
| 641 |
if (it == m_clients.end()) { |
| 642 |
return; |
| 643 |
} |
| 644 |
|
| 645 |
emit clientDisconnected(*it); |
| 646 |
m_clients.erase(it); |
| 647 |
} |
| 648 |
|
| 649 |
void DBusMgr::dbusServiceUnregistered(const QString& service) |
| 650 |
{ |
| 651 |
auto removed = m_watcher.removeWatchedService(service); |
| 652 |
if (!removed) { |
| 653 |
qDebug("FdoSecrets: Failed to remove service watcher"); |
| 654 |
} |
| 655 |
|
| 656 |
auto it = m_clients.find(service); |
| 657 |
if (it == m_clients.end()) { |
| 658 |
return; |
| 659 |
} |
| 660 |
auto client = it.value(); |
| 661 |
|
| 662 |
// client will notify DBusMgr and call DBusMgr::removeClient |
| 663 |
client->disconnectDBus(); |
| 664 |
} |
| 665 |
} // namespace FdoSecrets |