Fix bad return values in unix_strlower/unix_strupper.
[Samba/vl.git] / source4 / dns_server / dns_utils.c
blob7d95bdd1518a1da409c4c7e4f73caee053135081
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;
58 DEBUG(5, ("No mapping exists for %s\n", win_errstr(werr)));
59 return DNS_RCODE_SERVFAIL;
62 bool dns_name_match(const char *zone, const char *name, size_t *host_part_len)
64 size_t zl = strlen(zone);
65 size_t nl = strlen(name);
66 ssize_t zi, ni;
67 static const size_t fixup = 'a' - 'A';
69 if (zl > nl) {
70 return false;
73 for (zi = zl, ni = nl; zi >= 0; zi--, ni--) {
74 char zc = zone[zi];
75 char nc = name[ni];
77 /* convert to lower case */
78 if (zc >= 'A' && zc <= 'Z') {
79 zc += fixup;
81 if (nc >= 'A' && nc <= 'Z') {
82 nc += fixup;
85 if (zc != nc) {
86 return false;
90 if (ni >= 0) {
91 if (name[ni] != '.') {
92 return false;
95 ni--;
98 *host_part_len = ni+1;
100 return true;
103 /* Names are equal if they match and there's nothing left over */
104 bool dns_name_equal(const char *name1, const char *name2)
106 size_t host_part_len;
107 bool ret = dns_name_match(name1, name2, &host_part_len);
109 return ret && (host_part_len == 0);
113 see if two dns records match
115 bool dns_records_match(struct dnsp_DnssrvRpcRecord *rec1,
116 struct dnsp_DnssrvRpcRecord *rec2)
118 bool status;
119 int i;
121 if (rec1->wType != rec2->wType) {
122 return false;
125 /* see if the data matches */
126 switch (rec1->wType) {
127 case DNS_TYPE_A:
128 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
129 case DNS_TYPE_AAAA:
130 return strcmp(rec1->data.ipv6, rec2->data.ipv6) == 0;
131 case DNS_TYPE_CNAME:
132 return dns_name_equal(rec1->data.cname, rec2->data.cname);
133 case DNS_TYPE_TXT:
134 if (rec1->data.txt.count != rec2->data.txt.count) {
135 return false;
137 status = true;
138 for (i=0; i<rec1->data.txt.count; i++) {
139 status = status && (strcmp(rec1->data.txt.str[i],
140 rec2->data.txt.str[i]) == 0);
142 return status;
143 case DNS_TYPE_PTR:
144 return strcmp(rec1->data.ptr, rec2->data.ptr) == 0;
145 case DNS_TYPE_NS:
146 return dns_name_equal(rec1->data.ns, rec2->data.ns);
148 case DNS_TYPE_SRV:
149 return rec1->data.srv.wPriority == rec2->data.srv.wPriority &&
150 rec1->data.srv.wWeight == rec2->data.srv.wWeight &&
151 rec1->data.srv.wPort == rec2->data.srv.wPort &&
152 dns_name_equal(rec1->data.srv.nameTarget, rec2->data.srv.nameTarget);
154 case DNS_TYPE_MX:
155 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
156 dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
158 case DNS_TYPE_HINFO:
159 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
160 strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
162 case DNS_TYPE_SOA:
163 return dns_name_equal(rec1->data.soa.mname, rec2->data.soa.mname) &&
164 dns_name_equal(rec1->data.soa.rname, rec2->data.soa.rname) &&
165 rec1->data.soa.serial == rec2->data.soa.serial &&
166 rec1->data.soa.refresh == rec2->data.soa.refresh &&
167 rec1->data.soa.retry == rec2->data.soa.retry &&
168 rec1->data.soa.expire == rec2->data.soa.expire &&
169 rec1->data.soa.minimum == rec2->data.soa.minimum;
170 default:
171 break;
174 return false;
177 WERROR dns_lookup_records(struct dns_server *dns,
178 TALLOC_CTX *mem_ctx,
179 struct ldb_dn *dn,
180 struct dnsp_DnssrvRpcRecord **records,
181 uint16_t *rec_count)
183 static const char * const attrs[] = { "dnsRecord", NULL};
184 struct ldb_message_element *el;
185 uint16_t ri;
186 int ret;
187 struct ldb_message *msg = NULL;
188 struct dnsp_DnssrvRpcRecord *recs;
190 ret = dsdb_search_one(dns->samdb, mem_ctx, &msg, dn,
191 LDB_SCOPE_BASE, attrs, 0, "%s", "(objectClass=dnsNode)");
192 if (ret != LDB_SUCCESS) {
193 /* TODO: we need to check if there's a glue record we need to
194 * create a referral to */
195 return DNS_ERR(NAME_ERROR);
198 el = ldb_msg_find_element(msg, attrs[0]);
199 if (el == NULL) {
200 *records = NULL;
201 *rec_count = 0;
202 return WERR_OK;
205 recs = talloc_zero_array(mem_ctx, struct dnsp_DnssrvRpcRecord, el->num_values);
206 W_ERROR_HAVE_NO_MEMORY(recs);
207 for (ri = 0; ri < el->num_values; ri++) {
208 struct ldb_val *v = &el->values[ri];
209 enum ndr_err_code ndr_err;
211 ndr_err = ndr_pull_struct_blob(v, recs, &recs[ri],
212 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
213 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
214 DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
215 return DNS_ERR(SERVER_FAILURE);
218 *records = recs;
219 *rec_count = el->num_values;
220 return WERR_OK;
223 WERROR dns_replace_records(struct dns_server *dns,
224 TALLOC_CTX *mem_ctx,
225 struct ldb_dn *dn,
226 bool needs_add,
227 const struct dnsp_DnssrvRpcRecord *records,
228 uint16_t rec_count)
230 struct ldb_message_element *el;
231 uint16_t i;
232 int ret;
233 struct ldb_message *msg = NULL;
235 msg = ldb_msg_new(mem_ctx);
236 W_ERROR_HAVE_NO_MEMORY(msg);
238 msg->dn = dn;
240 ret = ldb_msg_add_empty(msg, "dnsRecord", LDB_FLAG_MOD_REPLACE, &el);
241 if (ret != LDB_SUCCESS) {
242 return DNS_ERR(SERVER_FAILURE);
245 el->values = talloc_zero_array(el, struct ldb_val, rec_count);
246 if (rec_count > 0) {
247 W_ERROR_HAVE_NO_MEMORY(el->values);
250 for (i = 0; i < rec_count; i++) {
251 static const struct dnsp_DnssrvRpcRecord zero;
252 struct ldb_val *v = &el->values[el->num_values];
253 enum ndr_err_code ndr_err;
255 if (memcmp(&records[i], &zero, sizeof(zero)) == 0) {
256 continue;
258 ndr_err = ndr_push_struct_blob(v, el->values, &records[i],
259 (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
260 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
261 DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
262 return DNS_ERR(SERVER_FAILURE);
264 el->num_values++;
268 if (el->num_values == 0) {
269 if (needs_add) {
270 return WERR_OK;
272 /* TODO: Delete object? */
275 if (needs_add) {
276 ret = ldb_msg_add_string(msg, "objectClass", "dnsNode");
277 if (ret != LDB_SUCCESS) {
278 return DNS_ERR(SERVER_FAILURE);
281 ret = ldb_add(dns->samdb, msg);
282 if (ret != LDB_SUCCESS) {
283 return DNS_ERR(SERVER_FAILURE);
286 return WERR_OK;
289 ret = ldb_modify(dns->samdb, msg);
290 if (ret != LDB_SUCCESS) {
291 return DNS_ERR(SERVER_FAILURE);
294 return WERR_OK;
297 bool dns_authorative_for_zone(struct dns_server *dns,
298 const char *name)
300 const struct dns_server_zone *z;
301 size_t host_part_len = 0;
303 if (name == NULL) {
304 return false;
307 if (strcmp(name, "") == 0) {
308 return true;
310 for (z = dns->zones; z != NULL; z = z->next) {
311 bool match;
313 match = dns_name_match(z->name, name, &host_part_len);
314 if (match) {
315 break;
318 if (z == NULL) {
319 return false;
322 return true;
325 WERROR dns_name2dn(struct dns_server *dns,
326 TALLOC_CTX *mem_ctx,
327 const char *name,
328 struct ldb_dn **_dn)
330 struct ldb_dn *base;
331 struct ldb_dn *dn;
332 const struct dns_server_zone *z;
333 size_t host_part_len = 0;
335 if (name == NULL) {
336 return DNS_ERR(FORMAT_ERROR);
339 /*TODO: Check if 'name' is a valid DNS name */
341 if (strcmp(name, "") == 0) {
342 base = ldb_get_default_basedn(dns->samdb);
343 dn = ldb_dn_copy(mem_ctx, base);
344 ldb_dn_add_child_fmt(dn, "DC=@,DC=RootDNSServers,CN=MicrosoftDNS,CN=System");
345 *_dn = dn;
346 return WERR_OK;
349 for (z = dns->zones; z != NULL; z = z->next) {
350 bool match;
352 match = dns_name_match(z->name, name, &host_part_len);
353 if (match) {
354 break;
358 if (z == NULL) {
359 return DNS_ERR(NAME_ERROR);
362 if (host_part_len == 0) {
363 dn = ldb_dn_copy(mem_ctx, z->dn);
364 ldb_dn_add_child_fmt(dn, "DC=@");
365 *_dn = dn;
366 return WERR_OK;
369 dn = ldb_dn_copy(mem_ctx, z->dn);
370 ldb_dn_add_child_fmt(dn, "DC=%*.*s", (int)host_part_len, (int)host_part_len, name);
371 *_dn = dn;
372 return WERR_OK;