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