dnsserv.c 12 KB

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