Skip to content

Commit

Permalink
SSH Agent: Reset settings when KeeAgent.settings is removed
Browse files Browse the repository at this point in the history
Fixes #4594
  • Loading branch information
hifi committed May 19, 2020
1 parent 3dc8b7a commit 33d901d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,7 @@ bool DatabaseWidget::currentEntryHasSshKey()
return false;
}

return KeeAgentSettings::inEntry(currentEntry);
return KeeAgentSettings::inEntryAttachments(currentEntry->attachments());
}
#endif

Expand Down
29 changes: 19 additions & 10 deletions src/gui/entry/EditEntryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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("");

Expand Down
1 change: 1 addition & 0 deletions src/gui/entry/EditEntryWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 13 additions & 4 deletions src/sshagent/KeeAgentSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ bool KeeAgentSettings::isDefault() const
return (*this == defaultSettings);
}

/**
* Reset this instance to default settings
*/
void KeeAgentSettings::reset()
{
KeeAgentSettings defaultSettings;
*this = defaultSettings;
}

/**
* Get last error as a QString.
*
Expand Down Expand Up @@ -300,14 +309,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");
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/sshagent/KeeAgentSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ class 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;
Expand Down

0 comments on commit 33d901d

Please sign in to comment.