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"
41 #define DBGC_CLASS DBGC_DNS
43 static WERROR create_response_rr(const char *name,
44 const struct dnsp_DnssrvRpcRecord *rec,
45 struct dns_res_rec **answers, uint16_t *ancount)
47 struct dns_res_rec *ans = *answers;
48 uint16_t ai = *ancount;
56 ans[ai].rdata.cname_record = talloc_strdup(ans, rec->data.cname);
57 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.cname_record);
60 ans[ai].rdata.ipv4_record = talloc_strdup(ans, rec->data.ipv4);
61 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ipv4_record);
64 ans[ai].rdata.ipv6_record = talloc_strdup(ans, rec->data.ipv6);
65 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ipv6_record);
68 ans[ai].rdata.ns_record = talloc_strdup(ans, rec->data.ns);
69 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ns_record);
72 ans[ai].rdata.srv_record.priority = rec->data.srv.wPriority;
73 ans[ai].rdata.srv_record.weight = rec->data.srv.wWeight;
74 ans[ai].rdata.srv_record.port = rec->data.srv.wPort;
75 ans[ai].rdata.srv_record.target = talloc_strdup(
76 ans, rec->data.srv.nameTarget);
77 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.srv_record.target);
80 ans[ai].rdata.soa_record.mname = talloc_strdup(
81 ans, rec->data.soa.mname);
82 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.soa_record.mname);
83 ans[ai].rdata.soa_record.rname = talloc_strdup(
84 ans, rec->data.soa.rname);
85 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.soa_record.rname);
86 ans[ai].rdata.soa_record.serial = rec->data.soa.serial;
87 ans[ai].rdata.soa_record.refresh = rec->data.soa.refresh;
88 ans[ai].rdata.soa_record.retry = rec->data.soa.retry;
89 ans[ai].rdata.soa_record.expire = rec->data.soa.expire;
90 ans[ai].rdata.soa_record.minimum = rec->data.soa.minimum;
93 ans[ai].rdata.ptr_record = talloc_strdup(ans, rec->data.ptr);
94 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ptr_record);
97 ans[ai].rdata.mx_record.preference = rec->data.mx.wPriority;
98 ans[ai].rdata.mx_record.exchange = talloc_strdup(
99 ans, rec->data.mx.nameTarget);
100 if (ans[ai].rdata.mx_record.exchange == NULL) {
105 tmp = talloc_asprintf(ans, "\"%s\"", rec->data.txt.str[0]);
106 W_ERROR_HAVE_NO_MEMORY(tmp);
107 for (i=1; i<rec->data.txt.count; i++) {
108 tmp = talloc_asprintf_append_buffer(
109 tmp, " \"%s\"", rec->data.txt.str[i]);
110 W_ERROR_HAVE_NO_MEMORY(tmp);
112 ans[ai].rdata.txt_record.txt = tmp;
115 DEBUG(0, ("Got unhandled type %u query.\n", rec->wType));
116 return DNS_ERR(NOT_IMPLEMENTED);
119 ans[ai].name = talloc_strdup(ans, name);
120 W_ERROR_HAVE_NO_MEMORY(ans[ai].name);
121 ans[ai].rr_type = rec->wType;
122 ans[ai].rr_class = DNS_QCLASS_IN;
123 ans[ai].ttl = rec->dwTtlSeconds;
124 ans[ai].length = UINT16_MAX;
133 struct ask_forwarder_state {
134 struct tevent_context *ev;
136 struct dns_name_packet in_packet;
139 static void ask_forwarder_done(struct tevent_req *subreq);
141 static struct tevent_req *ask_forwarder_send(
142 struct dns_server *dns,
143 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
144 const char *forwarder, struct dns_name_question *question)
146 struct tevent_req *req, *subreq;
147 struct ask_forwarder_state *state;
148 struct dns_res_rec *options;
149 struct dns_name_packet out_packet = { 0, };
151 enum ndr_err_code ndr_err;
154 req = tevent_req_create(mem_ctx, &state, struct ask_forwarder_state);
159 generate_random_buffer((uint8_t *)&state->id, sizeof(state->id));
161 if (!is_ipaddress(forwarder)) {
162 DEBUG(0, ("Invalid 'dns forwarder' setting '%s', needs to be "
163 "an IP address\n", forwarder));
164 tevent_req_werror(req, DNS_ERR(NAME_ERROR));
165 return tevent_req_post(req, ev);
168 out_packet.id = state->id;
169 out_packet.operation |= DNS_OPCODE_QUERY | DNS_FLAG_RECURSION_DESIRED;
170 out_packet.qdcount = 1;
171 out_packet.questions = question;
173 werr = dns_generate_options(dns, state, &options);
174 if (!W_ERROR_IS_OK(werr)) {
175 tevent_req_werror(req, werr);
176 return tevent_req_post(req, ev);
179 out_packet.arcount = 1;
180 out_packet.additional = options;
182 ndr_err = ndr_push_struct_blob(
183 &out_blob, state, &out_packet,
184 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
185 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
186 tevent_req_werror(req, DNS_ERR(SERVER_FAILURE));
187 return tevent_req_post(req, ev);
189 subreq = dns_udp_request_send(state, ev, forwarder, out_blob.data,
191 if (tevent_req_nomem(subreq, req)) {
192 return tevent_req_post(req, ev);
194 tevent_req_set_callback(subreq, ask_forwarder_done, req);
198 static void ask_forwarder_done(struct tevent_req *subreq)
200 struct tevent_req *req = tevent_req_callback_data(
201 subreq, struct tevent_req);
202 struct ask_forwarder_state *state = tevent_req_data(
203 req, struct ask_forwarder_state);
205 enum ndr_err_code ndr_err;
208 ret = dns_udp_request_recv(subreq, state,
209 &in_blob.data, &in_blob.length);
211 if (tevent_req_werror(req, ret)) {
215 ndr_err = ndr_pull_struct_blob(
216 &in_blob, state, &state->in_packet,
217 (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
218 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
219 tevent_req_werror(req, DNS_ERR(SERVER_FAILURE));
222 if (state->in_packet.id != state->id) {
223 tevent_req_werror(req, DNS_ERR(NAME_ERROR));
226 tevent_req_done(req);
229 static WERROR ask_forwarder_recv(
230 struct tevent_req *req, TALLOC_CTX *mem_ctx,
231 struct dns_res_rec **answers, uint16_t *ancount,
232 struct dns_res_rec **nsrecs, uint16_t *nscount,
233 struct dns_res_rec **additional, uint16_t *arcount)
235 struct ask_forwarder_state *state = tevent_req_data(
236 req, struct ask_forwarder_state);
237 struct dns_name_packet *in_packet = &state->in_packet;
240 if (tevent_req_is_werror(req, &err)) {
244 *ancount = in_packet->ancount;
245 *answers = talloc_move(mem_ctx, &in_packet->answers);
247 *nscount = in_packet->nscount;
248 *nsrecs = talloc_move(mem_ctx, &in_packet->nsrecs);
250 *arcount = in_packet->arcount;
251 *additional = talloc_move(mem_ctx, &in_packet->additional);
256 static WERROR add_zone_authority_record(struct dns_server *dns,
258 const struct dns_name_question *question,
259 struct dns_res_rec **nsrecs, uint16_t *nscount)
261 const char *zone = NULL;
262 struct dnsp_DnssrvRpcRecord *recs;
263 struct dns_res_rec *ns = *nsrecs;
264 uint16_t rec_count, ni = *nscount;
265 struct ldb_dn *dn = NULL;
269 zone = dns_get_authoritative_zone(dns, question->name);
270 DEBUG(10, ("Creating zone authority record for '%s'\n", zone));
272 werror = dns_name2dn(dns, mem_ctx, zone, &dn);
273 if (!W_ERROR_IS_OK(werror)) {
277 werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rec_count);
278 if (!W_ERROR_IS_OK(werror)) {
282 ns = talloc_realloc(mem_ctx, ns, struct dns_res_rec, rec_count + ni);
286 for (ri = 0; ri < rec_count; ri++) {
287 if (recs[ri].wType == DNS_TYPE_SOA) {
288 werror = create_response_rr(zone, &recs[ri], &ns, &ni);
289 if (!W_ERROR_IS_OK(werror)) {
302 static WERROR handle_question(struct dns_server *dns,
304 const struct dns_name_question *question,
305 struct dns_res_rec **answers, uint16_t *ancount,
306 struct dns_res_rec **nsrecs, uint16_t *nscount)
308 struct dns_res_rec *ans = *answers;
309 struct dns_res_rec *ns = *nsrecs;
310 WERROR werror, werror_return;
312 struct dnsp_DnssrvRpcRecord *recs;
313 uint16_t rec_count, ai = *ancount, ni = *nscount;
314 struct ldb_dn *dn = NULL;
316 werror = dns_name2dn(dns, mem_ctx, question->name, &dn);
317 if (!W_ERROR_IS_OK(werror)) {
321 werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rec_count);
322 if (!W_ERROR_IS_OK(werror)) {
323 werror_return = werror;
327 ans = talloc_realloc(mem_ctx, ans, struct dns_res_rec, rec_count + ai);
332 /* Set up for an NXDOMAIN reply if no match is found */
333 werror_return = DNS_ERR(NAME_ERROR);
335 for (ri = 0; ri < rec_count; ri++) {
336 if ((recs[ri].wType == DNS_TYPE_CNAME) &&
337 ((question->question_type == DNS_QTYPE_A) ||
338 (question->question_type == DNS_QTYPE_AAAA))) {
339 struct dns_name_question *new_q =
340 talloc(mem_ctx, struct dns_name_question);
346 /* We reply with one more record, so grow the array */
347 ans = talloc_realloc(mem_ctx, ans, struct dns_res_rec,
354 /* First put in the CNAME record */
355 werror = create_response_rr(question->name, &recs[ri], &ans, &ai);
356 if (!W_ERROR_IS_OK(werror)) {
361 /* And then look up the name it points at.. */
363 /* First build up the new question */
364 new_q->question_type = question->question_type;
365 new_q->question_class = question->question_class;
366 new_q->name = talloc_strdup(new_q, recs[ri].data.cname);
367 if (new_q->name == NULL) {
371 /* and then call the lookup again */
372 werror = handle_question(dns, mem_ctx, new_q, &ans, &ai, &ns, &ni);
373 if (!W_ERROR_IS_OK(werror)) {
376 werror_return = WERR_OK;
381 if ((question->question_type != DNS_QTYPE_ALL) &&
382 (recs[ri].wType != (enum dns_record_type) question->question_type)) {
383 werror_return = WERR_OK;
386 werror = create_response_rr(question->name, &recs[ri], &ans, &ai);
387 if (!W_ERROR_IS_OK(werror)) {
390 werror_return = WERR_OK;
394 /* Always add an authority record to replies we should know about */
395 add_zone_authority_record(dns, mem_ctx, question, &ns, &ni);
402 return werror_return;
405 static NTSTATUS create_tkey(struct dns_server *dns,
407 const char* algorithm,
408 struct dns_server_tkey **tkey)
411 struct dns_server_tkey_store *store = dns->tkeys;
412 struct dns_server_tkey *k = talloc_zero(store, struct dns_server_tkey);
415 return NT_STATUS_NO_MEMORY;
418 k->name = talloc_strdup(k, name);
420 if (k->name == NULL) {
421 return NT_STATUS_NO_MEMORY;
424 k->algorithm = talloc_strdup(k, algorithm);
425 if (k->algorithm == NULL) {
426 return NT_STATUS_NO_MEMORY;
429 status = samba_server_gensec_start(k,
430 dns->task->event_ctx,
433 dns->server_credentials,
436 if (!NT_STATUS_IS_OK(status)) {
437 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
442 gensec_want_feature(k->gensec, GENSEC_FEATURE_SIGN);
444 status = gensec_start_mech_by_oid(k->gensec, GENSEC_OID_SPNEGO);
446 if (!NT_STATUS_IS_OK(status)) {
447 DEBUG(1, ("Failed to start GENSEC server code: %s\n",
453 if (store->tkeys[store->next_idx] != NULL) {
454 TALLOC_FREE(store->tkeys[store->next_idx]);
457 store->tkeys[store->next_idx] = k;
459 store->next_idx %= store->size;
465 static NTSTATUS accept_gss_ticket(TALLOC_CTX *mem_ctx,
466 struct dns_server *dns,
467 struct dns_server_tkey *tkey,
468 const DATA_BLOB *key,
470 uint16_t *dns_auth_error)
474 status = gensec_update_ev(tkey->gensec, mem_ctx, dns->task->event_ctx,
477 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
478 *dns_auth_error = DNS_RCODE_OK;
482 if (NT_STATUS_IS_OK(status)) {
484 status = gensec_session_info(tkey->gensec, tkey, &tkey->session_info);
485 if (!NT_STATUS_IS_OK(status)) {
486 *dns_auth_error = DNS_RCODE_BADKEY;
489 *dns_auth_error = DNS_RCODE_OK;
495 static WERROR handle_tkey(struct dns_server *dns,
497 const struct dns_name_packet *in,
498 struct dns_request_state *state,
499 struct dns_res_rec **answers,
502 struct dns_res_rec *in_tkey = NULL;
503 struct dns_res_rec *ret_tkey;
506 for (i = 0; i < in->arcount; i++) {
507 if (in->additional[i].rr_type == DNS_QTYPE_TKEY) {
508 in_tkey = &in->additional[i];
513 /* If this is a TKEY query, it should have a TKEY RR.
514 * Behaviour is not really specified in RFC 2930 or RFC 3645, but
515 * FORMAT_ERROR seems to be what BIND uses .*/
516 if (in_tkey == NULL) {
517 return DNS_ERR(FORMAT_ERROR);
520 ret_tkey = talloc_zero(mem_ctx, struct dns_res_rec);
521 if (ret_tkey == NULL) {
525 ret_tkey->name = talloc_strdup(ret_tkey, in_tkey->name);
526 if (ret_tkey->name == NULL) {
530 ret_tkey->rr_type = DNS_QTYPE_TKEY;
531 ret_tkey->rr_class = DNS_QCLASS_ANY;
532 ret_tkey->length = UINT16_MAX;
534 ret_tkey->rdata.tkey_record.algorithm = talloc_strdup(ret_tkey,
535 in_tkey->rdata.tkey_record.algorithm);
536 if (ret_tkey->rdata.tkey_record.algorithm == NULL) {
540 ret_tkey->rdata.tkey_record.inception = in_tkey->rdata.tkey_record.inception;
541 ret_tkey->rdata.tkey_record.expiration = in_tkey->rdata.tkey_record.expiration;
542 ret_tkey->rdata.tkey_record.mode = in_tkey->rdata.tkey_record.mode;
544 switch (in_tkey->rdata.tkey_record.mode) {
545 case DNS_TKEY_MODE_DH:
546 /* FIXME: According to RFC 2930, we MUST support this, but we don't.
547 * Still, claim it's a bad key instead of a bad mode */
548 ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADKEY;
550 case DNS_TKEY_MODE_GSSAPI: {
552 struct dns_server_tkey *tkey;
556 tkey = dns_find_tkey(dns->tkeys, in->questions[0].name);
557 if (tkey != NULL && tkey->complete) {
558 /* TODO: check if the key is still valid */
559 DEBUG(1, ("Rejecting tkey negotiation for already established key\n"));
560 ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADNAME;
565 status = create_tkey(dns, in->questions[0].name,
566 in_tkey->rdata.tkey_record.algorithm,
568 if (!NT_STATUS_IS_OK(status)) {
569 ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADKEY;
570 return ntstatus_to_werror(status);
574 key.data = in_tkey->rdata.tkey_record.key_data;
575 key.length = in_tkey->rdata.tkey_record.key_size;
577 status = accept_gss_ticket(ret_tkey, dns, tkey, &key, &reply,
578 &ret_tkey->rdata.tkey_record.error);
579 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
580 DEBUG(1, ("More processing required\n"));
581 ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADKEY;
582 } else if (NT_STATUS_IS_OK(status)) {
583 DEBUG(1, ("Tkey handshake completed\n"));
584 ret_tkey->rdata.tkey_record.key_size = reply.length;
585 ret_tkey->rdata.tkey_record.key_data = talloc_memdup(ret_tkey,
589 state->key_name = talloc_strdup(state->mem_ctx, tkey->name);
590 if (state->key_name == NULL) {
594 DEBUG(1, ("GSS key negotiation returned %s\n", nt_errstr(status)));
595 ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADKEY;
600 case DNS_TKEY_MODE_DELETE:
601 /* TODO: implement me */
602 DEBUG(1, ("Should delete tkey here\n"));
603 ret_tkey->rdata.tkey_record.error = DNS_RCODE_OK;
605 case DNS_TKEY_MODE_NULL:
606 case DNS_TKEY_MODE_SERVER:
607 case DNS_TKEY_MODE_CLIENT:
608 case DNS_TKEY_MODE_LAST:
609 /* We don't have to implement these, return a mode error */
610 ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADMODE;
613 DEBUG(1, ("Unsupported TKEY mode %d\n",
614 in_tkey->rdata.tkey_record.mode));
623 struct dns_server_process_query_state {
624 struct dns_res_rec *answers;
626 struct dns_res_rec *nsrecs;
628 struct dns_res_rec *additional;
632 static void dns_server_process_query_got_response(struct tevent_req *subreq);
634 struct tevent_req *dns_server_process_query_send(
635 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
636 struct dns_server *dns, struct dns_request_state *req_state,
637 const struct dns_name_packet *in)
639 struct tevent_req *req, *subreq;
640 struct dns_server_process_query_state *state;
642 req = tevent_req_create(mem_ctx, &state,
643 struct dns_server_process_query_state);
647 if (in->qdcount != 1) {
648 tevent_req_werror(req, DNS_ERR(FORMAT_ERROR));
649 return tevent_req_post(req, ev);
652 /* Windows returns NOT_IMPLEMENTED on this as well */
653 if (in->questions[0].question_class == DNS_QCLASS_NONE) {
654 tevent_req_werror(req, DNS_ERR(NOT_IMPLEMENTED));
655 return tevent_req_post(req, ev);
658 if (in->questions[0].question_type == DNS_QTYPE_TKEY) {
661 err = handle_tkey(dns, state, in, req_state,
662 &state->answers, &state->ancount);
663 if (tevent_req_werror(req, err)) {
664 return tevent_req_post(req, ev);
666 tevent_req_done(req);
667 return tevent_req_post(req, ev);
670 if (dns_authorative_for_zone(dns, in->questions[0].name)) {
673 req_state->flags |= DNS_FLAG_AUTHORITATIVE;
674 err = handle_question(dns, state, &in->questions[0],
675 &state->answers, &state->ancount,
676 &state->nsrecs, &state->nscount);
678 if (W_ERROR_EQUAL(err, DNS_ERR(NAME_ERROR))) {
682 if (tevent_req_werror(req, err)) {
683 return tevent_req_post(req, ev);
686 tevent_req_done(req);
687 return tevent_req_post(req, ev);
690 if ((req_state->flags & DNS_FLAG_RECURSION_DESIRED) &&
691 (req_state->flags & DNS_FLAG_RECURSION_AVAIL)) {
692 DEBUG(2, ("Not authoritative for '%s', forwarding\n",
693 in->questions[0].name));
695 subreq = ask_forwarder_send(
697 state, ev, lpcfg_dns_forwarder(dns->task->lp_ctx),
699 if (tevent_req_nomem(subreq, req)) {
700 return tevent_req_post(req, ev);
702 tevent_req_set_callback(
703 subreq, dns_server_process_query_got_response, req);
707 tevent_req_werror(req, DNS_ERR(NAME_ERROR));
708 return tevent_req_post(req, ev);
711 static void dns_server_process_query_got_response(struct tevent_req *subreq)
713 struct tevent_req *req = tevent_req_callback_data(
714 subreq, struct tevent_req);
715 struct dns_server_process_query_state *state = tevent_req_data(
716 req, struct dns_server_process_query_state);
719 err = ask_forwarder_recv(subreq, state,
720 &state->answers, &state->ancount,
721 &state->nsrecs, &state->nscount,
722 &state->additional, &state->arcount);
724 if (tevent_req_werror(req, err)) {
727 tevent_req_done(req);
730 WERROR dns_server_process_query_recv(
731 struct tevent_req *req, TALLOC_CTX *mem_ctx,
732 struct dns_res_rec **answers, uint16_t *ancount,
733 struct dns_res_rec **nsrecs, uint16_t *nscount,
734 struct dns_res_rec **additional, uint16_t *arcount)
736 struct dns_server_process_query_state *state = tevent_req_data(
737 req, struct dns_server_process_query_state);
738 WERROR err = WERR_OK;
740 if (tevent_req_is_werror(req, &err)) {
742 if ((!W_ERROR_EQUAL(err, DNS_ERR(NAME_ERROR))) &&
743 (!W_ERROR_EQUAL(err, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST))) {
747 *answers = talloc_move(mem_ctx, &state->answers);
748 *ancount = state->ancount;
749 *nsrecs = talloc_move(mem_ctx, &state->nsrecs);
750 *nscount = state->nscount;
751 *additional = talloc_move(mem_ctx, &state->additional);
752 *arcount = state->arcount;