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 "lib/util/dlinklist.h"
40 #include "dlz_minimal.h"
41 #include "dns_server/dnsserver_common.h"
50 struct b9_zone *prev, *next;
53 struct dlz_bind9_data {
54 struct b9_options options;
55 struct ldb_context *samdb;
56 struct tevent_context *ev_ctx;
57 struct loadparm_context *lp;
58 int *transaction_token;
60 struct b9_zone *zonelist;
62 /* Used for dynamic update */
63 struct smb_krb5_context *smb_krb5_ctx;
64 struct auth4_context *auth_context;
65 struct auth_session_info *session_info;
68 /* helper functions from the dlz_dlopen driver */
70 dns_sdlz_putrr_t *putrr;
71 dns_sdlz_putnamedrr_t *putnamedrr;
72 dns_dlz_writeablezone_t *writeable_zone;
75 static struct dlz_bind9_data *dlz_bind9_state = NULL;
76 static int dlz_bind9_state_ref_count = 0;
78 static const char *zone_prefixes[] = {
79 "CN=MicrosoftDNS,DC=DomainDnsZones",
80 "CN=MicrosoftDNS,DC=ForestDnsZones",
81 "CN=MicrosoftDNS,CN=System",
86 return the version of the API
88 _PUBLIC_ int dlz_version(unsigned int *flags)
90 return DLZ_DLOPEN_VERSION;
94 remember a helper function from the bind9 dlz_dlopen driver
96 static void b9_add_helper(struct dlz_bind9_data *state, const char *helper_name, void *ptr)
98 if (strcmp(helper_name, "log") == 0) {
101 if (strcmp(helper_name, "putrr") == 0) {
104 if (strcmp(helper_name, "putnamedrr") == 0) {
105 state->putnamedrr = ptr;
107 if (strcmp(helper_name, "writeable_zone") == 0) {
108 state->writeable_zone = ptr;
113 format a record for bind9
115 static bool b9_format(struct dlz_bind9_data *state,
117 struct dnsp_DnssrvRpcRecord *rec,
118 const char **type, const char **data)
123 switch (rec->wType) {
126 *data = rec->data.ipv4;
131 *data = rec->data.ipv6;
136 *data = rec->data.cname;
141 tmp = talloc_asprintf(mem_ctx, "\"%s\"", rec->data.txt.str[0]);
142 for (i=1; i<rec->data.txt.count; i++) {
143 tmp = talloc_asprintf_append(tmp, " \"%s\"", rec->data.txt.str[i]);
150 *data = rec->data.ptr;
155 *data = talloc_asprintf(mem_ctx, "%u %u %u %s",
156 rec->data.srv.wPriority,
157 rec->data.srv.wWeight,
159 rec->data.srv.nameTarget);
164 *data = talloc_asprintf(mem_ctx, "%u %s",
165 rec->data.mx.wPriority,
166 rec->data.mx.nameTarget);
171 *data = talloc_asprintf(mem_ctx, "%s %s",
178 *data = rec->data.ns;
185 /* we need to fake the authoritative nameserver to
186 * point at ourselves. This is how AD DNS servers
187 * force clients to send updates to the right local DC
189 mname = talloc_asprintf(mem_ctx, "%s.%s",
190 lpcfg_netbios_name(state->lp), lpcfg_dnsdomain(state->lp));
194 mname = strlower_talloc(mem_ctx, mname);
199 state->soa_serial = rec->data.soa.serial;
201 *data = talloc_asprintf(mem_ctx, "%s %s %u %u %u %u %u",
204 rec->data.soa.serial,
205 rec->data.soa.refresh,
207 rec->data.soa.expire,
208 rec->data.soa.minimum);
213 state->log(ISC_LOG_ERROR, "samba_dlz b9_format: unhandled record type %u",
221 static const struct {
222 enum dns_record_type dns_type;
226 { DNS_TYPE_A, "A" , false},
227 { DNS_TYPE_AAAA, "AAAA" , false},
228 { DNS_TYPE_CNAME, "CNAME" , true},
229 { DNS_TYPE_TXT, "TXT" , false},
230 { DNS_TYPE_PTR, "PTR" , false},
231 { DNS_TYPE_SRV, "SRV" , false},
232 { DNS_TYPE_MX, "MX" , false},
233 { DNS_TYPE_HINFO, "HINFO" , false},
234 { DNS_TYPE_NS, "NS" , false},
235 { DNS_TYPE_SOA, "SOA" , true},
240 see if a DNS type is single valued
242 static bool b9_single_valued(enum dns_record_type dns_type)
245 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
246 if (dns_typemap[i].dns_type == dns_type) {
247 return dns_typemap[i].single_valued;
254 see if a DNS type is single valued
256 static bool b9_dns_type(const char *type, enum dns_record_type *dtype)
259 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
260 if (strcasecmp(dns_typemap[i].typestr, type) == 0) {
261 *dtype = dns_typemap[i].dns_type;
269 #define DNS_PARSE_STR(ret, str, sep, saveptr) do { \
270 (ret) = strtok_r(str, sep, &saveptr); \
271 if ((ret) == NULL) return false; \
274 #define DNS_PARSE_UINT(ret, str, sep, saveptr) do { \
275 char *istr = strtok_r(str, sep, &saveptr); \
276 if ((istr) == NULL) return false; \
277 (ret) = strtoul(istr, NULL, 10); \
281 parse a record from bind9
283 static bool b9_parse(struct dlz_bind9_data *state,
284 const char *rdatastr,
285 struct dnsp_DnssrvRpcRecord *rec)
287 char *full_name, *dclass, *type;
288 char *str, *tmp, *saveptr=NULL;
291 str = talloc_strdup(rec, rdatastr);
296 /* parse the SDLZ string form */
297 DNS_PARSE_STR(full_name, str, "\t", saveptr);
298 DNS_PARSE_UINT(rec->dwTtlSeconds, NULL, "\t", saveptr);
299 DNS_PARSE_STR(dclass, NULL, "\t", saveptr);
300 DNS_PARSE_STR(type, NULL, "\t", saveptr);
302 /* construct the record */
303 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
304 if (strcasecmp(type, dns_typemap[i].typestr) == 0) {
305 rec->wType = dns_typemap[i].dns_type;
309 if (i == ARRAY_SIZE(dns_typemap)) {
310 state->log(ISC_LOG_ERROR, "samba_dlz: unsupported record type '%s' for '%s'",
315 switch (rec->wType) {
317 DNS_PARSE_STR(rec->data.ipv4, NULL, " ", saveptr);
321 DNS_PARSE_STR(rec->data.ipv6, NULL, " ", saveptr);
325 DNS_PARSE_STR(rec->data.cname, NULL, " ", saveptr);
329 rec->data.txt.count = 0;
330 rec->data.txt.str = talloc_array(rec, const char *, rec->data.txt.count);
331 tmp = strtok_r(NULL, "\t", &saveptr);
333 rec->data.txt.str = talloc_realloc(rec, rec->data.txt.str, const char *,
334 rec->data.txt.count+1);
337 rec->data.txt.str[rec->data.txt.count] = talloc_strndup(rec, &tmp[1], strlen(tmp)-2);
339 rec->data.txt.str[rec->data.txt.count] = talloc_strdup(rec, tmp);
341 rec->data.txt.count++;
342 tmp = strtok_r(NULL, " ", &saveptr);
347 DNS_PARSE_STR(rec->data.ptr, NULL, " ", saveptr);
351 DNS_PARSE_UINT(rec->data.srv.wPriority, NULL, " ", saveptr);
352 DNS_PARSE_UINT(rec->data.srv.wWeight, NULL, " ", saveptr);
353 DNS_PARSE_UINT(rec->data.srv.wPort, NULL, " ", saveptr);
354 DNS_PARSE_STR(rec->data.srv.nameTarget, NULL, " ", saveptr);
358 DNS_PARSE_UINT(rec->data.mx.wPriority, NULL, " ", saveptr);
359 DNS_PARSE_STR(rec->data.mx.nameTarget, NULL, " ", saveptr);
363 DNS_PARSE_STR(rec->data.hinfo.cpu, NULL, " ", saveptr);
364 DNS_PARSE_STR(rec->data.hinfo.os, NULL, " ", saveptr);
368 DNS_PARSE_STR(rec->data.ns, NULL, " ", saveptr);
372 DNS_PARSE_STR(rec->data.soa.mname, NULL, " ", saveptr);
373 DNS_PARSE_STR(rec->data.soa.rname, NULL, " ", saveptr);
374 DNS_PARSE_UINT(rec->data.soa.serial, NULL, " ", saveptr);
375 DNS_PARSE_UINT(rec->data.soa.refresh, NULL, " ", saveptr);
376 DNS_PARSE_UINT(rec->data.soa.retry, NULL, " ", saveptr);
377 DNS_PARSE_UINT(rec->data.soa.expire, NULL, " ", saveptr);
378 DNS_PARSE_UINT(rec->data.soa.minimum, NULL, " ", saveptr);
382 state->log(ISC_LOG_ERROR, "samba_dlz b9_parse: unhandled record type %u",
387 /* we should be at the end of the buffer now */
388 if (strtok_r(NULL, "\t ", &saveptr) != NULL) {
389 state->log(ISC_LOG_ERROR, "samba_dlz b9_parse: unexpected data at end of string for '%s'",
398 send a resource record to bind9
400 static isc_result_t b9_putrr(struct dlz_bind9_data *state,
401 void *handle, struct dnsp_DnssrvRpcRecord *rec,
405 const char *type, *data;
406 TALLOC_CTX *tmp_ctx = talloc_new(state);
408 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
409 return ISC_R_FAILURE;
413 talloc_free(tmp_ctx);
414 return ISC_R_NOMEMORY;
419 for (i=0; types[i]; i++) {
420 if (strcmp(types[i], type) == 0) break;
422 if (types[i] == NULL) {
424 return ISC_R_SUCCESS;
428 result = state->putrr(handle, type, rec->dwTtlSeconds, data);
429 if (result != ISC_R_SUCCESS) {
430 state->log(ISC_LOG_ERROR, "Failed to put rr");
432 talloc_free(tmp_ctx);
438 send a named resource record to bind9
440 static isc_result_t b9_putnamedrr(struct dlz_bind9_data *state,
441 void *handle, const char *name,
442 struct dnsp_DnssrvRpcRecord *rec)
445 const char *type, *data;
446 TALLOC_CTX *tmp_ctx = talloc_new(state);
448 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
449 return ISC_R_FAILURE;
453 talloc_free(tmp_ctx);
454 return ISC_R_NOMEMORY;
457 result = state->putnamedrr(handle, name, type, rec->dwTtlSeconds, data);
458 if (result != ISC_R_SUCCESS) {
459 state->log(ISC_LOG_ERROR, "Failed to put named rr '%s'", name);
461 talloc_free(tmp_ctx);
468 static isc_result_t parse_options(struct dlz_bind9_data *state,
469 unsigned int argc, const char **argv,
470 struct b9_options *options)
474 struct poptOption long_options[] = {
475 { "url", 'H', POPT_ARG_STRING, &options->url, 0, "database URL", "URL" },
476 { "debug", 'd', POPT_ARG_STRING, &options->debug, 0, "debug level", "DEBUG" },
480 pc = poptGetContext("dlz_bind9", argc, argv, long_options,
481 POPT_CONTEXT_KEEP_FIRST);
482 while ((opt = poptGetNextOpt(pc)) != -1) {
485 state->log(ISC_LOG_ERROR, "dlz_bind9: Invalid option %s: %s",
486 poptBadOption(pc, 0), poptStrerror(opt));
487 return ISC_R_FAILURE;
491 return ISC_R_SUCCESS;
496 * Create session info from PAC
497 * This is called as auth_context->generate_session_info_pac()
499 static NTSTATUS b9_generate_session_info_pac(struct auth4_context *auth_context,
501 struct smb_krb5_context *smb_krb5_context,
503 const char *principal_name,
504 const struct tsocket_address *remote_addr,
505 uint32_t session_info_flags,
506 struct auth_session_info **session_info)
509 struct auth_user_info_dc *user_info_dc;
512 tmp_ctx = talloc_new(mem_ctx);
513 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
515 status = kerberos_pac_blob_to_user_info_dc(tmp_ctx,
517 smb_krb5_context->krb5_context,
521 if (!NT_STATUS_IS_OK(status)) {
522 talloc_free(tmp_ctx);
526 if (user_info_dc->info->authenticated) {
527 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
530 session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
532 status = auth_generate_session_info(mem_ctx, NULL, NULL, user_info_dc,
533 session_info_flags, session_info);
534 if (!NT_STATUS_IS_OK(status)) {
535 talloc_free(tmp_ctx);
539 talloc_free(tmp_ctx);
543 /* Callback for the DEBUG() system, to catch the remaining messages */
544 static void b9_debug(void *private_ptr, int msg_level, const char *msg)
546 static const int isc_log_map[] = {
547 ISC_LOG_CRITICAL, /* 0 */
548 ISC_LOG_ERROR, /* 1 */
549 ISC_LOG_WARNING, /* 2 */
550 ISC_LOG_NOTICE /* 3 */
552 struct dlz_bind9_data *state = private_ptr;
555 if (msg_level >= ARRAY_SIZE(isc_log_map) || msg_level < 0) {
556 isc_log_level = ISC_LOG_INFO;
558 isc_log_level = isc_log_map[msg_level];
560 state->log(isc_log_level, "samba_dlz: %s", msg);
563 static int dlz_state_debug_unregister(struct dlz_bind9_data *state)
565 /* Stop logging (to the bind9 logs) */
566 debug_set_callback(NULL, NULL);
571 called to initialise the driver
573 _PUBLIC_ isc_result_t dlz_create(const char *dlzname,
574 unsigned int argc, const char **argv,
577 struct dlz_bind9_data *state;
578 const char *helper_name;
584 if (dlz_bind9_state != NULL) {
585 *dbdata = dlz_bind9_state;
586 dlz_bind9_state_ref_count++;
587 return ISC_R_SUCCESS;
590 state = talloc_zero(NULL, struct dlz_bind9_data);
592 return ISC_R_NOMEMORY;
595 talloc_set_destructor(state, dlz_state_debug_unregister);
597 /* fill in the helper functions */
598 va_start(ap, dbdata);
599 while ((helper_name = va_arg(ap, const char *)) != NULL) {
600 b9_add_helper(state, helper_name, va_arg(ap, void*));
604 /* Do not install samba signal handlers */
605 fault_setup_disable();
607 /* Start logging (to the bind9 logs) */
608 debug_set_callback(state, b9_debug);
610 state->ev_ctx = s4_event_context_init(state);
611 if (state->ev_ctx == NULL) {
612 result = ISC_R_NOMEMORY;
616 result = parse_options(state, argc, argv, &state->options);
617 if (result != ISC_R_SUCCESS) {
621 state->lp = loadparm_init_global(true);
622 if (state->lp == NULL) {
623 result = ISC_R_NOMEMORY;
627 if (state->options.debug) {
628 lpcfg_do_global_parameter(state->lp, "log level", state->options.debug);
630 lpcfg_do_global_parameter(state->lp, "log level", "0");
633 if (smb_krb5_init_context(state, state->lp, &state->smb_krb5_ctx) != 0) {
634 result = ISC_R_NOMEMORY;
638 nt_status = gensec_init();
639 if (!NT_STATUS_IS_OK(nt_status)) {
640 result = ISC_R_NOMEMORY;
644 state->auth_context = talloc_zero(state, struct auth4_context);
645 if (state->auth_context == NULL) {
646 result = ISC_R_NOMEMORY;
650 if (state->options.url == NULL) {
651 state->options.url = lpcfg_private_path(state, state->lp, "dns/sam.ldb");
652 if (state->options.url == NULL) {
653 result = ISC_R_NOMEMORY;
658 state->samdb = samdb_connect_url(state, state->ev_ctx, state->lp,
659 system_session(state->lp), 0, state->options.url);
660 if (state->samdb == NULL) {
661 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to connect to %s",
663 result = ISC_R_FAILURE;
667 dn = ldb_get_default_basedn(state->samdb);
669 state->log(ISC_LOG_ERROR, "samba_dlz: Unable to get basedn for %s - %s",
670 state->options.url, ldb_errstring(state->samdb));
671 result = ISC_R_FAILURE;
675 state->log(ISC_LOG_INFO, "samba_dlz: started for DN %s",
676 ldb_dn_get_linearized(dn));
678 state->auth_context->event_ctx = state->ev_ctx;
679 state->auth_context->lp_ctx = state->lp;
680 state->auth_context->sam_ctx = state->samdb;
681 state->auth_context->generate_session_info_pac = b9_generate_session_info_pac;
684 dlz_bind9_state = state;
685 dlz_bind9_state_ref_count++;
687 return ISC_R_SUCCESS;
697 _PUBLIC_ void dlz_destroy(void *dbdata)
699 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
700 state->log(ISC_LOG_INFO, "samba_dlz: shutting down");
702 dlz_bind9_state_ref_count--;
703 if (dlz_bind9_state_ref_count == 0) {
704 talloc_unlink(state, state->samdb);
706 dlz_bind9_state = NULL;
712 return the base DN for a zone
714 static isc_result_t b9_find_zone_dn(struct dlz_bind9_data *state, const char *zone_name,
715 TALLOC_CTX *mem_ctx, struct ldb_dn **zone_dn)
718 TALLOC_CTX *tmp_ctx = talloc_new(state);
719 const char *attrs[] = { NULL };
722 for (i=0; zone_prefixes[i]; i++) {
724 struct ldb_result *res;
726 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
728 talloc_free(tmp_ctx);
729 return ISC_R_NOMEMORY;
732 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone_name, zone_prefixes[i])) {
733 talloc_free(tmp_ctx);
734 return ISC_R_NOMEMORY;
737 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsZone");
738 if (ret == LDB_SUCCESS) {
739 if (zone_dn != NULL) {
740 *zone_dn = talloc_steal(mem_ctx, dn);
742 talloc_free(tmp_ctx);
743 return ISC_R_SUCCESS;
748 talloc_free(tmp_ctx);
749 return ISC_R_NOTFOUND;
754 return the DN for a name. The record does not need to exist, but the
757 static isc_result_t b9_find_name_dn(struct dlz_bind9_data *state, const char *name,
758 TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
762 /* work through the name piece by piece, until we find a zone */
765 result = b9_find_zone_dn(state, p, mem_ctx, dn);
766 if (result == ISC_R_SUCCESS) {
767 /* we found a zone, now extend the DN to get
772 ret = ldb_dn_add_child_fmt(*dn, "DC=@");
774 ret = ldb_dn_add_child_fmt(*dn, "DC=%.*s", (int)(p-name)-1, name);
778 return ISC_R_NOMEMORY;
780 return ISC_R_SUCCESS;
788 return ISC_R_NOTFOUND;
793 see if we handle a given zone
795 _PUBLIC_ isc_result_t dlz_findzonedb(void *dbdata, const char *name)
797 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
798 return b9_find_zone_dn(state, name, NULL, NULL);
805 static isc_result_t dlz_lookup_types(struct dlz_bind9_data *state,
806 const char *zone, const char *name,
807 dns_sdlzlookup_t *lookup,
810 TALLOC_CTX *tmp_ctx = talloc_new(state);
812 WERROR werr = WERR_DNS_ERROR_NAME_DOES_NOT_EXIST;
813 struct dnsp_DnssrvRpcRecord *records = NULL;
814 uint16_t num_records = 0, i;
816 for (i=0; zone_prefixes[i]; i++) {
817 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
819 talloc_free(tmp_ctx);
820 return ISC_R_NOMEMORY;
823 if (!ldb_dn_add_child_fmt(dn, "DC=%s,DC=%s,%s", name, zone, zone_prefixes[i])) {
824 talloc_free(tmp_ctx);
825 return ISC_R_NOMEMORY;
828 werr = dns_common_lookup(state->samdb, tmp_ctx, dn,
829 &records, &num_records, NULL);
830 if (W_ERROR_IS_OK(werr)) {
834 if (!W_ERROR_IS_OK(werr)) {
835 talloc_free(tmp_ctx);
836 return ISC_R_NOTFOUND;
839 for (i=0; i < num_records; i++) {
842 result = b9_putrr(state, lookup, &records[i], types);
843 if (result != ISC_R_SUCCESS) {
844 talloc_free(tmp_ctx);
849 talloc_free(tmp_ctx);
850 return ISC_R_SUCCESS;
856 #ifdef BIND_VERSION_9_8
857 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name,
858 void *dbdata, dns_sdlzlookup_t *lookup)
860 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name,
861 void *dbdata, dns_sdlzlookup_t *lookup,
862 dns_clientinfomethods_t *methods,
863 dns_clientinfo_t *clientinfo)
866 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
867 return dlz_lookup_types(state, zone, name, lookup, NULL);
872 see if a zone transfer is allowed
874 _PUBLIC_ isc_result_t dlz_allowzonexfr(void *dbdata, const char *name, const char *client)
876 /* just say yes for all our zones for now */
877 return dlz_findzonedb(dbdata, name);
881 perform a zone transfer
883 _PUBLIC_ isc_result_t dlz_allnodes(const char *zone, void *dbdata,
884 dns_sdlzallnodes_t *allnodes)
886 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
887 const char *attrs[] = { "dnsRecord", NULL };
888 int ret = LDB_SUCCESS, i, j;
890 struct ldb_result *res;
891 TALLOC_CTX *tmp_ctx = talloc_new(state);
893 for (i=0; zone_prefixes[i]; i++) {
894 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
896 talloc_free(tmp_ctx);
897 return ISC_R_NOMEMORY;
900 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone, zone_prefixes[i])) {
901 talloc_free(tmp_ctx);
902 return ISC_R_NOMEMORY;
905 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
906 attrs, "objectClass=dnsNode");
907 if (ret == LDB_SUCCESS) {
911 if (ret != LDB_SUCCESS) {
912 talloc_free(tmp_ctx);
913 return ISC_R_NOTFOUND;
916 for (i=0; i<res->count; i++) {
917 struct ldb_message_element *el;
918 TALLOC_CTX *el_ctx = talloc_new(tmp_ctx);
919 const char *rdn, *name;
920 const struct ldb_val *v;
922 el = ldb_msg_find_element(res->msgs[i], "dnsRecord");
923 if (el == NULL || el->num_values == 0) {
924 state->log(ISC_LOG_INFO, "failed to find dnsRecord for %s",
925 ldb_dn_get_linearized(dn));
930 v = ldb_dn_get_rdn_val(res->msgs[i]->dn);
932 state->log(ISC_LOG_INFO, "failed to find RDN for %s",
933 ldb_dn_get_linearized(dn));
938 rdn = talloc_strndup(el_ctx, (char *)v->data, v->length);
940 talloc_free(tmp_ctx);
941 return ISC_R_NOMEMORY;
944 if (strcmp(rdn, "@") == 0) {
947 name = talloc_asprintf(el_ctx, "%s.%s", rdn, zone);
950 talloc_free(tmp_ctx);
951 return ISC_R_NOMEMORY;
954 for (j=0; j<el->num_values; j++) {
955 struct dnsp_DnssrvRpcRecord rec;
956 enum ndr_err_code ndr_err;
959 ndr_err = ndr_pull_struct_blob(&el->values[j], el_ctx, &rec,
960 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
961 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
962 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
963 ldb_dn_get_linearized(dn));
967 result = b9_putnamedrr(state, allnodes, name, &rec);
968 if (result != ISC_R_SUCCESS) {
976 talloc_free(tmp_ctx);
978 return ISC_R_SUCCESS;
985 _PUBLIC_ isc_result_t dlz_newversion(const char *zone, void *dbdata, void **versionp)
987 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
989 state->log(ISC_LOG_INFO, "samba_dlz: starting transaction on zone %s", zone);
991 if (state->transaction_token != NULL) {
992 state->log(ISC_LOG_INFO, "samba_dlz: transaction already started for zone %s", zone);
993 return ISC_R_FAILURE;
996 state->transaction_token = talloc_zero(state, int);
997 if (state->transaction_token == NULL) {
998 return ISC_R_NOMEMORY;
1001 if (ldb_transaction_start(state->samdb) != LDB_SUCCESS) {
1002 state->log(ISC_LOG_INFO, "samba_dlz: failed to start a transaction for zone %s", zone);
1003 talloc_free(state->transaction_token);
1004 state->transaction_token = NULL;
1005 return ISC_R_FAILURE;
1008 *versionp = (void *)state->transaction_token;
1010 return ISC_R_SUCCESS;
1016 _PUBLIC_ void dlz_closeversion(const char *zone, isc_boolean_t commit,
1017 void *dbdata, void **versionp)
1019 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1021 if (state->transaction_token != (int *)*versionp) {
1022 state->log(ISC_LOG_INFO, "samba_dlz: transaction not started for zone %s", zone);
1027 if (ldb_transaction_commit(state->samdb) != LDB_SUCCESS) {
1028 state->log(ISC_LOG_INFO, "samba_dlz: failed to commit a transaction for zone %s", zone);
1031 state->log(ISC_LOG_INFO, "samba_dlz: committed transaction on zone %s", zone);
1033 if (ldb_transaction_cancel(state->samdb) != LDB_SUCCESS) {
1034 state->log(ISC_LOG_INFO, "samba_dlz: failed to cancel a transaction for zone %s", zone);
1037 state->log(ISC_LOG_INFO, "samba_dlz: cancelling transaction on zone %s", zone);
1040 talloc_free(state->transaction_token);
1041 state->transaction_token = NULL;
1047 see if there is a SOA record for a zone
1049 static bool b9_has_soa(struct dlz_bind9_data *state, struct ldb_dn *dn, const char *zone)
1051 TALLOC_CTX *tmp_ctx = talloc_new(state);
1053 struct dnsp_DnssrvRpcRecord *records = NULL;
1054 uint16_t num_records = 0, i;
1056 if (!ldb_dn_add_child_fmt(dn, "DC=@,DC=%s", zone)) {
1057 talloc_free(tmp_ctx);
1061 werr = dns_common_lookup(state->samdb, tmp_ctx, dn,
1062 &records, &num_records, NULL);
1063 if (!W_ERROR_IS_OK(werr)) {
1064 talloc_free(tmp_ctx);
1068 for (i=0; i < num_records; i++) {
1069 if (records[i].wType == DNS_TYPE_SOA) {
1070 talloc_free(tmp_ctx);
1075 talloc_free(tmp_ctx);
1079 static bool b9_zone_add(struct dlz_bind9_data *state, const char *name)
1081 struct b9_zone *zone;
1083 zone = talloc_zero(state, struct b9_zone);
1088 zone->name = talloc_strdup(zone, name);
1089 if (zone->name == NULL) {
1094 DLIST_ADD(state->zonelist, zone);
1098 static bool b9_zone_exists(struct dlz_bind9_data *state, const char *name)
1100 struct b9_zone *zone = state->zonelist;
1103 while (zone != NULL) {
1104 if (strcasecmp(name, zone->name) == 0) {
1116 configure a writeable zone
1118 _PUBLIC_ isc_result_t dlz_configure(dns_view_t *view, void *dbdata)
1120 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1121 TALLOC_CTX *tmp_ctx;
1125 state->log(ISC_LOG_INFO, "samba_dlz: starting configure");
1126 if (state->writeable_zone == NULL) {
1127 state->log(ISC_LOG_INFO, "samba_dlz: no writeable_zone method available");
1128 return ISC_R_FAILURE;
1131 tmp_ctx = talloc_new(state);
1133 for (i=0; zone_prefixes[i]; i++) {
1134 const char *attrs[] = { "name", NULL };
1136 struct ldb_result *res;
1138 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
1140 talloc_free(tmp_ctx);
1141 return ISC_R_NOMEMORY;
1144 if (!ldb_dn_add_child_fmt(dn, "%s", zone_prefixes[i])) {
1145 talloc_free(tmp_ctx);
1146 return ISC_R_NOMEMORY;
1149 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
1150 attrs, "objectClass=dnsZone");
1151 if (ret != LDB_SUCCESS) {
1155 for (j=0; j<res->count; j++) {
1156 isc_result_t result;
1157 const char *zone = ldb_msg_find_attr_as_string(res->msgs[j], "name", NULL);
1158 struct ldb_dn *zone_dn;
1163 /* Ignore zones that are not handled in BIND */
1164 if ((strcmp(zone, "RootDNSServers") == 0) ||
1165 (strcmp(zone, "..TrustAnchors") == 0)) {
1168 zone_dn = ldb_dn_copy(tmp_ctx, dn);
1169 if (zone_dn == NULL) {
1170 talloc_free(tmp_ctx);
1171 return ISC_R_NOMEMORY;
1174 if (!b9_has_soa(state, zone_dn, zone)) {
1178 if (b9_zone_exists(state, zone)) {
1179 state->log(ISC_LOG_WARNING, "samba_dlz: Ignoring duplicate zone '%s' from '%s'",
1180 zone, ldb_dn_get_linearized(zone_dn));
1184 if (!b9_zone_add(state, zone)) {
1185 talloc_free(tmp_ctx);
1186 return ISC_R_NOMEMORY;
1189 result = state->writeable_zone(view, zone);
1190 if (result != ISC_R_SUCCESS) {
1191 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to configure zone '%s'",
1193 talloc_free(tmp_ctx);
1196 state->log(ISC_LOG_INFO, "samba_dlz: configured writeable zone '%s'", zone);
1200 talloc_free(tmp_ctx);
1201 return ISC_R_SUCCESS;
1205 authorize a zone update
1207 _PUBLIC_ isc_boolean_t dlz_ssumatch(const char *signer, const char *name, const char *tcpaddr,
1208 const char *type, const char *key, uint32_t keydatalen, uint8_t *keydata,
1211 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1212 TALLOC_CTX *tmp_ctx;
1214 struct cli_credentials *server_credentials;
1219 struct gensec_security *gensec_ctx;
1220 struct auth_session_info *session_info;
1222 isc_result_t result;
1223 struct ldb_result *res;
1224 const char * attrs[] = { NULL };
1225 uint32_t access_mask;
1227 /* Remove cached credentials, if any */
1228 if (state->session_info) {
1229 talloc_free(state->session_info);
1230 state->session_info = NULL;
1232 if (state->update_name) {
1233 talloc_free(state->update_name);
1234 state->update_name = NULL;
1237 tmp_ctx = talloc_new(NULL);
1238 if (tmp_ctx == NULL) {
1239 state->log(ISC_LOG_ERROR, "samba_dlz: no memory");
1243 ap_req = data_blob_const(keydata, keydatalen);
1244 server_credentials = cli_credentials_init(tmp_ctx);
1245 if (!server_credentials) {
1246 state->log(ISC_LOG_ERROR, "samba_dlz: failed to init server credentials");
1247 talloc_free(tmp_ctx);
1251 cli_credentials_set_krb5_context(server_credentials, state->smb_krb5_ctx);
1252 cli_credentials_set_conf(server_credentials, state->lp);
1254 keytab_name = talloc_asprintf(tmp_ctx, "file:%s/dns.keytab",
1255 lpcfg_private_dir(state->lp));
1256 ret = cli_credentials_set_keytab_name(server_credentials, state->lp, keytab_name,
1259 state->log(ISC_LOG_ERROR, "samba_dlz: failed to obtain server credentials from %s",
1261 talloc_free(tmp_ctx);
1264 talloc_free(keytab_name);
1266 nt_status = gensec_server_start(tmp_ctx,
1267 lpcfg_gensec_settings(tmp_ctx, state->lp),
1268 state->auth_context, &gensec_ctx);
1269 if (!NT_STATUS_IS_OK(nt_status)) {
1270 state->log(ISC_LOG_ERROR, "samba_dlz: failed to start gensec server");
1271 talloc_free(tmp_ctx);
1275 gensec_set_credentials(gensec_ctx, server_credentials);
1277 nt_status = gensec_start_mech_by_name(gensec_ctx, "spnego");
1278 if (!NT_STATUS_IS_OK(nt_status)) {
1279 state->log(ISC_LOG_ERROR, "samba_dlz: failed to start spnego");
1280 talloc_free(tmp_ctx);
1284 nt_status = gensec_update_ev(gensec_ctx, tmp_ctx, state->ev_ctx, ap_req, &ap_req);
1285 if (!NT_STATUS_IS_OK(nt_status)) {
1286 state->log(ISC_LOG_ERROR, "samba_dlz: spnego update failed");
1287 talloc_free(tmp_ctx);
1291 nt_status = gensec_session_info(gensec_ctx, tmp_ctx, &session_info);
1292 if (!NT_STATUS_IS_OK(nt_status)) {
1293 state->log(ISC_LOG_ERROR, "samba_dlz: failed to create session info");
1294 talloc_free(tmp_ctx);
1298 /* Get the DN from name */
1299 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1300 if (result != ISC_R_SUCCESS) {
1301 state->log(ISC_LOG_ERROR, "samba_dlz: failed to find name %s", name);
1302 talloc_free(tmp_ctx);
1306 /* make sure the dn exists, or find parent dn in case new object is being added */
1307 ldb_ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
1308 attrs, "objectClass=dnsNode");
1309 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
1310 ldb_dn_remove_child_components(dn, 1);
1311 access_mask = SEC_ADS_CREATE_CHILD;
1313 } else if (ldb_ret == LDB_SUCCESS) {
1314 access_mask = SEC_STD_REQUIRED | SEC_ADS_SELF_WRITE;
1317 talloc_free(tmp_ctx);
1322 ldb_ret = dsdb_check_access_on_dn(state->samdb, tmp_ctx, dn,
1323 session_info->security_token,
1325 if (ldb_ret != LDB_SUCCESS) {
1326 state->log(ISC_LOG_INFO,
1327 "samba_dlz: disallowing update of signer=%s name=%s type=%s error=%s",
1328 signer, name, type, ldb_strerror(ldb_ret));
1329 talloc_free(tmp_ctx);
1333 /* Cache session_info, so it can be used in the actual add/delete operation */
1334 state->update_name = talloc_strdup(state, name);
1335 if (state->update_name == NULL) {
1336 state->log(ISC_LOG_ERROR, "samba_dlz: memory allocation error");
1337 talloc_free(tmp_ctx);
1340 state->session_info = talloc_steal(state, session_info);
1342 state->log(ISC_LOG_INFO, "samba_dlz: allowing update of signer=%s name=%s tcpaddr=%s type=%s key=%s",
1343 signer, name, tcpaddr, type, key);
1345 talloc_free(tmp_ctx);
1353 static isc_result_t b9_add_record(struct dlz_bind9_data *state, const char *name,
1355 struct dnsp_DnssrvRpcRecord *rec)
1357 struct ldb_message *msg;
1358 enum ndr_err_code ndr_err;
1362 msg = ldb_msg_new(rec);
1364 return ISC_R_NOMEMORY;
1367 ret = ldb_msg_add_string(msg, "objectClass", "dnsNode");
1368 if (ret != LDB_SUCCESS) {
1369 return ISC_R_FAILURE;
1372 ndr_err = ndr_push_struct_blob(&v, rec, rec, (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
1373 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1374 return ISC_R_FAILURE;
1376 ret = ldb_msg_add_value(msg, "dnsRecord", &v, NULL);
1377 if (ret != LDB_SUCCESS) {
1378 return ISC_R_FAILURE;
1381 ret = ldb_add(state->samdb, msg);
1382 if (ret != LDB_SUCCESS) {
1383 return ISC_R_FAILURE;
1386 return ISC_R_SUCCESS;
1390 see if two DNS names are the same
1392 static bool dns_name_equal(const char *name1, const char *name2)
1394 size_t len1 = strlen(name1);
1395 size_t len2 = strlen(name2);
1396 if (name1[len1-1] == '.') len1--;
1397 if (name2[len2-1] == '.') len2--;
1401 return strncasecmp_m(name1, name2, len1) == 0;
1406 see if two dns records match
1408 static bool b9_record_match(struct dlz_bind9_data *state,
1409 struct dnsp_DnssrvRpcRecord *rec1, struct dnsp_DnssrvRpcRecord *rec2)
1413 struct in6_addr rec1_in_addr6;
1414 struct in6_addr rec2_in_addr6;
1416 if (rec1->wType != rec2->wType) {
1419 /* see if this type is single valued */
1420 if (b9_single_valued(rec1->wType)) {
1424 /* see if the data matches */
1425 switch (rec1->wType) {
1427 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
1429 inet_pton(AF_INET6, rec1->data.ipv6, &rec1_in_addr6);
1430 inet_pton(AF_INET6, rec2->data.ipv6, &rec2_in_addr6);
1431 return memcmp(&rec1_in_addr6, &rec2_in_addr6, sizeof(rec1_in_addr6)) == 0;
1432 case DNS_TYPE_CNAME:
1433 return dns_name_equal(rec1->data.cname, rec2->data.cname);
1435 status = (rec1->data.txt.count == rec2->data.txt.count);
1436 if (!status) return status;
1437 for (i=0; i<rec1->data.txt.count; i++) {
1438 status &= (strcmp(rec1->data.txt.str[i], rec2->data.txt.str[i]) == 0);
1442 return dns_name_equal(rec1->data.ptr, rec2->data.ptr);
1444 return dns_name_equal(rec1->data.ns, rec2->data.ns);
1447 return rec1->data.srv.wPriority == rec2->data.srv.wPriority &&
1448 rec1->data.srv.wWeight == rec2->data.srv.wWeight &&
1449 rec1->data.srv.wPort == rec2->data.srv.wPort &&
1450 dns_name_equal(rec1->data.srv.nameTarget, rec2->data.srv.nameTarget);
1453 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
1454 dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
1456 case DNS_TYPE_HINFO:
1457 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
1458 strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
1461 return dns_name_equal(rec1->data.soa.mname, rec2->data.soa.mname) &&
1462 dns_name_equal(rec1->data.soa.rname, rec2->data.soa.rname) &&
1463 rec1->data.soa.serial == rec2->data.soa.serial &&
1464 rec1->data.soa.refresh == rec2->data.soa.refresh &&
1465 rec1->data.soa.retry == rec2->data.soa.retry &&
1466 rec1->data.soa.expire == rec2->data.soa.expire &&
1467 rec1->data.soa.minimum == rec2->data.soa.minimum;
1469 state->log(ISC_LOG_ERROR, "samba_dlz b9_record_match: unhandled record type %u",
1478 * Update session_info on samdb using the cached credentials
1480 static bool b9_set_session_info(struct dlz_bind9_data *state, const char *name)
1484 if (state->update_name == NULL || state->session_info == NULL) {
1485 state->log(ISC_LOG_ERROR, "samba_dlz: invalid credentials");
1489 /* Do not use client credentials, if we're not updating the client specified name */
1490 if (strcmp(state->update_name, name) != 0) {
1494 ret = ldb_set_opaque(state->samdb, "sessionInfo", state->session_info);
1495 if (ret != LDB_SUCCESS) {
1496 state->log(ISC_LOG_ERROR, "samba_dlz: unable to set session info");
1504 * Reset session_info on samdb as system session
1506 static void b9_reset_session_info(struct dlz_bind9_data *state)
1508 ldb_set_opaque(state->samdb, "sessionInfo", system_session(state->lp));
1512 add or modify a rdataset
1514 _PUBLIC_ isc_result_t dlz_addrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1516 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1517 struct dnsp_DnssrvRpcRecord *rec;
1519 isc_result_t result;
1520 struct ldb_result *res;
1521 const char *attrs[] = { "dnsRecord", NULL };
1523 struct ldb_message_element *el;
1524 enum ndr_err_code ndr_err;
1527 if (state->transaction_token != (void*)version) {
1528 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1529 return ISC_R_FAILURE;
1532 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1534 return ISC_R_NOMEMORY;
1537 unix_to_nt_time(&t, time(NULL));
1538 t /= 10*1000*1000; /* convert to seconds (NT time is in 100ns units) */
1539 t /= 3600; /* convert to hours */
1541 rec->rank = DNS_RANK_ZONE;
1542 rec->dwSerial = state->soa_serial;
1543 rec->dwTimeStamp = (uint32_t)t;
1545 if (!b9_parse(state, rdatastr, rec)) {
1546 state->log(ISC_LOG_INFO, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1548 return ISC_R_FAILURE;
1551 /* find the DN of the record */
1552 result = b9_find_name_dn(state, name, rec, &dn);
1553 if (result != ISC_R_SUCCESS) {
1558 /* get any existing records */
1559 ret = ldb_search(state->samdb, rec, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1560 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1561 if (!b9_set_session_info(state, name)) {
1563 return ISC_R_FAILURE;
1565 result = b9_add_record(state, name, dn, rec);
1566 b9_reset_session_info(state);
1568 if (result == ISC_R_SUCCESS) {
1569 state->log(ISC_LOG_INFO, "samba_dlz: added %s %s", name, rdatastr);
1574 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1576 ret = ldb_msg_add_empty(res->msgs[0], "dnsRecord", LDB_FLAG_MOD_ADD, &el);
1577 if (ret != LDB_SUCCESS) {
1578 state->log(ISC_LOG_ERROR, "samba_dlz: failed to add dnsRecord for %s",
1579 ldb_dn_get_linearized(dn));
1581 return ISC_R_FAILURE;
1585 /* there are existing records. We need to see if this will
1586 * replace a record or add to it
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) {
1605 /* adding a new value */
1606 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values+1);
1607 if (el->values == NULL) {
1609 return ISC_R_NOMEMORY;
1614 ndr_err = ndr_push_struct_blob(&el->values[i], rec, rec,
1615 (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
1616 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1617 state->log(ISC_LOG_ERROR, "samba_dlz: failed to push dnsRecord for %s",
1618 ldb_dn_get_linearized(dn));
1620 return ISC_R_FAILURE;
1624 if (!b9_set_session_info(state, name)) {
1626 return ISC_R_FAILURE;
1629 /* modify the record */
1630 el->flags = LDB_FLAG_MOD_REPLACE;
1631 ret = ldb_modify(state->samdb, res->msgs[0]);
1632 b9_reset_session_info(state);
1633 if (ret != LDB_SUCCESS) {
1634 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1635 ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1637 return ISC_R_FAILURE;
1640 state->log(ISC_LOG_INFO, "samba_dlz: added rdataset %s '%s'", name, rdatastr);
1643 return ISC_R_SUCCESS;
1649 _PUBLIC_ isc_result_t dlz_subrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1651 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1652 struct dnsp_DnssrvRpcRecord *rec;
1654 isc_result_t result;
1655 struct ldb_result *res;
1656 const char *attrs[] = { "dnsRecord", NULL };
1658 struct ldb_message_element *el;
1659 enum ndr_err_code ndr_err;
1661 if (state->transaction_token != (void*)version) {
1662 state->log(ISC_LOG_ERROR, "samba_dlz: bad transaction version");
1663 return ISC_R_FAILURE;
1666 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1668 return ISC_R_NOMEMORY;
1671 if (!b9_parse(state, rdatastr, rec)) {
1672 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1674 return ISC_R_FAILURE;
1677 /* find the DN of the record */
1678 result = b9_find_name_dn(state, name, rec, &dn);
1679 if (result != ISC_R_SUCCESS) {
1684 /* get the existing records */
1685 ret = ldb_search(state->samdb, rec, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1686 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1688 return ISC_R_NOTFOUND;
1691 /* there are existing records. We need to see if any match
1693 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1694 if (el == NULL || el->num_values == 0) {
1695 state->log(ISC_LOG_ERROR, "samba_dlz: no dnsRecord attribute for %s",
1696 ldb_dn_get_linearized(dn));
1698 return ISC_R_FAILURE;
1701 for (i=0; i<el->num_values; i++) {
1702 struct dnsp_DnssrvRpcRecord rec2;
1704 ndr_err = ndr_pull_struct_blob(&el->values[i], rec, &rec2,
1705 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1706 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1707 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1708 ldb_dn_get_linearized(dn));
1710 return ISC_R_FAILURE;
1713 if (b9_record_match(state, rec, &rec2)) {
1717 if (i == el->num_values) {
1719 return ISC_R_NOTFOUND;
1722 if (i < el->num_values-1) {
1723 memmove(&el->values[i], &el->values[i+1], sizeof(el->values[0])*((el->num_values-1)-i));
1727 if (!b9_set_session_info(state, name)) {
1729 return ISC_R_FAILURE;
1732 if (el->num_values == 0) {
1733 el->flags = LDB_FLAG_MOD_DELETE;
1735 el->flags = LDB_FLAG_MOD_REPLACE;
1737 ret = ldb_modify(state->samdb, res->msgs[0]);
1739 b9_reset_session_info(state);
1740 if (ret != LDB_SUCCESS) {
1741 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1742 ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1744 return ISC_R_FAILURE;
1747 state->log(ISC_LOG_INFO, "samba_dlz: subtracted rdataset %s '%s'", name, rdatastr);
1750 return ISC_R_SUCCESS;
1755 delete all records of the given type
1757 _PUBLIC_ isc_result_t dlz_delrdataset(const char *name, const char *type, void *dbdata, void *version)
1759 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1760 TALLOC_CTX *tmp_ctx;
1762 isc_result_t result;
1763 struct ldb_result *res;
1764 const char *attrs[] = { "dnsRecord", NULL };
1766 struct ldb_message_element *el;
1767 enum ndr_err_code ndr_err;
1768 enum dns_record_type dns_type;
1771 if (state->transaction_token != (void*)version) {
1772 state->log(ISC_LOG_ERROR, "samba_dlz: bad transaction version");
1773 return ISC_R_FAILURE;
1776 if (!b9_dns_type(type, &dns_type)) {
1777 state->log(ISC_LOG_ERROR, "samba_dlz: bad dns type %s in delete", type);
1778 return ISC_R_FAILURE;
1781 tmp_ctx = talloc_new(state);
1783 /* find the DN of the record */
1784 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1785 if (result != ISC_R_SUCCESS) {
1786 talloc_free(tmp_ctx);
1790 /* get the existing records */
1791 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1792 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1793 talloc_free(tmp_ctx);
1794 return ISC_R_NOTFOUND;
1797 /* there are existing records. We need to see if any match the type
1799 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1800 if (el == NULL || el->num_values == 0) {
1801 talloc_free(tmp_ctx);
1802 return ISC_R_NOTFOUND;
1805 for (i=0; i<el->num_values; i++) {
1806 struct dnsp_DnssrvRpcRecord rec2;
1808 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec2,
1809 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1810 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1811 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1812 ldb_dn_get_linearized(dn));
1813 talloc_free(tmp_ctx);
1814 return ISC_R_FAILURE;
1817 if (dns_type == rec2.wType) {
1818 if (i < el->num_values-1) {
1819 memmove(&el->values[i], &el->values[i+1],
1820 sizeof(el->values[0])*((el->num_values-1)-i));
1829 talloc_free(tmp_ctx);
1830 return ISC_R_FAILURE;
1833 if (!b9_set_session_info(state, name)) {
1834 talloc_free(tmp_ctx);
1835 return ISC_R_FAILURE;
1838 if (el->num_values == 0) {
1839 el->flags = LDB_FLAG_MOD_DELETE;
1841 el->flags = LDB_FLAG_MOD_REPLACE;
1843 ret = ldb_modify(state->samdb, res->msgs[0]);
1845 b9_reset_session_info(state);
1846 if (ret != LDB_SUCCESS) {
1847 state->log(ISC_LOG_ERROR, "samba_dlz: failed to delete type %s in %s - %s",
1848 type, ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1849 talloc_free(tmp_ctx);
1850 return ISC_R_FAILURE;
1853 state->log(ISC_LOG_INFO, "samba_dlz: deleted rdataset %s of type %s", name, type);
1855 talloc_free(tmp_ctx);
1856 return ISC_R_SUCCESS;