1 /* $OpenBSD: ssh-keyscan.c,v 1.126 2019/01/26 22:35:01 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 "includes.h" 11 12 #include <sys/types.h> 13 #include "openbsd-compat/sys-queue.h" 14 #include <sys/resource.h> 15 #ifdef HAVE_SYS_TIME_H 16 # include <sys/time.h> 17 #endif 18 19 #include <netinet/in.h> 20 #include <arpa/inet.h> 21 22 #include <openssl/bn.h> 23 24 #include <netdb.h> 25 #include <errno.h> 26 #include <stdarg.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <signal.h> 30 #include <string.h> 31 #include <unistd.h> 32 33 #include "xmalloc.h" 34 #include "ssh.h" 35 #include "sshbuf.h" 36 #include "sshkey.h" 37 #include "cipher.h" 38 #include "kex.h" 39 #include "compat.h" 40 #include "myproposal.h" 41 #include "packet.h" 42 #include "dispatch.h" 43 #include "log.h" 44 #include "atomicio.h" 45 #include "misc.h" 46 #include "hostfile.h" 47 #include "ssherr.h" 48 #include "ssh_api.h" 49 #include "dns.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 63 #define KT_MIN KT_DSA 64 #define KT_MAX KT_XMSS 65 66 int get_cert = 0; 67 int get_keytypes = KT_RSA|KT_ECDSA|KT_ED25519; 68 69 int hash_hosts = 0; /* Hash hostname on output */ 70 71 int print_sshfp = 0; /* Print SSHFP records instead of known_hosts */ 72 73 int found_one = 0; /* Successfully found a key */ 74 75 #define MAXMAXFD 256 76 77 /* The number of seconds after which to give up on a TCP connection */ 78 int timeout = 5; 79 80 int maxfd; 81 #define MAXCON (maxfd - 10) 82 83 extern char *__progname; 84 fd_set *read_wait; 85 size_t read_wait_nfdset; 86 int ncon; 87 88 /* 89 * Keep a connection structure for each file descriptor. The state 90 * associated with file descriptor n is held in fdcon[n]. 91 */ 92 typedef struct Connection { 93 u_char c_status; /* State of connection on this file desc. */ 94 #define CS_UNUSED 0 /* File descriptor unused */ 95 #define CS_CON 1 /* Waiting to connect/read greeting */ 96 #define CS_SIZE 2 /* Waiting to read initial packet size */ 97 #define CS_KEYS 3 /* Waiting to read public key packet */ 98 int c_fd; /* Quick lookup: c->c_fd == c - fdcon */ 99 int c_plen; /* Packet length field for ssh packet */ 100 int c_len; /* Total bytes which must be read. */ 101 int c_off; /* Length of data read so far. */ 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 char *c_data; /* Data read from this fd */ 109 struct ssh *c_ssh; /* SSH-connection */ 110 struct timeval c_tv; /* Time at which connection gets aborted */ 111 TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */ 112 } con; 113 114 TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */ 115 con *fdcon; 116 117 static void keyprint(con *c, struct sshkey *key); 118 119 static int 120 fdlim_get(int hard) 121 { 122 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) 123 struct rlimit rlfd; 124 125 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) 126 return (-1); 127 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY) 128 return SSH_SYSFDMAX; 129 else 130 return hard ? rlfd.rlim_max : rlfd.rlim_cur; 131 #else 132 return SSH_SYSFDMAX; 133 #endif 134 } 135 136 static int 137 fdlim_set(int lim) 138 { 139 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) 140 struct rlimit rlfd; 141 #endif 142 143 if (lim <= 0) 144 return (-1); 145 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) 146 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) 147 return (-1); 148 rlfd.rlim_cur = lim; 149 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0) 150 return (-1); 151 #elif defined (HAVE_SETDTABLESIZE) 152 setdtablesize(lim); 153 #endif 154 return (0); 155 } 156 157 /* 158 * This is an strsep function that returns a null field for adjacent 159 * separators. This is the same as the 4.4BSD strsep, but different from the 160 * one in the GNU libc. 161 */ 162 static char * 163 xstrsep(char **str, const char *delim) 164 { 165 char *s, *e; 166 167 if (!**str) 168 return (NULL); 169 170 s = *str; 171 e = s + strcspn(s, delim); 172 173 if (*e != '\0') 174 *e++ = '\0'; 175 *str = e; 176 177 return (s); 178 } 179 180 /* 181 * Get the next non-null token (like GNU strsep). Strsep() will return a 182 * null token for two adjacent separators, so we may have to loop. 183 */ 184 static char * 185 strnnsep(char **stringp, char *delim) 186 { 187 char *tok; 188 189 do { 190 tok = xstrsep(stringp, delim); 191 } while (tok && *tok == '\0'); 192 return (tok); 193 } 194 195 196 static int 197 key_print_wrapper(struct sshkey *hostkey, struct ssh *ssh) 198 { 199 con *c; 200 201 if ((c = ssh_get_app_data(ssh)) != NULL) 202 keyprint(c, hostkey); 203 /* always abort key exchange */ 204 return -1; 205 } 206 207 static int 208 ssh2_capable(int remote_major, int remote_minor) 209 { 210 switch (remote_major) { 211 case 1: 212 if (remote_minor == 99) 213 return 1; 214 break; 215 case 2: 216 return 1; 217 default: 218 break; 219 } 220 return 0; 221 } 222 223 static void 224 keygrab_ssh2(con *c) 225 { 226 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT }; 227 int r; 228 229 switch (c->c_keytype) { 230 case KT_DSA: 231 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ? 232 "ssh-dss-cert-v01@openssh.com" : "ssh-dss"; 233 break; 234 case KT_RSA: 235 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ? 236 "ssh-rsa-cert-v01@openssh.com" : "ssh-rsa"; 237 break; 238 case KT_ED25519: 239 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ? 240 "ssh-ed25519-cert-v01@openssh.com" : "ssh-ed25519"; 241 break; 242 case KT_XMSS: 243 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ? 244 "ssh-xmss-cert-v01@openssh.com" : "ssh-xmss@openssh.com"; 245 break; 246 case KT_ECDSA: 247 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ? 248 "ecdsa-sha2-nistp256-cert-v01@openssh.com," 249 "ecdsa-sha2-nistp384-cert-v01@openssh.com," 250 "ecdsa-sha2-nistp521-cert-v01@openssh.com" : 251 "ecdsa-sha2-nistp256," 252 "ecdsa-sha2-nistp384," 253 "ecdsa-sha2-nistp521"; 254 break; 255 default: 256 fatal("unknown key type %d", c->c_keytype); 257 break; 258 } 259 if ((r = kex_setup(c->c_ssh, myproposal)) != 0) { 260 free(c->c_ssh); 261 fprintf(stderr, "kex_setup: %s\n", ssh_err(r)); 262 exit(1); 263 } 264 #ifdef WITH_OPENSSL 265 c->c_ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client; 266 c->c_ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client; 267 c->c_ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client; 268 c->c_ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client; 269 c->c_ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client; 270 c->c_ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; 271 c->c_ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; 272 # ifdef OPENSSL_HAS_ECC 273 c->c_ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client; 274 # endif 275 #endif 276 c->c_ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client; 277 c->c_ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client; 278 ssh_set_verify_host_key_callback(c->c_ssh, key_print_wrapper); 279 /* 280 * do the key-exchange until an error occurs or until 281 * the key_print_wrapper() callback sets c_done. 282 */ 283 ssh_dispatch_run(c->c_ssh, DISPATCH_BLOCK, &c->c_done); 284 } 285 286 static void 287 keyprint_one(const char *host, struct sshkey *key) 288 { 289 char *hostport; 290 const char *known_host, *hashed; 291 292 found_one = 1; 293 294 if (print_sshfp) { 295 export_dns_rr(host, key, stdout, 0); 296 return; 297 } 298 299 hostport = put_host_port(host, ssh_port); 300 lowercase(hostport); 301 if (hash_hosts && (hashed = host_hash(host, NULL, 0)) == NULL) 302 fatal("host_hash failed"); 303 known_host = hash_hosts ? hashed : hostport; 304 if (!get_cert) 305 fprintf(stdout, "%s ", known_host); 306 sshkey_write(key, stdout); 307 fputs("\n", stdout); 308 free(hostport); 309 } 310 311 static void 312 keyprint(con *c, struct sshkey *key) 313 { 314 char *hosts = c->c_output_name ? c->c_output_name : c->c_name; 315 char *host, *ohosts; 316 317 if (key == NULL) 318 return; 319 if (get_cert || (!hash_hosts && ssh_port == SSH_DEFAULT_PORT)) { 320 keyprint_one(hosts, key); 321 return; 322 } 323 ohosts = hosts = xstrdup(hosts); 324 while ((host = strsep(&hosts, ",")) != NULL) 325 keyprint_one(host, key); 326 free(ohosts); 327 } 328 329 static int 330 tcpconnect(char *host) 331 { 332 struct addrinfo hints, *ai, *aitop; 333 char strport[NI_MAXSERV]; 334 int gaierr, s = -1; 335 336 snprintf(strport, sizeof strport, "%d", ssh_port); 337 memset(&hints, 0, sizeof(hints)); 338 hints.ai_family = IPv4or6; 339 hints.ai_socktype = SOCK_STREAM; 340 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) { 341 error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr)); 342 return -1; 343 } 344 for (ai = aitop; ai; ai = ai->ai_next) { 345 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 346 if (s < 0) { 347 error("socket: %s", strerror(errno)); 348 continue; 349 } 350 if (set_nonblock(s) == -1) 351 fatal("%s: set_nonblock(%d)", __func__, s); 352 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 && 353 errno != EINPROGRESS) 354 error("connect (`%s'): %s", host, strerror(errno)); 355 else 356 break; 357 close(s); 358 s = -1; 359 } 360 freeaddrinfo(aitop); 361 return s; 362 } 363 364 static int 365 conalloc(char *iname, char *oname, int keytype) 366 { 367 char *namebase, *name, *namelist; 368 int s; 369 370 namebase = namelist = xstrdup(iname); 371 372 do { 373 name = xstrsep(&namelist, ","); 374 if (!name) { 375 free(namebase); 376 return (-1); 377 } 378 } while ((s = tcpconnect(name)) < 0); 379 380 if (s >= maxfd) 381 fatal("conalloc: fdno %d too high", s); 382 if (fdcon[s].c_status) 383 fatal("conalloc: attempt to reuse fdno %d", s); 384 385 debug3("%s: oname %s kt %d", __func__, oname, keytype); 386 fdcon[s].c_fd = s; 387 fdcon[s].c_status = CS_CON; 388 fdcon[s].c_namebase = namebase; 389 fdcon[s].c_name = name; 390 fdcon[s].c_namelist = namelist; 391 fdcon[s].c_output_name = xstrdup(oname); 392 fdcon[s].c_data = (char *) &fdcon[s].c_plen; 393 fdcon[s].c_len = 4; 394 fdcon[s].c_off = 0; 395 fdcon[s].c_keytype = keytype; 396 monotime_tv(&fdcon[s].c_tv); 397 fdcon[s].c_tv.tv_sec += timeout; 398 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); 399 FD_SET(s, read_wait); 400 ncon++; 401 return (s); 402 } 403 404 static void 405 confree(int s) 406 { 407 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED) 408 fatal("confree: attempt to free bad fdno %d", s); 409 free(fdcon[s].c_namebase); 410 free(fdcon[s].c_output_name); 411 if (fdcon[s].c_status == CS_KEYS) 412 free(fdcon[s].c_data); 413 fdcon[s].c_status = CS_UNUSED; 414 fdcon[s].c_keytype = 0; 415 if (fdcon[s].c_ssh) { 416 ssh_packet_close(fdcon[s].c_ssh); 417 free(fdcon[s].c_ssh); 418 fdcon[s].c_ssh = NULL; 419 } else 420 close(s); 421 TAILQ_REMOVE(&tq, &fdcon[s], c_link); 422 FD_CLR(s, read_wait); 423 ncon--; 424 } 425 426 static void 427 contouch(int s) 428 { 429 TAILQ_REMOVE(&tq, &fdcon[s], c_link); 430 monotime_tv(&fdcon[s].c_tv); 431 fdcon[s].c_tv.tv_sec += timeout; 432 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); 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 for (;;) { 470 memset(buf, '\0', sizeof(buf)); 471 bufsiz = sizeof(buf); 472 cp = buf; 473 while (bufsiz-- && 474 (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') { 475 if (*cp == '\r') 476 *cp = '\n'; 477 cp++; 478 } 479 if (n != 1 || strncmp(buf, "SSH-", 4) == 0) 480 break; 481 } 482 if (n == 0) { 483 switch (errno) { 484 case EPIPE: 485 error("%s: Connection closed by remote host", c->c_name); 486 break; 487 case ECONNREFUSED: 488 break; 489 default: 490 error("read (%s): %s", c->c_name, strerror(errno)); 491 break; 492 } 493 conrecycle(s); 494 return; 495 } 496 if (*cp != '\n' && *cp != '\r') { 497 error("%s: bad greeting", c->c_name); 498 confree(s); 499 return; 500 } 501 *cp = '\0'; 502 if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL) 503 fatal("ssh_packet_set_connection failed"); 504 ssh_packet_set_timeout(c->c_ssh, timeout, 1); 505 ssh_set_app_data(c->c_ssh, c); /* back link */ 506 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", 507 &remote_major, &remote_minor, remote_version) == 3) 508 c->c_ssh->compat = compat_datafellows(remote_version); 509 else 510 c->c_ssh->compat = 0; 511 if (!ssh2_capable(remote_major, remote_minor)) { 512 debug("%s doesn't support ssh2", c->c_name); 513 confree(s); 514 return; 515 } 516 fprintf(stderr, "%c %s:%d %s\n", print_sshfp ? ';' : '#', 517 c->c_name, ssh_port, chop(buf)); 518 keygrab_ssh2(c); 519 confree(s); 520 } 521 522 static void 523 conread(int s) 524 { 525 con *c = &fdcon[s]; 526 size_t n; 527 528 if (c->c_status == CS_CON) { 529 congreet(s); 530 return; 531 } 532 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off); 533 if (n == 0) { 534 error("read (%s): %s", c->c_name, strerror(errno)); 535 confree(s); 536 return; 537 } 538 c->c_off += n; 539 540 if (c->c_off == c->c_len) 541 switch (c->c_status) { 542 case CS_SIZE: 543 c->c_plen = htonl(c->c_plen); 544 c->c_len = c->c_plen + 8 - (c->c_plen & 7); 545 c->c_off = 0; 546 c->c_data = xmalloc(c->c_len); 547 c->c_status = CS_KEYS; 548 break; 549 default: 550 fatal("conread: invalid status %d", c->c_status); 551 break; 552 } 553 554 contouch(s); 555 } 556 557 static void 558 conloop(void) 559 { 560 struct timeval seltime, now; 561 fd_set *r, *e; 562 con *c; 563 int i; 564 565 monotime_tv(&now); 566 c = TAILQ_FIRST(&tq); 567 568 if (c && (c->c_tv.tv_sec > now.tv_sec || 569 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) { 570 seltime = c->c_tv; 571 seltime.tv_sec -= now.tv_sec; 572 seltime.tv_usec -= now.tv_usec; 573 if (seltime.tv_usec < 0) { 574 seltime.tv_usec += 1000000; 575 seltime.tv_sec--; 576 } 577 } else 578 timerclear(&seltime); 579 580 r = xcalloc(read_wait_nfdset, sizeof(fd_mask)); 581 e = xcalloc(read_wait_nfdset, sizeof(fd_mask)); 582 memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask)); 583 memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask)); 584 585 while (select(maxfd, r, NULL, e, &seltime) == -1 && 586 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)) 587 ; 588 589 for (i = 0; i < maxfd; i++) { 590 if (FD_ISSET(i, e)) { 591 error("%s: exception!", fdcon[i].c_name); 592 confree(i); 593 } else if (FD_ISSET(i, r)) 594 conread(i); 595 } 596 free(r); 597 free(e); 598 599 c = TAILQ_FIRST(&tq); 600 while (c && (c->c_tv.tv_sec < now.tv_sec || 601 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) { 602 int s = c->c_fd; 603 604 c = TAILQ_NEXT(c, c_link); 605 conrecycle(s); 606 } 607 } 608 609 static void 610 do_host(char *host) 611 { 612 char *name = strnnsep(&host, " \t\n"); 613 int j; 614 615 if (name == NULL) 616 return; 617 for (j = KT_MIN; j <= KT_MAX; j *= 2) { 618 if (get_keytypes & j) { 619 while (ncon >= MAXCON) 620 conloop(); 621 conalloc(name, *host ? host : name, j); 622 } 623 } 624 } 625 626 void 627 fatal(const char *fmt,...) 628 { 629 va_list args; 630 631 va_start(args, fmt); 632 do_log(SYSLOG_LEVEL_FATAL, fmt, args); 633 va_end(args); 634 exit(255); 635 } 636 637 static void 638 usage(void) 639 { 640 fprintf(stderr, 641 "usage: %s [-46cDHv] [-f file] [-p port] [-T timeout] [-t type]\n" 642 "\t\t [host | addrlist namelist]\n", 643 __progname); 644 exit(1); 645 } 646 647 int 648 main(int argc, char **argv) 649 { 650 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; 651 int opt, fopt_count = 0, j; 652 char *tname, *cp, *line = NULL; 653 size_t linesize = 0; 654 FILE *fp; 655 656 extern int optind; 657 extern char *optarg; 658 659 ssh_malloc_init(); /* must be called before any mallocs */ 660 __progname = ssh_get_progname(argv[0]); 661 seed_rng(); 662 TAILQ_INIT(&tq); 663 664 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 665 sanitise_stdfd(); 666 667 if (argc <= 1) 668 usage(); 669 670 while ((opt = getopt(argc, argv, "cDHv46p:T:t:f:")) != -1) { 671 switch (opt) { 672 case 'H': 673 hash_hosts = 1; 674 break; 675 case 'c': 676 get_cert = 1; 677 break; 678 case 'D': 679 print_sshfp = 1; 680 break; 681 case 'p': 682 ssh_port = a2port(optarg); 683 if (ssh_port <= 0) { 684 fprintf(stderr, "Bad port '%s'\n", optarg); 685 exit(1); 686 } 687 break; 688 case 'T': 689 timeout = convtime(optarg); 690 if (timeout == -1 || timeout == 0) { 691 fprintf(stderr, "Bad timeout '%s'\n", optarg); 692 usage(); 693 } 694 break; 695 case 'v': 696 if (!debug_flag) { 697 debug_flag = 1; 698 log_level = SYSLOG_LEVEL_DEBUG1; 699 } 700 else if (log_level < SYSLOG_LEVEL_DEBUG3) 701 log_level++; 702 else 703 fatal("Too high debugging level."); 704 break; 705 case 'f': 706 if (strcmp(optarg, "-") == 0) 707 optarg = NULL; 708 argv[fopt_count++] = optarg; 709 break; 710 case 't': 711 get_keytypes = 0; 712 tname = strtok(optarg, ","); 713 while (tname) { 714 int type = sshkey_type_from_name(tname); 715 716 switch (type) { 717 case KEY_DSA: 718 get_keytypes |= KT_DSA; 719 break; 720 case KEY_ECDSA: 721 get_keytypes |= KT_ECDSA; 722 break; 723 case KEY_RSA: 724 get_keytypes |= KT_RSA; 725 break; 726 case KEY_ED25519: 727 get_keytypes |= KT_ED25519; 728 break; 729 case KEY_XMSS: 730 get_keytypes |= KT_XMSS; 731 break; 732 case KEY_UNSPEC: 733 default: 734 fatal("Unknown key type \"%s\"", tname); 735 } 736 tname = strtok(NULL, ","); 737 } 738 break; 739 case '4': 740 IPv4or6 = AF_INET; 741 break; 742 case '6': 743 IPv4or6 = AF_INET6; 744 break; 745 case '?': 746 default: 747 usage(); 748 } 749 } 750 if (optind == argc && !fopt_count) 751 usage(); 752 753 log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1); 754 755 maxfd = fdlim_get(1); 756 if (maxfd < 0) 757 fatal("%s: fdlim_get: bad value", __progname); 758 if (maxfd > MAXMAXFD) 759 maxfd = MAXMAXFD; 760 if (MAXCON <= 0) 761 fatal("%s: not enough file descriptors", __progname); 762 if (maxfd > fdlim_get(0)) 763 fdlim_set(maxfd); 764 fdcon = xcalloc(maxfd, sizeof(con)); 765 766 read_wait_nfdset = howmany(maxfd, NFDBITS); 767 read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask)); 768 769 for (j = 0; j < fopt_count; j++) { 770 if (argv[j] == NULL) 771 fp = stdin; 772 else if ((fp = fopen(argv[j], "r")) == NULL) 773 fatal("%s: %s: %s", __progname, argv[j], 774 strerror(errno)); 775 776 while (getline(&line, &linesize, fp) != -1) { 777 /* Chomp off trailing whitespace and comments */ 778 if ((cp = strchr(line, '#')) == NULL) 779 cp = line + strlen(line) - 1; 780 while (cp >= line) { 781 if (*cp == ' ' || *cp == '\t' || 782 *cp == '\n' || *cp == '#') 783 *cp-- = '\0'; 784 else 785 break; 786 } 787 788 /* Skip empty lines */ 789 if (*line == '\0') 790 continue; 791 792 do_host(line); 793 } 794 795 if (ferror(fp)) 796 fatal("%s: %s: %s", __progname, argv[j], 797 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