dnsserv.c 12 KB

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