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