tcpudp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /* Based on ipsvd utilities written by Gerrit Pape <pape@smarden.org>
  2. * which are released into public domain by the author.
  3. * Homepage: http://smarden.sunsite.dk/ipsvd/
  4. *
  5. * Copyright (C) 2007 Denys Vlasenko.
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. /* Based on ipsvd-0.12.1. This tcpsvd accepts all options
  10. * which are supported by one from ipsvd-0.12.1, but not all are
  11. * functional. See help text at the end of this file for details.
  12. *
  13. * Code inside "#ifdef SSLSVD" is for sslsvd and is currently unused.
  14. *
  15. * Busybox version exports TCPLOCALADDR instead of
  16. * TCPLOCALIP + TCPLOCALPORT pair. ADDR more closely matches reality
  17. * (which is "struct sockaddr_XXX". Port is not a separate entity,
  18. * it's just a part of (AF_INET[6]) sockaddr!).
  19. *
  20. * TCPORIGDSTADDR is Busybox-specific addition.
  21. *
  22. * udp server is hacked up by reusing TCP code. It has the following
  23. * limitation inherent in Unix DGRAM sockets implementation:
  24. * - local IP address is retrieved (using recvmsg voodoo) but
  25. * child's socket is not bound to it (bind cannot be called on
  26. * already bound socket). Thus it still can emit outgoing packets
  27. * with wrong source IP...
  28. * - don't know how to retrieve ORIGDST for udp.
  29. */
  30. //config:config TCPSVD
  31. //config: bool "tcpsvd (13 kb)"
  32. //config: default y
  33. //config: help
  34. //config: tcpsvd listens on a TCP port and runs a program for each new
  35. //config: connection.
  36. //config:
  37. //config:config UDPSVD
  38. //config: bool "udpsvd (13 kb)"
  39. //config: default y
  40. //config: help
  41. //config: udpsvd listens on an UDP port and runs a program for each new
  42. //config: connection.
  43. //applet:IF_TCPSVD(APPLET_ODDNAME(tcpsvd, tcpudpsvd, BB_DIR_USR_BIN, BB_SUID_DROP, tcpsvd))
  44. //applet:IF_UDPSVD(APPLET_ODDNAME(udpsvd, tcpudpsvd, BB_DIR_USR_BIN, BB_SUID_DROP, udpsvd))
  45. //kbuild:lib-$(CONFIG_TCPSVD) += tcpudp.o tcpudp_perhost.o
  46. //kbuild:lib-$(CONFIG_UDPSVD) += tcpudp.o tcpudp_perhost.o
  47. //usage:#define tcpsvd_trivial_usage
  48. //usage: "[-hEv] [-c N] [-C N[:MSG]] [-b N] [-u USER] [-l NAME] IP PORT PROG"
  49. /* with not-implemented options: */
  50. /* //usage: "[-hpEvv] [-c N] [-C N[:MSG]] [-b N] [-u USER] [-l NAME] [-i DIR|-x CDB] [-t SEC] IP PORT PROG" */
  51. //usage:#define tcpsvd_full_usage "\n\n"
  52. //usage: "Create TCP socket, bind to IP:PORT and listen for incoming connections.\n"
  53. //usage: "Run PROG for each connection.\n"
  54. //usage: "\n IP PORT IP:PORT to listen on"
  55. //usage: "\n PROG ARGS Program to run"
  56. //usage: "\n -u USER[:GRP] Change to user/group after bind"
  57. //usage: "\n -c N Up to N connections simultaneously (default 30)"
  58. //usage: "\n -b N Allow backlog of approximately N TCP SYNs (default 20)"
  59. //usage: "\n -C N[:MSG] Allow only up to N connections from the same IP:"
  60. //usage: "\n new connections from this IP address are closed"
  61. //usage: "\n immediately, MSG is written to the peer before close"
  62. //usage: "\n -E Don't set up environment"
  63. //usage: "\n -h Look up peer's hostname"
  64. //usage: "\n -l NAME Local hostname (else look up local hostname in DNS)"
  65. //usage: "\n -v Verbose"
  66. //usage: "\n"
  67. //usage: "\nEnvironment if no -E:"
  68. //usage: "\nPROTO='TCP'"
  69. //usage: "\nTCPREMOTEADDR='ip:port'" IF_FEATURE_IPV6(" ('[ip]:port' for IPv6)")
  70. //usage: "\nTCPLOCALADDR='ip:port'"
  71. //usage: "\nTCPORIGDSTADDR='ip:port' of destination before firewall"
  72. //usage: "\n Useful for REDIRECTed-to-local connections:"
  73. //usage: "\n iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080"
  74. //usage: "\nTCPCONCURRENCY=num_of_connects_from_this_ip"
  75. //usage: "\nIf -h:"
  76. //usage: "\nTCPLOCALHOST='hostname' (-l NAME is used if specified)"
  77. //usage: "\nTCPREMOTEHOST='hostname'"
  78. //usage:
  79. //usage:#define udpsvd_trivial_usage
  80. //usage: "[-hEv] [-c N] [-u USER] [-l NAME] IP PORT PROG"
  81. //usage:#define udpsvd_full_usage "\n\n"
  82. //usage: "Create UDP socket, bind to IP:PORT and wait for incoming packets.\n"
  83. //usage: "Run PROG for each packet, redirecting all further packets with same\n"
  84. //usage: "peer ip:port to it.\n"
  85. //usage: "\n IP PORT IP:PORT to listen on"
  86. //usage: "\n PROG ARGS Program to run"
  87. //usage: "\n -u USER[:GRP] Change to user/group after bind"
  88. //usage: "\n -c N Up to N connections simultaneously (default 30)"
  89. //usage: "\n -E Don't set up environment"
  90. //usage: "\n -h Look up peer's hostname"
  91. //usage: "\n -l NAME Local hostname (else look up local hostname in DNS)"
  92. //usage: "\n -v Verbose"
  93. //usage: "\n"
  94. //usage: "\nEnvironment if no -E:"
  95. //usage: "\nPROTO='UDP'"
  96. //usage: "\nUDPREMOTEADDR='ip:port'" IF_FEATURE_IPV6(" ('[ip]:port' for IPv6)")
  97. //usage: "\nUDPLOCALADDR='ip:port'"
  98. //usage: "\nIf -h:"
  99. //usage: "\nUDPLOCALHOST='hostname' (-l NAME is used if specified)"
  100. //usage: "\nUDPREMOTEHOST='hostname'"
  101. #include "libbb.h"
  102. #include "common_bufsiz.h"
  103. #ifdef __linux__
  104. /* from linux/netfilter_ipv4.h: */
  105. # undef SO_ORIGINAL_DST
  106. # define SO_ORIGINAL_DST 80
  107. #endif
  108. // TODO: move into this file:
  109. #include "tcpudp_perhost.h"
  110. #ifdef SSLSVD
  111. #include "matrixSsl.h"
  112. #include "ssl_io.h"
  113. #endif
  114. struct globals {
  115. unsigned verbose;
  116. unsigned max_per_host;
  117. unsigned cur_per_host;
  118. unsigned cnum;
  119. unsigned cmax;
  120. char **env_cur;
  121. char *env_var[1]; /* actually bigger */
  122. } FIX_ALIASING;
  123. #define G (*(struct globals*)bb_common_bufsiz1)
  124. #define verbose (G.verbose )
  125. #define max_per_host (G.max_per_host)
  126. #define cur_per_host (G.cur_per_host)
  127. #define cnum (G.cnum )
  128. #define cmax (G.cmax )
  129. #define env_cur (G.env_cur )
  130. #define env_var (G.env_var )
  131. #define INIT_G() do { \
  132. setup_common_bufsiz(); \
  133. cmax = 30; \
  134. env_cur = &env_var[0]; \
  135. } while (0)
  136. /* We have to be careful about leaking memory in repeated setenv's */
  137. static void xsetenv_plain(const char *n, const char *v)
  138. {
  139. char *var = xasprintf("%s=%s", n, v);
  140. *env_cur++ = var;
  141. putenv(var);
  142. }
  143. static void xsetenv_proto(const char *proto, const char *n, const char *v)
  144. {
  145. char *var = xasprintf("%s%s=%s", proto, n, v);
  146. *env_cur++ = var;
  147. putenv(var);
  148. }
  149. static void undo_xsetenv(void)
  150. {
  151. char **pp = env_cur = &env_var[0];
  152. while (*pp) {
  153. char *var = *pp;
  154. bb_unsetenv_and_free(var);
  155. *pp++ = NULL;
  156. }
  157. }
  158. static void sig_term_handler(int sig)
  159. {
  160. if (verbose)
  161. bb_error_msg("got signal %u, exit", sig);
  162. kill_myself_with_sig(sig);
  163. }
  164. /* Little bloated, but tries to give accurate info how child exited.
  165. * Makes easier to spot segfaulting children etc... */
  166. static void print_waitstat(unsigned pid, int wstat)
  167. {
  168. unsigned e = 0;
  169. const char *cause = "?exit";
  170. if (WIFEXITED(wstat)) {
  171. cause++;
  172. e = WEXITSTATUS(wstat);
  173. } else if (WIFSIGNALED(wstat)) {
  174. cause = "signal";
  175. e = WTERMSIG(wstat);
  176. }
  177. bb_error_msg("end %d %s %d", pid, cause, e);
  178. }
  179. /* Must match getopt32 in main! */
  180. enum {
  181. OPT_c = (1 << 0),
  182. OPT_C = (1 << 1),
  183. OPT_i = (1 << 2),
  184. OPT_x = (1 << 3),
  185. OPT_u = (1 << 4),
  186. OPT_l = (1 << 5),
  187. OPT_E = (1 << 6),
  188. OPT_b = (1 << 7),
  189. OPT_h = (1 << 8),
  190. OPT_p = (1 << 9),
  191. OPT_t = (1 << 10),
  192. OPT_v = (1 << 11),
  193. OPT_V = (1 << 12),
  194. OPT_U = (1 << 13), /* from here: sslsvd only */
  195. OPT_slash = (1 << 14),
  196. OPT_Z = (1 << 15),
  197. OPT_K = (1 << 16),
  198. };
  199. static void connection_status(void)
  200. {
  201. /* "only 1 client max" desn't need this */
  202. if (cmax > 1)
  203. bb_error_msg("status %u/%u", cnum, cmax);
  204. }
  205. static void sig_child_handler(int sig UNUSED_PARAM)
  206. {
  207. int wstat;
  208. pid_t pid;
  209. while ((pid = wait_any_nohang(&wstat)) > 0) {
  210. if (max_per_host)
  211. ipsvd_perhost_remove(pid);
  212. if (cnum)
  213. cnum--;
  214. if (verbose)
  215. print_waitstat(pid, wstat);
  216. }
  217. if (verbose)
  218. connection_status();
  219. }
  220. int tcpudpsvd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  221. int tcpudpsvd_main(int argc UNUSED_PARAM, char **argv)
  222. {
  223. char *str_C, *str_t;
  224. char *user;
  225. struct hcc *hccp;
  226. const char *instructs;
  227. char *msg_per_host = NULL;
  228. unsigned len_per_host = len_per_host; /* gcc */
  229. #ifndef SSLSVD
  230. struct bb_uidgid_t ugid;
  231. #endif
  232. bool tcp;
  233. uint16_t local_port;
  234. char *preset_local_hostname = NULL;
  235. char *remote_hostname = remote_hostname; /* for compiler */
  236. char *remote_addr = remote_addr; /* for compiler */
  237. len_and_sockaddr *lsa;
  238. len_and_sockaddr local, remote;
  239. socklen_t sa_len;
  240. int pid;
  241. int sock;
  242. int conn;
  243. unsigned backlog = 20;
  244. unsigned opts;
  245. INIT_G();
  246. tcp = (applet_name[0] == 't');
  247. /* "+": stop on first non-option */
  248. #ifdef SSLSVD
  249. opts = getopt32(argv, "^+"
  250. "c:+C:i:x:u:l:Eb:+hpt:vU:/:Z:K:" /* -c NUM, -b NUM */
  251. "\0"
  252. /* 3+ args, -i at most once, -p implies -h, -v is a counter */
  253. "-3:i--i:ph:vv",
  254. &cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname,
  255. &backlog, &str_t, &ssluser, &root, &cert, &key, &verbose
  256. );
  257. #else
  258. opts = getopt32(argv, "^+"
  259. "c:+C:i:x:u:l:Eb:+hpt:v" /* -c NUM, -b NUM */
  260. "\0"
  261. /* 3+ args, -i at most once, -p implies -h, -v is a counter */
  262. "-3:i--i:ph:vv",
  263. &cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname,
  264. &backlog, &str_t, &verbose
  265. );
  266. #endif
  267. if (opts & OPT_C) { /* -C n[:message] */
  268. max_per_host = bb_strtou(str_C, &str_C, 10);
  269. if (str_C[0]) {
  270. if (str_C[0] != ':')
  271. bb_show_usage();
  272. msg_per_host = str_C + 1;
  273. len_per_host = strlen(msg_per_host);
  274. }
  275. }
  276. if (max_per_host > cmax)
  277. max_per_host = cmax;
  278. if (opts & OPT_u) {
  279. xget_uidgid(&ugid, user);
  280. }
  281. #ifdef SSLSVD
  282. if (opts & OPT_U) ssluser = optarg;
  283. if (opts & OPT_slash) root = optarg;
  284. if (opts & OPT_Z) cert = optarg;
  285. if (opts & OPT_K) key = optarg;
  286. #endif
  287. argv += optind;
  288. if (!argv[0][0] || LONE_CHAR(argv[0], '0'))
  289. argv[0] = (char*)"0.0.0.0";
  290. /* Per-IP flood protection is not thought-out for UDP */
  291. if (!tcp)
  292. max_per_host = 0;
  293. bb_sanitize_stdio(); /* fd# 0,1,2 must be opened */
  294. #ifdef SSLSVD
  295. sslser = user;
  296. client = 0;
  297. if ((getuid() == 0) && !(opts & OPT_u)) {
  298. xfunc_exitcode = 100;
  299. bb_error_msg_and_die(bb_msg_you_must_be_root);
  300. }
  301. if (opts & OPT_u)
  302. if (!uidgid_get(&sslugid, ssluser, 1)) {
  303. if (errno) {
  304. bb_perror_msg_and_die("can't get user/group: %s", ssluser);
  305. }
  306. bb_error_msg_and_die("unknown user/group %s", ssluser);
  307. }
  308. if (!cert) cert = "./cert.pem";
  309. if (!key) key = cert;
  310. if (matrixSslOpen() < 0)
  311. fatal("can't initialize ssl");
  312. if (matrixSslReadKeys(&keys, cert, key, 0, ca) < 0) {
  313. if (client)
  314. fatal("can't read cert, key, or ca file");
  315. fatal("can't read cert or key file");
  316. }
  317. if (matrixSslNewSession(&ssl, keys, 0, SSL_FLAGS_SERVER) < 0)
  318. fatal("can't create ssl session");
  319. #endif
  320. sig_block(SIGCHLD);
  321. signal(SIGCHLD, sig_child_handler);
  322. bb_signals(BB_FATAL_SIGS, sig_term_handler);
  323. signal(SIGPIPE, SIG_IGN);
  324. if (max_per_host)
  325. ipsvd_perhost_init(cmax);
  326. local_port = bb_lookup_port(argv[1], tcp ? "tcp" : "udp", 0);
  327. lsa = xhost2sockaddr(argv[0], local_port);
  328. argv += 2;
  329. sock = xsocket(lsa->u.sa.sa_family, tcp ? SOCK_STREAM : SOCK_DGRAM, 0);
  330. setsockopt_reuseaddr(sock);
  331. sa_len = lsa->len; /* I presume sockaddr len stays the same */
  332. xbind(sock, &lsa->u.sa, sa_len);
  333. if (tcp) {
  334. xlisten(sock, backlog);
  335. close_on_exec_on(sock);
  336. } else { /* udp: needed for recv_from_to to work: */
  337. socket_want_pktinfo(sock);
  338. }
  339. /* ndelay_off(sock); - it is the default I think? */
  340. #ifndef SSLSVD
  341. if (opts & OPT_u) {
  342. /* drop permissions */
  343. xsetgid(ugid.gid);
  344. xsetuid(ugid.uid);
  345. }
  346. #endif
  347. if (verbose) {
  348. char *addr = xmalloc_sockaddr2dotted(&lsa->u.sa);
  349. if (opts & OPT_u)
  350. bb_error_msg("listening on %s, starting, uid %u, gid %u", addr,
  351. (unsigned)ugid.uid, (unsigned)ugid.gid);
  352. else
  353. bb_error_msg("listening on %s, starting", addr);
  354. free(addr);
  355. }
  356. /* Main accept() loop */
  357. again:
  358. hccp = NULL;
  359. again1:
  360. close(0);
  361. /* It's important to close(0) _before_ wait loop:
  362. * fd#0 can be a shared connection fd.
  363. * If kept open by us, peer can't detect PROG closing it.
  364. */
  365. while (cnum >= cmax)
  366. wait_for_any_sig(); /* expecting SIGCHLD */
  367. again2:
  368. sig_unblock(SIGCHLD);
  369. local.len = remote.len = sa_len;
  370. if (tcp) {
  371. /* Accept a connection to fd #0 */
  372. conn = accept(sock, &remote.u.sa, &remote.len);
  373. } else {
  374. /* In case recv_from_to won't be able to recover local addr.
  375. * Also sets port - recv_from_to is unable to do it. */
  376. local = *lsa;
  377. conn = recv_from_to(sock, NULL, 0, MSG_PEEK,
  378. &remote.u.sa, &local.u.sa, sa_len);
  379. }
  380. sig_block(SIGCHLD);
  381. if (conn < 0) {
  382. if (errno != EINTR)
  383. bb_perror_msg(tcp ? "accept" : "recv");
  384. goto again2;
  385. }
  386. xmove_fd(tcp ? conn : sock, 0);
  387. if (max_per_host) {
  388. /* Drop connection immediately if cur_per_host > max_per_host
  389. * (minimizing load under SYN flood) */
  390. remote_addr = xmalloc_sockaddr2dotted_noport(&remote.u.sa);
  391. cur_per_host = ipsvd_perhost_add(remote_addr, max_per_host, &hccp);
  392. if (cur_per_host > max_per_host) {
  393. /* ipsvd_perhost_add detected that max is exceeded
  394. * (and did not store ip in connection table) */
  395. free(remote_addr);
  396. if (msg_per_host) {
  397. /* don't block or test for errors */
  398. send(0, msg_per_host, len_per_host, MSG_DONTWAIT);
  399. }
  400. goto again1;
  401. }
  402. /* NB: remote_addr is not leaked, it is stored in conn table */
  403. }
  404. if (!tcp) {
  405. /* Voodoo magic: making udp sockets each receive its own
  406. * packets is not trivial, and I still not sure
  407. * I do it 100% right.
  408. * 1) we have to do it before fork()
  409. * 2) order is important - is it right now? */
  410. /* Open new non-connected UDP socket for further clients... */
  411. sock = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
  412. setsockopt_reuseaddr(sock);
  413. /* Make plain write/send work for old socket by supplying default
  414. * destination address. This also restricts incoming packets
  415. * to ones coming from this remote IP. */
  416. xconnect(0, &remote.u.sa, sa_len);
  417. /* hole? at this point we have no wildcard udp socket...
  418. * can this cause clients to get "port unreachable" icmp?
  419. * Yup, time window is very small, but it exists (is it?) */
  420. /* ..."open new socket", continued */
  421. xbind(sock, &lsa->u.sa, sa_len);
  422. socket_want_pktinfo(sock);
  423. /* Doesn't work:
  424. * we cannot replace fd #0 - we will lose pending packet
  425. * which is already buffered for us! And we cannot use fd #1
  426. * instead - it will "intercept" all following packets, but child
  427. * does not expect data coming *from fd #1*! */
  428. #if 0
  429. /* Make it so that local addr is fixed to localp->u.sa
  430. * and we don't accidentally accept packets to other local IPs. */
  431. /* NB: we possibly bind to the _very_ same_ address & port as the one
  432. * already bound in parent! This seems to work in Linux.
  433. * (otherwise we can move socket to fd #0 only if bind succeeds) */
  434. close(0);
  435. set_nport(&localp->u.sa, htons(local_port));
  436. xmove_fd(xsocket(localp->u.sa.sa_family, SOCK_DGRAM, 0), 0);
  437. setsockopt_reuseaddr(0); /* crucial */
  438. xbind(0, &localp->u.sa, localp->len);
  439. #endif
  440. }
  441. pid = vfork();
  442. if (pid == -1) {
  443. bb_perror_msg("vfork");
  444. goto again;
  445. }
  446. if (pid != 0) {
  447. /* Parent */
  448. cnum++;
  449. if (verbose)
  450. connection_status();
  451. if (hccp)
  452. hccp->pid = pid;
  453. /* clean up changes done by vforked child */
  454. undo_xsetenv();
  455. goto again;
  456. }
  457. /* Child: prepare env, log, and exec prog */
  458. { /* vfork alert! every xmalloc in this block should be freed! */
  459. char *local_hostname = local_hostname; /* for compiler */
  460. char *local_addr = NULL;
  461. char *free_me0 = NULL;
  462. char *free_me1 = NULL;
  463. char *free_me2 = NULL;
  464. if (verbose || !(opts & OPT_E)) {
  465. if (!max_per_host) /* remote_addr is not yet known */
  466. free_me0 = remote_addr = xmalloc_sockaddr2dotted(&remote.u.sa);
  467. if (opts & OPT_h) {
  468. free_me1 = remote_hostname = xmalloc_sockaddr2host_noport(&remote.u.sa);
  469. if (!remote_hostname) {
  470. bb_error_msg("can't look up hostname for %s", remote_addr);
  471. remote_hostname = remote_addr;
  472. }
  473. }
  474. /* Find out local IP peer connected to.
  475. * Errors ignored (I'm not paranoid enough to imagine kernel
  476. * which doesn't know local IP). */
  477. if (tcp)
  478. getsockname(0, &local.u.sa, &local.len);
  479. /* else: for UDP it is done earlier by parent */
  480. local_addr = xmalloc_sockaddr2dotted(&local.u.sa);
  481. if (opts & OPT_h) {
  482. local_hostname = preset_local_hostname;
  483. if (!local_hostname) {
  484. free_me2 = local_hostname = xmalloc_sockaddr2host_noport(&local.u.sa);
  485. if (!local_hostname)
  486. bb_error_msg_and_die("can't look up hostname for %s", local_addr);
  487. }
  488. /* else: local_hostname is not NULL, but is NOT malloced! */
  489. }
  490. }
  491. if (verbose) {
  492. pid = getpid();
  493. if (max_per_host) {
  494. bb_error_msg("concurrency %s %u/%u",
  495. remote_addr,
  496. cur_per_host, max_per_host);
  497. }
  498. bb_error_msg((opts & OPT_h)
  499. ? "start %u %s-%s (%s-%s)"
  500. : "start %u %s-%s",
  501. pid,
  502. local_addr, remote_addr,
  503. local_hostname, remote_hostname);
  504. }
  505. if (!(opts & OPT_E)) {
  506. /* setup ucspi env */
  507. const char *proto = tcp ? "TCP" : "UDP";
  508. #ifdef SO_ORIGINAL_DST
  509. /* Extract "original" destination addr:port
  510. * from Linux firewall. Useful when you redirect
  511. * an outbond connection to local handler, and it needs
  512. * to know where it originally tried to connect */
  513. if (tcp && getsockopt(0, SOL_IP, SO_ORIGINAL_DST, &local.u.sa, &local.len) == 0) {
  514. char *addr = xmalloc_sockaddr2dotted(&local.u.sa);
  515. xsetenv_plain("TCPORIGDSTADDR", addr);
  516. free(addr);
  517. }
  518. #endif
  519. xsetenv_plain("PROTO", proto);
  520. xsetenv_proto(proto, "LOCALADDR", local_addr);
  521. xsetenv_proto(proto, "REMOTEADDR", remote_addr);
  522. if (opts & OPT_h) {
  523. xsetenv_proto(proto, "LOCALHOST", local_hostname);
  524. xsetenv_proto(proto, "REMOTEHOST", remote_hostname);
  525. }
  526. //compat? xsetenv_proto(proto, "REMOTEINFO", "");
  527. /* additional */
  528. if (cur_per_host > 0) /* can not be true for udp */
  529. xsetenv_plain("TCPCONCURRENCY", utoa(cur_per_host));
  530. }
  531. free(local_addr);
  532. free(free_me0);
  533. free(free_me1);
  534. free(free_me2);
  535. }
  536. xdup2(0, 1);
  537. signal(SIGPIPE, SIG_DFL); /* this one was SIG_IGNed */
  538. /* Non-ignored signals revert to SIG_DFL on exec anyway */
  539. /*signal(SIGCHLD, SIG_DFL);*/
  540. sig_unblock(SIGCHLD);
  541. #ifdef SSLSVD
  542. strcpy(id, utoa(pid));
  543. ssl_io(0, argv);
  544. bb_perror_msg_and_die("can't execute '%s'", argv[0]);
  545. #else
  546. BB_EXECVP_or_die(argv);
  547. #endif
  548. }
  549. /*
  550. tcpsvd [-hpEvv] [-c n] [-C n:msg] [-b n] [-u user] [-l name]
  551. [-i dir|-x cdb] [ -t sec] host port prog
  552. tcpsvd creates a TCP/IP socket, binds it to the address host:port,
  553. and listens on the socket for incoming connections.
  554. On each incoming connection, tcpsvd conditionally runs a program,
  555. with standard input reading from the socket, and standard output
  556. writing to the socket, to handle this connection. tcpsvd keeps
  557. listening on the socket for new connections, and can handle
  558. multiple connections simultaneously.
  559. tcpsvd optionally checks for special instructions depending
  560. on the IP address or hostname of the client that initiated
  561. the connection, see ipsvd-instruct(5).
  562. host
  563. host either is a hostname, or a dotted-decimal IP address,
  564. or 0. If host is 0, tcpsvd accepts connections to any local
  565. IP address.
  566. * busybox accepts IPv6 addresses and host:port pairs too
  567. In this case second parameter is ignored
  568. port
  569. tcpsvd accepts connections to host:port. port may be a name
  570. from /etc/services or a number.
  571. prog
  572. prog consists of one or more arguments. For each connection,
  573. tcpsvd normally runs prog, with file descriptor 0 reading from
  574. the network, and file descriptor 1 writing to the network.
  575. By default it also sets up TCP-related environment variables,
  576. see tcp-environ(5)
  577. -i dir
  578. read instructions for handling new connections from the instructions
  579. directory dir. See ipsvd-instruct(5) for details.
  580. * ignored by busyboxed version
  581. -x cdb
  582. read instructions for handling new connections from the constant database
  583. cdb. The constant database normally is created from an instructions
  584. directory by running ipsvd-cdb(8).
  585. * ignored by busyboxed version
  586. -t sec
  587. timeout. This option only takes effect if the -i option is given.
  588. While checking the instructions directory, check the time of last access
  589. of the file that matches the clients address or hostname if any, discard
  590. and remove the file if it wasn't accessed within the last sec seconds;
  591. tcpsvd does not discard or remove a file if the user's write permission
  592. is not set, for those files the timeout is disabled. Default is 0,
  593. which means that the timeout is disabled.
  594. * ignored by busyboxed version
  595. -l name
  596. local hostname. Do not look up the local hostname in DNS, but use name
  597. as hostname. This option must be set if tcpsvd listens on port 53
  598. to avoid loops.
  599. -u user[:group]
  600. drop permissions. Switch user ID to user's UID, and group ID to user's
  601. primary GID after creating and binding to the socket. If user is followed
  602. by a colon and a group name, the group ID is switched to the GID of group
  603. instead. All supplementary groups are removed.
  604. -c n
  605. concurrency. Handle up to n connections simultaneously. Default is 30.
  606. If there are n connections active, tcpsvd defers acceptance of a new
  607. connection until an active connection is closed.
  608. -C n[:msg]
  609. per host concurrency. Allow only up to n connections from the same IP
  610. address simultaneously. If there are n active connections from one IP
  611. address, new incoming connections from this IP address are closed
  612. immediately. If n is followed by :msg, the message msg is written
  613. to the client if possible, before closing the connection. By default
  614. msg is empty. See ipsvd-instruct(5) for supported escape sequences in msg.
  615. For each accepted connection, the current per host concurrency is
  616. available through the environment variable TCPCONCURRENCY. n and msg
  617. can be overwritten by ipsvd(7) instructions, see ipsvd-instruct(5).
  618. By default tcpsvd doesn't keep track of connections.
  619. -h
  620. Look up the client's hostname in DNS.
  621. -p
  622. paranoid. After looking up the client's hostname in DNS, look up the IP
  623. addresses in DNS for that hostname, and forget about the hostname
  624. if none of the addresses match the client's IP address. You should
  625. set this option if you use hostname based instructions. The -p option
  626. implies the -h option.
  627. * ignored by busyboxed version
  628. -b n
  629. backlog. Allow a backlog of approximately n TCP SYNs. On some systems n
  630. is silently limited. Default is 20.
  631. -E
  632. no special environment. Do not set up TCP-related environment variables.
  633. -v
  634. verbose. Print verbose messages to standard output.
  635. -vv
  636. more verbose. Print more verbose messages to standard output.
  637. * no difference between -v and -vv in busyboxed version
  638. */