1 /* $OpenBSD: ssh-keyscan.c,v 1.127 2019/06/06 05:13:13 otto 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 "sshbuf.h" 30 #include "sshkey.h" 31 #include "cipher.h" 32 #include "kex.h" 33 #include "compat.h" 34 #include "myproposal.h" 35 #include "packet.h" 36 #include "dispatch.h" 37 #include "log.h" 38 #include "atomicio.h" 39 #include "misc.h" 40 #include "hostfile.h" 41 #include "ssherr.h" 42 #include "ssh_api.h" 43 #include "dns.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_DSA (1) 52 #define KT_RSA (1<<1) 53 #define KT_ECDSA (1<<2) 54 #define KT_ED25519 (1<<3) 55 #define KT_XMSS (1<<4) 56 57 #define KT_MIN KT_DSA 58 #define KT_MAX KT_XMSS 59 60 int get_cert = 0; 61 int get_keytypes = KT_RSA|KT_ECDSA|KT_ED25519; 62 63 int hash_hosts = 0; /* Hash hostname on output */ 64 65 int print_sshfp = 0; /* Print SSHFP records instead of known_hosts */ 66 67 int found_one = 0; /* Successfully found a key */ 68 69 #define MAXMAXFD 256 70 71 /* The number of seconds after which to give up on a TCP connection */ 72 int timeout = 5; 73 74 int maxfd; 75 #define MAXCON (maxfd - 10) 76 77 extern char *__progname; 78 fd_set *read_wait; 79 size_t read_wait_nfdset; 80 int ncon; 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 */ 86 typedef 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_* */ 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 108 TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */ 109 con *fdcon; 110 111 static void keyprint(con *c, struct sshkey *key); 112 113 static int 114 fdlim_get(int hard) 115 { 116 struct rlimit rlfd; 117 118 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) 119 return (-1); 120 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY) 121 return sysconf(_SC_OPEN_MAX); 122 else 123 return hard ? rlfd.rlim_max : rlfd.rlim_cur; 124 } 125 126 static int 127 fdlim_set(int lim) 128 { 129 struct rlimit rlfd; 130 131 if (lim <= 0) 132 return (-1); 133 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) 134 return (-1); 135 rlfd.rlim_cur = lim; 136 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0) 137 return (-1); 138 return (0); 139 } 140 141 /* 142 * This is an strsep function that returns a null field for adjacent 143 * separators. This is the same as the 4.4BSD strsep, but different from the 144 * one in the GNU libc. 145 */ 146 static char * 147 xstrsep(char **str, const char *delim) 148 { 149 char *s, *e; 150 151 if (!**str) 152 return (NULL); 153 154 s = *str; 155 e = s + strcspn(s, delim); 156 157 if (*e != '\0') 158 *e++ = '\0'; 159 *str = e; 160 161 return (s); 162 } 163 164 /* 165 * Get the next non-null token (like GNU strsep). Strsep() will return a 166 * null token for two adjacent separators, so we may have to loop. 167 */ 168 static char * 169 strnnsep(char **stringp, char *delim) 170 { 171 char *tok; 172 173 do { 174 tok = xstrsep(stringp, delim); 175 } while (tok && *tok == '\0'); 176 return (tok); 177 } 178 179 180 static int 181 key_print_wrapper(struct sshkey *hostkey, struct ssh *ssh) 182 { 183 con *c; 184 185 if ((c = ssh_get_app_data(ssh)) != NULL) 186 keyprint(c, hostkey); 187 /* always abort key exchange */ 188 return -1; 189 } 190 191 static int 192 ssh2_capable(int remote_major, int remote_minor) 193 { 194 switch (remote_major) { 195 case 1: 196 if (remote_minor == 99) 197 return 1; 198 break; 199 case 2: 200 return 1; 201 default: 202 break; 203 } 204 return 0; 205 } 206 207 static void 208 keygrab_ssh2(con *c) 209 { 210 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT }; 211 int r; 212 213 switch (c->c_keytype) { 214 case KT_DSA: 215 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ? 216 "ssh-dss-cert-v01@openssh.com" : "ssh-dss"; 217 break; 218 case KT_RSA: 219 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ? 220 "ssh-rsa-cert-v01@openssh.com" : "ssh-rsa"; 221 break; 222 case KT_ED25519: 223 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ? 224 "ssh-ed25519-cert-v01@openssh.com" : "ssh-ed25519"; 225 break; 226 case KT_XMSS: 227 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ? 228 "ssh-xmss-cert-v01@openssh.com" : "ssh-xmss@openssh.com"; 229 break; 230 case KT_ECDSA: 231 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ? 232 "ecdsa-sha2-nistp256-cert-v01@openssh.com," 233 "ecdsa-sha2-nistp384-cert-v01@openssh.com," 234 "ecdsa-sha2-nistp521-cert-v01@openssh.com" : 235 "ecdsa-sha2-nistp256," 236 "ecdsa-sha2-nistp384," 237 "ecdsa-sha2-nistp521"; 238 break; 239 default: 240 fatal("unknown key type %d", c->c_keytype); 241 break; 242 } 243 if ((r = kex_setup(c->c_ssh, myproposal)) != 0) { 244 free(c->c_ssh); 245 fprintf(stderr, "kex_setup: %s\n", ssh_err(r)); 246 exit(1); 247 } 248 #ifdef WITH_OPENSSL 249 c->c_ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client; 250 c->c_ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client; 251 c->c_ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client; 252 c->c_ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client; 253 c->c_ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client; 254 c->c_ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; 255 c->c_ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; 256 c->c_ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client; 257 #endif 258 c->c_ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client; 259 c->c_ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client; 260 ssh_set_verify_host_key_callback(c->c_ssh, key_print_wrapper); 261 /* 262 * do the key-exchange until an error occurs or until 263 * the key_print_wrapper() callback sets c_done. 264 */ 265 ssh_dispatch_run(c->c_ssh, DISPATCH_BLOCK, &c->c_done); 266 } 267 268 static void 269 keyprint_one(const char *host, struct sshkey *key) 270 { 271 char *hostport; 272 const char *known_host, *hashed; 273 274 found_one = 1; 275 276 if (print_sshfp) { 277 export_dns_rr(host, key, stdout, 0); 278 return; 279 } 280 281 hostport = put_host_port(host, ssh_port); 282 lowercase(hostport); 283 if (hash_hosts && (hashed = host_hash(host, NULL, 0)) == NULL) 284 fatal("host_hash failed"); 285 known_host = hash_hosts ? hashed : hostport; 286 if (!get_cert) 287 fprintf(stdout, "%s ", known_host); 288 sshkey_write(key, stdout); 289 fputs("\n", stdout); 290 free(hostport); 291 } 292 293 static void 294 keyprint(con *c, struct sshkey *key) 295 { 296 char *hosts = c->c_output_name ? c->c_output_name : c->c_name; 297 char *host, *ohosts; 298 299 if (key == NULL) 300 return; 301 if (get_cert || (!hash_hosts && ssh_port == SSH_DEFAULT_PORT)) { 302 keyprint_one(hosts, key); 303 return; 304 } 305 ohosts = hosts = xstrdup(hosts); 306 while ((host = strsep(&hosts, ",")) != NULL) 307 keyprint_one(host, key); 308 free(ohosts); 309 } 310 311 static int 312 tcpconnect(char *host) 313 { 314 struct addrinfo hints, *ai, *aitop; 315 char strport[NI_MAXSERV]; 316 int gaierr, s = -1; 317 318 snprintf(strport, sizeof strport, "%d", ssh_port); 319 memset(&hints, 0, sizeof(hints)); 320 hints.ai_family = IPv4or6; 321 hints.ai_socktype = SOCK_STREAM; 322 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) { 323 error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr)); 324 return -1; 325 } 326 for (ai = aitop; ai; ai = ai->ai_next) { 327 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 328 if (s < 0) { 329 error("socket: %s", strerror(errno)); 330 continue; 331 } 332 if (set_nonblock(s) == -1) 333 fatal("%s: set_nonblock(%d)", __func__, s); 334 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 && 335 errno != EINPROGRESS) 336 error("connect (`%s'): %s", host, strerror(errno)); 337 else 338 break; 339 close(s); 340 s = -1; 341 } 342 freeaddrinfo(aitop); 343 return s; 344 } 345 346 static int 347 conalloc(char *iname, char *oname, int keytype) 348 { 349 char *namebase, *name, *namelist; 350 int s; 351 352 namebase = namelist = xstrdup(iname); 353 354 do { 355 name = xstrsep(&namelist, ","); 356 if (!name) { 357 free(namebase); 358 return (-1); 359 } 360 } while ((s = tcpconnect(name)) < 0); 361 362 if (s >= maxfd) 363 fatal("conalloc: fdno %d too high", s); 364 if (fdcon[s].c_status) 365 fatal("conalloc: attempt to reuse fdno %d", s); 366 367 debug3("%s: oname %s kt %d", __func__, oname, keytype); 368 fdcon[s].c_fd = s; 369 fdcon[s].c_status = CS_CON; 370 fdcon[s].c_namebase = namebase; 371 fdcon[s].c_name = name; 372 fdcon[s].c_namelist = namelist; 373 fdcon[s].c_output_name = xstrdup(oname); 374 fdcon[s].c_data = (char *) &fdcon[s].c_plen; 375 fdcon[s].c_len = 4; 376 fdcon[s].c_off = 0; 377 fdcon[s].c_keytype = keytype; 378 monotime_tv(&fdcon[s].c_tv); 379 fdcon[s].c_tv.tv_sec += timeout; 380 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); 381 FD_SET(s, read_wait); 382 ncon++; 383 return (s); 384 } 385 386 static void 387 confree(int s) 388 { 389 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED) 390 fatal("confree: attempt to free bad fdno %d", s); 391 free(fdcon[s].c_namebase); 392 free(fdcon[s].c_output_name); 393 if (fdcon[s].c_status == CS_KEYS) 394 free(fdcon[s].c_data); 395 fdcon[s].c_status = CS_UNUSED; 396 fdcon[s].c_keytype = 0; 397 if (fdcon[s].c_ssh) { 398 ssh_packet_close(fdcon[s].c_ssh); 399 free(fdcon[s].c_ssh); 400 fdcon[s].c_ssh = NULL; 401 } else 402 close(s); 403 TAILQ_REMOVE(&tq, &fdcon[s], c_link); 404 FD_CLR(s, read_wait); 405 ncon--; 406 } 407 408 static void 409 contouch(int s) 410 { 411 TAILQ_REMOVE(&tq, &fdcon[s], c_link); 412 monotime_tv(&fdcon[s].c_tv); 413 fdcon[s].c_tv.tv_sec += timeout; 414 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); 415 } 416 417 static int 418 conrecycle(int s) 419 { 420 con *c = &fdcon[s]; 421 int ret; 422 423 ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype); 424 confree(s); 425 return (ret); 426 } 427 428 static void 429 congreet(int s) 430 { 431 int n = 0, remote_major = 0, remote_minor = 0; 432 char buf[256], *cp; 433 char remote_version[sizeof buf]; 434 size_t bufsiz; 435 con *c = &fdcon[s]; 436 437 /* send client banner */ 438 n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n", 439 PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2); 440 if (n < 0 || (size_t)n >= sizeof(buf)) { 441 error("snprintf: buffer too small"); 442 confree(s); 443 return; 444 } 445 if (atomicio(vwrite, s, buf, n) != (size_t)n) { 446 error("write (%s): %s", c->c_name, strerror(errno)); 447 confree(s); 448 return; 449 } 450 451 for (;;) { 452 memset(buf, '\0', sizeof(buf)); 453 bufsiz = sizeof(buf); 454 cp = buf; 455 while (bufsiz-- && 456 (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') { 457 if (*cp == '\r') 458 *cp = '\n'; 459 cp++; 460 } 461 if (n != 1 || strncmp(buf, "SSH-", 4) == 0) 462 break; 463 } 464 if (n == 0) { 465 switch (errno) { 466 case EPIPE: 467 error("%s: Connection closed by remote host", c->c_name); 468 break; 469 case ECONNREFUSED: 470 break; 471 default: 472 error("read (%s): %s", c->c_name, strerror(errno)); 473 break; 474 } 475 conrecycle(s); 476 return; 477 } 478 if (*cp != '\n' && *cp != '\r') { 479 error("%s: bad greeting", c->c_name); 480 confree(s); 481 return; 482 } 483 *cp = '\0'; 484 if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL) 485 fatal("ssh_packet_set_connection failed"); 486 ssh_packet_set_timeout(c->c_ssh, timeout, 1); 487 ssh_set_app_data(c->c_ssh, c); /* back link */ 488 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", 489 &remote_major, &remote_minor, remote_version) == 3) 490 c->c_ssh->compat = compat_datafellows(remote_version); 491 else 492 c->c_ssh->compat = 0; 493 if (!ssh2_capable(remote_major, remote_minor)) { 494 debug("%s doesn't support ssh2", c->c_name); 495 confree(s); 496 return; 497 } 498 fprintf(stderr, "%c %s:%d %s\n", print_sshfp ? ';' : '#', 499 c->c_name, ssh_port, chop(buf)); 500 keygrab_ssh2(c); 501 confree(s); 502 } 503 504 static void 505 conread(int s) 506 { 507 con *c = &fdcon[s]; 508 size_t n; 509 510 if (c->c_status == CS_CON) { 511 congreet(s); 512 return; 513 } 514 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off); 515 if (n == 0) { 516 error("read (%s): %s", c->c_name, strerror(errno)); 517 confree(s); 518 return; 519 } 520 c->c_off += n; 521 522 if (c->c_off == c->c_len) 523 switch (c->c_status) { 524 case CS_SIZE: 525 c->c_plen = htonl(c->c_plen); 526 c->c_len = c->c_plen + 8 - (c->c_plen & 7); 527 c->c_off = 0; 528 c->c_data = xmalloc(c->c_len); 529 c->c_status = CS_KEYS; 530 break; 531 default: 532 fatal("conread: invalid status %d", c->c_status); 533 break; 534 } 535 536 contouch(s); 537 } 538 539 static void 540 conloop(void) 541 { 542 struct timeval seltime, now; 543 fd_set *r, *e; 544 con *c; 545 int i; 546 547 monotime_tv(&now); 548 c = TAILQ_FIRST(&tq); 549 550 if (c && (c->c_tv.tv_sec > now.tv_sec || 551 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) { 552 seltime = c->c_tv; 553 seltime.tv_sec -= now.tv_sec; 554 seltime.tv_usec -= now.tv_usec; 555 if (seltime.tv_usec < 0) { 556 seltime.tv_usec += 1000000; 557 seltime.tv_sec--; 558 } 559 } else 560 timerclear(&seltime); 561 562 r = xcalloc(read_wait_nfdset, sizeof(fd_mask)); 563 e = xcalloc(read_wait_nfdset, sizeof(fd_mask)); 564 memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask)); 565 memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask)); 566 567 while (select(maxfd, r, NULL, e, &seltime) == -1 && 568 (errno == EAGAIN || errno == EINTR)) 569 ; 570 571 for (i = 0; i < maxfd; i++) { 572 if (FD_ISSET(i, e)) { 573 error("%s: exception!", fdcon[i].c_name); 574 confree(i); 575 } else if (FD_ISSET(i, r)) 576 conread(i); 577 } 578 free(r); 579 free(e); 580 581 c = TAILQ_FIRST(&tq); 582 while (c && (c->c_tv.tv_sec < now.tv_sec || 583 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) { 584 int s = c->c_fd; 585 586 c = TAILQ_NEXT(c, c_link); 587 conrecycle(s); 588 } 589 } 590 591 static void 592 do_host(char *host) 593 { 594 char *name = strnnsep(&host, " \t\n"); 595 int j; 596 597 if (name == NULL) 598 return; 599 for (j = KT_MIN; j <= KT_MAX; j *= 2) { 600 if (get_keytypes & j) { 601 while (ncon >= MAXCON) 602 conloop(); 603 conalloc(name, *host ? host : name, j); 604 } 605 } 606 } 607 608 void 609 fatal(const char *fmt,...) 610 { 611 va_list args; 612 613 va_start(args, fmt); 614 do_log(SYSLOG_LEVEL_FATAL, fmt, args); 615 va_end(args); 616 exit(255); 617 } 618 619 static void 620 usage(void) 621 { 622 fprintf(stderr, 623 "usage: %s [-46cDHv] [-f file] [-p port] [-T timeout] [-t type]\n" 624 "\t\t [host | addrlist namelist]\n", 625 __progname); 626 exit(1); 627 } 628 629 int 630 main(int argc, char **argv) 631 { 632 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; 633 int opt, fopt_count = 0, j; 634 char *tname, *cp, *line = NULL; 635 size_t linesize = 0; 636 FILE *fp; 637 638 extern int optind; 639 extern char *optarg; 640 641 TAILQ_INIT(&tq); 642 643 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 644 sanitise_stdfd(); 645 646 if (argc <= 1) 647 usage(); 648 649 while ((opt = getopt(argc, argv, "cDHv46p:T:t:f:")) != -1) { 650 switch (opt) { 651 case 'H': 652 hash_hosts = 1; 653 break; 654 case 'c': 655 get_cert = 1; 656 break; 657 case 'D': 658 print_sshfp = 1; 659 break; 660 case 'p': 661 ssh_port = a2port(optarg); 662 if (ssh_port <= 0) { 663 fprintf(stderr, "Bad port '%s'\n", optarg); 664 exit(1); 665 } 666 break; 667 case 'T': 668 timeout = convtime(optarg); 669 if (timeout == -1 || timeout == 0) { 670 fprintf(stderr, "Bad timeout '%s'\n", optarg); 671 usage(); 672 } 673 break; 674 case 'v': 675 if (!debug_flag) { 676 debug_flag = 1; 677 log_level = SYSLOG_LEVEL_DEBUG1; 678 } 679 else if (log_level < SYSLOG_LEVEL_DEBUG3) 680 log_level++; 681 else 682 fatal("Too high debugging level."); 683 break; 684 case 'f': 685 if (strcmp(optarg, "-") == 0) 686 optarg = NULL; 687 argv[fopt_count++] = optarg; 688 break; 689 case 't': 690 get_keytypes = 0; 691 tname = strtok(optarg, ","); 692 while (tname) { 693 int type = sshkey_type_from_name(tname); 694 695 switch (type) { 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_XMSS: 709 get_keytypes |= KT_XMSS; 710 break; 711 case KEY_UNSPEC: 712 default: 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 755 while (getline(&line, &linesize, fp) != -1) { 756 /* Chomp off trailing whitespace and comments */ 757 if ((cp = strchr(line, '#')) == NULL) 758 cp = line + strlen(line) - 1; 759 while (cp >= line) { 760 if (*cp == ' ' || *cp == '\t' || 761 *cp == '\n' || *cp == '#') 762 *cp-- = '\0'; 763 else 764 break; 765 } 766 767 /* Skip empty lines */ 768 if (*line == '\0') 769 continue; 770 771 do_host(line); 772 } 773 774 if (ferror(fp)) 775 fatal("%s: %s: %s", __progname, argv[j], 776 strerror(errno)); 777 778 fclose(fp); 779 } 780 free(line); 781 782 while (optind < argc) 783 do_host(argv[optind++]); 784 785 while (ncon > 0) 786 conloop(); 787 788 return found_one ? 0 : 1; 789 } 790