tor-fw-helper.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* Copyright (c) 2010, Jacob Appelbaum, Steven J. Murdoch.
  2. * Copyright (c) 2010-2011, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file tor-fw-helper.c
  6. * \brief The main wrapper around our firewall helper logic.
  7. **/
  8. /*
  9. * tor-fw-helper is a tool for opening firewalls with NAT-PMP and UPnP; this
  10. * tool is designed to be called by hand or by Tor by way of a exec() at a
  11. * later date.
  12. */
  13. #include "orconfig.h"
  14. #include <stdio.h>
  15. #include <stdint.h>
  16. #include <stdlib.h>
  17. #include <getopt.h>
  18. #include <time.h>
  19. #include <string.h>
  20. #ifdef _WIN32
  21. #include <winsock2.h>
  22. #endif
  23. #include "tor-fw-helper.h"
  24. #ifdef NAT_PMP
  25. #include "tor-fw-helper-natpmp.h"
  26. #endif
  27. #ifdef MINIUPNPC
  28. #include "tor-fw-helper-upnp.h"
  29. #endif
  30. /** This is our meta storage type - it holds information about each helper
  31. including the total number of helper backends, function pointers, and helper
  32. state. */
  33. typedef struct backends_t {
  34. /** The total number of backends */
  35. int n_backends;
  36. /** The backend functions as an array */
  37. tor_fw_backend_t backend_ops[MAX_BACKENDS];
  38. /** The internal backend state */
  39. void *backend_state[MAX_BACKENDS];
  40. } backends_t;
  41. /** Initalize each backend helper with the user input stored in <b>options</b>
  42. * and put the results in the <b>backends</b> struct. */
  43. static int
  44. init_backends(tor_fw_options_t *options, backends_t *backends)
  45. {
  46. int n_available = 0;
  47. int i, r, n;
  48. tor_fw_backend_t *backend_ops_list[MAX_BACKENDS];
  49. void *data = NULL;
  50. /* First, build a list of the working backends. */
  51. n = 0;
  52. #ifdef MINIUPNPC
  53. backend_ops_list[n++] = (tor_fw_backend_t *) tor_fw_get_miniupnp_backend();
  54. #endif
  55. #ifdef NAT_PMP
  56. backend_ops_list[n++] = (tor_fw_backend_t *) tor_fw_get_natpmp_backend();
  57. #endif
  58. n_available = n;
  59. /* Now, for each backend that might work, try to initialize it.
  60. * That's how we roll, initialized.
  61. */
  62. n = 0;
  63. for (i=0; i<n_available; ++i) {
  64. data = calloc(1, backend_ops_list[i]->state_len);
  65. if (!data) {
  66. perror("calloc");
  67. exit(1);
  68. }
  69. r = backend_ops_list[i]->init(options, data);
  70. if (r == 0) {
  71. backends->backend_ops[n] = *backend_ops_list[i];
  72. backends->backend_state[n] = data;
  73. n++;
  74. } else {
  75. free(data);
  76. }
  77. }
  78. backends->n_backends = n;
  79. return n;
  80. }
  81. /** Return the proper commandline switches when the user needs information. */
  82. static void
  83. usage(void)
  84. {
  85. fprintf(stderr, "tor-fw-helper usage:\n"
  86. " [-h|--help]\n"
  87. " [-T|--Test]\n"
  88. " [-v|--verbose]\n"
  89. " [-g|--fetch-public-ip]\n"
  90. " -i|--internal-or-port [TCP port]\n"
  91. " [-e|--external-or-port [TCP port]]\n"
  92. " [-d|--internal-dir-port [TCP port]\n"
  93. " [-p|--external-dir-port [TCP port]]]\n");
  94. }
  95. /** Log commandline options to a hardcoded file <b>tor-fw-helper.log</b> in the
  96. * current working directory. */
  97. static int
  98. log_commandline_options(int argc, char **argv)
  99. {
  100. int i, retval;
  101. FILE *logfile;
  102. time_t now;
  103. /* Open the log file */
  104. logfile = fopen("tor-fw-helper.log", "a");
  105. if (NULL == logfile)
  106. return -1;
  107. /* Send all commandline arguments to the file */
  108. now = time(NULL);
  109. retval = fprintf(logfile, "START: %s\n", ctime(&now));
  110. for (i = 0; i < argc; i++) {
  111. retval = fprintf(logfile, "ARG: %d: %s\n", i, argv[i]);
  112. if (retval < 0)
  113. goto error;
  114. retval = fprintf(stdout, "ARG: %d: %s\n", i, argv[i]);
  115. if (retval < 0)
  116. goto error;
  117. }
  118. now = time(NULL);
  119. retval = fprintf(logfile, "END: %s\n", ctime(&now));
  120. /* Close and clean up */
  121. retval = fclose(logfile);
  122. return retval;
  123. /* If there was an error during writing */
  124. error:
  125. fclose(logfile);
  126. return -1;
  127. }
  128. /** Iterate over over each of the supported <b>backends</b> and attempt to
  129. * fetch the public ip. */
  130. static void
  131. tor_fw_fetch_public_ip(tor_fw_options_t *tor_fw_options,
  132. backends_t *backends)
  133. {
  134. int i;
  135. int r = 0;
  136. if (tor_fw_options->verbose)
  137. fprintf(stdout, "V: tor_fw_fetch_public_ip\n");
  138. for (i=0; i<backends->n_backends; ++i) {
  139. if (tor_fw_options->verbose) {
  140. fprintf(stdout, "V: running backend_state now: %i\n", i);
  141. fprintf(stdout, "V: size of backend state: %u\n",
  142. (int)(backends->backend_ops)[i].state_len);
  143. fprintf(stdout, "V: backend state name: %s\n",
  144. (char *)(backends->backend_ops)[i].name);
  145. }
  146. r = backends->backend_ops[i].fetch_public_ip(tor_fw_options,
  147. backends->backend_state[i]);
  148. fprintf(stdout, "tor-fw-helper: tor_fw_fetch_public_ip backend %s "
  149. " returned: %i\n", (char *)(backends->backend_ops)[i].name, r);
  150. }
  151. }
  152. /** Iterate over each of the supported <b>backends</b> and attempt to add a
  153. * port forward for the OR port stored in <b>tor_fw_options</b>. */
  154. static void
  155. tor_fw_add_or_port(tor_fw_options_t *tor_fw_options,
  156. backends_t *backends)
  157. {
  158. int i;
  159. int r = 0;
  160. if (tor_fw_options->verbose)
  161. fprintf(stdout, "V: tor_fw_add_or_port\n");
  162. for (i=0; i<backends->n_backends; ++i) {
  163. if (tor_fw_options->verbose) {
  164. fprintf(stdout, "V: running backend_state now: %i\n", i);
  165. fprintf(stdout, "V: size of backend state: %u\n",
  166. (int)(backends->backend_ops)[i].state_len);
  167. fprintf(stdout, "V: backend state name: %s\n",
  168. (const char *) backends->backend_ops[i].name);
  169. }
  170. r = backends->backend_ops[i].add_tcp_mapping(tor_fw_options,
  171. backends->backend_state[i]);
  172. fprintf(stdout, "tor-fw-helper: tor_fw_add_or_port backend %s "
  173. "returned: %i\n", (const char *) backends->backend_ops[i].name, r);
  174. }
  175. }
  176. /** Iterate over each of the supported <b>backends</b> and attempt to add a
  177. * port forward for the Dir port stored in <b>tor_fw_options</b>. */
  178. static void
  179. tor_fw_add_dir_port(tor_fw_options_t *tor_fw_options,
  180. backends_t *backends)
  181. {
  182. int i;
  183. int r = 0;
  184. if (tor_fw_options->verbose)
  185. fprintf(stdout, "V: tor_fw_add_dir_port\n");
  186. for (i=0; i<backends->n_backends; ++i) {
  187. if (tor_fw_options->verbose) {
  188. fprintf(stdout, "V: running backend_state now: %i\n", i);
  189. fprintf(stdout, "V: size of backend state: %u\n",
  190. (int)(backends->backend_ops)[i].state_len);
  191. fprintf(stdout, "V: backend state name: %s\n",
  192. (char *)(backends->backend_ops)[i].name);
  193. }
  194. r = backends->backend_ops[i].add_tcp_mapping(tor_fw_options,
  195. backends->backend_state[i]);
  196. fprintf(stdout, "tor-fw-helper: tor_fw_add_dir_port backend %s "
  197. "returned: %i\n", (const char *)backends->backend_ops[i].name, r);
  198. }
  199. }
  200. /** Called before we make any calls to network-related functions.
  201. * (Some operating systems require their network libraries to be
  202. * initialized.) (from common/compat.c) */
  203. static int
  204. network_init(void)
  205. {
  206. #ifdef _WIN32
  207. /* This silly exercise is necessary before windows will allow
  208. * gethostbyname to work. */
  209. WSADATA WSAData;
  210. int r;
  211. r = WSAStartup(0x101, &WSAData);
  212. if (r) {
  213. fprintf(stderr, "E: Error initializing Windows network layer "
  214. "- code was %d", r);
  215. return -1;
  216. }
  217. /* WSAData.iMaxSockets might show the max sockets we're allowed to use.
  218. * We might use it to complain if we're trying to be a server but have
  219. * too few sockets available. */
  220. #endif
  221. return 0;
  222. }
  223. int
  224. main(int argc, char **argv)
  225. {
  226. int r = 0;
  227. int c = 0;
  228. tor_fw_options_t tor_fw_options;
  229. backends_t backend_state;
  230. memset(&tor_fw_options, 0, sizeof(tor_fw_options));
  231. memset(&backend_state, 0, sizeof(backend_state));
  232. while (1) {
  233. int option_index = 0;
  234. static struct option long_options[] =
  235. {
  236. {"verbose", 0, 0, 'v'},
  237. {"help", 0, 0, 'h'},
  238. {"internal-or-port", 1, 0, 'i'},
  239. {"external-or-port", 1, 0, 'e'},
  240. {"internal-dir-port", 1, 0, 'd'},
  241. {"external-dir-port", 1, 0, 'p'},
  242. {"fetch-public-ip", 0, 0, 'g'},
  243. {"test-commandline", 0, 0, 'T'},
  244. {0, 0, 0, 0}
  245. };
  246. c = getopt_long(argc, argv, "vhi:e:d:p:gT",
  247. long_options, &option_index);
  248. if (c == -1)
  249. break;
  250. switch (c) {
  251. case 'v': tor_fw_options.verbose = 1; break;
  252. case 'h': tor_fw_options.help = 1; usage(); exit(1); break;
  253. case 'i': sscanf(optarg, "%hu", &tor_fw_options.private_or_port);
  254. break;
  255. case 'e': sscanf(optarg, "%hu", &tor_fw_options.public_or_port);
  256. break;
  257. case 'd': sscanf(optarg, "%hu", &tor_fw_options.private_dir_port);
  258. break;
  259. case 'p': sscanf(optarg, "%hu", &tor_fw_options.public_dir_port);
  260. break;
  261. case 'g': tor_fw_options.fetch_public_ip = 1; break;
  262. case 'T': tor_fw_options.test_commandline = 1; break;
  263. case '?': break;
  264. default : fprintf(stderr, "Unknown option!\n"); usage(); exit(1);
  265. }
  266. }
  267. if (tor_fw_options.verbose) {
  268. fprintf(stderr, "V: tor-fw-helper version %s\n"
  269. "V: We were called with the following arguments:\n"
  270. "V: verbose = %d, help = %d, pub or port = %u, "
  271. "priv or port = %u\n"
  272. "V: pub dir port = %u, priv dir port = %u\n"
  273. "V: fetch_public_ip = %u\n",
  274. tor_fw_version, tor_fw_options.verbose, tor_fw_options.help,
  275. tor_fw_options.private_or_port, tor_fw_options.public_or_port,
  276. tor_fw_options.private_dir_port, tor_fw_options.public_dir_port,
  277. tor_fw_options.fetch_public_ip);
  278. }
  279. if (tor_fw_options.test_commandline) {
  280. return log_commandline_options(argc, argv);
  281. }
  282. /* At the very least, we require an ORPort;
  283. Given a private ORPort, we can ask for a mapping that matches the port
  284. externally.
  285. */
  286. if (!tor_fw_options.private_or_port && !tor_fw_options.fetch_public_ip) {
  287. fprintf(stderr, "E: We require an ORPort or fetch_public_ip"
  288. " request!\n");
  289. usage();
  290. exit(1);
  291. } else {
  292. /* When we only have one ORPort, internal/external are
  293. set to be the same.*/
  294. if (!tor_fw_options.public_or_port && tor_fw_options.private_or_port) {
  295. if (tor_fw_options.verbose)
  296. fprintf(stdout, "V: We're setting public_or_port = "
  297. "private_or_port.\n");
  298. tor_fw_options.public_or_port = tor_fw_options.private_or_port;
  299. }
  300. }
  301. if (!tor_fw_options.private_dir_port) {
  302. if (tor_fw_options.verbose)
  303. fprintf(stdout, "V: We have no DirPort; no hole punching for "
  304. "DirPorts\n");
  305. } else {
  306. /* When we only have one DirPort, internal/external are
  307. set to be the same.*/
  308. if (!tor_fw_options.public_dir_port && tor_fw_options.private_dir_port) {
  309. if (tor_fw_options.verbose)
  310. fprintf(stdout, "V: We're setting public_or_port = "
  311. "private_or_port.\n");
  312. tor_fw_options.public_dir_port = tor_fw_options.private_dir_port;
  313. }
  314. }
  315. if (tor_fw_options.verbose) {
  316. fprintf(stdout, "V: pub or port = %u, priv or port = %u\n"
  317. "V: pub dir port = %u, priv dir port = %u\n",
  318. tor_fw_options.private_or_port, tor_fw_options.public_or_port,
  319. tor_fw_options.private_dir_port,
  320. tor_fw_options.public_dir_port);
  321. }
  322. // Initialize networking
  323. if (network_init())
  324. exit(1);
  325. // Initalize the various fw-helper backend helpers
  326. r = init_backends(&tor_fw_options, &backend_state);
  327. if (r)
  328. printf("tor-fw-helper: %i NAT traversal helper(s) loaded\n", r);
  329. if (tor_fw_options.fetch_public_ip) {
  330. tor_fw_fetch_public_ip(&tor_fw_options, &backend_state);
  331. }
  332. if (tor_fw_options.private_or_port) {
  333. tor_fw_options.internal_port = tor_fw_options.private_or_port;
  334. tor_fw_options.external_port = tor_fw_options.private_or_port;
  335. tor_fw_add_or_port(&tor_fw_options, &backend_state);
  336. }
  337. if (tor_fw_options.private_dir_port) {
  338. tor_fw_options.internal_port = tor_fw_options.private_dir_port;
  339. tor_fw_options.external_port = tor_fw_options.private_dir_port;
  340. tor_fw_add_dir_port(&tor_fw_options, &backend_state);
  341. }
  342. r = (((tor_fw_options.nat_pmp_status | tor_fw_options.upnp_status)
  343. |tor_fw_options.public_ip_status));
  344. if (r > 0) {
  345. fprintf(stdout, "tor-fw-helper: SUCCESS\n");
  346. } else {
  347. fprintf(stderr, "tor-fw-helper: FAILURE\n");
  348. }
  349. exit(r);
  350. }