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