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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1991, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)from: inetd.c 8.4 (Berkeley) 4/13/94";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
50 * Inetd - Internet super-server
52 * This program invokes all internet services as needed. Connection-oriented
53 * services are invoked each time a connection is made, by creating a process.
54 * This process is passed the connection as file descriptor 0 and is expected
55 * to do a getpeername to find out the source host and port.
57 * Datagram oriented services are invoked when a datagram
58 * arrives; a process is created and passed a pending message
59 * on file descriptor 0. Datagram servers may either connect
60 * to their peer, freeing up the original socket for inetd
61 * to receive further messages on, or ``take over the socket'',
62 * processing all arriving datagrams and, eventually, timing
63 * out. The first type of server is said to be ``multi-threaded'';
64 * the second type of server ``single-threaded''.
66 * Inetd uses a configuration file which is read at startup
67 * and, possibly, at some later time in response to a hangup signal.
68 * The configuration file is ``free format'' with fields given in the
69 * order shown below. Continuation lines for an entry must begin with
70 * a space or tab. All fields must be present in each entry.
72 * service name must be in /etc/services
73 * or name a tcpmux service
74 * or specify a unix domain socket
75 * socket type stream/dgram/raw/rdm/seqpacket
76 * protocol tcp[4][6][/faith,ttcp], udp[4][6], unix
77 * wait/nowait single-threaded/multi-threaded
78 * user user to run daemon as
79 * server program full path name
80 * server program arguments maximum of MAXARGS (20)
82 * TCP services without official port numbers are handled with the
83 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for
84 * requests. When a connection is made from a foreign host, the service
85 * requested is passed to tcpmux, which looks it up in the servtab list
86 * and returns the proper entry for the service. Tcpmux returns a
87 * negative reply if the service doesn't exist, otherwise the invoked
88 * server is expected to return the positive reply if the service type in
89 * inetd.conf file has the prefix "tcpmux/". If the service type has the
90 * prefix "tcpmux/+", tcpmux will return the positive reply for the
91 * process; this is for compatibility with older server code, and also
92 * allows you to invoke programs that use stdin/stdout without putting any
93 * special server code in them. Services that use tcpmux are "nowait"
94 * because they do not have a well-known port and hence cannot listen
98 * service name/version must be in /etc/rpc
99 * socket type stream/dgram/raw/rdm/seqpacket
100 * protocol rpc/tcp[4][6], rpc/udp[4][6]
101 * wait/nowait single-threaded/multi-threaded
102 * user user to run daemon as
103 * server program full path name
104 * server program arguments maximum of MAXARGS
106 * Comment lines are indicated by a `#' in column 1.
109 * Comment lines that start with "#@" denote IPsec policy string, as described
110 * in ipsec_set_policy(3). This will affect all the following items in
111 * inetd.conf(8). To reset the policy, just use "#@" line. By default,
112 * there's no IPsec policy.
115 #include <sys/param.h>
116 #include <sys/ioctl.h>
117 #include <sys/wait.h>
118 #include <sys/time.h>
119 #include <sys/resource.h>
120 #include <sys/stat.h>
123 #include <netinet/in.h>
124 #include <netinet/tcp.h>
125 #include <arpa/inet.h>
127 #include <rpc/pmap_clnt.h>
143 #include <sysexits.h>
147 #include "pathnames.h"
150 #include <netinet6/ipsec.h>
151 #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
156 /* wrapper for KAME-special getnameinfo() */
157 #ifndef NI_WITHSCOPEID
158 #define NI_WITHSCOPEID 0
161 #ifndef LIBWRAP_ALLOW_FACILITY
162 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
164 #ifndef LIBWRAP_ALLOW_SEVERITY
165 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
167 #ifndef LIBWRAP_DENY_FACILITY
168 # define LIBWRAP_DENY_FACILITY LOG_AUTH
170 #ifndef LIBWRAP_DENY_SEVERITY
171 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
174 #define ISWRAP(sep) \
175 ( ((wrap_ex && !(sep)->se_bi) || (wrap_bi && (sep)->se_bi)) \
176 && (sep->se_family == AF_INET || sep->se_family == AF_INET6) \
177 && ( ((sep)->se_accept && (sep)->se_socktype == SOCK_STREAM) \
178 || (sep)->se_socktype == SOCK_DGRAM))
181 #include <login_cap.h>
184 #define RESOURCE_RC "daemon"
189 #define MAXCHILD -1 /* maximum number of this service
194 #define MAXCPM -1 /* rate limit invocations from a
195 single remote address,
200 #define MAXPERIP -1 /* maximum number of this service
201 from a single remote address,
206 #define TOOMANY 256 /* don't start more than TOOMANY */
208 #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
209 #define RETRYTIME (60*10) /* retry after bind or server fail */
210 #define MAX_MAXCHLD 32767 /* max allowable max children */
212 #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
214 void close_sep(struct servtab *);
215 void flag_signal(int);
216 void flag_config(int);
218 int cpmip(const struct servtab *, int);
219 void endconfig(void);
220 struct servtab *enter(struct servtab *);
221 void freeconfig(struct servtab *);
222 struct servtab *getconfigent(void);
223 int matchservent(const char *, const char *, const char *);
224 char *nextline(FILE *);
225 void addchild(struct servtab *, int);
226 void flag_reapchild(int);
227 void reapchild(void);
228 void enable(struct servtab *);
229 void disable(struct servtab *);
230 void flag_retry(int);
233 void setup(struct servtab *);
235 void ipsecsetup(struct servtab *);
237 void unregisterrpc(register struct servtab *sep);
238 static struct conninfo *search_conn(struct servtab *sep, int ctrl);
239 static int room_conn(struct servtab *sep, struct conninfo *conn);
240 static void addchild_conn(struct conninfo *conn, pid_t pid);
241 static void reapchild_conn(pid_t pid);
242 static void free_conn(struct conninfo *conn);
243 static void resize_conn(struct servtab *sep, int maxperip);
244 static void free_connlist(struct servtab *sep);
245 static void free_proc(struct procinfo *);
246 static struct procinfo *search_proc(pid_t pid, int add);
247 static int hashval(char *p, int len);
255 int maxsock; /* highest-numbered descriptor */
259 int toomany = TOOMANY;
260 int maxchild = MAXCHILD;
262 int maxperip = MAXPERIP;
265 char *hostname = NULL;
266 struct sockaddr_in *bind_sa4;
269 struct sockaddr_in6 *bind_sa6;
280 struct servtab *servtab;
282 extern struct biltin biltins[];
284 const char *CONFIG = _PATH_INETDCONF;
285 const char *pid_file = _PATH_INETDPID;
287 struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
289 static LIST_HEAD(, procinfo) proctable[PERIPSIZE];
292 getvalue(const char *arg, int *value, const char *whine)
297 tmp = strtol(arg, &p, 0);
299 syslog(LOG_ERR, whine, arg);
300 return 1; /* failure */
303 return 0; /* success */
307 main(int argc, char **argv)
312 struct sigaction sa, saalrm, sachld, sahup, sapipe;
313 int tmpint, ch, dofork;
317 login_cap_t *lc = NULL;
319 struct request_info req;
321 char *service = NULL;
323 struct sockaddr peer_un;
324 struct sockaddr_in peer_un4;
325 struct sockaddr_in6 peer_un6;
326 struct sockaddr_storage peer_max;
328 #define peer p_un.peer_un
329 #define peer4 p_un.peer_un4
330 #define peer6 p_un.peer_un6
331 #define peermax p_un.peer_max
333 struct addrinfo hints, *res;
334 const char *servname;
336 struct conninfo *conn;
338 openlog("inetd", LOG_PID | LOG_NOWAIT | LOG_PERROR, LOG_DAEMON);
340 while ((ch = getopt(argc, argv, "dlwWR:a:c:C:p:s:")) != -1)
350 getvalue(optarg, &toomany,
351 "-R %s: bad value for service invocation rate");
354 getvalue(optarg, &maxchild,
355 "-c %s: bad value for maximum children");
358 getvalue(optarg, &maxcpm,
359 "-C %s: bad value for maximum children/minute");
368 getvalue(optarg, &maxperip,
369 "-s %s: bad value for maximum children per source address");
380 "usage: inetd [-dlwW] [-a address] [-R rate]"
381 " [-c maximum] [-C rate]"
382 " [-p pidfile] [conf-file]");
386 * Initialize Bind Addrs.
387 * When hostname is NULL, wild card bind addrs are obtained from
388 * getaddrinfo(). But getaddrinfo() requires at least one of
389 * hostname or servname is non NULL.
390 * So when hostname is NULL, set dummy value to servname.
392 servname = (hostname == NULL) ? "discard" /* dummy */ : NULL;
394 bzero(&hints, sizeof(struct addrinfo));
395 hints.ai_flags = AI_PASSIVE;
396 hints.ai_family = AF_UNSPEC;
397 error = getaddrinfo(hostname, servname, &hints, &res);
399 syslog(LOG_ERR, "-a %s: %s", hostname, gai_strerror(error));
400 if (error == EAI_SYSTEM)
401 syslog(LOG_ERR, "%s", strerror(errno));
405 if (res->ai_addr == NULL) {
406 syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
409 switch (res->ai_addr->sa_family) {
413 bind_sa4 = (struct sockaddr_in *)res->ai_addr;
414 /* init port num in case servname is dummy */
415 bind_sa4->sin_port = 0;
422 bind_sa6 = (struct sockaddr_in6 *)res->ai_addr;
423 /* init port num in case servname is dummy */
424 bind_sa6->sin6_port = 0;
435 } while ((res = res->ai_next) != NULL);
441 syslog(LOG_ERR, "-a %s: unknown address family", hostname);
447 umask(mask = umask(0777));
456 if (daemon(0, 0) < 0) {
457 syslog(LOG_WARNING, "daemon(0,0) failed: %m");
459 /* From now on we don't want syslog messages going to stderr. */
461 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
463 * In case somebody has started inetd manually, we need to
464 * clear the logname, so that old servers run as root do not
465 * get the user's logname..
467 if (setlogin("") < 0) {
468 syslog(LOG_WARNING, "cannot clear logname: %m");
469 /* no big deal if it fails.. */
472 fp = fopen(pid_file, "w");
474 fprintf(fp, "%ld\n", (long)pid);
477 syslog(LOG_WARNING, "%s: %m", pid_file);
481 for (i = 0; i < PERIPSIZE; ++i)
482 LIST_INIT(&proctable[i]);
485 udpconf = getnetconfigent("udp");
486 tcpconf = getnetconfigent("tcp");
487 if (udpconf == NULL || tcpconf == NULL) {
488 syslog(LOG_ERR, "unknown rpc/udp or rpc/tpc");
494 udp6conf = getnetconfigent("udp6");
495 tcp6conf = getnetconfigent("tcp6");
496 if (udp6conf == NULL || tcp6conf == NULL) {
497 syslog(LOG_ERR, "unknown rpc/udp6 or rpc/tpc6");
504 sigemptyset(&sa.sa_mask);
505 sigaddset(&sa.sa_mask, SIGALRM);
506 sigaddset(&sa.sa_mask, SIGCHLD);
507 sigaddset(&sa.sa_mask, SIGHUP);
508 sa.sa_handler = flag_retry;
509 sigaction(SIGALRM, &sa, &saalrm);
511 sa.sa_handler = flag_config;
512 sigaction(SIGHUP, &sa, &sahup);
513 sa.sa_handler = flag_reapchild;
514 sigaction(SIGCHLD, &sa, &sachld);
515 sa.sa_handler = SIG_IGN;
516 sigaction(SIGPIPE, &sa, &sapipe);
519 /* space for daemons to overwrite environment for ps */
520 #define DUMMYSIZE 100
521 char dummy[DUMMYSIZE];
523 (void)memset(dummy, 'x', DUMMYSIZE - 1);
524 dummy[DUMMYSIZE - 1] = '\0';
525 (void)setenv("inetd_dummy", dummy, 1);
528 if (pipe(signalpipe) != 0) {
529 syslog(LOG_ERR, "pipe: %m");
532 FD_SET(signalpipe[0], &allsock);
536 if (signalpipe[0] > maxsock)
537 maxsock = signalpipe[0];
538 if (signalpipe[1] > maxsock)
539 maxsock = signalpipe[1];
547 syslog(LOG_ERR, "%s: nsock=0", __FUNCTION__);
552 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
553 (fd_set *)0, (struct timeval *)0)) <= 0) {
554 if (n < 0 && errno != EINTR) {
555 syslog(LOG_WARNING, "select: %m");
560 /* handle any queued signal flags */
561 if (FD_ISSET(signalpipe[0], &readable)) {
563 if (ioctl(signalpipe[0], FIONREAD, &nsig) != 0) {
564 syslog(LOG_ERR, "ioctl: %m");
567 while (--nsig >= 0) {
569 if (read(signalpipe[0], &c, 1) != 1) {
570 syslog(LOG_ERR, "read: %m");
574 warnx("handling signal flag %c", c);
576 case 'A': /* sigalrm */
579 case 'C': /* sigchld */
582 case 'H': /* sighup */
588 for (sep = servtab; n && sep; sep = sep->se_next)
589 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
592 warnx("someone wants %s", sep->se_service);
593 dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep);
595 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) {
597 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
598 syslog(LOG_ERR, "ioctl (FIONBIO, 1): %m");
599 ctrl = accept(sep->se_fd, (struct sockaddr *)0,
602 warnx("accept, ctrl %d", ctrl);
606 "accept (for %s): %m",
608 if (sep->se_accept &&
609 sep->se_socktype == SOCK_STREAM)
614 if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
615 syslog(LOG_ERR, "ioctl1(FIONBIO, 0): %m");
616 if (ioctl(ctrl, FIONBIO, &i) < 0)
617 syslog(LOG_ERR, "ioctl2(FIONBIO, 0): %m");
618 if (cpmip(sep, ctrl) < 0) {
623 (conn = search_conn(sep, ctrl)) != NULL &&
624 !room_conn(sep, conn)) {
630 if (log && !ISWRAP(sep)) {
631 char pname[INET6_ADDRSTRLEN] = "unknown";
634 if (getpeername(ctrl, (struct sockaddr *)
637 if (recvfrom(ctrl, buf, sizeof(buf),
639 (struct sockaddr *)&peermax,
641 getnameinfo((struct sockaddr *)&peermax,
643 pname, sizeof(pname),
649 getnameinfo((struct sockaddr *)&peermax,
651 pname, sizeof(pname),
656 syslog(LOG_INFO,"%s from %s", sep->se_service, pname);
658 (void) sigblock(SIGBLOCK);
661 * Fork for all external services, builtins which need to
662 * fork and anything we're wrapping (as wrapping might
663 * block or use hosts_options(5) twist).
666 if (sep->se_count++ == 0)
667 (void)gettimeofday(&sep->se_time, (struct timezone *)NULL);
668 else if (toomany > 0 && sep->se_count >= toomany) {
671 (void)gettimeofday(&now, (struct timezone *)NULL);
672 if (now.tv_sec - sep->se_time.tv_sec >
678 "%s/%s server failing (looping), service terminated",
679 sep->se_service, sep->se_proto);
680 if (sep->se_accept &&
681 sep->se_socktype == SOCK_STREAM)
696 syslog(LOG_ERR, "fork: %m");
697 if (sep->se_accept &&
698 sep->se_socktype == SOCK_STREAM)
706 addchild_conn(conn, pid);
713 warnx("+ closing from %d", maxsock);
714 for (tmpint = maxsock; tmpint > 2; tmpint--)
716 (void) close(tmpint);
717 sigaction(SIGALRM, &saalrm, (struct sigaction *)0);
718 sigaction(SIGCHLD, &sachld, (struct sigaction *)0);
719 sigaction(SIGHUP, &sahup, (struct sigaction *)0);
720 /* SIGPIPE reset before exec */
723 * Call tcpmux to find the real service to exec.
726 sep->se_bi->bi_fn == (bi_fn_t *) tcpmux) {
734 inetd_setproctitle("wrapping", ctrl);
735 service = sep->se_server_name ?
736 sep->se_server_name : sep->se_service;
737 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, NULL);
739 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
740 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
741 denied = !hosts_access(&req);
743 syslog(deny_severity,
744 "refused connection from %.500s, service %s (%s%s)",
745 eval_client(&req), service, sep->se_proto,
746 (((struct sockaddr *)req.client->sin)->sa_family == AF_INET6 && !IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)req.client->sin)->sin6_addr)) ? "6" : "");
747 if (sep->se_socktype != SOCK_STREAM)
748 recv(ctrl, buf, sizeof (buf), 0);
755 syslog(allow_severity,
756 "connection from %.500s, service %s (%s%s)",
757 eval_client(&req), service, sep->se_proto,
758 (((struct sockaddr *)req.client->sin)->sa_family == AF_INET6 && !IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)req.client->sin)->sin6_addr)) ? "6" : "");
762 (*sep->se_bi->bi_fn)(ctrl, sep);
766 getpid(), sep->se_server);
771 if ((pwd = getpwnam(sep->se_user)) == NULL) {
773 "%s/%s: %s: no such user",
774 sep->se_service, sep->se_proto,
776 if (sep->se_socktype != SOCK_STREAM)
777 recv(0, buf, sizeof (buf), 0);
781 if ( sep->se_group != NULL
782 && (grp = getgrnam(sep->se_group)) == NULL
785 "%s/%s: %s: no such group",
786 sep->se_service, sep->se_proto,
788 if (sep->se_socktype != SOCK_STREAM)
789 recv(0, buf, sizeof (buf), 0);
793 pwd->pw_gid = grp->gr_gid;
795 if ((lc = login_getclass(sep->se_class)) == NULL) {
796 /* error syslogged by getclass */
798 "%s/%s: %s: login class error",
799 sep->se_service, sep->se_proto,
801 if (sep->se_socktype != SOCK_STREAM)
802 recv(0, buf, sizeof (buf), 0);
808 "%s: can't setsid(): %m",
810 /* _exit(EX_OSERR); not fatal yet */
813 if (setusercontext(lc, pwd, pwd->pw_uid,
814 LOGIN_SETALL) != 0) {
816 "%s: can't setusercontext(..%s..): %m",
817 sep->se_service, sep->se_user);
822 if (setlogin(sep->se_user) < 0) {
824 "%s: can't setlogin(%s): %m",
825 sep->se_service, sep->se_user);
826 /* _exit(EX_OSERR); not yet */
828 if (setgid(pwd->pw_gid) < 0) {
830 "%s: can't set gid %d: %m",
831 sep->se_service, pwd->pw_gid);
834 (void) initgroups(pwd->pw_name,
836 if (setuid(pwd->pw_uid) < 0) {
838 "%s: can't set uid %d: %m",
839 sep->se_service, pwd->pw_uid);
844 sigaction(SIGPIPE, &sapipe,
845 (struct sigaction *)0);
846 execv(sep->se_server, sep->se_argv);
848 "cannot execute %s: %m", sep->se_server);
849 if (sep->se_socktype != SOCK_STREAM)
850 recv(0, buf, sizeof (buf), 0);
855 if (sep->se_accept && sep->se_socktype == SOCK_STREAM)
862 * Add a signal flag to the signal flag queue for later handling
870 if (write(signalpipe[1], &ch, 1) != 1) {
871 syslog(LOG_ERR, "write: %m");
877 * Record a new child pid for this service. If we've reached the
878 * limit on children, then stop accepting incoming requests.
882 addchild(struct servtab *sep, pid_t pid)
884 if (sep->se_maxchild <= 0)
887 if (sep->se_numchild >= sep->se_maxchild) {
888 syslog(LOG_ERR, "%s: %d >= %d",
889 __FUNCTION__, sep->se_numchild, sep->se_maxchild);
893 sep->se_pids[sep->se_numchild++] = pid;
894 if (sep->se_numchild == sep->se_maxchild)
899 * Some child process has exited. See if it's on somebody's list.
903 flag_reapchild(int signo __unused)
916 pid = wait3(&status, WNOHANG, (struct rusage *)0);
920 warnx("%d reaped, status %#x", pid, status);
921 for (sep = servtab; sep; sep = sep->se_next) {
922 for (k = 0; k < sep->se_numchild; k++)
923 if (sep->se_pids[k] == pid)
925 if (k == sep->se_numchild)
927 if (sep->se_numchild == sep->se_maxchild)
929 sep->se_pids[k] = sep->se_pids[--sep->se_numchild];
932 "%s[%d]: exit status 0x%x",
933 sep->se_server, pid, status);
941 flag_config(int signo __unused)
949 struct servtab *sep, *new, **sepp;
950 struct conninfo *conn;
955 syslog(LOG_ERR, "%s: %m", CONFIG);
958 for (sep = servtab; sep; sep = sep->se_next)
960 while ((new = getconfigent())) {
961 if (getpwnam(new->se_user) == NULL) {
963 "%s/%s: no such user '%s', service ignored",
964 new->se_service, new->se_proto, new->se_user);
967 if (new->se_group && getgrnam(new->se_group) == NULL) {
969 "%s/%s: no such group '%s', service ignored",
970 new->se_service, new->se_proto, new->se_group);
974 if (login_getclass(new->se_class) == NULL) {
975 /* error syslogged by getclass */
977 "%s/%s: %s: login class error, service ignored",
978 new->se_service, new->se_proto, new->se_class);
982 new_nomapped = new->se_nomapped;
983 for (sep = servtab; sep; sep = sep->se_next)
984 if (strcmp(sep->se_service, new->se_service) == 0 &&
985 strcmp(sep->se_proto, new->se_proto) == 0 &&
986 sep->se_rpc == new->se_rpc &&
987 sep->se_socktype == new->se_socktype &&
988 sep->se_family == new->se_family)
993 #define SWAP(t,a, b) { t c = a; a = b; b = c; }
994 omask = sigblock(SIGBLOCK);
995 if (sep->se_nomapped != new->se_nomapped) {
996 /* for rpc keep old nommaped till unregister */
998 sep->se_nomapped = new->se_nomapped;
1001 /* copy over outstanding child pids */
1002 if (sep->se_maxchild > 0 && new->se_maxchild > 0) {
1003 new->se_numchild = sep->se_numchild;
1004 if (new->se_numchild > new->se_maxchild)
1005 new->se_numchild = new->se_maxchild;
1006 memcpy(new->se_pids, sep->se_pids,
1007 new->se_numchild * sizeof(*new->se_pids));
1009 SWAP(pid_t *, sep->se_pids, new->se_pids);
1010 sep->se_maxchild = new->se_maxchild;
1011 sep->se_numchild = new->se_numchild;
1012 sep->se_maxcpm = new->se_maxcpm;
1013 resize_conn(sep, new->se_maxperip);
1014 sep->se_maxperip = new->se_maxperip;
1015 sep->se_bi = new->se_bi;
1016 /* might need to turn on or off service now */
1017 if (sep->se_fd >= 0) {
1018 if (sep->se_maxchild > 0
1019 && sep->se_numchild == sep->se_maxchild) {
1020 if (FD_ISSET(sep->se_fd, &allsock))
1023 if (!FD_ISSET(sep->se_fd, &allsock))
1027 sep->se_accept = new->se_accept;
1028 SWAP(char *, sep->se_user, new->se_user);
1029 SWAP(char *, sep->se_group, new->se_group);
1031 SWAP(char *, sep->se_class, new->se_class);
1033 SWAP(char *, sep->se_server, new->se_server);
1034 SWAP(char *, sep->se_server_name, new->se_server_name);
1035 for (i = 0; i < MAXARGV; i++)
1036 SWAP(char *, sep->se_argv[i], new->se_argv[i]);
1038 SWAP(char *, sep->se_policy, new->se_policy);
1044 print_service("REDO", sep);
1048 print_service("ADD ", sep);
1050 sep->se_checked = 1;
1055 switch (sep->se_family) {
1057 if (no_v4bind != 0) {
1064 if (no_v6bind != 0) {
1072 if (sep->se_family != AF_UNIX) {
1073 sp = getservbyname(sep->se_service, sep->se_proto);
1075 syslog(LOG_ERR, "%s/%s: unknown service",
1076 sep->se_service, sep->se_proto);
1077 sep->se_checked = 0;
1081 switch (sep->se_family) {
1083 if (sp->s_port != sep->se_ctrladdr4.sin_port) {
1084 sep->se_ctrladdr4.sin_port =
1092 sep->se_ctrladdr6.sin6_port) {
1093 sep->se_ctrladdr6.sin6_port =
1100 if (sep->se_reset != 0 && sep->se_fd >= 0)
1103 rpc = getrpcbyname(sep->se_service);
1105 syslog(LOG_ERR, "%s/%s unknown RPC service",
1106 sep->se_service, sep->se_proto);
1107 if (sep->se_fd != -1)
1108 (void) close(sep->se_fd);
1112 if (sep->se_reset != 0 ||
1113 rpc->r_number != sep->se_rpc_prog) {
1114 if (sep->se_rpc_prog)
1116 sep->se_rpc_prog = rpc->r_number;
1117 if (sep->se_fd != -1)
1118 (void) close(sep->se_fd);
1121 sep->se_nomapped = new_nomapped;
1124 if (sep->se_fd == -1)
1129 * Purge anything not looked at above.
1131 omask = sigblock(SIGBLOCK);
1133 while ((sep = *sepp)) {
1134 if (sep->se_checked) {
1135 sepp = &sep->se_next;
1138 *sepp = sep->se_next;
1139 if (sep->se_fd >= 0)
1142 print_service("FREE", sep);
1143 if (sep->se_rpc && sep->se_rpc_prog > 0)
1148 (void) sigsetmask(omask);
1152 unregisterrpc(struct servtab *sep)
1155 struct servtab *sepp;
1157 struct netconfig *netid4, *netid6;
1159 omask = sigblock(SIGBLOCK);
1160 netid4 = sep->se_socktype == SOCK_DGRAM ? udpconf : tcpconf;
1161 netid6 = sep->se_socktype == SOCK_DGRAM ? udp6conf : tcp6conf;
1162 if (sep->se_family == AF_INET)
1164 else if (sep->se_nomapped)
1167 * Conflict if same prog and protocol - In that case one should look
1168 * to versions, but it is not interesting: having separate servers for
1169 * different versions does not work well.
1170 * Therefore one do not unregister if there is a conflict.
1171 * There is also transport conflict if destroying INET when INET46
1172 * exists, or destroying INET46 when INET exists
1174 for (sepp = servtab; sepp; sepp = sepp->se_next) {
1177 if (sepp->se_checked == 0 ||
1179 strcmp(sep->se_proto, sepp->se_proto) != 0 ||
1180 sep->se_rpc_prog != sepp->se_rpc_prog)
1182 if (sepp->se_family == AF_INET)
1184 if (sepp->se_family == AF_INET6) {
1186 if (!sep->se_nomapped)
1189 if (netid4 == NULL && netid6 == NULL)
1193 print_service("UNREG", sep);
1194 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1196 rpcb_unset(sep->se_rpc_prog, i, netid4);
1198 rpcb_unset(sep->se_rpc_prog, i, netid6);
1200 if (sep->se_fd != -1)
1201 (void) close(sep->se_fd);
1203 (void) sigsetmask(omask);
1207 flag_retry(int signo __unused)
1215 struct servtab *sep;
1218 for (sep = servtab; sep; sep = sep->se_next)
1219 if (sep->se_fd == -1 && !ISMUX(sep))
1224 setup(struct servtab *sep)
1228 if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
1230 warn("socket failed on %s/%s",
1231 sep->se_service, sep->se_proto);
1232 syslog(LOG_ERR, "%s/%s: socket: %m",
1233 sep->se_service, sep->se_proto);
1236 #define turnon(fd, opt) \
1237 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
1238 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
1239 turnon(sep->se_fd, SO_DEBUG) < 0)
1240 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
1241 if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
1242 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
1244 if (turnon(sep->se_fd, SO_PRIVSTATE) < 0)
1245 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m");
1247 /* tftpd opens a new connection then needs more infos */
1248 if ((sep->se_family == AF_INET6) &&
1249 (strcmp(sep->se_proto, "udp") == 0) &&
1250 (sep->se_accept == 0) &&
1251 (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_PKTINFO,
1252 (char *)&on, sizeof (on)) < 0))
1253 syslog(LOG_ERR, "setsockopt (IPV6_RECVPKTINFO): %m");
1254 if (sep->se_family == AF_INET6) {
1255 int flag = sep->se_nomapped ? 1 : 0;
1256 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_V6ONLY,
1257 (char *)&flag, sizeof (flag)) < 0)
1258 syslog(LOG_ERR, "setsockopt (IPV6_V6ONLY): %m");
1261 if (sep->se_type == TTCP_TYPE)
1262 if (setsockopt(sep->se_fd, IPPROTO_TCP, TCP_NOPUSH,
1263 (char *)&on, sizeof (on)) < 0)
1264 syslog(LOG_ERR, "setsockopt (TCP_NOPUSH): %m");
1266 if (sep->se_type == FAITH_TYPE) {
1267 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_FAITH, &on,
1269 syslog(LOG_ERR, "setsockopt (IPV6_FAITH): %m");
1276 if (sep->se_family == AF_UNIX) {
1277 (void) unlink(sep->se_ctrladdr_un.sun_path);
1278 umask(0777); /* Make socket with conservative permissions */
1280 if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr,
1281 sep->se_ctrladdr_size) < 0) {
1283 warn("bind failed on %s/%s",
1284 sep->se_service, sep->se_proto);
1285 syslog(LOG_ERR, "%s/%s: bind: %m",
1286 sep->se_service, sep->se_proto);
1287 (void) close(sep->se_fd);
1293 if (sep->se_family == AF_UNIX)
1297 if (sep->se_family == AF_UNIX) {
1298 /* Ick - fch{own,mod} don't work on Unix domain sockets */
1299 if (chown(sep->se_service, sep->se_sockuid, sep->se_sockgid) < 0)
1300 syslog(LOG_ERR, "chown socket: %m");
1301 if (chmod(sep->se_service, sep->se_sockmode) < 0)
1302 syslog(LOG_ERR, "chmod socket: %m");
1307 socklen_t len = sep->se_ctrladdr_size;
1308 struct netconfig *netid, *netid2 = NULL;
1309 struct sockaddr_in sock;
1310 struct netbuf nbuf, nbuf2;
1312 if (getsockname(sep->se_fd,
1313 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){
1314 syslog(LOG_ERR, "%s/%s: getsockname: %m",
1315 sep->se_service, sep->se_proto);
1316 (void) close(sep->se_fd);
1320 nbuf.buf = &sep->se_ctrladdr;
1321 nbuf.len = sep->se_ctrladdr.sa_len;
1322 if (sep->se_family == AF_INET)
1323 netid = sep->se_socktype==SOCK_DGRAM? udpconf:tcpconf;
1325 netid = sep->se_socktype==SOCK_DGRAM? udp6conf:tcp6conf;
1326 if (!sep->se_nomapped) { /* INET and INET6 */
1327 netid2 = netid==udp6conf? udpconf:tcpconf;
1328 memset(&sock, 0, sizeof sock); /* ADDR_ANY */
1330 nbuf2.len = sock.sin_len = sizeof sock;
1331 sock.sin_family = AF_INET;
1332 sock.sin_port = sep->se_ctrladdr6.sin6_port;
1336 print_service("REG ", sep);
1337 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1338 rpcb_unset(sep->se_rpc_prog, i, netid);
1339 rpcb_set(sep->se_rpc_prog, i, netid, &nbuf);
1341 rpcb_unset(sep->se_rpc_prog, i, netid2);
1342 rpcb_set(sep->se_rpc_prog, i, netid2, &nbuf2);
1346 if (sep->se_socktype == SOCK_STREAM)
1347 listen(sep->se_fd, 64);
1350 warnx("registered %s on %d",
1351 sep->se_server, sep->se_fd);
1358 struct servtab *sep;
1361 char *policy_in = NULL;
1362 char *policy_out = NULL;
1366 switch (sep->se_family) {
1369 opt = IP_IPSEC_POLICY;
1373 level = IPPROTO_IPV6;
1374 opt = IPV6_IPSEC_POLICY;
1381 if (!sep->se_policy || sep->se_policy[0] == '\0') {
1382 static char def_in[] = "in entrust", def_out[] = "out entrust";
1384 policy_out = def_out;
1386 if (!strncmp("in", sep->se_policy, 2))
1387 policy_in = sep->se_policy;
1388 else if (!strncmp("out", sep->se_policy, 3))
1389 policy_out = sep->se_policy;
1391 syslog(LOG_ERR, "invalid security policy \"%s\"",
1397 if (policy_in != NULL) {
1398 buf = ipsec_set_policy(policy_in, strlen(policy_in));
1400 if (setsockopt(sep->se_fd, level, opt,
1401 buf, ipsec_get_policylen(buf)) < 0 &&
1403 warnx("%s/%s: ipsec initialization failed; %s",
1404 sep->se_service, sep->se_proto,
1408 syslog(LOG_ERR, "invalid security policy \"%s\"",
1411 if (policy_out != NULL) {
1412 buf = ipsec_set_policy(policy_out, strlen(policy_out));
1414 if (setsockopt(sep->se_fd, level, opt,
1415 buf, ipsec_get_policylen(buf)) < 0 &&
1417 warnx("%s/%s: ipsec initialization failed; %s",
1418 sep->se_service, sep->se_proto,
1422 syslog(LOG_ERR, "invalid security policy \"%s\"",
1429 * Finish with a service and its socket.
1432 close_sep(struct servtab *sep)
1434 if (sep->se_fd >= 0) {
1435 if (FD_ISSET(sep->se_fd, &allsock))
1437 (void) close(sep->se_fd);
1441 sep->se_numchild = 0; /* forget about any existing children */
1445 matchservent(const char *name1, const char *name2, const char *proto)
1450 if (strcmp(proto, "unix") == 0) {
1451 if ((p = strrchr(name1, '/')) != NULL)
1453 if ((p = strrchr(name2, '/')) != NULL)
1456 if (strcmp(name1, name2) == 0)
1458 if ((se = getservbyname(name1, proto)) != NULL) {
1459 if (strcmp(name2, se->s_name) == 0)
1461 for (alias = se->s_aliases; *alias; alias++)
1462 if (strcmp(name2, *alias) == 0)
1469 enter(struct servtab *cp)
1471 struct servtab *sep;
1474 sep = (struct servtab *)malloc(sizeof (*sep));
1475 if (sep == (struct servtab *)0) {
1476 syslog(LOG_ERR, "malloc: %m");
1481 omask = sigblock(SIGBLOCK);
1482 sep->se_next = servtab;
1489 enable(struct servtab *sep)
1493 "enabling %s, fd %d", sep->se_service, sep->se_fd);
1495 if (sep->se_fd < 0) {
1497 "%s: %s: bad fd", __FUNCTION__, sep->se_service);
1502 "%s: %s: is mux", __FUNCTION__, sep->se_service);
1505 if (FD_ISSET(sep->se_fd, &allsock)) {
1507 "%s: %s: not off", __FUNCTION__, sep->se_service);
1512 FD_SET(sep->se_fd, &allsock);
1513 if (sep->se_fd > maxsock)
1514 maxsock = sep->se_fd;
1518 disable(struct servtab *sep)
1522 "disabling %s, fd %d", sep->se_service, sep->se_fd);
1524 if (sep->se_fd < 0) {
1526 "%s: %s: bad fd", __FUNCTION__, sep->se_service);
1531 "%s: %s: is mux", __FUNCTION__, sep->se_service);
1534 if (!FD_ISSET(sep->se_fd, &allsock)) {
1536 "%s: %s: not on", __FUNCTION__, sep->se_service);
1540 syslog(LOG_ERR, "%s: nsock=0", __FUNCTION__);
1545 FD_CLR(sep->se_fd, &allsock);
1546 if (sep->se_fd == maxsock)
1550 FILE *fconfig = NULL;
1551 struct servtab serv;
1552 char line[LINE_MAX];
1558 if (fconfig != NULL) {
1559 fseek(fconfig, 0L, SEEK_SET);
1562 fconfig = fopen(CONFIG, "r");
1563 return (fconfig != NULL);
1570 (void) fclose(fconfig);
1578 struct servtab *sep = &serv;
1582 static char TCPMUX_TOKEN[] = "tcpmux/";
1583 #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
1585 char *policy = NULL;
1594 while ((cp = nextline(fconfig)) != NULL) {
1596 /* lines starting with #@ is not a comment, but the policy */
1597 if (cp[0] == '#' && cp[1] == '@') {
1599 for (p = cp + 2; p && *p && isspace(*p); p++)
1605 } else if (ipsec_get_policylen(p) >= 0) {
1611 "%s: invalid ipsec policy \"%s\"",
1617 if (*cp == '#' || *cp == '\0')
1622 return ((struct servtab *)0);
1624 * clear the static buffer, since some fields (se_ctrladdr,
1625 * for example) don't get initialized here.
1627 memset(sep, 0, sizeof *sep);
1630 /* got an empty line containing just blanks/tabs. */
1633 if (arg[0] == ':') { /* :user:group:perm: */
1634 char *user, *group, *perm;
1638 if ((group = strchr(user, ':')) == NULL) {
1639 syslog(LOG_ERR, "no group after user '%s'", user);
1643 if ((perm = strchr(group, ':')) == NULL) {
1644 syslog(LOG_ERR, "no mode after group '%s'", group);
1648 if ((pw = getpwnam(user)) == NULL) {
1649 syslog(LOG_ERR, "no such user '%s'", user);
1652 sep->se_sockuid = pw->pw_uid;
1653 if ((gr = getgrnam(group)) == NULL) {
1654 syslog(LOG_ERR, "no such user '%s'", group);
1657 sep->se_sockgid = gr->gr_gid;
1658 sep->se_sockmode = strtol(perm, &arg, 8);
1660 syslog(LOG_ERR, "bad mode '%s'", perm);
1665 sep->se_sockuid = euid;
1666 sep->se_sockgid = egid;
1667 sep->se_sockmode = 0200;
1669 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
1670 char *c = arg + MUX_LEN;
1672 sep->se_type = MUXPLUS_TYPE;
1675 sep->se_type = MUX_TYPE;
1676 sep->se_service = newstr(c);
1678 sep->se_service = newstr(arg);
1679 sep->se_type = NORM_TYPE;
1682 if (strcmp(arg, "stream") == 0)
1683 sep->se_socktype = SOCK_STREAM;
1684 else if (strcmp(arg, "dgram") == 0)
1685 sep->se_socktype = SOCK_DGRAM;
1686 else if (strcmp(arg, "rdm") == 0)
1687 sep->se_socktype = SOCK_RDM;
1688 else if (strcmp(arg, "seqpacket") == 0)
1689 sep->se_socktype = SOCK_SEQPACKET;
1690 else if (strcmp(arg, "raw") == 0)
1691 sep->se_socktype = SOCK_RAW;
1693 sep->se_socktype = -1;
1696 if (strncmp(arg, "tcp", 3) == 0) {
1697 sep->se_proto = newstr(strsep(&arg, "/"));
1699 if (strcmp(arg, "ttcp") == 0)
1700 sep->se_type = TTCP_TYPE;
1701 else if (strcmp(arg, "faith") == 0)
1702 sep->se_type = FAITH_TYPE;
1705 if (sep->se_type == NORM_TYPE &&
1706 strncmp(arg, "faith/", 6) == 0) {
1708 sep->se_type = FAITH_TYPE;
1710 sep->se_proto = newstr(arg);
1712 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1713 memmove(sep->se_proto, sep->se_proto + 4,
1714 strlen(sep->se_proto) + 1 - 4);
1716 sep->se_rpc_prog = sep->se_rpc_lowvers =
1717 sep->se_rpc_lowvers = 0;
1718 memcpy(&sep->se_ctrladdr4, bind_sa4,
1719 sizeof(sep->se_ctrladdr4));
1720 if ((versp = rindex(sep->se_service, '/'))) {
1722 switch (sscanf(versp, "%d-%d",
1723 &sep->se_rpc_lowvers,
1724 &sep->se_rpc_highvers)) {
1728 sep->se_rpc_highvers =
1729 sep->se_rpc_lowvers;
1733 "bad RPC version specifier; %s",
1740 sep->se_rpc_lowvers =
1741 sep->se_rpc_highvers = 1;
1744 sep->se_nomapped = 0;
1745 while (isdigit(sep->se_proto[strlen(sep->se_proto) - 1])) {
1747 if (sep->se_proto[strlen(sep->se_proto) - 1] == '6') {
1748 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1753 if (sep->se_proto[strlen(sep->se_proto) - 1] == '4') {
1754 sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1758 /* illegal version num */
1759 syslog(LOG_ERR, "bad IP version for %s", sep->se_proto);
1763 if (strcmp(sep->se_proto, "unix") == 0) {
1764 sep->se_family = AF_UNIX;
1767 if (v6bind != 0 && no_v6bind != 0) {
1768 syslog(LOG_INFO, "IPv6 bind is ignored for %s",
1770 if (v4bind && no_v4bind == 0)
1778 sep->se_family = AF_INET6;
1779 if (v4bind == 0 || no_v4bind != 0)
1780 sep->se_nomapped = 1;
1783 { /* default to v4 bind if not v6 bind */
1784 if (no_v4bind != 0) {
1785 syslog(LOG_NOTICE, "IPv4 bind is ignored for %s",
1790 sep->se_family = AF_INET;
1793 switch(sep->se_family) {
1795 memcpy(&sep->se_ctrladdr4, bind_sa4,
1796 sizeof(sep->se_ctrladdr4));
1797 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr4);
1801 memcpy(&sep->se_ctrladdr6, bind_sa6,
1802 sizeof(sep->se_ctrladdr6));
1803 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr6);
1807 if (strlen(sep->se_service) >= sizeof(sep->se_ctrladdr_un.sun_path)) {
1809 "domain socket pathname too long for service %s",
1813 memset(&sep->se_ctrladdr, 0, sizeof(sep->se_ctrladdr));
1814 sep->se_ctrladdr_un.sun_family = sep->se_family;
1815 sep->se_ctrladdr_un.sun_len = strlen(sep->se_service);
1816 strcpy(sep->se_ctrladdr_un.sun_path, sep->se_service);
1817 sep->se_ctrladdr_size = SUN_LEN(&sep->se_ctrladdr_un);
1820 if (!strncmp(arg, "wait", 4))
1822 else if (!strncmp(arg, "nowait", 6))
1826 "%s: bad wait/nowait for service %s",
1827 CONFIG, sep->se_service);
1830 sep->se_maxchild = -1;
1831 sep->se_maxcpm = -1;
1832 sep->se_maxperip = -1;
1833 if ((s = strchr(arg, '/')) != NULL) {
1837 val = strtoul(s + 1, &eptr, 10);
1838 if (eptr == s + 1 || val > MAX_MAXCHLD) {
1840 "%s: bad max-child for service %s",
1841 CONFIG, sep->se_service);
1845 if (!sep->se_accept && val != 1)
1846 warnx("maxchild=%lu for wait service %s"
1847 " not recommended", val, sep->se_service);
1848 sep->se_maxchild = val;
1850 sep->se_maxcpm = strtol(eptr + 1, &eptr, 10);
1852 sep->se_maxperip = strtol(eptr + 1, &eptr, 10);
1854 * explicitly do not check for \0 for future expansion /
1855 * backwards compatibility
1860 * Silently enforce "nowait" mode for TCPMUX services
1861 * since they don't have an assigned port to listen on.
1864 if (strcmp(sep->se_proto, "tcp")) {
1866 "%s: bad protocol for tcpmux service %s",
1867 CONFIG, sep->se_service);
1870 if (sep->se_socktype != SOCK_STREAM) {
1872 "%s: bad socket type for tcpmux service %s",
1873 CONFIG, sep->se_service);
1877 sep->se_user = newstr(sskip(&cp));
1879 if ((s = strrchr(sep->se_user, '/')) != NULL) {
1881 sep->se_class = newstr(s + 1);
1883 sep->se_class = newstr(RESOURCE_RC);
1885 if ((s = strrchr(sep->se_user, ':')) != NULL) {
1887 sep->se_group = newstr(s + 1);
1889 sep->se_group = NULL;
1890 sep->se_server = newstr(sskip(&cp));
1891 if ((sep->se_server_name = rindex(sep->se_server, '/')))
1892 sep->se_server_name++;
1893 if (strcmp(sep->se_server, "internal") == 0) {
1896 for (bi = biltins; bi->bi_service; bi++)
1897 if (bi->bi_socktype == sep->se_socktype &&
1898 matchservent(bi->bi_service, sep->se_service,
1901 if (bi->bi_service == 0) {
1902 syslog(LOG_ERR, "internal service %s unknown",
1906 sep->se_accept = 1; /* force accept mode for built-ins */
1910 if (sep->se_maxperip < 0)
1911 sep->se_maxperip = maxperip;
1912 if (sep->se_maxcpm < 0)
1913 sep->se_maxcpm = maxcpm;
1914 if (sep->se_maxchild < 0) { /* apply default max-children */
1915 if (sep->se_bi && sep->se_bi->bi_maxchild >= 0)
1916 sep->se_maxchild = sep->se_bi->bi_maxchild;
1917 else if (sep->se_accept)
1918 sep->se_maxchild = maxchild > 0 ? maxchild : 0;
1920 sep->se_maxchild = 1;
1922 if (sep->se_maxchild > 0) {
1923 sep->se_pids = malloc(sep->se_maxchild * sizeof(*sep->se_pids));
1924 if (sep->se_pids == NULL) {
1925 syslog(LOG_ERR, "malloc: %m");
1930 for (arg = skip(&cp); cp; arg = skip(&cp))
1931 if (argc < MAXARGV) {
1932 sep->se_argv[argc++] = newstr(arg);
1935 "%s: too many arguments for service %s",
1936 CONFIG, sep->se_service);
1939 while (argc <= MAXARGV)
1940 sep->se_argv[argc++] = NULL;
1941 for (i = 0; i < PERIPSIZE; ++i)
1942 LIST_INIT(&sep->se_conn[i]);
1944 sep->se_policy = policy ? newstr(policy) : NULL;
1950 freeconfig(struct servtab *cp)
1955 free(cp->se_service);
1967 free(cp->se_server);
1970 for (i = 0; i < MAXARGV; i++)
1972 free(cp->se_argv[i]);
1976 free(cp->se_policy);
1982 * Safe skip - if skip returns null, log a syntax error in the
1983 * configuration file and exit.
1992 syslog(LOG_ERR, "%s: syntax error", CONFIG);
2006 while (*cp == ' ' || *cp == '\t')
2012 (void) ungetc(c, fconfig);
2013 if (c == ' ' || c == '\t')
2014 if ((cp = nextline(fconfig)))
2019 if (*cp == '"' || *cp == '\'')
2023 while (*cp && *cp != quote)
2026 while (*cp && *cp != ' ' && *cp != '\t')
2039 if (fgets(line, sizeof (line), fd) == NULL)
2041 cp = strchr(line, '\n');
2048 newstr(const char *cp)
2052 if ((cr = strdup(cp != NULL ? cp : "")))
2054 syslog(LOG_ERR, "strdup: %m");
2059 inetd_setproctitle(const char *a, int s)
2062 struct sockaddr_storage ss;
2063 char buf[80], pbuf[INET6_ADDRSTRLEN];
2066 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) {
2067 getnameinfo((struct sockaddr *)&ss, size, pbuf, sizeof(pbuf),
2068 NULL, 0, NI_NUMERICHOST|NI_WITHSCOPEID);
2069 (void) sprintf(buf, "%s [%s]", a, pbuf);
2071 (void) sprintf(buf, "%s", a);
2072 setproctitle("%s", buf);
2076 check_loop(const struct sockaddr *sa, const struct servtab *sep)
2078 struct servtab *se2;
2079 char pname[INET6_ADDRSTRLEN];
2081 for (se2 = servtab; se2; se2 = se2->se_next) {
2082 if (!se2->se_bi || se2->se_socktype != SOCK_DGRAM)
2085 switch (se2->se_family) {
2087 if (((const struct sockaddr_in *)sa)->sin_port ==
2088 se2->se_ctrladdr4.sin_port)
2093 if (((const struct sockaddr_in *)sa)->sin_port ==
2094 se2->se_ctrladdr4.sin_port)
2102 getnameinfo(sa, sa->sa_len, pname, sizeof(pname), NULL, 0,
2103 NI_NUMERICHOST|NI_WITHSCOPEID);
2104 syslog(LOG_WARNING, "%s/%s:%s/%s loop request REFUSED from %s",
2105 sep->se_service, sep->se_proto,
2106 se2->se_service, se2->se_proto,
2115 * Dump relevant information to stderr
2118 print_service(const char *action, const struct servtab *sep)
2121 "%s: %s proto=%s accept=%d max=%d user=%s group=%s"
2125 " builtin=%p server=%s"
2130 action, sep->se_service, sep->se_proto,
2131 sep->se_accept, sep->se_maxchild, sep->se_user, sep->se_group,
2135 (void *) sep->se_bi, sep->se_server
2137 , (sep->se_policy ? sep->se_policy : "")
2142 #define CPMHSIZE 256
2143 #define CPMHMASK (CPMHSIZE-1)
2147 typedef struct CTime {
2148 unsigned long ct_Ticks;
2152 typedef struct CHash {
2154 struct in_addr c4_Addr;
2155 struct in6_addr c6_Addr;
2157 #define ch_Addr4 cu_Addr.c4_Addr
2158 #define ch_Addr6 cu_Addr.c6_Addr
2162 CTime ch_Times[CHTSIZE];
2165 CHash CHashAry[CPMHSIZE];
2168 cpmip(const struct servtab *sep, int ctrl)
2170 struct sockaddr_storage rss;
2171 socklen_t rssLen = sizeof(rss);
2175 * If getpeername() fails, just let it through (if logging is
2176 * enabled the condition is caught elsewhere)
2179 if (sep->se_maxcpm > 0 &&
2180 getpeername(ctrl, (struct sockaddr *)&rss, &rssLen) == 0 ) {
2181 time_t t = time(NULL);
2182 int hv = 0xABC3D20F;
2185 CHash *chBest = NULL;
2186 unsigned int ticks = t / CHTGRAN;
2187 struct sockaddr_in *sin4;
2189 struct sockaddr_in6 *sin6;
2192 sin4 = (struct sockaddr_in *)&rss;
2194 sin6 = (struct sockaddr_in6 *)&rss;
2200 switch (rss.ss_family) {
2202 p = (char *)&sin4->sin_addr;
2203 addrlen = sizeof(struct in_addr);
2207 p = (char *)&sin6->sin6_addr;
2208 addrlen = sizeof(struct in6_addr);
2212 /* should not happen */
2216 for (i = 0; i < addrlen; ++i, ++p) {
2217 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2219 hv = (hv ^ (hv >> 16));
2221 for (i = 0; i < 5; ++i) {
2222 CHash *ch = &CHashAry[(hv + i) & CPMHMASK];
2224 if (rss.ss_family == AF_INET &&
2225 ch->ch_Family == AF_INET &&
2226 sin4->sin_addr.s_addr == ch->ch_Addr4.s_addr &&
2227 ch->ch_Service && strcmp(sep->se_service,
2228 ch->ch_Service) == 0) {
2233 if (rss.ss_family == AF_INET6 &&
2234 ch->ch_Family == AF_INET6 &&
2235 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2236 &ch->ch_Addr6) != 0 &&
2237 ch->ch_Service && strcmp(sep->se_service,
2238 ch->ch_Service) == 0) {
2243 if (chBest == NULL || ch->ch_LTime == 0 ||
2244 ch->ch_LTime < chBest->ch_LTime) {
2248 if ((rss.ss_family == AF_INET &&
2249 (chBest->ch_Family != AF_INET ||
2250 sin4->sin_addr.s_addr != chBest->ch_Addr4.s_addr)) ||
2251 chBest->ch_Service == NULL ||
2252 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2253 chBest->ch_Family = sin4->sin_family;
2254 chBest->ch_Addr4 = sin4->sin_addr;
2255 if (chBest->ch_Service)
2256 free(chBest->ch_Service);
2257 chBest->ch_Service = strdup(sep->se_service);
2258 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2261 if ((rss.ss_family == AF_INET6 &&
2262 (chBest->ch_Family != AF_INET6 ||
2263 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2264 &chBest->ch_Addr6) == 0)) ||
2265 chBest->ch_Service == NULL ||
2266 strcmp(sep->se_service, chBest->ch_Service) != 0) {
2267 chBest->ch_Family = sin6->sin6_family;
2268 chBest->ch_Addr6 = sin6->sin6_addr;
2269 if (chBest->ch_Service)
2270 free(chBest->ch_Service);
2271 chBest->ch_Service = strdup(sep->se_service);
2272 bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2275 chBest->ch_LTime = t;
2277 CTime *ct = &chBest->ch_Times[ticks % CHTSIZE];
2278 if (ct->ct_Ticks != ticks) {
2279 ct->ct_Ticks = ticks;
2284 for (i = 0; i < CHTSIZE; ++i) {
2285 CTime *ct = &chBest->ch_Times[i];
2286 if (ct->ct_Ticks <= ticks &&
2287 ct->ct_Ticks >= ticks - CHTSIZE) {
2288 cnt += ct->ct_Count;
2291 if (cnt * (CHTSIZE * CHTGRAN) / 60 > sep->se_maxcpm) {
2292 char pname[INET6_ADDRSTRLEN];
2294 getnameinfo((struct sockaddr *)&rss,
2295 ((struct sockaddr *)&rss)->sa_len,
2296 pname, sizeof(pname), NULL, 0,
2297 NI_NUMERICHOST|NI_WITHSCOPEID);
2300 "%s from %s exceeded counts/min (limit %d/min)",
2301 sep->se_service, pname,
2308 static struct conninfo *
2309 search_conn(struct servtab *sep, int ctrl)
2311 struct sockaddr_storage ss;
2312 socklen_t sslen = sizeof(ss);
2313 struct conninfo *conn;
2315 char pname[NI_MAXHOST], pname2[NI_MAXHOST];
2317 if (sep->se_maxperip <= 0)
2321 * If getpeername() fails, just let it through (if logging is
2322 * enabled the condition is caught elsewhere)
2324 if (getpeername(ctrl, (struct sockaddr *)&ss, &sslen) != 0)
2327 switch (ss.ss_family) {
2329 hv = hashval((char *)&((struct sockaddr_in *)&ss)->sin_addr,
2330 sizeof(struct in_addr));
2334 hv = hashval((char *)&((struct sockaddr_in6 *)&ss)->sin6_addr,
2335 sizeof(struct in6_addr));
2340 * Since we only support AF_INET and AF_INET6, just
2341 * let other than AF_INET and AF_INET6 through.
2346 if (getnameinfo((struct sockaddr *)&ss, sslen, pname, sizeof(pname),
2347 NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0)
2350 LIST_FOREACH(conn, &sep->se_conn[hv], co_link) {
2351 if (getnameinfo((struct sockaddr *)&conn->co_addr,
2352 conn->co_addr.ss_len, pname2, sizeof(pname2), NULL, 0,
2353 NI_NUMERICHOST | NI_WITHSCOPEID) == 0 &&
2354 strcmp(pname, pname2) == 0)
2359 if ((conn = malloc(sizeof(struct conninfo))) == NULL) {
2360 syslog(LOG_ERR, "malloc: %m");
2363 conn->co_proc = malloc(sep->se_maxperip * sizeof(*conn->co_proc));
2364 if (conn->co_proc == NULL) {
2365 syslog(LOG_ERR, "malloc: %m");
2368 memcpy(&conn->co_addr, (struct sockaddr *)&ss, sslen);
2369 conn->co_numchild = 0;
2370 LIST_INSERT_HEAD(&sep->se_conn[hv], conn, co_link);
2374 * Since a child process is not invoked yet, we cannot
2375 * determine a pid of a child. So, co_proc and co_numchild
2376 * should be filled leter.
2383 room_conn(struct servtab *sep, struct conninfo *conn)
2385 char pname[NI_MAXHOST];
2387 if (conn->co_numchild >= sep->se_maxperip) {
2388 getnameinfo((struct sockaddr *)&conn->co_addr,
2389 conn->co_addr.ss_len, pname, sizeof(pname), NULL, 0,
2390 NI_NUMERICHOST | NI_WITHSCOPEID);
2391 syslog(LOG_ERR, "%s from %s exceeded counts (limit %d)",
2392 sep->se_service, pname, sep->se_maxperip);
2399 addchild_conn(struct conninfo *conn, pid_t pid)
2401 struct procinfo *proc;
2406 if ((proc = search_proc(pid, 1)) != NULL) {
2407 if (proc->pr_conn != NULL) {
2409 "addchild_conn: child already on process list");
2412 proc->pr_conn = conn;
2415 conn->co_proc[conn->co_numchild++] = proc;
2419 reapchild_conn(pid_t pid)
2421 struct procinfo *proc;
2422 struct conninfo *conn;
2425 if ((proc = search_proc(pid, 0)) == NULL)
2427 if ((conn = proc->pr_conn) == NULL)
2429 for (i = 0; i < conn->co_numchild; ++i)
2430 if (conn->co_proc[i] == proc) {
2431 conn->co_proc[i] = conn->co_proc[--conn->co_numchild];
2439 resize_conn(struct servtab *sep, int maxperip)
2441 struct conninfo *conn;
2444 if (sep->se_maxperip <= 0)
2446 if (maxperip <= 0) {
2450 for (i = 0; i < PERIPSIZE; ++i) {
2451 LIST_FOREACH(conn, &sep->se_conn[i], co_link) {
2452 for (j = maxperip; j < conn->co_numchild; ++j)
2453 free_proc(conn->co_proc[j]);
2454 conn->co_proc = realloc(conn->co_proc,
2455 maxperip * sizeof(*conn->co_proc));
2456 if (conn->co_proc == NULL) {
2457 syslog(LOG_ERR, "realloc: %m");
2460 if (conn->co_numchild > maxperip)
2461 conn->co_numchild = maxperip;
2467 free_connlist(struct servtab *sep)
2469 struct conninfo *conn;
2472 for (i = 0; i < PERIPSIZE; ++i) {
2473 while ((conn = LIST_FIRST(&sep->se_conn[i])) != NULL) {
2474 for (j = 0; j < conn->co_numchild; ++j)
2475 free_proc(conn->co_proc[j]);
2476 conn->co_numchild = 0;
2483 free_conn(struct conninfo *conn)
2487 if (conn->co_numchild <= 0) {
2488 LIST_REMOVE(conn, co_link);
2489 free(conn->co_proc);
2494 static struct procinfo *
2495 search_proc(pid_t pid, int add)
2497 struct procinfo *proc;
2500 hv = hashval((char *)&pid, sizeof(pid));
2501 LIST_FOREACH(proc, &proctable[hv], pr_link) {
2502 if (proc->pr_pid == pid)
2505 if (proc == NULL && add) {
2506 if ((proc = malloc(sizeof(struct procinfo))) == NULL) {
2507 syslog(LOG_ERR, "malloc: %m");
2511 proc->pr_conn = NULL;
2512 LIST_INSERT_HEAD(&proctable[hv], proc, pr_link);
2518 free_proc(struct procinfo *proc)
2522 LIST_REMOVE(proc, pr_link);
2527 hashval(char *p, int len)
2529 int i, hv = 0xABC3D20F;
2531 for (i = 0; i < len; ++i, ++p)
2532 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2533 hv = (hv ^ (hv >> 16)) & (PERIPSIZE - 1);