s4 dns: Support TXT updates, add tests
[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         char *tmp;
289         char *txt_record_txt;
290         char *saveptr = NULL;
291
292         if (rrec->rr_type == DNS_QTYPE_ALL) {
293                 return DNS_ERR(FORMAT_ERROR);
294         }
295
296         ZERO_STRUCTP(r);
297
298         r->wType = rrec->rr_type;
299         r->dwTtlSeconds = rrec->ttl;
300         r->rank = DNS_RANK_ZONE;
301         /* TODO: Autogenerate this somehow */
302         r->dwSerial = 110;
303
304         /* If we get QCLASS_ANY, we're done here */
305         if (rrec->rr_class == DNS_QCLASS_ANY) {
306                 goto done;
307         }
308
309         switch(rrec->rr_type) {
310         case DNS_QTYPE_A:
311                 r->data.ipv4 = talloc_strdup(mem_ctx, rrec->rdata.ipv4_record);
312                 W_ERROR_HAVE_NO_MEMORY(r->data.ipv4);
313                 break;
314         case DNS_QTYPE_AAAA:
315                 r->data.ipv6 = talloc_strdup(mem_ctx, rrec->rdata.ipv6_record);
316                 W_ERROR_HAVE_NO_MEMORY(r->data.ipv6);
317                 break;
318         case DNS_QTYPE_NS:
319                 r->data.ns = talloc_strdup(mem_ctx, rrec->rdata.ns_record);
320                 W_ERROR_HAVE_NO_MEMORY(r->data.ns);
321                 break;
322         case DNS_QTYPE_CNAME:
323                 r->data.cname = talloc_strdup(mem_ctx, rrec->rdata.cname_record);
324                 W_ERROR_HAVE_NO_MEMORY(r->data.cname);
325                 break;
326         case DNS_QTYPE_SRV:
327                 r->data.srv.wPriority = rrec->rdata.srv_record.priority;
328                 r->data.srv.wWeight = rrec->rdata.srv_record.weight;
329                 r->data.srv.wPort = rrec->rdata.srv_record.port;
330                 r->data.srv.nameTarget = talloc_strdup(mem_ctx,
331                                 rrec->rdata.srv_record.target);
332                 W_ERROR_HAVE_NO_MEMORY(r->data.srv.nameTarget);
333                 break;
334         case DNS_QTYPE_MX:
335                 r->data.mx.wPriority = rrec->rdata.mx_record.preference;
336                 r->data.mx.nameTarget = talloc_strdup(mem_ctx,
337                                 rrec->rdata.mx_record.exchange);
338                 W_ERROR_HAVE_NO_MEMORY(r->data.mx.nameTarget);
339                 break;
340         case DNS_QTYPE_TXT:
341                 r->data.txt.count = 0;
342                 r->data.txt.str = talloc_array(mem_ctx, const char *,
343                                                r->data.txt.count);
344                 W_ERROR_HAVE_NO_MEMORY(r->data.txt.str);
345
346                 txt_record_txt = talloc_strdup(r->data.txt.str,
347                                                rrec->rdata.txt_record.txt);
348                 W_ERROR_HAVE_NO_MEMORY(txt_record_txt);
349
350                 tmp = strtok_r(txt_record_txt, "\"", &saveptr);
351                 while (tmp) {
352                         if (strcmp(tmp, " ") == 0) {
353                                 tmp = strtok_r(NULL, "\"", &saveptr);
354                                 continue;
355                         }
356                         r->data.txt.str = talloc_realloc(mem_ctx, r->data.txt.str, const char *,
357                                                         r->data.txt.count+1);
358                         r->data.txt.str[r->data.txt.count] = talloc_strdup(r->data.txt.str, tmp);
359                         W_ERROR_HAVE_NO_MEMORY(r->data.txt.str[r->data.txt.count]);
360
361                         r->data.txt.count++;
362                         tmp = strtok_r(NULL, "\"", &saveptr);
363                 }
364
365                 break;
366         default:
367                 DEBUG(0, ("Got a qytpe of %d\n", rrec->rr_type));
368                 return DNS_ERR(NOT_IMPLEMENTED);
369         }
370
371 done:
372
373         return WERR_OK;
374 }
375
376
377 static WERROR handle_one_update(struct dns_server *dns,
378                                 TALLOC_CTX *mem_ctx,
379                                 const struct dns_name_question *zone,
380                                 const struct dns_res_rec *update)
381 {
382         struct dnsp_DnssrvRpcRecord *recs = NULL;
383         uint16_t rcount = 0;
384         struct ldb_dn *dn;
385         uint16_t i;
386         WERROR werror;
387         bool needs_add = false;
388
389         DEBUG(1, ("Looking at record: \n"));
390         NDR_PRINT_DEBUG(dns_res_rec, discard_const(update));
391
392         switch (update->rr_type) {
393         case DNS_QTYPE_A:
394                 break;
395         case DNS_QTYPE_NS:
396                 break;
397         case DNS_QTYPE_CNAME:
398                 break;
399         case DNS_QTYPE_SOA:
400                 break;
401         case DNS_QTYPE_PTR:
402                 break;
403         case DNS_QTYPE_MX:
404                 break;
405         case DNS_QTYPE_AAAA:
406                 break;
407         case DNS_QTYPE_SRV:
408                 break;
409         case DNS_QTYPE_TXT:
410                 break;
411         default:
412                 DEBUG(0, ("Can't handle updates of type %u yet\n",
413                           update->rr_type));
414                 return DNS_ERR(NOT_IMPLEMENTED);
415         }
416
417         werror = dns_name2dn(dns, mem_ctx, update->name, &dn);
418         W_ERROR_NOT_OK_RETURN(werror);
419
420         werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rcount);
421         if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
422                 recs = NULL;
423                 rcount = 0;
424                 needs_add = true;
425                 werror = WERR_OK;
426         }
427         W_ERROR_NOT_OK_RETURN(werror);
428
429         if (update->rr_class == zone->question_class) {
430                 if (update->rr_type == DNS_QTYPE_CNAME) {
431                         /*
432                          * If there is a record in the directory
433                          * that's not a CNAME, ignore update
434                          */
435                         for (i = 0; i < rcount; i++) {
436                                 if (recs[i].wType != DNS_TYPE_CNAME) {
437                                         DEBUG(0, ("Skipping update\n"));
438                                         return WERR_OK;
439                                 }
440                                 break;
441                         }
442
443                         /*
444                          * There should be no entries besides one CNAME record
445                          * per name, so replace everything with the new CNAME
446                          */
447
448                         rcount = 1;
449                         recs = talloc_realloc(mem_ctx, recs,
450                                         struct dnsp_DnssrvRpcRecord, rcount);
451                         W_ERROR_HAVE_NO_MEMORY(recs);
452
453                         werror = dns_rr_to_dnsp(recs, update, &recs[0]);
454                         W_ERROR_NOT_OK_RETURN(werror);
455
456                         werror = dns_replace_records(dns, mem_ctx, dn,
457                                                      needs_add, recs, rcount);
458                         W_ERROR_NOT_OK_RETURN(werror);
459
460                         return WERR_OK;
461                 } else {
462                         /*
463                          * If there is a CNAME record for this name,
464                          * ignore update
465                          */
466                         for (i = 0; i < rcount; i++) {
467                                 if (recs[i].wType == DNS_TYPE_CNAME) {
468                                         DEBUG(0, ("Skipping update\n"));
469                                         return WERR_OK;
470                                 }
471                         }
472                 }
473                 if (update->rr_type == DNS_QTYPE_SOA) {
474                         bool found = false;
475
476                         /*
477                          * If the zone has no SOA record?? or update's
478                          * serial number is smaller than existing SOA's,
479                          * ignore update
480                          */
481                         for (i = 0; i < rcount; i++) {
482                                 if (recs[i].wType == DNS_TYPE_SOA) {
483                                         uint16_t n, o;
484
485                                         n = update->rdata.soa_record.serial;
486                                         o = recs[i].data.soa.serial;
487                                         /*
488                                          * TODO: Implement RFC 1982 comparison
489                                          * logic for RFC2136
490                                          */
491                                         if (n <= o) {
492                                                 DEBUG(0, ("Skipping update\n"));
493                                                 return WERR_OK;
494                                         }
495                                         found = true;
496                                         break;
497                                 }
498                         }
499                         if (!found) {
500                                 DEBUG(0, ("Skipping update\n"));
501                                 return WERR_OK;
502                         }
503
504                         werror = dns_rr_to_dnsp(mem_ctx, update, &recs[i]);
505                         W_ERROR_NOT_OK_RETURN(werror);
506
507                         for (i++; i < rcount; i++) {
508                                 if (recs[i].wType != DNS_TYPE_SOA) {
509                                         continue;
510                                 }
511
512                                 ZERO_STRUCT(recs[i]);
513                         }
514
515                         werror = dns_replace_records(dns, mem_ctx, dn,
516                                                      needs_add, recs, rcount);
517                         W_ERROR_NOT_OK_RETURN(werror);
518
519                         return WERR_OK;
520                 }
521
522                 recs = talloc_realloc(mem_ctx, recs,
523                                 struct dnsp_DnssrvRpcRecord, rcount+1);
524                 W_ERROR_HAVE_NO_MEMORY(recs);
525
526                 werror = dns_rr_to_dnsp(recs, update, &recs[rcount]);
527                 W_ERROR_NOT_OK_RETURN(werror);
528
529                 for (i = 0; i < rcount; i++) {
530                         if (!dns_records_match(&recs[i], &recs[rcount])) {
531                                 continue;
532                         }
533
534                         recs[i] = recs[rcount];
535
536                         werror = dns_replace_records(dns, mem_ctx, dn,
537                                                      needs_add, recs, rcount);
538                         W_ERROR_NOT_OK_RETURN(werror);
539
540                         return WERR_OK;
541                 }
542
543                 werror = dns_replace_records(dns, mem_ctx, dn,
544                                              needs_add, recs, rcount+1);
545                 W_ERROR_NOT_OK_RETURN(werror);
546
547                 return WERR_OK;
548         } else if (update->rr_class == DNS_QCLASS_ANY) {
549                 if (update->rr_type == DNS_QTYPE_ALL) {
550                         if (dns_name_equal(update->name, zone->name)) {
551                                 for (i = 0; i < rcount; i++) {
552
553                                         if (recs[i].wType == DNS_TYPE_SOA) {
554                                                 continue;
555                                         }
556
557                                         if (recs[i].wType == DNS_TYPE_NS) {
558                                                 continue;
559                                         }
560
561                                         ZERO_STRUCT(recs[i]);
562                                 }
563
564                         } else {
565                                 for (i = 0; i < rcount; i++) {
566                                         ZERO_STRUCT(recs[i]);
567                                 }
568                         }
569
570                 } else if (dns_name_equal(update->name, zone->name)) {
571
572                         if (update->rr_type == DNS_QTYPE_SOA) {
573                                 return WERR_OK;
574                         }
575
576                         if (update->rr_type == DNS_QTYPE_NS) {
577                                 return WERR_OK;
578                         }
579                 }
580                 for (i = 0; i < rcount; i++) {
581                         if (recs[i].wType == update->rr_type) {
582                                 ZERO_STRUCT(recs[i]);
583                         }
584                 }
585
586                 werror = dns_replace_records(dns, mem_ctx, dn,
587                                              needs_add, recs, rcount);
588                 W_ERROR_NOT_OK_RETURN(werror);
589
590                 return WERR_OK;
591         } else if (update->rr_class == DNS_QCLASS_NONE) {
592                 struct dnsp_DnssrvRpcRecord *del_rec;
593
594                 if (update->rr_type == DNS_QTYPE_SOA) {
595                         return WERR_OK;
596                 }
597                 if (update->rr_type == DNS_QTYPE_NS) {
598                         bool found = false;
599                         struct dnsp_DnssrvRpcRecord *ns_rec = talloc(mem_ctx,
600                                                 struct dnsp_DnssrvRpcRecord);
601                         W_ERROR_HAVE_NO_MEMORY(ns_rec);
602
603
604                         werror = dns_rr_to_dnsp(ns_rec, update, ns_rec);
605                         W_ERROR_NOT_OK_RETURN(werror);
606
607                         for (i = 0; i < rcount; i++) {
608                                 if (dns_records_match(ns_rec, &recs[i])) {
609                                         found = true;
610                                         break;
611                                 }
612                         }
613                         if (found) {
614                                 return WERR_OK;
615                         }
616                 }
617
618                 del_rec = talloc(mem_ctx, struct dnsp_DnssrvRpcRecord);
619                 W_ERROR_HAVE_NO_MEMORY(del_rec);
620
621                 werror = dns_rr_to_dnsp(del_rec, update, del_rec);
622                 W_ERROR_NOT_OK_RETURN(werror);
623
624                 for (i = 0; i < rcount; i++) {
625                         if (dns_records_match(del_rec, &recs[i])) {
626                                 ZERO_STRUCT(recs[i]);
627                         }
628                 }
629         }
630
631         return WERR_OK;
632 }
633
634 static WERROR handle_updates(struct dns_server *dns,
635                              TALLOC_CTX *mem_ctx,
636                              const struct dns_name_question *zone,
637                              const struct dns_res_rec *prereqs, uint16_t pcount,
638                              struct dns_res_rec *updates, uint16_t upd_count)
639 {
640         struct ldb_dn *zone_dn = NULL;
641         WERROR werror = WERR_OK;
642         int ret;
643         uint16_t ri;
644         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
645
646         werror = dns_name2dn(dns, tmp_ctx, zone->name, &zone_dn);
647         W_ERROR_NOT_OK_RETURN(werror);
648
649         ret = ldb_transaction_start(dns->samdb);
650         if (ret != LDB_SUCCESS) {
651                 return DNS_ERR(SERVER_FAILURE);
652         }
653
654         werror = check_prerequisites(dns, tmp_ctx, zone, prereqs, pcount);
655         W_ERROR_NOT_OK_GOTO(werror, failed);
656
657         DEBUG(0, ("update count is %u\n", upd_count));
658
659         for (ri = 0; ri < upd_count; ri++) {
660                 werror = handle_one_update(dns, tmp_ctx, zone,
661                                            &updates[ri]);
662                 W_ERROR_NOT_OK_GOTO(werror, failed);
663         }
664
665         ldb_transaction_commit(dns->samdb);
666         TALLOC_FREE(tmp_ctx);
667         return WERR_OK;
668
669 failed:
670         ldb_transaction_cancel(dns->samdb);
671         TALLOC_FREE(tmp_ctx);
672         return werror;
673
674 }
675
676 WERROR dns_server_process_update(struct dns_server *dns,
677                                  TALLOC_CTX *mem_ctx,
678                                  struct dns_name_packet *in,
679                                  struct dns_res_rec **prereqs,    uint16_t *prereq_count,
680                                  struct dns_res_rec **updates,    uint16_t *update_count,
681                                  struct dns_res_rec **additional, uint16_t *arcount)
682 {
683         struct dns_name_question *zone;
684         const struct dns_server_zone *z;
685         size_t host_part_len = 0;
686         WERROR werror = DNS_ERR(NOT_IMPLEMENTED);
687
688         if (in->qdcount != 1) {
689                 return DNS_ERR(FORMAT_ERROR);
690         }
691
692         zone = &in->questions[0];
693
694         if (zone->question_class != DNS_QCLASS_IN &&
695             zone->question_class != DNS_QCLASS_ANY) {
696                 return DNS_ERR(NOT_IMPLEMENTED);
697         }
698
699         if (zone->question_type != DNS_QTYPE_SOA) {
700                 return DNS_ERR(FORMAT_ERROR);
701         }
702
703         DEBUG(0, ("Got a dns update request.\n"));
704
705         for (z = dns->zones; z != NULL; z = z->next) {
706                 bool match;
707
708                 match = dns_name_match(z->name, zone->name, &host_part_len);
709                 if (match) {
710                         break;
711                 }
712         }
713
714         if (z == NULL) {
715                 DEBUG(0, ("We're not authorative for this zone\n"));
716                 return DNS_ERR(NOTAUTH);
717         }
718
719         if (host_part_len != 0) {
720                 /* TODO: We need to delegate this one */
721                 DEBUG(0, ("Would have to delegate zones.\n"));
722                 return DNS_ERR(NOT_IMPLEMENTED);
723         }
724
725         *prereq_count = in->ancount;
726         *prereqs = in->answers;
727         werror = check_prerequisites(dns, mem_ctx, in->questions, *prereqs,
728                                      *prereq_count);
729         W_ERROR_NOT_OK_RETURN(werror);
730
731         /* TODO: Check if update is allowed, we probably want "always",
732          * key-based GSSAPI, key-based bind-style TSIG and "never" as
733          * smb.conf options. */
734         if (lpcfg_allow_dns_updates(dns->task->lp_ctx) != DNS_UPDATE_ON) {
735                 DEBUG(0, ("Update not allowed."));
736                 return DNS_ERR(REFUSED);
737         }
738
739         *update_count = in->nscount;
740         *updates = in->nsrecs;
741         werror = update_prescan(in->questions, *updates, *update_count);
742         W_ERROR_NOT_OK_RETURN(werror);
743
744
745         werror = handle_updates(dns, mem_ctx, in->questions, *prereqs,
746                                 *prereq_count, *updates, *update_count);
747         W_ERROR_NOT_OK_RETURN(werror);
748
749         return werror;
750 }