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