test_entryconn.c 28 KB

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