2
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 2 or (at your option)
7
* version 3 of the License.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include <QCommandLineParser>
23
#include "TextStream.h"
25
#include "config-keepassx.h"
26
#include "core/Bootstrap.h"
27
#include "core/Config.h"
28
#include "core/Metadata.h"
29
#include "core/Tools.h"
30
#include "crypto/Crypto.h"
32
#if defined(WITH_ASAN) && defined(WITH_LSAN)
33
#include <sanitizer/lsan_interface.h>
36
#if defined(USE_READLINE)
37
#include <readline/history.h>
38
#include <readline/readline.h>
44
virtual ~LineReader() = default;
45
virtual QString readLine(QString prompt) = 0;
46
virtual bool isFinished() = 0;
49
class SimpleLineReader : public LineReader
53
: inStream(stdin, QIODevice::ReadOnly)
54
, outStream(stdout, QIODevice::WriteOnly)
59
QString readLine(QString prompt) override
63
QString result = inStream.readLine();
64
if (result.isNull()) {
70
bool isFinished() override
81
#if defined(USE_READLINE)
82
class ReadlineLineReader : public LineReader
90
QString readLine(QString prompt) override
92
char* result = readline(prompt.toStdString().c_str());
103
bool isFinished() override
113
int enterInteractiveMode(const QStringList& arguments)
115
auto& err = Utils::STDERR;
116
// Replace command list with interactive version
117
Commands::setupCommands(true);
120
QStringList openArgs(arguments);
121
openArgs.removeFirst();
122
if (openCmd.execute(openArgs) != EXIT_SUCCESS) {
126
QScopedPointer<LineReader> reader;
127
#if defined(USE_READLINE)
128
reader.reset(new ReadlineLineReader());
130
reader.reset(new SimpleLineReader());
133
QSharedPointer<Database> currentDatabase(openCmd.currentDatabase);
138
if (currentDatabase) {
139
prompt += currentDatabase->metadata()->name();
140
if (prompt.isEmpty()) {
141
prompt += QFileInfo(currentDatabase->filePath()).fileName();
145
command = reader->readLine(prompt);
146
if (reader->isFinished()) {
150
QStringList args = Utils::splitCommandString(command);
155
auto cmd = Commands::getCommand(args[0]);
157
err << QObject::tr("Unknown command %1").arg(args[0]) << endl;
159
} else if (cmd->name == "quit" || cmd->name == "exit") {
163
cmd->currentDatabase.swap(currentDatabase);
165
currentDatabase.swap(cmd->currentDatabase);
168
if (currentDatabase) {
169
currentDatabase->releaseData();
175
int main(int argc, char** argv)
177
if (!Crypto::init()) {
178
qWarning("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString()));
182
QCoreApplication app(argc, argv);
183
QCoreApplication::setApplicationVersion(KEEPASSXC_VERSION);
185
Bootstrap::bootstrap(config()->get(Config::GUI_Language).toString());
186
Utils::setDefaultTextStreams();
187
Commands::setupCommands(false);
189
auto& out = Utils::STDOUT;
190
auto& err = Utils::STDERR;
192
QStringList arguments;
193
for (int i = 0; i < argc; ++i) {
194
arguments << QString(argv[i]);
196
QCommandLineParser parser;
198
QString description("KeePassXC command line interface.");
199
description = description.append(QObject::tr("\n\nAvailable commands:\n"));
200
for (auto& command : Commands::getCommands()) {
201
description = description.append(command->getDescriptionLine());
203
parser.setApplicationDescription(description);
205
parser.addPositionalArgument("command", QObject::tr("Name of the command to execute."));
207
QCommandLineOption debugInfoOption(QStringList() << "debug-info", QObject::tr("Displays debugging information."));
208
parser.addOption(debugInfoOption);
209
parser.addHelpOption();
210
parser.addVersionOption();
211
// TODO : use the setOptionsAfterPositionalArgumentsMode (Qt 5.6) function
212
// when available. Until then, options passed to sub-commands won't be
213
// recognized by this parser.
214
parser.parse(arguments);
216
if (parser.positionalArguments().empty()) {
217
if (parser.isSet("version")) {
218
// Switch to parser.showVersion() when available (QT 5.4).
219
out << KEEPASSXC_VERSION << endl;
221
} else if (parser.isSet(debugInfoOption)) {
222
QString debugInfo = Tools::debugInfo().append("\n").append(Crypto::debugInfo());
223
out << debugInfo << endl;
226
// showHelp exits the application immediately.
230
QString commandName = parser.positionalArguments().at(0);
231
if (commandName == "open") {
232
return enterInteractiveMode(arguments);
235
auto command = Commands::getCommand(commandName);
237
err << QObject::tr("Invalid command %1.").arg(commandName) << endl;
238
err << parser.helpText();
242
// Removing the first argument (keepassxc).
243
arguments.removeFirst();
244
int exitCode = command->execute(arguments);
246
if (command->currentDatabase) {
247
command->currentDatabase.reset();
250
#if defined(WITH_ASAN) && defined(WITH_LSAN)
251
// do leak check here to prevent massive tail of end-of-process leak errors from third-party libraries
252
__lsan_do_leak_check();