2 * Copyright (c) 1983, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1991, 1993, 1994\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)from: inetd.c 8.4 (Berkeley) 4/13/94";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * Inetd - Internet super-server
48 * This program invokes all internet services as needed. Connection-oriented
49 * services are invoked each time a connection is made, by creating a process.
50 * This process is passed the connection as file descriptor 0 and is expected
51 * to do a getpeername to find out the source host and port.
53 * Datagram oriented services are invoked when a datagram
54 * arrives; a process is created and passed a pending message
55 * on file descriptor 0. Datagram servers may either connect
56 * to their peer, freeing up the original socket for inetd
57 * to receive further messages on, or ``take over the socket'',
58 * processing all arriving datagrams and, eventually, timing
59 * out. The first type of server is said to be ``multi-threaded'';
60 * the second type of server ``single-threaded''.
62 * Inetd uses a configuration file which is read at startup
63 * and, possibly, at some later time in response to a hangup signal.
64 * The configuration file is ``free format'' with fields given in the
65 * order shown below. Continuation lines for an entry must begin with
66 * a space or tab. All fields must be present in each entry.
68 * service name must be in /etc/services
69 * or name a tcpmux service
70 * or specify a unix domain socket
71 * socket type stream/dgram/raw/rdm/seqpacket
72 * protocol tcp[4][6][/faith], udp[4][6], unix
73 * wait/nowait single-threaded/multi-threaded
74 * user[:group][/login-class] user/group/login-class to run daemon as
75 * server program full path name
76 * server program arguments maximum of MAXARGS (20)
78 * TCP services without official port numbers are handled with the
79 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for
80 * requests. When a connection is made from a foreign host, the service
81 * requested is passed to tcpmux, which looks it up in the servtab list
82 * and returns the proper entry for the service. Tcpmux returns a
83 * negative reply if the service doesn't exist, otherwise the invoked
84 * server is expected to return the positive reply if the service type in
85 * inetd.conf file has the prefix "tcpmux/". If the service type has the
86 * prefix "tcpmux/+", tcpmux will return the positive reply for the
87 * process; this is for compatibility with older server code, and also
88 * allows you to invoke programs that use stdin/stdout without putting any
89 * special server code in them. Services that use tcpmux are "nowait"
90 * because they do not have a well-known port and hence cannot listen
94 * service name/version must be in /etc/rpc
95 * socket type stream/dgram/raw/rdm/seqpacket
96 * protocol rpc/tcp[4][6], rpc/udp[4][6]
97 * wait/nowait single-threaded/multi-threaded
98 * user[:group][/login-class] user/group/login-class to run daemon as
99 * server program full path name
100 * server program arguments maximum of MAXARGS
102 * Comment lines are indicated by a `#' in column 1.
105 * Comment lines that start with "#@" denote IPsec policy string, as described
106 * in ipsec_set_policy(3). This will affect all the following items in
107 * inetd.conf(8). To reset the policy, just use "#@" line. By default,
108 * there's no IPsec policy.
111 #include <sys/param.h>
112 #include <sys/ioctl.h>
113 #include <sys/mman.h>
114 #include <sys/wait.h>
115 #include <sys/time.h>
116 #include <sys/resource.h>
117 #include <sys/stat.h>
120 #include <netinet/in.h>
121 #include <netinet/tcp.h>
122 #include <arpa/inet.h>
124 #include <rpc/pmap_clnt.h>
139 #include <sysexits.h>
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 void close_sep(struct servtab *);
210 void flag_signal(int);
211 void flag_config(int);
213 int cpmip(const struct servtab *, int);
214 void endconfig(void);
215 struct servtab *enter(struct servtab *);
216 void freeconfig(struct servtab *);
217 struct servtab *getconfigent(void);
218 int matchservent(const char *, const char *, const char *);
219 char *nextline(FILE *);
220 void addchild(struct servtab *, int);
221 void flag_reapchild(int);
222 void reapchild(void);
223 void enable(struct servtab *);
224 void disable(struct servtab *);
225 void flag_retry(int);
228 void setup(struct servtab *);
230 void ipsecsetup(struct servtab *);
232 void unregisterrpc(register struct servtab *sep);
233 static struct conninfo *search_conn(struct servtab *sep, int ctrl);
234 static int room_conn(struct servtab *sep, struct conninfo *conn);
235 static void addchild_conn(struct conninfo *conn, pid_t pid);
236 static void reapchild_conn(pid_t pid);
237 static void free_conn(struct conninfo *conn);
238 static void resize_conn(struct servtab *sep, int maxperip);
239 static void free_connlist(struct servtab *sep);
240 static void free_proc(struct procinfo *);
241 static struct procinfo *search_proc(pid_t pid, int add);
242 static int hashval(char *p, int len);
250 int maxsock; /* highest-numbered descriptor */
254 int toomany = TOOMANY;
255 int maxchild = MAXCHILD;
257 int maxperip = MAXPERIP;
260 char *hostname = NULL;
261 struct sockaddr_in *bind_sa4;
264 struct sockaddr_in6 *bind_sa6;
275 struct servtab *servtab;
277 extern struct biltin biltins[];
279 const char *CONFIG = _PATH_INETDCONF;
280 const char *pid_file = _PATH_INETDPID;
281 struct pidfh *pfh = NULL;
283 struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
285 static LIST_HEAD(, procinfo) proctable[PERIPSIZE];
288 getvalue(const char *arg, int *value, const char *whine)
293 tmp = strtol(arg, &p, 0);
295 syslog(LOG_ERR, whine, arg);
296 return 1; /* failure */
299 return 0; /* success */
304 whichaf(struct request_info *req)
308 sa = (struct sockaddr *)req->client->sin;
311 if (sa->sa_family == AF_INET6 &&
312 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr))
314 return sa->sa_family;
319 main(int argc, char **argv)
324 struct sigaction sa, saalrm, sachld, sahup, sapipe;
329 login_cap_t *lc = NULL;
332 struct request_info req;
334 char *service = NULL;
336 struct sockaddr_storage peer;
338 struct addrinfo hints, *res;
339 const char *servname;
341 struct conninfo *conn;
343 openlog("inetd", LOG_PID | LOG_NOWAIT | LOG_PERROR, LOG_DAEMON);
345 while ((ch = getopt(argc, argv, "dlwWR:a:c:C:p:s:")) != -1)
355 getvalue(optarg, &toomany,
356 "-R %s: bad value for service invocation rate");
359 getvalue(optarg, &maxchild,
360 "-c %s: bad value for maximum children");
363 getvalue(optarg, &maxcpm,
364 "-C %s: bad value for maximum children/minute");
373 getvalue(optarg, &maxperip,
374 "-s %s: bad value for maximum children per source address");
385 "usage: inetd [-dlwW] [-a address] [-R rate]"
386 " [-c maximum] [-C rate]"
387 " [-p pidfile] [conf-file]");
391 * Initialize Bind Addrs.
392 * When hostname is NULL, wild card bind addrs are obtained from
393 * getaddrinfo(). But getaddrinfo() requires at least one of
394 * hostname or servname is non NULL.
395 * So when hostname is NULL, set dummy value to servname.
396 * Since getaddrinfo() doesn't accept numeric servname, and
397 * we doesn't use ai_socktype of struct addrinfo returned
398 * from getaddrinfo(), we set dummy value to ai_socktype.
400 servname = (hostname == NULL) ? "0" /* dummy */ : NULL;
402 bzero(&hints, sizeof(struct addrinfo));
403 hints.ai_flags = AI_PASSIVE;
404 hints.ai_family = AF_UNSPEC;
405 hints.ai_socktype = SOCK_STREAM; /* dummy */
406 error = getaddrinfo(hostname, servname, &hints, &res);
408 syslog(LOG_ERR, "-a %s: %s", hostname, gai_strerror(error));
409 if (error == EAI_SYSTEM)
410 syslog(LOG_ERR, "%s", strerror(errno));
414 if (res->ai_addr == NULL) {
415 syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
418 switch (res->ai_addr->sa_family) {
422 bind_sa4 = (struct sockaddr_in *)res->ai_addr;
423 /* init port num in case servname is dummy */
424 bind_sa4->sin_port = 0;
431 bind_sa6 = (struct sockaddr_in6 *)res->ai_addr;
432 /* init port num in case servname is dummy */
433 bind_sa6->sin6_port = 0;
444 } while ((res = res->ai_next) != NULL);
450 syslog(LOG_ERR, "-a %s: unknown address family", hostname);
456 umask(mask = umask(0777));
463 if (access(CONFIG, R_OK) < 0)
464 syslog(LOG_ERR, "Accessing %s: %m, continuing anyway.", CONFIG);
468 pfh = pidfile_open(pid_file, 0600, &otherpid);
470 if (errno == EEXIST) {
471 syslog(LOG_ERR, "%s already running, pid: %d",
472 getprogname(), otherpid);
475 syslog(LOG_WARNING, "pidfile_open() failed: %m");
478 if (daemon(0, 0) < 0) {
479 syslog(LOG_WARNING, "daemon(0,0) failed: %m");
481 /* From now on we don't want syslog messages going to stderr. */
483 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
485 * In case somebody has started inetd manually, we need to
486 * clear the logname, so that old servers run as root do not
487 * get the user's logname..
489 if (setlogin("") < 0) {
490 syslog(LOG_WARNING, "cannot clear logname: %m");
491 /* no big deal if it fails.. */
493 if (pfh != NULL && pidfile_write(pfh) == -1) {
494 syslog(LOG_WARNING, "pidfile_write(): %m");
498 if (madvise(NULL, 0, MADV_PROTECT) != 0)
499 syslog(LOG_WARNING, "madvise() failed: %s", strerror(errno));
501 for (i = 0; i < PERIPSIZE; ++i)
502 LIST_INIT(&proctable[i]);
505 udpconf = getnetconfigent("udp");
506 tcpconf = getnetconfigent("tcp");
507 if (udpconf == NULL || tcpconf == NULL) {
508 syslog(LOG_ERR, "unknown rpc/udp or rpc/tcp");
514 udp6conf = getnetconfigent("udp6");
515 tcp6conf = getnetconfigent("tcp6");
516 if (udp6conf == NULL || tcp6conf == NULL) {
517 syslog(LOG_ERR, "unknown rpc/udp6 or rpc/tcp6");
524 sigemptyset(&sa.sa_mask);
525 sigaddset(&sa.sa_mask, SIGALRM);
526 sigaddset(&sa.sa_mask, SIGCHLD);
527 sigaddset(&sa.sa_mask, SIGHUP);
528 sa.sa_handler = flag_retry;
529 sigaction(SIGALRM, &sa, &saalrm);
531 sa.sa_handler = flag_config;
532 sigaction(SIGHUP, &sa, &sahup);
533 sa.sa_handler = flag_reapchild;
534 sigaction(SIGCHLD, &sa, &sachld);
535 sa.sa_handler = SIG_IGN;
536 sigaction(SIGPIPE, &sa, &sapipe);
539 /* space for daemons to overwrite environment for ps */
540 #define DUMMYSIZE 100
541 char dummy[DUMMYSIZE];
543 (void)memset(dummy, 'x', DUMMYSIZE - 1);
544 dummy[DUMMYSIZE - 1] = '\0';
545 (void)setenv("inetd_dummy", dummy, 1);
548 if (pipe2(signalpipe, O_CLOEXEC) != 0) {
549 syslog(LOG_ERR, "pipe: %m");
552 FD_SET(signalpipe[0], &allsock);
556 if (signalpipe[0] > maxsock)
557 maxsock = signalpipe[0];
558 if (signalpipe[1] > maxsock)
559 maxsock = signalpipe[1];
567 syslog(LOG_ERR, "%s: nsock=0", __func__);
572 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
573 (fd_set *)0, (struct timeval *)0)) <= 0) {
574 if (n < 0 && errno != EINTR) {
575 syslog(LOG_WARNING, "select: %m");
580 /* handle any queued signal flags */
581 if (FD_ISSET(signalpipe[0], &readable)) {
583 if (ioctl(signalpipe[0], FIONREAD, &nsig) != 0) {
584 syslog(LOG_ERR, "ioctl: %m");
587 while (--nsig >= 0) {
589 if (read(signalpipe[0], &c, 1) != 1) {
590 syslog(LOG_ERR, "read: %m");
594 warnx("handling signal flag %c", c);
596 case 'A': /* sigalrm */
599 case 'C': /* sigchld */
602 case 'H': /* sighup */
608 for (sep = servtab; n && sep; sep = sep->se_next)
609 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
612 warnx("someone wants %s", sep->se_service);
613 dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep);
615 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) {
617 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
618 syslog(LOG_ERR, "ioctl (FIONBIO, 1): %m");
619 ctrl = accept(sep->se_fd, (struct sockaddr *)0,
622 warnx("accept, ctrl %d", ctrl);
626 "accept (for %s): %m",
628 if (sep->se_accept &&
629 sep->se_socktype == SOCK_STREAM)
634 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
635 syslog(LOG_ERR, "ioctl1(FIONBIO, 0): %m");
636 if (ioctl(ctrl, FIONBIO, &i) < 0)
637 syslog(LOG_ERR, "ioctl2(FIONBIO, 0): %m");
638 if (cpmip(sep, ctrl) < 0) {
643 (conn = search_conn(sep, ctrl)) != NULL &&
644 !room_conn(sep, conn)) {
650 if (dolog && !ISWRAP(sep)) {
651 char pname[NI_MAXHOST] = "unknown";
654 if (getpeername(ctrl, (struct sockaddr *)
657 if (recvfrom(ctrl, buf, sizeof(buf),
659 (struct sockaddr *)&peer,
661 getnameinfo((struct sockaddr *)&peer,
663 pname, sizeof(pname),
664 NULL, 0, NI_NUMERICHOST);
667 getnameinfo((struct sockaddr *)&peer,
669 pname, sizeof(pname),
670 NULL, 0, NI_NUMERICHOST);
672 syslog(LOG_INFO,"%s from %s", sep->se_service, pname);
674 (void) sigblock(SIGBLOCK);
677 * Fork for all external services, builtins which need to
678 * fork and anything we're wrapping (as wrapping might
679 * block or use hosts_options(5) twist).
682 if (sep->se_count++ == 0)
683 (void)clock_gettime(CLOCK_MONOTONIC_FAST, &sep->se_time);
684 else if (toomany > 0 && sep->se_count >= toomany) {
687 (void)clock_gettime(CLOCK_MONOTONIC_FAST, &now);
688 if (now.tv_sec - sep->se_time.tv_sec >
694 "%s/%s server failing (looping), service terminated",
695 sep->se_service, sep->se_proto);
696 if (sep->se_accept &&
697 sep->se_socktype == SOCK_STREAM)
712 syslog(LOG_ERR, "fork: %m");
713 if (sep->se_accept &&
714 sep->se_socktype == SOCK_STREAM)
722 addchild_conn(conn, pid);
729 sigaction(SIGALRM, &saalrm, (struct sigaction *)0);
730 sigaction(SIGCHLD, &sachld, (struct sigaction *)0);
731 sigaction(SIGHUP, &sahup, (struct sigaction *)0);
732 /* SIGPIPE reset before exec */
735 * Call tcpmux to find the real service to exec.
738 sep->se_bi->bi_fn == (bi_fn_t *) tcpmux) {
747 inetd_setproctitle("wrapping", ctrl);
748 service = sep->se_server_name ?
749 sep->se_server_name : sep->se_service;
750 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, 0);
752 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
753 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
754 denied = !hosts_access(&req);
756 syslog(deny_severity,
757 "refused connection from %.500s, service %s (%s%s)",
758 eval_client(&req), service, sep->se_proto,
759 (whichaf(&req) == AF_INET6) ? "6" : "");
760 if (sep->se_socktype != SOCK_STREAM)
761 recv(ctrl, buf, sizeof (buf), 0);
768 syslog(allow_severity,
769 "connection from %.500s, service %s (%s%s)",
770 eval_client(&req), service, sep->se_proto,
771 (whichaf(&req) == AF_INET6) ? "6" : "");
776 (*sep->se_bi->bi_fn)(ctrl, sep);
780 getpid(), sep->se_server);
781 /* Clear close-on-exec. */
782 if (fcntl(ctrl, F_SETFD, 0) < 0) {
784 "%s/%s: fcntl (F_SETFD, 0): %m",
785 sep->se_service, sep->se_proto);
794 if ((pwd = getpwnam(sep->se_user)) == NULL) {
796 "%s/%s: %s: no such user",
797 sep->se_service, sep->se_proto,
799 if (sep->se_socktype != SOCK_STREAM)
800 recv(0, buf, sizeof (buf), 0);
804 if ( sep->se_group != NULL
805 && (grp = getgrnam(sep->se_group)) == NULL
808 "%s/%s: %s: no such group",
809 sep->se_service, sep->se_proto,
811 if (sep->se_socktype != SOCK_STREAM)
812 recv(0, buf, sizeof (buf), 0);
816 pwd->pw_gid = grp->gr_gid;
818 if ((lc = login_getclass(sep->se_class)) == NULL) {
819 /* error syslogged by getclass */
821 "%s/%s: %s: login class error",
822 sep->se_service, sep->se_proto,
824 if (sep->se_socktype != SOCK_STREAM)
825 recv(0, buf, sizeof (buf), 0);
831 "%s: can't setsid(): %m",
833 /* _exit(EX_OSERR); not fatal yet */
836 if (setusercontext(lc, pwd, pwd->pw_uid,
837 LOGIN_SETALL & ~LOGIN_SETMAC)
840 "%s: can't setusercontext(..%s..): %m",
841 sep->se_service, sep->se_user);
847 if (setlogin(sep->se_user) < 0) {
849 "%s: can't setlogin(%s): %m",
850 sep->se_service, sep->se_user);
851 /* _exit(EX_OSERR); not yet */
853 if (setgid(pwd->pw_gid) < 0) {
855 "%s: can't set gid %d: %m",
856 sep->se_service, pwd->pw_gid);
859 (void) initgroups(pwd->pw_name,
861 if (setuid(pwd->pw_uid) < 0) {
863 "%s: can't set uid %d: %m",
864 sep->se_service, pwd->pw_uid);
869 sigaction(SIGPIPE, &sapipe,
870 (struct sigaction *)0);
871 execv(sep->se_server, sep->se_argv);
873 "cannot execute %s: %m", sep->se_server);
874 if (sep->se_socktype != SOCK_STREAM)
875 recv(0, buf, sizeof (buf), 0);
880 if (sep->se_accept && sep->se_socktype == SOCK_STREAM)
887 * Add a signal flag to the signal flag queue for later handling
895 if (write(signalpipe[1], &ch, 1) != 1) {
896 syslog(LOG_ERR, "write: %m");
902 * Record a new child pid for this service. If we've reached the
903 * limit on children, then stop accepting incoming requests.
907 addchild(struct servtab *sep, pid_t pid)
909 if (sep->se_maxchild <= 0)
912 if (sep->se_numchild >= sep->se_maxchild) {
913 syslog(LOG_ERR, "%s: %d >= %d",
914 __func__, sep->se_numchild, sep->se_maxchild);
918 sep->se_pids[sep->se_numchild++] = pid;
919 if (sep->se_numchild == sep->se_maxchild)
924 * Some child process has exited. See if it's on somebody's list.
928 flag_reapchild(int signo __unused)
941 pid = wait3(&status, WNOHANG, (struct rusage *)0);
945 warnx("%d reaped, %s %u", pid,
946 WIFEXITED(status) ? "status" : "signal",
947 WIFEXITED(status) ? WEXITSTATUS(status)
949 for (sep = servtab; sep; sep = sep->se_next) {
950 for (k = 0; k < sep->se_numchild; k++)
951 if (sep->se_pids[k] == pid)
953 if (k == sep->se_numchild)
955 if (sep->se_numchild == sep->se_maxchild)
957 sep->se_pids[k] = sep->se_pids[--sep->se_numchild];
958 if (WIFSIGNALED(status) || WEXITSTATUS(status))
960 "%s[%d]: exited, %s %u",
962 WIFEXITED(status) ? "status" : "signal",
963 WIFEXITED(status) ? WEXITSTATUS(status)
972 flag_config(int signo __unused)
980 struct servtab *sep, *new, **sepp;
984 login_cap_t *lc = NULL;
988 syslog(LOG_ERR, "%s: %m", CONFIG);
991 for (sep = servtab; sep; sep = sep->se_next)
993 while ((new = getconfigent())) {
994 if (getpwnam(new->se_user) == NULL) {
996 "%s/%s: no such user '%s', service ignored",
997 new->se_service, new->se_proto, new->se_user);
1000 if (new->se_group && getgrnam(new->se_group) == NULL) {
1002 "%s/%s: no such group '%s', service ignored",
1003 new->se_service, new->se_proto, new->se_group);
1007 if ((lc = login_getclass(new->se_class)) == NULL) {
1008 /* error syslogged by getclass */
1010 "%s/%s: %s: login class error, service ignored",
1011 new->se_service, new->se_proto, new->se_class);
1016 new_nomapped = new->se_nomapped;
1017 for (sep = servtab; sep; sep = sep->se_next)
1018 if (strcmp(sep->se_service, new->se_service) == 0 &&
1019 strcmp(sep->se_proto, new->se_proto) == 0 &&
1020 sep->se_rpc == new->se_rpc &&
1021 sep->se_socktype == new->se_socktype &&
1022 sep->se_family == new->se_family)
1027 #define SWAP(t,a, b) { t c = a; a = b; b = c; }
1028 omask = sigblock(SIGBLOCK);
1029 if (sep->se_nomapped != new->se_nomapped) {
1030 /* for rpc keep old nommaped till unregister */
1032 sep->se_nomapped = new->se_nomapped;
1035 /* copy over outstanding child pids */
1036 if (sep->se_maxchild > 0 && new->se_maxchild > 0) {
1037 new->se_numchild = sep->se_numchild;
1038 if (new->se_numchild > new->se_maxchild)
1039 new->se_numchild = new->se_maxchild;
1040 memcpy(new->se_pids, sep->se_pids,
1041 new->se_numchild * sizeof(*new->se_pids));
1043 SWAP(pid_t *, sep->se_pids, new->se_pids);
1044 sep->se_maxchild = new->se_maxchild;
1045 sep->se_numchild = new->se_numchild;
1046 sep->se_maxcpm = new->se_maxcpm;
1047 resize_conn(sep, new->se_maxperip);
1048 sep->se_maxperip = new->se_maxperip;
1049 sep->se_bi = new->se_bi;
1050 /* might need to turn on or off service now */
1051 if (sep->se_fd >= 0) {
1052 if (sep->se_maxchild > 0
1053 && sep->se_numchild == sep->se_maxchild) {
1054 if (FD_ISSET(sep->se_fd, &allsock))
1057 if (!FD_ISSET(sep->se_fd, &allsock))
1061 sep->se_accept = new->se_accept;
1062 SWAP(char *, sep->se_user, new->se_user);
1063 SWAP(char *, sep->se_group, new->se_group);
1065 SWAP(char *, sep->se_class, new->se_class);
1067 SWAP(char *, sep->se_server, new->se_server);
1068 SWAP(char *, sep->se_server_name, new->se_server_name);
1069 for (i = 0; i < MAXARGV; i++)
1070 SWAP(char *, sep->se_argv[i], new->se_argv[i]);
1072 SWAP(char *, sep->se_policy, new->se_policy);
1078 print_service("REDO", sep);
1082 print_service("ADD ", sep);
1084 sep->se_checked = 1;
1089 switch (sep->se_family) {
1106 if (sep->se_family != AF_UNIX) {
1107 sp = getservbyname(sep->se_service, sep->se_proto);
1109 syslog(LOG_ERR, "%s/%s: unknown service",
1110 sep->se_service, sep->se_proto);
1111 sep->se_checked = 0;
1115 switch (sep->se_family) {
1117 if (sp->s_port != sep->se_ctrladdr4.sin_port) {
1118 sep->se_ctrladdr4.sin_port =
1126 sep->se_ctrladdr6.sin6_port) {
1127 sep->se_ctrladdr6.sin6_port =
1134 if (sep->se_reset != 0 && sep->se_fd >= 0)
1137 rpc = getrpcbyname(sep->se_service);
1139 syslog(LOG_ERR, "%s/%s unknown RPC service",
1140 sep->se_service, sep->se_proto);
1141 if (sep->se_fd != -1)
1142 (void) close(sep->se_fd);
1146 if (sep->se_reset != 0 ||
1147 rpc->r_number != sep->se_rpc_prog) {
1148 if (sep->se_rpc_prog)
1150 sep->se_rpc_prog = rpc->r_number;
1151 if (sep->se_fd != -1)
1152 (void) close(sep->se_fd);
1155 sep->se_nomapped = new_nomapped;
1158 if (sep->se_fd == -1)
1163 * Purge anything not looked at above.
1165 omask = sigblock(SIGBLOCK);
1167 while ((sep = *sepp)) {
1168 if (sep->se_checked) {
1169 sepp = &sep->se_next;
1172 *sepp = sep->se_next;
1173 if (sep->se_fd >= 0)
1176 print_service("FREE", sep);
1177 if (sep->se_rpc && sep->se_rpc_prog > 0)
1182 (void) sigsetmask(omask);
1186 unregisterrpc(struct servtab *sep)
1189 struct servtab *sepp;
1191 struct netconfig *netid4, *netid6;
1193 omask = sigblock(SIGBLOCK);
1194 netid4 = sep->se_socktype == SOCK_DGRAM ? udpconf : tcpconf;
1195 netid6 = sep->se_socktype == SOCK_DGRAM ? udp6conf : tcp6conf;
1196 if (sep->se_family == AF_INET)
1198 else if (sep->se_nomapped)
1201 * Conflict if same prog and protocol - In that case one should look
1202 * to versions, but it is not interesting: having separate servers for
1203 * different versions does not work well.
1204 * Therefore one do not unregister if there is a conflict.
1205 * There is also transport conflict if destroying INET when INET46
1206 * exists, or destroying INET46 when INET exists
1208 for (sepp = servtab; sepp; sepp = sepp->se_next) {
1211 if (sepp->se_checked == 0 ||
1213 strcmp(sep->se_proto, sepp->se_proto) != 0 ||
1214 sep->se_rpc_prog != sepp->se_rpc_prog)
1216 if (sepp->se_family == AF_INET)
1218 if (sepp->se_family == AF_INET6) {
1220 if (!sep->se_nomapped)
1223 if (netid4 == NULL && netid6 == NULL)
1227 print_service("UNREG", sep);
1228 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1230 rpcb_unset(sep->se_rpc_prog, i, netid4);
1232 rpcb_unset(sep->se_rpc_prog, i, netid6);
1234 if (sep->se_fd != -1)
1235 (void) close(sep->se_fd);
1237 (void) sigsetmask(omask);
1241 flag_retry(int signo __unused)
1249 struct servtab *sep;
1252 for (sep = servtab; sep; sep = sep->se_next)
1253 if (sep->se_fd == -1 && !ISMUX(sep))
1258 setup(struct servtab *sep)
1262 /* Set all listening sockets to close-on-exec. */
1263 if ((sep->se_fd = socket(sep->se_family,
1264 sep->se_socktype | SOCK_CLOEXEC, 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 #define turnon(fd, opt) \
1273 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
1274 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
1275 turnon(sep->se_fd, SO_DEBUG) < 0)
1276 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
1277 if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
1278 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
1280 if (turnon(sep->se_fd, SO_PRIVSTATE) < 0)
1281 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m");
1283 /* tftpd opens a new connection then needs more infos */
1284 if ((sep->se_family == AF_INET6) &&
1285 (strcmp(sep->se_proto, "udp") == 0) &&
1286 (sep->se_accept == 0) &&
1287 (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1288 (char *)&on, sizeof (on)) < 0))
1289 syslog(LOG_ERR, "setsockopt (IPV6_RECVPKTINFO): %m");
1290 if (sep->se_family == AF_INET6) {
1291 int flag = sep->se_nomapped ? 1 : 0;
1292 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_V6ONLY,
1293 (char *)&flag, sizeof (flag)) < 0)
1294 syslog(LOG_ERR, "setsockopt (IPV6_V6ONLY): %m");
1298 if (sep->se_type == FAITH_TYPE) {
1299 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_FAITH, &on,
1301 syslog(LOG_ERR, "setsockopt (IPV6_FAITH): %m");
1308 if (sep->se_family == AF_UNIX) {
1309 (void) unlink(sep->se_ctrladdr_un.sun_path);
1310 umask(0777); /* Make socket with conservative permissions */
1312 if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr,
1313 sep->se_ctrladdr_size) < 0) {
1315 warn("bind failed on %s/%s",
1316 sep->se_service, sep->se_proto);
1317 syslog(LOG_ERR, "%s/%s: bind: %m",
1318 sep->se_service, sep->se_proto);
1319 (void) close(sep->se_fd);
1325 if (sep->se_family == AF_UNIX)
1329 if (sep->se_family == AF_UNIX) {
1330 /* Ick - fch{own,mod} don't work on Unix domain sockets */
1331 if (chown(sep->se_service, sep->se_sockuid, sep->se_sockgid) < 0)
1332 syslog(LOG_ERR, "chown socket: %m");
1333 if (chmod(sep->se_service, sep->se_sockmode) < 0)
1334 syslog(LOG_ERR, "chmod socket: %m");
1339 socklen_t len = sep->se_ctrladdr_size;
1340 struct netconfig *netid, *netid2 = NULL;
1341 struct sockaddr_in sock;
1342 struct netbuf nbuf, nbuf2;
1344 if (getsockname(sep->se_fd,
1345 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){
1346 syslog(LOG_ERR, "%s/%s: getsockname: %m",
1347 sep->se_service, sep->se_proto);
1348 (void) close(sep->se_fd);
1352 nbuf.buf = &sep->se_ctrladdr;
1353 nbuf.len = sep->se_ctrladdr.sa_len;
1354 if (sep->se_family == AF_INET)
1355 netid = sep->se_socktype==SOCK_DGRAM? udpconf:tcpconf;
1357 netid = sep->se_socktype==SOCK_DGRAM? udp6conf:tcp6conf;
1358 if (!sep->se_nomapped) { /* INET and INET6 */
1359 netid2 = netid==udp6conf? udpconf:tcpconf;
1360 memset(&sock, 0, sizeof sock); /* ADDR_ANY */
1362 nbuf2.len = sock.sin_len = sizeof sock;
1363 sock.sin_family = AF_INET;
1364 sock.sin_port = sep->se_ctrladdr6.sin6_port;
1368 print_service("REG ", sep);
1369 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1370 rpcb_unset(sep->se_rpc_prog, i, netid);
1371 rpcb_set(sep->se_rpc_prog, i, netid, &nbuf);
1373 rpcb_unset(sep->se_rpc_prog, i, netid2);
1374 rpcb_set(sep->se_rpc_prog, i, netid2, &nbuf2);
1378 if (sep->se_socktype == SOCK_STREAM)
1379 listen(sep->se_fd, -1);
1382 warnx("registered %s on %d",
1383 sep->se_server, sep->se_fd);
1389 ipsecsetup(struct servtab *sep)
1392 char *policy_in = NULL;
1393 char *policy_out = NULL;
1397 switch (sep->se_family) {
1400 opt = IP_IPSEC_POLICY;
1404 level = IPPROTO_IPV6;
1405 opt = IPV6_IPSEC_POLICY;
1412 if (!sep->se_policy || sep->se_policy[0] == '\0') {
1413 static char def_in[] = "in entrust", def_out[] = "out entrust";
1415 policy_out = def_out;
1417 if (!strncmp("in", sep->se_policy, 2))
1418 policy_in = sep->se_policy;
1419 else if (!strncmp("out", sep->se_policy, 3))
1420 policy_out = sep->se_policy;
1422 syslog(LOG_ERR, "invalid security policy \"%s\"",
1428 if (policy_in != NULL) {
1429 buf = ipsec_set_policy(policy_in, strlen(policy_in));
1431 if (setsockopt(sep->se_fd, level, opt,
1432 buf, ipsec_get_policylen(buf)) < 0 &&
1434 warnx("%s/%s: ipsec initialization failed; %s",
1435 sep->se_service, sep->se_proto,
1439 syslog(LOG_ERR, "invalid security policy \"%s\"",
1442 if (policy_out != NULL) {
1443 buf = ipsec_set_policy(policy_out, strlen(policy_out));
1445 if (setsockopt(sep->se_fd, level, opt,
1446 buf, ipsec_get_policylen(buf)) < 0 &&
1448 warnx("%s/%s: ipsec initialization failed; %s",
1449 sep->se_service, sep->se_proto,
1453 syslog(LOG_ERR, "invalid security policy \"%s\"",
1460 * Finish with a service and its socket.
1463 close_sep(struct servtab *sep)
1465 if (sep->se_fd >= 0) {
1466 if (FD_ISSET(sep->se_fd, &allsock))
1468 (void) close(sep->se_fd);
1472 sep->se_numchild = 0; /* forget about any existing children */
1476 matchservent(const char *name1, const char *name2, const char *proto)
1481 if (strcmp(proto, "unix") == 0) {
1482 if ((p = strrchr(name1, '/')) != NULL)
1484 if ((p = strrchr(name2, '/')) != NULL)
1487 if (strcmp(name1, name2) == 0)
1489 if ((se = getservbyname(name1, proto)) != NULL) {
1490 if (strcmp(name2, se->s_name) == 0)
1492 for (alias = se->s_aliases; *alias; alias++)
1493 if (strcmp(name2, *alias) == 0)
1500 enter(struct servtab *cp)
1502 struct servtab *sep;
1505 sep = (struct servtab *)malloc(sizeof (*sep));
1506 if (sep == (struct servtab *)0) {
1507 syslog(LOG_ERR, "malloc: %m");
1512 omask = sigblock(SIGBLOCK);
1513 sep->se_next = servtab;
1520 enable(struct servtab *sep)
1524 "enabling %s, fd %d", sep->se_service, sep->se_fd);
1526 if (sep->se_fd < 0) {
1528 "%s: %s: bad fd", __func__, sep->se_service);
1533 "%s: %s: is mux", __func__, sep->se_service);
1536 if (FD_ISSET(sep->se_fd, &allsock)) {
1538 "%s: %s: not off", __func__, sep->se_service);
1543 FD_SET(sep->se_fd, &allsock);
1544 if (sep->se_fd > maxsock)
1545 maxsock = sep->se_fd;
1549 disable(struct servtab *sep)
1553 "disabling %s, fd %d", sep->se_service, sep->se_fd);
1555 if (sep->se_fd < 0) {
1557 "%s: %s: bad fd", __func__, sep->se_service);
1562 "%s: %s: is mux", __func__, sep->se_service);
1565 if (!FD_ISSET(sep->se_fd, &allsock)) {
1567 "%s: %s: not on", __func__, sep->se_service);
1571 syslog(LOG_ERR, "%s: nsock=0", __func__);
1576 FD_CLR(sep->se_fd, &allsock);
1577 if (sep->se_fd == maxsock)
1581 FILE *fconfig = NULL;
1582 struct servtab serv;
1583 char line[LINE_MAX];
1589 if (fconfig != NULL) {
1590 fseek(fconfig, 0L, SEEK_SET);
1593 fconfig = fopen(CONFIG, "r");
1594 return (fconfig != NULL);
1601 (void) fclose(fconfig);
1609 struct servtab *sep = &serv;
1613 static char TCPMUX_TOKEN[] = "tcpmux/";
1614 #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
1632 while ((cp = nextline(fconfig)) != NULL) {
1634 /* lines starting with #@ is not a comment, but the policy */
1635 if (cp[0] == '#' && cp[1] == '@') {
1637 for (p = cp + 2; p && *p && isspace(*p); p++)
1643 } else if (ipsec_get_policylen(p) >= 0) {
1649 "%s: invalid ipsec policy \"%s\"",
1655 if (*cp == '#' || *cp == '\0')
1660 return ((struct servtab *)0);
1662 * clear the static buffer, since some fields (se_ctrladdr,
1663 * for example) don't get initialized here.
1665 memset(sep, 0, sizeof *sep);
1668 /* got an empty line containing just blanks/tabs. */
1671 if (arg[0] == ':') { /* :user:group:perm: */
1672 char *user, *group, *perm;
1676 if ((group = strchr(user, ':')) == NULL) {
1677 syslog(LOG_ERR, "no group after user '%s'", user);
1681 if ((perm = strchr(group, ':')) == NULL) {
1682 syslog(LOG_ERR, "no mode after group '%s'", group);
1686 if ((pw = getpwnam(user)) == NULL) {
1687 syslog(LOG_ERR, "no such user '%s'", user);
1690 sep->se_sockuid = pw->pw_uid;
1691 if ((gr = getgrnam(group)) == NULL) {
1692 syslog(LOG_ERR, "no such user '%s'", group);
1695 sep->se_sockgid = gr->gr_gid;
1696 sep->se_sockmode = strtol(perm, &arg, 8);
1698 syslog(LOG_ERR, "bad mode '%s'", perm);
1703 sep->se_sockuid = euid;
1704 sep->se_sockgid = egid;
1705 sep->se_sockmode = 0200;
1707 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
1708 char *c = arg + MUX_LEN;
1710 sep->se_type = MUXPLUS_TYPE;
1713 sep->se_type = MUX_TYPE;
1714 sep->se_service = newstr(c);
1716 sep->se_service = newstr(arg);
1717 sep->se_type = NORM_TYPE;
1720 if (strcmp(arg, "stream") == 0)
1721 sep->se_socktype = SOCK_STREAM;
1722 else if (strcmp(arg, "dgram") == 0)
1723 sep->se_socktype = SOCK_DGRAM;
1724 else if (strcmp(arg, "rdm") == 0)
1725 sep->se_socktype = SOCK_RDM;
1726 else if (strcmp(arg, "seqpacket") == 0)
1727 sep->se_socktype = SOCK_SEQPACKET;
1728 else if (strcmp(arg, "raw") == 0)
1729 sep->se_socktype = SOCK_RAW;
1731 sep->se_socktype = -1;
1734 if (strncmp(arg, "tcp", 3) == 0) {
1735 sep->se_proto = newstr(strsep(&arg, "/"));
1737 if (strcmp(arg, "faith") == 0)
1738 sep->se_type = FAITH_TYPE;
1741 if (sep->se_type == NORM_TYPE &&
1742 strncmp(arg, "faith/", 6) == 0) {
1744 sep->se_type = FAITH_TYPE;
1746 sep->se_proto = newstr(arg);
1748 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1749 memmove(sep->se_proto, sep->se_proto + 4,
1750 strlen(sep->se_proto) + 1 - 4);
1752 sep->se_rpc_prog = sep->se_rpc_lowvers =
1753 sep->se_rpc_highvers = 0;
1754 if ((versp = strrchr(sep->se_service, '/'))) {
1756 switch (sscanf(versp, "%u-%u",
1757 &sep->se_rpc_lowvers,
1758 &sep->se_rpc_highvers)) {
1762 sep->se_rpc_highvers =
1763 sep->se_rpc_lowvers;
1767 "bad RPC version specifier; %s",
1774 sep->se_rpc_lowvers =
1775 sep->se_rpc_highvers = 1;
1778 sep->se_nomapped = 0;
1779 if (strcmp(sep->se_proto, "unix") == 0) {
1780 sep->se_family = AF_UNIX;
1782 while (isdigit(sep->se_proto[strlen(sep->se_proto) - 1])) {
1784 if (sep->se_proto[strlen(sep->se_proto) - 1] == '6') {
1785 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1790 if (sep->se_proto[strlen(sep->se_proto) - 1] == '4') {
1791 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1795 /* illegal version num */
1796 syslog(LOG_ERR, "bad IP version for %s", sep->se_proto);
1801 if (v6bind && !v6bind_ok) {
1802 syslog(LOG_INFO, "IPv6 bind is ignored for %s",
1804 if (v4bind && v4bind_ok)
1812 sep->se_family = AF_INET6;
1813 if (!v4bind || !v4bind_ok)
1814 sep->se_nomapped = 1;
1817 { /* default to v4 bind if not v6 bind */
1819 syslog(LOG_NOTICE, "IPv4 bind is ignored for %s",
1824 sep->se_family = AF_INET;
1828 switch(sep->se_family) {
1830 memcpy(&sep->se_ctrladdr4, bind_sa4,
1831 sizeof(sep->se_ctrladdr4));
1832 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr4);
1836 memcpy(&sep->se_ctrladdr6, bind_sa6,
1837 sizeof(sep->se_ctrladdr6));
1838 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr6);
1842 if (strlen(sep->se_service) >= sizeof(sep->se_ctrladdr_un.sun_path)) {
1844 "domain socket pathname too long for service %s",
1848 memset(&sep->se_ctrladdr, 0, sizeof(sep->se_ctrladdr));
1849 sep->se_ctrladdr_un.sun_family = sep->se_family;
1850 sep->se_ctrladdr_un.sun_len = strlen(sep->se_service);
1851 strcpy(sep->se_ctrladdr_un.sun_path, sep->se_service);
1852 sep->se_ctrladdr_size = SUN_LEN(&sep->se_ctrladdr_un);
1855 if (!strncmp(arg, "wait", 4))
1857 else if (!strncmp(arg, "nowait", 6))
1861 "%s: bad wait/nowait for service %s",
1862 CONFIG, sep->se_service);
1865 sep->se_maxchild = -1;
1866 sep->se_maxcpm = -1;
1867 sep->se_maxperip = -1;
1868 if ((s = strchr(arg, '/')) != NULL) {
1872 val = strtoul(s + 1, &eptr, 10);
1873 if (eptr == s + 1 || val > MAX_MAXCHLD) {
1875 "%s: bad max-child for service %s",
1876 CONFIG, sep->se_service);
1880 if (!sep->se_accept && val != 1)
1881 warnx("maxchild=%lu for wait service %s"
1882 " not recommended", val, sep->se_service);
1883 sep->se_maxchild = val;
1885 sep->se_maxcpm = strtol(eptr + 1, &eptr, 10);
1887 sep->se_maxperip = strtol(eptr + 1, &eptr, 10);
1889 * explicitly do not check for \0 for future expansion /
1890 * backwards compatibility
1895 * Silently enforce "nowait" mode for TCPMUX services
1896 * since they don't have an assigned port to listen on.
1899 if (strcmp(sep->se_proto, "tcp")) {
1901 "%s: bad protocol for tcpmux service %s",
1902 CONFIG, sep->se_service);
1905 if (sep->se_socktype != SOCK_STREAM) {
1907 "%s: bad socket type for tcpmux service %s",
1908 CONFIG, sep->se_service);
1912 sep->se_user = newstr(sskip(&cp));
1914 if ((s = strrchr(sep->se_user, '/')) != NULL) {
1916 sep->se_class = newstr(s + 1);
1918 sep->se_class = newstr(RESOURCE_RC);
1920 if ((s = strrchr(sep->se_user, ':')) != NULL) {
1922 sep->se_group = newstr(s + 1);
1924 sep->se_group = NULL;
1925 sep->se_server = newstr(sskip(&cp));
1926 if ((sep->se_server_name = strrchr(sep->se_server, '/')))
1927 sep->se_server_name++;
1928 if (strcmp(sep->se_server, "internal") == 0) {
1931 for (bi = biltins; bi->bi_service; bi++)
1932 if (bi->bi_socktype == sep->se_socktype &&
1933 matchservent(bi->bi_service, sep->se_service,
1936 if (bi->bi_service == 0) {
1937 syslog(LOG_ERR, "internal service %s unknown",
1941 sep->se_accept = 1; /* force accept mode for built-ins */
1945 if (sep->se_maxperip < 0)
1946 sep->se_maxperip = maxperip;
1947 if (sep->se_maxcpm < 0)
1948 sep->se_maxcpm = maxcpm;
1949 if (sep->se_maxchild < 0) { /* apply default max-children */
1950 if (sep->se_bi && sep->se_bi->bi_maxchild >= 0)
1951 sep->se_maxchild = sep->se_bi->bi_maxchild;
1952 else if (sep->se_accept)
1953 sep->se_maxchild = maxchild > 0 ? maxchild : 0;
1955 sep->se_maxchild = 1;
1957 if (sep->se_maxchild > 0) {
1958 sep->se_pids = malloc(sep->se_maxchild * sizeof(*sep->se_pids));
1959 if (sep->se_pids == NULL) {
1960 syslog(LOG_ERR, "malloc: %m");
1965 for (arg = skip(&cp); cp; arg = skip(&cp))
1966 if (argc < MAXARGV) {
1967 sep->se_argv[argc++] = newstr(arg);
1970 "%s: too many arguments for service %s",
1971 CONFIG, sep->se_service);
1974 while (argc <= MAXARGV)
1975 sep->se_argv[argc++] = NULL;
1976 for (i = 0; i < PERIPSIZE; ++i)
1977 LIST_INIT(&sep->se_conn[i]);
1979 sep->se_policy = policy ? newstr(policy) : NULL;
1985 freeconfig(struct servtab *cp)
1990 free(cp->se_service);
2002 free(cp->se_server);
2005 for (i = 0; i < MAXARGV; i++)
2007 free(cp->se_argv[i]);
2011 free(cp->se_policy);
2017 * Safe skip - if skip returns null, log a syntax error in the
2018 * configuration file and exit.
2027 syslog(LOG_ERR, "%s: syntax error", CONFIG);
2041 while (*cp == ' ' || *cp == '\t')
2047 (void) ungetc(c, fconfig);
2048 if (c == ' ' || c == '\t')
2049 if ((cp = nextline(fconfig)))
2054 if (*cp == '"' || *cp == '\'')
2058 while (*cp && *cp != quote)
2061 while (*cp && *cp != ' ' && *cp != '\t')
2074 if (fgets(line, sizeof (line), fd) == NULL)
2076 cp = strchr(line, '\n');
2083 newstr(const char *cp)
2087 if ((cr = strdup(cp != NULL ? cp : "")))
2089 syslog(LOG_ERR, "strdup: %m");
2094 inetd_setproctitle(const char *a, int s)
2097 struct sockaddr_storage ss;
2098 char buf[80], pbuf[NI_MAXHOST];
2101 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) {
2102 getnameinfo((struct sockaddr *)&ss, size, pbuf, sizeof(pbuf),
2103 NULL, 0, NI_NUMERICHOST);
2104 (void) sprintf(buf, "%s [%s]", a, pbuf);
2106 (void) sprintf(buf, "%s", a);
2107 setproctitle("%s", buf);
2111 check_loop(const struct sockaddr *sa, const struct servtab *sep)
2113 struct servtab *se2;
2114 char pname[NI_MAXHOST];
2116 for (se2 = servtab; se2; se2 = se2->se_next) {
2117 if (!se2->se_bi || se2->se_socktype != SOCK_DGRAM)
2120 switch (se2->se_family) {
2122 if (((const struct sockaddr_in *)sa)->sin_port ==
2123 se2->se_ctrladdr4.sin_port)
2128 if (((const struct sockaddr_in6 *)sa)->sin6_port ==
2129 se2->se_ctrladdr6.sin6_port)
2137 getnameinfo(sa, sa->sa_len, pname, sizeof(pname), NULL, 0,
2139 syslog(LOG_WARNING, "%s/%s:%s/%s loop request REFUSED from %s",
2140 sep->se_service, sep->se_proto,
2141 se2->se_service, se2->se_proto,
2150 * Dump relevant information to stderr
2153 print_service(const char *action, const struct servtab *sep)
2156 "%s: %s proto=%s accept=%d max=%d user=%s group=%s"
2160 " builtin=%p server=%s"
2165 action, sep->se_service, sep->se_proto,
2166 sep->se_accept, sep->se_maxchild, sep->se_user, sep->se_group,
2170 (void *) sep->se_bi, sep->se_server
2172 , (sep->se_policy ? sep->se_policy : "")
2177 #define CPMHSIZE 256
2178 #define CPMHMASK (CPMHSIZE-1)
2182 typedef struct CTime {
2183 unsigned long ct_Ticks;
2187 typedef struct CHash {
2189 struct in_addr c4_Addr;
2190 struct in6_addr c6_Addr;
2192 #define ch_Addr4 cu_Addr.c4_Addr
2193 #define ch_Addr6 cu_Addr.c6_Addr
2197 CTime ch_Times[CHTSIZE];
2200 CHash CHashAry[CPMHSIZE];
2203 cpmip(const struct servtab *sep, int ctrl)
2205 struct sockaddr_storage rss;
2206 socklen_t rssLen = sizeof(rss);
2210 * If getpeername() fails, just let it through (if logging is
2211 * enabled the condition is caught elsewhere)
2214 if (sep->se_maxcpm > 0 &&
2215 (sep->se_family == AF_INET || sep->se_family == AF_INET6) &&
2216 getpeername(ctrl, (struct sockaddr *)&rss, &rssLen) == 0 ) {
2217 time_t t = time(NULL);
2218 int hv = 0xABC3D20F;
2221 CHash *chBest = NULL;
2222 unsigned int ticks = t / CHTGRAN;
2223 struct sockaddr_in *sin4;
2225 struct sockaddr_in6 *sin6;
2228 sin4 = (struct sockaddr_in *)&rss;
2230 sin6 = (struct sockaddr_in6 *)&rss;
2236 switch (rss.ss_family) {
2238 p = (char *)&sin4->sin_addr;
2239 addrlen = sizeof(struct in_addr);
2243 p = (char *)&sin6->sin6_addr;
2244 addrlen = sizeof(struct in6_addr);
2248 /* should not happen */
2252 for (i = 0; i < addrlen; ++i, ++p) {
2253 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2255 hv = (hv ^ (hv >> 16));
2257 for (i = 0; i < 5; ++i) {
2258 CHash *ch = &CHashAry[(hv + i) & CPMHMASK];
2260 if (rss.ss_family == AF_INET &&
2261 ch->ch_Family == AF_INET &&
2262 sin4->sin_addr.s_addr == ch->ch_Addr4.s_addr &&
2263 ch->ch_Service && strcmp(sep->se_service,
2264 ch->ch_Service) == 0) {
2269 if (rss.ss_family == AF_INET6 &&
2270 ch->ch_Family == AF_INET6 &&
2271 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2272 &ch->ch_Addr6) != 0 &&
2273 ch->ch_Service && strcmp(sep->se_service,
2274 ch->ch_Service) == 0) {
2279 if (chBest == NULL || ch->ch_LTime == 0 ||
2280 ch->ch_LTime < chBest->ch_LTime) {
2284 if ((rss.ss_family == AF_INET &&
2285 (chBest->ch_Family != AF_INET ||
2286 sin4->sin_addr.s_addr != chBest->ch_Addr4.s_addr)) ||
2287 chBest->ch_Service == NULL ||
2288 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2289 chBest->ch_Family = sin4->sin_family;
2290 chBest->ch_Addr4 = sin4->sin_addr;
2291 if (chBest->ch_Service)
2292 free(chBest->ch_Service);
2293 chBest->ch_Service = strdup(sep->se_service);
2294 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2297 if ((rss.ss_family == AF_INET6 &&
2298 (chBest->ch_Family != AF_INET6 ||
2299 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2300 &chBest->ch_Addr6) == 0)) ||
2301 chBest->ch_Service == NULL ||
2302 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2303 chBest->ch_Family = sin6->sin6_family;
2304 chBest->ch_Addr6 = sin6->sin6_addr;
2305 if (chBest->ch_Service)
2306 free(chBest->ch_Service);
2307 chBest->ch_Service = strdup(sep->se_service);
2308 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2311 chBest->ch_LTime = t;
2313 CTime *ct = &chBest->ch_Times[ticks % CHTSIZE];
2314 if (ct->ct_Ticks != ticks) {
2315 ct->ct_Ticks = ticks;
2320 for (i = 0; i < CHTSIZE; ++i) {
2321 CTime *ct = &chBest->ch_Times[i];
2322 if (ct->ct_Ticks <= ticks &&
2323 ct->ct_Ticks >= ticks - CHTSIZE) {
2324 cnt += ct->ct_Count;
2327 if ((cnt * 60) / (CHTSIZE * CHTGRAN) > sep->se_maxcpm) {
2328 char pname[NI_MAXHOST];
2330 getnameinfo((struct sockaddr *)&rss,
2331 ((struct sockaddr *)&rss)->sa_len,
2332 pname, sizeof(pname), NULL, 0,
2336 "%s from %s exceeded counts/min (limit %d/min)",
2337 sep->se_service, pname,
2344 static struct conninfo *
2345 search_conn(struct servtab *sep, int ctrl)
2347 struct sockaddr_storage ss;
2348 socklen_t sslen = sizeof(ss);
2349 struct conninfo *conn;
2351 char pname[NI_MAXHOST], pname2[NI_MAXHOST];
2353 if (sep->se_maxperip <= 0)
2357 * If getpeername() fails, just let it through (if logging is
2358 * enabled the condition is caught elsewhere)
2360 if (getpeername(ctrl, (struct sockaddr *)&ss, &sslen) != 0)
2363 switch (ss.ss_family) {
2365 hv = hashval((char *)&((struct sockaddr_in *)&ss)->sin_addr,
2366 sizeof(struct in_addr));
2370 hv = hashval((char *)&((struct sockaddr_in6 *)&ss)->sin6_addr,
2371 sizeof(struct in6_addr));
2376 * Since we only support AF_INET and AF_INET6, just
2377 * let other than AF_INET and AF_INET6 through.
2382 if (getnameinfo((struct sockaddr *)&ss, sslen, pname, sizeof(pname),
2383 NULL, 0, NI_NUMERICHOST) != 0)
2386 LIST_FOREACH(conn, &sep->se_conn[hv], co_link) {
2387 if (getnameinfo((struct sockaddr *)&conn->co_addr,
2388 conn->co_addr.ss_len, pname2, sizeof(pname2), NULL, 0,
2389 NI_NUMERICHOST) == 0 &&
2390 strcmp(pname, pname2) == 0)
2395 if ((conn = malloc(sizeof(struct conninfo))) == NULL) {
2396 syslog(LOG_ERR, "malloc: %m");
2399 conn->co_proc = malloc(sep->se_maxperip * sizeof(*conn->co_proc));
2400 if (conn->co_proc == NULL) {
2401 syslog(LOG_ERR, "malloc: %m");
2404 memcpy(&conn->co_addr, (struct sockaddr *)&ss, sslen);
2405 conn->co_numchild = 0;
2406 LIST_INSERT_HEAD(&sep->se_conn[hv], conn, co_link);
2410 * Since a child process is not invoked yet, we cannot
2411 * determine a pid of a child. So, co_proc and co_numchild
2412 * should be filled leter.
2419 room_conn(struct servtab *sep, struct conninfo *conn)
2421 char pname[NI_MAXHOST];
2423 if (conn->co_numchild >= sep->se_maxperip) {
2424 getnameinfo((struct sockaddr *)&conn->co_addr,
2425 conn->co_addr.ss_len, pname, sizeof(pname), NULL, 0,
2427 syslog(LOG_ERR, "%s from %s exceeded counts (limit %d)",
2428 sep->se_service, pname, sep->se_maxperip);
2435 addchild_conn(struct conninfo *conn, pid_t pid)
2437 struct procinfo *proc;
2442 if ((proc = search_proc(pid, 1)) != NULL) {
2443 if (proc->pr_conn != NULL) {
2445 "addchild_conn: child already on process list");
2448 proc->pr_conn = conn;
2451 conn->co_proc[conn->co_numchild++] = proc;
2455 reapchild_conn(pid_t pid)
2457 struct procinfo *proc;
2458 struct conninfo *conn;
2461 if ((proc = search_proc(pid, 0)) == NULL)
2463 if ((conn = proc->pr_conn) == NULL)
2465 for (i = 0; i < conn->co_numchild; ++i)
2466 if (conn->co_proc[i] == proc) {
2467 conn->co_proc[i] = conn->co_proc[--conn->co_numchild];
2475 resize_conn(struct servtab *sep, int maxpip)
2477 struct conninfo *conn;
2480 if (sep->se_maxperip <= 0)
2486 for (i = 0; i < PERIPSIZE; ++i) {
2487 LIST_FOREACH(conn, &sep->se_conn[i], co_link) {
2488 for (j = maxpip; j < conn->co_numchild; ++j)
2489 free_proc(conn->co_proc[j]);
2490 conn->co_proc = realloc(conn->co_proc,
2491 maxpip * sizeof(*conn->co_proc));
2492 if (conn->co_proc == NULL) {
2493 syslog(LOG_ERR, "realloc: %m");
2496 if (conn->co_numchild > maxpip)
2497 conn->co_numchild = maxpip;
2503 free_connlist(struct servtab *sep)
2505 struct conninfo *conn;
2508 for (i = 0; i < PERIPSIZE; ++i) {
2509 while ((conn = LIST_FIRST(&sep->se_conn[i])) != NULL) {
2510 for (j = 0; j < conn->co_numchild; ++j)
2511 free_proc(conn->co_proc[j]);
2512 conn->co_numchild = 0;
2519 free_conn(struct conninfo *conn)
2523 if (conn->co_numchild <= 0) {
2524 LIST_REMOVE(conn, co_link);
2525 free(conn->co_proc);
2530 static struct procinfo *
2531 search_proc(pid_t pid, int add)
2533 struct procinfo *proc;
2536 hv = hashval((char *)&pid, sizeof(pid));
2537 LIST_FOREACH(proc, &proctable[hv], pr_link) {
2538 if (proc->pr_pid == pid)
2541 if (proc == NULL && add) {
2542 if ((proc = malloc(sizeof(struct procinfo))) == NULL) {
2543 syslog(LOG_ERR, "malloc: %m");
2547 proc->pr_conn = NULL;
2548 LIST_INSERT_HEAD(&proctable[hv], proc, pr_link);
2554 free_proc(struct procinfo *proc)
2558 LIST_REMOVE(proc, pr_link);
2563 hashval(char *p, int len)
2565 int i, hv = 0xABC3D20F;
2567 for (i = 0; i < len; ++i, ++p)
2568 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2569 hv = (hv ^ (hv >> 16)) & (PERIPSIZE - 1);