2005-07-05 05:21:44 +04:00
|
|
|
/*
|
2009-03-30 00:19:18 +04:00
|
|
|
* sftp.c - Secure FTP functions
|
|
|
|
*
|
|
|
|
* This file is part of the SSH Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2008 by Aris Adamantiadis
|
2014-01-07 19:08:23 +04:00
|
|
|
* Copyright (c) 2008-2009 by Andreas Schneider <asn@cryptomilk.org>
|
2009-03-30 00:19:18 +04:00
|
|
|
*
|
|
|
|
* The SSH Library is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* The SSH Library is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with the SSH Library; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
|
|
|
|
* MA 02111-1307, USA.
|
|
|
|
*/
|
2005-07-05 05:21:44 +04:00
|
|
|
|
2009-03-30 00:19:18 +04:00
|
|
|
/* This file contains code written by Nick Zitzmann */
|
2005-07-05 05:21:44 +04:00
|
|
|
|
2017-10-28 15:31:37 +03:00
|
|
|
#include "config.h"
|
|
|
|
|
2008-11-24 21:05:55 +03:00
|
|
|
#include <errno.h>
|
2010-02-24 00:50:43 +03:00
|
|
|
#include <ctype.h>
|
2009-04-23 13:07:18 +04:00
|
|
|
#include <fcntl.h>
|
2010-03-30 01:13:39 +04:00
|
|
|
#include <stdio.h>
|
2010-05-31 12:52:36 +04:00
|
|
|
#include <stdio.h>
|
2009-04-23 13:07:18 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2015-05-04 17:46:21 +03:00
|
|
|
#include <limits.h>
|
2009-04-23 13:07:18 +04:00
|
|
|
|
2009-06-09 16:03:00 +04:00
|
|
|
#ifndef _WIN32
|
2012-07-17 20:16:07 +04:00
|
|
|
#include <netinet/in.h>
|
2009-06-09 16:03:00 +04:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2005-07-05 05:21:44 +04:00
|
|
|
#include "libssh/priv.h"
|
|
|
|
#include "libssh/ssh2.h"
|
|
|
|
#include "libssh/sftp.h"
|
2009-09-24 01:51:04 +04:00
|
|
|
#include "libssh/buffer.h"
|
|
|
|
#include "libssh/channels.h"
|
|
|
|
#include "libssh/session.h"
|
2009-09-26 03:15:48 +04:00
|
|
|
#include "libssh/misc.h"
|
2009-04-27 14:58:29 +04:00
|
|
|
|
|
|
|
#ifdef WITH_SFTP
|
2005-07-05 05:21:44 +04:00
|
|
|
|
2009-07-28 15:21:05 +04:00
|
|
|
struct sftp_ext_struct {
|
|
|
|
unsigned int count;
|
|
|
|
char **name;
|
|
|
|
char **data;
|
|
|
|
};
|
|
|
|
|
2005-07-05 05:21:44 +04:00
|
|
|
/* functions */
|
2009-10-10 22:55:17 +04:00
|
|
|
static int sftp_enqueue(sftp_session session, sftp_message msg);
|
|
|
|
static void sftp_message_free(sftp_message msg);
|
|
|
|
static void sftp_set_error(sftp_session sftp, int errnum);
|
|
|
|
static void status_msg_free(sftp_status_message status);
|
2005-07-05 05:21:44 +04:00
|
|
|
|
2009-07-28 15:21:05 +04:00
|
|
|
static sftp_ext sftp_ext_new(void) {
|
|
|
|
sftp_ext ext;
|
|
|
|
|
2016-05-04 15:56:22 +03:00
|
|
|
ext = calloc(1, sizeof(struct sftp_ext_struct));
|
2009-07-28 15:21:05 +04:00
|
|
|
if (ext == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sftp_ext_free(sftp_ext ext) {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (ext == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ext->count) {
|
|
|
|
for (i = 0; i < ext->count; i++) {
|
|
|
|
SAFE_FREE(ext->name[i]);
|
|
|
|
SAFE_FREE(ext->data[i]);
|
|
|
|
}
|
|
|
|
SAFE_FREE(ext->name);
|
|
|
|
SAFE_FREE(ext->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
SAFE_FREE(ext);
|
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_session sftp_new(ssh_session session){
|
|
|
|
sftp_session sftp;
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2009-04-22 17:59:13 +04:00
|
|
|
if (session == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2016-05-04 15:56:22 +03:00
|
|
|
sftp = calloc(1, sizeof(struct sftp_session_struct));
|
2009-04-22 17:59:13 +04:00
|
|
|
if (sftp == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(session);
|
2013-07-14 15:31:24 +04:00
|
|
|
|
2009-04-22 17:59:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-07-28 15:21:05 +04:00
|
|
|
|
|
|
|
sftp->ext = sftp_ext_new();
|
|
|
|
if (sftp->ext == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(session);
|
2009-07-28 15:21:05 +04:00
|
|
|
SAFE_FREE(sftp);
|
2013-07-14 15:31:24 +04:00
|
|
|
|
2009-07-28 15:21:05 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-22 17:59:13 +04:00
|
|
|
|
|
|
|
sftp->session = session;
|
2010-05-14 02:51:08 +04:00
|
|
|
sftp->channel = ssh_channel_new(session);
|
2009-04-22 17:59:13 +04:00
|
|
|
if (sftp->channel == NULL) {
|
2015-04-14 21:40:04 +03:00
|
|
|
sftp_ext_free(sftp->ext);
|
2009-04-22 17:59:13 +04:00
|
|
|
SAFE_FREE(sftp);
|
2013-07-14 15:31:24 +04:00
|
|
|
|
2009-04-22 17:59:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
if (ssh_channel_open_session(sftp->channel)) {
|
|
|
|
ssh_channel_free(sftp->channel);
|
2015-04-14 21:40:04 +03:00
|
|
|
sftp_ext_free(sftp->ext);
|
2009-04-22 17:59:13 +04:00
|
|
|
SAFE_FREE(sftp);
|
2013-07-14 15:31:24 +04:00
|
|
|
|
2009-04-22 17:59:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
if (ssh_channel_request_sftp(sftp->channel)) {
|
2009-04-22 17:59:13 +04:00
|
|
|
sftp_free(sftp);
|
2013-07-14 15:31:24 +04:00
|
|
|
|
2009-04-22 17:59:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-02 20:40:13 +04:00
|
|
|
return sftp;
|
|
|
|
}
|
|
|
|
|
|
|
|
sftp_session sftp_new_channel(ssh_session session, ssh_channel channel){
|
|
|
|
sftp_session sftp;
|
|
|
|
|
|
|
|
if (session == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-04 15:56:22 +03:00
|
|
|
sftp = calloc(1, sizeof(struct sftp_session_struct));
|
2013-06-02 20:40:13 +04:00
|
|
|
if (sftp == NULL) {
|
|
|
|
ssh_set_error_oom(session);
|
2013-07-14 15:31:24 +04:00
|
|
|
|
2013-06-02 20:40:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sftp->ext = sftp_ext_new();
|
|
|
|
if (sftp->ext == NULL) {
|
|
|
|
ssh_set_error_oom(session);
|
|
|
|
SAFE_FREE(sftp);
|
2013-07-14 15:31:24 +04:00
|
|
|
|
2013-06-02 20:40:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sftp->session = session;
|
|
|
|
sftp->channel = channel;
|
|
|
|
|
2009-04-22 17:59:13 +04:00
|
|
|
return sftp;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-03-29 00:56:28 +03:00
|
|
|
#ifdef WITH_SERVER
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_session sftp_server_new(ssh_session session, ssh_channel chan){
|
|
|
|
sftp_session sftp = NULL;
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2016-05-04 15:56:22 +03:00
|
|
|
sftp = calloc(1, sizeof(struct sftp_session_struct));
|
2009-04-22 18:05:11 +04:00
|
|
|
if (sftp == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(session);
|
2009-04-22 18:05:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2009-04-22 18:05:11 +04:00
|
|
|
sftp->session = session;
|
|
|
|
sftp->channel = chan;
|
|
|
|
|
|
|
|
return sftp;
|
2005-08-18 14:08:20 +04:00
|
|
|
}
|
2005-08-25 17:44:26 +04:00
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_server_init(sftp_session sftp){
|
2009-07-25 22:26:01 +04:00
|
|
|
ssh_session session = sftp->session;
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_packet packet = NULL;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer reply = NULL;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t version;
|
2009-04-22 18:05:11 +04:00
|
|
|
|
|
|
|
packet = sftp_packet_read(sftp);
|
|
|
|
if (packet == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet->type != SSH_FXP_INIT) {
|
|
|
|
ssh_set_error(session, SSH_FATAL,
|
|
|
|
"Packet read of type %d instead of SSH_FXP_INIT",
|
|
|
|
packet->type);
|
|
|
|
|
2005-08-25 17:44:26 +04:00
|
|
|
sftp_packet_free(packet);
|
2009-04-22 18:05:11 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_PACKET, "Received SSH_FXP_INIT");
|
2009-04-22 18:05:11 +04:00
|
|
|
|
2015-09-17 10:43:33 +03:00
|
|
|
ssh_buffer_get_u32(packet->payload, &version);
|
2009-04-22 18:05:11 +04:00
|
|
|
version = ntohl(version);
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_PACKET, "Client version: %d", version);
|
2009-04-22 18:05:11 +04:00
|
|
|
sftp->client_version = version;
|
|
|
|
|
|
|
|
sftp_packet_free(packet);
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
reply = ssh_buffer_new();
|
2009-04-22 18:05:11 +04:00
|
|
|
if (reply == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(session);
|
2009-04-22 18:05:11 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(reply, ntohl(LIBSFTP_VERSION)) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(reply);
|
2009-04-22 18:05:11 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_VERSION, reply) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(reply);
|
2009-04-22 18:05:11 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(reply);
|
2009-04-22 18:05:11 +04:00
|
|
|
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_RARE, "Server version sent");
|
2009-04-22 18:05:11 +04:00
|
|
|
|
|
|
|
if (version > LIBSFTP_VERSION) {
|
|
|
|
sftp->version = LIBSFTP_VERSION;
|
|
|
|
} else {
|
|
|
|
sftp->version=version;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2005-08-25 17:44:26 +04:00
|
|
|
}
|
2009-03-29 00:56:28 +03:00
|
|
|
#endif /* WITH_SERVER */
|
2005-08-18 14:08:20 +04:00
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
void sftp_free(sftp_session sftp){
|
|
|
|
sftp_request_queue ptr;
|
2009-04-22 17:59:13 +04:00
|
|
|
|
|
|
|
if (sftp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_channel_send_eof(sftp->channel);
|
2009-04-22 17:59:13 +04:00
|
|
|
ptr = sftp->queue;
|
|
|
|
while(ptr) {
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_request_queue old;
|
2009-04-22 17:59:13 +04:00
|
|
|
sftp_message_free(ptr->message);
|
|
|
|
old = ptr->next;
|
|
|
|
SAFE_FREE(ptr);
|
|
|
|
ptr = old;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_channel_free(sftp->channel);
|
2012-02-04 23:55:34 +04:00
|
|
|
|
|
|
|
SAFE_FREE(sftp->handles);
|
|
|
|
|
2009-07-28 15:21:05 +04:00
|
|
|
sftp_ext_free(sftp->ext);
|
|
|
|
ZERO_STRUCTP(sftp);
|
2009-04-22 17:59:13 +04:00
|
|
|
|
|
|
|
SAFE_FREE(sftp);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_packet_write(sftp_session sftp, uint8_t type, ssh_buffer payload){
|
2009-04-22 18:48:00 +04:00
|
|
|
int size;
|
|
|
|
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_prepend_data(payload, &type, sizeof(uint8_t)) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-22 18:48:00 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-25 01:34:42 +03:00
|
|
|
size = htonl(ssh_buffer_get_len(payload));
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_prepend_data(payload, &size, sizeof(uint32_t)) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-22 18:48:00 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-25 02:10:03 +03:00
|
|
|
size = ssh_channel_write(sftp->channel, ssh_buffer_get(payload),
|
2015-09-25 01:34:42 +03:00
|
|
|
ssh_buffer_get_len(payload));
|
2009-04-22 18:48:00 +04:00
|
|
|
if (size < 0) {
|
|
|
|
return -1;
|
2015-09-25 01:34:42 +03:00
|
|
|
} else if((uint32_t) size != ssh_buffer_get_len(payload)) {
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_PACKET,
|
2009-04-22 18:48:00 +04:00
|
|
|
"Had to write %d bytes, wrote only %d",
|
2015-09-25 01:34:42 +03:00
|
|
|
ssh_buffer_get_len(payload),
|
2009-04-22 18:48:00 +04:00
|
|
|
size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_packet sftp_packet_read(sftp_session sftp) {
|
2014-02-02 22:28:51 +04:00
|
|
|
unsigned char buffer[MAX_BUF_SIZE];
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_packet packet = NULL;
|
2015-05-05 11:07:16 +03:00
|
|
|
uint32_t tmp;
|
|
|
|
size_t size;
|
2015-07-31 14:22:02 +03:00
|
|
|
int r, s;
|
2009-04-22 18:52:04 +04:00
|
|
|
|
2016-05-04 15:56:22 +03:00
|
|
|
packet = calloc(1, sizeof(struct sftp_packet_struct));
|
2009-04-22 18:52:04 +04:00
|
|
|
if (packet == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-22 18:52:04 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
packet->sftp = sftp;
|
2010-05-14 02:51:08 +04:00
|
|
|
packet->payload = ssh_buffer_new();
|
2009-04-22 18:52:04 +04:00
|
|
|
if (packet->payload == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-22 18:52:04 +04:00
|
|
|
SAFE_FREE(packet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-07-31 14:22:02 +03:00
|
|
|
r=0;
|
|
|
|
do {
|
|
|
|
// read from channel until 4 bytes have been read or an error occurs
|
|
|
|
s=ssh_channel_read(sftp->channel, buffer+r, 4-r, 0);
|
2016-10-07 14:36:58 +03:00
|
|
|
if (s < 0) {
|
|
|
|
goto error;
|
|
|
|
} else if (s == 0) {
|
|
|
|
int is_eof;
|
|
|
|
|
|
|
|
is_eof = ssh_channel_is_eof(sftp->channel);
|
|
|
|
if (is_eof) {
|
|
|
|
goto error;
|
|
|
|
}
|
2015-07-31 14:22:02 +03:00
|
|
|
} else {
|
|
|
|
r += s;
|
|
|
|
}
|
|
|
|
} while (r<4);
|
2014-01-19 23:55:55 +04:00
|
|
|
ssh_buffer_add_data(packet->payload, buffer, r);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_get_u32(packet->payload, &tmp) != sizeof(uint32_t)) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error(sftp->session, SSH_FATAL, "Short sftp packet!");
|
2016-10-07 14:36:58 +03:00
|
|
|
goto error;
|
2009-04-22 18:52:04 +04:00
|
|
|
}
|
|
|
|
|
2010-10-01 23:41:56 +04:00
|
|
|
r=ssh_channel_read(sftp->channel, buffer, 1, 0);
|
|
|
|
if (r <= 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
/* TODO: check if there are cases where an error needs to be set here */
|
2016-10-07 14:36:58 +03:00
|
|
|
goto error;
|
2009-04-22 18:52:04 +04:00
|
|
|
}
|
2014-01-19 23:55:55 +04:00
|
|
|
ssh_buffer_add_data(packet->payload, buffer, r);
|
2015-09-17 10:43:33 +03:00
|
|
|
ssh_buffer_get_u8(packet->payload, &packet->type);
|
2015-01-14 13:16:59 +03:00
|
|
|
|
2015-05-05 11:07:16 +03:00
|
|
|
size = ntohl(tmp);
|
|
|
|
if (size == 0) {
|
2015-01-14 13:16:59 +03:00
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
size--;
|
2015-05-05 11:07:16 +03:00
|
|
|
|
|
|
|
while (size > 0 && size < UINT_MAX) {
|
2010-10-01 23:41:56 +04:00
|
|
|
r=ssh_channel_read(sftp->channel,buffer,
|
|
|
|
sizeof(buffer)>size ? size:sizeof(buffer),0);
|
|
|
|
|
|
|
|
if(r <= 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
/* TODO: check if there are cases where an error needs to be set here */
|
2016-10-07 14:36:58 +03:00
|
|
|
goto error;
|
2009-04-02 00:09:20 +04:00
|
|
|
}
|
2014-01-19 23:55:55 +04:00
|
|
|
if (ssh_buffer_add_data(packet->payload, buffer, r) == SSH_ERROR) {
|
2010-10-01 23:41:56 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2016-10-07 14:36:58 +03:00
|
|
|
goto error;
|
2010-10-01 23:41:56 +04:00
|
|
|
}
|
|
|
|
size -= r;
|
2009-04-22 18:52:04 +04:00
|
|
|
}
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2009-04-22 18:52:04 +04:00
|
|
|
return packet;
|
2016-10-07 14:36:58 +03:00
|
|
|
error:
|
|
|
|
ssh_buffer_free(packet->payload);
|
|
|
|
SAFE_FREE(packet);
|
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static void sftp_set_error(sftp_session sftp, int errnum) {
|
2008-11-24 21:05:55 +03:00
|
|
|
if (sftp != NULL) {
|
|
|
|
sftp->errnum = errnum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Get the last sftp error */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_get_error(sftp_session sftp) {
|
2008-11-24 21:05:55 +03:00
|
|
|
if (sftp == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sftp->errnum;
|
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static sftp_message sftp_message_new(sftp_session sftp){
|
|
|
|
sftp_message msg = NULL;
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2016-05-04 15:56:22 +03:00
|
|
|
msg = calloc(1, sizeof(struct sftp_message_struct));
|
2009-04-22 19:06:28 +04:00
|
|
|
if (msg == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-22 19:06:28 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
msg->payload = ssh_buffer_new();
|
2009-04-22 19:06:28 +04:00
|
|
|
if (msg->payload == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-22 19:06:28 +04:00
|
|
|
SAFE_FREE(msg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
msg->sftp = sftp;
|
|
|
|
|
|
|
|
return msg;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static void sftp_message_free(sftp_message msg) {
|
2009-04-22 19:09:54 +04:00
|
|
|
if (msg == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(msg->payload);
|
2009-04-22 19:09:54 +04:00
|
|
|
SAFE_FREE(msg);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static sftp_message sftp_get_message(sftp_packet packet) {
|
|
|
|
sftp_session sftp = packet->sftp;
|
|
|
|
sftp_message msg = NULL;
|
2014-07-06 02:29:58 +04:00
|
|
|
int rc;
|
2009-04-22 19:17:20 +04:00
|
|
|
|
|
|
|
msg = sftp_message_new(sftp);
|
|
|
|
if (msg == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->sftp = packet->sftp;
|
|
|
|
msg->packet_type = packet->type;
|
|
|
|
|
|
|
|
if ((packet->type != SSH_FXP_STATUS) && (packet->type!=SSH_FXP_HANDLE) &&
|
|
|
|
(packet->type != SSH_FXP_DATA) && (packet->type != SSH_FXP_ATTRS) &&
|
2009-08-12 15:08:11 +04:00
|
|
|
(packet->type != SSH_FXP_NAME) && (packet->type != SSH_FXP_EXTENDED_REPLY)) {
|
|
|
|
ssh_set_error(packet->sftp->session, SSH_FATAL,
|
2009-04-22 19:17:20 +04:00
|
|
|
"Unknown packet type %d", packet->type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-06 02:29:58 +04:00
|
|
|
rc = ssh_buffer_unpack(packet->payload, "d", &msg->id);
|
|
|
|
if (rc != SSH_OK) {
|
2009-04-22 19:17:20 +04:00
|
|
|
ssh_set_error(packet->sftp->session, SSH_FATAL,
|
|
|
|
"Invalid packet %d: no ID", packet->type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_PACKET,
|
2009-04-22 19:17:20 +04:00
|
|
|
"Packet with id %d type %d",
|
|
|
|
msg->id,
|
|
|
|
msg->packet_type);
|
|
|
|
|
2015-09-25 02:10:03 +03:00
|
|
|
if (ssh_buffer_add_data(msg->payload, ssh_buffer_get(packet->payload),
|
2015-09-25 01:34:42 +03:00
|
|
|
ssh_buffer_get_len(packet->payload)) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-22 19:17:20 +04:00
|
|
|
sftp_message_free(msg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static int sftp_read_and_dispatch(sftp_session sftp) {
|
|
|
|
sftp_packet packet = NULL;
|
|
|
|
sftp_message msg = NULL;
|
2009-04-22 19:21:36 +04:00
|
|
|
|
|
|
|
packet = sftp_packet_read(sftp);
|
|
|
|
if (packet == NULL) {
|
|
|
|
return -1; /* something nasty happened reading the packet */
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = sftp_get_message(packet);
|
|
|
|
sftp_packet_free(packet);
|
2009-04-22 19:23:45 +04:00
|
|
|
if (msg == NULL) {
|
2009-04-22 19:21:36 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-04-22 19:53:29 +04:00
|
|
|
if (sftp_enqueue(sftp, msg) < 0) {
|
|
|
|
sftp_message_free(msg);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-04-22 19:21:36 +04:00
|
|
|
|
|
|
|
return 0;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
void sftp_packet_free(sftp_packet packet) {
|
2009-04-22 19:22:22 +04:00
|
|
|
if (packet == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(packet->payload);
|
2009-04-22 19:22:22 +04:00
|
|
|
free(packet);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Initialize the sftp session with the server. */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_init(sftp_session sftp) {
|
|
|
|
sftp_packet packet = NULL;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer = NULL;
|
2009-04-22 19:41:14 +04:00
|
|
|
char *ext_name = NULL;
|
|
|
|
char *ext_data = NULL;
|
2014-04-22 20:46:29 +04:00
|
|
|
uint32_t version;
|
|
|
|
int rc;
|
2009-04-22 19:41:14 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-22 19:41:14 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-22 19:41:14 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_pack(buffer, "d", LIBSFTP_VERSION);
|
|
|
|
if (rc != SSH_OK) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-10-24 00:19:13 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_INIT, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-22 19:41:14 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-22 19:41:14 +04:00
|
|
|
|
|
|
|
packet = sftp_packet_read(sftp);
|
|
|
|
if (packet == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet->type != SSH_FXP_VERSION) {
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received a %d messages instead of SSH_FXP_VERSION", packet->type);
|
2005-07-05 05:21:44 +04:00
|
|
|
sftp_packet_free(packet);
|
2009-04-22 19:41:14 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-10-24 00:19:13 +04:00
|
|
|
/* TODO: are we sure there are 4 bytes ready? */
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_unpack(packet->payload, "d", &version);
|
|
|
|
if (rc != SSH_OK){
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_RARE,
|
2009-07-28 15:21:05 +04:00
|
|
|
"SFTP server version %d",
|
|
|
|
version);
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_unpack(packet->payload, "s", &ext_name);
|
|
|
|
while (rc == SSH_OK) {
|
2009-07-28 15:21:05 +04:00
|
|
|
int count = sftp->ext->count;
|
2015-01-13 13:18:41 +03:00
|
|
|
char **tmp;
|
2009-07-28 15:21:05 +04:00
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_unpack(packet->payload, "s", &ext_data);
|
|
|
|
if (rc == SSH_ERROR) {
|
2009-07-28 15:21:05 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_RARE,
|
2009-07-28 15:21:05 +04:00
|
|
|
"SFTP server extension: %s, version: %s",
|
|
|
|
ext_name, ext_data);
|
|
|
|
|
|
|
|
count++;
|
2015-01-13 13:18:41 +03:00
|
|
|
tmp = realloc(sftp->ext->name, count * sizeof(char *));
|
|
|
|
if (tmp == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-07-28 15:21:05 +04:00
|
|
|
SAFE_FREE(ext_name);
|
|
|
|
SAFE_FREE(ext_data);
|
|
|
|
return -1;
|
2009-04-22 19:41:14 +04:00
|
|
|
}
|
2015-01-13 13:18:41 +03:00
|
|
|
tmp[count - 1] = ext_name;
|
|
|
|
sftp->ext->name = tmp;
|
2009-07-28 15:21:05 +04:00
|
|
|
|
2015-01-13 13:18:41 +03:00
|
|
|
tmp = realloc(sftp->ext->data, count * sizeof(char *));
|
|
|
|
if (tmp == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-07-28 15:21:05 +04:00
|
|
|
SAFE_FREE(ext_name);
|
|
|
|
SAFE_FREE(ext_data);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-13 13:18:41 +03:00
|
|
|
tmp[count - 1] = ext_data;
|
|
|
|
sftp->ext->data = tmp;
|
2009-07-28 15:21:05 +04:00
|
|
|
|
|
|
|
sftp->ext->count = count;
|
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_unpack(packet->payload, "s", &ext_name);
|
2009-04-22 19:41:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
sftp_packet_free(packet);
|
|
|
|
|
|
|
|
sftp->version = sftp->server_version = version;
|
|
|
|
|
2009-07-28 15:21:05 +04:00
|
|
|
|
2009-04-22 19:41:14 +04:00
|
|
|
return 0;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
unsigned int sftp_extensions_get_count(sftp_session sftp) {
|
2009-07-28 16:13:22 +04:00
|
|
|
if (sftp == NULL || sftp->ext == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sftp->ext->count;
|
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
const char *sftp_extensions_get_name(sftp_session sftp, unsigned int idx) {
|
2009-10-24 00:19:13 +04:00
|
|
|
if (sftp == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (sftp->ext == NULL || sftp->ext->name == NULL) {
|
2011-09-17 12:28:39 +04:00
|
|
|
ssh_set_error_invalid(sftp->session);
|
2009-07-28 16:13:22 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-19 12:52:30 +04:00
|
|
|
if (idx > sftp->ext->count) {
|
2011-09-17 12:28:39 +04:00
|
|
|
ssh_set_error_invalid(sftp->session);
|
2009-07-28 16:13:22 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-19 12:52:30 +04:00
|
|
|
return sftp->ext->name[idx];
|
2009-07-28 16:13:22 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
const char *sftp_extensions_get_data(sftp_session sftp, unsigned int idx) {
|
2009-10-24 00:19:13 +04:00
|
|
|
if (sftp == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (sftp->ext == NULL || sftp->ext->name == NULL) {
|
2011-09-17 12:28:39 +04:00
|
|
|
ssh_set_error_invalid(sftp->session);
|
2009-07-28 16:13:22 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-19 12:52:30 +04:00
|
|
|
if (idx > sftp->ext->count) {
|
2011-09-17 12:28:39 +04:00
|
|
|
ssh_set_error_invalid(sftp->session);
|
2009-07-28 16:13:22 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-19 12:52:30 +04:00
|
|
|
return sftp->ext->data[idx];
|
2009-07-28 16:13:22 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_extension_supported(sftp_session sftp, const char *name,
|
2009-08-12 19:12:26 +04:00
|
|
|
const char *data) {
|
|
|
|
int i, n;
|
|
|
|
|
2012-10-05 12:37:53 +04:00
|
|
|
if (sftp == NULL || name == NULL || data == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-12 19:12:26 +04:00
|
|
|
n = sftp_extensions_get_count(sftp);
|
|
|
|
for (i = 0; i < n; i++) {
|
2012-10-05 12:37:53 +04:00
|
|
|
const char *ext_name = sftp_extensions_get_name(sftp, i);
|
|
|
|
const char *ext_data = sftp_extensions_get_data(sftp, i);
|
|
|
|
|
|
|
|
if (ext_name != NULL && ext_data != NULL &&
|
|
|
|
strcmp(ext_name, name) == 0 &&
|
|
|
|
strcmp(ext_data, data) == 0) {
|
2009-08-12 19:12:26 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static sftp_request_queue request_queue_new(sftp_message msg) {
|
|
|
|
sftp_request_queue queue = NULL;
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2016-05-04 15:56:22 +03:00
|
|
|
queue = calloc(1, sizeof(struct sftp_request_queue_struct));
|
2009-04-22 19:48:16 +04:00
|
|
|
if (queue == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(msg->sftp->session);
|
2009-04-22 19:48:16 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2009-04-22 19:48:16 +04:00
|
|
|
queue->message = msg;
|
|
|
|
|
|
|
|
return queue;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static void request_queue_free(sftp_request_queue queue) {
|
2009-04-22 19:48:16 +04:00
|
|
|
if (queue == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZERO_STRUCTP(queue);
|
|
|
|
SAFE_FREE(queue);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static int sftp_enqueue(sftp_session sftp, sftp_message msg) {
|
|
|
|
sftp_request_queue queue = NULL;
|
|
|
|
sftp_request_queue ptr;
|
2009-04-22 19:53:29 +04:00
|
|
|
|
|
|
|
queue = request_queue_new(msg);
|
|
|
|
if (queue == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_PACKET,
|
2009-04-22 19:53:29 +04:00
|
|
|
"Queued msg type %d id %d",
|
|
|
|
msg->id, msg->packet_type);
|
|
|
|
|
|
|
|
if(sftp->queue == NULL) {
|
|
|
|
sftp->queue = queue;
|
|
|
|
} else {
|
|
|
|
ptr = sftp->queue;
|
|
|
|
while(ptr->next) {
|
|
|
|
ptr=ptr->next; /* find end of linked list */
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-22 19:53:29 +04:00
|
|
|
ptr->next = queue; /* add it on bottom */
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-04-23 10:57:49 +04:00
|
|
|
/*
|
|
|
|
* Pulls of a message from the queue based on the ID.
|
|
|
|
* Returns NULL if no message has been found.
|
|
|
|
*/
|
2009-10-10 22:55:17 +04:00
|
|
|
static sftp_message sftp_dequeue(sftp_session sftp, uint32_t id){
|
|
|
|
sftp_request_queue prev = NULL;
|
|
|
|
sftp_request_queue queue;
|
|
|
|
sftp_message msg;
|
2009-04-23 10:57:49 +04:00
|
|
|
|
|
|
|
if(sftp->queue == NULL) {
|
2005-07-05 05:21:44 +04:00
|
|
|
return NULL;
|
2009-04-23 10:57:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
queue = sftp->queue;
|
|
|
|
while (queue) {
|
|
|
|
if(queue->message->id == id) {
|
|
|
|
/* remove from queue */
|
|
|
|
if (prev == NULL) {
|
|
|
|
sftp->queue = queue->next;
|
|
|
|
} else {
|
|
|
|
prev->next = queue->next;
|
|
|
|
}
|
|
|
|
msg = queue->message;
|
|
|
|
request_queue_free(queue);
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_PACKET,
|
2009-04-23 10:57:49 +04:00
|
|
|
"Dequeued msg id %d type %d",
|
|
|
|
msg->id,
|
|
|
|
msg->packet_type);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
prev = queue;
|
|
|
|
queue = queue->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/*
|
2009-04-23 10:59:18 +04:00
|
|
|
* Assigns a new SFTP ID for new requests and assures there is no collision
|
|
|
|
* between them.
|
2008-12-22 12:51:31 +03:00
|
|
|
* Returns a new ID ready to use in a request
|
2008-11-05 19:42:43 +03:00
|
|
|
*/
|
2009-10-10 22:55:17 +04:00
|
|
|
static inline uint32_t sftp_get_new_id(sftp_session session) {
|
2009-04-23 10:59:18 +04:00
|
|
|
return ++session->id_counter;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static sftp_status_message parse_status_msg(sftp_message msg){
|
|
|
|
sftp_status_message status;
|
2014-04-22 20:46:29 +04:00
|
|
|
int rc;
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2009-04-23 11:06:07 +04:00
|
|
|
if (msg->packet_type != SSH_FXP_STATUS) {
|
|
|
|
ssh_set_error(msg->sftp->session, SSH_FATAL,
|
|
|
|
"Not a ssh_fxp_status message passed in!");
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2016-05-04 15:56:22 +03:00
|
|
|
status = calloc(1, sizeof(struct sftp_status_message_struct));
|
2009-04-23 11:06:07 +04:00
|
|
|
if (status == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(msg->sftp->session);
|
2009-04-23 11:06:07 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
status->id = msg->id;
|
2014-07-06 02:29:58 +04:00
|
|
|
rc = ssh_buffer_unpack(msg->payload, "d",
|
2014-04-22 20:46:29 +04:00
|
|
|
&status->status);
|
|
|
|
if (rc != SSH_OK){
|
2009-04-23 11:06:07 +04:00
|
|
|
SAFE_FREE(status);
|
|
|
|
ssh_set_error(msg->sftp->session, SSH_FATAL,
|
|
|
|
"Invalid SSH_FXP_STATUS message");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_unpack(msg->payload, "ss",
|
|
|
|
&status->errormsg,
|
|
|
|
&status->langmsg);
|
|
|
|
|
|
|
|
if(rc != SSH_OK && msg->sftp->version >=3){
|
2009-10-23 14:19:22 +04:00
|
|
|
/* These are mandatory from version 3 */
|
|
|
|
SAFE_FREE(status);
|
|
|
|
ssh_set_error(msg->sftp->session, SSH_FATAL,
|
|
|
|
"Invalid SSH_FXP_STATUS message");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-04-22 20:46:29 +04:00
|
|
|
if (status->errormsg == NULL)
|
2009-10-23 14:19:22 +04:00
|
|
|
status->errormsg = strdup("No error message in packet");
|
2014-04-22 20:46:29 +04:00
|
|
|
if (status->langmsg == NULL)
|
2009-10-23 14:19:22 +04:00
|
|
|
status->langmsg = strdup("");
|
2009-04-23 11:06:07 +04:00
|
|
|
if (status->errormsg == NULL || status->langmsg == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(msg->sftp->session);
|
2009-04-23 11:08:57 +04:00
|
|
|
status_msg_free(status);
|
2009-04-23 11:06:07 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static void status_msg_free(sftp_status_message status){
|
2009-04-23 11:08:57 +04:00
|
|
|
if (status == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-23 11:10:10 +04:00
|
|
|
SAFE_FREE(status->errormsg);
|
|
|
|
SAFE_FREE(status->langmsg);
|
2009-04-23 11:08:57 +04:00
|
|
|
SAFE_FREE(status);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static sftp_file parse_handle_msg(sftp_message msg){
|
|
|
|
sftp_file file;
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2009-04-23 11:13:25 +04:00
|
|
|
if(msg->packet_type != SSH_FXP_HANDLE) {
|
|
|
|
ssh_set_error(msg->sftp->session, SSH_FATAL,
|
|
|
|
"Not a ssh_fxp_handle message passed in!");
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-02 00:09:20 +04:00
|
|
|
|
2016-05-04 15:56:22 +03:00
|
|
|
file = calloc(1, sizeof(struct sftp_file_struct));
|
2009-04-23 11:13:25 +04:00
|
|
|
if (file == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(msg->sftp->session);
|
2009-04-23 11:13:25 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-17 10:43:33 +03:00
|
|
|
file->handle = ssh_buffer_get_ssh_string(msg->payload);
|
2009-04-23 11:13:25 +04:00
|
|
|
if (file->handle == NULL) {
|
|
|
|
ssh_set_error(msg->sftp->session, SSH_FATAL,
|
|
|
|
"Invalid SSH_FXP_HANDLE message");
|
|
|
|
SAFE_FREE(file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
file->sftp = msg->sftp;
|
|
|
|
file->offset = 0;
|
|
|
|
file->eof = 0;
|
|
|
|
|
|
|
|
return file;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2008-11-24 21:05:55 +03:00
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Open a directory */
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_dir sftp_opendir(sftp_session sftp, const char *path){
|
|
|
|
sftp_message msg = NULL;
|
|
|
|
sftp_file file = NULL;
|
|
|
|
sftp_dir dir = NULL;
|
|
|
|
sftp_status_message status;
|
2009-07-24 23:45:41 +04:00
|
|
|
ssh_string path_s;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer payload;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t id;
|
2009-04-23 13:09:36 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
payload = ssh_buffer_new();
|
2009-04-23 13:09:36 +04:00
|
|
|
if (payload == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-23 13:09:36 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
path_s = ssh_string_from_char(path);
|
2009-04-23 13:09:36 +04:00
|
|
|
if (path_s == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(payload);
|
2009-04-23 13:09:36 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = sftp_get_new_id(sftp);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(payload, htonl(id)) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(payload, path_s) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(payload);
|
|
|
|
ssh_string_free(path_s);
|
2009-04-23 13:09:36 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_string_free(path_s);
|
2009-04-23 13:09:36 +04:00
|
|
|
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_OPENDIR, payload) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(payload);
|
2005-07-05 05:21:44 +04:00
|
|
|
return NULL;
|
2009-04-23 13:09:36 +04:00
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(payload);
|
2009-04-23 13:09:36 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
/* something nasty has happened */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg->packet_type) {
|
|
|
|
case 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);
|
|
|
|
return NULL;
|
|
|
|
case SSH_FXP_HANDLE:
|
|
|
|
file = parse_handle_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (file != NULL) {
|
2016-05-04 15:56:22 +03:00
|
|
|
dir = calloc(1, sizeof(struct sftp_dir_struct));
|
2009-04-23 13:09:36 +04:00
|
|
|
if (dir == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2012-10-05 13:08:05 +04:00
|
|
|
free(file);
|
2009-04-23 13:09:36 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir->sftp = sftp;
|
|
|
|
dir->name = strdup(path);
|
|
|
|
if (dir->name == NULL) {
|
|
|
|
SAFE_FREE(dir);
|
|
|
|
SAFE_FREE(file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
dir->handle = file->handle;
|
|
|
|
SAFE_FREE(file);
|
|
|
|
}
|
|
|
|
return dir;
|
|
|
|
default:
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d during opendir!", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-04-23 13:37:28 +04:00
|
|
|
/*
|
|
|
|
* Parse the attributes from a payload from some messages. It is coded on
|
|
|
|
* baselines from the protocol version 4.
|
|
|
|
* This code is more or less dead but maybe we need it in future.
|
|
|
|
*/
|
2009-10-10 22:55:17 +04:00
|
|
|
static sftp_attributes sftp_parse_attr_4(sftp_session sftp, ssh_buffer buf,
|
2009-02-02 19:47:51 +03:00
|
|
|
int expectnames) {
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_attributes attr;
|
2009-07-24 23:45:41 +04:00
|
|
|
ssh_string owner = NULL;
|
|
|
|
ssh_string group = NULL;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t flags = 0;
|
2009-04-23 13:37:28 +04:00
|
|
|
int ok = 0;
|
2009-02-02 19:47:51 +03:00
|
|
|
|
2009-04-23 13:37:28 +04:00
|
|
|
/* unused member variable */
|
|
|
|
(void) expectnames;
|
2009-02-02 19:47:51 +03:00
|
|
|
|
2016-05-04 15:56:22 +03:00
|
|
|
attr = calloc(1, sizeof(struct sftp_attributes_struct));
|
2009-04-23 13:37:28 +04:00
|
|
|
if (attr == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-23 13:37:28 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This isn't really a loop, but it is like a try..catch.. */
|
|
|
|
do {
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_get_u32(buf, &flags) != 4) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
2009-04-02 00:09:20 +04:00
|
|
|
}
|
|
|
|
|
2009-04-23 13:37:28 +04:00
|
|
|
flags = ntohl(flags);
|
|
|
|
attr->flags = flags;
|
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_SIZE) {
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_get_u64(buf, &attr->size) != 8) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
attr->size = ntohll(attr->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_OWNERGROUP) {
|
2015-09-17 10:43:33 +03:00
|
|
|
owner = ssh_buffer_get_ssh_string(buf);
|
2012-10-08 22:57:49 +04:00
|
|
|
if (owner == NULL) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-10-08 22:57:49 +04:00
|
|
|
attr->owner = ssh_string_to_char(owner);
|
2014-01-16 18:27:46 +04:00
|
|
|
ssh_string_free(owner);
|
2012-10-08 22:57:49 +04:00
|
|
|
if (attr->owner == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-17 10:43:33 +03:00
|
|
|
group = ssh_buffer_get_ssh_string(buf);
|
2012-10-08 22:57:49 +04:00
|
|
|
if (group == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
attr->group = ssh_string_to_char(group);
|
2014-01-16 18:27:46 +04:00
|
|
|
ssh_string_free(group);
|
2012-10-08 22:57:49 +04:00
|
|
|
if (attr->group == NULL) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_get_u32(buf, &attr->permissions) != 4) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
attr->permissions = ntohl(attr->permissions);
|
|
|
|
|
2009-05-05 14:03:34 +04:00
|
|
|
/* FIXME on windows! */
|
2015-07-22 16:24:14 +03:00
|
|
|
switch (attr->permissions & SSH_S_IFMT) {
|
|
|
|
case SSH_S_IFSOCK:
|
|
|
|
case SSH_S_IFBLK:
|
|
|
|
case SSH_S_IFCHR:
|
|
|
|
case SSH_S_IFIFO:
|
2009-04-23 13:37:28 +04:00
|
|
|
attr->type = SSH_FILEXFER_TYPE_SPECIAL;
|
|
|
|
break;
|
2015-07-22 16:24:14 +03:00
|
|
|
case SSH_S_IFLNK:
|
2009-04-23 13:37:28 +04:00
|
|
|
attr->type = SSH_FILEXFER_TYPE_SYMLINK;
|
|
|
|
break;
|
2015-07-22 16:24:14 +03:00
|
|
|
case SSH_S_IFREG:
|
2009-04-23 13:37:28 +04:00
|
|
|
attr->type = SSH_FILEXFER_TYPE_REGULAR;
|
|
|
|
break;
|
2015-07-22 16:24:14 +03:00
|
|
|
case SSH_S_IFDIR:
|
2009-04-23 13:37:28 +04:00
|
|
|
attr->type = SSH_FILEXFER_TYPE_DIRECTORY;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
attr->type = SSH_FILEXFER_TYPE_UNKNOWN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_ACCESSTIME) {
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_get_u64(buf, &attr->atime64) != 8) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
attr->atime64 = ntohll(attr->atime64);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_SUBSECOND_TIMES) {
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_get_u32(buf, &attr->atime_nseconds) != 4) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
attr->atime_nseconds = ntohl(attr->atime_nseconds);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-23 13:37:28 +04:00
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_CREATETIME) {
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_get_u64(buf, &attr->createtime) != 8) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
attr->createtime = ntohll(attr->createtime);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-23 13:37:28 +04:00
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_SUBSECOND_TIMES) {
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_get_u32(buf, &attr->createtime_nseconds) != 4) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
attr->createtime_nseconds = ntohl(attr->createtime_nseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_MODIFYTIME) {
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_get_u64(buf, &attr->mtime64) != 8) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
attr->mtime64 = ntohll(attr->mtime64);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_SUBSECOND_TIMES) {
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_get_u32(buf, &attr->mtime_nseconds) != 4) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
attr->mtime_nseconds = ntohl(attr->mtime_nseconds);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-23 13:37:28 +04:00
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_ACL) {
|
2015-09-17 10:43:33 +03:00
|
|
|
if ((attr->acl = ssh_buffer_get_ssh_string(buf)) == NULL) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_EXTENDED) {
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_get_u32(buf,&attr->extended_count) != 4) {
|
2009-04-23 13:37:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
attr->extended_count = ntohl(attr->extended_count);
|
|
|
|
|
|
|
|
while(attr->extended_count &&
|
2015-09-17 10:43:33 +03:00
|
|
|
(attr->extended_type = ssh_buffer_get_ssh_string(buf)) &&
|
|
|
|
(attr->extended_data = ssh_buffer_get_ssh_string(buf))){
|
2009-04-23 13:37:28 +04:00
|
|
|
attr->extended_count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr->extended_count) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ok = 1;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (ok == 0) {
|
|
|
|
/* break issued somewhere */
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_string_free(attr->acl);
|
|
|
|
ssh_string_free(attr->extended_type);
|
|
|
|
ssh_string_free(attr->extended_data);
|
2009-04-23 13:37:28 +04:00
|
|
|
SAFE_FREE(attr->owner);
|
|
|
|
SAFE_FREE(attr->group);
|
|
|
|
SAFE_FREE(attr);
|
|
|
|
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL, "Invalid ATTR structure");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return attr;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2010-02-24 00:50:43 +03:00
|
|
|
enum sftp_longname_field_e {
|
|
|
|
SFTP_LONGNAME_PERM = 0,
|
|
|
|
SFTP_LONGNAME_FIXME,
|
|
|
|
SFTP_LONGNAME_OWNER,
|
|
|
|
SFTP_LONGNAME_GROUP,
|
|
|
|
SFTP_LONGNAME_SIZE,
|
|
|
|
SFTP_LONGNAME_DATE,
|
|
|
|
SFTP_LONGNAME_TIME,
|
|
|
|
SFTP_LONGNAME_NAME,
|
|
|
|
};
|
|
|
|
|
|
|
|
static char *sftp_parse_longname(const char *longname,
|
|
|
|
enum sftp_longname_field_e longname_field) {
|
|
|
|
const char *p, *q;
|
2010-03-02 16:15:20 +03:00
|
|
|
size_t len, field = 0;
|
|
|
|
char *x;
|
2010-02-24 00:50:43 +03:00
|
|
|
|
|
|
|
p = longname;
|
|
|
|
/* Find the beginning of the field which is specified by sftp_longanme_field_e. */
|
|
|
|
while(field != longname_field) {
|
|
|
|
if(isspace(*p)) {
|
|
|
|
field++;
|
|
|
|
p++;
|
|
|
|
while(*p && isspace(*p)) {
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
q = p;
|
|
|
|
while (! isspace(*q)) {
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
|
2010-03-02 16:15:20 +03:00
|
|
|
/* There is no strndup on windows */
|
|
|
|
len = q - p + 1;
|
2016-05-04 15:56:22 +03:00
|
|
|
x = calloc(1, len);
|
2010-03-02 16:15:20 +03:00
|
|
|
if (x == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(x, len, "%s", p);
|
|
|
|
|
|
|
|
return x;
|
2010-02-24 00:50:43 +03:00
|
|
|
}
|
|
|
|
|
2009-10-23 14:19:22 +04:00
|
|
|
/* sftp version 0-3 code. It is different from the v4 */
|
2005-07-05 05:21:44 +04:00
|
|
|
/* maybe a paste of the draft is better than the code */
|
|
|
|
/*
|
|
|
|
uint32 flags
|
|
|
|
uint64 size present only if flag SSH_FILEXFER_ATTR_SIZE
|
|
|
|
uint32 uid present only if flag SSH_FILEXFER_ATTR_UIDGID
|
|
|
|
uint32 gid present only if flag SSH_FILEXFER_ATTR_UIDGID
|
|
|
|
uint32 permissions present only if flag SSH_FILEXFER_ATTR_PERMISSIONS
|
|
|
|
uint32 atime present only if flag SSH_FILEXFER_ACMODTIME
|
|
|
|
uint32 mtime present only if flag SSH_FILEXFER_ACMODTIME
|
|
|
|
uint32 extended_count present only if flag SSH_FILEXFER_ATTR_EXTENDED
|
|
|
|
string extended_type
|
|
|
|
string extended_data
|
|
|
|
... more extended data (extended_type - extended_data pairs),
|
|
|
|
so that number of pairs equals extended_count */
|
2009-10-10 22:55:17 +04:00
|
|
|
static sftp_attributes sftp_parse_attr_3(sftp_session sftp, ssh_buffer buf,
|
2014-04-22 20:46:29 +04:00
|
|
|
int expectname) {
|
|
|
|
sftp_attributes attr;
|
|
|
|
int rc;
|
2009-04-23 14:27:31 +04:00
|
|
|
|
2016-05-04 15:56:22 +03:00
|
|
|
attr = calloc(1, sizeof(struct sftp_attributes_struct));
|
2014-04-22 20:46:29 +04:00
|
|
|
if (attr == NULL) {
|
|
|
|
ssh_set_error_oom(sftp->session);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-23 14:27:31 +04:00
|
|
|
|
|
|
|
if (expectname) {
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_unpack(buf, "ss",
|
|
|
|
&attr->name,
|
|
|
|
&attr->longname);
|
|
|
|
if (rc != SSH_OK){
|
|
|
|
goto error;
|
2010-02-24 02:25:38 +03:00
|
|
|
}
|
2014-04-22 20:46:29 +04:00
|
|
|
SSH_LOG(SSH_LOG_RARE, "Name: %s", attr->name);
|
2010-02-24 02:25:38 +03:00
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
/* Set owner and group if we talk to openssh and have the longname */
|
|
|
|
if (ssh_get_openssh_version(sftp->session)) {
|
|
|
|
attr->owner = sftp_parse_longname(attr->longname, SFTP_LONGNAME_OWNER);
|
|
|
|
if (attr->owner == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
attr->group = sftp_parse_longname(attr->longname, SFTP_LONGNAME_GROUP);
|
|
|
|
if (attr->group == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
2010-02-24 02:25:38 +03:00
|
|
|
}
|
2009-04-02 00:09:20 +04:00
|
|
|
}
|
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_unpack(buf, "d", &attr->flags);
|
|
|
|
if (rc != SSH_OK){
|
|
|
|
goto error;
|
2009-04-23 14:27:31 +04:00
|
|
|
}
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_RARE,
|
2014-04-22 20:46:29 +04:00
|
|
|
"Flags: %.8lx\n", (long unsigned int) attr->flags);
|
2009-04-23 14:27:31 +04:00
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
if (attr->flags & SSH_FILEXFER_ATTR_SIZE) {
|
|
|
|
rc = ssh_buffer_unpack(buf, "q", &attr->size);
|
|
|
|
if(rc != SSH_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
SSH_LOG(SSH_LOG_RARE,
|
|
|
|
"Size: %llu\n",
|
|
|
|
(long long unsigned int) attr->size);
|
2009-04-23 14:27:31 +04:00
|
|
|
}
|
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
if (attr->flags & SSH_FILEXFER_ATTR_UIDGID) {
|
|
|
|
rc = ssh_buffer_unpack(buf, "dd",
|
|
|
|
&attr->uid,
|
|
|
|
&attr->gid);
|
|
|
|
if (rc != SSH_OK){
|
|
|
|
goto error;
|
|
|
|
}
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-23 14:27:31 +04:00
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
if (attr->flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
|
|
|
|
rc = ssh_buffer_unpack(buf, "d", &attr->permissions);
|
|
|
|
if (rc != SSH_OK){
|
|
|
|
goto error;
|
|
|
|
}
|
2009-04-23 14:27:31 +04:00
|
|
|
|
2015-07-22 16:24:14 +03:00
|
|
|
switch (attr->permissions & SSH_S_IFMT) {
|
|
|
|
case SSH_S_IFSOCK:
|
|
|
|
case SSH_S_IFBLK:
|
|
|
|
case SSH_S_IFCHR:
|
|
|
|
case SSH_S_IFIFO:
|
2014-04-22 20:46:29 +04:00
|
|
|
attr->type = SSH_FILEXFER_TYPE_SPECIAL;
|
|
|
|
break;
|
2015-07-22 16:24:14 +03:00
|
|
|
case SSH_S_IFLNK:
|
2014-04-22 20:46:29 +04:00
|
|
|
attr->type = SSH_FILEXFER_TYPE_SYMLINK;
|
|
|
|
break;
|
2015-07-22 16:24:14 +03:00
|
|
|
case SSH_S_IFREG:
|
2014-04-22 20:46:29 +04:00
|
|
|
attr->type = SSH_FILEXFER_TYPE_REGULAR;
|
|
|
|
break;
|
2015-07-22 16:24:14 +03:00
|
|
|
case SSH_S_IFDIR:
|
2014-04-22 20:46:29 +04:00
|
|
|
attr->type = SSH_FILEXFER_TYPE_DIRECTORY;
|
|
|
|
break;
|
2009-04-23 14:27:31 +04:00
|
|
|
default:
|
2014-04-22 20:46:29 +04:00
|
|
|
attr->type = SSH_FILEXFER_TYPE_UNKNOWN;
|
|
|
|
break;
|
|
|
|
}
|
2009-04-23 14:27:31 +04:00
|
|
|
}
|
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
if (attr->flags & SSH_FILEXFER_ATTR_ACMODTIME) {
|
|
|
|
rc = ssh_buffer_unpack(buf, "dd",
|
|
|
|
&attr->atime,
|
|
|
|
&attr->mtime);
|
|
|
|
if (rc != SSH_OK){
|
|
|
|
goto error;
|
|
|
|
}
|
2009-04-23 14:27:31 +04:00
|
|
|
}
|
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
if (attr->flags & SSH_FILEXFER_ATTR_EXTENDED) {
|
|
|
|
rc = ssh_buffer_unpack(buf, "d", &attr->extended_count);
|
|
|
|
if (rc != SSH_OK){
|
|
|
|
goto error;
|
|
|
|
}
|
2009-04-23 14:27:31 +04:00
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
if (attr->extended_count > 0){
|
|
|
|
rc = ssh_buffer_unpack(buf, "ss",
|
|
|
|
&attr->extended_type,
|
|
|
|
&attr->extended_data);
|
|
|
|
if (rc != SSH_OK){
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
attr->extended_count--;
|
|
|
|
}
|
|
|
|
/* just ignore the remaining extensions */
|
2009-04-23 14:27:31 +04:00
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
while (attr->extended_count > 0){
|
|
|
|
ssh_string tmp1,tmp2;
|
|
|
|
rc = ssh_buffer_unpack(buf, "SS", &tmp1, &tmp2);
|
|
|
|
if (rc != SSH_OK){
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
SAFE_FREE(tmp1);
|
|
|
|
SAFE_FREE(tmp2);
|
|
|
|
attr->extended_count--;
|
|
|
|
}
|
2009-04-23 14:27:31 +04:00
|
|
|
}
|
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
return attr;
|
|
|
|
|
|
|
|
error:
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_string_free(attr->extended_type);
|
|
|
|
ssh_string_free(attr->extended_data);
|
2009-04-23 14:27:31 +04:00
|
|
|
SAFE_FREE(attr->name);
|
|
|
|
SAFE_FREE(attr->longname);
|
2010-02-24 00:50:43 +03:00
|
|
|
SAFE_FREE(attr->owner);
|
|
|
|
SAFE_FREE(attr->group);
|
2009-04-23 14:27:31 +04:00
|
|
|
SAFE_FREE(attr);
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL, "Invalid ATTR structure");
|
|
|
|
|
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-04-23 15:36:57 +04:00
|
|
|
/* FIXME is this really needed as a public function? */
|
2009-10-10 22:55:17 +04:00
|
|
|
int buffer_add_attributes(ssh_buffer buffer, sftp_attributes attr) {
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t flags = (attr ? attr->flags : 0);
|
2014-04-22 20:46:29 +04:00
|
|
|
int rc;
|
2009-04-23 15:36:57 +04:00
|
|
|
|
|
|
|
flags &= (SSH_FILEXFER_ATTR_SIZE | SSH_FILEXFER_ATTR_UIDGID |
|
|
|
|
SSH_FILEXFER_ATTR_PERMISSIONS | SSH_FILEXFER_ATTR_ACMODTIME);
|
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_pack(buffer, "d", flags);
|
|
|
|
if (rc != SSH_OK) {
|
2009-04-23 15:36:57 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
if (attr != NULL) {
|
2009-04-23 15:36:57 +04:00
|
|
|
if (flags & SSH_FILEXFER_ATTR_SIZE) {
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_pack(buffer, "q", attr->size);
|
|
|
|
if (rc != SSH_OK) {
|
2009-04-23 15:36:57 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_UIDGID) {
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_pack(buffer, "dd", attr->uid, attr->gid);
|
|
|
|
if (rc != SSH_OK) {
|
2009-04-23 15:36:57 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_pack(buffer, "d", attr->permissions);
|
|
|
|
if (rc != SSH_OK) {
|
2009-04-23 15:36:57 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SSH_FILEXFER_ATTR_ACMODTIME) {
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_pack(buffer, "dd", attr->atime, attr->mtime);
|
|
|
|
if (rc != SSH_OK) {
|
2009-04-23 15:36:57 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-11-02 01:28:39 +03:00
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_attributes sftp_parse_attr(sftp_session session, ssh_buffer buf,
|
2009-04-23 15:38:55 +04:00
|
|
|
int expectname) {
|
|
|
|
switch(session->version) {
|
|
|
|
case 4:
|
|
|
|
return sftp_parse_attr_4(session, buf, expectname);
|
|
|
|
case 3:
|
2009-10-23 14:19:22 +04:00
|
|
|
case 2:
|
|
|
|
case 1:
|
|
|
|
case 0:
|
2009-04-23 15:38:55 +04:00
|
|
|
return sftp_parse_attr_3(session, buf, expectname);
|
|
|
|
default:
|
|
|
|
ssh_set_error(session->session, SSH_FATAL,
|
|
|
|
"Version %d unsupported by client", session->server_version);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Get the version of the SFTP protocol supported by the server */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_server_version(sftp_session sftp) {
|
2009-04-23 15:39:57 +04:00
|
|
|
return sftp->server_version;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Get a single file attributes structure of a directory. */
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_attributes sftp_readdir(sftp_session sftp, sftp_dir dir) {
|
|
|
|
sftp_message msg = NULL;
|
|
|
|
sftp_status_message status;
|
|
|
|
sftp_attributes attr;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer payload;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t id;
|
2009-04-23 16:21:30 +04:00
|
|
|
|
|
|
|
if (dir->buffer == NULL) {
|
2010-05-14 02:51:08 +04:00
|
|
|
payload = ssh_buffer_new();
|
2009-04-23 16:21:30 +04:00
|
|
|
if (payload == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-23 16:21:30 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = sftp_get_new_id(sftp);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(payload, htonl(id)) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(payload, dir->handle) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(payload);
|
2009-04-23 16:21:30 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_READDIR, payload) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(payload);
|
2009-04-23 16:21:30 +04:00
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(payload);
|
2009-04-23 16:21:30 +04:00
|
|
|
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_PACKET,
|
2009-04-23 16:21:30 +04:00
|
|
|
"Sent a ssh_fxp_readdir with id %d", id);
|
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
/* something nasty has happened */
|
2005-07-05 05:21:44 +04:00
|
|
|
return NULL;
|
2009-04-23 16:21:30 +04:00
|
|
|
}
|
|
|
|
msg = sftp_dequeue(sftp, id);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-23 16:21:30 +04:00
|
|
|
|
|
|
|
switch (msg->packet_type){
|
|
|
|
case SSH_FXP_STATUS:
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sftp_set_error(sftp, status->status);
|
|
|
|
switch (status->status) {
|
|
|
|
case SSH_FX_EOF:
|
|
|
|
dir->eof = 1;
|
|
|
|
status_msg_free(status);
|
|
|
|
return NULL;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Unknown error status: %d", status->status);
|
|
|
|
status_msg_free(status);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
case SSH_FXP_NAME:
|
2015-09-17 10:43:33 +03:00
|
|
|
ssh_buffer_get_u32(msg->payload, &dir->count);
|
2009-04-23 16:21:30 +04:00
|
|
|
dir->count = ntohl(dir->count);
|
|
|
|
dir->buffer = msg->payload;
|
|
|
|
msg->payload = NULL;
|
|
|
|
sftp_message_free(msg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Unsupported message back %d", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
|
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-23 16:21:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now dir->buffer contains a buffer and dir->count != 0 */
|
|
|
|
if (dir->count == 0) {
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Count of files sent by the server is zero, which is invalid, or "
|
|
|
|
"libsftp bug");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_RARE, "Count is %d", dir->count);
|
2009-04-23 16:21:30 +04:00
|
|
|
|
|
|
|
attr = sftp_parse_attr(sftp, dir->buffer, 1);
|
|
|
|
if (attr == NULL) {
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Couldn't parse the SFTP attributes");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir->count--;
|
|
|
|
if (dir->count == 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(dir->buffer);
|
2009-04-23 16:21:30 +04:00
|
|
|
dir->buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return attr;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Tell if the directory has reached EOF (End Of File). */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_dir_eof(sftp_dir dir) {
|
2009-04-23 16:21:30 +04:00
|
|
|
return dir->eof;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Free a SFTP_ATTRIBUTE handle */
|
2009-10-10 22:55:17 +04:00
|
|
|
void sftp_attributes_free(sftp_attributes file){
|
2009-04-23 16:23:38 +04:00
|
|
|
if (file == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_string_free(file->acl);
|
|
|
|
ssh_string_free(file->extended_data);
|
|
|
|
ssh_string_free(file->extended_type);
|
2009-04-23 16:23:38 +04:00
|
|
|
|
|
|
|
SAFE_FREE(file->name);
|
|
|
|
SAFE_FREE(file->longname);
|
|
|
|
SAFE_FREE(file->group);
|
|
|
|
SAFE_FREE(file->owner);
|
|
|
|
|
|
|
|
SAFE_FREE(file);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static int sftp_handle_close(sftp_session sftp, ssh_string handle) {
|
|
|
|
sftp_status_message status;
|
|
|
|
sftp_message msg = NULL;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer = NULL;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t id;
|
2009-04-23 19:08:34 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-23 19:08:34 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-23 19:08:34 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = sftp_get_new_id(sftp);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(buffer, handle) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-10-24 00:19:13 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_CLOSE ,buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-23 19:08:34 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-23 19:08:34 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
/* something nasty has happened */
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-23 19:08:34 +04:00
|
|
|
msg = sftp_dequeue(sftp,id);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg->packet_type) {
|
|
|
|
case SSH_FXP_STATUS:
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if(status == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sftp_set_error(sftp, status->status);
|
|
|
|
switch (status->status) {
|
|
|
|
case SSH_FX_OK:
|
|
|
|
status_msg_free(status);
|
|
|
|
return 0;
|
|
|
|
break;
|
2005-07-05 05:21:44 +04:00
|
|
|
default:
|
2009-04-23 19:08:34 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d during sftp_handle_close!", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Close an open file handle. */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_close(sftp_file file){
|
2009-04-23 19:10:59 +04:00
|
|
|
int err = SSH_NO_ERROR;
|
2005-07-05 05:21:44 +04:00
|
|
|
|
2009-04-23 19:10:59 +04:00
|
|
|
SAFE_FREE(file->name);
|
|
|
|
if (file->handle){
|
|
|
|
err = sftp_handle_close(file->sftp,file->handle);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_string_free(file->handle);
|
2009-04-23 19:10:59 +04:00
|
|
|
}
|
|
|
|
/* FIXME: check server response and implement errno */
|
|
|
|
SAFE_FREE(file);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2008-12-22 12:51:31 +03:00
|
|
|
|
|
|
|
/* Close an open directory. */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_closedir(sftp_dir dir){
|
2009-04-23 19:15:02 +04:00
|
|
|
int err = SSH_NO_ERROR;
|
|
|
|
|
|
|
|
SAFE_FREE(dir->name);
|
|
|
|
if (dir->handle) {
|
|
|
|
err = sftp_handle_close(dir->sftp, dir->handle);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_string_free(dir->handle);
|
2009-04-23 19:15:02 +04:00
|
|
|
}
|
|
|
|
/* FIXME: check server response and implement errno */
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(dir->buffer);
|
2009-04-23 19:15:02 +04:00
|
|
|
SAFE_FREE(dir);
|
|
|
|
|
|
|
|
return err;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Open a file on the server. */
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_file sftp_open(sftp_session sftp, const char *file, int flags,
|
2009-04-27 14:02:16 +04:00
|
|
|
mode_t mode) {
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_message msg = NULL;
|
|
|
|
sftp_status_message status;
|
|
|
|
struct sftp_attributes_struct attr;
|
|
|
|
sftp_file handle;
|
2009-07-24 23:45:41 +04:00
|
|
|
ssh_string filename;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer;
|
2016-07-01 11:57:38 +03:00
|
|
|
sftp_attributes stat_data;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t sftp_flags = 0;
|
|
|
|
uint32_t id;
|
2009-04-24 13:05:52 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-24 13:05:52 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-24 13:05:52 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
filename = ssh_string_from_char(file);
|
2009-04-24 13:05:52 +04:00
|
|
|
if (filename == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2008-11-02 01:28:39 +03:00
|
|
|
return NULL;
|
2009-04-24 13:05:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ZERO_STRUCT(attr);
|
|
|
|
attr.permissions = mode;
|
|
|
|
attr.flags = SSH_FILEXFER_ATTR_PERMISSIONS;
|
|
|
|
|
2016-06-30 13:27:43 +03:00
|
|
|
if ((flags & O_RDWR) == O_RDWR) {
|
2009-04-24 13:05:52 +04:00
|
|
|
sftp_flags |= (SSH_FXF_WRITE | SSH_FXF_READ);
|
2016-06-30 13:27:43 +03:00
|
|
|
} else if ((flags & O_WRONLY) == O_WRONLY) {
|
|
|
|
sftp_flags |= SSH_FXF_WRITE;
|
|
|
|
} else {
|
|
|
|
sftp_flags |= SSH_FXF_READ;
|
|
|
|
}
|
2015-07-22 16:26:56 +03:00
|
|
|
if ((flags & O_CREAT) == O_CREAT)
|
2009-04-24 13:05:52 +04:00
|
|
|
sftp_flags |= SSH_FXF_CREAT;
|
2015-07-22 16:26:56 +03:00
|
|
|
if ((flags & O_TRUNC) == O_TRUNC)
|
2009-04-24 13:05:52 +04:00
|
|
|
sftp_flags |= SSH_FXF_TRUNC;
|
2015-07-22 16:26:56 +03:00
|
|
|
if ((flags & O_EXCL) == O_EXCL)
|
2009-04-24 13:05:52 +04:00
|
|
|
sftp_flags |= SSH_FXF_EXCL;
|
2016-07-01 11:57:38 +03:00
|
|
|
if ((flags & O_APPEND) == O_APPEND) {
|
|
|
|
sftp_flags |= SSH_FXF_APPEND;
|
|
|
|
}
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_PACKET,"Opening file %s with sftp flags %x",file,sftp_flags);
|
2009-04-24 13:05:52 +04:00
|
|
|
id = sftp_get_new_id(sftp);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(buffer, filename) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(filename);
|
2009-04-24 13:05:52 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_string_free(filename);
|
2009-04-24 13:05:52 +04:00
|
|
|
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(buffer, htonl(sftp_flags)) < 0 ||
|
2009-10-24 00:19:13 +04:00
|
|
|
buffer_add_attributes(buffer, &attr) < 0) {
|
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-10-24 00:19:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_OPEN, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-24 13:05:52 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-24 13:05:52 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
/* something nasty has happened */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg->packet_type) {
|
|
|
|
case 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);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
case SSH_FXP_HANDLE:
|
|
|
|
handle = parse_handle_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
2016-07-01 11:57:38 +03:00
|
|
|
if ((flags & O_APPEND) == O_APPEND) {
|
|
|
|
stat_data = sftp_stat(sftp, file);
|
|
|
|
if (stat_data == NULL) {
|
|
|
|
sftp_close(handle);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((stat_data->flags & SSH_FILEXFER_ATTR_SIZE) != SSH_FILEXFER_ATTR_SIZE) {
|
|
|
|
ssh_set_error(sftp->session,
|
|
|
|
SSH_FATAL,
|
|
|
|
"Cannot open in append mode. Unknown file size.");
|
|
|
|
sftp_close(handle);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle->offset = stat_data->size;
|
|
|
|
}
|
2009-04-24 13:05:52 +04:00
|
|
|
return handle;
|
|
|
|
default:
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d during open!", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
void sftp_file_set_nonblocking(sftp_file handle){
|
2005-07-05 05:21:44 +04:00
|
|
|
handle->nonblocking=1;
|
|
|
|
}
|
2009-10-10 22:55:17 +04:00
|
|
|
void sftp_file_set_blocking(sftp_file handle){
|
2005-07-05 05:21:44 +04:00
|
|
|
handle->nonblocking=0;
|
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Read from a file using an opened sftp file handle. */
|
2009-10-10 22:55:17 +04:00
|
|
|
ssize_t sftp_read(sftp_file handle, void *buf, size_t count) {
|
|
|
|
sftp_session sftp = handle->sftp;
|
|
|
|
sftp_message msg = NULL;
|
|
|
|
sftp_status_message status;
|
2009-07-24 23:45:41 +04:00
|
|
|
ssh_string datastring;
|
2015-01-14 13:07:30 +03:00
|
|
|
size_t datalen;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer;
|
2009-04-24 13:05:52 +04:00
|
|
|
int id;
|
2014-04-22 20:46:29 +04:00
|
|
|
int rc;
|
2009-04-24 13:05:52 +04:00
|
|
|
|
|
|
|
if (handle->eof) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-24 13:05:52 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-24 13:05:52 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2014-07-06 02:29:58 +04:00
|
|
|
|
|
|
|
id = sftp_get_new_id(handle->sftp);
|
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_pack(buffer,
|
|
|
|
"dSqd",
|
|
|
|
id,
|
|
|
|
handle->handle,
|
|
|
|
handle->offset,
|
|
|
|
count);
|
|
|
|
if (rc != SSH_OK){
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-10-24 00:19:13 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(handle->sftp, SSH_FXP_READ, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-24 13:05:52 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-24 13:05:52 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (handle->nonblocking) {
|
2010-05-14 02:51:08 +04:00
|
|
|
if (ssh_channel_poll(handle->sftp->channel, 0) == 0) {
|
2009-04-24 13:05:52 +04:00
|
|
|
/* we cannot block */
|
|
|
|
return 0;
|
|
|
|
}
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-24 13:05:52 +04:00
|
|
|
if (sftp_read_and_dispatch(handle->sftp) < 0) {
|
|
|
|
/* something nasty has happened */
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-24 13:05:52 +04:00
|
|
|
msg = sftp_dequeue(handle->sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg->packet_type) {
|
|
|
|
case SSH_FXP_STATUS:
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sftp_set_error(sftp, status->status);
|
|
|
|
switch (status->status) {
|
|
|
|
case SSH_FX_EOF:
|
|
|
|
handle->eof = 1;
|
|
|
|
status_msg_free(status);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ssh_set_error(sftp->session,SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
|
|
|
return -1;
|
|
|
|
case SSH_FXP_DATA:
|
2015-09-17 10:43:33 +03:00
|
|
|
datastring = ssh_buffer_get_ssh_string(msg->payload);
|
2009-04-24 13:05:52 +04:00
|
|
|
sftp_message_free(msg);
|
|
|
|
if (datastring == NULL) {
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received invalid DATA packet from sftp server");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-14 13:07:30 +03:00
|
|
|
datalen = ssh_string_len(datastring);
|
|
|
|
if (datalen > count) {
|
2009-04-24 13:05:52 +04:00
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received a too big DATA packet from sftp server: "
|
2011-04-15 21:02:21 +04:00
|
|
|
"%" PRIdS " and asked for %" PRIdS,
|
2015-01-14 13:07:30 +03:00
|
|
|
datalen, count);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_string_free(datastring);
|
2009-04-24 13:05:52 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-14 13:07:30 +03:00
|
|
|
handle->offset += (uint64_t)datalen;
|
|
|
|
memcpy(buf, ssh_string_data(datastring), datalen);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_string_free(datastring);
|
2015-01-14 13:07:30 +03:00
|
|
|
return datalen;
|
2009-04-24 13:05:52 +04:00
|
|
|
default:
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d during read!", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1; /* not reached */
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Start an asynchronous read from a file using an opened sftp file handle. */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_async_read_begin(sftp_file file, uint32_t len){
|
|
|
|
sftp_session sftp = file->sftp;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t id;
|
2014-04-22 20:46:29 +04:00
|
|
|
int rc;
|
2009-04-24 13:33:47 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-24 13:33:47 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-24 13:33:47 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-06 02:29:58 +04:00
|
|
|
id = sftp_get_new_id(sftp);
|
2014-04-22 20:46:29 +04:00
|
|
|
|
|
|
|
rc = ssh_buffer_pack(buffer,
|
|
|
|
"dSqd",
|
|
|
|
id,
|
|
|
|
file->handle,
|
|
|
|
file->offset,
|
|
|
|
len);
|
|
|
|
if (rc != SSH_OK) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-10-24 00:19:13 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_READ, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-24 13:33:47 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-24 13:33:47 +04:00
|
|
|
|
|
|
|
file->offset += len; /* assume we'll read len bytes */
|
|
|
|
|
|
|
|
return id;
|
2008-11-05 19:42:43 +03:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Wait for an asynchronous read to complete and save the data. */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_async_read(sftp_file file, void *data, uint32_t size, uint32_t id){
|
2012-10-08 00:03:35 +04:00
|
|
|
sftp_session sftp;
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_message msg = NULL;
|
|
|
|
sftp_status_message status;
|
2009-07-24 23:45:41 +04:00
|
|
|
ssh_string datastring;
|
2009-04-24 13:33:47 +04:00
|
|
|
int err = SSH_OK;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t len;
|
2009-04-24 13:33:47 +04:00
|
|
|
|
2012-10-08 00:03:35 +04:00
|
|
|
if (file == NULL) {
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
|
|
|
sftp = file->sftp;
|
|
|
|
|
2009-04-24 13:33:47 +04:00
|
|
|
if (file->eof) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle an existing request */
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (file->nonblocking){
|
2010-05-14 02:51:08 +04:00
|
|
|
if (ssh_channel_poll(sftp->channel, 0) == 0) {
|
2009-04-24 13:33:47 +04:00
|
|
|
/* we cannot block */
|
|
|
|
return SSH_AGAIN;
|
|
|
|
}
|
2008-06-11 22:52:16 +04:00
|
|
|
}
|
2008-11-02 01:28:39 +03:00
|
|
|
|
2009-04-24 13:33:47 +04:00
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
/* something nasty has happened */
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = sftp_dequeue(sftp,id);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg->packet_type) {
|
|
|
|
case SSH_FXP_STATUS:
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sftp_set_error(sftp, status->status);
|
|
|
|
if (status->status != SSH_FX_EOF) {
|
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server : %s", status->errormsg);
|
|
|
|
err = SSH_ERROR;
|
|
|
|
} else {
|
|
|
|
file->eof = 1;
|
|
|
|
}
|
|
|
|
status_msg_free(status);
|
|
|
|
return err;
|
|
|
|
case SSH_FXP_DATA:
|
2015-09-17 10:43:33 +03:00
|
|
|
datastring = ssh_buffer_get_ssh_string(msg->payload);
|
2009-04-24 13:33:47 +04:00
|
|
|
sftp_message_free(msg);
|
|
|
|
if (datastring == NULL) {
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received invalid DATA packet from sftp server");
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
if (ssh_string_len(datastring) > size) {
|
2009-04-24 13:33:47 +04:00
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received a too big DATA packet from sftp server: "
|
2011-04-15 21:02:21 +04:00
|
|
|
"%" PRIdS " and asked for %u",
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_string_len(datastring), size);
|
|
|
|
ssh_string_free(datastring);
|
2009-04-24 13:33:47 +04:00
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
len = ssh_string_len(datastring);
|
2011-08-29 01:00:06 +04:00
|
|
|
/* Update the offset with the correct value */
|
|
|
|
file->offset = file->offset - (size - len);
|
2010-05-14 02:51:08 +04:00
|
|
|
memcpy(data, ssh_string_data(datastring), len);
|
|
|
|
ssh_string_free(datastring);
|
2009-04-24 13:33:47 +04:00
|
|
|
return len;
|
|
|
|
default:
|
|
|
|
ssh_set_error(sftp->session,SSH_FATAL,"Received message %d during read!",msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SSH_ERROR;
|
2008-06-11 22:52:16 +04:00
|
|
|
}
|
2008-11-05 19:42:43 +03:00
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
ssize_t sftp_write(sftp_file file, const void *buf, size_t count) {
|
|
|
|
sftp_session sftp = file->sftp;
|
|
|
|
sftp_message msg = NULL;
|
|
|
|
sftp_status_message status;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t id;
|
2009-04-24 14:08:46 +04:00
|
|
|
int len;
|
2009-06-22 00:30:28 +04:00
|
|
|
int packetlen;
|
2014-04-22 20:46:29 +04:00
|
|
|
int rc;
|
2009-04-24 14:08:46 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-24 14:08:46 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-24 14:08:46 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = sftp_get_new_id(file->sftp);
|
2014-07-06 02:29:58 +04:00
|
|
|
|
|
|
|
rc = ssh_buffer_pack(buffer,
|
|
|
|
"dSqdP",
|
|
|
|
id,
|
|
|
|
file->handle,
|
|
|
|
file->offset,
|
|
|
|
count, /* len of datastring */
|
|
|
|
(size_t)count, buf);
|
2014-04-22 20:46:29 +04:00
|
|
|
if (rc != SSH_OK){
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-24 14:08:46 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2015-09-25 01:34:42 +03:00
|
|
|
packetlen=ssh_buffer_get_len(buffer);
|
2009-04-24 14:08:46 +04:00
|
|
|
len = sftp_packet_write(file->sftp, SSH_FXP_WRITE, buffer);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-24 14:08:46 +04:00
|
|
|
if (len < 0) {
|
|
|
|
return -1;
|
2009-06-22 00:30:28 +04:00
|
|
|
} else if (len != packetlen) {
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_PACKET,
|
2009-04-24 14:08:46 +04:00
|
|
|
"Could not write as much data as expected");
|
|
|
|
}
|
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(file->sftp) < 0) {
|
|
|
|
/* something nasty has happened */
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-24 14:08:46 +04:00
|
|
|
msg = sftp_dequeue(file->sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg->packet_type) {
|
|
|
|
case SSH_FXP_STATUS:
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sftp_set_error(sftp, status->status);
|
|
|
|
switch (status->status) {
|
|
|
|
case SSH_FX_OK:
|
|
|
|
file->offset += count;
|
|
|
|
status_msg_free(status);
|
|
|
|
return count;
|
2005-07-05 05:21:44 +04:00
|
|
|
default:
|
2009-04-24 14:08:46 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
file->offset += count;
|
|
|
|
status_msg_free(status);
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d during write!", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1; /* not reached */
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Seek to a specific location in a file. */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_seek(sftp_file file, uint32_t new_offset) {
|
2009-04-24 14:16:16 +04:00
|
|
|
if (file == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
file->offset = new_offset;
|
2011-05-26 13:22:11 +04:00
|
|
|
file->eof = 0;
|
2009-04-24 14:16:16 +04:00
|
|
|
|
|
|
|
return 0;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_seek64(sftp_file file, uint64_t new_offset) {
|
2009-04-24 14:16:16 +04:00
|
|
|
if (file == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
file->offset = new_offset;
|
2011-05-26 13:22:11 +04:00
|
|
|
file->eof = 0;
|
2009-04-24 14:16:16 +04:00
|
|
|
|
|
|
|
return 0;
|
2008-12-15 00:46:31 +03:00
|
|
|
}
|
2008-12-22 12:51:31 +03:00
|
|
|
|
|
|
|
/* Report current byte position in file. */
|
2009-10-10 22:55:17 +04:00
|
|
|
unsigned long sftp_tell(sftp_file file) {
|
2009-07-25 19:55:58 +04:00
|
|
|
return (unsigned long)file->offset;
|
|
|
|
}
|
|
|
|
/* Report current byte position in file. */
|
2009-10-10 22:55:17 +04:00
|
|
|
uint64_t sftp_tell64(sftp_file file) {
|
2009-07-26 01:19:41 +04:00
|
|
|
return (uint64_t) file->offset;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Rewinds the position of the file pointer to the beginning of the file.*/
|
2009-10-10 22:55:17 +04:00
|
|
|
void sftp_rewind(sftp_file file) {
|
2009-04-24 16:49:59 +04:00
|
|
|
file->offset = 0;
|
2011-05-26 13:22:11 +04:00
|
|
|
file->eof = 0;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* code written by Nick */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_unlink(sftp_session sftp, const char *file) {
|
|
|
|
sftp_status_message status = NULL;
|
|
|
|
sftp_message msg = NULL;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t id;
|
2014-04-22 20:46:29 +04:00
|
|
|
int rc;
|
2005-07-05 05:21:44 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-24 16:57:11 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-24 16:57:11 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-06 02:29:58 +04:00
|
|
|
id = sftp_get_new_id(sftp);
|
2009-04-24 16:57:11 +04:00
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_pack(buffer,
|
|
|
|
"ds",
|
|
|
|
id,
|
|
|
|
file);
|
|
|
|
if (rc != SSH_OK) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2011-04-08 12:59:05 +04:00
|
|
|
return -1;
|
2009-10-24 00:19:13 +04:00
|
|
|
}
|
2014-04-22 20:46:29 +04:00
|
|
|
|
2009-10-24 00:19:13 +04:00
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_REMOVE, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2011-04-08 12:59:05 +04:00
|
|
|
return -1;
|
2009-04-24 16:57:11 +04:00
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-24 16:57:11 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp)) {
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-24 16:57:11 +04:00
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->packet_type == SSH_FXP_STATUS) {
|
|
|
|
/* by specification, this command's only supposed to return SSH_FXP_STATUS */
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-24 16:57:11 +04:00
|
|
|
sftp_set_error(sftp, status->status);
|
|
|
|
switch (status->status) {
|
|
|
|
case SSH_FX_OK:
|
|
|
|
status_msg_free(status);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The status should be SSH_FX_OK if the command was successful, if it
|
|
|
|
* didn't, then there was an error
|
|
|
|
*/
|
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
2005-07-05 05:21:44 +04:00
|
|
|
return -1;
|
2009-04-24 16:57:11 +04:00
|
|
|
} else {
|
|
|
|
ssh_set_error(sftp->session,SSH_FATAL,
|
|
|
|
"Received message %d when attempting to remove file", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* code written by Nick */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_rmdir(sftp_session sftp, const char *directory) {
|
|
|
|
sftp_status_message status = NULL;
|
|
|
|
sftp_message msg = NULL;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t id;
|
2014-04-22 20:46:29 +04:00
|
|
|
int rc;
|
2005-07-05 05:21:44 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-27 14:06:18 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-27 14:06:18 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-06 02:29:58 +04:00
|
|
|
id = sftp_get_new_id(sftp);
|
2009-04-27 14:06:18 +04:00
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_pack(buffer,
|
|
|
|
"ds",
|
|
|
|
id,
|
|
|
|
directory);
|
|
|
|
if (rc != SSH_OK) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-10-24 00:19:13 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_RMDIR, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-27 14:06:18 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-27 14:06:18 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:06:18 +04:00
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* By specification, this command returns SSH_FXP_STATUS */
|
|
|
|
if (msg->packet_type == SSH_FXP_STATUS) {
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:06:18 +04:00
|
|
|
sftp_set_error(sftp, status->status);
|
|
|
|
switch (status->status) {
|
|
|
|
case SSH_FX_OK:
|
|
|
|
status_msg_free(status);
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:06:18 +04:00
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
2005-07-05 05:21:44 +04:00
|
|
|
return -1;
|
2009-04-27 14:06:18 +04:00
|
|
|
} else {
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d when attempting to remove directory",
|
|
|
|
msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Code written by Nick */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_mkdir(sftp_session sftp, const char *directory, mode_t mode) {
|
|
|
|
sftp_status_message status = NULL;
|
|
|
|
sftp_message msg = NULL;
|
|
|
|
sftp_attributes errno_attr = NULL;
|
|
|
|
struct sftp_attributes_struct attr;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer;
|
2009-07-24 23:45:41 +04:00
|
|
|
ssh_string path;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t id;
|
2009-04-27 14:14:59 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-27 14:14:59 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-27 14:14:59 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
path = ssh_string_from_char(directory);
|
2009-04-27 14:14:59 +04:00
|
|
|
if (path == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-27 14:14:59 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZERO_STRUCT(attr);
|
|
|
|
attr.permissions = mode;
|
|
|
|
attr.flags = SSH_FILEXFER_ATTR_PERMISSIONS;
|
|
|
|
|
|
|
|
id = sftp_get_new_id(sftp);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(buffer, path) < 0 ||
|
2009-04-27 14:14:59 +04:00
|
|
|
buffer_add_attributes(buffer, &attr) < 0 ||
|
|
|
|
sftp_packet_write(sftp, SSH_FXP_MKDIR, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(path);
|
2012-07-17 20:11:16 +04:00
|
|
|
return -1;
|
2009-04-27 14:14:59 +04:00
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(path);
|
2009-04-27 14:14:59 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
2009-04-23 15:36:57 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2009-04-27 14:14:59 +04:00
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* By specification, this command only returns SSH_FXP_STATUS */
|
|
|
|
if (msg->packet_type == SSH_FXP_STATUS) {
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:14:59 +04:00
|
|
|
sftp_set_error(sftp, status->status);
|
|
|
|
switch (status->status) {
|
|
|
|
case SSH_FX_FAILURE:
|
|
|
|
/*
|
|
|
|
* mkdir always returns a failure, even if the path already exists.
|
|
|
|
* To be POSIX conform and to be able to map it to EEXIST a stat
|
|
|
|
* call is needed here.
|
|
|
|
*/
|
|
|
|
errno_attr = sftp_lstat(sftp, directory);
|
|
|
|
if (errno_attr != NULL) {
|
|
|
|
SAFE_FREE(errno_attr);
|
|
|
|
sftp_set_error(sftp, SSH_FX_FILE_ALREADY_EXISTS);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:14:59 +04:00
|
|
|
break;
|
|
|
|
case SSH_FX_OK:
|
2005-07-05 05:21:44 +04:00
|
|
|
status_msg_free(status);
|
2009-04-27 14:14:59 +04:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:14:59 +04:00
|
|
|
/*
|
|
|
|
* The status should be SSH_FX_OK if the command was successful, if it
|
|
|
|
* didn't, then there was an error
|
|
|
|
*/
|
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
2005-07-05 05:21:44 +04:00
|
|
|
return -1;
|
2009-04-27 14:14:59 +04:00
|
|
|
} else {
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d when attempting to make directory",
|
|
|
|
msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* code written by nick */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_rename(sftp_session sftp, const char *original, const char *newname) {
|
|
|
|
sftp_status_message status = NULL;
|
|
|
|
sftp_message msg = NULL;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t id;
|
2014-04-22 20:46:29 +04:00
|
|
|
int rc;
|
2005-07-05 05:21:44 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-27 14:20:54 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-27 14:20:54 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-06 02:29:58 +04:00
|
|
|
id = sftp_get_new_id(sftp);
|
2009-04-27 14:20:54 +04:00
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_pack(buffer,
|
|
|
|
"dss",
|
|
|
|
id,
|
|
|
|
original,
|
|
|
|
newname);
|
|
|
|
if (rc != SSH_OK) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-27 14:20:54 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
if (sftp->version >= 4){
|
2010-05-17 22:01:34 +04:00
|
|
|
/* POSIX rename atomically replaces newpath, we should do the same
|
|
|
|
* only available on >=v4 */
|
2015-09-17 10:43:33 +03:00
|
|
|
ssh_buffer_add_u32(buffer, SSH_FXF_RENAME_OVERWRITE);
|
2009-10-24 00:19:13 +04:00
|
|
|
}
|
2014-04-22 20:46:29 +04:00
|
|
|
|
2009-10-24 00:19:13 +04:00
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_RENAME, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-27 14:20:54 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-27 14:20:54 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:20:54 +04:00
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* By specification, this command only returns SSH_FXP_STATUS */
|
|
|
|
if (msg->packet_type == SSH_FXP_STATUS) {
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sftp_set_error(sftp, status->status);
|
|
|
|
switch (status->status) {
|
|
|
|
case SSH_FX_OK:
|
2005-07-05 05:21:44 +04:00
|
|
|
status_msg_free(status);
|
2009-04-27 14:20:54 +04:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:20:54 +04:00
|
|
|
/*
|
|
|
|
* Status should be SSH_FX_OK if the command was successful, if it didn't,
|
|
|
|
* then there was an error
|
|
|
|
*/
|
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
2005-07-05 05:21:44 +04:00
|
|
|
return -1;
|
2009-04-27 14:20:54 +04:00
|
|
|
} else {
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d when attempting to rename",
|
|
|
|
msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Code written by Nick */
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Set file attributes on a file, directory or symbolic link. */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_setstat(sftp_session sftp, const char *file, sftp_attributes attr) {
|
2010-04-28 21:36:39 +04:00
|
|
|
uint32_t id;
|
|
|
|
ssh_buffer buffer;
|
|
|
|
ssh_string path;
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_message msg = NULL;
|
|
|
|
sftp_status_message status = NULL;
|
2009-04-27 14:24:53 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-27 14:24:53 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-27 14:24:53 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
path = ssh_string_from_char(file);
|
2009-04-27 14:24:53 +04:00
|
|
|
if (path == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-27 14:24:53 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = sftp_get_new_id(sftp);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(buffer, path) < 0 ||
|
2009-10-24 00:19:13 +04:00
|
|
|
buffer_add_attributes(buffer, attr) < 0) {
|
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(path);
|
2009-10-24 00:19:13 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_SETSTAT, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(path);
|
2009-04-27 14:24:53 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(path);
|
2009-04-27 14:24:53 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
2009-04-23 15:36:57 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2009-04-27 14:24:53 +04:00
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* By specification, this command only returns SSH_FXP_STATUS */
|
|
|
|
if (msg->packet_type == SSH_FXP_STATUS) {
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:24:53 +04:00
|
|
|
sftp_set_error(sftp, status->status);
|
|
|
|
switch (status->status) {
|
|
|
|
case SSH_FX_OK:
|
2005-07-05 05:21:44 +04:00
|
|
|
status_msg_free(status);
|
2009-04-27 14:24:53 +04:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:24:53 +04:00
|
|
|
/*
|
|
|
|
* The status should be SSH_FX_OK if the command was successful, if it
|
|
|
|
* didn't, then there was an error
|
|
|
|
*/
|
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
2005-07-05 05:21:44 +04:00
|
|
|
return -1;
|
2009-04-27 14:24:53 +04:00
|
|
|
} else {
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d when attempting to set stats", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Change the file owner and group */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_chown(sftp_session sftp, const char *file, uid_t owner, gid_t group) {
|
|
|
|
struct sftp_attributes_struct attr;
|
2008-12-22 12:50:40 +03:00
|
|
|
ZERO_STRUCT(attr);
|
|
|
|
|
|
|
|
attr.uid = owner;
|
|
|
|
attr.gid = group;
|
2010-05-18 01:37:11 +04:00
|
|
|
|
|
|
|
attr.flags = SSH_FILEXFER_ATTR_UIDGID;
|
2008-12-22 12:50:40 +03:00
|
|
|
|
|
|
|
return sftp_setstat(sftp, file, &attr);
|
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Change permissions of a file */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_chmod(sftp_session sftp, const char *file, mode_t mode) {
|
|
|
|
struct sftp_attributes_struct attr;
|
2008-12-22 12:50:40 +03:00
|
|
|
ZERO_STRUCT(attr);
|
|
|
|
attr.permissions = mode;
|
|
|
|
attr.flags = SSH_FILEXFER_ATTR_PERMISSIONS;
|
|
|
|
|
|
|
|
return sftp_setstat(sftp, file, &attr);
|
|
|
|
}
|
|
|
|
|
2008-12-22 12:51:31 +03:00
|
|
|
/* Change the last modification and access time of a file. */
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_utimes(sftp_session sftp, const char *file,
|
2009-04-27 14:25:31 +04:00
|
|
|
const struct timeval *times) {
|
2009-10-10 22:55:17 +04:00
|
|
|
struct sftp_attributes_struct attr;
|
2008-12-22 12:50:40 +03:00
|
|
|
ZERO_STRUCT(attr);
|
|
|
|
|
|
|
|
attr.atime = times[0].tv_sec;
|
|
|
|
attr.atime_nseconds = times[0].tv_usec;
|
|
|
|
|
|
|
|
attr.mtime = times[1].tv_sec;
|
|
|
|
attr.mtime_nseconds = times[1].tv_usec;
|
|
|
|
|
|
|
|
attr.flags |= SSH_FILEXFER_ATTR_ACCESSTIME | SSH_FILEXFER_ATTR_MODIFYTIME |
|
|
|
|
SSH_FILEXFER_ATTR_SUBSECOND_TIMES;
|
|
|
|
|
|
|
|
return sftp_setstat(sftp, file, &attr);
|
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
int sftp_symlink(sftp_session sftp, const char *target, const char *dest) {
|
|
|
|
sftp_status_message status = NULL;
|
|
|
|
sftp_message msg = NULL;
|
2009-07-25 13:35:33 +04:00
|
|
|
ssh_buffer buffer;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t id;
|
2014-04-22 20:46:29 +04:00
|
|
|
int rc;
|
2009-07-25 13:35:33 +04:00
|
|
|
|
2009-10-24 00:19:13 +04:00
|
|
|
if (sftp == NULL)
|
|
|
|
return -1;
|
|
|
|
if (target == NULL || dest == NULL) {
|
2011-09-17 12:28:39 +04:00
|
|
|
ssh_set_error_invalid(sftp->session);
|
2009-07-25 13:35:33 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-07-25 13:35:33 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-07-25 13:35:33 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-06 02:29:58 +04:00
|
|
|
id = sftp_get_new_id(sftp);
|
2009-07-25 13:35:33 +04:00
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
/* TODO check for version number if they ever fix it. */
|
2009-07-28 13:47:53 +04:00
|
|
|
if (ssh_get_openssh_version(sftp->session)) {
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_pack(buffer,
|
|
|
|
"dss",
|
|
|
|
id,
|
|
|
|
target,
|
|
|
|
dest);
|
2009-07-28 13:47:53 +04:00
|
|
|
} else {
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_pack(buffer,
|
|
|
|
"dss",
|
|
|
|
id,
|
|
|
|
dest,
|
|
|
|
target);
|
|
|
|
}
|
|
|
|
if (rc != SSH_OK){
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-07-28 13:47:53 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_SYMLINK, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-07-25 13:35:33 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-07-25 13:35:33 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* By specification, this command only returns SSH_FXP_STATUS */
|
|
|
|
if (msg->packet_type == SSH_FXP_STATUS) {
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sftp_set_error(sftp, status->status);
|
|
|
|
switch (status->status) {
|
|
|
|
case SSH_FX_OK:
|
|
|
|
status_msg_free(status);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* The status should be SSH_FX_OK if the command was successful, if it
|
|
|
|
* didn't, then there was an error
|
|
|
|
*/
|
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d when attempting to set stats", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
char *sftp_readlink(sftp_session sftp, const char *path) {
|
|
|
|
sftp_status_message status = NULL;
|
|
|
|
sftp_message msg = NULL;
|
2009-07-25 13:54:21 +04:00
|
|
|
ssh_string path_s = NULL;
|
|
|
|
ssh_string link_s = NULL;
|
|
|
|
ssh_buffer buffer;
|
2009-08-19 12:52:30 +04:00
|
|
|
char *lnk;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t ignored;
|
|
|
|
uint32_t id;
|
2009-07-25 13:54:21 +04:00
|
|
|
|
2009-10-24 00:19:13 +04:00
|
|
|
if (sftp == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (path == NULL) {
|
2011-09-17 12:28:39 +04:00
|
|
|
ssh_set_error_invalid(sftp);
|
2009-07-25 13:54:21 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-10-23 14:19:22 +04:00
|
|
|
if (sftp->version < 3){
|
|
|
|
ssh_set_error(sftp,SSH_REQUEST_DENIED,"sftp version %d does not support sftp_readlink",sftp->version);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-07-25 13:54:21 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-07-25 13:54:21 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
path_s = ssh_string_from_char(path);
|
2009-07-25 13:54:21 +04:00
|
|
|
if (path_s == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-07-25 13:54:21 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = sftp_get_new_id(sftp);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(buffer, path_s) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(path_s);
|
2009-10-24 00:19:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_READLINK, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(path_s);
|
2009-07-25 13:54:21 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(path_s);
|
2009-07-25 13:54:21 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->packet_type == SSH_FXP_NAME) {
|
|
|
|
/* we don't care about "count" */
|
2015-09-17 10:43:33 +03:00
|
|
|
ssh_buffer_get_u32(msg->payload, &ignored);
|
2009-07-25 13:54:21 +04:00
|
|
|
/* we only care about the file name string */
|
2015-09-17 10:43:33 +03:00
|
|
|
link_s = ssh_buffer_get_ssh_string(msg->payload);
|
2009-07-25 13:54:21 +04:00
|
|
|
sftp_message_free(msg);
|
|
|
|
if (link_s == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
/* TODO: what error to set here? */
|
2009-07-25 13:54:21 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
lnk = ssh_string_to_char(link_s);
|
|
|
|
ssh_string_free(link_s);
|
2009-07-25 13:54:21 +04:00
|
|
|
|
2009-08-19 12:52:30 +04:00
|
|
|
return lnk;
|
2009-07-25 13:54:21 +04:00
|
|
|
} else if (msg->packet_type == SSH_FXP_STATUS) { /* bad response (error) */
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
|
|
|
} else { /* this shouldn't happen */
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d when attempting to set stats", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static sftp_statvfs_t sftp_parse_statvfs(sftp_session sftp, ssh_buffer buf) {
|
2014-04-22 20:46:29 +04:00
|
|
|
sftp_statvfs_t statvfs;
|
|
|
|
int rc;
|
2009-08-12 15:08:11 +04:00
|
|
|
|
2016-05-04 15:56:22 +03:00
|
|
|
statvfs = calloc(1, sizeof(struct sftp_statvfs_struct));
|
2009-08-12 15:08:11 +04:00
|
|
|
if (statvfs == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-08-12 15:08:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-22 20:46:29 +04:00
|
|
|
rc = ssh_buffer_unpack(buf, "qqqqqqqqqqq",
|
|
|
|
&statvfs->f_bsize, /* file system block size */
|
|
|
|
&statvfs->f_frsize, /* fundamental fs block size */
|
|
|
|
&statvfs->f_blocks, /* number of blocks (unit f_frsize) */
|
|
|
|
&statvfs->f_bfree, /* free blocks in file system */
|
|
|
|
&statvfs->f_bavail, /* free blocks for non-root */
|
|
|
|
&statvfs->f_files, /* total file inodes */
|
|
|
|
&statvfs->f_ffree, /* free file inodes */
|
|
|
|
&statvfs->f_favail, /* free file inodes for to non-root */
|
|
|
|
&statvfs->f_fsid, /* file system id */
|
|
|
|
&statvfs->f_flag, /* bit mask of f_flag values */
|
|
|
|
&statvfs->f_namemax/* maximum filename length */
|
|
|
|
);
|
|
|
|
if (rc != SSH_OK) {
|
2009-08-12 15:08:11 +04:00
|
|
|
SAFE_FREE(statvfs);
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL, "Invalid statvfs structure");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return statvfs;
|
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_statvfs_t sftp_statvfs(sftp_session sftp, const char *path) {
|
|
|
|
sftp_status_message status = NULL;
|
|
|
|
sftp_message msg = NULL;
|
2009-08-12 15:08:11 +04:00
|
|
|
ssh_string pathstr;
|
2009-08-19 12:52:30 +04:00
|
|
|
ssh_string ext;
|
2009-08-12 15:08:11 +04:00
|
|
|
ssh_buffer buffer;
|
|
|
|
uint32_t id;
|
|
|
|
|
2009-10-24 00:19:13 +04:00
|
|
|
if (sftp == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (path == NULL) {
|
2011-09-17 12:28:39 +04:00
|
|
|
ssh_set_error_invalid(sftp->session);
|
2009-08-12 15:08:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-10-23 14:19:22 +04:00
|
|
|
if (sftp->version < 3){
|
|
|
|
ssh_set_error(sftp,SSH_REQUEST_DENIED,"sftp version %d does not support sftp_statvfs",sftp->version);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-08 12:59:05 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-08-12 15:08:11 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-08-12 15:08:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
ext = ssh_string_from_char("statvfs@openssh.com");
|
2009-08-19 12:52:30 +04:00
|
|
|
if (ext == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-08-12 15:08:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
pathstr = ssh_string_from_char(path);
|
2009-08-12 15:08:11 +04:00
|
|
|
if (pathstr == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(ext);
|
2009-08-12 15:08:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = sftp_get_new_id(sftp);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(buffer, ext) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(buffer, pathstr) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(ext);
|
|
|
|
ssh_string_free(pathstr);
|
2009-10-24 00:19:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_EXTENDED, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(ext);
|
|
|
|
ssh_string_free(pathstr);
|
2009-08-12 15:08:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(ext);
|
|
|
|
ssh_string_free(pathstr);
|
2009-08-12 15:08:11 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->packet_type == SSH_FXP_EXTENDED_REPLY) {
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_statvfs_t buf = sftp_parse_statvfs(sftp, msg->payload);
|
2009-08-12 15:08:11 +04:00
|
|
|
sftp_message_free(msg);
|
2009-08-19 12:52:30 +04:00
|
|
|
if (buf == NULL) {
|
2009-08-12 15:08:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-19 12:52:30 +04:00
|
|
|
return buf;
|
2009-08-12 15:08:11 +04:00
|
|
|
} else if (msg->packet_type == SSH_FXP_STATUS) { /* bad response (error) */
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
|
|
|
} else { /* this shouldn't happen */
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d when attempting to get statvfs", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-05 19:26:14 +03:00
|
|
|
int sftp_fsync(sftp_file file)
|
|
|
|
{
|
|
|
|
sftp_session sftp;
|
|
|
|
sftp_message msg = NULL;
|
|
|
|
ssh_string ext;
|
|
|
|
ssh_buffer buffer;
|
|
|
|
uint32_t id;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (file == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sftp = file->sftp;
|
|
|
|
|
|
|
|
buffer = ssh_buffer_new();
|
|
|
|
if (buffer == NULL) {
|
|
|
|
ssh_set_error_oom(sftp->session);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ext = ssh_string_from_char("fsync@openssh.com");
|
|
|
|
if (ext == NULL) {
|
|
|
|
ssh_set_error_oom(sftp->session);
|
|
|
|
rc = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = sftp_get_new_id(sftp);
|
|
|
|
rc = ssh_buffer_add_u32(buffer, htonl(id));
|
|
|
|
if (rc < 0) {
|
|
|
|
ssh_set_error_oom(sftp->session);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ssh_buffer_add_ssh_string(buffer, ext);
|
|
|
|
if (rc < 0) {
|
|
|
|
ssh_set_error_oom(sftp->session);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ssh_buffer_add_ssh_string(buffer, file->handle);
|
|
|
|
if (rc < 0) {
|
|
|
|
ssh_set_error_oom(sftp->session);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = sftp_packet_write(sftp, SSH_FXP_EXTENDED, buffer);
|
|
|
|
if (rc < 0) {
|
|
|
|
ssh_set_error_oom(sftp->session);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
rc = sftp_read_and_dispatch(sftp);
|
|
|
|
if (rc < 0) {
|
|
|
|
ssh_set_error_oom(sftp->session);
|
|
|
|
rc = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
} while (msg == NULL);
|
|
|
|
|
|
|
|
/* By specification, this command only returns SSH_FXP_STATUS */
|
|
|
|
if (msg->packet_type == SSH_FXP_STATUS) {
|
|
|
|
sftp_status_message status;
|
|
|
|
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
rc = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
sftp_set_error(sftp, status->status);
|
|
|
|
switch (status->status) {
|
|
|
|
case SSH_FX_OK:
|
|
|
|
/* SUCCESS, LEAVE */
|
|
|
|
status_msg_free(status);
|
|
|
|
rc = 0;
|
|
|
|
goto done;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The status should be SSH_FX_OK if the command was successful, if it
|
|
|
|
* didn't, then there was an error
|
|
|
|
*/
|
|
|
|
ssh_set_error(sftp->session,
|
|
|
|
SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s",
|
|
|
|
status->errormsg);
|
|
|
|
status_msg_free(status);
|
|
|
|
|
|
|
|
rc = -1;
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
ssh_set_error(sftp->session,
|
|
|
|
SSH_FATAL,
|
|
|
|
"Received message %d when attempting to set stats",
|
|
|
|
msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = -1;
|
|
|
|
done:
|
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(ext);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_statvfs_t sftp_fstatvfs(sftp_file file) {
|
|
|
|
sftp_status_message status = NULL;
|
|
|
|
sftp_message msg = NULL;
|
|
|
|
sftp_session sftp;
|
2009-08-19 12:52:30 +04:00
|
|
|
ssh_string ext;
|
2009-08-12 15:08:11 +04:00
|
|
|
ssh_buffer buffer;
|
|
|
|
uint32_t id;
|
|
|
|
|
2009-09-10 20:57:58 +04:00
|
|
|
if (file == NULL) {
|
2009-08-12 15:08:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sftp = file->sftp;
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-08-12 15:08:11 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-08-12 15:08:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
ext = ssh_string_from_char("fstatvfs@openssh.com");
|
2009-08-19 12:52:30 +04:00
|
|
|
if (ext == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-08-12 15:08:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = sftp_get_new_id(sftp);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(buffer, ext) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(buffer, file->handle) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(ext);
|
2009-10-24 00:19:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_EXTENDED, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(ext);
|
2009-08-12 15:08:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(ext);
|
2009-08-12 15:08:11 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->packet_type == SSH_FXP_EXTENDED_REPLY) {
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_statvfs_t buf = sftp_parse_statvfs(sftp, msg->payload);
|
2009-08-12 15:08:11 +04:00
|
|
|
sftp_message_free(msg);
|
2009-08-19 12:52:30 +04:00
|
|
|
if (buf == NULL) {
|
2009-08-12 15:08:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-19 12:52:30 +04:00
|
|
|
return buf;
|
2009-08-12 15:08:11 +04:00
|
|
|
} else if (msg->packet_type == SSH_FXP_STATUS) { /* bad response (error) */
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
|
|
|
} else { /* this shouldn't happen */
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d when attempting to set stats", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
void sftp_statvfs_free(sftp_statvfs_t statvfs) {
|
2009-08-12 15:08:11 +04:00
|
|
|
if (statvfs == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SAFE_FREE(statvfs);
|
|
|
|
}
|
|
|
|
|
2005-07-05 05:21:44 +04:00
|
|
|
/* another code written by Nick */
|
2009-10-10 22:55:17 +04:00
|
|
|
char *sftp_canonicalize_path(sftp_session sftp, const char *path) {
|
|
|
|
sftp_status_message status = NULL;
|
|
|
|
sftp_message msg = NULL;
|
2009-07-24 23:45:41 +04:00
|
|
|
ssh_string name = NULL;
|
|
|
|
ssh_string pathstr;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer;
|
2009-04-27 14:32:13 +04:00
|
|
|
char *cname;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t ignored;
|
|
|
|
uint32_t id;
|
2009-04-27 14:32:13 +04:00
|
|
|
|
2009-10-24 00:19:13 +04:00
|
|
|
if (sftp == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (path == NULL) {
|
2011-09-17 12:28:39 +04:00
|
|
|
ssh_set_error_invalid(sftp->session);
|
2009-07-23 20:34:25 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-27 14:32:13 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-27 14:32:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
pathstr = ssh_string_from_char(path);
|
2009-04-27 14:32:13 +04:00
|
|
|
if (pathstr == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-27 14:32:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = sftp_get_new_id(sftp);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(buffer, pathstr) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(pathstr);
|
2009-10-24 00:19:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(sftp, SSH_FXP_REALPATH, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(pathstr);
|
2009-04-27 14:32:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(pathstr);
|
2009-04-27 14:32:13 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->packet_type == SSH_FXP_NAME) {
|
|
|
|
/* we don't care about "count" */
|
2015-09-17 10:43:33 +03:00
|
|
|
ssh_buffer_get_u32(msg->payload, &ignored);
|
2009-04-27 14:32:13 +04:00
|
|
|
/* we only care about the file name string */
|
2015-09-17 10:43:33 +03:00
|
|
|
name = ssh_buffer_get_ssh_string(msg->payload);
|
2009-04-27 14:32:13 +04:00
|
|
|
sftp_message_free(msg);
|
|
|
|
if (name == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
/* TODO: error message? */
|
2009-04-27 14:32:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
cname = ssh_string_to_char(name);
|
|
|
|
ssh_string_free(name);
|
2009-10-24 00:19:13 +04:00
|
|
|
if (cname == NULL) {
|
|
|
|
ssh_set_error_oom(sftp->session);
|
|
|
|
}
|
2009-04-27 14:32:13 +04:00
|
|
|
return cname;
|
|
|
|
} else if (msg->packet_type == SSH_FXP_STATUS) { /* bad response (error) */
|
|
|
|
status = parse_status_msg(msg);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
if (status == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
|
|
|
} else { /* this shouldn't happen */
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received message %d when attempting to set stats", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
static sftp_attributes sftp_xstat(sftp_session sftp, const char *path,
|
2009-04-27 14:37:54 +04:00
|
|
|
int param) {
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_status_message status = NULL;
|
|
|
|
sftp_message msg = NULL;
|
2009-07-24 23:45:41 +04:00
|
|
|
ssh_string pathstr;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t id;
|
2009-04-27 14:37:54 +04:00
|
|
|
|
2017-11-09 13:41:58 +03:00
|
|
|
if (path == NULL) {
|
|
|
|
ssh_set_error_invalid(sftp->session);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-27 14:37:54 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2009-04-27 14:37:54 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
pathstr = ssh_string_from_char(path);
|
2009-04-27 14:37:54 +04:00
|
|
|
if (pathstr == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-27 14:37:54 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = sftp_get_new_id(sftp);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(buffer, pathstr) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(pathstr);
|
2009-10-24 00:19:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(sftp, param, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(pathstr);
|
2009-04-27 14:37:54 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
|
|
|
ssh_string_free(pathstr);
|
2009-04-27 14:37:54 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:37:54 +04:00
|
|
|
msg = sftp_dequeue(sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->packet_type == SSH_FXP_ATTRS) {
|
2010-12-27 19:52:24 +03:00
|
|
|
sftp_attributes attr = sftp_parse_attr(sftp, msg->payload, 0);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
|
|
|
|
return attr;
|
2009-04-27 14:37:54 +04:00
|
|
|
} else if (msg->packet_type == SSH_FXP_STATUS) {
|
|
|
|
status = parse_status_msg(msg);
|
2005-07-05 05:21:44 +04:00
|
|
|
sftp_message_free(msg);
|
2009-04-27 14:37:54 +04:00
|
|
|
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);
|
2005-07-05 05:21:44 +04:00
|
|
|
return NULL;
|
2009-04-27 14:37:54 +04:00
|
|
|
}
|
|
|
|
ssh_set_error(sftp->session, SSH_FATAL,
|
|
|
|
"Received mesg %d during stat()", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
|
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_attributes sftp_stat(sftp_session session, const char *path) {
|
2009-04-27 14:38:40 +04:00
|
|
|
return sftp_xstat(session, path, SSH_FXP_STAT);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:38:40 +04:00
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_attributes sftp_lstat(sftp_session session, const char *path) {
|
2009-04-27 14:38:40 +04:00
|
|
|
return sftp_xstat(session, path, SSH_FXP_LSTAT);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 22:55:17 +04:00
|
|
|
sftp_attributes sftp_fstat(sftp_file file) {
|
|
|
|
sftp_status_message status = NULL;
|
|
|
|
sftp_message msg = NULL;
|
2009-07-24 23:56:11 +04:00
|
|
|
ssh_buffer buffer;
|
2009-07-26 01:19:41 +04:00
|
|
|
uint32_t id;
|
2009-04-27 14:42:39 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
buffer = ssh_buffer_new();
|
2009-04-27 14:42:39 +04:00
|
|
|
if (buffer == NULL) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(file->sftp->session);
|
2009-04-27 14:42:39 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = sftp_get_new_id(file->sftp);
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
|
|
|
ssh_buffer_add_ssh_string(buffer, file->handle) < 0) {
|
2009-10-24 00:19:13 +04:00
|
|
|
ssh_set_error_oom(file->sftp->session);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-10-24 00:19:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (sftp_packet_write(file->sftp, SSH_FXP_FSTAT, buffer) < 0) {
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-27 14:42:39 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(buffer);
|
2009-04-27 14:42:39 +04:00
|
|
|
|
|
|
|
while (msg == NULL) {
|
|
|
|
if (sftp_read_and_dispatch(file->sftp) < 0) {
|
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-04-27 14:42:39 +04:00
|
|
|
msg = sftp_dequeue(file->sftp, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->packet_type == SSH_FXP_ATTRS){
|
2016-10-07 16:50:19 +03:00
|
|
|
sftp_attributes attr = sftp_parse_attr(file->sftp, msg->payload, 0);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
|
|
|
|
return attr;
|
2009-04-27 14:42:39 +04:00
|
|
|
} else if (msg->packet_type == SSH_FXP_STATUS) {
|
|
|
|
status = parse_status_msg(msg);
|
2005-07-05 05:21:44 +04:00
|
|
|
sftp_message_free(msg);
|
2009-04-27 14:42:39 +04:00
|
|
|
if (status == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ssh_set_error(file->sftp->session, SSH_REQUEST_DENIED,
|
|
|
|
"SFTP server: %s", status->errormsg);
|
|
|
|
status_msg_free(status);
|
|
|
|
|
2005-07-05 05:21:44 +04:00
|
|
|
return NULL;
|
2009-04-27 14:42:39 +04:00
|
|
|
}
|
|
|
|
ssh_set_error(file->sftp->session, SSH_FATAL,
|
|
|
|
"Received msg %d during fstat()", msg->packet_type);
|
|
|
|
sftp_message_free(msg);
|
|
|
|
|
|
|
|
return NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-04-27 14:58:29 +04:00
|
|
|
#endif /* WITH_SFTP */
|