2017-12-12 03:15:23 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Sami Vänttinen <sami.vanttinen@protonmail.com>
|
|
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2018-03-31 16:01:30 -04:00
|
|
|
#include "NativeMessagingHost.h"
|
|
|
|
#include "BrowserSettings.h"
|
|
|
|
#include "sodium.h"
|
2017-12-12 03:15:23 -05:00
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QtNetwork>
|
|
|
|
#include <iostream>
|
|
|
|
|
2018-04-26 23:04:36 -04:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include <Winsock2.h>
|
|
|
|
#endif
|
|
|
|
|
2018-05-07 23:22:59 -04:00
|
|
|
NativeMessagingHost::NativeMessagingHost(DatabaseTabWidget* parent, const bool enabled)
|
|
|
|
: NativeMessagingBase(enabled)
|
2018-03-31 16:01:30 -04:00
|
|
|
, m_mutex(QMutex::Recursive)
|
|
|
|
, m_browserService(parent)
|
2018-09-05 16:21:59 -04:00
|
|
|
, m_browserClients(m_browserService)
|
2017-12-12 03:15:23 -05:00
|
|
|
{
|
|
|
|
m_localServer.reset(new QLocalServer(this));
|
|
|
|
m_localServer->setSocketOptions(QLocalServer::UserAccessOption);
|
|
|
|
m_running.store(false);
|
|
|
|
|
2018-09-05 16:21:59 -04:00
|
|
|
if (browserSettings()->isEnabled() && !m_running) {
|
2017-12-12 03:15:23 -05:00
|
|
|
run();
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(&m_browserService, SIGNAL(databaseLocked()), this, SLOT(databaseLocked()));
|
|
|
|
connect(&m_browserService, SIGNAL(databaseUnlocked()), this, SLOT(databaseUnlocked()));
|
|
|
|
}
|
|
|
|
|
|
|
|
NativeMessagingHost::~NativeMessagingHost()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
int NativeMessagingHost::init()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
return sodium_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingHost::run()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
if (!m_running.load() && init() == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update KeePassXC/keepassxc-proxy binary paths to Native Messaging scripts
|
2018-09-05 16:21:59 -04:00
|
|
|
if (browserSettings()->updateBinaryPath()) {
|
|
|
|
browserSettings()->updateBinaryPaths(browserSettings()->useCustomProxy() ? browserSettings()->customProxyLocation()
|
2018-03-31 16:01:30 -04:00
|
|
|
: "");
|
2017-12-12 03:15:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m_running.store(true);
|
|
|
|
#ifdef Q_OS_WIN
|
2018-03-31 16:01:30 -04:00
|
|
|
m_future =
|
|
|
|
QtConcurrent::run(this, static_cast<void (NativeMessagingHost::*)()>(&NativeMessagingHost::readNativeMessages));
|
2017-12-12 03:15:23 -05:00
|
|
|
#endif
|
|
|
|
|
2018-09-05 16:21:59 -04:00
|
|
|
if (browserSettings()->supportBrowserProxy()) {
|
2017-12-12 03:15:23 -05:00
|
|
|
QString serverPath = getLocalServerPath();
|
|
|
|
QFile::remove(serverPath);
|
2018-01-17 07:55:13 -05:00
|
|
|
|
2018-04-29 02:24:57 -04:00
|
|
|
// Ensure that STDIN is not being listened when proxy is used
|
2018-05-09 04:59:39 -04:00
|
|
|
if (m_notifier && m_notifier->isEnabled()) {
|
2018-04-29 02:24:57 -04:00
|
|
|
m_notifier->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
2018-01-17 07:55:13 -05:00
|
|
|
if (m_localServer->isListening()) {
|
|
|
|
m_localServer->close();
|
|
|
|
}
|
|
|
|
|
2017-12-12 03:15:23 -05:00
|
|
|
m_localServer->listen(serverPath);
|
|
|
|
connect(m_localServer.data(), SIGNAL(newConnection()), this, SLOT(newLocalConnection()));
|
|
|
|
} else {
|
|
|
|
m_localServer->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingHost::stop()
|
|
|
|
{
|
|
|
|
databaseLocked();
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
m_socketList.clear();
|
|
|
|
m_running.testAndSetOrdered(true, false);
|
|
|
|
m_future.waitForFinished();
|
|
|
|
m_localServer->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingHost::readLength()
|
|
|
|
{
|
|
|
|
quint32 length = 0;
|
|
|
|
std::cin.read(reinterpret_cast<char*>(&length), 4);
|
2018-01-10 05:24:09 -05:00
|
|
|
if (!std::cin.eof() && length > 0) {
|
2017-12-12 03:15:23 -05:00
|
|
|
readStdIn(length);
|
2018-01-10 05:24:09 -05:00
|
|
|
} else {
|
2018-03-31 16:01:30 -04:00
|
|
|
m_notifier->setEnabled(false);
|
2017-12-12 03:15:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 08:28:11 -04:00
|
|
|
bool NativeMessagingHost::readStdIn(const quint32 length)
|
2017-12-12 03:15:23 -05:00
|
|
|
{
|
2018-03-04 17:56:06 -05:00
|
|
|
if (length <= 0) {
|
2018-05-28 08:28:11 -04:00
|
|
|
return false;
|
2018-03-04 17:56:06 -05:00
|
|
|
}
|
2017-12-12 03:15:23 -05:00
|
|
|
|
2018-03-04 17:56:06 -05:00
|
|
|
QByteArray arr;
|
|
|
|
arr.reserve(length);
|
2017-12-12 03:15:23 -05:00
|
|
|
|
2018-03-04 17:56:06 -05:00
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
|
|
|
for (quint32 i = 0; i < length; ++i) {
|
|
|
|
int c = std::getchar();
|
|
|
|
if (c == EOF) {
|
|
|
|
// message ended prematurely, ignore it and return
|
2018-05-28 08:28:11 -04:00
|
|
|
return false;
|
2017-12-12 03:15:23 -05:00
|
|
|
}
|
2018-03-04 17:56:06 -05:00
|
|
|
arr.append(static_cast<char>(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arr.length() > 0) {
|
|
|
|
sendReply(m_browserClients.readResponse(arr));
|
2017-12-12 03:15:23 -05:00
|
|
|
}
|
2018-05-28 08:28:11 -04:00
|
|
|
return true;
|
2017-12-12 03:15:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingHost::newLocalConnection()
|
|
|
|
{
|
|
|
|
QLocalSocket* socket = m_localServer->nextPendingConnection();
|
2018-01-17 07:55:13 -05:00
|
|
|
if (socket) {
|
|
|
|
connect(socket, SIGNAL(readyRead()), this, SLOT(newLocalMessage()));
|
|
|
|
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnectSocket()));
|
|
|
|
}
|
2017-12-12 03:15:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingHost::newLocalMessage()
|
|
|
|
{
|
|
|
|
QLocalSocket* socket = qobject_cast<QLocalSocket*>(QObject::sender());
|
|
|
|
if (!socket || socket->bytesAvailable() <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-02 22:13:07 -04:00
|
|
|
socket->setReadBufferSize(NATIVE_MSG_MAX_LENGTH);
|
|
|
|
int socketDesc = socket->socketDescriptor();
|
|
|
|
if (socketDesc) {
|
|
|
|
int max = NATIVE_MSG_MAX_LENGTH;
|
2018-04-26 23:04:36 -04:00
|
|
|
setsockopt(socketDesc, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char*>(&max), sizeof(max));
|
2018-04-02 22:13:07 -04:00
|
|
|
}
|
|
|
|
|
2017-12-12 03:15:23 -05:00
|
|
|
QByteArray arr = socket->readAll();
|
|
|
|
if (arr.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
if (!m_socketList.contains(socket)) {
|
|
|
|
m_socketList.push_back(socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString reply = jsonToString(m_browserClients.readResponse(arr));
|
|
|
|
if (socket && socket->isValid() && socket->state() == QLocalSocket::ConnectedState) {
|
|
|
|
QByteArray arr = reply.toUtf8();
|
|
|
|
socket->write(arr.constData(), arr.length());
|
|
|
|
socket->flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingHost::sendReplyToAllClients(const QJsonObject& json)
|
|
|
|
{
|
|
|
|
QString reply = jsonToString(json);
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
for (const auto socket : m_socketList) {
|
|
|
|
if (socket && socket->isValid() && socket->state() == QLocalSocket::ConnectedState) {
|
|
|
|
QByteArray arr = reply.toUtf8();
|
|
|
|
socket->write(arr.constData(), arr.length());
|
|
|
|
socket->flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingHost::disconnectSocket()
|
|
|
|
{
|
|
|
|
QLocalSocket* socket(qobject_cast<QLocalSocket*>(QObject::sender()));
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
for (auto s : m_socketList) {
|
|
|
|
if (s == socket) {
|
|
|
|
m_socketList.removeOne(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingHost::removeSharedEncryptionKeys()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
m_browserService.removeSharedEncryptionKeys();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingHost::removeStoredPermissions()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
m_browserService.removeStoredPermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingHost::databaseLocked()
|
|
|
|
{
|
|
|
|
QJsonObject response;
|
|
|
|
response["action"] = "database-locked";
|
|
|
|
sendReplyToAllClients(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingHost::databaseUnlocked()
|
|
|
|
{
|
|
|
|
QJsonObject response;
|
|
|
|
response["action"] = "database-unlocked";
|
|
|
|
sendReplyToAllClients(response);
|
|
|
|
}
|