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