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/>.
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/auth.h"
29 #include "auth/session.h"
30 #include "auth/gensec/gensec.h"
31 #include "librpc/gen_ndr/security.h"
32 #include "auth/credentials/credentials.h"
33 #include "system/kerberos.h"
34 #include "auth/kerberos/kerberos.h"
35 #include "gen_ndr/ndr_dnsp.h"
36 #include "gen_ndr/server_id.h"
37 #include "messaging/messaging.h"
38 #include "lib/cmdline/popt_common.h"
39 #include "dlz_minimal.h"
47 struct dlz_bind9_data {
48 struct b9_options options;
49 struct ldb_context *samdb;
50 struct tevent_context *ev_ctx;
51 struct loadparm_context *lp;
52 int *transaction_token;
55 /* Used for dynamic update */
56 struct smb_krb5_context *smb_krb5_ctx;
57 struct auth4_context *auth_context;
58 struct auth_session_info *session_info;
61 /* helper functions from the dlz_dlopen driver */
62 void (*log)(int level, const char *fmt, ...);
63 isc_result_t (*putrr)(dns_sdlzlookup_t *handle, const char *type,
64 dns_ttl_t ttl, const char *data);
65 isc_result_t (*putnamedrr)(dns_sdlzlookup_t *handle, const char *name,
66 const char *type, dns_ttl_t ttl, const char *data);
67 isc_result_t (*writeable_zone)(dns_view_t *view, const char *zone_name);
71 static const char *zone_prefixes[] = {
72 "CN=MicrosoftDNS,DC=DomainDnsZones",
73 "CN=MicrosoftDNS,DC=ForestDnsZones",
74 "CN=MicrosoftDNS,CN=System",
79 return the version of the API
81 _PUBLIC_ int dlz_version(unsigned int *flags)
83 return DLZ_DLOPEN_VERSION;
87 remember a helper function from the bind9 dlz_dlopen driver
89 static void b9_add_helper(struct dlz_bind9_data *state, const char *helper_name, void *ptr)
91 if (strcmp(helper_name, "log") == 0) {
94 if (strcmp(helper_name, "putrr") == 0) {
97 if (strcmp(helper_name, "putnamedrr") == 0) {
98 state->putnamedrr = ptr;
100 if (strcmp(helper_name, "writeable_zone") == 0) {
101 state->writeable_zone = ptr;
106 format a record for bind9
108 static bool b9_format(struct dlz_bind9_data *state,
110 struct dnsp_DnssrvRpcRecord *rec,
111 const char **type, const char **data)
113 switch (rec->wType) {
116 *data = rec->data.ipv4;
121 *data = rec->data.ipv6;
126 *data = rec->data.cname;
131 *data = rec->data.txt;
136 *data = rec->data.ptr;
141 *data = talloc_asprintf(mem_ctx, "%u %u %u %s",
142 rec->data.srv.wPriority,
143 rec->data.srv.wWeight,
145 rec->data.srv.nameTarget);
150 *data = talloc_asprintf(mem_ctx, "%u %s",
151 rec->data.mx.wPriority,
152 rec->data.mx.nameTarget);
157 *data = talloc_asprintf(mem_ctx, "%s %s",
164 *data = rec->data.ns;
171 /* we need to fake the authoritative nameserver to
172 * point at ourselves. This is how AD DNS servers
173 * force clients to send updates to the right local DC
175 mname = talloc_asprintf(mem_ctx, "%s.%s",
176 lpcfg_netbios_name(state->lp), lpcfg_dnsdomain(state->lp));
180 mname = strlower_talloc(mem_ctx, mname);
185 state->soa_serial = rec->data.soa.serial;
187 *data = talloc_asprintf(mem_ctx, "%s %s %u %u %u %u %u",
190 rec->data.soa.serial,
191 rec->data.soa.refresh,
193 rec->data.soa.expire,
194 rec->data.soa.minimum);
199 state->log(ISC_LOG_ERROR, "samba b9_putrr: unhandled record type %u",
207 static const struct {
208 enum dns_record_type dns_type;
212 { DNS_TYPE_A, "A" , false},
213 { DNS_TYPE_AAAA, "AAAA" , false},
214 { DNS_TYPE_CNAME, "CNAME" , true},
215 { DNS_TYPE_TXT, "TXT" , false},
216 { DNS_TYPE_PTR, "PTR" , false},
217 { DNS_TYPE_SRV, "SRV" , false},
218 { DNS_TYPE_MX, "MX" , false},
219 { DNS_TYPE_HINFO, "HINFO" , false},
220 { DNS_TYPE_NS, "NS" , false},
221 { DNS_TYPE_SOA, "SOA" , true},
226 see if a DNS type is single valued
228 static bool b9_single_valued(enum dns_record_type dns_type)
231 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
232 if (dns_typemap[i].dns_type == dns_type) {
233 return dns_typemap[i].single_valued;
240 see if a DNS type is single valued
242 static bool b9_dns_type(const char *type, enum dns_record_type *dtype)
245 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
246 if (strcasecmp(dns_typemap[i].typestr, type) == 0) {
247 *dtype = dns_typemap[i].dns_type;
255 #define DNS_PARSE_STR(ret, str, sep, saveptr) do { \
256 (ret) = strtok_r(str, sep, &saveptr); \
257 if ((ret) == NULL) return false; \
260 #define DNS_PARSE_UINT(ret, str, sep, saveptr) do { \
261 char *istr = strtok_r(str, sep, &saveptr); \
262 if ((istr) == NULL) return false; \
263 (ret) = strtoul(istr, NULL, 10); \
267 parse a record from bind9
269 static bool b9_parse(struct dlz_bind9_data *state,
270 const char *rdatastr,
271 struct dnsp_DnssrvRpcRecord *rec)
273 char *full_name, *dclass, *type;
274 char *str, *saveptr=NULL;
277 str = talloc_strdup(rec, rdatastr);
282 /* parse the SDLZ string form */
283 DNS_PARSE_STR(full_name, str, "\t", saveptr);
284 DNS_PARSE_UINT(rec->dwTtlSeconds, NULL, "\t", saveptr);
285 DNS_PARSE_STR(dclass, NULL, "\t", saveptr);
286 DNS_PARSE_STR(type, NULL, "\t", saveptr);
288 /* construct the record */
289 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
290 if (strcasecmp(type, dns_typemap[i].typestr) == 0) {
291 rec->wType = dns_typemap[i].dns_type;
295 if (i == ARRAY_SIZE(dns_typemap)) {
296 state->log(ISC_LOG_ERROR, "samba_dlz: unsupported record type '%s' for '%s'",
301 switch (rec->wType) {
303 DNS_PARSE_STR(rec->data.ipv4, NULL, " ", saveptr);
307 DNS_PARSE_STR(rec->data.ipv6, NULL, " ", saveptr);
311 DNS_PARSE_STR(rec->data.cname, NULL, " ", saveptr);
315 DNS_PARSE_STR(rec->data.txt, NULL, "\t", saveptr);
319 DNS_PARSE_STR(rec->data.ptr, NULL, " ", saveptr);
323 DNS_PARSE_UINT(rec->data.srv.wPriority, NULL, " ", saveptr);
324 DNS_PARSE_UINT(rec->data.srv.wWeight, NULL, " ", saveptr);
325 DNS_PARSE_UINT(rec->data.srv.wPort, NULL, " ", saveptr);
326 DNS_PARSE_STR(rec->data.srv.nameTarget, NULL, " ", saveptr);
330 DNS_PARSE_UINT(rec->data.mx.wPriority, NULL, " ", saveptr);
331 DNS_PARSE_STR(rec->data.mx.nameTarget, NULL, " ", saveptr);
335 DNS_PARSE_STR(rec->data.hinfo.cpu, NULL, " ", saveptr);
336 DNS_PARSE_STR(rec->data.hinfo.os, NULL, " ", saveptr);
340 DNS_PARSE_STR(rec->data.ns, NULL, " ", saveptr);
344 DNS_PARSE_STR(rec->data.soa.mname, NULL, " ", saveptr);
345 DNS_PARSE_STR(rec->data.soa.rname, NULL, " ", saveptr);
346 DNS_PARSE_UINT(rec->data.soa.serial, NULL, " ", saveptr);
347 DNS_PARSE_UINT(rec->data.soa.refresh, NULL, " ", saveptr);
348 DNS_PARSE_UINT(rec->data.soa.retry, NULL, " ", saveptr);
349 DNS_PARSE_UINT(rec->data.soa.expire, NULL, " ", saveptr);
350 DNS_PARSE_UINT(rec->data.soa.minimum, NULL, " ", saveptr);
354 state->log(ISC_LOG_ERROR, "samba b9_parse: unhandled record type %u",
359 /* we should be at the end of the buffer now */
360 if (strtok_r(NULL, "\t ", &saveptr) != NULL) {
361 state->log(ISC_LOG_ERROR, "samba b9_parse: expected data at end of string for '%s'");
369 send a resource recond to bind9
371 static isc_result_t b9_putrr(struct dlz_bind9_data *state,
372 void *handle, struct dnsp_DnssrvRpcRecord *rec,
376 const char *type, *data;
377 TALLOC_CTX *tmp_ctx = talloc_new(state);
379 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
380 return ISC_R_FAILURE;
384 talloc_free(tmp_ctx);
385 return ISC_R_NOMEMORY;
390 for (i=0; types[i]; i++) {
391 if (strcmp(types[i], type) == 0) break;
393 if (types[i] == NULL) {
395 return ISC_R_SUCCESS;
399 result = state->putrr(handle, type, rec->dwTtlSeconds, data);
400 if (result != ISC_R_SUCCESS) {
401 state->log(ISC_LOG_ERROR, "Failed to put rr");
403 talloc_free(tmp_ctx);
409 send a named resource recond to bind9
411 static isc_result_t b9_putnamedrr(struct dlz_bind9_data *state,
412 void *handle, const char *name,
413 struct dnsp_DnssrvRpcRecord *rec)
416 const char *type, *data;
417 TALLOC_CTX *tmp_ctx = talloc_new(state);
419 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
420 return ISC_R_FAILURE;
424 talloc_free(tmp_ctx);
425 return ISC_R_NOMEMORY;
428 result = state->putnamedrr(handle, name, type, rec->dwTtlSeconds, data);
429 if (result != ISC_R_SUCCESS) {
430 state->log(ISC_LOG_ERROR, "Failed to put named rr '%s'", name);
432 talloc_free(tmp_ctx);
439 static isc_result_t parse_options(struct dlz_bind9_data *state,
440 unsigned int argc, char *argv[],
441 struct b9_options *options)
445 struct poptOption long_options[] = {
446 { "url", 'H', POPT_ARG_STRING, &options->url, 0, "database URL", "URL" },
447 { "debug", 'd', POPT_ARG_STRING, &options->debug, 0, "debug level", "DEBUG" },
451 pc = poptGetContext("dlz_bind9", argc, (const char **)argv, long_options,
452 POPT_CONTEXT_KEEP_FIRST);
453 while ((opt = poptGetNextOpt(pc)) != -1) {
456 state->log(ISC_LOG_ERROR, "dlz_bind9: Invalid option %s: %s",
457 poptBadOption(pc, 0), poptStrerror(opt));
458 return ISC_R_FAILURE;
462 return ISC_R_SUCCESS;
467 * Create session info from PAC
468 * This is called as auth_context->generate_session_info_pac()
470 static NTSTATUS b9_generate_session_info_pac(struct auth4_context *auth_context,
472 struct smb_krb5_context *smb_krb5_context,
474 const char *principal_name,
475 const struct tsocket_address *remote_addr,
476 uint32_t session_info_flags,
477 struct auth_session_info **session_info)
480 struct auth_user_info_dc *user_info_dc;
483 tmp_ctx = talloc_new(mem_ctx);
484 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
486 status = kerberos_pac_blob_to_user_info_dc(tmp_ctx,
488 smb_krb5_context->krb5_context,
492 if (!NT_STATUS_IS_OK(status)) {
493 talloc_free(tmp_ctx);
497 if (user_info_dc->info->authenticated) {
498 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
501 session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
503 status = auth_generate_session_info(mem_ctx, NULL, NULL, user_info_dc,
504 session_info_flags, session_info);
505 if (!NT_STATUS_IS_OK(status)) {
506 talloc_free(tmp_ctx);
510 talloc_free(tmp_ctx);
516 called to initialise the driver
518 _PUBLIC_ isc_result_t dlz_create(const char *dlzname,
519 unsigned int argc, char *argv[],
522 struct dlz_bind9_data *state;
523 const char *helper_name;
529 state = talloc_zero(NULL, struct dlz_bind9_data);
531 return ISC_R_NOMEMORY;
534 /* fill in the helper functions */
535 va_start(ap, dbdata);
536 while ((helper_name = va_arg(ap, const char *)) != NULL) {
537 b9_add_helper(state, helper_name, va_arg(ap, void*));
541 /* Do not install samba signal handlers */
542 fault_setup_disable();
545 setup_logging("samba_dlz", DEBUG_DEFAULT_STDERR);
547 state->ev_ctx = s4_event_context_init(state);
548 if (state->ev_ctx == NULL) {
549 result = ISC_R_NOMEMORY;
553 result = parse_options(state, argc, argv, &state->options);
554 if (result != ISC_R_SUCCESS) {
558 state->lp = loadparm_init_global(true);
559 if (state->lp == NULL) {
560 result = ISC_R_NOMEMORY;
564 if (state->options.debug) {
565 lpcfg_do_global_parameter(state->lp, "log level", state->options.debug);
567 lpcfg_do_global_parameter(state->lp, "log level", "0");
570 if (smb_krb5_init_context(state, state->ev_ctx, state->lp, &state->smb_krb5_ctx) != 0) {
571 result = ISC_R_NOMEMORY;
575 nt_status = gensec_init();
576 if (!NT_STATUS_IS_OK(nt_status)) {
577 result = ISC_R_NOMEMORY;
581 state->auth_context = talloc_zero(state, struct auth4_context);
582 if (state->auth_context == NULL) {
583 result = ISC_R_NOMEMORY;
587 if (state->options.url == NULL) {
588 state->options.url = lpcfg_private_path(state, state->lp, "dns/sam.ldb");
589 if (state->options.url == NULL) {
590 result = ISC_R_NOMEMORY;
595 state->samdb = samdb_connect_url(state, state->ev_ctx, state->lp,
596 system_session(state->lp), 0, state->options.url);
597 if (state->samdb == NULL) {
598 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to connect to %s",
600 result = ISC_R_FAILURE;
604 dn = ldb_get_default_basedn(state->samdb);
606 state->log(ISC_LOG_ERROR, "samba_dlz: Unable to get basedn for %s - %s",
607 state->options.url, ldb_errstring(state->samdb));
608 result = ISC_R_FAILURE;
612 state->log(ISC_LOG_INFO, "samba_dlz: started for DN %s",
613 ldb_dn_get_linearized(dn));
615 state->auth_context->event_ctx = state->ev_ctx;
616 state->auth_context->lp_ctx = state->lp;
617 state->auth_context->sam_ctx = state->samdb;
618 state->auth_context->generate_session_info_pac = b9_generate_session_info_pac;
622 return ISC_R_SUCCESS;
632 _PUBLIC_ void dlz_destroy(void *dbdata)
634 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
635 state->log(ISC_LOG_INFO, "samba_dlz: shutting down");
641 return the base DN for a zone
643 static isc_result_t b9_find_zone_dn(struct dlz_bind9_data *state, const char *zone_name,
644 TALLOC_CTX *mem_ctx, struct ldb_dn **zone_dn)
647 TALLOC_CTX *tmp_ctx = talloc_new(state);
648 const char *attrs[] = { NULL };
651 for (i=0; zone_prefixes[i]; i++) {
653 struct ldb_result *res;
655 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
657 talloc_free(tmp_ctx);
658 return ISC_R_NOMEMORY;
661 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone_name, zone_prefixes[i])) {
662 talloc_free(tmp_ctx);
663 return ISC_R_NOMEMORY;
666 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsZone");
667 if (ret == LDB_SUCCESS) {
668 if (zone_dn != NULL) {
669 *zone_dn = talloc_steal(mem_ctx, dn);
671 talloc_free(tmp_ctx);
672 return ISC_R_SUCCESS;
677 talloc_free(tmp_ctx);
678 return ISC_R_NOTFOUND;
683 return the DN for a name. The record does not need to exist, but the
686 static isc_result_t b9_find_name_dn(struct dlz_bind9_data *state, const char *name,
687 TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
691 /* work through the name piece by piece, until we find a zone */
694 result = b9_find_zone_dn(state, p, mem_ctx, dn);
695 if (result == ISC_R_SUCCESS) {
696 /* we found a zone, now extend the DN to get
701 ret = ldb_dn_add_child_fmt(*dn, "DC=@");
703 ret = ldb_dn_add_child_fmt(*dn, "DC=%.*s", (int)(p-name)-1, name);
707 return ISC_R_NOMEMORY;
709 return ISC_R_SUCCESS;
717 return ISC_R_NOTFOUND;
722 see if we handle a given zone
724 _PUBLIC_ isc_result_t dlz_findzonedb(void *dbdata, const char *name)
726 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
727 return b9_find_zone_dn(state, name, NULL, NULL);
734 static isc_result_t dlz_lookup_types(struct dlz_bind9_data *state,
735 const char *zone, const char *name,
736 dns_sdlzlookup_t *lookup,
739 TALLOC_CTX *tmp_ctx = talloc_new(state);
740 const char *attrs[] = { "dnsRecord", NULL };
741 int ret = LDB_SUCCESS, i;
742 struct ldb_result *res;
743 struct ldb_message_element *el;
746 for (i=0; zone_prefixes[i]; i++) {
747 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
749 talloc_free(tmp_ctx);
750 return ISC_R_NOMEMORY;
753 if (!ldb_dn_add_child_fmt(dn, "DC=%s,DC=%s,%s", name, zone, zone_prefixes[i])) {
754 talloc_free(tmp_ctx);
755 return ISC_R_NOMEMORY;
758 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
759 attrs, "objectClass=dnsNode");
760 if (ret == LDB_SUCCESS) {
764 if (ret != LDB_SUCCESS) {
765 talloc_free(tmp_ctx);
766 return ISC_R_NOTFOUND;
769 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
770 if (el == NULL || el->num_values == 0) {
771 talloc_free(tmp_ctx);
772 return ISC_R_NOTFOUND;
775 for (i=0; i<el->num_values; i++) {
776 struct dnsp_DnssrvRpcRecord rec;
777 enum ndr_err_code ndr_err;
780 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec,
781 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
782 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
783 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
784 ldb_dn_get_linearized(dn));
785 talloc_free(tmp_ctx);
786 return ISC_R_FAILURE;
789 result = b9_putrr(state, lookup, &rec, types);
790 if (result != ISC_R_SUCCESS) {
791 talloc_free(tmp_ctx);
796 talloc_free(tmp_ctx);
797 return ISC_R_SUCCESS;
803 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name,
804 void *dbdata, dns_sdlzlookup_t *lookup)
806 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
807 return dlz_lookup_types(state, zone, name, lookup, NULL);
812 see if a zone transfer is allowed
814 _PUBLIC_ isc_result_t dlz_allowzonexfr(void *dbdata, const char *name, const char *client)
816 /* just say yes for all our zones for now */
817 return dlz_findzonedb(dbdata, name);
821 perform a zone transfer
823 _PUBLIC_ isc_result_t dlz_allnodes(const char *zone, void *dbdata,
824 dns_sdlzallnodes_t *allnodes)
826 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
827 const char *attrs[] = { "dnsRecord", NULL };
828 int ret = LDB_SUCCESS, i, j;
830 struct ldb_result *res;
831 TALLOC_CTX *tmp_ctx = talloc_new(state);
833 for (i=0; zone_prefixes[i]; i++) {
834 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
836 talloc_free(tmp_ctx);
837 return ISC_R_NOMEMORY;
840 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone, zone_prefixes[i])) {
841 talloc_free(tmp_ctx);
842 return ISC_R_NOMEMORY;
845 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
846 attrs, "objectClass=dnsNode");
847 if (ret == LDB_SUCCESS) {
851 if (ret != LDB_SUCCESS) {
852 talloc_free(tmp_ctx);
853 return ISC_R_NOTFOUND;
856 for (i=0; i<res->count; i++) {
857 struct ldb_message_element *el;
858 TALLOC_CTX *el_ctx = talloc_new(tmp_ctx);
859 const char *rdn, *name;
860 const struct ldb_val *v;
862 el = ldb_msg_find_element(res->msgs[i], "dnsRecord");
863 if (el == NULL || el->num_values == 0) {
864 state->log(ISC_LOG_INFO, "failed to find dnsRecord for %s",
865 ldb_dn_get_linearized(dn));
870 v = ldb_dn_get_rdn_val(res->msgs[i]->dn);
872 state->log(ISC_LOG_INFO, "failed to find RDN for %s",
873 ldb_dn_get_linearized(dn));
878 rdn = talloc_strndup(el_ctx, (char *)v->data, v->length);
880 talloc_free(tmp_ctx);
881 return ISC_R_NOMEMORY;
884 if (strcmp(rdn, "@") == 0) {
887 name = talloc_asprintf(el_ctx, "%s.%s", rdn, zone);
890 talloc_free(tmp_ctx);
891 return ISC_R_NOMEMORY;
894 for (j=0; j<el->num_values; j++) {
895 struct dnsp_DnssrvRpcRecord rec;
896 enum ndr_err_code ndr_err;
899 ndr_err = ndr_pull_struct_blob(&el->values[j], el_ctx, &rec,
900 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
901 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
902 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
903 ldb_dn_get_linearized(dn));
907 result = b9_putnamedrr(state, allnodes, name, &rec);
908 if (result != ISC_R_SUCCESS) {
914 talloc_free(tmp_ctx);
916 return ISC_R_SUCCESS;
923 _PUBLIC_ isc_result_t dlz_newversion(const char *zone, void *dbdata, void **versionp)
925 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
927 state->log(ISC_LOG_INFO, "samba_dlz: starting transaction on zone %s", zone);
929 if (state->transaction_token != NULL) {
930 state->log(ISC_LOG_INFO, "samba_dlz: transaction already started for zone %s", zone);
931 return ISC_R_FAILURE;
934 state->transaction_token = talloc_zero(state, int);
935 if (state->transaction_token == NULL) {
936 return ISC_R_NOMEMORY;
939 if (ldb_transaction_start(state->samdb) != LDB_SUCCESS) {
940 state->log(ISC_LOG_INFO, "samba_dlz: failed to start a transaction for zone %s", zone);
941 talloc_free(state->transaction_token);
942 state->transaction_token = NULL;
943 return ISC_R_FAILURE;
946 *versionp = (void *)state->transaction_token;
948 return ISC_R_SUCCESS;
954 _PUBLIC_ void dlz_closeversion(const char *zone, isc_boolean_t commit,
955 void *dbdata, void **versionp)
957 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
959 if (state->transaction_token != (int *)*versionp) {
960 state->log(ISC_LOG_INFO, "samba_dlz: transaction not started for zone %s", zone);
965 if (ldb_transaction_commit(state->samdb) != LDB_SUCCESS) {
966 state->log(ISC_LOG_INFO, "samba_dlz: failed to commit a transaction for zone %s", zone);
969 state->log(ISC_LOG_INFO, "samba_dlz: committed transaction on zone %s", zone);
971 if (ldb_transaction_cancel(state->samdb) != LDB_SUCCESS) {
972 state->log(ISC_LOG_INFO, "samba_dlz: failed to cancel a transaction for zone %s", zone);
975 state->log(ISC_LOG_INFO, "samba_dlz: cancelling transaction on zone %s", zone);
978 talloc_free(state->transaction_token);
979 state->transaction_token = NULL;
985 see if there is a SOA record for a zone
987 static bool b9_has_soa(struct dlz_bind9_data *state, struct ldb_dn *dn, const char *zone)
989 const char *attrs[] = { "dnsRecord", NULL };
990 struct ldb_result *res;
991 struct ldb_message_element *el;
992 TALLOC_CTX *tmp_ctx = talloc_new(state);
995 if (!ldb_dn_add_child_fmt(dn, "DC=@,DC=%s", zone)) {
996 talloc_free(tmp_ctx);
1000 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
1001 attrs, "objectClass=dnsNode");
1002 if (ret != LDB_SUCCESS) {
1003 talloc_free(tmp_ctx);
1007 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1009 talloc_free(tmp_ctx);
1012 for (i=0; i<el->num_values; i++) {
1013 struct dnsp_DnssrvRpcRecord rec;
1014 enum ndr_err_code ndr_err;
1016 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec,
1017 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1018 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1021 if (rec.wType == DNS_TYPE_SOA) {
1022 talloc_free(tmp_ctx);
1027 talloc_free(tmp_ctx);
1032 configure a writeable zone
1034 _PUBLIC_ isc_result_t dlz_configure(dns_view_t *view, void *dbdata)
1036 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1037 TALLOC_CTX *tmp_ctx;
1041 state->log(ISC_LOG_INFO, "samba_dlz: starting configure");
1042 if (state->writeable_zone == NULL) {
1043 state->log(ISC_LOG_INFO, "samba_dlz: no writeable_zone method available");
1044 return ISC_R_FAILURE;
1047 tmp_ctx = talloc_new(state);
1049 for (i=0; zone_prefixes[i]; i++) {
1050 const char *attrs[] = { "name", NULL };
1052 struct ldb_result *res;
1054 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
1056 talloc_free(tmp_ctx);
1057 return ISC_R_NOMEMORY;
1060 if (!ldb_dn_add_child_fmt(dn, "%s", zone_prefixes[i])) {
1061 talloc_free(tmp_ctx);
1062 return ISC_R_NOMEMORY;
1065 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
1066 attrs, "objectClass=dnsZone");
1067 if (ret != LDB_SUCCESS) {
1071 for (j=0; j<res->count; j++) {
1072 isc_result_t result;
1073 const char *zone = ldb_msg_find_attr_as_string(res->msgs[j], "name", NULL);
1074 struct ldb_dn *zone_dn;
1079 zone_dn = ldb_dn_copy(tmp_ctx, dn);
1080 if (zone_dn == NULL) {
1081 talloc_free(tmp_ctx);
1082 return ISC_R_NOMEMORY;
1085 if (!b9_has_soa(state, zone_dn, zone)) {
1088 result = state->writeable_zone(view, zone);
1089 if (result != ISC_R_SUCCESS) {
1090 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to configure zone '%s'",
1092 talloc_free(tmp_ctx);
1095 state->log(ISC_LOG_INFO, "samba_dlz: configured writeable zone '%s'", zone);
1099 talloc_free(tmp_ctx);
1100 return ISC_R_SUCCESS;
1104 authorize a zone update
1106 _PUBLIC_ isc_boolean_t dlz_ssumatch(const char *signer, const char *name, const char *tcpaddr,
1107 const char *type, const char *key, uint32_t keydatalen, uint8_t *keydata,
1110 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1111 TALLOC_CTX *tmp_ctx;
1113 struct cli_credentials *server_credentials;
1118 struct gensec_security *gensec_ctx;
1119 struct auth_session_info *session_info;
1121 isc_result_t result;
1122 struct ldb_result *res;
1123 const char * attrs[] = { NULL };
1124 uint32_t access_mask;
1126 /* Remove cached credentials, if any */
1127 if (state->session_info) {
1128 talloc_free(state->session_info);
1129 state->session_info = NULL;
1131 if (state->update_name) {
1132 talloc_free(state->update_name);
1133 state->update_name = NULL;
1136 tmp_ctx = talloc_new(NULL);
1137 if (tmp_ctx == NULL) {
1138 state->log(ISC_LOG_ERROR, "samba_dlz: no memory");
1142 ap_req = data_blob_const(keydata, keydatalen);
1143 server_credentials = cli_credentials_init(tmp_ctx);
1144 if (!server_credentials) {
1145 state->log(ISC_LOG_ERROR, "samba_dlz: failed to init server credentials");
1146 talloc_free(tmp_ctx);
1150 cli_credentials_set_krb5_context(server_credentials, state->smb_krb5_ctx);
1151 cli_credentials_set_conf(server_credentials, state->lp);
1153 keytab_name = talloc_asprintf(tmp_ctx, "file:%s/dns.keytab",
1154 lpcfg_private_dir(state->lp));
1155 ret = cli_credentials_set_keytab_name(server_credentials, state->lp, keytab_name,
1158 state->log(ISC_LOG_ERROR, "samba_dlz: failed to obtain server credentials from %s",
1160 talloc_free(tmp_ctx);
1163 talloc_free(keytab_name);
1165 nt_status = gensec_server_start(tmp_ctx,
1166 lpcfg_gensec_settings(tmp_ctx, state->lp),
1167 state->auth_context, &gensec_ctx);
1168 if (!NT_STATUS_IS_OK(nt_status)) {
1169 state->log(ISC_LOG_ERROR, "samba_dlz: failed to start gensec server");
1170 talloc_free(tmp_ctx);
1174 gensec_set_credentials(gensec_ctx, server_credentials);
1176 nt_status = gensec_start_mech_by_name(gensec_ctx, "spnego");
1177 if (!NT_STATUS_IS_OK(nt_status)) {
1178 state->log(ISC_LOG_ERROR, "samba_dlz: failed to start spnego");
1179 talloc_free(tmp_ctx);
1183 nt_status = gensec_update(gensec_ctx, tmp_ctx, state->ev_ctx, ap_req, &ap_req);
1184 if (!NT_STATUS_IS_OK(nt_status)) {
1185 state->log(ISC_LOG_ERROR, "samba_dlz: spnego update failed");
1186 talloc_free(tmp_ctx);
1190 nt_status = gensec_session_info(gensec_ctx, tmp_ctx, &session_info);
1191 if (!NT_STATUS_IS_OK(nt_status)) {
1192 state->log(ISC_LOG_ERROR, "samba_dlz: failed to create session info");
1193 talloc_free(tmp_ctx);
1197 /* Get the DN from name */
1198 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1199 if (result != ISC_R_SUCCESS) {
1200 state->log(ISC_LOG_ERROR, "samba_dlz: failed to find name %s", name);
1201 talloc_free(tmp_ctx);
1205 /* make sure the dn exists, or find parent dn in case new object is being added */
1206 ldb_ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
1207 attrs, "objectClass=dnsNode");
1208 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
1209 ldb_dn_remove_child_components(dn, 1);
1210 access_mask = SEC_ADS_CREATE_CHILD;
1212 } else if (ldb_ret == LDB_SUCCESS) {
1213 access_mask = SEC_STD_REQUIRED | SEC_ADS_SELF_WRITE;
1216 talloc_free(tmp_ctx);
1221 ldb_ret = dsdb_check_access_on_dn(state->samdb, tmp_ctx, dn,
1222 session_info->security_token,
1224 if (ldb_ret != LDB_SUCCESS) {
1225 state->log(ISC_LOG_INFO,
1226 "samba_dlz: disallowing update of signer=%s name=%s type=%s error=%s",
1227 signer, name, type, ldb_strerror(ldb_ret));
1228 talloc_free(tmp_ctx);
1232 /* Cache session_info, so it can be used in the actual add/delete operation */
1233 state->update_name = talloc_strdup(state, name);
1234 if (state->update_name == NULL) {
1235 state->log(ISC_LOG_ERROR, "samba_dlz: memory allocation error");
1236 talloc_free(tmp_ctx);
1239 state->session_info = talloc_steal(state, session_info);
1241 state->log(ISC_LOG_INFO, "samba_dlz: allowing update of signer=%s name=%s tcpaddr=%s type=%s key=%s",
1242 signer, name, tcpaddr, type, key);
1244 talloc_free(tmp_ctx);
1252 static isc_result_t b9_add_record(struct dlz_bind9_data *state, const char *name,
1254 struct dnsp_DnssrvRpcRecord *rec)
1256 struct ldb_message *msg;
1257 enum ndr_err_code ndr_err;
1261 msg = ldb_msg_new(rec);
1263 return ISC_R_NOMEMORY;
1266 ret = ldb_msg_add_string(msg, "objectClass", "dnsNode");
1267 if (ret != LDB_SUCCESS) {
1268 return ISC_R_FAILURE;
1271 ndr_err = ndr_push_struct_blob(&v, rec, rec, (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
1272 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1273 return ISC_R_FAILURE;
1275 ret = ldb_msg_add_value(msg, "dnsRecord", &v, NULL);
1276 if (ret != LDB_SUCCESS) {
1277 return ISC_R_FAILURE;
1280 ret = ldb_add(state->samdb, msg);
1281 if (ret != LDB_SUCCESS) {
1282 return ISC_R_FAILURE;
1285 return ISC_R_SUCCESS;
1289 see if two DNS names are the same
1291 static bool dns_name_equal(const char *name1, const char *name2)
1293 size_t len1 = strlen(name1);
1294 size_t len2 = strlen(name2);
1295 if (name1[len1-1] == '.') len1--;
1296 if (name2[len2-1] == '.') len2--;
1300 return strncasecmp_m(name1, name2, len1) == 0;
1305 see if two dns records match
1307 static bool b9_record_match(struct dlz_bind9_data *state,
1308 struct dnsp_DnssrvRpcRecord *rec1, struct dnsp_DnssrvRpcRecord *rec2)
1310 if (rec1->wType != rec2->wType) {
1313 /* see if this type is single valued */
1314 if (b9_single_valued(rec1->wType)) {
1318 /* see if the data matches */
1319 switch (rec1->wType) {
1321 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
1323 return strcmp(rec1->data.ipv6, rec2->data.ipv6) == 0;
1324 case DNS_TYPE_CNAME:
1325 return dns_name_equal(rec1->data.cname, rec2->data.cname);
1327 return strcmp(rec1->data.txt, rec2->data.txt) == 0;
1329 return strcmp(rec1->data.ptr, rec2->data.ptr) == 0;
1331 return dns_name_equal(rec1->data.ns, rec2->data.ns);
1334 return rec1->data.srv.wPriority == rec2->data.srv.wPriority &&
1335 rec1->data.srv.wWeight == rec2->data.srv.wWeight &&
1336 rec1->data.srv.wPort == rec2->data.srv.wPort &&
1337 dns_name_equal(rec1->data.srv.nameTarget, rec2->data.srv.nameTarget);
1340 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
1341 dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
1343 case DNS_TYPE_HINFO:
1344 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
1345 strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
1348 return dns_name_equal(rec1->data.soa.mname, rec2->data.soa.mname) &&
1349 dns_name_equal(rec1->data.soa.rname, rec2->data.soa.rname) &&
1350 rec1->data.soa.serial == rec2->data.soa.serial &&
1351 rec1->data.soa.refresh == rec2->data.soa.refresh &&
1352 rec1->data.soa.retry == rec2->data.soa.retry &&
1353 rec1->data.soa.expire == rec2->data.soa.expire &&
1354 rec1->data.soa.minimum == rec2->data.soa.minimum;
1356 state->log(ISC_LOG_ERROR, "samba b9_putrr: unhandled record type %u",
1365 * Update session_info on samdb using the cached credentials
1367 static bool b9_set_session_info(struct dlz_bind9_data *state, const char *name)
1371 if (state->update_name == NULL || state->session_info == NULL) {
1372 state->log(ISC_LOG_ERROR, "samba_dlz: invalid credentials");
1376 /* Do not use client credentials, if we not updating the client specified name */
1377 if (strcmp(state->update_name, name) != 0) {
1381 ret = ldb_set_opaque(state->samdb, "sessionInfo", state->session_info);
1382 if (ret != LDB_SUCCESS) {
1383 state->log(ISC_LOG_ERROR, "samba_dlz: unable to set session info");
1391 * Reset session_info on samdb as system session
1393 static void b9_reset_session_info(struct dlz_bind9_data *state)
1395 ldb_set_opaque(state->samdb, "sessionInfo", system_session(state->lp));
1399 add or modify a rdataset
1401 _PUBLIC_ isc_result_t dlz_addrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1403 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1404 struct dnsp_DnssrvRpcRecord *rec;
1406 isc_result_t result;
1407 struct ldb_result *res;
1408 const char *attrs[] = { "dnsRecord", NULL };
1410 struct ldb_message_element *el;
1411 enum ndr_err_code ndr_err;
1414 if (state->transaction_token != (void*)version) {
1415 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1416 return ISC_R_FAILURE;
1419 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1421 return ISC_R_NOMEMORY;
1424 unix_to_nt_time(&t, time(NULL));
1425 t /= 10*1000*1000; /* convert to seconds (NT time is in 100ns units) */
1426 t /= 3600; /* convert to hours */
1428 rec->rank = DNS_RANK_ZONE;
1429 rec->dwSerial = state->soa_serial;
1430 rec->dwTimeStamp = (uint32_t)t;
1432 if (!b9_parse(state, rdatastr, rec)) {
1433 state->log(ISC_LOG_INFO, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1435 return ISC_R_FAILURE;
1438 /* find the DN of the record */
1439 result = b9_find_name_dn(state, name, rec, &dn);
1440 if (result != ISC_R_SUCCESS) {
1445 /* get any existing records */
1446 ret = ldb_search(state->samdb, rec, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1447 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1448 if (!b9_set_session_info(state, name)) {
1450 return ISC_R_FAILURE;
1452 result = b9_add_record(state, name, dn, rec);
1453 b9_reset_session_info(state);
1455 if (result == ISC_R_SUCCESS) {
1456 state->log(ISC_LOG_INFO, "samba_dlz: added %s %s", name, rdatastr);
1461 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1463 ret = ldb_msg_add_empty(res->msgs[0], "dnsRecord", LDB_FLAG_MOD_ADD, &el);
1464 if (ret != LDB_SUCCESS) {
1465 state->log(ISC_LOG_ERROR, "samba_dlz: failed to add dnsRecord for %s",
1466 ldb_dn_get_linearized(dn));
1468 return ISC_R_FAILURE;
1472 /* there are existing records. We need to see if this will
1473 * replace a record or add to it
1475 for (i=0; i<el->num_values; i++) {
1476 struct dnsp_DnssrvRpcRecord rec2;
1478 ndr_err = ndr_pull_struct_blob(&el->values[i], rec, &rec2,
1479 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1480 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1481 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1482 ldb_dn_get_linearized(dn));
1484 return ISC_R_FAILURE;
1487 if (b9_record_match(state, rec, &rec2)) {
1491 if (i == el->num_values) {
1492 /* adding a new value */
1493 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values+1);
1494 if (el->values == NULL) {
1496 return ISC_R_NOMEMORY;
1501 ndr_err = ndr_push_struct_blob(&el->values[i], rec, rec,
1502 (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
1503 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1504 state->log(ISC_LOG_ERROR, "samba_dlz: failed to push dnsRecord for %s",
1505 ldb_dn_get_linearized(dn));
1507 return ISC_R_FAILURE;
1511 if (!b9_set_session_info(state, name)) {
1513 return ISC_R_FAILURE;
1516 /* modify the record */
1517 el->flags = LDB_FLAG_MOD_REPLACE;
1518 ret = ldb_modify(state->samdb, res->msgs[0]);
1519 b9_reset_session_info(state);
1520 if (ret != LDB_SUCCESS) {
1521 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1522 ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1524 return ISC_R_FAILURE;
1527 state->log(ISC_LOG_INFO, "samba_dlz: added rdataset %s '%s'", name, rdatastr);
1530 return ISC_R_SUCCESS;
1536 _PUBLIC_ isc_result_t dlz_subrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1538 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1539 struct dnsp_DnssrvRpcRecord *rec;
1541 isc_result_t result;
1542 struct ldb_result *res;
1543 const char *attrs[] = { "dnsRecord", NULL };
1545 struct ldb_message_element *el;
1546 enum ndr_err_code ndr_err;
1548 if (state->transaction_token != (void*)version) {
1549 state->log(ISC_LOG_ERROR, "samba_dlz: bad transaction version");
1550 return ISC_R_FAILURE;
1553 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1555 return ISC_R_NOMEMORY;
1558 if (!b9_parse(state, rdatastr, rec)) {
1559 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1561 return ISC_R_FAILURE;
1564 /* find the DN of the record */
1565 result = b9_find_name_dn(state, name, rec, &dn);
1566 if (result != ISC_R_SUCCESS) {
1571 /* get the existing records */
1572 ret = ldb_search(state->samdb, rec, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1573 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1575 return ISC_R_NOTFOUND;
1578 /* there are existing records. We need to see if any match
1580 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1581 if (el == NULL || el->num_values == 0) {
1582 state->log(ISC_LOG_ERROR, "samba_dlz: no dnsRecord attribute for %s",
1583 ldb_dn_get_linearized(dn));
1585 return ISC_R_FAILURE;
1588 for (i=0; i<el->num_values; i++) {
1589 struct dnsp_DnssrvRpcRecord rec2;
1591 ndr_err = ndr_pull_struct_blob(&el->values[i], rec, &rec2,
1592 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1593 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1594 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1595 ldb_dn_get_linearized(dn));
1597 return ISC_R_FAILURE;
1600 if (b9_record_match(state, rec, &rec2)) {
1604 if (i == el->num_values) {
1606 return ISC_R_NOTFOUND;
1609 if (i < el->num_values-1) {
1610 memmove(&el->values[i], &el->values[i+1], sizeof(el->values[0])*((el->num_values-1)-i));
1614 if (!b9_set_session_info(state, name)) {
1616 return ISC_R_FAILURE;
1619 if (el->num_values == 0) {
1620 el->flags = LDB_FLAG_MOD_DELETE;
1622 el->flags = LDB_FLAG_MOD_REPLACE;
1624 ret = ldb_modify(state->samdb, res->msgs[0]);
1626 b9_reset_session_info(state);
1627 if (ret != LDB_SUCCESS) {
1628 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1629 ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1631 return ISC_R_FAILURE;
1634 state->log(ISC_LOG_INFO, "samba_dlz: subtracted rdataset %s '%s'", name, rdatastr);
1637 return ISC_R_SUCCESS;
1642 delete all records of the given type
1644 _PUBLIC_ isc_result_t dlz_delrdataset(const char *name, const char *type, void *dbdata, void *version)
1646 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1647 TALLOC_CTX *tmp_ctx;
1649 isc_result_t result;
1650 struct ldb_result *res;
1651 const char *attrs[] = { "dnsRecord", NULL };
1653 struct ldb_message_element *el;
1654 enum ndr_err_code ndr_err;
1655 enum dns_record_type dns_type;
1658 if (state->transaction_token != (void*)version) {
1659 state->log(ISC_LOG_ERROR, "samba_dlz: bad transaction version");
1660 return ISC_R_FAILURE;
1663 if (!b9_dns_type(type, &dns_type)) {
1664 state->log(ISC_LOG_ERROR, "samba_dlz: bad dns type %s in delete", type);
1665 return ISC_R_FAILURE;
1668 tmp_ctx = talloc_new(state);
1670 /* find the DN of the record */
1671 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1672 if (result != ISC_R_SUCCESS) {
1673 talloc_free(tmp_ctx);
1677 /* get the existing records */
1678 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1679 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1680 talloc_free(tmp_ctx);
1681 return ISC_R_NOTFOUND;
1684 /* there are existing records. We need to see if any match the type
1686 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1687 if (el == NULL || el->num_values == 0) {
1688 talloc_free(tmp_ctx);
1689 return ISC_R_NOTFOUND;
1692 for (i=0; i<el->num_values; i++) {
1693 struct dnsp_DnssrvRpcRecord rec2;
1695 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec2,
1696 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1697 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1698 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1699 ldb_dn_get_linearized(dn));
1700 talloc_free(tmp_ctx);
1701 return ISC_R_FAILURE;
1704 if (dns_type == rec2.wType) {
1705 if (i < el->num_values-1) {
1706 memmove(&el->values[i], &el->values[i+1],
1707 sizeof(el->values[0])*((el->num_values-1)-i));
1716 talloc_free(tmp_ctx);
1717 return ISC_R_FAILURE;
1720 if (!b9_set_session_info(state, name)) {
1721 talloc_free(tmp_ctx);
1722 return ISC_R_FAILURE;
1725 if (el->num_values == 0) {
1726 el->flags = LDB_FLAG_MOD_DELETE;
1728 el->flags = LDB_FLAG_MOD_REPLACE;
1730 ret = ldb_modify(state->samdb, res->msgs[0]);
1732 b9_reset_session_info(state);
1733 if (ret != LDB_SUCCESS) {
1734 state->log(ISC_LOG_ERROR, "samba_dlz: failed to delete type %s in %s - %s",
1735 type, ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1736 talloc_free(tmp_ctx);
1737 return ISC_R_FAILURE;
1740 state->log(ISC_LOG_INFO, "samba_dlz: deleted rdataset %s of type %s", name, type);
1742 talloc_free(tmp_ctx);
1743 return ISC_R_SUCCESS;