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