2010-08-07 09:10:44 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
2020-10-08 22:48:45 -04:00
|
|
|
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
|
2010-08-07 09:10:44 -04: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-10-31 23:27:38 -04:00
|
|
|
#include <QCommandLineParser>
|
2013-10-03 09:18:16 -04:00
|
|
|
#include <QFile>
|
2016-10-24 14:22:02 -04:00
|
|
|
#include <QTextStream>
|
2021-02-04 16:39:13 -05:00
|
|
|
#include <QWindow>
|
2010-08-15 06:31:48 -04:00
|
|
|
|
2018-10-31 23:27:38 -04:00
|
|
|
#include "cli/Utils.h"
|
2014-01-18 09:31:24 -05:00
|
|
|
#include "config-keepassx.h"
|
2018-09-29 13:00:47 -04:00
|
|
|
#include "core/Config.h"
|
2018-10-31 23:27:38 -04:00
|
|
|
#include "core/Tools.h"
|
2010-09-13 17:25:48 -04:00
|
|
|
#include "crypto/Crypto.h"
|
2012-05-17 18:35:24 -04:00
|
|
|
#include "gui/Application.h"
|
2010-09-19 10:59:32 -04:00
|
|
|
#include "gui/MainWindow.h"
|
2014-06-15 05:17:40 -04:00
|
|
|
#include "gui/MessageBox.h"
|
2021-02-04 16:39:13 -05:00
|
|
|
#include "gui/osutils/OSUtils.h"
|
2018-03-08 10:24:11 -05:00
|
|
|
|
2017-03-14 09:53:29 -04:00
|
|
|
#if defined(WITH_ASAN) && defined(WITH_LSAN)
|
2017-03-14 10:32:48 -04:00
|
|
|
#include <sanitizer/lsan_interface.h>
|
|
|
|
#endif
|
|
|
|
|
2017-01-25 19:15:12 -05:00
|
|
|
#ifdef QT_STATIC
|
|
|
|
#include <QtPlugin>
|
|
|
|
|
2017-12-27 14:31:15 -05:00
|
|
|
#if defined(Q_OS_WIN)
|
2017-01-25 19:15:12 -05:00
|
|
|
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
|
2018-10-26 09:19:04 -04:00
|
|
|
#elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
|
2017-01-25 19:15:12 -05:00
|
|
|
Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-05-02 11:04:03 -04:00
|
|
|
int main(int argc, char** argv)
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
2019-01-24 22:45:05 -05:00
|
|
|
QT_REQUIRE_VERSION(argc, argv, QT_VERSION_STR)
|
|
|
|
|
2018-10-20 06:18:33 -04:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
|
|
|
|
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
2019-06-30 11:06:58 -04:00
|
|
|
QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
|
|
|
|
#endif
|
2020-07-14 18:34:14 -04:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) && defined(Q_OS_WIN)
|
2020-06-28 22:48:59 -04:00
|
|
|
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
|
|
|
|
#endif
|
2021-02-24 23:10:13 -05: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 06:18:33 -04:00
|
|
|
|
2014-01-18 09:23:55 -05:00
|
|
|
QCommandLineParser parser;
|
2019-03-19 14:48:33 -04:00
|
|
|
parser.setApplicationDescription(QObject::tr("KeePassXC - cross-platform password manager"));
|
2018-10-31 23:27:38 -04:00
|
|
|
parser.addPositionalArgument(
|
2020-09-20 09:39:59 -04:00
|
|
|
"filename(s)", QObject::tr("filenames of the password databases to open (*.kdbx)"), "[filename(s)]");
|
2018-03-31 16:01:30 -04:00
|
|
|
|
2019-03-19 14:48:33 -04:00
|
|
|
QCommandLineOption configOption("config", QObject::tr("path to a custom config file"), "config");
|
2020-09-20 09:39:59 -04:00
|
|
|
QCommandLineOption localConfigOption(
|
|
|
|
"localconfig", QObject::tr("path to a custom local config file"), "localconfig");
|
2019-03-19 14:48:33 -04: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 16:39:13 -05:00
|
|
|
QCommandLineOption allowScreenCaptureOption("allow-screencapture",
|
|
|
|
QObject::tr("allow app screen recordering and screenshots"));
|
2014-01-18 09:23:55 -05:00
|
|
|
|
2018-10-08 12:29:08 -04:00
|
|
|
QCommandLineOption helpOption = parser.addHelpOption();
|
2018-06-29 20:46:33 -04:00
|
|
|
QCommandLineOption versionOption = parser.addVersionOption();
|
2019-05-12 17:42:55 -04:00
|
|
|
QCommandLineOption debugInfoOption(QStringList() << "debug-info", QObject::tr("Displays debugging information."));
|
2014-01-18 09:23:55 -05:00
|
|
|
parser.addOption(configOption);
|
2020-09-20 09:39:59 -04:00
|
|
|
parser.addOption(localConfigOption);
|
2014-01-18 09:23:55 -05:00
|
|
|
parser.addOption(keyfileOption);
|
2016-10-24 14:22:02 -04:00
|
|
|
parser.addOption(pwstdinOption);
|
2019-02-18 20:17:28 -05:00
|
|
|
parser.addOption(debugInfoOption);
|
2014-01-18 09:23:55 -05:00
|
|
|
|
2021-02-04 16:39:13 -05:00
|
|
|
if (osUtils->canPreventScreenCapture()) {
|
|
|
|
parser.addOption(allowScreenCaptureOption);
|
|
|
|
}
|
|
|
|
|
2014-01-18 09:23:55 -05:00
|
|
|
parser.process(app);
|
2018-10-31 23:27:38 -04:00
|
|
|
|
2020-09-20 09:39:59 -04:00
|
|
|
// Exit early if we're only showing the help / version
|
2018-10-08 12:29:08 -04:00
|
|
|
if (parser.isSet(versionOption) || parser.isSet(helpOption)) {
|
2019-02-18 20:17:28 -05:00
|
|
|
return EXIT_SUCCESS;
|
2018-10-08 12:29:08 -04:00
|
|
|
}
|
2018-10-31 23:27:38 -04:00
|
|
|
|
2021-04-04 09:03:48 -04: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 09:39:59 -04:00
|
|
|
// Process config file options early
|
|
|
|
if (parser.isSet(configOption) || parser.isSet(localConfigOption)) {
|
|
|
|
Config::createConfigFromFile(parser.value(configOption), parser.value(localConfigOption));
|
|
|
|
}
|
2017-12-05 10:25:08 -05:00
|
|
|
|
2020-09-20 09:39:59 -04:00
|
|
|
// Process single instance and early exit if already running
|
|
|
|
const QStringList fileNames = parser.positionalArguments();
|
2018-10-27 09:58:50 -04:00
|
|
|
if (app.isAlreadyRunning()) {
|
2017-12-05 10:25:08 -05:00
|
|
|
if (!fileNames.isEmpty()) {
|
|
|
|
app.sendFileNamesToRunningInstance(fileNames);
|
|
|
|
}
|
2019-03-19 14:48:33 -04:00
|
|
|
qWarning() << QObject::tr("Another instance of KeePassXC is already running.").toUtf8().constData();
|
2019-02-18 20:17:28 -05:00
|
|
|
return EXIT_SUCCESS;
|
2017-12-05 10:25:08 -05:00
|
|
|
}
|
2017-12-12 03:15:23 -05:00
|
|
|
|
2017-12-05 10:25:08 -05:00
|
|
|
if (!Crypto::init()) {
|
2019-01-30 20:54:35 -05:00
|
|
|
QString error = QObject::tr("Fatal error while testing the cryptographic functions.");
|
2017-12-05 10:25:08 -05:00
|
|
|
error.append("\n");
|
|
|
|
error.append(Crypto::errorString());
|
2019-01-30 20:54:35 -05:00
|
|
|
MessageBox::critical(nullptr, QObject::tr("KeePassXC - Error"), error);
|
2019-02-18 20:17:28 -05:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2021-04-04 09:03:48 -04: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 09:23:55 -05:00
|
|
|
|
2010-09-19 10:59:32 -04:00
|
|
|
MainWindow mainWindow;
|
2017-04-05 09:00:40 -04:00
|
|
|
|
2021-02-04 16:39:13 -05: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 10:25:08 -05:00
|
|
|
const bool pwstdin = parser.isSet(pwstdinOption);
|
2018-03-31 16:01:30 -04:00
|
|
|
for (const QString& filename : fileNames) {
|
2018-03-04 17:56:06 -05: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 10:37:13 -04:00
|
|
|
QTextStream out(stdout, QIODevice::WriteOnly);
|
2019-01-30 20:54:35 -05:00
|
|
|
out << QObject::tr("Database password: ") << flush;
|
2020-06-28 16:24:37 -04:00
|
|
|
Utils::setDefaultTextStreams();
|
2018-03-08 10:24:11 -05:00
|
|
|
password = Utils::getPassword();
|
2018-03-04 17:56:06 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 03:15:23 -05:00
|
|
|
if (!filename.isEmpty() && QFile::exists(filename) && !filename.endsWith(".json", Qt::CaseInsensitive)) {
|
2018-03-02 19:19:30 -05:00
|
|
|
mainWindow.openDatabase(filename, password, parser.value(keyfileOption));
|
2014-01-18 09:23:55 -05:00
|
|
|
}
|
2012-04-24 05:47:16 -04:00
|
|
|
}
|
|
|
|
|
2018-09-29 13:00:47 -04:00
|
|
|
int exitCode = Application::exec();
|
2017-03-14 10:32:48 -04:00
|
|
|
|
2020-06-28 10:01:24 -04:00
|
|
|
// Check if restart was requested
|
|
|
|
if (exitCode == RESTART_EXITCODE) {
|
2020-07-06 21:09:59 -04:00
|
|
|
QProcess::startDetached(QCoreApplication::applicationFilePath(), {});
|
2020-06-28 10:01:24 -04:00
|
|
|
}
|
|
|
|
|
2017-03-14 09:53:29 -04:00
|
|
|
#if defined(WITH_ASAN) && defined(WITH_LSAN)
|
2017-03-14 10:32:48 -04: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 09:10:44 -04:00
|
|
|
}
|