2001-02-23 21:21:44 +03:00
|
|
|
/*
|
|
|
|
* sftp.c: SFTP generic client code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-02-24 19:08:56 +03:00
|
|
|
#include <string.h>
|
2001-02-23 21:21:44 +03:00
|
|
|
#include <assert.h>
|
2003-09-27 21:52:34 +04:00
|
|
|
#include <limits.h>
|
2001-02-23 21:21:44 +03:00
|
|
|
|
2001-11-14 15:58:42 +03:00
|
|
|
#include "misc.h"
|
2001-02-23 21:21:44 +03:00
|
|
|
#include "int64.h"
|
2003-06-29 18:26:09 +04:00
|
|
|
#include "tree234.h"
|
2001-02-23 21:21:44 +03:00
|
|
|
#include "sftp.h"
|
|
|
|
|
|
|
|
struct sftp_packet {
|
|
|
|
char *data;
|
2005-02-20 13:30:05 +03:00
|
|
|
unsigned length, maxlen;
|
|
|
|
unsigned savedpos;
|
2001-02-23 21:21:44 +03:00
|
|
|
int type;
|
|
|
|
};
|
|
|
|
|
2001-08-04 18:19:51 +04:00
|
|
|
static const char *fxp_error_message;
|
|
|
|
static int fxp_errtype;
|
|
|
|
|
2015-05-15 13:15:42 +03:00
|
|
|
static void fxp_internal_error(const char *msg);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
2001-02-23 21:21:44 +03:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
* SFTP packet construction functions.
|
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
static void sftp_pkt_ensure(struct sftp_packet *pkt, int length)
|
|
|
|
{
|
2007-01-09 21:24:07 +03:00
|
|
|
if ((int)pkt->maxlen < length) {
|
2001-05-06 18:35:20 +04:00
|
|
|
pkt->maxlen = length + 256;
|
2003-03-29 19:14:26 +03:00
|
|
|
pkt->data = sresize(pkt->data, pkt->maxlen, char);
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
|
|
|
}
|
2015-05-15 13:15:42 +03:00
|
|
|
static void sftp_pkt_adddata(struct sftp_packet *pkt,
|
|
|
|
const void *data, int len)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
pkt->length += len;
|
|
|
|
sftp_pkt_ensure(pkt, pkt->length);
|
2001-05-06 18:35:20 +04:00
|
|
|
memcpy(pkt->data + pkt->length - len, data, len);
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
2001-05-06 18:35:20 +04:00
|
|
|
static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte)
|
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_adddata(pkt, &byte, 1);
|
|
|
|
}
|
2012-08-13 00:17:13 +04:00
|
|
|
static void sftp_pkt_adduint32(struct sftp_packet *pkt,
|
|
|
|
unsigned long value)
|
|
|
|
{
|
|
|
|
unsigned char x[4];
|
|
|
|
PUT_32BIT(x, value);
|
|
|
|
sftp_pkt_adddata(pkt, x, 4);
|
|
|
|
}
|
2001-05-06 18:35:20 +04:00
|
|
|
static struct sftp_packet *sftp_pkt_init(int pkt_type)
|
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
struct sftp_packet *pkt;
|
2003-03-29 19:14:26 +03:00
|
|
|
pkt = snew(struct sftp_packet);
|
2001-02-23 21:21:44 +03:00
|
|
|
pkt->data = NULL;
|
|
|
|
pkt->savedpos = -1;
|
|
|
|
pkt->length = 0;
|
|
|
|
pkt->maxlen = 0;
|
2012-08-13 00:17:13 +04:00
|
|
|
sftp_pkt_adduint32(pkt, 0); /* length field will be filled in later */
|
2001-05-06 18:35:20 +04:00
|
|
|
sftp_pkt_addbyte(pkt, (unsigned char) pkt_type);
|
2001-02-23 21:21:44 +03:00
|
|
|
return pkt;
|
|
|
|
}
|
2003-08-24 17:22:17 +04:00
|
|
|
/*
|
2001-05-06 18:35:20 +04:00
|
|
|
static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
|
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_adddata(pkt, &value, 1);
|
|
|
|
}
|
2003-08-24 17:22:17 +04:00
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value)
|
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
unsigned char x[8];
|
|
|
|
PUT_32BIT(x, value.hi);
|
2001-05-06 18:35:20 +04:00
|
|
|
PUT_32BIT(x + 4, value.lo);
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_adddata(pkt, x, 8);
|
|
|
|
}
|
2001-05-06 18:35:20 +04:00
|
|
|
static void sftp_pkt_addstring_start(struct sftp_packet *pkt)
|
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_adduint32(pkt, 0);
|
|
|
|
pkt->savedpos = pkt->length;
|
|
|
|
}
|
2015-05-15 13:15:42 +03:00
|
|
|
static void sftp_pkt_addstring_str(struct sftp_packet *pkt, const char *data)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_adddata(pkt, data, strlen(data));
|
2001-05-06 18:35:20 +04:00
|
|
|
PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
|
|
|
static void sftp_pkt_addstring_data(struct sftp_packet *pkt,
|
2015-05-15 13:15:42 +03:00
|
|
|
const char *data, int len)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_adddata(pkt, data, len);
|
2001-05-06 18:35:20 +04:00
|
|
|
PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
2015-05-15 13:15:42 +03:00
|
|
|
static void sftp_pkt_addstring(struct sftp_packet *pkt, const char *data)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_addstring_start(pkt);
|
|
|
|
sftp_pkt_addstring_str(pkt, data);
|
|
|
|
}
|
2001-08-26 15:35:11 +04:00
|
|
|
static void sftp_pkt_addattrs(struct sftp_packet *pkt, struct fxp_attrs attrs)
|
|
|
|
{
|
|
|
|
sftp_pkt_adduint32(pkt, attrs.flags);
|
|
|
|
if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) {
|
|
|
|
sftp_pkt_adduint32(pkt, attrs.size.hi);
|
|
|
|
sftp_pkt_adduint32(pkt, attrs.size.lo);
|
|
|
|
}
|
|
|
|
if (attrs.flags & SSH_FILEXFER_ATTR_UIDGID) {
|
|
|
|
sftp_pkt_adduint32(pkt, attrs.uid);
|
|
|
|
sftp_pkt_adduint32(pkt, attrs.gid);
|
|
|
|
}
|
|
|
|
if (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
|
|
|
|
sftp_pkt_adduint32(pkt, attrs.permissions);
|
|
|
|
}
|
|
|
|
if (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
|
|
|
|
sftp_pkt_adduint32(pkt, attrs.atime);
|
|
|
|
sftp_pkt_adduint32(pkt, attrs.mtime);
|
|
|
|
}
|
|
|
|
if (attrs.flags & SSH_FILEXFER_ATTR_EXTENDED) {
|
|
|
|
/*
|
|
|
|
* We currently don't support sending any extended
|
|
|
|
* attributes.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
2001-02-23 21:21:44 +03:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
* SFTP packet decode functions.
|
|
|
|
*/
|
|
|
|
|
2005-02-20 13:30:05 +03:00
|
|
|
static int sftp_pkt_getbyte(struct sftp_packet *pkt, unsigned char *ret)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
if (pkt->length - pkt->savedpos < 1)
|
2005-02-20 13:30:05 +03:00
|
|
|
return 0;
|
|
|
|
*ret = (unsigned char) pkt->data[pkt->savedpos];
|
2001-02-23 21:21:44 +03:00
|
|
|
pkt->savedpos++;
|
2005-02-20 13:30:05 +03:00
|
|
|
return 1;
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
2005-02-20 13:30:05 +03:00
|
|
|
static int sftp_pkt_getuint32(struct sftp_packet *pkt, unsigned long *ret)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
if (pkt->length - pkt->savedpos < 4)
|
2005-02-20 13:30:05 +03:00
|
|
|
return 0;
|
|
|
|
*ret = GET_32BIT(pkt->data + pkt->savedpos);
|
2001-02-23 21:21:44 +03:00
|
|
|
pkt->savedpos += 4;
|
2005-02-20 13:30:05 +03:00
|
|
|
return 1;
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
2005-02-20 13:30:05 +03:00
|
|
|
static int sftp_pkt_getstring(struct sftp_packet *pkt,
|
|
|
|
char **p, int *length)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
*p = NULL;
|
|
|
|
if (pkt->length - pkt->savedpos < 4)
|
2005-02-20 13:30:05 +03:00
|
|
|
return 0;
|
2013-07-14 14:45:54 +04:00
|
|
|
*length = toint(GET_32BIT(pkt->data + pkt->savedpos));
|
2001-02-23 21:21:44 +03:00
|
|
|
pkt->savedpos += 4;
|
2007-01-09 21:24:07 +03:00
|
|
|
if ((int)(pkt->length - pkt->savedpos) < *length || *length < 0) {
|
2005-02-20 13:30:05 +03:00
|
|
|
*length = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-05-06 18:35:20 +04:00
|
|
|
*p = pkt->data + pkt->savedpos;
|
2001-02-23 21:21:44 +03:00
|
|
|
pkt->savedpos += *length;
|
2005-02-20 13:30:05 +03:00
|
|
|
return 1;
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
2005-02-20 13:30:05 +03:00
|
|
|
static int sftp_pkt_getattrs(struct sftp_packet *pkt, struct fxp_attrs *ret)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getuint32(pkt, &ret->flags))
|
|
|
|
return 0;
|
|
|
|
if (ret->flags & SSH_FILEXFER_ATTR_SIZE) {
|
2001-02-23 21:21:44 +03:00
|
|
|
unsigned long hi, lo;
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getuint32(pkt, &hi) ||
|
|
|
|
!sftp_pkt_getuint32(pkt, &lo))
|
|
|
|
return 0;
|
|
|
|
ret->size = uint64_make(hi, lo);
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
2005-02-20 13:30:05 +03:00
|
|
|
if (ret->flags & SSH_FILEXFER_ATTR_UIDGID) {
|
|
|
|
if (!sftp_pkt_getuint32(pkt, &ret->uid) ||
|
|
|
|
!sftp_pkt_getuint32(pkt, &ret->gid))
|
|
|
|
return 0;
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
2005-02-20 13:30:05 +03:00
|
|
|
if (ret->flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
|
|
|
|
if (!sftp_pkt_getuint32(pkt, &ret->permissions))
|
|
|
|
return 0;
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
2005-02-20 13:30:05 +03:00
|
|
|
if (ret->flags & SSH_FILEXFER_ATTR_ACMODTIME) {
|
|
|
|
if (!sftp_pkt_getuint32(pkt, &ret->atime) ||
|
|
|
|
!sftp_pkt_getuint32(pkt, &ret->mtime))
|
|
|
|
return 0;
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
2005-02-20 13:30:05 +03:00
|
|
|
if (ret->flags & SSH_FILEXFER_ATTR_EXTENDED) {
|
|
|
|
unsigned long count;
|
|
|
|
if (!sftp_pkt_getuint32(pkt, &count))
|
|
|
|
return 0;
|
2001-02-23 21:21:44 +03:00
|
|
|
while (count--) {
|
|
|
|
char *str;
|
|
|
|
int len;
|
|
|
|
/*
|
|
|
|
* We should try to analyse these, if we ever find one
|
|
|
|
* we recognise.
|
|
|
|
*/
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getstring(pkt, &str, &len) ||
|
|
|
|
!sftp_pkt_getstring(pkt, &str, &len))
|
|
|
|
return 0;
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
|
|
|
}
|
2005-02-20 13:30:05 +03:00
|
|
|
return 1;
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
2001-05-06 18:35:20 +04:00
|
|
|
static void sftp_pkt_free(struct sftp_packet *pkt)
|
|
|
|
{
|
|
|
|
if (pkt->data)
|
|
|
|
sfree(pkt->data);
|
2001-02-23 21:21:44 +03:00
|
|
|
sfree(pkt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
2001-02-24 19:08:56 +03:00
|
|
|
* Send and receive packet functions.
|
2001-02-23 21:21:44 +03:00
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
int sftp_send(struct sftp_packet *pkt)
|
|
|
|
{
|
2001-02-24 19:08:56 +03:00
|
|
|
int ret;
|
2012-08-13 00:17:13 +04:00
|
|
|
PUT_32BIT(pkt->data, pkt->length - 4);
|
|
|
|
ret = sftp_senddata(pkt->data, pkt->length);
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_free(pkt);
|
2001-02-24 19:08:56 +03:00
|
|
|
return ret;
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
2001-05-06 18:35:20 +04:00
|
|
|
struct sftp_packet *sftp_recv(void)
|
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
struct sftp_packet *pkt;
|
|
|
|
char x[4];
|
2005-02-20 13:30:05 +03:00
|
|
|
unsigned char uc;
|
2001-02-23 21:21:44 +03:00
|
|
|
|
2001-02-24 19:08:56 +03:00
|
|
|
if (!sftp_recvdata(x, 4))
|
|
|
|
return NULL;
|
2001-02-23 21:21:44 +03:00
|
|
|
|
2003-03-29 19:14:26 +03:00
|
|
|
pkt = snew(struct sftp_packet);
|
2001-02-23 21:21:44 +03:00
|
|
|
pkt->savedpos = 0;
|
|
|
|
pkt->length = pkt->maxlen = GET_32BIT(x);
|
2003-03-29 19:14:26 +03:00
|
|
|
pkt->data = snewn(pkt->length, char);
|
2001-02-23 21:21:44 +03:00
|
|
|
|
2001-02-24 19:08:56 +03:00
|
|
|
if (!sftp_recvdata(pkt->data, pkt->length)) {
|
|
|
|
sftp_pkt_free(pkt);
|
|
|
|
return NULL;
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
|
|
|
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getbyte(pkt, &uc)) {
|
|
|
|
sftp_pkt_free(pkt);
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
pkt->type = uc;
|
|
|
|
}
|
2001-02-23 21:21:44 +03:00
|
|
|
|
|
|
|
return pkt;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:26:09 +04:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
* Request ID allocation and temporary dispatch routines.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define REQUEST_ID_OFFSET 256
|
|
|
|
|
|
|
|
struct sftp_request {
|
|
|
|
unsigned id;
|
|
|
|
int registered;
|
2003-09-27 21:52:34 +04:00
|
|
|
void *userdata;
|
2003-06-29 18:26:09 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static int sftp_reqcmp(void *av, void *bv)
|
|
|
|
{
|
|
|
|
struct sftp_request *a = (struct sftp_request *)av;
|
|
|
|
struct sftp_request *b = (struct sftp_request *)bv;
|
|
|
|
if (a->id < b->id)
|
|
|
|
return -1;
|
|
|
|
if (a->id > b->id)
|
|
|
|
return +1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int sftp_reqfind(void *av, void *bv)
|
|
|
|
{
|
|
|
|
unsigned *a = (unsigned *) av;
|
|
|
|
struct sftp_request *b = (struct sftp_request *)bv;
|
|
|
|
if (*a < b->id)
|
|
|
|
return -1;
|
|
|
|
if (*a > b->id)
|
|
|
|
return +1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static tree234 *sftp_requests;
|
|
|
|
|
|
|
|
static struct sftp_request *sftp_alloc_request(void)
|
|
|
|
{
|
|
|
|
unsigned low, high, mid;
|
|
|
|
int tsize;
|
|
|
|
struct sftp_request *r;
|
|
|
|
|
|
|
|
if (sftp_requests == NULL)
|
|
|
|
sftp_requests = newtree234(sftp_reqcmp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First-fit allocation of request IDs: always pick the lowest
|
|
|
|
* unused one. To do this, binary-search using the counted
|
|
|
|
* B-tree to find the largest ID which is in a contiguous
|
|
|
|
* sequence from the beginning. (Precisely everything in that
|
|
|
|
* sequence must have ID equal to its tree index plus
|
2003-08-24 17:22:17 +04:00
|
|
|
* REQUEST_ID_OFFSET.)
|
2003-06-29 18:26:09 +04:00
|
|
|
*/
|
|
|
|
tsize = count234(sftp_requests);
|
|
|
|
|
|
|
|
low = -1;
|
|
|
|
high = tsize;
|
|
|
|
while (high - low > 1) {
|
|
|
|
mid = (high + low) / 2;
|
|
|
|
r = index234(sftp_requests, mid);
|
|
|
|
if (r->id == mid + REQUEST_ID_OFFSET)
|
|
|
|
low = mid; /* this one is fine */
|
|
|
|
else
|
|
|
|
high = mid; /* this one is past it */
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now low points to either -1, or the tree index of the
|
|
|
|
* largest ID in the initial sequence.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
unsigned i = low + 1 + REQUEST_ID_OFFSET;
|
|
|
|
assert(NULL == find234(sftp_requests, &i, sftp_reqfind));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* So the request ID we need to create is
|
|
|
|
* low + 1 + REQUEST_ID_OFFSET.
|
|
|
|
*/
|
|
|
|
r = snew(struct sftp_request);
|
|
|
|
r->id = low + 1 + REQUEST_ID_OFFSET;
|
|
|
|
r->registered = 0;
|
2003-09-27 21:52:34 +04:00
|
|
|
r->userdata = NULL;
|
2003-06-29 18:26:09 +04:00
|
|
|
add234(sftp_requests, r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2003-12-19 15:44:46 +03:00
|
|
|
void sftp_cleanup_request(void)
|
|
|
|
{
|
2005-02-25 12:59:24 +03:00
|
|
|
if (sftp_requests != NULL) {
|
2003-12-19 15:44:46 +03:00
|
|
|
freetree234(sftp_requests);
|
|
|
|
sftp_requests = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:26:09 +04:00
|
|
|
void sftp_register(struct sftp_request *req)
|
|
|
|
{
|
|
|
|
req->registered = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sftp_request *sftp_find_request(struct sftp_packet *pktin)
|
|
|
|
{
|
|
|
|
unsigned long id;
|
2016-11-19 02:34:46 +03:00
|
|
|
unsigned fid;
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req;
|
|
|
|
|
|
|
|
if (!pktin) {
|
|
|
|
fxp_internal_error("did not receive a valid SFTP packet\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getuint32(pktin, &id)) {
|
|
|
|
fxp_internal_error("did not receive a valid SFTP packet\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-11-19 02:34:46 +03:00
|
|
|
fid = (unsigned)id;
|
|
|
|
req = find234(sftp_requests, &fid, sftp_reqfind);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
if (!req || !req->registered) {
|
|
|
|
fxp_internal_error("request ID mismatch\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
del234(sftp_requests, req);
|
|
|
|
|
2003-06-29 18:26:09 +04:00
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2001-02-23 21:21:44 +03:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
* String handling routines.
|
|
|
|
*/
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
static char *mkstr(char *s, int len)
|
|
|
|
{
|
2003-03-29 19:14:26 +03:00
|
|
|
char *p = snewn(len + 1, char);
|
2001-02-23 21:21:44 +03:00
|
|
|
memcpy(p, s, len);
|
|
|
|
p[len] = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
* SFTP primitives.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deal with (and free) an FXP_STATUS packet. Return 1 if
|
|
|
|
* SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
|
|
|
|
* Also place the status into fxp_errtype.
|
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
static int fxp_got_status(struct sftp_packet *pktin)
|
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
static const char *const messages[] = {
|
|
|
|
/* SSH_FX_OK. The only time we will display a _message_ for this
|
|
|
|
* is if we were expecting something other than FXP_STATUS on
|
|
|
|
* success, so this is actually an error message! */
|
|
|
|
"unexpected OK response",
|
|
|
|
"end of file",
|
|
|
|
"no such file or directory",
|
|
|
|
"permission denied",
|
|
|
|
"failure",
|
|
|
|
"bad message",
|
|
|
|
"no connection",
|
|
|
|
"connection lost",
|
|
|
|
"operation unsupported",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (pktin->type != SSH_FXP_STATUS) {
|
|
|
|
fxp_error_message = "expected FXP_STATUS packet";
|
|
|
|
fxp_errtype = -1;
|
|
|
|
} else {
|
2005-02-20 13:30:05 +03:00
|
|
|
unsigned long ul;
|
|
|
|
if (!sftp_pkt_getuint32(pktin, &ul)) {
|
|
|
|
fxp_error_message = "malformed FXP_STATUS packet";
|
|
|
|
fxp_errtype = -1;
|
|
|
|
} else {
|
|
|
|
fxp_errtype = ul;
|
|
|
|
if (fxp_errtype < 0 ||
|
|
|
|
fxp_errtype >= sizeof(messages) / sizeof(*messages))
|
2001-05-06 18:35:20 +04:00
|
|
|
fxp_error_message = "unknown error code";
|
2005-02-20 13:30:05 +03:00
|
|
|
else
|
|
|
|
fxp_error_message = messages[fxp_errtype];
|
|
|
|
}
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fxp_errtype == SSH_FX_OK)
|
|
|
|
return 1;
|
|
|
|
else if (fxp_errtype == SSH_FX_EOF)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-15 13:15:42 +03:00
|
|
|
static void fxp_internal_error(const char *msg)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
fxp_error_message = msg;
|
|
|
|
fxp_errtype = -1;
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
const char *fxp_error(void)
|
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
return fxp_error_message;
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
int fxp_error_type(void)
|
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
return fxp_errtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform exchange of init/version packets. Return 0 on failure.
|
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
int fxp_init(void)
|
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
struct sftp_packet *pktout, *pktin;
|
2005-02-20 13:30:05 +03:00
|
|
|
unsigned long remotever;
|
2001-02-23 21:21:44 +03:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_INIT);
|
|
|
|
sftp_pkt_adduint32(pktout, SFTP_PROTO_VERSION);
|
|
|
|
sftp_send(pktout);
|
|
|
|
|
|
|
|
pktin = sftp_recv();
|
2001-02-27 12:11:42 +03:00
|
|
|
if (!pktin) {
|
|
|
|
fxp_internal_error("could not connect");
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-23 21:21:44 +03:00
|
|
|
if (pktin->type != SSH_FXP_VERSION) {
|
|
|
|
fxp_internal_error("did not receive FXP_VERSION");
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getuint32(pktin, &remotever)) {
|
|
|
|
fxp_internal_error("malformed FXP_VERSION packet");
|
|
|
|
sftp_pkt_free(pktin);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-23 21:21:44 +03:00
|
|
|
if (remotever > SFTP_PROTO_VERSION) {
|
2001-05-06 18:35:20 +04:00
|
|
|
fxp_internal_error
|
|
|
|
("remote protocol is more advanced than we support");
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* In principle, this packet might also contain extension-
|
|
|
|
* string pairs. We should work through them and look for any
|
|
|
|
* we recognise. In practice we don't currently do so because
|
|
|
|
* we know we don't recognise _any_.
|
|
|
|
*/
|
|
|
|
sftp_pkt_free(pktin);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-02-24 15:02:35 +03:00
|
|
|
* Canonify a pathname.
|
2001-02-23 21:21:44 +03:00
|
|
|
*/
|
2015-05-15 13:15:42 +03:00
|
|
|
struct sftp_request *fxp_realpath_send(const char *path)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-02-23 21:21:44 +03:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_REALPATH);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_addstring_start(pktout);
|
|
|
|
sftp_pkt_addstring_str(pktout, path);
|
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
char *fxp_realpath_recv(struct sftp_packet *pktin, struct sftp_request *req)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
sfree(req);
|
|
|
|
|
2001-02-23 21:21:44 +03:00
|
|
|
if (pktin->type == SSH_FXP_NAME) {
|
2005-02-20 13:30:05 +03:00
|
|
|
unsigned long count;
|
2001-02-23 21:21:44 +03:00
|
|
|
char *path;
|
|
|
|
int len;
|
|
|
|
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getuint32(pktin, &count) || count != 1) {
|
|
|
|
fxp_internal_error("REALPATH did not return name count of 1\n");
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getstring(pktin, &path, &len)) {
|
2001-02-23 21:21:44 +03:00
|
|
|
fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
path = mkstr(path, len);
|
|
|
|
sftp_pkt_free(pktin);
|
|
|
|
return path;
|
|
|
|
} else {
|
|
|
|
fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a file.
|
|
|
|
*/
|
2015-05-15 13:15:42 +03:00
|
|
|
struct sftp_request *fxp_open_send(const char *path, int type,
|
2011-08-11 21:59:30 +04:00
|
|
|
struct fxp_attrs *attrs)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-02-23 21:21:44 +03:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_OPEN);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_addstring(pktout, path);
|
|
|
|
sftp_pkt_adduint32(pktout, type);
|
2011-08-11 21:59:30 +04:00
|
|
|
if (attrs)
|
|
|
|
sftp_pkt_addattrs(pktout, *attrs);
|
|
|
|
else
|
|
|
|
sftp_pkt_adduint32(pktout, 0); /* empty ATTRS structure */
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
struct fxp_handle *fxp_open_recv(struct sftp_packet *pktin,
|
|
|
|
struct sftp_request *req)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
sfree(req);
|
|
|
|
|
2001-02-23 21:21:44 +03:00
|
|
|
if (pktin->type == SSH_FXP_HANDLE) {
|
|
|
|
char *hstring;
|
|
|
|
struct fxp_handle *handle;
|
|
|
|
int len;
|
|
|
|
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getstring(pktin, &hstring, &len)) {
|
2001-02-23 21:21:44 +03:00
|
|
|
fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-03-29 19:14:26 +03:00
|
|
|
handle = snew(struct fxp_handle);
|
2001-02-23 21:21:44 +03:00
|
|
|
handle->hstring = mkstr(hstring, len);
|
2001-02-24 15:02:35 +03:00
|
|
|
handle->hlen = len;
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_free(pktin);
|
|
|
|
return handle;
|
|
|
|
} else {
|
|
|
|
fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a directory.
|
|
|
|
*/
|
2015-05-15 13:15:42 +03:00
|
|
|
struct sftp_request *fxp_opendir_send(const char *path)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-02-23 21:21:44 +03:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_OPENDIR);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_addstring(pktout, path);
|
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
struct fxp_handle *fxp_opendir_recv(struct sftp_packet *pktin,
|
|
|
|
struct sftp_request *req)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
sfree(req);
|
2001-02-23 21:21:44 +03:00
|
|
|
if (pktin->type == SSH_FXP_HANDLE) {
|
|
|
|
char *hstring;
|
|
|
|
struct fxp_handle *handle;
|
|
|
|
int len;
|
|
|
|
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getstring(pktin, &hstring, &len)) {
|
2001-02-23 21:21:44 +03:00
|
|
|
fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-03-29 19:14:26 +03:00
|
|
|
handle = snew(struct fxp_handle);
|
2001-02-23 21:21:44 +03:00
|
|
|
handle->hstring = mkstr(hstring, len);
|
2001-02-24 15:02:35 +03:00
|
|
|
handle->hlen = len;
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_free(pktin);
|
|
|
|
return handle;
|
|
|
|
} else {
|
|
|
|
fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close a file/dir.
|
|
|
|
*/
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *fxp_close_send(struct fxp_handle *handle)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-02-23 21:21:44 +03:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_CLOSE);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-02-24 15:02:35 +03:00
|
|
|
sftp_pkt_addstring_start(pktout);
|
|
|
|
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
2001-02-23 21:21:44 +03:00
|
|
|
sfree(handle->hstring);
|
|
|
|
sfree(handle);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2016-12-28 17:34:53 +03:00
|
|
|
int fxp_close_recv(struct sftp_packet *pktin, struct sftp_request *req)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
sfree(req);
|
2003-06-29 18:26:09 +04:00
|
|
|
fxp_got_status(pktin);
|
|
|
|
sftp_pkt_free(pktin);
|
2016-12-28 17:34:53 +03:00
|
|
|
return fxp_errtype == SSH_FX_OK;
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
|
|
|
|
2015-05-15 13:15:42 +03:00
|
|
|
struct sftp_request *fxp_mkdir_send(const char *path)
|
2001-08-04 18:19:51 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-08-04 18:19:51 +04:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_MKDIR);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-08-26 15:35:11 +04:00
|
|
|
sftp_pkt_addstring(pktout, path);
|
2001-08-04 18:19:51 +04:00
|
|
|
sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
|
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
int fxp_mkdir_recv(struct sftp_packet *pktin, struct sftp_request *req)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
int id;
|
|
|
|
sfree(req);
|
|
|
|
id = fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-08-04 18:19:51 +04:00
|
|
|
if (id != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-15 13:15:42 +03:00
|
|
|
struct sftp_request *fxp_rmdir_send(const char *path)
|
2001-08-04 18:19:51 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-08-04 18:19:51 +04:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_RMDIR);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-08-26 15:35:11 +04:00
|
|
|
sftp_pkt_addstring(pktout, path);
|
2001-08-04 18:19:51 +04:00
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
int fxp_rmdir_recv(struct sftp_packet *pktin, struct sftp_request *req)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
int id;
|
|
|
|
sfree(req);
|
|
|
|
id = fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-08-04 18:19:51 +04:00
|
|
|
if (id != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-15 13:15:42 +03:00
|
|
|
struct sftp_request *fxp_remove_send(const char *fname)
|
2001-08-04 18:19:51 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-08-04 18:19:51 +04:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_REMOVE);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-08-26 15:35:11 +04:00
|
|
|
sftp_pkt_addstring(pktout, fname);
|
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
int fxp_remove_recv(struct sftp_packet *pktin, struct sftp_request *req)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
int id;
|
|
|
|
sfree(req);
|
|
|
|
id = fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-08-26 15:35:11 +04:00
|
|
|
if (id != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-15 13:15:42 +03:00
|
|
|
struct sftp_request *fxp_rename_send(const char *srcfname,
|
|
|
|
const char *dstfname)
|
2001-08-26 15:35:11 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-08-26 15:35:11 +04:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_RENAME);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-08-26 15:35:11 +04:00
|
|
|
sftp_pkt_addstring(pktout, srcfname);
|
|
|
|
sftp_pkt_addstring(pktout, dstfname);
|
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
int fxp_rename_recv(struct sftp_packet *pktin, struct sftp_request *req)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
int id;
|
|
|
|
sfree(req);
|
|
|
|
id = fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-08-26 15:35:11 +04:00
|
|
|
if (id != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve the attributes of a file. We have fxp_stat which works
|
|
|
|
* on filenames, and fxp_fstat which works on open file handles.
|
|
|
|
*/
|
2015-05-15 13:15:42 +03:00
|
|
|
struct sftp_request *fxp_stat_send(const char *fname)
|
2001-08-26 15:35:11 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-08-26 15:35:11 +04:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_STAT);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-08-26 15:35:11 +04:00
|
|
|
sftp_pkt_addstring(pktout, fname);
|
|
|
|
sftp_send(pktout);
|
|
|
|
|
2003-06-29 18:26:09 +04:00
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
int fxp_stat_recv(struct sftp_packet *pktin, struct sftp_request *req,
|
|
|
|
struct fxp_attrs *attrs)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
sfree(req);
|
2001-08-26 15:35:11 +04:00
|
|
|
if (pktin->type == SSH_FXP_ATTRS) {
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getattrs(pktin, attrs)) {
|
|
|
|
fxp_internal_error("malformed SSH_FXP_ATTRS packet");
|
|
|
|
sftp_pkt_free(pktin);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sftp_pkt_free(pktin);
|
2001-08-26 15:35:11 +04:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-08-26 15:35:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *fxp_fstat_send(struct fxp_handle *handle)
|
2001-08-26 15:35:11 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-08-26 15:35:11 +04:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_FSTAT);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-08-04 18:19:51 +04:00
|
|
|
sftp_pkt_addstring_start(pktout);
|
2001-08-26 15:35:11 +04:00
|
|
|
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
|
|
|
|
sftp_send(pktout);
|
|
|
|
|
2003-06-29 18:26:09 +04:00
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
int fxp_fstat_recv(struct sftp_packet *pktin, struct sftp_request *req,
|
2003-06-29 18:26:09 +04:00
|
|
|
struct fxp_attrs *attrs)
|
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
sfree(req);
|
2001-08-26 15:35:11 +04:00
|
|
|
if (pktin->type == SSH_FXP_ATTRS) {
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getattrs(pktin, attrs)) {
|
|
|
|
fxp_internal_error("malformed SSH_FXP_ATTRS packet");
|
|
|
|
sftp_pkt_free(pktin);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sftp_pkt_free(pktin);
|
2001-08-26 15:35:11 +04:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-08-26 15:35:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the attributes of a file.
|
|
|
|
*/
|
2015-05-15 13:15:42 +03:00
|
|
|
struct sftp_request *fxp_setstat_send(const char *fname,
|
|
|
|
struct fxp_attrs attrs)
|
2001-08-26 15:35:11 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-08-26 15:35:11 +04:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_SETSTAT);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-08-26 15:35:11 +04:00
|
|
|
sftp_pkt_addstring(pktout, fname);
|
|
|
|
sftp_pkt_addattrs(pktout, attrs);
|
2001-08-04 18:19:51 +04:00
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
int fxp_setstat_recv(struct sftp_packet *pktin, struct sftp_request *req)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
int id;
|
|
|
|
sfree(req);
|
|
|
|
id = fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-08-04 18:19:51 +04:00
|
|
|
if (id != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
struct sftp_request *fxp_fsetstat_send(struct fxp_handle *handle,
|
|
|
|
struct fxp_attrs attrs)
|
2001-08-26 22:32:28 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-08-26 22:32:28 +04:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_FSETSTAT);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-08-26 22:32:28 +04:00
|
|
|
sftp_pkt_addstring_start(pktout);
|
|
|
|
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
|
|
|
|
sftp_pkt_addattrs(pktout, attrs);
|
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
int fxp_fsetstat_recv(struct sftp_packet *pktin, struct sftp_request *req)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
int id;
|
|
|
|
sfree(req);
|
|
|
|
id = fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-08-26 22:32:28 +04:00
|
|
|
if (id != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2001-08-04 18:19:51 +04:00
|
|
|
|
2001-02-23 21:21:44 +03:00
|
|
|
/*
|
|
|
|
* Read from a file. Returns the number of bytes read, or -1 on an
|
|
|
|
* error, or possibly 0 if EOF. (I'm not entirely sure whether it
|
|
|
|
* will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
|
|
|
|
* error indicator. It might even depend on the SFTP server.)
|
|
|
|
*/
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *fxp_read_send(struct fxp_handle *handle,
|
|
|
|
uint64 offset, int len)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-02-23 21:21:44 +03:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_READ);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-02-24 15:02:35 +03:00
|
|
|
sftp_pkt_addstring_start(pktout);
|
|
|
|
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_adduint64(pktout, offset);
|
|
|
|
sftp_pkt_adduint32(pktout, len);
|
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
int fxp_read_recv(struct sftp_packet *pktin, struct sftp_request *req,
|
|
|
|
char *buffer, int len)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
sfree(req);
|
2001-02-23 21:21:44 +03:00
|
|
|
if (pktin->type == SSH_FXP_DATA) {
|
|
|
|
char *str;
|
|
|
|
int rlen;
|
|
|
|
|
2005-02-20 13:30:05 +03:00
|
|
|
if (!sftp_pkt_getstring(pktin, &str, &rlen)) {
|
|
|
|
fxp_internal_error("READ returned malformed SSH_FXP_DATA packet");
|
|
|
|
sftp_pkt_free(pktin);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-02-23 21:21:44 +03:00
|
|
|
|
|
|
|
if (rlen > len || rlen < 0) {
|
|
|
|
fxp_internal_error("READ returned more bytes than requested");
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer, str, rlen);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return rlen;
|
|
|
|
} else {
|
|
|
|
fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read from a directory.
|
|
|
|
*/
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *fxp_readdir_send(struct fxp_handle *handle)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-02-23 21:21:44 +03:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_READDIR);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-02-24 15:02:35 +03:00
|
|
|
sftp_pkt_addstring_start(pktout);
|
|
|
|
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
struct fxp_names *fxp_readdir_recv(struct sftp_packet *pktin,
|
|
|
|
struct sftp_request *req)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
sfree(req);
|
2001-02-23 21:21:44 +03:00
|
|
|
if (pktin->type == SSH_FXP_NAME) {
|
|
|
|
struct fxp_names *ret;
|
2005-02-20 13:30:05 +03:00
|
|
|
unsigned long i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sanity-check the number of names. Minimum is obviously
|
|
|
|
* zero. Maximum is the remaining space in the packet
|
|
|
|
* divided by the very minimum length of a name, which is
|
|
|
|
* 12 bytes (4 for an empty filename, 4 for an empty
|
|
|
|
* longname, 4 for a set of attribute flags indicating that
|
|
|
|
* no other attributes are supplied).
|
|
|
|
*/
|
|
|
|
if (!sftp_pkt_getuint32(pktin, &i) ||
|
|
|
|
i > (pktin->length-pktin->savedpos)/12) {
|
|
|
|
fxp_internal_error("malformed FXP_NAME packet");
|
|
|
|
sftp_pkt_free(pktin);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure the implicit multiplication in the snewn() call
|
|
|
|
* doesn't suffer integer overflow and cause us to malloc
|
|
|
|
* too little space.
|
|
|
|
*/
|
|
|
|
if (i > INT_MAX / sizeof(struct fxp_name)) {
|
|
|
|
fxp_internal_error("unreasonably large FXP_NAME packet");
|
|
|
|
sftp_pkt_free(pktin);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-03-29 19:14:26 +03:00
|
|
|
ret = snew(struct fxp_names);
|
2005-02-20 13:30:05 +03:00
|
|
|
ret->nnames = i;
|
2003-03-29 19:14:26 +03:00
|
|
|
ret->names = snewn(ret->nnames, struct fxp_name);
|
2007-01-09 21:24:07 +03:00
|
|
|
for (i = 0; i < (unsigned long)ret->nnames; i++) {
|
2005-02-20 13:30:05 +03:00
|
|
|
char *str1, *str2;
|
|
|
|
int len1, len2;
|
|
|
|
if (!sftp_pkt_getstring(pktin, &str1, &len1) ||
|
|
|
|
!sftp_pkt_getstring(pktin, &str2, &len2) ||
|
|
|
|
!sftp_pkt_getattrs(pktin, &ret->names[i].attrs)) {
|
|
|
|
fxp_internal_error("malformed FXP_NAME packet");
|
|
|
|
while (i--) {
|
|
|
|
sfree(ret->names[i].filename);
|
|
|
|
sfree(ret->names[i].longname);
|
|
|
|
}
|
|
|
|
sfree(ret->names);
|
|
|
|
sfree(ret);
|
|
|
|
sfree(pktin);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret->names[i].filename = mkstr(str1, len1);
|
|
|
|
ret->names[i].longname = mkstr(str2, len2);
|
2001-02-23 21:21:44 +03:00
|
|
|
}
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write to a file. Returns 0 on error, 1 on OK.
|
|
|
|
*/
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *fxp_write_send(struct fxp_handle *handle,
|
|
|
|
char *buffer, uint64 offset, int len)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2003-06-29 18:26:09 +04:00
|
|
|
struct sftp_request *req = sftp_alloc_request();
|
|
|
|
struct sftp_packet *pktout;
|
2001-02-23 21:21:44 +03:00
|
|
|
|
|
|
|
pktout = sftp_pkt_init(SSH_FXP_WRITE);
|
2003-06-29 18:26:09 +04:00
|
|
|
sftp_pkt_adduint32(pktout, req->id);
|
2001-02-24 15:02:35 +03:00
|
|
|
sftp_pkt_addstring_start(pktout);
|
|
|
|
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
|
2001-02-23 21:21:44 +03:00
|
|
|
sftp_pkt_adduint64(pktout, offset);
|
|
|
|
sftp_pkt_addstring_start(pktout);
|
|
|
|
sftp_pkt_addstring_data(pktout, buffer, len);
|
|
|
|
sftp_send(pktout);
|
2003-06-29 18:26:09 +04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2003-06-29 18:47:14 +04:00
|
|
|
int fxp_write_recv(struct sftp_packet *pktin, struct sftp_request *req)
|
2003-06-29 18:26:09 +04:00
|
|
|
{
|
2003-06-29 18:47:14 +04:00
|
|
|
sfree(req);
|
2001-02-23 21:21:44 +03:00
|
|
|
fxp_got_status(pktin);
|
2002-03-01 16:16:25 +03:00
|
|
|
sftp_pkt_free(pktin);
|
2001-02-23 21:21:44 +03:00
|
|
|
return fxp_errtype == SSH_FX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free up an fxp_names structure.
|
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
void fxp_free_names(struct fxp_names *names)
|
|
|
|
{
|
2001-02-23 21:21:44 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < names->nnames; i++) {
|
|
|
|
sfree(names->names[i].filename);
|
|
|
|
sfree(names->names[i].longname);
|
|
|
|
}
|
|
|
|
sfree(names->names);
|
|
|
|
sfree(names);
|
|
|
|
}
|
2002-03-31 20:26:13 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Duplicate an fxp_name structure.
|
|
|
|
*/
|
|
|
|
struct fxp_name *fxp_dup_name(struct fxp_name *name)
|
|
|
|
{
|
|
|
|
struct fxp_name *ret;
|
2003-03-29 19:14:26 +03:00
|
|
|
ret = snew(struct fxp_name);
|
2002-03-31 20:26:13 +04:00
|
|
|
ret->filename = dupstr(name->filename);
|
|
|
|
ret->longname = dupstr(name->longname);
|
|
|
|
ret->attrs = name->attrs; /* structure copy */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free up an fxp_name structure.
|
|
|
|
*/
|
|
|
|
void fxp_free_name(struct fxp_name *name)
|
|
|
|
{
|
|
|
|
sfree(name->filename);
|
|
|
|
sfree(name->longname);
|
|
|
|
sfree(name);
|
|
|
|
}
|
2003-09-27 21:52:34 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Store user data in an sftp_request structure.
|
|
|
|
*/
|
|
|
|
void *fxp_get_userdata(struct sftp_request *req)
|
|
|
|
{
|
|
|
|
return req->userdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fxp_set_userdata(struct sftp_request *req, void *data)
|
|
|
|
{
|
|
|
|
req->userdata = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A wrapper to go round fxp_read_* and fxp_write_*, which manages
|
|
|
|
* the queueing of multiple read/write requests.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct req {
|
|
|
|
char *buffer;
|
|
|
|
int len, retlen, complete;
|
|
|
|
uint64 offset;
|
|
|
|
struct req *next, *prev;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fxp_xfer {
|
|
|
|
uint64 offset, furthestdata, filesize;
|
2003-09-28 18:24:01 +04:00
|
|
|
int req_totalsize, req_maxsize, eof, err;
|
2003-09-27 21:52:34 +04:00
|
|
|
struct fxp_handle *fh;
|
|
|
|
struct req *head, *tail;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct fxp_xfer *xfer_init(struct fxp_handle *fh, uint64 offset)
|
|
|
|
{
|
|
|
|
struct fxp_xfer *xfer = snew(struct fxp_xfer);
|
|
|
|
|
|
|
|
xfer->fh = fh;
|
|
|
|
xfer->offset = offset;
|
|
|
|
xfer->head = xfer->tail = NULL;
|
2003-09-28 18:24:01 +04:00
|
|
|
xfer->req_totalsize = 0;
|
2006-06-02 12:46:34 +04:00
|
|
|
xfer->req_maxsize = 1048576;
|
2003-09-27 21:52:34 +04:00
|
|
|
xfer->err = 0;
|
|
|
|
xfer->filesize = uint64_make(ULONG_MAX, ULONG_MAX);
|
|
|
|
xfer->furthestdata = uint64_make(0, 0);
|
|
|
|
|
|
|
|
return xfer;
|
|
|
|
}
|
|
|
|
|
2003-09-28 18:24:01 +04:00
|
|
|
int xfer_done(struct fxp_xfer *xfer)
|
2003-09-27 21:52:34 +04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We're finished if we've seen EOF _and_ there are no
|
|
|
|
* outstanding requests.
|
|
|
|
*/
|
|
|
|
return (xfer->eof || xfer->err) && !xfer->head;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xfer_download_queue(struct fxp_xfer *xfer)
|
|
|
|
{
|
2004-12-17 16:39:41 +03:00
|
|
|
while (xfer->req_totalsize < xfer->req_maxsize &&
|
|
|
|
!xfer->eof && !xfer->err) {
|
2003-09-27 21:52:34 +04:00
|
|
|
/*
|
|
|
|
* Queue a new read request.
|
|
|
|
*/
|
|
|
|
struct req *rr;
|
|
|
|
struct sftp_request *req;
|
|
|
|
|
|
|
|
rr = snew(struct req);
|
|
|
|
rr->offset = xfer->offset;
|
|
|
|
rr->complete = 0;
|
|
|
|
if (xfer->tail) {
|
|
|
|
xfer->tail->next = rr;
|
|
|
|
rr->prev = xfer->tail;
|
|
|
|
} else {
|
|
|
|
xfer->head = rr;
|
|
|
|
rr->prev = NULL;
|
|
|
|
}
|
|
|
|
xfer->tail = rr;
|
|
|
|
rr->next = NULL;
|
|
|
|
|
2006-06-02 12:46:34 +04:00
|
|
|
rr->len = 32768;
|
2003-09-27 21:52:34 +04:00
|
|
|
rr->buffer = snewn(rr->len, char);
|
|
|
|
sftp_register(req = fxp_read_send(xfer->fh, rr->offset, rr->len));
|
|
|
|
fxp_set_userdata(req, rr);
|
|
|
|
|
|
|
|
xfer->offset = uint64_add32(xfer->offset, rr->len);
|
2003-09-28 18:24:01 +04:00
|
|
|
xfer->req_totalsize += rr->len;
|
2003-09-27 21:52:34 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_DOWNLOAD
|
|
|
|
{ char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing read request %p at %s\n", rr, buf); }
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fxp_xfer *xfer_download_init(struct fxp_handle *fh, uint64 offset)
|
|
|
|
{
|
|
|
|
struct fxp_xfer *xfer = xfer_init(fh, offset);
|
|
|
|
|
|
|
|
xfer->eof = FALSE;
|
|
|
|
xfer_download_queue(xfer);
|
|
|
|
|
|
|
|
return xfer;
|
|
|
|
}
|
|
|
|
|
2013-07-11 21:24:53 +04:00
|
|
|
/*
|
|
|
|
* Returns INT_MIN to indicate that it didn't even get as far as
|
|
|
|
* fxp_read_recv and hence has not freed pktin.
|
|
|
|
*/
|
2003-09-27 21:52:34 +04:00
|
|
|
int xfer_download_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin)
|
|
|
|
{
|
|
|
|
struct sftp_request *rreq;
|
|
|
|
struct req *rr;
|
|
|
|
|
|
|
|
rreq = sftp_find_request(pktin);
|
Clean up handling of the return value from sftp_find_request. In many
places we simply enforce by assertion that it will match the request
we sent out a moment ago: in fact it can also return NULL, so it makes
more sense to report a proper error message if it doesn't return the
expected value, and while we're at it, have that error message
whatever message was helpfully left in fxp_error() by
sftp_find_request when it failed.
To do this, I've written a centralised function in psftp.c called
sftp_wait_for_reply, which is handed a request that's just been sent
out and deals with the mechanics of waiting for its reply, returning
the reply when it arrives, and aborting with a sensible error if
anything else arrives instead. The numerous sites in psftp.c which
called sftp_find_request have all been rewritten to do this instead,
and as a side effect they now look more sensible. The only other uses
of sftp_find_request were in xfer_*load_gotpkt, which had to be
tweaked in its own way.
While I'm here, also fix memory management in sftp_find_request, which
was freeing its input packet on some but not all error return paths.
[originally from svn r9894]
2013-07-07 00:43:21 +04:00
|
|
|
if (!rreq)
|
2013-07-11 21:24:53 +04:00
|
|
|
return INT_MIN; /* this packet doesn't even make sense */
|
2003-09-27 21:52:34 +04:00
|
|
|
rr = (struct req *)fxp_get_userdata(rreq);
|
2013-07-11 21:24:53 +04:00
|
|
|
if (!rr) {
|
|
|
|
fxp_internal_error("request ID is not part of the current download");
|
|
|
|
return INT_MIN; /* this packet isn't ours */
|
|
|
|
}
|
2003-09-27 21:52:34 +04:00
|
|
|
rr->retlen = fxp_read_recv(pktin, rreq, rr->buffer, rr->len);
|
|
|
|
#ifdef DEBUG_DOWNLOAD
|
|
|
|
printf("read request %p has returned [%d]\n", rr, rr->retlen);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((rr->retlen < 0 && fxp_error_type()==SSH_FX_EOF) || rr->retlen == 0) {
|
|
|
|
xfer->eof = TRUE;
|
2017-02-16 00:39:23 +03:00
|
|
|
rr->retlen = 0;
|
2003-09-27 21:52:34 +04:00
|
|
|
rr->complete = -1;
|
|
|
|
#ifdef DEBUG_DOWNLOAD
|
|
|
|
printf("setting eof\n");
|
|
|
|
#endif
|
|
|
|
} else if (rr->retlen < 0) {
|
|
|
|
/* some error other than EOF; signal it back to caller */
|
2004-12-17 16:39:41 +03:00
|
|
|
xfer_set_error(xfer);
|
|
|
|
rr->complete = -1;
|
2003-09-27 21:52:34 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rr->complete = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Special case: if we have received fewer bytes than we
|
|
|
|
* actually read, we should do something. For the moment I'll
|
|
|
|
* just throw an ersatz FXP error to signal this; the SFTP
|
|
|
|
* draft I've got says that it can't happen except on special
|
|
|
|
* files, in which case seeking probably has very little
|
|
|
|
* meaning and so queueing an additional read request to fill
|
|
|
|
* up the gap sounds like the wrong answer. I'm not sure what I
|
|
|
|
* should be doing here - if it _was_ a special file, I suspect
|
|
|
|
* I simply shouldn't have been queueing multiple requests in
|
|
|
|
* the first place...
|
|
|
|
*/
|
|
|
|
if (rr->retlen > 0 && uint64_compare(xfer->furthestdata, rr->offset) < 0) {
|
|
|
|
xfer->furthestdata = rr->offset;
|
|
|
|
#ifdef DEBUG_DOWNLOAD
|
|
|
|
{ char buf[40];
|
|
|
|
uint64_decimal(xfer->furthestdata, buf);
|
|
|
|
printf("setting furthestdata = %s\n", buf); }
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rr->retlen < rr->len) {
|
|
|
|
uint64 filesize = uint64_add32(rr->offset,
|
|
|
|
(rr->retlen < 0 ? 0 : rr->retlen));
|
|
|
|
#ifdef DEBUG_DOWNLOAD
|
|
|
|
{ char buf[40];
|
|
|
|
uint64_decimal(filesize, buf);
|
|
|
|
printf("short block! trying filesize = %s\n", buf); }
|
|
|
|
#endif
|
|
|
|
if (uint64_compare(xfer->filesize, filesize) > 0) {
|
|
|
|
xfer->filesize = filesize;
|
|
|
|
#ifdef DEBUG_DOWNLOAD
|
|
|
|
printf("actually changing filesize\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uint64_compare(xfer->furthestdata, xfer->filesize) > 0) {
|
|
|
|
fxp_error_message = "received a short buffer from FXP_READ, but not"
|
|
|
|
" at EOF";
|
|
|
|
fxp_errtype = -1;
|
|
|
|
xfer_set_error(xfer);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xfer_set_error(struct fxp_xfer *xfer)
|
|
|
|
{
|
|
|
|
xfer->err = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xfer_download_data(struct fxp_xfer *xfer, void **buf, int *len)
|
|
|
|
{
|
|
|
|
void *retbuf = NULL;
|
|
|
|
int retlen = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Discard anything at the head of the rr queue with complete <
|
|
|
|
* 0; return the first thing with complete > 0.
|
|
|
|
*/
|
|
|
|
while (xfer->head && xfer->head->complete && !retbuf) {
|
|
|
|
struct req *rr = xfer->head;
|
|
|
|
|
|
|
|
if (rr->complete > 0) {
|
|
|
|
retbuf = rr->buffer;
|
|
|
|
retlen = rr->retlen;
|
|
|
|
#ifdef DEBUG_DOWNLOAD
|
|
|
|
printf("handing back data from read request %p\n", rr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_DOWNLOAD
|
|
|
|
else
|
|
|
|
printf("skipping failed read request %p\n", rr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
xfer->head = xfer->head->next;
|
|
|
|
if (xfer->head)
|
|
|
|
xfer->head->prev = NULL;
|
|
|
|
else
|
|
|
|
xfer->tail = NULL;
|
2003-09-28 18:24:01 +04:00
|
|
|
xfer->req_totalsize -= rr->len;
|
2003-09-27 21:52:34 +04:00
|
|
|
sfree(rr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retbuf) {
|
|
|
|
*buf = retbuf;
|
|
|
|
*len = retlen;
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-28 18:24:01 +04:00
|
|
|
struct fxp_xfer *xfer_upload_init(struct fxp_handle *fh, uint64 offset)
|
|
|
|
{
|
|
|
|
struct fxp_xfer *xfer = xfer_init(fh, offset);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We set `eof' to 1 because this will cause xfer_done() to
|
|
|
|
* return true iff there are no outstanding requests. During an
|
|
|
|
* upload, our caller will be responsible for working out
|
|
|
|
* whether all the data has been sent, so all it needs to know
|
|
|
|
* from us is whether the outstanding requests have been
|
|
|
|
* handled once that's done.
|
|
|
|
*/
|
|
|
|
xfer->eof = 1;
|
|
|
|
|
|
|
|
return xfer;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xfer_upload_ready(struct fxp_xfer *xfer)
|
|
|
|
{
|
2016-04-09 02:24:12 +03:00
|
|
|
if (sftp_sendbuffer() == 0)
|
2003-09-28 18:24:01 +04:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xfer_upload_data(struct fxp_xfer *xfer, char *buffer, int len)
|
|
|
|
{
|
|
|
|
struct req *rr;
|
|
|
|
struct sftp_request *req;
|
|
|
|
|
|
|
|
rr = snew(struct req);
|
|
|
|
rr->offset = xfer->offset;
|
|
|
|
rr->complete = 0;
|
|
|
|
if (xfer->tail) {
|
|
|
|
xfer->tail->next = rr;
|
|
|
|
rr->prev = xfer->tail;
|
|
|
|
} else {
|
|
|
|
xfer->head = rr;
|
|
|
|
rr->prev = NULL;
|
|
|
|
}
|
|
|
|
xfer->tail = rr;
|
|
|
|
rr->next = NULL;
|
|
|
|
|
|
|
|
rr->len = len;
|
|
|
|
rr->buffer = NULL;
|
|
|
|
sftp_register(req = fxp_write_send(xfer->fh, buffer, rr->offset, len));
|
|
|
|
fxp_set_userdata(req, rr);
|
|
|
|
|
|
|
|
xfer->offset = uint64_add32(xfer->offset, rr->len);
|
|
|
|
xfer->req_totalsize += rr->len;
|
|
|
|
|
|
|
|
#ifdef DEBUG_UPLOAD
|
|
|
|
{ char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing write request %p at %s [len %d]\n", rr, buf, len); }
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-07-11 21:24:53 +04:00
|
|
|
/*
|
|
|
|
* Returns INT_MIN to indicate that it didn't even get as far as
|
|
|
|
* fxp_write_recv and hence has not freed pktin.
|
|
|
|
*/
|
2003-09-28 18:24:01 +04:00
|
|
|
int xfer_upload_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin)
|
|
|
|
{
|
|
|
|
struct sftp_request *rreq;
|
|
|
|
struct req *rr, *prev, *next;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
rreq = sftp_find_request(pktin);
|
Clean up handling of the return value from sftp_find_request. In many
places we simply enforce by assertion that it will match the request
we sent out a moment ago: in fact it can also return NULL, so it makes
more sense to report a proper error message if it doesn't return the
expected value, and while we're at it, have that error message
whatever message was helpfully left in fxp_error() by
sftp_find_request when it failed.
To do this, I've written a centralised function in psftp.c called
sftp_wait_for_reply, which is handed a request that's just been sent
out and deals with the mechanics of waiting for its reply, returning
the reply when it arrives, and aborting with a sensible error if
anything else arrives instead. The numerous sites in psftp.c which
called sftp_find_request have all been rewritten to do this instead,
and as a side effect they now look more sensible. The only other uses
of sftp_find_request were in xfer_*load_gotpkt, which had to be
tweaked in its own way.
While I'm here, also fix memory management in sftp_find_request, which
was freeing its input packet on some but not all error return paths.
[originally from svn r9894]
2013-07-07 00:43:21 +04:00
|
|
|
if (!rreq)
|
2013-07-11 21:24:53 +04:00
|
|
|
return INT_MIN; /* this packet doesn't even make sense */
|
2003-09-28 18:24:01 +04:00
|
|
|
rr = (struct req *)fxp_get_userdata(rreq);
|
2013-07-11 21:24:53 +04:00
|
|
|
if (!rr) {
|
|
|
|
fxp_internal_error("request ID is not part of the current upload");
|
|
|
|
return INT_MIN; /* this packet isn't ours */
|
|
|
|
}
|
2003-09-28 18:24:01 +04:00
|
|
|
ret = fxp_write_recv(pktin, rreq);
|
|
|
|
#ifdef DEBUG_UPLOAD
|
|
|
|
printf("write request %p has returned [%d]\n", rr, ret);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove this one from the queue.
|
|
|
|
*/
|
|
|
|
prev = rr->prev;
|
|
|
|
next = rr->next;
|
|
|
|
if (prev)
|
|
|
|
prev->next = next;
|
|
|
|
else
|
|
|
|
xfer->head = next;
|
|
|
|
if (next)
|
|
|
|
next->prev = prev;
|
|
|
|
else
|
|
|
|
xfer->tail = prev;
|
|
|
|
xfer->req_totalsize -= rr->len;
|
|
|
|
sfree(rr);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-09-27 21:52:34 +04:00
|
|
|
void xfer_cleanup(struct fxp_xfer *xfer)
|
|
|
|
{
|
|
|
|
struct req *rr;
|
|
|
|
while (xfer->head) {
|
|
|
|
rr = xfer->head;
|
|
|
|
xfer->head = xfer->head->next;
|
|
|
|
sfree(rr->buffer);
|
|
|
|
sfree(rr);
|
|
|
|
}
|
|
|
|
sfree(xfer);
|
|
|
|
}
|