dnsserv.c 10 KB

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