2 * Copyright (c) 1983, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1991, 1993, 1994\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)from: inetd.c 8.4 (Berkeley) 4/13/94";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * Inetd - Internet super-server
48 * This program invokes all internet services as needed. Connection-oriented
49 * services are invoked each time a connection is made, by creating a process.
50 * This process is passed the connection as file descriptor 0 and is expected
51 * to do a getpeername to find out the source host and port.
53 * Datagram oriented services are invoked when a datagram
54 * arrives; a process is created and passed a pending message
55 * on file descriptor 0. Datagram servers may either connect
56 * to their peer, freeing up the original socket for inetd
57 * to receive further messages on, or ``take over the socket'',
58 * processing all arriving datagrams and, eventually, timing
59 * out. The first type of server is said to be ``multi-threaded'';
60 * the second type of server ``single-threaded''.
62 * Inetd uses a configuration file which is read at startup
63 * and, possibly, at some later time in response to a hangup signal.
64 * The configuration file is ``free format'' with fields given in the
65 * order shown below. Continuation lines for an entry must begin with
66 * a space or tab. All fields must be present in each entry.
68 * service name must be in /etc/services
69 * or name a tcpmux service
70 * or specify a unix domain socket
71 * socket type stream/dgram/raw/rdm/seqpacket
72 * protocol tcp[4][6][/faith], udp[4][6], unix
73 * wait/nowait single-threaded/multi-threaded
74 * user[:group][/login-class] user/group/login-class to run daemon as
75 * server program full path name
76 * server program arguments maximum of MAXARGS (20)
78 * TCP services without official port numbers are handled with the
79 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for
80 * requests. When a connection is made from a foreign host, the service
81 * requested is passed to tcpmux, which looks it up in the servtab list
82 * and returns the proper entry for the service. Tcpmux returns a
83 * negative reply if the service doesn't exist, otherwise the invoked
84 * server is expected to return the positive reply if the service type in
85 * inetd.conf file has the prefix "tcpmux/". If the service type has the
86 * prefix "tcpmux/+", tcpmux will return the positive reply for the
87 * process; this is for compatibility with older server code, and also
88 * allows you to invoke programs that use stdin/stdout without putting any
89 * special server code in them. Services that use tcpmux are "nowait"
90 * because they do not have a well-known port and hence cannot listen
94 * service name/version must be in /etc/rpc
95 * socket type stream/dgram/raw/rdm/seqpacket
96 * protocol rpc/tcp[4][6], rpc/udp[4][6]
97 * wait/nowait single-threaded/multi-threaded
98 * user[:group][/login-class] user/group/login-class to run daemon as
99 * server program full path name
100 * server program arguments maximum of MAXARGS
102 * Comment lines are indicated by a `#' in column 1.
105 * Comment lines that start with "#@" denote IPsec policy string, as described
106 * in ipsec_set_policy(3). This will affect all the following items in
107 * inetd.conf(8). To reset the policy, just use "#@" line. By default,
108 * there's no IPsec policy.
111 #include <sys/param.h>
112 #include <sys/ioctl.h>
113 #include <sys/mman.h>
114 #include <sys/wait.h>
115 #include <sys/time.h>
116 #include <sys/resource.h>
117 #include <sys/stat.h>
120 #include <netinet/in.h>
121 #include <netinet/tcp.h>
122 #include <arpa/inet.h>
124 #include <rpc/pmap_clnt.h>
139 #include <sysexits.h>
145 #include "pathnames.h"
148 #include <netipsec/ipsec.h>
149 #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
154 #ifndef LIBWRAP_ALLOW_FACILITY
155 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
157 #ifndef LIBWRAP_ALLOW_SEVERITY
158 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
160 #ifndef LIBWRAP_DENY_FACILITY
161 # define LIBWRAP_DENY_FACILITY LOG_AUTH
163 #ifndef LIBWRAP_DENY_SEVERITY
164 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
167 #define ISWRAP(sep) \
168 ( ((wrap_ex && !(sep)->se_bi) || (wrap_bi && (sep)->se_bi)) \
169 && (sep->se_family == AF_INET || sep->se_family == AF_INET6) \
170 && ( ((sep)->se_accept && (sep)->se_socktype == SOCK_STREAM) \
171 || (sep)->se_socktype == SOCK_DGRAM))
174 #include <login_cap.h>
177 #define RESOURCE_RC "daemon"
182 #define MAXCHILD -1 /* maximum number of this service
187 #define MAXCPM -1 /* rate limit invocations from a
188 single remote address,
193 #define MAXPERIP -1 /* maximum number of this service
194 from a single remote address,
199 #define TOOMANY 256 /* don't start more than TOOMANY */
201 #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
202 #define RETRYTIME (60*10) /* retry after bind or server fail */
203 #define MAX_MAXCHLD 32767 /* max allowable max children */
205 #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
207 void close_sep(struct servtab *);
208 void flag_signal(int);
209 void flag_config(int);
211 int cpmip(const struct servtab *, int);
212 void endconfig(void);
213 struct servtab *enter(struct servtab *);
214 void freeconfig(struct servtab *);
215 struct servtab *getconfigent(void);
216 int matchservent(const char *, const char *, const char *);
217 char *nextline(FILE *);
218 void addchild(struct servtab *, int);
219 void flag_reapchild(int);
220 void reapchild(void);
221 void enable(struct servtab *);
222 void disable(struct servtab *);
223 void flag_retry(int);
226 void setup(struct servtab *);
228 void ipsecsetup(struct servtab *);
230 void unregisterrpc(register struct servtab *sep);
231 static struct conninfo *search_conn(struct servtab *sep, int ctrl);
232 static int room_conn(struct servtab *sep, struct conninfo *conn);
233 static void addchild_conn(struct conninfo *conn, pid_t pid);
234 static void reapchild_conn(pid_t pid);
235 static void free_conn(struct conninfo *conn);
236 static void resize_conn(struct servtab *sep, int maxperip);
237 static void free_connlist(struct servtab *sep);
238 static void free_proc(struct procinfo *);
239 static struct procinfo *search_proc(pid_t pid, int add);
240 static int hashval(char *p, int len);
248 int maxsock; /* highest-numbered descriptor */
252 int toomany = TOOMANY;
253 int maxchild = MAXCHILD;
255 int maxperip = MAXPERIP;
258 char *hostname = NULL;
259 struct sockaddr_in *bind_sa4;
262 struct sockaddr_in6 *bind_sa6;
273 struct servtab *servtab;
275 extern struct biltin biltins[];
277 const char *CONFIG = _PATH_INETDCONF;
278 const char *pid_file = _PATH_INETDPID;
279 struct pidfh *pfh = NULL;
281 struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
283 static LIST_HEAD(, procinfo) proctable[PERIPSIZE];
286 getvalue(const char *arg, int *value, const char *whine)
291 tmp = strtol(arg, &p, 0);
293 syslog(LOG_ERR, whine, arg);
294 return 1; /* failure */
297 return 0; /* success */
301 whichaf(struct request_info *req)
305 sa = (struct sockaddr *)req->client->sin;
308 if (sa->sa_family == AF_INET6 &&
309 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr))
311 return sa->sa_family;
315 main(int argc, char **argv)
320 struct sigaction sa, saalrm, sachld, sahup, sapipe;
325 login_cap_t *lc = NULL;
327 struct request_info req;
329 char *service = NULL;
331 struct sockaddr peer_un;
332 struct sockaddr_in peer_un4;
333 struct sockaddr_in6 peer_un6;
334 struct sockaddr_storage peer_max;
336 #define peer p_un.peer_un
337 #define peer4 p_un.peer_un4
338 #define peer6 p_un.peer_un6
339 #define peermax p_un.peer_max
341 struct addrinfo hints, *res;
342 const char *servname;
344 struct conninfo *conn;
346 openlog("inetd", LOG_PID | LOG_NOWAIT | LOG_PERROR, LOG_DAEMON);
348 while ((ch = getopt(argc, argv, "dlwWR:a:c:C:p:s:")) != -1)
358 getvalue(optarg, &toomany,
359 "-R %s: bad value for service invocation rate");
362 getvalue(optarg, &maxchild,
363 "-c %s: bad value for maximum children");
366 getvalue(optarg, &maxcpm,
367 "-C %s: bad value for maximum children/minute");
376 getvalue(optarg, &maxperip,
377 "-s %s: bad value for maximum children per source address");
388 "usage: inetd [-dlwW] [-a address] [-R rate]"
389 " [-c maximum] [-C rate]"
390 " [-p pidfile] [conf-file]");
394 * Initialize Bind Addrs.
395 * When hostname is NULL, wild card bind addrs are obtained from
396 * getaddrinfo(). But getaddrinfo() requires at least one of
397 * hostname or servname is non NULL.
398 * So when hostname is NULL, set dummy value to servname.
399 * Since getaddrinfo() doesn't accept numeric servname, and
400 * we doesn't use ai_socktype of struct addrinfo returned
401 * from getaddrinfo(), we set dummy value to ai_socktype.
403 servname = (hostname == NULL) ? "0" /* dummy */ : NULL;
405 bzero(&hints, sizeof(struct addrinfo));
406 hints.ai_flags = AI_PASSIVE;
407 hints.ai_family = AF_UNSPEC;
408 hints.ai_socktype = SOCK_STREAM; /* dummy */
409 error = getaddrinfo(hostname, servname, &hints, &res);
411 syslog(LOG_ERR, "-a %s: %s", hostname, gai_strerror(error));
412 if (error == EAI_SYSTEM)
413 syslog(LOG_ERR, "%s", strerror(errno));
417 if (res->ai_addr == NULL) {
418 syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
421 switch (res->ai_addr->sa_family) {
425 bind_sa4 = (struct sockaddr_in *)res->ai_addr;
426 /* init port num in case servname is dummy */
427 bind_sa4->sin_port = 0;
434 bind_sa6 = (struct sockaddr_in6 *)res->ai_addr;
435 /* init port num in case servname is dummy */
436 bind_sa6->sin6_port = 0;
447 } while ((res = res->ai_next) != NULL);
453 syslog(LOG_ERR, "-a %s: unknown address family", hostname);
459 umask(mask = umask(0777));
466 if (access(CONFIG, R_OK) < 0)
467 syslog(LOG_ERR, "Accessing %s: %m, continuing anyway.", CONFIG);
471 pfh = pidfile_open(pid_file, 0600, &otherpid);
473 if (errno == EEXIST) {
474 syslog(LOG_ERR, "%s already running, pid: %d",
475 getprogname(), otherpid);
478 syslog(LOG_WARNING, "pidfile_open() failed: %m");
481 if (daemon(0, 0) < 0) {
482 syslog(LOG_WARNING, "daemon(0,0) failed: %m");
484 /* From now on we don't want syslog messages going to stderr. */
486 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
488 * In case somebody has started inetd manually, we need to
489 * clear the logname, so that old servers run as root do not
490 * get the user's logname..
492 if (setlogin("") < 0) {
493 syslog(LOG_WARNING, "cannot clear logname: %m");
494 /* no big deal if it fails.. */
496 if (pfh != NULL && pidfile_write(pfh) == -1) {
497 syslog(LOG_WARNING, "pidfile_write(): %m");
501 if (madvise(NULL, 0, MADV_PROTECT) != 0)
502 syslog(LOG_WARNING, "madvise() failed: %s", strerror(errno));
504 for (i = 0; i < PERIPSIZE; ++i)
505 LIST_INIT(&proctable[i]);
508 udpconf = getnetconfigent("udp");
509 tcpconf = getnetconfigent("tcp");
510 if (udpconf == NULL || tcpconf == NULL) {
511 syslog(LOG_ERR, "unknown rpc/udp or rpc/tcp");
517 udp6conf = getnetconfigent("udp6");
518 tcp6conf = getnetconfigent("tcp6");
519 if (udp6conf == NULL || tcp6conf == NULL) {
520 syslog(LOG_ERR, "unknown rpc/udp6 or rpc/tcp6");
527 sigemptyset(&sa.sa_mask);
528 sigaddset(&sa.sa_mask, SIGALRM);
529 sigaddset(&sa.sa_mask, SIGCHLD);
530 sigaddset(&sa.sa_mask, SIGHUP);
531 sa.sa_handler = flag_retry;
532 sigaction(SIGALRM, &sa, &saalrm);
534 sa.sa_handler = flag_config;
535 sigaction(SIGHUP, &sa, &sahup);
536 sa.sa_handler = flag_reapchild;
537 sigaction(SIGCHLD, &sa, &sachld);
538 sa.sa_handler = SIG_IGN;
539 sigaction(SIGPIPE, &sa, &sapipe);
542 /* space for daemons to overwrite environment for ps */
543 #define DUMMYSIZE 100
544 char dummy[DUMMYSIZE];
546 (void)memset(dummy, 'x', DUMMYSIZE - 1);
547 dummy[DUMMYSIZE - 1] = '\0';
548 (void)setenv("inetd_dummy", dummy, 1);
551 if (pipe(signalpipe) != 0) {
552 syslog(LOG_ERR, "pipe: %m");
555 if (fcntl(signalpipe[0], F_SETFD, FD_CLOEXEC) < 0 ||
556 fcntl(signalpipe[1], F_SETFD, FD_CLOEXEC) < 0) {
557 syslog(LOG_ERR, "signalpipe: fcntl (F_SETFD, FD_CLOEXEC): %m");
560 FD_SET(signalpipe[0], &allsock);
564 if (signalpipe[0] > maxsock)
565 maxsock = signalpipe[0];
566 if (signalpipe[1] > maxsock)
567 maxsock = signalpipe[1];
575 syslog(LOG_ERR, "%s: nsock=0", __func__);
580 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
581 (fd_set *)0, (struct timeval *)0)) <= 0) {
582 if (n < 0 && errno != EINTR) {
583 syslog(LOG_WARNING, "select: %m");
588 /* handle any queued signal flags */
589 if (FD_ISSET(signalpipe[0], &readable)) {
591 if (ioctl(signalpipe[0], FIONREAD, &nsig) != 0) {
592 syslog(LOG_ERR, "ioctl: %m");
595 while (--nsig >= 0) {
597 if (read(signalpipe[0], &c, 1) != 1) {
598 syslog(LOG_ERR, "read: %m");
602 warnx("handling signal flag %c", c);
604 case 'A': /* sigalrm */
607 case 'C': /* sigchld */
610 case 'H': /* sighup */
616 for (sep = servtab; n && sep; sep = sep->se_next)
617 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
620 warnx("someone wants %s", sep->se_service);
621 dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep);
623 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) {
625 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
626 syslog(LOG_ERR, "ioctl (FIONBIO, 1): %m");
627 ctrl = accept(sep->se_fd, (struct sockaddr *)0,
630 warnx("accept, ctrl %d", ctrl);
634 "accept (for %s): %m",
636 if (sep->se_accept &&
637 sep->se_socktype == SOCK_STREAM)
642 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
643 syslog(LOG_ERR, "ioctl1(FIONBIO, 0): %m");
644 if (ioctl(ctrl, FIONBIO, &i) < 0)
645 syslog(LOG_ERR, "ioctl2(FIONBIO, 0): %m");
646 if (cpmip(sep, ctrl) < 0) {
651 (conn = search_conn(sep, ctrl)) != NULL &&
652 !room_conn(sep, conn)) {
658 if (dolog && !ISWRAP(sep)) {
659 char pname[INET6_ADDRSTRLEN] = "unknown";
662 if (getpeername(ctrl, (struct sockaddr *)
665 if (recvfrom(ctrl, buf, sizeof(buf),
667 (struct sockaddr *)&peermax,
669 getnameinfo((struct sockaddr *)&peermax,
671 pname, sizeof(pname),
672 NULL, 0, NI_NUMERICHOST);
675 getnameinfo((struct sockaddr *)&peermax,
677 pname, sizeof(pname),
678 NULL, 0, NI_NUMERICHOST);
680 syslog(LOG_INFO,"%s from %s", sep->se_service, pname);
682 (void) sigblock(SIGBLOCK);
685 * Fork for all external services, builtins which need to
686 * fork and anything we're wrapping (as wrapping might
687 * block or use hosts_options(5) twist).
690 if (sep->se_count++ == 0)
691 (void)clock_gettime(CLOCK_MONOTONIC_FAST, &sep->se_time);
692 else if (toomany > 0 && sep->se_count >= toomany) {
695 (void)clock_gettime(CLOCK_MONOTONIC_FAST, &now);
696 if (now.tv_sec - sep->se_time.tv_sec >
702 "%s/%s server failing (looping), service terminated",
703 sep->se_service, sep->se_proto);
704 if (sep->se_accept &&
705 sep->se_socktype == SOCK_STREAM)
720 syslog(LOG_ERR, "fork: %m");
721 if (sep->se_accept &&
722 sep->se_socktype == SOCK_STREAM)
730 addchild_conn(conn, pid);
737 sigaction(SIGALRM, &saalrm, (struct sigaction *)0);
738 sigaction(SIGCHLD, &sachld, (struct sigaction *)0);
739 sigaction(SIGHUP, &sahup, (struct sigaction *)0);
740 /* SIGPIPE reset before exec */
743 * Call tcpmux to find the real service to exec.
746 sep->se_bi->bi_fn == (bi_fn_t *) tcpmux) {
754 inetd_setproctitle("wrapping", ctrl);
755 service = sep->se_server_name ?
756 sep->se_server_name : sep->se_service;
757 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, 0);
759 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
760 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
761 denied = !hosts_access(&req);
763 syslog(deny_severity,
764 "refused connection from %.500s, service %s (%s%s)",
765 eval_client(&req), service, sep->se_proto,
766 (whichaf(&req) == AF_INET6) ? "6" : "");
767 if (sep->se_socktype != SOCK_STREAM)
768 recv(ctrl, buf, sizeof (buf), 0);
775 syslog(allow_severity,
776 "connection from %.500s, service %s (%s%s)",
777 eval_client(&req), service, sep->se_proto,
778 (whichaf(&req) == AF_INET6) ? "6" : "");
782 (*sep->se_bi->bi_fn)(ctrl, sep);
786 getpid(), sep->se_server);
787 /* Clear close-on-exec. */
788 if (fcntl(ctrl, F_SETFD, 0) < 0) {
790 "%s/%s: fcntl (F_SETFD, 0): %m",
791 sep->se_service, sep->se_proto);
800 if ((pwd = getpwnam(sep->se_user)) == NULL) {
802 "%s/%s: %s: no such user",
803 sep->se_service, sep->se_proto,
805 if (sep->se_socktype != SOCK_STREAM)
806 recv(0, buf, sizeof (buf), 0);
810 if ( sep->se_group != NULL
811 && (grp = getgrnam(sep->se_group)) == NULL
814 "%s/%s: %s: no such group",
815 sep->se_service, sep->se_proto,
817 if (sep->se_socktype != SOCK_STREAM)
818 recv(0, buf, sizeof (buf), 0);
822 pwd->pw_gid = grp->gr_gid;
824 if ((lc = login_getclass(sep->se_class)) == NULL) {
825 /* error syslogged by getclass */
827 "%s/%s: %s: login class error",
828 sep->se_service, sep->se_proto,
830 if (sep->se_socktype != SOCK_STREAM)
831 recv(0, buf, sizeof (buf), 0);
837 "%s: can't setsid(): %m",
839 /* _exit(EX_OSERR); not fatal yet */
842 if (setusercontext(lc, pwd, pwd->pw_uid,
843 LOGIN_SETALL & ~LOGIN_SETMAC)
846 "%s: can't setusercontext(..%s..): %m",
847 sep->se_service, sep->se_user);
853 if (setlogin(sep->se_user) < 0) {
855 "%s: can't setlogin(%s): %m",
856 sep->se_service, sep->se_user);
857 /* _exit(EX_OSERR); not yet */
859 if (setgid(pwd->pw_gid) < 0) {
861 "%s: can't set gid %d: %m",
862 sep->se_service, pwd->pw_gid);
865 (void) initgroups(pwd->pw_name,
867 if (setuid(pwd->pw_uid) < 0) {
869 "%s: can't set uid %d: %m",
870 sep->se_service, pwd->pw_uid);
875 sigaction(SIGPIPE, &sapipe,
876 (struct sigaction *)0);
877 execv(sep->se_server, sep->se_argv);
879 "cannot execute %s: %m", sep->se_server);
880 if (sep->se_socktype != SOCK_STREAM)
881 recv(0, buf, sizeof (buf), 0);
886 if (sep->se_accept && sep->se_socktype == SOCK_STREAM)
893 * Add a signal flag to the signal flag queue for later handling
901 if (write(signalpipe[1], &ch, 1) != 1) {
902 syslog(LOG_ERR, "write: %m");
908 * Record a new child pid for this service. If we've reached the
909 * limit on children, then stop accepting incoming requests.
913 addchild(struct servtab *sep, pid_t pid)
915 if (sep->se_maxchild <= 0)
918 if (sep->se_numchild >= sep->se_maxchild) {
919 syslog(LOG_ERR, "%s: %d >= %d",
920 __func__, sep->se_numchild, sep->se_maxchild);
924 sep->se_pids[sep->se_numchild++] = pid;
925 if (sep->se_numchild == sep->se_maxchild)
930 * Some child process has exited. See if it's on somebody's list.
934 flag_reapchild(int signo __unused)
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)
978 flag_config(int signo __unused)
986 struct servtab *sep, *new, **sepp;
990 login_cap_t *lc = NULL;
994 syslog(LOG_ERR, "%s: %m", CONFIG);
997 for (sep = servtab; sep; sep = sep->se_next)
999 while ((new = getconfigent())) {
1000 if (getpwnam(new->se_user) == NULL) {
1002 "%s/%s: no such user '%s', service ignored",
1003 new->se_service, new->se_proto, new->se_user);
1006 if (new->se_group && getgrnam(new->se_group) == NULL) {
1008 "%s/%s: no such group '%s', service ignored",
1009 new->se_service, new->se_proto, new->se_group);
1013 if ((lc = login_getclass(new->se_class)) == NULL) {
1014 /* error syslogged by getclass */
1016 "%s/%s: %s: login class error, service ignored",
1017 new->se_service, new->se_proto, new->se_class);
1022 new_nomapped = new->se_nomapped;
1023 for (sep = servtab; sep; sep = sep->se_next)
1024 if (strcmp(sep->se_service, new->se_service) == 0 &&
1025 strcmp(sep->se_proto, new->se_proto) == 0 &&
1026 sep->se_rpc == new->se_rpc &&
1027 sep->se_socktype == new->se_socktype &&
1028 sep->se_family == new->se_family)
1033 #define SWAP(t,a, b) { t c = a; a = b; b = c; }
1034 omask = sigblock(SIGBLOCK);
1035 if (sep->se_nomapped != new->se_nomapped) {
1036 /* for rpc keep old nommaped till unregister */
1038 sep->se_nomapped = new->se_nomapped;
1041 /* copy over outstanding child pids */
1042 if (sep->se_maxchild > 0 && new->se_maxchild > 0) {
1043 new->se_numchild = sep->se_numchild;
1044 if (new->se_numchild > new->se_maxchild)
1045 new->se_numchild = new->se_maxchild;
1046 memcpy(new->se_pids, sep->se_pids,
1047 new->se_numchild * sizeof(*new->se_pids));
1049 SWAP(pid_t *, sep->se_pids, new->se_pids);
1050 sep->se_maxchild = new->se_maxchild;
1051 sep->se_numchild = new->se_numchild;
1052 sep->se_maxcpm = new->se_maxcpm;
1053 resize_conn(sep, new->se_maxperip);
1054 sep->se_maxperip = new->se_maxperip;
1055 sep->se_bi = new->se_bi;
1056 /* might need to turn on or off service now */
1057 if (sep->se_fd >= 0) {
1058 if (sep->se_maxchild > 0
1059 && sep->se_numchild == sep->se_maxchild) {
1060 if (FD_ISSET(sep->se_fd, &allsock))
1063 if (!FD_ISSET(sep->se_fd, &allsock))
1067 sep->se_accept = new->se_accept;
1068 SWAP(char *, sep->se_user, new->se_user);
1069 SWAP(char *, sep->se_group, new->se_group);
1071 SWAP(char *, sep->se_class, new->se_class);
1073 SWAP(char *, sep->se_server, new->se_server);
1074 SWAP(char *, sep->se_server_name, new->se_server_name);
1075 for (i = 0; i < MAXARGV; i++)
1076 SWAP(char *, sep->se_argv[i], new->se_argv[i]);
1078 SWAP(char *, sep->se_policy, new->se_policy);
1084 print_service("REDO", sep);
1088 print_service("ADD ", sep);
1090 sep->se_checked = 1;
1095 switch (sep->se_family) {
1112 if (sep->se_family != AF_UNIX) {
1113 sp = getservbyname(sep->se_service, sep->se_proto);
1115 syslog(LOG_ERR, "%s/%s: unknown service",
1116 sep->se_service, sep->se_proto);
1117 sep->se_checked = 0;
1121 switch (sep->se_family) {
1123 if (sp->s_port != sep->se_ctrladdr4.sin_port) {
1124 sep->se_ctrladdr4.sin_port =
1132 sep->se_ctrladdr6.sin6_port) {
1133 sep->se_ctrladdr6.sin6_port =
1140 if (sep->se_reset != 0 && sep->se_fd >= 0)
1143 rpc = getrpcbyname(sep->se_service);
1145 syslog(LOG_ERR, "%s/%s unknown RPC service",
1146 sep->se_service, sep->se_proto);
1147 if (sep->se_fd != -1)
1148 (void) close(sep->se_fd);
1152 if (sep->se_reset != 0 ||
1153 rpc->r_number != sep->se_rpc_prog) {
1154 if (sep->se_rpc_prog)
1156 sep->se_rpc_prog = rpc->r_number;
1157 if (sep->se_fd != -1)
1158 (void) close(sep->se_fd);
1161 sep->se_nomapped = new_nomapped;
1164 if (sep->se_fd == -1)
1169 * Purge anything not looked at above.
1171 omask = sigblock(SIGBLOCK);
1173 while ((sep = *sepp)) {
1174 if (sep->se_checked) {
1175 sepp = &sep->se_next;
1178 *sepp = sep->se_next;
1179 if (sep->se_fd >= 0)
1182 print_service("FREE", sep);
1183 if (sep->se_rpc && sep->se_rpc_prog > 0)
1188 (void) sigsetmask(omask);
1192 unregisterrpc(struct servtab *sep)
1195 struct servtab *sepp;
1197 struct netconfig *netid4, *netid6;
1199 omask = sigblock(SIGBLOCK);
1200 netid4 = sep->se_socktype == SOCK_DGRAM ? udpconf : tcpconf;
1201 netid6 = sep->se_socktype == SOCK_DGRAM ? udp6conf : tcp6conf;
1202 if (sep->se_family == AF_INET)
1204 else if (sep->se_nomapped)
1207 * Conflict if same prog and protocol - In that case one should look
1208 * to versions, but it is not interesting: having separate servers for
1209 * different versions does not work well.
1210 * Therefore one do not unregister if there is a conflict.
1211 * There is also transport conflict if destroying INET when INET46
1212 * exists, or destroying INET46 when INET exists
1214 for (sepp = servtab; sepp; sepp = sepp->se_next) {
1217 if (sepp->se_checked == 0 ||
1219 strcmp(sep->se_proto, sepp->se_proto) != 0 ||
1220 sep->se_rpc_prog != sepp->se_rpc_prog)
1222 if (sepp->se_family == AF_INET)
1224 if (sepp->se_family == AF_INET6) {
1226 if (!sep->se_nomapped)
1229 if (netid4 == NULL && netid6 == NULL)
1233 print_service("UNREG", sep);
1234 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1236 rpcb_unset(sep->se_rpc_prog, i, netid4);
1238 rpcb_unset(sep->se_rpc_prog, i, netid6);
1240 if (sep->se_fd != -1)
1241 (void) close(sep->se_fd);
1243 (void) sigsetmask(omask);
1247 flag_retry(int signo __unused)
1255 struct servtab *sep;
1258 for (sep = servtab; sep; sep = sep->se_next)
1259 if (sep->se_fd == -1 && !ISMUX(sep))
1264 setup(struct servtab *sep)
1268 if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
1270 warn("socket failed on %s/%s",
1271 sep->se_service, sep->se_proto);
1272 syslog(LOG_ERR, "%s/%s: socket: %m",
1273 sep->se_service, sep->se_proto);
1276 /* Set all listening sockets to close-on-exec. */
1277 if (fcntl(sep->se_fd, F_SETFD, FD_CLOEXEC) < 0) {
1278 syslog(LOG_ERR, "%s/%s: fcntl (F_SETFD, FD_CLOEXEC): %m",
1279 sep->se_service, sep->se_proto);
1283 #define turnon(fd, opt) \
1284 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
1285 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
1286 turnon(sep->se_fd, SO_DEBUG) < 0)
1287 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
1288 if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
1289 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
1291 if (turnon(sep->se_fd, SO_PRIVSTATE) < 0)
1292 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m");
1294 /* tftpd opens a new connection then needs more infos */
1295 if ((sep->se_family == AF_INET6) &&
1296 (strcmp(sep->se_proto, "udp") == 0) &&
1297 (sep->se_accept == 0) &&
1298 (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1299 (char *)&on, sizeof (on)) < 0))
1300 syslog(LOG_ERR, "setsockopt (IPV6_RECVPKTINFO): %m");
1301 if (sep->se_family == AF_INET6) {
1302 int flag = sep->se_nomapped ? 1 : 0;
1303 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_V6ONLY,
1304 (char *)&flag, sizeof (flag)) < 0)
1305 syslog(LOG_ERR, "setsockopt (IPV6_V6ONLY): %m");
1309 if (sep->se_type == FAITH_TYPE) {
1310 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_FAITH, &on,
1312 syslog(LOG_ERR, "setsockopt (IPV6_FAITH): %m");
1319 if (sep->se_family == AF_UNIX) {
1320 (void) unlink(sep->se_ctrladdr_un.sun_path);
1321 umask(0777); /* Make socket with conservative permissions */
1323 if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr,
1324 sep->se_ctrladdr_size) < 0) {
1326 warn("bind failed on %s/%s",
1327 sep->se_service, sep->se_proto);
1328 syslog(LOG_ERR, "%s/%s: bind: %m",
1329 sep->se_service, sep->se_proto);
1330 (void) close(sep->se_fd);
1336 if (sep->se_family == AF_UNIX)
1340 if (sep->se_family == AF_UNIX) {
1341 /* Ick - fch{own,mod} don't work on Unix domain sockets */
1342 if (chown(sep->se_service, sep->se_sockuid, sep->se_sockgid) < 0)
1343 syslog(LOG_ERR, "chown socket: %m");
1344 if (chmod(sep->se_service, sep->se_sockmode) < 0)
1345 syslog(LOG_ERR, "chmod socket: %m");
1350 socklen_t len = sep->se_ctrladdr_size;
1351 struct netconfig *netid, *netid2 = NULL;
1352 struct sockaddr_in sock;
1353 struct netbuf nbuf, nbuf2;
1355 if (getsockname(sep->se_fd,
1356 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){
1357 syslog(LOG_ERR, "%s/%s: getsockname: %m",
1358 sep->se_service, sep->se_proto);
1359 (void) close(sep->se_fd);
1363 nbuf.buf = &sep->se_ctrladdr;
1364 nbuf.len = sep->se_ctrladdr.sa_len;
1365 if (sep->se_family == AF_INET)
1366 netid = sep->se_socktype==SOCK_DGRAM? udpconf:tcpconf;
1368 netid = sep->se_socktype==SOCK_DGRAM? udp6conf:tcp6conf;
1369 if (!sep->se_nomapped) { /* INET and INET6 */
1370 netid2 = netid==udp6conf? udpconf:tcpconf;
1371 memset(&sock, 0, sizeof sock); /* ADDR_ANY */
1373 nbuf2.len = sock.sin_len = sizeof sock;
1374 sock.sin_family = AF_INET;
1375 sock.sin_port = sep->se_ctrladdr6.sin6_port;
1379 print_service("REG ", sep);
1380 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1381 rpcb_unset(sep->se_rpc_prog, i, netid);
1382 rpcb_set(sep->se_rpc_prog, i, netid, &nbuf);
1384 rpcb_unset(sep->se_rpc_prog, i, netid2);
1385 rpcb_set(sep->se_rpc_prog, i, netid2, &nbuf2);
1389 if (sep->se_socktype == SOCK_STREAM)
1390 listen(sep->se_fd, -1);
1393 warnx("registered %s on %d",
1394 sep->se_server, sep->se_fd);
1400 ipsecsetup(struct servtab *sep)
1403 char *policy_in = NULL;
1404 char *policy_out = NULL;
1408 switch (sep->se_family) {
1411 opt = IP_IPSEC_POLICY;
1415 level = IPPROTO_IPV6;
1416 opt = IPV6_IPSEC_POLICY;
1423 if (!sep->se_policy || sep->se_policy[0] == '\0') {
1424 static char def_in[] = "in entrust", def_out[] = "out entrust";
1426 policy_out = def_out;
1428 if (!strncmp("in", sep->se_policy, 2))
1429 policy_in = sep->se_policy;
1430 else if (!strncmp("out", sep->se_policy, 3))
1431 policy_out = sep->se_policy;
1433 syslog(LOG_ERR, "invalid security policy \"%s\"",
1439 if (policy_in != NULL) {
1440 buf = ipsec_set_policy(policy_in, strlen(policy_in));
1442 if (setsockopt(sep->se_fd, level, opt,
1443 buf, ipsec_get_policylen(buf)) < 0 &&
1445 warnx("%s/%s: ipsec initialization failed; %s",
1446 sep->se_service, sep->se_proto,
1450 syslog(LOG_ERR, "invalid security policy \"%s\"",
1453 if (policy_out != NULL) {
1454 buf = ipsec_set_policy(policy_out, strlen(policy_out));
1456 if (setsockopt(sep->se_fd, level, opt,
1457 buf, ipsec_get_policylen(buf)) < 0 &&
1459 warnx("%s/%s: ipsec initialization failed; %s",
1460 sep->se_service, sep->se_proto,
1464 syslog(LOG_ERR, "invalid security policy \"%s\"",
1471 * Finish with a service and its socket.
1474 close_sep(struct servtab *sep)
1476 if (sep->se_fd >= 0) {
1477 if (FD_ISSET(sep->se_fd, &allsock))
1479 (void) close(sep->se_fd);
1483 sep->se_numchild = 0; /* forget about any existing children */
1487 matchservent(const char *name1, const char *name2, const char *proto)
1492 if (strcmp(proto, "unix") == 0) {
1493 if ((p = strrchr(name1, '/')) != NULL)
1495 if ((p = strrchr(name2, '/')) != NULL)
1498 if (strcmp(name1, name2) == 0)
1500 if ((se = getservbyname(name1, proto)) != NULL) {
1501 if (strcmp(name2, se->s_name) == 0)
1503 for (alias = se->s_aliases; *alias; alias++)
1504 if (strcmp(name2, *alias) == 0)
1511 enter(struct servtab *cp)
1513 struct servtab *sep;
1516 sep = (struct servtab *)malloc(sizeof (*sep));
1517 if (sep == (struct servtab *)0) {
1518 syslog(LOG_ERR, "malloc: %m");
1523 omask = sigblock(SIGBLOCK);
1524 sep->se_next = servtab;
1531 enable(struct servtab *sep)
1535 "enabling %s, fd %d", sep->se_service, sep->se_fd);
1537 if (sep->se_fd < 0) {
1539 "%s: %s: bad fd", __func__, sep->se_service);
1544 "%s: %s: is mux", __func__, sep->se_service);
1547 if (FD_ISSET(sep->se_fd, &allsock)) {
1549 "%s: %s: not off", __func__, sep->se_service);
1554 FD_SET(sep->se_fd, &allsock);
1555 if (sep->se_fd > maxsock)
1556 maxsock = sep->se_fd;
1560 disable(struct servtab *sep)
1564 "disabling %s, fd %d", sep->se_service, sep->se_fd);
1566 if (sep->se_fd < 0) {
1568 "%s: %s: bad fd", __func__, sep->se_service);
1573 "%s: %s: is mux", __func__, sep->se_service);
1576 if (!FD_ISSET(sep->se_fd, &allsock)) {
1578 "%s: %s: not on", __func__, sep->se_service);
1582 syslog(LOG_ERR, "%s: nsock=0", __func__);
1587 FD_CLR(sep->se_fd, &allsock);
1588 if (sep->se_fd == maxsock)
1592 FILE *fconfig = NULL;
1593 struct servtab serv;
1594 char line[LINE_MAX];
1600 if (fconfig != NULL) {
1601 fseek(fconfig, 0L, SEEK_SET);
1604 fconfig = fopen(CONFIG, "r");
1605 return (fconfig != NULL);
1612 (void) fclose(fconfig);
1620 struct servtab *sep = &serv;
1624 static char TCPMUX_TOKEN[] = "tcpmux/";
1625 #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
1643 while ((cp = nextline(fconfig)) != NULL) {
1645 /* lines starting with #@ is not a comment, but the policy */
1646 if (cp[0] == '#' && cp[1] == '@') {
1648 for (p = cp + 2; p && *p && isspace(*p); p++)
1654 } else if (ipsec_get_policylen(p) >= 0) {
1660 "%s: invalid ipsec policy \"%s\"",
1666 if (*cp == '#' || *cp == '\0')
1671 return ((struct servtab *)0);
1673 * clear the static buffer, since some fields (se_ctrladdr,
1674 * for example) don't get initialized here.
1676 memset(sep, 0, sizeof *sep);
1679 /* got an empty line containing just blanks/tabs. */
1682 if (arg[0] == ':') { /* :user:group:perm: */
1683 char *user, *group, *perm;
1687 if ((group = strchr(user, ':')) == NULL) {
1688 syslog(LOG_ERR, "no group after user '%s'", user);
1692 if ((perm = strchr(group, ':')) == NULL) {
1693 syslog(LOG_ERR, "no mode after group '%s'", group);
1697 if ((pw = getpwnam(user)) == NULL) {
1698 syslog(LOG_ERR, "no such user '%s'", user);
1701 sep->se_sockuid = pw->pw_uid;
1702 if ((gr = getgrnam(group)) == NULL) {
1703 syslog(LOG_ERR, "no such user '%s'", group);
1706 sep->se_sockgid = gr->gr_gid;
1707 sep->se_sockmode = strtol(perm, &arg, 8);
1709 syslog(LOG_ERR, "bad mode '%s'", perm);
1714 sep->se_sockuid = euid;
1715 sep->se_sockgid = egid;
1716 sep->se_sockmode = 0200;
1718 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
1719 char *c = arg + MUX_LEN;
1721 sep->se_type = MUXPLUS_TYPE;
1724 sep->se_type = MUX_TYPE;
1725 sep->se_service = newstr(c);
1727 sep->se_service = newstr(arg);
1728 sep->se_type = NORM_TYPE;
1731 if (strcmp(arg, "stream") == 0)
1732 sep->se_socktype = SOCK_STREAM;
1733 else if (strcmp(arg, "dgram") == 0)
1734 sep->se_socktype = SOCK_DGRAM;
1735 else if (strcmp(arg, "rdm") == 0)
1736 sep->se_socktype = SOCK_RDM;
1737 else if (strcmp(arg, "seqpacket") == 0)
1738 sep->se_socktype = SOCK_SEQPACKET;
1739 else if (strcmp(arg, "raw") == 0)
1740 sep->se_socktype = SOCK_RAW;
1742 sep->se_socktype = -1;
1745 if (strncmp(arg, "tcp", 3) == 0) {
1746 sep->se_proto = newstr(strsep(&arg, "/"));
1748 if (strcmp(arg, "faith") == 0)
1749 sep->se_type = FAITH_TYPE;
1752 if (sep->se_type == NORM_TYPE &&
1753 strncmp(arg, "faith/", 6) == 0) {
1755 sep->se_type = FAITH_TYPE;
1757 sep->se_proto = newstr(arg);
1759 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1760 memmove(sep->se_proto, sep->se_proto + 4,
1761 strlen(sep->se_proto) + 1 - 4);
1763 sep->se_rpc_prog = sep->se_rpc_lowvers =
1764 sep->se_rpc_lowvers = 0;
1765 memcpy(&sep->se_ctrladdr4, bind_sa4,
1766 sizeof(sep->se_ctrladdr4));
1767 if ((versp = rindex(sep->se_service, '/'))) {
1769 switch (sscanf(versp, "%u-%u",
1770 &sep->se_rpc_lowvers,
1771 &sep->se_rpc_highvers)) {
1775 sep->se_rpc_highvers =
1776 sep->se_rpc_lowvers;
1780 "bad RPC version specifier; %s",
1787 sep->se_rpc_lowvers =
1788 sep->se_rpc_highvers = 1;
1791 sep->se_nomapped = 0;
1792 if (strcmp(sep->se_proto, "unix") == 0) {
1793 sep->se_family = AF_UNIX;
1795 while (isdigit(sep->se_proto[strlen(sep->se_proto) - 1])) {
1797 if (sep->se_proto[strlen(sep->se_proto) - 1] == '6') {
1798 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1803 if (sep->se_proto[strlen(sep->se_proto) - 1] == '4') {
1804 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1808 /* illegal version num */
1809 syslog(LOG_ERR, "bad IP version for %s", sep->se_proto);
1814 if (v6bind && !v6bind_ok) {
1815 syslog(LOG_INFO, "IPv6 bind is ignored for %s",
1817 if (v4bind && v4bind_ok)
1825 sep->se_family = AF_INET6;
1826 if (!v4bind || !v4bind_ok)
1827 sep->se_nomapped = 1;
1830 { /* default to v4 bind if not v6 bind */
1832 syslog(LOG_NOTICE, "IPv4 bind is ignored for %s",
1837 sep->se_family = AF_INET;
1841 switch(sep->se_family) {
1843 memcpy(&sep->se_ctrladdr4, bind_sa4,
1844 sizeof(sep->se_ctrladdr4));
1845 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr4);
1849 memcpy(&sep->se_ctrladdr6, bind_sa6,
1850 sizeof(sep->se_ctrladdr6));
1851 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr6);
1855 if (strlen(sep->se_service) >= sizeof(sep->se_ctrladdr_un.sun_path)) {
1857 "domain socket pathname too long for service %s",
1861 memset(&sep->se_ctrladdr, 0, sizeof(sep->se_ctrladdr));
1862 sep->se_ctrladdr_un.sun_family = sep->se_family;
1863 sep->se_ctrladdr_un.sun_len = strlen(sep->se_service);
1864 strcpy(sep->se_ctrladdr_un.sun_path, sep->se_service);
1865 sep->se_ctrladdr_size = SUN_LEN(&sep->se_ctrladdr_un);
1868 if (!strncmp(arg, "wait", 4))
1870 else if (!strncmp(arg, "nowait", 6))
1874 "%s: bad wait/nowait for service %s",
1875 CONFIG, sep->se_service);
1878 sep->se_maxchild = -1;
1879 sep->se_maxcpm = -1;
1880 sep->se_maxperip = -1;
1881 if ((s = strchr(arg, '/')) != NULL) {
1885 val = strtoul(s + 1, &eptr, 10);
1886 if (eptr == s + 1 || val > MAX_MAXCHLD) {
1888 "%s: bad max-child for service %s",
1889 CONFIG, sep->se_service);
1893 if (!sep->se_accept && val != 1)
1894 warnx("maxchild=%lu for wait service %s"
1895 " not recommended", val, sep->se_service);
1896 sep->se_maxchild = val;
1898 sep->se_maxcpm = strtol(eptr + 1, &eptr, 10);
1900 sep->se_maxperip = strtol(eptr + 1, &eptr, 10);
1902 * explicitly do not check for \0 for future expansion /
1903 * backwards compatibility
1908 * Silently enforce "nowait" mode for TCPMUX services
1909 * since they don't have an assigned port to listen on.
1912 if (strcmp(sep->se_proto, "tcp")) {
1914 "%s: bad protocol for tcpmux service %s",
1915 CONFIG, sep->se_service);
1918 if (sep->se_socktype != SOCK_STREAM) {
1920 "%s: bad socket type for tcpmux service %s",
1921 CONFIG, sep->se_service);
1925 sep->se_user = newstr(sskip(&cp));
1927 if ((s = strrchr(sep->se_user, '/')) != NULL) {
1929 sep->se_class = newstr(s + 1);
1931 sep->se_class = newstr(RESOURCE_RC);
1933 if ((s = strrchr(sep->se_user, ':')) != NULL) {
1935 sep->se_group = newstr(s + 1);
1937 sep->se_group = NULL;
1938 sep->se_server = newstr(sskip(&cp));
1939 if ((sep->se_server_name = rindex(sep->se_server, '/')))
1940 sep->se_server_name++;
1941 if (strcmp(sep->se_server, "internal") == 0) {
1944 for (bi = biltins; bi->bi_service; bi++)
1945 if (bi->bi_socktype == sep->se_socktype &&
1946 matchservent(bi->bi_service, sep->se_service,
1949 if (bi->bi_service == 0) {
1950 syslog(LOG_ERR, "internal service %s unknown",
1954 sep->se_accept = 1; /* force accept mode for built-ins */
1958 if (sep->se_maxperip < 0)
1959 sep->se_maxperip = maxperip;
1960 if (sep->se_maxcpm < 0)
1961 sep->se_maxcpm = maxcpm;
1962 if (sep->se_maxchild < 0) { /* apply default max-children */
1963 if (sep->se_bi && sep->se_bi->bi_maxchild >= 0)
1964 sep->se_maxchild = sep->se_bi->bi_maxchild;
1965 else if (sep->se_accept)
1966 sep->se_maxchild = maxchild > 0 ? maxchild : 0;
1968 sep->se_maxchild = 1;
1970 if (sep->se_maxchild > 0) {
1971 sep->se_pids = malloc(sep->se_maxchild * sizeof(*sep->se_pids));
1972 if (sep->se_pids == NULL) {
1973 syslog(LOG_ERR, "malloc: %m");
1978 for (arg = skip(&cp); cp; arg = skip(&cp))
1979 if (argc < MAXARGV) {
1980 sep->se_argv[argc++] = newstr(arg);
1983 "%s: too many arguments for service %s",
1984 CONFIG, sep->se_service);
1987 while (argc <= MAXARGV)
1988 sep->se_argv[argc++] = NULL;
1989 for (i = 0; i < PERIPSIZE; ++i)
1990 LIST_INIT(&sep->se_conn[i]);
1992 sep->se_policy = policy ? newstr(policy) : NULL;
1998 freeconfig(struct servtab *cp)
2003 free(cp->se_service);
2015 free(cp->se_server);
2018 for (i = 0; i < MAXARGV; i++)
2020 free(cp->se_argv[i]);
2024 free(cp->se_policy);
2030 * Safe skip - if skip returns null, log a syntax error in the
2031 * configuration file and exit.
2040 syslog(LOG_ERR, "%s: syntax error", CONFIG);
2054 while (*cp == ' ' || *cp == '\t')
2060 (void) ungetc(c, fconfig);
2061 if (c == ' ' || c == '\t')
2062 if ((cp = nextline(fconfig)))
2067 if (*cp == '"' || *cp == '\'')
2071 while (*cp && *cp != quote)
2074 while (*cp && *cp != ' ' && *cp != '\t')
2087 if (fgets(line, sizeof (line), fd) == NULL)
2089 cp = strchr(line, '\n');
2096 newstr(const char *cp)
2100 if ((cr = strdup(cp != NULL ? cp : "")))
2102 syslog(LOG_ERR, "strdup: %m");
2107 inetd_setproctitle(const char *a, int s)
2110 struct sockaddr_storage ss;
2111 char buf[80], pbuf[INET6_ADDRSTRLEN];
2114 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) {
2115 getnameinfo((struct sockaddr *)&ss, size, pbuf, sizeof(pbuf),
2116 NULL, 0, NI_NUMERICHOST);
2117 (void) sprintf(buf, "%s [%s]", a, pbuf);
2119 (void) sprintf(buf, "%s", a);
2120 setproctitle("%s", buf);
2124 check_loop(const struct sockaddr *sa, const struct servtab *sep)
2126 struct servtab *se2;
2127 char pname[INET6_ADDRSTRLEN];
2129 for (se2 = servtab; se2; se2 = se2->se_next) {
2130 if (!se2->se_bi || se2->se_socktype != SOCK_DGRAM)
2133 switch (se2->se_family) {
2135 if (((const struct sockaddr_in *)sa)->sin_port ==
2136 se2->se_ctrladdr4.sin_port)
2141 if (((const struct sockaddr_in *)sa)->sin_port ==
2142 se2->se_ctrladdr4.sin_port)
2150 getnameinfo(sa, sa->sa_len, pname, sizeof(pname), NULL, 0,
2152 syslog(LOG_WARNING, "%s/%s:%s/%s loop request REFUSED from %s",
2153 sep->se_service, sep->se_proto,
2154 se2->se_service, se2->se_proto,
2163 * Dump relevant information to stderr
2166 print_service(const char *action, const struct servtab *sep)
2169 "%s: %s proto=%s accept=%d max=%d user=%s group=%s"
2173 " builtin=%p server=%s"
2178 action, sep->se_service, sep->se_proto,
2179 sep->se_accept, sep->se_maxchild, sep->se_user, sep->se_group,
2183 (void *) sep->se_bi, sep->se_server
2185 , (sep->se_policy ? sep->se_policy : "")
2190 #define CPMHSIZE 256
2191 #define CPMHMASK (CPMHSIZE-1)
2195 typedef struct CTime {
2196 unsigned long ct_Ticks;
2200 typedef struct CHash {
2202 struct in_addr c4_Addr;
2203 struct in6_addr c6_Addr;
2205 #define ch_Addr4 cu_Addr.c4_Addr
2206 #define ch_Addr6 cu_Addr.c6_Addr
2210 CTime ch_Times[CHTSIZE];
2213 CHash CHashAry[CPMHSIZE];
2216 cpmip(const struct servtab *sep, int ctrl)
2218 struct sockaddr_storage rss;
2219 socklen_t rssLen = sizeof(rss);
2223 * If getpeername() fails, just let it through (if logging is
2224 * enabled the condition is caught elsewhere)
2227 if (sep->se_maxcpm > 0 &&
2228 (sep->se_family == AF_INET || sep->se_family == AF_INET6) &&
2229 getpeername(ctrl, (struct sockaddr *)&rss, &rssLen) == 0 ) {
2230 time_t t = time(NULL);
2231 int hv = 0xABC3D20F;
2234 CHash *chBest = NULL;
2235 unsigned int ticks = t / CHTGRAN;
2236 struct sockaddr_in *sin4;
2238 struct sockaddr_in6 *sin6;
2241 sin4 = (struct sockaddr_in *)&rss;
2243 sin6 = (struct sockaddr_in6 *)&rss;
2249 switch (rss.ss_family) {
2251 p = (char *)&sin4->sin_addr;
2252 addrlen = sizeof(struct in_addr);
2256 p = (char *)&sin6->sin6_addr;
2257 addrlen = sizeof(struct in6_addr);
2261 /* should not happen */
2265 for (i = 0; i < addrlen; ++i, ++p) {
2266 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2268 hv = (hv ^ (hv >> 16));
2270 for (i = 0; i < 5; ++i) {
2271 CHash *ch = &CHashAry[(hv + i) & CPMHMASK];
2273 if (rss.ss_family == AF_INET &&
2274 ch->ch_Family == AF_INET &&
2275 sin4->sin_addr.s_addr == ch->ch_Addr4.s_addr &&
2276 ch->ch_Service && strcmp(sep->se_service,
2277 ch->ch_Service) == 0) {
2282 if (rss.ss_family == AF_INET6 &&
2283 ch->ch_Family == AF_INET6 &&
2284 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2285 &ch->ch_Addr6) != 0 &&
2286 ch->ch_Service && strcmp(sep->se_service,
2287 ch->ch_Service) == 0) {
2292 if (chBest == NULL || ch->ch_LTime == 0 ||
2293 ch->ch_LTime < chBest->ch_LTime) {
2297 if ((rss.ss_family == AF_INET &&
2298 (chBest->ch_Family != AF_INET ||
2299 sin4->sin_addr.s_addr != chBest->ch_Addr4.s_addr)) ||
2300 chBest->ch_Service == NULL ||
2301 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2302 chBest->ch_Family = sin4->sin_family;
2303 chBest->ch_Addr4 = sin4->sin_addr;
2304 if (chBest->ch_Service)
2305 free(chBest->ch_Service);
2306 chBest->ch_Service = strdup(sep->se_service);
2307 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2310 if ((rss.ss_family == AF_INET6 &&
2311 (chBest->ch_Family != AF_INET6 ||
2312 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2313 &chBest->ch_Addr6) == 0)) ||
2314 chBest->ch_Service == NULL ||
2315 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2316 chBest->ch_Family = sin6->sin6_family;
2317 chBest->ch_Addr6 = sin6->sin6_addr;
2318 if (chBest->ch_Service)
2319 free(chBest->ch_Service);
2320 chBest->ch_Service = strdup(sep->se_service);
2321 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2324 chBest->ch_LTime = t;
2326 CTime *ct = &chBest->ch_Times[ticks % CHTSIZE];
2327 if (ct->ct_Ticks != ticks) {
2328 ct->ct_Ticks = ticks;
2333 for (i = 0; i < CHTSIZE; ++i) {
2334 CTime *ct = &chBest->ch_Times[i];
2335 if (ct->ct_Ticks <= ticks &&
2336 ct->ct_Ticks >= ticks - CHTSIZE) {
2337 cnt += ct->ct_Count;
2340 if ((cnt * 60) / (CHTSIZE * CHTGRAN) > sep->se_maxcpm) {
2341 char pname[INET6_ADDRSTRLEN];
2343 getnameinfo((struct sockaddr *)&rss,
2344 ((struct sockaddr *)&rss)->sa_len,
2345 pname, sizeof(pname), NULL, 0,
2349 "%s from %s exceeded counts/min (limit %d/min)",
2350 sep->se_service, pname,
2357 static struct conninfo *
2358 search_conn(struct servtab *sep, int ctrl)
2360 struct sockaddr_storage ss;
2361 socklen_t sslen = sizeof(ss);
2362 struct conninfo *conn;
2364 char pname[NI_MAXHOST], pname2[NI_MAXHOST];
2366 if (sep->se_maxperip <= 0)
2370 * If getpeername() fails, just let it through (if logging is
2371 * enabled the condition is caught elsewhere)
2373 if (getpeername(ctrl, (struct sockaddr *)&ss, &sslen) != 0)
2376 switch (ss.ss_family) {
2378 hv = hashval((char *)&((struct sockaddr_in *)&ss)->sin_addr,
2379 sizeof(struct in_addr));
2383 hv = hashval((char *)&((struct sockaddr_in6 *)&ss)->sin6_addr,
2384 sizeof(struct in6_addr));
2389 * Since we only support AF_INET and AF_INET6, just
2390 * let other than AF_INET and AF_INET6 through.
2395 if (getnameinfo((struct sockaddr *)&ss, sslen, pname, sizeof(pname),
2396 NULL, 0, NI_NUMERICHOST) != 0)
2399 LIST_FOREACH(conn, &sep->se_conn[hv], co_link) {
2400 if (getnameinfo((struct sockaddr *)&conn->co_addr,
2401 conn->co_addr.ss_len, pname2, sizeof(pname2), NULL, 0,
2402 NI_NUMERICHOST) == 0 &&
2403 strcmp(pname, pname2) == 0)
2408 if ((conn = malloc(sizeof(struct conninfo))) == NULL) {
2409 syslog(LOG_ERR, "malloc: %m");
2412 conn->co_proc = malloc(sep->se_maxperip * sizeof(*conn->co_proc));
2413 if (conn->co_proc == NULL) {
2414 syslog(LOG_ERR, "malloc: %m");
2417 memcpy(&conn->co_addr, (struct sockaddr *)&ss, sslen);
2418 conn->co_numchild = 0;
2419 LIST_INSERT_HEAD(&sep->se_conn[hv], conn, co_link);
2423 * Since a child process is not invoked yet, we cannot
2424 * determine a pid of a child. So, co_proc and co_numchild
2425 * should be filled leter.
2432 room_conn(struct servtab *sep, struct conninfo *conn)
2434 char pname[NI_MAXHOST];
2436 if (conn->co_numchild >= sep->se_maxperip) {
2437 getnameinfo((struct sockaddr *)&conn->co_addr,
2438 conn->co_addr.ss_len, pname, sizeof(pname), NULL, 0,
2440 syslog(LOG_ERR, "%s from %s exceeded counts (limit %d)",
2441 sep->se_service, pname, sep->se_maxperip);
2448 addchild_conn(struct conninfo *conn, pid_t pid)
2450 struct procinfo *proc;
2455 if ((proc = search_proc(pid, 1)) != NULL) {
2456 if (proc->pr_conn != NULL) {
2458 "addchild_conn: child already on process list");
2461 proc->pr_conn = conn;
2464 conn->co_proc[conn->co_numchild++] = proc;
2468 reapchild_conn(pid_t pid)
2470 struct procinfo *proc;
2471 struct conninfo *conn;
2474 if ((proc = search_proc(pid, 0)) == NULL)
2476 if ((conn = proc->pr_conn) == NULL)
2478 for (i = 0; i < conn->co_numchild; ++i)
2479 if (conn->co_proc[i] == proc) {
2480 conn->co_proc[i] = conn->co_proc[--conn->co_numchild];
2488 resize_conn(struct servtab *sep, int maxpip)
2490 struct conninfo *conn;
2493 if (sep->se_maxperip <= 0)
2499 for (i = 0; i < PERIPSIZE; ++i) {
2500 LIST_FOREACH(conn, &sep->se_conn[i], co_link) {
2501 for (j = maxpip; j < conn->co_numchild; ++j)
2502 free_proc(conn->co_proc[j]);
2503 conn->co_proc = realloc(conn->co_proc,
2504 maxpip * sizeof(*conn->co_proc));
2505 if (conn->co_proc == NULL) {
2506 syslog(LOG_ERR, "realloc: %m");
2509 if (conn->co_numchild > maxpip)
2510 conn->co_numchild = maxpip;
2516 free_connlist(struct servtab *sep)
2518 struct conninfo *conn;
2521 for (i = 0; i < PERIPSIZE; ++i) {
2522 while ((conn = LIST_FIRST(&sep->se_conn[i])) != NULL) {
2523 for (j = 0; j < conn->co_numchild; ++j)
2524 free_proc(conn->co_proc[j]);
2525 conn->co_numchild = 0;
2532 free_conn(struct conninfo *conn)
2536 if (conn->co_numchild <= 0) {
2537 LIST_REMOVE(conn, co_link);
2538 free(conn->co_proc);
2543 static struct procinfo *
2544 search_proc(pid_t pid, int add)
2546 struct procinfo *proc;
2549 hv = hashval((char *)&pid, sizeof(pid));
2550 LIST_FOREACH(proc, &proctable[hv], pr_link) {
2551 if (proc->pr_pid == pid)
2554 if (proc == NULL && add) {
2555 if ((proc = malloc(sizeof(struct procinfo))) == NULL) {
2556 syslog(LOG_ERR, "malloc: %m");
2560 proc->pr_conn = NULL;
2561 LIST_INSERT_HEAD(&proctable[hv], proc, pr_link);
2567 free_proc(struct procinfo *proc)
2571 LIST_REMOVE(proc, pr_link);
2576 hashval(char *p, int len)
2578 int i, hv = 0xABC3D20F;
2580 for (i = 0; i < len; ++i, ++p)
2581 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2582 hv = (hv ^ (hv >> 16)) & (PERIPSIZE - 1);