Annotation of src/usr.bin/ssh/ssh-keyscan.c, Revision 1.60
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.60 ! djm 10: RCSID("$OpenBSD: ssh-keyscan.c,v 1.59 2006/02/08 14:31:30 stevesk Exp $");
1.1 markus 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;
60: size_t read_wait_size;
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:
134: if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) {
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: break;
600: default:
1.4 markus 601: fatal("conread: invalid status %d", c->c_status);
1.1 markus 602: break;
603: }
604:
605: contouch(s);
606: }
607:
1.24 itojun 608: static void
1.1 markus 609: conloop(void)
610: {
1.39 deraadt 611: struct timeval seltime, now;
1.19 millert 612: fd_set *r, *e;
1.39 deraadt 613: con *c;
1.1 markus 614: int i;
615:
616: gettimeofday(&now, NULL);
1.36 itojun 617: c = TAILQ_FIRST(&tq);
1.1 markus 618:
1.18 deraadt 619: if (c && (c->c_tv.tv_sec > now.tv_sec ||
620: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
1.1 markus 621: seltime = c->c_tv;
622: seltime.tv_sec -= now.tv_sec;
623: seltime.tv_usec -= now.tv_usec;
1.13 itojun 624: if (seltime.tv_usec < 0) {
1.1 markus 625: seltime.tv_usec += 1000000;
626: seltime.tv_sec--;
627: }
628: } else
629: seltime.tv_sec = seltime.tv_usec = 0;
630:
1.19 millert 631: r = xmalloc(read_wait_size);
632: memcpy(r, read_wait, read_wait_size);
633: e = xmalloc(read_wait_size);
634: memcpy(e, read_wait, read_wait_size);
635:
636: while (select(maxfd, r, NULL, e, &seltime) == -1 &&
1.16 deraadt 637: (errno == EAGAIN || errno == EINTR))
638: ;
639:
1.18 deraadt 640: for (i = 0; i < maxfd; i++) {
1.19 millert 641: if (FD_ISSET(i, e)) {
1.1 markus 642: error("%s: exception!", fdcon[i].c_name);
643: confree(i);
1.19 millert 644: } else if (FD_ISSET(i, r))
1.1 markus 645: conread(i);
1.18 deraadt 646: }
1.19 millert 647: xfree(r);
648: xfree(e);
1.1 markus 649:
1.36 itojun 650: c = TAILQ_FIRST(&tq);
1.18 deraadt 651: while (c && (c->c_tv.tv_sec < now.tv_sec ||
652: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
1.1 markus 653: int s = c->c_fd;
1.18 deraadt 654:
1.36 itojun 655: c = TAILQ_NEXT(c, c_link);
1.1 markus 656: conrecycle(s);
657: }
658: }
659:
1.26 markus 660: static void
661: do_host(char *host)
1.1 markus 662: {
1.26 markus 663: char *name = strnnsep(&host, " \t\n");
664: int j;
1.1 markus 665:
1.31 markus 666: if (name == NULL)
667: return;
1.26 markus 668: for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
669: if (get_keytypes & j) {
670: while (ncon >= MAXCON)
671: conloop();
672: conalloc(name, *host ? host : name, j);
1.1 markus 673: }
674: }
675: }
676:
1.34 markus 677: void
678: fatal(const char *fmt,...)
1.26 markus 679: {
1.34 markus 680: va_list args;
1.39 deraadt 681:
1.34 markus 682: va_start(args, fmt);
683: do_log(SYSLOG_LEVEL_FATAL, fmt, args);
684: va_end(args);
1.26 markus 685: if (nonfatal_fatal)
686: longjmp(kexjmp, -1);
1.34 markus 687: else
1.45 markus 688: exit(255);
1.26 markus 689: }
690:
691: static void
1.1 markus 692: usage(void)
693: {
1.52 jmc 694: fprintf(stderr, "usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n"
1.39 deraadt 695: "\t\t [host | addrlist namelist] [...]\n",
1.25 jakob 696: __progname);
697: exit(1);
1.1 markus 698: }
699:
700: int
701: main(int argc, char **argv)
702: {
1.26 markus 703: int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
704: int opt, fopt_count = 0;
705: char *tname;
706:
707: extern int optind;
708: extern char *optarg;
1.1 markus 709:
710: TAILQ_INIT(&tq);
1.56 djm 711:
712: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
713: sanitise_stdfd();
1.1 markus 714:
1.26 markus 715: if (argc <= 1)
1.1 markus 716: usage();
717:
1.51 djm 718: while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) {
1.26 markus 719: switch (opt) {
1.51 djm 720: case 'H':
721: hash_hosts = 1;
722: break;
1.26 markus 723: case 'p':
724: ssh_port = a2port(optarg);
725: if (ssh_port == 0) {
726: fprintf(stderr, "Bad port '%s'\n", optarg);
727: exit(1);
728: }
729: break;
730: case 'T':
1.38 stevesk 731: timeout = convtime(optarg);
732: if (timeout == -1 || timeout == 0) {
733: fprintf(stderr, "Bad timeout '%s'\n", optarg);
1.1 markus 734: usage();
1.38 stevesk 735: }
1.26 markus 736: break;
737: case 'v':
738: if (!debug_flag) {
739: debug_flag = 1;
740: log_level = SYSLOG_LEVEL_DEBUG1;
741: }
742: else if (log_level < SYSLOG_LEVEL_DEBUG3)
743: log_level++;
744: else
745: fatal("Too high debugging level.");
746: break;
747: case 'f':
748: if (strcmp(optarg, "-") == 0)
749: optarg = NULL;
750: argv[fopt_count++] = optarg;
751: break;
752: case 't':
753: get_keytypes = 0;
754: tname = strtok(optarg, ",");
755: while (tname) {
756: int type = key_type_from_name(tname);
757: switch (type) {
758: case KEY_RSA1:
759: get_keytypes |= KT_RSA1;
760: break;
761: case KEY_DSA:
762: get_keytypes |= KT_DSA;
763: break;
764: case KEY_RSA:
765: get_keytypes |= KT_RSA;
766: break;
767: case KEY_UNSPEC:
1.32 stevesk 768: fatal("unknown key type %s", tname);
1.26 markus 769: }
770: tname = strtok(NULL, ",");
771: }
772: break;
773: case '4':
774: IPv4or6 = AF_INET;
775: break;
776: case '6':
777: IPv4or6 = AF_INET6;
778: break;
779: case '?':
780: default:
781: usage();
1.1 markus 782: }
783: }
1.26 markus 784: if (optind == argc && !fopt_count)
1.1 markus 785: usage();
786:
1.26 markus 787: log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
788:
1.1 markus 789: maxfd = fdlim_get(1);
790: if (maxfd < 0)
1.4 markus 791: fatal("%s: fdlim_get: bad value", __progname);
1.1 markus 792: if (maxfd > MAXMAXFD)
793: maxfd = MAXMAXFD;
1.18 deraadt 794: if (MAXCON <= 0)
1.4 markus 795: fatal("%s: not enough file descriptors", __progname);
1.1 markus 796: if (maxfd > fdlim_get(0))
797: fdlim_set(maxfd);
798: fdcon = xmalloc(maxfd * sizeof(con));
1.15 itojun 799: memset(fdcon, 0, maxfd * sizeof(con));
1.19 millert 800:
801: read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
802: read_wait = xmalloc(read_wait_size);
803: memset(read_wait, 0, read_wait_size);
1.1 markus 804:
1.26 markus 805: if (fopt_count) {
806: Linebuf *lb;
807: char *line;
808: int j;
809:
810: for (j = 0; j < fopt_count; j++) {
811: lb = Linebuf_alloc(argv[j], error);
1.28 danh 812: if (!lb)
813: continue;
1.26 markus 814: while ((line = Linebuf_getline(lb)) != NULL)
815: do_host(line);
816: Linebuf_free(lb);
817: }
818: }
819:
820: while (optind < argc)
821: do_host(argv[optind++]);
1.1 markus 822:
823: while (ncon > 0)
824: conloop();
825:
826: return (0);
827: }