keepassxc/src/core/Database.cpp

300 lines
6.3 KiB
C++
Raw Normal View History

2010-08-07 09:10:44 -04: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/>.
*/
#include "Database.h"
#include <QFile>
#include <QTimer>
#include <QXmlStreamReader>
2010-08-07 09:10:44 -04:00
2011-07-08 07:57:02 -04:00
#include "core/Group.h"
#include "core/Metadata.h"
#include "core/Tools.h"
#include "crypto/Random.h"
#include "format/KeePass2.h"
2010-08-07 09:10:44 -04:00
QHash<Uuid, Database*> Database::m_uuidMap;
2010-08-13 12:08:06 -04:00
Database::Database()
2012-04-23 13:44:43 -04:00
: m_metadata(new Metadata(this))
2012-06-24 11:53:01 -04:00
, m_timer(new QTimer(this))
, m_emitModified(false)
, m_uuid(Uuid::random())
2010-08-07 09:10:44 -04:00
{
2013-11-22 04:28:11 -05:00
m_data.cipher = KeePass2::CIPHER_AES;
m_data.compressionAlgo = CompressionGZip;
m_data.transformRounds = 50000;
m_data.hasKey = false;
setRootGroup(new Group());
rootGroup()->setUuid(Uuid::random());
2012-06-24 11:53:01 -04:00
m_timer->setSingleShot(true);
m_uuidMap.insert(m_uuid, this);
2012-06-24 11:53:01 -04:00
connect(m_metadata, SIGNAL(modified()), this, SIGNAL(modifiedImmediate()));
connect(m_metadata, SIGNAL(nameTextChanged()), this, SIGNAL(nameTextChanged()));
2012-06-24 11:53:01 -04:00
connect(this, SIGNAL(modifiedImmediate()), this, SLOT(startModifiedTimer()));
connect(m_timer, SIGNAL(timeout()), SIGNAL(modified()));
2010-08-07 09:10:44 -04:00
}
Database::~Database()
{
m_uuidMap.remove(m_uuid);
}
Group* Database::rootGroup()
{
return m_rootGroup;
}
2010-08-15 09:03:47 -04:00
const Group* Database::rootGroup() const
{
return m_rootGroup;
}
void Database::setRootGroup(Group* group)
{
Q_ASSERT(group);
2011-07-08 07:57:02 -04:00
m_rootGroup = group;
m_rootGroup->setParent(this);
}
Metadata* Database::metadata()
{
return m_metadata;
}
2010-09-19 15:22:24 -04:00
const Metadata* Database::metadata() const
{
2010-09-19 15:22:24 -04:00
return m_metadata;
}
Entry* Database::resolveEntry(const Uuid& uuid)
{
return recFindEntry(uuid, m_rootGroup);
}
Entry* Database::recFindEntry(const Uuid& uuid, Group* group)
{
Q_FOREACH (Entry* entry, group->entries()) {
2012-04-18 18:25:57 -04:00
if (entry->uuid() == uuid) {
return entry;
2012-04-18 18:25:57 -04:00
}
}
Q_FOREACH (Group* child, group->children()) {
Entry* result = recFindEntry(uuid, child);
2012-04-18 18:25:57 -04:00
if (result) {
return result;
2012-04-18 18:25:57 -04:00
}
}
return Q_NULLPTR;
}
Group* Database::resolveGroup(const Uuid& uuid)
{
return recFindGroup(uuid, m_rootGroup);
}
Group* Database::recFindGroup(const Uuid& uuid, Group* group)
{
2012-04-18 18:25:57 -04:00
if (group->uuid() == uuid) {
return group;
2012-04-18 18:25:57 -04:00
}
Q_FOREACH (Group* child, group->children()) {
2010-09-13 17:16:28 -04:00
Group* result = recFindGroup(uuid, child);
2012-04-18 18:25:57 -04:00
if (result) {
2010-09-13 17:16:28 -04:00
return result;
2012-04-18 18:25:57 -04:00
}
}
return Q_NULLPTR;
}
QList<DeletedObject> Database::deletedObjects()
{
return m_deletedObjects;
}
void Database::addDeletedObject(const DeletedObject& delObj)
{
Q_ASSERT(delObj.deletionTime.timeSpec() == Qt::UTC);
m_deletedObjects.append(delObj);
}
void Database::addDeletedObject(const Uuid& uuid)
{
DeletedObject delObj;
delObj.deletionTime = Tools::currentDateTimeUtc();
delObj.uuid = uuid;
addDeletedObject(delObj);
}
Uuid Database::cipher() const
{
2013-11-22 04:28:11 -05:00
return m_data.cipher;
}
Database::CompressionAlgorithm Database::compressionAlgo() const
{
2013-11-22 04:28:11 -05:00
return m_data.compressionAlgo;
}
QByteArray Database::transformSeed() const
{
2013-11-22 04:28:11 -05:00
return m_data.transformSeed;
}
quint64 Database::transformRounds() const
{
2013-11-22 04:28:11 -05:00
return m_data.transformRounds;
}
QByteArray Database::transformedMasterKey() const
{
2013-11-22 04:28:11 -05:00
return m_data.transformedMasterKey;
}
void Database::setCipher(const Uuid& cipher)
{
Q_ASSERT(!cipher.isNull());
2013-11-22 04:28:11 -05:00
m_data.cipher = cipher;
}
void Database::setCompressionAlgo(Database::CompressionAlgorithm algo)
{
Q_ASSERT(static_cast<quint32>(algo) <= CompressionAlgorithmMax);
2013-11-22 04:28:11 -05:00
m_data.compressionAlgo = algo;
}
void Database::setTransformRounds(quint64 rounds)
{
2013-11-22 04:28:11 -05:00
if (m_data.transformRounds != rounds) {
m_data.transformRounds = rounds;
2013-11-22 04:28:11 -05:00
if (m_data.hasKey) {
setKey(m_data.key);
}
}
}
2012-04-11 09:57:11 -04:00
void Database::setKey(const CompositeKey& key, const QByteArray& transformSeed, bool updateChangedTime)
{
2013-11-22 04:28:11 -05:00
m_data.key = key;
m_data.transformSeed = transformSeed;
m_data.transformedMasterKey = key.transform(transformSeed, transformRounds());
m_data.hasKey = true;
2012-04-11 09:57:11 -04:00
if (updateChangedTime) {
m_metadata->setMasterKeyChanged(Tools::currentDateTimeUtc());
2012-04-11 09:57:11 -04:00
}
2012-06-24 11:53:01 -04:00
Q_EMIT modifiedImmediate();
}
void Database::setKey(const CompositeKey& key)
{
setKey(key, randomGen()->randomArray(32));
}
bool Database::hasKey() const
{
2013-11-22 04:28:11 -05:00
return m_data.hasKey;
}
bool Database::verifyKey(const CompositeKey& key) const
{
Q_ASSERT(hasKey());
2013-11-22 04:28:11 -05:00
return (m_data.key.rawKey() == key.rawKey());
}
2012-04-21 13:06:28 -04:00
void Database::createRecycleBin()
{
Group* recycleBin = Group::createRecycleBin();
2012-04-21 13:06:28 -04:00
recycleBin->setParent(rootGroup());
m_metadata->setRecycleBin(recycleBin);
}
void Database::recycleEntry(Entry* entry)
{
if (m_metadata->recycleBinEnabled()) {
if (!m_metadata->recycleBin()) {
2012-04-21 13:06:28 -04:00
createRecycleBin();
}
entry->setGroup(metadata()->recycleBin());
}
else {
delete entry;
}
}
2012-04-21 13:06:28 -04:00
void Database::recycleGroup(Group* group)
{
if (m_metadata->recycleBinEnabled()) {
if (!m_metadata->recycleBin()) {
createRecycleBin();
}
group->setParent(metadata()->recycleBin());
}
else {
delete group;
2012-06-24 11:53:01 -04:00
}
}
void Database::setEmitModified(bool value)
{
if (m_emitModified && !value) {
m_timer->stop();
2012-04-21 13:06:28 -04:00
}
2012-06-24 11:53:01 -04:00
m_emitModified = value;
2012-04-21 13:06:28 -04:00
}
2013-11-22 04:28:11 -05:00
void Database::copyAttributesFrom(const Database* other)
{
m_data = other->m_data;
m_metadata->copyAttributesFrom(other->m_metadata);
}
Uuid Database::uuid()
{
return m_uuid;
}
Database* Database::databaseByUuid(const Uuid& uuid)
{
return m_uuidMap.value(uuid, 0);
}
2012-06-24 11:53:01 -04:00
void Database::startModifiedTimer()
{
if (!m_emitModified) {
return;
}
if (m_timer->isActive()) {
m_timer->stop();
}
m_timer->start(150);
}