dnsserv.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright 2007 Roger Dingledine, Nick Mathewson */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char dnsserv_c_id[] =
  5. "$Id$";
  6. /**
  7. * \file dnservs.c
  8. * \brief Implements client-side DNS proxy server code.
  9. **/
  10. #include "or.h"
  11. #include "eventdns.h"
  12. /* Helper function: called by evdns whenever the client sends a request to our
  13. * DNSPort. We need to eventually answer the request <b>req</b>.
  14. */
  15. static void
  16. evdns_server_callback(struct evdns_server_request *req, void *_data)
  17. {
  18. edge_connection_t *conn;
  19. int i = 0;
  20. struct evdns_server_question *q = NULL;
  21. struct sockaddr_storage addr;
  22. struct sockaddr *sa;
  23. int addrlen;
  24. uint32_t ipaddr;
  25. int err = DNS_ERR_NONE;
  26. char *q_name;
  27. tor_assert(req);
  28. tor_assert(_data == NULL);
  29. log_info(LD_APP, "Got a new DNS request!");
  30. req->flags |= 0x80; /* set RA */
  31. /* First, check whether the requesting address matches our SOCKSPolicy. */
  32. if ((addrlen = evdns_server_request_get_requesting_addr(req,
  33. (struct sockaddr*)&addr, sizeof(addr))) < 0) {
  34. log_warn(LD_APP, "Couldn't get requesting address.");
  35. evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
  36. return;
  37. }
  38. (void) addrlen;
  39. sa = (struct sockaddr*) &addr;
  40. if (sa->sa_family != AF_INET) {
  41. /* XXXX020 Handle IPV6 */
  42. log_warn(LD_APP, "Requesting address wasn't ipv4.");
  43. evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
  44. return;
  45. } else {
  46. struct sockaddr_in *sin = (struct sockaddr_in*)&addr;
  47. ipaddr = ntohl(sin->sin_addr.s_addr);
  48. }
  49. if (!socks_policy_permits_address(ipaddr)) {
  50. log_warn(LD_APP, "Rejecting DNS request from disallowed IP.");
  51. evdns_server_request_respond(req, DNS_ERR_REFUSED);
  52. return;
  53. }
  54. /* Now, let's find the first actual question of a type we can answer in this
  55. * DNS request. It makes us a little noncompliant to act like this; we
  56. * should fix that eventually if it turns out to make a difference for
  57. * anybody. */
  58. if (req->nquestions == 0) {
  59. log_info(LD_APP, "No questions in DNS request; sending back nil reply.");
  60. evdns_server_request_respond(req, 0);
  61. return;
  62. }
  63. if (req->nquestions > 1) {
  64. log_info(LD_APP, "Got a DNS request with more than one question; I only "
  65. "handle one question at a time for now. Skipping the extras.");
  66. }
  67. for (i = 0; i < req->nquestions; ++i) {
  68. if (req->questions[i]->class != EVDNS_CLASS_INET)
  69. continue;
  70. switch (req->questions[i]->type) {
  71. case EVDNS_TYPE_A:
  72. case EVDNS_TYPE_PTR:
  73. q = req->questions[i];
  74. default:
  75. break;
  76. }
  77. }
  78. if (!q) {
  79. log_info(LD_APP, "None of the questions we got were ones we're willing "
  80. "to support. Sending NODATA.");
  81. evdns_server_request_respond(req, DNS_ERR_NONE);
  82. return;
  83. }
  84. if (q->type == EVDNS_TYPE_A) {
  85. /* Refuse any attempt to resolve a noconnect address, right now. */
  86. if (hostname_is_noconnect_address(q->name)) {
  87. err = DNS_ERR_REFUSED;
  88. }
  89. } else {
  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. /* XXXX020 Send a stream event to the controller. */
  101. /* Make a new dummy AP connection, and attach the request to it. */
  102. conn = TO_EDGE_CONN(connection_new(CONN_TYPE_AP, AF_INET));
  103. conn->_base.state = AP_CONN_STATE_RESOLVE_WAIT;
  104. if (q->type == EVDNS_TYPE_A)
  105. conn->socks_request->command = SOCKS_COMMAND_RESOLVE;
  106. else
  107. conn->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  108. strlcpy(conn->socks_request->address, q->name,
  109. sizeof(conn->socks_request->address));
  110. conn->dns_server_request = req;
  111. /* Now, throw the connection over to get rewritten (which will answer it
  112. * immediately if it's in the cache, or completely bogus, or automapped),
  113. * and then attached to a circuit. */
  114. log_info(LD_APP, "Passing request for %s to rewrite_and_attach.",
  115. escaped_safe_str(q->name));
  116. q_name = tor_strdup(q->name); /* q could be freed in rewrite_and_attach */
  117. connection_ap_handshake_rewrite_and_attach(conn, NULL, NULL);
  118. /* Now, the connection is marked if it was bad. */
  119. log_info(LD_APP, "Passed request for %s to rewrite_and_attach.",
  120. escaped_safe_str(q_name));
  121. tor_free(q_name);
  122. }
  123. /** If there is a pending request on <b>conn</b> that's waiting for an answer,
  124. * send back an error and free the request. */
  125. void
  126. dnsserv_reject_request(edge_connection_t *conn)
  127. {
  128. if (conn->dns_server_request) {
  129. evdns_server_request_respond(conn->dns_server_request,
  130. DNS_ERR_SERVERFAILED);
  131. conn->dns_server_request = NULL;
  132. }
  133. }
  134. /** Tell the dns request waiting for an answer on <b>conn</b> that we have an
  135. * answer of type <b>answer_type</b> (RESOLVE_TYPE_IPV4/IPV6/ERR), of length
  136. * <b>answer_len</b>, in <b>answer</b>, with TTL <b>ttl</b>. Doesn't do
  137. * any caching; that's handled elsewhere. */
  138. void
  139. dnsserv_resolved(edge_connection_t *conn,
  140. int answer_type,
  141. size_t answer_len,
  142. const char *answer,
  143. int ttl)
  144. {
  145. struct evdns_server_request *req = conn->dns_server_request;
  146. int err = DNS_ERR_NONE;
  147. if (!req)
  148. return;
  149. /* XXXX020 Re-do; this is dumb. */
  150. if (ttl < 60)
  151. ttl = 60;
  152. /* The evdns interface is: add a bunch of reply items (corresponding to one
  153. * or more of the questions in the request); then, call
  154. * evdns_server_request_respond. */
  155. if (answer_type == RESOLVED_TYPE_IPV6) {
  156. log_info(LD_APP, "Got an IPv6 answer; that's not implemented.");
  157. err = DNS_ERR_NOTIMPL;
  158. } else if (answer_type == RESOLVED_TYPE_IPV4 && answer_len == 4 &&
  159. conn->socks_request->command == SOCKS_COMMAND_RESOLVE) {
  160. evdns_server_request_add_a_reply(req,
  161. conn->socks_request->address,
  162. 1, (char*)answer, ttl);
  163. } else if (answer_type == RESOLVED_TYPE_HOSTNAME &&
  164. conn->socks_request->command == SOCKS_COMMAND_RESOLVE_PTR) {
  165. char *ans = tor_strndup(answer, answer_len);
  166. evdns_server_request_add_ptr_reply(req, NULL,
  167. conn->socks_request->address,
  168. (char*)answer, ttl);
  169. tor_free(ans);
  170. } else if (answer_type == RESOLVED_TYPE_ERROR) {
  171. err = DNS_ERR_NOTEXIST;
  172. } else { /* answer_type == RESOLVED_TYPE_ERROR_TRANSIENT */
  173. err = DNS_ERR_SERVERFAILED;
  174. }
  175. evdns_server_request_respond(req, err);
  176. conn->dns_server_request = NULL;
  177. }
  178. /* Set up the evdns server port for the UDP socket on <b>conn</b>, which
  179. * must be an AP_DNS_LISTENER */
  180. void
  181. dnsserv_configure_listener(connection_t *conn)
  182. {
  183. tor_assert(conn);
  184. tor_assert(conn->s);
  185. tor_assert(conn->type == CONN_TYPE_AP_DNS_LISTENER);
  186. evdns_add_server_port(conn->s, 0, evdns_server_callback, NULL);
  187. }
  188. /** Free the evdns server port for <b>conn</b>, which must be an
  189. * AP_DNS_LISTENER. */
  190. void
  191. dnsserv_close_listener(connection_t *conn)
  192. {
  193. tor_assert(conn);
  194. tor_assert(conn->type == CONN_TYPE_AP_DNS_LISTENER);
  195. if (conn->dns_server_port) {
  196. evdns_close_server_port(conn->dns_server_port);
  197. conn->dns_server_port = NULL;
  198. }
  199. }