SSH Agent: SSH_AUTH_SOCK override and conn test

Fixes #3795
This commit is contained in:
Toni Spets 2019-11-05 21:30:34 +02:00 committed by Jonathan White
parent 6fc7be78ea
commit 4dee16c9fa
4 changed files with 132 additions and 2 deletions

View File

@ -17,9 +17,11 @@
*/
#include "AgentSettingsWidget.h"
#include "SSHAgent.h"
#include "ui_AgentSettingsWidget.h"
#include "core/Config.h"
#include <QProcessEnvironment>
AgentSettingsWidget::AgentSettingsWidget(QWidget* parent)
: QWidget(parent)
@ -28,7 +30,13 @@ AgentSettingsWidget::AgentSettingsWidget(QWidget* parent)
m_ui->setupUi(this);
#ifndef Q_OS_WIN
m_ui->useOpenSSHCheckBox->setVisible(false);
#else
m_ui->sshAuthSockWidget->setVisible(false);
#endif
auto sshAgentEnabled = config()->get("SSHAgent", false).toBool();
m_ui->sshAuthSockMessageWidget->setVisible(sshAgentEnabled);
m_ui->sshAuthSockMessageWidget->setCloseButtonVisible(false);
m_ui->sshAuthSockMessageWidget->setAutoHideTimeout(-1);
}
AgentSettingsWidget::~AgentSettingsWidget()
@ -37,15 +45,42 @@ AgentSettingsWidget::~AgentSettingsWidget()
void AgentSettingsWidget::loadSettings()
{
m_ui->enableSSHAgentCheckBox->setChecked(config()->get("SSHAgent", false).toBool());
auto sshAgentEnabled = config()->get("SSHAgent", false).toBool();
m_ui->enableSSHAgentCheckBox->setChecked(sshAgentEnabled);
#ifdef Q_OS_WIN
m_ui->useOpenSSHCheckBox->setChecked(config()->get("SSHAgentOpenSSH", false).toBool());
#else
auto sshAuthSock = QProcessEnvironment::systemEnvironment().value("SSH_AUTH_SOCK");
auto sshAuthSockOverride = config()->get("SSHAuthSockOverride", "").toString();
m_ui->sshAuthSockLabel->setText(sshAuthSock.isEmpty() ? tr("(empty)") : sshAuthSock);
m_ui->sshAuthSockOverrideEdit->setText(sshAuthSockOverride);
#endif
if (sshAgentEnabled) {
m_ui->sshAuthSockMessageWidget->setVisible(true);
#ifndef Q_OS_WIN
if (sshAuthSock.isEmpty() && sshAuthSockOverride.isEmpty()) {
m_ui->sshAuthSockMessageWidget->showMessage(
tr("No SSH Agent socket available. Either make sure SSH_AUTH_SOCK environment variable exists or set "
"an override."),
MessageWidget::Warning);
return;
}
#endif
if (SSHAgent::instance()->testConnection()) {
m_ui->sshAuthSockMessageWidget->showMessage(tr("SSH Agent connection is working!"),
MessageWidget::Positive);
} else {
m_ui->sshAuthSockMessageWidget->showMessage(SSHAgent::instance()->errorString(), MessageWidget::Error);
}
}
}
void AgentSettingsWidget::saveSettings()
{
config()->set("SSHAgent", m_ui->enableSSHAgentCheckBox->isChecked());
config()->set("SSHAuthSockOverride", m_ui->sshAuthSockOverrideEdit->text());
#ifdef Q_OS_WIN
config()->set("SSHAgentOpenSSH", m_ui->useOpenSSHCheckBox->isChecked());
#endif

View File

@ -37,6 +37,59 @@
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="sshAuthSockWidget" native="true">
<layout class="QGridLayout" name="sshAuthSockOverrideLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="4" column="0">
<widget class="QLabel" name="sshAuthSockValueLabel">
<property name="text">
<string>SSH_AUTH_SOCK value</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLabel" name="sshAuthSockLabel">
<property name="font">
<font>
<family>Monospace</family>
</font>
</property>
<property name="text">
<string>(empty)</string>
</property>
<property name="textInteractionFlags">
<set>Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="sshAuthSockOverrideLabel">
<property name="text">
<string>SSH_AUTH_SOCK override</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLineEdit" name="sshAuthSockOverrideEdit"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="MessageWidget" name="sshAuthSockMessageWidget" native="true"/>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
@ -52,6 +105,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>MessageWidget</class>
<extends>QWidget</extends>
<header>gui/MessageWidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -35,7 +35,10 @@ SSHAgent::SSHAgent(QObject* parent)
: QObject(parent)
{
#ifndef Q_OS_WIN
m_socketPath = config()->get("SSHAuthSockOverride", "").toString();
if (m_socketPath.isEmpty()) {
m_socketPath = QProcessEnvironment::systemEnvironment().value("SSH_AUTH_SOCK");
}
#else
m_socketPath = "\\\\.\\pipe\\openssh-ssh-agent";
#endif
@ -181,6 +184,36 @@ bool SSHAgent::sendMessagePageant(const QByteArray& in, QByteArray& out)
}
#endif
/**
* Test if connection to SSH agent is working.
*
* @return true on success
*/
bool SSHAgent::testConnection()
{
if (!isAgentRunning()) {
m_error = tr("No agent running, cannot test connection.");
return false;
}
QByteArray requestData;
BinaryStream request(&requestData);
request.write(SSH_AGENTC_REQUEST_IDENTITIES);
QByteArray responseData;
if (!sendMessage(requestData, responseData)) {
return false;
}
if (responseData.length() < 1 || static_cast<quint8>(responseData[0]) != SSH_AGENT_IDENTITIES_ANSWER) {
m_error = tr("Agent protocol error.");
return false;
}
return true;
}
/**
* Add the identity to the SSH agent.
*

View File

@ -37,6 +37,7 @@ public:
const QString errorString() const;
bool isAgentRunning() const;
bool testConnection();
bool addIdentity(OpenSSHKey& key, KeeAgentSettings& settings);
bool removeIdentity(OpenSSHKey& key);
void setAutoRemoveOnLock(const OpenSSHKey& key, bool autoRemove);