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/session.h"
29 #include "auth/gensec/gensec.h"
30 #include "gen_ndr/ndr_dnsp.h"
31 #include "lib/cmdline/popt_common.h"
32 #include "lib/cmdline/popt_credentials.h"
33 #include "ldb_module.h"
34 #include "dlz_minimal.h"
36 struct dlz_bind9_data
{
37 struct ldb_context
*samdb
;
38 struct tevent_context
*ev_ctx
;
39 struct loadparm_context
*lp
;
40 int *transaction_token
;
43 /* helper functions from the dlz_dlopen driver */
44 void (*log
)(int level
, const char *fmt
, ...);
45 isc_result_t (*putrr
)(dns_sdlzlookup_t
*handle
, const char *type
,
46 dns_ttl_t ttl
, const char *data
);
47 isc_result_t (*putnamedrr
)(dns_sdlzlookup_t
*handle
, const char *name
,
48 const char *type
, dns_ttl_t ttl
, const char *data
);
49 isc_result_t (*writeable_zone
)(dns_view_t
*view
, const char *zone_name
);
53 static const char *zone_prefixes
[] = {
54 "CN=MicrosoftDNS,DC=DomainDnsZones",
55 "CN=MicrosoftDNS,DC=ForestDnsZones",
60 return the version of the API
62 _PUBLIC_
int dlz_version(unsigned int *flags
)
64 return DLZ_DLOPEN_VERSION
;
68 remember a helper function from the bind9 dlz_dlopen driver
70 static void b9_add_helper(struct dlz_bind9_data
*state
, const char *helper_name
, void *ptr
)
72 if (strcmp(helper_name
, "log") == 0) {
75 if (strcmp(helper_name
, "putrr") == 0) {
78 if (strcmp(helper_name
, "putnamedrr") == 0) {
79 state
->putnamedrr
= ptr
;
81 if (strcmp(helper_name
, "writeable_zone") == 0) {
82 state
->writeable_zone
= ptr
;
87 format a record for bind9
89 static bool b9_format(struct dlz_bind9_data
*state
,
91 struct dnsp_DnssrvRpcRecord
*rec
,
92 const char **type
, const char **data
)
97 *data
= rec
->data
.ipv4
;
102 *data
= rec
->data
.ipv6
;
107 *data
= rec
->data
.cname
;
112 *data
= rec
->data
.txt
;
117 *data
= rec
->data
.ptr
;
122 *data
= talloc_asprintf(mem_ctx
, "%u %u %u %s",
123 rec
->data
.srv
.wPriority
,
124 rec
->data
.srv
.wWeight
,
126 rec
->data
.srv
.nameTarget
);
131 *data
= talloc_asprintf(mem_ctx
, "%u %s",
132 rec
->data
.mx
.wPriority
,
133 rec
->data
.mx
.nameTarget
);
138 *data
= talloc_asprintf(mem_ctx
, "%s %s",
145 *data
= rec
->data
.ns
;
152 /* we need to fake the authoritative nameserver to
153 * point at ourselves. This is how AD DNS servers
154 * force clients to send updates to the right local DC
156 mname
= talloc_asprintf(mem_ctx
, "%s.%s",
157 lpcfg_netbios_name(state
->lp
), lpcfg_dnsdomain(state
->lp
));
161 mname
= strlower_talloc(mem_ctx
, mname
);
166 state
->soa_serial
= rec
->data
.soa
.serial
;
168 *data
= talloc_asprintf(mem_ctx
, "%s %s %u %u %u %u %u",
171 rec
->data
.soa
.serial
,
172 rec
->data
.soa
.refresh
,
174 rec
->data
.soa
.expire
,
175 rec
->data
.soa
.minimum
);
180 state
->log(ISC_LOG_ERROR
, "samba b9_putrr: unhandled record type %u",
188 static const struct {
189 enum dns_record_type dns_type
;
193 { DNS_TYPE_A
, "A" , false},
194 { DNS_TYPE_AAAA
, "AAAA" , false},
195 { DNS_TYPE_CNAME
, "CNAME" , true},
196 { DNS_TYPE_TXT
, "TXT" , false},
197 { DNS_TYPE_PTR
, "PTR" , false},
198 { DNS_TYPE_SRV
, "SRV" , false},
199 { DNS_TYPE_MX
, "MX" , false},
200 { DNS_TYPE_HINFO
, "HINFO" , false},
201 { DNS_TYPE_NS
, "NS" , false},
202 { DNS_TYPE_SOA
, "SOA" , true},
207 see if a DNS type is single valued
209 static bool b9_single_valued(enum dns_record_type dns_type
)
212 for (i
=0; i
<ARRAY_SIZE(dns_typemap
); i
++) {
213 if (dns_typemap
[i
].dns_type
== dns_type
) {
214 return dns_typemap
[i
].single_valued
;
221 see if a DNS type is single valued
223 static bool b9_dns_type(const char *type
, enum dns_record_type
*dtype
)
226 for (i
=0; i
<ARRAY_SIZE(dns_typemap
); i
++) {
227 if (strcasecmp(dns_typemap
[i
].typestr
, type
) == 0) {
228 *dtype
= dns_typemap
[i
].dns_type
;
236 #define DNS_PARSE_STR(ret, str, sep, saveptr) do { \
237 (ret) = strtok_r(str, sep, &saveptr); \
238 if ((ret) == NULL) return false; \
241 #define DNS_PARSE_UINT(ret, str, sep, saveptr) do { \
242 char *istr = strtok_r(str, sep, &saveptr); \
243 if ((istr) == NULL) return false; \
244 (ret) = strtoul(istr, NULL, 10); \
248 parse a record from bind9
250 static bool b9_parse(struct dlz_bind9_data
*state
,
251 const char *rdatastr
,
252 struct dnsp_DnssrvRpcRecord
*rec
)
254 char *full_name
, *dclass
, *type
;
255 char *str
, *saveptr
=NULL
;
258 str
= talloc_strdup(rec
, rdatastr
);
263 /* parse the SDLZ string form */
264 DNS_PARSE_STR(full_name
, str
, "\t", saveptr
);
265 DNS_PARSE_UINT(rec
->dwTtlSeconds
, NULL
, "\t", saveptr
);
266 DNS_PARSE_STR(dclass
, NULL
, "\t", saveptr
);
267 DNS_PARSE_STR(type
, NULL
, "\t", saveptr
);
269 /* construct the record */
270 for (i
=0; i
<ARRAY_SIZE(dns_typemap
); i
++) {
271 if (strcasecmp(type
, dns_typemap
[i
].typestr
) == 0) {
272 rec
->wType
= dns_typemap
[i
].dns_type
;
276 if (i
== ARRAY_SIZE(dns_typemap
)) {
277 state
->log(ISC_LOG_ERROR
, "samba_dlz: unsupported record type '%s' for '%s'",
282 switch (rec
->wType
) {
284 DNS_PARSE_STR(rec
->data
.ipv4
, NULL
, " ", saveptr
);
288 DNS_PARSE_STR(rec
->data
.ipv6
, NULL
, " ", saveptr
);
292 DNS_PARSE_STR(rec
->data
.cname
, NULL
, " ", saveptr
);
296 DNS_PARSE_STR(rec
->data
.txt
, NULL
, "\t", saveptr
);
300 DNS_PARSE_STR(rec
->data
.ptr
, NULL
, " ", saveptr
);
304 DNS_PARSE_UINT(rec
->data
.srv
.wPriority
, NULL
, " ", saveptr
);
305 DNS_PARSE_UINT(rec
->data
.srv
.wWeight
, NULL
, " ", saveptr
);
306 DNS_PARSE_UINT(rec
->data
.srv
.wPort
, NULL
, " ", saveptr
);
307 DNS_PARSE_STR(rec
->data
.srv
.nameTarget
, NULL
, " ", saveptr
);
311 DNS_PARSE_UINT(rec
->data
.mx
.wPriority
, NULL
, " ", saveptr
);
312 DNS_PARSE_STR(rec
->data
.mx
.nameTarget
, NULL
, " ", saveptr
);
316 DNS_PARSE_STR(rec
->data
.hinfo
.cpu
, NULL
, " ", saveptr
);
317 DNS_PARSE_STR(rec
->data
.hinfo
.os
, NULL
, " ", saveptr
);
321 DNS_PARSE_STR(rec
->data
.ns
, NULL
, " ", saveptr
);
325 DNS_PARSE_STR(rec
->data
.soa
.mname
, NULL
, " ", saveptr
);
326 DNS_PARSE_STR(rec
->data
.soa
.rname
, NULL
, " ", saveptr
);
327 DNS_PARSE_UINT(rec
->data
.soa
.serial
, NULL
, " ", saveptr
);
328 DNS_PARSE_UINT(rec
->data
.soa
.refresh
, NULL
, " ", saveptr
);
329 DNS_PARSE_UINT(rec
->data
.soa
.retry
, NULL
, " ", saveptr
);
330 DNS_PARSE_UINT(rec
->data
.soa
.expire
, NULL
, " ", saveptr
);
331 DNS_PARSE_UINT(rec
->data
.soa
.minimum
, NULL
, " ", saveptr
);
335 state
->log(ISC_LOG_ERROR
, "samba b9_parse: unhandled record type %u",
340 /* we should be at the end of the buffer now */
341 if (strtok_r(NULL
, "\t ", &saveptr
) != NULL
) {
342 state
->log(ISC_LOG_ERROR
, "samba b9_parse: expected data at end of string for '%s'");
350 send a resource recond to bind9
352 static isc_result_t
b9_putrr(struct dlz_bind9_data
*state
,
353 void *handle
, struct dnsp_DnssrvRpcRecord
*rec
,
357 const char *type
, *data
;
358 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
360 if (!b9_format(state
, tmp_ctx
, rec
, &type
, &data
)) {
361 return ISC_R_FAILURE
;
365 talloc_free(tmp_ctx
);
366 return ISC_R_NOMEMORY
;
371 for (i
=0; types
[i
]; i
++) {
372 if (strcmp(types
[i
], type
) == 0) break;
374 if (types
[i
] == NULL
) {
376 return ISC_R_SUCCESS
;
380 result
= state
->putrr(handle
, type
, rec
->dwTtlSeconds
, data
);
381 if (result
!= ISC_R_SUCCESS
) {
382 state
->log(ISC_LOG_ERROR
, "Failed to put rr");
384 talloc_free(tmp_ctx
);
390 send a named resource recond to bind9
392 static isc_result_t
b9_putnamedrr(struct dlz_bind9_data
*state
,
393 void *handle
, const char *name
,
394 struct dnsp_DnssrvRpcRecord
*rec
)
397 const char *type
, *data
;
398 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
400 if (!b9_format(state
, tmp_ctx
, rec
, &type
, &data
)) {
401 return ISC_R_FAILURE
;
405 talloc_free(tmp_ctx
);
406 return ISC_R_NOMEMORY
;
409 result
= state
->putnamedrr(handle
, name
, type
, rec
->dwTtlSeconds
, data
);
410 if (result
!= ISC_R_SUCCESS
) {
411 state
->log(ISC_LOG_ERROR
, "Failed to put named rr '%s'", name
);
413 talloc_free(tmp_ctx
);
424 static isc_result_t
parse_options(struct dlz_bind9_data
*state
,
425 unsigned int argc
, char *argv
[],
426 struct b9_options
*options
)
430 struct poptOption long_options
[] = {
431 { "url", 'H', POPT_ARG_STRING
, &options
->url
, 0, "database URL", "URL" },
434 struct poptOption
**popt_options
;
437 fault_setup_disable();
439 popt_options
= ldb_module_popt_options(state
->samdb
);
440 (*popt_options
) = long_options
;
442 ret
= ldb_modules_hook(state
->samdb
, LDB_MODULE_HOOK_CMDLINE_OPTIONS
);
443 if (ret
!= LDB_SUCCESS
) {
444 state
->log(ISC_LOG_ERROR
, "dlz samba: failed cmdline hook");
445 return ISC_R_FAILURE
;
448 pc
= poptGetContext("dlz_bind9", argc
, (const char **)argv
, *popt_options
,
449 POPT_CONTEXT_KEEP_FIRST
);
451 while ((opt
= poptGetNextOpt(pc
)) != -1) {
454 state
->log(ISC_LOG_ERROR
, "dlz samba: Invalid option %s: %s",
455 poptBadOption(pc
, 0), poptStrerror(opt
));
456 return ISC_R_FAILURE
;
460 ret
= ldb_modules_hook(state
->samdb
, LDB_MODULE_HOOK_CMDLINE_PRECONNECT
);
461 if (ret
!= LDB_SUCCESS
) {
462 state
->log(ISC_LOG_ERROR
, "dlz samba: failed cmdline preconnect");
463 return ISC_R_FAILURE
;
466 return ISC_R_SUCCESS
;
471 called to initialise the driver
473 _PUBLIC_ isc_result_t
dlz_create(const char *dlzname
,
474 unsigned int argc
, char *argv
[],
477 struct dlz_bind9_data
*state
;
478 const char *helper_name
;
484 struct b9_options options
;
486 ZERO_STRUCT(options
);
488 state
= talloc_zero(NULL
, struct dlz_bind9_data
);
490 return ISC_R_NOMEMORY
;
493 tmp_ctx
= talloc_new(state
);
495 /* fill in the helper functions */
496 va_start(ap
, dbdata
);
497 while ((helper_name
= va_arg(ap
, const char *)) != NULL
) {
498 b9_add_helper(state
, helper_name
, va_arg(ap
, void*));
502 state
->ev_ctx
= s4_event_context_init(state
);
503 if (state
->ev_ctx
== NULL
) {
504 result
= ISC_R_NOMEMORY
;
508 state
->samdb
= ldb_init(state
, state
->ev_ctx
);
509 if (state
->samdb
== NULL
) {
510 state
->log(ISC_LOG_ERROR
, "samba_dlz: Failed to create ldb");
511 result
= ISC_R_FAILURE
;
515 result
= parse_options(state
, argc
, argv
, &options
);
516 if (result
!= ISC_R_SUCCESS
) {
520 state
->lp
= loadparm_init_global(true);
521 if (state
->lp
== NULL
) {
522 result
= ISC_R_NOMEMORY
;
526 if (options
.url
== NULL
) {
527 options
.url
= talloc_asprintf(tmp_ctx
, "ldapi://%s",
528 private_path(tmp_ctx
, state
->lp
, "ldap_priv/ldapi"));
529 if (options
.url
== NULL
) {
530 result
= ISC_R_NOMEMORY
;
535 ret
= ldb_connect(state
->samdb
, options
.url
, 0, NULL
);
537 state
->log(ISC_LOG_ERROR
, "samba_dlz: Failed to connect to %s - %s",
538 options
.url
, ldb_errstring(state
->samdb
));
539 result
= ISC_R_FAILURE
;
543 ret
= ldb_modules_hook(state
->samdb
, LDB_MODULE_HOOK_CMDLINE_POSTCONNECT
);
544 if (ret
!= LDB_SUCCESS
) {
545 state
->log(ISC_LOG_ERROR
, "samba_dlz: Failed postconnect for %s - %s",
546 options
.url
, ldb_errstring(state
->samdb
));
547 result
= ISC_R_FAILURE
;
551 dn
= ldb_get_default_basedn(state
->samdb
);
553 state
->log(ISC_LOG_ERROR
, "samba_dlz: Unable to get basedn for %s - %s",
554 options
.url
, ldb_errstring(state
->samdb
));
555 result
= ISC_R_FAILURE
;
559 state
->log(ISC_LOG_INFO
, "samba_dlz: started for DN %s",
560 ldb_dn_get_linearized(dn
));
564 talloc_free(tmp_ctx
);
565 return ISC_R_SUCCESS
;
575 _PUBLIC_
void dlz_destroy(void *dbdata
)
577 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
578 state
->log(ISC_LOG_INFO
, "samba_dlz: shutting down");
584 return the base DN for a zone
586 static isc_result_t
b9_find_zone_dn(struct dlz_bind9_data
*state
, const char *zone_name
,
587 TALLOC_CTX
*mem_ctx
, struct ldb_dn
**zone_dn
)
590 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
591 const char *attrs
[] = { NULL
};
594 for (i
=0; zone_prefixes
[i
]; i
++) {
596 struct ldb_result
*res
;
598 dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(state
->samdb
));
600 talloc_free(tmp_ctx
);
601 return ISC_R_NOMEMORY
;
604 if (!ldb_dn_add_child_fmt(dn
, "DC=%s,%s", zone_name
, zone_prefixes
[i
])) {
605 talloc_free(tmp_ctx
);
606 return ISC_R_NOMEMORY
;
609 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, "objectClass=dnsZone");
610 if (ret
== LDB_SUCCESS
) {
611 if (zone_dn
!= NULL
) {
612 *zone_dn
= talloc_steal(mem_ctx
, dn
);
614 talloc_free(tmp_ctx
);
615 return ISC_R_SUCCESS
;
620 talloc_free(tmp_ctx
);
621 return ISC_R_NOTFOUND
;
626 return the DN for a name. The record does not need to exist, but the
629 static isc_result_t
b9_find_name_dn(struct dlz_bind9_data
*state
, const char *name
,
630 TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
634 /* work through the name piece by piece, until we find a zone */
637 result
= b9_find_zone_dn(state
, p
, mem_ctx
, dn
);
638 if (result
== ISC_R_SUCCESS
) {
639 /* we found a zone, now extend the DN to get
644 ret
= ldb_dn_add_child_fmt(*dn
, "DC=@");
646 ret
= ldb_dn_add_child_fmt(*dn
, "DC=%.*s", (int)(p
-name
)-1, name
);
650 return ISC_R_NOMEMORY
;
652 return ISC_R_SUCCESS
;
660 return ISC_R_NOTFOUND
;
665 see if we handle a given zone
667 _PUBLIC_ isc_result_t
dlz_findzonedb(void *dbdata
, const char *name
)
669 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
670 return b9_find_zone_dn(state
, name
, NULL
, NULL
);
677 static isc_result_t
dlz_lookup_types(struct dlz_bind9_data
*state
,
678 const char *zone
, const char *name
,
679 dns_sdlzlookup_t
*lookup
,
682 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
683 const char *attrs
[] = { "dnsRecord", NULL
};
684 int ret
= LDB_SUCCESS
, i
;
685 struct ldb_result
*res
;
686 struct ldb_message_element
*el
;
689 for (i
=0; zone_prefixes
[i
]; i
++) {
690 dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(state
->samdb
));
692 talloc_free(tmp_ctx
);
693 return ISC_R_NOMEMORY
;
696 if (!ldb_dn_add_child_fmt(dn
, "DC=%s,DC=%s,%s", name
, zone
, zone_prefixes
[i
])) {
697 talloc_free(tmp_ctx
);
698 return ISC_R_NOMEMORY
;
701 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
,
702 attrs
, "objectClass=dnsNode");
703 if (ret
== LDB_SUCCESS
) {
707 if (ret
!= LDB_SUCCESS
) {
708 talloc_free(tmp_ctx
);
709 return ISC_R_NOTFOUND
;
712 el
= ldb_msg_find_element(res
->msgs
[0], "dnsRecord");
713 if (el
== NULL
|| el
->num_values
== 0) {
714 talloc_free(tmp_ctx
);
715 return ISC_R_NOTFOUND
;
718 for (i
=0; i
<el
->num_values
; i
++) {
719 struct dnsp_DnssrvRpcRecord rec
;
720 enum ndr_err_code ndr_err
;
723 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
], tmp_ctx
, &rec
,
724 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
725 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
726 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse dnsRecord for %s",
727 ldb_dn_get_linearized(dn
));
728 talloc_free(tmp_ctx
);
729 return ISC_R_FAILURE
;
732 result
= b9_putrr(state
, lookup
, &rec
, types
);
733 if (result
!= ISC_R_SUCCESS
) {
734 talloc_free(tmp_ctx
);
739 talloc_free(tmp_ctx
);
740 return ISC_R_SUCCESS
;
746 _PUBLIC_ isc_result_t
dlz_lookup(const char *zone
, const char *name
,
747 void *dbdata
, dns_sdlzlookup_t
*lookup
)
749 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
750 return dlz_lookup_types(state
, zone
, name
, lookup
, NULL
);
755 see if a zone transfer is allowed
757 _PUBLIC_ isc_result_t
dlz_allowzonexfr(void *dbdata
, const char *name
, const char *client
)
759 /* just say yes for all our zones for now */
760 return dlz_findzonedb(dbdata
, name
);
764 perform a zone transfer
766 _PUBLIC_ isc_result_t
dlz_allnodes(const char *zone
, void *dbdata
,
767 dns_sdlzallnodes_t
*allnodes
)
769 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
770 const char *attrs
[] = { "dnsRecord", NULL
};
771 int ret
= LDB_SUCCESS
, i
, j
;
773 struct ldb_result
*res
;
774 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
776 for (i
=0; zone_prefixes
[i
]; i
++) {
777 dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(state
->samdb
));
779 talloc_free(tmp_ctx
);
780 return ISC_R_NOMEMORY
;
783 if (!ldb_dn_add_child_fmt(dn
, "DC=%s,%s", zone
, zone_prefixes
[i
])) {
784 talloc_free(tmp_ctx
);
785 return ISC_R_NOMEMORY
;
788 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_SUBTREE
,
789 attrs
, "objectClass=dnsNode");
790 if (ret
== LDB_SUCCESS
) {
794 if (ret
!= LDB_SUCCESS
) {
795 talloc_free(tmp_ctx
);
796 return ISC_R_NOTFOUND
;
799 for (i
=0; i
<res
->count
; i
++) {
800 struct ldb_message_element
*el
;
801 TALLOC_CTX
*el_ctx
= talloc_new(tmp_ctx
);
802 const char *rdn
, *name
;
803 const struct ldb_val
*v
;
805 el
= ldb_msg_find_element(res
->msgs
[i
], "dnsRecord");
806 if (el
== NULL
|| el
->num_values
== 0) {
807 state
->log(ISC_LOG_INFO
, "failed to find dnsRecord for %s",
808 ldb_dn_get_linearized(dn
));
813 v
= ldb_dn_get_rdn_val(res
->msgs
[i
]->dn
);
815 state
->log(ISC_LOG_INFO
, "failed to find RDN for %s",
816 ldb_dn_get_linearized(dn
));
821 rdn
= talloc_strndup(el_ctx
, (char *)v
->data
, v
->length
);
823 talloc_free(tmp_ctx
);
824 return ISC_R_NOMEMORY
;
827 if (strcmp(rdn
, "@") == 0) {
830 name
= talloc_asprintf(el_ctx
, "%s.%s", rdn
, zone
);
833 talloc_free(tmp_ctx
);
834 return ISC_R_NOMEMORY
;
837 for (j
=0; j
<el
->num_values
; j
++) {
838 struct dnsp_DnssrvRpcRecord rec
;
839 enum ndr_err_code ndr_err
;
842 ndr_err
= ndr_pull_struct_blob(&el
->values
[j
], el_ctx
, &rec
,
843 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
844 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
845 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse dnsRecord for %s",
846 ldb_dn_get_linearized(dn
));
850 result
= b9_putnamedrr(state
, allnodes
, name
, &rec
);
851 if (result
!= ISC_R_SUCCESS
) {
857 talloc_free(tmp_ctx
);
859 return ISC_R_SUCCESS
;
866 _PUBLIC_ isc_result_t
dlz_newversion(const char *zone
, void *dbdata
, void **versionp
)
868 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
870 state
->log(ISC_LOG_INFO
, "samba_dlz: starting transaction on zone %s", zone
);
872 if (state
->transaction_token
!= NULL
) {
873 state
->log(ISC_LOG_INFO
, "samba_dlz: transaction already started for zone %s", zone
);
874 return ISC_R_FAILURE
;
877 state
->transaction_token
= talloc_zero(state
, int);
878 if (state
->transaction_token
== NULL
) {
879 return ISC_R_NOMEMORY
;
882 if (ldb_transaction_start(state
->samdb
) != LDB_SUCCESS
) {
883 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to start a transaction for zone %s", zone
);
884 talloc_free(state
->transaction_token
);
885 state
->transaction_token
= NULL
;
886 return ISC_R_FAILURE
;
889 *versionp
= (void *)state
->transaction_token
;
891 return ISC_R_SUCCESS
;
897 _PUBLIC_
void dlz_closeversion(const char *zone
, isc_boolean_t commit
,
898 void *dbdata
, void **versionp
)
900 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
902 if (state
->transaction_token
!= (int *)*versionp
) {
903 state
->log(ISC_LOG_INFO
, "samba_dlz: transaction not started for zone %s", zone
);
908 if (ldb_transaction_commit(state
->samdb
) != LDB_SUCCESS
) {
909 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to commit a transaction for zone %s", zone
);
912 state
->log(ISC_LOG_INFO
, "samba_dlz: committed transaction on zone %s", zone
);
914 if (ldb_transaction_cancel(state
->samdb
) != LDB_SUCCESS
) {
915 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to cancel a transaction for zone %s", zone
);
918 state
->log(ISC_LOG_INFO
, "samba_dlz: cancelling transaction on zone %s", zone
);
921 talloc_free(state
->transaction_token
);
922 state
->transaction_token
= NULL
;
928 see if there is a SOA record for a zone
930 static bool b9_has_soa(struct dlz_bind9_data
*state
, struct ldb_dn
*dn
, const char *zone
)
932 const char *attrs
[] = { "dnsRecord", NULL
};
933 struct ldb_result
*res
;
934 struct ldb_message_element
*el
;
935 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
938 if (!ldb_dn_add_child_fmt(dn
, "DC=@,DC=%s", zone
)) {
939 talloc_free(tmp_ctx
);
943 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
,
944 attrs
, "objectClass=dnsNode");
945 if (ret
!= LDB_SUCCESS
) {
946 talloc_free(tmp_ctx
);
950 el
= ldb_msg_find_element(res
->msgs
[0], "dnsRecord");
952 talloc_free(tmp_ctx
);
955 for (i
=0; i
<el
->num_values
; i
++) {
956 struct dnsp_DnssrvRpcRecord rec
;
957 enum ndr_err_code ndr_err
;
959 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
], tmp_ctx
, &rec
,
960 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
961 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
964 if (rec
.wType
== DNS_TYPE_SOA
) {
965 talloc_free(tmp_ctx
);
970 talloc_free(tmp_ctx
);
975 configure a writeable zone
977 _PUBLIC_ isc_result_t
dlz_configure(dns_view_t
*view
, void *dbdata
)
979 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
984 state
->log(ISC_LOG_INFO
, "samba_dlz: starting configure");
985 if (state
->writeable_zone
== NULL
) {
986 state
->log(ISC_LOG_INFO
, "samba_dlz: no writeable_zone method available");
987 return ISC_R_FAILURE
;
990 tmp_ctx
= talloc_new(state
);
992 for (i
=0; zone_prefixes
[i
]; i
++) {
993 const char *attrs
[] = { "name", NULL
};
995 struct ldb_result
*res
;
997 dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(state
->samdb
));
999 talloc_free(tmp_ctx
);
1000 return ISC_R_NOMEMORY
;
1003 if (!ldb_dn_add_child_fmt(dn
, "%s", zone_prefixes
[i
])) {
1004 talloc_free(tmp_ctx
);
1005 return ISC_R_NOMEMORY
;
1008 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_SUBTREE
,
1009 attrs
, "objectClass=dnsZone");
1010 if (ret
!= LDB_SUCCESS
) {
1014 for (j
=0; j
<res
->count
; j
++) {
1015 isc_result_t result
;
1016 const char *zone
= ldb_msg_find_attr_as_string(res
->msgs
[j
], "name", NULL
);
1020 if (!b9_has_soa(state
, dn
, zone
)) {
1023 result
= state
->writeable_zone(view
, zone
);
1024 if (result
!= ISC_R_SUCCESS
) {
1025 state
->log(ISC_LOG_ERROR
, "samba_dlz: Failed to configure zone '%s'",
1027 talloc_free(tmp_ctx
);
1030 state
->log(ISC_LOG_INFO
, "samba_dlz: configured writeable zone '%s'", zone
);
1034 talloc_free(tmp_ctx
);
1035 return ISC_R_SUCCESS
;
1039 authorize a zone update
1041 _PUBLIC_ isc_boolean_t
dlz_ssumatch(const char *signer
, const char *name
, const char *tcpaddr
,
1042 const char *type
, const char *key
, uint32_t keydatalen
, uint8_t *keydata
,
1045 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1047 state
->log(ISC_LOG_INFO
, "samba_dlz: allowing update of signer=%s name=%s tcpaddr=%s type=%s key=%s keydatalen=%u",
1048 signer
, name
, tcpaddr
, type
, key
, keydatalen
);
1056 static isc_result_t
b9_add_record(struct dlz_bind9_data
*state
, const char *name
,
1058 struct dnsp_DnssrvRpcRecord
*rec
)
1060 struct ldb_message
*msg
;
1061 enum ndr_err_code ndr_err
;
1065 msg
= ldb_msg_new(rec
);
1067 return ISC_R_NOMEMORY
;
1070 ret
= ldb_msg_add_string(msg
, "objectClass", "dnsNode");
1071 if (ret
!= LDB_SUCCESS
) {
1072 return ISC_R_FAILURE
;
1075 ndr_err
= ndr_push_struct_blob(&v
, rec
, rec
, (ndr_push_flags_fn_t
)ndr_push_dnsp_DnssrvRpcRecord
);
1076 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1077 return ISC_R_FAILURE
;
1079 ret
= ldb_msg_add_value(msg
, "dnsRecord", &v
, NULL
);
1080 if (ret
!= LDB_SUCCESS
) {
1081 return ISC_R_FAILURE
;
1084 ret
= ldb_add(state
->samdb
, msg
);
1085 if (ret
!= LDB_SUCCESS
) {
1086 return ISC_R_FAILURE
;
1089 return ISC_R_SUCCESS
;
1093 see if two DNS names are the same
1095 static bool dns_name_equal(const char *name1
, const char *name2
)
1097 size_t len1
= strlen(name1
);
1098 size_t len2
= strlen(name2
);
1099 if (name1
[len1
-1] == '.') len1
--;
1100 if (name2
[len2
-1] == '.') len2
--;
1104 return strncasecmp_m(name1
, name2
, len1
) == 0;
1109 see if two dns records match
1111 static bool b9_record_match(struct dlz_bind9_data
*state
,
1112 struct dnsp_DnssrvRpcRecord
*rec1
, struct dnsp_DnssrvRpcRecord
*rec2
)
1114 if (rec1
->wType
!= rec2
->wType
) {
1117 /* see if this type is single valued */
1118 if (b9_single_valued(rec1
->wType
)) {
1122 /* see if the data matches */
1123 switch (rec1
->wType
) {
1125 return strcmp(rec1
->data
.ipv4
, rec2
->data
.ipv4
) == 0;
1127 return strcmp(rec1
->data
.ipv6
, rec2
->data
.ipv6
) == 0;
1128 case DNS_TYPE_CNAME
:
1129 return dns_name_equal(rec1
->data
.cname
, rec2
->data
.cname
);
1131 return strcmp(rec1
->data
.txt
, rec2
->data
.txt
) == 0;
1133 return strcmp(rec1
->data
.ptr
, rec2
->data
.ptr
) == 0;
1135 return dns_name_equal(rec1
->data
.ns
, rec2
->data
.ns
);
1138 return rec1
->data
.srv
.wPriority
== rec2
->data
.srv
.wPriority
&&
1139 rec1
->data
.srv
.wWeight
== rec2
->data
.srv
.wWeight
&&
1140 rec1
->data
.srv
.wPort
== rec2
->data
.srv
.wPort
&&
1141 dns_name_equal(rec1
->data
.srv
.nameTarget
, rec2
->data
.srv
.nameTarget
);
1144 return rec1
->data
.mx
.wPriority
== rec2
->data
.mx
.wPriority
&&
1145 dns_name_equal(rec1
->data
.mx
.nameTarget
, rec2
->data
.mx
.nameTarget
);
1147 case DNS_TYPE_HINFO
:
1148 return strcmp(rec1
->data
.hinfo
.cpu
, rec2
->data
.hinfo
.cpu
) == 0 &&
1149 strcmp(rec1
->data
.hinfo
.os
, rec2
->data
.hinfo
.os
) == 0;
1152 return dns_name_equal(rec1
->data
.soa
.mname
, rec2
->data
.soa
.mname
) &&
1153 dns_name_equal(rec1
->data
.soa
.rname
, rec2
->data
.soa
.rname
) &&
1154 rec1
->data
.soa
.serial
== rec2
->data
.soa
.serial
&&
1155 rec1
->data
.soa
.refresh
== rec2
->data
.soa
.refresh
&&
1156 rec1
->data
.soa
.retry
== rec2
->data
.soa
.retry
&&
1157 rec1
->data
.soa
.expire
== rec2
->data
.soa
.expire
&&
1158 rec1
->data
.soa
.minimum
== rec2
->data
.soa
.minimum
;
1160 state
->log(ISC_LOG_ERROR
, "samba b9_putrr: unhandled record type %u",
1170 add or modify a rdataset
1172 _PUBLIC_ isc_result_t
dlz_addrdataset(const char *name
, const char *rdatastr
, void *dbdata
, void *version
)
1174 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1175 struct dnsp_DnssrvRpcRecord
*rec
;
1177 isc_result_t result
;
1178 struct ldb_result
*res
;
1179 const char *attrs
[] = { "dnsRecord", NULL
};
1181 struct ldb_message_element
*el
;
1182 enum ndr_err_code ndr_err
;
1185 if (state
->transaction_token
!= (void*)version
) {
1186 state
->log(ISC_LOG_INFO
, "samba_dlz: bad transaction version");
1187 return ISC_R_FAILURE
;
1190 rec
= talloc_zero(state
, struct dnsp_DnssrvRpcRecord
);
1192 return ISC_R_NOMEMORY
;
1195 unix_to_nt_time(&t
, time(NULL
));
1196 t
/= 10*1000*1000; /* convert to seconds (NT time is in 100ns units) */
1197 t
/= 3600; /* convert to hours */
1199 rec
->rank
= DNS_RANK_ZONE
;
1200 rec
->dwSerial
= state
->soa_serial
;
1201 rec
->dwTimeStamp
= (uint32_t)t
;
1203 if (!b9_parse(state
, rdatastr
, rec
)) {
1204 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to parse rdataset '%s'", rdatastr
);
1206 return ISC_R_FAILURE
;
1209 /* find the DN of the record */
1210 result
= b9_find_name_dn(state
, name
, rec
, &dn
);
1211 if (result
!= ISC_R_SUCCESS
) {
1216 /* get any existing records */
1217 ret
= ldb_search(state
->samdb
, rec
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, "objectClass=dnsNode");
1218 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1219 result
= b9_add_record(state
, name
, dn
, rec
);
1221 if (result
== ISC_R_SUCCESS
) {
1222 state
->log(ISC_LOG_ERROR
, "samba_dlz: added %s %s", name
, rdatastr
);
1227 /* there are existing records. We need to see if this will
1228 * replace a record or add to it
1230 el
= ldb_msg_find_element(res
->msgs
[0], "dnsRecord");
1232 state
->log(ISC_LOG_ERROR
, "samba_dlz: no dnsRecord attribute for %s",
1233 ldb_dn_get_linearized(dn
));
1235 return ISC_R_FAILURE
;
1238 for (i
=0; i
<el
->num_values
; i
++) {
1239 struct dnsp_DnssrvRpcRecord rec2
;
1241 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
], rec
, &rec2
,
1242 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
1243 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1244 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse dnsRecord for %s",
1245 ldb_dn_get_linearized(dn
));
1247 return ISC_R_FAILURE
;
1250 if (b9_record_match(state
, rec
, &rec2
)) {
1254 if (i
== el
->num_values
) {
1255 /* adding a new value */
1256 el
->values
= talloc_realloc(el
, el
->values
, struct ldb_val
, el
->num_values
+1);
1257 if (el
->values
== NULL
) {
1259 return ISC_R_NOMEMORY
;
1264 ndr_err
= ndr_push_struct_blob(&el
->values
[i
], rec
, rec
,
1265 (ndr_push_flags_fn_t
)ndr_push_dnsp_DnssrvRpcRecord
);
1266 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1267 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to push dnsRecord for %s",
1268 ldb_dn_get_linearized(dn
));
1270 return ISC_R_FAILURE
;
1273 /* modify the record */
1274 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1275 ret
= ldb_modify(state
->samdb
, res
->msgs
[0]);
1276 if (ret
!= LDB_SUCCESS
) {
1277 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to modify %s - %s",
1278 ldb_dn_get_linearized(dn
), ldb_errstring(state
->samdb
));
1280 return ISC_R_FAILURE
;
1283 state
->log(ISC_LOG_INFO
, "samba_dlz: added rdataset %s '%s'", name
, rdatastr
);
1286 return ISC_R_SUCCESS
;
1292 _PUBLIC_ isc_result_t
dlz_subrdataset(const char *name
, const char *rdatastr
, void *dbdata
, void *version
)
1294 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1295 struct dnsp_DnssrvRpcRecord
*rec
;
1297 isc_result_t result
;
1298 struct ldb_result
*res
;
1299 const char *attrs
[] = { "dnsRecord", NULL
};
1301 struct ldb_message_element
*el
;
1302 enum ndr_err_code ndr_err
;
1304 if (state
->transaction_token
!= (void*)version
) {
1305 state
->log(ISC_LOG_INFO
, "samba_dlz: bad transaction version");
1306 return ISC_R_FAILURE
;
1309 rec
= talloc_zero(state
, struct dnsp_DnssrvRpcRecord
);
1311 return ISC_R_NOMEMORY
;
1314 if (!b9_parse(state
, rdatastr
, rec
)) {
1315 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to parse rdataset '%s'", rdatastr
);
1317 return ISC_R_FAILURE
;
1320 /* find the DN of the record */
1321 result
= b9_find_name_dn(state
, name
, rec
, &dn
);
1322 if (result
!= ISC_R_SUCCESS
) {
1327 /* get the existing records */
1328 ret
= ldb_search(state
->samdb
, rec
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, "objectClass=dnsNode");
1329 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1331 return ISC_R_NOTFOUND
;
1334 /* there are existing records. We need to see if any match
1336 el
= ldb_msg_find_element(res
->msgs
[0], "dnsRecord");
1337 if (el
== NULL
|| el
->num_values
== 0) {
1338 state
->log(ISC_LOG_ERROR
, "samba_dlz: no dnsRecord attribute for %s",
1339 ldb_dn_get_linearized(dn
));
1341 return ISC_R_FAILURE
;
1344 for (i
=0; i
<el
->num_values
; i
++) {
1345 struct dnsp_DnssrvRpcRecord rec2
;
1347 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
], rec
, &rec2
,
1348 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
1349 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1350 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse dnsRecord for %s",
1351 ldb_dn_get_linearized(dn
));
1353 return ISC_R_FAILURE
;
1356 if (b9_record_match(state
, rec
, &rec2
)) {
1360 if (i
== el
->num_values
) {
1362 return ISC_R_NOTFOUND
;
1365 if (i
< el
->num_values
-1) {
1366 memmove(&el
->values
[i
], &el
->values
[i
+1], sizeof(el
->values
[0])*((el
->num_values
-1)-i
));
1370 if (el
->num_values
== 0) {
1371 /* delete the record */
1372 ret
= ldb_delete(state
->samdb
, dn
);
1374 /* modify the record */
1375 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1376 ret
= ldb_modify(state
->samdb
, res
->msgs
[0]);
1378 if (ret
!= LDB_SUCCESS
) {
1379 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to modify %s - %s",
1380 ldb_dn_get_linearized(dn
), ldb_errstring(state
->samdb
));
1382 return ISC_R_FAILURE
;
1385 state
->log(ISC_LOG_INFO
, "samba_dlz: subtracted rdataset %s '%s'", name
, rdatastr
);
1388 return ISC_R_SUCCESS
;
1393 delete all records of the given type
1395 _PUBLIC_ isc_result_t
dlz_delrdataset(const char *name
, const char *type
, void *dbdata
, void *version
)
1397 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1398 TALLOC_CTX
*tmp_ctx
;
1400 isc_result_t result
;
1401 struct ldb_result
*res
;
1402 const char *attrs
[] = { "dnsRecord", NULL
};
1404 struct ldb_message_element
*el
;
1405 enum ndr_err_code ndr_err
;
1406 enum dns_record_type dns_type
;
1409 if (state
->transaction_token
!= (void*)version
) {
1410 state
->log(ISC_LOG_INFO
, "samba_dlz: bad transaction version");
1411 return ISC_R_FAILURE
;
1414 if (!b9_dns_type(type
, &dns_type
)) {
1415 state
->log(ISC_LOG_INFO
, "samba_dlz: bad dns type %s in delete", type
);
1416 return ISC_R_FAILURE
;
1419 tmp_ctx
= talloc_new(state
);
1421 /* find the DN of the record */
1422 result
= b9_find_name_dn(state
, name
, tmp_ctx
, &dn
);
1423 if (result
!= ISC_R_SUCCESS
) {
1424 talloc_free(tmp_ctx
);
1428 /* get the existing records */
1429 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, "objectClass=dnsNode");
1430 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1431 talloc_free(tmp_ctx
);
1432 return ISC_R_NOTFOUND
;
1435 /* there are existing records. We need to see if any match the type
1437 el
= ldb_msg_find_element(res
->msgs
[0], "dnsRecord");
1438 if (el
== NULL
|| el
->num_values
== 0) {
1439 talloc_free(tmp_ctx
);
1440 return ISC_R_NOTFOUND
;
1443 for (i
=0; i
<el
->num_values
; i
++) {
1444 struct dnsp_DnssrvRpcRecord rec2
;
1446 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
], tmp_ctx
, &rec2
,
1447 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
1448 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1449 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse dnsRecord for %s",
1450 ldb_dn_get_linearized(dn
));
1451 talloc_free(tmp_ctx
);
1452 return ISC_R_FAILURE
;
1455 if (dns_type
== rec2
.wType
) {
1456 if (i
< el
->num_values
-1) {
1457 memmove(&el
->values
[i
], &el
->values
[i
+1],
1458 sizeof(el
->values
[0])*((el
->num_values
-1)-i
));
1467 talloc_free(tmp_ctx
);
1468 return ISC_R_FAILURE
;
1471 if (el
->num_values
== 0) {
1472 /* delete the record */
1473 ret
= ldb_delete(state
->samdb
, dn
);
1475 /* modify the record */
1476 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1477 ret
= ldb_modify(state
->samdb
, res
->msgs
[0]);
1479 if (ret
!= LDB_SUCCESS
) {
1480 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to delete type %s in %s - %s",
1481 type
, ldb_dn_get_linearized(dn
), ldb_errstring(state
->samdb
));
1482 talloc_free(tmp_ctx
);
1483 return ISC_R_FAILURE
;
1486 state
->log(ISC_LOG_INFO
, "samba_dlz: deleted rdataset %s of type %s", name
, type
);
1488 talloc_free(tmp_ctx
);
1489 return ISC_R_SUCCESS
;