dns_server: Put more code in common
[samba.git] / source4 / dns_server / dns_utils.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    DNS server utils
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 "libcli/util/werror.h"
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/gen_ndr/ndr_dns.h"
27 #include "librpc/gen_ndr/ndr_dnsp.h"
28 #include <ldb.h>
29 #include "dsdb/samdb/samdb.h"
30 #include "dsdb/common/util.h"
31 #include "dns_server/dns_server.h"
32
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_DNS
35
36 /* Names are equal if they match and there's nothing left over */
37 bool dns_name_equal(const char *name1, const char *name2)
38 {
39         size_t host_part_len;
40         bool ret = dns_name_match(name1, name2, &host_part_len);
41
42         return ret && (host_part_len == 0);
43 }
44
45 /*
46   see if two dns records match
47  */
48 bool dns_records_match(struct dnsp_DnssrvRpcRecord *rec1,
49                        struct dnsp_DnssrvRpcRecord *rec2)
50 {
51         bool status;
52         int i;
53
54         if (rec1->wType != rec2->wType) {
55                 return false;
56         }
57
58         /* see if the data matches */
59         switch (rec1->wType) {
60         case DNS_TYPE_A:
61                 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
62         case DNS_TYPE_AAAA:
63                 return strcmp(rec1->data.ipv6, rec2->data.ipv6) == 0;
64         case DNS_TYPE_CNAME:
65                 return dns_name_equal(rec1->data.cname, rec2->data.cname);
66         case DNS_TYPE_TXT:
67                 if (rec1->data.txt.count != rec2->data.txt.count) {
68                         return false;
69                 }
70                 status = true;
71                 for (i=0; i<rec1->data.txt.count; i++) {
72                         status = status && (strcmp(rec1->data.txt.str[i],
73                                                 rec2->data.txt.str[i]) == 0);
74                 }
75                 return status;
76         case DNS_TYPE_PTR:
77                 return strcmp(rec1->data.ptr, rec2->data.ptr) == 0;
78         case DNS_TYPE_NS:
79                 return dns_name_equal(rec1->data.ns, rec2->data.ns);
80
81         case DNS_TYPE_SRV:
82                 return rec1->data.srv.wPriority == rec2->data.srv.wPriority &&
83                         rec1->data.srv.wWeight  == rec2->data.srv.wWeight &&
84                         rec1->data.srv.wPort    == rec2->data.srv.wPort &&
85                         dns_name_equal(rec1->data.srv.nameTarget, rec2->data.srv.nameTarget);
86
87         case DNS_TYPE_MX:
88                 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
89                         dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
90
91         case DNS_TYPE_HINFO:
92                 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
93                         strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
94
95         case DNS_TYPE_SOA:
96                 return dns_name_equal(rec1->data.soa.mname, rec2->data.soa.mname) &&
97                         dns_name_equal(rec1->data.soa.rname, rec2->data.soa.rname) &&
98                         rec1->data.soa.serial == rec2->data.soa.serial &&
99                         rec1->data.soa.refresh == rec2->data.soa.refresh &&
100                         rec1->data.soa.retry == rec2->data.soa.retry &&
101                         rec1->data.soa.expire == rec2->data.soa.expire &&
102                         rec1->data.soa.minimum == rec2->data.soa.minimum;
103         default:
104                 break;
105         }
106
107         return false;
108 }
109
110 WERROR dns_lookup_records(struct dns_server *dns,
111                           TALLOC_CTX *mem_ctx,
112                           struct ldb_dn *dn,
113                           struct dnsp_DnssrvRpcRecord **records,
114                           uint16_t *rec_count)
115 {
116         return dns_common_lookup(dns->samdb, mem_ctx, dn,
117                                  records, rec_count, NULL);
118 }
119
120 WERROR dns_replace_records(struct dns_server *dns,
121                            TALLOC_CTX *mem_ctx,
122                            struct ldb_dn *dn,
123                            bool needs_add,
124                            struct dnsp_DnssrvRpcRecord *records,
125                            uint16_t rec_count)
126 {
127         /* TODO: Autogenerate this somehow */
128         uint32_t dwSerial = 110;
129         return dns_common_replace(dns->samdb, mem_ctx, dn,
130                                   needs_add, dwSerial, records, rec_count);
131 }
132
133 bool dns_authorative_for_zone(struct dns_server *dns,
134                               const char *name)
135 {
136         const struct dns_server_zone *z;
137         size_t host_part_len = 0;
138
139         if (name == NULL) {
140                 return false;
141         }
142
143         if (strcmp(name, "") == 0) {
144                 return true;
145         }
146         for (z = dns->zones; z != NULL; z = z->next) {
147                 bool match;
148
149                 match = dns_name_match(z->name, name, &host_part_len);
150                 if (match) {
151                         break;
152                 }
153         }
154         if (z == NULL) {
155                 return false;
156         }
157
158         return true;
159 }
160
161 const char *dns_get_authoritative_zone(struct dns_server *dns,
162                                        const char *name)
163 {
164         const struct dns_server_zone *z;
165         size_t host_part_len = 0;
166
167         for (z = dns->zones; z != NULL; z = z->next) {
168                 bool match;
169                 match = dns_name_match(z->name, name, &host_part_len);
170                 if (match) {
171                         return z->name;
172                 }
173         }
174         return NULL;
175 }
176
177 WERROR dns_name2dn(struct dns_server *dns,
178                    TALLOC_CTX *mem_ctx,
179                    const char *name,
180                    struct ldb_dn **dn)
181 {
182         return dns_common_name2dn(dns->samdb, dns->zones,
183                                   mem_ctx, name, dn);
184 }
185
186 WERROR dns_generate_options(struct dns_server *dns,
187                             TALLOC_CTX *mem_ctx,
188                             struct dns_res_rec **options)
189 {
190         struct dns_res_rec *o;
191
192         o = talloc_zero(mem_ctx, struct dns_res_rec);
193         if (o == NULL) {
194                 return WERR_NOMEM;
195         }
196         o->name = '\0';
197         o->rr_type = DNS_QTYPE_OPT;
198         /* This is ugly, but RFC2671 wants the payload size in this field */
199         o->rr_class = (enum dns_qclass) dns->max_payload;
200         o->ttl = 0;
201         o->length = 0;
202
203         *options = o;
204         return WERR_OK;
205 }