3
0

tcpudp.c 22 KB

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