2010-08-07 13:10:44 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
2017-06-09 21:40:36 +00:00
|
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
2010-08-07 13:10:44 +00:00
|
|
|
*
|
|
|
|
|
* 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"
|
|
|
|
|
|
2018-03-22 21:56:05 +00:00
|
|
|
#include <QDebug>
|
2013-10-03 13:18:16 +00:00
|
|
|
#include <QFile>
|
2018-01-18 01:13:13 +00:00
|
|
|
#include <QSaveFile>
|
2018-01-14 23:04:33 +00:00
|
|
|
#include <QTemporaryFile>
|
2017-05-22 21:53:41 +00:00
|
|
|
#include <QTextStream>
|
2013-10-03 13:18:16 +00:00
|
|
|
#include <QTimer>
|
|
|
|
|
#include <QXmlStreamReader>
|
2010-08-07 13:10:44 +00:00
|
|
|
|
2017-07-22 23:40:30 +00:00
|
|
|
#include "cli/Utils.h"
|
2018-09-30 12:45:06 +00:00
|
|
|
#include "core/Clock.h"
|
2011-07-08 11:57:02 +00:00
|
|
|
#include "core/Group.h"
|
2018-09-30 12:45:06 +00:00
|
|
|
#include "core/Merger.h"
|
2011-07-08 11:57:02 +00:00
|
|
|
#include "core/Metadata.h"
|
2017-11-12 12:20:57 +00:00
|
|
|
#include "crypto/kdf/AesKdf.h"
|
2011-06-29 14:47:05 +00:00
|
|
|
#include "format/KeePass2.h"
|
2017-03-12 17:47:05 +00:00
|
|
|
#include "format/KeePass2Reader.h"
|
2017-06-14 23:50:19 +00:00
|
|
|
#include "format/KeePass2Writer.h"
|
2017-07-25 17:41:52 +00:00
|
|
|
#include "keys/FileKey.h"
|
2018-03-31 20:01:30 +00:00
|
|
|
#include "keys/PasswordKey.h"
|
2010-08-07 13:10:44 +00:00
|
|
|
|
2018-03-22 21:56:05 +00:00
|
|
|
QHash<QUuid, Database*> Database::m_uuidMap;
|
2012-04-24 22:12:23 +00:00
|
|
|
|
2010-08-13 16:08:06 +00:00
|
|
|
Database::Database()
|
2012-04-23 17:44:43 +00:00
|
|
|
: m_metadata(new Metadata(this))
|
2018-09-30 12:45:06 +00:00
|
|
|
, m_rootGroup(nullptr)
|
2012-06-24 15:53:01 +00:00
|
|
|
, m_timer(new QTimer(this))
|
|
|
|
|
, m_emitModified(false)
|
2018-03-22 21:56:05 +00:00
|
|
|
, m_uuid(QUuid::createUuid())
|
2010-08-07 13:10:44 +00:00
|
|
|
{
|
2018-09-29 17:00:47 +00:00
|
|
|
m_data.cipher = KeePass2::CIPHER_AES256;
|
2013-11-22 09:28:11 +00:00
|
|
|
m_data.compressionAlgo = CompressionGZip;
|
2018-01-10 20:24:53 +00:00
|
|
|
|
|
|
|
|
// instantiate default AES-KDF with legacy KDBX3 flag set
|
|
|
|
|
// KDBX4+ will re-initialize the KDF using parameters read from the KDBX file
|
|
|
|
|
m_data.kdf = QSharedPointer<AesKdf>::create(true);
|
2018-01-01 18:21:02 +00:00
|
|
|
m_data.kdf->randomizeSeed();
|
2013-11-22 09:28:11 +00:00
|
|
|
m_data.hasKey = false;
|
|
|
|
|
|
2011-07-09 19:54:01 +00:00
|
|
|
setRootGroup(new Group());
|
2018-03-22 21:56:05 +00:00
|
|
|
rootGroup()->setUuid(QUuid::createUuid());
|
2012-06-24 15:53:01 +00:00
|
|
|
m_timer->setSingleShot(true);
|
2011-06-29 14:47:05 +00:00
|
|
|
|
2012-04-24 22:12:23 +00:00
|
|
|
m_uuidMap.insert(m_uuid, this);
|
|
|
|
|
|
2012-06-24 15:53:01 +00:00
|
|
|
connect(m_metadata, SIGNAL(modified()), this, SIGNAL(modifiedImmediate()));
|
2012-05-14 15:04:05 +00:00
|
|
|
connect(m_metadata, SIGNAL(nameTextChanged()), this, SIGNAL(nameTextChanged()));
|
2012-06-24 15:53:01 +00:00
|
|
|
connect(this, SIGNAL(modifiedImmediate()), this, SLOT(startModifiedTimer()));
|
|
|
|
|
connect(m_timer, SIGNAL(timeout()), SIGNAL(modified()));
|
2010-08-07 13:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-24 22:12:23 +00:00
|
|
|
Database::~Database()
|
|
|
|
|
{
|
|
|
|
|
m_uuidMap.remove(m_uuid);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-12 19:38:59 +00:00
|
|
|
Group* Database::rootGroup()
|
|
|
|
|
{
|
|
|
|
|
return m_rootGroup;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-15 13:03:47 +00:00
|
|
|
const Group* Database::rootGroup() const
|
|
|
|
|
{
|
|
|
|
|
return m_rootGroup;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-12 19:38:59 +00:00
|
|
|
void Database::setRootGroup(Group* group)
|
|
|
|
|
{
|
2011-07-09 19:54:01 +00:00
|
|
|
Q_ASSERT(group);
|
2011-07-08 11:57:02 +00:00
|
|
|
|
2010-08-12 19:38:59 +00:00
|
|
|
m_rootGroup = group;
|
2011-07-09 19:54:01 +00:00
|
|
|
m_rootGroup->setParent(this);
|
2010-08-12 19:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Metadata* Database::metadata()
|
|
|
|
|
{
|
|
|
|
|
return m_metadata;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-19 19:22:24 +00:00
|
|
|
const Metadata* Database::metadata() const
|
2010-08-12 19:38:59 +00:00
|
|
|
{
|
2010-09-19 19:22:24 +00:00
|
|
|
return m_metadata;
|
2010-08-12 19:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2018-05-13 21:21:43 +00:00
|
|
|
QString Database::filePath() const
|
|
|
|
|
{
|
|
|
|
|
return m_filePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Database::setFilePath(const QString& filePath)
|
|
|
|
|
{
|
|
|
|
|
m_filePath = filePath;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 21:56:05 +00:00
|
|
|
Entry* Database::resolveEntry(const QUuid& uuid)
|
2010-08-12 19:38:59 +00:00
|
|
|
{
|
2017-11-19 19:06:09 +00:00
|
|
|
return findEntryRecursive(uuid, m_rootGroup);
|
2010-08-12 19:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-19 19:06:09 +00:00
|
|
|
Entry* Database::resolveEntry(const QString& text, EntryReferenceType referenceType)
|
2017-11-12 15:35:54 +00:00
|
|
|
{
|
2017-11-19 19:06:09 +00:00
|
|
|
return findEntryRecursive(text, referenceType, m_rootGroup);
|
2017-11-12 15:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-22 21:56:05 +00:00
|
|
|
Entry* Database::findEntryRecursive(const QUuid& uuid, Group* group)
|
2010-08-12 19:38:59 +00:00
|
|
|
{
|
2016-09-02 17:51:51 +00:00
|
|
|
const QList<Entry*> entryList = group->entries();
|
|
|
|
|
for (Entry* entry : entryList) {
|
2012-04-18 22:25:57 +00:00
|
|
|
if (entry->uuid() == uuid) {
|
2010-08-12 19:38:59 +00:00
|
|
|
return entry;
|
2012-04-18 22:25:57 +00:00
|
|
|
}
|
2010-08-12 19:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-02 17:51:51 +00:00
|
|
|
const QList<Group*> children = group->children();
|
|
|
|
|
for (Group* child : children) {
|
2017-11-19 19:06:09 +00:00
|
|
|
Entry* result = findEntryRecursive(uuid, child);
|
2012-04-18 22:25:57 +00:00
|
|
|
if (result) {
|
2010-08-12 19:38:59 +00:00
|
|
|
return result;
|
2012-04-18 22:25:57 +00:00
|
|
|
}
|
2010-08-12 19:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-24 16:28:12 +00:00
|
|
|
return nullptr;
|
2010-08-12 19:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-19 19:06:09 +00:00
|
|
|
Entry* Database::findEntryRecursive(const QString& text, EntryReferenceType referenceType, Group* group)
|
2017-11-12 15:35:54 +00:00
|
|
|
{
|
2018-03-31 20:01:30 +00:00
|
|
|
Q_ASSERT_X(referenceType != EntryReferenceType::Unknown,
|
|
|
|
|
"Database::findEntryRecursive",
|
2017-11-12 15:35:54 +00:00
|
|
|
"Can't search entry with \"referenceType\" parameter equal to \"Unknown\"");
|
|
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
|
const QList<Entry*> entryList = group->entries();
|
|
|
|
|
for (Entry* entry : entryList) {
|
|
|
|
|
switch (referenceType) {
|
|
|
|
|
case EntryReferenceType::Unknown:
|
|
|
|
|
return nullptr;
|
|
|
|
|
case EntryReferenceType::Title:
|
|
|
|
|
found = entry->title() == text;
|
|
|
|
|
break;
|
|
|
|
|
case EntryReferenceType::UserName:
|
|
|
|
|
found = entry->username() == text;
|
|
|
|
|
break;
|
|
|
|
|
case EntryReferenceType::Password:
|
|
|
|
|
found = entry->password() == text;
|
|
|
|
|
break;
|
|
|
|
|
case EntryReferenceType::Url:
|
|
|
|
|
found = entry->url() == text;
|
|
|
|
|
break;
|
|
|
|
|
case EntryReferenceType::Notes:
|
|
|
|
|
found = entry->notes() == text;
|
|
|
|
|
break;
|
2018-03-22 21:56:05 +00:00
|
|
|
case EntryReferenceType::QUuid:
|
|
|
|
|
found = entry->uuid() == QUuid::fromRfc4122(QByteArray::fromHex(text.toLatin1()));
|
2017-11-12 15:35:54 +00:00
|
|
|
break;
|
|
|
|
|
case EntryReferenceType::CustomAttributes:
|
|
|
|
|
found = entry->attributes()->containsValue(text);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QList<Group*> children = group->children();
|
|
|
|
|
for (Group* child : children) {
|
2017-11-19 19:06:09 +00:00
|
|
|
Entry* result = findEntryRecursive(text, referenceType, child);
|
2017-11-12 15:35:54 +00:00
|
|
|
if (result) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 21:56:05 +00:00
|
|
|
Group* Database::resolveGroup(const QUuid& uuid)
|
2010-08-12 19:38:59 +00:00
|
|
|
{
|
2017-11-19 19:06:09 +00:00
|
|
|
return findGroupRecursive(uuid, m_rootGroup);
|
2010-08-12 19:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-22 21:56:05 +00:00
|
|
|
Group* Database::findGroupRecursive(const QUuid& uuid, Group* group)
|
2010-08-12 19:38:59 +00:00
|
|
|
{
|
2012-04-18 22:25:57 +00:00
|
|
|
if (group->uuid() == uuid) {
|
2010-08-12 19:38:59 +00:00
|
|
|
return group;
|
2012-04-18 22:25:57 +00:00
|
|
|
}
|
2010-08-12 19:38:59 +00:00
|
|
|
|
2016-09-02 17:51:51 +00:00
|
|
|
const QList<Group*> children = group->children();
|
|
|
|
|
for (Group* child : children) {
|
2017-11-19 19:06:09 +00:00
|
|
|
Group* result = findGroupRecursive(uuid, child);
|
2012-04-18 22:25:57 +00:00
|
|
|
if (result) {
|
2010-09-13 21:16:28 +00:00
|
|
|
return result;
|
2012-04-18 22:25:57 +00:00
|
|
|
}
|
2010-08-12 19:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-24 16:28:12 +00:00
|
|
|
return nullptr;
|
2010-08-12 19:38:59 +00:00
|
|
|
}
|
2010-08-25 11:52:59 +00:00
|
|
|
|
|
|
|
|
QList<DeletedObject> Database::deletedObjects()
|
|
|
|
|
{
|
|
|
|
|
return m_deletedObjects;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-30 12:45:06 +00:00
|
|
|
const QList<DeletedObject>& Database::deletedObjects() const
|
|
|
|
|
{
|
|
|
|
|
return m_deletedObjects;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Database::containsDeletedObject(const QUuid& uuid) const
|
|
|
|
|
{
|
|
|
|
|
for (const DeletedObject& currentObject : m_deletedObjects) {
|
|
|
|
|
if (currentObject.uuid == uuid) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Database::containsDeletedObject(const DeletedObject& object) const
|
|
|
|
|
{
|
|
|
|
|
for (const DeletedObject& currentObject : m_deletedObjects) {
|
|
|
|
|
if (currentObject.uuid == object.uuid) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Database::setDeletedObjects(const QList<DeletedObject>& delObjs)
|
|
|
|
|
{
|
|
|
|
|
if (m_deletedObjects == delObjs) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_deletedObjects = delObjs;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-25 11:52:59 +00:00
|
|
|
void Database::addDeletedObject(const DeletedObject& delObj)
|
|
|
|
|
{
|
2012-05-15 22:04:10 +00:00
|
|
|
Q_ASSERT(delObj.deletionTime.timeSpec() == Qt::UTC);
|
2010-08-25 11:52:59 +00:00
|
|
|
m_deletedObjects.append(delObj);
|
|
|
|
|
}
|
2010-09-25 10:41:00 +00:00
|
|
|
|
2018-03-22 21:56:05 +00:00
|
|
|
void Database::addDeletedObject(const QUuid& uuid)
|
2012-04-21 22:29:39 +00:00
|
|
|
{
|
|
|
|
|
DeletedObject delObj;
|
2018-09-30 12:45:06 +00:00
|
|
|
delObj.deletionTime = Clock::currentDateTimeUtc();
|
2012-04-21 22:29:39 +00:00
|
|
|
delObj.uuid = uuid;
|
|
|
|
|
|
|
|
|
|
addDeletedObject(delObj);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 21:56:05 +00:00
|
|
|
const QUuid& Database::cipher() const
|
2010-09-25 10:41:00 +00:00
|
|
|
{
|
2013-11-22 09:28:11 +00:00
|
|
|
return m_data.cipher;
|
2010-09-25 10:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Database::CompressionAlgorithm Database::compressionAlgo() const
|
|
|
|
|
{
|
2013-11-22 09:28:11 +00:00
|
|
|
return m_data.compressionAlgo;
|
2010-09-25 10:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray Database::transformedMasterKey() const
|
|
|
|
|
{
|
2013-11-22 09:28:11 +00:00
|
|
|
return m_data.transformedMasterKey;
|
2010-09-25 10:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
2014-09-07 23:37:46 +00:00
|
|
|
QByteArray Database::challengeResponseKey() const
|
2014-05-26 08:40:38 +00:00
|
|
|
{
|
2014-09-07 23:37:46 +00:00
|
|
|
return m_data.challengeResponseKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Database::challengeMasterSeed(const QByteArray& masterSeed)
|
|
|
|
|
{
|
2014-09-07 23:43:04 +00:00
|
|
|
m_data.masterSeed = masterSeed;
|
2018-05-13 21:21:43 +00:00
|
|
|
return m_data.key->challenge(masterSeed, m_data.challengeResponseKey);
|
2014-05-26 08:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-22 21:56:05 +00:00
|
|
|
void Database::setCipher(const QUuid& cipher)
|
2010-09-25 10:41:00 +00:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(!cipher.isNull());
|
|
|
|
|
|
2013-11-22 09:28:11 +00:00
|
|
|
m_data.cipher = cipher;
|
2010-09-25 10:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Database::setCompressionAlgo(Database::CompressionAlgorithm algo)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(static_cast<quint32>(algo) <= CompressionAlgorithmMax);
|
|
|
|
|
|
2013-11-22 09:28:11 +00:00
|
|
|
m_data.compressionAlgo = algo;
|
2010-09-25 10:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
2018-01-07 17:46:24 +00:00
|
|
|
/**
|
|
|
|
|
* Set and transform a new encryption key.
|
|
|
|
|
*
|
2018-05-13 21:21:43 +00:00
|
|
|
* @param key key to set and transform or nullptr to reset the key
|
2018-01-07 17:46:24 +00:00
|
|
|
* @param updateChangedTime true to update database change time
|
|
|
|
|
* @param updateTransformSalt true to update the transform salt
|
|
|
|
|
* @return true on success
|
|
|
|
|
*/
|
2018-05-13 21:21:43 +00:00
|
|
|
bool Database::setKey(QSharedPointer<const CompositeKey> key, bool updateChangedTime, bool updateTransformSalt)
|
2010-09-25 10:41:00 +00:00
|
|
|
{
|
2018-05-13 21:21:43 +00:00
|
|
|
if (!key) {
|
|
|
|
|
m_data.key.reset();
|
|
|
|
|
m_data.transformedMasterKey = {};
|
|
|
|
|
m_data.hasKey = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-12 12:20:57 +00:00
|
|
|
if (updateTransformSalt) {
|
2018-01-01 18:21:02 +00:00
|
|
|
m_data.kdf->randomizeSeed();
|
2018-01-07 17:46:24 +00:00
|
|
|
Q_ASSERT(!m_data.kdf->seed().isEmpty());
|
2012-07-17 08:31:00 +00:00
|
|
|
}
|
2015-05-09 17:47:53 +00:00
|
|
|
|
2018-01-01 18:59:57 +00:00
|
|
|
QByteArray oldTransformedMasterKey = m_data.transformedMasterKey;
|
2017-11-12 12:20:57 +00:00
|
|
|
QByteArray transformedMasterKey;
|
2018-05-13 21:21:43 +00:00
|
|
|
if (!key->transform(*m_data.kdf, transformedMasterKey)) {
|
2015-05-09 17:47:53 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-22 09:28:11 +00:00
|
|
|
m_data.key = key;
|
2015-05-09 17:47:53 +00:00
|
|
|
m_data.transformedMasterKey = transformedMasterKey;
|
2013-11-22 09:28:11 +00:00
|
|
|
m_data.hasKey = true;
|
2012-04-11 13:57:11 +00:00
|
|
|
if (updateChangedTime) {
|
2018-09-30 12:45:06 +00:00
|
|
|
m_metadata->setMasterKeyChanged(Clock::currentDateTimeUtc());
|
2012-04-11 13:57:11 +00:00
|
|
|
}
|
2018-01-01 18:59:57 +00:00
|
|
|
|
|
|
|
|
if (oldTransformedMasterKey != m_data.transformedMasterKey) {
|
|
|
|
|
emit modifiedImmediate();
|
|
|
|
|
}
|
2015-05-09 17:47:53 +00:00
|
|
|
|
|
|
|
|
return true;
|
2011-06-29 14:39:39 +00:00
|
|
|
}
|
2010-09-25 10:41:00 +00:00
|
|
|
|
2012-10-12 10:10:41 +00:00
|
|
|
bool Database::hasKey() const
|
2012-04-16 19:03:35 +00:00
|
|
|
{
|
2013-11-22 09:28:11 +00:00
|
|
|
return m_data.hasKey;
|
2012-04-16 19:03:35 +00:00
|
|
|
}
|
2012-04-18 19:59:00 +00:00
|
|
|
|
2018-05-13 21:21:43 +00:00
|
|
|
bool Database::verifyKey(QSharedPointer<CompositeKey> key) const
|
2012-10-12 10:10:41 +00:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(hasKey());
|
|
|
|
|
|
2014-09-07 23:43:04 +00:00
|
|
|
if (!m_data.challengeResponseKey.isEmpty()) {
|
|
|
|
|
QByteArray result;
|
|
|
|
|
|
2018-05-13 21:21:43 +00:00
|
|
|
if (!key->challenge(m_data.masterSeed, result)) {
|
2017-02-23 22:52:36 +00:00
|
|
|
// challenge failed, (YubiKey?) removed?
|
2014-09-07 23:43:04 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_data.challengeResponseKey != result) {
|
2017-02-23 22:52:36 +00:00
|
|
|
// wrong response from challenged device(s)
|
2014-09-07 23:43:04 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-13 21:21:43 +00:00
|
|
|
return (m_data.key->rawKey() == key->rawKey());
|
2012-10-12 10:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-01 20:25:29 +00:00
|
|
|
QVariantMap& Database::publicCustomData()
|
|
|
|
|
{
|
|
|
|
|
return m_data.publicCustomData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QVariantMap& Database::publicCustomData() const
|
2018-02-18 20:01:22 +00:00
|
|
|
{
|
|
|
|
|
return m_data.publicCustomData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Database::setPublicCustomData(const QVariantMap& customData)
|
|
|
|
|
{
|
|
|
|
|
m_data.publicCustomData = customData;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-21 17:06:28 +00:00
|
|
|
void Database::createRecycleBin()
|
|
|
|
|
{
|
2012-07-21 09:57:00 +00:00
|
|
|
Group* recycleBin = Group::createRecycleBin();
|
2012-04-21 17:06:28 +00:00
|
|
|
recycleBin->setParent(rootGroup());
|
|
|
|
|
m_metadata->setRecycleBin(recycleBin);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-18 19:59:00 +00:00
|
|
|
void Database::recycleEntry(Entry* entry)
|
|
|
|
|
{
|
|
|
|
|
if (m_metadata->recycleBinEnabled()) {
|
|
|
|
|
if (!m_metadata->recycleBin()) {
|
2012-04-21 17:06:28 +00:00
|
|
|
createRecycleBin();
|
2012-04-18 19:59:00 +00:00
|
|
|
}
|
2012-04-18 21:17:29 +00:00
|
|
|
entry->setGroup(metadata()->recycleBin());
|
2017-06-15 14:31:14 +00:00
|
|
|
} else {
|
2012-04-18 19:59:00 +00:00
|
|
|
delete entry;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-21 17:06:28 +00:00
|
|
|
|
|
|
|
|
void Database::recycleGroup(Group* group)
|
|
|
|
|
{
|
2017-06-15 14:31:14 +00:00
|
|
|
if (m_metadata->recycleBinEnabled()) {
|
2012-04-21 17:06:28 +00:00
|
|
|
if (!m_metadata->recycleBin()) {
|
|
|
|
|
createRecycleBin();
|
|
|
|
|
}
|
|
|
|
|
group->setParent(metadata()->recycleBin());
|
2017-06-15 14:31:14 +00:00
|
|
|
} else {
|
2012-04-21 17:06:28 +00:00
|
|
|
delete group;
|
2017-06-15 14:31:14 +00:00
|
|
|
}
|
2012-06-24 15:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
2017-04-21 14:33:06 +00:00
|
|
|
void Database::emptyRecycleBin()
|
|
|
|
|
{
|
|
|
|
|
if (m_metadata->recycleBinEnabled() && m_metadata->recycleBin()) {
|
|
|
|
|
// destroying direct entries of the recycle bin
|
|
|
|
|
QList<Entry*> subEntries = m_metadata->recycleBin()->entries();
|
|
|
|
|
for (Entry* entry : subEntries) {
|
|
|
|
|
delete entry;
|
|
|
|
|
}
|
|
|
|
|
// destroying direct subgroups of the recycle bin
|
|
|
|
|
QList<Group*> subGroups = m_metadata->recycleBin()->children();
|
|
|
|
|
for (Group* group : subGroups) {
|
|
|
|
|
delete group;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-24 15:53:01 +00:00
|
|
|
void Database::setEmitModified(bool value)
|
|
|
|
|
{
|
|
|
|
|
if (m_emitModified && !value) {
|
|
|
|
|
m_timer->stop();
|
2012-04-21 17:06:28 +00:00
|
|
|
}
|
2012-06-24 15:53:01 +00:00
|
|
|
|
|
|
|
|
m_emitModified = value;
|
2012-04-21 17:06:28 +00:00
|
|
|
}
|
2012-04-24 22:12:23 +00:00
|
|
|
|
2018-09-30 12:45:06 +00:00
|
|
|
void Database::markAsModified()
|
|
|
|
|
{
|
|
|
|
|
emit modified();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 21:56:05 +00:00
|
|
|
const QUuid& Database::uuid()
|
2012-04-24 22:12:23 +00:00
|
|
|
{
|
|
|
|
|
return m_uuid;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 21:56:05 +00:00
|
|
|
Database* Database::databaseByUuid(const QUuid& uuid)
|
2012-04-24 22:12:23 +00:00
|
|
|
{
|
|
|
|
|
return m_uuidMap.value(uuid, 0);
|
|
|
|
|
}
|
2012-06-24 15:53:01 +00:00
|
|
|
|
|
|
|
|
void Database::startModifiedTimer()
|
|
|
|
|
{
|
|
|
|
|
if (!m_emitModified) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_timer->isActive()) {
|
|
|
|
|
m_timer->stop();
|
|
|
|
|
}
|
|
|
|
|
m_timer->start(150);
|
|
|
|
|
}
|
2014-05-03 14:59:41 +00:00
|
|
|
|
2018-05-13 21:21:43 +00:00
|
|
|
QSharedPointer<const CompositeKey> Database::key() const
|
2014-05-03 14:59:41 +00:00
|
|
|
{
|
|
|
|
|
return m_data.key;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-13 21:21:43 +00:00
|
|
|
Database* Database::openDatabaseFile(const QString& fileName, QSharedPointer<const CompositeKey> key)
|
2017-03-12 17:47:05 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
QFile dbFile(fileName);
|
|
|
|
|
if (!dbFile.exists()) {
|
|
|
|
|
qCritical("File %s does not exist.", qPrintable(fileName));
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (!dbFile.open(QIODevice::ReadOnly)) {
|
|
|
|
|
qCritical("Unable to open file %s.", qPrintable(fileName));
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KeePass2Reader reader;
|
|
|
|
|
Database* db = reader.readDatabase(&dbFile, key);
|
|
|
|
|
if (reader.hasError()) {
|
|
|
|
|
qCritical("Error while parsing the database: %s", qPrintable(reader.errorString()));
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return db;
|
|
|
|
|
}
|
2017-05-22 21:53:41 +00:00
|
|
|
|
2018-09-29 17:00:47 +00:00
|
|
|
Database* Database::unlockFromStdin(QString databaseFilename, QString keyFilename, FILE* outputDescriptor, FILE* errorDescriptor)
|
2017-05-22 21:53:41 +00:00
|
|
|
{
|
2018-05-13 21:21:43 +00:00
|
|
|
auto compositeKey = QSharedPointer<CompositeKey>::create();
|
2018-09-29 17:00:47 +00:00
|
|
|
QTextStream out(outputDescriptor);
|
|
|
|
|
QTextStream err(errorDescriptor);
|
2017-07-31 14:17:08 +00:00
|
|
|
|
2018-09-29 17:00:47 +00:00
|
|
|
out << QObject::tr("Insert password to unlock %1: ").arg(databaseFilename);
|
|
|
|
|
out.flush();
|
2017-07-31 14:17:08 +00:00
|
|
|
|
|
|
|
|
QString line = Utils::getPassword();
|
2018-05-13 21:21:43 +00:00
|
|
|
auto passwordKey = QSharedPointer<PasswordKey>::create();
|
|
|
|
|
passwordKey->setPassword(line);
|
|
|
|
|
compositeKey->addKey(passwordKey);
|
2017-07-29 21:24:24 +00:00
|
|
|
|
|
|
|
|
if (!keyFilename.isEmpty()) {
|
2018-05-13 21:21:43 +00:00
|
|
|
auto fileKey = QSharedPointer<FileKey>::create();
|
2017-07-29 21:24:24 +00:00
|
|
|
QString errorMessage;
|
2018-09-29 17:00:47 +00:00
|
|
|
// LCOV_EXCL_START
|
2018-05-13 21:21:43 +00:00
|
|
|
if (!fileKey->load(keyFilename, &errorMessage)) {
|
2018-09-29 17:00:47 +00:00
|
|
|
err << QObject::tr("Failed to load key file %1: %2").arg(keyFilename, errorMessage)<< endl;
|
2017-07-29 21:24:24 +00:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
2018-09-29 17:00:47 +00:00
|
|
|
|
|
|
|
|
if (fileKey->type() != FileKey::Hashed) {
|
|
|
|
|
err << QObject::tr("WARNING: You are using a legacy key file format which may become\n"
|
|
|
|
|
"unsupported in the future.\n\n"
|
|
|
|
|
"Please consider generating a new key file.") << endl;
|
|
|
|
|
}
|
|
|
|
|
// LCOV_EXCL_STOP
|
|
|
|
|
|
2018-05-13 21:21:43 +00:00
|
|
|
compositeKey->addKey(fileKey);
|
2017-07-29 21:24:24 +00:00
|
|
|
}
|
|
|
|
|
|
2017-07-25 17:41:52 +00:00
|
|
|
return Database::openDatabaseFile(databaseFilename, compositeKey);
|
2017-05-22 21:53:41 +00:00
|
|
|
}
|
2017-06-14 23:50:19 +00:00
|
|
|
|
2018-01-14 23:04:33 +00:00
|
|
|
/**
|
|
|
|
|
* Save the database to a file.
|
|
|
|
|
*
|
|
|
|
|
* This function uses QTemporaryFile instead of QSaveFile due to a bug
|
|
|
|
|
* in Qt (https://bugreports.qt.io/browse/QTBUG-57299) that may prevent
|
2018-01-28 15:14:36 +00:00
|
|
|
* the QSaveFile from renaming itself when using Dropbox, Drive, or OneDrive.
|
2018-01-14 23:04:33 +00:00
|
|
|
*
|
|
|
|
|
* The risk in using QTemporaryFile is that the rename function is not atomic
|
|
|
|
|
* and may result in loss of data if there is a crash or power loss at the
|
|
|
|
|
* wrong moment.
|
|
|
|
|
*
|
|
|
|
|
* @param filePath Absolute path of the file to save
|
2018-01-18 01:13:13 +00:00
|
|
|
* @param atomic Use atomic file transactions
|
|
|
|
|
* @param backup Backup the existing database file, if exists
|
2018-01-14 23:04:33 +00:00
|
|
|
* @return error string, if any
|
|
|
|
|
*/
|
2018-01-18 01:13:13 +00:00
|
|
|
QString Database::saveToFile(QString filePath, bool atomic, bool backup)
|
|
|
|
|
{
|
|
|
|
|
QString error;
|
|
|
|
|
if (atomic) {
|
|
|
|
|
QSaveFile saveFile(filePath);
|
|
|
|
|
if (saveFile.open(QIODevice::WriteOnly)) {
|
|
|
|
|
// write the database to the file
|
|
|
|
|
error = writeDatabase(&saveFile);
|
|
|
|
|
if (!error.isEmpty()) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (backup) {
|
|
|
|
|
backupDatabase(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (saveFile.commit()) {
|
|
|
|
|
// successfully saved database file
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2017-06-14 23:50:19 +00:00
|
|
|
}
|
2018-01-18 01:13:13 +00:00
|
|
|
error = saveFile.errorString();
|
|
|
|
|
} else {
|
|
|
|
|
QTemporaryFile tempFile;
|
|
|
|
|
if (tempFile.open()) {
|
|
|
|
|
// write the database to the file
|
|
|
|
|
error = writeDatabase(&tempFile);
|
|
|
|
|
if (!error.isEmpty()) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tempFile.close(); // flush to disk
|
|
|
|
|
|
|
|
|
|
if (backup) {
|
|
|
|
|
backupDatabase(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete the original db and move the temp file in place
|
|
|
|
|
QFile::remove(filePath);
|
2018-02-27 19:06:33 +00:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
|
// workaround to make this workaround work, see: https://bugreports.qt.io/browse/QTBUG-64008
|
|
|
|
|
if (tempFile.copy(filePath)) {
|
|
|
|
|
// successfully saved database file
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
#else
|
2018-01-18 01:13:13 +00:00
|
|
|
if (tempFile.rename(filePath)) {
|
|
|
|
|
// successfully saved database file
|
|
|
|
|
tempFile.setAutoRemove(false);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2018-02-27 19:06:33 +00:00
|
|
|
#endif
|
2018-01-14 23:04:33 +00:00
|
|
|
}
|
2018-01-18 01:13:13 +00:00
|
|
|
error = tempFile.errorString();
|
|
|
|
|
}
|
|
|
|
|
// Saving failed
|
|
|
|
|
return error;
|
|
|
|
|
}
|
2018-01-14 23:04:33 +00:00
|
|
|
|
2018-01-18 01:13:13 +00:00
|
|
|
QString Database::writeDatabase(QIODevice* device)
|
|
|
|
|
{
|
|
|
|
|
KeePass2Writer writer;
|
|
|
|
|
setEmitModified(false);
|
|
|
|
|
writer.writeDatabase(device, this);
|
|
|
|
|
setEmitModified(true);
|
|
|
|
|
|
|
|
|
|
if (writer.hasError()) {
|
|
|
|
|
// the writer failed
|
|
|
|
|
return writer.errorString();
|
2017-06-14 23:50:19 +00:00
|
|
|
}
|
2018-01-18 01:13:13 +00:00
|
|
|
return {};
|
|
|
|
|
}
|
2018-01-14 23:04:33 +00:00
|
|
|
|
2018-01-18 01:13:13 +00:00
|
|
|
/**
|
|
|
|
|
* Remove the old backup and replace it with a new one
|
2018-01-28 15:14:36 +00:00
|
|
|
* backups are named <filename>.old.kdbx
|
2018-01-18 01:13:13 +00:00
|
|
|
*
|
|
|
|
|
* @param filePath Path to the file to backup
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
bool Database::backupDatabase(QString filePath)
|
|
|
|
|
{
|
|
|
|
|
QString backupFilePath = filePath;
|
2018-02-27 14:28:12 +00:00
|
|
|
auto re = QRegularExpression("\\.kdbx$|(?<!\\.kdbx)$", QRegularExpression::CaseInsensitiveOption);
|
2018-01-28 15:14:36 +00:00
|
|
|
backupFilePath.replace(re, ".old.kdbx");
|
2018-01-18 01:13:13 +00:00
|
|
|
QFile::remove(backupFilePath);
|
|
|
|
|
return QFile::copy(filePath, backupFilePath);
|
2017-06-14 23:50:19 +00:00
|
|
|
}
|
2017-11-12 12:20:57 +00:00
|
|
|
|
2017-12-16 17:36:42 +00:00
|
|
|
QSharedPointer<Kdf> Database::kdf() const
|
|
|
|
|
{
|
2017-11-12 12:20:57 +00:00
|
|
|
return m_data.kdf;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-16 17:36:42 +00:00
|
|
|
void Database::setKdf(QSharedPointer<Kdf> kdf)
|
|
|
|
|
{
|
|
|
|
|
m_data.kdf = std::move(kdf);
|
2017-11-12 12:20:57 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-16 17:36:42 +00:00
|
|
|
bool Database::changeKdf(QSharedPointer<Kdf> kdf)
|
|
|
|
|
{
|
2018-01-01 18:21:02 +00:00
|
|
|
kdf->randomizeSeed();
|
2017-11-12 12:20:57 +00:00
|
|
|
QByteArray transformedMasterKey;
|
2018-05-13 21:21:43 +00:00
|
|
|
if (!m_data.key) {
|
|
|
|
|
m_data.key = QSharedPointer<CompositeKey>::create();
|
|
|
|
|
}
|
|
|
|
|
if (!m_data.key->transform(*kdf, transformedMasterKey)) {
|
2017-11-12 12:20:57 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setKdf(kdf);
|
|
|
|
|
m_data.transformedMasterKey = transformedMasterKey;
|
|
|
|
|
emit modifiedImmediate();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|