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