blob: 3cb52ac2e9f3f4d4bf49a6add54ca56d580b0973 [file] [log] [blame]
Ben Lindstromb6434ae2000-12-05 01:15:09 +00001/*
2 * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
3 *
4 * Modification and redistribution in source and binary forms is
5 * permitted provided that due credit is given to the author and the
Ben Lindstroma238f6e2001-06-09 01:30:39 +00006 * OpenBSD project by leaving this copyright notice intact.
Ben Lindstromb6434ae2000-12-05 01:15:09 +00007 */
8
9#include "includes.h"
Darren Tuckerfe6649d2004-08-13 21:19:37 +100010RCSID("$OpenBSD: ssh-keyscan.c,v 1.50 2004/08/11 21:44:32 avsm Exp $");
Ben Lindstromb6434ae2000-12-05 01:15:09 +000011
Damien Miller9b481512002-09-12 10:43:29 +100012#include "openbsd-compat/sys-queue.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000013
14#include <openssl/bn.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000015
Ben Lindstrom325e70c2001-08-06 22:41:30 +000016#include <setjmp.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000017#include "xmalloc.h"
18#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000019#include "ssh1.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000020#include "key.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000021#include "kex.h"
22#include "compat.h"
23#include "myproposal.h"
24#include "packet.h"
25#include "dispatch.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000026#include "buffer.h"
27#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include "log.h"
Ben Lindstromd20b8552001-03-05 07:01:18 +000029#include "atomicio.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000030#include "misc.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000031
Ben Lindstrom325e70c2001-08-06 22:41:30 +000032/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
33 Default value is AF_UNSPEC means both IPv4 and IPv6. */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000034int IPv4or6 = AF_UNSPEC;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000035
Ben Lindstrom325e70c2001-08-06 22:41:30 +000036int ssh_port = SSH_DEFAULT_PORT;
Kevin Steves76e7d9b2001-09-20 20:30:09 +000037
38#define KT_RSA1 1
39#define KT_DSA 2
40#define KT_RSA 4
41
42int get_keytypes = KT_RSA1; /* Get only RSA1 keys by default */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000043
Ben Lindstromb6434ae2000-12-05 01:15:09 +000044#define MAXMAXFD 256
45
46/* The number of seconds after which to give up on a TCP connection */
47int timeout = 5;
48
49int maxfd;
Ben Lindstromd20b8552001-03-05 07:01:18 +000050#define MAXCON (maxfd - 10)
Ben Lindstromb6434ae2000-12-05 01:15:09 +000051
Kevin Stevesec84dc12000-12-13 17:45:15 +000052extern char *__progname;
Ben Lindstromc1e04212001-03-05 07:04:38 +000053fd_set *read_wait;
54size_t read_wait_size;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000055int ncon;
Ben Lindstrom325e70c2001-08-06 22:41:30 +000056int nonfatal_fatal = 0;
57jmp_buf kexjmp;
Ben Lindstrom520b55c2001-09-12 18:05:05 +000058Key *kexjmp_key;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000059
60/*
61 * Keep a connection structure for each file descriptor. The state
62 * associated with file descriptor n is held in fdcon[n].
63 */
64typedef struct Connection {
Ben Lindstrom46c16222000-12-22 01:43:59 +000065 u_char c_status; /* State of connection on this file desc. */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000066#define CS_UNUSED 0 /* File descriptor unused */
67#define CS_CON 1 /* Waiting to connect/read greeting */
68#define CS_SIZE 2 /* Waiting to read initial packet size */
69#define CS_KEYS 3 /* Waiting to read public key packet */
70 int c_fd; /* Quick lookup: c->c_fd == c - fdcon */
71 int c_plen; /* Packet length field for ssh packet */
72 int c_len; /* Total bytes which must be read. */
73 int c_off; /* Length of data read so far. */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000074 int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000075 char *c_namebase; /* Address to free for c_name and c_namelist */
76 char *c_name; /* Hostname of connection for errors */
77 char *c_namelist; /* Pointer to other possible addresses */
78 char *c_output_name; /* Hostname of connection for output */
79 char *c_data; /* Data read from this fd */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000080 Kex *c_kex; /* The key-exchange struct for ssh2 */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000081 struct timeval c_tv; /* Time at which connection gets aborted */
82 TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
83} con;
84
85TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */
86con *fdcon;
87
88/*
89 * This is just a wrapper around fgets() to make it usable.
90 */
91
92/* Stress-test. Increase this later. */
93#define LINEBUF_SIZE 16
94
95typedef struct {
96 char *buf;
Ben Lindstrom46c16222000-12-22 01:43:59 +000097 u_int size;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000098 int lineno;
99 const char *filename;
100 FILE *stream;
101 void (*errfun) (const char *,...);
102} Linebuf;
103
Ben Lindstrombba81212001-06-25 05:01:22 +0000104static Linebuf *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000105Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
106{
107 Linebuf *lb;
108
109 if (!(lb = malloc(sizeof(*lb)))) {
110 if (errfun)
Ben Lindstrom04f9af72002-07-04 00:03:56 +0000111 (*errfun) ("linebuf (%s): malloc failed\n",
112 filename ? filename : "(stdin)");
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000113 return (NULL);
114 }
115 if (filename) {
116 lb->filename = filename;
117 if (!(lb->stream = fopen(filename, "r"))) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000118 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000119 if (errfun)
120 (*errfun) ("%s: %s\n", filename, strerror(errno));
121 return (NULL);
122 }
123 } else {
124 lb->filename = "(stdin)";
125 lb->stream = stdin;
126 }
127
128 if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) {
129 if (errfun)
130 (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000131 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000132 return (NULL);
133 }
134 lb->errfun = errfun;
135 lb->lineno = 0;
136 return (lb);
137}
138
Ben Lindstrombba81212001-06-25 05:01:22 +0000139static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000140Linebuf_free(Linebuf * lb)
141{
142 fclose(lb->stream);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000143 xfree(lb->buf);
144 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000145}
146
Ben Lindstrombba81212001-06-25 05:01:22 +0000147#if 0
148static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000149Linebuf_restart(Linebuf * lb)
150{
151 clearerr(lb->stream);
152 rewind(lb->stream);
153 lb->lineno = 0;
154}
155
Ben Lindstrombba81212001-06-25 05:01:22 +0000156static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000157Linebuf_lineno(Linebuf * lb)
158{
159 return (lb->lineno);
160}
Ben Lindstrombba81212001-06-25 05:01:22 +0000161#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000162
Ben Lindstrombba81212001-06-25 05:01:22 +0000163static char *
Ben Lindstromc791beb2001-02-10 23:18:11 +0000164Linebuf_getline(Linebuf * lb)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000165{
166 int n = 0;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000167 void *p;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000168
169 lb->lineno++;
170 for (;;) {
171 /* Read a line */
172 if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
173 if (ferror(lb->stream) && lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000174 (*lb->errfun)("%s: %s\n", lb->filename,
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000175 strerror(errno));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000176 return (NULL);
177 }
178 n = strlen(lb->buf);
179
180 /* Return it or an error if it fits */
181 if (n > 0 && lb->buf[n - 1] == '\n') {
182 lb->buf[n - 1] = '\0';
183 return (lb->buf);
184 }
185 if (n != lb->size - 1) {
186 if (lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000187 (*lb->errfun)("%s: skipping incomplete last line\n",
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000188 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000189 return (NULL);
190 }
191 /* Double the buffer if we need more space */
Ben Lindstrom965710f2002-07-07 22:17:22 +0000192 lb->size *= 2;
193 if ((p = realloc(lb->buf, lb->size)) == NULL) {
194 lb->size /= 2;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000195 if (lb->errfun)
Ben Lindstrom965710f2002-07-07 22:17:22 +0000196 (*lb->errfun)("linebuf (%s): realloc failed\n",
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000197 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000198 return (NULL);
199 }
Ben Lindstrom965710f2002-07-07 22:17:22 +0000200 lb->buf = p;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000201 }
202}
203
Ben Lindstrombba81212001-06-25 05:01:22 +0000204static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000205fdlim_get(int hard)
206{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000207#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000208 struct rlimit rlfd;
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000209
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000210 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
211 return (-1);
212 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
Damien Millere00074a2003-11-24 13:07:45 +1100213 return SSH_SYSFDMAX;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000214 else
215 return hard ? rlfd.rlim_max : rlfd.rlim_cur;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000216#else
Damien Millere00074a2003-11-24 13:07:45 +1100217 return SSH_SYSFDMAX;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000218#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000219}
220
Ben Lindstrombba81212001-06-25 05:01:22 +0000221static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000222fdlim_set(int lim)
223{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000224#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000225 struct rlimit rlfd;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000226#endif
Ben Lindstrom5c98db52002-07-07 22:25:29 +0000227
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000228 if (lim <= 0)
229 return (-1);
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000230#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000231 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
232 return (-1);
233 rlfd.rlim_cur = lim;
234 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
235 return (-1);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000236#elif defined (HAVE_SETDTABLESIZE)
Kevin Steves28a7f262001-02-05 15:43:59 +0000237 setdtablesize(lim);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000238#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000239 return (0);
240}
241
242/*
243 * This is an strsep function that returns a null field for adjacent
244 * separators. This is the same as the 4.4BSD strsep, but different from the
245 * one in the GNU libc.
246 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000247static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000248xstrsep(char **str, const char *delim)
249{
250 char *s, *e;
251
252 if (!**str)
253 return (NULL);
254
255 s = *str;
256 e = s + strcspn(s, delim);
257
258 if (*e != '\0')
259 *e++ = '\0';
260 *str = e;
261
262 return (s);
263}
264
265/*
266 * Get the next non-null token (like GNU strsep). Strsep() will return a
267 * null token for two adjacent separators, so we may have to loop.
268 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000269static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000270strnnsep(char **stringp, char *delim)
271{
272 char *tok;
273
274 do {
275 tok = xstrsep(stringp, delim);
276 } while (tok && *tok == '\0');
277 return (tok);
278}
279
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000280static Key *
281keygrab_ssh1(con *c)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000282{
283 static Key *rsa;
284 static Buffer msg;
285
286 if (rsa == NULL) {
287 buffer_init(&msg);
288 rsa = key_new(KEY_RSA1);
289 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000290 buffer_append(&msg, c->c_data, c->c_plen);
291 buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000292 if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000293 error("%s: invalid packet type", c->c_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000294 buffer_clear(&msg);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000295 return NULL;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000296 }
297 buffer_consume(&msg, 8); /* cookie */
298
299 /* server key */
300 (void) buffer_get_int(&msg);
301 buffer_get_bignum(&msg, rsa->rsa->e);
302 buffer_get_bignum(&msg, rsa->rsa->n);
303
304 /* host key */
305 (void) buffer_get_int(&msg);
306 buffer_get_bignum(&msg, rsa->rsa->e);
307 buffer_get_bignum(&msg, rsa->rsa->n);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000308
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000309 buffer_clear(&msg);
310
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000311 return (rsa);
312}
313
314static int
315hostjump(Key *hostkey)
316{
Ben Lindstrom520b55c2001-09-12 18:05:05 +0000317 kexjmp_key = hostkey;
318 longjmp(kexjmp, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000319}
320
321static int
322ssh2_capable(int remote_major, int remote_minor)
323{
324 switch (remote_major) {
325 case 1:
326 if (remote_minor == 99)
327 return 1;
328 break;
329 case 2:
330 return 1;
331 default:
332 break;
333 }
334 return 0;
335}
336
337static Key *
338keygrab_ssh2(con *c)
339{
340 int j;
341
342 packet_set_connection(c->c_fd, c->c_fd);
343 enable_compat20();
344 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
345 "ssh-dss": "ssh-rsa";
346 c->c_kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100347 c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000348 c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100349 c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000350 c->c_kex->verify_host_key = hostjump;
351
352 if (!(j = setjmp(kexjmp))) {
353 nonfatal_fatal = 1;
354 dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
355 fprintf(stderr, "Impossible! dispatch_run() returned!\n");
356 exit(1);
357 }
358 nonfatal_fatal = 0;
359 xfree(c->c_kex);
360 c->c_kex = NULL;
361 packet_close();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000362
Ben Lindstrom520b55c2001-09-12 18:05:05 +0000363 return j < 0? NULL : kexjmp_key;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000364}
365
366static void
367keyprint(con *c, Key *key)
368{
369 if (!key)
370 return;
371
372 fprintf(stdout, "%s ", c->c_output_name ? c->c_output_name : c->c_name);
373 key_write(key, stdout);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000374 fputs("\n", stdout);
375}
376
Ben Lindstrombba81212001-06-25 05:01:22 +0000377static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000378tcpconnect(char *host)
379{
380 struct addrinfo hints, *ai, *aitop;
381 char strport[NI_MAXSERV];
382 int gaierr, s = -1;
383
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000384 snprintf(strport, sizeof strport, "%d", ssh_port);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000385 memset(&hints, 0, sizeof(hints));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000386 hints.ai_family = IPv4or6;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000387 hints.ai_socktype = SOCK_STREAM;
388 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
389 fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
390 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller2372ace2003-05-14 13:42:23 +1000391 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000392 if (s < 0) {
393 error("socket: %s", strerror(errno));
394 continue;
395 }
Damien Miller232711f2004-06-15 10:35:30 +1000396 if (set_nonblock(s) == -1)
397 fatal("%s: set_nonblock(%d)", __func__, s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000398 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
399 errno != EINPROGRESS)
400 error("connect (`%s'): %s", host, strerror(errno));
401 else
402 break;
403 close(s);
404 s = -1;
405 }
406 freeaddrinfo(aitop);
407 return s;
408}
409
Ben Lindstrombba81212001-06-25 05:01:22 +0000410static int
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000411conalloc(char *iname, char *oname, int keytype)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000412{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000413 char *namebase, *name, *namelist;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000414 int s;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000415
416 namebase = namelist = xstrdup(iname);
417
418 do {
419 name = xstrsep(&namelist, ",");
420 if (!name) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000421 xfree(namebase);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000422 return (-1);
423 }
424 } while ((s = tcpconnect(name)) < 0);
425
426 if (s >= maxfd)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000427 fatal("conalloc: fdno %d too high", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000428 if (fdcon[s].c_status)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000429 fatal("conalloc: attempt to reuse fdno %d", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000430
431 fdcon[s].c_fd = s;
432 fdcon[s].c_status = CS_CON;
433 fdcon[s].c_namebase = namebase;
434 fdcon[s].c_name = name;
435 fdcon[s].c_namelist = namelist;
436 fdcon[s].c_output_name = xstrdup(oname);
437 fdcon[s].c_data = (char *) &fdcon[s].c_plen;
438 fdcon[s].c_len = 4;
439 fdcon[s].c_off = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000440 fdcon[s].c_keytype = keytype;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000441 gettimeofday(&fdcon[s].c_tv, NULL);
442 fdcon[s].c_tv.tv_sec += timeout;
443 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000444 FD_SET(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000445 ncon++;
446 return (s);
447}
448
Ben Lindstrombba81212001-06-25 05:01:22 +0000449static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000450confree(int s)
451{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000452 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000453 fatal("confree: attempt to free bad fdno %d", s);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000454 close(s);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000455 xfree(fdcon[s].c_namebase);
456 xfree(fdcon[s].c_output_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000457 if (fdcon[s].c_status == CS_KEYS)
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000458 xfree(fdcon[s].c_data);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000459 fdcon[s].c_status = CS_UNUSED;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000460 fdcon[s].c_keytype = 0;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000461 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000462 FD_CLR(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000463 ncon--;
464}
465
Ben Lindstrombba81212001-06-25 05:01:22 +0000466static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000467contouch(int s)
468{
469 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
470 gettimeofday(&fdcon[s].c_tv, NULL);
471 fdcon[s].c_tv.tv_sec += timeout;
472 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
473}
474
Ben Lindstrombba81212001-06-25 05:01:22 +0000475static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000476conrecycle(int s)
477{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000478 con *c = &fdcon[s];
Ben Lindstrom965710f2002-07-07 22:17:22 +0000479 int ret;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000480
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000481 ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000482 confree(s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000483 return (ret);
484}
485
Ben Lindstrombba81212001-06-25 05:01:22 +0000486static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000487congreet(int s)
488{
Damien Miller3b513012004-03-08 23:13:00 +1100489 int remote_major = 0, remote_minor = 0, n = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000490 char buf[256], *cp;
Damien Miller83c02ef2001-12-21 12:45:43 +1100491 char remote_version[sizeof buf];
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000492 size_t bufsiz;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000493 con *c = &fdcon[s];
494
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000495 bufsiz = sizeof(buf);
496 cp = buf;
Darren Tuckerfe6649d2004-08-13 21:19:37 +1000497 while (bufsiz-- && (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000498 if (*cp == '\r')
499 *cp = '\n';
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000500 cp++;
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000501 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000502 if (n < 0) {
503 if (errno != ECONNREFUSED)
504 error("read (%s): %s", c->c_name, strerror(errno));
505 conrecycle(s);
506 return;
507 }
Ben Lindstrom6b28c352002-03-05 01:54:52 +0000508 if (n == 0) {
509 error("%s: Connection closed by remote host", c->c_name);
510 conrecycle(s);
511 return;
512 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000513 if (*cp != '\n' && *cp != '\r') {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000514 error("%s: bad greeting", c->c_name);
515 confree(s);
516 return;
517 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000518 *cp = '\0';
Damien Miller83c02ef2001-12-21 12:45:43 +1100519 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
520 &remote_major, &remote_minor, remote_version) == 3)
521 compat_datafellows(remote_version);
522 else
523 datafellows = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000524 if (c->c_keytype != KT_RSA1) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000525 if (!ssh2_capable(remote_major, remote_minor)) {
526 debug("%s doesn't support ssh2", c->c_name);
527 confree(s);
528 return;
529 }
Damien Miller83c02ef2001-12-21 12:45:43 +1100530 } else if (remote_major != 1) {
531 debug("%s doesn't support ssh1", c->c_name);
532 confree(s);
533 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000534 }
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000535 fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000536 n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
537 c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
538 c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
Darren Tucker9f63f222003-07-03 13:46:56 +1000539 if (atomicio(vwrite, s, buf, n) != n) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000540 error("write (%s): %s", c->c_name, strerror(errno));
541 confree(s);
542 return;
543 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000544 if (c->c_keytype != KT_RSA1) {
545 keyprint(c, keygrab_ssh2(c));
546 confree(s);
547 return;
548 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000549 c->c_status = CS_SIZE;
550 contouch(s);
551}
552
Ben Lindstrombba81212001-06-25 05:01:22 +0000553static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000554conread(int s)
555{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000556 con *c = &fdcon[s];
Ben Lindstrom965710f2002-07-07 22:17:22 +0000557 int n;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000558
559 if (c->c_status == CS_CON) {
560 congreet(s);
561 return;
562 }
Darren Tuckerfe6649d2004-08-13 21:19:37 +1000563 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000564 if (n < 0) {
565 error("read (%s): %s", c->c_name, strerror(errno));
566 confree(s);
567 return;
568 }
569 c->c_off += n;
570
571 if (c->c_off == c->c_len)
572 switch (c->c_status) {
573 case CS_SIZE:
574 c->c_plen = htonl(c->c_plen);
575 c->c_len = c->c_plen + 8 - (c->c_plen & 7);
576 c->c_off = 0;
577 c->c_data = xmalloc(c->c_len);
578 c->c_status = CS_KEYS;
579 break;
580 case CS_KEYS:
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000581 keyprint(c, keygrab_ssh1(c));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000582 confree(s);
583 return;
584 break;
585 default:
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000586 fatal("conread: invalid status %d", c->c_status);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000587 break;
588 }
589
590 contouch(s);
591}
592
Ben Lindstrombba81212001-06-25 05:01:22 +0000593static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000594conloop(void)
595{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000596 struct timeval seltime, now;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000597 fd_set *r, *e;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000598 con *c;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000599 int i;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000600
601 gettimeofday(&now, NULL);
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000602 c = TAILQ_FIRST(&tq);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000603
Ben Lindstromd20b8552001-03-05 07:01:18 +0000604 if (c && (c->c_tv.tv_sec > now.tv_sec ||
605 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000606 seltime = c->c_tv;
607 seltime.tv_sec -= now.tv_sec;
608 seltime.tv_usec -= now.tv_usec;
Ben Lindstromc791beb2001-02-10 23:18:11 +0000609 if (seltime.tv_usec < 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000610 seltime.tv_usec += 1000000;
611 seltime.tv_sec--;
612 }
613 } else
614 seltime.tv_sec = seltime.tv_usec = 0;
615
Ben Lindstromc1e04212001-03-05 07:04:38 +0000616 r = xmalloc(read_wait_size);
617 memcpy(r, read_wait, read_wait_size);
618 e = xmalloc(read_wait_size);
619 memcpy(e, read_wait, read_wait_size);
620
621 while (select(maxfd, r, NULL, e, &seltime) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000622 (errno == EAGAIN || errno == EINTR))
623 ;
624
Ben Lindstromd20b8552001-03-05 07:01:18 +0000625 for (i = 0; i < maxfd; i++) {
Ben Lindstromc1e04212001-03-05 07:04:38 +0000626 if (FD_ISSET(i, e)) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000627 error("%s: exception!", fdcon[i].c_name);
628 confree(i);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000629 } else if (FD_ISSET(i, r))
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000630 conread(i);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000631 }
Ben Lindstromc1e04212001-03-05 07:04:38 +0000632 xfree(r);
633 xfree(e);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000634
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000635 c = TAILQ_FIRST(&tq);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000636 while (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 int s = c->c_fd;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000639
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000640 c = TAILQ_NEXT(c, c_link);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000641 conrecycle(s);
642 }
643}
644
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000645static void
646do_host(char *host)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000647{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000648 char *name = strnnsep(&host, " \t\n");
649 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000650
Ben Lindstromeaffb9d2001-12-06 16:28:19 +0000651 if (name == NULL)
652 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000653 for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
654 if (get_keytypes & j) {
655 while (ncon >= MAXCON)
656 conloop();
657 conalloc(name, *host ? host : name, j);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000658 }
659 }
660}
661
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000662void
663fatal(const char *fmt,...)
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000664{
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000665 va_list args;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000666
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000667 va_start(args, fmt);
668 do_log(SYSLOG_LEVEL_FATAL, fmt, args);
669 va_end(args);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000670 if (nonfatal_fatal)
671 longjmp(kexjmp, -1);
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000672 else
Darren Tucker3d326222003-09-22 21:11:20 +1000673 exit(255);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000674}
675
676static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000677usage(void)
678{
Damien Millerea5ade22003-05-14 13:43:53 +1000679 fprintf(stderr, "usage: %s [-v46] [-p port] [-T timeout] [-t type] [-f file]\n"
Ben Lindstrom965710f2002-07-07 22:17:22 +0000680 "\t\t [host | addrlist namelist] [...]\n",
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000681 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000682 exit(1);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000683}
684
685int
686main(int argc, char **argv)
687{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000688 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
689 int opt, fopt_count = 0;
690 char *tname;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000691
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000692 extern int optind;
693 extern char *optarg;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000694
Damien Miller59d3d5b2003-08-22 09:34:41 +1000695 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom4e088e42001-10-10 20:45:43 +0000696 init_rng();
697 seed_rng();
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000698 TAILQ_INIT(&tq);
699
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000700 if (argc <= 1)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000701 usage();
702
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000703 while ((opt = getopt(argc, argv, "v46p:T:t:f:")) != -1) {
704 switch (opt) {
705 case 'p':
706 ssh_port = a2port(optarg);
707 if (ssh_port == 0) {
708 fprintf(stderr, "Bad port '%s'\n", optarg);
709 exit(1);
710 }
711 break;
712 case 'T':
Ben Lindstromedd098b2002-07-04 00:07:13 +0000713 timeout = convtime(optarg);
714 if (timeout == -1 || timeout == 0) {
715 fprintf(stderr, "Bad timeout '%s'\n", optarg);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000716 usage();
Ben Lindstromedd098b2002-07-04 00:07:13 +0000717 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000718 break;
719 case 'v':
720 if (!debug_flag) {
721 debug_flag = 1;
722 log_level = SYSLOG_LEVEL_DEBUG1;
723 }
724 else if (log_level < SYSLOG_LEVEL_DEBUG3)
725 log_level++;
726 else
727 fatal("Too high debugging level.");
728 break;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000729 case 'f':
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000730 if (strcmp(optarg, "-") == 0)
731 optarg = NULL;
732 argv[fopt_count++] = optarg;
733 break;
734 case 't':
735 get_keytypes = 0;
736 tname = strtok(optarg, ",");
737 while (tname) {
738 int type = key_type_from_name(tname);
739 switch (type) {
740 case KEY_RSA1:
741 get_keytypes |= KT_RSA1;
742 break;
743 case KEY_DSA:
744 get_keytypes |= KT_DSA;
745 break;
746 case KEY_RSA:
747 get_keytypes |= KT_RSA;
748 break;
749 case KEY_UNSPEC:
Ben Lindstrom28c603b2001-12-06 16:45:10 +0000750 fatal("unknown key type %s", tname);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000751 }
752 tname = strtok(NULL, ",");
753 }
754 break;
755 case '4':
756 IPv4or6 = AF_INET;
757 break;
758 case '6':
759 IPv4or6 = AF_INET6;
760 break;
761 case '?':
762 default:
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000763 usage();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000764 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000765 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000766 if (optind == argc && !fopt_count)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000767 usage();
768
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000769 log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000770
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000771 maxfd = fdlim_get(1);
772 if (maxfd < 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000773 fatal("%s: fdlim_get: bad value", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000774 if (maxfd > MAXMAXFD)
775 maxfd = MAXMAXFD;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000776 if (MAXCON <= 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000777 fatal("%s: not enough file descriptors", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000778 if (maxfd > fdlim_get(0))
779 fdlim_set(maxfd);
780 fdcon = xmalloc(maxfd * sizeof(con));
Ben Lindstromc791beb2001-02-10 23:18:11 +0000781 memset(fdcon, 0, maxfd * sizeof(con));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000782
Ben Lindstromc1e04212001-03-05 07:04:38 +0000783 read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
784 read_wait = xmalloc(read_wait_size);
785 memset(read_wait, 0, read_wait_size);
786
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000787 if (fopt_count) {
788 Linebuf *lb;
789 char *line;
790 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000791
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000792 for (j = 0; j < fopt_count; j++) {
793 lb = Linebuf_alloc(argv[j], error);
Ben Lindstrom78bbd9e2001-09-12 17:10:40 +0000794 if (!lb)
795 continue;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000796 while ((line = Linebuf_getline(lb)) != NULL)
797 do_host(line);
798 Linebuf_free(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000799 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000800 }
801
802 while (optind < argc)
803 do_host(argv[optind++]);
804
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000805 while (ncon > 0)
806 conloop();
807
808 return (0);
809}