2016-01-28 22:07:04 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2016 Felix Geyer <debfx@fobos.de>
|
2017-11-27 20:41:58 +00:00
|
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
2016-01-28 22:07:04 +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 "KeePass2Repair.h"
|
|
|
|
|
|
|
|
|
|
#include <QBuffer>
|
|
|
|
|
|
2018-01-07 03:08:32 +00:00
|
|
|
#include "core/Group.h"
|
2017-11-12 18:55:03 +00:00
|
|
|
#include "format/KeePass2.h"
|
2016-01-28 22:07:04 +00:00
|
|
|
#include "format/KeePass2RandomStream.h"
|
|
|
|
|
#include "format/KeePass2Reader.h"
|
2018-01-05 15:41:29 +00:00
|
|
|
#include "format/Kdbx4Reader.h"
|
2018-01-07 03:08:32 +00:00
|
|
|
#include "format/KdbxXmlReader.h"
|
2016-01-28 22:07:04 +00:00
|
|
|
|
2017-11-27 20:41:58 +00:00
|
|
|
KeePass2Repair::RepairOutcome KeePass2Repair::repairDatabase(QIODevice* device, const CompositeKey& key)
|
2016-01-28 22:07:04 +00:00
|
|
|
{
|
|
|
|
|
m_errorStr.clear();
|
|
|
|
|
|
|
|
|
|
KeePass2Reader reader;
|
|
|
|
|
reader.setSaveXml(true);
|
|
|
|
|
|
2017-11-27 20:41:58 +00:00
|
|
|
QScopedPointer<Database> db(reader.readDatabase(device, key, true));
|
2016-01-28 22:07:04 +00:00
|
|
|
if (!reader.hasError()) {
|
2017-11-27 20:41:58 +00:00
|
|
|
return qMakePair(NothingTodo, nullptr);
|
2016-01-28 22:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
2018-01-07 03:08:32 +00:00
|
|
|
QByteArray xmlData = reader.reader()->xmlData();
|
2016-01-28 22:07:04 +00:00
|
|
|
if (!db || xmlData.isEmpty()) {
|
|
|
|
|
m_errorStr = reader.errorString();
|
2017-11-27 20:41:58 +00:00
|
|
|
return qMakePair(UnableToOpen, nullptr);
|
2016-01-28 22:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool repairAction = false;
|
|
|
|
|
|
|
|
|
|
QString xmlStart = QString::fromLatin1(xmlData.constData(), qMin(100, xmlData.size()));
|
|
|
|
|
QRegExp encodingRegExp("encoding=\"([^\"]+)\"", Qt::CaseInsensitive, QRegExp::RegExp2);
|
|
|
|
|
if (encodingRegExp.indexIn(xmlStart) != -1) {
|
|
|
|
|
if (encodingRegExp.cap(1).compare("utf-8", Qt::CaseInsensitive) != 0
|
|
|
|
|
&& encodingRegExp.cap(1).compare("utf8", Qt::CaseInsensitive) != 0)
|
|
|
|
|
{
|
|
|
|
|
// database is not utf-8 encoded, we don't support repairing that
|
2017-11-27 20:41:58 +00:00
|
|
|
return qMakePair(RepairFailed, nullptr);
|
2016-01-28 22:07:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try to fix broken databases because of bug #392
|
|
|
|
|
for (int i = (xmlData.size() - 1); i >= 0; i--) {
|
2018-01-07 03:08:32 +00:00
|
|
|
auto ch = static_cast<quint8>(xmlData.at(i));
|
2016-01-28 22:07:04 +00:00
|
|
|
if (ch < 0x20 && ch != 0x09 && ch != 0x0A && ch != 0x0D) {
|
|
|
|
|
xmlData.remove(i, 1);
|
|
|
|
|
repairAction = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!repairAction) {
|
|
|
|
|
// we were unable to find the problem
|
2017-11-27 20:41:58 +00:00
|
|
|
return qMakePair(RepairFailed, nullptr);
|
2016-01-28 22:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
2018-01-07 03:08:32 +00:00
|
|
|
KeePass2RandomStream randomStream(reader.reader()->protectedStreamAlgo());
|
|
|
|
|
randomStream.init(reader.reader()->streamKey());
|
2018-01-05 15:41:29 +00:00
|
|
|
bool hasError;
|
|
|
|
|
|
2016-01-28 22:07:04 +00:00
|
|
|
QBuffer buffer(&xmlData);
|
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
2018-01-05 15:41:29 +00:00
|
|
|
if ((reader.version() & KeePass2::FILE_VERSION_CRITICAL_MASK) < KeePass2::FILE_VERSION_4) {
|
2018-01-07 03:08:32 +00:00
|
|
|
KdbxXmlReader xmlReader(KeePass2::FILE_VERSION_3);
|
2018-01-05 15:41:29 +00:00
|
|
|
xmlReader.readDatabase(&buffer, db.data(), &randomStream);
|
|
|
|
|
hasError = xmlReader.hasError();
|
|
|
|
|
} else {
|
|
|
|
|
auto reader4 = reader.reader().staticCast<Kdbx4Reader>();
|
|
|
|
|
QHash<QString, QByteArray> pool = reader4->binaryPool();
|
2018-01-07 03:08:32 +00:00
|
|
|
KdbxXmlReader xmlReader(KeePass2::FILE_VERSION_4, pool);
|
2018-01-05 15:41:29 +00:00
|
|
|
xmlReader.readDatabase(&buffer, db.data(), &randomStream);
|
|
|
|
|
hasError = xmlReader.hasError();
|
|
|
|
|
}
|
2016-01-28 22:07:04 +00:00
|
|
|
|
2018-01-05 15:41:29 +00:00
|
|
|
if (hasError) {
|
2017-11-27 20:41:58 +00:00
|
|
|
return qMakePair(RepairFailed, nullptr);
|
2016-01-28 22:07:04 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2017-11-27 20:41:58 +00:00
|
|
|
return qMakePair(RepairSuccess, db.take());
|
2016-01-28 22:07:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString KeePass2Repair::errorString() const
|
|
|
|
|
{
|
|
|
|
|
return m_errorStr;
|
|
|
|
|
}
|