| 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 "Item.h" |
| 19 |
|
| 20 |
#include "fdosecrets/FdoSecretsPlugin.h" |
| 21 |
#include "fdosecrets/objects/Collection.h" |
| 22 |
#include "fdosecrets/objects/Prompt.h" |
| 23 |
#include "fdosecrets/objects/Service.h" |
| 24 |
#include "fdosecrets/objects/Session.h" |
| 25 |
|
| 26 |
#include "core/EntryAttributes.h" |
| 27 |
#include "core/Group.h" |
| 28 |
|
| 29 |
#include <QMimeDatabase> |
| 30 |
#include <QSet> |
| 31 |
#include <QTextCodec> |
| 32 |
|
| 33 |
namespace FdoSecrets |
| 34 |
{ |
| 35 |
struct EntryUpdater |
| 36 |
{ |
| 37 |
Entry* entry; |
| 38 |
explicit EntryUpdater(Entry* entry) |
| 39 |
: entry(entry) |
| 40 |
{ |
| 41 |
entry->beginUpdate(); |
| 42 |
} |
| 43 |
|
| 44 |
~EntryUpdater() |
| 45 |
{ |
| 46 |
entry->endUpdate(); |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
const QSet<QString> Item::ReadOnlyAttributes(QSet<QString>() << ItemAttributes::UuidKey << ItemAttributes::PathKey |
| 51 |
<< ItemAttributes::TotpKey); |
| 52 |
|
| 53 |
static DBusResult setEntrySecret(Entry* entry, const QByteArray& data, const QString& contentType); |
| 54 |
static Secret getEntrySecret(Entry* entry); |
| 55 |
|
| 56 |
namespace |
| 57 |
{ |
| 58 |
constexpr auto FDO_SECRETS_DATA = "FDO_SECRETS_DATA"; |
| 59 |
constexpr auto FDO_SECRETS_CONTENT_TYPE = "FDO_SECRETS_CONTENT_TYPE"; |
| 60 |
} // namespace |
| 61 |
|
| 62 |
Item* Item::Create(Collection* parent, Entry* backend) |
| 63 |
{ |
| 64 |
QScopedPointer<Item> res{new Item(parent, backend)}; |
| 65 |
if (!res->dbus()->registerObject(res.data())) { |
| 66 |
return nullptr; |
| 67 |
} |
| 68 |
|
| 69 |
return res.take(); |
| 70 |
} |
| 71 |
|
| 72 |
Item::Item(Collection* parent, Entry* backend) |
| 73 |
: DBusObject(parent) |
| 74 |
, m_backend(backend) |
| 75 |
{ |
| 76 |
connect(m_backend, &Entry::modified, this, &Item::itemChanged); |
| 77 |
} |
| 78 |
|
| 79 |
DBusResult Item::locked(const DBusClientPtr& client, bool& locked) const |
| 80 |
{ |
| 81 |
auto ret = ensureBackend(); |
| 82 |
if (ret.err()) { |
| 83 |
return ret; |
| 84 |
} |
| 85 |
ret = collection()->locked(locked); |
| 86 |
if (ret.err()) { |
| 87 |
return ret; |
| 88 |
} |
| 89 |
locked = locked || !client->itemAuthorized(m_backend->uuid()); |
| 90 |
return {}; |
| 91 |
} |
| 92 |
|
| 93 |
DBusResult Item::attributes(StringStringMap& attrs) const |
| 94 |
{ |
| 95 |
auto ret = ensureBackend(); |
| 96 |
if (ret.err()) { |
| 97 |
return ret; |
| 98 |
} |
| 99 |
ret = ensureUnlocked(); |
| 100 |
if (ret.err()) { |
| 101 |
return ret; |
| 102 |
} |
| 103 |
|
| 104 |
// add default attributes except password |
| 105 |
auto entryAttrs = m_backend->attributes(); |
| 106 |
for (const auto& attr : EntryAttributes::DefaultAttributes) { |
| 107 |
if (entryAttrs->isProtected(attr) || attr == EntryAttributes::PasswordKey) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
auto value = entryAttrs->value(attr); |
| 112 |
if (entryAttrs->isReference(attr)) { |
| 113 |
value = m_backend->maskPasswordPlaceholders(value); |
| 114 |
value = m_backend->resolveMultiplePlaceholders(value); |
| 115 |
} |
| 116 |
attrs[attr] = value; |
| 117 |
} |
| 118 |
|
| 119 |
// add custom attributes |
| 120 |
const auto customKeys = entryAttrs->customKeys(); |
| 121 |
for (const auto& attr : customKeys) { |
| 122 |
attrs[attr] = entryAttrs->value(attr); |
| 123 |
} |
| 124 |
|
| 125 |
// add some informative and readonly attributes |
| 126 |
attrs[ItemAttributes::UuidKey] = m_backend->uuidToHex(); |
| 127 |
attrs[ItemAttributes::PathKey] = path(); |
| 128 |
if (m_backend->hasTotp()) { |
| 129 |
attrs[ItemAttributes::TotpKey] = m_backend->totp(); |
| 130 |
} |
| 131 |
return {}; |
| 132 |
} |
| 133 |
|
| 134 |
DBusResult Item::setAttributes(const StringStringMap& attrs) |
| 135 |
{ |
| 136 |
auto ret = ensureBackend(); |
| 137 |
if (ret.err()) { |
| 138 |
return ret; |
| 139 |
} |
| 140 |
ret = ensureUnlocked(); |
| 141 |
if (ret.err()) { |
| 142 |
return ret; |
| 143 |
} |
| 144 |
|
| 145 |
// set on a temp variable first and check for errors to avoid partial updates |
| 146 |
EntryAttributes tempAttrs; |
| 147 |
tempAttrs.copyDataFrom(m_backend->attributes()); |
| 148 |
for (auto it = attrs.constBegin(); it != attrs.constEnd(); ++it) { |
| 149 |
if (tempAttrs.isProtected(it.key()) || it.key() == EntryAttributes::PasswordKey) { |
| 150 |
return QDBusError::InvalidArgs; |
| 151 |
} |
| 152 |
|
| 153 |
if (ReadOnlyAttributes.contains(it.key())) { |
| 154 |
return QDBusError::InvalidArgs; |
| 155 |
} |
| 156 |
|
| 157 |
if (EntryAttributes::matchReference(it.value()).hasMatch()) { |
| 158 |
return QDBusError::InvalidArgs; |
| 159 |
} |
| 160 |
|
| 161 |
tempAttrs.set(it.key(), it.value()); |
| 162 |
} |
| 163 |
|
| 164 |
// actually update the backend |
| 165 |
EntryUpdater eu(m_backend); |
| 166 |
m_backend->attributes()->copyDataFrom(&tempAttrs); |
| 167 |
return {}; |
| 168 |
} |
| 169 |
|
| 170 |
DBusResult Item::label(QString& label) const |
| 171 |
{ |
| 172 |
auto ret = ensureBackend(); |
| 173 |
if (ret.err()) { |
| 174 |
return ret; |
| 175 |
} |
| 176 |
ret = ensureUnlocked(); |
| 177 |
if (ret.err()) { |
| 178 |
return ret; |
| 179 |
} |
| 180 |
|
| 181 |
label = m_backend->title(); |
| 182 |
return {}; |
| 183 |
} |
| 184 |
|
| 185 |
DBusResult Item::setLabel(const QString& label) |
| 186 |
{ |
| 187 |
auto ret = ensureBackend(); |
| 188 |
if (ret.err()) { |
| 189 |
return ret; |
| 190 |
} |
| 191 |
ret = ensureUnlocked(); |
| 192 |
if (ret.err()) { |
| 193 |
return ret; |
| 194 |
} |
| 195 |
|
| 196 |
if (EntryAttributes::matchReference(label).hasMatch()) { |
| 197 |
return QDBusError::InvalidArgs; |
| 198 |
} |
| 199 |
|
| 200 |
EntryUpdater eu(m_backend); |
| 201 |
m_backend->setTitle(label); |
| 202 |
return {}; |
| 203 |
} |
| 204 |
|
| 205 |
DBusResult Item::created(qulonglong& created) const |
| 206 |
{ |
| 207 |
auto ret = ensureBackend(); |
| 208 |
if (ret.err()) { |
| 209 |
return ret; |
| 210 |
} |
| 211 |
ret = ensureUnlocked(); |
| 212 |
if (ret.err()) { |
| 213 |
return ret; |
| 214 |
} |
| 215 |
|
| 216 |
created = static_cast<qulonglong>(m_backend->timeInfo().creationTime().toMSecsSinceEpoch() / 1000); |
| 217 |
return {}; |
| 218 |
} |
| 219 |
|
| 220 |
DBusResult Item::modified(qulonglong& modified) const |
| 221 |
{ |
| 222 |
auto ret = ensureBackend(); |
| 223 |
if (ret.err()) { |
| 224 |
return ret; |
| 225 |
} |
| 226 |
ret = ensureUnlocked(); |
| 227 |
if (ret.err()) { |
| 228 |
return ret; |
| 229 |
} |
| 230 |
|
| 231 |
modified = static_cast<qulonglong>(m_backend->timeInfo().lastModificationTime().toMSecsSinceEpoch() / 1000); |
| 232 |
return {}; |
| 233 |
} |
| 234 |
|
| 235 |
DBusResult Item::remove(PromptBase*& prompt) |
| 236 |
{ |
| 237 |
auto ret = ensureBackend(); |
| 238 |
if (ret.err()) { |
| 239 |
return ret; |
| 240 |
} |
| 241 |
prompt = PromptBase::Create<DeleteItemPrompt>(service(), this); |
| 242 |
if (!prompt) { |
| 243 |
return QDBusError::InternalError; |
| 244 |
} |
| 245 |
return {}; |
| 246 |
} |
| 247 |
|
| 248 |
DBusResult Item::getSecret(const DBusClientPtr& client, Session* session, Secret& secret) |
| 249 |
{ |
| 250 |
auto ret = getSecretNoNotification(client, session, secret); |
| 251 |
if (ret.ok()) { |
| 252 |
service()->plugin()->emitRequestShowNotification( |
| 253 |
tr(R"(Entry "%1" from database "%2" was used by %3)") |
| 254 |
.arg(m_backend->title(), collection()->name(), client->name())); |
| 255 |
} |
| 256 |
return ret; |
| 257 |
} |
| 258 |
|
| 259 |
DBusResult Item::getSecretNoNotification(const DBusClientPtr& client, Session* session, Secret& secret) const |
| 260 |
{ |
| 261 |
auto ret = ensureBackend(); |
| 262 |
if (ret.err()) { |
| 263 |
return ret; |
| 264 |
} |
| 265 |
ret = ensureUnlocked(); |
| 266 |
if (ret.err()) { |
| 267 |
return ret; |
| 268 |
} |
| 269 |
if (!client->itemAuthorizedResetOnce(backend()->uuid())) { |
| 270 |
return DBusResult(DBUS_ERROR_SECRET_IS_LOCKED); |
| 271 |
} |
| 272 |
|
| 273 |
if (!session) { |
| 274 |
return DBusResult(DBUS_ERROR_SECRET_NO_SESSION); |
| 275 |
} |
| 276 |
|
| 277 |
secret = getEntrySecret(m_backend); |
| 278 |
|
| 279 |
// encode using session |
| 280 |
secret = session->encode(secret); |
| 281 |
|
| 282 |
return {}; |
| 283 |
} |
| 284 |
|
| 285 |
DBusResult Item::setSecret(const DBusClientPtr& client, const Secret& secret) |
| 286 |
{ |
| 287 |
auto ret = ensureBackend(); |
| 288 |
if (ret.err()) { |
| 289 |
return ret; |
| 290 |
} |
| 291 |
ret = ensureUnlocked(); |
| 292 |
if (ret.err()) { |
| 293 |
return ret; |
| 294 |
} |
| 295 |
if (!client->itemAuthorizedResetOnce(backend()->uuid())) { |
| 296 |
return DBusResult(DBUS_ERROR_SECRET_IS_LOCKED); |
| 297 |
} |
| 298 |
|
| 299 |
if (!secret.session) { |
| 300 |
return DBusResult(DBUS_ERROR_SECRET_NO_SESSION); |
| 301 |
} |
| 302 |
|
| 303 |
// decode using session |
| 304 |
auto decoded = secret.session->decode(secret); |
| 305 |
|
| 306 |
// block references |
| 307 |
if (EntryAttributes::matchReference(decoded.value).hasMatch()) { |
| 308 |
return QDBusError::InvalidArgs; |
| 309 |
} |
| 310 |
|
| 311 |
EntryUpdater eu(m_backend); |
| 312 |
// set in backend |
| 313 |
return setEntrySecret(m_backend, decoded.value, decoded.contentType); |
| 314 |
} |
| 315 |
|
| 316 |
DBusResult Item::setProperties(const QVariantMap& properties) |
| 317 |
{ |
| 318 |
auto label = properties.value(DBUS_INTERFACE_SECRET_ITEM + ".Label").toString(); |
| 319 |
|
| 320 |
auto ret = setLabel(label); |
| 321 |
if (ret.err()) { |
| 322 |
return ret; |
| 323 |
} |
| 324 |
|
| 325 |
auto attributes = properties.value(DBUS_INTERFACE_SECRET_ITEM + ".Attributes").value<StringStringMap>(); |
| 326 |
ret = setAttributes(attributes); |
| 327 |
if (ret.err()) { |
| 328 |
return ret; |
| 329 |
} |
| 330 |
|
| 331 |
return {}; |
| 332 |
} |
| 333 |
|
| 334 |
Collection* Item::collection() const |
| 335 |
{ |
| 336 |
return qobject_cast<Collection*>(parent()); |
| 337 |
} |
| 338 |
|
| 339 |
DBusResult Item::ensureBackend() const |
| 340 |
{ |
| 341 |
if (!m_backend) { |
| 342 |
return DBusResult(DBUS_ERROR_SECRET_NO_SUCH_OBJECT); |
| 343 |
} |
| 344 |
return {}; |
| 345 |
} |
| 346 |
|
| 347 |
DBusResult Item::ensureUnlocked() const |
| 348 |
{ |
| 349 |
bool l; |
| 350 |
auto ret = collection()->locked(l); |
| 351 |
if (ret.err()) { |
| 352 |
return ret; |
| 353 |
} |
| 354 |
if (l) { |
| 355 |
return DBusResult(DBUS_ERROR_SECRET_IS_LOCKED); |
| 356 |
} |
| 357 |
return {}; |
| 358 |
} |
| 359 |
|
| 360 |
Entry* Item::backend() const |
| 361 |
{ |
| 362 |
return m_backend; |
| 363 |
} |
| 364 |
|
| 365 |
bool Item::doDelete() |
| 366 |
{ |
| 367 |
Q_ASSERT(m_backend); |
| 368 |
|
| 369 |
return collection()->doDeleteEntry(m_backend); |
| 370 |
} |
| 371 |
|
| 372 |
void Item::removeFromDBus() |
| 373 |
{ |
| 374 |
emit itemAboutToDelete(); |
| 375 |
|
| 376 |
// Unregister current path early, do not rely on deleteLater's call to destructor |
| 377 |
// as in case of Entry moving between groups, new Item will be created at the same DBus path |
| 378 |
// before the current Item is deleted in the event loop. |
| 379 |
dbus()->unregisterObject(this); |
| 380 |
|
| 381 |
m_backend = nullptr; |
| 382 |
deleteLater(); |
| 383 |
} |
| 384 |
|
| 385 |
Service* Item::service() const |
| 386 |
{ |
| 387 |
return collection()->service(); |
| 388 |
} |
| 389 |
|
| 390 |
QString Item::path() const |
| 391 |
{ |
| 392 |
QStringList pathComponents{m_backend->title()}; |
| 393 |
|
| 394 |
Group* group = m_backend->group(); |
| 395 |
while (group && group != collection()->exposedRootGroup()) { |
| 396 |
pathComponents.prepend(group->name()); |
| 397 |
group = group->parentGroup(); |
| 398 |
} |
| 399 |
// we should always reach the exposed root group |
| 400 |
Q_ASSERT(group); |
| 401 |
|
| 402 |
// root group is represented by a single slash, thus adding an empty component. |
| 403 |
pathComponents.prepend(QLatin1Literal("")); |
| 404 |
|
| 405 |
return pathComponents.join('/'); |
| 406 |
} |
| 407 |
|
| 408 |
DBusResult setEntrySecret(Entry* entry, const QByteArray& data, const QString& contentType) |
| 409 |
{ |
| 410 |
auto mimeName = contentType.split(';').takeFirst().trimmed(); |
| 411 |
|
| 412 |
// find the mime type |
| 413 |
QMimeDatabase db; |
| 414 |
auto mimeType = db.mimeTypeForName(mimeName); |
| 415 |
|
| 416 |
// find a suitable codec |
| 417 |
QTextCodec* codec = nullptr; |
| 418 |
static const QRegularExpression charsetPattern(QStringLiteral(R"re(charset=(?<encode>.+)$)re")); |
| 419 |
auto match = charsetPattern.match(contentType); |
| 420 |
if (match.hasMatch()) { |
| 421 |
codec = QTextCodec::codecForName(match.captured(QStringLiteral("encode")).toLatin1()); |
| 422 |
} else { |
| 423 |
codec = QTextCodec::codecForName(QByteArrayLiteral("utf-8")); |
| 424 |
} |
| 425 |
|
| 426 |
if (!mimeType.isValid() || !mimeType.inherits(QStringLiteral("text/plain")) || !codec) { |
| 427 |
if (EntryAttributes::matchReference(contentType).hasMatch()) { |
| 428 |
return QDBusError::InvalidArgs; |
| 429 |
} |
| 430 |
// we can't handle this content type, save the data as attachment, and clear the password field |
| 431 |
entry->setPassword(""); |
| 432 |
entry->attachments()->set(FDO_SECRETS_DATA, data); |
| 433 |
entry->attributes()->set(FDO_SECRETS_CONTENT_TYPE, contentType); |
| 434 |
} else { |
| 435 |
auto password = codec->toUnicode(data); |
| 436 |
if (EntryAttributes::matchReference(password).hasMatch()) { |
| 437 |
return QDBusError::InvalidArgs; |
| 438 |
} |
| 439 |
// save the data to password field |
| 440 |
entry->setPassword(password); |
| 441 |
if (entry->attachments()->hasKey(FDO_SECRETS_DATA)) { |
| 442 |
entry->attachments()->remove(FDO_SECRETS_DATA); |
| 443 |
} |
| 444 |
if (entry->attributes()->hasKey(FDO_SECRETS_CONTENT_TYPE)) { |
| 445 |
entry->attributes()->remove(FDO_SECRETS_CONTENT_TYPE); |
| 446 |
} |
| 447 |
} |
| 448 |
return {}; |
| 449 |
} |
| 450 |
|
| 451 |
Secret getEntrySecret(Entry* entry) |
| 452 |
{ |
| 453 |
Secret ss{}; |
| 454 |
|
| 455 |
if (entry->attachments()->hasKey(FDO_SECRETS_DATA)) { |
| 456 |
ss.value = entry->attachments()->value(FDO_SECRETS_DATA); |
| 457 |
if (entry->attributes()->hasKey(FDO_SECRETS_CONTENT_TYPE)) { |
| 458 |
ss.contentType = entry->attributes()->value(FDO_SECRETS_CONTENT_TYPE); |
| 459 |
} else { |
| 460 |
// the entry is somehow corrupted, maybe the user deleted it. |
| 461 |
// set to binary and hope for the best... |
| 462 |
ss.contentType = QStringLiteral("application/octet-stream"); |
| 463 |
} |
| 464 |
return ss; |
| 465 |
} |
| 466 |
|
| 467 |
ss.value = entry->resolveMultiplePlaceholders(entry->password()).toUtf8(); |
| 468 |
ss.contentType = QStringLiteral("text/plain"); |
| 469 |
return ss; |
| 470 |
} |
| 471 |
|
| 472 |
} // namespace FdoSecrets |