channel.c 29.6 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
/* 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"
Niels Möller's avatar
Niels Möller committed
28
#include "read_data.h"
Niels Möller's avatar
Niels Möller committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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;
43
44
45
46

  /* 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
47
48
};

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

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

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

  struct alist *channel_types;
};

#if 0
struct channel_request_handler
{
73
  struct channel_handler *super;
Niels Möller's avatar
Niels Möller committed
74
75
76
77
78
79
80
81
82
83

  struct alist *request_types;
};
#endif

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

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
struct lsh_string *format_open_confirmation(struct ssh_channel *channel,
					    UINT32 channel_number,
					    char *format, ...)
{
  va_list args;
  UINT32 l1, l2;
  struct lsh_string *packet;

#define CONFIRM_FORMAT "%c%i%i%i%i"
#define CONFIRM_ARGS SSH_MSG_CHANNEL_OPEN_CONFIRMATION, channel->channel_number, \
  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);
  ssh_format_write(format, l2, packet->data+l1, args);
  va_end(args);

  return packet;
#undef CONFIRM_FORMAT
#undef CONFIRM_ARGS
}

Niels Möller's avatar
Niels Möller committed
115
116
117
118
119
120
121
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);
}

122
123
124
125
126
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
127
128
129
130
131
struct lsh_string *format_channel_failure(UINT32 channel)
{
  return ssh_format("%c%i", SSH_MSG_CHANNEL_FAILURE, channel);
}

132
133
134
135
136
137
138
139
140
141
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);
}

142
/* Channel objects */
Niels Möller's avatar
Niels Möller committed
143
144
145
146
147

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

Niels Möller's avatar
Niels Möller committed
148
struct channel_table *make_channel_table(void)
Niels Möller's avatar
Niels Möller committed
149
{
150
  struct channel_table *table;
Niels Möller's avatar
Niels Möller committed
151

152
153
  NEW(table);
  table->channels = lsh_space_alloc(sizeof(struct ssh_channel *)
154
				      * INITIAL_CHANNELS);
155
156
157
158
  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
159

160
  return table;
Niels Möller's avatar
Niels Möller committed
161
162
163
};

/* Returns -1 if allocation fails */
164
int alloc_channel(struct channel_table *table)
Niels Möller's avatar
Niels Möller committed
165
166
{
  int i;
167
  for(i = table->next_channel; i < table->used_channels; i++)
Niels Möller's avatar
Niels Möller committed
168
    {
169
      if (!table->channels[i])
Niels Möller's avatar
Niels Möller committed
170
	{
171
	  table->next_channel = i+1;
Niels Möller's avatar
Niels Möller committed
172
173
174
	  return i;
	}
    }
175
  if (i == table->max_channels)
Niels Möller's avatar
Niels Möller committed
176
    return -1;
177
  if (i == table->allocated_channels) 
Niels Möller's avatar
Niels Möller committed
178
    {
179
      int new_size = table->allocated_channels * 2;
Niels Möller's avatar
Niels Möller committed
180
      struct ssh_channel **new
181
	= lsh_space_alloc(sizeof(struct ssh_channel *) * new_size);
Niels Möller's avatar
Niels Möller committed
182

183
184
      memcpy(new, table->channels,
	     sizeof(struct ssh_channel *) * table->used_channels);
Niels Möller's avatar
Niels Möller committed
185
      
186
187
      table->channels = new;
      table->allocated_channels = new_size;
Niels Möller's avatar
Niels Möller committed
188
189
    }

190
  table->next_channel = table->used_channels = i+1;
Niels Möller's avatar
Niels Möller committed
191
192
193
194

  return i;
}

195
void dealloc_channel(struct channel_table *table, int i)
Niels Möller's avatar
Niels Möller committed
196
197
{
  assert(i >= 0);
198
  assert(i < table->used_channels);
Niels Möller's avatar
Niels Möller committed
199
  
200
201
202
203
204
205
206
207
208
209
210
211
  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
212

213
  return n;
Niels Möller's avatar
Niels Möller committed
214
215
}

216
struct ssh_channel *lookup_channel(struct channel_table *table, UINT32 i)
Niels Möller's avatar
Niels Möller committed
217
{
218
219
  return (i < table->used_channels)
    ? table->channels[i] : NULL;
Niels Möller's avatar
Niels Möller committed
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
266
267
}

/* 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;
268
269
270
  UINT32 remote_channel_number;
  UINT32 window_size;
  UINT32 max_packet;
Niels Möller's avatar
Niels Möller committed
271
272
273
274
275
276
277
278
  
  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)
279
280
281
      && 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
282
283
    {
      struct channel_open *open;
284
285
286
287
288
289
290
      struct ssh_channel *channel;
      UINT32 error = 0;
      char *error_msg;
      struct lsh_string *args = NULL;
      
      int local_channel_number;
      
Niels Möller's avatar
Niels Möller committed
291
292
293
294
      lsh_string_free(packet);
      
      if (!type || !(open = ALIST_GET(closure->channel_types, type)))
	return A_WRITE(connection->write,
295
		       format_open_failure(remote_channel_number,
Niels Möller's avatar
Niels Möller committed
296
297
					   SSH_OPEN_UNKNOWN_CHANNEL_TYPE,
					   "Unknown channel type", ""));
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
      
      channel = CHANNEL_OPEN(open, &buffer, &error, &error_msg, &args);
      
      if (!channel)
	{
	  if (error)
	    return A_WRITE(connection->write,
			   format_open_failure(remote_channel_number,
					       error, error_msg, ""));
	  /* The request was invalid */
	  return LSH_FAIL | LSH_DIE;
	}

      if ( (local_channel_number
	    = register_channel(closure->super.table, channel)) < 0)
	{
	  werror("Could not allocate a channel number for pened channel!\n");
	  return A_WRITE(connection->write,
			 format_open_failure(remote_channel_number,
					     SSH_OPEN_RESOURCE_SHORTAGE,
					     "Could not allocate a channel number "
					     "(shouldn't happen...)", ""));
	}
      
      channel->send_window_size = window_size;
      channel->send_max_packet = max_packet;
      channel->channel_number = remote_channel_number;

      channel->write = connection->write;

      return A_WRITE(connection->write,
		     args
		     ? format_open_confirmation(channel, local_channel_number,
						"%lfS", args)
		     : format_open_confirmation(channel, local_channel_number, ""));
Niels Möller's avatar
Niels Möller committed
333
334
335
336
337
338
339
340
341
342
    }
  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)
{
343
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360

  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))
    {
361
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
362
363
364
365
366
367
368
369
						   channel_number);

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

370
371
372
373
	  if (type && channel->request_types 
	      && ( (req = ALIST_GET(channel->request_types, type)) ))
	    return CHANNEL_REQUEST(req, channel, want_reply, &buffer);
	  else
374
375
376
377
378
	    return want_reply
	      ? A_WRITE(connection->write,
			format_channel_failure(channel->channel_number))
	      : LSH_OK | LSH_GOON;
	  
Niels Möller's avatar
Niels Möller committed
379
380
381
382
383
384
385
386
387
388
389
390
	}
      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,
391
			    struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
392
{
393
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

  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))
    {
409
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
410
411
412
413
						   channel_number);

      lsh_string_free(packet);
      
Niels Möller's avatar
Niels Möller committed
414
415
      if (channel
	  && !(channel->flags & (CHANNEL_RECIEVED_EOF | CHANNEL_RECIEVED_CLOSE)))
Niels Möller's avatar
Niels Möller committed
416
	{
Niels Möller's avatar
Niels Möller committed
417
418
419
420
421
422
	  if (! (channel->flags & CHANNEL_SENT_CLOSE))
	    {
	      channel->send_window_size += size;
	      if (channel->send_window_size && channel->send)
		CHANNEL_SEND(channel);
	    }
Niels Möller's avatar
Niels Möller committed
423
424
425
426
	  return LSH_OK | LSH_GOON;
	}
      /* FIXME: What to do now? Should unknown channel numbers be
       * ignored silently? */
Niels Möller's avatar
Niels Möller committed
427
      werror("SSH_MSG_CHANNEL_WINDOW_ADJUST on nonexistant or closed channel %d\n",
Niels Möller's avatar
Niels Möller committed
428
429
430
431
432
433
434
435
436
437
438
439
	     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)
{
440
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455

  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))
    {
456
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
457
458
459
460
						   channel_number);

      lsh_string_free(packet);
      
Niels Möller's avatar
Niels Möller committed
461
462
      if (channel && channel->recieve
	  && !(channel->flags & (CHANNEL_RECIEVED_EOF | CHANNEL_RECIEVED_CLOSE)))
Niels Möller's avatar
Niels Möller committed
463
	{
Niels Möller's avatar
Niels Möller committed
464
	  if (channel->flags & CHANNEL_SENT_CLOSE)
Niels Möller's avatar
Niels Möller committed
465
	    {
Niels Möller's avatar
Niels Möller committed
466
467
	      werror("Ignoring data on channel which is closing\n");
	      return LSH_OK | LSH_GOON;
Niels Möller's avatar
Niels Möller committed
468
	    }
Niels Möller's avatar
Niels Möller committed
469
470
	  else
	    {
471
472
	      int res = 0;
	      
Niels Möller's avatar
Niels Möller committed
473
474
475
476
477
478
	      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;
		}
479
480
481
482
483
484
485
486
487
488
489
490
491
	      channel->rec_window_size -= data->length;

	      if (channel->rec_window_size < channel->max_window / 2)
		{
		  res = A_WRITE(channel->write, prepare_window_adjust
				(channel,
				 channel->max_window - channel->rec_window_size));
		  if (LSH_CLOSEDP(res))
		    return res;
		}

	      return res | CHANNEL_RECIEVE(channel, 
					   CHANNEL_DATA, data);
Niels Möller's avatar
Niels Möller committed
492
493
	    }
	  return LSH_OK | LSH_GOON;
Niels Möller's avatar
Niels Möller committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
	}
	  
      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)
{
510
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527

  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))
    {
528
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
529
530
531
532
						   channel_number);

      lsh_string_free(packet);
      
Niels Möller's avatar
Niels Möller committed
533
534
      if (channel && channel->recieve
	  && !(channel->flags & (CHANNEL_RECIEVED_EOF | CHANNEL_RECIEVED_CLOSE)))
Niels Möller's avatar
Niels Möller committed
535
	{
Niels Möller's avatar
Niels Möller committed
536
	  if (channel->flags & CHANNEL_SENT_CLOSE)
Niels Möller's avatar
Niels Möller committed
537
	    {
Niels Möller's avatar
Niels Möller committed
538
539
	      werror("Ignoring extended data on channel which is closing\n");
	      return LSH_OK | LSH_GOON;
Niels Möller's avatar
Niels Möller committed
540
	    }
Niels Möller's avatar
Niels Möller committed
541
	  else
Niels Möller's avatar
Niels Möller committed
542
	    {
543
544
	      int res = 0;
	      
Niels Möller's avatar
Niels Möller committed
545
546
547
548
549
550
551
552
553
	      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;
554
555
556
557
558
559
560
561
562
563

	      if (channel->rec_window_size < channel->max_window / 2)
		{
		  res = A_WRITE(channel->write, prepare_window_adjust
				(channel,
				 channel->max_window - channel->rec_window_size));
		  if (LSH_CLOSEDP(res))
		    return res;
		}

Niels Möller's avatar
Niels Möller committed
564
565
566
	      switch(type)
		{
		case SSH_EXTENDED_DATA_STDERR:
567
568
		  return res | CHANNEL_RECIEVE(channel, 
					       CHANNEL_STDERR_DATA, data);
Niels Möller's avatar
Niels Möller committed
569
570
571
572
573
574
		default:
		  werror("Unknown type %d of extended data.\n",
			 type);
		  lsh_string_free(data);
		  return LSH_FAIL | LSH_DIE;
		}
Niels Möller's avatar
Niels Möller committed
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
	    }
	}
      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)
{
591
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
592
593
594
595
596
597
598
599
600
601
602
603
604

  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))
    {
605
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
606
607
608
609
						   channel_number);

      lsh_string_free(packet);

Niels Möller's avatar
Niels Möller committed
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
      if (channel)
	{
	  if (channel->flags & (CHANNEL_RECIEVED_EOF | CHANNEL_RECIEVED_CLOSE))
	    {
	      werror("Recieving EOF on channel on closed channel.\n");
	      return LSH_FAIL | LSH_DIE;
	    }

	  channel->flags |= CHANNEL_RECIEVED_EOF;
	  
	  if (channel->flags & CHANNEL_SENT_CLOSE)
	    /* Do nothing */
	    return LSH_OK | LSH_GOON;

	  if (channel->flags & CHANNEL_SENT_EOF)
	    {
	      /* Both parties have sent EOF. Initiate close, if we
	       * havn't done that already. */

	      return (channel->flags & CHANNEL_SENT_CLOSE)
		? LSH_OK | LSH_GOON
		: channel_close(channel);
	    }
	}
      werror("EOF on non-existant channel %d\n",
	     channel_number);
      return LSH_FAIL | LSH_DIE;
Niels Möller's avatar
Niels Möller committed
637
    }
Niels Möller's avatar
Niels Möller committed
638
      
Niels Möller's avatar
Niels Möller committed
639
640
641
642
643
644
645
646
  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)
{
647
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
648
649
650
651
652
653
654
655
656
657
658
659
660

  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))
    {
661
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
662
663
664
665
						   channel_number);

      lsh_string_free(packet);
      
Niels Möller's avatar
Niels Möller committed
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
      if (channel)
	{
	  if (channel->flags & CHANNEL_RECIEVED_CLOSE)
	    {
	      werror("Recieving multiple CLOSE on channel.\n");
	      return LSH_FAIL | LSH_DIE;
	    }

	  channel->flags |= CHANNEL_RECIEVED_CLOSE;
	  
	  if (channel->flags & (CHANNEL_RECIEVED_EOF | CHANNEL_SENT_EOF))
	    {
	      werror("Unexpected channel CLOSE.\n");
	    }
	  
	  return (channel->flags & (CHANNEL_SENT_CLOSE))
	    ? LSH_OK | LSH_CHANNEL_FINISHED
	    : LSH_OK | LSH_GOON;
	}
      werror("CLOSE on non-existant channel %d\n",
	     channel_number);
      return LSH_FAIL | LSH_DIE;
Niels Möller's avatar
Niels Möller committed
688
689
690
691
692
693
      
    }
  lsh_string_free(packet);
  return LSH_FAIL | LSH_DIE;
}

694
static int do_channel_open_confirm(struct packet_handler *c,
Niels Möller's avatar
Niels Möller committed
695
696
697
			      struct ssh_connection *connection,
			      struct lsh_string *packet)
{
698
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718

  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))
    {
719
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
720
721
722
723
						   local_channel_number);

      lsh_string_free(packet);

724
      if (channel && channel->open_confirm)
Niels Möller's avatar
Niels Möller committed
725
726
727
728
729
	{
	  channel->channel_number = remote_channel_number;
	  channel->send_window_size = window_size;
	  channel->send_max_packet = max_packet;

730
	  return CHANNEL_OPEN_CONFIRM(channel);
Niels Möller's avatar
Niels Möller committed
731
732
733
734
735
736
737
738
739
	}
      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;
}

740
static int do_channel_open_failure(struct packet_handler *c,
Niels Möller's avatar
Niels Möller committed
741
742
743
			      struct ssh_connection *connection,
			      struct lsh_string *packet)
{
744
  struct channel_handler *closure = (struct channel_handler *) c;
Niels Möller's avatar
Niels Möller committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768

  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))
    {
769
      struct ssh_channel *channel = lookup_channel(closure->table,
Niels Möller's avatar
Niels Möller committed
770
771
772
773
						   channel_number);

      /* lsh_string_free(packet); */

774
      if (channel && channel->open_failure)
Niels Möller's avatar
Niels Möller committed
775
	{
776
	  int res = CHANNEL_OPEN_FAILURE(channel);
Niels Möller's avatar
Niels Möller committed
777
778

	  lsh_string_free(packet);
779
	  dealloc_channel(closure->table, channel_number);
Niels Möller's avatar
Niels Möller committed
780
781
782
783
784
785
786
787
788
789
790
791
792
	  
	  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;
}

793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
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
816
      if (channel && channel->channel_success)
817
	return CHANNEL_SUCCESS(channel);
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
      
    }
  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
847
      if (channel && channel->channel_failure)
848
	return CHANNEL_FAILURE(channel);
849
850
851
852
853
854
      
    }
  lsh_string_free(packet);
  return LSH_FAIL | LSH_DIE;
}

855
static int init_connection_service(struct ssh_service *s,
Niels Möller's avatar
Niels Möller committed
856
				   struct ssh_connection *connection)
Niels Möller's avatar
Niels Möller committed
857
858
{
  struct connection_service *self = (struct connection_service *) s;
859
  struct channel_table *table;
Niels Möller's avatar
Niels Möller committed
860
861
862
  
  struct global_request_handler *globals;
  struct channel_open_handler *open;
863
  struct channel_handler *request;
Niels Möller's avatar
Niels Möller committed
864

865
866
867
  struct channel_handler *adjust;
  struct channel_handler *data;
  struct channel_handler *extended;
Niels Möller's avatar
Niels Möller committed
868

869
870
  struct channel_handler *eof;
  struct channel_handler *close;
Niels Möller's avatar
Niels Möller committed
871

872
873
874
875
876
  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
877
878
879

  MDEBUG(self);

880
  table = make_channel_table();
Niels Möller's avatar
Niels Möller committed
881
882
883
  
  NEW(globals);
  globals->super.super.handler = do_global_request;
884
  globals->super.table = table;
Niels Möller's avatar
Niels Möller committed
885
886
887
888
889
  globals->global_requests = self->global_requests;
  connection->dispatch[SSH_MSG_GLOBAL_REQUEST] = &globals->super.super;
    
  NEW(open);
  open->super.super.handler = do_channel_open;
890
  open->super.table = table;
Niels Möller's avatar
Niels Möller committed
891
892
893
894
895
  open->channel_types = self->channel_types;
  connection->dispatch[SSH_MSG_CHANNEL_OPEN] = &open->super.super;

  NEW(request);
  request->super.handler = do_channel_request;
896
  request->table = table;
Niels Möller's avatar
Niels Möller committed
897
898
899
900
  connection->dispatch[SSH_MSG_CHANNEL_REQUEST] = &request->super;
  
  NEW(adjust);
  adjust->super.handler = do_window_adjust;
901
  adjust->table = table;
Niels Möller's avatar
Niels Möller committed
902
903
904
905
  connection->dispatch[SSH_MSG_CHANNEL_WINDOW_ADJUST] = &adjust->super;

  NEW(data);
  data->super.handler = do_channel_data;
906
  data->table = table;
Niels Möller's avatar
Niels Möller committed
907
908
909
910
  connection->dispatch[SSH_MSG_CHANNEL_DATA] = &data->super;

  NEW(extended);
  extended->super.handler = do_channel_extended_data;
911
  extended->table = table;
Niels Möller's avatar
Niels Möller committed
912
913
914
915
  connection->dispatch[SSH_MSG_CHANNEL_WINDOW_ADJUST] = &extended->super;

  NEW(eof);
  eof->super.handler = do_channel_eof;
916
  eof->table = table;
Niels Möller's avatar
Niels Möller committed
917
918
919
920
  connection->dispatch[SSH_MSG_CHANNEL_EOF] = &eof->super;

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

924
925
926
927
  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
928

929
930
931
932
  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
933
  
934
935
936
937
938
939
940
941
942
943
  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
944
945
  return self->start
    ? CONNECTION_START(self->start, table, connection->write)
946
    : LSH_OK | LSH_GOON;
Niels Möller's avatar
Niels Möller committed
947
948
}

949
950
951
struct ssh_service *make_connection_service(struct alist *global_requests,
					    struct alist *channel_types,
					    struct connection_startup *start)
952
{
953
  struct connection_service *self;
954

955
  NEW(self);
956

957
958
959
960
961
962
  self->super.init = init_connection_service;
  self->global_requests = global_requests;
  self->channel_types = channel_types;
  self->start = start;
  
  return &self->super;
963
964
}

Niels Möller's avatar
Niels Möller committed
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
struct lsh_string *format_channel_close(struct ssh_channel *channel)
{
  return ssh_format("%c%i",
		    SSH_MSG_CHANNEL_CLOSE,
		    channel->channel_number);
}

int channel_close(struct ssh_channel *channel)
{
  channel->flags |= CHANNEL_SENT_CLOSE;

  return A_WRITE(channel->write, format_channel_close(channel))
    | ( (channel->flags & CHANNEL_RECIEVED_CLOSE)
	? LSH_CHANNEL_FINISHED : 0);
}

struct lsh_string *format_channel_eof(struct ssh_channel *channel)
{
  return ssh_format("%c%i",
		    SSH_MSG_CHANNEL_EOF,
		    channel->channel_number);
}

int channel_eof(struct ssh_channel *channel)
{
  int res ;
  
  channel->flags |= CHANNEL_SENT_CLOSE;
  res =  A_WRITE(channel->write, format_channel_eof(channel));

  if (LSH_CLOSEDP(res))
    return res;

  if (channel->flags & CHANNEL_RECIEVED_EOF)
    {
      /* Initiate close */
      res |= channel_close(channel);
    }

  return res;
}

void init_channel(struct ssh_channel *channel)
{
  /* channel->super.handler = do_read_channel; */
  channel->write = NULL;

  channel->flags = 0;
  
  channel->request_types = NULL;
  channel->recieve = NULL;
  channel->send = NULL;
#if 0
  channel->close = NULL;
  channel->eof = NULL;
#endif
  channel->open_confirm = NULL;
  channel->open_failure = NULL;
  channel->channel_success = NULL;
  channel->channel_failure = NULL;
}

1027
1028
struct lsh_string *channel_transmit_data(struct ssh_channel *channel,
					 struct lsh_string *data)
1029
{
1030
1031
1032
1033
1034
1035
1036
  assert(data->length <= channel->send_window_size);
  assert(data->length <= channel->send_max_packet);
  
  return ssh_format("%c%i%fS",
		    SSH_MSG_CHANNEL_DATA,
		    channel->channel_number,
		    data);
1037
1038
}

1039
1040
1041
struct lsh_string *channel_transmit_extended(struct ssh_channel *channel,
					     UINT32 type,
					     struct lsh_string *data)
1042
{
1043
1044
  assert(data->length <= channel->send_window_size);
  assert(data->length <= channel->send_max_packet);
1045
  
1046
1047
1048
1049
1050
  return ssh_format("%c%i%i%fS",
		    SSH_MSG_CHANNEL_EXTENDED_DATA,
		    type,
		    channel->channel_number,
		    data);
1051
1052
}

Niels Möller's avatar
Niels Möller committed
1053
1054
/* Writing data to a channel */
struct channel_write
Niels Möller's avatar
Niels Möller committed
1055
{
Niels Möller's avatar
Niels Möller committed
1056
1057
1058
  struct abstract_write super;
  struct ssh_channel *channel;
};
Niels Möller's avatar
Niels Möller committed
1059

Niels Möller's avatar
Niels Möller committed
1060
1061
1062
1063
1064
struct channel_write_extended
{
  struct channel_write super;
  UINT32 type;
};
Niels Möller's avatar
Niels Möller committed
1065

Niels Möller's avatar
Niels Möller committed
1066
1067
1068
1069
static int do_channel_write(struct abstract_write **c,
			    struct lsh_string *packet)
{
  struct channel_write *closure = (struct channel_write *) *c;
Niels Möller's avatar
Niels Möller committed
1070

Niels Möller's avatar
Niels Möller committed
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
  return A_WRITE(closure->channel->write,
		 channel_transmit_data(closure->channel, packet));
}

static int do_channel_write_extended(struct abstract_write **c,
				struct lsh_string *packet)
{
  struct channel_write_extended *closure = (struct channel_write_extended *) *c;

  return A_WRITE(closure->super.channel->write,
		 channel_transmit_extended(closure->super.channel,
					   closure->type,
					   packet));
1084
1085
}

Niels Möller's avatar
Niels Möller committed
1086
struct abstract_write *make_channel_write(struct ssh_channel *channel)
1087
{
Niels Möller's avatar
Niels Möller committed
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
  struct channel_write *closure;

  NEW(closure);

  closure->super.write = do_channel_write;
  closure->channel = channel;

  return &closure->super;
}

struct abstract_write *make_channel_write_extended(struct ssh_channel *channel,
						   UINT32 type)
{
  struct channel_write_extended *closure;

  NEW(closure);

  closure->super.super.write = do_channel_write_extended;
  closure->super.channel = channel;
  closure->type = type;
1108
  
Niels Möller's avatar
Niels Möller committed
1109
  return &closure->super.super;
Niels Möller's avatar
Niels Möller committed
1110
}
1111

Niels Möller's avatar
Niels Möller committed
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
struct read_handler *make_channel_read_data(struct ssh_channel *channel)
{
  return make_read_data(channel, make_channel_write(channel));
}

struct read_handler *make_channel_read_stderr(struct ssh_channel *channel)
{
  return make_read_data(channel,
			make_channel_write_extended(channel,
						    SSH_EXTENDED_DATA_STDERR));
}    

1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
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
1169
#define REQUEST_ARGS SSH_MSG_CHANNEL_REQUEST, channel->channel_number, \
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
  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
}