xref: /openbsd-src/usr.bin/ssh/ssh-keyscan.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
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.40 2002/07/06 17:47:58 stevesk 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 10000;
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->verify_host_key = hostjump;
339 
340 	if (!(j = setjmp(kexjmp))) {
341 		nonfatal_fatal = 1;
342 		dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
343 		fprintf(stderr, "Impossible! dispatch_run() returned!\n");
344 		exit(1);
345 	}
346 	nonfatal_fatal = 0;
347 	xfree(c->c_kex);
348 	c->c_kex = NULL;
349 	packet_close();
350 
351 	return j < 0? NULL : kexjmp_key;
352 }
353 
354 static void
355 keyprint(con *c, Key *key)
356 {
357 	if (!key)
358 		return;
359 
360 	fprintf(stdout, "%s ", c->c_output_name ? c->c_output_name : c->c_name);
361 	key_write(key, stdout);
362 	fputs("\n", stdout);
363 }
364 
365 static int
366 tcpconnect(char *host)
367 {
368 	struct addrinfo hints, *ai, *aitop;
369 	char strport[NI_MAXSERV];
370 	int gaierr, s = -1;
371 
372 	snprintf(strport, sizeof strport, "%d", ssh_port);
373 	memset(&hints, 0, sizeof(hints));
374 	hints.ai_family = IPv4or6;
375 	hints.ai_socktype = SOCK_STREAM;
376 	if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
377 		fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
378 	for (ai = aitop; ai; ai = ai->ai_next) {
379 		s = socket(ai->ai_family, SOCK_STREAM, 0);
380 		if (s < 0) {
381 			error("socket: %s", strerror(errno));
382 			continue;
383 		}
384 		if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
385 			fatal("F_SETFL: %s", strerror(errno));
386 		if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
387 		    errno != EINPROGRESS)
388 			error("connect (`%s'): %s", host, strerror(errno));
389 		else
390 			break;
391 		close(s);
392 		s = -1;
393 	}
394 	freeaddrinfo(aitop);
395 	return s;
396 }
397 
398 static int
399 conalloc(char *iname, char *oname, int keytype)
400 {
401 	char *namebase, *name, *namelist;
402 	int s;
403 
404 	namebase = namelist = xstrdup(iname);
405 
406 	do {
407 		name = xstrsep(&namelist, ",");
408 		if (!name) {
409 			xfree(namebase);
410 			return (-1);
411 		}
412 	} while ((s = tcpconnect(name)) < 0);
413 
414 	if (s >= maxfd)
415 		fatal("conalloc: fdno %d too high", s);
416 	if (fdcon[s].c_status)
417 		fatal("conalloc: attempt to reuse fdno %d", s);
418 
419 	fdcon[s].c_fd = s;
420 	fdcon[s].c_status = CS_CON;
421 	fdcon[s].c_namebase = namebase;
422 	fdcon[s].c_name = name;
423 	fdcon[s].c_namelist = namelist;
424 	fdcon[s].c_output_name = xstrdup(oname);
425 	fdcon[s].c_data = (char *) &fdcon[s].c_plen;
426 	fdcon[s].c_len = 4;
427 	fdcon[s].c_off = 0;
428 	fdcon[s].c_keytype = keytype;
429 	gettimeofday(&fdcon[s].c_tv, NULL);
430 	fdcon[s].c_tv.tv_sec += timeout;
431 	TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
432 	FD_SET(s, read_wait);
433 	ncon++;
434 	return (s);
435 }
436 
437 static void
438 confree(int s)
439 {
440 	if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
441 		fatal("confree: attempt to free bad fdno %d", s);
442 	close(s);
443 	xfree(fdcon[s].c_namebase);
444 	xfree(fdcon[s].c_output_name);
445 	if (fdcon[s].c_status == CS_KEYS)
446 		xfree(fdcon[s].c_data);
447 	fdcon[s].c_status = CS_UNUSED;
448 	fdcon[s].c_keytype = 0;
449 	TAILQ_REMOVE(&tq, &fdcon[s], c_link);
450 	FD_CLR(s, read_wait);
451 	ncon--;
452 }
453 
454 static void
455 contouch(int s)
456 {
457 	TAILQ_REMOVE(&tq, &fdcon[s], c_link);
458 	gettimeofday(&fdcon[s].c_tv, NULL);
459 	fdcon[s].c_tv.tv_sec += timeout;
460 	TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
461 }
462 
463 static int
464 conrecycle(int s)
465 {
466 	con *c = &fdcon[s];
467 	int ret;
468 
469 	ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
470 	confree(s);
471 	return (ret);
472 }
473 
474 static void
475 congreet(int s)
476 {
477 	int remote_major, remote_minor, n = 0;
478 	char buf[256], *cp;
479 	char remote_version[sizeof buf];
480 	size_t bufsiz;
481 	con *c = &fdcon[s];
482 
483 	bufsiz = sizeof(buf);
484 	cp = buf;
485 	while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n') {
486 		if (*cp == '\r')
487 			*cp = '\n';
488 		cp++;
489 	}
490 	if (n < 0) {
491 		if (errno != ECONNREFUSED)
492 			error("read (%s): %s", c->c_name, strerror(errno));
493 		conrecycle(s);
494 		return;
495 	}
496 	if (n == 0) {
497 		error("%s: Connection closed by remote host", c->c_name);
498 		conrecycle(s);
499 		return;
500 	}
501 	if (*cp != '\n' && *cp != '\r') {
502 		error("%s: bad greeting", c->c_name);
503 		confree(s);
504 		return;
505 	}
506 	*cp = '\0';
507 	if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
508 	    &remote_major, &remote_minor, remote_version) == 3)
509 		compat_datafellows(remote_version);
510 	else
511 		datafellows = 0;
512 	if (c->c_keytype != KT_RSA1) {
513 		if (!ssh2_capable(remote_major, remote_minor)) {
514 			debug("%s doesn't support ssh2", c->c_name);
515 			confree(s);
516 			return;
517 		}
518 	} else if (remote_major != 1) {
519 		debug("%s doesn't support ssh1", c->c_name);
520 		confree(s);
521 		return;
522 	}
523 	fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
524 	n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
525 	    c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
526 	    c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
527 	if (atomicio(write, s, buf, n) != n) {
528 		error("write (%s): %s", c->c_name, strerror(errno));
529 		confree(s);
530 		return;
531 	}
532 	if (c->c_keytype != KT_RSA1) {
533 		keyprint(c, keygrab_ssh2(c));
534 		confree(s);
535 		return;
536 	}
537 	c->c_status = CS_SIZE;
538 	contouch(s);
539 }
540 
541 static void
542 conread(int s)
543 {
544 	con *c = &fdcon[s];
545 	int n;
546 
547 	if (c->c_status == CS_CON) {
548 		congreet(s);
549 		return;
550 	}
551 	n = read(s, c->c_data + c->c_off, c->c_len - c->c_off);
552 	if (n < 0) {
553 		error("read (%s): %s", c->c_name, strerror(errno));
554 		confree(s);
555 		return;
556 	}
557 	c->c_off += n;
558 
559 	if (c->c_off == c->c_len)
560 		switch (c->c_status) {
561 		case CS_SIZE:
562 			c->c_plen = htonl(c->c_plen);
563 			c->c_len = c->c_plen + 8 - (c->c_plen & 7);
564 			c->c_off = 0;
565 			c->c_data = xmalloc(c->c_len);
566 			c->c_status = CS_KEYS;
567 			break;
568 		case CS_KEYS:
569 			keyprint(c, keygrab_ssh1(c));
570 			confree(s);
571 			return;
572 			break;
573 		default:
574 			fatal("conread: invalid status %d", c->c_status);
575 			break;
576 		}
577 
578 	contouch(s);
579 }
580 
581 static void
582 conloop(void)
583 {
584 	struct timeval seltime, now;
585 	fd_set *r, *e;
586 	con *c;
587 	int i;
588 
589 	gettimeofday(&now, NULL);
590 	c = TAILQ_FIRST(&tq);
591 
592 	if (c && (c->c_tv.tv_sec > now.tv_sec ||
593 	    (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
594 		seltime = c->c_tv;
595 		seltime.tv_sec -= now.tv_sec;
596 		seltime.tv_usec -= now.tv_usec;
597 		if (seltime.tv_usec < 0) {
598 			seltime.tv_usec += 1000000;
599 			seltime.tv_sec--;
600 		}
601 	} else
602 		seltime.tv_sec = seltime.tv_usec = 0;
603 
604 	r = xmalloc(read_wait_size);
605 	memcpy(r, read_wait, read_wait_size);
606 	e = xmalloc(read_wait_size);
607 	memcpy(e, read_wait, read_wait_size);
608 
609 	while (select(maxfd, r, NULL, e, &seltime) == -1 &&
610 	    (errno == EAGAIN || errno == EINTR))
611 		;
612 
613 	for (i = 0; i < maxfd; i++) {
614 		if (FD_ISSET(i, e)) {
615 			error("%s: exception!", fdcon[i].c_name);
616 			confree(i);
617 		} else if (FD_ISSET(i, r))
618 			conread(i);
619 	}
620 	xfree(r);
621 	xfree(e);
622 
623 	c = TAILQ_FIRST(&tq);
624 	while (c && (c->c_tv.tv_sec < now.tv_sec ||
625 	    (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
626 		int s = c->c_fd;
627 
628 		c = TAILQ_NEXT(c, c_link);
629 		conrecycle(s);
630 	}
631 }
632 
633 static void
634 do_host(char *host)
635 {
636 	char *name = strnnsep(&host, " \t\n");
637 	int j;
638 
639 	if (name == NULL)
640 		return;
641 	for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
642 		if (get_keytypes & j) {
643 			while (ncon >= MAXCON)
644 				conloop();
645 			conalloc(name, *host ? host : name, j);
646 		}
647 	}
648 }
649 
650 void
651 fatal(const char *fmt,...)
652 {
653 	va_list args;
654 
655 	va_start(args, fmt);
656 	do_log(SYSLOG_LEVEL_FATAL, fmt, args);
657 	va_end(args);
658 	if (nonfatal_fatal)
659 		longjmp(kexjmp, -1);
660 	else
661 		fatal_cleanup();
662 }
663 
664 static void
665 usage(void)
666 {
667 	fprintf(stderr, "usage: %s [-v46] [-p port] [-T timeout] [-f file]\n"
668 	    "\t\t   [host | addrlist namelist] [...]\n",
669 	    __progname);
670 	exit(1);
671 }
672 
673 int
674 main(int argc, char **argv)
675 {
676 	int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
677 	int opt, fopt_count = 0;
678 	char *tname;
679 
680 	extern int optind;
681 	extern char *optarg;
682 
683 	TAILQ_INIT(&tq);
684 
685 	if (argc <= 1)
686 		usage();
687 
688 	while ((opt = getopt(argc, argv, "v46p:T:t:f:")) != -1) {
689 		switch (opt) {
690 		case 'p':
691 			ssh_port = a2port(optarg);
692 			if (ssh_port == 0) {
693 				fprintf(stderr, "Bad port '%s'\n", optarg);
694 				exit(1);
695 			}
696 			break;
697 		case 'T':
698 			timeout = convtime(optarg);
699 			if (timeout == -1 || timeout == 0) {
700 				fprintf(stderr, "Bad timeout '%s'\n", optarg);
701 				usage();
702 			}
703 			break;
704 		case 'v':
705 			if (!debug_flag) {
706 				debug_flag = 1;
707 				log_level = SYSLOG_LEVEL_DEBUG1;
708 			}
709 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
710 				log_level++;
711 			else
712 				fatal("Too high debugging level.");
713 			break;
714 		case 'f':
715 			if (strcmp(optarg, "-") == 0)
716 				optarg = NULL;
717 			argv[fopt_count++] = optarg;
718 			break;
719 		case 't':
720 			get_keytypes = 0;
721 			tname = strtok(optarg, ",");
722 			while (tname) {
723 				int type = key_type_from_name(tname);
724 				switch (type) {
725 				case KEY_RSA1:
726 					get_keytypes |= KT_RSA1;
727 					break;
728 				case KEY_DSA:
729 					get_keytypes |= KT_DSA;
730 					break;
731 				case KEY_RSA:
732 					get_keytypes |= KT_RSA;
733 					break;
734 				case KEY_UNSPEC:
735 					fatal("unknown key type %s", tname);
736 				}
737 				tname = strtok(NULL, ",");
738 			}
739 			break;
740 		case '4':
741 			IPv4or6 = AF_INET;
742 			break;
743 		case '6':
744 			IPv4or6 = AF_INET6;
745 			break;
746 		case '?':
747 		default:
748 			usage();
749 		}
750 	}
751 	if (optind == argc && !fopt_count)
752 		usage();
753 
754 	log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
755 
756 	maxfd = fdlim_get(1);
757 	if (maxfd < 0)
758 		fatal("%s: fdlim_get: bad value", __progname);
759 	if (maxfd > MAXMAXFD)
760 		maxfd = MAXMAXFD;
761 	if (MAXCON <= 0)
762 		fatal("%s: not enough file descriptors", __progname);
763 	if (maxfd > fdlim_get(0))
764 		fdlim_set(maxfd);
765 	fdcon = xmalloc(maxfd * sizeof(con));
766 	memset(fdcon, 0, maxfd * sizeof(con));
767 
768 	read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
769 	read_wait = xmalloc(read_wait_size);
770 	memset(read_wait, 0, read_wait_size);
771 
772 	if (fopt_count) {
773 		Linebuf *lb;
774 		char *line;
775 		int j;
776 
777 		for (j = 0; j < fopt_count; j++) {
778 			lb = Linebuf_alloc(argv[j], error);
779 			if (!lb)
780 				continue;
781 			while ((line = Linebuf_getline(lb)) != NULL)
782 				do_host(line);
783 			Linebuf_free(lb);
784 		}
785 	}
786 
787 	while (optind < argc)
788 		do_host(argv[optind++]);
789 
790 	while (ncon > 0)
791 		conloop();
792 
793 	return (0);
794 }
795