keepassxc/src/core/Database.h

240 lines
7.4 KiB
C++
Raw Normal View History

2010-08-07 15:10:44 +02: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
#include <QDateTime>
#include <QHash>
#include <QMutex>
#include <QPointer>
#include <QTimer>
2010-08-07 15:10:44 +02:00
#include "config-keepassx.h"
#include "core/ModifiableObject.h"
#include "crypto/kdf/AesKdf.h"
#include "format/KeePass2.h"
#include "keys/CompositeKey.h"
2019-11-18 06:57:04 +00:00
#include "keys/PasswordKey.h"
2011-07-08 13:57:02 +02:00
class Entry;
enum class EntryReferenceType;
class FileWatcher;
2011-07-08 13:57:02 +02:00
class Group;
2010-08-07 15:10:44 +02:00
class Metadata;
class QIODevice;
2010-08-07 15:10:44 +02:00
struct DeletedObject
{
2018-03-22 22:56:05 +01:00
QUuid uuid;
QDateTime deletionTime;
bool operator==(const DeletedObject& other) const
{
return uuid == other.uuid && deletionTime == other.deletionTime;
}
};
Q_DECLARE_TYPEINFO(DeletedObject, Q_MOVABLE_TYPE);
class Database : public ModifiableObject
2010-08-07 15:10:44 +02:00
{
Q_OBJECT
2010-08-07 15:10:44 +02:00
public:
enum CompressionAlgorithm
{
CompressionNone = 0,
CompressionGZip = 1
};
static const quint32 CompressionAlgorithmMax = CompressionGZip;
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();
explicit Database(const QString& filePath);
~Database() override;
2011-07-08 13:57:02 +02: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);
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);
bool extract(QByteArray&, QString* error = nullptr);
bool import(const QString& xmlExportPath, QString* error = nullptr);
2011-07-08 13:57:02 +02:00
quint32 formatVersion() const;
void setFormatVersion(quint32 version);
bool hasMinorVersionMismatch() const;
void releaseData();
bool isInitialized() const;
bool isModified() const;
bool hasNonDataChanges() const;
bool isSaving();
QUuid uuid() const;
QString filePath() const;
QString canonicalFilePath() const;
void setFilePath(const QString& filePath);
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();
QList<DeletedObject> deletedObjects();
const QList<DeletedObject>& deletedObjects() const;
void addDeletedObject(const DeletedObject& delObj);
2018-03-22 22:56:05 +01:00
void addDeletedObject(const QUuid& uuid);
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
const QStringList& commonUsernames() const;
const QStringList& tagList() const;
void removeTag(const QString& tag);
QSharedPointer<const CompositeKey> key() const;
bool setKey(const QSharedPointer<const CompositeKey>& key,
bool updateChangedTime = true,
bool updateTransformSalt = false,
bool transformKey = true);
QString keyError();
QByteArray challengeResponseKey() const;
bool challengeMasterSeed(const QByteArray& masterSeed);
const QUuid& cipher() const;
2018-03-22 22:56:05 +01:00
void setCipher(const QUuid& cipher);
Database::CompressionAlgorithm compressionAlgorithm() const;
void setCompressionAlgorithm(Database::CompressionAlgorithm algo);
QSharedPointer<Kdf> kdf() const;
void setKdf(QSharedPointer<Kdf> kdf);
bool changeKdf(const QSharedPointer<Kdf>& kdf);
QByteArray transformedDatabaseKey() const;
2018-03-22 22:56:05 +01:00
static Database* databaseByUuid(const QUuid& uuid);
public slots:
void markAsModified();
void markAsClean();
void updateCommonUsernames(int topN = 10);
void updateTagList();
void markNonDataChange();
signals:
void filePathChanged(const QString& oldPath, const QString& newPath);
void groupDataChanged(Group* group);
void groupAboutToAdd(Group* group, int index);
void groupAdded();
void groupAboutToRemove(Group* group);
void groupRemoved();
void groupAboutToMove(Group* group, Group* toGroup, int index);
void groupMoved();
void databaseOpened();
void databaseSaved();
void databaseDiscarded();
void databaseFileChanged();
void tagListUpdated();
2012-06-24 17:53:01 +02:00
2010-08-07 15:10:44 +02:00
private:
struct DatabaseData
{
quint32 formatVersion = 0;
QString filePath;
QUuid cipher = KeePass2::CIPHER_AES256;
CompressionAlgorithm compressionAlgorithm = CompressionGZip;
QScopedPointer<PasswordKey> masterSeed;
QScopedPointer<PasswordKey> transformedDatabaseKey;
QScopedPointer<PasswordKey> challengeResponseKey;
QSharedPointer<const CompositeKey> key;
QSharedPointer<Kdf> kdf = QSharedPointer<AesKdf>::create(true);
QVariantMap publicCustomData;
DatabaseData()
: masterSeed(new PasswordKey())
, transformedDatabaseKey(new PasswordKey())
, challengeResponseKey(new PasswordKey())
{
kdf->randomizeSeed();
}
void clear()
{
filePath.clear();
masterSeed.reset();
transformedDatabaseKey.reset();
challengeResponseKey.reset();
key.reset();
kdf.reset();
publicCustomData.clear();
}
};
2010-08-07 15:10:44 +02:00
2012-04-21 19:06:28 +02:00
void createRecycleBin();
void startModifiedTimer();
void stopModifiedTimer();
2012-04-21 19:06:28 +02:00
QPointer<Metadata> const m_metadata;
DatabaseData m_data;
QPointer<Group> m_rootGroup;
QList<DeletedObject> m_deletedObjects;
QTimer m_modifiedTimer;
QMutex m_saveMutex;
QPointer<FileWatcher> m_fileWatcher;
bool m_modified = false;
bool m_hasNonDataChange = false;
QString m_keyError;
QStringList m_commonUsernames;
QStringList m_tagList;
2018-03-22 22:56:05 +01:00
QUuid m_uuid;
static QHash<QUuid, QPointer<Database>> s_uuidMap;
2010-08-07 15:10:44 +02:00
};
#endif // KEEPASSX_DATABASE_H