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