2 Unix SMB/CIFS implementation.
4 DNS server handler for queries
6 Copyright (C) 2010 Kai Blin <kai@samba.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "smbd/service_task.h"
24 #include "libcli/util/werror.h"
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/gen_ndr/ndr_dns.h"
27 #include "librpc/gen_ndr/ndr_dnsp.h"
29 #include "param/param.h"
30 #include "dsdb/samdb/samdb.h"
31 #include "dsdb/common/util.h"
32 #include "dns_server/dns_server.h"
33 #include "libcli/dns/libdns.h"
34 #include "lib/util/util_net.h"
35 #include "lib/util/tevent_werror.h"
37 static WERROR
create_response_rr(const struct dns_name_question
*question
,
38 const struct dnsp_DnssrvRpcRecord
*rec
,
39 struct dns_res_rec
**answers
, uint16_t *ancount
)
41 struct dns_res_rec
*ans
= *answers
;
42 uint16_t ai
= *ancount
;
50 ans
[ai
].rdata
.cname_record
= talloc_strdup(ans
, rec
->data
.cname
);
51 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.cname_record
);
54 ans
[ai
].rdata
.ipv4_record
= talloc_strdup(ans
, rec
->data
.ipv4
);
55 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.ipv4_record
);
58 ans
[ai
].rdata
.ipv6_record
= talloc_strdup(ans
, rec
->data
.ipv6
);
59 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.ipv6_record
);
62 ans
[ai
].rdata
.ns_record
= talloc_strdup(ans
, rec
->data
.ns
);
63 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.ns_record
);
66 ans
[ai
].rdata
.srv_record
.priority
= rec
->data
.srv
.wPriority
;
67 ans
[ai
].rdata
.srv_record
.weight
= rec
->data
.srv
.wWeight
;
68 ans
[ai
].rdata
.srv_record
.port
= rec
->data
.srv
.wPort
;
69 ans
[ai
].rdata
.srv_record
.target
= talloc_strdup(
70 ans
, rec
->data
.srv
.nameTarget
);
71 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.srv_record
.target
);
74 ans
[ai
].rdata
.soa_record
.mname
= talloc_strdup(
75 ans
, rec
->data
.soa
.mname
);
76 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.soa_record
.mname
);
77 ans
[ai
].rdata
.soa_record
.rname
= talloc_strdup(
78 ans
, rec
->data
.soa
.rname
);
79 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.soa_record
.rname
);
80 ans
[ai
].rdata
.soa_record
.serial
= rec
->data
.soa
.serial
;
81 ans
[ai
].rdata
.soa_record
.refresh
= rec
->data
.soa
.refresh
;
82 ans
[ai
].rdata
.soa_record
.retry
= rec
->data
.soa
.retry
;
83 ans
[ai
].rdata
.soa_record
.expire
= rec
->data
.soa
.expire
;
84 ans
[ai
].rdata
.soa_record
.minimum
= rec
->data
.soa
.minimum
;
87 ans
[ai
].rdata
.ptr_record
= talloc_strdup(ans
, rec
->data
.ptr
);
90 tmp
= talloc_asprintf(ans
, "\"%s\"", rec
->data
.txt
.str
[0]);
91 W_ERROR_HAVE_NO_MEMORY(tmp
);
92 for (i
=1; i
<rec
->data
.txt
.count
; i
++) {
93 tmp
= talloc_asprintf_append_buffer(
94 tmp
, " \"%s\"", rec
->data
.txt
.str
[i
]);
95 W_ERROR_HAVE_NO_MEMORY(tmp
);
97 ans
[ai
].rdata
.txt_record
.txt
= tmp
;
100 DEBUG(0, ("Got unhandled type %u query.\n", rec
->wType
));
101 return DNS_ERR(NOT_IMPLEMENTED
);
104 ans
[ai
].name
= talloc_strdup(ans
, question
->name
);
105 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].name
);
106 ans
[ai
].rr_type
= rec
->wType
;
107 ans
[ai
].rr_class
= DNS_QCLASS_IN
;
108 ans
[ai
].ttl
= rec
->dwTtlSeconds
;
109 ans
[ai
].length
= UINT16_MAX
;
118 struct ask_forwarder_state
{
119 struct tevent_context
*ev
;
121 struct dns_name_packet in_packet
;
124 static void ask_forwarder_done(struct tevent_req
*subreq
);
126 static struct tevent_req
*ask_forwarder_send(
127 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
128 const char *forwarder
, struct dns_name_question
*question
)
130 struct tevent_req
*req
, *subreq
;
131 struct ask_forwarder_state
*state
;
132 struct dns_name_packet out_packet
= { 0, };
134 enum ndr_err_code ndr_err
;
136 req
= tevent_req_create(mem_ctx
, &state
, struct ask_forwarder_state
);
141 generate_random_buffer((uint8_t *)&state
->id
, sizeof(state
->id
));
143 if (!is_ipaddress(forwarder
)) {
144 DEBUG(0, ("Invalid 'dns forwarder' setting '%s', needs to be "
145 "an IP address\n", forwarder
));
146 tevent_req_werror(req
, DNS_ERR(NAME_ERROR
));
147 return tevent_req_post(req
, ev
);
150 out_packet
.id
= state
->id
;
151 out_packet
.operation
|= DNS_OPCODE_QUERY
| DNS_FLAG_RECURSION_DESIRED
;
152 out_packet
.qdcount
= 1;
153 out_packet
.questions
= question
;
155 ndr_err
= ndr_push_struct_blob(
156 &out_blob
, state
, &out_packet
,
157 (ndr_push_flags_fn_t
)ndr_push_dns_name_packet
);
158 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
159 tevent_req_werror(req
, DNS_ERR(SERVER_FAILURE
));
160 return tevent_req_post(req
, ev
);
162 subreq
= dns_udp_request_send(state
, ev
, forwarder
, out_blob
.data
,
164 if (tevent_req_nomem(subreq
, req
)) {
165 return tevent_req_post(req
, ev
);
167 tevent_req_set_callback(subreq
, ask_forwarder_done
, req
);
171 static void ask_forwarder_done(struct tevent_req
*subreq
)
173 struct tevent_req
*req
= tevent_req_callback_data(
174 subreq
, struct tevent_req
);
175 struct ask_forwarder_state
*state
= tevent_req_data(
176 req
, struct ask_forwarder_state
);
178 enum ndr_err_code ndr_err
;
181 ret
= dns_udp_request_recv(subreq
, state
,
182 &in_blob
.data
, &in_blob
.length
);
184 if (tevent_req_werror(req
, ret
)) {
188 ndr_err
= ndr_pull_struct_blob(
189 &in_blob
, state
, &state
->in_packet
,
190 (ndr_pull_flags_fn_t
)ndr_pull_dns_name_packet
);
191 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
192 tevent_req_werror(req
, DNS_ERR(SERVER_FAILURE
));
195 if (state
->in_packet
.id
!= state
->id
) {
196 tevent_req_werror(req
, DNS_ERR(NAME_ERROR
));
199 tevent_req_done(req
);
202 static WERROR
ask_forwarder_recv(
203 struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
204 struct dns_res_rec
**answers
, uint16_t *ancount
,
205 struct dns_res_rec
**nsrecs
, uint16_t *nscount
,
206 struct dns_res_rec
**additional
, uint16_t *arcount
)
208 struct ask_forwarder_state
*state
= tevent_req_data(
209 req
, struct ask_forwarder_state
);
210 struct dns_name_packet
*in_packet
= &state
->in_packet
;
213 if (tevent_req_is_werror(req
, &err
)) {
217 *ancount
= in_packet
->ancount
;
218 *answers
= talloc_move(mem_ctx
, &in_packet
->answers
);
220 *nscount
= in_packet
->nscount
;
221 *nsrecs
= talloc_move(mem_ctx
, &in_packet
->nsrecs
);
223 *arcount
= in_packet
->arcount
;
224 *additional
= talloc_move(mem_ctx
, &in_packet
->additional
);
229 static WERROR
handle_question(struct dns_server
*dns
,
231 const struct dns_name_question
*question
,
232 struct dns_res_rec
**answers
, uint16_t *ancount
)
234 struct dns_res_rec
*ans
= *answers
;
237 struct dnsp_DnssrvRpcRecord
*recs
;
238 uint16_t rec_count
, ai
= *ancount
;
239 struct ldb_dn
*dn
= NULL
;
241 werror
= dns_name2dn(dns
, mem_ctx
, question
->name
, &dn
);
242 W_ERROR_NOT_OK_RETURN(werror
);
244 werror
= dns_lookup_records(dns
, mem_ctx
, dn
, &recs
, &rec_count
);
245 W_ERROR_NOT_OK_RETURN(werror
);
247 ans
= talloc_realloc(mem_ctx
, ans
, struct dns_res_rec
, rec_count
+ ai
);
252 for (ri
= 0; ri
< rec_count
; ri
++) {
253 if ((recs
[ri
].wType
== DNS_TYPE_CNAME
) &&
254 ((question
->question_type
== DNS_QTYPE_A
) ||
255 (question
->question_type
== DNS_QTYPE_AAAA
))) {
256 struct dns_name_question
*new_q
=
257 talloc(mem_ctx
, struct dns_name_question
);
263 /* We reply with one more record, so grow the array */
264 ans
= talloc_realloc(mem_ctx
, ans
, struct dns_res_rec
,
271 /* First put in the CNAME record */
272 werror
= create_response_rr(question
, &recs
[ri
], &ans
, &ai
);
273 if (!W_ERROR_IS_OK(werror
)) {
277 /* And then look up the name it points at.. */
279 /* First build up the new question */
280 new_q
->question_type
= question
->question_type
;
281 new_q
->question_class
= question
->question_class
;
282 if (new_q
->question_type
== DNS_QTYPE_A
) {
283 new_q
->name
= talloc_strdup(new_q
, recs
[ri
].data
.ipv4
);
284 } else if (new_q
->question_type
== DNS_QTYPE_AAAA
) {
285 new_q
->name
= talloc_strdup(new_q
, recs
[ri
].data
.ipv6
);
287 if (new_q
->name
== NULL
) {
291 /* and then call the lookup again */
292 werror
= handle_question(dns
, mem_ctx
, new_q
, &ans
, &ai
);
293 if (!W_ERROR_IS_OK(werror
)) {
300 if ((question
->question_type
!= DNS_QTYPE_ALL
) &&
301 (recs
[ri
].wType
!= question
->question_type
)) {
304 werror
= create_response_rr(question
, &recs
[ri
], &ans
, &ai
);
305 if (!W_ERROR_IS_OK(werror
)) {
311 return DNS_ERR(NAME_ERROR
);
320 struct dns_server_process_query_state
{
321 struct dns_res_rec
*answers
;
323 struct dns_res_rec
*nsrecs
;
325 struct dns_res_rec
*additional
;
329 static void dns_server_process_query_got_response(struct tevent_req
*subreq
);
331 struct tevent_req
*dns_server_process_query_send(
332 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
333 struct dns_server
*dns
, struct dns_request_state
*req_state
,
334 const struct dns_name_packet
*in
)
336 struct tevent_req
*req
, *subreq
;
337 struct dns_server_process_query_state
*state
;
339 req
= tevent_req_create(mem_ctx
, &state
,
340 struct dns_server_process_query_state
);
344 if (in
->qdcount
!= 1) {
345 tevent_req_werror(req
, DNS_ERR(FORMAT_ERROR
));
346 return tevent_req_post(req
, ev
);
349 /* Windows returns NOT_IMPLEMENTED on this as well */
350 if (in
->questions
[0].question_class
== DNS_QCLASS_NONE
) {
351 tevent_req_werror(req
, DNS_ERR(NOT_IMPLEMENTED
));
352 return tevent_req_post(req
, ev
);
355 if (dns_authorative_for_zone(dns
, in
->questions
[0].name
)) {
358 req_state
->flags
|= DNS_FLAG_AUTHORITATIVE
;
359 err
= handle_question(dns
, state
, &in
->questions
[0],
360 &state
->answers
, &state
->ancount
);
361 if (tevent_req_werror(req
, err
)) {
362 return tevent_req_post(req
, ev
);
364 tevent_req_done(req
);
365 return tevent_req_post(req
, ev
);
368 if ((req_state
->flags
& DNS_FLAG_RECURSION_DESIRED
) &&
369 (req_state
->flags
& DNS_FLAG_RECURSION_AVAIL
)) {
370 DEBUG(2, ("Not authoritative for '%s', forwarding\n",
371 in
->questions
[0].name
));
373 subreq
= ask_forwarder_send(
374 state
, ev
, lpcfg_dns_forwarder(dns
->task
->lp_ctx
),
376 if (tevent_req_nomem(subreq
, req
)) {
377 return tevent_req_post(req
, ev
);
379 tevent_req_set_callback(
380 subreq
, dns_server_process_query_got_response
, req
);
384 tevent_req_werror(req
, DNS_ERR(NAME_ERROR
));
385 return tevent_req_post(req
, ev
);
388 static void dns_server_process_query_got_response(struct tevent_req
*subreq
)
390 struct tevent_req
*req
= tevent_req_callback_data(
391 subreq
, struct tevent_req
);
392 struct dns_server_process_query_state
*state
= tevent_req_data(
393 req
, struct dns_server_process_query_state
);
396 err
= ask_forwarder_recv(subreq
, state
,
397 &state
->answers
, &state
->ancount
,
398 &state
->nsrecs
, &state
->nscount
,
399 &state
->additional
, &state
->arcount
);
401 if (tevent_req_werror(req
, err
)) {
404 tevent_req_done(req
);
407 WERROR
dns_server_process_query_recv(
408 struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
409 struct dns_res_rec
**answers
, uint16_t *ancount
,
410 struct dns_res_rec
**nsrecs
, uint16_t *nscount
,
411 struct dns_res_rec
**additional
, uint16_t *arcount
)
413 struct dns_server_process_query_state
*state
= tevent_req_data(
414 req
, struct dns_server_process_query_state
);
417 if (tevent_req_is_werror(req
, &err
)) {
420 *answers
= talloc_move(mem_ctx
, &state
->answers
);
421 *ancount
= state
->ancount
;
422 *nsrecs
= talloc_move(mem_ctx
, &state
->nsrecs
);
423 *nscount
= state
->nscount
;
424 *additional
= talloc_move(mem_ctx
, &state
->additional
);
425 *arcount
= state
->arcount
;