From cefa88032bb63c28715dac30f85a44aa07200782 Mon Sep 17 00:00:00 2001 From: Toni Spets Date: Mon, 13 Apr 2020 08:30:02 +0300 Subject: [PATCH] SSH Agent: Reset settings when KeeAgent.settings is removed Fixes #4594 --- src/gui/DatabaseWidget.cpp | 2 +- src/gui/entry/EditEntryWidget.cpp | 29 ++++++++++++++++++---------- src/gui/entry/EditEntryWidget.h | 1 + src/sshagent/KeeAgentSettings.cpp | 32 +++++++++++++++++++++++++++---- src/sshagent/KeeAgentSettings.h | 20 ++++++++++--------- 5 files changed, 60 insertions(+), 24 deletions(-) diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 7664c64b37..5717eaca25 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -1758,7 +1758,7 @@ bool DatabaseWidget::currentEntryHasSshKey() return false; } - return KeeAgentSettings::inEntry(currentEntry); + return KeeAgentSettings::inEntryAttachments(currentEntry->attachments()); } #endif diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index 2036a39ba2..8274968b7d 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -532,22 +532,24 @@ void EditEntryWidget::setupSSHAgent() addPage(tr("SSH Agent"), Resources::instance()->icon("utilities-terminal"), m_sshAgentWidget); } -void EditEntryWidget::updateSSHAgent() +void EditEntryWidget::setSSHAgentSettings() { - KeeAgentSettings settings; - settings.fromEntry(m_entry); - - m_sshAgentUi->addKeyToAgentCheckBox->setChecked(settings.addAtDatabaseOpen()); - m_sshAgentUi->removeKeyFromAgentCheckBox->setChecked(settings.removeAtDatabaseClose()); - m_sshAgentUi->requireUserConfirmationCheckBox->setChecked(settings.useConfirmConstraintWhenAdding()); - m_sshAgentUi->lifetimeCheckBox->setChecked(settings.useLifetimeConstraintWhenAdding()); - m_sshAgentUi->lifetimeSpinBox->setValue(settings.lifetimeConstraintDuration()); + m_sshAgentUi->addKeyToAgentCheckBox->setChecked(m_sshAgentSettings.addAtDatabaseOpen()); + m_sshAgentUi->removeKeyFromAgentCheckBox->setChecked(m_sshAgentSettings.removeAtDatabaseClose()); + m_sshAgentUi->requireUserConfirmationCheckBox->setChecked(m_sshAgentSettings.useConfirmConstraintWhenAdding()); + m_sshAgentUi->lifetimeCheckBox->setChecked(m_sshAgentSettings.useLifetimeConstraintWhenAdding()); + m_sshAgentUi->lifetimeSpinBox->setValue(m_sshAgentSettings.lifetimeConstraintDuration()); m_sshAgentUi->attachmentComboBox->clear(); m_sshAgentUi->addToAgentButton->setEnabled(false); m_sshAgentUi->removeFromAgentButton->setEnabled(false); m_sshAgentUi->copyToClipboardButton->setEnabled(false); +} - m_sshAgentSettings = settings; +void EditEntryWidget::updateSSHAgent() +{ + m_sshAgentSettings.reset(); + m_sshAgentSettings.fromEntry(m_entry); + setSSHAgentSettings(); updateSSHAgentAttachments(); } @@ -560,6 +562,13 @@ void EditEntryWidget::updateSSHAgentAttachment() void EditEntryWidget::updateSSHAgentAttachments() { + // detect if KeeAgent.settings was removed by hand and reset settings + if (m_entry && KeeAgentSettings::inEntryAttachments(m_entry->attachments()) + && !KeeAgentSettings::inEntryAttachments(m_advancedUi->attachmentsWidget->entryAttachments())) { + m_sshAgentSettings.reset(); + setSSHAgentSettings(); + } + m_sshAgentUi->attachmentComboBox->clear(); m_sshAgentUi->attachmentComboBox->addItem(""); diff --git a/src/gui/entry/EditEntryWidget.h b/src/gui/entry/EditEntryWidget.h index e0a67b5eac..ea630d7ebe 100644 --- a/src/gui/entry/EditEntryWidget.h +++ b/src/gui/entry/EditEntryWidget.h @@ -111,6 +111,7 @@ private slots: void pickColor(); #ifdef WITH_XC_SSHAGENT void toKeeAgentSettings(KeeAgentSettings& settings) const; + void setSSHAgentSettings(); void updateSSHAgent(); void updateSSHAgentAttachment(); void updateSSHAgentAttachments(); diff --git a/src/sshagent/KeeAgentSettings.cpp b/src/sshagent/KeeAgentSettings.cpp index f88735b983..e9f6e4bdd4 100644 --- a/src/sshagent/KeeAgentSettings.cpp +++ b/src/sshagent/KeeAgentSettings.cpp @@ -19,6 +19,11 @@ #include "KeeAgentSettings.h" #include "core/Tools.h" +KeeAgentSettings::KeeAgentSettings() +{ + reset(); +} + bool KeeAgentSettings::operator==(const KeeAgentSettings& other) const { // clang-format off @@ -50,6 +55,25 @@ bool KeeAgentSettings::isDefault() const return (*this == defaultSettings); } +/** + * Reset this instance to default settings + */ +void KeeAgentSettings::reset() +{ + m_allowUseOfSshKey = false; + m_addAtDatabaseOpen = false; + m_removeAtDatabaseClose = false; + m_useConfirmConstraintWhenAdding = false; + m_useLifetimeConstraintWhenAdding = false; + m_lifetimeConstraintDuration = 600; + + m_selectedType = QStringLiteral("file"); + m_attachmentName.clear(); + m_saveAttachmentToTempFile = false; + m_fileName.clear(); + m_error.clear(); +} + /** * Get last error as a QString. * @@ -300,14 +324,14 @@ QByteArray KeeAgentSettings::toXml() const } /** - * Check if an entry has KeeAgent settings configured + * Check if entry attachments have KeeAgent settings configured * - * @param entry Entry to check the attachment + * @param attachments EntryAttachments to check the key * @return true if XML document exists */ -bool KeeAgentSettings::inEntry(const Entry* entry) +bool KeeAgentSettings::inEntryAttachments(const EntryAttachments* attachments) { - return entry->attachments()->hasKey("KeeAgent.settings"); + return attachments->hasKey("KeeAgent.settings"); } /** diff --git a/src/sshagent/KeeAgentSettings.h b/src/sshagent/KeeAgentSettings.h index ec6fd3ee79..3cc7016661 100644 --- a/src/sshagent/KeeAgentSettings.h +++ b/src/sshagent/KeeAgentSettings.h @@ -28,14 +28,16 @@ class KeeAgentSettings { public: + KeeAgentSettings(); bool operator==(const KeeAgentSettings& other) const; bool operator!=(const KeeAgentSettings& other) const; bool isDefault() const; + void reset(); bool fromXml(const QByteArray& ba); QByteArray toXml() const; - static bool inEntry(const Entry* entry); + static bool inEntryAttachments(const EntryAttachments* attachments); bool fromEntry(const Entry* entry); void toEntry(Entry* entry) const; bool keyConfigured() const; @@ -77,17 +79,17 @@ class KeeAgentSettings bool readBool(QXmlStreamReader& reader); int readInt(QXmlStreamReader& reader); - bool m_allowUseOfSshKey = false; - bool m_addAtDatabaseOpen = false; - bool m_removeAtDatabaseClose = false; - bool m_useConfirmConstraintWhenAdding = false; - bool m_useLifetimeConstraintWhenAdding = false; - int m_lifetimeConstraintDuration = 600; + bool m_allowUseOfSshKey; + bool m_addAtDatabaseOpen; + bool m_removeAtDatabaseClose; + bool m_useConfirmConstraintWhenAdding; + bool m_useLifetimeConstraintWhenAdding; + int m_lifetimeConstraintDuration; // location - QString m_selectedType = QString("file"); + QString m_selectedType; QString m_attachmentName; - bool m_saveAttachmentToTempFile = false; + bool m_saveAttachmentToTempFile; QString m_fileName; QString m_error; };