2010-08-07 13:10:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
2020-10-09 02:48:45 +00:00
|
|
|
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
|
2010-08-07 13:10:44 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2018-11-01 03:27:38 +00:00
|
|
|
#include <QCommandLineParser>
|
2022-04-12 19:05:06 +00:00
|
|
|
#include <QDir>
|
2013-10-03 13:18:16 +00:00
|
|
|
#include <QFile>
|
2021-02-04 21:39:13 +00:00
|
|
|
#include <QWindow>
|
2010-08-15 10:31:48 +00:00
|
|
|
|
2018-11-01 03:27:38 +00:00
|
|
|
#include "cli/Utils.h"
|
2014-01-18 14:31:24 +00:00
|
|
|
#include "config-keepassx.h"
|
2018-11-01 03:27:38 +00:00
|
|
|
#include "core/Tools.h"
|
2010-09-13 21:25:48 +00:00
|
|
|
#include "crypto/Crypto.h"
|
2012-05-17 22:35:24 +00:00
|
|
|
#include "gui/Application.h"
|
2010-09-19 14:59:32 +00:00
|
|
|
#include "gui/MainWindow.h"
|
2014-06-15 09:17:40 +00:00
|
|
|
#include "gui/MessageBox.h"
|
2021-02-04 21:39:13 +00:00
|
|
|
#include "gui/osutils/OSUtils.h"
|
2018-03-08 15:24:11 +00:00
|
|
|
|
2017-03-14 13:53:29 +00:00
|
|
|
#if defined(WITH_ASAN) && defined(WITH_LSAN)
|
2017-03-14 14:32:48 +00:00
|
|
|
#include <sanitizer/lsan_interface.h>
|
|
|
|
#endif
|
|
|
|
|
2017-01-26 00:15:12 +00:00
|
|
|
#ifdef QT_STATIC
|
|
|
|
#include <QtPlugin>
|
|
|
|
|
2017-12-27 19:31:15 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
2017-01-26 00:15:12 +00:00
|
|
|
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
|
2018-10-26 13:19:04 +00:00
|
|
|
#elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
|
2017-01-26 00:15:12 +00:00
|
|
|
Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-05-02 15:04:03 +00:00
|
|
|
int main(int argc, char** argv)
|
2010-08-07 13:10:44 +00:00
|
|
|
{
|
2019-01-25 03:45:05 +00:00
|
|
|
QT_REQUIRE_VERSION(argc, argv, QT_VERSION_STR)
|
|
|
|
|
2018-10-20 10:18:33 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
|
|
|
|
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
2019-06-30 15:06:58 +00:00
|
|
|
QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
|
|
|
|
#endif
|
2020-07-14 22:34:14 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) && defined(Q_OS_WIN)
|
2020-06-29 02:48:59 +00:00
|
|
|
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
|
|
|
|
#endif
|
2021-02-25 04:10:13 +00:00
|
|
|
Application app(argc, argv);
|
|
|
|
// don't set organizationName as that changes the return value of
|
|
|
|
// QStandardPaths::writableLocation(QDesktopServices::DataLocation)
|
|
|
|
Application::setApplicationName("KeePassXC");
|
|
|
|
Application::setApplicationVersion(KEEPASSXC_VERSION);
|
|
|
|
app.setProperty("KPXC_QUALIFIED_APPNAME", "org.keepassxc.KeePassXC");
|
2018-10-20 10:18:33 +00:00
|
|
|
|
2014-01-18 14:23:55 +00:00
|
|
|
QCommandLineParser parser;
|
2019-03-19 18:48:33 +00:00
|
|
|
parser.setApplicationDescription(QObject::tr("KeePassXC - cross-platform password manager"));
|
2018-11-01 03:27:38 +00:00
|
|
|
parser.addPositionalArgument(
|
2020-09-20 13:39:59 +00:00
|
|
|
"filename(s)", QObject::tr("filenames of the password databases to open (*.kdbx)"), "[filename(s)]");
|
2018-03-31 20:01:30 +00:00
|
|
|
|
2019-03-19 18:48:33 +00:00
|
|
|
QCommandLineOption configOption("config", QObject::tr("path to a custom config file"), "config");
|
2020-09-20 13:39:59 +00:00
|
|
|
QCommandLineOption localConfigOption(
|
|
|
|
"localconfig", QObject::tr("path to a custom local config file"), "localconfig");
|
2021-05-15 13:48:59 +00:00
|
|
|
QCommandLineOption lockOption("lock", QObject::tr("lock all open databases"));
|
2019-03-19 18:48:33 +00:00
|
|
|
QCommandLineOption keyfileOption("keyfile", QObject::tr("key file of the database"), "keyfile");
|
|
|
|
QCommandLineOption pwstdinOption("pw-stdin", QObject::tr("read password of the database from stdin"));
|
2021-02-04 21:39:13 +00:00
|
|
|
QCommandLineOption allowScreenCaptureOption("allow-screencapture",
|
|
|
|
QObject::tr("allow app screen recordering and screenshots"));
|
2014-01-18 14:23:55 +00:00
|
|
|
|
2018-10-08 16:29:08 +00:00
|
|
|
QCommandLineOption helpOption = parser.addHelpOption();
|
2018-06-30 00:46:33 +00:00
|
|
|
QCommandLineOption versionOption = parser.addVersionOption();
|
2019-05-12 21:42:55 +00:00
|
|
|
QCommandLineOption debugInfoOption(QStringList() << "debug-info", QObject::tr("Displays debugging information."));
|
2014-01-18 14:23:55 +00:00
|
|
|
parser.addOption(configOption);
|
2020-09-20 13:39:59 +00:00
|
|
|
parser.addOption(localConfigOption);
|
2021-05-15 13:48:59 +00:00
|
|
|
parser.addOption(lockOption);
|
2014-01-18 14:23:55 +00:00
|
|
|
parser.addOption(keyfileOption);
|
2016-10-24 18:22:02 +00:00
|
|
|
parser.addOption(pwstdinOption);
|
2019-02-19 01:17:28 +00:00
|
|
|
parser.addOption(debugInfoOption);
|
2014-01-18 14:23:55 +00:00
|
|
|
|
2021-02-04 21:39:13 +00:00
|
|
|
if (osUtils->canPreventScreenCapture()) {
|
|
|
|
parser.addOption(allowScreenCaptureOption);
|
|
|
|
}
|
|
|
|
|
2014-01-18 14:23:55 +00:00
|
|
|
parser.process(app);
|
2018-11-01 03:27:38 +00:00
|
|
|
|
2020-09-20 13:39:59 +00:00
|
|
|
// Exit early if we're only showing the help / version
|
2018-10-08 16:29:08 +00:00
|
|
|
if (parser.isSet(versionOption) || parser.isSet(helpOption)) {
|
2019-02-19 01:17:28 +00:00
|
|
|
return EXIT_SUCCESS;
|
2018-10-08 16:29:08 +00:00
|
|
|
}
|
2018-11-01 03:27:38 +00:00
|
|
|
|
2021-04-04 13:03:48 +00:00
|
|
|
// Show debug information and then exit
|
|
|
|
if (parser.isSet(debugInfoOption)) {
|
|
|
|
QTextStream out(stdout, QIODevice::WriteOnly);
|
|
|
|
QString debugInfo = Tools::debugInfo().append("\n").append(Crypto::debugInfo());
|
|
|
|
out << debugInfo << endl;
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-09-20 13:39:59 +00:00
|
|
|
// Process config file options early
|
|
|
|
if (parser.isSet(configOption) || parser.isSet(localConfigOption)) {
|
|
|
|
Config::createConfigFromFile(parser.value(configOption), parser.value(localConfigOption));
|
|
|
|
}
|
2017-12-05 15:25:08 +00:00
|
|
|
|
2020-09-20 13:39:59 +00:00
|
|
|
// Process single instance and early exit if already running
|
2021-05-15 13:48:59 +00:00
|
|
|
// FIXME: this is a *mess* and it is entirely my fault. --wundrweapon
|
2020-09-20 13:39:59 +00:00
|
|
|
const QStringList fileNames = parser.positionalArguments();
|
2018-10-27 13:58:50 +00:00
|
|
|
if (app.isAlreadyRunning()) {
|
2021-05-15 13:48:59 +00:00
|
|
|
if (parser.isSet(lockOption)) {
|
|
|
|
if (app.sendLockToInstance()) {
|
|
|
|
qInfo() << QObject::tr("Locked databases.").toUtf8().constData();
|
|
|
|
} else {
|
|
|
|
qWarning() << QObject::tr("Database failed to lock.").toUtf8().constData();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!fileNames.isEmpty()) {
|
|
|
|
app.sendFileNamesToRunningInstance(fileNames);
|
|
|
|
}
|
|
|
|
|
|
|
|
qWarning() << QObject::tr("Another instance of KeePassXC is already running.").toUtf8().constData();
|
2017-12-05 15:25:08 +00:00
|
|
|
}
|
2019-02-19 01:17:28 +00:00
|
|
|
return EXIT_SUCCESS;
|
2017-12-05 15:25:08 +00:00
|
|
|
}
|
2017-12-12 08:15:23 +00:00
|
|
|
|
2017-12-05 15:25:08 +00:00
|
|
|
if (!Crypto::init()) {
|
2019-01-31 01:54:35 +00:00
|
|
|
QString error = QObject::tr("Fatal error while testing the cryptographic functions.");
|
2017-12-05 15:25:08 +00:00
|
|
|
error.append("\n");
|
|
|
|
error.append(Crypto::errorString());
|
2019-01-31 01:54:35 +00:00
|
|
|
MessageBox::critical(nullptr, QObject::tr("KeePassXC - Error"), error);
|
2019-02-19 01:17:28 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2021-04-04 13:03:48 +00:00
|
|
|
// Apply the configured theme before creating any GUI elements
|
|
|
|
app.applyTheme();
|
|
|
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
|
|
|
|
QGuiApplication::setDesktopFileName(app.property("KPXC_QUALIFIED_APPNAME").toString() + QStringLiteral(".desktop"));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Application::bootstrap();
|
2014-01-18 14:23:55 +00:00
|
|
|
|
2010-09-19 14:59:32 +00:00
|
|
|
MainWindow mainWindow;
|
2017-04-05 13:00:40 +00:00
|
|
|
|
2021-02-04 21:39:13 +00:00
|
|
|
#ifndef QT_DEBUG
|
|
|
|
// Disable screen capture if capable and not explicitly allowed
|
|
|
|
if (osUtils->canPreventScreenCapture() && !parser.isSet(allowScreenCaptureOption)) {
|
|
|
|
// This ensures any top-level windows (Main Window, Modal Dialogs, etc.) are excluded from screenshots
|
|
|
|
QObject::connect(&app, &QGuiApplication::focusWindowChanged, &mainWindow, [&](QWindow* window) {
|
|
|
|
if (window) {
|
|
|
|
if (!osUtils->setPreventScreenCapture(window, true)) {
|
|
|
|
mainWindow.displayGlobalMessage(
|
|
|
|
QObject::tr("Warning: Failed to prevent screenshots on a top level window!"),
|
|
|
|
MessageWidget::Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-12-05 15:25:08 +00:00
|
|
|
const bool pwstdin = parser.isSet(pwstdinOption);
|
2021-04-07 16:50:31 +00:00
|
|
|
if (!fileNames.isEmpty() && pwstdin) {
|
|
|
|
Utils::setDefaultTextStreams();
|
|
|
|
}
|
2018-03-31 20:01:30 +00:00
|
|
|
for (const QString& filename : fileNames) {
|
2018-03-04 22:56:06 +00:00
|
|
|
QString password;
|
|
|
|
if (pwstdin) {
|
|
|
|
// we always need consume a line of STDIN if --pw-stdin is set to clear out the
|
|
|
|
// buffer for native messaging, even if the specified file does not exist
|
2019-04-25 14:37:13 +00:00
|
|
|
QTextStream out(stdout, QIODevice::WriteOnly);
|
2019-01-31 01:54:35 +00:00
|
|
|
out << QObject::tr("Database password: ") << flush;
|
2018-03-08 15:24:11 +00:00
|
|
|
password = Utils::getPassword();
|
2018-03-04 22:56:06 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 08:15:23 +00:00
|
|
|
if (!filename.isEmpty() && QFile::exists(filename) && !filename.endsWith(".json", Qt::CaseInsensitive)) {
|
2022-04-12 19:05:06 +00:00
|
|
|
mainWindow.openDatabase(QDir::toNativeSeparators(filename), password, parser.value(keyfileOption));
|
2014-01-18 14:23:55 +00:00
|
|
|
}
|
2012-04-24 09:47:16 +00:00
|
|
|
}
|
|
|
|
|
2018-09-29 17:00:47 +00:00
|
|
|
int exitCode = Application::exec();
|
2017-03-14 14:32:48 +00:00
|
|
|
|
2020-06-28 14:01:24 +00:00
|
|
|
// Check if restart was requested
|
|
|
|
if (exitCode == RESTART_EXITCODE) {
|
2020-07-07 01:09:59 +00:00
|
|
|
QProcess::startDetached(QCoreApplication::applicationFilePath(), {});
|
2020-06-28 14:01:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 13:53:29 +00:00
|
|
|
#if defined(WITH_ASAN) && defined(WITH_LSAN)
|
2017-03-14 14:32:48 +00:00
|
|
|
// 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;
|
2010-08-07 13:10:44 +00:00
|
|
|
}
|