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