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