Merge branch 'develop' into middle-mouse-click-status-icon

This commit is contained in:
TheZ3ro 2017-10-07 17:57:38 +02:00 committed by GitHub
commit e9dd6e6c90
80 changed files with 14475 additions and 7097 deletions

View file

@ -66,6 +66,9 @@ set(keepassx_SOURCES
core/Tools.cpp
core/Translator.cpp
core/Uuid.cpp
core/Base32.h
core/Base32.cpp
core/Optional.h
cli/Utils.cpp
cli/Utils.h
crypto/Crypto.cpp
@ -145,8 +148,6 @@ set(keepassx_SOURCES
streams/qtiocompressor.cpp
streams/StoreDataStream.cpp
streams/SymmetricCipherStream.cpp
totp/base32.h
totp/base32.cpp
totp/totp.h
totp/totp.cpp
)

210
src/core/Base32.cpp Normal file
View file

@ -0,0 +1,210 @@
/*
* 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 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/>.
*/
// Conforms to RFC 4648. For details, see: https://tools.ietf.org/html/rfc4648
#include "Base32.h"
constexpr quint64 MASK_40BIT = quint64(0xF8) << 32;
constexpr quint64 MASK_35BIT = quint64(0x7C0000000);
constexpr quint64 MASK_25BIT = quint64(0x1F00000);
constexpr quint64 MASK_20BIT = quint64(0xF8000);
constexpr quint64 MASK_10BIT = quint64(0x3E0);
constexpr char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
constexpr quint8 ALPH_POS_2 = 26;
constexpr quint8 ASCII_2 = static_cast<quint8>('2');
constexpr quint8 ASCII_7 = static_cast<quint8>('7');
constexpr quint8 ASCII_A = static_cast<quint8>('A');
constexpr quint8 ASCII_Z = static_cast<quint8>('Z');
constexpr quint8 ASCII_a = static_cast<quint8>('a');
constexpr quint8 ASCII_z = static_cast<quint8>('z');
constexpr quint8 ASCII_EQ = static_cast<quint8>('=');
Optional<QByteArray> Base32::decode(const QByteArray& encodedData)
{
if (encodedData.size() <= 0)
return Optional<QByteArray>("");
if (encodedData.size() % 8 != 0)
return Optional<QByteArray>();
int nPads = 0;
for (int i = -1; i > -7; --i) {
if ('=' == encodedData[encodedData.size()+i])
++nPads;
}
int specialOffset;
int nSpecialBytes;
switch (nPads) { // in {0, 1, 3, 4, 6}
case 1:
nSpecialBytes = 4;
specialOffset = 3;
break;
case 3:
nSpecialBytes = 3;
specialOffset = 1;
break;
case 4:
nSpecialBytes = 2;
specialOffset = 4;
break;
case 6:
nSpecialBytes = 1;
specialOffset = 2;
break;
default:
nSpecialBytes = 0;
specialOffset = 0;
}
Q_ASSERT(encodedData.size() > 0);
const int nQuantums = encodedData.size() / 8;
const int nBytes = (nQuantums - 1) * 5 + nSpecialBytes;
QByteArray data(nBytes, Qt::Uninitialized);
int i = 0;
int o = 0;
while (i < encodedData.size()) {
quint64 quantum = 0;
int nQuantumBytes = 5;
for (int n = 0; n < 8; n++) {
quint8 ch = static_cast<quint8>(encodedData[i++]);
if ((ASCII_A <= ch && ch <= ASCII_Z) || (ASCII_a <= ch && ch <= ASCII_z)) {
ch -= ASCII_A;
if (ch >= ALPH_POS_2)
ch -= ASCII_a - ASCII_A;
} else {
if (ch >= ASCII_2 && ch <= ASCII_7) {
ch -= ASCII_2;
ch += ALPH_POS_2;
} else {
if (ASCII_EQ == ch) {
if(i == encodedData.size()) {
// finished with special quantum
quantum >>= specialOffset;
nQuantumBytes = nSpecialBytes;
}
continue;
} else {
// illegal character
return Optional<QByteArray>();
}
}
}
quantum <<= 5;
quantum |= ch;
}
const int offset = (nQuantumBytes - 1) * 8;
quint64 mask = quint64(0xFF) << offset;
for (int n = offset; n >= 0; n -= 8) {
char c = static_cast<char>((quantum & mask) >> n);
data[o++] = c;
mask >>= 8;
}
}
return Optional<QByteArray>(data);
}
QByteArray Base32::encode(const QByteArray& data)
{
if (data.size() < 1)
return QByteArray();
const int nBits = data.size() * 8;
const int rBits = nBits % 40; // in {0, 8, 16, 24, 32}
const int nQuantums = nBits / 40 + (rBits > 0 ? 1 : 0);
QByteArray encodedData(nQuantums * 8, Qt::Uninitialized);
int i = 0;
int o = 0;
int n;
quint64 mask;
quint64 quantum;
// 40-bits of input per input group
while (i + 5 <= data.size()) {
quantum = 0;
for (n = 32; n >= 0; n -= 8) {
quantum |= (static_cast<quint64>(data[i++]) << n);
}
mask = MASK_40BIT;
int index;
for (n = 35; n >= 0; n -= 5) {
index = (quantum & mask) >> n;
encodedData[o++] = alphabet[index];
mask >>= 5;
}
}
// < 40-bits of input at final input group
if (i < data.size()) {
Q_ASSERT(rBits > 0);
quantum = 0;
for (n = rBits - 8; n >= 0; n -= 8)
quantum |= static_cast<quint64>(data[i++]) << n;
switch (rBits) {
case 8: // expand to 10 bits
quantum <<= 2;
mask = MASK_10BIT;
n = 5;
break;
case 16: // expand to 20 bits
quantum <<= 4;
mask = MASK_20BIT;
n = 15;
break;
case 24: // expand to 25 bits
quantum <<= 1;
mask = MASK_25BIT;
n = 20;
break;
default: // expand to 35 bits
Q_ASSERT(rBits == 32);
quantum <<= 3;
mask = MASK_35BIT;
n = 30;
}
while (n >= 0) {
int index = (quantum & mask) >> n;
encodedData[o++] = alphabet[index];
mask >>= 5;
n -= 5;
}
// add pad characters
while (o < encodedData.size())
encodedData[o++] = '=';
}
Q_ASSERT(encodedData.size() == o);
return encodedData;
}

36
src/core/Base32.h Normal file
View file

@ -0,0 +1,36 @@
/*
* 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 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/>.
*/
// Conforms to RFC 4648. For details, see: https://tools.ietf.org/html/rfc4648
#ifndef BASE32_H
#define BASE32_H
#include "Optional.h"
#include <QtCore/qglobal.h>
#include <QByteArray>
class Base32
{
public:
Base32() =default;
Q_REQUIRED_RESULT static Optional<QByteArray> decode(const QByteArray&);
Q_REQUIRED_RESULT static QByteArray encode(const QByteArray&);
};
#endif //BASE32_H

View file

@ -122,7 +122,7 @@ void Config::init(const QString& fileName)
m_defaults.insert("AutoTypeEntryURLMatch", true);
m_defaults.insert("AutoTypeDelay", 25);
m_defaults.insert("UseGroupIconOnEntryCreation", true);
m_defaults.insert("IgnoreGroupExpansion", false);
m_defaults.insert("IgnoreGroupExpansion", true);
m_defaults.insert("security/clearclipboard", true);
m_defaults.insert("security/clearclipboardtimeout", 10);
m_defaults.insert("security/lockdatabaseidle", false);

View file

@ -331,6 +331,15 @@ void Database::emptyRecycleBin()
void Database::merge(const Database* other)
{
m_rootGroup->merge(other->rootGroup());
for (Uuid customIconId : other->metadata()->customIcons().keys()) {
QImage customIcon = other->metadata()->customIcon(customIconId);
if (!this->metadata()->containsCustomIcon(customIconId)) {
qDebug("Adding custom icon %s to database.", qPrintable(customIconId.toHex()));
this->metadata()->addCustomIcon(customIconId, customIcon);
}
}
emit modified();
}
@ -418,7 +427,7 @@ Database* Database::unlockFromStdin(QString databaseFilename, QString keyFilenam
FileKey fileKey;
QString errorMessage;
if (!fileKey.load(keyFilename, &errorMessage)) {
errorTextStream << QObject::tr("Failed to load key file %1 : %2").arg(keyFilename).arg(errorMessage);
errorTextStream << QObject::tr("Failed to load key file %1 : %2").arg(keyFilename, errorMessage);
errorTextStream << endl;
return nullptr;
}

View file

@ -727,6 +727,13 @@ const Database* Entry::database() const
}
}
QString Entry::maskPasswordPlaceholders(const QString &str) const
{
QString result = str;
result.replace(QRegExp("(\\{PASSWORD\\})", Qt::CaseInsensitive, QRegExp::RegExp2), "******");
return result;
}
QString Entry::resolveMultiplePlaceholders(const QString& str) const
{
QString result = str;
@ -818,4 +825,4 @@ QString Entry::resolveUrl(const QString& url) const
Q_UNUSED(url);
#endif
return QString("");
}
}

View file

@ -142,6 +142,7 @@ public:
*/
Entry* clone(CloneFlags flags) const;
void copyDataFrom(const Entry* other);
QString maskPasswordPlaceholders(const QString& str) const;
QString resolveMultiplePlaceholders(const QString& str) const;
QString resolvePlaceholder(const QString& str) const;
QString resolveUrl(const QString& url) const;

View file

@ -29,7 +29,7 @@ QList<QString> EntryAttachments::keys() const
bool EntryAttachments::hasKey(const QString& key) const
{
return m_attachments.keys().contains(key);
return m_attachments.contains(key);
}
QList<QByteArray> EntryAttachments::values() const

View file

@ -41,7 +41,7 @@ QList<QString> EntryAttributes::keys() const
bool EntryAttributes::hasKey(const QString& key) const
{
return m_attributes.keys().contains(key);
return m_attributes.contains(key);
}
QList<QString> EntryAttributes::customKeys()

View file

@ -668,7 +668,7 @@ void Group::merge(const Group* other)
// This entry does not exist at all. Create it.
if (!existingEntry) {
qDebug("New entry %s detected. Creating it.", qPrintable(entry->title()));
entry->clone(Entry::CloneNoFlags)->setGroup(this);
entry->clone(Entry::CloneIncludeHistory)->setGroup(this);
// Entry is already present in the database. Update it.
} else {
bool locationChanged = existingEntry->timeInfo().locationChanged() < entry->timeInfo().locationChanged();
@ -902,11 +902,11 @@ void Group::resolveConflict(Entry* existingEntry, Entry* otherEntry)
case KeepBoth:
// if one entry is newer, create a clone and add it to the group
if (timeExisting > timeOther) {
clonedEntry = otherEntry->clone(Entry::CloneNewUuid);
clonedEntry = otherEntry->clone(Entry::CloneNewUuid | Entry::CloneIncludeHistory);
clonedEntry->setGroup(this);
markOlderEntry(clonedEntry);
} else if (timeExisting < timeOther) {
clonedEntry = otherEntry->clone(Entry::CloneNewUuid);
clonedEntry = otherEntry->clone(Entry::CloneNewUuid | Entry::CloneIncludeHistory);
clonedEntry->setGroup(this);
markOlderEntry(existingEntry);
}
@ -917,7 +917,7 @@ void Group::resolveConflict(Entry* existingEntry, Entry* otherEntry)
// only if other entry is newer, replace existing one
Group* currentGroup = existingEntry->group();
currentGroup->removeEntry(existingEntry);
otherEntry->clone(Entry::CloneNoFlags)->setGroup(currentGroup);
otherEntry->clone(Entry::CloneIncludeHistory)->setGroup(currentGroup);
}
break;

View file

@ -15,6 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtCore/QCryptographicHash>
#include "Metadata.h"
#include "core/Entry.h"
@ -40,7 +41,6 @@ Metadata::Metadata(QObject* parent)
m_data.protectPassword = true;
m_data.protectUrl = false;
m_data.protectNotes = false;
// m_data.autoEnableVisualHiding = false;
QDateTime now = QDateTime::currentDateTimeUtc();
m_data.nameChanged = now;
@ -157,11 +157,6 @@ bool Metadata::protectNotes() const
return m_data.protectNotes;
}
/*bool Metadata::autoEnableVisualHiding() const
{
return m_autoEnableVisualHiding;
}*/
QImage Metadata::customIcon(const Uuid& uuid) const
{
return m_customIcons.value(uuid);
@ -375,11 +370,6 @@ void Metadata::setProtectNotes(bool value)
set(m_data.protectNotes, value);
}
/*void Metadata::setAutoEnableVisualHiding(bool value)
{
set(m_autoEnableVisualHiding, value);
}*/
void Metadata::addCustomIcon(const Uuid& uuid, const QImage& icon)
{
Q_ASSERT(!uuid.isNull());
@ -390,6 +380,9 @@ void Metadata::addCustomIcon(const Uuid& uuid, const QImage& icon)
m_customIconCacheKeys[uuid] = QPixmapCache::Key();
m_customIconScaledCacheKeys[uuid] = QPixmapCache::Key();
m_customIconsOrder.append(uuid);
// Associate image hash to uuid
QByteArray hash = hashImage(icon);
m_customIconsHashes[hash] = uuid;
Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count());
emit modified();
}
@ -415,6 +408,12 @@ void Metadata::removeCustomIcon(const Uuid& uuid)
Q_ASSERT(!uuid.isNull());
Q_ASSERT(m_customIcons.contains(uuid));
// Remove hash record only if this is the same uuid
QByteArray hash = hashImage(m_customIcons[uuid]);
if (m_customIconsHashes.contains(hash) && m_customIconsHashes[hash] == uuid) {
m_customIconsHashes.remove(hash);
}
m_customIcons.remove(uuid);
QPixmapCache::remove(m_customIconCacheKeys.value(uuid));
m_customIconCacheKeys.remove(uuid);
@ -425,6 +424,12 @@ void Metadata::removeCustomIcon(const Uuid& uuid)
emit modified();
}
Uuid Metadata::findCustomIcon(const QImage &candidate)
{
QByteArray hash = hashImage(candidate);
return m_customIconsHashes.value(hash, Uuid());
}
void Metadata::copyCustomIcons(const QSet<Uuid>& iconList, const Metadata* otherMetadata)
{
for (const Uuid& uuid : iconList) {
@ -436,6 +441,12 @@ void Metadata::copyCustomIcons(const QSet<Uuid>& iconList, const Metadata* other
}
}
QByteArray Metadata::hashImage(const QImage& image)
{
auto data = QByteArray((char*)image.bits(), image.byteCount());
return QCryptographicHash::hash(data, QCryptographicHash::Md5);
}
void Metadata::setRecycleBinEnabled(bool value)
{
set(m_data.recycleBinEnabled, value);

View file

@ -60,7 +60,6 @@ public:
bool protectPassword;
bool protectUrl;
bool protectNotes;
// bool autoEnableVisualHiding;
};
QString generator() const;
@ -77,7 +76,6 @@ public:
bool protectPassword() const;
bool protectUrl() const;
bool protectNotes() const;
// bool autoEnableVisualHiding() const;
QImage customIcon(const Uuid& uuid) const;
QPixmap customIconPixmap(const Uuid& uuid) const;
QPixmap customIconScaledPixmap(const Uuid& uuid) const;
@ -117,11 +115,11 @@ public:
void setProtectPassword(bool value);
void setProtectUrl(bool value);
void setProtectNotes(bool value);
// void setAutoEnableVisualHiding(bool value);
void addCustomIcon(const Uuid& uuid, const QImage& icon);
void addCustomIconScaled(const Uuid& uuid, const QImage& icon);
void removeCustomIcon(const Uuid& uuid);
void copyCustomIcons(const QSet<Uuid>& iconList, const Metadata* otherMetadata);
Uuid findCustomIcon(const QImage& candidate);
void setRecycleBinEnabled(bool value);
void setRecycleBin(Group* group);
void setRecycleBinChanged(const QDateTime& value);
@ -154,12 +152,15 @@ private:
template <class P, class V> bool set(P& property, const V& value);
template <class P, class V> bool set(P& property, const V& value, QDateTime& dateTime);
QByteArray hashImage(const QImage& image);
MetadataData m_data;
QHash<Uuid, QImage> m_customIcons;
mutable QHash<Uuid, QPixmapCache::Key> m_customIconCacheKeys;
mutable QHash<Uuid, QPixmapCache::Key> m_customIconScaledCacheKeys;
QList<Uuid> m_customIconsOrder;
QHash<QByteArray, Uuid> m_customIconsHashes;
QPointer<Group> m_recycleBin;
QDateTime m_recycleBinChanged;

93
src/core/Optional.h Normal file
View file

@ -0,0 +1,93 @@
/*
* 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 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/>.
*/
#ifndef OPTIONAL_H
#define OPTIONAL_H
/*
* This utility class is for providing basic support for an option type.
* It can be replaced by std::optional (C++17) or
* std::experimental::optional (C++11) when they become fully supported
* by all the compilers.
*/
template <typename T>
class Optional
{
public:
// None
Optional() :
m_hasValue(false),
m_value()
{ };
// Some T
Optional(const T& value) :
m_hasValue(true),
m_value(value)
{ };
// Copy
Optional(const Optional& other) :
m_hasValue(other.m_hasValue),
m_value(other.m_value)
{ };
const Optional& operator=(const Optional& other)
{
m_hasValue = other.m_hasValue;
m_value = other.m_value;
return *this;
}
bool operator==(const Optional& other) const
{
if(m_hasValue)
return other.m_hasValue && m_value == other.m_value;
else
return !other.m_hasValue;
}
bool operator!=(const Optional& other) const
{
return !(*this == other);
}
bool hasValue() const
{
return m_hasValue;
}
T valueOr(const T& other) const
{
return m_hasValue ? m_value : other;
}
Optional static makeOptional(const T& value)
{
return Optional(value);
}
private:
bool m_hasValue;
T m_value;
};
#endif // OPTIONAL_H

View file

@ -323,9 +323,6 @@ void KeePass2XmlReader::parseMemoryProtection()
else if (m_xml.name() == "ProtectNotes") {
m_meta->setProtectNotes(readBool());
}
/*else if (m_xml.name() == "AutoEnableVisualHiding") {
m_meta->setAutoEnableVisualHiding(readBool());
}*/
else {
skipCurrentElement();
}

View file

@ -141,7 +141,6 @@ void KeePass2XmlWriter::writeMemoryProtection()
writeBool("ProtectPassword", m_meta->protectPassword());
writeBool("ProtectURL", m_meta->protectUrl());
writeBool("ProtectNotes", m_meta->protectNotes());
// writeBool("AutoEnableVisualHiding", m_meta->autoEnableVisualHiding());
m_xml.writeEndElement();
}
@ -521,9 +520,9 @@ void KeePass2XmlWriter::writeColor(const QString& qualifiedName, const QColor& c
QString colorStr;
if (color.isValid()) {
colorStr = QString("#%1%2%3").arg(colorPartToString(color.red()))
.arg(colorPartToString(color.green()))
.arg(colorPartToString(color.blue()));
colorStr = QString("#%1%2%3").arg(colorPartToString(color.red()),
colorPartToString(color.green()),
colorPartToString(color.blue()));
}
writeString(qualifiedName, colorStr);

View file

@ -59,16 +59,16 @@ AboutDialog::AboutDialog(QWidget* parent)
}
debugInfo.append(QString("%1\n- Qt %2\n- %3\n\n")
.arg(tr("Libraries:"))
.arg(QString::fromLocal8Bit(qVersion()))
.arg(Crypto::backendVersion()));
.arg(tr("Libraries:"),
QString::fromLocal8Bit(qVersion()),
Crypto::backendVersion()));
#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
debugInfo.append(tr("Operating system: %1\nCPU architecture: %2\nKernel: %3 %4")
.arg(QSysInfo::prettyProductName())
.arg(QSysInfo::currentCpuArchitecture())
.arg(QSysInfo::kernelType())
.arg(QSysInfo::kernelVersion()));
.arg(QSysInfo::prettyProductName(),
QSysInfo::currentCpuArchitecture(),
QSysInfo::kernelType(),
QSysInfo::kernelVersion()));
debugInfo.append("\n\n");
#endif

View file

@ -2,14 +2,6 @@
<ui version="4.0">
<class>AboutDialog</class>
<widget class="QDialog" name="AboutDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>652</width>
<height>516</height>
</rect>
</property>
<property name="windowTitle">
<string>About KeePassXC</string>
</property>

View file

@ -81,8 +81,8 @@ Application::Application(int& argc, char** argv)
#ifdef Q_OS_UNIX
, m_unixSignalNotifier(nullptr)
#endif
, alreadyRunning(false)
, lock(nullptr)
, m_alreadyRunning(false)
, m_lockFile(nullptr)
{
#if defined(Q_OS_UNIX) && !defined(Q_OS_OSX)
installNativeEventFilter(new XcbEventFilter());
@ -99,56 +99,72 @@ Application::Application(int& argc, char** argv)
}
QString identifier = "keepassxc";
if (!userName.isEmpty()) {
identifier.append("-");
identifier.append(userName);
#ifdef QT_DEBUG
// In DEBUG mode don't interfere with Release instances
identifier.append("-DEBUG");
#endif
identifier += "-" + userName;
}
#ifdef QT_DEBUG
// In DEBUG mode don't interfere with Release instances
identifier += "-DEBUG";
#endif
QString socketName = identifier + ".socket";
QString lockName = identifier + ".lock";
// According to documentation we should use RuntimeLocation on *nixes, but even Qt doesn't respect
// this and creates sockets in TempLocation, so let's be consistent.
lock = new QLockFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/" + lockName);
lock->setStaleLockTime(0);
lock->tryLock();
switch (lock->error()) {
m_lockFile = new QLockFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/" + lockName);
m_lockFile->setStaleLockTime(0);
m_lockFile->tryLock();
switch (m_lockFile->error()) {
case QLockFile::NoError:
server.setSocketOptions(QLocalServer::UserAccessOption);
server.listen(socketName);
connect(&server, SIGNAL(newConnection()), this, SIGNAL(anotherInstanceStarted()));
// No existing lock was found, start listener
m_lockServer.setSocketOptions(QLocalServer::UserAccessOption);
m_lockServer.listen(socketName);
connect(&m_lockServer, SIGNAL(newConnection()), this, SIGNAL(anotherInstanceStarted()));
break;
case QLockFile::LockFailedError: {
alreadyRunning = true;
// notify the other instance
// try several times, in case the other instance is still starting up
if (config()->get("SingleInstance").toBool()) {
// Attempt to connect to the existing instance
QLocalSocket client;
for (int i = 0; i < 3; i++) {
client.connectToServer(socketName);
if (client.waitForConnected(150)) {
// Connection succeeded, this will raise the existing window if minimized
client.abort();
m_alreadyRunning = true;
break;
}
}
if (!m_alreadyRunning) {
// If we get here then the original instance is likely dead
qWarning() << QCoreApplication::translate("Main",
"Existing single-instance lock file is invalid. Launching new instance.")
.toUtf8().constData();
// forceably reset the lock file
m_lockFile->removeStaleLockFile();
m_lockFile->tryLock();
// start the listen server
m_lockServer.setSocketOptions(QLocalServer::UserAccessOption);
m_lockServer.listen(socketName);
connect(&m_lockServer, SIGNAL(newConnection()), this, SIGNAL(anotherInstanceStarted()));
}
}
break;
}
default:
qWarning() << QCoreApplication::translate("Main",
"The lock file could not be created. Single-instance mode disabled.")
.toUtf8().constData();
"The lock file could not be created. Single-instance mode disabled.")
.toUtf8().constData();
}
}
Application::~Application()
{
server.close();
if (lock) {
lock->unlock();
delete lock;
m_lockServer.close();
if (m_lockFile) {
m_lockFile->unlock();
delete m_lockFile;
}
}
@ -243,6 +259,6 @@ bool Application::isAlreadyRunning() const
// In DEBUG mode we can run unlimited instances
return false;
#endif
return config()->get("SingleInstance").toBool() && alreadyRunning;
return config()->get("SingleInstance").toBool() && m_alreadyRunning;
}

View file

@ -60,9 +60,9 @@ private:
static void handleUnixSignal(int sig);
static int unixSignalSocket[2];
#endif
bool alreadyRunning;
QLockFile* lock;
QLocalServer server;
bool m_alreadyRunning;
QLockFile* m_lockFile;
QLocalServer m_lockServer;
};
#endif // KEEPASSX_APPLICATION_H

View file

@ -63,7 +63,7 @@ void CloneDialog::cloneEntry()
Entry* entry = m_entry->clone(flags);
entry->setGroup(m_entry->group());
emit m_parent->refreshSearch();
m_parent->refreshSearch();
close();
}

View file

@ -119,6 +119,18 @@ void DatabaseOpenWidget::load(const QString& filename)
m_ui->editPassword->setFocus();
}
void DatabaseOpenWidget::clearForms()
{
m_ui->editPassword->clear();
m_ui->comboKeyFile->clear();
m_ui->checkPassword->setChecked(false);
m_ui->checkKeyFile->setChecked(false);
m_ui->checkChallengeResponse->setChecked(false);
m_ui->buttonTogglePassword->setChecked(false);
m_db = nullptr;
}
Database* DatabaseOpenWidget::database()
{
return m_db;

View file

@ -39,6 +39,7 @@ public:
explicit DatabaseOpenWidget(QWidget* parent = nullptr);
~DatabaseOpenWidget();
void load(const QString& filename);
void clearForms();
void enterKey(const QString& pw, const QString& keyFile);
Database* database();

View file

@ -181,6 +181,7 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw,
lockFile->tryLock();
}
} else {
delete lockFile;
return;
}
}
@ -363,6 +364,8 @@ bool DatabaseTabWidget::saveDatabase(Database* db)
emit messageDismissTab();
return true;
} else {
dbStruct.modified = true;
updateTabName(db);
emit messageTab(tr("Writing the database failed.").append("\n").append(errorMessage),
MessageWidget::Error);
return false;

View file

@ -813,6 +813,7 @@ void DatabaseWidget::mergeDatabase(bool accepted)
m_db->merge(srcDb);
}
m_databaseOpenMergeWidget->clearForms();
setCurrentWidget(m_mainWidget);
emit databaseMerged(m_db);
}
@ -918,6 +919,7 @@ void DatabaseWidget::switchToImportCsv(const QString& fileName)
void DatabaseWidget::switchToOpenMergeDatabase(const QString& fileName)
{
m_databaseOpenMergeWidget->clearForms();
m_databaseOpenMergeWidget->load(fileName);
setCurrentWidget(m_databaseOpenMergeWidget);
}

View file

@ -68,7 +68,7 @@ EditWidgetIcons::EditWidgetIcons(QWidget* parent)
this, SLOT(updateWidgetsDefaultIcons(bool)));
connect(m_ui->customIconsRadio, SIGNAL(toggled(bool)),
this, SLOT(updateWidgetsCustomIcons(bool)));
connect(m_ui->addButton, SIGNAL(clicked()), SLOT(addCustomIcon()));
connect(m_ui->addButton, SIGNAL(clicked()), SLOT(addCustomIconFromFile()));
connect(m_ui->deleteButton, SIGNAL(clicked()), SLOT(removeCustomIcon()));
connect(m_ui->faviconButton, SIGNAL(clicked()), SLOT(downloadFavicon()));
@ -185,15 +185,7 @@ void EditWidgetIcons::fetchFavicon(const QUrl& url)
image.loadFromData(response->collectedData());
if (!image.isNull()) {
//Set the image
Uuid uuid = Uuid::random();
m_database->metadata()->addCustomIcon(uuid, image.scaled(16, 16));
m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(),
m_database->metadata()->customIconsOrder());
QModelIndex index = m_customIconModel->indexFromUuid(uuid);
m_ui->customIconsView->setCurrentIndex(index);
m_ui->customIconsRadio->setChecked(true);
addCustomIcon(image);
resetFaviconDownload();
} else {
fetchFaviconFromGoogle(url.host());
@ -226,7 +218,9 @@ void EditWidgetIcons::fetchFavicon(const QUrl& url)
QUrl tempurl = QUrl(m_url);
if (tempurl.scheme() == "http") {
resetFaviconDownload();
MessageBox::warning(this, tr("Error"), tr("Unable to fetch favicon.") + "\n" + tr("Hint: You can enable Google as a fallback under Tools>Settings>Security"));
emit messageEditEntry(tr("Unable to fetch favicon.") + "\n" +
tr("Hint: You can enable Google as a fallback under Tools>Settings>Security"),
MessageWidget::Error);
} else {
tempurl.setScheme("http");
m_url = tempurl.url();
@ -248,7 +242,7 @@ void EditWidgetIcons::fetchFaviconFromGoogle(const QString& domain)
fetchFavicon(faviconUrl);
} else {
resetFaviconDownload();
MessageBox::warning(this, tr("Error"), tr("Unable to fetch favicon."));
emit messageEditEntry(tr("Unable to fetch favicon."), MessageWidget::Error);
}
}
@ -269,7 +263,7 @@ void EditWidgetIcons::resetFaviconDownload(bool clearRedirect)
}
#endif
void EditWidgetIcons::addCustomIcon()
void EditWidgetIcons::addCustomIconFromFile()
{
if (m_database) {
QString filter = QString("%1 (%2);;%3 (*)").arg(tr("Images"),
@ -278,22 +272,41 @@ void EditWidgetIcons::addCustomIcon()
QString filename = QFileDialog::getOpenFileName(
this, tr("Select Image"), "", filter);
if (!filename.isEmpty()) {
QImage image(filename);
if (!image.isNull()) {
Uuid uuid = Uuid::random();
m_database->metadata()->addCustomIcon(uuid, image.scaled(16, 16));
m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(),
m_database->metadata()->customIconsOrder());
QModelIndex index = m_customIconModel->indexFromUuid(uuid);
m_ui->customIconsView->setCurrentIndex(index);
}
else {
auto icon = QImage(filename);
if (!icon.isNull()) {
addCustomIcon(QImage(filename));
} else {
emit messageEditEntry(tr("Can't read icon"), MessageWidget::Error);
}
}
}
}
void EditWidgetIcons::addCustomIcon(const QImage &icon)
{
if (m_database) {
Uuid uuid = m_database->metadata()->findCustomIcon(icon);
if (uuid.isNull()) {
uuid = Uuid::random();
// Don't add an icon larger than 128x128, but retain original size if smaller
if (icon.width() > 128 || icon.height() > 128) {
m_database->metadata()->addCustomIcon(uuid, icon.scaled(128, 128));
} else {
m_database->metadata()->addCustomIcon(uuid, icon);
}
m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(),
m_database->metadata()->customIconsOrder());
} else {
emit messageEditEntry(tr("Custom icon already exists"), MessageWidget::Information);
}
// Select the new or existing icon
QModelIndex index = m_customIconModel->indexFromUuid(uuid);
m_ui->customIconsView->setCurrentIndex(index);
}
}
void EditWidgetIcons::removeCustomIcon()
{
if (m_database) {

View file

@ -78,7 +78,8 @@ private slots:
void fetchFaviconFromGoogle(const QString& domain);
void resetFaviconDownload(bool clearRedirect = true);
#endif
void addCustomIcon();
void addCustomIconFromFile();
void addCustomIcon(const QImage& icon);
void removeCustomIcon();
void updateWidgetsDefaultIcons(bool checked);
void updateWidgetsCustomIcons(bool checked);

View file

@ -228,6 +228,15 @@ void SettingsWidget::saveSettings()
config()->set("security/passwordscleartext", m_secUi->passwordCleartextCheckBox->isChecked());
config()->set("security/passwordsrepeat", m_secUi->passwordRepeatCheckBox->isChecked());
// Security: clear storage if related settings are disabled
if (!config()->get("RememberLastDatabases").toBool()) {
config()->set("LastDatabases", QVariant());
}
if (!config()->get("RememberLastKeyFiles").toBool()) {
config()->set("LastKeyFiles", QVariant());
}
for (const ExtraPage& page: asConst(m_extraPages)) {
page.saveSettings();
}

View file

@ -26,14 +26,3 @@ UnlockDatabaseWidget::UnlockDatabaseWidget(QWidget* parent)
{
m_ui->labelHeadline->setText(tr("Unlock database"));
}
void UnlockDatabaseWidget::clearForms()
{
m_ui->editPassword->clear();
m_ui->comboKeyFile->clear();
m_ui->checkPassword->setChecked(false);
m_ui->checkKeyFile->setChecked(false);
m_ui->checkChallengeResponse->setChecked(false);
m_ui->buttonTogglePassword->setChecked(false);
m_db = nullptr;
}

View file

@ -26,7 +26,6 @@ class UnlockDatabaseWidget : public DatabaseOpenWidget
public:
explicit UnlockDatabaseWidget(QWidget* parent = nullptr);
void clearForms();
};
#endif // KEEPASSX_UNLOCKDATABASEWIDGET_H

View file

@ -56,7 +56,7 @@ void CsvImportWizard::load(const QString& filename, Database* database)
void CsvImportWizard::keyFinished(bool accepted)
{
if (!accepted) {
emit(importFinished(false));
emit importFinished(false);
return;
}
@ -68,11 +68,11 @@ void CsvImportWizard::keyFinished(bool accepted)
if (!result) {
MessageBox::critical(this, tr("Error"), tr("Unable to calculate master key"));
emit(importFinished(false));
emit importFinished(false);
}
}
void CsvImportWizard::parseFinished(bool accepted)
{
emit(importFinished(accepted));
emit importFinished(accepted);
}

View file

@ -429,6 +429,7 @@ void EditEntryWidget::saveEntry()
// must stand before beginUpdate()
// we don't want to create a new history item, if only the history has changed
m_entry->removeHistoryItems(m_historyModel->deletedEntries());
m_historyModel->clearDeletedEntries();
m_autoTypeAssoc->removeEmpty();
@ -445,6 +446,12 @@ void EditEntryWidget::saveEntry()
void EditEntryWidget::acceptEntry()
{
// Check if passwords are mismatched first to prevent saving
if (!passwordsEqual()) {
showMessage(tr("Different passwords supplied."), MessageWidget::Error);
return;
}
saveEntry();
clear();
emit editFinished(true);
@ -912,8 +919,7 @@ void EditEntryWidget::deleteHistoryEntry()
m_historyModel->deleteIndex(index);
if (m_historyModel->rowCount() > 0) {
m_historyUi->deleteAllButton->setEnabled(true);
}
else {
} else {
m_historyUi->deleteAllButton->setEnabled(false);
}
}

View file

@ -115,6 +115,11 @@ void EntryHistoryModel::clear()
endResetModel();
}
void EntryHistoryModel::clearDeletedEntries()
{
m_deletedHistoryEntries.clear();
}
QList<Entry*> EntryHistoryModel::deletedEntries()
{
return m_deletedHistoryEntries;

View file

@ -37,6 +37,7 @@ public:
void setEntries(const QList<Entry*>& entries);
void clear();
void clearDeletedEntries();
QList<Entry*> deletedEntries();
void deleteIndex(QModelIndex index);
void deleteAll();

View file

@ -151,7 +151,8 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const
}
return result;
case Url:
result = entry->resolveMultiplePlaceholders(entry->url());
result = entry->maskPasswordPlaceholders(entry->url());
result = entry->resolveMultiplePlaceholders(result);
if (attr->isReference(EntryAttributes::URLKey)) {
result.prepend(tr("Ref: ","Reference abbreviation"));
}

View file

@ -50,7 +50,7 @@ QByteArray YkChallengeResponseKey::rawKey() const
*/
bool YkChallengeResponseKey::challenge(const QByteArray& challenge)
{
return this->challenge(challenge, 1);
return this->challenge(challenge, 2);
}
bool YkChallengeResponseKey::challenge(const QByteArray& challenge, unsigned retries)
@ -70,8 +70,8 @@ bool YkChallengeResponseKey::challenge(const QByteArray& challenge, unsigned ret
QEventLoop loop;
QFutureWatcher<YubiKey::ChallengeResult> watcher;
watcher.setFuture(future);
connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
watcher.setFuture(future);
loop.exec();
if (m_blocking) {

View file

@ -154,7 +154,7 @@ YubiKey::ChallengeResult YubiKey::challenge(int slot, bool mayBlock, const QByte
QByteArray paddedChallenge = challenge;
// ensure that YubiKey::init() succeeded
if (m_yk == NULL) {
if (!init()) {
m_mutex.unlock();
return ERROR;
}

View file

@ -1,68 +0,0 @@
// Base32 implementation
// Source: https://github.com/google/google-authenticator-libpam/blob/master/src/base32.c
//
// Copyright 2010 Google Inc.
// Author: Markus Gutschke
// Modifications Copyright 2017 KeePassXC team <team@keepassxc.org>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "base32.h"
Base32::Base32()
{
}
QByteArray Base32::base32_decode(const QByteArray encoded)
{
QByteArray result;
int buffer = 0;
int bitsLeft = 0;
for (char ch : encoded) {
if (ch == 0 || ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-' || ch == '=') {
continue;
}
buffer <<= 5;
// Deal with commonly mistyped characters
if (ch == '0') {
ch = 'O';
} else if (ch == '1') {
ch = 'L';
} else if (ch == '8') {
ch = 'B';
}
// Look up one base32 digit
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
ch = (ch & 0x1F) - 1;
} else if (ch >= '2' && ch <= '7') {
ch -= '2' - 26;
} else {
return QByteArray();
}
buffer |= ch;
bitsLeft += 5;
if (bitsLeft >= 8) {
result.append(static_cast<char> (buffer >> (bitsLeft - 8)));
bitsLeft -= 8;
}
}
return result;
}

View file

@ -1,34 +0,0 @@
// Base32 implementation
// Source: https://github.com/google/google-authenticator-libpam/blob/master/src/base32.h
//
// Copyright 2010 Google Inc.
// Author: Markus Gutschke
// Modifications Copyright 2017 KeePassXC team <team@keepassxc.org>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef BASE32_H
#define BASE32_H
#include <QtCore/qglobal.h>
#include <QByteArray>
class Base32
{
public:
Base32();
static QByteArray base32_decode(const QByteArray encoded);
};
#endif //BASE32_H

View file

@ -17,7 +17,7 @@
*/
#include "totp.h"
#include "base32.h"
#include "core/Base32.h"
#include <cmath>
#include <QtEndian>
#include <QRegExp>
@ -98,13 +98,13 @@ QString QTotp::generateTotp(const QByteArray key, quint64 time,
{
quint64 current = qToBigEndian(time / step);
QByteArray secret = Base32::base32_decode(key);
if (secret.isEmpty()) {
Optional<QByteArray> secret = Base32::decode(key);
if (!secret.hasValue()) {
return "Invalid TOTP secret key";
}
QMessageAuthenticationCode code(QCryptographicHash::Sha1);
code.setKey(secret);
code.setKey(secret.valueOr(""));
code.addData(QByteArray(reinterpret_cast<char*>(&current), sizeof(current)));
QByteArray hmac = code.result();