client.c 2.95 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
26
27
  struct client_callback *closure
    = (struct client_callback *) c;
  
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
36
37
38
39
  connection->client_version
    = ssh_format("SSH-%z-%z %z\r\n",
		 PROTOCOL_VERSION,
		 SOFTWARE_CLIENT_VERSION,
		 closure->id_comment);
Niels Möller's avatar
Niels Möller committed
40
  /* Copies the version string, so that it is isn't freed */
Niels Möller's avatar
Niels Möller committed
41
  return A_WRITE(write, ssh_format("%lS", 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
Niels Möller's avatar
Niels Möller committed
69
	    = ssh_format("%s", length, line);
Niels Möller's avatar
Niels Möller committed
70

Niels Möller's avatar
Niels Möller committed
71
72
73
74
	  /* FIXME: Cleanup properly. */
	  free(closure);

	  return new;
Niels Möller's avatar
Niels Möller committed
75
76
77
78
	}
      else
	{
	  werror("Unsupported protocol version: ");
Niels Möller's avatar
Niels Möller committed
79
	  werror_safe(length, line);
Niels Möller's avatar
Niels Möller committed
80
	  werror("\n");
Niels Möller's avatar
Niels Möller committed
81

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

      /* Read next line */
Niels Möller's avatar
Niels Möller committed
95
      return 0;
Niels Möller's avatar
Niels Möller committed
96
97
98
    }
}

Niels Möller's avatar
Niels Möller committed
99
struct read_handler *make_client_read_line(struct ssh_connection *s)
Niels Möller's avatar
Niels Möller committed
100
101
102
103
{
  struct client_line_handler *closure
    = xalloc(sizeof(struct client_line_handler));
  
104
  closure->super.handler = do_line;
Niels Möller's avatar
Niels Möller committed
105
  closure->connection = s;
Niels Möller's avatar
Niels Möller committed
106
  
107
  return make_read_line(&closure->super);
Niels Möller's avatar
Niels Möller committed
108
109
}
  
Niels Möller's avatar
Niels Möller committed
110
struct fd_callback *make_client_callback(struct io_backend *b,
Niels Möller's avatar
Niels Möller committed
111
					 char *comment,
Niels Möller's avatar
Niels Möller committed
112
					 UINT32 block_size)
Niels Möller's avatar
Niels Möller committed
113
					 
Niels Möller's avatar
Niels Möller committed
114
{
Niels Möller's avatar
Niels Möller committed
115
  struct client_callback *connected = xalloc(sizeof(struct client_callback));
Niels Möller's avatar
Niels Möller committed
116

117
  connected->super.f = client_initiate;
Niels Möller's avatar
Niels Möller committed
118
119
  connected->backend = b;
  connected->block_size = block_size;
Niels Möller's avatar
Niels Möller committed
120
  connected->id_comment = comment;
Niels Möller's avatar
Niels Möller committed
121
  
122
  return &connected->super;
Niels Möller's avatar
Niels Möller committed
123
}
Niels Möller's avatar
Niels Möller committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

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;
}