2007-04-01 15:09:03 +05:30
|
|
|
/* Based on ipsvd utilities written by Gerrit Pape <pape@smarden.org>
|
|
|
|
* which are released into public domain by the author.
|
|
|
|
* Homepage: http://smarden.sunsite.dk/ipsvd/
|
|
|
|
*
|
2008-03-02 18:23:15 +05:30
|
|
|
* Copyright (C) 2007 Denys Vlasenko.
|
2007-04-01 15:09:03 +05:30
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
2008-03-17 14:47:27 +05:30
|
|
|
/* Based on ipsvd-0.12.1. This tcpsvd accepts all options
|
2007-04-04 04:53:10 +05:30
|
|
|
* which are supported by one from ipsvd-0.12.1, but not all are
|
|
|
|
* functional. See help text at the end of this file for details.
|
|
|
|
*
|
|
|
|
* Code inside "#ifdef SSLSVD" is for sslsvd and is currently unused.
|
|
|
|
*
|
2008-03-17 14:05:44 +05:30
|
|
|
* Busybox version exports TCPLOCALADDR instead of
|
|
|
|
* TCPLOCALIP + TCPLOCALPORT pair. ADDR more closely matches reality
|
|
|
|
* (which is "struct sockaddr_XXX". Port is not a separate entity,
|
|
|
|
* it's just a part of (AF_INET[6]) sockaddr!).
|
2007-04-04 04:53:10 +05:30
|
|
|
*
|
2008-03-17 14:05:44 +05:30
|
|
|
* TCPORIGDSTADDR is Busybox-specific addition.
|
2007-04-04 04:53:10 +05:30
|
|
|
*
|
|
|
|
* udp server is hacked up by reusing TCP code. It has the following
|
|
|
|
* limitation inherent in Unix DGRAM sockets implementation:
|
2007-04-04 15:46:15 +05:30
|
|
|
* - local IP address is retrieved (using recvmsg voodoo) but
|
2007-04-04 04:53:10 +05:30
|
|
|
* child's socket is not bound to it (bind cannot be called on
|
2007-04-04 15:46:15 +05:30
|
|
|
* already bound socket). Thus it still can emit outgoing packets
|
2007-04-13 22:02:26 +05:30
|
|
|
* with wrong source IP...
|
2007-04-04 04:53:10 +05:30
|
|
|
* - don't know how to retrieve ORIGDST for udp.
|
|
|
|
*/
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2008-04-01 02:00:38 +05:30
|
|
|
/* Wants <limits.h> etc, thus included after libbb.h: */
|
2008-06-07 17:53:44 +05:30
|
|
|
#include <linux/types.h> /* for __be32 etc */
|
2008-04-01 02:00:38 +05:30
|
|
|
#include <linux/netfilter_ipv4.h>
|
|
|
|
|
2008-03-17 14:47:27 +05:30
|
|
|
// TODO: move into this file:
|
|
|
|
#include "tcpudp_perhost.h"
|
2007-04-04 04:53:10 +05:30
|
|
|
|
|
|
|
#ifdef SSLSVD
|
|
|
|
#include "matrixSsl.h"
|
|
|
|
#include "ssl_io.h"
|
|
|
|
#endif
|
|
|
|
|
2007-09-28 15:59:17 +05:30
|
|
|
struct globals {
|
|
|
|
unsigned verbose;
|
|
|
|
unsigned max_per_host;
|
|
|
|
unsigned cur_per_host;
|
|
|
|
unsigned cnum;
|
2007-10-14 10:25:59 +05:30
|
|
|
unsigned cmax;
|
2008-03-17 14:05:44 +05:30
|
|
|
char **env_cur;
|
|
|
|
char *env_var[1]; /* actually bigger */
|
2007-09-28 15:59:17 +05:30
|
|
|
};
|
|
|
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
|
|
|
#define verbose (G.verbose )
|
|
|
|
#define max_per_host (G.max_per_host)
|
|
|
|
#define cur_per_host (G.cur_per_host)
|
|
|
|
#define cnum (G.cnum )
|
|
|
|
#define cmax (G.cmax )
|
2008-03-17 14:05:44 +05:30
|
|
|
#define env_cur (G.env_cur )
|
|
|
|
#define env_var (G.env_var )
|
2008-06-25 15:23:17 +05:30
|
|
|
#define INIT_G() do { \
|
|
|
|
cmax = 30; \
|
|
|
|
env_cur = &env_var[0]; \
|
|
|
|
} while (0)
|
2007-09-28 15:59:17 +05:30
|
|
|
|
2007-04-04 04:53:10 +05:30
|
|
|
|
2008-03-17 14:05:44 +05:30
|
|
|
/* We have to be careful about leaking memory in repeated setenv's */
|
|
|
|
static void xsetenv_plain(const char *n, const char *v)
|
|
|
|
{
|
|
|
|
char *var = xasprintf("%s=%s", n, v);
|
|
|
|
*env_cur++ = var;
|
|
|
|
putenv(var);
|
|
|
|
}
|
|
|
|
|
2007-04-04 04:53:10 +05:30
|
|
|
static void xsetenv_proto(const char *proto, const char *n, const char *v)
|
|
|
|
{
|
2008-03-17 14:05:44 +05:30
|
|
|
char *var = xasprintf("%s%s=%s", proto, n, v);
|
|
|
|
*env_cur++ = var;
|
|
|
|
putenv(var);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void undo_xsetenv(void)
|
|
|
|
{
|
|
|
|
char **pp = env_cur = &env_var[0];
|
|
|
|
while (*pp) {
|
|
|
|
char *var = *pp;
|
|
|
|
*strchrnul(var, '=') = '\0';
|
|
|
|
unsetenv(var);
|
|
|
|
free(var);
|
|
|
|
*pp++ = NULL;
|
|
|
|
}
|
2007-04-04 04:53:10 +05:30
|
|
|
}
|
2007-04-03 17:39:46 +05:30
|
|
|
|
|
|
|
static void sig_term_handler(int sig)
|
|
|
|
{
|
|
|
|
if (verbose)
|
2008-03-17 14:05:44 +05:30
|
|
|
bb_error_msg("got signal %u, exit", sig);
|
2008-02-24 19:06:01 +05:30
|
|
|
kill_myself_with_sig(sig);
|
2007-04-03 17:39:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Little bloated, but tries to give accurate info how child exited.
|
|
|
|
* Makes easier to spot segfaulting children etc... */
|
|
|
|
static void print_waitstat(unsigned pid, int wstat)
|
|
|
|
{
|
|
|
|
unsigned e = 0;
|
|
|
|
const char *cause = "?exit";
|
|
|
|
|
|
|
|
if (WIFEXITED(wstat)) {
|
|
|
|
cause++;
|
|
|
|
e = WEXITSTATUS(wstat);
|
|
|
|
} else if (WIFSIGNALED(wstat)) {
|
|
|
|
cause = "signal";
|
|
|
|
e = WTERMSIG(wstat);
|
|
|
|
}
|
2008-03-17 14:05:44 +05:30
|
|
|
bb_error_msg("end %d %s %d", pid, cause, e);
|
2007-04-03 17:39:46 +05:30
|
|
|
}
|
|
|
|
|
2007-04-01 06:48:20 +05:30
|
|
|
/* Must match getopt32 in main! */
|
|
|
|
enum {
|
|
|
|
OPT_c = (1 << 0),
|
|
|
|
OPT_C = (1 << 1),
|
|
|
|
OPT_i = (1 << 2),
|
|
|
|
OPT_x = (1 << 3),
|
|
|
|
OPT_u = (1 << 4),
|
|
|
|
OPT_l = (1 << 5),
|
|
|
|
OPT_E = (1 << 6),
|
|
|
|
OPT_b = (1 << 7),
|
|
|
|
OPT_h = (1 << 8),
|
|
|
|
OPT_p = (1 << 9),
|
|
|
|
OPT_t = (1 << 10),
|
|
|
|
OPT_v = (1 << 11),
|
|
|
|
OPT_V = (1 << 12),
|
2007-04-01 16:29:33 +05:30
|
|
|
OPT_U = (1 << 13), /* from here: sslsvd only */
|
2007-04-01 06:48:20 +05:30
|
|
|
OPT_slash = (1 << 14),
|
|
|
|
OPT_Z = (1 << 15),
|
|
|
|
OPT_K = (1 << 16),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void connection_status(void)
|
|
|
|
{
|
2007-08-24 15:57:41 +05:30
|
|
|
/* "only 1 client max" desn't need this */
|
2007-04-04 04:53:10 +05:30
|
|
|
if (cmax > 1)
|
2008-03-17 14:05:44 +05:30
|
|
|
bb_error_msg("status %u/%u", cnum, cmax);
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void sig_child_handler(int sig UNUSED_PARAM)
|
2007-04-01 06:48:20 +05:30
|
|
|
{
|
|
|
|
int wstat;
|
|
|
|
int pid;
|
|
|
|
|
2008-01-03 01:25:04 +05:30
|
|
|
while ((pid = wait_any_nohang(&wstat)) > 0) {
|
2007-04-01 06:48:20 +05:30
|
|
|
if (max_per_host)
|
|
|
|
ipsvd_perhost_remove(pid);
|
|
|
|
if (cnum)
|
|
|
|
cnum--;
|
2007-04-03 17:39:46 +05:30
|
|
|
if (verbose)
|
|
|
|
print_waitstat(pid, wstat);
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
|
|
|
if (verbose)
|
|
|
|
connection_status();
|
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int tcpudpsvd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int tcpudpsvd_main(int argc UNUSED_PARAM, char **argv)
|
2007-04-01 06:48:20 +05:30
|
|
|
{
|
2008-03-17 14:39:09 +05:30
|
|
|
char *str_C, *str_t;
|
2007-04-01 06:48:20 +05:30
|
|
|
char *user;
|
|
|
|
struct hcc *hccp;
|
|
|
|
const char *instructs;
|
|
|
|
char *msg_per_host = NULL;
|
|
|
|
unsigned len_per_host = len_per_host; /* gcc */
|
2007-04-04 15:46:15 +05:30
|
|
|
#ifndef SSLSVD
|
|
|
|
struct bb_uidgid_t ugid;
|
|
|
|
#endif
|
2008-03-17 14:05:44 +05:30
|
|
|
bool tcp;
|
2007-04-04 15:46:15 +05:30
|
|
|
uint16_t local_port;
|
2008-03-17 14:05:44 +05:30
|
|
|
char *preset_local_hostname = NULL;
|
|
|
|
char *remote_hostname = remote_hostname; /* for compiler */
|
|
|
|
char *remote_addr = remote_addr; /* for compiler */
|
2007-04-04 15:46:15 +05:30
|
|
|
len_and_sockaddr *lsa;
|
|
|
|
len_and_sockaddr local, remote;
|
|
|
|
socklen_t sa_len;
|
2007-04-01 06:48:20 +05:30
|
|
|
int pid;
|
|
|
|
int sock;
|
|
|
|
int conn;
|
|
|
|
unsigned backlog = 20;
|
2007-04-04 15:46:15 +05:30
|
|
|
|
2007-09-28 15:59:17 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2007-04-04 04:53:10 +05:30
|
|
|
tcp = (applet_name[0] == 't');
|
2007-04-01 06:48:20 +05:30
|
|
|
|
2008-03-17 14:39:09 +05:30
|
|
|
/* 3+ args, -i at most once, -p implies -h, -v is counter, -b N, -c N */
|
|
|
|
opt_complementary = "-3:i--i:ph:vv:b+:c+";
|
2007-04-01 06:48:20 +05:30
|
|
|
#ifdef SSLSVD
|
2007-08-18 21:02:12 +05:30
|
|
|
getopt32(argv, "+c:C:i:x:u:l:Eb:hpt:vU:/:Z:K:",
|
2008-03-17 14:39:09 +05:30
|
|
|
&cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname,
|
|
|
|
&backlog, &str_t, &ssluser, &root, &cert, &key, &verbose
|
2007-04-01 06:48:20 +05:30
|
|
|
);
|
|
|
|
#else
|
2008-03-17 14:39:09 +05:30
|
|
|
/* "+": stop on first non-option */
|
2007-08-18 21:02:12 +05:30
|
|
|
getopt32(argv, "+c:C:i:x:u:l:Eb:hpt:v",
|
2008-03-17 14:39:09 +05:30
|
|
|
&cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname,
|
|
|
|
&backlog, &str_t, &verbose
|
2007-04-01 06:48:20 +05:30
|
|
|
);
|
|
|
|
#endif
|
|
|
|
if (option_mask32 & OPT_C) { /* -C n[:message] */
|
|
|
|
max_per_host = bb_strtou(str_C, &str_C, 10);
|
|
|
|
if (str_C[0]) {
|
|
|
|
if (str_C[0] != ':')
|
|
|
|
bb_show_usage();
|
|
|
|
msg_per_host = str_C + 1;
|
|
|
|
len_per_host = strlen(msg_per_host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (max_per_host > cmax)
|
|
|
|
max_per_host = cmax;
|
|
|
|
if (option_mask32 & OPT_u) {
|
|
|
|
if (!get_uidgid(&ugid, user, 1))
|
|
|
|
bb_error_msg_and_die("unknown user/group: %s", user);
|
|
|
|
}
|
|
|
|
#ifdef SSLSVD
|
2007-04-04 04:53:10 +05:30
|
|
|
if (option_mask32 & OPT_U) ssluser = optarg;
|
|
|
|
if (option_mask32 & OPT_slash) root = optarg;
|
|
|
|
if (option_mask32 & OPT_Z) cert = optarg;
|
|
|
|
if (option_mask32 & OPT_K) key = optarg;
|
2007-04-01 06:48:20 +05:30
|
|
|
#endif
|
|
|
|
argv += optind;
|
|
|
|
if (!argv[0][0] || LONE_CHAR(argv[0], '0'))
|
|
|
|
argv[0] = (char*)"0.0.0.0";
|
|
|
|
|
2007-04-04 04:53:10 +05:30
|
|
|
/* Per-IP flood protection is not thought-out for UDP */
|
|
|
|
if (!tcp)
|
|
|
|
max_per_host = 0;
|
|
|
|
|
|
|
|
bb_sanitize_stdio(); /* fd# 0,1,2 must be opened */
|
2007-04-03 06:43:04 +05:30
|
|
|
|
2007-04-01 06:48:20 +05:30
|
|
|
#ifdef SSLSVD
|
|
|
|
sslser = user;
|
|
|
|
client = 0;
|
|
|
|
if ((getuid() == 0) && !(option_mask32 & OPT_u)) {
|
|
|
|
xfunc_exitcode = 100;
|
2008-03-17 14:05:44 +05:30
|
|
|
bb_error_msg_and_die("-U ssluser must be set when running as root");
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
|
|
|
if (option_mask32 & OPT_u)
|
|
|
|
if (!uidgid_get(&sslugid, ssluser, 1)) {
|
|
|
|
if (errno) {
|
|
|
|
bb_perror_msg_and_die("fatal: cannot get user/group: %s", ssluser);
|
|
|
|
}
|
2008-03-17 14:05:44 +05:30
|
|
|
bb_error_msg_and_die("unknown user/group '%s'", ssluser);
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
|
|
|
if (!cert) cert = "./cert.pem";
|
|
|
|
if (!key) key = cert;
|
|
|
|
if (matrixSslOpen() < 0)
|
|
|
|
fatal("cannot initialize ssl");
|
|
|
|
if (matrixSslReadKeys(&keys, cert, key, 0, ca) < 0) {
|
|
|
|
if (client)
|
|
|
|
fatal("cannot read cert, key, or ca file");
|
|
|
|
fatal("cannot read cert or key file");
|
|
|
|
}
|
|
|
|
if (matrixSslNewSession(&ssl, keys, 0, SSL_FLAGS_SERVER) < 0)
|
|
|
|
fatal("cannot create ssl session");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sig_block(SIGCHLD);
|
|
|
|
signal(SIGCHLD, sig_child_handler);
|
2008-03-20 01:08:46 +05:30
|
|
|
bb_signals(BB_FATAL_SIGS, sig_term_handler);
|
2007-04-01 06:48:20 +05:30
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
|
|
|
if (max_per_host)
|
|
|
|
ipsvd_perhost_init(cmax);
|
|
|
|
|
2007-04-04 04:53:10 +05:30
|
|
|
local_port = bb_lookup_port(argv[1], tcp ? "tcp" : "udp", 0);
|
2007-04-03 17:39:46 +05:30
|
|
|
lsa = xhost2sockaddr(argv[0], local_port);
|
2008-03-17 14:05:44 +05:30
|
|
|
argv += 2;
|
|
|
|
|
2008-01-29 16:03:34 +05:30
|
|
|
sock = xsocket(lsa->u.sa.sa_family, tcp ? SOCK_STREAM : SOCK_DGRAM, 0);
|
2007-04-04 04:53:10 +05:30
|
|
|
setsockopt_reuseaddr(sock);
|
2007-04-04 15:46:15 +05:30
|
|
|
sa_len = lsa->len; /* I presume sockaddr len stays the same */
|
2008-01-29 16:03:34 +05:30
|
|
|
xbind(sock, &lsa->u.sa, sa_len);
|
2007-04-04 04:53:10 +05:30
|
|
|
if (tcp)
|
|
|
|
xlisten(sock, backlog);
|
|
|
|
else /* udp: needed for recv_from_to to work: */
|
|
|
|
socket_want_pktinfo(sock);
|
2007-04-01 06:48:20 +05:30
|
|
|
/* ndelay_off(sock); - it is the default I think? */
|
|
|
|
|
|
|
|
#ifndef SSLSVD
|
|
|
|
if (option_mask32 & OPT_u) {
|
|
|
|
/* drop permissions */
|
|
|
|
xsetgid(ugid.gid);
|
|
|
|
xsetuid(ugid.uid);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (verbose) {
|
2008-01-29 16:03:34 +05:30
|
|
|
char *addr = xmalloc_sockaddr2dotted(&lsa->u.sa);
|
2008-03-17 14:05:44 +05:30
|
|
|
bb_error_msg("listening on %s, starting", addr);
|
2007-04-01 06:48:20 +05:30
|
|
|
free(addr);
|
|
|
|
#ifndef SSLSVD
|
|
|
|
if (option_mask32 & OPT_u)
|
|
|
|
printf(", uid %u, gid %u",
|
2007-04-02 00:40:36 +05:30
|
|
|
(unsigned)ugid.uid, (unsigned)ugid.gid);
|
2007-04-01 06:48:20 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-04-04 04:53:10 +05:30
|
|
|
/* Main accept() loop */
|
2007-04-01 06:48:20 +05:30
|
|
|
|
|
|
|
again:
|
|
|
|
hccp = NULL;
|
|
|
|
|
|
|
|
while (cnum >= cmax)
|
2008-03-17 13:59:08 +05:30
|
|
|
wait_for_any_sig(); /* expecting SIGCHLD */
|
2007-04-01 06:48:20 +05:30
|
|
|
|
2007-04-03 17:39:46 +05:30
|
|
|
/* Accept a connection to fd #0 */
|
|
|
|
again1:
|
|
|
|
close(0);
|
|
|
|
again2:
|
2007-04-01 06:48:20 +05:30
|
|
|
sig_unblock(SIGCHLD);
|
2008-03-17 14:05:44 +05:30
|
|
|
local.len = remote.len = sa_len;
|
2007-04-04 04:53:10 +05:30
|
|
|
if (tcp) {
|
2008-01-29 16:03:34 +05:30
|
|
|
conn = accept(sock, &remote.u.sa, &remote.len);
|
2007-04-04 15:46:15 +05:30
|
|
|
} else {
|
2007-04-04 23:19:47 +05:30
|
|
|
/* In case recv_from_to won't be able to recover local addr.
|
2007-04-04 15:46:15 +05:30
|
|
|
* Also sets port - recv_from_to is unable to do it. */
|
|
|
|
local = *lsa;
|
2008-03-17 14:40:39 +05:30
|
|
|
conn = recv_from_to(sock, NULL, 0, MSG_PEEK,
|
2008-03-17 14:05:44 +05:30
|
|
|
&remote.u.sa, &local.u.sa, sa_len);
|
2007-04-04 15:46:15 +05:30
|
|
|
}
|
2007-04-01 06:48:20 +05:30
|
|
|
sig_block(SIGCHLD);
|
2007-04-03 17:39:46 +05:30
|
|
|
if (conn < 0) {
|
2007-04-01 06:48:20 +05:30
|
|
|
if (errno != EINTR)
|
2007-04-04 04:53:10 +05:30
|
|
|
bb_perror_msg(tcp ? "accept" : "recv");
|
2007-04-03 17:39:46 +05:30
|
|
|
goto again2;
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
2007-04-04 04:53:10 +05:30
|
|
|
xmove_fd(tcp ? conn : sock, 0);
|
2007-04-01 06:48:20 +05:30
|
|
|
|
|
|
|
if (max_per_host) {
|
2007-04-03 17:39:46 +05:30
|
|
|
/* Drop connection immediately if cur_per_host > max_per_host
|
2007-04-01 06:48:20 +05:30
|
|
|
* (minimizing load under SYN flood) */
|
2008-03-17 14:05:44 +05:30
|
|
|
remote_addr = xmalloc_sockaddr2dotted_noport(&remote.u.sa);
|
|
|
|
cur_per_host = ipsvd_perhost_add(remote_addr, max_per_host, &hccp);
|
2007-04-01 06:48:20 +05:30
|
|
|
if (cur_per_host > max_per_host) {
|
|
|
|
/* ipsvd_perhost_add detected that max is exceeded
|
2007-04-03 17:39:46 +05:30
|
|
|
* (and did not store ip in connection table) */
|
2008-03-17 14:05:44 +05:30
|
|
|
free(remote_addr);
|
2007-04-01 06:48:20 +05:30
|
|
|
if (msg_per_host) {
|
2007-04-03 17:39:46 +05:30
|
|
|
/* don't block or test for errors */
|
2008-03-17 14:05:44 +05:30
|
|
|
send(0, msg_per_host, len_per_host, MSG_DONTWAIT);
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
2007-04-03 17:39:46 +05:30
|
|
|
goto again1;
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
2008-03-17 14:05:44 +05:30
|
|
|
/* NB: remote_addr is not leaked, it is stored in conn table */
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
|
|
|
|
2007-04-04 04:53:10 +05:30
|
|
|
if (!tcp) {
|
|
|
|
/* Voodoo magic: making udp sockets each receive its own
|
2007-04-04 15:46:15 +05:30
|
|
|
* packets is not trivial, and I still not sure
|
|
|
|
* I do it 100% right.
|
|
|
|
* 1) we have to do it before fork()
|
|
|
|
* 2) order is important - is it right now? */
|
2007-04-04 04:53:10 +05:30
|
|
|
|
2008-03-13 04:43:50 +05:30
|
|
|
/* Open new non-connected UDP socket for further clients... */
|
|
|
|
sock = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
|
|
|
|
setsockopt_reuseaddr(sock);
|
|
|
|
/* Make plain write/send work for old socket by supplying default
|
2007-04-04 04:53:10 +05:30
|
|
|
* destination address. This also restricts incoming packets
|
|
|
|
* to ones coming from this remote IP. */
|
2008-01-29 16:03:34 +05:30
|
|
|
xconnect(0, &remote.u.sa, sa_len);
|
2007-04-04 15:46:15 +05:30
|
|
|
/* hole? at this point we have no wildcard udp socket...
|
2007-04-04 16:32:55 +05:30
|
|
|
* can this cause clients to get "port unreachable" icmp?
|
|
|
|
* Yup, time window is very small, but it exists (is it?) */
|
2008-03-13 04:43:50 +05:30
|
|
|
/* ..."open new socket", continued */
|
2008-01-29 16:03:34 +05:30
|
|
|
xbind(sock, &lsa->u.sa, sa_len);
|
2007-04-04 04:53:10 +05:30
|
|
|
socket_want_pktinfo(sock);
|
2007-04-04 16:32:55 +05:30
|
|
|
|
|
|
|
/* Doesn't work:
|
|
|
|
* we cannot replace fd #0 - we will lose pending packet
|
|
|
|
* which is already buffered for us! And we cannot use fd #1
|
|
|
|
* instead - it will "intercept" all following packets, but child
|
2007-08-24 15:57:41 +05:30
|
|
|
* does not expect data coming *from fd #1*! */
|
2007-04-04 16:32:55 +05:30
|
|
|
#if 0
|
2008-01-29 16:03:34 +05:30
|
|
|
/* Make it so that local addr is fixed to localp->u.sa
|
2007-04-04 16:32:55 +05:30
|
|
|
* and we don't accidentally accept packets to other local IPs. */
|
|
|
|
/* NB: we possibly bind to the _very_ same_ address & port as the one
|
|
|
|
* already bound in parent! This seems to work in Linux.
|
|
|
|
* (otherwise we can move socket to fd #0 only if bind succeeds) */
|
|
|
|
close(0);
|
|
|
|
set_nport(localp, htons(local_port));
|
2008-01-29 16:03:34 +05:30
|
|
|
xmove_fd(xsocket(localp->u.sa.sa_family, SOCK_DGRAM, 0), 0);
|
2007-04-04 16:32:55 +05:30
|
|
|
setsockopt_reuseaddr(0); /* crucial */
|
2008-01-29 16:03:34 +05:30
|
|
|
xbind(0, &localp->u.sa, localp->len);
|
2007-04-04 16:32:55 +05:30
|
|
|
#endif
|
2007-04-04 04:53:10 +05:30
|
|
|
}
|
2007-04-01 06:48:20 +05:30
|
|
|
|
2008-03-17 14:05:44 +05:30
|
|
|
pid = vfork();
|
2007-04-01 06:48:20 +05:30
|
|
|
if (pid == -1) {
|
2008-03-17 14:05:44 +05:30
|
|
|
bb_perror_msg("vfork");
|
2007-04-01 06:48:20 +05:30
|
|
|
goto again;
|
|
|
|
}
|
2007-04-04 04:53:10 +05:30
|
|
|
|
2007-04-01 06:48:20 +05:30
|
|
|
if (pid != 0) {
|
2008-03-17 14:05:44 +05:30
|
|
|
/* Parent */
|
2007-04-04 04:53:10 +05:30
|
|
|
cnum++;
|
|
|
|
if (verbose)
|
|
|
|
connection_status();
|
2007-04-01 06:48:20 +05:30
|
|
|
if (hccp)
|
|
|
|
hccp->pid = pid;
|
2008-03-17 14:05:44 +05:30
|
|
|
/* clean up changes done by vforked child */
|
|
|
|
undo_xsetenv();
|
2007-04-01 06:48:20 +05:30
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Child: prepare env, log, and exec prog */
|
|
|
|
|
2007-04-04 04:53:10 +05:30
|
|
|
/* Closing tcp listening socket */
|
|
|
|
if (tcp)
|
|
|
|
close(sock);
|
2007-04-03 17:39:46 +05:30
|
|
|
|
2008-03-17 14:05:44 +05:30
|
|
|
{ /* vfork alert! every xmalloc in this block should be freed! */
|
|
|
|
char *local_hostname = local_hostname; /* for compiler */
|
|
|
|
char *local_addr = NULL;
|
|
|
|
char *free_me0 = NULL;
|
|
|
|
char *free_me1 = NULL;
|
|
|
|
char *free_me2 = NULL;
|
|
|
|
|
|
|
|
if (verbose || !(option_mask32 & OPT_E)) {
|
|
|
|
if (!max_per_host) /* remote_addr is not yet known */
|
|
|
|
free_me0 = remote_addr = xmalloc_sockaddr2dotted(&remote.u.sa);
|
|
|
|
if (option_mask32 & OPT_h) {
|
|
|
|
free_me1 = remote_hostname = xmalloc_sockaddr2host_noport(&remote.u.sa);
|
|
|
|
if (!remote_hostname) {
|
|
|
|
bb_error_msg("cannot look up hostname for %s", remote_addr);
|
|
|
|
remote_hostname = remote_addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Find out local IP peer connected to.
|
|
|
|
* Errors ignored (I'm not paranoid enough to imagine kernel
|
|
|
|
* which doesn't know local IP). */
|
|
|
|
if (tcp)
|
|
|
|
getsockname(0, &local.u.sa, &local.len);
|
|
|
|
/* else: for UDP it is done earlier by parent */
|
|
|
|
local_addr = xmalloc_sockaddr2dotted(&local.u.sa);
|
|
|
|
if (option_mask32 & OPT_h) {
|
|
|
|
local_hostname = preset_local_hostname;
|
|
|
|
if (!local_hostname) {
|
|
|
|
free_me2 = local_hostname = xmalloc_sockaddr2host_noport(&local.u.sa);
|
|
|
|
if (!local_hostname)
|
|
|
|
bb_error_msg_and_die("cannot look up hostname for %s", local_addr);
|
|
|
|
}
|
|
|
|
/* else: local_hostname is not NULL, but is NOT malloced! */
|
2007-04-03 17:39:46 +05:30
|
|
|
}
|
|
|
|
}
|
2008-03-17 14:05:44 +05:30
|
|
|
if (verbose) {
|
|
|
|
pid = getpid();
|
|
|
|
if (max_per_host) {
|
|
|
|
bb_error_msg("concurrency %s %u/%u",
|
|
|
|
remote_addr,
|
|
|
|
cur_per_host, max_per_host);
|
|
|
|
}
|
|
|
|
bb_error_msg((option_mask32 & OPT_h)
|
|
|
|
? "start %u %s-%s (%s-%s)"
|
|
|
|
: "start %u %s-%s",
|
|
|
|
pid,
|
|
|
|
local_addr, remote_addr,
|
|
|
|
local_hostname, remote_hostname);
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
2007-04-01 15:09:03 +05:30
|
|
|
|
2008-03-17 14:05:44 +05:30
|
|
|
if (!(option_mask32 & OPT_E)) {
|
|
|
|
/* setup ucspi env */
|
|
|
|
const char *proto = tcp ? "TCP" : "UDP";
|
|
|
|
|
|
|
|
/* Extract "original" destination addr:port
|
|
|
|
* from Linux firewall. Useful when you redirect
|
|
|
|
* an outbond connection to local handler, and it needs
|
|
|
|
* to know where it originally tried to connect */
|
|
|
|
if (tcp && getsockopt(0, SOL_IP, SO_ORIGINAL_DST, &local.u.sa, &local.len) == 0) {
|
|
|
|
char *addr = xmalloc_sockaddr2dotted(&local.u.sa);
|
|
|
|
xsetenv_plain("TCPORIGDSTADDR", addr);
|
|
|
|
free(addr);
|
|
|
|
}
|
|
|
|
xsetenv_plain("PROTO", proto);
|
|
|
|
xsetenv_proto(proto, "LOCALADDR", local_addr);
|
|
|
|
xsetenv_proto(proto, "REMOTEADDR", remote_addr);
|
|
|
|
if (option_mask32 & OPT_h) {
|
|
|
|
xsetenv_proto(proto, "LOCALHOST", local_hostname);
|
|
|
|
xsetenv_proto(proto, "REMOTEHOST", remote_hostname);
|
|
|
|
}
|
|
|
|
//compat? xsetenv_proto(proto, "REMOTEINFO", "");
|
|
|
|
/* additional */
|
|
|
|
if (cur_per_host > 0) /* can not be true for udp */
|
|
|
|
xsetenv_plain("TCPCONCURRENCY", utoa(cur_per_host));
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
2008-03-17 14:05:44 +05:30
|
|
|
free(local_addr);
|
|
|
|
free(free_me0);
|
|
|
|
free(free_me1);
|
|
|
|
free(free_me2);
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
2007-04-01 15:09:03 +05:30
|
|
|
|
2008-03-17 14:05:44 +05:30
|
|
|
xdup2(0, 1);
|
2007-04-03 06:43:04 +05:30
|
|
|
|
2007-04-01 06:48:20 +05:30
|
|
|
signal(SIGTERM, SIG_DFL);
|
|
|
|
signal(SIGPIPE, SIG_DFL);
|
|
|
|
signal(SIGCHLD, SIG_DFL);
|
|
|
|
sig_unblock(SIGCHLD);
|
|
|
|
|
|
|
|
#ifdef SSLSVD
|
2008-03-13 04:43:50 +05:30
|
|
|
strcpy(id, utoa(pid));
|
2007-04-01 06:48:20 +05:30
|
|
|
ssl_io(0, argv);
|
|
|
|
#else
|
|
|
|
BB_EXECVP(argv[0], argv);
|
|
|
|
#endif
|
|
|
|
bb_perror_msg_and_die("exec '%s'", argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-04-01 16:29:33 +05:30
|
|
|
tcpsvd [-hpEvv] [-c n] [-C n:msg] [-b n] [-u user] [-l name]
|
|
|
|
[-i dir|-x cdb] [ -t sec] host port prog
|
2007-04-01 06:48:20 +05:30
|
|
|
|
|
|
|
tcpsvd creates a TCP/IP socket, binds it to the address host:port,
|
|
|
|
and listens on the socket for incoming connections.
|
|
|
|
|
|
|
|
On each incoming connection, tcpsvd conditionally runs a program,
|
|
|
|
with standard input reading from the socket, and standard output
|
|
|
|
writing to the socket, to handle this connection. tcpsvd keeps
|
|
|
|
listening on the socket for new connections, and can handle
|
|
|
|
multiple connections simultaneously.
|
|
|
|
|
|
|
|
tcpsvd optionally checks for special instructions depending
|
|
|
|
on the IP address or hostname of the client that initiated
|
|
|
|
the connection, see ipsvd-instruct(5).
|
|
|
|
|
|
|
|
host
|
|
|
|
host either is a hostname, or a dotted-decimal IP address,
|
|
|
|
or 0. If host is 0, tcpsvd accepts connections to any local
|
|
|
|
IP address.
|
2007-04-01 15:09:03 +05:30
|
|
|
* busybox accepts IPv6 addresses and host:port pairs too
|
|
|
|
In this case second parameter is ignored
|
2007-04-01 06:48:20 +05:30
|
|
|
port
|
|
|
|
tcpsvd accepts connections to host:port. port may be a name
|
|
|
|
from /etc/services or a number.
|
|
|
|
prog
|
|
|
|
prog consists of one or more arguments. For each connection,
|
|
|
|
tcpsvd normally runs prog, with file descriptor 0 reading from
|
|
|
|
the network, and file descriptor 1 writing to the network.
|
|
|
|
By default it also sets up TCP-related environment variables,
|
2007-04-01 15:09:03 +05:30
|
|
|
see tcp-environ(5)
|
2007-04-01 06:48:20 +05:30
|
|
|
-i dir
|
|
|
|
read instructions for handling new connections from the instructions
|
2007-04-01 15:09:03 +05:30
|
|
|
directory dir. See ipsvd-instruct(5) for details.
|
|
|
|
* ignored by busyboxed version
|
2007-04-01 06:48:20 +05:30
|
|
|
-x cdb
|
|
|
|
read instructions for handling new connections from the constant database
|
|
|
|
cdb. The constant database normally is created from an instructions
|
2007-04-01 15:09:03 +05:30
|
|
|
directory by running ipsvd-cdb(8).
|
|
|
|
* ignored by busyboxed version
|
2007-04-01 06:48:20 +05:30
|
|
|
-t sec
|
|
|
|
timeout. This option only takes effect if the -i option is given.
|
|
|
|
While checking the instructions directory, check the time of last access
|
|
|
|
of the file that matches the clients address or hostname if any, discard
|
|
|
|
and remove the file if it wasn't accessed within the last sec seconds;
|
|
|
|
tcpsvd does not discard or remove a file if the user's write permission
|
|
|
|
is not set, for those files the timeout is disabled. Default is 0,
|
2007-04-01 15:09:03 +05:30
|
|
|
which means that the timeout is disabled.
|
|
|
|
* ignored by busyboxed version
|
2007-04-01 06:48:20 +05:30
|
|
|
-l name
|
|
|
|
local hostname. Do not look up the local hostname in DNS, but use name
|
|
|
|
as hostname. This option must be set if tcpsvd listens on port 53
|
2007-04-01 15:09:03 +05:30
|
|
|
to avoid loops.
|
2007-04-01 06:48:20 +05:30
|
|
|
-u user[:group]
|
|
|
|
drop permissions. Switch user ID to user's UID, and group ID to user's
|
|
|
|
primary GID after creating and binding to the socket. If user is followed
|
|
|
|
by a colon and a group name, the group ID is switched to the GID of group
|
2007-04-01 15:09:03 +05:30
|
|
|
instead. All supplementary groups are removed.
|
2007-04-01 06:48:20 +05:30
|
|
|
-c n
|
|
|
|
concurrency. Handle up to n connections simultaneously. Default is 30.
|
|
|
|
If there are n connections active, tcpsvd defers acceptance of a new
|
2007-04-01 15:09:03 +05:30
|
|
|
connection until an active connection is closed.
|
2007-04-01 06:48:20 +05:30
|
|
|
-C n[:msg]
|
|
|
|
per host concurrency. Allow only up to n connections from the same IP
|
2007-04-01 15:09:03 +05:30
|
|
|
address simultaneously. If there are n active connections from one IP
|
2007-04-01 06:48:20 +05:30
|
|
|
address, new incoming connections from this IP address are closed
|
2007-04-01 15:09:03 +05:30
|
|
|
immediately. If n is followed by :msg, the message msg is written
|
2007-04-01 06:48:20 +05:30
|
|
|
to the client if possible, before closing the connection. By default
|
|
|
|
msg is empty. See ipsvd-instruct(5) for supported escape sequences in msg.
|
|
|
|
|
|
|
|
For each accepted connection, the current per host concurrency is
|
|
|
|
available through the environment variable TCPCONCURRENCY. n and msg
|
|
|
|
can be overwritten by ipsvd(7) instructions, see ipsvd-instruct(5).
|
2007-04-01 15:09:03 +05:30
|
|
|
By default tcpsvd doesn't keep track of connections.
|
2007-04-01 06:48:20 +05:30
|
|
|
-h
|
2007-04-01 15:09:03 +05:30
|
|
|
Look up the client's hostname in DNS.
|
2007-04-01 06:48:20 +05:30
|
|
|
-p
|
|
|
|
paranoid. After looking up the client's hostname in DNS, look up the IP
|
|
|
|
addresses in DNS for that hostname, and forget about the hostname
|
|
|
|
if none of the addresses match the client's IP address. You should
|
|
|
|
set this option if you use hostname based instructions. The -p option
|
2007-04-01 15:09:03 +05:30
|
|
|
implies the -h option.
|
|
|
|
* ignored by busyboxed version
|
2007-04-01 06:48:20 +05:30
|
|
|
-b n
|
|
|
|
backlog. Allow a backlog of approximately n TCP SYNs. On some systems n
|
2007-04-01 15:09:03 +05:30
|
|
|
is silently limited. Default is 20.
|
2007-04-01 06:48:20 +05:30
|
|
|
-E
|
2007-04-01 15:09:03 +05:30
|
|
|
no special environment. Do not set up TCP-related environment variables.
|
2007-04-01 06:48:20 +05:30
|
|
|
-v
|
2007-04-01 15:09:03 +05:30
|
|
|
verbose. Print verbose messsages to standard output.
|
2007-04-01 06:48:20 +05:30
|
|
|
-vv
|
2007-04-01 15:09:03 +05:30
|
|
|
more verbose. Print more verbose messages to standard output.
|
|
|
|
* no difference between -v and -vv in busyboxed version
|
2007-04-01 06:48:20 +05:30
|
|
|
*/
|