server.c 3.11 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
/* server.c
 *
 */

#include "server.h"

#include "version.h"
#include "connection.h"
#include "abstract_io.h"
#include "read_line.h"
#include "read_packet.h"
#include "debug.h"
#include "format.h"
#include "werror.h"
#include "void.h"
#include "xalloc.h"

struct read_handler *make_server_read_line();
struct callback *make_server_close_handler();

Niels Möller's avatar
Niels Möller committed
21
static int server_initiate(struct fd_callback **c,
Niels Möller's avatar
Niels Möller committed
22 23
			   int fd)
{
Niels Möller's avatar
Niels Möller committed
24 25
  struct server_callback *closure = (struct server_callback *) *c;
  
Niels Möller's avatar
Niels Möller committed
26 27
  /* FIXME: Should pass a key exchange handler, not NULL! */
  struct ssh_connection *connection = make_ssh_connection(NULL);
Niels Möller's avatar
Niels Möller committed
28 29 30 31 32 33 34
  struct abstract_write *write =
    io_read_write(closure->backend, fd,
		  make_server_read_line(),
		  closure->block_size,
		  make_server_close_handler());
  
  connection->server_version
Niels Möller's avatar
Niels Möller committed
35
    = ssh_format("SSH-%lz-%lz %lz",
Niels Möller's avatar
Niels Möller committed
36 37 38
		 PROTOCOL_VERSION,
		 SOFTWARE_SERVER_VERSION,
		 closure->id_comment);
Niels Möller's avatar
Niels Möller committed
39 40

  return A_WRITE(write, ssh_format("%lS\r\n", connection->server_version));
Niels Möller's avatar
Niels Möller committed
41 42 43 44 45 46 47 48
}

struct server_line_handler
{
  struct line_handler super;
  struct ssh_connection *connection;
};

Niels Möller's avatar
Niels Möller committed
49
static struct read_handler *do_line(struct line_handler **h,
Niels Möller's avatar
Niels Möller committed
50 51 52
				    UINT32 length,
				    UINT8 *line)
{
Niels Möller's avatar
Niels Möller committed
53 54
  struct server_line_handler *closure = (struct server_line_handler *) *h;
  
Niels Möller's avatar
Niels Möller committed
55 56 57 58 59 60
  if ( (length >= 4) && !memcmp(line, "SSH-", 4))
    {
      /* Parse and remember format string */
      if ((length >= 8) && !memcmp(line + 4, "2.0-", 4))
	{
	  struct read_handler *new
Niels Möller's avatar
Niels Möller committed
61
	    = make_read_packet(make_packet_debug(&closure->connection->super,
Niels Möller's avatar
Niels Möller committed
62
						 stderr),
Niels Möller's avatar
Niels Möller committed
63 64 65
			       closure->connection->max_packet);
	  
	  closure->connection->client_version
Niels Möller's avatar
Niels Möller committed
66
	    = ssh_format("%ls", length, line);
Niels Möller's avatar
Niels Möller committed
67

Niels Möller's avatar
Niels Möller committed
68 69 70 71 72
	  verbose("Client version: ");
	  verbose_safe(closure->connection->client_version->length,
		       closure->connection->client_version->data);
	  verbose("\n");
	  
Niels Möller's avatar
Niels Möller committed
73
	  /* FIXME: Cleanup properly. */
Niels Möller's avatar
Niels Möller committed
74
	  lsh_free(closure);
Niels Möller's avatar
Niels Möller committed
75 76 77 78 79 80 81 82 83

	  return new;
	}
      else
	{
	  werror("Unsupported protocol version: ");
	  werror_safe(length, line);
	  werror("\n");

Niels Möller's avatar
Niels Möller committed
84 85 86 87
	  /* FIXME: Clean up properly */
	  lsh_free(closure);
	  *h = 0;
		  
Niels Möller's avatar
Niels Möller committed
88 89 90 91 92 93 94 95 96 97 98 99
	  return 0;
	}
    }
  else
    {
      /* Display line */
      werror_safe(length, line);

      /* Read next line */
      return 0;
    }
}
Niels Möller's avatar
Niels Möller committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

struct read_handler *make_server_read_line(struct ssh_connection *s)
{
  struct server_line_handler *closure
    = xalloc(sizeof(struct server_line_handler));
  
  closure->super.handler = do_line;
  closure->connection = s;
  
  return make_read_line(&closure->super);
}

struct fd_callback *make_server_callback(struct io_backend *b,
					 char *comment,
					 UINT32 block_size)
					 
{
  struct server_callback *connected = xalloc(sizeof(struct server_callback));

  connected->super.f = server_initiate;
  connected->backend = b;
  connected->block_size = block_size;
  connected->id_comment = comment;
  
  return &connected->super;
}

static int server_die(struct callback *closure)
{
  werror("Connection died.\n");
  /* FIXME: Cleanup properly. */
  return 0;  /* Ignored */
}

struct callback *make_server_close_handler()
{
  struct callback *c = xalloc(sizeof(struct callback));

  c->f = server_die;

  return c;
}