From 52991f3d66eb5e7d7a5098dfe3b9bb7106466f52 Mon Sep 17 00:00:00 2001 From: Louis-Bertrand Varin Date: Mon, 30 Jan 2017 19:18:35 -0500 Subject: [PATCH] Add first keepassxc-cli version. --- src/CMakeLists.txt | 1 + {utils => src/cli}/CMakeLists.txt | 29 +++---- utils/kdbx-extract.cpp => src/cli/Extract.cpp | 9 +-- src/cli/Extract.h | 27 +++++++ utils/kdbx-merge.cpp => src/cli/Merge.cpp | 12 +-- src/cli/Merge.h | 27 +++++++ src/cli/keepassxc-cli.cpp | 77 +++++++++++++++++++ 7 files changed, 150 insertions(+), 32 deletions(-) rename {utils => src/cli}/CMakeLists.txt (58%) rename utils/kdbx-extract.cpp => src/cli/Extract.cpp (92%) create mode 100644 src/cli/Extract.h rename utils/kdbx-merge.cpp => src/cli/Merge.cpp (93%) create mode 100644 src/cli/Merge.h create mode 100644 src/cli/keepassxc-cli.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5e221b916..1d9984460 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -164,6 +164,7 @@ if(WITH_XC_HTTP) endif() add_subdirectory(autotype) +add_subdirectory(cli) set(autotype_SOURCES core/Tools.cpp diff --git a/utils/CMakeLists.txt b/src/cli/CMakeLists.txt similarity index 58% rename from utils/CMakeLists.txt rename to src/cli/CMakeLists.txt index 83f00b4bc..4456cbeaa 100644 --- a/utils/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (C) 2010 Felix Geyer +# 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,19 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -include_directories(../src) +set(cli_SOURCES + Merge.cpp + Merge.h + Extract.cpp + Extract.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}) - -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) diff --git a/utils/kdbx-extract.cpp b/src/cli/Extract.cpp similarity index 92% rename from utils/kdbx-extract.cpp rename to src/cli/Extract.cpp index 255f5d003..4ad658784 100644 --- a/utils/kdbx-extract.cpp +++ b/src/cli/Extract.cpp @@ -17,6 +17,8 @@ #include +#include "Extract.h" + #include #include #include @@ -30,7 +32,7 @@ #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); @@ -38,7 +40,6 @@ int main(int argc, char **argv) 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(); @@ -47,10 +48,6 @@ int main(int argc, char **argv) return 1; } - if (!Crypto::init()) { - qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString())); - } - static QTextStream inputTextStream(stdin, QIODevice::ReadOnly); QString line = inputTextStream.readLine(); CompositeKey key = CompositeKey::readFromLine(line); diff --git a/src/cli/Extract.h b/src/cli/Extract.h new file mode 100644 index 000000000..9a6638e4b --- /dev/null +++ b/src/cli/Extract.h @@ -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 . + */ + +#ifndef KEEPASSXC_EXTRACT_H +#define KEEPASSXC_EXTRACT_H + +class Extract +{ +public: + static int execute(int argc, char** argv); +}; + +#endif // KEEPASSXC_EXTRACT_H diff --git a/utils/kdbx-merge.cpp b/src/cli/Merge.cpp similarity index 93% rename from utils/kdbx-merge.cpp rename to src/cli/Merge.cpp index da780ea1b..bac8f4bd8 100644 --- a/utils/kdbx-merge.cpp +++ b/src/cli/Merge.cpp @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -#include +#include "Merge.h" #include #include @@ -25,12 +25,11 @@ #include #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); @@ -43,7 +42,6 @@ int main(int argc, char **argv) QCommandLineOption samePasswordOption(QStringList() << "s" << "same-password", QCoreApplication::translate("main", "use the same password for both database files.")); - parser.addHelpOption(); parser.addOption(samePasswordOption); parser.process(app); @@ -53,10 +51,6 @@ int main(int argc, char **argv) return 1; } - if (!Crypto::init()) { - qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString())); - } - static QTextStream inputTextStream(stdin, QIODevice::ReadOnly); QString line1 = inputTextStream.readLine(); @@ -133,6 +127,6 @@ int main(int argc, char **argv) } qDebug("Successfully merged the database files.\n"); - return 1; + return 0; } diff --git a/src/cli/Merge.h b/src/cli/Merge.h new file mode 100644 index 000000000..dd9b8a4c0 --- /dev/null +++ b/src/cli/Merge.h @@ -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 . + */ + +#ifndef KEEPASSXC_MERGE_H +#define KEEPASSXC_MERGE_H + +class Merge +{ +public: + static int execute(int argc, char** argv); +}; + +#endif // KEEPASSXC_MERGE_H diff --git a/src/cli/keepassxc-cli.cpp b/src/cli/keepassxc-cli.cpp new file mode 100644 index 000000000..086e544a6 --- /dev/null +++ b/src/cli/keepassxc-cli.cpp @@ -0,0 +1,77 @@ +/* + * 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 . + */ + +#include +#include + +#include +#include +#include + +#include "config-keepassx.h" +#include "crypto/Crypto.h" + +int main(int argc, char **argv) +{ + + if (!Crypto::init()) { + qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString())); + return 1; + } + + QCoreApplication app(argc, argv); + app.setApplicationVersion(KEEPASSX_VERSION); + + QCommandLineParser parser; + parser.setApplicationDescription(QCoreApplication::translate("main", "KeepassXC command line interface.")); + parser.addPositionalArgument("command", QCoreApplication::translate("main", "Name of the command to execute.")); + + parser.addHelpOption(); + parser.addVersionOption(); + parser.process(app); + + const QStringList args = parser.positionalArguments(); + if (args.size() < 1) { + parser.showHelp(); + return 1; + } + + QString commandName = args.at(0); + + for (int i = 1; i < argc - 1; ++i) { + argv[i] = argv[i+1]; + } + argv[argc - 1] = nullptr; + argc--; + + if (commandName == "merge") + { + argv[0] = const_cast("keepassxc-cli merge"); + return Merge::execute(argc, argv); + } + + if (commandName == "extract") + { + argv[0] = const_cast("keepassxc-cli extract"); + return Extract::execute(argc, argv); + } + + qCritical("Invalid command %s.", qPrintable(commandName)); + parser.showHelp(); + return 1; + +}