inetd.c revision 1.80 1 /* $NetBSD: inetd.c,v 1.80 2002/06/01 00:32:41 itojun Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1983, 1991, 1993, 1994
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 */
72
73 #include <sys/cdefs.h>
74 #ifndef lint
75 __COPYRIGHT("@(#) Copyright (c) 1983, 1991, 1993, 1994\n\
76 The Regents of the University of California. All rights reserved.\n");
77 #if 0
78 static char sccsid[] = "@(#)inetd.c 8.4 (Berkeley) 4/13/94";
79 #else
80 __RCSID("$NetBSD: inetd.c,v 1.80 2002/06/01 00:32:41 itojun Exp $");
81 #endif
82 #endif /* not lint */
83
84 /*
85 * Inetd - Internet super-server
86 *
87 * This program invokes all internet services as needed. Connection-oriented
88 * services are invoked each time a connection is made, by creating a process.
89 * This process is passed the connection as file descriptor 0 and is expected
90 * to do a getpeername to find out the source host and port.
91 *
92 * Datagram oriented services are invoked when a datagram
93 * arrives; a process is created and passed a pending message
94 * on file descriptor 0. Datagram servers may either connect
95 * to their peer, freeing up the original socket for inetd
96 * to receive further messages on, or ``take over the socket'',
97 * processing all arriving datagrams and, eventually, timing
98 * out. The first type of server is said to be ``multi-threaded'';
99 * the second type of server ``single-threaded''.
100 *
101 * Inetd uses a configuration file which is read at startup
102 * and, possibly, at some later time in response to a hangup signal.
103 * The configuration file is ``free format'' with fields given in the
104 * order shown below. Continuation lines for an entry must being with
105 * a space or tab. All fields must be present in each entry.
106 *
107 * service name must be in /etc/services or must
108 * name a tcpmux service
109 * socket type stream/dgram/raw/rdm/seqpacket
110 * protocol must be in /etc/protocols
111 * wait/nowait[:max] single-threaded/multi-threaded, max #
112 * user[:group] user/group to run daemon as
113 * server program full path name
114 * server program arguments maximum of MAXARGS (20)
115 *
116 * For RPC services
117 * service name/version must be in /etc/rpc
118 * socket type stream/dgram/raw/rdm/seqpacket
119 * protocol must be in /etc/protocols
120 * wait/nowait[:max] single-threaded/multi-threaded
121 * user[:group] user to run daemon as
122 * server program full path name
123 * server program arguments maximum of MAXARGS (20)
124 *
125 * For non-RPC services, the "service name" can be of the form
126 * hostaddress:servicename, in which case the hostaddress is used
127 * as the host portion of the address to listen on. If hostaddress
128 * consists of a single `*' character, INADDR_ANY is used.
129 *
130 * A line can also consist of just
131 * hostaddress:
132 * where hostaddress is as in the preceding paragraph. Such a line must
133 * have no further fields; the specified hostaddress is remembered and
134 * used for all further lines that have no hostaddress specified,
135 * until the next such line (or EOF). (This is why * is provided to
136 * allow explicit specification of INADDR_ANY.) A line
137 * *:
138 * is implicitly in effect at the beginning of the file.
139 *
140 * The hostaddress specifier may (and often will) contain dots;
141 * the service name must not.
142 *
143 * For RPC services, host-address specifiers are accepted and will
144 * work to some extent; however, because of limitations in the
145 * portmapper interface, it will not work to try to give more than
146 * one line for any given RPC service, even if the host-address
147 * specifiers are different.
148 *
149 * TCP services without official port numbers are handled with the
150 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for
151 * requests. When a connection is made from a foreign host, the service
152 * requested is passed to tcpmux, which looks it up in the servtab list
153 * and returns the proper entry for the service. Tcpmux returns a
154 * negative reply if the service doesn't exist, otherwise the invoked
155 * server is expected to return the positive reply if the service type in
156 * inetd.conf file has the prefix "tcpmux/". If the service type has the
157 * prefix "tcpmux/+", tcpmux will return the positive reply for the
158 * process; this is for compatibility with older server code, and also
159 * allows you to invoke programs that use stdin/stdout without putting any
160 * special server code in them. Services that use tcpmux are "nowait"
161 * because they do not have a well-known port and hence cannot listen
162 * for new requests.
163 *
164 * Comment lines are indicated by a `#' in column 1.
165 *
166 * #ifdef IPSEC
167 * Comment lines that start with "#@" denote IPsec policy string, as described
168 * in ipsec_set_policy(3). This will affect all the following items in
169 * inetd.conf(8). To reset the policy, just use "#@" line. By default,
170 * there's no IPsec policy.
171 * #endif
172 */
173
174 /*
175 * Here's the scoop concerning the user:group feature:
176 *
177 * 1) set-group-option off.
178 *
179 * a) user = root: NO setuid() or setgid() is done
180 *
181 * b) other: setuid()
182 * setgid(primary group as found in passwd)
183 * initgroups(name, primary group)
184 *
185 * 2) set-group-option on.
186 *
187 * a) user = root: NO setuid()
188 * setgid(specified group)
189 * NO initgroups()
190 *
191 * b) other: setuid()
192 * setgid(specified group)
193 * initgroups(name, specified group)
194 *
195 */
196
197 #include <sys/param.h>
198 #include <sys/stat.h>
199 #include <sys/ioctl.h>
200 #include <sys/socket.h>
201 #include <sys/un.h>
202 #include <sys/wait.h>
203 #include <sys/time.h>
204 #include <sys/resource.h>
205
206 #ifndef RLIMIT_NOFILE
207 #define RLIMIT_NOFILE RLIMIT_OFILE
208 #endif
209
210 #ifndef NO_RPC
211 #define RPC
212 #endif
213
214 #include <netinet/in.h>
215 #include <arpa/inet.h>
216 #ifdef RPC
217 #include <rpc/rpc.h>
218 #include <rpc/rpcb_clnt.h>
219 #include <netconfig.h>
220 #endif
221
222 #include <ctype.h>
223 #include <errno.h>
224 #include <fcntl.h>
225 #include <grp.h>
226 #include <netdb.h>
227 #include <pwd.h>
228 #include <signal.h>
229 #include <stdio.h>
230 #include <stdlib.h>
231 #include <string.h>
232 #include <syslog.h>
233 #include <unistd.h>
234 #include <util.h>
235
236 #include "pathnames.h"
237
238 #ifdef IPSEC
239 #include <netinet6/ipsec.h>
240 #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
241 #undef IPSEC
242 #endif
243 #include "ipsec.h"
244 #endif
245
246 #ifdef LIBWRAP
247 # include <tcpd.h>
248 #ifndef LIBWRAP_ALLOW_FACILITY
249 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
250 #endif
251 #ifndef LIBWRAP_ALLOW_SEVERITY
252 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
253 #endif
254 #ifndef LIBWRAP_DENY_FACILITY
255 # define LIBWRAP_DENY_FACILITY LOG_AUTH
256 #endif
257 #ifndef LIBWRAP_DENY_SEVERITY
258 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
259 #endif
260 int allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
261 int deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
262 #endif
263
264 #define TOOMANY 40 /* don't start more than TOOMANY */
265 #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
266 #define RETRYTIME (60*10) /* retry after bind or server fail */
267
268 #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
269
270 int debug;
271 #ifdef LIBWRAP
272 int lflag;
273 #endif
274 int nsock, maxsock;
275 fd_set allsock;
276 int options;
277 int timingout;
278 struct servent *sp;
279 char *curdom;
280 const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
281
282 #ifndef OPEN_MAX
283 #define OPEN_MAX 64
284 #endif
285
286 /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
287 #define FD_MARGIN (8)
288 rlim_t rlim_ofile_cur = OPEN_MAX;
289
290 #ifdef RLIMIT_NOFILE
291 struct rlimit rlim_ofile;
292 #endif
293
294 struct servtab {
295 char *se_hostaddr; /* host address to listen on */
296 char *se_service; /* name of service */
297 int se_socktype; /* type of socket to use */
298 int se_family; /* address family */
299 char *se_proto; /* protocol used */
300 int se_sndbuf; /* sndbuf size */
301 int se_rcvbuf; /* rcvbuf size */
302 int se_rpcprog; /* rpc program number */
303 int se_rpcversl; /* rpc program lowest version */
304 int se_rpcversh; /* rpc program highest version */
305 #define isrpcservice(sep) ((sep)->se_rpcversl != 0)
306 pid_t se_wait; /* single threaded server */
307 short se_checked; /* looked at during merge */
308 char *se_user; /* user name to run as */
309 char *se_group; /* group name to run as */
310 struct biltin *se_bi; /* if built-in, description */
311 char *se_server; /* server program */
312 #define MAXARGV 20
313 char *se_argv[MAXARGV+1]; /* program arguments */
314 #ifdef IPSEC
315 char *se_policy; /* IPsec poilcy string */
316 #endif
317 int se_fd; /* open descriptor */
318 int se_type; /* type */
319 union {
320 struct sockaddr se_un_ctrladdr;
321 struct sockaddr_in se_un_ctrladdr_in;
322 struct sockaddr_in6 se_un_ctrladdr_in6;
323 struct sockaddr_un se_un_ctrladdr_un;
324 } se_un; /* bound address */
325 #define se_ctrladdr se_un.se_un_ctrladdr
326 #define se_ctrladdr_in se_un.se_un_ctrladdr_in
327 #define se_ctrladdr_un se_un.se_un_ctrladdr_un
328 int se_ctrladdr_size;
329 int se_max; /* max # of instances of this service */
330 int se_count; /* number started since se_time */
331 struct timeval se_time; /* start of se_count */
332 #ifdef MULOG
333 int se_log;
334 #define MULOG_RFC931 0x40000000
335 #endif
336 struct servtab *se_next;
337 } *servtab;
338
339 #define NORM_TYPE 0
340 #define MUX_TYPE 1
341 #define MUXPLUS_TYPE 2
342 #define FAITH_TYPE 3
343 #define ISMUX(sep) (((sep)->se_type == MUX_TYPE) || \
344 ((sep)->se_type == MUXPLUS_TYPE))
345 #define ISMUXPLUS(sep) ((sep)->se_type == MUXPLUS_TYPE)
346
347
348 void chargen_dg __P((int, struct servtab *));
349 void chargen_stream __P((int, struct servtab *));
350 void close_sep __P((struct servtab *));
351 void config __P((int));
352 void daytime_dg __P((int, struct servtab *));
353 void daytime_stream __P((int, struct servtab *));
354 void discard_dg __P((int, struct servtab *));
355 void discard_stream __P((int, struct servtab *));
356 void echo_dg __P((int, struct servtab *));
357 void echo_stream __P((int, struct servtab *));
358 void endconfig __P((void));
359 struct servtab *enter __P((struct servtab *));
360 void freeconfig __P((struct servtab *));
361 struct servtab *getconfigent __P((void));
362 void goaway __P((int));
363 void machtime_dg __P((int, struct servtab *));
364 void machtime_stream __P((int, struct servtab *));
365 char *newstr __P((char *));
366 char *nextline __P((FILE *));
367 void print_service __P((char *, struct servtab *));
368 void reapchild __P((int));
369 void retry __P((int));
370 void run_service __P((int, struct servtab *));
371 int setconfig __P((void));
372 void setup __P((struct servtab *));
373 char *sskip __P((char **));
374 char *skip __P((char **));
375 void tcpmux __P((int, struct servtab *));
376 void usage __P((void));
377 void register_rpc __P((struct servtab *sep));
378 void unregister_rpc __P((struct servtab *sep));
379 void bump_nofile __P((void));
380 void inetd_setproctitle __P((char *, int));
381 void initring __P((void));
382 uint32_t machtime __P((void));
383 int port_good_dg __P((struct sockaddr *sa));
384 static int getline __P((int, char *, int));
385 int main __P((int, char *[]));
386 void spawn __P((struct servtab *, int));
387 #ifdef MULOG
388 void dolog __P((struct servtab *, int));
389 static void timeout __P((int));
390 char *rfc931_name __P((struct sockaddr *, int));
391 #endif
392
393 struct biltin {
394 char *bi_service; /* internally provided service name */
395 int bi_socktype; /* type of socket supported */
396 short bi_fork; /* 1 if should fork before call */
397 short bi_wait; /* 1 if should wait for child */
398 void (*bi_fn) __P((int, struct servtab *));
399 /* function which performs it */
400 } biltins[] = {
401 /* Echo received data */
402 { "echo", SOCK_STREAM, 1, 0, echo_stream },
403 { "echo", SOCK_DGRAM, 0, 0, echo_dg },
404
405 /* Internet /dev/null */
406 { "discard", SOCK_STREAM, 1, 0, discard_stream },
407 { "discard", SOCK_DGRAM, 0, 0, discard_dg },
408
409 /* Return 32 bit time since 1970 */
410 { "time", SOCK_STREAM, 0, 0, machtime_stream },
411 { "time", SOCK_DGRAM, 0, 0, machtime_dg },
412
413 /* Return human-readable time */
414 { "daytime", SOCK_STREAM, 0, 0, daytime_stream },
415 { "daytime", SOCK_DGRAM, 0, 0, daytime_dg },
416
417 /* Familiar character generator */
418 { "chargen", SOCK_STREAM, 1, 0, chargen_stream },
419 { "chargen", SOCK_DGRAM, 0, 0, chargen_dg },
420
421 { "tcpmux", SOCK_STREAM, 1, 0, tcpmux },
422
423 { NULL }
424 };
425
426 /* list of "bad" ports. I.e. ports that are most obviously used for
427 * "cycling packets" denial of service attacks. See /etc/services.
428 * List must end with port number "0".
429 */
430
431 u_int16_t bad_ports[] = { 7, 9, 13, 19, 37, 0 };
432
433
434 #define NUMINT (sizeof(intab) / sizeof(struct inent))
435 char *CONFIG = _PATH_INETDCONF;
436
437 int
438 main(argc, argv)
439 int argc;
440 char *argv[];
441 {
442 struct servtab *sep, *nsep;
443 struct sigvec sv;
444 int ch;
445
446 while ((ch = getopt(argc, argv,
447 #ifdef LIBWRAP
448 "dl"
449 #else
450 "d"
451 #endif
452 )) != -1)
453 switch(ch) {
454 case 'd':
455 debug = 1;
456 options |= SO_DEBUG;
457 break;
458 #ifdef LIBWRAP
459 case 'l':
460 lflag = 1;
461 break;
462 #endif
463 case '?':
464 default:
465 usage();
466 }
467 argc -= optind;
468 argv += optind;
469
470 if (argc > 0)
471 CONFIG = argv[0];
472
473 if (debug == 0)
474 daemon(0, 0);
475 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
476 pidfile(NULL);
477
478 #ifdef RLIMIT_NOFILE
479 if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
480 syslog(LOG_ERR, "getrlimit: %m");
481 } else {
482 rlim_ofile_cur = rlim_ofile.rlim_cur;
483 if (rlim_ofile_cur == RLIM_INFINITY) /* ! */
484 rlim_ofile_cur = OPEN_MAX;
485 }
486 #endif
487
488 memset(&sv, 0, sizeof(sv));
489 sv.sv_mask = SIGBLOCK;
490 sv.sv_handler = retry;
491 sigvec(SIGALRM, &sv, (struct sigvec *)0);
492 config(SIGHUP);
493 sv.sv_handler = config;
494 sigvec(SIGHUP, &sv, (struct sigvec *)0);
495 sv.sv_handler = reapchild;
496 sigvec(SIGCHLD, &sv, (struct sigvec *)0);
497 sv.sv_handler = goaway;
498 sigvec(SIGTERM, &sv, (struct sigvec *)0);
499 sv.sv_handler = goaway;
500 sigvec(SIGINT, &sv, (struct sigvec *)0);
501 sv.sv_mask = 0L;
502 sv.sv_handler = SIG_IGN;
503 sigvec(SIGPIPE, &sv, (struct sigvec *)0);
504
505 {
506 /* space for daemons to overwrite environment for ps */
507 #define DUMMYSIZE 100
508 char dummy[DUMMYSIZE];
509
510 (void)memset(dummy, 'x', DUMMYSIZE - 1);
511 dummy[DUMMYSIZE - 1] = '\0';
512
513 (void)setenv("inetd_dummy", dummy, 1);
514 }
515
516 for (;;) {
517 int n, ctrl;
518 fd_set readable;
519
520 if (nsock == 0) {
521 (void) sigblock(SIGBLOCK);
522 while (nsock == 0)
523 sigpause(0L);
524 (void) sigsetmask(0L);
525 }
526 readable = allsock;
527 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
528 (fd_set *)0, (struct timeval *)0)) <= 0) {
529 if (n == -1 && errno != EINTR) {
530 syslog(LOG_WARNING, "select: %m");
531 sleep(1);
532 }
533 continue;
534 }
535 for (sep = servtab; n && sep; sep = nsep) {
536 nsep = sep->se_next;
537 if (sep->se_fd == -1 ||
538 !FD_ISSET(sep->se_fd, &readable))
539 continue;
540
541 n--;
542 if (debug)
543 fprintf(stderr, "someone wants %s\n", sep->se_service);
544 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
545 /* XXX here do the libwrap check-before-accept */
546 ctrl = accept(sep->se_fd, (struct sockaddr *)0,
547 (int *)0);
548 if (debug)
549 fprintf(stderr, "accept, ctrl %d\n",
550 ctrl);
551 if (ctrl < 0) {
552 if (errno != EINTR)
553 syslog(LOG_WARNING,
554 "accept (for %s): %m",
555 sep->se_service);
556 continue;
557 }
558 } else
559 ctrl = sep->se_fd;
560 (void) sigblock(SIGBLOCK);
561 spawn(sep, ctrl);
562 }
563 }
564 }
565
566 void
567 spawn(sep, ctrl)
568 struct servtab *sep;
569 int ctrl;
570 {
571 int dofork;
572 pid_t pid;
573 struct sigvec sv;
574
575 pid = 0;
576 #ifdef LIBWRAP_INTERNAL
577 dofork = 1;
578 #else
579 dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork);
580 #endif
581 if (dofork) {
582 if (sep->se_count++ == 0)
583 (void)gettimeofday(&sep->se_time, (struct timezone *)0);
584 else if (sep->se_count >= sep->se_max) {
585 struct timeval now;
586
587 (void)gettimeofday(&now, (struct timezone *)0);
588 if (now.tv_sec - sep->se_time.tv_sec > CNT_INTVL) {
589 sep->se_time = now;
590 sep->se_count = 1;
591 } else {
592 syslog(LOG_ERR,
593 "%s/%s server failing (looping), service terminated\n",
594 sep->se_service, sep->se_proto);
595 if (!sep->se_wait && sep->se_socktype ==
596 SOCK_STREAM)
597 close(ctrl);
598 close_sep(sep);
599 sigsetmask(0L);
600 if (!timingout) {
601 timingout = 1;
602 alarm(RETRYTIME);
603 }
604 return;
605 }
606 }
607 pid = fork();
608 if (pid < 0) {
609 syslog(LOG_ERR, "fork: %m");
610 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
611 close(ctrl);
612 sigsetmask(0L);
613 sleep(1);
614 return;
615 }
616 if (pid != 0 && sep->se_wait) {
617 sep->se_wait = pid;
618 FD_CLR(sep->se_fd, &allsock);
619 nsock--;
620 }
621 if (pid == 0) {
622 memset(&sv, 0, sizeof(sv));
623 sv.sv_mask = 0L;
624 sv.sv_handler = SIG_DFL;
625 sigvec(SIGPIPE, &sv, (struct sigvec *)0);
626 if (debug)
627 setsid();
628 }
629 }
630 sigsetmask(0L);
631 if (pid == 0) {
632 run_service(ctrl, sep);
633 if (dofork)
634 exit(0);
635 }
636 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
637 close(ctrl);
638 }
639
640 void
641 run_service(ctrl, sep)
642 int ctrl;
643 struct servtab *sep;
644 {
645 struct passwd *pwd;
646 struct group *grp = NULL; /* XXX gcc */
647 char buf[NI_MAXSERV];
648 #ifdef LIBWRAP
649 struct request_info req;
650 int denied;
651 char *service = NULL; /* XXX gcc */
652 #endif
653
654 #ifdef LIBWRAP
655 #ifndef LIBWRAP_INTERNAL
656 if (sep->se_bi == 0)
657 #endif
658 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
659 request_init(&req, RQ_DAEMON, sep->se_argv[0] ?
660 sep->se_argv[0] : sep->se_service, RQ_FILE, ctrl, NULL);
661 fromhost(&req);
662 denied = !hosts_access(&req);
663 if (denied || lflag) {
664 if (getnameinfo(&sep->se_ctrladdr,
665 sep->se_ctrladdr.sa_len, NULL, 0,
666 buf, sizeof(buf), 0) != 0) {
667 /* shouldn't happen */
668 (void)snprintf(buf, sizeof buf, "%d",
669 ntohs(sep->se_ctrladdr_in.sin_port));
670 }
671 service = buf;
672 }
673 if (denied) {
674 syslog(deny_severity,
675 "refused connection from %.500s, service %s (%s)",
676 eval_client(&req), service, sep->se_proto);
677 goto reject;
678 }
679 if (lflag) {
680 syslog(allow_severity,
681 "connection from %.500s, service %s (%s)",
682 eval_client(&req), service, sep->se_proto);
683 }
684 }
685 #endif /* LIBWRAP */
686
687 if (sep->se_bi) {
688 (*sep->se_bi->bi_fn)(ctrl, sep);
689 } else {
690 if ((pwd = getpwnam(sep->se_user)) == NULL) {
691 syslog(LOG_ERR, "%s/%s: %s: No such user",
692 sep->se_service, sep->se_proto, sep->se_user);
693 goto reject;
694 }
695 if (sep->se_group &&
696 (grp = getgrnam(sep->se_group)) == NULL) {
697 syslog(LOG_ERR, "%s/%s: %s: No such group",
698 sep->se_service, sep->se_proto, sep->se_group);
699 goto reject;
700 }
701 if (pwd->pw_uid) {
702 if (sep->se_group)
703 pwd->pw_gid = grp->gr_gid;
704 if (setgid(pwd->pw_gid) < 0) {
705 syslog(LOG_ERR,
706 "%s/%s: can't set gid %d: %m", sep->se_service,
707 sep->se_proto, pwd->pw_gid);
708 goto reject;
709 }
710 (void) initgroups(pwd->pw_name,
711 pwd->pw_gid);
712 if (setuid(pwd->pw_uid) < 0) {
713 syslog(LOG_ERR,
714 "%s/%s: can't set uid %d: %m", sep->se_service,
715 sep->se_proto, pwd->pw_uid);
716 goto reject;
717 }
718 } else if (sep->se_group) {
719 (void) setgid((gid_t)grp->gr_gid);
720 }
721 if (debug)
722 fprintf(stderr, "%d execl %s\n",
723 getpid(), sep->se_server);
724 #ifdef MULOG
725 if (sep->se_log)
726 dolog(sep, ctrl);
727 #endif
728 /* Set our control descriptor to not close-on-exec... */
729 if (fcntl(ctrl, F_SETFD, 0) < 0)
730 syslog(LOG_ERR, "fcntl (F_SETFD, 0): %m");
731 /* ...and dup it to stdin, stdout, and stderr. */
732 if (ctrl != 0) {
733 dup2(ctrl, 0);
734 close(ctrl);
735 ctrl = 0;
736 }
737 dup2(0, 1);
738 dup2(0, 2);
739 #ifdef RLIMIT_NOFILE
740 if (rlim_ofile.rlim_cur != rlim_ofile_cur &&
741 setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0)
742 syslog(LOG_ERR, "setrlimit: %m");
743 #endif
744 execv(sep->se_server, sep->se_argv);
745 syslog(LOG_ERR, "cannot execute %s: %m", sep->se_server);
746 reject:
747 if (sep->se_socktype != SOCK_STREAM)
748 recv(ctrl, buf, sizeof (buf), 0);
749 _exit(1);
750 }
751 }
752
753 void
754 reapchild(signo)
755 int signo;
756 {
757 int status;
758 pid_t pid;
759 struct servtab *sep;
760
761 for (;;) {
762 pid = wait3(&status, WNOHANG, (struct rusage *)0);
763 if (pid <= 0)
764 break;
765 if (debug)
766 fprintf(stderr, "%d reaped, status %#x\n",
767 pid, status);
768 for (sep = servtab; sep; sep = sep->se_next)
769 if (sep->se_wait == pid) {
770 if (WIFEXITED(status) && WEXITSTATUS(status))
771 syslog(LOG_WARNING,
772 "%s: exit status 0x%x",
773 sep->se_server, WEXITSTATUS(status));
774 else if (WIFSIGNALED(status))
775 syslog(LOG_WARNING,
776 "%s: exit signal 0x%x",
777 sep->se_server, WTERMSIG(status));
778 sep->se_wait = 1;
779 FD_SET(sep->se_fd, &allsock);
780 nsock++;
781 if (debug)
782 fprintf(stderr, "restored %s, fd %d\n",
783 sep->se_service, sep->se_fd);
784 }
785 }
786 }
787
788 void
789 config(signo)
790 int signo;
791 {
792 struct servtab *sep, *cp, **sepp;
793 long omask;
794 int n;
795
796 if (!setconfig()) {
797 syslog(LOG_ERR, "%s: %m", CONFIG);
798 return;
799 }
800 for (sep = servtab; sep; sep = sep->se_next)
801 sep->se_checked = 0;
802 while ((cp = getconfigent())) {
803 for (sep = servtab; sep; sep = sep->se_next)
804 if (strcmp(sep->se_service, cp->se_service) == 0 &&
805 strcmp(sep->se_hostaddr, cp->se_hostaddr) == 0 &&
806 strcmp(sep->se_proto, cp->se_proto) == 0 &&
807 ISMUX(sep) == ISMUX(cp))
808 break;
809 if (sep != 0) {
810 int i;
811
812 #define SWAP(type, a, b) {type c=(type)a; (type)a=(type)b; (type)b=(type)c;}
813
814 omask = sigblock(SIGBLOCK);
815 /*
816 * sep->se_wait may be holding the pid of a daemon
817 * that we're waiting for. If so, don't overwrite
818 * it unless the config file explicitly says don't
819 * wait.
820 */
821 if (cp->se_bi == 0 &&
822 (sep->se_wait == 1 || cp->se_wait == 0))
823 sep->se_wait = cp->se_wait;
824 SWAP(char *, sep->se_user, cp->se_user);
825 SWAP(char *, sep->se_group, cp->se_group);
826 SWAP(char *, sep->se_server, cp->se_server);
827 for (i = 0; i < MAXARGV; i++)
828 SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
829 #ifdef IPSEC
830 SWAP(char *, sep->se_policy, cp->se_policy);
831 #endif
832 SWAP(int, cp->se_type, sep->se_type);
833 SWAP(int, cp->se_max, sep->se_max);
834 #undef SWAP
835 if (isrpcservice(sep))
836 unregister_rpc(sep);
837 sep->se_rpcversl = cp->se_rpcversl;
838 sep->se_rpcversh = cp->se_rpcversh;
839 sigsetmask(omask);
840 freeconfig(cp);
841 if (debug)
842 print_service("REDO", sep);
843 } else {
844 sep = enter(cp);
845 if (debug)
846 print_service("ADD ", sep);
847 }
848 sep->se_checked = 1;
849
850 switch (sep->se_family) {
851 case AF_LOCAL:
852 if (sep->se_fd != -1)
853 break;
854 n = strlen(sep->se_service);
855 if (n > sizeof(sep->se_ctrladdr_un.sun_path)) {
856 syslog(LOG_ERR, "%s: address too long",
857 sep->se_service);
858 sep->se_checked = 0;
859 continue;
860 }
861 (void)unlink(sep->se_service);
862 strncpy(sep->se_ctrladdr_un.sun_path,
863 sep->se_service, n);
864 sep->se_ctrladdr_un.sun_family = AF_LOCAL;
865 sep->se_ctrladdr_size = n +
866 sizeof(sep->se_ctrladdr_un) -
867 sizeof(sep->se_ctrladdr_un.sun_path);
868 if (!ISMUX(sep))
869 setup(sep);
870 break;
871 case AF_INET:
872 #ifdef INET6
873 case AF_INET6:
874 #endif
875 {
876 struct addrinfo hints, *res;
877 char *host, *port;
878 int error;
879 int s;
880
881 /* check if the family is supported */
882 s = socket(sep->se_family, SOCK_DGRAM, 0);
883 if (s < 0) {
884 syslog(LOG_WARNING,
885 "%s/%s: %s: the address family is not supported by the kernel",
886 sep->se_service, sep->se_proto,
887 sep->se_hostaddr);
888 sep->se_checked = 0;
889 continue;
890 }
891 close(s);
892
893 memset(&hints, 0, sizeof(hints));
894 hints.ai_family = sep->se_family;
895 hints.ai_socktype = sep->se_socktype;
896 hints.ai_flags = AI_PASSIVE;
897 if (!strcmp(sep->se_hostaddr, "*"))
898 host = NULL;
899 else
900 host = sep->se_hostaddr;
901 if (isrpcservice(sep) || ISMUX(sep))
902 port = "0";
903 else
904 port = sep->se_service;
905 error = getaddrinfo(host, port, &hints, &res);
906 if (error) {
907 if (error == EAI_SERVICE) {
908 /* gai_strerror not friendly enough */
909 syslog(LOG_WARNING, "%s/%s: "
910 "unknown service",
911 sep->se_service, sep->se_proto);
912 } else {
913 syslog(LOG_ERR, "%s/%s: %s: %s",
914 sep->se_service, sep->se_proto,
915 sep->se_hostaddr,
916 gai_strerror(error));
917 }
918 sep->se_checked = 0;
919 continue;
920 }
921 if (res->ai_next) {
922 syslog(LOG_ERR,
923 "%s/%s: %s: resolved to multiple addr",
924 sep->se_service, sep->se_proto,
925 sep->se_hostaddr);
926 sep->se_checked = 0;
927 freeaddrinfo(res);
928 continue;
929 }
930 memcpy(&sep->se_ctrladdr, res->ai_addr,
931 res->ai_addrlen);
932 if (ISMUX(sep)) {
933 sep->se_fd = -1;
934 freeaddrinfo(res);
935 continue;
936 }
937 sep->se_ctrladdr_size = res->ai_addrlen;
938 freeaddrinfo(res);
939 #ifdef RPC
940 if (isrpcservice(sep)) {
941 struct rpcent *rp;
942
943 sep->se_rpcprog = atoi(sep->se_service);
944 if (sep->se_rpcprog == 0) {
945 rp = getrpcbyname(sep->se_service);
946 if (rp == 0) {
947 syslog(LOG_ERR,
948 "%s/%s: unknown service",
949 sep->se_service,
950 sep->se_proto);
951 sep->se_checked = 0;
952 continue;
953 }
954 sep->se_rpcprog = rp->r_number;
955 }
956 if (sep->se_fd == -1 && !ISMUX(sep))
957 setup(sep);
958 if (sep->se_fd != -1)
959 register_rpc(sep);
960 } else
961 #endif
962 {
963 if (sep->se_fd >= 0)
964 close_sep(sep);
965 if (sep->se_fd == -1 && !ISMUX(sep))
966 setup(sep);
967 }
968 }
969 }
970 }
971 endconfig();
972 /*
973 * Purge anything not looked at above.
974 */
975 omask = sigblock(SIGBLOCK);
976 sepp = &servtab;
977 while ((sep = *sepp)) {
978 if (sep->se_checked) {
979 sepp = &sep->se_next;
980 continue;
981 }
982 *sepp = sep->se_next;
983 if (sep->se_fd >= 0)
984 close_sep(sep);
985 if (isrpcservice(sep))
986 unregister_rpc(sep);
987 if (sep->se_family == AF_LOCAL)
988 (void)unlink(sep->se_service);
989 if (debug)
990 print_service("FREE", sep);
991 freeconfig(sep);
992 free((char *)sep);
993 }
994 (void) sigsetmask(omask);
995 }
996
997 void
998 retry(signo)
999 int signo;
1000 {
1001 struct servtab *sep;
1002
1003 timingout = 0;
1004 for (sep = servtab; sep; sep = sep->se_next) {
1005 if (sep->se_fd == -1 && !ISMUX(sep)) {
1006 switch (sep->se_family) {
1007 case AF_LOCAL:
1008 case AF_INET:
1009 #ifdef INET6
1010 case AF_INET6:
1011 #endif
1012 setup(sep);
1013 if (sep->se_fd != -1 && isrpcservice(sep))
1014 register_rpc(sep);
1015 break;
1016 }
1017 }
1018 }
1019 }
1020
1021 void
1022 goaway(signo)
1023 int signo;
1024 {
1025 struct servtab *sep;
1026
1027 for (sep = servtab; sep; sep = sep->se_next) {
1028 if (sep->se_fd == -1)
1029 continue;
1030
1031 switch (sep->se_family) {
1032 case AF_LOCAL:
1033 (void)unlink(sep->se_service);
1034 break;
1035 case AF_INET:
1036 #ifdef INET6
1037 case AF_INET6:
1038 #endif
1039 if (sep->se_wait == 1 && isrpcservice(sep))
1040 unregister_rpc(sep);
1041 break;
1042 }
1043 (void)close(sep->se_fd);
1044 }
1045 exit(0);
1046 }
1047
1048 void
1049 setup(sep)
1050 struct servtab *sep;
1051 {
1052 int on = 1;
1053
1054 if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
1055 if (debug)
1056 fprintf(stderr, "socket failed on %s/%s: %s\n",
1057 sep->se_service, sep->se_proto, strerror(errno));
1058 syslog(LOG_ERR, "%s/%s: socket: %m",
1059 sep->se_service, sep->se_proto);
1060 return;
1061 }
1062 /* Set all listening sockets to close-on-exec. */
1063 if (fcntl(sep->se_fd, F_SETFD, FD_CLOEXEC) < 0)
1064 syslog(LOG_ERR, "fcntl (F_SETFD, FD_CLOEXEC): %m");
1065
1066 #define turnon(fd, opt) \
1067 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
1068 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
1069 turnon(sep->se_fd, SO_DEBUG) < 0)
1070 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
1071 if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
1072 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
1073 #undef turnon
1074
1075 /* Set the socket buffer sizes, if specified. */
1076 if (sep->se_sndbuf != 0 && setsockopt(sep->se_fd, SOL_SOCKET,
1077 SO_SNDBUF, (char *)&sep->se_sndbuf, sizeof(sep->se_sndbuf)) < 0)
1078 syslog(LOG_ERR, "setsockopt (SO_SNDBUF %d): %m",
1079 sep->se_sndbuf);
1080 if (sep->se_rcvbuf != 0 && setsockopt(sep->se_fd, SOL_SOCKET,
1081 SO_RCVBUF, (char *)&sep->se_rcvbuf, sizeof(sep->se_rcvbuf)) < 0)
1082 syslog(LOG_ERR, "setsockopt (SO_RCVBUF %d): %m",
1083 sep->se_rcvbuf);
1084 if (sep->se_type == FAITH_TYPE && setsockopt(sep->se_fd, IPPROTO_IPV6,
1085 IPV6_FAITH, (char *)&on, sizeof(on)) < 0)
1086 syslog(LOG_ERR, "setsockopt (IPV6_FAITH): %m");
1087 #ifdef IPSEC
1088 if (ipsecsetup(sep->se_family, sep->se_fd, sep->se_policy) < 0 &&
1089 sep->se_policy) {
1090 syslog(LOG_ERR, "%s/%s: ipsec setup failed",
1091 sep->se_service, sep->se_proto);
1092 (void)close(sep->se_fd);
1093 sep->se_fd = -1;
1094 return;
1095 }
1096 #endif
1097
1098 if (bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size) < 0) {
1099 if (debug)
1100 fprintf(stderr, "bind failed on %s/%s: %s\n",
1101 sep->se_service, sep->se_proto, strerror(errno));
1102 syslog(LOG_ERR, "%s/%s: bind: %m",
1103 sep->se_service, sep->se_proto);
1104 (void) close(sep->se_fd);
1105 sep->se_fd = -1;
1106 if (!timingout) {
1107 timingout = 1;
1108 alarm(RETRYTIME);
1109 }
1110 return;
1111 }
1112 if (sep->se_socktype == SOCK_STREAM)
1113 listen(sep->se_fd, 10);
1114
1115 FD_SET(sep->se_fd, &allsock);
1116 nsock++;
1117 if (sep->se_fd > maxsock) {
1118 maxsock = sep->se_fd;
1119 if (maxsock > rlim_ofile_cur - FD_MARGIN)
1120 bump_nofile();
1121 }
1122 if (debug)
1123 fprintf(stderr, "registered %s on %d\n",
1124 sep->se_server, sep->se_fd);
1125 }
1126
1127 /*
1128 * Finish with a service and its socket.
1129 */
1130 void
1131 close_sep(sep)
1132 struct servtab *sep;
1133 {
1134 if (sep->se_fd >= 0) {
1135 nsock--;
1136 FD_CLR(sep->se_fd, &allsock);
1137 (void) close(sep->se_fd);
1138 sep->se_fd = -1;
1139 }
1140 sep->se_count = 0;
1141 /*
1142 * Don't keep the pid of this running daemon: when reapchild()
1143 * reaps this pid, it would erroneously increment nsock.
1144 */
1145 if (sep->se_wait > 1)
1146 sep->se_wait = 1;
1147 }
1148
1149 void
1150 register_rpc(sep)
1151 struct servtab *sep;
1152 {
1153 #ifdef RPC
1154 int n;
1155 struct netbuf nbuf;
1156 struct sockaddr_storage ss;
1157 struct netconfig *nconf;
1158
1159 if ((nconf = getnetconfigent(sep->se_proto+4)) == NULL) {
1160 syslog(LOG_ERR, "%s: getnetconfigent failed",
1161 sep->se_proto);
1162 return;
1163 }
1164 n = sizeof ss;
1165 if (getsockname(sep->se_fd, (struct sockaddr *)&ss, &n) < 0) {
1166 syslog(LOG_ERR, "%s/%s: getsockname: %m",
1167 sep->se_service, sep->se_proto);
1168 return;
1169 }
1170
1171 nbuf.buf = &ss;
1172 nbuf.len = ss.ss_len;
1173 nbuf.maxlen = sizeof (struct sockaddr_storage);
1174 for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
1175 if (debug)
1176 fprintf(stderr, "rpcb_set: %u %d %s %s\n",
1177 sep->se_rpcprog, n, nconf->nc_netid,
1178 taddr2uaddr(nconf, &nbuf));
1179 (void)rpcb_unset(sep->se_rpcprog, n, nconf);
1180 if (!rpcb_set(sep->se_rpcprog, n, nconf, &nbuf))
1181 syslog(LOG_ERR, "rpcb_set: %u %d %s %s%s",
1182 sep->se_rpcprog, n, nconf->nc_netid,
1183 taddr2uaddr(nconf, &nbuf), clnt_spcreateerror(""));
1184 }
1185 #endif /* RPC */
1186 }
1187
1188 void
1189 unregister_rpc(sep)
1190 struct servtab *sep;
1191 {
1192 #ifdef RPC
1193 int n;
1194 struct netconfig *nconf;
1195
1196 if ((nconf = getnetconfigent(sep->se_proto+4)) == NULL) {
1197 syslog(LOG_ERR, "%s: getnetconfigent failed",
1198 sep->se_proto);
1199 return;
1200 }
1201
1202 for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
1203 if (debug)
1204 fprintf(stderr, "rpcb_unset(%u, %d, %s)\n",
1205 sep->se_rpcprog, n, nconf->nc_netid);
1206 if (!rpcb_unset(sep->se_rpcprog, n, nconf))
1207 syslog(LOG_ERR, "rpcb_unset(%u, %d, %s) failed\n",
1208 sep->se_rpcprog, n, nconf->nc_netid);
1209 }
1210 #endif /* RPC */
1211 }
1212
1213
1214 struct servtab *
1215 enter(cp)
1216 struct servtab *cp;
1217 {
1218 struct servtab *sep;
1219 long omask;
1220
1221 sep = (struct servtab *)malloc(sizeof (*sep));
1222 if (sep == (struct servtab *)0) {
1223 syslog(LOG_ERR, "Out of memory.");
1224 exit(1);
1225 }
1226 *sep = *cp;
1227 sep->se_fd = -1;
1228 sep->se_rpcprog = -1;
1229 omask = sigblock(SIGBLOCK);
1230 sep->se_next = servtab;
1231 servtab = sep;
1232 sigsetmask(omask);
1233 return (sep);
1234 }
1235
1236 FILE *fconfig = NULL;
1237 struct servtab serv;
1238 char line[LINE_MAX];
1239 char *defhost;
1240 #ifdef IPSEC
1241 static char *policy = NULL;
1242 #endif
1243
1244 int
1245 setconfig()
1246 {
1247 if (defhost)
1248 free(defhost);
1249 defhost = newstr("*");
1250 #ifdef IPSEC
1251 if (policy)
1252 free(policy);
1253 policy = NULL;
1254 #endif
1255 if (fconfig != NULL) {
1256 fseek(fconfig, 0L, SEEK_SET);
1257 return (1);
1258 }
1259 fconfig = fopen(CONFIG, "r");
1260 return (fconfig != NULL);
1261 }
1262
1263 void
1264 endconfig()
1265 {
1266 if (fconfig) {
1267 (void) fclose(fconfig);
1268 fconfig = NULL;
1269 }
1270 if (defhost) {
1271 free(defhost);
1272 defhost = 0;
1273 }
1274 }
1275
1276 struct servtab *
1277 getconfigent()
1278 {
1279 struct servtab *sep = &serv;
1280 int argc, val;
1281 char *cp, *cp0, *arg, *buf0, *buf1, *sz0, *sz1;
1282 static char TCPMUX_TOKEN[] = "tcpmux/";
1283 #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
1284 char *hostdelim;
1285
1286 more:
1287 while ((cp = nextline(fconfig))) {
1288 #ifdef IPSEC
1289 /* lines starting with #@ is not a comment, but the policy */
1290 if (cp[0] == '#' && cp[1] == '@') {
1291 char *p;
1292 for (p = cp + 2; p && *p && isspace(*p); p++)
1293 ;
1294 if (*p == '\0') {
1295 if (policy)
1296 free(policy);
1297 policy = NULL;
1298 } else {
1299 if (ipsecsetup_test(p) < 0) {
1300 syslog(LOG_ERR,
1301 "%s: invalid ipsec policy \"%s\"",
1302 CONFIG, p);
1303 exit(1);
1304 } else {
1305 if (policy)
1306 free(policy);
1307 policy = newstr(p);
1308 }
1309 }
1310 }
1311 #endif
1312 if (*cp == '#' || *cp == '\0')
1313 continue;
1314 #ifdef MULOG
1315 /* Avoid use of `skip' if there is a danger of it looking
1316 * at continuation lines.
1317 */
1318 do {
1319 cp++;
1320 } while (*cp == ' ' || *cp == '\t');
1321 if (*cp == '\0')
1322 continue;
1323 if ((arg = skip(&cp)) == NULL)
1324 continue;
1325 if (strcmp(arg, "DOMAIN"))
1326 continue;
1327 if (curdom)
1328 free(curdom);
1329 curdom = NULL;
1330 while (*cp == ' ' || *cp == '\t')
1331 cp++;
1332 if (*cp == '\0')
1333 continue;
1334 arg = cp;
1335 while (*cp && *cp != ' ' && *cp != '\t')
1336 cp++;
1337 if (*cp != '\0')
1338 *cp++ = '\0';
1339 curdom = newstr(arg);
1340 #endif
1341 break;
1342 }
1343 if (cp == NULL)
1344 return ((struct servtab *)0);
1345 /*
1346 * clear the static buffer, since some fields (se_ctrladdr,
1347 * for example) don't get initialized here.
1348 */
1349 memset((caddr_t)sep, 0, sizeof *sep);
1350 arg = skip(&cp);
1351 if (cp == NULL) {
1352 /* got an empty line containing just blanks/tabs. */
1353 goto more;
1354 }
1355 /* Check for a host name. */
1356 hostdelim = strrchr(arg, ':');
1357 if (hostdelim) {
1358 *hostdelim = '\0';
1359 if (arg[0] == '[' && hostdelim > arg && hostdelim[-1] == ']') {
1360 hostdelim[-1] = '\0';
1361 sep->se_hostaddr = newstr(arg + 1);
1362 } else
1363 sep->se_hostaddr = newstr(arg);
1364 arg = hostdelim + 1;
1365 /*
1366 * If the line is of the form `host:', then just change the
1367 * default host for the following lines.
1368 */
1369 if (*arg == '\0') {
1370 arg = skip(&cp);
1371 if (cp == NULL) {
1372 free(defhost);
1373 defhost = sep->se_hostaddr;
1374 goto more;
1375 }
1376 }
1377 } else
1378 sep->se_hostaddr = newstr(defhost);
1379 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
1380 char *c = arg + MUX_LEN;
1381 if (*c == '+') {
1382 sep->se_type = MUXPLUS_TYPE;
1383 c++;
1384 } else
1385 sep->se_type = MUX_TYPE;
1386 sep->se_service = newstr(c);
1387 } else {
1388 sep->se_service = newstr(arg);
1389 sep->se_type = NORM_TYPE;
1390 }
1391
1392 arg = sskip(&cp);
1393 if (strcmp(arg, "stream") == 0)
1394 sep->se_socktype = SOCK_STREAM;
1395 else if (strcmp(arg, "dgram") == 0)
1396 sep->se_socktype = SOCK_DGRAM;
1397 else if (strcmp(arg, "rdm") == 0)
1398 sep->se_socktype = SOCK_RDM;
1399 else if (strcmp(arg, "seqpacket") == 0)
1400 sep->se_socktype = SOCK_SEQPACKET;
1401 else if (strcmp(arg, "raw") == 0)
1402 sep->se_socktype = SOCK_RAW;
1403 else
1404 sep->se_socktype = -1;
1405
1406 arg = sskip(&cp);
1407 if (sep->se_type == NORM_TYPE &&
1408 strncmp(arg, "faith/", strlen("faith/")) == 0) {
1409 arg += strlen("faith/");
1410 sep->se_type = FAITH_TYPE;
1411 }
1412 sep->se_proto = newstr(arg);
1413
1414 #define MALFORMED(arg) \
1415 do { \
1416 syslog(LOG_ERR, "%s: malformed buffer size option `%s'", \
1417 sep->se_service, (arg)); \
1418 goto more; \
1419 } while (0)
1420
1421 #define GETVAL(arg) \
1422 do { \
1423 if (!isdigit(*(arg))) \
1424 MALFORMED(arg); \
1425 val = strtol((arg), &cp0, 10); \
1426 if (cp0 != NULL) { \
1427 if (cp0[1] != '\0') \
1428 MALFORMED((arg)); \
1429 if (cp0[0] == 'k') \
1430 val *= 1024; \
1431 if (cp0[0] == 'm') \
1432 val *= 1024 * 1024; \
1433 } \
1434 if (val < 1) { \
1435 syslog(LOG_ERR, "%s: invalid buffer size `%s'", \
1436 sep->se_service, (arg)); \
1437 goto more; \
1438 } \
1439 } while (0)
1440
1441 #define ASSIGN(arg) \
1442 do { \
1443 if (strcmp((arg), "sndbuf") == 0) \
1444 sep->se_sndbuf = val; \
1445 else if (strcmp((arg), "rcvbuf") == 0) \
1446 sep->se_rcvbuf = val; \
1447 else \
1448 MALFORMED((arg)); \
1449 } while (0)
1450
1451 /*
1452 * Extract the send and receive buffer sizes before parsing
1453 * the protocol.
1454 */
1455 sep->se_sndbuf = sep->se_rcvbuf = 0;
1456 buf0 = buf1 = sz0 = sz1 = NULL;
1457 if ((buf0 = strchr(sep->se_proto, ',')) != NULL) {
1458 /* Not meaningful for Tcpmux services. */
1459 if (ISMUX(sep)) {
1460 syslog(LOG_ERR, "%s: can't specify buffer sizes for "
1461 "tcpmux services", sep->se_service);
1462 goto more;
1463 }
1464
1465 /* Skip the , */
1466 *buf0++ = '\0';
1467
1468 /* Check to see if another socket buffer size was specified. */
1469 if ((buf1 = strchr(buf0, ',')) != NULL) {
1470 /* Skip the , */
1471 *buf1++ = '\0';
1472
1473 /* Make sure a 3rd one wasn't specified. */
1474 if (strchr(buf1, ',') != NULL) {
1475 syslog(LOG_ERR, "%s: too many buffer sizes",
1476 sep->se_service);
1477 goto more;
1478 }
1479
1480 /* Locate the size. */
1481 if ((sz1 = strchr(buf1, '=')) == NULL)
1482 MALFORMED(buf1);
1483
1484 /* Skip the = */
1485 *sz1++ = '\0';
1486 }
1487
1488 /* Locate the size. */
1489 if ((sz0 = strchr(buf0, '=')) == NULL)
1490 MALFORMED(buf0);
1491
1492 /* Skip the = */
1493 *sz0++ = '\0';
1494
1495 GETVAL(sz0);
1496 ASSIGN(buf0);
1497
1498 if (buf1 != NULL) {
1499 GETVAL(sz1);
1500 ASSIGN(buf1);
1501 }
1502 }
1503
1504 #undef ASSIGN
1505 #undef GETVAL
1506 #undef MALFORMED
1507
1508 if (strcmp(sep->se_proto, "unix") == 0) {
1509 sep->se_family = AF_LOCAL;
1510 } else {
1511 val = strlen(sep->se_proto);
1512 if (!val) {
1513 syslog(LOG_ERR, "%s: invalid protocol specified",
1514 sep->se_service);
1515 goto more;
1516 }
1517 val = sep->se_proto[val - 1];
1518 switch (val) {
1519 case '4': /*tcp4 or udp4*/
1520 sep->se_family = AF_INET;
1521 break;
1522 #ifdef INET6
1523 case '6': /*tcp6 or udp6*/
1524 sep->se_family = AF_INET6;
1525 break;
1526 #endif
1527 default:
1528 sep->se_family = AF_INET; /*will become AF_INET6*/
1529 break;
1530 }
1531 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1532 #ifdef RPC
1533 char *cp, *ccp;
1534 cp = strchr(sep->se_service, '/');
1535 if (cp == 0) {
1536 syslog(LOG_ERR, "%s: no rpc version",
1537 sep->se_service);
1538 goto more;
1539 }
1540 *cp++ = '\0';
1541 sep->se_rpcversl = sep->se_rpcversh =
1542 strtol(cp, &ccp, 0);
1543 if (ccp == cp) {
1544 badafterall:
1545 syslog(LOG_ERR, "%s/%s: bad rpc version",
1546 sep->se_service, cp);
1547 goto more;
1548 }
1549 if (*ccp == '-') {
1550 cp = ccp + 1;
1551 sep->se_rpcversh = strtol(cp, &ccp, 0);
1552 if (ccp == cp)
1553 goto badafterall;
1554 }
1555 #else
1556 syslog(LOG_ERR, "%s: rpc services not suported",
1557 sep->se_service);
1558 goto more;
1559 #endif /* RPC */
1560 }
1561 }
1562 arg = sskip(&cp);
1563 {
1564 char *cp;
1565 if ((cp = strchr(arg, ':')) == NULL)
1566 cp = strchr(arg, '.');
1567 if (cp != NULL) {
1568 *cp++ = '\0';
1569 sep->se_max = atoi(cp);
1570 } else
1571 sep->se_max = TOOMANY;
1572 }
1573 sep->se_wait = strcmp(arg, "wait") == 0;
1574 if (ISMUX(sep)) {
1575 /*
1576 * Silently enforce "nowait" for TCPMUX services since
1577 * they don't have an assigned port to listen on.
1578 */
1579 sep->se_wait = 0;
1580
1581 if (strncmp(sep->se_proto, "tcp", 3)) {
1582 syslog(LOG_ERR,
1583 "%s: bad protocol for tcpmux service %s",
1584 CONFIG, sep->se_service);
1585 goto more;
1586 }
1587 if (sep->se_socktype != SOCK_STREAM) {
1588 syslog(LOG_ERR,
1589 "%s: bad socket type for tcpmux service %s",
1590 CONFIG, sep->se_service);
1591 goto more;
1592 }
1593 }
1594 sep->se_user = newstr(sskip(&cp));
1595 if ((sep->se_group = strchr(sep->se_user, ':')) != NULL)
1596 *sep->se_group++ = '\0';
1597 else if ((sep->se_group = strchr(sep->se_user, '.')) != NULL)
1598 *sep->se_group++ = '\0';
1599
1600 sep->se_server = newstr(sskip(&cp));
1601 if (strcmp(sep->se_server, "internal") == 0) {
1602 struct biltin *bi;
1603
1604 for (bi = biltins; bi->bi_service; bi++)
1605 if (bi->bi_socktype == sep->se_socktype &&
1606 strcmp(bi->bi_service, sep->se_service) == 0)
1607 break;
1608 if (bi->bi_service == 0) {
1609 syslog(LOG_ERR, "internal service %s unknown",
1610 sep->se_service);
1611 goto more;
1612 }
1613 sep->se_bi = bi;
1614 sep->se_wait = bi->bi_wait;
1615 } else
1616 sep->se_bi = NULL;
1617 argc = 0;
1618 for (arg = skip(&cp); cp; arg = skip(&cp)) {
1619 #if MULOG
1620 char *colon;
1621
1622 if (argc == 0 && (colon = strrchr(arg, ':'))) {
1623 while (arg < colon) {
1624 int x;
1625 char *ccp;
1626
1627 switch (*arg++) {
1628 case 'l':
1629 x = 1;
1630 if (isdigit(*arg)) {
1631 x = strtol(arg, &ccp, 0);
1632 if (ccp == arg)
1633 break;
1634 arg = ccp;
1635 }
1636 sep->se_log &= ~MULOG_RFC931;
1637 sep->se_log |= x;
1638 break;
1639 case 'a':
1640 sep->se_log |= MULOG_RFC931;
1641 break;
1642 default:
1643 break;
1644 }
1645 }
1646 arg = colon + 1;
1647 }
1648 #endif
1649 if (argc < MAXARGV)
1650 sep->se_argv[argc++] = newstr(arg);
1651 }
1652 while (argc <= MAXARGV)
1653 sep->se_argv[argc++] = NULL;
1654 #ifdef IPSEC
1655 sep->se_policy = policy ? newstr(policy) : NULL;
1656 #endif
1657 return (sep);
1658 }
1659
1660 void
1661 freeconfig(cp)
1662 struct servtab *cp;
1663 {
1664 int i;
1665
1666 if (cp->se_hostaddr)
1667 free(cp->se_hostaddr);
1668 if (cp->se_service)
1669 free(cp->se_service);
1670 if (cp->se_proto)
1671 free(cp->se_proto);
1672 if (cp->se_user)
1673 free(cp->se_user);
1674 /* Note: se_group is part of the newstr'ed se_user */
1675 if (cp->se_server)
1676 free(cp->se_server);
1677 for (i = 0; i < MAXARGV; i++)
1678 if (cp->se_argv[i])
1679 free(cp->se_argv[i]);
1680 #ifdef IPSEC
1681 if (cp->se_policy)
1682 free(cp->se_policy);
1683 #endif
1684 }
1685
1686
1687 /*
1688 * Safe skip - if skip returns null, log a syntax error in the
1689 * configuration file and exit.
1690 */
1691 char *
1692 sskip(cpp)
1693 char **cpp;
1694 {
1695 char *cp;
1696
1697 cp = skip(cpp);
1698 if (cp == NULL) {
1699 syslog(LOG_ERR, "%s: syntax error", CONFIG);
1700 exit(1);
1701 }
1702 return (cp);
1703 }
1704
1705 char *
1706 skip(cpp)
1707 char **cpp;
1708 {
1709 char *cp = *cpp;
1710 char *start;
1711
1712 if (*cpp == NULL)
1713 return ((char *)0);
1714
1715 again:
1716 while (*cp == ' ' || *cp == '\t')
1717 cp++;
1718 if (*cp == '\0') {
1719 int c;
1720
1721 c = getc(fconfig);
1722 (void) ungetc(c, fconfig);
1723 if (c == ' ' || c == '\t')
1724 if ((cp = nextline(fconfig)))
1725 goto again;
1726 *cpp = (char *)0;
1727 return ((char *)0);
1728 }
1729 start = cp;
1730 while (*cp && *cp != ' ' && *cp != '\t')
1731 cp++;
1732 if (*cp != '\0')
1733 *cp++ = '\0';
1734 *cpp = cp;
1735 return (start);
1736 }
1737
1738 char *
1739 nextline(fd)
1740 FILE *fd;
1741 {
1742 char *cp;
1743
1744 if (fgets(line, sizeof (line), fd) == NULL)
1745 return ((char *)0);
1746 cp = strchr(line, '\n');
1747 if (cp)
1748 *cp = '\0';
1749 return (line);
1750 }
1751
1752 char *
1753 newstr(cp)
1754 char *cp;
1755 {
1756 if ((cp = strdup(cp ? cp : "")))
1757 return (cp);
1758 syslog(LOG_ERR, "strdup: %m");
1759 exit(1);
1760 }
1761
1762 void
1763 inetd_setproctitle(a, s)
1764 char *a;
1765 int s;
1766 {
1767 socklen_t size;
1768 struct sockaddr_storage ss;
1769 char hbuf[NI_MAXHOST];
1770
1771 size = sizeof(ss);
1772 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) {
1773 if (getnameinfo((struct sockaddr *)&ss, size, hbuf,
1774 sizeof(hbuf), NULL, 0, niflags) == 0)
1775 setproctitle("-%s [%s]", a, hbuf);
1776 else
1777 setproctitle("-%s [?]", a);
1778 } else
1779 setproctitle("-%s", a);
1780 }
1781
1782 void
1783 bump_nofile()
1784 {
1785 #ifdef RLIMIT_NOFILE
1786
1787 #define FD_CHUNK 32
1788
1789 struct rlimit rl;
1790
1791 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1792 syslog(LOG_ERR, "getrlimit: %m");
1793 return;
1794 }
1795 rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK);
1796 if (rl.rlim_cur <= rlim_ofile_cur) {
1797 syslog(LOG_ERR,
1798 "bump_nofile: cannot extend file limit, max = %d",
1799 (int)rl.rlim_cur);
1800 return;
1801 }
1802
1803 if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
1804 syslog(LOG_ERR, "setrlimit: %m");
1805 return;
1806 }
1807
1808 rlim_ofile_cur = rl.rlim_cur;
1809 return;
1810
1811 #else
1812 syslog(LOG_ERR, "bump_nofile: cannot extend file limit");
1813 return;
1814 #endif
1815 }
1816
1817 /*
1818 * Internet services provided internally by inetd:
1819 */
1820 #define BUFSIZE 4096
1821
1822 /* ARGSUSED */
1823 void
1824 echo_stream(s, sep) /* Echo service -- echo data back */
1825 int s;
1826 struct servtab *sep;
1827 {
1828 char buffer[BUFSIZE];
1829 int i;
1830
1831 inetd_setproctitle(sep->se_service, s);
1832 while ((i = read(s, buffer, sizeof(buffer))) > 0 &&
1833 write(s, buffer, i) > 0)
1834 ;
1835 }
1836
1837 /* ARGSUSED */
1838 void
1839 echo_dg(s, sep) /* Echo service -- echo data back */
1840 int s;
1841 struct servtab *sep;
1842 {
1843 char buffer[BUFSIZE];
1844 int i;
1845 socklen_t size;
1846 struct sockaddr_storage ss;
1847 struct sockaddr *sa;
1848
1849 sa = (struct sockaddr *)&ss;
1850 size = sizeof(ss);
1851 if ((i = recvfrom(s, buffer, sizeof(buffer), 0, sa, &size)) < 0)
1852 return;
1853 if (port_good_dg(sa))
1854 (void) sendto(s, buffer, i, 0, sa, size);
1855 }
1856
1857 /* ARGSUSED */
1858 void
1859 discard_stream(s, sep) /* Discard service -- ignore data */
1860 int s;
1861 struct servtab *sep;
1862 {
1863 char buffer[BUFSIZE];
1864
1865 inetd_setproctitle(sep->se_service, s);
1866 while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) ||
1867 errno == EINTR)
1868 ;
1869 }
1870
1871 /* ARGSUSED */
1872 void
1873 discard_dg(s, sep) /* Discard service -- ignore data */
1874 int s;
1875 struct servtab *sep;
1876 {
1877 char buffer[BUFSIZE];
1878
1879 (void) read(s, buffer, sizeof(buffer));
1880 }
1881
1882 #include <ctype.h>
1883 #define LINESIZ 72
1884 char ring[128];
1885 char *endring;
1886
1887 void
1888 initring()
1889 {
1890 int i;
1891
1892 endring = ring;
1893
1894 for (i = 0; i <= 128; ++i)
1895 if (isprint(i))
1896 *endring++ = i;
1897 }
1898
1899 /* ARGSUSED */
1900 void
1901 chargen_stream(s, sep) /* Character generator */
1902 int s;
1903 struct servtab *sep;
1904 {
1905 int len;
1906 char *rs, text[LINESIZ+2];
1907
1908 inetd_setproctitle(sep->se_service, s);
1909
1910 if (!endring) {
1911 initring();
1912 rs = ring;
1913 }
1914
1915 text[LINESIZ] = '\r';
1916 text[LINESIZ + 1] = '\n';
1917 for (rs = ring;;) {
1918 if ((len = endring - rs) >= LINESIZ)
1919 memmove(text, rs, LINESIZ);
1920 else {
1921 memmove(text, rs, len);
1922 memmove(text + len, ring, LINESIZ - len);
1923 }
1924 if (++rs == endring)
1925 rs = ring;
1926 if (write(s, text, sizeof(text)) != sizeof(text))
1927 break;
1928 }
1929 }
1930
1931 /* ARGSUSED */
1932 void
1933 chargen_dg(s, sep) /* Character generator */
1934 int s;
1935 struct servtab *sep;
1936 {
1937 struct sockaddr_storage ss;
1938 struct sockaddr *sa;
1939 static char *rs;
1940 int len;
1941 socklen_t size;
1942 char text[LINESIZ+2];
1943
1944 if (endring == 0) {
1945 initring();
1946 rs = ring;
1947 }
1948
1949 sa = (struct sockaddr *)&ss;
1950 size = sizeof(ss);
1951 if (recvfrom(s, text, sizeof(text), 0, sa, &size) < 0)
1952 return;
1953
1954 if (!port_good_dg(sa))
1955 return;
1956
1957 if ((len = endring - rs) >= LINESIZ)
1958 memmove(text, rs, LINESIZ);
1959 else {
1960 memmove(text, rs, len);
1961 memmove(text + len, ring, LINESIZ - len);
1962 }
1963 if (++rs == endring)
1964 rs = ring;
1965 text[LINESIZ] = '\r';
1966 text[LINESIZ + 1] = '\n';
1967 (void) sendto(s, text, sizeof(text), 0, sa, size);
1968 }
1969
1970 /*
1971 * Return a machine readable date and time, in the form of the
1972 * number of seconds since midnight, Jan 1, 1900. Since gettimeofday
1973 * returns the number of seconds since midnight, Jan 1, 1970,
1974 * we must add 2208988800 seconds to this figure to make up for
1975 * some seventy years Bell Labs was asleep.
1976 */
1977
1978 uint32_t
1979 machtime()
1980 {
1981 struct timeval tv;
1982
1983 if (gettimeofday(&tv, (struct timezone *)0) < 0) {
1984 if (debug)
1985 fprintf(stderr, "Unable to get time of day\n");
1986 return (0);
1987 }
1988 #define OFFSET ((uint32_t)25567 * 24*60*60)
1989 return (htonl((uint32_t)(tv.tv_sec + OFFSET)));
1990 #undef OFFSET
1991 }
1992
1993 /* ARGSUSED */
1994 void
1995 machtime_stream(s, sep)
1996 int s;
1997 struct servtab *sep;
1998 {
1999 uint32_t result;
2000
2001 result = machtime();
2002 (void) write(s, (char *) &result, sizeof(result));
2003 }
2004
2005 /* ARGSUSED */
2006 void
2007 machtime_dg(s, sep)
2008 int s;
2009 struct servtab *sep;
2010 {
2011 uint32_t result;
2012 struct sockaddr_storage ss;
2013 struct sockaddr *sa;
2014 socklen_t size;
2015
2016 sa = (struct sockaddr *)&ss;
2017 size = sizeof(ss);
2018 if (recvfrom(s, (char *)&result, sizeof(result), 0, sa, &size) < 0)
2019 return;
2020 if (!port_good_dg(sa))
2021 return;
2022 result = machtime();
2023 (void) sendto(s, (char *) &result, sizeof(result), 0, sa, size);
2024 }
2025
2026 /* ARGSUSED */
2027 void
2028 daytime_stream(s, sep) /* Return human-readable time of day */
2029 int s;
2030 struct servtab *sep;
2031 {
2032 char buffer[256];
2033 time_t clock;
2034 int len;
2035
2036 clock = time((time_t *) 0);
2037
2038 len = snprintf(buffer, sizeof buffer, "%.24s\r\n", ctime(&clock));
2039 (void) write(s, buffer, len);
2040 }
2041
2042 /* ARGSUSED */
2043 void
2044 daytime_dg(s, sep) /* Return human-readable time of day */
2045 int s;
2046 struct servtab *sep;
2047 {
2048 char buffer[256];
2049 time_t clock;
2050 struct sockaddr_storage ss;
2051 struct sockaddr *sa;
2052 socklen_t size;
2053 int len;
2054
2055 clock = time((time_t *) 0);
2056
2057 sa = (struct sockaddr *)&ss;
2058 size = sizeof(ss);
2059 if (recvfrom(s, buffer, sizeof(buffer), 0, sa, &size) < 0)
2060 return;
2061 if (!port_good_dg(sa))
2062 return;
2063 len = snprintf(buffer, sizeof buffer, "%.24s\r\n", ctime(&clock));
2064 (void) sendto(s, buffer, len, 0, sa, size);
2065 }
2066
2067 /*
2068 * print_service:
2069 * Dump relevant information to stderr
2070 */
2071 void
2072 print_service(action, sep)
2073 char *action;
2074 struct servtab *sep;
2075 {
2076
2077 if (isrpcservice(sep))
2078 fprintf(stderr,
2079 "%s: %s rpcprog=%d, rpcvers = %d/%d, proto=%s, wait:max=%d.%d, user:group=%s.%s builtin=%lx server=%s"
2080 #ifdef IPSEC
2081 " policy=\"%s\""
2082 #endif
2083 "\n",
2084 action, sep->se_service,
2085 sep->se_rpcprog, sep->se_rpcversh, sep->se_rpcversl, sep->se_proto,
2086 sep->se_wait, sep->se_max, sep->se_user, sep->se_group,
2087 (long)sep->se_bi, sep->se_server
2088 #ifdef IPSEC
2089 , (sep->se_policy ? sep->se_policy : "")
2090 #endif
2091 );
2092 else
2093 fprintf(stderr,
2094 "%s: %s proto=%s%s, wait:max=%d.%d, user:group=%s.%s builtin=%lx server=%s"
2095 #ifdef IPSEC
2096 " policy=%s"
2097 #endif
2098 "\n",
2099 action, sep->se_service,
2100 sep->se_type == FAITH_TYPE ? "faith/" : "",
2101 sep->se_proto,
2102 sep->se_wait, sep->se_max, sep->se_user, sep->se_group,
2103 (long)sep->se_bi, sep->se_server
2104 #ifdef IPSEC
2105 , (sep->se_policy ? sep->se_policy : "")
2106 #endif
2107 );
2108 }
2109
2110 void
2111 usage()
2112 {
2113
2114 #ifdef LIBWRAP
2115 (void)fprintf(stderr, "usage: %s [-dl] [conf]\n", getprogname());
2116 #else
2117 (void)fprintf(stderr, "usage: %s [-d] [conf]\n", getprogname());
2118 #endif
2119 exit(1);
2120 }
2121
2122
2123 /*
2124 * Based on TCPMUX.C by Mark K. Lottor November 1988
2125 * sri-nic::ps:<mkl>tcpmux.c
2126 */
2127
2128 static int /* # of characters upto \r,\n or \0 */
2129 getline(fd, buf, len)
2130 int fd;
2131 char *buf;
2132 int len;
2133 {
2134 int count = 0, n;
2135
2136 do {
2137 n = read(fd, buf, len-count);
2138 if (n == 0)
2139 return (count);
2140 if (n < 0)
2141 return (-1);
2142 while (--n >= 0) {
2143 if (*buf == '\r' || *buf == '\n' || *buf == '\0')
2144 return (count);
2145 count++;
2146 buf++;
2147 }
2148 } while (count < len);
2149 return (count);
2150 }
2151
2152 #define MAX_SERV_LEN (256+2) /* 2 bytes for \r\n */
2153
2154 #define strwrite(fd, buf) (void) write(fd, buf, sizeof(buf)-1)
2155
2156 void
2157 tcpmux(ctrl, sep)
2158 int ctrl;
2159 struct servtab *sep;
2160 {
2161 char service[MAX_SERV_LEN+1];
2162 int len;
2163
2164 /* Get requested service name */
2165 if ((len = getline(ctrl, service, MAX_SERV_LEN)) < 0) {
2166 strwrite(ctrl, "-Error reading service name\r\n");
2167 goto reject;
2168 }
2169 service[len] = '\0';
2170
2171 if (debug)
2172 fprintf(stderr, "tcpmux: someone wants %s\n", service);
2173
2174 /*
2175 * Help is a required command, and lists available services,
2176 * one per line.
2177 */
2178 if (!strcasecmp(service, "help")) {
2179 strwrite(ctrl, "+Available services:\r\n");
2180 strwrite(ctrl, "help\r\n");
2181 for (sep = servtab; sep; sep = sep->se_next) {
2182 if (!ISMUX(sep))
2183 continue;
2184 (void)write(ctrl, sep->se_service,
2185 strlen(sep->se_service));
2186 strwrite(ctrl, "\r\n");
2187 }
2188 goto reject;
2189 }
2190
2191 /* Try matching a service in inetd.conf with the request */
2192 for (sep = servtab; sep; sep = sep->se_next) {
2193 if (!ISMUX(sep))
2194 continue;
2195 if (!strcasecmp(service, sep->se_service)) {
2196 if (ISMUXPLUS(sep))
2197 strwrite(ctrl, "+Go\r\n");
2198 run_service(ctrl, sep);
2199 return;
2200 }
2201 }
2202 strwrite(ctrl, "-Service not available\r\n");
2203 reject:
2204 _exit(1);
2205 }
2206
2207 #ifdef MULOG
2208 void
2209 dolog(sep, ctrl)
2210 struct servtab *sep;
2211 int ctrl;
2212 {
2213 struct sockaddr_storage ss;
2214 struct sockaddr *sa = (struct sockaddr *)&ss;
2215 socklen_t len = sizeof(ss);
2216 char *host, *dp, buf[BUFSIZ];
2217 int connected = 1;
2218
2219 switch (sep->se_family) {
2220 case AF_INET:
2221 #ifdef INET6
2222 case AF_INET6:
2223 #endif
2224 break;
2225 default:
2226 return;
2227 }
2228
2229 if (getpeername(ctrl, sa, &len) < 0) {
2230 if (errno != ENOTCONN) {
2231 syslog(LOG_ERR, "getpeername: %m");
2232 return;
2233 }
2234 if (recvfrom(ctrl, buf, sizeof(buf), MSG_PEEK, sa, &len) < 0) {
2235 syslog(LOG_ERR, "recvfrom: %m");
2236 return;
2237 }
2238 connected = 0;
2239 }
2240 switch (sa->sa_family) {
2241 case AF_INET:
2242 #ifdef INET6
2243 case AF_INET6:
2244 #endif
2245 break;
2246 default:
2247 syslog(LOG_ERR, "unexpected address family %u", sa->sa_family);
2248 return;
2249 }
2250
2251 if (getnameinfo(sa, len, buf, sizeof(buf), NULL, 0, 0) != 0)
2252 strcpy(buf, "?");
2253 host = buf;
2254
2255 switch (sep->se_log & ~MULOG_RFC931) {
2256 case 0:
2257 return;
2258 case 1:
2259 if (curdom == NULL || *curdom == '\0')
2260 break;
2261 dp = host + strlen(host) - strlen(curdom);
2262 if (dp < host)
2263 break;
2264 if (debug)
2265 fprintf(stderr, "check \"%s\" against curdom \"%s\"\n",
2266 host, curdom);
2267 if (strcasecmp(dp, curdom) == 0)
2268 return;
2269 break;
2270 case 2:
2271 default:
2272 break;
2273 }
2274
2275 openlog("", LOG_NOWAIT, MULOG);
2276
2277 if (connected && (sep->se_log & MULOG_RFC931))
2278 syslog(LOG_INFO, "%s@%s wants %s",
2279 rfc931_name(sa, ctrl), host, sep->se_service);
2280 else
2281 syslog(LOG_INFO, "%s wants %s",
2282 host, sep->se_service);
2283 }
2284
2285 /*
2286 * From tcp_log by
2287 * Wietse Venema, Eindhoven University of Technology, The Netherlands.
2288 */
2289 #if 0
2290 static char sccsid[] = "@(#) rfc931.c 1.3 92/08/31 22:54:46";
2291 #endif
2292
2293 #include <setjmp.h>
2294
2295 #define RFC931_PORT 113 /* Semi-well-known port */
2296 #define TIMEOUT 4
2297 #define TIMEOUT2 10
2298
2299 static jmp_buf timebuf;
2300
2301 /* timeout - handle timeouts */
2302
2303 static void
2304 timeout(sig)
2305 int sig;
2306 {
2307 longjmp(timebuf, sig);
2308 }
2309
2310 /* rfc931_name - return remote user name */
2311
2312 char *
2313 rfc931_name(there, ctrl)
2314 struct sockaddr *there; /* remote link information */
2315 int ctrl;
2316 {
2317 struct sockaddr_storage here; /* local link information */
2318 struct sockaddr_storage sin; /* for talking to RFC931 daemon */
2319 socklen_t length;
2320 int s;
2321 unsigned remote;
2322 unsigned local;
2323 static char user[256]; /* XXX */
2324 char buf[256];
2325 char *cp;
2326 char *result = "USER_UNKNOWN";
2327 int len;
2328 u_int16_t myport, hisport;
2329
2330 /* Find out local port number of our stdin. */
2331
2332 length = sizeof(here);
2333 if (getsockname(ctrl, (struct sockaddr *) &here, &length) == -1) {
2334 syslog(LOG_ERR, "getsockname: %m");
2335 return (result);
2336 }
2337 switch (here.ss_family) {
2338 case AF_INET:
2339 myport = ((struct sockaddr_in *)&here)->sin_port;
2340 break;
2341 #ifdef INET6
2342 case AF_INET6:
2343 myport = ((struct sockaddr_in6 *)&here)->sin6_port;
2344 break;
2345 #endif
2346 }
2347 switch (there->sa_family) {
2348 case AF_INET:
2349 hisport = ((struct sockaddr_in *)&there)->sin_port;
2350 break;
2351 #ifdef INET6
2352 case AF_INET6:
2353 hisport = ((struct sockaddr_in6 *)&there)->sin6_port;
2354 break;
2355 #endif
2356 }
2357 /* Set up timer so we won't get stuck. */
2358
2359 if ((s = socket(here.ss_family, SOCK_STREAM, 0)) == -1) {
2360 syslog(LOG_ERR, "socket: %m");
2361 return (result);
2362 }
2363
2364 sin = here;
2365 switch (sin.ss_family) {
2366 case AF_INET:
2367 ((struct sockaddr_in *)&sin)->sin_port = htons(0);
2368 break;
2369 #ifdef INET6
2370 case AF_INET6:
2371 ((struct sockaddr_in6 *)&sin)->sin6_port = htons(0);
2372 break;
2373 #endif
2374 }
2375 if (bind(s, (struct sockaddr *) &sin, sin.ss_len) == -1) {
2376 syslog(LOG_ERR, "bind: %m");
2377 return (result);
2378 }
2379
2380 signal(SIGALRM, timeout);
2381 if (setjmp(timebuf)) {
2382 close(s); /* not: fclose(fp) */
2383 return (result);
2384 }
2385 alarm(TIMEOUT);
2386
2387 /* Connect to the RFC931 daemon. */
2388
2389 memcpy(&sin, there, there->sa_len);
2390 switch (sin.ss_family) {
2391 case AF_INET:
2392 ((struct sockaddr_in *)&sin)->sin_port = htons(RFC931_PORT);
2393 break;
2394 #ifdef INET6
2395 case AF_INET6:
2396 ((struct sockaddr_in6 *)&sin)->sin6_port = htons(RFC931_PORT);
2397 break;
2398 #endif
2399 }
2400 if (connect(s, (struct sockaddr *) &sin, sin.ss_len) == -1) {
2401 close(s);
2402 alarm(0);
2403 return (result);
2404 }
2405
2406 /* Query the RFC 931 server. Would 13-byte writes ever be broken up? */
2407 (void)snprintf(buf, sizeof buf, "%u,%u\r\n", ntohs(hisport),
2408 ntohs(myport));
2409
2410 for (len = 0, cp = buf; len < strlen(buf); ) {
2411 int n;
2412
2413 if ((n = write(s, cp, strlen(buf) - len)) == -1) {
2414 close(s);
2415 alarm(0);
2416 return (result);
2417 }
2418 cp += n;
2419 len += n;
2420 }
2421
2422 /* Read response */
2423 for (cp = buf; cp < buf + sizeof(buf) - 1; ) {
2424 char c;
2425 if (read(s, &c, 1) != 1) {
2426 close(s);
2427 alarm(0);
2428 return (result);
2429 }
2430 if (c == '\n')
2431 break;
2432 *cp++ = c;
2433 }
2434 *cp = '\0';
2435
2436 if (sscanf(buf, "%u , %u : USERID :%*[^:]:%255s", &remote, &local,
2437 user) == 3 && ntohs(hisport) == remote && ntohs(myport) == local) {
2438 /* Strip trailing carriage return. */
2439 if ((cp = strchr(user, '\r')) != NULL)
2440 *cp = 0;
2441 result = user;
2442 }
2443
2444 alarm(0);
2445 close(s);
2446 return (result);
2447 }
2448 #endif
2449
2450 /*
2451 * check if the address/port where send data to is one of the obvious ports
2452 * that are used for denial of service attacks like two echo ports
2453 * just echoing data between them
2454 */
2455 int
2456 port_good_dg(sa)
2457 struct sockaddr *sa;
2458 {
2459 struct in_addr in;
2460 #ifdef INET6
2461 struct in6_addr *in6;
2462 #endif
2463 u_int16_t port;
2464 int i, bad;
2465 char hbuf[NI_MAXHOST];
2466
2467 bad = 0;
2468
2469 switch (sa->sa_family) {
2470 case AF_INET:
2471 in.s_addr = ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
2472 port = ntohs(((struct sockaddr_in *)sa)->sin_port);
2473 v4chk:
2474 if (IN_MULTICAST(in.s_addr))
2475 goto bad;
2476 switch ((in.s_addr & 0xff000000) >> 24) {
2477 case 0: case 127: case 255:
2478 goto bad;
2479 }
2480 /* XXX check for subnet broadcast using getifaddrs(3) */
2481 break;
2482 #ifdef INET6
2483 case AF_INET6:
2484 in6 = &((struct sockaddr_in6 *)sa)->sin6_addr;
2485 port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
2486 if (IN6_IS_ADDR_MULTICAST(in6) || IN6_IS_ADDR_UNSPECIFIED(in6))
2487 goto bad;
2488 if (IN6_IS_ADDR_V4MAPPED(in6) || IN6_IS_ADDR_V4COMPAT(in6)) {
2489 memcpy(&in, &in6->s6_addr[12], sizeof(in));
2490 in.s_addr = ntohl(in.s_addr);
2491 goto v4chk;
2492 }
2493 break;
2494 #endif
2495 default:
2496 /* XXX unsupported af, is it safe to assume it to be safe? */
2497 return 1;
2498 }
2499
2500 for (i = 0; bad_ports[i] != 0; i++) {
2501 if (port == bad_ports[i])
2502 goto bad;
2503 }
2504
2505 return (1);
2506
2507 bad:
2508 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
2509 niflags) != 0)
2510 strcpy(hbuf, "?");
2511 syslog(LOG_WARNING,"Possible DoS attack from %s, Port %d",
2512 hbuf, port);
2513 return (0);
2514 }
2515