2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 __COPYRIGHT("@(#) 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";
47 * Inetd - Internet super-server
49 * This program invokes all internet services as needed. Connection-oriented
50 * services are invoked each time a connection is made, by creating a process.
51 * This process is passed the connection as file descriptor 0 and is expected
52 * to do a getpeername to find out the source host and port.
54 * Datagram oriented services are invoked when a datagram
55 * arrives; a process is created and passed a pending message
56 * on file descriptor 0. Datagram servers may either connect
57 * to their peer, freeing up the original socket for inetd
58 * to receive further messages on, or ``take over the socket'',
59 * processing all arriving datagrams and, eventually, timing
60 * out. The first type of server is said to be ``multi-threaded'';
61 * the second type of server ``single-threaded''.
63 * Inetd uses a configuration file which is read at startup
64 * and, possibly, at some later time in response to a hangup signal.
65 * The configuration file is ``free format'' with fields given in the
66 * order shown below. Continuation lines for an entry must begin with
67 * a space or tab. All fields must be present in each entry.
69 * service name must be in /etc/services
70 * or name a tcpmux service
71 * or specify a unix domain socket
72 * socket type stream/dgram/raw/rdm/seqpacket
73 * protocol tcp[4][6], udp[4][6], unix
74 * wait/nowait single-threaded/multi-threaded
75 * user[:group][/login-class] user/group/login-class to run daemon as
76 * server program full path name
77 * server program arguments maximum of MAXARGS (20)
79 * TCP services without official port numbers are handled with the
80 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for
81 * requests. When a connection is made from a foreign host, the service
82 * requested is passed to tcpmux, which looks it up in the servtab list
83 * and returns the proper entry for the service. Tcpmux returns a
84 * negative reply if the service doesn't exist, otherwise the invoked
85 * server is expected to return the positive reply if the service type in
86 * inetd.conf file has the prefix "tcpmux/". If the service type has the
87 * prefix "tcpmux/+", tcpmux will return the positive reply for the
88 * process; this is for compatibility with older server code, and also
89 * allows you to invoke programs that use stdin/stdout without putting any
90 * special server code in them. Services that use tcpmux are "nowait"
91 * because they do not have a well-known port and hence cannot listen
95 * service name/version must be in /etc/rpc
96 * socket type stream/dgram/raw/rdm/seqpacket
97 * protocol rpc/tcp[4][6], rpc/udp[4][6]
98 * wait/nowait single-threaded/multi-threaded
99 * user[:group][/login-class] user/group/login-class to run daemon as
100 * server program full path name
101 * server program arguments maximum of MAXARGS
103 * Comment lines are indicated by a `#' in column 1.
106 * Comment lines that start with "#@" denote IPsec policy string, as described
107 * in ipsec_set_policy(3). This will affect all the following items in
108 * inetd.conf(8). To reset the policy, just use "#@" line. By default,
109 * there's no IPsec policy.
112 #include <sys/param.h>
113 #include <sys/ioctl.h>
114 #include <sys/mman.h>
115 #include <sys/wait.h>
116 #include <sys/time.h>
117 #include <sys/resource.h>
118 #include <sys/stat.h>
121 #include <netinet/in.h>
122 #include <netinet/tcp.h>
123 #include <arpa/inet.h>
125 #include <rpc/pmap_clnt.h>
140 #include <sysexits.h>
148 #include "pathnames.h"
151 #include <netipsec/ipsec.h>
152 #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
157 #ifndef LIBWRAP_ALLOW_FACILITY
158 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
160 #ifndef LIBWRAP_ALLOW_SEVERITY
161 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
163 #ifndef LIBWRAP_DENY_FACILITY
164 # define LIBWRAP_DENY_FACILITY LOG_AUTH
166 #ifndef LIBWRAP_DENY_SEVERITY
167 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
170 #define ISWRAP(sep) \
171 ( ((wrap_ex && !(sep)->se_bi) || (wrap_bi && (sep)->se_bi)) \
172 && (sep->se_family == AF_INET || sep->se_family == AF_INET6) \
173 && ( ((sep)->se_accept && (sep)->se_socktype == SOCK_STREAM) \
174 || (sep)->se_socktype == SOCK_DGRAM))
177 #include <login_cap.h>
180 #define RESOURCE_RC "daemon"
185 #define MAXCHILD -1 /* maximum number of this service
190 #define MAXCPM -1 /* rate limit invocations from a
191 single remote address,
196 #define MAXPERIP -1 /* maximum number of this service
197 from a single remote address,
202 #define TOOMANY 256 /* don't start more than TOOMANY */
204 #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
205 #define RETRYTIME (60*10) /* retry after bind or server fail */
206 #define MAX_MAXCHLD 32767 /* max allowable max children */
208 #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
210 #define satosin(sa) ((struct sockaddr_in *)(void *)sa)
211 #define csatosin(sa) ((const struct sockaddr_in *)(const void *)sa)
213 #define satosin6(sa) ((struct sockaddr_in6 *)(void *)sa)
214 #define csatosin6(sa) ((const struct sockaddr_in6 *)(const void *)sa)
216 static void close_sep(struct servtab *);
217 static void flag_signal(int);
218 static void config(void);
219 static int cpmip(const struct servtab *, int);
220 static void endconfig(void);
221 static struct servtab *enter(struct servtab *);
222 static void freeconfig(struct servtab *);
223 static struct servtab *getconfigent(void);
224 static int matchservent(const char *, const char *, const char *);
225 static char *nextline(FILE *);
226 static void addchild(struct servtab *, int);
227 static void reapchild(void);
228 static void enable(struct servtab *);
229 static void disable(struct servtab *);
230 static void retry(void);
231 static int setconfig(void);
232 static void setup(struct servtab *);
234 static void ipsecsetup(struct servtab *);
236 static void unregisterrpc(register struct servtab *sep);
237 static struct conninfo *search_conn(struct servtab *sep, int ctrl);
238 static int room_conn(struct servtab *sep, struct conninfo *conn);
239 static void addchild_conn(struct conninfo *conn, pid_t pid);
240 static void reapchild_conn(pid_t pid);
241 static void free_conn(struct conninfo *conn);
242 static void resize_conn(struct servtab *sep, int maxperip);
243 static void free_connlist(struct servtab *sep);
244 static void free_proc(struct procinfo *);
245 static struct procinfo *search_proc(pid_t pid, int add);
246 static int hashval(char *p, int len);
247 static char *skip(char **);
248 static char *sskip(char **);
249 static char *newstr(const char *);
250 static void print_service(const char *, const struct servtab *);
258 static int wrap_ex = 0;
259 static int wrap_bi = 0;
261 static int dolog = 0;
262 static int maxsock; /* highest-numbered descriptor */
263 static fd_set allsock;
265 static int timingout;
266 static int toomany = TOOMANY;
267 static int maxchild = MAXCHILD;
268 static int maxcpm = MAXCPM;
269 static int maxperip = MAXPERIP;
270 static struct servent *sp;
271 static struct rpcent *rpc;
272 static char *hostname = NULL;
273 static struct sockaddr_in *bind_sa4;
274 static int v4bind_ok = 0;
276 static struct sockaddr_in6 *bind_sa6;
277 static int v6bind_ok = 0;
279 static int signalpipe[2];
287 struct servtab *servtab;
289 static const char *CONFIG = _PATH_INETDCONF;
290 static const char *pid_file = _PATH_INETDPID;
291 static struct pidfh *pfh = NULL;
293 static struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
295 static LIST_HEAD(, procinfo) proctable[PERIPSIZE];
298 getvalue(const char *arg, int *value, const char *whine)
303 tmp = strtol(arg, &p, 0);
305 syslog(LOG_ERR, whine, arg);
306 return 1; /* failure */
309 return 0; /* success */
314 whichaf(struct request_info *req)
318 sa = (struct sockaddr *)req->client->sin;
322 if (sa->sa_family == AF_INET6 &&
323 IN6_IS_ADDR_V4MAPPED(&satosin6(sa)->sin6_addr))
326 return sa->sa_family;
331 main(int argc, char **argv)
336 struct sigaction sa, saalrm, sachld, sahup, sapipe;
341 login_cap_t *lc = NULL;
344 struct request_info req;
346 char *service = NULL;
348 struct sockaddr_storage peer;
350 struct addrinfo hints, *res;
351 const char *servname;
353 struct conninfo *conn;
355 openlog("inetd", LOG_PID | LOG_NOWAIT | LOG_PERROR, LOG_DAEMON);
357 while ((ch = getopt(argc, argv, "dlwWR:a:c:C:p:s:")) != -1)
367 getvalue(optarg, &toomany,
368 "-R %s: bad value for service invocation rate");
371 getvalue(optarg, &maxchild,
372 "-c %s: bad value for maximum children");
375 getvalue(optarg, &maxcpm,
376 "-C %s: bad value for maximum children/minute");
385 getvalue(optarg, &maxperip,
386 "-s %s: bad value for maximum children per source address");
397 "usage: inetd [-dlwW] [-a address] [-R rate]"
398 " [-c maximum] [-C rate]"
399 " [-p pidfile] [conf-file]");
403 * Initialize Bind Addrs.
404 * When hostname is NULL, wild card bind addrs are obtained from
405 * getaddrinfo(). But getaddrinfo() requires at least one of
406 * hostname or servname is non NULL.
407 * So when hostname is NULL, set dummy value to servname.
408 * Since getaddrinfo() doesn't accept numeric servname, and
409 * we doesn't use ai_socktype of struct addrinfo returned
410 * from getaddrinfo(), we set dummy value to ai_socktype.
412 servname = (hostname == NULL) ? "0" /* dummy */ : NULL;
414 memset(&hints, 0, sizeof(struct addrinfo));
415 hints.ai_flags = AI_PASSIVE;
416 hints.ai_family = AF_UNSPEC;
417 hints.ai_socktype = SOCK_STREAM; /* dummy */
418 error = getaddrinfo(hostname, servname, &hints, &res);
420 syslog(LOG_ERR, "-a %s: %s", hostname, gai_strerror(error));
421 if (error == EAI_SYSTEM)
422 syslog(LOG_ERR, "%s", strerror(errno));
426 if (res->ai_addr == NULL) {
427 syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
430 switch (res->ai_addr->sa_family) {
434 bind_sa4 = satosin(res->ai_addr);
435 /* init port num in case servname is dummy */
436 bind_sa4->sin_port = 0;
443 bind_sa6 = satosin6(res->ai_addr);
444 /* init port num in case servname is dummy */
445 bind_sa6->sin6_port = 0;
456 } while ((res = res->ai_next) != NULL);
462 syslog(LOG_ERR, "-a %s: unknown address family", hostname);
468 umask(mask = umask(0777));
475 if (access(CONFIG, R_OK) < 0)
476 syslog(LOG_ERR, "Accessing %s: %m, continuing anyway.", CONFIG);
480 pfh = pidfile_open(pid_file, 0600, &otherpid);
482 if (errno == EEXIST) {
483 syslog(LOG_ERR, "%s already running, pid: %d",
484 getprogname(), otherpid);
487 syslog(LOG_WARNING, "pidfile_open() failed: %m");
490 if (daemon(0, 0) < 0) {
491 syslog(LOG_WARNING, "daemon(0,0) failed: %m");
493 /* From now on we don't want syslog messages going to stderr. */
495 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
497 * In case somebody has started inetd manually, we need to
498 * clear the logname, so that old servers run as root do not
499 * get the user's logname..
501 if (setlogin("") < 0) {
502 syslog(LOG_WARNING, "cannot clear logname: %m");
503 /* no big deal if it fails.. */
505 if (pfh != NULL && pidfile_write(pfh) == -1) {
506 syslog(LOG_WARNING, "pidfile_write(): %m");
510 if (madvise(NULL, 0, MADV_PROTECT) != 0)
511 syslog(LOG_WARNING, "madvise() failed: %s", strerror(errno));
513 for (i = 0; i < PERIPSIZE; ++i)
514 LIST_INIT(&proctable[i]);
517 udpconf = getnetconfigent("udp");
518 tcpconf = getnetconfigent("tcp");
519 if (udpconf == NULL || tcpconf == NULL) {
520 syslog(LOG_ERR, "unknown rpc/udp or rpc/tcp");
526 udp6conf = getnetconfigent("udp6");
527 tcp6conf = getnetconfigent("tcp6");
528 if (udp6conf == NULL || tcp6conf == NULL) {
529 syslog(LOG_ERR, "unknown rpc/udp6 or rpc/tcp6");
535 sa = (struct sigaction){
537 .sa_handler = flag_signal,
539 sigemptyset(&sa.sa_mask);
540 sigaddset(&sa.sa_mask, SIGALRM);
541 sigaddset(&sa.sa_mask, SIGCHLD);
542 sigaddset(&sa.sa_mask, SIGHUP);
543 sigaction(SIGALRM, &sa, &saalrm);
545 sigaction(SIGHUP, &sa, &sahup);
546 sigaction(SIGCHLD, &sa, &sachld);
547 sa.sa_handler = SIG_IGN;
548 sigaction(SIGPIPE, &sa, &sapipe);
551 /* space for daemons to overwrite environment for ps */
552 #define DUMMYSIZE 100
553 char dummy[DUMMYSIZE];
555 (void)memset(dummy, 'x', DUMMYSIZE - 1);
556 dummy[DUMMYSIZE - 1] = '\0';
557 (void)setenv("inetd_dummy", dummy, 1);
560 if (pipe2(signalpipe, O_CLOEXEC) != 0) {
561 syslog(LOG_ERR, "pipe: %m");
564 FD_SET(signalpipe[0], &allsock);
568 maxsock = MAX(MAX(maxsock, signalpipe[0]), signalpipe[1]);
576 syslog(LOG_ERR, "%s: nsock=0", __func__);
581 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
582 (fd_set *)0, (struct timeval *)0)) <= 0) {
583 if (n < 0 && errno != EINTR) {
584 syslog(LOG_WARNING, "select: %m");
589 /* handle any queued signal flags */
590 if (FD_ISSET(signalpipe[0], &readable)) {
593 if (ioctl(signalpipe[0], FIONREAD, &nsig) != 0) {
594 syslog(LOG_ERR, "ioctl: %m");
597 nsig /= sizeof(signo);
598 while (--nsig >= 0) {
601 len = read(signalpipe[0], &signo, sizeof(signo));
602 if (len != sizeof(signo)) {
603 syslog(LOG_ERR, "read: %m");
607 warnx("handling signal flag %d", signo);
621 for (sep = servtab; n && sep; sep = sep->se_next)
622 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
625 warnx("someone wants %s", sep->se_service);
626 dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep);
628 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) {
630 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
631 syslog(LOG_ERR, "ioctl (FIONBIO, 1): %m");
632 ctrl = accept(sep->se_fd, (struct sockaddr *)0,
635 warnx("accept, ctrl %d", ctrl);
639 "accept (for %s): %m",
641 if (sep->se_accept &&
642 sep->se_socktype == SOCK_STREAM)
647 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
648 syslog(LOG_ERR, "ioctl1(FIONBIO, 0): %m");
649 if (ioctl(ctrl, FIONBIO, &i) < 0)
650 syslog(LOG_ERR, "ioctl2(FIONBIO, 0): %m");
651 if (cpmip(sep, ctrl) < 0) {
656 (conn = search_conn(sep, ctrl)) != NULL &&
657 !room_conn(sep, conn)) {
663 if (dolog && !ISWRAP(sep)) {
664 char pname[NI_MAXHOST] = "unknown";
667 if (getpeername(ctrl, (struct sockaddr *)
670 if (recvfrom(ctrl, buf, sizeof(buf),
672 (struct sockaddr *)&peer,
674 getnameinfo((struct sockaddr *)&peer,
676 pname, sizeof(pname),
677 NULL, 0, NI_NUMERICHOST);
680 getnameinfo((struct sockaddr *)&peer,
682 pname, sizeof(pname),
683 NULL, 0, NI_NUMERICHOST);
685 syslog(LOG_INFO,"%s from %s", sep->se_service, pname);
687 (void) sigblock(SIGBLOCK);
690 * Fork for all external services, builtins which need to
691 * fork and anything we're wrapping (as wrapping might
692 * block or use hosts_options(5) twist).
695 if (sep->se_count++ == 0)
696 (void)clock_gettime(CLOCK_MONOTONIC_FAST, &sep->se_time);
697 else if (toomany > 0 && sep->se_count >= toomany) {
700 (void)clock_gettime(CLOCK_MONOTONIC_FAST, &now);
701 if (now.tv_sec - sep->se_time.tv_sec >
707 "%s/%s server failing (looping), service terminated",
708 sep->se_service, sep->se_proto);
709 if (sep->se_accept &&
710 sep->se_socktype == SOCK_STREAM)
725 syslog(LOG_ERR, "fork: %m");
726 if (sep->se_accept &&
727 sep->se_socktype == SOCK_STREAM)
735 addchild_conn(conn, pid);
742 sigaction(SIGALRM, &saalrm, (struct sigaction *)0);
743 sigaction(SIGCHLD, &sachld, (struct sigaction *)0);
744 sigaction(SIGHUP, &sahup, (struct sigaction *)0);
745 /* SIGPIPE reset before exec */
748 * Call tcpmux to find the real service to exec.
751 sep->se_bi->bi_fn == (bi_fn_t *) tcpmux) {
760 inetd_setproctitle("wrapping", ctrl);
761 service = sep->se_server_name ?
762 sep->se_server_name : sep->se_service;
763 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, 0);
765 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
766 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
767 denied = !hosts_access(&req);
769 syslog(deny_severity,
770 "refused connection from %.500s, service %s (%s%s)",
771 eval_client(&req), service, sep->se_proto,
772 (whichaf(&req) == AF_INET6) ? "6" : "");
773 if (sep->se_socktype != SOCK_STREAM)
774 recv(ctrl, buf, sizeof (buf), 0);
781 syslog(allow_severity,
782 "connection from %.500s, service %s (%s%s)",
783 eval_client(&req), service, sep->se_proto,
784 (whichaf(&req) == AF_INET6) ? "6" : "");
789 (*sep->se_bi->bi_fn)(ctrl, sep);
793 getpid(), sep->se_server);
794 /* Clear close-on-exec. */
795 if (fcntl(ctrl, F_SETFD, 0) < 0) {
797 "%s/%s: fcntl (F_SETFD, 0): %m",
798 sep->se_service, sep->se_proto);
807 if ((pwd = getpwnam(sep->se_user)) == NULL) {
809 "%s/%s: %s: no such user",
810 sep->se_service, sep->se_proto,
812 if (sep->se_socktype != SOCK_STREAM)
813 recv(0, buf, sizeof (buf), 0);
817 if ( sep->se_group != NULL
818 && (grp = getgrnam(sep->se_group)) == NULL
821 "%s/%s: %s: no such group",
822 sep->se_service, sep->se_proto,
824 if (sep->se_socktype != SOCK_STREAM)
825 recv(0, buf, sizeof (buf), 0);
829 pwd->pw_gid = grp->gr_gid;
831 if ((lc = login_getclass(sep->se_class)) == NULL) {
832 /* error syslogged by getclass */
834 "%s/%s: %s: login class error",
835 sep->se_service, sep->se_proto,
837 if (sep->se_socktype != SOCK_STREAM)
838 recv(0, buf, sizeof (buf), 0);
844 "%s: can't setsid(): %m",
846 /* _exit(EX_OSERR); not fatal yet */
849 if (setusercontext(lc, pwd, pwd->pw_uid,
850 LOGIN_SETALL & ~LOGIN_SETMAC)
853 "%s: can't setusercontext(..%s..): %m",
854 sep->se_service, sep->se_user);
860 if (setlogin(sep->se_user) < 0) {
862 "%s: can't setlogin(%s): %m",
863 sep->se_service, sep->se_user);
864 /* _exit(EX_OSERR); not yet */
866 if (setgid(pwd->pw_gid) < 0) {
868 "%s: can't set gid %d: %m",
869 sep->se_service, pwd->pw_gid);
872 (void) initgroups(pwd->pw_name,
874 if (setuid(pwd->pw_uid) < 0) {
876 "%s: can't set uid %d: %m",
877 sep->se_service, pwd->pw_uid);
882 sigaction(SIGPIPE, &sapipe,
883 (struct sigaction *)0);
884 execv(sep->se_server, sep->se_argv);
886 "cannot execute %s: %m", sep->se_server);
887 if (sep->se_socktype != SOCK_STREAM)
888 recv(0, buf, sizeof (buf), 0);
893 if (sep->se_accept && sep->se_socktype == SOCK_STREAM)
900 * Add a signal flag to the signal flag queue for later handling
904 flag_signal(int signo)
908 len = write(signalpipe[1], &signo, sizeof(signo));
909 if (len != sizeof(signo)) {
910 syslog(LOG_ERR, "write: %m");
916 * Record a new child pid for this service. If we've reached the
917 * limit on children, then stop accepting incoming requests.
921 addchild(struct servtab *sep, pid_t pid)
923 struct stabchild *sc;
926 if (SERVTAB_EXCEEDS_LIMIT(sep)) {
927 syslog(LOG_ERR, "%s: %d >= %d",
928 __func__, sep->se_numchild, sep->se_maxchild);
932 sc = calloc(1, sizeof(*sc));
934 syslog(LOG_ERR, "calloc: %m");
938 LIST_INSERT_HEAD(&sep->se_children, sc, sc_link);
940 if (SERVTAB_AT_LIMIT(sep))
949 struct stabchild *sc;
953 pid = wait3(&status, WNOHANG, (struct rusage *)0);
957 warnx("%d reaped, %s %u", pid,
958 WIFEXITED(status) ? "status" : "signal",
959 WIFEXITED(status) ? WEXITSTATUS(status)
961 for (sep = servtab; sep; sep = sep->se_next) {
962 LIST_FOREACH(sc, &sep->se_children, sc_link) {
963 if (sc->sc_pid == pid)
968 if (SERVTAB_AT_LIMIT(sep))
970 LIST_REMOVE(sc, sc_link);
973 if (WIFSIGNALED(status) || WEXITSTATUS(status))
975 "%s[%d]: exited, %s %u",
977 WIFEXITED(status) ? "status" : "signal",
978 WIFEXITED(status) ? WEXITSTATUS(status)
989 struct servtab *sep, *new, **sepp;
993 login_cap_t *lc = NULL;
997 syslog(LOG_ERR, "%s: %m", CONFIG);
1000 for (sep = servtab; sep; sep = sep->se_next)
1001 sep->se_checked = 0;
1002 while ((new = getconfigent())) {
1003 if (getpwnam(new->se_user) == NULL) {
1005 "%s/%s: no such user '%s', service ignored",
1006 new->se_service, new->se_proto, new->se_user);
1009 if (new->se_group && getgrnam(new->se_group) == NULL) {
1011 "%s/%s: no such group '%s', service ignored",
1012 new->se_service, new->se_proto, new->se_group);
1016 if ((lc = login_getclass(new->se_class)) == NULL) {
1017 /* error syslogged by getclass */
1019 "%s/%s: %s: login class error, service ignored",
1020 new->se_service, new->se_proto, new->se_class);
1025 new_nomapped = new->se_nomapped;
1026 for (sep = servtab; sep; sep = sep->se_next)
1027 if (strcmp(sep->se_service, new->se_service) == 0 &&
1028 strcmp(sep->se_proto, new->se_proto) == 0 &&
1029 sep->se_rpc == new->se_rpc &&
1030 sep->se_socktype == new->se_socktype &&
1031 sep->se_family == new->se_family)
1036 #define SWAP(t,a, b) { t c = a; a = b; b = c; }
1037 omask = sigblock(SIGBLOCK);
1038 if (sep->se_nomapped != new->se_nomapped) {
1039 /* for rpc keep old nommaped till unregister */
1041 sep->se_nomapped = new->se_nomapped;
1046 * The children tracked remain; we want numchild to
1047 * still reflect how many jobs are running so we don't
1048 * throw off our accounting.
1050 sep->se_maxcpm = new->se_maxcpm;
1051 sep->se_maxchild = new->se_maxchild;
1052 resize_conn(sep, new->se_maxperip);
1053 sep->se_maxperip = new->se_maxperip;
1054 sep->se_bi = new->se_bi;
1055 /* might need to turn on or off service now */
1056 if (sep->se_fd >= 0) {
1057 if (SERVTAB_EXCEEDS_LIMIT(sep)) {
1058 if (FD_ISSET(sep->se_fd, &allsock))
1061 if (!FD_ISSET(sep->se_fd, &allsock))
1065 sep->se_accept = new->se_accept;
1066 SWAP(char *, sep->se_user, new->se_user);
1067 SWAP(char *, sep->se_group, new->se_group);
1069 SWAP(char *, sep->se_class, new->se_class);
1071 SWAP(char *, sep->se_server, new->se_server);
1072 SWAP(char *, sep->se_server_name, new->se_server_name);
1073 for (i = 0; i < MAXARGV; i++)
1074 SWAP(char *, sep->se_argv[i], new->se_argv[i]);
1076 SWAP(char *, sep->se_policy, new->se_policy);
1082 print_service("REDO", sep);
1086 print_service("ADD ", sep);
1088 sep->se_checked = 1;
1093 switch (sep->se_family) {
1110 if (sep->se_family != AF_UNIX) {
1111 sp = getservbyname(sep->se_service, sep->se_proto);
1113 syslog(LOG_ERR, "%s/%s: unknown service",
1114 sep->se_service, sep->se_proto);
1115 sep->se_checked = 0;
1119 switch (sep->se_family) {
1121 if (sp->s_port != sep->se_ctrladdr4.sin_port) {
1122 sep->se_ctrladdr4.sin_port =
1130 sep->se_ctrladdr6.sin6_port) {
1131 sep->se_ctrladdr6.sin6_port =
1138 if (sep->se_reset != 0 && sep->se_fd >= 0)
1141 rpc = getrpcbyname(sep->se_service);
1143 syslog(LOG_ERR, "%s/%s unknown RPC service",
1144 sep->se_service, sep->se_proto);
1145 if (sep->se_fd != -1)
1146 (void) close(sep->se_fd);
1150 if (sep->se_reset != 0 ||
1151 rpc->r_number != sep->se_rpc_prog) {
1152 if (sep->se_rpc_prog)
1154 sep->se_rpc_prog = rpc->r_number;
1155 if (sep->se_fd != -1)
1156 (void) close(sep->se_fd);
1159 sep->se_nomapped = new_nomapped;
1162 if (sep->se_fd == -1)
1167 * Purge anything not looked at above.
1169 omask = sigblock(SIGBLOCK);
1171 while ((sep = *sepp)) {
1172 if (sep->se_checked) {
1173 sepp = &sep->se_next;
1176 *sepp = sep->se_next;
1177 if (sep->se_fd >= 0)
1180 print_service("FREE", sep);
1181 if (sep->se_rpc && sep->se_rpc_prog > 0)
1186 (void) sigsetmask(omask);
1190 unregisterrpc(struct servtab *sep)
1193 struct servtab *sepp;
1195 struct netconfig *netid4, *netid6;
1197 omask = sigblock(SIGBLOCK);
1198 netid4 = sep->se_socktype == SOCK_DGRAM ? udpconf : tcpconf;
1199 netid6 = sep->se_socktype == SOCK_DGRAM ? udp6conf : tcp6conf;
1200 if (sep->se_family == AF_INET)
1202 else if (sep->se_nomapped)
1205 * Conflict if same prog and protocol - In that case one should look
1206 * to versions, but it is not interesting: having separate servers for
1207 * different versions does not work well.
1208 * Therefore one do not unregister if there is a conflict.
1209 * There is also transport conflict if destroying INET when INET46
1210 * exists, or destroying INET46 when INET exists
1212 for (sepp = servtab; sepp; sepp = sepp->se_next) {
1215 if (sepp->se_checked == 0 ||
1217 strcmp(sep->se_proto, sepp->se_proto) != 0 ||
1218 sep->se_rpc_prog != sepp->se_rpc_prog)
1220 if (sepp->se_family == AF_INET)
1222 if (sepp->se_family == AF_INET6) {
1224 if (!sep->se_nomapped)
1227 if (netid4 == NULL && netid6 == NULL)
1231 print_service("UNREG", sep);
1232 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1234 rpcb_unset(sep->se_rpc_prog, i, netid4);
1236 rpcb_unset(sep->se_rpc_prog, i, netid6);
1238 if (sep->se_fd != -1)
1239 (void) close(sep->se_fd);
1241 (void) sigsetmask(omask);
1247 struct servtab *sep;
1250 for (sep = servtab; sep; sep = sep->se_next)
1251 if (sep->se_fd == -1 && !ISMUX(sep))
1256 setup(struct servtab *sep)
1260 /* Set all listening sockets to close-on-exec. */
1261 if ((sep->se_fd = socket(sep->se_family,
1262 sep->se_socktype | SOCK_CLOEXEC, 0)) < 0) {
1264 warn("socket failed on %s/%s",
1265 sep->se_service, sep->se_proto);
1266 syslog(LOG_ERR, "%s/%s: socket: %m",
1267 sep->se_service, sep->se_proto);
1270 #define turnon(fd, opt) \
1271 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
1272 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
1273 turnon(sep->se_fd, SO_DEBUG) < 0)
1274 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
1275 if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
1276 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
1278 if (turnon(sep->se_fd, SO_PRIVSTATE) < 0)
1279 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m");
1281 /* tftpd opens a new connection then needs more infos */
1283 if ((sep->se_family == AF_INET6) &&
1284 (strcmp(sep->se_proto, "udp") == 0) &&
1285 (sep->se_accept == 0) &&
1286 (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1287 (char *)&on, sizeof (on)) < 0))
1288 syslog(LOG_ERR, "setsockopt (IPV6_RECVPKTINFO): %m");
1289 if (sep->se_family == AF_INET6) {
1290 int flag = sep->se_nomapped ? 1 : 0;
1291 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_V6ONLY,
1292 (char *)&flag, sizeof (flag)) < 0)
1293 syslog(LOG_ERR, "setsockopt (IPV6_V6ONLY): %m");
1300 if (sep->se_family == AF_UNIX) {
1301 (void) unlink(sep->se_ctrladdr_un.sun_path);
1302 umask(0777); /* Make socket with conservative permissions */
1304 if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr,
1305 sep->se_ctrladdr_size) < 0) {
1307 warn("bind failed on %s/%s",
1308 sep->se_service, sep->se_proto);
1309 syslog(LOG_ERR, "%s/%s: bind: %m",
1310 sep->se_service, sep->se_proto);
1311 (void) close(sep->se_fd);
1317 if (sep->se_family == AF_UNIX)
1321 if (sep->se_family == AF_UNIX) {
1322 /* Ick - fch{own,mod} don't work on Unix domain sockets */
1323 if (chown(sep->se_service, sep->se_sockuid, sep->se_sockgid) < 0)
1324 syslog(LOG_ERR, "chown socket: %m");
1325 if (chmod(sep->se_service, sep->se_sockmode) < 0)
1326 syslog(LOG_ERR, "chmod socket: %m");
1331 socklen_t len = sep->se_ctrladdr_size;
1332 struct netconfig *netid, *netid2 = NULL;
1334 struct sockaddr_in sock;
1336 struct netbuf nbuf, nbuf2;
1338 if (getsockname(sep->se_fd,
1339 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){
1340 syslog(LOG_ERR, "%s/%s: getsockname: %m",
1341 sep->se_service, sep->se_proto);
1342 (void) close(sep->se_fd);
1346 nbuf.buf = &sep->se_ctrladdr;
1347 nbuf.len = sep->se_ctrladdr.sa_len;
1348 if (sep->se_family == AF_INET)
1349 netid = sep->se_socktype==SOCK_DGRAM? udpconf:tcpconf;
1352 netid = sep->se_socktype==SOCK_DGRAM? udp6conf:tcp6conf;
1353 if (!sep->se_nomapped) { /* INET and INET6 */
1354 netid2 = netid==udp6conf? udpconf:tcpconf;
1355 memset(&sock, 0, sizeof sock); /* ADDR_ANY */
1357 nbuf2.len = sock.sin_len = sizeof sock;
1358 sock.sin_family = AF_INET;
1359 sock.sin_port = sep->se_ctrladdr6.sin6_port;
1365 "%s/%s: inetd compiled without inet6 support\n",
1366 sep->se_service, sep->se_proto);
1367 (void) close(sep->se_fd);
1373 print_service("REG ", sep);
1374 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1375 rpcb_unset(sep->se_rpc_prog, i, netid);
1376 rpcb_set(sep->se_rpc_prog, i, netid, &nbuf);
1378 rpcb_unset(sep->se_rpc_prog, i, netid2);
1379 rpcb_set(sep->se_rpc_prog, i, netid2, &nbuf2);
1383 if (sep->se_socktype == SOCK_STREAM)
1384 listen(sep->se_fd, -1);
1387 warnx("registered %s on %d",
1388 sep->se_server, sep->se_fd);
1394 ipsecsetup(struct servtab *sep)
1397 char *policy_in = NULL;
1398 char *policy_out = NULL;
1402 switch (sep->se_family) {
1405 opt = IP_IPSEC_POLICY;
1409 level = IPPROTO_IPV6;
1410 opt = IPV6_IPSEC_POLICY;
1417 if (!sep->se_policy || sep->se_policy[0] == '\0') {
1418 static char def_in[] = "in entrust", def_out[] = "out entrust";
1420 policy_out = def_out;
1422 if (!strncmp("in", sep->se_policy, 2))
1423 policy_in = sep->se_policy;
1424 else if (!strncmp("out", sep->se_policy, 3))
1425 policy_out = sep->se_policy;
1427 syslog(LOG_ERR, "invalid security policy \"%s\"",
1433 if (policy_in != NULL) {
1434 buf = ipsec_set_policy(policy_in, strlen(policy_in));
1436 if (setsockopt(sep->se_fd, level, opt,
1437 buf, ipsec_get_policylen(buf)) < 0 &&
1439 warnx("%s/%s: ipsec initialization failed; %s",
1440 sep->se_service, sep->se_proto,
1444 syslog(LOG_ERR, "invalid security policy \"%s\"",
1447 if (policy_out != NULL) {
1448 buf = ipsec_set_policy(policy_out, strlen(policy_out));
1450 if (setsockopt(sep->se_fd, level, opt,
1451 buf, ipsec_get_policylen(buf)) < 0 &&
1453 warnx("%s/%s: ipsec initialization failed; %s",
1454 sep->se_service, sep->se_proto,
1458 syslog(LOG_ERR, "invalid security policy \"%s\"",
1465 * Finish with a service and its socket.
1468 close_sep(struct servtab *sep)
1470 if (sep->se_fd >= 0) {
1471 if (FD_ISSET(sep->se_fd, &allsock))
1473 (void) close(sep->se_fd);
1477 sep->se_numchild = 0; /* forget about any existing children */
1481 matchservent(const char *name1, const char *name2, const char *proto)
1486 if (strcmp(proto, "unix") == 0) {
1487 if ((p = strrchr(name1, '/')) != NULL)
1489 if ((p = strrchr(name2, '/')) != NULL)
1492 if (strcmp(name1, name2) == 0)
1494 if ((se = getservbyname(name1, proto)) != NULL) {
1495 if (strcmp(name2, se->s_name) == 0)
1497 for (alias = se->s_aliases; *alias; alias++)
1498 if (strcmp(name2, *alias) == 0)
1504 static struct servtab *
1505 enter(struct servtab *cp)
1507 struct servtab *sep;
1510 sep = malloc(sizeof(*sep));
1512 syslog(LOG_ERR, "malloc: %m");
1517 omask = sigblock(SIGBLOCK);
1518 sep->se_next = servtab;
1525 enable(struct servtab *sep)
1529 "enabling %s, fd %d", sep->se_service, sep->se_fd);
1531 if (sep->se_fd < 0) {
1533 "%s: %s: bad fd", __func__, sep->se_service);
1538 "%s: %s: is mux", __func__, sep->se_service);
1541 if (FD_ISSET(sep->se_fd, &allsock)) {
1543 "%s: %s: not off", __func__, sep->se_service);
1548 FD_SET(sep->se_fd, &allsock);
1549 maxsock = MAX(maxsock, sep->se_fd);
1553 disable(struct servtab *sep)
1557 "disabling %s, fd %d", sep->se_service, sep->se_fd);
1559 if (sep->se_fd < 0) {
1561 "%s: %s: bad fd", __func__, sep->se_service);
1566 "%s: %s: is mux", __func__, sep->se_service);
1569 if (!FD_ISSET(sep->se_fd, &allsock)) {
1571 "%s: %s: not on", __func__, sep->se_service);
1575 syslog(LOG_ERR, "%s: nsock=0", __func__);
1580 FD_CLR(sep->se_fd, &allsock);
1581 if (sep->se_fd == maxsock)
1585 static FILE *fconfig = NULL;
1586 static struct servtab serv;
1587 static char line[LINE_MAX];
1593 if (fconfig != NULL) {
1594 fseek(fconfig, 0L, SEEK_SET);
1597 fconfig = fopen(CONFIG, "r");
1598 return (fconfig != NULL);
1605 (void) fclose(fconfig);
1610 static struct servtab *
1613 struct servtab *sep = &serv;
1617 static char TCPMUX_TOKEN[] = "tcpmux/";
1618 #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
1637 while ((cp = nextline(fconfig)) != NULL) {
1639 /* lines starting with #@ is not a comment, but the policy */
1640 if (cp[0] == '#' && cp[1] == '@') {
1642 for (p = cp + 2; p && *p && isspace(*p); p++)
1647 } else if (ipsec_get_policylen(p) >= 0) {
1652 "%s: invalid ipsec policy \"%s\"",
1658 if (*cp == '#' || *cp == '\0')
1670 * clear the static buffer, since some fields (se_ctrladdr,
1671 * for example) don't get initialized here.
1673 memset(sep, 0, sizeof *sep);
1676 /* got an empty line containing just blanks/tabs. */
1679 if (arg[0] == ':') { /* :user:group:perm: */
1680 char *user, *group, *perm;
1684 if ((group = strchr(user, ':')) == NULL) {
1685 syslog(LOG_ERR, "no group after user '%s'", user);
1689 if ((perm = strchr(group, ':')) == NULL) {
1690 syslog(LOG_ERR, "no mode after group '%s'", group);
1694 if ((pw = getpwnam(user)) == NULL) {
1695 syslog(LOG_ERR, "no such user '%s'", user);
1698 sep->se_sockuid = pw->pw_uid;
1699 if ((gr = getgrnam(group)) == NULL) {
1700 syslog(LOG_ERR, "no such user '%s'", group);
1703 sep->se_sockgid = gr->gr_gid;
1704 sep->se_sockmode = strtol(perm, &arg, 8);
1706 syslog(LOG_ERR, "bad mode '%s'", perm);
1711 sep->se_sockuid = euid;
1712 sep->se_sockgid = egid;
1713 sep->se_sockmode = 0200;
1715 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
1716 char *c = arg + MUX_LEN;
1718 sep->se_type = MUXPLUS_TYPE;
1721 sep->se_type = MUX_TYPE;
1722 sep->se_service = newstr(c);
1724 sep->se_service = newstr(arg);
1725 sep->se_type = NORM_TYPE;
1728 if (strcmp(arg, "stream") == 0)
1729 sep->se_socktype = SOCK_STREAM;
1730 else if (strcmp(arg, "dgram") == 0)
1731 sep->se_socktype = SOCK_DGRAM;
1732 else if (strcmp(arg, "rdm") == 0)
1733 sep->se_socktype = SOCK_RDM;
1734 else if (strcmp(arg, "seqpacket") == 0)
1735 sep->se_socktype = SOCK_SEQPACKET;
1736 else if (strcmp(arg, "raw") == 0)
1737 sep->se_socktype = SOCK_RAW;
1739 sep->se_socktype = -1;
1742 if (strncmp(arg, "tcp", 3) == 0) {
1743 sep->se_proto = newstr(strsep(&arg, "/"));
1744 if (arg != NULL && (strcmp(arg, "faith") == 0)) {
1745 syslog(LOG_ERR, "faith has been deprecated");
1749 if (sep->se_type == NORM_TYPE &&
1750 strncmp(arg, "faith/", 6) == 0) {
1751 syslog(LOG_ERR, "faith has been deprecated");
1754 sep->se_proto = newstr(arg);
1756 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1757 memmove(sep->se_proto, sep->se_proto + 4,
1758 strlen(sep->se_proto) + 1 - 4);
1760 sep->se_rpc_prog = sep->se_rpc_lowvers =
1761 sep->se_rpc_highvers = 0;
1762 if ((versp = strrchr(sep->se_service, '/'))) {
1764 switch (sscanf(versp, "%u-%u",
1765 &sep->se_rpc_lowvers,
1766 &sep->se_rpc_highvers)) {
1770 sep->se_rpc_highvers =
1771 sep->se_rpc_lowvers;
1775 "bad RPC version specifier; %s",
1782 sep->se_rpc_lowvers =
1783 sep->se_rpc_highvers = 1;
1786 sep->se_nomapped = 0;
1787 if (strcmp(sep->se_proto, "unix") == 0) {
1788 sep->se_family = AF_UNIX;
1790 while (isdigit(sep->se_proto[strlen(sep->se_proto) - 1])) {
1792 if (sep->se_proto[strlen(sep->se_proto) - 1] == '6') {
1793 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1798 if (sep->se_proto[strlen(sep->se_proto) - 1] == '4') {
1799 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1805 /* illegal version num */
1806 syslog(LOG_ERR, "bad IP version for %s", sep->se_proto);
1811 if (v6bind && !v6bind_ok) {
1812 syslog(LOG_INFO, "IPv6 bind is ignored for %s",
1814 if (v4bind && v4bind_ok)
1822 sep->se_family = AF_INET6;
1823 if (!v4bind || !v4bind_ok)
1824 sep->se_nomapped = 1;
1827 { /* default to v4 bind if not v6 bind */
1829 syslog(LOG_NOTICE, "IPv4 bind is ignored for %s",
1834 sep->se_family = AF_INET;
1838 switch(sep->se_family) {
1840 memcpy(&sep->se_ctrladdr4, bind_sa4,
1841 sizeof(sep->se_ctrladdr4));
1842 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr4);
1846 memcpy(&sep->se_ctrladdr6, bind_sa6,
1847 sizeof(sep->se_ctrladdr6));
1848 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr6);
1852 #define SUN_PATH_MAXSIZE sizeof(sep->se_ctrladdr_un.sun_path)
1853 memset(&sep->se_ctrladdr, 0, sizeof(sep->se_ctrladdr));
1854 sep->se_ctrladdr_un.sun_family = sep->se_family;
1855 if ((unsz = strlcpy(sep->se_ctrladdr_un.sun_path,
1856 sep->se_service, SUN_PATH_MAXSIZE) >= SUN_PATH_MAXSIZE)) {
1858 "domain socket pathname too long for service %s",
1862 sep->se_ctrladdr_un.sun_len = unsz;
1863 #undef SUN_PATH_MAXSIZE
1864 sep->se_ctrladdr_size = SUN_LEN(&sep->se_ctrladdr_un);
1867 if (!strncmp(arg, "wait", 4))
1869 else if (!strncmp(arg, "nowait", 6))
1873 "%s: bad wait/nowait for service %s",
1874 CONFIG, sep->se_service);
1877 sep->se_maxchild = -1;
1878 sep->se_maxcpm = -1;
1879 sep->se_maxperip = -1;
1880 if ((s = strchr(arg, '/')) != NULL) {
1884 val = strtoul(s + 1, &eptr, 10);
1885 if (eptr == s + 1 || val > MAX_MAXCHLD) {
1887 "%s: bad max-child for service %s",
1888 CONFIG, sep->se_service);
1892 if (!sep->se_accept && val != 1)
1893 warnx("maxchild=%lu for wait service %s"
1894 " not recommended", val, sep->se_service);
1895 sep->se_maxchild = val;
1897 sep->se_maxcpm = strtol(eptr + 1, &eptr, 10);
1899 sep->se_maxperip = strtol(eptr + 1, &eptr, 10);
1901 * explicitly do not check for \0 for future expansion /
1902 * backwards compatibility
1907 * Silently enforce "nowait" mode for TCPMUX services
1908 * since they don't have an assigned port to listen on.
1911 if (strcmp(sep->se_proto, "tcp")) {
1913 "%s: bad protocol for tcpmux service %s",
1914 CONFIG, sep->se_service);
1917 if (sep->se_socktype != SOCK_STREAM) {
1919 "%s: bad socket type for tcpmux service %s",
1920 CONFIG, sep->se_service);
1924 sep->se_user = newstr(sskip(&cp));
1926 if ((s = strrchr(sep->se_user, '/')) != NULL) {
1928 sep->se_class = newstr(s + 1);
1930 sep->se_class = newstr(RESOURCE_RC);
1932 if ((s = strrchr(sep->se_user, ':')) != NULL) {
1934 sep->se_group = newstr(s + 1);
1936 sep->se_group = NULL;
1937 sep->se_server = newstr(sskip(&cp));
1938 if ((sep->se_server_name = strrchr(sep->se_server, '/')))
1939 sep->se_server_name++;
1940 if (strcmp(sep->se_server, "internal") == 0) {
1943 for (bi = biltins; bi->bi_service; bi++)
1944 if (bi->bi_socktype == sep->se_socktype &&
1945 matchservent(bi->bi_service, sep->se_service,
1948 if (bi->bi_service == 0) {
1949 syslog(LOG_ERR, "internal service %s unknown",
1953 sep->se_accept = 1; /* force accept mode for built-ins */
1957 if (sep->se_maxperip < 0)
1958 sep->se_maxperip = maxperip;
1959 if (sep->se_maxcpm < 0)
1960 sep->se_maxcpm = maxcpm;
1961 if (sep->se_maxchild < 0) { /* apply default max-children */
1962 if (sep->se_bi && sep->se_bi->bi_maxchild >= 0)
1963 sep->se_maxchild = sep->se_bi->bi_maxchild;
1964 else if (sep->se_accept)
1965 sep->se_maxchild = MAX(maxchild, 0);
1967 sep->se_maxchild = 1;
1969 LIST_INIT(&sep->se_children);
1971 for (arg = skip(&cp); cp; arg = skip(&cp))
1972 if (argc < MAXARGV) {
1973 sep->se_argv[argc++] = newstr(arg);
1976 "%s: too many arguments for service %s",
1977 CONFIG, sep->se_service);
1980 while (argc <= MAXARGV)
1981 sep->se_argv[argc++] = NULL;
1982 for (i = 0; i < PERIPSIZE; ++i)
1983 LIST_INIT(&sep->se_conn[i]);
1985 sep->se_policy = policy ? newstr(policy) : NULL;
1992 freeconfig(struct servtab *cp)
1994 struct stabchild *sc;
1997 free(cp->se_service);
2004 free(cp->se_server);
2005 while (!LIST_EMPTY(&cp->se_children)) {
2006 sc = LIST_FIRST(&cp->se_children);
2007 LIST_REMOVE(sc, sc_link);
2010 for (i = 0; i < MAXARGV; i++)
2012 free(cp->se_argv[i]);
2015 free(cp->se_policy);
2021 * Safe skip - if skip returns null, log a syntax error in the
2022 * configuration file and exit.
2031 syslog(LOG_ERR, "%s: syntax error", CONFIG);
2045 while (*cp == ' ' || *cp == '\t')
2051 (void) ungetc(c, fconfig);
2052 if (c == ' ' || c == '\t')
2053 if ((cp = nextline(fconfig)))
2058 if (*cp == '"' || *cp == '\'')
2062 while (*cp && *cp != quote)
2065 while (*cp && *cp != ' ' && *cp != '\t')
2078 if (fgets(line, sizeof (line), fd) == NULL)
2080 cp = strchr(line, '\n');
2087 newstr(const char *cp)
2091 if ((cr = strdup(cp != NULL ? cp : "")))
2093 syslog(LOG_ERR, "strdup: %m");
2098 inetd_setproctitle(const char *a, int s)
2101 struct sockaddr_storage ss;
2102 char buf[80], pbuf[NI_MAXHOST];
2105 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) {
2106 getnameinfo((struct sockaddr *)&ss, size, pbuf, sizeof(pbuf),
2107 NULL, 0, NI_NUMERICHOST);
2108 (void) sprintf(buf, "%s [%s]", a, pbuf);
2110 (void) sprintf(buf, "%s", a);
2111 setproctitle("%s", buf);
2115 check_loop(const struct sockaddr *sa, const struct servtab *sep)
2117 struct servtab *se2;
2118 char pname[NI_MAXHOST];
2120 for (se2 = servtab; se2; se2 = se2->se_next) {
2121 if (!se2->se_bi || se2->se_socktype != SOCK_DGRAM)
2124 switch (se2->se_family) {
2126 if (csatosin(sa)->sin_port ==
2127 se2->se_ctrladdr4.sin_port)
2132 if (csatosin6(sa)->sin6_port ==
2133 se2->se_ctrladdr6.sin6_port)
2141 getnameinfo(sa, sa->sa_len, pname, sizeof(pname), NULL, 0,
2143 syslog(LOG_WARNING, "%s/%s:%s/%s loop request REFUSED from %s",
2144 sep->se_service, sep->se_proto,
2145 se2->se_service, se2->se_proto,
2154 * Dump relevant information to stderr
2157 print_service(const char *action, const struct servtab *sep)
2160 "%s: %s proto=%s accept=%d max=%d user=%s group=%s"
2164 " builtin=%p server=%s"
2169 action, sep->se_service, sep->se_proto,
2170 sep->se_accept, sep->se_maxchild, sep->se_user, sep->se_group,
2174 (void *) sep->se_bi, sep->se_server
2176 , (sep->se_policy ? sep->se_policy : "")
2181 #define CPMHSIZE 256
2182 #define CPMHMASK (CPMHSIZE-1)
2186 typedef struct CTime {
2187 unsigned long ct_Ticks;
2191 typedef struct CHash {
2193 struct in_addr c4_Addr;
2194 struct in6_addr c6_Addr;
2196 #define ch_Addr4 cu_Addr.c4_Addr
2197 #define ch_Addr6 cu_Addr.c6_Addr
2201 CTime ch_Times[CHTSIZE];
2204 static CHash CHashAry[CPMHSIZE];
2207 cpmip(const struct servtab *sep, int ctrl)
2209 struct sockaddr_storage rss;
2210 socklen_t rssLen = sizeof(rss);
2214 * If getpeername() fails, just let it through (if logging is
2215 * enabled the condition is caught elsewhere)
2218 if (sep->se_maxcpm > 0 &&
2219 (sep->se_family == AF_INET || sep->se_family == AF_INET6) &&
2220 getpeername(ctrl, (struct sockaddr *)&rss, &rssLen) == 0 ) {
2221 time_t t = time(NULL);
2222 unsigned int hv = 0xABC3D20F;
2225 CHash *chBest = NULL;
2226 unsigned int ticks = t / CHTGRAN;
2227 struct sockaddr_in *sin4;
2229 struct sockaddr_in6 *sin6;
2232 sin4 = (struct sockaddr_in *)&rss;
2234 sin6 = (struct sockaddr_in6 *)&rss;
2240 switch (rss.ss_family) {
2242 p = (char *)&sin4->sin_addr;
2243 addrlen = sizeof(struct in_addr);
2247 p = (char *)&sin6->sin6_addr;
2248 addrlen = sizeof(struct in6_addr);
2252 /* should not happen */
2256 for (i = 0; i < addrlen; ++i, ++p) {
2257 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2259 hv = (hv ^ (hv >> 16));
2261 for (i = 0; i < 5; ++i) {
2262 CHash *ch = &CHashAry[(hv + i) & CPMHMASK];
2264 if (rss.ss_family == AF_INET &&
2265 ch->ch_Family == AF_INET &&
2266 sin4->sin_addr.s_addr == ch->ch_Addr4.s_addr &&
2267 ch->ch_Service && strcmp(sep->se_service,
2268 ch->ch_Service) == 0) {
2273 if (rss.ss_family == AF_INET6 &&
2274 ch->ch_Family == AF_INET6 &&
2275 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2276 &ch->ch_Addr6) != 0 &&
2277 ch->ch_Service && strcmp(sep->se_service,
2278 ch->ch_Service) == 0) {
2283 if (chBest == NULL || ch->ch_LTime == 0 ||
2284 ch->ch_LTime < chBest->ch_LTime) {
2288 if ((rss.ss_family == AF_INET &&
2289 (chBest->ch_Family != AF_INET ||
2290 sin4->sin_addr.s_addr != chBest->ch_Addr4.s_addr)) ||
2291 chBest->ch_Service == NULL ||
2292 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2293 chBest->ch_Family = sin4->sin_family;
2294 chBest->ch_Addr4 = sin4->sin_addr;
2295 free(chBest->ch_Service);
2296 chBest->ch_Service = strdup(sep->se_service);
2297 memset(chBest->ch_Times, 0, sizeof(chBest->ch_Times));
2300 if ((rss.ss_family == AF_INET6 &&
2301 (chBest->ch_Family != AF_INET6 ||
2302 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2303 &chBest->ch_Addr6) == 0)) ||
2304 chBest->ch_Service == NULL ||
2305 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2306 chBest->ch_Family = sin6->sin6_family;
2307 chBest->ch_Addr6 = sin6->sin6_addr;
2308 free(chBest->ch_Service);
2309 chBest->ch_Service = strdup(sep->se_service);
2310 memset(chBest->ch_Times, 0, sizeof(chBest->ch_Times));
2313 chBest->ch_LTime = t;
2315 CTime *ct = &chBest->ch_Times[ticks % CHTSIZE];
2316 if (ct->ct_Ticks != ticks) {
2317 ct->ct_Ticks = ticks;
2322 for (i = 0; i < CHTSIZE; ++i) {
2323 CTime *ct = &chBest->ch_Times[i];
2324 if (ct->ct_Ticks <= ticks &&
2325 ct->ct_Ticks >= ticks - CHTSIZE) {
2326 cnt += ct->ct_Count;
2329 if ((cnt * 60) / (CHTSIZE * CHTGRAN) > sep->se_maxcpm) {
2330 char pname[NI_MAXHOST];
2332 getnameinfo((struct sockaddr *)&rss,
2333 ((struct sockaddr *)&rss)->sa_len,
2334 pname, sizeof(pname), NULL, 0,
2338 "%s from %s exceeded counts/min (limit %d/min)",
2339 sep->se_service, pname,
2346 static struct conninfo *
2347 search_conn(struct servtab *sep, int ctrl)
2349 struct sockaddr_storage ss;
2350 socklen_t sslen = sizeof(ss);
2351 struct conninfo *conn;
2353 char pname[NI_MAXHOST], pname2[NI_MAXHOST];
2355 if (sep->se_maxperip <= 0)
2359 * If getpeername() fails, just let it through (if logging is
2360 * enabled the condition is caught elsewhere)
2362 if (getpeername(ctrl, (struct sockaddr *)&ss, &sslen) != 0)
2365 switch (ss.ss_family) {
2367 hv = hashval((char *)&((struct sockaddr_in *)&ss)->sin_addr,
2368 sizeof(struct in_addr));
2372 hv = hashval((char *)&((struct sockaddr_in6 *)&ss)->sin6_addr,
2373 sizeof(struct in6_addr));
2378 * Since we only support AF_INET and AF_INET6, just
2379 * let other than AF_INET and AF_INET6 through.
2384 if (getnameinfo((struct sockaddr *)&ss, sslen, pname, sizeof(pname),
2385 NULL, 0, NI_NUMERICHOST) != 0)
2388 LIST_FOREACH(conn, &sep->se_conn[hv], co_link) {
2389 if (getnameinfo((struct sockaddr *)&conn->co_addr,
2390 conn->co_addr.ss_len, pname2, sizeof(pname2), NULL, 0,
2391 NI_NUMERICHOST) == 0 &&
2392 strcmp(pname, pname2) == 0)
2397 if ((conn = malloc(sizeof(struct conninfo))) == NULL) {
2398 syslog(LOG_ERR, "malloc: %m");
2401 conn->co_proc = reallocarray(NULL, sep->se_maxperip,
2402 sizeof(*conn->co_proc));
2403 if (conn->co_proc == NULL) {
2404 syslog(LOG_ERR, "reallocarray: %m");
2407 memcpy(&conn->co_addr, (struct sockaddr *)&ss, sslen);
2408 conn->co_numchild = 0;
2409 LIST_INSERT_HEAD(&sep->se_conn[hv], conn, co_link);
2413 * Since a child process is not invoked yet, we cannot
2414 * determine a pid of a child. So, co_proc and co_numchild
2415 * should be filled leter.
2422 room_conn(struct servtab *sep, struct conninfo *conn)
2424 char pname[NI_MAXHOST];
2426 if (conn->co_numchild >= sep->se_maxperip) {
2427 getnameinfo((struct sockaddr *)&conn->co_addr,
2428 conn->co_addr.ss_len, pname, sizeof(pname), NULL, 0,
2430 syslog(LOG_ERR, "%s from %s exceeded counts (limit %d)",
2431 sep->se_service, pname, sep->se_maxperip);
2438 addchild_conn(struct conninfo *conn, pid_t pid)
2440 struct procinfo *proc;
2445 if ((proc = search_proc(pid, 1)) != NULL) {
2446 if (proc->pr_conn != NULL) {
2448 "addchild_conn: child already on process list");
2451 proc->pr_conn = conn;
2454 conn->co_proc[conn->co_numchild++] = proc;
2458 reapchild_conn(pid_t pid)
2460 struct procinfo *proc;
2461 struct conninfo *conn;
2464 if ((proc = search_proc(pid, 0)) == NULL)
2466 if ((conn = proc->pr_conn) == NULL)
2468 for (i = 0; i < conn->co_numchild; ++i)
2469 if (conn->co_proc[i] == proc) {
2470 conn->co_proc[i] = conn->co_proc[--conn->co_numchild];
2478 resize_conn(struct servtab *sep, int maxpip)
2480 struct conninfo *conn;
2483 if (sep->se_maxperip <= 0)
2489 for (i = 0; i < PERIPSIZE; ++i) {
2490 LIST_FOREACH(conn, &sep->se_conn[i], co_link) {
2491 for (j = maxpip; j < conn->co_numchild; ++j)
2492 free_proc(conn->co_proc[j]);
2493 conn->co_proc = reallocarray(conn->co_proc, maxpip,
2494 sizeof(*conn->co_proc));
2495 if (conn->co_proc == NULL) {
2496 syslog(LOG_ERR, "reallocarray: %m");
2499 if (conn->co_numchild > maxpip)
2500 conn->co_numchild = maxpip;
2506 free_connlist(struct servtab *sep)
2508 struct conninfo *conn, *conn_temp;
2511 for (i = 0; i < PERIPSIZE; ++i) {
2512 LIST_FOREACH_SAFE(conn, &sep->se_conn[i], co_link, conn_temp) {
2514 LIST_REMOVE(conn, co_link);
2517 for (j = 0; j < conn->co_numchild; ++j)
2518 free_proc(conn->co_proc[j]);
2519 conn->co_numchild = 0;
2526 free_conn(struct conninfo *conn)
2530 if (conn->co_numchild <= 0) {
2531 LIST_REMOVE(conn, co_link);
2532 free(conn->co_proc);
2537 static struct procinfo *
2538 search_proc(pid_t pid, int add)
2540 struct procinfo *proc;
2543 hv = hashval((char *)&pid, sizeof(pid));
2544 LIST_FOREACH(proc, &proctable[hv], pr_link) {
2545 if (proc->pr_pid == pid)
2548 if (proc == NULL && add) {
2549 if ((proc = malloc(sizeof(struct procinfo))) == NULL) {
2550 syslog(LOG_ERR, "malloc: %m");
2554 proc->pr_conn = NULL;
2555 LIST_INSERT_HEAD(&proctable[hv], proc, pr_link);
2561 free_proc(struct procinfo *proc)
2565 LIST_REMOVE(proc, pr_link);
2570 hashval(char *p, int len)
2572 unsigned int hv = 0xABC3D20F;
2575 for (i = 0; i < len; ++i, ++p)
2576 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2577 hv = (hv ^ (hv >> 16)) & (PERIPSIZE - 1);