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 user 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 user 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/wait.h>
114 #include <sys/time.h>
115 #include <sys/resource.h>
116 #include <sys/stat.h>
119 #include <netinet/in.h>
120 #include <netinet/tcp.h>
121 #include <arpa/inet.h>
123 #include <rpc/pmap_clnt.h>
138 #include <sysexits.h>
144 #include "pathnames.h"
147 #include <netipsec/ipsec.h>
148 #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
153 #ifndef LIBWRAP_ALLOW_FACILITY
154 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
156 #ifndef LIBWRAP_ALLOW_SEVERITY
157 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
159 #ifndef LIBWRAP_DENY_FACILITY
160 # define LIBWRAP_DENY_FACILITY LOG_AUTH
162 #ifndef LIBWRAP_DENY_SEVERITY
163 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
166 #define ISWRAP(sep) \
167 ( ((wrap_ex && !(sep)->se_bi) || (wrap_bi && (sep)->se_bi)) \
168 && (sep->se_family == AF_INET || sep->se_family == AF_INET6) \
169 && ( ((sep)->se_accept && (sep)->se_socktype == SOCK_STREAM) \
170 || (sep)->se_socktype == SOCK_DGRAM))
173 #include <login_cap.h>
176 #define RESOURCE_RC "daemon"
181 #define MAXCHILD -1 /* maximum number of this service
186 #define MAXCPM -1 /* rate limit invocations from a
187 single remote address,
192 #define MAXPERIP -1 /* maximum number of this service
193 from a single remote address,
198 #define TOOMANY 256 /* don't start more than TOOMANY */
200 #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
201 #define RETRYTIME (60*10) /* retry after bind or server fail */
202 #define MAX_MAXCHLD 32767 /* max allowable max children */
204 #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
206 void close_sep(struct servtab *);
207 void flag_signal(int);
208 void flag_config(int);
210 int cpmip(const struct servtab *, int);
211 void endconfig(void);
212 struct servtab *enter(struct servtab *);
213 void freeconfig(struct servtab *);
214 struct servtab *getconfigent(void);
215 int matchservent(const char *, const char *, const char *);
216 char *nextline(FILE *);
217 void addchild(struct servtab *, int);
218 void flag_reapchild(int);
219 void reapchild(void);
220 void enable(struct servtab *);
221 void disable(struct servtab *);
222 void flag_retry(int);
225 void setup(struct servtab *);
227 void ipsecsetup(struct servtab *);
229 void unregisterrpc(register struct servtab *sep);
230 static struct conninfo *search_conn(struct servtab *sep, int ctrl);
231 static int room_conn(struct servtab *sep, struct conninfo *conn);
232 static void addchild_conn(struct conninfo *conn, pid_t pid);
233 static void reapchild_conn(pid_t pid);
234 static void free_conn(struct conninfo *conn);
235 static void resize_conn(struct servtab *sep, int maxperip);
236 static void free_connlist(struct servtab *sep);
237 static void free_proc(struct procinfo *);
238 static struct procinfo *search_proc(pid_t pid, int add);
239 static int hashval(char *p, int len);
247 int maxsock; /* highest-numbered descriptor */
251 int toomany = TOOMANY;
252 int maxchild = MAXCHILD;
254 int maxperip = MAXPERIP;
257 char *hostname = NULL;
258 struct sockaddr_in *bind_sa4;
261 struct sockaddr_in6 *bind_sa6;
272 struct servtab *servtab;
274 extern struct biltin biltins[];
276 const char *CONFIG = _PATH_INETDCONF;
277 const char *pid_file = _PATH_INETDPID;
278 struct pidfh *pfh = NULL;
280 struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
282 static LIST_HEAD(, procinfo) proctable[PERIPSIZE];
285 getvalue(const char *arg, int *value, const char *whine)
290 tmp = strtol(arg, &p, 0);
292 syslog(LOG_ERR, whine, arg);
293 return 1; /* failure */
296 return 0; /* success */
300 whichaf(struct request_info *req)
304 sa = (struct sockaddr *)req->client->sin;
307 if (sa->sa_family == AF_INET6 &&
308 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr))
310 return sa->sa_family;
314 main(int argc, char **argv)
319 struct sigaction sa, saalrm, sachld, sahup, sapipe;
324 login_cap_t *lc = NULL;
326 struct request_info req;
328 char *service = NULL;
330 struct sockaddr peer_un;
331 struct sockaddr_in peer_un4;
332 struct sockaddr_in6 peer_un6;
333 struct sockaddr_storage peer_max;
335 #define peer p_un.peer_un
336 #define peer4 p_un.peer_un4
337 #define peer6 p_un.peer_un6
338 #define peermax p_un.peer_max
340 struct addrinfo hints, *res;
341 const char *servname;
343 struct conninfo *conn;
345 openlog("inetd", LOG_PID | LOG_NOWAIT | LOG_PERROR, LOG_DAEMON);
347 while ((ch = getopt(argc, argv, "dlwWR:a:c:C:p:s:")) != -1)
357 getvalue(optarg, &toomany,
358 "-R %s: bad value for service invocation rate");
361 getvalue(optarg, &maxchild,
362 "-c %s: bad value for maximum children");
365 getvalue(optarg, &maxcpm,
366 "-C %s: bad value for maximum children/minute");
375 getvalue(optarg, &maxperip,
376 "-s %s: bad value for maximum children per source address");
387 "usage: inetd [-dlwW] [-a address] [-R rate]"
388 " [-c maximum] [-C rate]"
389 " [-p pidfile] [conf-file]");
393 * Initialize Bind Addrs.
394 * When hostname is NULL, wild card bind addrs are obtained from
395 * getaddrinfo(). But getaddrinfo() requires at least one of
396 * hostname or servname is non NULL.
397 * So when hostname is NULL, set dummy value to servname.
398 * Since getaddrinfo() doesn't accept numeric servname, and
399 * we doesn't use ai_socktype of struct addrinfo returned
400 * from getaddrinfo(), we set dummy value to ai_socktype.
402 servname = (hostname == NULL) ? "0" /* dummy */ : NULL;
404 bzero(&hints, sizeof(struct addrinfo));
405 hints.ai_flags = AI_PASSIVE;
406 hints.ai_family = AF_UNSPEC;
407 hints.ai_socktype = SOCK_STREAM; /* dummy */
408 error = getaddrinfo(hostname, servname, &hints, &res);
410 syslog(LOG_ERR, "-a %s: %s", hostname, gai_strerror(error));
411 if (error == EAI_SYSTEM)
412 syslog(LOG_ERR, "%s", strerror(errno));
416 if (res->ai_addr == NULL) {
417 syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
420 switch (res->ai_addr->sa_family) {
424 bind_sa4 = (struct sockaddr_in *)res->ai_addr;
425 /* init port num in case servname is dummy */
426 bind_sa4->sin_port = 0;
433 bind_sa6 = (struct sockaddr_in6 *)res->ai_addr;
434 /* init port num in case servname is dummy */
435 bind_sa6->sin6_port = 0;
446 } while ((res = res->ai_next) != NULL);
452 syslog(LOG_ERR, "-a %s: unknown address family", hostname);
458 umask(mask = umask(0777));
465 if (access(CONFIG, R_OK) < 0)
466 syslog(LOG_ERR, "Accessing %s: %m, continuing anyway.", CONFIG);
470 pfh = pidfile_open(pid_file, 0600, &otherpid);
472 if (errno == EEXIST) {
473 syslog(LOG_ERR, "%s already running, pid: %d",
474 getprogname(), otherpid);
477 syslog(LOG_WARNING, "pidfile_open() failed: %m");
480 if (daemon(0, 0) < 0) {
481 syslog(LOG_WARNING, "daemon(0,0) failed: %m");
483 /* From now on we don't want syslog messages going to stderr. */
485 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
487 * In case somebody has started inetd manually, we need to
488 * clear the logname, so that old servers run as root do not
489 * get the user's logname..
491 if (setlogin("") < 0) {
492 syslog(LOG_WARNING, "cannot clear logname: %m");
493 /* no big deal if it fails.. */
495 if (pfh != NULL && pidfile_write(pfh) == -1) {
496 syslog(LOG_WARNING, "pidfile_write(): %m");
500 for (i = 0; i < PERIPSIZE; ++i)
501 LIST_INIT(&proctable[i]);
504 udpconf = getnetconfigent("udp");
505 tcpconf = getnetconfigent("tcp");
506 if (udpconf == NULL || tcpconf == NULL) {
507 syslog(LOG_ERR, "unknown rpc/udp or rpc/tcp");
513 udp6conf = getnetconfigent("udp6");
514 tcp6conf = getnetconfigent("tcp6");
515 if (udp6conf == NULL || tcp6conf == NULL) {
516 syslog(LOG_ERR, "unknown rpc/udp6 or rpc/tcp6");
523 sigemptyset(&sa.sa_mask);
524 sigaddset(&sa.sa_mask, SIGALRM);
525 sigaddset(&sa.sa_mask, SIGCHLD);
526 sigaddset(&sa.sa_mask, SIGHUP);
527 sa.sa_handler = flag_retry;
528 sigaction(SIGALRM, &sa, &saalrm);
530 sa.sa_handler = flag_config;
531 sigaction(SIGHUP, &sa, &sahup);
532 sa.sa_handler = flag_reapchild;
533 sigaction(SIGCHLD, &sa, &sachld);
534 sa.sa_handler = SIG_IGN;
535 sigaction(SIGPIPE, &sa, &sapipe);
538 /* space for daemons to overwrite environment for ps */
539 #define DUMMYSIZE 100
540 char dummy[DUMMYSIZE];
542 (void)memset(dummy, 'x', DUMMYSIZE - 1);
543 dummy[DUMMYSIZE - 1] = '\0';
544 (void)setenv("inetd_dummy", dummy, 1);
547 if (pipe(signalpipe) != 0) {
548 syslog(LOG_ERR, "pipe: %m");
551 if (fcntl(signalpipe[0], F_SETFD, FD_CLOEXEC) < 0 ||
552 fcntl(signalpipe[1], F_SETFD, FD_CLOEXEC) < 0) {
553 syslog(LOG_ERR, "signalpipe: fcntl (F_SETFD, FD_CLOEXEC): %m");
556 FD_SET(signalpipe[0], &allsock);
560 if (signalpipe[0] > maxsock)
561 maxsock = signalpipe[0];
562 if (signalpipe[1] > maxsock)
563 maxsock = signalpipe[1];
571 syslog(LOG_ERR, "%s: nsock=0", __func__);
576 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
577 (fd_set *)0, (struct timeval *)0)) <= 0) {
578 if (n < 0 && errno != EINTR) {
579 syslog(LOG_WARNING, "select: %m");
584 /* handle any queued signal flags */
585 if (FD_ISSET(signalpipe[0], &readable)) {
587 if (ioctl(signalpipe[0], FIONREAD, &nsig) != 0) {
588 syslog(LOG_ERR, "ioctl: %m");
591 while (--nsig >= 0) {
593 if (read(signalpipe[0], &c, 1) != 1) {
594 syslog(LOG_ERR, "read: %m");
598 warnx("handling signal flag %c", c);
600 case 'A': /* sigalrm */
603 case 'C': /* sigchld */
606 case 'H': /* sighup */
612 for (sep = servtab; n && sep; sep = sep->se_next)
613 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
616 warnx("someone wants %s", sep->se_service);
617 dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep);
619 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) {
621 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
622 syslog(LOG_ERR, "ioctl (FIONBIO, 1): %m");
623 ctrl = accept(sep->se_fd, (struct sockaddr *)0,
626 warnx("accept, ctrl %d", ctrl);
630 "accept (for %s): %m",
632 if (sep->se_accept &&
633 sep->se_socktype == SOCK_STREAM)
638 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
639 syslog(LOG_ERR, "ioctl1(FIONBIO, 0): %m");
640 if (ioctl(ctrl, FIONBIO, &i) < 0)
641 syslog(LOG_ERR, "ioctl2(FIONBIO, 0): %m");
642 if (cpmip(sep, ctrl) < 0) {
647 (conn = search_conn(sep, ctrl)) != NULL &&
648 !room_conn(sep, conn)) {
654 if (dolog && !ISWRAP(sep)) {
655 char pname[INET6_ADDRSTRLEN] = "unknown";
658 if (getpeername(ctrl, (struct sockaddr *)
661 if (recvfrom(ctrl, buf, sizeof(buf),
663 (struct sockaddr *)&peermax,
665 getnameinfo((struct sockaddr *)&peermax,
667 pname, sizeof(pname),
668 NULL, 0, NI_NUMERICHOST);
671 getnameinfo((struct sockaddr *)&peermax,
673 pname, sizeof(pname),
674 NULL, 0, NI_NUMERICHOST);
676 syslog(LOG_INFO,"%s from %s", sep->se_service, pname);
678 (void) sigblock(SIGBLOCK);
681 * Fork for all external services, builtins which need to
682 * fork and anything we're wrapping (as wrapping might
683 * block or use hosts_options(5) twist).
686 if (sep->se_count++ == 0)
687 (void)gettimeofday(&sep->se_time, (struct timezone *)NULL);
688 else if (toomany > 0 && sep->se_count >= toomany) {
691 (void)gettimeofday(&now, (struct timezone *)NULL);
692 if (now.tv_sec - sep->se_time.tv_sec >
698 "%s/%s server failing (looping), service terminated",
699 sep->se_service, sep->se_proto);
700 if (sep->se_accept &&
701 sep->se_socktype == SOCK_STREAM)
716 syslog(LOG_ERR, "fork: %m");
717 if (sep->se_accept &&
718 sep->se_socktype == SOCK_STREAM)
726 addchild_conn(conn, pid);
733 sigaction(SIGALRM, &saalrm, (struct sigaction *)0);
734 sigaction(SIGCHLD, &sachld, (struct sigaction *)0);
735 sigaction(SIGHUP, &sahup, (struct sigaction *)0);
736 /* SIGPIPE reset before exec */
739 * Call tcpmux to find the real service to exec.
742 sep->se_bi->bi_fn == (bi_fn_t *) tcpmux) {
750 inetd_setproctitle("wrapping", ctrl);
751 service = sep->se_server_name ?
752 sep->se_server_name : sep->se_service;
753 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, 0);
755 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
756 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
757 denied = !hosts_access(&req);
759 syslog(deny_severity,
760 "refused connection from %.500s, service %s (%s%s)",
761 eval_client(&req), service, sep->se_proto,
762 (whichaf(&req) == AF_INET6) ? "6" : "");
763 if (sep->se_socktype != SOCK_STREAM)
764 recv(ctrl, buf, sizeof (buf), 0);
771 syslog(allow_severity,
772 "connection from %.500s, service %s (%s%s)",
773 eval_client(&req), service, sep->se_proto,
774 (whichaf(&req) == AF_INET6) ? "6" : "");
778 (*sep->se_bi->bi_fn)(ctrl, sep);
782 getpid(), sep->se_server);
783 /* Clear close-on-exec. */
784 if (fcntl(ctrl, F_SETFD, 0) < 0) {
786 "%s/%s: fcntl (F_SETFD, 0): %m",
787 sep->se_service, sep->se_proto);
796 if ((pwd = getpwnam(sep->se_user)) == NULL) {
798 "%s/%s: %s: no such user",
799 sep->se_service, sep->se_proto,
801 if (sep->se_socktype != SOCK_STREAM)
802 recv(0, buf, sizeof (buf), 0);
806 if ( sep->se_group != NULL
807 && (grp = getgrnam(sep->se_group)) == NULL
810 "%s/%s: %s: no such group",
811 sep->se_service, sep->se_proto,
813 if (sep->se_socktype != SOCK_STREAM)
814 recv(0, buf, sizeof (buf), 0);
818 pwd->pw_gid = grp->gr_gid;
820 if ((lc = login_getclass(sep->se_class)) == NULL) {
821 /* error syslogged by getclass */
823 "%s/%s: %s: login class error",
824 sep->se_service, sep->se_proto,
826 if (sep->se_socktype != SOCK_STREAM)
827 recv(0, buf, sizeof (buf), 0);
833 "%s: can't setsid(): %m",
835 /* _exit(EX_OSERR); not fatal yet */
838 if (setusercontext(lc, pwd, pwd->pw_uid,
839 LOGIN_SETALL & ~LOGIN_SETMAC)
842 "%s: can't setusercontext(..%s..): %m",
843 sep->se_service, sep->se_user);
849 if (setlogin(sep->se_user) < 0) {
851 "%s: can't setlogin(%s): %m",
852 sep->se_service, sep->se_user);
853 /* _exit(EX_OSERR); not yet */
855 if (setgid(pwd->pw_gid) < 0) {
857 "%s: can't set gid %d: %m",
858 sep->se_service, pwd->pw_gid);
861 (void) initgroups(pwd->pw_name,
863 if (setuid(pwd->pw_uid) < 0) {
865 "%s: can't set uid %d: %m",
866 sep->se_service, pwd->pw_uid);
871 sigaction(SIGPIPE, &sapipe,
872 (struct sigaction *)0);
873 execv(sep->se_server, sep->se_argv);
875 "cannot execute %s: %m", sep->se_server);
876 if (sep->se_socktype != SOCK_STREAM)
877 recv(0, buf, sizeof (buf), 0);
882 if (sep->se_accept && sep->se_socktype == SOCK_STREAM)
889 * Add a signal flag to the signal flag queue for later handling
897 if (write(signalpipe[1], &ch, 1) != 1) {
898 syslog(LOG_ERR, "write: %m");
904 * Record a new child pid for this service. If we've reached the
905 * limit on children, then stop accepting incoming requests.
909 addchild(struct servtab *sep, pid_t pid)
911 if (sep->se_maxchild <= 0)
914 if (sep->se_numchild >= sep->se_maxchild) {
915 syslog(LOG_ERR, "%s: %d >= %d",
916 __func__, sep->se_numchild, sep->se_maxchild);
920 sep->se_pids[sep->se_numchild++] = pid;
921 if (sep->se_numchild == sep->se_maxchild)
926 * Some child process has exited. See if it's on somebody's list.
930 flag_reapchild(int signo __unused)
943 pid = wait3(&status, WNOHANG, (struct rusage *)0);
947 warnx("%d reaped, %s %u", pid,
948 WIFEXITED(status) ? "status" : "signal",
949 WIFEXITED(status) ? WEXITSTATUS(status)
951 for (sep = servtab; sep; sep = sep->se_next) {
952 for (k = 0; k < sep->se_numchild; k++)
953 if (sep->se_pids[k] == pid)
955 if (k == sep->se_numchild)
957 if (sep->se_numchild == sep->se_maxchild)
959 sep->se_pids[k] = sep->se_pids[--sep->se_numchild];
960 if (WIFSIGNALED(status) || WEXITSTATUS(status))
962 "%s[%d]: exited, %s %u",
964 WIFEXITED(status) ? "status" : "signal",
965 WIFEXITED(status) ? WEXITSTATUS(status)
974 flag_config(int signo __unused)
982 struct servtab *sep, *new, **sepp;
986 login_cap_t *lc = NULL;
990 syslog(LOG_ERR, "%s: %m", CONFIG);
993 for (sep = servtab; sep; sep = sep->se_next)
995 while ((new = getconfigent())) {
996 if (getpwnam(new->se_user) == NULL) {
998 "%s/%s: no such user '%s', service ignored",
999 new->se_service, new->se_proto, new->se_user);
1002 if (new->se_group && getgrnam(new->se_group) == NULL) {
1004 "%s/%s: no such group '%s', service ignored",
1005 new->se_service, new->se_proto, new->se_group);
1009 if ((lc = login_getclass(new->se_class)) == NULL) {
1010 /* error syslogged by getclass */
1012 "%s/%s: %s: login class error, service ignored",
1013 new->se_service, new->se_proto, new->se_class);
1018 new_nomapped = new->se_nomapped;
1019 for (sep = servtab; sep; sep = sep->se_next)
1020 if (strcmp(sep->se_service, new->se_service) == 0 &&
1021 strcmp(sep->se_proto, new->se_proto) == 0 &&
1022 sep->se_rpc == new->se_rpc &&
1023 sep->se_socktype == new->se_socktype &&
1024 sep->se_family == new->se_family)
1029 #define SWAP(t,a, b) { t c = a; a = b; b = c; }
1030 omask = sigblock(SIGBLOCK);
1031 if (sep->se_nomapped != new->se_nomapped) {
1032 /* for rpc keep old nommaped till unregister */
1034 sep->se_nomapped = new->se_nomapped;
1037 /* copy over outstanding child pids */
1038 if (sep->se_maxchild > 0 && new->se_maxchild > 0) {
1039 new->se_numchild = sep->se_numchild;
1040 if (new->se_numchild > new->se_maxchild)
1041 new->se_numchild = new->se_maxchild;
1042 memcpy(new->se_pids, sep->se_pids,
1043 new->se_numchild * sizeof(*new->se_pids));
1045 SWAP(pid_t *, sep->se_pids, new->se_pids);
1046 sep->se_maxchild = new->se_maxchild;
1047 sep->se_numchild = new->se_numchild;
1048 sep->se_maxcpm = new->se_maxcpm;
1049 resize_conn(sep, new->se_maxperip);
1050 sep->se_maxperip = new->se_maxperip;
1051 sep->se_bi = new->se_bi;
1052 /* might need to turn on or off service now */
1053 if (sep->se_fd >= 0) {
1054 if (sep->se_maxchild > 0
1055 && sep->se_numchild == sep->se_maxchild) {
1056 if (FD_ISSET(sep->se_fd, &allsock))
1059 if (!FD_ISSET(sep->se_fd, &allsock))
1063 sep->se_accept = new->se_accept;
1064 SWAP(char *, sep->se_user, new->se_user);
1065 SWAP(char *, sep->se_group, new->se_group);
1067 SWAP(char *, sep->se_class, new->se_class);
1069 SWAP(char *, sep->se_server, new->se_server);
1070 SWAP(char *, sep->se_server_name, new->se_server_name);
1071 for (i = 0; i < MAXARGV; i++)
1072 SWAP(char *, sep->se_argv[i], new->se_argv[i]);
1074 SWAP(char *, sep->se_policy, new->se_policy);
1080 print_service("REDO", sep);
1084 print_service("ADD ", sep);
1086 sep->se_checked = 1;
1091 switch (sep->se_family) {
1108 if (sep->se_family != AF_UNIX) {
1109 sp = getservbyname(sep->se_service, sep->se_proto);
1111 syslog(LOG_ERR, "%s/%s: unknown service",
1112 sep->se_service, sep->se_proto);
1113 sep->se_checked = 0;
1117 switch (sep->se_family) {
1119 if (sp->s_port != sep->se_ctrladdr4.sin_port) {
1120 sep->se_ctrladdr4.sin_port =
1128 sep->se_ctrladdr6.sin6_port) {
1129 sep->se_ctrladdr6.sin6_port =
1136 if (sep->se_reset != 0 && sep->se_fd >= 0)
1139 rpc = getrpcbyname(sep->se_service);
1141 syslog(LOG_ERR, "%s/%s unknown RPC service",
1142 sep->se_service, sep->se_proto);
1143 if (sep->se_fd != -1)
1144 (void) close(sep->se_fd);
1148 if (sep->se_reset != 0 ||
1149 rpc->r_number != sep->se_rpc_prog) {
1150 if (sep->se_rpc_prog)
1152 sep->se_rpc_prog = rpc->r_number;
1153 if (sep->se_fd != -1)
1154 (void) close(sep->se_fd);
1157 sep->se_nomapped = new_nomapped;
1160 if (sep->se_fd == -1)
1165 * Purge anything not looked at above.
1167 omask = sigblock(SIGBLOCK);
1169 while ((sep = *sepp)) {
1170 if (sep->se_checked) {
1171 sepp = &sep->se_next;
1174 *sepp = sep->se_next;
1175 if (sep->se_fd >= 0)
1178 print_service("FREE", sep);
1179 if (sep->se_rpc && sep->se_rpc_prog > 0)
1184 (void) sigsetmask(omask);
1188 unregisterrpc(struct servtab *sep)
1191 struct servtab *sepp;
1193 struct netconfig *netid4, *netid6;
1195 omask = sigblock(SIGBLOCK);
1196 netid4 = sep->se_socktype == SOCK_DGRAM ? udpconf : tcpconf;
1197 netid6 = sep->se_socktype == SOCK_DGRAM ? udp6conf : tcp6conf;
1198 if (sep->se_family == AF_INET)
1200 else if (sep->se_nomapped)
1203 * Conflict if same prog and protocol - In that case one should look
1204 * to versions, but it is not interesting: having separate servers for
1205 * different versions does not work well.
1206 * Therefore one do not unregister if there is a conflict.
1207 * There is also transport conflict if destroying INET when INET46
1208 * exists, or destroying INET46 when INET exists
1210 for (sepp = servtab; sepp; sepp = sepp->se_next) {
1213 if (sepp->se_checked == 0 ||
1215 strcmp(sep->se_proto, sepp->se_proto) != 0 ||
1216 sep->se_rpc_prog != sepp->se_rpc_prog)
1218 if (sepp->se_family == AF_INET)
1220 if (sepp->se_family == AF_INET6) {
1222 if (!sep->se_nomapped)
1225 if (netid4 == NULL && netid6 == NULL)
1229 print_service("UNREG", sep);
1230 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1232 rpcb_unset(sep->se_rpc_prog, i, netid4);
1234 rpcb_unset(sep->se_rpc_prog, i, netid6);
1236 if (sep->se_fd != -1)
1237 (void) close(sep->se_fd);
1239 (void) sigsetmask(omask);
1243 flag_retry(int signo __unused)
1251 struct servtab *sep;
1254 for (sep = servtab; sep; sep = sep->se_next)
1255 if (sep->se_fd == -1 && !ISMUX(sep))
1260 setup(struct servtab *sep)
1264 if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
1266 warn("socket failed on %s/%s",
1267 sep->se_service, sep->se_proto);
1268 syslog(LOG_ERR, "%s/%s: socket: %m",
1269 sep->se_service, sep->se_proto);
1272 /* Set all listening sockets to close-on-exec. */
1273 if (fcntl(sep->se_fd, F_SETFD, FD_CLOEXEC) < 0) {
1274 syslog(LOG_ERR, "%s/%s: fcntl (F_SETFD, FD_CLOEXEC): %m",
1275 sep->se_service, sep->se_proto);
1279 #define turnon(fd, opt) \
1280 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
1281 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
1282 turnon(sep->se_fd, SO_DEBUG) < 0)
1283 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
1284 if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
1285 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
1287 if (turnon(sep->se_fd, SO_PRIVSTATE) < 0)
1288 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m");
1290 /* tftpd opens a new connection then needs more infos */
1291 if ((sep->se_family == AF_INET6) &&
1292 (strcmp(sep->se_proto, "udp") == 0) &&
1293 (sep->se_accept == 0) &&
1294 (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1295 (char *)&on, sizeof (on)) < 0))
1296 syslog(LOG_ERR, "setsockopt (IPV6_RECVPKTINFO): %m");
1297 if (sep->se_family == AF_INET6) {
1298 int flag = sep->se_nomapped ? 1 : 0;
1299 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_V6ONLY,
1300 (char *)&flag, sizeof (flag)) < 0)
1301 syslog(LOG_ERR, "setsockopt (IPV6_V6ONLY): %m");
1305 if (sep->se_type == FAITH_TYPE) {
1306 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_FAITH, &on,
1308 syslog(LOG_ERR, "setsockopt (IPV6_FAITH): %m");
1315 if (sep->se_family == AF_UNIX) {
1316 (void) unlink(sep->se_ctrladdr_un.sun_path);
1317 umask(0777); /* Make socket with conservative permissions */
1319 if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr,
1320 sep->se_ctrladdr_size) < 0) {
1322 warn("bind failed on %s/%s",
1323 sep->se_service, sep->se_proto);
1324 syslog(LOG_ERR, "%s/%s: bind: %m",
1325 sep->se_service, sep->se_proto);
1326 (void) close(sep->se_fd);
1332 if (sep->se_family == AF_UNIX)
1336 if (sep->se_family == AF_UNIX) {
1337 /* Ick - fch{own,mod} don't work on Unix domain sockets */
1338 if (chown(sep->se_service, sep->se_sockuid, sep->se_sockgid) < 0)
1339 syslog(LOG_ERR, "chown socket: %m");
1340 if (chmod(sep->se_service, sep->se_sockmode) < 0)
1341 syslog(LOG_ERR, "chmod socket: %m");
1346 socklen_t len = sep->se_ctrladdr_size;
1347 struct netconfig *netid, *netid2 = NULL;
1348 struct sockaddr_in sock;
1349 struct netbuf nbuf, nbuf2;
1351 if (getsockname(sep->se_fd,
1352 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){
1353 syslog(LOG_ERR, "%s/%s: getsockname: %m",
1354 sep->se_service, sep->se_proto);
1355 (void) close(sep->se_fd);
1359 nbuf.buf = &sep->se_ctrladdr;
1360 nbuf.len = sep->se_ctrladdr.sa_len;
1361 if (sep->se_family == AF_INET)
1362 netid = sep->se_socktype==SOCK_DGRAM? udpconf:tcpconf;
1364 netid = sep->se_socktype==SOCK_DGRAM? udp6conf:tcp6conf;
1365 if (!sep->se_nomapped) { /* INET and INET6 */
1366 netid2 = netid==udp6conf? udpconf:tcpconf;
1367 memset(&sock, 0, sizeof sock); /* ADDR_ANY */
1369 nbuf2.len = sock.sin_len = sizeof sock;
1370 sock.sin_family = AF_INET;
1371 sock.sin_port = sep->se_ctrladdr6.sin6_port;
1375 print_service("REG ", sep);
1376 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1377 rpcb_unset(sep->se_rpc_prog, i, netid);
1378 rpcb_set(sep->se_rpc_prog, i, netid, &nbuf);
1380 rpcb_unset(sep->se_rpc_prog, i, netid2);
1381 rpcb_set(sep->se_rpc_prog, i, netid2, &nbuf2);
1385 if (sep->se_socktype == SOCK_STREAM)
1386 listen(sep->se_fd, 64);
1389 warnx("registered %s on %d",
1390 sep->se_server, sep->se_fd);
1397 struct servtab *sep;
1400 char *policy_in = NULL;
1401 char *policy_out = NULL;
1405 switch (sep->se_family) {
1408 opt = IP_IPSEC_POLICY;
1412 level = IPPROTO_IPV6;
1413 opt = IPV6_IPSEC_POLICY;
1420 if (!sep->se_policy || sep->se_policy[0] == '\0') {
1421 static char def_in[] = "in entrust", def_out[] = "out entrust";
1423 policy_out = def_out;
1425 if (!strncmp("in", sep->se_policy, 2))
1426 policy_in = sep->se_policy;
1427 else if (!strncmp("out", sep->se_policy, 3))
1428 policy_out = sep->se_policy;
1430 syslog(LOG_ERR, "invalid security policy \"%s\"",
1436 if (policy_in != NULL) {
1437 buf = ipsec_set_policy(policy_in, strlen(policy_in));
1439 if (setsockopt(sep->se_fd, level, opt,
1440 buf, ipsec_get_policylen(buf)) < 0 &&
1442 warnx("%s/%s: ipsec initialization failed; %s",
1443 sep->se_service, sep->se_proto,
1447 syslog(LOG_ERR, "invalid security policy \"%s\"",
1450 if (policy_out != NULL) {
1451 buf = ipsec_set_policy(policy_out, strlen(policy_out));
1453 if (setsockopt(sep->se_fd, level, opt,
1454 buf, ipsec_get_policylen(buf)) < 0 &&
1456 warnx("%s/%s: ipsec initialization failed; %s",
1457 sep->se_service, sep->se_proto,
1461 syslog(LOG_ERR, "invalid security policy \"%s\"",
1468 * Finish with a service and its socket.
1471 close_sep(struct servtab *sep)
1473 if (sep->se_fd >= 0) {
1474 if (FD_ISSET(sep->se_fd, &allsock))
1476 (void) close(sep->se_fd);
1480 sep->se_numchild = 0; /* forget about any existing children */
1484 matchservent(const char *name1, const char *name2, const char *proto)
1489 if (strcmp(proto, "unix") == 0) {
1490 if ((p = strrchr(name1, '/')) != NULL)
1492 if ((p = strrchr(name2, '/')) != NULL)
1495 if (strcmp(name1, name2) == 0)
1497 if ((se = getservbyname(name1, proto)) != NULL) {
1498 if (strcmp(name2, se->s_name) == 0)
1500 for (alias = se->s_aliases; *alias; alias++)
1501 if (strcmp(name2, *alias) == 0)
1508 enter(struct servtab *cp)
1510 struct servtab *sep;
1513 sep = (struct servtab *)malloc(sizeof (*sep));
1514 if (sep == (struct servtab *)0) {
1515 syslog(LOG_ERR, "malloc: %m");
1520 omask = sigblock(SIGBLOCK);
1521 sep->se_next = servtab;
1528 enable(struct servtab *sep)
1532 "enabling %s, fd %d", sep->se_service, sep->se_fd);
1534 if (sep->se_fd < 0) {
1536 "%s: %s: bad fd", __func__, sep->se_service);
1541 "%s: %s: is mux", __func__, sep->se_service);
1544 if (FD_ISSET(sep->se_fd, &allsock)) {
1546 "%s: %s: not off", __func__, sep->se_service);
1551 FD_SET(sep->se_fd, &allsock);
1552 if (sep->se_fd > maxsock)
1553 maxsock = sep->se_fd;
1557 disable(struct servtab *sep)
1561 "disabling %s, fd %d", sep->se_service, sep->se_fd);
1563 if (sep->se_fd < 0) {
1565 "%s: %s: bad fd", __func__, sep->se_service);
1570 "%s: %s: is mux", __func__, sep->se_service);
1573 if (!FD_ISSET(sep->se_fd, &allsock)) {
1575 "%s: %s: not on", __func__, sep->se_service);
1579 syslog(LOG_ERR, "%s: nsock=0", __func__);
1584 FD_CLR(sep->se_fd, &allsock);
1585 if (sep->se_fd == maxsock)
1589 FILE *fconfig = NULL;
1590 struct servtab serv;
1591 char line[LINE_MAX];
1597 if (fconfig != NULL) {
1598 fseek(fconfig, 0L, SEEK_SET);
1601 fconfig = fopen(CONFIG, "r");
1602 return (fconfig != NULL);
1609 (void) fclose(fconfig);
1617 struct servtab *sep = &serv;
1621 static char TCPMUX_TOKEN[] = "tcpmux/";
1622 #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
1640 while ((cp = nextline(fconfig)) != NULL) {
1642 /* lines starting with #@ is not a comment, but the policy */
1643 if (cp[0] == '#' && cp[1] == '@') {
1645 for (p = cp + 2; p && *p && isspace(*p); p++)
1651 } else if (ipsec_get_policylen(p) >= 0) {
1657 "%s: invalid ipsec policy \"%s\"",
1663 if (*cp == '#' || *cp == '\0')
1668 return ((struct servtab *)0);
1670 * clear the static buffer, since some fields (se_ctrladdr,
1671 * for example) don't get initialized here.
1673 memset(sep, 0, sizeof *sep);
1676 /* got an empty line containing just blanks/tabs. */
1679 if (arg[0] == ':') { /* :user:group:perm: */
1680 char *user, *group, *perm;
1684 if ((group = strchr(user, ':')) == NULL) {
1685 syslog(LOG_ERR, "no group after user '%s'", user);
1689 if ((perm = strchr(group, ':')) == NULL) {
1690 syslog(LOG_ERR, "no mode after group '%s'", group);
1694 if ((pw = getpwnam(user)) == NULL) {
1695 syslog(LOG_ERR, "no such user '%s'", user);
1698 sep->se_sockuid = pw->pw_uid;
1699 if ((gr = getgrnam(group)) == NULL) {
1700 syslog(LOG_ERR, "no such user '%s'", group);
1703 sep->se_sockgid = gr->gr_gid;
1704 sep->se_sockmode = strtol(perm, &arg, 8);
1706 syslog(LOG_ERR, "bad mode '%s'", perm);
1711 sep->se_sockuid = euid;
1712 sep->se_sockgid = egid;
1713 sep->se_sockmode = 0200;
1715 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
1716 char *c = arg + MUX_LEN;
1718 sep->se_type = MUXPLUS_TYPE;
1721 sep->se_type = MUX_TYPE;
1722 sep->se_service = newstr(c);
1724 sep->se_service = newstr(arg);
1725 sep->se_type = NORM_TYPE;
1728 if (strcmp(arg, "stream") == 0)
1729 sep->se_socktype = SOCK_STREAM;
1730 else if (strcmp(arg, "dgram") == 0)
1731 sep->se_socktype = SOCK_DGRAM;
1732 else if (strcmp(arg, "rdm") == 0)
1733 sep->se_socktype = SOCK_RDM;
1734 else if (strcmp(arg, "seqpacket") == 0)
1735 sep->se_socktype = SOCK_SEQPACKET;
1736 else if (strcmp(arg, "raw") == 0)
1737 sep->se_socktype = SOCK_RAW;
1739 sep->se_socktype = -1;
1742 if (strncmp(arg, "tcp", 3) == 0) {
1743 sep->se_proto = newstr(strsep(&arg, "/"));
1745 if (strcmp(arg, "faith") == 0)
1746 sep->se_type = FAITH_TYPE;
1749 if (sep->se_type == NORM_TYPE &&
1750 strncmp(arg, "faith/", 6) == 0) {
1752 sep->se_type = FAITH_TYPE;
1754 sep->se_proto = newstr(arg);
1756 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1757 memmove(sep->se_proto, sep->se_proto + 4,
1758 strlen(sep->se_proto) + 1 - 4);
1760 sep->se_rpc_prog = sep->se_rpc_lowvers =
1761 sep->se_rpc_lowvers = 0;
1762 memcpy(&sep->se_ctrladdr4, bind_sa4,
1763 sizeof(sep->se_ctrladdr4));
1764 if ((versp = rindex(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 = rindex(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 = maxchild > 0 ? 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[INET6_ADDRSTRLEN];
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[INET6_ADDRSTRLEN];
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 (((const struct sockaddr_in *)sa)->sin_port ==
2133 se2->se_ctrladdr4.sin_port)
2138 if (((const struct sockaddr_in *)sa)->sin_port ==
2139 se2->se_ctrladdr4.sin_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 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[INET6_ADDRSTRLEN];
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);