blob: 76c2ed13cfd146c11bd281c3e77090b229b7da4c [file] [log] [blame]
Darren Tucker5d196262006-07-12 22:15:16 +10001/* $OpenBSD: ssh-keyscan.c,v 1.66 2006/07/10 16:37:36 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>
Darren Tucker5d196262006-07-12 22:15:16 +100014#include <stdarg.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000015
16#include <openssl/bn.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000017
Ben Lindstrom325e70c2001-08-06 22:41:30 +000018#include <setjmp.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000019#include "xmalloc.h"
20#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000021#include "ssh1.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000022#include "key.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000023#include "kex.h"
24#include "compat.h"
25#include "myproposal.h"
26#include "packet.h"
27#include "dispatch.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000028#include "buffer.h"
29#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#include "log.h"
Ben Lindstromd20b8552001-03-05 07:01:18 +000031#include "atomicio.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000032#include "misc.h"
Damien Millerdb7b8172005-03-01 21:48:03 +110033#include "hostfile.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000034
Ben Lindstrom325e70c2001-08-06 22:41:30 +000035/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
36 Default value is AF_UNSPEC means both IPv4 and IPv6. */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000037int IPv4or6 = AF_UNSPEC;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000038
Ben Lindstrom325e70c2001-08-06 22:41:30 +000039int ssh_port = SSH_DEFAULT_PORT;
Kevin Steves76e7d9b2001-09-20 20:30:09 +000040
41#define KT_RSA1 1
42#define KT_DSA 2
43#define KT_RSA 4
44
45int get_keytypes = KT_RSA1; /* Get only RSA1 keys by default */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000046
Damien Millerdb7b8172005-03-01 21:48:03 +110047int hash_hosts = 0; /* Hash hostname on output */
48
Ben Lindstromb6434ae2000-12-05 01:15:09 +000049#define MAXMAXFD 256
50
51/* The number of seconds after which to give up on a TCP connection */
52int timeout = 5;
53
54int maxfd;
Ben Lindstromd20b8552001-03-05 07:01:18 +000055#define MAXCON (maxfd - 10)
Ben Lindstromb6434ae2000-12-05 01:15:09 +000056
Kevin Stevesec84dc12000-12-13 17:45:15 +000057extern char *__progname;
Ben Lindstromc1e04212001-03-05 07:04:38 +000058fd_set *read_wait;
Damien Miller07d86be2006-03-26 14:19:21 +110059size_t read_wait_nfdset;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000060int ncon;
Ben Lindstrom325e70c2001-08-06 22:41:30 +000061int nonfatal_fatal = 0;
62jmp_buf kexjmp;
Ben Lindstrom520b55c2001-09-12 18:05:05 +000063Key *kexjmp_key;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000064
65/*
66 * Keep a connection structure for each file descriptor. The state
67 * associated with file descriptor n is held in fdcon[n].
68 */
69typedef struct Connection {
Ben Lindstrom46c16222000-12-22 01:43:59 +000070 u_char c_status; /* State of connection on this file desc. */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000071#define CS_UNUSED 0 /* File descriptor unused */
72#define CS_CON 1 /* Waiting to connect/read greeting */
73#define CS_SIZE 2 /* Waiting to read initial packet size */
74#define CS_KEYS 3 /* Waiting to read public key packet */
75 int c_fd; /* Quick lookup: c->c_fd == c - fdcon */
76 int c_plen; /* Packet length field for ssh packet */
77 int c_len; /* Total bytes which must be read. */
78 int c_off; /* Length of data read so far. */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000079 int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000080 char *c_namebase; /* Address to free for c_name and c_namelist */
81 char *c_name; /* Hostname of connection for errors */
82 char *c_namelist; /* Pointer to other possible addresses */
83 char *c_output_name; /* Hostname of connection for output */
84 char *c_data; /* Data read from this fd */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000085 Kex *c_kex; /* The key-exchange struct for ssh2 */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000086 struct timeval c_tv; /* Time at which connection gets aborted */
87 TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
88} con;
89
90TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */
91con *fdcon;
92
93/*
94 * This is just a wrapper around fgets() to make it usable.
95 */
96
97/* Stress-test. Increase this later. */
98#define LINEBUF_SIZE 16
99
100typedef struct {
101 char *buf;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000102 u_int size;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000103 int lineno;
104 const char *filename;
105 FILE *stream;
106 void (*errfun) (const char *,...);
107} Linebuf;
108
Ben Lindstrombba81212001-06-25 05:01:22 +0000109static Linebuf *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000110Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
111{
112 Linebuf *lb;
113
114 if (!(lb = malloc(sizeof(*lb)))) {
115 if (errfun)
Ben Lindstrom04f9af72002-07-04 00:03:56 +0000116 (*errfun) ("linebuf (%s): malloc failed\n",
117 filename ? filename : "(stdin)");
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000118 return (NULL);
119 }
120 if (filename) {
121 lb->filename = filename;
122 if (!(lb->stream = fopen(filename, "r"))) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000123 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000124 if (errfun)
125 (*errfun) ("%s: %s\n", filename, strerror(errno));
126 return (NULL);
127 }
128 } else {
129 lb->filename = "(stdin)";
130 lb->stream = stdin;
131 }
132
Damien Miller3bbaba62006-03-26 13:59:38 +1100133 if (!(lb->buf = malloc((lb->size = LINEBUF_SIZE)))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000134 if (errfun)
135 (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000136 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000137 return (NULL);
138 }
139 lb->errfun = errfun;
140 lb->lineno = 0;
141 return (lb);
142}
143
Ben Lindstrombba81212001-06-25 05:01:22 +0000144static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000145Linebuf_free(Linebuf * lb)
146{
147 fclose(lb->stream);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000148 xfree(lb->buf);
149 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000150}
151
Ben Lindstrombba81212001-06-25 05:01:22 +0000152#if 0
153static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000154Linebuf_restart(Linebuf * lb)
155{
156 clearerr(lb->stream);
157 rewind(lb->stream);
158 lb->lineno = 0;
159}
160
Ben Lindstrombba81212001-06-25 05:01:22 +0000161static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000162Linebuf_lineno(Linebuf * lb)
163{
164 return (lb->lineno);
165}
Ben Lindstrombba81212001-06-25 05:01:22 +0000166#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000167
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static char *
Ben Lindstromc791beb2001-02-10 23:18:11 +0000169Linebuf_getline(Linebuf * lb)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000170{
Damien Millereccb9de2005-06-17 12:59:34 +1000171 size_t n = 0;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000172 void *p;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000173
174 lb->lineno++;
175 for (;;) {
176 /* Read a line */
177 if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
178 if (ferror(lb->stream) && lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000179 (*lb->errfun)("%s: %s\n", lb->filename,
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000180 strerror(errno));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000181 return (NULL);
182 }
183 n = strlen(lb->buf);
184
185 /* Return it or an error if it fits */
186 if (n > 0 && lb->buf[n - 1] == '\n') {
187 lb->buf[n - 1] = '\0';
188 return (lb->buf);
189 }
190 if (n != lb->size - 1) {
191 if (lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000192 (*lb->errfun)("%s: skipping incomplete last line\n",
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000193 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000194 return (NULL);
195 }
196 /* Double the buffer if we need more space */
Ben Lindstrom965710f2002-07-07 22:17:22 +0000197 lb->size *= 2;
198 if ((p = realloc(lb->buf, lb->size)) == NULL) {
199 lb->size /= 2;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000200 if (lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000201 (*lb->errfun)("linebuf (%s): realloc failed\n",
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000202 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000203 return (NULL);
204 }
Ben Lindstrom965710f2002-07-07 22:17:22 +0000205 lb->buf = p;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000206 }
207}
208
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000210fdlim_get(int hard)
211{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000212#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000213 struct rlimit rlfd;
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000214
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000215 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
216 return (-1);
217 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
Damien Millere00074a2003-11-24 13:07:45 +1100218 return SSH_SYSFDMAX;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000219 else
220 return hard ? rlfd.rlim_max : rlfd.rlim_cur;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000221#else
Damien Millere00074a2003-11-24 13:07:45 +1100222 return SSH_SYSFDMAX;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000223#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000224}
225
Ben Lindstrombba81212001-06-25 05:01:22 +0000226static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000227fdlim_set(int lim)
228{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000229#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000230 struct rlimit rlfd;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000231#endif
Ben Lindstrom5c98db52002-07-07 22:25:29 +0000232
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000233 if (lim <= 0)
234 return (-1);
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000235#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000236 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
237 return (-1);
238 rlfd.rlim_cur = lim;
239 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
240 return (-1);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000241#elif defined (HAVE_SETDTABLESIZE)
Kevin Steves28a7f262001-02-05 15:43:59 +0000242 setdtablesize(lim);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000243#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000244 return (0);
245}
246
247/*
248 * This is an strsep function that returns a null field for adjacent
249 * separators. This is the same as the 4.4BSD strsep, but different from the
250 * one in the GNU libc.
251 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000252static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000253xstrsep(char **str, const char *delim)
254{
255 char *s, *e;
256
257 if (!**str)
258 return (NULL);
259
260 s = *str;
261 e = s + strcspn(s, delim);
262
263 if (*e != '\0')
264 *e++ = '\0';
265 *str = e;
266
267 return (s);
268}
269
270/*
271 * Get the next non-null token (like GNU strsep). Strsep() will return a
272 * null token for two adjacent separators, so we may have to loop.
273 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000274static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000275strnnsep(char **stringp, char *delim)
276{
277 char *tok;
278
279 do {
280 tok = xstrsep(stringp, delim);
281 } while (tok && *tok == '\0');
282 return (tok);
283}
284
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000285static Key *
286keygrab_ssh1(con *c)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000287{
288 static Key *rsa;
289 static Buffer msg;
290
291 if (rsa == NULL) {
292 buffer_init(&msg);
293 rsa = key_new(KEY_RSA1);
294 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000295 buffer_append(&msg, c->c_data, c->c_plen);
296 buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000297 if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000298 error("%s: invalid packet type", c->c_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000299 buffer_clear(&msg);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000300 return NULL;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000301 }
302 buffer_consume(&msg, 8); /* cookie */
303
304 /* server key */
305 (void) buffer_get_int(&msg);
306 buffer_get_bignum(&msg, rsa->rsa->e);
307 buffer_get_bignum(&msg, rsa->rsa->n);
308
309 /* host key */
310 (void) buffer_get_int(&msg);
311 buffer_get_bignum(&msg, rsa->rsa->e);
312 buffer_get_bignum(&msg, rsa->rsa->n);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000313
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000314 buffer_clear(&msg);
315
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000316 return (rsa);
317}
318
319static int
320hostjump(Key *hostkey)
321{
Ben Lindstrom520b55c2001-09-12 18:05:05 +0000322 kexjmp_key = hostkey;
323 longjmp(kexjmp, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000324}
325
326static int
327ssh2_capable(int remote_major, int remote_minor)
328{
329 switch (remote_major) {
330 case 1:
331 if (remote_minor == 99)
332 return 1;
333 break;
334 case 2:
335 return 1;
336 default:
337 break;
338 }
339 return 0;
340}
341
342static Key *
343keygrab_ssh2(con *c)
344{
345 int j;
346
347 packet_set_connection(c->c_fd, c->c_fd);
348 enable_compat20();
349 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
350 "ssh-dss": "ssh-rsa";
351 c->c_kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100352 c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000353 c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100354 c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100355 c->c_kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000356 c->c_kex->verify_host_key = hostjump;
357
358 if (!(j = setjmp(kexjmp))) {
359 nonfatal_fatal = 1;
360 dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
361 fprintf(stderr, "Impossible! dispatch_run() returned!\n");
362 exit(1);
363 }
364 nonfatal_fatal = 0;
365 xfree(c->c_kex);
366 c->c_kex = NULL;
367 packet_close();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000368
Ben Lindstrom520b55c2001-09-12 18:05:05 +0000369 return j < 0? NULL : kexjmp_key;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000370}
371
372static void
373keyprint(con *c, Key *key)
374{
Damien Millerdb7b8172005-03-01 21:48:03 +1100375 char *host = c->c_output_name ? c->c_output_name : c->c_name;
376
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000377 if (!key)
378 return;
Damien Millerdb7b8172005-03-01 21:48:03 +1100379 if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL)
380 fatal("host_hash failed");
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000381
Damien Millerdb7b8172005-03-01 21:48:03 +1100382 fprintf(stdout, "%s ", host);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000383 key_write(key, stdout);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000384 fputs("\n", stdout);
385}
386
Ben Lindstrombba81212001-06-25 05:01:22 +0000387static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000388tcpconnect(char *host)
389{
390 struct addrinfo hints, *ai, *aitop;
391 char strport[NI_MAXSERV];
392 int gaierr, s = -1;
393
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000394 snprintf(strport, sizeof strport, "%d", ssh_port);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000395 memset(&hints, 0, sizeof(hints));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000396 hints.ai_family = IPv4or6;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000397 hints.ai_socktype = SOCK_STREAM;
398 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
399 fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
400 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller2372ace2003-05-14 13:42:23 +1000401 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000402 if (s < 0) {
403 error("socket: %s", strerror(errno));
404 continue;
405 }
Damien Miller232711f2004-06-15 10:35:30 +1000406 if (set_nonblock(s) == -1)
407 fatal("%s: set_nonblock(%d)", __func__, s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000408 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
409 errno != EINPROGRESS)
410 error("connect (`%s'): %s", host, strerror(errno));
411 else
412 break;
413 close(s);
414 s = -1;
415 }
416 freeaddrinfo(aitop);
417 return s;
418}
419
Ben Lindstrombba81212001-06-25 05:01:22 +0000420static int
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000421conalloc(char *iname, char *oname, int keytype)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000422{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000423 char *namebase, *name, *namelist;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000424 int s;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000425
426 namebase = namelist = xstrdup(iname);
427
428 do {
429 name = xstrsep(&namelist, ",");
430 if (!name) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000431 xfree(namebase);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000432 return (-1);
433 }
434 } while ((s = tcpconnect(name)) < 0);
435
436 if (s >= maxfd)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000437 fatal("conalloc: fdno %d too high", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000438 if (fdcon[s].c_status)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000439 fatal("conalloc: attempt to reuse fdno %d", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000440
441 fdcon[s].c_fd = s;
442 fdcon[s].c_status = CS_CON;
443 fdcon[s].c_namebase = namebase;
444 fdcon[s].c_name = name;
445 fdcon[s].c_namelist = namelist;
446 fdcon[s].c_output_name = xstrdup(oname);
447 fdcon[s].c_data = (char *) &fdcon[s].c_plen;
448 fdcon[s].c_len = 4;
449 fdcon[s].c_off = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000450 fdcon[s].c_keytype = keytype;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000451 gettimeofday(&fdcon[s].c_tv, NULL);
452 fdcon[s].c_tv.tv_sec += timeout;
453 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000454 FD_SET(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000455 ncon++;
456 return (s);
457}
458
Ben Lindstrombba81212001-06-25 05:01:22 +0000459static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000460confree(int s)
461{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000462 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000463 fatal("confree: attempt to free bad fdno %d", s);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000464 close(s);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000465 xfree(fdcon[s].c_namebase);
466 xfree(fdcon[s].c_output_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000467 if (fdcon[s].c_status == CS_KEYS)
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000468 xfree(fdcon[s].c_data);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000469 fdcon[s].c_status = CS_UNUSED;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000470 fdcon[s].c_keytype = 0;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000471 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000472 FD_CLR(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000473 ncon--;
474}
475
Ben Lindstrombba81212001-06-25 05:01:22 +0000476static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000477contouch(int s)
478{
479 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
480 gettimeofday(&fdcon[s].c_tv, NULL);
481 fdcon[s].c_tv.tv_sec += timeout;
482 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
483}
484
Ben Lindstrombba81212001-06-25 05:01:22 +0000485static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000486conrecycle(int s)
487{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000488 con *c = &fdcon[s];
Ben Lindstrom965710f2002-07-07 22:17:22 +0000489 int ret;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000490
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000491 ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000492 confree(s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000493 return (ret);
494}
495
Ben Lindstrombba81212001-06-25 05:01:22 +0000496static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000497congreet(int s)
498{
Damien Millereccb9de2005-06-17 12:59:34 +1000499 int n = 0, remote_major = 0, remote_minor = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000500 char buf[256], *cp;
Damien Miller83c02ef2001-12-21 12:45:43 +1100501 char remote_version[sizeof buf];
Damien Millereccb9de2005-06-17 12:59:34 +1000502 size_t bufsiz;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000503 con *c = &fdcon[s];
504
Damien Miller4bbacb72005-11-05 15:12:28 +1100505 for (;;) {
506 memset(buf, '\0', sizeof(buf));
507 bufsiz = sizeof(buf);
508 cp = buf;
509 while (bufsiz-- &&
510 (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
511 if (*cp == '\r')
512 *cp = '\n';
513 cp++;
514 }
515 if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
516 break;
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000517 }
Ben Lindstrom6b28c352002-03-05 01:54:52 +0000518 if (n == 0) {
Damien Millerb253cc42005-05-26 12:23:44 +1000519 switch (errno) {
520 case EPIPE:
521 error("%s: Connection closed by remote host", c->c_name);
522 break;
523 case ECONNREFUSED:
524 break;
525 default:
526 error("read (%s): %s", c->c_name, strerror(errno));
527 break;
528 }
Ben Lindstrom6b28c352002-03-05 01:54:52 +0000529 conrecycle(s);
530 return;
531 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000532 if (*cp != '\n' && *cp != '\r') {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000533 error("%s: bad greeting", c->c_name);
534 confree(s);
535 return;
536 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000537 *cp = '\0';
Damien Miller83c02ef2001-12-21 12:45:43 +1100538 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
539 &remote_major, &remote_minor, remote_version) == 3)
540 compat_datafellows(remote_version);
541 else
542 datafellows = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000543 if (c->c_keytype != KT_RSA1) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000544 if (!ssh2_capable(remote_major, remote_minor)) {
545 debug("%s doesn't support ssh2", c->c_name);
546 confree(s);
547 return;
548 }
Damien Miller83c02ef2001-12-21 12:45:43 +1100549 } else if (remote_major != 1) {
550 debug("%s doesn't support ssh1", c->c_name);
551 confree(s);
552 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000553 }
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000554 fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000555 n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
556 c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
557 c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
Damien Millereccb9de2005-06-17 12:59:34 +1000558 if (n < 0 || (size_t)n >= sizeof(buf)) {
Damien Miller41bfc292005-05-26 12:07:32 +1000559 error("snprintf: buffer too small");
560 confree(s);
561 return;
562 }
Damien Millereccb9de2005-06-17 12:59:34 +1000563 if (atomicio(vwrite, s, buf, n) != (size_t)n) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000564 error("write (%s): %s", c->c_name, strerror(errno));
565 confree(s);
566 return;
567 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000568 if (c->c_keytype != KT_RSA1) {
569 keyprint(c, keygrab_ssh2(c));
570 confree(s);
571 return;
572 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000573 c->c_status = CS_SIZE;
574 contouch(s);
575}
576
Ben Lindstrombba81212001-06-25 05:01:22 +0000577static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000578conread(int s)
579{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000580 con *c = &fdcon[s];
Damien Millerb253cc42005-05-26 12:23:44 +1000581 size_t n;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000582
583 if (c->c_status == CS_CON) {
584 congreet(s);
585 return;
586 }
Darren Tuckerfe6649d2004-08-13 21:19:37 +1000587 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
Damien Millerb253cc42005-05-26 12:23:44 +1000588 if (n == 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000589 error("read (%s): %s", c->c_name, strerror(errno));
590 confree(s);
591 return;
592 }
593 c->c_off += n;
594
595 if (c->c_off == c->c_len)
596 switch (c->c_status) {
597 case CS_SIZE:
598 c->c_plen = htonl(c->c_plen);
599 c->c_len = c->c_plen + 8 - (c->c_plen & 7);
600 c->c_off = 0;
601 c->c_data = xmalloc(c->c_len);
602 c->c_status = CS_KEYS;
603 break;
604 case CS_KEYS:
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000605 keyprint(c, keygrab_ssh1(c));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000606 confree(s);
607 return;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000608 default:
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000609 fatal("conread: invalid status %d", c->c_status);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000610 break;
611 }
612
613 contouch(s);
614}
615
Ben Lindstrombba81212001-06-25 05:01:22 +0000616static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000617conloop(void)
618{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000619 struct timeval seltime, now;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000620 fd_set *r, *e;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000621 con *c;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000622 int i;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000623
624 gettimeofday(&now, NULL);
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000625 c = TAILQ_FIRST(&tq);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000626
Ben Lindstromd20b8552001-03-05 07:01:18 +0000627 if (c && (c->c_tv.tv_sec > now.tv_sec ||
628 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000629 seltime = c->c_tv;
630 seltime.tv_sec -= now.tv_sec;
631 seltime.tv_usec -= now.tv_usec;
Ben Lindstromc791beb2001-02-10 23:18:11 +0000632 if (seltime.tv_usec < 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000633 seltime.tv_usec += 1000000;
634 seltime.tv_sec--;
635 }
636 } else
637 seltime.tv_sec = seltime.tv_usec = 0;
638
Damien Miller07d86be2006-03-26 14:19:21 +1100639 r = xcalloc(read_wait_nfdset, sizeof(fd_mask));
640 e = xcalloc(read_wait_nfdset, sizeof(fd_mask));
641 memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask));
642 memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask));
Ben Lindstromc1e04212001-03-05 07:04:38 +0000643
644 while (select(maxfd, r, NULL, e, &seltime) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000645 (errno == EAGAIN || errno == EINTR))
646 ;
647
Ben Lindstromd20b8552001-03-05 07:01:18 +0000648 for (i = 0; i < maxfd; i++) {
Ben Lindstromc1e04212001-03-05 07:04:38 +0000649 if (FD_ISSET(i, e)) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000650 error("%s: exception!", fdcon[i].c_name);
651 confree(i);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000652 } else if (FD_ISSET(i, r))
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000653 conread(i);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000654 }
Ben Lindstromc1e04212001-03-05 07:04:38 +0000655 xfree(r);
656 xfree(e);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000657
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000658 c = TAILQ_FIRST(&tq);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000659 while (c && (c->c_tv.tv_sec < now.tv_sec ||
660 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000661 int s = c->c_fd;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000662
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000663 c = TAILQ_NEXT(c, c_link);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000664 conrecycle(s);
665 }
666}
667
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000668static void
669do_host(char *host)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000670{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000671 char *name = strnnsep(&host, " \t\n");
672 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000673
Ben Lindstromeaffb9d2001-12-06 16:28:19 +0000674 if (name == NULL)
675 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000676 for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
677 if (get_keytypes & j) {
678 while (ncon >= MAXCON)
679 conloop();
680 conalloc(name, *host ? host : name, j);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000681 }
682 }
683}
684
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000685void
686fatal(const char *fmt,...)
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000687{
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000688 va_list args;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000689
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000690 va_start(args, fmt);
691 do_log(SYSLOG_LEVEL_FATAL, fmt, args);
692 va_end(args);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000693 if (nonfatal_fatal)
694 longjmp(kexjmp, -1);
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000695 else
Darren Tucker3d326222003-09-22 21:11:20 +1000696 exit(255);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000697}
698
699static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000700usage(void)
701{
Damien Miller9a2fdbd2005-03-02 12:04:01 +1100702 fprintf(stderr, "usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n"
Ben Lindstrom965710f2002-07-07 22:17:22 +0000703 "\t\t [host | addrlist namelist] [...]\n",
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000704 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000705 exit(1);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000706}
707
708int
709main(int argc, char **argv)
710{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000711 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
712 int opt, fopt_count = 0;
713 char *tname;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000714
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000715 extern int optind;
716 extern char *optarg;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000717
Damien Miller59d3d5b2003-08-22 09:34:41 +1000718 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom4e088e42001-10-10 20:45:43 +0000719 init_rng();
720 seed_rng();
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000721 TAILQ_INIT(&tq);
722
Darren Tuckerce321d82005-10-03 18:11:24 +1000723 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
724 sanitise_stdfd();
725
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000726 if (argc <= 1)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000727 usage();
728
Damien Millerdb7b8172005-03-01 21:48:03 +1100729 while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000730 switch (opt) {
Damien Millerdb7b8172005-03-01 21:48:03 +1100731 case 'H':
732 hash_hosts = 1;
733 break;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000734 case 'p':
735 ssh_port = a2port(optarg);
736 if (ssh_port == 0) {
737 fprintf(stderr, "Bad port '%s'\n", optarg);
738 exit(1);
739 }
740 break;
741 case 'T':
Ben Lindstromedd098b2002-07-04 00:07:13 +0000742 timeout = convtime(optarg);
743 if (timeout == -1 || timeout == 0) {
744 fprintf(stderr, "Bad timeout '%s'\n", optarg);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000745 usage();
Ben Lindstromedd098b2002-07-04 00:07:13 +0000746 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000747 break;
748 case 'v':
749 if (!debug_flag) {
750 debug_flag = 1;
751 log_level = SYSLOG_LEVEL_DEBUG1;
752 }
753 else if (log_level < SYSLOG_LEVEL_DEBUG3)
754 log_level++;
755 else
756 fatal("Too high debugging level.");
757 break;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000758 case 'f':
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000759 if (strcmp(optarg, "-") == 0)
760 optarg = NULL;
761 argv[fopt_count++] = optarg;
762 break;
763 case 't':
764 get_keytypes = 0;
765 tname = strtok(optarg, ",");
766 while (tname) {
767 int type = key_type_from_name(tname);
768 switch (type) {
769 case KEY_RSA1:
770 get_keytypes |= KT_RSA1;
771 break;
772 case KEY_DSA:
773 get_keytypes |= KT_DSA;
774 break;
775 case KEY_RSA:
776 get_keytypes |= KT_RSA;
777 break;
778 case KEY_UNSPEC:
Ben Lindstrom28c603b2001-12-06 16:45:10 +0000779 fatal("unknown key type %s", tname);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000780 }
781 tname = strtok(NULL, ",");
782 }
783 break;
784 case '4':
785 IPv4or6 = AF_INET;
786 break;
787 case '6':
788 IPv4or6 = AF_INET6;
789 break;
790 case '?':
791 default:
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000792 usage();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000793 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000794 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000795 if (optind == argc && !fopt_count)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000796 usage();
797
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000798 log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000799
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000800 maxfd = fdlim_get(1);
801 if (maxfd < 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000802 fatal("%s: fdlim_get: bad value", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000803 if (maxfd > MAXMAXFD)
804 maxfd = MAXMAXFD;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000805 if (MAXCON <= 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000806 fatal("%s: not enough file descriptors", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000807 if (maxfd > fdlim_get(0))
808 fdlim_set(maxfd);
Damien Miller07d86be2006-03-26 14:19:21 +1100809 fdcon = xcalloc(maxfd, sizeof(con));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000810
Damien Miller07d86be2006-03-26 14:19:21 +1100811 read_wait_nfdset = howmany(maxfd, NFDBITS);
812 read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
Ben Lindstromc1e04212001-03-05 07:04:38 +0000813
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000814 if (fopt_count) {
815 Linebuf *lb;
816 char *line;
817 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000818
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000819 for (j = 0; j < fopt_count; j++) {
820 lb = Linebuf_alloc(argv[j], error);
Ben Lindstrom78bbd9e2001-09-12 17:10:40 +0000821 if (!lb)
822 continue;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000823 while ((line = Linebuf_getline(lb)) != NULL)
824 do_host(line);
825 Linebuf_free(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000826 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000827 }
828
829 while (optind < argc)
830 do_host(argv[optind++]);
831
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000832 while (ncon > 0)
833 conloop();
834
835 return (0);
836}