param: replace P_OCTAL variable setting with s3 version which uses sscanf
[Samba.git] / source4 / dns_server / dns_utils.c
blob72782cf450011bd211ad6e24955ff9eb9390dbb4
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 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_DNS
36 uint8_t werr_to_dns_err(WERROR werr)
38 if (W_ERROR_EQUAL(WERR_OK, werr)) {
39 return DNS_RCODE_OK;
40 } else if (W_ERROR_EQUAL(DNS_ERR(FORMAT_ERROR), werr)) {
41 return DNS_RCODE_FORMERR;
42 } else if (W_ERROR_EQUAL(DNS_ERR(SERVER_FAILURE), werr)) {
43 return DNS_RCODE_SERVFAIL;
44 } else if (W_ERROR_EQUAL(DNS_ERR(NAME_ERROR), werr)) {
45 return DNS_RCODE_NXDOMAIN;
46 } else if (W_ERROR_EQUAL(DNS_ERR(NOT_IMPLEMENTED), werr)) {
47 return DNS_RCODE_NOTIMP;
48 } else if (W_ERROR_EQUAL(DNS_ERR(REFUSED), werr)) {
49 return DNS_RCODE_REFUSED;
50 } else if (W_ERROR_EQUAL(DNS_ERR(YXDOMAIN), werr)) {
51 return DNS_RCODE_YXDOMAIN;
52 } else if (W_ERROR_EQUAL(DNS_ERR(YXRRSET), werr)) {
53 return DNS_RCODE_YXRRSET;
54 } else if (W_ERROR_EQUAL(DNS_ERR(NXRRSET), werr)) {
55 return DNS_RCODE_NXRRSET;
56 } else if (W_ERROR_EQUAL(DNS_ERR(NOTAUTH), werr)) {
57 return DNS_RCODE_NOTAUTH;
58 } else if (W_ERROR_EQUAL(DNS_ERR(NOTZONE), werr)) {
59 return DNS_RCODE_NOTZONE;
60 } else if (W_ERROR_EQUAL(DNS_ERR(BADKEY), werr)) {
61 return DNS_RCODE_BADKEY;
63 DEBUG(5, ("No mapping exists for %s\n", win_errstr(werr)));
64 return DNS_RCODE_SERVFAIL;
67 bool dns_name_match(const char *zone, const char *name, size_t *host_part_len)
69 size_t zl = strlen(zone);
70 size_t nl = strlen(name);
71 ssize_t zi, ni;
72 static const size_t fixup = 'a' - 'A';
74 if (zl > nl) {
75 return false;
78 for (zi = zl, ni = nl; zi >= 0; zi--, ni--) {
79 char zc = zone[zi];
80 char nc = name[ni];
82 /* convert to lower case */
83 if (zc >= 'A' && zc <= 'Z') {
84 zc += fixup;
86 if (nc >= 'A' && nc <= 'Z') {
87 nc += fixup;
90 if (zc != nc) {
91 return false;
95 if (ni >= 0) {
96 if (name[ni] != '.') {
97 return false;
100 ni--;
103 *host_part_len = ni+1;
105 return true;
108 /* Names are equal if they match and there's nothing left over */
109 bool dns_name_equal(const char *name1, const char *name2)
111 size_t host_part_len;
112 bool ret = dns_name_match(name1, name2, &host_part_len);
114 return ret && (host_part_len == 0);
118 see if two dns records match
120 bool dns_records_match(struct dnsp_DnssrvRpcRecord *rec1,
121 struct dnsp_DnssrvRpcRecord *rec2)
123 bool status;
124 int i;
126 if (rec1->wType != rec2->wType) {
127 return false;
130 /* see if the data matches */
131 switch (rec1->wType) {
132 case DNS_TYPE_A:
133 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
134 case DNS_TYPE_AAAA:
135 return strcmp(rec1->data.ipv6, rec2->data.ipv6) == 0;
136 case DNS_TYPE_CNAME:
137 return dns_name_equal(rec1->data.cname, rec2->data.cname);
138 case DNS_TYPE_TXT:
139 if (rec1->data.txt.count != rec2->data.txt.count) {
140 return false;
142 status = true;
143 for (i=0; i<rec1->data.txt.count; i++) {
144 status = status && (strcmp(rec1->data.txt.str[i],
145 rec2->data.txt.str[i]) == 0);
147 return status;
148 case DNS_TYPE_PTR:
149 return strcmp(rec1->data.ptr, rec2->data.ptr) == 0;
150 case DNS_TYPE_NS:
151 return dns_name_equal(rec1->data.ns, rec2->data.ns);
153 case DNS_TYPE_SRV:
154 return rec1->data.srv.wPriority == rec2->data.srv.wPriority &&
155 rec1->data.srv.wWeight == rec2->data.srv.wWeight &&
156 rec1->data.srv.wPort == rec2->data.srv.wPort &&
157 dns_name_equal(rec1->data.srv.nameTarget, rec2->data.srv.nameTarget);
159 case DNS_TYPE_MX:
160 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
161 dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
163 case DNS_TYPE_HINFO:
164 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
165 strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
167 case DNS_TYPE_SOA:
168 return dns_name_equal(rec1->data.soa.mname, rec2->data.soa.mname) &&
169 dns_name_equal(rec1->data.soa.rname, rec2->data.soa.rname) &&
170 rec1->data.soa.serial == rec2->data.soa.serial &&
171 rec1->data.soa.refresh == rec2->data.soa.refresh &&
172 rec1->data.soa.retry == rec2->data.soa.retry &&
173 rec1->data.soa.expire == rec2->data.soa.expire &&
174 rec1->data.soa.minimum == rec2->data.soa.minimum;
175 default:
176 break;
179 return false;
182 WERROR dns_lookup_records(struct dns_server *dns,
183 TALLOC_CTX *mem_ctx,
184 struct ldb_dn *dn,
185 struct dnsp_DnssrvRpcRecord **records,
186 uint16_t *rec_count)
188 static const char * const attrs[] = { "dnsRecord", NULL};
189 struct ldb_message_element *el;
190 uint16_t ri;
191 int ret;
192 struct ldb_message *msg = NULL;
193 struct dnsp_DnssrvRpcRecord *recs;
195 ret = dsdb_search_one(dns->samdb, mem_ctx, &msg, dn,
196 LDB_SCOPE_BASE, attrs, 0, "%s", "(objectClass=dnsNode)");
197 if (ret != LDB_SUCCESS) {
198 /* TODO: we need to check if there's a glue record we need to
199 * create a referral to */
200 return DNS_ERR(NAME_ERROR);
203 el = ldb_msg_find_element(msg, attrs[0]);
204 if (el == NULL) {
205 *records = NULL;
206 *rec_count = 0;
207 return DNS_ERR(NAME_ERROR);
210 recs = talloc_zero_array(mem_ctx, struct dnsp_DnssrvRpcRecord, el->num_values);
211 if (recs == NULL) {
212 return WERR_NOMEM;
214 for (ri = 0; ri < el->num_values; ri++) {
215 struct ldb_val *v = &el->values[ri];
216 enum ndr_err_code ndr_err;
218 ndr_err = ndr_pull_struct_blob(v, recs, &recs[ri],
219 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
220 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
221 DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
222 return DNS_ERR(SERVER_FAILURE);
225 *records = recs;
226 *rec_count = el->num_values;
227 return WERR_OK;
230 WERROR dns_replace_records(struct dns_server *dns,
231 TALLOC_CTX *mem_ctx,
232 struct ldb_dn *dn,
233 bool needs_add,
234 const struct dnsp_DnssrvRpcRecord *records,
235 uint16_t rec_count)
237 struct ldb_message_element *el;
238 uint16_t i;
239 int ret;
240 struct ldb_message *msg = NULL;
242 msg = ldb_msg_new(mem_ctx);
243 W_ERROR_HAVE_NO_MEMORY(msg);
245 msg->dn = dn;
247 ret = ldb_msg_add_empty(msg, "dnsRecord", LDB_FLAG_MOD_REPLACE, &el);
248 if (ret != LDB_SUCCESS) {
249 return DNS_ERR(SERVER_FAILURE);
252 el->values = talloc_zero_array(el, struct ldb_val, rec_count);
253 if (rec_count > 0) {
254 W_ERROR_HAVE_NO_MEMORY(el->values);
257 for (i = 0; i < rec_count; i++) {
258 static const struct dnsp_DnssrvRpcRecord zero;
259 struct ldb_val *v = &el->values[el->num_values];
260 enum ndr_err_code ndr_err;
262 if (memcmp(&records[i], &zero, sizeof(zero)) == 0) {
263 continue;
265 ndr_err = ndr_push_struct_blob(v, el->values, &records[i],
266 (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
267 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
268 DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
269 return DNS_ERR(SERVER_FAILURE);
271 el->num_values++;
275 if (el->num_values == 0) {
276 if (needs_add) {
277 return WERR_OK;
279 /* No entries left, delete the dnsNode object */
280 ret = ldb_delete(dns->samdb, msg->dn);
281 if (ret != LDB_SUCCESS) {
282 DEBUG(0, ("Deleting record failed; %d\n", ret));
283 return DNS_ERR(SERVER_FAILURE);
285 return WERR_OK;
288 if (needs_add) {
289 ret = ldb_msg_add_string(msg, "objectClass", "dnsNode");
290 if (ret != LDB_SUCCESS) {
291 return DNS_ERR(SERVER_FAILURE);
294 ret = ldb_add(dns->samdb, msg);
295 if (ret != LDB_SUCCESS) {
296 return DNS_ERR(SERVER_FAILURE);
299 return WERR_OK;
302 ret = ldb_modify(dns->samdb, msg);
303 if (ret != LDB_SUCCESS) {
304 return DNS_ERR(SERVER_FAILURE);
307 return WERR_OK;
310 bool dns_authorative_for_zone(struct dns_server *dns,
311 const char *name)
313 const struct dns_server_zone *z;
314 size_t host_part_len = 0;
316 if (name == NULL) {
317 return false;
320 if (strcmp(name, "") == 0) {
321 return true;
323 for (z = dns->zones; z != NULL; z = z->next) {
324 bool match;
326 match = dns_name_match(z->name, name, &host_part_len);
327 if (match) {
328 break;
331 if (z == NULL) {
332 return false;
335 return true;
338 WERROR dns_name2dn(struct dns_server *dns,
339 TALLOC_CTX *mem_ctx,
340 const char *name,
341 struct ldb_dn **_dn)
343 struct ldb_dn *base;
344 struct ldb_dn *dn;
345 const struct dns_server_zone *z;
346 size_t host_part_len = 0;
348 if (name == NULL) {
349 return DNS_ERR(FORMAT_ERROR);
352 /*TODO: Check if 'name' is a valid DNS name */
354 if (strcmp(name, "") == 0) {
355 base = ldb_get_default_basedn(dns->samdb);
356 dn = ldb_dn_copy(mem_ctx, base);
357 ldb_dn_add_child_fmt(dn, "DC=@,DC=RootDNSServers,CN=MicrosoftDNS,CN=System");
358 *_dn = dn;
359 return WERR_OK;
362 for (z = dns->zones; z != NULL; z = z->next) {
363 bool match;
365 match = dns_name_match(z->name, name, &host_part_len);
366 if (match) {
367 break;
371 if (z == NULL) {
372 return DNS_ERR(NAME_ERROR);
375 if (host_part_len == 0) {
376 dn = ldb_dn_copy(mem_ctx, z->dn);
377 ldb_dn_add_child_fmt(dn, "DC=@");
378 *_dn = dn;
379 return WERR_OK;
382 dn = ldb_dn_copy(mem_ctx, z->dn);
383 ldb_dn_add_child_fmt(dn, "DC=%*.*s", (int)host_part_len, (int)host_part_len, name);
384 *_dn = dn;
385 return WERR_OK;
388 WERROR dns_generate_options(struct dns_server *dns,
389 TALLOC_CTX *mem_ctx,
390 struct dns_res_rec **options)
392 struct dns_res_rec *o;
394 o = talloc_zero(mem_ctx, struct dns_res_rec);
395 if (o == NULL) {
396 return WERR_NOMEM;
398 o->name = '\0';
399 o->rr_type = DNS_QTYPE_OPT;
400 /* This is ugly, but RFC2671 wants the payload size in this field */
401 o->rr_class = (enum dns_qclass) dns->max_payload;
402 o->ttl = 0;
403 o->length = 0;
405 *options = o;
406 return WERR_OK;