diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-03-17 09:17:27 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-03-17 09:17:27 +0000 |
commit | 4866e905d7e1f11d86374fad4e46aa2bd669c2ba (patch) | |
tree | a6a8e9521de92d658903ba9f54cd01e94a34c262 /ipsvd | |
parent | 4ee7cd4f6f9f85871c8814bb524d3e691a2992a9 (diff) | |
download | busybox-w32-4866e905d7e1f11d86374fad4e46aa2bd669c2ba.tar.gz busybox-w32-4866e905d7e1f11d86374fad4e46aa2bd669c2ba.tar.bz2 busybox-w32-4866e905d7e1f11d86374fad4e46aa2bd669c2ba.zip |
svn add/svn rm to actually move tcp/udpsvd...
Diffstat (limited to 'ipsvd')
-rw-r--r-- | ipsvd/Config.in | 20 | ||||
-rw-r--r-- | ipsvd/Kbuild | 9 | ||||
-rw-r--r-- | ipsvd/ipsvd_perhost.c | 65 | ||||
-rw-r--r-- | ipsvd/ipsvd_perhost.h | 29 | ||||
-rw-r--r-- | ipsvd/tcpudp.c | 608 |
5 files changed, 0 insertions, 731 deletions
diff --git a/ipsvd/Config.in b/ipsvd/Config.in deleted file mode 100644 index 0cb8c62de..000000000 --- a/ipsvd/Config.in +++ /dev/null | |||
@@ -1,20 +0,0 @@ | |||
1 | # | ||
2 | # For a description of the syntax of this configuration file, | ||
3 | # see scripts/kbuild/config-language.txt. | ||
4 | # | ||
5 | |||
6 | menu "ipsvd Utilities" | ||
7 | |||
8 | config TCPSVD | ||
9 | bool "tcpsvd" | ||
10 | default n | ||
11 | help | ||
12 | tcpsvd listens on a tcp port and runs a program for each new connection | ||
13 | |||
14 | config UDPSVD | ||
15 | bool "udpsvd" | ||
16 | default n | ||
17 | help | ||
18 | udpsvd listens on a udp port and runs a program for each new connection | ||
19 | |||
20 | endmenu | ||
diff --git a/ipsvd/Kbuild b/ipsvd/Kbuild deleted file mode 100644 index fc34fea49..000000000 --- a/ipsvd/Kbuild +++ /dev/null | |||
@@ -1,9 +0,0 @@ | |||
1 | # Makefile for busybox | ||
2 | # | ||
3 | # Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org> | ||
4 | # | ||
5 | # Licensed under the GPL v2, see the file LICENSE in this tarball. | ||
6 | |||
7 | lib-y:= | ||
8 | lib-$(CONFIG_TCPSVD) += tcpudp.o ipsvd_perhost.o | ||
9 | lib-$(CONFIG_UDPSVD) += tcpudp.o ipsvd_perhost.o | ||
diff --git a/ipsvd/ipsvd_perhost.c b/ipsvd/ipsvd_perhost.c deleted file mode 100644 index 6075c0a3a..000000000 --- a/ipsvd/ipsvd_perhost.c +++ /dev/null | |||
@@ -1,65 +0,0 @@ | |||
1 | /* Based on ipsvd utilities written by Gerrit Pape <pape@smarden.org> | ||
2 | * which are released into public domain by the author. | ||
3 | * Homepage: http://smarden.sunsite.dk/ipsvd/ | ||
4 | * | ||
5 | * Copyright (C) 2007 Denys Vlasenko. | ||
6 | * | ||
7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. | ||
8 | */ | ||
9 | |||
10 | #include "libbb.h" | ||
11 | #include "ipsvd_perhost.h" | ||
12 | |||
13 | static struct hcc *cc; | ||
14 | static unsigned cclen; | ||
15 | |||
16 | /* to be optimized */ | ||
17 | |||
18 | void ipsvd_perhost_init(unsigned c) | ||
19 | { | ||
20 | // free(cc); | ||
21 | cc = xzalloc(c * sizeof(*cc)); | ||
22 | cclen = c; | ||
23 | } | ||
24 | |||
25 | unsigned ipsvd_perhost_add(char *ip, unsigned maxconn, struct hcc **hccpp) | ||
26 | { | ||
27 | unsigned i; | ||
28 | unsigned conn = 1; | ||
29 | int freepos = -1; | ||
30 | |||
31 | for (i = 0; i < cclen; ++i) { | ||
32 | if (!cc[i].ip) { | ||
33 | freepos = i; | ||
34 | continue; | ||
35 | } | ||
36 | if (strcmp(cc[i].ip, ip) == 0) { | ||
37 | conn++; | ||
38 | continue; | ||
39 | } | ||
40 | } | ||
41 | if (freepos == -1) return 0; | ||
42 | if (conn <= maxconn) { | ||
43 | cc[freepos].ip = ip; | ||
44 | *hccpp = &cc[freepos]; | ||
45 | } | ||
46 | return conn; | ||
47 | } | ||
48 | |||
49 | void ipsvd_perhost_remove(int pid) | ||
50 | { | ||
51 | unsigned i; | ||
52 | for (i = 0; i < cclen; ++i) { | ||
53 | if (cc[i].pid == pid) { | ||
54 | free(cc[i].ip); | ||
55 | cc[i].ip = NULL; | ||
56 | cc[i].pid = 0; | ||
57 | return; | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | |||
62 | //void ipsvd_perhost_free(void) | ||
63 | //{ | ||
64 | // free(cc); | ||
65 | //} | ||
diff --git a/ipsvd/ipsvd_perhost.h b/ipsvd/ipsvd_perhost.h deleted file mode 100644 index 9fc8cee61..000000000 --- a/ipsvd/ipsvd_perhost.h +++ /dev/null | |||
@@ -1,29 +0,0 @@ | |||
1 | /* Based on ipsvd utilities written by Gerrit Pape <pape@smarden.org> | ||
2 | * which are released into public domain by the author. | ||
3 | * Homepage: http://smarden.sunsite.dk/ipsvd/ | ||
4 | * | ||
5 | * Copyright (C) 2007 Denys Vlasenko. | ||
6 | * | ||
7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. | ||
8 | */ | ||
9 | |||
10 | struct hcc { | ||
11 | char *ip; | ||
12 | int pid; | ||
13 | }; | ||
14 | |||
15 | void ipsvd_perhost_init(unsigned); | ||
16 | |||
17 | /* Returns number of already opened connects to this ips, including this one. | ||
18 | * ip should be a malloc'ed ptr. | ||
19 | * If return value is <= maxconn, ip is inserted into the table | ||
20 | * and pointer to table entry if stored in *hccpp | ||
21 | * (useful for storing pid later). | ||
22 | * Else ip is NOT inserted (you must take care of it - free() etc) */ | ||
23 | unsigned ipsvd_perhost_add(char *ip, unsigned maxconn, struct hcc **hccpp); | ||
24 | |||
25 | /* Finds and frees element with pid */ | ||
26 | void ipsvd_perhost_remove(int pid); | ||
27 | |||
28 | //unsigned ipsvd_perhost_setpid(int pid); | ||
29 | //void ipsvd_perhost_free(void); | ||
diff --git a/ipsvd/tcpudp.c b/ipsvd/tcpudp.c deleted file mode 100644 index 729b7bca9..000000000 --- a/ipsvd/tcpudp.c +++ /dev/null | |||
@@ -1,608 +0,0 @@ | |||
1 | /* Based on ipsvd utilities written by Gerrit Pape <pape@smarden.org> | ||
2 | * which are released into public domain by the author. | ||
3 | * Homepage: http://smarden.sunsite.dk/ipsvd/ | ||
4 | * | ||
5 | * Copyright (C) 2007 Denys Vlasenko. | ||
6 | * | ||
7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. | ||
8 | */ | ||
9 | |||
10 | /* Based on ipsvd ipsvd-0.12.1. This tcpsvd accepts all options | ||
11 | * 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 | * | ||
16 | * 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!). | ||
20 | * | ||
21 | * TCPORIGDSTADDR is Busybox-specific addition. | ||
22 | * | ||
23 | * udp server is hacked up by reusing TCP code. It has the following | ||
24 | * limitation inherent in Unix DGRAM sockets implementation: | ||
25 | * - local IP address is retrieved (using recvmsg voodoo) but | ||
26 | * child's socket is not bound to it (bind cannot be called on | ||
27 | * already bound socket). Thus it still can emit outgoing packets | ||
28 | * with wrong source IP... | ||
29 | * - don't know how to retrieve ORIGDST for udp. | ||
30 | */ | ||
31 | |||
32 | #include <limits.h> | ||
33 | #include <linux/netfilter_ipv4.h> /* wants <limits.h> */ | ||
34 | |||
35 | #include "libbb.h" | ||
36 | #include "ipsvd_perhost.h" | ||
37 | |||
38 | #ifdef SSLSVD | ||
39 | #include "matrixSsl.h" | ||
40 | #include "ssl_io.h" | ||
41 | #endif | ||
42 | |||
43 | struct globals { | ||
44 | unsigned verbose; | ||
45 | unsigned max_per_host; | ||
46 | unsigned cur_per_host; | ||
47 | unsigned cnum; | ||
48 | unsigned cmax; | ||
49 | char **env_cur; | ||
50 | char *env_var[1]; /* actually bigger */ | ||
51 | }; | ||
52 | #define G (*(struct globals*)&bb_common_bufsiz1) | ||
53 | #define verbose (G.verbose ) | ||
54 | #define max_per_host (G.max_per_host) | ||
55 | #define cur_per_host (G.cur_per_host) | ||
56 | #define cnum (G.cnum ) | ||
57 | #define cmax (G.cmax ) | ||
58 | #define env_cur (G.env_cur ) | ||
59 | #define env_var (G.env_var ) | ||
60 | #define INIT_G() \ | ||
61 | do { \ | ||
62 | cmax = 30; \ | ||
63 | env_cur = &env_var[0]; \ | ||
64 | } while (0) | ||
65 | |||
66 | |||
67 | /* We have to be careful about leaking memory in repeated setenv's */ | ||
68 | static void xsetenv_plain(const char *n, const char *v) | ||
69 | { | ||
70 | char *var = xasprintf("%s=%s", n, v); | ||
71 | *env_cur++ = var; | ||
72 | putenv(var); | ||
73 | } | ||
74 | |||
75 | static void xsetenv_proto(const char *proto, const char *n, const char *v) | ||
76 | { | ||
77 | char *var = xasprintf("%s%s=%s", proto, n, v); | ||
78 | *env_cur++ = var; | ||
79 | putenv(var); | ||
80 | } | ||
81 | |||
82 | static void undo_xsetenv(void) | ||
83 | { | ||
84 | char **pp = env_cur = &env_var[0]; | ||
85 | while (*pp) { | ||
86 | char *var = *pp; | ||
87 | *strchrnul(var, '=') = '\0'; | ||
88 | unsetenv(var); | ||
89 | free(var); | ||
90 | *pp++ = NULL; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | static void sig_term_handler(int sig) | ||
95 | { | ||
96 | if (verbose) | ||
97 | bb_error_msg("got signal %u, exit", sig); | ||
98 | kill_myself_with_sig(sig); | ||
99 | } | ||
100 | |||
101 | /* Little bloated, but tries to give accurate info how child exited. | ||
102 | * Makes easier to spot segfaulting children etc... */ | ||
103 | static 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 | } | ||
115 | bb_error_msg("end %d %s %d", pid, cause, e); | ||
116 | } | ||
117 | |||
118 | /* Must match getopt32 in main! */ | ||
119 | enum { | ||
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 | |||
139 | static void connection_status(void) | ||
140 | { | ||
141 | /* "only 1 client max" desn't need this */ | ||
142 | if (cmax > 1) | ||
143 | bb_error_msg("status %u/%u", cnum, cmax); | ||
144 | } | ||
145 | |||
146 | static void sig_child_handler(int sig ATTRIBUTE_UNUSED) | ||
147 | { | ||
148 | int wstat; | ||
149 | int pid; | ||
150 | |||
151 | while ((pid = wait_any_nohang(&wstat)) > 0) { | ||
152 | 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 | |||
163 | int tcpudpsvd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
164 | int tcpudpsvd_main(int argc ATTRIBUTE_UNUSED, char **argv) | ||
165 | { | ||
166 | char *str_C, *str_t; | ||
167 | 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 */ | ||
172 | #ifndef SSLSVD | ||
173 | struct bb_uidgid_t ugid; | ||
174 | #endif | ||
175 | bool tcp; | ||
176 | uint16_t local_port; | ||
177 | char *preset_local_hostname = NULL; | ||
178 | char *remote_hostname = remote_hostname; /* for compiler */ | ||
179 | char *remote_addr = remote_addr; /* for compiler */ | ||
180 | len_and_sockaddr *lsa; | ||
181 | len_and_sockaddr local, remote; | ||
182 | socklen_t sa_len; | ||
183 | int pid; | ||
184 | int sock; | ||
185 | int conn; | ||
186 | unsigned backlog = 20; | ||
187 | |||
188 | INIT_G(); | ||
189 | |||
190 | tcp = (applet_name[0] == 't'); | ||
191 | |||
192 | /* 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+"; | ||
194 | #ifdef SSLSVD | ||
195 | getopt32(argv, "+c:C:i:x:u:l:Eb:hpt:vU:/:Z:K:", | ||
196 | &cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname, | ||
197 | &backlog, &str_t, &ssluser, &root, &cert, &key, &verbose | ||
198 | ); | ||
199 | #else | ||
200 | /* "+": stop on first non-option */ | ||
201 | getopt32(argv, "+c:C:i:x:u:l:Eb:hpt:v", | ||
202 | &cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname, | ||
203 | &backlog, &str_t, &verbose | ||
204 | ); | ||
205 | #endif | ||
206 | 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) { | ||
218 | if (!get_uidgid(&ugid, user, 1)) | ||
219 | bb_error_msg_and_die("unknown user/group: %s", user); | ||
220 | } | ||
221 | #ifdef SSLSVD | ||
222 | if (option_mask32 & OPT_U) ssluser = optarg; | ||
223 | if (option_mask32 & OPT_slash) root = optarg; | ||
224 | if (option_mask32 & OPT_Z) cert = optarg; | ||
225 | if (option_mask32 & OPT_K) key = optarg; | ||
226 | #endif | ||
227 | argv += optind; | ||
228 | if (!argv[0][0] || LONE_CHAR(argv[0], '0')) | ||
229 | argv[0] = (char*)"0.0.0.0"; | ||
230 | |||
231 | /* Per-IP flood protection is not thought-out for UDP */ | ||
232 | if (!tcp) | ||
233 | max_per_host = 0; | ||
234 | |||
235 | bb_sanitize_stdio(); /* fd# 0,1,2 must be opened */ | ||
236 | |||
237 | #ifdef SSLSVD | ||
238 | sslser = user; | ||
239 | client = 0; | ||
240 | if ((getuid() == 0) && !(option_mask32 & OPT_u)) { | ||
241 | xfunc_exitcode = 100; | ||
242 | bb_error_msg_and_die("-U ssluser must be set when running as root"); | ||
243 | } | ||
244 | if (option_mask32 & OPT_u) | ||
245 | if (!uidgid_get(&sslugid, ssluser, 1)) { | ||
246 | if (errno) { | ||
247 | bb_perror_msg_and_die("fatal: cannot get user/group: %s", ssluser); | ||
248 | } | ||
249 | bb_error_msg_and_die("unknown user/group '%s'", ssluser); | ||
250 | } | ||
251 | if (!cert) cert = "./cert.pem"; | ||
252 | if (!key) key = cert; | ||
253 | if (matrixSslOpen() < 0) | ||
254 | fatal("cannot initialize ssl"); | ||
255 | if (matrixSslReadKeys(&keys, cert, key, 0, ca) < 0) { | ||
256 | if (client) | ||
257 | fatal("cannot read cert, key, or ca file"); | ||
258 | fatal("cannot read cert or key file"); | ||
259 | } | ||
260 | if (matrixSslNewSession(&ssl, keys, 0, SSL_FLAGS_SERVER) < 0) | ||
261 | fatal("cannot create ssl session"); | ||
262 | #endif | ||
263 | |||
264 | sig_block(SIGCHLD); | ||
265 | signal(SIGCHLD, sig_child_handler); | ||
266 | bb_signals(BB_SIGS_FATAL, sig_term_handler); | ||
267 | signal(SIGPIPE, SIG_IGN); | ||
268 | |||
269 | if (max_per_host) | ||
270 | ipsvd_perhost_init(cmax); | ||
271 | |||
272 | local_port = bb_lookup_port(argv[1], tcp ? "tcp" : "udp", 0); | ||
273 | lsa = xhost2sockaddr(argv[0], local_port); | ||
274 | argv += 2; | ||
275 | |||
276 | sock = xsocket(lsa->u.sa.sa_family, tcp ? SOCK_STREAM : SOCK_DGRAM, 0); | ||
277 | setsockopt_reuseaddr(sock); | ||
278 | sa_len = lsa->len; /* I presume sockaddr len stays the same */ | ||
279 | xbind(sock, &lsa->u.sa, sa_len); | ||
280 | if (tcp) | ||
281 | xlisten(sock, backlog); | ||
282 | else /* udp: needed for recv_from_to to work: */ | ||
283 | socket_want_pktinfo(sock); | ||
284 | /* ndelay_off(sock); - it is the default I think? */ | ||
285 | |||
286 | #ifndef SSLSVD | ||
287 | if (option_mask32 & OPT_u) { | ||
288 | /* drop permissions */ | ||
289 | xsetgid(ugid.gid); | ||
290 | xsetuid(ugid.uid); | ||
291 | } | ||
292 | #endif | ||
293 | |||
294 | if (verbose) { | ||
295 | char *addr = xmalloc_sockaddr2dotted(&lsa->u.sa); | ||
296 | bb_error_msg("listening on %s, starting", addr); | ||
297 | free(addr); | ||
298 | #ifndef SSLSVD | ||
299 | if (option_mask32 & OPT_u) | ||
300 | printf(", uid %u, gid %u", | ||
301 | (unsigned)ugid.uid, (unsigned)ugid.gid); | ||
302 | #endif | ||
303 | } | ||
304 | |||
305 | /* Main accept() loop */ | ||
306 | |||
307 | again: | ||
308 | hccp = NULL; | ||
309 | |||
310 | while (cnum >= cmax) | ||
311 | wait_for_any_sig(); /* expecting SIGCHLD */ | ||
312 | |||
313 | /* Accept a connection to fd #0 */ | ||
314 | again1: | ||
315 | close(0); | ||
316 | again2: | ||
317 | sig_unblock(SIGCHLD); | ||
318 | local.len = remote.len = sa_len; | ||
319 | if (tcp) { | ||
320 | conn = accept(sock, &remote.u.sa, &remote.len); | ||
321 | } else { | ||
322 | /* In case recv_from_to won't be able to recover local addr. | ||
323 | * Also sets port - recv_from_to is unable to do it. */ | ||
324 | local = *lsa; | ||
325 | conn = recv_from_to(sock, NULL, 0, MSG_PEEK, | ||
326 | &remote.u.sa, &local.u.sa, sa_len); | ||
327 | } | ||
328 | sig_block(SIGCHLD); | ||
329 | if (conn < 0) { | ||
330 | if (errno != EINTR) | ||
331 | bb_perror_msg(tcp ? "accept" : "recv"); | ||
332 | goto again2; | ||
333 | } | ||
334 | xmove_fd(tcp ? conn : sock, 0); | ||
335 | |||
336 | if (max_per_host) { | ||
337 | /* Drop connection immediately if cur_per_host > max_per_host | ||
338 | * (minimizing load under SYN flood) */ | ||
339 | remote_addr = xmalloc_sockaddr2dotted_noport(&remote.u.sa); | ||
340 | cur_per_host = ipsvd_perhost_add(remote_addr, max_per_host, &hccp); | ||
341 | if (cur_per_host > max_per_host) { | ||
342 | /* ipsvd_perhost_add detected that max is exceeded | ||
343 | * (and did not store ip in connection table) */ | ||
344 | free(remote_addr); | ||
345 | if (msg_per_host) { | ||
346 | /* don't block or test for errors */ | ||
347 | send(0, msg_per_host, len_per_host, MSG_DONTWAIT); | ||
348 | } | ||
349 | goto again1; | ||
350 | } | ||
351 | /* NB: remote_addr is not leaked, it is stored in conn table */ | ||
352 | } | ||
353 | |||
354 | if (!tcp) { | ||
355 | /* Voodoo magic: making udp sockets each receive its own | ||
356 | * packets is not trivial, and I still not sure | ||
357 | * I do it 100% right. | ||
358 | * 1) we have to do it before fork() | ||
359 | * 2) order is important - is it right now? */ | ||
360 | |||
361 | /* Open new non-connected UDP socket for further clients... */ | ||
362 | sock = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0); | ||
363 | setsockopt_reuseaddr(sock); | ||
364 | /* Make plain write/send work for old socket by supplying default | ||
365 | * destination address. This also restricts incoming packets | ||
366 | * to ones coming from this remote IP. */ | ||
367 | xconnect(0, &remote.u.sa, sa_len); | ||
368 | /* hole? at this point we have no wildcard udp socket... | ||
369 | * can this cause clients to get "port unreachable" icmp? | ||
370 | * Yup, time window is very small, but it exists (is it?) */ | ||
371 | /* ..."open new socket", continued */ | ||
372 | xbind(sock, &lsa->u.sa, sa_len); | ||
373 | socket_want_pktinfo(sock); | ||
374 | |||
375 | /* Doesn't work: | ||
376 | * we cannot replace fd #0 - we will lose pending packet | ||
377 | * which is already buffered for us! And we cannot use fd #1 | ||
378 | * instead - it will "intercept" all following packets, but child | ||
379 | * does not expect data coming *from fd #1*! */ | ||
380 | #if 0 | ||
381 | /* Make it so that local addr is fixed to localp->u.sa | ||
382 | * and we don't accidentally accept packets to other local IPs. */ | ||
383 | /* NB: we possibly bind to the _very_ same_ address & port as the one | ||
384 | * already bound in parent! This seems to work in Linux. | ||
385 | * (otherwise we can move socket to fd #0 only if bind succeeds) */ | ||
386 | close(0); | ||
387 | set_nport(localp, htons(local_port)); | ||
388 | xmove_fd(xsocket(localp->u.sa.sa_family, SOCK_DGRAM, 0), 0); | ||
389 | setsockopt_reuseaddr(0); /* crucial */ | ||
390 | xbind(0, &localp->u.sa, localp->len); | ||
391 | #endif | ||
392 | } | ||
393 | |||
394 | pid = vfork(); | ||
395 | if (pid == -1) { | ||
396 | bb_perror_msg("vfork"); | ||
397 | goto again; | ||
398 | } | ||
399 | |||
400 | if (pid != 0) { | ||
401 | /* Parent */ | ||
402 | cnum++; | ||
403 | if (verbose) | ||
404 | connection_status(); | ||
405 | if (hccp) | ||
406 | hccp->pid = pid; | ||
407 | /* clean up changes done by vforked child */ | ||
408 | undo_xsetenv(); | ||
409 | goto again; | ||
410 | } | ||
411 | |||
412 | /* Child: prepare env, log, and exec prog */ | ||
413 | |||
414 | /* Closing tcp listening socket */ | ||
415 | if (tcp) | ||
416 | close(sock); | ||
417 | |||
418 | { /* vfork alert! every xmalloc in this block should be freed! */ | ||
419 | char *local_hostname = local_hostname; /* for compiler */ | ||
420 | char *local_addr = NULL; | ||
421 | char *free_me0 = NULL; | ||
422 | char *free_me1 = NULL; | ||
423 | char *free_me2 = NULL; | ||
424 | |||
425 | if (verbose || !(option_mask32 & OPT_E)) { | ||
426 | if (!max_per_host) /* remote_addr is not yet known */ | ||
427 | free_me0 = remote_addr = xmalloc_sockaddr2dotted(&remote.u.sa); | ||
428 | if (option_mask32 & OPT_h) { | ||
429 | free_me1 = remote_hostname = xmalloc_sockaddr2host_noport(&remote.u.sa); | ||
430 | if (!remote_hostname) { | ||
431 | bb_error_msg("cannot look up hostname for %s", remote_addr); | ||
432 | remote_hostname = remote_addr; | ||
433 | } | ||
434 | } | ||
435 | /* Find out local IP peer connected to. | ||
436 | * Errors ignored (I'm not paranoid enough to imagine kernel | ||
437 | * which doesn't know local IP). */ | ||
438 | if (tcp) | ||
439 | getsockname(0, &local.u.sa, &local.len); | ||
440 | /* else: for UDP it is done earlier by parent */ | ||
441 | local_addr = xmalloc_sockaddr2dotted(&local.u.sa); | ||
442 | if (option_mask32 & OPT_h) { | ||
443 | local_hostname = preset_local_hostname; | ||
444 | if (!local_hostname) { | ||
445 | free_me2 = local_hostname = xmalloc_sockaddr2host_noport(&local.u.sa); | ||
446 | if (!local_hostname) | ||
447 | bb_error_msg_and_die("cannot look up hostname for %s", local_addr); | ||
448 | } | ||
449 | /* else: local_hostname is not NULL, but is NOT malloced! */ | ||
450 | } | ||
451 | } | ||
452 | if (verbose) { | ||
453 | pid = getpid(); | ||
454 | if (max_per_host) { | ||
455 | bb_error_msg("concurrency %s %u/%u", | ||
456 | remote_addr, | ||
457 | cur_per_host, max_per_host); | ||
458 | } | ||
459 | bb_error_msg((option_mask32 & OPT_h) | ||
460 | ? "start %u %s-%s (%s-%s)" | ||
461 | : "start %u %s-%s", | ||
462 | pid, | ||
463 | local_addr, remote_addr, | ||
464 | local_hostname, remote_hostname); | ||
465 | } | ||
466 | |||
467 | if (!(option_mask32 & OPT_E)) { | ||
468 | /* setup ucspi env */ | ||
469 | const char *proto = tcp ? "TCP" : "UDP"; | ||
470 | |||
471 | /* 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 | } | ||
480 | xsetenv_plain("PROTO", proto); | ||
481 | xsetenv_proto(proto, "LOCALADDR", local_addr); | ||
482 | xsetenv_proto(proto, "REMOTEADDR", remote_addr); | ||
483 | if (option_mask32 & OPT_h) { | ||
484 | xsetenv_proto(proto, "LOCALHOST", local_hostname); | ||
485 | xsetenv_proto(proto, "REMOTEHOST", remote_hostname); | ||
486 | } | ||
487 | //compat? xsetenv_proto(proto, "REMOTEINFO", ""); | ||
488 | /* additional */ | ||
489 | if (cur_per_host > 0) /* can not be true for udp */ | ||
490 | xsetenv_plain("TCPCONCURRENCY", utoa(cur_per_host)); | ||
491 | } | ||
492 | free(local_addr); | ||
493 | free(free_me0); | ||
494 | free(free_me1); | ||
495 | free(free_me2); | ||
496 | } | ||
497 | |||
498 | xdup2(0, 1); | ||
499 | |||
500 | signal(SIGTERM, SIG_DFL); | ||
501 | signal(SIGPIPE, SIG_DFL); | ||
502 | signal(SIGCHLD, SIG_DFL); | ||
503 | sig_unblock(SIGCHLD); | ||
504 | |||
505 | #ifdef SSLSVD | ||
506 | strcpy(id, utoa(pid)); | ||
507 | ssl_io(0, argv); | ||
508 | #else | ||
509 | BB_EXECVP(argv[0], argv); | ||
510 | #endif | ||
511 | bb_perror_msg_and_die("exec '%s'", argv[0]); | ||
512 | } | ||
513 | |||
514 | /* | ||
515 | tcpsvd [-hpEvv] [-c n] [-C n:msg] [-b n] [-u user] [-l name] | ||
516 | [-i dir|-x cdb] [ -t sec] host port prog | ||
517 | |||
518 | tcpsvd creates a TCP/IP socket, binds it to the address host:port, | ||
519 | and listens on the socket for incoming connections. | ||
520 | |||
521 | On each incoming connection, tcpsvd conditionally runs a program, | ||
522 | with standard input reading from the socket, and standard output | ||
523 | writing to the socket, to handle this connection. tcpsvd keeps | ||
524 | listening on the socket for new connections, and can handle | ||
525 | multiple connections simultaneously. | ||
526 | |||
527 | tcpsvd optionally checks for special instructions depending | ||
528 | on the IP address or hostname of the client that initiated | ||
529 | the connection, see ipsvd-instruct(5). | ||
530 | |||
531 | host | ||
532 | host either is a hostname, or a dotted-decimal IP address, | ||
533 | or 0. If host is 0, tcpsvd accepts connections to any local | ||
534 | IP address. | ||
535 | * busybox accepts IPv6 addresses and host:port pairs too | ||
536 | In this case second parameter is ignored | ||
537 | port | ||
538 | tcpsvd accepts connections to host:port. port may be a name | ||
539 | from /etc/services or a number. | ||
540 | prog | ||
541 | prog consists of one or more arguments. For each connection, | ||
542 | tcpsvd normally runs prog, with file descriptor 0 reading from | ||
543 | the network, and file descriptor 1 writing to the network. | ||
544 | By default it also sets up TCP-related environment variables, | ||
545 | see tcp-environ(5) | ||
546 | -i dir | ||
547 | read instructions for handling new connections from the instructions | ||
548 | directory dir. See ipsvd-instruct(5) for details. | ||
549 | * ignored by busyboxed version | ||
550 | -x cdb | ||
551 | read instructions for handling new connections from the constant database | ||
552 | cdb. The constant database normally is created from an instructions | ||
553 | directory by running ipsvd-cdb(8). | ||
554 | * ignored by busyboxed version | ||
555 | -t sec | ||
556 | timeout. This option only takes effect if the -i option is given. | ||
557 | While checking the instructions directory, check the time of last access | ||
558 | of the file that matches the clients address or hostname if any, discard | ||
559 | and remove the file if it wasn't accessed within the last sec seconds; | ||
560 | tcpsvd does not discard or remove a file if the user's write permission | ||
561 | is not set, for those files the timeout is disabled. Default is 0, | ||
562 | which means that the timeout is disabled. | ||
563 | * ignored by busyboxed version | ||
564 | -l name | ||
565 | local hostname. Do not look up the local hostname in DNS, but use name | ||
566 | as hostname. This option must be set if tcpsvd listens on port 53 | ||
567 | to avoid loops. | ||
568 | -u user[:group] | ||
569 | drop permissions. Switch user ID to user's UID, and group ID to user's | ||
570 | primary GID after creating and binding to the socket. If user is followed | ||
571 | by a colon and a group name, the group ID is switched to the GID of group | ||
572 | instead. All supplementary groups are removed. | ||
573 | -c n | ||
574 | concurrency. Handle up to n connections simultaneously. Default is 30. | ||
575 | If there are n connections active, tcpsvd defers acceptance of a new | ||
576 | connection until an active connection is closed. | ||
577 | -C n[:msg] | ||
578 | per host concurrency. Allow only up to n connections from the same IP | ||
579 | address simultaneously. If there are n active connections from one IP | ||
580 | address, new incoming connections from this IP address are closed | ||
581 | immediately. If n is followed by :msg, the message msg is written | ||
582 | to the client if possible, before closing the connection. By default | ||
583 | msg is empty. See ipsvd-instruct(5) for supported escape sequences in msg. | ||
584 | |||
585 | For each accepted connection, the current per host concurrency is | ||
586 | available through the environment variable TCPCONCURRENCY. n and msg | ||
587 | can be overwritten by ipsvd(7) instructions, see ipsvd-instruct(5). | ||
588 | By default tcpsvd doesn't keep track of connections. | ||
589 | -h | ||
590 | Look up the client's hostname in DNS. | ||
591 | -p | ||
592 | paranoid. After looking up the client's hostname in DNS, look up the IP | ||
593 | addresses in DNS for that hostname, and forget about the hostname | ||
594 | if none of the addresses match the client's IP address. You should | ||
595 | set this option if you use hostname based instructions. The -p option | ||
596 | implies the -h option. | ||
597 | * ignored by busyboxed version | ||
598 | -b n | ||
599 | backlog. Allow a backlog of approximately n TCP SYNs. On some systems n | ||
600 | is silently limited. Default is 20. | ||
601 | -E | ||
602 | no special environment. Do not set up TCP-related environment variables. | ||
603 | -v | ||
604 | verbose. Print verbose messsages to standard output. | ||
605 | -vv | ||
606 | more verbose. Print more verbose messages to standard output. | ||
607 | * no difference between -v and -vv in busyboxed version | ||
608 | */ | ||