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