s3-includes: no point in including all security headers globally.
[Samba/vl.git] / source4 / dns_server / dlz_bind9.c
blob7dd06c1b2c886141d2215425538c8beb3c3adb18
1 /*
2 Unix SMB/CIFS implementation.
4 bind9 dlz driver for Samba
6 Copyright (C) 2010 Andrew Tridgell
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 "talloc.h"
24 #include "param/param.h"
25 #include "lib/events/events.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "dsdb/common/util.h"
28 #include "auth/session.h"
29 #include "auth/gensec/gensec.h"
30 #include "gen_ndr/ndr_dnsp.h"
31 #include "lib/cmdline/popt_common.h"
32 #include "lib/cmdline/popt_credentials.h"
33 #include "ldb_module.h"
34 #include "dlz_minimal.h"
36 struct dlz_bind9_data {
37 struct ldb_context *samdb;
38 struct tevent_context *ev_ctx;
39 struct loadparm_context *lp;
40 int *transaction_token;
41 uint32_t soa_serial;
43 /* helper functions from the dlz_dlopen driver */
44 void (*log)(int level, const char *fmt, ...);
45 isc_result_t (*putrr)(dns_sdlzlookup_t *handle, const char *type,
46 dns_ttl_t ttl, const char *data);
47 isc_result_t (*putnamedrr)(dns_sdlzlookup_t *handle, const char *name,
48 const char *type, dns_ttl_t ttl, const char *data);
49 isc_result_t (*writeable_zone)(dns_view_t *view, const char *zone_name);
53 static const char *zone_prefixes[] = {
54 "CN=MicrosoftDNS,DC=DomainDnsZones",
55 "CN=MicrosoftDNS,DC=ForestDnsZones",
56 NULL
60 return the version of the API
62 _PUBLIC_ int dlz_version(unsigned int *flags)
64 return DLZ_DLOPEN_VERSION;
68 remember a helper function from the bind9 dlz_dlopen driver
70 static void b9_add_helper(struct dlz_bind9_data *state, const char *helper_name, void *ptr)
72 if (strcmp(helper_name, "log") == 0) {
73 state->log = ptr;
75 if (strcmp(helper_name, "putrr") == 0) {
76 state->putrr = ptr;
78 if (strcmp(helper_name, "putnamedrr") == 0) {
79 state->putnamedrr = ptr;
81 if (strcmp(helper_name, "writeable_zone") == 0) {
82 state->writeable_zone = ptr;
87 format a record for bind9
89 static bool b9_format(struct dlz_bind9_data *state,
90 TALLOC_CTX *mem_ctx,
91 struct dnsp_DnssrvRpcRecord *rec,
92 const char **type, const char **data)
94 switch (rec->wType) {
95 case DNS_TYPE_A:
96 *type = "a";
97 *data = rec->data.ipv4;
98 break;
100 case DNS_TYPE_AAAA:
101 *type = "aaaa";
102 *data = rec->data.ipv6;
103 break;
105 case DNS_TYPE_CNAME:
106 *type = "cname";
107 *data = rec->data.cname;
108 break;
110 case DNS_TYPE_TXT:
111 *type = "txt";
112 *data = rec->data.txt;
113 break;
115 case DNS_TYPE_PTR:
116 *type = "ptr";
117 *data = rec->data.ptr;
118 break;
120 case DNS_TYPE_SRV:
121 *type = "srv";
122 *data = talloc_asprintf(mem_ctx, "%u %u %u %s",
123 rec->data.srv.wPriority,
124 rec->data.srv.wWeight,
125 rec->data.srv.wPort,
126 rec->data.srv.nameTarget);
127 break;
129 case DNS_TYPE_MX:
130 *type = "mx";
131 *data = talloc_asprintf(mem_ctx, "%u %s",
132 rec->data.mx.wPriority,
133 rec->data.mx.nameTarget);
134 break;
136 case DNS_TYPE_HINFO:
137 *type = "hinfo";
138 *data = talloc_asprintf(mem_ctx, "%s %s",
139 rec->data.hinfo.cpu,
140 rec->data.hinfo.os);
141 break;
143 case DNS_TYPE_NS:
144 *type = "ns";
145 *data = rec->data.ns;
146 break;
148 case DNS_TYPE_SOA: {
149 const char *mname;
150 *type = "soa";
152 /* we need to fake the authoritative nameserver to
153 * point at ourselves. This is how AD DNS servers
154 * force clients to send updates to the right local DC
156 mname = talloc_asprintf(mem_ctx, "%s.%s",
157 lpcfg_netbios_name(state->lp), lpcfg_dnsdomain(state->lp));
158 if (mname == NULL) {
159 return false;
161 mname = strlower_talloc(mem_ctx, mname);
162 if (mname == NULL) {
163 return false;
166 state->soa_serial = rec->data.soa.serial;
168 *data = talloc_asprintf(mem_ctx, "%s %s %u %u %u %u %u",
169 mname,
170 rec->data.soa.rname,
171 rec->data.soa.serial,
172 rec->data.soa.refresh,
173 rec->data.soa.retry,
174 rec->data.soa.expire,
175 rec->data.soa.minimum);
176 break;
179 default:
180 state->log(ISC_LOG_ERROR, "samba b9_putrr: unhandled record type %u",
181 rec->wType);
182 return false;
185 return true;
188 static const struct {
189 enum dns_record_type dns_type;
190 const char *typestr;
191 bool single_valued;
192 } dns_typemap[] = {
193 { DNS_TYPE_A, "A" , false},
194 { DNS_TYPE_AAAA, "AAAA" , false},
195 { DNS_TYPE_CNAME, "CNAME" , true},
196 { DNS_TYPE_TXT, "TXT" , false},
197 { DNS_TYPE_PTR, "PTR" , false},
198 { DNS_TYPE_SRV, "SRV" , false},
199 { DNS_TYPE_MX, "MX" , false},
200 { DNS_TYPE_HINFO, "HINFO" , false},
201 { DNS_TYPE_NS, "NS" , false},
202 { DNS_TYPE_SOA, "SOA" , true},
207 see if a DNS type is single valued
209 static bool b9_single_valued(enum dns_record_type dns_type)
211 int i;
212 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
213 if (dns_typemap[i].dns_type == dns_type) {
214 return dns_typemap[i].single_valued;
217 return false;
221 see if a DNS type is single valued
223 static bool b9_dns_type(const char *type, enum dns_record_type *dtype)
225 int i;
226 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
227 if (strcasecmp(dns_typemap[i].typestr, type) == 0) {
228 *dtype = dns_typemap[i].dns_type;
229 return true;
232 return false;
236 #define DNS_PARSE_STR(ret, str, sep, saveptr) do { \
237 (ret) = strtok_r(str, sep, &saveptr); \
238 if ((ret) == NULL) return false; \
239 } while (0)
241 #define DNS_PARSE_UINT(ret, str, sep, saveptr) do { \
242 char *istr = strtok_r(str, sep, &saveptr); \
243 if ((istr) == NULL) return false; \
244 (ret) = strtoul(istr, NULL, 10); \
245 } while (0)
248 parse a record from bind9
250 static bool b9_parse(struct dlz_bind9_data *state,
251 const char *rdatastr,
252 struct dnsp_DnssrvRpcRecord *rec)
254 char *full_name, *dclass, *type;
255 char *str, *saveptr=NULL;
256 int i;
258 str = talloc_strdup(rec, rdatastr);
259 if (str == NULL) {
260 return false;
263 /* parse the SDLZ string form */
264 DNS_PARSE_STR(full_name, str, "\t", saveptr);
265 DNS_PARSE_UINT(rec->dwTtlSeconds, NULL, "\t", saveptr);
266 DNS_PARSE_STR(dclass, NULL, "\t", saveptr);
267 DNS_PARSE_STR(type, NULL, "\t", saveptr);
269 /* construct the record */
270 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
271 if (strcasecmp(type, dns_typemap[i].typestr) == 0) {
272 rec->wType = dns_typemap[i].dns_type;
273 break;
276 if (i == ARRAY_SIZE(dns_typemap)) {
277 state->log(ISC_LOG_ERROR, "samba_dlz: unsupported record type '%s' for '%s'",
278 type, full_name);
279 return false;
282 switch (rec->wType) {
283 case DNS_TYPE_A:
284 DNS_PARSE_STR(rec->data.ipv4, NULL, " ", saveptr);
285 break;
287 case DNS_TYPE_AAAA:
288 DNS_PARSE_STR(rec->data.ipv6, NULL, " ", saveptr);
289 break;
291 case DNS_TYPE_CNAME:
292 DNS_PARSE_STR(rec->data.cname, NULL, " ", saveptr);
293 break;
295 case DNS_TYPE_TXT:
296 DNS_PARSE_STR(rec->data.txt, NULL, "\t", saveptr);
297 break;
299 case DNS_TYPE_PTR:
300 DNS_PARSE_STR(rec->data.ptr, NULL, " ", saveptr);
301 break;
303 case DNS_TYPE_SRV:
304 DNS_PARSE_UINT(rec->data.srv.wPriority, NULL, " ", saveptr);
305 DNS_PARSE_UINT(rec->data.srv.wWeight, NULL, " ", saveptr);
306 DNS_PARSE_UINT(rec->data.srv.wPort, NULL, " ", saveptr);
307 DNS_PARSE_STR(rec->data.srv.nameTarget, NULL, " ", saveptr);
308 break;
310 case DNS_TYPE_MX:
311 DNS_PARSE_UINT(rec->data.mx.wPriority, NULL, " ", saveptr);
312 DNS_PARSE_STR(rec->data.mx.nameTarget, NULL, " ", saveptr);
313 break;
315 case DNS_TYPE_HINFO:
316 DNS_PARSE_STR(rec->data.hinfo.cpu, NULL, " ", saveptr);
317 DNS_PARSE_STR(rec->data.hinfo.os, NULL, " ", saveptr);
318 break;
320 case DNS_TYPE_NS:
321 DNS_PARSE_STR(rec->data.ns, NULL, " ", saveptr);
322 break;
324 case DNS_TYPE_SOA:
325 DNS_PARSE_STR(rec->data.soa.mname, NULL, " ", saveptr);
326 DNS_PARSE_STR(rec->data.soa.rname, NULL, " ", saveptr);
327 DNS_PARSE_UINT(rec->data.soa.serial, NULL, " ", saveptr);
328 DNS_PARSE_UINT(rec->data.soa.refresh, NULL, " ", saveptr);
329 DNS_PARSE_UINT(rec->data.soa.retry, NULL, " ", saveptr);
330 DNS_PARSE_UINT(rec->data.soa.expire, NULL, " ", saveptr);
331 DNS_PARSE_UINT(rec->data.soa.minimum, NULL, " ", saveptr);
332 break;
334 default:
335 state->log(ISC_LOG_ERROR, "samba b9_parse: unhandled record type %u",
336 rec->wType);
337 return false;
340 /* we should be at the end of the buffer now */
341 if (strtok_r(NULL, "\t ", &saveptr) != NULL) {
342 state->log(ISC_LOG_ERROR, "samba b9_parse: expected data at end of string for '%s'");
343 return false;
346 return true;
350 send a resource recond to bind9
352 static isc_result_t b9_putrr(struct dlz_bind9_data *state,
353 void *handle, struct dnsp_DnssrvRpcRecord *rec,
354 const char **types)
356 isc_result_t result;
357 const char *type, *data;
358 TALLOC_CTX *tmp_ctx = talloc_new(state);
360 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
361 return ISC_R_FAILURE;
364 if (data == NULL) {
365 talloc_free(tmp_ctx);
366 return ISC_R_NOMEMORY;
369 if (types) {
370 int i;
371 for (i=0; types[i]; i++) {
372 if (strcmp(types[i], type) == 0) break;
374 if (types[i] == NULL) {
375 /* skip it */
376 return ISC_R_SUCCESS;
380 result = state->putrr(handle, type, rec->dwTtlSeconds, data);
381 if (result != ISC_R_SUCCESS) {
382 state->log(ISC_LOG_ERROR, "Failed to put rr");
384 talloc_free(tmp_ctx);
385 return result;
390 send a named resource recond to bind9
392 static isc_result_t b9_putnamedrr(struct dlz_bind9_data *state,
393 void *handle, const char *name,
394 struct dnsp_DnssrvRpcRecord *rec)
396 isc_result_t result;
397 const char *type, *data;
398 TALLOC_CTX *tmp_ctx = talloc_new(state);
400 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
401 return ISC_R_FAILURE;
404 if (data == NULL) {
405 talloc_free(tmp_ctx);
406 return ISC_R_NOMEMORY;
409 result = state->putnamedrr(handle, name, type, rec->dwTtlSeconds, data);
410 if (result != ISC_R_SUCCESS) {
411 state->log(ISC_LOG_ERROR, "Failed to put named rr '%s'", name);
413 talloc_free(tmp_ctx);
414 return result;
417 struct b9_options {
418 const char *url;
422 parse options
424 static isc_result_t parse_options(struct dlz_bind9_data *state,
425 unsigned int argc, char *argv[],
426 struct b9_options *options)
428 int opt;
429 poptContext pc;
430 struct poptOption long_options[] = {
431 { "url", 'H', POPT_ARG_STRING, &options->url, 0, "database URL", "URL" },
432 { NULL }
434 struct poptOption **popt_options;
435 int ret;
437 fault_setup_disable();
439 popt_options = ldb_module_popt_options(state->samdb);
440 (*popt_options) = long_options;
442 ret = ldb_modules_hook(state->samdb, LDB_MODULE_HOOK_CMDLINE_OPTIONS);
443 if (ret != LDB_SUCCESS) {
444 state->log(ISC_LOG_ERROR, "dlz samba: failed cmdline hook");
445 return ISC_R_FAILURE;
448 pc = poptGetContext("dlz_bind9", argc, (const char **)argv, *popt_options,
449 POPT_CONTEXT_KEEP_FIRST);
451 while ((opt = poptGetNextOpt(pc)) != -1) {
452 switch (opt) {
453 default:
454 state->log(ISC_LOG_ERROR, "dlz samba: Invalid option %s: %s",
455 poptBadOption(pc, 0), poptStrerror(opt));
456 return ISC_R_FAILURE;
460 ret = ldb_modules_hook(state->samdb, LDB_MODULE_HOOK_CMDLINE_PRECONNECT);
461 if (ret != LDB_SUCCESS) {
462 state->log(ISC_LOG_ERROR, "dlz samba: failed cmdline preconnect");
463 return ISC_R_FAILURE;
466 return ISC_R_SUCCESS;
471 called to initialise the driver
473 _PUBLIC_ isc_result_t dlz_create(const char *dlzname,
474 unsigned int argc, char *argv[],
475 void **dbdata, ...)
477 struct dlz_bind9_data *state;
478 const char *helper_name;
479 va_list ap;
480 isc_result_t result;
481 TALLOC_CTX *tmp_ctx;
482 int ret;
483 struct ldb_dn *dn;
484 struct b9_options options;
486 ZERO_STRUCT(options);
488 state = talloc_zero(NULL, struct dlz_bind9_data);
489 if (state == NULL) {
490 return ISC_R_NOMEMORY;
493 tmp_ctx = talloc_new(state);
495 /* fill in the helper functions */
496 va_start(ap, dbdata);
497 while ((helper_name = va_arg(ap, const char *)) != NULL) {
498 b9_add_helper(state, helper_name, va_arg(ap, void*));
500 va_end(ap);
502 state->ev_ctx = s4_event_context_init(state);
503 if (state->ev_ctx == NULL) {
504 result = ISC_R_NOMEMORY;
505 goto failed;
508 state->samdb = ldb_init(state, state->ev_ctx);
509 if (state->samdb == NULL) {
510 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to create ldb");
511 result = ISC_R_FAILURE;
512 goto failed;
515 result = parse_options(state, argc, argv, &options);
516 if (result != ISC_R_SUCCESS) {
517 goto failed;
520 state->lp = loadparm_init_global(true);
521 if (state->lp == NULL) {
522 result = ISC_R_NOMEMORY;
523 goto failed;
526 if (options.url == NULL) {
527 options.url = talloc_asprintf(tmp_ctx, "ldapi://%s",
528 private_path(tmp_ctx, state->lp, "ldap_priv/ldapi"));
529 if (options.url == NULL) {
530 result = ISC_R_NOMEMORY;
531 goto failed;
535 ret = ldb_connect(state->samdb, options.url, 0, NULL);
536 if (ret == -1) {
537 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to connect to %s - %s",
538 options.url, ldb_errstring(state->samdb));
539 result = ISC_R_FAILURE;
540 goto failed;
543 ret = ldb_modules_hook(state->samdb, LDB_MODULE_HOOK_CMDLINE_POSTCONNECT);
544 if (ret != LDB_SUCCESS) {
545 state->log(ISC_LOG_ERROR, "samba_dlz: Failed postconnect for %s - %s",
546 options.url, ldb_errstring(state->samdb));
547 result = ISC_R_FAILURE;
548 goto failed;
551 dn = ldb_get_default_basedn(state->samdb);
552 if (dn == NULL) {
553 state->log(ISC_LOG_ERROR, "samba_dlz: Unable to get basedn for %s - %s",
554 options.url, ldb_errstring(state->samdb));
555 result = ISC_R_FAILURE;
556 goto failed;
559 state->log(ISC_LOG_INFO, "samba_dlz: started for DN %s",
560 ldb_dn_get_linearized(dn));
562 *dbdata = state;
564 talloc_free(tmp_ctx);
565 return ISC_R_SUCCESS;
567 failed:
568 talloc_free(state);
569 return result;
573 shutdown the backend
575 _PUBLIC_ void dlz_destroy(void *dbdata)
577 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
578 state->log(ISC_LOG_INFO, "samba_dlz: shutting down");
579 talloc_free(state);
584 return the base DN for a zone
586 static isc_result_t b9_find_zone_dn(struct dlz_bind9_data *state, const char *zone_name,
587 TALLOC_CTX *mem_ctx, struct ldb_dn **zone_dn)
589 int ret;
590 TALLOC_CTX *tmp_ctx = talloc_new(state);
591 const char *attrs[] = { NULL };
592 int i;
594 for (i=0; zone_prefixes[i]; i++) {
595 struct ldb_dn *dn;
596 struct ldb_result *res;
598 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
599 if (dn == NULL) {
600 talloc_free(tmp_ctx);
601 return ISC_R_NOMEMORY;
604 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone_name, zone_prefixes[i])) {
605 talloc_free(tmp_ctx);
606 return ISC_R_NOMEMORY;
609 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsZone");
610 if (ret == LDB_SUCCESS) {
611 if (zone_dn != NULL) {
612 *zone_dn = talloc_steal(mem_ctx, dn);
614 talloc_free(tmp_ctx);
615 return ISC_R_SUCCESS;
617 talloc_free(dn);
620 talloc_free(tmp_ctx);
621 return ISC_R_NOTFOUND;
626 return the DN for a name. The record does not need to exist, but the
627 zone must exist
629 static isc_result_t b9_find_name_dn(struct dlz_bind9_data *state, const char *name,
630 TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
632 const char *p;
634 /* work through the name piece by piece, until we find a zone */
635 for (p=name; p; ) {
636 isc_result_t result;
637 result = b9_find_zone_dn(state, p, mem_ctx, dn);
638 if (result == ISC_R_SUCCESS) {
639 /* we found a zone, now extend the DN to get
640 * the full DN
642 bool ret;
643 if (p == name) {
644 ret = ldb_dn_add_child_fmt(*dn, "DC=@");
645 } else {
646 ret = ldb_dn_add_child_fmt(*dn, "DC=%.*s", (int)(p-name)-1, name);
648 if (!ret) {
649 talloc_free(*dn);
650 return ISC_R_NOMEMORY;
652 return ISC_R_SUCCESS;
654 p = strchr(p, '.');
655 if (p == NULL) {
656 break;
658 p++;
660 return ISC_R_NOTFOUND;
665 see if we handle a given zone
667 _PUBLIC_ isc_result_t dlz_findzonedb(void *dbdata, const char *name)
669 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
670 return b9_find_zone_dn(state, name, NULL, NULL);
675 lookup one record
677 static isc_result_t dlz_lookup_types(struct dlz_bind9_data *state,
678 const char *zone, const char *name,
679 dns_sdlzlookup_t *lookup,
680 const char **types)
682 TALLOC_CTX *tmp_ctx = talloc_new(state);
683 const char *attrs[] = { "dnsRecord", NULL };
684 int ret = LDB_SUCCESS, i;
685 struct ldb_result *res;
686 struct ldb_message_element *el;
687 struct ldb_dn *dn;
689 for (i=0; zone_prefixes[i]; i++) {
690 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
691 if (dn == NULL) {
692 talloc_free(tmp_ctx);
693 return ISC_R_NOMEMORY;
696 if (!ldb_dn_add_child_fmt(dn, "DC=%s,DC=%s,%s", name, zone, zone_prefixes[i])) {
697 talloc_free(tmp_ctx);
698 return ISC_R_NOMEMORY;
701 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
702 attrs, "objectClass=dnsNode");
703 if (ret == LDB_SUCCESS) {
704 break;
707 if (ret != LDB_SUCCESS) {
708 talloc_free(tmp_ctx);
709 return ISC_R_NOTFOUND;
712 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
713 if (el == NULL || el->num_values == 0) {
714 talloc_free(tmp_ctx);
715 return ISC_R_NOTFOUND;
718 for (i=0; i<el->num_values; i++) {
719 struct dnsp_DnssrvRpcRecord rec;
720 enum ndr_err_code ndr_err;
721 isc_result_t result;
723 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec,
724 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
725 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
726 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
727 ldb_dn_get_linearized(dn));
728 talloc_free(tmp_ctx);
729 return ISC_R_FAILURE;
732 result = b9_putrr(state, lookup, &rec, types);
733 if (result != ISC_R_SUCCESS) {
734 talloc_free(tmp_ctx);
735 return result;
739 talloc_free(tmp_ctx);
740 return ISC_R_SUCCESS;
744 lookup one record
746 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name,
747 void *dbdata, dns_sdlzlookup_t *lookup)
749 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
750 return dlz_lookup_types(state, zone, name, lookup, NULL);
755 see if a zone transfer is allowed
757 _PUBLIC_ isc_result_t dlz_allowzonexfr(void *dbdata, const char *name, const char *client)
759 /* just say yes for all our zones for now */
760 return dlz_findzonedb(dbdata, name);
764 perform a zone transfer
766 _PUBLIC_ isc_result_t dlz_allnodes(const char *zone, void *dbdata,
767 dns_sdlzallnodes_t *allnodes)
769 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
770 const char *attrs[] = { "dnsRecord", NULL };
771 int ret = LDB_SUCCESS, i, j;
772 struct ldb_dn *dn;
773 struct ldb_result *res;
774 TALLOC_CTX *tmp_ctx = talloc_new(state);
776 for (i=0; zone_prefixes[i]; i++) {
777 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
778 if (dn == NULL) {
779 talloc_free(tmp_ctx);
780 return ISC_R_NOMEMORY;
783 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone, zone_prefixes[i])) {
784 talloc_free(tmp_ctx);
785 return ISC_R_NOMEMORY;
788 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
789 attrs, "objectClass=dnsNode");
790 if (ret == LDB_SUCCESS) {
791 break;
794 if (ret != LDB_SUCCESS) {
795 talloc_free(tmp_ctx);
796 return ISC_R_NOTFOUND;
799 for (i=0; i<res->count; i++) {
800 struct ldb_message_element *el;
801 TALLOC_CTX *el_ctx = talloc_new(tmp_ctx);
802 const char *rdn, *name;
803 const struct ldb_val *v;
805 el = ldb_msg_find_element(res->msgs[i], "dnsRecord");
806 if (el == NULL || el->num_values == 0) {
807 state->log(ISC_LOG_INFO, "failed to find dnsRecord for %s",
808 ldb_dn_get_linearized(dn));
809 talloc_free(el_ctx);
810 continue;
813 v = ldb_dn_get_rdn_val(res->msgs[i]->dn);
814 if (v == NULL) {
815 state->log(ISC_LOG_INFO, "failed to find RDN for %s",
816 ldb_dn_get_linearized(dn));
817 talloc_free(el_ctx);
818 continue;
821 rdn = talloc_strndup(el_ctx, (char *)v->data, v->length);
822 if (rdn == NULL) {
823 talloc_free(tmp_ctx);
824 return ISC_R_NOMEMORY;
827 if (strcmp(rdn, "@") == 0) {
828 name = zone;
829 } else {
830 name = talloc_asprintf(el_ctx, "%s.%s", rdn, zone);
832 if (name == NULL) {
833 talloc_free(tmp_ctx);
834 return ISC_R_NOMEMORY;
837 for (j=0; j<el->num_values; j++) {
838 struct dnsp_DnssrvRpcRecord rec;
839 enum ndr_err_code ndr_err;
840 isc_result_t result;
842 ndr_err = ndr_pull_struct_blob(&el->values[j], el_ctx, &rec,
843 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
844 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
845 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
846 ldb_dn_get_linearized(dn));
847 continue;
850 result = b9_putnamedrr(state, allnodes, name, &rec);
851 if (result != ISC_R_SUCCESS) {
852 continue;
857 talloc_free(tmp_ctx);
859 return ISC_R_SUCCESS;
864 start a transaction
866 _PUBLIC_ isc_result_t dlz_newversion(const char *zone, void *dbdata, void **versionp)
868 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
870 state->log(ISC_LOG_INFO, "samba_dlz: starting transaction on zone %s", zone);
872 if (state->transaction_token != NULL) {
873 state->log(ISC_LOG_INFO, "samba_dlz: transaction already started for zone %s", zone);
874 return ISC_R_FAILURE;
877 state->transaction_token = talloc_zero(state, int);
878 if (state->transaction_token == NULL) {
879 return ISC_R_NOMEMORY;
882 if (ldb_transaction_start(state->samdb) != LDB_SUCCESS) {
883 state->log(ISC_LOG_INFO, "samba_dlz: failed to start a transaction for zone %s", zone);
884 talloc_free(state->transaction_token);
885 state->transaction_token = NULL;
886 return ISC_R_FAILURE;
889 *versionp = (void *)state->transaction_token;
891 return ISC_R_SUCCESS;
895 end a transaction
897 _PUBLIC_ void dlz_closeversion(const char *zone, isc_boolean_t commit,
898 void *dbdata, void **versionp)
900 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
902 if (state->transaction_token != (int *)*versionp) {
903 state->log(ISC_LOG_INFO, "samba_dlz: transaction not started for zone %s", zone);
904 return;
907 if (commit) {
908 if (ldb_transaction_commit(state->samdb) != LDB_SUCCESS) {
909 state->log(ISC_LOG_INFO, "samba_dlz: failed to commit a transaction for zone %s", zone);
910 return;
912 state->log(ISC_LOG_INFO, "samba_dlz: committed transaction on zone %s", zone);
913 } else {
914 if (ldb_transaction_cancel(state->samdb) != LDB_SUCCESS) {
915 state->log(ISC_LOG_INFO, "samba_dlz: failed to cancel a transaction for zone %s", zone);
916 return;
918 state->log(ISC_LOG_INFO, "samba_dlz: cancelling transaction on zone %s", zone);
921 talloc_free(state->transaction_token);
922 state->transaction_token = NULL;
923 *versionp = NULL;
928 see if there is a SOA record for a zone
930 static bool b9_has_soa(struct dlz_bind9_data *state, struct ldb_dn *dn, const char *zone)
932 const char *attrs[] = { "dnsRecord", NULL };
933 struct ldb_result *res;
934 struct ldb_message_element *el;
935 TALLOC_CTX *tmp_ctx = talloc_new(state);
936 int ret, i;
938 if (!ldb_dn_add_child_fmt(dn, "DC=@,DC=%s", zone)) {
939 talloc_free(tmp_ctx);
940 return false;
943 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
944 attrs, "objectClass=dnsNode");
945 if (ret != LDB_SUCCESS) {
946 talloc_free(tmp_ctx);
947 return false;
950 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
951 if (el == NULL) {
952 talloc_free(tmp_ctx);
953 return false;
955 for (i=0; i<el->num_values; i++) {
956 struct dnsp_DnssrvRpcRecord rec;
957 enum ndr_err_code ndr_err;
959 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec,
960 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
961 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
962 continue;
964 if (rec.wType == DNS_TYPE_SOA) {
965 talloc_free(tmp_ctx);
966 return true;
970 talloc_free(tmp_ctx);
971 return false;
975 configure a writeable zone
977 _PUBLIC_ isc_result_t dlz_configure(dns_view_t *view, void *dbdata)
979 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
980 TALLOC_CTX *tmp_ctx;
981 struct ldb_dn *dn;
982 int i;
984 state->log(ISC_LOG_INFO, "samba_dlz: starting configure");
985 if (state->writeable_zone == NULL) {
986 state->log(ISC_LOG_INFO, "samba_dlz: no writeable_zone method available");
987 return ISC_R_FAILURE;
990 tmp_ctx = talloc_new(state);
992 for (i=0; zone_prefixes[i]; i++) {
993 const char *attrs[] = { "name", NULL };
994 int j, ret;
995 struct ldb_result *res;
997 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
998 if (dn == NULL) {
999 talloc_free(tmp_ctx);
1000 return ISC_R_NOMEMORY;
1003 if (!ldb_dn_add_child_fmt(dn, "%s", zone_prefixes[i])) {
1004 talloc_free(tmp_ctx);
1005 return ISC_R_NOMEMORY;
1008 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
1009 attrs, "objectClass=dnsZone");
1010 if (ret != LDB_SUCCESS) {
1011 continue;
1014 for (j=0; j<res->count; j++) {
1015 isc_result_t result;
1016 const char *zone = ldb_msg_find_attr_as_string(res->msgs[j], "name", NULL);
1017 if (zone == NULL) {
1018 continue;
1020 if (!b9_has_soa(state, dn, zone)) {
1021 continue;
1023 result = state->writeable_zone(view, zone);
1024 if (result != ISC_R_SUCCESS) {
1025 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to configure zone '%s'",
1026 zone);
1027 talloc_free(tmp_ctx);
1028 return result;
1030 state->log(ISC_LOG_INFO, "samba_dlz: configured writeable zone '%s'", zone);
1034 talloc_free(tmp_ctx);
1035 return ISC_R_SUCCESS;
1039 authorize a zone update
1041 _PUBLIC_ isc_boolean_t dlz_ssumatch(const char *signer, const char *name, const char *tcpaddr,
1042 const char *type, const char *key, uint32_t keydatalen, uint8_t *keydata,
1043 void *dbdata)
1045 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1047 state->log(ISC_LOG_INFO, "samba_dlz: allowing update of signer=%s name=%s tcpaddr=%s type=%s key=%s keydatalen=%u",
1048 signer, name, tcpaddr, type, key, keydatalen);
1049 return true;
1054 add a new record
1056 static isc_result_t b9_add_record(struct dlz_bind9_data *state, const char *name,
1057 struct ldb_dn *dn,
1058 struct dnsp_DnssrvRpcRecord *rec)
1060 struct ldb_message *msg;
1061 enum ndr_err_code ndr_err;
1062 struct ldb_val v;
1063 int ret;
1065 msg = ldb_msg_new(rec);
1066 if (msg == NULL) {
1067 return ISC_R_NOMEMORY;
1069 msg->dn = dn;
1070 ret = ldb_msg_add_string(msg, "objectClass", "dnsNode");
1071 if (ret != LDB_SUCCESS) {
1072 return ISC_R_FAILURE;
1075 ndr_err = ndr_push_struct_blob(&v, rec, rec, (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
1076 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1077 return ISC_R_FAILURE;
1079 ret = ldb_msg_add_value(msg, "dnsRecord", &v, NULL);
1080 if (ret != LDB_SUCCESS) {
1081 return ISC_R_FAILURE;
1084 ret = ldb_add(state->samdb, msg);
1085 if (ret != LDB_SUCCESS) {
1086 return ISC_R_FAILURE;
1089 return ISC_R_SUCCESS;
1093 see if two DNS names are the same
1095 static bool dns_name_equal(const char *name1, const char *name2)
1097 size_t len1 = strlen(name1);
1098 size_t len2 = strlen(name2);
1099 if (name1[len1-1] == '.') len1--;
1100 if (name2[len2-1] == '.') len2--;
1101 if (len1 != len2) {
1102 return false;
1104 return strncasecmp_m(name1, name2, len1) == 0;
1109 see if two dns records match
1111 static bool b9_record_match(struct dlz_bind9_data *state,
1112 struct dnsp_DnssrvRpcRecord *rec1, struct dnsp_DnssrvRpcRecord *rec2)
1114 if (rec1->wType != rec2->wType) {
1115 return false;
1117 /* see if this type is single valued */
1118 if (b9_single_valued(rec1->wType)) {
1119 return true;
1122 /* see if the data matches */
1123 switch (rec1->wType) {
1124 case DNS_TYPE_A:
1125 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
1126 case DNS_TYPE_AAAA:
1127 return strcmp(rec1->data.ipv6, rec2->data.ipv6) == 0;
1128 case DNS_TYPE_CNAME:
1129 return dns_name_equal(rec1->data.cname, rec2->data.cname);
1130 case DNS_TYPE_TXT:
1131 return strcmp(rec1->data.txt, rec2->data.txt) == 0;
1132 case DNS_TYPE_PTR:
1133 return strcmp(rec1->data.ptr, rec2->data.ptr) == 0;
1134 case DNS_TYPE_NS:
1135 return dns_name_equal(rec1->data.ns, rec2->data.ns);
1137 case DNS_TYPE_SRV:
1138 return rec1->data.srv.wPriority == rec2->data.srv.wPriority &&
1139 rec1->data.srv.wWeight == rec2->data.srv.wWeight &&
1140 rec1->data.srv.wPort == rec2->data.srv.wPort &&
1141 dns_name_equal(rec1->data.srv.nameTarget, rec2->data.srv.nameTarget);
1143 case DNS_TYPE_MX:
1144 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
1145 dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
1147 case DNS_TYPE_HINFO:
1148 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
1149 strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
1151 case DNS_TYPE_SOA:
1152 return dns_name_equal(rec1->data.soa.mname, rec2->data.soa.mname) &&
1153 dns_name_equal(rec1->data.soa.rname, rec2->data.soa.rname) &&
1154 rec1->data.soa.serial == rec2->data.soa.serial &&
1155 rec1->data.soa.refresh == rec2->data.soa.refresh &&
1156 rec1->data.soa.retry == rec2->data.soa.retry &&
1157 rec1->data.soa.expire == rec2->data.soa.expire &&
1158 rec1->data.soa.minimum == rec2->data.soa.minimum;
1159 default:
1160 state->log(ISC_LOG_ERROR, "samba b9_putrr: unhandled record type %u",
1161 rec1->wType);
1162 break;
1165 return false;
1170 add or modify a rdataset
1172 _PUBLIC_ isc_result_t dlz_addrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1174 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1175 struct dnsp_DnssrvRpcRecord *rec;
1176 struct ldb_dn *dn;
1177 isc_result_t result;
1178 struct ldb_result *res;
1179 const char *attrs[] = { "dnsRecord", NULL };
1180 int ret, i;
1181 struct ldb_message_element *el;
1182 enum ndr_err_code ndr_err;
1183 NTTIME t;
1185 if (state->transaction_token != (void*)version) {
1186 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1187 return ISC_R_FAILURE;
1190 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1191 if (rec == NULL) {
1192 return ISC_R_NOMEMORY;
1195 unix_to_nt_time(&t, time(NULL));
1196 t /= 10*1000*1000; /* convert to seconds (NT time is in 100ns units) */
1197 t /= 3600; /* convert to hours */
1199 rec->rank = DNS_RANK_ZONE;
1200 rec->dwSerial = state->soa_serial;
1201 rec->dwTimeStamp = (uint32_t)t;
1203 if (!b9_parse(state, rdatastr, rec)) {
1204 state->log(ISC_LOG_INFO, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1205 talloc_free(rec);
1206 return ISC_R_FAILURE;
1209 /* find the DN of the record */
1210 result = b9_find_name_dn(state, name, rec, &dn);
1211 if (result != ISC_R_SUCCESS) {
1212 talloc_free(rec);
1213 return result;
1216 /* get any existing records */
1217 ret = ldb_search(state->samdb, rec, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1218 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1219 result = b9_add_record(state, name, dn, rec);
1220 talloc_free(rec);
1221 if (result == ISC_R_SUCCESS) {
1222 state->log(ISC_LOG_ERROR, "samba_dlz: added %s %s", name, rdatastr);
1224 return result;
1227 /* there are existing records. We need to see if this will
1228 * replace a record or add to it
1230 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1231 if (el == NULL) {
1232 state->log(ISC_LOG_ERROR, "samba_dlz: no dnsRecord attribute for %s",
1233 ldb_dn_get_linearized(dn));
1234 talloc_free(rec);
1235 return ISC_R_FAILURE;
1238 for (i=0; i<el->num_values; i++) {
1239 struct dnsp_DnssrvRpcRecord rec2;
1241 ndr_err = ndr_pull_struct_blob(&el->values[i], rec, &rec2,
1242 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1243 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1244 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1245 ldb_dn_get_linearized(dn));
1246 talloc_free(rec);
1247 return ISC_R_FAILURE;
1250 if (b9_record_match(state, rec, &rec2)) {
1251 break;
1254 if (i == el->num_values) {
1255 /* adding a new value */
1256 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values+1);
1257 if (el->values == NULL) {
1258 talloc_free(rec);
1259 return ISC_R_NOMEMORY;
1261 el->num_values++;
1264 ndr_err = ndr_push_struct_blob(&el->values[i], rec, rec,
1265 (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
1266 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1267 state->log(ISC_LOG_ERROR, "samba_dlz: failed to push dnsRecord for %s",
1268 ldb_dn_get_linearized(dn));
1269 talloc_free(rec);
1270 return ISC_R_FAILURE;
1273 /* modify the record */
1274 el->flags = LDB_FLAG_MOD_REPLACE;
1275 ret = ldb_modify(state->samdb, res->msgs[0]);
1276 if (ret != LDB_SUCCESS) {
1277 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1278 ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1279 talloc_free(rec);
1280 return ISC_R_FAILURE;
1283 state->log(ISC_LOG_INFO, "samba_dlz: added rdataset %s '%s'", name, rdatastr);
1285 talloc_free(rec);
1286 return ISC_R_SUCCESS;
1290 remove a rdataset
1292 _PUBLIC_ isc_result_t dlz_subrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1294 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1295 struct dnsp_DnssrvRpcRecord *rec;
1296 struct ldb_dn *dn;
1297 isc_result_t result;
1298 struct ldb_result *res;
1299 const char *attrs[] = { "dnsRecord", NULL };
1300 int ret, i;
1301 struct ldb_message_element *el;
1302 enum ndr_err_code ndr_err;
1304 if (state->transaction_token != (void*)version) {
1305 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1306 return ISC_R_FAILURE;
1309 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1310 if (rec == NULL) {
1311 return ISC_R_NOMEMORY;
1314 if (!b9_parse(state, rdatastr, rec)) {
1315 state->log(ISC_LOG_INFO, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1316 talloc_free(rec);
1317 return ISC_R_FAILURE;
1320 /* find the DN of the record */
1321 result = b9_find_name_dn(state, name, rec, &dn);
1322 if (result != ISC_R_SUCCESS) {
1323 talloc_free(rec);
1324 return result;
1327 /* get the existing records */
1328 ret = ldb_search(state->samdb, rec, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1329 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1330 talloc_free(rec);
1331 return ISC_R_NOTFOUND;
1334 /* there are existing records. We need to see if any match
1336 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1337 if (el == NULL || el->num_values == 0) {
1338 state->log(ISC_LOG_ERROR, "samba_dlz: no dnsRecord attribute for %s",
1339 ldb_dn_get_linearized(dn));
1340 talloc_free(rec);
1341 return ISC_R_FAILURE;
1344 for (i=0; i<el->num_values; i++) {
1345 struct dnsp_DnssrvRpcRecord rec2;
1347 ndr_err = ndr_pull_struct_blob(&el->values[i], rec, &rec2,
1348 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1349 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1350 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1351 ldb_dn_get_linearized(dn));
1352 talloc_free(rec);
1353 return ISC_R_FAILURE;
1356 if (b9_record_match(state, rec, &rec2)) {
1357 break;
1360 if (i == el->num_values) {
1361 talloc_free(rec);
1362 return ISC_R_NOTFOUND;
1365 if (i < el->num_values-1) {
1366 memmove(&el->values[i], &el->values[i+1], sizeof(el->values[0])*((el->num_values-1)-i));
1368 el->num_values--;
1370 if (el->num_values == 0) {
1371 /* delete the record */
1372 ret = ldb_delete(state->samdb, dn);
1373 } else {
1374 /* modify the record */
1375 el->flags = LDB_FLAG_MOD_REPLACE;
1376 ret = ldb_modify(state->samdb, res->msgs[0]);
1378 if (ret != LDB_SUCCESS) {
1379 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1380 ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1381 talloc_free(rec);
1382 return ISC_R_FAILURE;
1385 state->log(ISC_LOG_INFO, "samba_dlz: subtracted rdataset %s '%s'", name, rdatastr);
1387 talloc_free(rec);
1388 return ISC_R_SUCCESS;
1393 delete all records of the given type
1395 _PUBLIC_ isc_result_t dlz_delrdataset(const char *name, const char *type, void *dbdata, void *version)
1397 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1398 TALLOC_CTX *tmp_ctx;
1399 struct ldb_dn *dn;
1400 isc_result_t result;
1401 struct ldb_result *res;
1402 const char *attrs[] = { "dnsRecord", NULL };
1403 int ret, i;
1404 struct ldb_message_element *el;
1405 enum ndr_err_code ndr_err;
1406 enum dns_record_type dns_type;
1407 bool found = false;
1409 if (state->transaction_token != (void*)version) {
1410 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1411 return ISC_R_FAILURE;
1414 if (!b9_dns_type(type, &dns_type)) {
1415 state->log(ISC_LOG_INFO, "samba_dlz: bad dns type %s in delete", type);
1416 return ISC_R_FAILURE;
1419 tmp_ctx = talloc_new(state);
1421 /* find the DN of the record */
1422 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1423 if (result != ISC_R_SUCCESS) {
1424 talloc_free(tmp_ctx);
1425 return result;
1428 /* get the existing records */
1429 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1430 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1431 talloc_free(tmp_ctx);
1432 return ISC_R_NOTFOUND;
1435 /* there are existing records. We need to see if any match the type
1437 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1438 if (el == NULL || el->num_values == 0) {
1439 talloc_free(tmp_ctx);
1440 return ISC_R_NOTFOUND;
1443 for (i=0; i<el->num_values; i++) {
1444 struct dnsp_DnssrvRpcRecord rec2;
1446 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec2,
1447 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1448 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1449 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1450 ldb_dn_get_linearized(dn));
1451 talloc_free(tmp_ctx);
1452 return ISC_R_FAILURE;
1455 if (dns_type == rec2.wType) {
1456 if (i < el->num_values-1) {
1457 memmove(&el->values[i], &el->values[i+1],
1458 sizeof(el->values[0])*((el->num_values-1)-i));
1460 el->num_values--;
1461 i--;
1462 found = true;
1466 if (!found) {
1467 talloc_free(tmp_ctx);
1468 return ISC_R_FAILURE;
1471 if (el->num_values == 0) {
1472 /* delete the record */
1473 ret = ldb_delete(state->samdb, dn);
1474 } else {
1475 /* modify the record */
1476 el->flags = LDB_FLAG_MOD_REPLACE;
1477 ret = ldb_modify(state->samdb, res->msgs[0]);
1479 if (ret != LDB_SUCCESS) {
1480 state->log(ISC_LOG_ERROR, "samba_dlz: failed to delete type %s in %s - %s",
1481 type, ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1482 talloc_free(tmp_ctx);
1483 return ISC_R_FAILURE;
1486 state->log(ISC_LOG_INFO, "samba_dlz: deleted rdataset %s of type %s", name, type);
1488 talloc_free(tmp_ctx);
1489 return ISC_R_SUCCESS;