1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-07-31 00:03:07 +03:00

sftp: fix problems in sftp APIs and example

Signed-off-by: tatataeki <shengzeyu19_98@163.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
This commit is contained in:
tatataeki
2022-11-07 22:08:06 +08:00
committed by Jakub Jelen
parent 0a5161a7d1
commit 8104c19013
4 changed files with 627 additions and 669 deletions

View File

@ -43,13 +43,8 @@ The goal is to show the API in action.
#include <stdbool.h>
/* below are for sftp */
#include <sys/types.h>
#include <sys/statvfs.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>
@ -146,9 +141,9 @@ const struct message_handler extended_handlers[] = {
struct sftp_handle s_handle_table[MAX_HANDLE_NUM];
static void init_handle_table(int handle_num) {
static void init_handle_table(void) {
int obj_size = sizeof(struct sftp_handle);
memset(s_handle_table, 0, obj_size*handle_num);
memset(s_handle_table, 0, obj_size * MAX_HANDLE_NUM);
}
static void reinit_single_handle(struct sftp_handle* handle) {
@ -159,11 +154,11 @@ static void reinit_single_handle(struct sftp_handle* handle) {
handle->fd = -1;
}
static int handle_is_ok(int i, int type) {
return i >= 0 && (u_int)i < MAX_HANDLE_NUM && s_handle_table[i].type == type;
static int handle_is_ok(uint8_t i, int type) {
return i < MAX_HANDLE_NUM && s_handle_table[i].type == type;
}
static int handle_close(int handle_ind) {
static int handle_close(uint8_t handle_ind) {
int ret = SSH_ERROR;
if (handle_is_ok(handle_ind, FILE_HANDLE)) {
@ -202,9 +197,9 @@ static int handle_close_by_pointer(struct sftp_handle* handle) {
return 0;
}
static void free_handles(int handle_num) {
int i;
for(i = 0; i < handle_num; i++) {
static void free_handles(void) {
uint8_t i;
for(i = 0; i < MAX_HANDLE_NUM; i++) {
handle_close(i);
/* reinit this handle */
reinit_single_handle(&s_handle_table[i]);
@ -213,8 +208,8 @@ static void free_handles(int handle_num) {
}
static int add_handle(int type, void *dirp, int fd, const char *name, void *session_id) {
int ret = SSH_ERROR;
int i;
int ret = -1;
uint8_t i;
if (dirp == NULL && fd < 0) {
return ret;
}
@ -240,12 +235,10 @@ static int add_handle(int type, void *dirp, int fd, const char *name, void *sess
}
static char* get_handle_name(struct sftp_handle* handle) {
char *ret = NULL;
if (handle != NULL && handle->name != NULL)
ret = handle->name;
return handle->name;
return ret;
return NULL;
}
static const char* ssh_str_error(int u_errno) {
@ -299,8 +292,7 @@ static int unix_errno_to_ssh_stat(int u_errno) {
static void stat_to_filexfer_attrib(const struct stat* z_st, struct sftp_attributes_struct* z_attr)
{
z_attr->flags = 0;
z_attr->flags |= (uint32_t)SSH_FILEXFER_ATTR_SIZE;
z_attr->flags = 0 | (uint32_t)SSH_FILEXFER_ATTR_SIZE;
z_attr->size = z_st->st_size;
z_attr->flags |= (uint32_t)SSH_FILEXFER_ATTR_UIDGID;
@ -415,7 +407,6 @@ static int readdir_long_name(char* z_file_name, struct stat* z_st, char* z_long_
}
static int process_open(sftp_client_message client_msg) {
int ret = SSH_OK;
const char *filename = sftp_client_message_get_filename(client_msg);
uint32_t msg_flag = sftp_client_message_get_flags(client_msg);
int file_flag;
@ -439,17 +430,15 @@ static int process_open(sftp_client_message client_msg) {
} else {
printf("undefined message flag\n");
sftp_reply_status(client_msg, SSH_FX_FAILURE, "Flag error");
ret = SSH_ERROR;
return ret;
return SSH_ERROR;
}
fd = open(filename, file_flag, 0600);
if(fd == -1){
status = unix_errno_to_ssh_stat(errno);
printf("error open file with error: %d\n", errno);
printf("error opening file with error: %d\n", errno);
sftp_reply_status(client_msg, status, "Write error");
ret = SSH_ERROR;
return ret;
return SSH_ERROR;
}
handle_ind = add_handle(FILE_HANDLE, NULL, fd, filename, client_msg->sftp);
@ -458,18 +447,16 @@ static int process_open(sftp_client_message client_msg) {
ssh_string handle_s = sftp_handle_alloc(client_msg->sftp, handle_ptr);
sftp_reply_handle(client_msg, handle_s);
ssh_string_free(handle_s);
ret = SSH_OK;
}else {
close(fd);
printf("opening file failed");
sftp_reply_status(client_msg, SSH_FX_FAILURE, "No handle available");
}
return ret;
return SSH_OK;
}
static int process_read(sftp_client_message client_msg) {
int ret = SSH_OK;
struct sftp_handle *client_handle = (struct sftp_handle*)sftp_handle(client_msg->sftp, client_msg->handle);
uint32_t readn;
int fd;
@ -477,7 +464,7 @@ static int process_read(sftp_client_message client_msg) {
int rv;
if (client_handle == NULL || client_handle->session_id != client_msg->sftp) {
printf("get wrong handle from msg\n");
printf("got wrong handle from msg\n");
sftp_reply_status(client_msg, SSH_FX_FAILURE, NULL);
return SSH_ERROR;
}
@ -487,18 +474,16 @@ static int process_read(sftp_client_message client_msg) {
if (fd < 0) {
sftp_reply_status(client_msg, SSH_FX_INVALID_HANDLE, NULL);
printf("error reading file fd: %d\n", fd);
ret = SSH_ERROR;
return ret;
return SSH_ERROR;
}
rv = lseek(fd, client_msg->offset, SEEK_SET);
if (rv == -1) {
sftp_reply_status(client_msg, SSH_FX_FAILURE, NULL);
printf("error seeking file fd: %d at offset: %" PRIu64 "\n", fd, client_msg->offset);
ret = SSH_ERROR;
return ret;
return SSH_ERROR;
}
buffer = malloc((client_msg->len)*sizeof(char));
buffer = malloc(client_msg->len);
readn = read(fd, buffer, client_msg->len);
if(readn > 0){
@ -508,14 +493,16 @@ static int process_read(sftp_client_message client_msg) {
}else {
sftp_reply_status(client_msg, SSH_FX_FAILURE, NULL);
printf("read file error!\n");
return SSH_ERROR;
}
return ret;
free(buffer);
return SSH_OK;
}
static int process_write(sftp_client_message client_msg) {
int ret = SSH_OK;
struct sftp_handle *client_handle = (struct sftp_handle*)sftp_handle(client_msg->sftp, client_msg->handle);
uint32_t writen;
int written;
int fd;
const char *msg_data;
uint32_t len;
@ -532,8 +519,7 @@ static int process_write(sftp_client_message client_msg) {
if (fd < 0) {
sftp_reply_status(client_msg, SSH_FX_INVALID_HANDLE, NULL);
printf("write file fd error!\n");
ret = SSH_ERROR;
return ret;
return SSH_ERROR;
}
msg_data = ssh_string_get_char(client_msg->data);
@ -544,19 +530,21 @@ static int process_write(sftp_client_message client_msg) {
sftp_reply_status(client_msg, SSH_FX_FAILURE, NULL);
printf("error seeking file at offset: %" PRIu64 "\n", client_msg->offset);
}
writen = write(fd, msg_data, len);
if(writen == len) {
written = write(fd, msg_data, len);
if (written == (int)len) {
sftp_reply_status(client_msg, SSH_FX_OK, NULL);
}else {
} else if (written == -1) {
sftp_reply_status(client_msg, SSH_FX_FAILURE, "Write error");
} else {
sftp_reply_status(client_msg, SSH_FX_FAILURE, "Partial write");
}
return ret;
return SSH_OK;
}
static int process_close(sftp_client_message client_msg) {
int ret = SSH_OK;
struct sftp_handle *client_handle = (struct sftp_handle*)sftp_handle(client_msg->sftp, client_msg->handle);
int ret;
if (client_handle == NULL) {
printf("get wrong handle from msg\n");
@ -564,19 +552,18 @@ static int process_close(sftp_client_message client_msg) {
}
ret = handle_close_by_pointer(client_handle);
reinit_single_handle(client_handle);
if (ret == SSH_OK) {
reinit_single_handle(client_handle);
sftp_reply_status(client_msg, SSH_FX_OK, NULL);
} else {
printf("closing file failed\n");
sftp_reply_status(client_msg, SSH_FX_BAD_MESSAGE, "Invalid handle");
}
return ret;
return SSH_OK;
}
static int process_opendir(sftp_client_message client_msg) {
int ret = SSH_OK;
DIR *dir = NULL;
const char *dir_name = sftp_client_message_get_filename(client_msg);
int handle_ind = -1;
@ -592,13 +579,12 @@ static int process_opendir(sftp_client_message client_msg) {
ssh_string handle_s = sftp_handle_alloc(client_msg->sftp, &s_handle_table[handle_ind]);
sftp_reply_handle(client_msg, handle_s);
ssh_string_free(handle_s);
ret = SSH_OK;
} else {
closedir(dir);
sftp_reply_status(client_msg, SSH_FX_FAILURE, "No handle available");
}
return ret;
return SSH_OK;
}
static int process_readdir(sftp_client_message client_msg) {
@ -610,6 +596,7 @@ static int process_readdir(sftp_client_message client_msg) {
char long_path[PATH_MAX];
int path_length;
int srclen;
char *handle_name;
if (client_handle == NULL || client_handle->session_id != client_msg->sftp) {
@ -622,18 +609,23 @@ static int process_readdir(sftp_client_message client_msg) {
if (dir == NULL) {
sftp_reply_status(client_msg, SSH_FX_INVALID_HANDLE, NULL);
printf("read dir handle error!\n");
ret = SSH_ERROR;
return ret;
return SSH_ERROR;
}
ret = SSH_OK;
handle_name = get_handle_name(client_handle);
if (handle_name == NULL) {
sftp_reply_status(client_msg, SSH_FX_INVALID_HANDLE, NULL);
return SSH_ERROR;
}
strcpy(long_path, handle_name);
strcat(long_path, "/");
srclen = strlen(handle_name);
if (srclen + 2 >= PATH_MAX) {
printf("handle string length exceed max length!\n");
sftp_reply_status(client_msg, SSH_FX_INVALID_HANDLE, NULL);
return SSH_ERROR;
}
strncpy(long_path, handle_name, PATH_MAX-strlen(long_path)-1);
strncat(long_path, "/", PATH_MAX-strlen(long_path)-1);
path_length = (int)strlen(long_path);
for(int i=0; i < MAX_ENTRIES_NUM_IN_PACKET; i++) {
@ -644,7 +636,12 @@ static int process_readdir(sftp_client_message client_msg) {
struct stat st;
char long_name[MAX_LONG_NAME_LEN];
strcpy(&long_path[path_length], dentry->d_name);
if (strlen(dentry->d_name)+path_length+1 >= PATH_MAX) {
printf("handle string length exceed max length!\n");
sftp_reply_status(client_msg, SSH_FX_INVALID_HANDLE, NULL);
return SSH_ERROR;
}
strncpy(&long_path[path_length], dentry->d_name, strlen(dentry->d_name)+1);
if(lstat(long_path, &st) == 0) {
stat_to_filexfer_attrib(&st, &attr);
@ -820,14 +817,12 @@ static int process_remove(sftp_client_message client_msg) {
}
static int process_unsupposed(sftp_client_message client_msg) {
int ret = SSH_OK;
sftp_reply_status(client_msg, SSH_FX_OP_UNSUPPORTED, "Operation not supported");
printf("Message type %d not implemented\n", sftp_client_message_get_type(client_msg));
return ret;
return SSH_OK;
}
static int process_extended_statvfs(sftp_client_message client_msg) {
int ret = SSH_OK;
const char *path = sftp_client_message_get_filename(client_msg);
struct statvfs st;
int status;
@ -839,36 +834,35 @@ static int process_extended_statvfs(sftp_client_message client_msg) {
u_int64_t flag;
sftp_statvfs = calloc(1, sizeof(struct sftp_statvfs_struct));
if (sftp_statvfs == NULL) {
goto error;
if (sftp_statvfs != NULL) {
flag = (st.f_flag & ST_RDONLY) ? SSH_FXE_STATVFS_ST_RDONLY : 0;
flag |= (st.f_flag & ST_NOSUID) ? SSH_FXE_STATVFS_ST_NOSUID : 0;
sftp_statvfs->f_bsize = st.f_bsize;
sftp_statvfs->f_frsize = st.f_frsize;
sftp_statvfs->f_blocks = st.f_blocks;
sftp_statvfs->f_bfree = st.f_bfree;
sftp_statvfs->f_bavail = st.f_bavail;
sftp_statvfs->f_files = st.f_files;
sftp_statvfs->f_ffree = st.f_ffree;
sftp_statvfs->f_favail = st.f_favail;
sftp_statvfs->f_fsid = st.f_fsid;
sftp_statvfs->f_flag = flag;
sftp_statvfs->f_namemax = st.f_namemax;
rv = sftp_reply_statvfs(client_msg, sftp_statvfs);
free(sftp_statvfs);
if (rv == 0) {
return SSH_OK;
}
}
flag = (st.f_flag & ST_RDONLY) ? SSH_FXE_STATVFS_ST_RDONLY : 0;
flag |= (st.f_flag & ST_NOSUID) ? SSH_FXE_STATVFS_ST_NOSUID : 0;
sftp_statvfs->f_bsize = st.f_bsize;
sftp_statvfs->f_frsize = st.f_frsize;
sftp_statvfs->f_blocks = st.f_blocks;
sftp_statvfs->f_bfree = st.f_bfree;
sftp_statvfs->f_bavail = st.f_bavail;
sftp_statvfs->f_files = st.f_files;
sftp_statvfs->f_ffree = st.f_ffree;
sftp_statvfs->f_favail = st.f_favail;
sftp_statvfs->f_fsid = st.f_fsid;
sftp_statvfs->f_flag = flag;
sftp_statvfs->f_namemax = st.f_namemax;
if(sftp_reply_statvfs(client_msg, sftp_statvfs) == 0)
return ret;
}
error:
status = unix_errno_to_ssh_stat(errno);
ret = SSH_ERROR;
sftp_reply_status(client_msg, status, NULL);
printf("statvfs send failed!\n");
return ret;
return SSH_ERROR;
}
static int process_extended(sftp_client_message sftp_msg) {
@ -890,8 +884,6 @@ static int process_extended(sftp_client_message sftp_msg) {
sftp_reply_status(sftp_msg, SSH_FX_OP_UNSUPPORTED, "Extended Operation not supported");
printf("Extended Message type %s not implemented\n", subtype);
return SSH_OK;
return status;
}
static int dispatch_sftp_request(sftp_client_message sftp_msg) {
@ -905,17 +897,16 @@ static int dispatch_sftp_request(sftp_client_message sftp_msg) {
break;
}
}
if (handler!=NULL)
if (handler!=NULL) {
status = handler(sftp_msg);
else
goto not_implemented;
} else {
sftp_reply_status(sftp_msg, SSH_FX_OP_UNSUPPORTED, "Operation not supported");
printf("Message type %d not implemented\n", sft_msg_type);
return SSH_OK;
}
return status;
not_implemented:
sftp_reply_status(sftp_msg, SSH_FX_OP_UNSUPPORTED, "Operation not supported");
printf("Message type %d not implemented\n", sft_msg_type);
return SSH_OK;
}
static int process_client_message(sftp_client_message client_msg) {
@ -932,7 +923,6 @@ static int process_client_message(sftp_client_message client_msg) {
status = dispatch_sftp_request(client_msg);
}
// status = dispatch_sftp_request(client_msg);
if (status!=SSH_OK)
printf("error occur in process client message!\n");
@ -1152,7 +1142,7 @@ static int data_function(ssh_session session, ssh_channel channel, void *data,
if(decode_len == -1)
return -1;
msg = sftp_get_client_message_from_packet(cdata->sftp);
msg = sftp_get_client_message_from_packet(sftp);
rc = process_client_message(msg);
sftp_client_message_free(msg);
if(rc != SSH_OK)
@ -1169,7 +1159,7 @@ static int subsystem_request(ssh_session session, ssh_channel channel,
/* initialize sftp session and file handler */
cdata->sftp = sftp_server_new(session, channel);
init_handle_table(MAX_HANDLE_NUM);
init_handle_table();
return SSH_OK;
}
@ -1180,8 +1170,6 @@ static int auth_password(ssh_session session, const char *user,
const char *pass, void *userdata) {
struct session_data_struct *sdata = (struct session_data_struct *) userdata;
(void) session;
if (strcmp(user, USER) == 0 && strcmp(pass, PASS) == 0) {
sdata->authenticated = 1;
return SSH_AUTH_SUCCESS;
@ -1199,9 +1187,6 @@ static int auth_publickey(ssh_session session,
{
struct session_data_struct *sdata = (struct session_data_struct *) userdata;
(void) user;
(void) session;
if (signature_state == SSH_PUBLICKEY_STATE_NONE) {
return SSH_AUTH_SUCCESS;
}
@ -1340,7 +1325,7 @@ static void handle_session(ssh_event event, ssh_session session) {
} while(ssh_channel_is_open(sdata.channel) &&
(cdata.pid == 0 || waitpid(cdata.pid, &rc, WNOHANG) == 0));
free_handles(MAX_HANDLE_NUM);
free_handles();
ssh_channel_send_eof(sdata.channel);
ssh_channel_close(sdata.channel);
@ -1353,7 +1338,6 @@ static void handle_session(ssh_event event, ssh_session session) {
/* SIGCHLD handler for cleaning up dead children. */
static void sigchld_handler(int signo) {
(void) signo;
while (waitpid(-1, NULL, WNOHANG) > 0);
}
@ -1376,13 +1360,13 @@ int main(int argc, char **argv) {
rc = ssh_init();
if (rc < 0) {
fprintf(stderr, "ssh_init failed\n");
return 1;
goto exit;
}
sshbind = ssh_bind_new();
if (sshbind == NULL) {
fprintf(stderr, "ssh_bind_new failed\n");
return 1;
goto exit;
}
#ifdef HAVE_ARGP_H
@ -1396,7 +1380,7 @@ int main(int argc, char **argv) {
if(ssh_bind_listen(sshbind) < 0) {
fprintf(stderr, "%s\n", ssh_get_error(sshbind));
return 1;
goto exit;
}
while (1) {
@ -1442,6 +1426,7 @@ int main(int argc, char **argv) {
ssh_free(session);
}
exit:
ssh_bind_free(sshbind);
ssh_finalize();
return 0;

View File

@ -356,7 +356,6 @@ void sftp_server_free(sftp_session sftp)
int sftp_process_init_packet(sftp_client_message client_msg)
{
int ret = SSH_OK;
sftp_session sftp = client_msg->sftp;
ssh_session session = sftp->session;
int version;
@ -384,7 +383,8 @@ int sftp_process_init_packet(sftp_client_message client_msg)
return -1;
}
if (sftp_packet_write(sftp, SSH_FXP_VERSION, reply) < 0) {
rc = sftp_packet_write(sftp, SSH_FXP_VERSION, reply);
if (rc < 0) {
SSH_BUFFER_FREE(reply);
return -1;
}
@ -395,10 +395,10 @@ int sftp_process_init_packet(sftp_client_message client_msg)
if (version > LIBSFTP_VERSION) {
sftp->version = LIBSFTP_VERSION;
} else {
sftp->version = (int)version;
sftp->version = version;
}
return ret;
return SSH_OK;
}
#endif /* WITH_SERVER */
@ -443,26 +443,35 @@ int sftp_decode_channel_data_to_packet(sftp_session sftp, void *data)
int offset;
int to_read;
if(packet->sftp == NULL)
if (packet->sftp == NULL) {
packet->sftp = sftp;
}
packet->type = *((uint8_t *)data + sizeof(uint32_t));
payload_len = PULL_BE_U32(data, 0);
/* We should check the legality of payload length */
if(payload_len > MAX_PACKET_LEN || payload_len < 0)
if (payload_len > MAX_PACKET_LEN || payload_len < 0) {
return SSH_ERROR;
}
offset = sizeof(int) + sizeof(uint8_t);
to_read = payload_len - sizeof(uint8_t);
ssh_buffer_add_data(packet->payload, (void*)((uint8_t *)data + offset), payload_len - sizeof(uint8_t));
ssh_buffer_add_data(packet->payload,
(void*)((uint8_t *)data + offset),
payload_len - sizeof(uint8_t));
nread = ssh_buffer_get_len(packet->payload);
/* We should check if we copied the whole data */
if(nread != to_read)
if (nread != to_read) {
return SSH_ERROR;
}
return payload_len + sizeof(int);
/*
* We should return how many bytes we decoded, including packet length header
* and the payload length.
*/
return payload_len + sizeof(uint32_t);
}
int sftp_packet_write(sftp_session sftp, uint8_t type, ssh_buffer payload)

View File

@ -266,9 +266,7 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
packet = sftp->read_packet;
if (packet == NULL) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
payload = packet->payload;
@ -278,21 +276,17 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
/* take a copy of the whole packet */
msg->complete_message = ssh_buffer_new();
if (msg->complete_message == NULL) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
rc = ssh_buffer_add_data(msg->complete_message,
ssh_buffer_get(payload),
ssh_buffer_get_len(payload));
if (rc < 0) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
if (msg->type!=SSH_FXP_INIT) {
if (msg->type != SSH_FXP_INIT) {
ssh_buffer_get_u32(payload, &msg->id);
}
@ -302,21 +296,17 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
"d",
&version);
if (rc != SSH_OK) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
printf("unpack init failed!\n");
return NULL;
printf("unpack init failed!\n");
goto error;
}
version = ntohl(version);
sftp->client_version = (int)version;
sftp->client_version = version;
break;
case SSH_FXP_CLOSE:
case SSH_FXP_READDIR:
msg->handle = ssh_buffer_get_ssh_string(payload);
if (msg->handle == NULL) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
break;
case SSH_FXP_READ:
@ -326,9 +316,7 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
&msg->offset,
&msg->len);
if (rc != SSH_OK) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
break;
case SSH_FXP_WRITE:
@ -338,9 +326,7 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
&msg->offset,
&msg->data);
if (rc != SSH_OK) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
break;
case SSH_FXP_REMOVE:
@ -352,9 +338,7 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
"s",
&msg->filename);
if (rc != SSH_OK) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
break;
case SSH_FXP_RENAME:
@ -364,9 +348,7 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
&msg->filename,
&msg->data);
if (rc != SSH_OK) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
break;
case SSH_FXP_MKDIR:
@ -375,29 +357,21 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
"s",
&msg->filename);
if (rc != SSH_OK) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
msg->attr = sftp_parse_attr(sftp, payload, 0);
if (msg->attr == NULL) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
break;
case SSH_FXP_FSETSTAT:
msg->handle = ssh_buffer_get_ssh_string(payload);
if (msg->handle == NULL) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
msg->attr = sftp_parse_attr(sftp, payload, 0);
if (msg->attr == NULL) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
break;
case SSH_FXP_LSTAT:
@ -406,9 +380,7 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
"s",
&msg->filename);
if (rc != SSH_OK) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
if(sftp->version > 3) {
ssh_buffer_unpack(payload, "d", &msg->flags);
@ -420,15 +392,11 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
&msg->filename,
&msg->flags);
if (rc != SSH_OK) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
msg->attr = sftp_parse_attr(sftp, payload, 0);
if (msg->attr == NULL) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
break;
case SSH_FXP_FSTAT:
@ -436,9 +404,7 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
"S",
&msg->handle);
if (rc != SSH_OK) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
break;
case SSH_FXP_EXTENDED:
@ -446,9 +412,7 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
"s",
&msg->submessage);
if (rc != SSH_OK) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
if (strcmp(msg->submessage, "hardlink@openssh.com") == 0 ||
@ -458,18 +422,14 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
&msg->filename,
&msg->data);
if (rc != SSH_OK) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
} else if (strcmp(msg->submessage, "statvfs@openssh.com") == 0 ){
rc = ssh_buffer_unpack(payload,
"s",
&msg->filename);
if (rc != SSH_OK) {
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
goto error;
}
}
break;
@ -481,6 +441,11 @@ sftp_client_message sftp_get_client_message_from_packet(sftp_session sftp)
}
return msg;
error:
ssh_set_error_oom(session);
sftp_client_message_free(msg);
return NULL;
}
@ -722,8 +687,8 @@ int sftp_reply_data(sftp_client_message msg, const void *data, int len) {
int sftp_reply_statvfs(sftp_client_message msg, sftp_statvfs_t st)
{
int ret = 0;
ssh_buffer out;
out = ssh_buffer_new();
if (out == NULL) {
return -1;
@ -742,12 +707,11 @@ int sftp_reply_statvfs(sftp_client_message msg, sftp_statvfs_t st)
ssh_buffer_add_u64(out, ntohll(st->f_flag)) < 0 ||
ssh_buffer_add_u64(out, ntohll(st->f_namemax)) < 0 ||
sftp_packet_write(msg->sftp, SSH_FXP_EXTENDED_REPLY, out) < 0) {
SSH_BUFFER_FREE(out);
return -1;
ret = -1;
}
SSH_BUFFER_FREE(out);
return 0;
return ret;
}

View File

@ -51,8 +51,8 @@ struct test_server_st {
};
void sftp_handle_session_cb(ssh_event event,
ssh_session session,
struct server_state_st *state);
ssh_session session,
struct server_state_st *state);
static int setup_default_server(void **state)
{