2 * Copyright (c) 1983, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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";
42 static char sccsid[] = "@(#)from: inetd.c 8.4 (Berkeley) 4/13/94";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
50 * Inetd - Internet super-server
52 * This program invokes all internet services as needed. Connection-oriented
53 * services are invoked each time a connection is made, by creating a process.
54 * This process is passed the connection as file descriptor 0 and is expected
55 * to do a getpeername to find out the source host and port.
57 * Datagram oriented services are invoked when a datagram
58 * arrives; a process is created and passed a pending message
59 * on file descriptor 0. Datagram servers may either connect
60 * to their peer, freeing up the original socket for inetd
61 * to receive further messages on, or ``take over the socket'',
62 * processing all arriving datagrams and, eventually, timing
63 * out. The first type of server is said to be ``multi-threaded'';
64 * the second type of server ``single-threaded''.
66 * Inetd uses a configuration file which is read at startup
67 * and, possibly, at some later time in response to a hangup signal.
68 * The configuration file is ``free format'' with fields given in the
69 * order shown below. Continuation lines for an entry must begin with
70 * a space or tab. All fields must be present in each entry.
72 * service name must be in /etc/services
73 * or name a tcpmux service
74 * or specify a unix domain socket
75 * socket type stream/dgram/raw/rdm/seqpacket
76 * protocol tcp[4][6][/faith,ttcp], udp[4][6], unix
77 * wait/nowait single-threaded/multi-threaded
78 * user user to run daemon as
79 * server program full path name
80 * server program arguments maximum of MAXARGS (20)
82 * TCP services without official port numbers are handled with the
83 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for
84 * requests. When a connection is made from a foreign host, the service
85 * requested is passed to tcpmux, which looks it up in the servtab list
86 * and returns the proper entry for the service. Tcpmux returns a
87 * negative reply if the service doesn't exist, otherwise the invoked
88 * server is expected to return the positive reply if the service type in
89 * inetd.conf file has the prefix "tcpmux/". If the service type has the
90 * prefix "tcpmux/+", tcpmux will return the positive reply for the
91 * process; this is for compatibility with older server code, and also
92 * allows you to invoke programs that use stdin/stdout without putting any
93 * special server code in them. Services that use tcpmux are "nowait"
94 * because they do not have a well-known port and hence cannot listen
98 * service name/version must be in /etc/rpc
99 * socket type stream/dgram/raw/rdm/seqpacket
100 * protocol rpc/tcp, rpc/udp
101 * wait/nowait single-threaded/multi-threaded
102 * user user to run daemon as
103 * server program full path name
104 * server program arguments maximum of MAXARGS
106 * Comment lines are indicated by a `#' in column 1.
109 * Comment lines that start with "#@" denote IPsec policy string, as described
110 * in ipsec_set_policy(3). This will affect all the following items in
111 * inetd.conf(8). To reset the policy, just use "#@" line. By default,
112 * there's no IPsec policy.
115 #include <sys/param.h>
116 #include <sys/ioctl.h>
117 #include <sys/wait.h>
118 #include <sys/time.h>
119 #include <sys/resource.h>
120 #include <sys/stat.h>
123 #include <netinet/in.h>
124 #include <netinet/tcp.h>
125 #include <arpa/inet.h>
127 #include <rpc/pmap_clnt.h>
143 #include <sysexits.h>
147 #include "pathnames.h"
150 #include <netinet6/ipsec.h>
151 #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
156 /* wrapper for KAME-special getnameinfo() */
157 #ifndef NI_WITHSCOPEID
158 #define NI_WITHSCOPEID 0
161 #ifndef LIBWRAP_ALLOW_FACILITY
162 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
164 #ifndef LIBWRAP_ALLOW_SEVERITY
165 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
167 #ifndef LIBWRAP_DENY_FACILITY
168 # define LIBWRAP_DENY_FACILITY LOG_AUTH
170 #ifndef LIBWRAP_DENY_SEVERITY
171 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
174 #define ISWRAP(sep) \
175 ( ((wrap_ex && !(sep)->se_bi) || (wrap_bi && (sep)->se_bi)) \
176 && (sep->se_family == AF_INET || sep->se_family == AF_INET6) \
177 && ( ((sep)->se_accept && (sep)->se_socktype == SOCK_STREAM) \
178 || (sep)->se_socktype == SOCK_DGRAM))
181 #include <login_cap.h>
184 #define RESOURCE_RC "daemon"
189 #define MAXCHILD -1 /* maximum number of this service
194 #define MAXCPM -1 /* rate limit invocations from a
195 single remote address,
200 #define TOOMANY 256 /* don't start more than TOOMANY */
202 #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
203 #define RETRYTIME (60*10) /* retry after bind or server fail */
204 #define MAX_MAXCHLD 32767 /* max allowable max children */
206 #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
208 void close_sep(struct servtab *);
209 void flag_signal(int);
210 void flag_config(int);
212 int cpmip(const struct servtab *, int);
213 void endconfig(void);
214 struct servtab *enter(struct servtab *);
215 void freeconfig(struct servtab *);
216 struct servtab *getconfigent(void);
217 int matchservent(const char *, const char *, const char *);
218 char *nextline(FILE *);
219 void addchild(struct servtab *, int);
220 void flag_reapchild(int);
221 void reapchild(void);
222 void enable(struct servtab *);
223 void disable(struct servtab *);
224 void flag_retry(int);
227 void setup(struct servtab *);
229 void ipsecsetup(struct servtab *);
231 void unregisterrpc(register struct servtab *sep);
239 int maxsock; /* highest-numbered descriptor */
243 int toomany = TOOMANY;
244 int maxchild = MAXCHILD;
248 char *hostname = NULL;
249 struct sockaddr_in *bind_sa4;
252 struct sockaddr_in6 *bind_sa6;
263 struct servtab *servtab;
265 extern struct biltin biltins[];
267 const char *CONFIG = _PATH_INETDCONF;
268 const char *pid_file = _PATH_INETDPID;
271 getvalue(const char *arg, int *value, const char *whine)
276 tmp = strtol(arg, &p, 0);
278 syslog(LOG_ERR, whine, arg);
279 return 1; /* failure */
282 return 0; /* success */
286 main(int argc, char **argv)
291 struct sigaction sa, saalrm, sachld, sahup, sapipe;
292 int tmpint, ch, dofork;
296 login_cap_t *lc = NULL;
298 struct request_info req;
300 char *service = NULL;
302 struct sockaddr peer_un;
303 struct sockaddr_in peer_un4;
304 struct sockaddr_in6 peer_un6;
305 struct sockaddr_storage peer_max;
307 #define peer p_un.peer_un
308 #define peer4 p_un.peer_un4
309 #define peer6 p_un.peer_un6
310 #define peermax p_un.peer_max
312 struct addrinfo hints, *res;
313 const char *servname;
316 openlog("inetd", LOG_PID | LOG_NOWAIT | LOG_PERROR, LOG_DAEMON);
318 while ((ch = getopt(argc, argv, "dlwWR:a:c:C:p:")) != -1)
328 getvalue(optarg, &toomany,
329 "-R %s: bad value for service invocation rate");
332 getvalue(optarg, &maxchild,
333 "-c %s: bad value for maximum children");
336 getvalue(optarg, &maxcpm,
337 "-C %s: bad value for maximum children/minute");
354 "usage: inetd [-dlwW] [-a address] [-R rate]"
355 " [-c maximum] [-C rate]"
356 " [-p pidfile] [conf-file]");
360 * Initialize Bind Addrs.
361 * When hostname is NULL, wild card bind addrs are obtained from
362 * getaddrinfo(). But getaddrinfo() requires at least one of
363 * hostname or servname is non NULL.
364 * So when hostname is NULL, set dummy value to servname.
366 servname = (hostname == NULL) ? "discard" /* dummy */ : NULL;
368 bzero(&hints, sizeof(struct addrinfo));
369 hints.ai_flags = AI_PASSIVE;
370 hints.ai_family = AF_UNSPEC;
371 error = getaddrinfo(hostname, servname, &hints, &res);
373 syslog(LOG_ERR, "-a %s: %s", hostname, gai_strerror(error));
374 if (error == EAI_SYSTEM)
375 syslog(LOG_ERR, "%s", strerror(errno));
379 if (res->ai_addr == NULL) {
380 syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
383 switch (res->ai_addr->sa_family) {
387 bind_sa4 = (struct sockaddr_in *)res->ai_addr;
388 /* init port num in case servname is dummy */
389 bind_sa4->sin_port = 0;
396 bind_sa6 = (struct sockaddr_in6 *)res->ai_addr;
397 /* init port num in case servname is dummy */
398 bind_sa6->sin6_port = 0;
409 } while ((res = res->ai_next) != NULL);
415 syslog(LOG_ERR, "-a %s: unknown address family", hostname);
421 umask(mask = umask(0777));
430 if (daemon(0, 0) < 0) {
431 syslog(LOG_WARNING, "daemon(0,0) failed: %m");
433 /* From now on we don't want syslog messages going to stderr. */
435 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
437 * In case somebody has started inetd manually, we need to
438 * clear the logname, so that old servers run as root do not
439 * get the user's logname..
441 if (setlogin("") < 0) {
442 syslog(LOG_WARNING, "cannot clear logname: %m");
443 /* no big deal if it fails.. */
446 fp = fopen(pid_file, "w");
448 fprintf(fp, "%ld\n", (long)pid);
451 syslog(LOG_WARNING, "%s: %m", pid_file);
455 sigemptyset(&sa.sa_mask);
456 sigaddset(&sa.sa_mask, SIGALRM);
457 sigaddset(&sa.sa_mask, SIGCHLD);
458 sigaddset(&sa.sa_mask, SIGHUP);
459 sa.sa_handler = flag_retry;
460 sigaction(SIGALRM, &sa, &saalrm);
462 sa.sa_handler = flag_config;
463 sigaction(SIGHUP, &sa, &sahup);
464 sa.sa_handler = flag_reapchild;
465 sigaction(SIGCHLD, &sa, &sachld);
466 sa.sa_handler = SIG_IGN;
467 sigaction(SIGPIPE, &sa, &sapipe);
470 /* space for daemons to overwrite environment for ps */
471 #define DUMMYSIZE 100
472 char dummy[DUMMYSIZE];
474 (void)memset(dummy, 'x', DUMMYSIZE - 1);
475 dummy[DUMMYSIZE - 1] = '\0';
476 (void)setenv("inetd_dummy", dummy, 1);
479 if (pipe(signalpipe) != 0) {
480 syslog(LOG_ERR, "pipe: %m");
483 FD_SET(signalpipe[0], &allsock);
487 if (signalpipe[0] > maxsock)
488 maxsock = signalpipe[0];
489 if (signalpipe[1] > maxsock)
490 maxsock = signalpipe[1];
498 syslog(LOG_ERR, "%s: nsock=0", __FUNCTION__);
503 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
504 (fd_set *)0, (struct timeval *)0)) <= 0) {
505 if (n < 0 && errno != EINTR) {
506 syslog(LOG_WARNING, "select: %m");
511 /* handle any queued signal flags */
512 if (FD_ISSET(signalpipe[0], &readable)) {
514 if (ioctl(signalpipe[0], FIONREAD, &nsig) != 0) {
515 syslog(LOG_ERR, "ioctl: %m");
518 while (--nsig >= 0) {
520 if (read(signalpipe[0], &c, 1) != 1) {
521 syslog(LOG_ERR, "read: %m");
525 warnx("handling signal flag %c", c);
527 case 'A': /* sigalrm */
530 case 'C': /* sigchld */
533 case 'H': /* sighup */
539 for (sep = servtab; n && sep; sep = sep->se_next)
540 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
543 warnx("someone wants %s", sep->se_service);
544 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) {
546 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
547 syslog(LOG_ERR, "ioctl (FIONBIO, 1): %m");
548 ctrl = accept(sep->se_fd, (struct sockaddr *)0,
551 warnx("accept, ctrl %d", ctrl);
555 "accept (for %s): %m",
557 if (sep->se_accept &&
558 sep->se_socktype == SOCK_STREAM)
563 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
564 syslog(LOG_ERR, "ioctl1(FIONBIO, 0): %m");
565 if (ioctl(ctrl, FIONBIO, &i) < 0)
566 syslog(LOG_ERR, "ioctl2(FIONBIO, 0): %m");
567 if (cpmip(sep, ctrl) < 0) {
573 if (log && !ISWRAP(sep)) {
574 char pname[INET6_ADDRSTRLEN] = "unknown";
577 if (getpeername(ctrl, (struct sockaddr *)
580 if (recvfrom(ctrl, buf, sizeof(buf),
582 (struct sockaddr *)&peermax,
584 getnameinfo((struct sockaddr *)&peermax,
586 pname, sizeof(pname),
592 getnameinfo((struct sockaddr *)&peermax,
594 pname, sizeof(pname),
599 syslog(LOG_INFO,"%s from %s", sep->se_service, pname);
601 (void) sigblock(SIGBLOCK);
604 * Fork for all external services, builtins which need to
605 * fork and anything we're wrapping (as wrapping might
606 * block or use hosts_options(5) twist).
608 dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep);
610 if (sep->se_count++ == 0)
611 (void)gettimeofday(&sep->se_time, (struct timezone *)NULL);
612 else if (toomany > 0 && sep->se_count >= toomany) {
615 (void)gettimeofday(&now, (struct timezone *)NULL);
616 if (now.tv_sec - sep->se_time.tv_sec >
622 "%s/%s server failing (looping), service terminated",
623 sep->se_service, sep->se_proto);
624 if (sep->se_accept &&
625 sep->se_socktype == SOCK_STREAM)
639 syslog(LOG_ERR, "fork: %m");
640 if (sep->se_accept &&
641 sep->se_socktype == SOCK_STREAM)
653 warnx("+ closing from %d", maxsock);
654 for (tmpint = maxsock; tmpint > 2; tmpint--)
656 (void) close(tmpint);
657 sigaction(SIGALRM, &saalrm, (struct sigaction *)0);
658 sigaction(SIGCHLD, &sachld, (struct sigaction *)0);
659 sigaction(SIGHUP, &sahup, (struct sigaction *)0);
660 /* SIGPIPE reset before exec */
663 * Call tcpmux to find the real service to exec.
666 sep->se_bi->bi_fn == (bi_fn_t *) tcpmux) {
674 inetd_setproctitle("wrapping", ctrl);
675 service = sep->se_server_name ?
676 sep->se_server_name : sep->se_service;
677 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, NULL);
679 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
680 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
681 denied = !hosts_access(&req);
683 syslog(deny_severity,
684 "refused connection from %.500s, service %s (%s%s)",
685 eval_client(&req), service, sep->se_proto,
686 (((struct sockaddr *)req.client->sin)->sa_family == AF_INET6 && !IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)req.client->sin)->sin6_addr)) ? "6" : "");
687 if (sep->se_socktype != SOCK_STREAM)
688 recv(ctrl, buf, sizeof (buf), 0);
695 syslog(allow_severity,
696 "connection from %.500s, service %s (%s%s)",
697 eval_client(&req), service, sep->se_proto,
698 (((struct sockaddr *)req.client->sin)->sa_family == AF_INET6 && !IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)req.client->sin)->sin6_addr)) ? "6" : "");
702 (*sep->se_bi->bi_fn)(ctrl, sep);
706 getpid(), sep->se_server);
711 if ((pwd = getpwnam(sep->se_user)) == NULL) {
713 "%s/%s: %s: no such user",
714 sep->se_service, sep->se_proto,
716 if (sep->se_socktype != SOCK_STREAM)
717 recv(0, buf, sizeof (buf), 0);
721 if ( sep->se_group != NULL
722 && (grp = getgrnam(sep->se_group)) == NULL
725 "%s/%s: %s: no such group",
726 sep->se_service, sep->se_proto,
728 if (sep->se_socktype != SOCK_STREAM)
729 recv(0, buf, sizeof (buf), 0);
733 pwd->pw_gid = grp->gr_gid;
735 if ((lc = login_getclass(sep->se_class)) == NULL) {
736 /* error syslogged by getclass */
738 "%s/%s: %s: login class error",
739 sep->se_service, sep->se_proto,
741 if (sep->se_socktype != SOCK_STREAM)
742 recv(0, buf, sizeof (buf), 0);
748 "%s: can't setsid(): %m",
750 /* _exit(EX_OSERR); not fatal yet */
753 if (setusercontext(lc, pwd, pwd->pw_uid,
754 LOGIN_SETALL) != 0) {
756 "%s: can't setusercontext(..%s..): %m",
757 sep->se_service, sep->se_user);
762 if (setlogin(sep->se_user) < 0) {
764 "%s: can't setlogin(%s): %m",
765 sep->se_service, sep->se_user);
766 /* _exit(EX_OSERR); not yet */
768 if (setgid(pwd->pw_gid) < 0) {
770 "%s: can't set gid %d: %m",
771 sep->se_service, pwd->pw_gid);
774 (void) initgroups(pwd->pw_name,
776 if (setuid(pwd->pw_uid) < 0) {
778 "%s: can't set uid %d: %m",
779 sep->se_service, pwd->pw_uid);
784 sigaction(SIGPIPE, &sapipe,
785 (struct sigaction *)0);
786 execv(sep->se_server, sep->se_argv);
788 "cannot execute %s: %m", sep->se_server);
789 if (sep->se_socktype != SOCK_STREAM)
790 recv(0, buf, sizeof (buf), 0);
795 if (sep->se_accept && sep->se_socktype == SOCK_STREAM)
802 * Add a signal flag to the signal flag queue for later handling
810 if (write(signalpipe[1], &ch, 1) != 1) {
811 syslog(LOG_ERR, "write: %m");
817 * Record a new child pid for this service. If we've reached the
818 * limit on children, then stop accepting incoming requests.
822 addchild(struct servtab *sep, pid_t pid)
824 if (sep->se_maxchild <= 0)
827 if (sep->se_numchild >= sep->se_maxchild) {
828 syslog(LOG_ERR, "%s: %d >= %d",
829 __FUNCTION__, sep->se_numchild, sep->se_maxchild);
833 sep->se_pids[sep->se_numchild++] = pid;
834 if (sep->se_numchild == sep->se_maxchild)
839 * Some child process has exited. See if it's on somebody's list.
843 flag_reapchild(int signo __unused)
856 pid = wait3(&status, WNOHANG, (struct rusage *)0);
860 warnx("%d reaped, status %#x", pid, status);
861 for (sep = servtab; sep; sep = sep->se_next) {
862 for (k = 0; k < sep->se_numchild; k++)
863 if (sep->se_pids[k] == pid)
865 if (k == sep->se_numchild)
867 if (sep->se_numchild == sep->se_maxchild)
869 sep->se_pids[k] = sep->se_pids[--sep->se_numchild];
872 "%s[%d]: exit status 0x%x",
873 sep->se_server, pid, status);
880 flag_config(int signo __unused)
888 struct servtab *sep, *new, **sepp;
892 syslog(LOG_ERR, "%s: %m", CONFIG);
895 for (sep = servtab; sep; sep = sep->se_next)
897 while ((new = getconfigent())) {
898 if (getpwnam(new->se_user) == NULL) {
900 "%s/%s: no such user '%s', service ignored",
901 new->se_service, new->se_proto, new->se_user);
904 if (new->se_group && getgrnam(new->se_group) == NULL) {
906 "%s/%s: no such group '%s', service ignored",
907 new->se_service, new->se_proto, new->se_group);
911 if (login_getclass(new->se_class) == NULL) {
912 /* error syslogged by getclass */
914 "%s/%s: %s: login class error, service ignored",
915 new->se_service, new->se_proto, new->se_class);
919 for (sep = servtab; sep; sep = sep->se_next)
920 if (strcmp(sep->se_service, new->se_service) == 0 &&
921 strcmp(sep->se_proto, new->se_proto) == 0 &&
922 sep->se_socktype == new->se_socktype &&
923 sep->se_family == new->se_family)
928 #define SWAP(t,a, b) { t c = a; a = b; b = c; }
929 omask = sigblock(SIGBLOCK);
930 if (sep->se_nomapped != new->se_nomapped) {
931 sep->se_nomapped = new->se_nomapped;
934 /* copy over outstanding child pids */
935 if (sep->se_maxchild > 0 && new->se_maxchild > 0) {
936 new->se_numchild = sep->se_numchild;
937 if (new->se_numchild > new->se_maxchild)
938 new->se_numchild = new->se_maxchild;
939 memcpy(new->se_pids, sep->se_pids,
940 new->se_numchild * sizeof(*new->se_pids));
942 SWAP(pid_t *, sep->se_pids, new->se_pids);
943 sep->se_maxchild = new->se_maxchild;
944 sep->se_numchild = new->se_numchild;
945 sep->se_maxcpm = new->se_maxcpm;
946 sep->se_bi = new->se_bi;
947 /* might need to turn on or off service now */
948 if (sep->se_fd >= 0) {
949 if (sep->se_maxchild > 0
950 && sep->se_numchild == sep->se_maxchild) {
951 if (FD_ISSET(sep->se_fd, &allsock))
954 if (!FD_ISSET(sep->se_fd, &allsock))
958 sep->se_accept = new->se_accept;
959 SWAP(char *, sep->se_user, new->se_user);
960 SWAP(char *, sep->se_group, new->se_group);
962 SWAP(char *, sep->se_class, new->se_class);
964 SWAP(char *, sep->se_server, new->se_server);
965 SWAP(char *, sep->se_server_name, new->se_server_name);
966 for (i = 0; i < MAXARGV; i++)
967 SWAP(char *, sep->se_argv[i], new->se_argv[i]);
969 SWAP(char *, sep->se_policy, new->se_policy);
975 print_service("REDO", sep);
979 print_service("ADD ", sep);
986 switch (sep->se_family) {
988 if (no_v4bind != 0) {
995 if (no_v6bind != 0) {
1003 if (sep->se_family != AF_UNIX) {
1004 sp = getservbyname(sep->se_service, sep->se_proto);
1006 syslog(LOG_ERR, "%s/%s: unknown service",
1007 sep->se_service, sep->se_proto);
1008 sep->se_checked = 0;
1012 switch (sep->se_family) {
1014 if (sp->s_port != sep->se_ctrladdr4.sin_port) {
1015 sep->se_ctrladdr4.sin_port =
1023 sep->se_ctrladdr6.sin6_port) {
1024 sep->se_ctrladdr6.sin6_port =
1031 if (sep->se_reset != 0 && sep->se_fd >= 0)
1034 rpc = getrpcbyname(sep->se_service);
1036 syslog(LOG_ERR, "%s/%s unknown RPC service",
1037 sep->se_service, sep->se_proto);
1038 if (sep->se_fd != -1)
1039 (void) close(sep->se_fd);
1043 if (rpc->r_number != sep->se_rpc_prog) {
1044 if (sep->se_rpc_prog)
1046 sep->se_rpc_prog = rpc->r_number;
1047 if (sep->se_fd != -1)
1048 (void) close(sep->se_fd);
1052 if (sep->se_fd == -1)
1057 * Purge anything not looked at above.
1059 omask = sigblock(SIGBLOCK);
1061 while ((sep = *sepp)) {
1062 if (sep->se_checked) {
1063 sepp = &sep->se_next;
1066 *sepp = sep->se_next;
1067 if (sep->se_fd >= 0)
1070 print_service("FREE", sep);
1071 if (sep->se_rpc && sep->se_rpc_prog > 0)
1076 (void) sigsetmask(omask);
1080 unregisterrpc(struct servtab *sep)
1083 struct servtab *sepp;
1086 omask = sigblock(SIGBLOCK);
1087 for (sepp = servtab; sepp; sepp = sepp->se_next) {
1090 if (sep->se_checked == 0 ||
1092 sep->se_rpc_prog != sepp->se_rpc_prog)
1097 print_service("UNREG", sep);
1098 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++)
1099 pmap_unset(sep->se_rpc_prog, i);
1100 if (sep->se_fd != -1)
1101 (void) close(sep->se_fd);
1103 (void) sigsetmask(omask);
1107 flag_retry(int signo __unused)
1115 struct servtab *sep;
1118 for (sep = servtab; sep; sep = sep->se_next)
1119 if (sep->se_fd == -1 && !ISMUX(sep))
1124 setup(struct servtab *sep)
1128 if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
1130 warn("socket failed on %s/%s",
1131 sep->se_service, sep->se_proto);
1132 syslog(LOG_ERR, "%s/%s: socket: %m",
1133 sep->se_service, sep->se_proto);
1136 #define turnon(fd, opt) \
1137 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
1138 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
1139 turnon(sep->se_fd, SO_DEBUG) < 0)
1140 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
1141 if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
1142 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
1144 if (turnon(sep->se_fd, SO_PRIVSTATE) < 0)
1145 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m");
1147 /* tftpd opens a new connection then needs more infos */
1148 if ((sep->se_family == AF_INET6) &&
1149 (strcmp(sep->se_proto, "udp") == 0) &&
1150 (sep->se_accept == 0) &&
1151 (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_PKTINFO,
1152 (char *)&on, sizeof (on)) < 0))
1153 syslog(LOG_ERR, "setsockopt (IPV6_RECVPKTINFO): %m");
1154 #ifdef IPV6_BINDV6ONLY
1155 if (sep->se_family == AF_INET6) {
1156 int flag = sep->se_nomapped ? 1 : 0;
1157 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_BINDV6ONLY,
1158 (char *)&flag, sizeof (flag)) < 0)
1159 syslog(LOG_ERR, "setsockopt (IPV6_BINDV6ONLY): %m");
1161 #endif /* IPV6_BINDV6ONLY */
1163 if (sep->se_type == TTCP_TYPE)
1164 if (setsockopt(sep->se_fd, IPPROTO_TCP, TCP_NOPUSH,
1165 (char *)&on, sizeof (on)) < 0)
1166 syslog(LOG_ERR, "setsockopt (TCP_NOPUSH): %m");
1168 if (sep->se_type == FAITH_TYPE) {
1169 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_FAITH, &on,
1171 syslog(LOG_ERR, "setsockopt (IPV6_FAITH): %m");
1178 if (sep->se_family == AF_UNIX) {
1179 (void) unlink(sep->se_ctrladdr_un.sun_path);
1180 umask(0777); /* Make socket with conservative permissions */
1182 if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr,
1183 sep->se_ctrladdr_size) < 0) {
1185 warn("bind failed on %s/%s",
1186 sep->se_service, sep->se_proto);
1187 syslog(LOG_ERR, "%s/%s: bind: %m",
1188 sep->se_service, sep->se_proto);
1189 (void) close(sep->se_fd);
1195 if (sep->se_family == AF_UNIX)
1199 if (sep->se_family == AF_UNIX) {
1200 /* Ick - fch{own,mod} don't work on Unix domain sockets */
1201 if (chown(sep->se_service, sep->se_sockuid, sep->se_sockgid) < 0)
1202 syslog(LOG_ERR, "chown socket: %m");
1203 if (chmod(sep->se_service, sep->se_sockmode) < 0)
1204 syslog(LOG_ERR, "chmod socket: %m");
1209 socklen_t len = sep->se_ctrladdr_size;
1211 if (sep->se_family != AF_INET) {
1213 "%s/%s: unsupported address family for rpc",
1214 sep->se_service, sep->se_proto);
1215 (void) close(sep->se_fd);
1219 if (getsockname(sep->se_fd,
1220 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){
1221 syslog(LOG_ERR, "%s/%s: getsockname: %m",
1222 sep->se_service, sep->se_proto);
1223 (void) close(sep->se_fd);
1228 print_service("REG ", sep);
1229 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1230 pmap_unset(sep->se_rpc_prog, i);
1231 pmap_set(sep->se_rpc_prog, i,
1232 (sep->se_socktype == SOCK_DGRAM)
1233 ? IPPROTO_UDP : IPPROTO_TCP,
1234 ntohs(sep->se_ctrladdr4.sin_port));
1237 if (sep->se_socktype == SOCK_STREAM)
1238 listen(sep->se_fd, 64);
1241 warnx("registered %s on %d",
1242 sep->se_server, sep->se_fd);
1249 struct servtab *sep;
1252 char *policy_in = NULL;
1253 char *policy_out = NULL;
1257 switch (sep->se_family) {
1260 opt = IP_IPSEC_POLICY;
1264 level = IPPROTO_IPV6;
1265 opt = IPV6_IPSEC_POLICY;
1272 if (!sep->se_policy || sep->se_policy[0] == '\0') {
1273 static char def_in[] = "in entrust", def_out[] = "out entrust";
1275 policy_out = def_out;
1277 if (!strncmp("in", sep->se_policy, 2))
1278 policy_in = sep->se_policy;
1279 else if (!strncmp("out", sep->se_policy, 3))
1280 policy_out = sep->se_policy;
1282 syslog(LOG_ERR, "invalid security policy \"%s\"",
1288 if (policy_in != NULL) {
1289 buf = ipsec_set_policy(policy_in, strlen(policy_in));
1291 if (setsockopt(sep->se_fd, level, opt,
1292 buf, ipsec_get_policylen(buf)) < 0 &&
1294 warnx("%s/%s: ipsec initialization failed; %s",
1295 sep->se_service, sep->se_proto,
1299 syslog(LOG_ERR, "invalid security policy \"%s\"",
1302 if (policy_out != NULL) {
1303 buf = ipsec_set_policy(policy_out, strlen(policy_out));
1305 if (setsockopt(sep->se_fd, level, opt,
1306 buf, ipsec_get_policylen(buf)) < 0 &&
1308 warnx("%s/%s: ipsec initialization failed; %s",
1309 sep->se_service, sep->se_proto,
1313 syslog(LOG_ERR, "invalid security policy \"%s\"",
1320 * Finish with a service and its socket.
1323 close_sep(struct servtab *sep)
1325 if (sep->se_fd >= 0) {
1326 if (FD_ISSET(sep->se_fd, &allsock))
1328 (void) close(sep->se_fd);
1332 sep->se_numchild = 0; /* forget about any existing children */
1336 matchservent(const char *name1, const char *name2, const char *proto)
1341 if (strcmp(proto, "unix") == 0) {
1342 if ((p = strrchr(name1, '/')) != NULL)
1344 if ((p = strrchr(name2, '/')) != NULL)
1347 if (strcmp(name1, name2) == 0)
1349 if ((se = getservbyname(name1, proto)) != NULL) {
1350 if (strcmp(name2, se->s_name) == 0)
1352 for (alias = se->s_aliases; *alias; alias++)
1353 if (strcmp(name2, *alias) == 0)
1360 enter(struct servtab *cp)
1362 struct servtab *sep;
1365 sep = (struct servtab *)malloc(sizeof (*sep));
1366 if (sep == (struct servtab *)0) {
1367 syslog(LOG_ERR, "malloc: %m");
1372 omask = sigblock(SIGBLOCK);
1373 sep->se_next = servtab;
1380 enable(struct servtab *sep)
1384 "enabling %s, fd %d", sep->se_service, sep->se_fd);
1386 if (sep->se_fd < 0) {
1388 "%s: %s: bad fd", __FUNCTION__, sep->se_service);
1393 "%s: %s: is mux", __FUNCTION__, sep->se_service);
1396 if (FD_ISSET(sep->se_fd, &allsock)) {
1398 "%s: %s: not off", __FUNCTION__, sep->se_service);
1403 FD_SET(sep->se_fd, &allsock);
1404 if (sep->se_fd > maxsock)
1405 maxsock = sep->se_fd;
1409 disable(struct servtab *sep)
1413 "disabling %s, fd %d", sep->se_service, sep->se_fd);
1415 if (sep->se_fd < 0) {
1417 "%s: %s: bad fd", __FUNCTION__, sep->se_service);
1422 "%s: %s: is mux", __FUNCTION__, sep->se_service);
1425 if (!FD_ISSET(sep->se_fd, &allsock)) {
1427 "%s: %s: not on", __FUNCTION__, sep->se_service);
1431 syslog(LOG_ERR, "%s: nsock=0", __FUNCTION__);
1436 FD_CLR(sep->se_fd, &allsock);
1437 if (sep->se_fd == maxsock)
1441 FILE *fconfig = NULL;
1442 struct servtab serv;
1443 char line[LINE_MAX];
1449 if (fconfig != NULL) {
1450 fseek(fconfig, 0L, SEEK_SET);
1453 fconfig = fopen(CONFIG, "r");
1454 return (fconfig != NULL);
1461 (void) fclose(fconfig);
1469 struct servtab *sep = &serv;
1473 static char TCPMUX_TOKEN[] = "tcpmux/";
1474 #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
1476 char *policy = NULL;
1484 while ((cp = nextline(fconfig)) != NULL) {
1486 /* lines starting with #@ is not a comment, but the policy */
1487 if (cp[0] == '#' && cp[1] == '@') {
1489 for (p = cp + 2; p && *p && isspace(*p); p++)
1495 } else if (ipsec_get_policylen(p) >= 0) {
1501 "%s: invalid ipsec policy \"%s\"",
1507 if (*cp == '#' || *cp == '\0')
1512 return ((struct servtab *)0);
1514 * clear the static buffer, since some fields (se_ctrladdr,
1515 * for example) don't get initialized here.
1517 memset(sep, 0, sizeof *sep);
1520 /* got an empty line containing just blanks/tabs. */
1523 if (arg[0] == ':') { /* :user:group:perm: */
1524 char *user, *group, *perm;
1528 if ((group = strchr(user, ':')) == NULL) {
1529 syslog(LOG_ERR, "no group after user '%s'", user);
1533 if ((perm = strchr(group, ':')) == NULL) {
1534 syslog(LOG_ERR, "no mode after group '%s'", group);
1538 if ((pw = getpwnam(user)) == NULL) {
1539 syslog(LOG_ERR, "no such user '%s'", user);
1542 sep->se_sockuid = pw->pw_uid;
1543 if ((gr = getgrnam(group)) == NULL) {
1544 syslog(LOG_ERR, "no such user '%s'", group);
1547 sep->se_sockgid = gr->gr_gid;
1548 sep->se_sockmode = strtol(perm, &arg, 8);
1550 syslog(LOG_ERR, "bad mode '%s'", perm);
1555 sep->se_sockuid = euid;
1556 sep->se_sockgid = egid;
1557 sep->se_sockmode = 0200;
1559 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
1560 char *c = arg + MUX_LEN;
1562 sep->se_type = MUXPLUS_TYPE;
1565 sep->se_type = MUX_TYPE;
1566 sep->se_service = newstr(c);
1568 sep->se_service = newstr(arg);
1569 sep->se_type = NORM_TYPE;
1572 if (strcmp(arg, "stream") == 0)
1573 sep->se_socktype = SOCK_STREAM;
1574 else if (strcmp(arg, "dgram") == 0)
1575 sep->se_socktype = SOCK_DGRAM;
1576 else if (strcmp(arg, "rdm") == 0)
1577 sep->se_socktype = SOCK_RDM;
1578 else if (strcmp(arg, "seqpacket") == 0)
1579 sep->se_socktype = SOCK_SEQPACKET;
1580 else if (strcmp(arg, "raw") == 0)
1581 sep->se_socktype = SOCK_RAW;
1583 sep->se_socktype = -1;
1586 if (strncmp(arg, "tcp", 3) == 0) {
1587 sep->se_proto = newstr(strsep(&arg, "/"));
1589 if (strcmp(arg, "ttcp") == 0)
1590 sep->se_type = TTCP_TYPE;
1591 else if (strcmp(arg, "faith") == 0)
1592 sep->se_type = FAITH_TYPE;
1595 if (sep->se_type == NORM_TYPE &&
1596 strncmp(arg, "faith/", 6) == 0) {
1598 sep->se_type = FAITH_TYPE;
1600 sep->se_proto = newstr(arg);
1602 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1603 if (no_v4bind != 0) {
1604 syslog(LOG_NOTICE, "IPv4 bind is ignored for %s",
1609 memmove(sep->se_proto, sep->se_proto + 4,
1610 strlen(sep->se_proto) + 1 - 4);
1612 sep->se_rpc_prog = sep->se_rpc_lowvers =
1613 sep->se_rpc_lowvers = 0;
1614 memcpy(&sep->se_ctrladdr4, bind_sa4,
1615 sizeof(sep->se_ctrladdr4));
1616 if ((versp = rindex(sep->se_service, '/'))) {
1618 switch (sscanf(versp, "%d-%d",
1619 &sep->se_rpc_lowvers,
1620 &sep->se_rpc_highvers)) {
1624 sep->se_rpc_highvers =
1625 sep->se_rpc_lowvers;
1629 "bad RPC version specifier; %s",
1636 sep->se_rpc_lowvers =
1637 sep->se_rpc_highvers = 1;
1640 sep->se_nomapped = 0;
1641 while (isdigit(sep->se_proto[strlen(sep->se_proto) - 1])) {
1643 if (sep->se_proto[strlen(sep->se_proto) - 1] == '6') {
1644 if (no_v6bind != 0) {
1645 syslog(LOG_NOTICE, "IPv6 bind is ignored for %s",
1650 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1655 if (sep->se_proto[strlen(sep->se_proto) - 1] == '4') {
1656 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1660 /* illegal version num */
1661 syslog(LOG_ERR, "bad IP version for %s", sep->se_proto);
1665 if (strcmp(sep->se_proto, "unix") == 0) {
1666 sep->se_family = AF_UNIX;
1670 sep->se_family = AF_INET6;
1671 if (v4bind == 0 || no_v4bind != 0)
1672 sep->se_nomapped = 1;
1675 { /* default to v4 bind if not v6 bind */
1676 if (no_v4bind != 0) {
1677 syslog(LOG_NOTICE, "IPv4 bind is ignored for %s",
1682 sep->se_family = AF_INET;
1685 switch(sep->se_family) {
1687 memcpy(&sep->se_ctrladdr4, bind_sa4,
1688 sizeof(sep->se_ctrladdr4));
1689 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr4);
1693 memcpy(&sep->se_ctrladdr6, bind_sa6,
1694 sizeof(sep->se_ctrladdr6));
1695 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr6);
1699 if (strlen(sep->se_service) >= sizeof(sep->se_ctrladdr_un.sun_path)) {
1701 "domain socket pathname too long for service %s",
1705 memset(&sep->se_ctrladdr, 0, sizeof(sep->se_ctrladdr));
1706 sep->se_ctrladdr_un.sun_family = sep->se_family;
1707 sep->se_ctrladdr_un.sun_len = strlen(sep->se_service);
1708 strcpy(sep->se_ctrladdr_un.sun_path, sep->se_service);
1709 sep->se_ctrladdr_size = SUN_LEN(&sep->se_ctrladdr_un);
1712 if (!strncmp(arg, "wait", 4))
1714 else if (!strncmp(arg, "nowait", 6))
1718 "%s: bad wait/nowait for service %s",
1719 CONFIG, sep->se_service);
1722 sep->se_maxchild = -1;
1723 sep->se_maxcpm = -1;
1724 if ((s = strchr(arg, '/')) != NULL) {
1728 val = strtoul(s + 1, &eptr, 10);
1729 if (eptr == s + 1 || val > MAX_MAXCHLD) {
1731 "%s: bad max-child for service %s",
1732 CONFIG, sep->se_service);
1736 if (!sep->se_accept && val != 1)
1737 warnx("maxchild=%lu for wait service %s"
1738 " not recommended", val, sep->se_service);
1739 sep->se_maxchild = val;
1741 sep->se_maxcpm = strtol(eptr + 1, &eptr, 10);
1743 * explicitly do not check for \0 for future expansion /
1744 * backwards compatibility
1749 * Silently enforce "nowait" mode for TCPMUX services
1750 * since they don't have an assigned port to listen on.
1753 if (strcmp(sep->se_proto, "tcp")) {
1755 "%s: bad protocol for tcpmux service %s",
1756 CONFIG, sep->se_service);
1759 if (sep->se_socktype != SOCK_STREAM) {
1761 "%s: bad socket type for tcpmux service %s",
1762 CONFIG, sep->se_service);
1766 sep->se_user = newstr(sskip(&cp));
1768 if ((s = strrchr(sep->se_user, '/')) != NULL) {
1770 sep->se_class = newstr(s + 1);
1772 sep->se_class = newstr(RESOURCE_RC);
1774 if ((s = strrchr(sep->se_user, ':')) != NULL) {
1776 sep->se_group = newstr(s + 1);
1778 sep->se_group = NULL;
1779 sep->se_server = newstr(sskip(&cp));
1780 if ((sep->se_server_name = rindex(sep->se_server, '/')))
1781 sep->se_server_name++;
1782 if (strcmp(sep->se_server, "internal") == 0) {
1785 for (bi = biltins; bi->bi_service; bi++)
1786 if (bi->bi_socktype == sep->se_socktype &&
1787 matchservent(bi->bi_service, sep->se_service,
1790 if (bi->bi_service == 0) {
1791 syslog(LOG_ERR, "internal service %s unknown",
1795 sep->se_accept = 1; /* force accept mode for built-ins */
1799 if (sep->se_maxcpm < 0)
1800 sep->se_maxcpm = maxcpm;
1801 if (sep->se_maxchild < 0) { /* apply default max-children */
1802 if (sep->se_bi && sep->se_bi->bi_maxchild >= 0)
1803 sep->se_maxchild = sep->se_bi->bi_maxchild;
1804 else if (sep->se_accept)
1805 sep->se_maxchild = maxchild > 0 ? maxchild : 0;
1807 sep->se_maxchild = 1;
1809 if (sep->se_maxchild > 0) {
1810 sep->se_pids = malloc(sep->se_maxchild * sizeof(*sep->se_pids));
1811 if (sep->se_pids == NULL) {
1812 syslog(LOG_ERR, "malloc: %m");
1817 for (arg = skip(&cp); cp; arg = skip(&cp))
1818 if (argc < MAXARGV) {
1819 sep->se_argv[argc++] = newstr(arg);
1822 "%s: too many arguments for service %s",
1823 CONFIG, sep->se_service);
1826 while (argc <= MAXARGV)
1827 sep->se_argv[argc++] = NULL;
1829 sep->se_policy = policy ? newstr(policy) : NULL;
1835 freeconfig(struct servtab *cp)
1840 free(cp->se_service);
1852 free(cp->se_server);
1855 for (i = 0; i < MAXARGV; i++)
1857 free(cp->se_argv[i]);
1860 free(cp->se_policy);
1866 * Safe skip - if skip returns null, log a syntax error in the
1867 * configuration file and exit.
1876 syslog(LOG_ERR, "%s: syntax error", CONFIG);
1890 while (*cp == ' ' || *cp == '\t')
1896 (void) ungetc(c, fconfig);
1897 if (c == ' ' || c == '\t')
1898 if ((cp = nextline(fconfig)))
1903 if (*cp == '"' || *cp == '\'')
1907 while (*cp && *cp != quote)
1910 while (*cp && *cp != ' ' && *cp != '\t')
1923 if (fgets(line, sizeof (line), fd) == NULL)
1925 cp = strchr(line, '\n');
1932 newstr(const char *cp)
1936 if ((cr = strdup(cp != NULL ? cp : "")))
1938 syslog(LOG_ERR, "strdup: %m");
1943 inetd_setproctitle(const char *a, int s)
1946 struct sockaddr_storage ss;
1947 char buf[80], pbuf[INET6_ADDRSTRLEN];
1950 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) {
1951 getnameinfo((struct sockaddr *)&ss, size, pbuf, sizeof(pbuf),
1952 NULL, 0, NI_NUMERICHOST|NI_WITHSCOPEID);
1953 (void) sprintf(buf, "%s [%s]", a, pbuf);
1955 (void) sprintf(buf, "%s", a);
1956 setproctitle("%s", buf);
1960 check_loop(const struct sockaddr *sa, const struct servtab *sep)
1962 struct servtab *se2;
1963 char pname[INET6_ADDRSTRLEN];
1965 for (se2 = servtab; se2; se2 = se2->se_next) {
1966 if (!se2->se_bi || se2->se_socktype != SOCK_DGRAM)
1969 switch (se2->se_family) {
1971 if (((const struct sockaddr_in *)sa)->sin_port ==
1972 se2->se_ctrladdr4.sin_port)
1977 if (((const struct sockaddr_in *)sa)->sin_port ==
1978 se2->se_ctrladdr4.sin_port)
1986 getnameinfo(sa, sa->sa_len, pname, sizeof(pname), NULL, 0,
1987 NI_NUMERICHOST|NI_WITHSCOPEID);
1988 syslog(LOG_WARNING, "%s/%s:%s/%s loop request REFUSED from %s",
1989 sep->se_service, sep->se_proto,
1990 se2->se_service, se2->se_proto,
1999 * Dump relevant information to stderr
2002 print_service(const char *action, const struct servtab *sep)
2005 "%s: %s proto=%s accept=%d max=%d user=%s group=%s"
2009 " builtin=%p server=%s"
2014 action, sep->se_service, sep->se_proto,
2015 sep->se_accept, sep->se_maxchild, sep->se_user, sep->se_group,
2019 (void *) sep->se_bi, sep->se_server
2021 , (sep->se_policy ? sep->se_policy : "")
2026 #define CPMHSIZE 256
2027 #define CPMHMASK (CPMHSIZE-1)
2031 typedef struct CTime {
2032 unsigned long ct_Ticks;
2036 typedef struct CHash {
2038 struct in_addr c4_Addr;
2039 struct in6_addr c6_Addr;
2041 #define ch_Addr4 cu_Addr.c4_Addr
2042 #define ch_Addr6 cu_Addr.c6_Addr
2046 CTime ch_Times[CHTSIZE];
2049 CHash CHashAry[CPMHSIZE];
2052 cpmip(const struct servtab *sep, int ctrl)
2054 struct sockaddr_storage rss;
2055 socklen_t rssLen = sizeof(rss);
2059 * If getpeername() fails, just let it through (if logging is
2060 * enabled the condition is caught elsewhere)
2063 if (sep->se_maxcpm > 0 &&
2064 getpeername(ctrl, (struct sockaddr *)&rss, &rssLen) == 0 ) {
2065 time_t t = time(NULL);
2066 int hv = 0xABC3D20F;
2069 CHash *chBest = NULL;
2070 unsigned int ticks = t / CHTGRAN;
2071 struct sockaddr_in *sin4;
2073 struct sockaddr_in6 *sin6;
2076 sin4 = (struct sockaddr_in *)&rss;
2078 sin6 = (struct sockaddr_in6 *)&rss;
2084 switch (rss.ss_family) {
2086 p = (char *)&sin4->sin_addr;
2087 addrlen = sizeof(struct in_addr);
2091 p = (char *)&sin6->sin6_addr;
2092 addrlen = sizeof(struct in6_addr);
2096 /* should not happen */
2100 for (i = 0; i < addrlen; ++i, ++p) {
2101 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2103 hv = (hv ^ (hv >> 16));
2105 for (i = 0; i < 5; ++i) {
2106 CHash *ch = &CHashAry[(hv + i) & CPMHMASK];
2108 if (rss.ss_family == AF_INET &&
2109 ch->ch_Family == AF_INET &&
2110 sin4->sin_addr.s_addr == ch->ch_Addr4.s_addr &&
2111 ch->ch_Service && strcmp(sep->se_service,
2112 ch->ch_Service) == 0) {
2117 if (rss.ss_family == AF_INET6 &&
2118 ch->ch_Family == AF_INET6 &&
2119 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2120 &ch->ch_Addr6) != 0 &&
2121 ch->ch_Service && strcmp(sep->se_service,
2122 ch->ch_Service) == 0) {
2127 if (chBest == NULL || ch->ch_LTime == 0 ||
2128 ch->ch_LTime < chBest->ch_LTime) {
2132 if ((rss.ss_family == AF_INET &&
2133 (chBest->ch_Family != AF_INET ||
2134 sin4->sin_addr.s_addr != chBest->ch_Addr4.s_addr)) ||
2135 chBest->ch_Service == NULL ||
2136 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2137 chBest->ch_Family = sin4->sin_family;
2138 chBest->ch_Addr4 = sin4->sin_addr;
2139 if (chBest->ch_Service)
2140 free(chBest->ch_Service);
2141 chBest->ch_Service = strdup(sep->se_service);
2142 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2145 if ((rss.ss_family == AF_INET6 &&
2146 (chBest->ch_Family != AF_INET6 ||
2147 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2148 &chBest->ch_Addr6) == 0)) ||
2149 chBest->ch_Service == NULL ||
2150 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2151 chBest->ch_Family = sin6->sin6_family;
2152 chBest->ch_Addr6 = sin6->sin6_addr;
2153 if (chBest->ch_Service)
2154 free(chBest->ch_Service);
2155 chBest->ch_Service = strdup(sep->se_service);
2156 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2159 chBest->ch_LTime = t;
2161 CTime *ct = &chBest->ch_Times[ticks % CHTSIZE];
2162 if (ct->ct_Ticks != ticks) {
2163 ct->ct_Ticks = ticks;
2168 for (i = 0; i < CHTSIZE; ++i) {
2169 CTime *ct = &chBest->ch_Times[i];
2170 if (ct->ct_Ticks <= ticks &&
2171 ct->ct_Ticks >= ticks - CHTSIZE) {
2172 cnt += ct->ct_Count;
2175 if (cnt * (CHTSIZE * CHTGRAN) / 60 > sep->se_maxcpm) {
2176 char pname[INET6_ADDRSTRLEN];
2178 getnameinfo((struct sockaddr *)&rss,
2179 ((struct sockaddr *)&rss)->sa_len,
2180 pname, sizeof(pname), NULL, 0,
2181 NI_NUMERICHOST|NI_WITHSCOPEID);
2184 "%s from %s exceeded counts/min (limit %d/min)",
2185 sep->se_service, pname,