Welcome to mirror list, hosted at ThFree Co, Russian Federation.

keepassxc-cli.cpp « cli « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18bb224ec31a98eeff6cec5582de75bdae263af7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 *  Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
 *
 *  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 <QTextStream>

#include <cli/Clip.h>
#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;
    }

    QStringList arguments;
    for (int i = 0; i < argc; ++i) {
        arguments << QString(argv[i]);
    }
    QCommandLineParser parser;

    QString description("KeePassXC command line interface.");
    description = description.append(QString("\n\nAvailable commands:"));
    description = description.append(QString("\n  clip\t\tCopy a password to the clipboard."));
    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 the setOptionsAfterPositionalArgumentsMode (Qt 5.6) function
    // when available. Until then, options passed to sub-commands won't be
    // recognized by this parser.
    parser.parse(arguments);

    if (parser.positionalArguments().size() < 1) {
        QCoreApplication app(argc, argv);
        app.setApplicationVersion(KEEPASSX_VERSION);
        if (parser.isSet("version")) {
            // Switch to parser.showVersion() when available (QT 5.4).
            QTextStream out(stdout);
            out << KEEPASSX_VERSION << "\n";
            out.flush();
            return EXIT_SUCCESS;
        }
        parser.showHelp();
    }

    QString commandName = parser.positionalArguments().at(0);

    int exitCode = EXIT_FAILURE;

    if (commandName == "clip") {
        // Removing the first cli argument before dispatching.
        ++argv;
        --argc;
        argv[0] = const_cast<char*>("keepassxc-cli clip");
        exitCode = Clip::execute(argc, argv);
    } else if (commandName == "entropy-meter") {
        ++argv;
        --argc;
        argv[0] = const_cast<char*>("keepassxc-cli entropy-meter");
        exitCode = EntropyMeter::execute(argc, argv);
    } else if (commandName == "extract") {
        ++argv;
        --argc;
        argv[0] = const_cast<char*>("keepassxc-cli extract");
        exitCode = Extract::execute(argc, argv);
    } else if (commandName == "list") {
        ++argv;
        --argc;
        argv[0] = const_cast<char*>("keepassxc-cli list");
        exitCode = List::execute(argc, argv);
    } else if (commandName == "merge") {
        ++argv;
        --argc;
        argv[0] = const_cast<char*>("keepassxc-cli merge");
        exitCode = Merge::execute(argc, argv);
    } else if (commandName == "show") {
        ++argv;
        --argc;
        argv[0] = const_cast<char*>("keepassxc-cli show");
        exitCode = Show::execute(argc, argv);
    } else {
        qCritical("Invalid command %s.", qPrintable(commandName));
        QCoreApplication app(argc, argv);
        app.setApplicationVersion(KEEPASSX_VERSION);
        // showHelp exits the application immediately, so we need to set the
        // exit code here.
        parser.showHelp(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;
}