From cb3c4893dce571c9cbfb7f32ee4580f24bb05108 Mon Sep 17 00:00:00 2001 From: louib Date: Tue, 11 Dec 2018 10:49:51 -0500 Subject: [PATCH] Move unlockDatabase to CLI/Utils (#2539) Move unlockDatabase from Database to to cli/Utils --- src/cli/Add.cpp | 8 ++++---- src/cli/Clip.cpp | 8 ++++---- src/cli/Edit.cpp | 8 ++++---- src/cli/List.cpp | 8 ++++---- src/cli/Locate.cpp | 8 ++++---- src/cli/Merge.cpp | 10 +++++----- src/cli/Remove.cpp | 8 ++++---- src/cli/Show.cpp | 8 ++++---- src/cli/Utils.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/cli/Utils.h | 8 ++++++++ src/core/Database.cpp | 41 ----------------------------------------- src/core/Database.h | 2 -- tests/TestCli.cpp | 2 +- 13 files changed, 83 insertions(+), 77 deletions(-) diff --git a/src/cli/Add.cpp b/src/cli/Add.cpp index 9c78db2fb..3eb3b1bfc 100644 --- a/src/cli/Add.cpp +++ b/src/cli/Add.cpp @@ -90,10 +90,10 @@ int Add::execute(const QStringList& arguments) const QString& databasePath = args.at(0); const QString& entryPath = args.at(1); - auto db = Database::unlockFromStdin(databasePath, - parser.value(Command::KeyFileOption), - parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, - Utils::STDERR); + auto db = Utils::unlockDatabase(databasePath, + parser.value(Command::KeyFileOption), + parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, + Utils::STDERR); if (!db) { return EXIT_FAILURE; } diff --git a/src/cli/Clip.cpp b/src/cli/Clip.cpp index 7e81bcf70..0cec5b3ce 100644 --- a/src/cli/Clip.cpp +++ b/src/cli/Clip.cpp @@ -66,10 +66,10 @@ int Clip::execute(const QStringList& arguments) return EXIT_FAILURE; } - auto db = Database::unlockFromStdin(args.at(0), - parser.value(Command::KeyFileOption), - parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, - Utils::STDERR); + auto db = Utils::unlockDatabase(args.at(0), + parser.value(Command::KeyFileOption), + parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, + Utils::STDERR); if (!db) { return EXIT_FAILURE; } diff --git a/src/cli/Edit.cpp b/src/cli/Edit.cpp index 81c1e3223..48ea5d739 100644 --- a/src/cli/Edit.cpp +++ b/src/cli/Edit.cpp @@ -95,10 +95,10 @@ int Edit::execute(const QStringList& arguments) const QString& databasePath = args.at(0); const QString& entryPath = args.at(1); - auto db = Database::unlockFromStdin(databasePath, - parser.value(Command::KeyFileOption), - parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, - Utils::STDERR); + auto db = Utils::unlockDatabase(databasePath, + parser.value(Command::KeyFileOption), + parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, + Utils::STDERR); if (!db) { return EXIT_FAILURE; } diff --git a/src/cli/List.cpp b/src/cli/List.cpp index e4b06f2cc..9c4f733ef 100644 --- a/src/cli/List.cpp +++ b/src/cli/List.cpp @@ -64,10 +64,10 @@ int List::execute(const QStringList& arguments) bool recursive = parser.isSet(recursiveOption); - auto db = Database::unlockFromStdin(args.at(0), - parser.value(Command::KeyFileOption), - parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, - Utils::STDERR); + auto db = Utils::unlockDatabase(args.at(0), + parser.value(Command::KeyFileOption), + parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, + Utils::STDERR); if (!db) { return EXIT_FAILURE; } diff --git a/src/cli/Locate.cpp b/src/cli/Locate.cpp index 0ccf5825c..e348df117 100644 --- a/src/cli/Locate.cpp +++ b/src/cli/Locate.cpp @@ -59,10 +59,10 @@ int Locate::execute(const QStringList& arguments) return EXIT_FAILURE; } - auto db = Database::unlockFromStdin(args.at(0), - parser.value(Command::KeyFileOption), - parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, - Utils::STDERR); + auto db = Utils::unlockDatabase(args.at(0), + parser.value(Command::KeyFileOption), + parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, + Utils::STDERR); if (!db) { return EXIT_FAILURE; } diff --git a/src/cli/Merge.cpp b/src/cli/Merge.cpp index 58c7965dc..c4759fe40 100644 --- a/src/cli/Merge.cpp +++ b/src/cli/Merge.cpp @@ -68,17 +68,17 @@ int Merge::execute(const QStringList& arguments) return EXIT_FAILURE; } - auto db1 = Database::unlockFromStdin(args.at(0), - parser.value(Command::KeyFileOption), - parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, - Utils::STDERR); + auto db1 = Utils::unlockDatabase(args.at(0), + parser.value(Command::KeyFileOption), + parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, + Utils::STDERR); if (!db1) { return EXIT_FAILURE; } QSharedPointer db2; if (!parser.isSet("same-credentials")) { - db2 = Database::unlockFromStdin(args.at(1), parser.value(keyFileFromOption), Utils::STDOUT, Utils::STDERR); + db2 = Utils::unlockDatabase(args.at(1), parser.value(keyFileFromOption), Utils::STDOUT, Utils::STDERR); } else { db2 = QSharedPointer::create(); QString errorMessage; diff --git a/src/cli/Remove.cpp b/src/cli/Remove.cpp index 1341fc6d1..cd6275b77 100644 --- a/src/cli/Remove.cpp +++ b/src/cli/Remove.cpp @@ -61,10 +61,10 @@ int Remove::execute(const QStringList& arguments) return EXIT_FAILURE; } - auto db = Database::unlockFromStdin(args.at(0), - parser.value(Command::KeyFileOption), - parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, - Utils::STDERR); + auto db = Utils::unlockDatabase(args.at(0), + parser.value(Command::KeyFileOption), + parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, + Utils::STDERR); if (!db) { return EXIT_FAILURE; } diff --git a/src/cli/Show.cpp b/src/cli/Show.cpp index e900a07b9..d8cf5a4fe 100644 --- a/src/cli/Show.cpp +++ b/src/cli/Show.cpp @@ -71,10 +71,10 @@ int Show::execute(const QStringList& arguments) return EXIT_FAILURE; } - auto db = Database::unlockFromStdin(args.at(0), - parser.value(Command::KeyFileOption), - parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, - Utils::STDERR); + auto db = Utils::unlockDatabase(args.at(0), + parser.value(Command::KeyFileOption), + parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, + Utils::STDERR); if (!db) { return EXIT_FAILURE; } diff --git a/src/cli/Utils.cpp b/src/cli/Utils.cpp index d78c06074..26434669b 100644 --- a/src/cli/Utils.cpp +++ b/src/cli/Utils.cpp @@ -98,6 +98,47 @@ void setStdinEcho(bool enable = true) } } // namespace Test +QSharedPointer unlockDatabase(const QString& databaseFilename, + const QString& keyFilename, + FILE* outputDescriptor, + FILE* errorDescriptor) +{ + auto compositeKey = QSharedPointer::create(); + TextStream out(outputDescriptor); + TextStream err(errorDescriptor); + + out << QObject::tr("Insert password to unlock %1: ").arg(databaseFilename) << flush; + + QString line = Utils::getPassword(outputDescriptor); + auto passwordKey = QSharedPointer::create(); + passwordKey->setPassword(line); + compositeKey->addKey(passwordKey); + + if (!keyFilename.isEmpty()) { + auto fileKey = QSharedPointer::create(); + QString errorMessage; + // LCOV_EXCL_START + if (!fileKey->load(keyFilename, &errorMessage)) { + err << QObject::tr("Failed to load key file %1: %2").arg(keyFilename, errorMessage) << endl; + return {}; + } + + if (fileKey->type() != FileKey::Hashed) { + err << QObject::tr("WARNING: You are using a legacy key file format which may become\n" + "unsupported in the future.\n\n" + "Please consider generating a new key file.") + << endl; + } + // LCOV_EXCL_STOP + + compositeKey->addKey(fileKey); + } + + auto db = QSharedPointer::create(); + db->open(databaseFilename, compositeKey, nullptr, false); + return db; +} + /** * Read a user password from STDIN or return a password previously * set by \link setNextPassword(). diff --git a/src/cli/Utils.h b/src/cli/Utils.h index 28f260381..3d240c2c0 100644 --- a/src/cli/Utils.h +++ b/src/cli/Utils.h @@ -19,6 +19,10 @@ #define KEEPASSXC_UTILS_H #include "cli/TextStream.h" +#include "core/Database.h" +#include "keys/CompositeKey.h" +#include "keys/FileKey.h" +#include "keys/PasswordKey.h" #include namespace Utils @@ -31,6 +35,10 @@ namespace Utils void setStdinEcho(bool enable); QString getPassword(FILE* outputDescriptor = STDOUT); int clipText(const QString& text); + QSharedPointer unlockDatabase(const QString& databaseFilename, + const QString& keyFilename = {}, + FILE* outputDescriptor = STDOUT, + FILE* errorDescriptor = STDERR); namespace Test { diff --git a/src/core/Database.cpp b/src/core/Database.cpp index ff345c84e..69ea7b179 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -18,8 +18,6 @@ #include "Database.h" -#include "cli/Utils.h" -#include "cli/TextStream.h" #include "core/Clock.h" #include "core/Group.h" #include "core/Merger.h" @@ -701,45 +699,6 @@ QSharedPointer Database::key() const return m_data.key; } -QSharedPointer Database::unlockFromStdin(const QString& databaseFilename, const QString& keyFilename, - FILE* outputDescriptor, FILE* errorDescriptor) -{ - auto compositeKey = QSharedPointer::create(); - TextStream out(outputDescriptor); - TextStream err(errorDescriptor); - - out << QObject::tr("Insert password to unlock %1: ").arg(databaseFilename) << flush; - - QString line = Utils::getPassword(outputDescriptor); - auto passwordKey = QSharedPointer::create(); - passwordKey->setPassword(line); - compositeKey->addKey(passwordKey); - - if (!keyFilename.isEmpty()) { - auto fileKey = QSharedPointer::create(); - QString errorMessage; - // LCOV_EXCL_START - if (!fileKey->load(keyFilename, &errorMessage)) { - err << QObject::tr("Failed to load key file %1: %2").arg(keyFilename, errorMessage) << endl; - return {}; - } - - if (fileKey->type() != FileKey::Hashed) { - err << QObject::tr("WARNING: You are using a legacy key file format which may become\n" - "unsupported in the future.\n\n" - "Please consider generating a new key file.") - << endl; - } - // LCOV_EXCL_STOP - - compositeKey->addKey(fileKey); - } - - auto db = QSharedPointer::create(); - db->open(databaseFilename, compositeKey, nullptr, false); - return db; -} - QSharedPointer Database::kdf() const { return m_data.kdf; diff --git a/src/core/Database.h b/src/core/Database.h index 6d660b66d..3a4a3daf6 100644 --- a/src/core/Database.h +++ b/src/core/Database.h @@ -118,8 +118,6 @@ public: static Database* databaseByUuid(const QUuid& uuid); static Database* databaseByFilePath(const QString& filePath); - static QSharedPointer unlockFromStdin(const QString& databaseFilename, const QString& keyFilename = {}, - FILE* outputDescriptor = stdout, FILE* errorDescriptor = stderr); public slots: void markAsModified(); diff --git a/tests/TestCli.cpp b/tests/TestCli.cpp index 435c2f6e2..430a3ba32 100644 --- a/tests/TestCli.cpp +++ b/tests/TestCli.cpp @@ -130,7 +130,7 @@ void TestCli::cleanupTestCase() QSharedPointer TestCli::readTestDatabase() const { Utils::Test::setNextPassword("a"); - auto db = QSharedPointer(Database::unlockFromStdin(m_dbFile->fileName(), "", m_stdoutHandle)); + auto db = QSharedPointer(Utils::unlockDatabase(m_dbFile->fileName(), "", m_stdoutHandle)); m_stdoutFile->seek(ftell(m_stdoutHandle)); // re-synchronize handles return db; }