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