2
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
3
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
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.
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.
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/>.
19
#include <QCommandLineParser>
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"
33
#if defined(WITH_ASAN) && defined(WITH_LSAN)
34
#include <sanitizer/lsan_interface.h>
41
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
42
#elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
43
Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
51
int main(int argc, char** argv)
53
QT_REQUIRE_VERSION(argc, argv, QT_VERSION_STR)
55
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
56
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
57
QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
59
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) && defined(Q_OS_WIN)
60
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
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");
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)]");
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)"));
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);
96
// Exit early if we're only showing the help / version
97
if (parser.isSet(versionOption) || parser.isSet(helpOption)) {
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;
109
// Process config file options early
110
if (parser.isSet(configOption) || parser.isSet(localConfigOption)) {
111
Config::createConfigFromFile(parser.value(configOption), parser.value(localConfigOption));
114
// Extract file names provided on the command line for opening
115
QStringList fileNames;
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;
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));
130
for (const auto& file : parser.positionalArguments()) {
131
if (QFile::exists(file)) {
132
fileNames << QDir::toNativeSeparators(file);
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();
143
qWarning() << QObject::tr("Database failed to lock.").toUtf8().constData();
147
if (!fileNames.isEmpty()) {
148
app.sendFileNamesToRunningInstance(fileNames);
151
qWarning() << QObject::tr("Another instance of KeePassXC is already running.").toUtf8().constData();
156
if (parser.isSet(lockOption)) {
157
qWarning() << QObject::tr("KeePassXC is not running. No open database to lock").toUtf8().constData();
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
164
if (!Crypto::init()) {
165
QString error = QObject::tr("Fatal error while testing the cryptographic functions.");
167
error.append(Crypto::errorString());
168
MessageBox::critical(nullptr, QObject::tr("KeePassXC - Error"), error);
172
// Apply the configured theme before creating any GUI elements
175
#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
176
QGuiApplication::setDesktopFileName(app.property("KPXC_QUALIFIED_APPNAME").toString() + QStringLiteral(".desktop"));
179
Application::bootstrap(config()->get(Config::GUI_Language).toString());
181
MainWindow mainWindow;
183
// Qt Hack - Prevent white flicker when showing window
184
mainWindow.setProperty("windowOpacity", 0.0);
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));
191
const bool pwstdin = parser.isSet(pwstdinOption);
192
if (!fileNames.isEmpty() && pwstdin) {
193
Utils::setDefaultTextStreams();
195
for (const QString& filename : fileNames) {
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();
204
mainWindow.openDatabase(filename, password, parser.value(keyfileOption));
207
// start minimized if configured
208
if (config()->get(Config::GUI_MinimizeOnStartup).toBool()) {
209
mainWindow.hideWindow();
211
mainWindow.bringToFront();
212
Application::processEvents();
215
int exitCode = Application::exec();
217
// Check if restart was requested
218
if (exitCode == RESTART_EXITCODE) {
219
QProcess::startDetached(QCoreApplication::applicationFilePath(), {});
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();