tdeioslave/sftp: use a scope guards to close connection

There were a couple of missing closeConnection() calls after connection
errors. The probably haven't caused any major bugs, but use scope guards
to be on the safe side.

Signed-off-by: Alexander Golubev <fatzer2@gmail.com>
pull/447/head
Alexander Golubev 4 months ago committed by TDE Gitea
parent 75349be43e
commit 9c0a0ce976

@ -114,6 +114,10 @@ public:
if(f) { f(); f = nullptr; } if(f) { f(); f = nullptr; }
} }
void abort() {
f = nullptr;
}
ExitGuard(const ExitGuard&) = delete; ExitGuard(const ExitGuard&) = delete;
void operator= (const ExitGuard&) = delete; void operator= (const ExitGuard&) = delete;
@ -718,10 +722,11 @@ int sftpProtocol::initializeConnection() {
rc = ssh_connect(mSession); rc = ssh_connect(mSession);
if (rc < 0) { if (rc < 0) {
error(TDEIO::ERR_COULD_NOT_CONNECT, TQString::fromUtf8(ssh_get_error(mSession))); error(TDEIO::ERR_COULD_NOT_CONNECT, TQString::fromUtf8(ssh_get_error(mSession)));
closeConnection();
return rc; return rc;
} }
ExitGuard connectionCloser([this](){ closeConnection(); });
kdDebug(TDEIO_SFTP_DB) << "Getting the SSH server hash" << endl; kdDebug(TDEIO_SFTP_DB) << "Getting the SSH server hash" << endl;
/* get the hash */ /* get the hash */
@ -733,7 +738,6 @@ int sftpProtocol::initializeConnection() {
#endif #endif
if (rc<0) { if (rc<0) {
error(TDEIO::ERR_COULD_NOT_CONNECT, TQString::fromUtf8(ssh_get_error(mSession))); error(TDEIO::ERR_COULD_NOT_CONNECT, TQString::fromUtf8(ssh_get_error(mSession)));
closeConnection();
return rc; return rc;
} }
@ -745,7 +749,6 @@ int sftpProtocol::initializeConnection() {
#endif #endif
if (rc<0) { if (rc<0) {
error(TDEIO::ERR_COULD_NOT_CONNECT, TQString::fromUtf8(ssh_get_error(mSession))); error(TDEIO::ERR_COULD_NOT_CONNECT, TQString::fromUtf8(ssh_get_error(mSession)));
closeConnection();
return rc; return rc;
} }
@ -767,7 +770,6 @@ int sftpProtocol::initializeConnection() {
"An attacker might change the default server key to confuse your " "An attacker might change the default server key to confuse your "
"client into thinking the key does not exist.\n" "client into thinking the key does not exist.\n"
"Please contact your system administrator.\n%1").arg(TQString::fromUtf8(ssh_get_error(mSession)))); "Please contact your system administrator.\n%1").arg(TQString::fromUtf8(ssh_get_error(mSession))));
closeConnection();
return SSH_ERROR; return SSH_ERROR;
case TDEIO_SSH_KNOWN_HOSTS_CHANGED: case TDEIO_SSH_KNOWN_HOSTS_CHANGED:
hexa = ssh_get_hexa(hash, hlen); hexa = ssh_get_hexa(hash, hlen);
@ -780,7 +782,6 @@ int sftpProtocol::initializeConnection() {
"Please contact your system administrator.\n%3").arg( "Please contact your system administrator.\n%3").arg(
mHost).arg(TQString::fromUtf8(hexa)).arg(TQString::fromUtf8(ssh_get_error(mSession)))); mHost).arg(TQString::fromUtf8(hexa)).arg(TQString::fromUtf8(ssh_get_error(mSession))));
delete hexa; delete hexa;
closeConnection();
return SSH_ERROR; return SSH_ERROR;
case TDEIO_SSH_KNOWN_HOSTS_NOT_FOUND: case TDEIO_SSH_KNOWN_HOSTS_NOT_FOUND:
case TDEIO_SSH_KNOWN_HOSTS_UNKNOWN: case TDEIO_SSH_KNOWN_HOSTS_UNKNOWN:
@ -793,7 +794,6 @@ int sftpProtocol::initializeConnection() {
delete hexa; delete hexa;
if (KMessageBox::Yes != messageBox(WarningYesNo, msg, caption)) { if (KMessageBox::Yes != messageBox(WarningYesNo, msg, caption)) {
closeConnection();
error(TDEIO::ERR_USER_CANCELED, TQString()); error(TDEIO::ERR_USER_CANCELED, TQString());
return SSH_ERROR; return SSH_ERROR;
} }
@ -806,7 +806,6 @@ int sftpProtocol::initializeConnection() {
if (ssh_session_update_known_hosts(mSession) != SSH_OK) { if (ssh_session_update_known_hosts(mSession) != SSH_OK) {
#endif #endif
error(TDEIO::ERR_USER_CANCELED, TQString::fromUtf8(ssh_get_error(mSession))); error(TDEIO::ERR_USER_CANCELED, TQString::fromUtf8(ssh_get_error(mSession)));
closeConnection();
return SSH_ERROR; return SSH_ERROR;
} }
break; break;
@ -829,6 +828,8 @@ int sftpProtocol::initializeConnection() {
ssh_string_free_char(ssh_username); ssh_string_free_char(ssh_username);
} }
connectionCloser.abort();
return SSH_OK; return SSH_OK;
} }
@ -876,12 +877,13 @@ void sftpProtocol::openConnection() {
return; return;
} }
ExitGuard connectionCloser([this](){ closeConnection(); });
// Try to authenticate // Try to authenticate
rc = ssh_userauth_none(mSession, NULL); rc = ssh_userauth_none(mSession, NULL);
if (rc == SSH_AUTH_ERROR) { if (rc == SSH_AUTH_ERROR) {
closeConnection(); error(TDEIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed (method: %1).")
error(TDEIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed (method: %1).") .arg(i18n("none")));
.arg(i18n("none")));
return; return;
} }
@ -924,8 +926,6 @@ void sftpProtocol::openConnection() {
case SSH_AUTH_AGAIN: case SSH_AUTH_AGAIN:
// Returned in case of some errors like if server hangs up or there were too many auth attempts // Returned in case of some errors like if server hangs up or there were too many auth attempts
case SSH_AUTH_ERROR: case SSH_AUTH_ERROR:
closeConnection();
/* FIXME: Use scope guard to close connection <2024-01-20 Fat-Zer> */
error(TDEIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed (method: %1).") error(TDEIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed (method: %1).")
.arg(i18n("public key"))); .arg(i18n("public key")));
/* FIXME: add some additional info from ssh_get_error() if available <2024-01-20 Fat-Zer> */ /* FIXME: add some additional info from ssh_get_error() if available <2024-01-20 Fat-Zer> */
@ -961,7 +961,6 @@ void sftpProtocol::openConnection() {
} }
else if (rc == SSH_AUTH_ERROR) else if (rc == SSH_AUTH_ERROR)
{ {
closeConnection();
error(TDEIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed (method: %1).") error(TDEIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed (method: %1).")
.arg(i18n("keyboard interactive"))); .arg(i18n("keyboard interactive")));
return; return;
@ -988,7 +987,6 @@ void sftpProtocol::openConnection() {
// Handle user canceled or dialog failed to open... // Handle user canceled or dialog failed to open...
if (!dlgResult) { if (!dlgResult) {
kdDebug(TDEIO_SFTP_DB) << "User canceled, dlgResult = " << dlgResult << endl; kdDebug(TDEIO_SFTP_DB) << "User canceled, dlgResult = " << dlgResult << endl;
closeConnection();
error(TDEIO::ERR_USER_CANCELED, TQString()); error(TDEIO::ERR_USER_CANCELED, TQString());
return; return;
} }
@ -1006,7 +1004,6 @@ void sftpProtocol::openConnection() {
rc = ssh_userauth_password(mSession, mUsername.utf8().data(), rc = ssh_userauth_password(mSession, mUsername.utf8().data(),
mPassword.utf8().data()); mPassword.utf8().data());
if (rc == SSH_AUTH_ERROR) { if (rc == SSH_AUTH_ERROR) {
closeConnection();
error(TDEIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed (method: %1).") error(TDEIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed (method: %1).")
.arg(i18n("password"))); .arg(i18n("password")));
return; return;
@ -1021,7 +1018,6 @@ void sftpProtocol::openConnection() {
kdDebug(TDEIO_SFTP_DB) << "Trying to request the sftp session" << endl; kdDebug(TDEIO_SFTP_DB) << "Trying to request the sftp session" << endl;
mSftp = sftp_new(mSession); mSftp = sftp_new(mSession);
if (mSftp == NULL) { if (mSftp == NULL) {
closeConnection();
error(TDEIO::ERR_COULD_NOT_LOGIN, i18n("Unable to request the SFTP subsystem. " error(TDEIO::ERR_COULD_NOT_LOGIN, i18n("Unable to request the SFTP subsystem. "
"Make sure SFTP is enabled on the server.")); "Make sure SFTP is enabled on the server."));
return; return;
@ -1029,7 +1025,6 @@ void sftpProtocol::openConnection() {
kdDebug(TDEIO_SFTP_DB) << "Trying to initialize the sftp session" << endl; kdDebug(TDEIO_SFTP_DB) << "Trying to initialize the sftp session" << endl;
if (sftp_init(mSftp) < 0) { if (sftp_init(mSftp) < 0) {
closeConnection();
error(TDEIO::ERR_COULD_NOT_LOGIN, i18n("Could not initialize the SFTP session.")); error(TDEIO::ERR_COULD_NOT_LOGIN, i18n("Could not initialize the SFTP session."));
return; return;
} }
@ -1051,6 +1046,8 @@ void sftpProtocol::openConnection() {
//setTimeoutSpecialCommand(TDEIO_SFTP_SPECIAL_TIMEOUT); //setTimeoutSpecialCommand(TDEIO_SFTP_SPECIAL_TIMEOUT);
mConnected = true; mConnected = true;
connectionCloser.abort();
connected(); connected();
return; return;

Loading…
Cancel
Save