blob: e47460a8ae07a673aeca9b9ba53b0253e1617799 [file] [log] [blame]
Damien Miller9aec9192006-08-05 10:57:45 +10001/* $OpenBSD: ssh-keyscan.c,v 1.70 2006/07/25 02:59:21 stevesk Exp $ */
Ben Lindstromb6434ae2000-12-05 01:15:09 +00002/*
3 * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
4 *
5 * Modification and redistribution in source and binary forms is
6 * permitted provided that due credit is given to the author and the
Ben Lindstroma238f6e2001-06-09 01:30:39 +00007 * OpenBSD project by leaving this copyright notice intact.
Ben Lindstromb6434ae2000-12-05 01:15:09 +00008 */
9
10#include "includes.h"
Damien Millercd4223c2006-03-15 11:22:47 +110011
Damien Miller9b481512002-09-12 10:43:29 +100012#include "openbsd-compat/sys-queue.h"
Damien Millercd4223c2006-03-15 11:22:47 +110013#include <sys/resource.h>
Damien Miller9aec9192006-08-05 10:57:45 +100014#ifdef HAVE_SYS_TIME_H
15# include <sys/time.h>
16#endif
Damien Millere3476ed2006-07-24 14:13:33 +100017
18#include <openssl/bn.h>
19
Damien Millerb8fe89c2006-07-24 14:51:00 +100020#include <netdb.h>
Darren Tuckerdeecec92006-07-12 22:44:34 +100021#include <errno.h>
Darren Tucker5d196262006-07-12 22:15:16 +100022#include <stdarg.h>
Damien Millerbe43ebf2006-07-24 13:51:51 +100023#include <setjmp.h>
Damien Millere3476ed2006-07-24 14:13:33 +100024#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100025#include <unistd.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000026
Ben Lindstromb6434ae2000-12-05 01:15:09 +000027#include "xmalloc.h"
28#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000029#include "ssh1.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000030#include "key.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000031#include "kex.h"
32#include "compat.h"
33#include "myproposal.h"
34#include "packet.h"
35#include "dispatch.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000036#include "buffer.h"
37#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038#include "log.h"
Ben Lindstromd20b8552001-03-05 07:01:18 +000039#include "atomicio.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000040#include "misc.h"
Damien Millerdb7b8172005-03-01 21:48:03 +110041#include "hostfile.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000042
Ben Lindstrom325e70c2001-08-06 22:41:30 +000043/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
44 Default value is AF_UNSPEC means both IPv4 and IPv6. */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000045int IPv4or6 = AF_UNSPEC;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000046
Ben Lindstrom325e70c2001-08-06 22:41:30 +000047int ssh_port = SSH_DEFAULT_PORT;
Kevin Steves76e7d9b2001-09-20 20:30:09 +000048
49#define KT_RSA1 1
50#define KT_DSA 2
51#define KT_RSA 4
52
53int get_keytypes = KT_RSA1; /* Get only RSA1 keys by default */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000054
Damien Millerdb7b8172005-03-01 21:48:03 +110055int hash_hosts = 0; /* Hash hostname on output */
56
Ben Lindstromb6434ae2000-12-05 01:15:09 +000057#define MAXMAXFD 256
58
59/* The number of seconds after which to give up on a TCP connection */
60int timeout = 5;
61
62int maxfd;
Ben Lindstromd20b8552001-03-05 07:01:18 +000063#define MAXCON (maxfd - 10)
Ben Lindstromb6434ae2000-12-05 01:15:09 +000064
Kevin Stevesec84dc12000-12-13 17:45:15 +000065extern char *__progname;
Ben Lindstromc1e04212001-03-05 07:04:38 +000066fd_set *read_wait;
Damien Miller07d86be2006-03-26 14:19:21 +110067size_t read_wait_nfdset;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000068int ncon;
Ben Lindstrom325e70c2001-08-06 22:41:30 +000069int nonfatal_fatal = 0;
70jmp_buf kexjmp;
Ben Lindstrom520b55c2001-09-12 18:05:05 +000071Key *kexjmp_key;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000072
73/*
74 * Keep a connection structure for each file descriptor. The state
75 * associated with file descriptor n is held in fdcon[n].
76 */
77typedef struct Connection {
Ben Lindstrom46c16222000-12-22 01:43:59 +000078 u_char c_status; /* State of connection on this file desc. */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000079#define CS_UNUSED 0 /* File descriptor unused */
80#define CS_CON 1 /* Waiting to connect/read greeting */
81#define CS_SIZE 2 /* Waiting to read initial packet size */
82#define CS_KEYS 3 /* Waiting to read public key packet */
83 int c_fd; /* Quick lookup: c->c_fd == c - fdcon */
84 int c_plen; /* Packet length field for ssh packet */
85 int c_len; /* Total bytes which must be read. */
86 int c_off; /* Length of data read so far. */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000087 int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000088 char *c_namebase; /* Address to free for c_name and c_namelist */
89 char *c_name; /* Hostname of connection for errors */
90 char *c_namelist; /* Pointer to other possible addresses */
91 char *c_output_name; /* Hostname of connection for output */
92 char *c_data; /* Data read from this fd */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000093 Kex *c_kex; /* The key-exchange struct for ssh2 */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000094 struct timeval c_tv; /* Time at which connection gets aborted */
95 TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
96} con;
97
98TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */
99con *fdcon;
100
101/*
102 * This is just a wrapper around fgets() to make it usable.
103 */
104
105/* Stress-test. Increase this later. */
106#define LINEBUF_SIZE 16
107
108typedef struct {
109 char *buf;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000110 u_int size;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000111 int lineno;
112 const char *filename;
113 FILE *stream;
114 void (*errfun) (const char *,...);
115} Linebuf;
116
Ben Lindstrombba81212001-06-25 05:01:22 +0000117static Linebuf *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000118Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
119{
120 Linebuf *lb;
121
122 if (!(lb = malloc(sizeof(*lb)))) {
123 if (errfun)
Ben Lindstrom04f9af72002-07-04 00:03:56 +0000124 (*errfun) ("linebuf (%s): malloc failed\n",
125 filename ? filename : "(stdin)");
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000126 return (NULL);
127 }
128 if (filename) {
129 lb->filename = filename;
130 if (!(lb->stream = fopen(filename, "r"))) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000131 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000132 if (errfun)
133 (*errfun) ("%s: %s\n", filename, strerror(errno));
134 return (NULL);
135 }
136 } else {
137 lb->filename = "(stdin)";
138 lb->stream = stdin;
139 }
140
Damien Miller3bbaba62006-03-26 13:59:38 +1100141 if (!(lb->buf = malloc((lb->size = LINEBUF_SIZE)))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000142 if (errfun)
143 (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000144 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000145 return (NULL);
146 }
147 lb->errfun = errfun;
148 lb->lineno = 0;
149 return (lb);
150}
151
Ben Lindstrombba81212001-06-25 05:01:22 +0000152static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000153Linebuf_free(Linebuf * lb)
154{
155 fclose(lb->stream);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000156 xfree(lb->buf);
157 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000158}
159
Ben Lindstrombba81212001-06-25 05:01:22 +0000160#if 0
161static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000162Linebuf_restart(Linebuf * lb)
163{
164 clearerr(lb->stream);
165 rewind(lb->stream);
166 lb->lineno = 0;
167}
168
Ben Lindstrombba81212001-06-25 05:01:22 +0000169static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000170Linebuf_lineno(Linebuf * lb)
171{
172 return (lb->lineno);
173}
Ben Lindstrombba81212001-06-25 05:01:22 +0000174#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000175
Ben Lindstrombba81212001-06-25 05:01:22 +0000176static char *
Ben Lindstromc791beb2001-02-10 23:18:11 +0000177Linebuf_getline(Linebuf * lb)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000178{
Damien Millereccb9de2005-06-17 12:59:34 +1000179 size_t n = 0;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000180 void *p;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000181
182 lb->lineno++;
183 for (;;) {
184 /* Read a line */
185 if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
186 if (ferror(lb->stream) && lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000187 (*lb->errfun)("%s: %s\n", lb->filename,
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000188 strerror(errno));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000189 return (NULL);
190 }
191 n = strlen(lb->buf);
192
193 /* Return it or an error if it fits */
194 if (n > 0 && lb->buf[n - 1] == '\n') {
195 lb->buf[n - 1] = '\0';
196 return (lb->buf);
197 }
198 if (n != lb->size - 1) {
199 if (lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000200 (*lb->errfun)("%s: skipping incomplete last line\n",
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000201 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000202 return (NULL);
203 }
204 /* Double the buffer if we need more space */
Ben Lindstrom965710f2002-07-07 22:17:22 +0000205 lb->size *= 2;
206 if ((p = realloc(lb->buf, lb->size)) == NULL) {
207 lb->size /= 2;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000208 if (lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000209 (*lb->errfun)("linebuf (%s): realloc failed\n",
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000210 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000211 return (NULL);
212 }
Ben Lindstrom965710f2002-07-07 22:17:22 +0000213 lb->buf = p;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000214 }
215}
216
Ben Lindstrombba81212001-06-25 05:01:22 +0000217static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000218fdlim_get(int hard)
219{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000220#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000221 struct rlimit rlfd;
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000222
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000223 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
224 return (-1);
225 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
Damien Millere00074a2003-11-24 13:07:45 +1100226 return SSH_SYSFDMAX;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000227 else
228 return hard ? rlfd.rlim_max : rlfd.rlim_cur;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000229#else
Damien Millere00074a2003-11-24 13:07:45 +1100230 return SSH_SYSFDMAX;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000231#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000232}
233
Ben Lindstrombba81212001-06-25 05:01:22 +0000234static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000235fdlim_set(int lim)
236{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000237#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000238 struct rlimit rlfd;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000239#endif
Ben Lindstrom5c98db52002-07-07 22:25:29 +0000240
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000241 if (lim <= 0)
242 return (-1);
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000243#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000244 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
245 return (-1);
246 rlfd.rlim_cur = lim;
247 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
248 return (-1);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000249#elif defined (HAVE_SETDTABLESIZE)
Kevin Steves28a7f262001-02-05 15:43:59 +0000250 setdtablesize(lim);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000251#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000252 return (0);
253}
254
255/*
256 * This is an strsep function that returns a null field for adjacent
257 * separators. This is the same as the 4.4BSD strsep, but different from the
258 * one in the GNU libc.
259 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000260static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000261xstrsep(char **str, const char *delim)
262{
263 char *s, *e;
264
265 if (!**str)
266 return (NULL);
267
268 s = *str;
269 e = s + strcspn(s, delim);
270
271 if (*e != '\0')
272 *e++ = '\0';
273 *str = e;
274
275 return (s);
276}
277
278/*
279 * Get the next non-null token (like GNU strsep). Strsep() will return a
280 * null token for two adjacent separators, so we may have to loop.
281 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000282static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000283strnnsep(char **stringp, char *delim)
284{
285 char *tok;
286
287 do {
288 tok = xstrsep(stringp, delim);
289 } while (tok && *tok == '\0');
290 return (tok);
291}
292
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000293static Key *
294keygrab_ssh1(con *c)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000295{
296 static Key *rsa;
297 static Buffer msg;
298
299 if (rsa == NULL) {
300 buffer_init(&msg);
301 rsa = key_new(KEY_RSA1);
302 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000303 buffer_append(&msg, c->c_data, c->c_plen);
304 buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000305 if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000306 error("%s: invalid packet type", c->c_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000307 buffer_clear(&msg);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000308 return NULL;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000309 }
310 buffer_consume(&msg, 8); /* cookie */
311
312 /* server key */
313 (void) buffer_get_int(&msg);
314 buffer_get_bignum(&msg, rsa->rsa->e);
315 buffer_get_bignum(&msg, rsa->rsa->n);
316
317 /* host key */
318 (void) buffer_get_int(&msg);
319 buffer_get_bignum(&msg, rsa->rsa->e);
320 buffer_get_bignum(&msg, rsa->rsa->n);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000321
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000322 buffer_clear(&msg);
323
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000324 return (rsa);
325}
326
327static int
328hostjump(Key *hostkey)
329{
Ben Lindstrom520b55c2001-09-12 18:05:05 +0000330 kexjmp_key = hostkey;
331 longjmp(kexjmp, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000332}
333
334static int
335ssh2_capable(int remote_major, int remote_minor)
336{
337 switch (remote_major) {
338 case 1:
339 if (remote_minor == 99)
340 return 1;
341 break;
342 case 2:
343 return 1;
344 default:
345 break;
346 }
347 return 0;
348}
349
350static Key *
351keygrab_ssh2(con *c)
352{
353 int j;
354
355 packet_set_connection(c->c_fd, c->c_fd);
356 enable_compat20();
357 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
358 "ssh-dss": "ssh-rsa";
359 c->c_kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100360 c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000361 c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100362 c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100363 c->c_kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000364 c->c_kex->verify_host_key = hostjump;
365
366 if (!(j = setjmp(kexjmp))) {
367 nonfatal_fatal = 1;
368 dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
369 fprintf(stderr, "Impossible! dispatch_run() returned!\n");
370 exit(1);
371 }
372 nonfatal_fatal = 0;
373 xfree(c->c_kex);
374 c->c_kex = NULL;
375 packet_close();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000376
Ben Lindstrom520b55c2001-09-12 18:05:05 +0000377 return j < 0? NULL : kexjmp_key;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000378}
379
380static void
381keyprint(con *c, Key *key)
382{
Damien Millerdb7b8172005-03-01 21:48:03 +1100383 char *host = c->c_output_name ? c->c_output_name : c->c_name;
384
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000385 if (!key)
386 return;
Damien Millerdb7b8172005-03-01 21:48:03 +1100387 if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL)
388 fatal("host_hash failed");
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000389
Damien Millerdb7b8172005-03-01 21:48:03 +1100390 fprintf(stdout, "%s ", host);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000391 key_write(key, stdout);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000392 fputs("\n", stdout);
393}
394
Ben Lindstrombba81212001-06-25 05:01:22 +0000395static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000396tcpconnect(char *host)
397{
398 struct addrinfo hints, *ai, *aitop;
399 char strport[NI_MAXSERV];
400 int gaierr, s = -1;
401
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000402 snprintf(strport, sizeof strport, "%d", ssh_port);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000403 memset(&hints, 0, sizeof(hints));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000404 hints.ai_family = IPv4or6;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000405 hints.ai_socktype = SOCK_STREAM;
406 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
407 fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
408 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller2372ace2003-05-14 13:42:23 +1000409 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000410 if (s < 0) {
411 error("socket: %s", strerror(errno));
412 continue;
413 }
Damien Miller232711f2004-06-15 10:35:30 +1000414 if (set_nonblock(s) == -1)
415 fatal("%s: set_nonblock(%d)", __func__, s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000416 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
417 errno != EINPROGRESS)
418 error("connect (`%s'): %s", host, strerror(errno));
419 else
420 break;
421 close(s);
422 s = -1;
423 }
424 freeaddrinfo(aitop);
425 return s;
426}
427
Ben Lindstrombba81212001-06-25 05:01:22 +0000428static int
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000429conalloc(char *iname, char *oname, int keytype)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000430{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000431 char *namebase, *name, *namelist;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000432 int s;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000433
434 namebase = namelist = xstrdup(iname);
435
436 do {
437 name = xstrsep(&namelist, ",");
438 if (!name) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000439 xfree(namebase);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000440 return (-1);
441 }
442 } while ((s = tcpconnect(name)) < 0);
443
444 if (s >= maxfd)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000445 fatal("conalloc: fdno %d too high", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000446 if (fdcon[s].c_status)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000447 fatal("conalloc: attempt to reuse fdno %d", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000448
449 fdcon[s].c_fd = s;
450 fdcon[s].c_status = CS_CON;
451 fdcon[s].c_namebase = namebase;
452 fdcon[s].c_name = name;
453 fdcon[s].c_namelist = namelist;
454 fdcon[s].c_output_name = xstrdup(oname);
455 fdcon[s].c_data = (char *) &fdcon[s].c_plen;
456 fdcon[s].c_len = 4;
457 fdcon[s].c_off = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000458 fdcon[s].c_keytype = keytype;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000459 gettimeofday(&fdcon[s].c_tv, NULL);
460 fdcon[s].c_tv.tv_sec += timeout;
461 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000462 FD_SET(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000463 ncon++;
464 return (s);
465}
466
Ben Lindstrombba81212001-06-25 05:01:22 +0000467static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000468confree(int s)
469{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000470 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000471 fatal("confree: attempt to free bad fdno %d", s);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000472 close(s);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000473 xfree(fdcon[s].c_namebase);
474 xfree(fdcon[s].c_output_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000475 if (fdcon[s].c_status == CS_KEYS)
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000476 xfree(fdcon[s].c_data);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000477 fdcon[s].c_status = CS_UNUSED;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000478 fdcon[s].c_keytype = 0;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000479 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000480 FD_CLR(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000481 ncon--;
482}
483
Ben Lindstrombba81212001-06-25 05:01:22 +0000484static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000485contouch(int s)
486{
487 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
488 gettimeofday(&fdcon[s].c_tv, NULL);
489 fdcon[s].c_tv.tv_sec += timeout;
490 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
491}
492
Ben Lindstrombba81212001-06-25 05:01:22 +0000493static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000494conrecycle(int s)
495{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000496 con *c = &fdcon[s];
Ben Lindstrom965710f2002-07-07 22:17:22 +0000497 int ret;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000498
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000499 ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000500 confree(s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000501 return (ret);
502}
503
Ben Lindstrombba81212001-06-25 05:01:22 +0000504static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000505congreet(int s)
506{
Damien Millereccb9de2005-06-17 12:59:34 +1000507 int n = 0, remote_major = 0, remote_minor = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000508 char buf[256], *cp;
Damien Miller83c02ef2001-12-21 12:45:43 +1100509 char remote_version[sizeof buf];
Damien Millereccb9de2005-06-17 12:59:34 +1000510 size_t bufsiz;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000511 con *c = &fdcon[s];
512
Damien Miller4bbacb72005-11-05 15:12:28 +1100513 for (;;) {
514 memset(buf, '\0', sizeof(buf));
515 bufsiz = sizeof(buf);
516 cp = buf;
517 while (bufsiz-- &&
518 (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
519 if (*cp == '\r')
520 *cp = '\n';
521 cp++;
522 }
523 if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
524 break;
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000525 }
Ben Lindstrom6b28c352002-03-05 01:54:52 +0000526 if (n == 0) {
Damien Millerb253cc42005-05-26 12:23:44 +1000527 switch (errno) {
528 case EPIPE:
529 error("%s: Connection closed by remote host", c->c_name);
530 break;
531 case ECONNREFUSED:
532 break;
533 default:
534 error("read (%s): %s", c->c_name, strerror(errno));
535 break;
536 }
Ben Lindstrom6b28c352002-03-05 01:54:52 +0000537 conrecycle(s);
538 return;
539 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000540 if (*cp != '\n' && *cp != '\r') {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000541 error("%s: bad greeting", c->c_name);
542 confree(s);
543 return;
544 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000545 *cp = '\0';
Damien Miller83c02ef2001-12-21 12:45:43 +1100546 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
547 &remote_major, &remote_minor, remote_version) == 3)
548 compat_datafellows(remote_version);
549 else
550 datafellows = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000551 if (c->c_keytype != KT_RSA1) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000552 if (!ssh2_capable(remote_major, remote_minor)) {
553 debug("%s doesn't support ssh2", c->c_name);
554 confree(s);
555 return;
556 }
Damien Miller83c02ef2001-12-21 12:45:43 +1100557 } else if (remote_major != 1) {
558 debug("%s doesn't support ssh1", c->c_name);
559 confree(s);
560 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000561 }
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000562 fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000563 n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
564 c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
565 c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
Damien Millereccb9de2005-06-17 12:59:34 +1000566 if (n < 0 || (size_t)n >= sizeof(buf)) {
Damien Miller41bfc292005-05-26 12:07:32 +1000567 error("snprintf: buffer too small");
568 confree(s);
569 return;
570 }
Damien Millereccb9de2005-06-17 12:59:34 +1000571 if (atomicio(vwrite, s, buf, n) != (size_t)n) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000572 error("write (%s): %s", c->c_name, strerror(errno));
573 confree(s);
574 return;
575 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000576 if (c->c_keytype != KT_RSA1) {
577 keyprint(c, keygrab_ssh2(c));
578 confree(s);
579 return;
580 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000581 c->c_status = CS_SIZE;
582 contouch(s);
583}
584
Ben Lindstrombba81212001-06-25 05:01:22 +0000585static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000586conread(int s)
587{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000588 con *c = &fdcon[s];
Damien Millerb253cc42005-05-26 12:23:44 +1000589 size_t n;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000590
591 if (c->c_status == CS_CON) {
592 congreet(s);
593 return;
594 }
Darren Tuckerfe6649d2004-08-13 21:19:37 +1000595 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
Damien Millerb253cc42005-05-26 12:23:44 +1000596 if (n == 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000597 error("read (%s): %s", c->c_name, strerror(errno));
598 confree(s);
599 return;
600 }
601 c->c_off += n;
602
603 if (c->c_off == c->c_len)
604 switch (c->c_status) {
605 case CS_SIZE:
606 c->c_plen = htonl(c->c_plen);
607 c->c_len = c->c_plen + 8 - (c->c_plen & 7);
608 c->c_off = 0;
609 c->c_data = xmalloc(c->c_len);
610 c->c_status = CS_KEYS;
611 break;
612 case CS_KEYS:
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000613 keyprint(c, keygrab_ssh1(c));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000614 confree(s);
615 return;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000616 default:
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000617 fatal("conread: invalid status %d", c->c_status);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000618 break;
619 }
620
621 contouch(s);
622}
623
Ben Lindstrombba81212001-06-25 05:01:22 +0000624static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000625conloop(void)
626{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000627 struct timeval seltime, now;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000628 fd_set *r, *e;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000629 con *c;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000630 int i;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000631
632 gettimeofday(&now, NULL);
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000633 c = TAILQ_FIRST(&tq);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000634
Ben Lindstromd20b8552001-03-05 07:01:18 +0000635 if (c && (c->c_tv.tv_sec > now.tv_sec ||
636 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000637 seltime = c->c_tv;
638 seltime.tv_sec -= now.tv_sec;
639 seltime.tv_usec -= now.tv_usec;
Ben Lindstromc791beb2001-02-10 23:18:11 +0000640 if (seltime.tv_usec < 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000641 seltime.tv_usec += 1000000;
642 seltime.tv_sec--;
643 }
644 } else
645 seltime.tv_sec = seltime.tv_usec = 0;
646
Damien Miller07d86be2006-03-26 14:19:21 +1100647 r = xcalloc(read_wait_nfdset, sizeof(fd_mask));
648 e = xcalloc(read_wait_nfdset, sizeof(fd_mask));
649 memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask));
650 memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask));
Ben Lindstromc1e04212001-03-05 07:04:38 +0000651
652 while (select(maxfd, r, NULL, e, &seltime) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000653 (errno == EAGAIN || errno == EINTR))
654 ;
655
Ben Lindstromd20b8552001-03-05 07:01:18 +0000656 for (i = 0; i < maxfd; i++) {
Ben Lindstromc1e04212001-03-05 07:04:38 +0000657 if (FD_ISSET(i, e)) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000658 error("%s: exception!", fdcon[i].c_name);
659 confree(i);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000660 } else if (FD_ISSET(i, r))
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000661 conread(i);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000662 }
Ben Lindstromc1e04212001-03-05 07:04:38 +0000663 xfree(r);
664 xfree(e);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000665
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000666 c = TAILQ_FIRST(&tq);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000667 while (c && (c->c_tv.tv_sec < now.tv_sec ||
668 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000669 int s = c->c_fd;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000670
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000671 c = TAILQ_NEXT(c, c_link);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000672 conrecycle(s);
673 }
674}
675
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000676static void
677do_host(char *host)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000678{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000679 char *name = strnnsep(&host, " \t\n");
680 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000681
Ben Lindstromeaffb9d2001-12-06 16:28:19 +0000682 if (name == NULL)
683 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000684 for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
685 if (get_keytypes & j) {
686 while (ncon >= MAXCON)
687 conloop();
688 conalloc(name, *host ? host : name, j);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000689 }
690 }
691}
692
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000693void
694fatal(const char *fmt,...)
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000695{
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000696 va_list args;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000697
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000698 va_start(args, fmt);
699 do_log(SYSLOG_LEVEL_FATAL, fmt, args);
700 va_end(args);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000701 if (nonfatal_fatal)
702 longjmp(kexjmp, -1);
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000703 else
Darren Tucker3d326222003-09-22 21:11:20 +1000704 exit(255);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000705}
706
707static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000708usage(void)
709{
Damien Miller9a2fdbd2005-03-02 12:04:01 +1100710 fprintf(stderr, "usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n"
Ben Lindstrom965710f2002-07-07 22:17:22 +0000711 "\t\t [host | addrlist namelist] [...]\n",
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000712 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000713 exit(1);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000714}
715
716int
717main(int argc, char **argv)
718{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000719 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
720 int opt, fopt_count = 0;
721 char *tname;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000722
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000723 extern int optind;
724 extern char *optarg;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000725
Damien Miller59d3d5b2003-08-22 09:34:41 +1000726 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom4e088e42001-10-10 20:45:43 +0000727 init_rng();
728 seed_rng();
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000729 TAILQ_INIT(&tq);
730
Darren Tuckerce321d82005-10-03 18:11:24 +1000731 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
732 sanitise_stdfd();
733
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000734 if (argc <= 1)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000735 usage();
736
Damien Millerdb7b8172005-03-01 21:48:03 +1100737 while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000738 switch (opt) {
Damien Millerdb7b8172005-03-01 21:48:03 +1100739 case 'H':
740 hash_hosts = 1;
741 break;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000742 case 'p':
743 ssh_port = a2port(optarg);
744 if (ssh_port == 0) {
745 fprintf(stderr, "Bad port '%s'\n", optarg);
746 exit(1);
747 }
748 break;
749 case 'T':
Ben Lindstromedd098b2002-07-04 00:07:13 +0000750 timeout = convtime(optarg);
751 if (timeout == -1 || timeout == 0) {
752 fprintf(stderr, "Bad timeout '%s'\n", optarg);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000753 usage();
Ben Lindstromedd098b2002-07-04 00:07:13 +0000754 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000755 break;
756 case 'v':
757 if (!debug_flag) {
758 debug_flag = 1;
759 log_level = SYSLOG_LEVEL_DEBUG1;
760 }
761 else if (log_level < SYSLOG_LEVEL_DEBUG3)
762 log_level++;
763 else
764 fatal("Too high debugging level.");
765 break;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000766 case 'f':
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000767 if (strcmp(optarg, "-") == 0)
768 optarg = NULL;
769 argv[fopt_count++] = optarg;
770 break;
771 case 't':
772 get_keytypes = 0;
773 tname = strtok(optarg, ",");
774 while (tname) {
775 int type = key_type_from_name(tname);
776 switch (type) {
777 case KEY_RSA1:
778 get_keytypes |= KT_RSA1;
779 break;
780 case KEY_DSA:
781 get_keytypes |= KT_DSA;
782 break;
783 case KEY_RSA:
784 get_keytypes |= KT_RSA;
785 break;
786 case KEY_UNSPEC:
Ben Lindstrom28c603b2001-12-06 16:45:10 +0000787 fatal("unknown key type %s", tname);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000788 }
789 tname = strtok(NULL, ",");
790 }
791 break;
792 case '4':
793 IPv4or6 = AF_INET;
794 break;
795 case '6':
796 IPv4or6 = AF_INET6;
797 break;
798 case '?':
799 default:
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000800 usage();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000801 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000802 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000803 if (optind == argc && !fopt_count)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000804 usage();
805
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000806 log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000807
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000808 maxfd = fdlim_get(1);
809 if (maxfd < 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000810 fatal("%s: fdlim_get: bad value", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000811 if (maxfd > MAXMAXFD)
812 maxfd = MAXMAXFD;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000813 if (MAXCON <= 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000814 fatal("%s: not enough file descriptors", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000815 if (maxfd > fdlim_get(0))
816 fdlim_set(maxfd);
Damien Miller07d86be2006-03-26 14:19:21 +1100817 fdcon = xcalloc(maxfd, sizeof(con));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000818
Damien Miller07d86be2006-03-26 14:19:21 +1100819 read_wait_nfdset = howmany(maxfd, NFDBITS);
820 read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
Ben Lindstromc1e04212001-03-05 07:04:38 +0000821
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000822 if (fopt_count) {
823 Linebuf *lb;
824 char *line;
825 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000826
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000827 for (j = 0; j < fopt_count; j++) {
828 lb = Linebuf_alloc(argv[j], error);
Ben Lindstrom78bbd9e2001-09-12 17:10:40 +0000829 if (!lb)
830 continue;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000831 while ((line = Linebuf_getline(lb)) != NULL)
832 do_host(line);
833 Linebuf_free(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000834 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000835 }
836
837 while (optind < argc)
838 do_host(argv[optind++]);
839
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000840 while (ncon > 0)
841 conloop();
842
843 return (0);
844}