keepassxc/src/cli/Extract.cpp

88 lines
2.7 KiB
C++
Raw Normal View History

/*
* 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/>.
*/
2017-02-02 22:30:54 +00:00
#include <cstdlib>
#include <stdio.h>
2011-07-08 12:51:14 +00:00
2017-01-31 00:18:35 +00:00
#include "Extract.h"
2017-01-14 19:08:10 +00:00
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QFile>
#include <QStringList>
#include <QTextStream>
2012-10-29 13:59:13 +00:00
#include "core/Database.h"
#include "format/KeePass2Reader.h"
#include "keys/CompositeKey.h"
2017-05-19 18:04:11 +00:00
int Extract::execute(int argc, char** argv)
{
QCoreApplication app(argc, argv);
2017-02-16 19:26:51 +00:00
QTextStream out(stdout);
2017-01-14 19:08:10 +00:00
QCommandLineParser parser;
2017-05-19 18:04:11 +00:00
parser.setApplicationDescription(
QCoreApplication::translate("main", "Extract and print the content of a database."));
2017-03-12 17:34:56 +00:00
parser.addPositionalArgument("database", QCoreApplication::translate("main", "Path of the database to extract."));
2017-01-14 19:08:10 +00:00
parser.process(app);
const QStringList args = parser.positionalArguments();
if (args.size() != 1) {
parser.showHelp();
2017-02-02 22:30:54 +00:00
return EXIT_FAILURE;
}
2017-02-16 19:26:51 +00:00
out << "Insert the database password\n> ";
out.flush();
2017-01-14 18:25:30 +00:00
static QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
QString line = inputTextStream.readLine();
CompositeKey key = CompositeKey::readFromLine(line);
2017-01-14 19:08:10 +00:00
QString databaseFilename = args.at(0);
QFile dbFile(databaseFilename);
if (!dbFile.exists()) {
2017-01-14 19:08:10 +00:00
qCritical("File %s does not exist.", qPrintable(databaseFilename));
2017-02-02 22:30:54 +00:00
return EXIT_FAILURE;
}
if (!dbFile.open(QIODevice::ReadOnly)) {
2017-01-14 19:08:10 +00:00
qCritical("Unable to open file %s.", qPrintable(databaseFilename));
2017-02-02 22:30:54 +00:00
return EXIT_FAILURE;
}
KeePass2Reader reader;
reader.setSaveXml(true);
2012-10-29 13:59:13 +00:00
Database* db = reader.readDatabase(&dbFile, key);
delete db;
QByteArray xmlData = reader.xmlData();
if (reader.hasError()) {
if (xmlData.isEmpty()) {
qCritical("Error while reading the database:\n%s", qPrintable(reader.errorString()));
2017-05-19 18:04:11 +00:00
} else {
qWarning("Error while parsing the database:\n%s\n", qPrintable(reader.errorString()));
}
2017-02-02 22:30:54 +00:00
return EXIT_FAILURE;
}
out << xmlData.constData() << "\n";
2017-02-02 22:30:54 +00:00
return EXIT_SUCCESS;
}