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