2010-08-07 15:10:44 +02:00
|
|
|
/*
|
2018-11-22 11:47:31 +01:00
|
|
|
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
|
2010-08-07 15:10:44 +02:00
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef KEEPASSX_DATABASE_H
|
|
|
|
#define KEEPASSX_DATABASE_H
|
|
|
|
|
2013-10-03 15:18:16 +02:00
|
|
|
#include <QDateTime>
|
|
|
|
#include <QHash>
|
2020-03-05 22:59:07 -05:00
|
|
|
#include <QMutex>
|
2018-11-22 11:47:31 +01:00
|
|
|
#include <QPointer>
|
2019-12-08 09:52:01 -05:00
|
|
|
#include <QTimer>
|
2010-08-07 15:10:44 +02:00
|
|
|
|
2018-10-01 10:26:24 -04:00
|
|
|
#include "config-keepassx.h"
|
2021-05-27 21:50:15 -04:00
|
|
|
#include "core/ModifiableObject.h"
|
2019-01-20 09:50:20 -05:00
|
|
|
#include "crypto/kdf/AesKdf.h"
|
2018-11-22 11:47:31 +01:00
|
|
|
#include "format/KeePass2.h"
|
2011-06-29 16:39:39 +02:00
|
|
|
#include "keys/CompositeKey.h"
|
2019-11-18 06:57:04 +00:00
|
|
|
#include "keys/PasswordKey.h"
|
2019-11-08 22:21:33 +01:00
|
|
|
|
2011-07-08 13:57:02 +02:00
|
|
|
class Entry;
|
2017-11-12 17:35:54 +02:00
|
|
|
enum class EntryReferenceType;
|
2019-10-14 09:31:23 -04:00
|
|
|
class FileWatcher;
|
2011-07-08 13:57:02 +02:00
|
|
|
class Group;
|
2010-08-07 15:10:44 +02:00
|
|
|
class Metadata;
|
2018-01-17 20:13:13 -05:00
|
|
|
class QIODevice;
|
2010-08-07 15:10:44 +02:00
|
|
|
|
2010-08-25 13:52:59 +02:00
|
|
|
struct DeletedObject
|
|
|
|
{
|
2018-03-22 22:56:05 +01:00
|
|
|
QUuid uuid;
|
2010-08-25 13:52:59 +02:00
|
|
|
QDateTime deletionTime;
|
2018-09-30 08:45:06 -04:00
|
|
|
bool operator==(const DeletedObject& other) const
|
|
|
|
{
|
|
|
|
return uuid == other.uuid && deletionTime == other.deletionTime;
|
|
|
|
}
|
2010-08-25 13:52:59 +02:00
|
|
|
};
|
|
|
|
|
2012-04-21 18:39:09 +02:00
|
|
|
Q_DECLARE_TYPEINFO(DeletedObject, Q_MOVABLE_TYPE);
|
|
|
|
|
2021-05-27 21:50:15 -04:00
|
|
|
class Database : public ModifiableObject
|
2010-08-07 15:10:44 +02:00
|
|
|
{
|
2010-08-12 21:38:59 +02:00
|
|
|
Q_OBJECT
|
|
|
|
|
2010-08-07 15:10:44 +02:00
|
|
|
public:
|
2010-09-25 12:41:00 +02:00
|
|
|
enum CompressionAlgorithm
|
|
|
|
{
|
|
|
|
CompressionNone = 0,
|
|
|
|
CompressionGZip = 1
|
|
|
|
};
|
|
|
|
static const quint32 CompressionAlgorithmMax = CompressionGZip;
|
|
|
|
|
2021-10-01 16:56:49 -04:00
|
|
|
enum SaveAction
|
|
|
|
{
|
|
|
|
Atomic, // Saves are transactional and atomic
|
|
|
|
TempFile, // Write to a temporary location then move into place, may be non-atomic
|
|
|
|
DirectWrite, // Directly write to the destination file (dangerous)
|
|
|
|
};
|
|
|
|
|
2010-08-13 18:08:06 +02:00
|
|
|
Database();
|
2018-11-22 11:47:31 +01:00
|
|
|
explicit Database(const QString& filePath);
|
2017-12-16 18:36:42 +01:00
|
|
|
~Database() override;
|
2011-07-08 13:57:02 +02:00
|
|
|
|
2022-01-29 03:26:53 +01:00
|
|
|
private:
|
|
|
|
bool writeDatabase(QIODevice* device, QString* error = nullptr);
|
|
|
|
bool backupDatabase(const QString& filePath, const QString& destinationFilePath);
|
|
|
|
bool restoreDatabase(const QString& filePath, const QString& fromBackupFilePath);
|
|
|
|
bool performSave(const QString& filePath, SaveAction flags, const QString& backupFilePath, QString* error);
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool open(QSharedPointer<const CompositeKey> key, QString* error = nullptr);
|
|
|
|
bool open(const QString& filePath, QSharedPointer<const CompositeKey> key, QString* error = nullptr);
|
2021-11-07 23:41:17 +01:00
|
|
|
bool save(SaveAction action = Atomic, const QString& backupFilePath = QString(), QString* error = nullptr);
|
|
|
|
bool saveAs(const QString& filePath,
|
|
|
|
SaveAction action = Atomic,
|
|
|
|
const QString& backupFilePath = QString(),
|
|
|
|
QString* error = nullptr);
|
2019-02-13 13:24:55 -05:00
|
|
|
bool extract(QByteArray&, QString* error = nullptr);
|
2019-10-14 08:37:26 -04:00
|
|
|
bool import(const QString& xmlExportPath, QString* error = nullptr);
|
2011-07-08 13:57:02 +02:00
|
|
|
|
2021-11-20 00:32:09 +01:00
|
|
|
quint32 formatVersion() const;
|
|
|
|
void setFormatVersion(quint32 version);
|
|
|
|
bool hasMinorVersionMismatch() const;
|
|
|
|
|
2019-11-08 13:34:40 +01:00
|
|
|
void releaseData();
|
|
|
|
|
2018-11-22 11:47:31 +01:00
|
|
|
bool isInitialized() const;
|
2020-08-01 18:00:47 -04:00
|
|
|
bool isModified() const;
|
|
|
|
bool hasNonDataChanges() const;
|
2020-03-05 22:59:07 -05:00
|
|
|
bool isSaving();
|
2018-11-22 11:47:31 +01:00
|
|
|
|
|
|
|
QUuid uuid() const;
|
2018-05-13 23:21:43 +02:00
|
|
|
QString filePath() const;
|
2019-03-26 22:23:16 -04:00
|
|
|
QString canonicalFilePath() const;
|
2018-05-13 23:21:43 +02:00
|
|
|
void setFilePath(const QString& filePath);
|
2018-11-22 11:47:31 +01:00
|
|
|
|
|
|
|
Metadata* metadata();
|
|
|
|
const Metadata* metadata() const;
|
|
|
|
Group* rootGroup();
|
|
|
|
const Group* rootGroup() const;
|
|
|
|
void setRootGroup(Group* group);
|
|
|
|
QVariantMap& publicCustomData();
|
|
|
|
const QVariantMap& publicCustomData() const;
|
|
|
|
void setPublicCustomData(const QVariantMap& customData);
|
|
|
|
|
|
|
|
void recycleGroup(Group* group);
|
|
|
|
void recycleEntry(Entry* entry);
|
|
|
|
void emptyRecycleBin();
|
2010-08-25 13:52:59 +02:00
|
|
|
QList<DeletedObject> deletedObjects();
|
2018-09-30 08:45:06 -04:00
|
|
|
const QList<DeletedObject>& deletedObjects() const;
|
2010-08-25 13:52:59 +02:00
|
|
|
void addDeletedObject(const DeletedObject& delObj);
|
2018-03-22 22:56:05 +01:00
|
|
|
void addDeletedObject(const QUuid& uuid);
|
2018-09-30 08:45:06 -04:00
|
|
|
bool containsDeletedObject(const QUuid& uuid) const;
|
|
|
|
bool containsDeletedObject(const DeletedObject& uuid) const;
|
|
|
|
void setDeletedObjects(const QList<DeletedObject>& delObjs);
|
2010-08-07 15:10:44 +02:00
|
|
|
|
2022-01-23 10:00:48 -05:00
|
|
|
const QStringList& commonUsernames() const;
|
|
|
|
const QStringList& tagList() const;
|
2022-09-07 19:25:23 -04:00
|
|
|
void removeTag(const QString& tag);
|
2019-06-22 15:38:02 +02:00
|
|
|
|
2018-05-13 23:21:43 +02:00
|
|
|
QSharedPointer<const CompositeKey> key() const;
|
2019-01-20 09:50:20 -05:00
|
|
|
bool setKey(const QSharedPointer<const CompositeKey>& key,
|
|
|
|
bool updateChangedTime = true,
|
2019-02-18 17:21:02 +01:00
|
|
|
bool updateTransformSalt = false,
|
|
|
|
bool transformKey = true);
|
2020-04-06 08:42:20 -04:00
|
|
|
QString keyError();
|
2014-09-07 16:37:46 -07:00
|
|
|
QByteArray challengeResponseKey() const;
|
|
|
|
bool challengeMasterSeed(const QByteArray& masterSeed);
|
2018-11-22 11:47:31 +01:00
|
|
|
const QUuid& cipher() const;
|
2018-03-22 22:56:05 +01:00
|
|
|
void setCipher(const QUuid& cipher);
|
2018-11-22 11:47:31 +01:00
|
|
|
Database::CompressionAlgorithm compressionAlgorithm() const;
|
|
|
|
void setCompressionAlgorithm(Database::CompressionAlgorithm algo);
|
2010-09-25 12:41:00 +02:00
|
|
|
|
2018-11-22 11:47:31 +01:00
|
|
|
QSharedPointer<Kdf> kdf() const;
|
|
|
|
void setKdf(QSharedPointer<Kdf> kdf);
|
2018-10-28 12:49:32 +01:00
|
|
|
bool changeKdf(const QSharedPointer<Kdf>& kdf);
|
2020-07-01 19:16:40 -04:00
|
|
|
QByteArray transformedDatabaseKey() const;
|
2012-04-25 00:12:23 +02:00
|
|
|
|
2018-03-22 22:56:05 +01:00
|
|
|
static Database* databaseByUuid(const QUuid& uuid);
|
2018-11-22 11:47:31 +01:00
|
|
|
|
|
|
|
public slots:
|
|
|
|
void markAsModified();
|
|
|
|
void markAsClean();
|
2019-06-22 15:38:02 +02:00
|
|
|
void updateCommonUsernames(int topN = 10);
|
2022-01-23 10:00:48 -05:00
|
|
|
void updateTagList();
|
2020-05-21 21:43:00 -04:00
|
|
|
void markNonDataChange();
|
2012-04-25 00:12:23 +02:00
|
|
|
|
2017-03-10 15:58:42 +01:00
|
|
|
signals:
|
2018-11-22 11:47:31 +01:00
|
|
|
void filePathChanged(const QString& oldPath, const QString& newPath);
|
2010-08-23 21:30:20 +02:00
|
|
|
void groupDataChanged(Group* group);
|
|
|
|
void groupAboutToAdd(Group* group, int index);
|
2010-08-18 10:27:40 +02:00
|
|
|
void groupAdded();
|
2010-08-23 21:30:20 +02:00
|
|
|
void groupAboutToRemove(Group* group);
|
2010-08-18 10:27:40 +02:00
|
|
|
void groupRemoved();
|
2012-04-23 16:57:08 +02:00
|
|
|
void groupAboutToMove(Group* group, Group* toGroup, int index);
|
|
|
|
void groupMoved();
|
2019-10-14 09:31:23 -04:00
|
|
|
void databaseOpened();
|
2018-11-22 11:47:31 +01:00
|
|
|
void databaseSaved();
|
|
|
|
void databaseDiscarded();
|
2019-10-14 09:31:23 -04:00
|
|
|
void databaseFileChanged();
|
2022-01-23 10:00:48 -05:00
|
|
|
void tagListUpdated();
|
2012-06-24 17:53:01 +02:00
|
|
|
|
2010-08-07 15:10:44 +02:00
|
|
|
private:
|
2018-11-22 11:47:31 +01:00
|
|
|
struct DatabaseData
|
|
|
|
{
|
2021-11-20 00:32:09 +01:00
|
|
|
quint32 formatVersion = 0;
|
2018-11-22 11:47:31 +01:00
|
|
|
QString filePath;
|
|
|
|
QUuid cipher = KeePass2::CIPHER_AES256;
|
|
|
|
CompressionAlgorithm compressionAlgorithm = CompressionGZip;
|
2019-11-08 22:21:33 +01:00
|
|
|
|
|
|
|
QScopedPointer<PasswordKey> masterSeed;
|
2020-07-01 19:16:40 -04:00
|
|
|
QScopedPointer<PasswordKey> transformedDatabaseKey;
|
2019-11-08 22:21:33 +01:00
|
|
|
QScopedPointer<PasswordKey> challengeResponseKey;
|
|
|
|
|
|
|
|
QSharedPointer<const CompositeKey> key;
|
|
|
|
QSharedPointer<Kdf> kdf = QSharedPointer<AesKdf>::create(true);
|
|
|
|
|
2018-11-22 11:47:31 +01:00
|
|
|
QVariantMap publicCustomData;
|
|
|
|
|
|
|
|
DatabaseData()
|
2019-11-08 22:21:33 +01:00
|
|
|
: masterSeed(new PasswordKey())
|
2020-07-01 19:16:40 -04:00
|
|
|
, transformedDatabaseKey(new PasswordKey())
|
2019-11-08 22:21:33 +01:00
|
|
|
, challengeResponseKey(new PasswordKey())
|
2018-11-22 11:47:31 +01:00
|
|
|
{
|
|
|
|
kdf->randomizeSeed();
|
|
|
|
}
|
2019-11-08 22:21:33 +01:00
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
filePath.clear();
|
|
|
|
|
|
|
|
masterSeed.reset();
|
2020-07-01 19:16:40 -04:00
|
|
|
transformedDatabaseKey.reset();
|
2019-11-08 22:21:33 +01:00
|
|
|
challengeResponseKey.reset();
|
|
|
|
|
|
|
|
key.reset();
|
|
|
|
kdf.reset();
|
|
|
|
|
|
|
|
publicCustomData.clear();
|
|
|
|
}
|
2018-11-22 11:47:31 +01:00
|
|
|
};
|
2010-08-07 15:10:44 +02:00
|
|
|
|
2012-04-21 19:06:28 +02:00
|
|
|
void createRecycleBin();
|
2018-11-22 11:47:31 +01:00
|
|
|
|
2020-11-28 22:03:39 -05:00
|
|
|
void startModifiedTimer();
|
|
|
|
void stopModifiedTimer();
|
2012-04-21 19:06:28 +02:00
|
|
|
|
2019-11-08 13:34:40 +01:00
|
|
|
QPointer<Metadata> const m_metadata;
|
2018-11-22 11:47:31 +01:00
|
|
|
DatabaseData m_data;
|
2019-11-08 13:34:40 +01:00
|
|
|
QPointer<Group> m_rootGroup;
|
2010-08-25 13:52:59 +02:00
|
|
|
QList<DeletedObject> m_deletedObjects;
|
2019-12-08 09:52:01 -05:00
|
|
|
QTimer m_modifiedTimer;
|
2020-03-05 22:59:07 -05:00
|
|
|
QMutex m_saveMutex;
|
2019-10-14 09:31:23 -04:00
|
|
|
QPointer<FileWatcher> m_fileWatcher;
|
2018-11-22 11:47:31 +01:00
|
|
|
bool m_modified = false;
|
2020-05-21 21:43:00 -04:00
|
|
|
bool m_hasNonDataChange = false;
|
2020-04-06 08:42:20 -04:00
|
|
|
QString m_keyError;
|
2012-04-25 00:12:23 +02:00
|
|
|
|
2022-01-23 10:00:48 -05:00
|
|
|
QStringList m_commonUsernames;
|
|
|
|
QStringList m_tagList;
|
2019-06-22 15:38:02 +02:00
|
|
|
|
2018-03-22 22:56:05 +01:00
|
|
|
QUuid m_uuid;
|
2018-11-22 11:47:31 +01:00
|
|
|
static QHash<QUuid, QPointer<Database>> s_uuidMap;
|
2010-08-07 15:10:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // KEEPASSX_DATABASE_H
|