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