putty/sftpcommon.c

140 строки
3.4 KiB
C
Исходник Обычный вид История

Add an SFTP server to the SSH server code. Unlike the traditional Unix SSH server organisation, the SFTP server is built into the same process as all the rest of the code. sesschan.c spots a subsystem request for "sftp", and responds to it by instantiating an SftpServer object and swapping out its own vtable for one that talks to it. (I rather like the idea of an object swapping its own vtable for a different one in the middle of its lifetime! This is one of those tricks that would be absurdly hard to implement in a 'proper' OO language, but when you're doing vtables by hand in C, it's no more difficult than any other piece of ordinary pointer manipulation. As long as the methods in both vtables expect the same physical structure layout, it doesn't cause a problem.) The SftpServer object doesn't deal directly with SFTP packet formats; it implements the SFTP server logic in a more abstract way, by having a vtable method for each SFTP request type with an appropriate parameter list. It sends its replies by calling methods in another vtable called SftpReplyBuilder, which in the normal case will write an SFTP reply packet to send back to the client. So SftpServer can focus more or less completely on the details of a particular filesystem API - and hence, the implementation I've got lives in the unix source directory, and works directly with file descriptors and struct stat and the like. (One purpose of this abstraction layer is that I may well want to write a second dummy implementation, for test-suite purposes, with completely controllable behaviour, and now I have a handy place to plug it in in place of the live filesystem.) In between sesschan's parsing of the byte stream into SFTP packets and the SftpServer object, there's a layer in the new file sftpserver.c which does the actual packet decoding and encoding: each request packet is passed to that, which pulls the fields out of the request packet and calls the appropriate method of SftpServer. It also provides the default SftpReplyBuilder which makes the output packet. I've moved some code out of the previous SFTP client implementation - basic packet construction code, and in particular the BinarySink/ BinarySource marshalling fuinction for fxp_attrs - into sftpcommon.c, so that the two directions can share as much as possible.
2018-10-21 00:10:32 +03:00
/*
* sftpcommon.c: SFTP code shared between client and server.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include "misc.h"
#include "sftp.h"
static void sftp_pkt_BinarySink_write(
BinarySink *bs, const void *data, size_t length)
{
struct sftp_packet *pkt = BinarySink_DOWNCAST(bs, struct sftp_packet);
unsigned newlen;
assert(length <= 0xFFFFFFFFU - pkt->length);
newlen = pkt->length + length;
if (pkt->maxlen < newlen) {
pkt->maxlen = newlen * 5 / 4 + 256;
pkt->data = sresize(pkt->data, pkt->maxlen, char);
}
memcpy(pkt->data + pkt->length, data, length);
pkt->length = newlen;
}
struct sftp_packet *sftp_pkt_init(int type)
{
struct sftp_packet *pkt;
pkt = snew(struct sftp_packet);
pkt->data = NULL;
pkt->savedpos = -1;
pkt->length = 0;
pkt->maxlen = 0;
pkt->type = type;
BinarySink_INIT(pkt, sftp_pkt_BinarySink_write);
put_uint32(pkt, 0); /* length field will be filled in later */
put_byte(pkt, 0); /* so will the type field */
return pkt;
}
void BinarySink_put_fxp_attrs(BinarySink *bs, struct fxp_attrs attrs)
{
put_uint32(bs, attrs.flags);
if (attrs.flags & SSH_FILEXFER_ATTR_SIZE)
put_uint64(bs, attrs.size);
if (attrs.flags & SSH_FILEXFER_ATTR_UIDGID) {
put_uint32(bs, attrs.uid);
put_uint32(bs, attrs.gid);
}
if (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
put_uint32(bs, attrs.permissions);
}
if (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
put_uint32(bs, attrs.atime);
put_uint32(bs, attrs.mtime);
}
if (attrs.flags & SSH_FILEXFER_ATTR_EXTENDED) {
/*
* We currently don't support sending any extended
* attributes.
*/
}
}
const struct fxp_attrs no_attrs = { 0 };
#define put_fxp_attrs(bs, attrs) \
BinarySink_put_fxp_attrs(BinarySink_UPCAST(bs), attrs)
int BinarySource_get_fxp_attrs(BinarySource *src, struct fxp_attrs *attrs)
{
attrs->flags = get_uint32(src);
if (attrs->flags & SSH_FILEXFER_ATTR_SIZE)
attrs->size = get_uint64(src);
if (attrs->flags & SSH_FILEXFER_ATTR_UIDGID) {
attrs->uid = get_uint32(src);
attrs->gid = get_uint32(src);
}
if (attrs->flags & SSH_FILEXFER_ATTR_PERMISSIONS)
attrs->permissions = get_uint32(src);
if (attrs->flags & SSH_FILEXFER_ATTR_ACMODTIME) {
attrs->atime = get_uint32(src);
attrs->mtime = get_uint32(src);
}
if (attrs->flags & SSH_FILEXFER_ATTR_EXTENDED) {
unsigned long count = get_uint32(src);
while (count--) {
/*
* We should try to analyse these, if we ever find one
* we recognise.
*/
get_string(src);
get_string(src);
}
}
return 1;
}
void sftp_pkt_free(struct sftp_packet *pkt)
{
if (pkt->data)
sfree(pkt->data);
sfree(pkt);
}
void sftp_send_prepare(struct sftp_packet *pkt)
{
PUT_32BIT(pkt->data, pkt->length - 4);
if (pkt->length >= 5) {
/* Rewrite the type code, in case the caller changed its mind
* about pkt->type since calling sftp_pkt_init */
pkt->data[4] = pkt->type;
}
}
struct sftp_packet *sftp_recv_prepare(unsigned length)
{
struct sftp_packet *pkt;
pkt = snew(struct sftp_packet);
pkt->savedpos = 0;
pkt->length = pkt->maxlen = length;
pkt->data = snewn(pkt->length, char);
return pkt;
}
int sftp_recv_finish(struct sftp_packet *pkt)
{
BinarySource_INIT(pkt, pkt->data, pkt->length);
pkt->type = get_byte(pkt);
return !get_err(pkt);
}