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