proxy.c 11.8 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
/* proxy.c
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1999 Balázs Scheidler
 *
 * 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 "proxy.h"
Niels Möller's avatar
Niels Möller committed
25

26
27
#include "channel_commands.h"
#include "command.h"
Niels Möller's avatar
Niels Möller committed
28
29
#include "connection.h"
#include "exception.h"
30
#include "format.h"
Niels Möller's avatar
Niels Möller committed
31
#include "gateway_channel.h"
32
#include "io_commands.h"
Niels Möller's avatar
Niels Möller committed
33
34
#include "proxy_session.h"
#include "proxy_userauth.h"
35
#include "ssh.h"
36
#include "werror.h"
Niels Möller's avatar
Niels Möller committed
37
38
#include "xalloc.h"

39
40
41
#include <assert.h>
#include <arpa/inet.h>

42
43
44
45
#define CHAINED_CONNECTION (&chained_connection.super.super)

struct command_simple chained_connection;

46
#include "proxy.c.x" 
47

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* GABA:
   (class
     (name exc_chain_connections_handler)
     (super exception_handler)
     (vars
       (connection object ssh_connection)))
*/

static void 
do_exc_chain_connections_handler(struct exception_handler *c,
				 const struct exception *e)
{
  CAST(exc_chain_connections_handler, self, c);
  switch (e->type) 
    {
    case EXC_FINISH_READ:
    case EXC_FINISH_IO:
      {
	struct ssh_connection *chain = self->connection->chain;
	if (chain)
	  {
	    self->connection->chain->chain = NULL;
	    self->connection->chain = NULL; /* prevent raising this exception again */
71
72
73
74
75
76

	    /* FIXME: is it possible to pass the same exception to two handlers?
	     *
	     * It should be. /nisse
	     */
	    
77
78
79
80
	    EXCEPTION_RAISE(chain->e, e);
	  }
      }
    }
81
  EXCEPTION_RAISE(c->parent, e);  
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
}

static struct exception_handler *
make_exc_chain_connections_handler(struct ssh_connection *connection,
				   struct exception_handler *parent,
				   const char *context)
{
  NEW(exc_chain_connections_handler, self);

  self->super.parent = parent;
  self->super.raise = do_exc_chain_connections_handler;
  self->super.context = context;
  
  self->connection = connection;

  return &self->super;
}

100
101
102
/* GABA:
   (class
     (name chain_connections_continuation)
103
     (super command_continuation)
104
     (vars
105
106
       (connection object ssh_connection)
       (up object command_continuation)))
107
108
109
110
111
112
113
114
115
116
117
 */

static void
do_chain_connections_continuation(struct command_continuation *s,
				  struct lsh_object *x)
{
  CAST(chain_connections_continuation, self, s);
  CAST(ssh_connection, chained, x);

  self->connection->chain = chained;
  chained->chain = self->connection;
118
119
120
121
122
123
124
125
126
127
128

  /* FIXME: this a little bit kludgy here */
  self->connection->e = 
    make_exc_chain_connections_handler(self->connection,
				       self->connection->e, 
				       HANDLER_CONTEXT);
  chained->e =
    make_exc_chain_connections_handler(chained,
				       chained->e,
				       HANDLER_CONTEXT);

129
  COMMAND_RETURN(self->up, x);
130
131
132
133
}

static struct command_continuation *
make_chain_connections_continuation(struct ssh_connection *connection,
134
				    struct command_continuation *c)
135
136
137
{
  NEW(chain_connections_continuation, self);
  
138
  self->super.c = do_chain_connections_continuation;
139
  self->connection = connection;
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  self->up = c;
  return &self->super;
}

/* GABA:
   (class
     (name chain_connections_client)
     (super command_frame)
     (vars
       (client_addr object listen_value)
       (client_callback object command)))
*/

static void
do_chain_connections_client(struct command_continuation *s,
			    struct lsh_object *x)
{
  CAST(chain_connections_client, closure, s);
  CAST(ssh_connection, server_connection, x);

  COMMAND_CALL(closure->client_callback,
	       &closure->client_addr->super,
	       make_chain_connections_continuation(server_connection, closure->super.up),
	       closure->super.e);
}

static struct command_continuation *
make_chain_connections_client(struct command *client_callback,
			      struct listen_value *client_addr,
			      struct command_continuation *c,
			      struct exception_handler *e)
{
  NEW(chain_connections_client, self);

  self->super.super.c = do_chain_connections_client;
175
  self->super.up = c;
176
177
178
179
  self->super.e = e;
  self->client_callback = client_callback;
  self->client_addr = client_addr;

180
181
182
183
184
185
186
187
  return &self->super.super;
}

/* GABA:
   (class
     (name chain_connections)
     (super command)
     (vars
188
189
       (server_callback object command)
       (client_callback object command)))
190
191
192
193
194
195
196
197
198
*/

static void
do_chain_connections(struct command *s,
		     struct lsh_object *x,
		     struct command_continuation *c,
		     struct exception_handler *e)
{
  CAST(chain_connections, self, s);
199
  CAST(listen_value, client_addr, x);
200

201
202
  COMMAND_CALL(self->server_callback, &client_addr->super, 
               make_chain_connections_client(self->client_callback, client_addr, c, e),
203
               e);
204
205
206
}

static struct command *
207
208
make_chain_connections(struct command *server_callback,
		       struct command *client_callback)
209
210
211
212
{
  NEW(chain_connections, self);

  self->super.call = do_chain_connections;
213
214
  self->server_callback = server_callback;
  self->client_callback = client_callback;
215
216
217
218
  return &self->super;
}

static struct lsh_object *
219
do_collect_chain_params(struct collect_info_2 *info UNUSED,
220
			struct lsh_object *a,
221
			struct lsh_object *b)
222
{
223
224
  CAST_SUBTYPE(command, server_callback, a);
  CAST_SUBTYPE(command, client_callback, b);
225

226
  return &make_chain_connections(server_callback, client_callback)->super;
227
228
}

229
struct collect_info_2 chain_connections_2 =
230
STATIC_COLLECT_2_FINAL(do_collect_chain_params);
231
232
233
234

struct collect_info_1 chain_connections =
STATIC_COLLECT_1(&chain_connections_2);

235
/* (proxy_connection_service user connection) -> connection */
236
237
238
239
240
/* GABA:
   (class
     (name proxy_connection_service)
     (super command)
     (vars
241
242
243
244
       ; hooks called on the client side (the server connection)
       (server_hooks object object_list)
       ; hooks called on the server side (the client connection)
       (client_hooks object object_list)))
245
246
*/

247
DEFINE_COMMAND_SIMPLE(chained_connection, a)
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
{
  CAST(ssh_connection, connection, a);
  return &connection->chain->super.super;
}

/* GABA:
   (expr
     (name make_call_hooks)
     (params
       (server_hooks object object_list)
       (client_hooks object object_list))
     (expr 
       (lambda (connection) 
         (progn 
           client_hooks 
           (chained_connection (progn server_hooks connection))))))
 */

266
267
268
269
static void
do_login(struct command *s,
	 struct lsh_object *x UNUSED,
	 struct command_continuation *c,
270
	 struct exception_handler *e UNUSED)
271
272
{
  CAST(proxy_connection_service, self, s);
273
274

  COMMAND_RETURN(c, make_call_hooks(self->server_hooks, self->client_hooks));
275
276
277
}

struct command *
278
279
make_proxy_connection_service(struct object_list *server_hooks,
			      struct object_list *client_hooks)
280
281
282
283
{
  NEW(proxy_connection_service, self);

  self->super.call = do_login;
284
285
  self->server_hooks = server_hooks;
  self->client_hooks = client_hooks;
286
287
  return &self->super;
}
288
289
290
291
292
293

/* GABA:
   (class
     (name proxy_accept_service_handler)
     (super packet_handler)
     (vars
294
       (name . int)
295
296
297
298
299
300
301
302
303
304
305
306
307
308
       (service object command)
       (c object command_continuation)
       (e object exception_handler)))
*/

static void
do_proxy_accept_service(struct packet_handler *c,
			struct ssh_connection *connection,
			struct lsh_string *packet)
{
  CAST(proxy_accept_service_handler, closure, c);

  struct simple_buffer buffer;
  UINT32 msg_number;
309
  int name;
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

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

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_SERVICE_ACCEPT)
      && (
#if DATAFELLOWS_WORKAROUNDS
	  (connection->peer_flags & PEER_SERVICE_ACCEPT_KLUDGE)
#else
	  0
#endif
	  || (parse_atom(&buffer, &name)
	      && (name == closure->name)))
      && parse_eod(&buffer))
    {
325
326
      struct lsh_string *new_packet;

327
      connection->dispatch[SSH_MSG_SERVICE_ACCEPT] = &connection_fail_handler;
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#if DATAFELLOWS_WORKAROUNDS
      if ((connection->chain->peer_flags & PEER_SERVICE_ACCEPT_KLUDGE) ==
	  (connection->peer_flags & PEER_SERVICE_ACCEPT_KLUDGE))
	new_packet = packet;
      else
	{
	  if (connection->chain->peer_flags & PEER_SERVICE_ACCEPT_KLUDGE)
	    new_packet = ssh_format("%c", SSH_MSG_SERVICE_ACCEPT);
	  else
	    new_packet = ssh_format("%c%a", SSH_MSG_SERVICE_ACCEPT, closure->name);
	  lsh_string_free(packet);
	}
#else
      new_packet = packet;
#endif
343

344
      C_WRITE(connection->chain, new_packet);
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
401
402
403
404
405
      COMMAND_CALL(closure->service,
		   connection->chain,
		   closure->c, closure->e);
    }
  else
    {
      lsh_string_free(packet);
      PROTOCOL_ERROR(closure->e, "Invalid SSH_MSG_SERVICE_ACCEPT message");
    }
}

static struct packet_handler *
make_proxy_accept_service_handler(UINT32 name,
				  struct command *service,
				  struct command_continuation *c,
				  struct exception_handler *e)
{
  NEW(proxy_accept_service_handler, self);

  self->super.handler = do_proxy_accept_service;
  self->name = name;
  self->service = service;
  self->c = c;
  self->e = e;
  return &self->super;
}

/* GABA:
   (class
     (name proxy_service_handler)
     (super packet_handler)
     (vars
       (services object alist)
       (c object command_continuation)
       (e object exception_handler)))
*/

static void
do_proxy_service_request(struct packet_handler *c,
			 struct ssh_connection *connection,
			 struct lsh_string *packet)
{
  CAST(proxy_service_handler, self, c);

  struct simple_buffer buffer;
  unsigned msg_number;
  int name;

  simple_buffer_init(&buffer, packet->length, packet->data);
  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_SERVICE_REQUEST)
      && parse_atom(&buffer, &name)
      && parse_eod(&buffer))
    {
      if (name)
	{
	  CAST_SUBTYPE(command, service, ALIST_GET(self->services, name));
	  if (service)
	    {
	      /* Don't accept any further service requests */
	      connection->dispatch[SSH_MSG_SERVICE_REQUEST]
406
		= &connection_fail_handler;
407
408
409
410
411
412
413
414
415
416
417
418
419
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
460
461
462

	      connection->chain->dispatch[SSH_MSG_SERVICE_ACCEPT]
		= make_proxy_accept_service_handler(name, service, self->c, self->e);

	      C_WRITE(connection->chain, packet);

	      return;
	    }
	}

      EXCEPTION_RAISE(connection->e,
		      make_protocol_exception(SSH_DISCONNECT_SERVICE_NOT_AVAILABLE, NULL));
    }
  else
    {
      lsh_string_free(packet);
      PROTOCOL_ERROR(connection->e, "Invalid SERVICE_REQUEST message");
    }

}

static struct packet_handler *
make_proxy_service_handler(struct alist *services,
			   struct command_continuation *c,
			   struct exception_handler *e)
{
  NEW(proxy_service_handler, self);

  self->super.handler = do_proxy_service_request;
  self->services = services;
  self->c = c;
  self->e = e;
  return &self->super;
}

/* GABA:
   (class
     (name proxy_offer_service)
     (super command)
     (vars
       (services object alist)))
*/

static void
do_proxy_offer_service(struct command *s,
		       struct lsh_object *x,
		       struct command_continuation *c,
		       struct exception_handler *e)
{
  CAST(proxy_offer_service, self, s);
  CAST(ssh_connection, connection, x);

  connection->dispatch[SSH_MSG_SERVICE_REQUEST]
    = make_proxy_service_handler(self->services, c, e);

#if 0
463
  /* currently servers may not ask for services in clients */
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
  connection->chain->dispatch[SSH_MSG_SERVICE_REQUEST]
    = make_proxy_service_request(self->server_services, c, e);
#endif
}

struct command *
make_proxy_offer_service(struct alist *services)
{
  NEW(proxy_offer_service, self);

  self->super.call = do_proxy_offer_service;
  self->services = services;
  return &self->super;
}