test_entryconn.c 28 KB

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