blob: 55a3e0899ca3ad2931e80749e7aab141b2496bbc [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 *
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
8 */
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"
Denis Vlasenko165f5b32008-03-31 20:30:38 +000033/* Wants <limits.h> etc, thus included after libbb.h: */
Denis Vlasenkoc0cd9f22008-06-07 12:23:44 +000034#include <linux/types.h> /* for __be32 etc */
Denis Vlasenko165f5b32008-03-31 20:30:38 +000035#include <linux/netfilter_ipv4.h>
36
Denis Vlasenko4866e902008-03-17 09:17:27 +000037// TODO: move into this file:
38#include "tcpudp_perhost.h"
Denis Vlasenko02fd6682007-04-03 23:23:10 +000039
40#ifdef SSLSVD
41#include "matrixSsl.h"
42#include "ssl_io.h"
43#endif
44
Denis Vlasenkob9256052007-09-28 10:29:17 +000045struct globals {
46 unsigned verbose;
47 unsigned max_per_host;
48 unsigned cur_per_host;
49 unsigned cnum;
Denis Vlasenko3f5fdc72007-10-14 04:55:59 +000050 unsigned cmax;
Denis Vlasenkoaefed942008-03-17 08:35:44 +000051 char **env_cur;
52 char *env_var[1]; /* actually bigger */
Denis Vlasenkob9256052007-09-28 10:29:17 +000053};
54#define G (*(struct globals*)&bb_common_bufsiz1)
55#define verbose (G.verbose )
56#define max_per_host (G.max_per_host)
57#define cur_per_host (G.cur_per_host)
58#define cnum (G.cnum )
59#define cmax (G.cmax )
Denis Vlasenkoaefed942008-03-17 08:35:44 +000060#define env_cur (G.env_cur )
61#define env_var (G.env_var )
Denis Vlasenko7049ff82008-06-25 09:53:17 +000062#define INIT_G() do { \
63 cmax = 30; \
64 env_cur = &env_var[0]; \
65} while (0)
Denis Vlasenkob9256052007-09-28 10:29:17 +000066
Denis Vlasenko02fd6682007-04-03 23:23:10 +000067
Denis Vlasenkoaefed942008-03-17 08:35:44 +000068/* We have to be careful about leaking memory in repeated setenv's */
69static void xsetenv_plain(const char *n, const char *v)
70{
71 char *var = xasprintf("%s=%s", n, v);
72 *env_cur++ = var;
73 putenv(var);
74}
75
Denis Vlasenko02fd6682007-04-03 23:23:10 +000076static void xsetenv_proto(const char *proto, const char *n, const char *v)
77{
Denis Vlasenkoaefed942008-03-17 08:35:44 +000078 char *var = xasprintf("%s%s=%s", proto, n, v);
79 *env_cur++ = var;
80 putenv(var);
81}
82
83static void undo_xsetenv(void)
84{
85 char **pp = env_cur = &env_var[0];
86 while (*pp) {
87 char *var = *pp;
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +000088 bb_unsetenv(var);
Denis Vlasenkoaefed942008-03-17 08:35:44 +000089 free(var);
90 *pp++ = NULL;
91 }
Denis Vlasenko02fd6682007-04-03 23:23:10 +000092}
Denis Vlasenkob933ac12007-04-03 12:09:46 +000093
94static void sig_term_handler(int sig)
95{
96 if (verbose)
Denis Vlasenkoaefed942008-03-17 08:35:44 +000097 bb_error_msg("got signal %u, exit", sig);
Denis Vlasenko400d8bb2008-02-24 13:36:01 +000098 kill_myself_with_sig(sig);
Denis Vlasenkob933ac12007-04-03 12:09:46 +000099}
100
101/* Little bloated, but tries to give accurate info how child exited.
102 * Makes easier to spot segfaulting children etc... */
103static void print_waitstat(unsigned pid, int wstat)
104{
105 unsigned e = 0;
106 const char *cause = "?exit";
107
108 if (WIFEXITED(wstat)) {
109 cause++;
110 e = WEXITSTATUS(wstat);
111 } else if (WIFSIGNALED(wstat)) {
112 cause = "signal";
113 e = WTERMSIG(wstat);
114 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000115 bb_error_msg("end %d %s %d", pid, cause, e);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000116}
117
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000118/* Must match getopt32 in main! */
119enum {
120 OPT_c = (1 << 0),
121 OPT_C = (1 << 1),
122 OPT_i = (1 << 2),
123 OPT_x = (1 << 3),
124 OPT_u = (1 << 4),
125 OPT_l = (1 << 5),
126 OPT_E = (1 << 6),
127 OPT_b = (1 << 7),
128 OPT_h = (1 << 8),
129 OPT_p = (1 << 9),
130 OPT_t = (1 << 10),
131 OPT_v = (1 << 11),
132 OPT_V = (1 << 12),
133 OPT_U = (1 << 13), /* from here: sslsvd only */
134 OPT_slash = (1 << 14),
135 OPT_Z = (1 << 15),
136 OPT_K = (1 << 16),
137};
138
139static void connection_status(void)
140{
Denis Vlasenkof87f4952007-08-24 10:27:41 +0000141 /* "only 1 client max" desn't need this */
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000142 if (cmax > 1)
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000143 bb_error_msg("status %u/%u", cnum, cmax);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000144}
145
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000146static void sig_child_handler(int sig UNUSED_PARAM)
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000147{
148 int wstat;
Denis Vlasenko3854c5d2008-11-06 22:39:57 +0000149 pid_t pid;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000150
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000151 while ((pid = wait_any_nohang(&wstat)) > 0) {
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000152 if (max_per_host)
153 ipsvd_perhost_remove(pid);
154 if (cnum)
155 cnum--;
156 if (verbose)
157 print_waitstat(pid, wstat);
158 }
159 if (verbose)
160 connection_status();
161}
162
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000163int tcpudpsvd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000164int tcpudpsvd_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000165{
Denis Vlasenko1d426652008-03-17 09:09:09 +0000166 char *str_C, *str_t;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000167 char *user;
168 struct hcc *hccp;
169 const char *instructs;
170 char *msg_per_host = NULL;
171 unsigned len_per_host = len_per_host; /* gcc */
Denis Vlasenko64a15122007-04-04 10:16:15 +0000172#ifndef SSLSVD
173 struct bb_uidgid_t ugid;
174#endif
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000175 bool tcp;
Denis Vlasenko64a15122007-04-04 10:16:15 +0000176 uint16_t local_port;
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000177 char *preset_local_hostname = NULL;
178 char *remote_hostname = remote_hostname; /* for compiler */
179 char *remote_addr = remote_addr; /* for compiler */
Denis Vlasenko64a15122007-04-04 10:16:15 +0000180 len_and_sockaddr *lsa;
181 len_and_sockaddr local, remote;
182 socklen_t sa_len;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000183 int pid;
184 int sock;
185 int conn;
186 unsigned backlog = 20;
Denis Vlasenko64a15122007-04-04 10:16:15 +0000187
Denis Vlasenkob9256052007-09-28 10:29:17 +0000188 INIT_G();
189
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000190 tcp = (applet_name[0] == 't');
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000191
Denis Vlasenko1d426652008-03-17 09:09:09 +0000192 /* 3+ args, -i at most once, -p implies -h, -v is counter, -b N, -c N */
193 opt_complementary = "-3:i--i:ph:vv:b+:c+";
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000194#ifdef SSLSVD
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000195 getopt32(argv, "+c:C:i:x:u:l:Eb:hpt:vU:/:Z:K:",
Denis Vlasenko1d426652008-03-17 09:09:09 +0000196 &cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname,
197 &backlog, &str_t, &ssluser, &root, &cert, &key, &verbose
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000198 );
199#else
Denis Vlasenko1d426652008-03-17 09:09:09 +0000200 /* "+": stop on first non-option */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000201 getopt32(argv, "+c:C:i:x:u:l:Eb:hpt:v",
Denis Vlasenko1d426652008-03-17 09:09:09 +0000202 &cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname,
203 &backlog, &str_t, &verbose
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000204 );
205#endif
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000206 if (option_mask32 & OPT_C) { /* -C n[:message] */
207 max_per_host = bb_strtou(str_C, &str_C, 10);
208 if (str_C[0]) {
209 if (str_C[0] != ':')
210 bb_show_usage();
211 msg_per_host = str_C + 1;
212 len_per_host = strlen(msg_per_host);
213 }
214 }
215 if (max_per_host > cmax)
216 max_per_host = cmax;
217 if (option_mask32 & OPT_u) {
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +0000218 xget_uidgid(&ugid, user);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000219 }
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000220#ifdef SSLSVD
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000221 if (option_mask32 & OPT_U) ssluser = optarg;
222 if (option_mask32 & OPT_slash) root = optarg;
223 if (option_mask32 & OPT_Z) cert = optarg;
224 if (option_mask32 & OPT_K) key = optarg;
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000225#endif
226 argv += optind;
227 if (!argv[0][0] || LONE_CHAR(argv[0], '0'))
228 argv[0] = (char*)"0.0.0.0";
229
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000230 /* Per-IP flood protection is not thought-out for UDP */
231 if (!tcp)
232 max_per_host = 0;
233
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000234 bb_sanitize_stdio(); /* fd# 0,1,2 must be opened */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000235
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000236#ifdef SSLSVD
237 sslser = user;
238 client = 0;
239 if ((getuid() == 0) && !(option_mask32 & OPT_u)) {
240 xfunc_exitcode = 100;
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000241 bb_error_msg_and_die("-U ssluser must be set when running as root");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000242 }
243 if (option_mask32 & OPT_u)
244 if (!uidgid_get(&sslugid, ssluser, 1)) {
245 if (errno) {
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +0000246 bb_perror_msg_and_die("can't get user/group: %s", ssluser);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000247 }
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +0000248 bb_error_msg_and_die("unknown user/group %s", ssluser);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000249 }
250 if (!cert) cert = "./cert.pem";
251 if (!key) key = cert;
252 if (matrixSslOpen() < 0)
253 fatal("cannot initialize ssl");
254 if (matrixSslReadKeys(&keys, cert, key, 0, ca) < 0) {
255 if (client)
256 fatal("cannot read cert, key, or ca file");
257 fatal("cannot read cert or key file");
258 }
259 if (matrixSslNewSession(&ssl, keys, 0, SSL_FLAGS_SERVER) < 0)
260 fatal("cannot create ssl session");
261#endif
262
263 sig_block(SIGCHLD);
264 signal(SIGCHLD, sig_child_handler);
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000265 bb_signals(BB_FATAL_SIGS, sig_term_handler);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000266 signal(SIGPIPE, SIG_IGN);
267
268 if (max_per_host)
269 ipsvd_perhost_init(cmax);
270
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000271 local_port = bb_lookup_port(argv[1], tcp ? "tcp" : "udp", 0);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000272 lsa = xhost2sockaddr(argv[0], local_port);
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000273 argv += 2;
274
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000275 sock = xsocket(lsa->u.sa.sa_family, tcp ? SOCK_STREAM : SOCK_DGRAM, 0);
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000276 setsockopt_reuseaddr(sock);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000277 sa_len = lsa->len; /* I presume sockaddr len stays the same */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000278 xbind(sock, &lsa->u.sa, sa_len);
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000279 if (tcp)
280 xlisten(sock, backlog);
281 else /* udp: needed for recv_from_to to work: */
282 socket_want_pktinfo(sock);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000283 /* ndelay_off(sock); - it is the default I think? */
284
285#ifndef SSLSVD
286 if (option_mask32 & OPT_u) {
287 /* drop permissions */
288 xsetgid(ugid.gid);
289 xsetuid(ugid.uid);
290 }
291#endif
292
293 if (verbose) {
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000294 char *addr = xmalloc_sockaddr2dotted(&lsa->u.sa);
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000295 bb_error_msg("listening on %s, starting", addr);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000296 free(addr);
297#ifndef SSLSVD
298 if (option_mask32 & OPT_u)
299 printf(", uid %u, gid %u",
300 (unsigned)ugid.uid, (unsigned)ugid.gid);
301#endif
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000302 }
303
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000304 /* Main accept() loop */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000305
306 again:
307 hccp = NULL;
308
309 while (cnum >= cmax)
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000310 wait_for_any_sig(); /* expecting SIGCHLD */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000311
312 /* Accept a connection to fd #0 */
313 again1:
314 close(0);
315 again2:
316 sig_unblock(SIGCHLD);
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000317 local.len = remote.len = sa_len;
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000318 if (tcp) {
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000319 conn = accept(sock, &remote.u.sa, &remote.len);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000320 } else {
Denis Vlasenko1ca332b2007-04-04 17:49:47 +0000321 /* In case recv_from_to won't be able to recover local addr.
Denis Vlasenko64a15122007-04-04 10:16:15 +0000322 * Also sets port - recv_from_to is unable to do it. */
323 local = *lsa;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000324 conn = recv_from_to(sock, NULL, 0, MSG_PEEK,
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000325 &remote.u.sa, &local.u.sa, sa_len);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000326 }
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000327 sig_block(SIGCHLD);
328 if (conn < 0) {
329 if (errno != EINTR)
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000330 bb_perror_msg(tcp ? "accept" : "recv");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000331 goto again2;
332 }
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000333 xmove_fd(tcp ? conn : sock, 0);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000334
335 if (max_per_host) {
336 /* Drop connection immediately if cur_per_host > max_per_host
337 * (minimizing load under SYN flood) */
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000338 remote_addr = xmalloc_sockaddr2dotted_noport(&remote.u.sa);
339 cur_per_host = ipsvd_perhost_add(remote_addr, max_per_host, &hccp);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000340 if (cur_per_host > max_per_host) {
341 /* ipsvd_perhost_add detected that max is exceeded
342 * (and did not store ip in connection table) */
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000343 free(remote_addr);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000344 if (msg_per_host) {
345 /* don't block or test for errors */
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000346 send(0, msg_per_host, len_per_host, MSG_DONTWAIT);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000347 }
348 goto again1;
349 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000350 /* NB: remote_addr is not leaked, it is stored in conn table */
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000351 }
352
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000353 if (!tcp) {
354 /* Voodoo magic: making udp sockets each receive its own
Denis Vlasenko64a15122007-04-04 10:16:15 +0000355 * packets is not trivial, and I still not sure
356 * I do it 100% right.
357 * 1) we have to do it before fork()
358 * 2) order is important - is it right now? */
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000359
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000360 /* Open new non-connected UDP socket for further clients... */
361 sock = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
362 setsockopt_reuseaddr(sock);
363 /* Make plain write/send work for old socket by supplying default
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000364 * destination address. This also restricts incoming packets
365 * to ones coming from this remote IP. */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000366 xconnect(0, &remote.u.sa, sa_len);
Denis Vlasenko64a15122007-04-04 10:16:15 +0000367 /* hole? at this point we have no wildcard udp socket...
Denis Vlasenko79468792007-04-04 11:02:55 +0000368 * can this cause clients to get "port unreachable" icmp?
369 * Yup, time window is very small, but it exists (is it?) */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000370 /* ..."open new socket", continued */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000371 xbind(sock, &lsa->u.sa, sa_len);
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000372 socket_want_pktinfo(sock);
Denis Vlasenko79468792007-04-04 11:02:55 +0000373
374 /* Doesn't work:
375 * we cannot replace fd #0 - we will lose pending packet
376 * which is already buffered for us! And we cannot use fd #1
377 * instead - it will "intercept" all following packets, but child
Denis Vlasenkof87f4952007-08-24 10:27:41 +0000378 * does not expect data coming *from fd #1*! */
Denis Vlasenko79468792007-04-04 11:02:55 +0000379#if 0
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000380 /* Make it so that local addr is fixed to localp->u.sa
Denis Vlasenko79468792007-04-04 11:02:55 +0000381 * and we don't accidentally accept packets to other local IPs. */
382 /* NB: we possibly bind to the _very_ same_ address & port as the one
383 * already bound in parent! This seems to work in Linux.
384 * (otherwise we can move socket to fd #0 only if bind succeeds) */
385 close(0);
386 set_nport(localp, htons(local_port));
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000387 xmove_fd(xsocket(localp->u.sa.sa_family, SOCK_DGRAM, 0), 0);
Denis Vlasenko79468792007-04-04 11:02:55 +0000388 setsockopt_reuseaddr(0); /* crucial */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000389 xbind(0, &localp->u.sa, localp->len);
Denis Vlasenko79468792007-04-04 11:02:55 +0000390#endif
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000391 }
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000392
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000393 pid = vfork();
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000394 if (pid == -1) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000395 bb_perror_msg("vfork");
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000396 goto again;
397 }
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000398
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000399 if (pid != 0) {
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000400 /* Parent */
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000401 cnum++;
402 if (verbose)
403 connection_status();
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000404 if (hccp)
405 hccp->pid = pid;
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000406 /* clean up changes done by vforked child */
407 undo_xsetenv();
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000408 goto again;
409 }
410
411 /* Child: prepare env, log, and exec prog */
412
Denis Vlasenko02fd6682007-04-03 23:23:10 +0000413 /* Closing tcp listening socket */
414 if (tcp)
415 close(sock);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000416
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
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000424 if (verbose || !(option_mask32 & OPT_E)) {
425 if (!max_per_host) /* remote_addr is not yet known */
426 free_me0 = remote_addr = xmalloc_sockaddr2dotted(&remote.u.sa);
427 if (option_mask32 & OPT_h) {
428 free_me1 = remote_hostname = xmalloc_sockaddr2host_noport(&remote.u.sa);
429 if (!remote_hostname) {
430 bb_error_msg("cannot look up hostname for %s", remote_addr);
431 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);
441 if (option_mask32 & OPT_h) {
442 local_hostname = preset_local_hostname;
443 if (!local_hostname) {
444 free_me2 = local_hostname = xmalloc_sockaddr2host_noport(&local.u.sa);
445 if (!local_hostname)
446 bb_error_msg_and_die("cannot look up hostname for %s", local_addr);
447 }
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 }
458 bb_error_msg((option_mask32 & OPT_h)
459 ? "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
466 if (!(option_mask32 & OPT_E)) {
467 /* setup ucspi env */
468 const char *proto = tcp ? "TCP" : "UDP";
469
470 /* Extract "original" destination addr:port
471 * from Linux firewall. Useful when you redirect
472 * an outbond connection to local handler, and it needs
473 * to know where it originally tried to connect */
474 if (tcp && getsockopt(0, SOL_IP, SO_ORIGINAL_DST, &local.u.sa, &local.len) == 0) {
475 char *addr = xmalloc_sockaddr2dotted(&local.u.sa);
476 xsetenv_plain("TCPORIGDSTADDR", addr);
477 free(addr);
478 }
479 xsetenv_plain("PROTO", proto);
480 xsetenv_proto(proto, "LOCALADDR", local_addr);
481 xsetenv_proto(proto, "REMOTEADDR", remote_addr);
482 if (option_mask32 & OPT_h) {
483 xsetenv_proto(proto, "LOCALHOST", local_hostname);
484 xsetenv_proto(proto, "REMOTEHOST", remote_hostname);
485 }
486 //compat? xsetenv_proto(proto, "REMOTEINFO", "");
487 /* additional */
488 if (cur_per_host > 0) /* can not be true for udp */
489 xsetenv_plain("TCPCONCURRENCY", utoa(cur_per_host));
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000490 }
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000491 free(local_addr);
492 free(free_me0);
493 free(free_me1);
494 free(free_me2);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000495 }
496
Denis Vlasenkoaefed942008-03-17 08:35:44 +0000497 xdup2(0, 1);
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000498
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000499 signal(SIGPIPE, SIG_DFL); /* this one was SIG_IGNed */
500 /* Non-ignored signals revert to SIG_DFL on exec anyway */
501 /*signal(SIGCHLD, SIG_DFL);*/
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000502 sig_unblock(SIGCHLD);
503
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000504#ifdef SSLSVD
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000505 strcpy(id, utoa(pid));
Denis Vlasenkob933ac12007-04-03 12:09:46 +0000506 ssl_io(0, argv);
507#else
508 BB_EXECVP(argv[0], argv);
509#endif
510 bb_perror_msg_and_die("exec '%s'", argv[0]);
511}
512
513/*
514tcpsvd [-hpEvv] [-c n] [-C n:msg] [-b n] [-u user] [-l name]
515 [-i dir|-x cdb] [ -t sec] host port prog
516
517tcpsvd creates a TCP/IP socket, binds it to the address host:port,
518and listens on the socket for incoming connections.
519
520On each incoming connection, tcpsvd conditionally runs a program,
521with standard input reading from the socket, and standard output
522writing to the socket, to handle this connection. tcpsvd keeps
523listening on the socket for new connections, and can handle
524multiple connections simultaneously.
525
526tcpsvd optionally checks for special instructions depending
527on the IP address or hostname of the client that initiated
528the connection, see ipsvd-instruct(5).
529
530host
531 host either is a hostname, or a dotted-decimal IP address,
532 or 0. If host is 0, tcpsvd accepts connections to any local
533 IP address.
534 * busybox accepts IPv6 addresses and host:port pairs too
535 In this case second parameter is ignored
536port
537 tcpsvd accepts connections to host:port. port may be a name
538 from /etc/services or a number.
539prog
540 prog consists of one or more arguments. For each connection,
541 tcpsvd normally runs prog, with file descriptor 0 reading from
542 the network, and file descriptor 1 writing to the network.
543 By default it also sets up TCP-related environment variables,
544 see tcp-environ(5)
545-i dir
546 read instructions for handling new connections from the instructions
547 directory dir. See ipsvd-instruct(5) for details.
548 * ignored by busyboxed version
549-x cdb
550 read instructions for handling new connections from the constant database
551 cdb. The constant database normally is created from an instructions
552 directory by running ipsvd-cdb(8).
553 * ignored by busyboxed version
554-t sec
555 timeout. This option only takes effect if the -i option is given.
556 While checking the instructions directory, check the time of last access
557 of the file that matches the clients address or hostname if any, discard
558 and remove the file if it wasn't accessed within the last sec seconds;
559 tcpsvd does not discard or remove a file if the user's write permission
560 is not set, for those files the timeout is disabled. Default is 0,
561 which means that the timeout is disabled.
562 * ignored by busyboxed version
563-l name
564 local hostname. Do not look up the local hostname in DNS, but use name
565 as hostname. This option must be set if tcpsvd listens on port 53
566 to avoid loops.
567-u user[:group]
568 drop permissions. Switch user ID to user's UID, and group ID to user's
569 primary GID after creating and binding to the socket. If user is followed
570 by a colon and a group name, the group ID is switched to the GID of group
571 instead. All supplementary groups are removed.
572-c n
573 concurrency. Handle up to n connections simultaneously. Default is 30.
574 If there are n connections active, tcpsvd defers acceptance of a new
575 connection until an active connection is closed.
576-C n[:msg]
577 per host concurrency. Allow only up to n connections from the same IP
578 address simultaneously. If there are n active connections from one IP
579 address, new incoming connections from this IP address are closed
580 immediately. If n is followed by :msg, the message msg is written
581 to the client if possible, before closing the connection. By default
582 msg is empty. See ipsvd-instruct(5) for supported escape sequences in msg.
583
584 For each accepted connection, the current per host concurrency is
585 available through the environment variable TCPCONCURRENCY. n and msg
586 can be overwritten by ipsvd(7) instructions, see ipsvd-instruct(5).
587 By default tcpsvd doesn't keep track of connections.
588-h
589 Look up the client's hostname in DNS.
590-p
591 paranoid. After looking up the client's hostname in DNS, look up the IP
592 addresses in DNS for that hostname, and forget about the hostname
593 if none of the addresses match the client's IP address. You should
594 set this option if you use hostname based instructions. The -p option
595 implies the -h option.
596 * ignored by busyboxed version
597-b n
598 backlog. Allow a backlog of approximately n TCP SYNs. On some systems n
599 is silently limited. Default is 20.
600-E
601 no special environment. Do not set up TCP-related environment variables.
602-v
603 verbose. Print verbose messsages to standard output.
604-vv
605 more verbose. Print more verbose messages to standard output.
606 * no difference between -v and -vv in busyboxed version
607*/