channel.c 55 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* channel.c
 *
 * $Id$
 */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 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
J.H.M. Dassen's avatar
J.H.M. Dassen committed
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Niels Möller's avatar
Niels Möller committed
23
24
25
26
27
 */

#include "channel.h"

#include "format.h"
28
#include "io.h"
Niels Möller's avatar
Niels Möller committed
29
#include "read_data.h"
Niels Möller's avatar
Niels Möller committed
30
31
32
33
34
#include "ssh.h"
#include "werror.h"
#include "xalloc.h"

#include <assert.h>
35
#include <string.h>
Niels Möller's avatar
Niels Möller committed
36

37
#define GABA_DEFINE
38
#include "channel.h.x"
39
#undef GABA_DEFINE
40

41
42
#include "channel.c.x"

43
44
struct exception *
make_channel_open_exception(UINT32 error_code, const char *msg)
Niels Möller's avatar
Niels Möller committed
45
46
{
  NEW(channel_open_exception, self);
Niels Möller's avatar
Niels Möller committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#define MAX_ERROR 4
  static const char *msgs[MAX_ERROR + 1] = {
    "",
    "Administratively prohibited",
    "Connect failed",
    "Unknown channel type",
    "Resource shortage"
  };

  assert(error_code > 0);
  assert(error_code <= MAX_ERROR);
#undef MAX_ERROR
  
Niels Möller's avatar
Niels Möller committed
61
  self->super.type = EXC_CHANNEL_OPEN;
Niels Möller's avatar
Niels Möller committed
62
  self->super.msg = msg ? msg : msgs[error_code];
Niels Möller's avatar
Niels Möller committed
63
64
65
66
67
  self->error_code = error_code;

  return &self->super;
}

68
/* GABA:
69
70
   (class
     (name connection_service)
71
     (super command)
72
73
74
75
     (vars
       ; Supported global requests 
       (global_requests object alist)

76
       (channel_types object alist) ))
77
78
79
80

       ; Initialize connection (for instance, request channels to be 
       ; opened or services to be forwarded).

81
       ; (start object connection_startup)))
82
83
*/

84

Niels Möller's avatar
Niels Möller committed
85
86
87
88
89
struct lsh_string *format_global_failure(void)
{
  return ssh_format("%c", SSH_MSG_REQUEST_FAILURE);
}

90
91
92
93
94
struct lsh_string *format_global_success(void)
{
  return ssh_format("%c", SSH_MSG_REQUEST_SUCCESS);
}

95
96
struct lsh_string *format_open_confirmation(struct ssh_channel *channel,
					    UINT32 channel_number,
97
					    const char *format, ...)
98
99
100
101
102
103
{
  va_list args;
  UINT32 l1, l2;
  struct lsh_string *packet;

#define CONFIRM_FORMAT "%c%i%i%i%i"
104
105
#define CONFIRM_ARGS \
  SSH_MSG_CHANNEL_OPEN_CONFIRMATION, channel->channel_number, \
106
107
  channel_number, channel->rec_window_size, channel->rec_max_packet
    
108
  debug("format_open_confirmation: rec_window_size = %i,\n"
109
	"                          rec_max_packet = %i,\n",
110
       channel->rec_window_size,
111
       channel->rec_max_packet);
112
113
114
115
116
117
118
119
120
121
122
  l1 = ssh_format_length(CONFIRM_FORMAT, CONFIRM_ARGS);

  va_start(args, format);
  l2 = ssh_vformat_length(format, args);
  va_end(args);

  packet = lsh_string_alloc(l1 + l2);

  ssh_format_write(CONFIRM_FORMAT, l1, packet->data, CONFIRM_ARGS);

  va_start(args, format);
123
  ssh_vformat_write(format, l2, packet->data+l1, args);
124
125
126
127
128
129
130
  va_end(args);

  return packet;
#undef CONFIRM_FORMAT
#undef CONFIRM_ARGS
}

Niels Möller's avatar
Niels Möller committed
131
struct lsh_string *format_open_failure(UINT32 channel, UINT32 reason,
132
				       const char *msg, const char *language)
Niels Möller's avatar
Niels Möller committed
133
134
135
136
137
{
  return ssh_format("%c%i%i%z%z", SSH_MSG_CHANNEL_OPEN_FAILURE,
		    channel, reason, msg, language);
}

138
139
140
141
142
struct lsh_string *format_channel_success(UINT32 channel)
{
  return ssh_format("%c%i", SSH_MSG_CHANNEL_SUCCESS, channel);
}

Niels Möller's avatar
Niels Möller committed
143
144
145
146
147
struct lsh_string *format_channel_failure(UINT32 channel)
{
  return ssh_format("%c%i", SSH_MSG_CHANNEL_FAILURE, channel);
}

148
149
150
151
152
153
154
155
156
157
struct lsh_string *prepare_window_adjust(struct ssh_channel *channel,
					 UINT32 add)
{
  channel->rec_window_size += add;
  
  return ssh_format("%c%i%i",
		    SSH_MSG_CHANNEL_WINDOW_ADJUST,
		    channel->channel_number, add);
}

Niels Möller's avatar
Niels Möller committed
158
159
160
161
162
/* GABA:
   (class
     (name exc_finish_channel_handler)
     (super exception_handler)
     (vars
163
164
       (connection object ssh_connection)
       ;; (table object channel_table)
165
166
       ; Non-zero if the channel has already been deallocated.
       (dead . int)
Niels Möller's avatar
Niels Möller committed
167
168
169
170
       ; Local channel number 
       (channel_number . UINT32)))
*/

171
172
173
static void
do_exc_finish_channel_handler(struct exception_handler *s,
			      const struct exception *e)
Niels Möller's avatar
Niels Möller committed
174
175
176
177
178
{
  CAST(exc_finish_channel_handler, self, s);

  switch (e->type)
    {
Niels Möller's avatar
Niels Möller committed
179
    case EXC_FINISH_PENDING:
180
181
      if (self->dead)
	werror("channel.c: EXC_FINISH_PENDING on dead channel.\n");
Niels Möller's avatar
Niels Möller committed
182

183
      self->connection->table->pending_close = 1;
184
      
185
186
      if (!self->connection->table->next_channel)
	EXCEPTION_RAISE(self->connection->e, &finish_read_exception);
Niels Möller's avatar
Niels Möller committed
187
188
      break;
      
Niels Möller's avatar
Niels Möller committed
189
190
    case EXC_FINISH_CHANNEL:
      /* NOTE: This type of exception must be handled only once.
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
       * However, there is at least one case where it is difficult to
       * ensure that the exception is raised only once.
       *
       * For instance, in do_channel_close, the CHANNEL_EOF callback
       * can decide to call close_channel, which might raise this
       * exception. When control gets back to do_channel_close, and
       * CHANNEL_SENT_CLOSE is true, it raises the exception again.
       *
       * To get this right, we set a flag when the channel is
       * deallocated. */
      if (self->dead)
	debug("EXC_FINISH_CHANNEL on dead channel.\n");
      else
	{
	  struct ssh_channel *channel
206
	    = self->connection->table->channels[self->channel_number];
207
208
209
210
211
212
213
214

	  assert(channel);
	  assert(channel->resources->super.alive);

	  if (channel->close)
	    CHANNEL_CLOSE(channel);
	
	  KILL_RESOURCE_LIST(channel->resources);
Niels Möller's avatar
Niels Möller committed
215
	
216
	  dealloc_channel(self->connection->table, self->channel_number);
217
218
	  self->dead = 1;

219
220
	  if (self->connection->table->pending_close &&
	      !self->connection->table->next_channel)
221
222
	    {
	      /* FIXME: Send a SSH_DISCONNECT_BY_APPLICATION message? */
223
	      EXCEPTION_RAISE(self->connection->e, &finish_read_exception);
224
225
	    }
	}
Niels Möller's avatar
Niels Möller committed
226
227
228
229
230
231
232
      break;
    default:
      EXCEPTION_RAISE(self->super.parent, e);
    }
}

static struct exception_handler *
233
make_exc_finish_channel_handler(struct ssh_connection *connection,
Niels Möller's avatar
Niels Möller committed
234
				UINT32 channel_number,
235
236
				struct exception_handler *e,
				const char *context)
Niels Möller's avatar
Niels Möller committed
237
238
239
240
{
  NEW(exc_finish_channel_handler, self);
  self->super.parent = e;
  self->super.raise = do_exc_finish_channel_handler;
241
  self->super.context = context;
Niels Möller's avatar
Niels Möller committed
242

243
  self->connection = connection;
244
  self->channel_number = channel_number;
245
  self->dead = 0;
246
  
Niels Möller's avatar
Niels Möller committed
247
  return &self->super;
Niels Möller's avatar
Niels Möller committed
248
249
250
}
				

251
/* Channel objects */
Niels Möller's avatar
Niels Möller committed
252
253
254
255
256

#define INITIAL_CHANNELS 32
/* Arbitrary limit */
#define MAX_CHANNELS (1L<<17)

Niels Möller's avatar
Niels Möller committed
257
struct channel_table *make_channel_table(void)
Niels Möller's avatar
Niels Möller committed
258
{
259
  NEW(channel_table, table);
Niels Möller's avatar
Niels Möller committed
260

261
  table->channels = lsh_space_alloc(sizeof(struct ssh_channel *)
262
				      * INITIAL_CHANNELS);
Niels Möller's avatar
Niels Möller committed
263
264
  table->in_use = lsh_space_alloc(INITIAL_CHANNELS);
  
265
266
267
268
  table->allocated_channels = INITIAL_CHANNELS;
  table->next_channel = 0;
  table->used_channels = 0;
  table->max_channels = MAX_CHANNELS;
Niels Möller's avatar
Niels Möller committed
269

Niels Möller's avatar
Niels Möller committed
270
  table->pending_close = 0;
271

272
273
274
  table->global_requests = make_alist(0, -1);
  table->channel_types = make_alist(0, -1);
  
275
276
277
  object_queue_init(&table->local_ports);
  object_queue_init(&table->remote_ports);
  
278
279
  object_queue_init(&table->active_global_requests);
  object_queue_init(&table->pending_global_requests);
Niels Möller's avatar
Niels Möller committed
280
  
281
  return table;
Niels Möller's avatar
Niels Möller committed
282
283
284
};

/* Returns -1 if allocation fails */
285
286
287
/* NOTE: This function returns locally chosen channel numbers, which
 * are always small integers. So there's no problem fitting them in
 * a signed int. */
288
int alloc_channel(struct channel_table *table)
Niels Möller's avatar
Niels Möller committed
289
{
290
  UINT32 i;
291
  
292
  for(i = table->next_channel; i < table->used_channels; i++)
Niels Möller's avatar
Niels Möller committed
293
    {
294
      if (table->in_use[i] == CHANNEL_FREE)
Niels Möller's avatar
Niels Möller committed
295
	{
Niels Möller's avatar
Niels Möller committed
296
	  assert(!table->channels[i]);
297
	  table->in_use[i] = CHANNEL_RESERVED;
298
	  table->next_channel = i+1;
299

300
	  verbose("Allocated local channel number %i\n", i);
Niels Möller's avatar
Niels Möller committed
301
302
303
	  return i;
	}
    }
304
  if (i == table->max_channels)
Niels Möller's avatar
Niels Möller committed
305
    return -1;
306

307
  if (i == table->allocated_channels) 
Niels Möller's avatar
Niels Möller committed
308
    {
309
      int new_size = table->allocated_channels * 2;
Niels Möller's avatar
Niels Möller committed
310
      struct ssh_channel **new_channels;
Niels Möller's avatar
Niels Möller committed
311
      UINT8 *new_in_use;
Niels Möller's avatar
Niels Möller committed
312

Niels Möller's avatar
Niels Möller committed
313
314
315
      new_channels = lsh_space_alloc(sizeof(struct ssh_channel *)
				     * new_size);
      memcpy(new_channels, table->channels,
316
	     sizeof(struct ssh_channel *) * table->used_channels);
Niels Möller's avatar
Niels Möller committed
317
318
319
      lsh_space_free(table->channels);
      table->channels = new_channels;

320
      /* FIXME: Use realloc(). */
Niels Möller's avatar
Niels Möller committed
321
322
323
324
325
      new_in_use = lsh_space_alloc(new_size);
      memcpy(new_in_use, table->in_use, table->used_channels);
      lsh_space_free(table->in_use);
      table->in_use = new_in_use;

326
      table->allocated_channels = new_size;
Niels Möller's avatar
Niels Möller committed
327
328
    }

329
  table->next_channel = table->used_channels = i+1;
Niels Möller's avatar
Niels Möller committed
330

331
  table->in_use[i] = CHANNEL_RESERVED;
332
  verbose("Allocated local channel number %i\n", i);
333

Niels Möller's avatar
Niels Möller committed
334
335
336
  return i;
}

337
void dealloc_channel(struct channel_table *table, int i)
Niels Möller's avatar
Niels Möller committed
338
339
{
  assert(i >= 0);
340
  assert( (unsigned) i < table->used_channels);
341

342
  verbose("Deallocating local channel %i\n", i);
343
  table->channels[i] = NULL;
344
  table->in_use[i] = CHANNEL_FREE;
Niels Möller's avatar
Niels Möller committed
345
  
346
  if ( (unsigned) i < table->next_channel)
347
348
349
    table->next_channel = i;
}

350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
void
use_channel(struct ssh_connection *connection,
	    UINT32 local_channel_number)
{
  struct channel_table *table = connection->table;
  struct ssh_channel *channel = table->channels[local_channel_number];

  assert(channel);
  assert(table->in_use[local_channel_number] == CHANNEL_RESERVED);
  
  table->in_use[local_channel_number] = CHANNEL_IN_USE;
  verbose("Taking channel %i in use, (local %i).\n",
	  channel->channel_number, local_channel_number);
}
	    
365
366
367
void
register_channel(struct ssh_connection *connection,
		 UINT32 local_channel_number,
368
369
		 struct ssh_channel *channel,
		 int take_into_use)
370
{
371
372
  struct channel_table *table = connection->table;
  
373
  assert(table->in_use[local_channel_number] == CHANNEL_RESERVED);
Niels Möller's avatar
Niels Möller committed
374
  assert(!table->channels[local_channel_number]);
375

376
377
378
  verbose("Registering local channel %i.\n",
	  local_channel_number);
  
Niels Möller's avatar
Niels Möller committed
379
  /* FIXME: Is this the right place to install this exception handler? */
380
381
382
383
384
385
386
  channel->e =
    make_exc_finish_channel_handler(connection,
				    local_channel_number,
				    channel->e ? channel->e : connection->e,
				    HANDLER_CONTEXT);

  table->channels[local_channel_number] = channel;
387

388
389
390
  if (take_into_use)
    use_channel(connection, local_channel_number);
  
391
  REMEMBER_RESOURCE(connection->resources, &channel->resources->super);
Niels Möller's avatar
Niels Möller committed
392
393
}

394
395
396
397
398
399
400
401
402
403
struct ssh_channel *
lookup_channel(struct channel_table *table, UINT32 i)
{
  return ( (i < table->used_channels)
	   && (table->in_use[i] == CHANNEL_IN_USE))
    ? table->channels[i] : NULL;
}

struct ssh_channel *
lookup_channel_reserved(struct channel_table *table, UINT32 i)
Niels Möller's avatar
Niels Möller committed
404
{
405
406
  return ( (i < table->used_channels)
	   && (table->in_use[i] == CHANNEL_RESERVED))
407
    ? table->channels[i] : NULL;
Niels Möller's avatar
Niels Möller committed
408
409
}

410

411
412
413
414
415
416
417
418
419
420
/* FIXME: It seems suboptimal to send a window adjust message for *every* write that we do.
 * A better scheme might be as follows:
 *
 * Delay window adjust messages, keeping track of both the locally
 * maintained window size, which is updated after each write, and the
 * size that has been reported to the remote end. When the difference
 * between these two values gets large enough (say, larger than one
 * half or one third of the maximum window size), we send a
 * window_adjust message to sync them. */
static void adjust_rec_window(struct flow_controlled *f, UINT32 written)
421
{
422
423
  CAST_SUBTYPE(ssh_channel, channel, f);

Niels Möller's avatar
Niels Möller committed
424
  A_WRITE(channel->write,
425
	  prepare_window_adjust(channel, written));
426
427
}

428
429
430
void
channel_start_receive(struct ssh_channel *channel,
		      UINT32 initial_window_size)
431
{
432
433
434
435
  if (channel->rec_window_size < initial_window_size)
    A_WRITE(channel->write,
	    prepare_window_adjust
	    (channel, initial_window_size - channel->rec_window_size));
436
437
}

Niels Möller's avatar
Niels Möller committed
438

439
440
441
/* Ugly macros to make it a little simpler to free the input packet at
 * the right time. */

Niels Möller's avatar
Niels Möller committed
442
443
444
445
446
#define RETURN goto foo_finish
#define END(s) do { foo_finish: \
                    lsh_string_free((s)); \
                    return; } while(0)

447

Niels Möller's avatar
Niels Möller committed
448
/* Channel related messages */
449
450
451

/* GABA:
   (class
452
     (name request_status)
453
454
455
456
457
458
459
     (vars
       ; -1 for still active requests,
       ; 0 for failure,
       ; 1 for success
       (status . int)))
*/

460
461
static struct request_status *
make_request_status(void)
462
{
463
  NEW(request_status, self);
464
465
466
467
468
469
470
  self->status = -1;

  return self;
}

/* GABA:
   (class
471
472
     (name global_request_continuation)
     (super command_continuation)
473
     (vars
474
475
       (connection object ssh_connection)
       (active object request_status)))
476
477
*/

478
479
480
static void 
send_global_request_responses(struct ssh_connection *connection, 
			      struct object_queue *q)
481
{
482
483
484
485
486
487
   for (;;)
     {
       CAST(request_status, n, object_queue_peek_head(q));
       if (!n || (n->status < 0))
	 break;
 
488
      object_queue_remove_head(q);
Niels Möller's avatar
Niels Möller committed
489

490
      C_WRITE(connection,
Niels Möller's avatar
Niels Möller committed
491
492
	      (n->status
	       ? format_global_success()
493
	       : format_global_failure()));
494
495
496
    }
}

497
498
499
static void
do_global_request_response(struct command_continuation *s,
			   struct lsh_object *x UNUSED)
500
{
501
502
  CAST(global_request_continuation, self, s);
  struct object_queue *q = &self->connection->table->active_global_requests;
503

504
505
506
507
  assert(self->active->status == -1);
  assert(!object_queue_is_empty(q));
	  
  self->active->status = 1;
508

509
510
  send_global_request_responses(self->connection, q);
}
511

512
513
514
515
516
517
518
519
520
521
static struct command_continuation *
make_global_request_response(struct ssh_connection *connection,
			     struct request_status *active)
{
  NEW(global_request_continuation, self);

  self->super.c = do_global_request_response;
  self->connection = connection;
  self->active = active;
   
522
523
  return &self->super;
}
524
525
526
527
528
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


/* GABA:
   (class
     (name global_request_exception_handler)
     (super exception_handler)
     (vars
       (connection object ssh_connection)
       (active object request_status)))
*/

/* NOTE: We handle *only* EXC_GLOBAL_REQUEST */
static void 
do_global_request_handler(struct exception_handler *c,
			  const struct exception *e)
{
  CAST(global_request_exception_handler, self, c);
  if (e->type == EXC_GLOBAL_REQUEST)
    {
      struct object_queue *q = &self->connection->table->active_global_requests;
      
      assert(self->active->status == -1);
      assert(!object_queue_is_empty(q));

      self->active->status = 0;
  
      send_global_request_responses(self->connection, q);
    }
  else
    EXCEPTION_RAISE(c->parent, e);
}

static struct exception_handler *
make_global_request_exception_handler(struct ssh_connection *connection,
				      struct request_status *active,
				      struct exception_handler *h,
				      const char *context)
Niels Möller's avatar
Niels Möller committed
561
{
562
563
564
565
566
567
568
569
570
  NEW(global_request_exception_handler, self);

  self->super.raise = do_global_request_handler;
  self->super.context = context;
  self->super.parent = h;
  self->active = active;
  self->connection = connection;
  return &self->super;
}
Niels Möller's avatar
Niels Möller committed
571

572
573
574
575
576
static void
do_global_request(struct packet_handler *s UNUSED,
		  struct ssh_connection *connection,
		  struct lsh_string *packet)
{
Niels Möller's avatar
Niels Möller committed
577
  struct simple_buffer buffer;
578
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
579
580
581
582
583
584
585
586
587
588
589
  int name;
  int want_reply;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_GLOBAL_REQUEST)
      && parse_atom(&buffer, &name)
      && parse_boolean(&buffer, &want_reply))
    {
      struct global_request *req;
590
591
      struct command_continuation *c = &discard_continuation;
      struct exception_handler *e = connection->e;
592
      
Niels Möller's avatar
Niels Möller committed
593
      if (!name || !(req = ALIST_GET(connection->table->global_requests,
594
				     name)))
595
	{
Niels Möller's avatar
Niels Möller committed
596
	  lsh_string_free(packet);
597

Niels Möller's avatar
Niels Möller committed
598
599
	  C_WRITE(connection, format_global_failure());
	  return;
Niels Möller's avatar
Niels Möller committed
600
601
602
603
604
	}
      else
	{
	  if (want_reply)
	    {
605
	      struct request_status *a = make_request_status();
Niels Möller's avatar
Niels Möller committed
606
	      
Niels Möller's avatar
Niels Möller committed
607
	      object_queue_add_tail(&connection->table->active_global_requests,
Niels Möller's avatar
Niels Möller committed
608
609
610
				    &a->super);
	      
	      c = make_global_request_response(connection, a);
611
612
613
614
615
616
617
618
619
620
621
	      e = make_global_request_exception_handler(connection, a, e, HANDLER_CONTEXT);
	    }
	  else
	    {
	      /* We should ignore failures. */
	      static struct report_exception_info global_req_ignore =
		STATIC_REPORT_EXCEPTION_INFO(EXC_ALL, EXC_GLOBAL_REQUEST,
					     "Ignored:");
	      
	      e = make_report_exception_handler(&global_req_ignore,
						e, HANDLER_CONTEXT);
Niels Möller's avatar
Niels Möller committed
622
	    }
623
	  GLOBAL_REQUEST(req, connection, name, want_reply, &buffer, c, e);
624
	}
Niels Möller's avatar
Niels Möller committed
625
    }
Niels Möller's avatar
Niels Möller committed
626
627
  else
    {
628
      PROTOCOL_ERROR(connection->e, "Invalid SSH_MSG_GLOBAL_REQUEST message.");
Niels Möller's avatar
Niels Möller committed
629
630
    }
  lsh_string_free(packet);
Niels Möller's avatar
Niels Möller committed
631
632
}

Niels Möller's avatar
Niels Möller committed
633
634
635
636
static void
do_global_request_success(struct packet_handler *s UNUSED,
			  struct ssh_connection *connection,
			  struct lsh_string *packet)
637
638
{
  if (packet->length != 1)
Niels Möller's avatar
Niels Möller committed
639
    {
640
      PROTOCOL_ERROR(connection->e, "Invalid GLOBAL_REQUEST_SUCCESS message.");
Niels Möller's avatar
Niels Möller committed
641
642
      RETURN;
    }
643
644
645

  assert(packet->data[0] == SSH_MSG_REQUEST_SUCCESS);

Niels Möller's avatar
Niels Möller committed
646
  if (object_queue_is_empty(&connection->table->pending_global_requests))
647
648
    {
      werror("do_global_request_success: Unexpected message, ignoring.\n");
Niels Möller's avatar
Niels Möller committed
649
      return;
650
651
    }
  {
652
    CAST_SUBTYPE(command_context, ctx,
Niels Möller's avatar
Niels Möller committed
653
654
		 object_queue_remove_head(&connection->table->pending_global_requests));
    COMMAND_RETURN(ctx->c, connection);
655
  }
Niels Möller's avatar
Niels Möller committed
656
  END(packet);
657
658
}

659
660
661
struct exception global_request_exception =
STATIC_EXCEPTION(EXC_GLOBAL_REQUEST, "Global request failed");

Niels Möller's avatar
Niels Möller committed
662
663
664
665
static void
do_global_request_failure(struct packet_handler *s UNUSED,
			  struct ssh_connection *connection,
			  struct lsh_string *packet)
666
667
{
  if (packet->length != 1)
Niels Möller's avatar
Niels Möller committed
668
    {
669
      PROTOCOL_ERROR(connection->e, "Invalid GLOBAL_REQUEST_FAILURE message.");
Niels Möller's avatar
Niels Möller committed
670
671
      RETURN;
    }
672
673
674

  assert(packet->data[0] == SSH_MSG_REQUEST_FAILURE);

Niels Möller's avatar
Niels Möller committed
675
  if (object_queue_is_empty(&connection->table->pending_global_requests))
676
677
678
    {
      werror("do_global_request_failure: Unexpected message, ignoring.\n");
    }
Niels Möller's avatar
Niels Möller committed
679
680
681
682
683
684
685
  else
    {
      CAST_SUBTYPE(command_context, ctx,
		   object_queue_remove_head(&connection->table->pending_global_requests));
      EXCEPTION_RAISE(ctx->e, &global_request_exception);
    }
  END(packet);
686
687
}

688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
#if 0
static void 
do_connection_sent(struct ssh_connection *connection,
		   UINT32 length UNUSED)
{
  UINT32 i;
  
  for (i = 0; i < connection->table->used_channels; i++)
    {
      if (connection->table->in_use[i] == CHANNEL_IN_USE)
        {
          struct ssh_channel *channel = connection->table->channels[i];
          
          if (! (channel->flags & (CHANNEL_SENT_CLOSE | CHANNEL_SENT_EOF)))
	    {
	      if (channel->send_window_size && channel->send)
		CHANNEL_SEND(channel, connection);
	    }
        }
    }
}
#endif
710

711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/* FIXME: Don't store the channel here, instead have it passed as the
 * argument of the continuation. This might also allow some
 * unification with the handling of global_requests. */

/* GABA:
   (class
     (name channel_request_continuation)
     (super command_continuation)
     (vars
       (connection object ssh_connection)
       (channel object ssh_channel)
       (active object request_status)))
*/

static void
send_channel_request_responses(struct ssh_connection *connection,
			       struct ssh_channel *channel,
			       struct object_queue *q)
{
  for (;;)
    {
      CAST(request_status, n, object_queue_peek_head(q));
      if (!n || (n->status < 0))
	break;

      object_queue_remove_head(q);

      C_WRITE(connection,
	      (n->status
	       ? format_channel_success(channel->channel_number)
	       : format_channel_failure(channel->channel_number)));
    }
}

static void
do_channel_request_response(struct command_continuation *s,
			    struct lsh_object *x UNUSED)
{
  CAST(channel_request_continuation, self, s);
  struct object_queue *q = &self->channel->active_requests;

  assert(self->active->status == -1);
  assert(!object_queue_is_empty(q));
	  
  self->active->status = 1;

  send_channel_request_responses(self->connection, self->channel, q);
}

static struct command_continuation *
make_channel_request_response(struct ssh_connection *connection,
			      struct ssh_channel *channel,
			      struct request_status *active)
{
  NEW(channel_request_continuation, self);

  self->super.c = do_channel_request_response;
  self->connection = connection;
  self->channel = channel;
  self->active = active;

  return &self->super;
}

/* GABA:
   (class
     (name channel_request_exception_handler)
     (super exception_handler)
     (vars
       (connection object ssh_connection)
       (channel object ssh_channel)
       (active object request_status)))
*/

/* NOTE: We handle *only* EXC_CHANNEL_REQUEST */
static void 
do_channel_request_handler(struct exception_handler *c,
			   const struct exception *e)
{
  CAST(channel_request_exception_handler, self, c);
  if (e->type == EXC_CHANNEL_REQUEST)
    {
      struct object_queue *q = &self->channel->active_requests;

      assert(self->active->status == -1);
      assert(!object_queue_is_empty(q));
      
      self->active->status = 0;
      
      send_channel_request_responses(self->connection, self->channel, q);
    }
  else
    EXCEPTION_RAISE(c->parent, e);
}

static struct exception_handler *
make_channel_request_exception_handler(struct ssh_connection *connection,
				       struct ssh_channel *channel,
				       struct request_status *active,
				       struct exception_handler *h,
				       const char *context)
{
  NEW(channel_request_exception_handler, self);

  self->super.raise = do_channel_request_handler;
  self->super.parent = h;
  self->super.context = context;
  self->connection = connection;
  self->channel = channel;
  self->active = active;

  return &self->super;
}

static void
do_channel_request(struct packet_handler *closure UNUSED,
		   struct ssh_connection *connection,
		   struct lsh_string *packet)
{
  struct simple_buffer buffer;
  unsigned msg_number;
  UINT32 channel_number;
  int type;
  int want_reply;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_REQUEST)
      && parse_uint32(&buffer, &channel_number)
      && parse_atom(&buffer, &type)
      && parse_boolean(&buffer, &want_reply))
    {
      struct ssh_channel *channel = lookup_channel(connection->table,
						   channel_number);

      /* NOTE: We can't free packet yet, because it is not yet fully
       * parsed. There may be some more arguments, which are parsed by
       * the CHANNEL_REQUEST method below. */

      if (channel)
	{
	  struct channel_request *req;
	  struct command_continuation *c = &discard_continuation;
	  struct exception_handler *e = channel->e;

	  if (type && channel->request_types 
	      && ( (req = ALIST_GET(channel->request_types, type)) ))
	    {
	      if (want_reply)
		{
		  struct request_status *a = make_request_status();
		  
		  object_queue_add_tail(&channel->active_requests,
					&a->super);
		  
		  c = make_channel_request_response(connection, channel, a);
		  e = make_channel_request_exception_handler(connection, channel, a, e, HANDLER_CONTEXT);
		  
		}
	      else
		{
		  /* We should ignore failures. */
		  static struct report_exception_info channel_req_ignore =
		    STATIC_REPORT_EXCEPTION_INFO(EXC_ALL, EXC_CHANNEL_REQUEST,
						 "Ignored:");
		  
		  e = make_report_exception_handler(&channel_req_ignore,
						    e, HANDLER_CONTEXT);
		}
	      
	      CHANNEL_REQUEST(req, channel, connection, type, want_reply, &buffer, c, e);
	    }
	  else
	    {
	      if (want_reply)
		C_WRITE(connection,
			format_channel_failure(channel->channel_number));
	    }
	}
      else
	{
	  werror("SSH_MSG_CHANNEL_REQUEST on nonexistant channel %i\n",
		 channel_number);
	}
    }
  else
    PROTOCOL_ERROR(connection->e, "Invalid SSH_MSG_CHANNEL_REQUEST message.");
  
  lsh_string_free(packet);
}


Niels Möller's avatar
Niels Möller committed
904
/* GABA:
905
906
907
908
909
   (class
     (name channel_open_continuation)
     (super command_continuation)
     (vars
       (connection object ssh_connection)
Niels Möller's avatar
Niels Möller committed
910
911
912
913
       (local_channel_number . UINT32)
       (remote_channel_number . UINT32)
       (window_size . UINT32)
       (max_packet . UINT32)))
914
*/
915

Niels Möller's avatar
Niels Möller committed
916
917
918
static void
do_channel_open_continue(struct command_continuation *c,
			 struct lsh_object *value)
919
920
{
  CAST(channel_open_continuation, self, c);
Niels Möller's avatar
Niels Möller committed
921
922
923
924
925
926
927
928
929
930
931
932
  CAST_SUBTYPE(ssh_channel, channel, value);

  assert(channel);

  /* FIXME: This copying could just as well be done by the
   * CHANNEL_OPEN handler? Then we can remove the corresponding fields
   * from the closure as well. */
  channel->send_window_size = self->window_size;
  channel->send_max_packet = self->max_packet;
  channel->channel_number = self->remote_channel_number;

  /* FIXME: Is the channel->write field really needed? */
Niels Möller's avatar
Niels Möller committed
933
  channel->write = self->connection->write;
Niels Möller's avatar
Niels Möller committed
934

935
  register_channel(self->connection,
936
937
		   self->local_channel_number, channel,
		   1);
Niels Möller's avatar
Niels Möller committed
938
939
940
941
942

  /* FIXME: Doesn't support sending extra arguments with the
   * confirmation message. */

  C_WRITE(self->connection,
Niels Möller's avatar
Niels Möller committed
943
	  format_open_confirmation(channel, self->local_channel_number, ""));
944
}
Niels Möller's avatar
Niels Möller committed
945

Niels Möller's avatar
Niels Möller committed
946
static struct command_continuation *
Niels Möller's avatar
Niels Möller committed
947
948
949
950
951
952
953
make_channel_open_continuation(struct ssh_connection *connection,
			       UINT32 local_channel_number,
			       UINT32 remote_channel_number,
			       UINT32 window_size,
			       UINT32 max_packet)
{
  NEW(channel_open_continuation, self);
Niels Möller's avatar
Niels Möller committed
954

Niels Möller's avatar
Niels Möller committed
955
956
957
958
959
960
961
  self->super.c = do_channel_open_continue;
  self->connection = connection;
  self->local_channel_number = local_channel_number;
  self->remote_channel_number = remote_channel_number;
  self->window_size = window_size;
  self->max_packet = max_packet;

Niels Möller's avatar
Niels Möller committed
962
  return &self->super;
Niels Möller's avatar
Niels Möller committed
963
964
965
966
967
968
969
970
971
}
			       
/* GABA:
   (class
     (name exc_channel_open_handler)
     (super exception_handler)
     (vars
       (connection object ssh_connection)
       (local_channel_number . UINT32)
Niels Möller's avatar
Niels Möller committed
972
       (remote_channel_number . UINT32)))
Niels Möller's avatar
Niels Möller committed
973
974
975
*/

static void do_exc_channel_open_handler(struct exception_handler *s,
Niels Möller's avatar
Niels Möller committed
976
					const struct exception *e)
Niels Möller's avatar
Niels Möller committed
977
978
979
980
981
982
983
984
{
  CAST(exc_channel_open_handler, self, s);

  switch (e->type)
    {
    case EXC_CHANNEL_OPEN:
      {
	CAST_SUBTYPE(channel_open_exception, exc, e);
Niels Möller's avatar
Niels Möller committed
985
	struct channel_table *table = self->connection->table;
Niels Möller's avatar
Niels Möller committed
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
	
	assert(table->in_use[self->local_channel_number]);
	assert(!table->channels[self->local_channel_number]);

	dealloc_channel(table, self->local_channel_number);
	
        C_WRITE(self->connection,
		format_open_failure(self->remote_channel_number,
				    exc->error_code, e->msg, ""));
	break;
      }
    default:
      EXCEPTION_RAISE(self->super.parent, e);
    }      
}

static struct exception_handler *
make_exc_channel_open_handler(struct ssh_connection *connection,
			      UINT32 local_channel_number,
			      UINT32 remote_channel_number,
1006
1007
			      struct exception_handler *parent,
			      const char *context)
Niels Möller's avatar
Niels Möller committed
1008
{
Niels Möller's avatar
Niels Möller committed
1009
  NEW(exc_channel_open_handler, self);
Niels Möller's avatar
Niels Möller committed
1010
  self->super.parent = parent;
Niels Möller's avatar
Niels Möller committed
1011
  self->super.raise = do_exc_channel_open_handler;
1012
1013
  self->super.context = context;
  
Niels Möller's avatar
Niels Möller committed
1014
  self->connection = connection;
Niels Möller's avatar
Niels Möller committed
1015
1016
1017
1018
1019
1020
1021
1022
1023
  self->local_channel_number = local_channel_number;
  self->remote_channel_number = remote_channel_number;

  return &self->super;
}

static void do_channel_open(struct packet_handler *c UNUSED,
			    struct ssh_connection *connection,
			    struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
1024
1025
{
  struct simple_buffer buffer;
1026
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
1027
  int type;
1028
1029
1030
  UINT32 remote_channel_number;
  UINT32 window_size;
  UINT32 max_packet;
Niels Möller's avatar
Niels Möller committed
1031
1032
1033
1034
1035
1036
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_OPEN)
      && parse_atom(&buffer, &type)
1037
1038
1039
      && parse_uint32(&buffer, &remote_channel_number)
      && parse_uint32(&buffer, &window_size)
      && parse_uint32(&buffer, &max_packet))
Niels Möller's avatar
Niels Möller committed
1040
1041
    {
      struct channel_open *open;
Niels Möller's avatar
Niels Möller committed
1042

Niels Möller's avatar
Niels Möller committed
1043
1044
1045
1046
      /* NOTE: We can't free the packet yet, as the buffer is passed
       * to the CHANNEL_OPEN method later. */

      if (connection->table->pending_close)
Niels Möller's avatar
Niels Möller committed
1047
1048
1049
1050
1051
1052
1053
1054
	{
	  /* We are waiting for channels to close. Don't open any new ones. */

	  C_WRITE(connection,
		  format_open_failure(remote_channel_number,
				      SSH_OPEN_ADMINISTRATIVELY_PROHIBITED,
				      "Waiting for channels to close.", ""));
	}
Niels Möller's avatar
Niels Möller committed
1055
      else if (!type || !(open = ALIST_GET(connection->table->channel_types,
1056
				      type)))
Niels Möller's avatar
Niels Möller committed
1057
1058
1059
1060
1061
1062
1063
1064
	{
	  C_WRITE(connection,
		  format_open_failure(remote_channel_number,
				      SSH_OPEN_UNKNOWN_CHANNEL_TYPE,
				      "Unknown channel type", ""));
	}
      else
	{
Niels Möller's avatar
Niels Möller committed
1065
      	  int local_number = alloc_channel(connection->table);
Niels Möller's avatar
Niels Möller committed
1066
1067
1068
1069
1070
1071
1072
1073

	  if (local_number < 0)
	    C_WRITE(connection,
		    format_open_failure(remote_channel_number,
					SSH_OPEN_RESOURCE_SHORTAGE,
					"Unknown channel type", ""));

	  
Niels Möller's avatar
Niels Möller committed
1074
	  
1075
1076
	  CHANNEL_OPEN(open, connection,
		       type,
1077
		       window_size,
1078
1079
1080
1081
		       /* We don't support larger packets than the
			* default, SSH_MAX_PACKET */
		       MIN(max_packet, SSH_MAX_PACKET),
		       &buffer,
Niels Möller's avatar
Niels Möller committed
1082
1083
1084
1085
1086
1087
1088
1089
		       make_channel_open_continuation(connection,
						      local_number,
						      remote_channel_number,
						      window_size,
						      max_packet),
		       make_exc_channel_open_handler(connection,
						     local_number,
						     remote_channel_number,
1090
1091
						     connection->e,
						     HANDLER_CONTEXT));
Niels Möller's avatar
Niels Möller committed
1092
	}
Niels Möller's avatar
Niels Möller committed
1093
    }
Niels Möller's avatar
Niels Möller committed
1094
  else
1095
    PROTOCOL_ERROR(connection->e, "Invalid SSH_MSG_CHANNEL_OPEN message.");
Niels Möller's avatar
Niels Möller committed
1096

Niels Möller's avatar
Niels Möller committed
1097
  lsh_string_free(packet);
Niels Möller's avatar
Niels Möller committed
1098
1099
}     

Niels Möller's avatar
Niels Möller committed
1100
1101
1102
1103
static void
do_window_adjust(struct packet_handler *closure UNUSED,
		 struct ssh_connection *connection,
		 struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
1104
1105
{
  struct simple_buffer buffer;
1106
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
1107
1108
1109
1110
1111
1112
1113
  UINT32 channel_number;
  UINT32 size;

  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_WINDOW_ADJUST)
1114
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
1115
1116
1117
      && parse_uint32(&buffer, &size)
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
1118
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
1119
1120
1121
1122
						   channel_number);

      lsh_string_free(packet);
      
Niels Möller's avatar
Niels Möller committed
1123
      if (channel
1124
	  && !(channel->flags & CHANNEL_RECEIVED_CLOSE))
Niels Möller's avatar
Niels Möller committed
1125
	{
1126
	  if (! (channel->flags & (CHANNEL_SENT_CLOSE | CHANNEL_SENT_EOF)))
Niels Möller's avatar
Niels Möller committed
1127
1128
	    {
	      channel->send_window_size += size;
1129
1130
1131
1132
1133
	      if (channel->send_window_size && channel->send_adjust)
		{
		  assert(channel->send_window_size);
		  CHANNEL_SEND_ADJUST(channel, size);
		}
Niels Möller's avatar
Niels Möller committed
1134
	    }
Niels Möller's avatar
Niels Möller committed
1135
	}
Niels Möller's avatar
Niels Möller committed
1136
1137
1138
1139
1140
1141
      else
	{
	  /* FIXME: What to do now? Should unknown channel numbers be
	   * ignored silently? */
	  werror("SSH_MSG_CHANNEL_WINDOW_ADJUST on nonexistant or closed "
		 "channel %i\n", channel_number);
1142
	  PROTOCOL_ERROR(connection->e, "Unexpected CHANNEL_WINDOW_ADJUST");
Niels Möller's avatar
Niels Möller committed
1143
1144
1145
1146
1147
	}
    }
  else
    {
      lsh_string_free(packet);
1148
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_WINDOW_ADJUST message.");
Niels Möller's avatar
Niels Möller committed
1149
1150
1151
    }
}

Niels Möller's avatar
Niels Möller committed
1152
1153
1154
1155
static void
do_channel_data(struct packet_handler *closure UNUSED,
		struct ssh_connection *connection,
		struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
1156
1157
{
  struct simple_buffer buffer;
1158
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
1159
1160
1161
1162
1163
1164
1165
  UINT32 channel_number;
  struct lsh_string *data;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_DATA)
1166
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
1167
1168
1169
      && ( (data = parse_string_copy(&buffer)) )
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
1170
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
1171
1172
1173
1174
						   channel_number);

      lsh_string_free(packet);
      
1175
      if (channel && channel->receive
1176
1177
	  && !(channel->flags & (CHANNEL_RECEIVED_EOF
				 | CHANNEL_RECEIVED_CLOSE)))
Niels Möller's avatar
Niels Möller committed
1178
	{
Niels Möller's avatar
Niels Möller committed
1179
	  if (channel->flags & CHANNEL_SENT_CLOSE)
Niels Möller's avatar
Niels Möller committed
1180
	    {
Niels Möller's avatar
Niels Möller committed
1181
	      lsh_string_free(data);
1182
	      werror("Ignoring data on channel which is closing\n");
Niels Möller's avatar
Niels Möller committed
1183
	      return;
Niels Möller's avatar
Niels Möller committed
1184
	    }
Niels Möller's avatar
Niels Möller committed
1185
1186
	  else
	    {
1187
1188
1189
1190
1191
1192
              if (data->length > channel->rec_max_packet)
                {
                  werror("Channel data larger than rec_max_packet. Extra data ignored.\n");
                  data->length = channel->rec_max_packet;
                }

Niels Möller's avatar
Niels Möller committed
1193
1194
1195
	      if (data->length > channel->rec_window_size)
		{
		  /* Truncate data to fit window */
1196
1197
1198
1199
		  werror("Channel data overflow. Extra data ignored.\n");
		  debug("   (data->length=%i, rec_window_size=%i).\n", 
			data->length, channel->rec_window_size);
		  
Niels Möller's avatar
Niels Möller committed
1200
1201
		  data->length = channel->rec_window_size;
		}
1202
1203

	      if (!data->length)
Niels Möller's avatar
Niels Möller committed
1204
1205
1206
1207
1208
		{
		  /* Ignore data packet */
		  lsh_string_free(data);
		  return;
		}
1209
1210
	      channel->rec_window_size -= data->length;

Niels Möller's avatar
Niels Möller committed
1211
	      CHANNEL_RECEIVE(channel, CHANNEL_DATA, data);
Niels Möller's avatar
Niels Möller committed
1212
	    }
Niels Möller's avatar
Niels Möller committed
1213
	}
Niels Möller's avatar
Niels Möller committed
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
      else
	{
	  werror("Data on closed or non-existant channel %i\n",
		 channel_number);
	  lsh_string_free(data);
	}
    }
  else
    {
      lsh_string_free(packet);
1224
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_DATA message.");
Niels Möller's avatar
Niels Möller committed
1225
1226
1227
    }
}

Niels Möller's avatar
Niels Möller committed
1228
1229
1230
1231
static void
do_channel_extended_data(struct packet_handler *closure UNUSED,
			 struct ssh_connection *connection,
			 struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
1232
1233
{
  struct simple_buffer buffer;
1234
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
1235
1236
1237
1238
1239
1240
1241
1242
  UINT32 channel_number;
  UINT32 type;
  struct lsh_string *data;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_EXTENDED_DATA)
1243
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
1244
1245
1246
1247
      && parse_uint32(&buffer, &type)
      && ( (data = parse_string_copy(&buffer)) )
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
1248
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
1249
1250
1251
1252
						   channel_number);

      lsh_string_free(packet);
      
1253
      if (channel && channel->receive
1254
1255
	  && !(channel->flags & (CHANNEL_RECEIVED_EOF
				 | CHANNEL_RECEIVED_CLOSE)))
Niels Möller's avatar
Niels Möller committed
1256
	{
Niels Möller's avatar
Niels Möller committed
1257
	  if (channel->flags & CHANNEL_SENT_CLOSE)
Niels Möller's avatar
Niels Möller committed
1258
	    {
Niels Möller's avatar
Niels Möller committed
1259
	      lsh_string_free(data);
1260
	      werror("Ignoring extended data on channel which is closing\n");
Niels Möller's avatar
Niels Möller committed
1261
	      return;
Niels Möller's avatar
Niels Möller committed
1262
	    }
Niels Möller's avatar
Niels Möller committed
1263
	  else
Niels Möller's avatar
Niels Möller committed
1264
	    {
1265
1266
1267
1268
1269
1270
              if (data->length > channel->rec_max_packet)
                {
                  werror("Channel data larger than rec_max_packet. Extra data ignored.\n");
                  data->length = channel->rec_max_packet;
                }

Niels Möller's avatar
Niels Möller committed
1271
1272
1273
	      if (data->length > channel->rec_window_size)
		{
		  /* Truncate data to fit window */
1274
		  werror("Channel extended data overflow. "
Niels Möller's avatar
Niels Möller committed
1275
			 "Extra data ignored.\n");
1276
1277
		  debug("   (data->length=%i, rec_window_size=%i).\n", 
			data->length, channel->rec_window_size);
Niels Möller's avatar
Niels Möller committed
1278
1279