Annotation of src/usr.bin/ssh/ssh-keyscan.c, Revision 1.85
1.85 ! okan 1: /* $OpenBSD: ssh-keyscan.c,v 1.84 2011/01/04 20:44:13 otto 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:
1.65 stevesk 10: #include <sys/types.h>
11: #include <sys/socket.h>
1.1 markus 12: #include <sys/queue.h>
1.74 djm 13: #include <sys/time.h>
1.59 stevesk 14: #include <sys/resource.h>
1.58 stevesk 15:
1.69 stevesk 16: #include <openssl/bn.h>
17:
1.1 markus 18: #include <errno.h>
1.67 stevesk 19: #include <netdb.h>
1.58 stevesk 20: #include <setjmp.h>
1.66 stevesk 21: #include <stdarg.h>
1.72 stevesk 22: #include <stdio.h>
1.71 stevesk 23: #include <stdlib.h>
1.73 deraadt 24: #include <signal.h>
1.69 stevesk 25: #include <string.h>
1.68 stevesk 26: #include <unistd.h>
1.1 markus 27:
28: #include "xmalloc.h"
29: #include "ssh.h"
1.10 markus 30: #include "ssh1.h"
1.73 deraadt 31: #include "buffer.h"
1.1 markus 32: #include "key.h"
1.73 deraadt 33: #include "cipher.h"
1.26 markus 34: #include "kex.h"
35: #include "compat.h"
36: #include "myproposal.h"
37: #include "packet.h"
38: #include "dispatch.h"
1.11 markus 39: #include "log.h"
1.18 deraadt 40: #include "atomicio.h"
1.26 markus 41: #include "misc.h"
1.51 djm 42: #include "hostfile.h"
1.1 markus 43:
1.26 markus 44: /* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
45: Default value is AF_UNSPEC means both IPv4 and IPv6. */
46: int IPv4or6 = AF_UNSPEC;
47:
48: int ssh_port = SSH_DEFAULT_PORT;
1.1 markus 49:
1.83 djm 50: #define KT_RSA1 1
51: #define KT_DSA 2
52: #define KT_RSA 4
53: #define KT_ECDSA 8
1.26 markus 54:
1.76 djm 55: int get_keytypes = KT_RSA; /* Get only RSA 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:
1.24 itojun 103: static int
1.1 markus 104: fdlim_get(int hard)
105: {
106: struct rlimit rlfd;
1.17 deraadt 107:
1.1 markus 108: if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
109: return (-1);
110: if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
1.46 djm 111: return sysconf(_SC_OPEN_MAX);
1.1 markus 112: else
113: return hard ? rlfd.rlim_max : rlfd.rlim_cur;
114: }
115:
1.24 itojun 116: static int
1.1 markus 117: fdlim_set(int lim)
118: {
119: struct rlimit rlfd;
1.39 deraadt 120:
1.1 markus 121: if (lim <= 0)
122: return (-1);
123: if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
124: return (-1);
125: rlfd.rlim_cur = lim;
126: if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
127: return (-1);
128: return (0);
129: }
130:
131: /*
132: * This is an strsep function that returns a null field for adjacent
133: * separators. This is the same as the 4.4BSD strsep, but different from the
134: * one in the GNU libc.
135: */
1.24 itojun 136: static char *
1.1 markus 137: xstrsep(char **str, const char *delim)
138: {
139: char *s, *e;
140:
141: if (!**str)
142: return (NULL);
143:
144: s = *str;
145: e = s + strcspn(s, delim);
146:
147: if (*e != '\0')
148: *e++ = '\0';
149: *str = e;
150:
151: return (s);
152: }
153:
154: /*
155: * Get the next non-null token (like GNU strsep). Strsep() will return a
156: * null token for two adjacent separators, so we may have to loop.
157: */
1.24 itojun 158: static char *
1.1 markus 159: strnnsep(char **stringp, char *delim)
160: {
161: char *tok;
162:
163: do {
164: tok = xstrsep(stringp, delim);
165: } while (tok && *tok == '\0');
166: return (tok);
167: }
168:
1.26 markus 169: static Key *
170: keygrab_ssh1(con *c)
1.1 markus 171: {
172: static Key *rsa;
173: static Buffer msg;
174:
175: if (rsa == NULL) {
176: buffer_init(&msg);
177: rsa = key_new(KEY_RSA1);
178: }
1.26 markus 179: buffer_append(&msg, c->c_data, c->c_plen);
180: buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */
1.1 markus 181: if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
1.26 markus 182: error("%s: invalid packet type", c->c_name);
1.1 markus 183: buffer_clear(&msg);
1.26 markus 184: return NULL;
1.1 markus 185: }
186: buffer_consume(&msg, 8); /* cookie */
187:
188: /* server key */
189: (void) buffer_get_int(&msg);
190: buffer_get_bignum(&msg, rsa->rsa->e);
191: buffer_get_bignum(&msg, rsa->rsa->n);
192:
193: /* host key */
194: (void) buffer_get_int(&msg);
195: buffer_get_bignum(&msg, rsa->rsa->e);
196: buffer_get_bignum(&msg, rsa->rsa->n);
1.26 markus 197:
1.1 markus 198: buffer_clear(&msg);
199:
1.26 markus 200: return (rsa);
201: }
202:
203: static int
204: hostjump(Key *hostkey)
205: {
1.29 markus 206: kexjmp_key = hostkey;
207: longjmp(kexjmp, 1);
1.26 markus 208: }
209:
210: static int
211: ssh2_capable(int remote_major, int remote_minor)
212: {
213: switch (remote_major) {
214: case 1:
215: if (remote_minor == 99)
216: return 1;
217: break;
218: case 2:
219: return 1;
220: default:
221: break;
222: }
223: return 0;
224: }
225:
226: static Key *
227: keygrab_ssh2(con *c)
228: {
229: int j;
230:
231: packet_set_connection(c->c_fd, c->c_fd);
232: enable_compat20();
233: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
1.84 otto 234: "ssh-dss" : (c->c_keytype == KT_RSA ? "ssh-rsa" :
235: "ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521");
1.26 markus 236: c->c_kex = kex_setup(myproposal);
1.41 markus 237: c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
1.48 djm 238: c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
1.41 markus 239: c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
1.60 djm 240: c->c_kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
1.83 djm 241: c->c_kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
1.26 markus 242: c->c_kex->verify_host_key = hostjump;
243:
244: if (!(j = setjmp(kexjmp))) {
245: nonfatal_fatal = 1;
246: dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
247: fprintf(stderr, "Impossible! dispatch_run() returned!\n");
248: exit(1);
249: }
250: nonfatal_fatal = 0;
251: xfree(c->c_kex);
252: c->c_kex = NULL;
253: packet_close();
254:
1.29 markus 255: return j < 0? NULL : kexjmp_key;
1.26 markus 256: }
257:
258: static void
259: keyprint(con *c, Key *key)
260: {
1.51 djm 261: char *host = c->c_output_name ? c->c_output_name : c->c_name;
262:
1.26 markus 263: if (!key)
264: return;
1.51 djm 265: if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL)
266: fatal("host_hash failed");
1.26 markus 267:
1.51 djm 268: fprintf(stdout, "%s ", host);
1.26 markus 269: key_write(key, stdout);
1.1 markus 270: fputs("\n", stdout);
271: }
272:
1.24 itojun 273: static int
1.1 markus 274: tcpconnect(char *host)
275: {
276: struct addrinfo hints, *ai, *aitop;
277: char strport[NI_MAXSERV];
278: int gaierr, s = -1;
279:
1.26 markus 280: snprintf(strport, sizeof strport, "%d", ssh_port);
1.1 markus 281: memset(&hints, 0, sizeof(hints));
1.26 markus 282: hints.ai_family = IPv4or6;
1.1 markus 283: hints.ai_socktype = SOCK_STREAM;
284: if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
1.75 dtucker 285: fatal("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr));
1.1 markus 286: for (ai = aitop; ai; ai = ai->ai_next) {
1.81 dtucker 287: s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.1 markus 288: if (s < 0) {
289: error("socket: %s", strerror(errno));
290: continue;
291: }
1.49 djm 292: if (set_nonblock(s) == -1)
293: fatal("%s: set_nonblock(%d)", __func__, s);
1.1 markus 294: if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
295: errno != EINPROGRESS)
296: error("connect (`%s'): %s", host, strerror(errno));
297: else
298: break;
299: close(s);
300: s = -1;
301: }
302: freeaddrinfo(aitop);
303: return s;
304: }
305:
1.24 itojun 306: static int
1.26 markus 307: conalloc(char *iname, char *oname, int keytype)
1.1 markus 308: {
1.39 deraadt 309: char *namebase, *name, *namelist;
1.1 markus 310: int s;
311:
312: namebase = namelist = xstrdup(iname);
313:
314: do {
315: name = xstrsep(&namelist, ",");
316: if (!name) {
1.9 markus 317: xfree(namebase);
1.1 markus 318: return (-1);
319: }
320: } while ((s = tcpconnect(name)) < 0);
321:
322: if (s >= maxfd)
1.4 markus 323: fatal("conalloc: fdno %d too high", s);
1.1 markus 324: if (fdcon[s].c_status)
1.4 markus 325: fatal("conalloc: attempt to reuse fdno %d", s);
1.1 markus 326:
327: fdcon[s].c_fd = s;
328: fdcon[s].c_status = CS_CON;
329: fdcon[s].c_namebase = namebase;
330: fdcon[s].c_name = name;
331: fdcon[s].c_namelist = namelist;
332: fdcon[s].c_output_name = xstrdup(oname);
333: fdcon[s].c_data = (char *) &fdcon[s].c_plen;
334: fdcon[s].c_len = 4;
335: fdcon[s].c_off = 0;
1.26 markus 336: fdcon[s].c_keytype = keytype;
1.1 markus 337: gettimeofday(&fdcon[s].c_tv, NULL);
338: fdcon[s].c_tv.tv_sec += timeout;
339: TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
1.19 millert 340: FD_SET(s, read_wait);
1.1 markus 341: ncon++;
342: return (s);
343: }
344:
1.24 itojun 345: static void
1.1 markus 346: confree(int s)
347: {
348: if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
1.4 markus 349: fatal("confree: attempt to free bad fdno %d", s);
1.18 deraadt 350: close(s);
1.9 markus 351: xfree(fdcon[s].c_namebase);
352: xfree(fdcon[s].c_output_name);
1.1 markus 353: if (fdcon[s].c_status == CS_KEYS)
1.9 markus 354: xfree(fdcon[s].c_data);
1.1 markus 355: fdcon[s].c_status = CS_UNUSED;
1.26 markus 356: fdcon[s].c_keytype = 0;
1.1 markus 357: TAILQ_REMOVE(&tq, &fdcon[s], c_link);
1.19 millert 358: FD_CLR(s, read_wait);
1.1 markus 359: ncon--;
360: }
361:
1.24 itojun 362: static void
1.1 markus 363: contouch(int s)
364: {
365: TAILQ_REMOVE(&tq, &fdcon[s], c_link);
366: gettimeofday(&fdcon[s].c_tv, NULL);
367: fdcon[s].c_tv.tv_sec += timeout;
368: TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
369: }
370:
1.24 itojun 371: static int
1.1 markus 372: conrecycle(int s)
373: {
1.39 deraadt 374: con *c = &fdcon[s];
1.1 markus 375: int ret;
376:
1.26 markus 377: ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
1.1 markus 378: confree(s);
379: return (ret);
380: }
381:
1.24 itojun 382: static void
1.1 markus 383: congreet(int s)
384: {
1.55 djm 385: int n = 0, remote_major = 0, remote_minor = 0;
1.26 markus 386: char buf[256], *cp;
1.33 markus 387: char remote_version[sizeof buf];
1.55 djm 388: size_t bufsiz;
1.1 markus 389: con *c = &fdcon[s];
390:
1.57 djm 391: for (;;) {
392: memset(buf, '\0', sizeof(buf));
393: bufsiz = sizeof(buf);
394: cp = buf;
395: while (bufsiz-- &&
396: (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
397: if (*cp == '\r')
398: *cp = '\n';
399: cp++;
400: }
401: if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
402: break;
1.27 markus 403: }
1.54 avsm 404: if (n == 0) {
405: switch (errno) {
406: case EPIPE:
407: error("%s: Connection closed by remote host", c->c_name);
408: break;
409: case ECONNREFUSED:
410: break;
411: default:
1.1 markus 412: error("read (%s): %s", c->c_name, strerror(errno));
1.54 avsm 413: break;
414: }
1.1 markus 415: conrecycle(s);
416: return;
417: }
1.21 millert 418: if (*cp != '\n' && *cp != '\r') {
1.1 markus 419: error("%s: bad greeting", c->c_name);
420: confree(s);
421: return;
422: }
1.21 millert 423: *cp = '\0';
1.33 markus 424: if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
425: &remote_major, &remote_minor, remote_version) == 3)
426: compat_datafellows(remote_version);
427: else
428: datafellows = 0;
1.26 markus 429: if (c->c_keytype != KT_RSA1) {
430: if (!ssh2_capable(remote_major, remote_minor)) {
431: debug("%s doesn't support ssh2", c->c_name);
432: confree(s);
433: return;
434: }
1.33 markus 435: } else if (remote_major != 1) {
436: debug("%s doesn't support ssh1", c->c_name);
437: confree(s);
438: return;
1.26 markus 439: }
1.27 markus 440: fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
1.26 markus 441: n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
442: c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
443: c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
1.55 djm 444: if (n < 0 || (size_t)n >= sizeof(buf)) {
1.53 moritz 445: error("snprintf: buffer too small");
446: confree(s);
447: return;
448: }
1.55 djm 449: if (atomicio(vwrite, s, buf, n) != (size_t)n) {
1.1 markus 450: error("write (%s): %s", c->c_name, strerror(errno));
451: confree(s);
452: return;
453: }
1.26 markus 454: if (c->c_keytype != KT_RSA1) {
455: keyprint(c, keygrab_ssh2(c));
456: confree(s);
457: return;
458: }
1.1 markus 459: c->c_status = CS_SIZE;
460: contouch(s);
461: }
462:
1.24 itojun 463: static void
1.1 markus 464: conread(int s)
465: {
1.39 deraadt 466: con *c = &fdcon[s];
1.54 avsm 467: size_t n;
1.1 markus 468:
469: if (c->c_status == CS_CON) {
470: congreet(s);
471: return;
472: }
1.50 avsm 473: n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
1.54 avsm 474: if (n == 0) {
1.1 markus 475: error("read (%s): %s", c->c_name, strerror(errno));
476: confree(s);
477: return;
478: }
479: c->c_off += n;
480:
481: if (c->c_off == c->c_len)
482: switch (c->c_status) {
483: case CS_SIZE:
484: c->c_plen = htonl(c->c_plen);
485: c->c_len = c->c_plen + 8 - (c->c_plen & 7);
486: c->c_off = 0;
487: c->c_data = xmalloc(c->c_len);
488: c->c_status = CS_KEYS;
489: break;
490: case CS_KEYS:
1.26 markus 491: keyprint(c, keygrab_ssh1(c));
1.1 markus 492: confree(s);
493: return;
494: default:
1.4 markus 495: fatal("conread: invalid status %d", c->c_status);
1.1 markus 496: break;
497: }
498:
499: contouch(s);
500: }
501:
1.24 itojun 502: static void
1.1 markus 503: conloop(void)
504: {
1.39 deraadt 505: struct timeval seltime, now;
1.19 millert 506: fd_set *r, *e;
1.39 deraadt 507: con *c;
1.1 markus 508: int i;
509:
510: gettimeofday(&now, NULL);
1.36 itojun 511: c = TAILQ_FIRST(&tq);
1.1 markus 512:
1.18 deraadt 513: if (c && (c->c_tv.tv_sec > now.tv_sec ||
514: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
1.1 markus 515: seltime = c->c_tv;
516: seltime.tv_sec -= now.tv_sec;
517: seltime.tv_usec -= now.tv_usec;
1.13 itojun 518: if (seltime.tv_usec < 0) {
1.1 markus 519: seltime.tv_usec += 1000000;
520: seltime.tv_sec--;
521: }
522: } else
1.85 ! okan 523: timerclear(&seltime);
1.1 markus 524:
1.63 djm 525: r = xcalloc(read_wait_nfdset, sizeof(fd_mask));
526: e = xcalloc(read_wait_nfdset, sizeof(fd_mask));
527: memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask));
528: memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask));
1.19 millert 529:
530: while (select(maxfd, r, NULL, e, &seltime) == -1 &&
1.16 deraadt 531: (errno == EAGAIN || errno == EINTR))
532: ;
533:
1.18 deraadt 534: for (i = 0; i < maxfd; i++) {
1.19 millert 535: if (FD_ISSET(i, e)) {
1.1 markus 536: error("%s: exception!", fdcon[i].c_name);
537: confree(i);
1.19 millert 538: } else if (FD_ISSET(i, r))
1.1 markus 539: conread(i);
1.18 deraadt 540: }
1.19 millert 541: xfree(r);
542: xfree(e);
1.1 markus 543:
1.36 itojun 544: c = TAILQ_FIRST(&tq);
1.18 deraadt 545: while (c && (c->c_tv.tv_sec < now.tv_sec ||
546: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
1.1 markus 547: int s = c->c_fd;
1.18 deraadt 548:
1.36 itojun 549: c = TAILQ_NEXT(c, c_link);
1.1 markus 550: conrecycle(s);
551: }
552: }
553:
1.26 markus 554: static void
555: do_host(char *host)
1.1 markus 556: {
1.26 markus 557: char *name = strnnsep(&host, " \t\n");
558: int j;
1.1 markus 559:
1.31 markus 560: if (name == NULL)
561: return;
1.84 otto 562: for (j = KT_RSA1; j <= KT_ECDSA; j *= 2) {
1.26 markus 563: if (get_keytypes & j) {
564: while (ncon >= MAXCON)
565: conloop();
566: conalloc(name, *host ? host : name, j);
1.1 markus 567: }
568: }
569: }
570:
1.34 markus 571: void
572: fatal(const char *fmt,...)
1.26 markus 573: {
1.34 markus 574: va_list args;
1.39 deraadt 575:
1.34 markus 576: va_start(args, fmt);
577: do_log(SYSLOG_LEVEL_FATAL, fmt, args);
578: va_end(args);
1.26 markus 579: if (nonfatal_fatal)
580: longjmp(kexjmp, -1);
1.34 markus 581: else
1.45 markus 582: exit(255);
1.26 markus 583: }
584:
585: static void
1.1 markus 586: usage(void)
587: {
1.77 sobrado 588: fprintf(stderr,
589: "usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n"
1.81 dtucker 590: "\t\t [host | addrlist namelist] ...\n",
1.25 jakob 591: __progname);
592: exit(1);
1.1 markus 593: }
594:
595: int
596: main(int argc, char **argv)
597: {
1.26 markus 598: int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
1.82 djm 599: int opt, fopt_count = 0, j;
600: char *tname, *cp, line[NI_MAXHOST];
601: FILE *fp;
602: u_long linenum;
1.26 markus 603:
604: extern int optind;
605: extern char *optarg;
1.1 markus 606:
607: TAILQ_INIT(&tq);
1.56 djm 608:
609: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
610: sanitise_stdfd();
1.1 markus 611:
1.26 markus 612: if (argc <= 1)
1.1 markus 613: usage();
614:
1.81 dtucker 615: while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) {
1.26 markus 616: switch (opt) {
1.51 djm 617: case 'H':
618: hash_hosts = 1;
619: break;
1.26 markus 620: case 'p':
621: ssh_port = a2port(optarg);
1.78 djm 622: if (ssh_port <= 0) {
1.26 markus 623: fprintf(stderr, "Bad port '%s'\n", optarg);
624: exit(1);
625: }
626: break;
627: case 'T':
1.38 stevesk 628: timeout = convtime(optarg);
629: if (timeout == -1 || timeout == 0) {
630: fprintf(stderr, "Bad timeout '%s'\n", optarg);
1.1 markus 631: usage();
1.38 stevesk 632: }
1.26 markus 633: break;
634: case 'v':
635: if (!debug_flag) {
636: debug_flag = 1;
637: log_level = SYSLOG_LEVEL_DEBUG1;
638: }
639: else if (log_level < SYSLOG_LEVEL_DEBUG3)
640: log_level++;
641: else
642: fatal("Too high debugging level.");
643: break;
644: case 'f':
645: if (strcmp(optarg, "-") == 0)
646: optarg = NULL;
647: argv[fopt_count++] = optarg;
648: break;
649: case 't':
650: get_keytypes = 0;
651: tname = strtok(optarg, ",");
652: while (tname) {
653: int type = key_type_from_name(tname);
654: switch (type) {
655: case KEY_RSA1:
656: get_keytypes |= KT_RSA1;
657: break;
658: case KEY_DSA:
659: get_keytypes |= KT_DSA;
1.83 djm 660: break;
661: case KEY_ECDSA:
662: get_keytypes |= KT_ECDSA;
1.26 markus 663: break;
664: case KEY_RSA:
665: get_keytypes |= KT_RSA;
666: break;
667: case KEY_UNSPEC:
1.32 stevesk 668: fatal("unknown key type %s", tname);
1.26 markus 669: }
670: tname = strtok(NULL, ",");
671: }
672: break;
673: case '4':
674: IPv4or6 = AF_INET;
675: break;
676: case '6':
677: IPv4or6 = AF_INET6;
678: break;
679: case '?':
680: default:
681: usage();
1.1 markus 682: }
683: }
1.26 markus 684: if (optind == argc && !fopt_count)
1.1 markus 685: usage();
686:
1.26 markus 687: log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
688:
1.1 markus 689: maxfd = fdlim_get(1);
690: if (maxfd < 0)
1.4 markus 691: fatal("%s: fdlim_get: bad value", __progname);
1.1 markus 692: if (maxfd > MAXMAXFD)
693: maxfd = MAXMAXFD;
1.18 deraadt 694: if (MAXCON <= 0)
1.4 markus 695: fatal("%s: not enough file descriptors", __progname);
1.1 markus 696: if (maxfd > fdlim_get(0))
697: fdlim_set(maxfd);
1.63 djm 698: fdcon = xcalloc(maxfd, sizeof(con));
1.19 millert 699:
1.63 djm 700: read_wait_nfdset = howmany(maxfd, NFDBITS);
701: read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
1.1 markus 702:
1.82 djm 703: for (j = 0; j < fopt_count; j++) {
704: if (argv[j] == NULL)
705: fp = stdin;
706: else if ((fp = fopen(argv[j], "r")) == NULL)
707: fatal("%s: %s: %s", __progname, argv[j],
708: strerror(errno));
709: linenum = 0;
710:
711: while (read_keyfile_line(fp,
712: argv[j] == NULL ? "(stdin)" : argv[j], line, sizeof(line),
713: &linenum) != -1) {
714: /* Chomp off trailing whitespace and comments */
715: if ((cp = strchr(line, '#')) == NULL)
716: cp = line + strlen(line) - 1;
717: while (cp >= line) {
718: if (*cp == ' ' || *cp == '\t' ||
719: *cp == '\n' || *cp == '#')
720: *cp-- = '\0';
721: else
722: break;
723: }
724:
725: /* Skip empty lines */
726: if (*line == '\0')
1.28 danh 727: continue;
1.82 djm 728:
729: do_host(line);
1.26 markus 730: }
1.82 djm 731:
732: if (ferror(fp))
733: fatal("%s: %s: %s", __progname, argv[j],
734: strerror(errno));
735:
736: fclose(fp);
1.26 markus 737: }
738:
739: while (optind < argc)
740: do_host(argv[optind++]);
1.1 markus 741:
742: while (ncon > 0)
743: conloop();
744:
745: return (0);
746: }