Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Service.cpp « objects « fdosecrets « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 892ba68a46d621dfccf4b2d52f70c446a7889d71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 *  Copyright (C) 2018 Aetf <aetf@unlimitedcodeworks.xyz>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 or (at your option)
 *  version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Service.h"

#include "fdosecrets/FdoSecretsPlugin.h"
#include "fdosecrets/FdoSecretsSettings.h"
#include "fdosecrets/objects/Collection.h"
#include "fdosecrets/objects/Item.h"
#include "fdosecrets/objects/Prompt.h"
#include "fdosecrets/objects/Session.h"

#include "gui/DatabaseTabWidget.h"
#include "gui/DatabaseWidget.h"

#include <QDBusConnection>
#include <QDBusServiceWatcher>
#include <QDebug>

namespace
{
    constexpr auto DEFAULT_ALIAS = "default";
}

namespace FdoSecrets
{

    Service::Service(FdoSecretsPlugin* plugin,
                     QPointer<DatabaseTabWidget> dbTabs) // clazy: exclude=ctor-missing-parent-argument
        : DBusObject(nullptr)
        , m_plugin(plugin)
        , m_databases(std::move(dbTabs))
        , m_insdieEnsureDefaultAlias(false)
        , m_serviceWatcher(nullptr)
    {
        connect(
            m_databases, &DatabaseTabWidget::databaseUnlockDialogFinished, this, &Service::doneUnlockDatabaseInDialog);
    }

    Service::~Service()
    {
        QDBusConnection::sessionBus().unregisterService(QStringLiteral(DBUS_SERVICE_SECRET));
    }

    bool Service::initialize()
    {
        if (!QDBusConnection::sessionBus().registerService(QStringLiteral(DBUS_SERVICE_SECRET))) {
            emit error(tr("Failed to register DBus service at %1.<br/>").arg(QLatin1String(DBUS_SERVICE_SECRET))
                       + m_plugin->reportExistingService());
            return false;
        }

        registerWithPath(QStringLiteral(DBUS_PATH_SECRETS), new ServiceAdaptor(this));

        // Connect to service unregistered signal
        m_serviceWatcher.reset(new QDBusServiceWatcher());
        connect(m_serviceWatcher.data(),
                &QDBusServiceWatcher::serviceUnregistered,
                this,
                &Service::dbusServiceUnregistered);

        m_serviceWatcher->setConnection(QDBusConnection::sessionBus());

        // Add existing database tabs
        for (int idx = 0; idx != m_databases->count(); ++idx) {
            auto dbWidget = m_databases->databaseWidgetFromIndex(idx);
            onDatabaseTabOpened(dbWidget, false);
        }

        // Connect to new database signal
        // No need to connect to close signal, as collection will remove itself when backend delete/close database tab.
        connect(m_databases.data(), &DatabaseTabWidget::databaseOpened, this, [this](DatabaseWidget* dbWidget) {
            onDatabaseTabOpened(dbWidget, true);
        });

        // make default alias track current activated database
        connect(m_databases.data(), &DatabaseTabWidget::activateDatabaseChanged, this, &Service::ensureDefaultAlias);

        return true;
    }

    void Service::onDatabaseTabOpened(DatabaseWidget* dbWidget, bool emitSignal)
    {
        // The Collection will monitor the database's exposed group.
        // When the Collection finds that no exposed group, it will delete itself.
        // Thus the service also needs to monitor it and recreate the collection if the user changes
        // from no exposed to exposed something.
        if (!dbWidget->isLocked()) {
            monitorDatabaseExposedGroup(dbWidget);
        }
        connect(dbWidget, &DatabaseWidget::databaseUnlocked, this, [this, dbWidget]() {
            monitorDatabaseExposedGroup(dbWidget);
        });

        auto coll = new Collection(this, dbWidget);
        // Creation may fail if the database is not exposed.
        // This is okay, because we monitor the expose settings above
        if (!coll->isValid()) {
            coll->deleteLater();
            return;
        }

        m_collections << coll;
        m_dbToCollection[dbWidget] = coll;

        // handle alias
        connect(coll, &Collection::aliasAboutToAdd, this, &Service::onCollectionAliasAboutToAdd);
        connect(coll, &Collection::aliasAdded, this, &Service::onCollectionAliasAdded);
        connect(coll, &Collection::aliasRemoved, this, &Service::onCollectionAliasRemoved);

        ensureDefaultAlias();

        // Forward delete signal, we have to rely on filepath to identify the database being closed,
        // but we can not access m_backend safely because during the databaseClosed signal,
        // m_backend may already be reset to nullptr
        // We want to remove the collection object from dbus as early as possible, to avoid
        // race conditions when deleteLater was called on the m_backend, but not delivered yet,
        // and new method calls from dbus occurred. Therefore we can't rely on the destroyed
        // signal on m_backend.
        // bind to coll lifespan
        connect(m_databases.data(), &DatabaseTabWidget::databaseClosed, coll, [coll](const QString& filePath) {
            if (filePath == coll->backendFilePath()) {
                coll->doDelete();
            }
        });

        // relay signals
        connect(coll, &Collection::collectionChanged, this, [this, coll]() { emit collectionChanged(coll); });
        connect(coll, &Collection::collectionAboutToDelete, this, [this, coll]() {
            m_collections.removeAll(coll);
            m_dbToCollection.remove(coll->backend());
            emit collectionDeleted(coll);
        });

        if (emitSignal) {
            emit collectionCreated(coll);
        }
    }

    void Service::monitorDatabaseExposedGroup(DatabaseWidget* dbWidget)
    {
        Q_ASSERT(dbWidget);
        connect(
            dbWidget->database()->metadata()->customData(), &CustomData::customDataModified, this, [this, dbWidget]() {
                if (!FdoSecrets::settings()->exposedGroup(dbWidget->database()).isNull() && !findCollection(dbWidget)) {
                    onDatabaseTabOpened(dbWidget, true);
                }
            });
    }

    void Service::ensureDefaultAlias()
    {
        if (m_insdieEnsureDefaultAlias) {
            return;
        }

        m_insdieEnsureDefaultAlias = true;

        auto coll = findCollection(m_databases->currentDatabaseWidget());
        if (coll) {
            // adding alias will automatically remove the association with previous collection.
            coll->addAlias(DEFAULT_ALIAS).okOrDie();
        }

        m_insdieEnsureDefaultAlias = false;
    }

    void Service::dbusServiceUnregistered(const QString& service)
    {
        Q_ASSERT(m_serviceWatcher);

        auto removed = m_serviceWatcher->removeWatchedService(service);
        Q_UNUSED(removed);
        Q_ASSERT(removed);

        Session::CleanupNegotiation(service);
        auto sess = m_peerToSession.value(service, nullptr);
        if (sess) {
            sess->close().okOrDie();
        }
    }

    DBusReturn<const QList<Collection*>> Service::collections() const
    {
        return m_collections;
    }

    DBusReturn<QVariant> Service::openSession(const QString& algorithm, const QVariant& input, Session*& result)
    {
        QVariant output;
        bool incomplete = false;
        auto peer = callingPeer();

        // watch for service unregister to cleanup
        Q_ASSERT(m_serviceWatcher);
        m_serviceWatcher->addWatchedService(peer);

        // negotiate cipher
        auto ciphers = Session::CreateCiphers(peer, algorithm, input, output, incomplete);
        if (incomplete) {
            result = nullptr;
            return output;
        }
        if (!ciphers) {
            return DBusReturn<>::Error(QDBusError::NotSupported);
        }
        result = new Session(std::move(ciphers), callingPeerName(), this);

        m_sessions.append(result);
        m_peerToSession[peer] = result;
        connect(result, &Session::aboutToClose, this, [this, peer, result]() {
            emit sessionClosed(result);
            m_sessions.removeAll(result);
            m_peerToSession.remove(peer);
        });
        emit sessionOpened(result);

        return output;
    }

    DBusReturn<Collection*>
    Service::createCollection(const QVariantMap& properties, const QString& alias, PromptBase*& prompt)
    {
        prompt = nullptr;

        // return existing collection if alias is non-empty and exists.
        auto collection = findCollection(alias);
        if (!collection) {
            auto cp = new CreateCollectionPrompt(this);
            prompt = cp;

            // collection will be created when the prompt complets.
            // once it's done, we set additional properties on the collection
            connect(cp, &CreateCollectionPrompt::collectionCreated, cp, [alias, properties](Collection* coll) {
                coll->setProperties(properties).okOrDie();
                if (!alias.isEmpty()) {
                    coll->addAlias(alias).okOrDie();
                }
            });
        }
        return collection;
    }

    DBusReturn<const QList<Item*>> Service::searchItems(const StringStringMap& attributes, QList<Item*>& locked)
    {
        auto ret = collections();
        if (ret.isError()) {
            return ret;
        }

        QList<Item*> unlocked;
        for (const auto& coll : ret.value()) {
            auto items = coll->searchItems(attributes);
            if (items.isError()) {
                return items;
            }
            auto l = coll->locked();
            if (l.isError()) {
                return l;
            }
            if (l.value()) {
                locked.append(items.value());
            } else {
                unlocked.append(items.value());
            }
        }
        return unlocked;
    }

    DBusReturn<const QList<DBusObject*>> Service::unlock(const QList<DBusObject*>& objects, PromptBase*& prompt)
    {
        QSet<Collection*> needUnlock;
        needUnlock.reserve(objects.size());
        for (const auto& obj : asConst(objects)) {
            auto coll = qobject_cast<Collection*>(obj);
            if (coll) {
                needUnlock << coll;
            } else {
                auto item = qobject_cast<Item*>(obj);
                if (!item) {
                    continue;
                }
                // we lock the whole collection for item
                needUnlock << item->collection();
            }
        }

        // return anything already unlocked
        QList<DBusObject*> unlocked;
        QList<Collection*> toUnlock;
        for (const auto& coll : asConst(needUnlock)) {
            auto l = coll->locked();
            if (l.isError()) {
                return l;
            }
            if (!l.value()) {
                unlocked << coll;
            } else {
                toUnlock << coll;
            }
        }
        if (!toUnlock.isEmpty()) {
            prompt = new UnlockCollectionsPrompt(this, toUnlock);
        }
        return unlocked;
    }

    DBusReturn<const QList<DBusObject*>> Service::lock(const QList<DBusObject*>& objects, PromptBase*& prompt)
    {
        QSet<Collection*> needLock;
        needLock.reserve(objects.size());
        for (const auto& obj : asConst(objects)) {
            auto coll = qobject_cast<Collection*>(obj);
            if (coll) {
                needLock << coll;
            } else {
                auto item = qobject_cast<Item*>(obj);
                if (!item) {
                    continue;
                }
                // we lock the whole collection for item
                needLock << item->collection();
            }
        }

        // return anything already locked
        QList<DBusObject*> locked;
        QList<Collection*> toLock;
        for (const auto& coll : asConst(needLock)) {
            auto l = coll->locked();
            if (l.isError()) {
                return l;
            }
            if (l.value()) {
                locked << coll;
            } else {
                toLock << coll;
            }
        }
        if (!toLock.isEmpty()) {
            prompt = new LockCollectionsPrompt(this, toLock);
        }
        return locked;
    }

    DBusReturn<const QHash<Item*, SecretStruct>> Service::getSecrets(const QList<Item*>& items, Session* session)
    {
        if (!session) {
            return DBusReturn<>::Error(QStringLiteral(DBUS_ERROR_SECRET_NO_SESSION));
        }

        QHash<Item*, SecretStruct> res;

        for (const auto& item : asConst(items)) {
            auto ret = item->getSecret(session);
            if (ret.isError()) {
                return ret;
            }
            res[item] = std::move(ret).value();
        }
        if (calledFromDBus()) {
            plugin()->emitRequestShowNotification(
                tr(R"(%n Entry(s) was used by %1)", "%1 is the name of an application", res.size())
                    .arg(callingPeerName()));
        }
        return res;
    }

    DBusReturn<Collection*> Service::readAlias(const QString& name)
    {
        return findCollection(name);
    }

    DBusReturn<void> Service::setAlias(const QString& name, Collection* collection)
    {
        if (!collection) {
            // remove alias name from its collection
            collection = findCollection(name);
            if (!collection) {
                return DBusReturn<>::Error(QStringLiteral(DBUS_ERROR_SECRET_NO_SUCH_OBJECT));
            }
            return collection->removeAlias(name);
        }
        return collection->addAlias(name);
    }

    Collection* Service::findCollection(const QString& alias) const
    {
        if (alias.isEmpty()) {
            return nullptr;
        }

        auto it = m_aliases.find(alias);
        if (it != m_aliases.end()) {
            return it.value();
        }
        return nullptr;
    }

    void Service::onCollectionAliasAboutToAdd(const QString& alias)
    {
        auto coll = qobject_cast<Collection*>(sender());

        auto it = m_aliases.constFind(alias);
        if (it != m_aliases.constEnd() && it.value() != coll) {
            // another collection holds the alias
            // remove it first
            it.value()->removeAlias(alias).okOrDie();

            // onCollectionAliasRemoved called through signal
            // `it` becomes invalidated now
        }
    }

    void Service::onCollectionAliasAdded(const QString& alias)
    {
        auto coll = qobject_cast<Collection*>(sender());
        m_aliases[alias] = coll;
    }

    void Service::onCollectionAliasRemoved(const QString& alias)
    {
        m_aliases.remove(alias);
        ensureDefaultAlias();
    }

    Collection* Service::findCollection(const DatabaseWidget* db) const
    {
        return m_dbToCollection.value(db, nullptr);
    }

    const QList<Session*> Service::sessions() const
    {
        return m_sessions;
    }

    bool Service::doCloseDatabase(DatabaseWidget* dbWidget)
    {
        return m_databases->closeDatabaseTab(dbWidget);
    }

    Collection* Service::doNewDatabase()
    {
        auto dbWidget = m_databases->newDatabase();
        if (!dbWidget) {
            return nullptr;
        }

        // database created through dbus will be exposed to dbus by default
        auto db = dbWidget->database();
        FdoSecrets::settings()->setExposedGroup(db, db->rootGroup()->uuid());

        auto collection = findCollection(dbWidget);

        Q_ASSERT(collection);

        return collection;
    }

    void Service::doSwitchToDatabaseSettings(DatabaseWidget* dbWidget)
    {
        if (dbWidget->isLocked()) {
            return;
        }
        // switch selected to current
        m_databases->setCurrentWidget(dbWidget);
        m_databases->showDatabaseSettings();

        // open settings (switch from app settings to m_dbTabs)
        m_plugin->emitRequestSwitchToDatabases();
    }

    void Service::doUnlockDatabaseInDialog(DatabaseWidget* dbWidget)
    {
        m_databases->unlockDatabaseInDialog(dbWidget, DatabaseOpenDialog::Intent::None);
    }

} // namespace FdoSecrets