lsh_proxy.c 21.6 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 27 28 29 30 31
/* lsh_proxy.c
 *
 * main proxy program.
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998, 1999 Niels Mller, Balzs 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "algorithms.h"
#include "alist.h"
#include "atoms.h"
#include "channel.h"
#include "channel_commands.h"
#include "charset.h"
32
#include "client_keyexchange.h"
33 34 35 36
#include "compress.h"
#include "connection_commands.h"
#include "crypto.h"
#include "daemon.h"
37
#include "dsa.h"
38
#include "format.h"
Niels Möller's avatar
Niels Möller committed
39
#include "gateway_channel.h"
40
#include "handshake.h"
41 42 43
#include "io.h"
#include "io_commands.h"
#include "lookup_verifier.h"
44
#include "proxy.h"
45
#include "proxy_agentforward.h"
46
#include "proxy_session.h"
47
#include "proxy_tcpforward.h"
48
#include "proxy_userauth.h"
49
#include "proxy_x11forward.h"
50 51 52 53 54 55
#include "randomness.h"
#include "reaper.h"
#include "server.h"
#include "server_authorization.h"
#include "server_keyexchange.h"
#include "server_session.h"
56
#include "server_userauth.h"
57 58
#include "sexp.h"
#include "sexp_commands.h"
59
#include "spki.h"
60
#include "spki_commands.h"
61 62 63 64
#include "ssh.h"
#include "version.h"
#include "werror.h"
#include "xalloc.h"
65 66 67

#include "lsh_argp.h"

68 69 70 71 72 73 74 75 76 77 78
/* Forward declarations */
struct command_simple options2local;
#define OPTIONS2LOCAL (&options2local.super.super)

static struct command options2keyfile;
#define OPTIONS2KEYFILE (&options2keyfile.super)

struct command_simple options2signature_algorithms;
#define OPTIONS2SIGNATURE_ALGORITHMS \
  (&options2signature_algorithms.super.super)

79 80 81 82
struct command_simple proxy_destination;
#define PROXY_DESTINATION (&proxy_destination.super.super)


83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#include "lsh_proxy.c.x"

#include <assert.h>

#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif

/* Option parsing */

101 102 103 104 105
const char *argp_program_version
= "lsh_proxy-" VERSION;

const char *argp_program_bug_address = BUG_ADDRESS;

106 107 108 109 110
#define OPT_NO 0x400
#define OPT_SSH1_FALLBACK 0x200
#define OPT_INTERFACE 0x201
#define OPT_TCPIP_FORWARD 0x202
#define OPT_NO_TCPIP_FORWARD (OPT_TCPIP_FORWARD | OPT_NO)
111 112 113 114 115
#define OPT_X11_FORWARD 0x203
#define OPT_NO_X11_FORWARD (OPT_X11_FORWARD | OPT_NO)
#define OPT_AGENT_FORWARD  0x204
#define OPT_NO_AGENT_FORWARD (OPT_AGENT_FORWARD | OPT_NO)
#define OPT_DAEMONIC 0x205
116
#define OPT_NO_DAEMONIC (OPT_DAEMONIC | OPT_NO)
117
#define OPT_PIDFILE 0x206
118 119 120 121 122 123 124 125
#define OPT_NO_PIDFILE (OPT_PIDFILE | OPT_NO)
#define OPT_CORE 0x207

/* GABA:
   (class
     (name lsh_proxy_options)
     (super algorithms_options)
     (vars
126
       (backend object io_backend)
127
       (random object randomness_with_poll)
128
       (signature_algorithms object alist)
129 130 131 132 133
       (style . sexp_argp_state)
       (interface . "char *")
       (port . "char *")
       (hostkey . "char *")
       (local object address_info)
134
       (destination object address_info)
135
       (with_tcpip_forward . int)
136 137
       (with_x11_forward . int)
       (with_agent_forward . int)
138 139 140 141 142 143 144 145
       (daemonic . int)
       (corefile . int)
       (pid_file . "const char *")
       ; -1 means use pid file iff we're in daemonic mode
       (use_pid_file . int)))
*/

static struct lsh_proxy_options *
146
make_lsh_proxy_options(struct io_backend *backend, 
147
		       struct randomness_with_poll *random, 
148
		       struct alist *algorithms)
149 150 151 152
{
  NEW(lsh_proxy_options, self);

  init_algorithms_options(&self->super, algorithms);
153 154
  self->signature_algorithms
    = make_alist(1,
155
		 ATOM_DSA, make_dsa_algorithm(&random->super), -1);
156 157

  self->backend = backend;
158
  self->random = random;
159 160
  self->style = SEXP_TRANSPORT;
  self->interface = NULL;
161 162 163 164

  /* Default behaviour is to lookup the "ssh" service, and fall back
   * to port 22 if that fails. */
  self->port = NULL;
165

166 167 168 169 170
  /* FIXME: this should perhaps use sysconfdir */  
  self->hostkey = "/etc/lsh_host_key";
  self->local = NULL;

  self->with_tcpip_forward = 1;
171 172
  self->with_x11_forward = 1;
  self->with_agent_forward = 1;
173 174 175 176 177 178 179 180 181 182 183
  
  self->daemonic = 0;

  /* FIXME: Make the default a configure time option? */
  self->pid_file = "/var/run/lsh_proxy.pid";
  self->use_pid_file = -1;
  self->corefile = 0;
  
  return self;
}

184
/* Port to listen on */
185
DEFINE_COMMAND_SIMPLE(options2local, a)
186 187 188 189 190 191
{
  CAST(lsh_proxy_options, options, a);
  return &options->local->super;
}

/* alist of signature algorithms */
192
DEFINE_COMMAND_SIMPLE(options2signature_algorithms, a)
193 194 195 196 197 198 199 200 201 202 203 204 205 206
{
  CAST(lsh_proxy_options, options, a);
  return &options->signature_algorithms->super;
}

/* Read server's private key */
static void
do_options2keyfile(struct command *ignored UNUSED,
		   struct lsh_object *a,
		   struct command_continuation *c,
		   struct exception_handler *e)
{
  CAST(lsh_proxy_options, options, a);
  
Niels Möller's avatar
Niels Möller committed
207
  struct lsh_fd *f;
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

  f = io_read_file(options->backend, options->hostkey, e);

  if (f)
    COMMAND_RETURN(c, f);
  else
    {
      werror("Failed to open '%z' (errno = %i): %z.\n",
	     options->hostkey, errno, STRERROR(errno));
      EXCEPTION_RAISE(e, make_io_exception(EXC_IO_OPEN_READ, NULL, errno, NULL));
    }
}

static struct command options2keyfile =
STATIC_COMMAND(do_options2keyfile);

224 225 226 227 228 229 230 231 232 233 234 235 236 237
static const struct argp_option
main_options[] =
{
  /* Name, key, arg-name, flags, doc, group */
  { "interface", OPT_INTERFACE, "interface", 0,
    "Listen on this network interface", 0 }, 
  { "port", 'p', "Port", 0, "Listen on this port.", 0 },
  { "host-key", 'h', "Key file", 0, "Location of the server's private key.", 0},
  { "destination", 'D', "destination:port", 0, "Destination ssh server address (transparent if not given)", 0 },

#if WITH_TCP_FORWARD
  { "tcp-forward", OPT_TCPIP_FORWARD, NULL, 0, "Enable tcpip forwarding (default).", 0 },
  { "no-tcp-forward", OPT_NO_TCPIP_FORWARD, NULL, 0, "Disable tcpip forwarding.", 0 },
#endif /* WITH_TCP_FORWARD */
238 239 240 241 242 243 244 245
#if WITH_X11_FORWARD
  { "x11-forward", OPT_X11_FORWARD, NULL, 0, "Enable X11 forwarding (default).", 0 },
  { "no-x11-forward", OPT_NO_X11_FORWARD, NULL, 0, "Disable X11 forwarding.", 0 },
#endif
#if WITH_AGENT_FORWARD
  { "agent-forward", OPT_AGENT_FORWARD, NULL, 0, "Enable auth agent forwarding (default).", 0 },
  { "no-agent-forward", OPT_NO_AGENT_FORWARD, NULL, 0, "Disable auth agent forwarding.", 0 },
#endif
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

  { NULL, 0, NULL, 0, "Daemonic behaviour", 0 },
  { "daemonic", OPT_DAEMONIC, NULL, 0, "Run in the background, redirect stdio to /dev/null, and chdir to /.", 0 },
  { "no-daemonic", OPT_NO_DAEMONIC, NULL, 0, "Run in the foreground, with messages to stderr (default).", 0 },
  { "pid-file", OPT_PIDFILE, "file name", 0, "Create a pid file. When running in daemonic mode, "
    "the default is /var/run/lsh_proxy.pid.", 0 },
  { "no-pid-file", OPT_NO_PIDFILE, NULL, 0, "Don't use any pid file. Default in non-daemonic mode.", 0 },
  { "enable-core", OPT_CORE, NULL, 0, "Dump core on fatal errors (disabled by default).", 0 },
    
  { NULL, 0, NULL, 0, NULL, 0 }
};

static const struct argp_child
main_argp_children[] =
{
  { &sexp_input_argp, 0, "", 0 },
  { &algorithms_argp, 0, "", 0 },
  { &werror_argp, 0, "", 0 },
  { NULL, 0, NULL, 0}
};

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
static int 
parse_dest_arg(char *arg,
	       struct address_info **target)
{
  char *sep, *end;
  int port;
  
  sep = strchr(arg, ':');
  if (!sep)
    return 0;
    
  port = strtol(sep + 1, &end, 0);
  if ( (end == sep + 1) || (*end != '\0')
       || (port < 0) || (port > 0xffff) )
    return 0;

  *target = make_address_info(ssh_format("%ls", sep - arg, arg), port);
  return 1;
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static error_t
main_argp_parser(int key, char *arg, struct argp_state *state)
{
  CAST(lsh_proxy_options, self, state->input);
  
  switch(key)
    {
    default:
      return ARGP_ERR_UNKNOWN;
    case ARGP_KEY_INIT:
      state->child_inputs[0] = &self->style;
      state->child_inputs[1] = &self->super;
      state->child_inputs[2] = NULL;
      break;
    case ARGP_KEY_ARG:
      argp_error(state, "Spurious arguments.");
      break;
      
    case ARGP_KEY_END:
306 307 308 309 310
      if (self->port)
	self->local = make_address_info_c(arg, self->port, 0);
      else
	self->local = make_address_info_c(arg, "ssh", 22);

311 312 313 314 315 316
      if (!self->local)
	argp_error(state, "Invalid interface, port or service, %s:%s'.",
		   self->interface ? self->interface : "ANY",
		   self->port);
      if (self->use_pid_file < 0)
	self->use_pid_file = self->daemonic;
317 318 319

      /* Start background poll */
      RANDOM_POLL_BACKGROUND(self->random->poller);
320 321 322 323 324 325 326 327 328 329
      
      break;
      
    case 'p':
      self->port = arg;
      break;

    case 'h':
      self->hostkey = arg;
      break;
330 331 332 333 334 335 336 337
      
    case 'D':
      if (!parse_dest_arg(arg, &self->destination))
        {
          argp_error(state, "Invalid destination specification '%s'.", arg);
        }
      
      break;
338 339 340 341 342 343 344 345 346 347 348 349 350

    case OPT_INTERFACE:
      self->interface = arg;
      break;
 
    case OPT_TCPIP_FORWARD:
      self->with_tcpip_forward = 1;
      break;

    case OPT_NO_TCPIP_FORWARD:
      self->with_tcpip_forward = 0;
      break;

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    case OPT_X11_FORWARD:
      self->with_x11_forward = 1;
      break;

    case OPT_NO_X11_FORWARD:
      self->with_x11_forward = 0;
      break;

    case OPT_AGENT_FORWARD:
      self->with_agent_forward = 1;
      break;

    case OPT_NO_AGENT_FORWARD:
      self->with_agent_forward = 0;
      break;

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 406 407 408 409 410
    case OPT_DAEMONIC:
      self->daemonic = 1;
      break;

    case OPT_NO_DAEMONIC:
      self->daemonic = 0;
      break;

    case OPT_PIDFILE:
      self->pid_file = arg;
      self->use_pid_file = 1;
      break;

    case OPT_NO_PIDFILE:
      self->use_pid_file = 0;
      break;

    case OPT_CORE:
      self->corefile = 1;
      break;
    }
  return 0;
}

static const struct argp
main_argp =
{ main_options, main_argp_parser, 
  NULL,
  "Server for the ssh-2 protocol.",
  main_argp_children,
  NULL, NULL
};

/* GABA:
   (class
     (name fake_host_db)
     (super lookup_verifier)
     (vars
       ;; (algorithm object signature_algorithm)
       ))
*/

static struct verifier *
do_host_lookup(struct lookup_verifier *c UNUSED,
411
	       int method,
412
               struct lsh_user *user UNUSED,
413 414
               struct lsh_string *key)
{
415 416
  switch(method)
    {
417 418
    case ATOM_SSH_DSS:
	return make_ssh_dss_verifier(key->length, key->data);
419 420 421
    default:
      return NULL;
    }
422 423 424 425 426 427 428 429 430 431 432 433
}

static struct lookup_verifier *
make_fake_host_db(void)
{
  NEW(fake_host_db, res);

  res->super.lookup = do_host_lookup;

  return &res->super;
}

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
/* GABA:
   (class
     (name proxy_destination)
     (super command)
     (vars
       (options object lsh_proxy_options)))
*/

static void
do_proxy_destination(struct command *s,
		     struct lsh_object *x,
		     struct command_continuation *c,
		     struct exception_handler *e)
{
  CAST(proxy_destination, closure, s);
  CAST(listen_value, client_addr, x);

  if (closure->options->destination)
    COMMAND_RETURN(c, closure->options->destination);
  else
    {
455
      /* FIXME: Why not use client_addr->peer? /nisse*/
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
      struct sockaddr_in sa;
      int salen = sizeof(sa);

      /* try to be transparent */
      /* FIXME: support non AF_INET address families */
      if (getsockname(client_addr->fd->fd, (struct sockaddr *) &sa, &salen) != -1)
        {
          COMMAND_RETURN(c,
			 sockaddr2info(salen, (struct sockaddr *) &sa));
          /* a = make_address_info(ssh_format("localhost"), 1998); */
        }
      else
	{
	  struct exception *ex = 
	    make_io_exception(EXC_IO, client_addr->fd, errno, "getsockname() failed");

	  EXCEPTION_RAISE(e, ex);
	}
    }
}

static struct command *
make_proxy_destination_command(struct lsh_proxy_options *options)
{
  NEW(proxy_destination, self);

  self->super.call = do_proxy_destination;
  self->options = options;
  return &self->super;
}

487
DEFINE_COMMAND_SIMPLE(proxy_destination, a)
488 489 490 491 492
{
  CAST(lsh_proxy_options, options, a);
  return &make_proxy_destination_command(options)->super;
}

493 494 495 496 497 498
/* GABA:
   (expr
     (name lsh_proxy_listen)
     (params
       (listen object command)
       (services object command)
499 500
       (handshake_server object command)
       (handshake_client object command))
501
     (expr 
502
       (lambda (options)
503
         (services 
504 505 506 507
	   (let 
	     ; accept a connection
             ((client_addr (listen(options2local options)))) 
	     ; chain two connections
508
	     (chain_connections 
509 510 511 512 513 514 515 516 517 518
	       (handshake_server options)   ; callback to perform server side handshake
	       (handshake_client options)   ; callback to perform client side handshake
	       client_addr))))))	    ; address of accepted connection
	     
*/

/* GABA:
   (expr
     (name lsh_proxy_handshake_client)
     (params
519 520
       (handshake object handshake_info)
       (init object make_kexinit))
521 522 523
     (expr
       (lambda (options client_addr)
         (connection_handshake    ; handshake on the client side
524
            handshake init
525 526 527 528
	    (spki_read_hostkeys (options2signature_algorithms options)
	                        (options2keyfile options)) 
	    (log_peer client_addr)))))

529 530 531 532
*/

/* GABA:
   (expr
533
     (name lsh_proxy_handshake_server)
534 535
     (params
       (connect object command)
536
       (verifier object lookup_verifier)
537 538
       (handshake object handshake_info)
       (init object make_kexinit))
539
     (expr 
540
       (lambda (options client_addr)
541
         (init_connection_service 
542
	   (connection_handshake 
543
             handshake init
544 545
             verifier 
             (connect (proxy_destination options client_addr)))))))
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
*/

/* Invoked when the client requests the userauth service. */
/* GABA:
   (expr
     (name lsh_proxy_services)
     (params 
       (userauth object command))
     (expr
       (lambda (connection)
         ((userauth connection) connection))))
*/

/* Invoked when starting the ssh-connection service */
/* GABA:
   (expr
     (name lsh_proxy_connection_service)
     (params
564
       (login object command))
565 566
     (expr
       (lambda (user connection)
567 568 569 570
         (login user
	    ; We have to initialize the connection
	    ; before logging in.
	    (init_connection_service connection)))))
571 572
*/

573 574 575 576
#if WITH_ALF
#include "proxy_alf.h"
#endif

577

578 579 580 581 582 583 584 585
int main(int argc, char **argv)
{
  struct lsh_proxy_options *options;
  
  struct alist *keys;
  
  struct reap *reaper;
  
586
  struct randomness_with_poll *r;
587
  struct alist *algorithms_server, *algorithms_client;
588 589
  struct alist *signature_algorithms;
  
590
  struct make_kexinit *make_kexinit;
591
  struct exception_handler *handler;
592 593 594 595

  struct io_backend *backend;

  backend = make_io_backend();
596

597 598 599 600 601 602
#if WITH_ALF
  proxy_alf_init();
  proxy_alf_end();
#endif


603 604 605 606 607 608
  /* For filtering messages. Could perhaps also be used when converting
   * strings to and from UTF8. */
  setlocale(LC_CTYPE, "");
  /* FIXME: Choose character set depending on the locale */
  set_local_charset(CHARSET_LATIN1);

609 610 611 612
  handler = make_report_exception_handler
    (make_report_exception_info(EXC_IO, EXC_IO, "lsh_proxy: "),
     &default_exception_handler,
     HANDLER_CONTEXT);
613
  reaper = make_reaper(backend); 
614
  r = make_default_random(reaper, handler);
615

616 617 618 619
  algorithms_server = all_symmetric_algorithms();
  /* FIXME: copy algorithms_server */
  algorithms_client = all_symmetric_algorithms();
  
620
  signature_algorithms = all_signature_algorithms(&r->super);
621
  
622
  options = make_lsh_proxy_options(backend, r, algorithms_server);
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 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
  
  argp_parse(&main_argp, argc, argv, 0, NULL, options);

  if (!options->corefile && !daemon_disable_core())
    {
      werror("Disabling of core dumps failed.\n");
      return EXIT_FAILURE;
    }
  
  if (options->daemonic)
    {
#if HAVE_SYSLOG
      set_error_syslog("lsh_proxy");
#else /* !HAVE_SYSLOG */
      werror("lsh_proxy: No syslog. Further messages will be directed to /dev/null.\n");
#endif /* !HAVE_SYSLOG */
    }

  if (options->daemonic)
    switch (daemon_init())
      {
      case 0:
	werror("lsh_proxy: Spawning into background failed.\n");
	return EXIT_FAILURE;
      case DAEMON_INETD:
	werror("lsh_proxy: spawning from inetd not yet supported.\n");
	return EXIT_FAILURE;
      case DAEMON_INIT:
      case DAEMON_NORMAL:
	break;
      default:
	fatal("Internal error\n");
      }
  
  if (options->use_pid_file && !daemon_pidfile(options->pid_file))
    {
      werror("lsh_proxy seems to be running already.\n");
      return EXIT_FAILURE;
    }
      
  /* Read the hostkey */
  keys = make_alist(0, -1);
665
#if 0
666 667 668 669 670 671 672 673
  if (!(hostkey = read_spki_key_file(options->hostkey,
				     make_alist(1, ATOM_DSA, make_dsa_algorithm(r), -1),
				     &ignore_exception_handler)))
    {
      werror("lsh_proxy: Could not read hostkey.\n");
      return EXIT_FAILURE;
    }
  ALIST_SET(keys, hostkey->type, hostkey);
674
#endif
675 676 677 678 679

  /* FIXME: We should check that we have at least one host key.
   * We should also extract the host-key algorithms for which we have keys,
   * instead of hardcoding ssh-dss below. */
 
680
#if 0
681 682 683
  lookup_keys = make_alist(1, 
			   ATOM_SSH_DSS, make_fake_host_db(),
			   -1);
684
#endif
685

686
  ALIST_SET(algorithms_server, 
687
	    ATOM_DIFFIE_HELLMAN_GROUP1_SHA1,
688
	    &make_dh_server(make_dh1(&r->super))->super);
689
  ALIST_SET(algorithms_client, 
690
	    ATOM_DIFFIE_HELLMAN_GROUP1_SHA1,
691
	    &make_dh_client(make_dh1(&r->super))->super);
692
  
693
  make_kexinit
694
    = make_simple_kexinit(&r->super,
695 696
			  make_int_list(1, ATOM_DIFFIE_HELLMAN_GROUP1_SHA1,
					-1),
697
			  options->super.hostkey_algorithms,
698 699 700 701 702 703 704
			  options->super.crypto_algorithms,
			  options->super.mac_algorithms,
			  options->super.compression_algorithms,
			  make_int_list(0, -1));
  
  {
    /* Commands to be invoked on the connection */
705 706 707 708 709
    struct object_queue connection_server_hooks, connection_client_hooks;
    struct alist *server_session_requests, *client_session_requests;

    server_session_requests = make_alist
      (4,
Niels Möller's avatar
Niels Möller committed
710 711 712 713
       ATOM_SHELL, &gateway_channel_request, 
       ATOM_PTY_REQ, &gateway_channel_request,
       ATOM_EXIT_STATUS, &gateway_channel_request,
       ATOM_EXIT_SIGNAL, &gateway_channel_request,
714 715 716 717 718 719
       -1);
    client_session_requests = make_alist(0, -1);

#if WITH_X11_FORWARD
    if (options->with_x11_forward)
      {
Niels Möller's avatar
Niels Möller committed
720
	ALIST_SET(server_session_requests, ATOM_X11_REQ, &gateway_channel_request);
721 722 723 724 725
      }
#endif
#if WITH_AGENT_FORWARD
    if (options->with_agent_forward)
      {
Niels Möller's avatar
Niels Möller committed
726
	ALIST_SET(server_session_requests, ATOM_AUTH_AGENT_REQ, &gateway_channel_request);
727 728 729 730 731 732 733 734 735 736 737 738
      }
#endif

    object_queue_init(&connection_server_hooks);
    object_queue_init(&connection_client_hooks);

    object_queue_add_tail(&connection_server_hooks,
			  &make_install_fix_channel_open_handler
			  (ATOM_SESSION, 
			   make_proxy_open_session(server_session_requests,
						   client_session_requests))->super);

739 740
#ifdef WITH_TCP_FORWARD
    if (options->with_tcpip_forward)
741 742 743
      {
	object_queue_add_tail(&connection_server_hooks,
			      &make_install_fix_global_request_handler
Niels Möller's avatar
Niels Möller committed
744
			      (ATOM_TCPIP_FORWARD, &gateway_global_request)->super);
745 746
	object_queue_add_tail(&connection_server_hooks,
			      &make_install_fix_global_request_handler
Niels Möller's avatar
Niels Möller committed
747
			      (ATOM_CANCEL_TCPIP_FORWARD, &gateway_global_request)->super);
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
	object_queue_add_tail(&connection_server_hooks,
			      &make_install_fix_channel_open_handler
			      (ATOM_DIRECT_TCPIP,
			       make_proxy_open_direct_tcpip())->super);
	
	object_queue_add_tail(&connection_client_hooks,
			      &make_install_fix_channel_open_handler
			      (ATOM_FORWARDED_TCPIP,
			       make_proxy_open_forwarded_tcpip())->super);
      }
#endif

#if WITH_X11_FORWARD
    if (options->with_x11_forward)
      {
	object_queue_add_tail(&connection_client_hooks,
			      &make_install_fix_channel_open_handler
			      (ATOM_X11,
			       make_proxy_open_x11())->super);
      }
#endif

#if WITH_AGENT_FORWARD
    if (options->with_agent_forward)
      {
	object_queue_add_tail(&connection_client_hooks,
			      &make_install_fix_channel_open_handler
			      (ATOM_AUTH_AGENT,
			       make_proxy_open_auth_agent())->super);
      }
778 779
#endif
      
780 781 782
    {
      struct lsh_object *o = lsh_proxy_listen
	(make_simple_listen(backend, NULL),
783 784 785 786 787 788 789 790 791 792 793 794
	 make_proxy_offer_service
	 (make_alist(1, 
		     ATOM_SSH_USERAUTH, 
		     lsh_proxy_services
		     (make_proxy_userauth
		      (make_int_list(1, ATOM_PASSWORD, -1), 
		       make_alist(1, ATOM_PASSWORD, &proxy_password_auth, -1),
		       make_alist
		       (1, 
			ATOM_SSH_CONNECTION,
			lsh_proxy_connection_service
			(make_proxy_connection_service
795 796
			 (queue_to_list_and_kill(&connection_server_hooks),
			  queue_to_list_and_kill(&connection_client_hooks))),
797
			-1))),
798
		      -1)),
799 800

	 /* callback to call when client<->proxy handshake finished */
801 802 803 804 805 806 807
	 (struct command *)lsh_proxy_handshake_server(make_simple_connect(backend, NULL),
						      make_fake_host_db(),
						      make_handshake_info
						      (CONNECTION_CLIENT,
						       "lsh_proxy_client - a free ssh",
						       "proxy client",
						       SSH_MAX_PACKET,
808
						       &r->super,
809
						       algorithms_client,
810 811
						       NULL),
						      make_kexinit),
812 813 814 815 816
	 (struct command *)lsh_proxy_handshake_client(make_handshake_info
						      (CONNECTION_SERVER,
						       "lsh_proxy_server - a free ssh",
						       "proxy server",
						       SSH_MAX_PACKET,
817
						       &r->super,
818
						       algorithms_server,
819 820
						       NULL),
						      make_kexinit));
821 822 823 824


    
      CAST_SUBTYPE(command, server_listen, o);
825

826
      COMMAND_CALL(server_listen, options,
827
                   &discard_continuation,
828
		   handler);
829 830 831
    }
  }
  
832
  io_run(backend);
833 834 835

  return 0;
}