blob: 270325164b1c856fab79fe6f115e331b6f2b202f [file] [log] [blame]
Denis Vlasenkob933ac12007-04-03 12:09:46 +00001/* 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 *
Denis Vlasenkod18f52b2008-03-02 12:53:15 +00005 * Copyright (C) 2007 Denys Vlasenko.
Denis Vlasenkob933ac12007-04-03 12:09:46 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkob933ac12007-04-03 12:09:46 +00008 */
9
Denis Vlasenko4866e902008-03-17 09:17:27 +000010/* Based on ipsvd-0.12.1. This tcpsvd accepts all options
Denis Vlasenko02fd6682007-04-03 23:23:10 +000011 * which are supported by one from ipsvd-0.12.1, but not all are
12 * functional. See help text at the end of this file for details.
13 *
14 * Code inside "#ifdef SSLSVD" is for sslsvd and is currently unused.
15 *
Denis Vlasenkoaefed942008-03-17 08:35:44 +000016 * Busybox version exports TCPLOCALADDR instead of
17 * TCPLOCALIP + TCPLOCALPORT pair. ADDR more closely matches reality
18 * (which is "struct sockaddr_XXX". Port is not a separate entity,
19 * it's just a part of (AF_INET[6]) sockaddr!).
Denis Vlasenko02fd6682007-04-03 23:23:10 +000020 *
Denis Vlasenkoaefed942008-03-17 08:35:44 +000021 * TCPORIGDSTADDR is Busybox-specific addition.
Denis Vlasenko02fd6682007-04-03 23:23:10 +000022 *
23 * udp server is hacked up by reusing TCP code. It has the following
24 * limitation inherent in Unix DGRAM sockets implementation:
Denis Vlasenko64a15122007-04-04 10:16:15 +000025 * - local IP address is retrieved (using recvmsg voodoo) but
Denis Vlasenko02fd6682007-04-03 23:23:10 +000026 * child's socket is not bound to it (bind cannot be called on
Denis Vlasenko64a15122007-04-04 10:16:15 +000027 * already bound socket). Thus it still can emit outgoing packets
Denis Vlasenkobeaca812007-04-13 16:32:26 +000028 * with wrong source IP...
Denis Vlasenko02fd6682007-04-03 23:23:10 +000029 * - don't know how to retrieve ORIGDST for udp.
30 */
Denys Vlasenko47367e12016-11-23 09:05:14 +010031//config:config TCPSVD
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020032//config: bool "tcpsvd (13 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010033//config: default y
34//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020035//config: tcpsvd listens on a TCP port and runs a program for each new
36//config: connection.
Denys Vlasenko47367e12016-11-23 09:05:14 +010037//config:
38//config:config UDPSVD
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020039//config: bool "udpsvd (13 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010040//config: default y
41//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020042//config: udpsvd listens on an UDP port and runs a program for each new
43//config: connection.
Denys Vlasenko47367e12016-11-23 09:05:14 +010044
45//applet:IF_TCPSVD(APPLET_ODDNAME(tcpsvd, tcpudpsvd, BB_DIR_USR_BIN, BB_SUID_DROP, tcpsvd))
46//applet:IF_UDPSVD(APPLET_ODDNAME(udpsvd, tcpudpsvd, BB_DIR_USR_BIN, BB_SUID_DROP, udpsvd))
47
48//kbuild:lib-$(CONFIG_TCPSVD) += tcpudp.o tcpudp_perhost.o
49//kbuild:lib-$(CONFIG_UDPSVD) += tcpudp.o tcpudp_perhost.o
Denis Vlasenko02fd6682007-04-03 23:23:10 +000050
Pere Orga5bc8c002011-04-11 03:29:49 +020051//usage:#define tcpsvd_trivial_usage
52//usage: "[-hEv] [-c N] [-C N[:MSG]] [-b N] [-u USER] [-l NAME] IP PORT PROG"
53/* with not-implemented options: */
54/* //usage: "[-hpEvv] [-c N] [-C N[:MSG]] [-b N] [-u USER] [-l NAME] [-i DIR|-x CDB] [-t SEC] IP PORT PROG" */
55//usage:#define tcpsvd_full_usage "\n\n"
Denys Vlasenkoec1ea162016-10-07 15:56:47 +020056//usage: "Create TCP socket, bind to IP:PORT and listen for incoming connections.\n"
57//usage: "Run PROG for each connection.\n"
58//usage: "\n IP PORT IP:PORT to listen on"
Pere Orga5bc8c002011-04-11 03:29:49 +020059//usage: "\n PROG ARGS Program to run"
Pere Orga5bc8c002011-04-11 03:29:49 +020060//usage: "\n -u USER[:GRP] Change to user/group after bind"
Denys Vlasenkoec1ea162016-10-07 15:56:47 +020061//usage: "\n -c N Up to N connections simultaneously (default 30)"
62//usage: "\n -b N Allow backlog of approximately N TCP SYNs (default 20)"
63//usage: "\n -C N[:MSG] Allow only up to N connections from the same IP:"
64//usage: "\n new connections from this IP address are closed"
65//usage: "\n immediately, MSG is written to the peer before close"
66//usage: "\n -E Don't set up environment"
Pere Orga5bc8c002011-04-11 03:29:49 +020067//usage: "\n -h Look up peer's hostname"
Denys Vlasenkoec1ea162016-10-07 15:56:47 +020068//usage: "\n -l NAME Local hostname (else look up local hostname in DNS)"
Pere Orga5bc8c002011-04-11 03:29:49 +020069//usage: "\n -v Verbose"
Denys Vlasenkoec1ea162016-10-07 15:56:47 +020070//usage: "\n"
71//usage: "\nEnvironment if no -E:"
72//usage: "\nPROTO='TCP'"
73//usage: "\nTCPREMOTEADDR='ip:port'" IF_FEATURE_IPV6(" ('[ip]:port' for IPv6)")
74//usage: "\nTCPLOCALADDR='ip:port'"
75//usage: "\nTCPORIGDSTADDR='ip:port' of destination before firewall"
76//usage: "\n Useful for REDIRECTed-to-local connections:"
77//usage: "\n iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080"
78//usage: "\nTCPCONCURRENCY=num_of_connects_from_this_ip"
79//usage: "\nIf -h:"
80//usage: "\nTCPLOCALHOST='hostname' (-l NAME is used if specified)"
81//usage: "\nTCPREMOTEHOST='hostname'"
82
Pere Orga5bc8c002011-04-11 03:29:49 +020083//usage:
84//usage:#define udpsvd_trivial_usage
85//usage: "[-hEv] [-c N] [-u USER] [-l NAME] IP PORT PROG"
86//usage:#define udpsvd_full_usage "\n\n"
Denys Vlasenkoec1ea162016-10-07 15:56:47 +020087//usage: "Create UDP socket, bind to IP:PORT and wait for incoming packets.\n"
88//usage: "Run PROG for each packet, redirecting all further packets with same\n"
89//usage: "peer ip:port to it.\n"
90//usage: "\n IP PORT IP:PORT to listen on"
Pere Orga5bc8c002011-04-11 03:29:49 +020091//usage: "\n PROG ARGS Program to run"
Pere Orga5bc8c002011-04-11 03:29:49 +020092//usage: "\n -u USER[:GRP] Change to user/group after bind"
Denys Vlasenkoec1ea162016-10-07 15:56:47 +020093//usage: "\n -c N Up to N connections simultaneously (default 30)"
94//usage: "\n -E Don't set up environment"
Pere Orga5bc8c002011-04-11 03:29:49 +020095//usage: "\n -h Look up peer's hostname"
Denys Vlasenkoec1ea162016-10-07 15:56:47 +020096//usage: "\n -l NAME Local hostname (else look up local hostname in DNS)"
Pere Orga5bc8c002011-04-11 03:29:49 +020097//usage: "\n -v Verbose"
Denys Vlasenkoec1ea162016-10-07 15:56:47 +020098//usage: "\n"
99//usage: "\nEnvironment if no -E:"
100//usage: "\nPROTO='UDP'"
101//usage: "\nUDPREMOTEADDR='ip:port'" IF_FEATURE_IPV6(" ('[ip]:port' for IPv6)")
102//usage: "\nUDPLOCALADDR='ip:port'"
103//usage: "\nIf -h:"
104//usage: "\nUDPLOCALHOST='hostname' (-l NAME is used if specified)"
105//usage: "\nUDPREMOTEHOST='hostname'"
Pere Orga5bc8c002011-04-11 03:29:49 +0200106
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000107#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200108#include "common_bufsiz.h"
Jeremie Koenig2ea12d82010-05-27 15:46:25 +0200109
Jeremie Koenig2ea12d82010-05-27 15:46:25 +0200110#ifdef __linux__
Denys Vlasenko78900352017-01-02 10:46:08 +0100111/* from linux/netfilter_ipv4.h: */
112# undef SO_ORIGINAL_DST
113# define SO_ORIGINAL_DST 80
Jeremie Koenig2ea12d82010-05-27 15:46:25 +0200114#endif
Denis Vlasenko165f5b32008-03-31 20:30:38 +0000115
Denis Vlasenko4866e902008-03-17 09:17:27 +0000116// TODO: move into this file:
117#include "tcpudp_perhost.h"
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000118
119#ifdef SSLSVD
120#include "matrixSsl.h"
121#include "ssl_io.h"
122#endif
123
Denis Vlasenkob9256052007-09-28 10:29:17 +0000124struct globals {
125 unsigned verbose;
126 unsigned max_per_host;
127 unsigned cur_per_host;
128 unsigned cnum;
Denis Vlasenko3f5fdc72007-10-14 04:55:59 +0000129 unsigned cmax;
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000130 char **env_cur;
131 char *env_var[1]; /* actually bigger */
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100132} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200133#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkob9256052007-09-28 10:29:17 +0000134#define verbose (G.verbose )
135#define max_per_host (G.max_per_host)
136#define cur_per_host (G.cur_per_host)
137#define cnum (G.cnum )
138#define cmax (G.cmax )
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000139#define env_cur (G.env_cur )
140#define env_var (G.env_var )
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000141#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200142 setup_common_bufsiz(); \
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000143 cmax = 30; \
144 env_cur = &env_var[0]; \
145} while (0)
Denis Vlasenkob9256052007-09-28 10:29:17 +0000146
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000147
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000148/* We have to be careful about leaking memory in repeated setenv's */
149static void xsetenv_plain(const char *n, const char *v)
150{
151 char *var = xasprintf("%s=%s", n, v);
152 *env_cur++ = var;
153 putenv(var);
154}
155
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000156static void xsetenv_proto(const char *proto, const char *n, const char *v)
157{
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000158 char *var = xasprintf("%s%s=%s", proto, n, v);
159 *env_cur++ = var;
160 putenv(var);
161}
162
163static void undo_xsetenv(void)
164{
165 char **pp = env_cur = &env_var[0];
166 while (*pp) {
167 char *var = *pp;
Denys Vlasenkodd8adde2010-06-24 05:00:50 +0200168 bb_unsetenv_and_free(var);
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000169 *pp++ = NULL;
170 }
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000171}
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000172
173static void sig_term_handler(int sig)
174{
175 if (verbose)
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000176 bb_error_msg("got signal %u, exit", sig);
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000177 kill_myself_with_sig(sig);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000178}
179
180/* Little bloated, but tries to give accurate info how child exited.
181 * Makes easier to spot segfaulting children etc... */
182static void print_waitstat(unsigned pid, int wstat)
183{
184 unsigned e = 0;
185 const char *cause = "?exit";
186
187 if (WIFEXITED(wstat)) {
188 cause++;
189 e = WEXITSTATUS(wstat);
190 } else if (WIFSIGNALED(wstat)) {
191 cause = "signal";
192 e = WTERMSIG(wstat);
193 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000194 bb_error_msg("end %d %s %d", pid, cause, e);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000195}
196
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000197/* Must match getopt32 in main! */
198enum {
199 OPT_c = (1 << 0),
200 OPT_C = (1 << 1),
201 OPT_i = (1 << 2),
202 OPT_x = (1 << 3),
203 OPT_u = (1 << 4),
204 OPT_l = (1 << 5),
205 OPT_E = (1 << 6),
206 OPT_b = (1 << 7),
207 OPT_h = (1 << 8),
208 OPT_p = (1 << 9),
209 OPT_t = (1 << 10),
210 OPT_v = (1 << 11),
211 OPT_V = (1 << 12),
212 OPT_U = (1 << 13), /* from here: sslsvd only */
213 OPT_slash = (1 << 14),
214 OPT_Z = (1 << 15),
215 OPT_K = (1 << 16),
216};
217
218static void connection_status(void)
219{
Denis Vlasenkof87f4952007-08-24 10:27:41 +0000220 /* "only 1 client max" desn't need this */
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000221 if (cmax > 1)
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000222 bb_error_msg("status %u/%u", cnum, cmax);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000223}
224
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000225static void sig_child_handler(int sig UNUSED_PARAM)
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000226{
227 int wstat;
Denis Vlasenko3854c5d2008-11-06 22:39:57 +0000228 pid_t pid;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000229
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000230 while ((pid = wait_any_nohang(&wstat)) > 0) {
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000231 if (max_per_host)
232 ipsvd_perhost_remove(pid);
233 if (cnum)
234 cnum--;
235 if (verbose)
236 print_waitstat(pid, wstat);
237 }
238 if (verbose)
239 connection_status();
240}
241
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000242int tcpudpsvd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000243int tcpudpsvd_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000244{
Denis Vlasenko1d426652008-03-17 09:09:09 +0000245 char *str_C, *str_t;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000246 char *user;
247 struct hcc *hccp;
248 const char *instructs;
249 char *msg_per_host = NULL;
250 unsigned len_per_host = len_per_host; /* gcc */
Denis Vlasenko64a15122007-04-04 10:16:15 +0000251#ifndef SSLSVD
252 struct bb_uidgid_t ugid;
253#endif
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000254 bool tcp;
Denis Vlasenko64a15122007-04-04 10:16:15 +0000255 uint16_t local_port;
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000256 char *preset_local_hostname = NULL;
257 char *remote_hostname = remote_hostname; /* for compiler */
258 char *remote_addr = remote_addr; /* for compiler */
Denis Vlasenko64a15122007-04-04 10:16:15 +0000259 len_and_sockaddr *lsa;
260 len_and_sockaddr local, remote;
261 socklen_t sa_len;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000262 int pid;
263 int sock;
264 int conn;
265 unsigned backlog = 20;
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200266 unsigned opts;
Denis Vlasenko64a15122007-04-04 10:16:15 +0000267
Denis Vlasenkob9256052007-09-28 10:29:17 +0000268 INIT_G();
269
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000270 tcp = (applet_name[0] == 't');
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000271
Denis Vlasenko1d426652008-03-17 09:09:09 +0000272 /* 3+ args, -i at most once, -p implies -h, -v is counter, -b N, -c N */
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200273 opt_complementary = "-3:i--i:ph:vv";
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000274#ifdef SSLSVD
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200275 opts = getopt32(argv, "+c:+C:i:x:u:l:Eb:+hpt:vU:/:Z:K:",
Denis Vlasenko1d426652008-03-17 09:09:09 +0000276 &cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname,
277 &backlog, &str_t, &ssluser, &root, &cert, &key, &verbose
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000278 );
279#else
Denis Vlasenko1d426652008-03-17 09:09:09 +0000280 /* "+": stop on first non-option */
Denys Vlasenkoec1ea162016-10-07 15:56:47 +0200281 opts = getopt32(argv, "+c:+C:i:x:u:l:Eb:hpt:v",
Denis Vlasenko1d426652008-03-17 09:09:09 +0000282 &cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname,
283 &backlog, &str_t, &verbose
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000284 );
285#endif
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200286 if (opts & OPT_C) { /* -C n[:message] */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000287 max_per_host = bb_strtou(str_C, &str_C, 10);
288 if (str_C[0]) {
289 if (str_C[0] != ':')
290 bb_show_usage();
291 msg_per_host = str_C + 1;
292 len_per_host = strlen(msg_per_host);
293 }
294 }
295 if (max_per_host > cmax)
296 max_per_host = cmax;
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200297 if (opts & OPT_u) {
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +0000298 xget_uidgid(&ugid, user);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000299 }
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000300#ifdef SSLSVD
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200301 if (opts & OPT_U) ssluser = optarg;
302 if (opts & OPT_slash) root = optarg;
303 if (opts & OPT_Z) cert = optarg;
304 if (opts & OPT_K) key = optarg;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000305#endif
306 argv += optind;
307 if (!argv[0][0] || LONE_CHAR(argv[0], '0'))
308 argv[0] = (char*)"0.0.0.0";
309
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000310 /* Per-IP flood protection is not thought-out for UDP */
311 if (!tcp)
312 max_per_host = 0;
313
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000314 bb_sanitize_stdio(); /* fd# 0,1,2 must be opened */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000315
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000316#ifdef SSLSVD
317 sslser = user;
318 client = 0;
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200319 if ((getuid() == 0) && !(opts & OPT_u)) {
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000320 xfunc_exitcode = 100;
Denys Vlasenkob2e5fc32009-11-25 14:52:47 +0100321 bb_error_msg_and_die(bb_msg_you_must_be_root);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000322 }
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200323 if (opts & OPT_u)
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000324 if (!uidgid_get(&sslugid, ssluser, 1)) {
325 if (errno) {
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +0000326 bb_perror_msg_and_die("can't get user/group: %s", ssluser);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000327 }
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +0000328 bb_error_msg_and_die("unknown user/group %s", ssluser);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000329 }
330 if (!cert) cert = "./cert.pem";
331 if (!key) key = cert;
332 if (matrixSslOpen() < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100333 fatal("can't initialize ssl");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000334 if (matrixSslReadKeys(&keys, cert, key, 0, ca) < 0) {
335 if (client)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100336 fatal("can't read cert, key, or ca file");
337 fatal("can't read cert or key file");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000338 }
339 if (matrixSslNewSession(&ssl, keys, 0, SSL_FLAGS_SERVER) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100340 fatal("can't create ssl session");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000341#endif
342
343 sig_block(SIGCHLD);
344 signal(SIGCHLD, sig_child_handler);
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000345 bb_signals(BB_FATAL_SIGS, sig_term_handler);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000346 signal(SIGPIPE, SIG_IGN);
347
348 if (max_per_host)
349 ipsvd_perhost_init(cmax);
350
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000351 local_port = bb_lookup_port(argv[1], tcp ? "tcp" : "udp", 0);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000352 lsa = xhost2sockaddr(argv[0], local_port);
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000353 argv += 2;
354
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000355 sock = xsocket(lsa->u.sa.sa_family, tcp ? SOCK_STREAM : SOCK_DGRAM, 0);
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000356 setsockopt_reuseaddr(sock);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000357 sa_len = lsa->len; /* I presume sockaddr len stays the same */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000358 xbind(sock, &lsa->u.sa, sa_len);
Denys Vlasenko16635cc2009-06-13 22:49:08 +0200359 if (tcp) {
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000360 xlisten(sock, backlog);
Denys Vlasenko16635cc2009-06-13 22:49:08 +0200361 close_on_exec_on(sock);
362 } else { /* udp: needed for recv_from_to to work: */
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000363 socket_want_pktinfo(sock);
Denys Vlasenko16635cc2009-06-13 22:49:08 +0200364 }
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000365 /* ndelay_off(sock); - it is the default I think? */
366
367#ifndef SSLSVD
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200368 if (opts & OPT_u) {
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000369 /* drop permissions */
370 xsetgid(ugid.gid);
371 xsetuid(ugid.uid);
372 }
373#endif
374
375 if (verbose) {
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000376 char *addr = xmalloc_sockaddr2dotted(&lsa->u.sa);
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200377 if (opts & OPT_u)
378 bb_error_msg("listening on %s, starting, uid %u, gid %u", addr,
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000379 (unsigned)ugid.uid, (unsigned)ugid.gid);
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200380 else
381 bb_error_msg("listening on %s, starting", addr);
382 free(addr);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000383 }
384
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000385 /* Main accept() loop */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000386
387 again:
388 hccp = NULL;
389
Denys Vlasenkoec1ea162016-10-07 15:56:47 +0200390 again1:
391 close(0);
392 /* It's important to close(0) _before_ wait loop:
393 * fd#0 can be a shared connection fd.
394 * If kept open by us, peer can't detect PROG closing it.
395 */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000396 while (cnum >= cmax)
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000397 wait_for_any_sig(); /* expecting SIGCHLD */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000398
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000399 again2:
400 sig_unblock(SIGCHLD);
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000401 local.len = remote.len = sa_len;
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000402 if (tcp) {
Denys Vlasenkoec1ea162016-10-07 15:56:47 +0200403 /* Accept a connection to fd #0 */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000404 conn = accept(sock, &remote.u.sa, &remote.len);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000405 } else {
Denis Vlasenko1ca332b2007-04-04 17:49:47 +0000406 /* In case recv_from_to won't be able to recover local addr.
Denis Vlasenko64a15122007-04-04 10:16:15 +0000407 * Also sets port - recv_from_to is unable to do it. */
408 local = *lsa;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000409 conn = recv_from_to(sock, NULL, 0, MSG_PEEK,
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000410 &remote.u.sa, &local.u.sa, sa_len);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000411 }
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000412 sig_block(SIGCHLD);
413 if (conn < 0) {
414 if (errno != EINTR)
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000415 bb_perror_msg(tcp ? "accept" : "recv");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000416 goto again2;
417 }
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000418 xmove_fd(tcp ? conn : sock, 0);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000419
420 if (max_per_host) {
421 /* Drop connection immediately if cur_per_host > max_per_host
422 * (minimizing load under SYN flood) */
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000423 remote_addr = xmalloc_sockaddr2dotted_noport(&remote.u.sa);
424 cur_per_host = ipsvd_perhost_add(remote_addr, max_per_host, &hccp);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000425 if (cur_per_host > max_per_host) {
426 /* ipsvd_perhost_add detected that max is exceeded
427 * (and did not store ip in connection table) */
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000428 free(remote_addr);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000429 if (msg_per_host) {
430 /* don't block or test for errors */
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000431 send(0, msg_per_host, len_per_host, MSG_DONTWAIT);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000432 }
433 goto again1;
434 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000435 /* NB: remote_addr is not leaked, it is stored in conn table */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000436 }
437
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000438 if (!tcp) {
439 /* Voodoo magic: making udp sockets each receive its own
Denis Vlasenko64a15122007-04-04 10:16:15 +0000440 * packets is not trivial, and I still not sure
441 * I do it 100% right.
442 * 1) we have to do it before fork()
443 * 2) order is important - is it right now? */
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000444
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000445 /* Open new non-connected UDP socket for further clients... */
446 sock = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
447 setsockopt_reuseaddr(sock);
448 /* Make plain write/send work for old socket by supplying default
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000449 * destination address. This also restricts incoming packets
450 * to ones coming from this remote IP. */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000451 xconnect(0, &remote.u.sa, sa_len);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000452 /* hole? at this point we have no wildcard udp socket...
Denis Vlasenko79468792007-04-04 11:02:55 +0000453 * can this cause clients to get "port unreachable" icmp?
454 * Yup, time window is very small, but it exists (is it?) */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000455 /* ..."open new socket", continued */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000456 xbind(sock, &lsa->u.sa, sa_len);
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000457 socket_want_pktinfo(sock);
Denis Vlasenko79468792007-04-04 11:02:55 +0000458
459 /* Doesn't work:
460 * we cannot replace fd #0 - we will lose pending packet
461 * which is already buffered for us! And we cannot use fd #1
462 * instead - it will "intercept" all following packets, but child
Denis Vlasenkof87f4952007-08-24 10:27:41 +0000463 * does not expect data coming *from fd #1*! */
Denis Vlasenko79468792007-04-04 11:02:55 +0000464#if 0
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000465 /* Make it so that local addr is fixed to localp->u.sa
Denis Vlasenko79468792007-04-04 11:02:55 +0000466 * and we don't accidentally accept packets to other local IPs. */
467 /* NB: we possibly bind to the _very_ same_ address & port as the one
468 * already bound in parent! This seems to work in Linux.
469 * (otherwise we can move socket to fd #0 only if bind succeeds) */
470 close(0);
Denys Vlasenkoca183112011-04-07 17:52:20 +0200471 set_nport(&localp->u.sa, htons(local_port));
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000472 xmove_fd(xsocket(localp->u.sa.sa_family, SOCK_DGRAM, 0), 0);
Denis Vlasenko79468792007-04-04 11:02:55 +0000473 setsockopt_reuseaddr(0); /* crucial */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000474 xbind(0, &localp->u.sa, localp->len);
Denis Vlasenko79468792007-04-04 11:02:55 +0000475#endif
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000476 }
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000477
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000478 pid = vfork();
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000479 if (pid == -1) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000480 bb_perror_msg("vfork");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000481 goto again;
482 }
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000483
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000484 if (pid != 0) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000485 /* Parent */
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000486 cnum++;
487 if (verbose)
488 connection_status();
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000489 if (hccp)
490 hccp->pid = pid;
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000491 /* clean up changes done by vforked child */
492 undo_xsetenv();
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000493 goto again;
494 }
495
496 /* Child: prepare env, log, and exec prog */
497
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000498 { /* vfork alert! every xmalloc in this block should be freed! */
499 char *local_hostname = local_hostname; /* for compiler */
500 char *local_addr = NULL;
501 char *free_me0 = NULL;
502 char *free_me1 = NULL;
503 char *free_me2 = NULL;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000504
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200505 if (verbose || !(opts & OPT_E)) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000506 if (!max_per_host) /* remote_addr is not yet known */
507 free_me0 = remote_addr = xmalloc_sockaddr2dotted(&remote.u.sa);
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200508 if (opts & OPT_h) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000509 free_me1 = remote_hostname = xmalloc_sockaddr2host_noport(&remote.u.sa);
510 if (!remote_hostname) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100511 bb_error_msg("can't look up hostname for %s", remote_addr);
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000512 remote_hostname = remote_addr;
513 }
514 }
515 /* Find out local IP peer connected to.
516 * Errors ignored (I'm not paranoid enough to imagine kernel
517 * which doesn't know local IP). */
518 if (tcp)
519 getsockname(0, &local.u.sa, &local.len);
520 /* else: for UDP it is done earlier by parent */
521 local_addr = xmalloc_sockaddr2dotted(&local.u.sa);
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200522 if (opts & OPT_h) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000523 local_hostname = preset_local_hostname;
524 if (!local_hostname) {
525 free_me2 = local_hostname = xmalloc_sockaddr2host_noport(&local.u.sa);
526 if (!local_hostname)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100527 bb_error_msg_and_die("can't look up hostname for %s", local_addr);
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000528 }
529 /* else: local_hostname is not NULL, but is NOT malloced! */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000530 }
531 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000532 if (verbose) {
533 pid = getpid();
534 if (max_per_host) {
535 bb_error_msg("concurrency %s %u/%u",
536 remote_addr,
537 cur_per_host, max_per_host);
538 }
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200539 bb_error_msg((opts & OPT_h)
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000540 ? "start %u %s-%s (%s-%s)"
541 : "start %u %s-%s",
542 pid,
543 local_addr, remote_addr,
544 local_hostname, remote_hostname);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000545 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000546
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200547 if (!(opts & OPT_E)) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000548 /* setup ucspi env */
549 const char *proto = tcp ? "TCP" : "UDP";
550
Jeremie Koenig2ea12d82010-05-27 15:46:25 +0200551#ifdef SO_ORIGINAL_DST
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000552 /* Extract "original" destination addr:port
553 * from Linux firewall. Useful when you redirect
554 * an outbond connection to local handler, and it needs
555 * to know where it originally tried to connect */
556 if (tcp && getsockopt(0, SOL_IP, SO_ORIGINAL_DST, &local.u.sa, &local.len) == 0) {
557 char *addr = xmalloc_sockaddr2dotted(&local.u.sa);
558 xsetenv_plain("TCPORIGDSTADDR", addr);
559 free(addr);
560 }
Jeremie Koenig2ea12d82010-05-27 15:46:25 +0200561#endif
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000562 xsetenv_plain("PROTO", proto);
563 xsetenv_proto(proto, "LOCALADDR", local_addr);
564 xsetenv_proto(proto, "REMOTEADDR", remote_addr);
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200565 if (opts & OPT_h) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000566 xsetenv_proto(proto, "LOCALHOST", local_hostname);
567 xsetenv_proto(proto, "REMOTEHOST", remote_hostname);
568 }
569 //compat? xsetenv_proto(proto, "REMOTEINFO", "");
570 /* additional */
571 if (cur_per_host > 0) /* can not be true for udp */
572 xsetenv_plain("TCPCONCURRENCY", utoa(cur_per_host));
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000573 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000574 free(local_addr);
575 free(free_me0);
576 free(free_me1);
577 free(free_me2);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000578 }
579
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000580 xdup2(0, 1);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000581
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000582 signal(SIGPIPE, SIG_DFL); /* this one was SIG_IGNed */
583 /* Non-ignored signals revert to SIG_DFL on exec anyway */
584 /*signal(SIGCHLD, SIG_DFL);*/
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000585 sig_unblock(SIGCHLD);
586
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000587#ifdef SSLSVD
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000588 strcpy(id, utoa(pid));
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000589 ssl_io(0, argv);
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200590 bb_perror_msg_and_die("can't execute '%s'", argv[0]);
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200591#else
592 BB_EXECVP_or_die(argv);
593#endif
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000594}
595
596/*
597tcpsvd [-hpEvv] [-c n] [-C n:msg] [-b n] [-u user] [-l name]
598 [-i dir|-x cdb] [ -t sec] host port prog
599
600tcpsvd creates a TCP/IP socket, binds it to the address host:port,
601and listens on the socket for incoming connections.
602
603On each incoming connection, tcpsvd conditionally runs a program,
604with standard input reading from the socket, and standard output
605writing to the socket, to handle this connection. tcpsvd keeps
606listening on the socket for new connections, and can handle
607multiple connections simultaneously.
608
609tcpsvd optionally checks for special instructions depending
610on the IP address or hostname of the client that initiated
611the connection, see ipsvd-instruct(5).
612
613host
614 host either is a hostname, or a dotted-decimal IP address,
615 or 0. If host is 0, tcpsvd accepts connections to any local
616 IP address.
617 * busybox accepts IPv6 addresses and host:port pairs too
618 In this case second parameter is ignored
619port
620 tcpsvd accepts connections to host:port. port may be a name
621 from /etc/services or a number.
622prog
623 prog consists of one or more arguments. For each connection,
624 tcpsvd normally runs prog, with file descriptor 0 reading from
625 the network, and file descriptor 1 writing to the network.
626 By default it also sets up TCP-related environment variables,
627 see tcp-environ(5)
628-i dir
629 read instructions for handling new connections from the instructions
630 directory dir. See ipsvd-instruct(5) for details.
631 * ignored by busyboxed version
632-x cdb
633 read instructions for handling new connections from the constant database
634 cdb. The constant database normally is created from an instructions
635 directory by running ipsvd-cdb(8).
636 * ignored by busyboxed version
637-t sec
638 timeout. This option only takes effect if the -i option is given.
639 While checking the instructions directory, check the time of last access
640 of the file that matches the clients address or hostname if any, discard
641 and remove the file if it wasn't accessed within the last sec seconds;
642 tcpsvd does not discard or remove a file if the user's write permission
643 is not set, for those files the timeout is disabled. Default is 0,
644 which means that the timeout is disabled.
645 * ignored by busyboxed version
646-l name
647 local hostname. Do not look up the local hostname in DNS, but use name
648 as hostname. This option must be set if tcpsvd listens on port 53
649 to avoid loops.
650-u user[:group]
651 drop permissions. Switch user ID to user's UID, and group ID to user's
652 primary GID after creating and binding to the socket. If user is followed
653 by a colon and a group name, the group ID is switched to the GID of group
654 instead. All supplementary groups are removed.
655-c n
656 concurrency. Handle up to n connections simultaneously. Default is 30.
657 If there are n connections active, tcpsvd defers acceptance of a new
658 connection until an active connection is closed.
659-C n[:msg]
660 per host concurrency. Allow only up to n connections from the same IP
661 address simultaneously. If there are n active connections from one IP
662 address, new incoming connections from this IP address are closed
663 immediately. If n is followed by :msg, the message msg is written
664 to the client if possible, before closing the connection. By default
665 msg is empty. See ipsvd-instruct(5) for supported escape sequences in msg.
666
667 For each accepted connection, the current per host concurrency is
668 available through the environment variable TCPCONCURRENCY. n and msg
669 can be overwritten by ipsvd(7) instructions, see ipsvd-instruct(5).
670 By default tcpsvd doesn't keep track of connections.
671-h
672 Look up the client's hostname in DNS.
673-p
674 paranoid. After looking up the client's hostname in DNS, look up the IP
675 addresses in DNS for that hostname, and forget about the hostname
676 if none of the addresses match the client's IP address. You should
677 set this option if you use hostname based instructions. The -p option
678 implies the -h option.
679 * ignored by busyboxed version
680-b n
681 backlog. Allow a backlog of approximately n TCP SYNs. On some systems n
682 is silently limited. Default is 20.
683-E
684 no special environment. Do not set up TCP-related environment variables.
685-v
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200686 verbose. Print verbose messages to standard output.
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000687-vv
688 more verbose. Print more verbose messages to standard output.
689 * no difference between -v and -vv in busyboxed version
690*/