tor-fw-helper.c 11 KB

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