mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-05-24 09:11:11 -04:00
SSH Agent: Show error messages if something fails
This commit is contained in:
parent
4ce0005711
commit
084758908a
5 changed files with 69 additions and 13 deletions
|
@ -58,6 +58,11 @@ void SSHAgent::init(QObject* parent)
|
|||
m_instance = new SSHAgent(parent);
|
||||
}
|
||||
|
||||
const QString SSHAgent::errorString() const
|
||||
{
|
||||
return m_error;
|
||||
}
|
||||
|
||||
bool SSHAgent::isAgentRunning() const
|
||||
{
|
||||
#ifndef Q_OS_WIN
|
||||
|
@ -67,7 +72,7 @@ bool SSHAgent::isAgentRunning() const
|
|||
#endif
|
||||
}
|
||||
|
||||
bool SSHAgent::sendMessage(const QByteArray& in, QByteArray& out) const
|
||||
bool SSHAgent::sendMessage(const QByteArray& in, QByteArray& out)
|
||||
{
|
||||
#ifndef Q_OS_WIN
|
||||
QLocalSocket socket;
|
||||
|
@ -75,6 +80,7 @@ bool SSHAgent::sendMessage(const QByteArray& in, QByteArray& out) const
|
|||
|
||||
socket.connectToServer(m_socketPath);
|
||||
if (!socket.waitForConnected(500)) {
|
||||
m_error = tr("Agent connection failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -82,6 +88,7 @@ bool SSHAgent::sendMessage(const QByteArray& in, QByteArray& out) const
|
|||
stream.flush();
|
||||
|
||||
if (!stream.readString(out)) {
|
||||
m_error = tr("Agent protocol error.");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -92,10 +99,12 @@ bool SSHAgent::sendMessage(const QByteArray& in, QByteArray& out) const
|
|||
HWND hWnd = FindWindowA("Pageant", "Pageant");
|
||||
|
||||
if (!hWnd) {
|
||||
m_error = tr("Agent connection failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (static_cast<quint32>(in.length()) > AGENT_MAX_MSGLEN - 4) {
|
||||
m_error = tr("Agent connection failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -104,6 +113,7 @@ bool SSHAgent::sendMessage(const QByteArray& in, QByteArray& out) const
|
|||
HANDLE handle = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, AGENT_MAX_MSGLEN, mapName.data());
|
||||
|
||||
if (!handle) {
|
||||
m_error = tr("Agent connection failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -111,6 +121,7 @@ bool SSHAgent::sendMessage(const QByteArray& in, QByteArray& out) const
|
|||
|
||||
if (!ptr) {
|
||||
CloseHandle(handle);
|
||||
m_error = tr("Agent connection failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -132,7 +143,11 @@ bool SSHAgent::sendMessage(const QByteArray& in, QByteArray& out) const
|
|||
if (responseLength <= AGENT_MAX_MSGLEN) {
|
||||
out.resize(responseLength);
|
||||
memcpy(out.data(), requestData, responseLength);
|
||||
} else {
|
||||
m_error = tr("Agent protocol error.");
|
||||
}
|
||||
} else {
|
||||
m_error = tr("Agent protocol error.");
|
||||
}
|
||||
|
||||
UnmapViewOfFile(ptr);
|
||||
|
@ -143,8 +158,13 @@ bool SSHAgent::sendMessage(const QByteArray& in, QByteArray& out) const
|
|||
}
|
||||
|
||||
|
||||
bool SSHAgent::addIdentity(OpenSSHKey& key, quint32 lifetime, bool confirm) const
|
||||
bool SSHAgent::addIdentity(OpenSSHKey& key, quint32 lifetime, bool confirm)
|
||||
{
|
||||
if (!isAgentRunning()) {
|
||||
m_error = tr("No agent running, cannot add identity.");
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray requestData;
|
||||
BinaryStream request(&requestData);
|
||||
|
||||
|
@ -161,17 +181,25 @@ bool SSHAgent::addIdentity(OpenSSHKey& key, quint32 lifetime, bool confirm) cons
|
|||
}
|
||||
|
||||
QByteArray responseData;
|
||||
sendMessage(requestData, responseData);
|
||||
if (!sendMessage(requestData, responseData)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (responseData.length() < 1 || static_cast<quint8>(responseData[0]) != SSH_AGENT_SUCCESS) {
|
||||
m_error = tr("Agent refused this identity.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SSHAgent::removeIdentity(OpenSSHKey& key) const
|
||||
bool SSHAgent::removeIdentity(OpenSSHKey& key)
|
||||
{
|
||||
if (!isAgentRunning()) {
|
||||
m_error = tr("No agent running, cannot remove identity.");
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray requestData;
|
||||
BinaryStream request(&requestData);
|
||||
|
||||
|
@ -183,9 +211,12 @@ bool SSHAgent::removeIdentity(OpenSSHKey& key) const
|
|||
request.writeString(keyData);
|
||||
|
||||
QByteArray responseData;
|
||||
sendMessage(requestData, responseData);
|
||||
if (!sendMessage(requestData, responseData)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (responseData.length() < 1 || static_cast<quint8>(responseData[0]) != SSH_AGENT_SUCCESS) {
|
||||
m_error = tr("Agent does not have this identity.");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -210,9 +241,12 @@ void SSHAgent::databaseModeChanged(DatabaseWidget::Mode mode)
|
|||
Uuid uuid = widget->database()->uuid();
|
||||
|
||||
if (mode == DatabaseWidget::LockedMode && m_keys.contains(uuid.toHex())) {
|
||||
|
||||
QSet<OpenSSHKey> keys = m_keys.take(uuid.toHex());
|
||||
for (OpenSSHKey key : keys) {
|
||||
removeIdentity(key);
|
||||
if (!removeIdentity(key)) {
|
||||
emit error(m_error);
|
||||
}
|
||||
}
|
||||
} else if (mode == DatabaseWidget::ViewMode && !m_keys.contains(uuid.toHex())) {
|
||||
for (Entry* e : widget->database()->rootGroup()->entriesRecursive()) {
|
||||
|
@ -279,7 +313,9 @@ void SSHAgent::databaseModeChanged(DatabaseWidget::Mode mode)
|
|||
lifetime = settings.lifetimeConstraintDuration();
|
||||
}
|
||||
|
||||
addIdentity(key, lifetime, settings.useConfirmConstraintWhenAdding());
|
||||
if (!addIdentity(key, lifetime, settings.useConfirmConstraintWhenAdding())) {
|
||||
emit error(m_error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,11 +33,15 @@ public:
|
|||
static SSHAgent* instance();
|
||||
static void init(QObject* parent);
|
||||
|
||||
const QString errorString() const;
|
||||
bool isAgentRunning() const;
|
||||
bool addIdentity(OpenSSHKey& key, quint32 lifetime = 0, bool confirm = false) const;
|
||||
bool removeIdentity(OpenSSHKey& key) const;
|
||||
bool addIdentity(OpenSSHKey& key, quint32 lifetime = 0, bool confirm = false);
|
||||
bool removeIdentity(OpenSSHKey& key);
|
||||
void removeIdentityAtLock(const OpenSSHKey& key, const Uuid& uuid);
|
||||
|
||||
signals:
|
||||
void error(const QString& message);
|
||||
|
||||
public slots:
|
||||
void databaseModeChanged(DatabaseWidget::Mode mode = DatabaseWidget::LockedMode);
|
||||
|
||||
|
@ -56,7 +60,7 @@ private:
|
|||
explicit SSHAgent(QObject* parent = nullptr);
|
||||
~SSHAgent();
|
||||
|
||||
bool sendMessage(const QByteArray& in, QByteArray& out) const;
|
||||
bool sendMessage(const QByteArray& in, QByteArray& out);
|
||||
|
||||
static SSHAgent* m_instance;
|
||||
|
||||
|
@ -68,6 +72,7 @@ private:
|
|||
#endif
|
||||
|
||||
QMap<QString, QSet<OpenSSHKey>> m_keys;
|
||||
QString m_error;
|
||||
};
|
||||
|
||||
#endif // AGENTCLIENT_H
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue