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 */
63 dns_sdlz_putrr_t
*putrr
;
64 dns_sdlz_putnamedrr_t
*putnamedrr
;
65 dns_dlz_writeablezone_t
*writeable_zone
;
68 static struct dlz_bind9_data
*dlz_bind9_state
= NULL
;
69 static int dlz_bind9_state_ref_count
= 0;
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
)
116 switch (rec
->wType
) {
119 *data
= rec
->data
.ipv4
;
124 *data
= rec
->data
.ipv6
;
129 *data
= rec
->data
.cname
;
134 tmp
= talloc_asprintf(mem_ctx
, "\"%s\"", rec
->data
.txt
.str
[0]);
135 for (i
=1; i
<rec
->data
.txt
.count
; i
++) {
136 tmp
= talloc_asprintf_append(tmp
, " \"%s\"", rec
->data
.txt
.str
[i
]);
143 *data
= rec
->data
.ptr
;
148 *data
= talloc_asprintf(mem_ctx
, "%u %u %u %s",
149 rec
->data
.srv
.wPriority
,
150 rec
->data
.srv
.wWeight
,
152 rec
->data
.srv
.nameTarget
);
157 *data
= talloc_asprintf(mem_ctx
, "%u %s",
158 rec
->data
.mx
.wPriority
,
159 rec
->data
.mx
.nameTarget
);
164 *data
= talloc_asprintf(mem_ctx
, "%s %s",
171 *data
= rec
->data
.ns
;
178 /* we need to fake the authoritative nameserver to
179 * point at ourselves. This is how AD DNS servers
180 * force clients to send updates to the right local DC
182 mname
= talloc_asprintf(mem_ctx
, "%s.%s",
183 lpcfg_netbios_name(state
->lp
), lpcfg_dnsdomain(state
->lp
));
187 mname
= strlower_talloc(mem_ctx
, mname
);
192 state
->soa_serial
= rec
->data
.soa
.serial
;
194 *data
= talloc_asprintf(mem_ctx
, "%s %s %u %u %u %u %u",
197 rec
->data
.soa
.serial
,
198 rec
->data
.soa
.refresh
,
200 rec
->data
.soa
.expire
,
201 rec
->data
.soa
.minimum
);
206 state
->log(ISC_LOG_ERROR
, "samba b9_putrr: unhandled record type %u",
214 static const struct {
215 enum dns_record_type dns_type
;
219 { DNS_TYPE_A
, "A" , false},
220 { DNS_TYPE_AAAA
, "AAAA" , false},
221 { DNS_TYPE_CNAME
, "CNAME" , true},
222 { DNS_TYPE_TXT
, "TXT" , false},
223 { DNS_TYPE_PTR
, "PTR" , false},
224 { DNS_TYPE_SRV
, "SRV" , false},
225 { DNS_TYPE_MX
, "MX" , false},
226 { DNS_TYPE_HINFO
, "HINFO" , false},
227 { DNS_TYPE_NS
, "NS" , false},
228 { DNS_TYPE_SOA
, "SOA" , true},
233 see if a DNS type is single valued
235 static bool b9_single_valued(enum dns_record_type dns_type
)
238 for (i
=0; i
<ARRAY_SIZE(dns_typemap
); i
++) {
239 if (dns_typemap
[i
].dns_type
== dns_type
) {
240 return dns_typemap
[i
].single_valued
;
247 see if a DNS type is single valued
249 static bool b9_dns_type(const char *type
, enum dns_record_type
*dtype
)
252 for (i
=0; i
<ARRAY_SIZE(dns_typemap
); i
++) {
253 if (strcasecmp(dns_typemap
[i
].typestr
, type
) == 0) {
254 *dtype
= dns_typemap
[i
].dns_type
;
262 #define DNS_PARSE_STR(ret, str, sep, saveptr) do { \
263 (ret) = strtok_r(str, sep, &saveptr); \
264 if ((ret) == NULL) return false; \
267 #define DNS_PARSE_UINT(ret, str, sep, saveptr) do { \
268 char *istr = strtok_r(str, sep, &saveptr); \
269 if ((istr) == NULL) return false; \
270 (ret) = strtoul(istr, NULL, 10); \
274 parse a record from bind9
276 static bool b9_parse(struct dlz_bind9_data
*state
,
277 const char *rdatastr
,
278 struct dnsp_DnssrvRpcRecord
*rec
)
280 char *full_name
, *dclass
, *type
;
281 char *str
, *tmp
, *saveptr
=NULL
;
284 str
= talloc_strdup(rec
, rdatastr
);
289 /* parse the SDLZ string form */
290 DNS_PARSE_STR(full_name
, str
, "\t", saveptr
);
291 DNS_PARSE_UINT(rec
->dwTtlSeconds
, NULL
, "\t", saveptr
);
292 DNS_PARSE_STR(dclass
, NULL
, "\t", saveptr
);
293 DNS_PARSE_STR(type
, NULL
, "\t", saveptr
);
295 /* construct the record */
296 for (i
=0; i
<ARRAY_SIZE(dns_typemap
); i
++) {
297 if (strcasecmp(type
, dns_typemap
[i
].typestr
) == 0) {
298 rec
->wType
= dns_typemap
[i
].dns_type
;
302 if (i
== ARRAY_SIZE(dns_typemap
)) {
303 state
->log(ISC_LOG_ERROR
, "samba_dlz: unsupported record type '%s' for '%s'",
308 switch (rec
->wType
) {
310 DNS_PARSE_STR(rec
->data
.ipv4
, NULL
, " ", saveptr
);
314 DNS_PARSE_STR(rec
->data
.ipv6
, NULL
, " ", saveptr
);
318 DNS_PARSE_STR(rec
->data
.cname
, NULL
, " ", saveptr
);
322 rec
->data
.txt
.count
= 0;
323 rec
->data
.txt
.str
= talloc_array(rec
, const char *, rec
->data
.txt
.count
);
324 tmp
= strtok_r(NULL
, "\t", &saveptr
);
326 rec
->data
.txt
.str
= talloc_realloc(rec
, rec
->data
.txt
.str
, const char *,
327 rec
->data
.txt
.count
+1);
330 rec
->data
.txt
.str
[rec
->data
.txt
.count
] = talloc_strndup(rec
, &tmp
[1], strlen(tmp
)-2);
332 rec
->data
.txt
.str
[rec
->data
.txt
.count
] = talloc_strdup(rec
, tmp
);
334 rec
->data
.txt
.count
++;
335 tmp
= strtok_r(NULL
, " ", &saveptr
);
340 DNS_PARSE_STR(rec
->data
.ptr
, NULL
, " ", saveptr
);
344 DNS_PARSE_UINT(rec
->data
.srv
.wPriority
, NULL
, " ", saveptr
);
345 DNS_PARSE_UINT(rec
->data
.srv
.wWeight
, NULL
, " ", saveptr
);
346 DNS_PARSE_UINT(rec
->data
.srv
.wPort
, NULL
, " ", saveptr
);
347 DNS_PARSE_STR(rec
->data
.srv
.nameTarget
, NULL
, " ", saveptr
);
351 DNS_PARSE_UINT(rec
->data
.mx
.wPriority
, NULL
, " ", saveptr
);
352 DNS_PARSE_STR(rec
->data
.mx
.nameTarget
, NULL
, " ", saveptr
);
356 DNS_PARSE_STR(rec
->data
.hinfo
.cpu
, NULL
, " ", saveptr
);
357 DNS_PARSE_STR(rec
->data
.hinfo
.os
, NULL
, " ", saveptr
);
361 DNS_PARSE_STR(rec
->data
.ns
, NULL
, " ", saveptr
);
365 DNS_PARSE_STR(rec
->data
.soa
.mname
, NULL
, " ", saveptr
);
366 DNS_PARSE_STR(rec
->data
.soa
.rname
, NULL
, " ", saveptr
);
367 DNS_PARSE_UINT(rec
->data
.soa
.serial
, NULL
, " ", saveptr
);
368 DNS_PARSE_UINT(rec
->data
.soa
.refresh
, NULL
, " ", saveptr
);
369 DNS_PARSE_UINT(rec
->data
.soa
.retry
, NULL
, " ", saveptr
);
370 DNS_PARSE_UINT(rec
->data
.soa
.expire
, NULL
, " ", saveptr
);
371 DNS_PARSE_UINT(rec
->data
.soa
.minimum
, NULL
, " ", saveptr
);
375 state
->log(ISC_LOG_ERROR
, "samba b9_parse: unhandled record type %u",
380 /* we should be at the end of the buffer now */
381 if (strtok_r(NULL
, "\t ", &saveptr
) != NULL
) {
382 state
->log(ISC_LOG_ERROR
, "samba b9_parse: unexpected data at end of string for '%s'",
391 send a resource record to bind9
393 static isc_result_t
b9_putrr(struct dlz_bind9_data
*state
,
394 void *handle
, struct dnsp_DnssrvRpcRecord
*rec
,
398 const char *type
, *data
;
399 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
401 if (!b9_format(state
, tmp_ctx
, rec
, &type
, &data
)) {
402 return ISC_R_FAILURE
;
406 talloc_free(tmp_ctx
);
407 return ISC_R_NOMEMORY
;
412 for (i
=0; types
[i
]; i
++) {
413 if (strcmp(types
[i
], type
) == 0) break;
415 if (types
[i
] == NULL
) {
417 return ISC_R_SUCCESS
;
421 result
= state
->putrr(handle
, type
, rec
->dwTtlSeconds
, data
);
422 if (result
!= ISC_R_SUCCESS
) {
423 state
->log(ISC_LOG_ERROR
, "Failed to put rr");
425 talloc_free(tmp_ctx
);
431 send a named resource record to bind9
433 static isc_result_t
b9_putnamedrr(struct dlz_bind9_data
*state
,
434 void *handle
, const char *name
,
435 struct dnsp_DnssrvRpcRecord
*rec
)
438 const char *type
, *data
;
439 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
441 if (!b9_format(state
, tmp_ctx
, rec
, &type
, &data
)) {
442 return ISC_R_FAILURE
;
446 talloc_free(tmp_ctx
);
447 return ISC_R_NOMEMORY
;
450 result
= state
->putnamedrr(handle
, name
, type
, rec
->dwTtlSeconds
, data
);
451 if (result
!= ISC_R_SUCCESS
) {
452 state
->log(ISC_LOG_ERROR
, "Failed to put named rr '%s'", name
);
454 talloc_free(tmp_ctx
);
461 static isc_result_t
parse_options(struct dlz_bind9_data
*state
,
462 unsigned int argc
, char *argv
[],
463 struct b9_options
*options
)
467 struct poptOption long_options
[] = {
468 { "url", 'H', POPT_ARG_STRING
, &options
->url
, 0, "database URL", "URL" },
469 { "debug", 'd', POPT_ARG_STRING
, &options
->debug
, 0, "debug level", "DEBUG" },
473 pc
= poptGetContext("dlz_bind9", argc
, (const char **)argv
, long_options
,
474 POPT_CONTEXT_KEEP_FIRST
);
475 while ((opt
= poptGetNextOpt(pc
)) != -1) {
478 state
->log(ISC_LOG_ERROR
, "dlz_bind9: Invalid option %s: %s",
479 poptBadOption(pc
, 0), poptStrerror(opt
));
480 return ISC_R_FAILURE
;
484 return ISC_R_SUCCESS
;
489 * Create session info from PAC
490 * This is called as auth_context->generate_session_info_pac()
492 static NTSTATUS
b9_generate_session_info_pac(struct auth4_context
*auth_context
,
494 struct smb_krb5_context
*smb_krb5_context
,
496 const char *principal_name
,
497 const struct tsocket_address
*remote_addr
,
498 uint32_t session_info_flags
,
499 struct auth_session_info
**session_info
)
502 struct auth_user_info_dc
*user_info_dc
;
505 tmp_ctx
= talloc_new(mem_ctx
);
506 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
508 status
= kerberos_pac_blob_to_user_info_dc(tmp_ctx
,
510 smb_krb5_context
->krb5_context
,
514 if (!NT_STATUS_IS_OK(status
)) {
515 talloc_free(tmp_ctx
);
519 if (user_info_dc
->info
->authenticated
) {
520 session_info_flags
|= AUTH_SESSION_INFO_AUTHENTICATED
;
523 session_info_flags
|= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES
;
525 status
= auth_generate_session_info(mem_ctx
, NULL
, NULL
, user_info_dc
,
526 session_info_flags
, session_info
);
527 if (!NT_STATUS_IS_OK(status
)) {
528 talloc_free(tmp_ctx
);
532 talloc_free(tmp_ctx
);
536 /* Callback for the DEBUG() system, to catch the remaining messages */
537 static void b9_debug(void *private_ptr
, int msg_level
, const char *msg
)
539 static const int isc_log_map
[] = {
540 ISC_LOG_CRITICAL
, /* 0 */
541 ISC_LOG_ERROR
, /* 1 */
542 ISC_LOG_WARNING
, /* 2 */
543 ISC_LOG_NOTICE
/* 3 */
545 struct dlz_bind9_data
*state
= private_ptr
;
548 if (msg_level
>= ARRAY_SIZE(isc_log_map
) || msg_level
< 0) {
549 isc_log_level
= ISC_LOG_INFO
;
551 isc_log_level
= isc_log_map
[msg_level
];
553 state
->log(isc_log_level
, "samba_dlz: %s", msg
);
556 static int dlz_state_debug_unregister(struct dlz_bind9_data
*state
)
558 /* Stop logging (to the bind9 logs) */
559 debug_set_callback(NULL
, NULL
);
564 called to initialise the driver
566 _PUBLIC_ isc_result_t
dlz_create(const char *dlzname
,
567 unsigned int argc
, char *argv
[],
570 struct dlz_bind9_data
*state
;
571 const char *helper_name
;
577 if (dlz_bind9_state
!= NULL
) {
578 *dbdata
= dlz_bind9_state
;
579 dlz_bind9_state_ref_count
++;
580 return ISC_R_SUCCESS
;
583 state
= talloc_zero(NULL
, struct dlz_bind9_data
);
585 return ISC_R_NOMEMORY
;
588 talloc_set_destructor(state
, dlz_state_debug_unregister
);
590 /* fill in the helper functions */
591 va_start(ap
, dbdata
);
592 while ((helper_name
= va_arg(ap
, const char *)) != NULL
) {
593 b9_add_helper(state
, helper_name
, va_arg(ap
, void*));
597 /* Do not install samba signal handlers */
598 fault_setup_disable();
600 /* Start logging (to the bind9 logs) */
601 debug_set_callback(state
, b9_debug
);
603 state
->ev_ctx
= s4_event_context_init(state
);
604 if (state
->ev_ctx
== NULL
) {
605 result
= ISC_R_NOMEMORY
;
609 result
= parse_options(state
, argc
, argv
, &state
->options
);
610 if (result
!= ISC_R_SUCCESS
) {
614 state
->lp
= loadparm_init_global(true);
615 if (state
->lp
== NULL
) {
616 result
= ISC_R_NOMEMORY
;
620 if (state
->options
.debug
) {
621 lpcfg_do_global_parameter(state
->lp
, "log level", state
->options
.debug
);
623 lpcfg_do_global_parameter(state
->lp
, "log level", "0");
626 if (smb_krb5_init_context(state
, state
->ev_ctx
, state
->lp
, &state
->smb_krb5_ctx
) != 0) {
627 result
= ISC_R_NOMEMORY
;
631 nt_status
= gensec_init();
632 if (!NT_STATUS_IS_OK(nt_status
)) {
633 result
= ISC_R_NOMEMORY
;
637 state
->auth_context
= talloc_zero(state
, struct auth4_context
);
638 if (state
->auth_context
== NULL
) {
639 result
= ISC_R_NOMEMORY
;
643 if (state
->options
.url
== NULL
) {
644 state
->options
.url
= lpcfg_private_path(state
, state
->lp
, "dns/sam.ldb");
645 if (state
->options
.url
== NULL
) {
646 result
= ISC_R_NOMEMORY
;
651 state
->samdb
= samdb_connect_url(state
, state
->ev_ctx
, state
->lp
,
652 system_session(state
->lp
), 0, state
->options
.url
);
653 if (state
->samdb
== NULL
) {
654 state
->log(ISC_LOG_ERROR
, "samba_dlz: Failed to connect to %s",
656 result
= ISC_R_FAILURE
;
660 dn
= ldb_get_default_basedn(state
->samdb
);
662 state
->log(ISC_LOG_ERROR
, "samba_dlz: Unable to get basedn for %s - %s",
663 state
->options
.url
, ldb_errstring(state
->samdb
));
664 result
= ISC_R_FAILURE
;
668 state
->log(ISC_LOG_INFO
, "samba_dlz: started for DN %s",
669 ldb_dn_get_linearized(dn
));
671 state
->auth_context
->event_ctx
= state
->ev_ctx
;
672 state
->auth_context
->lp_ctx
= state
->lp
;
673 state
->auth_context
->sam_ctx
= state
->samdb
;
674 state
->auth_context
->generate_session_info_pac
= b9_generate_session_info_pac
;
677 dlz_bind9_state
= state
;
678 dlz_bind9_state_ref_count
++;
680 return ISC_R_SUCCESS
;
690 _PUBLIC_
void dlz_destroy(void *dbdata
)
692 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
693 state
->log(ISC_LOG_INFO
, "samba_dlz: shutting down");
695 dlz_bind9_state_ref_count
--;
696 if (dlz_bind9_state_ref_count
== 0) {
697 talloc_unlink(state
, state
->samdb
);
699 dlz_bind9_state
= NULL
;
705 return the base DN for a zone
707 static isc_result_t
b9_find_zone_dn(struct dlz_bind9_data
*state
, const char *zone_name
,
708 TALLOC_CTX
*mem_ctx
, struct ldb_dn
**zone_dn
)
711 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
712 const char *attrs
[] = { NULL
};
715 for (i
=0; zone_prefixes
[i
]; i
++) {
717 struct ldb_result
*res
;
719 dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(state
->samdb
));
721 talloc_free(tmp_ctx
);
722 return ISC_R_NOMEMORY
;
725 if (!ldb_dn_add_child_fmt(dn
, "DC=%s,%s", zone_name
, zone_prefixes
[i
])) {
726 talloc_free(tmp_ctx
);
727 return ISC_R_NOMEMORY
;
730 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, "objectClass=dnsZone");
731 if (ret
== LDB_SUCCESS
) {
732 if (zone_dn
!= NULL
) {
733 *zone_dn
= talloc_steal(mem_ctx
, dn
);
735 talloc_free(tmp_ctx
);
736 return ISC_R_SUCCESS
;
741 talloc_free(tmp_ctx
);
742 return ISC_R_NOTFOUND
;
747 return the DN for a name. The record does not need to exist, but the
750 static isc_result_t
b9_find_name_dn(struct dlz_bind9_data
*state
, const char *name
,
751 TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
755 /* work through the name piece by piece, until we find a zone */
758 result
= b9_find_zone_dn(state
, p
, mem_ctx
, dn
);
759 if (result
== ISC_R_SUCCESS
) {
760 /* we found a zone, now extend the DN to get
765 ret
= ldb_dn_add_child_fmt(*dn
, "DC=@");
767 ret
= ldb_dn_add_child_fmt(*dn
, "DC=%.*s", (int)(p
-name
)-1, name
);
771 return ISC_R_NOMEMORY
;
773 return ISC_R_SUCCESS
;
781 return ISC_R_NOTFOUND
;
786 see if we handle a given zone
788 _PUBLIC_ isc_result_t
dlz_findzonedb(void *dbdata
, const char *name
)
790 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
791 return b9_find_zone_dn(state
, name
, NULL
, NULL
);
798 static isc_result_t
dlz_lookup_types(struct dlz_bind9_data
*state
,
799 const char *zone
, const char *name
,
800 dns_sdlzlookup_t
*lookup
,
803 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
804 const char *attrs
[] = { "dnsRecord", NULL
};
805 int ret
= LDB_SUCCESS
, i
;
806 struct ldb_result
*res
;
807 struct ldb_message_element
*el
;
810 for (i
=0; zone_prefixes
[i
]; i
++) {
811 dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(state
->samdb
));
813 talloc_free(tmp_ctx
);
814 return ISC_R_NOMEMORY
;
817 if (!ldb_dn_add_child_fmt(dn
, "DC=%s,DC=%s,%s", name
, zone
, zone_prefixes
[i
])) {
818 talloc_free(tmp_ctx
);
819 return ISC_R_NOMEMORY
;
822 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
,
823 attrs
, "objectClass=dnsNode");
824 if (ret
== LDB_SUCCESS
) {
828 if (ret
!= LDB_SUCCESS
) {
829 talloc_free(tmp_ctx
);
830 return ISC_R_NOTFOUND
;
833 el
= ldb_msg_find_element(res
->msgs
[0], "dnsRecord");
834 if (el
== NULL
|| el
->num_values
== 0) {
835 talloc_free(tmp_ctx
);
836 return ISC_R_NOTFOUND
;
839 for (i
=0; i
<el
->num_values
; i
++) {
840 struct dnsp_DnssrvRpcRecord rec
;
841 enum ndr_err_code ndr_err
;
844 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
], tmp_ctx
, &rec
,
845 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
846 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
847 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse dnsRecord for %s",
848 ldb_dn_get_linearized(dn
));
849 talloc_free(tmp_ctx
);
850 return ISC_R_FAILURE
;
853 result
= b9_putrr(state
, lookup
, &rec
, types
);
854 if (result
!= ISC_R_SUCCESS
) {
855 talloc_free(tmp_ctx
);
860 talloc_free(tmp_ctx
);
861 return ISC_R_SUCCESS
;
867 #ifdef BIND_VERSION_9_8
868 _PUBLIC_ isc_result_t
dlz_lookup(const char *zone
, const char *name
,
869 void *dbdata
, dns_sdlzlookup_t
*lookup
)
871 _PUBLIC_ isc_result_t
dlz_lookup(const char *zone
, const char *name
,
872 void *dbdata
, dns_sdlzlookup_t
*lookup
,
873 dns_clientinfomethods_t
*methods
,
874 dns_clientinfo_t
*clientinfo
)
877 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
878 return dlz_lookup_types(state
, zone
, name
, lookup
, NULL
);
883 see if a zone transfer is allowed
885 _PUBLIC_ isc_result_t
dlz_allowzonexfr(void *dbdata
, const char *name
, const char *client
)
887 /* just say yes for all our zones for now */
888 return dlz_findzonedb(dbdata
, name
);
892 perform a zone transfer
894 _PUBLIC_ isc_result_t
dlz_allnodes(const char *zone
, void *dbdata
,
895 dns_sdlzallnodes_t
*allnodes
)
897 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
898 const char *attrs
[] = { "dnsRecord", NULL
};
899 int ret
= LDB_SUCCESS
, i
, j
;
901 struct ldb_result
*res
;
902 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
904 for (i
=0; zone_prefixes
[i
]; i
++) {
905 dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(state
->samdb
));
907 talloc_free(tmp_ctx
);
908 return ISC_R_NOMEMORY
;
911 if (!ldb_dn_add_child_fmt(dn
, "DC=%s,%s", zone
, zone_prefixes
[i
])) {
912 talloc_free(tmp_ctx
);
913 return ISC_R_NOMEMORY
;
916 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_SUBTREE
,
917 attrs
, "objectClass=dnsNode");
918 if (ret
== LDB_SUCCESS
) {
922 if (ret
!= LDB_SUCCESS
) {
923 talloc_free(tmp_ctx
);
924 return ISC_R_NOTFOUND
;
927 for (i
=0; i
<res
->count
; i
++) {
928 struct ldb_message_element
*el
;
929 TALLOC_CTX
*el_ctx
= talloc_new(tmp_ctx
);
930 const char *rdn
, *name
;
931 const struct ldb_val
*v
;
933 el
= ldb_msg_find_element(res
->msgs
[i
], "dnsRecord");
934 if (el
== NULL
|| el
->num_values
== 0) {
935 state
->log(ISC_LOG_INFO
, "failed to find dnsRecord for %s",
936 ldb_dn_get_linearized(dn
));
941 v
= ldb_dn_get_rdn_val(res
->msgs
[i
]->dn
);
943 state
->log(ISC_LOG_INFO
, "failed to find RDN for %s",
944 ldb_dn_get_linearized(dn
));
949 rdn
= talloc_strndup(el_ctx
, (char *)v
->data
, v
->length
);
951 talloc_free(tmp_ctx
);
952 return ISC_R_NOMEMORY
;
955 if (strcmp(rdn
, "@") == 0) {
958 name
= talloc_asprintf(el_ctx
, "%s.%s", rdn
, zone
);
961 talloc_free(tmp_ctx
);
962 return ISC_R_NOMEMORY
;
965 for (j
=0; j
<el
->num_values
; j
++) {
966 struct dnsp_DnssrvRpcRecord rec
;
967 enum ndr_err_code ndr_err
;
970 ndr_err
= ndr_pull_struct_blob(&el
->values
[j
], el_ctx
, &rec
,
971 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
972 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
973 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse dnsRecord for %s",
974 ldb_dn_get_linearized(dn
));
978 result
= b9_putnamedrr(state
, allnodes
, name
, &rec
);
979 if (result
!= ISC_R_SUCCESS
) {
985 talloc_free(tmp_ctx
);
987 return ISC_R_SUCCESS
;
994 _PUBLIC_ isc_result_t
dlz_newversion(const char *zone
, void *dbdata
, void **versionp
)
996 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
998 state
->log(ISC_LOG_INFO
, "samba_dlz: starting transaction on zone %s", zone
);
1000 if (state
->transaction_token
!= NULL
) {
1001 state
->log(ISC_LOG_INFO
, "samba_dlz: transaction already started for zone %s", zone
);
1002 return ISC_R_FAILURE
;
1005 state
->transaction_token
= talloc_zero(state
, int);
1006 if (state
->transaction_token
== NULL
) {
1007 return ISC_R_NOMEMORY
;
1010 if (ldb_transaction_start(state
->samdb
) != LDB_SUCCESS
) {
1011 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to start a transaction for zone %s", zone
);
1012 talloc_free(state
->transaction_token
);
1013 state
->transaction_token
= NULL
;
1014 return ISC_R_FAILURE
;
1017 *versionp
= (void *)state
->transaction_token
;
1019 return ISC_R_SUCCESS
;
1025 _PUBLIC_
void dlz_closeversion(const char *zone
, isc_boolean_t commit
,
1026 void *dbdata
, void **versionp
)
1028 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1030 if (state
->transaction_token
!= (int *)*versionp
) {
1031 state
->log(ISC_LOG_INFO
, "samba_dlz: transaction not started for zone %s", zone
);
1036 if (ldb_transaction_commit(state
->samdb
) != LDB_SUCCESS
) {
1037 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to commit a transaction for zone %s", zone
);
1040 state
->log(ISC_LOG_INFO
, "samba_dlz: committed transaction on zone %s", zone
);
1042 if (ldb_transaction_cancel(state
->samdb
) != LDB_SUCCESS
) {
1043 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to cancel a transaction for zone %s", zone
);
1046 state
->log(ISC_LOG_INFO
, "samba_dlz: cancelling transaction on zone %s", zone
);
1049 talloc_free(state
->transaction_token
);
1050 state
->transaction_token
= NULL
;
1056 see if there is a SOA record for a zone
1058 static bool b9_has_soa(struct dlz_bind9_data
*state
, struct ldb_dn
*dn
, const char *zone
)
1060 const char *attrs
[] = { "dnsRecord", NULL
};
1061 struct ldb_result
*res
;
1062 struct ldb_message_element
*el
;
1063 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
1066 if (!ldb_dn_add_child_fmt(dn
, "DC=@,DC=%s", zone
)) {
1067 talloc_free(tmp_ctx
);
1071 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
,
1072 attrs
, "objectClass=dnsNode");
1073 if (ret
!= LDB_SUCCESS
) {
1074 talloc_free(tmp_ctx
);
1078 el
= ldb_msg_find_element(res
->msgs
[0], "dnsRecord");
1080 talloc_free(tmp_ctx
);
1083 for (i
=0; i
<el
->num_values
; i
++) {
1084 struct dnsp_DnssrvRpcRecord rec
;
1085 enum ndr_err_code ndr_err
;
1087 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
], tmp_ctx
, &rec
,
1088 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
1089 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1092 if (rec
.wType
== DNS_TYPE_SOA
) {
1093 talloc_free(tmp_ctx
);
1098 talloc_free(tmp_ctx
);
1103 configure a writeable zone
1105 _PUBLIC_ isc_result_t
dlz_configure(dns_view_t
*view
, void *dbdata
)
1107 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1108 TALLOC_CTX
*tmp_ctx
;
1112 state
->log(ISC_LOG_INFO
, "samba_dlz: starting configure");
1113 if (state
->writeable_zone
== NULL
) {
1114 state
->log(ISC_LOG_INFO
, "samba_dlz: no writeable_zone method available");
1115 return ISC_R_FAILURE
;
1118 tmp_ctx
= talloc_new(state
);
1120 for (i
=0; zone_prefixes
[i
]; i
++) {
1121 const char *attrs
[] = { "name", NULL
};
1123 struct ldb_result
*res
;
1125 dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(state
->samdb
));
1127 talloc_free(tmp_ctx
);
1128 return ISC_R_NOMEMORY
;
1131 if (!ldb_dn_add_child_fmt(dn
, "%s", zone_prefixes
[i
])) {
1132 talloc_free(tmp_ctx
);
1133 return ISC_R_NOMEMORY
;
1136 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_SUBTREE
,
1137 attrs
, "objectClass=dnsZone");
1138 if (ret
!= LDB_SUCCESS
) {
1142 for (j
=0; j
<res
->count
; j
++) {
1143 isc_result_t result
;
1144 const char *zone
= ldb_msg_find_attr_as_string(res
->msgs
[j
], "name", NULL
);
1145 struct ldb_dn
*zone_dn
;
1150 /* Ignore zones that are not handled in BIND */
1151 if ((strcmp(zone
, "RootDNSServers") == 0) ||
1152 (strcmp(zone
, "..TrustAnchors") == 0)) {
1155 zone_dn
= ldb_dn_copy(tmp_ctx
, dn
);
1156 if (zone_dn
== NULL
) {
1157 talloc_free(tmp_ctx
);
1158 return ISC_R_NOMEMORY
;
1161 if (!b9_has_soa(state
, zone_dn
, zone
)) {
1164 result
= state
->writeable_zone(view
, zone
);
1165 if (result
!= ISC_R_SUCCESS
) {
1166 state
->log(ISC_LOG_ERROR
, "samba_dlz: Failed to configure zone '%s'",
1168 talloc_free(tmp_ctx
);
1171 state
->log(ISC_LOG_INFO
, "samba_dlz: configured writeable zone '%s'", zone
);
1175 talloc_free(tmp_ctx
);
1176 return ISC_R_SUCCESS
;
1180 authorize a zone update
1182 _PUBLIC_ isc_boolean_t
dlz_ssumatch(const char *signer
, const char *name
, const char *tcpaddr
,
1183 const char *type
, const char *key
, uint32_t keydatalen
, uint8_t *keydata
,
1186 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1187 TALLOC_CTX
*tmp_ctx
;
1189 struct cli_credentials
*server_credentials
;
1194 struct gensec_security
*gensec_ctx
;
1195 struct auth_session_info
*session_info
;
1197 isc_result_t result
;
1198 struct ldb_result
*res
;
1199 const char * attrs
[] = { NULL
};
1200 uint32_t access_mask
;
1202 /* Remove cached credentials, if any */
1203 if (state
->session_info
) {
1204 talloc_free(state
->session_info
);
1205 state
->session_info
= NULL
;
1207 if (state
->update_name
) {
1208 talloc_free(state
->update_name
);
1209 state
->update_name
= NULL
;
1212 tmp_ctx
= talloc_new(NULL
);
1213 if (tmp_ctx
== NULL
) {
1214 state
->log(ISC_LOG_ERROR
, "samba_dlz: no memory");
1218 ap_req
= data_blob_const(keydata
, keydatalen
);
1219 server_credentials
= cli_credentials_init(tmp_ctx
);
1220 if (!server_credentials
) {
1221 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to init server credentials");
1222 talloc_free(tmp_ctx
);
1226 cli_credentials_set_krb5_context(server_credentials
, state
->smb_krb5_ctx
);
1227 cli_credentials_set_conf(server_credentials
, state
->lp
);
1229 keytab_name
= talloc_asprintf(tmp_ctx
, "file:%s/dns.keytab",
1230 lpcfg_private_dir(state
->lp
));
1231 ret
= cli_credentials_set_keytab_name(server_credentials
, state
->lp
, keytab_name
,
1234 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to obtain server credentials from %s",
1236 talloc_free(tmp_ctx
);
1239 talloc_free(keytab_name
);
1241 nt_status
= gensec_server_start(tmp_ctx
,
1242 lpcfg_gensec_settings(tmp_ctx
, state
->lp
),
1243 state
->auth_context
, &gensec_ctx
);
1244 if (!NT_STATUS_IS_OK(nt_status
)) {
1245 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to start gensec server");
1246 talloc_free(tmp_ctx
);
1250 gensec_set_credentials(gensec_ctx
, server_credentials
);
1252 nt_status
= gensec_start_mech_by_name(gensec_ctx
, "spnego");
1253 if (!NT_STATUS_IS_OK(nt_status
)) {
1254 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to start spnego");
1255 talloc_free(tmp_ctx
);
1259 nt_status
= gensec_update(gensec_ctx
, tmp_ctx
, state
->ev_ctx
, ap_req
, &ap_req
);
1260 if (!NT_STATUS_IS_OK(nt_status
)) {
1261 state
->log(ISC_LOG_ERROR
, "samba_dlz: spnego update failed");
1262 talloc_free(tmp_ctx
);
1266 nt_status
= gensec_session_info(gensec_ctx
, tmp_ctx
, &session_info
);
1267 if (!NT_STATUS_IS_OK(nt_status
)) {
1268 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to create session info");
1269 talloc_free(tmp_ctx
);
1273 /* Get the DN from name */
1274 result
= b9_find_name_dn(state
, name
, tmp_ctx
, &dn
);
1275 if (result
!= ISC_R_SUCCESS
) {
1276 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to find name %s", name
);
1277 talloc_free(tmp_ctx
);
1281 /* make sure the dn exists, or find parent dn in case new object is being added */
1282 ldb_ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
,
1283 attrs
, "objectClass=dnsNode");
1284 if (ldb_ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1285 ldb_dn_remove_child_components(dn
, 1);
1286 access_mask
= SEC_ADS_CREATE_CHILD
;
1288 } else if (ldb_ret
== LDB_SUCCESS
) {
1289 access_mask
= SEC_STD_REQUIRED
| SEC_ADS_SELF_WRITE
;
1292 talloc_free(tmp_ctx
);
1297 ldb_ret
= dsdb_check_access_on_dn(state
->samdb
, tmp_ctx
, dn
,
1298 session_info
->security_token
,
1300 if (ldb_ret
!= LDB_SUCCESS
) {
1301 state
->log(ISC_LOG_INFO
,
1302 "samba_dlz: disallowing update of signer=%s name=%s type=%s error=%s",
1303 signer
, name
, type
, ldb_strerror(ldb_ret
));
1304 talloc_free(tmp_ctx
);
1308 /* Cache session_info, so it can be used in the actual add/delete operation */
1309 state
->update_name
= talloc_strdup(state
, name
);
1310 if (state
->update_name
== NULL
) {
1311 state
->log(ISC_LOG_ERROR
, "samba_dlz: memory allocation error");
1312 talloc_free(tmp_ctx
);
1315 state
->session_info
= talloc_steal(state
, session_info
);
1317 state
->log(ISC_LOG_INFO
, "samba_dlz: allowing update of signer=%s name=%s tcpaddr=%s type=%s key=%s",
1318 signer
, name
, tcpaddr
, type
, key
);
1320 talloc_free(tmp_ctx
);
1328 static isc_result_t
b9_add_record(struct dlz_bind9_data
*state
, const char *name
,
1330 struct dnsp_DnssrvRpcRecord
*rec
)
1332 struct ldb_message
*msg
;
1333 enum ndr_err_code ndr_err
;
1337 msg
= ldb_msg_new(rec
);
1339 return ISC_R_NOMEMORY
;
1342 ret
= ldb_msg_add_string(msg
, "objectClass", "dnsNode");
1343 if (ret
!= LDB_SUCCESS
) {
1344 return ISC_R_FAILURE
;
1347 ndr_err
= ndr_push_struct_blob(&v
, rec
, rec
, (ndr_push_flags_fn_t
)ndr_push_dnsp_DnssrvRpcRecord
);
1348 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1349 return ISC_R_FAILURE
;
1351 ret
= ldb_msg_add_value(msg
, "dnsRecord", &v
, NULL
);
1352 if (ret
!= LDB_SUCCESS
) {
1353 return ISC_R_FAILURE
;
1356 ret
= ldb_add(state
->samdb
, msg
);
1357 if (ret
!= LDB_SUCCESS
) {
1358 return ISC_R_FAILURE
;
1361 return ISC_R_SUCCESS
;
1365 see if two DNS names are the same
1367 static bool dns_name_equal(const char *name1
, const char *name2
)
1369 size_t len1
= strlen(name1
);
1370 size_t len2
= strlen(name2
);
1371 if (name1
[len1
-1] == '.') len1
--;
1372 if (name2
[len2
-1] == '.') len2
--;
1376 return strncasecmp_m(name1
, name2
, len1
) == 0;
1381 see if two dns records match
1383 static bool b9_record_match(struct dlz_bind9_data
*state
,
1384 struct dnsp_DnssrvRpcRecord
*rec1
, struct dnsp_DnssrvRpcRecord
*rec2
)
1389 if (rec1
->wType
!= rec2
->wType
) {
1392 /* see if this type is single valued */
1393 if (b9_single_valued(rec1
->wType
)) {
1397 /* see if the data matches */
1398 switch (rec1
->wType
) {
1400 return strcmp(rec1
->data
.ipv4
, rec2
->data
.ipv4
) == 0;
1402 return strcmp(rec1
->data
.ipv6
, rec2
->data
.ipv6
) == 0;
1403 case DNS_TYPE_CNAME
:
1404 return dns_name_equal(rec1
->data
.cname
, rec2
->data
.cname
);
1406 status
= (rec1
->data
.txt
.count
== rec2
->data
.txt
.count
);
1407 if (!status
) return status
;
1408 for (i
=0; i
<rec1
->data
.txt
.count
; i
++) {
1409 status
&= (strcmp(rec1
->data
.txt
.str
[i
], rec2
->data
.txt
.str
[i
]) == 0);
1413 return dns_name_equal(rec1
->data
.ptr
, rec2
->data
.ptr
);
1415 return dns_name_equal(rec1
->data
.ns
, rec2
->data
.ns
);
1418 return rec1
->data
.srv
.wPriority
== rec2
->data
.srv
.wPriority
&&
1419 rec1
->data
.srv
.wWeight
== rec2
->data
.srv
.wWeight
&&
1420 rec1
->data
.srv
.wPort
== rec2
->data
.srv
.wPort
&&
1421 dns_name_equal(rec1
->data
.srv
.nameTarget
, rec2
->data
.srv
.nameTarget
);
1424 return rec1
->data
.mx
.wPriority
== rec2
->data
.mx
.wPriority
&&
1425 dns_name_equal(rec1
->data
.mx
.nameTarget
, rec2
->data
.mx
.nameTarget
);
1427 case DNS_TYPE_HINFO
:
1428 return strcmp(rec1
->data
.hinfo
.cpu
, rec2
->data
.hinfo
.cpu
) == 0 &&
1429 strcmp(rec1
->data
.hinfo
.os
, rec2
->data
.hinfo
.os
) == 0;
1432 return dns_name_equal(rec1
->data
.soa
.mname
, rec2
->data
.soa
.mname
) &&
1433 dns_name_equal(rec1
->data
.soa
.rname
, rec2
->data
.soa
.rname
) &&
1434 rec1
->data
.soa
.serial
== rec2
->data
.soa
.serial
&&
1435 rec1
->data
.soa
.refresh
== rec2
->data
.soa
.refresh
&&
1436 rec1
->data
.soa
.retry
== rec2
->data
.soa
.retry
&&
1437 rec1
->data
.soa
.expire
== rec2
->data
.soa
.expire
&&
1438 rec1
->data
.soa
.minimum
== rec2
->data
.soa
.minimum
;
1440 state
->log(ISC_LOG_ERROR
, "samba b9_putrr: unhandled record type %u",
1449 * Update session_info on samdb using the cached credentials
1451 static bool b9_set_session_info(struct dlz_bind9_data
*state
, const char *name
)
1455 if (state
->update_name
== NULL
|| state
->session_info
== NULL
) {
1456 state
->log(ISC_LOG_ERROR
, "samba_dlz: invalid credentials");
1460 /* Do not use client credentials, if we're not updating the client specified name */
1461 if (strcmp(state
->update_name
, name
) != 0) {
1465 ret
= ldb_set_opaque(state
->samdb
, "sessionInfo", state
->session_info
);
1466 if (ret
!= LDB_SUCCESS
) {
1467 state
->log(ISC_LOG_ERROR
, "samba_dlz: unable to set session info");
1475 * Reset session_info on samdb as system session
1477 static void b9_reset_session_info(struct dlz_bind9_data
*state
)
1479 ldb_set_opaque(state
->samdb
, "sessionInfo", system_session(state
->lp
));
1483 add or modify a rdataset
1485 _PUBLIC_ isc_result_t
dlz_addrdataset(const char *name
, const char *rdatastr
, void *dbdata
, void *version
)
1487 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1488 struct dnsp_DnssrvRpcRecord
*rec
;
1490 isc_result_t result
;
1491 struct ldb_result
*res
;
1492 const char *attrs
[] = { "dnsRecord", NULL
};
1494 struct ldb_message_element
*el
;
1495 enum ndr_err_code ndr_err
;
1498 if (state
->transaction_token
!= (void*)version
) {
1499 state
->log(ISC_LOG_INFO
, "samba_dlz: bad transaction version");
1500 return ISC_R_FAILURE
;
1503 rec
= talloc_zero(state
, struct dnsp_DnssrvRpcRecord
);
1505 return ISC_R_NOMEMORY
;
1508 unix_to_nt_time(&t
, time(NULL
));
1509 t
/= 10*1000*1000; /* convert to seconds (NT time is in 100ns units) */
1510 t
/= 3600; /* convert to hours */
1512 rec
->rank
= DNS_RANK_ZONE
;
1513 rec
->dwSerial
= state
->soa_serial
;
1514 rec
->dwTimeStamp
= (uint32_t)t
;
1516 if (!b9_parse(state
, rdatastr
, rec
)) {
1517 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to parse rdataset '%s'", rdatastr
);
1519 return ISC_R_FAILURE
;
1522 /* find the DN of the record */
1523 result
= b9_find_name_dn(state
, name
, rec
, &dn
);
1524 if (result
!= ISC_R_SUCCESS
) {
1529 /* get any existing records */
1530 ret
= ldb_search(state
->samdb
, rec
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, "objectClass=dnsNode");
1531 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1532 if (!b9_set_session_info(state
, name
)) {
1534 return ISC_R_FAILURE
;
1536 result
= b9_add_record(state
, name
, dn
, rec
);
1537 b9_reset_session_info(state
);
1539 if (result
== ISC_R_SUCCESS
) {
1540 state
->log(ISC_LOG_INFO
, "samba_dlz: added %s %s", name
, rdatastr
);
1545 el
= ldb_msg_find_element(res
->msgs
[0], "dnsRecord");
1547 ret
= ldb_msg_add_empty(res
->msgs
[0], "dnsRecord", LDB_FLAG_MOD_ADD
, &el
);
1548 if (ret
!= LDB_SUCCESS
) {
1549 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to add dnsRecord for %s",
1550 ldb_dn_get_linearized(dn
));
1552 return ISC_R_FAILURE
;
1556 /* there are existing records. We need to see if this will
1557 * replace a record or add to it
1559 for (i
=0; i
<el
->num_values
; i
++) {
1560 struct dnsp_DnssrvRpcRecord rec2
;
1562 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
], rec
, &rec2
,
1563 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
1564 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1565 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse dnsRecord for %s",
1566 ldb_dn_get_linearized(dn
));
1568 return ISC_R_FAILURE
;
1571 if (b9_record_match(state
, rec
, &rec2
)) {
1575 if (i
== el
->num_values
) {
1576 /* adding a new value */
1577 el
->values
= talloc_realloc(el
, el
->values
, struct ldb_val
, el
->num_values
+1);
1578 if (el
->values
== NULL
) {
1580 return ISC_R_NOMEMORY
;
1585 ndr_err
= ndr_push_struct_blob(&el
->values
[i
], rec
, rec
,
1586 (ndr_push_flags_fn_t
)ndr_push_dnsp_DnssrvRpcRecord
);
1587 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1588 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to push dnsRecord for %s",
1589 ldb_dn_get_linearized(dn
));
1591 return ISC_R_FAILURE
;
1595 if (!b9_set_session_info(state
, name
)) {
1597 return ISC_R_FAILURE
;
1600 /* modify the record */
1601 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1602 ret
= ldb_modify(state
->samdb
, res
->msgs
[0]);
1603 b9_reset_session_info(state
);
1604 if (ret
!= LDB_SUCCESS
) {
1605 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to modify %s - %s",
1606 ldb_dn_get_linearized(dn
), ldb_errstring(state
->samdb
));
1608 return ISC_R_FAILURE
;
1611 state
->log(ISC_LOG_INFO
, "samba_dlz: added rdataset %s '%s'", name
, rdatastr
);
1614 return ISC_R_SUCCESS
;
1620 _PUBLIC_ isc_result_t
dlz_subrdataset(const char *name
, const char *rdatastr
, void *dbdata
, void *version
)
1622 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1623 struct dnsp_DnssrvRpcRecord
*rec
;
1625 isc_result_t result
;
1626 struct ldb_result
*res
;
1627 const char *attrs
[] = { "dnsRecord", NULL
};
1629 struct ldb_message_element
*el
;
1630 enum ndr_err_code ndr_err
;
1632 if (state
->transaction_token
!= (void*)version
) {
1633 state
->log(ISC_LOG_ERROR
, "samba_dlz: bad transaction version");
1634 return ISC_R_FAILURE
;
1637 rec
= talloc_zero(state
, struct dnsp_DnssrvRpcRecord
);
1639 return ISC_R_NOMEMORY
;
1642 if (!b9_parse(state
, rdatastr
, rec
)) {
1643 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse rdataset '%s'", rdatastr
);
1645 return ISC_R_FAILURE
;
1648 /* find the DN of the record */
1649 result
= b9_find_name_dn(state
, name
, rec
, &dn
);
1650 if (result
!= ISC_R_SUCCESS
) {
1655 /* get the existing records */
1656 ret
= ldb_search(state
->samdb
, rec
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, "objectClass=dnsNode");
1657 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1659 return ISC_R_NOTFOUND
;
1662 /* there are existing records. We need to see if any match
1664 el
= ldb_msg_find_element(res
->msgs
[0], "dnsRecord");
1665 if (el
== NULL
|| el
->num_values
== 0) {
1666 state
->log(ISC_LOG_ERROR
, "samba_dlz: no dnsRecord attribute for %s",
1667 ldb_dn_get_linearized(dn
));
1669 return ISC_R_FAILURE
;
1672 for (i
=0; i
<el
->num_values
; i
++) {
1673 struct dnsp_DnssrvRpcRecord rec2
;
1675 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
], rec
, &rec2
,
1676 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
1677 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1678 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse dnsRecord for %s",
1679 ldb_dn_get_linearized(dn
));
1681 return ISC_R_FAILURE
;
1684 if (b9_record_match(state
, rec
, &rec2
)) {
1688 if (i
== el
->num_values
) {
1690 return ISC_R_NOTFOUND
;
1693 if (i
< el
->num_values
-1) {
1694 memmove(&el
->values
[i
], &el
->values
[i
+1], sizeof(el
->values
[0])*((el
->num_values
-1)-i
));
1698 if (!b9_set_session_info(state
, name
)) {
1700 return ISC_R_FAILURE
;
1703 if (el
->num_values
== 0) {
1704 el
->flags
= LDB_FLAG_MOD_DELETE
;
1706 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1708 ret
= ldb_modify(state
->samdb
, res
->msgs
[0]);
1710 b9_reset_session_info(state
);
1711 if (ret
!= LDB_SUCCESS
) {
1712 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to modify %s - %s",
1713 ldb_dn_get_linearized(dn
), ldb_errstring(state
->samdb
));
1715 return ISC_R_FAILURE
;
1718 state
->log(ISC_LOG_INFO
, "samba_dlz: subtracted rdataset %s '%s'", name
, rdatastr
);
1721 return ISC_R_SUCCESS
;
1726 delete all records of the given type
1728 _PUBLIC_ isc_result_t
dlz_delrdataset(const char *name
, const char *type
, void *dbdata
, void *version
)
1730 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1731 TALLOC_CTX
*tmp_ctx
;
1733 isc_result_t result
;
1734 struct ldb_result
*res
;
1735 const char *attrs
[] = { "dnsRecord", NULL
};
1737 struct ldb_message_element
*el
;
1738 enum ndr_err_code ndr_err
;
1739 enum dns_record_type dns_type
;
1742 if (state
->transaction_token
!= (void*)version
) {
1743 state
->log(ISC_LOG_ERROR
, "samba_dlz: bad transaction version");
1744 return ISC_R_FAILURE
;
1747 if (!b9_dns_type(type
, &dns_type
)) {
1748 state
->log(ISC_LOG_ERROR
, "samba_dlz: bad dns type %s in delete", type
);
1749 return ISC_R_FAILURE
;
1752 tmp_ctx
= talloc_new(state
);
1754 /* find the DN of the record */
1755 result
= b9_find_name_dn(state
, name
, tmp_ctx
, &dn
);
1756 if (result
!= ISC_R_SUCCESS
) {
1757 talloc_free(tmp_ctx
);
1761 /* get the existing records */
1762 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, "objectClass=dnsNode");
1763 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1764 talloc_free(tmp_ctx
);
1765 return ISC_R_NOTFOUND
;
1768 /* there are existing records. We need to see if any match the type
1770 el
= ldb_msg_find_element(res
->msgs
[0], "dnsRecord");
1771 if (el
== NULL
|| el
->num_values
== 0) {
1772 talloc_free(tmp_ctx
);
1773 return ISC_R_NOTFOUND
;
1776 for (i
=0; i
<el
->num_values
; i
++) {
1777 struct dnsp_DnssrvRpcRecord rec2
;
1779 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
], tmp_ctx
, &rec2
,
1780 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
1781 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1782 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse dnsRecord for %s",
1783 ldb_dn_get_linearized(dn
));
1784 talloc_free(tmp_ctx
);
1785 return ISC_R_FAILURE
;
1788 if (dns_type
== rec2
.wType
) {
1789 if (i
< el
->num_values
-1) {
1790 memmove(&el
->values
[i
], &el
->values
[i
+1],
1791 sizeof(el
->values
[0])*((el
->num_values
-1)-i
));
1800 talloc_free(tmp_ctx
);
1801 return ISC_R_FAILURE
;
1804 if (!b9_set_session_info(state
, name
)) {
1805 talloc_free(tmp_ctx
);
1806 return ISC_R_FAILURE
;
1809 if (el
->num_values
== 0) {
1810 el
->flags
= LDB_FLAG_MOD_DELETE
;
1812 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1814 ret
= ldb_modify(state
->samdb
, res
->msgs
[0]);
1816 b9_reset_session_info(state
);
1817 if (ret
!= LDB_SUCCESS
) {
1818 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to delete type %s in %s - %s",
1819 type
, ldb_dn_get_linearized(dn
), ldb_errstring(state
->samdb
));
1820 talloc_free(tmp_ctx
);
1821 return ISC_R_FAILURE
;
1824 state
->log(ISC_LOG_INFO
, "samba_dlz: deleted rdataset %s of type %s", name
, type
);
1826 talloc_free(tmp_ctx
);
1827 return ISC_R_SUCCESS
;