dnsserv.c 10 KB

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