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