Tor 0.4.9.13
Loading...
Searching...
No Matches
dnsserv.c
Go to the documentation of this file.
1/* Copyright (c) 2007-2021, The Tor Project, Inc. */
2/* See LICENSE for licensing information */
3
4/**
5 * \file dnsserv.c
6 * \brief Implements client-side DNS proxy server code.
7 *
8 * When a user enables the DNSPort configuration option to have their local
9 * Tor client handle DNS requests, this module handles it. It functions as a
10 * "DNS Server" on the client side, which client applications use.
11 *
12 * Inbound DNS requests are represented as entry_connection_t here (since
13 * that's how Tor represents client-side streams), which are kept associated
14 * with an evdns_server_request structure as exposed by Libevent's
15 * evdns code.
16 *
17 * Upon receiving a DNS request, libevent calls our evdns_server_callback()
18 * function here, which causes this module to create an entry_connection_t
19 * request as appropriate. Later, when that request is answered,
20 * connection_edge.c calls dnsserv_resolved() so we can finish up and tell the
21 * DNS client.
22 **/
23
24#include "core/or/or.h"
26#include "app/config/config.h"
32#include "core/or/policies.h"
33
39
40#include <event2/dns.h>
41#include <event2/dns_compat.h>
42/* XXXX this implies we want an improved evdns */
43#include <event2/dns_struct.h>
44
45/** Helper function: called by evdns whenever the client sends a request to our
46 * DNSPort. We need to eventually answer the request <b>req</b>.
47 */
48static void
49evdns_server_callback(struct evdns_server_request *req, void *data_)
50{
51 const listener_connection_t *listener = data_;
52 entry_connection_t *entry_conn;
54 int i = 0;
55 struct evdns_server_question *q = NULL, *supported_q = NULL;
56 struct sockaddr_storage addr;
57 struct sockaddr *sa;
58 int addrlen;
59 tor_addr_t tor_addr;
60 uint16_t port;
61 int err = DNS_ERR_NONE;
62 char *q_name;
63
64 tor_assert(req);
65
66 log_info(LD_APP, "Got a new DNS request!");
67
68 /* Receiving a request on the DNSPort counts as user activity. */
70
71 req->flags |= 0x80; /* set RA */
72
73 /* First, check whether the requesting address matches our SOCKSPolicy. */
74 if ((addrlen = evdns_server_request_get_requesting_addr(req,
75 (struct sockaddr*)&addr, (socklen_t)sizeof(addr))) < 0) {
76 log_warn(LD_APP, "Couldn't get requesting address.");
77 evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
78 return;
79 }
80 (void) addrlen;
81 sa = (struct sockaddr*) &addr;
82 if (tor_addr_from_sockaddr(&tor_addr, sa, &port)<0) {
83 log_warn(LD_APP, "Requesting address wasn't recognized.");
84 evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
85 return;
86 }
87
88 if (!socks_policy_permits_address(&tor_addr)) {
89 log_warn(LD_APP, "Rejecting DNS request from disallowed IP.");
90 evdns_server_request_respond(req, DNS_ERR_REFUSED);
91 return;
92 }
93
94 /* Now, let's find the first actual question of a type we can answer in this
95 * DNS request. It makes us a little noncompliant to act like this; we
96 * should fix that eventually if it turns out to make a difference for
97 * anybody. */
98 if (req->nquestions == 0) {
99 log_info(LD_APP, "No questions in DNS request; sending back nil reply.");
100 evdns_server_request_respond(req, 0);
101 return;
102 }
103 if (req->nquestions > 1) {
104 log_info(LD_APP, "Got a DNS request with more than one question; I only "
105 "handle one question at a time for now. Skipping the extras.");
106 }
107 for (i = 0; i < req->nquestions; ++i) {
108 if (req->questions[i]->dns_question_class != EVDNS_CLASS_INET)
109 continue;
110 switch (req->questions[i]->type) {
111 case EVDNS_TYPE_A:
112 case EVDNS_TYPE_AAAA:
113 case EVDNS_TYPE_PTR:
114 /* We always pick the first one of these questions, if there is
115 one. */
116 if (! supported_q)
117 supported_q = req->questions[i];
118 break;
119 default:
120 break;
121 }
122 }
123 if (supported_q)
124 q = supported_q;
125 if (!q) {
126 log_info(LD_APP, "None of the questions we got were ones we're willing "
127 "to support. Sending NOTIMPL.");
128 evdns_server_request_respond(req, DNS_ERR_NOTIMPL);
129 return;
130 }
131
132 /* Make sure the name isn't too long: This should be impossible, I think. */
133 if (err == DNS_ERR_NONE && strlen(q->name) > MAX_SOCKS_ADDR_LEN-1)
134 err = DNS_ERR_FORMAT;
135 /* Make sure name is valid DNS. */
136 if (err == DNS_ERR_NONE && ! name_is_valid_for_dns(q->name)) {
137 err = DNS_ERR_FORMAT;
138 }
139
140 if (err != DNS_ERR_NONE || !supported_q) {
141 /* We got an error? There's no question we're willing to answer? Then
142 * send back an answer immediately; we're done. */
143 evdns_server_request_respond(req, err);
144 return;
145 }
146
147 /* Make a new dummy AP connection, and attach the request to it. */
148 entry_conn = entry_connection_new(CONN_TYPE_AP, AF_INET);
149 conn = ENTRY_TO_EDGE_CONN(entry_conn);
150 CONNECTION_AP_EXPECT_NONPENDING(entry_conn);
151 TO_CONN(conn)->state = AP_CONN_STATE_RESOLVE_WAIT;
152 conn->is_dns_request = 1;
153
154 tor_addr_copy(&TO_CONN(conn)->addr, &tor_addr);
155 TO_CONN(conn)->port = port;
156 TO_CONN(conn)->address = tor_addr_to_str_dup(&tor_addr);
157
158 if (q->type == EVDNS_TYPE_A || q->type == EVDNS_TYPE_AAAA ||
159 q->type == EVDNS_QTYPE_ALL) {
161 } else {
162 tor_assert(q->type == EVDNS_TYPE_PTR);
164 }
165
166 /* This serves our DNS port so enable DNS request by default. */
167 entry_conn->entry_cfg.dns_request = 1;
168 if (q->type == EVDNS_TYPE_A || q->type == EVDNS_QTYPE_ALL) {
169 entry_conn->entry_cfg.ipv4_traffic = 1;
170 entry_conn->entry_cfg.ipv6_traffic = 0;
171 entry_conn->entry_cfg.prefer_ipv6 = 0;
172 } else if (q->type == EVDNS_TYPE_AAAA) {
173 entry_conn->entry_cfg.ipv4_traffic = 0;
174 entry_conn->entry_cfg.ipv6_traffic = 1;
175 entry_conn->entry_cfg.prefer_ipv6 = 1;
176 }
177
178 strlcpy(entry_conn->socks_request->address, q->name,
179 sizeof(entry_conn->socks_request->address));
180
181 entry_conn->socks_request->listener_type = listener->base_.type;
182 entry_conn->dns_server_request = req;
183 entry_conn->entry_cfg.isolation_flags = listener->entry_cfg.isolation_flags;
184 entry_conn->entry_cfg.session_group = listener->entry_cfg.session_group;
185 entry_conn->nym_epoch = get_signewnym_epoch();
186
187 if (connection_add(ENTRY_TO_CONN(entry_conn)) < 0) {
188 log_warn(LD_APP, "Couldn't register dummy connection for DNS request");
189 evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
190 connection_free_(ENTRY_TO_CONN(entry_conn));
191 return;
192 }
193
194 control_event_stream_status(entry_conn, STREAM_EVENT_NEW_RESOLVE, 0);
195
196 /* Now, unless a controller asked us to leave streams unattached,
197 * throw the connection over to get rewritten (which will
198 * answer it immediately if it's in the cache, or completely bogus, or
199 * automapped), and then attached to a circuit. */
200 log_info(LD_APP, "Passing request for %s to rewrite_and_attach.",
201 escaped_safe_str_client(q->name));
202 q_name = tor_strdup(q->name); /* q could be freed in rewrite_and_attach */
203 connection_ap_rewrite_and_attach_if_allowed(entry_conn, NULL, NULL);
204 /* Now, the connection is marked if it was bad. */
205
206 log_info(LD_APP, "Passed request for %s to rewrite_and_attach_if_allowed.",
208 tor_free(q_name);
209}
210
211/** Helper function: called whenever the client sends a resolve request to our
212 * controller. We need to eventually answer the request <b>req</b>.
213 * Returns 0 if the controller will be getting (or has gotten) an event in
214 * response; -1 if we couldn't launch the request.
215 */
216int
217dnsserv_launch_request(const char *name, int reverse,
218 control_connection_t *control_conn)
219{
220 entry_connection_t *entry_conn;
221 edge_connection_t *conn;
222 char *q_name;
223
224 /* Launching a request for a user counts as user activity. */
226
227 /* Make a new dummy AP connection, and attach the request to it. */
228 entry_conn = entry_connection_new(CONN_TYPE_AP, AF_INET);
229 entry_conn->entry_cfg.dns_request = 1;
230 conn = ENTRY_TO_EDGE_CONN(entry_conn);
231 CONNECTION_AP_EXPECT_NONPENDING(entry_conn);
232 conn->base_.state = AP_CONN_STATE_RESOLVE_WAIT;
233
234 tor_addr_copy(&TO_CONN(conn)->addr, &control_conn->base_.addr);
235#ifdef AF_UNIX
236 /*
237 * The control connection can be AF_UNIX and if so tor_addr_to_str_dup will
238 * unhelpfully say "<unknown address type>"; say "(Tor_internal)"
239 * instead.
240 */
241 if (control_conn->base_.socket_family == AF_UNIX) {
242 TO_CONN(conn)->port = 0;
243 TO_CONN(conn)->address = tor_strdup("(Tor_internal)");
244 } else {
245 TO_CONN(conn)->port = control_conn->base_.port;
246 TO_CONN(conn)->address = tor_addr_to_str_dup(&control_conn->base_.addr);
247 }
248#else /* !defined(AF_UNIX) */
249 TO_CONN(conn)->port = control_conn->base_.port;
250 TO_CONN(conn)->address = tor_addr_to_str_dup(&control_conn->base_.addr);
251#endif /* defined(AF_UNIX) */
252
253 if (reverse)
255 else
257
258 conn->is_dns_request = 1;
259
260 strlcpy(entry_conn->socks_request->address, name,
261 sizeof(entry_conn->socks_request->address));
262
264 entry_conn->original_dest_address = tor_strdup(name);
265 entry_conn->entry_cfg.session_group = SESSION_GROUP_CONTROL_RESOLVE;
266 entry_conn->nym_epoch = get_signewnym_epoch();
267 entry_conn->entry_cfg.isolation_flags = ISO_DEFAULT;
268
269 if (connection_add(TO_CONN(conn))<0) {
270 log_warn(LD_APP, "Couldn't register dummy connection for RESOLVE request");
272 return -1;
273 }
274
275 control_event_stream_status(entry_conn, STREAM_EVENT_NEW_RESOLVE, 0);
276
277 /* Now, unless a controller asked us to leave streams unattached,
278 * throw the connection over to get rewritten (which will
279 * answer it immediately if it's in the cache, or completely bogus, or
280 * automapped), and then attached to a circuit. */
281 log_info(LD_APP, "Passing request for %s to rewrite_and_attach.",
283 q_name = tor_strdup(name); /* q could be freed in rewrite_and_attach */
284 connection_ap_rewrite_and_attach_if_allowed(entry_conn, NULL, NULL);
285 /* Now, the connection is marked if it was bad. */
286
287 log_info(LD_APP, "Passed request for %s to rewrite_and_attach_if_allowed.",
289 tor_free(q_name);
290 return 0;
291}
292
293/** If there is a pending request on <b>conn</b> that's waiting for an answer,
294 * send back an error and free the request. */
295void
297{
298 if (conn->dns_server_request) {
299 evdns_server_request_respond(conn->dns_server_request,
300 DNS_ERR_SERVERFAILED);
301 conn->dns_server_request = NULL;
302 }
303}
304
305/** Look up the original name that corresponds to 'addr' in req. We use this
306 * to preserve case in order to facilitate clients using 0x20-hacks to avoid
307 * DNS poisoning. */
308static const char *
309evdns_get_orig_address(const struct evdns_server_request *req,
310 int rtype, const char *addr)
311{
312 int i, type;
313
314 switch (rtype) {
315 case RESOLVED_TYPE_IPV4:
316 type = EVDNS_TYPE_A;
317 break;
318 case RESOLVED_TYPE_HOSTNAME:
319 type = EVDNS_TYPE_PTR;
320 break;
321 case RESOLVED_TYPE_IPV6:
322 type = EVDNS_TYPE_AAAA;
323 break;
324 case RESOLVED_TYPE_ERROR:
325 case RESOLVED_TYPE_ERROR_TRANSIENT:
326 case RESOLVED_TYPE_NOERROR:
327 /* Addr doesn't matter, since we're not sending it back in the reply.*/
328 return addr;
329 default:
331 return addr;
332 }
333
334 for (i = 0; i < req->nquestions; ++i) {
335 const struct evdns_server_question *q = req->questions[i];
336 if (q->type == type && !strcasecmp(q->name, addr))
337 return q->name;
338 }
339 return addr;
340}
341
342/** Tell the dns request waiting for an answer on <b>conn</b> that we have an
343 * answer of type <b>answer_type</b> (RESOLVE_TYPE_IPV4/IPV6/ERR), of length
344 * <b>answer_len</b>, in <b>answer</b>, with TTL <b>ttl</b>. Doesn't do
345 * any caching; that's handled elsewhere. */
346void
348 int answer_type,
349 size_t answer_len,
350 const char *answer,
351 int ttl)
352{
353 struct evdns_server_request *req = conn->dns_server_request;
354 const char *name;
355 int err = DNS_ERR_NONE;
356 if (!req)
357 return;
358 name = evdns_get_orig_address(req, answer_type,
359 conn->socks_request->address);
360
361 /* XXXX Re-do; this is dumb. */
362 if (ttl < 60)
363 ttl = 60;
364
365 /* The evdns interface is: add a bunch of reply items (corresponding to one
366 * or more of the questions in the request); then, call
367 * evdns_server_request_respond. */
368 if (answer_type == RESOLVED_TYPE_IPV6) {
369 evdns_server_request_add_aaaa_reply(req,
370 name,
371 1, answer, ttl);
372 } else if (answer_type == RESOLVED_TYPE_IPV4 && answer_len == 4 &&
374 evdns_server_request_add_a_reply(req,
375 name,
376 1, answer, ttl);
377 } else if (answer_type == RESOLVED_TYPE_HOSTNAME &&
378 answer_len < 256 &&
380 char *ans = tor_strndup(answer, answer_len);
381 if (name_is_valid_for_dns(ans)) {
382 evdns_server_request_add_ptr_reply(req, NULL,
383 name,
384 ans, ttl);
385 } else {
386 err = DNS_ERR_FORMAT;
387 }
388 tor_free(ans);
389 } else if (answer_type == RESOLVED_TYPE_ERROR) {
390 err = DNS_ERR_NOTEXIST;
391 } else if (answer_type == RESOLVED_TYPE_NOERROR) {
392 err = DNS_ERR_NONE;
393 } else { /* answer_type == RESOLVED_TYPE_ERROR_TRANSIENT */
394 err = DNS_ERR_SERVERFAILED;
395 }
396
397 evdns_server_request_respond(req, err);
398
399 conn->dns_server_request = NULL;
400}
401
402/** Set up the evdns server port for the UDP socket on <b>conn</b>, which
403 * must be an AP_DNS_LISTENER */
404void
406{
407 listener_connection_t *listener_conn;
408 tor_assert(conn);
409 tor_assert(SOCKET_OK(conn->s));
411
412 listener_conn = TO_LISTENER_CONN(conn);
413 listener_conn->dns_server_port =
414 tor_evdns_add_server_port(conn->s, 0, evdns_server_callback,
415 listener_conn);
416}
417
418/** Free the evdns server port for <b>conn</b>, which must be an
419 * AP_DNS_LISTENER. */
420void
422{
423 listener_connection_t *listener_conn;
424 tor_assert(conn);
426
427 listener_conn = TO_LISTENER_CONN(conn);
428
429 if (listener_conn->dns_server_port) {
430 evdns_close_server_port(listener_conn->dns_server_port);
431 listener_conn->dns_server_port = NULL;
432 }
433}
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition address.c:933
char * tor_addr_to_str_dup(const tor_addr_t *addr)
Definition address.c:1164
bool name_is_valid_for_dns(const char *name)
Definition address.c:2340
int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa, uint16_t *port_out)
Definition address.c:165
time_t approx_time(void)
Definition approx_time.c:32
Header for compat_libevent.c.
const char * escaped_safe_str_client(const char *address)
Definition config.c:1149
const char * name
Definition config.c:2475
Header file for config.c.
entry_connection_t * entry_connection_new(int type, int socket_family)
Definition connection.c:596
void connection_free_(connection_t *conn)
Definition connection.c:965
listener_connection_t * TO_LISTENER_CONN(connection_t *c)
Definition connection.c:233
Header file for connection.c.
#define CONN_TYPE_CONTROL_LISTENER
Definition connection.h:58
#define CONN_TYPE_AP
Definition connection.h:51
#define CONN_TYPE_AP_DNS_LISTENER
Definition connection.h:68
int connection_ap_rewrite_and_attach_if_allowed(entry_connection_t *conn, origin_circuit_t *circ, crypt_path_t *cpath)
Header file for connection_edge.c.
#define AP_CONN_STATE_RESOLVE_WAIT
Controller connection structure.
int control_event_stream_status(entry_connection_t *conn, stream_status_event_t tp, int reason_code)
Header file for control_events.c.
void dnsserv_configure_listener(connection_t *conn)
Definition dnsserv.c:405
static const char * evdns_get_orig_address(const struct evdns_server_request *req, int rtype, const char *addr)
Definition dnsserv.c:309
void dnsserv_reject_request(entry_connection_t *conn)
Definition dnsserv.c:296
static void evdns_server_callback(struct evdns_server_request *req, void *data_)
Definition dnsserv.c:49
int dnsserv_launch_request(const char *name, int reverse, control_connection_t *control_conn)
Definition dnsserv.c:217
void dnsserv_resolved(entry_connection_t *conn, int answer_type, size_t answer_len, const char *answer, int ttl)
Definition dnsserv.c:347
void dnsserv_close_listener(connection_t *conn)
Definition dnsserv.c:421
Header file for dnsserv.c.
Entry connection structure.
#define ENTRY_TO_EDGE_CONN(c)
Listener connection structure.
#define LD_APP
Definition log.h:78
unsigned get_signewnym_epoch(void)
Definition mainloop.c:1363
Header file for mainloop.c.
#define tor_free(p)
Definition malloc.h:56
void note_user_activity(time_t now)
Definition netstatus.c:63
Header for netstatus.c.
#define SOCKET_OK(s)
Definition nettypes.h:39
Master header file for Tor-specific functionality.
#define ISO_DEFAULT
Definition or.h:978
#define SESSION_GROUP_CONTROL_RESOLVE
Definition or.h:985
#define TO_CONN(c)
Definition or.h:709
#define ENTRY_TO_CONN(c)
Definition or.h:712
int socks_policy_permits_address(const tor_addr_t *addr)
Definition policies.c:1063
Header file for policies.c.
Client request structure.
#define SOCKS_COMMAND_RESOLVE_PTR
#define SOCKS_COMMAND_RESOLVE
unsigned int type
tor_socket_t s
tor_addr_t addr
unsigned int is_dns_request
socks_request_t * socks_request
struct evdns_server_request * dns_server_request
struct evdns_server_port * dns_server_port
char address[MAX_SOCKS_ADDR_LEN]
#define tor_assert(expr)
Definition util_bug.h:103
#define tor_fragile_assert()
Definition util_bug.h:278