channel.c 22.9 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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* 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
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "channel.h"

#include "format.h"
#include "service.h"
#include "ssh.h"
#include "werror.h"
#include "xalloc.h"

#include <assert.h>

struct connection_service
{
  struct ssh_service super;

  /* Supported global requests */
  struct alist *global_requests;
  struct alist *channel_types;
42
43
44
45

  /* Initialize connection (for instance, request channels to be opened
   * or services to be forwarded. */
  struct connection_startup *start;
Niels Möller's avatar
Niels Möller committed
46
47
};

48
struct channel_handler
Niels Möller's avatar
Niels Möller committed
49
50
51
{
  struct packet_handler super;
  
52
  struct channel_table *table;
Niels Möller's avatar
Niels Möller committed
53
54
55
56
};

struct global_request_handler
{
57
  struct channel_handler super;
Niels Möller's avatar
Niels Möller committed
58
59
60
61
62
63
  
  struct alist *global_requests;
};

struct channel_open_handler
{
64
  struct channel_handler super;
Niels Möller's avatar
Niels Möller committed
65
66
67
68
69
70
71

  struct alist *channel_types;
};

#if 0
struct channel_request_handler
{
72
  struct channel_handler *super;
Niels Möller's avatar
Niels Möller committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

  struct alist *request_types;
};
#endif

struct lsh_string *format_global_failure(void)
{
  return ssh_format("%c", SSH_MSG_REQUEST_FAILURE);
}

struct lsh_string *format_open_failure(UINT32 channel, UINT32 reason,
				       char *msg, char *language)
{
  return ssh_format("%c%i%i%z%z", SSH_MSG_CHANNEL_OPEN_FAILURE,
		    channel, reason, msg, language);
}

struct lsh_string *format_channel_failure(UINT32 channel)
{
  return ssh_format("%c%i", SSH_MSG_CHANNEL_FAILURE, channel);
}

95
96
97
98
99
100
101
102
103
104
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);
}

105
/* Channel objects */
Niels Möller's avatar
Niels Möller committed
106
107
108
109
110

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

Niels Möller's avatar
Niels Möller committed
111
struct channel_table *make_channel_table(void)
Niels Möller's avatar
Niels Möller committed
112
{
113
  struct channel_table *table;
Niels Möller's avatar
Niels Möller committed
114

115
116
  NEW(table);
  table->channels = lsh_space_alloc(sizeof(struct ssh_channel *)
117
				      * INITIAL_CHANNELS);
118
119
120
121
  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
122

123
  return table;
Niels Möller's avatar
Niels Möller committed
124
125
126
};

/* Returns -1 if allocation fails */
127
int alloc_channel(struct channel_table *table)
Niels Möller's avatar
Niels Möller committed
128
129
{
  int i;
130
  for(i = table->next_channel; i < table->used_channels; i++)
Niels Möller's avatar
Niels Möller committed
131
    {
132
      if (!table->channels[i])
Niels Möller's avatar
Niels Möller committed
133
	{
134
	  table->next_channel = i+1;
Niels Möller's avatar
Niels Möller committed
135
136
137
	  return i;
	}
    }
138
  if (i == table->max_channels)
Niels Möller's avatar
Niels Möller committed
139
    return -1;
140
  if (i == table->allocated_channels) 
Niels Möller's avatar
Niels Möller committed
141
    {
142
      int new_size = table->allocated_channels * 2;
Niels Möller's avatar
Niels Möller committed
143
      struct ssh_channel **new
144
	= lsh_space_alloc(sizeof(struct ssh_channel *) * new_size);
Niels Möller's avatar
Niels Möller committed
145

146
147
      memcpy(new, table->channels,
	     sizeof(struct ssh_channel *) * table->used_channels);
Niels Möller's avatar
Niels Möller committed
148
      
149
150
      table->channels = new;
      table->allocated_channels = new_size;
Niels Möller's avatar
Niels Möller committed
151
152
    }

153
  table->next_channel = table->used_channels = i+1;
Niels Möller's avatar
Niels Möller committed
154
155
156
157

  return i;
}

158
void dealloc_channel(struct channel_table *table, int i)
Niels Möller's avatar
Niels Möller committed
159
160
{
  assert(i >= 0);
161
  assert(i < table->used_channels);
Niels Möller's avatar
Niels Möller committed
162
  
163
164
165
166
167
168
169
170
171
172
173
174
  table->channels[i] = NULL;

  if (i < table->next_channel)
    table->next_channel = i;
}

int register_channel(struct channel_table *table, struct ssh_channel *channel)
{
  int n = alloc_channel(table);

  if (n >= 0)
    table->channels[n] = channel;
Niels Möller's avatar
Niels Möller committed
175

176
  return n;
Niels Möller's avatar
Niels Möller committed
177
178
}

179
struct ssh_channel *lookup_channel(struct channel_table *table, UINT32 i)
Niels Möller's avatar
Niels Möller committed
180
{
181
182
  return (i < table->used_channels)
    ? table->channels[i] : NULL;
Niels Möller's avatar
Niels Möller committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
}

/* Channel related messages */

static int do_global_request(struct packet_handler *c,
			     struct ssh_connection *connection,
			     struct lsh_string *packet)
{
  struct global_request_handler *closure = (struct global_request_handler *) c;

  struct simple_buffer buffer;
  int msg_number;
  int name;
  int want_reply;
  
  MDEBUG(closure);

  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;

      lsh_string_free(packet);
      
      if (!name || !(req = ALIST_GET(closure->global_requests, name)))
	return A_WRITE(connection->write,
		       format_global_failure());

      return GLOBAL_REQUEST(req, want_reply, &buffer);
    }
  lsh_string_free(packet);

  return LSH_FAIL | LSH_DIE;
}

static int do_channel_open(struct packet_handler *c,
			   struct ssh_connection *connection,
			   struct lsh_string *packet)
{
  struct channel_open_handler *closure = (struct channel_open_handler *) c;

  struct simple_buffer buffer;
  int msg_number;
  int type;
  UINT32 channel_number;
  UINT32 rec_window_size;
  UINT32 rec_max_packet;
  
  MDEBUG(closure);

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

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_OPEN)
      && parse_atom(&buffer, &type)
      && parse_uint32(&buffer, &rec_window_size)
      && parse_uint32(&buffer, &rec_max_packet))
    {
      struct channel_open *open;

      lsh_string_free(packet);
      
      if (!type || !(open = ALIST_GET(closure->channel_types, type)))
	return A_WRITE(connection->write,
		       format_open_failure(channel_number,
					   SSH_OPEN_UNKNOWN_CHANNEL_TYPE,
					   "Unknown channel type", ""));
      return CHANNEL_OPEN(open, channel_number, rec_window_size,
			  rec_max_packet, &buffer);
    }
  lsh_string_free(packet);

  return LSH_FAIL | LSH_DIE;
}     

static int do_channel_request(struct packet_handler *c,
			      struct ssh_connection *connection,
			      struct lsh_string *packet)
{
266
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

  struct simple_buffer buffer;
  int msg_number;
  UINT32 channel_number;
  int type;
  int want_reply;

  MDEBUG(closure);

  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))
    {
284
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
						   channel_number);

      lsh_string_free(packet);
      
      if (channel)
	{
	  struct channel_request *req;

	  if (!type || ! ( (req = ALIST_GET(channel->request_types, type)) ))
	    return A_WRITE(connection->write,
			   format_channel_failure(channel->channel_number));

	  return CHANNEL_REQUEST(req, channel, want_reply, &buffer);
	}
      werror("SSH_MSG_CHANNEL_REQUEST on nonexistant channel %d\n",
	     channel_number);
      return LSH_FAIL | LSH_DIE;
    }
  lsh_string_free(packet);

  return LSH_FAIL | LSH_DIE;
}
      
static int do_window_adjust(struct packet_handler *c,
			    struct ssh_connection *connection,
			   struct lsh_string *packet)
{
312
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327

  struct simple_buffer buffer;
  int msg_number;
  UINT32 channel_number;
  UINT32 size;

  MDEBUG(closure);

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

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_WINDOW_ADJUST)
      && parse_uint32(&buffer, &size)
      && parse_eod(&buffer))
    {
328
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
						   channel_number);

      lsh_string_free(packet);
      
      if (channel)
	{
	  channel->send_window_size += size;
	  return LSH_OK | LSH_GOON;
	}
      /* FIXME: What to do now? Should unknown channel numbers be
       * ignored silently? */
      werror("SSH_MSG_CHANNEL_WINDOW_ADJUST on nonexistant channel %d\n",
	     channel_number);
      return LSH_FAIL | LSH_DIE;
    }
  lsh_string_free(packet);

  return LSH_FAIL | LSH_DIE;
}

static int do_channel_data(struct packet_handler *c,
			   struct ssh_connection *connection,
			   struct lsh_string *packet)
{
353
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368

  struct simple_buffer buffer;
  int msg_number;
  UINT32 channel_number;
  struct lsh_string *data;
  
  MDEBUG(closure);

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

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_DATA)
      && ( (data = parse_string_copy(&buffer)) )
      && parse_eod(&buffer))
    {
369
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
						   channel_number);

      lsh_string_free(packet);
      
      if (channel && channel->recieve)
	{
	  if (data->length > channel->rec_window_size)
	    {
	      /* Truncate data to fit window */
	      werror("Channel data overflow. Extra data ignored.\n"); 
	      data->length = channel->rec_window_size;
	    }
	  channel->rec_window_size -= data->length; 
	  return CHANNEL_RECIEVE(channel, connection->write,
				 CHANNEL_DATA, data);
	}
	  
      werror("Data on closed or non-existant channel %d\n",
	     channel_number);
      lsh_string_free(data);
      return LSH_FAIL | LSH_DIE;
    }
  lsh_string_free(packet);
  
  return LSH_FAIL | LSH_DIE;
}

static int do_channel_extended_data(struct packet_handler *c,
				    struct ssh_connection *connection,
				    struct lsh_string *packet)
{
401
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

  struct simple_buffer buffer;
  int msg_number;
  UINT32 channel_number;
  UINT32 type;
  struct lsh_string *data;
  
  MDEBUG(closure);

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

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_EXTENDED_DATA)
      && parse_uint32(&buffer, &type)
      && ( (data = parse_string_copy(&buffer)) )
      && parse_eod(&buffer))
    {
419
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
						   channel_number);

      lsh_string_free(packet);
      
      if (channel && channel->recieve)
	{
	  if (data->length > channel->rec_window_size)
	    {
	      /* Truncate data to fit window */
	      werror("Channel extended data overflow. "
		     "Extra data ignored.\n");
	      data->length = channel->rec_window_size;
	    }
	  channel->rec_window_size -= data->length;
	  switch(type)
	    {
	    case SSH_EXTENDED_DATA_STDERR:
	      return CHANNEL_RECIEVE(channel, connection->write,
				     CHANNEL_DATA, data);
	    default:
	      werror("Unknown type %d of extended data.\n",
		     type);
	      lsh_string_free(data);
	      return LSH_FAIL | LSH_DIE;
	    }
	}
      werror("Extended data on closed or non-existant channel %d\n",
	     channel_number);
      lsh_string_free(data);
      return LSH_FAIL | LSH_DIE;
    }
  lsh_string_free(packet);
  
  return LSH_FAIL | LSH_DIE;
}

static int do_channel_eof(struct packet_handler *c,
				    struct ssh_connection *connection,
				    struct lsh_string *packet)
{
460
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
461
462
463
464
465
466
467
468
469
470
471
472
473

  struct simple_buffer buffer;
  int msg_number;
  UINT32 channel_number;
  
  MDEBUG(closure);

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

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_EOF)
      && parse_eod(&buffer))
    {
474
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
						   channel_number);

      lsh_string_free(packet);

      if (channel && channel->eof)
	return CHANNEL_EOF(channel, connection->write);
    }
  lsh_string_free(packet);
  return LSH_FAIL | LSH_DIE;
}

static int do_channel_close(struct packet_handler *c,
			    struct ssh_connection *connection,
			    struct lsh_string *packet)
{
490
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
491
492
493
494
495
496
497
498
499
500
501
502
503

  struct simple_buffer buffer;
  int msg_number;
  UINT32 channel_number;
  
  MDEBUG(closure);

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

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_CLOSE)
      && parse_eod(&buffer))
    {
504
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
505
506
507
508
509
510
511
512
513
514
515
516
						   channel_number);

      lsh_string_free(packet);
      
      if (channel && channel->close)
	return CHANNEL_CLOSE(channel, connection->write);
      
    }
  lsh_string_free(packet);
  return LSH_FAIL | LSH_DIE;
}

517
static int do_channel_open_confirm(struct packet_handler *c,
Niels Möller's avatar
Niels Möller committed
518
519
520
			      struct ssh_connection *connection,
			      struct lsh_string *packet)
{
521
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541

  struct simple_buffer buffer;
  int msg_number;
  UINT32 local_channel_number;
  UINT32 remote_channel_number;  
  UINT32 window_size;
  UINT32 max_packet;
  
  MDEBUG(closure);

  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))
    {
542
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
543
544
545
546
						   local_channel_number);

      lsh_string_free(packet);

547
      if (channel && channel->open_confirm)
Niels Möller's avatar
Niels Möller committed
548
549
550
551
552
	{
	  channel->channel_number = remote_channel_number;
	  channel->send_window_size = window_size;
	  channel->send_max_packet = max_packet;

553
	  return CHANNEL_OPEN_CONFIRM(channel, connection->write);
Niels Möller's avatar
Niels Möller committed
554
555
556
557
558
559
560
561
562
	}
      werror("Unexpected SSH_MSG_CHANNEL_OPEN_CONFIRMATION on channel %d\n",
	     local_channel_number);
      return LSH_FAIL | LSH_DIE;
    }
  lsh_string_free(packet);
  return LSH_FAIL | LSH_DIE;
}

563
static int do_channel_open_failure(struct packet_handler *c,
Niels Möller's avatar
Niels Möller committed
564
565
566
			      struct ssh_connection *connection,
			      struct lsh_string *packet)
{
567
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591

  struct simple_buffer buffer;
  int msg_number;
  UINT32 channel_number;
  UINT32 reason;

  UINT8 *msg;
  UINT32 length;

  UINT8 *language;
  UINT32 language_length;
  
  MDEBUG(closure);

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

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_OPEN_FAILURE)
      && parse_uint32(&buffer, &channel_number)
      && parse_uint32(&buffer, &reason)
      && parse_string(&buffer, &length, &msg)
      && parse_string(&buffer, &language_length, &language)
      && parse_eod(&buffer))
    {
592
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
593
594
595
596
						   channel_number);

      /* lsh_string_free(packet); */

597
      if (channel && channel->open_failure)
Niels Möller's avatar
Niels Möller committed
598
	{
599
	  int res = CHANNEL_OPEN_FAILURE(channel, connection->write);
Niels Möller's avatar
Niels Möller committed
600
601

	  lsh_string_free(packet);
602
	  dealloc_channel(closure->table, channel_number);
Niels Möller's avatar
Niels Möller committed
603
604
605
606
607
608
609
610
611
612
613
614
615
	  
	  return res;
	}
      werror("Unexpected SSH_MSG_CHANNEL_OPEN_FAILURE on channel %d\n",
	     channel_number);
      lsh_string_free(packet);
      
      return LSH_FAIL | LSH_DIE;
    }
  lsh_string_free(packet);
  return LSH_FAIL | LSH_DIE;
}

616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
static int do_channel_success(struct packet_handler *c,
			      struct ssh_connection *connection,
			      struct lsh_string *packet)
{
  struct channel_handler *closure = (struct channel_handler *) c;

  struct simple_buffer buffer;
  int msg_number;
  UINT32 channel_number;
  
  MDEBUG(closure);

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

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_SUCCESS)
      && parse_eod(&buffer))
    {
      struct ssh_channel *channel = lookup_channel(closure->table,
						   channel_number);

      lsh_string_free(packet);
      
Niels Möller's avatar
Niels Möller committed
639
      if (channel && channel->channel_success)
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
	return CHANNEL_SUCCESS(channel, connection->write);
      
    }
  lsh_string_free(packet);
  return LSH_FAIL | LSH_DIE;
}

static int do_channel_failure(struct packet_handler *c,
			      struct ssh_connection *connection,
			      struct lsh_string *packet)
{
  struct channel_handler *closure = (struct channel_handler *) c;

  struct simple_buffer buffer;
  int msg_number;
  UINT32 channel_number;
  
  MDEBUG(closure);

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

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_FAILURE)
      && parse_eod(&buffer))
    {
      struct ssh_channel *channel = lookup_channel(closure->table,
						   channel_number);

      lsh_string_free(packet);
      
Niels Möller's avatar
Niels Möller committed
670
      if (channel && channel->channel_failure)
671
672
673
674
675
676
677
	return CHANNEL_FAILURE(channel, connection->write);
      
    }
  lsh_string_free(packet);
  return LSH_FAIL | LSH_DIE;
}

678
static int init_connection_service(struct ssh_service *s,
Niels Möller's avatar
Niels Möller committed
679
				   struct ssh_connection *connection)
Niels Möller's avatar
Niels Möller committed
680
681
{
  struct connection_service *self = (struct connection_service *) s;
682
  struct channel_table *table;
Niels Möller's avatar
Niels Möller committed
683
684
685
  
  struct global_request_handler *globals;
  struct channel_open_handler *open;
686
  struct channel_handler *request;
Niels Möller's avatar
Niels Möller committed
687

688
689
690
  struct channel_handler *adjust;
  struct channel_handler *data;
  struct channel_handler *extended;
Niels Möller's avatar
Niels Möller committed
691

692
693
  struct channel_handler *eof;
  struct channel_handler *close;
Niels Möller's avatar
Niels Möller committed
694

695
696
697
698
699
  struct channel_handler *open_confirm;
  struct channel_handler *open_failure;

  struct channel_handler *channel_success;
  struct channel_handler *channel_failure;
Niels Möller's avatar
Niels Möller committed
700
701
702

  MDEBUG(self);

703
  table = make_channel_table();
Niels Möller's avatar
Niels Möller committed
704
705
706
  
  NEW(globals);
  globals->super.super.handler = do_global_request;
707
  globals->super.table = table;
Niels Möller's avatar
Niels Möller committed
708
709
710
711
712
  globals->global_requests = self->global_requests;
  connection->dispatch[SSH_MSG_GLOBAL_REQUEST] = &globals->super.super;
    
  NEW(open);
  open->super.super.handler = do_channel_open;
713
  open->super.table = table;
Niels Möller's avatar
Niels Möller committed
714
715
716
717
718
  open->channel_types = self->channel_types;
  connection->dispatch[SSH_MSG_CHANNEL_OPEN] = &open->super.super;

  NEW(request);
  request->super.handler = do_channel_request;
719
  request->table = table;
Niels Möller's avatar
Niels Möller committed
720
721
722
723
  connection->dispatch[SSH_MSG_CHANNEL_REQUEST] = &request->super;
  
  NEW(adjust);
  adjust->super.handler = do_window_adjust;
724
  adjust->table = table;
Niels Möller's avatar
Niels Möller committed
725
726
727
728
  connection->dispatch[SSH_MSG_CHANNEL_WINDOW_ADJUST] = &adjust->super;

  NEW(data);
  data->super.handler = do_channel_data;
729
  data->table = table;
Niels Möller's avatar
Niels Möller committed
730
731
732
733
  connection->dispatch[SSH_MSG_CHANNEL_DATA] = &data->super;

  NEW(extended);
  extended->super.handler = do_channel_extended_data;
734
  extended->table = table;
Niels Möller's avatar
Niels Möller committed
735
736
737
738
  connection->dispatch[SSH_MSG_CHANNEL_WINDOW_ADJUST] = &extended->super;

  NEW(eof);
  eof->super.handler = do_channel_eof;
739
  eof->table = table;
Niels Möller's avatar
Niels Möller committed
740
741
742
743
  connection->dispatch[SSH_MSG_CHANNEL_EOF] = &eof->super;

  NEW(close);
  close->super.handler = do_channel_close;
744
  close->table = table;
Niels Möller's avatar
Niels Möller committed
745
746
  connection->dispatch[SSH_MSG_CHANNEL_CLOSE] = &close->super;

747
748
749
750
  NEW(open_confirm);
  open_confirm->super.handler = do_channel_open_confirm;
  open_confirm->table = table;
  connection->dispatch[SSH_MSG_CHANNEL_OPEN_CONFIRMATION] = &open_confirm->super;
Niels Möller's avatar
Niels Möller committed
751

752
753
754
755
  NEW(open_failure);
  open_failure->super.handler = do_channel_open_failure;
  open_failure->table = table;
  connection->dispatch[SSH_MSG_CHANNEL_OPEN_FAILURE] = &open_failure->super;
Niels Möller's avatar
Niels Möller committed
756
  
757
758
759
760
761
762
763
764
765
766
  NEW(channel_success);
  channel_success->super.handler = do_channel_success;
  channel_success->table = table;
  connection->dispatch[SSH_MSG_CHANNEL_SUCCESS] = &channel_success->super;

  NEW(channel_failure);
  channel_failure->super.handler = do_channel_failure;
  channel_failure->table = table;
  connection->dispatch[SSH_MSG_CHANNEL_FAILURE] = &channel_failure->super;
    
Niels Möller's avatar
Niels Möller committed
767
768
  return self->start
    ? CONNECTION_START(self->start, table, connection->write)
769
    : LSH_OK | LSH_GOON;
Niels Möller's avatar
Niels Möller committed
770
771
}

Niels Möller's avatar
Niels Möller committed
772
773
774
775
int channel_transmit_header(struct ssh_channel *channel,
			    struct abstract_write *write,
			    struct lsh_string *header,
			    struct lsh_string *data)
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
{
  UINT32 i;
  UINT32 left;
  UINT32 chunk = channel->send_max_packet;
  int res = 0;
  
  assert(data->length <= channel->send_window_size);

  if (data->length <= chunk)
    return A_WRITE(write, ssh_format("%lS%fS",
				     header,
				     data));

  for(i = 0, left = data->length;
      left; )
    {
      UINT32 size = MIN(chunk, left);
      
      res |= A_WRITE(write, ssh_format("%lS%s", header, 
				       size, data->data + i));
      if (LSH_CLOSEDP(res))
	break;

      i += size;
      left -= size;
    }

  lsh_string_free(data);
  return res;
}

Niels Möller's avatar
Niels Möller committed
807
808
809
int channel_transmit(struct ssh_channel *channel,
		     struct abstract_write *write,
		     struct lsh_string *data)
810
811
812
813
814
{
  struct lsh_string *header = ssh_format("%c%i",
					 SSH_MSG_CHANNEL_DATA,
					 channel->channel_number);

Niels Möller's avatar
Niels Möller committed
815
  int res = channel_transmit_header(channel, write, header, data);
816
817
818
819
820
821

  lsh_string_free(header);

  return res;
}

Niels Möller's avatar
Niels Möller committed
822
823
824
825
int channel_transmit_extended(struct ssh_channel *channel,
			      struct abstract_write *write,
			      UINT32 type,
			      struct lsh_string *data)
826
827
828
829
830
831
{
  struct lsh_string *header = ssh_format("%c%i%i",
					 SSH_MSG_CHANNEL_EXTENDED_DATA,
					 type,
					 channel->channel_number);
  
Niels Möller's avatar
Niels Möller committed
832
  int res = channel_transmit_header(channel, write, header, data);
833
834
835
836
837
838
839

  lsh_string_free(header);

  return res;
}


Niels Möller's avatar
Niels Möller committed
840
841
842
843
844
845
846
struct ssh_service *make_connection_service(struct alist *global_requests,
					    struct alist *channel_types)
{
  struct connection_service *self;

  NEW(self);

847
  self->super.init = init_connection_service;
Niels Möller's avatar
Niels Möller committed
848
849
850
851
852
  self->global_requests = global_requests;
  self->channel_types = channel_types;

  return &self->super;
}
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

struct lsh_string *prepare_channel_open(struct channel_table *table,
					int type, struct ssh_channel *channel,
					char *format, ...)
{
  int index;
    
  va_list args;
  UINT32 l1, l2;
  struct lsh_string *packet;

#define OPEN_FORMAT "%c%a%i%i%i"
#define OPEN_ARGS SSH_MSG_CHANNEL_OPEN, type, (UINT32) index, \
  channel->rec_window_size, channel->rec_max_packet  

  index = register_channel(table, channel);
  if (index < 0)
    return NULL;

  l1 = ssh_format_length(OPEN_FORMAT, OPEN_ARGS);
  
  va_start(args, format);
  l2 = ssh_vformat_length(format, args);
  va_end(args);

  packet = lsh_string_alloc(l1 + l2);

  ssh_format_write(OPEN_FORMAT, l1, packet->data, OPEN_ARGS);

  va_start(args, format);
  ssh_format_write(format, l2, packet->data+l1, args);
  va_end(args);

  return packet;
#undef OPEN_FORMAT
#undef OPEN_ARGS
}
		   
struct lsh_string *format_channel_request(int type, struct ssh_channel *channel,
					  int want_reply, char *format, ...)
{
  va_list args;
  UINT32 l1, l2;
  struct lsh_string *packet;

#define REQUEST_FORMAT "%c%i%a%c"
Niels Möller's avatar
Niels Möller committed
899
#define REQUEST_ARGS SSH_MSG_CHANNEL_REQUEST, channel->channel_number, \
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
  type, want_reply
    
  l1 = ssh_format_length(REQUEST_FORMAT, REQUEST_ARGS);
  
  va_start(args, format);
  l2 = ssh_vformat_length(format, args);
  va_end(args);

  packet = lsh_string_alloc(l1 + l2);

  ssh_format_write(REQUEST_FORMAT, l1, packet->data, REQUEST_ARGS);

  va_start(args, format);
  ssh_format_write(format, l2, packet->data+l1, args);
  va_end(args);

  return packet;
#undef REQUEST_FORMAT
#undef REQUEST_ARGS
}