sftp.c: Validate that the SSH session is in nonblocking mode

The sftp API functions cannot interoperate properly with a
nonblocking ssh session.

Therefore code has been added in sftp_new() due to which the
function will return failure if the caller passes a non
blocking session without even trying to connect.

Signed-off-by: Eshan Kelkar <eshankelkar@galorithm.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
This commit is contained in:
Eshan Kelkar
2025-01-17 21:59:21 +05:30
committed by Jakub Jelen
parent c03d0d4823
commit 1a64c577f6

View File

@@ -102,12 +102,21 @@ static void sftp_ext_free(sftp_ext ext)
sftp_session sftp_new(ssh_session session)
{
sftp_session sftp;
sftp_session sftp = NULL;
int rc;
if (session == NULL) {
return NULL;
}
if (!ssh_is_blocking(session)) {
ssh_set_error(session,
SSH_FATAL,
"The SSH session needs to be set to blocking mode for "
"SFTP to work correctly.");
return NULL;
}
sftp = calloc(1, sizeof(struct sftp_session_struct));
if (sftp == NULL) {
ssh_set_error_oom(session);
@@ -140,11 +149,18 @@ sftp_session sftp_new(ssh_session session)
goto error;
}
if (ssh_channel_open_session(sftp->channel)) {
/*
* The following two calls shouldn't return SSH_AGAIN
* as the code has validated above that the SSH session
* is in blocking mode.
*/
rc = ssh_channel_open_session(sftp->channel);
if (rc != SSH_OK) {
goto error;
}
if (ssh_channel_request_sftp(sftp->channel)) {
rc = ssh_channel_request_sftp(sftp->channel);
if (rc != SSH_OK) {
goto error;
}