SSH Agent: Use database location to resolve relative key file path

Closes #5225
This commit is contained in:
Toni Spets 2021-04-04 09:36:51 +03:00 committed by Jonathan White
parent ed0ece304d
commit 9b8feed3ed
3 changed files with 18 additions and 3 deletions

View File

@ -701,6 +701,7 @@ bool EditEntryWidget::getOpenSSHKey(OpenSSHKey& key, bool decrypt)
if (!settings.toOpenSSHKey(m_mainUi->usernameComboBox->lineEdit()->text(),
m_mainUi->passwordEdit->text(),
m_db->filePath(),
m_advancedUi->attachmentsWidget->entryAttachments(),
key,
decrypt)) {

View File

@ -17,6 +17,7 @@
*/
#include "KeeAgentSettings.h"
#include "core/Database.h"
#include "core/Tools.h"
KeeAgentSettings::KeeAgentSettings()
@ -389,7 +390,8 @@ bool KeeAgentSettings::keyConfigured() const
*/
bool KeeAgentSettings::toOpenSSHKey(const Entry* entry, OpenSSHKey& key, bool decrypt)
{
return toOpenSSHKey(entry->username(), entry->password(), entry->attachments(), key, decrypt);
return toOpenSSHKey(
entry->username(), entry->password(), entry->database()->filePath(), entry->attachments(), key, decrypt);
}
/**
@ -399,6 +401,7 @@ bool KeeAgentSettings::toOpenSSHKey(const Entry* entry, OpenSSHKey& key, bool de
*
* @param username username to set on key if empty
* @param password password to decrypt key if needed
* @param databasePath path to database file this key is loaded from
* @param attachments attachments to read an attachment key from
* @param key output key object
* @param decrypt avoid private key decryption if possible (old RSA keys are always decrypted)
@ -406,6 +409,7 @@ bool KeeAgentSettings::toOpenSSHKey(const Entry* entry, OpenSSHKey& key, bool de
*/
bool KeeAgentSettings::toOpenSSHKey(const QString& username,
const QString& password,
const QString& databasePath,
const EntryAttachments* attachments,
OpenSSHKey& key,
bool decrypt)
@ -423,10 +427,19 @@ bool KeeAgentSettings::toOpenSSHKey(const QString& username,
fileName = m_attachmentName;
privateKeyData = attachments->value(fileName);
} else {
QFile localFile(fileNameEnvSubst());
QFileInfo localFileInfo(localFile);
QString fileNameSubst = fileNameEnvSubst();
QFileInfo localFileInfo(fileNameSubst);
// resolve relative private key path from database location
if (localFileInfo.isRelative()) {
QFileInfo databaseFileInfo(databasePath);
localFileInfo = QFileInfo(databaseFileInfo.absolutePath() + QDir::separator() + fileNameSubst);
}
fileName = localFileInfo.fileName();
QFile localFile(localFileInfo.absoluteFilePath());
if (localFile.fileName().isEmpty()) {
m_error = QCoreApplication::translate("KeeAgentSettings", "Private key is empty");
return false;

View File

@ -44,6 +44,7 @@ public:
bool toOpenSSHKey(const Entry* entry, OpenSSHKey& key, bool decrypt);
bool toOpenSSHKey(const QString& username,
const QString& password,
const QString& databasePath,
const EntryAttachments* attachments,
OpenSSHKey& key,
bool decrypt);