dnsserv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* Copyright (c) 2007-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file dnsserv.c
  5. * \brief Implements client-side DNS proxy server code.
  6. *
  7. * When a user enables the DNSPort configuration option to have their local
  8. * Tor client handle DNS requests, this module handles it. It functions as a
  9. * "DNS Server" on the client side, which client applications use.
  10. *
  11. * Inbound DNS requests are represented as entry_connection_t here (since
  12. * that's how Tor represents client-side streams), which are kept associated
  13. * with an evdns_server_request structure as exposed by Libevent's
  14. * evdns code.
  15. *
  16. * Upon receiving a DNS request, libevent calls our evdns_server_callback()
  17. * function here, which causes this module to create an entry_connection_t
  18. * request as appropriate. Later, when that request is answered,
  19. * connection_edge.c calls dnsserv_resolved() so we can finish up and tell the
  20. * DNS client.
  21. **/
  22. #include "core/or/or.h"
  23. #include "feature/client/dnsserv.h"
  24. #include "app/config/config.h"
  25. #include "core/mainloop/connection.h"
  26. #include "core/or/connection_edge.h"
  27. #include "feature/control/control.h"
  28. #include "core/mainloop/mainloop.h"
  29. #include "core/or/policies.h"
  30. #include "feature/control/control_connection_st.h"
  31. #include "core/or/entry_connection_st.h"
  32. #include "core/or/listener_connection_st.h"
  33. #include "core/or/socks_request_st.h"
  34. #include "lib/evloop/compat_libevent.h"
  35. #include <event2/dns.h>
  36. #include <event2/dns_compat.h>
  37. /* XXXX this implies we want an improved evdns */
  38. #include <event2/dns_struct.h>
  39. /** Helper function: called by evdns whenever the client sends a request to our
  40. * DNSPort. We need to eventually answer the request <b>req</b>.
  41. */
  42. static void
  43. evdns_server_callback(struct evdns_server_request *req, void *data_)
  44. {
  45. const listener_connection_t *listener = data_;
  46. entry_connection_t *entry_conn;
  47. edge_connection_t *conn;
  48. int i = 0;
  49. struct evdns_server_question *q = NULL, *supported_q = NULL;
  50. struct sockaddr_storage addr;
  51. struct sockaddr *sa;
  52. int addrlen;
  53. tor_addr_t tor_addr;
  54. uint16_t port;
  55. int err = DNS_ERR_NONE;
  56. char *q_name;
  57. tor_assert(req);
  58. log_info(LD_APP, "Got a new DNS request!");
  59. req->flags |= 0x80; /* set RA */
  60. /* First, check whether the requesting address matches our SOCKSPolicy. */
  61. if ((addrlen = evdns_server_request_get_requesting_addr(req,
  62. (struct sockaddr*)&addr, (socklen_t)sizeof(addr))) < 0) {
  63. log_warn(LD_APP, "Couldn't get requesting address.");
  64. evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
  65. return;
  66. }
  67. (void) addrlen;
  68. sa = (struct sockaddr*) &addr;
  69. if (tor_addr_from_sockaddr(&tor_addr, sa, &port)<0) {
  70. log_warn(LD_APP, "Requesting address wasn't recognized.");
  71. evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
  72. return;
  73. }
  74. if (!socks_policy_permits_address(&tor_addr)) {
  75. log_warn(LD_APP, "Rejecting DNS request from disallowed IP.");
  76. evdns_server_request_respond(req, DNS_ERR_REFUSED);
  77. return;
  78. }
  79. /* Now, let's find the first actual question of a type we can answer in this
  80. * DNS request. It makes us a little noncompliant to act like this; we
  81. * should fix that eventually if it turns out to make a difference for
  82. * anybody. */
  83. if (req->nquestions == 0) {
  84. log_info(LD_APP, "No questions in DNS request; sending back nil reply.");
  85. evdns_server_request_respond(req, 0);
  86. return;
  87. }
  88. if (req->nquestions > 1) {
  89. log_info(LD_APP, "Got a DNS request with more than one question; I only "
  90. "handle one question at a time for now. Skipping the extras.");
  91. }
  92. for (i = 0; i < req->nquestions; ++i) {
  93. if (req->questions[i]->dns_question_class != EVDNS_CLASS_INET)
  94. continue;
  95. switch (req->questions[i]->type) {
  96. case EVDNS_TYPE_A:
  97. case EVDNS_TYPE_AAAA:
  98. case EVDNS_TYPE_PTR:
  99. /* We always pick the first one of these questions, if there is
  100. one. */
  101. if (! supported_q)
  102. supported_q = req->questions[i];
  103. break;
  104. default:
  105. break;
  106. }
  107. }
  108. if (supported_q)
  109. q = supported_q;
  110. if (!q) {
  111. log_info(LD_APP, "None of the questions we got were ones we're willing "
  112. "to support. Sending NOTIMPL.");
  113. evdns_server_request_respond(req, DNS_ERR_NOTIMPL);
  114. return;
  115. }
  116. /* Make sure the name isn't too long: This should be impossible, I think. */
  117. if (err == DNS_ERR_NONE && strlen(q->name) > MAX_SOCKS_ADDR_LEN-1)
  118. err = DNS_ERR_FORMAT;
  119. if (err != DNS_ERR_NONE || !supported_q) {
  120. /* We got an error? There's no question we're willing to answer? Then
  121. * send back an answer immediately; we're done. */
  122. evdns_server_request_respond(req, err);
  123. return;
  124. }
  125. /* Make a new dummy AP connection, and attach the request to it. */
  126. entry_conn = entry_connection_new(CONN_TYPE_AP, AF_INET);
  127. conn = ENTRY_TO_EDGE_CONN(entry_conn);
  128. CONNECTION_AP_EXPECT_NONPENDING(entry_conn);
  129. TO_CONN(conn)->state = AP_CONN_STATE_RESOLVE_WAIT;
  130. conn->is_dns_request = 1;
  131. tor_addr_copy(&TO_CONN(conn)->addr, &tor_addr);
  132. TO_CONN(conn)->port = port;
  133. TO_CONN(conn)->address = tor_addr_to_str_dup(&tor_addr);
  134. if (q->type == EVDNS_TYPE_A || q->type == EVDNS_TYPE_AAAA ||
  135. q->type == EVDNS_QTYPE_ALL) {
  136. entry_conn->socks_request->command = SOCKS_COMMAND_RESOLVE;
  137. } else {
  138. tor_assert(q->type == EVDNS_TYPE_PTR);
  139. entry_conn->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  140. }
  141. /* This serves our DNS port so enable DNS request by default. */
  142. entry_conn->entry_cfg.dns_request = 1;
  143. if (q->type == EVDNS_TYPE_A || q->type == EVDNS_QTYPE_ALL) {
  144. entry_conn->entry_cfg.ipv4_traffic = 1;
  145. entry_conn->entry_cfg.ipv6_traffic = 0;
  146. entry_conn->entry_cfg.prefer_ipv6 = 0;
  147. } else if (q->type == EVDNS_TYPE_AAAA) {
  148. entry_conn->entry_cfg.ipv4_traffic = 0;
  149. entry_conn->entry_cfg.ipv6_traffic = 1;
  150. entry_conn->entry_cfg.prefer_ipv6 = 1;
  151. }
  152. strlcpy(entry_conn->socks_request->address, q->name,
  153. sizeof(entry_conn->socks_request->address));
  154. entry_conn->socks_request->listener_type = listener->base_.type;
  155. entry_conn->dns_server_request = req;
  156. entry_conn->entry_cfg.isolation_flags = listener->entry_cfg.isolation_flags;
  157. entry_conn->entry_cfg.session_group = listener->entry_cfg.session_group;
  158. entry_conn->nym_epoch = get_signewnym_epoch();
  159. if (connection_add(ENTRY_TO_CONN(entry_conn)) < 0) {
  160. log_warn(LD_APP, "Couldn't register dummy connection for DNS request");
  161. evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
  162. connection_free_(ENTRY_TO_CONN(entry_conn));
  163. return;
  164. }
  165. control_event_stream_status(entry_conn, STREAM_EVENT_NEW_RESOLVE, 0);
  166. /* Now, unless a controller asked us to leave streams unattached,
  167. * throw the connection over to get rewritten (which will
  168. * answer it immediately if it's in the cache, or completely bogus, or
  169. * automapped), and then attached to a circuit. */
  170. log_info(LD_APP, "Passing request for %s to rewrite_and_attach.",
  171. escaped_safe_str_client(q->name));
  172. q_name = tor_strdup(q->name); /* q could be freed in rewrite_and_attach */
  173. connection_ap_rewrite_and_attach_if_allowed(entry_conn, NULL, NULL);
  174. /* Now, the connection is marked if it was bad. */
  175. log_info(LD_APP, "Passed request for %s to rewrite_and_attach_if_allowed.",
  176. escaped_safe_str_client(q_name));
  177. tor_free(q_name);
  178. }
  179. /** Helper function: called whenever the client sends a resolve request to our
  180. * controller. We need to eventually answer the request <b>req</b>.
  181. * Returns 0 if the controller will be getting (or has gotten) an event in
  182. * response; -1 if we couldn't launch the request.
  183. */
  184. int
  185. dnsserv_launch_request(const char *name, int reverse,
  186. control_connection_t *control_conn)
  187. {
  188. entry_connection_t *entry_conn;
  189. edge_connection_t *conn;
  190. char *q_name;
  191. /* Make a new dummy AP connection, and attach the request to it. */
  192. entry_conn = entry_connection_new(CONN_TYPE_AP, AF_INET);
  193. entry_conn->entry_cfg.dns_request = 1;
  194. conn = ENTRY_TO_EDGE_CONN(entry_conn);
  195. CONNECTION_AP_EXPECT_NONPENDING(entry_conn);
  196. conn->base_.state = AP_CONN_STATE_RESOLVE_WAIT;
  197. tor_addr_copy(&TO_CONN(conn)->addr, &control_conn->base_.addr);
  198. #ifdef AF_UNIX
  199. /*
  200. * The control connection can be AF_UNIX and if so tor_addr_to_str_dup will
  201. * unhelpfully say "<unknown address type>"; say "(Tor_internal)"
  202. * instead.
  203. */
  204. if (control_conn->base_.socket_family == AF_UNIX) {
  205. TO_CONN(conn)->port = 0;
  206. TO_CONN(conn)->address = tor_strdup("(Tor_internal)");
  207. } else {
  208. TO_CONN(conn)->port = control_conn->base_.port;
  209. TO_CONN(conn)->address = tor_addr_to_str_dup(&control_conn->base_.addr);
  210. }
  211. #else /* !(defined(AF_UNIX)) */
  212. TO_CONN(conn)->port = control_conn->base_.port;
  213. TO_CONN(conn)->address = tor_addr_to_str_dup(&control_conn->base_.addr);
  214. #endif /* defined(AF_UNIX) */
  215. if (reverse)
  216. entry_conn->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  217. else
  218. entry_conn->socks_request->command = SOCKS_COMMAND_RESOLVE;
  219. conn->is_dns_request = 1;
  220. strlcpy(entry_conn->socks_request->address, name,
  221. sizeof(entry_conn->socks_request->address));
  222. entry_conn->socks_request->listener_type = CONN_TYPE_CONTROL_LISTENER;
  223. entry_conn->original_dest_address = tor_strdup(name);
  224. entry_conn->entry_cfg.session_group = SESSION_GROUP_CONTROL_RESOLVE;
  225. entry_conn->nym_epoch = get_signewnym_epoch();
  226. entry_conn->entry_cfg.isolation_flags = ISO_DEFAULT;
  227. if (connection_add(TO_CONN(conn))<0) {
  228. log_warn(LD_APP, "Couldn't register dummy connection for RESOLVE request");
  229. connection_free_(TO_CONN(conn));
  230. return -1;
  231. }
  232. control_event_stream_status(entry_conn, STREAM_EVENT_NEW_RESOLVE, 0);
  233. /* Now, unless a controller asked us to leave streams unattached,
  234. * throw the connection over to get rewritten (which will
  235. * answer it immediately if it's in the cache, or completely bogus, or
  236. * automapped), and then attached to a circuit. */
  237. log_info(LD_APP, "Passing request for %s to rewrite_and_attach.",
  238. escaped_safe_str_client(name));
  239. q_name = tor_strdup(name); /* q could be freed in rewrite_and_attach */
  240. connection_ap_rewrite_and_attach_if_allowed(entry_conn, NULL, NULL);
  241. /* Now, the connection is marked if it was bad. */
  242. log_info(LD_APP, "Passed request for %s to rewrite_and_attach_if_allowed.",
  243. escaped_safe_str_client(q_name));
  244. tor_free(q_name);
  245. return 0;
  246. }
  247. /** If there is a pending request on <b>conn</b> that's waiting for an answer,
  248. * send back an error and free the request. */
  249. void
  250. dnsserv_reject_request(entry_connection_t *conn)
  251. {
  252. if (conn->dns_server_request) {
  253. evdns_server_request_respond(conn->dns_server_request,
  254. DNS_ERR_SERVERFAILED);
  255. conn->dns_server_request = NULL;
  256. }
  257. }
  258. /** Look up the original name that corresponds to 'addr' in req. We use this
  259. * to preserve case in order to facilitate clients using 0x20-hacks to avoid
  260. * DNS poisoning. */
  261. static const char *
  262. evdns_get_orig_address(const struct evdns_server_request *req,
  263. int rtype, const char *addr)
  264. {
  265. int i, type;
  266. switch (rtype) {
  267. case RESOLVED_TYPE_IPV4:
  268. type = EVDNS_TYPE_A;
  269. break;
  270. case RESOLVED_TYPE_HOSTNAME:
  271. type = EVDNS_TYPE_PTR;
  272. break;
  273. case RESOLVED_TYPE_IPV6:
  274. type = EVDNS_TYPE_AAAA;
  275. break;
  276. case RESOLVED_TYPE_ERROR:
  277. case RESOLVED_TYPE_ERROR_TRANSIENT:
  278. /* Addr doesn't matter, since we're not sending it back in the reply.*/
  279. return addr;
  280. default:
  281. tor_fragile_assert();
  282. return addr;
  283. }
  284. for (i = 0; i < req->nquestions; ++i) {
  285. const struct evdns_server_question *q = req->questions[i];
  286. if (q->type == type && !strcasecmp(q->name, addr))
  287. return q->name;
  288. }
  289. return addr;
  290. }
  291. /** Tell the dns request waiting for an answer on <b>conn</b> that we have an
  292. * answer of type <b>answer_type</b> (RESOLVE_TYPE_IPV4/IPV6/ERR), of length
  293. * <b>answer_len</b>, in <b>answer</b>, with TTL <b>ttl</b>. Doesn't do
  294. * any caching; that's handled elsewhere. */
  295. void
  296. dnsserv_resolved(entry_connection_t *conn,
  297. int answer_type,
  298. size_t answer_len,
  299. const char *answer,
  300. int ttl)
  301. {
  302. struct evdns_server_request *req = conn->dns_server_request;
  303. const char *name;
  304. int err = DNS_ERR_NONE;
  305. if (!req)
  306. return;
  307. name = evdns_get_orig_address(req, answer_type,
  308. conn->socks_request->address);
  309. /* XXXX Re-do; this is dumb. */
  310. if (ttl < 60)
  311. ttl = 60;
  312. /* The evdns interface is: add a bunch of reply items (corresponding to one
  313. * or more of the questions in the request); then, call
  314. * evdns_server_request_respond. */
  315. if (answer_type == RESOLVED_TYPE_IPV6) {
  316. evdns_server_request_add_aaaa_reply(req,
  317. name,
  318. 1, answer, ttl);
  319. } else if (answer_type == RESOLVED_TYPE_IPV4 && answer_len == 4 &&
  320. conn->socks_request->command == SOCKS_COMMAND_RESOLVE) {
  321. evdns_server_request_add_a_reply(req,
  322. name,
  323. 1, answer, ttl);
  324. } else if (answer_type == RESOLVED_TYPE_HOSTNAME &&
  325. answer_len < 256 &&
  326. conn->socks_request->command == SOCKS_COMMAND_RESOLVE_PTR) {
  327. char *ans = tor_strndup(answer, answer_len);
  328. evdns_server_request_add_ptr_reply(req, NULL,
  329. name,
  330. ans, ttl);
  331. tor_free(ans);
  332. } else if (answer_type == RESOLVED_TYPE_ERROR) {
  333. err = DNS_ERR_NOTEXIST;
  334. } else { /* answer_type == RESOLVED_TYPE_ERROR_TRANSIENT */
  335. err = DNS_ERR_SERVERFAILED;
  336. }
  337. evdns_server_request_respond(req, err);
  338. conn->dns_server_request = NULL;
  339. }
  340. /** Set up the evdns server port for the UDP socket on <b>conn</b>, which
  341. * must be an AP_DNS_LISTENER */
  342. void
  343. dnsserv_configure_listener(connection_t *conn)
  344. {
  345. listener_connection_t *listener_conn;
  346. tor_assert(conn);
  347. tor_assert(SOCKET_OK(conn->s));
  348. tor_assert(conn->type == CONN_TYPE_AP_DNS_LISTENER);
  349. listener_conn = TO_LISTENER_CONN(conn);
  350. listener_conn->dns_server_port =
  351. tor_evdns_add_server_port(conn->s, 0, evdns_server_callback,
  352. listener_conn);
  353. }
  354. /** Free the evdns server port for <b>conn</b>, which must be an
  355. * AP_DNS_LISTENER. */
  356. void
  357. dnsserv_close_listener(connection_t *conn)
  358. {
  359. listener_connection_t *listener_conn;
  360. tor_assert(conn);
  361. tor_assert(conn->type == CONN_TYPE_AP_DNS_LISTENER);
  362. listener_conn = TO_LISTENER_CONN(conn);
  363. if (listener_conn->dns_server_port) {
  364. evdns_close_server_port(listener_conn->dns_server_port);
  365. listener_conn->dns_server_port = NULL;
  366. }
  367. }