Mark Lord writes:
[oweals/busybox.git] / networking / inetd.c
1 /*
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>
126 #include <stdio.h>
127 #include <stdlib.h>
128 #include <string.h>
129 #include <getopt.h>
130 #include <unistd.h>
131 #include <stdarg.h>
132 #include <time.h>
133
134 #ifndef OPEN_MAX
135 #define OPEN_MAX        64
136 #endif
137
138 #define _PATH_INETDCONF "/etc/inetd.conf"
139 #define _PATH_INETDPID  "/var/run/inetd.pid"
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 #define MAXARGV         20
145
146 #define se_ctrladdr             se_un.se_un_ctrladdr
147 #define se_ctrladdr_in  se_un.se_un_ctrladdr_in
148 #define se_ctrladdr_un  se_un.se_un_ctrladdr_un
149
150 /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
151 #define FD_MARGIN       (8)
152
153 /* Check unsupporting builtin */
154 #if defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO || \
155         defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD || \
156         defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME || \
157         defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME || \
158         defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
159 # define INETD_FEATURE_ENABLED
160 #endif
161
162 static struct  servtab {
163         char    *se_service;            /* name of service */
164         int     se_socktype;            /* type of socket to use */
165         int     se_family;              /* address family */
166         char    *se_proto;              /* protocol used */
167         short   se_wait;                /* single threaded server */
168         short   se_checked;             /* looked at during merge */
169         char    *se_user;               /* user name to run as */
170         char    *se_group;              /* group name to run as */
171 #ifdef INETD_FEATURE_ENABLED
172         const struct  biltin *se_bi;    /* if built-in, description */
173 #endif
174         char    *se_server;             /* server program */
175         char    *se_argv[MAXARGV+1];    /* program arguments */
176         int     se_fd;                  /* open descriptor */
177         union {
178                 struct  sockaddr se_un_ctrladdr;
179                 struct  sockaddr_in se_un_ctrladdr_in;
180                 struct  sockaddr_un se_un_ctrladdr_un;
181         } se_un;                        /* bound address */
182         int     se_ctrladdr_size;
183         int     se_max;                 /* max # of instances of this service */
184         int     se_count;               /* number started since se_time */
185         struct  timeval se_time;        /* start of se_count */
186         struct  servtab *se_next;
187 } *servtab;
188
189 #ifdef INETD_FEATURE_ENABLED
190 struct biltin {
191         const char *bi_service;         /* internally provided service name */
192         int bi_socktype;                /* type of socket supported */
193         short bi_fork;          /* 1 if should fork before call */
194         short bi_wait;          /* 1 if should wait for child */
195         void (*bi_fn)(int, struct servtab *); /* fn which performs it */
196 };
197
198     /* Echo received data */
199 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
200 static void echo_stream(int, struct servtab *);
201 static void echo_dg(int, struct servtab *);
202 #endif
203     /* Internet /dev/null */
204 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
205 static void discard_stream(int, struct servtab *);
206 static void discard_dg(int, struct servtab *);
207 #endif
208         /* Return 32 bit time since 1900 */
209 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
210 static void machtime_stream(int, struct servtab *);
211 static void machtime_dg(int, struct servtab *);
212 #endif
213         /* Return human-readable time */
214 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
215 static void daytime_stream(int, struct servtab *);
216 static void daytime_dg(int, struct servtab *);
217 #endif
218         /* Familiar character generator */
219 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
220 static void chargen_stream(int, struct servtab *);
221 static void chargen_dg(int, struct servtab *);
222 #endif
223
224 static const struct biltin biltins[] = {
225 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
226         /* Echo received data */
227         { "echo",               SOCK_STREAM,    1, 0,   echo_stream, },
228         { "echo",               SOCK_DGRAM,     0, 0,   echo_dg, },
229 #endif
230 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
231         /* Internet /dev/null */
232         { "discard",    SOCK_STREAM,    1, 0,   discard_stream, },
233         { "discard",    SOCK_DGRAM,     0, 0,   discard_dg, },
234 #endif
235 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
236         /* Return 32 bit time since 1900 */
237         { "time",               SOCK_STREAM,    0, 0,   machtime_stream, },
238         { "time",               SOCK_DGRAM,     0, 0,   machtime_dg,     },
239 #endif
240 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
241         /* Return human-readable time */
242         { "daytime",    SOCK_STREAM,    0, 0,   daytime_stream, },
243         { "daytime",    SOCK_DGRAM,     0, 0,   daytime_dg,     },
244 #endif
245 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
246         /* Familiar character generator */
247         { "chargen",    SOCK_STREAM,    1, 0,   chargen_stream, },
248         { "chargen",    SOCK_DGRAM,     0, 0,   chargen_dg,     },
249 #endif
250         { NULL, 0, 0, 0, NULL }
251 };
252 #endif  /* INETD_FEATURE_ENABLED */
253
254 #ifdef RLIMIT_NOFILE
255 static struct rlimit   rlim_ofile;
256 #endif
257
258 /* Length of socket listen queue. Should be per-service probably. */
259 static int      global_queuelen = 128;
260
261 static FILE *fconfig;
262 static sigset_t blockmask;
263 static sigset_t emptymask;
264 static fd_set   allsock;
265 static int      nsock;
266 static int      maxsock;
267 static int      timingout;
268 static int      rlim_ofile_cur = OPEN_MAX;
269 static const char *CONFIG = _PATH_INETDCONF;
270
271 static void
272 syslog_err_and_discard_dg(int se_socktype, const char *msg, ...)
273         __attribute__ ((noreturn, format (printf, 2, 3)));
274
275 static void
276 syslog_err_and_discard_dg(int se_socktype, const char *msg, ...)
277 {
278         char buf[50];
279         va_list p;
280
281         va_start(p, msg);
282         vsyslog(LOG_ERR, msg, p);
283         if (se_socktype != SOCK_STREAM)
284                 recv(0, buf, sizeof (buf), 0);
285         _exit(1);
286 }
287
288 static struct servtab *getconfigent(void)
289 {
290         static struct servtab serv;
291         struct servtab *sep = &serv;
292         int argc;
293         char *cp = NULL;
294         char *cp_ptr;
295         char *cp_ptr_ptr = NULL;
296
297 more:
298         free(cp);
299         cp = bb_get_chomped_line_from_file(fconfig);
300         if (feof(fconfig)) {
301                 free(cp);
302                 return (NULL);
303         }
304         if ((cp == NULL) || (*cp == '#')) {
305                 goto more;
306         }
307         printf("line is %s\n", cp);
308
309         cp_ptr = strtok_r(cp, " \t", &cp_ptr_ptr);
310         if (cp_ptr == NULL) {
311                 printf("error\n");
312                 /* Error */
313                 goto more;
314         }
315         sep->se_service = bb_xstrdup(cp_ptr);
316
317         cp_ptr = strtok_r(NULL, " \t", &cp_ptr_ptr);
318         if (cp_ptr == NULL) {
319                 printf("error\n");
320                 /* Error */
321                 goto more;
322         }
323         if (strcmp(cp_ptr, "stream") == 0)
324                 sep->se_socktype = SOCK_STREAM;
325         else if (strcmp(cp_ptr, "dgram") == 0)
326                 sep->se_socktype = SOCK_DGRAM;
327         else if (strcmp(cp_ptr, "rdm") == 0)
328                 sep->se_socktype = SOCK_RDM;
329         else if (strcmp(cp_ptr, "seqpacket") == 0)
330                 sep->se_socktype = SOCK_SEQPACKET;
331         else if (strcmp(cp_ptr, "raw") == 0)
332                 sep->se_socktype = SOCK_RAW;
333         else
334                 sep->se_socktype = -1;
335
336         cp_ptr = strtok_r(NULL, " \t", &cp_ptr_ptr);
337         if (cp_ptr == NULL) {
338                 printf("error\n");
339                 /* error */
340                 goto more;
341         }
342         if (strcmp(cp_ptr, "unix") == 0) {
343                 sep->se_family = AF_UNIX;
344         } else {
345                 if (strncmp(cp_ptr, "rpc/", 4) == 0) {
346                         syslog(LOG_ERR, "%s: rpc services not suported",
347                             sep->se_service);
348                         goto more;
349                 }
350                 sep->se_family = AF_INET;
351         }
352         sep->se_proto = bb_xstrdup(cp_ptr);
353
354         cp_ptr = strtok_r(NULL, " \t", &cp_ptr_ptr);
355         if (cp_ptr == NULL) {
356                 printf("error\n");
357                 /* error */
358                 goto more;
359         }
360         {
361                 char *s = strchr(cp_ptr, '.');
362                 if (s) {
363                         *s++ = '\0';
364                         sep->se_max = atoi(s);
365                 } else
366                         sep->se_max = TOOMANY;
367         }
368         sep->se_wait = strcmp(cp_ptr, "wait") == 0;
369
370         cp_ptr = strtok_r(NULL, " \t", &cp_ptr_ptr);
371         if (cp_ptr == NULL) {
372                 printf("error\n");
373                 /* error */
374                 goto more;
375         }
376         {
377                 char *cp_ptr2 = strchr(cp_ptr, '.');
378                 if (cp_ptr2) {
379                         *cp_ptr2++ = '\0';
380                         sep->se_group = bb_xstrdup(cp_ptr2);
381                 }
382         }
383         sep->se_user = bb_xstrdup(cp_ptr);
384
385         cp_ptr = strtok_r(NULL, " \t", &cp_ptr_ptr);
386         if (cp_ptr == NULL) {
387                 printf("error\n");
388                 /* error */
389                 goto more;
390         }
391         if (strcmp(cp_ptr, "internal") == 0) {
392 #ifdef INETD_FEATURE_ENABLED
393                 const struct biltin *bi;
394
395                 for (bi = biltins; bi->bi_service; bi++) {
396                         if ((bi->bi_socktype == sep->se_socktype) &&
397                             (strcmp(bi->bi_service, sep->se_service) == 0)) {
398                                 break;
399                         }
400                 }
401                 if (bi->bi_service == 0) {
402                         syslog(LOG_ERR, "internal service %s unknown", sep->se_service);
403                         goto more;
404                 }
405                 sep->se_bi = bi;
406                 sep->se_wait = bi->bi_wait;
407 #else
408                 syslog(LOG_ERR, "internal service %s unknown", cp_ptr);
409                 goto more;
410 #endif
411         }
412 #ifdef INETD_FEATURE_ENABLED
413         else {
414                 sep->se_bi = NULL;
415         }
416 #endif
417         sep->se_server = bb_xstrdup(cp_ptr);
418
419         argc = 0;
420         while ((cp_ptr = strtok_r(NULL, " \t", &cp_ptr_ptr)) != NULL) {
421                 if (argc < MAXARGV) {
422                         sep->se_argv[argc++] = cp_ptr;
423                 }
424         }
425         while (argc <= MAXARGV) {
426                 sep->se_argv[argc++] = NULL;
427         }
428
429         //free(cp);     // BUG: cp is the argv[] container; we must not free it here!
430         return (sep);
431 }
432
433 static void freeconfig(struct servtab *cp)
434 {
435         int i;
436
437         free(cp->se_service);
438         free(cp->se_proto);
439         free(cp->se_user);
440         /* Note: se_group is part of the newstr'ed se_user */
441         free(cp->se_server);
442         for (i = 0; i < MAXARGV; i++)
443                 free(cp->se_argv[i]);
444 }
445
446 #ifdef INETD_FEATURE_ENABLED
447 static char **Argv;
448 static char *LastArg;
449
450 static void setproctitle(char *a, int s)
451 {
452         size_t size;
453         char *cp;
454         struct sockaddr_in sn;
455         char buf[80];
456
457         cp = Argv[0];
458         size = sizeof(sn);
459         if (getpeername(s, (struct sockaddr *)&sn, &size) == 0)
460                 (void) sprintf(buf, "-%s [%s]", a, inet_ntoa(sn.sin_addr));
461         else
462                 (void) sprintf(buf, "-%s", a);
463         strncpy(cp, buf, LastArg - cp);
464         cp += strlen(cp);
465         while (cp < LastArg)
466                 *cp++ = ' ';
467 }
468 #endif  /* INETD_FEATURE_ENABLED */
469
470
471 static void setup(struct servtab *sep)
472 {
473         int on = 1;
474
475         if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
476                 syslog(LOG_ERR, "%s/%s: socket: %m",
477                     sep->se_service, sep->se_proto);
478                 return;
479         }
480         if (setsockopt(sep->se_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
481                             sizeof(on)) < 0)
482                 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
483         if (bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size) < 0) {
484                 syslog(LOG_ERR, "%s/%s: bind: %m",
485                     sep->se_service, sep->se_proto);
486                 (void) close(sep->se_fd);
487                 sep->se_fd = -1;
488                 if (!timingout) {
489                         timingout = 1;
490                         alarm(RETRYTIME);
491                 }
492                 return;
493         }
494         if (sep->se_socktype == SOCK_STREAM)
495                 listen(sep->se_fd, global_queuelen);
496
497         FD_SET(sep->se_fd, &allsock);
498         nsock++;
499         if (sep->se_fd > maxsock) {
500                 maxsock = sep->se_fd;
501                 if (maxsock > rlim_ofile_cur - FD_MARGIN) {
502 #ifdef RLIMIT_NOFILE
503 # define FD_CHUNK        32
504                         struct rlimit rl;
505
506                         if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
507                                 syslog(LOG_ERR, "getrlimit: %m");
508                                 return;
509                         }
510                         rl.rlim_cur = rl.rlim_max < (rl.rlim_cur + FD_CHUNK) ? rl.rlim_max : (rl.rlim_cur + FD_CHUNK);
511                         if (rl.rlim_cur <= rlim_ofile_cur) {
512                                 syslog(LOG_ERR,
513 # if _FILE_OFFSET_BITS == 64
514                                         "bump_nofile: cannot extend file limit, max = %lld",
515 # else
516                                         "bump_nofile: cannot extend file limit, max = %ld",
517 # endif
518                                         rl.rlim_cur);
519                                 return;
520                         }
521
522                         if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
523                                 syslog(LOG_ERR, "setrlimit: %m");
524                                 return;
525                         }
526
527                         rlim_ofile_cur = rl.rlim_cur;
528                         return;
529 #else
530                         syslog(LOG_ERR, "bump_nofile: cannot extend file limit");
531                         return;
532 #endif  /* RLIMIT_NOFILE */
533                 }
534         }
535 }
536
537 static void config(int signum)
538 {
539         struct servtab *sep, *cp, **sepp;
540         sigset_t oldmask;
541         unsigned n;
542
543         (void)signum;
544
545         if (fconfig != NULL) {
546                 fseek(fconfig, 0L, L_SET);
547         } else {
548                 fconfig = fopen(CONFIG, "r");
549                 if (fconfig == NULL) {
550                         syslog(LOG_ERR, "%s: %m", CONFIG);
551                         return;
552                 }
553         }
554
555         for (sep = servtab; sep; sep = sep->se_next)
556                 sep->se_checked = 0;
557         while ((cp = getconfigent()) != NULL) {
558                 for (sep = servtab; sep; sep = sep->se_next)
559                         if (strcmp(sep->se_service, cp->se_service) == 0 &&
560                             strcmp(sep->se_proto, cp->se_proto) == 0)
561                                 break;
562                 if (sep != 0) {
563                         int i;
564
565 #define SWAP(type, a, b) {type c=(type)a; (type)a=(type)b; (type)b=(type)c;}
566
567                         sigprocmask(SIG_BLOCK, &emptymask, &oldmask);
568                         /*
569                          * sep->se_wait may be holding the pid of a daemon
570                          * that we're waiting for.  If so, don't overwrite
571                          * it unless the config file explicitly says don't
572                          * wait.
573                          */
574                         if (
575 #ifdef INETD_FEATURE_ENABLED
576                             cp->se_bi == 0 &&
577 #endif
578                             (sep->se_wait == 1 || cp->se_wait == 0))
579                                 sep->se_wait = cp->se_wait;
580                         if (cp->se_max != sep->se_max)
581                                 SWAP(int, cp->se_max, sep->se_max);
582                         if (cp->se_user)
583                                 SWAP(char *, sep->se_user, cp->se_user);
584                         if (cp->se_group)
585                                 SWAP(char *, sep->se_group, cp->se_group);
586                         if (cp->se_server)
587                                 SWAP(char *, sep->se_server, cp->se_server);
588                         for (i = 0; i < MAXARGV; i++)
589                                 SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
590 #undef SWAP
591                         sigprocmask(SIG_SETMASK, &oldmask, NULL);
592                         // This freeconfig() is probably a bug, since it will try and free()
593                         // each of the argv[] values, which are really just pointers
594                         // into the middle of a single line buffer for the config file.
595                         //freeconfig(cp);       // BUG?
596                 } else {
597                         sep = (struct servtab *)xmalloc(sizeof (*sep));
598                         *sep = *cp;
599                         sep->se_fd = -1;
600                         sigprocmask(SIG_BLOCK, &blockmask, &oldmask);
601                         sep->se_next = servtab;
602                         servtab = sep;
603                         sigprocmask(SIG_SETMASK, &oldmask, NULL);
604                 }
605                 sep->se_checked = 1;
606
607                 switch (sep->se_family) {
608                 case AF_UNIX:
609                         if (sep->se_fd != -1)
610                                 break;
611                         (void)unlink(sep->se_service);
612                         n = strlen(sep->se_service);
613                         if (n > sizeof(sep->se_ctrladdr_un.sun_path) - 1)
614                                 n = sizeof(sep->se_ctrladdr_un.sun_path) - 1;
615                         strncpy(sep->se_ctrladdr_un.sun_path, sep->se_service, n);
616                         sep->se_ctrladdr_un.sun_family = AF_UNIX;
617                         sep->se_ctrladdr_size = n +
618                                         sizeof sep->se_ctrladdr_un.sun_family;
619                         setup(sep);
620                         break;
621                 case AF_INET:
622                         sep->se_ctrladdr_in.sin_family = AF_INET;
623                         sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in;
624                         {
625                                 u_short port = bb_lookup_port(sep->se_service, sep->se_proto, 0);
626
627                                 if (port == 0) {
628                                         syslog(LOG_ERR,
629                                             "%s/%s: unknown service",
630                                             sep->se_service, sep->se_proto);
631                                         continue;
632                                 }
633                                 if (port != sep->se_ctrladdr_in.sin_port) {
634                                         sep->se_ctrladdr_in.sin_port = port;
635                                         if (sep->se_fd != -1) {
636                                                 FD_CLR(sep->se_fd, &allsock);
637                                                 nsock--;
638                                                 (void) close(sep->se_fd);
639                                         }
640                                         sep->se_fd = -1;
641                                 }
642                                 if (sep->se_fd == -1)
643                                         setup(sep);
644                         }
645                 }
646         }
647         if (fconfig) {
648                 (void) fclose(fconfig);
649                 fconfig = NULL;
650         }
651         /*
652          * Purge anything not looked at above.
653          */
654         sigprocmask(SIG_SETMASK, &blockmask, &oldmask);
655         sepp = &servtab;
656         while ((sep = *sepp) != NULL) {
657                 if (sep->se_checked) {
658                         sepp = &sep->se_next;
659                         continue;
660                 }
661                 *sepp = sep->se_next;
662                 if (sep->se_fd != -1) {
663                         FD_CLR(sep->se_fd, &allsock);
664                         nsock--;
665                         (void) close(sep->se_fd);
666                 }
667                 if (sep->se_family == AF_UNIX)
668                         (void)unlink(sep->se_service);
669                 freeconfig(sep);
670                 free((char *)sep);
671         }
672         sigprocmask(SIG_SETMASK, &oldmask, NULL);
673 }
674
675
676
677 static void reapchild(int signum)
678 {
679         int status;
680         int pid;
681         struct servtab *sep;
682
683         (void)signum;
684         for (;;) {
685                 pid = wait3(&status, WNOHANG, (struct rusage *)0);
686                 if (pid <= 0)
687                         break;
688                 for (sep = servtab; sep; sep = sep->se_next)
689                         if (sep->se_wait == pid) {
690                                 if (WIFEXITED(status) && WEXITSTATUS(status))
691                                         syslog(LOG_WARNING,
692                                             "%s: exit status 0x%x",
693                                             sep->se_server, WEXITSTATUS(status));
694                                 else if (WIFSIGNALED(status))
695                                         syslog(LOG_WARNING,
696                                             "%s: exit signal 0x%x",
697                                             sep->se_server, WTERMSIG(status));
698                                 sep->se_wait = 1;
699                                 FD_SET(sep->se_fd, &allsock);
700                                 nsock++;
701                         }
702         }
703 }
704
705 static void retry(int signum)
706 {
707         struct servtab *sep;
708
709         (void)signum;
710         timingout = 0;
711         for (sep = servtab; sep; sep = sep->se_next) {
712                 if (sep->se_fd == -1) {
713                         switch (sep->se_family) {
714                         case AF_UNIX:
715                         case AF_INET:
716                                 setup(sep);
717                                 break;
718                         }
719                 }
720         }
721 }
722
723 static void goaway(int signum)
724 {
725         struct servtab *sep;
726
727         (void)signum;
728         for (sep = servtab; sep; sep = sep->se_next)
729                 if (sep->se_fd != -1 && sep->se_family == AF_UNIX)
730                         (void)unlink(sep->se_service);
731         (void)unlink(_PATH_INETDPID);
732         exit(0);
733 }
734
735
736
737 extern int inetd_main(int argc, char *argv[])
738 {
739         struct servtab *sep;
740         struct group *grp = NULL;
741         struct sigaction sa;
742         int pid;
743         unsigned long opt;
744         char *sq;
745         gid_t gid;
746
747 #ifdef INETD_FEATURE_ENABLED
748         extern char **environ;
749 #endif
750
751         gid = getgid();
752         setgroups(1, &gid);
753
754 #ifdef INETD_FEATURE_ENABLED
755         Argv = argv;
756         if (environ == 0 || *environ == 0)
757                 environ = argv;
758         while (*environ)
759                 environ++;
760         LastArg = environ[-1] + strlen(environ[-1]);
761 #endif
762
763 #if defined(__uClinux__)
764         opt = bb_getopt_ulflags(argc, argv, "q:f", &sq);
765         if (!(opt & 2)) {
766             daemon(0, 0);
767             /* reexec for vfork() do continue parent */
768             vfork_daemon_rexec(argc, argv, "-f");
769         }
770 #else
771         opt = bb_getopt_ulflags(argc, argv, "q:", &sq);
772         daemon(0, 0);
773 #endif /* uClinux */
774
775         if(opt & 1) {
776                         global_queuelen = atoi(optarg);
777                         if (global_queuelen < 8) global_queuelen=8;
778                 }
779         argc -= optind;
780         argv += optind;
781
782         if (argc > 0)
783                 CONFIG = argv[0];
784
785         openlog(bb_applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
786         {
787                 FILE *fp;
788
789                 if ((fp = fopen(_PATH_INETDPID, "w")) != NULL) {
790                         fprintf(fp, "%u\n", getpid());
791                         (void)fclose(fp);
792                 }
793         }
794
795 #ifdef RLIMIT_NOFILE
796         if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
797                 syslog(LOG_ERR, "getrlimit: %m");
798         } else {
799                 rlim_ofile_cur = rlim_ofile.rlim_cur;
800                 if (rlim_ofile_cur == RLIM_INFINITY)    /* ! */
801                         rlim_ofile_cur = OPEN_MAX;
802         }
803 #endif
804
805         config(0);
806
807         sigemptyset(&emptymask);
808         sigemptyset(&blockmask);
809         sigaddset(&blockmask, SIGCHLD);
810         sigaddset(&blockmask, SIGHUP);
811         sigaddset(&blockmask, SIGALRM);
812
813         memset(&sa, 0, sizeof(sa));
814         sa.sa_mask = blockmask;
815         sa.sa_handler = retry;
816         sigaction(SIGALRM, &sa, NULL);
817         sa.sa_handler = config;
818         sigaction(SIGHUP, &sa, NULL);
819         sa.sa_handler = reapchild;
820         sigaction(SIGCHLD, &sa, NULL);
821         sa.sa_handler = goaway;
822         sigaction(SIGTERM, &sa, NULL);
823         sa.sa_handler = goaway;
824         sigaction(SIGINT, &sa,  NULL);
825         sa.sa_handler = SIG_IGN;
826         sigaction(SIGPIPE, &sa, NULL);
827
828         {
829                 /* space for daemons to overwrite environment for ps */
830 #define DUMMYSIZE       100
831                 char dummy[DUMMYSIZE];
832
833                 (void)memset(dummy, 'x', DUMMYSIZE - 1);
834                 dummy[DUMMYSIZE - 1] = '\0';
835
836                 (void)setenv("inetd_dummy", dummy, 1);
837         }
838
839         for (;;) {
840             int n, ctrl;
841             fd_set readable;
842
843             if (nsock == 0) {
844                 sigprocmask(SIG_BLOCK, &blockmask, NULL);
845                 while (nsock == 0)
846                     sigsuspend(&emptymask);
847                 sigprocmask(SIG_SETMASK, &emptymask, NULL);
848             }
849             readable = allsock;
850             if ((n = select(maxsock + 1, &readable, (fd_set *)0,
851                 (fd_set *)0, (struct timeval *)0)) <= 0) {
852                     if (n < 0 && errno != EINTR)
853                         syslog(LOG_WARNING, "select: %m");
854                     sleep(1);
855                     continue;
856             }
857             for (sep = servtab; n && sep; sep = sep->se_next)
858             if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
859                 n--;
860                 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
861                         /* Fixed AGC */
862                         fcntl(sep->se_fd, F_SETFL, O_NDELAY);
863                         /* --------- */
864                         ctrl = accept(sep->se_fd, NULL, NULL);
865                         fcntl(sep->se_fd, F_SETFL, 0);
866                         if (ctrl < 0) {
867                                 if (errno == EINTR || errno == EWOULDBLOCK)
868                                         continue;
869                                 syslog(LOG_WARNING, "accept (for %s): %m",
870                                         sep->se_service);
871                                 continue;
872                         }
873                 } else
874                         ctrl = sep->se_fd;
875                 sigprocmask(SIG_BLOCK, &blockmask, NULL);
876                 pid = 0;
877 #ifdef INETD_FEATURE_ENABLED
878                 if (sep->se_bi == 0 || sep->se_bi->bi_fork)
879 #endif
880                 {
881                         if (sep->se_count++ == 0)
882                             (void)gettimeofday(&sep->se_time, (struct timezone *)0);
883                         else if (sep->se_count >= sep->se_max) {
884                                 struct timeval now;
885
886                                 (void)gettimeofday(&now, (struct timezone *)0);
887                                 if (now.tv_sec - sep->se_time.tv_sec >
888                                     CNT_INTVL) {
889                                         sep->se_time = now;
890                                         sep->se_count = 1;
891                                 } else {
892                                         syslog(LOG_ERR,
893                         "%s/%s server failing (looping), service terminated",
894                                             sep->se_service, sep->se_proto);
895                                         FD_CLR(sep->se_fd, &allsock);
896                                         (void) close(sep->se_fd);
897                                         sep->se_fd = -1;
898                                         sep->se_count = 0;
899                                         nsock--;
900                                         sigprocmask(SIG_SETMASK, &emptymask,
901                                                     NULL);
902                                         if (!timingout) {
903                                                 timingout = 1;
904                                                 alarm(RETRYTIME);
905                                         }
906                                         continue;
907                                 }
908                         }
909                         pid = fork();
910                         if (pid < 0) {
911                                 syslog(LOG_ERR, "fork: %m");
912                                 if (sep->se_socktype == SOCK_STREAM)
913                                         close(ctrl);
914                                 sigprocmask(SIG_SETMASK, &emptymask, NULL);
915                                 sleep(1);
916                                 continue;
917                         }
918                         if (pid && sep->se_wait) {
919                                 sep->se_wait = pid;
920                                 FD_CLR(sep->se_fd, &allsock);
921                                 nsock--;
922                         }
923                 }
924                 sigprocmask(SIG_SETMASK, &emptymask, NULL);
925                 if (pid == 0) {
926 #ifdef INETD_FEATURE_ENABLED
927                         if (sep->se_bi)
928                                 (*sep->se_bi->bi_fn)(ctrl, sep);
929                         else
930 #endif
931                         {
932                                 struct passwd *pwd = getpwnam(sep->se_user);
933                                 if (pwd == NULL) {
934                                         syslog_err_and_discard_dg(
935                                                 sep->se_socktype,
936                                                 "getpwnam: %s: No such user",
937                                                 sep->se_user);
938                                 }
939                                 if (sep->se_group &&
940                                     (grp = getgrnam(sep->se_group)) == NULL) {
941                                         syslog_err_and_discard_dg(
942                                                 sep->se_socktype,
943                                                 "getgrnam: %s: No such group",
944                                                 sep->se_group);
945                                 }
946                                 /*
947                                  * Ok. There are four cases here:
948                                  *   1. nonroot user, no group specified
949                                  *   2. nonroot user, some group specified
950                                  *   3. root user, no group specified
951                                  *   4. root user, some group specified
952                                  * In cases 2 and 4 we setgid to the specified
953                                  * group. In cases 1 and 2 we run initgroups
954                                  * to run with the groups of the given user.
955                                  * In case 4 we do setgroups to run with the
956                                  * given group. In case 3 we do nothing.
957                                  */
958                                 if (pwd->pw_uid) {
959                                         if (sep->se_group)
960                                                 pwd->pw_gid = grp->gr_gid;
961                                         setgid((gid_t)pwd->pw_gid);
962                                         initgroups(pwd->pw_name, pwd->pw_gid);
963                                         setuid((uid_t)pwd->pw_uid);
964                                 } else if (sep->se_group) {
965                                         setgid((gid_t)grp->gr_gid);
966                                         setgroups(1, &grp->gr_gid);
967                                 }
968                                 dup2(ctrl, 0);
969                                 close(ctrl);
970                                 dup2(0, 1);
971                                 dup2(0, 2);
972 #ifdef RLIMIT_NOFILE
973                                 if (rlim_ofile.rlim_cur != rlim_ofile_cur) {
974                                         if (setrlimit(RLIMIT_NOFILE,
975                                                         &rlim_ofile) < 0)
976                                                 syslog(LOG_ERR,"setrlimit: %m");
977                                 }
978 #endif
979                                 for (ctrl = rlim_ofile_cur-1; --ctrl > 2; )
980                                         (void)close(ctrl);
981
982                                 memset(&sa, 0, sizeof(sa));
983                                 sa.sa_handler = SIG_DFL;
984                                 sigaction(SIGPIPE, &sa, NULL);
985
986                                 execv(sep->se_server, sep->se_argv);
987                                 syslog_err_and_discard_dg(sep->se_socktype,
988                                         "execv %s: %m", sep->se_server);
989                         }
990                 }
991                 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
992                         close(ctrl);
993             }
994         }
995 }
996
997
998 /*
999  * Internet services provided internally by inetd:
1000  */
1001 #define BUFSIZE 4096
1002
1003 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
1004 /* Echo service -- echo data back */
1005 static void echo_stream(int s, struct servtab *sep)
1006 {
1007         char buffer[BUFSIZE];
1008         int i;
1009
1010         setproctitle(sep->se_service, s);
1011         while ((i = read(s, buffer, sizeof(buffer))) > 0 &&
1012             write(s, buffer, i) > 0)
1013                 ;
1014         exit(0);
1015 }
1016
1017 /* Echo service -- echo data back */
1018 static void echo_dg(int s, struct servtab *sep)
1019 {
1020         char buffer[BUFSIZE];
1021         int i;
1022         size_t size;
1023         struct sockaddr sa;
1024
1025         (void)sep;
1026
1027         size = sizeof(sa);
1028         if ((i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size)) < 0)
1029                 return;
1030         (void) sendto(s, buffer, i, 0, &sa, sizeof(sa));
1031 }
1032 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO */
1033
1034
1035 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
1036 /* Discard service -- ignore data */
1037 static void discard_stream(int s, struct servtab *sep)
1038 {
1039         char buffer[BUFSIZE];
1040
1041         setproctitle(sep->se_service, s);
1042         while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) ||
1043                         errno == EINTR)
1044                 ;
1045         exit(0);
1046 }
1047
1048 /* Discard service -- ignore data */
1049 static void discard_dg(int s, struct servtab *sep)
1050 {
1051         char buffer[BUFSIZE];
1052         (void)sep;
1053         read(s, buffer, sizeof(buffer));
1054 }
1055 #endif  /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD */
1056
1057
1058 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
1059 #include <ctype.h>
1060 #define LINESIZ 72
1061 static char ring[128];
1062 static char *endring;
1063
1064 static void initring(void)
1065 {
1066         int i;
1067
1068         endring = ring;
1069
1070         for (i = 0; i <= 128; ++i)
1071                 if (isprint(i))
1072                         *endring++ = i;
1073 }
1074
1075 /* Character generator */
1076 static void chargen_stream(int s, struct servtab *sep)
1077 {
1078         char *rs;
1079         int len;
1080         char text[LINESIZ+2];
1081
1082         setproctitle(sep->se_service, s);
1083
1084         if (!endring) {
1085                 initring();
1086                 rs = ring;
1087         }
1088
1089         text[LINESIZ] = '\r';
1090         text[LINESIZ + 1] = '\n';
1091         for (rs = ring;;) {
1092                 if ((len = endring - rs) >= LINESIZ)
1093                         memcpy(rs, text, LINESIZ);
1094                 else {
1095                         memcpy(rs, text, len);
1096                         memcpy(ring, text + len, LINESIZ - len);
1097                 }
1098                 if (++rs == endring)
1099                         rs = ring;
1100                 if (write(s, text, sizeof(text)) != sizeof(text))
1101                         break;
1102         }
1103         exit(0);
1104 }
1105
1106 /* Character generator */
1107 static void chargen_dg(int s, struct servtab *sep)
1108 {
1109         struct sockaddr sa;
1110         static char *rs;
1111         size_t len, size;
1112         char text[LINESIZ+2];
1113
1114         (void)sep;
1115
1116         if (endring == 0) {
1117                 initring();
1118                 rs = ring;
1119         }
1120
1121         size = sizeof(sa);
1122         if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0)
1123                 return;
1124
1125         if ((len = endring - rs) >= LINESIZ)
1126                 memcpy(rs, text, LINESIZ);
1127         else {
1128                 memcpy(rs, text, len);
1129                 memcpy(ring, text + len, LINESIZ - len);
1130         }
1131         if (++rs == endring)
1132                 rs = ring;
1133         text[LINESIZ] = '\r';
1134         text[LINESIZ + 1] = '\n';
1135         (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa));
1136 }
1137 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN */
1138
1139
1140 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
1141 /*
1142  * Return a machine readable date and time, in the form of the
1143  * number of seconds since midnight, Jan 1, 1900.  Since gettimeofday
1144  * returns the number of seconds since midnight, Jan 1, 1970,
1145  * we must add 2208988800 seconds to this figure to make up for
1146  * some seventy years Bell Labs was asleep.
1147  */
1148
1149 static long machtime(void)
1150 {
1151         struct timeval tv;
1152
1153         if (gettimeofday(&tv, (struct timezone *)0) < 0) {
1154                 fprintf(stderr, "Unable to get time of day\n");
1155                 return (0L);
1156         }
1157         return (htonl((long)tv.tv_sec + 2208988800UL));
1158 }
1159
1160 static void machtime_stream(int s, struct servtab *sep)
1161 {
1162         long result;
1163         (void)sep;
1164
1165         result = machtime();
1166         write(s, (char *) &result, sizeof(result));
1167 }
1168
1169 static void machtime_dg(int s, struct servtab *sep)
1170 {
1171         long result;
1172         struct sockaddr sa;
1173         size_t size;
1174         (void)sep;
1175
1176         size = sizeof(sa);
1177         if (recvfrom(s, (char *)&result, sizeof(result), 0, &sa, &size) < 0)
1178                 return;
1179         result = machtime();
1180         (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa));
1181 }
1182 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME */
1183
1184
1185 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
1186 /* Return human-readable time of day */
1187 static int human_readable_time_sprintf(char *buffer)
1188 {
1189         time_t clocc = time(NULL);
1190
1191         return sprintf(buffer, "%.24s\r\n", ctime(&clocc));
1192 }
1193
1194 static void daytime_stream(int s, struct servtab *sep)
1195 {
1196         char buffer[256];
1197         size_t st = human_readable_time_sprintf(buffer);
1198
1199         (void)sep;
1200
1201         write(s, buffer, st);
1202 }
1203
1204 /* Return human-readable time of day */
1205 static void daytime_dg(int s, struct servtab *sep)
1206 {
1207         char buffer[256];
1208         struct sockaddr sa;
1209         size_t size;
1210
1211         (void)sep;
1212
1213         size = sizeof(sa);
1214         if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0)
1215                 return;
1216         size = human_readable_time_sprintf(buffer);
1217         sendto(s, buffer, size, 0, &sa, sizeof(sa));
1218 }
1219 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME */