| 1 |
/* |
| 2 |
* Copyright (C) 2019 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 "Prompt.h" |
| 19 |
|
| 20 |
#include "fdosecrets/objects/Collection.h" |
| 21 |
#include "fdosecrets/objects/Item.h" |
| 22 |
#include "fdosecrets/objects/Service.h" |
| 23 |
#include "fdosecrets/objects/Session.h" |
| 24 |
#include "fdosecrets/widgets/AccessControlDialog.h" |
| 25 |
|
| 26 |
#include "FdoSecretsSettings.h" |
| 27 |
#include "core/Entry.h" |
| 28 |
#include "gui/MessageBox.h" |
| 29 |
|
| 30 |
#include <QThread> |
| 31 |
#include <QTimer> |
| 32 |
#include <QWindow> |
| 33 |
|
| 34 |
namespace FdoSecrets |
| 35 |
{ |
| 36 |
const PromptResult PromptResult::Pending{PromptResult::AsyncPending}; |
| 37 |
|
| 38 |
PromptBase::PromptBase(Service* parent) |
| 39 |
: DBusObject(parent) |
| 40 |
{ |
| 41 |
connect(this, &PromptBase::completed, this, &PromptBase::deleteLater); |
| 42 |
} |
| 43 |
|
| 44 |
QWindow* PromptBase::findWindow(const QString& windowId) |
| 45 |
{ |
| 46 |
// find parent window, or nullptr if not found |
| 47 |
bool ok = false; |
| 48 |
WId wid = windowId.toULongLong(&ok, 0); |
| 49 |
QWindow* parent = nullptr; |
| 50 |
if (ok) { |
| 51 |
parent = QWindow::fromWinId(wid); |
| 52 |
} |
| 53 |
if (parent) { |
| 54 |
// parent is not the child of any object, so make sure it gets deleted at some point |
| 55 |
QObject::connect(this, &QObject::destroyed, parent, &QObject::deleteLater); |
| 56 |
} |
| 57 |
return parent; |
| 58 |
} |
| 59 |
|
| 60 |
Service* PromptBase::service() const |
| 61 |
{ |
| 62 |
return qobject_cast<Service*>(parent()); |
| 63 |
} |
| 64 |
|
| 65 |
DBusResult PromptBase::prompt(const DBusClientPtr& client, const QString& windowId) |
| 66 |
{ |
| 67 |
if (thread() != QThread::currentThread()) { |
| 68 |
DBusResult ret; |
| 69 |
QMetaObject::invokeMethod( |
| 70 |
this, "prompt", Qt::BlockingQueuedConnection, Q_ARG(QString, windowId), Q_RETURN_ARG(DBusResult, ret)); |
| 71 |
return ret; |
| 72 |
} |
| 73 |
|
| 74 |
QWeakPointer<DBusClient> weak = client; |
| 75 |
// execute the actual prompt method in event loop to avoid block this method |
| 76 |
QTimer::singleShot(0, this, [this, weak, windowId]() { |
| 77 |
auto c = weak.lock(); |
| 78 |
if (!c) { |
| 79 |
return; |
| 80 |
} |
| 81 |
if (m_signalSent) { |
| 82 |
return; |
| 83 |
} |
| 84 |
auto res = promptSync(c, windowId); |
| 85 |
if (!res.isPending()) { |
| 86 |
finishPrompt(res.isDismiss()); |
| 87 |
} |
| 88 |
}); |
| 89 |
return {}; |
| 90 |
} |
| 91 |
|
| 92 |
DBusResult PromptBase::dismiss() |
| 93 |
{ |
| 94 |
finishPrompt(true); |
| 95 |
return {}; |
| 96 |
} |
| 97 |
|
| 98 |
QVariant PromptBase::currentResult() const |
| 99 |
{ |
| 100 |
return ""; |
| 101 |
} |
| 102 |
|
| 103 |
void PromptBase::finishPrompt(bool dismissed) |
| 104 |
{ |
| 105 |
if (m_signalSent) { |
| 106 |
return; |
| 107 |
} |
| 108 |
m_signalSent = true; |
| 109 |
emit completed(dismissed, currentResult()); |
| 110 |
} |
| 111 |
|
| 112 |
DeleteCollectionPrompt::DeleteCollectionPrompt(Service* parent, Collection* coll) |
| 113 |
: PromptBase(parent) |
| 114 |
, m_collection(coll) |
| 115 |
{ |
| 116 |
} |
| 117 |
|
| 118 |
PromptResult DeleteCollectionPrompt::promptSync(const DBusClientPtr&, const QString& windowId) |
| 119 |
{ |
| 120 |
MessageBox::OverrideParent override(findWindow(windowId)); |
| 121 |
|
| 122 |
// if m_collection is already gone then treat as deletion accepted |
| 123 |
auto accepted = true; |
| 124 |
if (m_collection) { |
| 125 |
accepted = m_collection->doDelete(); |
| 126 |
} |
| 127 |
return PromptResult::accepted(accepted); |
| 128 |
} |
| 129 |
|
| 130 |
CreateCollectionPrompt::CreateCollectionPrompt(Service* parent, QVariantMap properties, QString alias) |
| 131 |
: PromptBase(parent) |
| 132 |
, m_properties(std::move(properties)) |
| 133 |
, m_alias(std::move(alias)) |
| 134 |
{ |
| 135 |
} |
| 136 |
|
| 137 |
QVariant CreateCollectionPrompt::currentResult() const |
| 138 |
{ |
| 139 |
return QVariant::fromValue(DBusMgr::objectPathSafe(m_coll)); |
| 140 |
} |
| 141 |
|
| 142 |
PromptResult CreateCollectionPrompt::promptSync(const DBusClientPtr&, const QString& windowId) |
| 143 |
{ |
| 144 |
MessageBox::OverrideParent override(findWindow(windowId)); |
| 145 |
|
| 146 |
bool created = false; |
| 147 |
// collection with the alias may be created since the prompt was created |
| 148 |
auto ret = service()->readAlias(m_alias, m_coll); |
| 149 |
if (ret.err()) { |
| 150 |
return ret; |
| 151 |
} |
| 152 |
if (!m_coll) { |
| 153 |
created = true; |
| 154 |
m_coll = service()->doNewDatabase(); |
| 155 |
if (!m_coll) { |
| 156 |
return PromptResult::accepted(false); |
| 157 |
} |
| 158 |
} |
| 159 |
ret = m_coll->setProperties(m_properties); |
| 160 |
if (ret.err()) { |
| 161 |
if (created) { |
| 162 |
m_coll->removeFromDBus(); |
| 163 |
} |
| 164 |
return ret; |
| 165 |
} |
| 166 |
if (!m_alias.isEmpty()) { |
| 167 |
ret = m_coll->addAlias(m_alias); |
| 168 |
if (ret.err()) { |
| 169 |
if (created) { |
| 170 |
m_coll->removeFromDBus(); |
| 171 |
} |
| 172 |
return ret; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return {}; |
| 177 |
} |
| 178 |
|
| 179 |
LockCollectionsPrompt::LockCollectionsPrompt(Service* parent, const QList<Collection*>& colls) |
| 180 |
: PromptBase(parent) |
| 181 |
{ |
| 182 |
m_collections.reserve(colls.size()); |
| 183 |
for (const auto& c : asConst(colls)) { |
| 184 |
m_collections << c; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
QVariant LockCollectionsPrompt::currentResult() const |
| 189 |
{ |
| 190 |
return QVariant::fromValue(m_locked); |
| 191 |
} |
| 192 |
|
| 193 |
PromptResult LockCollectionsPrompt::promptSync(const DBusClientPtr&, const QString& windowId) |
| 194 |
{ |
| 195 |
MessageBox::OverrideParent override(findWindow(windowId)); |
| 196 |
|
| 197 |
for (const auto& c : asConst(m_collections)) { |
| 198 |
if (c) { |
| 199 |
auto accepted = c->doLock(); |
| 200 |
if (accepted) { |
| 201 |
m_locked << c->objectPath(); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
return PromptResult::accepted(m_locked.size() == m_collections.size()); |
| 206 |
} |
| 207 |
|
| 208 |
UnlockPrompt::UnlockPrompt(Service* parent, const QSet<Collection*>& colls, const QSet<Item*>& items) |
| 209 |
: PromptBase(parent) |
| 210 |
{ |
| 211 |
m_collections.reserve(colls.size()); |
| 212 |
for (const auto& coll : asConst(colls)) { |
| 213 |
m_collections << coll; |
| 214 |
} |
| 215 |
for (const auto& item : asConst(items)) { |
| 216 |
m_items[item->collection()] << item; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
QVariant UnlockPrompt::currentResult() const |
| 221 |
{ |
| 222 |
return QVariant::fromValue(m_unlocked); |
| 223 |
} |
| 224 |
|
| 225 |
PromptResult UnlockPrompt::promptSync(const DBusClientPtr& client, const QString& windowId) |
| 226 |
{ |
| 227 |
MessageBox::OverrideParent override(findWindow(windowId)); |
| 228 |
|
| 229 |
// for use in unlockItems |
| 230 |
m_windowId = windowId; |
| 231 |
m_client = client; |
| 232 |
|
| 233 |
// first unlock any collections |
| 234 |
bool waitingForCollections = false; |
| 235 |
for (const auto& c : asConst(m_collections)) { |
| 236 |
if (c) { |
| 237 |
connect(c, &Collection::doneUnlockCollection, this, &UnlockPrompt::collectionUnlockFinished); |
| 238 |
// doUnlock is nonblocking, execution will continue in collectionUnlockFinished |
| 239 |
// it is ok to call doUnlock multiple times before it's actually unlocked by the user |
| 240 |
c->doUnlock(); |
| 241 |
waitingForCollections = true; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
// unlock items directly if no collection unlocking pending |
| 246 |
// o.w. doing it in collectionUnlockFinished |
| 247 |
if (!waitingForCollections) { |
| 248 |
unlockItems(); |
| 249 |
} |
| 250 |
|
| 251 |
return PromptResult::Pending; |
| 252 |
} |
| 253 |
|
| 254 |
void UnlockPrompt::collectionUnlockFinished(bool accepted) |
| 255 |
{ |
| 256 |
auto coll = qobject_cast<Collection*>(sender()); |
| 257 |
if (!coll) { |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
// one shot |
| 262 |
coll->disconnect(this); |
| 263 |
|
| 264 |
if (!m_collections.contains(coll)) { |
| 265 |
// should not happen |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
if (accepted) { |
| 270 |
m_unlocked << coll->objectPath(); |
| 271 |
} else { |
| 272 |
m_numRejected += 1; |
| 273 |
// no longer need to unlock the item if its containing collection didn't unlock. |
| 274 |
m_items.remove(coll); |
| 275 |
} |
| 276 |
|
| 277 |
// if we got response for all collections |
| 278 |
if (m_numRejected + m_unlocked.size() == m_collections.size()) { |
| 279 |
// next step is to unlock items |
| 280 |
unlockItems(); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
void UnlockPrompt::unlockItems() |
| 285 |
{ |
| 286 |
auto client = m_client.lock(); |
| 287 |
if (!client) { |
| 288 |
// client already gone |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
// flatten to list of entries |
| 293 |
QList<Entry*> entries; |
| 294 |
for (const auto& itemsPerColl : asConst(m_items)) { |
| 295 |
for (const auto& item : itemsPerColl) { |
| 296 |
if (!item) { |
| 297 |
m_numRejected += 1; |
| 298 |
continue; |
| 299 |
} |
| 300 |
auto entry = item->backend(); |
| 301 |
auto uuid = entry->uuid(); |
| 302 |
if (client->itemKnown(uuid) || !FdoSecrets::settings()->confirmAccessItem()) { |
| 303 |
if (!client->itemAuthorized(uuid)) { |
| 304 |
m_numRejected += 1; |
| 305 |
} |
| 306 |
// Already saw this entry |
| 307 |
continue; |
| 308 |
} |
| 309 |
m_entryToItems[uuid] = item.data(); |
| 310 |
entries << entry; |
| 311 |
} |
| 312 |
} |
| 313 |
if (!entries.isEmpty()) { |
| 314 |
QString app = tr("%1 (PID: %2)").arg(client->name()).arg(client->pid()); |
| 315 |
auto ac = new AccessControlDialog( |
| 316 |
findWindow(m_windowId), entries, app, client->processInfo(), AuthOption::Remember); |
| 317 |
connect(ac, &AccessControlDialog::finished, this, &UnlockPrompt::itemUnlockFinished); |
| 318 |
connect(ac, &AccessControlDialog::finished, ac, &AccessControlDialog::deleteLater); |
| 319 |
ac->open(); |
| 320 |
} else { |
| 321 |
itemUnlockFinished({}, AuthDecision::Undecided); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
void UnlockPrompt::itemUnlockFinished(const QHash<Entry*, AuthDecision>& decisions, AuthDecision forFutureEntries) |
| 326 |
{ |
| 327 |
auto client = m_client.lock(); |
| 328 |
if (!client) { |
| 329 |
// client already gone |
| 330 |
qDebug() << "DBus client gone before item unlocking finish"; |
| 331 |
return; |
| 332 |
} |
| 333 |
for (auto it = decisions.constBegin(); it != decisions.constEnd(); ++it) { |
| 334 |
auto entry = it.key(); |
| 335 |
auto uuid = entry->uuid(); |
| 336 |
// get back the corresponding item |
| 337 |
auto item = m_entryToItems.value(uuid); |
| 338 |
if (!item) { |
| 339 |
continue; |
| 340 |
} |
| 341 |
|
| 342 |
// set auth |
| 343 |
client->setItemAuthorized(uuid, it.value()); |
| 344 |
|
| 345 |
if (client->itemAuthorized(uuid)) { |
| 346 |
m_unlocked += item->objectPath(); |
| 347 |
} else { |
| 348 |
m_numRejected += 1; |
| 349 |
} |
| 350 |
} |
| 351 |
if (forFutureEntries != AuthDecision::Undecided) { |
| 352 |
client->setAllAuthorized(forFutureEntries); |
| 353 |
} |
| 354 |
// if anything is not unlocked, treat the whole prompt as dismissed |
| 355 |
// so the client has a chance to handle the error |
| 356 |
finishPrompt(m_numRejected > 0); |
| 357 |
} |
| 358 |
|
| 359 |
DeleteItemPrompt::DeleteItemPrompt(Service* parent, Item* item) |
| 360 |
: PromptBase(parent) |
| 361 |
, m_item(item) |
| 362 |
{ |
| 363 |
} |
| 364 |
|
| 365 |
PromptResult DeleteItemPrompt::promptSync(const DBusClientPtr&, const QString& windowId) |
| 366 |
{ |
| 367 |
MessageBox::OverrideParent override(findWindow(windowId)); |
| 368 |
|
| 369 |
// if m_item is gone, assume it's already deleted |
| 370 |
bool deleted = true; |
| 371 |
if (m_item) { |
| 372 |
deleted = m_item->doDelete(); |
| 373 |
} |
| 374 |
return PromptResult::accepted(deleted); |
| 375 |
} |
| 376 |
|
| 377 |
CreateItemPrompt::CreateItemPrompt(Service* parent, |
| 378 |
Collection* coll, |
| 379 |
QVariantMap properties, |
| 380 |
Secret secret, |
| 381 |
bool replace) |
| 382 |
: PromptBase(parent) |
| 383 |
, m_coll(coll) |
| 384 |
, m_properties(std::move(properties)) |
| 385 |
, m_secret(std::move(secret)) |
| 386 |
, m_replace(replace) |
| 387 |
, m_item(nullptr) |
| 388 |
// session aliveness also need to be tracked, for potential use later in updateItem |
| 389 |
, m_sess(m_secret.session) |
| 390 |
{ |
| 391 |
} |
| 392 |
|
| 393 |
QVariant CreateItemPrompt::currentResult() const |
| 394 |
{ |
| 395 |
return QVariant::fromValue(DBusMgr::objectPathSafe(m_item)); |
| 396 |
} |
| 397 |
|
| 398 |
PromptResult CreateItemPrompt::promptSync(const DBusClientPtr& client, const QString& windowId) |
| 399 |
{ |
| 400 |
if (!m_coll) { |
| 401 |
return PromptResult::accepted(false); |
| 402 |
} |
| 403 |
|
| 404 |
// save a weak reference to the client which may be used asynchronously later |
| 405 |
m_client = client; |
| 406 |
|
| 407 |
// give the user a chance to unlock the collection |
| 408 |
// UnlockPrompt will handle the case of collection already unlocked |
| 409 |
auto prompt = PromptBase::Create<UnlockPrompt>(service(), QSet<Collection*>{m_coll.data()}, QSet<Item*>{}); |
| 410 |
if (!prompt) { |
| 411 |
return DBusResult{QDBusError::InternalError}; |
| 412 |
} |
| 413 |
// postpone anything after the prompt |
| 414 |
connect(prompt, &PromptBase::completed, this, [this, windowId](bool dismissed) { |
| 415 |
if (dismissed) { |
| 416 |
finishPrompt(dismissed); |
| 417 |
} else { |
| 418 |
auto res = createItem(windowId); |
| 419 |
if (res.err()) { |
| 420 |
qWarning() << "FdoSecrets:" << res; |
| 421 |
finishPrompt(true); |
| 422 |
} |
| 423 |
} |
| 424 |
}); |
| 425 |
|
| 426 |
auto ret = prompt->prompt(client, windowId); |
| 427 |
if (ret.err()) { |
| 428 |
return ret; |
| 429 |
} |
| 430 |
return PromptResult::Pending; |
| 431 |
} |
| 432 |
|
| 433 |
DBusResult CreateItemPrompt::createItem(const QString& windowId) |
| 434 |
{ |
| 435 |
auto client = m_client.lock(); |
| 436 |
if (!client) { |
| 437 |
// client already gone |
| 438 |
return {}; |
| 439 |
} |
| 440 |
|
| 441 |
if (!m_coll) { |
| 442 |
return DBusResult{DBUS_ERROR_SECRET_NO_SUCH_OBJECT}; |
| 443 |
} |
| 444 |
|
| 445 |
// get itemPath to create item and |
| 446 |
// try to find an existing item using attributes |
| 447 |
QString itemPath{}; |
| 448 |
auto iterAttr = m_properties.find(DBUS_INTERFACE_SECRET_ITEM + ".Attributes"); |
| 449 |
if (iterAttr != m_properties.end()) { |
| 450 |
// the actual value in iterAttr.value() is QDBusArgument, which represents a structure |
| 451 |
// and qt has no idea what this corresponds to. |
| 452 |
// we thus force a conversion to StringStringMap here. The conversion is registered in |
| 453 |
// DBusTypes.cpp |
| 454 |
auto attributes = iterAttr.value().value<StringStringMap>(); |
| 455 |
|
| 456 |
itemPath = attributes.value(ItemAttributes::PathKey); |
| 457 |
|
| 458 |
// check existing item using attributes |
| 459 |
QList<Item*> existing; |
| 460 |
auto ret = m_coll->searchItems(client, attributes, existing); |
| 461 |
if (ret.err()) { |
| 462 |
return ret; |
| 463 |
} |
| 464 |
if (!existing.isEmpty() && m_replace) { |
| 465 |
m_item = existing.front(); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
if (!m_item) { |
| 470 |
// the item doesn't exist yet, create it |
| 471 |
m_item = m_coll->doNewItem(client, itemPath); |
| 472 |
if (!m_item) { |
| 473 |
// may happen if entry somehow ends up in recycle bin |
| 474 |
return DBusResult{DBUS_ERROR_SECRET_NO_SUCH_OBJECT}; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
// the item may be locked due to authorization |
| 479 |
// give the user a chance to unlock the item |
| 480 |
auto prompt = PromptBase::Create<UnlockPrompt>(service(), QSet<Collection*>{}, QSet<Item*>{m_item}); |
| 481 |
if (!prompt) { |
| 482 |
return DBusResult{QDBusError::InternalError}; |
| 483 |
} |
| 484 |
// postpone anything after the confirmation |
| 485 |
connect(prompt, &PromptBase::completed, this, [this](bool dismissed) { |
| 486 |
if (dismissed) { |
| 487 |
finishPrompt(dismissed); |
| 488 |
} else { |
| 489 |
auto res = updateItem(); |
| 490 |
if (res.err()) { |
| 491 |
qWarning() << "FdoSecrets:" << res; |
| 492 |
finishPrompt(true); |
| 493 |
} |
| 494 |
} |
| 495 |
}); |
| 496 |
|
| 497 |
auto ret = prompt->prompt(client, windowId); |
| 498 |
if (ret.err()) { |
| 499 |
return ret; |
| 500 |
} |
| 501 |
return {}; |
| 502 |
} |
| 503 |
|
| 504 |
DBusResult CreateItemPrompt::updateItem() |
| 505 |
{ |
| 506 |
auto client = m_client.lock(); |
| 507 |
if (!client) { |
| 508 |
// client already gone |
| 509 |
return {}; |
| 510 |
} |
| 511 |
|
| 512 |
if (!m_sess || m_sess != m_secret.session) { |
| 513 |
return DBusResult(DBUS_ERROR_SECRET_NO_SESSION); |
| 514 |
} |
| 515 |
if (!m_item) { |
| 516 |
return {}; |
| 517 |
} |
| 518 |
auto ret = m_item->setProperties(m_properties); |
| 519 |
if (ret.err()) { |
| 520 |
return ret; |
| 521 |
} |
| 522 |
ret = m_item->setSecret(client, m_secret); |
| 523 |
if (ret.err()) { |
| 524 |
return ret; |
| 525 |
} |
| 526 |
|
| 527 |
// finally can finish the prompt without dismissing it |
| 528 |
finishPrompt(false); |
| 529 |
return {}; |
| 530 |
} |
| 531 |
} // namespace FdoSecrets |