dnsserv.c 13 KB

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