2017-12-12 10:15:23 +02:00
|
|
|
/*
|
2018-11-01 04:27:38 +01:00
|
|
|
* Copyright (C) 2013 Francois Ferrand
|
|
|
|
* Copyright (C) 2017 Sami Vänttinen <sami.vanttinen@protonmail.com>
|
2020-05-10 21:20:00 -04:00
|
|
|
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
|
2018-11-01 04:27:38 +01: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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2017-12-12 10:15:23 +02:00
|
|
|
|
2019-08-30 20:18:41 -04:00
|
|
|
#include <QCheckBox>
|
2019-11-01 20:13:12 +02:00
|
|
|
#include <QHostAddress>
|
2018-03-23 11:18:06 +01:00
|
|
|
#include <QInputDialog>
|
2018-11-01 04:27:38 +01:00
|
|
|
#include <QJsonArray>
|
2018-03-23 11:18:06 +01:00
|
|
|
#include <QMessageBox>
|
2018-11-01 04:27:38 +01:00
|
|
|
#include <QProgressDialog>
|
2018-03-23 11:18:06 +01:00
|
|
|
#include <QUuid>
|
|
|
|
|
2017-12-12 10:15:23 +02:00
|
|
|
#include "BrowserAccessControlDialog.h"
|
2020-05-10 21:20:00 -04:00
|
|
|
#include "BrowserAction.h"
|
2018-03-31 16:01:30 -04:00
|
|
|
#include "BrowserEntryConfig.h"
|
2018-10-19 21:44:08 +03:00
|
|
|
#include "BrowserEntrySaveDialog.h"
|
2020-05-10 21:20:00 -04:00
|
|
|
#include "BrowserHost.h"
|
2018-11-01 04:27:38 +01:00
|
|
|
#include "BrowserService.h"
|
2018-03-31 16:01:30 -04:00
|
|
|
#include "BrowserSettings.h"
|
2017-12-12 10:15:23 +02:00
|
|
|
#include "core/Database.h"
|
|
|
|
#include "core/EntrySearcher.h"
|
2018-03-31 16:01:30 -04:00
|
|
|
#include "core/Group.h"
|
2017-12-12 10:15:23 +02:00
|
|
|
#include "core/Metadata.h"
|
|
|
|
#include "core/PasswordGenerator.h"
|
2018-01-25 14:21:05 +02:00
|
|
|
#include "core/Tools.h"
|
2018-01-15 01:15:39 +01:00
|
|
|
#include "gui/MainWindow.h"
|
2018-12-19 20:14:11 -08:00
|
|
|
#include "gui/MessageBox.h"
|
2018-01-25 14:21:05 +02:00
|
|
|
#ifdef Q_OS_MACOS
|
2020-01-28 21:42:57 +01:00
|
|
|
#include "gui/osutils/macutils/MacUtils.h"
|
2018-01-25 14:21:05 +02:00
|
|
|
#endif
|
2017-12-12 10:15:23 +02:00
|
|
|
|
2019-08-15 12:35:11 +03:00
|
|
|
const QString BrowserService::KEEPASSXCBROWSER_NAME = QStringLiteral("KeePassXC-Browser Settings");
|
|
|
|
const QString BrowserService::KEEPASSXCBROWSER_OLD_NAME = QStringLiteral("keepassxc-browser Settings");
|
|
|
|
static const QString KEEPASSXCBROWSER_GROUP_NAME = QStringLiteral("KeePassXC-Browser Passwords");
|
2018-03-31 16:01:30 -04:00
|
|
|
static int KEEPASSXCBROWSER_DEFAULT_ICON = 1;
|
2018-10-24 17:49:53 +03:00
|
|
|
// These are for the settings and password conversion
|
2019-08-15 12:35:11 +03:00
|
|
|
static const QString KEEPASSHTTP_NAME = QStringLiteral("KeePassHttp Settings");
|
|
|
|
static const QString KEEPASSHTTP_GROUP_NAME = QStringLiteral("KeePassHttp Passwords");
|
2019-08-15 12:35:11 +03:00
|
|
|
// Extra entry related options saved in custom data
|
2019-08-15 12:35:11 +03:00
|
|
|
const QString BrowserService::OPTION_SKIP_AUTO_SUBMIT = QStringLiteral("BrowserSkipAutoSubmit");
|
|
|
|
const QString BrowserService::OPTION_HIDE_ENTRY = QStringLiteral("BrowserHideEntry");
|
2019-11-24 18:10:40 +02:00
|
|
|
const QString BrowserService::OPTION_ONLY_HTTP_AUTH = QStringLiteral("BrowserOnlyHttpAuth");
|
2019-08-15 12:35:11 +03:00
|
|
|
// Multiple URL's
|
|
|
|
const QString BrowserService::ADDITIONAL_URL = QStringLiteral("KP2A_URL");
|
2017-12-12 10:15:23 +02:00
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
Q_GLOBAL_STATIC(BrowserService, s_browserService);
|
|
|
|
|
|
|
|
BrowserService::BrowserService()
|
|
|
|
: QObject()
|
|
|
|
, m_browserHost(new BrowserHost)
|
2018-03-31 16:01:30 -04:00
|
|
|
, m_dialogActive(false)
|
2020-07-05 14:32:23 +03:00
|
|
|
, m_bringToFrontRequested(false)
|
2019-02-01 18:11:15 -05:00
|
|
|
, m_prevWindowState(WindowState::Normal)
|
2019-01-30 16:48:22 +02:00
|
|
|
, m_keepassBrowserUUID(Tools::hexToUuid("de887cc3036343b8974b5911b8816224"))
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
2020-05-10 21:20:00 -04:00
|
|
|
connect(m_browserHost, &BrowserHost::clientMessageReceived, this, &BrowserService::processClientMessage);
|
|
|
|
setEnabled(browserSettings()->isEnabled());
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowserService* BrowserService::instance()
|
|
|
|
{
|
|
|
|
return s_browserService;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserService::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled) {
|
|
|
|
// Update KeePassXC/keepassxc-proxy binary paths to Native Messaging scripts
|
|
|
|
if (browserSettings()->updateBinaryPath()) {
|
|
|
|
browserSettings()->updateBinaryPaths();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_browserHost->start();
|
|
|
|
} else {
|
|
|
|
m_browserHost->stop();
|
2018-10-24 17:49:53 +03:00
|
|
|
}
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BrowserService::isDatabaseOpened() const
|
|
|
|
{
|
2020-05-10 21:20:00 -04:00
|
|
|
if (m_currentDatabaseWidget) {
|
|
|
|
return !m_currentDatabaseWidget->isLocked();
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
2020-05-10 21:20:00 -04:00
|
|
|
return false;
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2018-01-17 14:55:13 +02:00
|
|
|
bool BrowserService::openDatabase(bool triggerUnlock)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
2018-09-05 16:21:59 -04:00
|
|
|
if (!browserSettings()->unlockDatabase()) {
|
2017-12-12 10:15:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
if (m_currentDatabaseWidget && !m_currentDatabaseWidget->isLocked()) {
|
2017-12-12 10:15:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:55:13 +02:00
|
|
|
if (triggerUnlock) {
|
2020-07-05 14:32:23 +03:00
|
|
|
m_bringToFrontRequested = true;
|
|
|
|
updateWindowState();
|
2019-10-28 10:52:03 +02:00
|
|
|
emit requestUnlock();
|
2018-01-17 14:55:13 +02:00
|
|
|
}
|
2018-01-15 01:15:39 +01:00
|
|
|
|
2017-12-12 10:15:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserService::lockDatabase()
|
|
|
|
{
|
2020-05-10 21:20:00 -04:00
|
|
|
if (m_currentDatabaseWidget) {
|
|
|
|
m_currentDatabaseWidget->lock();
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
2020-05-10 21:20:00 -04:00
|
|
|
}
|
2017-12-12 10:15:23 +02:00
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
QString BrowserService::getDatabaseHash(bool legacy)
|
|
|
|
{
|
|
|
|
if (legacy) {
|
|
|
|
return QCryptographicHash::hash(
|
|
|
|
(browserService()->getDatabaseRootUuid() + browserService()->getDatabaseRecycleBinUuid()).toUtf8(),
|
|
|
|
QCryptographicHash::Sha256)
|
|
|
|
.toHex();
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
2020-05-10 21:20:00 -04:00
|
|
|
return QCryptographicHash::hash(getDatabaseRootUuid().toUtf8(), QCryptographicHash::Sha256).toHex();
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString BrowserService::getDatabaseRootUuid()
|
|
|
|
{
|
2018-11-22 11:47:31 +01:00
|
|
|
auto db = getDatabase();
|
2017-12-12 10:15:23 +02:00
|
|
|
if (!db) {
|
2018-10-24 17:49:53 +03:00
|
|
|
return {};
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Group* rootGroup = db->rootGroup();
|
|
|
|
if (!rootGroup) {
|
2018-10-24 17:49:53 +03:00
|
|
|
return {};
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 08:45:06 -04:00
|
|
|
return rootGroup->uuidToHex();
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString BrowserService::getDatabaseRecycleBinUuid()
|
|
|
|
{
|
2018-11-22 11:47:31 +01:00
|
|
|
auto db = getDatabase();
|
2017-12-12 10:15:23 +02:00
|
|
|
if (!db) {
|
2018-10-24 17:49:53 +03:00
|
|
|
return {};
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Group* recycleBin = db->metadata()->recycleBin();
|
|
|
|
if (!recycleBin) {
|
2018-10-24 17:49:53 +03:00
|
|
|
return {};
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
2018-09-30 08:45:06 -04:00
|
|
|
return recycleBin->uuidToHex();
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2019-03-16 10:34:50 +02:00
|
|
|
QJsonArray BrowserService::getChildrenFromGroup(Group* group)
|
2019-01-30 16:48:22 +02:00
|
|
|
{
|
|
|
|
QJsonArray groupList;
|
|
|
|
|
|
|
|
if (!group) {
|
|
|
|
return groupList;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& c : group->children()) {
|
|
|
|
if (c == group->database()->metadata()->recycleBin()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject jsonGroup;
|
|
|
|
jsonGroup["name"] = c->name();
|
|
|
|
jsonGroup["uuid"] = Tools::uuidToHex(c->uuid());
|
2019-03-16 10:34:50 +02:00
|
|
|
jsonGroup["children"] = getChildrenFromGroup(c);
|
2019-01-30 16:48:22 +02:00
|
|
|
groupList.push_back(jsonGroup);
|
|
|
|
}
|
|
|
|
return groupList;
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
QJsonObject BrowserService::getDatabaseGroups()
|
2019-01-30 16:48:22 +02:00
|
|
|
{
|
2020-05-10 21:20:00 -04:00
|
|
|
auto db = getDatabase();
|
2019-01-30 16:48:22 +02:00
|
|
|
if (!db) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Group* rootGroup = db->rootGroup();
|
|
|
|
if (!rootGroup) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject root;
|
|
|
|
root["name"] = rootGroup->name();
|
|
|
|
root["uuid"] = Tools::uuidToHex(rootGroup->uuid());
|
2019-03-16 10:34:50 +02:00
|
|
|
root["children"] = getChildrenFromGroup(rootGroup);
|
2019-01-30 16:48:22 +02:00
|
|
|
|
|
|
|
QJsonArray groups;
|
|
|
|
groups.push_back(root);
|
|
|
|
|
|
|
|
QJsonObject result;
|
|
|
|
result["groups"] = groups;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-16 10:34:50 +02:00
|
|
|
QJsonObject BrowserService::createNewGroup(const QString& groupName)
|
|
|
|
{
|
|
|
|
auto db = getDatabase();
|
|
|
|
if (!db) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Group* rootGroup = db->rootGroup();
|
|
|
|
if (!rootGroup) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto group = rootGroup->findGroupByPath(groupName);
|
|
|
|
|
|
|
|
// Group already exists
|
|
|
|
if (group) {
|
2020-05-10 21:20:00 -04:00
|
|
|
QJsonObject result;
|
2019-03-16 10:34:50 +02:00
|
|
|
result["name"] = group->name();
|
|
|
|
result["uuid"] = Tools::uuidToHex(group->uuid());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto dialogResult = MessageBox::warning(nullptr,
|
|
|
|
tr("KeePassXC: Create a new group"),
|
|
|
|
tr("A request for creating a new group \"%1\" has been received.\n"
|
2019-03-19 14:48:33 -04:00
|
|
|
"Do you want to create this group?\n")
|
|
|
|
.arg(groupName),
|
2019-03-16 10:34:50 +02:00
|
|
|
MessageBox::Yes | MessageBox::No);
|
|
|
|
|
|
|
|
if (dialogResult != MessageBox::Yes) {
|
2020-05-10 21:20:00 -04:00
|
|
|
return {};
|
2019-03-16 10:34:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString name, uuid;
|
|
|
|
Group* previousGroup = rootGroup;
|
|
|
|
auto groups = groupName.split("/");
|
|
|
|
|
|
|
|
// Returns the group name based on depth
|
|
|
|
auto getGroupName = [&](int depth) {
|
|
|
|
QString gName;
|
2019-03-19 14:48:33 -04:00
|
|
|
for (int i = 0; i < depth + 1; ++i) {
|
2019-03-16 10:34:50 +02:00
|
|
|
gName.append((i == 0 ? "" : "/") + groups[i]);
|
|
|
|
}
|
|
|
|
return gName;
|
|
|
|
};
|
2019-03-19 14:48:33 -04:00
|
|
|
|
2019-03-16 10:34:50 +02:00
|
|
|
// Create new group(s) always when the path is not found
|
|
|
|
for (int i = 0; i < groups.length(); ++i) {
|
|
|
|
QString gName = getGroupName(i);
|
|
|
|
auto tempGroup = rootGroup->findGroupByPath(gName);
|
|
|
|
if (!tempGroup) {
|
2019-03-19 14:48:33 -04:00
|
|
|
Group* newGroup = new Group();
|
2019-03-16 10:34:50 +02:00
|
|
|
newGroup->setName(groups[i]);
|
|
|
|
newGroup->setUuid(QUuid::createUuid());
|
|
|
|
newGroup->setParent(previousGroup);
|
|
|
|
name = newGroup->name();
|
|
|
|
uuid = Tools::uuidToHex(newGroup->uuid());
|
|
|
|
previousGroup = newGroup;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousGroup = tempGroup;
|
|
|
|
}
|
2019-03-19 14:48:33 -04:00
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
QJsonObject result;
|
2019-03-16 10:34:50 +02:00
|
|
|
result["name"] = name;
|
|
|
|
result["uuid"] = uuid;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-17 13:17:58 +03:00
|
|
|
QString BrowserService::getCurrentTotp(const QString& uuid)
|
|
|
|
{
|
|
|
|
QList<QSharedPointer<Database>> databases;
|
|
|
|
if (browserSettings()->searchInAllDatabases()) {
|
|
|
|
for (auto dbWidget : getMainWindow()->getOpenDatabases()) {
|
|
|
|
auto db = dbWidget->database();
|
|
|
|
if (db) {
|
|
|
|
databases << db;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
databases << getDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto entryUuid = Tools::hexToUuid(uuid);
|
|
|
|
for (const auto& db : databases) {
|
|
|
|
auto entry = db->rootGroup()->findEntryByUuid(entryUuid, true);
|
|
|
|
if (entry) {
|
|
|
|
return entry->totp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:15:23 +02:00
|
|
|
QString BrowserService::storeKey(const QString& key)
|
|
|
|
{
|
2018-11-22 11:47:31 +01:00
|
|
|
auto db = getDatabase();
|
2018-10-24 17:49:53 +03:00
|
|
|
if (!db) {
|
2018-01-15 00:09:15 +01:00
|
|
|
return {};
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2018-01-15 00:09:15 +01:00
|
|
|
bool contains;
|
2020-05-10 21:20:00 -04:00
|
|
|
auto dialogResult = MessageBox::Cancel;
|
|
|
|
QString id;
|
2017-12-12 10:15:23 +02:00
|
|
|
|
|
|
|
do {
|
2018-01-15 01:15:39 +01:00
|
|
|
QInputDialog keyDialog;
|
2020-05-10 21:20:00 -04:00
|
|
|
connect(m_currentDatabaseWidget, SIGNAL(databaseLocked()), &keyDialog, SLOT(reject()));
|
2018-01-15 01:15:39 +01:00
|
|
|
keyDialog.setWindowTitle(tr("KeePassXC: New key association request"));
|
2019-10-19 14:22:44 +03:00
|
|
|
keyDialog.setLabelText(tr("You have received an association request for the following database:\n%1\n\n"
|
|
|
|
"Give the connection a unique name or ID, for example:\nchrome-laptop.")
|
|
|
|
.arg(db->metadata()->name().toHtmlEscaped()));
|
2018-01-15 01:15:39 +01:00
|
|
|
keyDialog.setOkButtonText(tr("Save and allow access"));
|
2018-02-04 15:43:45 -06:00
|
|
|
keyDialog.setWindowFlags(keyDialog.windowFlags() | Qt::WindowStaysOnTopHint);
|
2018-01-25 14:21:05 +02:00
|
|
|
raiseWindow();
|
2018-01-15 01:15:39 +01:00
|
|
|
keyDialog.show();
|
|
|
|
keyDialog.activateWindow();
|
|
|
|
keyDialog.raise();
|
|
|
|
auto ok = keyDialog.exec();
|
|
|
|
|
|
|
|
id = keyDialog.textValue();
|
|
|
|
|
2019-04-27 13:37:42 +03:00
|
|
|
if (ok != QDialog::Accepted || id.isEmpty() || !isDatabaseOpened()) {
|
2018-01-25 14:21:05 +02:00
|
|
|
hideWindow();
|
2018-01-15 00:09:15 +01:00
|
|
|
return {};
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 11:07:14 +03:00
|
|
|
contains = db->metadata()->customData()->contains(CustomData::BrowserKeyPrefix + id);
|
2018-01-15 00:09:15 +01:00
|
|
|
if (contains) {
|
2018-12-19 20:14:11 -08:00
|
|
|
dialogResult = MessageBox::warning(nullptr,
|
|
|
|
tr("KeePassXC: Overwrite existing key?"),
|
|
|
|
tr("A shared encryption key with the name \"%1\" "
|
|
|
|
"already exists.\nDo you want to overwrite it?")
|
|
|
|
.arg(id),
|
|
|
|
MessageBox::Overwrite | MessageBox::Cancel,
|
|
|
|
MessageBox::Cancel);
|
2018-01-15 00:09:15 +01:00
|
|
|
}
|
2018-12-19 20:14:11 -08:00
|
|
|
} while (contains && dialogResult == MessageBox::Cancel);
|
2017-12-12 10:15:23 +02:00
|
|
|
|
2018-01-25 14:21:05 +02:00
|
|
|
hideWindow();
|
2020-05-01 11:07:14 +03:00
|
|
|
db->metadata()->customData()->set(CustomData::BrowserKeyPrefix + id, key);
|
2020-03-02 10:18:33 +02:00
|
|
|
db->metadata()->customData()->set(QString("%1_%2").arg(CustomData::Created, id),
|
|
|
|
Clock::currentDateTime().toString(Qt::SystemLocaleShortDate));
|
2017-12-12 10:15:23 +02:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BrowserService::getKey(const QString& id)
|
|
|
|
{
|
2018-11-22 11:47:31 +01:00
|
|
|
auto db = getDatabase();
|
2018-10-24 17:49:53 +03:00
|
|
|
if (!db) {
|
|
|
|
return {};
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 11:07:14 +03:00
|
|
|
return db->metadata()->customData()->value(CustomData::BrowserKeyPrefix + id);
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
QJsonArray BrowserService::findMatchingEntries(const QString& dbid,
|
2020-09-13 17:38:19 +03:00
|
|
|
const QString& siteUrlStr,
|
|
|
|
const QString& formUrlStr,
|
2018-05-07 23:22:59 -04:00
|
|
|
const QString& realm,
|
2018-12-08 12:12:52 +02:00
|
|
|
const StringPairList& keyList,
|
|
|
|
const bool httpAuth)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
2020-05-10 21:20:00 -04:00
|
|
|
Q_UNUSED(dbid);
|
2018-09-05 16:21:59 -04:00
|
|
|
const bool alwaysAllowAccess = browserSettings()->alwaysAllowAccess();
|
2018-12-08 12:12:52 +02:00
|
|
|
const bool ignoreHttpAuth = browserSettings()->httpAuthPermission();
|
2020-09-13 17:38:19 +03:00
|
|
|
const QString siteHost = QUrl(siteUrlStr).host();
|
|
|
|
const QString formHost = QUrl(formUrlStr).host();
|
2017-12-12 10:15:23 +02:00
|
|
|
|
|
|
|
// Check entries for authorization
|
|
|
|
QList<Entry*> pwEntriesToConfirm;
|
|
|
|
QList<Entry*> pwEntries;
|
2020-09-13 17:38:19 +03:00
|
|
|
for (auto* entry : searchEntries(siteUrlStr, formUrlStr, keyList)) {
|
2019-08-15 12:35:11 +03:00
|
|
|
if (entry->customData()->contains(BrowserService::OPTION_HIDE_ENTRY)
|
2019-11-24 18:10:40 +02:00
|
|
|
&& entry->customData()->value(BrowserService::OPTION_HIDE_ENTRY) == TRUE_STR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!httpAuth && entry->customData()->contains(BrowserService::OPTION_ONLY_HTTP_AUTH)
|
|
|
|
&& entry->customData()->value(BrowserService::OPTION_ONLY_HTTP_AUTH) == TRUE_STR) {
|
2019-08-15 12:35:11 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-08 12:12:52 +02:00
|
|
|
// HTTP Basic Auth always needs a confirmation
|
|
|
|
if (!ignoreHttpAuth && httpAuth) {
|
|
|
|
pwEntriesToConfirm.append(entry);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
switch (checkAccess(entry, siteHost, formHost, realm)) {
|
2017-12-12 10:15:23 +02:00
|
|
|
case Denied:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case Unknown:
|
|
|
|
if (alwaysAllowAccess) {
|
|
|
|
pwEntries.append(entry);
|
|
|
|
} else {
|
|
|
|
pwEntriesToConfirm.append(entry);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Allowed:
|
|
|
|
pwEntries.append(entry);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm entries
|
2020-09-13 17:38:19 +03:00
|
|
|
QList<Entry*> selectedEntriesToConfirm =
|
|
|
|
confirmEntries(pwEntriesToConfirm, siteUrlStr, siteHost, formHost, realm, httpAuth);
|
2020-01-05 12:07:18 -05:00
|
|
|
if (!selectedEntriesToConfirm.isEmpty()) {
|
|
|
|
pwEntries.append(selectedEntriesToConfirm);
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pwEntries.isEmpty()) {
|
2020-05-10 21:20:00 -04:00
|
|
|
return {};
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2019-04-27 13:37:42 +03:00
|
|
|
// Ensure that database is not locked when the popup was visible
|
|
|
|
if (!isDatabaseOpened()) {
|
2020-05-10 21:20:00 -04:00
|
|
|
return {};
|
2019-04-27 13:37:42 +03:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:15:23 +02:00
|
|
|
// Sort results
|
2020-09-13 17:38:19 +03:00
|
|
|
pwEntries = sortEntries(pwEntries, siteUrlStr, formUrlStr);
|
2017-12-12 10:15:23 +02:00
|
|
|
|
|
|
|
// Fill the list
|
2020-05-10 21:20:00 -04:00
|
|
|
QJsonArray result;
|
2019-08-15 12:35:11 +03:00
|
|
|
for (auto* entry : pwEntries) {
|
2019-05-19 15:58:52 -04:00
|
|
|
result.append(prepareEntry(entry));
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
void BrowserService::addEntry(const QString& dbid,
|
2019-01-20 09:50:20 -05:00
|
|
|
const QString& login,
|
|
|
|
const QString& password,
|
2020-09-13 17:38:19 +03:00
|
|
|
const QString& siteUrlStr,
|
|
|
|
const QString& formUrlStr,
|
2019-01-20 09:50:20 -05:00
|
|
|
const QString& realm,
|
2019-01-30 16:48:22 +02:00
|
|
|
const QString& group,
|
|
|
|
const QString& groupUuid,
|
2019-01-17 06:39:53 +01:00
|
|
|
const QSharedPointer<Database>& selectedDb)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
2020-05-10 21:20:00 -04:00
|
|
|
// TODO: select database based on this key id
|
|
|
|
Q_UNUSED(dbid);
|
2018-11-22 11:47:31 +01:00
|
|
|
auto db = selectedDb ? selectedDb : selectedDatabase();
|
2018-10-19 21:44:08 +03:00
|
|
|
if (!db) {
|
2019-06-18 18:23:12 -04:00
|
|
|
return;
|
2018-10-19 21:44:08 +03:00
|
|
|
}
|
|
|
|
|
2018-11-22 11:47:31 +01:00
|
|
|
auto* entry = new Entry();
|
2018-03-23 11:18:06 +01:00
|
|
|
entry->setUuid(QUuid::createUuid());
|
2020-09-13 17:38:19 +03:00
|
|
|
entry->setTitle(QUrl(siteUrlStr).host());
|
|
|
|
entry->setUrl(siteUrlStr);
|
2017-12-12 10:15:23 +02:00
|
|
|
entry->setIcon(KEEPASSXCBROWSER_DEFAULT_ICON);
|
|
|
|
entry->setUsername(login);
|
|
|
|
entry->setPassword(password);
|
2019-01-30 16:48:22 +02:00
|
|
|
|
|
|
|
// Select a group for the entry
|
|
|
|
if (!group.isEmpty()) {
|
|
|
|
if (db->rootGroup()) {
|
|
|
|
auto selectedGroup = db->rootGroup()->findGroupByUuid(Tools::hexToUuid(groupUuid));
|
2019-05-07 10:51:24 +03:00
|
|
|
if (selectedGroup) {
|
2019-01-30 16:48:22 +02:00
|
|
|
entry->setGroup(selectedGroup);
|
2019-05-07 10:51:24 +03:00
|
|
|
} else {
|
|
|
|
entry->setGroup(getDefaultEntryGroup(db));
|
2019-01-30 16:48:22 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-07 10:51:24 +03:00
|
|
|
} else {
|
|
|
|
entry->setGroup(getDefaultEntryGroup(db));
|
2019-01-30 16:48:22 +02:00
|
|
|
}
|
2017-12-12 10:15:23 +02:00
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
const QString host = QUrl(siteUrlStr).host();
|
|
|
|
const QString submitHost = QUrl(formUrlStr).host();
|
2017-12-12 10:15:23 +02:00
|
|
|
BrowserEntryConfig config;
|
|
|
|
config.allow(host);
|
|
|
|
|
|
|
|
if (!submitHost.isEmpty()) {
|
|
|
|
config.allow(submitHost);
|
|
|
|
}
|
|
|
|
if (!realm.isEmpty()) {
|
|
|
|
config.setRealm(realm);
|
|
|
|
}
|
|
|
|
config.save(entry);
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
bool BrowserService::updateEntry(const QString& dbid,
|
|
|
|
const QString& uuid,
|
|
|
|
const QString& login,
|
|
|
|
const QString& password,
|
2020-09-13 17:38:19 +03:00
|
|
|
const QString& siteUrlStr,
|
|
|
|
const QString& formUrlStr)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
2020-05-10 21:20:00 -04:00
|
|
|
// TODO: select database based on this key id
|
|
|
|
Q_UNUSED(dbid);
|
2018-11-22 11:47:31 +01:00
|
|
|
auto db = selectedDatabase();
|
2017-12-12 10:15:23 +02:00
|
|
|
if (!db) {
|
2020-05-10 21:20:00 -04:00
|
|
|
return false;
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2019-01-30 16:48:22 +02:00
|
|
|
Entry* entry = db->rootGroup()->findEntryByUuid(Tools::hexToUuid(uuid));
|
2017-12-12 10:15:23 +02:00
|
|
|
if (!entry) {
|
2018-10-19 21:44:08 +03:00
|
|
|
// If entry is not found for update, add a new one to the selected database
|
2020-09-13 17:38:19 +03:00
|
|
|
addEntry(dbid, login, password, siteUrlStr, formUrlStr, "", "", "", db);
|
2020-05-10 21:20:00 -04:00
|
|
|
return true;
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 21:24:57 +02:00
|
|
|
// Check if the entry password is a reference. If so, update the original entry instead
|
|
|
|
while (entry->attributes()->isReference(EntryAttributes::PasswordKey)) {
|
|
|
|
const QUuid referenceUuid = entry->attributes()->referenceUuid(EntryAttributes::PasswordKey);
|
|
|
|
if (!referenceUuid.isNull()) {
|
|
|
|
entry = db->rootGroup()->findEntryByUuid(referenceUuid);
|
|
|
|
if (!entry) {
|
2020-05-10 21:20:00 -04:00
|
|
|
return false;
|
2019-01-21 21:24:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:15:23 +02:00
|
|
|
QString username = entry->username();
|
|
|
|
if (username.isEmpty()) {
|
2020-05-10 21:20:00 -04:00
|
|
|
return false;
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
bool result = false;
|
2018-11-01 04:27:38 +01:00
|
|
|
if (username.compare(login, Qt::CaseSensitive) != 0
|
|
|
|
|| entry->password().compare(password, Qt::CaseSensitive) != 0) {
|
2018-12-19 20:14:11 -08:00
|
|
|
MessageBox::Button dialogResult = MessageBox::No;
|
2018-09-05 16:21:59 -04:00
|
|
|
if (!browserSettings()->alwaysAllowUpdate()) {
|
2018-01-25 14:21:05 +02:00
|
|
|
raiseWindow();
|
2019-01-20 09:50:20 -05:00
|
|
|
dialogResult = MessageBox::question(
|
|
|
|
nullptr,
|
|
|
|
tr("KeePassXC: Update Entry"),
|
2020-09-13 17:38:19 +03:00
|
|
|
tr("Do you want to update the information in %1 - %2?").arg(QUrl(siteUrlStr).host(), username),
|
2019-01-20 09:50:20 -05:00
|
|
|
MessageBox::Save | MessageBox::Cancel,
|
|
|
|
MessageBox::Cancel,
|
|
|
|
MessageBox::Raise);
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:14:11 -08:00
|
|
|
if (browserSettings()->alwaysAllowUpdate() || dialogResult == MessageBox::Save) {
|
2017-12-12 10:15:23 +02:00
|
|
|
entry->beginUpdate();
|
2019-01-21 21:24:57 +02:00
|
|
|
if (!entry->attributes()->isReference(EntryAttributes::UserNameKey)) {
|
|
|
|
entry->setUsername(login);
|
|
|
|
}
|
2017-12-12 10:15:23 +02:00
|
|
|
entry->setPassword(password);
|
|
|
|
entry->endUpdate();
|
2020-05-10 21:20:00 -04:00
|
|
|
result = true;
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
2018-01-25 14:21:05 +02:00
|
|
|
|
|
|
|
hideWindow();
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
2019-06-13 10:05:29 +03:00
|
|
|
|
|
|
|
return result;
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 05:39:43 +01:00
|
|
|
QList<Entry*>
|
2020-09-13 17:38:19 +03:00
|
|
|
BrowserService::searchEntries(const QSharedPointer<Database>& db, const QString& siteUrlStr, const QString& formUrlStr)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
|
|
|
QList<Entry*> entries;
|
2018-11-22 11:47:31 +01:00
|
|
|
auto* rootGroup = db->rootGroup();
|
2017-12-12 10:15:23 +02:00
|
|
|
if (!rootGroup) {
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2019-08-15 12:35:11 +03:00
|
|
|
for (const auto& group : rootGroup->groupsRecursive(true)) {
|
|
|
|
if (group->isRecycled() || !group->resolveSearchingEnabled()) {
|
2018-07-11 11:53:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
2017-12-12 10:15:23 +02:00
|
|
|
|
2019-08-15 12:35:11 +03:00
|
|
|
for (auto* entry : group->entries()) {
|
|
|
|
if (entry->isRecycled()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for additional URL's starting with KP2A_URL
|
2019-12-17 14:30:39 +02:00
|
|
|
for (const auto& key : entry->attributes()->keys()) {
|
2020-09-13 17:38:19 +03:00
|
|
|
if (key.startsWith(ADDITIONAL_URL) && handleURL(entry->attributes()->value(key), siteUrlStr, formUrlStr)
|
2020-02-04 17:40:44 +02:00
|
|
|
&& !entries.contains(entry)) {
|
2019-12-17 14:30:39 +02:00
|
|
|
entries.append(entry);
|
|
|
|
continue;
|
2019-08-15 12:35:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 12:05:33 -04:00
|
|
|
if (!handleEntry(entry, siteUrlStr, formUrlStr)) {
|
2019-08-15 12:35:11 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
// Additional URL check may have already inserted the entry to the list
|
|
|
|
if (!entries.contains(entry)) {
|
|
|
|
entries.append(entry);
|
|
|
|
}
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
QList<Entry*>
|
|
|
|
BrowserService::searchEntries(const QString& siteUrlStr, const QString& formUrlStr, const StringPairList& keyList)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
2019-08-27 21:50:54 +03:00
|
|
|
// Check if database is connected with KeePassXC-Browser
|
|
|
|
auto databaseConnected = [&](const QSharedPointer<Database>& db) {
|
|
|
|
for (const StringPair& keyPair : keyList) {
|
2020-05-01 11:07:14 +03:00
|
|
|
QString key = db->metadata()->customData()->value(CustomData::BrowserKeyPrefix + keyPair.first);
|
2019-08-27 21:50:54 +03:00
|
|
|
if (!key.isEmpty() && keyPair.second == key) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2017-12-12 10:15:23 +02:00
|
|
|
// Get the list of databases to search
|
2018-11-22 11:47:31 +01:00
|
|
|
QList<QSharedPointer<Database>> databases;
|
2018-09-05 16:21:59 -04:00
|
|
|
if (browserSettings()->searchInAllDatabases()) {
|
2020-05-10 21:20:00 -04:00
|
|
|
for (auto dbWidget : getMainWindow()->getOpenDatabases()) {
|
|
|
|
auto db = dbWidget->database();
|
|
|
|
if (db && databaseConnected(dbWidget->database())) {
|
|
|
|
databases << db;
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-10 21:20:00 -04:00
|
|
|
} else {
|
|
|
|
const auto& db = getDatabase();
|
2019-08-27 21:50:54 +03:00
|
|
|
if (databaseConnected(db)) {
|
|
|
|
databases << db;
|
|
|
|
}
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search entries matching the hostname
|
2020-09-13 17:38:19 +03:00
|
|
|
QString hostname = QUrl(siteUrlStr).host();
|
2017-12-12 10:15:23 +02:00
|
|
|
QList<Entry*> entries;
|
|
|
|
do {
|
2018-11-22 11:47:31 +01:00
|
|
|
for (const auto& db : databases) {
|
2020-09-13 17:38:19 +03:00
|
|
|
entries << searchEntries(db, siteUrlStr, formUrlStr);
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
} while (entries.isEmpty() && removeFirstDomain(hostname));
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
void BrowserService::convertAttributesToCustomData(QSharedPointer<Database> db)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
|
|
|
if (!db) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<Entry*> entries = db->rootGroup()->entriesRecursive();
|
2018-10-24 17:49:53 +03:00
|
|
|
QProgressDialog progress(tr("Converting attributes to custom data…"), tr("Abort"), 0, entries.count());
|
2017-12-12 10:15:23 +02:00
|
|
|
progress.setWindowModality(Qt::WindowModal);
|
|
|
|
|
2018-10-24 17:49:53 +03:00
|
|
|
int counter = 0;
|
|
|
|
int keyCounter = 0;
|
2019-08-15 12:35:11 +03:00
|
|
|
for (auto* entry : entries) {
|
2017-12-12 10:15:23 +02:00
|
|
|
if (progress.wasCanceled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:49:53 +03:00
|
|
|
if (moveSettingsToCustomData(entry, KEEPASSHTTP_NAME)) {
|
2017-12-12 10:15:23 +02:00
|
|
|
++counter;
|
|
|
|
}
|
2018-11-01 13:07:51 +02:00
|
|
|
|
|
|
|
if (moveSettingsToCustomData(entry, KEEPASSXCBROWSER_OLD_NAME)) {
|
|
|
|
++counter;
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:49:53 +03:00
|
|
|
if (moveSettingsToCustomData(entry, KEEPASSXCBROWSER_NAME)) {
|
|
|
|
++counter;
|
|
|
|
}
|
|
|
|
|
2018-11-01 13:07:51 +02:00
|
|
|
if (entry->title() == KEEPASSHTTP_NAME || entry->title().contains(KEEPASSXCBROWSER_NAME, Qt::CaseInsensitive)) {
|
2018-10-24 17:49:53 +03:00
|
|
|
keyCounter += moveKeysToCustomData(entry, db);
|
2020-04-12 10:22:07 +03:00
|
|
|
db->recycleEntry(entry);
|
2018-10-24 17:49:53 +03:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:15:23 +02:00
|
|
|
progress.setValue(progress.value() + 1);
|
|
|
|
}
|
|
|
|
progress.reset();
|
|
|
|
|
|
|
|
if (counter > 0) {
|
2018-12-19 20:14:11 -08:00
|
|
|
MessageBox::information(nullptr,
|
|
|
|
tr("KeePassXC: Converted KeePassHTTP attributes"),
|
|
|
|
tr("Successfully converted attributes from %1 entry(s).\n"
|
|
|
|
"Moved %2 keys to custom data.",
|
|
|
|
"")
|
|
|
|
.arg(counter)
|
|
|
|
.arg(keyCounter),
|
|
|
|
MessageBox::Ok);
|
2018-10-24 17:49:53 +03:00
|
|
|
} else if (counter == 0 && keyCounter > 0) {
|
2018-12-19 20:14:11 -08:00
|
|
|
MessageBox::information(nullptr,
|
|
|
|
tr("KeePassXC: Converted KeePassHTTP attributes"),
|
|
|
|
tr("Successfully moved %n keys to custom data.", "", keyCounter),
|
|
|
|
MessageBox::Ok);
|
2017-12-12 10:15:23 +02:00
|
|
|
} else {
|
2018-12-19 20:14:11 -08:00
|
|
|
MessageBox::information(nullptr,
|
|
|
|
tr("KeePassXC: No entry with KeePassHTTP attributes found!"),
|
|
|
|
tr("The active database does not contain an entry with KeePassHTTP attributes."),
|
|
|
|
MessageBox::Ok);
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
2018-10-24 17:49:53 +03:00
|
|
|
|
|
|
|
// Rename password groupName
|
|
|
|
Group* rootGroup = db->rootGroup();
|
|
|
|
if (!rootGroup) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-15 12:35:11 +03:00
|
|
|
for (auto* g : rootGroup->groupsRecursive(true)) {
|
|
|
|
if (g->name() == KEEPASSHTTP_GROUP_NAME) {
|
|
|
|
g->setName(KEEPASSXCBROWSER_GROUP_NAME);
|
2018-10-24 17:49:53 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
QList<Entry*>
|
|
|
|
BrowserService::sortEntries(QList<Entry*>& pwEntries, const QString& siteUrlStr, const QString& formUrlStr)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
2018-05-06 00:51:03 +03:00
|
|
|
// Build map of prioritized entries
|
|
|
|
QMultiMap<int, Entry*> priorities;
|
2019-08-15 12:35:11 +03:00
|
|
|
for (auto* entry : pwEntries) {
|
2020-09-13 17:38:19 +03:00
|
|
|
priorities.insert(sortPriority(getEntryURLs(entry), siteUrlStr, formUrlStr), entry);
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
auto keys = priorities.uniqueKeys();
|
|
|
|
std::sort(keys.begin(), keys.end(), [](int l, int r) { return l > r; });
|
|
|
|
|
2018-05-06 00:51:03 +03:00
|
|
|
QList<Entry*> results;
|
2020-09-13 17:38:19 +03:00
|
|
|
auto sortField = browserSettings()->sortByTitle() ? EntryAttributes::TitleKey : EntryAttributes::UserNameKey;
|
|
|
|
for (auto key : keys) {
|
|
|
|
// Sort same priority entries by Title or UserName
|
|
|
|
auto entries = priorities.values(key);
|
|
|
|
std::sort(entries.begin(), entries.end(), [&sortField](Entry* left, Entry* right) {
|
|
|
|
return QString::localeAwareCompare(left->attribute(sortField), right->attribute(sortField));
|
|
|
|
});
|
|
|
|
results << entries;
|
|
|
|
if (browserSettings()->bestMatchOnly() && !results.isEmpty()) {
|
|
|
|
// Early out once we find the highest batch of matches
|
|
|
|
break;
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
2018-05-06 00:51:03 +03:00
|
|
|
}
|
2017-12-12 10:15:23 +02:00
|
|
|
|
2018-05-06 00:51:03 +03:00
|
|
|
return results;
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2020-01-05 12:07:18 -05:00
|
|
|
QList<Entry*> BrowserService::confirmEntries(QList<Entry*>& pwEntriesToConfirm,
|
2020-09-13 17:38:19 +03:00
|
|
|
const QString& siteUrlStr,
|
|
|
|
const QString& siteHost,
|
|
|
|
const QString& formUrlStr,
|
2020-01-05 12:07:18 -05:00
|
|
|
const QString& realm,
|
|
|
|
const bool httpAuth)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
|
|
|
if (pwEntriesToConfirm.isEmpty() || m_dialogActive) {
|
2020-01-05 12:07:18 -05:00
|
|
|
return {};
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_dialogActive = true;
|
2020-07-05 14:32:23 +03:00
|
|
|
updateWindowState();
|
2017-12-12 10:15:23 +02:00
|
|
|
BrowserAccessControlDialog accessControlDialog;
|
2020-01-05 12:07:18 -05:00
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
connect(m_currentDatabaseWidget, SIGNAL(databaseLocked()), &accessControlDialog, SLOT(reject()));
|
2020-04-24 18:38:33 +03:00
|
|
|
|
2020-01-05 12:07:18 -05:00
|
|
|
connect(&accessControlDialog, &BrowserAccessControlDialog::disableAccess, [&](QTableWidgetItem* item) {
|
|
|
|
auto entry = pwEntriesToConfirm[item->row()];
|
|
|
|
BrowserEntryConfig config;
|
|
|
|
config.load(entry);
|
2020-09-13 17:38:19 +03:00
|
|
|
config.deny(siteHost);
|
|
|
|
if (!formUrlStr.isEmpty() && siteHost != formUrlStr) {
|
|
|
|
config.deny(formUrlStr);
|
2020-01-05 12:07:18 -05:00
|
|
|
}
|
|
|
|
if (!realm.isEmpty()) {
|
|
|
|
config.setRealm(realm);
|
|
|
|
}
|
|
|
|
config.save(entry);
|
|
|
|
});
|
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
accessControlDialog.setItems(pwEntriesToConfirm, siteUrlStr, httpAuth);
|
2017-12-12 10:15:23 +02:00
|
|
|
|
2020-01-05 12:07:18 -05:00
|
|
|
QList<Entry*> allowedEntries;
|
|
|
|
if (accessControlDialog.exec() == QDialog::Accepted) {
|
|
|
|
const auto selectedEntries = accessControlDialog.getSelectedEntries();
|
|
|
|
for (auto item : accessControlDialog.getSelectedEntries()) {
|
|
|
|
auto entry = pwEntriesToConfirm[item->row()];
|
|
|
|
if (accessControlDialog.remember()) {
|
|
|
|
BrowserEntryConfig config;
|
|
|
|
config.load(entry);
|
2020-09-13 17:38:19 +03:00
|
|
|
config.allow(siteHost);
|
|
|
|
if (!formUrlStr.isEmpty() && siteHost != formUrlStr) {
|
|
|
|
config.allow(formUrlStr);
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
2020-01-05 12:07:18 -05:00
|
|
|
if (!realm.isEmpty()) {
|
|
|
|
config.setRealm(realm);
|
|
|
|
}
|
|
|
|
config.save(entry);
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
2020-01-05 12:07:18 -05:00
|
|
|
allowedEntries.append(entry);
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 18:38:33 +03:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
// Re-hide the application if it wasn't visible before
|
|
|
|
// only affects macOS because dialogs force the main window to show
|
2020-07-05 14:32:23 +03:00
|
|
|
hideWindow();
|
2020-04-24 18:38:33 +03:00
|
|
|
#endif
|
|
|
|
|
2017-12-12 10:15:23 +02:00
|
|
|
m_dialogActive = false;
|
2020-04-24 18:38:33 +03:00
|
|
|
|
2020-01-05 12:07:18 -05:00
|
|
|
return allowedEntries;
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject BrowserService::prepareEntry(const Entry* entry)
|
|
|
|
{
|
|
|
|
QJsonObject res;
|
|
|
|
res["login"] = entry->resolveMultiplePlaceholders(entry->username());
|
|
|
|
res["password"] = entry->resolveMultiplePlaceholders(entry->password());
|
|
|
|
res["name"] = entry->resolveMultiplePlaceholders(entry->title());
|
2018-09-30 08:45:06 -04:00
|
|
|
res["uuid"] = entry->resolveMultiplePlaceholders(entry->uuidToHex());
|
2020-01-03 22:58:17 +01:00
|
|
|
res["group"] = entry->resolveMultiplePlaceholders(entry->group()->name());
|
2017-12-12 10:15:23 +02:00
|
|
|
|
2018-04-16 11:41:45 +03:00
|
|
|
if (entry->hasTotp()) {
|
|
|
|
res["totp"] = entry->totp();
|
|
|
|
}
|
|
|
|
|
2019-05-09 08:46:16 +03:00
|
|
|
if (entry->isExpired()) {
|
2019-11-24 18:10:40 +02:00
|
|
|
res["expired"] = TRUE_STR;
|
2019-05-09 08:46:16 +03:00
|
|
|
}
|
|
|
|
|
2019-08-15 12:35:11 +03:00
|
|
|
if (entry->customData()->contains(BrowserService::OPTION_SKIP_AUTO_SUBMIT)) {
|
|
|
|
res["skipAutoSubmit"] = entry->customData()->value(BrowserService::OPTION_SKIP_AUTO_SUBMIT);
|
|
|
|
}
|
|
|
|
|
2018-09-05 16:21:59 -04:00
|
|
|
if (browserSettings()->supportKphFields()) {
|
2017-12-12 10:15:23 +02:00
|
|
|
const EntryAttributes* attr = entry->attributes();
|
|
|
|
QJsonArray stringFields;
|
2019-08-15 12:35:11 +03:00
|
|
|
for (const auto& key : attr->keys()) {
|
|
|
|
if (key.startsWith("KPH: ")) {
|
2017-12-12 10:15:23 +02:00
|
|
|
QJsonObject sField;
|
|
|
|
sField[key] = entry->resolveMultiplePlaceholders(attr->value(key));
|
2019-05-19 15:58:52 -04:00
|
|
|
stringFields.append(sField);
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
res["stringFields"] = stringFields;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-03-31 16:01:30 -04:00
|
|
|
BrowserService::Access
|
2020-09-13 17:38:19 +03:00
|
|
|
BrowserService::checkAccess(const Entry* entry, const QString& siteHost, const QString& formHost, const QString& realm)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
2020-06-19 18:46:49 -04:00
|
|
|
if (entry->isExpired()) {
|
|
|
|
return browserSettings()->allowExpiredCredentials() ? Allowed : Denied;
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:15:23 +02:00
|
|
|
BrowserEntryConfig config;
|
|
|
|
if (!config.load(entry)) {
|
|
|
|
return Unknown;
|
|
|
|
}
|
2020-09-13 17:38:19 +03:00
|
|
|
if ((config.isAllowed(siteHost)) && (formHost.isEmpty() || config.isAllowed(formHost))) {
|
2017-12-12 10:15:23 +02:00
|
|
|
return Allowed;
|
|
|
|
}
|
2020-09-13 17:38:19 +03:00
|
|
|
if ((config.isDenied(siteHost)) || (!formHost.isEmpty() && config.isDenied(formHost))) {
|
2017-12-12 10:15:23 +02:00
|
|
|
return Denied;
|
|
|
|
}
|
|
|
|
if (!realm.isEmpty() && config.realm() != realm) {
|
|
|
|
return Denied;
|
|
|
|
}
|
|
|
|
return Unknown;
|
|
|
|
}
|
|
|
|
|
2019-05-07 10:51:24 +03:00
|
|
|
Group* BrowserService::getDefaultEntryGroup(const QSharedPointer<Database>& selectedDb)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
2018-11-22 11:47:31 +01:00
|
|
|
auto db = selectedDb ? selectedDb : getDatabase();
|
2017-12-12 10:15:23 +02:00
|
|
|
if (!db) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-30 16:48:22 +02:00
|
|
|
auto* rootGroup = db->rootGroup();
|
2017-12-12 10:15:23 +02:00
|
|
|
if (!rootGroup) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-30 16:48:22 +02:00
|
|
|
for (auto* g : rootGroup->groupsRecursive(true)) {
|
2019-08-15 12:35:11 +03:00
|
|
|
if (g->name() == KEEPASSXCBROWSER_GROUP_NAME && !g->isRecycled()) {
|
2018-11-22 11:47:31 +01:00
|
|
|
return db->rootGroup()->findGroupByUuid(g->uuid());
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 11:47:31 +01:00
|
|
|
auto* group = new Group();
|
2018-03-23 11:18:06 +01:00
|
|
|
group->setUuid(QUuid::createUuid());
|
2019-08-15 12:35:11 +03:00
|
|
|
group->setName(KEEPASSXCBROWSER_GROUP_NAME);
|
2017-12-12 10:15:23 +02:00
|
|
|
group->setIcon(KEEPASSXCBROWSER_DEFAULT_ICON);
|
|
|
|
group->setParent(rootGroup);
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
// Returns the maximum sort priority given a set of match urls and the
|
|
|
|
// extension provided site and form url.
|
|
|
|
int BrowserService::sortPriority(const QStringList& urls, const QString& siteUrlStr, const QString& formUrlStr)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
2020-09-13 17:38:19 +03:00
|
|
|
QList<int> priorityList;
|
|
|
|
// NOTE: QUrl::matches is utterly broken in Qt < 5.11, so we work around that
|
|
|
|
// by removing parts of the url that we don't match and direct matching others
|
|
|
|
const auto stdOpts = QUrl::RemoveFragment | QUrl::RemoveUserInfo;
|
|
|
|
const auto siteUrl = QUrl(siteUrlStr).adjusted(stdOpts);
|
|
|
|
const auto formUrl = QUrl(formUrlStr).adjusted(stdOpts);
|
|
|
|
|
|
|
|
auto getPriority = [&](const QString& givenUrl) {
|
|
|
|
auto url = QUrl::fromUserInput(givenUrl).adjusted(stdOpts);
|
|
|
|
|
|
|
|
// Default to https scheme if undefined
|
|
|
|
if (url.scheme().isEmpty() || !givenUrl.contains("://")) {
|
|
|
|
url.setScheme("https");
|
|
|
|
}
|
2020-01-11 12:50:21 +02:00
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
// Add the empty path to the URL if it's missing.
|
|
|
|
// URL's from the extension always have a path set, entry URL's can be without.
|
|
|
|
if (url.path().isEmpty() && !url.hasFragment() && !url.hasQuery()) {
|
|
|
|
url.setPath("/");
|
|
|
|
}
|
2020-01-11 12:50:21 +02:00
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
// Reject invalid urls and hosts, except 'localhost', and scheme mismatch
|
|
|
|
if (!url.isValid() || (!url.host().contains(".") && url.host() != "localhost")
|
|
|
|
|| url.scheme() != siteUrl.scheme()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-12 10:15:23 +02:00
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
// Exact match with site url or form url
|
|
|
|
if (url.matches(siteUrl, QUrl::None) || url.matches(formUrl, QUrl::None)) {
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exact match without the query string
|
|
|
|
if (url.matches(siteUrl, QUrl::RemoveQuery) || url.matches(formUrl, QUrl::RemoveQuery)) {
|
|
|
|
return 90;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match without path (ie, FQDN match), form url prioritizes lower than site url
|
|
|
|
if (url.host() == siteUrl.host()) {
|
|
|
|
return 80;
|
|
|
|
}
|
|
|
|
if (url.host() == formUrl.host()) {
|
|
|
|
return 70;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Site/form url ends with given url (subdomain mismatch)
|
|
|
|
if (siteUrl.host().endsWith(url.host())) {
|
|
|
|
return 60;
|
|
|
|
}
|
|
|
|
if (formUrl.host().endsWith(url.host())) {
|
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No valid match found
|
2019-11-01 20:13:12 +02:00
|
|
|
return 0;
|
2020-09-13 17:38:19 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& entryUrl : urls) {
|
|
|
|
priorityList << getPriority(entryUrl);
|
2019-11-01 20:13:12 +02:00
|
|
|
}
|
2020-09-13 17:38:19 +03:00
|
|
|
|
|
|
|
return *std::max_element(priorityList.begin(), priorityList.end());
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2019-11-01 20:13:12 +02:00
|
|
|
bool BrowserService::schemeFound(const QString& url)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
|
|
|
QUrl address(url);
|
|
|
|
return !address.scheme().isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BrowserService::removeFirstDomain(QString& hostname)
|
|
|
|
{
|
|
|
|
int pos = hostname.indexOf(".");
|
|
|
|
if (pos < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't remove the second-level domain if it's the only one
|
|
|
|
if (hostname.count(".") > 1) {
|
|
|
|
hostname = hostname.mid(pos + 1);
|
|
|
|
return !hostname.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing removed
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-20 14:53:42 +02:00
|
|
|
/* Test if a search URL matches a custom entry. If the URL has the schema "keepassxc", some special checks will be made.
|
|
|
|
* Otherwise, this simply delegates to handleURL(). */
|
|
|
|
bool BrowserService::handleEntry(Entry* entry, const QString& url, const QString& submitUrl)
|
|
|
|
{
|
|
|
|
// Use this special scheme to find entries by UUID
|
|
|
|
if (url.startsWith("keepassxc://")) {
|
|
|
|
return ("keepassxc://by-uuid/" + entry->uuidToHex()) == url;
|
|
|
|
}
|
|
|
|
return handleURL(entry->url(), url, submitUrl);
|
|
|
|
}
|
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
bool BrowserService::handleURL(const QString& entryUrl, const QString& siteUrlStr, const QString& formUrlStr)
|
2019-08-15 12:35:11 +03:00
|
|
|
{
|
2019-11-01 20:13:12 +02:00
|
|
|
if (entryUrl.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl entryQUrl;
|
|
|
|
if (entryUrl.contains("://")) {
|
|
|
|
entryQUrl = entryUrl;
|
|
|
|
} else {
|
|
|
|
entryQUrl = QUrl::fromUserInput(entryUrl);
|
|
|
|
|
|
|
|
if (browserSettings()->matchUrlScheme()) {
|
|
|
|
entryQUrl.setScheme("https");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 22:38:20 +02:00
|
|
|
// Make a direct compare if a local file is used
|
2020-09-27 12:05:33 -04:00
|
|
|
if (siteUrlStr.startsWith("file://")) {
|
2020-09-13 17:38:19 +03:00
|
|
|
return entryUrl == formUrlStr;
|
2019-11-12 22:38:20 +02:00
|
|
|
}
|
|
|
|
|
2019-11-01 20:13:12 +02:00
|
|
|
// URL host validation fails
|
2019-11-12 22:38:20 +02:00
|
|
|
if (entryQUrl.host().isEmpty()) {
|
2019-11-01 20:13:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match port, if used
|
2020-09-13 17:38:19 +03:00
|
|
|
QUrl siteQUrl(siteUrlStr);
|
2019-11-12 22:38:20 +02:00
|
|
|
if (entryQUrl.port() > 0 && entryQUrl.port() != siteQUrl.port()) {
|
2019-11-01 20:13:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-15 12:35:11 +03:00
|
|
|
|
2019-11-01 20:13:12 +02:00
|
|
|
// Match scheme
|
2019-11-18 06:57:04 +00:00
|
|
|
if (browserSettings()->matchUrlScheme() && !entryQUrl.scheme().isEmpty()
|
|
|
|
&& entryQUrl.scheme().compare(siteQUrl.scheme()) != 0) {
|
2019-11-01 20:13:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for illegal characters
|
|
|
|
QRegularExpression re("[<>\\^`{|}]");
|
2019-11-12 22:38:20 +02:00
|
|
|
if (re.match(entryUrl).hasMatch()) {
|
2019-08-15 12:35:11 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-14 10:05:24 +02:00
|
|
|
// Match the base domain
|
|
|
|
if (baseDomain(siteQUrl.host()) != baseDomain(entryQUrl.host())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match the subdomains with the limited wildcard
|
|
|
|
if (siteQUrl.host().endsWith(entryQUrl.host())) {
|
2020-09-13 17:38:19 +03:00
|
|
|
return true;
|
2019-08-15 12:35:11 +03:00
|
|
|
}
|
2019-11-01 20:13:12 +02:00
|
|
|
|
2019-08-15 12:35:11 +03:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2018-08-30 13:40:41 +03:00
|
|
|
/**
|
|
|
|
* Gets the base domain of URL.
|
|
|
|
*
|
|
|
|
* Returns the base domain, e.g. https://another.example.co.uk -> example.co.uk
|
|
|
|
*/
|
2019-11-01 20:13:12 +02:00
|
|
|
QString BrowserService::baseDomain(const QString& hostname) const
|
2018-08-30 13:40:41 +03:00
|
|
|
{
|
2019-11-01 20:13:12 +02:00
|
|
|
QUrl qurl = QUrl::fromUserInput(hostname);
|
|
|
|
QString host = qurl.host();
|
|
|
|
|
|
|
|
// If the hostname is an IP address, return it directly
|
|
|
|
QHostAddress hostAddress(hostname);
|
|
|
|
if (!hostAddress.isNull()) {
|
|
|
|
return hostname;
|
|
|
|
}
|
2018-08-30 13:40:41 +03:00
|
|
|
|
2019-11-01 20:13:12 +02:00
|
|
|
if (host.isEmpty() || !host.contains(qurl.topLevelDomain())) {
|
2018-08-30 13:40:41 +03:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the top level domain part from the hostname, e.g. https://another.example.co.uk -> https://another.example
|
2019-11-01 20:13:12 +02:00
|
|
|
host.chop(qurl.topLevelDomain().length());
|
2018-08-30 13:40:41 +03:00
|
|
|
// Split the URL and select the last part, e.g. https://another.example -> example
|
2019-11-01 20:13:12 +02:00
|
|
|
QString baseDomain = host.split('.').last();
|
2018-08-30 13:40:41 +03:00
|
|
|
// Append the top level domain back to the URL, e.g. example -> example.co.uk
|
|
|
|
baseDomain.append(qurl.topLevelDomain());
|
|
|
|
return baseDomain;
|
|
|
|
}
|
|
|
|
|
2018-11-22 11:47:31 +01:00
|
|
|
QSharedPointer<Database> BrowserService::getDatabase()
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
2020-05-10 21:20:00 -04:00
|
|
|
if (m_currentDatabaseWidget) {
|
|
|
|
return m_currentDatabaseWidget->database();
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
2018-11-22 11:47:31 +01:00
|
|
|
return {};
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 11:47:31 +01:00
|
|
|
QSharedPointer<Database> BrowserService::selectedDatabase()
|
2018-10-19 21:44:08 +03:00
|
|
|
{
|
|
|
|
QList<DatabaseWidget*> databaseWidgets;
|
2020-05-10 21:20:00 -04:00
|
|
|
for (auto dbWidget : getMainWindow()->getOpenDatabases()) {
|
2018-10-19 21:44:08 +03:00
|
|
|
// Add only open databases
|
2020-05-10 21:20:00 -04:00
|
|
|
if (!dbWidget->isLocked()) {
|
|
|
|
databaseWidgets << dbWidget;
|
2018-10-19 21:44:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowserEntrySaveDialog browserEntrySaveDialog;
|
2020-05-10 21:20:00 -04:00
|
|
|
int openDatabaseCount = browserEntrySaveDialog.setItems(databaseWidgets, m_currentDatabaseWidget);
|
2018-10-19 21:44:08 +03:00
|
|
|
if (openDatabaseCount > 1) {
|
|
|
|
int res = browserEntrySaveDialog.exec();
|
|
|
|
if (res == QDialog::Accepted) {
|
|
|
|
const auto selectedDatabase = browserEntrySaveDialog.getSelected();
|
|
|
|
if (selectedDatabase.length() > 0) {
|
2019-11-01 20:13:12 +02:00
|
|
|
int index = selectedDatabase[0]->data(Qt::UserRole).toInt();
|
2018-10-19 21:44:08 +03:00
|
|
|
return databaseWidgets[index]->database();
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-22 11:47:31 +01:00
|
|
|
return {};
|
2018-10-19 21:44:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return current database
|
|
|
|
return getDatabase();
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
bool BrowserService::moveSettingsToCustomData(Entry* entry, const QString& name)
|
2018-10-24 17:49:53 +03:00
|
|
|
{
|
|
|
|
if (entry->attributes()->contains(name)) {
|
|
|
|
QString attr = entry->attributes()->value(name);
|
|
|
|
entry->beginUpdate();
|
|
|
|
if (!attr.isEmpty()) {
|
|
|
|
entry->customData()->set(KEEPASSXCBROWSER_NAME, attr);
|
|
|
|
}
|
|
|
|
entry->attributes()->remove(name);
|
|
|
|
entry->endUpdate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
int BrowserService::moveKeysToCustomData(Entry* entry, QSharedPointer<Database> db)
|
2018-10-24 17:49:53 +03:00
|
|
|
{
|
|
|
|
int keyCounter = 0;
|
|
|
|
for (const auto& key : entry->attributes()->keys()) {
|
2020-05-01 11:07:14 +03:00
|
|
|
if (key.contains(CustomData::BrowserLegacyKeyPrefix)) {
|
2018-10-24 17:49:53 +03:00
|
|
|
QString publicKey = key;
|
2020-05-01 11:07:14 +03:00
|
|
|
publicKey.remove(CustomData::BrowserLegacyKeyPrefix);
|
2018-10-24 17:49:53 +03:00
|
|
|
|
|
|
|
// Add key to database custom data
|
2020-05-01 11:07:14 +03:00
|
|
|
if (db && !db->metadata()->customData()->contains(CustomData::BrowserKeyPrefix + publicKey)) {
|
|
|
|
db->metadata()->customData()->set(CustomData::BrowserKeyPrefix + publicKey,
|
|
|
|
entry->attributes()->value(key));
|
2018-10-24 17:49:53 +03:00
|
|
|
++keyCounter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyCounter;
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
bool BrowserService::checkLegacySettings(QSharedPointer<Database> db)
|
2018-10-24 17:49:53 +03:00
|
|
|
{
|
2020-05-10 21:20:00 -04:00
|
|
|
if (!db || !browserSettings()->isEnabled() || browserSettings()->noMigrationPrompt()) {
|
2018-10-24 17:49:53 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool legacySettingsFound = false;
|
|
|
|
QList<Entry*> entries = db->rootGroup()->entriesRecursive();
|
|
|
|
for (const auto& e : entries) {
|
2020-09-27 20:41:41 +03:00
|
|
|
if (e->isRecycled()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-20 09:50:20 -05:00
|
|
|
if ((e->attributes()->contains(KEEPASSHTTP_NAME) || e->attributes()->contains(KEEPASSXCBROWSER_NAME))
|
|
|
|
|| (e->title() == KEEPASSHTTP_NAME || e->title().contains(KEEPASSXCBROWSER_NAME, Qt::CaseInsensitive))) {
|
2018-10-24 17:49:53 +03:00
|
|
|
legacySettingsFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!legacySettingsFound) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-02 11:55:15 +03:00
|
|
|
auto* checkbox = new QCheckBox(tr("Don't show this warning again"));
|
|
|
|
QObject::connect(checkbox, &QCheckBox::stateChanged, [&](int state) {
|
|
|
|
browserSettings()->setNoMigrationPrompt(static_cast<Qt::CheckState>(state) == Qt::CheckState::Checked);
|
|
|
|
});
|
|
|
|
|
2019-03-19 14:48:33 -04:00
|
|
|
auto dialogResult =
|
|
|
|
MessageBox::warning(nullptr,
|
|
|
|
tr("KeePassXC: Legacy browser integration settings detected"),
|
|
|
|
tr("Your KeePassXC-Browser settings need to be moved into the database settings.\n"
|
|
|
|
"This is necessary to maintain your current browser connections.\n"
|
|
|
|
"Would you like to migrate your existing settings now?"),
|
2019-07-02 11:55:15 +03:00
|
|
|
MessageBox::Yes | MessageBox::No,
|
|
|
|
MessageBox::NoButton,
|
|
|
|
MessageBox::Raise,
|
|
|
|
checkbox);
|
2018-12-19 20:14:11 -08:00
|
|
|
|
|
|
|
return dialogResult == MessageBox::Yes;
|
2018-10-24 17:49:53 +03:00
|
|
|
}
|
|
|
|
|
2020-09-13 17:38:19 +03:00
|
|
|
QStringList BrowserService::getEntryURLs(const Entry* entry)
|
|
|
|
{
|
|
|
|
QStringList urlList;
|
|
|
|
urlList << entry->url();
|
|
|
|
|
|
|
|
// Handle additional URL's
|
|
|
|
for (const auto& key : entry->attributes()->keys()) {
|
|
|
|
if (key.startsWith(ADDITIONAL_URL)) {
|
|
|
|
urlList << entry->attributes()->value(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return urlList;
|
|
|
|
}
|
|
|
|
|
2018-01-25 14:21:05 +02:00
|
|
|
void BrowserService::hideWindow() const
|
|
|
|
{
|
2019-02-01 18:11:15 -05:00
|
|
|
if (m_prevWindowState == WindowState::Minimized) {
|
2018-01-25 14:21:05 +02:00
|
|
|
getMainWindow()->showMinimized();
|
|
|
|
} else {
|
|
|
|
#ifdef Q_OS_MACOS
|
2019-02-01 18:11:15 -05:00
|
|
|
if (m_prevWindowState == WindowState::Hidden) {
|
2019-01-16 18:03:54 +02:00
|
|
|
macUtils()->hideOwnWindow();
|
|
|
|
} else {
|
|
|
|
macUtils()->raiseLastActiveWindow();
|
|
|
|
}
|
2018-01-25 14:21:05 +02:00
|
|
|
#else
|
2019-02-01 18:11:15 -05:00
|
|
|
if (m_prevWindowState == WindowState::Hidden) {
|
|
|
|
getMainWindow()->hideWindow();
|
|
|
|
} else {
|
|
|
|
getMainWindow()->lower();
|
|
|
|
}
|
2018-01-25 14:21:05 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserService::raiseWindow(const bool force)
|
|
|
|
{
|
2019-02-01 18:11:15 -05:00
|
|
|
m_prevWindowState = WindowState::Normal;
|
|
|
|
if (getMainWindow()->isMinimized()) {
|
|
|
|
m_prevWindowState = WindowState::Minimized;
|
|
|
|
}
|
2018-01-25 14:21:05 +02:00
|
|
|
#ifdef Q_OS_MACOS
|
2019-11-01 20:13:12 +02:00
|
|
|
Q_UNUSED(force)
|
2019-03-19 08:30:49 -04:00
|
|
|
|
2019-02-01 18:11:15 -05:00
|
|
|
if (macUtils()->isHidden()) {
|
|
|
|
m_prevWindowState = WindowState::Hidden;
|
|
|
|
}
|
2018-01-25 14:21:05 +02:00
|
|
|
macUtils()->raiseOwnWindow();
|
|
|
|
Tools::wait(500);
|
|
|
|
#else
|
2019-02-01 18:11:15 -05:00
|
|
|
if (getMainWindow()->isHidden()) {
|
|
|
|
m_prevWindowState = WindowState::Hidden;
|
|
|
|
}
|
|
|
|
|
2018-01-25 14:21:05 +02:00
|
|
|
if (force) {
|
|
|
|
getMainWindow()->bringToFront();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-10-28 10:52:03 +02:00
|
|
|
void BrowserService::updateWindowState()
|
|
|
|
{
|
|
|
|
m_prevWindowState = WindowState::Normal;
|
|
|
|
if (getMainWindow()->isMinimized()) {
|
|
|
|
m_prevWindowState = WindowState::Minimized;
|
|
|
|
}
|
|
|
|
#ifdef Q_OS_MACOS
|
|
|
|
if (macUtils()->isHidden()) {
|
|
|
|
m_prevWindowState = WindowState::Hidden;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (getMainWindow()->isHidden()) {
|
|
|
|
m_prevWindowState = WindowState::Hidden;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:15:23 +02:00
|
|
|
void BrowserService::databaseLocked(DatabaseWidget* dbWidget)
|
|
|
|
{
|
|
|
|
if (dbWidget) {
|
2020-05-10 21:20:00 -04:00
|
|
|
QJsonObject msg;
|
|
|
|
msg["action"] = QString("database-locked");
|
|
|
|
m_browserHost->sendClientMessage(msg);
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserService::databaseUnlocked(DatabaseWidget* dbWidget)
|
|
|
|
{
|
|
|
|
if (dbWidget) {
|
2020-07-05 14:32:23 +03:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
if (m_bringToFrontRequested) {
|
|
|
|
m_bringToFrontRequested = false;
|
|
|
|
hideWindow();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
QJsonObject msg;
|
|
|
|
msg["action"] = QString("database-unlocked");
|
|
|
|
m_browserHost->sendClientMessage(msg);
|
|
|
|
|
|
|
|
auto db = dbWidget->database();
|
|
|
|
if (checkLegacySettings(db)) {
|
|
|
|
convertAttributesToCustomData(db);
|
2018-10-24 17:49:53 +03:00
|
|
|
}
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:20:00 -04:00
|
|
|
void BrowserService::activeDatabaseChanged(DatabaseWidget* dbWidget)
|
2017-12-12 10:15:23 +02:00
|
|
|
{
|
2020-04-24 18:38:33 +03:00
|
|
|
// Only emit these signals when we are not searching in all databases
|
|
|
|
if (dbWidget && !browserSettings()->searchInAllDatabases()) {
|
2020-05-10 21:20:00 -04:00
|
|
|
if (dbWidget->isLocked()) {
|
|
|
|
databaseLocked(dbWidget);
|
2017-12-12 10:15:23 +02:00
|
|
|
} else {
|
2020-05-10 21:20:00 -04:00
|
|
|
databaseUnlocked(dbWidget);
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 18:38:33 +03:00
|
|
|
|
|
|
|
m_currentDatabaseWidget = dbWidget;
|
2017-12-12 10:15:23 +02:00
|
|
|
}
|
2020-05-10 21:20:00 -04:00
|
|
|
|
|
|
|
void BrowserService::processClientMessage(const QJsonObject& message)
|
|
|
|
{
|
|
|
|
auto clientID = message["clientID"].toString();
|
|
|
|
if (clientID.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new client action if we haven't seen this id yet
|
|
|
|
if (!m_browserClients.contains(clientID)) {
|
|
|
|
m_browserClients.insert(clientID, QSharedPointer<BrowserAction>::create());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& action = m_browserClients.value(clientID);
|
|
|
|
auto response = action->processClientMessage(message);
|
|
|
|
m_browserHost->sendClientMessage(response);
|
|
|
|
}
|