2 * Copyright (c) 1983,1991 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Busybox port by Vladimir Oleynik (C) 2001-2003 <dzo@simtreas.ru>
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.
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.
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
27 * Inetd - Internet super-server
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
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''.
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.
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)
59 * RPC services unsuported
61 * Comment lines are indicated by a `#' in column 1.
65 * Here's the scoop concerning the user.group feature:
69 * a) for root: NO setuid() or setgid() is done
71 * b) nonroot: setuid()
72 * setgid(primary group as found in passwd)
73 * initgroups(name, primary group)
75 * 2) set-group-option on.
77 * a) for root: NO setuid()
78 * setgid(specified group)
79 * setgroups(1, specified group)
81 * b) nonroot: setuid()
82 * setgid(specified group)
83 * initgroups(name, specified group)
85 * All supplementary groups are discarded at startup in case inetd was
89 #define __USE_BSD_SIGNAL
93 #include <sys/param.h>
95 #include <sys/ioctl.h>
96 #include <sys/socket.h>
100 #include <sys/time.h>
101 #include <sys/resource.h>
104 #ifndef RLIMIT_NOFILE
105 #define RLIMIT_NOFILE RLIMIT_OFILE
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>
118 #include <netinet/in.h>
119 #include <netinet/ip.h>
120 #include <arpa/inet.h>
134 #define _PATH_INETDCONF "/etc/inetd.conf"
135 #define _PATH_INETDPID "/var/run/inetd.pid"
138 #define MIN(a, b) ((a) < (b) ? (a) : (b))
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 */
150 /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
151 #define FD_MARGIN (8)
152 static int rlim_ofile_cur = OPEN_MAX;
155 static struct rlimit rlim_ofile;
158 #define INETD_UNSUPPORT_BILTIN 1
160 /* Check unsupporting builtin */
161 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
162 #undef INETD_UNSUPPORT_BILTIN
164 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
165 #undef INETD_UNSUPPORT_BILTIN
167 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
168 #undef INETD_UNSUPPORT_BILTIN
170 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
171 #undef INETD_UNSUPPORT_BILTIN
173 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
174 #undef INETD_UNSUPPORT_BILTIN
177 static struct servtab {
178 char *se_service; /* name of service */
179 int se_socktype; /* type of socket to use */
180 int se_family; /* address family */
181 char *se_proto; /* protocol used */
182 short se_wait; /* single threaded server */
183 short se_checked; /* looked at during merge */
184 char *se_user; /* user name to run as */
185 char *se_group; /* group name to run as */
186 #ifndef INETD_UNSUPPORT_BILTIN
187 const struct biltin *se_bi; /* if built-in, description */
189 char *se_server; /* server program */
191 char *se_argv[MAXARGV+1]; /* program arguments */
192 int se_fd; /* open descriptor */
194 struct sockaddr se_un_ctrladdr;
195 struct sockaddr_in se_un_ctrladdr_in;
196 struct sockaddr_un se_un_ctrladdr_un;
197 } se_un; /* bound address */
198 #define se_ctrladdr se_un.se_un_ctrladdr
199 #define se_ctrladdr_in se_un.se_un_ctrladdr_in
200 #define se_ctrladdr_un se_un.se_un_ctrladdr_un
201 int se_ctrladdr_size;
202 int se_max; /* max # of instances of this service */
203 int se_count; /* number started since se_time */
204 struct timeval se_time; /* start of se_count */
205 struct servtab *se_next;
208 /* Length of socket listen queue. Should be per-service probably. */
209 static int global_queuelen = 128;
211 static int nsock, maxsock;
212 static fd_set allsock;
213 static int timingout;
214 static sigset_t blockmask, emptymask;
217 /* Echo received data */
218 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
219 static void echo_stream(int, struct servtab *);
220 static void echo_dg(int, struct servtab *);
222 /* Internet /dev/null */
223 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
224 static void discard_stream(int, struct servtab *);
225 static void discard_dg(int, struct servtab *);
227 /* Return 32 bit time since 1900 */
228 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
229 static void machtime_stream(int, struct servtab *);
230 static void machtime_dg(int, struct servtab *);
232 /* Return human-readable time */
233 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
234 static void daytime_stream(int, struct servtab *);
235 static void daytime_dg(int, struct servtab *);
237 /* Familiar character generator */
238 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
239 static void chargen_stream(int, struct servtab *);
240 static void chargen_dg(int, struct servtab *);
243 #ifndef INETD_UNSUPPORT_BILTIN
245 const char *bi_service; /* internally provided service name */
246 int bi_socktype; /* type of socket supported */
247 short bi_fork; /* 1 if should fork before call */
248 short bi_wait; /* 1 if should wait for child */
249 void (*bi_fn)(int, struct servtab *); /* fn which performs it */
252 static const struct biltin biltins[] = {
253 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
254 /* Echo received data */
255 { "echo", SOCK_STREAM, 1, 0, echo_stream, },
256 { "echo", SOCK_DGRAM, 0, 0, echo_dg, },
258 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
259 /* Internet /dev/null */
260 { "discard", SOCK_STREAM, 1, 0, discard_stream, },
261 { "discard", SOCK_DGRAM, 0, 0, discard_dg, },
263 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
264 /* Return 32 bit time since 1900 */
265 { "time", SOCK_STREAM, 0, 0, machtime_stream, },
266 { "time", SOCK_DGRAM, 0, 0, machtime_dg, },
268 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
269 /* Return human-readable time */
270 { "daytime", SOCK_STREAM, 0, 0, daytime_stream, },
271 { "daytime", SOCK_DGRAM, 0, 0, daytime_dg, },
273 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
274 /* Familiar character generator */
275 { "chargen", SOCK_STREAM, 1, 0, chargen_stream, },
276 { "chargen", SOCK_DGRAM, 0, 0, chargen_dg, },
278 { NULL, 0, 0, 0, NULL }
280 #endif /* INETD_UNSUPPORT_BILTIN */
282 #define NUMINT (sizeof(intab) / sizeof(struct inent))
283 static const char *CONFIG = _PATH_INETDCONF;
285 #define BCOPY(s, d, z) memcpy(d, s, z)
288 syslog_err_and_discard_dg(int se_socktype, const char *msg, ...)
289 __attribute__ ((noreturn, format (printf, 2, 3)));
292 syslog_err_and_discard_dg(int se_socktype, const char *msg, ...)
298 vsyslog(LOG_ERR, msg, p);
299 if (se_socktype != SOCK_STREAM)
300 recv(0, buf, sizeof (buf), 0);
304 static FILE *fconfig;
305 static char line[256];
315 f = fconfig = fopen(CONFIG, "r");
317 syslog(LOG_ERR, "%s: %m", CONFIG);
328 if (fgets(line, sizeof (line), fd) == NULL)
330 cp = strchr(line, '\n');
346 while (*cp == ' ' || *cp == '\t')
352 (void) ungetc(c, fconfig);
353 if (c == ' ' || c == '\t')
354 if ((cp = nextline()) != NULL)
360 while (*cp && *cp != ' ' && *cp != '\t')
371 cp = strdup(cp ? cp : "");
375 syslog_err_and_discard_dg(SOCK_STREAM, "strdup: %m");
379 static struct servtab *
382 static struct servtab serv;
383 struct servtab *sep = &serv;
388 while ((cp = nextline()) && *cp == '#')
391 return ((struct servtab *)0);
392 memset((char *)sep, 0, sizeof *sep);
393 sep->se_service = newstr(skip(&cp));
398 if (strcmp(arg, "stream") == 0)
399 sep->se_socktype = SOCK_STREAM;
400 else if (strcmp(arg, "dgram") == 0)
401 sep->se_socktype = SOCK_DGRAM;
402 else if (strcmp(arg, "rdm") == 0)
403 sep->se_socktype = SOCK_RDM;
404 else if (strcmp(arg, "seqpacket") == 0)
405 sep->se_socktype = SOCK_SEQPACKET;
406 else if (strcmp(arg, "raw") == 0)
407 sep->se_socktype = SOCK_RAW;
409 sep->se_socktype = -1;
411 sep->se_proto = newstr(skip(&cp));
412 if (strcmp(sep->se_proto, "unix") == 0) {
413 sep->se_family = AF_UNIX;
415 sep->se_family = AF_INET;
416 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
417 syslog(LOG_ERR, "%s: rpc services not suported",
426 char *s = strchr(arg, '.');
429 sep->se_max = atoi(s);
431 sep->se_max = TOOMANY;
433 sep->se_wait = strcmp(arg, "wait") == 0;
434 sep->se_user = newstr(skip(&cp));
435 sep->se_group = strchr(sep->se_user, '.');
437 *sep->se_group++ = '\0';
439 sep->se_server = newstr(skip(&cp));
440 if (strcmp(sep->se_server, "internal") == 0) {
441 #ifndef INETD_UNSUPPORT_BILTIN
442 const struct biltin *bi;
444 for (bi = biltins; bi->bi_service; bi++)
445 if (bi->bi_socktype == sep->se_socktype &&
446 strcmp(bi->bi_service, sep->se_service) == 0)
448 if (bi->bi_service == 0) {
449 syslog(LOG_ERR, "internal service %s unknown",
454 sep->se_wait = bi->bi_wait;
456 syslog(LOG_ERR, "internal service %s unknown",
461 #ifndef INETD_UNSUPPORT_BILTIN
466 for (arg = skip(&cp); cp; arg = skip(&cp)) {
468 sep->se_argv[argc++] = newstr(arg);
470 while (argc <= MAXARGV)
471 sep->se_argv[argc++] = NULL;
476 freeconfig(struct servtab *cp)
480 free(cp->se_service);
483 /* Note: se_group is part of the newstr'ed se_user */
485 for (i = 0; i < MAXARGV; i++)
486 free(cp->se_argv[i]);
489 #ifndef INETD_UNSUPPORT_BILTIN
491 static char *LastArg;
494 setproctitle(char *a, int s)
498 struct sockaddr_in sn;
503 if (getpeername(s, (struct sockaddr *)&sn, &size) == 0)
504 (void) sprintf(buf, "-%s [%s]", a, inet_ntoa(sn.sin_addr));
506 (void) sprintf(buf, "-%s", a);
507 strncpy(cp, buf, LastArg - cp);
512 #endif /* INETD_UNSUPPORT_BILTIN */
514 static struct servtab *
515 enter(struct servtab *cp)
520 sep = (struct servtab *)malloc(sizeof (*sep));
522 syslog_err_and_discard_dg(SOCK_STREAM, bb_msg_memory_exhausted);
526 sigprocmask(SIG_BLOCK, &blockmask, &oldmask);
527 sep->se_next = servtab;
529 sigprocmask(SIG_SETMASK, &oldmask, NULL);
542 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
543 syslog(LOG_ERR, "getrlimit: %m");
546 rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK);
547 if (rl.rlim_cur <= rlim_ofile_cur) {
549 #if _FILE_OFFSET_BITS == 64
550 "bump_nofile: cannot extend file limit, max = %lld",
552 "bump_nofile: cannot extend file limit, max = %ld",
558 if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
559 syslog(LOG_ERR, "setrlimit: %m");
563 rlim_ofile_cur = rl.rlim_cur;
567 syslog(LOG_ERR, "bump_nofile: cannot extend file limit");
574 setup(struct servtab *sep)
578 if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
579 syslog(LOG_ERR, "%s/%s: socket: %m",
580 sep->se_service, sep->se_proto);
583 if (setsockopt(sep->se_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
585 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
586 if (bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size) < 0) {
587 syslog(LOG_ERR, "%s/%s: bind: %m",
588 sep->se_service, sep->se_proto);
589 (void) close(sep->se_fd);
597 if (sep->se_socktype == SOCK_STREAM)
598 listen(sep->se_fd, global_queuelen);
600 FD_SET(sep->se_fd, &allsock);
602 if (sep->se_fd > maxsock) {
603 maxsock = sep->se_fd;
604 if (maxsock > rlim_ofile_cur - FD_MARGIN)
612 struct servtab *sep, *cp, **sepp;
617 if (setconfig() == NULL)
620 for (sep = servtab; sep; sep = sep->se_next)
622 while ((cp = getconfigent()) != NULL) {
623 for (sep = servtab; sep; sep = sep->se_next)
624 if (strcmp(sep->se_service, cp->se_service) == 0 &&
625 strcmp(sep->se_proto, cp->se_proto) == 0)
630 #define SWAP(type, a, b) {type c=(type)a; (type)a=(type)b; (type)b=(type)c;}
632 sigprocmask(SIG_BLOCK, &emptymask, &oldmask);
634 * sep->se_wait may be holding the pid of a daemon
635 * that we're waiting for. If so, don't overwrite
636 * it unless the config file explicitly says don't
640 #ifndef INETD_UNSUPPORT_BILTIN
643 (sep->se_wait == 1 || cp->se_wait == 0))
644 sep->se_wait = cp->se_wait;
645 if (cp->se_max != sep->se_max)
646 SWAP(int, cp->se_max, sep->se_max);
648 SWAP(char *, sep->se_user, cp->se_user);
650 SWAP(char *, sep->se_group, cp->se_group);
652 SWAP(char *, sep->se_server, cp->se_server);
653 for (i = 0; i < MAXARGV; i++)
654 SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
656 sigprocmask(SIG_SETMASK, &oldmask, NULL);
663 switch (sep->se_family) {
665 if (sep->se_fd != -1)
667 (void)unlink(sep->se_service);
668 n = strlen(sep->se_service);
669 if (n > sizeof(sep->se_ctrladdr_un.sun_path) - 1)
670 n = sizeof(sep->se_ctrladdr_un.sun_path) - 1;
671 strncpy(sep->se_ctrladdr_un.sun_path, sep->se_service, n);
672 sep->se_ctrladdr_un.sun_family = AF_UNIX;
673 sep->se_ctrladdr_size = n +
674 sizeof sep->se_ctrladdr_un.sun_family;
678 sep->se_ctrladdr_in.sin_family = AF_INET;
679 sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in;
681 u_short port = htons(atoi(sep->se_service));
685 sp = getservbyname(sep->se_service,
689 "%s/%s: unknown service",
690 sep->se_service, sep->se_proto);
695 if (port != sep->se_ctrladdr_in.sin_port) {
696 sep->se_ctrladdr_in.sin_port = port;
697 if (sep->se_fd != -1) {
698 FD_CLR(sep->se_fd, &allsock);
700 (void) close(sep->se_fd);
704 if (sep->se_fd == -1)
710 (void) fclose(fconfig);
714 * Purge anything not looked at above.
716 sigprocmask(SIG_SETMASK, &blockmask, &oldmask);
718 while ((sep = *sepp) != NULL) {
719 if (sep->se_checked) {
720 sepp = &sep->se_next;
723 *sepp = sep->se_next;
724 if (sep->se_fd != -1) {
725 FD_CLR(sep->se_fd, &allsock);
727 (void) close(sep->se_fd);
729 if (sep->se_family == AF_UNIX)
730 (void)unlink(sep->se_service);
734 sigprocmask(SIG_SETMASK, &oldmask, NULL);
740 reapchild(int signum)
748 pid = wait3(&status, WNOHANG, (struct rusage *)0);
751 for (sep = servtab; sep; sep = sep->se_next)
752 if (sep->se_wait == pid) {
753 if (WIFEXITED(status) && WEXITSTATUS(status))
755 "%s: exit status 0x%x",
756 sep->se_server, WEXITSTATUS(status));
757 else if (WIFSIGNALED(status))
759 "%s: exit signal 0x%x",
760 sep->se_server, WTERMSIG(status));
762 FD_SET(sep->se_fd, &allsock);
775 for (sep = servtab; sep; sep = sep->se_next) {
776 if (sep->se_fd == -1) {
777 switch (sep->se_family) {
793 for (sep = servtab; sep; sep = sep->se_next)
794 if (sep->se_fd != -1 && sep->se_family == AF_UNIX)
795 (void)unlink(sep->se_service);
796 (void)unlink(_PATH_INETDPID);
803 inetd_main(int argc, char *argv[])
807 struct group *grp = NULL;
812 #ifdef INETD_UNSUPPORT_BILTIN
816 extern char **environ;
822 #ifndef INETD_UNSUPPORT_BILTIN
824 if (environ == 0 || *environ == 0)
828 LastArg = environ[-1] + strlen(environ[-1]);
831 while ((ch = getopt(argc, argv, "q:")) != EOF)
834 global_queuelen = atoi(optarg);
835 if (global_queuelen < 8) global_queuelen=8;
838 bb_show_usage(); // "[-q len] [conf]"
847 openlog(bb_applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
851 if ((fp = fopen(_PATH_INETDPID, "w")) != NULL) {
852 fprintf(fp, "%u\n", getpid());
858 if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
859 syslog(LOG_ERR, "getrlimit: %m");
861 rlim_ofile_cur = rlim_ofile.rlim_cur;
862 if (rlim_ofile_cur == RLIM_INFINITY) /* ! */
863 rlim_ofile_cur = OPEN_MAX;
869 sigemptyset(&emptymask);
870 sigemptyset(&blockmask);
871 sigaddset(&blockmask, SIGCHLD);
872 sigaddset(&blockmask, SIGHUP);
873 sigaddset(&blockmask, SIGALRM);
875 memset(&sa, 0, sizeof(sa));
876 sa.sa_mask = blockmask;
877 sa.sa_handler = retry;
878 sigaction(SIGALRM, &sa, NULL);
879 sa.sa_handler = config;
880 sigaction(SIGHUP, &sa, NULL);
881 sa.sa_handler = reapchild;
882 sigaction(SIGCHLD, &sa, NULL);
883 sa.sa_handler = goaway;
884 sigaction(SIGTERM, &sa, NULL);
885 sa.sa_handler = goaway;
886 sigaction(SIGINT, &sa, NULL);
887 sa.sa_handler = SIG_IGN;
888 sigaction(SIGPIPE, &sa, NULL);
891 /* space for daemons to overwrite environment for ps */
892 #define DUMMYSIZE 100
893 char dummy[DUMMYSIZE];
895 (void)memset(dummy, 'x', DUMMYSIZE - 1);
896 dummy[DUMMYSIZE - 1] = '\0';
898 (void)setenv("inetd_dummy", dummy, 1);
906 sigprocmask(SIG_BLOCK, &blockmask, NULL);
908 sigsuspend(&emptymask);
909 sigprocmask(SIG_SETMASK, &emptymask, NULL);
912 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
913 (fd_set *)0, (struct timeval *)0)) <= 0) {
914 if (n < 0 && errno != EINTR)
915 syslog(LOG_WARNING, "select: %m");
919 for (sep = servtab; n && sep; sep = sep->se_next)
920 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
922 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
924 fcntl(sep->se_fd, F_SETFL, O_NDELAY);
926 ctrl = accept(sep->se_fd, NULL, NULL);
927 fcntl(sep->se_fd, F_SETFL, 0);
929 if (errno == EINTR || errno == EWOULDBLOCK)
931 syslog(LOG_WARNING, "accept (for %s): %m",
937 sigprocmask(SIG_BLOCK, &blockmask, NULL);
939 #ifndef INETD_UNSUPPORT_BILTIN
940 dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork);
943 if (sep->se_count++ == 0)
944 (void)gettimeofday(&sep->se_time,
945 (struct timezone *)0);
946 else if (sep->se_count >= sep->se_max) {
949 (void)gettimeofday(&now, (struct timezone *)0);
950 if (now.tv_sec - sep->se_time.tv_sec >
956 "%s/%s server failing (looping), service terminated",
957 sep->se_service, sep->se_proto);
958 FD_CLR(sep->se_fd, &allsock);
959 (void) close(sep->se_fd);
963 sigprocmask(SIG_SETMASK, &emptymask,
975 syslog(LOG_ERR, "fork: %m");
976 if (sep->se_socktype == SOCK_STREAM)
978 sigprocmask(SIG_SETMASK, &emptymask, NULL);
982 if (pid && sep->se_wait) {
984 FD_CLR(sep->se_fd, &allsock);
987 sigprocmask(SIG_SETMASK, &emptymask, NULL);
989 #ifndef INETD_UNSUPPORT_BILTIN
991 (*sep->se_bi->bi_fn)(ctrl, sep);
995 if ((pwd = getpwnam(sep->se_user)) == NULL) {
996 syslog_err_and_discard_dg(
998 "getpwnam: %s: No such user",
1001 if (sep->se_group &&
1002 (grp = getgrnam(sep->se_group)) == NULL) {
1003 syslog_err_and_discard_dg(
1005 "getgrnam: %s: No such group",
1009 * Ok. There are four cases here:
1010 * 1. nonroot user, no group specified
1011 * 2. nonroot user, some group specified
1012 * 3. root user, no group specified
1013 * 4. root user, some group specified
1014 * In cases 2 and 4 we setgid to the specified
1015 * group. In cases 1 and 2 we run initgroups
1016 * to run with the groups of the given user.
1017 * In case 4 we do setgroups to run with the
1018 * given group. In case 3 we do nothing.
1022 pwd->pw_gid = grp->gr_gid;
1023 setgid((gid_t)pwd->pw_gid);
1024 initgroups(pwd->pw_name, pwd->pw_gid);
1025 setuid((uid_t)pwd->pw_uid);
1026 } else if (sep->se_group) {
1027 setgid((gid_t)grp->gr_gid);
1028 setgroups(1, &grp->gr_gid);
1034 #ifdef RLIMIT_NOFILE
1035 if (rlim_ofile.rlim_cur != rlim_ofile_cur) {
1036 if (setrlimit(RLIMIT_NOFILE,
1038 syslog(LOG_ERR,"setrlimit: %m");
1041 for (ctrl = rlim_ofile_cur-1; --ctrl > 2; )
1044 memset(&sa, 0, sizeof(sa));
1045 sa.sa_handler = SIG_DFL;
1046 sigaction(SIGPIPE, &sa, NULL);
1048 execv(sep->se_server, sep->se_argv);
1049 syslog_err_and_discard_dg(sep->se_socktype,
1050 "execv %s: %m", sep->se_server);
1053 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
1061 * Internet services provided internally by inetd:
1063 #define BUFSIZE 4096
1065 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
1066 /* Echo service -- echo data back */
1068 echo_stream(int s, struct servtab *sep)
1070 char buffer[BUFSIZE];
1073 setproctitle(sep->se_service, s);
1074 while ((i = read(s, buffer, sizeof(buffer))) > 0 &&
1075 write(s, buffer, i) > 0)
1080 /* Echo service -- echo data back */
1082 echo_dg(int s, struct servtab *sep)
1084 char buffer[BUFSIZE];
1092 if ((i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size)) < 0)
1094 (void) sendto(s, buffer, i, 0, &sa, sizeof(sa));
1096 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO */
1099 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
1100 /* Discard service -- ignore data */
1102 discard_stream(int s, struct servtab *sep)
1104 char buffer[BUFSIZE];
1106 setproctitle(sep->se_service, s);
1107 while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) ||
1113 /* Discard service -- ignore data */
1115 discard_dg(int s, struct servtab *sep)
1117 char buffer[BUFSIZE];
1119 read(s, buffer, sizeof(buffer));
1121 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD */
1124 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
1127 static char ring[128];
1128 static char *endring;
1137 for (i = 0; i <= 128; ++i)
1142 /* Character generator */
1144 chargen_stream(int s, struct servtab *sep)
1148 char text[LINESIZ+2];
1150 setproctitle(sep->se_service, s);
1157 text[LINESIZ] = '\r';
1158 text[LINESIZ + 1] = '\n';
1160 if ((len = endring - rs) >= LINESIZ)
1161 BCOPY(rs, text, LINESIZ);
1163 BCOPY(rs, text, len);
1164 BCOPY(ring, text + len, LINESIZ - len);
1166 if (++rs == endring)
1168 if (write(s, text, sizeof(text)) != sizeof(text))
1174 /* Character generator */
1176 chargen_dg(int s, struct servtab *sep)
1181 char text[LINESIZ+2];
1191 if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0)
1194 if ((len = endring - rs) >= LINESIZ)
1195 BCOPY(rs, text, LINESIZ);
1197 BCOPY(rs, text, len);
1198 BCOPY(ring, text + len, LINESIZ - len);
1200 if (++rs == endring)
1202 text[LINESIZ] = '\r';
1203 text[LINESIZ + 1] = '\n';
1204 (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa));
1206 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN */
1209 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
1211 * Return a machine readable date and time, in the form of the
1212 * number of seconds since midnight, Jan 1, 1900. Since gettimeofday
1213 * returns the number of seconds since midnight, Jan 1, 1970,
1214 * we must add 2208988800 seconds to this figure to make up for
1215 * some seventy years Bell Labs was asleep.
1223 if (gettimeofday(&tv, (struct timezone *)0) < 0) {
1224 fprintf(stderr, "Unable to get time of day\n");
1227 return (htonl((long)tv.tv_sec + 2208988800UL));
1231 machtime_stream(int s, struct servtab *sep)
1236 result = machtime();
1237 write(s, (char *) &result, sizeof(result));
1241 machtime_dg(int s, struct servtab *sep)
1249 if (recvfrom(s, (char *)&result, sizeof(result), 0, &sa, &size) < 0)
1251 result = machtime();
1252 (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa));
1254 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME */
1257 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
1258 /* Return human-readable time of day */
1260 human_readable_time_sprintf(char *buffer)
1262 time_t clocc = time(NULL);
1264 return sprintf(buffer, "%.24s\r\n", ctime(&clocc));
1268 daytime_stream(int s, struct servtab *sep)
1271 size_t st = human_readable_time_sprintf(buffer);
1275 write(s, buffer, st);
1278 /* Return human-readable time of day */
1280 daytime_dg(int s, struct servtab *sep)
1289 if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0)
1291 size = human_readable_time_sprintf(buffer);
1292 sendto(s, buffer, size, 0, &sa, sizeof(sa));
1294 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME */