dnsserv.c 14 KB

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