blob: 3d8cc7e4a4b870ebd772a8e514134b95e0092291 [file] [log] [blame]
Damien Millere7a1e5c2006-08-05 11:34:19 +10001/* $OpenBSD: ssh-keyscan.c,v 1.71 2006/07/26 13:57:17 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>
Damien Millerbe43ebf2006-07-24 13:51:51 +100022#include <setjmp.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100023#include <stdarg.h>
24#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100025#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100026#include <unistd.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000027
Ben Lindstromb6434ae2000-12-05 01:15:09 +000028#include "xmalloc.h"
29#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#include "ssh1.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000031#include "key.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000032#include "kex.h"
33#include "compat.h"
34#include "myproposal.h"
35#include "packet.h"
36#include "dispatch.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000037#include "buffer.h"
38#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "log.h"
Ben Lindstromd20b8552001-03-05 07:01:18 +000040#include "atomicio.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000041#include "misc.h"
Damien Millerdb7b8172005-03-01 21:48:03 +110042#include "hostfile.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000043
Ben Lindstrom325e70c2001-08-06 22:41:30 +000044/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
45 Default value is AF_UNSPEC means both IPv4 and IPv6. */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000046int IPv4or6 = AF_UNSPEC;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000047
Ben Lindstrom325e70c2001-08-06 22:41:30 +000048int ssh_port = SSH_DEFAULT_PORT;
Kevin Steves76e7d9b2001-09-20 20:30:09 +000049
50#define KT_RSA1 1
51#define KT_DSA 2
52#define KT_RSA 4
53
54int get_keytypes = KT_RSA1; /* Get only RSA1 keys by default */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000055
Damien Millerdb7b8172005-03-01 21:48:03 +110056int hash_hosts = 0; /* Hash hostname on output */
57
Ben Lindstromb6434ae2000-12-05 01:15:09 +000058#define MAXMAXFD 256
59
60/* The number of seconds after which to give up on a TCP connection */
61int timeout = 5;
62
63int maxfd;
Ben Lindstromd20b8552001-03-05 07:01:18 +000064#define MAXCON (maxfd - 10)
Ben Lindstromb6434ae2000-12-05 01:15:09 +000065
Kevin Stevesec84dc12000-12-13 17:45:15 +000066extern char *__progname;
Ben Lindstromc1e04212001-03-05 07:04:38 +000067fd_set *read_wait;
Damien Miller07d86be2006-03-26 14:19:21 +110068size_t read_wait_nfdset;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000069int ncon;
Ben Lindstrom325e70c2001-08-06 22:41:30 +000070int nonfatal_fatal = 0;
71jmp_buf kexjmp;
Ben Lindstrom520b55c2001-09-12 18:05:05 +000072Key *kexjmp_key;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000073
74/*
75 * Keep a connection structure for each file descriptor. The state
76 * associated with file descriptor n is held in fdcon[n].
77 */
78typedef struct Connection {
Ben Lindstrom46c16222000-12-22 01:43:59 +000079 u_char c_status; /* State of connection on this file desc. */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000080#define CS_UNUSED 0 /* File descriptor unused */
81#define CS_CON 1 /* Waiting to connect/read greeting */
82#define CS_SIZE 2 /* Waiting to read initial packet size */
83#define CS_KEYS 3 /* Waiting to read public key packet */
84 int c_fd; /* Quick lookup: c->c_fd == c - fdcon */
85 int c_plen; /* Packet length field for ssh packet */
86 int c_len; /* Total bytes which must be read. */
87 int c_off; /* Length of data read so far. */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000088 int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000089 char *c_namebase; /* Address to free for c_name and c_namelist */
90 char *c_name; /* Hostname of connection for errors */
91 char *c_namelist; /* Pointer to other possible addresses */
92 char *c_output_name; /* Hostname of connection for output */
93 char *c_data; /* Data read from this fd */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000094 Kex *c_kex; /* The key-exchange struct for ssh2 */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000095 struct timeval c_tv; /* Time at which connection gets aborted */
96 TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
97} con;
98
99TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */
100con *fdcon;
101
102/*
103 * This is just a wrapper around fgets() to make it usable.
104 */
105
106/* Stress-test. Increase this later. */
107#define LINEBUF_SIZE 16
108
109typedef struct {
110 char *buf;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000111 u_int size;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000112 int lineno;
113 const char *filename;
114 FILE *stream;
115 void (*errfun) (const char *,...);
116} Linebuf;
117
Ben Lindstrombba81212001-06-25 05:01:22 +0000118static Linebuf *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000119Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
120{
121 Linebuf *lb;
122
123 if (!(lb = malloc(sizeof(*lb)))) {
124 if (errfun)
Ben Lindstrom04f9af72002-07-04 00:03:56 +0000125 (*errfun) ("linebuf (%s): malloc failed\n",
126 filename ? filename : "(stdin)");
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000127 return (NULL);
128 }
129 if (filename) {
130 lb->filename = filename;
131 if (!(lb->stream = fopen(filename, "r"))) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000132 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000133 if (errfun)
134 (*errfun) ("%s: %s\n", filename, strerror(errno));
135 return (NULL);
136 }
137 } else {
138 lb->filename = "(stdin)";
139 lb->stream = stdin;
140 }
141
Damien Miller3bbaba62006-03-26 13:59:38 +1100142 if (!(lb->buf = malloc((lb->size = LINEBUF_SIZE)))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000143 if (errfun)
144 (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000145 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000146 return (NULL);
147 }
148 lb->errfun = errfun;
149 lb->lineno = 0;
150 return (lb);
151}
152
Ben Lindstrombba81212001-06-25 05:01:22 +0000153static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000154Linebuf_free(Linebuf * lb)
155{
156 fclose(lb->stream);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000157 xfree(lb->buf);
158 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000159}
160
Ben Lindstrombba81212001-06-25 05:01:22 +0000161#if 0
162static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000163Linebuf_restart(Linebuf * lb)
164{
165 clearerr(lb->stream);
166 rewind(lb->stream);
167 lb->lineno = 0;
168}
169
Ben Lindstrombba81212001-06-25 05:01:22 +0000170static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000171Linebuf_lineno(Linebuf * lb)
172{
173 return (lb->lineno);
174}
Ben Lindstrombba81212001-06-25 05:01:22 +0000175#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000176
Ben Lindstrombba81212001-06-25 05:01:22 +0000177static char *
Ben Lindstromc791beb2001-02-10 23:18:11 +0000178Linebuf_getline(Linebuf * lb)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000179{
Damien Millereccb9de2005-06-17 12:59:34 +1000180 size_t n = 0;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000181 void *p;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000182
183 lb->lineno++;
184 for (;;) {
185 /* Read a line */
186 if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
187 if (ferror(lb->stream) && lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000188 (*lb->errfun)("%s: %s\n", lb->filename,
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000189 strerror(errno));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000190 return (NULL);
191 }
192 n = strlen(lb->buf);
193
194 /* Return it or an error if it fits */
195 if (n > 0 && lb->buf[n - 1] == '\n') {
196 lb->buf[n - 1] = '\0';
197 return (lb->buf);
198 }
199 if (n != lb->size - 1) {
200 if (lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000201 (*lb->errfun)("%s: skipping incomplete last line\n",
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000202 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000203 return (NULL);
204 }
205 /* Double the buffer if we need more space */
Ben Lindstrom965710f2002-07-07 22:17:22 +0000206 lb->size *= 2;
207 if ((p = realloc(lb->buf, lb->size)) == NULL) {
208 lb->size /= 2;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000209 if (lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000210 (*lb->errfun)("linebuf (%s): realloc failed\n",
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000211 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000212 return (NULL);
213 }
Ben Lindstrom965710f2002-07-07 22:17:22 +0000214 lb->buf = p;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000215 }
216}
217
Ben Lindstrombba81212001-06-25 05:01:22 +0000218static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000219fdlim_get(int hard)
220{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000221#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000222 struct rlimit rlfd;
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000223
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000224 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
225 return (-1);
226 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
Damien Millere00074a2003-11-24 13:07:45 +1100227 return SSH_SYSFDMAX;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000228 else
229 return hard ? rlfd.rlim_max : rlfd.rlim_cur;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000230#else
Damien Millere00074a2003-11-24 13:07:45 +1100231 return SSH_SYSFDMAX;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000232#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000233}
234
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000236fdlim_set(int lim)
237{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000238#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000239 struct rlimit rlfd;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000240#endif
Ben Lindstrom5c98db52002-07-07 22:25:29 +0000241
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000242 if (lim <= 0)
243 return (-1);
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000244#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000245 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
246 return (-1);
247 rlfd.rlim_cur = lim;
248 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
249 return (-1);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000250#elif defined (HAVE_SETDTABLESIZE)
Kevin Steves28a7f262001-02-05 15:43:59 +0000251 setdtablesize(lim);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000252#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000253 return (0);
254}
255
256/*
257 * This is an strsep function that returns a null field for adjacent
258 * separators. This is the same as the 4.4BSD strsep, but different from the
259 * one in the GNU libc.
260 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000261static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000262xstrsep(char **str, const char *delim)
263{
264 char *s, *e;
265
266 if (!**str)
267 return (NULL);
268
269 s = *str;
270 e = s + strcspn(s, delim);
271
272 if (*e != '\0')
273 *e++ = '\0';
274 *str = e;
275
276 return (s);
277}
278
279/*
280 * Get the next non-null token (like GNU strsep). Strsep() will return a
281 * null token for two adjacent separators, so we may have to loop.
282 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000283static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000284strnnsep(char **stringp, char *delim)
285{
286 char *tok;
287
288 do {
289 tok = xstrsep(stringp, delim);
290 } while (tok && *tok == '\0');
291 return (tok);
292}
293
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000294static Key *
295keygrab_ssh1(con *c)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000296{
297 static Key *rsa;
298 static Buffer msg;
299
300 if (rsa == NULL) {
301 buffer_init(&msg);
302 rsa = key_new(KEY_RSA1);
303 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000304 buffer_append(&msg, c->c_data, c->c_plen);
305 buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000306 if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000307 error("%s: invalid packet type", c->c_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000308 buffer_clear(&msg);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000309 return NULL;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000310 }
311 buffer_consume(&msg, 8); /* cookie */
312
313 /* server key */
314 (void) buffer_get_int(&msg);
315 buffer_get_bignum(&msg, rsa->rsa->e);
316 buffer_get_bignum(&msg, rsa->rsa->n);
317
318 /* host key */
319 (void) buffer_get_int(&msg);
320 buffer_get_bignum(&msg, rsa->rsa->e);
321 buffer_get_bignum(&msg, rsa->rsa->n);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000322
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000323 buffer_clear(&msg);
324
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000325 return (rsa);
326}
327
328static int
329hostjump(Key *hostkey)
330{
Ben Lindstrom520b55c2001-09-12 18:05:05 +0000331 kexjmp_key = hostkey;
332 longjmp(kexjmp, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000333}
334
335static int
336ssh2_capable(int remote_major, int remote_minor)
337{
338 switch (remote_major) {
339 case 1:
340 if (remote_minor == 99)
341 return 1;
342 break;
343 case 2:
344 return 1;
345 default:
346 break;
347 }
348 return 0;
349}
350
351static Key *
352keygrab_ssh2(con *c)
353{
354 int j;
355
356 packet_set_connection(c->c_fd, c->c_fd);
357 enable_compat20();
358 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
359 "ssh-dss": "ssh-rsa";
360 c->c_kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100361 c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000362 c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100363 c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100364 c->c_kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000365 c->c_kex->verify_host_key = hostjump;
366
367 if (!(j = setjmp(kexjmp))) {
368 nonfatal_fatal = 1;
369 dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
370 fprintf(stderr, "Impossible! dispatch_run() returned!\n");
371 exit(1);
372 }
373 nonfatal_fatal = 0;
374 xfree(c->c_kex);
375 c->c_kex = NULL;
376 packet_close();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000377
Ben Lindstrom520b55c2001-09-12 18:05:05 +0000378 return j < 0? NULL : kexjmp_key;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000379}
380
381static void
382keyprint(con *c, Key *key)
383{
Damien Millerdb7b8172005-03-01 21:48:03 +1100384 char *host = c->c_output_name ? c->c_output_name : c->c_name;
385
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000386 if (!key)
387 return;
Damien Millerdb7b8172005-03-01 21:48:03 +1100388 if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL)
389 fatal("host_hash failed");
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000390
Damien Millerdb7b8172005-03-01 21:48:03 +1100391 fprintf(stdout, "%s ", host);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000392 key_write(key, stdout);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000393 fputs("\n", stdout);
394}
395
Ben Lindstrombba81212001-06-25 05:01:22 +0000396static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000397tcpconnect(char *host)
398{
399 struct addrinfo hints, *ai, *aitop;
400 char strport[NI_MAXSERV];
401 int gaierr, s = -1;
402
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000403 snprintf(strport, sizeof strport, "%d", ssh_port);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000404 memset(&hints, 0, sizeof(hints));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000405 hints.ai_family = IPv4or6;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000406 hints.ai_socktype = SOCK_STREAM;
407 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
408 fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
409 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller2372ace2003-05-14 13:42:23 +1000410 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000411 if (s < 0) {
412 error("socket: %s", strerror(errno));
413 continue;
414 }
Damien Miller232711f2004-06-15 10:35:30 +1000415 if (set_nonblock(s) == -1)
416 fatal("%s: set_nonblock(%d)", __func__, s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000417 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
418 errno != EINPROGRESS)
419 error("connect (`%s'): %s", host, strerror(errno));
420 else
421 break;
422 close(s);
423 s = -1;
424 }
425 freeaddrinfo(aitop);
426 return s;
427}
428
Ben Lindstrombba81212001-06-25 05:01:22 +0000429static int
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000430conalloc(char *iname, char *oname, int keytype)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000431{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000432 char *namebase, *name, *namelist;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000433 int s;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000434
435 namebase = namelist = xstrdup(iname);
436
437 do {
438 name = xstrsep(&namelist, ",");
439 if (!name) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000440 xfree(namebase);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000441 return (-1);
442 }
443 } while ((s = tcpconnect(name)) < 0);
444
445 if (s >= maxfd)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000446 fatal("conalloc: fdno %d too high", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000447 if (fdcon[s].c_status)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000448 fatal("conalloc: attempt to reuse fdno %d", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000449
450 fdcon[s].c_fd = s;
451 fdcon[s].c_status = CS_CON;
452 fdcon[s].c_namebase = namebase;
453 fdcon[s].c_name = name;
454 fdcon[s].c_namelist = namelist;
455 fdcon[s].c_output_name = xstrdup(oname);
456 fdcon[s].c_data = (char *) &fdcon[s].c_plen;
457 fdcon[s].c_len = 4;
458 fdcon[s].c_off = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000459 fdcon[s].c_keytype = keytype;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000460 gettimeofday(&fdcon[s].c_tv, NULL);
461 fdcon[s].c_tv.tv_sec += timeout;
462 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000463 FD_SET(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000464 ncon++;
465 return (s);
466}
467
Ben Lindstrombba81212001-06-25 05:01:22 +0000468static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000469confree(int s)
470{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000471 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000472 fatal("confree: attempt to free bad fdno %d", s);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000473 close(s);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000474 xfree(fdcon[s].c_namebase);
475 xfree(fdcon[s].c_output_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000476 if (fdcon[s].c_status == CS_KEYS)
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000477 xfree(fdcon[s].c_data);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000478 fdcon[s].c_status = CS_UNUSED;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000479 fdcon[s].c_keytype = 0;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000480 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000481 FD_CLR(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000482 ncon--;
483}
484
Ben Lindstrombba81212001-06-25 05:01:22 +0000485static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000486contouch(int s)
487{
488 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
489 gettimeofday(&fdcon[s].c_tv, NULL);
490 fdcon[s].c_tv.tv_sec += timeout;
491 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
492}
493
Ben Lindstrombba81212001-06-25 05:01:22 +0000494static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000495conrecycle(int s)
496{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000497 con *c = &fdcon[s];
Ben Lindstrom965710f2002-07-07 22:17:22 +0000498 int ret;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000499
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000500 ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000501 confree(s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000502 return (ret);
503}
504
Ben Lindstrombba81212001-06-25 05:01:22 +0000505static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000506congreet(int s)
507{
Damien Millereccb9de2005-06-17 12:59:34 +1000508 int n = 0, remote_major = 0, remote_minor = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000509 char buf[256], *cp;
Damien Miller83c02ef2001-12-21 12:45:43 +1100510 char remote_version[sizeof buf];
Damien Millereccb9de2005-06-17 12:59:34 +1000511 size_t bufsiz;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000512 con *c = &fdcon[s];
513
Damien Miller4bbacb72005-11-05 15:12:28 +1100514 for (;;) {
515 memset(buf, '\0', sizeof(buf));
516 bufsiz = sizeof(buf);
517 cp = buf;
518 while (bufsiz-- &&
519 (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
520 if (*cp == '\r')
521 *cp = '\n';
522 cp++;
523 }
524 if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
525 break;
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000526 }
Ben Lindstrom6b28c352002-03-05 01:54:52 +0000527 if (n == 0) {
Damien Millerb253cc42005-05-26 12:23:44 +1000528 switch (errno) {
529 case EPIPE:
530 error("%s: Connection closed by remote host", c->c_name);
531 break;
532 case ECONNREFUSED:
533 break;
534 default:
535 error("read (%s): %s", c->c_name, strerror(errno));
536 break;
537 }
Ben Lindstrom6b28c352002-03-05 01:54:52 +0000538 conrecycle(s);
539 return;
540 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000541 if (*cp != '\n' && *cp != '\r') {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000542 error("%s: bad greeting", c->c_name);
543 confree(s);
544 return;
545 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000546 *cp = '\0';
Damien Miller83c02ef2001-12-21 12:45:43 +1100547 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
548 &remote_major, &remote_minor, remote_version) == 3)
549 compat_datafellows(remote_version);
550 else
551 datafellows = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000552 if (c->c_keytype != KT_RSA1) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000553 if (!ssh2_capable(remote_major, remote_minor)) {
554 debug("%s doesn't support ssh2", c->c_name);
555 confree(s);
556 return;
557 }
Damien Miller83c02ef2001-12-21 12:45:43 +1100558 } else if (remote_major != 1) {
559 debug("%s doesn't support ssh1", c->c_name);
560 confree(s);
561 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000562 }
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000563 fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000564 n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
565 c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
566 c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
Damien Millereccb9de2005-06-17 12:59:34 +1000567 if (n < 0 || (size_t)n >= sizeof(buf)) {
Damien Miller41bfc292005-05-26 12:07:32 +1000568 error("snprintf: buffer too small");
569 confree(s);
570 return;
571 }
Damien Millereccb9de2005-06-17 12:59:34 +1000572 if (atomicio(vwrite, s, buf, n) != (size_t)n) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000573 error("write (%s): %s", c->c_name, strerror(errno));
574 confree(s);
575 return;
576 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000577 if (c->c_keytype != KT_RSA1) {
578 keyprint(c, keygrab_ssh2(c));
579 confree(s);
580 return;
581 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000582 c->c_status = CS_SIZE;
583 contouch(s);
584}
585
Ben Lindstrombba81212001-06-25 05:01:22 +0000586static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000587conread(int s)
588{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000589 con *c = &fdcon[s];
Damien Millerb253cc42005-05-26 12:23:44 +1000590 size_t n;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000591
592 if (c->c_status == CS_CON) {
593 congreet(s);
594 return;
595 }
Darren Tuckerfe6649d2004-08-13 21:19:37 +1000596 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
Damien Millerb253cc42005-05-26 12:23:44 +1000597 if (n == 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000598 error("read (%s): %s", c->c_name, strerror(errno));
599 confree(s);
600 return;
601 }
602 c->c_off += n;
603
604 if (c->c_off == c->c_len)
605 switch (c->c_status) {
606 case CS_SIZE:
607 c->c_plen = htonl(c->c_plen);
608 c->c_len = c->c_plen + 8 - (c->c_plen & 7);
609 c->c_off = 0;
610 c->c_data = xmalloc(c->c_len);
611 c->c_status = CS_KEYS;
612 break;
613 case CS_KEYS:
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000614 keyprint(c, keygrab_ssh1(c));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000615 confree(s);
616 return;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000617 default:
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000618 fatal("conread: invalid status %d", c->c_status);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000619 break;
620 }
621
622 contouch(s);
623}
624
Ben Lindstrombba81212001-06-25 05:01:22 +0000625static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000626conloop(void)
627{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000628 struct timeval seltime, now;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000629 fd_set *r, *e;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000630 con *c;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000631 int i;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000632
633 gettimeofday(&now, NULL);
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000634 c = TAILQ_FIRST(&tq);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000635
Ben Lindstromd20b8552001-03-05 07:01:18 +0000636 if (c && (c->c_tv.tv_sec > now.tv_sec ||
637 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000638 seltime = c->c_tv;
639 seltime.tv_sec -= now.tv_sec;
640 seltime.tv_usec -= now.tv_usec;
Ben Lindstromc791beb2001-02-10 23:18:11 +0000641 if (seltime.tv_usec < 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000642 seltime.tv_usec += 1000000;
643 seltime.tv_sec--;
644 }
645 } else
646 seltime.tv_sec = seltime.tv_usec = 0;
647
Damien Miller07d86be2006-03-26 14:19:21 +1100648 r = xcalloc(read_wait_nfdset, sizeof(fd_mask));
649 e = xcalloc(read_wait_nfdset, sizeof(fd_mask));
650 memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask));
651 memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask));
Ben Lindstromc1e04212001-03-05 07:04:38 +0000652
653 while (select(maxfd, r, NULL, e, &seltime) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000654 (errno == EAGAIN || errno == EINTR))
655 ;
656
Ben Lindstromd20b8552001-03-05 07:01:18 +0000657 for (i = 0; i < maxfd; i++) {
Ben Lindstromc1e04212001-03-05 07:04:38 +0000658 if (FD_ISSET(i, e)) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000659 error("%s: exception!", fdcon[i].c_name);
660 confree(i);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000661 } else if (FD_ISSET(i, r))
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000662 conread(i);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000663 }
Ben Lindstromc1e04212001-03-05 07:04:38 +0000664 xfree(r);
665 xfree(e);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000666
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000667 c = TAILQ_FIRST(&tq);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000668 while (c && (c->c_tv.tv_sec < now.tv_sec ||
669 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000670 int s = c->c_fd;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000671
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000672 c = TAILQ_NEXT(c, c_link);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000673 conrecycle(s);
674 }
675}
676
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000677static void
678do_host(char *host)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000679{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000680 char *name = strnnsep(&host, " \t\n");
681 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000682
Ben Lindstromeaffb9d2001-12-06 16:28:19 +0000683 if (name == NULL)
684 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000685 for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
686 if (get_keytypes & j) {
687 while (ncon >= MAXCON)
688 conloop();
689 conalloc(name, *host ? host : name, j);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000690 }
691 }
692}
693
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000694void
695fatal(const char *fmt,...)
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000696{
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000697 va_list args;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000698
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000699 va_start(args, fmt);
700 do_log(SYSLOG_LEVEL_FATAL, fmt, args);
701 va_end(args);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000702 if (nonfatal_fatal)
703 longjmp(kexjmp, -1);
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000704 else
Darren Tucker3d326222003-09-22 21:11:20 +1000705 exit(255);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000706}
707
708static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000709usage(void)
710{
Damien Miller9a2fdbd2005-03-02 12:04:01 +1100711 fprintf(stderr, "usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n"
Ben Lindstrom965710f2002-07-07 22:17:22 +0000712 "\t\t [host | addrlist namelist] [...]\n",
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000713 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000714 exit(1);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000715}
716
717int
718main(int argc, char **argv)
719{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000720 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
721 int opt, fopt_count = 0;
722 char *tname;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000723
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000724 extern int optind;
725 extern char *optarg;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000726
Damien Miller59d3d5b2003-08-22 09:34:41 +1000727 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom4e088e42001-10-10 20:45:43 +0000728 init_rng();
729 seed_rng();
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000730 TAILQ_INIT(&tq);
731
Darren Tuckerce321d82005-10-03 18:11:24 +1000732 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
733 sanitise_stdfd();
734
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000735 if (argc <= 1)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000736 usage();
737
Damien Millerdb7b8172005-03-01 21:48:03 +1100738 while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000739 switch (opt) {
Damien Millerdb7b8172005-03-01 21:48:03 +1100740 case 'H':
741 hash_hosts = 1;
742 break;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000743 case 'p':
744 ssh_port = a2port(optarg);
745 if (ssh_port == 0) {
746 fprintf(stderr, "Bad port '%s'\n", optarg);
747 exit(1);
748 }
749 break;
750 case 'T':
Ben Lindstromedd098b2002-07-04 00:07:13 +0000751 timeout = convtime(optarg);
752 if (timeout == -1 || timeout == 0) {
753 fprintf(stderr, "Bad timeout '%s'\n", optarg);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000754 usage();
Ben Lindstromedd098b2002-07-04 00:07:13 +0000755 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000756 break;
757 case 'v':
758 if (!debug_flag) {
759 debug_flag = 1;
760 log_level = SYSLOG_LEVEL_DEBUG1;
761 }
762 else if (log_level < SYSLOG_LEVEL_DEBUG3)
763 log_level++;
764 else
765 fatal("Too high debugging level.");
766 break;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000767 case 'f':
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000768 if (strcmp(optarg, "-") == 0)
769 optarg = NULL;
770 argv[fopt_count++] = optarg;
771 break;
772 case 't':
773 get_keytypes = 0;
774 tname = strtok(optarg, ",");
775 while (tname) {
776 int type = key_type_from_name(tname);
777 switch (type) {
778 case KEY_RSA1:
779 get_keytypes |= KT_RSA1;
780 break;
781 case KEY_DSA:
782 get_keytypes |= KT_DSA;
783 break;
784 case KEY_RSA:
785 get_keytypes |= KT_RSA;
786 break;
787 case KEY_UNSPEC:
Ben Lindstrom28c603b2001-12-06 16:45:10 +0000788 fatal("unknown key type %s", tname);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000789 }
790 tname = strtok(NULL, ",");
791 }
792 break;
793 case '4':
794 IPv4or6 = AF_INET;
795 break;
796 case '6':
797 IPv4or6 = AF_INET6;
798 break;
799 case '?':
800 default:
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000801 usage();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000802 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000803 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000804 if (optind == argc && !fopt_count)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000805 usage();
806
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000807 log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000808
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000809 maxfd = fdlim_get(1);
810 if (maxfd < 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000811 fatal("%s: fdlim_get: bad value", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000812 if (maxfd > MAXMAXFD)
813 maxfd = MAXMAXFD;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000814 if (MAXCON <= 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000815 fatal("%s: not enough file descriptors", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000816 if (maxfd > fdlim_get(0))
817 fdlim_set(maxfd);
Damien Miller07d86be2006-03-26 14:19:21 +1100818 fdcon = xcalloc(maxfd, sizeof(con));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000819
Damien Miller07d86be2006-03-26 14:19:21 +1100820 read_wait_nfdset = howmany(maxfd, NFDBITS);
821 read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
Ben Lindstromc1e04212001-03-05 07:04:38 +0000822
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000823 if (fopt_count) {
824 Linebuf *lb;
825 char *line;
826 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000827
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000828 for (j = 0; j < fopt_count; j++) {
829 lb = Linebuf_alloc(argv[j], error);
Ben Lindstrom78bbd9e2001-09-12 17:10:40 +0000830 if (!lb)
831 continue;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000832 while ((line = Linebuf_getline(lb)) != NULL)
833 do_host(line);
834 Linebuf_free(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000835 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000836 }
837
838 while (optind < argc)
839 do_host(argv[optind++]);
840
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000841 while (ncon > 0)
842 conloop();
843
844 return (0);
845}