1 /* $NetBSD: ssh-keyscan.c,v 1.13 2015/04/13 17:54:52 christos Exp $ */ 2 /* $OpenBSD: ssh-keyscan.c,v 1.99 2015/01/30 10:44:49 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.13 2015/04/13 17:54:52 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 289 if (!key) 290 return; 291 if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL) 292 fatal("host_hash failed"); 293 294 fprintf(stdout, "%s ", host); 295 if ((r = sshkey_write(key, stdout)) != 0) 296 fprintf(stderr, "key_write failed: %s", ssh_err(r)); 297 298 fputs("\n", stdout); 299 } 300 301 static int 302 tcpconnect(char *host) 303 { 304 struct addrinfo hints, *ai, *aitop; 305 char strport[NI_MAXSERV]; 306 int gaierr, s = -1; 307 308 snprintf(strport, sizeof strport, "%d", ssh_port); 309 memset(&hints, 0, sizeof(hints)); 310 hints.ai_family = IPv4or6; 311 hints.ai_socktype = SOCK_STREAM; 312 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) { 313 error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr)); 314 return -1; 315 } 316 for (ai = aitop; ai; ai = ai->ai_next) { 317 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 318 if (s < 0) { 319 error("socket: %s", strerror(errno)); 320 continue; 321 } 322 if (set_nonblock(s) == -1) 323 fatal("%s: set_nonblock(%d)", __func__, s); 324 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 && 325 errno != EINPROGRESS) 326 error("connect (`%s'): %s", host, strerror(errno)); 327 else 328 break; 329 close(s); 330 s = -1; 331 } 332 freeaddrinfo(aitop); 333 return s; 334 } 335 336 static int 337 conalloc(char *iname, char *oname, int keytype) 338 { 339 char *namebase, *name, *namelist; 340 int s; 341 342 namebase = namelist = xstrdup(iname); 343 344 do { 345 name = xstrsep(&namelist, ","); 346 if (!name) { 347 free(namebase); 348 return (-1); 349 } 350 } while ((s = tcpconnect(name)) < 0); 351 352 if (s >= maxfd) 353 fatal("conalloc: fdno %d too high", s); 354 if (fdcon[s].c_status) 355 fatal("conalloc: attempt to reuse fdno %d", s); 356 357 fdcon[s].c_fd = s; 358 fdcon[s].c_status = CS_CON; 359 fdcon[s].c_namebase = namebase; 360 fdcon[s].c_name = name; 361 fdcon[s].c_namelist = namelist; 362 fdcon[s].c_output_name = xstrdup(oname); 363 fdcon[s].c_data = (char *) &fdcon[s].c_plen; 364 fdcon[s].c_len = 4; 365 fdcon[s].c_off = 0; 366 fdcon[s].c_keytype = keytype; 367 gettimeofday(&fdcon[s].c_tv, NULL); 368 fdcon[s].c_tv.tv_sec += timeout; 369 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); 370 FD_SET(s, read_wait); 371 ncon++; 372 return (s); 373 } 374 375 static void 376 confree(int s) 377 { 378 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED) 379 fatal("confree: attempt to free bad fdno %d", s); 380 close(s); 381 free(fdcon[s].c_namebase); 382 free(fdcon[s].c_output_name); 383 if (fdcon[s].c_status == CS_KEYS) 384 free(fdcon[s].c_data); 385 fdcon[s].c_status = CS_UNUSED; 386 fdcon[s].c_keytype = 0; 387 if (fdcon[s].c_ssh) { 388 ssh_packet_close(fdcon[s].c_ssh); 389 free(fdcon[s].c_ssh); 390 fdcon[s].c_ssh = NULL; 391 } 392 TAILQ_REMOVE(&tq, &fdcon[s], c_link); 393 FD_CLR(s, read_wait); 394 ncon--; 395 } 396 397 static void 398 contouch(int s) 399 { 400 TAILQ_REMOVE(&tq, &fdcon[s], c_link); 401 gettimeofday(&fdcon[s].c_tv, NULL); 402 fdcon[s].c_tv.tv_sec += timeout; 403 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); 404 } 405 406 static int 407 conrecycle(int s) 408 { 409 con *c = &fdcon[s]; 410 int ret; 411 412 ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype); 413 confree(s); 414 return (ret); 415 } 416 417 static void 418 congreet(int s) 419 { 420 int n = 0, remote_major = 0, remote_minor = 0; 421 char buf[256], *cp; 422 char remote_version[sizeof buf]; 423 size_t bufsiz; 424 con *c = &fdcon[s]; 425 426 for (;;) { 427 memset(buf, '\0', sizeof(buf)); 428 bufsiz = sizeof(buf); 429 cp = buf; 430 while (bufsiz-- && 431 (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') { 432 if (*cp == '\r') 433 *cp = '\n'; 434 cp++; 435 } 436 if (n != 1 || strncmp(buf, "SSH-", 4) == 0) 437 break; 438 } 439 if (n == 0) { 440 switch (errno) { 441 case EPIPE: 442 error("%s: Connection closed by remote host", c->c_name); 443 break; 444 case ECONNREFUSED: 445 break; 446 default: 447 error("read (%s): %s", c->c_name, strerror(errno)); 448 break; 449 } 450 conrecycle(s); 451 return; 452 } 453 if (*cp != '\n' && *cp != '\r') { 454 error("%s: bad greeting", c->c_name); 455 confree(s); 456 return; 457 } 458 *cp = '\0'; 459 if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL) 460 fatal("ssh_packet_set_connection failed"); 461 ssh_packet_set_timeout(c->c_ssh, timeout, 1); 462 ssh_set_app_data(c->c_ssh, c); /* back link */ 463 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", 464 &remote_major, &remote_minor, remote_version) == 3) 465 c->c_ssh->compat = compat_datafellows(remote_version); 466 else 467 c->c_ssh->compat = 0; 468 if (c->c_keytype != KT_RSA1) { 469 if (!ssh2_capable(remote_major, remote_minor)) { 470 debug("%s doesn't support ssh2", c->c_name); 471 confree(s); 472 return; 473 } 474 } else if (remote_major != 1) { 475 debug("%s doesn't support ssh1", c->c_name); 476 confree(s); 477 return; 478 } 479 fprintf(stderr, "# %s %s\n", c->c_name, chop(buf)); 480 n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n", 481 c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2, 482 c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2); 483 if (n < 0 || (size_t)n >= sizeof(buf)) { 484 error("snprintf: buffer too small"); 485 confree(s); 486 return; 487 } 488 if (atomicio(vwrite, s, buf, n) != (size_t)n) { 489 error("write (%s): %s", c->c_name, strerror(errno)); 490 confree(s); 491 return; 492 } 493 if (c->c_keytype != KT_RSA1) { 494 keygrab_ssh2(c); 495 confree(s); 496 return; 497 } 498 c->c_status = CS_SIZE; 499 contouch(s); 500 } 501 502 static void 503 conread(int s) 504 { 505 con *c = &fdcon[s]; 506 size_t n; 507 508 if (c->c_status == CS_CON) { 509 congreet(s); 510 return; 511 } 512 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off); 513 if (n == 0) { 514 error("read (%s): %s", c->c_name, strerror(errno)); 515 confree(s); 516 return; 517 } 518 c->c_off += n; 519 520 if (c->c_off == c->c_len) 521 switch (c->c_status) { 522 case CS_SIZE: 523 c->c_plen = htonl(c->c_plen); 524 c->c_len = c->c_plen + 8 - (c->c_plen & 7); 525 c->c_off = 0; 526 c->c_data = xmalloc(c->c_len); 527 c->c_status = CS_KEYS; 528 break; 529 #ifdef WITH_SSH1 530 case CS_KEYS: 531 keyprint(c, keygrab_ssh1(c)); 532 confree(s); 533 return; 534 #endif 535 default: 536 fatal("conread: invalid status %d", c->c_status); 537 break; 538 } 539 540 contouch(s); 541 } 542 543 static void 544 conloop(void) 545 { 546 struct timeval seltime, now; 547 fd_set *r, *e; 548 con *c; 549 int i; 550 551 gettimeofday(&now, NULL); 552 c = TAILQ_FIRST(&tq); 553 554 if (c && (c->c_tv.tv_sec > now.tv_sec || 555 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) { 556 seltime = c->c_tv; 557 seltime.tv_sec -= now.tv_sec; 558 seltime.tv_usec -= now.tv_usec; 559 if (seltime.tv_usec < 0) { 560 seltime.tv_usec += 1000000; 561 seltime.tv_sec--; 562 } 563 } else 564 timerclear(&seltime); 565 566 r = xcalloc(read_wait_nfdset, sizeof(fd_mask)); 567 e = xcalloc(read_wait_nfdset, sizeof(fd_mask)); 568 memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask)); 569 memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask)); 570 571 while (select(maxfd, r, NULL, e, &seltime) == -1 && 572 (errno == EAGAIN || errno == EINTR)) 573 ; 574 575 for (i = 0; i < maxfd; i++) { 576 if (FD_ISSET(i, e)) { 577 error("%s: exception!", fdcon[i].c_name); 578 confree(i); 579 } else if (FD_ISSET(i, r)) 580 conread(i); 581 } 582 free(r); 583 free(e); 584 585 c = TAILQ_FIRST(&tq); 586 while (c && (c->c_tv.tv_sec < now.tv_sec || 587 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) { 588 int s = c->c_fd; 589 590 c = TAILQ_NEXT(c, c_link); 591 conrecycle(s); 592 } 593 } 594 595 static void 596 do_host(char *host) 597 { 598 char *name = strnnsep(&host, " \t\n"); 599 int j; 600 601 if (name == NULL) 602 return; 603 for (j = KT_RSA1; j <= KT_ED25519; j *= 2) { 604 if (get_keytypes & j) { 605 while (ncon >= MAXCON) 606 conloop(); 607 conalloc(name, *host ? host : name, j); 608 } 609 } 610 } 611 612 __dead void 613 fatal(const char *fmt,...) 614 { 615 va_list args; 616 617 va_start(args, fmt); 618 do_log(SYSLOG_LEVEL_FATAL, fmt, args); 619 va_end(args); 620 exit(255); 621 } 622 623 __dead static void 624 usage(void) 625 { 626 fprintf(stderr, 627 "usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n" 628 "\t\t [host | addrlist namelist] ...\n", 629 __progname); 630 exit(1); 631 } 632 633 int 634 main(int argc, char **argv) 635 { 636 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; 637 int opt, fopt_count = 0, j; 638 char *tname, *cp, line[NI_MAXHOST]; 639 FILE *fp; 640 u_long linenum; 641 642 extern int optind; 643 extern char *optarg; 644 645 TAILQ_INIT(&tq); 646 647 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 648 sanitise_stdfd(); 649 650 if (argc <= 1) 651 usage(); 652 653 while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) { 654 switch (opt) { 655 case 'H': 656 hash_hosts = 1; 657 break; 658 case 'p': 659 ssh_port = a2port(optarg); 660 if (ssh_port <= 0) { 661 fprintf(stderr, "Bad port '%s'\n", optarg); 662 exit(1); 663 } 664 break; 665 case 'T': 666 timeout = convtime(optarg); 667 if (timeout == -1 || timeout == 0) { 668 fprintf(stderr, "Bad timeout '%s'\n", optarg); 669 usage(); 670 } 671 break; 672 case 'v': 673 if (!debug_flag) { 674 debug_flag = 1; 675 log_level = SYSLOG_LEVEL_DEBUG1; 676 } 677 else if (log_level < SYSLOG_LEVEL_DEBUG3) 678 log_level++; 679 else 680 fatal("Too high debugging level."); 681 break; 682 case 'f': 683 if (strcmp(optarg, "-") == 0) 684 optarg = NULL; 685 argv[fopt_count++] = optarg; 686 break; 687 case 't': 688 get_keytypes = 0; 689 tname = strtok(optarg, ","); 690 while (tname) { 691 int type = sshkey_type_from_name(tname); 692 switch (type) { 693 case KEY_RSA1: 694 get_keytypes |= KT_RSA1; 695 break; 696 case KEY_DSA: 697 get_keytypes |= KT_DSA; 698 break; 699 case KEY_ECDSA: 700 get_keytypes |= KT_ECDSA; 701 break; 702 case KEY_RSA: 703 get_keytypes |= KT_RSA; 704 break; 705 case KEY_ED25519: 706 get_keytypes |= KT_ED25519; 707 break; 708 case KEY_UNSPEC: 709 fatal("unknown key type %s", tname); 710 } 711 tname = strtok(NULL, ","); 712 } 713 break; 714 case '4': 715 IPv4or6 = AF_INET; 716 break; 717 case '6': 718 IPv4or6 = AF_INET6; 719 break; 720 case '?': 721 default: 722 usage(); 723 } 724 } 725 if (optind == argc && !fopt_count) 726 usage(); 727 728 log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1); 729 730 maxfd = fdlim_get(1); 731 if (maxfd < 0) 732 fatal("%s: fdlim_get: bad value", __progname); 733 if (maxfd > MAXMAXFD) 734 maxfd = MAXMAXFD; 735 if (MAXCON <= 0) 736 fatal("%s: not enough file descriptors", __progname); 737 if (maxfd > fdlim_get(0)) 738 fdlim_set(maxfd); 739 fdcon = xcalloc(maxfd, sizeof(con)); 740 741 read_wait_nfdset = howmany(maxfd, NFDBITS); 742 read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask)); 743 744 for (j = 0; j < fopt_count; j++) { 745 if (argv[j] == NULL) 746 fp = stdin; 747 else if ((fp = fopen(argv[j], "r")) == NULL) 748 fatal("%s: %s: %s", __progname, argv[j], 749 strerror(errno)); 750 linenum = 0; 751 752 while (read_keyfile_line(fp, 753 argv[j] == NULL ? "(stdin)" : argv[j], line, sizeof(line), 754 &linenum) != -1) { 755 /* Chomp off trailing whitespace and comments */ 756 if ((cp = strchr(line, '#')) == NULL) 757 cp = line + strlen(line) - 1; 758 while (cp >= line) { 759 if (*cp == ' ' || *cp == '\t' || 760 *cp == '\n' || *cp == '#') 761 *cp-- = '\0'; 762 else 763 break; 764 } 765 766 /* Skip empty lines */ 767 if (*line == '\0') 768 continue; 769 770 do_host(line); 771 } 772 773 if (ferror(fp)) 774 fatal("%s: %s: %s", __progname, argv[j], 775 strerror(errno)); 776 777 fclose(fp); 778 } 779 780 while (optind < argc) 781 do_host(argv[optind++]); 782 783 while (ncon > 0) 784 conloop(); 785 786 return (0); 787 } 788