lsh.c 4.58 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

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

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

#define BLOCK_SIZE 32768

/* Global variable */
struct io_backend backend;

Niels Möller's avatar
Niels Möller committed
54
void usage(void) NORETURN;
Niels Möller's avatar
Niels Möller committed
55

Niels Möller's avatar
Niels Möller committed
56
void usage(void)
Niels Möller's avatar
Niels Möller committed
57 58 59 60
{
  exit(1);
}

Niels Möller's avatar
Niels Möller committed
61 62 63 64 65 66 67 68
struct fake_host_db
{
  struct lookup_verifier super;

  struct signature_algorithm *algorithm;
};

static struct verifier *do_host_lookup(struct lookup_verifier *c,
Niels Möller's avatar
Niels Möller committed
69
				       struct lsh_string *key)
Niels Möller's avatar
Niels Möller committed
70 71
{
  struct fake_host_db *closure = (struct fake_host_db *) c;
Niels Möller's avatar
Niels Möller committed
72 73

  MDEBUG(closure);
Niels Möller's avatar
Niels Möller committed
74 75 76 77 78 79
  
  return MAKE_VERIFIER(closure->algorithm, key->length, key->data);
}

static struct lookup_verifier *make_fake_host_db(struct signature_algorithm *a)
{
80 81 82
  struct fake_host_db *res;

  NEW(res);
Niels Möller's avatar
Niels Möller committed
83 84 85 86 87 88 89

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

  return &res->super;
}

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

Niels Möller's avatar
Niels Möller committed
99 100 101 102 103 104 105 106
  struct lsh_string *random_seed;
  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;
107 108
  struct ssh_service *service;
  
Niels Möller's avatar
Niels Möller committed
109 110 111
  /* For filtering messages. Could perhaps also be used when converting
   * strings to and from UTF8. */
  setlocale(LC_CTYPE, "");
112 113
  /* FIXME: Choose character set depending on the locale */
  set_local_charset(CHARSET_LATIN1);
Niels Möller's avatar
Niels Möller committed
114
  
115
  while((option = getopt(argc, argv, "dl:p:qv")) != -1)
Niels Möller's avatar
Niels Möller committed
116 117 118 119 120
    switch(option)
      {
      case 'p':
	port = optarg;
	break;
121 122 123
      case 'l':
	user = optarg;
	break;
Niels Möller's avatar
Niels Möller committed
124 125 126 127 128 129
      case 'q':
	quiet_flag = 1;
	break;
      case 'd':
	debug_flag = 1;
	break;
Niels Möller's avatar
Niels Möller committed
130 131 132
      case 'v':
	verbose_flag = 1;
	break;
Niels Möller's avatar
Niels Möller committed
133 134 135 136 137 138 139 140
      default:
	usage();
      }

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

  host = argv[optind];
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  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
156

Niels Möller's avatar
Niels Möller committed
157 158 159 160 161 162 163 164 165 166 167 168
  random_seed = ssh_format("%z", "gazonk");
  r = make_poor_random(&sha_algorithm, random_seed);
  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);
  algorithms = make_alist(4,
			  ATOM_ARCFOUR, crypto_rc4_algorithm,
			  ATOM_HMAC_SHA1, make_hmac_algorithm(&sha_algorithm),
			  ATOM_DIFFIE_HELLMAN_GROUP1_SHA1, kex,
			  ATOM_SSH_DSS, make_dss_algorithm(r), -1);
  make_kexinit = make_test_kexinit(r);
Niels Möller's avatar
Niels Möller committed
169

170
  service = make_connection_service
171 172 173 174 175 176
    (make_alist(0, -1),
     make_alist(0, -1));
  
  kexinit_handler = make_kexinit_handler
    (CONNECTION_CLIENT,
     make_kexinit, algorithms,
Niels Möller's avatar
Niels Möller committed
177 178
     /* FIXME: make_service_handler is wrong function: It's for
      * the server side. */
179
     
180 181 182 183 184 185
     make_service_handler(make_alist
			  (1, ATOM_SSH_USERAUTH, 
			   make_client_userauth(ssh_format("%lz", user),
						ATOM_SSH_CONNECTION,
						service))));
  
Niels Möller's avatar
Niels Möller committed
186 187 188 189
  if (!io_connect(&backend, &remote, NULL,
		  make_client_callback(&backend,
				       "lsh - a free ssh",
				       BLOCK_SIZE,
Niels Möller's avatar
Niels Möller committed
190 191
				       r, make_kexinit,
				       kexinit_handler)))
Niels Möller's avatar
Niels Möller committed
192 193 194 195 196
    {
      werror("lsh: Connection failed: %s\n", strerror(errno));
      return 1;
    }
  
Niels Möller's avatar
Niels Möller committed
197
  lsh_string_free(random_seed);
Niels Möller's avatar
Niels Möller committed
198
  
Niels Möller's avatar
Niels Möller committed
199
  io_run(&backend);
Niels Möller's avatar
Niels Möller committed
200 201 202 203 204

  return 0;
}