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