test_entryconn.c 28 KB

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