]> andersk Git - openssh.git/blame - ssh-keyscan.c
- jakob@cvs.openbsd.org 2001/08/03 10:31:30
[openssh.git] / ssh-keyscan.c
CommitLineData
f6fdbddf 1/*
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
aa144206 6 * OpenBSD project by leaving this copyright notice intact.
f6fdbddf 7 */
8
9#include "includes.h"
33e766d2 10RCSID("$OpenBSD: ssh-keyscan.c,v 1.25 2001/08/03 10:31:30 jakob Exp $");
f6fdbddf 11
b705bea0 12#if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H)
f6fdbddf 13#include <sys/queue.h>
bf5f69f7 14#else
51e2fc8f 15#include "openbsd-compat/fake-queue.h"
bf5f69f7 16#endif
f6fdbddf 17#include <errno.h>
18
19#include <openssl/bn.h>
f6fdbddf 20
21#include "xmalloc.h"
22#include "ssh.h"
42f11eb2 23#include "ssh1.h"
f6fdbddf 24#include "key.h"
25#include "buffer.h"
26#include "bufaux.h"
42f11eb2 27#include "log.h"
2671b47f 28#include "atomicio.h"
f6fdbddf 29
30static int argno = 1; /* Number of argument currently being parsed */
31
32int family = AF_UNSPEC; /* IPv4, IPv6 or both */
33
f6fdbddf 34#define MAXMAXFD 256
35
36/* The number of seconds after which to give up on a TCP connection */
37int timeout = 5;
38
39int maxfd;
2671b47f 40#define MAXCON (maxfd - 10)
f6fdbddf 41
1fe6a48f 42#ifdef HAVE___PROGNAME
43extern char *__progname;
44#else
45char *__progname;
46#endif
ff7fee59 47fd_set *read_wait;
48size_t read_wait_size;
f6fdbddf 49int ncon;
50
51/*
52 * Keep a connection structure for each file descriptor. The state
53 * associated with file descriptor n is held in fdcon[n].
54 */
55typedef struct Connection {
1e3b8b07 56 u_char c_status; /* State of connection on this file desc. */
f6fdbddf 57#define CS_UNUSED 0 /* File descriptor unused */
58#define CS_CON 1 /* Waiting to connect/read greeting */
59#define CS_SIZE 2 /* Waiting to read initial packet size */
60#define CS_KEYS 3 /* Waiting to read public key packet */
61 int c_fd; /* Quick lookup: c->c_fd == c - fdcon */
62 int c_plen; /* Packet length field for ssh packet */
63 int c_len; /* Total bytes which must be read. */
64 int c_off; /* Length of data read so far. */
65 char *c_namebase; /* Address to free for c_name and c_namelist */
66 char *c_name; /* Hostname of connection for errors */
67 char *c_namelist; /* Pointer to other possible addresses */
68 char *c_output_name; /* Hostname of connection for output */
69 char *c_data; /* Data read from this fd */
70 struct timeval c_tv; /* Time at which connection gets aborted */
71 TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
72} con;
73
74TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */
75con *fdcon;
76
77/*
78 * This is just a wrapper around fgets() to make it usable.
79 */
80
81/* Stress-test. Increase this later. */
82#define LINEBUF_SIZE 16
83
84typedef struct {
85 char *buf;
1e3b8b07 86 u_int size;
f6fdbddf 87 int lineno;
88 const char *filename;
89 FILE *stream;
90 void (*errfun) (const char *,...);
91} Linebuf;
92
396c147e 93static Linebuf *
f6fdbddf 94Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
95{
96 Linebuf *lb;
97
98 if (!(lb = malloc(sizeof(*lb)))) {
99 if (errfun)
100 (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
101 return (NULL);
102 }
103 if (filename) {
104 lb->filename = filename;
105 if (!(lb->stream = fopen(filename, "r"))) {
b5c334cc 106 xfree(lb);
f6fdbddf 107 if (errfun)
108 (*errfun) ("%s: %s\n", filename, strerror(errno));
109 return (NULL);
110 }
111 } else {
112 lb->filename = "(stdin)";
113 lb->stream = stdin;
114 }
115
116 if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) {
117 if (errfun)
118 (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
b5c334cc 119 xfree(lb);
f6fdbddf 120 return (NULL);
121 }
122 lb->errfun = errfun;
123 lb->lineno = 0;
124 return (lb);
125}
126
396c147e 127static void
f6fdbddf 128Linebuf_free(Linebuf * lb)
129{
130 fclose(lb->stream);
b5c334cc 131 xfree(lb->buf);
132 xfree(lb);
f6fdbddf 133}
134
396c147e 135#if 0
136static void
f6fdbddf 137Linebuf_restart(Linebuf * lb)
138{
139 clearerr(lb->stream);
140 rewind(lb->stream);
141 lb->lineno = 0;
142}
143
396c147e 144static int
f6fdbddf 145Linebuf_lineno(Linebuf * lb)
146{
147 return (lb->lineno);
148}
396c147e 149#endif
f6fdbddf 150
396c147e 151static char *
5be2ec5e 152Linebuf_getline(Linebuf * lb)
f6fdbddf 153{
154 int n = 0;
155
156 lb->lineno++;
157 for (;;) {
158 /* Read a line */
159 if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
160 if (ferror(lb->stream) && lb->errfun)
778f6940 161 (*lb->errfun) ("%s: %s\n", lb->filename,
162 strerror(errno));
f6fdbddf 163 return (NULL);
164 }
165 n = strlen(lb->buf);
166
167 /* Return it or an error if it fits */
168 if (n > 0 && lb->buf[n - 1] == '\n') {
169 lb->buf[n - 1] = '\0';
170 return (lb->buf);
171 }
172 if (n != lb->size - 1) {
173 if (lb->errfun)
778f6940 174 (*lb->errfun) ("%s: skipping incomplete last line\n",
175 lb->filename);
f6fdbddf 176 return (NULL);
177 }
178 /* Double the buffer if we need more space */
179 if (!(lb->buf = realloc(lb->buf, (lb->size *= 2)))) {
180 if (lb->errfun)
778f6940 181 (*lb->errfun) ("linebuf (%s): realloc failed\n",
182 lb->filename);
f6fdbddf 183 return (NULL);
184 }
185 }
186}
187
396c147e 188static int
f6fdbddf 189fdlim_get(int hard)
190{
f318b98b 191#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
f6fdbddf 192 struct rlimit rlfd;
778f6940 193
f6fdbddf 194 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
195 return (-1);
196 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
197 return 10000;
198 else
199 return hard ? rlfd.rlim_max : rlfd.rlim_cur;
b03bd394 200#elif defined (HAVE_SYSCONF)
201 return sysconf (_SC_OPEN_MAX);
202#else
203 return 10000;
204#endif
f6fdbddf 205}
206
396c147e 207static int
f6fdbddf 208fdlim_set(int lim)
209{
f318b98b 210#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
f6fdbddf 211 struct rlimit rlfd;
b03bd394 212#endif
f6fdbddf 213 if (lim <= 0)
214 return (-1);
f318b98b 215#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
f6fdbddf 216 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
217 return (-1);
218 rlfd.rlim_cur = lim;
219 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
220 return (-1);
b03bd394 221#elif defined (HAVE_SETDTABLESIZE)
b705bea0 222 setdtablesize(lim);
b03bd394 223#endif
f6fdbddf 224 return (0);
225}
226
227/*
228 * This is an strsep function that returns a null field for adjacent
229 * separators. This is the same as the 4.4BSD strsep, but different from the
230 * one in the GNU libc.
231 */
396c147e 232static char *
f6fdbddf 233xstrsep(char **str, const char *delim)
234{
235 char *s, *e;
236
237 if (!**str)
238 return (NULL);
239
240 s = *str;
241 e = s + strcspn(s, delim);
242
243 if (*e != '\0')
244 *e++ = '\0';
245 *str = e;
246
247 return (s);
248}
249
250/*
251 * Get the next non-null token (like GNU strsep). Strsep() will return a
252 * null token for two adjacent separators, so we may have to loop.
253 */
396c147e 254static char *
f6fdbddf 255strnnsep(char **stringp, char *delim)
256{
257 char *tok;
258
259 do {
260 tok = xstrsep(stringp, delim);
261 } while (tok && *tok == '\0');
262 return (tok);
263}
264
396c147e 265static void
f6fdbddf 266keyprint(char *host, char *output_name, char *kd, int len)
267{
268 static Key *rsa;
269 static Buffer msg;
270
271 if (rsa == NULL) {
272 buffer_init(&msg);
273 rsa = key_new(KEY_RSA1);
274 }
275 buffer_append(&msg, kd, len);
276 buffer_consume(&msg, 8 - (len & 7)); /* padding */
277 if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
278 error("%s: invalid packet type", host);
279 buffer_clear(&msg);
280 return;
281 }
282 buffer_consume(&msg, 8); /* cookie */
283
284 /* server key */
285 (void) buffer_get_int(&msg);
286 buffer_get_bignum(&msg, rsa->rsa->e);
287 buffer_get_bignum(&msg, rsa->rsa->n);
288
289 /* host key */
290 (void) buffer_get_int(&msg);
291 buffer_get_bignum(&msg, rsa->rsa->e);
292 buffer_get_bignum(&msg, rsa->rsa->n);
293 buffer_clear(&msg);
294
295 fprintf(stdout, "%s ", output_name ? output_name : host);
296 key_write(rsa, stdout);
297 fputs("\n", stdout);
298}
299
396c147e 300static int
f6fdbddf 301tcpconnect(char *host)
302{
303 struct addrinfo hints, *ai, *aitop;
304 char strport[NI_MAXSERV];
305 int gaierr, s = -1;
306
b5c334cc 307 snprintf(strport, sizeof strport, "%d", SSH_DEFAULT_PORT);
f6fdbddf 308 memset(&hints, 0, sizeof(hints));
309 hints.ai_family = family;
310 hints.ai_socktype = SOCK_STREAM;
311 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
312 fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
313 for (ai = aitop; ai; ai = ai->ai_next) {
314 s = socket(ai->ai_family, SOCK_STREAM, 0);
315 if (s < 0) {
316 error("socket: %s", strerror(errno));
317 continue;
318 }
eea39c02 319 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
f6fdbddf 320 fatal("F_SETFL: %s", strerror(errno));
321 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
322 errno != EINPROGRESS)
323 error("connect (`%s'): %s", host, strerror(errno));
324 else
325 break;
326 close(s);
327 s = -1;
328 }
329 freeaddrinfo(aitop);
330 return s;
331}
332
396c147e 333static int
f6fdbddf 334conalloc(char *iname, char *oname)
335{
336 int s;
337 char *namebase, *name, *namelist;
338
339 namebase = namelist = xstrdup(iname);
340
341 do {
342 name = xstrsep(&namelist, ",");
343 if (!name) {
b5c334cc 344 xfree(namebase);
f6fdbddf 345 return (-1);
346 }
347 } while ((s = tcpconnect(name)) < 0);
348
349 if (s >= maxfd)
227e8e86 350 fatal("conalloc: fdno %d too high", s);
f6fdbddf 351 if (fdcon[s].c_status)
227e8e86 352 fatal("conalloc: attempt to reuse fdno %d", s);
f6fdbddf 353
354 fdcon[s].c_fd = s;
355 fdcon[s].c_status = CS_CON;
356 fdcon[s].c_namebase = namebase;
357 fdcon[s].c_name = name;
358 fdcon[s].c_namelist = namelist;
359 fdcon[s].c_output_name = xstrdup(oname);
360 fdcon[s].c_data = (char *) &fdcon[s].c_plen;
361 fdcon[s].c_len = 4;
362 fdcon[s].c_off = 0;
363 gettimeofday(&fdcon[s].c_tv, NULL);
364 fdcon[s].c_tv.tv_sec += timeout;
365 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
ff7fee59 366 FD_SET(s, read_wait);
f6fdbddf 367 ncon++;
368 return (s);
369}
370
396c147e 371static void
f6fdbddf 372confree(int s)
373{
f6fdbddf 374 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
227e8e86 375 fatal("confree: attempt to free bad fdno %d", s);
2671b47f 376 close(s);
b5c334cc 377 xfree(fdcon[s].c_namebase);
378 xfree(fdcon[s].c_output_name);
f6fdbddf 379 if (fdcon[s].c_status == CS_KEYS)
b5c334cc 380 xfree(fdcon[s].c_data);
f6fdbddf 381 fdcon[s].c_status = CS_UNUSED;
382 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
ff7fee59 383 FD_CLR(s, read_wait);
f6fdbddf 384 ncon--;
385}
386
396c147e 387static void
f6fdbddf 388contouch(int s)
389{
390 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
391 gettimeofday(&fdcon[s].c_tv, NULL);
392 fdcon[s].c_tv.tv_sec += timeout;
393 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
394}
395
396c147e 396static int
f6fdbddf 397conrecycle(int s)
398{
399 int ret;
400 con *c = &fdcon[s];
401 char *iname, *oname;
402
403 iname = xstrdup(c->c_namelist);
b5c334cc 404 oname = xstrdup(c->c_output_name);
f6fdbddf 405 confree(s);
406 ret = conalloc(iname, oname);
b5c334cc 407 xfree(iname);
408 xfree(oname);
f6fdbddf 409 return (ret);
410}
411
396c147e 412static void
f6fdbddf 413congreet(int s)
414{
c16c7f20 415 char buf[80], *cp;
416 size_t bufsiz;
3251b439 417 int n = 0;
f6fdbddf 418 con *c = &fdcon[s];
419
c16c7f20 420 bufsiz = sizeof(buf);
421 cp = buf;
422 while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n' && *cp != '\r')
423 cp++;
f6fdbddf 424 if (n < 0) {
425 if (errno != ECONNREFUSED)
426 error("read (%s): %s", c->c_name, strerror(errno));
427 conrecycle(s);
428 return;
429 }
c16c7f20 430 if (*cp != '\n' && *cp != '\r') {
f6fdbddf 431 error("%s: bad greeting", c->c_name);
432 confree(s);
433 return;
434 }
c16c7f20 435 *cp = '\0';
f6fdbddf 436 fprintf(stderr, "# %s %s\n", c->c_name, buf);
437 n = snprintf(buf, sizeof buf, "SSH-1.5-OpenSSH-keyscan\r\n");
2671b47f 438 if (atomicio(write, s, buf, n) != n) {
f6fdbddf 439 error("write (%s): %s", c->c_name, strerror(errno));
440 confree(s);
441 return;
442 }
443 c->c_status = CS_SIZE;
444 contouch(s);
445}
446
396c147e 447static void
f6fdbddf 448conread(int s)
449{
450 int n;
451 con *c = &fdcon[s];
452
453 if (c->c_status == CS_CON) {
454 congreet(s);
455 return;
456 }
457 n = read(s, c->c_data + c->c_off, c->c_len - c->c_off);
458 if (n < 0) {
459 error("read (%s): %s", c->c_name, strerror(errno));
460 confree(s);
461 return;
462 }
463 c->c_off += n;
464
465 if (c->c_off == c->c_len)
466 switch (c->c_status) {
467 case CS_SIZE:
468 c->c_plen = htonl(c->c_plen);
469 c->c_len = c->c_plen + 8 - (c->c_plen & 7);
470 c->c_off = 0;
471 c->c_data = xmalloc(c->c_len);
472 c->c_status = CS_KEYS;
473 break;
474 case CS_KEYS:
475 keyprint(c->c_name, c->c_output_name, c->c_data, c->c_plen);
476 confree(s);
477 return;
478 break;
479 default:
227e8e86 480 fatal("conread: invalid status %d", c->c_status);
f6fdbddf 481 break;
482 }
483
484 contouch(s);
485}
486
396c147e 487static void
f6fdbddf 488conloop(void)
489{
ff7fee59 490 fd_set *r, *e;
f6fdbddf 491 struct timeval seltime, now;
492 int i;
493 con *c;
494
495 gettimeofday(&now, NULL);
496 c = tq.tqh_first;
497
2671b47f 498 if (c && (c->c_tv.tv_sec > now.tv_sec ||
499 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
f6fdbddf 500 seltime = c->c_tv;
501 seltime.tv_sec -= now.tv_sec;
502 seltime.tv_usec -= now.tv_usec;
5be2ec5e 503 if (seltime.tv_usec < 0) {
f6fdbddf 504 seltime.tv_usec += 1000000;
505 seltime.tv_sec--;
506 }
507 } else
508 seltime.tv_sec = seltime.tv_usec = 0;
509
ff7fee59 510 r = xmalloc(read_wait_size);
511 memcpy(r, read_wait, read_wait_size);
512 e = xmalloc(read_wait_size);
513 memcpy(e, read_wait, read_wait_size);
514
515 while (select(maxfd, r, NULL, e, &seltime) == -1 &&
fd193ca4 516 (errno == EAGAIN || errno == EINTR))
517 ;
518
2671b47f 519 for (i = 0; i < maxfd; i++) {
ff7fee59 520 if (FD_ISSET(i, e)) {
f6fdbddf 521 error("%s: exception!", fdcon[i].c_name);
522 confree(i);
ff7fee59 523 } else if (FD_ISSET(i, r))
f6fdbddf 524 conread(i);
2671b47f 525 }
ff7fee59 526 xfree(r);
527 xfree(e);
f6fdbddf 528
529 c = tq.tqh_first;
2671b47f 530 while (c && (c->c_tv.tv_sec < now.tv_sec ||
531 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
f6fdbddf 532 int s = c->c_fd;
2671b47f 533
f6fdbddf 534 c = c->c_link.tqe_next;
535 conrecycle(s);
536 }
537}
538
396c147e 539static char *
f6fdbddf 540nexthost(int argc, char **argv)
541{
542 static Linebuf *lb;
543
544 for (;;) {
545 if (!lb) {
546 if (argno >= argc)
547 return (NULL);
548 if (argv[argno][0] != '-')
549 return (argv[argno++]);
550 if (!strcmp(argv[argno], "--")) {
551 if (++argno >= argc)
552 return (NULL);
553 return (argv[argno++]);
554 } else if (!strncmp(argv[argno], "-f", 2)) {
555 char *fname;
2671b47f 556
f6fdbddf 557 if (argv[argno][2])
558 fname = &argv[argno++][2];
559 else if (++argno >= argc) {
560 error("missing filename for `-f'");
561 return (NULL);
562 } else
563 fname = argv[argno++];
564 if (!strcmp(fname, "-"))
565 fname = NULL;
f20255cb 566 lb = Linebuf_alloc(fname, error);
f6fdbddf 567 } else
2671b47f 568 error("ignoring invalid/misplaced option `%s'",
569 argv[argno++]);
f6fdbddf 570 } else {
571 char *line;
2671b47f 572
5be2ec5e 573 line = Linebuf_getline(lb);
f6fdbddf 574 if (line)
575 return (line);
576 Linebuf_free(lb);
577 lb = NULL;
578 }
579 }
580}
581
396c147e 582static void
f6fdbddf 583usage(void)
584{
33e766d2 585 fprintf(stderr, "Usage: %s [options] [ host | addrlist namelist ]\n",
586 __progname);
587 fprintf(stderr, "Options:\n");
588 fprintf(stderr, " -t timeout Set connection timeout.\n");
589 fprintf(stderr, " -f file Read hosts or addresses from file.\n");
590 exit(1);
f6fdbddf 591}
592
593int
594main(int argc, char **argv)
595{
596 char *host = NULL;
597
1fe6a48f 598 __progname = get_progname(argv[0]);
f6fdbddf 599 TAILQ_INIT(&tq);
600
f6fdbddf 601 if (argc <= argno)
602 usage();
603
604 if (argv[1][0] == '-' && argv[1][1] == 't') {
605 argno++;
606 if (argv[1][2])
607 timeout = atoi(&argv[1][2]);
608 else {
609 if (argno >= argc)
610 usage();
611 timeout = atoi(argv[argno++]);
612 }
613 if (timeout <= 0)
614 usage();
615 }
616 if (argc <= argno)
617 usage();
618
619 maxfd = fdlim_get(1);
620 if (maxfd < 0)
227e8e86 621 fatal("%s: fdlim_get: bad value", __progname);
f6fdbddf 622 if (maxfd > MAXMAXFD)
623 maxfd = MAXMAXFD;
2671b47f 624 if (MAXCON <= 0)
227e8e86 625 fatal("%s: not enough file descriptors", __progname);
f6fdbddf 626 if (maxfd > fdlim_get(0))
627 fdlim_set(maxfd);
628 fdcon = xmalloc(maxfd * sizeof(con));
5be2ec5e 629 memset(fdcon, 0, maxfd * sizeof(con));
f6fdbddf 630
ff7fee59 631 read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
632 read_wait = xmalloc(read_wait_size);
633 memset(read_wait, 0, read_wait_size);
634
f6fdbddf 635 do {
2671b47f 636 while (ncon < MAXCON) {
f6fdbddf 637 char *name;
638
639 host = nexthost(argc, argv);
640 if (host == NULL)
641 break;
642 name = strnnsep(&host, " \t\n");
643 conalloc(name, *host ? host : name);
644 }
645 conloop();
646 } while (host);
647 while (ncon > 0)
648 conloop();
649
650 return (0);
651}
This page took 0.240671 seconds and 5 git commands to generate.