s4:smbd/open: add missing TALLOC_FREE(frame) to inherit_new_acl()
[Samba/gebeck_regimport.git] / source4 / dns_server / dns_utils.c
blob398839390e15a9d76a3bc331f7edab55b7dbde14
1 /*
2 Unix SMB/CIFS implementation.
4 DNS server utils
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 "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"
33 uint8_t werr_to_dns_err(WERROR werr)
35 if (W_ERROR_EQUAL(WERR_OK, werr)) {
36 return DNS_RCODE_OK;
37 } else if (W_ERROR_EQUAL(DNS_ERR(FORMAT_ERROR), werr)) {
38 return DNS_RCODE_FORMERR;
39 } else if (W_ERROR_EQUAL(DNS_ERR(SERVER_FAILURE), werr)) {
40 return DNS_RCODE_SERVFAIL;
41 } else if (W_ERROR_EQUAL(DNS_ERR(NAME_ERROR), werr)) {
42 return DNS_RCODE_NXDOMAIN;
43 } else if (W_ERROR_EQUAL(DNS_ERR(NOT_IMPLEMENTED), werr)) {
44 return DNS_RCODE_NOTIMP;
45 } else if (W_ERROR_EQUAL(DNS_ERR(REFUSED), werr)) {
46 return DNS_RCODE_REFUSED;
47 } else if (W_ERROR_EQUAL(DNS_ERR(YXDOMAIN), werr)) {
48 return DNS_RCODE_YXDOMAIN;
49 } else if (W_ERROR_EQUAL(DNS_ERR(YXRRSET), werr)) {
50 return DNS_RCODE_YXRRSET;
51 } else if (W_ERROR_EQUAL(DNS_ERR(NXRRSET), werr)) {
52 return DNS_RCODE_NXRRSET;
53 } else if (W_ERROR_EQUAL(DNS_ERR(NOTAUTH), werr)) {
54 return DNS_RCODE_NOTAUTH;
55 } else if (W_ERROR_EQUAL(DNS_ERR(NOTZONE), werr)) {
56 return DNS_RCODE_NOTZONE;
57 } else if (W_ERROR_EQUAL(DNS_ERR(BADKEY), werr)) {
58 return DNS_RCODE_BADKEY;
60 DEBUG(5, ("No mapping exists for %s\n", win_errstr(werr)));
61 return DNS_RCODE_SERVFAIL;
64 bool dns_name_match(const char *zone, const char *name, size_t *host_part_len)
66 size_t zl = strlen(zone);
67 size_t nl = strlen(name);
68 ssize_t zi, ni;
69 static const size_t fixup = 'a' - 'A';
71 if (zl > nl) {
72 return false;
75 for (zi = zl, ni = nl; zi >= 0; zi--, ni--) {
76 char zc = zone[zi];
77 char nc = name[ni];
79 /* convert to lower case */
80 if (zc >= 'A' && zc <= 'Z') {
81 zc += fixup;
83 if (nc >= 'A' && nc <= 'Z') {
84 nc += fixup;
87 if (zc != nc) {
88 return false;
92 if (ni >= 0) {
93 if (name[ni] != '.') {
94 return false;
97 ni--;
100 *host_part_len = ni+1;
102 return true;
105 /* Names are equal if they match and there's nothing left over */
106 bool dns_name_equal(const char *name1, const char *name2)
108 size_t host_part_len;
109 bool ret = dns_name_match(name1, name2, &host_part_len);
111 return ret && (host_part_len == 0);
115 see if two dns records match
117 bool dns_records_match(struct dnsp_DnssrvRpcRecord *rec1,
118 struct dnsp_DnssrvRpcRecord *rec2)
120 bool status;
121 int i;
123 if (rec1->wType != rec2->wType) {
124 return false;
127 /* see if the data matches */
128 switch (rec1->wType) {
129 case DNS_TYPE_A:
130 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
131 case DNS_TYPE_AAAA:
132 return strcmp(rec1->data.ipv6, rec2->data.ipv6) == 0;
133 case DNS_TYPE_CNAME:
134 return dns_name_equal(rec1->data.cname, rec2->data.cname);
135 case DNS_TYPE_TXT:
136 if (rec1->data.txt.count != rec2->data.txt.count) {
137 return false;
139 status = true;
140 for (i=0; i<rec1->data.txt.count; i++) {
141 status = status && (strcmp(rec1->data.txt.str[i],
142 rec2->data.txt.str[i]) == 0);
144 return status;
145 case DNS_TYPE_PTR:
146 return strcmp(rec1->data.ptr, rec2->data.ptr) == 0;
147 case DNS_TYPE_NS:
148 return dns_name_equal(rec1->data.ns, rec2->data.ns);
150 case DNS_TYPE_SRV:
151 return rec1->data.srv.wPriority == rec2->data.srv.wPriority &&
152 rec1->data.srv.wWeight == rec2->data.srv.wWeight &&
153 rec1->data.srv.wPort == rec2->data.srv.wPort &&
154 dns_name_equal(rec1->data.srv.nameTarget, rec2->data.srv.nameTarget);
156 case DNS_TYPE_MX:
157 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
158 dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
160 case DNS_TYPE_HINFO:
161 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
162 strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
164 case DNS_TYPE_SOA:
165 return dns_name_equal(rec1->data.soa.mname, rec2->data.soa.mname) &&
166 dns_name_equal(rec1->data.soa.rname, rec2->data.soa.rname) &&
167 rec1->data.soa.serial == rec2->data.soa.serial &&
168 rec1->data.soa.refresh == rec2->data.soa.refresh &&
169 rec1->data.soa.retry == rec2->data.soa.retry &&
170 rec1->data.soa.expire == rec2->data.soa.expire &&
171 rec1->data.soa.minimum == rec2->data.soa.minimum;
172 default:
173 break;
176 return false;
179 WERROR dns_lookup_records(struct dns_server *dns,
180 TALLOC_CTX *mem_ctx,
181 struct ldb_dn *dn,
182 struct dnsp_DnssrvRpcRecord **records,
183 uint16_t *rec_count)
185 static const char * const attrs[] = { "dnsRecord", NULL};
186 struct ldb_message_element *el;
187 uint16_t ri;
188 int ret;
189 struct ldb_message *msg = NULL;
190 struct dnsp_DnssrvRpcRecord *recs;
192 ret = dsdb_search_one(dns->samdb, mem_ctx, &msg, dn,
193 LDB_SCOPE_BASE, attrs, 0, "%s", "(objectClass=dnsNode)");
194 if (ret != LDB_SUCCESS) {
195 /* TODO: we need to check if there's a glue record we need to
196 * create a referral to */
197 return DNS_ERR(NAME_ERROR);
200 el = ldb_msg_find_element(msg, attrs[0]);
201 if (el == NULL) {
202 *records = NULL;
203 *rec_count = 0;
204 return DNS_ERR(NAME_ERROR);
207 recs = talloc_zero_array(mem_ctx, struct dnsp_DnssrvRpcRecord, el->num_values);
208 if (recs == NULL) {
209 return WERR_NOMEM;
211 for (ri = 0; ri < el->num_values; ri++) {
212 struct ldb_val *v = &el->values[ri];
213 enum ndr_err_code ndr_err;
215 ndr_err = ndr_pull_struct_blob(v, recs, &recs[ri],
216 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
217 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
218 DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
219 return DNS_ERR(SERVER_FAILURE);
222 *records = recs;
223 *rec_count = el->num_values;
224 return WERR_OK;
227 WERROR dns_replace_records(struct dns_server *dns,
228 TALLOC_CTX *mem_ctx,
229 struct ldb_dn *dn,
230 bool needs_add,
231 const struct dnsp_DnssrvRpcRecord *records,
232 uint16_t rec_count)
234 struct ldb_message_element *el;
235 uint16_t i;
236 int ret;
237 struct ldb_message *msg = NULL;
239 msg = ldb_msg_new(mem_ctx);
240 W_ERROR_HAVE_NO_MEMORY(msg);
242 msg->dn = dn;
244 ret = ldb_msg_add_empty(msg, "dnsRecord", LDB_FLAG_MOD_REPLACE, &el);
245 if (ret != LDB_SUCCESS) {
246 return DNS_ERR(SERVER_FAILURE);
249 el->values = talloc_zero_array(el, struct ldb_val, rec_count);
250 if (rec_count > 0) {
251 W_ERROR_HAVE_NO_MEMORY(el->values);
254 for (i = 0; i < rec_count; i++) {
255 static const struct dnsp_DnssrvRpcRecord zero;
256 struct ldb_val *v = &el->values[el->num_values];
257 enum ndr_err_code ndr_err;
259 if (memcmp(&records[i], &zero, sizeof(zero)) == 0) {
260 continue;
262 ndr_err = ndr_push_struct_blob(v, el->values, &records[i],
263 (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
264 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
265 DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
266 return DNS_ERR(SERVER_FAILURE);
268 el->num_values++;
272 if (el->num_values == 0) {
273 if (needs_add) {
274 return WERR_OK;
276 /* TODO: Delete object? */
279 if (needs_add) {
280 ret = ldb_msg_add_string(msg, "objectClass", "dnsNode");
281 if (ret != LDB_SUCCESS) {
282 return DNS_ERR(SERVER_FAILURE);
285 ret = ldb_add(dns->samdb, msg);
286 if (ret != LDB_SUCCESS) {
287 return DNS_ERR(SERVER_FAILURE);
290 return WERR_OK;
293 ret = ldb_modify(dns->samdb, msg);
294 if (ret != LDB_SUCCESS) {
295 return DNS_ERR(SERVER_FAILURE);
298 return WERR_OK;
301 bool dns_authorative_for_zone(struct dns_server *dns,
302 const char *name)
304 const struct dns_server_zone *z;
305 size_t host_part_len = 0;
307 if (name == NULL) {
308 return false;
311 if (strcmp(name, "") == 0) {
312 return true;
314 for (z = dns->zones; z != NULL; z = z->next) {
315 bool match;
317 match = dns_name_match(z->name, name, &host_part_len);
318 if (match) {
319 break;
322 if (z == NULL) {
323 return false;
326 return true;
329 WERROR dns_name2dn(struct dns_server *dns,
330 TALLOC_CTX *mem_ctx,
331 const char *name,
332 struct ldb_dn **_dn)
334 struct ldb_dn *base;
335 struct ldb_dn *dn;
336 const struct dns_server_zone *z;
337 size_t host_part_len = 0;
339 if (name == NULL) {
340 return DNS_ERR(FORMAT_ERROR);
343 /*TODO: Check if 'name' is a valid DNS name */
345 if (strcmp(name, "") == 0) {
346 base = ldb_get_default_basedn(dns->samdb);
347 dn = ldb_dn_copy(mem_ctx, base);
348 ldb_dn_add_child_fmt(dn, "DC=@,DC=RootDNSServers,CN=MicrosoftDNS,CN=System");
349 *_dn = dn;
350 return WERR_OK;
353 for (z = dns->zones; z != NULL; z = z->next) {
354 bool match;
356 match = dns_name_match(z->name, name, &host_part_len);
357 if (match) {
358 break;
362 if (z == NULL) {
363 return DNS_ERR(NAME_ERROR);
366 if (host_part_len == 0) {
367 dn = ldb_dn_copy(mem_ctx, z->dn);
368 ldb_dn_add_child_fmt(dn, "DC=@");
369 *_dn = dn;
370 return WERR_OK;
373 dn = ldb_dn_copy(mem_ctx, z->dn);
374 ldb_dn_add_child_fmt(dn, "DC=%*.*s", (int)host_part_len, (int)host_part_len, name);
375 *_dn = dn;
376 return WERR_OK;