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