Annotation of src/usr.bin/ssh/ssh-keyscan.c, Revision 1.58
1.1 markus 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
1.23 pvalchev 6: * OpenBSD project by leaving this copyright notice intact.
1.1 markus 7: */
8:
9: #include "includes.h"
1.58 ! stevesk 10: RCSID("$OpenBSD: ssh-keyscan.c,v 1.57 2005/10/30 04:01:03 djm Exp $");
1.1 markus 11:
12: #include <sys/queue.h>
1.58 ! stevesk 13:
1.1 markus 14: #include <errno.h>
1.58 ! stevesk 15: #include <setjmp.h>
1.1 markus 16:
1.5 markus 17: #include <openssl/bn.h>
1.1 markus 18:
19: #include "xmalloc.h"
20: #include "ssh.h"
1.10 markus 21: #include "ssh1.h"
1.1 markus 22: #include "key.h"
1.26 markus 23: #include "kex.h"
24: #include "compat.h"
25: #include "myproposal.h"
26: #include "packet.h"
27: #include "dispatch.h"
1.1 markus 28: #include "buffer.h"
29: #include "bufaux.h"
1.11 markus 30: #include "log.h"
1.18 deraadt 31: #include "atomicio.h"
1.26 markus 32: #include "misc.h"
1.51 djm 33: #include "hostfile.h"
1.1 markus 34:
1.26 markus 35: /* 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. */
37: int IPv4or6 = AF_UNSPEC;
38:
39: int ssh_port = SSH_DEFAULT_PORT;
1.1 markus 40:
1.26 markus 41: #define KT_RSA1 1
42: #define KT_DSA 2
43: #define KT_RSA 4
44:
45: int get_keytypes = KT_RSA1; /* Get only RSA1 keys by default */
1.1 markus 46:
1.51 djm 47: int hash_hosts = 0; /* Hash hostname on output */
48:
1.1 markus 49: #define MAXMAXFD 256
50:
51: /* The number of seconds after which to give up on a TCP connection */
52: int timeout = 5;
53:
54: int maxfd;
1.18 deraadt 55: #define MAXCON (maxfd - 10)
1.1 markus 56:
1.3 markus 57: extern char *__progname;
1.19 millert 58: fd_set *read_wait;
59: size_t read_wait_size;
1.1 markus 60: int ncon;
1.26 markus 61: int nonfatal_fatal = 0;
62: jmp_buf kexjmp;
1.29 markus 63: Key *kexjmp_key;
1.1 markus 64:
65: /*
66: * Keep a connection structure for each file descriptor. The state
67: * associated with file descriptor n is held in fdcon[n].
68: */
69: typedef struct Connection {
1.6 markus 70: u_char c_status; /* State of connection on this file desc. */
1.1 markus 71: #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. */
1.26 markus 79: int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */
1.1 markus 80: 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 */
1.26 markus 85: Kex *c_kex; /* The key-exchange struct for ssh2 */
1.1 markus 86: 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:
90: TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */
91: con *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:
100: typedef struct {
101: char *buf;
1.6 markus 102: u_int size;
1.1 markus 103: int lineno;
104: const char *filename;
105: FILE *stream;
106: void (*errfun) (const char *,...);
107: } Linebuf;
108:
1.24 itojun 109: static Linebuf *
1.1 markus 110: Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
111: {
112: Linebuf *lb;
113:
114: if (!(lb = malloc(sizeof(*lb)))) {
115: if (errfun)
1.37 markus 116: (*errfun) ("linebuf (%s): malloc failed\n",
117: filename ? filename : "(stdin)");
1.1 markus 118: return (NULL);
119: }
120: if (filename) {
121: lb->filename = filename;
122: if (!(lb->stream = fopen(filename, "r"))) {
1.9 markus 123: xfree(lb);
1.1 markus 124: 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:
133: if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) {
134: if (errfun)
135: (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
1.9 markus 136: xfree(lb);
1.1 markus 137: return (NULL);
138: }
139: lb->errfun = errfun;
140: lb->lineno = 0;
141: return (lb);
142: }
143:
1.24 itojun 144: static void
1.1 markus 145: Linebuf_free(Linebuf * lb)
146: {
147: fclose(lb->stream);
1.9 markus 148: xfree(lb->buf);
149: xfree(lb);
1.1 markus 150: }
151:
1.24 itojun 152: #if 0
153: static void
1.1 markus 154: Linebuf_restart(Linebuf * lb)
155: {
156: clearerr(lb->stream);
157: rewind(lb->stream);
158: lb->lineno = 0;
159: }
160:
1.24 itojun 161: static int
1.1 markus 162: Linebuf_lineno(Linebuf * lb)
163: {
164: return (lb->lineno);
165: }
1.24 itojun 166: #endif
1.1 markus 167:
1.24 itojun 168: static char *
1.14 markus 169: Linebuf_getline(Linebuf * lb)
1.1 markus 170: {
1.55 djm 171: size_t n = 0;
1.39 deraadt 172: void *p;
1.1 markus 173:
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)
1.39 deraadt 179: (*lb->errfun)("%s: %s\n", lb->filename,
1.17 deraadt 180: strerror(errno));
1.1 markus 181: 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)
1.39 deraadt 192: (*lb->errfun)("%s: skipping incomplete last line\n",
1.17 deraadt 193: lb->filename);
1.1 markus 194: return (NULL);
195: }
196: /* Double the buffer if we need more space */
1.39 deraadt 197: lb->size *= 2;
198: if ((p = realloc(lb->buf, lb->size)) == NULL) {
199: lb->size /= 2;
1.1 markus 200: if (lb->errfun)
1.39 deraadt 201: (*lb->errfun)("linebuf (%s): realloc failed\n",
1.17 deraadt 202: lb->filename);
1.1 markus 203: return (NULL);
204: }
1.39 deraadt 205: lb->buf = p;
1.1 markus 206: }
207: }
208:
1.24 itojun 209: static int
1.1 markus 210: fdlim_get(int hard)
211: {
212: struct rlimit rlfd;
1.17 deraadt 213:
1.1 markus 214: if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
215: return (-1);
216: if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
1.46 djm 217: return sysconf(_SC_OPEN_MAX);
1.1 markus 218: else
219: return hard ? rlfd.rlim_max : rlfd.rlim_cur;
220: }
221:
1.24 itojun 222: static int
1.1 markus 223: fdlim_set(int lim)
224: {
225: struct rlimit rlfd;
1.39 deraadt 226:
1.1 markus 227: if (lim <= 0)
228: return (-1);
229: if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
230: return (-1);
231: rlfd.rlim_cur = lim;
232: if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
233: return (-1);
234: return (0);
235: }
236:
237: /*
238: * This is an strsep function that returns a null field for adjacent
239: * separators. This is the same as the 4.4BSD strsep, but different from the
240: * one in the GNU libc.
241: */
1.24 itojun 242: static char *
1.1 markus 243: xstrsep(char **str, const char *delim)
244: {
245: char *s, *e;
246:
247: if (!**str)
248: return (NULL);
249:
250: s = *str;
251: e = s + strcspn(s, delim);
252:
253: if (*e != '\0')
254: *e++ = '\0';
255: *str = e;
256:
257: return (s);
258: }
259:
260: /*
261: * Get the next non-null token (like GNU strsep). Strsep() will return a
262: * null token for two adjacent separators, so we may have to loop.
263: */
1.24 itojun 264: static char *
1.1 markus 265: strnnsep(char **stringp, char *delim)
266: {
267: char *tok;
268:
269: do {
270: tok = xstrsep(stringp, delim);
271: } while (tok && *tok == '\0');
272: return (tok);
273: }
274:
1.26 markus 275: static Key *
276: keygrab_ssh1(con *c)
1.1 markus 277: {
278: static Key *rsa;
279: static Buffer msg;
280:
281: if (rsa == NULL) {
282: buffer_init(&msg);
283: rsa = key_new(KEY_RSA1);
284: }
1.26 markus 285: buffer_append(&msg, c->c_data, c->c_plen);
286: buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */
1.1 markus 287: if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
1.26 markus 288: error("%s: invalid packet type", c->c_name);
1.1 markus 289: buffer_clear(&msg);
1.26 markus 290: return NULL;
1.1 markus 291: }
292: buffer_consume(&msg, 8); /* cookie */
293:
294: /* server key */
295: (void) buffer_get_int(&msg);
296: buffer_get_bignum(&msg, rsa->rsa->e);
297: buffer_get_bignum(&msg, rsa->rsa->n);
298:
299: /* host key */
300: (void) buffer_get_int(&msg);
301: buffer_get_bignum(&msg, rsa->rsa->e);
302: buffer_get_bignum(&msg, rsa->rsa->n);
1.26 markus 303:
1.1 markus 304: buffer_clear(&msg);
305:
1.26 markus 306: return (rsa);
307: }
308:
309: static int
310: hostjump(Key *hostkey)
311: {
1.29 markus 312: kexjmp_key = hostkey;
313: longjmp(kexjmp, 1);
1.26 markus 314: }
315:
316: static int
317: ssh2_capable(int remote_major, int remote_minor)
318: {
319: switch (remote_major) {
320: case 1:
321: if (remote_minor == 99)
322: return 1;
323: break;
324: case 2:
325: return 1;
326: default:
327: break;
328: }
329: return 0;
330: }
331:
332: static Key *
333: keygrab_ssh2(con *c)
334: {
335: int j;
336:
337: packet_set_connection(c->c_fd, c->c_fd);
338: enable_compat20();
339: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
340: "ssh-dss": "ssh-rsa";
341: c->c_kex = kex_setup(myproposal);
1.41 markus 342: c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
1.48 djm 343: c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
1.41 markus 344: c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
1.26 markus 345: c->c_kex->verify_host_key = hostjump;
346:
347: if (!(j = setjmp(kexjmp))) {
348: nonfatal_fatal = 1;
349: dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
350: fprintf(stderr, "Impossible! dispatch_run() returned!\n");
351: exit(1);
352: }
353: nonfatal_fatal = 0;
354: xfree(c->c_kex);
355: c->c_kex = NULL;
356: packet_close();
357:
1.29 markus 358: return j < 0? NULL : kexjmp_key;
1.26 markus 359: }
360:
361: static void
362: keyprint(con *c, Key *key)
363: {
1.51 djm 364: char *host = c->c_output_name ? c->c_output_name : c->c_name;
365:
1.26 markus 366: if (!key)
367: return;
1.51 djm 368: if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL)
369: fatal("host_hash failed");
1.26 markus 370:
1.51 djm 371: fprintf(stdout, "%s ", host);
1.26 markus 372: key_write(key, stdout);
1.1 markus 373: fputs("\n", stdout);
374: }
375:
1.24 itojun 376: static int
1.1 markus 377: tcpconnect(char *host)
378: {
379: struct addrinfo hints, *ai, *aitop;
380: char strport[NI_MAXSERV];
381: int gaierr, s = -1;
382:
1.26 markus 383: snprintf(strport, sizeof strport, "%d", ssh_port);
1.1 markus 384: memset(&hints, 0, sizeof(hints));
1.26 markus 385: hints.ai_family = IPv4or6;
1.1 markus 386: hints.ai_socktype = SOCK_STREAM;
387: if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
388: fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
389: for (ai = aitop; ai; ai = ai->ai_next) {
1.42 markus 390: s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.1 markus 391: if (s < 0) {
392: error("socket: %s", strerror(errno));
393: continue;
394: }
1.49 djm 395: if (set_nonblock(s) == -1)
396: fatal("%s: set_nonblock(%d)", __func__, s);
1.1 markus 397: if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
398: errno != EINPROGRESS)
399: error("connect (`%s'): %s", host, strerror(errno));
400: else
401: break;
402: close(s);
403: s = -1;
404: }
405: freeaddrinfo(aitop);
406: return s;
407: }
408:
1.24 itojun 409: static int
1.26 markus 410: conalloc(char *iname, char *oname, int keytype)
1.1 markus 411: {
1.39 deraadt 412: char *namebase, *name, *namelist;
1.1 markus 413: int s;
414:
415: namebase = namelist = xstrdup(iname);
416:
417: do {
418: name = xstrsep(&namelist, ",");
419: if (!name) {
1.9 markus 420: xfree(namebase);
1.1 markus 421: return (-1);
422: }
423: } while ((s = tcpconnect(name)) < 0);
424:
425: if (s >= maxfd)
1.4 markus 426: fatal("conalloc: fdno %d too high", s);
1.1 markus 427: if (fdcon[s].c_status)
1.4 markus 428: fatal("conalloc: attempt to reuse fdno %d", s);
1.1 markus 429:
430: fdcon[s].c_fd = s;
431: fdcon[s].c_status = CS_CON;
432: fdcon[s].c_namebase = namebase;
433: fdcon[s].c_name = name;
434: fdcon[s].c_namelist = namelist;
435: fdcon[s].c_output_name = xstrdup(oname);
436: fdcon[s].c_data = (char *) &fdcon[s].c_plen;
437: fdcon[s].c_len = 4;
438: fdcon[s].c_off = 0;
1.26 markus 439: fdcon[s].c_keytype = keytype;
1.1 markus 440: gettimeofday(&fdcon[s].c_tv, NULL);
441: fdcon[s].c_tv.tv_sec += timeout;
442: TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
1.19 millert 443: FD_SET(s, read_wait);
1.1 markus 444: ncon++;
445: return (s);
446: }
447:
1.24 itojun 448: static void
1.1 markus 449: confree(int s)
450: {
451: if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
1.4 markus 452: fatal("confree: attempt to free bad fdno %d", s);
1.18 deraadt 453: close(s);
1.9 markus 454: xfree(fdcon[s].c_namebase);
455: xfree(fdcon[s].c_output_name);
1.1 markus 456: if (fdcon[s].c_status == CS_KEYS)
1.9 markus 457: xfree(fdcon[s].c_data);
1.1 markus 458: fdcon[s].c_status = CS_UNUSED;
1.26 markus 459: fdcon[s].c_keytype = 0;
1.1 markus 460: TAILQ_REMOVE(&tq, &fdcon[s], c_link);
1.19 millert 461: FD_CLR(s, read_wait);
1.1 markus 462: ncon--;
463: }
464:
1.24 itojun 465: static void
1.1 markus 466: contouch(int s)
467: {
468: TAILQ_REMOVE(&tq, &fdcon[s], c_link);
469: gettimeofday(&fdcon[s].c_tv, NULL);
470: fdcon[s].c_tv.tv_sec += timeout;
471: TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
472: }
473:
1.24 itojun 474: static int
1.1 markus 475: conrecycle(int s)
476: {
1.39 deraadt 477: con *c = &fdcon[s];
1.1 markus 478: int ret;
479:
1.26 markus 480: ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
1.1 markus 481: confree(s);
482: return (ret);
483: }
484:
1.24 itojun 485: static void
1.1 markus 486: congreet(int s)
487: {
1.55 djm 488: int n = 0, remote_major = 0, remote_minor = 0;
1.26 markus 489: char buf[256], *cp;
1.33 markus 490: char remote_version[sizeof buf];
1.55 djm 491: size_t bufsiz;
1.1 markus 492: con *c = &fdcon[s];
493:
1.57 djm 494: for (;;) {
495: memset(buf, '\0', sizeof(buf));
496: bufsiz = sizeof(buf);
497: cp = buf;
498: while (bufsiz-- &&
499: (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
500: if (*cp == '\r')
501: *cp = '\n';
502: cp++;
503: }
504: if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
505: break;
1.27 markus 506: }
1.54 avsm 507: if (n == 0) {
508: switch (errno) {
509: case EPIPE:
510: error("%s: Connection closed by remote host", c->c_name);
511: break;
512: case ECONNREFUSED:
513: break;
514: default:
1.1 markus 515: error("read (%s): %s", c->c_name, strerror(errno));
1.54 avsm 516: break;
517: }
1.1 markus 518: conrecycle(s);
519: return;
520: }
1.21 millert 521: if (*cp != '\n' && *cp != '\r') {
1.1 markus 522: error("%s: bad greeting", c->c_name);
523: confree(s);
524: return;
525: }
1.21 millert 526: *cp = '\0';
1.33 markus 527: if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
528: &remote_major, &remote_minor, remote_version) == 3)
529: compat_datafellows(remote_version);
530: else
531: datafellows = 0;
1.26 markus 532: if (c->c_keytype != KT_RSA1) {
533: if (!ssh2_capable(remote_major, remote_minor)) {
534: debug("%s doesn't support ssh2", c->c_name);
535: confree(s);
536: return;
537: }
1.33 markus 538: } else if (remote_major != 1) {
539: debug("%s doesn't support ssh1", c->c_name);
540: confree(s);
541: return;
1.26 markus 542: }
1.27 markus 543: fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
1.26 markus 544: n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
545: c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
546: c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
1.55 djm 547: if (n < 0 || (size_t)n >= sizeof(buf)) {
1.53 moritz 548: error("snprintf: buffer too small");
549: confree(s);
550: return;
551: }
1.55 djm 552: if (atomicio(vwrite, s, buf, n) != (size_t)n) {
1.1 markus 553: error("write (%s): %s", c->c_name, strerror(errno));
554: confree(s);
555: return;
556: }
1.26 markus 557: if (c->c_keytype != KT_RSA1) {
558: keyprint(c, keygrab_ssh2(c));
559: confree(s);
560: return;
561: }
1.1 markus 562: c->c_status = CS_SIZE;
563: contouch(s);
564: }
565:
1.24 itojun 566: static void
1.1 markus 567: conread(int s)
568: {
1.39 deraadt 569: con *c = &fdcon[s];
1.54 avsm 570: size_t n;
1.1 markus 571:
572: if (c->c_status == CS_CON) {
573: congreet(s);
574: return;
575: }
1.50 avsm 576: n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
1.54 avsm 577: if (n == 0) {
1.1 markus 578: error("read (%s): %s", c->c_name, strerror(errno));
579: confree(s);
580: return;
581: }
582: c->c_off += n;
583:
584: if (c->c_off == c->c_len)
585: switch (c->c_status) {
586: case CS_SIZE:
587: c->c_plen = htonl(c->c_plen);
588: c->c_len = c->c_plen + 8 - (c->c_plen & 7);
589: c->c_off = 0;
590: c->c_data = xmalloc(c->c_len);
591: c->c_status = CS_KEYS;
592: break;
593: case CS_KEYS:
1.26 markus 594: keyprint(c, keygrab_ssh1(c));
1.1 markus 595: confree(s);
596: return;
597: break;
598: default:
1.4 markus 599: fatal("conread: invalid status %d", c->c_status);
1.1 markus 600: break;
601: }
602:
603: contouch(s);
604: }
605:
1.24 itojun 606: static void
1.1 markus 607: conloop(void)
608: {
1.39 deraadt 609: struct timeval seltime, now;
1.19 millert 610: fd_set *r, *e;
1.39 deraadt 611: con *c;
1.1 markus 612: int i;
613:
614: gettimeofday(&now, NULL);
1.36 itojun 615: c = TAILQ_FIRST(&tq);
1.1 markus 616:
1.18 deraadt 617: if (c && (c->c_tv.tv_sec > now.tv_sec ||
618: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
1.1 markus 619: seltime = c->c_tv;
620: seltime.tv_sec -= now.tv_sec;
621: seltime.tv_usec -= now.tv_usec;
1.13 itojun 622: if (seltime.tv_usec < 0) {
1.1 markus 623: seltime.tv_usec += 1000000;
624: seltime.tv_sec--;
625: }
626: } else
627: seltime.tv_sec = seltime.tv_usec = 0;
628:
1.19 millert 629: r = xmalloc(read_wait_size);
630: memcpy(r, read_wait, read_wait_size);
631: e = xmalloc(read_wait_size);
632: memcpy(e, read_wait, read_wait_size);
633:
634: while (select(maxfd, r, NULL, e, &seltime) == -1 &&
1.16 deraadt 635: (errno == EAGAIN || errno == EINTR))
636: ;
637:
1.18 deraadt 638: for (i = 0; i < maxfd; i++) {
1.19 millert 639: if (FD_ISSET(i, e)) {
1.1 markus 640: error("%s: exception!", fdcon[i].c_name);
641: confree(i);
1.19 millert 642: } else if (FD_ISSET(i, r))
1.1 markus 643: conread(i);
1.18 deraadt 644: }
1.19 millert 645: xfree(r);
646: xfree(e);
1.1 markus 647:
1.36 itojun 648: c = TAILQ_FIRST(&tq);
1.18 deraadt 649: while (c && (c->c_tv.tv_sec < now.tv_sec ||
650: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
1.1 markus 651: int s = c->c_fd;
1.18 deraadt 652:
1.36 itojun 653: c = TAILQ_NEXT(c, c_link);
1.1 markus 654: conrecycle(s);
655: }
656: }
657:
1.26 markus 658: static void
659: do_host(char *host)
1.1 markus 660: {
1.26 markus 661: char *name = strnnsep(&host, " \t\n");
662: int j;
1.1 markus 663:
1.31 markus 664: if (name == NULL)
665: return;
1.26 markus 666: for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
667: if (get_keytypes & j) {
668: while (ncon >= MAXCON)
669: conloop();
670: conalloc(name, *host ? host : name, j);
1.1 markus 671: }
672: }
673: }
674:
1.34 markus 675: void
676: fatal(const char *fmt,...)
1.26 markus 677: {
1.34 markus 678: va_list args;
1.39 deraadt 679:
1.34 markus 680: va_start(args, fmt);
681: do_log(SYSLOG_LEVEL_FATAL, fmt, args);
682: va_end(args);
1.26 markus 683: if (nonfatal_fatal)
684: longjmp(kexjmp, -1);
1.34 markus 685: else
1.45 markus 686: exit(255);
1.26 markus 687: }
688:
689: static void
1.1 markus 690: usage(void)
691: {
1.52 jmc 692: fprintf(stderr, "usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n"
1.39 deraadt 693: "\t\t [host | addrlist namelist] [...]\n",
1.25 jakob 694: __progname);
695: exit(1);
1.1 markus 696: }
697:
698: int
699: main(int argc, char **argv)
700: {
1.26 markus 701: int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
702: int opt, fopt_count = 0;
703: char *tname;
704:
705: extern int optind;
706: extern char *optarg;
1.1 markus 707:
708: TAILQ_INIT(&tq);
1.56 djm 709:
710: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
711: sanitise_stdfd();
1.1 markus 712:
1.26 markus 713: if (argc <= 1)
1.1 markus 714: usage();
715:
1.51 djm 716: while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) {
1.26 markus 717: switch (opt) {
1.51 djm 718: case 'H':
719: hash_hosts = 1;
720: break;
1.26 markus 721: case 'p':
722: ssh_port = a2port(optarg);
723: if (ssh_port == 0) {
724: fprintf(stderr, "Bad port '%s'\n", optarg);
725: exit(1);
726: }
727: break;
728: case 'T':
1.38 stevesk 729: timeout = convtime(optarg);
730: if (timeout == -1 || timeout == 0) {
731: fprintf(stderr, "Bad timeout '%s'\n", optarg);
1.1 markus 732: usage();
1.38 stevesk 733: }
1.26 markus 734: break;
735: case 'v':
736: if (!debug_flag) {
737: debug_flag = 1;
738: log_level = SYSLOG_LEVEL_DEBUG1;
739: }
740: else if (log_level < SYSLOG_LEVEL_DEBUG3)
741: log_level++;
742: else
743: fatal("Too high debugging level.");
744: break;
745: case 'f':
746: if (strcmp(optarg, "-") == 0)
747: optarg = NULL;
748: argv[fopt_count++] = optarg;
749: break;
750: case 't':
751: get_keytypes = 0;
752: tname = strtok(optarg, ",");
753: while (tname) {
754: int type = key_type_from_name(tname);
755: switch (type) {
756: case KEY_RSA1:
757: get_keytypes |= KT_RSA1;
758: break;
759: case KEY_DSA:
760: get_keytypes |= KT_DSA;
761: break;
762: case KEY_RSA:
763: get_keytypes |= KT_RSA;
764: break;
765: case KEY_UNSPEC:
1.32 stevesk 766: fatal("unknown key type %s", tname);
1.26 markus 767: }
768: tname = strtok(NULL, ",");
769: }
770: break;
771: case '4':
772: IPv4or6 = AF_INET;
773: break;
774: case '6':
775: IPv4or6 = AF_INET6;
776: break;
777: case '?':
778: default:
779: usage();
1.1 markus 780: }
781: }
1.26 markus 782: if (optind == argc && !fopt_count)
1.1 markus 783: usage();
784:
1.26 markus 785: log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
786:
1.1 markus 787: maxfd = fdlim_get(1);
788: if (maxfd < 0)
1.4 markus 789: fatal("%s: fdlim_get: bad value", __progname);
1.1 markus 790: if (maxfd > MAXMAXFD)
791: maxfd = MAXMAXFD;
1.18 deraadt 792: if (MAXCON <= 0)
1.4 markus 793: fatal("%s: not enough file descriptors", __progname);
1.1 markus 794: if (maxfd > fdlim_get(0))
795: fdlim_set(maxfd);
796: fdcon = xmalloc(maxfd * sizeof(con));
1.15 itojun 797: memset(fdcon, 0, maxfd * sizeof(con));
1.19 millert 798:
799: read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
800: read_wait = xmalloc(read_wait_size);
801: memset(read_wait, 0, read_wait_size);
1.1 markus 802:
1.26 markus 803: if (fopt_count) {
804: Linebuf *lb;
805: char *line;
806: int j;
807:
808: for (j = 0; j < fopt_count; j++) {
809: lb = Linebuf_alloc(argv[j], error);
1.28 danh 810: if (!lb)
811: continue;
1.26 markus 812: while ((line = Linebuf_getline(lb)) != NULL)
813: do_host(line);
814: Linebuf_free(lb);
815: }
816: }
817:
818: while (optind < argc)
819: do_host(argv[optind++]);
1.1 markus 820:
821: while (ncon > 0)
822: conloop();
823:
824: return (0);
825: }