tor-fw-helper.c 12 KB

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