s4 dns: Allow updates based on smb.conf setting
[mat/samba.git] / source4 / dns_server / dns_update.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    DNS server handler for update requests
5
6    Copyright (C) 2010 Kai Blin  <kai@samba.org>
7
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.
12
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.
17
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/>.
20 */
21
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 "param/param.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "dsdb/common/util.h"
31 #include "smbd/service_task.h"
32 #include "dns_server/dns_server.h"
33 #include "dns_server/dns_update.h"
34
35 static WERROR dns_rr_to_dnsp(TALLOC_CTX *mem_ctx,
36                              const struct dns_res_rec *rrec,
37                              struct dnsp_DnssrvRpcRecord *r);
38
39 static WERROR check_one_prerequisite(struct dns_server *dns,
40                                      TALLOC_CTX *mem_ctx,
41                                      const struct dns_name_question *zone,
42                                      const struct dns_res_rec *pr,
43                                      bool *final_result)
44 {
45         bool match;
46         WERROR werror;
47         struct ldb_dn *dn;
48         uint16_t i;
49         bool found = false;
50         struct dnsp_DnssrvRpcRecord *rec = NULL;
51         struct dnsp_DnssrvRpcRecord *ans;
52         uint16_t acount;
53
54         size_t host_part_len = 0;
55
56         *final_result = true;
57
58         if (pr->ttl != 0) {
59                 return DNS_ERR(FORMAT_ERROR);
60         }
61
62         match = dns_name_match(zone->name, pr->name, &host_part_len);
63         if (!match) {
64                 return DNS_ERR(NOTZONE);
65         }
66
67         werror = dns_name2dn(dns, mem_ctx, pr->name, &dn);
68         W_ERROR_NOT_OK_RETURN(werror);
69
70         if (pr->rr_class == DNS_QCLASS_ANY) {
71
72                 if (pr->length != 0) {
73                         return DNS_ERR(FORMAT_ERROR);
74                 }
75
76
77                 if (pr->rr_type == DNS_QTYPE_ALL) {
78                         /*
79                          */
80                         werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
81                         W_ERROR_NOT_OK_RETURN(werror);
82
83                         if (acount == 0) {
84                                 return DNS_ERR(NAME_ERROR);
85                         }
86                 } else {
87                         /*
88                          */
89                         werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
90                         if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
91                                 return DNS_ERR(NXRRSET);
92                         }
93                         W_ERROR_NOT_OK_RETURN(werror);
94
95                         for (i = 0; i < acount; i++) {
96                                 if (ans[i].wType == pr->rr_type) {
97                                         found = true;
98                                         break;
99                                 }
100                         }
101                         if (!found) {
102                                 return DNS_ERR(NXRRSET);
103                         }
104                 }
105
106                 /*
107                  * RFC2136 3.2.5 doesn't actually mention the need to return
108                  * OK here, but otherwise we'd always return a FORMAT_ERROR
109                  * later on. This also matches Microsoft DNS behavior.
110                  */
111                 return WERR_OK;
112         }
113
114         if (pr->rr_class == DNS_QCLASS_NONE) {
115                 if (pr->length != 0) {
116                         return DNS_ERR(FORMAT_ERROR);
117                 }
118
119                 if (pr->rr_type == DNS_QTYPE_ALL) {
120                         /*
121                          */
122                         werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
123                         if (W_ERROR_EQUAL(werror, WERR_OK)) {
124                                 return DNS_ERR(YXDOMAIN);
125                         }
126                 } else {
127                         /*
128                          */
129                         werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
130                         if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
131                                 werror = WERR_OK;
132                                 ans = NULL;
133                                 acount = 0;
134                         }
135
136                         for (i = 0; i < acount; i++) {
137                                 if (ans[i].wType == pr->rr_type) {
138                                         found = true;
139                                         break;
140                                 }
141                         }
142                         if (found) {
143                                 return DNS_ERR(YXRRSET);
144                         }
145                 }
146
147                 /*
148                  * RFC2136 3.2.5 doesn't actually mention the need to return
149                  * OK here, but otherwise we'd always return a FORMAT_ERROR
150                  * later on. This also matches Microsoft DNS behavior.
151                  */
152                 return WERR_OK;
153         }
154
155         if (pr->rr_class != zone->question_class) {
156                 return DNS_ERR(FORMAT_ERROR);
157         }
158
159         *final_result = false;
160
161         werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
162         if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
163                 return DNS_ERR(NXRRSET);
164         }
165         W_ERROR_NOT_OK_RETURN(werror);
166
167         rec = talloc_zero(mem_ctx, struct dnsp_DnssrvRpcRecord);
168         W_ERROR_HAVE_NO_MEMORY(rec);
169
170         werror = dns_rr_to_dnsp(rec, pr, rec);
171         W_ERROR_NOT_OK_RETURN(werror);
172
173         for (i = 0; i < acount; i++) {
174                 if (dns_records_match(rec, &ans[i])) {
175                         found = true;
176                         break;
177                 }
178         }
179
180         if (!found) {
181                 return DNS_ERR(NXRRSET);
182         }
183
184         return WERR_OK;
185 }
186
187 static WERROR check_prerequisites(struct dns_server *dns,
188                                   TALLOC_CTX *mem_ctx,
189                                   const struct dns_name_question *zone,
190                                   const struct dns_res_rec *prereqs, uint16_t count)
191 {
192         uint16_t i;
193         WERROR final_error = WERR_OK;
194
195         for (i = 0; i < count; i++) {
196                 bool final;
197                 WERROR werror;
198
199                 werror = check_one_prerequisite(dns, mem_ctx, zone,
200                                                 &prereqs[i], &final);
201                 if (!W_ERROR_IS_OK(werror)) {
202                         if (final) {
203                                 return werror;
204                         }
205                         if (W_ERROR_IS_OK(final_error)) {
206                                 final_error = werror;
207                         }
208                 }
209         }
210
211         if (!W_ERROR_IS_OK(final_error)) {
212                 return final_error;
213         }
214
215         return WERR_OK;
216 }
217
218 static WERROR update_prescan(const struct dns_name_question *zone,
219                              const struct dns_res_rec *updates, uint16_t count)
220 {
221         const struct dns_res_rec *r;
222         uint16_t i;
223         size_t host_part_len;
224         bool match;
225
226         for (i = 0; i < count; i++) {
227                 r = &updates[i];
228                 match = dns_name_match(zone->name, r->name, &host_part_len);
229                 if (!match) {
230                         return DNS_ERR(NOTZONE);
231                 }
232                 if (zone->question_class == r->rr_class) {
233                         if (r->rr_type == DNS_QTYPE_ALL) {
234                                 return DNS_ERR(FORMAT_ERROR);
235                         }
236                         if (r->rr_type == DNS_QTYPE_AXFR) {
237                                 return DNS_ERR(FORMAT_ERROR);
238                         }
239                         if (r->rr_type == DNS_QTYPE_MAILB) {
240                                 return DNS_ERR(FORMAT_ERROR);
241                         }
242                         if (r->rr_type == DNS_QTYPE_MAILA) {
243                                 return DNS_ERR(FORMAT_ERROR);
244                         }
245                 } else if (r->rr_class == DNS_QCLASS_ANY) {
246                         if (r->ttl != 0) {
247                                 return DNS_ERR(FORMAT_ERROR);
248                         }
249                         if (r->length != 0) {
250                                 return DNS_ERR(FORMAT_ERROR);
251                         }
252                         if (r->rr_type == DNS_QTYPE_AXFR) {
253                                 return DNS_ERR(FORMAT_ERROR);
254                         }
255                         if (r->rr_type == DNS_QTYPE_MAILB) {
256                                 return DNS_ERR(FORMAT_ERROR);
257                         }
258                         if (r->rr_type == DNS_QTYPE_MAILA) {
259                                 return DNS_ERR(FORMAT_ERROR);
260                         }
261                 } else if (r->rr_class == DNS_QCLASS_NONE) {
262                         if (r->ttl != 0) {
263                                 return DNS_ERR(FORMAT_ERROR);
264                         }
265                         if (r->rr_type == DNS_QTYPE_ALL) {
266                                 return DNS_ERR(FORMAT_ERROR);
267                         }
268                         if (r->rr_type == DNS_QTYPE_AXFR) {
269                                 return DNS_ERR(FORMAT_ERROR);
270                         }
271                         if (r->rr_type == DNS_QTYPE_MAILB) {
272                                 return DNS_ERR(FORMAT_ERROR);
273                         }
274                         if (r->rr_type == DNS_QTYPE_MAILA) {
275                                 return DNS_ERR(FORMAT_ERROR);
276                         }
277                 } else {
278                         return DNS_ERR(FORMAT_ERROR);
279                 }
280         }
281         return WERR_OK;
282 }
283
284 static WERROR dns_rr_to_dnsp(TALLOC_CTX *mem_ctx,
285                              const struct dns_res_rec *rrec,
286                              struct dnsp_DnssrvRpcRecord *r)
287 {
288         if (rrec->rr_type == DNS_QTYPE_ALL) {
289                 return DNS_ERR(FORMAT_ERROR);
290         }
291
292         ZERO_STRUCTP(r);
293
294         r->wType = rrec->rr_type;
295         r->dwTtlSeconds = rrec->ttl;
296         r->rank = DNS_RANK_ZONE;
297         /* TODO: Autogenerate this somehow */
298         r->dwSerial = 110;
299
300         /* If we get QCLASS_ANY, we're done here */
301         if (rrec->rr_class == DNS_QCLASS_ANY) {
302                 goto done;
303         }
304
305         switch(rrec->rr_type) {
306         case DNS_QTYPE_A:
307                 r->data.ipv4 = talloc_strdup(mem_ctx, rrec->rdata.ipv4_record);
308                 W_ERROR_HAVE_NO_MEMORY(r->data.ipv4);
309                 break;
310         case DNS_QTYPE_AAAA:
311                 r->data.ipv6 = talloc_strdup(mem_ctx, rrec->rdata.ipv6_record);
312                 W_ERROR_HAVE_NO_MEMORY(r->data.ipv6);
313                 break;
314         case DNS_QTYPE_NS:
315                 r->data.ns = talloc_strdup(mem_ctx, rrec->rdata.ns_record);
316                 W_ERROR_HAVE_NO_MEMORY(r->data.ns);
317                 break;
318         case DNS_QTYPE_CNAME:
319                 r->data.cname = talloc_strdup(mem_ctx, rrec->rdata.cname_record);
320                 W_ERROR_HAVE_NO_MEMORY(r->data.cname);
321                 break;
322         case DNS_QTYPE_SRV:
323                 r->data.srv.wPriority = rrec->rdata.srv_record.priority;
324                 r->data.srv.wWeight = rrec->rdata.srv_record.weight;
325                 r->data.srv.wPort = rrec->rdata.srv_record.port;
326                 r->data.srv.nameTarget = talloc_strdup(mem_ctx,
327                                 rrec->rdata.srv_record.target);
328                 W_ERROR_HAVE_NO_MEMORY(r->data.srv.nameTarget);
329                 break;
330         case DNS_QTYPE_MX:
331                 r->data.mx.wPriority = rrec->rdata.mx_record.preference;
332                 r->data.mx.nameTarget = talloc_strdup(mem_ctx,
333                                 rrec->rdata.mx_record.exchange);
334                 W_ERROR_HAVE_NO_MEMORY(r->data.mx.nameTarget);
335                 break;
336         case DNS_QTYPE_TXT:
337                 r->data.txt = talloc_strdup(mem_ctx, rrec->rdata.txt_record.txt);
338                 W_ERROR_HAVE_NO_MEMORY(r->data.txt);
339                 break;
340         default:
341                 DEBUG(0, ("Got a qytpe of %d\n", rrec->rr_type));
342                 return DNS_ERR(NOT_IMPLEMENTED);
343         }
344
345 done:
346
347         return WERR_OK;
348 }
349
350
351 static WERROR handle_one_update(struct dns_server *dns,
352                                 TALLOC_CTX *mem_ctx,
353                                 const struct dns_name_question *zone,
354                                 const struct dns_res_rec *update)
355 {
356         struct dnsp_DnssrvRpcRecord *recs = NULL;
357         uint16_t rcount = 0;
358         struct ldb_dn *dn;
359         uint16_t i;
360         WERROR werror;
361         bool needs_add = false;
362
363         DEBUG(1, ("Looking at record: \n"));
364         NDR_PRINT_DEBUG(dns_res_rec, discard_const(update));
365
366         switch (update->rr_type) {
367         case DNS_QTYPE_A:
368                 break;
369         case DNS_QTYPE_NS:
370                 break;
371         case DNS_QTYPE_CNAME:
372                 break;
373         case DNS_QTYPE_SOA:
374                 break;
375         case DNS_QTYPE_PTR:
376                 break;
377         case DNS_QTYPE_MX:
378                 break;
379         case DNS_QTYPE_AAAA:
380                 break;
381         case DNS_QTYPE_SRV:
382                 break;
383         case DNS_QTYPE_TXT:
384                 break;
385         default:
386                 return DNS_ERR(NOT_IMPLEMENTED);
387         }
388
389         werror = dns_name2dn(dns, mem_ctx, update->name, &dn);
390         W_ERROR_NOT_OK_RETURN(werror);
391
392         werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rcount);
393         if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
394                 recs = NULL;
395                 rcount = 0;
396                 needs_add = true;
397                 werror = WERR_OK;
398         }
399         W_ERROR_NOT_OK_RETURN(werror);
400
401         if (update->rr_class == zone->question_class) {
402                 if (update->rr_type == DNS_QTYPE_CNAME) {
403                         /*
404                          * If there is a record in the directory
405                          * that's not a CNAME, ignore update
406                          */
407                         for (i = 0; i < rcount; i++) {
408                                 if (recs[i].wType != DNS_TYPE_CNAME) {
409                                         DEBUG(0, ("Skipping update\n"));
410                                         return WERR_OK;
411                                 }
412                                 break;
413                         }
414
415                         /*
416                          * There should be no entries besides one CNAME record
417                          * per name, so replace everything with the new CNAME
418                          */
419
420                         rcount = 1;
421                         recs = talloc_realloc(mem_ctx, recs,
422                                         struct dnsp_DnssrvRpcRecord, rcount);
423                         W_ERROR_HAVE_NO_MEMORY(recs);
424
425                         werror = dns_rr_to_dnsp(recs, update, &recs[0]);
426                         W_ERROR_NOT_OK_RETURN(werror);
427
428                         werror = dns_replace_records(dns, mem_ctx, dn,
429                                                      needs_add, recs, rcount);
430                         W_ERROR_NOT_OK_RETURN(werror);
431
432                         return WERR_OK;
433                 } else {
434                         /*
435                          * If there is a CNAME record for this name,
436                          * ignore update
437                          */
438                         for (i = 0; i < rcount; i++) {
439                                 if (recs[i].wType == DNS_TYPE_CNAME) {
440                                         DEBUG(0, ("Skipping update\n"));
441                                         return WERR_OK;
442                                 }
443                         }
444                 }
445                 if (update->rr_type == DNS_QTYPE_SOA) {
446                         bool found = false;
447
448                         /*
449                          * If the zone has no SOA record?? or update's
450                          * serial number is smaller than existing SOA's,
451                          * ignore update
452                          */
453                         for (i = 0; i < rcount; i++) {
454                                 if (recs[i].wType == DNS_TYPE_SOA) {
455                                         uint16_t n, o;
456
457                                         n = update->rdata.soa_record.serial;
458                                         o = recs[i].data.soa.serial;
459                                         /*
460                                          * TODO: Implement RFC 1982 comparison
461                                          * logic for RFC2136
462                                          */
463                                         if (n <= o) {
464                                                 DEBUG(0, ("Skipping update\n"));
465                                                 return WERR_OK;
466                                         }
467                                         found = true;
468                                         break;
469                                 }
470                         }
471                         if (!found) {
472                                 DEBUG(0, ("Skipping update\n"));
473                                 return WERR_OK;
474                         }
475
476                         werror = dns_rr_to_dnsp(mem_ctx, update, &recs[i]);
477                         W_ERROR_NOT_OK_RETURN(werror);
478
479                         for (i++; i < rcount; i++) {
480                                 if (recs[i].wType != DNS_TYPE_SOA) {
481                                         continue;
482                                 }
483
484                                 ZERO_STRUCT(recs[i]);
485                         }
486
487                         werror = dns_replace_records(dns, mem_ctx, dn,
488                                                      needs_add, recs, rcount);
489                         W_ERROR_NOT_OK_RETURN(werror);
490
491                         return WERR_OK;
492                 }
493
494                 recs = talloc_realloc(mem_ctx, recs,
495                                 struct dnsp_DnssrvRpcRecord, rcount+1);
496                 W_ERROR_HAVE_NO_MEMORY(recs);
497
498                 werror = dns_rr_to_dnsp(recs, update, &recs[rcount]);
499                 W_ERROR_NOT_OK_RETURN(werror);
500
501                 for (i = 0; i < rcount; i++) {
502                         if (!dns_records_match(&recs[i], &recs[rcount])) {
503                                 continue;
504                         }
505
506                         recs[i] = recs[rcount];
507
508                         werror = dns_replace_records(dns, mem_ctx, dn,
509                                                      needs_add, recs, rcount);
510                         W_ERROR_NOT_OK_RETURN(werror);
511
512                         return WERR_OK;
513                 }
514
515                 werror = dns_replace_records(dns, mem_ctx, dn,
516                                              needs_add, recs, rcount+1);
517                 W_ERROR_NOT_OK_RETURN(werror);
518
519                 return WERR_OK;
520         } else if (update->rr_class == DNS_QCLASS_ANY) {
521                 if (update->rr_type == DNS_QTYPE_ALL) {
522                         if (dns_name_equal(update->name, zone->name)) {
523                                 for (i = 0; i < rcount; i++) {
524
525                                         if (recs[i].wType == DNS_TYPE_SOA) {
526                                                 continue;
527                                         }
528
529                                         if (recs[i].wType == DNS_TYPE_NS) {
530                                                 continue;
531                                         }
532
533                                         ZERO_STRUCT(recs[i]);
534                                 }
535
536                         } else {
537                                 for (i = 0; i < rcount; i++) {
538                                         ZERO_STRUCT(recs[i]);
539                                 }
540                         }
541
542                 } else if (dns_name_equal(update->name, zone->name)) {
543
544                         if (update->rr_type == DNS_QTYPE_SOA) {
545                                 return WERR_OK;
546                         }
547
548                         if (update->rr_type == DNS_QTYPE_NS) {
549                                 return WERR_OK;
550                         }
551                 }
552                 for (i = 0; i < rcount; i++) {
553                         if (recs[i].wType == update->rr_type) {
554                                 ZERO_STRUCT(recs[i]);
555                         }
556                 }
557
558                 werror = dns_replace_records(dns, mem_ctx, dn,
559                                              needs_add, recs, rcount);
560                 W_ERROR_NOT_OK_RETURN(werror);
561
562                 return WERR_OK;
563         } else if (update->rr_class == DNS_QCLASS_NONE) {
564                 struct dnsp_DnssrvRpcRecord *del_rec;
565
566                 if (update->rr_type == DNS_QTYPE_SOA) {
567                         return WERR_OK;
568                 }
569                 if (update->rr_type == DNS_QTYPE_NS) {
570                         bool found = false;
571                         struct dnsp_DnssrvRpcRecord *ns_rec = talloc(mem_ctx,
572                                                 struct dnsp_DnssrvRpcRecord);
573                         W_ERROR_HAVE_NO_MEMORY(ns_rec);
574
575
576                         werror = dns_rr_to_dnsp(ns_rec, update, ns_rec);
577                         W_ERROR_NOT_OK_RETURN(werror);
578
579                         for (i = 0; i < rcount; i++) {
580                                 if (dns_records_match(ns_rec, &recs[i])) {
581                                         found = true;
582                                         break;
583                                 }
584                         }
585                         if (found) {
586                                 return WERR_OK;
587                         }
588                 }
589
590                 del_rec = talloc(mem_ctx, struct dnsp_DnssrvRpcRecord);
591                 W_ERROR_HAVE_NO_MEMORY(del_rec);
592
593                 werror = dns_rr_to_dnsp(del_rec, update, del_rec);
594                 W_ERROR_NOT_OK_RETURN(werror);
595
596                 for (i = 0; i < rcount; i++) {
597                         if (dns_records_match(del_rec, &recs[i])) {
598                                 ZERO_STRUCT(recs[i]);
599                         }
600                 }
601         }
602
603         return WERR_OK;
604 }
605
606 static WERROR handle_updates(struct dns_server *dns,
607                              TALLOC_CTX *mem_ctx,
608                              const struct dns_name_question *zone,
609                              const struct dns_res_rec *prereqs, uint16_t pcount,
610                              struct dns_res_rec *updates, uint16_t upd_count)
611 {
612         struct ldb_dn *zone_dn = NULL;
613         WERROR werror = WERR_OK;
614         int ret;
615         uint16_t ri;
616         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
617
618         werror = dns_name2dn(dns, tmp_ctx, zone->name, &zone_dn);
619         W_ERROR_NOT_OK_RETURN(werror);
620
621         ret = ldb_transaction_start(dns->samdb);
622         if (ret != LDB_SUCCESS) {
623                 return DNS_ERR(SERVER_FAILURE);
624         }
625
626         werror = check_prerequisites(dns, tmp_ctx, zone, prereqs, pcount);
627         W_ERROR_NOT_OK_GOTO(werror, failed);
628
629         DEBUG(0, ("update count is %u\n", upd_count));
630
631         for (ri = 0; ri < upd_count; ri++) {
632                 werror = handle_one_update(dns, tmp_ctx, zone,
633                                            &updates[ri]);
634                 W_ERROR_NOT_OK_GOTO(werror, failed);
635         }
636
637         ldb_transaction_commit(dns->samdb);
638         TALLOC_FREE(tmp_ctx);
639         return WERR_OK;
640
641 failed:
642         ldb_transaction_cancel(dns->samdb);
643         TALLOC_FREE(tmp_ctx);
644         return werror;
645
646 }
647
648 WERROR dns_server_process_update(struct dns_server *dns,
649                                  TALLOC_CTX *mem_ctx,
650                                  struct dns_name_packet *in,
651                                  struct dns_res_rec **prereqs,    uint16_t *prereq_count,
652                                  struct dns_res_rec **updates,    uint16_t *update_count,
653                                  struct dns_res_rec **additional, uint16_t *arcount)
654 {
655         struct dns_name_question *zone;
656         const struct dns_server_zone *z;
657         size_t host_part_len = 0;
658         WERROR werror = DNS_ERR(NOT_IMPLEMENTED);
659
660         if (in->qdcount != 1) {
661                 return DNS_ERR(FORMAT_ERROR);
662         }
663
664         zone = &in->questions[0];
665
666         if (zone->question_class != DNS_QCLASS_IN &&
667             zone->question_class != DNS_QCLASS_ANY) {
668                 return DNS_ERR(NOT_IMPLEMENTED);
669         }
670
671         if (zone->question_type != DNS_QTYPE_SOA) {
672                 return DNS_ERR(FORMAT_ERROR);
673         }
674
675         DEBUG(0, ("Got a dns update request.\n"));
676
677         for (z = dns->zones; z != NULL; z = z->next) {
678                 bool match;
679
680                 match = dns_name_match(z->name, zone->name, &host_part_len);
681                 if (match) {
682                         break;
683                 }
684         }
685
686         if (z == NULL) {
687                 DEBUG(0, ("We're not authorative for this zone\n"));
688                 return DNS_ERR(NOTAUTH);
689         }
690
691         if (host_part_len != 0) {
692                 /* TODO: We need to delegate this one */
693                 DEBUG(0, ("Would have to delegate zones.\n"));
694                 return DNS_ERR(NOT_IMPLEMENTED);
695         }
696
697         *prereq_count = in->ancount;
698         *prereqs = in->answers;
699         werror = check_prerequisites(dns, mem_ctx, in->questions, *prereqs,
700                                      *prereq_count);
701         W_ERROR_NOT_OK_RETURN(werror);
702
703         /* TODO: Check if update is allowed, we probably want "always",
704          * key-based GSSAPI, key-based bind-style TSIG and "never" as
705          * smb.conf options. */
706         if (lpcfg_allow_dns_updates(dns->task->lp_ctx) != DNS_UPDATE_ON) {
707                 DEBUG(0, ("Update not allowed."));
708                 return DNS_ERR(REFUSED);
709         }
710
711         *update_count = in->nscount;
712         *updates = in->nsrecs;
713         werror = update_prescan(in->questions, *updates, *update_count);
714         W_ERROR_NOT_OK_RETURN(werror);
715
716
717         werror = handle_updates(dns, mem_ctx, in->questions, *prereqs,
718                                 *prereq_count, *updates, *update_count);
719         W_ERROR_NOT_OK_RETURN(werror);
720
721         return werror;
722 }