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"
36 #include "auth/auth.h"
37 #include "auth/credentials/credentials.h"
38 #include "auth/gensec/gensec.h"
40 static WERROR
create_response_rr(const struct dns_name_question
*question
,
41 const struct dnsp_DnssrvRpcRecord
*rec
,
42 struct dns_res_rec
**answers
, uint16_t *ancount
)
44 struct dns_res_rec
*ans
= *answers
;
45 uint16_t ai
= *ancount
;
53 ans
[ai
].rdata
.cname_record
= talloc_strdup(ans
, rec
->data
.cname
);
54 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.cname_record
);
57 ans
[ai
].rdata
.ipv4_record
= talloc_strdup(ans
, rec
->data
.ipv4
);
58 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.ipv4_record
);
61 ans
[ai
].rdata
.ipv6_record
= talloc_strdup(ans
, rec
->data
.ipv6
);
62 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.ipv6_record
);
65 ans
[ai
].rdata
.ns_record
= talloc_strdup(ans
, rec
->data
.ns
);
66 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.ns_record
);
69 ans
[ai
].rdata
.srv_record
.priority
= rec
->data
.srv
.wPriority
;
70 ans
[ai
].rdata
.srv_record
.weight
= rec
->data
.srv
.wWeight
;
71 ans
[ai
].rdata
.srv_record
.port
= rec
->data
.srv
.wPort
;
72 ans
[ai
].rdata
.srv_record
.target
= talloc_strdup(
73 ans
, rec
->data
.srv
.nameTarget
);
74 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.srv_record
.target
);
77 ans
[ai
].rdata
.soa_record
.mname
= talloc_strdup(
78 ans
, rec
->data
.soa
.mname
);
79 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.soa_record
.mname
);
80 ans
[ai
].rdata
.soa_record
.rname
= talloc_strdup(
81 ans
, rec
->data
.soa
.rname
);
82 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].rdata
.soa_record
.rname
);
83 ans
[ai
].rdata
.soa_record
.serial
= rec
->data
.soa
.serial
;
84 ans
[ai
].rdata
.soa_record
.refresh
= rec
->data
.soa
.refresh
;
85 ans
[ai
].rdata
.soa_record
.retry
= rec
->data
.soa
.retry
;
86 ans
[ai
].rdata
.soa_record
.expire
= rec
->data
.soa
.expire
;
87 ans
[ai
].rdata
.soa_record
.minimum
= rec
->data
.soa
.minimum
;
90 ans
[ai
].rdata
.ptr_record
= talloc_strdup(ans
, rec
->data
.ptr
);
93 tmp
= talloc_asprintf(ans
, "\"%s\"", rec
->data
.txt
.str
[0]);
94 W_ERROR_HAVE_NO_MEMORY(tmp
);
95 for (i
=1; i
<rec
->data
.txt
.count
; i
++) {
96 tmp
= talloc_asprintf_append_buffer(
97 tmp
, " \"%s\"", rec
->data
.txt
.str
[i
]);
98 W_ERROR_HAVE_NO_MEMORY(tmp
);
100 ans
[ai
].rdata
.txt_record
.txt
= tmp
;
103 DEBUG(0, ("Got unhandled type %u query.\n", rec
->wType
));
104 return DNS_ERR(NOT_IMPLEMENTED
);
107 ans
[ai
].name
= talloc_strdup(ans
, question
->name
);
108 W_ERROR_HAVE_NO_MEMORY(ans
[ai
].name
);
109 ans
[ai
].rr_type
= rec
->wType
;
110 ans
[ai
].rr_class
= DNS_QCLASS_IN
;
111 ans
[ai
].ttl
= rec
->dwTtlSeconds
;
112 ans
[ai
].length
= UINT16_MAX
;
121 struct ask_forwarder_state
{
122 struct tevent_context
*ev
;
124 struct dns_name_packet in_packet
;
127 static void ask_forwarder_done(struct tevent_req
*subreq
);
129 static struct tevent_req
*ask_forwarder_send(
130 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
131 const char *forwarder
, struct dns_name_question
*question
)
133 struct tevent_req
*req
, *subreq
;
134 struct ask_forwarder_state
*state
;
135 struct dns_name_packet out_packet
= { 0, };
137 enum ndr_err_code ndr_err
;
139 req
= tevent_req_create(mem_ctx
, &state
, struct ask_forwarder_state
);
144 generate_random_buffer((uint8_t *)&state
->id
, sizeof(state
->id
));
146 if (!is_ipaddress(forwarder
)) {
147 DEBUG(0, ("Invalid 'dns forwarder' setting '%s', needs to be "
148 "an IP address\n", forwarder
));
149 tevent_req_werror(req
, DNS_ERR(NAME_ERROR
));
150 return tevent_req_post(req
, ev
);
153 out_packet
.id
= state
->id
;
154 out_packet
.operation
|= DNS_OPCODE_QUERY
| DNS_FLAG_RECURSION_DESIRED
;
155 out_packet
.qdcount
= 1;
156 out_packet
.questions
= question
;
158 ndr_err
= ndr_push_struct_blob(
159 &out_blob
, state
, &out_packet
,
160 (ndr_push_flags_fn_t
)ndr_push_dns_name_packet
);
161 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
162 tevent_req_werror(req
, DNS_ERR(SERVER_FAILURE
));
163 return tevent_req_post(req
, ev
);
165 subreq
= dns_udp_request_send(state
, ev
, forwarder
, out_blob
.data
,
167 if (tevent_req_nomem(subreq
, req
)) {
168 return tevent_req_post(req
, ev
);
170 tevent_req_set_callback(subreq
, ask_forwarder_done
, req
);
174 static void ask_forwarder_done(struct tevent_req
*subreq
)
176 struct tevent_req
*req
= tevent_req_callback_data(
177 subreq
, struct tevent_req
);
178 struct ask_forwarder_state
*state
= tevent_req_data(
179 req
, struct ask_forwarder_state
);
181 enum ndr_err_code ndr_err
;
184 ret
= dns_udp_request_recv(subreq
, state
,
185 &in_blob
.data
, &in_blob
.length
);
187 if (tevent_req_werror(req
, ret
)) {
191 ndr_err
= ndr_pull_struct_blob(
192 &in_blob
, state
, &state
->in_packet
,
193 (ndr_pull_flags_fn_t
)ndr_pull_dns_name_packet
);
194 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
195 tevent_req_werror(req
, DNS_ERR(SERVER_FAILURE
));
198 if (state
->in_packet
.id
!= state
->id
) {
199 tevent_req_werror(req
, DNS_ERR(NAME_ERROR
));
202 tevent_req_done(req
);
205 static WERROR
ask_forwarder_recv(
206 struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
207 struct dns_res_rec
**answers
, uint16_t *ancount
,
208 struct dns_res_rec
**nsrecs
, uint16_t *nscount
,
209 struct dns_res_rec
**additional
, uint16_t *arcount
)
211 struct ask_forwarder_state
*state
= tevent_req_data(
212 req
, struct ask_forwarder_state
);
213 struct dns_name_packet
*in_packet
= &state
->in_packet
;
216 if (tevent_req_is_werror(req
, &err
)) {
220 *ancount
= in_packet
->ancount
;
221 *answers
= talloc_move(mem_ctx
, &in_packet
->answers
);
223 *nscount
= in_packet
->nscount
;
224 *nsrecs
= talloc_move(mem_ctx
, &in_packet
->nsrecs
);
226 *arcount
= in_packet
->arcount
;
227 *additional
= talloc_move(mem_ctx
, &in_packet
->additional
);
232 static WERROR
handle_question(struct dns_server
*dns
,
234 const struct dns_name_question
*question
,
235 struct dns_res_rec
**answers
, uint16_t *ancount
)
237 struct dns_res_rec
*ans
= *answers
;
240 struct dnsp_DnssrvRpcRecord
*recs
;
241 uint16_t rec_count
, ai
= *ancount
;
242 struct ldb_dn
*dn
= NULL
;
244 werror
= dns_name2dn(dns
, mem_ctx
, question
->name
, &dn
);
245 W_ERROR_NOT_OK_RETURN(werror
);
247 werror
= dns_lookup_records(dns
, mem_ctx
, dn
, &recs
, &rec_count
);
248 W_ERROR_NOT_OK_RETURN(werror
);
250 ans
= talloc_realloc(mem_ctx
, ans
, struct dns_res_rec
, rec_count
+ ai
);
255 for (ri
= 0; ri
< rec_count
; ri
++) {
256 if ((recs
[ri
].wType
== DNS_TYPE_CNAME
) &&
257 ((question
->question_type
== DNS_QTYPE_A
) ||
258 (question
->question_type
== DNS_QTYPE_AAAA
))) {
259 struct dns_name_question
*new_q
=
260 talloc(mem_ctx
, struct dns_name_question
);
266 /* We reply with one more record, so grow the array */
267 ans
= talloc_realloc(mem_ctx
, ans
, struct dns_res_rec
,
274 /* First put in the CNAME record */
275 werror
= create_response_rr(question
, &recs
[ri
], &ans
, &ai
);
276 if (!W_ERROR_IS_OK(werror
)) {
280 /* And then look up the name it points at.. */
282 /* First build up the new question */
283 new_q
->question_type
= question
->question_type
;
284 new_q
->question_class
= question
->question_class
;
285 if (new_q
->question_type
== DNS_QTYPE_A
) {
286 new_q
->name
= talloc_strdup(new_q
, recs
[ri
].data
.ipv4
);
287 } else if (new_q
->question_type
== DNS_QTYPE_AAAA
) {
288 new_q
->name
= talloc_strdup(new_q
, recs
[ri
].data
.ipv6
);
290 if (new_q
->name
== NULL
) {
294 /* and then call the lookup again */
295 werror
= handle_question(dns
, mem_ctx
, new_q
, &ans
, &ai
);
296 if (!W_ERROR_IS_OK(werror
)) {
303 if ((question
->question_type
!= DNS_QTYPE_ALL
) &&
304 (recs
[ri
].wType
!= question
->question_type
)) {
307 werror
= create_response_rr(question
, &recs
[ri
], &ans
, &ai
);
308 if (!W_ERROR_IS_OK(werror
)) {
314 return DNS_ERR(NAME_ERROR
);
323 static NTSTATUS
create_tkey(struct dns_server
*dns
,
325 const char* algorithm
,
326 struct dns_server_tkey
**tkey
)
329 struct dns_server_tkey_store
*store
= dns
->tkeys
;
330 struct dns_server_tkey
*k
= talloc_zero(store
, struct dns_server_tkey
);
333 return NT_STATUS_NO_MEMORY
;
336 k
->name
= talloc_strdup(k
, name
);
338 if (k
->name
== NULL
) {
339 return NT_STATUS_NO_MEMORY
;
342 k
->algorithm
= talloc_strdup(k
, algorithm
);
343 if (k
->algorithm
== NULL
) {
344 return NT_STATUS_NO_MEMORY
;
347 status
= samba_server_gensec_start(k
,
348 dns
->task
->event_ctx
,
351 dns
->server_credentials
,
354 if (!NT_STATUS_IS_OK(status
)) {
355 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status
)));
360 gensec_want_feature(k
->gensec
, GENSEC_FEATURE_SIGN
);
362 status
= gensec_start_mech_by_oid(k
->gensec
, GENSEC_OID_SPNEGO
);
364 if (!NT_STATUS_IS_OK(status
)) {
365 DEBUG(1, ("Failed to start GENSEC server code: %s\n",
371 if (store
->tkeys
[store
->next_idx
] != NULL
) {
372 TALLOC_FREE(store
->tkeys
[store
->next_idx
]);
375 store
->tkeys
[store
->next_idx
] = k
;
377 store
->next_idx
%= store
->size
;
383 static NTSTATUS
accept_gss_ticket(TALLOC_CTX
*mem_ctx
,
384 struct dns_server
*dns
,
385 struct dns_server_tkey
*tkey
,
386 const DATA_BLOB
*key
,
388 uint16_t *dns_auth_error
)
392 status
= gensec_update(tkey
->gensec
, mem_ctx
, dns
->task
->event_ctx
,
395 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED
, status
)) {
396 *dns_auth_error
= DNS_RCODE_OK
;
400 if (NT_STATUS_IS_OK(status
)) {
402 status
= gensec_session_info(tkey
->gensec
, tkey
, &tkey
->session_info
);
403 if (!NT_STATUS_IS_OK(status
)) {
404 *dns_auth_error
= DNS_RCODE_BADKEY
;
407 *dns_auth_error
= DNS_RCODE_OK
;
413 static WERROR
handle_tkey(struct dns_server
*dns
,
415 const struct dns_name_packet
*in
,
416 struct dns_request_state
*state
,
417 struct dns_res_rec
**answers
,
420 struct dns_res_rec
*in_tkey
= NULL
;
421 struct dns_res_rec
*ret_tkey
;
424 for (i
= 0; i
< in
->arcount
; i
++) {
425 if (in
->additional
[i
].rr_type
== DNS_QTYPE_TKEY
) {
426 in_tkey
= &in
->additional
[i
];
431 /* If this is a TKEY query, it should have a TKEY RR.
432 * Behaviour is not really specified in RFC 2930 or RFC 3645, but
433 * FORMAT_ERROR seems to be what BIND uses .*/
434 if (in_tkey
== NULL
) {
435 return DNS_ERR(FORMAT_ERROR
);
438 ret_tkey
= talloc_zero(mem_ctx
, struct dns_res_rec
);
439 if (ret_tkey
== NULL
) {
443 ret_tkey
->name
= talloc_strdup(ret_tkey
, in_tkey
->name
);
444 if (ret_tkey
->name
== NULL
) {
448 ret_tkey
->rr_type
= DNS_QTYPE_TKEY
;
449 ret_tkey
->rr_class
= DNS_QCLASS_ANY
;
450 ret_tkey
->length
= UINT16_MAX
;
452 ret_tkey
->rdata
.tkey_record
.algorithm
= talloc_strdup(ret_tkey
,
453 in_tkey
->rdata
.tkey_record
.algorithm
);
454 if (ret_tkey
->rdata
.tkey_record
.algorithm
== NULL
) {
458 ret_tkey
->rdata
.tkey_record
.inception
= in_tkey
->rdata
.tkey_record
.inception
;
459 ret_tkey
->rdata
.tkey_record
.expiration
= in_tkey
->rdata
.tkey_record
.expiration
;
460 ret_tkey
->rdata
.tkey_record
.mode
= in_tkey
->rdata
.tkey_record
.mode
;
462 switch (in_tkey
->rdata
.tkey_record
.mode
) {
463 case DNS_TKEY_MODE_DH
:
464 /* FIXME: According to RFC 2930, we MUST support this, but we don't.
465 * Still, claim it's a bad key instead of a bad mode */
466 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_BADKEY
;
468 case DNS_TKEY_MODE_GSSAPI
: {
470 struct dns_server_tkey
*tkey
;
474 tkey
= dns_find_tkey(dns
->tkeys
, in
->questions
[0].name
);
475 if (tkey
!= NULL
&& tkey
->complete
) {
476 /* TODO: check if the key is still valid */
477 DEBUG(1, ("Rejecting tkey negotiation for already established key\n"));
478 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_BADNAME
;
483 status
= create_tkey(dns
, in
->questions
[0].name
,
484 in_tkey
->rdata
.tkey_record
.algorithm
,
486 if (!NT_STATUS_IS_OK(status
)) {
487 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_BADKEY
;
488 return ntstatus_to_werror(status
);
492 key
.data
= in_tkey
->rdata
.tkey_record
.key_data
;
493 key
.length
= in_tkey
->rdata
.tkey_record
.key_size
;
495 status
= accept_gss_ticket(ret_tkey
, dns
, tkey
, &key
, &reply
,
496 &ret_tkey
->rdata
.tkey_record
.error
);
497 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
498 DEBUG(1, ("More processing required\n"));
499 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_BADKEY
;
500 } else if (NT_STATUS_IS_OK(status
)) {
501 DEBUG(1, ("Tkey handshake completed\n"));
502 ret_tkey
->rdata
.tkey_record
.key_size
= reply
.length
;
503 ret_tkey
->rdata
.tkey_record
.key_data
= talloc_memdup(ret_tkey
,
507 state
->key_name
= talloc_strdup(mem_ctx
, tkey
->name
);
508 if (state
->key_name
== NULL
) {
512 DEBUG(1, ("GSS key negotiation returned %s\n", nt_errstr(status
)));
513 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_BADKEY
;
518 case DNS_TKEY_MODE_DELETE
:
519 /* TODO: implement me */
520 DEBUG(1, ("Should delete tkey here\n"));
521 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_OK
;
523 case DNS_TKEY_MODE_NULL
:
524 case DNS_TKEY_MODE_SERVER
:
525 case DNS_TKEY_MODE_CLIENT
:
526 case DNS_TKEY_MODE_LAST
:
527 /* We don't have to implement these, return a mode error */
528 ret_tkey
->rdata
.tkey_record
.error
= DNS_RCODE_BADMODE
;
531 DEBUG(1, ("Unsupported TKEY mode %d\n",
532 in_tkey
->rdata
.tkey_record
.mode
));
541 struct dns_server_process_query_state
{
542 struct dns_res_rec
*answers
;
544 struct dns_res_rec
*nsrecs
;
546 struct dns_res_rec
*additional
;
550 static void dns_server_process_query_got_response(struct tevent_req
*subreq
);
552 struct tevent_req
*dns_server_process_query_send(
553 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
554 struct dns_server
*dns
, struct dns_request_state
*req_state
,
555 const struct dns_name_packet
*in
)
557 struct tevent_req
*req
, *subreq
;
558 struct dns_server_process_query_state
*state
;
560 req
= tevent_req_create(mem_ctx
, &state
,
561 struct dns_server_process_query_state
);
565 if (in
->qdcount
!= 1) {
566 tevent_req_werror(req
, DNS_ERR(FORMAT_ERROR
));
567 return tevent_req_post(req
, ev
);
570 /* Windows returns NOT_IMPLEMENTED on this as well */
571 if (in
->questions
[0].question_class
== DNS_QCLASS_NONE
) {
572 tevent_req_werror(req
, DNS_ERR(NOT_IMPLEMENTED
));
573 return tevent_req_post(req
, ev
);
576 if (in
->questions
[0].question_type
== DNS_QTYPE_TKEY
) {
579 err
= handle_tkey(dns
, state
, in
, req_state
,
580 &state
->answers
, &state
->ancount
);
581 if (tevent_req_werror(req
, err
)) {
582 return tevent_req_post(req
, ev
);
584 tevent_req_done(req
);
585 return tevent_req_post(req
, ev
);
588 if (dns_authorative_for_zone(dns
, in
->questions
[0].name
)) {
591 req_state
->flags
|= DNS_FLAG_AUTHORITATIVE
;
592 err
= handle_question(dns
, state
, &in
->questions
[0],
593 &state
->answers
, &state
->ancount
);
594 if (tevent_req_werror(req
, err
)) {
595 return tevent_req_post(req
, ev
);
597 tevent_req_done(req
);
598 return tevent_req_post(req
, ev
);
601 if ((req_state
->flags
& DNS_FLAG_RECURSION_DESIRED
) &&
602 (req_state
->flags
& DNS_FLAG_RECURSION_AVAIL
)) {
603 DEBUG(2, ("Not authoritative for '%s', forwarding\n",
604 in
->questions
[0].name
));
606 subreq
= ask_forwarder_send(
607 state
, ev
, lpcfg_dns_forwarder(dns
->task
->lp_ctx
),
609 if (tevent_req_nomem(subreq
, req
)) {
610 return tevent_req_post(req
, ev
);
612 tevent_req_set_callback(
613 subreq
, dns_server_process_query_got_response
, req
);
617 tevent_req_werror(req
, DNS_ERR(NAME_ERROR
));
618 return tevent_req_post(req
, ev
);
621 static void dns_server_process_query_got_response(struct tevent_req
*subreq
)
623 struct tevent_req
*req
= tevent_req_callback_data(
624 subreq
, struct tevent_req
);
625 struct dns_server_process_query_state
*state
= tevent_req_data(
626 req
, struct dns_server_process_query_state
);
629 err
= ask_forwarder_recv(subreq
, state
,
630 &state
->answers
, &state
->ancount
,
631 &state
->nsrecs
, &state
->nscount
,
632 &state
->additional
, &state
->arcount
);
634 if (tevent_req_werror(req
, err
)) {
637 tevent_req_done(req
);
640 WERROR
dns_server_process_query_recv(
641 struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
642 struct dns_res_rec
**answers
, uint16_t *ancount
,
643 struct dns_res_rec
**nsrecs
, uint16_t *nscount
,
644 struct dns_res_rec
**additional
, uint16_t *arcount
)
646 struct dns_server_process_query_state
*state
= tevent_req_data(
647 req
, struct dns_server_process_query_state
);
650 if (tevent_req_is_werror(req
, &err
)) {
653 *answers
= talloc_move(mem_ctx
, &state
->answers
);
654 *ancount
= state
->ancount
;
655 *nsrecs
= talloc_move(mem_ctx
, &state
->nsrecs
);
656 *nscount
= state
->nscount
;
657 *additional
= talloc_move(mem_ctx
, &state
->additional
);
658 *arcount
= state
->arcount
;