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/dlinklist.h"
35 #include "lib/util/util_net.h"
36 #include "lib/util/tevent_werror.h"
37 #include "auth/auth.h"
38 #include "auth/credentials/credentials.h"
39 #include "auth/gensec/gensec.h"
42 #define DBGC_CLASS DBGC_DNS
44 struct forwarder_string
{
45 const char *forwarder
;
46 struct forwarder_string
*prev
, *next
;
49 static WERROR
add_response_rr(const char *name
,
50 const struct dnsp_DnssrvRpcRecord
*rec
,
51 struct dns_res_rec
**answers
)
53 struct dns_res_rec
*ans
= *answers
;
54 uint16_t ai
= talloc_array_length(ans
);
55 enum ndr_err_code ndr_err
;
57 if (ai
== UINT16_MAX
) {
58 return WERR_BUFFER_OVERFLOW
;
62 * "ans" is always non-NULL and thus its own talloc context
64 ans
= talloc_realloc(ans
, ans
, struct dns_res_rec
, ai
+1);
66 return WERR_NOT_ENOUGH_MEMORY
;
73 ans
[ai
].rdata
.cname_record
= talloc_strdup(ans
, rec
->data
.cname
);
74 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.cname_record
);
77 ans
[ai
].rdata
.ipv4_record
= talloc_strdup(ans
, rec
->data
.ipv4
);
78 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.ipv4_record
);
81 ans
[ai
].rdata
.ipv6_record
= talloc_strdup(ans
, rec
->data
.ipv6
);
82 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.ipv6_record
);
85 ans
[ai
].rdata
.ns_record
= talloc_strdup(ans
, rec
->data
.ns
);
86 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.ns_record
);
89 ans
[ai
].rdata
.srv_record
.priority
= rec
->data
.srv
.wPriority
;
90 ans
[ai
].rdata
.srv_record
.weight
= rec
->data
.srv
.wWeight
;
91 ans
[ai
].rdata
.srv_record
.port
= rec
->data
.srv
.wPort
;
92 ans
[ai
].rdata
.srv_record
.target
= talloc_strdup(
93 ans
, rec
->data
.srv
.nameTarget
);
94 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.srv_record
.target
);
97 ans
[ai
].rdata
.soa_record
.mname
= talloc_strdup(
98 ans
, rec
->data
.soa
.mname
);
99 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.soa_record
.mname
);
100 ans
[ai
].rdata
.soa_record
.rname
= talloc_strdup(
101 ans
, rec
->data
.soa
.rname
);
102 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.soa_record
.rname
);
103 ans
[ai
].rdata
.soa_record
.serial
= rec
->data
.soa
.serial
;
104 ans
[ai
].rdata
.soa_record
.refresh
= rec
->data
.soa
.refresh
;
105 ans
[ai
].rdata
.soa_record
.retry
= rec
->data
.soa
.retry
;
106 ans
[ai
].rdata
.soa_record
.expire
= rec
->data
.soa
.expire
;
107 ans
[ai
].rdata
.soa_record
.minimum
= rec
->data
.soa
.minimum
;
110 ans
[ai
].rdata
.ptr_record
= talloc_strdup(ans
, rec
->data
.ptr
);
111 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.ptr_record
);
114 ans
[ai
].rdata
.mx_record
.preference
= rec
->data
.mx
.wPriority
;
115 ans
[ai
].rdata
.mx_record
.exchange
= talloc_strdup(
116 ans
, rec
->data
.mx
.nameTarget
);
117 if (ans
[ai
].rdata
.mx_record
.exchange
== NULL
) {
118 return WERR_NOT_ENOUGH_MEMORY
;
122 ndr_err
= ndr_dnsp_string_list_copy(ans
,
124 &ans
[ai
].rdata
.txt_record
.txt
);
125 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
126 return WERR_NOT_ENOUGH_MEMORY
;
130 DEBUG(0, ("Got unhandled type %u query.\n", rec
->wType
));
131 return DNS_ERR(NOT_IMPLEMENTED
);
134 ans
[ai
].name
= talloc_strdup(ans
, name
);
135 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].name
);
136 ans
[ai
].rr_type
= rec
->wType
;
137 ans
[ai
].rr_class
= DNS_QCLASS_IN
;
138 ans
[ai
].ttl
= rec
->dwTtlSeconds
;
139 ans
[ai
].length
= UINT16_MAX
;
146 static WERROR
add_dns_res_rec(struct dns_res_rec
**pdst
,
147 const struct dns_res_rec
*src
)
149 struct dns_res_rec
*dst
= *pdst
;
150 uint16_t di
= talloc_array_length(dst
);
151 enum ndr_err_code ndr_err
;
153 if (di
== UINT16_MAX
) {
154 return WERR_BUFFER_OVERFLOW
;
157 dst
= talloc_realloc(dst
, dst
, struct dns_res_rec
, di
+1);
159 return WERR_NOT_ENOUGH_MEMORY
;
162 ZERO_STRUCT(dst
[di
]);
164 dst
[di
] = (struct dns_res_rec
) {
165 .name
= talloc_strdup(dst
, src
->name
),
166 .rr_type
= src
->rr_type
,
167 .rr_class
= src
->rr_class
,
169 .length
= src
->length
172 if (dst
[di
].name
== NULL
) {
173 return WERR_NOT_ENOUGH_MEMORY
;
176 switch (src
->rr_type
) {
177 case DNS_QTYPE_CNAME
:
178 dst
[di
].rdata
.cname_record
= talloc_strdup(
179 dst
, src
->rdata
.cname_record
);
180 if (dst
[di
].rdata
.cname_record
== NULL
) {
181 return WERR_NOT_ENOUGH_MEMORY
;
185 dst
[di
].rdata
.ipv4_record
= talloc_strdup(
186 dst
, src
->rdata
.ipv4_record
);
187 if (dst
[di
].rdata
.ipv4_record
== NULL
) {
188 return WERR_NOT_ENOUGH_MEMORY
;
192 dst
[di
].rdata
.ipv6_record
= talloc_strdup(
193 dst
, src
->rdata
.ipv6_record
);
194 if (dst
[di
].rdata
.ipv6_record
== NULL
) {
195 return WERR_NOT_ENOUGH_MEMORY
;
199 dst
[di
].rdata
.ns_record
= talloc_strdup(
200 dst
, src
->rdata
.ns_record
);
201 if (dst
[di
].rdata
.ns_record
== NULL
) {
202 return WERR_NOT_ENOUGH_MEMORY
;
206 dst
[di
].rdata
.srv_record
= (struct dns_srv_record
) {
207 .priority
= src
->rdata
.srv_record
.priority
,
208 .weight
= src
->rdata
.srv_record
.weight
,
209 .port
= src
->rdata
.srv_record
.port
,
210 .target
= talloc_strdup(
211 dst
, src
->rdata
.srv_record
.target
)
213 if (dst
[di
].rdata
.srv_record
.target
== NULL
) {
214 return WERR_NOT_ENOUGH_MEMORY
;
218 dst
[di
].rdata
.soa_record
= (struct dns_soa_record
) {
219 .mname
= talloc_strdup(
220 dst
, src
->rdata
.soa_record
.mname
),
221 .rname
= talloc_strdup(
222 dst
, src
->rdata
.soa_record
.rname
),
223 .serial
= src
->rdata
.soa_record
.serial
,
224 .refresh
= src
->rdata
.soa_record
.refresh
,
225 .retry
= src
->rdata
.soa_record
.retry
,
226 .expire
= src
->rdata
.soa_record
.expire
,
227 .minimum
= src
->rdata
.soa_record
.minimum
230 if ((dst
[di
].rdata
.soa_record
.mname
== NULL
) ||
231 (dst
[di
].rdata
.soa_record
.rname
== NULL
)) {
232 return WERR_NOT_ENOUGH_MEMORY
;
237 dst
[di
].rdata
.ptr_record
= talloc_strdup(
238 dst
, src
->rdata
.ptr_record
);
239 if (dst
[di
].rdata
.ptr_record
== NULL
) {
240 return WERR_NOT_ENOUGH_MEMORY
;
244 dst
[di
].rdata
.mx_record
= (struct dns_mx_record
) {
245 .preference
= src
->rdata
.mx_record
.preference
,
246 .exchange
= talloc_strdup(
247 src
, src
->rdata
.mx_record
.exchange
)
250 if (dst
[di
].rdata
.mx_record
.exchange
== NULL
) {
251 return WERR_NOT_ENOUGH_MEMORY
;
255 ndr_err
= ndr_dnsp_string_list_copy(dst
,
256 &src
->rdata
.txt_record
.txt
,
257 &dst
[di
].rdata
.txt_record
.txt
);
258 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
259 return WERR_NOT_ENOUGH_MEMORY
;
263 DBG_WARNING("Got unhandled type %u query.\n", src
->rr_type
);
264 return DNS_ERR(NOT_IMPLEMENTED
);
272 struct ask_forwarder_state
{
273 struct tevent_context
*ev
;
275 struct dns_name_packet in_packet
;
278 static void ask_forwarder_done(struct tevent_req
*subreq
);
280 static struct tevent_req
*ask_forwarder_send(
281 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
282 struct dns_server
*dns
,
283 const char *forwarder
, struct dns_name_question
*question
)
285 struct tevent_req
*req
, *subreq
;
286 struct ask_forwarder_state
*state
;
287 struct dns_res_rec
*options
;
288 struct dns_name_packet out_packet
= { 0, };
290 enum ndr_err_code ndr_err
;
293 req
= tevent_req_create(mem_ctx
, &state
, struct ask_forwarder_state
);
298 generate_random_buffer((uint8_t *)&state
->id
, sizeof(state
->id
));
300 if (!is_ipaddress(forwarder
)) {
301 DEBUG(0, ("Invalid 'dns forwarder' setting '%s', needs to be "
302 "an IP address\n", forwarder
));
303 tevent_req_werror(req
, DNS_ERR(NAME_ERROR
));
304 return tevent_req_post(req
, ev
);
307 out_packet
.id
= state
->id
;
308 out_packet
.operation
|= DNS_OPCODE_QUERY
| DNS_FLAG_RECURSION_DESIRED
;
309 out_packet
.qdcount
= 1;
310 out_packet
.questions
= question
;
312 werr
= dns_generate_options(dns
, state
, &options
);
313 if (!W_ERROR_IS_OK(werr
)) {
314 tevent_req_werror(req
, werr
);
315 return tevent_req_post(req
, ev
);
318 out_packet
.arcount
= 1;
319 out_packet
.additional
= options
;
321 ndr_err
= ndr_push_struct_blob(
322 &out_blob
, state
, &out_packet
,
323 (ndr_push_flags_fn_t
)ndr_push_dns_name_packet
);
324 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
325 tevent_req_werror(req
, DNS_ERR(SERVER_FAILURE
));
326 return tevent_req_post(req
, ev
);
328 subreq
= dns_udp_request_send(state
, ev
, forwarder
, out_blob
.data
,
330 if (tevent_req_nomem(subreq
, req
)) {
331 return tevent_req_post(req
, ev
);
333 tevent_req_set_callback(subreq
, ask_forwarder_done
, req
);
337 static void ask_forwarder_done(struct tevent_req
*subreq
)
339 struct tevent_req
*req
= tevent_req_callback_data(
340 subreq
, struct tevent_req
);
341 struct ask_forwarder_state
*state
= tevent_req_data(
342 req
, struct ask_forwarder_state
);
344 enum ndr_err_code ndr_err
;
347 ret
= dns_udp_request_recv(subreq
, state
,
348 &in_blob
.data
, &in_blob
.length
);
352 tevent_req_werror(req
, unix_to_werror(ret
));
356 ndr_err
= ndr_pull_struct_blob(
357 &in_blob
, state
, &state
->in_packet
,
358 (ndr_pull_flags_fn_t
)ndr_pull_dns_name_packet
);
359 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
360 tevent_req_werror(req
, DNS_ERR(SERVER_FAILURE
));
363 if (state
->in_packet
.id
!= state
->id
) {
364 tevent_req_werror(req
, DNS_ERR(NAME_ERROR
));
367 tevent_req_done(req
);
370 static WERROR
ask_forwarder_recv(
371 struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
372 struct dns_res_rec
**answers
, uint16_t *ancount
,
373 struct dns_res_rec
**nsrecs
, uint16_t *nscount
,
374 struct dns_res_rec
**additional
, uint16_t *arcount
)
376 struct ask_forwarder_state
*state
= tevent_req_data(
377 req
, struct ask_forwarder_state
);
378 struct dns_name_packet
*in_packet
= &state
->in_packet
;
381 if (tevent_req_is_werror(req
, &err
)) {
385 *ancount
= in_packet
->ancount
;
386 *answers
= talloc_move(mem_ctx
, &in_packet
->answers
);
388 *nscount
= in_packet
->nscount
;
389 *nsrecs
= talloc_move(mem_ctx
, &in_packet
->nsrecs
);
391 *arcount
= in_packet
->arcount
;
392 *additional
= talloc_move(mem_ctx
, &in_packet
->additional
);
397 static WERROR
add_zone_authority_record(struct dns_server
*dns
,
399 const struct dns_name_question
*question
,
400 struct dns_res_rec
**nsrecs
)
402 const char *zone
= NULL
;
403 struct dnsp_DnssrvRpcRecord
*recs
;
404 struct dns_res_rec
*ns
= *nsrecs
;
406 struct ldb_dn
*dn
= NULL
;
410 zone
= dns_get_authoritative_zone(dns
, question
->name
);
411 DEBUG(10, ("Creating zone authority record for '%s'\n", zone
));
413 werror
= dns_name2dn(dns
, mem_ctx
, zone
, &dn
);
414 if (!W_ERROR_IS_OK(werror
)) {
418 werror
= dns_lookup_records(dns
, mem_ctx
, dn
, &recs
, &rec_count
);
419 if (!W_ERROR_IS_OK(werror
)) {
423 for (ri
= 0; ri
< rec_count
; ri
++) {
424 if (recs
[ri
].wType
== DNS_TYPE_SOA
) {
425 werror
= add_response_rr(zone
, &recs
[ri
], &ns
);
426 if (!W_ERROR_IS_OK(werror
)) {
437 static struct tevent_req
*handle_authoritative_send(
438 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
439 struct dns_server
*dns
, const char *forwarder
,
440 struct dns_name_question
*question
,
441 struct dns_res_rec
**answers
, struct dns_res_rec
**nsrecs
);
442 static WERROR
handle_authoritative_recv(struct tevent_req
*req
);
444 struct handle_dnsrpcrec_state
{
445 struct dns_res_rec
**answers
;
446 struct dns_res_rec
**nsrecs
;
449 static void handle_dnsrpcrec_gotauth(struct tevent_req
*subreq
);
450 static void handle_dnsrpcrec_gotforwarded(struct tevent_req
*subreq
);
452 static struct tevent_req
*handle_dnsrpcrec_send(
453 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
454 struct dns_server
*dns
, const char *forwarder
,
455 const struct dns_name_question
*question
,
456 struct dnsp_DnssrvRpcRecord
*rec
,
457 struct dns_res_rec
**answers
, struct dns_res_rec
**nsrecs
)
459 struct tevent_req
*req
, *subreq
;
460 struct handle_dnsrpcrec_state
*state
;
461 struct dns_name_question
*new_q
;
465 req
= tevent_req_create(mem_ctx
, &state
,
466 struct handle_dnsrpcrec_state
);
470 state
->answers
= answers
;
471 state
->nsrecs
= nsrecs
;
473 resolve_cname
= ((rec
->wType
== DNS_TYPE_CNAME
) &&
474 ((question
->question_type
== DNS_QTYPE_A
) ||
475 (question
->question_type
== DNS_QTYPE_AAAA
)));
477 if (!resolve_cname
) {
478 if ((question
->question_type
!= DNS_QTYPE_ALL
) &&
480 (enum dns_record_type
) question
->question_type
)) {
481 tevent_req_done(req
);
482 return tevent_req_post(req
, ev
);
485 werr
= add_response_rr(question
->name
, rec
, state
->answers
);
486 if (tevent_req_werror(req
, werr
)) {
487 return tevent_req_post(req
, ev
);
490 tevent_req_done(req
);
491 return tevent_req_post(req
, ev
);
494 werr
= add_response_rr(question
->name
, rec
, state
->answers
);
495 if (tevent_req_werror(req
, werr
)) {
496 return tevent_req_post(req
, ev
);
499 new_q
= talloc(state
, struct dns_name_question
);
500 if (tevent_req_nomem(new_q
, req
)) {
501 return tevent_req_post(req
, ev
);
504 *new_q
= (struct dns_name_question
) {
505 .question_type
= question
->question_type
,
506 .question_class
= question
->question_class
,
507 .name
= rec
->data
.cname
510 if (dns_authoritative_for_zone(dns
, new_q
->name
)) {
511 subreq
= handle_authoritative_send(
512 state
, ev
, dns
, forwarder
, new_q
,
513 state
->answers
, state
->nsrecs
);
514 if (tevent_req_nomem(subreq
, req
)) {
515 return tevent_req_post(req
, ev
);
517 tevent_req_set_callback(subreq
, handle_dnsrpcrec_gotauth
, req
);
521 subreq
= ask_forwarder_send(state
, ev
, dns
, forwarder
, new_q
);
522 if (tevent_req_nomem(subreq
, req
)) {
523 return tevent_req_post(req
, ev
);
525 tevent_req_set_callback(subreq
, handle_dnsrpcrec_gotforwarded
, req
);
530 static void handle_dnsrpcrec_gotauth(struct tevent_req
*subreq
)
532 struct tevent_req
*req
= tevent_req_callback_data(
533 subreq
, struct tevent_req
);
536 werr
= handle_authoritative_recv(subreq
);
538 if (tevent_req_werror(req
, werr
)) {
541 tevent_req_done(req
);
544 static void handle_dnsrpcrec_gotforwarded(struct tevent_req
*subreq
)
546 struct tevent_req
*req
= tevent_req_callback_data(
547 subreq
, struct tevent_req
);
548 struct handle_dnsrpcrec_state
*state
= tevent_req_data(
549 req
, struct handle_dnsrpcrec_state
);
550 struct dns_res_rec
*answers
, *nsrecs
, *additional
;
551 uint16_t ancount
= 0;
552 uint16_t nscount
= 0;
553 uint16_t arcount
= 0;
557 werr
= ask_forwarder_recv(subreq
, state
, &answers
, &ancount
,
558 &nsrecs
, &nscount
, &additional
, &arcount
);
559 if (tevent_req_werror(req
, werr
)) {
563 for (i
=0; i
<ancount
; i
++) {
564 werr
= add_dns_res_rec(state
->answers
, &answers
[i
]);
565 if (tevent_req_werror(req
, werr
)) {
570 for (i
=0; i
<nscount
; i
++) {
571 werr
= add_dns_res_rec(state
->nsrecs
, &nsrecs
[i
]);
572 if (tevent_req_werror(req
, werr
)) {
577 tevent_req_done(req
);
580 static WERROR
handle_dnsrpcrec_recv(struct tevent_req
*req
)
582 return tevent_req_simple_recv_werror(req
);
585 struct handle_authoritative_state
{
586 struct tevent_context
*ev
;
587 struct dns_server
*dns
;
588 struct dns_name_question
*question
;
589 const char *forwarder
;
591 struct dnsp_DnssrvRpcRecord
*recs
;
595 struct dns_res_rec
**answers
;
596 struct dns_res_rec
**nsrecs
;
599 static void handle_authoritative_done(struct tevent_req
*subreq
);
601 static struct tevent_req
*handle_authoritative_send(
602 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
603 struct dns_server
*dns
, const char *forwarder
,
604 struct dns_name_question
*question
,
605 struct dns_res_rec
**answers
, struct dns_res_rec
**nsrecs
)
607 struct tevent_req
*req
, *subreq
;
608 struct handle_authoritative_state
*state
;
609 struct ldb_dn
*dn
= NULL
;
612 req
= tevent_req_create(mem_ctx
, &state
,
613 struct handle_authoritative_state
);
619 state
->question
= question
;
620 state
->forwarder
= forwarder
;
621 state
->answers
= answers
;
622 state
->nsrecs
= nsrecs
;
624 werr
= dns_name2dn(dns
, state
, question
->name
, &dn
);
625 if (tevent_req_werror(req
, werr
)) {
626 return tevent_req_post(req
, ev
);
628 werr
= dns_lookup_records_wildcard(dns
, state
, dn
, &state
->recs
,
631 if (tevent_req_werror(req
, werr
)) {
632 return tevent_req_post(req
, ev
);
635 if (state
->rec_count
== 0) {
636 tevent_req_werror(req
, DNS_ERR(NAME_ERROR
));
637 return tevent_req_post(req
, ev
);
640 subreq
= handle_dnsrpcrec_send(
641 state
, state
->ev
, state
->dns
, state
->forwarder
,
642 state
->question
, &state
->recs
[state
->recs_done
],
643 state
->answers
, state
->nsrecs
);
644 if (tevent_req_nomem(subreq
, req
)) {
645 return tevent_req_post(req
, ev
);
647 tevent_req_set_callback(subreq
, handle_authoritative_done
, req
);
651 static void handle_authoritative_done(struct tevent_req
*subreq
)
653 struct tevent_req
*req
= tevent_req_callback_data(
654 subreq
, struct tevent_req
);
655 struct handle_authoritative_state
*state
= tevent_req_data(
656 req
, struct handle_authoritative_state
);
659 werr
= handle_dnsrpcrec_recv(subreq
);
661 if (tevent_req_werror(req
, werr
)) {
665 state
->recs_done
+= 1;
667 if (state
->recs_done
== state
->rec_count
) {
668 tevent_req_done(req
);
672 subreq
= handle_dnsrpcrec_send(
673 state
, state
->ev
, state
->dns
, state
->forwarder
,
674 state
->question
, &state
->recs
[state
->recs_done
],
675 state
->answers
, state
->nsrecs
);
676 if (tevent_req_nomem(subreq
, req
)) {
679 tevent_req_set_callback(subreq
, handle_authoritative_done
, req
);
682 static WERROR
handle_authoritative_recv(struct tevent_req
*req
)
684 struct handle_authoritative_state
*state
= tevent_req_data(
685 req
, struct handle_authoritative_state
);
688 if (tevent_req_is_werror(req
, &werr
)) {
692 werr
= add_zone_authority_record(state
->dns
, state
, state
->question
,
694 if (!W_ERROR_IS_OK(werr
)) {
701 static NTSTATUS
create_tkey(struct dns_server
*dns
,
703 const char* algorithm
,
704 const struct tsocket_address
*remote_address
,
705 const struct tsocket_address
*local_address
,
706 struct dns_server_tkey
**tkey
)
709 struct dns_server_tkey_store
*store
= dns
->tkeys
;
710 struct dns_server_tkey
*k
= talloc_zero(store
, struct dns_server_tkey
);
713 return NT_STATUS_NO_MEMORY
;
716 k
->name
= talloc_strdup(k
, name
);
718 if (k
->name
== NULL
) {
719 return NT_STATUS_NO_MEMORY
;
722 k
->algorithm
= talloc_strdup(k
, algorithm
);
723 if (k
->algorithm
== NULL
) {
724 return NT_STATUS_NO_MEMORY
;
728 * We only allow SPNEGO/KRB5 currently
729 * and rely on the backend to be RPC/IPC free.
731 * It allows gensec_update() not to block.
733 status
= samba_server_gensec_krb5_start(k
,
734 dns
->task
->event_ctx
,
737 dns
->server_credentials
,
740 if (!NT_STATUS_IS_OK(status
)) {
741 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status
)));
746 gensec_want_feature(k
->gensec
, GENSEC_FEATURE_SIGN
);
748 status
= gensec_set_remote_address(k
->gensec
,
750 if (!NT_STATUS_IS_OK(status
)) {
751 DEBUG(1, ("Failed to set remote address into GENSEC: %s\n",
757 status
= gensec_set_local_address(k
->gensec
,
759 if (!NT_STATUS_IS_OK(status
)) {
760 DEBUG(1, ("Failed to set local address into GENSEC: %s\n",
766 status
= gensec_start_mech_by_oid(k
->gensec
, GENSEC_OID_SPNEGO
);
768 if (!NT_STATUS_IS_OK(status
)) {
769 DEBUG(1, ("Failed to start GENSEC server code: %s\n",
775 if (store
->tkeys
[store
->next_idx
] != NULL
) {
776 TALLOC_FREE(store
->tkeys
[store
->next_idx
]);
779 store
->tkeys
[store
->next_idx
] = k
;
781 store
->next_idx
%= store
->size
;
787 static NTSTATUS
accept_gss_ticket(TALLOC_CTX
*mem_ctx
,
788 struct dns_server
*dns
,
789 struct dns_server_tkey
*tkey
,
790 const DATA_BLOB
*key
,
792 uint16_t *dns_auth_error
)
797 * We use samba_server_gensec_krb5_start(),
798 * which only allows SPNEGO/KRB5 currently
799 * and makes sure the backend to be RPC/IPC free.
801 * See gensec_gssapi_update_internal() as
804 * It allows gensec_update() not to block.
806 * If that changes in future we need to use
807 * gensec_update_send/recv here!
809 status
= gensec_update(tkey
->gensec
, mem_ctx
,
812 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED
, status
)) {
813 *dns_auth_error
= DNS_RCODE_OK
;
817 if (NT_STATUS_IS_OK(status
)) {
819 status
= gensec_session_info(tkey
->gensec
, tkey
, &tkey
->session_info
);
820 if (!NT_STATUS_IS_OK(status
)) {
821 *dns_auth_error
= DNS_RCODE_BADKEY
;
824 *dns_auth_error
= DNS_RCODE_OK
;
830 static WERROR
handle_tkey(struct dns_server
*dns
,
832 const struct dns_name_packet
*in
,
833 struct dns_request_state
*state
,
834 struct dns_res_rec
**answers
,
837 struct dns_res_rec
*in_tkey
= NULL
;
838 struct dns_res_rec
*ret_tkey
;
841 for (i
= 0; i
< in
->arcount
; i
++) {
842 if (in
->additional
[i
].rr_type
== DNS_QTYPE_TKEY
) {
843 in_tkey
= &in
->additional
[i
];
848 /* If this is a TKEY query, it should have a TKEY RR.
849 * Behaviour is not really specified in RFC 2930 or RFC 3645, but
850 * FORMAT_ERROR seems to be what BIND uses .*/
851 if (in_tkey
== NULL
) {
852 return DNS_ERR(FORMAT_ERROR
);
855 ret_tkey
= talloc_zero(mem_ctx
, struct dns_res_rec
);
856 if (ret_tkey
== NULL
) {
857 return WERR_NOT_ENOUGH_MEMORY
;
860 ret_tkey
->name
= talloc_strdup(ret_tkey
, in_tkey
->name
);
861 if (ret_tkey
->name
== NULL
) {
862 return WERR_NOT_ENOUGH_MEMORY
;
865 ret_tkey
->rr_type
= DNS_QTYPE_TKEY
;
866 ret_tkey
->rr_class
= DNS_QCLASS_ANY
;
867 ret_tkey
->length
= UINT16_MAX
;
869 ret_tkey
->rdata
.tkey_record
.algorithm
= talloc_strdup(ret_tkey
,
870 in_tkey
->rdata
.tkey_record
.algorithm
);
871 if (ret_tkey
->rdata
.tkey_record
.algorithm
== NULL
) {
872 return WERR_NOT_ENOUGH_MEMORY
;
875 ret_tkey
->rdata
.tkey_record
.inception
= in_tkey
->rdata
.tkey_record
.inception
;
876 ret_tkey
->rdata
.tkey_record
.expiration
= in_tkey
->rdata
.tkey_record
.expiration
;
877 ret_tkey
->rdata
.tkey_record
.mode
= in_tkey
->rdata
.tkey_record
.mode
;
879 switch (in_tkey
->rdata
.tkey_record
.mode
) {
880 case DNS_TKEY_MODE_DH
:
881 /* FIXME: According to RFC 2930, we MUST support this, but we don't.
882 * Still, claim it's a bad key instead of a bad mode */
883 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_BADKEY
;
885 case DNS_TKEY_MODE_GSSAPI
: {
887 struct dns_server_tkey
*tkey
;
891 tkey
= dns_find_tkey(dns
->tkeys
, in
->questions
[0].name
);
892 if (tkey
!= NULL
&& tkey
->complete
) {
893 /* TODO: check if the key is still valid */
894 DEBUG(1, ("Rejecting tkey negotiation for already established key\n"));
895 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_BADNAME
;
900 status
= create_tkey(dns
, in
->questions
[0].name
,
901 in_tkey
->rdata
.tkey_record
.algorithm
,
902 state
->remote_address
,
903 state
->local_address
,
905 if (!NT_STATUS_IS_OK(status
)) {
906 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_BADKEY
;
907 return ntstatus_to_werror(status
);
911 key
.data
= in_tkey
->rdata
.tkey_record
.key_data
;
912 key
.length
= in_tkey
->rdata
.tkey_record
.key_size
;
914 status
= accept_gss_ticket(ret_tkey
, dns
, tkey
, &key
, &reply
,
915 &ret_tkey
->rdata
.tkey_record
.error
);
916 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
917 DEBUG(1, ("More processing required\n"));
918 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_BADKEY
;
919 } else if (NT_STATUS_IS_OK(status
)) {
920 DEBUG(1, ("Tkey handshake completed\n"));
921 ret_tkey
->rdata
.tkey_record
.key_size
= reply
.length
;
922 ret_tkey
->rdata
.tkey_record
.key_data
= talloc_memdup(ret_tkey
,
926 state
->key_name
= talloc_strdup(state
->mem_ctx
, tkey
->name
);
927 if (state
->key_name
== NULL
) {
928 return WERR_NOT_ENOUGH_MEMORY
;
931 DEBUG(1, ("GSS key negotiation returned %s\n", nt_errstr(status
)));
932 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_BADKEY
;
937 case DNS_TKEY_MODE_DELETE
:
938 /* TODO: implement me */
939 DEBUG(1, ("Should delete tkey here\n"));
940 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_OK
;
942 case DNS_TKEY_MODE_NULL
:
943 case DNS_TKEY_MODE_SERVER
:
944 case DNS_TKEY_MODE_CLIENT
:
945 case DNS_TKEY_MODE_LAST
:
946 /* We don't have to implement these, return a mode error */
947 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_BADMODE
;
950 DEBUG(1, ("Unsupported TKEY mode %d\n",
951 in_tkey
->rdata
.tkey_record
.mode
));
960 struct dns_server_process_query_state
{
961 struct tevent_context
*ev
;
962 struct dns_server
*dns
;
963 struct dns_name_question
*question
;
965 struct dns_res_rec
*answers
;
967 struct dns_res_rec
*nsrecs
;
969 struct dns_res_rec
*additional
;
971 struct forwarder_string
*forwarders
;
974 static void dns_server_process_query_got_auth(struct tevent_req
*subreq
);
975 static void dns_server_process_query_got_response(struct tevent_req
*subreq
);
977 struct tevent_req
*dns_server_process_query_send(
978 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
979 struct dns_server
*dns
, struct dns_request_state
*req_state
,
980 const struct dns_name_packet
*in
)
982 struct tevent_req
*req
, *subreq
;
983 struct dns_server_process_query_state
*state
;
984 const char **forwarders
= NULL
;
987 req
= tevent_req_create(mem_ctx
, &state
,
988 struct dns_server_process_query_state
);
992 if (in
->qdcount
!= 1) {
993 tevent_req_werror(req
, DNS_ERR(FORMAT_ERROR
));
994 return tevent_req_post(req
, ev
);
997 /* Windows returns NOT_IMPLEMENTED on this as well */
998 if (in
->questions
[0].question_class
== DNS_QCLASS_NONE
) {
999 tevent_req_werror(req
, DNS_ERR(NOT_IMPLEMENTED
));
1000 return tevent_req_post(req
, ev
);
1003 if (in
->questions
[0].question_type
== DNS_QTYPE_TKEY
) {
1006 err
= handle_tkey(dns
, state
, in
, req_state
,
1007 &state
->answers
, &state
->ancount
);
1008 if (tevent_req_werror(req
, err
)) {
1009 return tevent_req_post(req
, ev
);
1011 tevent_req_done(req
);
1012 return tevent_req_post(req
, ev
);
1017 state
->question
= &in
->questions
[0];
1019 forwarders
= lpcfg_dns_forwarder(dns
->task
->lp_ctx
);
1020 for (i
= 0; forwarders
!= NULL
&& forwarders
[i
] != NULL
; i
++) {
1021 struct forwarder_string
*f
= talloc_zero(state
,
1022 struct forwarder_string
);
1023 f
->forwarder
= forwarders
[i
];
1024 DLIST_ADD_END(state
->forwarders
, f
);
1027 if (dns_authoritative_for_zone(dns
, in
->questions
[0].name
)) {
1029 req_state
->flags
|= DNS_FLAG_AUTHORITATIVE
;
1032 * Initialize the response arrays, so that we can use
1033 * them as their own talloc contexts when doing the
1036 state
->answers
= talloc_array(state
, struct dns_res_rec
, 0);
1037 if (tevent_req_nomem(state
->answers
, req
)) {
1038 return tevent_req_post(req
, ev
);
1040 state
->nsrecs
= talloc_array(state
, struct dns_res_rec
, 0);
1041 if (tevent_req_nomem(state
->nsrecs
, req
)) {
1042 return tevent_req_post(req
, ev
);
1045 subreq
= handle_authoritative_send(
1046 state
, ev
, dns
, (forwarders
== NULL
? NULL
: forwarders
[0]),
1047 &in
->questions
[0], &state
->answers
, &state
->nsrecs
);
1048 if (tevent_req_nomem(subreq
, req
)) {
1049 return tevent_req_post(req
, ev
);
1051 tevent_req_set_callback(
1052 subreq
, dns_server_process_query_got_auth
, req
);
1056 if ((req_state
->flags
& DNS_FLAG_RECURSION_DESIRED
) &&
1057 (req_state
->flags
& DNS_FLAG_RECURSION_AVAIL
)) {
1058 DEBUG(5, ("Not authoritative for '%s', forwarding\n",
1059 in
->questions
[0].name
));
1061 subreq
= ask_forwarder_send(state
, ev
, dns
,
1062 (forwarders
== NULL
? NULL
: forwarders
[0]),
1064 if (tevent_req_nomem(subreq
, req
)) {
1065 return tevent_req_post(req
, ev
);
1067 tevent_req_set_callback(
1068 subreq
, dns_server_process_query_got_response
, req
);
1072 tevent_req_werror(req
, DNS_ERR(NAME_ERROR
));
1073 return tevent_req_post(req
, ev
);
1076 static void dns_server_process_query_got_response(struct tevent_req
*subreq
)
1078 struct tevent_req
*req
= tevent_req_callback_data(
1079 subreq
, struct tevent_req
);
1080 struct dns_server_process_query_state
*state
= tevent_req_data(
1081 req
, struct dns_server_process_query_state
);
1084 werr
= ask_forwarder_recv(subreq
, state
,
1085 &state
->answers
, &state
->ancount
,
1086 &state
->nsrecs
, &state
->nscount
,
1087 &state
->additional
, &state
->arcount
);
1088 TALLOC_FREE(subreq
);
1090 /* If you get an error, attempt a different forwarder */
1091 if (!W_ERROR_IS_OK(werr
)) {
1092 if (state
->forwarders
!= NULL
) {
1093 DLIST_REMOVE(state
->forwarders
, state
->forwarders
);
1096 /* If you have run out of forwarders, simply finish */
1097 if (state
->forwarders
== NULL
) {
1098 tevent_req_werror(req
, werr
);
1102 DEBUG(5, ("DNS query returned %s, trying another forwarder.\n",
1104 subreq
= ask_forwarder_send(state
, state
->ev
, state
->dns
,
1105 state
->forwarders
->forwarder
,
1108 if (tevent_req_nomem(subreq
, req
)) {
1112 tevent_req_set_callback(subreq
,
1113 dns_server_process_query_got_response
,
1118 tevent_req_done(req
);
1121 static void dns_server_process_query_got_auth(struct tevent_req
*subreq
)
1123 struct tevent_req
*req
= tevent_req_callback_data(
1124 subreq
, struct tevent_req
);
1125 struct dns_server_process_query_state
*state
= tevent_req_data(
1126 req
, struct dns_server_process_query_state
);
1129 werr
= handle_authoritative_recv(subreq
);
1130 TALLOC_FREE(subreq
);
1132 /* If you get an error, attempt a different forwarder */
1133 if (!W_ERROR_IS_OK(werr
)) {
1134 if (state
->forwarders
!= NULL
) {
1135 DLIST_REMOVE(state
->forwarders
, state
->forwarders
);
1138 /* If you have run out of forwarders, simply finish */
1139 if (state
->forwarders
== NULL
) {
1140 tevent_req_werror(req
, werr
);
1144 DEBUG(5, ("Error: %s, trying a different forwarder.\n",
1146 subreq
= handle_authoritative_send(state
, state
->ev
, state
->dns
,
1147 state
->forwarders
->forwarder
,
1148 state
->question
, &state
->answers
,
1151 if (tevent_req_nomem(subreq
, req
)) {
1155 tevent_req_set_callback(subreq
,
1156 dns_server_process_query_got_auth
,
1161 state
->ancount
= talloc_array_length(state
->answers
);
1162 state
->nscount
= talloc_array_length(state
->nsrecs
);
1163 state
->arcount
= talloc_array_length(state
->additional
);
1165 tevent_req_done(req
);
1168 WERROR
dns_server_process_query_recv(
1169 struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
1170 struct dns_res_rec
**answers
, uint16_t *ancount
,
1171 struct dns_res_rec
**nsrecs
, uint16_t *nscount
,
1172 struct dns_res_rec
**additional
, uint16_t *arcount
)
1174 struct dns_server_process_query_state
*state
= tevent_req_data(
1175 req
, struct dns_server_process_query_state
);
1176 WERROR err
= WERR_OK
;
1178 if (tevent_req_is_werror(req
, &err
)) {
1180 if ((!W_ERROR_EQUAL(err
, DNS_ERR(NAME_ERROR
))) &&
1181 (!W_ERROR_EQUAL(err
, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST
))) {
1185 *answers
= talloc_move(mem_ctx
, &state
->answers
);
1186 *ancount
= state
->ancount
;
1187 *nsrecs
= talloc_move(mem_ctx
, &state
->nsrecs
);
1188 *nscount
= state
->nscount
;
1189 *additional
= talloc_move(mem_ctx
, &state
->additional
);
1190 *arcount
= state
->arcount
;