dnsserv.c 10 KB

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