dnsserv.c 9.2 KB

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