Commit 9421387c authored by Niels Möller's avatar Niels Möller
Browse files

* src/sftp/buffer.c: Let most output-related functions call exit()

directly on failure.

Rev: src/sftp/buffer.c:1.2
Rev: src/sftp/buffer.h:1.2
Rev: src/sftp/sftp-server.c:1.2
parent 67e54d0d
......@@ -8,6 +8,8 @@
#include "buffer.h"
#include <assert.h>
#define FATAL(x) do { fputs("sftp-server: " x "\n", stderr); exit(EXIT_FAILURE); } while (0)
#if LSH
#include <stdlib.h>
......@@ -166,7 +168,7 @@ sftp_read_packet(struct sftp_input *i)
/* Output */
static int
static void
sftp_check_output(struct sftp_output *o, UINT32 length)
{
UINT32 needed = o->i + length;
......@@ -176,48 +178,40 @@ sftp_check_output(struct sftp_output *o, UINT32 length)
UINT32 size = 2 * needed + 40;
p = realloc(o->data, size);
if (!p)
return 0;
FATAL("Virtual memory exhausted");
o->data = p;
o->size = size;
}
return 1;
}
int
void
sftp_put_data(struct sftp_output *o, UINT32 length, const UINT8 *data)
{
if (!sftp_check_output(o, length))
return 0;
sftp_check_output(o, length);
memcpy(o->data + o->i, data, length);
o->i += length;
return 1;
}
#define PUT_DATA(o, buf) \
(sftp_put_data((o), sizeof((buf)), (buf)))
int
void
sftp_put_uint8(struct sftp_output *o, UINT8 value)
{
if (!sftp_check_output(o, 1))
return 0;
sftp_check_output(o, 1);
o->data[o->i++] = value;
return 1;
}
int
void
sftp_put_uint32(struct sftp_output *o, UINT32 value)
{
UINT8 buf[4];
WRITE_UINT32(buf, value);
return PUT_DATA(o, buf);
PUT_DATA(o, buf);
}
#define WRITE_UINT64(p, i) \
......@@ -232,20 +226,20 @@ do { \
(p)[7] = (i) & 0xff; \
} while(0)
int
void
sftp_put_uint64(struct sftp_output *o, UINT64 value)
{
UINT8 buf[8];
WRITE_UINT64(buf, value);
return PUT_DATA(o, buf);
PUT_DATA(o, buf);
}
int
void
sftp_put_string(struct sftp_output *o, UINT32 length, UINT8 *data)
{
return sftp_put_uint32(o, length)
&& sftp_put_data(o, length, data);
sftp_put_uint32(o, length);
sftp_put_data(o, length, data);
}
UINT8 *
......@@ -253,8 +247,7 @@ sftp_put_reserve(struct sftp_output *o, UINT32 length)
{
UINT8 *result;
if (!sftp_check_output(o, length))
return NULL;
sftp_check_output(o, length);
result = o->data + o->i;
o->i += length;
......@@ -315,7 +308,7 @@ sftp_write_packet(struct sftp_output *o)
/* FIXME: Flushing after each packet is sub-optimal. */
if (fflush(o->f))
return 0;
return 1;
}
......
......@@ -37,11 +37,9 @@ sftp_set_id(struct sftp_output *o, UINT32 id);
int
sftp_write_packet(struct sftp_output *o);
#elif OPENSSH
#else /* !OPENSSH */
# error Needs either LSH or OPENSSH
#endif /* !OPENSSH */
#else /* !LSH */
# error Needs either LSH config.h
#endif /* !LSH */
struct sftp_input;
struct sftp_output;
......@@ -69,19 +67,19 @@ int
sftp_get_eod(struct sftp_input *i);
/* Output */
int
void
sftp_put_data(struct sftp_output *o, UINT32 length, const UINT8 *data);
int
void
sftp_put_uint8(struct sftp_output *o, UINT8 value);
int
void
sftp_put_uint32(struct sftp_output *o, UINT32 value);
int
void
sftp_put_uint64(struct sftp_output *o, UINT64 value);
int
void
sftp_put_string(struct sftp_output *o, UINT32 length, UINT8 *data);
UINT8 *
......
......@@ -116,45 +116,30 @@ sftp_get_attrib(struct sftp_input *i, struct sftp_attrib *a)
return 1;
}
int
void
sftp_put_attrib(struct sftp_output *o, const struct sftp_attrib *a)
{
assert(!a->flags & SSH_FILEXFER_ATTR_EXTENDED);
if (!sftp_put_uint32(o, a->flags))
return 0;
sftp_put_uint32(o, a->flags);
if (a->flags & SSH_FILEXFER_ATTR_SIZE)
{
if (!sftp_put_uint64(o, a->size))
return 0;
}
sftp_put_uint64(o, a->size);
if (a->flags & SSH_FILEXFER_ATTR_UIDGID)
{
if (!sftp_put_uint32(o, a->uid))
return 0;
if (!sftp_put_uint32(o, a->gid))
return 0;
sftp_put_uint32(o, a->uid);
sftp_put_uint32(o, a->gid);
}
if (a->flags & SSH_FILEXFER_ATTR_PERMISSIONS)
{
if (!sftp_put_uint32(o, a->permissions))
return 0;
}
sftp_put_uint32(o, a->permissions);
if (a->flags & SSH_FILEXFER_ATTR_ACMODTIME)
{
if (!sftp_put_uint32(o, a->atime))
return 0;
if (!sftp_put_uint32(o, a->mtime))
return 0;
sftp_put_uint32(o, a->atime);
sftp_put_uint32(o, a->mtime);
}
return 1;
}
#define SFTP_MAX_FDS 200
......@@ -227,11 +212,11 @@ sftp_get_handle(struct sftp_ctx *ctx, handle_t *handle)
return 0;
}
int
void
sftp_put_handle(struct sftp_ctx *ctx, handle_t handle)
{
return sftp_put_uint32(ctx->o, 4)
&& sftp_put_uint32(ctx->o, handle);
sftp_put_uint32(ctx->o, 4);
sftp_put_uint32(ctx->o, handle);
}
/* NOTE: The status message should be expanded with a human-readable
......@@ -240,7 +225,8 @@ int
sftp_send_status(struct sftp_ctx *ctx, UINT32 status)
{
sftp_set_msg(ctx->o, SSH_FXP_STATUS);
return sftp_put_uint32(ctx->o, status);
sftp_put_uint32(ctx->o, status);
return 1;
}
int
......@@ -307,7 +293,8 @@ sftp_process_open(struct sftp_ctx *ctx)
switch (pflags & (SSH_FXF_READ | SSH_FXF_WRITE))
{
case 0:
return sftp_send_status(ctx, SSH_FX_FAILURE);
sftp_send_status(ctx, SSH_FX_FAILURE);
return 1;
case SSH_FXF_READ:
mode = O_RDONLY;
break;
......@@ -414,8 +401,8 @@ sftp_process(sftp_process_func **dispatch,
/* Every handler should result in at least one message */
if (!sftp_write_packet(ctx->o))
FATAL("writing packet failed.");
exit(EXIT_FAILURE);
if (!ok)
exit(EXIT_FAILURE);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment