keepassxc

Форк
0
/
main.cpp 
229 строк · 8.7 Кб
1
/*
2
 *  Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
3
 *  Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation, either version 2 or (at your option)
8
 *  version 3 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
#include <QCommandLineParser>
20
#include <QDir>
21
#include <QFile>
22
#include <QWindow>
23

24
#include "cli/Utils.h"
25
#include "config-keepassx.h"
26
#include "core/Tools.h"
27
#include "crypto/Crypto.h"
28
#include "gui/Application.h"
29
#include "gui/MainWindow.h"
30
#include "gui/MessageBox.h"
31
#include "gui/osutils/OSUtils.h"
32

33
#if defined(WITH_ASAN) && defined(WITH_LSAN)
34
#include <sanitizer/lsan_interface.h>
35
#endif
36

37
#ifdef QT_STATIC
38
#include <QtPlugin>
39

40
#if defined(Q_OS_WIN)
41
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
42
#elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
43
Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
44
#endif
45
#endif
46

47
#ifdef Q_OS_WIN
48
#include <windows.h>
49
#endif
50

51
int main(int argc, char** argv)
52
{
53
    QT_REQUIRE_VERSION(argc, argv, QT_VERSION_STR)
54

55
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
56
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
57
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
58
#endif
59
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) && defined(Q_OS_WIN)
60
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
61
#endif
62
    Application app(argc, argv);
63
    // don't set organizationName as that changes the return value of
64
    // QStandardPaths::writableLocation(QDesktopServices::DataLocation)
65
    Application::setApplicationName("KeePassXC");
66
    Application::setApplicationVersion(KEEPASSXC_VERSION);
67
    app.setProperty("KPXC_QUALIFIED_APPNAME", "org.keepassxc.KeePassXC");
68

69
    QCommandLineParser parser;
70
    parser.setApplicationDescription(QObject::tr("KeePassXC - cross-platform password manager"));
71
    parser.addPositionalArgument(
72
        "filename(s)", QObject::tr("filenames of the password databases to open (*.kdbx)"), "[filename(s)]");
73

74
    QCommandLineOption configOption("config", QObject::tr("path to a custom config file"), "config");
75
    QCommandLineOption localConfigOption(
76
        "localconfig", QObject::tr("path to a custom local config file"), "localconfig");
77
    QCommandLineOption lockOption("lock", QObject::tr("lock all open databases"));
78
    QCommandLineOption keyfileOption("keyfile", QObject::tr("key file of the database"), "keyfile");
79
    QCommandLineOption pwstdinOption("pw-stdin", QObject::tr("read password of the database from stdin"));
80
    QCommandLineOption allowScreenCaptureOption("allow-screencapture",
81
                                                QObject::tr("allow screenshots and app recording (Windows/macOS)"));
82

83
    QCommandLineOption helpOption = parser.addHelpOption();
84
    QCommandLineOption versionOption = parser.addVersionOption();
85
    QCommandLineOption debugInfoOption(QStringList() << "debug-info", QObject::tr("Displays debugging information."));
86
    parser.addOption(configOption);
87
    parser.addOption(localConfigOption);
88
    parser.addOption(lockOption);
89
    parser.addOption(keyfileOption);
90
    parser.addOption(pwstdinOption);
91
    parser.addOption(debugInfoOption);
92
    parser.addOption(allowScreenCaptureOption);
93

94
    parser.process(app);
95

96
    // Exit early if we're only showing the help / version
97
    if (parser.isSet(versionOption) || parser.isSet(helpOption)) {
98
        return EXIT_SUCCESS;
99
    }
100

101
    // Show debug information and then exit
102
    if (parser.isSet(debugInfoOption)) {
103
        QTextStream out(stdout, QIODevice::WriteOnly);
104
        QString debugInfo = Tools::debugInfo().append("\n").append(Crypto::debugInfo());
105
        out << debugInfo << endl;
106
        return EXIT_SUCCESS;
107
    }
108

109
    // Process config file options early
110
    if (parser.isSet(configOption) || parser.isSet(localConfigOption)) {
111
        Config::createConfigFromFile(parser.value(configOption), parser.value(localConfigOption));
112
    }
113

114
    // Extract file names provided on the command line for opening
115
    QStringList fileNames;
116
#ifdef Q_OS_WIN
117
    // Get correct case for Windows filenames (fixes #7139)
118
    for (const auto& file : parser.positionalArguments()) {
119
        const auto fileInfo = QFileInfo(file);
120
        WIN32_FIND_DATAW findFileData;
121
        HANDLE hFind;
122
        const wchar_t* absolutePathWchar = reinterpret_cast<const wchar_t*>(fileInfo.absoluteFilePath().utf16());
123
        hFind = FindFirstFileW(absolutePathWchar, &findFileData);
124
        if (hFind != INVALID_HANDLE_VALUE) {
125
            fileNames << QString("%1/%2").arg(fileInfo.absolutePath(), QString::fromWCharArray(findFileData.cFileName));
126
            FindClose(hFind);
127
        }
128
    }
129
#else
130
    for (const auto& file : parser.positionalArguments()) {
131
        if (QFile::exists(file)) {
132
            fileNames << QDir::toNativeSeparators(file);
133
        }
134
    }
135
#endif
136

137
    // Process single instance and early exit if already running
138
    if (app.isAlreadyRunning()) {
139
        if (parser.isSet(lockOption)) {
140
            if (app.sendLockToInstance()) {
141
                qInfo() << QObject::tr("Databases have been locked.").toUtf8().constData();
142
            } else {
143
                qWarning() << QObject::tr("Database failed to lock.").toUtf8().constData();
144
                return EXIT_FAILURE;
145
            }
146
        } else {
147
            if (!fileNames.isEmpty()) {
148
                app.sendFileNamesToRunningInstance(fileNames);
149
            }
150

151
            qWarning() << QObject::tr("Another instance of KeePassXC is already running.").toUtf8().constData();
152
        }
153
        return EXIT_SUCCESS;
154
    }
155

156
    if (parser.isSet(lockOption)) {
157
        qWarning() << QObject::tr("KeePassXC is not running. No open database to lock").toUtf8().constData();
158

159
        // still return with EXIT_SUCCESS because when used within a script for ensuring that there is no unlocked
160
        // keepass database (e.g. screen locking) we can consider it as successful
161
        return EXIT_SUCCESS;
162
    }
163

164
    if (!Crypto::init()) {
165
        QString error = QObject::tr("Fatal error while testing the cryptographic functions.");
166
        error.append("\n");
167
        error.append(Crypto::errorString());
168
        MessageBox::critical(nullptr, QObject::tr("KeePassXC - Error"), error);
169
        return EXIT_FAILURE;
170
    }
171

172
    // Apply the configured theme before creating any GUI elements
173
    app.applyTheme();
174

175
#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
176
    QGuiApplication::setDesktopFileName(app.property("KPXC_QUALIFIED_APPNAME").toString() + QStringLiteral(".desktop"));
177
#endif
178

179
    Application::bootstrap(config()->get(Config::GUI_Language).toString());
180

181
    MainWindow mainWindow;
182
#ifdef Q_OS_WIN
183
    // Qt Hack - Prevent white flicker when showing window
184
    mainWindow.setProperty("windowOpacity", 0.0);
185
#endif
186

187
    // Disable screen capture if not explicitly allowed
188
    // This ensures any top-level windows (Main Window, Modal Dialogs, etc.) are excluded from screenshots
189
    mainWindow.setAllowScreenCapture(parser.isSet(allowScreenCaptureOption));
190

191
    const bool pwstdin = parser.isSet(pwstdinOption);
192
    if (!fileNames.isEmpty() && pwstdin) {
193
        Utils::setDefaultTextStreams();
194
    }
195
    for (const QString& filename : fileNames) {
196
        QString password;
197
        if (pwstdin) {
198
            // we always need consume a line of STDIN if --pw-stdin is set to clear out the
199
            // buffer for native messaging, even if the specified file does not exist
200
            QTextStream out(stdout, QIODevice::WriteOnly);
201
            out << QObject::tr("Database password: ") << flush;
202
            password = Utils::getPassword();
203
        }
204
        mainWindow.openDatabase(filename, password, parser.value(keyfileOption));
205
    }
206

207
    // start minimized if configured
208
    if (config()->get(Config::GUI_MinimizeOnStartup).toBool()) {
209
        mainWindow.hideWindow();
210
    } else {
211
        mainWindow.bringToFront();
212
        Application::processEvents();
213
    }
214

215
    int exitCode = Application::exec();
216

217
    // Check if restart was requested
218
    if (exitCode == RESTART_EXITCODE) {
219
        QProcess::startDetached(QCoreApplication::applicationFilePath(), {});
220
    }
221

222
#if defined(WITH_ASAN) && defined(WITH_LSAN)
223
    // do leak check here to prevent massive tail of end-of-process leak errors from third-party libraries
224
    __lsan_do_leak_check();
225
    __lsan_disable();
226
#endif
227

228
    return exitCode;
229
}
230

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.