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