tcpforward_commands.c 15.2 KB
Newer Older
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
/* tcpforward_commands.c
 *
 * $Id$
 */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Balazs Scheidler, Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "tcpforward_commands.h"

Niels Möller's avatar
Niels Möller committed
27
28
29
30
31
32
33
34
35
36
37
#include "atoms.h"
#include "channel_commands.h"
#include "format.h"
#include "io_commands.h"
#include "ssh.h"
#include "werror.h"
#include "xalloc.h"

#include <assert.h>

/* Forward declarations */
38

39
extern struct collect_info_1 open_direct_tcpip;
Niels Möller's avatar
Niels Möller committed
40
extern struct collect_info_1 remote_listen_command;
41
extern struct collect_info_1 open_forwarded_tcpip;
Niels Möller's avatar
Niels Möller committed
42
43
44
extern struct command tcpip_start_io;
extern struct command tcpip_connect_io;

45
46
struct collect_info_1 install_direct_tcpip_handler;
struct collect_info_1 install_forwared_tcpip_handler;
Niels Möller's avatar
Niels Möller committed
47

48
49
50
51
static struct command make_direct_tcpip_handler;

struct collect_info_1 install_tcpip_forward_handler;
static struct command make_tcpip_forward_handler;
Niels Möller's avatar
Niels Möller committed
52

53
#define OPEN_DIRECT_TCPIP (&open_direct_tcpip.super.super.super)
Niels Möller's avatar
Niels Möller committed
54
55
56
#define REMOTE_LISTEN (&remote_listen_command.super.super.super)
#define TCPIP_START_IO (&tcpip_start_io.super)
#define TCPIP_CONNECT_IO (&tcpip_connect_io.super)
57
#define OPEN_FORWARDED_TCPIP (&open_forwarded_tcpip.super.super.super)
Niels Möller's avatar
Niels Möller committed
58

59
60
61
62
63
64
65
66

#include "tcpforward_commands.c.x"


/* Takes a socket as argument, and returns a tcpip channel. Used by
 * the party receiving a open-tcp request, when a channel to the
 * target has been opened. */

67
68
#define TCPIP_WINDOW_SIZE (SSH_MAX_PACKET << 3)

Niels Möller's avatar
Niels Möller committed
69
/* NOTE: This command does not do any remembering. */
Niels Möller's avatar
Niels Möller committed
70
71
72
73
74
static void
do_tcpip_connect_io(struct command *ignored UNUSED,
		    struct lsh_object *x,
		    struct command_continuation *c,
		    struct exception_handler *e UNUSED)
75
76
{
  CAST(io_fd, socket, x);
77

78
79
80
  assert(socket);

#if 0
81
82
  debug("tcpforward_commands.c: do_tcpip_connect_io, socket is %z\n",
	socket ? "valid" : "invalid");
83
84
85
  
  if (!socket)
    COMMAND_RETURN(c, NULL);
86
87
#endif
  
Niels Möller's avatar
Niels Möller committed
88
  COMMAND_RETURN(c, make_tcpip_channel(socket, TCPIP_WINDOW_SIZE));
89
90
}

Niels Möller's avatar
Niels Möller committed
91
struct command tcpip_connect_io = STATIC_COMMAND(do_tcpip_connect_io);
92
93
94
95
96
97

/* Used by the party requesting tcp forwarding, i.e. when a socket is
 * already open, and we have asked the other end to forward it. Takes
 * a channel as argument, and connects it to the socket. Returns the
 * channel. */

Niels Möller's avatar
Niels Möller committed
98
static void
Niels Möller's avatar
Niels Möller committed
99
100
do_tcpip_start_io(struct command *s UNUSED, 
		  struct lsh_object *x,
101
102
		  struct command_continuation *c,
		  struct exception_handler *e UNUSED)
103
{
Niels Möller's avatar
Niels Möller committed
104
  CAST_SUBTYPE(ssh_channel, channel, x);
105

106
107
108
  assert(channel);

#if 0
109
110
111
  debug("tcpforward_commands.c: do_tcpip_start_io, channel is %z\n",
	channel ? "valid" : "invalid");

112
113
114
  if (!channel)
    {
      verbose("Error opening channel.\n");
Niels Möller's avatar
Niels Möller committed
115
      COMMAND_RETURN(c, NULL);
116
    }
117
118
#endif
  
Niels Möller's avatar
Niels Möller committed
119
  tcpip_channel_start_io(channel);
120

Niels Möller's avatar
Niels Möller committed
121
  COMMAND_RETURN(c, channel);
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
}

struct command tcpip_start_io =
{ STATIC_HEADER, do_tcpip_start_io };


/* Requesting the opening of a forwarded tcpip channel. */

/* Used for both forwarded-tcpip and direct-tcpip. Takes a listen
 * value as argument, and returns a channel connected to some tcpip
 * port at the other end. */

/* GABA:
   (class
     (name open_tcpip_command)
     (super channel_open_command)
     (vars
       ; ATOM_FORWARDED_TCPIP or ATOM_DIRECT_TCPIP
       (type . int)
141
       (max_window . UINT32)
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
       ; For forwarded-tcpip, port is the port listened to.
       ; For direct-tcpip, poprt is the port to conect to.
       ; In both cases, it's a port used on the server end.
       (port object address_info)
       (peer object listen_value)))
*/

static struct ssh_channel *
new_tcpip_channel(struct channel_open_command *c,
		  struct ssh_connection *connection,
		  struct lsh_string **request)
{
  CAST(open_tcpip_command, self, c);
  struct ssh_channel *channel;

  /* NOTE: All accepted fd:s must end up in this function, so it
   * should be ok to delay the REMEMBER() call until here. */
159
160
161

  debug("tcpforward_commands.c: new_tcpip_channel()\n");

162
163
  REMEMBER_RESOURCE(connection->resources, &self->peer->fd->super.super);
  
164
  channel = make_tcpip_channel(self->peer->fd, TCPIP_WINDOW_SIZE);
165
  channel->write = connection->write;
166
  
Niels Möller's avatar
Niels Möller committed
167
  *request = prepare_channel_open(connection, self->type, 
168
169
  				  channel, 
  				  "%S%i%S%i",
Niels Möller's avatar
Niels Möller committed
170
				  self->port->ip, self->port->port,
171
172
173
174
175
176
				  self->peer->peer->ip, self->peer->peer->port);
  
  return channel;
}

static struct command *
177
make_open_tcpip_command(int type, UINT32 max_window,
Niels Möller's avatar
Niels Möller committed
178
			struct address_info *port,
179
180
			struct listen_value *peer)
{
Niels Möller's avatar
Niels Möller committed
181
  NEW(open_tcpip_command, self);
182
183
184

  debug("tcpforward_commands.c: make_open_tcpip_command()\n");

185
  self->super.super.call = do_channel_open_command;
Niels Möller's avatar
Niels Möller committed
186
  self->super.new_channel = new_tcpip_channel;
187
188

  self->type = type;
189
190
  self->max_window = max_window;
  
Niels Möller's avatar
Niels Möller committed
191
  self->port = port;
192
193
194
195
196
197
  self->peer = peer;
  
  return &self->super.super;
}

static struct lsh_object *
198
199
200
collect_open_forwarded_tcpip(struct collect_info_2 *info,
			     struct lsh_object *a,
			     struct lsh_object *b)
201
202
203
204
{
  CAST(address_info, local, a);
  CAST(listen_value, peer, b);

205
  assert(!info->next);
206

207
  return &make_open_tcpip_command(ATOM_FORWARDED_TCPIP, TCPIP_WINDOW_SIZE,
Niels Möller's avatar
Niels Möller committed
208
				  local, peer)->super;
209
210
}

211
212
static struct collect_info_2 collect_open_forwarded_tcpip_2 =
STATIC_COLLECT_2_FINAL(collect_open_forwarded_tcpip);
213

214
215
struct collect_info_1 open_forwarded_tcpip =
STATIC_COLLECT_1(&collect_open_forwarded_tcpip_2);
216
217

static struct lsh_object *
218
219
220
collect_open_direct_tcpip(struct collect_info_2 *info,
			  struct lsh_object *a,
			  struct lsh_object *b)
221
222
223
224
{
  CAST(address_info, local, a);
  CAST(listen_value, peer, b);

225
  assert(!info->next);
226

227
  return &make_open_tcpip_command(ATOM_DIRECT_TCPIP, TCPIP_WINDOW_SIZE,
Niels Möller's avatar
Niels Möller committed
228
				  local, peer)->super;
229
230
}

231
232
static struct collect_info_2 collect_open_direct_tcpip_2 =
STATIC_COLLECT_2_FINAL(collect_open_direct_tcpip);
233

234
235
struct collect_info_1 open_direct_tcpip =
STATIC_COLLECT_1(&collect_open_direct_tcpip_2);
236
237
238
239
240
241
242
243
244


/* Requesting remote forwarding of a port */

/* GABA:
   (class
     (name remote_port_install_continuation)
     (super command_frame)
     (vars
245
       (port object remote_port)
246
247
248
       (callback object command)))
*/

Niels Möller's avatar
Niels Möller committed
249
250
251
static void
do_remote_port_install_continuation(struct command_continuation *s,
				    struct lsh_object *x)
252
253
{
  CAST(remote_port_install_continuation, self, s);
254
255
256
257
258
  CAST(ssh_connection, connection, x);

  if (connection)
    {
      debug("tcpforward_commands.c: do_remote_port_install_continuation(), success.\n");
259
  
260
261
262
263
264
      self->port->callback = self->callback;
    }
  else
    {
      debug("tcpforward_commands.c: do_remote_port_install_continuation(), failed.\n");
265

266
267
268
      /* FIXME: Cleanup, and delete the port from the remote_ports list. */
      
    }
Niels Möller's avatar
Niels Möller committed
269
  COMMAND_RETURN(self->super.up, x);
270
271
272
}

static struct command_continuation *
273
274
make_remote_port_install_continuation(struct remote_port *port,
				      struct command *callback,
275
276
277
278
				      struct command_continuation *c)
{
  NEW(remote_port_install_continuation, self);

279
280
  debug("tcpforward_commands.c: make_remote_port_install_continuation()\n");

281
282
  self->super.super.c = do_remote_port_install_continuation;
  self->super.up = c;
283
284

  self->port = port;
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
312
313
314
315
316
317
  self->callback = callback;

  return &self->super.super;
}

/* Listening on a remote port
 *
 * (remote_listen callback port connection)
 *
 * Returns a remote_port or NULL.
 * 
 * callback is invoked with a address_info peer as argument, and
 * should return a channel or NULL.
 */

/* GABA:
   (class
     (name request_tcpip_forward_command)
     (super global_request_command)
     (vars
       ; Invoked when a forwarded_tcpip request is received.
       ; Called with the struct address_info *peer as argument.
       (callback object command)
       (port object address_info))) */

static struct lsh_string *
do_format_request_tcpip_forward(struct global_request_command *s,
				struct ssh_connection *connection,
				struct command_continuation **c)
{
  CAST(request_tcpip_forward_command, self, s);
  struct remote_port *port;
  int want_reply;
318
319
320

  debug("tcpforward_commands.c: do_format_request_tcpip_forward()\n");

Niels Möller's avatar
Niels Möller committed
321
  if (CONTINUATION_USED_P(*c))
322
323
    {
      port = make_remote_port(self->port, NULL);
324
      *c = make_remote_port_install_continuation(port, self->callback, *c);
325
326
327
328
329
330
331
332
      want_reply = 1;
    }
  else
    {
      port = make_remote_port(self->port, self->callback);
      want_reply = 0;
    }
  
Niels Möller's avatar
Niels Möller committed
333
  object_queue_add_tail(&connection->table->remote_ports,
334
335
336
337
338
339
340
341
342
343
344
			&port->super.super);
  
  return ssh_format("%c%a%c%S%i", SSH_MSG_GLOBAL_REQUEST, ATOM_TCPIP_FORWARD,
		    want_reply, self->port->ip, self->port->port);
}
		    
static struct command *
make_request_tcpip_forward_command(struct command *callback,
				   struct address_info *listen)
{
  NEW(request_tcpip_forward_command, self);
345
346
347

  debug("tcpforward_commands.c: make_request_tcpip_forward_command()\n");
  
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
  self->super.super.call = do_channel_global_command;
  self->super.format_request = do_format_request_tcpip_forward;

  self->callback = callback;
  self->port = listen;
  
  return &self->super.super;
}

static struct lsh_object *
collect_remote_listen(struct collect_info_2 *info,
		      struct lsh_object *a, struct lsh_object *b)
{
  CAST_SUBTYPE(command, callback, a);
  CAST(address_info, port, b);
363
  assert(!info->next);
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  
  return &make_request_tcpip_forward_command(callback, port)->super;
}

static struct collect_info_2 collect_info_remote_listen_2 =
STATIC_COLLECT_2_FINAL(collect_remote_listen);

static struct collect_info_1 remote_listen_command =
STATIC_COLLECT_1(&collect_info_remote_listen_2);


/* Cancel a remotely forwarded port.
 * FIXME: Not implemented */



/* GABA:
   (expr
382
     (name forward_local_port)
383
384
     (globals
       (listen LISTEN_COMMAND)
Niels Möller's avatar
Niels Möller committed
385
       (start_io TCPIP_START_IO)
386
       (open_direct_tcpip OPEN_DIRECT_TCPIP))
387
388
389
390
391
392
393
     (params
       (backend object io_backend)
       (local object address_info)
       (target object address_info))
     (expr
       (lambda (connection)
         (listen (lambda (peer)
394
	           (start_io (open_direct_tcpip target peer connection)))
395
396
397
398
		 backend
	         local))))
*/

399
400
401
402
struct command *
make_forward_local_port(struct io_backend *backend,
			struct address_info *local,
			struct address_info *target)
403
{
404
  CAST_SUBTYPE(command, res,
405
406
407
	       forward_local_port(backend, local, target));

  debug("tcpforward_commands.c: forward_local_port()\n");
408
409
410
411
412
413

  return res;
}

/* GABA:
   (expr
414
     (name forward_remote_port)
415
416
417
     (globals
       (remote_listen REMOTE_LISTEN)
       ;; (connection_remember CONNECTION_REMEMBER)
418
       (connect_io TCPIP_CONNECT_IO))
419
420
     (params
       (connect object command)
421
       (remote object address_info))
422
423
424
     (expr
       (lambda (connection)
         (remote_listen (lambda (peer)
425
	                  (connect_io ; peer
426
427
			            ;; FIXME: This calls connect too early
				    ;; Let it take peer as argument
428
429
430
				    ;; Replacing connection with (K connection peer)
				    ;; might work, except that it is optimized away.
			            (connect connection)))
431
432
433
434
	                remote
			connection))))
*/

435
436
437
438
struct command *
make_forward_remote_port(struct io_backend *backend,
			 struct address_info *remote,
			 struct address_info *target)
439
440
{
  CAST_SUBTYPE(command, res,
441
	       forward_remote_port(make_connect_port(backend, target), remote));
442

443
444
  debug("tcpforward_commands.c: forward_remote_port()\n");
  
445
446
447
  return res;
}

Niels Möller's avatar
Niels Möller committed
448
449
/* Takes a callback function and returns a channel_open
 * handler. */
Niels Möller's avatar
Niels Möller committed
450
static void
451
do_make_direct_tcpip_handler(struct command *s UNUSED,
452
453
454
			     struct lsh_object *x,
			     struct command_continuation *c,
			     struct exception_handler *e UNUSED)
Niels Möller's avatar
Niels Möller committed
455
456
457
{
  CAST_SUBTYPE(command, callback,  x);

458
459
  trace("tcpforward_commands.c: do_make_open_tcp_handler()\n");
  
Niels Möller's avatar
Niels Möller committed
460
461
  COMMAND_RETURN(c,
		 &make_channel_open_direct_tcpip(callback)->super);
Niels Möller's avatar
Niels Möller committed
462
463
464
}

static struct command
465
make_direct_tcpip_handler = STATIC_COMMAND(do_make_direct_tcpip_handler);
Niels Möller's avatar
Niels Möller committed
466
467

/* Takes a callback function and returns a global_request handler. */
Niels Möller's avatar
Niels Möller committed
468
static void
Niels Möller's avatar
Niels Möller committed
469
470
do_make_tcpip_forward_handler(struct command *s UNUSED,
			      struct lsh_object *x,
471
472
			      struct command_continuation *c,
			      struct exception_handler *e UNUSED)
Niels Möller's avatar
Niels Möller committed
473
474
475
{
  CAST_SUBTYPE(command, callback,  x);

476
477
  debug("tcpforward_commands.c: do_make_tcpip_forward_handler()\n");
  
Niels Möller's avatar
Niels Möller committed
478
479
  COMMAND_RETURN(c,
		 &make_tcpip_forward_request(callback)->super);
Niels Möller's avatar
Niels Möller committed
480
481
482
}

static struct command
483
make_tcpip_forward_handler
Niels Möller's avatar
Niels Möller committed
484
485
486
= STATIC_COMMAND(do_make_tcpip_forward_handler);


487
488
/* Commands to install handlers */
struct install_info install_direct_tcpip_info_2 =
Niels Möller's avatar
Niels Möller committed
489
490
STATIC_INSTALL_OPEN_HANDLER(ATOM_DIRECT_TCPIP);

491
492
struct collect_info_1 install_direct_tcpip_handler =
STATIC_COLLECT_1(&install_direct_tcpip_info_2.super);
Niels Möller's avatar
Niels Möller committed
493

494
struct install_info install_forwarded_tcpip_info_2 =
Niels Möller's avatar
Niels Möller committed
495
496
STATIC_INSTALL_OPEN_HANDLER(ATOM_FORWARDED_TCPIP);

497
498
struct collect_info_1 install_forwarded_tcpip_handler =
STATIC_COLLECT_1(&install_forwarded_tcpip_info_2.super);
Niels Möller's avatar
Niels Möller committed
499
500
501
502
503
504
505
506

/* Server side callbacks */

/* Make this non-static? */
/* GABA:
   (expr
     (name direct_tcpip_hook)
     (globals
507
508
509
       (install "&install_direct_tcpip_handler.super.super.super")
       (handler "&make_direct_tcpip_handler.super")
       (connect_io TCPIP_CONNECT_IO))
Niels Möller's avatar
Niels Möller committed
510
511
512
513
514
515
     (params
       (connect object command))
     (expr
       (lambda (connection)
         (install connection
	   (handler (lambda (port)
516
	     (connect_io (connect connection port))))))))
Niels Möller's avatar
Niels Möller committed
517
518
519
520
521
522
523
524
*/

struct command *
make_direct_tcpip_hook(struct io_backend *backend)
{
  CAST_SUBTYPE(command, res,
	       direct_tcpip_hook(make_connect_connection(backend)));

525
526
  debug("tcpforward_commands.c: make_direct_tcpip_hook()\n");

Niels Möller's avatar
Niels Möller committed
527
528
529
530
  return res;
}


531
532
struct install_info install_tcpip_forward_info_2 =
STATIC_INSTALL_GLOBAL_HANDLER(ATOM_TCPIP_FORWARD);
Niels Möller's avatar
Niels Möller committed
533

534
535
struct collect_info_1 install_tcpip_forward_handler =
STATIC_COLLECT_1(&install_tcpip_forward_info_2.super);
Niels Möller's avatar
Niels Möller committed
536
537
538

/* GABA:
   (expr
539
     (name tcpip_forward_hook)
Niels Möller's avatar
Niels Möller committed
540
     (globals
541
542
       (install "&install_tcpip_forward_handler.super.super.super")
       (handler "&make_tcpip_forward_handler.super")
Niels Möller's avatar
Niels Möller committed
543
       (start_io TCPIP_START_IO)
544
       (open_forwarded_tcpip OPEN_FORWARDED_TCPIP)
Niels Möller's avatar
Niels Möller committed
545
546
547
548
549
       (listen LISTEN_COMMAND))
     (params
       (backend object io_backend))
     (expr
       (lambda (connection)
550
         ;; Called when the ssh-connection is established
Niels Möller's avatar
Niels Möller committed
551
552
         (install connection
	   (handler (lambda (port)
553
	     ;; Called when the client requests remote forwarding
Niels Möller's avatar
Niels Möller committed
554
             (listen (lambda (peer)
555
556
557
558
	               ;; Called when someone connects to the
		       ;; forwarded port.
                       (start_io (open_forwarded_tcpip port peer
		                                       connection)))
Niels Möller's avatar
Niels Möller committed
559
560
561
562
	             backend port)))))))
*/

struct command *
563
make_tcpip_forward_hook(struct io_backend *backend)
Niels Möller's avatar
Niels Möller committed
564
{
565
  CAST_SUBTYPE(command, res, tcpip_forward_hook(backend));
Niels Möller's avatar
Niels Möller committed
566

567
568
  debug("tcpforward_commands.c: tcpip_forward_hook()\n");
  
Niels Möller's avatar
Niels Möller committed
569
570
  return res;
}