1 /* Copyright (c) 2007-2013, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
5 * \file dnsserv.c \brief Implements client-side DNS proxy server code. Note:
6 * this is the DNS Server code, not the Server DNS code. Confused? This code
7 * runs on client-side, and acts as a DNS server. The code in dns.c, on the
8 * other hand, runs on Tor servers, and acts as a DNS client.
14 #include "connection.h"
15 #include "connection_edge.h"
19 #ifdef HAVE_EVENT2_DNS_H
20 #include <event2/dns.h>
21 #include <event2/dns_compat.h>
22 /* XXXX this implies we want an improved evdns */
23 #include <event2/dns_struct.h>
28 /** Helper function: called by evdns whenever the client sends a request to our
29 * DNSPort. We need to eventually answer the request <b>req</b>.
32 evdns_server_callback(struct evdns_server_request
*req
, void *data_
)
34 const listener_connection_t
*listener
= data_
;
35 entry_connection_t
*entry_conn
;
36 edge_connection_t
*conn
;
38 struct evdns_server_question
*q
= NULL
, *supported_q
= NULL
;
39 struct sockaddr_storage addr
;
44 int err
= DNS_ERR_NONE
;
49 log_info(LD_APP
, "Got a new DNS request!");
51 req
->flags
|= 0x80; /* set RA */
53 /* First, check whether the requesting address matches our SOCKSPolicy. */
54 if ((addrlen
= evdns_server_request_get_requesting_addr(req
,
55 (struct sockaddr
*)&addr
, (socklen_t
)sizeof(addr
))) < 0) {
56 log_warn(LD_APP
, "Couldn't get requesting address.");
57 evdns_server_request_respond(req
, DNS_ERR_SERVERFAILED
);
61 sa
= (struct sockaddr
*) &addr
;
62 if (tor_addr_from_sockaddr(&tor_addr
, sa
, &port
)<0) {
63 log_warn(LD_APP
, "Requesting address wasn't recognized.");
64 evdns_server_request_respond(req
, DNS_ERR_SERVERFAILED
);
68 if (!socks_policy_permits_address(&tor_addr
)) {
69 log_warn(LD_APP
, "Rejecting DNS request from disallowed IP.");
70 evdns_server_request_respond(req
, DNS_ERR_REFUSED
);
74 /* Now, let's find the first actual question of a type we can answer in this
75 * DNS request. It makes us a little noncompliant to act like this; we
76 * should fix that eventually if it turns out to make a difference for
78 if (req
->nquestions
== 0) {
79 log_info(LD_APP
, "No questions in DNS request; sending back nil reply.");
80 evdns_server_request_respond(req
, 0);
83 if (req
->nquestions
> 1) {
84 log_info(LD_APP
, "Got a DNS request with more than one question; I only "
85 "handle one question at a time for now. Skipping the extras.");
87 for (i
= 0; i
< req
->nquestions
; ++i
) {
88 if (req
->questions
[i
]->dns_question_class
!= EVDNS_CLASS_INET
)
91 q
= req
->questions
[i
];
92 switch (req
->questions
[i
]->type
) {
96 /* We always pick the first one of these questions, if there is
108 log_info(LD_APP
, "None of the questions we got were ones we're willing "
109 "to support. Sending NOTIMPL.");
110 evdns_server_request_respond(req
, DNS_ERR_NOTIMPL
);
114 /* Make sure the name isn't too long: This should be impossible, I think. */
115 if (err
== DNS_ERR_NONE
&& strlen(q
->name
) > MAX_SOCKS_ADDR_LEN
-1)
116 err
= DNS_ERR_FORMAT
;
118 if (err
!= DNS_ERR_NONE
|| !supported_q
) {
119 /* We got an error? There's no question we're willing to answer? Then
120 * send back an answer immediately; we're done. */
121 evdns_server_request_respond(req
, err
);
125 /* Make a new dummy AP connection, and attach the request to it. */
126 entry_conn
= entry_connection_new(CONN_TYPE_AP
, AF_INET
);
127 conn
= ENTRY_TO_EDGE_CONN(entry_conn
);
128 TO_CONN(conn
)->state
= AP_CONN_STATE_RESOLVE_WAIT
;
129 conn
->is_dns_request
= 1;
131 tor_addr_copy(&TO_CONN(conn
)->addr
, &tor_addr
);
132 TO_CONN(conn
)->port
= port
;
133 TO_CONN(conn
)->address
= tor_dup_addr(&tor_addr
);
135 if (q
->type
== EVDNS_TYPE_A
|| q
->type
== EVDNS_TYPE_AAAA
||
136 q
->type
== EVDNS_QTYPE_ALL
) {
137 entry_conn
->socks_request
->command
= SOCKS_COMMAND_RESOLVE
;
139 tor_assert(q
->type
== EVDNS_TYPE_PTR
);
140 entry_conn
->socks_request
->command
= SOCKS_COMMAND_RESOLVE_PTR
;
143 if (q
->type
== EVDNS_TYPE_A
|| q
->type
== EVDNS_QTYPE_ALL
) {
144 entry_conn
->ipv4_traffic_ok
= 1;
145 entry_conn
->ipv6_traffic_ok
= 0;
146 entry_conn
->prefer_ipv6_traffic
= 0;
147 } else if (q
->type
== EVDNS_TYPE_AAAA
) {
148 entry_conn
->ipv4_traffic_ok
= 0;
149 entry_conn
->ipv6_traffic_ok
= 1;
150 entry_conn
->prefer_ipv6_traffic
= 1;
153 strlcpy(entry_conn
->socks_request
->address
, q
->name
,
154 sizeof(entry_conn
->socks_request
->address
));
156 entry_conn
->socks_request
->listener_type
= listener
->base_
.type
;
157 entry_conn
->dns_server_request
= req
;
158 entry_conn
->isolation_flags
= listener
->isolation_flags
;
159 entry_conn
->session_group
= listener
->session_group
;
160 entry_conn
->nym_epoch
= get_signewnym_epoch();
162 if (connection_add(ENTRY_TO_CONN(entry_conn
)) < 0) {
163 log_warn(LD_APP
, "Couldn't register dummy connection for DNS request");
164 evdns_server_request_respond(req
, DNS_ERR_SERVERFAILED
);
165 connection_free(ENTRY_TO_CONN(entry_conn
));
169 control_event_stream_status(entry_conn
, STREAM_EVENT_NEW_RESOLVE
, 0);
171 /* Now, unless a controller asked us to leave streams unattached,
172 * throw the connection over to get rewritten (which will
173 * answer it immediately if it's in the cache, or completely bogus, or
174 * automapped), and then attached to a circuit. */
175 log_info(LD_APP
, "Passing request for %s to rewrite_and_attach.",
176 escaped_safe_str_client(q
->name
));
177 q_name
= tor_strdup(q
->name
); /* q could be freed in rewrite_and_attach */
178 connection_ap_rewrite_and_attach_if_allowed(entry_conn
, NULL
, NULL
);
179 /* Now, the connection is marked if it was bad. */
181 log_info(LD_APP
, "Passed request for %s to rewrite_and_attach_if_allowed.",
182 escaped_safe_str_client(q_name
));
186 /** Helper function: called whenever the client sends a resolve request to our
187 * controller. We need to eventually answer the request <b>req</b>.
188 * Returns 0 if the controller will be getting (or has gotten) an event in
189 * response; -1 if we couldn't launch the request.
192 dnsserv_launch_request(const char *name
, int reverse
,
193 control_connection_t
*control_conn
)
195 entry_connection_t
*entry_conn
;
196 edge_connection_t
*conn
;
199 /* Make a new dummy AP connection, and attach the request to it. */
200 entry_conn
= entry_connection_new(CONN_TYPE_AP
, AF_INET
);
201 conn
= ENTRY_TO_EDGE_CONN(entry_conn
);
202 conn
->base_
.state
= AP_CONN_STATE_RESOLVE_WAIT
;
204 tor_addr_copy(&TO_CONN(conn
)->addr
, &control_conn
->base_
.addr
);
207 * The control connection can be AF_UNIX and if so tor_dup_addr will
208 * unhelpfully say "<unknown address type>"; say "(Tor_internal)"
211 if (control_conn
->base_
.socket_family
== AF_UNIX
) {
212 TO_CONN(conn
)->port
= 0;
213 TO_CONN(conn
)->address
= tor_strdup("(Tor_internal)");
215 TO_CONN(conn
)->port
= control_conn
->base_
.port
;
216 TO_CONN(conn
)->address
= tor_dup_addr(&control_conn
->base_
.addr
);
219 TO_CONN(conn
)->port
= control_conn
->base_
.port
;
220 TO_CONN(conn
)->address
= tor_dup_addr(&control_conn
->base_
.addr
);
224 entry_conn
->socks_request
->command
= SOCKS_COMMAND_RESOLVE_PTR
;
226 entry_conn
->socks_request
->command
= SOCKS_COMMAND_RESOLVE
;
228 conn
->is_dns_request
= 1;
230 strlcpy(entry_conn
->socks_request
->address
, name
,
231 sizeof(entry_conn
->socks_request
->address
));
233 entry_conn
->socks_request
->listener_type
= CONN_TYPE_CONTROL_LISTENER
;
234 entry_conn
->original_dest_address
= tor_strdup(name
);
235 entry_conn
->session_group
= SESSION_GROUP_CONTROL_RESOLVE
;
236 entry_conn
->nym_epoch
= get_signewnym_epoch();
237 entry_conn
->isolation_flags
= ISO_DEFAULT
;
239 if (connection_add(TO_CONN(conn
))<0) {
240 log_warn(LD_APP
, "Couldn't register dummy connection for RESOLVE request");
241 connection_free(TO_CONN(conn
));
245 control_event_stream_status(entry_conn
, STREAM_EVENT_NEW_RESOLVE
, 0);
247 /* Now, unless a controller asked us to leave streams unattached,
248 * throw the connection over to get rewritten (which will
249 * answer it immediately if it's in the cache, or completely bogus, or
250 * automapped), and then attached to a circuit. */
251 log_info(LD_APP
, "Passing request for %s to rewrite_and_attach.",
252 escaped_safe_str_client(name
));
253 q_name
= tor_strdup(name
); /* q could be freed in rewrite_and_attach */
254 connection_ap_rewrite_and_attach_if_allowed(entry_conn
, NULL
, NULL
);
255 /* Now, the connection is marked if it was bad. */
257 log_info(LD_APP
, "Passed request for %s to rewrite_and_attach_if_allowed.",
258 escaped_safe_str_client(q_name
));
263 /** If there is a pending request on <b>conn</b> that's waiting for an answer,
264 * send back an error and free the request. */
266 dnsserv_reject_request(entry_connection_t
*conn
)
268 if (conn
->dns_server_request
) {
269 evdns_server_request_respond(conn
->dns_server_request
,
270 DNS_ERR_SERVERFAILED
);
271 conn
->dns_server_request
= NULL
;
275 /** Look up the original name that corresponds to 'addr' in req. We use this
276 * to preserve case in order to facilitate people using 0x20-hacks to avoid
279 evdns_get_orig_address(const struct evdns_server_request
*req
,
280 int rtype
, const char *addr
)
285 case RESOLVED_TYPE_IPV4
:
288 case RESOLVED_TYPE_HOSTNAME
:
289 type
= EVDNS_TYPE_PTR
;
291 case RESOLVED_TYPE_IPV6
:
292 type
= EVDNS_TYPE_AAAA
;
295 tor_fragile_assert();
299 for (i
= 0; i
< req
->nquestions
; ++i
) {
300 const struct evdns_server_question
*q
= req
->questions
[i
];
301 if (q
->type
== type
&& !strcasecmp(q
->name
, addr
))
307 /** Tell the dns request waiting for an answer on <b>conn</b> that we have an
308 * answer of type <b>answer_type</b> (RESOLVE_TYPE_IPV4/IPV6/ERR), of length
309 * <b>answer_len</b>, in <b>answer</b>, with TTL <b>ttl</b>. Doesn't do
310 * any caching; that's handled elsewhere. */
312 dnsserv_resolved(entry_connection_t
*conn
,
318 struct evdns_server_request
*req
= conn
->dns_server_request
;
320 int err
= DNS_ERR_NONE
;
323 name
= evdns_get_orig_address(req
, answer_type
,
324 conn
->socks_request
->address
);
326 /* XXXX Re-do; this is dumb. */
330 /* The evdns interface is: add a bunch of reply items (corresponding to one
331 * or more of the questions in the request); then, call
332 * evdns_server_request_respond. */
333 if (answer_type
== RESOLVED_TYPE_IPV6
) {
334 evdns_server_request_add_aaaa_reply(req
,
337 } else if (answer_type
== RESOLVED_TYPE_IPV4
&& answer_len
== 4 &&
338 conn
->socks_request
->command
== SOCKS_COMMAND_RESOLVE
) {
339 evdns_server_request_add_a_reply(req
,
342 } else if (answer_type
== RESOLVED_TYPE_HOSTNAME
&&
344 conn
->socks_request
->command
== SOCKS_COMMAND_RESOLVE_PTR
) {
345 char *ans
= tor_strndup(answer
, answer_len
);
346 evdns_server_request_add_ptr_reply(req
, NULL
,
350 } else if (answer_type
== RESOLVED_TYPE_ERROR
) {
351 err
= DNS_ERR_NOTEXIST
;
352 } else { /* answer_type == RESOLVED_TYPE_ERROR_TRANSIENT */
353 err
= DNS_ERR_SERVERFAILED
;
356 evdns_server_request_respond(req
, err
);
358 conn
->dns_server_request
= NULL
;
361 /** Set up the evdns server port for the UDP socket on <b>conn</b>, which
362 * must be an AP_DNS_LISTENER */
364 dnsserv_configure_listener(connection_t
*conn
)
366 listener_connection_t
*listener_conn
;
368 tor_assert(SOCKET_OK(conn
->s
));
369 tor_assert(conn
->type
== CONN_TYPE_AP_DNS_LISTENER
);
371 listener_conn
= TO_LISTENER_CONN(conn
);
372 listener_conn
->dns_server_port
=
373 tor_evdns_add_server_port(conn
->s
, 0, evdns_server_callback
,
377 /** Free the evdns server port for <b>conn</b>, which must be an
378 * AP_DNS_LISTENER. */
380 dnsserv_close_listener(connection_t
*conn
)
382 listener_connection_t
*listener_conn
;
384 tor_assert(conn
->type
== CONN_TYPE_AP_DNS_LISTENER
);
386 listener_conn
= TO_LISTENER_CONN(conn
);
388 if (listener_conn
->dns_server_port
) {
389 evdns_close_server_port(listener_conn
->dns_server_port
);
390 listener_conn
->dns_server_port
= NULL
;