lshd.c 11.5 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
/* lshd.c
 *
 * main server program.
4
5
 *
 * $Id$ */
Niels Möller's avatar
Niels Möller committed
6

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* 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
25

26
#include "algorithms.h"
27
28
#include "alist.h"
#include "atoms.h"
29
#include "channel.h"
30
#include "charset.h"
31
32
#include "crypto.h"
#include "format.h"
Niels Möller's avatar
Niels Möller committed
33
#include "io.h"
Niels Möller's avatar
Niels Möller committed
34
#include "password.h"
35
#include "randomness.h"
Niels Möller's avatar
Niels Möller committed
36
#include "reaper.h"
Niels Möller's avatar
Niels Möller committed
37
#include "server.h"
38
#include "server_keyexchange.h"
39
#include "sexp.h"
Niels Möller's avatar
Niels Möller committed
40
#include "ssh.h"
Niels Möller's avatar
Niels Möller committed
41
#include "userauth.h"
42
43
#include "werror.h"
#include "xalloc.h"
Niels Möller's avatar
Niels Möller committed
44
#include "compress.h"
45
#include "server_pty.h"
46

47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "getopt.h"

#include "lshd.c.x"

#include <assert.h>

#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
61
#if HAVE_UNISTD_H
62
#include <unistd.h>
63
#endif
64

65
/* Block size for stdout and stderr buffers */
Niels Möller's avatar
Niels Möller committed
66
67
#define BLOCK_SIZE 32768

Niels Möller's avatar
Niels Möller committed
68
void usage(void) NORETURN;
Niels Möller's avatar
Niels Möller committed
69

70
71
#define OPT_SSH1_FALLBACK -2

Niels Möller's avatar
Niels Möller committed
72
void usage(void)
Niels Möller's avatar
Niels Möller committed
73
{
74
  werror("lshd [options]\n"
75
76
77
	 " -p,  --port=PORT\n"
	 " -h,  --hostkey=KEYFILE\n"
	 " -c,  --crypto=ALGORITHM\n"
78
	 " -z,  --compression[=ALGORITHM]\n"
79
80
	 "      --mac=ALGORITHM\n"
	 " -q,  --quiet\n"
81
82
83
#if WITH_SSH1_FALLBACK
         "      --ssh1-fallback=SSHD\n"
#endif
84
85
	 " -v,  --verbose\n"
	 "      --debug\n");
Niels Möller's avatar
Niels Möller committed
86
87
88
  exit(1);
}

89
#if 0
Niels Möller's avatar
Niels Möller committed
90
91
92
93
struct signer *secret_key;
struct lsh_string *public_key;

/* A key generated by gnupg */
94
static void init_host_key(struct randomness *r)
Niels Möller's avatar
Niels Möller committed
95
96
97
{
  mpz_t p, q, g, y, a;
  mpz_t tmp;
98
  struct lsh_string *s;
Niels Möller's avatar
Niels Möller committed
99
100
101
102
103
104
105
  
  mpz_init_set_str(p,
		   "BC7797D55CF2449CA4B02396246AF5C75CA38C52B6F2E543"
		   "6754198B137B25B0A81DFE269D5CDFD0AEA290A32BA5B918"
		   "B58D64762D40EAA8D70F282B3AC4A7771171B1B1D1AE89F4"
		   "1CD091FE95A6F42A2340081F9E97A4B5F953DE223F10F878"
		   "4C0619A9979643E5325DF71C9C088F3BC82FA0A6C47B5C64"
106
		   "BC07A31B9CDB2B07", 16);
Niels Möller's avatar
Niels Möller committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  mpz_init_set_str(q,
		   "867F7E6563B3FAF19B65C83E9B843150C5CC2201", 16);
  mpz_init_set_str(g,
		   "7FA83EAEDFD8679A4A80C869AD7E353F3B517569C2079C79"
		   "97EA6655764581B073F71AA15C07A789AEB213B106741AAB"
		   "CA81B8300B1F8510D3CD1C3D9D7D11640C1608E8E2E71527"
		   "68B8FDCB5544E29A020D14CC5C12E264C59E57E9F6832DA7"
		   "10B805CD9866C1110D60069D31D5A72D1A1ED96F2B11CFEF"
		   "7AB347F0632CB0C7", 16);
  mpz_init_set_str(y,
		   "2DA5B458DF3616097FA22DB6BDDD31A29E532054D4C208F7"
		   "EBF63EB2476E8E98E0885CFBC5669B56EC834E42058E8BCF"
		   "C259CA1BE981D7721306709499DE27E7B13F62359D9520D1"
		   "3D73C62E8E5C5F6B8E2C70217EC3B557FBCB98535BE3C6EE"
		   "0C71DEC1FE9C6791D3780DD8D593D5030969D303A5818B01"
		   "C4B855C07E8C4F64", 16);
123
124
  mpz_init_set_str(a,
		   "295190AEDBBD6EBD2F817F7D8CCC8B0095DCD82E", 16);
Niels Möller's avatar
Niels Möller committed
125
126
127
128
129
130
131
132
133
134
135

  mpz_init_set(tmp, g);
  mpz_powm(tmp, tmp, a, p);
  if (mpz_cmp(tmp, y))
    fatal("Test key invalid\n");

  mpz_clear(tmp);

  public_key = ssh_format("%a%n%n%n%n", ATOM_SSH_DSS, p, q, g, y);
  s = ssh_format("%n", a);

Niels Möller's avatar
Niels Möller committed
136
  secret_key = MAKE_SIGNER(make_dsa_algorithm(r),
Niels Möller's avatar
Niels Möller committed
137
138
139
			   public_key->length, public_key->data,
			   s->length, s->data);

Niels Möller's avatar
Niels Möller committed
140
141
142
  if (!secret_key)
    fatal("Can't parse secret key\n");

143
  lsh_string_free(s);
Niels Möller's avatar
Niels Möller committed
144
145
146
147
148
149
  mpz_clear(p);
  mpz_clear(q);
  mpz_clear(g);
  mpz_clear(y);
  mpz_clear(a);
}
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#endif

/* FIXME: We should have some more general functions for reading private keys. */

/* CLASS:
   (class
     (name read_key)
     (super sexp_handler)
     (vars
       (random object randomness)
       (secret pointer (object signer))
       (public pointer (string))))
*/

static int do_read_key(struct sexp_handler *h, struct sexp *private)
{
  CAST(read_key, closure, h);
  struct sexp_iterator *i;
  struct sexp *e;
  mpz_t p, q, g, y, x;

  int res;
  
  if (!sexp_check_type(private, "private-key", &i))
    {
      werror("lshd: Host key file does not contain a private key.");
      return LSH_FAIL | LSH_DIE;
    }

  e = SEXP_GET(i);
Niels Möller's avatar
Niels Möller committed
180
  if (! (e && sexp_check_type(e, "dsa", &i)))
181
    {
Niels Möller's avatar
Niels Möller committed
182
      werror("lshd: Unknown key type (only dsa is supported)\n");
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
      return LSH_FAIL | LSH_DIE;
    }

  mpz_init(p);
  mpz_init(q);
  mpz_init(g);
  mpz_init(y);
  mpz_init(x);

  if (sexp_get_un(i, "p", p)
      && sexp_get_un(i, "q", q)
      && sexp_get_un(i, "g", g)
      && sexp_get_un(i, "y", y)
      && sexp_get_un(i, "x", x)
      && !SEXP_GET(i))
    {
      /* Test key */
      mpz_t tmp;
      struct lsh_string *s;
      
      mpz_init_set(tmp, g);
      mpz_powm(tmp, tmp, x, p);
      if (mpz_cmp(tmp, y))
	{
	  werror("lshd: Host key doesn't work.\n");
	  mpz_clear(tmp);

	  res = LSH_FAIL | LSH_DIE;
	}
      else
	{
	  struct lsh_string *public
	    = ssh_format("%a%n%n%n%n", ATOM_SSH_DSS, p, q, g, y);
	  struct signer *secret;
	  	  
	  s = ssh_format("%n", x);
	  
Niels Möller's avatar
Niels Möller committed
220
	  secret = MAKE_SIGNER(make_dsa_algorithm(closure->random),
221
222
223
224
225
226
227
228
			       public->length, public->data,
			       s->length, s->data);
	  assert(secret);
	  lsh_string_free(s);

	  *closure->public = public;
	  *closure->secret = secret;

229
230
231
232
233
234
	  verbose("lshd: Using (public) hostkey:\n"
		  "  p=%hn\n"
		  "  q=%hn\n"
		  "  g=%hn\n"
		  "  y=%hn\n",
		  p, q, g, y);
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
		  
	  res = LSH_OK | LSH_CLOSE;
	}
    }
  else
    res = LSH_FAIL | LSH_DIE;

  /* Cleanup */
  mpz_clear(p);
  mpz_clear(q);
  mpz_clear(g);
  mpz_clear(y);
  mpz_clear(x);

  return res;
}

static int read_host_key(const char *name,
			 struct lsh_string **public,
			 struct signer **secret,
			 struct randomness *r)
{
  int fd = open(name, O_RDONLY);
  if (fd < 0)
    {
260
      werror("lshd: Could not open %z (errno = %i): %z\n",
261
262
263
264
265
266
267
268
269
	     name, errno, strerror(errno));
      return 0;
    }
  else
    {
      int res;
      
      NEW(read_key, handler);
      handler->super.handler = do_read_key;
Niels Möller's avatar
Niels Möller committed
270

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
      handler->random = r;
      handler->public = public;
      handler->secret = secret;
      
      res = blocking_read(fd, make_read_sexp(&handler->super,
					     2000, SEXP_TRANSPORT, 0));
      close(fd);

      KILL(handler);
      
      if (LSH_FAILUREP(res))
	{
	  werror("lshd: Invalid host key.\n");
	  return 0;
	}

      return 1;
    }
}
  
Niels Möller's avatar
Niels Möller committed
291
292
int main(int argc, char **argv)
{
293
  char *host = NULL;  /* Interface to bind */
Niels Möller's avatar
Niels Möller committed
294
  char *port = "ssh";
Niels Möller's avatar
Niels Möller committed
295
  /* TODO: this should probably use sysconfdir */  
296
297
  char *hostkey = "/etc/lsh_host_key";

298
299
300
301
#if WITH_SSH1_FALLBACK
  char *sshd1 = NULL;
#endif
  
302
303
304
  struct lsh_string *public_key = NULL; 
  struct signer *secret_key = NULL;
  
Niels Möller's avatar
Niels Möller committed
305
306
  int option;

307
308
309
  int preferred_crypto = 0;
  int preferred_compression = 0;
  int preferred_mac = 0;
310

311
  struct sockaddr_in local;
312

Niels Möller's avatar
Niels Möller committed
313
314
  struct reap *reaper;
  
315
316
317
318
  struct randomness *r;
  struct diffie_hellman_method *dh;
  struct keyexchange_algorithm *kex;
  struct alist *algorithms;
Niels Möller's avatar
Niels Möller committed
319
  struct make_kexinit *make_kexinit;
320
  struct packet_handler *kexinit_handler;
321
322
323

  NEW(io_backend, backend);

Niels Möller's avatar
Niels Möller committed
324
325
326
  /* For filtering messages. Could perhaps also be used when converting
   * strings to and from UTF8. */
  setlocale(LC_CTYPE, "");
327
328
  /* FIXME: Choose character set depending on the locale */
  set_local_charset(CHARSET_LATIN1);
329
330
331

  r = make_reasonably_random();
  dh = make_dh1(r);
Niels Möller's avatar
Niels Möller committed
332
  
333
  algorithms = many_algorithms(1,
Niels Möller's avatar
Niels Möller committed
334
			       ATOM_SSH_DSS, make_dsa_algorithm(r),
335
336
			       -1);

337
  for (;;)
338
339
    {
      static struct option options[] =
Niels Möller's avatar
Niels Möller committed
340
      {
341
342
343
344
345
346
347
	{ "verbose", no_argument, NULL, 'v' },
	{ "quiet", no_argument, NULL, 'q' },
	{ "debug", no_argument, &debug_flag, 1},
	{ "port", required_argument, NULL, 'p' },
	{ "crypto", required_argument, NULL, 'c' },
	{ "compression", optional_argument, NULL, 'z'},
	{ "mac", required_argument, NULL, 'm' },
348
	{ "hostkey", required_argument, NULL, 'h' },
349
350
351
#if WITH_SSH1_FALLBACK
	{ "ssh1-fallback", optional_argument, NULL, OPT_SSH1_FALLBACK},
#endif
352
353
354
	{ NULL }
      };
      
355
      option = getopt_long(argc, argv, "c:h:p:qvz::", options, NULL);
356
357
358
359
360
361
      switch(option)
	{
	case -1:
	  goto options_done;
	case 0:
	  break;
362
363
364
365
366
#if WITH_SSH1_FALLBACK
	case OPT_SSH1_FALLBACK:
	  sshd1 = optarg ? optarg : SSHD1;
	  break;
#endif
367
368
369
370
371
372
373
374
375
	case 'p':
	  port = optarg;
	  break;
	case 'q':
	  quiet_flag = 1;
	  break;
	case 'v':
	  verbose_flag = 1;
	  break;
376
377
378
	case 'h':
	  hostkey = optarg;
	  break;
379
380
381
382
	case 'c':
	  preferred_crypto = lookup_crypto(algorithms, optarg);
	  if (!preferred_crypto)
	    {
383
	      werror("lsh: Unknown crypto algorithm '%z'.\n", optarg);
384
385
386
387
388
389
390
391
392
393
	      exit(1);
	    }
	  break;
	case 'z':
	  if (!optarg)
	    optarg = "zlib";
	
	  preferred_compression = lookup_compression(algorithms, optarg);
	  if (!preferred_compression)
	    {
394
	      werror("lsh: Unknown compression algorithm '%z'.\n", optarg);
395
396
397
398
399
400
401
	      exit(1);
	    }
	  break;
	case 'm':
	  preferred_mac = lookup_mac(algorithms, optarg);
	  if (!preferred_mac)
	    {
402
	      werror("lsh: Unknown message authentication algorithm '%z'.\n",
403
404
405
406
407
408
409
410
411
		      optarg);
	      exit(1);
	    }
	    
	case '?':
	  usage();
	}
    }
 options_done:
Niels Möller's avatar
Niels Möller committed
412
413
414
415

  if ( (argc - optind) != 0)
    usage();

416
417
418
  /* Read the hostkey */
  if (!read_host_key(hostkey, &public_key, &secret_key, r))
    {
Niels Möller's avatar
Niels Möller committed
419
      werror("lshd: Could not read hostkey.\n");
420
421
422
      return EXIT_FAILURE;
    }

423
424
  if (!get_inaddr(&local, host, port, "tcp"))
    {
Niels Möller's avatar
Niels Möller committed
425
      werror("lshd: No such host or service.\n");
426
      return EXIT_FAILURE;
427
428
    }

Niels Möller's avatar
Niels Möller committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#if 0
#ifdef HAVE_SYSLOG
  {
    int option = LOG_PID | LOG_CONS;
    if (foreground_flag)
      {
	option |= LOG_PERROR;
      }
    openlog("lshd", option, LOG_DAEMON);
    syslog_flag = 1;
  }
#endif /* HAVE_SYSLOG */
#endif
 
443
  init_backend(backend);
Niels Möller's avatar
Niels Möller committed
444
  reaper = make_reaper();
Niels Möller's avatar
Niels Möller committed
445

446
  kex = make_dh_server(dh, public_key, secret_key);
Niels Möller's avatar
Niels Möller committed
447

448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
  ALIST_SET(algorithms, ATOM_DIFFIE_HELLMAN_GROUP1_SHA1, kex);

  make_kexinit
    = make_simple_kexinit(r,
			  make_int_list(1, ATOM_DIFFIE_HELLMAN_GROUP1_SHA1, -1),
			  make_int_list(1, ATOM_SSH_DSS, -1),
			  (preferred_crypto
			   ? make_int_list(1, preferred_crypto, -1)
			   : default_crypto_algorithms()),
			  (preferred_mac
			   ? make_int_list(1, preferred_mac, -1)
			   : default_mac_algorithms()),
			  (preferred_compression
			   ? make_int_list(1, preferred_compression, -1)
			   : default_compression_algorithms()),
			  make_int_list(0, -1));
464
465
466
467

  kexinit_handler = make_kexinit_handler
    (CONNECTION_SERVER,
     make_kexinit, algorithms,
Niels Möller's avatar
Niels Möller committed
468
469
470
471
     make_meta_service
     (make_alist
      (1, ATOM_SSH_USERAUTH,
       make_userauth_service
472
       (make_int_list(1, ATOM_PASSWORD, -1),
473
	make_alist(1, ATOM_PASSWORD,
Niels Möller's avatar
Niels Möller committed
474
475
476
477
478
		   make_unix_userauth
		   (make_alist(1,
			       ATOM_SSH_CONNECTION,
			       make_server_session_service
			       (make_alist(0, -1),
479
480
481
482
483
				make_alist(1
#if WITH_PTY_SUPPORT
					   +1, ATOM_PTY_REQ, make_pty_handler()
#endif /* WITH_PTY_SUPPORT */
					   , ATOM_SHELL, make_shell_handler(backend, reaper),
484
485
486
487
					   -1)),
			       -1)),
		   -1)),
       -1)));
Niels Möller's avatar
Niels Möller committed
488
     
489
490
  if (!io_listen(backend, &local, 
	    make_server_callback(backend,
491
				 "lsh - a free ssh",
492
493
494
#if WITH_SSH1_FALLBACK
				 sshd1 ? make_ssh1_fallback (sshd1) :
#endif /* WITH_SSH1_FALLBACK */
Niels Möller's avatar
Niels Möller committed
495
				 NULL,
496
				 SSH_MAX_PACKET,
Niels Möller's avatar
Niels Möller committed
497
				 r, make_kexinit,
498
				 kexinit_handler)))
Niels Möller's avatar
Niels Möller committed
499
    {
Niels Möller's avatar
Niels Möller committed
500
501
      werror("lshd: listen() failed: (errno = %i): %z\n",
	     errno, strerror(errno));
Niels Möller's avatar
Niels Möller committed
502
503
      return 1;
    }
Niels Möller's avatar
Niels Möller committed
504
  
505
  reaper_run(reaper, backend);
Niels Möller's avatar
Niels Möller committed
506
507
508

  return 0;
}