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