test_entryconn.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /* Copyright (c) 2014-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #define CONNECTION_PRIVATE
  5. #define CONNECTION_EDGE_PRIVATE
  6. #include "or.h"
  7. #include "test.h"
  8. #include "addressmap.h"
  9. #include "config.h"
  10. #include "confparse.h"
  11. #include "connection.h"
  12. #include "connection_edge.h"
  13. static void *
  14. entryconn_rewrite_setup(const struct testcase_t *tc)
  15. {
  16. (void)tc;
  17. entry_connection_t *ec = entry_connection_new(CONN_TYPE_AP, AF_INET);
  18. addressmap_init();
  19. return ec;
  20. }
  21. static int
  22. entryconn_rewrite_teardown(const struct testcase_t *tc, void *arg)
  23. {
  24. (void)tc;
  25. entry_connection_t *ec = arg;
  26. if (ec)
  27. connection_free_(ENTRY_TO_CONN(ec));
  28. addressmap_free_all();
  29. return 1;
  30. }
  31. static struct testcase_setup_t test_rewrite_setup = {
  32. entryconn_rewrite_setup, entryconn_rewrite_teardown
  33. };
  34. /* Simple rewrite: no changes needed */
  35. static void
  36. test_entryconn_rewrite_basic(void *arg)
  37. {
  38. entry_connection_t *ec = arg;
  39. rewrite_result_t rr;
  40. tt_assert(ec->socks_request);
  41. strlcpy(ec->socks_request->address, "www.TORproject.org",
  42. sizeof(ec->socks_request->address));
  43. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  44. connection_ap_handshake_rewrite(ec, &rr);
  45. tt_int_op(rr.should_close, OP_EQ, 0);
  46. tt_int_op(rr.end_reason, OP_EQ, 0);
  47. tt_int_op(rr.automap, OP_EQ, 0);
  48. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  49. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  50. tt_str_op(rr.orig_address, OP_EQ, "www.torproject.org");
  51. tt_str_op(ec->socks_request->address, OP_EQ, "www.torproject.org");
  52. tt_str_op(ec->original_dest_address, OP_EQ, "www.torproject.org");
  53. done:
  54. ;
  55. }
  56. /* Rewrite but reject because of disallowed .exit */
  57. static void
  58. test_entryconn_rewrite_bad_dotexit(void *arg)
  59. {
  60. entry_connection_t *ec = arg;
  61. rewrite_result_t rr;
  62. get_options_mutable()->AllowDotExit = 0;
  63. tt_assert(ec->socks_request);
  64. strlcpy(ec->socks_request->address, "www.TORproject.org.foo.exit",
  65. sizeof(ec->socks_request->address));
  66. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  67. connection_ap_handshake_rewrite(ec, &rr);
  68. tt_int_op(rr.should_close, OP_EQ, 1);
  69. tt_int_op(rr.end_reason, OP_EQ, END_STREAM_REASON_TORPROTOCOL);
  70. done:
  71. ;
  72. }
  73. /* Automap on resolve, connect to automapped address, resolve again and get
  74. * same answer. (IPv4) */
  75. static void
  76. test_entryconn_rewrite_automap_ipv4(void *arg)
  77. {
  78. entry_connection_t *ec = arg;
  79. entry_connection_t *ec2=NULL, *ec3=NULL;
  80. rewrite_result_t rr;
  81. char *msg = NULL;
  82. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  83. ec3 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  84. get_options_mutable()->AutomapHostsOnResolve = 1;
  85. smartlist_add_strdup(get_options_mutable()->AutomapHostsSuffixes, ".");
  86. parse_virtual_addr_network("127.202.0.0/16", AF_INET, 0, &msg);
  87. /* Automap this on resolve. */
  88. strlcpy(ec->socks_request->address, "WWW.MIT.EDU",
  89. sizeof(ec->socks_request->address));
  90. ec->socks_request->command = SOCKS_COMMAND_RESOLVE;
  91. connection_ap_handshake_rewrite(ec, &rr);
  92. tt_int_op(rr.automap, OP_EQ, 1);
  93. tt_int_op(rr.should_close, OP_EQ, 0);
  94. tt_int_op(rr.end_reason, OP_EQ, 0);
  95. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  96. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  97. tt_str_op(rr.orig_address, OP_EQ, "www.mit.edu");
  98. tt_str_op(ec->original_dest_address, OP_EQ, "www.mit.edu");
  99. tt_assert(!strcmpstart(ec->socks_request->address,"127.202."));
  100. /* Connect to it and make sure we get the original address back. */
  101. strlcpy(ec2->socks_request->address, ec->socks_request->address,
  102. sizeof(ec2->socks_request->address));
  103. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  104. connection_ap_handshake_rewrite(ec2, &rr);
  105. tt_int_op(rr.automap, OP_EQ, 0);
  106. tt_int_op(rr.should_close, OP_EQ, 0);
  107. tt_int_op(rr.end_reason, OP_EQ, 0);
  108. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  109. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  110. tt_str_op(rr.orig_address, OP_EQ, ec->socks_request->address);
  111. tt_str_op(ec2->original_dest_address, OP_EQ, ec->socks_request->address);
  112. tt_str_op(ec2->socks_request->address, OP_EQ, "www.mit.edu");
  113. /* Resolve it again, make sure the answer is the same. */
  114. strlcpy(ec3->socks_request->address, "www.MIT.EDU",
  115. sizeof(ec3->socks_request->address));
  116. ec3->socks_request->command = SOCKS_COMMAND_RESOLVE;
  117. connection_ap_handshake_rewrite(ec3, &rr);
  118. tt_int_op(rr.automap, OP_EQ, 1);
  119. tt_int_op(rr.should_close, OP_EQ, 0);
  120. tt_int_op(rr.end_reason, OP_EQ, 0);
  121. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  122. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  123. tt_str_op(rr.orig_address, OP_EQ, "www.mit.edu");
  124. tt_str_op(ec3->original_dest_address, OP_EQ, "www.mit.edu");
  125. tt_str_op(ec3->socks_request->address, OP_EQ,
  126. ec->socks_request->address);
  127. done:
  128. connection_free_(ENTRY_TO_CONN(ec2));
  129. connection_free_(ENTRY_TO_CONN(ec3));
  130. }
  131. /* Automap on resolve, connect to automapped address, resolve again and get
  132. * same answer. (IPv6) */
  133. static void
  134. test_entryconn_rewrite_automap_ipv6(void *arg)
  135. {
  136. (void)arg;
  137. entry_connection_t *ec =NULL;
  138. entry_connection_t *ec2=NULL, *ec3=NULL;
  139. rewrite_result_t rr;
  140. char *msg = NULL;
  141. ec = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  142. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  143. ec3 = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  144. get_options_mutable()->AutomapHostsOnResolve = 1;
  145. smartlist_add_strdup(get_options_mutable()->AutomapHostsSuffixes, ".");
  146. parse_virtual_addr_network("FE80::/32", AF_INET6, 0, &msg);
  147. /* Automap this on resolve. */
  148. strlcpy(ec->socks_request->address, "WWW.MIT.EDU",
  149. sizeof(ec->socks_request->address));
  150. ec->socks_request->command = SOCKS_COMMAND_RESOLVE;
  151. connection_ap_handshake_rewrite(ec, &rr);
  152. tt_int_op(rr.automap, OP_EQ, 1);
  153. tt_int_op(rr.should_close, OP_EQ, 0);
  154. tt_int_op(rr.end_reason, OP_EQ, 0);
  155. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  156. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  157. tt_str_op(rr.orig_address, OP_EQ, "www.mit.edu");
  158. tt_str_op(ec->original_dest_address, OP_EQ, "www.mit.edu");
  159. /* Yes, this [ should be here. */
  160. tt_assert(!strcmpstart(ec->socks_request->address,"[fe80:"));
  161. /* Connect to it and make sure we get the original address back. */
  162. strlcpy(ec2->socks_request->address, ec->socks_request->address,
  163. sizeof(ec2->socks_request->address));
  164. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  165. connection_ap_handshake_rewrite(ec2, &rr);
  166. tt_int_op(rr.automap, OP_EQ, 0);
  167. tt_int_op(rr.should_close, OP_EQ, 0);
  168. tt_int_op(rr.end_reason, OP_EQ, 0);
  169. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  170. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  171. tt_str_op(rr.orig_address, OP_EQ, ec->socks_request->address);
  172. tt_str_op(ec2->original_dest_address, OP_EQ, ec->socks_request->address);
  173. tt_str_op(ec2->socks_request->address, OP_EQ, "www.mit.edu");
  174. /* Resolve it again, make sure the answer is the same. */
  175. strlcpy(ec3->socks_request->address, "www.MIT.EDU",
  176. sizeof(ec3->socks_request->address));
  177. ec3->socks_request->command = SOCKS_COMMAND_RESOLVE;
  178. connection_ap_handshake_rewrite(ec3, &rr);
  179. tt_int_op(rr.automap, OP_EQ, 1);
  180. tt_int_op(rr.should_close, OP_EQ, 0);
  181. tt_int_op(rr.end_reason, OP_EQ, 0);
  182. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  183. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  184. tt_str_op(rr.orig_address, OP_EQ, "www.mit.edu");
  185. tt_str_op(ec3->original_dest_address, OP_EQ, "www.mit.edu");
  186. tt_str_op(ec3->socks_request->address, OP_EQ,
  187. ec->socks_request->address);
  188. done:
  189. connection_free_(ENTRY_TO_CONN(ec));
  190. connection_free_(ENTRY_TO_CONN(ec2));
  191. connection_free_(ENTRY_TO_CONN(ec3));
  192. }
  193. #if 0
  194. /* FFFF not actually supported. */
  195. /* automap on resolve, reverse lookup. */
  196. static void
  197. test_entryconn_rewrite_automap_reverse(void *arg)
  198. {
  199. entry_connection_t *ec = arg;
  200. entry_connection_t *ec2=NULL;
  201. rewrite_result_t rr;
  202. char *msg = NULL;
  203. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  204. get_options_mutable()->AutomapHostsOnResolve = 1;
  205. get_options_mutable()->SafeLogging_ = SAFELOG_SCRUB_NONE;
  206. smartlist_add(get_options_mutable()->AutomapHostsSuffixes,
  207. tor_strdup(".bloom"));
  208. parse_virtual_addr_network("127.80.0.0/16", AF_INET, 0, &msg);
  209. /* Automap this on resolve. */
  210. strlcpy(ec->socks_request->address, "www.poldy.BLOOM",
  211. sizeof(ec->socks_request->address));
  212. ec->socks_request->command = SOCKS_COMMAND_RESOLVE;
  213. connection_ap_handshake_rewrite(ec, &rr);
  214. tt_int_op(rr.automap, OP_EQ, 1);
  215. tt_int_op(rr.should_close, OP_EQ, 0);
  216. tt_int_op(rr.end_reason, OP_EQ, 0);
  217. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  218. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  219. tt_str_op(rr.orig_address, OP_EQ, "www.poldy.bloom");
  220. tt_str_op(ec->original_dest_address, OP_EQ, "www.poldy.bloom");
  221. tt_assert(!strcmpstart(ec->socks_request->address,"127.80."));
  222. strlcpy(ec2->socks_request->address, ec->socks_request->address,
  223. sizeof(ec2->socks_request->address));
  224. ec2->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  225. connection_ap_handshake_rewrite(ec2, &rr);
  226. tt_int_op(rr.automap, OP_EQ, 0);
  227. tt_int_op(rr.should_close, OP_EQ, 1);
  228. tt_int_op(rr.end_reason, OP_EQ,
  229. END_STREAM_REASON_DONE|END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  230. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  231. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  232. done:
  233. connection_free_(ENTRY_TO_CONN(ec2));
  234. }
  235. #endif
  236. /* Rewrite because of cached DNS entry. */
  237. static void
  238. test_entryconn_rewrite_cached_dns_ipv4(void *arg)
  239. {
  240. entry_connection_t *ec = arg;
  241. rewrite_result_t rr;
  242. time_t expires = time(NULL) + 3600;
  243. entry_connection_t *ec2=NULL;
  244. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  245. addressmap_register("www.friendly.example.com",
  246. tor_strdup("240.240.241.241"),
  247. expires,
  248. ADDRMAPSRC_DNS,
  249. 0, 0);
  250. strlcpy(ec->socks_request->address, "www.friendly.example.com",
  251. sizeof(ec->socks_request->address));
  252. strlcpy(ec2->socks_request->address, "www.friendly.example.com",
  253. sizeof(ec2->socks_request->address));
  254. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  255. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  256. ec2->entry_cfg.use_cached_ipv4_answers = 1; /* only ec2 gets this flag */
  257. connection_ap_handshake_rewrite(ec, &rr);
  258. tt_int_op(rr.automap, OP_EQ, 0);
  259. tt_int_op(rr.should_close, OP_EQ, 0);
  260. tt_int_op(rr.end_reason, OP_EQ, 0);
  261. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  262. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  263. tt_str_op(rr.orig_address, OP_EQ, "www.friendly.example.com");
  264. tt_str_op(ec->socks_request->address, OP_EQ, "www.friendly.example.com");
  265. connection_ap_handshake_rewrite(ec2, &rr);
  266. tt_int_op(rr.automap, OP_EQ, 0);
  267. tt_int_op(rr.should_close, OP_EQ, 0);
  268. tt_int_op(rr.end_reason, OP_EQ, 0);
  269. tt_i64_op(rr.map_expires, OP_EQ, expires);
  270. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  271. tt_str_op(rr.orig_address, OP_EQ, "www.friendly.example.com");
  272. tt_str_op(ec2->socks_request->address, OP_EQ, "240.240.241.241");
  273. done:
  274. connection_free_(ENTRY_TO_CONN(ec2));
  275. }
  276. /* Rewrite because of cached DNS entry. */
  277. static void
  278. test_entryconn_rewrite_cached_dns_ipv6(void *arg)
  279. {
  280. entry_connection_t *ec = NULL;
  281. rewrite_result_t rr;
  282. time_t expires = time(NULL) + 3600;
  283. entry_connection_t *ec2=NULL;
  284. (void)arg;
  285. ec = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  286. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  287. addressmap_register("www.friendly.example.com",
  288. tor_strdup("[::f00f]"),
  289. expires,
  290. ADDRMAPSRC_DNS,
  291. 0, 0);
  292. strlcpy(ec->socks_request->address, "www.friendly.example.com",
  293. sizeof(ec->socks_request->address));
  294. strlcpy(ec2->socks_request->address, "www.friendly.example.com",
  295. sizeof(ec2->socks_request->address));
  296. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  297. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  298. ec2->entry_cfg.use_cached_ipv6_answers = 1; /* only ec2 gets this flag */
  299. connection_ap_handshake_rewrite(ec, &rr);
  300. tt_int_op(rr.automap, OP_EQ, 0);
  301. tt_int_op(rr.should_close, OP_EQ, 0);
  302. tt_int_op(rr.end_reason, OP_EQ, 0);
  303. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  304. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  305. tt_str_op(rr.orig_address, OP_EQ, "www.friendly.example.com");
  306. tt_str_op(ec->socks_request->address, OP_EQ, "www.friendly.example.com");
  307. connection_ap_handshake_rewrite(ec2, &rr);
  308. tt_int_op(rr.automap, OP_EQ, 0);
  309. tt_int_op(rr.should_close, OP_EQ, 0);
  310. tt_int_op(rr.end_reason, OP_EQ, 0);
  311. tt_i64_op(rr.map_expires, OP_EQ, expires);
  312. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  313. tt_str_op(rr.orig_address, OP_EQ, "www.friendly.example.com");
  314. tt_str_op(ec2->socks_request->address, OP_EQ, "[::f00f]");
  315. done:
  316. connection_free_(ENTRY_TO_CONN(ec));
  317. connection_free_(ENTRY_TO_CONN(ec2));
  318. }
  319. /* Fail to connect to unmapped address in virtual range. */
  320. static void
  321. test_entryconn_rewrite_unmapped_virtual(void *arg)
  322. {
  323. entry_connection_t *ec = arg;
  324. rewrite_result_t rr;
  325. entry_connection_t *ec2 = NULL;
  326. char *msg = NULL;
  327. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  328. parse_virtual_addr_network("18.202.0.0/16", AF_INET, 0, &msg);
  329. parse_virtual_addr_network("[ABCD::]/16", AF_INET6, 0, &msg);
  330. strlcpy(ec->socks_request->address, "18.202.5.5",
  331. sizeof(ec->socks_request->address));
  332. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  333. connection_ap_handshake_rewrite(ec, &rr);
  334. tt_int_op(rr.should_close, OP_EQ, 1);
  335. tt_int_op(rr.end_reason, OP_EQ, END_STREAM_REASON_INTERNAL);
  336. tt_int_op(rr.automap, OP_EQ, 0);
  337. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  338. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  339. strlcpy(ec2->socks_request->address, "[ABCD:9::5314:9543]",
  340. sizeof(ec2->socks_request->address));
  341. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  342. connection_ap_handshake_rewrite(ec2, &rr);
  343. tt_int_op(rr.should_close, OP_EQ, 1);
  344. tt_int_op(rr.end_reason, OP_EQ, END_STREAM_REASON_INTERNAL);
  345. tt_int_op(rr.automap, OP_EQ, 0);
  346. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  347. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  348. done:
  349. connection_free_(ENTRY_TO_CONN(ec2));
  350. }
  351. /* Rewrite because of mapaddress option */
  352. static void
  353. test_entryconn_rewrite_mapaddress(void *arg)
  354. {
  355. entry_connection_t *ec = arg;
  356. rewrite_result_t rr;
  357. config_line_append(&get_options_mutable()->AddressMap,
  358. "MapAddress", "meta metaobjects.example");
  359. config_register_addressmaps(get_options());
  360. strlcpy(ec->socks_request->address, "meta",
  361. sizeof(ec->socks_request->address));
  362. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  363. connection_ap_handshake_rewrite(ec, &rr);
  364. tt_int_op(rr.should_close, OP_EQ, 0);
  365. tt_int_op(rr.end_reason, OP_EQ, 0);
  366. tt_int_op(rr.automap, OP_EQ, 0);
  367. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  368. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  369. tt_str_op(ec->socks_request->address, OP_EQ, "metaobjects.example");
  370. done:
  371. ;
  372. }
  373. /* Reject reverse lookups of internal address. */
  374. static void
  375. test_entryconn_rewrite_reject_internal_reverse(void *arg)
  376. {
  377. entry_connection_t *ec = arg;
  378. rewrite_result_t rr;
  379. strlcpy(ec->socks_request->address, "10.0.0.1",
  380. sizeof(ec->socks_request->address));
  381. ec->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  382. connection_ap_handshake_rewrite(ec, &rr);
  383. tt_int_op(rr.should_close, OP_EQ, 1);
  384. tt_int_op(rr.end_reason, OP_EQ, END_STREAM_REASON_SOCKSPROTOCOL |
  385. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  386. tt_int_op(rr.automap, OP_EQ, 0);
  387. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  388. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  389. done:
  390. ;
  391. }
  392. /* Rewrite into .exit because of virtual address mapping */
  393. static void
  394. test_entryconn_rewrite_automap_exit(void *arg)
  395. {
  396. entry_connection_t *ec = arg;
  397. entry_connection_t *ec2=NULL;
  398. rewrite_result_t rr;
  399. char *msg = NULL;
  400. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  401. get_options_mutable()->AutomapHostsOnResolve = 1;
  402. get_options_mutable()->AllowDotExit = 1;
  403. smartlist_add_strdup(get_options_mutable()->AutomapHostsSuffixes,
  404. ".EXIT");
  405. parse_virtual_addr_network("127.1.0.0/16", AF_INET, 0, &msg);
  406. /* Automap this on resolve. */
  407. strlcpy(ec->socks_request->address, "website.example.exit",
  408. sizeof(ec->socks_request->address));
  409. ec->socks_request->command = SOCKS_COMMAND_RESOLVE;
  410. connection_ap_handshake_rewrite(ec, &rr);
  411. tt_int_op(rr.automap, OP_EQ, 1);
  412. tt_int_op(rr.should_close, OP_EQ, 0);
  413. tt_int_op(rr.end_reason, OP_EQ, 0);
  414. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  415. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  416. tt_str_op(rr.orig_address, OP_EQ, "website.example.exit");
  417. tt_str_op(ec->original_dest_address, OP_EQ, "website.example.exit");
  418. tt_assert(!strcmpstart(ec->socks_request->address,"127.1."));
  419. /* Connect to it and make sure we get the original address back. */
  420. strlcpy(ec2->socks_request->address, ec->socks_request->address,
  421. sizeof(ec2->socks_request->address));
  422. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  423. connection_ap_handshake_rewrite(ec2, &rr);
  424. tt_int_op(rr.automap, OP_EQ, 0);
  425. tt_int_op(rr.should_close, OP_EQ, 0);
  426. tt_int_op(rr.end_reason, OP_EQ, 0);
  427. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  428. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_AUTOMAP);
  429. tt_str_op(rr.orig_address, OP_EQ, ec->socks_request->address);
  430. tt_str_op(ec2->original_dest_address, OP_EQ, ec->socks_request->address);
  431. tt_str_op(ec2->socks_request->address, OP_EQ, "website.example.exit");
  432. done:
  433. connection_free_(ENTRY_TO_CONN(ec2));
  434. }
  435. /* Rewrite into .exit because of mapaddress */
  436. static void
  437. test_entryconn_rewrite_mapaddress_exit(void *arg)
  438. {
  439. entry_connection_t *ec = arg;
  440. rewrite_result_t rr;
  441. config_line_append(&get_options_mutable()->AddressMap,
  442. "MapAddress", "*.example.com *.example.com.abc.exit");
  443. config_register_addressmaps(get_options());
  444. /* Automap this on resolve. */
  445. strlcpy(ec->socks_request->address, "abc.example.com",
  446. sizeof(ec->socks_request->address));
  447. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  448. connection_ap_handshake_rewrite(ec, &rr);
  449. tt_int_op(rr.automap, OP_EQ, 0);
  450. tt_int_op(rr.should_close, OP_EQ, 0);
  451. tt_int_op(rr.end_reason, OP_EQ, 0);
  452. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  453. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_TORRC);
  454. tt_str_op(rr.orig_address, OP_EQ, "abc.example.com");
  455. tt_str_op(ec->socks_request->address, OP_EQ, "abc.example.com.abc.exit");
  456. done:
  457. ;
  458. }
  459. /* Map foo.onion to longthing.onion, and also automap. */
  460. static void
  461. test_entryconn_rewrite_mapaddress_automap_onion(void *arg)
  462. {
  463. entry_connection_t *ec = arg;
  464. entry_connection_t *ec2 = NULL;
  465. entry_connection_t *ec3 = NULL;
  466. entry_connection_t *ec4 = NULL;
  467. rewrite_result_t rr;
  468. char *msg = NULL;
  469. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  470. ec3 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  471. ec4 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  472. get_options_mutable()->AutomapHostsOnResolve = 1;
  473. get_options_mutable()->AllowDotExit = 1;
  474. smartlist_add_strdup(get_options_mutable()->AutomapHostsSuffixes,
  475. ".onion");
  476. parse_virtual_addr_network("192.168.0.0/16", AF_INET, 0, &msg);
  477. config_line_append(&get_options_mutable()->AddressMap,
  478. "MapAddress", "foo.onion abcdefghijklmnop.onion");
  479. config_register_addressmaps(get_options());
  480. /* Connect to foo.onion. */
  481. strlcpy(ec->socks_request->address, "foo.onion",
  482. sizeof(ec->socks_request->address));
  483. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  484. connection_ap_handshake_rewrite(ec, &rr);
  485. tt_int_op(rr.automap, OP_EQ, 0);
  486. tt_int_op(rr.should_close, OP_EQ, 0);
  487. tt_int_op(rr.end_reason, OP_EQ, 0);
  488. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  489. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  490. tt_str_op(rr.orig_address, OP_EQ, "foo.onion");
  491. tt_str_op(ec->socks_request->address, OP_EQ, "abcdefghijklmnop.onion");
  492. /* Okay, resolve foo.onion */
  493. strlcpy(ec2->socks_request->address, "foo.onion",
  494. sizeof(ec2->socks_request->address));
  495. ec2->socks_request->command = SOCKS_COMMAND_RESOLVE;
  496. connection_ap_handshake_rewrite(ec2, &rr);
  497. tt_int_op(rr.automap, OP_EQ, 1);
  498. tt_int_op(rr.should_close, OP_EQ, 0);
  499. tt_int_op(rr.end_reason, OP_EQ, 0);
  500. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  501. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  502. tt_str_op(rr.orig_address, OP_EQ, "foo.onion");
  503. tt_assert(!strcmpstart(ec2->socks_request->address, "192.168."));
  504. /* Now connect */
  505. strlcpy(ec3->socks_request->address, ec2->socks_request->address,
  506. sizeof(ec3->socks_request->address));
  507. ec3->socks_request->command = SOCKS_COMMAND_CONNECT;
  508. connection_ap_handshake_rewrite(ec3, &rr);
  509. tt_int_op(rr.automap, OP_EQ, 0);
  510. tt_int_op(rr.should_close, OP_EQ, 0);
  511. tt_int_op(rr.end_reason, OP_EQ, 0);
  512. tt_assert(!strcmpstart(ec3->socks_request->address,
  513. "abcdefghijklmnop.onion"));
  514. /* Now resolve abcefghijklmnop.onion. */
  515. strlcpy(ec4->socks_request->address, "abcdefghijklmnop.onion",
  516. sizeof(ec4->socks_request->address));
  517. ec4->socks_request->command = SOCKS_COMMAND_RESOLVE;
  518. connection_ap_handshake_rewrite(ec4, &rr);
  519. tt_int_op(rr.automap, OP_EQ, 1);
  520. tt_int_op(rr.should_close, OP_EQ, 0);
  521. tt_int_op(rr.end_reason, OP_EQ, 0);
  522. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  523. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  524. tt_str_op(rr.orig_address, OP_EQ, "abcdefghijklmnop.onion");
  525. tt_assert(!strcmpstart(ec4->socks_request->address, "192.168."));
  526. /* XXXX doesn't work
  527. tt_str_op(ec4->socks_request->address, OP_EQ, ec2->socks_request->address);
  528. */
  529. done:
  530. connection_free_(ENTRY_TO_CONN(ec2));
  531. connection_free_(ENTRY_TO_CONN(ec3));
  532. connection_free_(ENTRY_TO_CONN(ec4));
  533. }
  534. static void
  535. test_entryconn_rewrite_mapaddress_automap_onion_common(entry_connection_t *ec,
  536. int map_to_onion,
  537. int map_to_address)
  538. {
  539. entry_connection_t *ec2 = NULL;
  540. entry_connection_t *ec3 = NULL;
  541. rewrite_result_t rr;
  542. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  543. ec3 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  544. /* Connect to irc.example.com */
  545. strlcpy(ec->socks_request->address, "irc.example.com",
  546. sizeof(ec->socks_request->address));
  547. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  548. connection_ap_handshake_rewrite(ec, &rr);
  549. tt_int_op(rr.automap, OP_EQ, 0);
  550. tt_int_op(rr.should_close, OP_EQ, 0);
  551. tt_int_op(rr.end_reason, OP_EQ, 0);
  552. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  553. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  554. tt_str_op(rr.orig_address, OP_EQ, "irc.example.com");
  555. tt_str_op(ec->socks_request->address, OP_EQ,
  556. map_to_onion ? "abcdefghijklmnop.onion" : "irc.example.com");
  557. /* Okay, resolve irc.example.com */
  558. strlcpy(ec2->socks_request->address, "irc.example.com",
  559. sizeof(ec2->socks_request->address));
  560. ec2->socks_request->command = SOCKS_COMMAND_RESOLVE;
  561. connection_ap_handshake_rewrite(ec2, &rr);
  562. tt_int_op(rr.automap, OP_EQ, map_to_onion && map_to_address);
  563. tt_int_op(rr.should_close, OP_EQ, 0);
  564. tt_int_op(rr.end_reason, OP_EQ, 0);
  565. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  566. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  567. tt_str_op(rr.orig_address, OP_EQ, "irc.example.com");
  568. if (map_to_onion && map_to_address)
  569. tt_assert(!strcmpstart(ec2->socks_request->address, "192.168."));
  570. /* Now connect */
  571. strlcpy(ec3->socks_request->address, ec2->socks_request->address,
  572. sizeof(ec3->socks_request->address));
  573. ec3->socks_request->command = SOCKS_COMMAND_CONNECT;
  574. connection_ap_handshake_rewrite(ec3, &rr);
  575. tt_int_op(rr.automap, OP_EQ, 0);
  576. tt_int_op(rr.should_close, OP_EQ, 0);
  577. tt_int_op(rr.end_reason, OP_EQ, 0);
  578. if (map_to_onion)
  579. tt_assert(!strcmpstart(ec3->socks_request->address,
  580. "abcdefghijklmnop.onion"));
  581. done:
  582. connection_free_(ENTRY_TO_CONN(ec2));
  583. connection_free_(ENTRY_TO_CONN(ec3));
  584. }
  585. /* This time is the same, but we start with a mapping from a non-onion
  586. * address. */
  587. static void
  588. test_entryconn_rewrite_mapaddress_automap_onion2(void *arg)
  589. {
  590. char *msg = NULL;
  591. get_options_mutable()->AutomapHostsOnResolve = 1;
  592. smartlist_add_strdup(get_options_mutable()->AutomapHostsSuffixes,
  593. ".onion");
  594. parse_virtual_addr_network("192.168.0.0/16", AF_INET, 0, &msg);
  595. config_line_append(&get_options_mutable()->AddressMap,
  596. "MapAddress", "irc.example.com abcdefghijklmnop.onion");
  597. config_register_addressmaps(get_options());
  598. test_entryconn_rewrite_mapaddress_automap_onion_common(arg, 1, 1);
  599. }
  600. /* Same as above, with automapped turned off */
  601. static void
  602. test_entryconn_rewrite_mapaddress_automap_onion3(void *arg)
  603. {
  604. config_line_append(&get_options_mutable()->AddressMap,
  605. "MapAddress", "irc.example.com abcdefghijklmnop.onion");
  606. config_register_addressmaps(get_options());
  607. test_entryconn_rewrite_mapaddress_automap_onion_common(arg, 1, 0);
  608. }
  609. /* As above, with no mapping. */
  610. static void
  611. test_entryconn_rewrite_mapaddress_automap_onion4(void *arg)
  612. {
  613. char *msg = NULL;
  614. get_options_mutable()->AutomapHostsOnResolve = 1;
  615. smartlist_add_strdup(get_options_mutable()->AutomapHostsSuffixes,
  616. ".onion");
  617. parse_virtual_addr_network("192.168.0.0/16", AF_INET, 0, &msg);
  618. test_entryconn_rewrite_mapaddress_automap_onion_common(arg, 0, 1);
  619. }
  620. #define REWRITE(name) \
  621. { #name, test_entryconn_##name, TT_FORK, &test_rewrite_setup, NULL }
  622. struct testcase_t entryconn_tests[] = {
  623. REWRITE(rewrite_basic),
  624. REWRITE(rewrite_bad_dotexit),
  625. REWRITE(rewrite_automap_ipv4),
  626. REWRITE(rewrite_automap_ipv6),
  627. // REWRITE(rewrite_automap_reverse),
  628. REWRITE(rewrite_cached_dns_ipv4),
  629. REWRITE(rewrite_cached_dns_ipv6),
  630. REWRITE(rewrite_unmapped_virtual),
  631. REWRITE(rewrite_mapaddress),
  632. REWRITE(rewrite_reject_internal_reverse),
  633. REWRITE(rewrite_automap_exit),
  634. REWRITE(rewrite_mapaddress_exit),
  635. REWRITE(rewrite_mapaddress_automap_onion),
  636. REWRITE(rewrite_mapaddress_automap_onion2),
  637. REWRITE(rewrite_mapaddress_automap_onion3),
  638. REWRITE(rewrite_mapaddress_automap_onion4),
  639. END_OF_TESTCASES
  640. };