blob: 333a38e34fc4087ea10e666aa11e55cc8d237b23 [file] [log] [blame]
Ben Lindstromb6434ae2000-12-05 01:15:09 +00001/*
2 * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
3 *
4 * Modification and redistribution in source and binary forms is
5 * permitted provided that due credit is given to the author and the
Ben Lindstroma238f6e2001-06-09 01:30:39 +00006 * OpenBSD project by leaving this copyright notice intact.
Ben Lindstromb6434ae2000-12-05 01:15:09 +00007 */
8
9#include "includes.h"
Ben Lindstrom61c183b2002-06-21 00:09:54 +000010RCSID("$OpenBSD: ssh-keyscan.c,v 1.36 2002/06/16 21:30:58 itojun Exp $");
Ben Lindstromb6434ae2000-12-05 01:15:09 +000011
Kevin Steves54f15b62001-03-14 18:37:13 +000012#include "openbsd-compat/fake-queue.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000013
14#include <openssl/bn.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000015
Ben Lindstrom325e70c2001-08-06 22:41:30 +000016#include <setjmp.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000017#include "xmalloc.h"
18#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000019#include "ssh1.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000020#include "key.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000021#include "kex.h"
22#include "compat.h"
23#include "myproposal.h"
24#include "packet.h"
25#include "dispatch.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000026#include "buffer.h"
27#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include "log.h"
Ben Lindstromd20b8552001-03-05 07:01:18 +000029#include "atomicio.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000030#include "misc.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000031
Ben Lindstrom325e70c2001-08-06 22:41:30 +000032/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
33 Default value is AF_UNSPEC means both IPv4 and IPv6. */
Damien Millerefdc1f12001-11-15 08:40:45 +110034#ifdef IPV4_DEFAULT
35int IPv4or6 = AF_INET;
36#else
Ben Lindstrom325e70c2001-08-06 22:41:30 +000037int IPv4or6 = AF_UNSPEC;
Damien Millerefdc1f12001-11-15 08:40:45 +110038#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +000039
Ben Lindstrom325e70c2001-08-06 22:41:30 +000040int ssh_port = SSH_DEFAULT_PORT;
Kevin Steves76e7d9b2001-09-20 20:30:09 +000041
42#define KT_RSA1 1
43#define KT_DSA 2
44#define KT_RSA 4
45
46int get_keytypes = KT_RSA1; /* Get only RSA1 keys by default */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000047
Ben Lindstromb6434ae2000-12-05 01:15:09 +000048#define MAXMAXFD 256
49
50/* The number of seconds after which to give up on a TCP connection */
51int timeout = 5;
52
53int maxfd;
Ben Lindstromd20b8552001-03-05 07:01:18 +000054#define MAXCON (maxfd - 10)
Ben Lindstromb6434ae2000-12-05 01:15:09 +000055
Kevin Stevesec84dc12000-12-13 17:45:15 +000056#ifdef HAVE___PROGNAME
57extern char *__progname;
58#else
59char *__progname;
60#endif
Ben Lindstromc1e04212001-03-05 07:04:38 +000061fd_set *read_wait;
62size_t read_wait_size;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000063int ncon;
Ben Lindstrom325e70c2001-08-06 22:41:30 +000064int nonfatal_fatal = 0;
65jmp_buf kexjmp;
Ben Lindstrom520b55c2001-09-12 18:05:05 +000066Key *kexjmp_key;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000067
68/*
69 * Keep a connection structure for each file descriptor. The state
70 * associated with file descriptor n is held in fdcon[n].
71 */
72typedef struct Connection {
Ben Lindstrom46c16222000-12-22 01:43:59 +000073 u_char c_status; /* State of connection on this file desc. */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000074#define CS_UNUSED 0 /* File descriptor unused */
75#define CS_CON 1 /* Waiting to connect/read greeting */
76#define CS_SIZE 2 /* Waiting to read initial packet size */
77#define CS_KEYS 3 /* Waiting to read public key packet */
78 int c_fd; /* Quick lookup: c->c_fd == c - fdcon */
79 int c_plen; /* Packet length field for ssh packet */
80 int c_len; /* Total bytes which must be read. */
81 int c_off; /* Length of data read so far. */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000082 int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000083 char *c_namebase; /* Address to free for c_name and c_namelist */
84 char *c_name; /* Hostname of connection for errors */
85 char *c_namelist; /* Pointer to other possible addresses */
86 char *c_output_name; /* Hostname of connection for output */
87 char *c_data; /* Data read from this fd */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000088 Kex *c_kex; /* The key-exchange struct for ssh2 */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000089 struct timeval c_tv; /* Time at which connection gets aborted */
90 TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
91} con;
92
93TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */
94con *fdcon;
95
96/*
97 * This is just a wrapper around fgets() to make it usable.
98 */
99
100/* Stress-test. Increase this later. */
101#define LINEBUF_SIZE 16
102
103typedef struct {
104 char *buf;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000105 u_int size;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000106 int lineno;
107 const char *filename;
108 FILE *stream;
109 void (*errfun) (const char *,...);
110} Linebuf;
111
Ben Lindstrombba81212001-06-25 05:01:22 +0000112static Linebuf *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000113Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
114{
115 Linebuf *lb;
116
117 if (!(lb = malloc(sizeof(*lb)))) {
118 if (errfun)
119 (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
120 return (NULL);
121 }
122 if (filename) {
123 lb->filename = filename;
124 if (!(lb->stream = fopen(filename, "r"))) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000125 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000126 if (errfun)
127 (*errfun) ("%s: %s\n", filename, strerror(errno));
128 return (NULL);
129 }
130 } else {
131 lb->filename = "(stdin)";
132 lb->stream = stdin;
133 }
134
135 if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) {
136 if (errfun)
137 (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000138 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000139 return (NULL);
140 }
141 lb->errfun = errfun;
142 lb->lineno = 0;
143 return (lb);
144}
145
Ben Lindstrombba81212001-06-25 05:01:22 +0000146static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000147Linebuf_free(Linebuf * lb)
148{
149 fclose(lb->stream);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000150 xfree(lb->buf);
151 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000152}
153
Ben Lindstrombba81212001-06-25 05:01:22 +0000154#if 0
155static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000156Linebuf_restart(Linebuf * lb)
157{
158 clearerr(lb->stream);
159 rewind(lb->stream);
160 lb->lineno = 0;
161}
162
Ben Lindstrombba81212001-06-25 05:01:22 +0000163static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000164Linebuf_lineno(Linebuf * lb)
165{
166 return (lb->lineno);
167}
Ben Lindstrombba81212001-06-25 05:01:22 +0000168#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000169
Ben Lindstrombba81212001-06-25 05:01:22 +0000170static char *
Ben Lindstromc791beb2001-02-10 23:18:11 +0000171Linebuf_getline(Linebuf * lb)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000172{
173 int n = 0;
174
175 lb->lineno++;
176 for (;;) {
177 /* Read a line */
178 if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
179 if (ferror(lb->stream) && lb->errfun)
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000180 (*lb->errfun) ("%s: %s\n", lb->filename,
181 strerror(errno));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000182 return (NULL);
183 }
184 n = strlen(lb->buf);
185
186 /* Return it or an error if it fits */
187 if (n > 0 && lb->buf[n - 1] == '\n') {
188 lb->buf[n - 1] = '\0';
189 return (lb->buf);
190 }
191 if (n != lb->size - 1) {
192 if (lb->errfun)
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000193 (*lb->errfun) ("%s: skipping incomplete last line\n",
194 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000195 return (NULL);
196 }
197 /* Double the buffer if we need more space */
198 if (!(lb->buf = realloc(lb->buf, (lb->size *= 2)))) {
199 if (lb->errfun)
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000200 (*lb->errfun) ("linebuf (%s): realloc failed\n",
201 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000202 return (NULL);
203 }
204 }
205}
206
Ben Lindstrombba81212001-06-25 05:01:22 +0000207static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000208fdlim_get(int hard)
209{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000210#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000211 struct rlimit rlfd;
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000212
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000213 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
214 return (-1);
215 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
216 return 10000;
217 else
218 return hard ? rlfd.rlim_max : rlfd.rlim_cur;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000219#elif defined (HAVE_SYSCONF)
220 return sysconf (_SC_OPEN_MAX);
221#else
222 return 10000;
223#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000224}
225
Ben Lindstrombba81212001-06-25 05:01:22 +0000226static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000227fdlim_set(int lim)
228{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000229#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000230 struct rlimit rlfd;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000231#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000232 if (lim <= 0)
233 return (-1);
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000234#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000235 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
236 return (-1);
237 rlfd.rlim_cur = lim;
238 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
239 return (-1);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000240#elif defined (HAVE_SETDTABLESIZE)
Kevin Steves28a7f262001-02-05 15:43:59 +0000241 setdtablesize(lim);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000242#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000243 return (0);
244}
245
246/*
247 * This is an strsep function that returns a null field for adjacent
248 * separators. This is the same as the 4.4BSD strsep, but different from the
249 * one in the GNU libc.
250 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000251static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000252xstrsep(char **str, const char *delim)
253{
254 char *s, *e;
255
256 if (!**str)
257 return (NULL);
258
259 s = *str;
260 e = s + strcspn(s, delim);
261
262 if (*e != '\0')
263 *e++ = '\0';
264 *str = e;
265
266 return (s);
267}
268
269/*
270 * Get the next non-null token (like GNU strsep). Strsep() will return a
271 * null token for two adjacent separators, so we may have to loop.
272 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000273static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000274strnnsep(char **stringp, char *delim)
275{
276 char *tok;
277
278 do {
279 tok = xstrsep(stringp, delim);
280 } while (tok && *tok == '\0');
281 return (tok);
282}
283
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000284static Key *
285keygrab_ssh1(con *c)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000286{
287 static Key *rsa;
288 static Buffer msg;
289
290 if (rsa == NULL) {
291 buffer_init(&msg);
292 rsa = key_new(KEY_RSA1);
293 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000294 buffer_append(&msg, c->c_data, c->c_plen);
295 buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000296 if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000297 error("%s: invalid packet type", c->c_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000298 buffer_clear(&msg);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000299 return NULL;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000300 }
301 buffer_consume(&msg, 8); /* cookie */
302
303 /* server key */
304 (void) buffer_get_int(&msg);
305 buffer_get_bignum(&msg, rsa->rsa->e);
306 buffer_get_bignum(&msg, rsa->rsa->n);
307
308 /* host key */
309 (void) buffer_get_int(&msg);
310 buffer_get_bignum(&msg, rsa->rsa->e);
311 buffer_get_bignum(&msg, rsa->rsa->n);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000312
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000313 buffer_clear(&msg);
314
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000315 return (rsa);
316}
317
318static int
319hostjump(Key *hostkey)
320{
Ben Lindstrom520b55c2001-09-12 18:05:05 +0000321 kexjmp_key = hostkey;
322 longjmp(kexjmp, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000323}
324
325static int
326ssh2_capable(int remote_major, int remote_minor)
327{
328 switch (remote_major) {
329 case 1:
330 if (remote_minor == 99)
331 return 1;
332 break;
333 case 2:
334 return 1;
335 default:
336 break;
337 }
338 return 0;
339}
340
341static Key *
342keygrab_ssh2(con *c)
343{
344 int j;
345
346 packet_set_connection(c->c_fd, c->c_fd);
347 enable_compat20();
348 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
349 "ssh-dss": "ssh-rsa";
350 c->c_kex = kex_setup(myproposal);
351 c->c_kex->verify_host_key = hostjump;
352
353 if (!(j = setjmp(kexjmp))) {
354 nonfatal_fatal = 1;
355 dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
356 fprintf(stderr, "Impossible! dispatch_run() returned!\n");
357 exit(1);
358 }
359 nonfatal_fatal = 0;
360 xfree(c->c_kex);
361 c->c_kex = NULL;
362 packet_close();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000363
Ben Lindstrom520b55c2001-09-12 18:05:05 +0000364 return j < 0? NULL : kexjmp_key;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000365}
366
367static void
368keyprint(con *c, Key *key)
369{
370 if (!key)
371 return;
372
373 fprintf(stdout, "%s ", c->c_output_name ? c->c_output_name : c->c_name);
374 key_write(key, stdout);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000375 fputs("\n", stdout);
376}
377
Ben Lindstrombba81212001-06-25 05:01:22 +0000378static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000379tcpconnect(char *host)
380{
381 struct addrinfo hints, *ai, *aitop;
382 char strport[NI_MAXSERV];
383 int gaierr, s = -1;
384
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000385 snprintf(strport, sizeof strport, "%d", ssh_port);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000386 memset(&hints, 0, sizeof(hints));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000387 hints.ai_family = IPv4or6;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000388 hints.ai_socktype = SOCK_STREAM;
389 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
390 fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
391 for (ai = aitop; ai; ai = ai->ai_next) {
392 s = socket(ai->ai_family, SOCK_STREAM, 0);
393 if (s < 0) {
394 error("socket: %s", strerror(errno));
395 continue;
396 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000397 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000398 fatal("F_SETFL: %s", strerror(errno));
399 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
400 errno != EINPROGRESS)
401 error("connect (`%s'): %s", host, strerror(errno));
402 else
403 break;
404 close(s);
405 s = -1;
406 }
407 freeaddrinfo(aitop);
408 return s;
409}
410
Ben Lindstrombba81212001-06-25 05:01:22 +0000411static int
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000412conalloc(char *iname, char *oname, int keytype)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000413{
414 int s;
415 char *namebase, *name, *namelist;
416
417 namebase = namelist = xstrdup(iname);
418
419 do {
420 name = xstrsep(&namelist, ",");
421 if (!name) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000422 xfree(namebase);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000423 return (-1);
424 }
425 } while ((s = tcpconnect(name)) < 0);
426
427 if (s >= maxfd)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000428 fatal("conalloc: fdno %d too high", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000429 if (fdcon[s].c_status)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000430 fatal("conalloc: attempt to reuse fdno %d", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000431
432 fdcon[s].c_fd = s;
433 fdcon[s].c_status = CS_CON;
434 fdcon[s].c_namebase = namebase;
435 fdcon[s].c_name = name;
436 fdcon[s].c_namelist = namelist;
437 fdcon[s].c_output_name = xstrdup(oname);
438 fdcon[s].c_data = (char *) &fdcon[s].c_plen;
439 fdcon[s].c_len = 4;
440 fdcon[s].c_off = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000441 fdcon[s].c_keytype = keytype;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000442 gettimeofday(&fdcon[s].c_tv, NULL);
443 fdcon[s].c_tv.tv_sec += timeout;
444 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000445 FD_SET(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000446 ncon++;
447 return (s);
448}
449
Ben Lindstrombba81212001-06-25 05:01:22 +0000450static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000451confree(int s)
452{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000453 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000454 fatal("confree: attempt to free bad fdno %d", s);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000455 close(s);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000456 xfree(fdcon[s].c_namebase);
457 xfree(fdcon[s].c_output_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000458 if (fdcon[s].c_status == CS_KEYS)
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000459 xfree(fdcon[s].c_data);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000460 fdcon[s].c_status = CS_UNUSED;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000461 fdcon[s].c_keytype = 0;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000462 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000463 FD_CLR(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000464 ncon--;
465}
466
Ben Lindstrombba81212001-06-25 05:01:22 +0000467static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000468contouch(int s)
469{
470 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
471 gettimeofday(&fdcon[s].c_tv, NULL);
472 fdcon[s].c_tv.tv_sec += timeout;
473 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
474}
475
Ben Lindstrombba81212001-06-25 05:01:22 +0000476static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000477conrecycle(int s)
478{
479 int ret;
480 con *c = &fdcon[s];
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000481
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000482 ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000483 confree(s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000484 return (ret);
485}
486
Ben Lindstrombba81212001-06-25 05:01:22 +0000487static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000488congreet(int s)
489{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000490 char buf[256], *cp;
Damien Miller83c02ef2001-12-21 12:45:43 +1100491 char remote_version[sizeof buf];
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000492 size_t bufsiz;
Damien Miller83c02ef2001-12-21 12:45:43 +1100493 int remote_major, remote_minor, n = 0;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000494 con *c = &fdcon[s];
495
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000496 bufsiz = sizeof(buf);
497 cp = buf;
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000498 while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n') {
499 if (*cp == '\r')
500 *cp = '\n';
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000501 cp++;
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000502 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000503 if (n < 0) {
504 if (errno != ECONNREFUSED)
505 error("read (%s): %s", c->c_name, strerror(errno));
506 conrecycle(s);
507 return;
508 }
Ben Lindstrom6b28c352002-03-05 01:54:52 +0000509 if (n == 0) {
510 error("%s: Connection closed by remote host", c->c_name);
511 conrecycle(s);
512 return;
513 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000514 if (*cp != '\n' && *cp != '\r') {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000515 error("%s: bad greeting", c->c_name);
516 confree(s);
517 return;
518 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000519 *cp = '\0';
Damien Miller83c02ef2001-12-21 12:45:43 +1100520 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
521 &remote_major, &remote_minor, remote_version) == 3)
522 compat_datafellows(remote_version);
523 else
524 datafellows = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000525 if (c->c_keytype != KT_RSA1) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000526 if (!ssh2_capable(remote_major, remote_minor)) {
527 debug("%s doesn't support ssh2", c->c_name);
528 confree(s);
529 return;
530 }
Damien Miller83c02ef2001-12-21 12:45:43 +1100531 } else if (remote_major != 1) {
532 debug("%s doesn't support ssh1", c->c_name);
533 confree(s);
534 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000535 }
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000536 fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000537 n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
538 c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
539 c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000540 if (atomicio(write, s, buf, n) != n) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000541 error("write (%s): %s", c->c_name, strerror(errno));
542 confree(s);
543 return;
544 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000545 if (c->c_keytype != KT_RSA1) {
546 keyprint(c, keygrab_ssh2(c));
547 confree(s);
548 return;
549 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000550 c->c_status = CS_SIZE;
551 contouch(s);
552}
553
Ben Lindstrombba81212001-06-25 05:01:22 +0000554static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000555conread(int s)
556{
557 int n;
558 con *c = &fdcon[s];
559
560 if (c->c_status == CS_CON) {
561 congreet(s);
562 return;
563 }
564 n = read(s, c->c_data + c->c_off, c->c_len - c->c_off);
565 if (n < 0) {
566 error("read (%s): %s", c->c_name, strerror(errno));
567 confree(s);
568 return;
569 }
570 c->c_off += n;
571
572 if (c->c_off == c->c_len)
573 switch (c->c_status) {
574 case CS_SIZE:
575 c->c_plen = htonl(c->c_plen);
576 c->c_len = c->c_plen + 8 - (c->c_plen & 7);
577 c->c_off = 0;
578 c->c_data = xmalloc(c->c_len);
579 c->c_status = CS_KEYS;
580 break;
581 case CS_KEYS:
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000582 keyprint(c, keygrab_ssh1(c));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000583 confree(s);
584 return;
585 break;
586 default:
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000587 fatal("conread: invalid status %d", c->c_status);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000588 break;
589 }
590
591 contouch(s);
592}
593
Ben Lindstrombba81212001-06-25 05:01:22 +0000594static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000595conloop(void)
596{
Ben Lindstromc1e04212001-03-05 07:04:38 +0000597 fd_set *r, *e;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000598 struct timeval seltime, now;
599 int i;
600 con *c;
601
602 gettimeofday(&now, NULL);
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000603 c = TAILQ_FIRST(&tq);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000604
Ben Lindstromd20b8552001-03-05 07:01:18 +0000605 if (c && (c->c_tv.tv_sec > now.tv_sec ||
606 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000607 seltime = c->c_tv;
608 seltime.tv_sec -= now.tv_sec;
609 seltime.tv_usec -= now.tv_usec;
Ben Lindstromc791beb2001-02-10 23:18:11 +0000610 if (seltime.tv_usec < 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000611 seltime.tv_usec += 1000000;
612 seltime.tv_sec--;
613 }
614 } else
615 seltime.tv_sec = seltime.tv_usec = 0;
616
Ben Lindstromc1e04212001-03-05 07:04:38 +0000617 r = xmalloc(read_wait_size);
618 memcpy(r, read_wait, read_wait_size);
619 e = xmalloc(read_wait_size);
620 memcpy(e, read_wait, read_wait_size);
621
622 while (select(maxfd, r, NULL, e, &seltime) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000623 (errno == EAGAIN || errno == EINTR))
624 ;
625
Ben Lindstromd20b8552001-03-05 07:01:18 +0000626 for (i = 0; i < maxfd; i++) {
Ben Lindstromc1e04212001-03-05 07:04:38 +0000627 if (FD_ISSET(i, e)) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000628 error("%s: exception!", fdcon[i].c_name);
629 confree(i);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000630 } else if (FD_ISSET(i, r))
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000631 conread(i);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000632 }
Ben Lindstromc1e04212001-03-05 07:04:38 +0000633 xfree(r);
634 xfree(e);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000635
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000636 c = TAILQ_FIRST(&tq);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000637 while (c && (c->c_tv.tv_sec < now.tv_sec ||
638 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000639 int s = c->c_fd;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000640
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000641 c = TAILQ_NEXT(c, c_link);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000642 conrecycle(s);
643 }
644}
645
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000646static void
647do_host(char *host)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000648{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000649 char *name = strnnsep(&host, " \t\n");
650 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000651
Ben Lindstromeaffb9d2001-12-06 16:28:19 +0000652 if (name == NULL)
653 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000654 for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
655 if (get_keytypes & j) {
656 while (ncon >= MAXCON)
657 conloop();
658 conalloc(name, *host ? host : name, j);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000659 }
660 }
661}
662
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000663void
664fatal(const char *fmt,...)
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000665{
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000666 va_list args;
667 va_start(args, fmt);
668 do_log(SYSLOG_LEVEL_FATAL, fmt, args);
669 va_end(args);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000670 if (nonfatal_fatal)
671 longjmp(kexjmp, -1);
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000672 else
673 fatal_cleanup();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000674}
675
676static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000677usage(void)
678{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000679 fprintf(stderr, "Usage: %s [options] host ...\n",
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000680 __progname);
681 fprintf(stderr, "Options:\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000682 fprintf(stderr, " -f file Read hosts or addresses from file.\n");
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000683 fprintf(stderr, " -p port Connect to the specified port.\n");
684 fprintf(stderr, " -t keytype Specify the host key type.\n");
685 fprintf(stderr, " -T timeout Set connection timeout.\n");
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000686 fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
687 fprintf(stderr, " -4 Use IPv4 only.\n");
688 fprintf(stderr, " -6 Use IPv6 only.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000689 exit(1);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000690}
691
692int
693main(int argc, char **argv)
694{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000695 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
696 int opt, fopt_count = 0;
697 char *tname;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000698
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000699 extern int optind;
700 extern char *optarg;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000701
Kevin Stevesec84dc12000-12-13 17:45:15 +0000702 __progname = get_progname(argv[0]);
Ben Lindstrom4e088e42001-10-10 20:45:43 +0000703 init_rng();
704 seed_rng();
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000705 TAILQ_INIT(&tq);
706
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000707 if (argc <= 1)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000708 usage();
709
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000710 while ((opt = getopt(argc, argv, "v46p:T:t:f:")) != -1) {
711 switch (opt) {
712 case 'p':
713 ssh_port = a2port(optarg);
714 if (ssh_port == 0) {
715 fprintf(stderr, "Bad port '%s'\n", optarg);
716 exit(1);
717 }
718 break;
719 case 'T':
720 timeout = atoi(optarg);
721 if (timeout <= 0)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000722 usage();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000723 break;
724 case 'v':
725 if (!debug_flag) {
726 debug_flag = 1;
727 log_level = SYSLOG_LEVEL_DEBUG1;
728 }
729 else if (log_level < SYSLOG_LEVEL_DEBUG3)
730 log_level++;
731 else
732 fatal("Too high debugging level.");
733 break;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000734 case 'f':
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000735 if (strcmp(optarg, "-") == 0)
736 optarg = NULL;
737 argv[fopt_count++] = optarg;
738 break;
739 case 't':
740 get_keytypes = 0;
741 tname = strtok(optarg, ",");
742 while (tname) {
743 int type = key_type_from_name(tname);
744 switch (type) {
745 case KEY_RSA1:
746 get_keytypes |= KT_RSA1;
747 break;
748 case KEY_DSA:
749 get_keytypes |= KT_DSA;
750 break;
751 case KEY_RSA:
752 get_keytypes |= KT_RSA;
753 break;
754 case KEY_UNSPEC:
Ben Lindstrom28c603b2001-12-06 16:45:10 +0000755 fatal("unknown key type %s", tname);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000756 }
757 tname = strtok(NULL, ",");
758 }
759 break;
760 case '4':
761 IPv4or6 = AF_INET;
762 break;
763 case '6':
764 IPv4or6 = AF_INET6;
765 break;
766 case '?':
767 default:
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000768 usage();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000769 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000770 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000771 if (optind == argc && !fopt_count)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000772 usage();
773
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000774 log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000775
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000776 maxfd = fdlim_get(1);
777 if (maxfd < 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000778 fatal("%s: fdlim_get: bad value", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000779 if (maxfd > MAXMAXFD)
780 maxfd = MAXMAXFD;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000781 if (MAXCON <= 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000782 fatal("%s: not enough file descriptors", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000783 if (maxfd > fdlim_get(0))
784 fdlim_set(maxfd);
785 fdcon = xmalloc(maxfd * sizeof(con));
Ben Lindstromc791beb2001-02-10 23:18:11 +0000786 memset(fdcon, 0, maxfd * sizeof(con));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000787
Ben Lindstromc1e04212001-03-05 07:04:38 +0000788 read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
789 read_wait = xmalloc(read_wait_size);
790 memset(read_wait, 0, read_wait_size);
791
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000792 if (fopt_count) {
793 Linebuf *lb;
794 char *line;
795 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000796
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000797 for (j = 0; j < fopt_count; j++) {
798 lb = Linebuf_alloc(argv[j], error);
Ben Lindstrom78bbd9e2001-09-12 17:10:40 +0000799 if (!lb)
800 continue;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000801 while ((line = Linebuf_getline(lb)) != NULL)
802 do_host(line);
803 Linebuf_free(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000804 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000805 }
806
807 while (optind < argc)
808 do_host(argv[optind++]);
809
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000810 while (ncon > 0)
811 conloop();
812
813 return (0);
814}