libssh2/src/sftp.c

3426 lines
115 KiB
C
Raw Normal View History

/* Copyright (c) 2004-2008, Sara Golemon <sarag@libssh2.org>
* Copyright (c) 2007 Eli Fant <elifantu@mail.ru>
* Copyright (c) 2009-2014 by Daniel Stenberg
2004-12-22 00:20:02 +00:00
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* Neither the name of the copyright holder nor the names
* of any other contributors may be used to endorse or
* promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
2009-03-27 22:24:09 +00:00
#include <assert.h>
2004-12-22 00:20:02 +00:00
#include "libssh2_priv.h"
#include "libssh2_sftp.h"
#include "channel.h"
#include "session.h"
#include "sftp.h"
2004-12-22 00:20:02 +00:00
/* Note: Version 6 was documented at the time of writing
* However it was marked as "DO NOT IMPLEMENT" due to pending changes
*
* This release of libssh2 implements Version 5 with automatic downgrade
* based on server's declaration
*/
/* SFTP packet types */
#define SSH_FXP_INIT 1
#define SSH_FXP_VERSION 2
#define SSH_FXP_OPEN 3
#define SSH_FXP_CLOSE 4
#define SSH_FXP_READ 5
#define SSH_FXP_WRITE 6
#define SSH_FXP_LSTAT 7
#define SSH_FXP_FSTAT 8
#define SSH_FXP_SETSTAT 9
#define SSH_FXP_FSETSTAT 10
#define SSH_FXP_OPENDIR 11
#define SSH_FXP_READDIR 12
#define SSH_FXP_REMOVE 13
#define SSH_FXP_MKDIR 14
#define SSH_FXP_RMDIR 15
#define SSH_FXP_REALPATH 16
#define SSH_FXP_STAT 17
#define SSH_FXP_RENAME 18
#define SSH_FXP_READLINK 19
#define SSH_FXP_SYMLINK 20
#define SSH_FXP_STATUS 101
#define SSH_FXP_HANDLE 102
#define SSH_FXP_DATA 103
#define SSH_FXP_NAME 104
#define SSH_FXP_ATTRS 105
#define SSH_FXP_EXTENDED 200
#define SSH_FXP_EXTENDED_REPLY 201
/* S_IFREG */
#define LIBSSH2_SFTP_ATTR_PFILETYPE_FILE 0100000
/* S_IFDIR */
#define LIBSSH2_SFTP_ATTR_PFILETYPE_DIR 0040000
#define SSH_FXE_STATVFS_ST_RDONLY 0x00000001
#define SSH_FXE_STATVFS_ST_NOSUID 0x00000002
/* This is the maximum packet length to accept, as larger than this indicate
some kind of server problem. */
#define LIBSSH2_SFTP_PACKET_MAXLEN 80000
static int sftp_packet_ask(LIBSSH2_SFTP *sftp, unsigned char packet_type,
uint32_t request_id, unsigned char **data,
size_t *data_len);
static void sftp_packet_flush(LIBSSH2_SFTP *sftp);
/* sftp_attrsize
* Size that attr with this flagset will occupy when turned into a bin struct
*/
static int sftp_attrsize(unsigned long flags)
{
return (4 + /* flags(4) */
2010-04-28 17:41:17 +01:00
((flags & LIBSSH2_SFTP_ATTR_SIZE) ? 8 : 0) +
((flags & LIBSSH2_SFTP_ATTR_UIDGID) ? 8 : 0) +
((flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) ? 4 : 0) +
((flags & LIBSSH2_SFTP_ATTR_ACMODTIME) ? 8 : 0));
/* atime + mtime as u32 */
}
/* _libssh2_store_u64
*/
static void _libssh2_store_u64(unsigned char **ptr, libssh2_uint64_t value)
{
uint32_t msl = (uint32_t)(value >> 32);
unsigned char *buf = *ptr;
buf[0] = (unsigned char)((msl >> 24) & 0xFF);
buf[1] = (unsigned char)((msl >> 16) & 0xFF);
buf[2] = (unsigned char)((msl >> 8) & 0xFF);
buf[3] = (unsigned char)( msl & 0xFF);
buf[4] = (unsigned char)((value >> 24) & 0xFF);
buf[5] = (unsigned char)((value >> 16) & 0xFF);
buf[6] = (unsigned char)((value >> 8) & 0xFF);
buf[7] = (unsigned char)( value & 0xFF);
*ptr += 8;
}
/*
* Search list of zombied FXP_READ request IDs.
*
* Returns NULL if ID not in list.
*/
static struct sftp_zombie_requests *
find_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id)
{
struct sftp_zombie_requests *zombie =
_libssh2_list_first(&sftp->zombie_requests);
while(zombie) {
if(zombie->request_id == request_id)
break;
else
zombie = _libssh2_list_next(&zombie->node);
}
return zombie;
}
static void
remove_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id)
{
LIBSSH2_SESSION *session = sftp->channel->session;
struct sftp_zombie_requests *zombie = find_zombie_request(sftp,
request_id);
if(zombie) {
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
"Removing request ID %ld from the list of zombie requests",
request_id);
_libssh2_list_remove(&zombie->node);
LIBSSH2_FREE(session, zombie);
}
}
static int
add_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id)
{
LIBSSH2_SESSION *session = sftp->channel->session;
struct sftp_zombie_requests *zombie;
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
"Marking request ID %ld as a zombie request", request_id);
zombie = LIBSSH2_ALLOC(sftp->channel->session,
sizeof(struct sftp_zombie_requests));
if (!zombie)
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"malloc fail for zombie request ID");
else {
zombie->request_id = request_id;
_libssh2_list_add(&sftp->zombie_requests, &zombie->node);
return LIBSSH2_ERROR_NONE;
}
}
/*
* sftp_packet_add
*
2004-12-22 00:20:02 +00:00
* Add a packet to the SFTP packet brigade
*/
static int
sftp_packet_add(LIBSSH2_SFTP *sftp, unsigned char *data,
size_t data_len)
2004-12-22 00:20:02 +00:00
{
LIBSSH2_SESSION *session = sftp->channel->session;
LIBSSH2_SFTP_PACKET *packet;
uint32_t request_id;
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
"Received packet type %d (len %d)",
(int) data[0], data_len);
/*
* Experience shows that if we mess up EAGAIN handling somewhere or
* otherwise get out of sync with the channel, this is where we first get
* a wrong byte and if so we need to bail out at once to aid tracking the
* problem better.
*/
switch(data[0]) {
case SSH_FXP_INIT:
case SSH_FXP_VERSION:
case SSH_FXP_OPEN:
case SSH_FXP_CLOSE:
case SSH_FXP_READ:
case SSH_FXP_WRITE:
case SSH_FXP_LSTAT:
case SSH_FXP_FSTAT:
case SSH_FXP_SETSTAT:
case SSH_FXP_FSETSTAT:
case SSH_FXP_OPENDIR:
case SSH_FXP_READDIR:
case SSH_FXP_REMOVE:
case SSH_FXP_MKDIR:
case SSH_FXP_RMDIR:
case SSH_FXP_REALPATH:
case SSH_FXP_STAT:
case SSH_FXP_RENAME:
case SSH_FXP_READLINK:
case SSH_FXP_SYMLINK:
case SSH_FXP_STATUS:
case SSH_FXP_HANDLE:
case SSH_FXP_DATA:
case SSH_FXP_NAME:
case SSH_FXP_ATTRS:
case SSH_FXP_EXTENDED:
case SSH_FXP_EXTENDED_REPLY:
break;
default:
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Out of sync with the world");
}
request_id = _libssh2_ntohu32(&data[1]);
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Received packet id %d",
request_id);
/* Don't add the packet if it answers a request we've given up on. */
if((data[0] == SSH_FXP_STATUS || data[0] == SSH_FXP_DATA)
&& find_zombie_request(sftp, request_id)) {
/* If we get here, the file ended before the response arrived. We
are no longer interested in the request so we discard it */
LIBSSH2_FREE(session, data);
remove_zombie_request(sftp, request_id);
return LIBSSH2_ERROR_NONE;
}
packet = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP_PACKET));
if (!packet) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate datablock for SFTP packet");
}
packet->data = data;
packet->data_len = data_len;
packet->request_id = request_id;
_libssh2_list_add(&sftp->packets, &packet->node);
return LIBSSH2_ERROR_NONE;
2004-12-22 00:20:02 +00:00
}
/*
* sftp_packet_read
*
2004-12-22 00:20:02 +00:00
* Frame an SFTP packet off the channel
*/
static int
sftp_packet_read(LIBSSH2_SFTP *sftp)
2004-12-22 00:20:02 +00:00
{
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
unsigned char *packet = NULL;
ssize_t rc;
unsigned long recv_window;
int packet_type;
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "recv packet");
switch(sftp->packet_state) {
case libssh2_NB_state_sent: /* EAGAIN from window adjusting */
sftp->packet_state = libssh2_NB_state_idle;
packet = sftp->partial_packet;
goto window_adjust;
case libssh2_NB_state_sent1: /* EAGAIN from channel read */
sftp->packet_state = libssh2_NB_state_idle;
packet = sftp->partial_packet;
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
"partial read cont, len: %lu", sftp->partial_len);
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
"partial read cont, already recvd: %lu",
sftp->partial_received);
/* fall-through */
default:
if(!packet) {
/* only do this if there's not already a packet buffer allocated
to use */
/* each packet starts with a 32 bit length field */
rc = _libssh2_channel_read(channel, 0,
(char *)&sftp->partial_size[
sftp->partial_size_len],
4 - sftp->partial_size_len);
if (rc == LIBSSH2_ERROR_EAGAIN)
return rc;
else if (rc < 0)
return _libssh2_error(session, rc, "channel read");
sftp->partial_size_len += rc;
if(4 != sftp->partial_size_len)
/* we got a short read for the length part */
return LIBSSH2_ERROR_EAGAIN;
sftp->partial_len = _libssh2_ntohu32(sftp->partial_size);
/* make sure we don't proceed if the packet size is unreasonably
large */
if (sftp->partial_len > LIBSSH2_SFTP_PACKET_MAXLEN)
return _libssh2_error(session,
LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED,
"SFTP packet too large");
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
"Data begin - Packet Length: %lu",
sftp->partial_len);
packet = LIBSSH2_ALLOC(session, sftp->partial_len);
if (!packet)
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate SFTP packet");
sftp->partial_size_len = 0;
sftp->partial_received = 0; /* how much of the packet already
received */
sftp->partial_packet = packet;
window_adjust:
recv_window = libssh2_channel_window_read_ex(channel, NULL, NULL);
if(sftp->partial_len > recv_window) {
/* ask for twice the data amount we need at once */
rc = _libssh2_channel_receive_window_adjust(channel,
sftp->partial_len*2,
1, NULL);
/* store the state so that we continue with the correct
operation at next invoke */
sftp->packet_state = (rc == LIBSSH2_ERROR_EAGAIN)?
libssh2_NB_state_sent:
libssh2_NB_state_idle;
if(rc == LIBSSH2_ERROR_EAGAIN)
return rc;
}
}
/* Read as much of the packet as we can */
while (sftp->partial_len > sftp->partial_received) {
rc = _libssh2_channel_read(channel, 0,
(char *)&packet[sftp->partial_received],
sftp->partial_len -
sftp->partial_received);
if (rc == LIBSSH2_ERROR_EAGAIN) {
/*
* We received EAGAIN, save what we have and return EAGAIN to
* the caller. Set 'partial_packet' so that this function
* knows how to continue on the next invoke.
*/
sftp->packet_state = libssh2_NB_state_sent1;
return rc;
}
else if (rc < 0) {
LIBSSH2_FREE(session, packet);
sftp->partial_packet = NULL;
return _libssh2_error(session, rc,
"Error waiting for SFTP packet");
}
sftp->partial_received += rc;
}
sftp->partial_packet = NULL;
/* sftp_packet_add takes ownership of the packet and might free it
so we take a copy of the packet type before we call it. */
packet_type = packet[0];
rc = sftp_packet_add(sftp, packet, sftp->partial_len);
if (rc) {
LIBSSH2_FREE(session, packet);
return rc;
}
else {
return packet_type;
}
}
/* WON'T REACH */
2004-12-22 00:20:02 +00:00
}
/*
* sftp_packetlist_flush
*
* Remove all pending packets in the packet_list and the corresponding one(s)
* in the SFTP packet brigade.
*/
static void sftp_packetlist_flush(LIBSSH2_SFTP_HANDLE *handle)
{
struct sftp_pipeline_chunk *chunk;
LIBSSH2_SFTP *sftp = handle->sftp;
LIBSSH2_SESSION *session = sftp->channel->session;
/* remove pending packets, if any */
chunk = _libssh2_list_first(&handle->packet_list);
while(chunk) {
unsigned char *data;
size_t data_len;
int rc;
struct sftp_pipeline_chunk *next = _libssh2_list_next(&chunk->node);
rc = sftp_packet_ask(sftp, SSH_FXP_STATUS,
chunk->request_id, &data, &data_len);
if(rc)
rc = sftp_packet_ask(sftp, SSH_FXP_DATA,
chunk->request_id, &data, &data_len);
if(!rc)
/* we found a packet, free it */
LIBSSH2_FREE(session, data);
else if(chunk->sent)
/* there was no incoming packet for this request, mark this
request as a zombie if it ever sent the request */
add_zombie_request(sftp, chunk->request_id);
_libssh2_list_remove(&chunk->node);
LIBSSH2_FREE(session, chunk);
chunk = next;
}
}
/*
* sftp_packet_ask()
*
* Checks if there's a matching SFTP packet available.
2004-12-22 00:20:02 +00:00
*/
static int
sftp_packet_ask(LIBSSH2_SFTP *sftp, unsigned char packet_type,
uint32_t request_id, unsigned char **data,
size_t *data_len)
2004-12-22 00:20:02 +00:00
{
LIBSSH2_SESSION *session = sftp->channel->session;
LIBSSH2_SFTP_PACKET *packet = _libssh2_list_first(&sftp->packets);
if(!packet)
return -1;
/* Special consideration when getting VERSION packet */
while (packet) {
if((packet->data[0] == packet_type) &&
((packet_type == SSH_FXP_VERSION) ||
(packet->request_id == request_id))) {
/* Match! Fetch the data */
*data = packet->data;
*data_len = packet->data_len;
/* unlink and free this struct */
_libssh2_list_remove(&packet->node);
LIBSSH2_FREE(session, packet);
return 0;
}
/* check next struct in the list */
packet = _libssh2_list_next(&packet->node);
}
return -1;
2004-12-22 00:20:02 +00:00
}
/* sftp_packet_require
2004-12-22 00:20:02 +00:00
* A la libssh2_packet_require
*/
static int
sftp_packet_require(LIBSSH2_SFTP *sftp, unsigned char packet_type,
uint32_t request_id, unsigned char **data,
size_t *data_len)
2004-12-22 00:20:02 +00:00
{
LIBSSH2_SESSION *session = sftp->channel->session;
int rc;
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Requiring packet %d id %ld",
(int) packet_type, request_id);
if (sftp_packet_ask(sftp, packet_type, request_id, data, data_len) == 0) {
/* The right packet was available in the packet brigade */
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Got %d",
(int) packet_type);
return LIBSSH2_ERROR_NONE;
}
while (session->socket_state == LIBSSH2_SOCKET_CONNECTED) {
rc = sftp_packet_read(sftp);
if (rc < 0)
return rc;
/* data was read, check the queue again */
if (!sftp_packet_ask(sftp, packet_type, request_id, data, data_len)) {
/* The right packet was available in the packet brigade */
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Got %d",
(int) packet_type);
return LIBSSH2_ERROR_NONE;
}
}
/* Only reached if the socket died */
2009-08-25 00:23:34 +02:00
return LIBSSH2_ERROR_SOCKET_DISCONNECT;
2004-12-22 00:20:02 +00:00
}
/* sftp_packet_requirev
2014-02-18 23:46:25 +01:00
* Require one of N possible responses
2004-12-22 00:20:02 +00:00
*/
static int
sftp_packet_requirev(LIBSSH2_SFTP *sftp, int num_valid_responses,
const unsigned char *valid_responses,
uint32_t request_id, unsigned char **data,
size_t *data_len)
2004-12-22 00:20:02 +00:00
{
int i;
int rc;
/* If no timeout is active, start a new one */
if (sftp->requirev_start == 0)
sftp->requirev_start = time(NULL);
while (sftp->channel->session->socket_state == LIBSSH2_SOCKET_CONNECTED) {
for(i = 0; i < num_valid_responses; i++) {
if (sftp_packet_ask(sftp, valid_responses[i], request_id,
data, data_len) == 0) {
/*
* Set to zero before all returns to say
* the timeout is not active
*/
sftp->requirev_start = 0;
return LIBSSH2_ERROR_NONE;
}
}
rc = sftp_packet_read(sftp);
if ((rc < 0) && (rc != LIBSSH2_ERROR_EAGAIN)) {
sftp->requirev_start = 0;
return rc;
} else if (rc <= 0) {
/* prevent busy-looping */
long left =
LIBSSH2_READ_TIMEOUT - (long)(time(NULL) - sftp->requirev_start);
if (left <= 0) {
sftp->requirev_start = 0;
return LIBSSH2_ERROR_TIMEOUT;
}
else if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc;
}
}
}
sftp->requirev_start = 0;
/* Only reached if the socket died */
return LIBSSH2_ERROR_SOCKET_DISCONNECT;
2004-12-22 00:20:02 +00:00
}
/* sftp_attr2bin
2004-12-22 00:20:02 +00:00
* Populate attributes into an SFTP block
*/