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], 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>
147 #include "pathnames.h"
150 #include <netipsec/ipsec.h>
151 #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
156 #ifndef LIBWRAP_ALLOW_FACILITY
157 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
159 #ifndef LIBWRAP_ALLOW_SEVERITY
160 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
162 #ifndef LIBWRAP_DENY_FACILITY
163 # define LIBWRAP_DENY_FACILITY LOG_AUTH
165 #ifndef LIBWRAP_DENY_SEVERITY
166 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
169 #define ISWRAP(sep) \
170 ( ((wrap_ex && !(sep)->se_bi) || (wrap_bi && (sep)->se_bi)) \
171 && (sep->se_family == AF_INET || sep->se_family == AF_INET6) \
172 && ( ((sep)->se_accept && (sep)->se_socktype == SOCK_STREAM) \
173 || (sep)->se_socktype == SOCK_DGRAM))
176 #include <login_cap.h>
179 #define RESOURCE_RC "daemon"
184 #define MAXCHILD -1 /* maximum number of this service
189 #define MAXCPM -1 /* rate limit invocations from a
190 single remote address,
195 #define MAXPERIP -1 /* maximum number of this service
196 from a single remote address,
201 #define TOOMANY 256 /* don't start more than TOOMANY */
203 #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
204 #define RETRYTIME (60*10) /* retry after bind or server fail */
205 #define MAX_MAXCHLD 32767 /* max allowable max children */
207 #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
209 #define satosin(sa) ((struct sockaddr_in *)(void *)sa)
210 #define csatosin(sa) ((const struct sockaddr_in *)(const void *)sa)
212 #define satosin6(sa) ((struct sockaddr_in6 *)(void *)sa)
213 #define csatosin6(sa) ((const struct sockaddr_in6 *)(const void *)sa)
215 static void close_sep(struct servtab *);
216 static void flag_signal(int);
217 static void flag_config(int);
218 static void config(void);
219 static int cpmip(const struct servtab *, int);
220 static void endconfig(void);
221 static struct servtab *enter(struct servtab *);
222 static void freeconfig(struct servtab *);
223 static struct servtab *getconfigent(void);
224 static int matchservent(const char *, const char *, const char *);
225 static char *nextline(FILE *);
226 static void addchild(struct servtab *, int);
227 static void flag_reapchild(int);
228 static void reapchild(void);
229 static void enable(struct servtab *);
230 static void disable(struct servtab *);
231 static void flag_retry(int);
232 static void retry(void);
233 static int setconfig(void);
234 static void setup(struct servtab *);
236 static void ipsecsetup(struct servtab *);
238 static void unregisterrpc(register struct servtab *sep);
239 static struct conninfo *search_conn(struct servtab *sep, int ctrl);
240 static int room_conn(struct servtab *sep, struct conninfo *conn);
241 static void addchild_conn(struct conninfo *conn, pid_t pid);
242 static void reapchild_conn(pid_t pid);
243 static void free_conn(struct conninfo *conn);
244 static void resize_conn(struct servtab *sep, int maxperip);
245 static void free_connlist(struct servtab *sep);
246 static void free_proc(struct procinfo *);
247 static struct procinfo *search_proc(pid_t pid, int add);
248 static int hashval(char *p, int len);
249 static char *skip(char **);
250 static char *sskip(char **);
251 static char *newstr(const char *);
252 static void print_service(const char *, const struct servtab *);
258 static int wrap_ex = 0;
259 static int wrap_bi = 0;
261 static int dolog = 0;
262 static int maxsock; /* highest-numbered descriptor */
263 static fd_set allsock;
265 static int timingout;
266 static int toomany = TOOMANY;
267 static int maxchild = MAXCHILD;
268 static int maxcpm = MAXCPM;
269 static int maxperip = MAXPERIP;
270 static struct servent *sp;
271 static struct rpcent *rpc;
272 static char *hostname = NULL;
273 static struct sockaddr_in *bind_sa4;
274 static int v4bind_ok = 0;
276 static struct sockaddr_in6 *bind_sa6;
277 static int v6bind_ok = 0;
279 static int signalpipe[2];
287 struct servtab *servtab;
289 static const char *CONFIG = _PATH_INETDCONF;
290 static const char *pid_file = _PATH_INETDPID;
291 static struct pidfh *pfh = NULL;
293 static struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
295 static LIST_HEAD(, procinfo) proctable[PERIPSIZE];
298 getvalue(const char *arg, int *value, const char *whine)
303 tmp = strtol(arg, &p, 0);
305 syslog(LOG_ERR, whine, arg);
306 return 1; /* failure */
309 return 0; /* success */
314 whichaf(struct request_info *req)
318 sa = (struct sockaddr *)req->client->sin;
322 if (sa->sa_family == AF_INET6 &&
323 IN6_IS_ADDR_V4MAPPED(&satosin6(sa)->sin6_addr))
326 return sa->sa_family;
331 main(int argc, char **argv)
336 struct sigaction sa, saalrm, sachld, sahup, sapipe;
341 login_cap_t *lc = NULL;
344 struct request_info req;
346 char *service = NULL;
348 struct sockaddr_storage peer;
350 struct addrinfo hints, *res;
351 const char *servname;
353 struct conninfo *conn;
355 openlog("inetd", LOG_PID | LOG_NOWAIT | LOG_PERROR, LOG_DAEMON);
357 while ((ch = getopt(argc, argv, "dlwWR:a:c:C:p:s:")) != -1)
367 getvalue(optarg, &toomany,
368 "-R %s: bad value for service invocation rate");
371 getvalue(optarg, &maxchild,
372 "-c %s: bad value for maximum children");
375 getvalue(optarg, &maxcpm,
376 "-C %s: bad value for maximum children/minute");
385 getvalue(optarg, &maxperip,
386 "-s %s: bad value for maximum children per source address");
397 "usage: inetd [-dlwW] [-a address] [-R rate]"
398 " [-c maximum] [-C rate]"
399 " [-p pidfile] [conf-file]");
403 * Initialize Bind Addrs.
404 * When hostname is NULL, wild card bind addrs are obtained from
405 * getaddrinfo(). But getaddrinfo() requires at least one of
406 * hostname or servname is non NULL.
407 * So when hostname is NULL, set dummy value to servname.
408 * Since getaddrinfo() doesn't accept numeric servname, and
409 * we doesn't use ai_socktype of struct addrinfo returned
410 * from getaddrinfo(), we set dummy value to ai_socktype.
412 servname = (hostname == NULL) ? "0" /* dummy */ : NULL;
414 bzero(&hints, sizeof(struct addrinfo));
415 hints.ai_flags = AI_PASSIVE;
416 hints.ai_family = AF_UNSPEC;
417 hints.ai_socktype = SOCK_STREAM; /* dummy */
418 error = getaddrinfo(hostname, servname, &hints, &res);
420 syslog(LOG_ERR, "-a %s: %s", hostname, gai_strerror(error));
421 if (error == EAI_SYSTEM)
422 syslog(LOG_ERR, "%s", strerror(errno));
426 if (res->ai_addr == NULL) {
427 syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
430 switch (res->ai_addr->sa_family) {
434 bind_sa4 = satosin(res->ai_addr);
435 /* init port num in case servname is dummy */
436 bind_sa4->sin_port = 0;
443 bind_sa6 = satosin6(res->ai_addr);
444 /* init port num in case servname is dummy */
445 bind_sa6->sin6_port = 0;
456 } while ((res = res->ai_next) != NULL);
462 syslog(LOG_ERR, "-a %s: unknown address family", hostname);
468 umask(mask = umask(0777));
475 if (access(CONFIG, R_OK) < 0)
476 syslog(LOG_ERR, "Accessing %s: %m, continuing anyway.", CONFIG);
480 pfh = pidfile_open(pid_file, 0600, &otherpid);
482 if (errno == EEXIST) {
483 syslog(LOG_ERR, "%s already running, pid: %d",
484 getprogname(), otherpid);
487 syslog(LOG_WARNING, "pidfile_open() failed: %m");
490 if (daemon(0, 0) < 0) {
491 syslog(LOG_WARNING, "daemon(0,0) failed: %m");
493 /* From now on we don't want syslog messages going to stderr. */
495 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
497 * In case somebody has started inetd manually, we need to
498 * clear the logname, so that old servers run as root do not
499 * get the user's logname..
501 if (setlogin("") < 0) {
502 syslog(LOG_WARNING, "cannot clear logname: %m");
503 /* no big deal if it fails.. */
505 if (pfh != NULL && pidfile_write(pfh) == -1) {
506 syslog(LOG_WARNING, "pidfile_write(): %m");
510 if (madvise(NULL, 0, MADV_PROTECT) != 0)
511 syslog(LOG_WARNING, "madvise() failed: %s", strerror(errno));
513 for (i = 0; i < PERIPSIZE; ++i)
514 LIST_INIT(&proctable[i]);
517 udpconf = getnetconfigent("udp");
518 tcpconf = getnetconfigent("tcp");
519 if (udpconf == NULL || tcpconf == NULL) {
520 syslog(LOG_ERR, "unknown rpc/udp or rpc/tcp");
526 udp6conf = getnetconfigent("udp6");
527 tcp6conf = getnetconfigent("tcp6");
528 if (udp6conf == NULL || tcp6conf == NULL) {
529 syslog(LOG_ERR, "unknown rpc/udp6 or rpc/tcp6");
536 sigemptyset(&sa.sa_mask);
537 sigaddset(&sa.sa_mask, SIGALRM);
538 sigaddset(&sa.sa_mask, SIGCHLD);
539 sigaddset(&sa.sa_mask, SIGHUP);
540 sa.sa_handler = flag_retry;
541 sigaction(SIGALRM, &sa, &saalrm);
543 sa.sa_handler = flag_config;
544 sigaction(SIGHUP, &sa, &sahup);
545 sa.sa_handler = flag_reapchild;
546 sigaction(SIGCHLD, &sa, &sachld);
547 sa.sa_handler = SIG_IGN;
548 sigaction(SIGPIPE, &sa, &sapipe);
551 /* space for daemons to overwrite environment for ps */
552 #define DUMMYSIZE 100
553 char dummy[DUMMYSIZE];
555 (void)memset(dummy, 'x', DUMMYSIZE - 1);
556 dummy[DUMMYSIZE - 1] = '\0';
557 (void)setenv("inetd_dummy", dummy, 1);
560 if (pipe2(signalpipe, O_CLOEXEC) != 0) {
561 syslog(LOG_ERR, "pipe: %m");
564 FD_SET(signalpipe[0], &allsock);
568 if (signalpipe[0] > maxsock)
569 maxsock = signalpipe[0];
570 if (signalpipe[1] > maxsock)
571 maxsock = signalpipe[1];
579 syslog(LOG_ERR, "%s: nsock=0", __func__);
584 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
585 (fd_set *)0, (struct timeval *)0)) <= 0) {
586 if (n < 0 && errno != EINTR) {
587 syslog(LOG_WARNING, "select: %m");
592 /* handle any queued signal flags */
593 if (FD_ISSET(signalpipe[0], &readable)) {
595 if (ioctl(signalpipe[0], FIONREAD, &nsig) != 0) {
596 syslog(LOG_ERR, "ioctl: %m");
599 while (--nsig >= 0) {
601 if (read(signalpipe[0], &c, 1) != 1) {
602 syslog(LOG_ERR, "read: %m");
606 warnx("handling signal flag %c", c);
608 case 'A': /* sigalrm */
611 case 'C': /* sigchld */
614 case 'H': /* sighup */
620 for (sep = servtab; n && sep; sep = sep->se_next)
621 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
624 warnx("someone wants %s", sep->se_service);
625 dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep);
627 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) {
629 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
630 syslog(LOG_ERR, "ioctl (FIONBIO, 1): %m");
631 ctrl = accept(sep->se_fd, (struct sockaddr *)0,
634 warnx("accept, ctrl %d", ctrl);
638 "accept (for %s): %m",
640 if (sep->se_accept &&
641 sep->se_socktype == SOCK_STREAM)
646 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
647 syslog(LOG_ERR, "ioctl1(FIONBIO, 0): %m");
648 if (ioctl(ctrl, FIONBIO, &i) < 0)
649 syslog(LOG_ERR, "ioctl2(FIONBIO, 0): %m");
650 if (cpmip(sep, ctrl) < 0) {
655 (conn = search_conn(sep, ctrl)) != NULL &&
656 !room_conn(sep, conn)) {
662 if (dolog && !ISWRAP(sep)) {
663 char pname[NI_MAXHOST] = "unknown";
666 if (getpeername(ctrl, (struct sockaddr *)
669 if (recvfrom(ctrl, buf, sizeof(buf),
671 (struct sockaddr *)&peer,
673 getnameinfo((struct sockaddr *)&peer,
675 pname, sizeof(pname),
676 NULL, 0, NI_NUMERICHOST);
679 getnameinfo((struct sockaddr *)&peer,
681 pname, sizeof(pname),
682 NULL, 0, NI_NUMERICHOST);
684 syslog(LOG_INFO,"%s from %s", sep->se_service, pname);
686 (void) sigblock(SIGBLOCK);
689 * Fork for all external services, builtins which need to
690 * fork and anything we're wrapping (as wrapping might
691 * block or use hosts_options(5) twist).
694 if (sep->se_count++ == 0)
695 (void)clock_gettime(CLOCK_MONOTONIC_FAST, &sep->se_time);
696 else if (toomany > 0 && sep->se_count >= toomany) {
699 (void)clock_gettime(CLOCK_MONOTONIC_FAST, &now);
700 if (now.tv_sec - sep->se_time.tv_sec >
706 "%s/%s server failing (looping), service terminated",
707 sep->se_service, sep->se_proto);
708 if (sep->se_accept &&
709 sep->se_socktype == SOCK_STREAM)
724 syslog(LOG_ERR, "fork: %m");
725 if (sep->se_accept &&
726 sep->se_socktype == SOCK_STREAM)
734 addchild_conn(conn, pid);
741 sigaction(SIGALRM, &saalrm, (struct sigaction *)0);
742 sigaction(SIGCHLD, &sachld, (struct sigaction *)0);
743 sigaction(SIGHUP, &sahup, (struct sigaction *)0);
744 /* SIGPIPE reset before exec */
747 * Call tcpmux to find the real service to exec.
750 sep->se_bi->bi_fn == (bi_fn_t *) tcpmux) {
759 inetd_setproctitle("wrapping", ctrl);
760 service = sep->se_server_name ?
761 sep->se_server_name : sep->se_service;
762 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, 0);
764 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
765 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
766 denied = !hosts_access(&req);
768 syslog(deny_severity,
769 "refused connection from %.500s, service %s (%s%s)",
770 eval_client(&req), service, sep->se_proto,
771 (whichaf(&req) == AF_INET6) ? "6" : "");
772 if (sep->se_socktype != SOCK_STREAM)
773 recv(ctrl, buf, sizeof (buf), 0);
780 syslog(allow_severity,
781 "connection from %.500s, service %s (%s%s)",
782 eval_client(&req), service, sep->se_proto,
783 (whichaf(&req) == AF_INET6) ? "6" : "");
788 (*sep->se_bi->bi_fn)(ctrl, sep);
792 getpid(), sep->se_server);
793 /* Clear close-on-exec. */
794 if (fcntl(ctrl, F_SETFD, 0) < 0) {
796 "%s/%s: fcntl (F_SETFD, 0): %m",
797 sep->se_service, sep->se_proto);
806 if ((pwd = getpwnam(sep->se_user)) == NULL) {
808 "%s/%s: %s: no such user",
809 sep->se_service, sep->se_proto,
811 if (sep->se_socktype != SOCK_STREAM)
812 recv(0, buf, sizeof (buf), 0);
816 if ( sep->se_group != NULL
817 && (grp = getgrnam(sep->se_group)) == NULL
820 "%s/%s: %s: no such group",
821 sep->se_service, sep->se_proto,
823 if (sep->se_socktype != SOCK_STREAM)
824 recv(0, buf, sizeof (buf), 0);
828 pwd->pw_gid = grp->gr_gid;
830 if ((lc = login_getclass(sep->se_class)) == NULL) {
831 /* error syslogged by getclass */
833 "%s/%s: %s: login class error",
834 sep->se_service, sep->se_proto,
836 if (sep->se_socktype != SOCK_STREAM)
837 recv(0, buf, sizeof (buf), 0);
843 "%s: can't setsid(): %m",
845 /* _exit(EX_OSERR); not fatal yet */
848 if (setusercontext(lc, pwd, pwd->pw_uid,
849 LOGIN_SETALL & ~LOGIN_SETMAC)
852 "%s: can't setusercontext(..%s..): %m",
853 sep->se_service, sep->se_user);
859 if (setlogin(sep->se_user) < 0) {
861 "%s: can't setlogin(%s): %m",
862 sep->se_service, sep->se_user);
863 /* _exit(EX_OSERR); not yet */
865 if (setgid(pwd->pw_gid) < 0) {
867 "%s: can't set gid %d: %m",
868 sep->se_service, pwd->pw_gid);
871 (void) initgroups(pwd->pw_name,
873 if (setuid(pwd->pw_uid) < 0) {
875 "%s: can't set uid %d: %m",
876 sep->se_service, pwd->pw_uid);
881 sigaction(SIGPIPE, &sapipe,
882 (struct sigaction *)0);
883 execv(sep->se_server, sep->se_argv);
885 "cannot execute %s: %m", sep->se_server);
886 if (sep->se_socktype != SOCK_STREAM)
887 recv(0, buf, sizeof (buf), 0);
892 if (sep->se_accept && sep->se_socktype == SOCK_STREAM)
899 * Add a signal flag to the signal flag queue for later handling
907 if (write(signalpipe[1], &ch, 1) != 1) {
908 syslog(LOG_ERR, "write: %m");
914 * Record a new child pid for this service. If we've reached the
915 * limit on children, then stop accepting incoming requests.
919 addchild(struct servtab *sep, pid_t pid)
921 if (sep->se_maxchild <= 0)
924 if (sep->se_numchild >= sep->se_maxchild) {
925 syslog(LOG_ERR, "%s: %d >= %d",
926 __func__, sep->se_numchild, sep->se_maxchild);
930 sep->se_pids[sep->se_numchild++] = pid;
931 if (sep->se_numchild == sep->se_maxchild)
936 * Some child process has exited. See if it's on somebody's list.
940 flag_reapchild(int signo __unused)
953 pid = wait3(&status, WNOHANG, (struct rusage *)0);
957 warnx("%d reaped, %s %u", pid,
958 WIFEXITED(status) ? "status" : "signal",
959 WIFEXITED(status) ? WEXITSTATUS(status)
961 for (sep = servtab; sep; sep = sep->se_next) {
962 for (k = 0; k < sep->se_numchild; k++)
963 if (sep->se_pids[k] == pid)
965 if (k == sep->se_numchild)
967 if (sep->se_numchild == sep->se_maxchild)
969 sep->se_pids[k] = sep->se_pids[--sep->se_numchild];
970 if (WIFSIGNALED(status) || WEXITSTATUS(status))
972 "%s[%d]: exited, %s %u",
974 WIFEXITED(status) ? "status" : "signal",
975 WIFEXITED(status) ? WEXITSTATUS(status)
984 flag_config(int signo __unused)
992 struct servtab *sep, *new, **sepp;
996 login_cap_t *lc = NULL;
1000 syslog(LOG_ERR, "%s: %m", CONFIG);
1003 for (sep = servtab; sep; sep = sep->se_next)
1004 sep->se_checked = 0;
1005 while ((new = getconfigent())) {
1006 if (getpwnam(new->se_user) == NULL) {
1008 "%s/%s: no such user '%s', service ignored",
1009 new->se_service, new->se_proto, new->se_user);
1012 if (new->se_group && getgrnam(new->se_group) == NULL) {
1014 "%s/%s: no such group '%s', service ignored",
1015 new->se_service, new->se_proto, new->se_group);
1019 if ((lc = login_getclass(new->se_class)) == NULL) {
1020 /* error syslogged by getclass */
1022 "%s/%s: %s: login class error, service ignored",
1023 new->se_service, new->se_proto, new->se_class);
1028 new_nomapped = new->se_nomapped;
1029 for (sep = servtab; sep; sep = sep->se_next)
1030 if (strcmp(sep->se_service, new->se_service) == 0 &&
1031 strcmp(sep->se_proto, new->se_proto) == 0 &&
1032 sep->se_rpc == new->se_rpc &&
1033 sep->se_socktype == new->se_socktype &&
1034 sep->se_family == new->se_family)
1039 #define SWAP(t,a, b) { t c = a; a = b; b = c; }
1040 omask = sigblock(SIGBLOCK);
1041 if (sep->se_nomapped != new->se_nomapped) {
1042 /* for rpc keep old nommaped till unregister */
1044 sep->se_nomapped = new->se_nomapped;
1047 /* copy over outstanding child pids */
1048 if (sep->se_maxchild > 0 && new->se_maxchild > 0) {
1049 new->se_numchild = sep->se_numchild;
1050 if (new->se_numchild > new->se_maxchild)
1051 new->se_numchild = new->se_maxchild;
1052 memcpy(new->se_pids, sep->se_pids,
1053 new->se_numchild * sizeof(*new->se_pids));
1055 SWAP(pid_t *, sep->se_pids, new->se_pids);
1056 sep->se_maxchild = new->se_maxchild;
1057 sep->se_numchild = new->se_numchild;
1058 sep->se_maxcpm = new->se_maxcpm;
1059 resize_conn(sep, new->se_maxperip);
1060 sep->se_maxperip = new->se_maxperip;
1061 sep->se_bi = new->se_bi;
1062 /* might need to turn on or off service now */
1063 if (sep->se_fd >= 0) {
1064 if (sep->se_maxchild > 0
1065 && sep->se_numchild == sep->se_maxchild) {
1066 if (FD_ISSET(sep->se_fd, &allsock))
1069 if (!FD_ISSET(sep->se_fd, &allsock))
1073 sep->se_accept = new->se_accept;
1074 SWAP(char *, sep->se_user, new->se_user);
1075 SWAP(char *, sep->se_group, new->se_group);
1077 SWAP(char *, sep->se_class, new->se_class);
1079 SWAP(char *, sep->se_server, new->se_server);
1080 SWAP(char *, sep->se_server_name, new->se_server_name);
1081 for (i = 0; i < MAXARGV; i++)
1082 SWAP(char *, sep->se_argv[i], new->se_argv[i]);
1084 SWAP(char *, sep->se_policy, new->se_policy);
1090 print_service("REDO", sep);
1094 print_service("ADD ", sep);
1096 sep->se_checked = 1;
1101 switch (sep->se_family) {
1118 if (sep->se_family != AF_UNIX) {
1119 sp = getservbyname(sep->se_service, sep->se_proto);
1121 syslog(LOG_ERR, "%s/%s: unknown service",
1122 sep->se_service, sep->se_proto);
1123 sep->se_checked = 0;
1127 switch (sep->se_family) {
1129 if (sp->s_port != sep->se_ctrladdr4.sin_port) {
1130 sep->se_ctrladdr4.sin_port =
1138 sep->se_ctrladdr6.sin6_port) {
1139 sep->se_ctrladdr6.sin6_port =
1146 if (sep->se_reset != 0 && sep->se_fd >= 0)
1149 rpc = getrpcbyname(sep->se_service);
1151 syslog(LOG_ERR, "%s/%s unknown RPC service",
1152 sep->se_service, sep->se_proto);
1153 if (sep->se_fd != -1)
1154 (void) close(sep->se_fd);
1158 if (sep->se_reset != 0 ||
1159 rpc->r_number != sep->se_rpc_prog) {
1160 if (sep->se_rpc_prog)
1162 sep->se_rpc_prog = rpc->r_number;
1163 if (sep->se_fd != -1)
1164 (void) close(sep->se_fd);
1167 sep->se_nomapped = new_nomapped;
1170 if (sep->se_fd == -1)
1175 * Purge anything not looked at above.
1177 omask = sigblock(SIGBLOCK);
1179 while ((sep = *sepp)) {
1180 if (sep->se_checked) {
1181 sepp = &sep->se_next;
1184 *sepp = sep->se_next;
1185 if (sep->se_fd >= 0)
1188 print_service("FREE", sep);
1189 if (sep->se_rpc && sep->se_rpc_prog > 0)
1194 (void) sigsetmask(omask);
1198 unregisterrpc(struct servtab *sep)
1201 struct servtab *sepp;
1203 struct netconfig *netid4, *netid6;
1205 omask = sigblock(SIGBLOCK);
1206 netid4 = sep->se_socktype == SOCK_DGRAM ? udpconf : tcpconf;
1207 netid6 = sep->se_socktype == SOCK_DGRAM ? udp6conf : tcp6conf;
1208 if (sep->se_family == AF_INET)
1210 else if (sep->se_nomapped)
1213 * Conflict if same prog and protocol - In that case one should look
1214 * to versions, but it is not interesting: having separate servers for
1215 * different versions does not work well.
1216 * Therefore one do not unregister if there is a conflict.
1217 * There is also transport conflict if destroying INET when INET46
1218 * exists, or destroying INET46 when INET exists
1220 for (sepp = servtab; sepp; sepp = sepp->se_next) {
1223 if (sepp->se_checked == 0 ||
1225 strcmp(sep->se_proto, sepp->se_proto) != 0 ||
1226 sep->se_rpc_prog != sepp->se_rpc_prog)
1228 if (sepp->se_family == AF_INET)
1230 if (sepp->se_family == AF_INET6) {
1232 if (!sep->se_nomapped)
1235 if (netid4 == NULL && netid6 == NULL)
1239 print_service("UNREG", sep);
1240 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1242 rpcb_unset(sep->se_rpc_prog, i, netid4);
1244 rpcb_unset(sep->se_rpc_prog, i, netid6);
1246 if (sep->se_fd != -1)
1247 (void) close(sep->se_fd);
1249 (void) sigsetmask(omask);
1253 flag_retry(int signo __unused)
1261 struct servtab *sep;
1264 for (sep = servtab; sep; sep = sep->se_next)
1265 if (sep->se_fd == -1 && !ISMUX(sep))
1270 setup(struct servtab *sep)
1274 /* Set all listening sockets to close-on-exec. */
1275 if ((sep->se_fd = socket(sep->se_family,
1276 sep->se_socktype | SOCK_CLOEXEC, 0)) < 0) {
1278 warn("socket failed on %s/%s",
1279 sep->se_service, sep->se_proto);
1280 syslog(LOG_ERR, "%s/%s: socket: %m",
1281 sep->se_service, sep->se_proto);
1284 #define turnon(fd, opt) \
1285 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
1286 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
1287 turnon(sep->se_fd, SO_DEBUG) < 0)
1288 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
1289 if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
1290 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
1292 if (turnon(sep->se_fd, SO_PRIVSTATE) < 0)
1293 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m");
1295 /* tftpd opens a new connection then needs more infos */
1297 if ((sep->se_family == AF_INET6) &&
1298 (strcmp(sep->se_proto, "udp") == 0) &&
1299 (sep->se_accept == 0) &&
1300 (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1301 (char *)&on, sizeof (on)) < 0))
1302 syslog(LOG_ERR, "setsockopt (IPV6_RECVPKTINFO): %m");
1303 if (sep->se_family == AF_INET6) {
1304 int flag = sep->se_nomapped ? 1 : 0;
1305 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_V6ONLY,
1306 (char *)&flag, sizeof (flag)) < 0)
1307 syslog(LOG_ERR, "setsockopt (IPV6_V6ONLY): %m");
1314 if (sep->se_family == AF_UNIX) {
1315 (void) unlink(sep->se_ctrladdr_un.sun_path);
1316 umask(0777); /* Make socket with conservative permissions */
1318 if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr,
1319 sep->se_ctrladdr_size) < 0) {
1321 warn("bind failed on %s/%s",
1322 sep->se_service, sep->se_proto);
1323 syslog(LOG_ERR, "%s/%s: bind: %m",
1324 sep->se_service, sep->se_proto);
1325 (void) close(sep->se_fd);
1331 if (sep->se_family == AF_UNIX)
1335 if (sep->se_family == AF_UNIX) {
1336 /* Ick - fch{own,mod} don't work on Unix domain sockets */
1337 if (chown(sep->se_service, sep->se_sockuid, sep->se_sockgid) < 0)
1338 syslog(LOG_ERR, "chown socket: %m");
1339 if (chmod(sep->se_service, sep->se_sockmode) < 0)
1340 syslog(LOG_ERR, "chmod socket: %m");
1345 socklen_t len = sep->se_ctrladdr_size;
1346 struct netconfig *netid, *netid2 = NULL;
1348 struct sockaddr_in sock;
1350 struct netbuf nbuf, nbuf2;
1352 if (getsockname(sep->se_fd,
1353 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){
1354 syslog(LOG_ERR, "%s/%s: getsockname: %m",
1355 sep->se_service, sep->se_proto);
1356 (void) close(sep->se_fd);
1360 nbuf.buf = &sep->se_ctrladdr;
1361 nbuf.len = sep->se_ctrladdr.sa_len;
1362 if (sep->se_family == AF_INET)
1363 netid = sep->se_socktype==SOCK_DGRAM? udpconf:tcpconf;
1366 netid = sep->se_socktype==SOCK_DGRAM? udp6conf:tcp6conf;
1367 if (!sep->se_nomapped) { /* INET and INET6 */
1368 netid2 = netid==udp6conf? udpconf:tcpconf;
1369 memset(&sock, 0, sizeof sock); /* ADDR_ANY */
1371 nbuf2.len = sock.sin_len = sizeof sock;
1372 sock.sin_family = AF_INET;
1373 sock.sin_port = sep->se_ctrladdr6.sin6_port;
1378 print_service("REG ", sep);
1379 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1380 rpcb_unset(sep->se_rpc_prog, i, netid);
1381 rpcb_set(sep->se_rpc_prog, i, netid, &nbuf);
1383 rpcb_unset(sep->se_rpc_prog, i, netid2);
1384 rpcb_set(sep->se_rpc_prog, i, netid2, &nbuf2);
1388 if (sep->se_socktype == SOCK_STREAM)
1389 listen(sep->se_fd, -1);
1392 warnx("registered %s on %d",
1393 sep->se_server, sep->se_fd);
1399 ipsecsetup(struct servtab *sep)
1402 char *policy_in = NULL;
1403 char *policy_out = NULL;
1407 switch (sep->se_family) {
1410 opt = IP_IPSEC_POLICY;
1414 level = IPPROTO_IPV6;
1415 opt = IPV6_IPSEC_POLICY;
1422 if (!sep->se_policy || sep->se_policy[0] == '\0') {
1423 static char def_in[] = "in entrust", def_out[] = "out entrust";
1425 policy_out = def_out;
1427 if (!strncmp("in", sep->se_policy, 2))
1428 policy_in = sep->se_policy;
1429 else if (!strncmp("out", sep->se_policy, 3))
1430 policy_out = sep->se_policy;
1432 syslog(LOG_ERR, "invalid security policy \"%s\"",
1438 if (policy_in != NULL) {
1439 buf = ipsec_set_policy(policy_in, strlen(policy_in));
1441 if (setsockopt(sep->se_fd, level, opt,
1442 buf, ipsec_get_policylen(buf)) < 0 &&
1444 warnx("%s/%s: ipsec initialization failed; %s",
1445 sep->se_service, sep->se_proto,
1449 syslog(LOG_ERR, "invalid security policy \"%s\"",
1452 if (policy_out != NULL) {
1453 buf = ipsec_set_policy(policy_out, strlen(policy_out));
1455 if (setsockopt(sep->se_fd, level, opt,
1456 buf, ipsec_get_policylen(buf)) < 0 &&
1458 warnx("%s/%s: ipsec initialization failed; %s",
1459 sep->se_service, sep->se_proto,
1463 syslog(LOG_ERR, "invalid security policy \"%s\"",
1470 * Finish with a service and its socket.
1473 close_sep(struct servtab *sep)
1475 if (sep->se_fd >= 0) {
1476 if (FD_ISSET(sep->se_fd, &allsock))
1478 (void) close(sep->se_fd);
1482 sep->se_numchild = 0; /* forget about any existing children */
1486 matchservent(const char *name1, const char *name2, const char *proto)
1491 if (strcmp(proto, "unix") == 0) {
1492 if ((p = strrchr(name1, '/')) != NULL)
1494 if ((p = strrchr(name2, '/')) != NULL)
1497 if (strcmp(name1, name2) == 0)
1499 if ((se = getservbyname(name1, proto)) != NULL) {
1500 if (strcmp(name2, se->s_name) == 0)
1502 for (alias = se->s_aliases; *alias; alias++)
1503 if (strcmp(name2, *alias) == 0)
1509 static struct servtab *
1510 enter(struct servtab *cp)
1512 struct servtab *sep;
1515 sep = (struct servtab *)malloc(sizeof (*sep));
1516 if (sep == (struct servtab *)0) {
1517 syslog(LOG_ERR, "malloc: %m");
1522 omask = sigblock(SIGBLOCK);
1523 sep->se_next = servtab;
1530 enable(struct servtab *sep)
1534 "enabling %s, fd %d", sep->se_service, sep->se_fd);
1536 if (sep->se_fd < 0) {
1538 "%s: %s: bad fd", __func__, sep->se_service);
1543 "%s: %s: is mux", __func__, sep->se_service);
1546 if (FD_ISSET(sep->se_fd, &allsock)) {
1548 "%s: %s: not off", __func__, sep->se_service);
1553 FD_SET(sep->se_fd, &allsock);
1554 if (sep->se_fd > maxsock)
1555 maxsock = sep->se_fd;
1559 disable(struct servtab *sep)
1563 "disabling %s, fd %d", sep->se_service, sep->se_fd);
1565 if (sep->se_fd < 0) {
1567 "%s: %s: bad fd", __func__, sep->se_service);
1572 "%s: %s: is mux", __func__, sep->se_service);
1575 if (!FD_ISSET(sep->se_fd, &allsock)) {
1577 "%s: %s: not on", __func__, sep->se_service);
1581 syslog(LOG_ERR, "%s: nsock=0", __func__);
1586 FD_CLR(sep->se_fd, &allsock);
1587 if (sep->se_fd == maxsock)
1591 static FILE *fconfig = NULL;
1592 static struct servtab serv;
1593 static char line[LINE_MAX];
1599 if (fconfig != NULL) {
1600 fseek(fconfig, 0L, SEEK_SET);
1603 fconfig = fopen(CONFIG, "r");
1604 return (fconfig != NULL);
1611 (void) fclose(fconfig);
1616 static struct servtab *
1619 struct servtab *sep = &serv;
1623 static char TCPMUX_TOKEN[] = "tcpmux/";
1624 #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
1642 while ((cp = nextline(fconfig)) != NULL) {
1644 /* lines starting with #@ is not a comment, but the policy */
1645 if (cp[0] == '#' && cp[1] == '@') {
1647 for (p = cp + 2; p && *p && isspace(*p); p++)
1653 } else if (ipsec_get_policylen(p) >= 0) {
1659 "%s: invalid ipsec policy \"%s\"",
1665 if (*cp == '#' || *cp == '\0')
1670 return ((struct servtab *)0);
1672 * clear the static buffer, since some fields (se_ctrladdr,
1673 * for example) don't get initialized here.
1675 memset(sep, 0, sizeof *sep);
1678 /* got an empty line containing just blanks/tabs. */
1681 if (arg[0] == ':') { /* :user:group:perm: */
1682 char *user, *group, *perm;
1686 if ((group = strchr(user, ':')) == NULL) {
1687 syslog(LOG_ERR, "no group after user '%s'", user);
1691 if ((perm = strchr(group, ':')) == NULL) {
1692 syslog(LOG_ERR, "no mode after group '%s'", group);
1696 if ((pw = getpwnam(user)) == NULL) {
1697 syslog(LOG_ERR, "no such user '%s'", user);
1700 sep->se_sockuid = pw->pw_uid;
1701 if ((gr = getgrnam(group)) == NULL) {
1702 syslog(LOG_ERR, "no such user '%s'", group);
1705 sep->se_sockgid = gr->gr_gid;
1706 sep->se_sockmode = strtol(perm, &arg, 8);
1708 syslog(LOG_ERR, "bad mode '%s'", perm);
1713 sep->se_sockuid = euid;
1714 sep->se_sockgid = egid;
1715 sep->se_sockmode = 0200;
1717 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
1718 char *c = arg + MUX_LEN;
1720 sep->se_type = MUXPLUS_TYPE;
1723 sep->se_type = MUX_TYPE;
1724 sep->se_service = newstr(c);
1726 sep->se_service = newstr(arg);
1727 sep->se_type = NORM_TYPE;
1730 if (strcmp(arg, "stream") == 0)
1731 sep->se_socktype = SOCK_STREAM;
1732 else if (strcmp(arg, "dgram") == 0)
1733 sep->se_socktype = SOCK_DGRAM;
1734 else if (strcmp(arg, "rdm") == 0)
1735 sep->se_socktype = SOCK_RDM;
1736 else if (strcmp(arg, "seqpacket") == 0)
1737 sep->se_socktype = SOCK_SEQPACKET;
1738 else if (strcmp(arg, "raw") == 0)
1739 sep->se_socktype = SOCK_RAW;
1741 sep->se_socktype = -1;
1744 if (strncmp(arg, "tcp", 3) == 0) {
1745 sep->se_proto = newstr(strsep(&arg, "/"));
1746 if (arg != NULL && (strcmp(arg, "faith") == 0)) {
1747 syslog(LOG_ERR, "faith has been deprecated");
1751 if (sep->se_type == NORM_TYPE &&
1752 strncmp(arg, "faith/", 6) == 0) {
1753 syslog(LOG_ERR, "faith has been deprecated");
1756 sep->se_proto = newstr(arg);
1758 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1759 memmove(sep->se_proto, sep->se_proto + 4,
1760 strlen(sep->se_proto) + 1 - 4);
1762 sep->se_rpc_prog = sep->se_rpc_lowvers =
1763 sep->se_rpc_highvers = 0;
1764 if ((versp = strrchr(sep->se_service, '/'))) {
1766 switch (sscanf(versp, "%u-%u",
1767 &sep->se_rpc_lowvers,
1768 &sep->se_rpc_highvers)) {
1772 sep->se_rpc_highvers =
1773 sep->se_rpc_lowvers;
1777 "bad RPC version specifier; %s",
1784 sep->se_rpc_lowvers =
1785 sep->se_rpc_highvers = 1;
1788 sep->se_nomapped = 0;
1789 if (strcmp(sep->se_proto, "unix") == 0) {
1790 sep->se_family = AF_UNIX;
1792 while (isdigit(sep->se_proto[strlen(sep->se_proto) - 1])) {
1794 if (sep->se_proto[strlen(sep->se_proto) - 1] == '6') {
1795 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1800 if (sep->se_proto[strlen(sep->se_proto) - 1] == '4') {
1801 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1805 /* illegal version num */
1806 syslog(LOG_ERR, "bad IP version for %s", sep->se_proto);
1811 if (v6bind && !v6bind_ok) {
1812 syslog(LOG_INFO, "IPv6 bind is ignored for %s",
1814 if (v4bind && v4bind_ok)
1822 sep->se_family = AF_INET6;
1823 if (!v4bind || !v4bind_ok)
1824 sep->se_nomapped = 1;
1827 { /* default to v4 bind if not v6 bind */
1829 syslog(LOG_NOTICE, "IPv4 bind is ignored for %s",
1834 sep->se_family = AF_INET;
1838 switch(sep->se_family) {
1840 memcpy(&sep->se_ctrladdr4, bind_sa4,
1841 sizeof(sep->se_ctrladdr4));
1842 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr4);
1846 memcpy(&sep->se_ctrladdr6, bind_sa6,
1847 sizeof(sep->se_ctrladdr6));
1848 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr6);
1852 if (strlen(sep->se_service) >= sizeof(sep->se_ctrladdr_un.sun_path)) {
1854 "domain socket pathname too long for service %s",
1858 memset(&sep->se_ctrladdr, 0, sizeof(sep->se_ctrladdr));
1859 sep->se_ctrladdr_un.sun_family = sep->se_family;
1860 sep->se_ctrladdr_un.sun_len = strlen(sep->se_service);
1861 strcpy(sep->se_ctrladdr_un.sun_path, sep->se_service);
1862 sep->se_ctrladdr_size = SUN_LEN(&sep->se_ctrladdr_un);
1865 if (!strncmp(arg, "wait", 4))
1867 else if (!strncmp(arg, "nowait", 6))
1871 "%s: bad wait/nowait for service %s",
1872 CONFIG, sep->se_service);
1875 sep->se_maxchild = -1;
1876 sep->se_maxcpm = -1;
1877 sep->se_maxperip = -1;
1878 if ((s = strchr(arg, '/')) != NULL) {
1882 val = strtoul(s + 1, &eptr, 10);
1883 if (eptr == s + 1 || val > MAX_MAXCHLD) {
1885 "%s: bad max-child for service %s",
1886 CONFIG, sep->se_service);
1890 if (!sep->se_accept && val != 1)
1891 warnx("maxchild=%lu for wait service %s"
1892 " not recommended", val, sep->se_service);
1893 sep->se_maxchild = val;
1895 sep->se_maxcpm = strtol(eptr + 1, &eptr, 10);
1897 sep->se_maxperip = strtol(eptr + 1, &eptr, 10);
1899 * explicitly do not check for \0 for future expansion /
1900 * backwards compatibility
1905 * Silently enforce "nowait" mode for TCPMUX services
1906 * since they don't have an assigned port to listen on.
1909 if (strcmp(sep->se_proto, "tcp")) {
1911 "%s: bad protocol for tcpmux service %s",
1912 CONFIG, sep->se_service);
1915 if (sep->se_socktype != SOCK_STREAM) {
1917 "%s: bad socket type for tcpmux service %s",
1918 CONFIG, sep->se_service);
1922 sep->se_user = newstr(sskip(&cp));
1924 if ((s = strrchr(sep->se_user, '/')) != NULL) {
1926 sep->se_class = newstr(s + 1);
1928 sep->se_class = newstr(RESOURCE_RC);
1930 if ((s = strrchr(sep->se_user, ':')) != NULL) {
1932 sep->se_group = newstr(s + 1);
1934 sep->se_group = NULL;
1935 sep->se_server = newstr(sskip(&cp));
1936 if ((sep->se_server_name = strrchr(sep->se_server, '/')))
1937 sep->se_server_name++;
1938 if (strcmp(sep->se_server, "internal") == 0) {
1941 for (bi = biltins; bi->bi_service; bi++)
1942 if (bi->bi_socktype == sep->se_socktype &&
1943 matchservent(bi->bi_service, sep->se_service,
1946 if (bi->bi_service == 0) {
1947 syslog(LOG_ERR, "internal service %s unknown",
1951 sep->se_accept = 1; /* force accept mode for built-ins */
1955 if (sep->se_maxperip < 0)
1956 sep->se_maxperip = maxperip;
1957 if (sep->se_maxcpm < 0)
1958 sep->se_maxcpm = maxcpm;
1959 if (sep->se_maxchild < 0) { /* apply default max-children */
1960 if (sep->se_bi && sep->se_bi->bi_maxchild >= 0)
1961 sep->se_maxchild = sep->se_bi->bi_maxchild;
1962 else if (sep->se_accept)
1963 sep->se_maxchild = MAX(maxchild, 0);
1965 sep->se_maxchild = 1;
1967 if (sep->se_maxchild > 0) {
1968 sep->se_pids = malloc(sep->se_maxchild * sizeof(*sep->se_pids));
1969 if (sep->se_pids == NULL) {
1970 syslog(LOG_ERR, "malloc: %m");
1975 for (arg = skip(&cp); cp; arg = skip(&cp))
1976 if (argc < MAXARGV) {
1977 sep->se_argv[argc++] = newstr(arg);
1980 "%s: too many arguments for service %s",
1981 CONFIG, sep->se_service);
1984 while (argc <= MAXARGV)
1985 sep->se_argv[argc++] = NULL;
1986 for (i = 0; i < PERIPSIZE; ++i)
1987 LIST_INIT(&sep->se_conn[i]);
1989 sep->se_policy = policy ? newstr(policy) : NULL;
1995 freeconfig(struct servtab *cp)
2000 free(cp->se_service);
2012 free(cp->se_server);
2015 for (i = 0; i < MAXARGV; i++)
2017 free(cp->se_argv[i]);
2021 free(cp->se_policy);
2027 * Safe skip - if skip returns null, log a syntax error in the
2028 * configuration file and exit.
2037 syslog(LOG_ERR, "%s: syntax error", CONFIG);
2051 while (*cp == ' ' || *cp == '\t')
2057 (void) ungetc(c, fconfig);
2058 if (c == ' ' || c == '\t')
2059 if ((cp = nextline(fconfig)))
2064 if (*cp == '"' || *cp == '\'')
2068 while (*cp && *cp != quote)
2071 while (*cp && *cp != ' ' && *cp != '\t')
2084 if (fgets(line, sizeof (line), fd) == NULL)
2086 cp = strchr(line, '\n');
2093 newstr(const char *cp)
2097 if ((cr = strdup(cp != NULL ? cp : "")))
2099 syslog(LOG_ERR, "strdup: %m");
2104 inetd_setproctitle(const char *a, int s)
2107 struct sockaddr_storage ss;
2108 char buf[80], pbuf[NI_MAXHOST];
2111 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) {
2112 getnameinfo((struct sockaddr *)&ss, size, pbuf, sizeof(pbuf),
2113 NULL, 0, NI_NUMERICHOST);
2114 (void) sprintf(buf, "%s [%s]", a, pbuf);
2116 (void) sprintf(buf, "%s", a);
2117 setproctitle("%s", buf);
2121 check_loop(const struct sockaddr *sa, const struct servtab *sep)
2123 struct servtab *se2;
2124 char pname[NI_MAXHOST];
2126 for (se2 = servtab; se2; se2 = se2->se_next) {
2127 if (!se2->se_bi || se2->se_socktype != SOCK_DGRAM)
2130 switch (se2->se_family) {
2132 if (csatosin(sa)->sin_port ==
2133 se2->se_ctrladdr4.sin_port)
2138 if (csatosin6(sa)->sin6_port ==
2139 se2->se_ctrladdr6.sin6_port)
2147 getnameinfo(sa, sa->sa_len, pname, sizeof(pname), NULL, 0,
2149 syslog(LOG_WARNING, "%s/%s:%s/%s loop request REFUSED from %s",
2150 sep->se_service, sep->se_proto,
2151 se2->se_service, se2->se_proto,
2160 * Dump relevant information to stderr
2163 print_service(const char *action, const struct servtab *sep)
2166 "%s: %s proto=%s accept=%d max=%d user=%s group=%s"
2170 " builtin=%p server=%s"
2175 action, sep->se_service, sep->se_proto,
2176 sep->se_accept, sep->se_maxchild, sep->se_user, sep->se_group,
2180 (void *) sep->se_bi, sep->se_server
2182 , (sep->se_policy ? sep->se_policy : "")
2187 #define CPMHSIZE 256
2188 #define CPMHMASK (CPMHSIZE-1)
2192 typedef struct CTime {
2193 unsigned long ct_Ticks;
2197 typedef struct CHash {
2199 struct in_addr c4_Addr;
2200 struct in6_addr c6_Addr;
2202 #define ch_Addr4 cu_Addr.c4_Addr
2203 #define ch_Addr6 cu_Addr.c6_Addr
2207 CTime ch_Times[CHTSIZE];
2210 static CHash CHashAry[CPMHSIZE];
2213 cpmip(const struct servtab *sep, int ctrl)
2215 struct sockaddr_storage rss;
2216 socklen_t rssLen = sizeof(rss);
2220 * If getpeername() fails, just let it through (if logging is
2221 * enabled the condition is caught elsewhere)
2224 if (sep->se_maxcpm > 0 &&
2225 (sep->se_family == AF_INET || sep->se_family == AF_INET6) &&
2226 getpeername(ctrl, (struct sockaddr *)&rss, &rssLen) == 0 ) {
2227 time_t t = time(NULL);
2228 int hv = 0xABC3D20F;
2231 CHash *chBest = NULL;
2232 unsigned int ticks = t / CHTGRAN;
2233 struct sockaddr_in *sin4;
2235 struct sockaddr_in6 *sin6;
2238 sin4 = (struct sockaddr_in *)&rss;
2240 sin6 = (struct sockaddr_in6 *)&rss;
2246 switch (rss.ss_family) {
2248 p = (char *)&sin4->sin_addr;
2249 addrlen = sizeof(struct in_addr);
2253 p = (char *)&sin6->sin6_addr;
2254 addrlen = sizeof(struct in6_addr);
2258 /* should not happen */
2262 for (i = 0; i < addrlen; ++i, ++p) {
2263 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2265 hv = (hv ^ (hv >> 16));
2267 for (i = 0; i < 5; ++i) {
2268 CHash *ch = &CHashAry[(hv + i) & CPMHMASK];
2270 if (rss.ss_family == AF_INET &&
2271 ch->ch_Family == AF_INET &&
2272 sin4->sin_addr.s_addr == ch->ch_Addr4.s_addr &&
2273 ch->ch_Service && strcmp(sep->se_service,
2274 ch->ch_Service) == 0) {
2279 if (rss.ss_family == AF_INET6 &&
2280 ch->ch_Family == AF_INET6 &&
2281 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2282 &ch->ch_Addr6) != 0 &&
2283 ch->ch_Service && strcmp(sep->se_service,
2284 ch->ch_Service) == 0) {
2289 if (chBest == NULL || ch->ch_LTime == 0 ||
2290 ch->ch_LTime < chBest->ch_LTime) {
2294 if ((rss.ss_family == AF_INET &&
2295 (chBest->ch_Family != AF_INET ||
2296 sin4->sin_addr.s_addr != chBest->ch_Addr4.s_addr)) ||
2297 chBest->ch_Service == NULL ||
2298 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2299 chBest->ch_Family = sin4->sin_family;
2300 chBest->ch_Addr4 = sin4->sin_addr;
2301 if (chBest->ch_Service)
2302 free(chBest->ch_Service);
2303 chBest->ch_Service = strdup(sep->se_service);
2304 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2307 if ((rss.ss_family == AF_INET6 &&
2308 (chBest->ch_Family != AF_INET6 ||
2309 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2310 &chBest->ch_Addr6) == 0)) ||
2311 chBest->ch_Service == NULL ||
2312 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2313 chBest->ch_Family = sin6->sin6_family;
2314 chBest->ch_Addr6 = sin6->sin6_addr;
2315 if (chBest->ch_Service)
2316 free(chBest->ch_Service);
2317 chBest->ch_Service = strdup(sep->se_service);
2318 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2321 chBest->ch_LTime = t;
2323 CTime *ct = &chBest->ch_Times[ticks % CHTSIZE];
2324 if (ct->ct_Ticks != ticks) {
2325 ct->ct_Ticks = ticks;
2330 for (i = 0; i < CHTSIZE; ++i) {
2331 CTime *ct = &chBest->ch_Times[i];
2332 if (ct->ct_Ticks <= ticks &&
2333 ct->ct_Ticks >= ticks - CHTSIZE) {
2334 cnt += ct->ct_Count;
2337 if ((cnt * 60) / (CHTSIZE * CHTGRAN) > sep->se_maxcpm) {
2338 char pname[NI_MAXHOST];
2340 getnameinfo((struct sockaddr *)&rss,
2341 ((struct sockaddr *)&rss)->sa_len,
2342 pname, sizeof(pname), NULL, 0,
2346 "%s from %s exceeded counts/min (limit %d/min)",
2347 sep->se_service, pname,
2354 static struct conninfo *
2355 search_conn(struct servtab *sep, int ctrl)
2357 struct sockaddr_storage ss;
2358 socklen_t sslen = sizeof(ss);
2359 struct conninfo *conn;
2361 char pname[NI_MAXHOST], pname2[NI_MAXHOST];
2363 if (sep->se_maxperip <= 0)
2367 * If getpeername() fails, just let it through (if logging is
2368 * enabled the condition is caught elsewhere)
2370 if (getpeername(ctrl, (struct sockaddr *)&ss, &sslen) != 0)
2373 switch (ss.ss_family) {
2375 hv = hashval((char *)&((struct sockaddr_in *)&ss)->sin_addr,
2376 sizeof(struct in_addr));
2380 hv = hashval((char *)&((struct sockaddr_in6 *)&ss)->sin6_addr,
2381 sizeof(struct in6_addr));
2386 * Since we only support AF_INET and AF_INET6, just
2387 * let other than AF_INET and AF_INET6 through.
2392 if (getnameinfo((struct sockaddr *)&ss, sslen, pname, sizeof(pname),
2393 NULL, 0, NI_NUMERICHOST) != 0)
2396 LIST_FOREACH(conn, &sep->se_conn[hv], co_link) {
2397 if (getnameinfo((struct sockaddr *)&conn->co_addr,
2398 conn->co_addr.ss_len, pname2, sizeof(pname2), NULL, 0,
2399 NI_NUMERICHOST) == 0 &&
2400 strcmp(pname, pname2) == 0)
2405 if ((conn = malloc(sizeof(struct conninfo))) == NULL) {
2406 syslog(LOG_ERR, "malloc: %m");
2409 conn->co_proc = malloc(sep->se_maxperip * sizeof(*conn->co_proc));
2410 if (conn->co_proc == NULL) {
2411 syslog(LOG_ERR, "malloc: %m");
2414 memcpy(&conn->co_addr, (struct sockaddr *)&ss, sslen);
2415 conn->co_numchild = 0;
2416 LIST_INSERT_HEAD(&sep->se_conn[hv], conn, co_link);
2420 * Since a child process is not invoked yet, we cannot
2421 * determine a pid of a child. So, co_proc and co_numchild
2422 * should be filled leter.
2429 room_conn(struct servtab *sep, struct conninfo *conn)
2431 char pname[NI_MAXHOST];
2433 if (conn->co_numchild >= sep->se_maxperip) {
2434 getnameinfo((struct sockaddr *)&conn->co_addr,
2435 conn->co_addr.ss_len, pname, sizeof(pname), NULL, 0,
2437 syslog(LOG_ERR, "%s from %s exceeded counts (limit %d)",
2438 sep->se_service, pname, sep->se_maxperip);
2445 addchild_conn(struct conninfo *conn, pid_t pid)
2447 struct procinfo *proc;
2452 if ((proc = search_proc(pid, 1)) != NULL) {
2453 if (proc->pr_conn != NULL) {
2455 "addchild_conn: child already on process list");
2458 proc->pr_conn = conn;
2461 conn->co_proc[conn->co_numchild++] = proc;
2465 reapchild_conn(pid_t pid)
2467 struct procinfo *proc;
2468 struct conninfo *conn;
2471 if ((proc = search_proc(pid, 0)) == NULL)
2473 if ((conn = proc->pr_conn) == NULL)
2475 for (i = 0; i < conn->co_numchild; ++i)
2476 if (conn->co_proc[i] == proc) {
2477 conn->co_proc[i] = conn->co_proc[--conn->co_numchild];
2485 resize_conn(struct servtab *sep, int maxpip)
2487 struct conninfo *conn;
2490 if (sep->se_maxperip <= 0)
2496 for (i = 0; i < PERIPSIZE; ++i) {
2497 LIST_FOREACH(conn, &sep->se_conn[i], co_link) {
2498 for (j = maxpip; j < conn->co_numchild; ++j)
2499 free_proc(conn->co_proc[j]);
2500 conn->co_proc = realloc(conn->co_proc,
2501 maxpip * sizeof(*conn->co_proc));
2502 if (conn->co_proc == NULL) {
2503 syslog(LOG_ERR, "realloc: %m");
2506 if (conn->co_numchild > maxpip)
2507 conn->co_numchild = maxpip;
2513 free_connlist(struct servtab *sep)
2515 struct conninfo *conn;
2518 for (i = 0; i < PERIPSIZE; ++i) {
2519 while ((conn = LIST_FIRST(&sep->se_conn[i])) != NULL) {
2520 for (j = 0; j < conn->co_numchild; ++j)
2521 free_proc(conn->co_proc[j]);
2522 conn->co_numchild = 0;
2529 free_conn(struct conninfo *conn)
2533 if (conn->co_numchild <= 0) {
2534 LIST_REMOVE(conn, co_link);
2535 free(conn->co_proc);
2540 static struct procinfo *
2541 search_proc(pid_t pid, int add)
2543 struct procinfo *proc;
2546 hv = hashval((char *)&pid, sizeof(pid));
2547 LIST_FOREACH(proc, &proctable[hv], pr_link) {
2548 if (proc->pr_pid == pid)
2551 if (proc == NULL && add) {
2552 if ((proc = malloc(sizeof(struct procinfo))) == NULL) {
2553 syslog(LOG_ERR, "malloc: %m");
2557 proc->pr_conn = NULL;
2558 LIST_INSERT_HEAD(&proctable[hv], proc, pr_link);
2564 free_proc(struct procinfo *proc)
2568 LIST_REMOVE(proc, pr_link);
2573 hashval(char *p, int len)
2575 int i, hv = 0xABC3D20F;
2577 for (i = 0; i < len; ++i, ++p)
2578 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2579 hv = (hv ^ (hv >> 16)) & (PERIPSIZE - 1);