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