3
0

inetd.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /*
  2. * Copyright (c) 1983,1991 The Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * David A. Holland.
  7. *
  8. * Busybox port by Vladimir Oleynik (C) 2001-2003 <dzo@simtreas.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. /*
  26. * Inetd - Internet super-server
  27. *
  28. * This program invokes all internet services as needed.
  29. * connection-oriented services are invoked each time a
  30. * connection is made, by creating a process. This process
  31. * is passed the connection as file descriptor 0 and is
  32. * expected to do a getpeername to find out the source host
  33. * and port.
  34. *
  35. * Datagram oriented services are invoked when a datagram
  36. * arrives; a process is created and passed a pending message
  37. * on file descriptor 0. Datagram servers may either connect
  38. * to their peer, freeing up the original socket for inetd
  39. * to receive further messages on, or ``take over the socket'',
  40. * processing all arriving datagrams and, eventually, timing
  41. * out. The first type of server is said to be ``multi-threaded'';
  42. * the second type of server ``single-threaded''.
  43. *
  44. * Inetd uses a configuration file which is read at startup
  45. * and, possibly, at some later time in response to a hangup signal.
  46. * The configuration file is ``free format'' with fields given in the
  47. * order shown below. Continuation lines for an entry must being with
  48. * a space or tab. All fields must be present in each entry.
  49. *
  50. * service name must be in /etc/services
  51. * socket type stream/dgram/raw/rdm/seqpacket
  52. * protocol must be in /etc/protocols
  53. * wait/nowait[.max] single-threaded/multi-threaded, max #
  54. * user[.group] user/group to run daemon as
  55. * server program full path name
  56. * server program arguments maximum of MAXARGS (20)
  57. *
  58. * RPC services unsupported
  59. *
  60. * Comment lines are indicated by a `#' in column 1.
  61. */
  62. /*
  63. * Here's the scoop concerning the user.group feature:
  64. *
  65. * 1) No group listed.
  66. *
  67. * a) for root: NO setuid() or setgid() is done
  68. *
  69. * b) nonroot: setuid()
  70. * setgid(primary group as found in passwd)
  71. * initgroups(name, primary group)
  72. *
  73. * 2) set-group-option on.
  74. *
  75. * a) for root: NO setuid()
  76. * setgid(specified group)
  77. * setgroups(1, specified group)
  78. *
  79. * b) nonroot: setuid()
  80. * setgid(specified group)
  81. * initgroups(name, specified group)
  82. *
  83. * All supplementary groups are discarded at startup in case inetd was
  84. * run manually.
  85. */
  86. #define __USE_BSD_SIGNAL
  87. #include "busybox.h"
  88. #ifndef __linux__
  89. #ifndef RLIMIT_NOFILE
  90. #define RLIMIT_NOFILE RLIMIT_OFILE
  91. #endif
  92. #endif
  93. #include <sys/file.h>
  94. #include <sys/ioctl.h>
  95. #include <sys/param.h>
  96. #include <sys/resource.h>
  97. #include <sys/socket.h>
  98. #include <sys/stat.h>
  99. #include <sys/time.h>
  100. #include <sys/un.h>
  101. #include <sys/wait.h>
  102. #include <netinet/in.h>
  103. #include <netinet/ip.h>
  104. #include <arpa/inet.h>
  105. #include <errno.h>
  106. #include <signal.h>
  107. #include <netdb.h>
  108. #include <syslog.h>
  109. #include <stdio.h>
  110. #include <stdlib.h>
  111. #include <string.h>
  112. #include <getopt.h>
  113. #include <unistd.h>
  114. #include <stdarg.h>
  115. #include <time.h>
  116. #ifndef OPEN_MAX
  117. #define OPEN_MAX 64
  118. #endif
  119. #define _PATH_INETDCONF "/etc/inetd.conf"
  120. #define _PATH_INETDPID "/var/run/inetd.pid"
  121. #define TOOMANY 40 /* don't start more than TOOMANY */
  122. #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
  123. #define RETRYTIME (60*10) /* retry after bind or server fail */
  124. #define MAXARGV 20
  125. #define se_ctrladdr se_un.se_un_ctrladdr
  126. #define se_ctrladdr_in se_un.se_un_ctrladdr_in
  127. #define se_ctrladdr_un se_un.se_un_ctrladdr_un
  128. /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
  129. #define FD_MARGIN (8)
  130. /* Check unsupporting builtin */
  131. #if defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO || \
  132. defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD || \
  133. defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME || \
  134. defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME || \
  135. defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
  136. # define INETD_FEATURE_ENABLED
  137. #endif
  138. typedef struct servtab_s {
  139. char *se_service; /* name of service */
  140. int se_socktype; /* type of socket to use */
  141. int se_family; /* address family */
  142. char *se_proto; /* protocol used */
  143. short se_wait; /* single threaded server */
  144. short se_checked; /* looked at during merge */
  145. char *se_user; /* user name to run as */
  146. char *se_group; /* group name to run as */
  147. #ifdef INETD_FEATURE_ENABLED
  148. const struct biltin *se_bi; /* if built-in, description */
  149. #endif
  150. char *se_server; /* server program */
  151. char *se_argv[MAXARGV+1]; /* program arguments */
  152. int se_fd; /* open descriptor */
  153. union {
  154. struct sockaddr se_un_ctrladdr;
  155. struct sockaddr_in se_un_ctrladdr_in;
  156. struct sockaddr_un se_un_ctrladdr_un;
  157. } se_un; /* bound address */
  158. int se_ctrladdr_size;
  159. int se_max; /* max # of instances of this service */
  160. int se_count; /* number started since se_time */
  161. struct timeval se_time; /* start of se_count */
  162. struct servtab_s *se_next;
  163. } servtab_t;
  164. static servtab_t *servtab;
  165. #ifdef INETD_FEATURE_ENABLED
  166. struct biltin {
  167. const char *bi_service; /* internally provided service name */
  168. int bi_socktype; /* type of socket supported */
  169. short bi_fork; /* 1 if should fork before call */
  170. short bi_wait; /* 1 if should wait for child */
  171. void (*bi_fn)(int, servtab_t *); /* fn which performs it */
  172. };
  173. /* Echo received data */
  174. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
  175. static void echo_stream(int, servtab_t *);
  176. static void echo_dg(int, servtab_t *);
  177. #endif
  178. /* Internet /dev/null */
  179. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
  180. static void discard_stream(int, servtab_t *);
  181. static void discard_dg(int, servtab_t *);
  182. #endif
  183. /* Return 32 bit time since 1900 */
  184. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
  185. static void machtime_stream(int, servtab_t *);
  186. static void machtime_dg(int, servtab_t *);
  187. #endif
  188. /* Return human-readable time */
  189. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
  190. static void daytime_stream(int, servtab_t *);
  191. static void daytime_dg(int, servtab_t *);
  192. #endif
  193. /* Familiar character generator */
  194. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
  195. static void chargen_stream(int, servtab_t *);
  196. static void chargen_dg(int, servtab_t *);
  197. #endif
  198. static const struct biltin biltins[] = {
  199. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
  200. /* Echo received data */
  201. { "echo", SOCK_STREAM, 1, 0, echo_stream, },
  202. { "echo", SOCK_DGRAM, 0, 0, echo_dg, },
  203. #endif
  204. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
  205. /* Internet /dev/null */
  206. { "discard", SOCK_STREAM, 1, 0, discard_stream, },
  207. { "discard", SOCK_DGRAM, 0, 0, discard_dg, },
  208. #endif
  209. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
  210. /* Return 32 bit time since 1900 */
  211. { "time", SOCK_STREAM, 0, 0, machtime_stream, },
  212. { "time", SOCK_DGRAM, 0, 0, machtime_dg, },
  213. #endif
  214. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
  215. /* Return human-readable time */
  216. { "daytime", SOCK_STREAM, 0, 0, daytime_stream, },
  217. { "daytime", SOCK_DGRAM, 0, 0, daytime_dg, },
  218. #endif
  219. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
  220. /* Familiar character generator */
  221. { "chargen", SOCK_STREAM, 1, 0, chargen_stream, },
  222. { "chargen", SOCK_DGRAM, 0, 0, chargen_dg, },
  223. #endif
  224. { NULL, 0, 0, 0, NULL }
  225. };
  226. #endif /* INETD_FEATURE_ENABLED */
  227. #ifdef RLIMIT_NOFILE
  228. static struct rlimit rlim_ofile;
  229. #endif
  230. /* Length of socket listen queue. Should be per-service probably. */
  231. static int global_queuelen = 128;
  232. static FILE *fconfig;
  233. static sigset_t blockmask;
  234. static sigset_t emptymask;
  235. static fd_set allsock;
  236. static int nsock;
  237. static int maxsock;
  238. static int timingout;
  239. static int rlim_ofile_cur = OPEN_MAX;
  240. static const char *CONFIG = _PATH_INETDCONF;
  241. static void
  242. syslog_err_and_discard_dg(int se_socktype, const char *msg, ...)
  243. __attribute__ ((noreturn, format (printf, 2, 3)));
  244. static void
  245. syslog_err_and_discard_dg(int se_socktype, const char *msg, ...)
  246. {
  247. char buf[50];
  248. va_list p;
  249. va_start(p, msg);
  250. vsyslog(LOG_ERR, msg, p);
  251. if (se_socktype != SOCK_STREAM)
  252. recv(0, buf, sizeof (buf), 0);
  253. _exit(1);
  254. }
  255. static char * inetd_strdup(const char *s)
  256. {
  257. char *ms = strdup(s);
  258. if(ms == NULL)
  259. syslog_err_and_discard_dg(SOCK_STREAM, "strdup: %m");
  260. return ms;
  261. }
  262. static servtab_t *getconfigent(void)
  263. {
  264. static servtab_t serv;
  265. servtab_t *sep = &serv;
  266. int argc;
  267. char *cp = NULL;
  268. char *cp_ptr;
  269. char *cp_ptr_ptr = NULL;
  270. more:
  271. free(cp);
  272. cp = bb_get_chomped_line_from_file(fconfig);
  273. if (feof(fconfig)) {
  274. free(cp);
  275. return (NULL);
  276. }
  277. if ((cp == NULL) || (*cp == '#')) {
  278. goto more;
  279. }
  280. /* make bind 0.0.0.0 and other zero default */
  281. memset((char *)sep, 0, sizeof *sep);
  282. cp_ptr = strtok_r(cp, " \t", &cp_ptr_ptr);
  283. if (cp_ptr == NULL) {
  284. /* Error */
  285. goto more;
  286. }
  287. sep->se_service = inetd_strdup(cp_ptr);
  288. cp_ptr = strtok_r(NULL, " \t", &cp_ptr_ptr);
  289. if (cp_ptr == NULL) {
  290. /* Error */
  291. goto more;
  292. }
  293. if (strcmp(cp_ptr, "stream") == 0)
  294. sep->se_socktype = SOCK_STREAM;
  295. else if (strcmp(cp_ptr, "dgram") == 0)
  296. sep->se_socktype = SOCK_DGRAM;
  297. else if (strcmp(cp_ptr, "rdm") == 0)
  298. sep->se_socktype = SOCK_RDM;
  299. else if (strcmp(cp_ptr, "seqpacket") == 0)
  300. sep->se_socktype = SOCK_SEQPACKET;
  301. else if (strcmp(cp_ptr, "raw") == 0)
  302. sep->se_socktype = SOCK_RAW;
  303. else
  304. sep->se_socktype = -1;
  305. cp_ptr = strtok_r(NULL, " \t", &cp_ptr_ptr);
  306. if (cp_ptr == NULL) {
  307. /* error */
  308. goto more;
  309. }
  310. if (strcmp(cp_ptr, "unix") == 0) {
  311. sep->se_family = AF_UNIX;
  312. } else {
  313. if (strncmp(cp_ptr, "rpc/", 4) == 0) {
  314. syslog(LOG_ERR, "%s: rpc services not supported",
  315. sep->se_service);
  316. goto more;
  317. }
  318. sep->se_family = AF_INET;
  319. }
  320. sep->se_proto = inetd_strdup(cp_ptr);
  321. cp_ptr = strtok_r(NULL, " \t", &cp_ptr_ptr);
  322. if (cp_ptr == NULL) {
  323. /* error */
  324. goto more;
  325. }
  326. {
  327. char *s = strchr(cp_ptr, '.');
  328. if (s) {
  329. *s++ = '\0';
  330. sep->se_max = atoi(s);
  331. } else
  332. sep->se_max = TOOMANY;
  333. }
  334. sep->se_wait = strcmp(cp_ptr, "wait") == 0;
  335. cp_ptr = strtok_r(NULL, " \t", &cp_ptr_ptr);
  336. if (cp_ptr == NULL) {
  337. /* error */
  338. goto more;
  339. }
  340. sep->se_user = inetd_strdup(cp_ptr);
  341. {
  342. char *cp_ptr2 = strchr(sep->se_user, '.');
  343. if (cp_ptr2) {
  344. *cp_ptr2++ = '\0';
  345. }
  346. sep->se_group = cp_ptr2;
  347. }
  348. cp_ptr = strtok_r(NULL, " \t", &cp_ptr_ptr);
  349. if (cp_ptr == NULL) {
  350. /* error */
  351. goto more;
  352. }
  353. if (strcmp(cp_ptr, "internal") == 0) {
  354. #ifdef INETD_FEATURE_ENABLED
  355. const struct biltin *bi;
  356. for (bi = biltins; bi->bi_service; bi++) {
  357. if ((bi->bi_socktype == sep->se_socktype) &&
  358. (strcmp(bi->bi_service, sep->se_service) == 0)) {
  359. break;
  360. }
  361. }
  362. if (bi->bi_service == 0) {
  363. syslog(LOG_ERR, "internal service %s unknown", sep->se_service);
  364. goto more;
  365. }
  366. sep->se_bi = bi;
  367. sep->se_wait = bi->bi_wait;
  368. #else
  369. syslog(LOG_ERR, "internal service %s unknown", cp_ptr);
  370. goto more;
  371. #endif
  372. }
  373. #ifdef INETD_FEATURE_ENABLED
  374. else {
  375. sep->se_bi = NULL;
  376. }
  377. #endif
  378. sep->se_server = inetd_strdup(cp_ptr);
  379. argc = 0;
  380. while ((cp_ptr = strtok_r(NULL, " \t", &cp_ptr_ptr)) != NULL) {
  381. if (argc < MAXARGV) {
  382. sep->se_argv[argc++] = inetd_strdup(cp_ptr);
  383. }
  384. }
  385. free(cp);
  386. return (sep);
  387. }
  388. static void freeconfig(servtab_t *cp)
  389. {
  390. int i;
  391. free(cp->se_service);
  392. free(cp->se_proto);
  393. free(cp->se_user);
  394. /* Note: se_group is part of the newstr'ed se_user */
  395. free(cp->se_server);
  396. for (i = 0; i < MAXARGV; i++)
  397. free(cp->se_argv[i]);
  398. }
  399. #ifdef INETD_FEATURE_ENABLED
  400. static char **Argv;
  401. static char *LastArg;
  402. static void setproctitle(char *a, int s)
  403. {
  404. size_t size;
  405. char *cp;
  406. struct sockaddr_in sn;
  407. char buf[80];
  408. cp = Argv[0];
  409. size = sizeof(sn);
  410. if (getpeername(s, (struct sockaddr *)&sn, &size) == 0)
  411. (void) sprintf(buf, "-%s [%s]", a, inet_ntoa(sn.sin_addr));
  412. else
  413. (void) sprintf(buf, "-%s", a);
  414. strncpy(cp, buf, LastArg - cp);
  415. cp += strlen(cp);
  416. while (cp < LastArg)
  417. *cp++ = ' ';
  418. }
  419. #endif /* INETD_FEATURE_ENABLED */
  420. static void setup(servtab_t *sep)
  421. {
  422. int on = 1;
  423. if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
  424. syslog(LOG_ERR, "%s/%s: socket: %m",
  425. sep->se_service, sep->se_proto);
  426. return;
  427. }
  428. if (setsockopt(sep->se_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
  429. sizeof(on)) < 0)
  430. syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
  431. if (bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size) < 0) {
  432. syslog(LOG_ERR, "%s/%s: bind: %m",
  433. sep->se_service, sep->se_proto);
  434. (void) close(sep->se_fd);
  435. sep->se_fd = -1;
  436. if (!timingout) {
  437. timingout = 1;
  438. alarm(RETRYTIME);
  439. }
  440. return;
  441. }
  442. if (sep->se_socktype == SOCK_STREAM)
  443. listen(sep->se_fd, global_queuelen);
  444. FD_SET(sep->se_fd, &allsock);
  445. nsock++;
  446. if (sep->se_fd > maxsock) {
  447. maxsock = sep->se_fd;
  448. if (maxsock > rlim_ofile_cur - FD_MARGIN) {
  449. #ifdef RLIMIT_NOFILE
  450. # define FD_CHUNK 32
  451. struct rlimit rl;
  452. if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
  453. syslog(LOG_ERR, "getrlimit: %m");
  454. return;
  455. }
  456. rl.rlim_cur = rl.rlim_max < (rl.rlim_cur + FD_CHUNK) ? rl.rlim_max : (rl.rlim_cur + FD_CHUNK);
  457. if (rl.rlim_cur <= rlim_ofile_cur) {
  458. syslog(LOG_ERR,
  459. # if _FILE_OFFSET_BITS == 64
  460. "bump_nofile: cannot extend file limit, max = %lld",
  461. # else
  462. "bump_nofile: cannot extend file limit, max = %ld",
  463. # endif
  464. rl.rlim_cur);
  465. return;
  466. }
  467. if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
  468. syslog(LOG_ERR, "setrlimit: %m");
  469. return;
  470. }
  471. rlim_ofile_cur = rl.rlim_cur;
  472. return;
  473. #else
  474. syslog(LOG_ERR, "bump_nofile: cannot extend file limit");
  475. return;
  476. #endif /* RLIMIT_NOFILE */
  477. }
  478. }
  479. }
  480. static void config(int signum)
  481. {
  482. servtab_t *sep, *cp, **sepp;
  483. sigset_t oldmask;
  484. unsigned n;
  485. (void)signum;
  486. if (fconfig != NULL) {
  487. fseek(fconfig, 0L, L_SET);
  488. } else {
  489. fconfig = fopen(CONFIG, "r");
  490. if (fconfig == NULL) {
  491. syslog(LOG_ERR, "%s: %m", CONFIG);
  492. return;
  493. }
  494. }
  495. for (sep = servtab; sep; sep = sep->se_next)
  496. sep->se_checked = 0;
  497. while ((cp = getconfigent()) != NULL) {
  498. for (sep = servtab; sep; sep = sep->se_next)
  499. if (strcmp(sep->se_service, cp->se_service) == 0 &&
  500. strcmp(sep->se_proto, cp->se_proto) == 0)
  501. break;
  502. if (sep != 0) {
  503. int i;
  504. #define SWAP(type, a, b) {type c=(type)(a); (a)=(type)(b); (b)=(type)c;}
  505. sigprocmask(SIG_BLOCK, &emptymask, &oldmask);
  506. /*
  507. * sep->se_wait may be holding the pid of a daemon
  508. * that we're waiting for. If so, don't overwrite
  509. * it unless the config file explicitly says don't
  510. * wait.
  511. */
  512. if (
  513. #ifdef INETD_FEATURE_ENABLED
  514. cp->se_bi == 0 &&
  515. #endif
  516. (sep->se_wait == 1 || cp->se_wait == 0))
  517. sep->se_wait = cp->se_wait;
  518. if (cp->se_max != sep->se_max)
  519. SWAP(int, cp->se_max, sep->se_max);
  520. if (cp->se_user)
  521. SWAP(char *, sep->se_user, cp->se_user);
  522. if (cp->se_group)
  523. SWAP(char *, sep->se_group, cp->se_group);
  524. if (cp->se_server)
  525. SWAP(char *, sep->se_server, cp->se_server);
  526. for (i = 0; i < MAXARGV; i++)
  527. SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
  528. #undef SWAP
  529. sigprocmask(SIG_SETMASK, &oldmask, NULL);
  530. // This freeconfig() is probably a bug, since it will try and free()
  531. // each of the argv[] values, which are really just pointers
  532. // into the middle of a single line buffer for the config file.
  533. //freeconfig(cp); // BUG?
  534. } else {
  535. sep = (servtab_t *)xmalloc(sizeof (*sep));
  536. *sep = *cp;
  537. sep->se_fd = -1;
  538. sigprocmask(SIG_BLOCK, &blockmask, &oldmask);
  539. sep->se_next = servtab;
  540. servtab = sep;
  541. sigprocmask(SIG_SETMASK, &oldmask, NULL);
  542. }
  543. sep->se_checked = 1;
  544. switch (sep->se_family) {
  545. case AF_UNIX:
  546. if (sep->se_fd != -1)
  547. break;
  548. (void)unlink(sep->se_service);
  549. n = strlen(sep->se_service);
  550. if (n > sizeof(sep->se_ctrladdr_un.sun_path) - 1)
  551. n = sizeof(sep->se_ctrladdr_un.sun_path) - 1;
  552. strncpy(sep->se_ctrladdr_un.sun_path, sep->se_service, n);
  553. sep->se_ctrladdr_un.sun_family = AF_UNIX;
  554. sep->se_ctrladdr_size = n +
  555. sizeof sep->se_ctrladdr_un.sun_family;
  556. setup(sep);
  557. break;
  558. case AF_INET:
  559. sep->se_ctrladdr_in.sin_family = AF_INET;
  560. sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in;
  561. {
  562. u_short port = bb_lookup_port(sep->se_service, sep->se_proto, 0);
  563. if (port == 0) {
  564. syslog(LOG_ERR,
  565. "%s/%s: unknown service",
  566. sep->se_service, sep->se_proto);
  567. continue;
  568. }
  569. if (port != sep->se_ctrladdr_in.sin_port) {
  570. sep->se_ctrladdr_in.sin_port = port;
  571. if (sep->se_fd != -1) {
  572. FD_CLR(sep->se_fd, &allsock);
  573. nsock--;
  574. (void) close(sep->se_fd);
  575. }
  576. sep->se_fd = -1;
  577. }
  578. if (sep->se_fd == -1)
  579. setup(sep);
  580. }
  581. }
  582. }
  583. if (fconfig) {
  584. (void) fclose(fconfig);
  585. fconfig = NULL;
  586. }
  587. /*
  588. * Purge anything not looked at above.
  589. */
  590. sigprocmask(SIG_SETMASK, &blockmask, &oldmask);
  591. sepp = &servtab;
  592. while ((sep = *sepp) != NULL) {
  593. if (sep->se_checked) {
  594. sepp = &sep->se_next;
  595. continue;
  596. }
  597. *sepp = sep->se_next;
  598. if (sep->se_fd != -1) {
  599. FD_CLR(sep->se_fd, &allsock);
  600. nsock--;
  601. (void) close(sep->se_fd);
  602. }
  603. if (sep->se_family == AF_UNIX)
  604. (void)unlink(sep->se_service);
  605. freeconfig(sep);
  606. free((char *)sep);
  607. }
  608. sigprocmask(SIG_SETMASK, &oldmask, NULL);
  609. }
  610. static void reapchild(int signum)
  611. {
  612. int status;
  613. int pid;
  614. servtab_t *sep;
  615. (void)signum;
  616. for (;;) {
  617. pid = wait3(&status, WNOHANG, (struct rusage *)0);
  618. if (pid <= 0)
  619. break;
  620. for (sep = servtab; sep; sep = sep->se_next)
  621. if (sep->se_wait == pid) {
  622. if (WIFEXITED(status) && WEXITSTATUS(status))
  623. syslog(LOG_WARNING,
  624. "%s: exit status 0x%x",
  625. sep->se_server, WEXITSTATUS(status));
  626. else if (WIFSIGNALED(status))
  627. syslog(LOG_WARNING,
  628. "%s: exit signal 0x%x",
  629. sep->se_server, WTERMSIG(status));
  630. sep->se_wait = 1;
  631. FD_SET(sep->se_fd, &allsock);
  632. nsock++;
  633. }
  634. }
  635. }
  636. static void retry(int signum)
  637. {
  638. servtab_t *sep;
  639. (void)signum;
  640. timingout = 0;
  641. for (sep = servtab; sep; sep = sep->se_next) {
  642. if (sep->se_fd == -1) {
  643. switch (sep->se_family) {
  644. case AF_UNIX:
  645. case AF_INET:
  646. setup(sep);
  647. break;
  648. }
  649. }
  650. }
  651. }
  652. static void goaway(int signum)
  653. {
  654. servtab_t *sep;
  655. (void)signum;
  656. for (sep = servtab; sep; sep = sep->se_next)
  657. if (sep->se_fd != -1 && sep->se_family == AF_UNIX)
  658. (void)unlink(sep->se_service);
  659. (void)unlink(_PATH_INETDPID);
  660. exit(0);
  661. }
  662. extern int inetd_main(int argc, char *argv[])
  663. {
  664. servtab_t *sep;
  665. struct group *grp = NULL;
  666. struct sigaction sa;
  667. int pid;
  668. unsigned long opt;
  669. char *sq;
  670. gid_t gid;
  671. #ifdef INETD_FEATURE_ENABLED
  672. extern char **environ;
  673. #endif
  674. gid = getgid();
  675. setgroups(1, &gid);
  676. #ifdef INETD_FEATURE_ENABLED
  677. Argv = argv;
  678. if (environ == 0 || *environ == 0)
  679. environ = argv;
  680. while (*environ)
  681. environ++;
  682. LastArg = environ[-1] + strlen(environ[-1]);
  683. #endif
  684. #if defined(__uClinux__)
  685. opt = bb_getopt_ulflags(argc, argv, "q:f", &sq);
  686. if (!(opt & 2)) {
  687. daemon(0, 0);
  688. /* reexec for vfork() do continue parent */
  689. vfork_daemon_rexec(argc, argv, "-f");
  690. }
  691. #else
  692. opt = bb_getopt_ulflags(argc, argv, "q:", &sq);
  693. daemon(0, 0);
  694. #endif /* uClinux */
  695. if(opt & 1) {
  696. global_queuelen = atoi(sq);
  697. if (global_queuelen < 8) global_queuelen=8;
  698. }
  699. argc -= optind;
  700. argv += optind;
  701. if (argc > 0)
  702. CONFIG = argv[0];
  703. openlog(bb_applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
  704. {
  705. FILE *fp;
  706. if ((fp = fopen(_PATH_INETDPID, "w")) != NULL) {
  707. fprintf(fp, "%u\n", getpid());
  708. (void)fclose(fp);
  709. }
  710. }
  711. #ifdef RLIMIT_NOFILE
  712. if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
  713. syslog(LOG_ERR, "getrlimit: %m");
  714. } else {
  715. rlim_ofile_cur = rlim_ofile.rlim_cur;
  716. if (rlim_ofile_cur == RLIM_INFINITY) /* ! */
  717. rlim_ofile_cur = OPEN_MAX;
  718. }
  719. #endif
  720. config(0);
  721. sigemptyset(&emptymask);
  722. sigemptyset(&blockmask);
  723. sigaddset(&blockmask, SIGCHLD);
  724. sigaddset(&blockmask, SIGHUP);
  725. sigaddset(&blockmask, SIGALRM);
  726. memset(&sa, 0, sizeof(sa));
  727. sa.sa_mask = blockmask;
  728. sa.sa_handler = retry;
  729. sigaction(SIGALRM, &sa, NULL);
  730. sa.sa_handler = config;
  731. sigaction(SIGHUP, &sa, NULL);
  732. sa.sa_handler = reapchild;
  733. sigaction(SIGCHLD, &sa, NULL);
  734. sa.sa_handler = goaway;
  735. sigaction(SIGTERM, &sa, NULL);
  736. sa.sa_handler = goaway;
  737. sigaction(SIGINT, &sa, NULL);
  738. sa.sa_handler = SIG_IGN;
  739. sigaction(SIGPIPE, &sa, NULL);
  740. {
  741. /* space for daemons to overwrite environment for ps */
  742. #define DUMMYSIZE 100
  743. char dummy[DUMMYSIZE];
  744. (void)memset(dummy, 'x', DUMMYSIZE - 1);
  745. dummy[DUMMYSIZE - 1] = '\0';
  746. (void)setenv("inetd_dummy", dummy, 1);
  747. }
  748. for (;;) {
  749. fd_set readable;
  750. int ctrl;
  751. int n;
  752. if (nsock == 0) {
  753. sigprocmask(SIG_BLOCK, &blockmask, NULL);
  754. while (nsock == 0) {
  755. sigsuspend(&emptymask);
  756. }
  757. sigprocmask(SIG_SETMASK, &emptymask, NULL);
  758. }
  759. readable = allsock;
  760. n = select(maxsock + 1, &readable, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
  761. if (n <= 0) {
  762. if (n < 0 && errno != EINTR) {
  763. syslog(LOG_WARNING, "select: %m");
  764. }
  765. sleep(1);
  766. continue;
  767. }
  768. for (sep = servtab; n && sep; sep = sep->se_next) {
  769. if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
  770. n--;
  771. if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
  772. /* Fixed AGC */
  773. fcntl(sep->se_fd, F_SETFL, O_NDELAY);
  774. /* --------- */
  775. ctrl = accept(sep->se_fd, NULL, NULL);
  776. fcntl(sep->se_fd, F_SETFL, 0);
  777. if (ctrl < 0) {
  778. if (errno == EINTR || errno == EWOULDBLOCK) {
  779. continue;
  780. }
  781. syslog(LOG_WARNING, "accept (for %s): %m",
  782. sep->se_service);
  783. continue;
  784. }
  785. } else {
  786. ctrl = sep->se_fd;
  787. }
  788. sigprocmask(SIG_BLOCK, &blockmask, NULL);
  789. pid = 0;
  790. #ifdef INETD_FEATURE_ENABLED
  791. if (sep->se_bi == 0 || sep->se_bi->bi_fork)
  792. #endif
  793. {
  794. if (sep->se_count++ == 0) {
  795. gettimeofday(&sep->se_time, (struct timezone *)0);
  796. }
  797. else if (sep->se_count >= sep->se_max) {
  798. struct timeval now;
  799. gettimeofday(&now, (struct timezone *)0);
  800. if (now.tv_sec - sep->se_time.tv_sec > CNT_INTVL) {
  801. sep->se_time = now;
  802. sep->se_count = 1;
  803. } else {
  804. syslog(LOG_ERR,
  805. "%s/%s server failing (looping), service terminated",
  806. sep->se_service, sep->se_proto);
  807. FD_CLR(sep->se_fd, &allsock);
  808. close(sep->se_fd);
  809. sep->se_fd = -1;
  810. sep->se_count = 0;
  811. nsock--;
  812. sigprocmask(SIG_SETMASK, &emptymask, NULL);
  813. if (!timingout) {
  814. timingout = 1;
  815. alarm(RETRYTIME);
  816. }
  817. continue;
  818. }
  819. }
  820. pid = fork();
  821. if (pid < 0) {
  822. syslog(LOG_ERR, "fork: %m");
  823. if (sep->se_socktype == SOCK_STREAM) {
  824. close(ctrl);
  825. }
  826. sigprocmask(SIG_SETMASK, &emptymask, NULL);
  827. sleep(1);
  828. continue;
  829. }
  830. if (pid && sep->se_wait) {
  831. sep->se_wait = pid;
  832. FD_CLR(sep->se_fd, &allsock);
  833. nsock--;
  834. }
  835. }
  836. sigprocmask(SIG_SETMASK, &emptymask, NULL);
  837. if (pid == 0) {
  838. #ifdef INETD_FEATURE_ENABLED
  839. if (sep->se_bi) {
  840. (*sep->se_bi->bi_fn)(ctrl, sep);
  841. } else
  842. #endif
  843. {
  844. struct passwd *pwd = getpwnam(sep->se_user);
  845. if (pwd == NULL) {
  846. syslog_err_and_discard_dg(
  847. sep->se_socktype,
  848. "getpwnam: %s: No such user",
  849. sep->se_user);
  850. }
  851. if (sep->se_group && (grp = getgrnam(sep->se_group)) == NULL) {
  852. syslog_err_and_discard_dg(sep->se_socktype,
  853. "getgrnam: %s: No such group", sep->se_group);
  854. }
  855. /*
  856. * Ok. There are four cases here:
  857. * 1. nonroot user, no group specified
  858. * 2. nonroot user, some group specified
  859. * 3. root user, no group specified
  860. * 4. root user, some group specified
  861. * In cases 2 and 4 we setgid to the specified
  862. * group. In cases 1 and 2 we run initgroups
  863. * to run with the groups of the given user.
  864. * In case 4 we do setgroups to run with the
  865. * given group. In case 3 we do nothing.
  866. */
  867. if (pwd->pw_uid) {
  868. if (sep->se_group) {
  869. pwd->pw_gid = grp->gr_gid;
  870. }
  871. setgid((gid_t)pwd->pw_gid);
  872. initgroups(pwd->pw_name, pwd->pw_gid);
  873. setuid((uid_t)pwd->pw_uid);
  874. } else if (sep->se_group) {
  875. setgid((gid_t)grp->gr_gid);
  876. setgroups(1, &grp->gr_gid);
  877. }
  878. dup2(ctrl, 0);
  879. close(ctrl);
  880. dup2(0, 1);
  881. dup2(0, 2);
  882. #ifdef RLIMIT_NOFILE
  883. if (rlim_ofile.rlim_cur != rlim_ofile_cur) {
  884. if (setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
  885. syslog(LOG_ERR,"setrlimit: %m");
  886. }
  887. }
  888. #endif
  889. for (ctrl = rlim_ofile_cur-1; --ctrl > 2; ) {
  890. (void)close(ctrl);
  891. }
  892. memset(&sa, 0, sizeof(sa));
  893. sa.sa_handler = SIG_DFL;
  894. sigaction(SIGPIPE, &sa, NULL);
  895. execv(sep->se_server, sep->se_argv);
  896. syslog_err_and_discard_dg(sep->se_socktype, "execv %s: %m", sep->se_server);
  897. }
  898. }
  899. if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
  900. close(ctrl);
  901. }
  902. }
  903. }
  904. }
  905. }
  906. /*
  907. * Internet services provided internally by inetd:
  908. */
  909. #define BUFSIZE 4096
  910. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
  911. /* Echo service -- echo data back */
  912. static void echo_stream(int s, servtab_t *sep)
  913. {
  914. char buffer[BUFSIZE];
  915. int i;
  916. setproctitle(sep->se_service, s);
  917. while ((i = read(s, buffer, sizeof(buffer))) > 0 &&
  918. write(s, buffer, i) > 0)
  919. ;
  920. exit(0);
  921. }
  922. /* Echo service -- echo data back */
  923. static void echo_dg(int s, servtab_t *sep)
  924. {
  925. char buffer[BUFSIZE];
  926. int i;
  927. size_t size;
  928. struct sockaddr sa;
  929. (void)sep;
  930. size = sizeof(sa);
  931. if ((i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size)) < 0)
  932. return;
  933. (void) sendto(s, buffer, i, 0, &sa, sizeof(sa));
  934. }
  935. #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO */
  936. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
  937. /* Discard service -- ignore data */
  938. static void discard_stream(int s, servtab_t *sep)
  939. {
  940. char buffer[BUFSIZE];
  941. setproctitle(sep->se_service, s);
  942. while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) ||
  943. errno == EINTR)
  944. ;
  945. exit(0);
  946. }
  947. /* Discard service -- ignore data */
  948. static void discard_dg(int s, servtab_t *sep)
  949. {
  950. char buffer[BUFSIZE];
  951. (void)sep;
  952. read(s, buffer, sizeof(buffer));
  953. }
  954. #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD */
  955. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
  956. #include <ctype.h>
  957. #define LINESIZ 72
  958. static char ring[128];
  959. static char *endring;
  960. static void initring(void)
  961. {
  962. int i;
  963. endring = ring;
  964. for (i = 0; i <= 128; ++i)
  965. if (isprint(i))
  966. *endring++ = i;
  967. }
  968. /* Character generator */
  969. static void chargen_stream(int s, servtab_t *sep)
  970. {
  971. char *rs;
  972. int len;
  973. char text[LINESIZ+2];
  974. setproctitle(sep->se_service, s);
  975. if (!endring) {
  976. initring();
  977. rs = ring;
  978. }
  979. text[LINESIZ] = '\r';
  980. text[LINESIZ + 1] = '\n';
  981. for (rs = ring;;) {
  982. if ((len = endring - rs) >= LINESIZ)
  983. memcpy(rs, text, LINESIZ);
  984. else {
  985. memcpy(rs, text, len);
  986. memcpy(ring, text + len, LINESIZ - len);
  987. }
  988. if (++rs == endring)
  989. rs = ring;
  990. if (write(s, text, sizeof(text)) != sizeof(text))
  991. break;
  992. }
  993. exit(0);
  994. }
  995. /* Character generator */
  996. static void chargen_dg(int s, servtab_t *sep)
  997. {
  998. struct sockaddr sa;
  999. static char *rs;
  1000. size_t len, size;
  1001. char text[LINESIZ+2];
  1002. (void)sep;
  1003. if (endring == 0) {
  1004. initring();
  1005. rs = ring;
  1006. }
  1007. size = sizeof(sa);
  1008. if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0)
  1009. return;
  1010. if ((len = endring - rs) >= LINESIZ)
  1011. memcpy(rs, text, LINESIZ);
  1012. else {
  1013. memcpy(rs, text, len);
  1014. memcpy(ring, text + len, LINESIZ - len);
  1015. }
  1016. if (++rs == endring)
  1017. rs = ring;
  1018. text[LINESIZ] = '\r';
  1019. text[LINESIZ + 1] = '\n';
  1020. (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa));
  1021. }
  1022. #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN */
  1023. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
  1024. /*
  1025. * Return a machine readable date and time, in the form of the
  1026. * number of seconds since midnight, Jan 1, 1900. Since gettimeofday
  1027. * returns the number of seconds since midnight, Jan 1, 1970,
  1028. * we must add 2208988800 seconds to this figure to make up for
  1029. * some seventy years Bell Labs was asleep.
  1030. */
  1031. static long machtime(void)
  1032. {
  1033. struct timeval tv;
  1034. if (gettimeofday(&tv, (struct timezone *)0) < 0) {
  1035. fprintf(stderr, "Unable to get time of day\n");
  1036. return (0L);
  1037. }
  1038. return (htonl((long)tv.tv_sec + 2208988800UL));
  1039. }
  1040. static void machtime_stream(int s, servtab_t *sep)
  1041. {
  1042. long result;
  1043. (void)sep;
  1044. result = machtime();
  1045. write(s, (char *) &result, sizeof(result));
  1046. }
  1047. static void machtime_dg(int s, servtab_t *sep)
  1048. {
  1049. long result;
  1050. struct sockaddr sa;
  1051. size_t size;
  1052. (void)sep;
  1053. size = sizeof(sa);
  1054. if (recvfrom(s, (char *)&result, sizeof(result), 0, &sa, &size) < 0)
  1055. return;
  1056. result = machtime();
  1057. (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa));
  1058. }
  1059. #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME */
  1060. #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
  1061. /* Return human-readable time of day */
  1062. static int human_readable_time_sprintf(char *buffer)
  1063. {
  1064. time_t clocc = time(NULL);
  1065. return sprintf(buffer, "%.24s\r\n", ctime(&clocc));
  1066. }
  1067. static void daytime_stream(int s, servtab_t *sep)
  1068. {
  1069. char buffer[256];
  1070. size_t st = human_readable_time_sprintf(buffer);
  1071. (void)sep;
  1072. write(s, buffer, st);
  1073. }
  1074. /* Return human-readable time of day */
  1075. static void daytime_dg(int s, servtab_t *sep)
  1076. {
  1077. char buffer[256];
  1078. struct sockaddr sa;
  1079. size_t size;
  1080. (void)sep;
  1081. size = sizeof(sa);
  1082. if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0)
  1083. return;
  1084. size = human_readable_time_sprintf(buffer);
  1085. sendto(s, buffer, size, 0, &sa, sizeof(sa));
  1086. }
  1087. #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME */