lsh.c 5.83 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2 3
/* lsh.c
 *
 * client main program
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
 */

Niels Möller's avatar
Niels Möller committed
26
#include <errno.h>
Niels Möller's avatar
Niels Möller committed
27
#include <locale.h>
Niels Möller's avatar
Niels Möller committed
28
#include <stdio.h>
29
#include <stdlib.h>
Niels Möller's avatar
Niels Möller committed
30
#include <string.h>
Niels Möller's avatar
Niels Möller committed
31

32 33
#include <unistd.h>

Niels Möller's avatar
Niels Möller committed
34
#include "getopt.h"
Niels Möller's avatar
Niels Möller committed
35

Niels Möller's avatar
Niels Möller committed
36 37
#include "alist.h"
#include "atoms.h"
38
#include "channel.h"
39
#include "charset.h"
Niels Möller's avatar
Niels Möller committed
40
#include "client.h"
Niels Möller's avatar
Niels Möller committed
41
#include "client_keyexchange.h"
Niels Möller's avatar
Niels Möller committed
42
#include "crypto.h"
Niels Möller's avatar
Niels Möller committed
43 44 45
#include "format.h"
#include "io.h"
#include "randomness.h"
Niels Möller's avatar
Niels Möller committed
46
#include "service.h"
47
/* #include "session.h" */
48
#include "ssh.h"
Niels Möller's avatar
Niels Möller committed
49
#include "userauth.h"
Niels Möller's avatar
Niels Möller committed
50
#include "werror.h"
Niels Möller's avatar
Niels Möller committed
51
#include "xalloc.h"
Niels Möller's avatar
Niels Möller committed
52
#include "compress.h"
Niels Möller's avatar
Niels Möller committed
53

54 55
#include "lsh.c.x"

56
/* Block size for stdout and stderr buffers */
Niels Möller's avatar
Niels Möller committed
57 58
#define BLOCK_SIZE 32768

Niels Möller's avatar
Niels Möller committed
59
void usage(void) NORETURN;
Niels Möller's avatar
Niels Möller committed
60

Niels Möller's avatar
Niels Möller committed
61
void usage(void)
Niels Möller's avatar
Niels Möller committed
62
{
63
  fprintf(stderr, "lsh [-p port] [-l username] [-q] [-d] [-v] host\n");
Niels Möller's avatar
Niels Möller committed
64 65 66
  exit(1);
}

67 68 69 70 71 72 73 74
/* CLASS:
   (class
     (name fake_host_db)
     (super lookup_verifier)
     (vars
       (algorithm object signature_algorithm)))
*/

Niels Möller's avatar
Niels Möller committed
75
static struct verifier *do_host_lookup(struct lookup_verifier *c,
Niels Möller's avatar
Niels Möller committed
76
				       struct lsh_string *key)
Niels Möller's avatar
Niels Möller committed
77
{
78
  CAST(fake_host_db, closure, c);
Niels Möller's avatar
Niels Möller committed
79

Niels Möller's avatar
Niels Möller committed
80 81 82 83 84
  return MAKE_VERIFIER(closure->algorithm, key->length, key->data);
}

static struct lookup_verifier *make_fake_host_db(struct signature_algorithm *a)
{
85
  NEW(fake_host_db, res);
Niels Möller's avatar
Niels Möller committed
86 87 88 89 90 91 92

  res->super.lookup = do_host_lookup;
  res->algorithm = a;

  return &res->super;
}

Niels Möller's avatar
Niels Möller committed
93 94 95
int main(int argc, char **argv)
{
  char *host = NULL;
96
  char *user = NULL;
Niels Möller's avatar
Niels Möller committed
97 98
  char *port = "ssh";
  int option;
Niels Möller's avatar
Niels Möller committed
99 100

  int lsh_exit_code;
101
  
Niels Möller's avatar
Niels Möller committed
102
  struct sockaddr_in remote;
Niels Möller's avatar
Niels Möller committed
103

104
  NEW(io_backend, backend);
105

Niels Möller's avatar
Niels Möller committed
106 107 108 109 110 111 112
  struct randomness *r;
  struct diffie_hellman_method *dh;
  struct keyexchange_algorithm *kex;
  struct alist *algorithms;
  struct make_kexinit *make_kexinit;
  struct packet_handler *kexinit_handler;
  struct lookup_verifier *lookup;
113
  struct ssh_service *service;
114 115

  int in, out, err;
116
  
Niels Möller's avatar
Niels Möller committed
117 118 119
  /* For filtering messages. Could perhaps also be used when converting
   * strings to and from UTF8. */
  setlocale(LC_CTYPE, "");
120 121
  /* FIXME: Choose character set depending on the locale */
  set_local_charset(CHARSET_LATIN1);
Niels Möller's avatar
Niels Möller committed
122
  
123
  while((option = getopt(argc, argv, "dl:p:qv")) != -1)
Niels Möller's avatar
Niels Möller committed
124 125 126 127 128
    switch(option)
      {
      case 'p':
	port = optarg;
	break;
129 130 131
      case 'l':
	user = optarg;
	break;
Niels Möller's avatar
Niels Möller committed
132 133 134 135 136 137
      case 'q':
	quiet_flag = 1;
	break;
      case 'd':
	debug_flag = 1;
	break;
Niels Möller's avatar
Niels Möller committed
138 139 140
      case 'v':
	verbose_flag = 1;
	break;
Niels Möller's avatar
Niels Möller committed
141 142 143 144 145 146 147 148
      default:
	usage();
      }

  if ( (argc - optind) < 1)
    usage();

  host = argv[optind];
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  if (!user)
      user = getenv("LOGNAME");

  if (!user)
    {
      werror("lsh: No user name.\n"
	     "Please use the -l option, or set LOGNAME in the environment\n");
      exit(EXIT_FAILURE);
    }

  if (!get_inaddr(&remote, host, port, "tcp"))
    {
      fprintf(stderr, "No such host or service\n");
      exit(1);
    }
Niels Möller's avatar
Niels Möller committed
164

165
  init_backend(backend);
166
  
Niels Möller's avatar
Niels Möller committed
167
  r = make_reasonably_random();
Niels Möller's avatar
Niels Möller committed
168 169 170 171
  dh = make_dh1(r);
  /* No randomness is needed for verifying signatures */
  lookup = make_fake_host_db(make_dss_algorithm(NULL)); 
  kex = make_dh_client(dh, lookup);
172
  algorithms = make_alist(7,
Niels Möller's avatar
Niels Möller committed
173
			  ATOM_ARCFOUR, &crypto_arcfour_algorithm,
Niels Möller's avatar
Niels Möller committed
174 175
			  ATOM_BLOWFISH_CBC, crypto_cbc(make_blowfish()),
			  ATOM_3DES_CBC, crypto_cbc(make_des3()),
Niels Möller's avatar
Niels Möller committed
176
			  ATOM_HMAC_SHA1, make_hmac_algorithm(&sha_algorithm),
177
			  ATOM_HMAC_MD5, make_hmac_algorithm(&md5_algorithm),
Niels Möller's avatar
Niels Möller committed
178
			  ATOM_DIFFIE_HELLMAN_GROUP1_SHA1, kex,
Niels Möller's avatar
Niels Möller committed
179 180 181 182 183 184 185 186 187 188 189
			  ATOM_SSH_DSS, make_dss_algorithm(r),
			  -1);

#if WITH_ZLIB
  ALIST_SET(algorithms, ATOM_ZLIB, make_zlib());
#endif

#if WITH_IDEA
  ALIST_SET(algorithms, ATOM_IDEA_CBC, crypto_cbc(&idea_algorithm));
#endif
  
Niels Möller's avatar
Niels Möller committed
190
  make_kexinit = make_test_kexinit(r);
Niels Möller's avatar
Niels Möller committed
191

192 193
  /* Dup stdio file descriptors, so that they can be closed without
   * confusing the c library. */
Niels Möller's avatar
Niels Möller committed
194 195 196 197

  /* FIXME: This doesn't really help. (libc is confused by having a
   * non-blocking stderr). It's better to avoid the stdio functions
   * completely. */
Niels Möller's avatar
Niels Möller committed
198
  if ( (in = dup(STDIN_FILENO)) < 0)
199 200 201 202
    {
      werror("Can't dup stdin: %s\n", strerror(errno));
      return EXIT_FAILURE;
    }
Niels Möller's avatar
Niels Möller committed
203
  if ( (out = dup(STDOUT_FILENO)) < 0)
204 205 206 207
    {
      werror("Can't dup stdout: %s\n", strerror(errno));
      return EXIT_FAILURE;
    }
Niels Möller's avatar
Niels Möller committed
208
  if ( (err = dup(STDERR_FILENO)) < 0)
209 210 211 212 213
    {
      werror("Can't dup stderr: %s\n", strerror(errno));
      return EXIT_FAILURE;
    }
  
214
  service = make_connection_service
215
    (make_alist(0, -1),
Niels Möller's avatar
Niels Möller committed
216
     make_alist(0, -1),
217 218 219
     make_client_startup(io_read(backend, in, NULL, NULL),
			 io_write(backend, out, BLOCK_SIZE, NULL),
			 io_write(backend, err, BLOCK_SIZE, NULL),
Niels Möller's avatar
Niels Möller committed
220
			 ATOM_SHELL, ssh_format(""), &lsh_exit_code));
221 222 223 224
  
  kexinit_handler = make_kexinit_handler
    (CONNECTION_CLIENT,
     make_kexinit, algorithms,
Niels Möller's avatar
Niels Möller committed
225 226 227 228 229
     request_service(ATOM_SSH_USERAUTH, 
		     make_client_userauth(ssh_format("%lz", user),
					  ATOM_SSH_CONNECTION,
					  service)));
  
230 231
  if (!io_connect(backend, &remote, NULL,
		  make_client_callback(backend,
Niels Möller's avatar
Niels Möller committed
232
				       "lsh - a free ssh",
233
				       SSH_MAX_PACKET,
Niels Möller's avatar
Niels Möller committed
234 235
				       r, make_kexinit,
				       kexinit_handler)))
Niels Möller's avatar
Niels Möller committed
236 237 238 239 240
    {
      werror("lsh: Connection failed: %s\n", strerror(errno));
      return 1;
    }
  
Niels Möller's avatar
Niels Möller committed
241 242
  /* Exit code if no session is established */
  lsh_exit_code = 17;
Niels Möller's avatar
Niels Möller committed
243
  
244
  io_run(backend);
Niels Möller's avatar
Niels Möller committed
245

Niels Möller's avatar
Niels Möller committed
246
  return lsh_exit_code;
Niels Möller's avatar
Niels Möller committed
247 248 249
}