From 23b818f9e668b1d15d84264fb64ef5604a368aa3 Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Wed, 2 Aug 2017 04:57:19 +0800 Subject: [PATCH] Support --key-file in `keepassxc-cli extract` (#824) --- src/cli/Extract.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/cli/Extract.cpp b/src/cli/Extract.cpp index a387686e2..0e5b10a2f 100644 --- a/src/cli/Extract.cpp +++ b/src/cli/Extract.cpp @@ -28,6 +28,7 @@ #include "core/Database.h" #include "format/KeePass2Reader.h" #include "keys/CompositeKey.h" +#include "keys/FileKey.h" #include "keys/PasswordKey.h" Extract::Extract() @@ -43,10 +44,16 @@ Extract::~Extract() int Extract::execute(QStringList arguments) { QTextStream out(stdout); + QTextStream errorTextStream(stderr); QCommandLineParser parser; parser.setApplicationDescription(this->description); parser.addPositionalArgument("database", QObject::tr("Path of the database to extract.")); + QCommandLineOption keyFile(QStringList() << "k" + << "key-file", + QObject::tr("Key file of the database."), + QObject::tr("path")); + parser.addOption(keyFile); parser.process(arguments); const QStringList args = parser.positionalArguments(); @@ -65,6 +72,20 @@ int Extract::execute(QStringList arguments) passwordKey.setPassword(line); compositeKey.addKey(passwordKey); + QString keyFilePath = parser.value(keyFile); + if (!keyFilePath.isEmpty()) { + FileKey fileKey; + QString errorMsg; + if (!fileKey.load(keyFilePath, &errorMsg)) { + errorTextStream << QObject::tr("Failed to load key file %s: %s").arg(keyFilePath).arg(errorMsg); + errorTextStream << endl; + return EXIT_FAILURE; + } + + compositeKey.addKey(fileKey); + } + + QString databaseFilename = args.at(0); QFile dbFile(databaseFilename); if (!dbFile.exists()) {