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