Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 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 | * |
Denis Vlasenko | d18f52b | 2008-03-02 12:53:15 +0000 | [diff] [blame] | 5 | * Copyright (C) 2007 Denys Vlasenko. |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | 4866e90 | 2008-03-17 09:17:27 +0000 | [diff] [blame] | 10 | /* Based on ipsvd-0.12.1. This tcpsvd accepts all options |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 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 | * |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 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!). |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 20 | * |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 21 | * TCPORIGDSTADDR is Busybox-specific addition. |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 22 | * |
| 23 | * udp server is hacked up by reusing TCP code. It has the following |
| 24 | * limitation inherent in Unix DGRAM sockets implementation: |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 25 | * - local IP address is retrieved (using recvmsg voodoo) but |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 26 | * child's socket is not bound to it (bind cannot be called on |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 27 | * already bound socket). Thus it still can emit outgoing packets |
Denis Vlasenko | beaca81 | 2007-04-13 16:32:26 +0000 | [diff] [blame] | 28 | * with wrong source IP... |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 29 | * - don't know how to retrieve ORIGDST for udp. |
| 30 | */ |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 31 | //config:config TCPSVD |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 32 | //config: bool "tcpsvd (13 kb)" |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 33 | //config: default y |
| 34 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 35 | //config: tcpsvd listens on a TCP port and runs a program for each new |
| 36 | //config: connection. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 37 | //config: |
| 38 | //config:config UDPSVD |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 39 | //config: bool "udpsvd (13 kb)" |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 40 | //config: default y |
| 41 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 42 | //config: udpsvd listens on an UDP port and runs a program for each new |
| 43 | //config: connection. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 44 | |
| 45 | //applet:IF_TCPSVD(APPLET_ODDNAME(tcpsvd, tcpudpsvd, BB_DIR_USR_BIN, BB_SUID_DROP, tcpsvd)) |
| 46 | //applet:IF_UDPSVD(APPLET_ODDNAME(udpsvd, tcpudpsvd, BB_DIR_USR_BIN, BB_SUID_DROP, udpsvd)) |
| 47 | |
| 48 | //kbuild:lib-$(CONFIG_TCPSVD) += tcpudp.o tcpudp_perhost.o |
| 49 | //kbuild:lib-$(CONFIG_UDPSVD) += tcpudp.o tcpudp_perhost.o |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 50 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 51 | //usage:#define tcpsvd_trivial_usage |
| 52 | //usage: "[-hEv] [-c N] [-C N[:MSG]] [-b N] [-u USER] [-l NAME] IP PORT PROG" |
| 53 | /* with not-implemented options: */ |
| 54 | /* //usage: "[-hpEvv] [-c N] [-C N[:MSG]] [-b N] [-u USER] [-l NAME] [-i DIR|-x CDB] [-t SEC] IP PORT PROG" */ |
| 55 | //usage:#define tcpsvd_full_usage "\n\n" |
Denys Vlasenko | ec1ea16 | 2016-10-07 15:56:47 +0200 | [diff] [blame] | 56 | //usage: "Create TCP socket, bind to IP:PORT and listen for incoming connections.\n" |
| 57 | //usage: "Run PROG for each connection.\n" |
| 58 | //usage: "\n IP PORT IP:PORT to listen on" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 59 | //usage: "\n PROG ARGS Program to run" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 60 | //usage: "\n -u USER[:GRP] Change to user/group after bind" |
Denys Vlasenko | ec1ea16 | 2016-10-07 15:56:47 +0200 | [diff] [blame] | 61 | //usage: "\n -c N Up to N connections simultaneously (default 30)" |
| 62 | //usage: "\n -b N Allow backlog of approximately N TCP SYNs (default 20)" |
| 63 | //usage: "\n -C N[:MSG] Allow only up to N connections from the same IP:" |
| 64 | //usage: "\n new connections from this IP address are closed" |
| 65 | //usage: "\n immediately, MSG is written to the peer before close" |
| 66 | //usage: "\n -E Don't set up environment" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 67 | //usage: "\n -h Look up peer's hostname" |
Denys Vlasenko | ec1ea16 | 2016-10-07 15:56:47 +0200 | [diff] [blame] | 68 | //usage: "\n -l NAME Local hostname (else look up local hostname in DNS)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 69 | //usage: "\n -v Verbose" |
Denys Vlasenko | ec1ea16 | 2016-10-07 15:56:47 +0200 | [diff] [blame] | 70 | //usage: "\n" |
| 71 | //usage: "\nEnvironment if no -E:" |
| 72 | //usage: "\nPROTO='TCP'" |
| 73 | //usage: "\nTCPREMOTEADDR='ip:port'" IF_FEATURE_IPV6(" ('[ip]:port' for IPv6)") |
| 74 | //usage: "\nTCPLOCALADDR='ip:port'" |
| 75 | //usage: "\nTCPORIGDSTADDR='ip:port' of destination before firewall" |
| 76 | //usage: "\n Useful for REDIRECTed-to-local connections:" |
| 77 | //usage: "\n iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080" |
| 78 | //usage: "\nTCPCONCURRENCY=num_of_connects_from_this_ip" |
| 79 | //usage: "\nIf -h:" |
| 80 | //usage: "\nTCPLOCALHOST='hostname' (-l NAME is used if specified)" |
| 81 | //usage: "\nTCPREMOTEHOST='hostname'" |
| 82 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 83 | //usage: |
| 84 | //usage:#define udpsvd_trivial_usage |
| 85 | //usage: "[-hEv] [-c N] [-u USER] [-l NAME] IP PORT PROG" |
| 86 | //usage:#define udpsvd_full_usage "\n\n" |
Denys Vlasenko | ec1ea16 | 2016-10-07 15:56:47 +0200 | [diff] [blame] | 87 | //usage: "Create UDP socket, bind to IP:PORT and wait for incoming packets.\n" |
| 88 | //usage: "Run PROG for each packet, redirecting all further packets with same\n" |
| 89 | //usage: "peer ip:port to it.\n" |
| 90 | //usage: "\n IP PORT IP:PORT to listen on" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 91 | //usage: "\n PROG ARGS Program to run" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 92 | //usage: "\n -u USER[:GRP] Change to user/group after bind" |
Denys Vlasenko | ec1ea16 | 2016-10-07 15:56:47 +0200 | [diff] [blame] | 93 | //usage: "\n -c N Up to N connections simultaneously (default 30)" |
| 94 | //usage: "\n -E Don't set up environment" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 95 | //usage: "\n -h Look up peer's hostname" |
Denys Vlasenko | ec1ea16 | 2016-10-07 15:56:47 +0200 | [diff] [blame] | 96 | //usage: "\n -l NAME Local hostname (else look up local hostname in DNS)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 97 | //usage: "\n -v Verbose" |
Denys Vlasenko | ec1ea16 | 2016-10-07 15:56:47 +0200 | [diff] [blame] | 98 | //usage: "\n" |
| 99 | //usage: "\nEnvironment if no -E:" |
| 100 | //usage: "\nPROTO='UDP'" |
| 101 | //usage: "\nUDPREMOTEADDR='ip:port'" IF_FEATURE_IPV6(" ('[ip]:port' for IPv6)") |
| 102 | //usage: "\nUDPLOCALADDR='ip:port'" |
| 103 | //usage: "\nIf -h:" |
| 104 | //usage: "\nUDPLOCALHOST='hostname' (-l NAME is used if specified)" |
| 105 | //usage: "\nUDPREMOTEHOST='hostname'" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 106 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 107 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 108 | #include "common_bufsiz.h" |
Jeremie Koenig | 2ea12d8 | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 109 | |
Jeremie Koenig | 2ea12d8 | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 110 | #ifdef __linux__ |
Denys Vlasenko | 7890035 | 2017-01-02 10:46:08 +0100 | [diff] [blame] | 111 | /* from linux/netfilter_ipv4.h: */ |
| 112 | # undef SO_ORIGINAL_DST |
| 113 | # define SO_ORIGINAL_DST 80 |
Jeremie Koenig | 2ea12d8 | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 114 | #endif |
Denis Vlasenko | 165f5b3 | 2008-03-31 20:30:38 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | 4866e90 | 2008-03-17 09:17:27 +0000 | [diff] [blame] | 116 | // TODO: move into this file: |
| 117 | #include "tcpudp_perhost.h" |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 118 | |
| 119 | #ifdef SSLSVD |
| 120 | #include "matrixSsl.h" |
| 121 | #include "ssl_io.h" |
| 122 | #endif |
| 123 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 124 | struct globals { |
| 125 | unsigned verbose; |
| 126 | unsigned max_per_host; |
| 127 | unsigned cur_per_host; |
| 128 | unsigned cnum; |
Denis Vlasenko | 3f5fdc7 | 2007-10-14 04:55:59 +0000 | [diff] [blame] | 129 | unsigned cmax; |
Denys Vlasenko | d82ea2b | 2018-02-27 13:03:44 +0100 | [diff] [blame] | 130 | struct hcc *cc; |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 131 | char **env_cur; |
| 132 | char *env_var[1]; /* actually bigger */ |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 133 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 134 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 135 | #define verbose (G.verbose ) |
| 136 | #define max_per_host (G.max_per_host) |
| 137 | #define cur_per_host (G.cur_per_host) |
| 138 | #define cnum (G.cnum ) |
| 139 | #define cmax (G.cmax ) |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 140 | #define env_cur (G.env_cur ) |
| 141 | #define env_var (G.env_var ) |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 142 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 143 | setup_common_bufsiz(); \ |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 144 | cmax = 30; \ |
| 145 | env_cur = &env_var[0]; \ |
| 146 | } while (0) |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 147 | |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 148 | |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 149 | /* We have to be careful about leaking memory in repeated setenv's */ |
| 150 | static void xsetenv_plain(const char *n, const char *v) |
| 151 | { |
| 152 | char *var = xasprintf("%s=%s", n, v); |
| 153 | *env_cur++ = var; |
| 154 | putenv(var); |
| 155 | } |
| 156 | |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 157 | static void xsetenv_proto(const char *proto, const char *n, const char *v) |
| 158 | { |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 159 | char *var = xasprintf("%s%s=%s", proto, n, v); |
| 160 | *env_cur++ = var; |
| 161 | putenv(var); |
| 162 | } |
| 163 | |
| 164 | static void undo_xsetenv(void) |
| 165 | { |
| 166 | char **pp = env_cur = &env_var[0]; |
| 167 | while (*pp) { |
| 168 | char *var = *pp; |
Denys Vlasenko | dd8adde | 2010-06-24 05:00:50 +0200 | [diff] [blame] | 169 | bb_unsetenv_and_free(var); |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 170 | *pp++ = NULL; |
| 171 | } |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 172 | } |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 173 | |
| 174 | static void sig_term_handler(int sig) |
| 175 | { |
| 176 | if (verbose) |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 177 | bb_error_msg("got signal %u, exit", sig); |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 178 | kill_myself_with_sig(sig); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* Little bloated, but tries to give accurate info how child exited. |
| 182 | * Makes easier to spot segfaulting children etc... */ |
| 183 | static void print_waitstat(unsigned pid, int wstat) |
| 184 | { |
| 185 | unsigned e = 0; |
| 186 | const char *cause = "?exit"; |
| 187 | |
| 188 | if (WIFEXITED(wstat)) { |
| 189 | cause++; |
| 190 | e = WEXITSTATUS(wstat); |
| 191 | } else if (WIFSIGNALED(wstat)) { |
| 192 | cause = "signal"; |
| 193 | e = WTERMSIG(wstat); |
| 194 | } |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 195 | bb_error_msg("end %d %s %d", pid, cause, e); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 198 | /* Must match getopt32 in main! */ |
| 199 | enum { |
| 200 | OPT_c = (1 << 0), |
| 201 | OPT_C = (1 << 1), |
| 202 | OPT_i = (1 << 2), |
| 203 | OPT_x = (1 << 3), |
| 204 | OPT_u = (1 << 4), |
| 205 | OPT_l = (1 << 5), |
| 206 | OPT_E = (1 << 6), |
| 207 | OPT_b = (1 << 7), |
| 208 | OPT_h = (1 << 8), |
| 209 | OPT_p = (1 << 9), |
| 210 | OPT_t = (1 << 10), |
| 211 | OPT_v = (1 << 11), |
| 212 | OPT_V = (1 << 12), |
| 213 | OPT_U = (1 << 13), /* from here: sslsvd only */ |
| 214 | OPT_slash = (1 << 14), |
| 215 | OPT_Z = (1 << 15), |
| 216 | OPT_K = (1 << 16), |
| 217 | }; |
| 218 | |
| 219 | static void connection_status(void) |
| 220 | { |
Denis Vlasenko | f87f495 | 2007-08-24 10:27:41 +0000 | [diff] [blame] | 221 | /* "only 1 client max" desn't need this */ |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 222 | if (cmax > 1) |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 223 | bb_error_msg("status %u/%u", cnum, cmax); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 226 | static void sig_child_handler(int sig UNUSED_PARAM) |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 227 | { |
| 228 | int wstat; |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 229 | pid_t pid; |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 230 | |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 231 | while ((pid = wait_any_nohang(&wstat)) > 0) { |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 232 | if (max_per_host) |
Denys Vlasenko | d82ea2b | 2018-02-27 13:03:44 +0100 | [diff] [blame] | 233 | ipsvd_perhost_remove(G.cc, pid); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 234 | if (cnum) |
| 235 | cnum--; |
| 236 | if (verbose) |
| 237 | print_waitstat(pid, wstat); |
| 238 | } |
| 239 | if (verbose) |
| 240 | connection_status(); |
| 241 | } |
| 242 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 243 | int tcpudpsvd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 244 | int tcpudpsvd_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 245 | { |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 246 | char *str_C, *str_t; |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 247 | char *user; |
| 248 | struct hcc *hccp; |
| 249 | const char *instructs; |
| 250 | char *msg_per_host = NULL; |
| 251 | unsigned len_per_host = len_per_host; /* gcc */ |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 252 | #ifndef SSLSVD |
| 253 | struct bb_uidgid_t ugid; |
| 254 | #endif |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 255 | bool tcp; |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 256 | uint16_t local_port; |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 257 | char *preset_local_hostname = NULL; |
| 258 | char *remote_hostname = remote_hostname; /* for compiler */ |
| 259 | char *remote_addr = remote_addr; /* for compiler */ |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 260 | len_and_sockaddr *lsa; |
| 261 | len_and_sockaddr local, remote; |
| 262 | socklen_t sa_len; |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 263 | int pid; |
| 264 | int sock; |
| 265 | int conn; |
| 266 | unsigned backlog = 20; |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 267 | unsigned opts; |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 268 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 269 | INIT_G(); |
| 270 | |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 271 | tcp = (applet_name[0] == 't'); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 272 | |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 273 | #ifdef SSLSVD |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 274 | opts = getopt32(argv, "^+" |
| 275 | "c:+C:i:x:u:l:Eb:+hpt:vU:/:Z:K:" /* -c NUM, -b NUM */ |
| 276 | /* 3+ args, -i at most once, -p implies -h, -v is a counter */ |
| 277 | "\0" "-3:i--i:ph:vv", |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 278 | &cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname, |
| 279 | &backlog, &str_t, &ssluser, &root, &cert, &key, &verbose |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 280 | ); |
| 281 | #else |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 282 | /* "+": stop on first non-option */ |
Denys Vlasenko | ec1ea16 | 2016-10-07 15:56:47 +0200 | [diff] [blame] | 283 | opts = getopt32(argv, "+c:+C:i:x:u:l:Eb:hpt:v", |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 284 | &cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname, |
| 285 | &backlog, &str_t, &verbose |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 286 | ); |
| 287 | #endif |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 288 | if (opts & OPT_C) { /* -C n[:message] */ |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 289 | max_per_host = bb_strtou(str_C, &str_C, 10); |
| 290 | if (str_C[0]) { |
| 291 | if (str_C[0] != ':') |
| 292 | bb_show_usage(); |
| 293 | msg_per_host = str_C + 1; |
| 294 | len_per_host = strlen(msg_per_host); |
| 295 | } |
| 296 | } |
| 297 | if (max_per_host > cmax) |
| 298 | max_per_host = cmax; |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 299 | if (opts & OPT_u) { |
Bernhard Reutner-Fischer | d73cbd3 | 2008-07-21 14:41:33 +0000 | [diff] [blame] | 300 | xget_uidgid(&ugid, user); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 301 | } |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 302 | #ifdef SSLSVD |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 303 | if (opts & OPT_U) ssluser = optarg; |
| 304 | if (opts & OPT_slash) root = optarg; |
| 305 | if (opts & OPT_Z) cert = optarg; |
| 306 | if (opts & OPT_K) key = optarg; |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 307 | #endif |
| 308 | argv += optind; |
| 309 | if (!argv[0][0] || LONE_CHAR(argv[0], '0')) |
| 310 | argv[0] = (char*)"0.0.0.0"; |
| 311 | |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 312 | /* Per-IP flood protection is not thought-out for UDP */ |
| 313 | if (!tcp) |
| 314 | max_per_host = 0; |
| 315 | |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 316 | bb_sanitize_stdio(); /* fd# 0,1,2 must be opened */ |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 317 | |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 318 | #ifdef SSLSVD |
| 319 | sslser = user; |
| 320 | client = 0; |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 321 | if ((getuid() == 0) && !(opts & OPT_u)) { |
Jan Luebbe | e789c3b | 2018-02-14 14:05:27 +0100 | [diff] [blame] | 322 | xfunc_error_retval = 100; |
Denys Vlasenko | b2e5fc3 | 2009-11-25 14:52:47 +0100 | [diff] [blame] | 323 | bb_error_msg_and_die(bb_msg_you_must_be_root); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 324 | } |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 325 | if (opts & OPT_u) |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 326 | if (!uidgid_get(&sslugid, ssluser, 1)) { |
| 327 | if (errno) { |
Bernhard Reutner-Fischer | d73cbd3 | 2008-07-21 14:41:33 +0000 | [diff] [blame] | 328 | bb_perror_msg_and_die("can't get user/group: %s", ssluser); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 329 | } |
Bernhard Reutner-Fischer | d73cbd3 | 2008-07-21 14:41:33 +0000 | [diff] [blame] | 330 | bb_error_msg_and_die("unknown user/group %s", ssluser); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 331 | } |
| 332 | if (!cert) cert = "./cert.pem"; |
| 333 | if (!key) key = cert; |
| 334 | if (matrixSslOpen() < 0) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 335 | fatal("can't initialize ssl"); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 336 | if (matrixSslReadKeys(&keys, cert, key, 0, ca) < 0) { |
| 337 | if (client) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 338 | fatal("can't read cert, key, or ca file"); |
| 339 | fatal("can't read cert or key file"); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 340 | } |
| 341 | if (matrixSslNewSession(&ssl, keys, 0, SSL_FLAGS_SERVER) < 0) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 342 | fatal("can't create ssl session"); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 343 | #endif |
| 344 | |
| 345 | sig_block(SIGCHLD); |
| 346 | signal(SIGCHLD, sig_child_handler); |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 347 | bb_signals(BB_FATAL_SIGS, sig_term_handler); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 348 | signal(SIGPIPE, SIG_IGN); |
| 349 | |
| 350 | if (max_per_host) |
Denys Vlasenko | d82ea2b | 2018-02-27 13:03:44 +0100 | [diff] [blame] | 351 | G.cc = ipsvd_perhost_init(cmax); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 352 | |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 353 | local_port = bb_lookup_port(argv[1], tcp ? "tcp" : "udp", 0); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 354 | lsa = xhost2sockaddr(argv[0], local_port); |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 355 | argv += 2; |
| 356 | |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 357 | sock = xsocket(lsa->u.sa.sa_family, tcp ? SOCK_STREAM : SOCK_DGRAM, 0); |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 358 | setsockopt_reuseaddr(sock); |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 359 | sa_len = lsa->len; /* I presume sockaddr len stays the same */ |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 360 | xbind(sock, &lsa->u.sa, sa_len); |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 361 | if (tcp) { |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 362 | xlisten(sock, backlog); |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 363 | close_on_exec_on(sock); |
| 364 | } else { /* udp: needed for recv_from_to to work: */ |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 365 | socket_want_pktinfo(sock); |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 366 | } |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 367 | /* ndelay_off(sock); - it is the default I think? */ |
| 368 | |
| 369 | #ifndef SSLSVD |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 370 | if (opts & OPT_u) { |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 371 | /* drop permissions */ |
| 372 | xsetgid(ugid.gid); |
| 373 | xsetuid(ugid.uid); |
| 374 | } |
| 375 | #endif |
| 376 | |
| 377 | if (verbose) { |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 378 | char *addr = xmalloc_sockaddr2dotted(&lsa->u.sa); |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 379 | if (opts & OPT_u) |
| 380 | bb_error_msg("listening on %s, starting, uid %u, gid %u", addr, |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 381 | (unsigned)ugid.uid, (unsigned)ugid.gid); |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 382 | else |
| 383 | bb_error_msg("listening on %s, starting", addr); |
| 384 | free(addr); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 387 | /* Main accept() loop */ |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 388 | |
| 389 | again: |
| 390 | hccp = NULL; |
| 391 | |
Denys Vlasenko | ec1ea16 | 2016-10-07 15:56:47 +0200 | [diff] [blame] | 392 | again1: |
| 393 | close(0); |
| 394 | /* It's important to close(0) _before_ wait loop: |
| 395 | * fd#0 can be a shared connection fd. |
| 396 | * If kept open by us, peer can't detect PROG closing it. |
| 397 | */ |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 398 | while (cnum >= cmax) |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 399 | wait_for_any_sig(); /* expecting SIGCHLD */ |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 400 | |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 401 | again2: |
| 402 | sig_unblock(SIGCHLD); |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 403 | local.len = remote.len = sa_len; |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 404 | if (tcp) { |
Denys Vlasenko | ec1ea16 | 2016-10-07 15:56:47 +0200 | [diff] [blame] | 405 | /* Accept a connection to fd #0 */ |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 406 | conn = accept(sock, &remote.u.sa, &remote.len); |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 407 | } else { |
Denis Vlasenko | 1ca332b | 2007-04-04 17:49:47 +0000 | [diff] [blame] | 408 | /* In case recv_from_to won't be able to recover local addr. |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 409 | * Also sets port - recv_from_to is unable to do it. */ |
| 410 | local = *lsa; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 411 | conn = recv_from_to(sock, NULL, 0, MSG_PEEK, |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 412 | &remote.u.sa, &local.u.sa, sa_len); |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 413 | } |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 414 | sig_block(SIGCHLD); |
| 415 | if (conn < 0) { |
| 416 | if (errno != EINTR) |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 417 | bb_perror_msg(tcp ? "accept" : "recv"); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 418 | goto again2; |
| 419 | } |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 420 | xmove_fd(tcp ? conn : sock, 0); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 421 | |
| 422 | if (max_per_host) { |
| 423 | /* Drop connection immediately if cur_per_host > max_per_host |
| 424 | * (minimizing load under SYN flood) */ |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 425 | remote_addr = xmalloc_sockaddr2dotted_noport(&remote.u.sa); |
Denys Vlasenko | d82ea2b | 2018-02-27 13:03:44 +0100 | [diff] [blame] | 426 | cur_per_host = ipsvd_perhost_add(G.cc, remote_addr, max_per_host, &hccp); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 427 | if (cur_per_host > max_per_host) { |
| 428 | /* ipsvd_perhost_add detected that max is exceeded |
| 429 | * (and did not store ip in connection table) */ |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 430 | free(remote_addr); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 431 | if (msg_per_host) { |
| 432 | /* don't block or test for errors */ |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 433 | send(0, msg_per_host, len_per_host, MSG_DONTWAIT); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 434 | } |
| 435 | goto again1; |
| 436 | } |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 437 | /* NB: remote_addr is not leaked, it is stored in conn table */ |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 440 | if (!tcp) { |
| 441 | /* Voodoo magic: making udp sockets each receive its own |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 442 | * packets is not trivial, and I still not sure |
| 443 | * I do it 100% right. |
| 444 | * 1) we have to do it before fork() |
| 445 | * 2) order is important - is it right now? */ |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 446 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 447 | /* Open new non-connected UDP socket for further clients... */ |
| 448 | sock = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0); |
| 449 | setsockopt_reuseaddr(sock); |
| 450 | /* Make plain write/send work for old socket by supplying default |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 451 | * destination address. This also restricts incoming packets |
| 452 | * to ones coming from this remote IP. */ |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 453 | xconnect(0, &remote.u.sa, sa_len); |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 454 | /* hole? at this point we have no wildcard udp socket... |
Denis Vlasenko | 7946879 | 2007-04-04 11:02:55 +0000 | [diff] [blame] | 455 | * can this cause clients to get "port unreachable" icmp? |
| 456 | * Yup, time window is very small, but it exists (is it?) */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 457 | /* ..."open new socket", continued */ |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 458 | xbind(sock, &lsa->u.sa, sa_len); |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 459 | socket_want_pktinfo(sock); |
Denis Vlasenko | 7946879 | 2007-04-04 11:02:55 +0000 | [diff] [blame] | 460 | |
| 461 | /* Doesn't work: |
| 462 | * we cannot replace fd #0 - we will lose pending packet |
| 463 | * which is already buffered for us! And we cannot use fd #1 |
| 464 | * instead - it will "intercept" all following packets, but child |
Denis Vlasenko | f87f495 | 2007-08-24 10:27:41 +0000 | [diff] [blame] | 465 | * does not expect data coming *from fd #1*! */ |
Denis Vlasenko | 7946879 | 2007-04-04 11:02:55 +0000 | [diff] [blame] | 466 | #if 0 |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 467 | /* Make it so that local addr is fixed to localp->u.sa |
Denis Vlasenko | 7946879 | 2007-04-04 11:02:55 +0000 | [diff] [blame] | 468 | * and we don't accidentally accept packets to other local IPs. */ |
| 469 | /* NB: we possibly bind to the _very_ same_ address & port as the one |
| 470 | * already bound in parent! This seems to work in Linux. |
| 471 | * (otherwise we can move socket to fd #0 only if bind succeeds) */ |
| 472 | close(0); |
Denys Vlasenko | ca18311 | 2011-04-07 17:52:20 +0200 | [diff] [blame] | 473 | set_nport(&localp->u.sa, htons(local_port)); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 474 | xmove_fd(xsocket(localp->u.sa.sa_family, SOCK_DGRAM, 0), 0); |
Denis Vlasenko | 7946879 | 2007-04-04 11:02:55 +0000 | [diff] [blame] | 475 | setsockopt_reuseaddr(0); /* crucial */ |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 476 | xbind(0, &localp->u.sa, localp->len); |
Denis Vlasenko | 7946879 | 2007-04-04 11:02:55 +0000 | [diff] [blame] | 477 | #endif |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 478 | } |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 479 | |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 480 | pid = vfork(); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 481 | if (pid == -1) { |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 482 | bb_perror_msg("vfork"); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 483 | goto again; |
| 484 | } |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 485 | |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 486 | if (pid != 0) { |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 487 | /* Parent */ |
Denis Vlasenko | 02fd668 | 2007-04-03 23:23:10 +0000 | [diff] [blame] | 488 | cnum++; |
| 489 | if (verbose) |
| 490 | connection_status(); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 491 | if (hccp) |
| 492 | hccp->pid = pid; |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 493 | /* clean up changes done by vforked child */ |
| 494 | undo_xsetenv(); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 495 | goto again; |
| 496 | } |
| 497 | |
| 498 | /* Child: prepare env, log, and exec prog */ |
| 499 | |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 500 | { /* vfork alert! every xmalloc in this block should be freed! */ |
| 501 | char *local_hostname = local_hostname; /* for compiler */ |
| 502 | char *local_addr = NULL; |
| 503 | char *free_me0 = NULL; |
| 504 | char *free_me1 = NULL; |
| 505 | char *free_me2 = NULL; |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 506 | |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 507 | if (verbose || !(opts & OPT_E)) { |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 508 | if (!max_per_host) /* remote_addr is not yet known */ |
| 509 | free_me0 = remote_addr = xmalloc_sockaddr2dotted(&remote.u.sa); |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 510 | if (opts & OPT_h) { |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 511 | free_me1 = remote_hostname = xmalloc_sockaddr2host_noport(&remote.u.sa); |
| 512 | if (!remote_hostname) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 513 | bb_error_msg("can't look up hostname for %s", remote_addr); |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 514 | remote_hostname = remote_addr; |
| 515 | } |
| 516 | } |
| 517 | /* Find out local IP peer connected to. |
| 518 | * Errors ignored (I'm not paranoid enough to imagine kernel |
| 519 | * which doesn't know local IP). */ |
| 520 | if (tcp) |
| 521 | getsockname(0, &local.u.sa, &local.len); |
| 522 | /* else: for UDP it is done earlier by parent */ |
| 523 | local_addr = xmalloc_sockaddr2dotted(&local.u.sa); |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 524 | if (opts & OPT_h) { |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 525 | local_hostname = preset_local_hostname; |
| 526 | if (!local_hostname) { |
| 527 | free_me2 = local_hostname = xmalloc_sockaddr2host_noport(&local.u.sa); |
| 528 | if (!local_hostname) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 529 | bb_error_msg_and_die("can't look up hostname for %s", local_addr); |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 530 | } |
| 531 | /* else: local_hostname is not NULL, but is NOT malloced! */ |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 532 | } |
| 533 | } |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 534 | if (verbose) { |
| 535 | pid = getpid(); |
| 536 | if (max_per_host) { |
| 537 | bb_error_msg("concurrency %s %u/%u", |
| 538 | remote_addr, |
| 539 | cur_per_host, max_per_host); |
| 540 | } |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 541 | bb_error_msg((opts & OPT_h) |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 542 | ? "start %u %s-%s (%s-%s)" |
| 543 | : "start %u %s-%s", |
| 544 | pid, |
| 545 | local_addr, remote_addr, |
| 546 | local_hostname, remote_hostname); |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 547 | } |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 548 | |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 549 | if (!(opts & OPT_E)) { |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 550 | /* setup ucspi env */ |
| 551 | const char *proto = tcp ? "TCP" : "UDP"; |
| 552 | |
Jeremie Koenig | 2ea12d8 | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 553 | #ifdef SO_ORIGINAL_DST |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 554 | /* Extract "original" destination addr:port |
| 555 | * from Linux firewall. Useful when you redirect |
| 556 | * an outbond connection to local handler, and it needs |
| 557 | * to know where it originally tried to connect */ |
| 558 | if (tcp && getsockopt(0, SOL_IP, SO_ORIGINAL_DST, &local.u.sa, &local.len) == 0) { |
| 559 | char *addr = xmalloc_sockaddr2dotted(&local.u.sa); |
| 560 | xsetenv_plain("TCPORIGDSTADDR", addr); |
| 561 | free(addr); |
| 562 | } |
Jeremie Koenig | 2ea12d8 | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 563 | #endif |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 564 | xsetenv_plain("PROTO", proto); |
| 565 | xsetenv_proto(proto, "LOCALADDR", local_addr); |
| 566 | xsetenv_proto(proto, "REMOTEADDR", remote_addr); |
Denys Vlasenko | d6513cf | 2009-07-19 23:07:13 +0200 | [diff] [blame] | 567 | if (opts & OPT_h) { |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 568 | xsetenv_proto(proto, "LOCALHOST", local_hostname); |
| 569 | xsetenv_proto(proto, "REMOTEHOST", remote_hostname); |
| 570 | } |
| 571 | //compat? xsetenv_proto(proto, "REMOTEINFO", ""); |
| 572 | /* additional */ |
| 573 | if (cur_per_host > 0) /* can not be true for udp */ |
| 574 | xsetenv_plain("TCPCONCURRENCY", utoa(cur_per_host)); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 575 | } |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 576 | free(local_addr); |
| 577 | free(free_me0); |
| 578 | free(free_me1); |
| 579 | free(free_me2); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Denis Vlasenko | aefed94 | 2008-03-17 08:35:44 +0000 | [diff] [blame] | 582 | xdup2(0, 1); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 583 | |
Denis Vlasenko | 3fa36e2 | 2008-11-09 00:15:11 +0000 | [diff] [blame] | 584 | signal(SIGPIPE, SIG_DFL); /* this one was SIG_IGNed */ |
| 585 | /* Non-ignored signals revert to SIG_DFL on exec anyway */ |
| 586 | /*signal(SIGCHLD, SIG_DFL);*/ |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 587 | sig_unblock(SIGCHLD); |
| 588 | |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 589 | #ifdef SSLSVD |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 590 | strcpy(id, utoa(pid)); |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 591 | ssl_io(0, argv); |
Denys Vlasenko | 41ddd9f | 2010-06-25 01:46:53 +0200 | [diff] [blame] | 592 | bb_perror_msg_and_die("can't execute '%s'", argv[0]); |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 593 | #else |
| 594 | BB_EXECVP_or_die(argv); |
| 595 | #endif |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /* |
| 599 | tcpsvd [-hpEvv] [-c n] [-C n:msg] [-b n] [-u user] [-l name] |
| 600 | [-i dir|-x cdb] [ -t sec] host port prog |
| 601 | |
| 602 | tcpsvd creates a TCP/IP socket, binds it to the address host:port, |
| 603 | and listens on the socket for incoming connections. |
| 604 | |
| 605 | On each incoming connection, tcpsvd conditionally runs a program, |
| 606 | with standard input reading from the socket, and standard output |
| 607 | writing to the socket, to handle this connection. tcpsvd keeps |
| 608 | listening on the socket for new connections, and can handle |
| 609 | multiple connections simultaneously. |
| 610 | |
| 611 | tcpsvd optionally checks for special instructions depending |
| 612 | on the IP address or hostname of the client that initiated |
| 613 | the connection, see ipsvd-instruct(5). |
| 614 | |
| 615 | host |
| 616 | host either is a hostname, or a dotted-decimal IP address, |
| 617 | or 0. If host is 0, tcpsvd accepts connections to any local |
| 618 | IP address. |
| 619 | * busybox accepts IPv6 addresses and host:port pairs too |
| 620 | In this case second parameter is ignored |
| 621 | port |
| 622 | tcpsvd accepts connections to host:port. port may be a name |
| 623 | from /etc/services or a number. |
| 624 | prog |
| 625 | prog consists of one or more arguments. For each connection, |
| 626 | tcpsvd normally runs prog, with file descriptor 0 reading from |
| 627 | the network, and file descriptor 1 writing to the network. |
| 628 | By default it also sets up TCP-related environment variables, |
| 629 | see tcp-environ(5) |
| 630 | -i dir |
| 631 | read instructions for handling new connections from the instructions |
| 632 | directory dir. See ipsvd-instruct(5) for details. |
| 633 | * ignored by busyboxed version |
| 634 | -x cdb |
| 635 | read instructions for handling new connections from the constant database |
| 636 | cdb. The constant database normally is created from an instructions |
| 637 | directory by running ipsvd-cdb(8). |
| 638 | * ignored by busyboxed version |
| 639 | -t sec |
| 640 | timeout. This option only takes effect if the -i option is given. |
| 641 | While checking the instructions directory, check the time of last access |
| 642 | of the file that matches the clients address or hostname if any, discard |
| 643 | and remove the file if it wasn't accessed within the last sec seconds; |
| 644 | tcpsvd does not discard or remove a file if the user's write permission |
| 645 | is not set, for those files the timeout is disabled. Default is 0, |
| 646 | which means that the timeout is disabled. |
| 647 | * ignored by busyboxed version |
| 648 | -l name |
| 649 | local hostname. Do not look up the local hostname in DNS, but use name |
| 650 | as hostname. This option must be set if tcpsvd listens on port 53 |
| 651 | to avoid loops. |
| 652 | -u user[:group] |
| 653 | drop permissions. Switch user ID to user's UID, and group ID to user's |
| 654 | primary GID after creating and binding to the socket. If user is followed |
| 655 | by a colon and a group name, the group ID is switched to the GID of group |
| 656 | instead. All supplementary groups are removed. |
| 657 | -c n |
| 658 | concurrency. Handle up to n connections simultaneously. Default is 30. |
| 659 | If there are n connections active, tcpsvd defers acceptance of a new |
| 660 | connection until an active connection is closed. |
| 661 | -C n[:msg] |
| 662 | per host concurrency. Allow only up to n connections from the same IP |
| 663 | address simultaneously. If there are n active connections from one IP |
| 664 | address, new incoming connections from this IP address are closed |
| 665 | immediately. If n is followed by :msg, the message msg is written |
| 666 | to the client if possible, before closing the connection. By default |
| 667 | msg is empty. See ipsvd-instruct(5) for supported escape sequences in msg. |
| 668 | |
| 669 | For each accepted connection, the current per host concurrency is |
| 670 | available through the environment variable TCPCONCURRENCY. n and msg |
| 671 | can be overwritten by ipsvd(7) instructions, see ipsvd-instruct(5). |
| 672 | By default tcpsvd doesn't keep track of connections. |
| 673 | -h |
| 674 | Look up the client's hostname in DNS. |
| 675 | -p |
| 676 | paranoid. After looking up the client's hostname in DNS, look up the IP |
| 677 | addresses in DNS for that hostname, and forget about the hostname |
| 678 | if none of the addresses match the client's IP address. You should |
| 679 | set this option if you use hostname based instructions. The -p option |
| 680 | implies the -h option. |
| 681 | * ignored by busyboxed version |
| 682 | -b n |
| 683 | backlog. Allow a backlog of approximately n TCP SYNs. On some systems n |
| 684 | is silently limited. Default is 20. |
| 685 | -E |
| 686 | no special environment. Do not set up TCP-related environment variables. |
| 687 | -v |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 688 | verbose. Print verbose messages to standard output. |
Denis Vlasenko | b933ac1 | 2007-04-03 12:09:46 +0000 | [diff] [blame] | 689 | -vv |
| 690 | more verbose. Print more verbose messages to standard output. |
| 691 | * no difference between -v and -vv in busyboxed version |
| 692 | */ |