dnsserv.c 11 KB

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