]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/inetd/inetd.c
This commit was generated by cvs2svn to compensate for changes in r98944,
[FreeBSD/FreeBSD.git] / usr.sbin / inetd / inetd.c
1 /*
2  * Copyright (c) 1983, 1991, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
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";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)from: inetd.c       8.4 (Berkeley) 4/13/94";
43 #endif
44 #endif /* not lint */
45
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48
49 /*
50  * Inetd - Internet super-server
51  *
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.
56  *
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''.
65  *
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.
71  *
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)
81  *
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
95  * for new requests.
96  *
97  * For RPC services
98  *      service name/version            must be in /etc/rpc
99  *      socket type                     stream/dgram/raw/rdm/seqpacket
100  *      protocol                        rpc/tcp, rpc/udp
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
105  *
106  * Comment lines are indicated by a `#' in column 1.
107  *
108  * #ifdef IPSEC
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.
113  * #endif
114  */
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>
121 #include <sys/un.h>
122
123 #include <netinet/in.h>
124 #include <netinet/tcp.h>
125 #include <arpa/inet.h>
126 #include <rpc/rpc.h>
127 #include <rpc/pmap_clnt.h>
128
129 #include <errno.h>
130 #include <err.h>
131 #include <fcntl.h>
132 #include <grp.h>
133 #include <netdb.h>
134 #include <pwd.h>
135 #include <signal.h>
136 #include <stdio.h>
137 #include <stdlib.h>
138 #include <string.h>
139 #include <syslog.h>
140 #include <tcpd.h>
141 #include <unistd.h>
142 #include <libutil.h>
143 #include <sysexits.h>
144 #include <ctype.h>
145
146 #include "inetd.h"
147 #include "pathnames.h"
148
149 #ifdef IPSEC
150 #include <netinet6/ipsec.h>
151 #ifndef IPSEC_POLICY_IPSEC      /* no ipsec support on old ipsec */
152 #undef IPSEC
153 #endif
154 #endif
155
156 /* wrapper for KAME-special getnameinfo() */
157 #ifndef NI_WITHSCOPEID
158 #define NI_WITHSCOPEID  0
159 #endif
160
161 #ifndef LIBWRAP_ALLOW_FACILITY
162 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
163 #endif
164 #ifndef LIBWRAP_ALLOW_SEVERITY
165 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
166 #endif
167 #ifndef LIBWRAP_DENY_FACILITY
168 # define LIBWRAP_DENY_FACILITY LOG_AUTH
169 #endif
170 #ifndef LIBWRAP_DENY_SEVERITY
171 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
172 #endif
173
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))
179
180 #ifdef LOGIN_CAP
181 #include <login_cap.h>
182
183 /* see init.c */
184 #define RESOURCE_RC "daemon"
185
186 #endif
187
188 #ifndef MAXCHILD
189 #define MAXCHILD        -1              /* maximum number of this service
190                                            < 0 = no limit */
191 #endif
192
193 #ifndef MAXCPM
194 #define MAXCPM          -1              /* rate limit invocations from a
195                                            single remote address,
196                                            < 0 = no limit */
197 #endif
198
199 #ifndef TOOMANY
200 #define TOOMANY         256             /* don't start more than TOOMANY */
201 #endif
202 #define CNT_INTVL       60              /* servers in CNT_INTVL sec. */
203 #define RETRYTIME       (60*10)         /* retry after bind or server fail */
204 #define MAX_MAXCHLD     32767           /* max allowable max children */
205
206 #define SIGBLOCK        (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
207
208 void            close_sep(struct servtab *);
209 void            flag_signal(int);
210 void            flag_config(int);
211 void            config(void);
212 int             cpmip(const struct servtab *, int);
213 void            endconfig(void);
214 struct servtab *enter(struct servtab *);
215 void            freeconfig(struct servtab *);
216 struct servtab *getconfigent(void);
217 int             matchservent(const char *, const char *, const char *);
218 char           *nextline(FILE *);
219 void            addchild(struct servtab *, int);
220 void            flag_reapchild(int);
221 void            reapchild(void);
222 void            enable(struct servtab *);
223 void            disable(struct servtab *);
224 void            flag_retry(int);
225 void            retry(void);
226 int             setconfig(void);
227 void            setup(struct servtab *);
228 #ifdef IPSEC
229 void            ipsecsetup(struct servtab *);
230 #endif
231 void            unregisterrpc(register struct servtab *sep);
232
233 int     allow_severity;
234 int     deny_severity;
235 int     wrap_ex = 0;
236 int     wrap_bi = 0;
237 int     debug = 0;
238 int     log = 0;
239 int     maxsock;                        /* highest-numbered descriptor */
240 fd_set  allsock;
241 int     options;
242 int     timingout;
243 int     toomany = TOOMANY;
244 int     maxchild = MAXCHILD;
245 int     maxcpm = MAXCPM;
246 struct  servent *sp;
247 struct  rpcent *rpc;
248 char    *hostname = NULL;
249 struct  sockaddr_in *bind_sa4;
250 int     no_v4bind = 1;
251 #ifdef INET6
252 struct  sockaddr_in6 *bind_sa6;
253 int     no_v6bind = 1;
254 #endif
255 int     signalpipe[2];
256 #ifdef SANITY_CHECK
257 int     nsock;
258 #endif
259 uid_t   euid;
260 gid_t   egid;
261 mode_t  mask;
262
263 struct  servtab *servtab;
264
265 extern struct biltin biltins[];
266
267 const char      *CONFIG = _PATH_INETDCONF;
268 const char      *pid_file = _PATH_INETDPID;
269
270 int
271 getvalue(const char *arg, int *value, const char *whine)
272 {
273         int  tmp;
274         char *p;
275
276         tmp = strtol(arg, &p, 0);
277         if (tmp < 0 || *p) {
278                 syslog(LOG_ERR, whine, arg);
279                 return 1;                       /* failure */
280         }
281         *value = tmp;
282         return 0;                               /* success */
283 }
284
285 int
286 main(int argc, char **argv)
287 {
288         struct servtab *sep;
289         struct passwd *pwd;
290         struct group *grp;
291         struct sigaction sa, saalrm, sachld, sahup, sapipe;
292         int tmpint, ch, dofork;
293         pid_t pid;
294         char buf[50];
295 #ifdef LOGIN_CAP
296         login_cap_t *lc = NULL;
297 #endif
298         struct request_info req;
299         int denied;
300         char *service = NULL;
301         union {
302                 struct sockaddr peer_un;
303                 struct sockaddr_in peer_un4;
304                 struct sockaddr_in6 peer_un6;
305                 struct sockaddr_storage peer_max;
306         } p_un;
307 #define peer    p_un.peer_un
308 #define peer4   p_un.peer_un4
309 #define peer6   p_un.peer_un6
310 #define peermax p_un.peer_max
311         int i;
312         struct addrinfo hints, *res;
313         const char *servname;
314         int error;
315
316         openlog("inetd", LOG_PID | LOG_NOWAIT | LOG_PERROR, LOG_DAEMON);
317
318         while ((ch = getopt(argc, argv, "dlwWR:a:c:C:p:")) != -1)
319                 switch(ch) {
320                 case 'd':
321                         debug = 1;
322                         options |= SO_DEBUG;
323                         break;
324                 case 'l':
325                         log = 1;
326                         break;
327                 case 'R':
328                         getvalue(optarg, &toomany,
329                                 "-R %s: bad value for service invocation rate");
330                         break;
331                 case 'c':
332                         getvalue(optarg, &maxchild,
333                                 "-c %s: bad value for maximum children");
334                         break;
335                 case 'C':
336                         getvalue(optarg, &maxcpm,
337                                 "-C %s: bad value for maximum children/minute");
338                         break;
339                 case 'a':
340                         hostname = optarg;
341                         break;
342                 case 'p':
343                         pid_file = optarg;
344                         break;
345                 case 'w':
346                         wrap_ex++;
347                         break;
348                 case 'W':
349                         wrap_bi++;
350                         break;
351                 case '?':
352                 default:
353                         syslog(LOG_ERR,
354                                 "usage: inetd [-dlwW] [-a address] [-R rate]"
355                                 " [-c maximum] [-C rate]"
356                                 " [-p pidfile] [conf-file]");
357                         exit(EX_USAGE);
358                 }
359         /*
360          * Initialize Bind Addrs.
361          *   When hostname is NULL, wild card bind addrs are obtained from
362          *   getaddrinfo(). But getaddrinfo() requires at least one of
363          *   hostname or servname is non NULL.
364          *   So when hostname is NULL, set dummy value to servname.
365          */
366         servname = (hostname == NULL) ? "discard" /* dummy */ : NULL;
367
368         bzero(&hints, sizeof(struct addrinfo));
369         hints.ai_flags = AI_PASSIVE;
370         hints.ai_family = AF_UNSPEC;
371         error = getaddrinfo(hostname, servname, &hints, &res);
372         if (error != 0) {
373                 syslog(LOG_ERR, "-a %s: %s", hostname, gai_strerror(error));
374                 if (error == EAI_SYSTEM)
375                         syslog(LOG_ERR, "%s", strerror(errno));
376                 exit(EX_USAGE);
377         }
378         do {
379                 if (res->ai_addr == NULL) {
380                         syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
381                         exit(EX_USAGE);
382                 }
383                 switch (res->ai_addr->sa_family) {
384                 case AF_INET:
385                         if (no_v4bind == 0)
386                                 continue;
387                         bind_sa4 = (struct sockaddr_in *)res->ai_addr;
388                         /* init port num in case servname is dummy */
389                         bind_sa4->sin_port = 0;
390                         no_v4bind = 0;
391                         continue;
392 #ifdef INET6
393                 case AF_INET6:
394                         if (no_v6bind == 0)
395                                 continue;
396                         bind_sa6 = (struct sockaddr_in6 *)res->ai_addr;
397                         /* init port num in case servname is dummy */
398                         bind_sa6->sin6_port = 0;
399                         no_v6bind = 0;
400                         continue;
401 #endif
402                 }
403                 if (no_v4bind == 0
404 #ifdef INET6
405                     && no_v6bind == 0
406 #endif
407                     )
408                         break;
409         } while ((res = res->ai_next) != NULL);
410         if (no_v4bind != 0
411 #ifdef INET6
412             && no_v6bind != 0
413 #endif
414             ) {
415                 syslog(LOG_ERR, "-a %s: unknown address family", hostname);
416                 exit(EX_USAGE);
417         }
418
419         euid = geteuid();
420         egid = getegid();
421         umask(mask = umask(0777));
422
423         argc -= optind;
424         argv += optind;
425
426         if (argc > 0)
427                 CONFIG = argv[0];
428         if (debug == 0) {
429                 FILE *fp;
430                 if (daemon(0, 0) < 0) {
431                         syslog(LOG_WARNING, "daemon(0,0) failed: %m");
432                 }
433                 /* From now on we don't want syslog messages going to stderr. */
434                 closelog();
435                 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
436                 /*
437                  * In case somebody has started inetd manually, we need to
438                  * clear the logname, so that old servers run as root do not
439                  * get the user's logname..
440                  */
441                 if (setlogin("") < 0) {
442                         syslog(LOG_WARNING, "cannot clear logname: %m");
443                         /* no big deal if it fails.. */
444                 }
445                 pid = getpid();
446                 fp = fopen(pid_file, "w");
447                 if (fp) {
448                         fprintf(fp, "%ld\n", (long)pid);
449                         fclose(fp);
450                 } else {
451                         syslog(LOG_WARNING, "%s: %m", pid_file);
452                 }
453         }
454         sa.sa_flags = 0;
455         sigemptyset(&sa.sa_mask);
456         sigaddset(&sa.sa_mask, SIGALRM);
457         sigaddset(&sa.sa_mask, SIGCHLD);
458         sigaddset(&sa.sa_mask, SIGHUP);
459         sa.sa_handler = flag_retry;
460         sigaction(SIGALRM, &sa, &saalrm);
461         config();
462         sa.sa_handler = flag_config;
463         sigaction(SIGHUP, &sa, &sahup);
464         sa.sa_handler = flag_reapchild;
465         sigaction(SIGCHLD, &sa, &sachld);
466         sa.sa_handler = SIG_IGN;
467         sigaction(SIGPIPE, &sa, &sapipe);
468
469         {
470                 /* space for daemons to overwrite environment for ps */
471 #define DUMMYSIZE       100
472                 char dummy[DUMMYSIZE];
473
474                 (void)memset(dummy, 'x', DUMMYSIZE - 1);
475                 dummy[DUMMYSIZE - 1] = '\0';
476                 (void)setenv("inetd_dummy", dummy, 1);
477         }
478
479         if (pipe(signalpipe) != 0) {
480                 syslog(LOG_ERR, "pipe: %m");
481                 exit(EX_OSERR);
482         }
483         FD_SET(signalpipe[0], &allsock);
484 #ifdef SANITY_CHECK
485         nsock++;
486 #endif
487         if (signalpipe[0] > maxsock)
488             maxsock = signalpipe[0];
489         if (signalpipe[1] > maxsock)
490             maxsock = signalpipe[1];
491
492         for (;;) {
493             int n, ctrl;
494             fd_set readable;
495
496 #ifdef SANITY_CHECK
497             if (nsock == 0) {
498                 syslog(LOG_ERR, "%s: nsock=0", __FUNCTION__);
499                 exit(EX_SOFTWARE);
500             }
501 #endif
502             readable = allsock;
503             if ((n = select(maxsock + 1, &readable, (fd_set *)0,
504                 (fd_set *)0, (struct timeval *)0)) <= 0) {
505                     if (n < 0 && errno != EINTR) {
506                         syslog(LOG_WARNING, "select: %m");
507                         sleep(1);
508                     }
509                     continue;
510             }
511             /* handle any queued signal flags */
512             if (FD_ISSET(signalpipe[0], &readable)) {
513                 int nsig;
514                 if (ioctl(signalpipe[0], FIONREAD, &nsig) != 0) {
515                     syslog(LOG_ERR, "ioctl: %m");
516                     exit(EX_OSERR);
517                 }
518                 while (--nsig >= 0) {
519                     char c;
520                     if (read(signalpipe[0], &c, 1) != 1) {
521                         syslog(LOG_ERR, "read: %m");
522                         exit(EX_OSERR);
523                     }
524                     if (debug)
525                         warnx("handling signal flag %c", c);
526                     switch(c) {
527                     case 'A': /* sigalrm */
528                         retry();
529                         break;
530                     case 'C': /* sigchld */
531                         reapchild();
532                         break;
533                     case 'H': /* sighup */
534                         config();
535                         break;
536                     }
537                 }
538             }
539             for (sep = servtab; n && sep; sep = sep->se_next)
540                 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
541                     n--;
542                     if (debug)
543                             warnx("someone wants %s", sep->se_service);
544                     if (sep->se_accept && sep->se_socktype == SOCK_STREAM) {
545                             i = 1;
546                             if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
547                                     syslog(LOG_ERR, "ioctl (FIONBIO, 1): %m");
548                             ctrl = accept(sep->se_fd, (struct sockaddr *)0,
549                                 (socklen_t *)0);
550                             if (debug)
551                                     warnx("accept, ctrl %d", ctrl);
552                             if (ctrl < 0) {
553                                     if (errno != EINTR)
554                                             syslog(LOG_WARNING,
555                                                 "accept (for %s): %m",
556                                                 sep->se_service);
557                                       if (sep->se_accept &&
558                                           sep->se_socktype == SOCK_STREAM)
559                                               close(ctrl);
560                                     continue;
561                             }
562                             i = 0;
563                             if (ioctl(sep->se_fd, FIONBIO, &i) < 0)
564                                     syslog(LOG_ERR, "ioctl1(FIONBIO, 0): %m");
565                             if (ioctl(ctrl, FIONBIO, &i) < 0)
566                                     syslog(LOG_ERR, "ioctl2(FIONBIO, 0): %m");
567                             if (cpmip(sep, ctrl) < 0) {
568                                 close(ctrl);
569                                 continue;
570                             }
571                     } else
572                             ctrl = sep->se_fd;
573                     if (log && !ISWRAP(sep)) {
574                             char pname[INET6_ADDRSTRLEN] = "unknown";
575                             socklen_t sl;
576                             sl = sizeof peermax;
577                             if (getpeername(ctrl, (struct sockaddr *)
578                                             &peermax, &sl)) {
579                                     sl = sizeof peermax;
580                                     if (recvfrom(ctrl, buf, sizeof(buf),
581                                         MSG_PEEK,
582                                         (struct sockaddr *)&peermax,
583                                         &sl) >= 0) {
584                                       getnameinfo((struct sockaddr *)&peermax,
585                                                   peer.sa_len,
586                                                   pname, sizeof(pname),
587                                                   NULL, 0, 
588                                                   NI_NUMERICHOST|
589                                                   NI_WITHSCOPEID);
590                                     }
591                             } else {
592                                     getnameinfo((struct sockaddr *)&peermax,
593                                                 peer.sa_len,
594                                                 pname, sizeof(pname),
595                                                 NULL, 0, 
596                                                 NI_NUMERICHOST|
597                                                 NI_WITHSCOPEID);
598                             }
599                             syslog(LOG_INFO,"%s from %s", sep->se_service, pname);
600                     }
601                     (void) sigblock(SIGBLOCK);
602                     pid = 0;
603                     /*
604                      * Fork for all external services, builtins which need to
605                      * fork and anything we're wrapping (as wrapping might
606                      * block or use hosts_options(5) twist).
607                      */
608                     dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep);
609                     if (dofork) {
610                             if (sep->se_count++ == 0)
611                                 (void)gettimeofday(&sep->se_time, (struct timezone *)NULL);
612                             else if (toomany > 0 && sep->se_count >= toomany) {
613                                 struct timeval now;
614
615                                 (void)gettimeofday(&now, (struct timezone *)NULL);
616                                 if (now.tv_sec - sep->se_time.tv_sec >
617                                     CNT_INTVL) {
618                                         sep->se_time = now;
619                                         sep->se_count = 1;
620                                 } else {
621                                         syslog(LOG_ERR,
622                         "%s/%s server failing (looping), service terminated",
623                                             sep->se_service, sep->se_proto);
624                                         if (sep->se_accept &&
625                                             sep->se_socktype == SOCK_STREAM)
626                                                 close(ctrl);
627                                         close_sep(sep);
628                                         sigsetmask(0L);
629                                         if (!timingout) {
630                                                 timingout = 1;
631                                                 alarm(RETRYTIME);
632                                         }
633                                         continue;
634                                 }
635                             }
636                             pid = fork();
637                     }
638                     if (pid < 0) {
639                             syslog(LOG_ERR, "fork: %m");
640                             if (sep->se_accept &&
641                                 sep->se_socktype == SOCK_STREAM)
642                                     close(ctrl);
643                             sigsetmask(0L);
644                             sleep(1);
645                             continue;
646                     }
647                     if (pid)
648                         addchild(sep, pid);
649                     sigsetmask(0L);
650                     if (pid == 0) {
651                             if (dofork) {
652                                 if (debug)
653                                         warnx("+ closing from %d", maxsock);
654                                 for (tmpint = maxsock; tmpint > 2; tmpint--)
655                                         if (tmpint != ctrl)
656                                                 (void) close(tmpint);
657                                 sigaction(SIGALRM, &saalrm, (struct sigaction *)0);
658                                 sigaction(SIGCHLD, &sachld, (struct sigaction *)0);
659                                 sigaction(SIGHUP, &sahup, (struct sigaction *)0);
660                                 /* SIGPIPE reset before exec */
661                             }
662                             /*
663                              * Call tcpmux to find the real service to exec.
664                              */
665                             if (sep->se_bi &&
666                                 sep->se_bi->bi_fn == (bi_fn_t *) tcpmux) {
667                                     sep = tcpmux(ctrl);
668                                     if (sep == NULL) {
669                                             close(ctrl);
670                                             _exit(0);
671                                     }
672                             }
673                             if (ISWRAP(sep)) {
674                                 inetd_setproctitle("wrapping", ctrl);
675                                 service = sep->se_server_name ?
676                                     sep->se_server_name : sep->se_service;
677                                 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, NULL);
678                                 fromhost(&req);
679                                 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
680                                 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
681                                 denied = !hosts_access(&req);
682                                 if (denied) {
683                                     syslog(deny_severity,
684                                         "refused connection from %.500s, service %s (%s%s)",
685                                         eval_client(&req), service, sep->se_proto,
686                                         (((struct sockaddr *)req.client->sin)->sa_family == AF_INET6 && !IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)req.client->sin)->sin6_addr)) ? "6" : "");
687                                     if (sep->se_socktype != SOCK_STREAM)
688                                         recv(ctrl, buf, sizeof (buf), 0);
689                                     if (dofork) {
690                                         sleep(1);
691                                         _exit(0);
692                                     }
693                                 }
694                                 if (log) {
695                                     syslog(allow_severity,
696                                         "connection from %.500s, service %s (%s%s)",
697                                         eval_client(&req), service, sep->se_proto,
698                                         (((struct sockaddr *)req.client->sin)->sa_family == AF_INET6 && !IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)req.client->sin)->sin6_addr)) ? "6" : "");
699                                 }
700                             }
701                             if (sep->se_bi) {
702                                 (*sep->se_bi->bi_fn)(ctrl, sep);
703                             } else {
704                                 if (debug)
705                                         warnx("%d execl %s",
706                                                 getpid(), sep->se_server);
707                                 dup2(ctrl, 0);
708                                 close(ctrl);
709                                 dup2(0, 1);
710                                 dup2(0, 2);
711                                 if ((pwd = getpwnam(sep->se_user)) == NULL) {
712                                         syslog(LOG_ERR,
713                                             "%s/%s: %s: no such user",
714                                                 sep->se_service, sep->se_proto,
715                                                 sep->se_user);
716                                         if (sep->se_socktype != SOCK_STREAM)
717                                                 recv(0, buf, sizeof (buf), 0);
718                                         _exit(EX_NOUSER);
719                                 }
720                                 grp = NULL;
721                                 if (   sep->se_group != NULL
722                                     && (grp = getgrnam(sep->se_group)) == NULL
723                                    ) {
724                                         syslog(LOG_ERR,
725                                             "%s/%s: %s: no such group",
726                                                 sep->se_service, sep->se_proto,
727                                                 sep->se_group);
728                                         if (sep->se_socktype != SOCK_STREAM)
729                                                 recv(0, buf, sizeof (buf), 0);
730                                         _exit(EX_NOUSER);
731                                 }
732                                 if (grp != NULL)
733                                         pwd->pw_gid = grp->gr_gid;
734 #ifdef LOGIN_CAP
735                                 if ((lc = login_getclass(sep->se_class)) == NULL) {
736                                         /* error syslogged by getclass */
737                                         syslog(LOG_ERR,
738                                             "%s/%s: %s: login class error",
739                                                 sep->se_service, sep->se_proto,
740                                                 sep->se_class);
741                                         if (sep->se_socktype != SOCK_STREAM)
742                                                 recv(0, buf, sizeof (buf), 0);
743                                         _exit(EX_NOUSER);
744                                 }
745 #endif
746                                 if (setsid() < 0) {
747                                         syslog(LOG_ERR,
748                                                 "%s: can't setsid(): %m",
749                                                  sep->se_service);
750                                         /* _exit(EX_OSERR); not fatal yet */
751                                 }
752 #ifdef LOGIN_CAP
753                                 if (setusercontext(lc, pwd, pwd->pw_uid,
754                                     LOGIN_SETALL) != 0) {
755                                         syslog(LOG_ERR,
756                                          "%s: can't setusercontext(..%s..): %m",
757                                          sep->se_service, sep->se_user);
758                                         _exit(EX_OSERR);
759                                 }
760 #else
761                                 if (pwd->pw_uid) {
762                                         if (setlogin(sep->se_user) < 0) {
763                                                 syslog(LOG_ERR,
764                                                  "%s: can't setlogin(%s): %m",
765                                                  sep->se_service, sep->se_user);
766                                                 /* _exit(EX_OSERR); not yet */
767                                         }
768                                         if (setgid(pwd->pw_gid) < 0) {
769                                                 syslog(LOG_ERR,
770                                                   "%s: can't set gid %d: %m",
771                                                   sep->se_service, pwd->pw_gid);
772                                                 _exit(EX_OSERR);
773                                         }
774                                         (void) initgroups(pwd->pw_name,
775                                                         pwd->pw_gid);
776                                         if (setuid(pwd->pw_uid) < 0) {
777                                                 syslog(LOG_ERR,
778                                                   "%s: can't set uid %d: %m",
779                                                   sep->se_service, pwd->pw_uid);
780                                                 _exit(EX_OSERR);
781                                         }
782                                 }
783 #endif
784                                 sigaction(SIGPIPE, &sapipe,
785                                     (struct sigaction *)0);
786                                 execv(sep->se_server, sep->se_argv);
787                                 syslog(LOG_ERR,
788                                     "cannot execute %s: %m", sep->se_server);
789                                 if (sep->se_socktype != SOCK_STREAM)
790                                         recv(0, buf, sizeof (buf), 0);
791                             }
792                             if (dofork)
793                                 _exit(0);
794                     }
795                     if (sep->se_accept && sep->se_socktype == SOCK_STREAM)
796                             close(ctrl);
797                 }
798         }
799 }
800
801 /*
802  * Add a signal flag to the signal flag queue for later handling
803  */
804
805 void
806 flag_signal(int c)
807 {
808         char ch = c;
809
810         if (write(signalpipe[1], &ch, 1) != 1) {
811                 syslog(LOG_ERR, "write: %m");
812                 _exit(EX_OSERR);
813         }
814 }
815
816 /*
817  * Record a new child pid for this service. If we've reached the
818  * limit on children, then stop accepting incoming requests.
819  */
820
821 void
822 addchild(struct servtab *sep, pid_t pid)
823 {
824         if (sep->se_maxchild <= 0)
825                 return;
826 #ifdef SANITY_CHECK
827         if (sep->se_numchild >= sep->se_maxchild) {
828                 syslog(LOG_ERR, "%s: %d >= %d",
829                     __FUNCTION__, sep->se_numchild, sep->se_maxchild);
830                 exit(EX_SOFTWARE);
831         }
832 #endif
833         sep->se_pids[sep->se_numchild++] = pid;
834         if (sep->se_numchild == sep->se_maxchild)
835                 disable(sep);
836 }
837
838 /*
839  * Some child process has exited. See if it's on somebody's list.
840  */
841
842 void
843 flag_reapchild(int signo __unused)
844 {
845         flag_signal('C');
846 }
847
848 void
849 reapchild(void)
850 {
851         int k, status;
852         pid_t pid;
853         struct servtab *sep;
854
855         for (;;) {
856                 pid = wait3(&status, WNOHANG, (struct rusage *)0);
857                 if (pid <= 0)
858                         break;
859                 if (debug)
860                         warnx("%d reaped, status %#x", pid, status);
861                 for (sep = servtab; sep; sep = sep->se_next) {
862                         for (k = 0; k < sep->se_numchild; k++)
863                                 if (sep->se_pids[k] == pid)
864                                         break;
865                         if (k == sep->se_numchild)
866                                 continue;
867                         if (sep->se_numchild == sep->se_maxchild)
868                                 enable(sep);
869                         sep->se_pids[k] = sep->se_pids[--sep->se_numchild];
870                         if (status)
871                                 syslog(LOG_WARNING,
872                                     "%s[%d]: exit status 0x%x",
873                                     sep->se_server, pid, status);
874                         break;
875                 }
876         }
877 }
878
879 void
880 flag_config(int signo __unused)
881 {
882         flag_signal('H');
883 }
884
885 void
886 config(void)
887 {
888         struct servtab *sep, *new, **sepp;
889         long omask;
890
891         if (!setconfig()) {
892                 syslog(LOG_ERR, "%s: %m", CONFIG);
893                 return;
894         }
895         for (sep = servtab; sep; sep = sep->se_next)
896                 sep->se_checked = 0;
897         while ((new = getconfigent())) {
898                 if (getpwnam(new->se_user) == NULL) {
899                         syslog(LOG_ERR,
900                                 "%s/%s: no such user '%s', service ignored",
901                                 new->se_service, new->se_proto, new->se_user);
902                         continue;
903                 }
904                 if (new->se_group && getgrnam(new->se_group) == NULL) {
905                         syslog(LOG_ERR,
906                                 "%s/%s: no such group '%s', service ignored",
907                                 new->se_service, new->se_proto, new->se_group);
908                         continue;
909                 }
910 #ifdef LOGIN_CAP
911                 if (login_getclass(new->se_class) == NULL) {
912                         /* error syslogged by getclass */
913                         syslog(LOG_ERR,
914                                 "%s/%s: %s: login class error, service ignored",
915                                 new->se_service, new->se_proto, new->se_class);
916                         continue;
917                 }
918 #endif
919                 for (sep = servtab; sep; sep = sep->se_next)
920                         if (strcmp(sep->se_service, new->se_service) == 0 &&
921                             strcmp(sep->se_proto, new->se_proto) == 0 &&
922                             sep->se_socktype == new->se_socktype &&
923                             sep->se_family == new->se_family)
924                                 break;
925                 if (sep != 0) {
926                         int i;
927
928 #define SWAP(t,a, b) { t c = a; a = b; b = c; }
929                         omask = sigblock(SIGBLOCK);
930                         if (sep->se_nomapped != new->se_nomapped) {
931                                 sep->se_nomapped = new->se_nomapped;
932                                 sep->se_reset = 1;
933                         }
934                         /* copy over outstanding child pids */
935                         if (sep->se_maxchild > 0 && new->se_maxchild > 0) {
936                                 new->se_numchild = sep->se_numchild;
937                                 if (new->se_numchild > new->se_maxchild)
938                                         new->se_numchild = new->se_maxchild;
939                                 memcpy(new->se_pids, sep->se_pids,
940                                     new->se_numchild * sizeof(*new->se_pids));
941                         }
942                         SWAP(pid_t *, sep->se_pids, new->se_pids);
943                         sep->se_maxchild = new->se_maxchild;
944                         sep->se_numchild = new->se_numchild;
945                         sep->se_maxcpm = new->se_maxcpm;
946                         sep->se_bi = new->se_bi;
947                         /* might need to turn on or off service now */
948                         if (sep->se_fd >= 0) {
949                               if (sep->se_maxchild > 0
950                                   && sep->se_numchild == sep->se_maxchild) {
951                                       if (FD_ISSET(sep->se_fd, &allsock))
952                                           disable(sep);
953                               } else {
954                                       if (!FD_ISSET(sep->se_fd, &allsock))
955                                           enable(sep);
956                               }
957                         }
958                         sep->se_accept = new->se_accept;
959                         SWAP(char *, sep->se_user, new->se_user);
960                         SWAP(char *, sep->se_group, new->se_group);
961 #ifdef LOGIN_CAP
962                         SWAP(char *, sep->se_class, new->se_class);
963 #endif
964                         SWAP(char *, sep->se_server, new->se_server);
965                         SWAP(char *, sep->se_server_name, new->se_server_name);
966                         for (i = 0; i < MAXARGV; i++)
967                                 SWAP(char *, sep->se_argv[i], new->se_argv[i]);
968 #ifdef IPSEC
969                         SWAP(char *, sep->se_policy, new->se_policy);
970                         ipsecsetup(sep);
971 #endif
972                         sigsetmask(omask);
973                         freeconfig(new);
974                         if (debug)
975                                 print_service("REDO", sep);
976                 } else {
977                         sep = enter(new);
978                         if (debug)
979                                 print_service("ADD ", sep);
980                 }
981                 sep->se_checked = 1;
982                 if (ISMUX(sep)) {
983                         sep->se_fd = -1;
984                         continue;
985                 }
986                 switch (sep->se_family) {
987                 case AF_INET:
988                         if (no_v4bind != 0) {
989                                 sep->se_fd = -1;
990                                 continue;
991                         }
992                         break;
993 #ifdef INET6
994                 case AF_INET6:
995                         if (no_v6bind != 0) {
996                                 sep->se_fd = -1;
997                                 continue;
998                         }
999                         break;
1000 #endif
1001                 }
1002                 if (!sep->se_rpc) {
1003                         if (sep->se_family != AF_UNIX) {
1004                                 sp = getservbyname(sep->se_service, sep->se_proto);
1005                                 if (sp == 0) {
1006                                         syslog(LOG_ERR, "%s/%s: unknown service",
1007                                         sep->se_service, sep->se_proto);
1008                                         sep->se_checked = 0;
1009                                         continue;
1010                                 }
1011                         }
1012                         switch (sep->se_family) {
1013                         case AF_INET:
1014                                 if (sp->s_port != sep->se_ctrladdr4.sin_port) {
1015                                         sep->se_ctrladdr4.sin_port =
1016                                                 sp->s_port;
1017                                         sep->se_reset = 1;
1018                                 }
1019                                 break;
1020 #ifdef INET6
1021                         case AF_INET6:
1022                                 if (sp->s_port !=
1023                                     sep->se_ctrladdr6.sin6_port) {
1024                                         sep->se_ctrladdr6.sin6_port =
1025                                                 sp->s_port;
1026                                         sep->se_reset = 1;
1027                                 }
1028                                 break;
1029 #endif
1030                         }
1031                         if (sep->se_reset != 0 && sep->se_fd >= 0)
1032                                 close_sep(sep);
1033                 } else {
1034                         rpc = getrpcbyname(sep->se_service);
1035                         if (rpc == 0) {
1036                                 syslog(LOG_ERR, "%s/%s unknown RPC service",
1037                                         sep->se_service, sep->se_proto);
1038                                 if (sep->se_fd != -1)
1039                                         (void) close(sep->se_fd);
1040                                 sep->se_fd = -1;
1041                                         continue;
1042                         }
1043                         if (rpc->r_number != sep->se_rpc_prog) {
1044                                 if (sep->se_rpc_prog)
1045                                         unregisterrpc(sep);
1046                                 sep->se_rpc_prog = rpc->r_number;
1047                                 if (sep->se_fd != -1)
1048                                         (void) close(sep->se_fd);
1049                                 sep->se_fd = -1;
1050                         }
1051                 }
1052                 if (sep->se_fd == -1)
1053                         setup(sep);
1054         }
1055         endconfig();
1056         /*
1057          * Purge anything not looked at above.
1058          */
1059         omask = sigblock(SIGBLOCK);
1060         sepp = &servtab;
1061         while ((sep = *sepp)) {
1062                 if (sep->se_checked) {
1063                         sepp = &sep->se_next;
1064                         continue;
1065                 }
1066                 *sepp = sep->se_next;
1067                 if (sep->se_fd >= 0)
1068                         close_sep(sep);
1069                 if (debug)
1070                         print_service("FREE", sep);
1071                 if (sep->se_rpc && sep->se_rpc_prog > 0)
1072                         unregisterrpc(sep);
1073                 freeconfig(sep);
1074                 free(sep);
1075         }
1076         (void) sigsetmask(omask);
1077 }
1078
1079 void
1080 unregisterrpc(struct servtab *sep)
1081 {
1082         u_int i;
1083         struct servtab *sepp;
1084         long omask;
1085
1086         omask = sigblock(SIGBLOCK);
1087         for (sepp = servtab; sepp; sepp = sepp->se_next) {
1088                 if (sepp == sep)
1089                         continue;
1090                 if (sep->se_checked == 0 ||
1091                     !sepp->se_rpc ||
1092                     sep->se_rpc_prog != sepp->se_rpc_prog)
1093                         continue;
1094                 return;
1095         }
1096         if (debug)
1097                 print_service("UNREG", sep);
1098         for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++)
1099                 pmap_unset(sep->se_rpc_prog, i);
1100         if (sep->se_fd != -1)
1101                 (void) close(sep->se_fd);
1102         sep->se_fd = -1;
1103         (void) sigsetmask(omask);
1104 }
1105
1106 void
1107 flag_retry(int signo __unused)
1108 {
1109         flag_signal('A');
1110 }
1111
1112 void
1113 retry(void)
1114 {
1115         struct servtab *sep;
1116
1117         timingout = 0;
1118         for (sep = servtab; sep; sep = sep->se_next)
1119                 if (sep->se_fd == -1 && !ISMUX(sep))
1120                         setup(sep);
1121 }
1122
1123 void
1124 setup(struct servtab *sep)
1125 {
1126         int on = 1;
1127
1128         if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
1129                 if (debug)
1130                         warn("socket failed on %s/%s",
1131                                 sep->se_service, sep->se_proto);
1132                 syslog(LOG_ERR, "%s/%s: socket: %m",
1133                     sep->se_service, sep->se_proto);
1134                 return;
1135         }
1136 #define turnon(fd, opt) \
1137 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
1138         if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
1139             turnon(sep->se_fd, SO_DEBUG) < 0)
1140                 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
1141         if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
1142                 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
1143 #ifdef SO_PRIVSTATE
1144         if (turnon(sep->se_fd, SO_PRIVSTATE) < 0)
1145                 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m");
1146 #endif
1147         /* tftpd opens a new connection then needs more infos */
1148         if ((sep->se_family == AF_INET6) &&
1149             (strcmp(sep->se_proto, "udp") == 0) &&
1150             (sep->se_accept == 0) &&
1151             (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_PKTINFO,
1152                         (char *)&on, sizeof (on)) < 0))
1153                 syslog(LOG_ERR, "setsockopt (IPV6_RECVPKTINFO): %m");
1154 #ifdef IPV6_BINDV6ONLY
1155         if (sep->se_family == AF_INET6) {
1156                 int flag = sep->se_nomapped ? 1 : 0;
1157                 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_BINDV6ONLY,
1158                                (char *)&flag, sizeof (flag)) < 0)
1159                         syslog(LOG_ERR, "setsockopt (IPV6_BINDV6ONLY): %m");
1160         }
1161 #endif /* IPV6_BINDV6ONLY */
1162 #undef turnon
1163         if (sep->se_type == TTCP_TYPE)
1164                 if (setsockopt(sep->se_fd, IPPROTO_TCP, TCP_NOPUSH,
1165                     (char *)&on, sizeof (on)) < 0)
1166                         syslog(LOG_ERR, "setsockopt (TCP_NOPUSH): %m");
1167 #ifdef IPV6_FAITH
1168         if (sep->se_type == FAITH_TYPE) {
1169                 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_FAITH, &on,
1170                                 sizeof(on)) < 0) {
1171                         syslog(LOG_ERR, "setsockopt (IPV6_FAITH): %m");
1172                 }
1173         }
1174 #endif
1175 #ifdef IPSEC
1176         ipsecsetup(sep);
1177 #endif
1178         if (sep->se_family == AF_UNIX) {
1179                 (void) unlink(sep->se_ctrladdr_un.sun_path);
1180                 umask(0777); /* Make socket with conservative permissions */
1181         }
1182         if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr,
1183             sep->se_ctrladdr_size) < 0) {
1184                 if (debug)
1185                         warn("bind failed on %s/%s",
1186                                 sep->se_service, sep->se_proto);
1187                 syslog(LOG_ERR, "%s/%s: bind: %m",
1188                     sep->se_service, sep->se_proto);
1189                 (void) close(sep->se_fd);
1190                 sep->se_fd = -1;
1191                 if (!timingout) {
1192                         timingout = 1;
1193                         alarm(RETRYTIME);
1194                 }
1195                 if (sep->se_family == AF_UNIX)
1196                         umask(mask);
1197                 return;
1198         }
1199         if (sep->se_family == AF_UNIX) {
1200                 /* Ick - fch{own,mod} don't work on Unix domain sockets */
1201                 if (chown(sep->se_service, sep->se_sockuid, sep->se_sockgid) < 0)
1202                         syslog(LOG_ERR, "chown socket: %m");
1203                 if (chmod(sep->se_service, sep->se_sockmode) < 0)
1204                         syslog(LOG_ERR, "chmod socket: %m");
1205                 umask(mask);
1206         }
1207         if (sep->se_rpc) {
1208                 u_int i;
1209                 socklen_t len = sep->se_ctrladdr_size;
1210
1211                 if (sep->se_family != AF_INET) {
1212                         syslog(LOG_ERR,
1213                                "%s/%s: unsupported address family for rpc",
1214                                sep->se_service, sep->se_proto);
1215                         (void) close(sep->se_fd);
1216                         sep->se_fd = -1;
1217                         return;
1218                 }
1219                 if (getsockname(sep->se_fd,
1220                                 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){
1221                         syslog(LOG_ERR, "%s/%s: getsockname: %m",
1222                                sep->se_service, sep->se_proto);
1223                         (void) close(sep->se_fd);
1224                         sep->se_fd = -1;
1225                         return;
1226                 }
1227                 if (debug)
1228                         print_service("REG ", sep);
1229                 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) {
1230                         pmap_unset(sep->se_rpc_prog, i);
1231                         pmap_set(sep->se_rpc_prog, i,
1232                                  (sep->se_socktype == SOCK_DGRAM)
1233                                  ? IPPROTO_UDP : IPPROTO_TCP,
1234                                  ntohs(sep->se_ctrladdr4.sin_port));
1235                 }
1236         }
1237         if (sep->se_socktype == SOCK_STREAM)
1238                 listen(sep->se_fd, 64);
1239         enable(sep);
1240         if (debug) {
1241                 warnx("registered %s on %d",
1242                         sep->se_server, sep->se_fd);
1243         }
1244 }
1245
1246 #ifdef IPSEC
1247 void
1248 ipsecsetup(sep)
1249         struct servtab *sep;
1250 {
1251         char *buf;
1252         char *policy_in = NULL;
1253         char *policy_out = NULL;
1254         int level;
1255         int opt;
1256
1257         switch (sep->se_family) {
1258         case AF_INET:
1259                 level = IPPROTO_IP;
1260                 opt = IP_IPSEC_POLICY;
1261                 break;
1262 #ifdef INET6
1263         case AF_INET6:
1264                 level = IPPROTO_IPV6;
1265                 opt = IPV6_IPSEC_POLICY;
1266                 break;
1267 #endif
1268         default:
1269                 return;
1270         }
1271
1272         if (!sep->se_policy || sep->se_policy[0] == '\0') {
1273                 static char def_in[] = "in entrust", def_out[] = "out entrust";
1274                 policy_in = def_in;
1275                 policy_out = def_out;
1276         } else {
1277                 if (!strncmp("in", sep->se_policy, 2))
1278                         policy_in = sep->se_policy;
1279                 else if (!strncmp("out", sep->se_policy, 3))
1280                         policy_out = sep->se_policy;
1281                 else {
1282                         syslog(LOG_ERR, "invalid security policy \"%s\"",
1283                                 sep->se_policy);
1284                         return;
1285                 }
1286         }
1287
1288         if (policy_in != NULL) {
1289                 buf = ipsec_set_policy(policy_in, strlen(policy_in));
1290                 if (buf != NULL) {
1291                         if (setsockopt(sep->se_fd, level, opt,
1292                                         buf, ipsec_get_policylen(buf)) < 0 &&
1293                             debug != 0)
1294                                 warnx("%s/%s: ipsec initialization failed; %s",
1295                                       sep->se_service, sep->se_proto,
1296                                       policy_in);
1297                         free(buf);
1298                 } else
1299                         syslog(LOG_ERR, "invalid security policy \"%s\"",
1300                                 policy_in);
1301         }
1302         if (policy_out != NULL) {
1303                 buf = ipsec_set_policy(policy_out, strlen(policy_out));
1304                 if (buf != NULL) {
1305                         if (setsockopt(sep->se_fd, level, opt,
1306                                         buf, ipsec_get_policylen(buf)) < 0 &&
1307                             debug != 0)
1308                                 warnx("%s/%s: ipsec initialization failed; %s",
1309                                       sep->se_service, sep->se_proto,
1310                                       policy_out);
1311                         free(buf);
1312                 } else
1313                         syslog(LOG_ERR, "invalid security policy \"%s\"",
1314                                 policy_out);
1315         }
1316 }
1317 #endif
1318
1319 /*
1320  * Finish with a service and its socket.
1321  */
1322 void
1323 close_sep(struct servtab *sep)
1324 {
1325         if (sep->se_fd >= 0) {
1326                 if (FD_ISSET(sep->se_fd, &allsock))
1327                         disable(sep);
1328                 (void) close(sep->se_fd);
1329                 sep->se_fd = -1;
1330         }
1331         sep->se_count = 0;
1332         sep->se_numchild = 0;   /* forget about any existing children */
1333 }
1334
1335 int
1336 matchservent(const char *name1, const char *name2, const char *proto)
1337 {
1338         char **alias, *p;
1339         struct servent *se;
1340
1341         if (strcmp(proto, "unix") == 0) {
1342                 if ((p = strrchr(name1, '/')) != NULL)
1343                         name1 = p + 1;
1344                 if ((p = strrchr(name2, '/')) != NULL)
1345                         name2 = p + 1;
1346         }
1347         if (strcmp(name1, name2) == 0)
1348                 return(1);
1349         if ((se = getservbyname(name1, proto)) != NULL) {
1350                 if (strcmp(name2, se->s_name) == 0)
1351                         return(1);
1352                 for (alias = se->s_aliases; *alias; alias++)
1353                         if (strcmp(name2, *alias) == 0)
1354                                 return(1);
1355         }
1356         return(0);
1357 }
1358
1359 struct servtab *
1360 enter(struct servtab *cp)
1361 {
1362         struct servtab *sep;
1363         long omask;
1364
1365         sep = (struct servtab *)malloc(sizeof (*sep));
1366         if (sep == (struct servtab *)0) {
1367                 syslog(LOG_ERR, "malloc: %m");
1368                 exit(EX_OSERR);
1369         }
1370         *sep = *cp;
1371         sep->se_fd = -1;
1372         omask = sigblock(SIGBLOCK);
1373         sep->se_next = servtab;
1374         servtab = sep;
1375         sigsetmask(omask);
1376         return (sep);
1377 }
1378
1379 void
1380 enable(struct servtab *sep)
1381 {
1382         if (debug)
1383                 warnx(
1384                     "enabling %s, fd %d", sep->se_service, sep->se_fd);
1385 #ifdef SANITY_CHECK
1386         if (sep->se_fd < 0) {
1387                 syslog(LOG_ERR,
1388                     "%s: %s: bad fd", __FUNCTION__, sep->se_service);
1389                 exit(EX_SOFTWARE);
1390         }
1391         if (ISMUX(sep)) {
1392                 syslog(LOG_ERR,
1393                     "%s: %s: is mux", __FUNCTION__, sep->se_service);
1394                 exit(EX_SOFTWARE);
1395         }
1396         if (FD_ISSET(sep->se_fd, &allsock)) {
1397                 syslog(LOG_ERR,
1398                     "%s: %s: not off", __FUNCTION__, sep->se_service);
1399                 exit(EX_SOFTWARE);
1400         }
1401         nsock++;
1402 #endif
1403         FD_SET(sep->se_fd, &allsock);
1404         if (sep->se_fd > maxsock)
1405                 maxsock = sep->se_fd;
1406 }
1407
1408 void
1409 disable(struct servtab *sep)
1410 {
1411         if (debug)
1412                 warnx(
1413                     "disabling %s, fd %d", sep->se_service, sep->se_fd);
1414 #ifdef SANITY_CHECK
1415         if (sep->se_fd < 0) {
1416                 syslog(LOG_ERR,
1417                     "%s: %s: bad fd", __FUNCTION__, sep->se_service);
1418                 exit(EX_SOFTWARE);
1419         }
1420         if (ISMUX(sep)) {
1421                 syslog(LOG_ERR,
1422                     "%s: %s: is mux", __FUNCTION__, sep->se_service);
1423                 exit(EX_SOFTWARE);
1424         }
1425         if (!FD_ISSET(sep->se_fd, &allsock)) {
1426                 syslog(LOG_ERR,
1427                     "%s: %s: not on", __FUNCTION__, sep->se_service);
1428                 exit(EX_SOFTWARE);
1429         }
1430         if (nsock == 0) {
1431                 syslog(LOG_ERR, "%s: nsock=0", __FUNCTION__);
1432                 exit(EX_SOFTWARE);
1433         }
1434         nsock--;
1435 #endif
1436         FD_CLR(sep->se_fd, &allsock);
1437         if (sep->se_fd == maxsock)
1438                 maxsock--;
1439 }
1440
1441 FILE    *fconfig = NULL;
1442 struct  servtab serv;
1443 char    line[LINE_MAX];
1444
1445 int
1446 setconfig(void)
1447 {
1448
1449         if (fconfig != NULL) {
1450                 fseek(fconfig, 0L, SEEK_SET);
1451                 return (1);
1452         }
1453         fconfig = fopen(CONFIG, "r");
1454         return (fconfig != NULL);
1455 }
1456
1457 void
1458 endconfig(void)
1459 {
1460         if (fconfig) {
1461                 (void) fclose(fconfig);
1462                 fconfig = NULL;
1463         }
1464 }
1465
1466 struct servtab *
1467 getconfigent(void)
1468 {
1469         struct servtab *sep = &serv;
1470         int argc;
1471         char *cp, *arg, *s;
1472         char *versp;
1473         static char TCPMUX_TOKEN[] = "tcpmux/";
1474 #define MUX_LEN         (sizeof(TCPMUX_TOKEN)-1)
1475 #ifdef IPSEC
1476         char *policy = NULL;
1477 #endif
1478         int v4bind = 0;
1479 #ifdef INET6
1480         int v6bind = 0;
1481 #endif
1482
1483 more:
1484         while ((cp = nextline(fconfig)) != NULL) {
1485 #ifdef IPSEC
1486                 /* lines starting with #@ is not a comment, but the policy */
1487                 if (cp[0] == '#' && cp[1] == '@') {
1488                         char *p;
1489                         for (p = cp + 2; p && *p && isspace(*p); p++)
1490                                 ;
1491                         if (*p == '\0') {
1492                                 if (policy)
1493                                         free(policy);
1494                                 policy = NULL;
1495                         } else if (ipsec_get_policylen(p) >= 0) {
1496                                 if (policy)
1497                                         free(policy);
1498                                 policy = newstr(p);
1499                         } else {
1500                                 syslog(LOG_ERR,
1501                                         "%s: invalid ipsec policy \"%s\"",
1502                                         CONFIG, p);
1503                                 exit(EX_CONFIG);
1504                         }
1505                 }
1506 #endif
1507                 if (*cp == '#' || *cp == '\0')
1508                         continue;
1509                 break;
1510         }
1511         if (cp == NULL)
1512                 return ((struct servtab *)0);
1513         /*
1514          * clear the static buffer, since some fields (se_ctrladdr,
1515          * for example) don't get initialized here.
1516          */
1517         memset(sep, 0, sizeof *sep);
1518         arg = skip(&cp);
1519         if (cp == NULL) {
1520                 /* got an empty line containing just blanks/tabs. */
1521                 goto more;
1522         }
1523         if (arg[0] == ':') { /* :user:group:perm: */
1524                 char *user, *group, *perm;
1525                 struct passwd *pw;
1526                 struct group *gr;
1527                 user = arg+1;
1528                 if ((group = strchr(user, ':')) == NULL) {
1529                         syslog(LOG_ERR, "no group after user '%s'", user);
1530                         goto more;
1531                 }
1532                 *group++ = '\0';
1533                 if ((perm = strchr(group, ':')) == NULL) {
1534                         syslog(LOG_ERR, "no mode after group '%s'", group);
1535                         goto more;
1536                 }
1537                 *perm++ = '\0';
1538                 if ((pw = getpwnam(user)) == NULL) {
1539                         syslog(LOG_ERR, "no such user '%s'", user);
1540                         goto more;
1541                 }
1542                 sep->se_sockuid = pw->pw_uid;
1543                 if ((gr = getgrnam(group)) == NULL) {
1544                         syslog(LOG_ERR, "no such user '%s'", group);
1545                         goto more;
1546                 }
1547                 sep->se_sockgid = gr->gr_gid;
1548                 sep->se_sockmode = strtol(perm, &arg, 8);
1549                 if (*arg != ':') {
1550                         syslog(LOG_ERR, "bad mode '%s'", perm);
1551                         goto more;
1552                 }
1553                 *arg++ = '\0';
1554         } else {
1555                 sep->se_sockuid = euid;
1556                 sep->se_sockgid = egid;
1557                 sep->se_sockmode = 0200;
1558         }
1559         if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
1560                 char *c = arg + MUX_LEN;
1561                 if (*c == '+') {
1562                         sep->se_type = MUXPLUS_TYPE;
1563                         c++;
1564                 } else
1565                         sep->se_type = MUX_TYPE;
1566                 sep->se_service = newstr(c);
1567         } else {
1568                 sep->se_service = newstr(arg);
1569                 sep->se_type = NORM_TYPE;
1570         }
1571         arg = sskip(&cp);
1572         if (strcmp(arg, "stream") == 0)
1573                 sep->se_socktype = SOCK_STREAM;
1574         else if (strcmp(arg, "dgram") == 0)
1575                 sep->se_socktype = SOCK_DGRAM;
1576         else if (strcmp(arg, "rdm") == 0)
1577                 sep->se_socktype = SOCK_RDM;
1578         else if (strcmp(arg, "seqpacket") == 0)
1579                 sep->se_socktype = SOCK_SEQPACKET;
1580         else if (strcmp(arg, "raw") == 0)
1581                 sep->se_socktype = SOCK_RAW;
1582         else
1583                 sep->se_socktype = -1;
1584
1585         arg = sskip(&cp);
1586         if (strncmp(arg, "tcp", 3) == 0) {
1587                 sep->se_proto = newstr(strsep(&arg, "/"));
1588                 if (arg != NULL) {
1589                         if (strcmp(arg, "ttcp") == 0)
1590                                 sep->se_type = TTCP_TYPE;
1591                         else if (strcmp(arg, "faith") == 0)
1592                                 sep->se_type = FAITH_TYPE;
1593                 }
1594         } else {
1595                 if (sep->se_type == NORM_TYPE &&
1596                     strncmp(arg, "faith/", 6) == 0) {
1597                         arg += 6;
1598                         sep->se_type = FAITH_TYPE;
1599                 }
1600                 sep->se_proto = newstr(arg);
1601         }
1602         if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1603                 if (no_v4bind != 0) {
1604                         syslog(LOG_NOTICE, "IPv4 bind is ignored for %s",
1605                                sep->se_service);
1606                         freeconfig(sep);
1607                         goto more;
1608                 }
1609                 memmove(sep->se_proto, sep->se_proto + 4,
1610                     strlen(sep->se_proto) + 1 - 4);
1611                 sep->se_rpc = 1;
1612                 sep->se_rpc_prog = sep->se_rpc_lowvers =
1613                         sep->se_rpc_lowvers = 0;
1614                 memcpy(&sep->se_ctrladdr4, bind_sa4,
1615                        sizeof(sep->se_ctrladdr4));
1616                 if ((versp = rindex(sep->se_service, '/'))) {
1617                         *versp++ = '\0';
1618                         switch (sscanf(versp, "%d-%d",
1619                                        &sep->se_rpc_lowvers,
1620                                        &sep->se_rpc_highvers)) {
1621                         case 2:
1622                                 break;
1623                         case 1:
1624                                 sep->se_rpc_highvers =
1625                                         sep->se_rpc_lowvers;
1626                                 break;
1627                         default:
1628                                 syslog(LOG_ERR,
1629                                         "bad RPC version specifier; %s",
1630                                         sep->se_service);
1631                                 freeconfig(sep);
1632                                 goto more;
1633                         }
1634                 }
1635                 else {
1636                         sep->se_rpc_lowvers =
1637                                 sep->se_rpc_highvers = 1;
1638                 }
1639         }
1640         sep->se_nomapped = 0;
1641         while (isdigit(sep->se_proto[strlen(sep->se_proto) - 1])) {
1642 #ifdef INET6
1643                 if (sep->se_proto[strlen(sep->se_proto) - 1] == '6') {
1644                         if (no_v6bind != 0) {
1645                                 syslog(LOG_NOTICE, "IPv6 bind is ignored for %s",
1646                                        sep->se_service);
1647                                 freeconfig(sep);
1648                                 goto more;
1649                         }
1650                         sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1651                         v6bind = 1;
1652                         continue;
1653                 }
1654 #endif
1655                 if (sep->se_proto[strlen(sep->se_proto) - 1] == '4') {
1656                         sep->se_proto[strlen(sep->se_proto) - 1] = '\0';
1657                         v4bind = 1;
1658                         continue;
1659                 }
1660                 /* illegal version num */
1661                 syslog(LOG_ERR, "bad IP version for %s", sep->se_proto);
1662                 freeconfig(sep);
1663                 goto more;
1664         }
1665         if (strcmp(sep->se_proto, "unix") == 0) {
1666                 sep->se_family = AF_UNIX;
1667         } else
1668 #ifdef INET6
1669         if (v6bind != 0) {
1670                 sep->se_family = AF_INET6;
1671                 if (v4bind == 0 || no_v4bind != 0)
1672                         sep->se_nomapped = 1;
1673         } else
1674 #endif
1675         { /* default to v4 bind if not v6 bind */
1676                 if (no_v4bind != 0) {
1677                         syslog(LOG_NOTICE, "IPv4 bind is ignored for %s",
1678                                sep->se_service);
1679                         freeconfig(sep);
1680                         goto more;
1681                 }
1682                 sep->se_family = AF_INET;
1683         }
1684         /* init ctladdr */
1685         switch(sep->se_family) {
1686         case AF_INET:
1687                 memcpy(&sep->se_ctrladdr4, bind_sa4,
1688                        sizeof(sep->se_ctrladdr4));
1689                 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr4);
1690                 break;
1691 #ifdef INET6
1692         case AF_INET6:
1693                 memcpy(&sep->se_ctrladdr6, bind_sa6,
1694                        sizeof(sep->se_ctrladdr6));
1695                 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr6);
1696                 break;
1697 #endif
1698         case AF_UNIX:
1699                 if (strlen(sep->se_service) >= sizeof(sep->se_ctrladdr_un.sun_path)) {
1700                         syslog(LOG_ERR, 
1701                             "domain socket pathname too long for service %s",
1702                             sep->se_service);
1703                         goto more;
1704                 }
1705                 memset(&sep->se_ctrladdr, 0, sizeof(sep->se_ctrladdr));
1706                 sep->se_ctrladdr_un.sun_family = sep->se_family;
1707                 sep->se_ctrladdr_un.sun_len = strlen(sep->se_service);
1708                 strcpy(sep->se_ctrladdr_un.sun_path, sep->se_service);
1709                 sep->se_ctrladdr_size = SUN_LEN(&sep->se_ctrladdr_un);
1710         }
1711         arg = sskip(&cp);
1712         if (!strncmp(arg, "wait", 4))
1713                 sep->se_accept = 0;
1714         else if (!strncmp(arg, "nowait", 6))
1715                 sep->se_accept = 1;
1716         else {
1717                 syslog(LOG_ERR,
1718                         "%s: bad wait/nowait for service %s",
1719                         CONFIG, sep->se_service);
1720                 goto more;
1721         }
1722         sep->se_maxchild = -1;
1723         sep->se_maxcpm = -1;
1724         if ((s = strchr(arg, '/')) != NULL) {
1725                 char *eptr;
1726                 u_long val;
1727
1728                 val = strtoul(s + 1, &eptr, 10);
1729                 if (eptr == s + 1 || val > MAX_MAXCHLD) {
1730                         syslog(LOG_ERR,
1731                                 "%s: bad max-child for service %s",
1732                                 CONFIG, sep->se_service);
1733                         goto more;
1734                 }
1735                 if (debug)
1736                         if (!sep->se_accept && val != 1)
1737                                 warnx("maxchild=%lu for wait service %s"
1738                                     " not recommended", val, sep->se_service);
1739                 sep->se_maxchild = val;
1740                 if (*eptr == '/')
1741                         sep->se_maxcpm = strtol(eptr + 1, &eptr, 10);
1742                 /*
1743                  * explicitly do not check for \0 for future expansion /
1744                  * backwards compatibility
1745                  */
1746         }
1747         if (ISMUX(sep)) {
1748                 /*
1749                  * Silently enforce "nowait" mode for TCPMUX services
1750                  * since they don't have an assigned port to listen on.
1751                  */
1752                 sep->se_accept = 1;
1753                 if (strcmp(sep->se_proto, "tcp")) {
1754                         syslog(LOG_ERR,
1755                                 "%s: bad protocol for tcpmux service %s",
1756                                 CONFIG, sep->se_service);
1757                         goto more;
1758                 }
1759                 if (sep->se_socktype != SOCK_STREAM) {
1760                         syslog(LOG_ERR,
1761                                 "%s: bad socket type for tcpmux service %s",
1762                                 CONFIG, sep->se_service);
1763                         goto more;
1764                 }
1765         }
1766         sep->se_user = newstr(sskip(&cp));
1767 #ifdef LOGIN_CAP
1768         if ((s = strrchr(sep->se_user, '/')) != NULL) {
1769                 *s = '\0';
1770                 sep->se_class = newstr(s + 1);
1771         } else
1772                 sep->se_class = newstr(RESOURCE_RC);
1773 #endif
1774         if ((s = strrchr(sep->se_user, ':')) != NULL) {
1775                 *s = '\0';
1776                 sep->se_group = newstr(s + 1);
1777         } else
1778                 sep->se_group = NULL;
1779         sep->se_server = newstr(sskip(&cp));
1780         if ((sep->se_server_name = rindex(sep->se_server, '/')))
1781                 sep->se_server_name++;
1782         if (strcmp(sep->se_server, "internal") == 0) {
1783                 struct biltin *bi;
1784
1785                 for (bi = biltins; bi->bi_service; bi++)
1786                         if (bi->bi_socktype == sep->se_socktype &&
1787                             matchservent(bi->bi_service, sep->se_service,
1788                             sep->se_proto))
1789                                 break;
1790                 if (bi->bi_service == 0) {
1791                         syslog(LOG_ERR, "internal service %s unknown",
1792                                 sep->se_service);
1793                         goto more;
1794                 }
1795                 sep->se_accept = 1;     /* force accept mode for built-ins */
1796                 sep->se_bi = bi;
1797         } else
1798                 sep->se_bi = NULL;
1799         if (sep->se_maxcpm < 0)
1800                 sep->se_maxcpm = maxcpm;
1801         if (sep->se_maxchild < 0) {     /* apply default max-children */
1802                 if (sep->se_bi && sep->se_bi->bi_maxchild >= 0)
1803                         sep->se_maxchild = sep->se_bi->bi_maxchild;
1804                 else if (sep->se_accept) 
1805                         sep->se_maxchild = maxchild > 0 ? maxchild : 0;
1806                 else
1807                         sep->se_maxchild = 1;
1808         }
1809         if (sep->se_maxchild > 0) {
1810                 sep->se_pids = malloc(sep->se_maxchild * sizeof(*sep->se_pids));
1811                 if (sep->se_pids == NULL) {
1812                         syslog(LOG_ERR, "malloc: %m");
1813                         exit(EX_OSERR);
1814                 }
1815         }
1816         argc = 0;
1817         for (arg = skip(&cp); cp; arg = skip(&cp))
1818                 if (argc < MAXARGV) {
1819                         sep->se_argv[argc++] = newstr(arg);
1820                 } else {
1821                         syslog(LOG_ERR,
1822                                 "%s: too many arguments for service %s",
1823                                 CONFIG, sep->se_service);
1824                         goto more;
1825                 }
1826         while (argc <= MAXARGV)
1827                 sep->se_argv[argc++] = NULL;
1828 #ifdef IPSEC
1829         sep->se_policy = policy ? newstr(policy) : NULL;
1830 #endif
1831         return (sep);
1832 }
1833
1834 void
1835 freeconfig(struct servtab *cp)
1836 {
1837         int i;
1838
1839         if (cp->se_service)
1840                 free(cp->se_service);
1841         if (cp->se_proto)
1842                 free(cp->se_proto);
1843         if (cp->se_user)
1844                 free(cp->se_user);
1845         if (cp->se_group)
1846                 free(cp->se_group);
1847 #ifdef LOGIN_CAP
1848         if (cp->se_class)
1849                 free(cp->se_class);
1850 #endif
1851         if (cp->se_server)
1852                 free(cp->se_server);
1853         if (cp->se_pids)
1854                 free(cp->se_pids);
1855         for (i = 0; i < MAXARGV; i++)
1856                 if (cp->se_argv[i])
1857                         free(cp->se_argv[i]);
1858 #ifdef IPSEC
1859         if (cp->se_policy)
1860                 free(cp->se_policy);
1861 #endif
1862 }
1863
1864
1865 /*
1866  * Safe skip - if skip returns null, log a syntax error in the
1867  * configuration file and exit.
1868  */
1869 char *
1870 sskip(char **cpp)
1871 {
1872         char *cp;
1873
1874         cp = skip(cpp);
1875         if (cp == NULL) {
1876                 syslog(LOG_ERR, "%s: syntax error", CONFIG);
1877                 exit(EX_DATAERR);
1878         }
1879         return (cp);
1880 }
1881
1882 char *
1883 skip(char **cpp)
1884 {
1885         char *cp = *cpp;
1886         char *start;
1887         char quote = '\0';
1888
1889 again:
1890         while (*cp == ' ' || *cp == '\t')
1891                 cp++;
1892         if (*cp == '\0') {
1893                 int c;
1894
1895                 c = getc(fconfig);
1896                 (void) ungetc(c, fconfig);
1897                 if (c == ' ' || c == '\t')
1898                         if ((cp = nextline(fconfig)))
1899                                 goto again;
1900                 *cpp = (char *)0;
1901                 return ((char *)0);
1902         }
1903         if (*cp == '"' || *cp == '\'')
1904                 quote = *cp++;
1905         start = cp;
1906         if (quote)
1907                 while (*cp && *cp != quote)
1908                         cp++;
1909         else
1910                 while (*cp && *cp != ' ' && *cp != '\t')
1911                         cp++;
1912         if (*cp != '\0')
1913                 *cp++ = '\0';
1914         *cpp = cp;
1915         return (start);
1916 }
1917
1918 char *
1919 nextline(FILE *fd)
1920 {
1921         char *cp;
1922
1923         if (fgets(line, sizeof (line), fd) == NULL)
1924                 return ((char *)0);
1925         cp = strchr(line, '\n');
1926         if (cp)
1927                 *cp = '\0';
1928         return (line);
1929 }
1930
1931 char *
1932 newstr(const char *cp)
1933 {
1934         char *cr;
1935
1936         if ((cr = strdup(cp != NULL ? cp : "")))
1937                 return (cr);
1938         syslog(LOG_ERR, "strdup: %m");
1939         exit(EX_OSERR);
1940 }
1941
1942 void
1943 inetd_setproctitle(const char *a, int s)
1944 {
1945         socklen_t size;
1946         struct sockaddr_storage ss;
1947         char buf[80], pbuf[INET6_ADDRSTRLEN];
1948
1949         size = sizeof(ss);
1950         if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) {
1951                 getnameinfo((struct sockaddr *)&ss, size, pbuf, sizeof(pbuf),
1952                             NULL, 0, NI_NUMERICHOST|NI_WITHSCOPEID);
1953                 (void) sprintf(buf, "%s [%s]", a, pbuf);
1954         } else
1955                 (void) sprintf(buf, "%s", a);
1956         setproctitle("%s", buf);
1957 }
1958
1959 int
1960 check_loop(const struct sockaddr *sa, const struct servtab *sep)
1961 {
1962         struct servtab *se2;
1963         char pname[INET6_ADDRSTRLEN];
1964
1965         for (se2 = servtab; se2; se2 = se2->se_next) {
1966                 if (!se2->se_bi || se2->se_socktype != SOCK_DGRAM)
1967                         continue;
1968
1969                 switch (se2->se_family) {
1970                 case AF_INET:
1971                         if (((const struct sockaddr_in *)sa)->sin_port ==
1972                             se2->se_ctrladdr4.sin_port)
1973                                 goto isloop;
1974                         continue;
1975 #ifdef INET6
1976                 case AF_INET6:
1977                         if (((const struct sockaddr_in *)sa)->sin_port ==
1978                             se2->se_ctrladdr4.sin_port)
1979                                 goto isloop;
1980                         continue;
1981 #endif
1982                 default:
1983                         continue;
1984                 }
1985         isloop:
1986                 getnameinfo(sa, sa->sa_len, pname, sizeof(pname), NULL, 0,
1987                             NI_NUMERICHOST|NI_WITHSCOPEID);
1988                 syslog(LOG_WARNING, "%s/%s:%s/%s loop request REFUSED from %s",
1989                        sep->se_service, sep->se_proto,
1990                        se2->se_service, se2->se_proto,
1991                        pname);
1992                 return 1;
1993         }
1994         return 0;
1995 }
1996
1997 /*
1998  * print_service:
1999  *      Dump relevant information to stderr
2000  */
2001 void
2002 print_service(const char *action, const struct servtab *sep)
2003 {
2004         fprintf(stderr,
2005             "%s: %s proto=%s accept=%d max=%d user=%s group=%s"
2006 #ifdef LOGIN_CAP
2007             "class=%s"
2008 #endif
2009             " builtin=%p server=%s"
2010 #ifdef IPSEC
2011             " policy=\"%s\""
2012 #endif
2013             "\n",
2014             action, sep->se_service, sep->se_proto,
2015             sep->se_accept, sep->se_maxchild, sep->se_user, sep->se_group,
2016 #ifdef LOGIN_CAP
2017             sep->se_class,
2018 #endif
2019             (void *) sep->se_bi, sep->se_server
2020 #ifdef IPSEC
2021             , (sep->se_policy ? sep->se_policy : "")
2022 #endif
2023             );
2024 }
2025
2026 #define CPMHSIZE        256
2027 #define CPMHMASK        (CPMHSIZE-1)
2028 #define CHTGRAN         10
2029 #define CHTSIZE         6
2030
2031 typedef struct CTime {
2032         unsigned long   ct_Ticks;
2033         int             ct_Count;
2034 } CTime;
2035
2036 typedef struct CHash {
2037         union {
2038                 struct in_addr  c4_Addr;
2039                 struct in6_addr c6_Addr;
2040         } cu_Addr;
2041 #define ch_Addr4        cu_Addr.c4_Addr
2042 #define ch_Addr6        cu_Addr.c6_Addr
2043         int             ch_Family;
2044         time_t          ch_LTime;
2045         char            *ch_Service;
2046         CTime           ch_Times[CHTSIZE];
2047 } CHash;
2048
2049 CHash   CHashAry[CPMHSIZE];
2050
2051 int
2052 cpmip(const struct servtab *sep, int ctrl)
2053 {
2054         struct sockaddr_storage rss;
2055         socklen_t rssLen = sizeof(rss);
2056         int r = 0;
2057
2058         /*
2059          * If getpeername() fails, just let it through (if logging is
2060          * enabled the condition is caught elsewhere)
2061          */
2062
2063         if (sep->se_maxcpm > 0 && 
2064             getpeername(ctrl, (struct sockaddr *)&rss, &rssLen) == 0 ) {
2065                 time_t t = time(NULL);
2066                 int hv = 0xABC3D20F;
2067                 int i;
2068                 int cnt = 0;
2069                 CHash *chBest = NULL;
2070                 unsigned int ticks = t / CHTGRAN;
2071                 struct sockaddr_in *sin4;
2072 #ifdef INET6
2073                 struct sockaddr_in6 *sin6;
2074 #endif
2075
2076                 sin4 = (struct sockaddr_in *)&rss;
2077 #ifdef INET6
2078                 sin6 = (struct sockaddr_in6 *)&rss;
2079 #endif
2080                 {
2081                         char *p;
2082                         int addrlen;
2083
2084                         switch (rss.ss_family) {
2085                         case AF_INET:
2086                                 p = (char *)&sin4->sin_addr;
2087                                 addrlen = sizeof(struct in_addr);
2088                                 break;
2089 #ifdef INET6
2090                         case AF_INET6:
2091                                 p = (char *)&sin6->sin6_addr;
2092                                 addrlen = sizeof(struct in6_addr);
2093                                 break;
2094 #endif
2095                         default:
2096                                 /* should not happen */
2097                                 return -1;
2098                         }
2099
2100                         for (i = 0; i < addrlen; ++i, ++p) {
2101                                 hv = (hv << 5) ^ (hv >> 23) ^ *p;
2102                         }
2103                         hv = (hv ^ (hv >> 16));
2104                 }
2105                 for (i = 0; i < 5; ++i) {
2106                         CHash *ch = &CHashAry[(hv + i) & CPMHMASK];
2107
2108                         if (rss.ss_family == AF_INET &&
2109                             ch->ch_Family == AF_INET &&
2110                             sin4->sin_addr.s_addr == ch->ch_Addr4.s_addr &&
2111                             ch->ch_Service && strcmp(sep->se_service,
2112                             ch->ch_Service) == 0) {
2113                                 chBest = ch;
2114                                 break;
2115                         }
2116 #ifdef INET6
2117                         if (rss.ss_family == AF_INET6 &&
2118                             ch->ch_Family == AF_INET6 &&
2119                             IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2120                                                &ch->ch_Addr6) != 0 &&
2121                             ch->ch_Service && strcmp(sep->se_service,
2122                             ch->ch_Service) == 0) {
2123                                 chBest = ch;
2124                                 break;
2125                         }
2126 #endif
2127                         if (chBest == NULL || ch->ch_LTime == 0 || 
2128                             ch->ch_LTime < chBest->ch_LTime) {
2129                                 chBest = ch;
2130                         }
2131                 }
2132                 if ((rss.ss_family == AF_INET &&
2133                      (chBest->ch_Family != AF_INET ||
2134                       sin4->sin_addr.s_addr != chBest->ch_Addr4.s_addr)) ||
2135                     chBest->ch_Service == NULL ||
2136                     strcmp(sep->se_service, chBest->ch_Service) != 0) {
2137                         chBest->ch_Family = sin4->sin_family;
2138                         chBest->ch_Addr4 = sin4->sin_addr;
2139                         if (chBest->ch_Service)
2140                                 free(chBest->ch_Service);
2141                         chBest->ch_Service = strdup(sep->se_service);
2142                         bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2143                 } 
2144 #ifdef INET6
2145                 if ((rss.ss_family == AF_INET6 &&
2146                      (chBest->ch_Family != AF_INET6 ||
2147                       IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2148                                          &chBest->ch_Addr6) == 0)) ||
2149                     chBest->ch_Service == NULL ||
2150                     strcmp(sep->se_service, chBest->ch_Service) != 0) {
2151                         chBest->ch_Family = sin6->sin6_family;
2152                         chBest->ch_Addr6 = sin6->sin6_addr;
2153                         if (chBest->ch_Service)
2154                                 free(chBest->ch_Service);
2155                         chBest->ch_Service = strdup(sep->se_service);
2156                         bzero(chBest->ch_Times, sizeof(chBest->ch_Times));
2157                 }
2158 #endif
2159                 chBest->ch_LTime = t;
2160                 {
2161                         CTime *ct = &chBest->ch_Times[ticks % CHTSIZE];
2162                         if (ct->ct_Ticks != ticks) {
2163                                 ct->ct_Ticks = ticks;
2164                                 ct->ct_Count = 0;
2165                         }
2166                         ++ct->ct_Count;
2167                 }
2168                 for (i = 0; i < CHTSIZE; ++i) {
2169                         CTime *ct = &chBest->ch_Times[i];
2170                         if (ct->ct_Ticks <= ticks &&
2171                             ct->ct_Ticks >= ticks - CHTSIZE) {
2172                                 cnt += ct->ct_Count;
2173                         }
2174                 }
2175                 if (cnt * (CHTSIZE * CHTGRAN) / 60 > sep->se_maxcpm) {
2176                         char pname[INET6_ADDRSTRLEN];
2177
2178                         getnameinfo((struct sockaddr *)&rss,
2179                                     ((struct sockaddr *)&rss)->sa_len,
2180                                     pname, sizeof(pname), NULL, 0,
2181                                     NI_NUMERICHOST|NI_WITHSCOPEID);
2182                         r = -1;
2183                         syslog(LOG_ERR,
2184                             "%s from %s exceeded counts/min (limit %d/min)",
2185                             sep->se_service, pname,
2186                             sep->se_maxcpm);
2187                 }
2188         }
2189         return(r);
2190 }