client.c 2.92 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 client_callback *closure,
Niels Möller's avatar
Niels Möller committed
23
			   int fd)
Niels Möller's avatar
Niels Möller committed
24
{
Niels Möller's avatar
Niels Möller committed
25
  struct ssh_connection *connection = ssh_connection_alloc();
Niels Möller's avatar
Niels Möller committed
26
27
28
29
30
31
  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
32
33
34
35
36
  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
37
  /* Copies the version string, so that it is isn't freed */
Niels Möller's avatar
Niels Möller committed
38
  return A_WRITE(write, ssh_format("%lS", connection->client_version));
Niels Möller's avatar
Niels Möller committed
39
40
}

Niels Möller's avatar
Niels Möller committed
41
struct client_line_handler
Niels Möller's avatar
Niels Möller committed
42
{
Niels Möller's avatar
Niels Möller committed
43
  struct line_handler super;
Niels Möller's avatar
Niels Möller committed
44
  struct ssh_connection *connection;
Niels Möller's avatar
Niels Möller committed
45
};
Niels Möller's avatar
Niels Möller committed
46

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

Niels Möller's avatar
Niels Möller committed
65
66
67
68
	  /* FIXME: Cleanup properly. */
	  free(closure);

	  return new;
Niels Möller's avatar
Niels Möller committed
69
70
71
72
	}
      else
	{
	  werror("Unsupported protocol version: ");
Niels Möller's avatar
Niels Möller committed
73
	  werror_safe(length, line);
Niels Möller's avatar
Niels Möller committed
74
	  werror("\n");
Niels Möller's avatar
Niels Möller committed
75
76

	  fatal("client.c: do_line: Unsupported version.\n"); 
Niels Möller's avatar
Niels Möller committed
77
	  /* FIXME: What could be returned here? */
Niels Möller's avatar
Niels Möller committed
78
79
80
81
82
83
	  return 0;
	}
    }
  else
    {
      /* Display line */
Niels Möller's avatar
Niels Möller committed
84
      werror_safe(length, line);
Niels Möller's avatar
Niels Möller committed
85
86

      /* Read next line */
Niels Möller's avatar
Niels Möller committed
87
      return 0;
Niels Möller's avatar
Niels Möller committed
88
89
90
    }
}

Niels Möller's avatar
Niels Möller committed
91
struct read_handler *make_client_read_line(struct ssh_connection *s)
Niels Möller's avatar
Niels Möller committed
92
93
94
95
96
{
  struct client_line_handler *closure
    = xalloc(sizeof(struct client_line_handler));
  
  closure->super.handler = (line_handler_f) do_line;
Niels Möller's avatar
Niels Möller committed
97
  closure->connection = s;
Niels Möller's avatar
Niels Möller committed
98
99
100
101
  
  return make_read_line( (struct line_handler *) closure);
}
  
Niels Möller's avatar
Niels Möller committed
102
struct fd_callback *make_client_callback(struct io_backend *b,
Niels Möller's avatar
Niels Möller committed
103
					 char *comment,
Niels Möller's avatar
Niels Möller committed
104
					 UINT32 block_size)
Niels Möller's avatar
Niels Möller committed
105
					 
Niels Möller's avatar
Niels Möller committed
106
{
Niels Möller's avatar
Niels Möller committed
107
  struct client_callback *connected = xalloc(sizeof(struct client_callback));
Niels Möller's avatar
Niels Möller committed
108
109
110
111

  connected->c.f = (fd_callback_f) client_initiate;
  connected->backend = b;
  connected->block_size = block_size;
Niels Möller's avatar
Niels Möller committed
112
  connected->id_comment = comment;
Niels Möller's avatar
Niels Möller committed
113
114
115
  
  return (struct fd_callback *) connected;
}
Niels Möller's avatar
Niels Möller committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

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