buffer.c 10.8 KB
Newer Older
1
2
3
4
5
6
7
/* buffer.c
 *
 * $Id$
 *
 * Buffering for sftp.
 */

Niels Möller's avatar
Niels Möller committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 2001 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

26
#include "buffer.h"
27

28
#include "xmalloc.h"
29
30
#include "sftp.h"

31
#include <assert.h>
32
#include <stdarg.h>
Niels Möller's avatar
Niels Möller committed
33
#include <string.h>
34

35
36
#define FATAL(x) do { fputs("sftp-server: " x "\n", stderr); exit(EXIT_FAILURE); } while (0)

37
38
#include <stdlib.h>

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Reads a 32-bit integer, in network byte order */
#define READ_UINT32(p)				\
(  (((UINT32) (p)[0]) << 24)			\
 | (((UINT32) (p)[1]) << 16)			\
 | (((UINT32) (p)[2]) << 8)			\
 |  ((UINT32) (p)[3]))

#define WRITE_UINT32(p, i)			\
do {						\
  (p)[0] = ((i) >> 24) & 0xff;			\
  (p)[1] = ((i) >> 16) & 0xff;			\
  (p)[2] = ((i) >> 8) & 0xff;			\
  (p)[3] = (i) & 0xff;				\
} while(0)

#define SFTP_MAX_STRINGS 2

56
57
58
59
struct sftp_input
{
  FILE *f;
  UINT32 left;
60
61
62
63

  /* Strings that we own */
  UINT8 *strings[SFTP_MAX_STRINGS];
  unsigned used_strings;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
};

struct sftp_output
{
  FILE *f;

  /* The message type is the first byte of a message, after the
   * length. */
  UINT8 msg;

  /* The next word is either the id, or the version. */
  UINT32 first;

  /* The rest of the packet is variable length. */
  UINT8 *data;
  UINT32 size;
  UINT32 i;
};

/* Input */

static int
86
sftp_check_input(const struct sftp_input *i, UINT32 length)
87
{
88
  return (i->left >= length);
89
90
91
92
93
}

int
sftp_get_data(struct sftp_input *i, UINT32 length, UINT8 *data)
{
94
95
96
97
98
99
  if (sftp_check_input(i, length))
    {
      i->left -= length;
      return (fread(data, 1, length, i->f) == length);
    }
  return 0;
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
}

#define GET_DATA(i, buf) \
(sftp_get_data((i), sizeof((buf)), (buf)))

int
sftp_get_uint8(struct sftp_input *i, UINT8 *value)
{
  return sftp_get_data(i, 1, value);    
}

int
sftp_get_uint32(struct sftp_input *i, UINT32 *value)
{
  UINT8 buf[4];
  if (!GET_DATA(i, buf))
    return 0;

  *value = READ_UINT32(buf);
  return 1;
}

UINT8 *
sftp_get_string(struct sftp_input *i, UINT32 *length)
{
  UINT8 *data;
126

127
128
129
  if (!(sftp_get_uint32(i, length) && sftp_check_input(i, *length)))
    return NULL;

130
  data = xmalloc(*length + 1);
131
132
133
134
135
136
137
138
139
140

  if (!sftp_get_data(i, *length, data))
    {
      free(data);
      return NULL;
    }

  /* NUL-terminate, for convenience */
  data[*length] = '\0';

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  return data;
}

void
sftp_free_string(UINT8 *s)
{
  free(s);
}

UINT8 *
sftp_get_string_auto(struct sftp_input *i, UINT32 *length)
{
  UINT8 *data;

  assert(i->used_strings < SFTP_MAX_STRINGS);

  data = sftp_get_string(i, length);

  if (!data)
    return NULL;

162
163
164
  /* Remember the string. */
  i->strings[i->used_strings++] = data;
  return data;
165
166
}

167
168
int
sftp_get_eod(struct sftp_input *i)
169
170
171
172
173
174
175
176
{
  return !i->left;
}

/* Input */
struct sftp_input *
sftp_make_input(FILE *f)
{
177
178
179
180
181
182
  struct sftp_input *i = xmalloc(sizeof(struct sftp_input));

  i->f = f;
  i->left = 0;
  i->used_strings = 0;

183
184
185
  return i;
}

186
void
187
188
189
190
191
sftp_input_clear_strings(struct sftp_input *i)
{
  unsigned k;

  for (k = 0; k < i->used_strings; k++)
192
    sftp_free_string(i->strings[k]);
193
194
195
196

  i->used_strings = 0;
}

197
198
199
200
201
202
/* Returns 1 of all was well, 0 on error, and -1 on EOF */
int
sftp_read_packet(struct sftp_input *i)
{
  UINT8 buf[4];
  int done;
203

204
205
  if (i->left)
    return 0;
206

207
208
209
  /* First, deallocate the strings. */
  sftp_input_clear_strings(i);
  
210
211
212
213
214
  done = fread(buf, 1, sizeof(buf), i->f);

  switch (done)
    {
    case 0:
215
216
217
218
219
220
      {
	int err =  feof(i->f) ? -1 : 0;
	clearerr( i->f );
	return err;
      }

221
222
223
224
225
226
227
228
229
230
    case 4:
      i->left = READ_UINT32(buf);
      return 1;
    default:
      return 0;
    }
}

/* Output */

231
static void
232
233
234
235
236
237
sftp_check_output(struct sftp_output *o, UINT32 length)
{
  UINT32 needed = o->i + length;
  if (!o->data || (needed > o->size))
  {
    UINT32 size = 2 * needed + 40;
238
    o->data = xrealloc(o->data, size);
239
240
241
242
243

    o->size = size;
  }
}

244
void
245
246
sftp_put_data(struct sftp_output *o, UINT32 length, const UINT8 *data)
{
247
  sftp_check_output(o, length);
248
249
250
251
252
253
254
255

  memcpy(o->data + o->i, data, length);
  o->i += length;
}

#define PUT_DATA(o, buf) \
(sftp_put_data((o), sizeof((buf)), (buf)))

256
void
257
258
sftp_put_uint8(struct sftp_output *o, UINT8 value)
{
259
  sftp_check_output(o, 1);
260
261
262
263

  o->data[o->i++] = value;
}

264
void
265
266
267
268
269
sftp_put_uint32(struct sftp_output *o, UINT32 value)
{
  UINT8 buf[4];

  WRITE_UINT32(buf, value);
270
  PUT_DATA(o, buf);
271
272
}

273
void
274
sftp_put_string(struct sftp_output *o, UINT32 length, const UINT8 *data)
275
{
276
277
  sftp_put_uint32(o, length);
  sftp_put_data(o, length, data);
278
279
}

280
281
UINT32
sftp_put_reserve_length(struct sftp_output *o)
282
{
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
  UINT32 index;
  sftp_check_output(o, 4);

  index = o->i;
  o->i += 4;

  return index;
}

void
sftp_put_length(struct sftp_output *o,
		UINT32 index,
		UINT32 length)
{
  assert( (index + 4) < o->i);
  WRITE_UINT32(o->data + index, length);
}

void
sftp_put_final_length(struct sftp_output *o,
		      UINT32 index)
{
305
  sftp_put_length(o, index, o->i - index - 4);
306
307
}

308
309
310
311
void
sftp_put_reset(struct sftp_output *o,
	       UINT32 index)
{
312
  assert(index < o->i);
313
314
315
  o->i = index;
}

316
317
318
319
UINT32
sftp_put_printf(struct sftp_output *o, const char *format, ...)
{
  /* Initial buffer space */
320
  int needed;
321
  int length;
322
  
323
324
325
326
327
328
329
330
331
332
  for (needed = 100;; needed *= 2)
    {
      va_list args;
      
      va_start(args, format);

      sftp_check_output(o, needed);
      length = vsnprintf(o->data + o->i, needed, format, args);
      
      va_end(args);
333

334
335
336
      if ( (length >= 0) && (length < needed))
	break;
    }
337
  o->i += length;
338
339
340
341
342
343
344
345
346
347
348
349
  
  return length;
}

/* If SIZE > 0 it is the desired field length, and
 * smaller output is padded with blanks. */
UINT32
sftp_put_strftime(struct sftp_output *o, UINT32 size, const char *format,
		  const struct tm *tm)
{
  /* Initial buffer space */
  size_t needed;
350
  size_t length;
351
352
353
354
355
  
  for (needed = size ? size : 100;; needed *= 2)
    {
      sftp_check_output(o, needed);
      length = strftime(o->data + o->i, needed, format, tm);
356

357
      if ( (length > 0) && (length < needed))
358
359
360
	break;
    }

361
  while (length < size)
362
363
364
365
366
    o->data[o->i + length++] = ' ';

  o->i += length;
  
  return length;
367
368
}

369
370
371
372

/* 64-bit stuff */
#if SIZEOF_OFF_T > 4
#define READ_UINT64(p)				\
373
374
375
376
377
378
379
380
(  (((off_t) (p)[0]) << 56)			\
 | (((off_t) (p)[1]) << 48)			\
 | (((off_t) (p)[2]) << 40)			\
 | (((off_t) (p)[3]) << 32)			\
 | (((off_t) (p)[4]) << 24)			\
 | (((off_t) (p)[5]) << 16)			\
 | (((off_t) (p)[6]) << 8)			\
 |  ((off_t) (p)[7]))
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443


int
sftp_get_uint64(struct sftp_input *i, off_t *value)
{
  UINT8 buf[8];
  if (!GET_DATA(i, buf))
    return 0;

  *value = READ_UINT64(buf);
  return 1;
}

#define WRITE_UINT64(p, i)			\
do {						\
  (p)[0] = ((i) >> 56) & 0xff;			\
  (p)[1] = ((i) >> 48) & 0xff;			\
  (p)[2] = ((i) >> 40) & 0xff;			\
  (p)[3] = ((i) >> 32) & 0xff;			\
  (p)[4] = ((i) >> 24) & 0xff;			\
  (p)[5] = ((i) >> 16) & 0xff;			\
  (p)[6] = ((i) >> 8) & 0xff;			\
  (p)[7] = (i) & 0xff;				\
} while(0)

void
sftp_put_uint64(struct sftp_output *o, off_t value)
{
  UINT8 buf[8];

  WRITE_UINT64(buf, value);
  PUT_DATA(o, buf);
}

#else /* SIZEOF_OFF_T <= 4 */

/* Fail for too large numbers. */
int
sftp_get_uint64(struct sftp_input *i, off_t *value)
{
  UINT32 high;
  UINT32 low;

  if (sftp_get_uint32(i, &high)
      && !high
      && sftp_get_uint32(i, &low))
    {
      *value = low;
      return 1;
    }
  else
    return 0;
}

void
sftp_put_uint64(struct sftp_output *o, off_t value)
{
  sftp_put_uint32(o, 0);
  sftp_put_uint32(o, value);
}

#endif /* SIZEOF_OFF_T <= 4 */

444
445
446
447
448
449
450
451
452
453
/* The first part of the buffer is always
 *
 * uint32 length
 * uint8  msg
 * uint32 id/version
 */

struct sftp_output *
sftp_make_output(FILE *f)
{
454
455
456
457
458
459
460
  struct sftp_output *o = xmalloc(sizeof(struct sftp_output));

  o->f = f;
  o->data = NULL;
  o->size = 0;
  o->i = 0;

461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  return o;
}

void
sftp_set_msg(struct sftp_output *o, UINT8 msg)
{
  o->msg = msg;
}

void
sftp_set_id(struct sftp_output *o, UINT32 id)
{
  o->first = id;
}

int
sftp_write_packet(struct sftp_output *o)
{
  UINT32 length = o->i + 5;
  UINT8 buf[9];

  WRITE_UINT32(buf, length);
  buf[4] = o->msg;
  WRITE_UINT32(buf + 5, o->first);

  if (fwrite(buf, 1, 9, o->f) != 9)
    return 0;
  if (fwrite(o->data, 1, o->i, o->f) != o->i)
    return 0;

  o->i = 0;

  /* FIXME: Flushing after each packet is sub-optimal. */
  if (fflush(o->f))
    return 0;
496

497
498
499
500
  return 1;
}

/* General functions */
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526

void
sftp_clear_attrib(struct sftp_attrib *a)
{
  a->flags = 0;
  a->size = 0;
  a->uid = 0;
  a->gid = 0;
  a->permissions = 0;
  a->atime = 0;
  a->mtime = 0;
};

int
sftp_skip_extension(struct sftp_input *i)
{
  UINT32 length;
  UINT8 *data;
  unsigned j;
  
  /* Skip name and value*/
  for (j = 0; j<2; j++)
    {
      if (!(data = sftp_get_string(i, &length)))
	return 0;
    }
527
528
  sftp_input_clear_strings(i);
  
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
  return 1;
}

int
sftp_get_attrib(struct sftp_input *i, struct sftp_attrib *a)
{
  sftp_clear_attrib(a);
  
  if (!sftp_get_uint32(i, &a->flags))
    return 0;

  if (a->flags & SSH_FILEXFER_ATTR_SIZE)
    {
      if (!sftp_get_uint64(i, &a->size))
	return 0;
    }

  if (a->flags & SSH_FILEXFER_ATTR_UIDGID)
    {
      if (!sftp_get_uint32(i, &a->uid))
	return 0;

      if (!sftp_get_uint32(i, &a->gid))
	return 0;
    }

  if (a->flags & SSH_FILEXFER_ATTR_PERMISSIONS)
    {
      if (!sftp_get_uint32(i, &a->permissions))
	return 0;
    }

  if (a->flags & SSH_FILEXFER_ATTR_ACMODTIME)
    {
      if (!sftp_get_uint32(i, &a->atime))
	return 0;

      if (!sftp_get_uint32(i, &a->mtime))
	return 0;
    }

  if (a->flags & SSH_FILEXFER_ATTR_EXTENDED)
    {
      UINT32 count;
      UINT32 n;

      if (!sftp_get_uint32(i, &count))
	return 0;

      /* Just skip the extensions */
      for (n = 0; n < count; n++)
	if (!sftp_skip_extension(i))
	  return 0;
    }
  return 1;
}

void
sftp_put_attrib(struct sftp_output *o, const struct sftp_attrib *a)
{
589
  assert(!(a->flags & SSH_FILEXFER_ATTR_EXTENDED));
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
  
  sftp_put_uint32(o, a->flags);

  if (a->flags & SSH_FILEXFER_ATTR_SIZE)
    sftp_put_uint64(o, a->size);

  if (a->flags & SSH_FILEXFER_ATTR_UIDGID)
    {
      sftp_put_uint32(o, a->uid);
      sftp_put_uint32(o, a->gid);
    }

  if (a->flags & SSH_FILEXFER_ATTR_PERMISSIONS)
    sftp_put_uint32(o, a->permissions);

  if (a->flags & SSH_FILEXFER_ATTR_ACMODTIME)
    {
      sftp_put_uint32(o, a->atime);
      sftp_put_uint32(o, a->mtime);
    }
}
611
612
613
614
615
616

int
sftp_packet_size(struct sftp_output* out)
{
  return out->i;
}