inetd.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. /* vi: set sw=4 ts=4: */
  2. /* $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> */
  6. /*
  7. * Copyright (c) 1983,1991 The Regents of the University of California.
  8. * All rights reserved.
  9. *
  10. * 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.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
  27. * 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.
  37. */
  38. /* Inetd - Internet super-server
  39. *
  40. * This program invokes all internet services as needed.
  41. * connection-oriented services are invoked each time a
  42. * connection is made, by creating a process. This process
  43. * is passed the connection as file descriptor 0 and is
  44. * expected to do a getpeername to find out the source host
  45. * and port.
  46. *
  47. * Datagram oriented services are invoked when a datagram
  48. * arrives; a process is created and passed a pending message
  49. * on file descriptor 0. Datagram servers may either connect
  50. * to their peer, freeing up the original socket for inetd
  51. * to receive further messages on, or "take over the socket",
  52. * processing all arriving datagrams and, eventually, timing
  53. * out. The first type of server is said to be "multi-threaded";
  54. * the second type of server "single-threaded".
  55. *
  56. * Inetd uses a configuration file which is read at startup
  57. * and, possibly, at some later time in response to a hangup signal.
  58. * The configuration file is "free format" with fields given in the
  59. * order shown below. Continuation lines for an entry must begin with
  60. * a space or tab. All fields must be present in each entry.
  61. *
  62. * service name must be in /etc/services
  63. * socket type stream/dgram/raw/rdm/seqpacket
  64. * protocol must be in /etc/protocols
  65. * wait/nowait[.max] single-threaded/multi-threaded, max #
  66. * user[.group] or user[:group] user/group to run daemon as
  67. * server program full path name
  68. * server program arguments maximum of MAXARGS (20)
  69. *
  70. * For RPC services
  71. * service name/version must be in /etc/rpc
  72. * socket type stream/dgram/raw/rdm/seqpacket
  73. * protocol must be in /etc/protocols
  74. * wait/nowait[.max] single-threaded/multi-threaded
  75. * user[.group] or user[:group] user to run daemon as
  76. * server program full path name
  77. * server program arguments maximum of MAXARGS (20)
  78. *
  79. * For non-RPC services, the "service name" can be of the form
  80. * hostaddress:servicename, in which case the hostaddress is used
  81. * as the host portion of the address to listen on. If hostaddress
  82. * consists of a single `*' character, INADDR_ANY is used.
  83. *
  84. * A line can also consist of just
  85. * hostaddress:
  86. * where hostaddress is as in the preceding paragraph. Such a line must
  87. * have no further fields; the specified hostaddress is remembered and
  88. * used for all further lines that have no hostaddress specified,
  89. * until the next such line (or EOF). (This is why * is provided to
  90. * allow explicit specification of INADDR_ANY.) A line
  91. * *:
  92. * is implicitly in effect at the beginning of the file.
  93. *
  94. * The hostaddress specifier may (and often will) contain dots;
  95. * the service name must not.
  96. *
  97. * For RPC services, host-address specifiers are accepted and will
  98. * work to some extent; however, because of limitations in the
  99. * portmapper interface, it will not work to try to give more than
  100. * one line for any given RPC service, even if the host-address
  101. * specifiers are different.
  102. *
  103. * Comment lines are indicated by a `#' in column 1.
  104. */
  105. /* inetd rules for passing file descriptors to children
  106. * (http://www.freebsd.org/cgi/man.cgi?query=inetd):
  107. *
  108. * The wait/nowait entry specifies whether the server that is invoked by
  109. * inetd will take over the socket associated with the service access point,
  110. * and thus whether inetd should wait for the server to exit before listen-
  111. * ing for new service requests. Datagram servers must use "wait", as
  112. * they are always invoked with the original datagram socket bound to the
  113. * specified service address. These servers must read at least one datagram
  114. * from the socket before exiting. If a datagram server connects to its
  115. * peer, freeing the socket so inetd can receive further messages on the
  116. * socket, it is said to be a "multi-threaded" server; it should read one
  117. * datagram from the socket and create a new socket connected to the peer.
  118. * It should fork, and the parent should then exit to allow inetd to check
  119. * for new service requests to spawn new servers. Datagram servers which
  120. * process all incoming datagrams on a socket and eventually time out are
  121. * said to be "single-threaded". The comsat(8), (biff(1)) and talkd(8)
  122. * utilities are both examples of the latter type of datagram server. The
  123. * tftpd(8) utility is an example of a multi-threaded datagram server.
  124. *
  125. * Servers using stream sockets generally are multi-threaded and use the
  126. * "nowait" entry. Connection requests for these services are accepted by
  127. * inetd, and the server is given only the newly-accepted socket connected
  128. * to a client of the service. Most stream-based services operate in this
  129. * manner. Stream-based servers that use "wait" are started with the lis-
  130. * tening service socket, and must accept at least one connection request
  131. * before exiting. Such a server would normally accept and process incoming
  132. * connection requests until a timeout.
  133. */
  134. /* Here's the scoop concerning the user[.:]group feature:
  135. *
  136. * 1) set-group-option off.
  137. *
  138. * a) user = root: NO setuid() or setgid() is done
  139. *
  140. * b) other: setgid(primary group as found in passwd)
  141. * initgroups(name, primary group)
  142. * setuid()
  143. *
  144. * 2) set-group-option on.
  145. *
  146. * a) user = root: setgid(specified group)
  147. * NO initgroups()
  148. * NO setuid()
  149. *
  150. * b) other: setgid(specified group)
  151. * initgroups(name, specified group)
  152. * setuid()
  153. */
  154. #include "busybox.h"
  155. #include <syslog.h>
  156. #include <sys/un.h>
  157. //#define ENABLE_FEATURE_INETD_RPC 1
  158. //#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO 1
  159. //#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD 1
  160. //#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME 1
  161. //#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME 1
  162. //#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN 1
  163. //#define ENABLE_FEATURE_IPV6 1
  164. #if ENABLE_FEATURE_INETD_RPC
  165. #include <rpc/rpc.h>
  166. #include <rpc/pmap_clnt.h>
  167. #endif
  168. #define _PATH_INETDCONF "/etc/inetd.conf"
  169. #define _PATH_INETDPID "/var/run/inetd.pid"
  170. #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
  171. #define RETRYTIME (60*10) /* retry after bind or server fail */
  172. #ifndef RLIMIT_NOFILE
  173. #define RLIMIT_NOFILE RLIMIT_OFILE
  174. #endif
  175. #ifndef OPEN_MAX
  176. #define OPEN_MAX 64
  177. #endif
  178. /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
  179. #define FD_MARGIN 8
  180. static rlim_t rlim_ofile_cur = OPEN_MAX;
  181. static struct rlimit rlim_ofile;
  182. /* Check unsupporting builtin */
  183. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \
  184. ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD || \
  185. ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME || \
  186. ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME || \
  187. ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
  188. # define INETD_FEATURE_ENABLED
  189. #endif
  190. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \
  191. ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD || \
  192. ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
  193. # define INETD_SETPROCTITLE
  194. #endif
  195. typedef struct servtab {
  196. char *se_hostaddr; /* host address to listen on */
  197. char *se_service; /* name of service */
  198. int se_socktype; /* type of socket to use */
  199. int se_family; /* address family */
  200. char *se_proto; /* protocol used */
  201. #if ENABLE_FEATURE_INETD_RPC
  202. int se_rpcprog; /* rpc program number */
  203. int se_rpcversl; /* rpc program lowest version */
  204. int se_rpcversh; /* rpc program highest version */
  205. #define isrpcservice(sep) ((sep)->se_rpcversl != 0)
  206. #else
  207. #define isrpcservice(sep) 0
  208. #endif
  209. pid_t se_wait; /* single threaded server */
  210. short se_checked; /* looked at during merge */
  211. char *se_user; /* user name to run as */
  212. char *se_group; /* group name to run as */
  213. #ifdef INETD_FEATURE_ENABLED
  214. const struct builtin *se_bi; /* if built-in, description */
  215. #endif
  216. char *se_server; /* server program */
  217. #define MAXARGV 20
  218. char *se_argv[MAXARGV + 1]; /* program arguments */
  219. int se_fd; /* open descriptor */
  220. union {
  221. struct sockaddr se_un_ctrladdr;
  222. struct sockaddr_in se_un_ctrladdr_in;
  223. #if ENABLE_FEATURE_IPV6
  224. struct sockaddr_in6 se_un_ctrladdr_in6;
  225. #endif
  226. struct sockaddr_un se_un_ctrladdr_un;
  227. } se_un; /* bound address */
  228. #define se_ctrladdr se_un.se_un_ctrladdr
  229. #define se_ctrladdr_in se_un.se_un_ctrladdr_in
  230. #define se_ctrladdr_in6 se_un.se_un_ctrladdr_in6
  231. #define se_ctrladdr_un se_un.se_un_ctrladdr_un
  232. int se_ctrladdr_size;
  233. int se_max; /* max # of instances of this service */
  234. int se_count; /* number started since se_time */
  235. struct timeval se_time; /* start of se_count */
  236. struct servtab *se_next;
  237. } servtab_t;
  238. static servtab_t *servtab;
  239. #ifdef INETD_FEATURE_ENABLED
  240. struct builtin {
  241. const char *bi_service; /* internally provided service name */
  242. int bi_socktype; /* type of socket supported */
  243. short bi_fork; /* 1 if should fork before call */
  244. short bi_wait; /* 1 if should wait for child */
  245. void (*bi_fn) (int, servtab_t *);
  246. };
  247. /* Echo received data */
  248. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
  249. static void echo_stream(int, servtab_t *);
  250. static void echo_dg(int, servtab_t *);
  251. #endif
  252. /* Internet /dev/null */
  253. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
  254. static void discard_stream(int, servtab_t *);
  255. static void discard_dg(int, servtab_t *);
  256. #endif
  257. /* Return 32 bit time since 1900 */
  258. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME
  259. static void machtime_stream(int, servtab_t *);
  260. static void machtime_dg(int, servtab_t *);
  261. #endif
  262. /* Return human-readable time */
  263. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
  264. static void daytime_stream(int, servtab_t *);
  265. static void daytime_dg(int, servtab_t *);
  266. #endif
  267. /* Familiar character generator */
  268. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
  269. static void chargen_stream(int, servtab_t *);
  270. static void chargen_dg(int, servtab_t *);
  271. #endif
  272. static const struct builtin builtins[] = {
  273. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
  274. /* Echo received data */
  275. {"echo", SOCK_STREAM, 1, 0, echo_stream,},
  276. {"echo", SOCK_DGRAM, 0, 0, echo_dg,},
  277. #endif
  278. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
  279. /* Internet /dev/null */
  280. {"discard", SOCK_STREAM, 1, 0, discard_stream,},
  281. {"discard", SOCK_DGRAM, 0, 0, discard_dg,},
  282. #endif
  283. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME
  284. /* Return 32 bit time since 1900 */
  285. {"time", SOCK_STREAM, 0, 0, machtime_stream,},
  286. {"time", SOCK_DGRAM, 0, 0, machtime_dg,},
  287. #endif
  288. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
  289. /* Return human-readable time */
  290. {"daytime", SOCK_STREAM, 0, 0, daytime_stream,},
  291. {"daytime", SOCK_DGRAM, 0, 0, daytime_dg,},
  292. #endif
  293. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
  294. /* Familiar character generator */
  295. {"chargen", SOCK_STREAM, 1, 0, chargen_stream,},
  296. {"chargen", SOCK_DGRAM, 0, 0, chargen_dg,},
  297. #endif
  298. {NULL, 0, 0, 0, NULL}
  299. };
  300. #endif /* INETD_FEATURE_ENABLED */
  301. static int global_queuelen = 128;
  302. static int nsock, maxsock;
  303. static fd_set allsock;
  304. static int toomany;
  305. static int timingout;
  306. static struct servent *sp;
  307. static uid_t uid;
  308. static char *CONFIG = _PATH_INETDCONF;
  309. static FILE *fconfig;
  310. static char line[1024];
  311. static char *defhost;
  312. /* xstrdup(NULL) returns NULL, but this one
  313. * will return newly-allocated "" if called with NULL arg
  314. * TODO: audit whether this makes any real difference
  315. */
  316. static char *xxstrdup(char *cp)
  317. {
  318. return xstrdup(cp ? cp : "");
  319. }
  320. static int setconfig(void)
  321. {
  322. free(defhost);
  323. defhost = xstrdup("*");
  324. if (fconfig != NULL) {
  325. fseek(fconfig, 0L, SEEK_SET);
  326. return 1;
  327. }
  328. fconfig = fopen(CONFIG, "r");
  329. return (fconfig != NULL);
  330. }
  331. static void endconfig(void)
  332. {
  333. if (fconfig) {
  334. (void) fclose(fconfig);
  335. fconfig = NULL;
  336. }
  337. free(defhost);
  338. defhost = 0;
  339. }
  340. #if ENABLE_FEATURE_INETD_RPC
  341. static void register_rpc(servtab_t *sep)
  342. {
  343. int n;
  344. struct sockaddr_in ir_sin;
  345. struct protoent *pp;
  346. socklen_t size;
  347. if ((pp = getprotobyname(sep->se_proto + 4)) == NULL) {
  348. bb_perror_msg("%s: getproto", sep->se_proto);
  349. return;
  350. }
  351. size = sizeof ir_sin;
  352. if (getsockname(sep->se_fd, (struct sockaddr *) &ir_sin, &size) < 0) {
  353. bb_perror_msg("%s/%s: getsockname",
  354. sep->se_service, sep->se_proto);
  355. return;
  356. }
  357. for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
  358. (void) pmap_unset(sep->se_rpcprog, n);
  359. if (!pmap_set(sep->se_rpcprog, n, pp->p_proto, ntohs(ir_sin.sin_port)))
  360. bb_perror_msg("%s %s: pmap_set: %u %u %u %u",
  361. sep->se_service, sep->se_proto,
  362. sep->se_rpcprog, n, pp->p_proto, ntohs(ir_sin.sin_port));
  363. }
  364. }
  365. static void unregister_rpc(servtab_t *sep)
  366. {
  367. int n;
  368. for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
  369. if (!pmap_unset(sep->se_rpcprog, n))
  370. bb_error_msg("pmap_unset(%u, %u)", sep->se_rpcprog, n);
  371. }
  372. }
  373. #endif /* FEATURE_INETD_RPC */
  374. static void freeconfig(servtab_t *cp)
  375. {
  376. int i;
  377. free(cp->se_hostaddr);
  378. free(cp->se_service);
  379. free(cp->se_proto);
  380. free(cp->se_user);
  381. free(cp->se_group);
  382. free(cp->se_server);
  383. for (i = 0; i < MAXARGV; i++)
  384. free(cp->se_argv[i]);
  385. }
  386. static int bump_nofile(void)
  387. {
  388. #define FD_CHUNK 32
  389. struct rlimit rl;
  390. if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
  391. bb_perror_msg("getrlimit");
  392. return -1;
  393. }
  394. rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK);
  395. rl.rlim_cur = MIN(FD_SETSIZE, rl.rlim_cur + FD_CHUNK);
  396. if (rl.rlim_cur <= rlim_ofile_cur) {
  397. bb_error_msg("bump_nofile: cannot extend file limit, max = %d",
  398. (int) rl.rlim_cur);
  399. return -1;
  400. }
  401. if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
  402. bb_perror_msg("setrlimit");
  403. return -1;
  404. }
  405. rlim_ofile_cur = rl.rlim_cur;
  406. return 0;
  407. }
  408. static void setup(servtab_t *sep)
  409. {
  410. int r;
  411. sep->se_fd = socket(sep->se_family, sep->se_socktype, 0);
  412. if (sep->se_fd < 0) {
  413. bb_perror_msg("%s/%s: socket", sep->se_service, sep->se_proto);
  414. return;
  415. }
  416. if (setsockopt_reuseaddr(sep->se_fd) < 0)
  417. bb_perror_msg("setsockopt(SO_REUSEADDR)");
  418. #if ENABLE_FEATURE_INETD_RPC
  419. if (isrpcservice(sep)) {
  420. struct passwd *pwd;
  421. /*
  422. * for RPC services, attempt to use a reserved port
  423. * if they are going to be running as root.
  424. *
  425. * Also, zero out the port for all RPC services; let bind()
  426. * find one.
  427. */
  428. sep->se_ctrladdr_in.sin_port = 0;
  429. if (sep->se_user && (pwd = getpwnam(sep->se_user)) &&
  430. pwd->pw_uid == 0 && uid == 0)
  431. r = bindresvport(sep->se_fd, &sep->se_ctrladdr_in);
  432. else {
  433. r = bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size);
  434. if (r == 0) {
  435. socklen_t len = sep->se_ctrladdr_size;
  436. int saveerrno = errno;
  437. /* update se_ctrladdr_in.sin_port */
  438. r = getsockname(sep->se_fd, &sep->se_ctrladdr, &len);
  439. if (r <= 0)
  440. errno = saveerrno;
  441. }
  442. }
  443. } else
  444. #endif
  445. r = bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size);
  446. if (r < 0) {
  447. bb_perror_msg("%s/%s (%d): bind",
  448. sep->se_service, sep->se_proto, sep->se_ctrladdr.sa_family);
  449. close(sep->se_fd);
  450. sep->se_fd = -1;
  451. if (!timingout) {
  452. timingout = 1;
  453. alarm(RETRYTIME);
  454. }
  455. return;
  456. }
  457. if (sep->se_socktype == SOCK_STREAM)
  458. listen(sep->se_fd, global_queuelen);
  459. FD_SET(sep->se_fd, &allsock);
  460. nsock++;
  461. if (sep->se_fd > maxsock) {
  462. maxsock = sep->se_fd;
  463. if ((rlim_t)maxsock > rlim_ofile_cur - FD_MARGIN)
  464. bump_nofile();
  465. }
  466. }
  467. static char *nextline(void)
  468. {
  469. char *cp;
  470. FILE *fd = fconfig;
  471. if (fgets(line, sizeof(line), fd) == NULL)
  472. return NULL;
  473. cp = strchr(line, '\n');
  474. if (cp)
  475. *cp = '\0';
  476. return line;
  477. }
  478. static char *skip(char **cpp) /* int report; */
  479. {
  480. char *cp = *cpp;
  481. char *start;
  482. /* erp: */
  483. if (*cpp == NULL) {
  484. /* if (report) */
  485. /* bb_error_msg("syntax error in inetd config file"); */
  486. return NULL;
  487. }
  488. again:
  489. while (*cp == ' ' || *cp == '\t')
  490. cp++;
  491. if (*cp == '\0') {
  492. int c;
  493. c = getc(fconfig);
  494. (void) ungetc(c, fconfig);
  495. if (c == ' ' || c == '\t')
  496. if ((cp = nextline()))
  497. goto again;
  498. *cpp = NULL;
  499. /* goto erp; */
  500. return NULL;
  501. }
  502. start = cp;
  503. while (*cp && *cp != ' ' && *cp != '\t')
  504. cp++;
  505. if (*cp != '\0')
  506. *cp++ = '\0';
  507. /* if ((*cpp = cp) == NULL) */
  508. /* goto erp; */
  509. *cpp = cp;
  510. return start;
  511. }
  512. static servtab_t *new_servtab(void)
  513. {
  514. return xmalloc(sizeof(servtab_t));
  515. }
  516. static servtab_t *dupconfig(servtab_t *sep)
  517. {
  518. servtab_t *newtab;
  519. int argc;
  520. newtab = new_servtab();
  521. memset(newtab, 0, sizeof(servtab_t));
  522. newtab->se_service = xstrdup(sep->se_service);
  523. newtab->se_socktype = sep->se_socktype;
  524. newtab->se_family = sep->se_family;
  525. newtab->se_proto = xstrdup(sep->se_proto);
  526. #if ENABLE_FEATURE_INETD_RPC
  527. newtab->se_rpcprog = sep->se_rpcprog;
  528. newtab->se_rpcversl = sep->se_rpcversl;
  529. newtab->se_rpcversh = sep->se_rpcversh;
  530. #endif
  531. newtab->se_wait = sep->se_wait;
  532. newtab->se_user = xstrdup(sep->se_user);
  533. newtab->se_group = xstrdup(sep->se_group);
  534. #ifdef INETD_FEATURE_ENABLED
  535. newtab->se_bi = sep->se_bi;
  536. #endif
  537. newtab->se_server = xstrdup(sep->se_server);
  538. for (argc = 0; argc <= MAXARGV; argc++)
  539. newtab->se_argv[argc] = xstrdup(sep->se_argv[argc]);
  540. newtab->se_max = sep->se_max;
  541. return newtab;
  542. }
  543. static servtab_t *getconfigent(void)
  544. {
  545. servtab_t *sep;
  546. int argc;
  547. char *cp, *arg;
  548. char *hostdelim;
  549. servtab_t *nsep;
  550. servtab_t *psep;
  551. sep = new_servtab();
  552. /* memset(sep, 0, sizeof *sep); */
  553. more:
  554. /* freeconfig(sep); */
  555. while ((cp = nextline()) && *cp == '#') /* skip comment line */;
  556. if (cp == NULL) {
  557. /* free(sep); */
  558. return NULL;
  559. }
  560. memset((char *) sep, 0, sizeof *sep);
  561. arg = skip(&cp);
  562. if (arg == NULL) {
  563. /* A blank line. */
  564. goto more;
  565. }
  566. /* Check for a host name. */
  567. hostdelim = strrchr(arg, ':');
  568. if (hostdelim) {
  569. *hostdelim = '\0';
  570. sep->se_hostaddr = xstrdup(arg);
  571. arg = hostdelim + 1;
  572. /*
  573. * If the line is of the form `host:', then just change the
  574. * default host for the following lines.
  575. */
  576. if (*arg == '\0') {
  577. arg = skip(&cp);
  578. if (cp == NULL) {
  579. free(defhost);
  580. defhost = sep->se_hostaddr;
  581. goto more;
  582. }
  583. }
  584. } else
  585. sep->se_hostaddr = xxstrdup(defhost);
  586. sep->se_service = xxstrdup(arg);
  587. arg = skip(&cp);
  588. if (strcmp(arg, "stream") == 0)
  589. sep->se_socktype = SOCK_STREAM;
  590. else if (strcmp(arg, "dgram") == 0)
  591. sep->se_socktype = SOCK_DGRAM;
  592. else if (strcmp(arg, "rdm") == 0)
  593. sep->se_socktype = SOCK_RDM;
  594. else if (strcmp(arg, "seqpacket") == 0)
  595. sep->se_socktype = SOCK_SEQPACKET;
  596. else if (strcmp(arg, "raw") == 0)
  597. sep->se_socktype = SOCK_RAW;
  598. else
  599. sep->se_socktype = -1;
  600. sep->se_proto = xxstrdup(skip(&cp));
  601. if (strcmp(sep->se_proto, "unix") == 0) {
  602. sep->se_family = AF_UNIX;
  603. } else {
  604. sep->se_family = AF_INET;
  605. if (sep->se_proto[strlen(sep->se_proto) - 1] == '6')
  606. #if ENABLE_FEATURE_IPV6
  607. sep->se_family = AF_INET6;
  608. #else
  609. bb_error_msg("%s: IPV6 not supported", sep->se_proto);
  610. #endif
  611. if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
  612. #if ENABLE_FEATURE_INETD_RPC
  613. char *p, *ccp;
  614. long l;
  615. p = strchr(sep->se_service, '/');
  616. if (p == 0) {
  617. bb_error_msg("%s: no rpc version", sep->se_service);
  618. goto more;
  619. }
  620. *p++ = '\0';
  621. l = strtol(p, &ccp, 0);
  622. if (ccp == p || l < 0 || l > INT_MAX) {
  623. badafterall:
  624. bb_error_msg("%s/%s: bad rpc version", sep->se_service, p);
  625. goto more;
  626. }
  627. sep->se_rpcversl = sep->se_rpcversh = l;
  628. if (*ccp == '-') {
  629. p = ccp + 1;
  630. l = strtol(p, &ccp, 0);
  631. if (ccp == p || l < 0 || l > INT_MAX || l < sep->se_rpcversl || *ccp)
  632. goto badafterall;
  633. sep->se_rpcversh = l;
  634. } else if (*ccp != '\0')
  635. goto badafterall;
  636. #else
  637. bb_error_msg("%s: rpc services not supported", sep->se_service);
  638. #endif
  639. }
  640. }
  641. arg = skip(&cp);
  642. if (arg == NULL)
  643. goto more;
  644. {
  645. char *s = strchr(arg, '.');
  646. if (s) {
  647. *s++ = '\0';
  648. sep->se_max = xatoi(s);
  649. } else
  650. sep->se_max = toomany;
  651. }
  652. sep->se_wait = strcmp(arg, "wait") == 0;
  653. /* if ((arg = skip(&cp, 1)) == NULL) */
  654. /* goto more; */
  655. sep->se_user = xxstrdup(skip(&cp));
  656. arg = strchr(sep->se_user, '.');
  657. if (arg == NULL)
  658. arg = strchr(sep->se_user, ':');
  659. if (arg) {
  660. *arg++ = '\0';
  661. sep->se_group = xstrdup(arg);
  662. }
  663. /* if ((arg = skip(&cp, 1)) == NULL) */
  664. /* goto more; */
  665. sep->se_server = xxstrdup(skip(&cp));
  666. if (strcmp(sep->se_server, "internal") == 0) {
  667. #ifdef INETD_FEATURE_ENABLED
  668. const struct builtin *bi;
  669. for (bi = builtins; bi->bi_service; bi++)
  670. if (bi->bi_socktype == sep->se_socktype &&
  671. strcmp(bi->bi_service, sep->se_service) == 0)
  672. break;
  673. if (bi->bi_service == 0) {
  674. bb_error_msg("internal service %s unknown", sep->se_service);
  675. goto more;
  676. }
  677. sep->se_bi = bi;
  678. sep->se_wait = bi->bi_wait;
  679. #else
  680. bb_perror_msg("internal service %s unknown", sep->se_service);
  681. goto more;
  682. #endif
  683. }
  684. #ifdef INETD_FEATURE_ENABLED
  685. else
  686. sep->se_bi = NULL;
  687. #endif
  688. argc = 0;
  689. for (arg = skip(&cp); cp; arg = skip(&cp)) {
  690. if (argc < MAXARGV)
  691. sep->se_argv[argc++] = xxstrdup(arg);
  692. }
  693. while (argc <= MAXARGV)
  694. sep->se_argv[argc++] = NULL;
  695. /*
  696. * Now that we've processed the entire line, check if the hostname
  697. * specifier was a comma separated list of hostnames. If so
  698. * we'll make new entries for each address.
  699. */
  700. while ((hostdelim = strrchr(sep->se_hostaddr, ',')) != NULL) {
  701. nsep = dupconfig(sep);
  702. /*
  703. * NULL terminate the hostname field of the existing entry,
  704. * and make a dup for the new entry.
  705. */
  706. *hostdelim++ = '\0';
  707. nsep->se_hostaddr = xstrdup(hostdelim);
  708. nsep->se_next = sep->se_next;
  709. sep->se_next = nsep;
  710. }
  711. nsep = sep;
  712. while (nsep != NULL) {
  713. nsep->se_checked = 1;
  714. if (nsep->se_family == AF_INET) {
  715. if (LONE_CHAR(nsep->se_hostaddr, '*'))
  716. nsep->se_ctrladdr_in.sin_addr.s_addr = INADDR_ANY;
  717. else if (!inet_aton(nsep->se_hostaddr, &nsep->se_ctrladdr_in.sin_addr)) {
  718. struct hostent *hp;
  719. hp = gethostbyname(nsep->se_hostaddr);
  720. if (hp == 0) {
  721. bb_error_msg("%s: unknown host", nsep->se_hostaddr);
  722. nsep->se_checked = 0;
  723. goto skip;
  724. } else if (hp->h_addrtype != AF_INET) {
  725. bb_error_msg("%s: address isn't an Internet "
  726. "address", nsep->se_hostaddr);
  727. nsep->se_checked = 0;
  728. goto skip;
  729. } else {
  730. int i = 1;
  731. memmove(&nsep->se_ctrladdr_in.sin_addr,
  732. hp->h_addr_list[0], sizeof(struct in_addr));
  733. while (hp->h_addr_list[i] != NULL) {
  734. psep = dupconfig(nsep);
  735. psep->se_hostaddr = xxstrdup(nsep->se_hostaddr);
  736. psep->se_checked = 1;
  737. memmove(&psep->se_ctrladdr_in.sin_addr,
  738. hp->h_addr_list[i], sizeof(struct in_addr));
  739. psep->se_ctrladdr_size = sizeof(psep->se_ctrladdr_in);
  740. i++;
  741. /* Prepend to list, don't want to look up */
  742. /* its hostname again. */
  743. psep->se_next = sep;
  744. sep = psep;
  745. }
  746. }
  747. }
  748. }
  749. /* XXX BUG?: is this skip: label supposed to remain? */
  750. skip:
  751. nsep = nsep->se_next;
  752. }
  753. /*
  754. * Finally, free any entries which failed the gethostbyname
  755. * check.
  756. */
  757. psep = NULL;
  758. nsep = sep;
  759. while (nsep != NULL) {
  760. servtab_t *tsep;
  761. if (nsep->se_checked == 0) {
  762. tsep = nsep;
  763. if (psep == NULL) {
  764. sep = nsep->se_next;
  765. nsep = sep;
  766. } else {
  767. nsep = nsep->se_next;
  768. psep->se_next = nsep;
  769. }
  770. freeconfig(tsep);
  771. } else {
  772. nsep->se_checked = 0;
  773. psep = nsep;
  774. nsep = nsep->se_next;
  775. }
  776. }
  777. return sep;
  778. }
  779. #define Block_Using_Signals(m) do { \
  780. sigemptyset(&m); \
  781. sigaddset(&m, SIGCHLD); \
  782. sigaddset(&m, SIGHUP); \
  783. sigaddset(&m, SIGALRM); \
  784. sigprocmask(SIG_BLOCK, &m, NULL); \
  785. } while (0)
  786. static servtab_t *enter(servtab_t *cp)
  787. {
  788. servtab_t *sep;
  789. sigset_t omask;
  790. sep = new_servtab();
  791. *sep = *cp;
  792. sep->se_fd = -1;
  793. #if ENABLE_FEATURE_INETD_RPC
  794. sep->se_rpcprog = -1;
  795. #endif
  796. Block_Using_Signals(omask);
  797. sep->se_next = servtab;
  798. servtab = sep;
  799. sigprocmask(SIG_UNBLOCK, &omask, NULL);
  800. return sep;
  801. }
  802. static int matchconf(servtab_t *old, servtab_t *new)
  803. {
  804. if (strcmp(old->se_service, new->se_service) != 0)
  805. return 0;
  806. if (strcmp(old->se_hostaddr, new->se_hostaddr) != 0)
  807. return 0;
  808. if (strcmp(old->se_proto, new->se_proto) != 0)
  809. return 0;
  810. /*
  811. * If the new servtab is bound to a specific address, check that the
  812. * old servtab is bound to the same entry. If the new service is not
  813. * bound to a specific address then the check of se_hostaddr above
  814. * is sufficient.
  815. */
  816. if (old->se_family == AF_INET && new->se_family == AF_INET &&
  817. memcmp(&old->se_ctrladdr_in.sin_addr,
  818. &new->se_ctrladdr_in.sin_addr,
  819. sizeof(new->se_ctrladdr_in.sin_addr)) != 0)
  820. return 0;
  821. #if ENABLE_FEATURE_IPV6
  822. if (old->se_family == AF_INET6 && new->se_family == AF_INET6 &&
  823. memcmp(&old->se_ctrladdr_in6.sin6_addr,
  824. &new->se_ctrladdr_in6.sin6_addr,
  825. sizeof(new->se_ctrladdr_in6.sin6_addr)) != 0)
  826. return 0;
  827. #endif
  828. return 1;
  829. }
  830. static void config(int sig ATTRIBUTE_UNUSED)
  831. {
  832. servtab_t *sep, *cp, **sepp;
  833. sigset_t omask;
  834. size_t n;
  835. char protoname[10];
  836. if (!setconfig()) {
  837. bb_perror_msg("%s", CONFIG);
  838. return;
  839. }
  840. for (sep = servtab; sep; sep = sep->se_next)
  841. sep->se_checked = 0;
  842. cp = getconfigent();
  843. while (cp != NULL) {
  844. for (sep = servtab; sep; sep = sep->se_next)
  845. if (matchconf(sep, cp))
  846. break;
  847. if (sep != 0) {
  848. int i;
  849. #define SWAP(type, a, b) do {type c=(type)a; a=(type)b; b=(type)c;} while (0)
  850. Block_Using_Signals(omask);
  851. /*
  852. * sep->se_wait may be holding the pid of a daemon
  853. * that we're waiting for. If so, don't overwrite
  854. * it unless the config file explicitly says don't
  855. * wait.
  856. */
  857. if (
  858. #ifdef INETD_FEATURE_ENABLED
  859. cp->se_bi == 0 &&
  860. #endif
  861. (sep->se_wait == 1 || cp->se_wait == 0))
  862. sep->se_wait = cp->se_wait;
  863. SWAP(int, cp->se_max, sep->se_max);
  864. SWAP(char *, sep->se_user, cp->se_user);
  865. SWAP(char *, sep->se_group, cp->se_group);
  866. SWAP(char *, sep->se_server, cp->se_server);
  867. for (i = 0; i < MAXARGV; i++)
  868. SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
  869. #undef SWAP
  870. #if ENABLE_FEATURE_INETD_RPC
  871. if (isrpcservice(sep))
  872. unregister_rpc(sep);
  873. sep->se_rpcversl = cp->se_rpcversl;
  874. sep->se_rpcversh = cp->se_rpcversh;
  875. #endif
  876. sigprocmask(SIG_UNBLOCK, &omask, NULL);
  877. freeconfig(cp);
  878. } else {
  879. sep = enter(cp);
  880. }
  881. sep->se_checked = 1;
  882. switch (sep->se_family) {
  883. case AF_UNIX:
  884. if (sep->se_fd != -1)
  885. break;
  886. (void) unlink(sep->se_service);
  887. n = strlen(sep->se_service);
  888. if (n > sizeof sep->se_ctrladdr_un.sun_path - 1)
  889. n = sizeof sep->se_ctrladdr_un.sun_path - 1;
  890. safe_strncpy(sep->se_ctrladdr_un.sun_path, sep->se_service, n + 1);
  891. sep->se_ctrladdr_un.sun_family = AF_UNIX;
  892. sep->se_ctrladdr_size = n + sizeof sep->se_ctrladdr_un.sun_family;
  893. setup(sep);
  894. break;
  895. case AF_INET:
  896. sep->se_ctrladdr_in.sin_family = AF_INET;
  897. /* se_ctrladdr_in was set in getconfigent */
  898. sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in;
  899. #if ENABLE_FEATURE_INETD_RPC
  900. if (isrpcservice(sep)) {
  901. struct rpcent *rp;
  902. // FIXME: atoi_or_else(str, 0) would be handy here
  903. sep->se_rpcprog = atoi(sep->se_service);
  904. if (sep->se_rpcprog == 0) {
  905. rp = getrpcbyname(sep->se_service);
  906. if (rp == 0) {
  907. bb_error_msg("%s: unknown rpc service", sep->se_service);
  908. goto serv_unknown;
  909. }
  910. sep->se_rpcprog = rp->r_number;
  911. }
  912. if (sep->se_fd == -1)
  913. setup(sep);
  914. if (sep->se_fd != -1)
  915. register_rpc(sep);
  916. } else
  917. #endif
  918. {
  919. uint16_t port = htons(atoi(sep->se_service));
  920. // FIXME: atoi_or_else(str, 0) would be handy here
  921. if (!port) {
  922. /*XXX*/ strncpy(protoname, sep->se_proto, sizeof(protoname));
  923. if (isdigit(protoname[strlen(protoname) - 1]))
  924. protoname[strlen(protoname) - 1] = '\0';
  925. sp = getservbyname(sep->se_service, protoname);
  926. if (sp == 0) {
  927. bb_error_msg("%s/%s: unknown service",
  928. sep->se_service, sep->se_proto);
  929. goto serv_unknown;
  930. }
  931. port = sp->s_port;
  932. }
  933. if (port != sep->se_ctrladdr_in.sin_port) {
  934. sep->se_ctrladdr_in.sin_port = port;
  935. if (sep->se_fd != -1) {
  936. FD_CLR(sep->se_fd, &allsock);
  937. nsock--;
  938. (void) close(sep->se_fd);
  939. }
  940. sep->se_fd = -1;
  941. }
  942. if (sep->se_fd == -1)
  943. setup(sep);
  944. }
  945. break;
  946. #if ENABLE_FEATURE_IPV6
  947. case AF_INET6:
  948. sep->se_ctrladdr_in6.sin6_family = AF_INET6;
  949. /* se_ctrladdr_in was set in getconfigent */
  950. sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in6;
  951. #if ENABLE_FEATURE_INETD_RPC
  952. if (isrpcservice(sep)) {
  953. struct rpcent *rp;
  954. sep->se_rpcprog = atoi(sep->se_service);
  955. if (sep->se_rpcprog == 0) {
  956. rp = getrpcbyname(sep->se_service);
  957. if (rp == 0) {
  958. bb_error_msg("%s: unknown rpc service", sep->se_service);
  959. goto serv_unknown;
  960. }
  961. sep->se_rpcprog = rp->r_number;
  962. }
  963. if (sep->se_fd == -1)
  964. setup(sep);
  965. if (sep->se_fd != -1)
  966. register_rpc(sep);
  967. } else
  968. #endif
  969. {
  970. uint16_t port = htons(atoi(sep->se_service));
  971. if (!port) {
  972. /*XXX*/ strncpy(protoname, sep->se_proto, sizeof(protoname));
  973. if (isdigit(protoname[strlen(protoname) - 1]))
  974. protoname[strlen(protoname) - 1] = '\0';
  975. sp = getservbyname(sep->se_service, protoname);
  976. if (sp == 0) {
  977. bb_error_msg("%s/%s: unknown service",
  978. sep->se_service, sep->se_proto);
  979. goto serv_unknown;
  980. }
  981. port = sp->s_port;
  982. }
  983. if (port != sep->se_ctrladdr_in6.sin6_port) {
  984. sep->se_ctrladdr_in6.sin6_port = port;
  985. if (sep->se_fd != -1) {
  986. FD_CLR(sep->se_fd, &allsock);
  987. nsock--;
  988. (void) close(sep->se_fd);
  989. }
  990. sep->se_fd = -1;
  991. }
  992. if (sep->se_fd == -1)
  993. setup(sep);
  994. }
  995. break;
  996. #endif /* FEATURE_IPV6 */
  997. }
  998. serv_unknown:
  999. if (cp->se_next != NULL) {
  1000. servtab_t *tmp = cp;
  1001. cp = cp->se_next;
  1002. free(tmp);
  1003. } else {
  1004. free(cp);
  1005. cp = getconfigent();
  1006. }
  1007. }
  1008. endconfig();
  1009. /*
  1010. * Purge anything not looked at above.
  1011. */
  1012. Block_Using_Signals(omask);
  1013. sepp = &servtab;
  1014. while ((sep = *sepp)) {
  1015. if (sep->se_checked) {
  1016. sepp = &sep->se_next;
  1017. continue;
  1018. }
  1019. *sepp = sep->se_next;
  1020. if (sep->se_fd != -1) {
  1021. FD_CLR(sep->se_fd, &allsock);
  1022. nsock--;
  1023. (void) close(sep->se_fd);
  1024. }
  1025. #if ENABLE_FEATURE_INETD_RPC
  1026. if (isrpcservice(sep))
  1027. unregister_rpc(sep);
  1028. #endif
  1029. if (sep->se_family == AF_UNIX)
  1030. (void) unlink(sep->se_service);
  1031. freeconfig(sep);
  1032. free(sep);
  1033. }
  1034. sigprocmask(SIG_UNBLOCK, &omask, NULL);
  1035. }
  1036. static void reapchild(int sig ATTRIBUTE_UNUSED)
  1037. {
  1038. pid_t pid;
  1039. int save_errno = errno, status;
  1040. servtab_t *sep;
  1041. for (;;) {
  1042. pid = wait3(&status, WNOHANG, NULL);
  1043. if (pid <= 0)
  1044. break;
  1045. for (sep = servtab; sep; sep = sep->se_next)
  1046. if (sep->se_wait == pid) {
  1047. if (WIFEXITED(status) && WEXITSTATUS(status))
  1048. bb_error_msg("%s: exit status 0x%x",
  1049. sep->se_server, WEXITSTATUS(status));
  1050. else if (WIFSIGNALED(status))
  1051. bb_error_msg("%s: exit signal 0x%x",
  1052. sep->se_server, WTERMSIG(status));
  1053. sep->se_wait = 1;
  1054. FD_SET(sep->se_fd, &allsock);
  1055. nsock++;
  1056. }
  1057. }
  1058. errno = save_errno;
  1059. }
  1060. static void retry(int sig ATTRIBUTE_UNUSED)
  1061. {
  1062. servtab_t *sep;
  1063. timingout = 0;
  1064. for (sep = servtab; sep; sep = sep->se_next) {
  1065. if (sep->se_fd == -1) {
  1066. switch (sep->se_family) {
  1067. case AF_UNIX:
  1068. case AF_INET:
  1069. #if ENABLE_FEATURE_IPV6
  1070. case AF_INET6:
  1071. #endif
  1072. setup(sep);
  1073. #if ENABLE_FEATURE_INETD_RPC
  1074. if (sep->se_fd != -1 && isrpcservice(sep))
  1075. register_rpc(sep);
  1076. #endif
  1077. break;
  1078. }
  1079. }
  1080. }
  1081. }
  1082. static void goaway(int sig ATTRIBUTE_UNUSED)
  1083. {
  1084. servtab_t *sep;
  1085. /* XXX signal race walking sep list */
  1086. for (sep = servtab; sep; sep = sep->se_next) {
  1087. if (sep->se_fd == -1)
  1088. continue;
  1089. switch (sep->se_family) {
  1090. case AF_UNIX:
  1091. (void) unlink(sep->se_service);
  1092. break;
  1093. case AF_INET:
  1094. #if ENABLE_FEATURE_IPV6
  1095. case AF_INET6:
  1096. #endif
  1097. #if ENABLE_FEATURE_INETD_RPC
  1098. if (sep->se_wait == 1 && isrpcservice(sep))
  1099. unregister_rpc(sep); /* XXX signal race */
  1100. #endif
  1101. break;
  1102. }
  1103. (void) close(sep->se_fd);
  1104. }
  1105. (void) unlink(_PATH_INETDPID);
  1106. exit(0);
  1107. }
  1108. #ifdef INETD_SETPROCTITLE
  1109. static char **Argv;
  1110. static char *LastArg;
  1111. static void
  1112. inetd_setproctitle(char *a, int s)
  1113. {
  1114. socklen_t size;
  1115. char *cp;
  1116. struct sockaddr_in prt_sin;
  1117. char buf[80];
  1118. cp = Argv[0];
  1119. size = sizeof(prt_sin);
  1120. (void) snprintf(buf, sizeof buf, "-%s", a);
  1121. if (getpeername(s, (struct sockaddr *) &prt_sin, &size) == 0) {
  1122. char *sa = inet_ntoa(prt_sin.sin_addr);
  1123. buf[sizeof(buf) - 1 - strlen(sa) - 3] = '\0';
  1124. strcat(buf, " [");
  1125. strcat(buf, sa);
  1126. strcat(buf, "]");
  1127. }
  1128. strncpy(cp, buf, LastArg - cp);
  1129. cp += strlen(cp);
  1130. while (cp < LastArg)
  1131. *cp++ = ' ';
  1132. }
  1133. #endif
  1134. int
  1135. inetd_main(int argc, char *argv[])
  1136. {
  1137. servtab_t *sep;
  1138. struct passwd *pwd;
  1139. struct group *grp = NULL;
  1140. int tmpint;
  1141. struct sigaction sa, sapipe;
  1142. int opt;
  1143. pid_t pid;
  1144. char buf[50];
  1145. char *stoomany;
  1146. sigset_t omask, wait_mask;
  1147. #ifdef INETD_SETPROCTITLE
  1148. extern char **environ;
  1149. char **envp = environ;
  1150. Argv = argv;
  1151. if (envp == 0 || *envp == 0)
  1152. envp = argv;
  1153. while (*envp)
  1154. envp++;
  1155. LastArg = envp[-1] + strlen(envp[-1]);
  1156. #endif
  1157. opt = getopt32(argc, argv, "R:f", &stoomany);
  1158. if(opt & 1) {
  1159. toomany = xatoi_u(stoomany);
  1160. }
  1161. argc -= optind;
  1162. argv += optind;
  1163. uid = getuid();
  1164. if (uid != 0)
  1165. CONFIG = NULL;
  1166. if (argc > 0)
  1167. CONFIG = argv[0];
  1168. if (CONFIG == NULL)
  1169. bb_error_msg_and_die("non-root must specify a config file");
  1170. #ifdef BB_NOMMU
  1171. if (!(opt & 2)) {
  1172. /* reexec for vfork() do continue parent */
  1173. vfork_daemon_rexec(0, 0, argc, argv, "-f");
  1174. }
  1175. bb_sanitize_stdio();
  1176. #else
  1177. bb_sanitize_stdio_maybe_daemonize(!(opt & 2));
  1178. #endif
  1179. openlog(applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
  1180. logmode = LOGMODE_SYSLOG;
  1181. if (uid == 0) {
  1182. /* If run by hand, ensure groups vector gets trashed */
  1183. gid_t gid = getgid();
  1184. setgroups(1, &gid);
  1185. }
  1186. {
  1187. FILE *fp = fopen(_PATH_INETDPID, "w");
  1188. if (fp != NULL) {
  1189. fprintf(fp, "%u\n", getpid());
  1190. fclose(fp);
  1191. }
  1192. }
  1193. if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
  1194. bb_perror_msg("getrlimit");
  1195. } else {
  1196. rlim_ofile_cur = rlim_ofile.rlim_cur;
  1197. if (rlim_ofile_cur == RLIM_INFINITY) /* ! */
  1198. rlim_ofile_cur = OPEN_MAX;
  1199. }
  1200. memset((char *) &sa, 0, sizeof(sa));
  1201. sigemptyset(&sa.sa_mask);
  1202. sigaddset(&sa.sa_mask, SIGALRM);
  1203. sigaddset(&sa.sa_mask, SIGCHLD);
  1204. sigaddset(&sa.sa_mask, SIGHUP);
  1205. sa.sa_handler = retry;
  1206. sigaction(SIGALRM, &sa, NULL);
  1207. config(SIGHUP);
  1208. sa.sa_handler = config;
  1209. sigaction(SIGHUP, &sa, NULL);
  1210. sa.sa_handler = reapchild;
  1211. sigaction(SIGCHLD, &sa, NULL);
  1212. sa.sa_handler = goaway;
  1213. sigaction(SIGTERM, &sa, NULL);
  1214. sa.sa_handler = goaway;
  1215. sigaction(SIGINT, &sa, NULL);
  1216. sa.sa_handler = SIG_IGN;
  1217. sigaction(SIGPIPE, &sa, &sapipe);
  1218. memset(&wait_mask, 0, sizeof(wait_mask));
  1219. {
  1220. /* space for daemons to overwrite environment for ps */
  1221. #define DUMMYSIZE 100
  1222. char dummy[DUMMYSIZE];
  1223. (void) memset(dummy, 'x', DUMMYSIZE - 1);
  1224. dummy[DUMMYSIZE - 1] = '\0';
  1225. (void) setenv("inetd_dummy", dummy, 1);
  1226. }
  1227. for (;;) {
  1228. int n, ctrl = -1;
  1229. fd_set readable;
  1230. if (nsock == 0) {
  1231. Block_Using_Signals(omask);
  1232. while (nsock == 0)
  1233. sigsuspend(&wait_mask);
  1234. sigprocmask(SIG_UNBLOCK, &omask, NULL);
  1235. }
  1236. readable = allsock;
  1237. n = select(maxsock + 1, &readable, NULL, NULL, NULL);
  1238. if (n <= 0) {
  1239. if (n < 0 && errno != EINTR) {
  1240. bb_perror_msg("select");
  1241. sleep(1);
  1242. }
  1243. continue;
  1244. }
  1245. for (sep = servtab; n && sep; sep = sep->se_next) {
  1246. if (sep->se_fd == -1 || !FD_ISSET(sep->se_fd, &readable))
  1247. continue;
  1248. n--;
  1249. if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
  1250. ctrl = accept(sep->se_fd, NULL, NULL);
  1251. if (ctrl < 0) {
  1252. if (errno == EINTR)
  1253. continue;
  1254. bb_perror_msg("accept (for %s)", sep->se_service);
  1255. continue;
  1256. }
  1257. if (sep->se_family == AF_INET && sep->se_socktype == SOCK_STREAM) {
  1258. struct sockaddr_in peer;
  1259. socklen_t plen = sizeof(peer);
  1260. if (getpeername(ctrl, (struct sockaddr *) &peer, &plen) < 0) {
  1261. bb_error_msg("cannot getpeername");
  1262. close(ctrl);
  1263. continue;
  1264. }
  1265. if (ntohs(peer.sin_port) == 20) {
  1266. /* XXX ftp bounce */
  1267. close(ctrl);
  1268. continue;
  1269. }
  1270. }
  1271. } else
  1272. ctrl = sep->se_fd;
  1273. Block_Using_Signals(omask);
  1274. pid = 0;
  1275. #ifdef INETD_FEATURE_ENABLED
  1276. if (sep->se_bi == 0 || sep->se_bi->bi_fork)
  1277. #endif
  1278. {
  1279. if (sep->se_count++ == 0)
  1280. (void) gettimeofday(&sep->se_time, NULL);
  1281. else if (toomany > 0 && sep->se_count >= sep->se_max) {
  1282. struct timeval now;
  1283. (void) gettimeofday(&now, NULL);
  1284. if (now.tv_sec - sep->se_time.tv_sec > CNT_INTVL) {
  1285. sep->se_time = now;
  1286. sep->se_count = 1;
  1287. } else {
  1288. if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
  1289. close(ctrl);
  1290. if (sep->se_family == AF_INET &&
  1291. ntohs(sep->se_ctrladdr_in.sin_port) >= IPPORT_RESERVED) {
  1292. /*
  1293. * Cannot close it -- there are
  1294. * thieves on the system.
  1295. * Simply ignore the connection.
  1296. */
  1297. --sep->se_count;
  1298. continue;
  1299. }
  1300. bb_error_msg("%s/%s server failing (looping), service terminated",
  1301. sep->se_service, sep->se_proto);
  1302. if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
  1303. close(ctrl);
  1304. FD_CLR(sep->se_fd, &allsock);
  1305. (void) close(sep->se_fd);
  1306. sep->se_fd = -1;
  1307. sep->se_count = 0;
  1308. nsock--;
  1309. sigprocmask(SIG_UNBLOCK, &omask, NULL);
  1310. if (!timingout) {
  1311. timingout = 1;
  1312. alarm(RETRYTIME);
  1313. }
  1314. continue;
  1315. }
  1316. }
  1317. pid = fork();
  1318. }
  1319. if (pid < 0) {
  1320. bb_perror_msg("fork");
  1321. if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
  1322. close(ctrl);
  1323. sigprocmask(SIG_UNBLOCK, &omask, NULL);
  1324. sleep(1);
  1325. continue;
  1326. }
  1327. if (pid && sep->se_wait) {
  1328. sep->se_wait = pid;
  1329. FD_CLR(sep->se_fd, &allsock);
  1330. nsock--;
  1331. }
  1332. sigprocmask(SIG_UNBLOCK, &omask, NULL);
  1333. if (pid == 0) {
  1334. #ifdef INETD_FEATURE_ENABLED
  1335. if (sep->se_bi) {
  1336. (*sep->se_bi->bi_fn)(ctrl, sep);
  1337. } else
  1338. #endif
  1339. {
  1340. pwd = getpwnam(sep->se_user);
  1341. if (pwd == NULL) {
  1342. bb_error_msg("getpwnam: %s: no such user", sep->se_user);
  1343. goto do_exit1;
  1344. }
  1345. if (setsid() < 0)
  1346. bb_perror_msg("%s: setsid", sep->se_service);
  1347. if (sep->se_group && (grp = getgrnam(sep->se_group)) == NULL) {
  1348. bb_error_msg("getgrnam: %s: no such group", sep->se_group);
  1349. goto do_exit1;
  1350. }
  1351. if (uid != 0) {
  1352. /* a user running private inetd */
  1353. if (uid != pwd->pw_uid)
  1354. _exit(1);
  1355. } else if (pwd->pw_uid) {
  1356. if (sep->se_group)
  1357. pwd->pw_gid = grp->gr_gid;
  1358. xsetgid((gid_t) pwd->pw_gid);
  1359. initgroups(pwd->pw_name, pwd->pw_gid);
  1360. xsetuid((uid_t) pwd->pw_uid);
  1361. } else if (sep->se_group) {
  1362. xsetgid(grp->gr_gid);
  1363. setgroups(1, &grp->gr_gid);
  1364. }
  1365. dup2(ctrl, 0);
  1366. if (ctrl) close(ctrl);
  1367. dup2(0, 1);
  1368. dup2(0, 2);
  1369. if (rlim_ofile.rlim_cur != rlim_ofile_cur)
  1370. if (setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0)
  1371. bb_perror_msg("setrlimit");
  1372. closelog();
  1373. for (tmpint = rlim_ofile_cur - 1; --tmpint > 2;)
  1374. (void) close(tmpint);
  1375. sigaction(SIGPIPE, &sapipe, NULL);
  1376. execv(sep->se_server, sep->se_argv);
  1377. bb_perror_msg("execv %s", sep->se_server);
  1378. do_exit1:
  1379. if (sep->se_socktype != SOCK_STREAM)
  1380. recv(0, buf, sizeof(buf), 0);
  1381. _exit(1);
  1382. }
  1383. }
  1384. if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
  1385. close(ctrl);
  1386. } /* for (sep = servtab...) */
  1387. } /* for (;;) */
  1388. }
  1389. /*
  1390. * Internet services provided internally by inetd:
  1391. */
  1392. #define BUFSIZE 4096
  1393. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \
  1394. ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN || \
  1395. ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
  1396. static int dg_badinput(struct sockaddr_in *dg_sin)
  1397. {
  1398. if (ntohs(dg_sin->sin_port) < IPPORT_RESERVED)
  1399. return 1;
  1400. if (dg_sin->sin_addr.s_addr == htonl(INADDR_BROADCAST))
  1401. return 1;
  1402. /* XXX compare against broadcast addresses in SIOCGIFCONF list? */
  1403. return 0;
  1404. }
  1405. #endif
  1406. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
  1407. /* Echo service -- echo data back */
  1408. /* ARGSUSED */
  1409. static void
  1410. echo_stream(int s, servtab_t *sep)
  1411. {
  1412. char buffer[BUFSIZE];
  1413. int i;
  1414. inetd_setproctitle(sep->se_service, s);
  1415. while (1) {
  1416. i = read(s, buffer, sizeof(buffer));
  1417. if (i <= 0) break;
  1418. /* FIXME: this isnt correct - safe_write()? */
  1419. if (write(s, buffer, i) <= 0) break;
  1420. }
  1421. exit(0);
  1422. }
  1423. /* Echo service -- echo data back */
  1424. /* ARGSUSED */
  1425. static void
  1426. echo_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
  1427. {
  1428. char buffer[BUFSIZE];
  1429. int i;
  1430. socklen_t size;
  1431. /* struct sockaddr_storage ss; */
  1432. struct sockaddr sa;
  1433. size = sizeof(sa);
  1434. i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size);
  1435. if (i < 0)
  1436. return;
  1437. if (dg_badinput((struct sockaddr_in *) &sa))
  1438. return;
  1439. (void) sendto(s, buffer, i, 0, &sa, sizeof(sa));
  1440. }
  1441. #endif /* FEATURE_INETD_SUPPORT_BUILTIN_ECHO */
  1442. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
  1443. /* Discard service -- ignore data */
  1444. /* ARGSUSED */
  1445. static void
  1446. discard_stream(int s, servtab_t *sep)
  1447. {
  1448. char buffer[BUFSIZE];
  1449. inetd_setproctitle(sep->se_service, s);
  1450. while (1) {
  1451. errno = 0;
  1452. if (read(s, buffer, sizeof(buffer)) <= 0 && errno != EINTR)
  1453. exit(0);
  1454. }
  1455. }
  1456. /* Discard service -- ignore data */
  1457. /* ARGSUSED */
  1458. static void
  1459. discard_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
  1460. {
  1461. char buffer[BUFSIZE];
  1462. (void) read(s, buffer, sizeof(buffer));
  1463. }
  1464. #endif /* FEATURE_INETD_SUPPORT_BUILTIN_DISCARD */
  1465. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
  1466. #define LINESIZ 72
  1467. static char ring[128];
  1468. static char *endring;
  1469. static void
  1470. initring(void)
  1471. {
  1472. int i;
  1473. endring = ring;
  1474. for (i = 0; i <= 128; ++i)
  1475. if (isprint(i))
  1476. *endring++ = i;
  1477. }
  1478. /* Character generator */
  1479. /* ARGSUSED */
  1480. static void
  1481. chargen_stream(int s, servtab_t *sep)
  1482. {
  1483. char *rs;
  1484. int len;
  1485. char text[LINESIZ + 2];
  1486. inetd_setproctitle(sep->se_service, s);
  1487. if (!endring) {
  1488. initring();
  1489. rs = ring;
  1490. }
  1491. text[LINESIZ] = '\r';
  1492. text[LINESIZ + 1] = '\n';
  1493. rs = ring;
  1494. for (;;) {
  1495. len = endring - rs;
  1496. if (len >= LINESIZ)
  1497. memmove(text, rs, LINESIZ);
  1498. else {
  1499. memmove(text, rs, len);
  1500. memmove(text + len, ring, LINESIZ - len);
  1501. }
  1502. if (++rs == endring)
  1503. rs = ring;
  1504. if (write(s, text, sizeof(text)) != sizeof(text))
  1505. break;
  1506. }
  1507. exit(0);
  1508. }
  1509. /* Character generator */
  1510. /* ARGSUSED */
  1511. static void
  1512. chargen_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
  1513. {
  1514. /* struct sockaddr_storage ss; */
  1515. struct sockaddr sa;
  1516. static char *rs;
  1517. int len;
  1518. char text[LINESIZ + 2];
  1519. socklen_t size;
  1520. if (endring == 0) {
  1521. initring();
  1522. rs = ring;
  1523. }
  1524. size = sizeof(sa);
  1525. if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0)
  1526. return;
  1527. if (dg_badinput((struct sockaddr_in *) &sa))
  1528. return;
  1529. if ((len = endring - rs) >= LINESIZ)
  1530. memmove(text, rs, LINESIZ);
  1531. else {
  1532. memmove(text, rs, len);
  1533. memmove(text + len, ring, LINESIZ - len);
  1534. }
  1535. if (++rs == endring)
  1536. rs = ring;
  1537. text[LINESIZ] = '\r';
  1538. text[LINESIZ + 1] = '\n';
  1539. (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa));
  1540. }
  1541. #endif /* FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN */
  1542. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME
  1543. /*
  1544. * Return a machine readable date and time, in the form of the
  1545. * number of seconds since midnight, Jan 1, 1900. Since gettimeofday
  1546. * returns the number of seconds since midnight, Jan 1, 1970,
  1547. * we must add 2208988800 seconds to this figure to make up for
  1548. * some seventy years Bell Labs was asleep.
  1549. */
  1550. static u_int machtime(void)
  1551. {
  1552. struct timeval tv;
  1553. if (gettimeofday(&tv, NULL) < 0) {
  1554. fprintf(stderr, "Unable to get time of day\n");
  1555. return 0L;
  1556. }
  1557. return htonl((u_int) tv.tv_sec + 2208988800UL);
  1558. }
  1559. /* ARGSUSED */
  1560. static void
  1561. machtime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED)
  1562. {
  1563. u_int result;
  1564. result = machtime();
  1565. (void) write(s, (char *) &result, sizeof(result));
  1566. }
  1567. /* ARGSUSED */
  1568. static void
  1569. machtime_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
  1570. {
  1571. u_int result;
  1572. /* struct sockaddr_storage ss; */
  1573. struct sockaddr sa;
  1574. struct sockaddr_in *dg_sin;
  1575. socklen_t size;
  1576. size = sizeof(sa);
  1577. if (recvfrom(s, (char *) &result, sizeof(result), 0, &sa, &size) < 0)
  1578. return;
  1579. /* if (dg_badinput((struct sockaddr *)&ss)) */
  1580. dg_sin = (struct sockaddr_in *) &sa;
  1581. if (dg_sin->sin_addr.s_addr == htonl(INADDR_BROADCAST) ||
  1582. ntohs(dg_sin->sin_port) < IPPORT_RESERVED / 2)
  1583. return;
  1584. result = machtime();
  1585. (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa));
  1586. }
  1587. #endif /* FEATURE_INETD_SUPPORT_BUILTIN_TIME */
  1588. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
  1589. /* Return human-readable time of day */
  1590. /* ARGSUSED */
  1591. static void daytime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED)
  1592. {
  1593. char buffer[32];
  1594. time_t t;
  1595. t = time(NULL);
  1596. // fdprintf instead?
  1597. (void) sprintf(buffer, "%.24s\r\n", ctime(&t));
  1598. (void) write(s, buffer, strlen(buffer));
  1599. }
  1600. /* Return human-readable time of day */
  1601. /* ARGSUSED */
  1602. void
  1603. daytime_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
  1604. {
  1605. char buffer[256];
  1606. time_t t;
  1607. /* struct sockaddr_storage ss; */
  1608. struct sockaddr sa;
  1609. socklen_t size;
  1610. t = time(NULL);
  1611. size = sizeof(sa);
  1612. if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0)
  1613. return;
  1614. if (dg_badinput((struct sockaddr_in *) &sa))
  1615. return;
  1616. (void) sprintf(buffer, "%.24s\r\n", ctime(&t));
  1617. (void) sendto(s, buffer, strlen(buffer), 0, &sa, sizeof(sa));
  1618. }
  1619. #endif /* FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME */