client.c 4.78 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
45
46
47
48
49
50
51
52
53
54
55
struct client_callback
{
  struct fd_callback super;
  struct io_backend *backend;
  UINT32 block_size;
  char *id_comment;

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

56
static int client_initiate(struct fd_callback **c,
Niels Möller's avatar
Niels Möller committed
57
			   int fd)
Niels Möller's avatar
Niels Möller committed
58
{
59
  struct client_callback *closure
60
    = (struct client_callback *) *c;
Niels Möller's avatar
Niels Möller committed
61

Niels Möller's avatar
Niels Möller committed
62
63
  int res;
  
Niels Möller's avatar
Niels Möller committed
64
  /* FIXME: Should pass a key exchange handler, not NULL! */
65
  struct ssh_connection *connection
Niels Möller's avatar
Niels Möller committed
66
    = make_ssh_connection(closure->kexinit_handler);
67
68
69

  connection_init_io(connection,
		     io_read_write(closure->backend, fd,
Niels Möller's avatar
Niels Möller committed
70
				   make_client_read_line(connection),
71
72
73
				   closure->block_size,
				   make_client_close_handler()),
		     closure->random);
Niels Möller's avatar
Niels Möller committed
74
  
Niels Möller's avatar
Niels Möller committed
75
  connection->client_version
76
    = ssh_format("SSH-%lz-%lz %lz",
Niels Möller's avatar
Niels Möller committed
77
78
79
		 PROTOCOL_VERSION,
		 SOFTWARE_CLIENT_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->client_version));
Niels Möller's avatar
Niels Möller committed
83
  if (LSH_PROBLEMP(res))
Niels Möller's avatar
Niels Möller committed
84
85
86
87
88
    return res;

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

Niels Möller's avatar
Niels Möller committed
91
struct client_line_handler
Niels Möller's avatar
Niels Möller committed
92
{
Niels Möller's avatar
Niels Möller committed
93
  struct line_handler super;
Niels Möller's avatar
Niels Möller committed
94
  struct ssh_connection *connection;
Niels Möller's avatar
Niels Möller committed
95
};
Niels Möller's avatar
Niels Möller committed
96

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)
{
101
102
  struct client_line_handler *closure
    = (struct client_line_handler *) *h;
103
104

  MDEBUG(closure);
105
  
Niels Möller's avatar
Niels Möller committed
106
  if ( (length >= 4) && !memcmp(line, "SSH-", 4))
Niels Möller's avatar
Niels Möller committed
107
108
    {
      /* Parse and remember format string */
Niels Möller's avatar
Niels Möller committed
109
110
      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
111
	{
112
	  struct read_handler *new = make_read_packet
Niels Möller's avatar
Niels Möller committed
113
114
115
	    (make_packet_unpad
	     (make_packet_debug
	      (&closure->connection->super,
116
	       "")),
117
	     closure->connection);
Niels Möller's avatar
Niels Möller committed
118
	     
Niels Möller's avatar
Niels Möller committed
119
	  closure->connection->server_version
120
	    = ssh_format("%ls", length, line);
Niels Möller's avatar
Niels Möller committed
121

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

	  return new;
Niels Möller's avatar
Niels Möller committed
134
135
136
137
	}
      else
	{
	  werror("Unsupported protocol version: ");
Niels Möller's avatar
Niels Möller committed
138
	  werror_safe(length, line);
Niels Möller's avatar
Niels Möller committed
139
	  werror("\n");
Niels Möller's avatar
Niels Möller committed
140

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

      /* Read next line */
Niels Möller's avatar
Niels Möller committed
154
      return 0;
Niels Möller's avatar
Niels Möller committed
155
156
157
    }
}

Niels Möller's avatar
Niels Möller committed
158
struct read_handler *make_client_read_line(struct ssh_connection *c)
Niels Möller's avatar
Niels Möller committed
159
160
161
{
  struct client_line_handler *closure
    = xalloc(sizeof(struct client_line_handler));
162
163
164

  MDEBUG(closure);

165
  closure->super.handler = do_line;
Niels Möller's avatar
Niels Möller committed
166
  closure->connection = c;
Niels Möller's avatar
Niels Möller committed
167
  
168
  return make_read_line(&closure->super);
Niels Möller's avatar
Niels Möller committed
169
170
}
  
Niels Möller's avatar
Niels Möller committed
171
172
173
174
175
176
177
178
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
179
{
Niels Möller's avatar
Niels Möller committed
180
  struct client_callback *connected = xalloc(sizeof(struct client_callback));
Niels Möller's avatar
Niels Möller committed
181

182
  connected->super.f = client_initiate;
Niels Möller's avatar
Niels Möller committed
183
184
  connected->backend = b;
  connected->block_size = block_size;
Niels Möller's avatar
Niels Möller committed
185
  connected->id_comment = comment;
Niels Möller's avatar
Niels Möller committed
186
187
188
189
190

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

191
  return &connected->super;
Niels Möller's avatar
Niels Möller committed
192
}
Niels Möller's avatar
Niels Möller committed
193
194
195
196
197
198
199

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

Niels Möller's avatar
Niels Möller committed
200
struct callback *make_client_close_handler(void)
Niels Möller's avatar
Niels Möller committed
201
202
203
204
205
206
207
{
  struct callback *c = xalloc(sizeof(struct callback));

  c->f = client_die;

  return c;
}