dnsserv.c 10 KB

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