xref: /openbsd-src/usr.bin/ssh/ssh-keyscan.c (revision a8396bba287763f3617961762e98baaf9a2528eb)
1 /* $OpenBSD: ssh-keyscan.c,v 1.158 2024/06/14 00:25:25 djm Exp $ */
2 /*
3  * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
4  *
5  * Modification and redistribution in source and binary forms is
6  * permitted provided that due credit is given to the author and the
7  * OpenBSD project by leaving this copyright notice intact.
8  */
9 
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/queue.h>
13 #include <sys/time.h>
14 #include <sys/resource.h>
15 
16 #ifdef WITH_OPENSSL
17 #include <openssl/bn.h>
18 #endif
19 
20 #include <errno.h>
21 #include <limits.h>
22 #include <netdb.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <poll.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <unistd.h>
30 
31 #include "xmalloc.h"
32 #include "ssh.h"
33 #include "sshbuf.h"
34 #include "sshkey.h"
35 #include "cipher.h"
36 #include "digest.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 #include "addr.h"
50 
51 /* Flag indicating whether IPv4 or IPv6.  This can be set on the command line.
52    Default value is AF_UNSPEC means both IPv4 and IPv6. */
53 int IPv4or6 = AF_UNSPEC;
54 
55 int ssh_port = SSH_DEFAULT_PORT;
56 
57 #define KT_DSA		(1)
58 #define KT_RSA		(1<<1)
59 #define KT_ECDSA	(1<<2)
60 #define KT_ED25519	(1<<3)
61 #define KT_XMSS		(1<<4)
62 #define KT_ECDSA_SK	(1<<5)
63 #define KT_ED25519_SK	(1<<6)
64 
65 #define KT_MIN		KT_DSA
66 #define KT_MAX		KT_ED25519_SK
67 
68 int get_cert = 0;
69 int get_keytypes = KT_RSA|KT_ECDSA|KT_ED25519|KT_ECDSA_SK|KT_ED25519_SK;
70 
71 int hash_hosts = 0;		/* Hash hostname on output */
72 
73 int print_sshfp = 0;		/* Print SSHFP records instead of known_hosts */
74 
75 int found_one = 0;		/* Successfully found a key */
76 
77 int hashalg = -1;		/* Hash for SSHFP records or -1 for all */
78 
79 int quiet = 0;			/* Don't print key comment lines */
80 
81 #define MAXMAXFD 256
82 
83 /* The number of seconds after which to give up on a TCP connection */
84 int timeout = 5;
85 
86 int maxfd;
87 #define MAXCON (maxfd - 10)
88 
89 extern char *__progname;
90 struct pollfd *read_wait;
91 int ncon;
92 
93 /*
94  * Keep a connection structure for each file descriptor.  The state
95  * associated with file descriptor n is held in fdcon[n].
96  */
97 typedef struct Connection {
98 	u_char c_status;	/* State of connection on this file desc. */
99 #define CS_UNUSED 0		/* File descriptor unused */
100 #define CS_CON 1		/* Waiting to connect/read greeting */
101 	int c_fd;		/* Quick lookup: c->c_fd == c - fdcon */
102 	int c_keytype;		/* Only one of KT_* */
103 	sig_atomic_t c_done;	/* SSH2 done */
104 	char *c_namebase;	/* Address to free for c_name and c_namelist */
105 	char *c_name;		/* Hostname of connection for errors */
106 	char *c_namelist;	/* Pointer to other possible addresses */
107 	char *c_output_name;	/* Hostname of connection for output */
108 	struct ssh *c_ssh;	/* SSH-connection */
109 	struct timespec c_ts;	/* 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) == -1)
124 		return (-1);
125 	if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY ||
126 	    (hard ? rlfd.rlim_max : rlfd.rlim_cur) > INT_MAX)
127 		return sysconf(_SC_OPEN_MAX);
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) == -1)
139 		return (-1);
140 	rlfd.rlim_cur = lim;
141 	if (setrlimit(RLIMIT_NOFILE, &rlfd) == -1)
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, 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 	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 		    "rsa-sha2-512-cert-v01@openssh.com,"
226 		    "rsa-sha2-256-cert-v01@openssh.com,"
227 		    "ssh-rsa-cert-v01@openssh.com" :
228 		    "rsa-sha2-512,"
229 		    "rsa-sha2-256,"
230 		    "ssh-rsa";
231 		break;
232 	case KT_ED25519:
233 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
234 		    "ssh-ed25519-cert-v01@openssh.com" : "ssh-ed25519";
235 		break;
236 	case KT_XMSS:
237 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
238 		    "ssh-xmss-cert-v01@openssh.com" : "ssh-xmss@openssh.com";
239 		break;
240 	case KT_ECDSA:
241 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
242 		    "ecdsa-sha2-nistp256-cert-v01@openssh.com,"
243 		    "ecdsa-sha2-nistp384-cert-v01@openssh.com,"
244 		    "ecdsa-sha2-nistp521-cert-v01@openssh.com" :
245 		    "ecdsa-sha2-nistp256,"
246 		    "ecdsa-sha2-nistp384,"
247 		    "ecdsa-sha2-nistp521";
248 		break;
249 	case KT_ECDSA_SK:
250 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
251 		    "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com" :
252 		    "sk-ecdsa-sha2-nistp256@openssh.com";
253 		break;
254 	case KT_ED25519_SK:
255 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
256 		    "sk-ssh-ed25519-cert-v01@openssh.com" :
257 		    "sk-ssh-ed25519@openssh.com";
258 		break;
259 	default:
260 		fatal("unknown key type %d", c->c_keytype);
261 		break;
262 	}
263 	if ((r = kex_setup(c->c_ssh, myproposal)) != 0) {
264 		free(c->c_ssh);
265 		fprintf(stderr, "kex_setup: %s\n", ssh_err(r));
266 		exit(1);
267 	}
268 #ifdef WITH_OPENSSL
269 	c->c_ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
270 	c->c_ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
271 	c->c_ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
272 	c->c_ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
273 	c->c_ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
274 	c->c_ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
275 	c->c_ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
276 	c->c_ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
277 #endif
278 	c->c_ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
279 	c->c_ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_client;
280 	ssh_set_verify_host_key_callback(c->c_ssh, key_print_wrapper);
281 	/*
282 	 * do the key-exchange until an error occurs or until
283 	 * the key_print_wrapper() callback sets c_done.
284 	 */
285 	ssh_dispatch_run(c->c_ssh, DISPATCH_BLOCK, &c->c_done);
286 }
287 
288 static void
289 keyprint_one(const char *host, struct sshkey *key)
290 {
291 	char *hostport = NULL, *hashed = NULL;
292 	const char *known_host;
293 	int r = 0;
294 
295 	found_one = 1;
296 
297 	if (print_sshfp) {
298 		export_dns_rr(host, key, stdout, 0, hashalg);
299 		return;
300 	}
301 
302 	hostport = put_host_port(host, ssh_port);
303 	lowercase(hostport);
304 	if (hash_hosts && (hashed = host_hash(hostport, NULL, 0)) == NULL)
305 		fatal("host_hash failed");
306 	known_host = hash_hosts ? hashed : hostport;
307 	if (!get_cert)
308 		r = fprintf(stdout, "%s ", known_host);
309 	if (r >= 0 && sshkey_write(key, stdout) == 0)
310 		(void)fputs("\n", stdout);
311 	free(hashed);
312 	free(hostport);
313 }
314 
315 static void
316 keyprint(con *c, struct sshkey *key)
317 {
318 	char *hosts = c->c_output_name ? c->c_output_name : c->c_name;
319 	char *host, *ohosts;
320 
321 	if (key == NULL)
322 		return;
323 	if (get_cert || (!hash_hosts && ssh_port == SSH_DEFAULT_PORT)) {
324 		keyprint_one(hosts, key);
325 		return;
326 	}
327 	ohosts = hosts = xstrdup(hosts);
328 	while ((host = strsep(&hosts, ",")) != NULL)
329 		keyprint_one(host, key);
330 	free(ohosts);
331 }
332 
333 static int
334 tcpconnect(char *host)
335 {
336 	struct addrinfo hints, *ai, *aitop;
337 	char strport[NI_MAXSERV];
338 	int gaierr, s = -1;
339 
340 	snprintf(strport, sizeof strport, "%d", ssh_port);
341 	memset(&hints, 0, sizeof(hints));
342 	hints.ai_family = IPv4or6;
343 	hints.ai_socktype = SOCK_STREAM;
344 	if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
345 		error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr));
346 		return -1;
347 	}
348 	for (ai = aitop; ai; ai = ai->ai_next) {
349 		s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
350 		if (s == -1) {
351 			error("socket: %s", strerror(errno));
352 			continue;
353 		}
354 		if (set_nonblock(s) == -1)
355 			fatal_f("set_nonblock(%d)", s);
356 		if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1 &&
357 		    errno != EINPROGRESS)
358 			error("connect (`%s'): %s", host, strerror(errno));
359 		else
360 			break;
361 		close(s);
362 		s = -1;
363 	}
364 	freeaddrinfo(aitop);
365 	return s;
366 }
367 
368 static int
369 conalloc(const char *iname, const char *oname, int keytype)
370 {
371 	char *namebase, *name, *namelist;
372 	int s;
373 
374 	namebase = namelist = xstrdup(iname);
375 
376 	do {
377 		name = xstrsep(&namelist, ",");
378 		if (!name) {
379 			free(namebase);
380 			return (-1);
381 		}
382 	} while ((s = tcpconnect(name)) < 0);
383 
384 	if (s >= maxfd)
385 		fatal("conalloc: fdno %d too high", s);
386 	if (fdcon[s].c_status)
387 		fatal("conalloc: attempt to reuse fdno %d", s);
388 
389 	debug3_f("oname %s kt %d", oname, keytype);
390 	fdcon[s].c_fd = s;
391 	fdcon[s].c_status = CS_CON;
392 	fdcon[s].c_namebase = namebase;
393 	fdcon[s].c_name = name;
394 	fdcon[s].c_namelist = namelist;
395 	fdcon[s].c_output_name = xstrdup(oname);
396 	fdcon[s].c_keytype = keytype;
397 	monotime_ts(&fdcon[s].c_ts);
398 	fdcon[s].c_ts.tv_sec += timeout;
399 	TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
400 	read_wait[s].fd = s;
401 	read_wait[s].events = POLLIN;
402 	ncon++;
403 	return (s);
404 }
405 
406 static void
407 confree(int s)
408 {
409 	if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
410 		fatal("confree: attempt to free bad fdno %d", s);
411 	free(fdcon[s].c_namebase);
412 	free(fdcon[s].c_output_name);
413 	fdcon[s].c_status = CS_UNUSED;
414 	fdcon[s].c_keytype = 0;
415 	if (fdcon[s].c_ssh) {
416 		ssh_packet_close(fdcon[s].c_ssh);
417 		free(fdcon[s].c_ssh);
418 		fdcon[s].c_ssh = NULL;
419 	} else
420 		close(s);
421 	TAILQ_REMOVE(&tq, &fdcon[s], c_link);
422 	read_wait[s].fd = -1;
423 	read_wait[s].events = 0;
424 	ncon--;
425 }
426 
427 static int
428 conrecycle(int s)
429 {
430 	con *c = &fdcon[s];
431 	int ret;
432 
433 	ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
434 	confree(s);
435 	return (ret);
436 }
437 
438 static void
439 congreet(int s)
440 {
441 	int n = 0, remote_major = 0, remote_minor = 0;
442 	char buf[256], *cp;
443 	char remote_version[sizeof buf];
444 	size_t bufsiz;
445 	con *c = &fdcon[s];
446 
447 	/* send client banner */
448 	n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
449 	    PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
450 	if (n < 0 || (size_t)n >= sizeof(buf)) {
451 		error("snprintf: buffer too small");
452 		confree(s);
453 		return;
454 	}
455 	if (atomicio(vwrite, s, buf, n) != (size_t)n) {
456 		error("write (%s): %s", c->c_name, strerror(errno));
457 		confree(s);
458 		return;
459 	}
460 
461 	/*
462 	 * Read the server banner as per RFC4253 section 4.2.  The "SSH-"
463 	 * protocol identification string may be preceded by an arbitrarily
464 	 * large banner which we must read and ignore.  Loop while reading
465 	 * newline-terminated lines until we have one starting with "SSH-".
466 	 * The ID string cannot be longer than 255 characters although the
467 	 * preceding banner lines may (in which case they'll be discarded
468 	 * in multiple iterations of the outer loop).
469 	 */
470 	for (;;) {
471 		memset(buf, '\0', sizeof(buf));
472 		bufsiz = sizeof(buf);
473 		cp = buf;
474 		while (bufsiz-- &&
475 		    (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
476 			if (*cp == '\r')
477 				*cp = '\n';
478 			cp++;
479 		}
480 		if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
481 			break;
482 	}
483 	if (n == 0) {
484 		switch (errno) {
485 		case EPIPE:
486 			error("%s: Connection closed by remote host", c->c_name);
487 			break;
488 		case ECONNREFUSED:
489 			break;
490 		default:
491 			error("read (%s): %s", c->c_name, strerror(errno));
492 			break;
493 		}
494 		conrecycle(s);
495 		return;
496 	}
497 	if (cp >= buf + sizeof(buf)) {
498 		error("%s: greeting exceeds allowable length", c->c_name);
499 		confree(s);
500 		return;
501 	}
502 	if (*cp != '\n' && *cp != '\r') {
503 		error("%s: bad greeting", c->c_name);
504 		confree(s);
505 		return;
506 	}
507 	*cp = '\0';
508 	if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL)
509 		fatal("ssh_packet_set_connection failed");
510 	ssh_packet_set_timeout(c->c_ssh, timeout, 1);
511 	ssh_set_app_data(c->c_ssh, c);	/* back link */
512 	c->c_ssh->compat = 0;
513 	if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
514 	    &remote_major, &remote_minor, remote_version) == 3)
515 		compat_banner(c->c_ssh, remote_version);
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 	if (!quiet) {
522 		fprintf(stdout, "%c %s:%d %s\n", print_sshfp ? ';' : '#',
523 		    c->c_name, ssh_port, chop(buf));
524 	}
525 	keygrab_ssh2(c);
526 	confree(s);
527 }
528 
529 static void
530 conread(int s)
531 {
532 	con *c = &fdcon[s];
533 
534 	if (c->c_status != CS_CON)
535 		fatal("conread: invalid status %d", c->c_status);
536 
537 	congreet(s);
538 }
539 
540 static void
541 conloop(void)
542 {
543 	struct timespec seltime, now;
544 	con *c;
545 	int i;
546 
547 	monotime_ts(&now);
548 	c = TAILQ_FIRST(&tq);
549 
550 	if (c && timespeccmp(&c->c_ts, &now, >))
551 		timespecsub(&c->c_ts, &now, &seltime);
552 	else
553 		timespecclear(&seltime);
554 
555 	while (ppoll(read_wait, maxfd, &seltime, NULL) == -1) {
556 		if (errno == EAGAIN || errno == EINTR)
557 			continue;
558 		error("poll error");
559 	}
560 
561 	for (i = 0; i < maxfd; i++) {
562 		if (read_wait[i].revents & (POLLHUP|POLLERR|POLLNVAL))
563 			confree(i);
564 		else if (read_wait[i].revents & (POLLIN|POLLHUP))
565 			conread(i);
566 	}
567 
568 	c = TAILQ_FIRST(&tq);
569 	while (c && timespeccmp(&c->c_ts, &now, <)) {
570 		int s = c->c_fd;
571 
572 		c = TAILQ_NEXT(c, c_link);
573 		conrecycle(s);
574 	}
575 }
576 
577 static void
578 do_one_host(char *host)
579 {
580 	char *name = strnnsep(&host, " \t\n");
581 	int j;
582 
583 	if (name == NULL)
584 		return;
585 	for (j = KT_MIN; j <= KT_MAX; j *= 2) {
586 		if (get_keytypes & j) {
587 			while (ncon >= MAXCON)
588 				conloop();
589 			conalloc(name, *host ? host : name, j);
590 		}
591 	}
592 }
593 
594 static void
595 do_host(char *host)
596 {
597 	char daddr[128];
598 	struct xaddr addr, end_addr;
599 	u_int masklen;
600 
601 	if (host == NULL)
602 		return;
603 	if (addr_pton_cidr(host, &addr, &masklen) != 0) {
604 		/* Assume argument is a hostname */
605 		do_one_host(host);
606 	} else {
607 		/* Argument is a CIDR range */
608 		debug("CIDR range %s", host);
609 		end_addr = addr;
610 		if (addr_host_to_all1s(&end_addr, masklen) != 0)
611 			goto badaddr;
612 		/*
613 		 * Note: we deliberately include the all-zero/ones addresses.
614 		 */
615 		for (;;) {
616 			if (addr_ntop(&addr, daddr, sizeof(daddr)) != 0) {
617  badaddr:
618 				error("Invalid address %s", host);
619 				return;
620 			}
621 			debug("CIDR expand: address %s", daddr);
622 			do_one_host(daddr);
623 			if (addr_cmp(&addr, &end_addr) == 0)
624 				break;
625 			addr_increment(&addr);
626 		};
627 	}
628 }
629 
630 void
631 sshfatal(const char *file, const char *func, int line, int showfunc,
632     LogLevel level, const char *suffix, const char *fmt, ...)
633 {
634 	va_list args;
635 
636 	va_start(args, fmt);
637 	sshlogv(file, func, line, showfunc, level, suffix, fmt, args);
638 	va_end(args);
639 	cleanup_exit(255);
640 }
641 
642 static void
643 usage(void)
644 {
645 	fprintf(stderr,
646 	    "usage: ssh-keyscan [-46cDHqv] [-f file] [-O option] [-p port] [-T timeout]\n"
647 	    "                   [-t type] [host | addrlist namelist]\n");
648 	exit(1);
649 }
650 
651 int
652 main(int argc, char **argv)
653 {
654 	int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
655 	int opt, fopt_count = 0, j;
656 	char *tname, *cp, *line = NULL;
657 	size_t linesize = 0;
658 	FILE *fp;
659 
660 	extern int optind;
661 	extern char *optarg;
662 
663 	TAILQ_INIT(&tq);
664 
665 	/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
666 	sanitise_stdfd();
667 
668 	if (argc <= 1)
669 		usage();
670 
671 	while ((opt = getopt(argc, argv, "cDHqv46O:p:T:t:f:")) != -1) {
672 		switch (opt) {
673 		case 'H':
674 			hash_hosts = 1;
675 			break;
676 		case 'c':
677 			get_cert = 1;
678 			break;
679 		case 'D':
680 			print_sshfp = 1;
681 			break;
682 		case 'p':
683 			ssh_port = a2port(optarg);
684 			if (ssh_port <= 0) {
685 				fprintf(stderr, "Bad port '%s'\n", optarg);
686 				exit(1);
687 			}
688 			break;
689 		case 'T':
690 			timeout = convtime(optarg);
691 			if (timeout == -1 || timeout == 0) {
692 				fprintf(stderr, "Bad timeout '%s'\n", optarg);
693 				usage();
694 			}
695 			break;
696 		case 'v':
697 			if (!debug_flag) {
698 				debug_flag = 1;
699 				log_level = SYSLOG_LEVEL_DEBUG1;
700 			}
701 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
702 				log_level++;
703 			else
704 				fatal("Too high debugging level.");
705 			break;
706 		case 'q':
707 			quiet = 1;
708 			break;
709 		case 'f':
710 			if (strcmp(optarg, "-") == 0)
711 				optarg = NULL;
712 			argv[fopt_count++] = optarg;
713 			break;
714 		case 'O':
715 			/* Maybe other misc options in the future too */
716 			if (strncmp(optarg, "hashalg=", 8) != 0)
717 				fatal("Unsupported -O option");
718 			if ((hashalg = ssh_digest_alg_by_name(
719 			    optarg + 8)) == -1)
720 				fatal("Unsupported hash algorithm");
721 			break;
722 		case 't':
723 			get_keytypes = 0;
724 			tname = strtok(optarg, ",");
725 			while (tname) {
726 				int type = sshkey_type_from_name(tname);
727 
728 				switch (type) {
729 #ifdef WITH_DSA
730 				case KEY_DSA:
731 					get_keytypes |= KT_DSA;
732 					break;
733 #endif
734 				case KEY_ECDSA:
735 					get_keytypes |= KT_ECDSA;
736 					break;
737 				case KEY_RSA:
738 					get_keytypes |= KT_RSA;
739 					break;
740 				case KEY_ED25519:
741 					get_keytypes |= KT_ED25519;
742 					break;
743 				case KEY_XMSS:
744 					get_keytypes |= KT_XMSS;
745 					break;
746 				case KEY_ED25519_SK:
747 					get_keytypes |= KT_ED25519_SK;
748 					break;
749 				case KEY_ECDSA_SK:
750 					get_keytypes |= KT_ECDSA_SK;
751 					break;
752 				case KEY_UNSPEC:
753 				default:
754 					fatal("Unknown key type \"%s\"", tname);
755 				}
756 				tname = strtok(NULL, ",");
757 			}
758 			break;
759 		case '4':
760 			IPv4or6 = AF_INET;
761 			break;
762 		case '6':
763 			IPv4or6 = AF_INET6;
764 			break;
765 		default:
766 			usage();
767 		}
768 	}
769 	if (optind == argc && !fopt_count)
770 		usage();
771 
772 	log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
773 
774 	maxfd = fdlim_get(1);
775 	if (maxfd < 0)
776 		fatal("%s: fdlim_get: bad value", __progname);
777 	if (maxfd > MAXMAXFD)
778 		maxfd = MAXMAXFD;
779 	if (MAXCON <= 0)
780 		fatal("%s: not enough file descriptors", __progname);
781 	if (maxfd > fdlim_get(0))
782 		fdlim_set(maxfd);
783 	fdcon = xcalloc(maxfd, sizeof(con));
784 	read_wait = xcalloc(maxfd, sizeof(struct pollfd));
785 	for (j = 0; j < maxfd; j++)
786 		read_wait[j].fd = -1;
787 
788 	for (j = 0; j < fopt_count; j++) {
789 		if (argv[j] == NULL)
790 			fp = stdin;
791 		else if ((fp = fopen(argv[j], "r")) == NULL)
792 			fatal("%s: %s: %s", __progname,
793 			    fp == stdin ? "<stdin>" : argv[j], strerror(errno));
794 
795 		while (getline(&line, &linesize, fp) != -1) {
796 			/* Chomp off trailing whitespace and comments */
797 			if ((cp = strchr(line, '#')) == NULL)
798 				cp = line + strlen(line) - 1;
799 			while (cp >= line) {
800 				if (*cp == ' ' || *cp == '\t' ||
801 				    *cp == '\n' || *cp == '#')
802 					*cp-- = '\0';
803 				else
804 					break;
805 			}
806 
807 			/* Skip empty lines */
808 			if (*line == '\0')
809 				continue;
810 
811 			do_host(line);
812 		}
813 
814 		if (ferror(fp))
815 			fatal("%s: %s: %s", __progname,
816 			    fp == stdin ? "<stdin>" : argv[j], strerror(errno));
817 
818 		if (fp != stdin)
819 			fclose(fp);
820 	}
821 	free(line);
822 
823 	while (optind < argc)
824 		do_host(argv[optind++]);
825 
826 	while (ncon > 0)
827 		conloop();
828 
829 	return found_one ? 0 : 1;
830 }
831