2011-07-06 14:23:29 -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 <stdio.h>
|
2011-07-08 08:51:14 -04:00
|
|
|
|
2017-01-14 14:08:10 -05:00
|
|
|
#include <QCommandLineParser>
|
2013-10-03 09:18:16 -04:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QTextStream>
|
2011-07-06 14:23:29 -04:00
|
|
|
|
2012-10-29 09:59:13 -04:00
|
|
|
#include "core/Database.h"
|
2011-07-06 14:23:29 -04:00
|
|
|
#include "crypto/Crypto.h"
|
|
|
|
#include "format/KeePass2Reader.h"
|
|
|
|
#include "keys/CompositeKey.h"
|
2012-01-06 14:05:46 -05:00
|
|
|
#include "keys/FileKey.h"
|
2011-07-06 14:23:29 -04:00
|
|
|
#include "keys/PasswordKey.h"
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
|
2017-01-14 14:08:10 -05:00
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.setApplicationDescription(QCoreApplication::translate("main",
|
|
|
|
"Extract and print a KeePassXC database file."));
|
|
|
|
parser.addPositionalArgument("database", QCoreApplication::translate("main", "path of the database to extract."));
|
|
|
|
parser.addHelpOption();
|
|
|
|
parser.process(app);
|
|
|
|
|
|
|
|
const QStringList args = parser.positionalArguments();
|
|
|
|
if (args.size() != 1) {
|
|
|
|
parser.showHelp();
|
2011-07-06 14:23:29 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-15 05:17:40 -04:00
|
|
|
if (!Crypto::init()) {
|
|
|
|
qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString()));
|
|
|
|
}
|
2011-07-06 14:23:29 -04:00
|
|
|
|
2017-01-14 13:25:30 -05:00
|
|
|
static QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
|
|
|
|
QString line = inputTextStream.readLine();
|
|
|
|
CompositeKey key = CompositeKey::readFromLine(line);
|
2011-07-06 14:23:29 -04:00
|
|
|
|
2017-01-14 14:08:10 -05:00
|
|
|
QString databaseFilename = args.at(0);
|
|
|
|
QFile dbFile(databaseFilename);
|
2011-07-06 14:23:29 -04:00
|
|
|
if (!dbFile.exists()) {
|
2017-01-14 14:08:10 -05:00
|
|
|
qCritical("File %s does not exist.", qPrintable(databaseFilename));
|
2011-07-06 14:23:29 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!dbFile.open(QIODevice::ReadOnly)) {
|
2017-01-14 14:08:10 -05:00
|
|
|
qCritical("Unable to open file %s.", qPrintable(databaseFilename));
|
2011-07-06 14:23:29 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeePass2Reader reader;
|
|
|
|
reader.setSaveXml(true);
|
2012-10-29 09:59:13 -04:00
|
|
|
Database* db = reader.readDatabase(&dbFile, key);
|
|
|
|
delete db;
|
2011-07-06 14:23:29 -04:00
|
|
|
|
2012-10-29 09:57:43 -04:00
|
|
|
QByteArray xmlData = reader.xmlData();
|
|
|
|
|
2012-01-06 14:03:13 -05:00
|
|
|
if (reader.hasError()) {
|
2012-10-29 09:57:43 -04:00
|
|
|
if (xmlData.isEmpty()) {
|
|
|
|
qCritical("Error while reading the database:\n%s", qPrintable(reader.errorString()));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qWarning("Error while parsing the database:\n%s\n", qPrintable(reader.errorString()));
|
|
|
|
}
|
2011-07-06 14:23:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QTextStream out(stdout);
|
2012-10-29 09:57:43 -04:00
|
|
|
out << xmlData.constData() << "\n";
|
2011-07-06 14:23:29 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|