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