server.c 4.71 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2
/* server.c
 *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Möller
 *
 * 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.
Niels Möller's avatar
Niels Möller committed
24 25 26 27 28
 */

#include "server.h"

#include "abstract_io.h"
29
#include "connection.h"
Niels Möller's avatar
Niels Möller committed
30 31
#include "debug.h"
#include "format.h"
32 33 34
#include "keyexchange.h"
#include "read_line.h"
#include "read_packet.h"
Niels Möller's avatar
Niels Möller committed
35
#include "unpad.h"
36
#include "version.h"
Niels Möller's avatar
Niels Möller committed
37 38 39
#include "werror.h"
#include "xalloc.h"

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
struct server_callback
{
  struct fd_callback super;
  struct io_backend *backend;

  struct signer *secret;        /* secret key */
  struct lsh_string *host_key;  /* public key */
  UINT32 block_size;
  char *id_comment;

  struct randomness *random;
  struct make_kexinit *init;
  struct packet_handler *kexinit_handler;
};

Niels Möller's avatar
Niels Möller committed
55
static int server_initiate(struct fd_callback **c,
Niels Möller's avatar
Niels Möller committed
56 57
			   int fd)
{
Niels Möller's avatar
Niels Möller committed
58 59
  struct server_callback *closure = (struct server_callback *) *c;
  
60 61 62
  struct ssh_connection *connection
    = make_ssh_connection(closure->kexinit_handler);

Niels Möller's avatar
Niels Möller committed
63 64
  int res;
  
65
  verbose("server_initiate()\n");
Niels Möller's avatar
Niels Möller committed
66 67 68 69 70 71 72 73

  connection_init_io(connection,
		     io_read_write(closure->backend, fd,
				   make_server_read_line(connection),
				   closure->block_size,
				   make_server_close_handler()),
		     closure->random);

Niels Möller's avatar
Niels Möller committed
74 75
  
  connection->server_version
Niels Möller's avatar
Niels Möller committed
76
    = ssh_format("SSH-%lz-%lz %lz",
Niels Möller's avatar
Niels Möller committed
77 78 79
		 PROTOCOL_VERSION,
		 SOFTWARE_SERVER_VERSION,
		 closure->id_comment);
Niels Möller's avatar
Niels Möller committed
80

Niels Möller's avatar
Niels Möller committed
81 82
  res = A_WRITE(connection->raw,
		 ssh_format("%lS\r\n", connection->server_version));
83
  if (LSH_CLOSEDP(res))
Niels Möller's avatar
Niels Möller committed
84 85
    return res;

86 87 88
  return res | initiate_keyexchange(connection, CONNECTION_SERVER,
				    MAKE_KEXINIT(closure->init),
				    NULL);
Niels Möller's avatar
Niels Möller committed
89 90 91 92 93 94 95 96
}

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

Niels Möller's avatar
Niels Möller committed
97
static struct read_handler *do_line(struct line_handler **h,
Niels Möller's avatar
Niels Möller committed
98 99 100
				    UINT32 length,
				    UINT8 *line)
{
Niels Möller's avatar
Niels Möller committed
101 102
  struct server_line_handler *closure = (struct server_line_handler *) *h;
  
Niels Möller's avatar
Niels Möller committed
103 104
  MDEBUG(closure);
  
Niels Möller's avatar
Niels Möller committed
105 106 107 108 109
  if ( (length >= 4) && !memcmp(line, "SSH-", 4))
    {
      /* Parse and remember format string */
      if ((length >= 8) && !memcmp(line + 4, "2.0-", 4))
	{
Niels Möller's avatar
Niels Möller committed
110 111 112
	  struct read_handler *new = make_read_packet
	    (make_packet_unpad
	     (make_packet_debug(&closure->connection->super,
113
				"recieved")),
Niels Möller's avatar
Niels Möller committed
114
	     closure->connection);
Niels Möller's avatar
Niels Möller committed
115 116
	  
	  closure->connection->client_version
Niels Möller's avatar
Niels Möller committed
117
	    = ssh_format("%ls", length, line);
Niels Möller's avatar
Niels Möller committed
118

Niels Möller's avatar
Niels Möller committed
119 120 121 122 123
	  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
124
	  /* FIXME: Cleanup properly. */
Niels Möller's avatar
Niels Möller committed
125
	  lsh_free(closure);
Niels Möller's avatar
Niels Möller committed
126 127 128 129 130 131 132 133 134

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

Niels Möller's avatar
Niels Möller committed
135 136 137 138
	  /* FIXME: Clean up properly */
	  lsh_free(closure);
	  *h = 0;
		  
Niels Möller's avatar
Niels Möller committed
139 140 141 142 143 144 145 146 147 148 149 150
	  return 0;
	}
    }
  else
    {
      /* Display line */
      werror_safe(length, line);

      /* Read next line */
      return 0;
    }
}
Niels Möller's avatar
Niels Möller committed
151

Niels Möller's avatar
Niels Möller committed
152
struct read_handler *make_server_read_line(struct ssh_connection *c)
Niels Möller's avatar
Niels Möller committed
153 154 155 156 157
{
  struct server_line_handler *closure
    = xalloc(sizeof(struct server_line_handler));
  
  closure->super.handler = do_line;
Niels Möller's avatar
Niels Möller committed
158
  closure->connection = c;
Niels Möller's avatar
Niels Möller committed
159 160 161 162
  
  return make_read_line(&closure->super);
}

163 164 165 166
struct fd_callback *
make_server_callback(struct io_backend *b,
		     char *comment,
		     UINT32 block_size,
Niels Möller's avatar
Niels Möller committed
167 168
		     struct randomness *random,
		     struct make_kexinit *init,
169
		     struct packet_handler *kexinit_handler)
Niels Möller's avatar
Niels Möller committed
170 171 172 173 174 175 176
{
  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;
177

Niels Möller's avatar
Niels Möller committed
178 179
  connected->random = random;  
  connected->init = init;
180
  connected->kexinit_handler = kexinit_handler;
Niels Möller's avatar
Niels Möller committed
181 182 183 184
  
  return &connected->super;
}

185
static int server_die(struct close_callback *closure, int reason)
Niels Möller's avatar
Niels Möller committed
186
{
187 188 189 190
  verbose("Connection died, for reason %d.\n", reason);
  if (reason != CLOSE_EOF)
    werror("Connection died.\n");

Niels Möller's avatar
Niels Möller committed
191 192 193
  return 0;  /* Ignored */
}

194
struct close_callback *make_server_close_handler(void)
Niels Möller's avatar
Niels Möller committed
195
{
196
  struct close_callback *c = xalloc(sizeof(struct close_callback));
Niels Möller's avatar
Niels Möller committed
197 198 199 200 201 202

  c->f = server_die;

  return c;
}