blob: a7259f3d43ce084a2c80702bccaf84a5056bb6a6 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00002/* $Slackware: inetd.c 1.79s 2001/02/06 13:18:00 volkerdi Exp $ */
3/* $OpenBSD: inetd.c,v 1.79 2001/01/30 08:30:57 deraadt Exp $ */
4/* $NetBSD: inetd.c,v 1.11 1996/02/22 11:14:41 mycroft Exp $ */
5/* Busybox port by Vladimir Oleynik (C) 2001-2005 <dzo@simtreas.ru> */
Glenn L McGrath06e95652003-02-09 06:51:14 +00006/*
7 * Copyright (c) 1983,1991 The Regents of the University of California.
8 * All rights reserved.
9 *
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +000010 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
Glenn L McGrath06e95652003-02-09 06:51:14 +000025 *
Denis Vlasenkof8138d12007-01-11 23:26:13 +000026 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +000027 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
Glenn L McGrath06e95652003-02-09 06:51:14 +000037 */
38
Denis Vlasenkof8138d12007-01-11 23:26:13 +000039/* Inetd - Internet super-server
Glenn L McGrath06e95652003-02-09 06:51:14 +000040 *
41 * This program invokes all internet services as needed.
42 * connection-oriented services are invoked each time a
43 * connection is made, by creating a process. This process
44 * is passed the connection as file descriptor 0 and is
45 * expected to do a getpeername to find out the source host
46 * and port.
47 *
48 * Datagram oriented services are invoked when a datagram
49 * arrives; a process is created and passed a pending message
50 * on file descriptor 0. Datagram servers may either connect
51 * to their peer, freeing up the original socket for inetd
Denis Vlasenkof8138d12007-01-11 23:26:13 +000052 * to receive further messages on, or "take over the socket",
Glenn L McGrath06e95652003-02-09 06:51:14 +000053 * processing all arriving datagrams and, eventually, timing
Denis Vlasenkof8138d12007-01-11 23:26:13 +000054 * out. The first type of server is said to be "multi-threaded";
55 * the second type of server "single-threaded".
Glenn L McGrath06e95652003-02-09 06:51:14 +000056 *
57 * Inetd uses a configuration file which is read at startup
58 * and, possibly, at some later time in response to a hangup signal.
Denis Vlasenkof8138d12007-01-11 23:26:13 +000059 * The configuration file is "free format" with fields given in the
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +000060 * order shown below. Continuation lines for an entry must begin with
Glenn L McGrath06e95652003-02-09 06:51:14 +000061 * a space or tab. All fields must be present in each entry.
62 *
63 * service name must be in /etc/services
64 * socket type stream/dgram/raw/rdm/seqpacket
65 * protocol must be in /etc/protocols
66 * wait/nowait[.max] single-threaded/multi-threaded, max #
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +000067 * user[.group] or user[:group] user/group to run daemon as
Glenn L McGrath06e95652003-02-09 06:51:14 +000068 * server program full path name
69 * server program arguments maximum of MAXARGS (20)
70 *
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +000071 * For RPC services
72 * service name/version must be in /etc/rpc
73 * socket type stream/dgram/raw/rdm/seqpacket
74 * protocol must be in /etc/protocols
75 * wait/nowait[.max] single-threaded/multi-threaded
76 * user[.group] or user[:group] user to run daemon as
77 * server program full path name
78 * server program arguments maximum of MAXARGS (20)
79 *
80 * For non-RPC services, the "service name" can be of the form
81 * hostaddress:servicename, in which case the hostaddress is used
82 * as the host portion of the address to listen on. If hostaddress
83 * consists of a single `*' character, INADDR_ANY is used.
84 *
85 * A line can also consist of just
86 * hostaddress:
87 * where hostaddress is as in the preceding paragraph. Such a line must
88 * have no further fields; the specified hostaddress is remembered and
89 * used for all further lines that have no hostaddress specified,
90 * until the next such line (or EOF). (This is why * is provided to
91 * allow explicit specification of INADDR_ANY.) A line
92 * *:
93 * is implicitly in effect at the beginning of the file.
94 *
95 * The hostaddress specifier may (and often will) contain dots;
96 * the service name must not.
97 *
98 * For RPC services, host-address specifiers are accepted and will
99 * work to some extent; however, because of limitations in the
100 * portmapper interface, it will not work to try to give more than
101 * one line for any given RPC service, even if the host-address
102 * specifiers are different.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000103 *
104 * Comment lines are indicated by a `#' in column 1.
105 */
106
Denis Vlasenkof8138d12007-01-11 23:26:13 +0000107/* inetd rules for passing file descriptors to children
108 * (http://www.freebsd.org/cgi/man.cgi?query=inetd):
109 *
110 * The wait/nowait entry specifies whether the server that is invoked by
111 * inetd will take over the socket associated with the service access point,
112 * and thus whether inetd should wait for the server to exit before listen-
113 * ing for new service requests. Datagram servers must use "wait", as
114 * they are always invoked with the original datagram socket bound to the
115 * specified service address. These servers must read at least one datagram
116 * from the socket before exiting. If a datagram server connects to its
117 * peer, freeing the socket so inetd can receive further messages on the
118 * socket, it is said to be a "multi-threaded" server; it should read one
119 * datagram from the socket and create a new socket connected to the peer.
120 * It should fork, and the parent should then exit to allow inetd to check
121 * for new service requests to spawn new servers. Datagram servers which
122 * process all incoming datagrams on a socket and eventually time out are
123 * said to be "single-threaded". The comsat(8), (biff(1)) and talkd(8)
124 * utilities are both examples of the latter type of datagram server. The
125 * tftpd(8) utility is an example of a multi-threaded datagram server.
126 *
127 * Servers using stream sockets generally are multi-threaded and use the
128 * "nowait" entry. Connection requests for these services are accepted by
129 * inetd, and the server is given only the newly-accepted socket connected
130 * to a client of the service. Most stream-based services operate in this
131 * manner. Stream-based servers that use "wait" are started with the lis-
132 * tening service socket, and must accept at least one connection request
133 * before exiting. Such a server would normally accept and process incoming
134 * connection requests until a timeout.
135 */
136
137/* Here's the scoop concerning the user[.:]group feature:
Glenn L McGrath06e95652003-02-09 06:51:14 +0000138 *
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000139 * 1) set-group-option off.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000140 *
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000141 * a) user = root: NO setuid() or setgid() is done
Glenn L McGrath06e95652003-02-09 06:51:14 +0000142 *
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000143 * b) other: setgid(primary group as found in passwd)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000144 * initgroups(name, primary group)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000145 * setuid()
Glenn L McGrath06e95652003-02-09 06:51:14 +0000146 *
147 * 2) set-group-option on.
148 *
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000149 * a) user = root: setgid(specified group)
150 * NO initgroups()
151 * NO setuid()
Glenn L McGrath06e95652003-02-09 06:51:14 +0000152 *
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000153 * b) other: setgid(specified group)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000154 * initgroups(name, specified group)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000155 * setuid()
Glenn L McGrath06e95652003-02-09 06:51:14 +0000156 */
157
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000158#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +0000159#include <syslog.h>
Rob Landley099ed502006-08-28 09:41:49 +0000160#include <sys/un.h>
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000161
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000162//#define ENABLE_FEATURE_INETD_RPC 1
163//#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO 1
164//#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD 1
165//#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME 1
166//#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME 1
167//#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN 1
168//#define ENABLE_FEATURE_IPV6 1
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000169
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000170#if ENABLE_FEATURE_INETD_RPC
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000171#include <rpc/rpc.h>
172#include <rpc/pmap_clnt.h>
Glenn L McGrathff6ec8a2004-01-17 02:47:45 +0000173#endif
174
Denis Vlasenko5b340832007-05-17 23:02:14 +0000175#define _PATH_INETDPID "/var/run/inetd.pid"
176
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000177#define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
178#define RETRYTIME (60*10) /* retry after bind or server fail */
179
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000180#ifndef RLIMIT_NOFILE
181#define RLIMIT_NOFILE RLIMIT_OFILE
182#endif
183
184#ifndef OPEN_MAX
185#define OPEN_MAX 64
186#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000187
188/* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
Denis Vlasenkof8138d12007-01-11 23:26:13 +0000189#define FD_MARGIN 8
Glenn L McGrath06e95652003-02-09 06:51:14 +0000190
Glenn L McGrathb1207b32003-02-10 22:31:09 +0000191/* Check unsupporting builtin */
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000192#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \
193 ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD || \
194 ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME || \
195 ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME || \
196 ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
Glenn L McGrathc3b134f2004-01-17 01:26:53 +0000197# define INETD_FEATURE_ENABLED
Glenn L McGrathb1207b32003-02-10 22:31:09 +0000198#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000199
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000200#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \
201 ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD || \
202 ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000203# define INETD_SETPROCTITLE
Glenn L McGrath06e95652003-02-09 06:51:14 +0000204#endif
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000205
Denis Vlasenko512a5452007-09-24 10:41:30 +0000206typedef struct servtab_t {
Denis Vlasenko39824072007-09-26 10:46:18 +0000207 /* The most frequently referenced one: */
208 int se_fd; /* open descriptor */
209 /* NB: 'biggest fields last' saves on code size (~250 bytes) */
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000210 char *se_hostaddr; /* host address to listen on */
211 char *se_service; /* name of service */
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000212 char *se_proto; /* protocol used */
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000213#if ENABLE_FEATURE_INETD_RPC
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000214 int se_rpcprog; /* rpc program number */
215 int se_rpcversl; /* rpc program lowest version */
216 int se_rpcversh; /* rpc program highest version */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000217#define isrpcservice(sep) ((sep)->se_rpcversl != 0)
218#else
219#define isrpcservice(sep) 0
220#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000221 pid_t se_wait; /* single threaded server */
Denis Vlasenko512a5452007-09-24 10:41:30 +0000222 socktype_t se_socktype; /* type of socket to use */
223 family_t se_family; /* address family */
224 smallint se_checked; /* looked at during merge */
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000225 char *se_user; /* user name to run as */
226 char *se_group; /* group name to run as */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000227#ifdef INETD_FEATURE_ENABLED
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000228 const struct builtin *se_bi; /* if built-in, description */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000229#endif
Denis Vlasenko39824072007-09-26 10:46:18 +0000230 int se_ctrladdr_size;
231 int se_max; /* max # of instances of this service */
232 int se_count; /* number started since se_time */
233 struct servtab_t *se_next;
234 struct timeval se_time; /* start of se_count */
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000235 char *se_server; /* server program */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000236#define MAXARGV 20
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000237 char *se_argv[MAXARGV + 1]; /* program arguments */
Denis Vlasenkoc1876d72006-09-23 15:58:01 +0000238 union {
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000239 struct sockaddr se_un_ctrladdr;
240 struct sockaddr_in se_un_ctrladdr_in;
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000241#if ENABLE_FEATURE_IPV6
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000242 struct sockaddr_in6 se_un_ctrladdr_in6;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000243#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000244 struct sockaddr_un se_un_ctrladdr_un;
245 } se_un; /* bound address */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000246#define se_ctrladdr se_un.se_un_ctrladdr
247#define se_ctrladdr_in se_un.se_un_ctrladdr_in
248#define se_ctrladdr_in6 se_un.se_un_ctrladdr_in6
249#define se_ctrladdr_un se_un.se_un_ctrladdr_un
Glenn L McGrath03a06432004-02-18 13:19:58 +0000250} servtab_t;
251
Glenn L McGrathff6ec8a2004-01-17 02:47:45 +0000252#ifdef INETD_FEATURE_ENABLED
Denis Vlasenkoc1876d72006-09-23 15:58:01 +0000253struct builtin {
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000254 const char *bi_service; /* internally provided service name */
Denis Vlasenko512a5452007-09-24 10:41:30 +0000255 socktype_t bi_socktype; /* type of socket supported */
256 uint8_t bi_fork; /* 1 if should fork before call */
257// Commented since it is always 0
258// uint8_t bi_wait; /* 1 if should wait for child */
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000259 void (*bi_fn) (int, servtab_t *);
Glenn L McGrathff6ec8a2004-01-17 02:47:45 +0000260};
Glenn L McGrath06e95652003-02-09 06:51:14 +0000261
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000262 /* Echo received data */
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000263#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000264static void echo_stream(int, servtab_t *);
265static void echo_dg(int, servtab_t *);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000266#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000267 /* Internet /dev/null */
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000268#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000269static void discard_stream(int, servtab_t *);
270static void discard_dg(int, servtab_t *);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000271#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000272 /* Return 32 bit time since 1900 */
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000273#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000274static void machtime_stream(int, servtab_t *);
275static void machtime_dg(int, servtab_t *);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000276#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000277 /* Return human-readable time */
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000278#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000279static void daytime_stream(int, servtab_t *);
280static void daytime_dg(int, servtab_t *);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000281#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000282 /* Familiar character generator */
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000283#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000284static void chargen_stream(int, servtab_t *);
285static void chargen_dg(int, servtab_t *);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000286#endif
287
Bernhard Reutner-Fischera4acf662006-04-10 12:26:47 +0000288static const struct builtin builtins[] = {
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000289#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000290 /* Echo received data */
Denis Vlasenko512a5452007-09-24 10:41:30 +0000291 {"echo", SOCK_STREAM, 1, echo_stream,},
292 {"echo", SOCK_DGRAM, 0, echo_dg,},
Glenn L McGrath06e95652003-02-09 06:51:14 +0000293#endif
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000294#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000295 /* Internet /dev/null */
Denis Vlasenko512a5452007-09-24 10:41:30 +0000296 {"discard", SOCK_STREAM, 1, discard_stream,},
297 {"discard", SOCK_DGRAM, 0, discard_dg,},
Glenn L McGrath06e95652003-02-09 06:51:14 +0000298#endif
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000299#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000300 /* Return 32 bit time since 1900 */
Denis Vlasenko512a5452007-09-24 10:41:30 +0000301 {"time", SOCK_STREAM, 0, machtime_stream,},
302 {"time", SOCK_DGRAM, 0, machtime_dg,},
Glenn L McGrath06e95652003-02-09 06:51:14 +0000303#endif
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000304#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000305 /* Return human-readable time */
Denis Vlasenko512a5452007-09-24 10:41:30 +0000306 {"daytime", SOCK_STREAM, 0, daytime_stream,},
307 {"daytime", SOCK_DGRAM, 0, daytime_dg,},
Glenn L McGrath06e95652003-02-09 06:51:14 +0000308#endif
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000309#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000310 /* Familiar character generator */
Denis Vlasenko512a5452007-09-24 10:41:30 +0000311 {"chargen", SOCK_STREAM, 1, chargen_stream,},
312 {"chargen", SOCK_DGRAM, 0, chargen_dg,},
Glenn L McGrath06e95652003-02-09 06:51:14 +0000313#endif
Denis Vlasenko512a5452007-09-24 10:41:30 +0000314 { /* zero filled */ }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000315};
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000316#endif /* INETD_FEATURE_ENABLED */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000317
Denis Vlasenko512a5452007-09-24 10:41:30 +0000318struct globals {
319 rlim_t rlim_ofile_cur;
320 struct rlimit rlim_ofile;
321 servtab_t *servtab;
322 int global_queuelen;
323 int nsock;
324 int maxsock;
325 int toomany;
326 int timingout;
327 struct servent *sp;
328 uid_t uid;
329 const char *config_filename;
330 FILE *fconfig;
331 char *defhost;
332#ifdef INETD_SETPROCTITLE
333 char **Argv;
334 char *LastArg;
335#endif
336#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
337 char *endring;
338 char *ringpos;
339 char ring[128];
340#endif
341 fd_set allsock;
342 /* Used only in nextline() */
343 char line[80]; /* at least 80, see LINE_SIZE */
344};
345#define G (*(struct globals*)&bb_common_bufsiz1)
346enum { LINE_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line) };
347struct BUG_G_too_big {
348 char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1];
349};
350#define rlim_ofile_cur (G.rlim_ofile_cur )
351#define rlim_ofile (G.rlim_ofile )
352#define servtab (G.servtab )
353#define global_queuelen (G.global_queuelen)
354#define nsock (G.nsock )
355#define maxsock (G.maxsock )
356#define toomany (G.toomany )
357#define timingout (G.timingout )
358#define sp (G.sp )
359#define uid (G.uid )
360#define config_filename (G.config_filename)
361#define fconfig (G.fconfig )
362#define defhost (G.defhost )
363#define Argv (G.Argv )
364#define LastArg (G.LastArg )
365#define endring (G.endring )
366#define ringpos (G.ringpos )
367#define ring (G.ring )
368#define allsock (G.allsock )
369#define line (G.line )
370#define INIT_G() do { \
371 rlim_ofile_cur = OPEN_MAX; \
372 global_queuelen = 128; \
373 config_filename = "/etc/inetd.conf"; \
374} while (0)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000375
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000376/* xstrdup(NULL) returns NULL, but this one
377 * will return newly-allocated "" if called with NULL arg
378 * TODO: audit whether this makes any real difference
379 */
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000380static char *xxstrdup(char *cp)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000381{
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000382 return xstrdup(cp ? cp : "");
Glenn L McGrath06e95652003-02-09 06:51:14 +0000383}
384
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000385static int setconfig(void)
Glenn L McGrathdf7d84c2004-02-22 11:25:13 +0000386{
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000387 free(defhost);
388 defhost = xstrdup("*");
389 if (fconfig != NULL) {
390 fseek(fconfig, 0L, SEEK_SET);
391 return 1;
392 }
Denis Vlasenko55f30b02007-03-24 22:42:29 +0000393 fconfig = fopen(config_filename, "r");
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000394 return (fconfig != NULL);
Glenn L McGrathdf7d84c2004-02-22 11:25:13 +0000395}
396
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000397static void endconfig(void)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000398{
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000399 if (fconfig) {
400 (void) fclose(fconfig);
401 fconfig = NULL;
402 }
403 free(defhost);
404 defhost = 0;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000405}
Glenn L McGrath53766c42004-01-18 08:58:06 +0000406
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000407#if ENABLE_FEATURE_INETD_RPC
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000408static void register_rpc(servtab_t *sep)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000409{
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000410 int n;
411 struct sockaddr_in ir_sin;
412 struct protoent *pp;
413 socklen_t size;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000414
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000415 pp = getprotobyname(sep->se_proto + 4);
416 if (pp == NULL) {
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000417 bb_perror_msg("%s: getproto", sep->se_proto);
418 return;
419 }
420 size = sizeof ir_sin;
421 if (getsockname(sep->se_fd, (struct sockaddr *) &ir_sin, &size) < 0) {
422 bb_perror_msg("%s/%s: getsockname",
423 sep->se_service, sep->se_proto);
424 return;
425 }
Glenn L McGratheaf5bc02004-01-20 15:32:39 +0000426
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000427 for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
428 (void) pmap_unset(sep->se_rpcprog, n);
429 if (!pmap_set(sep->se_rpcprog, n, pp->p_proto, ntohs(ir_sin.sin_port)))
430 bb_perror_msg("%s %s: pmap_set: %u %u %u %u",
431 sep->se_service, sep->se_proto,
432 sep->se_rpcprog, n, pp->p_proto, ntohs(ir_sin.sin_port));
433 }
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000434}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000435
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000436static void unregister_rpc(servtab_t *sep)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000437{
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000438 int n;
Glenn L McGratheaf5bc02004-01-20 15:32:39 +0000439
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000440 for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
441 if (!pmap_unset(sep->se_rpcprog, n))
442 bb_error_msg("pmap_unset(%u, %u)", sep->se_rpcprog, n);
443 }
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000444}
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000445#endif /* FEATURE_INETD_RPC */
Glenn L McGratheaf5bc02004-01-20 15:32:39 +0000446
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000447static void freeconfig(servtab_t *cp)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000448{
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000449 int i;
Glenn L McGrathdf7d84c2004-02-22 11:25:13 +0000450
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000451 free(cp->se_hostaddr);
452 free(cp->se_service);
453 free(cp->se_proto);
454 free(cp->se_user);
455 free(cp->se_group);
456 free(cp->se_server);
457 for (i = 0; i < MAXARGV; i++)
458 free(cp->se_argv[i]);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000459}
Glenn L McGrathdf7d84c2004-02-22 11:25:13 +0000460
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000461static int bump_nofile(void)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000462{
463#define FD_CHUNK 32
Glenn L McGratheaf5bc02004-01-20 15:32:39 +0000464
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000465 struct rlimit rl;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000466
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000467 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000468 bb_perror_msg("getrlimit");
469 return -1;
470 }
471 rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK);
472 rl.rlim_cur = MIN(FD_SETSIZE, rl.rlim_cur + FD_CHUNK);
473 if (rl.rlim_cur <= rlim_ofile_cur) {
474 bb_error_msg("bump_nofile: cannot extend file limit, max = %d",
475 (int) rl.rlim_cur);
476 return -1;
477 }
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000478
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000479 if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
480 bb_perror_msg("setrlimit");
481 return -1;
482 }
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000483
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000484 rlim_ofile_cur = rl.rlim_cur;
485 return 0;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000486}
487
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000488static void setup(servtab_t *sep)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000489{
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000490 int r;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000491
Denis Vlasenkoc1876d72006-09-23 15:58:01 +0000492 sep->se_fd = socket(sep->se_family, sep->se_socktype, 0);
493 if (sep->se_fd < 0) {
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000494 bb_perror_msg("%s/%s: socket", sep->se_service, sep->se_proto);
495 return;
496 }
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000497 setsockopt_reuseaddr(sep->se_fd);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000498
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000499#if ENABLE_FEATURE_INETD_RPC
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000500 if (isrpcservice(sep)) {
501 struct passwd *pwd;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000502
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000503 /*
504 * for RPC services, attempt to use a reserved port
505 * if they are going to be running as root.
506 *
507 * Also, zero out the port for all RPC services; let bind()
508 * find one.
509 */
510 sep->se_ctrladdr_in.sin_port = 0;
511 if (sep->se_user && (pwd = getpwnam(sep->se_user)) &&
512 pwd->pw_uid == 0 && uid == 0)
513 r = bindresvport(sep->se_fd, &sep->se_ctrladdr_in);
514 else {
515 r = bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size);
516 if (r == 0) {
517 socklen_t len = sep->se_ctrladdr_size;
518 int saveerrno = errno;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000519
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000520 /* update se_ctrladdr_in.sin_port */
521 r = getsockname(sep->se_fd, &sep->se_ctrladdr, &len);
522 if (r <= 0)
523 errno = saveerrno;
524 }
525 }
526 } else
Glenn L McGrath53766c42004-01-18 08:58:06 +0000527#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000528 r = bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size);
529 if (r < 0) {
530 bb_perror_msg("%s/%s (%d): bind",
531 sep->se_service, sep->se_proto, sep->se_ctrladdr.sa_family);
532 close(sep->se_fd);
533 sep->se_fd = -1;
534 if (!timingout) {
535 timingout = 1;
536 alarm(RETRYTIME);
537 }
538 return;
Glenn L McGrath53766c42004-01-18 08:58:06 +0000539 }
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000540 if (sep->se_socktype == SOCK_STREAM)
541 listen(sep->se_fd, global_queuelen);
Glenn L McGrath53766c42004-01-18 08:58:06 +0000542
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000543 FD_SET(sep->se_fd, &allsock);
544 nsock++;
545 if (sep->se_fd > maxsock) {
546 maxsock = sep->se_fd;
547 if ((rlim_t)maxsock > rlim_ofile_cur - FD_MARGIN)
548 bump_nofile();
549 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000550}
551
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000552static char *nextline(void)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000553{
Denis Vlasenko6df9e3c2007-11-12 21:21:35 +0000554 if (fgets(line, LINE_SIZE, fconfig) == NULL)
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000555 return NULL;
Denis Vlasenkoc03e8722007-12-26 20:56:55 +0000556 *strchrnul(line, '\n') = '\0';
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000557 return line;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000558}
559
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000560static char *skip(char **cpp) /* int report; */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000561{
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000562 char *cp = *cpp;
563 char *start;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000564
565/* erp: */
Denis Vlasenko6df9e3c2007-11-12 21:21:35 +0000566 if (cp == NULL) {
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000567 /* if (report) */
568 /* bb_error_msg("syntax error in inetd config file"); */
569 return NULL;
570 }
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000571
Denis Vlasenkoce074df2007-03-24 12:07:31 +0000572 again:
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000573 while (*cp == ' ' || *cp == '\t')
574 cp++;
575 if (*cp == '\0') {
Denis Vlasenko6df9e3c2007-11-12 21:21:35 +0000576 int c = getc(fconfig);
Denis Vlasenko55f30b02007-03-24 22:42:29 +0000577 ungetc(c, fconfig);
578 if (c == ' ' || c == '\t') {
579 cp = nextline();
580 if (cp)
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000581 goto again;
Denis Vlasenko55f30b02007-03-24 22:42:29 +0000582 }
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000583 *cpp = NULL;
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000584 return NULL;
585 }
586 start = cp;
587 while (*cp && *cp != ' ' && *cp != '\t')
588 cp++;
589 if (*cp != '\0')
590 *cp++ = '\0';
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000591
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000592 *cpp = cp;
593 return start;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000594}
595
596static servtab_t *new_servtab(void)
597{
Denis Vlasenko6df9e3c2007-11-12 21:21:35 +0000598 return xzalloc(sizeof(servtab_t));
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000599}
600
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000601static servtab_t *dupconfig(servtab_t *sep)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000602{
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000603 servtab_t *newtab;
604 int argc;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000605
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000606 newtab = new_servtab();
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000607 newtab->se_service = xstrdup(sep->se_service);
608 newtab->se_socktype = sep->se_socktype;
609 newtab->se_family = sep->se_family;
610 newtab->se_proto = xstrdup(sep->se_proto);
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000611#if ENABLE_FEATURE_INETD_RPC
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000612 newtab->se_rpcprog = sep->se_rpcprog;
613 newtab->se_rpcversl = sep->se_rpcversl;
614 newtab->se_rpcversh = sep->se_rpcversh;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000615#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000616 newtab->se_wait = sep->se_wait;
617 newtab->se_user = xstrdup(sep->se_user);
618 newtab->se_group = xstrdup(sep->se_group);
Glenn L McGrathc3b134f2004-01-17 01:26:53 +0000619#ifdef INETD_FEATURE_ENABLED
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000620 newtab->se_bi = sep->se_bi;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000621#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000622 newtab->se_server = xstrdup(sep->se_server);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000623
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000624 for (argc = 0; argc <= MAXARGV; argc++)
625 newtab->se_argv[argc] = xstrdup(sep->se_argv[argc]);
626 newtab->se_max = sep->se_max;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000627
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000628 return newtab;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000629}
630
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000631static servtab_t *getconfigent(void)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000632{
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000633 servtab_t *sep;
634 int argc;
635 char *cp, *arg;
636 char *hostdelim;
637 servtab_t *nsep;
638 servtab_t *psep;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000639
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000640 sep = new_servtab();
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000641
Denis Vlasenko13858992006-10-08 12:49:22 +0000642 more:
Denis Vlasenko6df9e3c2007-11-12 21:21:35 +0000643 while ((cp = nextline()) && *cp == '#')
644 continue; /* skip comment lines */
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000645 if (cp == NULL) {
Denis Vlasenko6df9e3c2007-11-12 21:21:35 +0000646 free(sep);
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000647 return NULL;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000648 }
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000649
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000650 arg = skip(&cp);
651 if (arg == NULL) {
652 /* A blank line. */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000653 goto more;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000654 }
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000655
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000656 /* Check for a host name. */
657 hostdelim = strrchr(arg, ':');
658 if (hostdelim) {
659 *hostdelim = '\0';
660 sep->se_hostaddr = xstrdup(arg);
661 arg = hostdelim + 1;
662 /*
663 * If the line is of the form `host:', then just change the
664 * default host for the following lines.
665 */
666 if (*arg == '\0') {
667 arg = skip(&cp);
668 if (cp == NULL) {
669 free(defhost);
670 defhost = sep->se_hostaddr;
671 goto more;
672 }
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000673 }
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000674 } else
675 sep->se_hostaddr = xxstrdup(defhost);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000676
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000677 sep->se_service = xxstrdup(arg);
678 arg = skip(&cp);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000679
Denis Vlasenko9d1afdb2007-10-30 19:54:39 +0000680 {
681 static int8_t SOCK_xxx[] ALIGN1 = {
682 -1,
683 SOCK_STREAM, SOCK_DGRAM, SOCK_RDM,
684 SOCK_SEQPACKET, SOCK_RAW
685 };
686 sep->se_socktype = SOCK_xxx[1 + index_in_strings(
687 "stream""\0" "dgram""\0" "rdm""\0"
688 "seqpacket""\0" "raw""\0"
689 , arg)];
690 }
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000691
692 sep->se_proto = xxstrdup(skip(&cp));
693
694 if (strcmp(sep->se_proto, "unix") == 0) {
695 sep->se_family = AF_UNIX;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000696 } else {
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000697 sep->se_family = AF_INET;
698 if (sep->se_proto[strlen(sep->se_proto) - 1] == '6')
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000699#if ENABLE_FEATURE_IPV6
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000700 sep->se_family = AF_INET6;
701#else
702 bb_error_msg("%s: IPV6 not supported", sep->se_proto);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000703#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000704 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000705#if ENABLE_FEATURE_INETD_RPC
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000706 char *p, *ccp;
707 long l;
708
709 p = strchr(sep->se_service, '/');
710 if (p == 0) {
711 bb_error_msg("%s: no rpc version", sep->se_service);
712 goto more;
713 }
714 *p++ = '\0';
715 l = strtol(p, &ccp, 0);
716 if (ccp == p || l < 0 || l > INT_MAX) {
Denis Vlasenkoce074df2007-03-24 12:07:31 +0000717 badafterall:
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000718 bb_error_msg("%s/%s: bad rpc version", sep->se_service, p);
719 goto more;
720 }
721 sep->se_rpcversl = sep->se_rpcversh = l;
722 if (*ccp == '-') {
723 p = ccp + 1;
724 l = strtol(p, &ccp, 0);
725 if (ccp == p || l < 0 || l > INT_MAX || l < sep->se_rpcversl || *ccp)
726 goto badafterall;
727 sep->se_rpcversh = l;
728 } else if (*ccp != '\0')
729 goto badafterall;
730#else
Denis Vlasenko13858992006-10-08 12:49:22 +0000731 bb_error_msg("%s: rpc services not supported", sep->se_service);
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000732#endif
733 }
734 }
735 arg = skip(&cp);
736 if (arg == NULL)
737 goto more;
738
739 {
740 char *s = strchr(arg, '.');
741 if (s) {
742 *s++ = '\0';
Denis Vlasenko13858992006-10-08 12:49:22 +0000743 sep->se_max = xatoi(s);
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000744 } else
745 sep->se_max = toomany;
746 }
Denis Vlasenko6df9e3c2007-11-12 21:21:35 +0000747 sep->se_wait = (strcmp(arg, "wait") == 0);
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000748 sep->se_user = xxstrdup(skip(&cp));
749 arg = strchr(sep->se_user, '.');
750 if (arg == NULL)
751 arg = strchr(sep->se_user, ':');
752 if (arg) {
753 *arg++ = '\0';
754 sep->se_group = xstrdup(arg);
755 }
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000756
Denis Vlasenko6df9e3c2007-11-12 21:21:35 +0000757 sep->se_server = xxstrdup(skip(&cp));
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000758#ifdef INETD_FEATURE_ENABLED
Denis Vlasenko6df9e3c2007-11-12 21:21:35 +0000759 /* sep->se_bi = NULL; - done by new_servtab() */
760 if (strcmp(sep->se_server, "internal") == 0) {
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000761 const struct builtin *bi;
762
763 for (bi = builtins; bi->bi_service; bi++)
Denis Vlasenko512a5452007-09-24 10:41:30 +0000764 if (bi->bi_socktype == sep->se_socktype
765 && strcmp(bi->bi_service, sep->se_service) == 0)
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000766 break;
767 if (bi->bi_service == 0) {
768 bb_error_msg("internal service %s unknown", sep->se_service);
769 goto more;
770 }
771 sep->se_bi = bi;
Denis Vlasenko512a5452007-09-24 10:41:30 +0000772 sep->se_wait = 0; /* = bi->bi_wait; - always 0 */
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000773 }
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000774#endif
775 argc = 0;
Denis Vlasenko6df9e3c2007-11-12 21:21:35 +0000776 while ((arg = skip(&cp)) != NULL && argc < MAXARGV) {
777 sep->se_argv[argc++] = xxstrdup(arg);
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000778 }
Denis Vlasenko6df9e3c2007-11-12 21:21:35 +0000779 /* while (argc <= MAXARGV) */
780 /* sep->se_argv[argc++] = NULL; - done by new_servtab() */
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000781
782 /*
783 * Now that we've processed the entire line, check if the hostname
784 * specifier was a comma separated list of hostnames. If so
785 * we'll make new entries for each address.
786 */
787 while ((hostdelim = strrchr(sep->se_hostaddr, ',')) != NULL) {
788 nsep = dupconfig(sep);
789
790 /*
791 * NULL terminate the hostname field of the existing entry,
792 * and make a dup for the new entry.
793 */
794 *hostdelim++ = '\0';
795 nsep->se_hostaddr = xstrdup(hostdelim);
796
797 nsep->se_next = sep->se_next;
798 sep->se_next = nsep;
799 }
800
801 nsep = sep;
802 while (nsep != NULL) {
803 nsep->se_checked = 1;
804 if (nsep->se_family == AF_INET) {
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000805 if (LONE_CHAR(nsep->se_hostaddr, '*'))
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000806 nsep->se_ctrladdr_in.sin_addr.s_addr = INADDR_ANY;
807 else if (!inet_aton(nsep->se_hostaddr, &nsep->se_ctrladdr_in.sin_addr)) {
Denis Vlasenko512a5452007-09-24 10:41:30 +0000808 int i;
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000809 struct hostent *hp;
810
811 hp = gethostbyname(nsep->se_hostaddr);
Denis Vlasenko512a5452007-09-24 10:41:30 +0000812 if (hp == NULL) {
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000813 bb_error_msg("%s: unknown host", nsep->se_hostaddr);
814 nsep->se_checked = 0;
815 goto skip;
Denis Vlasenko512a5452007-09-24 10:41:30 +0000816 }
817 if (hp->h_addrtype != AF_INET) {
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000818 bb_error_msg("%s: address isn't an Internet "
819 "address", nsep->se_hostaddr);
820 nsep->se_checked = 0;
821 goto skip;
Denis Vlasenko512a5452007-09-24 10:41:30 +0000822 }
823 i = 1;
824 memmove(&nsep->se_ctrladdr_in.sin_addr,
825 hp->h_addr_list[0], sizeof(struct in_addr));
826 while (hp->h_addr_list[i] != NULL) {
827 psep = dupconfig(nsep);
828 psep->se_hostaddr = xxstrdup(nsep->se_hostaddr);
829 psep->se_checked = 1;
830 memmove(&psep->se_ctrladdr_in.sin_addr,
831 hp->h_addr_list[i], sizeof(struct in_addr));
832 psep->se_ctrladdr_size = sizeof(psep->se_ctrladdr_in);
833 i++;
834 /* Prepend to list, don't want to look up */
835 /* its hostname again. */
836 psep->se_next = sep;
837 sep = psep;
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000838 }
839 }
840 }
841/* XXX BUG?: is this skip: label supposed to remain? */
Denis Vlasenkoce074df2007-03-24 12:07:31 +0000842 skip:
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000843 nsep = nsep->se_next;
844 }
845
846 /*
847 * Finally, free any entries which failed the gethostbyname
848 * check.
849 */
850 psep = NULL;
851 nsep = sep;
852 while (nsep != NULL) {
853 servtab_t *tsep;
854
855 if (nsep->se_checked == 0) {
856 tsep = nsep;
857 if (psep == NULL) {
858 sep = nsep->se_next;
859 nsep = sep;
860 } else {
861 nsep = nsep->se_next;
862 psep->se_next = nsep;
863 }
864 freeconfig(tsep);
865 } else {
866 nsep->se_checked = 0;
867 psep = nsep;
868 nsep = nsep->se_next;
869 }
870 }
871
872 return sep;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000873}
874
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000875#define Block_Using_Signals(m) do { \
876 sigemptyset(&m); \
877 sigaddset(&m, SIGCHLD); \
878 sigaddset(&m, SIGHUP); \
879 sigaddset(&m, SIGALRM); \
880 sigprocmask(SIG_BLOCK, &m, NULL); \
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000881} while (0)
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000882
883static servtab_t *enter(servtab_t *cp)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000884{
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000885 servtab_t *sep;
886 sigset_t omask;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000887
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000888 sep = new_servtab();
889 *sep = *cp;
890 sep->se_fd = -1;
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000891#if ENABLE_FEATURE_INETD_RPC
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000892 sep->se_rpcprog = -1;
893#endif
894 Block_Using_Signals(omask);
895 sep->se_next = servtab;
896 servtab = sep;
897 sigprocmask(SIG_UNBLOCK, &omask, NULL);
898 return sep;
899}
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000900
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000901static int matchconf(servtab_t *old, servtab_t *new)
902{
903 if (strcmp(old->se_service, new->se_service) != 0)
904 return 0;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000905
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000906 if (strcmp(old->se_hostaddr, new->se_hostaddr) != 0)
907 return 0;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000908
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000909 if (strcmp(old->se_proto, new->se_proto) != 0)
910 return 0;
911
912 /*
913 * If the new servtab is bound to a specific address, check that the
914 * old servtab is bound to the same entry. If the new service is not
915 * bound to a specific address then the check of se_hostaddr above
916 * is sufficient.
917 */
918
919 if (old->se_family == AF_INET && new->se_family == AF_INET &&
920 memcmp(&old->se_ctrladdr_in.sin_addr,
921 &new->se_ctrladdr_in.sin_addr,
922 sizeof(new->se_ctrladdr_in.sin_addr)) != 0)
923 return 0;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000924
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000925#if ENABLE_FEATURE_IPV6
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000926 if (old->se_family == AF_INET6 && new->se_family == AF_INET6 &&
927 memcmp(&old->se_ctrladdr_in6.sin6_addr,
928 &new->se_ctrladdr_in6.sin6_addr,
929 sizeof(new->se_ctrladdr_in6.sin6_addr)) != 0)
930 return 0;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000931#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000932 return 1;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000933}
934
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000935static void config(int sig ATTRIBUTE_UNUSED)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000936{
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000937 servtab_t *sep, *cp, **sepp;
938 sigset_t omask;
939 size_t n;
940 char protoname[10];
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000941
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000942 if (!setconfig()) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000943 bb_simple_perror_msg(config_filename);
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000944 return;
945 }
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000946 for (sep = servtab; sep; sep = sep->se_next)
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000947 sep->se_checked = 0;
948 cp = getconfigent();
949 while (cp != NULL) {
950 for (sep = servtab; sep; sep = sep->se_next)
951 if (matchconf(sep, cp))
952 break;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000953
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000954 if (sep != 0) {
955 int i;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000956
Mike Frysinger23fedb32005-10-05 00:50:03 +0000957#define SWAP(type, a, b) do {type c=(type)a; a=(type)b; b=(type)c;} while (0)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000958
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000959 Block_Using_Signals(omask);
960 /*
961 * sep->se_wait may be holding the pid of a daemon
962 * that we're waiting for. If so, don't overwrite
963 * it unless the config file explicitly says don't
964 * wait.
965 */
966 if (
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000967#ifdef INETD_FEATURE_ENABLED
Denis Vlasenko13858992006-10-08 12:49:22 +0000968 cp->se_bi == 0 &&
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000969#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000970 (sep->se_wait == 1 || cp->se_wait == 0))
971 sep->se_wait = cp->se_wait;
972 SWAP(int, cp->se_max, sep->se_max);
973 SWAP(char *, sep->se_user, cp->se_user);
974 SWAP(char *, sep->se_group, cp->se_group);
975 SWAP(char *, sep->se_server, cp->se_server);
976 for (i = 0; i < MAXARGV; i++)
977 SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000978#undef SWAP
979
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +0000980#if ENABLE_FEATURE_INETD_RPC
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000981 if (isrpcservice(sep))
982 unregister_rpc(sep);
983 sep->se_rpcversl = cp->se_rpcversl;
984 sep->se_rpcversh = cp->se_rpcversh;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000985#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000986 sigprocmask(SIG_UNBLOCK, &omask, NULL);
987 freeconfig(cp);
988 } else {
989 sep = enter(cp);
990 }
991 sep->se_checked = 1;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +0000992
Denis Vlasenkoec17d432006-09-23 15:18:38 +0000993 switch (sep->se_family) {
994 case AF_UNIX:
995 if (sep->se_fd != -1)
996 break;
997 (void) unlink(sep->se_service);
998 n = strlen(sep->se_service);
999 if (n > sizeof sep->se_ctrladdr_un.sun_path - 1)
1000 n = sizeof sep->se_ctrladdr_un.sun_path - 1;
1001 safe_strncpy(sep->se_ctrladdr_un.sun_path, sep->se_service, n + 1);
1002 sep->se_ctrladdr_un.sun_family = AF_UNIX;
1003 sep->se_ctrladdr_size = n + sizeof sep->se_ctrladdr_un.sun_family;
1004 setup(sep);
1005 break;
1006 case AF_INET:
1007 sep->se_ctrladdr_in.sin_family = AF_INET;
1008 /* se_ctrladdr_in was set in getconfigent */
1009 sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001010
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001011#if ENABLE_FEATURE_INETD_RPC
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001012 if (isrpcservice(sep)) {
1013 struct rpcent *rp;
Denis Vlasenko13858992006-10-08 12:49:22 +00001014 // FIXME: atoi_or_else(str, 0) would be handy here
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001015 sep->se_rpcprog = atoi(sep->se_service);
1016 if (sep->se_rpcprog == 0) {
1017 rp = getrpcbyname(sep->se_service);
1018 if (rp == 0) {
1019 bb_error_msg("%s: unknown rpc service", sep->se_service);
1020 goto serv_unknown;
1021 }
1022 sep->se_rpcprog = rp->r_number;
1023 }
1024 if (sep->se_fd == -1)
1025 setup(sep);
1026 if (sep->se_fd != -1)
1027 register_rpc(sep);
1028 } else
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001029#endif
Denis Vlasenko13858992006-10-08 12:49:22 +00001030 {
Denis Vlasenko28703012006-12-19 20:32:02 +00001031 uint16_t port = htons(atoi(sep->se_service));
Denis Vlasenko13858992006-10-08 12:49:22 +00001032 // FIXME: atoi_or_else(str, 0) would be handy here
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001033 if (!port) {
1034 /*XXX*/ strncpy(protoname, sep->se_proto, sizeof(protoname));
1035 if (isdigit(protoname[strlen(protoname) - 1]))
1036 protoname[strlen(protoname) - 1] = '\0';
1037 sp = getservbyname(sep->se_service, protoname);
1038 if (sp == 0) {
1039 bb_error_msg("%s/%s: unknown service",
1040 sep->se_service, sep->se_proto);
1041 goto serv_unknown;
1042 }
1043 port = sp->s_port;
1044 }
1045 if (port != sep->se_ctrladdr_in.sin_port) {
1046 sep->se_ctrladdr_in.sin_port = port;
1047 if (sep->se_fd != -1) {
1048 FD_CLR(sep->se_fd, &allsock);
1049 nsock--;
1050 (void) close(sep->se_fd);
1051 }
1052 sep->se_fd = -1;
1053 }
1054 if (sep->se_fd == -1)
1055 setup(sep);
1056 }
1057 break;
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001058#if ENABLE_FEATURE_IPV6
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001059 case AF_INET6:
1060 sep->se_ctrladdr_in6.sin6_family = AF_INET6;
1061 /* se_ctrladdr_in was set in getconfigent */
1062 sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in6;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001063
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001064#if ENABLE_FEATURE_INETD_RPC
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001065 if (isrpcservice(sep)) {
1066 struct rpcent *rp;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001067
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001068 sep->se_rpcprog = atoi(sep->se_service);
1069 if (sep->se_rpcprog == 0) {
1070 rp = getrpcbyname(sep->se_service);
1071 if (rp == 0) {
1072 bb_error_msg("%s: unknown rpc service", sep->se_service);
1073 goto serv_unknown;
1074 }
1075 sep->se_rpcprog = rp->r_number;
1076 }
1077 if (sep->se_fd == -1)
1078 setup(sep);
1079 if (sep->se_fd != -1)
1080 register_rpc(sep);
1081 } else
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001082#endif
Denis Vlasenko28703012006-12-19 20:32:02 +00001083 {
1084 uint16_t port = htons(atoi(sep->se_service));
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001085
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001086 if (!port) {
1087 /*XXX*/ strncpy(protoname, sep->se_proto, sizeof(protoname));
1088 if (isdigit(protoname[strlen(protoname) - 1]))
1089 protoname[strlen(protoname) - 1] = '\0';
1090 sp = getservbyname(sep->se_service, protoname);
1091 if (sp == 0) {
1092 bb_error_msg("%s/%s: unknown service",
1093 sep->se_service, sep->se_proto);
1094 goto serv_unknown;
1095 }
1096 port = sp->s_port;
1097 }
1098 if (port != sep->se_ctrladdr_in6.sin6_port) {
1099 sep->se_ctrladdr_in6.sin6_port = port;
1100 if (sep->se_fd != -1) {
1101 FD_CLR(sep->se_fd, &allsock);
1102 nsock--;
1103 (void) close(sep->se_fd);
1104 }
1105 sep->se_fd = -1;
1106 }
1107 if (sep->se_fd == -1)
1108 setup(sep);
1109 }
1110 break;
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001111#endif /* FEATURE_IPV6 */
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001112 }
Denis Vlasenkoce074df2007-03-24 12:07:31 +00001113 serv_unknown:
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001114 if (cp->se_next != NULL) {
1115 servtab_t *tmp = cp;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001116
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001117 cp = cp->se_next;
1118 free(tmp);
1119 } else {
1120 free(cp);
1121 cp = getconfigent();
1122 }
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001123 }
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001124 endconfig();
1125 /*
1126 * Purge anything not looked at above.
1127 */
1128 Block_Using_Signals(omask);
1129 sepp = &servtab;
1130 while ((sep = *sepp)) {
1131 if (sep->se_checked) {
1132 sepp = &sep->se_next;
1133 continue;
1134 }
1135 *sepp = sep->se_next;
1136 if (sep->se_fd != -1) {
1137 FD_CLR(sep->se_fd, &allsock);
1138 nsock--;
1139 (void) close(sep->se_fd);
1140 }
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001141#if ENABLE_FEATURE_INETD_RPC
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001142 if (isrpcservice(sep))
1143 unregister_rpc(sep);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001144#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001145 if (sep->se_family == AF_UNIX)
1146 (void) unlink(sep->se_service);
1147 freeconfig(sep);
1148 free(sep);
1149 }
1150 sigprocmask(SIG_UNBLOCK, &omask, NULL);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001151}
1152
1153
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001154static void reapchild(int sig ATTRIBUTE_UNUSED)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001155{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001156 pid_t pid;
1157 int save_errno = errno, status;
1158 servtab_t *sep;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001159
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001160 for (;;) {
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001161 pid = wait_any_nohang(&status);
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001162 if (pid <= 0)
1163 break;
1164 for (sep = servtab; sep; sep = sep->se_next)
1165 if (sep->se_wait == pid) {
1166 if (WIFEXITED(status) && WEXITSTATUS(status))
1167 bb_error_msg("%s: exit status 0x%x",
1168 sep->se_server, WEXITSTATUS(status));
1169 else if (WIFSIGNALED(status))
1170 bb_error_msg("%s: exit signal 0x%x",
1171 sep->se_server, WTERMSIG(status));
1172 sep->se_wait = 1;
1173 FD_SET(sep->se_fd, &allsock);
1174 nsock++;
1175 }
1176 }
1177 errno = save_errno;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001178}
1179
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001180static void retry(int sig ATTRIBUTE_UNUSED)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001181{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001182 servtab_t *sep;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001183
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001184 timingout = 0;
1185 for (sep = servtab; sep; sep = sep->se_next) {
1186 if (sep->se_fd == -1) {
1187 switch (sep->se_family) {
1188 case AF_UNIX:
1189 case AF_INET:
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001190#if ENABLE_FEATURE_IPV6
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001191 case AF_INET6:
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001192#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001193 setup(sep);
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001194#if ENABLE_FEATURE_INETD_RPC
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001195 if (sep->se_fd != -1 && isrpcservice(sep))
1196 register_rpc(sep);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001197#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001198 break;
1199 }
1200 }
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001201 }
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001202}
1203
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001204static void goaway(int sig ATTRIBUTE_UNUSED)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001205{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001206 servtab_t *sep;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001207
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001208 /* XXX signal race walking sep list */
1209 for (sep = servtab; sep; sep = sep->se_next) {
1210 if (sep->se_fd == -1)
1211 continue;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001212
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001213 switch (sep->se_family) {
1214 case AF_UNIX:
1215 (void) unlink(sep->se_service);
1216 break;
1217 case AF_INET:
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001218#if ENABLE_FEATURE_IPV6
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001219 case AF_INET6:
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001220#endif
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001221#if ENABLE_FEATURE_INETD_RPC
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001222 if (sep->se_wait == 1 && isrpcservice(sep))
1223 unregister_rpc(sep); /* XXX signal race */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001224#endif
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001225 break;
1226 }
1227 (void) close(sep->se_fd);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001228 }
Denis Vlasenko10457b92007-03-27 22:01:31 +00001229 remove_pidfile(_PATH_INETDPID);
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001230 exit(0);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001231}
1232
1233
1234#ifdef INETD_SETPROCTITLE
Glenn L McGrath06e95652003-02-09 06:51:14 +00001235
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001236static void
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001237inetd_setproctitle(char *a, int s)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001238{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001239 socklen_t size;
1240 char *cp;
1241 struct sockaddr_in prt_sin;
1242 char buf[80];
Glenn L McGrath06e95652003-02-09 06:51:14 +00001243
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001244 cp = Argv[0];
1245 size = sizeof(prt_sin);
1246 (void) snprintf(buf, sizeof buf, "-%s", a);
1247 if (getpeername(s, (struct sockaddr *) &prt_sin, &size) == 0) {
1248 char *sa = inet_ntoa(prt_sin.sin_addr);
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001249
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001250 buf[sizeof(buf) - 1 - strlen(sa) - 3] = '\0';
1251 strcat(buf, " [");
1252 strcat(buf, sa);
1253 strcat(buf, "]");
1254 }
1255 strncpy(cp, buf, LastArg - cp);
1256 cp += strlen(cp);
1257 while (cp < LastArg)
1258 *cp++ = ' ';
Glenn L McGrath06e95652003-02-09 06:51:14 +00001259}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001260#endif
1261
Glenn L McGrath06e95652003-02-09 06:51:14 +00001262
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001263int inetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +00001264int inetd_main(int argc, char **argv)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001265{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001266 servtab_t *sep;
1267 struct passwd *pwd;
1268 struct group *grp = NULL;
1269 int tmpint;
1270 struct sigaction sa, sapipe;
1271 int opt;
1272 pid_t pid;
1273 char buf[50];
1274 char *stoomany;
1275 sigset_t omask, wait_mask;
Denis Vlasenko512a5452007-09-24 10:41:30 +00001276#ifdef INETD_SETPROCTITLE
1277 char **envp;
1278#endif
1279
1280 INIT_G();
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001281
1282#ifdef INETD_SETPROCTITLE
Denis Vlasenko512a5452007-09-24 10:41:30 +00001283 envp = environ;
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001284 Argv = argv;
1285 if (envp == 0 || *envp == 0)
1286 envp = argv;
1287 while (*envp)
1288 envp++;
1289 LastArg = envp[-1] + strlen(envp[-1]);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001290#endif
1291
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001292 uid = getuid();
1293 if (uid != 0)
Denis Vlasenko55f30b02007-03-24 22:42:29 +00001294 config_filename = NULL;
Denis Vlasenko5a142022007-03-26 13:20:54 +00001295
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001296 opt = getopt32(argv, "R:f", &stoomany);
Denis Vlasenko5a142022007-03-26 13:20:54 +00001297 if (opt & 1)
1298 toomany = xatoi_u(stoomany);
1299 argv += optind;
1300 argc -= optind;
1301 if (argc)
Denis Vlasenko55f30b02007-03-24 22:42:29 +00001302 config_filename = argv[0];
1303 if (config_filename == NULL)
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001304 bb_error_msg_and_die("non-root must specify a config file");
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001305
Denis Vlasenko5a142022007-03-26 13:20:54 +00001306 if (!(opt & 2))
1307 bb_daemonize_or_rexec(0, argv - optind);
1308 else
1309 bb_sanitize_stdio();
Denis Vlasenkoffcef2d2007-01-14 02:03:28 +00001310 openlog(applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001311 logmode = LOGMODE_SYSLOG;
Eric Andersen35e643b2003-07-28 07:40:39 +00001312
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001313 if (uid == 0) {
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001314 /* If run by hand, ensure groups vector gets trashed */
Denis Vlasenko7a431b32007-01-14 01:29:06 +00001315 gid_t gid = getgid();
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001316 setgroups(1, &gid);
1317 }
1318
Denis Vlasenko10457b92007-03-27 22:01:31 +00001319 write_pidfile(_PATH_INETDPID);
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001320
1321 if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
1322 bb_perror_msg("getrlimit");
1323 } else {
1324 rlim_ofile_cur = rlim_ofile.rlim_cur;
1325 if (rlim_ofile_cur == RLIM_INFINITY) /* ! */
1326 rlim_ofile_cur = OPEN_MAX;
1327 }
1328
1329 memset((char *) &sa, 0, sizeof(sa));
1330 sigemptyset(&sa.sa_mask);
1331 sigaddset(&sa.sa_mask, SIGALRM);
1332 sigaddset(&sa.sa_mask, SIGCHLD);
1333 sigaddset(&sa.sa_mask, SIGHUP);
1334 sa.sa_handler = retry;
1335 sigaction(SIGALRM, &sa, NULL);
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001336 config(SIGHUP);
1337 sa.sa_handler = config;
1338 sigaction(SIGHUP, &sa, NULL);
1339 sa.sa_handler = reapchild;
1340 sigaction(SIGCHLD, &sa, NULL);
1341 sa.sa_handler = goaway;
1342 sigaction(SIGTERM, &sa, NULL);
1343 sa.sa_handler = goaway;
1344 sigaction(SIGINT, &sa, NULL);
1345 sa.sa_handler = SIG_IGN;
1346 sigaction(SIGPIPE, &sa, &sapipe);
1347 memset(&wait_mask, 0, sizeof(wait_mask));
1348 {
1349 /* space for daemons to overwrite environment for ps */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001350#define DUMMYSIZE 100
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001351 char dummy[DUMMYSIZE];
Glenn L McGrath06e95652003-02-09 06:51:14 +00001352
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001353 (void) memset(dummy, 'x', DUMMYSIZE - 1);
1354 dummy[DUMMYSIZE - 1] = '\0';
Glenn L McGrath06e95652003-02-09 06:51:14 +00001355
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001356 (void) setenv("inetd_dummy", dummy, 1);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001357 }
1358
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001359 for (;;) {
1360 int n, ctrl = -1;
1361 fd_set readable;
1362
1363 if (nsock == 0) {
1364 Block_Using_Signals(omask);
1365 while (nsock == 0)
1366 sigsuspend(&wait_mask);
1367 sigprocmask(SIG_UNBLOCK, &omask, NULL);
1368 }
1369
1370 readable = allsock;
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001371 n = select(maxsock + 1, &readable, NULL, NULL, NULL);
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001372 if (n <= 0) {
1373 if (n < 0 && errno != EINTR) {
1374 bb_perror_msg("select");
1375 sleep(1);
1376 }
Glenn L McGrath82d42db2004-02-18 13:12:53 +00001377 continue;
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001378 }
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001379
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001380 for (sep = servtab; n && sep; sep = sep->se_next) {
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001381 if (sep->se_fd == -1 || !FD_ISSET(sep->se_fd, &readable))
1382 continue;
1383
1384 n--;
1385 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
1386 ctrl = accept(sep->se_fd, NULL, NULL);
1387 if (ctrl < 0) {
1388 if (errno == EINTR)
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001389 continue;
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001390 bb_perror_msg("accept (for %s)", sep->se_service);
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001391 continue;
1392 }
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001393 if (sep->se_family == AF_INET && sep->se_socktype == SOCK_STREAM) {
1394 struct sockaddr_in peer;
1395 socklen_t plen = sizeof(peer);
1396
1397 if (getpeername(ctrl, (struct sockaddr *) &peer, &plen) < 0) {
Denis Vlasenkoea620772006-10-14 02:23:43 +00001398 bb_error_msg("cannot getpeername");
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001399 close(ctrl);
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001400 continue;
1401 }
1402 if (ntohs(peer.sin_port) == 20) {
1403 /* XXX ftp bounce */
1404 close(ctrl);
1405 continue;
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001406 }
1407 }
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001408 } else
1409 ctrl = sep->se_fd;
1410
1411 Block_Using_Signals(omask);
1412 pid = 0;
1413#ifdef INETD_FEATURE_ENABLED
1414 if (sep->se_bi == 0 || sep->se_bi->bi_fork)
1415#endif
1416 {
1417 if (sep->se_count++ == 0)
1418 (void) gettimeofday(&sep->se_time, NULL);
1419 else if (toomany > 0 && sep->se_count >= sep->se_max) {
1420 struct timeval now;
1421
1422 (void) gettimeofday(&now, NULL);
1423 if (now.tv_sec - sep->se_time.tv_sec > CNT_INTVL) {
1424 sep->se_time = now;
1425 sep->se_count = 1;
1426 } else {
1427 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
1428 close(ctrl);
1429 if (sep->se_family == AF_INET &&
1430 ntohs(sep->se_ctrladdr_in.sin_port) >= IPPORT_RESERVED) {
1431 /*
1432 * Cannot close it -- there are
1433 * thieves on the system.
1434 * Simply ignore the connection.
1435 */
1436 --sep->se_count;
1437 continue;
1438 }
1439 bb_error_msg("%s/%s server failing (looping), service terminated",
1440 sep->se_service, sep->se_proto);
1441 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
1442 close(ctrl);
1443 FD_CLR(sep->se_fd, &allsock);
1444 (void) close(sep->se_fd);
1445 sep->se_fd = -1;
1446 sep->se_count = 0;
1447 nsock--;
1448 sigprocmask(SIG_UNBLOCK, &omask, NULL);
1449 if (!timingout) {
1450 timingout = 1;
1451 alarm(RETRYTIME);
1452 }
1453 continue;
1454 }
1455 }
1456 pid = fork();
1457 }
1458 if (pid < 0) {
1459 bb_perror_msg("fork");
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001460 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
1461 close(ctrl);
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001462 sigprocmask(SIG_UNBLOCK, &omask, NULL);
1463 sleep(1);
1464 continue;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001465 }
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001466 if (pid && sep->se_wait) {
1467 sep->se_wait = pid;
1468 FD_CLR(sep->se_fd, &allsock);
1469 nsock--;
1470 }
1471 sigprocmask(SIG_UNBLOCK, &omask, NULL);
1472 if (pid == 0) {
1473#ifdef INETD_FEATURE_ENABLED
1474 if (sep->se_bi) {
1475 (*sep->se_bi->bi_fn)(ctrl, sep);
1476 } else
1477#endif
Denis Vlasenkob64eed62007-01-14 17:06:11 +00001478 {
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001479 pwd = getpwnam(sep->se_user);
1480 if (pwd == NULL) {
1481 bb_error_msg("getpwnam: %s: no such user", sep->se_user);
1482 goto do_exit1;
1483 }
1484 if (setsid() < 0)
1485 bb_perror_msg("%s: setsid", sep->se_service);
1486 if (sep->se_group && (grp = getgrnam(sep->se_group)) == NULL) {
1487 bb_error_msg("getgrnam: %s: no such group", sep->se_group);
1488 goto do_exit1;
1489 }
1490 if (uid != 0) {
1491 /* a user running private inetd */
1492 if (uid != pwd->pw_uid)
1493 _exit(1);
1494 } else if (pwd->pw_uid) {
1495 if (sep->se_group)
1496 pwd->pw_gid = grp->gr_gid;
1497 xsetgid((gid_t) pwd->pw_gid);
1498 initgroups(pwd->pw_name, pwd->pw_gid);
1499 xsetuid((uid_t) pwd->pw_uid);
1500 } else if (sep->se_group) {
1501 xsetgid(grp->gr_gid);
1502 setgroups(1, &grp->gr_gid);
1503 }
1504 dup2(ctrl, 0);
1505 if (ctrl) close(ctrl);
1506 dup2(0, 1);
1507 dup2(0, 2);
1508 if (rlim_ofile.rlim_cur != rlim_ofile_cur)
1509 if (setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0)
1510 bb_perror_msg("setrlimit");
1511 closelog();
1512 for (tmpint = rlim_ofile_cur - 1; --tmpint > 2;)
1513 (void) close(tmpint);
1514 sigaction(SIGPIPE, &sapipe, NULL);
1515 execv(sep->se_server, sep->se_argv);
1516 bb_perror_msg("execv %s", sep->se_server);
Denis Vlasenkoce074df2007-03-24 12:07:31 +00001517 do_exit1:
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001518 if (sep->se_socktype != SOCK_STREAM)
1519 recv(0, buf, sizeof(buf), 0);
1520 _exit(1);
1521 }
1522 }
1523 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
1524 close(ctrl);
1525 } /* for (sep = servtab...) */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001526 } /* for (;;) */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001527}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001528
1529/*
1530 * Internet services provided internally by inetd:
1531 */
1532#define BUFSIZE 4096
1533
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001534#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \
1535 ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN || \
1536 ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001537static int dg_badinput(struct sockaddr_in *dg_sin)
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001538{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001539 if (ntohs(dg_sin->sin_port) < IPPORT_RESERVED)
1540 return 1;
1541 if (dg_sin->sin_addr.s_addr == htonl(INADDR_BROADCAST))
1542 return 1;
1543 /* XXX compare against broadcast addresses in SIOCGIFCONF list? */
1544 return 0;
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001545}
1546#endif
1547
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001548#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
Glenn L McGrath06e95652003-02-09 06:51:14 +00001549/* Echo service -- echo data back */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001550/* ARGSUSED */
1551static void
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001552echo_stream(int s, servtab_t *sep)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001553{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001554 char buffer[BUFSIZE];
1555 int i;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001556
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001557 inetd_setproctitle(sep->se_service, s);
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001558 while (1) {
1559 i = read(s, buffer, sizeof(buffer));
1560 if (i <= 0) break;
1561 /* FIXME: this isnt correct - safe_write()? */
1562 if (write(s, buffer, i) <= 0) break;
1563 }
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001564 exit(0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001565}
1566
1567/* Echo service -- echo data back */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001568/* ARGSUSED */
1569static void
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001570echo_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001571{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001572 char buffer[BUFSIZE];
1573 int i;
1574 socklen_t size;
1575 /* struct sockaddr_storage ss; */
1576 struct sockaddr sa;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001577
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001578 size = sizeof(sa);
1579 i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size);
1580 if (i < 0)
1581 return;
1582 if (dg_badinput((struct sockaddr_in *) &sa))
1583 return;
1584 (void) sendto(s, buffer, i, 0, &sa, sizeof(sa));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001585}
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001586#endif /* FEATURE_INETD_SUPPORT_BUILTIN_ECHO */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001587
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001588#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
Glenn L McGrath06e95652003-02-09 06:51:14 +00001589/* Discard service -- ignore data */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001590/* ARGSUSED */
1591static void
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001592discard_stream(int s, servtab_t *sep)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001593{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001594 char buffer[BUFSIZE];
Glenn L McGrath06e95652003-02-09 06:51:14 +00001595
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001596 inetd_setproctitle(sep->se_service, s);
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001597 while (1) {
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00001598 if (safe_read(s, buffer, sizeof(buffer)) <= 0)
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001599 exit(0);
1600 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001601}
1602
1603/* Discard service -- ignore data */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001604/* ARGSUSED */
1605static void
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001606discard_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001607{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001608 char buffer[BUFSIZE];
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001609
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001610 (void) read(s, buffer, sizeof(buffer));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001611}
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001612#endif /* FEATURE_INETD_SUPPORT_BUILTIN_DISCARD */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001613
1614
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001615#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
Glenn L McGrath06e95652003-02-09 06:51:14 +00001616#define LINESIZ 72
Glenn L McGrath06e95652003-02-09 06:51:14 +00001617
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001618static void
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001619initring(void)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001620{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001621 int i;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001622
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001623 endring = ring;
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001624 for (i = 0; i <= 128; ++i)
1625 if (isprint(i))
1626 *endring++ = i;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001627}
1628
1629/* Character generator */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001630/* ARGSUSED */
1631static void
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001632chargen_stream(int s, servtab_t *sep)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001633{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001634 char *rs;
1635 int len;
1636 char text[LINESIZ + 2];
Glenn L McGrath06e95652003-02-09 06:51:14 +00001637
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001638 inetd_setproctitle(sep->se_service, s);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001639
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001640 if (!endring) {
1641 initring();
1642 rs = ring;
1643 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001644
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001645 text[LINESIZ] = '\r';
1646 text[LINESIZ + 1] = '\n';
Denis Vlasenkoc1876d72006-09-23 15:58:01 +00001647 rs = ring;
1648 for (;;) {
1649 len = endring - rs;
1650 if (len >= LINESIZ)
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001651 memmove(text, rs, LINESIZ);
1652 else {
1653 memmove(text, rs, len);
1654 memmove(text + len, ring, LINESIZ - len);
1655 }
1656 if (++rs == endring)
1657 rs = ring;
1658 if (write(s, text, sizeof(text)) != sizeof(text))
1659 break;
1660 }
1661 exit(0);
1662}
1663
1664/* Character generator */
1665/* ARGSUSED */
1666static void
1667chargen_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
1668{
1669 /* struct sockaddr_storage ss; */
1670 struct sockaddr sa;
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001671 int len;
1672 char text[LINESIZ + 2];
1673 socklen_t size;
1674
Denis Vlasenko512a5452007-09-24 10:41:30 +00001675 if (!endring) {
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001676 initring();
Denis Vlasenko512a5452007-09-24 10:41:30 +00001677 ringpos = ring;
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001678 }
1679
1680 size = sizeof(sa);
1681 if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0)
1682 return;
1683 if (dg_badinput((struct sockaddr_in *) &sa))
1684 return;
1685
Denis Vlasenko512a5452007-09-24 10:41:30 +00001686 len = endring - ringpos;
1687 if (len >= LINESIZ)
1688 memmove(text, ringpos, LINESIZ);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001689 else {
Denis Vlasenko512a5452007-09-24 10:41:30 +00001690 memmove(text, ringpos, len);
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001691 memmove(text + len, ring, LINESIZ - len);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001692 }
Denis Vlasenko512a5452007-09-24 10:41:30 +00001693 if (++ringpos == endring)
1694 ringpos = ring;
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001695 text[LINESIZ] = '\r';
1696 text[LINESIZ + 1] = '\n';
1697 (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001698}
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001699#endif /* FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001700
1701
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001702#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME
Glenn L McGrath06e95652003-02-09 06:51:14 +00001703/*
1704 * Return a machine readable date and time, in the form of the
1705 * number of seconds since midnight, Jan 1, 1900. Since gettimeofday
1706 * returns the number of seconds since midnight, Jan 1, 1970,
1707 * we must add 2208988800 seconds to this figure to make up for
1708 * some seventy years Bell Labs was asleep.
1709 */
1710
Denis Vlasenko35d4da02007-01-22 14:04:27 +00001711static unsigned machtime(void)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001712{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001713 struct timeval tv;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001714
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001715 if (gettimeofday(&tv, NULL) < 0) {
1716 fprintf(stderr, "Unable to get time of day\n");
1717 return 0L;
1718 }
Denis Vlasenko35d4da02007-01-22 14:04:27 +00001719 return htonl((unsigned) tv.tv_sec + 2208988800UL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001720}
1721
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001722/* ARGSUSED */
1723static void
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001724machtime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001725{
Denis Vlasenko35d4da02007-01-22 14:04:27 +00001726 unsigned result;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001727
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001728 result = machtime();
1729 (void) write(s, (char *) &result, sizeof(result));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001730}
1731
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001732/* ARGSUSED */
1733static void
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001734machtime_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001735{
Denis Vlasenko35d4da02007-01-22 14:04:27 +00001736 unsigned result;
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001737 /* struct sockaddr_storage ss; */
1738 struct sockaddr sa;
1739 struct sockaddr_in *dg_sin;
1740 socklen_t size;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001741
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001742 size = sizeof(sa);
1743 if (recvfrom(s, (char *) &result, sizeof(result), 0, &sa, &size) < 0)
1744 return;
1745 /* if (dg_badinput((struct sockaddr *)&ss)) */
1746 dg_sin = (struct sockaddr_in *) &sa;
1747 if (dg_sin->sin_addr.s_addr == htonl(INADDR_BROADCAST) ||
1748 ntohs(dg_sin->sin_port) < IPPORT_RESERVED / 2)
1749 return;
1750 result = machtime();
1751 (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001752}
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001753#endif /* FEATURE_INETD_SUPPORT_BUILTIN_TIME */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001754
1755
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001756#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
Glenn L McGrath06e95652003-02-09 06:51:14 +00001757/* Return human-readable time of day */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001758/* ARGSUSED */
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001759static void daytime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001760{
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001761 char buffer[32];
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001762 time_t t;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001763
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001764 t = time(NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001765
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001766// fdprintf instead?
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001767 (void) sprintf(buffer, "%.24s\r\n", ctime(&t));
1768 (void) write(s, buffer, strlen(buffer));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001769}
1770
1771/* Return human-readable time of day */
"Vladimir N. Oleynik"1a2f4d92005-10-03 08:08:58 +00001772/* ARGSUSED */
1773void
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001774daytime_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001775{
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001776 char buffer[256];
1777 time_t t;
1778 /* struct sockaddr_storage ss; */
1779 struct sockaddr sa;
1780 socklen_t size;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001781
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001782 t = time(NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001783
Denis Vlasenkoec17d432006-09-23 15:18:38 +00001784 size = sizeof(sa);
1785 if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0)
1786 return;
1787 if (dg_badinput((struct sockaddr_in *) &sa))
1788 return;
1789 (void) sprintf(buffer, "%.24s\r\n", ctime(&t));
1790 (void) sendto(s, buffer, strlen(buffer), 0, &sa, sizeof(sa));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001791}
Denis Vlasenkod1a6e8d2007-01-14 14:46:18 +00001792#endif /* FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME */