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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1983, 1991, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)from: inetd.c 8.4 (Berkeley) 4/13/94
35 * $FreeBSD: src/usr.sbin/inetd/inetd.c,v 1.80.2.11 2003/04/05 13:39:18 dwmalone Exp $
36 * $DragonFly: src/usr.sbin/inetd/inetd.c,v 1.2 2003/06/17 04:29:55 dillon Exp $
40 * Inetd - Internet super-server
42 * This program invokes all internet services as needed. Connection-oriented
43 * services are invoked each time a connection is made, by creating a process.
44 * This process is passed the connection as file descriptor 0 and is expected
45 * to do a getpeername to find out the source host and port.
47 * Datagram oriented services are invoked when a datagram
48 * arrives; a process is created and passed a pending message
49 * on file descriptor 0. Datagram servers may either connect
50 * to their peer, freeing up the original socket for inetd
51 * to receive further messages on, or ``take over the socket'',
52 * processing all arriving datagrams and, eventually, timing
53 * out. The first type of server is said to be ``multi-threaded'';
54 * the second type of server ``single-threaded''.
56 * Inetd uses a configuration file which is read at startup
57 * and, possibly, at some later time in response to a hangup signal.
58 * The configuration file is ``free format'' with fields given in the
59 * order shown below. Continuation lines for an entry must begin with
60 * a space or tab. All fields must be present in each entry.
62 * service name must be in /etc/services
63 * or name a tcpmux service
64 * or specify a unix domain socket
65 * socket type stream/dgram/raw/rdm/seqpacket
66 * protocol tcp[4][6][/faith,ttcp], udp[4][6], unix
67 * wait/nowait single-threaded/multi-threaded
68 * user user to run daemon as
69 * server program full path name
70 * server program arguments maximum of MAXARGS (20)
72 * TCP services without official port numbers are handled with the
73 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for
74 * requests. When a connection is made from a foreign host, the service
75 * requested is passed to tcpmux, which looks it up in the servtab list
76 * and returns the proper entry for the service. Tcpmux returns a
77 * negative reply if the service doesn't exist, otherwise the invoked
78 * server is expected to return the positive reply if the service type in
79 * inetd.conf file has the prefix "tcpmux/". If the service type has the
80 * prefix "tcpmux/+", tcpmux will return the positive reply for the
81 * process; this is for compatibility with older server code, and also
82 * allows you to invoke programs that use stdin/stdout without putting any
83 * special server code in them. Services that use tcpmux are "nowait"
84 * because they do not have a well-known port and hence cannot listen
88 * service name/version must be in /etc/rpc
89 * socket type stream/dgram/raw/rdm/seqpacket
90 * protocol rpc/tcp, rpc/udp
91 * wait/nowait single-threaded/multi-threaded
92 * user user to run daemon as
93 * server program full path name
94 * server program arguments maximum of MAXARGS
96 * Comment lines are indicated by a `#' in column 1.
99 * Comment lines that start with "#@" denote IPsec policy string, as described
100 * in ipsec_set_policy(3). This will affect all the following items in
101 * inetd.conf(8). To reset the policy, just use "#@" line. By default,
102 * there's no IPsec policy.
105 #include <sys/param.h>
106 #include <sys/ioctl.h>
107 #include <sys/wait.h>
108 #include <sys/time.h>
109 #include <sys/resource.h>
110 #include <sys/stat.h>
113 #include <netinet/in.h>
114 #include <netinet/tcp.h>
115 #include <arpa/inet.h>
117 #include <rpc/pmap_clnt.h>
133 #include <sysexits.h>
137 #include "pathnames.h"
140 #include <netinet6/ipsec.h>
141 #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
146 /* wrapper for KAME-special getnameinfo() */
147 #ifndef NI_WITHSCOPEID
148 #define NI_WITHSCOPEID 0
151 #ifndef LIBWRAP_ALLOW_FACILITY
152 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
154 #ifndef LIBWRAP_ALLOW_SEVERITY
155 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
157 #ifndef LIBWRAP_DENY_FACILITY
158 # define LIBWRAP_DENY_FACILITY LOG_AUTH
160 #ifndef LIBWRAP_DENY_SEVERITY
161 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
164 #define ISWRAP(sep) \
165 ( ((wrap_ex && !(sep)->se_bi) || (wrap_bi && (sep)->se_bi)) \
166 && (sep->se_family == AF_INET || sep->se_family == AF_INET6) \
167 && ( ((sep)->se_accept && (sep)->se_socktype == SOCK_STREAM) \
168 || (sep)->se_socktype == SOCK_DGRAM))
171 #include <login_cap.h>
174 #define RESOURCE_RC "daemon"
179 #define MAXCHILD -1 /* maximum number of this service
184 #define MAXCPM -1 /* rate limit invocations from a
185 single remote address,
190 #define MAXPERIP -1 /* maximum number of this service
191 from a single remote address,
196 #define TOOMANY 256 /* don't start more than TOOMANY */
198 #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
199 #define RETRYTIME (60*10) /* retry after bind or server fail */
200 #define MAX_MAXCHLD 32767 /* max allowable max children */
202 #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
204 void close_sep __P((struct servtab *));
205 void flag_signal __P((int));
206 void flag_config __P((int));
207 void config __P((void));
208 int cpmip __P((const struct servtab *, int));
209 void endconfig __P((void));
210 struct servtab *enter __P((struct servtab *));
211 void freeconfig __P((struct servtab *));
212 struct servtab *getconfigent __P((void));
213 int matchservent __P((const char *, const char *, const char *));
214 char *nextline __P((FILE *));
215 void addchild __P((struct servtab *, int));
216 void flag_reapchild __P((int));
217 void reapchild __P((void));
218 void enable __P((struct servtab *));
219 void disable __P((struct servtab *));
220 void flag_retry __P((int));
221 void retry __P((void));
222 int setconfig __P((void));
223 void setup __P((struct servtab *));
225 void ipsecsetup __P((struct servtab *));
227 void unregisterrpc __P((register struct servtab *sep));
228 static struct conninfo *search_conn __P((struct servtab *sep, int ctrl));
229 static int room_conn __P((struct servtab *sep, struct conninfo *conn));
230 static void addchild_conn __P((struct conninfo *conn, pid_t pid));
231 static void reapchild_conn __P((pid_t pid));
232 static void free_conn __P((struct conninfo *conn));
233 static void resize_conn __P((struct servtab *sep, int maxperip));
234 static void free_connlist __P((struct servtab *sep));
235 static void free_proc __P((struct procinfo *));
236 static struct procinfo *search_proc __P((pid_t pid, int add));
237 static int hashval __P((char *p, int len));
245 int maxsock; /* highest-numbered descriptor */
249 int toomany = TOOMANY;
250 int maxchild = MAXCHILD;
252 int maxperip = MAXPERIP;
255 char *hostname = NULL;
256 struct sockaddr_in *bind_sa4;
259 struct sockaddr_in6 *bind_sa6;
270 struct servtab *servtab;
272 extern struct biltin biltins[];
274 #define NUMINT (sizeof(intab) / sizeof(struct inent))
275 const char *CONFIG = _PATH_INETDCONF;
276 const char *pid_file = _PATH_INETDPID;
278 static LIST_HEAD(, procinfo) proctable[PERIPSIZE];
281 getvalue(arg, value, whine)
282 const char *arg, *whine;
288 tmp = strtol(arg, &p, 0);
290 syslog(LOG_ERR, whine, arg);
291 return 1; /* failure */
294 return 0; /* success */
298 whichaf(struct request_info *req)
302 sa = (struct sockaddr *)req->client->sin;
305 if (sa->sa_family == AF_INET6 &&
306 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr))
308 return sa->sa_family;
319 struct sigaction sa, saalrm, sachld, sahup, sapipe;
320 int tmpint, ch, dofork;
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.
399 servname = (hostname == NULL) ? "discard" /* dummy */ : NULL;
401 bzero(&hints, sizeof(struct addrinfo));
402 hints.ai_flags = AI_PASSIVE;
403 hints.ai_family = AF_UNSPEC;
404 error = getaddrinfo(hostname, servname, &hints, &res);
406 syslog(LOG_ERR, "-a %s: %s", hostname, gai_strerror(error));
407 if (error == EAI_SYSTEM)
408 syslog(LOG_ERR, "%s", strerror(errno));
412 if (res->ai_addr == NULL) {
413 syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
416 switch (res->ai_addr->sa_family) {
420 bind_sa4 = (struct sockaddr_in *)res->ai_addr;
421 /* init port num in case servname is dummy */
422 bind_sa4->sin_port = 0;
429 bind_sa6 = (struct sockaddr_in6 *)res->ai_addr;
430 /* init port num in case servname is dummy */
431 bind_sa6->sin6_port = 0;
442 } while ((res = res->ai_next) != NULL);
448 syslog(LOG_ERR, "-a %s: unknown address family", hostname);
454 umask(mask = umask(0777));
463 if (daemon(0, 0) < 0) {
464 syslog(LOG_WARNING, "daemon(0,0) failed: %m");
466 /* From now on we don't want syslog messages going to stderr. */
468 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
470 * In case somebody has started inetd manually, we need to
471 * clear the logname, so that old servers run as root do not
472 * get the user's logname..
474 if (setlogin("") < 0) {
475 syslog(LOG_WARNING, "cannot clear logname: %m");
476 /* no big deal if it fails.. */
479 fp = fopen(pid_file, "w");
481 fprintf(fp, "%ld\n", (long)pid);
484 syslog(LOG_WARNING, "%s: %m", pid_file);
487 for (i = 0; i < PERIPSIZE; ++i)
488 LIST_INIT(&proctable[i]);
490 sigemptyset(&sa.sa_mask);
491 sigaddset(&sa.sa_mask, SIGALRM);
492 sigaddset(&sa.sa_mask, SIGCHLD);
493 sigaddset(&sa.sa_mask, SIGHUP);
494 sa.sa_handler = flag_retry;
495 sigaction(SIGALRM, &sa, &saalrm);
497 sa.sa_handler = flag_config;
498 sigaction(SIGHUP, &sa, &sahup);
499 sa.sa_handler = flag_reapchild;
500 sigaction(SIGCHLD, &sa, &sachld);
501 sa.sa_handler = SIG_IGN;
502 sigaction(SIGPIPE, &sa, &sapipe);
505 /* space for daemons to overwrite environment for ps */
506 #define DUMMYSIZE 100
507 char dummy[DUMMYSIZE];
509 (void)memset(dummy, 'x', DUMMYSIZE - 1);
510 dummy[DUMMYSIZE - 1] = '\0';
511 (void)setenv("inetd_dummy", dummy, 1);
514 if (pipe(signalpipe) != 0) {
515 syslog(LOG_ERR, "pipe: %m");
518 if (fcntl(signalpipe[0], F_SETFD, FD_CLOEXEC) < 0 ||
519 fcntl(signalpipe[1], F_SETFD, FD_CLOEXEC) < 0) {
520 syslog(LOG_ERR, "signalpipe: fcntl (F_SETFD, FD_CLOEXEC): %m");
523 FD_SET(signalpipe[0], &allsock);
527 if (signalpipe[0] > maxsock)
528 maxsock = signalpipe[0];
529 if (signalpipe[1] > maxsock)
530 maxsock = signalpipe[1];
538 syslog(LOG_ERR, "%s: nsock=0", __FUNCTION__);
543 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
544 (fd_set *)0, (struct timeval *)0)) <= 0) {
545 if (n < 0 && errno != EINTR) {
546 syslog(LOG_WARNING, "select: %m");
551 /* handle any queued signal flags */
552 if (FD_ISSET(signalpipe[0], &readable)) {
554 if (ioctl(signalpipe[0], FIONREAD, &nsig) != 0) {
555 syslog(LOG_ERR, "ioctl: %m");
558 while (--nsig >= 0) {
560 if (read(signalpipe[0], &c, 1) != 1) {
561 syslog(LOG_ERR, "read: %m");
565 warnx("handling signal flag %c", c);
567 case 'A': /* sigalrm */
570 case 'C': /* sigchld */
573 case 'H': /* sighup */
579 for (sep = servtab; n && sep; sep = sep->se_next)
580 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
583 warnx("someone wants %s", sep->se_service);
584 dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep);
586 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) {
588 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
589 syslog(LOG_ERR, "ioctl (FIONBIO, 1): %m");
590 ctrl = accept(sep->se_fd, (struct sockaddr *)0,
593 warnx("accept, ctrl %d", ctrl);
597 "accept (for %s): %m",
599 if (sep->se_accept &&
600 sep->se_socktype == SOCK_STREAM)
605 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
606 syslog(LOG_ERR, "ioctl1(FIONBIO, 0): %m");
607 if (ioctl(ctrl, FIONBIO, &i) < 0)
608 syslog(LOG_ERR, "ioctl2(FIONBIO, 0): %m");
609 if (cpmip(sep, ctrl) < 0) {
614 (conn = search_conn(sep, ctrl)) != NULL &&
615 !room_conn(sep, conn)) {
621 if (log && !ISWRAP(sep)) {
622 char pname[INET6_ADDRSTRLEN] = "unknown";
625 if (getpeername(ctrl, (struct sockaddr *)
628 if (recvfrom(ctrl, buf, sizeof(buf),
630 (struct sockaddr *)&peermax,
632 getnameinfo((struct sockaddr *)&peermax,
634 pname, sizeof(pname),
640 getnameinfo((struct sockaddr *)&peermax,
642 pname, sizeof(pname),
647 syslog(LOG_INFO,"%s from %s", sep->se_service, pname);
649 (void) sigblock(SIGBLOCK);
652 * Fork for all external services, builtins which need to
653 * fork and anything we're wrapping (as wrapping might
654 * block or use hosts_options(5) twist).
657 if (sep->se_count++ == 0)
658 (void)gettimeofday(&sep->se_time, (struct timezone *)NULL);
659 else if (toomany > 0 && sep->se_count >= toomany) {
662 (void)gettimeofday(&now, (struct timezone *)NULL);
663 if (now.tv_sec - sep->se_time.tv_sec >
669 "%s/%s server failing (looping), service terminated",
670 sep->se_service, sep->se_proto);
671 if (sep->se_accept &&
672 sep->se_socktype == SOCK_STREAM)
687 syslog(LOG_ERR, "fork: %m");
688 if (sep->se_accept &&
689 sep->se_socktype == SOCK_STREAM)
697 addchild_conn(conn, pid);
703 sigaction(SIGALRM, &saalrm, (struct sigaction *)0);
704 sigaction(SIGCHLD, &sachld, (struct sigaction *)0);
705 sigaction(SIGHUP, &sahup, (struct sigaction *)0);
706 /* SIGPIPE reset before exec */
709 * Call tcpmux to find the real service to exec.
712 sep->se_bi->bi_fn == (bi_fn_t *) tcpmux) {
720 inetd_setproctitle("wrapping", ctrl);
721 service = sep->se_server_name ?
722 sep->se_server_name : sep->se_service;
723 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, NULL);
725 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
726 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
727 denied = !hosts_access(&req);
729 syslog(deny_severity,
730 "refused connection from %.500s, service %s (%s%s)",
731 eval_client(&req), service, sep->se_proto,
732 (whichaf(&req) == AF_INET6) ? "6" : "");
733 if (sep->se_socktype != SOCK_STREAM)
734 recv(ctrl, buf, sizeof (buf), 0);
741 syslog(allow_severity,
742 "connection from %.500s, service %s (%s%s)",
743 eval_client(&req), service, sep->se_proto,
744 (whichaf(&req) == AF_INET6) ? "6" : "");
748 (*sep->se_bi->bi_fn)(ctrl, sep);
752 getpid(), sep->se_server);
753 /* Clear close-on-exec. */
754 if (fcntl(ctrl, F_SETFD, 0) < 0) {
756 "%s/%s: fcntl (F_SETFD, 0): %m",
757 sep->se_service, sep->se_proto);
766 if ((pwd = getpwnam(sep->se_user)) == NULL) {
768 "%s/%s: %s: no such user",
769 sep->se_service, sep->se_proto,
771 if (sep->se_socktype != SOCK_STREAM)
772 recv(0, buf, sizeof (buf), 0);
776 if ( sep->se_group != NULL
777 && (grp = getgrnam(sep->se_group)) == NULL
780 "%s/%s: %s: no such group",
781 sep->se_service, sep->se_proto,
783 if (sep->se_socktype != SOCK_STREAM)
784 recv(0, buf, sizeof (buf), 0);
788 pwd->pw_gid = grp->gr_gid;
790 if ((lc = login_getclass(sep->se_class)) == NULL) {
791 /* error syslogged by getclass */
793 "%s/%s: %s: login class error",
794 sep->se_service, sep->se_proto,
796 if (sep->se_socktype != SOCK_STREAM)
797 recv(0, buf, sizeof (buf), 0);
803 "%s: can't setsid(): %m",
805 /* _exit(EX_OSERR); not fatal yet */
808 if (setusercontext(lc, pwd, pwd->pw_uid,
809 LOGIN_SETALL) != 0) {
811 "%s: can't setusercontext(..%s..): %m",
812 sep->se_service, sep->se_user);
818 if (setlogin(sep->se_user) < 0) {
820 "%s: can't setlogin(%s): %m",
821 sep->se_service, sep->se_user);
822 /* _exit(EX_OSERR); not yet */
824 if (setgid(pwd->pw_gid) < 0) {
826 "%s: can't set gid %d: %m",
827 sep->se_service, pwd->pw_gid);
830 (void) initgroups(pwd->pw_name,
832 if (setuid(pwd->pw_uid) < 0) {
834 "%s: can't set uid %d: %m",
835 sep->se_service, pwd->pw_uid);
840 sigaction(SIGPIPE, &sapipe,
841 (struct sigaction *)0);
842 execv(sep->se_server, sep->se_argv);
844 "cannot execute %s: %m", sep->se_server);
845 if (sep->se_socktype != SOCK_STREAM)
846 recv(0, buf, sizeof (buf), 0);
851 if (sep->se_accept && sep->se_socktype == SOCK_STREAM)
858 * Add a signal flag to the signal flag queue for later handling
867 if (write(signalpipe[1], &ch, 1) != 1) {
868 syslog(LOG_ERR, "write: %m");
874 * Record a new child pid for this service. If we've reached the
875 * limit on children, then stop accepting incoming requests.
879 addchild(struct servtab *sep, pid_t pid)
881 if (sep->se_maxchild <= 0)
884 if (sep->se_numchild >= sep->se_maxchild) {
885 syslog(LOG_ERR, "%s: %d >= %d",
886 __FUNCTION__, sep->se_numchild, sep->se_maxchild);
890 sep->se_pids[sep->se_numchild++] = pid;
891 if (sep->se_numchild == sep->se_maxchild)
896 * Some child process has exited. See if it's on somebody's list.
900 flag_reapchild(signo)
914 pid = wait3(&status, WNOHANG, (struct rusage *)0);
918 warnx("%d reaped, %s %u", pid,
919 WIFEXITED(status) ? "status" : "signal",
920 WIFEXITED(status) ? WEXITSTATUS(status)
922 for (sep = servtab; sep; sep = sep->se_next) {
923 for (k = 0; k < sep->se_numchild; k++)
924 if (sep->se_pids[k] == pid)
926 if (k == sep->se_numchild)
928 if (sep->se_numchild == sep->se_maxchild)
930 sep->se_pids[k] = sep->se_pids[--sep->se_numchild];
931 if (WIFSIGNALED(status) || WEXITSTATUS(status))
933 "%s[%d]: exited, %s %u",
935 WIFEXITED(status) ? "status" : "signal",
936 WIFEXITED(status) ? WEXITSTATUS(status)
954 struct servtab *sep, *new, **sepp;
957 login_cap_t *lc = NULL;
962 syslog(LOG_ERR, "%s: %m", CONFIG);
965 for (sep = servtab; sep; sep = sep->se_next)
967 while ((new = getconfigent())) {
968 if (getpwnam(new->se_user) == NULL) {
970 "%s/%s: no such user '%s', service ignored",
971 new->se_service, new->se_proto, new->se_user);
974 if (new->se_group && getgrnam(new->se_group) == NULL) {
976 "%s/%s: no such group '%s', service ignored",
977 new->se_service, new->se_proto, new->se_group);
981 if ((lc = login_getclass(new->se_class)) == NULL) {
982 /* error syslogged by getclass */
984 "%s/%s: %s: login class error, service ignored",
985 new->se_service, new->se_proto, new->se_class);
990 for (sep = servtab; sep; sep = sep->se_next)
991 if (strcmp(sep->se_service, new->se_service) == 0 &&
992 strcmp(sep->se_proto, new->se_proto) == 0 &&
993 sep->se_socktype == new->se_socktype &&
994 sep->se_family == new->se_family)
999 #define SWAP(a, b) { typeof(a) c = a; a = b; b = c; }
1000 omask = sigblock(SIGBLOCK);
1001 if (sep->se_nomapped != new->se_nomapped) {
1002 sep->se_nomapped = new->se_nomapped;
1005 /* copy over outstanding child pids */
1006 if (sep->se_maxchild > 0 && new->se_maxchild > 0) {
1007 new->se_numchild = sep->se_numchild;
1008 if (new->se_numchild > new->se_maxchild)
1009 new->se_numchild = new->se_maxchild;
1010 memcpy(new->se_pids, sep->se_pids,
1011 new->se_numchild * sizeof(*new->se_pids));
1013 SWAP(sep->se_pids, new->se_pids);
1014 sep->se_maxchild = new->se_maxchild;
1015 sep->se_numchild = new->se_numchild;
1016 sep->se_maxcpm = new->se_maxcpm;
1017 resize_conn(sep, new->se_maxperip);
1018 sep->se_maxperip = new->se_maxperip;
1019 sep->se_bi = new->se_bi;
1020 /* might need to turn on or off service now */
1021 if (sep->se_fd >= 0) {
1022 if (sep->se_maxchild > 0
1023 && sep->se_numchild == sep->se_maxchild) {
1024 if (FD_ISSET(sep->se_fd, &allsock))
1027 if (!FD_ISSET(sep->se_fd, &allsock))
1031 sep->se_accept = new->se_accept;
1032 SWAP(sep->se_user, new->se_user);
1033 SWAP(sep->se_group, new->se_group);
1035 SWAP(sep->se_class, new->se_class);
1037 SWAP(sep->se_server, new->se_server);
1038 SWAP(sep->se_server_name, new->se_server_name);
1039 for (i = 0; i < MAXARGV; i++)
1040 SWAP(sep->se_argv[i], new->se_argv[i]);
1042 SWAP(sep->se_policy, new->se_policy);
1048 print_service("REDO", sep);
1052 print_service("ADD ", sep);
1054 sep->se_checked = 1;
1059 switch (sep->se_family) {
1061 if (no_v4bind != 0) {
1068 if (no_v6bind != 0) {
1076 if (sep->se_family != AF_UNIX) {
1077 sp = getservbyname(sep->se_service, sep->se_proto);
1079 syslog(LOG_ERR, "%s/%s: unknown service",
1080 sep->se_service, sep->se_proto);
1081 sep->se_checked = 0;
1085 switch (sep->se_family) {
1087 if (sp->s_port != sep->se_ctrladdr4.sin_port) {
1088 sep->se_ctrladdr4.sin_port =
1096 sep->se_ctrladdr6.sin6_port) {
1097 sep->se_ctrladdr6.sin6_port =
1104 if (sep->se_reset != 0 && sep->se_fd >= 0)
1107 rpc = getrpcbyname(sep->se_service);
1109 syslog(LOG_ERR, "%s/%s unknown RPC service",
1110 sep->se_service, sep->se_proto);
1111 if (sep->se_fd != -1)
1112 (void) close(sep->se_fd);
1116 if (rpc->r_number != sep->se_rpc_prog) {
1117 if (sep->se_rpc_prog)
1119 sep->se_rpc_prog = rpc->r_number;
1120 if (sep->se_fd != -1)
1121 (void) close(sep->se_fd);
1125 if (sep->se_fd == -1)
1130 * Purge anything not looked at above.
1132 omask = sigblock(SIGBLOCK);
1134 while ((sep = *sepp)) {
1135 if (sep->se_checked) {
1136 sepp = &sep->se_next;
1139 *sepp = sep->se_next;
1140 if (sep->se_fd >= 0)
1143 print_service("FREE", sep);
1144 if (sep->se_rpc && sep->se_rpc_prog > 0)
1149 (void) sigsetmask(omask);
1154 struct servtab *sep;
1157 struct servtab *sepp;
1160 omask = sigblock(SIGBLOCK);
1161 for (sepp = servtab; sepp; sepp = sepp->se_next) {
1164 if (sep->se_checked == 0 ||
1166 sep->se_rpc_prog != sepp->se_rpc_prog)
1171 print_service("UNREG", sep);
1172 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++)
1173 pmap_unset(sep->se_rpc_prog, i);
1174 if (sep->se_fd != -1)
1175 (void) close(sep->se_fd);
1177 (void) sigsetmask(omask);
1190 struct servtab *sep;
1193 for (sep = servtab; sep; sep = sep->se_next)
1194 if (sep->se_fd == -1 && !ISMUX(sep))
1200 struct servtab *sep;
1204 if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
1206 warn("socket failed on %s/%s",
1207 sep->se_service, sep->se_proto);
1208 syslog(LOG_ERR, "%s/%s: socket: %m",
1209 sep->se_service, sep->se_proto);
1212 /* Set all listening sockets to close-on-exec. */
1213 if (fcntl(sep->se_fd, F_SETFD, FD_CLOEXEC) < 0) {
1214 syslog(LOG_ERR, "%s/%s: fcntl (F_SETFD, FD_CLOEXEC): %m",
1215 sep->se_service, sep->se_proto);
1219 #define turnon(fd, opt) \
1220 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
1221 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
1222 turnon(sep->se_fd, SO_DEBUG) < 0)
1223 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
1224 if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
1225 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
1227 if (turnon(sep->se_fd, SO_PRIVSTATE) < 0)
1228 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m");
1230 /* tftpd opens a new connection then needs more infos */
1231 if ((sep->se_family == AF_INET6) &&
1232 (strcmp(sep->se_proto, "udp") == 0) &&
1233 (sep->se_accept == 0) &&
1234 (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_PKTINFO,
1235 (char *)&on, sizeof (on)) < 0))
1236 syslog(LOG_ERR, "setsockopt (IPV6_RECVPKTINFO): %m");
1237 if (sep->se_family == AF_INET6) {
1238 int flag = sep->se_nomapped ? 1 : 0;
1239 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_V6ONLY,
1240 (char *)&flag, sizeof (flag)) < 0)
1241 syslog(LOG_ERR, "setsockopt (IPV6_V6ONLY): %m");
1244 if (sep->se_type == TTCP_TYPE)
1245 if (setsockopt(sep->se_fd, IPPROTO_TCP, TCP_NOPUSH,
1246 (char *)&on, sizeof (on)) < 0)
1247 syslog(LOG_ERR, "setsockopt (TCP_NOPUSH): %m");
1249 if (sep->se_type == FAITH_TYPE) {
1250 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_FAITH, &on,
1252 syslog(LOG_ERR, "setsockopt (IPV6_FAITH): %m");
1259 if (sep->se_family == AF_UNIX) {
1260 (void) unlink(sep->se_ctrladdr_un.sun_path);
1261 umask(0777); /* Make socket with conservative permissions */
1263 if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr,
1264 sep->se_ctrladdr_size) < 0) {
1266 warn("bind failed on %s/%s",
1267 sep->se_service, sep->se_proto);
1268 syslog(LOG_ERR, "%s/%s: bind: %m",
1269 sep->se_service, sep->se_proto);
1270 (void) close(sep->se_fd);
1276 if (sep->se_family == AF_UNIX)
1280 if (sep->se_family == AF_UNIX) {
1281 /* Ick - fch{own,mod} don't work on Unix domain sockets */
1282 if (chown(sep->se_service, sep->se_sockuid, sep->se_sockgid) < 0)
1283 syslog(LOG_ERR, "chown socket: %m");
1284 if (chmod(sep->se_service, sep->se_sockmode) < 0)
1285 syslog(LOG_ERR, "chmod socket: %m");
1290 socklen_t len = sep->se_ctrladdr_size;
1292 if (sep->se_family != AF_INET) {
1294 "%s/%s: unsupported address family for rpc",
1295 sep->se_service, sep->se_proto);
1296 (void) close(sep->se_fd);
1300 if (getsockname(sep->se_fd,
1301 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){
1302 syslog(LOG_ERR, "%s/%s: getsockname: %m",
1303 sep->se_service, sep->se_proto);
1304 (void) close(sep->se_fd);
1309 print_service("REG ", sep);
1310 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1311 pmap_unset(sep->se_rpc_prog, i);
1312 pmap_set(sep->se_rpc_prog, i,
1313 (sep->se_socktype == SOCK_DGRAM)
1314 ? IPPROTO_UDP : IPPROTO_TCP,
1315 ntohs(sep->se_ctrladdr4.sin_port));
1318 if (sep->se_socktype == SOCK_STREAM)
1319 listen(sep->se_fd, 64);
1322 warnx("registered %s on %d",
1323 sep->se_server, sep->se_fd);
1330 struct servtab *sep;
1333 char *policy_in = NULL;
1334 char *policy_out = NULL;
1338 switch (sep->se_family) {
1341 opt = IP_IPSEC_POLICY;
1345 level = IPPROTO_IPV6;
1346 opt = IPV6_IPSEC_POLICY;
1353 if (!sep->se_policy || sep->se_policy[0] == '\0') {
1354 static char def_in[] = "in entrust", def_out[] = "out entrust";
1356 policy_out = def_out;
1358 if (!strncmp("in", sep->se_policy, 2))
1359 policy_in = sep->se_policy;
1360 else if (!strncmp("out", sep->se_policy, 3))
1361 policy_out = sep->se_policy;
1363 syslog(LOG_ERR, "invalid security policy \"%s\"",
1369 if (policy_in != NULL) {
1370 buf = ipsec_set_policy(policy_in, strlen(policy_in));
1372 if (setsockopt(sep->se_fd, level, opt,
1373 buf, ipsec_get_policylen(buf)) < 0 &&
1375 warnx("%s/%s: ipsec initialization failed; %s",
1376 sep->se_service, sep->se_proto,
1380 syslog(LOG_ERR, "invalid security policy \"%s\"",
1383 if (policy_out != NULL) {
1384 buf = ipsec_set_policy(policy_out, strlen(policy_out));
1386 if (setsockopt(sep->se_fd, level, opt,
1387 buf, ipsec_get_policylen(buf)) < 0 &&
1389 warnx("%s/%s: ipsec initialization failed; %s",
1390 sep->se_service, sep->se_proto,
1394 syslog(LOG_ERR, "invalid security policy \"%s\"",
1401 * Finish with a service and its socket.
1405 struct servtab *sep;
1407 if (sep->se_fd >= 0) {
1408 if (FD_ISSET(sep->se_fd, &allsock))
1410 (void) close(sep->se_fd);
1414 sep->se_numchild = 0; /* forget about any existing children */
1418 matchservent(name1, name2, proto)
1419 const char *name1, *name2, *proto;
1424 if (strcmp(proto, "unix") == 0) {
1425 if ((p = strrchr(name1, '/')) != NULL)
1427 if ((p = strrchr(name2, '/')) != NULL)
1430 if (strcmp(name1, name2) == 0)
1432 if ((se = getservbyname(name1, proto)) != NULL) {
1433 if (strcmp(name2, se->s_name) == 0)
1435 for (alias = se->s_aliases; *alias; alias++)
1436 if (strcmp(name2, *alias) == 0)
1446 struct servtab *sep;
1449 sep = (struct servtab *)malloc(sizeof (*sep));
1450 if (sep == (struct servtab *)0) {
1451 syslog(LOG_ERR, "malloc: %m");
1456 omask = sigblock(SIGBLOCK);
1457 sep->se_next = servtab;
1465 struct servtab *sep;
1469 "enabling %s, fd %d", sep->se_service, sep->se_fd);
1471 if (sep->se_fd < 0) {
1473 "%s: %s: bad fd", __FUNCTION__, sep->se_service);
1478 "%s: %s: is mux", __FUNCTION__, sep->se_service);
1481 if (FD_ISSET(sep->se_fd, &allsock)) {
1483 "%s: %s: not off", __FUNCTION__, sep->se_service);
1488 FD_SET(sep->se_fd, &allsock);
1489 if (sep->se_fd > maxsock)
1490 maxsock = sep->se_fd;
1495 struct servtab *sep;
1499 "disabling %s, fd %d", sep->se_service, sep->se_fd);
1501 if (sep->se_fd < 0) {
1503 "%s: %s: bad fd", __FUNCTION__, sep->se_service);
1508 "%s: %s: is mux", __FUNCTION__, sep->se_service);
1511 if (!FD_ISSET(sep->se_fd, &allsock)) {
1513 "%s: %s: not on", __FUNCTION__, sep->se_service);
1517 syslog(LOG_ERR, "%s: nsock=0", __FUNCTION__);
1522 FD_CLR(sep->se_fd, &allsock);
1523 if (sep->se_fd == maxsock)
1527 FILE *fconfig = NULL;
1528 struct servtab serv;
1529 char line[LINE_MAX];
1535 if (fconfig != NULL) {
1536 fseek(fconfig, 0L, SEEK_SET);
1539 fconfig = fopen(CONFIG, "r");
1540 return (fconfig != NULL);
1547 (void) fclose(fconfig);
1555 struct servtab *sep = &serv;
1559 static char TCPMUX_TOKEN[] = "tcpmux/";
1560 #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
1562 char *policy = NULL;
1571 while ((cp = nextline(fconfig)) != NULL) {
1573 /* lines starting with #@ is not a comment, but the policy */
1574 if (cp[0] == '#' && cp[1] == '@') {
1576 for (p = cp + 2; p && *p && isspace(*p); p++)
1582 } else if (ipsec_get_policylen(p) >= 0) {
1588 "%s: invalid ipsec policy \"%s\"",
1594 if (*cp == '#' || *cp == '\0')
1599 return ((struct servtab *)0);
1601 * clear the static buffer, since some fields (se_ctrladdr,
1602 * for example) don't get initialized here.
1604 memset(sep, 0, sizeof *sep);
1607 /* got an empty line containing just blanks/tabs. */
1610 if (arg[0] == ':') { /* :user:group:perm: */
1611 char *user, *group, *perm;
1615 if ((group = strchr(user, ':')) == NULL) {
1616 syslog(LOG_ERR, "no group after user '%s'", user);
1620 if ((perm = strchr(group, ':')) == NULL) {
1621 syslog(LOG_ERR, "no mode after group '%s'", group);
1625 if ((pw = getpwnam(user)) == NULL) {
1626 syslog(LOG_ERR, "no such user '%s'", user);
1629 sep->se_sockuid = pw->pw_uid;
1630 if ((gr = getgrnam(group)) == NULL) {
1631 syslog(LOG_ERR, "no such user '%s'", group);
1634 sep->se_sockgid = gr->gr_gid;
1635 sep->se_sockmode = strtol(perm, &arg, 8);
1637 syslog(LOG_ERR, "bad mode '%s'", perm);
1642 sep->se_sockuid = euid;
1643 sep->se_sockgid = egid;
1644 sep->se_sockmode = 0200;
1646 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
1647 char *c = arg + MUX_LEN;
1649 sep->se_type = MUXPLUS_TYPE;
1652 sep->se_type = MUX_TYPE;
1653 sep->se_service = newstr(c);
1655 sep->se_service = newstr(arg);
1656 sep->se_type = NORM_TYPE;
1659 if (strcmp(arg, "stream") == 0)
1660 sep->se_socktype = SOCK_STREAM;
1661 else if (strcmp(arg, "dgram") == 0)
1662 sep->se_socktype = SOCK_DGRAM;
1663 else if (strcmp(arg, "rdm") == 0)
1664 sep->se_socktype = SOCK_RDM;
1665 else if (strcmp(arg, "seqpacket") == 0)
1666 sep->se_socktype = SOCK_SEQPACKET;
1667 else if (strcmp(arg, "raw") == 0)
1668 sep->se_socktype = SOCK_RAW;
1670 sep->se_socktype = -1;
1673 if (strncmp(arg, "tcp", 3) == 0) {
1674 sep->se_proto = newstr(strsep(&arg, "/"));
1676 if (strcmp(arg, "ttcp") == 0)
1677 sep->se_type = TTCP_TYPE;
1678 else if (strcmp(arg, "faith") == 0)
1679 sep->se_type = FAITH_TYPE;
1682 if (sep->se_type == NORM_TYPE &&
1683 strncmp(arg, "faith/", 6) == 0) {
1685 sep->se_type = FAITH_TYPE;
1687 sep->se_proto = newstr(arg);
1689 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1690 if (no_v4bind != 0) {
1691 syslog(LOG_NOTICE, "IPv4 bind is ignored for %s",
1696 memmove(sep->se_proto, sep->se_proto + 4,
1697 strlen(sep->se_proto) + 1 - 4);
1699 sep->se_rpc_prog = sep->se_rpc_lowvers =
1700 sep->se_rpc_lowvers = 0;
1701 memcpy(&sep->se_ctrladdr4, bind_sa4,
1702 sizeof(sep->se_ctrladdr4));
1703 if ((versp = rindex(sep->se_service, '/'))) {
1705 switch (sscanf(versp, "%u-%u",
1706 &sep->se_rpc_lowvers,
1707 &sep->se_rpc_highvers)) {
1711 sep->se_rpc_highvers =
1712 sep->se_rpc_lowvers;
1716 "bad RPC version specifier; %s",
1723 sep->se_rpc_lowvers =
1724 sep->se_rpc_highvers = 1;
1727 sep->se_nomapped = 0;
1728 while (isdigit(sep->se_proto[strlen(sep->se_proto) - 1])) {
1730 if (sep->se_proto[strlen(sep->se_proto) - 1] == '6') {
1731 if (no_v6bind != 0) {
1732 syslog(LOG_NOTICE, "IPv6 bind is ignored for %s",
1737 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1742 if (sep->se_proto[strlen(sep->se_proto) - 1] == '4') {
1743 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1747 /* illegal version num */
1748 syslog(LOG_ERR, "bad IP version for %s", sep->se_proto);
1752 if (strcmp(sep->se_proto, "unix") == 0) {
1753 sep->se_family = AF_UNIX;
1757 sep->se_family = AF_INET6;
1758 if (v4bind == 0 || no_v4bind != 0)
1759 sep->se_nomapped = 1;
1762 { /* default to v4 bind if not v6 bind */
1763 if (no_v4bind != 0) {
1764 syslog(LOG_NOTICE, "IPv4 bind is ignored for %s",
1769 sep->se_family = AF_INET;
1772 switch(sep->se_family) {
1774 memcpy(&sep->se_ctrladdr4, bind_sa4,
1775 sizeof(sep->se_ctrladdr4));
1776 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr4);
1780 memcpy(&sep->se_ctrladdr6, bind_sa6,
1781 sizeof(sep->se_ctrladdr6));
1782 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr6);
1786 if (strlen(sep->se_service) >= sizeof(sep->se_ctrladdr_un.sun_path)) {
1788 "domain socket pathname too long for service %s",
1792 memset(&sep->se_ctrladdr, 0, sizeof(sep->se_ctrladdr));
1793 sep->se_ctrladdr_un.sun_family = sep->se_family;
1794 sep->se_ctrladdr_un.sun_len = strlen(sep->se_service);
1795 strcpy(sep->se_ctrladdr_un.sun_path, sep->se_service);
1796 sep->se_ctrladdr_size = SUN_LEN(&sep->se_ctrladdr_un);
1799 if (!strncmp(arg, "wait", 4))
1801 else if (!strncmp(arg, "nowait", 6))
1805 "%s: bad wait/nowait for service %s",
1806 CONFIG, sep->se_service);
1809 sep->se_maxchild = -1;
1810 sep->se_maxcpm = -1;
1811 sep->se_maxperip = -1;
1812 if ((s = strchr(arg, '/')) != NULL) {
1816 val = strtoul(s + 1, &eptr, 10);
1817 if (eptr == s + 1 || val > MAX_MAXCHLD) {
1819 "%s: bad max-child for service %s",
1820 CONFIG, sep->se_service);
1824 if (!sep->se_accept && val != 1)
1825 warnx("maxchild=%lu for wait service %s"
1826 " not recommended", val, sep->se_service);
1827 sep->se_maxchild = val;
1829 sep->se_maxcpm = strtol(eptr + 1, &eptr, 10);
1831 sep->se_maxperip = strtol(eptr + 1, &eptr, 10);
1833 * explicitly do not check for \0 for future expansion /
1834 * backwards compatibility
1839 * Silently enforce "nowait" mode for TCPMUX services
1840 * since they don't have an assigned port to listen on.
1843 if (strcmp(sep->se_proto, "tcp")) {
1845 "%s: bad protocol for tcpmux service %s",
1846 CONFIG, sep->se_service);
1849 if (sep->se_socktype != SOCK_STREAM) {
1851 "%s: bad socket type for tcpmux service %s",
1852 CONFIG, sep->se_service);
1856 sep->se_user = newstr(sskip(&cp));
1858 if ((s = strrchr(sep->se_user, '/')) != NULL) {
1860 sep->se_class = newstr(s + 1);
1862 sep->se_class = newstr(RESOURCE_RC);
1864 if ((s = strrchr(sep->se_user, ':')) != NULL) {
1866 sep->se_group = newstr(s + 1);
1868 sep->se_group = NULL;
1869 sep->se_server = newstr(sskip(&cp));
1870 if ((sep->se_server_name = rindex(sep->se_server, '/')))
1871 sep->se_server_name++;
1872 if (strcmp(sep->se_server, "internal") == 0) {
1875 for (bi = biltins; bi->bi_service; bi++)
1876 if (bi->bi_socktype == sep->se_socktype &&
1877 matchservent(bi->bi_service, sep->se_service,
1880 if (bi->bi_service == 0) {
1881 syslog(LOG_ERR, "internal service %s unknown",
1885 sep->se_accept = 1; /* force accept mode for built-ins */
1889 if (sep->se_maxperip < 0)
1890 sep->se_maxperip = maxperip;
1891 if (sep->se_maxcpm < 0)
1892 sep->se_maxcpm = maxcpm;
1893 if (sep->se_maxchild < 0) { /* apply default max-children */
1894 if (sep->se_bi && sep->se_bi->bi_maxchild >= 0)
1895 sep->se_maxchild = sep->se_bi->bi_maxchild;
1896 else if (sep->se_accept)
1897 sep->se_maxchild = maxchild > 0 ? maxchild : 0;
1899 sep->se_maxchild = 1;
1901 if (sep->se_maxchild > 0) {
1902 sep->se_pids = malloc(sep->se_maxchild * sizeof(*sep->se_pids));
1903 if (sep->se_pids == NULL) {
1904 syslog(LOG_ERR, "malloc: %m");
1909 for (arg = skip(&cp); cp; arg = skip(&cp))
1910 if (argc < MAXARGV) {
1911 sep->se_argv[argc++] = newstr(arg);
1914 "%s: too many arguments for service %s",
1915 CONFIG, sep->se_service);
1918 while (argc <= MAXARGV)
1919 sep->se_argv[argc++] = NULL;
1920 for (i = 0; i < PERIPSIZE; ++i)
1921 LIST_INIT(&sep->se_conn[i]);
1923 sep->se_policy = policy ? newstr(policy) : NULL;
1935 free(cp->se_service);
1947 free(cp->se_server);
1950 for (i = 0; i < MAXARGV; i++)
1952 free(cp->se_argv[i]);
1956 free(cp->se_policy);
1962 * Safe skip - if skip returns null, log a syntax error in the
1963 * configuration file and exit.
1973 syslog(LOG_ERR, "%s: syntax error", CONFIG);
1988 while (*cp == ' ' || *cp == '\t')
1994 (void) ungetc(c, fconfig);
1995 if (c == ' ' || c == '\t')
1996 if ((cp = nextline(fconfig)))
2001 if (*cp == '"' || *cp == '\'')
2005 while (*cp && *cp != quote)
2008 while (*cp && *cp != ' ' && *cp != '\t')
2022 if (fgets(line, sizeof (line), fd) == NULL)
2024 cp = strchr(line, '\n');
2036 if ((cr = strdup(cp != NULL ? cp : "")))
2038 syslog(LOG_ERR, "strdup: %m");
2043 inetd_setproctitle(a, s)
2048 struct sockaddr_storage ss;
2049 char buf[80], pbuf[INET6_ADDRSTRLEN];
2052 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) {
2053 getnameinfo((struct sockaddr *)&ss, size, pbuf, sizeof(pbuf),
2054 NULL, 0, NI_NUMERICHOST|NI_WITHSCOPEID);
2055 (void) sprintf(buf, "%s [%s]", a, pbuf);
2057 (void) sprintf(buf, "%s", a);
2058 setproctitle("%s", buf);
2063 const struct sockaddr *sa;
2064 const struct servtab *sep;
2066 struct servtab *se2;
2067 char pname[INET6_ADDRSTRLEN];
2069 for (se2 = servtab; se2; se2 = se2->se_next) {
2070 if (!se2->se_bi || se2->se_socktype != SOCK_DGRAM)
2073 switch (se2->se_family) {
2075 if (((const struct sockaddr_in *)sa)->sin_port ==
2076 se2->se_ctrladdr4.sin_port)
2081 if (((const struct sockaddr_in *)sa)->sin_port ==
2082 se2->se_ctrladdr4.sin_port)
2090 getnameinfo(sa, sa->sa_len, pname, sizeof(pname), NULL, 0,
2091 NI_NUMERICHOST|NI_WITHSCOPEID);
2092 syslog(LOG_WARNING, "%s/%s:%s/%s loop request REFUSED from %s",
2093 sep->se_service, sep->se_proto,
2094 se2->se_service, se2->se_proto,
2103 * Dump relevant information to stderr
2106 print_service(action, sep)
2108 const struct servtab *sep;
2111 "%s: %s proto=%s accept=%d max=%d user=%s group=%s"
2115 " builtin=%p server=%s"
2120 action, sep->se_service, sep->se_proto,
2121 sep->se_accept, sep->se_maxchild, sep->se_user, sep->se_group,
2125 (void *) sep->se_bi, sep->se_server
2127 , (sep->se_policy ? sep->se_policy : "")
2132 #define CPMHSIZE 256
2133 #define CPMHMASK (CPMHSIZE-1)
2137 typedef struct CTime {
2138 unsigned long ct_Ticks;
2142 typedef struct CHash {
2144 struct in_addr c4_Addr;
2145 struct in6_addr c6_Addr;
2147 #define ch_Addr4 cu_Addr.c4_Addr
2148 #define ch_Addr6 cu_Addr.c6_Addr
2152 CTime ch_Times[CHTSIZE];
2155 CHash CHashAry[CPMHSIZE];
2159 const struct servtab *sep;
2162 struct sockaddr_storage rss;
2163 socklen_t rssLen = sizeof(rss);
2167 * If getpeername() fails, just let it through (if logging is
2168 * enabled the condition is caught elsewhere)
2171 if (sep->se_maxcpm > 0 &&
2172 getpeername(ctrl, (struct sockaddr *)&rss, &rssLen) == 0 ) {
2173 time_t t = time(NULL);
2174 int hv = 0xABC3D20F;
2177 CHash *chBest = NULL;
2178 unsigned int ticks = t / CHTGRAN;
2179 struct sockaddr_in *sin4;
2181 struct sockaddr_in6 *sin6;
2184 sin4 = (struct sockaddr_in *)&rss;
2186 sin6 = (struct sockaddr_in6 *)&rss;
2192 switch (rss.ss_family) {
2194 p = (char *)&sin4->sin_addr;
2195 addrlen = sizeof(struct in_addr);
2199 p = (char *)&sin6->sin6_addr;
2200 addrlen = sizeof(struct in6_addr);
2204 /* should not happen */
2208 for (i = 0; i < addrlen; ++i, ++p) {
2209 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2211 hv = (hv ^ (hv >> 16));
2213 for (i = 0; i < 5; ++i) {
2214 CHash *ch = &CHashAry[(hv + i) & CPMHMASK];
2216 if (rss.ss_family == AF_INET &&
2217 ch->ch_Family == AF_INET &&
2218 sin4->sin_addr.s_addr == ch->ch_Addr4.s_addr &&
2219 ch->ch_Service && strcmp(sep->se_service,
2220 ch->ch_Service) == 0) {
2225 if (rss.ss_family == AF_INET6 &&
2226 ch->ch_Family == AF_INET6 &&
2227 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2228 &ch->ch_Addr6) != 0 &&
2229 ch->ch_Service && strcmp(sep->se_service,
2230 ch->ch_Service) == 0) {
2235 if (chBest == NULL || ch->ch_LTime == 0 ||
2236 ch->ch_LTime < chBest->ch_LTime) {
2240 if ((rss.ss_family == AF_INET &&
2241 (chBest->ch_Family != AF_INET ||
2242 sin4->sin_addr.s_addr != chBest->ch_Addr4.s_addr)) ||
2243 chBest->ch_Service == NULL ||
2244 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2245 chBest->ch_Family = sin4->sin_family;
2246 chBest->ch_Addr4 = sin4->sin_addr;
2247 if (chBest->ch_Service)
2248 free(chBest->ch_Service);
2249 chBest->ch_Service = strdup(sep->se_service);
2250 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2253 if ((rss.ss_family == AF_INET6 &&
2254 (chBest->ch_Family != AF_INET6 ||
2255 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2256 &chBest->ch_Addr6) == 0)) ||
2257 chBest->ch_Service == NULL ||
2258 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2259 chBest->ch_Family = sin6->sin6_family;
2260 chBest->ch_Addr6 = sin6->sin6_addr;
2261 if (chBest->ch_Service)
2262 free(chBest->ch_Service);
2263 chBest->ch_Service = strdup(sep->se_service);
2264 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2267 chBest->ch_LTime = t;
2269 CTime *ct = &chBest->ch_Times[ticks % CHTSIZE];
2270 if (ct->ct_Ticks != ticks) {
2271 ct->ct_Ticks = ticks;
2276 for (i = 0; i < CHTSIZE; ++i) {
2277 CTime *ct = &chBest->ch_Times[i];
2278 if (ct->ct_Ticks <= ticks &&
2279 ct->ct_Ticks >= ticks - CHTSIZE) {
2280 cnt += ct->ct_Count;
2283 if (cnt * (CHTSIZE * CHTGRAN) / 60 > sep->se_maxcpm) {
2284 char pname[INET6_ADDRSTRLEN];
2286 getnameinfo((struct sockaddr *)&rss,
2287 ((struct sockaddr *)&rss)->sa_len,
2288 pname, sizeof(pname), NULL, 0,
2289 NI_NUMERICHOST|NI_WITHSCOPEID);
2292 "%s from %s exceeded counts/min (limit %d/min)",
2293 sep->se_service, pname,
2300 static struct conninfo *
2301 search_conn(struct servtab *sep, int ctrl)
2303 struct sockaddr_storage ss;
2304 socklen_t sslen = sizeof(ss);
2305 struct conninfo *conn;
2307 char pname[NI_MAXHOST], pname2[NI_MAXHOST];
2309 if (sep->se_maxperip <= 0)
2313 * If getpeername() fails, just let it through (if logging is
2314 * enabled the condition is caught elsewhere)
2316 if (getpeername(ctrl, (struct sockaddr *)&ss, &sslen) != 0)
2319 switch (ss.ss_family) {
2321 hv = hashval((char *)&((struct sockaddr_in *)&ss)->sin_addr,
2322 sizeof(struct in_addr));
2326 hv = hashval((char *)&((struct sockaddr_in6 *)&ss)->sin6_addr,
2327 sizeof(struct in6_addr));
2332 * Since we only support AF_INET and AF_INET6, just
2333 * let other than AF_INET and AF_INET6 through.
2338 if (getnameinfo((struct sockaddr *)&ss, sslen, pname, sizeof(pname),
2339 NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0)
2342 LIST_FOREACH(conn, &sep->se_conn[hv], co_link) {
2343 if (getnameinfo((struct sockaddr *)&conn->co_addr,
2344 conn->co_addr.ss_len, pname2, sizeof(pname2), NULL, 0,
2345 NI_NUMERICHOST | NI_WITHSCOPEID) == 0 &&
2346 strcmp(pname, pname2) == 0)
2351 if ((conn = malloc(sizeof(struct conninfo))) == NULL) {
2352 syslog(LOG_ERR, "malloc: %m");
2355 conn->co_proc = malloc(sep->se_maxperip * sizeof(*conn->co_proc));
2356 if (conn->co_proc == NULL) {
2357 syslog(LOG_ERR, "malloc: %m");
2360 memcpy(&conn->co_addr, (struct sockaddr *)&ss, sslen);
2361 conn->co_numchild = 0;
2362 LIST_INSERT_HEAD(&sep->se_conn[hv], conn, co_link);
2366 * Since a child process is not invoked yet, we cannot
2367 * determine a pid of a child. So, co_proc and co_numchild
2368 * should be filled leter.
2375 room_conn(struct servtab *sep, struct conninfo *conn)
2377 char pname[NI_MAXHOST];
2379 if (conn->co_numchild >= sep->se_maxperip) {
2380 getnameinfo((struct sockaddr *)&conn->co_addr,
2381 conn->co_addr.ss_len, pname, sizeof(pname), NULL, 0,
2382 NI_NUMERICHOST | NI_WITHSCOPEID);
2383 syslog(LOG_ERR, "%s from %s exceeded counts (limit %d)",
2384 sep->se_service, pname, sep->se_maxperip);
2391 addchild_conn(struct conninfo *conn, pid_t pid)
2393 struct procinfo *proc;
2398 if ((proc = search_proc(pid, 1)) != NULL) {
2399 if (proc->pr_conn != NULL) {
2401 "addchild_conn: child already on process list");
2404 proc->pr_conn = conn;
2407 conn->co_proc[conn->co_numchild++] = proc;
2411 reapchild_conn(pid_t pid)
2413 struct procinfo *proc;
2414 struct conninfo *conn;
2417 if ((proc = search_proc(pid, 0)) == NULL)
2419 if ((conn = proc->pr_conn) == NULL)
2421 for (i = 0; i < conn->co_numchild; ++i)
2422 if (conn->co_proc[i] == proc) {
2423 conn->co_proc[i] = conn->co_proc[--conn->co_numchild];
2431 resize_conn(struct servtab *sep, int maxpip)
2433 struct conninfo *conn;
2436 if (sep->se_maxperip <= 0)
2442 for (i = 0; i < PERIPSIZE; ++i) {
2443 LIST_FOREACH(conn, &sep->se_conn[i], co_link) {
2444 for (j = maxpip; j < conn->co_numchild; ++j)
2445 free_proc(conn->co_proc[j]);
2446 conn->co_proc = realloc(conn->co_proc,
2447 maxpip * sizeof(*conn->co_proc));
2448 if (conn->co_proc == NULL) {
2449 syslog(LOG_ERR, "realloc: %m");
2452 if (conn->co_numchild > maxpip)
2453 conn->co_numchild = maxpip;
2459 free_connlist(struct servtab *sep)
2461 struct conninfo *conn;
2464 for (i = 0; i < PERIPSIZE; ++i) {
2465 while ((conn = LIST_FIRST(&sep->se_conn[i])) != NULL) {
2466 for (j = 0; j < conn->co_numchild; ++j)
2467 free_proc(conn->co_proc[j]);
2468 conn->co_numchild = 0;
2475 free_conn(struct conninfo *conn)
2479 if (conn->co_numchild <= 0) {
2480 LIST_REMOVE(conn, co_link);
2481 free(conn->co_proc);
2486 static struct procinfo *
2487 search_proc(pid_t pid, int add)
2489 struct procinfo *proc;
2492 hv = hashval((char *)&pid, sizeof(pid));
2493 LIST_FOREACH(proc, &proctable[hv], pr_link) {
2494 if (proc->pr_pid == pid)
2497 if (proc == NULL && add) {
2498 if ((proc = malloc(sizeof(struct procinfo))) == NULL) {
2499 syslog(LOG_ERR, "malloc: %m");
2503 proc->pr_conn = NULL;
2504 LIST_INSERT_HEAD(&proctable[hv], proc, pr_link);
2510 free_proc(struct procinfo *proc)
2514 LIST_REMOVE(proc, pr_link);
2519 hashval(char *p, int len)
2521 int i, hv = 0xABC3D20F;
2523 for (i = 0; i < len; ++i, ++p)
2524 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2525 hv = (hv ^ (hv >> 16)) & (PERIPSIZE - 1);