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