Merge pull request #254 from keepassxreboot/feature/keepassxc-cli

KeePassXC CLI
This commit is contained in:
TheZ3ro 2017-03-16 19:01:23 +01:00 committed by GitHub
commit 4cccf28b4d
16 changed files with 508 additions and 92 deletions

View File

@ -166,16 +166,19 @@ if(APPLE AND "${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr/local")
endif()
if(MINGW)
set(CLI_INSTALL_DIR ".")
set(BIN_INSTALL_DIR ".")
set(PLUGIN_INSTALL_DIR ".")
set(DATA_INSTALL_DIR "share")
elseif(APPLE)
set(CLI_INSTALL_DIR "/usr/local/bin")
set(BIN_INSTALL_DIR ".")
set(PLUGIN_INSTALL_DIR "${PROGNAME}.app/Contents/PlugIns")
set(DATA_INSTALL_DIR "${PROGNAME}.app/Contents/Resources")
else()
include(GNUInstallDirs)
set(CLI_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}")
set(BIN_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}")
set(PLUGIN_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/keepassxc")
set(DATA_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/keepassxc")
@ -254,7 +257,6 @@ include(FeatureSummary)
add_subdirectory(src)
add_subdirectory(share)
add_subdirectory(utils)
if(WITH_TESTS)
add_subdirectory(tests)
endif(WITH_TESTS)

View File

@ -164,6 +164,7 @@ if(WITH_XC_HTTP)
endif()
add_subdirectory(autotype)
add_subdirectory(cli)
set(autotype_SOURCES
core/Tools.cpp

View File

@ -1,4 +1,4 @@
# Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
# Copyright (C) 2017 KeePassXC Team
#
# 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
@ -13,24 +13,31 @@
# 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_directories(../src)
set(cli_SOURCES
EntropyMeter.cpp
EntropyMeter.h
Extract.cpp
Extract.h
List.cpp
List.h
Merge.cpp
Merge.h
Show.cpp
Show.h)
add_executable(kdbx-extract kdbx-extract.cpp)
target_link_libraries(kdbx-extract
add_library(cli STATIC ${cli_SOURCES})
target_link_libraries(cli Qt5::Core Qt5::Widgets)
add_executable(keepassxc-cli keepassxc-cli.cpp)
target_link_libraries(keepassxc-cli
cli
keepassx_core
Qt5::Core
${GCRYPT_LIBRARIES}
${GPGERROR_LIBRARIES}
${ZLIB_LIBRARIES})
${ZLIB_LIBRARIES}
zxcvbn)
add_executable(kdbx-merge kdbx-merge.cpp)
target_link_libraries(kdbx-merge
keepassx_core
Qt5::Core
${GCRYPT_LIBRARIES}
${GPGERROR_LIBRARIES}
${ZLIB_LIBRARIES})
add_executable(entropy-meter entropy-meter.cpp)
target_link_libraries(entropy-meter zxcvbn)
install(TARGETS keepassxc-cli
BUNDLE DESTINATION . COMPONENT Runtime
RUNTIME DESTINATION ${CLI_INSTALL_DIR} COMPONENT Runtime)

View File

@ -6,6 +6,8 @@ Copyright (c) 2016, KeePassXC Team
See zxcvbn/zxcvbn.cpp for complete COPYRIGHT Notice
*/
#include "EntropyMeter.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@ -76,7 +78,7 @@ static void calculate(const char *pwd, int advanced)
}
}
int main(int argc, char **argv)
int EntropyMeter::execute(int argc, char **argv)
{
printf("KeePassXC Entropy Meter, based on zxcvbn-c.\nEnter your password below or pass it as argv\n");
printf(" Usage: entropy-meter [-a] [pwd1 pwd2 ...]\n> ");

27
src/cli/EntropyMeter.h Normal file
View File

@ -0,0 +1,27 @@
/*
* Copyright (C) 2017 KeePassXC Team
*
* 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/>.
*/
#ifndef KEEPASSXC_ENTROPYMETER_H
#define KEEPASSXC_ENTROPYMETER_H
class EntropyMeter
{
public:
static int execute(int argc, char** argv);
};
#endif // KEEPASSXC_ENTROPYMETER_H

View File

@ -15,8 +15,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "Extract.h"
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QFile>
@ -24,32 +27,28 @@
#include <QTextStream>
#include "core/Database.h"
#include "crypto/Crypto.h"
#include "format/KeePass2Reader.h"
#include "keys/CompositeKey.h"
#include "keys/FileKey.h"
#include "keys/PasswordKey.h"
int main(int argc, char **argv)
int Extract::execute(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QTextStream out(stdout);
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();
"Extract and print the content of a database."));
parser.addPositionalArgument("database", QCoreApplication::translate("main", "Path of the database to extract."));
parser.process(app);
const QStringList args = parser.positionalArguments();
if (args.size() != 1) {
parser.showHelp();
return 1;
return EXIT_FAILURE;
}
if (!Crypto::init()) {
qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString()));
}
out << "Insert the database password\n> ";
out.flush();
static QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
QString line = inputTextStream.readLine();
@ -59,11 +58,11 @@ int main(int argc, char **argv)
QFile dbFile(databaseFilename);
if (!dbFile.exists()) {
qCritical("File %s does not exist.", qPrintable(databaseFilename));
return 1;
return EXIT_FAILURE;
}
if (!dbFile.open(QIODevice::ReadOnly)) {
qCritical("Unable to open file %s.", qPrintable(databaseFilename));
return 1;
return EXIT_FAILURE;
}
KeePass2Reader reader;
@ -76,15 +75,14 @@ int main(int argc, char **argv)
if (reader.hasError()) {
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()));
}
return EXIT_FAILURE;
}
QTextStream out(stdout);
out << xmlData.constData() << "\n";
return 0;
return EXIT_SUCCESS;
}

27
src/cli/Extract.h Normal file
View File

@ -0,0 +1,27 @@
/*
* Copyright (C) 2017 KeePassXC Team
*
* 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/>.
*/
#ifndef KEEPASSXC_EXTRACT_H
#define KEEPASSXC_EXTRACT_H
class Extract
{
public:
static int execute(int argc, char** argv);
};
#endif // KEEPASSXC_EXTRACT_H

89
src/cli/List.cpp Normal file
View File

@ -0,0 +1,89 @@
/*
* Copyright (C) 2017 KeePassXC Team
*
* 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 <cstdlib>
#include <stdio.h>
#include "List.h"
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QStringList>
#include <QTextStream>
#include "core/Database.h"
#include "core/Entry.h"
#include "core/Group.h"
#include "keys/CompositeKey.h"
void printGroup(Group* group, QString baseName, int depth) {
QTextStream out(stdout);
QString groupName = baseName + group->name() + "/";
QString indentation = QString(" ").repeated(depth);
out << indentation << groupName << " " << group->uuid().toHex() << "\n";
out.flush();
if (group->entries().isEmpty() && group->children().isEmpty()) {
out << indentation << " [empty]\n";
return;
}
for (Entry* entry : group->entries()) {
out << indentation << " " << entry->title() << " " << entry->uuid().toHex() << "\n";
}
for (Group* innerGroup : group->children()) {
printGroup(innerGroup, groupName, depth + 1);
}
}
int List::execute(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QTextStream out(stdout);
QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main",
"List database entries."));
parser.addPositionalArgument("database", QCoreApplication::translate("main", "Path of the database."));
parser.process(app);
const QStringList args = parser.positionalArguments();
if (args.size() != 1) {
parser.showHelp();
return EXIT_FAILURE;
}
out << "Insert the database password\n> ";
out.flush();
static QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
QString line = inputTextStream.readLine();
CompositeKey key = CompositeKey::readFromLine(line);
Database* db = Database::openDatabaseFile(args.at(0), key);
if (db == nullptr) {
return EXIT_FAILURE;
}
printGroup(db->rootGroup(), QString(""), 0);
return EXIT_SUCCESS;
}

27
src/cli/List.h Normal file
View File

@ -0,0 +1,27 @@
/*
* Copyright (C) 2017 KeePassXC Team
*
* 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/>.
*/
#ifndef KEEPASSXC_LIST_H
#define KEEPASSXC_LIST_H
class List
{
public:
static int execute(int argc, char** argv);
};
#endif // KEEPASSXC_LIST_H

View File

@ -15,50 +15,47 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <cstdlib>
#include "Merge.h"
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QFile>
#include <QSaveFile>
#include <QStringList>
#include <QTextStream>
#include "core/Database.h"
#include "crypto/Crypto.h"
#include "format/KeePass2Reader.h"
#include "format/KeePass2Writer.h"
#include "keys/CompositeKey.h"
int main(int argc, char **argv)
int Merge::execute(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QTextStream out(stdout);
QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main", "Merge 2 KeePassXC database files."));
parser.addPositionalArgument("database1", QCoreApplication::translate("main", "path of the database to merge into."));
parser.addPositionalArgument("database2", QCoreApplication::translate("main", "path of the database to merge from."));
parser.setApplicationDescription(QCoreApplication::translate("main", "Merge two databases."));
parser.addPositionalArgument("database1", QCoreApplication::translate("main", "Path of the database to merge into."));
parser.addPositionalArgument("database2", QCoreApplication::translate("main", "Path of the database to merge from."));
QCommandLineOption samePasswordOption(QStringList() << "s" << "same-password",
QCoreApplication::translate("main", "use the same password for both database files."));
QCoreApplication::translate("main", "Use the same password for both database files."));
parser.addHelpOption();
parser.addOption(samePasswordOption);
parser.process(app);
const QStringList args = parser.positionalArguments();
if (args.size() != 2) {
parser.showHelp();
return 1;
return EXIT_FAILURE;
}
if (!Crypto::init()) {
qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString()));
}
out << "Insert the first database password\n> ";
out.flush();
static QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
QString line1 = inputTextStream.readLine();
CompositeKey key1 = CompositeKey::readFromLine(line1);
@ -67,56 +64,29 @@ int main(int argc, char **argv)
key2 = *key1.clone();
}
else {
out << "Insert the second database password\n> ";
out.flush();
QString line2 = inputTextStream.readLine();
key2 = CompositeKey::readFromLine(line2);
}
QString databaseFilename1 = args.at(0);
QFile dbFile1(databaseFilename1);
if (!dbFile1.exists()) {
qCritical("File %s does not exist.", qPrintable(databaseFilename1));
return 1;
}
if (!dbFile1.open(QIODevice::ReadOnly)) {
qCritical("Unable to open file %s.", qPrintable(databaseFilename1));
return 1;
Database* db1 = Database::openDatabaseFile(args.at(0), key1);
if (db1 == nullptr) {
return EXIT_FAILURE;
}
KeePass2Reader reader1;
Database* db1 = reader1.readDatabase(&dbFile1, key1);
if (reader1.hasError()) {
qCritical("Error while parsing the database:\n%s\n", qPrintable(reader1.errorString()));
return 1;
}
QString databaseFilename2 = args.at(1);
QFile dbFile2(databaseFilename2);
if (!dbFile2.exists()) {
qCritical("File %s does not exist.", qPrintable(databaseFilename2));
return 1;
}
if (!dbFile2.open(QIODevice::ReadOnly)) {
qCritical("Unable to open file %s.", qPrintable(databaseFilename2));
return 1;
}
KeePass2Reader reader2;
Database* db2 = reader2.readDatabase(&dbFile2, key2);
if (reader2.hasError()) {
qCritical("Error while parsing the database:\n%s\n", qPrintable(reader2.errorString()));
return 1;
Database* db2 = Database::openDatabaseFile(args.at(1), key2);
if (db2 == nullptr) {
return EXIT_FAILURE;
}
db1->merge(db2);
QSaveFile saveFile(databaseFilename1);
QSaveFile saveFile(args.at(0));
if (!saveFile.open(QIODevice::WriteOnly)) {
qCritical("Unable to open file %s for writing.", qPrintable(databaseFilename1));
return 1;
qCritical("Unable to open file %s for writing.", qPrintable(args.at(0)));
return EXIT_FAILURE;
}
KeePass2Writer writer;
@ -124,15 +94,15 @@ int main(int argc, char **argv)
if (writer.hasError()) {
qCritical("Error while updating the database:\n%s\n", qPrintable(writer.errorString()));
return 1;
return EXIT_FAILURE;
}
if (!saveFile.commit()) {
qCritical("Error while updating the database:\n%s\n", qPrintable(writer.errorString()));
return 0;
return EXIT_FAILURE;
}
qDebug("Successfully merged the database files.\n");
return 1;
out << "Successfully merged the database files.\n";
return EXIT_SUCCESS;
}

27
src/cli/Merge.h Normal file
View File

@ -0,0 +1,27 @@
/*
* Copyright (C) 2017 KeePassXC Team
*
* 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/>.
*/
#ifndef KEEPASSXC_MERGE_H
#define KEEPASSXC_MERGE_H
class Merge
{
public:
static int execute(int argc, char** argv);
};
#endif // KEEPASSXC_MERGE_H

72
src/cli/Show.cpp Normal file
View File

@ -0,0 +1,72 @@
/*
* Copyright (C) 2017 KeePassXC Team
*
* 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 <cstdlib>
#include <stdio.h>
#include "Show.h"
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QStringList>
#include <QTextStream>
#include "core/Database.h"
#include "core/Entry.h"
#include "core/Group.h"
#include "keys/CompositeKey.h"
int Show::execute(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QTextStream out(stdout);
QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main",
"Show a password."));
parser.addPositionalArgument("database", QCoreApplication::translate("main", "Path of the database."));
parser.addPositionalArgument("uuid", QCoreApplication::translate("main", "Uuid of the entry to show"));
parser.process(app);
const QStringList args = parser.positionalArguments();
if (args.size() != 2) {
parser.showHelp();
return EXIT_FAILURE;
}
out << "Insert the database password\n> ";
out.flush();
static QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
QString line = inputTextStream.readLine();
CompositeKey key = CompositeKey::readFromLine(line);
Database* db = Database::openDatabaseFile(args.at(0), key);
if (db == nullptr) {
return EXIT_FAILURE;
}
Uuid uuid = Uuid::fromHex(args.at(1));
Entry* entry = db->resolveEntry(uuid);
if (entry == nullptr) {
qCritical("No entry found with uuid %s", qPrintable(uuid.toHex()));
return EXIT_FAILURE;
}
out << entry->password() << "\n";
return EXIT_SUCCESS;
}

27
src/cli/Show.h Normal file
View File

@ -0,0 +1,27 @@
/*
* Copyright (C) 2017 KeePassXC Team
*
* 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/>.
*/
#ifndef KEEPASSXC_SHOW_H
#define KEEPASSXC_SHOW_H
class Show
{
public:
static int execute(int argc, char** argv);
};
#endif // KEEPASSXC_SHOW_H

114
src/cli/keepassxc-cli.cpp Normal file
View File

@ -0,0 +1,114 @@
/*
* Copyright (C) 2017 KeePassXC Team
*
* 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 <cstdlib>
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QStringList>
#include <cli/EntropyMeter.h>
#include <cli/Extract.h>
#include <cli/List.h>
#include <cli/Merge.h>
#include <cli/Show.h>
#include "config-keepassx.h"
#include "core/Tools.h"
#include "crypto/Crypto.h"
#if defined(WITH_ASAN) && defined(WITH_LSAN)
#include <sanitizer/lsan_interface.h>
#endif
int main(int argc, char **argv)
{
#ifdef QT_NO_DEBUG
Tools::disableCoreDumps();
#endif
if (!Crypto::init()) {
qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString()));
return EXIT_FAILURE;
}
QCoreApplication app(argc, argv);
app.setApplicationVersion(KEEPASSX_VERSION);
QCommandLineParser parser;
QString description("KeePassXC command line interface.");
description = description.append(QString("\n\nAvailable commands:"));
description = description.append(QString("\n extract\tExtract and print the content of a database."));
description = description.append(QString("\n entropy-meter\tCalculate password entropy."));
description = description.append(QString("\n list\t\tList database entries."));
description = description.append(QString("\n merge\t\tMerge two databases."));
description = description.append(QString("\n show\t\tShow a password."));
parser.setApplicationDescription(QCoreApplication::translate("main", qPrintable(description)));
parser.addPositionalArgument("command", QCoreApplication::translate("main", "Name of the command to execute."));
parser.addHelpOption();
parser.addVersionOption();
// TODO : use process once the setOptionsAfterPositionalArgumentsMode (Qt 5.6)
// is available. Until then, options passed to sub-commands won't be
// recognized by this parser.
// parser.process(app);
if (argc < 2) {
parser.showHelp();
return EXIT_FAILURE;
}
QString commandName = argv[1];
// Removing the first cli argument before dispatching.
++argv;
--argc;
int exitCode = EXIT_FAILURE;
if (commandName == "entropy-meter") {
argv[0] = const_cast<char*>("keepassxc-cli entropy-meter");
exitCode = EntropyMeter::execute(argc, argv);
} else if (commandName == "extract") {
argv[0] = const_cast<char*>("keepassxc-cli extract");
exitCode = Extract::execute(argc, argv);
} else if (commandName == "list") {
argv[0] = const_cast<char*>("keepassxc-cli list");
exitCode = List::execute(argc, argv);
} else if (commandName == "merge") {
argv[0] = const_cast<char*>("keepassxc-cli merge");
exitCode = Merge::execute(argc, argv);
} else if (commandName == "show") {
argv[0] = const_cast<char*>("keepassxc-cli show");
exitCode = Show::execute(argc, argv);
} else {
qCritical("Invalid command %s.", qPrintable(commandName));
parser.showHelp();
exitCode = EXIT_FAILURE;
}
#if defined(WITH_ASAN) && defined(WITH_LSAN)
// do leak check here to prevent massive tail of end-of-process leak errors from third-party libraries
__lsan_do_leak_check();
__lsan_disable();
#endif
return exitCode;
}

View File

@ -25,6 +25,7 @@
#include "core/Metadata.h"
#include "crypto/Random.h"
#include "format/KeePass2.h"
#include "format/KeePass2Reader.h"
QHash<Uuid, Database*> Database::m_uuidMap;
@ -355,3 +356,27 @@ const CompositeKey & Database::key() const
return m_data.key;
}
Database* Database::openDatabaseFile(QString fileName, CompositeKey key)
{
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;
}

View File

@ -118,6 +118,7 @@ public:
Uuid uuid();
static Database* databaseByUuid(const Uuid& uuid);
static Database* openDatabaseFile(QString fileName, CompositeKey key);
signals:
void groupDataChanged(Group* group);