tor-fw-helper.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. * tor-fw-helper is a tool for opening firewalls with NAT-PMP and UPnP; this
  6. * tool is designed to be called by hand or by Tor by way of a exec() at a
  7. * later date.
  8. */
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <getopt.h>
  13. #include <time.h>
  14. #include "orconfig.h"
  15. #include "tor-fw-helper.h"
  16. #include "tor-fw-helper-natpmp.h"
  17. #include "tor-fw-helper-upnp.h"
  18. static void
  19. usage(void)
  20. {
  21. fprintf(stderr, "tor-fw-helper usage:\n"
  22. " [-h|--help]\n"
  23. " [-T|--Test]\n"
  24. " [-v|--verbose]\n"
  25. " [-g|--fetch-public-ip]\n"
  26. " -i|--internal-or-port [TCP port]\n"
  27. " [-e|--external-or-port [TCP port]]\n"
  28. " [-d|--internal-dir-port [TCP port]\n"
  29. " [-p|--external-dir-port [TCP port]]]\n");
  30. }
  31. /* Log commandline options */
  32. static int
  33. test_commandline_options(int argc, char **argv)
  34. {
  35. int i, retval;
  36. FILE *logfile;
  37. time_t now;
  38. /* Open the log file */
  39. logfile = fopen("tor-fw-helper.log", "a");
  40. if (NULL == logfile)
  41. return -1;
  42. /* Send all commandline arguments to the file */
  43. now = time(NULL);
  44. retval = fprintf(logfile, "START: %s\n", ctime(&now));
  45. for (i = 0; i < argc; i++) {
  46. retval = fprintf(logfile, "ARG: %d: %s\n", i, argv[i]);
  47. if (retval < 0)
  48. goto error;
  49. retval = fprintf(stdout, "ARG: %d: %s\n", i, argv[i]);
  50. if (retval < 0)
  51. goto error;
  52. }
  53. now = time(NULL);
  54. retval = fprintf(logfile, "END: %s\n", ctime(&now));
  55. /* Close and clean up */
  56. retval = fclose(logfile);
  57. return retval;
  58. /* If there was an error during writing */
  59. error:
  60. fclose(logfile);
  61. return -1;
  62. }
  63. static void
  64. tor_fw_fetch_public_ip(tor_fw_options_t *tor_fw_options,
  65. miniupnpc_state_t *miniupnpc_state)
  66. {
  67. int r = 0;
  68. r = tor_natpmp_fetch_public_ip(tor_fw_options);
  69. if (tor_fw_options->verbose)
  70. fprintf(stdout, "V: Attempts to fetch public ip (natpmp) resulted in: "
  71. "%d\n", r);
  72. if (r == 0)
  73. tor_fw_options->public_ip_status = 1;
  74. r = tor_upnp_fetch_public_ip(miniupnpc_state);
  75. if (tor_fw_options->verbose)
  76. fprintf(stdout, "V: Attempts to fetch public ip (upnp) resulted in: "
  77. "%d\n", r);
  78. if (r == 0)
  79. tor_fw_options->public_ip_status = 1;
  80. }
  81. static void
  82. tor_fw_add_or_port(tor_fw_options_t *tor_fw_options, miniupnpc_state_t
  83. *miniupnpc_state)
  84. {
  85. int r = 0;
  86. tor_fw_options->internal_port = tor_fw_options->private_or_port;
  87. tor_fw_options->external_port = tor_fw_options->public_or_port;
  88. r = tor_natpmp_add_tcp_mapping(tor_fw_options);
  89. fprintf(stdout, "tor-fw-helper: Attempts to add ORPort mapping (natpmp)"
  90. "resulted in: %d\n", r);
  91. if (r == 0)
  92. tor_fw_options->nat_pmp_status = 1;
  93. r = tor_upnp_add_tcp_mapping(miniupnpc_state,
  94. tor_fw_options->private_or_port,
  95. tor_fw_options->public_or_port);
  96. fprintf(stdout, "tor-fw-helper: Attempts to add ORPort mapping (upnp)"
  97. "resulted in: %d\n", r);
  98. if (r == 0)
  99. tor_fw_options->upnp_status = 1;
  100. }
  101. static void
  102. tor_fw_add_dir_port(tor_fw_options_t *tor_fw_options, miniupnpc_state_t
  103. *miniupnpc_state)
  104. {
  105. int r = 0;
  106. tor_fw_options->internal_port = tor_fw_options->private_dir_port;
  107. tor_fw_options->external_port = tor_fw_options->public_dir_port;
  108. r = tor_natpmp_add_tcp_mapping(tor_fw_options);
  109. fprintf(stdout, "V: Attempts to add DirPort mapping (natpmp) resulted in: "
  110. "%d\n", r);
  111. r = tor_upnp_add_tcp_mapping(miniupnpc_state,
  112. tor_fw_options->private_or_port,
  113. tor_fw_options->public_or_port);
  114. fprintf(stdout, "V: Attempts to add DirPort mapping (upnp) resulted in: "
  115. "%d\n",
  116. r);
  117. }
  118. int
  119. main(int argc, char **argv)
  120. {
  121. int r = 0;
  122. int c = 0;
  123. tor_fw_options_t tor_fw_options = {0,0,0,0,0,0,0,0,0,0,0,0,0};
  124. miniupnpc_state_t miniupnpc_state;
  125. miniupnpc_state.init = 0;
  126. while (1)
  127. {
  128. int option_index = 0;
  129. static struct option long_options[] =
  130. {
  131. {"verbose", 0, 0, 'v'},
  132. {"help", 0, 0, 'h'},
  133. {"internal-or-port", 1, 0, 'i'},
  134. {"external-or-port", 1, 0, 'e'},
  135. {"internal-dir-port", 1, 0, 'd'},
  136. {"external-dir-port", 1, 0, 'p'},
  137. {"fetch-public-ip", 0, 0, 'g'},
  138. {"test-commandline", 0, 0, 'T'},
  139. {0, 0, 0, 0}
  140. };
  141. c = getopt_long(argc, argv, "vhi:e:d:p:gT",
  142. long_options, &option_index);
  143. if (c == -1)
  144. break;
  145. switch (c)
  146. {
  147. case 'v': tor_fw_options.verbose = 1; break;
  148. case 'h': tor_fw_options.help = 1; usage(); exit(1); break;
  149. case 'i': sscanf(optarg, "%hu", &tor_fw_options.private_or_port);
  150. break;
  151. case 'e': sscanf(optarg, "%hu", &tor_fw_options.public_or_port);
  152. break;
  153. case 'd': sscanf(optarg, "%hu", &tor_fw_options.private_dir_port);
  154. break;
  155. case 'p': sscanf(optarg, "%hu", &tor_fw_options.public_dir_port);
  156. break;
  157. case 'g': tor_fw_options.fetch_public_ip = 1; break;
  158. case 'T': tor_fw_options.test_commandline = 1; break;
  159. case '?': break;
  160. default : fprintf(stderr, "Unknown option!\n"); usage(); exit(1);
  161. }
  162. }
  163. if (tor_fw_options.verbose)
  164. {
  165. fprintf(stderr, "V: tor-fw-helper version %s\n"
  166. "V: We were called with the following arguments:\n"
  167. "V: verbose = %d, help = %d, pub or port = %u, "
  168. "priv or port = %u\n"
  169. "V: pub dir port = %u, priv dir port = %u\n"
  170. "V: fetch_public_ip = %u\n",
  171. tor_fw_version, tor_fw_options.verbose, tor_fw_options.help,
  172. tor_fw_options.private_or_port, tor_fw_options.public_or_port,
  173. tor_fw_options.private_dir_port, tor_fw_options.public_dir_port,
  174. tor_fw_options.fetch_public_ip);
  175. }
  176. if (tor_fw_options.test_commandline) {
  177. return test_commandline_options(argc, argv);
  178. }
  179. /* At the very least, we require an ORPort;
  180. Given a private ORPort, we can ask for a mapping that matches the port
  181. externally.
  182. */
  183. if (!tor_fw_options.private_or_port && !tor_fw_options.fetch_public_ip)
  184. {
  185. fprintf(stderr, "E: We require an ORPort or fetch_public_ip"
  186. " request!\n");
  187. usage();
  188. exit(1);
  189. } else {
  190. /* When we only have one ORPort, internal/external are
  191. set to be the same.*/
  192. if (!tor_fw_options.public_or_port && tor_fw_options.private_or_port)
  193. {
  194. if (tor_fw_options.verbose)
  195. fprintf(stdout, "V: We're setting public_or_port = "
  196. "private_or_port.\n");
  197. tor_fw_options.public_or_port = tor_fw_options.private_or_port;
  198. }
  199. }
  200. if (!tor_fw_options.private_dir_port)
  201. {
  202. if (tor_fw_options.verbose)
  203. fprintf(stdout, "V: We have no DirPort; no hole punching for "
  204. "DirPorts\n");
  205. } else {
  206. /* When we only have one DirPort, internal/external are
  207. set to be the same.*/
  208. if (!tor_fw_options.public_dir_port && tor_fw_options.private_dir_port)
  209. {
  210. if (tor_fw_options.verbose)
  211. fprintf(stdout, "V: We're setting public_or_port = "
  212. "private_or_port.\n");
  213. tor_fw_options.public_dir_port = tor_fw_options.private_dir_port;
  214. }
  215. }
  216. if (tor_fw_options.verbose)
  217. {
  218. fprintf(stdout, "V: pub or port = %u, priv or port = %u\n"
  219. "V: pub dir port = %u, priv dir port = %u\n",
  220. tor_fw_options.private_or_port, tor_fw_options.public_or_port,
  221. tor_fw_options.private_dir_port,
  222. tor_fw_options.public_dir_port);
  223. }
  224. if (tor_fw_options.fetch_public_ip)
  225. {
  226. tor_fw_fetch_public_ip(&tor_fw_options, &miniupnpc_state);
  227. }
  228. if (tor_fw_options.private_or_port)
  229. {
  230. tor_fw_add_or_port(&tor_fw_options, &miniupnpc_state);
  231. }
  232. if (tor_fw_options.private_dir_port)
  233. {
  234. tor_fw_add_dir_port(&tor_fw_options, &miniupnpc_state);
  235. }
  236. r = (((tor_fw_options.nat_pmp_status | tor_fw_options.upnp_status)
  237. |tor_fw_options.public_ip_status));
  238. if (r > 0)
  239. {
  240. fprintf(stdout, "tor-fw-helper: SUCCESS\n");
  241. } else {
  242. fprintf(stderr, "tor-fw-helper: FAILURE\n");
  243. }
  244. exit(r);
  245. }