SSH Agent: Add ssh-add -D function (#8346)

This commit is contained in:
Alexandre Petit 2024-04-27 23:22:08 +02:00
parent 166a371050
commit 1b03b5f079
No known key found for this signature in database
9 changed files with 95 additions and 1 deletions

View File

@ -831,6 +831,16 @@ void DatabaseWidget::removeFromAgent()
m_messageWidget->showMessage(settings.errorString(), MessageWidget::Error);
}
}
void DatabaseWidget::flushSSHAgent()
{
SSHAgent* agent = SSHAgent::instance();
if (!agent->flushAllAgentIdentities()) {
showMessage(agent->errorString(), MessageWidget::Error);
} else {
showMessage(agent->errorString(), MessageWidget::Positive);
}
}
#endif
void DatabaseWidget::performAutoType(const QString& sequence)

View File

@ -200,6 +200,7 @@ public slots:
#ifdef WITH_XC_SSHAGENT
void addToAgent();
void removeFromAgent();
void flushSSHAgent();
#endif
void performAutoType(const QString& sequence = {});
void performAutoTypeUsername();

View File

@ -420,6 +420,7 @@ MainWindow::MainWindow()
m_ui->actionSettings->setIcon(icons()->icon("configure"));
m_ui->actionPasswordGenerator->setIcon(icons()->icon("password-generator"));
m_ui->actionFlushSSHAgent->setIcon(icons()->icon("utilities-terminal"));
m_ui->actionAbout->setIcon(icons()->icon("help-about"));
m_ui->actionDonate->setIcon(icons()->icon("donate"));
@ -533,6 +534,7 @@ MainWindow::MainWindow()
#ifdef WITH_XC_SSHAGENT
m_actionMultiplexer.connect(m_ui->actionEntryAddToAgent, SIGNAL(triggered()), SLOT(addToAgent()));
m_actionMultiplexer.connect(m_ui->actionEntryRemoveFromAgent, SIGNAL(triggered()), SLOT(removeFromAgent()));
m_actionMultiplexer.connect(m_ui->actionFlushSSHAgent, SIGNAL(triggered()), SLOT(flushSSHAgent()));
#endif
m_actionMultiplexer.connect(m_ui->actionGroupNew, SIGNAL(triggered()), SLOT(createGroup()));
@ -990,6 +992,8 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
m_ui->actionEntryAddToAgent->setEnabled(singleEntryHasSshKey);
m_ui->actionEntryRemoveFromAgent->setVisible(singleEntryHasSshKey);
m_ui->actionEntryRemoveFromAgent->setEnabled(singleEntryHasSshKey);
m_ui->actionFlushSSHAgent->setVisible(true);
m_ui->actionFlushSSHAgent->setEnabled(true);
#endif
m_searchWidgetAction->setEnabled(true);
@ -1663,6 +1667,7 @@ void MainWindow::agentEnabled(bool enabled)
{
m_ui->actionEntryAddToAgent->setVisible(enabled);
m_ui->actionEntryRemoveFromAgent->setVisible(enabled);
m_ui->actionFlushSSHAgent->setVisible(enabled);
}
void MainWindow::showEntryContextMenu(const QPoint& globalPos)
@ -2147,6 +2152,7 @@ void MainWindow::initActionCollection()
m_ui->actionGroupEmptyRecycleBin,
// Tools Menu
m_ui->actionPasswordGenerator,
m_ui->actionFlushSSHAgent,
m_ui->actionSettings,
// View Menu
m_ui->actionThemeAuto,

View File

@ -372,6 +372,7 @@
<string>&amp;Tools</string>
</property>
<addaction name="actionPasswordGenerator"/>
<addaction name="actionFlushSSHAgent"/>
<addaction name="actionSettings"/>
</widget>
<widget class="QMenu" name="menuView">
@ -1394,6 +1395,17 @@
<string>Import…</string>
</property>
</action>
<action name="actionFlushSSHAgent">
<property name="text">
<string>Flush SSH Agent</string>
</property>
<property name="toolTip">
<string>Flush identities like ssh-add -D</string>
</property>
<property name="menuRole">
<enum>QAction::TextHeuristicRole</enum>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

View File

@ -565,6 +565,7 @@ void EditEntryWidget::setupSSHAgent()
connect(m_sshAgentUi->browseButton, &QPushButton::clicked, this, &EditEntryWidget::browsePrivateKey);
connect(m_sshAgentUi->addToAgentButton, &QPushButton::clicked, this, &EditEntryWidget::addKeyToAgent);
connect(m_sshAgentUi->removeFromAgentButton, &QPushButton::clicked, this, &EditEntryWidget::removeKeyFromAgent);
connect(m_sshAgentUi->flushAgentButton, &QPushButton::clicked, this, &EditEntryWidget::flushAgent);
connect(m_sshAgentUi->decryptButton, &QPushButton::clicked, this, &EditEntryWidget::decryptPrivateKey);
connect(m_sshAgentUi->copyToClipboardButton, &QPushButton::clicked, this, &EditEntryWidget::copyPublicKey);
connect(m_sshAgentUi->generateButton, &QPushButton::clicked, this, &EditEntryWidget::generatePrivateKey);
@ -678,6 +679,7 @@ void EditEntryWidget::updateSSHAgentKeyInfo()
if (sshAgent()->isAgentRunning()) {
m_sshAgentUi->addToAgentButton->setEnabled(true);
m_sshAgentUi->removeFromAgentButton->setEnabled(true);
m_sshAgentUi->flushAgentButton->setEnabled(true);
sshAgent()->setAutoRemoveOnLock(key, m_sshAgentUi->removeKeyFromAgentCheckBox->isChecked());
}
@ -780,6 +782,16 @@ void EditEntryWidget::removeKeyFromAgent()
}
}
void EditEntryWidget::flushAgent()
{
if (!sshAgent()->flushAllAgentIdentities()) {
showMessage(sshAgent()->errorString(), MessageWidget::Error);
return;
}
showMessage(sshAgent()->errorString(), MessageWidget::Positive);
}
void EditEntryWidget::decryptPrivateKey()
{
OpenSSHKey key;

View File

@ -122,6 +122,7 @@ private slots:
void browsePrivateKey();
void addKeyToAgent();
void removeKeyFromAgent();
void flushAgent();
void decryptPrivateKey();
void copyPublicKey();
void generatePrivateKey();

View File

@ -139,7 +139,7 @@
</widget>
</item>
<item row="4" column="3">
<layout class="QHBoxLayout" name="agentActionsLayout" stretch="0,0">
<layout class="QHBoxLayout" name="agentActionsLayout" stretch="0,0,0">
<item>
<widget class="QPushButton" name="addToAgentButton">
<property name="text">
@ -154,6 +154,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="flushAgentButton">
<property name="text">
<string>Flush agent</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="0">

View File

@ -363,6 +363,48 @@ bool SSHAgent::removeIdentity(OpenSSHKey& key)
return sendMessage(requestData, responseData);
}
/**
* Flush all identities from the SSH agent.
*
* Since the agent might be forwarded, old or non-OpenSSH, when asked
* to remove all keys, attempt to remove both protocol v.1 and v.2
* keys.
*
* @return true on success
*/
bool SSHAgent::flushAllAgentIdentities()
{
if (!isAgentRunning()) {
m_error = tr("No agent running, cannot remove identity.");
return false;
}
bool ret = true;
QByteArray requestData;
QByteArray responseData;
BinaryStream request(&requestData);
// Same request order as OpenBSD ssh-add: useful?
request.write(SSH2_AGENTC_REMOVE_ALL_IDENTITIES);
if (!sendMessage(requestData, responseData)) {
m_error = tr("Failed to remove all SSH identities from agent.");
ret = false;
}
request.flush();
responseData.clear();
// Same request order as OpenBSD ssh-add: useful?
request.write(SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES);
// ignore error-code for ssh1
sendMessage(requestData, responseData);
m_error = tr("All SSH identities removed from agent.");
return ret;
}
/**
* Get a list of identities from the SSH agent.
*

View File

@ -56,6 +56,7 @@ public:
bool checkIdentity(const OpenSSHKey& key, bool& loaded);
bool removeIdentity(OpenSSHKey& key);
void removeAllIdentities();
bool flushAllAgentIdentities();
void setAutoRemoveOnLock(const OpenSSHKey& key, bool autoRemove);
signals:
@ -74,6 +75,8 @@ private:
const quint8 SSH_AGENTC_ADD_IDENTITY = 17;
const quint8 SSH_AGENTC_REMOVE_IDENTITY = 18;
const quint8 SSH_AGENTC_ADD_ID_CONSTRAINED = 25;
const quint8 SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES = 9;
const quint8 SSH2_AGENTC_REMOVE_ALL_IDENTITIES = 19;
const quint8 SSH_AGENT_CONSTRAIN_LIFETIME = 1;
const quint8 SSH_AGENT_CONSTRAIN_CONFIRM = 2;