channel.c 44.3 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"

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

#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
60
  self->super.type = EXC_CHANNEL_OPEN;
Niels Möller's avatar
Niels Möller committed
61
  self->super.msg = msg ? msg : msgs[error_code];
Niels Möller's avatar
Niels Möller committed
62
63
64
65
66
  self->error_code = error_code;

  return &self->super;
}

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

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

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

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

83
/* ;; GABA:
84
85
   (class
     (name global_request_handler)
86
     (super packet_handler)
87
88
89
90
     (vars
       (global_requests object alist)))
*/

91
/* ;; GABA:
92
93
   (class
     (name channel_open_handler)
94
     (super packet_handler)
95
96
97
98
     (vars
       (channel_types object alist)))
*/

Niels Möller's avatar
Niels Möller committed
99
/* ;; GABA:
100
101
102
103
104
105
106
107
108
   (class
     (name channel_open_response)
     (super channel_open_callback)
     (vars
       (remote_channel_number simple UINT32)
       (window_size simple UINT32)
       (max_packet simple UINT32)))
*/

Niels Möller's avatar
Niels Möller committed
109
110
111
112
113
struct lsh_string *format_global_failure(void)
{
  return ssh_format("%c", SSH_MSG_REQUEST_FAILURE);
}

114
115
116
117
118
struct lsh_string *format_global_success(void)
{
  return ssh_format("%c", SSH_MSG_REQUEST_SUCCESS);
}

119
120
struct lsh_string *format_open_confirmation(struct ssh_channel *channel,
					    UINT32 channel_number,
121
					    const char *format, ...)
122
123
124
125
126
127
{
  va_list args;
  UINT32 l1, l2;
  struct lsh_string *packet;

#define CONFIRM_FORMAT "%c%i%i%i%i"
128
129
#define CONFIRM_ARGS \
  SSH_MSG_CHANNEL_OPEN_CONFIRMATION, channel->channel_number, \
130
131
132
133
134
135
136
137
138
139
140
141
142
  channel_number, channel->rec_window_size, channel->rec_max_packet
    
  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);
143
  ssh_vformat_write(format, l2, packet->data+l1, args);
144
145
146
147
148
149
150
  va_end(args);

  return packet;
#undef CONFIRM_FORMAT
#undef CONFIRM_ARGS
}

Niels Möller's avatar
Niels Möller committed
151
struct lsh_string *format_open_failure(UINT32 channel, UINT32 reason,
152
				       const char *msg, const char *language)
Niels Möller's avatar
Niels Möller committed
153
154
155
156
157
{
  return ssh_format("%c%i%i%z%z", SSH_MSG_CHANNEL_OPEN_FAILURE,
		    channel, reason, msg, language);
}

158
159
160
161
162
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
163
164
165
166
167
struct lsh_string *format_channel_failure(UINT32 channel)
{
  return ssh_format("%c%i", SSH_MSG_CHANNEL_FAILURE, channel);
}

168
169
170
171
172
173
174
175
176
177
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* ;; GABA:
   (class
     (name channel_exception)
     (super exception)
     (vars
       (channel object ssh_channel)
       (pending_close . int)))
*/

/* GABA:
   (class
     (name exc_finish_channel_handler)
     (super exception_handler)
     (vars
       (table object channel_table)
       ; Local channel number 
       (channel_number . UINT32)))
*/

static void do_exc_finish_channel_handler(struct exception_handler *s,
Niels Möller's avatar
Niels Möller committed
198
					  const struct exception *e)
Niels Möller's avatar
Niels Möller committed
199
200
201
202
203
{
  CAST(exc_finish_channel_handler, self, s);

  switch (e->type)
    {
Niels Möller's avatar
Niels Möller committed
204
205
206
207
208
209
210
    case EXC_FINISH_PENDING:
      self->table->pending_close = 1;

      if (!self->table->next_channel)
	EXCEPTION_RAISE(self->super.parent, &finish_read_exception);
      break;
      
Niels Möller's avatar
Niels Möller committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
    case EXC_FINISH_CHANNEL:
      /* NOTE: This type of exception must be handled only once.
       * Perhaps we must add a liveness flag in the ssh_channel struct
       * to avoid deallocating dead channels? */
      {
	struct ssh_channel *channel
	  = self->table->channels[self->channel_number];

	assert(channel);
	
	if (channel->close)
	  CHANNEL_CLOSE(channel);
	
	dealloc_channel(self->table, self->channel_number);

	if (self->table->pending_close && !self->table->next_channel)
227
228
229
230
	  {
	    /* FIXME: Send a SSH_DISCONNECT_BY_APPLICATION message? */
	    EXCEPTION_RAISE(self->super.parent, &finish_read_exception);
	  }
Niels Möller's avatar
Niels Möller committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
      }
      break;
    default:
      EXCEPTION_RAISE(self->super.parent, e);
    }
}

static struct exception_handler *
make_exc_finish_channel_handler(struct channel_table *table,
				UINT32 channel_number,
				struct exception_handler *e)
{
  NEW(exc_finish_channel_handler, self);
  self->table = table;
  self->channel_number = channel_number;
  self->super.parent = e;
  self->super.raise = do_exc_finish_channel_handler;
Niels Möller's avatar
Niels Möller committed
248
249

  return &self->super;
Niels Möller's avatar
Niels Möller committed
250
251
252
}
				

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

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

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

263
  table->channels = lsh_space_alloc(sizeof(struct ssh_channel *)
264
				      * INITIAL_CHANNELS);
Niels Möller's avatar
Niels Möller committed
265
266
  table->in_use = lsh_space_alloc(INITIAL_CHANNELS);
  
267
268
269
270
  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
271

Niels Möller's avatar
Niels Möller committed
272
  table->pending_close = 0;
273

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

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

      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;

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

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

330
  table->in_use[i] = 1;
Niels Möller's avatar
Niels Möller committed
331
332
333
  return i;
}

334
void dealloc_channel(struct channel_table *table, int i)
Niels Möller's avatar
Niels Möller committed
335
336
{
  assert(i >= 0);
337
  assert( (unsigned) i < table->used_channels);
Niels Möller's avatar
Niels Möller committed
338
  
339
  table->channels[i] = NULL;
Niels Möller's avatar
Niels Möller committed
340
341
  table->in_use[i] = 0;
  
342
  if ( (unsigned) i < table->next_channel)
343
344
345
    table->next_channel = i;
}

346
347
348
349
void
register_channel(struct ssh_connection *connection,
		 UINT32 local_channel_number,
		 struct ssh_channel *channel)
350
{
351
352
  struct channel_table *table = connection->table;
  
Niels Möller's avatar
Niels Möller committed
353
354
  assert(table->in_use[local_channel_number]);
  assert(!table->channels[local_channel_number]);
Niels Möller's avatar
Niels Möller committed
355
  
Niels Möller's avatar
Niels Möller committed
356
357
358
359
360
  table->channels[local_channel_number] = channel;

  /* FIXME: Is this the right place to install this exception handler? */
  channel->e = make_exc_finish_channel_handler(table,
					       local_channel_number,
361
					       connection->e);
Niels Möller's avatar
Niels Möller committed
362
363
}

364
struct ssh_channel *lookup_channel(struct channel_table *table, UINT32 i)
Niels Möller's avatar
Niels Möller committed
365
{
366
367
  return (i < table->used_channels)
    ? table->channels[i] : NULL;
Niels Möller's avatar
Niels Möller committed
368
369
}

370
371
372
373
374
375
376
377
378
379
/* 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)
380
{
381
382
  CAST_SUBTYPE(ssh_channel, channel, f);

Niels Möller's avatar
Niels Möller committed
383
  A_WRITE(channel->write,
384
	  prepare_window_adjust(channel, written));
385
386
}

Niels Möller's avatar
Niels Möller committed
387
void channel_start_receive(struct ssh_channel *channel)
388
{
Niels Möller's avatar
Niels Möller committed
389
390
  A_WRITE(channel->write,
	  prepare_window_adjust
391
	  (channel, channel->max_window - channel->rec_window_size));
392
393
}

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

395
396
397
/* 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
398
399
400
401
402
#define RETURN goto foo_finish
#define END(s) do { foo_finish: \
                    lsh_string_free((s)); \
                    return; } while(0)

403

Niels Möller's avatar
Niels Möller committed
404
/* Channel related messages */
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423

/* GABA:
   (class
     (name global_request_status)
     (vars
       ; -1 for still active requests,
       ; 0 for failure,
       ; 1 for success
       (status . int)))
*/

static struct global_request_status *make_global_request_status(void)
{
  NEW(global_request_status, self);
  self->status = -1;

  return self;
}

Niels Möller's avatar
Niels Möller committed
424
/* FIXME: Split into a continuation and an exception handler */
425
426
427
428
429
430
431
432
/* GABA:
   (class
     (name global_request_response)
     (super global_request_callback)
     (vars
       (active object global_request_status)))
*/

Niels Möller's avatar
Niels Möller committed
433
static void
434
435
436
437
do_global_request_response(struct global_request_callback *c,
			   int success)
{
  CAST(global_request_response, self, c);
Niels Möller's avatar
Niels Möller committed
438
  struct object_queue *q = &self->super.connection->table->active_global_requests;
439
440
441
442
443
444
445
446
447
448
449

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

  for (;;)
    {
      CAST(global_request_status, n, object_queue_peek_head(q));
      if (!n || (n->status < 0))
Niels Möller's avatar
Niels Möller committed
450
	break;
451
452

      object_queue_remove_head(q);
Niels Möller's avatar
Niels Möller committed
453
454
455

      /* FIXME: Perhaps install some exception handler that cancels
       * the queue as soon as a write failes. */
456
      C_WRITE(self->super.connection,
Niels Möller's avatar
Niels Möller committed
457
458
	      (n->status
	       ? format_global_success()
459
	       : format_global_failure()));
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
    }
}

static struct global_request_callback *
make_global_request_response(struct ssh_connection *connection,
			     struct global_request_status *active)
{
  NEW(global_request_response, self);

  self->super.connection = connection;
  self->super.response = do_global_request_response;

  self->active = active;

  return &self->super;
}
     
Niels Möller's avatar
Niels Möller committed
477
478
479
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
480
{
481
  /* CAST(global_request_handler, closure, c); */
Niels Möller's avatar
Niels Möller committed
482
483

  struct simple_buffer buffer;
484
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
485
486
487
488
489
490
491
492
493
494
495
  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;
496
497
      struct global_request_callback *c = NULL;
      
Niels Möller's avatar
Niels Möller committed
498
      if (!name || !(req = ALIST_GET(connection->table->global_requests,
499
				     name)))
500
	{
Niels Möller's avatar
Niels Möller committed
501
	  lsh_string_free(packet);
502

Niels Möller's avatar
Niels Möller committed
503
504
	  C_WRITE(connection, format_global_failure());
	  return;
Niels Möller's avatar
Niels Möller committed
505
506
507
508
509
510
511
	}
      else
	{
	  if (want_reply)
	    {
	      struct global_request_status *a = make_global_request_status();
	      
Niels Möller's avatar
Niels Möller committed
512
	      object_queue_add_tail(&connection->table->active_global_requests,
Niels Möller's avatar
Niels Möller committed
513
514
515
516
517
				    &a->super);
	      
	      c = make_global_request_response(connection, a);
	    }
	  GLOBAL_REQUEST(req, connection, &buffer, c);
518
	}
Niels Möller's avatar
Niels Möller committed
519
    }
Niels Möller's avatar
Niels Möller committed
520
521
  else
    {
522
      PROTOCOL_ERROR(connection->e, "Invalid SSH_MSG_GLOBAL_REQUEST message.");
Niels Möller's avatar
Niels Möller committed
523
524
    }
  lsh_string_free(packet);
Niels Möller's avatar
Niels Möller committed
525
526
}

Niels Möller's avatar
Niels Möller committed
527
528
529
530
static void
do_global_request_success(struct packet_handler *s UNUSED,
			  struct ssh_connection *connection,
			  struct lsh_string *packet)
531
532
{
  if (packet->length != 1)
Niels Möller's avatar
Niels Möller committed
533
    {
534
      PROTOCOL_ERROR(connection->e, "Invalid GLOBAL_REQUEST_SUCCESS message.");
Niels Möller's avatar
Niels Möller committed
535
536
      RETURN;
    }
537
538
539

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

Niels Möller's avatar
Niels Möller committed
540
  if (object_queue_is_empty(&connection->table->pending_global_requests))
541
542
    {
      werror("do_global_request_success: Unexpected message, ignoring.\n");
Niels Möller's avatar
Niels Möller committed
543
      return;
544
545
    }
  {
546
    CAST_SUBTYPE(command_context, ctx,
Niels Möller's avatar
Niels Möller committed
547
548
		 object_queue_remove_head(&connection->table->pending_global_requests));
    COMMAND_RETURN(ctx->c, connection);
549
  }
Niels Möller's avatar
Niels Möller committed
550
  END(packet);
551
552
}

553
554
555
struct exception global_request_exception =
STATIC_EXCEPTION(EXC_GLOBAL_REQUEST, "Global request failed");

Niels Möller's avatar
Niels Möller committed
556
557
558
559
static void
do_global_request_failure(struct packet_handler *s UNUSED,
			  struct ssh_connection *connection,
			  struct lsh_string *packet)
560
561
{
  if (packet->length != 1)
Niels Möller's avatar
Niels Möller committed
562
    {
563
      PROTOCOL_ERROR(connection->e, "Invalid GLOBAL_REQUEST_FAILURE message.");
Niels Möller's avatar
Niels Möller committed
564
565
      RETURN;
    }
566
567
568

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

Niels Möller's avatar
Niels Möller committed
569
  if (object_queue_is_empty(&connection->table->pending_global_requests))
570
571
572
    {
      werror("do_global_request_failure: Unexpected message, ignoring.\n");
    }
Niels Möller's avatar
Niels Möller committed
573
574
575
576
577
578
579
  else
    {
      CAST_SUBTYPE(command_context, ctx,
		   object_queue_remove_head(&connection->table->pending_global_requests));
      EXCEPTION_RAISE(ctx->e, &global_request_exception);
    }
  END(packet);
580
581
}

582

Niels Möller's avatar
Niels Möller committed
583
/* GABA:
584
585
586
587
588
   (class
     (name channel_open_continuation)
     (super command_continuation)
     (vars
       (connection object ssh_connection)
Niels Möller's avatar
Niels Möller committed
589
590
591
592
       (local_channel_number . UINT32)
       (remote_channel_number . UINT32)
       (window_size . UINT32)
       (max_packet . UINT32)))
593
*/
594

Niels Möller's avatar
Niels Möller committed
595
596
597
static void
do_channel_open_continue(struct command_continuation *c,
			 struct lsh_object *value)
598
599
{
  CAST(channel_open_continuation, self, c);
Niels Möller's avatar
Niels Möller committed
600
601
602
603
604
605
606
607
608
609
610
611
  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
612
  channel->write = self->connection->write;
Niels Möller's avatar
Niels Möller committed
613

614
  register_channel(self->connection,
Niels Möller's avatar
Niels Möller committed
615
		   self->local_channel_number, channel);
Niels Möller's avatar
Niels Möller committed
616
617
618
619
620

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

  C_WRITE(self->connection,
Niels Möller's avatar
Niels Möller committed
621
	  format_open_confirmation(channel, self->local_channel_number, ""));
622
}
Niels Möller's avatar
Niels Möller committed
623

Niels Möller's avatar
Niels Möller committed
624
static struct command_continuation *
Niels Möller's avatar
Niels Möller committed
625
626
627
628
629
630
631
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
632

Niels Möller's avatar
Niels Möller committed
633
634
635
636
637
638
639
  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
640
  return &self->super;
Niels Möller's avatar
Niels Möller committed
641
642
643
644
645
646
647
648
649
}
			       
/* 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
650
       (remote_channel_number . UINT32)))
Niels Möller's avatar
Niels Möller committed
651
652
653
*/

static void do_exc_channel_open_handler(struct exception_handler *s,
Niels Möller's avatar
Niels Möller committed
654
					const struct exception *e)
Niels Möller's avatar
Niels Möller committed
655
656
657
658
659
660
661
662
{
  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
663
	struct channel_table *table = self->connection->table;
Niels Möller's avatar
Niels Möller committed
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
	
	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,
			      struct exception_handler *parent)
{
Niels Möller's avatar
Niels Möller committed
686
  NEW(exc_channel_open_handler, self);
Niels Möller's avatar
Niels Möller committed
687
  self->super.parent = parent;
Niels Möller's avatar
Niels Möller committed
688
689
  self->super.raise = do_exc_channel_open_handler;
  self->connection = connection;
Niels Möller's avatar
Niels Möller committed
690
691
692
693
694
695
696
697
698
  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
699
{
700
  /* CAST(channel_open_handler, closure, c); */
Niels Möller's avatar
Niels Möller committed
701
702

  struct simple_buffer buffer;
703
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
704
  int type;
705
706
707
  UINT32 remote_channel_number;
  UINT32 window_size;
  UINT32 max_packet;
Niels Möller's avatar
Niels Möller committed
708
709
710
711
712
713
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_OPEN)
      && parse_atom(&buffer, &type)
714
715
716
      && 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
717
718
    {
      struct channel_open *open;
Niels Möller's avatar
Niels Möller committed
719

Niels Möller's avatar
Niels Möller committed
720
721
722
723
      /* 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
724
725
726
727
728
729
730
731
	{
	  /* 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
732
      else if (!type || !(open = ALIST_GET(connection->table->channel_types,
733
				      type)))
Niels Möller's avatar
Niels Möller committed
734
735
736
737
738
739
740
741
	{
	  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
742
      	  int local_number = alloc_channel(connection->table);
Niels Möller's avatar
Niels Möller committed
743
744
745
746
747
748
749
750

	  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
751
752
753
754
755
756
757
758
759
760
761
	  
	  CHANNEL_OPEN(open, connection, &buffer,
		       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,
						     connection->e));
Niels Möller's avatar
Niels Möller committed
762
	}
Niels Möller's avatar
Niels Möller committed
763
    }
Niels Möller's avatar
Niels Möller committed
764
  else
765
    PROTOCOL_ERROR(connection->e, "Invalid SSH_MSG_CHANNEL_OPEN message.");
Niels Möller's avatar
Niels Möller committed
766

Niels Möller's avatar
Niels Möller committed
767
  lsh_string_free(packet);
Niels Möller's avatar
Niels Möller committed
768
769
}     

Niels Möller's avatar
Niels Möller committed
770
771
772
773
static void
do_channel_request(struct packet_handler *closure UNUSED,
		   struct ssh_connection *connection,
		   struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
774
775
{
  struct simple_buffer buffer;
776
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
777
778
779
  UINT32 channel_number;
  int type;
  int want_reply;
780
  
Niels Möller's avatar
Niels Möller committed
781
782
783
784
785
786
787
788
  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))
    {
Niels Möller's avatar
Niels Möller committed
789
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
790
791
						   channel_number);

792
793
794
795
      /* 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. */

Niels Möller's avatar
Niels Möller committed
796
797
798
799
      if (channel)
	{
	  struct channel_request *req;

800
801
	  if (type && channel->request_types 
	      && ( (req = ALIST_GET(channel->request_types, type)) ))
Niels Möller's avatar
Niels Möller committed
802
	    CHANNEL_REQUEST(req, channel, connection, want_reply, &buffer);
803
	  else
Niels Möller's avatar
Niels Möller committed
804
805
	    {
	      if (want_reply)
Niels Möller's avatar
Niels Möller committed
806
		C_WRITE(connection,
Niels Möller's avatar
Niels Möller committed
807
808
809
810
811
812
813
			format_channel_failure(channel->channel_number));
	    }
	}
      else
	{
	  werror("SSH_MSG_CHANNEL_REQUEST on nonexistant channel %i\n",
		 channel_number);
Niels Möller's avatar
Niels Möller committed
814
	}
815
    }
Niels Möller's avatar
Niels Möller committed
816
  else
817
    PROTOCOL_ERROR(connection->e, "Invalid SSH_MSG_CHANNEL_REQUEST message.");
Niels Möller's avatar
Niels Möller committed
818
  
Niels Möller's avatar
Niels Möller committed
819
  lsh_string_free(packet);
Niels Möller's avatar
Niels Möller committed
820
821
}
      
Niels Möller's avatar
Niels Möller committed
822
823
824
825
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
826
827
{
  struct simple_buffer buffer;
828
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
829
830
831
832
833
834
835
  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)
836
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
837
838
839
      && parse_uint32(&buffer, &size)
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
840
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
841
842
843
844
						   channel_number);

      lsh_string_free(packet);
      
Niels Möller's avatar
Niels Möller committed
845
      if (channel
846
847
	  && !(channel->flags & (CHANNEL_RECEIVED_EOF
				 | CHANNEL_RECEIVED_CLOSE)))
Niels Möller's avatar
Niels Möller committed
848
	{
Niels Möller's avatar
Niels Möller committed
849
850
851
852
	  if (! (channel->flags & CHANNEL_SENT_CLOSE))
	    {
	      channel->send_window_size += size;
	      if (channel->send_window_size && channel->send)
853
		CHANNEL_SEND(channel, connection);
Niels Möller's avatar
Niels Möller committed
854
	    }
Niels Möller's avatar
Niels Möller committed
855
	}
Niels Möller's avatar
Niels Möller committed
856
857
858
859
860
861
      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);
862
	  PROTOCOL_ERROR(connection->e, "Unexpected CHANNEL_WINDOW_ADJUST");
Niels Möller's avatar
Niels Möller committed
863
864
865
866
867
	}
    }
  else
    {
      lsh_string_free(packet);
868
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_WINDOW_ADJUST message.");
Niels Möller's avatar
Niels Möller committed
869
870
871
    }
}

Niels Möller's avatar
Niels Möller committed
872
873
874
875
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
876
877
{
  struct simple_buffer buffer;
878
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
879
880
881
882
883
884
885
  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)
886
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
887
888
889
      && ( (data = parse_string_copy(&buffer)) )
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
890
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
891
892
893
894
						   channel_number);

      lsh_string_free(packet);
      
895
      if (channel && channel->receive
896
897
	  && !(channel->flags & (CHANNEL_RECEIVED_EOF
				 | CHANNEL_RECEIVED_CLOSE)))
Niels Möller's avatar
Niels Möller committed
898
	{
Niels Möller's avatar
Niels Möller committed
899
	  if (channel->flags & CHANNEL_SENT_CLOSE)
Niels Möller's avatar
Niels Möller committed
900
	    {
Niels Möller's avatar
Niels Möller committed
901
	      lsh_string_free(data);
902
	      werror("Ignoring data on channel which is closing\n");
Niels Möller's avatar
Niels Möller committed
903
	      return;
Niels Möller's avatar
Niels Möller committed
904
	    }
Niels Möller's avatar
Niels Möller committed
905
906
907
908
909
	  else
	    {
	      if (data->length > channel->rec_window_size)
		{
		  /* Truncate data to fit window */
910
		  werror("Channel data overflow. Extra data ignored.\n"); 
Niels Möller's avatar
Niels Möller committed
911
912
		  data->length = channel->rec_window_size;
		}
913
914

	      if (!data->length)
Niels Möller's avatar
Niels Möller committed
915
916
917
918
919
		{
		  /* Ignore data packet */
		  lsh_string_free(data);
		  return;
		}
920
921
	      channel->rec_window_size -= data->length;

Niels Möller's avatar
Niels Möller committed
922
	      CHANNEL_RECEIVE(channel, CHANNEL_DATA, data);
Niels Möller's avatar
Niels Möller committed
923
	    }
Niels Möller's avatar
Niels Möller committed
924
	}
Niels Möller's avatar
Niels Möller committed
925
926
927
928
929
930
931
932
933
934
      else
	{
	  werror("Data on closed or non-existant channel %i\n",
		 channel_number);
	  lsh_string_free(data);
	}
    }
  else
    {
      lsh_string_free(packet);
935
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_DATA message.");
Niels Möller's avatar
Niels Möller committed
936
937
938
    }
}

Niels Möller's avatar
Niels Möller committed
939
940
941
942
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
943
944
{
  struct simple_buffer buffer;
945
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
946
947
948
949
950
951
952
953
  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)
954
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
955
956
957
958
      && parse_uint32(&buffer, &type)
      && ( (data = parse_string_copy(&buffer)) )
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
959
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
960
961
962
963
						   channel_number);

      lsh_string_free(packet);
      
964
      if (channel && channel->receive
965
966
	  && !(channel->flags & (CHANNEL_RECEIVED_EOF
				 | CHANNEL_RECEIVED_CLOSE)))
Niels Möller's avatar
Niels Möller committed
967
	{
Niels Möller's avatar
Niels Möller committed
968
	  if (channel->flags & CHANNEL_SENT_CLOSE)
Niels Möller's avatar
Niels Möller committed
969
	    {
Niels Möller's avatar
Niels Möller committed
970
	      lsh_string_free(data);
971
	      werror("Ignoring extended data on channel which is closing\n");
Niels Möller's avatar
Niels Möller committed
972
	      return;
Niels Möller's avatar
Niels Möller committed
973
	    }
Niels Möller's avatar
Niels Möller committed
974
	  else
Niels Möller's avatar
Niels Möller committed
975
	    {
Niels Möller's avatar
Niels Möller committed
976
977
978
	      if (data->length > channel->rec_window_size)
		{
		  /* Truncate data to fit window */
979
		  werror("Channel extended data overflow. "
Niels Möller's avatar
Niels Möller committed
980
981
982
983
			 "Extra data ignored.\n");
		  data->length = channel->rec_window_size;
		}
	      
Niels Möller's avatar
Niels Möller committed
984
985
986
987
988
989
	      if (!data->length)
		{
		  /* Ignore data packet */
		  lsh_string_free(data);
		  return;
		}
990

Niels Möller's avatar
Niels Möller committed
991
	      channel->rec_window_size -= data->length;
992

Niels Möller's avatar
Niels Möller committed
993
994
995
	      switch(type)
		{
		case SSH_EXTENDED_DATA_STDERR:
Niels Möller's avatar
Niels Möller committed
996
997
		  CHANNEL_RECEIVE(channel, CHANNEL_STDERR_DATA, data);
		  break;
Niels Möller's avatar
Niels Möller committed
998
		default:
999
		  werror("Unknown type %i of extended data.\n",
Niels Möller's avatar
Niels Möller committed
1000
1001
1002
			 type);
		  lsh_string_free(data);
		}
Niels Möller's avatar
Niels Möller committed
1003
1004
	    }
	}
Niels Möller's avatar
Niels Möller committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
      else
	{
	  werror("Extended data on closed or non-existant channel %i\n",
		 channel_number);
	  lsh_string_free(data);
	}
    }
  else
    {
      lsh_string_free(packet);
1015
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_EXTENDED_DATA message.");
Niels Möller's avatar
Niels Möller committed
1016
1017
1018
    }
}

Niels Möller's avatar
Niels Möller committed
1019
1020
1021
1022
static void
do_channel_eof(struct packet_handler *closure UNUSED,
	       struct ssh_connection *connection,
	       struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
1023
1024
{
  struct simple_buffer buffer;
1025
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
1026
1027
1028
1029
1030
1031
  UINT32 channel_number;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_EOF)
1032
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
1033
1034
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
1035
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
1036
1037
1038
1039
						   channel_number);

      lsh_string_free(packet);

Niels Möller's avatar
Niels Möller committed
1040
1041
      if (channel)
	{
1042
	  if (channel->flags & (CHANNEL_RECEIVED_EOF | CHANNEL_RECEIVED_CLOSE))
Niels Möller's avatar
Niels Möller committed
1043
	    {
1044
	      werror("Receiving EOF on channel on closed channel.\n");
1045
1046
	      PROTOCOL_ERROR(connection->e,
			     "Received EOF on channel on closed channel.");
Niels Möller's avatar
Niels Möller committed
1047
	    }
1048
	  else
Niels Möller's avatar
Niels Möller committed
1049
	    {
Niels Möller's avatar
Niels Möller committed
1050
	      channel->flags |= CHANNEL_RECEIVED_EOF;
1051
	      
Niels Möller's avatar
Niels Möller committed
1052
1053
1054
1055
1056
1057
1058
	      if (channel->eof)
		CHANNEL_EOF(channel);
	      else
		/* FIXME: What is a reasonable default behaviour?
		 * Closing the channel may be the right thing to do. */
		if (! (channel->flags & CHANNEL_SENT_CLOSE))
		  channel_close(channel);
Niels Möller's avatar
Niels Möller committed
1059
1060
	    }
	}
Niels Möller's avatar
Niels Möller committed
1061
1062
1063
1064
      else
	{
	  werror("EOF on non-existant channel %i\n",
		 channel_number);
1065
	  PROTOCOL_ERROR(connection->e, "EOF on non-existant channel");
Niels Möller's avatar
Niels Möller committed
1066
1067
1068
1069
1070
	}
    }
  else
    {
      lsh_string_free(packet);
1071
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_EOF message");
Niels Möller's avatar
Niels Möller committed
1072
1073
1074
    }
}

Niels Möller's avatar
Niels Möller committed
1075
1076
1077
1078
static void
do_channel_close(struct packet_handler *closure UNUSED,
		 struct ssh_connection *connection,
		 struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
1079
1080
{
  struct simple_buffer buffer;
1081
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
1082
1083
1084
1085
1086
1087
  UINT32 channel_number;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_CLOSE)
1088
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
1089
1090
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
1091
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
1092
1093
1094
1095
						   channel_number);

      lsh_string_free(packet);
      
Niels Möller's avatar
Niels Möller committed
1096
1097
      if (channel)
	{
1098
	  if (channel->flags & CHANNEL_RECEIVED_CLOSE)
Niels Möller's avatar
Niels Möller committed
1099
	    {
1100
	      werror("Receiving multiple CLOSE on channel.\n");
1101
	      PROTOCOL_ERROR(connection->e, "Receiving multiple CLOSE on channel.");
Niels Möller's avatar
Niels Möller committed
1102
	    }
Niels Möller's avatar
Niels Möller committed
1103
	  else
Niels Möller's avatar
Niels Möller committed
1104
	    {
Niels Möller's avatar
Niels Möller committed
1105
	      channel->flags |= CHANNEL_RECEIVED_CLOSE;
Niels Möller's avatar
Niels Möller committed
1106
	  
Niels Möller's avatar
Niels Möller committed
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
	      if (! (channel->flags & (CHANNEL_RECEIVED_EOF | CHANNEL_SENT_EOF
				       | CHANNEL_SENT_CLOSE)))
		{
		  werror("Unexpected channel CLOSE.\n");
		}

	      if (! (channel->flags & (CHANNEL_RECEIVED_EOF))
		  && channel->eof)
		CHANNEL_EOF(channel);

	      if (channel->flags & (CHANNEL_SENT_CLOSE))
		{
		  static const struct exception finish_exception
		    = STATIC_EXCEPTION(EXC_FINISH_CHANNEL, "Received CLOSE message.");
	      
		  EXCEPTION_RAISE(channel->e,
				  &finish_exception);
		}
	      else
		channel_close(channel);
	    }
	}
      else
	{
	  werror("CLOSE on non-existant channel %i\n",
		 channel_number);
1133
	  PROTOCOL_ERROR(connection->e, "CLOSE on non-existant channel");
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
  else
    {
      lsh_string_free(packet);
1139
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_CLOSE message");
Niels Möller's avatar
Niels Möller committed
1140
    }
Niels Möller's avatar
Niels Möller committed
1141
1142
}

Niels Möller's avatar
Niels Möller committed
1143
1144
1145
1146
static void
do_channel_open_confirm(struct packet_handler *closure UNUSED,
			struct ssh_connection *connection,
			struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
1147
1148
{
  struct simple_buffer buffer;
1149
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
  UINT32 local_channel_number;
  UINT32 remote_channel_number;  
  UINT32 window_size;
  UINT32 max_packet;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_OPEN_CONFIRMATION)
      && parse_uint32(&buffer, &local_channel_number)
      && parse_uint32(&buffer, &remote_channel_number)
      && parse_uint32(&buffer, &window_size)
      && parse_uint32(&buffer, &max_packet)
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
1165
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
1166
1167
1168
1169
						   local_channel_number);

      lsh_string_free(packet);

1170
      if (channel && channel->open_continuation)
Niels Möller's avatar
Niels Möller committed
1171
	{
1172
1173
1174
	  struct command_continuation *c = channel->open_continuation;
	  channel->open_continuation = NULL;
	  
Niels Möller's avatar
Niels Möller committed
1175
1176
1177
1178
	  channel->channel_number = remote_channel_number;
	  channel->send_window_size = window_size;
	  channel->send_max_packet = max_packet;

Niels Möller's avatar
Niels Möller committed
1179
1180
1181
1182
1183
1184
	  COMMAND_RETURN(c, channel);
	}
      else
	{
	  werror("Unexpected SSH_MSG_CHANNEL_OPEN_CONFIRMATION on channel %i\n",
		 local_channel_number);
1185
	  PROTOCOL_ERROR(connection->e, "Unexpected CHANNEL_OPEN_CONFIRMATION.");
Niels Möller's avatar
Niels Möller committed
1186
1187
	}
    }
Niels Möller's avatar
Niels Möller committed
1188
1189
1190
  else
    {
      lsh_string_free(packet);
1191
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_OPEN_CONFIRMATION message.");
Niels Möller's avatar
Niels Möller committed
1192
    }
Niels Möller's avatar
Niels Möller committed
1193
1194
}

Niels Möller's avatar
Niels Möller committed
1195
1196
1197
1198
static void
do_channel_open_failure(struct packet_handler *closure UNUSED,
			struct ssh_connection *connection,
			struct lsh_string *packet)