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