blob: b532e43cd975c62c57bdee228b3edac75cbd4be4 [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 */
31
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000032#include "libbb.h"
Jeremie Koenig2ea12d82010-05-27 15:46:25 +020033
Denis Vlasenko165f5b32008-03-31 20:30:38 +000034/* Wants <limits.h> etc, thus included after libbb.h: */
Jeremie Koenig2ea12d82010-05-27 15:46:25 +020035#ifdef __linux__
Denis Vlasenkoc0cd9f22008-06-07 12:23:44 +000036#include <linux/types.h> /* for __be32 etc */
Denis Vlasenko165f5b32008-03-31 20:30:38 +000037#include <linux/netfilter_ipv4.h>
Jeremie Koenig2ea12d82010-05-27 15:46:25 +020038#endif
Denis Vlasenko165f5b32008-03-31 20:30:38 +000039
Denis Vlasenko4866e902008-03-17 09:17:27 +000040// TODO: move into this file:
41#include "tcpudp_perhost.h"
Denis Vlasenko02fd6682007-04-03 23:23:10 +000042
43#ifdef SSLSVD
44#include "matrixSsl.h"
45#include "ssl_io.h"
46#endif
47
Denis Vlasenkob9256052007-09-28 10:29:17 +000048struct globals {
49 unsigned verbose;
50 unsigned max_per_host;
51 unsigned cur_per_host;
52 unsigned cnum;
Denis Vlasenko3f5fdc72007-10-14 04:55:59 +000053 unsigned cmax;
Denis Vlasenkoaefed942008-03-17 08:35:44 +000054 char **env_cur;
55 char *env_var[1]; /* actually bigger */
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010056} FIX_ALIASING;
Denis Vlasenkob9256052007-09-28 10:29:17 +000057#define G (*(struct globals*)&bb_common_bufsiz1)
58#define verbose (G.verbose )
59#define max_per_host (G.max_per_host)
60#define cur_per_host (G.cur_per_host)
61#define cnum (G.cnum )
62#define cmax (G.cmax )
Denis Vlasenkoaefed942008-03-17 08:35:44 +000063#define env_cur (G.env_cur )
64#define env_var (G.env_var )
Denis Vlasenko7049ff82008-06-25 09:53:17 +000065#define INIT_G() do { \
66 cmax = 30; \
67 env_cur = &env_var[0]; \
68} while (0)
Denis Vlasenkob9256052007-09-28 10:29:17 +000069
Denis Vlasenko02fd6682007-04-03 23:23:10 +000070
Denis Vlasenkoaefed942008-03-17 08:35:44 +000071/* We have to be careful about leaking memory in repeated setenv's */
72static void xsetenv_plain(const char *n, const char *v)
73{
74 char *var = xasprintf("%s=%s", n, v);
75 *env_cur++ = var;
76 putenv(var);
77}
78
Denis Vlasenko02fd6682007-04-03 23:23:10 +000079static void xsetenv_proto(const char *proto, const char *n, const char *v)
80{
Denis Vlasenkoaefed942008-03-17 08:35:44 +000081 char *var = xasprintf("%s%s=%s", proto, n, v);
82 *env_cur++ = var;
83 putenv(var);
84}
85
86static void undo_xsetenv(void)
87{
88 char **pp = env_cur = &env_var[0];
89 while (*pp) {
90 char *var = *pp;
Denys Vlasenkodd8adde2010-06-24 05:00:50 +020091 bb_unsetenv_and_free(var);
Denis Vlasenkoaefed942008-03-17 08:35:44 +000092 *pp++ = NULL;
93 }
Denis Vlasenko02fd6682007-04-03 23:23:10 +000094}
Denis Vlasenkob933ac12007-04-03 12:09:46 +000095
96static void sig_term_handler(int sig)
97{
98 if (verbose)
Denis Vlasenkoaefed942008-03-17 08:35:44 +000099 bb_error_msg("got signal %u, exit", sig);
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000100 kill_myself_with_sig(sig);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000101}
102
103/* Little bloated, but tries to give accurate info how child exited.
104 * Makes easier to spot segfaulting children etc... */
105static void print_waitstat(unsigned pid, int wstat)
106{
107 unsigned e = 0;
108 const char *cause = "?exit";
109
110 if (WIFEXITED(wstat)) {
111 cause++;
112 e = WEXITSTATUS(wstat);
113 } else if (WIFSIGNALED(wstat)) {
114 cause = "signal";
115 e = WTERMSIG(wstat);
116 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000117 bb_error_msg("end %d %s %d", pid, cause, e);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000118}
119
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000120/* Must match getopt32 in main! */
121enum {
122 OPT_c = (1 << 0),
123 OPT_C = (1 << 1),
124 OPT_i = (1 << 2),
125 OPT_x = (1 << 3),
126 OPT_u = (1 << 4),
127 OPT_l = (1 << 5),
128 OPT_E = (1 << 6),
129 OPT_b = (1 << 7),
130 OPT_h = (1 << 8),
131 OPT_p = (1 << 9),
132 OPT_t = (1 << 10),
133 OPT_v = (1 << 11),
134 OPT_V = (1 << 12),
135 OPT_U = (1 << 13), /* from here: sslsvd only */
136 OPT_slash = (1 << 14),
137 OPT_Z = (1 << 15),
138 OPT_K = (1 << 16),
139};
140
141static void connection_status(void)
142{
Denis Vlasenkof87f4952007-08-24 10:27:41 +0000143 /* "only 1 client max" desn't need this */
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000144 if (cmax > 1)
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000145 bb_error_msg("status %u/%u", cnum, cmax);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000146}
147
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000148static void sig_child_handler(int sig UNUSED_PARAM)
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000149{
150 int wstat;
Denis Vlasenko3854c5d2008-11-06 22:39:57 +0000151 pid_t pid;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000152
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000153 while ((pid = wait_any_nohang(&wstat)) > 0) {
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000154 if (max_per_host)
155 ipsvd_perhost_remove(pid);
156 if (cnum)
157 cnum--;
158 if (verbose)
159 print_waitstat(pid, wstat);
160 }
161 if (verbose)
162 connection_status();
163}
164
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000165int tcpudpsvd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000166int tcpudpsvd_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000167{
Denis Vlasenko1d426652008-03-17 09:09:09 +0000168 char *str_C, *str_t;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000169 char *user;
170 struct hcc *hccp;
171 const char *instructs;
172 char *msg_per_host = NULL;
173 unsigned len_per_host = len_per_host; /* gcc */
Denis Vlasenko64a15122007-04-04 10:16:15 +0000174#ifndef SSLSVD
175 struct bb_uidgid_t ugid;
176#endif
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000177 bool tcp;
Denis Vlasenko64a15122007-04-04 10:16:15 +0000178 uint16_t local_port;
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000179 char *preset_local_hostname = NULL;
180 char *remote_hostname = remote_hostname; /* for compiler */
181 char *remote_addr = remote_addr; /* for compiler */
Denis Vlasenko64a15122007-04-04 10:16:15 +0000182 len_and_sockaddr *lsa;
183 len_and_sockaddr local, remote;
184 socklen_t sa_len;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000185 int pid;
186 int sock;
187 int conn;
188 unsigned backlog = 20;
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200189 unsigned opts;
Denis Vlasenko64a15122007-04-04 10:16:15 +0000190
Denis Vlasenkob9256052007-09-28 10:29:17 +0000191 INIT_G();
192
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000193 tcp = (applet_name[0] == 't');
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000194
Denis Vlasenko1d426652008-03-17 09:09:09 +0000195 /* 3+ args, -i at most once, -p implies -h, -v is counter, -b N, -c N */
196 opt_complementary = "-3:i--i:ph:vv:b+:c+";
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000197#ifdef SSLSVD
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200198 opts = getopt32(argv, "+c:C:i:x:u:l:Eb:hpt:vU:/:Z:K:",
Denis Vlasenko1d426652008-03-17 09:09:09 +0000199 &cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname,
200 &backlog, &str_t, &ssluser, &root, &cert, &key, &verbose
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000201 );
202#else
Denis Vlasenko1d426652008-03-17 09:09:09 +0000203 /* "+": stop on first non-option */
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200204 opts = getopt32(argv, "+c:C:i:x:u:l:Eb:hpt:v",
Denis Vlasenko1d426652008-03-17 09:09:09 +0000205 &cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname,
206 &backlog, &str_t, &verbose
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000207 );
208#endif
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200209 if (opts & OPT_C) { /* -C n[:message] */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000210 max_per_host = bb_strtou(str_C, &str_C, 10);
211 if (str_C[0]) {
212 if (str_C[0] != ':')
213 bb_show_usage();
214 msg_per_host = str_C + 1;
215 len_per_host = strlen(msg_per_host);
216 }
217 }
218 if (max_per_host > cmax)
219 max_per_host = cmax;
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200220 if (opts & OPT_u) {
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +0000221 xget_uidgid(&ugid, user);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000222 }
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000223#ifdef SSLSVD
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200224 if (opts & OPT_U) ssluser = optarg;
225 if (opts & OPT_slash) root = optarg;
226 if (opts & OPT_Z) cert = optarg;
227 if (opts & OPT_K) key = optarg;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000228#endif
229 argv += optind;
230 if (!argv[0][0] || LONE_CHAR(argv[0], '0'))
231 argv[0] = (char*)"0.0.0.0";
232
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000233 /* Per-IP flood protection is not thought-out for UDP */
234 if (!tcp)
235 max_per_host = 0;
236
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000237 bb_sanitize_stdio(); /* fd# 0,1,2 must be opened */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000238
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000239#ifdef SSLSVD
240 sslser = user;
241 client = 0;
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200242 if ((getuid() == 0) && !(opts & OPT_u)) {
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000243 xfunc_exitcode = 100;
Denys Vlasenkob2e5fc32009-11-25 14:52:47 +0100244 bb_error_msg_and_die(bb_msg_you_must_be_root);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000245 }
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200246 if (opts & OPT_u)
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000247 if (!uidgid_get(&sslugid, ssluser, 1)) {
248 if (errno) {
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +0000249 bb_perror_msg_and_die("can't get user/group: %s", ssluser);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000250 }
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +0000251 bb_error_msg_and_die("unknown user/group %s", ssluser);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000252 }
253 if (!cert) cert = "./cert.pem";
254 if (!key) key = cert;
255 if (matrixSslOpen() < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100256 fatal("can't initialize ssl");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000257 if (matrixSslReadKeys(&keys, cert, key, 0, ca) < 0) {
258 if (client)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100259 fatal("can't read cert, key, or ca file");
260 fatal("can't read cert or key file");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000261 }
262 if (matrixSslNewSession(&ssl, keys, 0, SSL_FLAGS_SERVER) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100263 fatal("can't create ssl session");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000264#endif
265
266 sig_block(SIGCHLD);
267 signal(SIGCHLD, sig_child_handler);
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000268 bb_signals(BB_FATAL_SIGS, sig_term_handler);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000269 signal(SIGPIPE, SIG_IGN);
270
271 if (max_per_host)
272 ipsvd_perhost_init(cmax);
273
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000274 local_port = bb_lookup_port(argv[1], tcp ? "tcp" : "udp", 0);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000275 lsa = xhost2sockaddr(argv[0], local_port);
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000276 argv += 2;
277
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000278 sock = xsocket(lsa->u.sa.sa_family, tcp ? SOCK_STREAM : SOCK_DGRAM, 0);
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000279 setsockopt_reuseaddr(sock);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000280 sa_len = lsa->len; /* I presume sockaddr len stays the same */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000281 xbind(sock, &lsa->u.sa, sa_len);
Denys Vlasenko16635cc2009-06-13 22:49:08 +0200282 if (tcp) {
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000283 xlisten(sock, backlog);
Denys Vlasenko16635cc2009-06-13 22:49:08 +0200284 close_on_exec_on(sock);
285 } else { /* udp: needed for recv_from_to to work: */
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000286 socket_want_pktinfo(sock);
Denys Vlasenko16635cc2009-06-13 22:49:08 +0200287 }
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000288 /* ndelay_off(sock); - it is the default I think? */
289
290#ifndef SSLSVD
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200291 if (opts & OPT_u) {
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000292 /* drop permissions */
293 xsetgid(ugid.gid);
294 xsetuid(ugid.uid);
295 }
296#endif
297
298 if (verbose) {
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000299 char *addr = xmalloc_sockaddr2dotted(&lsa->u.sa);
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200300 if (opts & OPT_u)
301 bb_error_msg("listening on %s, starting, uid %u, gid %u", addr,
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000302 (unsigned)ugid.uid, (unsigned)ugid.gid);
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200303 else
304 bb_error_msg("listening on %s, starting", addr);
305 free(addr);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000306 }
307
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000308 /* Main accept() loop */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000309
310 again:
311 hccp = NULL;
312
313 while (cnum >= cmax)
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000314 wait_for_any_sig(); /* expecting SIGCHLD */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000315
316 /* Accept a connection to fd #0 */
317 again1:
318 close(0);
319 again2:
320 sig_unblock(SIGCHLD);
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000321 local.len = remote.len = sa_len;
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000322 if (tcp) {
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000323 conn = accept(sock, &remote.u.sa, &remote.len);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000324 } else {
Denis Vlasenko1ca332b2007-04-04 17:49:47 +0000325 /* In case recv_from_to won't be able to recover local addr.
Denis Vlasenko64a15122007-04-04 10:16:15 +0000326 * Also sets port - recv_from_to is unable to do it. */
327 local = *lsa;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000328 conn = recv_from_to(sock, NULL, 0, MSG_PEEK,
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000329 &remote.u.sa, &local.u.sa, sa_len);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000330 }
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000331 sig_block(SIGCHLD);
332 if (conn < 0) {
333 if (errno != EINTR)
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000334 bb_perror_msg(tcp ? "accept" : "recv");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000335 goto again2;
336 }
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000337 xmove_fd(tcp ? conn : sock, 0);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000338
339 if (max_per_host) {
340 /* Drop connection immediately if cur_per_host > max_per_host
341 * (minimizing load under SYN flood) */
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000342 remote_addr = xmalloc_sockaddr2dotted_noport(&remote.u.sa);
343 cur_per_host = ipsvd_perhost_add(remote_addr, max_per_host, &hccp);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000344 if (cur_per_host > max_per_host) {
345 /* ipsvd_perhost_add detected that max is exceeded
346 * (and did not store ip in connection table) */
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000347 free(remote_addr);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000348 if (msg_per_host) {
349 /* don't block or test for errors */
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000350 send(0, msg_per_host, len_per_host, MSG_DONTWAIT);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000351 }
352 goto again1;
353 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000354 /* NB: remote_addr is not leaked, it is stored in conn table */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000355 }
356
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000357 if (!tcp) {
358 /* Voodoo magic: making udp sockets each receive its own
Denis Vlasenko64a15122007-04-04 10:16:15 +0000359 * packets is not trivial, and I still not sure
360 * I do it 100% right.
361 * 1) we have to do it before fork()
362 * 2) order is important - is it right now? */
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000363
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000364 /* Open new non-connected UDP socket for further clients... */
365 sock = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
366 setsockopt_reuseaddr(sock);
367 /* Make plain write/send work for old socket by supplying default
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000368 * destination address. This also restricts incoming packets
369 * to ones coming from this remote IP. */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000370 xconnect(0, &remote.u.sa, sa_len);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000371 /* hole? at this point we have no wildcard udp socket...
Denis Vlasenko79468792007-04-04 11:02:55 +0000372 * can this cause clients to get "port unreachable" icmp?
373 * Yup, time window is very small, but it exists (is it?) */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000374 /* ..."open new socket", continued */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000375 xbind(sock, &lsa->u.sa, sa_len);
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000376 socket_want_pktinfo(sock);
Denis Vlasenko79468792007-04-04 11:02:55 +0000377
378 /* Doesn't work:
379 * we cannot replace fd #0 - we will lose pending packet
380 * which is already buffered for us! And we cannot use fd #1
381 * instead - it will "intercept" all following packets, but child
Denis Vlasenkof87f4952007-08-24 10:27:41 +0000382 * does not expect data coming *from fd #1*! */
Denis Vlasenko79468792007-04-04 11:02:55 +0000383#if 0
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000384 /* Make it so that local addr is fixed to localp->u.sa
Denis Vlasenko79468792007-04-04 11:02:55 +0000385 * and we don't accidentally accept packets to other local IPs. */
386 /* NB: we possibly bind to the _very_ same_ address & port as the one
387 * already bound in parent! This seems to work in Linux.
388 * (otherwise we can move socket to fd #0 only if bind succeeds) */
389 close(0);
390 set_nport(localp, htons(local_port));
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000391 xmove_fd(xsocket(localp->u.sa.sa_family, SOCK_DGRAM, 0), 0);
Denis Vlasenko79468792007-04-04 11:02:55 +0000392 setsockopt_reuseaddr(0); /* crucial */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000393 xbind(0, &localp->u.sa, localp->len);
Denis Vlasenko79468792007-04-04 11:02:55 +0000394#endif
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000395 }
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000396
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000397 pid = vfork();
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000398 if (pid == -1) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000399 bb_perror_msg("vfork");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000400 goto again;
401 }
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000402
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000403 if (pid != 0) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000404 /* Parent */
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000405 cnum++;
406 if (verbose)
407 connection_status();
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000408 if (hccp)
409 hccp->pid = pid;
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000410 /* clean up changes done by vforked child */
411 undo_xsetenv();
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000412 goto again;
413 }
414
415 /* Child: prepare env, log, and exec prog */
416
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000417 { /* vfork alert! every xmalloc in this block should be freed! */
418 char *local_hostname = local_hostname; /* for compiler */
419 char *local_addr = NULL;
420 char *free_me0 = NULL;
421 char *free_me1 = NULL;
422 char *free_me2 = NULL;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000423
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200424 if (verbose || !(opts & OPT_E)) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000425 if (!max_per_host) /* remote_addr is not yet known */
426 free_me0 = remote_addr = xmalloc_sockaddr2dotted(&remote.u.sa);
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200427 if (opts & OPT_h) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000428 free_me1 = remote_hostname = xmalloc_sockaddr2host_noport(&remote.u.sa);
429 if (!remote_hostname) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100430 bb_error_msg("can't look up hostname for %s", remote_addr);
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000431 remote_hostname = remote_addr;
432 }
433 }
434 /* Find out local IP peer connected to.
435 * Errors ignored (I'm not paranoid enough to imagine kernel
436 * which doesn't know local IP). */
437 if (tcp)
438 getsockname(0, &local.u.sa, &local.len);
439 /* else: for UDP it is done earlier by parent */
440 local_addr = xmalloc_sockaddr2dotted(&local.u.sa);
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200441 if (opts & OPT_h) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000442 local_hostname = preset_local_hostname;
443 if (!local_hostname) {
444 free_me2 = local_hostname = xmalloc_sockaddr2host_noport(&local.u.sa);
445 if (!local_hostname)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100446 bb_error_msg_and_die("can't look up hostname for %s", local_addr);
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000447 }
448 /* else: local_hostname is not NULL, but is NOT malloced! */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000449 }
450 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000451 if (verbose) {
452 pid = getpid();
453 if (max_per_host) {
454 bb_error_msg("concurrency %s %u/%u",
455 remote_addr,
456 cur_per_host, max_per_host);
457 }
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200458 bb_error_msg((opts & OPT_h)
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000459 ? "start %u %s-%s (%s-%s)"
460 : "start %u %s-%s",
461 pid,
462 local_addr, remote_addr,
463 local_hostname, remote_hostname);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000464 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000465
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200466 if (!(opts & OPT_E)) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000467 /* setup ucspi env */
468 const char *proto = tcp ? "TCP" : "UDP";
469
Jeremie Koenig2ea12d82010-05-27 15:46:25 +0200470#ifdef SO_ORIGINAL_DST
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000471 /* Extract "original" destination addr:port
472 * from Linux firewall. Useful when you redirect
473 * an outbond connection to local handler, and it needs
474 * to know where it originally tried to connect */
475 if (tcp && getsockopt(0, SOL_IP, SO_ORIGINAL_DST, &local.u.sa, &local.len) == 0) {
476 char *addr = xmalloc_sockaddr2dotted(&local.u.sa);
477 xsetenv_plain("TCPORIGDSTADDR", addr);
478 free(addr);
479 }
Jeremie Koenig2ea12d82010-05-27 15:46:25 +0200480#endif
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000481 xsetenv_plain("PROTO", proto);
482 xsetenv_proto(proto, "LOCALADDR", local_addr);
483 xsetenv_proto(proto, "REMOTEADDR", remote_addr);
Denys Vlasenkod6513cf2009-07-19 23:07:13 +0200484 if (opts & OPT_h) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000485 xsetenv_proto(proto, "LOCALHOST", local_hostname);
486 xsetenv_proto(proto, "REMOTEHOST", remote_hostname);
487 }
488 //compat? xsetenv_proto(proto, "REMOTEINFO", "");
489 /* additional */
490 if (cur_per_host > 0) /* can not be true for udp */
491 xsetenv_plain("TCPCONCURRENCY", utoa(cur_per_host));
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000492 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000493 free(local_addr);
494 free(free_me0);
495 free(free_me1);
496 free(free_me2);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000497 }
498
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000499 xdup2(0, 1);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000500
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000501 signal(SIGPIPE, SIG_DFL); /* this one was SIG_IGNed */
502 /* Non-ignored signals revert to SIG_DFL on exec anyway */
503 /*signal(SIGCHLD, SIG_DFL);*/
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000504 sig_unblock(SIGCHLD);
505
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000506#ifdef SSLSVD
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000507 strcpy(id, utoa(pid));
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000508 ssl_io(0, argv);
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200509 bb_perror_msg_and_die("can't execute '%s'", argv[0]);
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200510#else
511 BB_EXECVP_or_die(argv);
512#endif
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000513}
514
515/*
516tcpsvd [-hpEvv] [-c n] [-C n:msg] [-b n] [-u user] [-l name]
517 [-i dir|-x cdb] [ -t sec] host port prog
518
519tcpsvd creates a TCP/IP socket, binds it to the address host:port,
520and listens on the socket for incoming connections.
521
522On each incoming connection, tcpsvd conditionally runs a program,
523with standard input reading from the socket, and standard output
524writing to the socket, to handle this connection. tcpsvd keeps
525listening on the socket for new connections, and can handle
526multiple connections simultaneously.
527
528tcpsvd optionally checks for special instructions depending
529on the IP address or hostname of the client that initiated
530the connection, see ipsvd-instruct(5).
531
532host
533 host either is a hostname, or a dotted-decimal IP address,
534 or 0. If host is 0, tcpsvd accepts connections to any local
535 IP address.
536 * busybox accepts IPv6 addresses and host:port pairs too
537 In this case second parameter is ignored
538port
539 tcpsvd accepts connections to host:port. port may be a name
540 from /etc/services or a number.
541prog
542 prog consists of one or more arguments. For each connection,
543 tcpsvd normally runs prog, with file descriptor 0 reading from
544 the network, and file descriptor 1 writing to the network.
545 By default it also sets up TCP-related environment variables,
546 see tcp-environ(5)
547-i dir
548 read instructions for handling new connections from the instructions
549 directory dir. See ipsvd-instruct(5) for details.
550 * ignored by busyboxed version
551-x cdb
552 read instructions for handling new connections from the constant database
553 cdb. The constant database normally is created from an instructions
554 directory by running ipsvd-cdb(8).
555 * ignored by busyboxed version
556-t sec
557 timeout. This option only takes effect if the -i option is given.
558 While checking the instructions directory, check the time of last access
559 of the file that matches the clients address or hostname if any, discard
560 and remove the file if it wasn't accessed within the last sec seconds;
561 tcpsvd does not discard or remove a file if the user's write permission
562 is not set, for those files the timeout is disabled. Default is 0,
563 which means that the timeout is disabled.
564 * ignored by busyboxed version
565-l name
566 local hostname. Do not look up the local hostname in DNS, but use name
567 as hostname. This option must be set if tcpsvd listens on port 53
568 to avoid loops.
569-u user[:group]
570 drop permissions. Switch user ID to user's UID, and group ID to user's
571 primary GID after creating and binding to the socket. If user is followed
572 by a colon and a group name, the group ID is switched to the GID of group
573 instead. All supplementary groups are removed.
574-c n
575 concurrency. Handle up to n connections simultaneously. Default is 30.
576 If there are n connections active, tcpsvd defers acceptance of a new
577 connection until an active connection is closed.
578-C n[:msg]
579 per host concurrency. Allow only up to n connections from the same IP
580 address simultaneously. If there are n active connections from one IP
581 address, new incoming connections from this IP address are closed
582 immediately. If n is followed by :msg, the message msg is written
583 to the client if possible, before closing the connection. By default
584 msg is empty. See ipsvd-instruct(5) for supported escape sequences in msg.
585
586 For each accepted connection, the current per host concurrency is
587 available through the environment variable TCPCONCURRENCY. n and msg
588 can be overwritten by ipsvd(7) instructions, see ipsvd-instruct(5).
589 By default tcpsvd doesn't keep track of connections.
590-h
591 Look up the client's hostname in DNS.
592-p
593 paranoid. After looking up the client's hostname in DNS, look up the IP
594 addresses in DNS for that hostname, and forget about the hostname
595 if none of the addresses match the client's IP address. You should
596 set this option if you use hostname based instructions. The -p option
597 implies the -h option.
598 * ignored by busyboxed version
599-b n
600 backlog. Allow a backlog of approximately n TCP SYNs. On some systems n
601 is silently limited. Default is 20.
602-E
603 no special environment. Do not set up TCP-related environment variables.
604-v
605 verbose. Print verbose messsages to standard output.
606-vv
607 more verbose. Print more verbose messages to standard output.
608 * no difference between -v and -vv in busyboxed version
609*/