2 Unix SMB/CIFS implementation.
4 DNS server handler for update requests
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 "libcli/util/ntstatus.h"
24 #include "librpc/ndr/libndr.h"
25 #include "librpc/gen_ndr/ndr_dns.h"
26 #include "librpc/gen_ndr/ndr_dnsp.h"
28 #include "param/param.h"
29 #include "param/loadparm.h"
30 #include "dsdb/samdb/samdb.h"
31 #include "dsdb/common/util.h"
32 #include "smbd/service_task.h"
33 #include "dns_server/dns_server.h"
34 #include "auth/auth.h"
36 static WERROR dns_rr_to_dnsp(TALLOC_CTX *mem_ctx,
37 const struct dns_res_rec *rrec,
38 struct dnsp_DnssrvRpcRecord *r);
40 static WERROR check_one_prerequisite(struct dns_server *dns,
42 const struct dns_name_question *zone,
43 const struct dns_res_rec *pr,
51 struct dnsp_DnssrvRpcRecord *rec = NULL;
52 struct dnsp_DnssrvRpcRecord *ans;
55 size_t host_part_len = 0;
60 return DNS_ERR(FORMAT_ERROR);
63 match = dns_name_match(zone->name, pr->name, &host_part_len);
65 return DNS_ERR(NOTZONE);
68 werror = dns_name2dn(dns, mem_ctx, pr->name, &dn);
69 W_ERROR_NOT_OK_RETURN(werror);
71 if (pr->rr_class == DNS_QCLASS_ANY) {
73 if (pr->length != 0) {
74 return DNS_ERR(FORMAT_ERROR);
78 if (pr->rr_type == DNS_QTYPE_ALL) {
81 werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
82 if (W_ERROR_EQUAL(werror, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
83 return DNS_ERR(NAME_ERROR);
85 W_ERROR_NOT_OK_RETURN(werror);
88 return DNS_ERR(NAME_ERROR);
93 werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
94 if (W_ERROR_EQUAL(werror, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
95 return DNS_ERR(NXRRSET);
97 if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
98 return DNS_ERR(NXRRSET);
100 W_ERROR_NOT_OK_RETURN(werror);
102 for (i = 0; i < acount; i++) {
103 if (ans[i].wType == pr->rr_type) {
109 return DNS_ERR(NXRRSET);
114 * RFC2136 3.2.5 doesn't actually mention the need to return
115 * OK here, but otherwise we'd always return a FORMAT_ERROR
116 * later on. This also matches Microsoft DNS behavior.
121 if (pr->rr_class == DNS_QCLASS_NONE) {
122 if (pr->length != 0) {
123 return DNS_ERR(FORMAT_ERROR);
126 if (pr->rr_type == DNS_QTYPE_ALL) {
129 werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
130 if (W_ERROR_EQUAL(werror, WERR_OK)) {
131 return DNS_ERR(YXDOMAIN);
136 werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
137 if (W_ERROR_EQUAL(werror, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
140 if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
144 for (i = 0; i < acount; i++) {
145 if (ans[i].wType == pr->rr_type) {
151 return DNS_ERR(YXRRSET);
156 * RFC2136 3.2.5 doesn't actually mention the need to return
157 * OK here, but otherwise we'd always return a FORMAT_ERROR
158 * later on. This also matches Microsoft DNS behavior.
163 if (pr->rr_class != zone->question_class) {
164 return DNS_ERR(FORMAT_ERROR);
167 *final_result = false;
169 werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
170 if (W_ERROR_EQUAL(werror, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
171 return DNS_ERR(NXRRSET);
173 if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
174 return DNS_ERR(NXRRSET);
176 W_ERROR_NOT_OK_RETURN(werror);
178 rec = talloc_zero(mem_ctx, struct dnsp_DnssrvRpcRecord);
179 W_ERROR_HAVE_NO_MEMORY(rec);
181 werror = dns_rr_to_dnsp(rec, pr, rec);
182 W_ERROR_NOT_OK_RETURN(werror);
184 for (i = 0; i < acount; i++) {
185 if (dns_records_match(rec, &ans[i])) {
192 return DNS_ERR(NXRRSET);
198 static WERROR check_prerequisites(struct dns_server *dns,
200 const struct dns_name_question *zone,
201 const struct dns_res_rec *prereqs, uint16_t count)
204 WERROR final_error = WERR_OK;
206 for (i = 0; i < count; i++) {
210 werror = check_one_prerequisite(dns, mem_ctx, zone,
211 &prereqs[i], &final);
212 if (!W_ERROR_IS_OK(werror)) {
216 if (W_ERROR_IS_OK(final_error)) {
217 final_error = werror;
222 if (!W_ERROR_IS_OK(final_error)) {
229 static WERROR update_prescan(const struct dns_name_question *zone,
230 const struct dns_res_rec *updates, uint16_t count)
232 const struct dns_res_rec *r;
234 size_t host_part_len;
237 for (i = 0; i < count; i++) {
239 match = dns_name_match(zone->name, r->name, &host_part_len);
241 return DNS_ERR(NOTZONE);
243 if (zone->question_class == r->rr_class) {
244 if (r->rr_type == DNS_QTYPE_ALL) {
245 return DNS_ERR(FORMAT_ERROR);
247 if (r->rr_type == DNS_QTYPE_AXFR) {
248 return DNS_ERR(FORMAT_ERROR);
250 if (r->rr_type == DNS_QTYPE_MAILB) {
251 return DNS_ERR(FORMAT_ERROR);
253 if (r->rr_type == DNS_QTYPE_MAILA) {
254 return DNS_ERR(FORMAT_ERROR);
256 } else if (r->rr_class == DNS_QCLASS_ANY) {
258 return DNS_ERR(FORMAT_ERROR);
260 if (r->length != 0) {
261 return DNS_ERR(FORMAT_ERROR);
263 if (r->rr_type == DNS_QTYPE_AXFR) {
264 return DNS_ERR(FORMAT_ERROR);
266 if (r->rr_type == DNS_QTYPE_MAILB) {
267 return DNS_ERR(FORMAT_ERROR);
269 if (r->rr_type == DNS_QTYPE_MAILA) {
270 return DNS_ERR(FORMAT_ERROR);
272 } else if (r->rr_class == DNS_QCLASS_NONE) {
274 return DNS_ERR(FORMAT_ERROR);
276 if (r->rr_type == DNS_QTYPE_ALL) {
277 return DNS_ERR(FORMAT_ERROR);
279 if (r->rr_type == DNS_QTYPE_AXFR) {
280 return DNS_ERR(FORMAT_ERROR);
282 if (r->rr_type == DNS_QTYPE_MAILB) {
283 return DNS_ERR(FORMAT_ERROR);
285 if (r->rr_type == DNS_QTYPE_MAILA) {
286 return DNS_ERR(FORMAT_ERROR);
289 return DNS_ERR(FORMAT_ERROR);
295 static WERROR dns_rr_to_dnsp(TALLOC_CTX *mem_ctx,
296 const struct dns_res_rec *rrec,
297 struct dnsp_DnssrvRpcRecord *r)
300 char *txt_record_txt;
301 char *saveptr = NULL;
303 if (rrec->rr_type == DNS_QTYPE_ALL) {
304 return DNS_ERR(FORMAT_ERROR);
309 r->wType = rrec->rr_type;
310 r->dwTtlSeconds = rrec->ttl;
311 r->rank = DNS_RANK_ZONE;
313 /* If we get QCLASS_ANY, we're done here */
314 if (rrec->rr_class == DNS_QCLASS_ANY) {
318 switch(rrec->rr_type) {
320 r->data.ipv4 = talloc_strdup(mem_ctx, rrec->rdata.ipv4_record);
321 W_ERROR_HAVE_NO_MEMORY(r->data.ipv4);
324 r->data.ipv6 = talloc_strdup(mem_ctx, rrec->rdata.ipv6_record);
325 W_ERROR_HAVE_NO_MEMORY(r->data.ipv6);
328 r->data.ns = talloc_strdup(mem_ctx, rrec->rdata.ns_record);
329 W_ERROR_HAVE_NO_MEMORY(r->data.ns);
331 case DNS_QTYPE_CNAME:
332 r->data.cname = talloc_strdup(mem_ctx, rrec->rdata.cname_record);
333 W_ERROR_HAVE_NO_MEMORY(r->data.cname);
336 r->data.srv.wPriority = rrec->rdata.srv_record.priority;
337 r->data.srv.wWeight = rrec->rdata.srv_record.weight;
338 r->data.srv.wPort = rrec->rdata.srv_record.port;
339 r->data.srv.nameTarget = talloc_strdup(mem_ctx,
340 rrec->rdata.srv_record.target);
341 W_ERROR_HAVE_NO_MEMORY(r->data.srv.nameTarget);
344 r->data.ptr = talloc_strdup(mem_ctx, rrec->rdata.ptr_record);
345 W_ERROR_HAVE_NO_MEMORY(r->data.ptr);
348 r->data.mx.wPriority = rrec->rdata.mx_record.preference;
349 r->data.mx.nameTarget = talloc_strdup(mem_ctx,
350 rrec->rdata.mx_record.exchange);
351 W_ERROR_HAVE_NO_MEMORY(r->data.mx.nameTarget);
354 r->data.txt.count = 0;
355 r->data.txt.str = talloc_array(mem_ctx, const char *,
357 W_ERROR_HAVE_NO_MEMORY(r->data.txt.str);
359 txt_record_txt = talloc_strdup(r->data.txt.str,
360 rrec->rdata.txt_record.txt);
361 W_ERROR_HAVE_NO_MEMORY(txt_record_txt);
363 tmp = strtok_r(txt_record_txt, "\"", &saveptr);
365 if (strcmp(tmp, " ") == 0) {
366 tmp = strtok_r(NULL, "\"", &saveptr);
369 r->data.txt.str = talloc_realloc(mem_ctx, r->data.txt.str, const char *,
370 r->data.txt.count+1);
371 r->data.txt.str[r->data.txt.count] = talloc_strdup(r->data.txt.str, tmp);
372 W_ERROR_HAVE_NO_MEMORY(r->data.txt.str[r->data.txt.count]);
375 tmp = strtok_r(NULL, "\"", &saveptr);
380 DEBUG(0, ("Got a qytpe of %d\n", rrec->rr_type));
381 return DNS_ERR(NOT_IMPLEMENTED);
390 static WERROR handle_one_update(struct dns_server *dns,
392 const struct dns_name_question *zone,
393 const struct dns_res_rec *update,
394 const struct dns_server_tkey *tkey)
396 struct dnsp_DnssrvRpcRecord *recs = NULL;
402 bool tombstoned = false;
403 bool needs_add = false;
405 DEBUG(2, ("Looking at record: \n"));
407 NDR_PRINT_DEBUG(dns_res_rec, discard_const(update));
410 switch (update->rr_type) {
413 case DNS_QTYPE_CNAME:
422 DEBUG(0, ("Can't handle updates of type %u yet\n",
424 return DNS_ERR(NOT_IMPLEMENTED);
427 werror = dns_name2dn(dns, mem_ctx, update->name, &dn);
428 W_ERROR_NOT_OK_RETURN(werror);
430 werror = dns_common_lookup(dns->samdb, mem_ctx, dn,
431 &recs, &rcount, &tombstoned);
432 if (W_ERROR_EQUAL(werror, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
436 W_ERROR_NOT_OK_RETURN(werror);
440 * we need to keep the existing tombstone record
446 if (update->rr_class == zone->question_class) {
447 if (update->rr_type == DNS_QTYPE_CNAME) {
449 * If there is a record in the directory
450 * that's not a CNAME, ignore update
452 for (i = first; i < rcount; i++) {
453 if (recs[i].wType != DNS_TYPE_CNAME) {
454 DEBUG(5, ("Skipping update\n"));
461 * There should be no entries besides one CNAME record
462 * per name, so replace everything with the new CNAME
466 recs = talloc_realloc(mem_ctx, recs,
467 struct dnsp_DnssrvRpcRecord, rcount + 1);
468 W_ERROR_HAVE_NO_MEMORY(recs);
470 werror = dns_rr_to_dnsp(recs, update, &recs[rcount]);
471 W_ERROR_NOT_OK_RETURN(werror);
474 werror = dns_replace_records(dns, mem_ctx, dn,
475 needs_add, recs, rcount);
476 W_ERROR_NOT_OK_RETURN(werror);
481 * If there is a CNAME record for this name,
484 for (i = first; i < rcount; i++) {
485 if (recs[i].wType == DNS_TYPE_CNAME) {
486 DEBUG(5, ("Skipping update\n"));
491 if (update->rr_type == DNS_QTYPE_SOA) {
495 * If the zone has no SOA record?? or update's
496 * serial number is smaller than existing SOA's,
499 for (i = first; i < rcount; i++) {
500 if (recs[i].wType == DNS_TYPE_SOA) {
503 n = update->rdata.soa_record.serial;
504 o = recs[i].data.soa.serial;
506 * TODO: Implement RFC 1982 comparison
510 DEBUG(5, ("Skipping update\n"));
518 DEBUG(5, ("Skipping update\n"));
522 werror = dns_rr_to_dnsp(mem_ctx, update, &recs[i]);
523 W_ERROR_NOT_OK_RETURN(werror);
525 for (i++; i < rcount; i++) {
526 if (recs[i].wType != DNS_TYPE_SOA) {
530 recs[i] = (struct dnsp_DnssrvRpcRecord) {
531 .wType = DNS_TYPE_TOMBSTONE,
535 werror = dns_replace_records(dns, mem_ctx, dn,
536 needs_add, recs, rcount);
537 W_ERROR_NOT_OK_RETURN(werror);
542 recs = talloc_realloc(mem_ctx, recs,
543 struct dnsp_DnssrvRpcRecord, rcount+1);
544 W_ERROR_HAVE_NO_MEMORY(recs);
546 werror = dns_rr_to_dnsp(recs, update, &recs[rcount]);
547 W_ERROR_NOT_OK_RETURN(werror);
549 for (i = first; i < rcount; i++) {
550 if (!dns_records_match(&recs[i], &recs[rcount])) {
554 recs[i] = recs[rcount];
556 werror = dns_replace_records(dns, mem_ctx, dn,
557 needs_add, recs, rcount);
558 W_ERROR_NOT_OK_RETURN(werror);
563 werror = dns_replace_records(dns, mem_ctx, dn,
564 needs_add, recs, rcount+1);
565 W_ERROR_NOT_OK_RETURN(werror);
568 } else if (update->rr_class == DNS_QCLASS_ANY) {
569 if (update->rr_type == DNS_QTYPE_ALL) {
570 if (dns_name_equal(update->name, zone->name)) {
571 for (i = first; i < rcount; i++) {
573 if (recs[i].wType == DNS_TYPE_SOA) {
577 if (recs[i].wType == DNS_TYPE_NS) {
581 recs[i] = (struct dnsp_DnssrvRpcRecord) {
582 .wType = DNS_TYPE_TOMBSTONE,
587 for (i = first; i < rcount; i++) {
588 recs[i] = (struct dnsp_DnssrvRpcRecord) {
589 .wType = DNS_TYPE_TOMBSTONE,
594 } else if (dns_name_equal(update->name, zone->name)) {
596 if (update->rr_type == DNS_QTYPE_SOA) {
600 if (update->rr_type == DNS_QTYPE_NS) {
604 for (i = first; i < rcount; i++) {
605 if (recs[i].wType == update->rr_type) {
606 recs[i] = (struct dnsp_DnssrvRpcRecord) {
607 .wType = DNS_TYPE_TOMBSTONE,
612 werror = dns_replace_records(dns, mem_ctx, dn,
613 needs_add, recs, rcount);
614 W_ERROR_NOT_OK_RETURN(werror);
617 } else if (update->rr_class == DNS_QCLASS_NONE) {
618 struct dnsp_DnssrvRpcRecord *del_rec;
620 if (update->rr_type == DNS_QTYPE_SOA) {
623 if (update->rr_type == DNS_QTYPE_NS) {
625 struct dnsp_DnssrvRpcRecord *ns_rec = talloc(mem_ctx,
626 struct dnsp_DnssrvRpcRecord);
627 W_ERROR_HAVE_NO_MEMORY(ns_rec);
630 werror = dns_rr_to_dnsp(ns_rec, update, ns_rec);
631 W_ERROR_NOT_OK_RETURN(werror);
633 for (i = first; i < rcount; i++) {
634 if (dns_records_match(ns_rec, &recs[i])) {
644 del_rec = talloc(mem_ctx, struct dnsp_DnssrvRpcRecord);
645 W_ERROR_HAVE_NO_MEMORY(del_rec);
647 werror = dns_rr_to_dnsp(del_rec, update, del_rec);
648 W_ERROR_NOT_OK_RETURN(werror);
650 for (i = first; i < rcount; i++) {
651 if (dns_records_match(del_rec, &recs[i])) {
652 recs[i] = (struct dnsp_DnssrvRpcRecord) {
653 .wType = DNS_TYPE_TOMBSTONE,
658 werror = dns_replace_records(dns, mem_ctx, dn,
659 needs_add, recs, rcount);
660 W_ERROR_NOT_OK_RETURN(werror);
666 static WERROR handle_updates(struct dns_server *dns,
668 const struct dns_name_question *zone,
669 const struct dns_res_rec *prereqs, uint16_t pcount,
670 struct dns_res_rec *updates, uint16_t upd_count,
671 struct dns_server_tkey *tkey)
673 struct ldb_dn *zone_dn = NULL;
674 WERROR werror = WERR_OK;
677 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
680 ret = ldb_set_opaque(dns->samdb, "sessionInfo", tkey->session_info);
681 if (ret != LDB_SUCCESS) {
682 DEBUG(1, ("unable to set session info\n"));
683 werror = DNS_ERR(SERVER_FAILURE);
688 werror = dns_name2dn(dns, tmp_ctx, zone->name, &zone_dn);
689 W_ERROR_NOT_OK_GOTO(werror, failed);
691 ret = ldb_transaction_start(dns->samdb);
692 if (ret != LDB_SUCCESS) {
693 werror = DNS_ERR(SERVER_FAILURE);
697 werror = check_prerequisites(dns, tmp_ctx, zone, prereqs, pcount);
698 W_ERROR_NOT_OK_GOTO(werror, failed);
700 DEBUG(1, ("update count is %u\n", upd_count));
702 for (ri = 0; ri < upd_count; ri++) {
703 werror = handle_one_update(dns, tmp_ctx, zone,
705 W_ERROR_NOT_OK_GOTO(werror, failed);
708 ldb_transaction_commit(dns->samdb);
709 TALLOC_FREE(tmp_ctx);
712 ldb_set_opaque(dns->samdb, "sessionInfo",
713 system_session(dns->task->lp_ctx));
719 ldb_transaction_cancel(dns->samdb);
722 ldb_set_opaque(dns->samdb, "sessionInfo",
723 system_session(dns->task->lp_ctx));
726 TALLOC_FREE(tmp_ctx);
731 static WERROR dns_update_allowed(struct dns_server *dns,
732 struct dns_request_state *state,
733 struct dns_server_tkey **tkey)
735 if (lpcfg_allow_dns_updates(dns->task->lp_ctx) == DNS_UPDATE_ON) {
736 DEBUG(2, ("All updates allowed.\n"));
740 if (lpcfg_allow_dns_updates(dns->task->lp_ctx) == DNS_UPDATE_OFF) {
741 DEBUG(2, ("Updates disabled.\n"));
742 return DNS_ERR(REFUSED);
745 if (state->authenticated == false ) {
746 DEBUG(2, ("Update not allowed for unsigned packet.\n"));
747 return DNS_ERR(REFUSED);
750 *tkey = dns_find_tkey(dns->tkeys, state->key_name);
752 DEBUG(0, ("Authenticated, but key not found. Something is wrong.\n"));
753 return DNS_ERR(REFUSED);
760 WERROR dns_server_process_update(struct dns_server *dns,
761 struct dns_request_state *state,
763 struct dns_name_packet *in,
764 struct dns_res_rec **prereqs, uint16_t *prereq_count,
765 struct dns_res_rec **updates, uint16_t *update_count,
766 struct dns_res_rec **additional, uint16_t *arcount)
768 struct dns_name_question *zone;
769 const struct dns_server_zone *z;
770 size_t host_part_len = 0;
771 WERROR werror = DNS_ERR(NOT_IMPLEMENTED);
772 struct dns_server_tkey *tkey = NULL;
774 if (in->qdcount != 1) {
775 return DNS_ERR(FORMAT_ERROR);
778 zone = &in->questions[0];
780 if (zone->question_class != DNS_QCLASS_IN &&
781 zone->question_class != DNS_QCLASS_ANY) {
782 return DNS_ERR(NOT_IMPLEMENTED);
785 if (zone->question_type != DNS_QTYPE_SOA) {
786 return DNS_ERR(FORMAT_ERROR);
789 DEBUG(2, ("Got a dns update request.\n"));
791 for (z = dns->zones; z != NULL; z = z->next) {
794 match = dns_name_match(z->name, zone->name, &host_part_len);
801 DEBUG(1, ("We're not authoritative for this zone\n"));
802 return DNS_ERR(NOTAUTH);
805 if (host_part_len != 0) {
806 /* TODO: We need to delegate this one */
807 DEBUG(1, ("Would have to delegate zone '%s'.\n", zone->name));
808 return DNS_ERR(NOT_IMPLEMENTED);
811 *prereq_count = in->ancount;
812 *prereqs = in->answers;
813 werror = check_prerequisites(dns, mem_ctx, in->questions, *prereqs,
815 W_ERROR_NOT_OK_RETURN(werror);
817 werror = dns_update_allowed(dns, state, &tkey);
818 if (!W_ERROR_IS_OK(werror)) {
822 *update_count = in->nscount;
823 *updates = in->nsrecs;
824 werror = update_prescan(in->questions, *updates, *update_count);
825 W_ERROR_NOT_OK_RETURN(werror);
827 werror = handle_updates(dns, mem_ctx, in->questions, *prereqs,
828 *prereq_count, *updates, *update_count, tkey);
829 W_ERROR_NOT_OK_RETURN(werror);