s4 dns: Implement RFC-compatible update prescan
[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 "dsdb/samdb/samdb.h"
29 #include "dsdb/common/util.h"
30 #include "dns_server/dns_server.h"
31
32 static WERROR dns_rr_to_dnsp(TALLOC_CTX *mem_ctx,
33                              const struct dns_res_rec *rrec,
34                              struct dnsp_DnssrvRpcRecord *r);
35
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)
41 {
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;
50
51         size_t host_part_len = 0;
52
53         *final_result = true;
54
55         if (pr->ttl != 0) {
56                 return DNS_ERR(FORMAT_ERROR);
57         }
58
59         match = dns_name_match(zone->name, pr->name, &host_part_len);
60         if (!match) {
61                 return DNS_ERR(NOTZONE);
62         }
63
64         werror = dns_name2dn(dns, mem_ctx, pr->name, &dn);
65         W_ERROR_NOT_OK_RETURN(werror);
66
67         if (pr->rr_class == DNS_QCLASS_ANY) {
68
69                 if (pr->length != 0) {
70                         return DNS_ERR(FORMAT_ERROR);
71                 }
72
73
74                 if (pr->rr_type == DNS_QTYPE_ALL) {
75                         /*
76                          */
77                         werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
78                         W_ERROR_NOT_OK_RETURN(werror);
79
80                         if (acount == 0) {
81                                 return DNS_ERR(NAME_ERROR);
82                         }
83                 } else {
84                         /*
85                          */
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);
89                         }
90                         W_ERROR_NOT_OK_RETURN(werror);
91
92                         for (i = 0; i < acount; i++) {
93                                 if (ans[i].wType == pr->rr_type) {
94                                         found = true;
95                                         break;
96                                 }
97                         }
98                         if (!found) {
99                                 return DNS_ERR(NXRRSET);
100                         }
101                 }
102
103                 /*
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.
107                  */
108                 return WERR_OK;
109         }
110
111         if (pr->rr_class == DNS_QCLASS_NONE) {
112                 if (pr->length != 0) {
113                         return DNS_ERR(FORMAT_ERROR);
114                 }
115
116                 if (pr->rr_type == DNS_QTYPE_ALL) {
117                         /*
118                          */
119                         werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &acount);
120                         if (W_ERROR_EQUAL(werror, WERR_OK)) {
121                                 return DNS_ERR(YXDOMAIN);
122                         }
123                 } else {
124                         /*
125                          */
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;
131                         }
132
133                         for (i = 0; i < acount; i++) {
134                                 if (ans[i].wType == pr->rr_type) {
135                                         found = true;
136                                         break;
137                                 }
138                         }
139                         if (found) {
140                                 return DNS_ERR(YXRRSET);
141                         }
142                 }
143
144                 /*
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.
148                  */
149                 return WERR_OK;
150         }
151
152         if (pr->rr_class != zone->question_class) {
153                 return DNS_ERR(FORMAT_ERROR);
154         }
155
156         *final_result = false;
157
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);
161         }
162         W_ERROR_NOT_OK_RETURN(werror);
163
164         rec = talloc_zero(mem_ctx, struct dnsp_DnssrvRpcRecord);
165         W_ERROR_HAVE_NO_MEMORY(rec);
166
167         werror = dns_rr_to_dnsp(rec, pr, rec);
168         W_ERROR_NOT_OK_RETURN(werror);
169
170         for (i = 0; i < acount; i++) {
171                 if (dns_records_match(rec, &ans[i])) {
172                         found = true;
173                         break;
174                 }
175         }
176
177         if (!found) {
178                 return DNS_ERR(NXRRSET);
179         }
180
181         return WERR_OK;
182 }
183
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)
188 {
189         uint16_t i;
190         WERROR final_error = WERR_OK;
191
192         for (i = 0; i < count; i++) {
193                 bool final;
194                 WERROR werror;
195
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;
201                         }
202                         if (W_ERROR_IS_OK(final_error)) {
203                                 final_error = werror;
204                         }
205                 }
206         }
207
208         if (!W_ERROR_IS_OK(final_error)) {
209                 return final_error;
210         }
211
212         return WERR_OK;
213 }
214
215 static WERROR update_prescan(const struct dns_name_question *zone,
216                              const struct dns_res_rec *updates, uint16_t count)
217 {
218         const struct dns_res_rec *r;
219         uint16_t i;
220         size_t host_part_len;
221         bool match;
222
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);
228                 }
229                 if (zone->question_class == r->rr_class) {
230                         if (r->rr_type == DNS_QTYPE_ALL) {
231                                 return DNS_ERR(FORMAT_ERROR);
232                         }
233                         if (r->rr_type == DNS_QTYPE_AXFR) {
234                                 return DNS_ERR(FORMAT_ERROR);
235                         }
236                         if (r->rr_type == DNS_QTYPE_MAILB) {
237                                 return DNS_ERR(FORMAT_ERROR);
238                         }
239                         if (r->rr_type == DNS_QTYPE_MAILA) {
240                                 return DNS_ERR(FORMAT_ERROR);
241                         }
242                 } else if (r->rr_class == DNS_QCLASS_ANY) {
243                         if (r->ttl != 0) {
244                                 return DNS_ERR(FORMAT_ERROR);
245                         }
246                         if (r->length != 0) {
247                                 return DNS_ERR(FORMAT_ERROR);
248                         }
249                         if (r->rr_type == DNS_QTYPE_AXFR) {
250                                 return DNS_ERR(FORMAT_ERROR);
251                         }
252                         if (r->rr_type == DNS_QTYPE_MAILB) {
253                                 return DNS_ERR(FORMAT_ERROR);
254                         }
255                         if (r->rr_type == DNS_QTYPE_MAILA) {
256                                 return DNS_ERR(FORMAT_ERROR);
257                         }
258                 } else if (r->rr_class == DNS_QCLASS_NONE) {
259                         if (r->ttl != 0) {
260                                 return DNS_ERR(FORMAT_ERROR);
261                         }
262                         if (r->rr_type == DNS_QTYPE_ALL) {
263                                 return DNS_ERR(FORMAT_ERROR);
264                         }
265                         if (r->rr_type == DNS_QTYPE_AXFR) {
266                                 return DNS_ERR(FORMAT_ERROR);
267                         }
268                         if (r->rr_type == DNS_QTYPE_MAILB) {
269                                 return DNS_ERR(FORMAT_ERROR);
270                         }
271                         if (r->rr_type == DNS_QTYPE_MAILA) {
272                                 return DNS_ERR(FORMAT_ERROR);
273                         }
274                 } else {
275                         return DNS_ERR(FORMAT_ERROR);
276                 }
277         }
278         return WERR_OK;
279 }
280
281 static WERROR dns_rr_to_dnsp(TALLOC_CTX *mem_ctx,
282                              const struct dns_res_rec *rrec,
283                              struct dnsp_DnssrvRpcRecord *r)
284 {
285         if (rrec->rr_type == DNS_QTYPE_ALL) {
286                 return DNS_ERR(FORMAT_ERROR);
287         }
288
289         ZERO_STRUCTP(r);
290
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;
296
297         /* If we get QCLASS_ANY, we're done here */
298         if (rrec->rr_class == DNS_QCLASS_ANY) {
299                 goto done;
300         }
301
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);
340         }
341
342 done:
343
344         return WERR_OK;
345 }
346
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)
353 {
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;
359
360         if (in->qdcount != 1) {
361                 return DNS_ERR(FORMAT_ERROR);
362         }
363
364         zone = &in->questions[0];
365
366         if (zone->question_class != DNS_QCLASS_IN &&
367             zone->question_class != DNS_QCLASS_ANY) {
368                 return DNS_ERR(NOT_IMPLEMENTED);
369         }
370
371         if (zone->question_type != DNS_QTYPE_SOA) {
372                 return DNS_ERR(FORMAT_ERROR);
373         }
374
375         DEBUG(0, ("Got a dns update request.\n"));
376
377         for (z = dns->zones; z != NULL; z = z->next) {
378                 bool match;
379
380                 match = dns_name_match(z->name, zone->name, &host_part_len);
381                 if (match) {
382                         break;
383                 }
384         }
385
386         if (z == NULL) {
387                 return DNS_ERR(NOTAUTH);
388         }
389
390         if (host_part_len != 0) {
391                 /* TODO: We need to delegate this one */
392                 return DNS_ERR(NOT_IMPLEMENTED);
393         }
394
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);
400
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);
406         }
407
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);
412
413         return werror;
414 }