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 "samba/service_task.h"
33 #include "dns_server/dns_server.h"
34 #include "auth/auth.h"
37 #define DBGC_CLASS DBGC_DNS
39 static WERROR
dns_rr_to_dnsp(TALLOC_CTX
*mem_ctx
,
40 const struct dns_res_rec
*rrec
,
41 struct dnsp_DnssrvRpcRecord
*r
,
44 static WERROR
check_one_prerequisite(struct dns_server
*dns
,
46 const struct dns_name_question
*zone
,
47 const struct dns_res_rec
*pr
,
55 struct dnsp_DnssrvRpcRecord
*rec
= NULL
;
56 struct dnsp_DnssrvRpcRecord
*ans
;
59 size_t host_part_len
= 0;
64 return DNS_ERR(FORMAT_ERROR
);
67 match
= dns_name_match(zone
->name
, pr
->name
, &host_part_len
);
69 return DNS_ERR(NOTZONE
);
72 werror
= dns_name2dn(dns
, mem_ctx
, pr
->name
, &dn
);
73 W_ERROR_NOT_OK_RETURN(werror
);
75 if (pr
->rr_class
== DNS_QCLASS_ANY
) {
77 if (pr
->length
!= 0) {
78 return DNS_ERR(FORMAT_ERROR
);
82 if (pr
->rr_type
== DNS_QTYPE_ALL
) {
85 werror
= dns_lookup_records(dns
, mem_ctx
, dn
, &ans
, &a_count
);
86 if (W_ERROR_EQUAL(werror
, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST
)) {
87 return DNS_ERR(NAME_ERROR
);
89 W_ERROR_NOT_OK_RETURN(werror
);
92 return DNS_ERR(NAME_ERROR
);
97 werror
= dns_lookup_records(dns
, mem_ctx
, dn
, &ans
, &a_count
);
98 if (W_ERROR_EQUAL(werror
, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST
)) {
99 return DNS_ERR(NXRRSET
);
101 if (W_ERROR_EQUAL(werror
, DNS_ERR(NAME_ERROR
))) {
102 return DNS_ERR(NXRRSET
);
104 W_ERROR_NOT_OK_RETURN(werror
);
106 for (i
= 0; i
< a_count
; i
++) {
107 if (ans
[i
].wType
== (enum dns_record_type
) pr
->rr_type
) {
113 return DNS_ERR(NXRRSET
);
118 * RFC2136 3.2.5 doesn't actually mention the need to return
119 * OK here, but otherwise we'd always return a FORMAT_ERROR
120 * later on. This also matches Microsoft DNS behavior.
125 if (pr
->rr_class
== DNS_QCLASS_NONE
) {
126 if (pr
->length
!= 0) {
127 return DNS_ERR(FORMAT_ERROR
);
130 if (pr
->rr_type
== DNS_QTYPE_ALL
) {
133 werror
= dns_lookup_records(dns
, mem_ctx
, dn
, &ans
, &a_count
);
134 if (W_ERROR_EQUAL(werror
, WERR_OK
)) {
135 return DNS_ERR(YXDOMAIN
);
140 werror
= dns_lookup_records(dns
, mem_ctx
, dn
, &ans
, &a_count
);
141 if (W_ERROR_EQUAL(werror
, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST
)) {
144 if (W_ERROR_EQUAL(werror
, DNS_ERR(NAME_ERROR
))) {
148 for (i
= 0; i
< a_count
; i
++) {
149 if (ans
[i
].wType
== (enum dns_record_type
) pr
->rr_type
) {
155 return DNS_ERR(YXRRSET
);
160 * RFC2136 3.2.5 doesn't actually mention the need to return
161 * OK here, but otherwise we'd always return a FORMAT_ERROR
162 * later on. This also matches Microsoft DNS behavior.
167 if (pr
->rr_class
!= zone
->question_class
) {
168 return DNS_ERR(FORMAT_ERROR
);
171 *final_result
= false;
173 werror
= dns_lookup_records(dns
, mem_ctx
, dn
, &ans
, &a_count
);
174 if (W_ERROR_EQUAL(werror
, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST
)) {
175 return DNS_ERR(NXRRSET
);
177 if (W_ERROR_EQUAL(werror
, DNS_ERR(NAME_ERROR
))) {
178 return DNS_ERR(NXRRSET
);
180 W_ERROR_NOT_OK_RETURN(werror
);
182 rec
= talloc_zero(mem_ctx
, struct dnsp_DnssrvRpcRecord
);
183 W_ERROR_HAVE_NO_MEMORY(rec
);
185 werror
= dns_rr_to_dnsp(rec
, pr
, rec
, dns_name_is_static(ans
, a_count
));
186 W_ERROR_NOT_OK_RETURN(werror
);
188 for (i
= 0; i
< a_count
; i
++) {
189 if (dns_record_match(rec
, &ans
[i
])) {
196 return DNS_ERR(NXRRSET
);
202 static WERROR
check_prerequisites(struct dns_server
*dns
,
204 const struct dns_name_question
*zone
,
205 const struct dns_res_rec
*prereqs
, uint16_t count
)
208 WERROR final_error
= WERR_OK
;
210 for (i
= 0; i
< count
; i
++) {
214 werror
= check_one_prerequisite(dns
, mem_ctx
, zone
,
215 &prereqs
[i
], &final
);
216 if (!W_ERROR_IS_OK(werror
)) {
220 if (W_ERROR_IS_OK(final_error
)) {
221 final_error
= werror
;
226 if (!W_ERROR_IS_OK(final_error
)) {
233 static WERROR
update_prescan(const struct dns_name_question
*zone
,
234 const struct dns_res_rec
*updates
, uint16_t count
)
236 const struct dns_res_rec
*r
;
238 size_t host_part_len
;
241 for (i
= 0; i
< count
; i
++) {
243 match
= dns_name_match(zone
->name
, r
->name
, &host_part_len
);
245 return DNS_ERR(NOTZONE
);
247 if (zone
->question_class
== r
->rr_class
) {
248 if (r
->rr_type
== DNS_QTYPE_ALL
) {
249 return DNS_ERR(FORMAT_ERROR
);
251 if (r
->rr_type
== DNS_QTYPE_AXFR
) {
252 return DNS_ERR(FORMAT_ERROR
);
254 if (r
->rr_type
== DNS_QTYPE_MAILB
) {
255 return DNS_ERR(FORMAT_ERROR
);
257 if (r
->rr_type
== DNS_QTYPE_MAILA
) {
258 return DNS_ERR(FORMAT_ERROR
);
260 } else if (r
->rr_class
== DNS_QCLASS_ANY
) {
262 return DNS_ERR(FORMAT_ERROR
);
264 if (r
->length
!= 0) {
265 return DNS_ERR(FORMAT_ERROR
);
267 if (r
->rr_type
== DNS_QTYPE_AXFR
) {
268 return DNS_ERR(FORMAT_ERROR
);
270 if (r
->rr_type
== DNS_QTYPE_MAILB
) {
271 return DNS_ERR(FORMAT_ERROR
);
273 if (r
->rr_type
== DNS_QTYPE_MAILA
) {
274 return DNS_ERR(FORMAT_ERROR
);
276 } else if (r
->rr_class
== DNS_QCLASS_NONE
) {
278 return DNS_ERR(FORMAT_ERROR
);
280 if (r
->rr_type
== DNS_QTYPE_ALL
) {
281 return DNS_ERR(FORMAT_ERROR
);
283 if (r
->rr_type
== DNS_QTYPE_AXFR
) {
284 return DNS_ERR(FORMAT_ERROR
);
286 if (r
->rr_type
== DNS_QTYPE_MAILB
) {
287 return DNS_ERR(FORMAT_ERROR
);
289 if (r
->rr_type
== DNS_QTYPE_MAILA
) {
290 return DNS_ERR(FORMAT_ERROR
);
293 return DNS_ERR(FORMAT_ERROR
);
299 static WERROR
dns_rr_to_dnsp(TALLOC_CTX
*mem_ctx
,
300 const struct dns_res_rec
*rrec
,
301 struct dnsp_DnssrvRpcRecord
*r
,
304 enum ndr_err_code ndr_err
;
306 if (rrec
->rr_type
== DNS_QTYPE_ALL
) {
307 return DNS_ERR(FORMAT_ERROR
);
312 r
->wType
= (enum dns_record_type
) rrec
->rr_type
;
313 r
->dwTtlSeconds
= rrec
->ttl
;
314 r
->rank
= DNS_RANK_ZONE
;
315 if (name_is_static
) {
318 r
->dwTimeStamp
= unix_to_dns_timestamp(time(NULL
));
321 /* If we get QCLASS_ANY, we're done here */
322 if (rrec
->rr_class
== DNS_QCLASS_ANY
) {
326 switch(rrec
->rr_type
) {
328 r
->data
.ipv4
= talloc_strdup(mem_ctx
, rrec
->rdata
.ipv4_record
);
329 W_ERROR_HAVE_NO_MEMORY(r
->data
.ipv4
);
332 r
->data
.ipv6
= talloc_strdup(mem_ctx
, rrec
->rdata
.ipv6_record
);
333 W_ERROR_HAVE_NO_MEMORY(r
->data
.ipv6
);
336 r
->data
.ns
= talloc_strdup(mem_ctx
, rrec
->rdata
.ns_record
);
337 W_ERROR_HAVE_NO_MEMORY(r
->data
.ns
);
339 case DNS_QTYPE_CNAME
:
340 r
->data
.cname
= talloc_strdup(mem_ctx
, rrec
->rdata
.cname_record
);
341 W_ERROR_HAVE_NO_MEMORY(r
->data
.cname
);
344 r
->data
.srv
.wPriority
= rrec
->rdata
.srv_record
.priority
;
345 r
->data
.srv
.wWeight
= rrec
->rdata
.srv_record
.weight
;
346 r
->data
.srv
.wPort
= rrec
->rdata
.srv_record
.port
;
347 r
->data
.srv
.nameTarget
= talloc_strdup(mem_ctx
,
348 rrec
->rdata
.srv_record
.target
);
349 W_ERROR_HAVE_NO_MEMORY(r
->data
.srv
.nameTarget
);
352 r
->data
.ptr
= talloc_strdup(mem_ctx
, rrec
->rdata
.ptr_record
);
353 W_ERROR_HAVE_NO_MEMORY(r
->data
.ptr
);
356 r
->data
.mx
.wPriority
= rrec
->rdata
.mx_record
.preference
;
357 r
->data
.mx
.nameTarget
= talloc_strdup(mem_ctx
,
358 rrec
->rdata
.mx_record
.exchange
);
359 W_ERROR_HAVE_NO_MEMORY(r
->data
.mx
.nameTarget
);
362 ndr_err
= ndr_dnsp_string_list_copy(mem_ctx
,
363 &rrec
->rdata
.txt_record
.txt
,
365 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
366 return WERR_NOT_ENOUGH_MEMORY
;
371 DEBUG(0, ("Got a qytpe of %d\n", rrec
->rr_type
));
372 return DNS_ERR(NOT_IMPLEMENTED
);
381 static WERROR
handle_one_update(struct dns_server
*dns
,
383 const struct dns_name_question
*zone
,
384 const struct dns_res_rec
*update
,
385 const struct dns_server_tkey
*tkey
)
387 struct dnsp_DnssrvRpcRecord
*recs
= NULL
;
393 bool tombstoned
= false;
394 bool needs_add
= false;
397 DBG_NOTICE("Looking at record: \n");
398 if (DEBUGLVL(DBGLVL_NOTICE
)) {
399 NDR_PRINT_DEBUG(dns_res_rec
, discard_const(update
));
402 switch (update
->rr_type
) {
405 case DNS_QTYPE_CNAME
:
415 DEBUG(0, ("Can't handle updates of type %u yet\n",
417 return DNS_ERR(NOT_IMPLEMENTED
);
420 werror
= dns_name2dn(dns
, mem_ctx
, update
->name
, &dn
);
421 DBG_DEBUG("dns_name2dn(): %s\n", win_errstr(werror
));
422 W_ERROR_NOT_OK_RETURN(werror
);
424 werror
= dns_common_lookup(dns
->samdb
, mem_ctx
, dn
,
425 &recs
, &rcount
, &tombstoned
);
426 DBG_DEBUG("dns_common_lookup(): %s\n", win_errstr(werror
));
427 if (W_ERROR_EQUAL(werror
, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST
)) {
431 W_ERROR_NOT_OK_RETURN(werror
);
435 * we need to keep the existing tombstone record
438 * There *should* only be a single record of type TOMBSTONE,
439 * but we don't insist.
442 DBG_WARNING("Tombstoned dnsNode has %u records, "
443 "expected 1\n", rcount
);
444 if (DEBUGLVL(DBGLVL_WARNING
)) {
445 NDR_PRINT_DEBUG(dns_res_rec
, discard_const(update
));
451 name_is_static
= dns_name_is_static(recs
, rcount
);
453 if (update
->rr_class
== zone
->question_class
) {
454 if (update
->rr_type
== DNS_QTYPE_CNAME
) {
456 * If there is a record in the directory
457 * that's not a CNAME, ignore update
459 for (i
= first
; i
< rcount
; i
++) {
460 if (recs
[i
].wType
!= DNS_TYPE_CNAME
) {
461 DEBUG(5, ("Skipping update\n"));
467 * There should be no entries besides one CNAME record
468 * per name, so replace everything with the new CNAME
472 recs
= talloc_realloc(mem_ctx
, recs
,
473 struct dnsp_DnssrvRpcRecord
, rcount
+ 1);
474 W_ERROR_HAVE_NO_MEMORY(recs
);
476 werror
= dns_rr_to_dnsp(
477 recs
, update
, &recs
[rcount
], name_is_static
);
478 DBG_DEBUG("dns_rr_to_dnsp(CNAME): %s\n", win_errstr(werror
));
479 W_ERROR_NOT_OK_RETURN(werror
);
482 werror
= dns_replace_records(dns
, mem_ctx
, dn
,
483 needs_add
, recs
, rcount
);
484 DBG_DEBUG("dns_replace_records(CNAME): %s\n", win_errstr(werror
));
485 W_ERROR_NOT_OK_RETURN(werror
);
490 * If there is a CNAME record for this name,
493 for (i
= first
; i
< rcount
; i
++) {
494 if (recs
[i
].wType
== DNS_TYPE_CNAME
) {
495 DEBUG(5, ("Skipping update\n"));
500 if (update
->rr_type
== DNS_QTYPE_SOA
) {
504 * If the zone has no SOA record?? or update's
505 * serial number is smaller than existing SOA's,
508 for (i
= first
; i
< rcount
; i
++) {
509 if (recs
[i
].wType
== DNS_TYPE_SOA
) {
512 n
= update
->rdata
.soa_record
.serial
;
513 o
= recs
[i
].data
.soa
.serial
;
515 * TODO: Implement RFC 1982 comparison
519 DEBUG(5, ("Skipping update\n"));
527 DEBUG(5, ("Skipping update\n"));
531 werror
= dns_rr_to_dnsp(
532 mem_ctx
, update
, &recs
[i
], name_is_static
);
533 DBG_DEBUG("dns_rr_to_dnsp(SOA): %s\n", win_errstr(werror
));
534 W_ERROR_NOT_OK_RETURN(werror
);
537 * There should only be one SOA, which we have already
538 * found and replaced. We now check for and tombstone
541 for (i
++; i
< rcount
; i
++) {
542 if (recs
[i
].wType
!= DNS_TYPE_SOA
) {
545 DBG_ERR("Duplicate SOA records found.\n");
546 if (DEBUGLVL(DBGLVL_ERR
)) {
547 NDR_PRINT_DEBUG(dns_res_rec
,
548 discard_const(update
));
550 recs
[i
] = (struct dnsp_DnssrvRpcRecord
) {
551 .wType
= DNS_TYPE_TOMBSTONE
,
555 werror
= dns_replace_records(dns
, mem_ctx
, dn
,
556 needs_add
, recs
, rcount
);
557 DBG_DEBUG("dns_replace_records(SOA): %s\n", win_errstr(werror
));
558 W_ERROR_NOT_OK_RETURN(werror
);
562 /* All but CNAME, SOA */
563 recs
= talloc_realloc(mem_ctx
, recs
,
564 struct dnsp_DnssrvRpcRecord
, rcount
+1);
565 W_ERROR_HAVE_NO_MEMORY(recs
);
568 dns_rr_to_dnsp(recs
, update
, &recs
[rcount
], name_is_static
);
569 DBG_DEBUG("dns_rr_to_dnsp(GENERIC): %s\n", win_errstr(werror
));
570 W_ERROR_NOT_OK_RETURN(werror
);
572 for (i
= first
; i
< rcount
; i
++) {
573 struct dnsp_DnssrvRpcRecord orig_rec
= recs
[i
];
575 if (!dns_record_match(&recs
[i
], &recs
[rcount
])) {
579 recs
[i
].data
= recs
[rcount
].data
;
580 recs
[i
].wType
= recs
[rcount
].wType
;
581 recs
[i
].dwTtlSeconds
= recs
[rcount
].dwTtlSeconds
;
582 recs
[i
].rank
= recs
[rcount
].rank
;
583 recs
[i
].dwReserved
= 0;
585 werror
= dns_replace_records(dns
, mem_ctx
, dn
,
586 needs_add
, recs
, rcount
);
587 DBG_DEBUG("dns_replace_records(REPLACE): %s\n", win_errstr(werror
));
588 if (W_ERROR_EQUAL(werror
, WERR_ACCESS_DENIED
) &&
590 orig_rec
.dwTtlSeconds
== recs
[i
].dwTtlSeconds
)
592 DBG_NOTICE("dns_replace_records(REPLACE): %s "
597 W_ERROR_NOT_OK_RETURN(werror
);
601 /* we did not find a matching record. This is new. */
602 werror
= dns_replace_records(dns
, mem_ctx
, dn
,
603 needs_add
, recs
, rcount
+1);
604 DBG_DEBUG("dns_replace_records(ADD): %s\n", win_errstr(werror
));
605 W_ERROR_NOT_OK_RETURN(werror
);
608 } else if (update
->rr_class
== DNS_QCLASS_ANY
) {
610 * Mass-deleting records by type, which we do by adding a
611 * tombstone with zero timestamp. dns_replace_records() will
612 * work out if the node as a whole needs tombstoning.
614 if (update
->rr_type
== DNS_QTYPE_ALL
) {
615 if (samba_dns_name_equal(update
->name
, zone
->name
)) {
616 for (i
= first
; i
< rcount
; i
++) {
618 if (recs
[i
].wType
== DNS_TYPE_SOA
) {
622 if (recs
[i
].wType
== DNS_TYPE_NS
) {
626 recs
[i
] = (struct dnsp_DnssrvRpcRecord
) {
627 .wType
= DNS_TYPE_TOMBSTONE
,
632 for (i
= first
; i
< rcount
; i
++) {
633 recs
[i
] = (struct dnsp_DnssrvRpcRecord
) {
634 .wType
= DNS_TYPE_TOMBSTONE
,
639 } else if (samba_dns_name_equal(update
->name
, zone
->name
)) {
641 if (update
->rr_type
== DNS_QTYPE_SOA
) {
645 if (update
->rr_type
== DNS_QTYPE_NS
) {
649 for (i
= first
; i
< rcount
; i
++) {
650 if (recs
[i
].wType
== (enum dns_record_type
) update
->rr_type
) {
651 recs
[i
] = (struct dnsp_DnssrvRpcRecord
) {
652 .wType
= DNS_TYPE_TOMBSTONE
,
657 werror
= dns_replace_records(dns
, mem_ctx
, dn
,
658 needs_add
, recs
, rcount
);
659 DBG_DEBUG("dns_replace_records(DELETE-ANY): %s\n", win_errstr(werror
));
660 W_ERROR_NOT_OK_RETURN(werror
);
663 } else if (update
->rr_class
== DNS_QCLASS_NONE
) {
664 /* deleting individual records */
665 struct dnsp_DnssrvRpcRecord
*del_rec
;
667 if (update
->rr_type
== DNS_QTYPE_SOA
) {
670 if (update
->rr_type
== DNS_QTYPE_NS
) {
672 struct dnsp_DnssrvRpcRecord
*ns_rec
= talloc(mem_ctx
,
673 struct dnsp_DnssrvRpcRecord
);
674 W_ERROR_HAVE_NO_MEMORY(ns_rec
);
676 werror
= dns_rr_to_dnsp(
677 ns_rec
, update
, ns_rec
, name_is_static
);
678 DBG_DEBUG("dns_rr_to_dnsp(NS): %s\n", win_errstr(werror
));
679 W_ERROR_NOT_OK_RETURN(werror
);
681 for (i
= first
; i
< rcount
; i
++) {
682 if (dns_record_match(ns_rec
, &recs
[i
])) {
692 del_rec
= talloc(mem_ctx
, struct dnsp_DnssrvRpcRecord
);
693 W_ERROR_HAVE_NO_MEMORY(del_rec
);
696 dns_rr_to_dnsp(del_rec
, update
, del_rec
, name_is_static
);
697 DBG_DEBUG("dns_rr_to_dnsp(DELETE-NONE): %s\n", win_errstr(werror
));
698 W_ERROR_NOT_OK_RETURN(werror
);
700 for (i
= first
; i
< rcount
; i
++) {
701 if (dns_record_match(del_rec
, &recs
[i
])) {
702 recs
[i
] = (struct dnsp_DnssrvRpcRecord
) {
703 .wType
= DNS_TYPE_TOMBSTONE
,
708 werror
= dns_replace_records(dns
, mem_ctx
, dn
,
709 needs_add
, recs
, rcount
);
710 DBG_DEBUG("dns_replace_records(DELETE-NONE): %s\n", win_errstr(werror
));
711 W_ERROR_NOT_OK_RETURN(werror
);
717 static WERROR
handle_updates(struct dns_server
*dns
,
719 const struct dns_name_question
*zone
,
720 const struct dns_res_rec
*prereqs
, uint16_t pcount
,
721 struct dns_res_rec
*updates
, uint16_t upd_count
,
722 struct dns_server_tkey
*tkey
)
724 struct ldb_dn
*zone_dn
= NULL
;
725 WERROR werror
= WERR_OK
;
728 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
731 ret
= ldb_set_opaque(
735 if (ret
!= LDB_SUCCESS
) {
736 DEBUG(1, ("unable to set session info\n"));
737 werror
= DNS_ERR(SERVER_FAILURE
);
742 werror
= dns_name2dn(dns
, tmp_ctx
, zone
->name
, &zone_dn
);
743 DBG_DEBUG("dns_name2dn(): %s\n", win_errstr(werror
));
744 W_ERROR_NOT_OK_GOTO(werror
, failed
);
746 ret
= ldb_transaction_start(dns
->samdb
);
747 if (ret
!= LDB_SUCCESS
) {
748 werror
= DNS_ERR(SERVER_FAILURE
);
752 werror
= check_prerequisites(dns
, tmp_ctx
, zone
, prereqs
, pcount
);
753 W_ERROR_NOT_OK_GOTO(werror
, failed
);
755 DBG_DEBUG("dns update count is %u\n", upd_count
);
757 for (ri
= 0; ri
< upd_count
; ri
++) {
758 werror
= handle_one_update(dns
, tmp_ctx
, zone
,
760 DBG_DEBUG("handle_one_update(%u): %s\n",
761 ri
, win_errstr(werror
));
762 W_ERROR_NOT_OK_GOTO(werror
, failed
);
766 if (W_ERROR_IS_OK(werror
)) {
767 ret
= ldb_transaction_commit(dns
->samdb
);
768 if (ret
!= LDB_SUCCESS
) {
769 werror
= DNS_ERR(SERVER_FAILURE
);
772 ldb_transaction_cancel(dns
->samdb
);
779 system_session(dns
->task
->lp_ctx
));
782 TALLOC_FREE(tmp_ctx
);
787 static WERROR
dns_update_allowed(struct dns_server
*dns
,
788 const struct dns_request_state
*state
,
789 struct dns_server_tkey
**tkey
)
791 if (lpcfg_allow_dns_updates(dns
->task
->lp_ctx
) == DNS_UPDATE_ON
) {
792 DEBUG(2, ("All updates allowed.\n"));
796 if (lpcfg_allow_dns_updates(dns
->task
->lp_ctx
) == DNS_UPDATE_OFF
) {
797 DEBUG(2, ("Updates disabled.\n"));
798 return DNS_ERR(REFUSED
);
801 if (state
->authenticated
== false ) {
802 DEBUG(2, ("Update not allowed for unsigned packet.\n"));
803 return DNS_ERR(REFUSED
);
806 *tkey
= dns_find_tkey(dns
->tkeys
, state
->key_name
);
808 DEBUG(0, ("Authenticated, but key not found. Something is wrong.\n"));
809 return DNS_ERR(REFUSED
);
816 WERROR
dns_server_process_update(struct dns_server
*dns
,
817 const struct dns_request_state
*state
,
819 const struct dns_name_packet
*in
,
820 struct dns_res_rec
**prereqs
, uint16_t *prereq_count
,
821 struct dns_res_rec
**updates
, uint16_t *update_count
,
822 struct dns_res_rec
**additional
, uint16_t *arcount
)
824 struct dns_name_question
*zone
;
825 const struct dns_server_zone
*z
;
826 size_t host_part_len
= 0;
827 WERROR werror
= DNS_ERR(NOT_IMPLEMENTED
);
828 struct dns_server_tkey
*tkey
= NULL
;
830 if (in
->qdcount
!= 1) {
831 return DNS_ERR(FORMAT_ERROR
);
834 zone
= &in
->questions
[0];
836 if (zone
->question_class
!= DNS_QCLASS_IN
&&
837 zone
->question_class
!= DNS_QCLASS_ANY
) {
838 return DNS_ERR(NOT_IMPLEMENTED
);
841 if (zone
->question_type
!= DNS_QTYPE_SOA
) {
842 return DNS_ERR(FORMAT_ERROR
);
845 DEBUG(2, ("Got a dns update request.\n"));
847 for (z
= dns
->zones
; z
!= NULL
; z
= z
->next
) {
850 match
= dns_name_match(z
->name
, zone
->name
, &host_part_len
);
857 DEBUG(1, ("We're not authoritative for this zone\n"));
858 return DNS_ERR(NOTAUTH
);
861 if (host_part_len
!= 0) {
862 /* TODO: We need to delegate this one */
863 DEBUG(1, ("Would have to delegate zone '%s'.\n", zone
->name
));
864 return DNS_ERR(NOT_IMPLEMENTED
);
867 *prereq_count
= in
->ancount
;
868 *prereqs
= in
->answers
;
869 werror
= check_prerequisites(dns
, mem_ctx
, in
->questions
, *prereqs
,
871 W_ERROR_NOT_OK_RETURN(werror
);
873 werror
= dns_update_allowed(dns
, state
, &tkey
);
874 if (!W_ERROR_IS_OK(werror
)) {
878 *update_count
= in
->nscount
;
879 *updates
= in
->nsrecs
;
880 werror
= update_prescan(in
->questions
, *updates
, *update_count
);
881 DBG_DEBUG("update_prescan(): %s\n", win_errstr(werror
));
882 W_ERROR_NOT_OK_RETURN(werror
);
884 werror
= handle_updates(dns
, mem_ctx
, in
->questions
, *prereqs
,
885 *prereq_count
, *updates
, *update_count
, tkey
);
886 DBG_DEBUG("handle_updates(): %s\n", win_errstr(werror
));
887 W_ERROR_NOT_OK_RETURN(werror
);