s4 dns: Implement RFC-compatible update prescan
[Samba/gebeck_regimport.git] / source4 / dns_server / dns_update.c
blob397384421c4c0ce2e638524b7ac9f257a4a8548c
1 /*
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/>.
22 #include "includes.h"
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"
27 #include <ldb.h>
28 #include "dsdb/samdb/samdb.h"
29 #include "dsdb/common/util.h"
30 #include "dns_server/dns_server.h"
32 static WERROR dns_rr_to_dnsp(TALLOC_CTX *mem_ctx,
33 const struct dns_res_rec *rrec,
34 struct dnsp_DnssrvRpcRecord *r);
36 static WERROR check_one_prerequisite(struct dns_server *dns,
37 TALLOC_CTX *mem_ctx,
38 const struct dns_name_question *zone,
39 const struct dns_res_rec *pr,
40 bool *final_result)
42 bool match;
43 WERROR werror;
44 struct ldb_dn *dn;
45 uint16_t i;
46 bool found = false;
47 struct dnsp_DnssrvRpcRecord *rec = NULL;
48 struct dnsp_DnssrvRpcRecord *ans;
49 uint16_t acount;
51 size_t host_part_len = 0;
53 *final_result = true;
55 if (pr->ttl != 0) {
56 return DNS_ERR(FORMAT_ERROR);
59 match = dns_name_match(zone->name, pr->name, &host_part_len);
60 if (!match) {
61 return DNS_ERR(NOTZONE);
64 werror = dns_name2dn(dns, mem_ctx, pr->name, &dn);
65 W_ERROR_NOT_OK_RETURN(werror);
67 if (pr->rr_class == DNS_QCLASS_ANY) {
69 if (pr->length != 0) {
70 return DNS_ERR(FORMAT_ERROR);
74 if (pr->rr_type == DNS_QTYPE_ALL) {
77 werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
78 W_ERROR_NOT_OK_RETURN(werror);
80 if (acount == 0) {
81 return DNS_ERR(NAME_ERROR);
83 } else {
86 werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
87 if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
88 return DNS_ERR(NXRRSET);
90 W_ERROR_NOT_OK_RETURN(werror);
92 for (i = 0; i < acount; i++) {
93 if (ans[i].wType == pr->rr_type) {
94 found = true;
95 break;
98 if (!found) {
99 return DNS_ERR(NXRRSET);
104 * RFC2136 3.2.5 doesn't actually mention the need to return
105 * OK here, but otherwise we'd always return a FORMAT_ERROR
106 * later on. This also matches Microsoft DNS behavior.
108 return WERR_OK;
111 if (pr->rr_class == DNS_QCLASS_NONE) {
112 if (pr->length != 0) {
113 return DNS_ERR(FORMAT_ERROR);
116 if (pr->rr_type == DNS_QTYPE_ALL) {
119 werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
120 if (W_ERROR_EQUAL(werror, WERR_OK)) {
121 return DNS_ERR(YXDOMAIN);
123 } else {
126 werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
127 if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
128 werror = WERR_OK;
129 ans = NULL;
130 acount = 0;
133 for (i = 0; i < acount; i++) {
134 if (ans[i].wType == pr->rr_type) {
135 found = true;
136 break;
139 if (found) {
140 return DNS_ERR(YXRRSET);
145 * RFC2136 3.2.5 doesn't actually mention the need to return
146 * OK here, but otherwise we'd always return a FORMAT_ERROR
147 * later on. This also matches Microsoft DNS behavior.
149 return WERR_OK;
152 if (pr->rr_class != zone->question_class) {
153 return DNS_ERR(FORMAT_ERROR);
156 *final_result = false;
158 werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
159 if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
160 return DNS_ERR(NXRRSET);
162 W_ERROR_NOT_OK_RETURN(werror);
164 rec = talloc_zero(mem_ctx, struct dnsp_DnssrvRpcRecord);
165 W_ERROR_HAVE_NO_MEMORY(rec);
167 werror = dns_rr_to_dnsp(rec, pr, rec);
168 W_ERROR_NOT_OK_RETURN(werror);
170 for (i = 0; i < acount; i++) {
171 if (dns_records_match(rec, &ans[i])) {
172 found = true;
173 break;
177 if (!found) {
178 return DNS_ERR(NXRRSET);
181 return WERR_OK;
184 static WERROR check_prerequisites(struct dns_server *dns,
185 TALLOC_CTX *mem_ctx,
186 const struct dns_name_question *zone,
187 const struct dns_res_rec *prereqs, uint16_t count)
189 uint16_t i;
190 WERROR final_error = WERR_OK;
192 for (i = 0; i < count; i++) {
193 bool final;
194 WERROR werror;
196 werror = check_one_prerequisite(dns, mem_ctx, zone,
197 &prereqs[i], &final);
198 if (!W_ERROR_IS_OK(werror)) {
199 if (final) {
200 return werror;
202 if (W_ERROR_IS_OK(final_error)) {
203 final_error = werror;
208 if (!W_ERROR_IS_OK(final_error)) {
209 return final_error;
212 return WERR_OK;
215 static WERROR update_prescan(const struct dns_name_question *zone,
216 const struct dns_res_rec *updates, uint16_t count)
218 const struct dns_res_rec *r;
219 uint16_t i;
220 size_t host_part_len;
221 bool match;
223 for (i = 0; i < count; i++) {
224 r = &updates[i];
225 match = dns_name_match(zone->name, r->name, &host_part_len);
226 if (!match) {
227 return DNS_ERR(NOTZONE);
229 if (zone->question_class == r->rr_class) {
230 if (r->rr_type == DNS_QTYPE_ALL) {
231 return DNS_ERR(FORMAT_ERROR);
233 if (r->rr_type == DNS_QTYPE_AXFR) {
234 return DNS_ERR(FORMAT_ERROR);
236 if (r->rr_type == DNS_QTYPE_MAILB) {
237 return DNS_ERR(FORMAT_ERROR);
239 if (r->rr_type == DNS_QTYPE_MAILA) {
240 return DNS_ERR(FORMAT_ERROR);
242 } else if (r->rr_class == DNS_QCLASS_ANY) {
243 if (r->ttl != 0) {
244 return DNS_ERR(FORMAT_ERROR);
246 if (r->length != 0) {
247 return DNS_ERR(FORMAT_ERROR);
249 if (r->rr_type == DNS_QTYPE_AXFR) {
250 return DNS_ERR(FORMAT_ERROR);
252 if (r->rr_type == DNS_QTYPE_MAILB) {
253 return DNS_ERR(FORMAT_ERROR);
255 if (r->rr_type == DNS_QTYPE_MAILA) {
256 return DNS_ERR(FORMAT_ERROR);
258 } else if (r->rr_class == DNS_QCLASS_NONE) {
259 if (r->ttl != 0) {
260 return DNS_ERR(FORMAT_ERROR);
262 if (r->rr_type == DNS_QTYPE_ALL) {
263 return DNS_ERR(FORMAT_ERROR);
265 if (r->rr_type == DNS_QTYPE_AXFR) {
266 return DNS_ERR(FORMAT_ERROR);
268 if (r->rr_type == DNS_QTYPE_MAILB) {
269 return DNS_ERR(FORMAT_ERROR);
271 if (r->rr_type == DNS_QTYPE_MAILA) {
272 return DNS_ERR(FORMAT_ERROR);
274 } else {
275 return DNS_ERR(FORMAT_ERROR);
278 return WERR_OK;
281 static WERROR dns_rr_to_dnsp(TALLOC_CTX *mem_ctx,
282 const struct dns_res_rec *rrec,
283 struct dnsp_DnssrvRpcRecord *r)
285 if (rrec->rr_type == DNS_QTYPE_ALL) {
286 return DNS_ERR(FORMAT_ERROR);
289 ZERO_STRUCTP(r);
291 r->wType = rrec->rr_type;
292 r->dwTtlSeconds = rrec->ttl;
293 r->rank = DNS_RANK_ZONE;
294 /* TODO: Autogenerate this somehow */
295 r->dwSerial = 110;
297 /* If we get QCLASS_ANY, we're done here */
298 if (rrec->rr_class == DNS_QCLASS_ANY) {
299 goto done;
302 switch(rrec->rr_type) {
303 case DNS_QTYPE_A:
304 r->data.ipv4 = talloc_strdup(mem_ctx, rrec->rdata.ipv4_record);
305 W_ERROR_HAVE_NO_MEMORY(r->data.ipv4);
306 break;
307 case DNS_QTYPE_AAAA:
308 r->data.ipv6 = talloc_strdup(mem_ctx, rrec->rdata.ipv6_record);
309 W_ERROR_HAVE_NO_MEMORY(r->data.ipv6);
310 break;
311 case DNS_QTYPE_NS:
312 r->data.ns = talloc_strdup(mem_ctx, rrec->rdata.ns_record);
313 W_ERROR_HAVE_NO_MEMORY(r->data.ns);
314 break;
315 case DNS_QTYPE_CNAME:
316 r->data.cname = talloc_strdup(mem_ctx, rrec->rdata.cname_record);
317 W_ERROR_HAVE_NO_MEMORY(r->data.cname);
318 break;
319 case DNS_QTYPE_SRV:
320 r->data.srv.wPriority = rrec->rdata.srv_record.priority;
321 r->data.srv.wWeight = rrec->rdata.srv_record.weight;
322 r->data.srv.wPort = rrec->rdata.srv_record.port;
323 r->data.srv.nameTarget = talloc_strdup(mem_ctx,
324 rrec->rdata.srv_record.target);
325 W_ERROR_HAVE_NO_MEMORY(r->data.srv.nameTarget);
326 break;
327 case DNS_QTYPE_MX:
328 r->data.mx.wPriority = rrec->rdata.mx_record.preference;
329 r->data.mx.nameTarget = talloc_strdup(mem_ctx,
330 rrec->rdata.mx_record.exchange);
331 W_ERROR_HAVE_NO_MEMORY(r->data.mx.nameTarget);
332 break;
333 case DNS_QTYPE_TXT:
334 r->data.txt = talloc_strdup(mem_ctx, rrec->rdata.txt_record.txt);
335 W_ERROR_HAVE_NO_MEMORY(r->data.txt);
336 break;
337 default:
338 DEBUG(0, ("Got a qytpe of %d\n", rrec->rr_type));
339 return DNS_ERR(NOT_IMPLEMENTED);
342 done:
344 return WERR_OK;
347 WERROR dns_server_process_update(struct dns_server *dns,
348 TALLOC_CTX *mem_ctx,
349 struct dns_name_packet *in,
350 struct dns_res_rec **prereqs, uint16_t *prereq_count,
351 struct dns_res_rec **updates, uint16_t *update_count,
352 struct dns_res_rec **additional, uint16_t *arcount)
354 struct dns_name_question *zone;
355 const struct dns_server_zone *z;
356 size_t host_part_len = 0;
357 WERROR werror = DNS_ERR(NOT_IMPLEMENTED);
358 bool update_allowed = false;
360 if (in->qdcount != 1) {
361 return DNS_ERR(FORMAT_ERROR);
364 zone = &in->questions[0];
366 if (zone->question_class != DNS_QCLASS_IN &&
367 zone->question_class != DNS_QCLASS_ANY) {
368 return DNS_ERR(NOT_IMPLEMENTED);
371 if (zone->question_type != DNS_QTYPE_SOA) {
372 return DNS_ERR(FORMAT_ERROR);
375 DEBUG(0, ("Got a dns update request.\n"));
377 for (z = dns->zones; z != NULL; z = z->next) {
378 bool match;
380 match = dns_name_match(z->name, zone->name, &host_part_len);
381 if (match) {
382 break;
386 if (z == NULL) {
387 return DNS_ERR(NOTAUTH);
390 if (host_part_len != 0) {
391 /* TODO: We need to delegate this one */
392 return DNS_ERR(NOT_IMPLEMENTED);
395 *prereq_count = in->ancount;
396 *prereqs = in->answers;
397 werror = check_prerequisites(dns, mem_ctx, in->questions, *prereqs,
398 *prereq_count);
399 W_ERROR_NOT_OK_RETURN(werror);
401 /* TODO: Check if update is allowed, we probably want "always",
402 * key-based GSSAPI, key-based bind-style TSIG and "never" as
403 * smb.conf options. */
404 if (!update_allowed) {
405 return DNS_ERR(REFUSED);
408 *update_count = in->nscount;
409 *updates = in->nsrecs;
410 werror = update_prescan(in->questions, *updates, *update_count);
411 W_ERROR_NOT_OK_RETURN(werror);
413 return werror;