client.c 14.1 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 Mller
 *
 * 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
 */

26
/* FIXME: Why include stdio? */
Niels Möller's avatar
Niels Möller committed
27
/* #include <stdio.h> */
Niels Möller's avatar
Niels Möller committed
28

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

Niels Möller's avatar
Niels Möller committed
31
#include "abstract_io.h"
32
#include "channel.h"
33
34
#include "connection.h"
#include "crypto.h"
Niels Möller's avatar
Niels Möller committed
35
#include "debug.h"
Niels Möller's avatar
Niels Möller committed
36
#include "encrypt.h"
37
#include "format.h"
Niels Möller's avatar
Niels Möller committed
38
#include "pad.h"
39
#include "parse.h"
40
41
#include "read_line.h"
#include "read_packet.h"
42
#include "service.h"
43
#include "ssh.h"
Niels Möller's avatar
Niels Möller committed
44
#include "translate_signal.h"
Niels Möller's avatar
Niels Möller committed
45
#include "unpad.h"
46
47
48
#include "version.h"
#include "werror.h"
#include "xalloc.h"
Niels Möller's avatar
Niels Möller committed
49

Niels Möller's avatar
Niels Möller committed
50
#include <string.h>
51
#include <assert.h>
Niels Möller's avatar
Niels Möller committed
52

53
54
#include "client.c.x"

55
/* Handle connection and initial handshaking. */
56
57
58
59
60
61
62
63
64
65
66
67
68
/* CLASS:
   (class
     (name client_callback)
     (super fd_callback)
     (vars
       (backend object io_backend)
       (block_size simple UINT32)
       (id_comment simple "char *")
       (random object randomness)
       (init object make_kexinit)
       (kexinit_handler object packet_handler)))
*/

69
static int client_initiate(struct fd_callback **c,
Niels Möller's avatar
Niels Möller committed
70
			   int fd)
Niels Möller's avatar
Niels Möller committed
71
{
72
  struct client_callback *closure
73
    = (struct client_callback *) *c;
Niels Möller's avatar
Niels Möller committed
74

Niels Möller's avatar
Niels Möller committed
75
76
  int res;
  
77
  struct ssh_connection *connection
Niels Möller's avatar
Niels Möller committed
78
    = make_ssh_connection(closure->kexinit_handler);
79
80
81

  connection_init_io(connection,
		     io_read_write(closure->backend, fd,
Niels Möller's avatar
Niels Möller committed
82
				   make_client_read_line(connection),
83
84
85
				   closure->block_size,
				   make_client_close_handler()),
		     closure->random);
Niels Möller's avatar
Niels Möller committed
86
  
Niels Möller's avatar
Niels Möller committed
87
  connection->client_version
88
    = ssh_format("SSH-%lz-%lz %lz",
Niels Möller's avatar
Niels Möller committed
89
90
91
		 PROTOCOL_VERSION,
		 SOFTWARE_CLIENT_VERSION,
		 closure->id_comment);
Niels Möller's avatar
Niels Möller committed
92
  
Niels Möller's avatar
Niels Möller committed
93
94
  res = A_WRITE(connection->raw,
		ssh_format("%lS\r\n", connection->client_version));
95
  if (LSH_CLOSEDP(res))
Niels Möller's avatar
Niels Möller committed
96
97
    return res;

98
99
100
  return res | initiate_keyexchange(connection, CONNECTION_CLIENT,
				    MAKE_KEXINIT(closure->init),
				    NULL);
Niels Möller's avatar
Niels Möller committed
101
102
}

103
104
105
106
107
108
109
110
/* CLASS:
   (class
     (name client_line_handler)
     (super line_handler)
     (vars
       (connection object ssh_connection)))
*/

111
static struct read_handler *do_line(struct line_handler **h,
Niels Möller's avatar
Niels Möller committed
112
113
114
				    UINT32 length,
				    UINT8 *line)
{
115
  CAST(client_line_handler, closure, *h);
116

Niels Möller's avatar
Niels Möller committed
117
  if ( (length >= 4) && !memcmp(line, "SSH-", 4))
Niels Möller's avatar
Niels Möller committed
118
119
    {
      /* Parse and remember format string */
Niels Möller's avatar
Niels Möller committed
120
121
      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
122
	{
123
	  struct read_handler *new = make_read_packet
Niels Möller's avatar
Niels Möller committed
124
125
126
	    (make_packet_unpad
	     (make_packet_debug
	      (&closure->connection->super,
127
	       "")),
128
	     closure->connection);
Niels Möller's avatar
Niels Möller committed
129
	     
Niels Möller's avatar
Niels Möller committed
130
	  closure->connection->server_version
131
	    = ssh_format("%ls", length, line);
Niels Möller's avatar
Niels Möller committed
132

133
134
135
136
137
138
139
140
	  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
141
	  /* FIXME: Cleanup properly. */
142
	  KILL(closure);
Niels Möller's avatar
Niels Möller committed
143
144

	  return new;
Niels Möller's avatar
Niels Möller committed
145
146
147
148
	}
      else
	{
	  werror("Unsupported protocol version: ");
Niels Möller's avatar
Niels Möller committed
149
	  werror_safe(length, line);
Niels Möller's avatar
Niels Möller committed
150
	  werror("\n");
Niels Möller's avatar
Niels Möller committed
151

152
	  /* FIXME: Clean up properly */
153
	  KILL(closure);
Niels Möller's avatar
Niels Möller committed
154
	  *h = NULL;
155
		  
Niels Möller's avatar
Niels Möller committed
156
157
158
159
160
161
	  return 0;
	}
    }
  else
    {
      /* Display line */
Niels Möller's avatar
Niels Möller committed
162
      werror_safe(length, line);
Niels Möller's avatar
Niels Möller committed
163
164

      /* Read next line */
Niels Möller's avatar
Niels Möller committed
165
      return 0;
Niels Möller's avatar
Niels Möller committed
166
167
168
    }
}

Niels Möller's avatar
Niels Möller committed
169
struct read_handler *make_client_read_line(struct ssh_connection *c)
Niels Möller's avatar
Niels Möller committed
170
{
171
  NEW(client_line_handler, closure);
172

173
  closure->super.handler = do_line;
Niels Möller's avatar
Niels Möller committed
174
  closure->connection = c;
Niels Möller's avatar
Niels Möller committed
175
  
176
  return make_read_line(&closure->super);
Niels Möller's avatar
Niels Möller committed
177
178
}
  
Niels Möller's avatar
Niels Möller committed
179
180
181
182
183
184
185
186
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
187
{
188
  NEW(client_callback, connected);
Niels Möller's avatar
Niels Möller committed
189

190
  connected->super.f = client_initiate;
Niels Möller's avatar
Niels Möller committed
191
192
  connected->backend = b;
  connected->block_size = block_size;
Niels Möller's avatar
Niels Möller committed
193
  connected->id_comment = comment;
Niels Möller's avatar
Niels Möller committed
194
195
196
197
198

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

199
  return &connected->super;
Niels Möller's avatar
Niels Möller committed
200
}
Niels Möller's avatar
Niels Möller committed
201

202
static int client_close_die(struct close_callback *closure, int reason)
Niels Möller's avatar
Niels Möller committed
203
{
204
205
206
  verbose("Connection died, for reason %d.\n", reason);
  if (reason != CLOSE_EOF)
    werror("Connection died.\n");
207
208
209

  /* FIXME: Return value is not used. */
  return 4711;
Niels Möller's avatar
Niels Möller committed
210
211
}

212
struct close_callback *make_client_close_handler(void)
Niels Möller's avatar
Niels Möller committed
213
{
214
  NEW(close_callback, c);
Niels Möller's avatar
Niels Möller committed
215

216
  c->f = client_close_die;
Niels Möller's avatar
Niels Möller committed
217
218
219

  return c;
}
220

221
/* Start a service that the server has accepted (for instance ssh-userauth). */
222
223
224
225
226
227
228
229
230
/* CLASS:
   (class
     (name accept_service_handler)
     (super packet_handler)
     (vars
       (service_name simple int)
       (service object ssh_service)))
*/

231
232
233
234
static int do_accept_service(struct packet_handler *c,
			     struct ssh_connection *connection,
			     struct lsh_string *packet)
{
235
  CAST(accept_service_handler, closure, c);
236
237
238
239
240

  struct simple_buffer buffer;
  int msg_number;
  int name;

241
242
  simple_buffer_init(&buffer, packet->length, packet->data);
  
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_SERVICE_ACCEPT)
      && parse_atom(&buffer, &name)
      && parse_eod(&buffer)
      && (name == closure->service_name))
    {
      lsh_string_free(packet);
      connection->dispatch[SSH_MSG_SERVICE_ACCEPT] = connection->fail;
      
      return SERVICE_INIT(closure->service, connection);
    }

  lsh_string_free(packet);
  return LSH_FAIL | LSH_DIE;
}

Niels Möller's avatar
Niels Möller committed
259
260
struct packet_handler *make_accept_service_handler(int service_name,
						   struct ssh_service *service)
261
{
262
  NEW(accept_service_handler, closure);
263

Niels Möller's avatar
Niels Möller committed
264
  closure->super.handler = do_accept_service;
265
266
267
268
269
270
  closure->service_name = service_name;
  closure->service = service;

  return &closure->super;
}

271
272
273
274
275
276
277
278
279
/* CLASS:
   (class
     (name service_request)
     (super ssh_service)
     (vars
       (service_name simple int)
       (service object ssh_service)))
*/

280
281
282
static int do_request_service(struct ssh_service *c,
			      struct ssh_connection *connection)
{
283
  CAST(service_request, closure, c);
284
  
Niels Möller's avatar
Niels Möller committed
285
286
287
288
  connection->dispatch[SSH_MSG_SERVICE_ACCEPT]
    = make_accept_service_handler(closure->service_name,
				  closure->service);
  
289
  return A_WRITE(connection->write, format_service_request(closure->service_name));
290
291
292
293
294
}

struct ssh_service *request_service(int service_name,
				    struct ssh_service *service)
{
295
  NEW(service_request, closure);
296
297
298
299
300
301
302
303

  closure->super.init = do_request_service;
  closure->service_name = service_name;
  closure->service = service;

  return &closure->super;
}

Niels Möller's avatar
Niels Möller committed
304
/* Initiate and manage a session */
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* CLASS:
   (class
     (name client_session)
     (super ssh_channel)
     (vars
       ; Exec or shell request. 
       (final_request simple int)
       (args string)
  
       ; To access stdio
       (in object io_fd)
       (out object io_fd)
       (err object io_fd)

       ; Where to save the exit code.
       (exit_status simple "int *")))
*/

Niels Möller's avatar
Niels Möller committed
323
324
static int close_client_session(struct ssh_channel *c)
{
325
  CAST(client_session, session, c);
Niels Möller's avatar
Niels Möller committed
326
  
327
328
329
330
331
  close_fd(&session->in->super, 0);
#if 0
  close_fd(&session->out->super, 0);
  close_fd(&session->err->super, 0);
#endif
Niels Möller's avatar
Niels Möller committed
332
333
334
335
  
  return LSH_OK | LSH_CHANNEL_PENDING_CLOSE;
}  

Niels Möller's avatar
Niels Möller committed
336
static int client_session_die(struct ssh_channel *c)
337
{
338
  CAST(client_session, closure, c);
339
  
Niels Möller's avatar
Niels Möller committed
340
341
342
  /* FIXME: Don't die this hard. */
  if ( (closure->super.flags & (CHANNEL_SENT_CLOSE | CHANNEL_RECIEVED_CLOSE))
       ==  (CHANNEL_SENT_CLOSE | CHANNEL_RECIEVED_CLOSE))
343
344
345
346
347
    exit(EXIT_SUCCESS);

  exit(EXIT_FAILURE);
}

348
349
350
351
352
353
354
355
/* CLASS:
   (class
     (name exit_handler)
     (super channel_request)
     (vars
       (exit_status simple "int *")))
*/

Niels Möller's avatar
Niels Möller committed
356
357
358
359
360
static int do_exit_status(struct channel_request *c,
			  struct ssh_channel *channel,
			  int want_reply,
			  struct simple_buffer *args)
{
361
  CAST(exit_handler, closure, c);
Niels Möller's avatar
Niels Möller committed
362
363
364
365
366
367
368
369
370
371
372
  int status;

  if (!want_reply
      && parse_uint32(args, &status)
      && parse_eod(args))
    {
      *closure->exit_status = status;

      ALIST_SET(channel->request_types, ATOM_EXIT_STATUS, NULL);;
      ALIST_SET(channel->request_types, ATOM_EXIT_SIGNAL, NULL);;

373
      return LSH_OK | LSH_GOON;
Niels Möller's avatar
Niels Möller committed
374
375
376
377
378
379
380
381
382
383
384
    }
  
  /* Invalid request */
  return LSH_FAIL | LSH_DIE;
}

static int do_exit_signal(struct channel_request *c,
			  struct ssh_channel *channel,
			  int want_reply,
			  struct simple_buffer *args)
{
385
386
  CAST(exit_handler, closure, c);

Niels Möller's avatar
Niels Möller committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
  int signal;
  int core;

  UINT8 *msg;
  UINT32 length;

  UINT8 *language;
  UINT32 language_length;
  
  if (!want_reply
      && parse_uint32(args, &signal)
      && parse_boolean(args, &core)
      && parse_string(args, &length, &msg)
      && parse_string(args, &language_length, &language)
      && parse_eod(args))
    {
      /* FIXME: What exit status should be returned when the remote
       * process dies violently? */

      *closure->exit_status = 7;

      signal = signal_network_to_local(signal);

      werror_utf8(length, msg);
      werror("Remote process was killed by %s.\n",
	     signal ? strsignal(signal) : "an unknown signal");
      if (core)
	werror("(core dumped remotely)\n");

      ALIST_SET(channel->request_types, ATOM_EXIT_STATUS, NULL);;
      ALIST_SET(channel->request_types, ATOM_EXIT_SIGNAL, NULL);;

      return close_client_session(channel);
    }
  
  /* Invalid request */
  return LSH_FAIL | LSH_DIE;
}

struct channel_request *make_handle_exit_status(int *exit_status)
{
428
  NEW(exit_handler, self);
Niels Möller's avatar
Niels Möller committed
429
430
431
432
433
434
435
436
437
438

  self->super.handler = do_exit_status;

  self->exit_status = exit_status;

  return &self->super;
}

struct channel_request *make_handle_exit_signal(int *exit_status)
{
439
  NEW(exit_handler, self);
Niels Möller's avatar
Niels Möller committed
440
441
442
443
444
445
446
447

  self->super.handler = do_exit_signal;

  self->exit_status = exit_status;

  return &self->super;
}

Niels Möller's avatar
Niels Möller committed
448
/* Recieve channel data */
Niels Möller's avatar
Niels Möller committed
449
static int do_recieve(struct ssh_channel *c,
450
451
		      int type, struct lsh_string *data)
{
452
  CAST(client_session, closure, c);
453
454
455
456
  
  switch(type)
    {
    case CHANNEL_DATA:
457
      return A_WRITE(&closure->out->buffer->super, data);
458
    case CHANNEL_STDERR_DATA:
459
      return A_WRITE(&closure->err->buffer->super, data);
460
461
462
463
    default:
      fatal("Internal error!\n");
    }
}
Niels Möller's avatar
Niels Möller committed
464

Niels Möller's avatar
Niels Möller committed
465
466
467
/* We may send more data */
static int do_send(struct ssh_channel *c)
{
468
  CAST(client_session, closure, c);
Niels Möller's avatar
Niels Möller committed
469

470
471
472
  assert(closure->in->super.read);
  assert(closure->in->handler);
  closure->in->super.want_read = 1;
Niels Möller's avatar
Niels Möller committed
473
474
475
476

  return LSH_OK | LSH_GOON;
}

477
/* We have a remote shell */
478
static int do_io(struct ssh_channel *channel)
479
{
480
  CAST(client_session, closure, channel);
481
482
483
  
  channel->recieve = do_recieve;
  
484
485
  closure->out->super.close_callback
    = closure->err->super.close_callback = make_channel_close(channel);
Niels Möller's avatar
Niels Möller committed
486
  
Niels Möller's avatar
Niels Möller committed
487
  closure->in->handler = make_channel_read_data(&closure->super);
488
  channel->send = do_send;
Niels Möller's avatar
Niels Möller committed
489
490
491
492
493

  ALIST_SET(channel->request_types, ATOM_EXIT_STATUS,
	    make_handle_exit_status(closure->exit_status));
  ALIST_SET(channel->request_types, ATOM_EXIT_SIGNAL,
	    make_handle_exit_signal(closure->exit_status));
494
495
496

  channel->eof = close_client_session;

497
  return LSH_OK | LSH_CHANNEL_READY_SEND;
498
499
500
}

/* We have opened a channel of type "session" */
Niels Möller's avatar
Niels Möller committed
501
static int do_open_confirm(struct ssh_channel *c)
502
{
503
  CAST(client_session, closure, c);
Niels Möller's avatar
Niels Möller committed
504
505
  struct lsh_string *args;
  
506
507
508
  closure->super.open_confirm = NULL;
  closure->super.open_failure = NULL;

Niels Möller's avatar
Niels Möller committed
509
  closure->super.channel_success = do_io;
510
511
  closure->super.channel_failure = client_session_die;

Niels Möller's avatar
Niels Möller committed
512
513
514
515
516
517
  args = closure->args;
  closure->args = NULL; /* for gc */

  return A_WRITE(closure->super.write,
		 format_channel_request(closure->final_request, c, 1,
					"%lfS", args));
518
519
}

520
static struct ssh_channel *make_client_session(struct io_fd *in,
521
522
					       struct io_fd *out,
					       struct io_fd *err,
523
524
					       UINT32 max_window,
					       int final_request,
Niels Möller's avatar
Niels Möller committed
525
526
					       struct lsh_string *args,
					       int *exit_status)
Niels Möller's avatar
Niels Möller committed
527
{
528
  NEW(client_session, self);
Niels Möller's avatar
Niels Möller committed
529

Niels Möller's avatar
Niels Möller committed
530
531
  init_channel(&self->super);

532
  self->super.max_window = max_window;
Niels Möller's avatar
Niels Möller committed
533
534
535
536
  self->super.rec_window_size = max_window;

  /* FIXME: Make maximum packet size configurable */
  self->super.rec_max_packet = SSH_MAX_PACKET;
Niels Möller's avatar
Niels Möller committed
537
538

  self->super.request_types = make_alist(0, -1);
539

Niels Möller's avatar
Niels Möller committed
540
  /* self->expect_close = 0; */
Niels Möller's avatar
Niels Möller committed
541
542
543
544
  self->in = in;
  self->out = out;
  self->err = err;

Niels Möller's avatar
Niels Möller committed
545
546
  self->final_request = final_request;
  self->args = args;
Niels Möller's avatar
Niels Möller committed
547
548

  self->exit_status = exit_status;
Niels Möller's avatar
Niels Möller committed
549
  
Niels Möller's avatar
Niels Möller committed
550
551
552
  return &self->super;
}

553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/* CLASS:
   (class
     (name client_startup)
     (super connection_startup)
     (vars
       (session object ssh_channel)
       
       ; Exec or shell request. 
       ;; (final_request simple int)
       ;; (args string)
  
       ; To access stdio 
       ;; (in object io_fd)
       ;; (out object io_fd)
       ;; (err object io_fd)
       ))
*/

571
static int do_client_startup(struct connection_startup *c,
Niels Möller's avatar
Niels Möller committed
572
573
			     struct channel_table *table,
			     struct abstract_write *write)
574
{
575
  CAST(client_startup, closure, c);
576
  struct lsh_string *s;
Niels Möller's avatar
Niels Möller committed
577
578
  
  closure->session->write = write;
579
  
Niels Möller's avatar
Niels Möller committed
580
581
582
  closure->session->open_confirm = do_open_confirm;
  closure->session->open_failure = client_session_die;

583
584
585
586
587
588
  s = prepare_channel_open(table, ATOM_SESSION,
			   closure->session, "");
  if (!s)
    fatal("Couldn't allocate a channel number!\n");

  return A_WRITE(write, s);
589
590
}

Niels Möller's avatar
Niels Möller committed
591
592
#define WINDOW_SIZE (SSH_MAX_PACKET << 3)

593
/* Request opening a session. */
Niels Möller's avatar
Niels Möller committed
594
struct connection_startup *make_client_startup(struct io_fd *in,
595
596
					       struct io_fd *out,
					       struct io_fd *err,
Niels Möller's avatar
Niels Möller committed
597
					       int final_request,
Niels Möller's avatar
Niels Möller committed
598
599
					       struct lsh_string *args,
					       int *exit_status)
600
{
601
  NEW(client_startup, closure);
Niels Möller's avatar
Niels Möller committed
602
  
603
  closure->super.start = do_client_startup;
604
605
  closure->session = make_client_session(in, out, err,
					 WINDOW_SIZE,
Niels Möller's avatar
Niels Möller committed
606
607
					 final_request, args,
					 exit_status);
608

609
610
611
  return &closure->super;
}