1 /* $OpenBSD: ssh-keyscan.c,v 1.164 2024/10/18 05:32:51 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
fdlim_get(int hard)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
fdlim_set(int lim)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 *
xstrsep(char ** str,const char * delim)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 *
strnnsep(char ** stringp,char * delim)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
key_print_wrapper(struct sshkey * hostkey,struct ssh * ssh)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
ssh2_capable(int remote_major,int remote_minor)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
keygrab_ssh2(con * c)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 c->c_ssh->kex->kex[KEX_KEM_MLKEM768X25519_SHA256] = kex_gen_client;
281 ssh_set_verify_host_key_callback(c->c_ssh, key_print_wrapper);
282 /*
283 * do the key-exchange until an error occurs or until
284 * the key_print_wrapper() callback sets c_done.
285 */
286 ssh_dispatch_run(c->c_ssh, DISPATCH_BLOCK, &c->c_done);
287 }
288
289 static void
keyprint_one(const char * host,struct sshkey * key)290 keyprint_one(const char *host, struct sshkey *key)
291 {
292 char *hostport = NULL, *hashed = NULL;
293 const char *known_host;
294 int r = 0;
295
296 found_one = 1;
297
298 if (print_sshfp) {
299 export_dns_rr(host, key, stdout, 0, hashalg);
300 return;
301 }
302
303 hostport = put_host_port(host, ssh_port);
304 lowercase(hostport);
305 if (hash_hosts && (hashed = host_hash(hostport, NULL, 0)) == NULL)
306 fatal("host_hash failed");
307 known_host = hash_hosts ? hashed : hostport;
308 if (!get_cert)
309 r = fprintf(stdout, "%s ", known_host);
310 if (r >= 0 && sshkey_write(key, stdout) == 0)
311 (void)fputs("\n", stdout);
312 free(hashed);
313 free(hostport);
314 }
315
316 static void
keyprint(con * c,struct sshkey * key)317 keyprint(con *c, struct sshkey *key)
318 {
319 char *hosts = c->c_output_name ? c->c_output_name : c->c_name;
320 char *host, *ohosts;
321
322 if (key == NULL)
323 return;
324 if (get_cert || (!hash_hosts && ssh_port == SSH_DEFAULT_PORT)) {
325 keyprint_one(hosts, key);
326 return;
327 }
328 ohosts = hosts = xstrdup(hosts);
329 while ((host = strsep(&hosts, ",")) != NULL)
330 keyprint_one(host, key);
331 free(ohosts);
332 }
333
334 static int
tcpconnect(char * host)335 tcpconnect(char *host)
336 {
337 struct addrinfo hints, *ai, *aitop;
338 char strport[NI_MAXSERV];
339 int gaierr, s = -1;
340
341 snprintf(strport, sizeof strport, "%d", ssh_port);
342 memset(&hints, 0, sizeof(hints));
343 hints.ai_family = IPv4or6;
344 hints.ai_socktype = SOCK_STREAM;
345 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
346 error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr));
347 return -1;
348 }
349 for (ai = aitop; ai; ai = ai->ai_next) {
350 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
351 if (s == -1) {
352 error("socket: %s", strerror(errno));
353 continue;
354 }
355 if (set_nonblock(s) == -1)
356 fatal_f("set_nonblock(%d)", s);
357 if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1 &&
358 errno != EINPROGRESS)
359 error("connect (`%s'): %s", host, strerror(errno));
360 else
361 break;
362 close(s);
363 s = -1;
364 }
365 freeaddrinfo(aitop);
366 return s;
367 }
368
369 static int
conalloc(const char * iname,const char * oname,int keytype)370 conalloc(const char *iname, const char *oname, int keytype)
371 {
372 char *namebase, *name, *namelist;
373 int s;
374
375 namebase = namelist = xstrdup(iname);
376
377 do {
378 name = xstrsep(&namelist, ",");
379 if (!name) {
380 free(namebase);
381 return (-1);
382 }
383 } while ((s = tcpconnect(name)) < 0);
384
385 if (s >= maxfd)
386 fatal("conalloc: fdno %d too high", s);
387 if (fdcon[s].c_status)
388 fatal("conalloc: attempt to reuse fdno %d", s);
389
390 debug3_f("oname %s kt %d", oname, keytype);
391 fdcon[s].c_fd = s;
392 fdcon[s].c_status = CS_CON;
393 fdcon[s].c_namebase = namebase;
394 fdcon[s].c_name = name;
395 fdcon[s].c_namelist = namelist;
396 fdcon[s].c_output_name = xstrdup(oname);
397 fdcon[s].c_keytype = keytype;
398 monotime_ts(&fdcon[s].c_ts);
399 fdcon[s].c_ts.tv_sec += timeout;
400 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
401 read_wait[s].fd = s;
402 read_wait[s].events = POLLIN;
403 ncon++;
404 return (s);
405 }
406
407 static void
confree(int s)408 confree(int s)
409 {
410 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
411 fatal("confree: attempt to free bad fdno %d", s);
412 free(fdcon[s].c_namebase);
413 free(fdcon[s].c_output_name);
414 fdcon[s].c_status = CS_UNUSED;
415 fdcon[s].c_keytype = 0;
416 if (fdcon[s].c_ssh) {
417 ssh_packet_close(fdcon[s].c_ssh);
418 free(fdcon[s].c_ssh);
419 fdcon[s].c_ssh = NULL;
420 } else
421 close(s);
422 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
423 read_wait[s].fd = -1;
424 read_wait[s].events = 0;
425 ncon--;
426 }
427
428 static int
conrecycle(int s)429 conrecycle(int s)
430 {
431 con *c = &fdcon[s];
432 int ret;
433
434 ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
435 confree(s);
436 return (ret);
437 }
438
439 static void
congreet(int s)440 congreet(int s)
441 {
442 int n = 0, remote_major = 0, remote_minor = 0;
443 char buf[256], *cp;
444 char remote_version[sizeof buf];
445 size_t bufsiz;
446 con *c = &fdcon[s];
447
448 /* send client banner */
449 n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
450 PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
451 if (n < 0 || (size_t)n >= sizeof(buf)) {
452 error("snprintf: buffer too small");
453 confree(s);
454 return;
455 }
456 if (atomicio(vwrite, s, buf, n) != (size_t)n) {
457 error("write (%s): %s", c->c_name, strerror(errno));
458 confree(s);
459 return;
460 }
461
462 /*
463 * Read the server banner as per RFC4253 section 4.2. The "SSH-"
464 * protocol identification string may be preceded by an arbitrarily
465 * large banner which we must read and ignore. Loop while reading
466 * newline-terminated lines until we have one starting with "SSH-".
467 * The ID string cannot be longer than 255 characters although the
468 * preceding banner lines may (in which case they'll be discarded
469 * in multiple iterations of the outer loop).
470 */
471 for (;;) {
472 memset(buf, '\0', sizeof(buf));
473 bufsiz = sizeof(buf);
474 cp = buf;
475 while (bufsiz-- &&
476 (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
477 if (*cp == '\r')
478 *cp = '\n';
479 cp++;
480 }
481 if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
482 break;
483 }
484 if (n == 0) {
485 switch (errno) {
486 case EPIPE:
487 error("%s: Connection closed by remote host", c->c_name);
488 break;
489 case ECONNREFUSED:
490 break;
491 default:
492 error("read (%s): %s", c->c_name, strerror(errno));
493 break;
494 }
495 conrecycle(s);
496 return;
497 }
498 if (cp >= buf + sizeof(buf)) {
499 error("%s: greeting exceeds allowable length", c->c_name);
500 confree(s);
501 return;
502 }
503 if (*cp != '\n' && *cp != '\r') {
504 error("%s: bad greeting", c->c_name);
505 confree(s);
506 return;
507 }
508 *cp = '\0';
509 if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL)
510 fatal("ssh_packet_set_connection failed");
511 ssh_packet_set_timeout(c->c_ssh, timeout, 1);
512 ssh_set_app_data(c->c_ssh, c); /* back link */
513 c->c_ssh->compat = 0;
514 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
515 &remote_major, &remote_minor, remote_version) == 3)
516 compat_banner(c->c_ssh, remote_version);
517 if (!ssh2_capable(remote_major, remote_minor)) {
518 debug("%s doesn't support ssh2", c->c_name);
519 confree(s);
520 return;
521 }
522 if (!quiet) {
523 fprintf(stdout, "%c %s:%d %s\n", print_sshfp ? ';' : '#',
524 c->c_name, ssh_port, chop(buf));
525 }
526 keygrab_ssh2(c);
527 confree(s);
528 }
529
530 static void
conread(int s)531 conread(int s)
532 {
533 con *c = &fdcon[s];
534
535 if (c->c_status != CS_CON)
536 fatal("conread: invalid status %d", c->c_status);
537
538 congreet(s);
539 }
540
541 static void
conloop(void)542 conloop(void)
543 {
544 struct timespec seltime, now;
545 con *c;
546 int i;
547
548 monotime_ts(&now);
549 c = TAILQ_FIRST(&tq);
550
551 if (c && timespeccmp(&c->c_ts, &now, >))
552 timespecsub(&c->c_ts, &now, &seltime);
553 else
554 timespecclear(&seltime);
555
556 while (ppoll(read_wait, maxfd, &seltime, NULL) == -1) {
557 if (errno == EAGAIN || errno == EINTR)
558 continue;
559 error("poll error");
560 }
561
562 for (i = 0; i < maxfd; i++) {
563 if (read_wait[i].revents & (POLLHUP|POLLERR|POLLNVAL))
564 confree(i);
565 else if (read_wait[i].revents & (POLLIN))
566 conread(i);
567 }
568
569 c = TAILQ_FIRST(&tq);
570 while (c && timespeccmp(&c->c_ts, &now, <)) {
571 int s = c->c_fd;
572
573 c = TAILQ_NEXT(c, c_link);
574 conrecycle(s);
575 }
576 }
577
578 static void
do_one_host(char * host)579 do_one_host(char *host)
580 {
581 char *name = strnnsep(&host, " \t\n");
582 int j;
583
584 if (name == NULL)
585 return;
586 for (j = KT_MIN; j <= KT_MAX; j *= 2) {
587 if (get_keytypes & j) {
588 while (ncon >= MAXCON)
589 conloop();
590 conalloc(name, *host ? host : name, j);
591 }
592 }
593 }
594
595 static void
do_host(char * host)596 do_host(char *host)
597 {
598 char daddr[128];
599 struct xaddr addr, end_addr;
600 u_int masklen;
601
602 if (host == NULL)
603 return;
604 if (addr_pton_cidr(host, &addr, &masklen) != 0) {
605 /* Assume argument is a hostname */
606 do_one_host(host);
607 } else {
608 /* Argument is a CIDR range */
609 debug("CIDR range %s", host);
610 end_addr = addr;
611 if (addr_host_to_all1s(&end_addr, masklen) != 0)
612 goto badaddr;
613 /*
614 * Note: we deliberately include the all-zero/ones addresses.
615 */
616 for (;;) {
617 if (addr_ntop(&addr, daddr, sizeof(daddr)) != 0) {
618 badaddr:
619 error("Invalid address %s", host);
620 return;
621 }
622 debug("CIDR expand: address %s", daddr);
623 do_one_host(daddr);
624 if (addr_cmp(&addr, &end_addr) == 0)
625 break;
626 addr_increment(&addr);
627 }
628 }
629 }
630
631 static void
usage(void)632 usage(void)
633 {
634 fprintf(stderr,
635 "usage: ssh-keyscan [-46cDHqv] [-f file] [-O option] [-p port] [-T timeout]\n"
636 " [-t type] [host | addrlist namelist]\n");
637 exit(1);
638 }
639
640 int
main(int argc,char ** argv)641 main(int argc, char **argv)
642 {
643 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
644 int opt, fopt_count = 0, j;
645 char *tname, *cp, *line = NULL;
646 size_t linesize = 0;
647 FILE *fp;
648
649 extern int optind;
650 extern char *optarg;
651
652 TAILQ_INIT(&tq);
653
654 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
655 sanitise_stdfd();
656
657 if (argc <= 1)
658 usage();
659
660 while ((opt = getopt(argc, argv, "cDHqv46O:p:T:t:f:")) != -1) {
661 switch (opt) {
662 case 'H':
663 hash_hosts = 1;
664 break;
665 case 'c':
666 get_cert = 1;
667 break;
668 case 'D':
669 print_sshfp = 1;
670 break;
671 case 'p':
672 ssh_port = a2port(optarg);
673 if (ssh_port <= 0) {
674 fprintf(stderr, "Bad port '%s'\n", optarg);
675 exit(1);
676 }
677 break;
678 case 'T':
679 timeout = convtime(optarg);
680 if (timeout == -1 || timeout == 0) {
681 fprintf(stderr, "Bad timeout '%s'\n", optarg);
682 usage();
683 }
684 break;
685 case 'v':
686 if (!debug_flag) {
687 debug_flag = 1;
688 log_level = SYSLOG_LEVEL_DEBUG1;
689 }
690 else if (log_level < SYSLOG_LEVEL_DEBUG3)
691 log_level++;
692 else
693 fatal("Too high debugging level.");
694 break;
695 case 'q':
696 quiet = 1;
697 break;
698 case 'f':
699 if (strcmp(optarg, "-") == 0)
700 optarg = NULL;
701 argv[fopt_count++] = optarg;
702 break;
703 case 'O':
704 /* Maybe other misc options in the future too */
705 if (strncmp(optarg, "hashalg=", 8) != 0)
706 fatal("Unsupported -O option");
707 if ((hashalg = ssh_digest_alg_by_name(
708 optarg + 8)) == -1)
709 fatal("Unsupported hash algorithm");
710 break;
711 case 't':
712 get_keytypes = 0;
713 tname = strtok(optarg, ",");
714 while (tname) {
715 int type = sshkey_type_from_shortname(tname);
716
717 switch (type) {
718 #ifdef WITH_DSA
719 case KEY_DSA:
720 get_keytypes |= KT_DSA;
721 break;
722 #endif
723 case KEY_ECDSA:
724 get_keytypes |= KT_ECDSA;
725 break;
726 case KEY_RSA:
727 get_keytypes |= KT_RSA;
728 break;
729 case KEY_ED25519:
730 get_keytypes |= KT_ED25519;
731 break;
732 case KEY_XMSS:
733 get_keytypes |= KT_XMSS;
734 break;
735 case KEY_ED25519_SK:
736 get_keytypes |= KT_ED25519_SK;
737 break;
738 case KEY_ECDSA_SK:
739 get_keytypes |= KT_ECDSA_SK;
740 break;
741 case KEY_UNSPEC:
742 default:
743 fatal("Unknown key type \"%s\"", tname);
744 }
745 tname = strtok(NULL, ",");
746 }
747 break;
748 case '4':
749 IPv4or6 = AF_INET;
750 break;
751 case '6':
752 IPv4or6 = AF_INET6;
753 break;
754 default:
755 usage();
756 }
757 }
758 if (optind == argc && !fopt_count)
759 usage();
760
761 log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
762
763 maxfd = fdlim_get(1);
764 if (maxfd < 0)
765 fatal("%s: fdlim_get: bad value", __progname);
766 if (maxfd > MAXMAXFD)
767 maxfd = MAXMAXFD;
768 if (MAXCON <= 0)
769 fatal("%s: not enough file descriptors", __progname);
770 if (maxfd > fdlim_get(0))
771 fdlim_set(maxfd);
772 fdcon = xcalloc(maxfd, sizeof(con));
773 read_wait = xcalloc(maxfd, sizeof(struct pollfd));
774 for (j = 0; j < maxfd; j++)
775 read_wait[j].fd = -1;
776
777 for (j = 0; j < fopt_count; j++) {
778 if (argv[j] == NULL)
779 fp = stdin;
780 else if ((fp = fopen(argv[j], "r")) == NULL)
781 fatal("%s: %s: %s", __progname,
782 fp == stdin ? "<stdin>" : argv[j], strerror(errno));
783
784 while (getline(&line, &linesize, fp) != -1) {
785 /* Chomp off trailing whitespace and comments */
786 if ((cp = strchr(line, '#')) == NULL)
787 cp = line + strlen(line) - 1;
788 while (cp >= line) {
789 if (*cp == ' ' || *cp == '\t' ||
790 *cp == '\n' || *cp == '#')
791 *cp-- = '\0';
792 else
793 break;
794 }
795
796 /* Skip empty lines */
797 if (*line == '\0')
798 continue;
799
800 do_host(line);
801 }
802
803 if (ferror(fp))
804 fatal("%s: %s: %s", __progname,
805 fp == stdin ? "<stdin>" : argv[j], strerror(errno));
806
807 if (fp != stdin)
808 fclose(fp);
809 }
810 free(line);
811
812 while (optind < argc)
813 do_host(argv[optind++]);
814
815 while (ncon > 0)
816 conloop();
817
818 return found_one ? 0 : 1;
819 }
820