client.c 3.22 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
/* client.c
 *
 */

Niels Möller's avatar
Niels Möller committed
5
6
#include <stdio.h>

Niels Möller's avatar
Niels Möller committed
7
8
#include "client.h"
#include "version.h"
Niels Möller's avatar
Niels Möller committed
9
#include "connection.h"
Niels Möller's avatar
Niels Möller committed
10
11
12
13
14
15
#include "abstract_io.h"
#include "read_line.h"
#include "read_packet.h"
#include "debug.h"
#include "format.h"
#include "werror.h"
Niels Möller's avatar
Niels Möller committed
16
#include "void.h"
Niels Möller's avatar
Niels Möller committed
17
18
19
20
#include "xalloc.h"

struct read_handler *make_client_read_line();
struct callback *make_client_close_handler();
Niels Möller's avatar
Niels Möller committed
21

22
static int client_initiate(struct fd_callback **c,
Niels Möller's avatar
Niels Möller committed
23
			   int fd)
Niels Möller's avatar
Niels Möller committed
24
{
25
  struct client_callback *closure
26
    = (struct client_callback *) *c;
27
  
Niels Möller's avatar
Niels Möller committed
28
  struct ssh_connection *connection = ssh_connection_alloc();
Niels Möller's avatar
Niels Möller committed
29
30
31
32
33
34
  struct abstract_write *write =
    io_read_write(closure->backend, fd,
		  make_client_read_line(),
		  closure->block_size,
		  make_client_close_handler());
  
Niels Möller's avatar
Niels Möller committed
35
  connection->client_version
36
    = ssh_format("SSH-%lz-%lz %lz",
Niels Möller's avatar
Niels Möller committed
37
38
39
		 PROTOCOL_VERSION,
		 SOFTWARE_CLIENT_VERSION,
		 closure->id_comment);
40
41

  return A_WRITE(write, ssh_format("%lS\r\n", connection->client_version));
Niels Möller's avatar
Niels Möller committed
42
43
}

Niels Möller's avatar
Niels Möller committed
44
struct client_line_handler
Niels Möller's avatar
Niels Möller committed
45
{
Niels Möller's avatar
Niels Möller committed
46
  struct line_handler super;
Niels Möller's avatar
Niels Möller committed
47
  struct ssh_connection *connection;
Niels Möller's avatar
Niels Möller committed
48
};
Niels Möller's avatar
Niels Möller committed
49

50
static struct read_handler *do_line(struct line_handler **h,
Niels Möller's avatar
Niels Möller committed
51
52
53
				    UINT32 length,
				    UINT8 *line)
{
54
55
56
  struct client_line_handler *closure
    = (struct client_line_handler *) *h;
  
Niels Möller's avatar
Niels Möller committed
57
  if ( (length >= 4) && !memcmp(line, "SSH-", 4))
Niels Möller's avatar
Niels Möller committed
58
59
    {
      /* Parse and remember format string */
Niels Möller's avatar
Niels Möller committed
60
61
      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
62
	{
Niels Möller's avatar
Niels Möller committed
63
	  struct read_handler *new
64
65
	    = make_read_packet(make_packet_debug(make_packet_void(),
						 stderr),
Niels Möller's avatar
Niels Möller committed
66
			       closure->connection->max_packet);
Niels Möller's avatar
Niels Möller committed
67
	  
Niels Möller's avatar
Niels Möller committed
68
	  closure->connection->server_version
69
	    = ssh_format("%ls", length, line);
Niels Möller's avatar
Niels Möller committed
70

71
72
73
74
75
76
77
78
	  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
79
	  /* FIXME: Cleanup properly. */
80
	  lsh_free(closure);
Niels Möller's avatar
Niels Möller committed
81
82

	  return new;
Niels Möller's avatar
Niels Möller committed
83
84
85
86
	}
      else
	{
	  werror("Unsupported protocol version: ");
Niels Möller's avatar
Niels Möller committed
87
	  werror_safe(length, line);
Niels Möller's avatar
Niels Möller committed
88
	  werror("\n");
Niels Möller's avatar
Niels Möller committed
89

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

      /* Read next line */
Niels Möller's avatar
Niels Möller committed
103
      return 0;
Niels Möller's avatar
Niels Möller committed
104
105
106
    }
}

Niels Möller's avatar
Niels Möller committed
107
struct read_handler *make_client_read_line(struct ssh_connection *s)
Niels Möller's avatar
Niels Möller committed
108
109
110
111
{
  struct client_line_handler *closure
    = xalloc(sizeof(struct client_line_handler));
  
112
  closure->super.handler = do_line;
Niels Möller's avatar
Niels Möller committed
113
  closure->connection = s;
Niels Möller's avatar
Niels Möller committed
114
  
115
  return make_read_line(&closure->super);
Niels Möller's avatar
Niels Möller committed
116
117
}
  
Niels Möller's avatar
Niels Möller committed
118
struct fd_callback *make_client_callback(struct io_backend *b,
Niels Möller's avatar
Niels Möller committed
119
					 char *comment,
Niels Möller's avatar
Niels Möller committed
120
					 UINT32 block_size)
Niels Möller's avatar
Niels Möller committed
121
					 
Niels Möller's avatar
Niels Möller committed
122
{
Niels Möller's avatar
Niels Möller committed
123
  struct client_callback *connected = xalloc(sizeof(struct client_callback));
Niels Möller's avatar
Niels Möller committed
124

125
  connected->super.f = client_initiate;
Niels Möller's avatar
Niels Möller committed
126
127
  connected->backend = b;
  connected->block_size = block_size;
Niels Möller's avatar
Niels Möller committed
128
  connected->id_comment = comment;
Niels Möller's avatar
Niels Möller committed
129
  
130
  return &connected->super;
Niels Möller's avatar
Niels Möller committed
131
}
Niels Möller's avatar
Niels Möller committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

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

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

  c->f = client_die;

  return c;
}