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