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