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