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