dnsserv.c 14 KB

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