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/>.
|
|
|
|
*/
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
#include "KeePass2XmlReader.h"
|
2010-08-07 09:10:44 -04:00
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
#include <QtCore/QBuffer>
|
2010-08-07 09:10:44 -04:00
|
|
|
#include <QtCore/QFile>
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
#include "core/Database.h"
|
2010-09-22 18:21:36 -04:00
|
|
|
#include "core/DatabaseIcons.h"
|
2011-07-08 07:57:02 -04:00
|
|
|
#include "core/Group.h"
|
2010-08-31 08:39:35 -04:00
|
|
|
#include "core/Metadata.h"
|
2012-05-02 05:06:24 -04:00
|
|
|
#include "core/Tools.h"
|
2011-07-06 18:15:52 -04:00
|
|
|
#include "format/KeePass2RandomStream.h"
|
2012-04-21 10:45:46 -04:00
|
|
|
#include "streams/QtIOCompressor"
|
2010-08-12 15:38:59 -04:00
|
|
|
|
2010-08-31 10:18:45 -04:00
|
|
|
KeePass2XmlReader::KeePass2XmlReader()
|
2011-07-06 18:15:52 -04:00
|
|
|
: m_randomStream(0)
|
2011-01-13 16:31:17 -05:00
|
|
|
, m_db(0)
|
2010-08-31 10:18:45 -04:00
|
|
|
, m_meta(0)
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-07-06 18:15:52 -04:00
|
|
|
void KeePass2XmlReader::readDatabase(QIODevice* device, Database* db, KeePass2RandomStream* randomStream)
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
2012-01-06 14:03:13 -05:00
|
|
|
m_xml.clear();
|
2010-08-31 10:18:45 -04:00
|
|
|
m_xml.setDevice(device);
|
|
|
|
|
2010-09-25 06:41:00 -04:00
|
|
|
m_db = db;
|
2010-08-31 10:18:45 -04:00
|
|
|
m_meta = m_db->metadata();
|
2012-04-11 09:57:11 -04:00
|
|
|
m_meta->setUpdateDatetime(false);
|
|
|
|
|
2011-07-06 18:15:52 -04:00
|
|
|
m_randomStream = randomStream;
|
2010-08-07 09:10:44 -04:00
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
m_tmpParent = new Group();
|
2010-08-07 09:10:44 -04:00
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
if (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "KeePassFile") {
|
|
|
|
parseKeePassFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-11 12:46:23 -04:00
|
|
|
if (!m_xml.error()) {
|
|
|
|
if (!m_tmpParent->children().isEmpty()) {
|
|
|
|
qWarning("KeePass2XmlReader::readDatabase: found %d invalid group reference(s)",
|
|
|
|
m_tmpParent->children().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_tmpParent->entries().isEmpty()) {
|
|
|
|
qWarning("KeePass2XmlReader::readDatabase: found %d invalid entry reference(s)",
|
|
|
|
m_tmpParent->children().size());
|
|
|
|
}
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
QSet<QString> poolKeys = m_binaryPool.keys().toSet();
|
|
|
|
QSet<QString> entryKeys = m_binaryMap.keys().toSet();
|
|
|
|
QSet<QString> unmappedKeys = entryKeys - poolKeys;
|
|
|
|
QSet<QString> unusedKeys = poolKeys - entryKeys;
|
|
|
|
|
|
|
|
if (!unmappedKeys.isEmpty()) {
|
|
|
|
raiseError(17);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_xml.error()) {
|
|
|
|
Q_FOREACH (const QString& key, unusedKeys) {
|
|
|
|
qWarning("KeePass2XmlReader::readDatabase: found unused key \"%s\"", qPrintable(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<QString, QPair<Entry*, QString> >::const_iterator i;
|
|
|
|
for (i = m_binaryMap.constBegin(); i != m_binaryMap.constEnd(); ++i) {
|
2012-04-22 06:29:18 -04:00
|
|
|
const QPair<Entry*, QString>& target = i.value();
|
2012-04-21 10:45:46 -04:00
|
|
|
target.first->attachments()->set(target.second, m_binaryPool[i.key()]);
|
|
|
|
}
|
|
|
|
|
2012-04-11 09:57:11 -04:00
|
|
|
m_meta->setUpdateDatetime(true);
|
2012-04-23 11:02:09 -04:00
|
|
|
|
2012-04-22 06:29:18 -04:00
|
|
|
QHash<Uuid, Group*>::const_iterator iGroup;
|
|
|
|
for (iGroup = m_groups.constBegin(); iGroup != m_groups.constEnd(); ++iGroup) {
|
|
|
|
iGroup.value()->setUpdateTimeinfo(true);
|
2012-04-11 12:00:28 -04:00
|
|
|
}
|
2012-04-11 09:57:11 -04:00
|
|
|
|
2012-04-22 06:29:18 -04:00
|
|
|
QHash<Uuid, Entry*>::const_iterator iEntry;
|
|
|
|
for (iEntry = m_entries.constBegin(); iEntry != m_entries.constEnd(); ++iEntry) {
|
|
|
|
iEntry.value()->setUpdateTimeinfo(true);
|
|
|
|
|
|
|
|
Q_FOREACH (Entry* histEntry, iEntry.value()->historyItems()) {
|
2012-04-16 20:26:25 -04:00
|
|
|
histEntry->setUpdateTimeinfo(true);
|
|
|
|
}
|
2012-04-11 13:51:54 -04:00
|
|
|
}
|
|
|
|
|
2010-08-13 12:08:06 -04:00
|
|
|
delete m_tmpParent;
|
2010-09-25 06:41:00 -04:00
|
|
|
}
|
2010-08-13 12:08:06 -04:00
|
|
|
|
2010-09-25 06:41:00 -04:00
|
|
|
Database* KeePass2XmlReader::readDatabase(QIODevice* device)
|
|
|
|
{
|
|
|
|
Database* db = new Database();
|
|
|
|
readDatabase(device, db);
|
|
|
|
return db;
|
2010-08-31 10:18:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Database* KeePass2XmlReader::readDatabase(const QString& filename)
|
|
|
|
{
|
|
|
|
QFile file(filename);
|
2010-09-13 17:24:36 -04:00
|
|
|
file.open(QIODevice::ReadOnly);
|
2010-08-31 10:18:45 -04:00
|
|
|
return readDatabase(&file);
|
|
|
|
}
|
|
|
|
|
2012-01-06 14:03:13 -05:00
|
|
|
bool KeePass2XmlReader::hasError()
|
2010-08-31 10:18:45 -04:00
|
|
|
{
|
|
|
|
return m_xml.hasError();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
|
2010-08-31 10:18:45 -04:00
|
|
|
QString KeePass2XmlReader::errorString()
|
2010-08-13 12:08:06 -04:00
|
|
|
{
|
|
|
|
return QString("%1\nLine %2, column %3")
|
|
|
|
.arg(m_xml.errorString())
|
|
|
|
.arg(m_xml.lineNumber())
|
|
|
|
.arg(m_xml.columnNumber());
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::parseKeePassFile()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "KeePassFile");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "Meta") {
|
|
|
|
parseMeta();
|
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Root") {
|
|
|
|
parseRoot();
|
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::parseMeta()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Meta");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "Generator") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setGenerator(readString());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "DatabaseName") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setName(readString());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "DatabaseNameChanged") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setNameChanged(readDateTime());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "DatabaseDescription") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setDescription(readString());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "DatabaseDescriptionChanged") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setDescriptionChanged(readDateTime());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "DefaultUserName") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setDefaultUserName(readString());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "DefaultUserNameChanged") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setDefaultUserNameChanged(readDateTime());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "MaintenanceHistoryDays") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setMaintenanceHistoryDays(readNumber());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
2012-04-21 10:45:46 -04:00
|
|
|
else if (m_xml.name() == "Color") {
|
|
|
|
m_meta->setColor(readColor());
|
|
|
|
}
|
2010-09-25 06:41:00 -04:00
|
|
|
else if (m_xml.name() == "MasterKeyChanged") {
|
|
|
|
m_meta->setMasterKeyChanged(readDateTime());
|
|
|
|
}
|
|
|
|
else if (m_xml.name() == "MasterKeyChangeRec") {
|
|
|
|
m_meta->setMasterKeyChangeRec(readNumber());
|
|
|
|
}
|
|
|
|
else if (m_xml.name() == "MasterKeyChangeForce") {
|
|
|
|
m_meta->setMasterKeyChangeForce(readNumber());
|
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
else if (m_xml.name() == "MemoryProtection") {
|
|
|
|
parseMemoryProtection();
|
|
|
|
}
|
|
|
|
else if (m_xml.name() == "CustomIcons") {
|
|
|
|
parseCustomIcons();
|
|
|
|
}
|
|
|
|
else if (m_xml.name() == "RecycleBinEnabled") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setRecycleBinEnabled(readBool());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "RecycleBinUUID") {
|
2010-08-13 12:08:06 -04:00
|
|
|
m_meta->setRecycleBin(getGroup(readUuid()));
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "RecycleBinChanged") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setRecycleBinChanged(readDateTime());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "EntryTemplatesGroup") {
|
2010-08-13 12:08:06 -04:00
|
|
|
m_meta->setEntryTemplatesGroup(getGroup(readUuid()));
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "EntryTemplatesGroupChanged") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setEntryTemplatesGroupChanged(readDateTime());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "LastSelectedGroup") {
|
2010-08-13 12:08:06 -04:00
|
|
|
m_meta->setLastSelectedGroup(getGroup(readUuid()));
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "LastTopVisibleGroup") {
|
2010-08-13 12:08:06 -04:00
|
|
|
m_meta->setLastTopVisibleGroup(getGroup(readUuid()));
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
2012-04-21 10:45:46 -04:00
|
|
|
else if (m_xml.name() == "HistoryMaxItems") {
|
2012-05-19 09:05:07 -04:00
|
|
|
int value = readNumber();
|
|
|
|
if (value >= -1) {
|
|
|
|
m_meta->setHistoryMaxItems(value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
raiseError(18);
|
|
|
|
}
|
2012-04-21 10:45:46 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "HistoryMaxSize") {
|
2012-05-19 09:05:07 -04:00
|
|
|
int value = readNumber();
|
|
|
|
if (value >= -1) {
|
|
|
|
m_meta->setHistoryMaxSize(value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
raiseError(19);
|
|
|
|
}
|
2012-04-21 10:45:46 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Binaries") {
|
|
|
|
parseBinaries();
|
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
else if (m_xml.name() == "CustomData") {
|
|
|
|
parseCustomData();
|
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::parseMemoryProtection()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "MemoryProtection");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "ProtectTitle") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setProtectTitle(readBool());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "ProtectUserName") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setProtectUsername(readBool());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "ProtectPassword") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setProtectPassword(readBool());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "ProtectURL") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setProtectUrl(readBool());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "ProtectNotes") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setProtectNotes(readBool());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
2012-04-21 10:45:46 -04:00
|
|
|
/*else if (m_xml.name() == "AutoEnableVisualHiding") {
|
2010-08-12 15:38:59 -04:00
|
|
|
m_meta->setAutoEnableVisualHiding(readBool());
|
2012-04-21 10:45:46 -04:00
|
|
|
}*/
|
2010-08-07 09:10:44 -04:00
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::parseCustomIcons()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "CustomIcons");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "Icon") {
|
|
|
|
parseIcon();
|
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::parseIcon()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Icon");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
Uuid uuid;
|
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "UUID") {
|
2010-08-12 15:38:59 -04:00
|
|
|
uuid = readUuid();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Data") {
|
2012-01-01 15:52:54 -05:00
|
|
|
QImage icon;
|
|
|
|
icon.loadFromData(readBinary());
|
|
|
|
m_meta->addCustomIcon(uuid, icon);
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
void KeePass2XmlReader::parseBinaries()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Binaries");
|
|
|
|
|
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
|
|
|
if (m_xml.name() == "Binary") {
|
|
|
|
QXmlStreamAttributes attr = m_xml.attributes();
|
|
|
|
|
|
|
|
QString id = attr.value("ID").toString();
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
if (attr.value("Compressed").compare("True", Qt::CaseInsensitive) == 0) {
|
|
|
|
data = readCompressedBinary();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data = readBinary();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_binaryPool.contains(id)) {
|
|
|
|
qWarning("KeePass2XmlReader::parseBinaries: overwriting binary item \"%s\"",
|
|
|
|
qPrintable(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_binaryPool.insert(id, data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::parseCustomData()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "CustomData");
|
|
|
|
|
2010-08-13 12:08:06 -04:00
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-25 18:31:07 -04:00
|
|
|
if (m_xml.name() == "Item") {
|
|
|
|
parseCustomDataItem();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::parseCustomDataItem()
|
2010-08-25 18:31:07 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Item");
|
|
|
|
|
|
|
|
QString key;
|
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
|
|
|
if (m_xml.name() == "Key") {
|
|
|
|
key = readString();
|
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Value") {
|
|
|
|
m_meta->addCustomField(key, readString());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
skipCurrentElement();
|
|
|
|
}
|
2010-08-13 12:08:06 -04:00
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::parseRoot()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Root");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "Group") {
|
2010-08-12 15:38:59 -04:00
|
|
|
Group* rootGroup = parseGroup();
|
2010-08-12 15:43:57 -04:00
|
|
|
if (rootGroup) {
|
2011-07-09 15:54:01 -04:00
|
|
|
Group* oldRoot = m_db->rootGroup();
|
2011-07-08 07:57:02 -04:00
|
|
|
m_db->setRootGroup(rootGroup);
|
2011-07-09 15:54:01 -04:00
|
|
|
delete oldRoot;
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "DeletedObjects") {
|
2010-08-25 07:52:59 -04:00
|
|
|
parseDeletedObjects();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
Group* KeePass2XmlReader::parseGroup()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Group");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
Group* group = 0;
|
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "UUID") {
|
2010-08-12 15:38:59 -04:00
|
|
|
Uuid uuid = readUuid();
|
|
|
|
if (uuid.isNull()) {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(1);
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
group = getGroup(uuid);
|
2011-07-09 15:54:01 -04:00
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Name") {
|
2010-08-12 15:38:59 -04:00
|
|
|
group->setName(readString());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Notes") {
|
2010-08-12 15:38:59 -04:00
|
|
|
group->setNotes(readString());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "IconID") {
|
2010-08-12 15:38:59 -04:00
|
|
|
int iconId = readNumber();
|
2010-08-14 06:24:35 -04:00
|
|
|
if (iconId < 0) {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(2);
|
2010-08-14 06:24:35 -04:00
|
|
|
}
|
2010-08-25 13:26:01 -04:00
|
|
|
else {
|
2012-01-01 15:52:54 -05:00
|
|
|
if (iconId >= databaseIcons()->iconCount()) {
|
2010-09-22 18:21:36 -04:00
|
|
|
qWarning("KeePass2XmlReader::parseGroup: icon id \"%d\" not supported", iconId);
|
|
|
|
}
|
2010-08-12 15:38:59 -04:00
|
|
|
group->setIcon(iconId);
|
2010-08-14 06:24:35 -04:00
|
|
|
}
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "CustomIconUUID") {
|
|
|
|
Uuid uuid = readUuid();
|
|
|
|
if (!uuid.isNull()) {
|
|
|
|
group->setIcon(uuid);
|
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Times") {
|
2010-08-12 15:38:59 -04:00
|
|
|
group->setTimeInfo(parseTimes());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "IsExpanded") {
|
2010-08-12 15:38:59 -04:00
|
|
|
group->setExpanded(readBool());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "DefaultAutoTypeSequence") {
|
2010-08-12 15:38:59 -04:00
|
|
|
group->setDefaultAutoTypeSequence(readString());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "EnableAutoType") {
|
2010-08-19 08:03:54 -04:00
|
|
|
QString str = readString();
|
|
|
|
|
|
|
|
if (str.compare("null", Qt::CaseInsensitive) == 0) {
|
2011-07-07 06:45:14 -04:00
|
|
|
group->setAutoTypeEnabled(Group::Inherit);
|
2010-08-19 08:03:54 -04:00
|
|
|
}
|
|
|
|
else if (str.compare("true", Qt::CaseInsensitive) == 0) {
|
2011-07-07 06:45:14 -04:00
|
|
|
group->setAutoTypeEnabled(Group::Enable);
|
2010-08-19 08:03:54 -04:00
|
|
|
}
|
|
|
|
else if (str.compare("false", Qt::CaseInsensitive) == 0) {
|
2011-07-07 06:45:14 -04:00
|
|
|
group->setAutoTypeEnabled(Group::Disable);
|
2010-08-19 08:03:54 -04:00
|
|
|
}
|
|
|
|
else {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(3);
|
2010-08-19 08:03:54 -04:00
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "EnableSearching") {
|
2010-08-19 08:03:54 -04:00
|
|
|
QString str = readString();
|
|
|
|
|
|
|
|
if (str.compare("null", Qt::CaseInsensitive) == 0) {
|
2011-07-07 06:45:14 -04:00
|
|
|
group->setSearchingEnabled(Group::Inherit);
|
2010-08-19 08:03:54 -04:00
|
|
|
}
|
|
|
|
else if (str.compare("true", Qt::CaseInsensitive) == 0) {
|
2011-07-07 06:45:14 -04:00
|
|
|
group->setSearchingEnabled(Group::Enable);
|
2010-08-19 08:03:54 -04:00
|
|
|
}
|
|
|
|
else if (str.compare("false", Qt::CaseInsensitive) == 0) {
|
2011-07-07 06:45:14 -04:00
|
|
|
group->setSearchingEnabled(Group::Disable);
|
2010-08-19 08:03:54 -04:00
|
|
|
}
|
|
|
|
else {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(4);
|
2010-08-19 08:03:54 -04:00
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "LastTopVisibleEntry") {
|
2010-08-13 12:08:06 -04:00
|
|
|
group->setLastTopVisibleEntry(getEntry(readUuid()));
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Group") {
|
2010-08-12 15:38:59 -04:00
|
|
|
Group* newGroup = parseGroup();
|
|
|
|
if (newGroup) {
|
|
|
|
newGroup->setParent(group);
|
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Entry") {
|
2010-08-25 07:52:59 -04:00
|
|
|
Entry* newEntry = parseEntry(false);
|
2010-08-12 15:38:59 -04:00
|
|
|
if (newEntry) {
|
|
|
|
newEntry->setGroup(group);
|
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
2010-08-12 15:38:59 -04:00
|
|
|
|
|
|
|
return group;
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::parseDeletedObjects()
|
2010-08-25 07:52:59 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "DeletedObjects");
|
|
|
|
|
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
|
|
|
if (m_xml.name() == "DeletedObject") {
|
|
|
|
parseDeletedObject();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::parseDeletedObject()
|
2010-08-25 07:52:59 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "DeletedObject");
|
|
|
|
|
|
|
|
DeletedObject delObj;
|
|
|
|
|
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
|
|
|
if (m_xml.name() == "UUID") {
|
|
|
|
Uuid uuid = readUuid();
|
|
|
|
if (uuid.isNull()) {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(5);
|
2010-08-25 07:52:59 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
delObj.uuid = uuid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_xml.name() == "DeletionTime") {
|
|
|
|
delObj.deletionTime = readDateTime();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_db->addDeletedObject(delObj);
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
Entry* KeePass2XmlReader::parseEntry(bool history)
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Entry");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
Entry* entry = 0;
|
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "UUID") {
|
2010-08-12 15:38:59 -04:00
|
|
|
Uuid uuid = readUuid();
|
|
|
|
if (uuid.isNull()) {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(6);
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-25 07:52:59 -04:00
|
|
|
if (history) {
|
|
|
|
entry = new Entry();
|
2012-04-16 20:26:25 -04:00
|
|
|
entry->setUpdateTimeinfo(false);
|
2010-08-25 15:13:50 -04:00
|
|
|
entry->setUuid(uuid);
|
2010-08-25 07:52:59 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
entry = getEntry(uuid);
|
|
|
|
}
|
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "IconID") {
|
2010-08-12 15:38:59 -04:00
|
|
|
int iconId = readNumber();
|
2010-08-14 06:24:35 -04:00
|
|
|
if (iconId < 0) {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(7);
|
2010-08-14 06:24:35 -04:00
|
|
|
}
|
2010-08-25 13:26:01 -04:00
|
|
|
else {
|
2010-08-12 15:38:59 -04:00
|
|
|
entry->setIcon(iconId);
|
2010-08-14 06:24:35 -04:00
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "CustomIconUUID") {
|
2010-08-12 15:38:59 -04:00
|
|
|
Uuid uuid = readUuid();
|
2010-09-19 15:22:24 -04:00
|
|
|
if (!uuid.isNull()) {
|
2010-08-12 15:38:59 -04:00
|
|
|
entry->setIcon(uuid);
|
2010-09-19 15:22:24 -04:00
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "ForegroundColor") {
|
2010-08-12 15:38:59 -04:00
|
|
|
entry->setForegroundColor(readColor());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "BackgroundColor") {
|
2010-08-12 15:38:59 -04:00
|
|
|
entry->setBackgroundColor(readColor());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "OverrideURL") {
|
2010-08-12 15:38:59 -04:00
|
|
|
entry->setOverrideUrl(readString());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
2010-09-25 06:41:00 -04:00
|
|
|
else if (m_xml.name() == "Tags") {
|
|
|
|
entry->setTags(readString());
|
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
else if (m_xml.name() == "Times") {
|
2010-08-12 15:38:59 -04:00
|
|
|
entry->setTimeInfo(parseTimes());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "String") {
|
2010-08-12 15:38:59 -04:00
|
|
|
parseEntryString(entry);
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Binary") {
|
2010-08-12 15:38:59 -04:00
|
|
|
parseEntryBinary(entry);
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "AutoType") {
|
2010-08-12 15:38:59 -04:00
|
|
|
parseAutoType(entry);
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "History") {
|
2010-08-25 07:52:59 -04:00
|
|
|
if (history) {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(8);
|
2010-08-25 07:52:59 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
parseEntryHistory(entry);
|
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
2010-08-13 12:08:06 -04:00
|
|
|
|
|
|
|
return entry;
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
|
2012-05-02 11:04:03 -04:00
|
|
|
void KeePass2XmlReader::parseEntryString(Entry* entry)
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "String");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
QString key;
|
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "Key") {
|
2010-08-12 15:38:59 -04:00
|
|
|
key = readString();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Value") {
|
2011-01-13 16:31:17 -05:00
|
|
|
QXmlStreamAttributes attr = m_xml.attributes();
|
|
|
|
QString value = readString();
|
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
bool isProtected = attr.value("Protected") == "True";
|
|
|
|
bool protectInMemory = attr.value("ProtectInMemory") == "True";
|
2011-01-13 16:31:17 -05:00
|
|
|
|
|
|
|
if (isProtected && !value.isEmpty()) {
|
2011-07-06 18:15:52 -04:00
|
|
|
if (m_randomStream) {
|
|
|
|
value = m_randomStream->process(QByteArray::fromBase64(value.toAscii()));
|
2011-01-13 16:31:17 -05:00
|
|
|
}
|
|
|
|
else {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(9);
|
2011-01-13 16:31:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
entry->attributes()->set(key, value, isProtected || protectInMemory);
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-02 11:04:03 -04:00
|
|
|
void KeePass2XmlReader::parseEntryBinary(Entry* entry)
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Binary");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
QString key;
|
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "Key") {
|
2010-08-12 15:38:59 -04:00
|
|
|
key = readString();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Value") {
|
2011-01-13 16:31:17 -05:00
|
|
|
QXmlStreamAttributes attr = m_xml.attributes();
|
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
if (attr.hasAttribute("Ref")) {
|
|
|
|
m_binaryMap.insertMulti(attr.value("Ref").toString(), qMakePair(entry, key));
|
|
|
|
m_xml.skipCurrentElement();
|
2011-01-13 16:31:17 -05:00
|
|
|
}
|
2012-04-21 10:45:46 -04:00
|
|
|
else {
|
|
|
|
// format compatbility
|
|
|
|
QByteArray value = readBinary();
|
|
|
|
bool isProtected = attr.hasAttribute("Protected")
|
|
|
|
&& (attr.value("Protected") == "True");
|
|
|
|
|
|
|
|
if (isProtected && !value.isEmpty()) {
|
|
|
|
m_randomStream->processInPlace(value);
|
|
|
|
}
|
2011-01-13 16:31:17 -05:00
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
entry->attachments()->set(key, value);
|
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::parseAutoType(Entry* entry)
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "AutoType");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "Enabled") {
|
2010-08-12 15:38:59 -04:00
|
|
|
entry->setAutoTypeEnabled(readBool());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "DataTransferObfuscation") {
|
2010-08-12 15:38:59 -04:00
|
|
|
entry->setAutoTypeObfuscation(readNumber());
|
|
|
|
}
|
|
|
|
else if (m_xml.name() == "DefaultSequence") {
|
|
|
|
entry->setDefaultAutoTypeSequence(readString());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Association") {
|
2010-08-12 15:38:59 -04:00
|
|
|
parseAutoTypeAssoc(entry);
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-02 11:04:03 -04:00
|
|
|
void KeePass2XmlReader::parseAutoTypeAssoc(Entry* entry)
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Association");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
AutoTypeAssociation assoc;
|
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "Window") {
|
2010-08-12 15:38:59 -04:00
|
|
|
assoc.window = readString();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "KeystrokeSequence") {
|
2010-08-12 15:38:59 -04:00
|
|
|
assoc.sequence = readString();
|
|
|
|
entry->addAutoTypeAssociation(assoc);
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::parseEntryHistory(Entry* entry)
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "History");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "Entry") {
|
2010-08-25 07:52:59 -04:00
|
|
|
Entry* historyItem = parseEntry(true);
|
|
|
|
entry->addHistoryItem(historyItem);
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
TimeInfo KeePass2XmlReader::parseTimes()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Times");
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
TimeInfo timeInfo;
|
|
|
|
while (!m_xml.error() && m_xml.readNextStartElement()) {
|
2010-08-07 09:10:44 -04:00
|
|
|
if (m_xml.name() == "LastModificationTime") {
|
2010-08-12 15:38:59 -04:00
|
|
|
timeInfo.setLastModificationTime(readDateTime());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "CreationTime") {
|
2010-08-12 15:38:59 -04:00
|
|
|
timeInfo.setCreationTime(readDateTime());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "LastAccessTime") {
|
2010-08-12 15:38:59 -04:00
|
|
|
timeInfo.setLastAccessTime(readDateTime());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "ExpiryTime") {
|
2010-08-12 15:38:59 -04:00
|
|
|
timeInfo.setExpiryTime(readDateTime());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "Expires") {
|
2010-08-12 15:38:59 -04:00
|
|
|
timeInfo.setExpires(readBool());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "UsageCount") {
|
2010-08-12 15:38:59 -04:00
|
|
|
timeInfo.setUsageCount(readNumber());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else if (m_xml.name() == "LocationChanged") {
|
2010-08-12 15:38:59 -04:00
|
|
|
timeInfo.setLocationChanged(readDateTime());
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
skipCurrentElement();
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
2010-08-12 15:38:59 -04:00
|
|
|
|
|
|
|
return timeInfo;
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
QString KeePass2XmlReader::readString()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
return m_xml.readElementText();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
bool KeePass2XmlReader::readBool()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
QString str = readString();
|
|
|
|
|
2010-08-19 08:03:54 -04:00
|
|
|
if (str.compare("True", Qt::CaseInsensitive) == 0) {
|
2010-08-07 09:10:44 -04:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-19 08:03:54 -04:00
|
|
|
else if (str.compare("False", Qt::CaseInsensitive) == 0) {
|
2010-08-07 09:10:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(10);
|
2010-08-13 12:08:06 -04:00
|
|
|
return false;
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
QDateTime KeePass2XmlReader::readDateTime()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
|
|
|
QString str = readString();
|
|
|
|
QDateTime dt = QDateTime::fromString(str, Qt::ISODate);
|
|
|
|
|
|
|
|
if (!dt.isValid()) {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(11);
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
QColor KeePass2XmlReader::readColor()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
2010-08-12 15:38:59 -04:00
|
|
|
QString colorStr = readString();
|
2010-08-13 12:08:06 -04:00
|
|
|
|
|
|
|
if (colorStr.isEmpty()) {
|
|
|
|
return QColor();
|
|
|
|
}
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
if (colorStr.length() != 7 || colorStr[0] != '#') {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(12);
|
2010-08-12 15:38:59 -04:00
|
|
|
return QColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor color;
|
2012-04-18 16:08:22 -04:00
|
|
|
for (int i = 0; i <= 2; i++) {
|
2010-08-12 15:38:59 -04:00
|
|
|
QString rgbPartStr = colorStr.mid(1 + 2*i, 2);
|
|
|
|
bool ok;
|
2010-08-13 12:08:06 -04:00
|
|
|
int rgbPart = rgbPartStr.toInt(&ok, 16);
|
2010-08-12 15:38:59 -04:00
|
|
|
if (!ok || rgbPart > 255) {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(13);
|
2010-08-12 15:38:59 -04:00
|
|
|
return QColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
color.setRed(rgbPart);
|
|
|
|
}
|
|
|
|
else if (i == 1) {
|
|
|
|
color.setGreen(rgbPart);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
color.setBlue(rgbPart);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
int KeePass2XmlReader::readNumber()
|
2010-08-12 15:38:59 -04:00
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
int result = readString().toInt(&ok);
|
|
|
|
if (!ok) {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(14);
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
return result;
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
Uuid KeePass2XmlReader::readUuid()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
2010-08-12 15:38:59 -04:00
|
|
|
QByteArray uuidBin = readBinary();
|
2012-05-14 13:10:42 -04:00
|
|
|
if (uuidBin.length() != Uuid::Length) {
|
2012-01-06 14:03:13 -05:00
|
|
|
raiseError(15);
|
2010-08-12 15:38:59 -04:00
|
|
|
return Uuid();
|
|
|
|
}
|
|
|
|
else {
|
2010-08-13 12:08:06 -04:00
|
|
|
return Uuid(uuidBin);
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
QByteArray KeePass2XmlReader::readBinary()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
2010-08-12 15:38:59 -04:00
|
|
|
return QByteArray::fromBase64(readString().toAscii());
|
|
|
|
}
|
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
QByteArray KeePass2XmlReader::readCompressedBinary()
|
|
|
|
{
|
|
|
|
QByteArray rawData = readBinary();
|
|
|
|
|
|
|
|
QBuffer buffer(&rawData);
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
QtIOCompressor compressor(&buffer);
|
|
|
|
compressor.setStreamFormat(QtIOCompressor::GzipFormat);
|
|
|
|
compressor.open(QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
QByteArray result;
|
2012-05-02 05:06:24 -04:00
|
|
|
if (!Tools::readAllFromDevice(&compressor, result)) {
|
2012-04-21 10:45:46 -04:00
|
|
|
raiseError(16);
|
|
|
|
}
|
2012-05-02 05:06:24 -04:00
|
|
|
return result;
|
2012-04-21 10:45:46 -04:00
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
Group* KeePass2XmlReader::getGroup(const Uuid& uuid)
|
2010-08-12 15:38:59 -04:00
|
|
|
{
|
2010-08-13 12:08:06 -04:00
|
|
|
if (uuid.isNull()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-22 06:29:18 -04:00
|
|
|
if (m_groups.contains(uuid)) {
|
|
|
|
return m_groups.value(uuid);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Group* group = new Group();
|
|
|
|
group->setUpdateTimeinfo(false);
|
|
|
|
group->setUuid(uuid);
|
|
|
|
group->setParent(m_tmpParent);
|
|
|
|
m_groups.insert(uuid, group);
|
|
|
|
return group;
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
Entry* KeePass2XmlReader::getEntry(const Uuid& uuid)
|
2010-08-12 15:38:59 -04:00
|
|
|
{
|
2010-08-13 12:08:06 -04:00
|
|
|
if (uuid.isNull()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-22 06:29:18 -04:00
|
|
|
if (m_entries.contains(uuid)) {
|
|
|
|
return m_entries.value(uuid);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Entry* entry = new Entry();
|
|
|
|
entry->setUpdateTimeinfo(false);
|
|
|
|
entry->setUuid(uuid);
|
|
|
|
entry->setGroup(m_tmpParent);
|
|
|
|
m_entries.insert(uuid, entry);
|
|
|
|
return entry;
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-06 14:03:13 -05:00
|
|
|
void KeePass2XmlReader::raiseError(int internalNumber)
|
2010-08-12 15:38:59 -04:00
|
|
|
{
|
2012-01-06 14:03:13 -05:00
|
|
|
m_xml.raiseError(tr("Invalid database file (error no %1).").arg(QString::number(internalNumber)));
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
2010-08-13 12:08:06 -04:00
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlReader::skipCurrentElement()
|
2010-08-13 12:08:06 -04:00
|
|
|
{
|
2010-09-22 18:21:36 -04:00
|
|
|
qWarning("KeePass2XmlReader::skipCurrentElement: skip element \"%s\"", qPrintable(m_xml.name().toString()));
|
2010-08-13 12:08:06 -04:00
|
|
|
m_xml.skipCurrentElement();
|
|
|
|
}
|