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