1 /* 2 * Copyright (c) 1983, 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1983, 1991, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)from: inetd.c 8.4 (Berkeley) 4/13/94"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 46 #endif /* not lint */ 47 48 /* 49 * Inetd - Internet super-server 50 * 51 * This program invokes all internet services as needed. Connection-oriented 52 * services are invoked each time a connection is made, by creating a process. 53 * This process is passed the connection as file descriptor 0 and is expected 54 * to do a getpeername to find out the source host and port. 55 * 56 * Datagram oriented services are invoked when a datagram 57 * arrives; a process is created and passed a pending message 58 * on file descriptor 0. Datagram servers may either connect 59 * to their peer, freeing up the original socket for inetd 60 * to receive further messages on, or ``take over the socket'', 61 * processing all arriving datagrams and, eventually, timing 62 * out. The first type of server is said to be ``multi-threaded''; 63 * the second type of server ``single-threaded''. 64 * 65 * Inetd uses a configuration file which is read at startup 66 * and, possibly, at some later time in response to a hangup signal. 67 * The configuration file is ``free format'' with fields given in the 68 * order shown below. Continuation lines for an entry must being with 69 * a space or tab. All fields must be present in each entry. 70 * 71 * service name must be in /etc/services or must 72 * name a tcpmux service 73 * socket type stream/dgram/raw/rdm/seqpacket 74 * protocol must be in /etc/protocols 75 * wait/nowait single-threaded/multi-threaded 76 * user user to run daemon as 77 * server program full path name 78 * server program arguments maximum of MAXARGS (20) 79 * 80 * TCP services without official port numbers are handled with the 81 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for 82 * requests. When a connection is made from a foreign host, the service 83 * requested is passed to tcpmux, which looks it up in the servtab list 84 * and returns the proper entry for the service. Tcpmux returns a 85 * negative reply if the service doesn't exist, otherwise the invoked 86 * server is expected to return the positive reply if the service type in 87 * inetd.conf file has the prefix "tcpmux/". If the service type has the 88 * prefix "tcpmux/+", tcpmux will return the positive reply for the 89 * process; this is for compatibility with older server code, and also 90 * allows you to invoke programs that use stdin/stdout without putting any 91 * special server code in them. Services that use tcpmux are "nowait" 92 * because they do not have a well-known port and hence cannot listen 93 * for new requests. 94 * 95 * For RPC services 96 * service name/version must be in /etc/rpc 97 * socket type stream/dgram/raw/rdm/seqpacket 98 * protocol must be in /etc/protocols 99 * wait/nowait single-threaded/multi-threaded 100 * user user to run daemon as 101 * server program full path name 102 * server program arguments maximum of MAXARGS 103 * 104 * Comment lines are indicated by a `#' in column 1. 105 * 106 * #ifdef IPSEC 107 * Comment lines that start with "#@" denote IPsec policy string, as described 108 * in ipsec_set_policy(3). This will affect all the following items in 109 * inetd.conf(8). To reset the policy, just use "#@" line. By default, 110 * there's no IPsec policy. 111 * #endif 112 */ 113 #include <sys/param.h> 114 #include <sys/ioctl.h> 115 #include <sys/wait.h> 116 #include <sys/time.h> 117 #include <sys/resource.h> 118 119 #include <netinet/in.h> 120 #include <netinet/tcp.h> 121 #include <arpa/inet.h> 122 #include <rpc/rpc.h> 123 #include <rpc/pmap_clnt.h> 124 125 #include <errno.h> 126 #include <err.h> 127 #include <fcntl.h> 128 #include <grp.h> 129 #include <netdb.h> 130 #include <pwd.h> 131 #include <signal.h> 132 #include <stdio.h> 133 #include <stdlib.h> 134 #include <string.h> 135 #include <syslog.h> 136 #include <tcpd.h> 137 #include <unistd.h> 138 #include <libutil.h> 139 #include <sysexits.h> 140 #include <ctype.h> 141 142 #include "inetd.h" 143 #include "pathnames.h" 144 145 #ifdef IPSEC 146 #include <netinet6/ipsec.h> 147 #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */ 148 #undef IPSEC 149 #endif 150 #endif 151 152 /* wrapper for KAME-special getnameinfo() */ 153 #ifndef NI_WITHSCOPEID 154 #define NI_WITHSCOPEID 0 155 #endif 156 157 #ifndef LIBWRAP_ALLOW_FACILITY 158 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH 159 #endif 160 #ifndef LIBWRAP_ALLOW_SEVERITY 161 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO 162 #endif 163 #ifndef LIBWRAP_DENY_FACILITY 164 # define LIBWRAP_DENY_FACILITY LOG_AUTH 165 #endif 166 #ifndef LIBWRAP_DENY_SEVERITY 167 # define LIBWRAP_DENY_SEVERITY LOG_WARNING 168 #endif 169 170 #define ISWRAP(sep) \ 171 ( ((wrap_ex && !(sep)->se_bi) || (wrap_bi && (sep)->se_bi)) \ 172 && ( ((sep)->se_accept && (sep)->se_socktype == SOCK_STREAM) \ 173 || (sep)->se_socktype == SOCK_DGRAM)) 174 175 #ifdef LOGIN_CAP 176 #include <login_cap.h> 177 178 /* see init.c */ 179 #define RESOURCE_RC "daemon" 180 181 #endif 182 183 #ifndef MAXCHILD 184 #define MAXCHILD -1 /* maximum number of this service 185 < 0 = no limit */ 186 #endif 187 188 #ifndef MAXCPM 189 #define MAXCPM -1 /* rate limit invocations from a 190 single remote address, 191 < 0 = no limit */ 192 #endif 193 194 #define TOOMANY 256 /* don't start more than TOOMANY */ 195 #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */ 196 #define RETRYTIME (60*10) /* retry after bind or server fail */ 197 #define MAX_MAXCHLD 32767 /* max allowable max children */ 198 199 #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM)) 200 201 int allow_severity; 202 int deny_severity; 203 int wrap_ex = 0; 204 int wrap_bi = 0; 205 int debug = 0; 206 int log = 0; 207 int maxsock; /* highest-numbered descriptor */ 208 fd_set allsock; 209 int options; 210 int timingout; 211 int toomany = TOOMANY; 212 int maxchild = MAXCHILD; 213 int maxcpm = MAXCPM; 214 struct servent *sp; 215 struct rpcent *rpc; 216 char *hostname = NULL; 217 struct sockaddr_in *bind_sa4; 218 int no_v4bind = 1; 219 #ifdef INET6 220 struct sockaddr_in6 *bind_sa6; 221 int no_v6bind = 1; 222 #endif 223 int signalpipe[2]; 224 #ifdef SANITY_CHECK 225 int nsock; 226 #endif 227 228 struct servtab *servtab; 229 230 extern struct biltin biltins[]; 231 232 #define NUMINT (sizeof(intab) / sizeof(struct inent)) 233 char *CONFIG = _PATH_INETDCONF; 234 char *pid_file = _PATH_INETDPID; 235 236 #ifdef OLD_SETPROCTITLE 237 char **Argv; 238 char *LastArg; 239 #endif 240 241 int 242 getvalue(arg, value, whine) 243 char *arg, *whine; 244 int *value; 245 { 246 int tmp; 247 char *p; 248 249 tmp = strtol(arg, &p, 0); 250 if (tmp < 1 || *p) { 251 syslog(LOG_ERR, whine, arg); 252 return 1; /* failure */ 253 } 254 *value = tmp; 255 return 0; /* success */ 256 } 257 258 int 259 main(argc, argv, envp) 260 int argc; 261 char *argv[], *envp[]; 262 { 263 struct servtab *sep; 264 struct passwd *pwd; 265 struct group *grp; 266 struct sigaction sa, saalrm, sachld, sahup, sapipe; 267 int tmpint, ch, dofork; 268 pid_t pid; 269 char buf[50]; 270 #ifdef LOGIN_CAP 271 login_cap_t *lc = NULL; 272 #endif 273 struct request_info req; 274 int denied; 275 char *service = NULL; 276 char *pnm; 277 union { 278 struct sockaddr peer_un; 279 struct sockaddr_in peer_un4; 280 struct sockaddr_in6 peer_un6; 281 struct sockaddr_storage peer_max; 282 } p_un; 283 #define peer p_un.peer_un 284 #define peer4 p_un.peer_un4 285 #define peer6 p_un.peer_un6 286 #define peermax p_un.peer_max 287 int i; 288 struct addrinfo hints, *res; 289 char *servname; 290 int error; 291 292 293 #ifdef OLD_SETPROCTITLE 294 Argv = argv; 295 if (envp == 0 || *envp == 0) 296 envp = argv; 297 while (*envp) 298 envp++; 299 LastArg = envp[-1] + strlen(envp[-1]); 300 #endif 301 302 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON); 303 304 while ((ch = getopt(argc, argv, "dlwWR:a:c:C:p:")) != -1) 305 switch(ch) { 306 case 'd': 307 debug = 1; 308 options |= SO_DEBUG; 309 break; 310 case 'l': 311 log = 1; 312 break; 313 case 'R': 314 getvalue(optarg, &toomany, 315 "-R %s: bad value for service invocation rate"); 316 break; 317 case 'c': 318 getvalue(optarg, &maxchild, 319 "-c %s: bad value for maximum children"); 320 break; 321 case 'C': 322 getvalue(optarg, &maxcpm, 323 "-C %s: bad value for maximum children/minute"); 324 break; 325 case 'a': 326 hostname = optarg; 327 break; 328 case 'p': 329 pid_file = optarg; 330 break; 331 case 'w': 332 wrap_ex++; 333 break; 334 case 'W': 335 wrap_bi++; 336 break; 337 case '?': 338 default: 339 syslog(LOG_ERR, 340 "usage: inetd [-dlwW] [-a address] [-R rate]" 341 " [-c maximum] [-C rate]" 342 " [-p pidfile] [conf-file]"); 343 exit(EX_USAGE); 344 } 345 /* 346 * Initialize Bind Addrs. 347 * When hostname is NULL, wild card bind addrs are obtained from 348 * getaddrinfo(). But getaddrinfo() requires at least one of 349 * hostname or servname is non NULL. 350 * So when hostname is NULL, set dummy value to servname. 351 */ 352 servname = (hostname == NULL) ? "discard" /* dummy */ : NULL; 353 354 bzero(&hints, sizeof(struct addrinfo)); 355 hints.ai_flags = AI_PASSIVE; 356 hints.ai_family = AF_UNSPEC; 357 error = getaddrinfo(hostname, servname, &hints, &res); 358 if (error != 0) { 359 syslog(LOG_ERR, "-a %s: %s", hostname, gai_strerror(error)); 360 if (error == EAI_SYSTEM) 361 syslog(LOG_ERR, "%s", strerror(errno)); 362 exit(EX_USAGE); 363 } 364 do { 365 if (res->ai_addr == NULL) { 366 syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname); 367 exit(EX_USAGE); 368 } 369 switch (res->ai_addr->sa_family) { 370 case AF_INET: 371 if (no_v4bind == 0) 372 continue; 373 bind_sa4 = (struct sockaddr_in *)res->ai_addr; 374 /* init port num in case servname is dummy */ 375 bind_sa4->sin_port = 0; 376 no_v4bind = 0; 377 continue; 378 #ifdef INET6 379 case AF_INET6: 380 if (no_v6bind == 0) 381 continue; 382 bind_sa6 = (struct sockaddr_in6 *)res->ai_addr; 383 /* init port num in case servname is dummy */ 384 bind_sa6->sin6_port = 0; 385 no_v6bind = 0; 386 continue; 387 #endif 388 } 389 if (no_v4bind == 0 390 #ifdef INET6 391 && no_v6bind == 0 392 #endif 393 ) 394 break; 395 } while ((res = res->ai_next) != NULL); 396 if (no_v4bind != 0 397 #ifdef INET6 398 && no_v6bind != 0 399 #endif 400 ) { 401 syslog(LOG_ERR, "-a %s: unknown address family", hostname); 402 exit(EX_USAGE); 403 } 404 405 argc -= optind; 406 argv += optind; 407 408 if (argc > 0) 409 CONFIG = argv[0]; 410 if (debug == 0) { 411 FILE *fp; 412 if (daemon(0, 0) < 0) { 413 syslog(LOG_WARNING, "daemon(0,0) failed: %m"); 414 } 415 /* 416 * In case somebody has started inetd manually, we need to 417 * clear the logname, so that old servers run as root do not 418 * get the user's logname.. 419 */ 420 if (setlogin("") < 0) { 421 syslog(LOG_WARNING, "cannot clear logname: %m"); 422 /* no big deal if it fails.. */ 423 } 424 pid = getpid(); 425 fp = fopen(pid_file, "w"); 426 if (fp) { 427 fprintf(fp, "%ld\n", (long)pid); 428 fclose(fp); 429 } else { 430 syslog(LOG_WARNING, "%s: %m", pid_file); 431 } 432 } 433 sa.sa_flags = 0; 434 sigemptyset(&sa.sa_mask); 435 sigaddset(&sa.sa_mask, SIGALRM); 436 sigaddset(&sa.sa_mask, SIGCHLD); 437 sigaddset(&sa.sa_mask, SIGHUP); 438 sa.sa_handler = flag_retry; 439 sigaction(SIGALRM, &sa, &saalrm); 440 config(); 441 sa.sa_handler = flag_config; 442 sigaction(SIGHUP, &sa, &sahup); 443 sa.sa_handler = flag_reapchild; 444 sigaction(SIGCHLD, &sa, &sachld); 445 sa.sa_handler = SIG_IGN; 446 sigaction(SIGPIPE, &sa, &sapipe); 447 448 { 449 /* space for daemons to overwrite environment for ps */ 450 #define DUMMYSIZE 100 451 char dummy[DUMMYSIZE]; 452 453 (void)memset(dummy, 'x', DUMMYSIZE - 1); 454 dummy[DUMMYSIZE - 1] = '\0'; 455 (void)setenv("inetd_dummy", dummy, 1); 456 } 457 458 if (pipe(signalpipe) != 0) { 459 syslog(LOG_ERR, "pipe: %m"); 460 exit(EX_OSERR); 461 } 462 FD_SET(signalpipe[0], &allsock); 463 #ifdef SANITY_CHECK 464 nsock++; 465 #endif 466 if (signalpipe[0] > maxsock) 467 maxsock = signalpipe[0]; 468 if (signalpipe[1] > maxsock) 469 maxsock = signalpipe[1]; 470 471 for (;;) { 472 int n, ctrl; 473 fd_set readable; 474 475 #ifdef SANITY_CHECK 476 if (nsock == 0) { 477 syslog(LOG_ERR, "%s: nsock=0", __FUNCTION__); 478 exit(EX_SOFTWARE); 479 } 480 #endif 481 readable = allsock; 482 if ((n = select(maxsock + 1, &readable, (fd_set *)0, 483 (fd_set *)0, (struct timeval *)0)) <= 0) { 484 if (n < 0 && errno != EINTR) { 485 syslog(LOG_WARNING, "select: %m"); 486 sleep(1); 487 } 488 continue; 489 } 490 /* handle any queued signal flags */ 491 if (FD_ISSET(signalpipe[0], &readable)) { 492 int n; 493 if (ioctl(signalpipe[0], FIONREAD, &n) != 0) { 494 syslog(LOG_ERR, "ioctl: %m"); 495 exit(EX_OSERR); 496 } 497 while (--n >= 0) { 498 char c; 499 if (read(signalpipe[0], &c, 1) != 1) { 500 syslog(LOG_ERR, "read: %m"); 501 exit(EX_OSERR); 502 } 503 if (debug) 504 warnx("handling signal flag %c", c); 505 switch(c) { 506 case 'A': /* sigalrm */ 507 retry(); 508 break; 509 case 'C': /* sigchld */ 510 reapchild(); 511 break; 512 case 'H': /* sighup */ 513 config(); 514 break; 515 } 516 } 517 } 518 for (sep = servtab; n && sep; sep = sep->se_next) 519 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) { 520 n--; 521 if (debug) 522 warnx("someone wants %s", sep->se_service); 523 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) { 524 i = 1; 525 if (ioctl(sep->se_fd, FIONBIO, &i) < 0) 526 syslog(LOG_ERR, "ioctl (FIONBIO, 1): %m"); 527 ctrl = accept(sep->se_fd, (struct sockaddr *)0, 528 (int *)0); 529 if (debug) 530 warnx("accept, ctrl %d", ctrl); 531 if (ctrl < 0) { 532 if (errno != EINTR) 533 syslog(LOG_WARNING, 534 "accept (for %s): %m", 535 sep->se_service); 536 if (sep->se_accept && 537 sep->se_socktype == SOCK_STREAM) 538 close(ctrl); 539 continue; 540 } 541 i = 0; 542 if (ioctl(sep->se_fd, FIONBIO, &i) < 0) 543 syslog(LOG_ERR, "ioctl1(FIONBIO, 0): %m"); 544 if (ioctl(ctrl, FIONBIO, &i) < 0) 545 syslog(LOG_ERR, "ioctl2(FIONBIO, 0): %m"); 546 if (cpmip(sep, ctrl) < 0) { 547 close(ctrl); 548 continue; 549 } 550 } else 551 ctrl = sep->se_fd; 552 if (log && !ISWRAP(sep)) { 553 char pname[INET6_ADDRSTRLEN]; 554 pnm = "unknown"; 555 i = sizeof peermax; 556 if (getpeername(ctrl, (struct sockaddr *) 557 &peermax, &i)) { 558 i = sizeof peermax; 559 if (recvfrom(ctrl, buf, sizeof(buf), 560 MSG_PEEK, 561 (struct sockaddr *)&peermax, 562 &i) >= 0) { 563 getnameinfo((struct sockaddr *)&peermax, 564 peer.sa_len, 565 pname, sizeof(pname), 566 NULL, 0, 567 NI_NUMERICHOST| 568 NI_WITHSCOPEID); 569 pnm = pname; 570 } 571 } else { 572 getnameinfo((struct sockaddr *)&peermax, 573 peer.sa_len, 574 pname, sizeof(pname), 575 NULL, 0, 576 NI_NUMERICHOST| 577 NI_WITHSCOPEID); 578 pnm = pname; 579 } 580 syslog(LOG_INFO,"%s from %s", sep->se_service, pnm); 581 } 582 (void) sigblock(SIGBLOCK); 583 pid = 0; 584 /* 585 * Fork for all external services, builtins which need to 586 * fork and anything we're wrapping (as wrapping might 587 * block or use hosts_options(5) twist). 588 */ 589 dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep); 590 if (dofork) { 591 if (sep->se_count++ == 0) 592 (void)gettimeofday(&sep->se_time, (struct timezone *)NULL); 593 else if (sep->se_count >= toomany) { 594 struct timeval now; 595 596 (void)gettimeofday(&now, (struct timezone *)NULL); 597 if (now.tv_sec - sep->se_time.tv_sec > 598 CNT_INTVL) { 599 sep->se_time = now; 600 sep->se_count = 1; 601 } else { 602 syslog(LOG_ERR, 603 "%s/%s server failing (looping), service terminated", 604 sep->se_service, sep->se_proto); 605 close_sep(sep); 606 sigsetmask(0L); 607 if (!timingout) { 608 timingout = 1; 609 alarm(RETRYTIME); 610 } 611 continue; 612 } 613 } 614 pid = fork(); 615 } 616 if (pid < 0) { 617 syslog(LOG_ERR, "fork: %m"); 618 if (sep->se_accept && 619 sep->se_socktype == SOCK_STREAM) 620 close(ctrl); 621 sigsetmask(0L); 622 sleep(1); 623 continue; 624 } 625 if (pid) 626 addchild(sep, pid); 627 sigsetmask(0L); 628 if (pid == 0) { 629 if (dofork) { 630 if (debug) 631 warnx("+ closing from %d", maxsock); 632 for (tmpint = maxsock; tmpint > 2; tmpint--) 633 if (tmpint != ctrl) 634 (void) close(tmpint); 635 sigaction(SIGALRM, &saalrm, (struct sigaction *)0); 636 sigaction(SIGCHLD, &sachld, (struct sigaction *)0); 637 sigaction(SIGHUP, &sahup, (struct sigaction *)0); 638 /* SIGPIPE reset before exec */ 639 } 640 /* 641 * Call tcpmux to find the real service to exec. 642 */ 643 if (sep->se_bi && 644 sep->se_bi->bi_fn == (void (*)()) tcpmux) { 645 sep = tcpmux(ctrl); 646 if (sep == NULL) { 647 close(ctrl); 648 _exit(0); 649 } 650 } 651 if (ISWRAP(sep)) { 652 inetd_setproctitle("wrapping", ctrl); 653 service = sep->se_server_name ? 654 sep->se_server_name : sep->se_service; 655 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, NULL); 656 fromhost(&req); 657 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY; 658 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY; 659 denied = !hosts_access(&req); 660 if (denied) { 661 syslog(deny_severity, 662 "refused connection from %.500s, service %s (%s)", 663 eval_client(&req), service, sep->se_proto); 664 if (sep->se_socktype != SOCK_STREAM) 665 recv(ctrl, buf, sizeof (buf), 0); 666 if (dofork) 667 _exit(0); 668 } 669 if (log) { 670 syslog(allow_severity, 671 "connection from %.500s, service %s (%s)", 672 eval_client(&req), service, sep->se_proto); 673 } 674 } 675 if (sep->se_bi) { 676 (*sep->se_bi->bi_fn)(ctrl, sep); 677 } else { 678 if (debug) 679 warnx("%d execl %s", 680 getpid(), sep->se_server); 681 dup2(ctrl, 0); 682 close(ctrl); 683 dup2(0, 1); 684 dup2(0, 2); 685 if ((pwd = getpwnam(sep->se_user)) == NULL) { 686 syslog(LOG_ERR, 687 "%s/%s: %s: no such user", 688 sep->se_service, sep->se_proto, 689 sep->se_user); 690 if (sep->se_socktype != SOCK_STREAM) 691 recv(0, buf, sizeof (buf), 0); 692 _exit(EX_NOUSER); 693 } 694 grp = NULL; 695 if ( sep->se_group != NULL 696 && (grp = getgrnam(sep->se_group)) == NULL 697 ) { 698 syslog(LOG_ERR, 699 "%s/%s: %s: no such group", 700 sep->se_service, sep->se_proto, 701 sep->se_group); 702 if (sep->se_socktype != SOCK_STREAM) 703 recv(0, buf, sizeof (buf), 0); 704 _exit(EX_NOUSER); 705 } 706 if (grp != NULL) 707 pwd->pw_gid = grp->gr_gid; 708 #ifdef LOGIN_CAP 709 if ((lc = login_getclass(sep->se_class)) == NULL) { 710 /* error syslogged by getclass */ 711 syslog(LOG_ERR, 712 "%s/%s: %s: login class error", 713 sep->se_service, sep->se_proto, 714 sep->se_class); 715 if (sep->se_socktype != SOCK_STREAM) 716 recv(0, buf, sizeof (buf), 0); 717 _exit(EX_NOUSER); 718 } 719 #endif 720 if (setsid() < 0) { 721 syslog(LOG_ERR, 722 "%s: can't setsid(): %m", 723 sep->se_service); 724 /* _exit(EX_OSERR); not fatal yet */ 725 } 726 #ifdef LOGIN_CAP 727 if (setusercontext(lc, pwd, pwd->pw_uid, 728 LOGIN_SETALL) != 0) { 729 syslog(LOG_ERR, 730 "%s: can't setusercontext(..%s..): %m", 731 sep->se_service, sep->se_user); 732 _exit(EX_OSERR); 733 } 734 #else 735 if (pwd->pw_uid) { 736 if (setlogin(sep->se_user) < 0) { 737 syslog(LOG_ERR, 738 "%s: can't setlogin(%s): %m", 739 sep->se_service, sep->se_user); 740 /* _exit(EX_OSERR); not yet */ 741 } 742 if (setgid(pwd->pw_gid) < 0) { 743 syslog(LOG_ERR, 744 "%s: can't set gid %d: %m", 745 sep->se_service, pwd->pw_gid); 746 _exit(EX_OSERR); 747 } 748 (void) initgroups(pwd->pw_name, 749 pwd->pw_gid); 750 if (setuid(pwd->pw_uid) < 0) { 751 syslog(LOG_ERR, 752 "%s: can't set uid %d: %m", 753 sep->se_service, pwd->pw_uid); 754 _exit(EX_OSERR); 755 } 756 } 757 #endif 758 sigaction(SIGPIPE, &sapipe, 759 (struct sigaction *)0); 760 execv(sep->se_server, sep->se_argv); 761 syslog(LOG_ERR, 762 "cannot execute %s: %m", sep->se_server); 763 if (sep->se_socktype != SOCK_STREAM) 764 recv(0, buf, sizeof (buf), 0); 765 } 766 if (dofork) 767 _exit(0); 768 } 769 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) 770 close(ctrl); 771 } 772 } 773 } 774 775 /* 776 * Add a signal flag to the signal flag queue for later handling 777 */ 778 779 void flag_signal(c) 780 char c; 781 { 782 if (write(signalpipe[1], &c, 1) != 1) { 783 syslog(LOG_ERR, "write: %m"); 784 _exit(EX_OSERR); 785 } 786 } 787 788 /* 789 * Record a new child pid for this service. If we've reached the 790 * limit on children, then stop accepting incoming requests. 791 */ 792 793 void 794 addchild(struct servtab *sep, pid_t pid) 795 { 796 #ifdef SANITY_CHECK 797 if (sep->se_numchild >= sep->se_maxchild) { 798 syslog(LOG_ERR, "%s: %d >= %d", 799 __FUNCTION__, sep->se_numchild, sep->se_maxchild); 800 exit(EX_SOFTWARE); 801 } 802 #endif 803 if (sep->se_maxchild == 0) 804 return; 805 sep->se_pids[sep->se_numchild++] = pid; 806 if (sep->se_numchild == sep->se_maxchild) 807 disable(sep); 808 } 809 810 /* 811 * Some child process has exited. See if it's on somebody's list. 812 */ 813 814 void 815 flag_reapchild(signo) 816 int signo; 817 { 818 flag_signal('C'); 819 } 820 821 void 822 reapchild() 823 { 824 int k, status; 825 pid_t pid; 826 struct servtab *sep; 827 828 for (;;) { 829 pid = wait3(&status, WNOHANG, (struct rusage *)0); 830 if (pid <= 0) 831 break; 832 if (debug) 833 warnx("%d reaped, status %#x", pid, status); 834 for (sep = servtab; sep; sep = sep->se_next) { 835 for (k = 0; k < sep->se_numchild; k++) 836 if (sep->se_pids[k] == pid) 837 break; 838 if (k == sep->se_numchild) 839 continue; 840 if (sep->se_numchild == sep->se_maxchild) 841 enable(sep); 842 sep->se_pids[k] = sep->se_pids[--sep->se_numchild]; 843 if (status) 844 syslog(LOG_WARNING, 845 "%s[%d]: exit status 0x%x", 846 sep->se_server, pid, status); 847 break; 848 } 849 } 850 } 851 852 void 853 flag_config(signo) 854 int signo; 855 { 856 flag_signal('H'); 857 } 858 859 void config() 860 { 861 struct servtab *sep, *new, **sepp; 862 long omask; 863 864 if (!setconfig()) { 865 syslog(LOG_ERR, "%s: %m", CONFIG); 866 return; 867 } 868 for (sep = servtab; sep; sep = sep->se_next) 869 sep->se_checked = 0; 870 while ((new = getconfigent())) { 871 if (getpwnam(new->se_user) == NULL) { 872 syslog(LOG_ERR, 873 "%s/%s: no such user '%s', service ignored", 874 new->se_service, new->se_proto, new->se_user); 875 continue; 876 } 877 if (new->se_group && getgrnam(new->se_group) == NULL) { 878 syslog(LOG_ERR, 879 "%s/%s: no such group '%s', service ignored", 880 new->se_service, new->se_proto, new->se_group); 881 continue; 882 } 883 #ifdef LOGIN_CAP 884 if (login_getclass(new->se_class) == NULL) { 885 /* error syslogged by getclass */ 886 syslog(LOG_ERR, 887 "%s/%s: %s: login class error, service ignored", 888 new->se_service, new->se_proto, new->se_class); 889 continue; 890 } 891 #endif 892 for (sep = servtab; sep; sep = sep->se_next) 893 if (strcmp(sep->se_service, new->se_service) == 0 && 894 strcmp(sep->se_proto, new->se_proto) == 0 && 895 sep->se_family == new->se_family) 896 break; 897 if (sep != 0) { 898 int i; 899 900 #define SWAP(a, b) { typeof(a) c = a; a = b; b = c; } 901 omask = sigblock(SIGBLOCK); 902 if (sep->se_nomapped != new->se_nomapped) { 903 sep->se_nomapped = new->se_nomapped; 904 sep->se_reset = 1; 905 } 906 /* copy over outstanding child pids */ 907 if (sep->se_maxchild && new->se_maxchild) { 908 new->se_numchild = sep->se_numchild; 909 if (new->se_numchild > new->se_maxchild) 910 new->se_numchild = new->se_maxchild; 911 memcpy(new->se_pids, sep->se_pids, 912 new->se_numchild * sizeof(*new->se_pids)); 913 } 914 SWAP(sep->se_pids, new->se_pids); 915 sep->se_maxchild = new->se_maxchild; 916 sep->se_numchild = new->se_numchild; 917 sep->se_maxcpm = new->se_maxcpm; 918 /* might need to turn on or off service now */ 919 if (sep->se_fd >= 0) { 920 if (sep->se_maxchild 921 && sep->se_numchild == sep->se_maxchild) { 922 if (FD_ISSET(sep->se_fd, &allsock)) 923 disable(sep); 924 } else { 925 if (!FD_ISSET(sep->se_fd, &allsock)) 926 enable(sep); 927 } 928 } 929 sep->se_accept = new->se_accept; 930 SWAP(sep->se_user, new->se_user); 931 SWAP(sep->se_group, new->se_group); 932 #ifdef LOGIN_CAP 933 SWAP(sep->se_class, new->se_class); 934 #endif 935 SWAP(sep->se_server, new->se_server); 936 SWAP(sep->se_server_name, new->se_server_name); 937 for (i = 0; i < MAXARGV; i++) 938 SWAP(sep->se_argv[i], new->se_argv[i]); 939 #ifdef IPSEC 940 SWAP(sep->se_policy, new->se_policy); 941 ipsecsetup(sep); 942 #endif 943 sigsetmask(omask); 944 freeconfig(new); 945 if (debug) 946 print_service("REDO", sep); 947 } else { 948 sep = enter(new); 949 if (debug) 950 print_service("ADD ", sep); 951 } 952 sep->se_checked = 1; 953 if (ISMUX(sep)) { 954 sep->se_fd = -1; 955 continue; 956 } 957 switch (sep->se_family) { 958 case AF_INET: 959 if (no_v4bind != 0) { 960 sep->se_fd = -1; 961 continue; 962 } 963 break; 964 #ifdef INET6 965 case AF_INET6: 966 if (no_v6bind != 0) { 967 sep->se_fd = -1; 968 continue; 969 } 970 break; 971 #endif 972 } 973 if (!sep->se_rpc) { 974 sp = getservbyname(sep->se_service, sep->se_proto); 975 if (sp == 0) { 976 syslog(LOG_ERR, "%s/%s: unknown service", 977 sep->se_service, sep->se_proto); 978 sep->se_checked = 0; 979 continue; 980 } 981 switch (sep->se_family) { 982 case AF_INET: 983 if (sep->se_ctladdrinitok == 0) { 984 memcpy(&sep->se_ctrladdr4, bind_sa4, 985 sizeof(sep->se_ctrladdr4)); 986 sep->se_ctrladdr_size = 987 sizeof(sep->se_ctrladdr4); 988 } 989 if (sp->s_port != sep->se_ctrladdr4.sin_port) { 990 sep->se_ctrladdr4.sin_port = 991 sp->s_port; 992 sep->se_reset = 1; 993 } 994 break; 995 #ifdef INET6 996 case AF_INET6: 997 if (sep->se_ctladdrinitok == 0) { 998 memcpy(&sep->se_ctrladdr6, bind_sa6, 999 sizeof(sep->se_ctrladdr6)); 1000 sep->se_ctrladdr_size = 1001 sizeof(sep->se_ctrladdr6); 1002 } 1003 if (sp->s_port != 1004 sep->se_ctrladdr6.sin6_port) { 1005 sep->se_ctrladdr6.sin6_port = 1006 sp->s_port; 1007 sep->se_reset = 1; 1008 } 1009 break; 1010 #endif 1011 } 1012 if (sep->se_reset != 0 && sep->se_fd >= 0) 1013 close_sep(sep); 1014 } else { 1015 rpc = getrpcbyname(sep->se_service); 1016 if (rpc == 0) { 1017 syslog(LOG_ERR, "%s/%s unknown RPC service", 1018 sep->se_service, sep->se_proto); 1019 if (sep->se_fd != -1) 1020 (void) close(sep->se_fd); 1021 sep->se_fd = -1; 1022 continue; 1023 } 1024 if (rpc->r_number != sep->se_rpc_prog) { 1025 if (sep->se_rpc_prog) 1026 unregisterrpc(sep); 1027 sep->se_rpc_prog = rpc->r_number; 1028 if (sep->se_fd != -1) 1029 (void) close(sep->se_fd); 1030 sep->se_fd = -1; 1031 } 1032 } 1033 if (sep->se_fd == -1) 1034 setup(sep); 1035 } 1036 endconfig(); 1037 /* 1038 * Purge anything not looked at above. 1039 */ 1040 omask = sigblock(SIGBLOCK); 1041 sepp = &servtab; 1042 while ((sep = *sepp)) { 1043 if (sep->se_checked) { 1044 sepp = &sep->se_next; 1045 continue; 1046 } 1047 *sepp = sep->se_next; 1048 if (sep->se_fd >= 0) 1049 close_sep(sep); 1050 if (debug) 1051 print_service("FREE", sep); 1052 if (sep->se_rpc && sep->se_rpc_prog > 0) 1053 unregisterrpc(sep); 1054 freeconfig(sep); 1055 free((char *)sep); 1056 } 1057 (void) sigsetmask(omask); 1058 } 1059 1060 void 1061 unregisterrpc(sep) 1062 struct servtab *sep; 1063 { 1064 int i; 1065 struct servtab *sepp; 1066 long omask; 1067 1068 omask = sigblock(SIGBLOCK); 1069 for (sepp = servtab; sepp; sepp = sepp->se_next) { 1070 if (sepp == sep) 1071 continue; 1072 if (sep->se_checked == 0 || 1073 !sepp->se_rpc || 1074 sep->se_rpc_prog != sepp->se_rpc_prog) 1075 continue; 1076 return; 1077 } 1078 if (debug) 1079 print_service("UNREG", sep); 1080 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) 1081 pmap_unset(sep->se_rpc_prog, i); 1082 if (sep->se_fd != -1) 1083 (void) close(sep->se_fd); 1084 sep->se_fd = -1; 1085 (void) sigsetmask(omask); 1086 } 1087 1088 void 1089 flag_retry(signo) 1090 int signo; 1091 { 1092 flag_signal('A'); 1093 } 1094 1095 void 1096 retry() 1097 { 1098 struct servtab *sep; 1099 1100 timingout = 0; 1101 for (sep = servtab; sep; sep = sep->se_next) 1102 if (sep->se_fd == -1 && !ISMUX(sep)) 1103 setup(sep); 1104 } 1105 1106 void 1107 setup(sep) 1108 struct servtab *sep; 1109 { 1110 int on = 1; 1111 1112 if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) { 1113 if (debug) 1114 warn("socket failed on %s/%s", 1115 sep->se_service, sep->se_proto); 1116 syslog(LOG_ERR, "%s/%s: socket: %m", 1117 sep->se_service, sep->se_proto); 1118 return; 1119 } 1120 #define turnon(fd, opt) \ 1121 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on)) 1122 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) && 1123 turnon(sep->se_fd, SO_DEBUG) < 0) 1124 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 1125 if (turnon(sep->se_fd, SO_REUSEADDR) < 0) 1126 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m"); 1127 #ifdef SO_PRIVSTATE 1128 if (turnon(sep->se_fd, SO_PRIVSTATE) < 0) 1129 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m"); 1130 #endif 1131 /* tftpd opens a new connection then needs more infos */ 1132 if ((sep->se_family == AF_INET6) && 1133 (strcmp(sep->se_proto, "udp") == 0) && 1134 (sep->se_accept == 0) && 1135 (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_PKTINFO, 1136 (char *)&on, sizeof (on)) < 0)) 1137 syslog(LOG_ERR, "setsockopt (IPV6_RECVPKTINFO): %m"); 1138 #ifdef IPV6_BINDV6ONLY 1139 if (sep->se_family == AF_INET6) { 1140 int flag = sep->se_nomapped ? 1 : 0; 1141 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_BINDV6ONLY, 1142 (char *)&flag, sizeof (flag)) < 0) 1143 syslog(LOG_ERR, "setsockopt (IPV6_BINDV6ONLY): %m"); 1144 } 1145 #endif /* IPV6_BINDV6ONLY */ 1146 #undef turnon 1147 if (sep->se_type == TTCP_TYPE) 1148 if (setsockopt(sep->se_fd, IPPROTO_TCP, TCP_NOPUSH, 1149 (char *)&on, sizeof (on)) < 0) 1150 syslog(LOG_ERR, "setsockopt (TCP_NOPUSH): %m"); 1151 #ifdef IPV6_FAITH 1152 if (sep->se_type == FAITH_TYPE) { 1153 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_FAITH, &on, 1154 sizeof(on)) < 0) { 1155 syslog(LOG_ERR, "setsockopt (IPV6_FAITH): %m"); 1156 } 1157 } 1158 #endif 1159 #ifdef IPSEC 1160 ipsecsetup(sep); 1161 #endif 1162 if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr, 1163 sep->se_ctrladdr_size) < 0) { 1164 if (debug) 1165 warn("bind failed on %s/%s", 1166 sep->se_service, sep->se_proto); 1167 syslog(LOG_ERR, "%s/%s: bind: %m", 1168 sep->se_service, sep->se_proto); 1169 (void) close(sep->se_fd); 1170 sep->se_fd = -1; 1171 if (!timingout) { 1172 timingout = 1; 1173 alarm(RETRYTIME); 1174 } 1175 return; 1176 } 1177 if (sep->se_rpc) { 1178 int i, len = sep->se_ctrladdr_size; 1179 1180 if (sep->se_family != AF_INET) { 1181 syslog(LOG_ERR, 1182 "%s/%s: unsupported address family for rpc", 1183 sep->se_service, sep->se_proto); 1184 (void) close(sep->se_fd); 1185 sep->se_fd = -1; 1186 return; 1187 } 1188 if (getsockname(sep->se_fd, 1189 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){ 1190 syslog(LOG_ERR, "%s/%s: getsockname: %m", 1191 sep->se_service, sep->se_proto); 1192 (void) close(sep->se_fd); 1193 sep->se_fd = -1; 1194 return; 1195 } 1196 if (debug) 1197 print_service("REG ", sep); 1198 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) { 1199 pmap_unset(sep->se_rpc_prog, i); 1200 pmap_set(sep->se_rpc_prog, i, 1201 (sep->se_socktype == SOCK_DGRAM) 1202 ? IPPROTO_UDP : IPPROTO_TCP, 1203 ntohs(sep->se_ctrladdr4.sin_port)); 1204 } 1205 } 1206 if (sep->se_socktype == SOCK_STREAM) 1207 listen(sep->se_fd, 64); 1208 enable(sep); 1209 if (debug) { 1210 warnx("registered %s on %d", 1211 sep->se_server, sep->se_fd); 1212 } 1213 } 1214 1215 #ifdef IPSEC 1216 void 1217 ipsecsetup(sep) 1218 struct servtab *sep; 1219 { 1220 char *buf; 1221 char *policy_in = NULL; 1222 char *policy_out = NULL; 1223 int level; 1224 int opt; 1225 1226 switch (sep->se_family) { 1227 case AF_INET: 1228 level = IPPROTO_IP; 1229 opt = IP_IPSEC_POLICY; 1230 break; 1231 #ifdef INET6 1232 case AF_INET6: 1233 level = IPPROTO_IPV6; 1234 opt = IPV6_IPSEC_POLICY; 1235 break; 1236 #endif 1237 default: 1238 return; 1239 } 1240 1241 if (!sep->se_policy || sep->se_policy[0] == '\0') { 1242 policy_in = "in entrust"; 1243 policy_out = "out entrust"; 1244 } else { 1245 if (!strncmp("in", sep->se_policy, 2)) 1246 policy_in = sep->se_policy; 1247 else if (!strncmp("out", sep->se_policy, 3)) 1248 policy_out = sep->se_policy; 1249 else { 1250 syslog(LOG_ERR, "invalid security policy \"%s\"", 1251 sep->se_policy); 1252 return; 1253 } 1254 } 1255 1256 if (policy_in != NULL) { 1257 buf = ipsec_set_policy(policy_in, strlen(policy_in)); 1258 if (buf != NULL) { 1259 if (setsockopt(sep->se_fd, level, opt, 1260 buf, ipsec_get_policylen(buf)) < 0 && 1261 debug != 0) 1262 warnx("%s/%s: ipsec initialization failed; %s", 1263 sep->se_service, sep->se_proto, 1264 policy_in); 1265 free(buf); 1266 } else 1267 syslog(LOG_ERR, "invalid security policy \"%s\"", 1268 policy_in); 1269 } 1270 if (policy_out != NULL) { 1271 buf = ipsec_set_policy(policy_out, strlen(policy_out)); 1272 if (buf != NULL) { 1273 if (setsockopt(sep->se_fd, level, opt, 1274 buf, ipsec_get_policylen(buf)) < 0 && 1275 debug != 0) 1276 warnx("%s/%s: ipsec initialization failed; %s", 1277 sep->se_service, sep->se_proto, 1278 policy_out); 1279 free(buf); 1280 } else 1281 syslog(LOG_ERR, "invalid security policy \"%s\"", 1282 policy_out); 1283 } 1284 } 1285 #endif 1286 1287 /* 1288 * Finish with a service and its socket. 1289 */ 1290 void 1291 close_sep(sep) 1292 struct servtab *sep; 1293 { 1294 if (sep->se_fd >= 0) { 1295 if (FD_ISSET(sep->se_fd, &allsock)) 1296 disable(sep); 1297 (void) close(sep->se_fd); 1298 sep->se_fd = -1; 1299 } 1300 sep->se_count = 0; 1301 sep->se_numchild = 0; /* forget about any existing children */ 1302 } 1303 1304 int 1305 matchservent(name1, name2, proto) 1306 char *name1, *name2, *proto; 1307 { 1308 char **alias; 1309 struct servent *se; 1310 1311 if (strcmp(name1, name2) == 0) 1312 return(1); 1313 if ((se = getservbyname(name1, proto)) != NULL) { 1314 if (strcmp(name2, se->s_name) == 0) 1315 return(1); 1316 for (alias = se->s_aliases; *alias; alias++) 1317 if (strcmp(name2, *alias) == 0) 1318 return(1); 1319 } 1320 return(0); 1321 } 1322 1323 struct servtab * 1324 enter(cp) 1325 struct servtab *cp; 1326 { 1327 struct servtab *sep; 1328 long omask; 1329 1330 sep = (struct servtab *)malloc(sizeof (*sep)); 1331 if (sep == (struct servtab *)0) { 1332 syslog(LOG_ERR, "malloc: %m"); 1333 exit(EX_OSERR); 1334 } 1335 *sep = *cp; 1336 sep->se_fd = -1; 1337 omask = sigblock(SIGBLOCK); 1338 sep->se_next = servtab; 1339 servtab = sep; 1340 sigsetmask(omask); 1341 return (sep); 1342 } 1343 1344 void 1345 enable(struct servtab *sep) 1346 { 1347 if (debug) 1348 warnx( 1349 "enabling %s, fd %d", sep->se_service, sep->se_fd); 1350 #ifdef SANITY_CHECK 1351 if (sep->se_fd < 0) { 1352 syslog(LOG_ERR, 1353 "%s: %s: bad fd", __FUNCTION__, sep->se_service); 1354 exit(EX_SOFTWARE); 1355 } 1356 if (ISMUX(sep)) { 1357 syslog(LOG_ERR, 1358 "%s: %s: is mux", __FUNCTION__, sep->se_service); 1359 exit(EX_SOFTWARE); 1360 } 1361 if (FD_ISSET(sep->se_fd, &allsock)) { 1362 syslog(LOG_ERR, 1363 "%s: %s: not off", __FUNCTION__, sep->se_service); 1364 exit(EX_SOFTWARE); 1365 } 1366 nsock++; 1367 #endif 1368 FD_SET(sep->se_fd, &allsock); 1369 if (sep->se_fd > maxsock) 1370 maxsock = sep->se_fd; 1371 } 1372 1373 void 1374 disable(struct servtab *sep) 1375 { 1376 if (debug) 1377 warnx( 1378 "disabling %s, fd %d", sep->se_service, sep->se_fd); 1379 #ifdef SANITY_CHECK 1380 if (sep->se_fd < 0) { 1381 syslog(LOG_ERR, 1382 "%s: %s: bad fd", __FUNCTION__, sep->se_service); 1383 exit(EX_SOFTWARE); 1384 } 1385 if (ISMUX(sep)) { 1386 syslog(LOG_ERR, 1387 "%s: %s: is mux", __FUNCTION__, sep->se_service); 1388 exit(EX_SOFTWARE); 1389 } 1390 if (!FD_ISSET(sep->se_fd, &allsock)) { 1391 syslog(LOG_ERR, 1392 "%s: %s: not on", __FUNCTION__, sep->se_service); 1393 exit(EX_SOFTWARE); 1394 } 1395 if (nsock == 0) { 1396 syslog(LOG_ERR, "%s: nsock=0", __FUNCTION__); 1397 exit(EX_SOFTWARE); 1398 } 1399 nsock--; 1400 #endif 1401 FD_CLR(sep->se_fd, &allsock); 1402 if (sep->se_fd == maxsock) 1403 maxsock--; 1404 } 1405 1406 FILE *fconfig = NULL; 1407 struct servtab serv; 1408 char line[LINE_MAX]; 1409 1410 int 1411 setconfig() 1412 { 1413 1414 if (fconfig != NULL) { 1415 fseek(fconfig, 0L, SEEK_SET); 1416 return (1); 1417 } 1418 fconfig = fopen(CONFIG, "r"); 1419 return (fconfig != NULL); 1420 } 1421 1422 void 1423 endconfig() 1424 { 1425 if (fconfig) { 1426 (void) fclose(fconfig); 1427 fconfig = NULL; 1428 } 1429 } 1430 1431 struct servtab * 1432 getconfigent() 1433 { 1434 struct servtab *sep = &serv; 1435 int argc; 1436 char *cp, *arg, *s; 1437 char *versp; 1438 static char TCPMUX_TOKEN[] = "tcpmux/"; 1439 #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1) 1440 #ifdef IPSEC 1441 char *policy = NULL; 1442 #endif 1443 int v4bind = 0; 1444 #ifdef INET6 1445 int v6bind = 0; 1446 #endif 1447 1448 more: 1449 while ((cp = nextline(fconfig)) != NULL) { 1450 #ifdef IPSEC 1451 /* lines starting with #@ is not a comment, but the policy */ 1452 if (cp[0] == '#' && cp[1] == '@') { 1453 char *p; 1454 for (p = cp + 2; p && *p && isspace(*p); p++) 1455 ; 1456 if (*p == '\0') { 1457 if (policy) 1458 free(policy); 1459 policy = NULL; 1460 } else if (ipsec_get_policylen(p) >= 0) { 1461 if (policy) 1462 free(policy); 1463 policy = newstr(p); 1464 } else { 1465 syslog(LOG_ERR, 1466 "%s: invalid ipsec policy \"%s\"", 1467 CONFIG, p); 1468 exit(EX_CONFIG); 1469 } 1470 } 1471 #endif 1472 if (*cp == '#' || *cp == '\0') 1473 continue; 1474 break; 1475 } 1476 if (cp == NULL) 1477 return ((struct servtab *)0); 1478 /* 1479 * clear the static buffer, since some fields (se_ctrladdr, 1480 * for example) don't get initialized here. 1481 */ 1482 memset((caddr_t)sep, 0, sizeof *sep); 1483 arg = skip(&cp); 1484 if (cp == NULL) { 1485 /* got an empty line containing just blanks/tabs. */ 1486 goto more; 1487 } 1488 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) { 1489 char *c = arg + MUX_LEN; 1490 if (*c == '+') { 1491 sep->se_type = MUXPLUS_TYPE; 1492 c++; 1493 } else 1494 sep->se_type = MUX_TYPE; 1495 sep->se_service = newstr(c); 1496 } else { 1497 sep->se_service = newstr(arg); 1498 sep->se_type = NORM_TYPE; 1499 } 1500 arg = sskip(&cp); 1501 if (strcmp(arg, "stream") == 0) 1502 sep->se_socktype = SOCK_STREAM; 1503 else if (strcmp(arg, "dgram") == 0) 1504 sep->se_socktype = SOCK_DGRAM; 1505 else if (strcmp(arg, "rdm") == 0) 1506 sep->se_socktype = SOCK_RDM; 1507 else if (strcmp(arg, "seqpacket") == 0) 1508 sep->se_socktype = SOCK_SEQPACKET; 1509 else if (strcmp(arg, "raw") == 0) 1510 sep->se_socktype = SOCK_RAW; 1511 else 1512 sep->se_socktype = -1; 1513 1514 arg = sskip(&cp); 1515 if (strncmp(arg, "tcp", 3) == 0) { 1516 sep->se_proto = newstr(strsep(&arg, "/")); 1517 if (arg != NULL) { 1518 if (strcmp(arg, "ttcp") == 0) 1519 sep->se_type = TTCP_TYPE; 1520 else if (strcmp(arg, "faith") == 0) 1521 sep->se_type = FAITH_TYPE; 1522 } 1523 } else 1524 sep->se_proto = newstr(arg); 1525 if (strncmp(sep->se_proto, "rpc/", 4) == 0) { 1526 if (no_v4bind != 0) { 1527 syslog(LOG_INFO, "IPv4 bind is ignored for %s", 1528 sep->se_service); 1529 freeconfig(sep); 1530 goto more; 1531 } 1532 memmove(sep->se_proto, sep->se_proto + 4, 1533 strlen(sep->se_proto) + 1 - 4); 1534 sep->se_rpc = 1; 1535 sep->se_rpc_prog = sep->se_rpc_lowvers = 1536 sep->se_rpc_lowvers = 0; 1537 memcpy(&sep->se_ctrladdr4, bind_sa4, 1538 sizeof(sep->se_ctrladdr4)); 1539 if ((versp = rindex(sep->se_service, '/'))) { 1540 *versp++ = '\0'; 1541 switch (sscanf(versp, "%d-%d", 1542 &sep->se_rpc_lowvers, 1543 &sep->se_rpc_highvers)) { 1544 case 2: 1545 break; 1546 case 1: 1547 sep->se_rpc_highvers = 1548 sep->se_rpc_lowvers; 1549 break; 1550 default: 1551 syslog(LOG_ERR, 1552 "bad RPC version specifier; %s", 1553 sep->se_service); 1554 freeconfig(sep); 1555 goto more; 1556 } 1557 } 1558 else { 1559 sep->se_rpc_lowvers = 1560 sep->se_rpc_highvers = 1; 1561 } 1562 } 1563 sep->se_nomapped = 0; 1564 while (isdigit(sep->se_proto[strlen(sep->se_proto) - 1])) { 1565 #ifdef INET6 1566 if (sep->se_proto[strlen(sep->se_proto) - 1] == '6') { 1567 if (no_v6bind != 0) { 1568 syslog(LOG_INFO, "IPv6 bind is ignored for %s", 1569 sep->se_service); 1570 freeconfig(sep); 1571 goto more; 1572 } 1573 sep->se_proto[strlen(sep->se_proto) - 1] = '\0'; 1574 v6bind = 1; 1575 continue; 1576 } 1577 #endif 1578 if (sep->se_proto[strlen(sep->se_proto) - 1] == '4') { 1579 sep->se_proto[strlen(sep->se_proto) - 1] = '\0'; 1580 v4bind = 1; 1581 continue; 1582 } 1583 /* illegal version num */ 1584 syslog(LOG_ERR, "bad IP version for %s", sep->se_proto); 1585 freeconfig(sep); 1586 goto more; 1587 } 1588 #ifdef INET6 1589 if (v6bind != 0) { 1590 sep->se_family = AF_INET6; 1591 if (v4bind == 0 || no_v4bind != 0) 1592 sep->se_nomapped = 1; 1593 } else 1594 #endif 1595 { /* default to v4 bind if not v6 bind */ 1596 if (no_v4bind != 0) { 1597 syslog(LOG_INFO, "IPv4 bind is ignored for %s", 1598 sep->se_service); 1599 freeconfig(sep); 1600 goto more; 1601 } 1602 sep->se_family = AF_INET; 1603 } 1604 /* init ctladdr */ 1605 switch(sep->se_family) { 1606 case AF_INET: 1607 memcpy(&sep->se_ctrladdr4, bind_sa4, 1608 sizeof(sep->se_ctrladdr4)); 1609 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr4); 1610 sep->se_ctladdrinitok = 1; 1611 break; 1612 #ifdef INET6 1613 case AF_INET6: 1614 memcpy(&sep->se_ctrladdr6, bind_sa6, 1615 sizeof(sep->se_ctrladdr6)); 1616 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr6); 1617 sep->se_ctladdrinitok = 1; 1618 break; 1619 #endif 1620 } 1621 arg = sskip(&cp); 1622 if (!strncmp(arg, "wait", 4)) 1623 sep->se_accept = 0; 1624 else if (!strncmp(arg, "nowait", 6)) 1625 sep->se_accept = 1; 1626 else { 1627 syslog(LOG_ERR, 1628 "%s: bad wait/nowait for service %s", 1629 CONFIG, sep->se_service); 1630 goto more; 1631 } 1632 sep->se_maxchild = -1; 1633 sep->se_maxcpm = -1; 1634 if ((s = strchr(arg, '/')) != NULL) { 1635 char *eptr; 1636 u_long val; 1637 1638 val = strtoul(s + 1, &eptr, 10); 1639 if (eptr == s + 1 || val > MAX_MAXCHLD) { 1640 syslog(LOG_ERR, 1641 "%s: bad max-child for service %s", 1642 CONFIG, sep->se_service); 1643 goto more; 1644 } 1645 if (debug) 1646 if (!sep->se_accept && val != 1) 1647 warnx("maxchild=%lu for wait service %s" 1648 " not recommended", val, sep->se_service); 1649 sep->se_maxchild = val; 1650 if (*eptr == '/') 1651 sep->se_maxcpm = strtol(eptr + 1, &eptr, 10); 1652 /* 1653 * explicitly do not check for \0 for future expansion / 1654 * backwards compatibility 1655 */ 1656 } 1657 if (ISMUX(sep)) { 1658 /* 1659 * Silently enforce "nowait" mode for TCPMUX services 1660 * since they don't have an assigned port to listen on. 1661 */ 1662 sep->se_accept = 1; 1663 if (strcmp(sep->se_proto, "tcp")) { 1664 syslog(LOG_ERR, 1665 "%s: bad protocol for tcpmux service %s", 1666 CONFIG, sep->se_service); 1667 goto more; 1668 } 1669 if (sep->se_socktype != SOCK_STREAM) { 1670 syslog(LOG_ERR, 1671 "%s: bad socket type for tcpmux service %s", 1672 CONFIG, sep->se_service); 1673 goto more; 1674 } 1675 } 1676 sep->se_user = newstr(sskip(&cp)); 1677 #ifdef LOGIN_CAP 1678 if ((s = strrchr(sep->se_user, '/')) != NULL) { 1679 *s = '\0'; 1680 sep->se_class = newstr(s + 1); 1681 } else 1682 sep->se_class = newstr(RESOURCE_RC); 1683 #endif 1684 if ((s = strrchr(sep->se_user, ':')) != NULL) { 1685 *s = '\0'; 1686 sep->se_group = newstr(s + 1); 1687 } else 1688 sep->se_group = NULL; 1689 sep->se_server = newstr(sskip(&cp)); 1690 if ((sep->se_server_name = rindex(sep->se_server, '/'))) 1691 sep->se_server_name++; 1692 if (strcmp(sep->se_server, "internal") == 0) { 1693 struct biltin *bi; 1694 1695 for (bi = biltins; bi->bi_service; bi++) 1696 if (bi->bi_socktype == sep->se_socktype && 1697 matchservent(bi->bi_service, sep->se_service, 1698 sep->se_proto)) 1699 break; 1700 if (bi->bi_service == 0) { 1701 syslog(LOG_ERR, "internal service %s unknown", 1702 sep->se_service); 1703 goto more; 1704 } 1705 sep->se_accept = 1; /* force accept mode for built-ins */ 1706 sep->se_bi = bi; 1707 } else 1708 sep->se_bi = NULL; 1709 if (sep->se_maxcpm < 0) 1710 sep->se_maxcpm = maxcpm; 1711 if (sep->se_maxchild < 0) { /* apply default max-children */ 1712 if (sep->se_bi && sep->se_bi->bi_maxchild >= 0) 1713 sep->se_maxchild = sep->se_bi->bi_maxchild; 1714 else if (sep->se_accept) 1715 sep->se_maxchild = maxchild > 0 ? maxchild : 0; 1716 else 1717 sep->se_maxchild = 1; 1718 } 1719 if (sep->se_maxchild) { 1720 sep->se_pids = malloc(sep->se_maxchild * sizeof(*sep->se_pids)); 1721 if (sep->se_pids == NULL) { 1722 syslog(LOG_ERR, "malloc: %m"); 1723 exit(EX_OSERR); 1724 } 1725 } 1726 argc = 0; 1727 for (arg = skip(&cp); cp; arg = skip(&cp)) 1728 if (argc < MAXARGV) { 1729 sep->se_argv[argc++] = newstr(arg); 1730 } else { 1731 syslog(LOG_ERR, 1732 "%s: too many arguments for service %s", 1733 CONFIG, sep->se_service); 1734 goto more; 1735 } 1736 while (argc <= MAXARGV) 1737 sep->se_argv[argc++] = NULL; 1738 #ifdef IPSEC 1739 sep->se_policy = policy ? newstr(policy) : NULL; 1740 #endif 1741 return (sep); 1742 } 1743 1744 void 1745 freeconfig(cp) 1746 struct servtab *cp; 1747 { 1748 int i; 1749 1750 if (cp->se_service) 1751 free(cp->se_service); 1752 if (cp->se_proto) 1753 free(cp->se_proto); 1754 if (cp->se_user) 1755 free(cp->se_user); 1756 if (cp->se_group) 1757 free(cp->se_group); 1758 #ifdef LOGIN_CAP 1759 if (cp->se_class) 1760 free(cp->se_class); 1761 #endif 1762 if (cp->se_server) 1763 free(cp->se_server); 1764 if (cp->se_pids) 1765 free(cp->se_pids); 1766 for (i = 0; i < MAXARGV; i++) 1767 if (cp->se_argv[i]) 1768 free(cp->se_argv[i]); 1769 #ifdef IPSEC 1770 if (cp->se_policy) 1771 free(cp->se_policy); 1772 #endif 1773 } 1774 1775 1776 /* 1777 * Safe skip - if skip returns null, log a syntax error in the 1778 * configuration file and exit. 1779 */ 1780 char * 1781 sskip(cpp) 1782 char **cpp; 1783 { 1784 char *cp; 1785 1786 cp = skip(cpp); 1787 if (cp == NULL) { 1788 syslog(LOG_ERR, "%s: syntax error", CONFIG); 1789 exit(EX_DATAERR); 1790 } 1791 return (cp); 1792 } 1793 1794 char * 1795 skip(cpp) 1796 char **cpp; 1797 { 1798 char *cp = *cpp; 1799 char *start; 1800 char quote = '\0'; 1801 1802 again: 1803 while (*cp == ' ' || *cp == '\t') 1804 cp++; 1805 if (*cp == '\0') { 1806 int c; 1807 1808 c = getc(fconfig); 1809 (void) ungetc(c, fconfig); 1810 if (c == ' ' || c == '\t') 1811 if ((cp = nextline(fconfig))) 1812 goto again; 1813 *cpp = (char *)0; 1814 return ((char *)0); 1815 } 1816 if (*cp == '"' || *cp == '\'') 1817 quote = *cp++; 1818 start = cp; 1819 if (quote) 1820 while (*cp && *cp != quote) 1821 cp++; 1822 else 1823 while (*cp && *cp != ' ' && *cp != '\t') 1824 cp++; 1825 if (*cp != '\0') 1826 *cp++ = '\0'; 1827 *cpp = cp; 1828 return (start); 1829 } 1830 1831 char * 1832 nextline(fd) 1833 FILE *fd; 1834 { 1835 char *cp; 1836 1837 if (fgets(line, sizeof (line), fd) == NULL) 1838 return ((char *)0); 1839 cp = strchr(line, '\n'); 1840 if (cp) 1841 *cp = '\0'; 1842 return (line); 1843 } 1844 1845 char * 1846 newstr(cp) 1847 char *cp; 1848 { 1849 if ((cp = strdup(cp ? cp : ""))) 1850 return (cp); 1851 syslog(LOG_ERR, "strdup: %m"); 1852 exit(EX_OSERR); 1853 } 1854 1855 #ifdef OLD_SETPROCTITLE 1856 void 1857 inetd_setproctitle(a, s) 1858 char *a; 1859 int s; 1860 { 1861 int size; 1862 char *cp; 1863 struct sockaddr_storage ss; 1864 char buf[80], pbuf[INET6_ADDRSTRLEN]; 1865 1866 cp = Argv[0]; 1867 size = sizeof(ss); 1868 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) { 1869 getnameinfo((struct sockaddr *)&ss, size, pbuf, sizeof(pbuf), 1870 NULL, 0, NI_NUMERICHOST|NI_WITHSCOPEID); 1871 (void) sprintf(buf, "-%s [%s]", a, pbuf); 1872 } else 1873 (void) sprintf(buf, "-%s", a); 1874 strncpy(cp, buf, LastArg - cp); 1875 cp += strlen(cp); 1876 while (cp < LastArg) 1877 *cp++ = ' '; 1878 } 1879 #else 1880 void 1881 inetd_setproctitle(a, s) 1882 char *a; 1883 int s; 1884 { 1885 int size; 1886 struct sockaddr_storage ss; 1887 char buf[80], pbuf[INET6_ADDRSTRLEN]; 1888 1889 size = sizeof(ss); 1890 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) { 1891 getnameinfo((struct sockaddr *)&ss, size, pbuf, sizeof(pbuf), 1892 NULL, 0, NI_NUMERICHOST|NI_WITHSCOPEID); 1893 (void) sprintf(buf, "%s [%s]", a, pbuf); 1894 } else 1895 (void) sprintf(buf, "%s", a); 1896 setproctitle("%s", buf); 1897 } 1898 #endif 1899 1900 1901 /* 1902 * Internet services provided internally by inetd: 1903 */ 1904 1905 int check_loop(sa, sep) 1906 struct sockaddr *sa; 1907 struct servtab *sep; 1908 { 1909 struct servtab *se2; 1910 char pname[INET6_ADDRSTRLEN]; 1911 1912 for (se2 = servtab; se2; se2 = se2->se_next) { 1913 if (!se2->se_bi || se2->se_socktype != SOCK_DGRAM) 1914 continue; 1915 1916 switch (se2->se_family) { 1917 case AF_INET: 1918 if (((struct sockaddr_in *)sa)->sin_port == 1919 se2->se_ctrladdr4.sin_port) 1920 goto isloop; 1921 continue; 1922 #ifdef INET6 1923 case AF_INET6: 1924 if (((struct sockaddr_in *)sa)->sin_port == 1925 se2->se_ctrladdr4.sin_port) 1926 goto isloop; 1927 continue; 1928 #endif 1929 default: 1930 continue; 1931 } 1932 isloop: 1933 getnameinfo(sa, sa->sa_len, pname, sizeof(pname), NULL, 0, 1934 NI_NUMERICHOST|NI_WITHSCOPEID); 1935 syslog(LOG_WARNING, "%s/%s:%s/%s loop request REFUSED from %s", 1936 sep->se_service, sep->se_proto, 1937 se2->se_service, se2->se_proto, 1938 pname); 1939 return 1; 1940 } 1941 return 0; 1942 } 1943 1944 /* 1945 * print_service: 1946 * Dump relevant information to stderr 1947 */ 1948 void 1949 print_service(action, sep) 1950 char *action; 1951 struct servtab *sep; 1952 { 1953 fprintf(stderr, 1954 "%s: %s proto=%s accept=%d max=%d user=%s group=%s" 1955 #ifdef LOGIN_CAP 1956 "class=%s" 1957 #endif 1958 " builtin=%p server=%s" 1959 #ifdef IPSEC 1960 " policy=\"%s\"" 1961 #endif 1962 "\n", 1963 action, sep->se_service, sep->se_proto, 1964 sep->se_accept, sep->se_maxchild, sep->se_user, sep->se_group, 1965 #ifdef LOGIN_CAP 1966 sep->se_class, 1967 #endif 1968 (void *) sep->se_bi, sep->se_server 1969 #ifdef IPSEC 1970 , (sep->se_policy ? sep->se_policy : "") 1971 #endif 1972 ); 1973 } 1974 1975 #define CPMHSIZE 256 1976 #define CPMHMASK (CPMHSIZE-1) 1977 #define CHTGRAN 10 1978 #define CHTSIZE 6 1979 1980 typedef struct CTime { 1981 unsigned long ct_Ticks; 1982 int ct_Count; 1983 } CTime; 1984 1985 typedef struct CHash { 1986 union { 1987 struct in_addr c4_Addr; 1988 struct in6_addr c6_Addr; 1989 } cu_Addr; 1990 #define ch_Addr4 cu_Addr.c4_Addr 1991 #define ch_Addr6 cu_Addr.c6_Addr 1992 int ch_Family; 1993 time_t ch_LTime; 1994 char *ch_Service; 1995 CTime ch_Times[CHTSIZE]; 1996 } CHash; 1997 1998 CHash CHashAry[CPMHSIZE]; 1999 2000 int 2001 cpmip(sep, ctrl) 2002 struct servtab *sep; 2003 int ctrl; 2004 { 2005 struct sockaddr_storage rss; 2006 int rssLen = sizeof(rss); 2007 int r = 0; 2008 2009 /* 2010 * If getpeername() fails, just let it through (if logging is 2011 * enabled the condition is caught elsewhere) 2012 */ 2013 2014 if (sep->se_maxcpm > 0 && 2015 getpeername(ctrl, (struct sockaddr *)&rss, &rssLen) == 0 ) { 2016 time_t t = time(NULL); 2017 int hv = 0xABC3D20F; 2018 int i; 2019 int cnt = 0; 2020 CHash *chBest = NULL; 2021 unsigned int ticks = t / CHTGRAN; 2022 struct sockaddr_in *sin; 2023 #ifdef INET6 2024 struct sockaddr_in6 *sin6; 2025 #endif 2026 2027 sin = (struct sockaddr_in *)&rss; 2028 #ifdef INET6 2029 sin6 = (struct sockaddr_in6 *)&rss; 2030 #endif 2031 { 2032 char *p; 2033 int i, addrlen; 2034 2035 switch (rss.ss_family) { 2036 case AF_INET: 2037 p = (char *)&sin->sin_addr; 2038 addrlen = sizeof(struct in_addr); 2039 break; 2040 #ifdef INET6 2041 case AF_INET6: 2042 p = (char *)&sin6->sin6_addr; 2043 addrlen = sizeof(struct in6_addr); 2044 break; 2045 #endif 2046 default: 2047 /* should not happen */ 2048 return -1; 2049 } 2050 2051 for (i = 0; i < addrlen; ++i, ++p) { 2052 hv = (hv << 5) ^ (hv >> 23) ^ *p; 2053 } 2054 hv = (hv ^ (hv >> 16)); 2055 } 2056 for (i = 0; i < 5; ++i) { 2057 CHash *ch = &CHashAry[(hv + i) & CPMHMASK]; 2058 2059 if (rss.ss_family == AF_INET && 2060 ch->ch_Family == AF_INET && 2061 sin->sin_addr.s_addr == ch->ch_Addr4.s_addr && 2062 ch->ch_Service && strcmp(sep->se_service, 2063 ch->ch_Service) == 0) { 2064 chBest = ch; 2065 break; 2066 } 2067 #ifdef INET6 2068 if (rss.ss_family == AF_INET6 && 2069 ch->ch_Family == AF_INET6 && 2070 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 2071 &ch->ch_Addr6) != 0 && 2072 ch->ch_Service && strcmp(sep->se_service, 2073 ch->ch_Service) == 0) { 2074 chBest = ch; 2075 break; 2076 } 2077 #endif 2078 if (chBest == NULL || ch->ch_LTime == 0 || 2079 ch->ch_LTime < chBest->ch_LTime) { 2080 chBest = ch; 2081 } 2082 } 2083 if ((rss.ss_family == AF_INET && 2084 (chBest->ch_Family != AF_INET || 2085 sin->sin_addr.s_addr != chBest->ch_Addr4.s_addr)) || 2086 chBest->ch_Service == NULL || 2087 strcmp(sep->se_service, chBest->ch_Service) != 0) { 2088 chBest->ch_Family = sin->sin_family; 2089 chBest->ch_Addr4 = sin->sin_addr; 2090 if (chBest->ch_Service) 2091 free(chBest->ch_Service); 2092 chBest->ch_Service = strdup(sep->se_service); 2093 bzero(chBest->ch_Times, sizeof(chBest->ch_Times)); 2094 } 2095 #ifdef INET6 2096 if ((rss.ss_family == AF_INET6 && 2097 (chBest->ch_Family != AF_INET6 || 2098 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 2099 &chBest->ch_Addr6) == 0)) || 2100 chBest->ch_Service == NULL || 2101 strcmp(sep->se_service, chBest->ch_Service) != 0) { 2102 chBest->ch_Family = sin6->sin6_family; 2103 chBest->ch_Addr6 = sin6->sin6_addr; 2104 if (chBest->ch_Service) 2105 free(chBest->ch_Service); 2106 chBest->ch_Service = strdup(sep->se_service); 2107 bzero(chBest->ch_Times, sizeof(chBest->ch_Times)); 2108 } 2109 #endif 2110 chBest->ch_LTime = t; 2111 { 2112 CTime *ct = &chBest->ch_Times[ticks % CHTSIZE]; 2113 if (ct->ct_Ticks != ticks) { 2114 ct->ct_Ticks = ticks; 2115 ct->ct_Count = 0; 2116 } 2117 ++ct->ct_Count; 2118 } 2119 for (i = 0; i < CHTSIZE; ++i) { 2120 CTime *ct = &chBest->ch_Times[i]; 2121 if (ct->ct_Ticks <= ticks && 2122 ct->ct_Ticks >= ticks - CHTSIZE) { 2123 cnt += ct->ct_Count; 2124 } 2125 } 2126 if (cnt * (CHTSIZE * CHTGRAN) / 60 > sep->se_maxcpm) { 2127 char pname[INET6_ADDRSTRLEN]; 2128 2129 getnameinfo((struct sockaddr *)&rss, 2130 ((struct sockaddr *)&rss)->sa_len, 2131 pname, sizeof(pname), NULL, 0, 2132 NI_NUMERICHOST|NI_WITHSCOPEID); 2133 r = -1; 2134 syslog(LOG_ERR, 2135 "%s from %s exceeded counts/min (limit %d/min)", 2136 sep->se_service, pname, 2137 sep->se_maxcpm); 2138 } 2139 } 2140 return(r); 2141 } 2142