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