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