blob: 6b706f0af661c7da8f90b81ff6fdb22d93e2dc9d [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: ssh-keyscan.c,v 1.69 2006/07/22 20:48:23 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 Millere3476ed2006-07-24 14:13:33 +100014
15#include <openssl/bn.h>
16
Damien Millerb8fe89c2006-07-24 14:51:00 +100017#include <netdb.h>
Darren Tuckerdeecec92006-07-12 22:44:34 +100018#include <errno.h>
Darren Tucker5d196262006-07-12 22:15:16 +100019#include <stdarg.h>
Damien Millerbe43ebf2006-07-24 13:51:51 +100020#include <setjmp.h>
Damien Millere3476ed2006-07-24 14:13:33 +100021#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100022#include <unistd.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000023
Ben Lindstromb6434ae2000-12-05 01:15:09 +000024#include "xmalloc.h"
25#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "ssh1.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000027#include "key.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000028#include "kex.h"
29#include "compat.h"
30#include "myproposal.h"
31#include "packet.h"
32#include "dispatch.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000033#include "buffer.h"
34#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "log.h"
Ben Lindstromd20b8552001-03-05 07:01:18 +000036#include "atomicio.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000037#include "misc.h"
Damien Millerdb7b8172005-03-01 21:48:03 +110038#include "hostfile.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000039
Ben Lindstrom325e70c2001-08-06 22:41:30 +000040/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
41 Default value is AF_UNSPEC means both IPv4 and IPv6. */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000042int IPv4or6 = AF_UNSPEC;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000043
Ben Lindstrom325e70c2001-08-06 22:41:30 +000044int ssh_port = SSH_DEFAULT_PORT;
Kevin Steves76e7d9b2001-09-20 20:30:09 +000045
46#define KT_RSA1 1
47#define KT_DSA 2
48#define KT_RSA 4
49
50int get_keytypes = KT_RSA1; /* Get only RSA1 keys by default */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000051
Damien Millerdb7b8172005-03-01 21:48:03 +110052int hash_hosts = 0; /* Hash hostname on output */
53
Ben Lindstromb6434ae2000-12-05 01:15:09 +000054#define MAXMAXFD 256
55
56/* The number of seconds after which to give up on a TCP connection */
57int timeout = 5;
58
59int maxfd;
Ben Lindstromd20b8552001-03-05 07:01:18 +000060#define MAXCON (maxfd - 10)
Ben Lindstromb6434ae2000-12-05 01:15:09 +000061
Kevin Stevesec84dc12000-12-13 17:45:15 +000062extern char *__progname;
Ben Lindstromc1e04212001-03-05 07:04:38 +000063fd_set *read_wait;
Damien Miller07d86be2006-03-26 14:19:21 +110064size_t read_wait_nfdset;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000065int ncon;
Ben Lindstrom325e70c2001-08-06 22:41:30 +000066int nonfatal_fatal = 0;
67jmp_buf kexjmp;
Ben Lindstrom520b55c2001-09-12 18:05:05 +000068Key *kexjmp_key;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000069
70/*
71 * Keep a connection structure for each file descriptor. The state
72 * associated with file descriptor n is held in fdcon[n].
73 */
74typedef struct Connection {
Ben Lindstrom46c16222000-12-22 01:43:59 +000075 u_char c_status; /* State of connection on this file desc. */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000076#define CS_UNUSED 0 /* File descriptor unused */
77#define CS_CON 1 /* Waiting to connect/read greeting */
78#define CS_SIZE 2 /* Waiting to read initial packet size */
79#define CS_KEYS 3 /* Waiting to read public key packet */
80 int c_fd; /* Quick lookup: c->c_fd == c - fdcon */
81 int c_plen; /* Packet length field for ssh packet */
82 int c_len; /* Total bytes which must be read. */
83 int c_off; /* Length of data read so far. */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000084 int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000085 char *c_namebase; /* Address to free for c_name and c_namelist */
86 char *c_name; /* Hostname of connection for errors */
87 char *c_namelist; /* Pointer to other possible addresses */
88 char *c_output_name; /* Hostname of connection for output */
89 char *c_data; /* Data read from this fd */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000090 Kex *c_kex; /* The key-exchange struct for ssh2 */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000091 struct timeval c_tv; /* Time at which connection gets aborted */
92 TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
93} con;
94
95TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */
96con *fdcon;
97
98/*
99 * This is just a wrapper around fgets() to make it usable.
100 */
101
102/* Stress-test. Increase this later. */
103#define LINEBUF_SIZE 16
104
105typedef struct {
106 char *buf;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000107 u_int size;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000108 int lineno;
109 const char *filename;
110 FILE *stream;
111 void (*errfun) (const char *,...);
112} Linebuf;
113
Ben Lindstrombba81212001-06-25 05:01:22 +0000114static Linebuf *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000115Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
116{
117 Linebuf *lb;
118
119 if (!(lb = malloc(sizeof(*lb)))) {
120 if (errfun)
Ben Lindstrom04f9af72002-07-04 00:03:56 +0000121 (*errfun) ("linebuf (%s): malloc failed\n",
122 filename ? filename : "(stdin)");
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000123 return (NULL);
124 }
125 if (filename) {
126 lb->filename = filename;
127 if (!(lb->stream = fopen(filename, "r"))) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000128 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000129 if (errfun)
130 (*errfun) ("%s: %s\n", filename, strerror(errno));
131 return (NULL);
132 }
133 } else {
134 lb->filename = "(stdin)";
135 lb->stream = stdin;
136 }
137
Damien Miller3bbaba62006-03-26 13:59:38 +1100138 if (!(lb->buf = malloc((lb->size = LINEBUF_SIZE)))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000139 if (errfun)
140 (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000141 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000142 return (NULL);
143 }
144 lb->errfun = errfun;
145 lb->lineno = 0;
146 return (lb);
147}
148
Ben Lindstrombba81212001-06-25 05:01:22 +0000149static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000150Linebuf_free(Linebuf * lb)
151{
152 fclose(lb->stream);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000153 xfree(lb->buf);
154 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000155}
156
Ben Lindstrombba81212001-06-25 05:01:22 +0000157#if 0
158static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000159Linebuf_restart(Linebuf * lb)
160{
161 clearerr(lb->stream);
162 rewind(lb->stream);
163 lb->lineno = 0;
164}
165
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000167Linebuf_lineno(Linebuf * lb)
168{
169 return (lb->lineno);
170}
Ben Lindstrombba81212001-06-25 05:01:22 +0000171#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000172
Ben Lindstrombba81212001-06-25 05:01:22 +0000173static char *
Ben Lindstromc791beb2001-02-10 23:18:11 +0000174Linebuf_getline(Linebuf * lb)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000175{
Damien Millereccb9de2005-06-17 12:59:34 +1000176 size_t n = 0;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000177 void *p;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000178
179 lb->lineno++;
180 for (;;) {
181 /* Read a line */
182 if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
183 if (ferror(lb->stream) && lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000184 (*lb->errfun)("%s: %s\n", lb->filename,
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000185 strerror(errno));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000186 return (NULL);
187 }
188 n = strlen(lb->buf);
189
190 /* Return it or an error if it fits */
191 if (n > 0 && lb->buf[n - 1] == '\n') {
192 lb->buf[n - 1] = '\0';
193 return (lb->buf);
194 }
195 if (n != lb->size - 1) {
196 if (lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000197 (*lb->errfun)("%s: skipping incomplete last line\n",
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000198 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000199 return (NULL);
200 }
201 /* Double the buffer if we need more space */
Ben Lindstrom965710f2002-07-07 22:17:22 +0000202 lb->size *= 2;
203 if ((p = realloc(lb->buf, lb->size)) == NULL) {
204 lb->size /= 2;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000205 if (lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000206 (*lb->errfun)("linebuf (%s): realloc failed\n",
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000207 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000208 return (NULL);
209 }
Ben Lindstrom965710f2002-07-07 22:17:22 +0000210 lb->buf = p;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000211 }
212}
213
Ben Lindstrombba81212001-06-25 05:01:22 +0000214static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000215fdlim_get(int hard)
216{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000217#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000218 struct rlimit rlfd;
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000219
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000220 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
221 return (-1);
222 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
Damien Millere00074a2003-11-24 13:07:45 +1100223 return SSH_SYSFDMAX;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000224 else
225 return hard ? rlfd.rlim_max : rlfd.rlim_cur;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000226#else
Damien Millere00074a2003-11-24 13:07:45 +1100227 return SSH_SYSFDMAX;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000228#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000229}
230
Ben Lindstrombba81212001-06-25 05:01:22 +0000231static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000232fdlim_set(int lim)
233{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000234#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000235 struct rlimit rlfd;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000236#endif
Ben Lindstrom5c98db52002-07-07 22:25:29 +0000237
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000238 if (lim <= 0)
239 return (-1);
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000240#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000241 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
242 return (-1);
243 rlfd.rlim_cur = lim;
244 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
245 return (-1);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000246#elif defined (HAVE_SETDTABLESIZE)
Kevin Steves28a7f262001-02-05 15:43:59 +0000247 setdtablesize(lim);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000248#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000249 return (0);
250}
251
252/*
253 * This is an strsep function that returns a null field for adjacent
254 * separators. This is the same as the 4.4BSD strsep, but different from the
255 * one in the GNU libc.
256 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000257static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000258xstrsep(char **str, const char *delim)
259{
260 char *s, *e;
261
262 if (!**str)
263 return (NULL);
264
265 s = *str;
266 e = s + strcspn(s, delim);
267
268 if (*e != '\0')
269 *e++ = '\0';
270 *str = e;
271
272 return (s);
273}
274
275/*
276 * Get the next non-null token (like GNU strsep). Strsep() will return a
277 * null token for two adjacent separators, so we may have to loop.
278 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000279static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000280strnnsep(char **stringp, char *delim)
281{
282 char *tok;
283
284 do {
285 tok = xstrsep(stringp, delim);
286 } while (tok && *tok == '\0');
287 return (tok);
288}
289
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000290static Key *
291keygrab_ssh1(con *c)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000292{
293 static Key *rsa;
294 static Buffer msg;
295
296 if (rsa == NULL) {
297 buffer_init(&msg);
298 rsa = key_new(KEY_RSA1);
299 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000300 buffer_append(&msg, c->c_data, c->c_plen);
301 buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000302 if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000303 error("%s: invalid packet type", c->c_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000304 buffer_clear(&msg);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000305 return NULL;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000306 }
307 buffer_consume(&msg, 8); /* cookie */
308
309 /* server key */
310 (void) buffer_get_int(&msg);
311 buffer_get_bignum(&msg, rsa->rsa->e);
312 buffer_get_bignum(&msg, rsa->rsa->n);
313
314 /* host key */
315 (void) buffer_get_int(&msg);
316 buffer_get_bignum(&msg, rsa->rsa->e);
317 buffer_get_bignum(&msg, rsa->rsa->n);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000318
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000319 buffer_clear(&msg);
320
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000321 return (rsa);
322}
323
324static int
325hostjump(Key *hostkey)
326{
Ben Lindstrom520b55c2001-09-12 18:05:05 +0000327 kexjmp_key = hostkey;
328 longjmp(kexjmp, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000329}
330
331static int
332ssh2_capable(int remote_major, int remote_minor)
333{
334 switch (remote_major) {
335 case 1:
336 if (remote_minor == 99)
337 return 1;
338 break;
339 case 2:
340 return 1;
341 default:
342 break;
343 }
344 return 0;
345}
346
347static Key *
348keygrab_ssh2(con *c)
349{
350 int j;
351
352 packet_set_connection(c->c_fd, c->c_fd);
353 enable_compat20();
354 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
355 "ssh-dss": "ssh-rsa";
356 c->c_kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100357 c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000358 c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100359 c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100360 c->c_kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000361 c->c_kex->verify_host_key = hostjump;
362
363 if (!(j = setjmp(kexjmp))) {
364 nonfatal_fatal = 1;
365 dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
366 fprintf(stderr, "Impossible! dispatch_run() returned!\n");
367 exit(1);
368 }
369 nonfatal_fatal = 0;
370 xfree(c->c_kex);
371 c->c_kex = NULL;
372 packet_close();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000373
Ben Lindstrom520b55c2001-09-12 18:05:05 +0000374 return j < 0? NULL : kexjmp_key;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000375}
376
377static void
378keyprint(con *c, Key *key)
379{
Damien Millerdb7b8172005-03-01 21:48:03 +1100380 char *host = c->c_output_name ? c->c_output_name : c->c_name;
381
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000382 if (!key)
383 return;
Damien Millerdb7b8172005-03-01 21:48:03 +1100384 if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL)
385 fatal("host_hash failed");
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000386
Damien Millerdb7b8172005-03-01 21:48:03 +1100387 fprintf(stdout, "%s ", host);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000388 key_write(key, stdout);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000389 fputs("\n", stdout);
390}
391
Ben Lindstrombba81212001-06-25 05:01:22 +0000392static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000393tcpconnect(char *host)
394{
395 struct addrinfo hints, *ai, *aitop;
396 char strport[NI_MAXSERV];
397 int gaierr, s = -1;
398
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000399 snprintf(strport, sizeof strport, "%d", ssh_port);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000400 memset(&hints, 0, sizeof(hints));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000401 hints.ai_family = IPv4or6;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000402 hints.ai_socktype = SOCK_STREAM;
403 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
404 fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
405 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller2372ace2003-05-14 13:42:23 +1000406 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000407 if (s < 0) {
408 error("socket: %s", strerror(errno));
409 continue;
410 }
Damien Miller232711f2004-06-15 10:35:30 +1000411 if (set_nonblock(s) == -1)
412 fatal("%s: set_nonblock(%d)", __func__, s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000413 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
414 errno != EINPROGRESS)
415 error("connect (`%s'): %s", host, strerror(errno));
416 else
417 break;
418 close(s);
419 s = -1;
420 }
421 freeaddrinfo(aitop);
422 return s;
423}
424
Ben Lindstrombba81212001-06-25 05:01:22 +0000425static int
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000426conalloc(char *iname, char *oname, int keytype)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000427{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000428 char *namebase, *name, *namelist;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000429 int s;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000430
431 namebase = namelist = xstrdup(iname);
432
433 do {
434 name = xstrsep(&namelist, ",");
435 if (!name) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000436 xfree(namebase);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000437 return (-1);
438 }
439 } while ((s = tcpconnect(name)) < 0);
440
441 if (s >= maxfd)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000442 fatal("conalloc: fdno %d too high", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000443 if (fdcon[s].c_status)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000444 fatal("conalloc: attempt to reuse fdno %d", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000445
446 fdcon[s].c_fd = s;
447 fdcon[s].c_status = CS_CON;
448 fdcon[s].c_namebase = namebase;
449 fdcon[s].c_name = name;
450 fdcon[s].c_namelist = namelist;
451 fdcon[s].c_output_name = xstrdup(oname);
452 fdcon[s].c_data = (char *) &fdcon[s].c_plen;
453 fdcon[s].c_len = 4;
454 fdcon[s].c_off = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000455 fdcon[s].c_keytype = keytype;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000456 gettimeofday(&fdcon[s].c_tv, NULL);
457 fdcon[s].c_tv.tv_sec += timeout;
458 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000459 FD_SET(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000460 ncon++;
461 return (s);
462}
463
Ben Lindstrombba81212001-06-25 05:01:22 +0000464static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000465confree(int s)
466{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000467 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000468 fatal("confree: attempt to free bad fdno %d", s);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000469 close(s);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000470 xfree(fdcon[s].c_namebase);
471 xfree(fdcon[s].c_output_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000472 if (fdcon[s].c_status == CS_KEYS)
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000473 xfree(fdcon[s].c_data);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000474 fdcon[s].c_status = CS_UNUSED;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000475 fdcon[s].c_keytype = 0;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000476 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000477 FD_CLR(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000478 ncon--;
479}
480
Ben Lindstrombba81212001-06-25 05:01:22 +0000481static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000482contouch(int s)
483{
484 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
485 gettimeofday(&fdcon[s].c_tv, NULL);
486 fdcon[s].c_tv.tv_sec += timeout;
487 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
488}
489
Ben Lindstrombba81212001-06-25 05:01:22 +0000490static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000491conrecycle(int s)
492{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000493 con *c = &fdcon[s];
Ben Lindstrom965710f2002-07-07 22:17:22 +0000494 int ret;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000495
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000496 ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000497 confree(s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000498 return (ret);
499}
500
Ben Lindstrombba81212001-06-25 05:01:22 +0000501static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000502congreet(int s)
503{
Damien Millereccb9de2005-06-17 12:59:34 +1000504 int n = 0, remote_major = 0, remote_minor = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000505 char buf[256], *cp;
Damien Miller83c02ef2001-12-21 12:45:43 +1100506 char remote_version[sizeof buf];
Damien Millereccb9de2005-06-17 12:59:34 +1000507 size_t bufsiz;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000508 con *c = &fdcon[s];
509
Damien Miller4bbacb72005-11-05 15:12:28 +1100510 for (;;) {
511 memset(buf, '\0', sizeof(buf));
512 bufsiz = sizeof(buf);
513 cp = buf;
514 while (bufsiz-- &&
515 (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
516 if (*cp == '\r')
517 *cp = '\n';
518 cp++;
519 }
520 if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
521 break;
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000522 }
Ben Lindstrom6b28c352002-03-05 01:54:52 +0000523 if (n == 0) {
Damien Millerb253cc42005-05-26 12:23:44 +1000524 switch (errno) {
525 case EPIPE:
526 error("%s: Connection closed by remote host", c->c_name);
527 break;
528 case ECONNREFUSED:
529 break;
530 default:
531 error("read (%s): %s", c->c_name, strerror(errno));
532 break;
533 }
Ben Lindstrom6b28c352002-03-05 01:54:52 +0000534 conrecycle(s);
535 return;
536 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000537 if (*cp != '\n' && *cp != '\r') {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000538 error("%s: bad greeting", c->c_name);
539 confree(s);
540 return;
541 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000542 *cp = '\0';
Damien Miller83c02ef2001-12-21 12:45:43 +1100543 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
544 &remote_major, &remote_minor, remote_version) == 3)
545 compat_datafellows(remote_version);
546 else
547 datafellows = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000548 if (c->c_keytype != KT_RSA1) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000549 if (!ssh2_capable(remote_major, remote_minor)) {
550 debug("%s doesn't support ssh2", c->c_name);
551 confree(s);
552 return;
553 }
Damien Miller83c02ef2001-12-21 12:45:43 +1100554 } else if (remote_major != 1) {
555 debug("%s doesn't support ssh1", c->c_name);
556 confree(s);
557 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000558 }
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000559 fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000560 n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
561 c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
562 c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
Damien Millereccb9de2005-06-17 12:59:34 +1000563 if (n < 0 || (size_t)n >= sizeof(buf)) {
Damien Miller41bfc292005-05-26 12:07:32 +1000564 error("snprintf: buffer too small");
565 confree(s);
566 return;
567 }
Damien Millereccb9de2005-06-17 12:59:34 +1000568 if (atomicio(vwrite, s, buf, n) != (size_t)n) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000569 error("write (%s): %s", c->c_name, strerror(errno));
570 confree(s);
571 return;
572 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000573 if (c->c_keytype != KT_RSA1) {
574 keyprint(c, keygrab_ssh2(c));
575 confree(s);
576 return;
577 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000578 c->c_status = CS_SIZE;
579 contouch(s);
580}
581
Ben Lindstrombba81212001-06-25 05:01:22 +0000582static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000583conread(int s)
584{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000585 con *c = &fdcon[s];
Damien Millerb253cc42005-05-26 12:23:44 +1000586 size_t n;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000587
588 if (c->c_status == CS_CON) {
589 congreet(s);
590 return;
591 }
Darren Tuckerfe6649d2004-08-13 21:19:37 +1000592 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
Damien Millerb253cc42005-05-26 12:23:44 +1000593 if (n == 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000594 error("read (%s): %s", c->c_name, strerror(errno));
595 confree(s);
596 return;
597 }
598 c->c_off += n;
599
600 if (c->c_off == c->c_len)
601 switch (c->c_status) {
602 case CS_SIZE:
603 c->c_plen = htonl(c->c_plen);
604 c->c_len = c->c_plen + 8 - (c->c_plen & 7);
605 c->c_off = 0;
606 c->c_data = xmalloc(c->c_len);
607 c->c_status = CS_KEYS;
608 break;
609 case CS_KEYS:
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000610 keyprint(c, keygrab_ssh1(c));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000611 confree(s);
612 return;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000613 default:
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000614 fatal("conread: invalid status %d", c->c_status);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000615 break;
616 }
617
618 contouch(s);
619}
620
Ben Lindstrombba81212001-06-25 05:01:22 +0000621static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000622conloop(void)
623{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000624 struct timeval seltime, now;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000625 fd_set *r, *e;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000626 con *c;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000627 int i;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000628
629 gettimeofday(&now, NULL);
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000630 c = TAILQ_FIRST(&tq);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000631
Ben Lindstromd20b8552001-03-05 07:01:18 +0000632 if (c && (c->c_tv.tv_sec > now.tv_sec ||
633 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000634 seltime = c->c_tv;
635 seltime.tv_sec -= now.tv_sec;
636 seltime.tv_usec -= now.tv_usec;
Ben Lindstromc791beb2001-02-10 23:18:11 +0000637 if (seltime.tv_usec < 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000638 seltime.tv_usec += 1000000;
639 seltime.tv_sec--;
640 }
641 } else
642 seltime.tv_sec = seltime.tv_usec = 0;
643
Damien Miller07d86be2006-03-26 14:19:21 +1100644 r = xcalloc(read_wait_nfdset, sizeof(fd_mask));
645 e = xcalloc(read_wait_nfdset, sizeof(fd_mask));
646 memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask));
647 memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask));
Ben Lindstromc1e04212001-03-05 07:04:38 +0000648
649 while (select(maxfd, r, NULL, e, &seltime) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000650 (errno == EAGAIN || errno == EINTR))
651 ;
652
Ben Lindstromd20b8552001-03-05 07:01:18 +0000653 for (i = 0; i < maxfd; i++) {
Ben Lindstromc1e04212001-03-05 07:04:38 +0000654 if (FD_ISSET(i, e)) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000655 error("%s: exception!", fdcon[i].c_name);
656 confree(i);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000657 } else if (FD_ISSET(i, r))
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000658 conread(i);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000659 }
Ben Lindstromc1e04212001-03-05 07:04:38 +0000660 xfree(r);
661 xfree(e);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000662
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000663 c = TAILQ_FIRST(&tq);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000664 while (c && (c->c_tv.tv_sec < now.tv_sec ||
665 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000666 int s = c->c_fd;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000667
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000668 c = TAILQ_NEXT(c, c_link);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000669 conrecycle(s);
670 }
671}
672
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000673static void
674do_host(char *host)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000675{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000676 char *name = strnnsep(&host, " \t\n");
677 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000678
Ben Lindstromeaffb9d2001-12-06 16:28:19 +0000679 if (name == NULL)
680 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000681 for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
682 if (get_keytypes & j) {
683 while (ncon >= MAXCON)
684 conloop();
685 conalloc(name, *host ? host : name, j);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000686 }
687 }
688}
689
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000690void
691fatal(const char *fmt,...)
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000692{
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000693 va_list args;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000694
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000695 va_start(args, fmt);
696 do_log(SYSLOG_LEVEL_FATAL, fmt, args);
697 va_end(args);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000698 if (nonfatal_fatal)
699 longjmp(kexjmp, -1);
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000700 else
Darren Tucker3d326222003-09-22 21:11:20 +1000701 exit(255);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000702}
703
704static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000705usage(void)
706{
Damien Miller9a2fdbd2005-03-02 12:04:01 +1100707 fprintf(stderr, "usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n"
Ben Lindstrom965710f2002-07-07 22:17:22 +0000708 "\t\t [host | addrlist namelist] [...]\n",
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000709 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000710 exit(1);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000711}
712
713int
714main(int argc, char **argv)
715{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000716 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
717 int opt, fopt_count = 0;
718 char *tname;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000719
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000720 extern int optind;
721 extern char *optarg;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000722
Damien Miller59d3d5b2003-08-22 09:34:41 +1000723 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom4e088e42001-10-10 20:45:43 +0000724 init_rng();
725 seed_rng();
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000726 TAILQ_INIT(&tq);
727
Darren Tuckerce321d82005-10-03 18:11:24 +1000728 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
729 sanitise_stdfd();
730
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000731 if (argc <= 1)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000732 usage();
733
Damien Millerdb7b8172005-03-01 21:48:03 +1100734 while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000735 switch (opt) {
Damien Millerdb7b8172005-03-01 21:48:03 +1100736 case 'H':
737 hash_hosts = 1;
738 break;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000739 case 'p':
740 ssh_port = a2port(optarg);
741 if (ssh_port == 0) {
742 fprintf(stderr, "Bad port '%s'\n", optarg);
743 exit(1);
744 }
745 break;
746 case 'T':
Ben Lindstromedd098b2002-07-04 00:07:13 +0000747 timeout = convtime(optarg);
748 if (timeout == -1 || timeout == 0) {
749 fprintf(stderr, "Bad timeout '%s'\n", optarg);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000750 usage();
Ben Lindstromedd098b2002-07-04 00:07:13 +0000751 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000752 break;
753 case 'v':
754 if (!debug_flag) {
755 debug_flag = 1;
756 log_level = SYSLOG_LEVEL_DEBUG1;
757 }
758 else if (log_level < SYSLOG_LEVEL_DEBUG3)
759 log_level++;
760 else
761 fatal("Too high debugging level.");
762 break;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000763 case 'f':
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000764 if (strcmp(optarg, "-") == 0)
765 optarg = NULL;
766 argv[fopt_count++] = optarg;
767 break;
768 case 't':
769 get_keytypes = 0;
770 tname = strtok(optarg, ",");
771 while (tname) {
772 int type = key_type_from_name(tname);
773 switch (type) {
774 case KEY_RSA1:
775 get_keytypes |= KT_RSA1;
776 break;
777 case KEY_DSA:
778 get_keytypes |= KT_DSA;
779 break;
780 case KEY_RSA:
781 get_keytypes |= KT_RSA;
782 break;
783 case KEY_UNSPEC:
Ben Lindstrom28c603b2001-12-06 16:45:10 +0000784 fatal("unknown key type %s", tname);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000785 }
786 tname = strtok(NULL, ",");
787 }
788 break;
789 case '4':
790 IPv4or6 = AF_INET;
791 break;
792 case '6':
793 IPv4or6 = AF_INET6;
794 break;
795 case '?':
796 default:
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000797 usage();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000798 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000799 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000800 if (optind == argc && !fopt_count)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000801 usage();
802
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000803 log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000804
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000805 maxfd = fdlim_get(1);
806 if (maxfd < 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000807 fatal("%s: fdlim_get: bad value", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000808 if (maxfd > MAXMAXFD)
809 maxfd = MAXMAXFD;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000810 if (MAXCON <= 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000811 fatal("%s: not enough file descriptors", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000812 if (maxfd > fdlim_get(0))
813 fdlim_set(maxfd);
Damien Miller07d86be2006-03-26 14:19:21 +1100814 fdcon = xcalloc(maxfd, sizeof(con));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000815
Damien Miller07d86be2006-03-26 14:19:21 +1100816 read_wait_nfdset = howmany(maxfd, NFDBITS);
817 read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
Ben Lindstromc1e04212001-03-05 07:04:38 +0000818
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000819 if (fopt_count) {
820 Linebuf *lb;
821 char *line;
822 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000823
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000824 for (j = 0; j < fopt_count; j++) {
825 lb = Linebuf_alloc(argv[j], error);
Ben Lindstrom78bbd9e2001-09-12 17:10:40 +0000826 if (!lb)
827 continue;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000828 while ((line = Linebuf_getline(lb)) != NULL)
829 do_host(line);
830 Linebuf_free(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000831 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000832 }
833
834 while (optind < argc)
835 do_host(argv[optind++]);
836
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000837 while (ncon > 0)
838 conloop();
839
840 return (0);
841}