dnsserv.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. struct sockaddr_in *sin;
  24. int addrlen;
  25. uint32_t ipaddr;
  26. int err = DNS_ERR_NONE;
  27. char *q_name;
  28. tor_assert(req);
  29. tor_assert(_data == NULL);
  30. log_info(LD_APP, "Got a new DNS request!");
  31. req->flags |= 0x80; /* set RA */
  32. /* First, check whether the requesting address matches our SOCKSPolicy. */
  33. if ((addrlen = evdns_server_request_get_requesting_addr(req,
  34. (struct sockaddr*)&addr, sizeof(addr))) < 0) {
  35. log_warn(LD_APP, "Couldn't get requesting address.");
  36. evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
  37. return;
  38. }
  39. (void) addrlen;
  40. sa = (struct sockaddr*) &addr;
  41. if (sa->sa_family != AF_INET) {
  42. /* XXXX020 Handle IPV6 */
  43. log_warn(LD_APP, "Requesting address wasn't ipv4.");
  44. evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
  45. return;
  46. } else {
  47. sin = (struct sockaddr_in*)&addr;
  48. ipaddr = ntohl(sin->sin_addr.s_addr);
  49. }
  50. if (!socks_policy_permits_address(ipaddr)) {
  51. log_warn(LD_APP, "Rejecting DNS request from disallowed IP.");
  52. evdns_server_request_respond(req, DNS_ERR_REFUSED);
  53. return;
  54. }
  55. /* Now, let's find the first actual question of a type we can answer in this
  56. * DNS request. It makes us a little noncompliant to act like this; we
  57. * should fix that eventually if it turns out to make a difference for
  58. * anybody. */
  59. if (req->nquestions == 0) {
  60. log_info(LD_APP, "No questions in DNS request; sending back nil reply.");
  61. evdns_server_request_respond(req, 0);
  62. return;
  63. }
  64. if (req->nquestions > 1) {
  65. log_info(LD_APP, "Got a DNS request with more than one question; I only "
  66. "handle one question at a time for now. Skipping the extras.");
  67. }
  68. for (i = 0; i < req->nquestions; ++i) {
  69. if (req->questions[i]->class != EVDNS_CLASS_INET)
  70. continue;
  71. switch (req->questions[i]->type) {
  72. case EVDNS_TYPE_A:
  73. case EVDNS_TYPE_PTR:
  74. q = req->questions[i];
  75. default:
  76. break;
  77. }
  78. }
  79. if (!q) {
  80. log_info(LD_APP, "None of the questions we got were ones we're willing "
  81. "to support. Sending NODATA.");
  82. evdns_server_request_respond(req, DNS_ERR_NONE);
  83. return;
  84. }
  85. if (q->type == EVDNS_TYPE_A) {
  86. /* Refuse any attempt to resolve a noconnect address, right now. */
  87. if (hostname_is_noconnect_address(q->name)) {
  88. err = DNS_ERR_REFUSED;
  89. }
  90. } else {
  91. tor_assert(q->type == EVDNS_TYPE_PTR);
  92. }
  93. /* Make sure the name isn't too long: This should be impossible, I think. */
  94. if (err == DNS_ERR_NONE && strlen(q->name) > MAX_SOCKS_ADDR_LEN-1)
  95. err = DNS_ERR_FORMAT;
  96. if (err != DNS_ERR_NONE) {
  97. /* We got an error? Then send back an answer immediately; we're done. */
  98. evdns_server_request_respond(req, err);
  99. return;
  100. }
  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. conn->is_dns_request = 1;
  105. TO_CONN(conn)->addr = ntohl(sin->sin_addr.s_addr);
  106. TO_CONN(conn)->port = ntohs(sin->sin_port);
  107. TO_CONN(conn)->address = tor_dup_addr(TO_CONN(conn)->addr);
  108. if (q->type == EVDNS_TYPE_A)
  109. conn->socks_request->command = SOCKS_COMMAND_RESOLVE;
  110. else
  111. conn->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  112. strlcpy(conn->socks_request->address, q->name,
  113. sizeof(conn->socks_request->address));
  114. conn->dns_server_request = req;
  115. connection_add(TO_CONN(conn));
  116. control_event_stream_status(conn, STREAM_EVENT_NEW, 0);
  117. /* Now, throw the connection over to get rewritten (which will answer it
  118. * immediately if it's in the cache, or completely bogus, or automapped),
  119. * and then attached to a circuit. */
  120. log_info(LD_APP, "Passing request for %s to rewrite_and_attach.",
  121. escaped_safe_str(q->name));
  122. q_name = tor_strdup(q->name); /* q could be freed in rewrite_and_attach */
  123. connection_ap_handshake_rewrite_and_attach(conn, NULL, NULL);
  124. /* Now, the connection is marked if it was bad. */
  125. log_info(LD_APP, "Passed request for %s to rewrite_and_attach.",
  126. escaped_safe_str(q_name));
  127. tor_free(q_name);
  128. }
  129. /* Helper function: called whenever the client sends a resolve request to our
  130. * controller. We need to eventually answer the request <b>req</b>.
  131. */
  132. void
  133. dnsserv_launch_request(const char *name, int reverse)
  134. {
  135. edge_connection_t *conn;
  136. char *q_name;
  137. /* Make a new dummy AP connection, and attach the request to it. */
  138. conn = TO_EDGE_CONN(connection_new(CONN_TYPE_AP, AF_INET));
  139. conn->_base.state = AP_CONN_STATE_RESOLVE_WAIT;
  140. if (reverse)
  141. conn->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  142. else
  143. conn->socks_request->command = SOCKS_COMMAND_RESOLVE;
  144. conn->is_dns_request = 1;
  145. strlcpy(conn->socks_request->address, name,
  146. sizeof(conn->socks_request->address));
  147. connection_add(TO_CONN(conn));
  148. /* Now, throw the connection over to get rewritten (which will answer it
  149. * immediately if it's in the cache, or completely bogus, or automapped),
  150. * and then attached to a circuit. */
  151. log_info(LD_APP, "Passing request for %s to rewrite_and_attach.",
  152. escaped_safe_str(name));
  153. q_name = tor_strdup(name); /* q could be freed in rewrite_and_attach */
  154. connection_ap_handshake_rewrite_and_attach(conn, NULL, NULL);
  155. /* Now, the connection is marked if it was bad. */
  156. log_info(LD_APP, "Passed request for %s to rewrite_and_attach.",
  157. escaped_safe_str(q_name));
  158. tor_free(q_name);
  159. }
  160. /** If there is a pending request on <b>conn</b> that's waiting for an answer,
  161. * send back an error and free the request. */
  162. void
  163. dnsserv_reject_request(edge_connection_t *conn)
  164. {
  165. if (conn->dns_server_request) {
  166. evdns_server_request_respond(conn->dns_server_request,
  167. DNS_ERR_SERVERFAILED);
  168. conn->dns_server_request = NULL;
  169. }
  170. }
  171. /** Tell the dns request waiting for an answer on <b>conn</b> that we have an
  172. * answer of type <b>answer_type</b> (RESOLVE_TYPE_IPV4/IPV6/ERR), of length
  173. * <b>answer_len</b>, in <b>answer</b>, with TTL <b>ttl</b>. Doesn't do
  174. * any caching; that's handled elsewhere. */
  175. void
  176. dnsserv_resolved(edge_connection_t *conn,
  177. int answer_type,
  178. size_t answer_len,
  179. const char *answer,
  180. int ttl)
  181. {
  182. struct evdns_server_request *req = conn->dns_server_request;
  183. int err = DNS_ERR_NONE;
  184. if (!req)
  185. return;
  186. /* XXXX020 Re-do; this is dumb. */
  187. if (ttl < 60)
  188. ttl = 60;
  189. /* The evdns interface is: add a bunch of reply items (corresponding to one
  190. * or more of the questions in the request); then, call
  191. * evdns_server_request_respond. */
  192. if (answer_type == RESOLVED_TYPE_IPV6) {
  193. log_info(LD_APP, "Got an IPv6 answer; that's not implemented.");
  194. err = DNS_ERR_NOTIMPL;
  195. } else if (answer_type == RESOLVED_TYPE_IPV4 && answer_len == 4 &&
  196. conn->socks_request->command == SOCKS_COMMAND_RESOLVE) {
  197. evdns_server_request_add_a_reply(req,
  198. conn->socks_request->address,
  199. 1, (char*)answer, ttl);
  200. } else if (answer_type == RESOLVED_TYPE_HOSTNAME &&
  201. conn->socks_request->command == SOCKS_COMMAND_RESOLVE_PTR) {
  202. char *ans = tor_strndup(answer, answer_len);
  203. evdns_server_request_add_ptr_reply(req, NULL,
  204. conn->socks_request->address,
  205. (char*)answer, ttl);
  206. tor_free(ans);
  207. } else if (answer_type == RESOLVED_TYPE_ERROR) {
  208. err = DNS_ERR_NOTEXIST;
  209. } else { /* answer_type == RESOLVED_TYPE_ERROR_TRANSIENT */
  210. err = DNS_ERR_SERVERFAILED;
  211. }
  212. evdns_server_request_respond(req, err);
  213. conn->dns_server_request = NULL;
  214. }
  215. /* Set up the evdns server port for the UDP socket on <b>conn</b>, which
  216. * must be an AP_DNS_LISTENER */
  217. void
  218. dnsserv_configure_listener(connection_t *conn)
  219. {
  220. tor_assert(conn);
  221. tor_assert(conn->s);
  222. tor_assert(conn->type == CONN_TYPE_AP_DNS_LISTENER);
  223. conn->dns_server_port = evdns_add_server_port(conn->s, 0,
  224. evdns_server_callback, NULL);
  225. }
  226. /** Free the evdns server port for <b>conn</b>, which must be an
  227. * AP_DNS_LISTENER. */
  228. void
  229. dnsserv_close_listener(connection_t *conn)
  230. {
  231. tor_assert(conn);
  232. tor_assert(conn->type == CONN_TYPE_AP_DNS_LISTENER);
  233. if (conn->dns_server_port) {
  234. evdns_close_server_port(conn->dns_server_port);
  235. conn->dns_server_port = NULL;
  236. }
  237. }