client.c 4.51 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2
/* client.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
 */

Niels Möller's avatar
Niels Möller committed
26 27
#include <stdio.h>

Niels Möller's avatar
Niels Möller committed
28
#include "client.h"
29

Niels Möller's avatar
Niels Möller committed
30
#include "abstract_io.h"
31 32
#include "connection.h"
#include "crypto.h"
Niels Möller's avatar
Niels Möller committed
33
#include "debug.h"
Niels Möller's avatar
Niels Möller committed
34
#include "encrypt.h"
35
#include "format.h"
Niels Möller's avatar
Niels Möller committed
36
#include "pad.h"
37 38
#include "read_line.h"
#include "read_packet.h"
Niels Möller's avatar
Niels Möller committed
39
#include "unpad.h"
40 41 42
#include "version.h"
#include "werror.h"
#include "xalloc.h"
Niels Möller's avatar
Niels Möller committed
43

44
static int client_initiate(struct fd_callback **c,
Niels Möller's avatar
Niels Möller committed
45
			   int fd)
Niels Möller's avatar
Niels Möller committed
46
{
47
  struct client_callback *closure
48
    = (struct client_callback *) *c;
Niels Möller's avatar
Niels Möller committed
49

Niels Möller's avatar
Niels Möller committed
50 51
  int res;
  
Niels Möller's avatar
Niels Möller committed
52
  /* FIXME: Should pass a key exchange handler, not NULL! */
53
  struct ssh_connection *connection
Niels Möller's avatar
Niels Möller committed
54
    = make_ssh_connection(closure->kexinit_handler);
55 56 57

  connection_init_io(connection,
		     io_read_write(closure->backend, fd,
Niels Möller's avatar
Niels Möller committed
58
				   make_client_read_line(connection),
59 60 61
				   closure->block_size,
				   make_client_close_handler()),
		     closure->random);
Niels Möller's avatar
Niels Möller committed
62
  
Niels Möller's avatar
Niels Möller committed
63
  connection->client_version
64
    = ssh_format("SSH-%lz-%lz %lz",
Niels Möller's avatar
Niels Möller committed
65 66 67
		 PROTOCOL_VERSION,
		 SOFTWARE_CLIENT_VERSION,
		 closure->id_comment);
Niels Möller's avatar
Niels Möller committed
68
  
Niels Möller's avatar
Niels Möller committed
69 70 71 72 73 74 75 76
  res = A_WRITE(connection->raw,
		ssh_format("%lS\r\n", connection->client_version));
  if (res != WRITE_OK)
    return res;

  return initiate_keyexchange(connection, CONNECTION_CLIENT,
			      MAKE_KEXINIT(closure->init),
			      NULL);
Niels Möller's avatar
Niels Möller committed
77 78
}

Niels Möller's avatar
Niels Möller committed
79
struct client_line_handler
Niels Möller's avatar
Niels Möller committed
80
{
Niels Möller's avatar
Niels Möller committed
81
  struct line_handler super;
Niels Möller's avatar
Niels Möller committed
82
  struct ssh_connection *connection;
Niels Möller's avatar
Niels Möller committed
83
};
Niels Möller's avatar
Niels Möller committed
84

85
static struct read_handler *do_line(struct line_handler **h,
Niels Möller's avatar
Niels Möller committed
86 87 88
				    UINT32 length,
				    UINT8 *line)
{
89 90 91
  struct client_line_handler *closure
    = (struct client_line_handler *) *h;
  
Niels Möller's avatar
Niels Möller committed
92
  if ( (length >= 4) && !memcmp(line, "SSH-", 4))
Niels Möller's avatar
Niels Möller committed
93 94
    {
      /* Parse and remember format string */
Niels Möller's avatar
Niels Möller committed
95 96
      if ( ((length >= 8) && !memcmp(line + 4, "2.0-", 4))
	   || ((length >= 9) && !memcmp(line + 4, "1.99-", 5)))
Niels Möller's avatar
Niels Möller committed
97
	{
98
	  struct read_handler *new = make_read_packet
Niels Möller's avatar
Niels Möller committed
99
	    (make_packet_debug
Niels Möller's avatar
Niels Möller committed
100
	     (make_packet_unpad(&closure->connection->super),
Niels Möller's avatar
Niels Möller committed
101
	      stderr),
102
	     closure->connection);
Niels Möller's avatar
Niels Möller committed
103
	  
Niels Möller's avatar
Niels Möller committed
104
	  closure->connection->server_version
105
	    = ssh_format("%ls", length, line);
Niels Möller's avatar
Niels Möller committed
106

107 108 109 110 111 112 113 114
	  verbose("Client version: ");
	  verbose_safe(closure->connection->client_version->length,
		       closure->connection->client_version->data);
	  verbose("\nServer version: ");
	  verbose_safe(closure->connection->server_version->length,
		       closure->connection->server_version->data);
	  verbose("\n");
	  
Niels Möller's avatar
Niels Möller committed
115
	  /* FIXME: Cleanup properly. */
116
	  lsh_free(closure);
Niels Möller's avatar
Niels Möller committed
117 118

	  return new;
Niels Möller's avatar
Niels Möller committed
119 120 121 122
	}
      else
	{
	  werror("Unsupported protocol version: ");
Niels Möller's avatar
Niels Möller committed
123
	  werror_safe(length, line);
Niels Möller's avatar
Niels Möller committed
124
	  werror("\n");
Niels Möller's avatar
Niels Möller committed
125

126
	  /* FIXME: Clean up properly */
127
	  lsh_free(closure);
Niels Möller's avatar
Niels Möller committed
128
	  *h = NULL;
129
		  
Niels Möller's avatar
Niels Möller committed
130 131 132 133 134 135
	  return 0;
	}
    }
  else
    {
      /* Display line */
Niels Möller's avatar
Niels Möller committed
136
      werror_safe(length, line);
Niels Möller's avatar
Niels Möller committed
137 138

      /* Read next line */
Niels Möller's avatar
Niels Möller committed
139
      return 0;
Niels Möller's avatar
Niels Möller committed
140 141 142
    }
}

Niels Möller's avatar
Niels Möller committed
143
struct read_handler *make_client_read_line(struct ssh_connection *c)
Niels Möller's avatar
Niels Möller committed
144 145 146 147
{
  struct client_line_handler *closure
    = xalloc(sizeof(struct client_line_handler));
  
148
  closure->super.handler = do_line;
Niels Möller's avatar
Niels Möller committed
149
  closure->connection = c;
Niels Möller's avatar
Niels Möller committed
150
  
151
  return make_read_line(&closure->super);
Niels Möller's avatar
Niels Möller committed
152 153
}
  
Niels Möller's avatar
Niels Möller committed
154 155 156 157 158 159 160 161
struct fd_callback *
make_client_callback(struct io_backend *b,
		     char *comment,
		     UINT32 block_size,
		     struct randomness *random,
		     struct make_kexinit *init,
		     struct packet_handler *kexinit_handler)
  
Niels Möller's avatar
Niels Möller committed
162
{
Niels Möller's avatar
Niels Möller committed
163
  struct client_callback *connected = xalloc(sizeof(struct client_callback));
Niels Möller's avatar
Niels Möller committed
164

165
  connected->super.f = client_initiate;
Niels Möller's avatar
Niels Möller committed
166 167
  connected->backend = b;
  connected->block_size = block_size;
Niels Möller's avatar
Niels Möller committed
168
  connected->id_comment = comment;
Niels Möller's avatar
Niels Möller committed
169 170 171 172 173

  connected->random = random;
  connected->init = init;
  connected->kexinit_handler = kexinit_handler;

174
  return &connected->super;
Niels Möller's avatar
Niels Möller committed
175
}
Niels Möller's avatar
Niels Möller committed
176 177 178 179 180 181 182

static int client_die(struct callback *closure)
{
  werror("Connection died.\n");
  exit(1);
}

Niels Möller's avatar
Niels Möller committed
183
struct callback *make_client_close_handler(void)
Niels Möller's avatar
Niels Möller committed
184 185 186 187 188 189 190
{
  struct callback *c = xalloc(sizeof(struct callback));

  c->f = client_die;

  return c;
}