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
33 static const char copyright[] =
34 "@(#) Copyright (c) 1983, 1991, 1993, 1994\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)from: inetd.c 8.4 (Berkeley) 4/13/94";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 * Inetd - Internet super-server
50 * This program invokes all internet services as needed. Connection-oriented
51 * services are invoked each time a connection is made, by creating a process.
52 * This process is passed the connection as file descriptor 0 and is expected
53 * to do a getpeername to find out the source host and port.
55 * Datagram oriented services are invoked when a datagram
56 * arrives; a process is created and passed a pending message
57 * on file descriptor 0. Datagram servers may either connect
58 * to their peer, freeing up the original socket for inetd
59 * to receive further messages on, or ``take over the socket'',
60 * processing all arriving datagrams and, eventually, timing
61 * out. The first type of server is said to be ``multi-threaded'';
62 * the second type of server ``single-threaded''.
64 * Inetd uses a configuration file which is read at startup
65 * and, possibly, at some later time in response to a hangup signal.
66 * The configuration file is ``free format'' with fields given in the
67 * order shown below. Continuation lines for an entry must begin with
68 * a space or tab. All fields must be present in each entry.
70 * service name must be in /etc/services
71 * or name a tcpmux service
72 * or specify a unix domain socket
73 * socket type stream/dgram/raw/rdm/seqpacket
74 * protocol tcp[4][6], udp[4][6], unix
75 * wait/nowait single-threaded/multi-threaded
76 * user[:group][/login-class] user/group/login-class to run daemon as
77 * server program full path name
78 * server program arguments maximum of MAXARGS (20)
80 * TCP services without official port numbers are handled with the
81 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for
82 * requests. When a connection is made from a foreign host, the service
83 * requested is passed to tcpmux, which looks it up in the servtab list
84 * and returns the proper entry for the service. Tcpmux returns a
85 * negative reply if the service doesn't exist, otherwise the invoked
86 * server is expected to return the positive reply if the service type in
87 * inetd.conf file has the prefix "tcpmux/". If the service type has the
88 * prefix "tcpmux/+", tcpmux will return the positive reply for the
89 * process; this is for compatibility with older server code, and also
90 * allows you to invoke programs that use stdin/stdout without putting any
91 * special server code in them. Services that use tcpmux are "nowait"
92 * because they do not have a well-known port and hence cannot listen
96 * service name/version must be in /etc/rpc
97 * socket type stream/dgram/raw/rdm/seqpacket
98 * protocol rpc/tcp[4][6], rpc/udp[4][6]
99 * wait/nowait single-threaded/multi-threaded
100 * user[:group][/login-class] user/group/login-class to run daemon as
101 * server program full path name
102 * server program arguments maximum of MAXARGS
104 * Comment lines are indicated by a `#' in column 1.
107 * Comment lines that start with "#@" denote IPsec policy string, as described
108 * in ipsec_set_policy(3). This will affect all the following items in
109 * inetd.conf(8). To reset the policy, just use "#@" line. By default,
110 * there's no IPsec policy.
113 #include <sys/param.h>
114 #include <sys/ioctl.h>
115 #include <sys/mman.h>
116 #include <sys/wait.h>
117 #include <sys/time.h>
118 #include <sys/resource.h>
119 #include <sys/stat.h>
122 #include <netinet/in.h>
123 #include <netinet/tcp.h>
124 #include <arpa/inet.h>
126 #include <rpc/pmap_clnt.h>
141 #include <sysexits.h>
149 #include "pathnames.h"
152 #include <netipsec/ipsec.h>
153 #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
158 #ifndef LIBWRAP_ALLOW_FACILITY
159 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
161 #ifndef LIBWRAP_ALLOW_SEVERITY
162 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
164 #ifndef LIBWRAP_DENY_FACILITY
165 # define LIBWRAP_DENY_FACILITY LOG_AUTH
167 #ifndef LIBWRAP_DENY_SEVERITY
168 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
171 #define ISWRAP(sep) \
172 ( ((wrap_ex && !(sep)->se_bi) || (wrap_bi && (sep)->se_bi)) \
173 && (sep->se_family == AF_INET || sep->se_family == AF_INET6) \
174 && ( ((sep)->se_accept && (sep)->se_socktype == SOCK_STREAM) \
175 || (sep)->se_socktype == SOCK_DGRAM))
178 #include <login_cap.h>
181 #define RESOURCE_RC "daemon"
186 #define MAXCHILD -1 /* maximum number of this service
191 #define MAXCPM -1 /* rate limit invocations from a
192 single remote address,
197 #define MAXPERIP -1 /* maximum number of this service
198 from a single remote address,
203 #define TOOMANY 256 /* don't start more than TOOMANY */
205 #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
206 #define RETRYTIME (60*10) /* retry after bind or server fail */
207 #define MAX_MAXCHLD 32767 /* max allowable max children */
209 #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
211 #define satosin(sa) ((struct sockaddr_in *)(void *)sa)
212 #define csatosin(sa) ((const struct sockaddr_in *)(const void *)sa)
214 #define satosin6(sa) ((struct sockaddr_in6 *)(void *)sa)
215 #define csatosin6(sa) ((const struct sockaddr_in6 *)(const void *)sa)
217 static void close_sep(struct servtab *);
218 static void flag_signal(int);
219 static void config(void);
220 static int cpmip(const struct servtab *, int);
221 static void endconfig(void);
222 static struct servtab *enter(struct servtab *);
223 static void freeconfig(struct servtab *);
224 static struct servtab *getconfigent(void);
225 static int matchservent(const char *, const char *, const char *);
226 static char *nextline(FILE *);
227 static void addchild(struct servtab *, int);
228 static void reapchild(void);
229 static void enable(struct servtab *);
230 static void disable(struct servtab *);
231 static void retry(void);
232 static int setconfig(void);
233 static void setup(struct servtab *);
235 static void ipsecsetup(struct servtab *);
237 static void unregisterrpc(register struct servtab *sep);
238 static struct conninfo *search_conn(struct servtab *sep, int ctrl);
239 static int room_conn(struct servtab *sep, struct conninfo *conn);
240 static void addchild_conn(struct conninfo *conn, pid_t pid);
241 static void reapchild_conn(pid_t pid);
242 static void free_conn(struct conninfo *conn);
243 static void resize_conn(struct servtab *sep, int maxperip);
244 static void free_connlist(struct servtab *sep);
245 static void free_proc(struct procinfo *);
246 static struct procinfo *search_proc(pid_t pid, int add);
247 static int hashval(char *p, int len);
248 static char *skip(char **);
249 static char *sskip(char **);
250 static char *newstr(const char *);
251 static void print_service(const char *, const struct servtab *);
257 static int wrap_ex = 0;
258 static int wrap_bi = 0;
260 static int dolog = 0;
261 static int maxsock; /* highest-numbered descriptor */
262 static fd_set allsock;
264 static int timingout;
265 static int toomany = TOOMANY;
266 static int maxchild = MAXCHILD;
267 static int maxcpm = MAXCPM;
268 static int maxperip = MAXPERIP;
269 static struct servent *sp;
270 static struct rpcent *rpc;
271 static char *hostname = NULL;
272 static struct sockaddr_in *bind_sa4;
273 static int v4bind_ok = 0;
275 static struct sockaddr_in6 *bind_sa6;
276 static int v6bind_ok = 0;
278 static int signalpipe[2];
286 struct servtab *servtab;
288 static const char *CONFIG = _PATH_INETDCONF;
289 static const char *pid_file = _PATH_INETDPID;
290 static struct pidfh *pfh = NULL;
292 static struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
294 static LIST_HEAD(, procinfo) proctable[PERIPSIZE];
297 getvalue(const char *arg, int *value, const char *whine)
302 tmp = strtol(arg, &p, 0);
304 syslog(LOG_ERR, whine, arg);
305 return 1; /* failure */
308 return 0; /* success */
313 whichaf(struct request_info *req)
317 sa = (struct sockaddr *)req->client->sin;
321 if (sa->sa_family == AF_INET6 &&
322 IN6_IS_ADDR_V4MAPPED(&satosin6(sa)->sin6_addr))
325 return sa->sa_family;
330 main(int argc, char **argv)
335 struct sigaction sa, saalrm, sachld, sahup, sapipe;
340 login_cap_t *lc = NULL;
343 struct request_info req;
345 char *service = NULL;
347 struct sockaddr_storage peer;
349 struct addrinfo hints, *res;
350 const char *servname;
352 struct conninfo *conn;
354 openlog("inetd", LOG_PID | LOG_NOWAIT | LOG_PERROR, LOG_DAEMON);
356 while ((ch = getopt(argc, argv, "dlwWR:a:c:C:p:s:")) != -1)
366 getvalue(optarg, &toomany,
367 "-R %s: bad value for service invocation rate");
370 getvalue(optarg, &maxchild,
371 "-c %s: bad value for maximum children");
374 getvalue(optarg, &maxcpm,
375 "-C %s: bad value for maximum children/minute");
384 getvalue(optarg, &maxperip,
385 "-s %s: bad value for maximum children per source address");
396 "usage: inetd [-dlwW] [-a address] [-R rate]"
397 " [-c maximum] [-C rate]"
398 " [-p pidfile] [conf-file]");
402 * Initialize Bind Addrs.
403 * When hostname is NULL, wild card bind addrs are obtained from
404 * getaddrinfo(). But getaddrinfo() requires at least one of
405 * hostname or servname is non NULL.
406 * So when hostname is NULL, set dummy value to servname.
407 * Since getaddrinfo() doesn't accept numeric servname, and
408 * we doesn't use ai_socktype of struct addrinfo returned
409 * from getaddrinfo(), we set dummy value to ai_socktype.
411 servname = (hostname == NULL) ? "0" /* dummy */ : NULL;
413 bzero(&hints, sizeof(struct addrinfo));
414 hints.ai_flags = AI_PASSIVE;
415 hints.ai_family = AF_UNSPEC;
416 hints.ai_socktype = SOCK_STREAM; /* dummy */
417 error = getaddrinfo(hostname, servname, &hints, &res);
419 syslog(LOG_ERR, "-a %s: %s", hostname, gai_strerror(error));
420 if (error == EAI_SYSTEM)
421 syslog(LOG_ERR, "%s", strerror(errno));
425 if (res->ai_addr == NULL) {
426 syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
429 switch (res->ai_addr->sa_family) {
433 bind_sa4 = satosin(res->ai_addr);
434 /* init port num in case servname is dummy */
435 bind_sa4->sin_port = 0;
442 bind_sa6 = satosin6(res->ai_addr);
443 /* init port num in case servname is dummy */
444 bind_sa6->sin6_port = 0;
455 } while ((res = res->ai_next) != NULL);
461 syslog(LOG_ERR, "-a %s: unknown address family", hostname);
467 umask(mask = umask(0777));
474 if (access(CONFIG, R_OK) < 0)
475 syslog(LOG_ERR, "Accessing %s: %m, continuing anyway.", CONFIG);
479 pfh = pidfile_open(pid_file, 0600, &otherpid);
481 if (errno == EEXIST) {
482 syslog(LOG_ERR, "%s already running, pid: %d",
483 getprogname(), otherpid);
486 syslog(LOG_WARNING, "pidfile_open() failed: %m");
489 if (daemon(0, 0) < 0) {
490 syslog(LOG_WARNING, "daemon(0,0) failed: %m");
492 /* From now on we don't want syslog messages going to stderr. */
494 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
496 * In case somebody has started inetd manually, we need to
497 * clear the logname, so that old servers run as root do not
498 * get the user's logname..
500 if (setlogin("") < 0) {
501 syslog(LOG_WARNING, "cannot clear logname: %m");
502 /* no big deal if it fails.. */
504 if (pfh != NULL && pidfile_write(pfh) == -1) {
505 syslog(LOG_WARNING, "pidfile_write(): %m");
509 if (madvise(NULL, 0, MADV_PROTECT) != 0)
510 syslog(LOG_WARNING, "madvise() failed: %s", strerror(errno));
512 for (i = 0; i < PERIPSIZE; ++i)
513 LIST_INIT(&proctable[i]);
516 udpconf = getnetconfigent("udp");
517 tcpconf = getnetconfigent("tcp");
518 if (udpconf == NULL || tcpconf == NULL) {
519 syslog(LOG_ERR, "unknown rpc/udp or rpc/tcp");
525 udp6conf = getnetconfigent("udp6");
526 tcp6conf = getnetconfigent("tcp6");
527 if (udp6conf == NULL || tcp6conf == NULL) {
528 syslog(LOG_ERR, "unknown rpc/udp6 or rpc/tcp6");
534 sa = (struct sigaction){
536 .sa_handler = flag_signal,
538 sigemptyset(&sa.sa_mask);
539 sigaddset(&sa.sa_mask, SIGALRM);
540 sigaddset(&sa.sa_mask, SIGCHLD);
541 sigaddset(&sa.sa_mask, SIGHUP);
542 sigaction(SIGALRM, &sa, &saalrm);
544 sigaction(SIGHUP, &sa, &sahup);
545 sigaction(SIGCHLD, &sa, &sachld);
546 sa.sa_handler = SIG_IGN;
547 sigaction(SIGPIPE, &sa, &sapipe);
550 /* space for daemons to overwrite environment for ps */
551 #define DUMMYSIZE 100
552 char dummy[DUMMYSIZE];
554 (void)memset(dummy, 'x', DUMMYSIZE - 1);
555 dummy[DUMMYSIZE - 1] = '\0';
556 (void)setenv("inetd_dummy", dummy, 1);
559 if (pipe2(signalpipe, O_CLOEXEC) != 0) {
560 syslog(LOG_ERR, "pipe: %m");
563 FD_SET(signalpipe[0], &allsock);
567 if (signalpipe[0] > maxsock)
568 maxsock = signalpipe[0];
569 if (signalpipe[1] > maxsock)
570 maxsock = signalpipe[1];
578 syslog(LOG_ERR, "%s: nsock=0", __func__);
583 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
584 (fd_set *)0, (struct timeval *)0)) <= 0) {
585 if (n < 0 && errno != EINTR) {
586 syslog(LOG_WARNING, "select: %m");
591 /* handle any queued signal flags */
592 if (FD_ISSET(signalpipe[0], &readable)) {
595 if (ioctl(signalpipe[0], FIONREAD, &nsig) != 0) {
596 syslog(LOG_ERR, "ioctl: %m");
599 nsig /= sizeof(signo);
600 while (--nsig >= 0) {
603 len = read(signalpipe[0], &signo, sizeof(signo));
604 if (len != sizeof(signo)) {
605 syslog(LOG_ERR, "read: %m");
609 warnx("handling signal flag %d", signo);
623 for (sep = servtab; n && sep; sep = sep->se_next)
624 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
627 warnx("someone wants %s", sep->se_service);
628 dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep);
630 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) {
632 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
633 syslog(LOG_ERR, "ioctl (FIONBIO, 1): %m");
634 ctrl = accept(sep->se_fd, (struct sockaddr *)0,
637 warnx("accept, ctrl %d", ctrl);
641 "accept (for %s): %m",
643 if (sep->se_accept &&
644 sep->se_socktype == SOCK_STREAM)
649 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
650 syslog(LOG_ERR, "ioctl1(FIONBIO, 0): %m");
651 if (ioctl(ctrl, FIONBIO, &i) < 0)
652 syslog(LOG_ERR, "ioctl2(FIONBIO, 0): %m");
653 if (cpmip(sep, ctrl) < 0) {
658 (conn = search_conn(sep, ctrl)) != NULL &&
659 !room_conn(sep, conn)) {
665 if (dolog && !ISWRAP(sep)) {
666 char pname[NI_MAXHOST] = "unknown";
669 if (getpeername(ctrl, (struct sockaddr *)
672 if (recvfrom(ctrl, buf, sizeof(buf),
674 (struct sockaddr *)&peer,
676 getnameinfo((struct sockaddr *)&peer,
678 pname, sizeof(pname),
679 NULL, 0, NI_NUMERICHOST);
682 getnameinfo((struct sockaddr *)&peer,
684 pname, sizeof(pname),
685 NULL, 0, NI_NUMERICHOST);
687 syslog(LOG_INFO,"%s from %s", sep->se_service, pname);
689 (void) sigblock(SIGBLOCK);
692 * Fork for all external services, builtins which need to
693 * fork and anything we're wrapping (as wrapping might
694 * block or use hosts_options(5) twist).
697 if (sep->se_count++ == 0)
698 (void)clock_gettime(CLOCK_MONOTONIC_FAST, &sep->se_time);
699 else if (toomany > 0 && sep->se_count >= toomany) {
702 (void)clock_gettime(CLOCK_MONOTONIC_FAST, &now);
703 if (now.tv_sec - sep->se_time.tv_sec >
709 "%s/%s server failing (looping), service terminated",
710 sep->se_service, sep->se_proto);
711 if (sep->se_accept &&
712 sep->se_socktype == SOCK_STREAM)
727 syslog(LOG_ERR, "fork: %m");
728 if (sep->se_accept &&
729 sep->se_socktype == SOCK_STREAM)
737 addchild_conn(conn, pid);
744 sigaction(SIGALRM, &saalrm, (struct sigaction *)0);
745 sigaction(SIGCHLD, &sachld, (struct sigaction *)0);
746 sigaction(SIGHUP, &sahup, (struct sigaction *)0);
747 /* SIGPIPE reset before exec */
750 * Call tcpmux to find the real service to exec.
753 sep->se_bi->bi_fn == (bi_fn_t *) tcpmux) {
762 inetd_setproctitle("wrapping", ctrl);
763 service = sep->se_server_name ?
764 sep->se_server_name : sep->se_service;
765 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, 0);
767 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
768 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
769 denied = !hosts_access(&req);
771 syslog(deny_severity,
772 "refused connection from %.500s, service %s (%s%s)",
773 eval_client(&req), service, sep->se_proto,
774 (whichaf(&req) == AF_INET6) ? "6" : "");
775 if (sep->se_socktype != SOCK_STREAM)
776 recv(ctrl, buf, sizeof (buf), 0);
783 syslog(allow_severity,
784 "connection from %.500s, service %s (%s%s)",
785 eval_client(&req), service, sep->se_proto,
786 (whichaf(&req) == AF_INET6) ? "6" : "");
791 (*sep->se_bi->bi_fn)(ctrl, sep);
795 getpid(), sep->se_server);
796 /* Clear close-on-exec. */
797 if (fcntl(ctrl, F_SETFD, 0) < 0) {
799 "%s/%s: fcntl (F_SETFD, 0): %m",
800 sep->se_service, sep->se_proto);
809 if ((pwd = getpwnam(sep->se_user)) == NULL) {
811 "%s/%s: %s: no such user",
812 sep->se_service, sep->se_proto,
814 if (sep->se_socktype != SOCK_STREAM)
815 recv(0, buf, sizeof (buf), 0);
819 if ( sep->se_group != NULL
820 && (grp = getgrnam(sep->se_group)) == NULL
823 "%s/%s: %s: no such group",
824 sep->se_service, sep->se_proto,
826 if (sep->se_socktype != SOCK_STREAM)
827 recv(0, buf, sizeof (buf), 0);
831 pwd->pw_gid = grp->gr_gid;
833 if ((lc = login_getclass(sep->se_class)) == NULL) {
834 /* error syslogged by getclass */
836 "%s/%s: %s: login class error",
837 sep->se_service, sep->se_proto,
839 if (sep->se_socktype != SOCK_STREAM)
840 recv(0, buf, sizeof (buf), 0);
846 "%s: can't setsid(): %m",
848 /* _exit(EX_OSERR); not fatal yet */
851 if (setusercontext(lc, pwd, pwd->pw_uid,
852 LOGIN_SETALL & ~LOGIN_SETMAC)
855 "%s: can't setusercontext(..%s..): %m",
856 sep->se_service, sep->se_user);
862 if (setlogin(sep->se_user) < 0) {
864 "%s: can't setlogin(%s): %m",
865 sep->se_service, sep->se_user);
866 /* _exit(EX_OSERR); not yet */
868 if (setgid(pwd->pw_gid) < 0) {
870 "%s: can't set gid %d: %m",
871 sep->se_service, pwd->pw_gid);
874 (void) initgroups(pwd->pw_name,
876 if (setuid(pwd->pw_uid) < 0) {
878 "%s: can't set uid %d: %m",
879 sep->se_service, pwd->pw_uid);
884 sigaction(SIGPIPE, &sapipe,
885 (struct sigaction *)0);
886 execv(sep->se_server, sep->se_argv);
888 "cannot execute %s: %m", sep->se_server);
889 if (sep->se_socktype != SOCK_STREAM)
890 recv(0, buf, sizeof (buf), 0);
895 if (sep->se_accept && sep->se_socktype == SOCK_STREAM)
902 * Add a signal flag to the signal flag queue for later handling
906 flag_signal(int signo)
910 len = write(signalpipe[1], &signo, sizeof(signo));
911 if (len != sizeof(signo)) {
912 syslog(LOG_ERR, "write: %m");
918 * Record a new child pid for this service. If we've reached the
919 * limit on children, then stop accepting incoming requests.
923 addchild(struct servtab *sep, pid_t pid)
925 if (sep->se_maxchild <= 0)
928 if (sep->se_numchild >= sep->se_maxchild) {
929 syslog(LOG_ERR, "%s: %d >= %d",
930 __func__, sep->se_numchild, sep->se_maxchild);
934 sep->se_pids[sep->se_numchild++] = pid;
935 if (sep->se_numchild == sep->se_maxchild)
947 pid = wait3(&status, WNOHANG, (struct rusage *)0);
951 warnx("%d reaped, %s %u", pid,
952 WIFEXITED(status) ? "status" : "signal",
953 WIFEXITED(status) ? WEXITSTATUS(status)
955 for (sep = servtab; sep; sep = sep->se_next) {
956 for (k = 0; k < sep->se_numchild; k++)
957 if (sep->se_pids[k] == pid)
959 if (k == sep->se_numchild)
961 if (sep->se_numchild == sep->se_maxchild)
963 sep->se_pids[k] = sep->se_pids[--sep->se_numchild];
964 if (WIFSIGNALED(status) || WEXITSTATUS(status))
966 "%s[%d]: exited, %s %u",
968 WIFEXITED(status) ? "status" : "signal",
969 WIFEXITED(status) ? WEXITSTATUS(status)
980 struct servtab *sep, *new, **sepp;
984 login_cap_t *lc = NULL;
988 syslog(LOG_ERR, "%s: %m", CONFIG);
991 for (sep = servtab; sep; sep = sep->se_next)
993 while ((new = getconfigent())) {
994 if (getpwnam(new->se_user) == NULL) {
996 "%s/%s: no such user '%s', service ignored",
997 new->se_service, new->se_proto, new->se_user);
1000 if (new->se_group && getgrnam(new->se_group) == NULL) {
1002 "%s/%s: no such group '%s', service ignored",
1003 new->se_service, new->se_proto, new->se_group);
1007 if ((lc = login_getclass(new->se_class)) == NULL) {
1008 /* error syslogged by getclass */
1010 "%s/%s: %s: login class error, service ignored",
1011 new->se_service, new->se_proto, new->se_class);
1016 new_nomapped = new->se_nomapped;
1017 for (sep = servtab; sep; sep = sep->se_next)
1018 if (strcmp(sep->se_service, new->se_service) == 0 &&
1019 strcmp(sep->se_proto, new->se_proto) == 0 &&
1020 sep->se_rpc == new->se_rpc &&
1021 sep->se_socktype == new->se_socktype &&
1022 sep->se_family == new->se_family)
1027 #define SWAP(t,a, b) { t c = a; a = b; b = c; }
1028 omask = sigblock(SIGBLOCK);
1029 if (sep->se_nomapped != new->se_nomapped) {
1030 /* for rpc keep old nommaped till unregister */
1032 sep->se_nomapped = new->se_nomapped;
1035 /* copy over outstanding child pids */
1036 if (sep->se_maxchild > 0 && new->se_maxchild > 0) {
1037 new->se_numchild = sep->se_numchild;
1038 if (new->se_numchild > new->se_maxchild)
1039 new->se_numchild = new->se_maxchild;
1040 memcpy(new->se_pids, sep->se_pids,
1041 new->se_numchild * sizeof(*new->se_pids));
1043 SWAP(pid_t *, sep->se_pids, new->se_pids);
1044 sep->se_maxchild = new->se_maxchild;
1045 sep->se_numchild = new->se_numchild;
1046 sep->se_maxcpm = new->se_maxcpm;
1047 resize_conn(sep, new->se_maxperip);
1048 sep->se_maxperip = new->se_maxperip;
1049 sep->se_bi = new->se_bi;
1050 /* might need to turn on or off service now */
1051 if (sep->se_fd >= 0) {
1052 if (sep->se_maxchild > 0
1053 && sep->se_numchild == sep->se_maxchild) {
1054 if (FD_ISSET(sep->se_fd, &allsock))
1057 if (!FD_ISSET(sep->se_fd, &allsock))
1061 sep->se_accept = new->se_accept;
1062 SWAP(char *, sep->se_user, new->se_user);
1063 SWAP(char *, sep->se_group, new->se_group);
1065 SWAP(char *, sep->se_class, new->se_class);
1067 SWAP(char *, sep->se_server, new->se_server);
1068 SWAP(char *, sep->se_server_name, new->se_server_name);
1069 for (i = 0; i < MAXARGV; i++)
1070 SWAP(char *, sep->se_argv[i], new->se_argv[i]);
1072 SWAP(char *, sep->se_policy, new->se_policy);
1078 print_service("REDO", sep);
1082 print_service("ADD ", sep);
1084 sep->se_checked = 1;
1089 switch (sep->se_family) {
1106 if (sep->se_family != AF_UNIX) {
1107 sp = getservbyname(sep->se_service, sep->se_proto);
1109 syslog(LOG_ERR, "%s/%s: unknown service",
1110 sep->se_service, sep->se_proto);
1111 sep->se_checked = 0;
1115 switch (sep->se_family) {
1117 if (sp->s_port != sep->se_ctrladdr4.sin_port) {
1118 sep->se_ctrladdr4.sin_port =
1126 sep->se_ctrladdr6.sin6_port) {
1127 sep->se_ctrladdr6.sin6_port =
1134 if (sep->se_reset != 0 && sep->se_fd >= 0)
1137 rpc = getrpcbyname(sep->se_service);
1139 syslog(LOG_ERR, "%s/%s unknown RPC service",
1140 sep->se_service, sep->se_proto);
1141 if (sep->se_fd != -1)
1142 (void) close(sep->se_fd);
1146 if (sep->se_reset != 0 ||
1147 rpc->r_number != sep->se_rpc_prog) {
1148 if (sep->se_rpc_prog)
1150 sep->se_rpc_prog = rpc->r_number;
1151 if (sep->se_fd != -1)
1152 (void) close(sep->se_fd);
1155 sep->se_nomapped = new_nomapped;
1158 if (sep->se_fd == -1)
1163 * Purge anything not looked at above.
1165 omask = sigblock(SIGBLOCK);
1167 while ((sep = *sepp)) {
1168 if (sep->se_checked) {
1169 sepp = &sep->se_next;
1172 *sepp = sep->se_next;
1173 if (sep->se_fd >= 0)
1176 print_service("FREE", sep);
1177 if (sep->se_rpc && sep->se_rpc_prog > 0)
1182 (void) sigsetmask(omask);
1186 unregisterrpc(struct servtab *sep)
1189 struct servtab *sepp;
1191 struct netconfig *netid4, *netid6;
1193 omask = sigblock(SIGBLOCK);
1194 netid4 = sep->se_socktype == SOCK_DGRAM ? udpconf : tcpconf;
1195 netid6 = sep->se_socktype == SOCK_DGRAM ? udp6conf : tcp6conf;
1196 if (sep->se_family == AF_INET)
1198 else if (sep->se_nomapped)
1201 * Conflict if same prog and protocol - In that case one should look
1202 * to versions, but it is not interesting: having separate servers for
1203 * different versions does not work well.
1204 * Therefore one do not unregister if there is a conflict.
1205 * There is also transport conflict if destroying INET when INET46
1206 * exists, or destroying INET46 when INET exists
1208 for (sepp = servtab; sepp; sepp = sepp->se_next) {
1211 if (sepp->se_checked == 0 ||
1213 strcmp(sep->se_proto, sepp->se_proto) != 0 ||
1214 sep->se_rpc_prog != sepp->se_rpc_prog)
1216 if (sepp->se_family == AF_INET)
1218 if (sepp->se_family == AF_INET6) {
1220 if (!sep->se_nomapped)
1223 if (netid4 == NULL && netid6 == NULL)
1227 print_service("UNREG", sep);
1228 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1230 rpcb_unset(sep->se_rpc_prog, i, netid4);
1232 rpcb_unset(sep->se_rpc_prog, i, netid6);
1234 if (sep->se_fd != -1)
1235 (void) close(sep->se_fd);
1237 (void) sigsetmask(omask);
1243 struct servtab *sep;
1246 for (sep = servtab; sep; sep = sep->se_next)
1247 if (sep->se_fd == -1 && !ISMUX(sep))
1252 setup(struct servtab *sep)
1256 /* Set all listening sockets to close-on-exec. */
1257 if ((sep->se_fd = socket(sep->se_family,
1258 sep->se_socktype | SOCK_CLOEXEC, 0)) < 0) {
1260 warn("socket failed on %s/%s",
1261 sep->se_service, sep->se_proto);
1262 syslog(LOG_ERR, "%s/%s: socket: %m",
1263 sep->se_service, sep->se_proto);
1266 #define turnon(fd, opt) \
1267 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
1268 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
1269 turnon(sep->se_fd, SO_DEBUG) < 0)
1270 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
1271 if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
1272 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
1274 if (turnon(sep->se_fd, SO_PRIVSTATE) < 0)
1275 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m");
1277 /* tftpd opens a new connection then needs more infos */
1279 if ((sep->se_family == AF_INET6) &&
1280 (strcmp(sep->se_proto, "udp") == 0) &&
1281 (sep->se_accept == 0) &&
1282 (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1283 (char *)&on, sizeof (on)) < 0))
1284 syslog(LOG_ERR, "setsockopt (IPV6_RECVPKTINFO): %m");
1285 if (sep->se_family == AF_INET6) {
1286 int flag = sep->se_nomapped ? 1 : 0;
1287 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_V6ONLY,
1288 (char *)&flag, sizeof (flag)) < 0)
1289 syslog(LOG_ERR, "setsockopt (IPV6_V6ONLY): %m");
1296 if (sep->se_family == AF_UNIX) {
1297 (void) unlink(sep->se_ctrladdr_un.sun_path);
1298 umask(0777); /* Make socket with conservative permissions */
1300 if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr,
1301 sep->se_ctrladdr_size) < 0) {
1303 warn("bind failed on %s/%s",
1304 sep->se_service, sep->se_proto);
1305 syslog(LOG_ERR, "%s/%s: bind: %m",
1306 sep->se_service, sep->se_proto);
1307 (void) close(sep->se_fd);
1313 if (sep->se_family == AF_UNIX)
1317 if (sep->se_family == AF_UNIX) {
1318 /* Ick - fch{own,mod} don't work on Unix domain sockets */
1319 if (chown(sep->se_service, sep->se_sockuid, sep->se_sockgid) < 0)
1320 syslog(LOG_ERR, "chown socket: %m");
1321 if (chmod(sep->se_service, sep->se_sockmode) < 0)
1322 syslog(LOG_ERR, "chmod socket: %m");
1327 socklen_t len = sep->se_ctrladdr_size;
1328 struct netconfig *netid, *netid2 = NULL;
1330 struct sockaddr_in sock;
1332 struct netbuf nbuf, nbuf2;
1334 if (getsockname(sep->se_fd,
1335 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){
1336 syslog(LOG_ERR, "%s/%s: getsockname: %m",
1337 sep->se_service, sep->se_proto);
1338 (void) close(sep->se_fd);
1342 nbuf.buf = &sep->se_ctrladdr;
1343 nbuf.len = sep->se_ctrladdr.sa_len;
1344 if (sep->se_family == AF_INET)
1345 netid = sep->se_socktype==SOCK_DGRAM? udpconf:tcpconf;
1348 netid = sep->se_socktype==SOCK_DGRAM? udp6conf:tcp6conf;
1349 if (!sep->se_nomapped) { /* INET and INET6 */
1350 netid2 = netid==udp6conf? udpconf:tcpconf;
1351 memset(&sock, 0, sizeof sock); /* ADDR_ANY */
1353 nbuf2.len = sock.sin_len = sizeof sock;
1354 sock.sin_family = AF_INET;
1355 sock.sin_port = sep->se_ctrladdr6.sin6_port;
1360 print_service("REG ", sep);
1361 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1362 rpcb_unset(sep->se_rpc_prog, i, netid);
1363 rpcb_set(sep->se_rpc_prog, i, netid, &nbuf);
1365 rpcb_unset(sep->se_rpc_prog, i, netid2);
1366 rpcb_set(sep->se_rpc_prog, i, netid2, &nbuf2);
1370 if (sep->se_socktype == SOCK_STREAM)
1371 listen(sep->se_fd, -1);
1374 warnx("registered %s on %d",
1375 sep->se_server, sep->se_fd);
1381 ipsecsetup(struct servtab *sep)
1384 char *policy_in = NULL;
1385 char *policy_out = NULL;
1389 switch (sep->se_family) {
1392 opt = IP_IPSEC_POLICY;
1396 level = IPPROTO_IPV6;
1397 opt = IPV6_IPSEC_POLICY;
1404 if (!sep->se_policy || sep->se_policy[0] == '\0') {
1405 static char def_in[] = "in entrust", def_out[] = "out entrust";
1407 policy_out = def_out;
1409 if (!strncmp("in", sep->se_policy, 2))
1410 policy_in = sep->se_policy;
1411 else if (!strncmp("out", sep->se_policy, 3))
1412 policy_out = sep->se_policy;
1414 syslog(LOG_ERR, "invalid security policy \"%s\"",
1420 if (policy_in != NULL) {
1421 buf = ipsec_set_policy(policy_in, strlen(policy_in));
1423 if (setsockopt(sep->se_fd, level, opt,
1424 buf, ipsec_get_policylen(buf)) < 0 &&
1426 warnx("%s/%s: ipsec initialization failed; %s",
1427 sep->se_service, sep->se_proto,
1431 syslog(LOG_ERR, "invalid security policy \"%s\"",
1434 if (policy_out != NULL) {
1435 buf = ipsec_set_policy(policy_out, strlen(policy_out));
1437 if (setsockopt(sep->se_fd, level, opt,
1438 buf, ipsec_get_policylen(buf)) < 0 &&
1440 warnx("%s/%s: ipsec initialization failed; %s",
1441 sep->se_service, sep->se_proto,
1445 syslog(LOG_ERR, "invalid security policy \"%s\"",
1452 * Finish with a service and its socket.
1455 close_sep(struct servtab *sep)
1457 if (sep->se_fd >= 0) {
1458 if (FD_ISSET(sep->se_fd, &allsock))
1460 (void) close(sep->se_fd);
1464 sep->se_numchild = 0; /* forget about any existing children */
1468 matchservent(const char *name1, const char *name2, const char *proto)
1473 if (strcmp(proto, "unix") == 0) {
1474 if ((p = strrchr(name1, '/')) != NULL)
1476 if ((p = strrchr(name2, '/')) != NULL)
1479 if (strcmp(name1, name2) == 0)
1481 if ((se = getservbyname(name1, proto)) != NULL) {
1482 if (strcmp(name2, se->s_name) == 0)
1484 for (alias = se->s_aliases; *alias; alias++)
1485 if (strcmp(name2, *alias) == 0)
1491 static struct servtab *
1492 enter(struct servtab *cp)
1494 struct servtab *sep;
1497 sep = (struct servtab *)malloc(sizeof (*sep));
1498 if (sep == (struct servtab *)0) {
1499 syslog(LOG_ERR, "malloc: %m");
1504 omask = sigblock(SIGBLOCK);
1505 sep->se_next = servtab;
1512 enable(struct servtab *sep)
1516 "enabling %s, fd %d", sep->se_service, sep->se_fd);
1518 if (sep->se_fd < 0) {
1520 "%s: %s: bad fd", __func__, sep->se_service);
1525 "%s: %s: is mux", __func__, sep->se_service);
1528 if (FD_ISSET(sep->se_fd, &allsock)) {
1530 "%s: %s: not off", __func__, sep->se_service);
1535 FD_SET(sep->se_fd, &allsock);
1536 if (sep->se_fd > maxsock)
1537 maxsock = sep->se_fd;
1541 disable(struct servtab *sep)
1545 "disabling %s, fd %d", sep->se_service, sep->se_fd);
1547 if (sep->se_fd < 0) {
1549 "%s: %s: bad fd", __func__, sep->se_service);
1554 "%s: %s: is mux", __func__, sep->se_service);
1557 if (!FD_ISSET(sep->se_fd, &allsock)) {
1559 "%s: %s: not on", __func__, sep->se_service);
1563 syslog(LOG_ERR, "%s: nsock=0", __func__);
1568 FD_CLR(sep->se_fd, &allsock);
1569 if (sep->se_fd == maxsock)
1573 static FILE *fconfig = NULL;
1574 static struct servtab serv;
1575 static char line[LINE_MAX];
1581 if (fconfig != NULL) {
1582 fseek(fconfig, 0L, SEEK_SET);
1585 fconfig = fopen(CONFIG, "r");
1586 return (fconfig != NULL);
1593 (void) fclose(fconfig);
1598 static struct servtab *
1601 struct servtab *sep = &serv;
1605 static char TCPMUX_TOKEN[] = "tcpmux/";
1606 #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
1624 while ((cp = nextline(fconfig)) != NULL) {
1626 /* lines starting with #@ is not a comment, but the policy */
1627 if (cp[0] == '#' && cp[1] == '@') {
1629 for (p = cp + 2; p && *p && isspace(*p); p++)
1635 } else if (ipsec_get_policylen(p) >= 0) {
1641 "%s: invalid ipsec policy \"%s\"",
1647 if (*cp == '#' || *cp == '\0')
1652 return ((struct servtab *)0);
1654 * clear the static buffer, since some fields (se_ctrladdr,
1655 * for example) don't get initialized here.
1657 memset(sep, 0, sizeof *sep);
1660 /* got an empty line containing just blanks/tabs. */
1663 if (arg[0] == ':') { /* :user:group:perm: */
1664 char *user, *group, *perm;
1668 if ((group = strchr(user, ':')) == NULL) {
1669 syslog(LOG_ERR, "no group after user '%s'", user);
1673 if ((perm = strchr(group, ':')) == NULL) {
1674 syslog(LOG_ERR, "no mode after group '%s'", group);
1678 if ((pw = getpwnam(user)) == NULL) {
1679 syslog(LOG_ERR, "no such user '%s'", user);
1682 sep->se_sockuid = pw->pw_uid;
1683 if ((gr = getgrnam(group)) == NULL) {
1684 syslog(LOG_ERR, "no such user '%s'", group);
1687 sep->se_sockgid = gr->gr_gid;
1688 sep->se_sockmode = strtol(perm, &arg, 8);
1690 syslog(LOG_ERR, "bad mode '%s'", perm);
1695 sep->se_sockuid = euid;
1696 sep->se_sockgid = egid;
1697 sep->se_sockmode = 0200;
1699 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
1700 char *c = arg + MUX_LEN;
1702 sep->se_type = MUXPLUS_TYPE;
1705 sep->se_type = MUX_TYPE;
1706 sep->se_service = newstr(c);
1708 sep->se_service = newstr(arg);
1709 sep->se_type = NORM_TYPE;
1712 if (strcmp(arg, "stream") == 0)
1713 sep->se_socktype = SOCK_STREAM;
1714 else if (strcmp(arg, "dgram") == 0)
1715 sep->se_socktype = SOCK_DGRAM;
1716 else if (strcmp(arg, "rdm") == 0)
1717 sep->se_socktype = SOCK_RDM;
1718 else if (strcmp(arg, "seqpacket") == 0)
1719 sep->se_socktype = SOCK_SEQPACKET;
1720 else if (strcmp(arg, "raw") == 0)
1721 sep->se_socktype = SOCK_RAW;
1723 sep->se_socktype = -1;
1726 if (strncmp(arg, "tcp", 3) == 0) {
1727 sep->se_proto = newstr(strsep(&arg, "/"));
1728 if (arg != NULL && (strcmp(arg, "faith") == 0)) {
1729 syslog(LOG_ERR, "faith has been deprecated");
1733 if (sep->se_type == NORM_TYPE &&
1734 strncmp(arg, "faith/", 6) == 0) {
1735 syslog(LOG_ERR, "faith has been deprecated");
1738 sep->se_proto = newstr(arg);
1740 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1741 memmove(sep->se_proto, sep->se_proto + 4,
1742 strlen(sep->se_proto) + 1 - 4);
1744 sep->se_rpc_prog = sep->se_rpc_lowvers =
1745 sep->se_rpc_highvers = 0;
1746 if ((versp = strrchr(sep->se_service, '/'))) {
1748 switch (sscanf(versp, "%u-%u",
1749 &sep->se_rpc_lowvers,
1750 &sep->se_rpc_highvers)) {
1754 sep->se_rpc_highvers =
1755 sep->se_rpc_lowvers;
1759 "bad RPC version specifier; %s",
1766 sep->se_rpc_lowvers =
1767 sep->se_rpc_highvers = 1;
1770 sep->se_nomapped = 0;
1771 if (strcmp(sep->se_proto, "unix") == 0) {
1772 sep->se_family = AF_UNIX;
1774 while (isdigit(sep->se_proto[strlen(sep->se_proto) - 1])) {
1776 if (sep->se_proto[strlen(sep->se_proto) - 1] == '6') {
1777 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1782 if (sep->se_proto[strlen(sep->se_proto) - 1] == '4') {
1783 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1787 /* illegal version num */
1788 syslog(LOG_ERR, "bad IP version for %s", sep->se_proto);
1793 if (v6bind && !v6bind_ok) {
1794 syslog(LOG_INFO, "IPv6 bind is ignored for %s",
1796 if (v4bind && v4bind_ok)
1804 sep->se_family = AF_INET6;
1805 if (!v4bind || !v4bind_ok)
1806 sep->se_nomapped = 1;
1809 { /* default to v4 bind if not v6 bind */
1811 syslog(LOG_NOTICE, "IPv4 bind is ignored for %s",
1816 sep->se_family = AF_INET;
1820 switch(sep->se_family) {
1822 memcpy(&sep->se_ctrladdr4, bind_sa4,
1823 sizeof(sep->se_ctrladdr4));
1824 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr4);
1828 memcpy(&sep->se_ctrladdr6, bind_sa6,
1829 sizeof(sep->se_ctrladdr6));
1830 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr6);
1834 if (strlen(sep->se_service) >= sizeof(sep->se_ctrladdr_un.sun_path)) {
1836 "domain socket pathname too long for service %s",
1840 memset(&sep->se_ctrladdr, 0, sizeof(sep->se_ctrladdr));
1841 sep->se_ctrladdr_un.sun_family = sep->se_family;
1842 sep->se_ctrladdr_un.sun_len = strlen(sep->se_service);
1843 strcpy(sep->se_ctrladdr_un.sun_path, sep->se_service);
1844 sep->se_ctrladdr_size = SUN_LEN(&sep->se_ctrladdr_un);
1847 if (!strncmp(arg, "wait", 4))
1849 else if (!strncmp(arg, "nowait", 6))
1853 "%s: bad wait/nowait for service %s",
1854 CONFIG, sep->se_service);
1857 sep->se_maxchild = -1;
1858 sep->se_maxcpm = -1;
1859 sep->se_maxperip = -1;
1860 if ((s = strchr(arg, '/')) != NULL) {
1864 val = strtoul(s + 1, &eptr, 10);
1865 if (eptr == s + 1 || val > MAX_MAXCHLD) {
1867 "%s: bad max-child for service %s",
1868 CONFIG, sep->se_service);
1872 if (!sep->se_accept && val != 1)
1873 warnx("maxchild=%lu for wait service %s"
1874 " not recommended", val, sep->se_service);
1875 sep->se_maxchild = val;
1877 sep->se_maxcpm = strtol(eptr + 1, &eptr, 10);
1879 sep->se_maxperip = strtol(eptr + 1, &eptr, 10);
1881 * explicitly do not check for \0 for future expansion /
1882 * backwards compatibility
1887 * Silently enforce "nowait" mode for TCPMUX services
1888 * since they don't have an assigned port to listen on.
1891 if (strcmp(sep->se_proto, "tcp")) {
1893 "%s: bad protocol for tcpmux service %s",
1894 CONFIG, sep->se_service);
1897 if (sep->se_socktype != SOCK_STREAM) {
1899 "%s: bad socket type for tcpmux service %s",
1900 CONFIG, sep->se_service);
1904 sep->se_user = newstr(sskip(&cp));
1906 if ((s = strrchr(sep->se_user, '/')) != NULL) {
1908 sep->se_class = newstr(s + 1);
1910 sep->se_class = newstr(RESOURCE_RC);
1912 if ((s = strrchr(sep->se_user, ':')) != NULL) {
1914 sep->se_group = newstr(s + 1);
1916 sep->se_group = NULL;
1917 sep->se_server = newstr(sskip(&cp));
1918 if ((sep->se_server_name = strrchr(sep->se_server, '/')))
1919 sep->se_server_name++;
1920 if (strcmp(sep->se_server, "internal") == 0) {
1923 for (bi = biltins; bi->bi_service; bi++)
1924 if (bi->bi_socktype == sep->se_socktype &&
1925 matchservent(bi->bi_service, sep->se_service,
1928 if (bi->bi_service == 0) {
1929 syslog(LOG_ERR, "internal service %s unknown",
1933 sep->se_accept = 1; /* force accept mode for built-ins */
1937 if (sep->se_maxperip < 0)
1938 sep->se_maxperip = maxperip;
1939 if (sep->se_maxcpm < 0)
1940 sep->se_maxcpm = maxcpm;
1941 if (sep->se_maxchild < 0) { /* apply default max-children */
1942 if (sep->se_bi && sep->se_bi->bi_maxchild >= 0)
1943 sep->se_maxchild = sep->se_bi->bi_maxchild;
1944 else if (sep->se_accept)
1945 sep->se_maxchild = MAX(maxchild, 0);
1947 sep->se_maxchild = 1;
1949 if (sep->se_maxchild > 0) {
1950 sep->se_pids = malloc(sep->se_maxchild * sizeof(*sep->se_pids));
1951 if (sep->se_pids == NULL) {
1952 syslog(LOG_ERR, "malloc: %m");
1957 for (arg = skip(&cp); cp; arg = skip(&cp))
1958 if (argc < MAXARGV) {
1959 sep->se_argv[argc++] = newstr(arg);
1962 "%s: too many arguments for service %s",
1963 CONFIG, sep->se_service);
1966 while (argc <= MAXARGV)
1967 sep->se_argv[argc++] = NULL;
1968 for (i = 0; i < PERIPSIZE; ++i)
1969 LIST_INIT(&sep->se_conn[i]);
1971 sep->se_policy = policy ? newstr(policy) : NULL;
1977 freeconfig(struct servtab *cp)
1982 free(cp->se_service);
1994 free(cp->se_server);
1997 for (i = 0; i < MAXARGV; i++)
1999 free(cp->se_argv[i]);
2003 free(cp->se_policy);
2009 * Safe skip - if skip returns null, log a syntax error in the
2010 * configuration file and exit.
2019 syslog(LOG_ERR, "%s: syntax error", CONFIG);
2033 while (*cp == ' ' || *cp == '\t')
2039 (void) ungetc(c, fconfig);
2040 if (c == ' ' || c == '\t')
2041 if ((cp = nextline(fconfig)))
2046 if (*cp == '"' || *cp == '\'')
2050 while (*cp && *cp != quote)
2053 while (*cp && *cp != ' ' && *cp != '\t')
2066 if (fgets(line, sizeof (line), fd) == NULL)
2068 cp = strchr(line, '\n');
2075 newstr(const char *cp)
2079 if ((cr = strdup(cp != NULL ? cp : "")))
2081 syslog(LOG_ERR, "strdup: %m");
2086 inetd_setproctitle(const char *a, int s)
2089 struct sockaddr_storage ss;
2090 char buf[80], pbuf[NI_MAXHOST];
2093 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) {
2094 getnameinfo((struct sockaddr *)&ss, size, pbuf, sizeof(pbuf),
2095 NULL, 0, NI_NUMERICHOST);
2096 (void) sprintf(buf, "%s [%s]", a, pbuf);
2098 (void) sprintf(buf, "%s", a);
2099 setproctitle("%s", buf);
2103 check_loop(const struct sockaddr *sa, const struct servtab *sep)
2105 struct servtab *se2;
2106 char pname[NI_MAXHOST];
2108 for (se2 = servtab; se2; se2 = se2->se_next) {
2109 if (!se2->se_bi || se2->se_socktype != SOCK_DGRAM)
2112 switch (se2->se_family) {
2114 if (csatosin(sa)->sin_port ==
2115 se2->se_ctrladdr4.sin_port)
2120 if (csatosin6(sa)->sin6_port ==
2121 se2->se_ctrladdr6.sin6_port)
2129 getnameinfo(sa, sa->sa_len, pname, sizeof(pname), NULL, 0,
2131 syslog(LOG_WARNING, "%s/%s:%s/%s loop request REFUSED from %s",
2132 sep->se_service, sep->se_proto,
2133 se2->se_service, se2->se_proto,
2142 * Dump relevant information to stderr
2145 print_service(const char *action, const struct servtab *sep)
2148 "%s: %s proto=%s accept=%d max=%d user=%s group=%s"
2152 " builtin=%p server=%s"
2157 action, sep->se_service, sep->se_proto,
2158 sep->se_accept, sep->se_maxchild, sep->se_user, sep->se_group,
2162 (void *) sep->se_bi, sep->se_server
2164 , (sep->se_policy ? sep->se_policy : "")
2169 #define CPMHSIZE 256
2170 #define CPMHMASK (CPMHSIZE-1)
2174 typedef struct CTime {
2175 unsigned long ct_Ticks;
2179 typedef struct CHash {
2181 struct in_addr c4_Addr;
2182 struct in6_addr c6_Addr;
2184 #define ch_Addr4 cu_Addr.c4_Addr
2185 #define ch_Addr6 cu_Addr.c6_Addr
2189 CTime ch_Times[CHTSIZE];
2192 static CHash CHashAry[CPMHSIZE];
2195 cpmip(const struct servtab *sep, int ctrl)
2197 struct sockaddr_storage rss;
2198 socklen_t rssLen = sizeof(rss);
2202 * If getpeername() fails, just let it through (if logging is
2203 * enabled the condition is caught elsewhere)
2206 if (sep->se_maxcpm > 0 &&
2207 (sep->se_family == AF_INET || sep->se_family == AF_INET6) &&
2208 getpeername(ctrl, (struct sockaddr *)&rss, &rssLen) == 0 ) {
2209 time_t t = time(NULL);
2210 int hv = 0xABC3D20F;
2213 CHash *chBest = NULL;
2214 unsigned int ticks = t / CHTGRAN;
2215 struct sockaddr_in *sin4;
2217 struct sockaddr_in6 *sin6;
2220 sin4 = (struct sockaddr_in *)&rss;
2222 sin6 = (struct sockaddr_in6 *)&rss;
2228 switch (rss.ss_family) {
2230 p = (char *)&sin4->sin_addr;
2231 addrlen = sizeof(struct in_addr);
2235 p = (char *)&sin6->sin6_addr;
2236 addrlen = sizeof(struct in6_addr);
2240 /* should not happen */
2244 for (i = 0; i < addrlen; ++i, ++p) {
2245 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2247 hv = (hv ^ (hv >> 16));
2249 for (i = 0; i < 5; ++i) {
2250 CHash *ch = &CHashAry[(hv + i) & CPMHMASK];
2252 if (rss.ss_family == AF_INET &&
2253 ch->ch_Family == AF_INET &&
2254 sin4->sin_addr.s_addr == ch->ch_Addr4.s_addr &&
2255 ch->ch_Service && strcmp(sep->se_service,
2256 ch->ch_Service) == 0) {
2261 if (rss.ss_family == AF_INET6 &&
2262 ch->ch_Family == AF_INET6 &&
2263 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2264 &ch->ch_Addr6) != 0 &&
2265 ch->ch_Service && strcmp(sep->se_service,
2266 ch->ch_Service) == 0) {
2271 if (chBest == NULL || ch->ch_LTime == 0 ||
2272 ch->ch_LTime < chBest->ch_LTime) {
2276 if ((rss.ss_family == AF_INET &&
2277 (chBest->ch_Family != AF_INET ||
2278 sin4->sin_addr.s_addr != chBest->ch_Addr4.s_addr)) ||
2279 chBest->ch_Service == NULL ||
2280 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2281 chBest->ch_Family = sin4->sin_family;
2282 chBest->ch_Addr4 = sin4->sin_addr;
2283 if (chBest->ch_Service)
2284 free(chBest->ch_Service);
2285 chBest->ch_Service = strdup(sep->se_service);
2286 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2289 if ((rss.ss_family == AF_INET6 &&
2290 (chBest->ch_Family != AF_INET6 ||
2291 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2292 &chBest->ch_Addr6) == 0)) ||
2293 chBest->ch_Service == NULL ||
2294 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2295 chBest->ch_Family = sin6->sin6_family;
2296 chBest->ch_Addr6 = sin6->sin6_addr;
2297 if (chBest->ch_Service)
2298 free(chBest->ch_Service);
2299 chBest->ch_Service = strdup(sep->se_service);
2300 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2303 chBest->ch_LTime = t;
2305 CTime *ct = &chBest->ch_Times[ticks % CHTSIZE];
2306 if (ct->ct_Ticks != ticks) {
2307 ct->ct_Ticks = ticks;
2312 for (i = 0; i < CHTSIZE; ++i) {
2313 CTime *ct = &chBest->ch_Times[i];
2314 if (ct->ct_Ticks <= ticks &&
2315 ct->ct_Ticks >= ticks - CHTSIZE) {
2316 cnt += ct->ct_Count;
2319 if ((cnt * 60) / (CHTSIZE * CHTGRAN) > sep->se_maxcpm) {
2320 char pname[NI_MAXHOST];
2322 getnameinfo((struct sockaddr *)&rss,
2323 ((struct sockaddr *)&rss)->sa_len,
2324 pname, sizeof(pname), NULL, 0,
2328 "%s from %s exceeded counts/min (limit %d/min)",
2329 sep->se_service, pname,
2336 static struct conninfo *
2337 search_conn(struct servtab *sep, int ctrl)
2339 struct sockaddr_storage ss;
2340 socklen_t sslen = sizeof(ss);
2341 struct conninfo *conn;
2343 char pname[NI_MAXHOST], pname2[NI_MAXHOST];
2345 if (sep->se_maxperip <= 0)
2349 * If getpeername() fails, just let it through (if logging is
2350 * enabled the condition is caught elsewhere)
2352 if (getpeername(ctrl, (struct sockaddr *)&ss, &sslen) != 0)
2355 switch (ss.ss_family) {
2357 hv = hashval((char *)&((struct sockaddr_in *)&ss)->sin_addr,
2358 sizeof(struct in_addr));
2362 hv = hashval((char *)&((struct sockaddr_in6 *)&ss)->sin6_addr,
2363 sizeof(struct in6_addr));
2368 * Since we only support AF_INET and AF_INET6, just
2369 * let other than AF_INET and AF_INET6 through.
2374 if (getnameinfo((struct sockaddr *)&ss, sslen, pname, sizeof(pname),
2375 NULL, 0, NI_NUMERICHOST) != 0)
2378 LIST_FOREACH(conn, &sep->se_conn[hv], co_link) {
2379 if (getnameinfo((struct sockaddr *)&conn->co_addr,
2380 conn->co_addr.ss_len, pname2, sizeof(pname2), NULL, 0,
2381 NI_NUMERICHOST) == 0 &&
2382 strcmp(pname, pname2) == 0)
2387 if ((conn = malloc(sizeof(struct conninfo))) == NULL) {
2388 syslog(LOG_ERR, "malloc: %m");
2391 conn->co_proc = malloc(sep->se_maxperip * sizeof(*conn->co_proc));
2392 if (conn->co_proc == NULL) {
2393 syslog(LOG_ERR, "malloc: %m");
2396 memcpy(&conn->co_addr, (struct sockaddr *)&ss, sslen);
2397 conn->co_numchild = 0;
2398 LIST_INSERT_HEAD(&sep->se_conn[hv], conn, co_link);
2402 * Since a child process is not invoked yet, we cannot
2403 * determine a pid of a child. So, co_proc and co_numchild
2404 * should be filled leter.
2411 room_conn(struct servtab *sep, struct conninfo *conn)
2413 char pname[NI_MAXHOST];
2415 if (conn->co_numchild >= sep->se_maxperip) {
2416 getnameinfo((struct sockaddr *)&conn->co_addr,
2417 conn->co_addr.ss_len, pname, sizeof(pname), NULL, 0,
2419 syslog(LOG_ERR, "%s from %s exceeded counts (limit %d)",
2420 sep->se_service, pname, sep->se_maxperip);
2427 addchild_conn(struct conninfo *conn, pid_t pid)
2429 struct procinfo *proc;
2434 if ((proc = search_proc(pid, 1)) != NULL) {
2435 if (proc->pr_conn != NULL) {
2437 "addchild_conn: child already on process list");
2440 proc->pr_conn = conn;
2443 conn->co_proc[conn->co_numchild++] = proc;
2447 reapchild_conn(pid_t pid)
2449 struct procinfo *proc;
2450 struct conninfo *conn;
2453 if ((proc = search_proc(pid, 0)) == NULL)
2455 if ((conn = proc->pr_conn) == NULL)
2457 for (i = 0; i < conn->co_numchild; ++i)
2458 if (conn->co_proc[i] == proc) {
2459 conn->co_proc[i] = conn->co_proc[--conn->co_numchild];
2467 resize_conn(struct servtab *sep, int maxpip)
2469 struct conninfo *conn;
2472 if (sep->se_maxperip <= 0)
2478 for (i = 0; i < PERIPSIZE; ++i) {
2479 LIST_FOREACH(conn, &sep->se_conn[i], co_link) {
2480 for (j = maxpip; j < conn->co_numchild; ++j)
2481 free_proc(conn->co_proc[j]);
2482 conn->co_proc = realloc(conn->co_proc,
2483 maxpip * sizeof(*conn->co_proc));
2484 if (conn->co_proc == NULL) {
2485 syslog(LOG_ERR, "realloc: %m");
2488 if (conn->co_numchild > maxpip)
2489 conn->co_numchild = maxpip;
2495 free_connlist(struct servtab *sep)
2497 struct conninfo *conn;
2500 for (i = 0; i < PERIPSIZE; ++i) {
2501 while ((conn = LIST_FIRST(&sep->se_conn[i])) != NULL) {
2502 for (j = 0; j < conn->co_numchild; ++j)
2503 free_proc(conn->co_proc[j]);
2504 conn->co_numchild = 0;
2511 free_conn(struct conninfo *conn)
2515 if (conn->co_numchild <= 0) {
2516 LIST_REMOVE(conn, co_link);
2517 free(conn->co_proc);
2522 static struct procinfo *
2523 search_proc(pid_t pid, int add)
2525 struct procinfo *proc;
2528 hv = hashval((char *)&pid, sizeof(pid));
2529 LIST_FOREACH(proc, &proctable[hv], pr_link) {
2530 if (proc->pr_pid == pid)
2533 if (proc == NULL && add) {
2534 if ((proc = malloc(sizeof(struct procinfo))) == NULL) {
2535 syslog(LOG_ERR, "malloc: %m");
2539 proc->pr_conn = NULL;
2540 LIST_INSERT_HEAD(&proctable[hv], proc, pr_link);
2546 free_proc(struct procinfo *proc)
2550 LIST_REMOVE(proc, pr_link);
2555 hashval(char *p, int len)
2557 int i, hv = 0xABC3D20F;
2559 for (i = 0; i < len; ++i, ++p)
2560 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2561 hv = (hv ^ (hv >> 16)) & (PERIPSIZE - 1);