blob: 4e3c3431e8ff6c7542c0ff1f9a54b4ade6cb2a9a [file] [log] [blame]
Glenn L McGrath06e95652003-02-09 06:51:14 +00001/*
2 * Copyright (c) 1983,1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * David A. Holland.
7 *
8 * Busybox port by Vladimir Oleynik (C) 2001-2003 <dzo@simtreas.ru>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26/*
27 * Inetd - Internet super-server
28 *
29 * This program invokes all internet services as needed.
30 * connection-oriented services are invoked each time a
31 * connection is made, by creating a process. This process
32 * is passed the connection as file descriptor 0 and is
33 * expected to do a getpeername to find out the source host
34 * and port.
35 *
36 * Datagram oriented services are invoked when a datagram
37 * arrives; a process is created and passed a pending message
38 * on file descriptor 0. Datagram servers may either connect
39 * to their peer, freeing up the original socket for inetd
40 * to receive further messages on, or ``take over the socket'',
41 * processing all arriving datagrams and, eventually, timing
42 * out. The first type of server is said to be ``multi-threaded'';
43 * the second type of server ``single-threaded''.
44 *
45 * Inetd uses a configuration file which is read at startup
46 * and, possibly, at some later time in response to a hangup signal.
47 * The configuration file is ``free format'' with fields given in the
48 * order shown below. Continuation lines for an entry must being with
49 * a space or tab. All fields must be present in each entry.
50 *
51 * service name must be in /etc/services
52 * socket type stream/dgram/raw/rdm/seqpacket
53 * protocol must be in /etc/protocols
54 * wait/nowait[.max] single-threaded/multi-threaded, max #
55 * user[.group] user/group to run daemon as
56 * server program full path name
57 * server program arguments maximum of MAXARGS (20)
58 *
59 * RPC services unsuported
60 *
61 * Comment lines are indicated by a `#' in column 1.
62 */
63
64/*
65 * Here's the scoop concerning the user.group feature:
66 *
67 * 1) No group listed.
68 *
69 * a) for root: NO setuid() or setgid() is done
70 *
71 * b) nonroot: setuid()
72 * setgid(primary group as found in passwd)
73 * initgroups(name, primary group)
74 *
75 * 2) set-group-option on.
76 *
77 * a) for root: NO setuid()
78 * setgid(specified group)
79 * setgroups(1, specified group)
80 *
81 * b) nonroot: setuid()
82 * setgid(specified group)
83 * initgroups(name, specified group)
84 *
85 * All supplementary groups are discarded at startup in case inetd was
86 * run manually.
87 */
88
89#define __USE_BSD_SIGNAL
90
91#include "busybox.h"
92
93#include <sys/param.h>
94#include <sys/stat.h>
95#include <sys/ioctl.h>
96#include <sys/socket.h>
97#include <sys/un.h>
98#include <sys/file.h>
99#include <sys/wait.h>
100#include <sys/time.h>
101#include <sys/resource.h>
102
103#ifndef __linux__
104#ifndef RLIMIT_NOFILE
105#define RLIMIT_NOFILE RLIMIT_OFILE
106#endif
107#endif
108
109#include <sys/param.h>
110#include <sys/stat.h>
111#include <sys/ioctl.h>
112#include <sys/socket.h>
113#include <sys/file.h>
114#include <sys/wait.h>
115#include <sys/time.h>
116#include <sys/resource.h>
117
118#include <netinet/in.h>
119#include <netinet/ip.h>
120#include <arpa/inet.h>
121
122#include <errno.h>
123#include <signal.h>
124#include <netdb.h>
125#include <syslog.h>
Glenn L McGrath06e95652003-02-09 06:51:14 +0000126#include <stdio.h>
127#include <stdlib.h>
128#include <string.h>
129#include <getopt.h>
130#include <unistd.h>
131#include <stdarg.h>
Manuel Novoa III c2843562003-02-11 07:06:06 +0000132#include <time.h>
Glenn L McGrath06e95652003-02-09 06:51:14 +0000133
134#define _PATH_INETDCONF "/etc/inetd.conf"
135#define _PATH_INETDPID "/var/run/inetd.pid"
136
137#ifndef MIN
138#define MIN(a, b) ((a) < (b) ? (a) : (b))
139#endif
140
141#define TOOMANY 40 /* don't start more than TOOMANY */
142#define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
143#define RETRYTIME (60*10) /* retry after bind or server fail */
144
145#ifndef OPEN_MAX
146#define OPEN_MAX 64
147#endif
148
149
150/* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
151#define FD_MARGIN (8)
152static int rlim_ofile_cur = OPEN_MAX;
153
154#ifdef RLIMIT_NOFILE
155static struct rlimit rlim_ofile;
156#endif
157
Glenn L McGrathb1207b32003-02-10 22:31:09 +0000158#define INETD_UNSUPPORT_BILTIN 1
159
160/* Check unsupporting builtin */
161#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
162#undef INETD_UNSUPPORT_BILTIN
163#endif
164#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
165#undef INETD_UNSUPPORT_BILTIN
166#endif
167#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
168#undef INETD_UNSUPPORT_BILTIN
169#endif
170#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
171#undef INETD_UNSUPPORT_BILTIN
172#endif
173#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
174#undef INETD_UNSUPPORT_BILTIN
175#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000176
177static struct servtab {
178 char *se_service; /* name of service */
179 int se_socktype; /* type of socket to use */
180 int se_family; /* address family */
181 char *se_proto; /* protocol used */
182 short se_wait; /* single threaded server */
183 short se_checked; /* looked at during merge */
184 char *se_user; /* user name to run as */
185 char *se_group; /* group name to run as */
186#ifndef INETD_UNSUPPORT_BILTIN
187 const struct biltin *se_bi; /* if built-in, description */
188#endif
189 char *se_server; /* server program */
190#define MAXARGV 20
191 char *se_argv[MAXARGV+1]; /* program arguments */
192 int se_fd; /* open descriptor */
193 union {
194 struct sockaddr se_un_ctrladdr;
195 struct sockaddr_in se_un_ctrladdr_in;
196 struct sockaddr_un se_un_ctrladdr_un;
197 } se_un; /* bound address */
198#define se_ctrladdr se_un.se_un_ctrladdr
199#define se_ctrladdr_in se_un.se_un_ctrladdr_in
200#define se_ctrladdr_un se_un.se_un_ctrladdr_un
201 int se_ctrladdr_size;
202 int se_max; /* max # of instances of this service */
203 int se_count; /* number started since se_time */
204 struct timeval se_time; /* start of se_count */
205 struct servtab *se_next;
206} *servtab;
207
208/* Length of socket listen queue. Should be per-service probably. */
209static int global_queuelen = 128;
210
211static int nsock, maxsock;
212static fd_set allsock;
213static int timingout;
214static sigset_t blockmask, emptymask;
215
216
Glenn L McGrath06e95652003-02-09 06:51:14 +0000217 /* Echo received data */
218#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
Glenn L McGrath06e95652003-02-09 06:51:14 +0000219static void echo_stream(int, struct servtab *);
220static void echo_dg(int, struct servtab *);
221#endif
222 /* Internet /dev/null */
223#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
Glenn L McGrath06e95652003-02-09 06:51:14 +0000224static void discard_stream(int, struct servtab *);
225static void discard_dg(int, struct servtab *);
226#endif
227 /* Return 32 bit time since 1900 */
228#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
Glenn L McGrath06e95652003-02-09 06:51:14 +0000229static void machtime_stream(int, struct servtab *);
230static void machtime_dg(int, struct servtab *);
231#endif
232 /* Return human-readable time */
233#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
Glenn L McGrath06e95652003-02-09 06:51:14 +0000234static void daytime_stream(int, struct servtab *);
235static void daytime_dg(int, struct servtab *);
236#endif
237 /* Familiar character generator */
238#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
Glenn L McGrath06e95652003-02-09 06:51:14 +0000239static void chargen_stream(int, struct servtab *);
240static void chargen_dg(int, struct servtab *);
241#endif
242
243#ifndef INETD_UNSUPPORT_BILTIN
244struct biltin {
245 const char *bi_service; /* internally provided service name */
246 int bi_socktype; /* type of socket supported */
247 short bi_fork; /* 1 if should fork before call */
248 short bi_wait; /* 1 if should wait for child */
249 void (*bi_fn)(int, struct servtab *); /* fn which performs it */
250};
251
252static const struct biltin biltins[] = {
253#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
254 /* Echo received data */
255 { "echo", SOCK_STREAM, 1, 0, echo_stream, },
256 { "echo", SOCK_DGRAM, 0, 0, echo_dg, },
257#endif
258#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
259 /* Internet /dev/null */
260 { "discard", SOCK_STREAM, 1, 0, discard_stream, },
261 { "discard", SOCK_DGRAM, 0, 0, discard_dg, },
262#endif
263#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
264 /* Return 32 bit time since 1900 */
265 { "time", SOCK_STREAM, 0, 0, machtime_stream, },
266 { "time", SOCK_DGRAM, 0, 0, machtime_dg, },
267#endif
268#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
269 /* Return human-readable time */
270 { "daytime", SOCK_STREAM, 0, 0, daytime_stream, },
271 { "daytime", SOCK_DGRAM, 0, 0, daytime_dg, },
272#endif
273#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
274 /* Familiar character generator */
275 { "chargen", SOCK_STREAM, 1, 0, chargen_stream, },
276 { "chargen", SOCK_DGRAM, 0, 0, chargen_dg, },
277#endif
278 { NULL, 0, 0, 0, NULL }
279};
280#endif /* INETD_UNSUPPORT_BILTIN */
281
282#define NUMINT (sizeof(intab) / sizeof(struct inent))
283static const char *CONFIG = _PATH_INETDCONF;
284
285#define BCOPY(s, d, z) memcpy(d, s, z)
286
287static void
288syslog_err_and_discard_dg(int se_socktype, const char *msg, ...)
289 __attribute__ ((noreturn, format (printf, 2, 3)));
290
291static void
292syslog_err_and_discard_dg(int se_socktype, const char *msg, ...)
293{
294 char buf[50];
295 va_list p;
296
297 va_start(p, msg);
298 vsyslog(LOG_ERR, msg, p);
299 if (se_socktype != SOCK_STREAM)
300 recv(0, buf, sizeof (buf), 0);
301 _exit(1);
302}
303
304static FILE *fconfig;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000305
306static FILE *
307setconfig(void)
308{
309 FILE *f = fconfig;
310
311 if (f != NULL) {
312 fseek(f, 0L, L_SET);
313 } else {
314 f = fconfig = fopen(CONFIG, "r");
315 if(f == NULL)
316 syslog(LOG_ERR, "%s: %m", CONFIG);
317 }
318 return f;
319}
320
321static char *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000322skip(char **cpp)
323{
324 char *cp = *cpp;
325 char *start;
326
327 if (*cpp == NULL)
328 return ((char *)0);
329
330again:
331 while (*cp == ' ' || *cp == '\t')
332 cp++;
333 if (*cp == '\0') {
334 int c;
335
336 c = getc(fconfig);
337 (void) ungetc(c, fconfig);
338 if (c == ' ' || c == '\t')
Glenn L McGrath2faf3062004-01-17 00:34:31 +0000339 cp = bb_get_chomped_line_from_file(fconfig);
340 if (cp != NULL)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000341 goto again;
342 *cpp = NULL;
343 return NULL;
344 }
345 start = cp;
346 while (*cp && *cp != ' ' && *cp != '\t')
347 cp++;
348 if (*cp != '\0')
349 *cp++ = '\0';
350 *cpp = cp;
351 return (start);
352}
353
354static char *
355newstr(char *cp)
356{
357 cp = strdup(cp ? cp : "");
358 if (cp)
359 return(cp);
360
361 syslog_err_and_discard_dg(SOCK_STREAM, "strdup: %m");
362}
363
364
365static struct servtab *
366getconfigent(void)
367{
368 static struct servtab serv;
369 struct servtab *sep = &serv;
370 int argc;
371 char *cp, *arg;
372
373more:
Glenn L McGrath2faf3062004-01-17 00:34:31 +0000374 while ((cp = bb_get_chomped_line_from_file(fconfig)) && *cp == '#');
Glenn L McGrath06e95652003-02-09 06:51:14 +0000375 if (cp == NULL)
376 return ((struct servtab *)0);
377 memset((char *)sep, 0, sizeof *sep);
378 sep->se_service = newstr(skip(&cp));
379 arg = skip(&cp);
380 if (arg == NULL)
381 goto more;
382
383 if (strcmp(arg, "stream") == 0)
384 sep->se_socktype = SOCK_STREAM;
385 else if (strcmp(arg, "dgram") == 0)
386 sep->se_socktype = SOCK_DGRAM;
387 else if (strcmp(arg, "rdm") == 0)
388 sep->se_socktype = SOCK_RDM;
389 else if (strcmp(arg, "seqpacket") == 0)
390 sep->se_socktype = SOCK_SEQPACKET;
391 else if (strcmp(arg, "raw") == 0)
392 sep->se_socktype = SOCK_RAW;
393 else
394 sep->se_socktype = -1;
395
396 sep->se_proto = newstr(skip(&cp));
397 if (strcmp(sep->se_proto, "unix") == 0) {
398 sep->se_family = AF_UNIX;
399 } else {
400 sep->se_family = AF_INET;
401 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
402 syslog(LOG_ERR, "%s: rpc services not suported",
403 sep->se_service);
404 goto more;
405 }
406 }
407 arg = skip(&cp);
408 if (arg == NULL)
409 goto more;
410 {
411 char *s = strchr(arg, '.');
412 if (s) {
413 *s++ = '\0';
414 sep->se_max = atoi(s);
415 } else
416 sep->se_max = TOOMANY;
417 }
418 sep->se_wait = strcmp(arg, "wait") == 0;
419 sep->se_user = newstr(skip(&cp));
420 sep->se_group = strchr(sep->se_user, '.');
421 if (sep->se_group) {
422 *sep->se_group++ = '\0';
423 }
424 sep->se_server = newstr(skip(&cp));
425 if (strcmp(sep->se_server, "internal") == 0) {
426#ifndef INETD_UNSUPPORT_BILTIN
427 const struct biltin *bi;
428
429 for (bi = biltins; bi->bi_service; bi++)
430 if (bi->bi_socktype == sep->se_socktype &&
431 strcmp(bi->bi_service, sep->se_service) == 0)
432 break;
433 if (bi->bi_service == 0) {
434 syslog(LOG_ERR, "internal service %s unknown",
435 sep->se_service);
436 goto more;
437 }
438 sep->se_bi = bi;
439 sep->se_wait = bi->bi_wait;
440#else
441 syslog(LOG_ERR, "internal service %s unknown",
442 sep->se_service);
443 goto more;
444#endif
445 } else
446#ifndef INETD_UNSUPPORT_BILTIN
447 sep->se_bi = NULL
448#endif
449 ;
450 argc = 0;
451 for (arg = skip(&cp); cp; arg = skip(&cp)) {
452 if (argc < MAXARGV)
453 sep->se_argv[argc++] = newstr(arg);
454 }
455 while (argc <= MAXARGV)
456 sep->se_argv[argc++] = NULL;
457 return (sep);
458}
459
460static void
461freeconfig(struct servtab *cp)
462{
463 int i;
464
465 free(cp->se_service);
466 free(cp->se_proto);
467 free(cp->se_user);
468 /* Note: se_group is part of the newstr'ed se_user */
469 free(cp->se_server);
470 for (i = 0; i < MAXARGV; i++)
471 free(cp->se_argv[i]);
472}
473
474#ifndef INETD_UNSUPPORT_BILTIN
475static char **Argv;
476static char *LastArg;
477
478static void
479setproctitle(char *a, int s)
480{
481 size_t size;
482 char *cp;
483 struct sockaddr_in sn;
484 char buf[80];
485
486 cp = Argv[0];
487 size = sizeof(sn);
488 if (getpeername(s, (struct sockaddr *)&sn, &size) == 0)
489 (void) sprintf(buf, "-%s [%s]", a, inet_ntoa(sn.sin_addr));
490 else
491 (void) sprintf(buf, "-%s", a);
492 strncpy(cp, buf, LastArg - cp);
493 cp += strlen(cp);
494 while (cp < LastArg)
495 *cp++ = ' ';
496}
497#endif /* INETD_UNSUPPORT_BILTIN */
498
499static struct servtab *
500enter(struct servtab *cp)
501{
502 struct servtab *sep;
503 sigset_t oldmask;
504
505 sep = (struct servtab *)malloc(sizeof (*sep));
506 if (sep == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000507 syslog_err_and_discard_dg(SOCK_STREAM, bb_msg_memory_exhausted);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000508 }
509 *sep = *cp;
510 sep->se_fd = -1;
511 sigprocmask(SIG_BLOCK, &blockmask, &oldmask);
512 sep->se_next = servtab;
513 servtab = sep;
514 sigprocmask(SIG_SETMASK, &oldmask, NULL);
515 return (sep);
516}
517
518static int
519bump_nofile(void)
520{
521#ifdef RLIMIT_NOFILE
522
523#define FD_CHUNK 32
524
525 struct rlimit rl;
526
527 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
528 syslog(LOG_ERR, "getrlimit: %m");
529 return -1;
530 }
531 rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK);
532 if (rl.rlim_cur <= rlim_ofile_cur) {
533 syslog(LOG_ERR,
Glenn L McGrathb1207b32003-02-10 22:31:09 +0000534#if _FILE_OFFSET_BITS == 64
535 "bump_nofile: cannot extend file limit, max = %lld",
536#else
537 "bump_nofile: cannot extend file limit, max = %ld",
538#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000539 rl.rlim_cur);
540 return -1;
541 }
542
543 if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
544 syslog(LOG_ERR, "setrlimit: %m");
545 return -1;
546 }
547
548 rlim_ofile_cur = rl.rlim_cur;
549 return 0;
550
551#else
552 syslog(LOG_ERR, "bump_nofile: cannot extend file limit");
553 return -1;
554#endif
555}
556
557
558static void
559setup(struct servtab *sep)
560{
561 int on = 1;
562
563 if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
564 syslog(LOG_ERR, "%s/%s: socket: %m",
565 sep->se_service, sep->se_proto);
566 return;
567 }
568 if (setsockopt(sep->se_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
569 sizeof(on)) < 0)
570 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
571 if (bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size) < 0) {
572 syslog(LOG_ERR, "%s/%s: bind: %m",
573 sep->se_service, sep->se_proto);
574 (void) close(sep->se_fd);
575 sep->se_fd = -1;
576 if (!timingout) {
577 timingout = 1;
578 alarm(RETRYTIME);
579 }
580 return;
581 }
582 if (sep->se_socktype == SOCK_STREAM)
583 listen(sep->se_fd, global_queuelen);
584
585 FD_SET(sep->se_fd, &allsock);
586 nsock++;
587 if (sep->se_fd > maxsock) {
588 maxsock = sep->se_fd;
589 if (maxsock > rlim_ofile_cur - FD_MARGIN)
590 bump_nofile();
591 }
592}
593
594static void
595config(int signum)
596{
597 struct servtab *sep, *cp, **sepp;
598 sigset_t oldmask;
599 unsigned n;
600
601 (void)signum;
602 if (setconfig() == NULL)
603 return;
604
605 for (sep = servtab; sep; sep = sep->se_next)
606 sep->se_checked = 0;
607 while ((cp = getconfigent()) != NULL) {
608 for (sep = servtab; sep; sep = sep->se_next)
609 if (strcmp(sep->se_service, cp->se_service) == 0 &&
610 strcmp(sep->se_proto, cp->se_proto) == 0)
611 break;
612 if (sep != 0) {
613 int i;
614
615#define SWAP(type, a, b) {type c=(type)a; (type)a=(type)b; (type)b=(type)c;}
616
617 sigprocmask(SIG_BLOCK, &emptymask, &oldmask);
618 /*
619 * sep->se_wait may be holding the pid of a daemon
620 * that we're waiting for. If so, don't overwrite
621 * it unless the config file explicitly says don't
622 * wait.
623 */
624 if (
625#ifndef INETD_UNSUPPORT_BILTIN
626 cp->se_bi == 0 &&
627#endif
628 (sep->se_wait == 1 || cp->se_wait == 0))
629 sep->se_wait = cp->se_wait;
630 if (cp->se_max != sep->se_max)
631 SWAP(int, cp->se_max, sep->se_max);
632 if (cp->se_user)
633 SWAP(char *, sep->se_user, cp->se_user);
634 if (cp->se_group)
635 SWAP(char *, sep->se_group, cp->se_group);
636 if (cp->se_server)
637 SWAP(char *, sep->se_server, cp->se_server);
638 for (i = 0; i < MAXARGV; i++)
639 SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
640#undef SWAP
641 sigprocmask(SIG_SETMASK, &oldmask, NULL);
642 freeconfig(cp);
643 } else {
644 sep = enter(cp);
645 }
646 sep->se_checked = 1;
647
648 switch (sep->se_family) {
649 case AF_UNIX:
650 if (sep->se_fd != -1)
651 break;
652 (void)unlink(sep->se_service);
653 n = strlen(sep->se_service);
654 if (n > sizeof(sep->se_ctrladdr_un.sun_path) - 1)
655 n = sizeof(sep->se_ctrladdr_un.sun_path) - 1;
656 strncpy(sep->se_ctrladdr_un.sun_path, sep->se_service, n);
657 sep->se_ctrladdr_un.sun_family = AF_UNIX;
658 sep->se_ctrladdr_size = n +
659 sizeof sep->se_ctrladdr_un.sun_family;
660 setup(sep);
661 break;
662 case AF_INET:
663 sep->se_ctrladdr_in.sin_family = AF_INET;
664 sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in;
665 {
666 u_short port = htons(atoi(sep->se_service));
667
668 if (!port) {
669 struct servent *sp;
670 sp = getservbyname(sep->se_service,
671 sep->se_proto);
672 if (sp == 0) {
673 syslog(LOG_ERR,
674 "%s/%s: unknown service",
675 sep->se_service, sep->se_proto);
676 continue;
677 }
678 port = sp->s_port;
679 }
680 if (port != sep->se_ctrladdr_in.sin_port) {
681 sep->se_ctrladdr_in.sin_port = port;
682 if (sep->se_fd != -1) {
683 FD_CLR(sep->se_fd, &allsock);
684 nsock--;
685 (void) close(sep->se_fd);
686 }
687 sep->se_fd = -1;
688 }
689 if (sep->se_fd == -1)
690 setup(sep);
691 }
692 }
693 }
694 if (fconfig) {
695 (void) fclose(fconfig);
696 fconfig = NULL;
697 }
698 /*
699 * Purge anything not looked at above.
700 */
701 sigprocmask(SIG_SETMASK, &blockmask, &oldmask);
702 sepp = &servtab;
703 while ((sep = *sepp) != NULL) {
704 if (sep->se_checked) {
705 sepp = &sep->se_next;
706 continue;
707 }
708 *sepp = sep->se_next;
709 if (sep->se_fd != -1) {
710 FD_CLR(sep->se_fd, &allsock);
711 nsock--;
712 (void) close(sep->se_fd);
713 }
714 if (sep->se_family == AF_UNIX)
715 (void)unlink(sep->se_service);
716 freeconfig(sep);
717 free((char *)sep);
718 }
719 sigprocmask(SIG_SETMASK, &oldmask, NULL);
720}
721
722
723
724static void
725reapchild(int signum)
726{
727 int status;
728 int pid;
729 struct servtab *sep;
730
731 (void)signum;
732 for (;;) {
733 pid = wait3(&status, WNOHANG, (struct rusage *)0);
734 if (pid <= 0)
735 break;
736 for (sep = servtab; sep; sep = sep->se_next)
737 if (sep->se_wait == pid) {
738 if (WIFEXITED(status) && WEXITSTATUS(status))
739 syslog(LOG_WARNING,
740 "%s: exit status 0x%x",
741 sep->se_server, WEXITSTATUS(status));
742 else if (WIFSIGNALED(status))
743 syslog(LOG_WARNING,
744 "%s: exit signal 0x%x",
745 sep->se_server, WTERMSIG(status));
746 sep->se_wait = 1;
747 FD_SET(sep->se_fd, &allsock);
748 nsock++;
749 }
750 }
751}
752
753static void
754retry(int signum)
755{
756 struct servtab *sep;
757
758 (void)signum;
759 timingout = 0;
760 for (sep = servtab; sep; sep = sep->se_next) {
761 if (sep->se_fd == -1) {
762 switch (sep->se_family) {
763 case AF_UNIX:
764 case AF_INET:
765 setup(sep);
766 break;
767 }
768 }
769 }
770}
771
772static void
773goaway(int signum)
774{
775 struct servtab *sep;
776
777 (void)signum;
778 for (sep = servtab; sep; sep = sep->se_next)
779 if (sep->se_fd != -1 && sep->se_family == AF_UNIX)
780 (void)unlink(sep->se_service);
781 (void)unlink(_PATH_INETDPID);
782 exit(0);
783}
784
785
786
787extern int
788inetd_main(int argc, char *argv[])
789{
790 struct servtab *sep;
791 struct passwd *pwd;
792 struct group *grp = NULL;
793 struct sigaction sa;
Eric Andersen35e643b2003-07-28 07:40:39 +0000794 int pid;
795 unsigned long opt;
796 char *sq;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000797 gid_t gid;
798
799#ifdef INETD_UNSUPPORT_BILTIN
800# define dofork 1
801#else
802 int dofork;
803 extern char **environ;
804#endif
805
806 gid = getgid();
807 setgroups(1, &gid);
808
809#ifndef INETD_UNSUPPORT_BILTIN
810 Argv = argv;
811 if (environ == 0 || *environ == 0)
812 environ = argv;
813 while (*environ)
814 environ++;
815 LastArg = environ[-1] + strlen(environ[-1]);
816#endif
817
Eric Andersen35e643b2003-07-28 07:40:39 +0000818#if defined(__uClinux__)
819 opt = bb_getopt_ulflags(argc, argv, "q:f", &sq);
Eric Andersen68d4a852003-07-28 09:31:28 +0000820 if (!(opt & 2)) {
Eric Andersen35e643b2003-07-28 07:40:39 +0000821 daemon(0, 0);
822 /* reexec for vfork() do continue parent */
823 vfork_daemon_rexec(argc, argv, "-f");
824 }
825#else
Eric Andersen68d4a852003-07-28 09:31:28 +0000826 opt = bb_getopt_ulflags(argc, argv, "q:", &sq);
Eric Andersen35e643b2003-07-28 07:40:39 +0000827 daemon(0, 0);
828#endif /* uClinux */
829
830 if(opt & 1) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000831 global_queuelen = atoi(optarg);
832 if (global_queuelen < 8) global_queuelen=8;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000833 }
834 argc -= optind;
835 argv += optind;
836
837 if (argc > 0)
838 CONFIG = argv[0];
839
Manuel Novoa III cad53642003-03-19 09:13:01 +0000840 openlog(bb_applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000841 {
842 FILE *fp;
843
844 if ((fp = fopen(_PATH_INETDPID, "w")) != NULL) {
845 fprintf(fp, "%u\n", getpid());
846 (void)fclose(fp);
847 }
848 }
849
850#ifdef RLIMIT_NOFILE
851 if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
852 syslog(LOG_ERR, "getrlimit: %m");
853 } else {
854 rlim_ofile_cur = rlim_ofile.rlim_cur;
855 if (rlim_ofile_cur == RLIM_INFINITY) /* ! */
856 rlim_ofile_cur = OPEN_MAX;
857 }
858#endif
859
860 config(0);
861
862 sigemptyset(&emptymask);
863 sigemptyset(&blockmask);
864 sigaddset(&blockmask, SIGCHLD);
865 sigaddset(&blockmask, SIGHUP);
866 sigaddset(&blockmask, SIGALRM);
867
868 memset(&sa, 0, sizeof(sa));
869 sa.sa_mask = blockmask;
870 sa.sa_handler = retry;
871 sigaction(SIGALRM, &sa, NULL);
872 sa.sa_handler = config;
873 sigaction(SIGHUP, &sa, NULL);
874 sa.sa_handler = reapchild;
875 sigaction(SIGCHLD, &sa, NULL);
876 sa.sa_handler = goaway;
877 sigaction(SIGTERM, &sa, NULL);
878 sa.sa_handler = goaway;
879 sigaction(SIGINT, &sa, NULL);
880 sa.sa_handler = SIG_IGN;
881 sigaction(SIGPIPE, &sa, NULL);
882
883 {
884 /* space for daemons to overwrite environment for ps */
885#define DUMMYSIZE 100
886 char dummy[DUMMYSIZE];
887
888 (void)memset(dummy, 'x', DUMMYSIZE - 1);
889 dummy[DUMMYSIZE - 1] = '\0';
890
891 (void)setenv("inetd_dummy", dummy, 1);
892 }
893
894 for (;;) {
895 int n, ctrl;
896 fd_set readable;
897
898 if (nsock == 0) {
899 sigprocmask(SIG_BLOCK, &blockmask, NULL);
900 while (nsock == 0)
901 sigsuspend(&emptymask);
902 sigprocmask(SIG_SETMASK, &emptymask, NULL);
903 }
904 readable = allsock;
905 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
906 (fd_set *)0, (struct timeval *)0)) <= 0) {
907 if (n < 0 && errno != EINTR)
908 syslog(LOG_WARNING, "select: %m");
909 sleep(1);
910 continue;
911 }
912 for (sep = servtab; n && sep; sep = sep->se_next)
913 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
914 n--;
915 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
916 /* Fixed AGC */
917 fcntl(sep->se_fd, F_SETFL, O_NDELAY);
918 /* --------- */
919 ctrl = accept(sep->se_fd, NULL, NULL);
920 fcntl(sep->se_fd, F_SETFL, 0);
921 if (ctrl < 0) {
922 if (errno == EINTR || errno == EWOULDBLOCK)
923 continue;
924 syslog(LOG_WARNING, "accept (for %s): %m",
925 sep->se_service);
926 continue;
927 }
928 } else
929 ctrl = sep->se_fd;
930 sigprocmask(SIG_BLOCK, &blockmask, NULL);
931 pid = 0;
932#ifndef INETD_UNSUPPORT_BILTIN
933 dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork);
934#endif
935 if (dofork) {
936 if (sep->se_count++ == 0)
937 (void)gettimeofday(&sep->se_time,
938 (struct timezone *)0);
939 else if (sep->se_count >= sep->se_max) {
940 struct timeval now;
941
942 (void)gettimeofday(&now, (struct timezone *)0);
943 if (now.tv_sec - sep->se_time.tv_sec >
944 CNT_INTVL) {
945 sep->se_time = now;
946 sep->se_count = 1;
947 } else {
948 syslog(LOG_ERR,
949 "%s/%s server failing (looping), service terminated",
950 sep->se_service, sep->se_proto);
951 FD_CLR(sep->se_fd, &allsock);
952 (void) close(sep->se_fd);
953 sep->se_fd = -1;
954 sep->se_count = 0;
955 nsock--;
956 sigprocmask(SIG_SETMASK, &emptymask,
957 NULL);
958 if (!timingout) {
959 timingout = 1;
960 alarm(RETRYTIME);
961 }
962 continue;
963 }
964 }
965 pid = fork();
966 }
967 if (pid < 0) {
968 syslog(LOG_ERR, "fork: %m");
969 if (sep->se_socktype == SOCK_STREAM)
970 close(ctrl);
971 sigprocmask(SIG_SETMASK, &emptymask, NULL);
972 sleep(1);
973 continue;
974 }
975 if (pid && sep->se_wait) {
976 sep->se_wait = pid;
977 FD_CLR(sep->se_fd, &allsock);
978 nsock--;
979 }
980 sigprocmask(SIG_SETMASK, &emptymask, NULL);
981 if (pid == 0) {
982#ifndef INETD_UNSUPPORT_BILTIN
983 if (sep->se_bi)
984 (*sep->se_bi->bi_fn)(ctrl, sep);
985 else
986#endif
987 {
988 if ((pwd = getpwnam(sep->se_user)) == NULL) {
989 syslog_err_and_discard_dg(
990 sep->se_socktype,
991 "getpwnam: %s: No such user",
992 sep->se_user);
993 }
994 if (sep->se_group &&
995 (grp = getgrnam(sep->se_group)) == NULL) {
996 syslog_err_and_discard_dg(
997 sep->se_socktype,
998 "getgrnam: %s: No such group",
999 sep->se_group);
1000 }
1001 /*
1002 * Ok. There are four cases here:
1003 * 1. nonroot user, no group specified
1004 * 2. nonroot user, some group specified
1005 * 3. root user, no group specified
1006 * 4. root user, some group specified
1007 * In cases 2 and 4 we setgid to the specified
1008 * group. In cases 1 and 2 we run initgroups
1009 * to run with the groups of the given user.
1010 * In case 4 we do setgroups to run with the
1011 * given group. In case 3 we do nothing.
1012 */
1013 if (pwd->pw_uid) {
1014 if (sep->se_group)
1015 pwd->pw_gid = grp->gr_gid;
1016 setgid((gid_t)pwd->pw_gid);
1017 initgroups(pwd->pw_name, pwd->pw_gid);
1018 setuid((uid_t)pwd->pw_uid);
1019 } else if (sep->se_group) {
1020 setgid((gid_t)grp->gr_gid);
1021 setgroups(1, &grp->gr_gid);
1022 }
1023 dup2(ctrl, 0);
1024 close(ctrl);
1025 dup2(0, 1);
1026 dup2(0, 2);
1027#ifdef RLIMIT_NOFILE
1028 if (rlim_ofile.rlim_cur != rlim_ofile_cur) {
1029 if (setrlimit(RLIMIT_NOFILE,
1030 &rlim_ofile) < 0)
1031 syslog(LOG_ERR,"setrlimit: %m");
1032 }
1033#endif
1034 for (ctrl = rlim_ofile_cur-1; --ctrl > 2; )
1035 (void)close(ctrl);
1036
1037 memset(&sa, 0, sizeof(sa));
1038 sa.sa_handler = SIG_DFL;
1039 sigaction(SIGPIPE, &sa, NULL);
1040
1041 execv(sep->se_server, sep->se_argv);
1042 syslog_err_and_discard_dg(sep->se_socktype,
1043 "execv %s: %m", sep->se_server);
1044 }
1045 }
1046 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
1047 close(ctrl);
1048 }
1049 }
1050}
1051
1052
1053/*
1054 * Internet services provided internally by inetd:
1055 */
1056#define BUFSIZE 4096
1057
1058#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
1059/* Echo service -- echo data back */
1060static void
1061echo_stream(int s, struct servtab *sep)
1062{
1063 char buffer[BUFSIZE];
1064 int i;
1065
1066 setproctitle(sep->se_service, s);
1067 while ((i = read(s, buffer, sizeof(buffer))) > 0 &&
1068 write(s, buffer, i) > 0)
1069 ;
1070 exit(0);
1071}
1072
1073/* Echo service -- echo data back */
1074static void
1075echo_dg(int s, struct servtab *sep)
1076{
1077 char buffer[BUFSIZE];
1078 int i;
1079 size_t size;
1080 struct sockaddr sa;
1081
1082 (void)sep;
1083
1084 size = sizeof(sa);
1085 if ((i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size)) < 0)
1086 return;
1087 (void) sendto(s, buffer, i, 0, &sa, sizeof(sa));
1088}
1089#endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO */
1090
1091
1092#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
1093/* Discard service -- ignore data */
1094static void
1095discard_stream(int s, struct servtab *sep)
1096{
1097 char buffer[BUFSIZE];
1098
1099 setproctitle(sep->se_service, s);
1100 while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) ||
1101 errno == EINTR)
1102 ;
1103 exit(0);
1104}
1105
1106/* Discard service -- ignore data */
1107static void
1108discard_dg(int s, struct servtab *sep)
1109{
1110 char buffer[BUFSIZE];
1111 (void)sep;
1112 read(s, buffer, sizeof(buffer));
1113}
1114#endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD */
1115
1116
1117#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
1118#include <ctype.h>
1119#define LINESIZ 72
1120static char ring[128];
1121static char *endring;
1122
1123static void
1124initring(void)
1125{
1126 int i;
1127
1128 endring = ring;
1129
1130 for (i = 0; i <= 128; ++i)
1131 if (isprint(i))
1132 *endring++ = i;
1133}
1134
1135/* Character generator */
1136static void
1137chargen_stream(int s, struct servtab *sep)
1138{
1139 char *rs;
1140 int len;
1141 char text[LINESIZ+2];
1142
1143 setproctitle(sep->se_service, s);
1144
1145 if (!endring) {
1146 initring();
1147 rs = ring;
1148 }
1149
1150 text[LINESIZ] = '\r';
1151 text[LINESIZ + 1] = '\n';
1152 for (rs = ring;;) {
1153 if ((len = endring - rs) >= LINESIZ)
1154 BCOPY(rs, text, LINESIZ);
1155 else {
1156 BCOPY(rs, text, len);
1157 BCOPY(ring, text + len, LINESIZ - len);
1158 }
1159 if (++rs == endring)
1160 rs = ring;
1161 if (write(s, text, sizeof(text)) != sizeof(text))
1162 break;
1163 }
1164 exit(0);
1165}
1166
1167/* Character generator */
1168static void
1169chargen_dg(int s, struct servtab *sep)
1170{
1171 struct sockaddr sa;
1172 static char *rs;
1173 size_t len, size;
1174 char text[LINESIZ+2];
1175
1176 (void)sep;
1177
1178 if (endring == 0) {
1179 initring();
1180 rs = ring;
1181 }
1182
1183 size = sizeof(sa);
1184 if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0)
1185 return;
1186
1187 if ((len = endring - rs) >= LINESIZ)
1188 BCOPY(rs, text, LINESIZ);
1189 else {
1190 BCOPY(rs, text, len);
1191 BCOPY(ring, text + len, LINESIZ - len);
1192 }
1193 if (++rs == endring)
1194 rs = ring;
1195 text[LINESIZ] = '\r';
1196 text[LINESIZ + 1] = '\n';
1197 (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa));
1198}
1199#endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN */
1200
1201
1202#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
1203/*
1204 * Return a machine readable date and time, in the form of the
1205 * number of seconds since midnight, Jan 1, 1900. Since gettimeofday
1206 * returns the number of seconds since midnight, Jan 1, 1970,
1207 * we must add 2208988800 seconds to this figure to make up for
1208 * some seventy years Bell Labs was asleep.
1209 */
1210
1211static long
1212machtime(void)
1213{
1214 struct timeval tv;
1215
1216 if (gettimeofday(&tv, (struct timezone *)0) < 0) {
1217 fprintf(stderr, "Unable to get time of day\n");
1218 return (0L);
1219 }
1220 return (htonl((long)tv.tv_sec + 2208988800UL));
1221}
1222
1223static void
1224machtime_stream(int s, struct servtab *sep)
1225{
1226 long result;
1227 (void)sep;
1228
1229 result = machtime();
1230 write(s, (char *) &result, sizeof(result));
1231}
1232
1233static void
1234machtime_dg(int s, struct servtab *sep)
1235{
1236 long result;
1237 struct sockaddr sa;
1238 size_t size;
1239 (void)sep;
1240
1241 size = sizeof(sa);
1242 if (recvfrom(s, (char *)&result, sizeof(result), 0, &sa, &size) < 0)
1243 return;
1244 result = machtime();
1245 (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa));
1246}
1247#endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME */
1248
1249
1250#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
1251/* Return human-readable time of day */
1252static int
1253human_readable_time_sprintf(char *buffer)
1254{
1255 time_t clocc = time(NULL);
1256
1257 return sprintf(buffer, "%.24s\r\n", ctime(&clocc));
1258}
1259
1260static void
1261daytime_stream(int s, struct servtab *sep)
1262{
1263 char buffer[256];
1264 size_t st = human_readable_time_sprintf(buffer);
1265
1266 (void)sep;
1267
1268 write(s, buffer, st);
1269}
1270
1271/* Return human-readable time of day */
1272static void
1273daytime_dg(int s, struct servtab *sep)
1274{
1275 char buffer[256];
1276 struct sockaddr sa;
1277 size_t size;
1278
1279 (void)sep;
1280
1281 size = sizeof(sa);
1282 if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0)
1283 return;
1284 size = human_readable_time_sprintf(buffer);
1285 sendto(s, buffer, size, 0, &sa, sizeof(sa));
1286}
1287#endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME */