mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-07 18:50:27 +09:00
Adding expand-path@openssh.com extension for client
Signed-off-by: anshul agrawal <anshulagrawal2902@gmail.com> Reviewed-by: Sahana Prasad <sahana@redhat.com> Reviewed-by: Eshan Kelkar <eshankelkar@galorithm.com> Reviewed-by: Jakub Jelen <jjelen@redhat.com>
This commit is contained in:
committed by
Sahana Prasad
parent
6e834b8df2
commit
a8fe05cc40
94
src/sftp.c
94
src/sftp.c
@@ -3744,4 +3744,98 @@ sftp_attributes sftp_fstat(sftp_file file)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *sftp_expand_path(sftp_session sftp, const char *path)
|
||||
{
|
||||
sftp_status_message status = NULL;
|
||||
sftp_message msg = NULL;
|
||||
ssh_buffer buffer = NULL;
|
||||
uint32_t id;
|
||||
int rc;
|
||||
|
||||
if (sftp == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (path == NULL) {
|
||||
ssh_set_error(sftp->session,
|
||||
SSH_FATAL,
|
||||
"NULL received as an argument instead of the path to expand");
|
||||
sftp_set_error(sftp, SSH_FX_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buffer = ssh_buffer_new();
|
||||
if (buffer == NULL) {
|
||||
ssh_set_error_oom(sftp->session);
|
||||
sftp_set_error(sftp, SSH_FX_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
id = sftp_get_new_id(sftp);
|
||||
|
||||
rc = ssh_buffer_pack(buffer,
|
||||
"dss",
|
||||
id,
|
||||
"expand-path@openssh.com",
|
||||
path);
|
||||
if (rc != SSH_OK) {
|
||||
ssh_set_error_oom(sftp->session);
|
||||
SSH_BUFFER_FREE(buffer);
|
||||
sftp_set_error(sftp, SSH_FX_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rc = sftp_packet_write(sftp, SSH_FXP_EXTENDED, buffer);
|
||||
SSH_BUFFER_FREE(buffer);
|
||||
if (rc < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (msg == NULL) {
|
||||
rc = sftp_read_and_dispatch(sftp);
|
||||
if (rc < 0) {
|
||||
return NULL;
|
||||
}
|
||||
msg = sftp_dequeue(sftp, id);
|
||||
}
|
||||
|
||||
if (msg->packet_type == SSH_FXP_NAME) {
|
||||
uint32_t ignored = 0;
|
||||
char *cname = NULL;
|
||||
|
||||
rc = ssh_buffer_unpack(msg->payload,
|
||||
"ds",
|
||||
&ignored,
|
||||
&cname);
|
||||
sftp_message_free(msg);
|
||||
if (rc != SSH_OK) {
|
||||
ssh_set_error(sftp->session,
|
||||
SSH_ERROR,
|
||||
"Failed to parse expanded path");
|
||||
sftp_set_error(sftp, SSH_FX_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return cname;
|
||||
} else if (msg->packet_type == SSH_FXP_STATUS) {
|
||||
status = parse_status_msg(msg);
|
||||
sftp_message_free(msg);
|
||||
if (status == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
sftp_set_error(sftp, status->status);
|
||||
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
||||
"SFTP server: %s", status->errormsg);
|
||||
status_msg_free(status);
|
||||
} else {
|
||||
ssh_set_error(sftp->session, SSH_FATAL,
|
||||
"Received message %d when attempting to expand path",
|
||||
msg->packet_type);
|
||||
sftp_message_free(msg);
|
||||
sftp_set_error(sftp, SSH_FX_BAD_MESSAGE);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif /* WITH_SFTP */
|
||||
|
||||
Reference in New Issue
Block a user