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