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