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 "dlz_minimal.h"
33 struct dlz_bind9_data {
34 struct ldb_context *samdb;
35 struct tevent_context *ev_ctx;
36 struct loadparm_context *lp;
37 int *transaction_token;
40 /* helper functions from the dlz_dlopen driver */
41 void (*log)(int level, const char *fmt, ...);
42 isc_result_t (*putrr)(dns_sdlzlookup_t *handle, const char *type,
43 dns_ttl_t ttl, const char *data);
44 isc_result_t (*putnamedrr)(dns_sdlzlookup_t *handle, const char *name,
45 const char *type, dns_ttl_t ttl, const char *data);
46 isc_result_t (*writeable_zone)(dns_view_t *view, const char *zone_name);
50 static const char *zone_prefixes[] = {
51 "CN=MicrosoftDNS,DC=DomainDnsZones",
52 "CN=MicrosoftDNS,DC=ForestDnsZones",
53 "CN=MicrosoftDNS,CN=System",
58 return the version of the API
60 _PUBLIC_ int dlz_version(unsigned int *flags)
62 return DLZ_DLOPEN_VERSION;
66 remember a helper function from the bind9 dlz_dlopen driver
68 static void b9_add_helper(struct dlz_bind9_data *state, const char *helper_name, void *ptr)
70 if (strcmp(helper_name, "log") == 0) {
73 if (strcmp(helper_name, "putrr") == 0) {
76 if (strcmp(helper_name, "putnamedrr") == 0) {
77 state->putnamedrr = ptr;
79 if (strcmp(helper_name, "writeable_zone") == 0) {
80 state->writeable_zone = ptr;
85 format a record for bind9
87 static bool b9_format(struct dlz_bind9_data *state,
89 struct dnsp_DnssrvRpcRecord *rec,
90 const char **type, const char **data)
95 *data = rec->data.ipv4;
100 *data = rec->data.ipv6;
105 *data = rec->data.cname;
110 *data = rec->data.txt;
115 *data = rec->data.ptr;
120 *data = talloc_asprintf(mem_ctx, "%u %u %u %s",
121 rec->data.srv.wPriority,
122 rec->data.srv.wWeight,
124 rec->data.srv.nameTarget);
129 *data = talloc_asprintf(mem_ctx, "%u %s",
130 rec->data.mx.wPriority,
131 rec->data.mx.nameTarget);
136 *data = talloc_asprintf(mem_ctx, "%s %s",
143 *data = rec->data.ns;
150 /* we need to fake the authoritative nameserver to
151 * point at ourselves. This is how AD DNS servers
152 * force clients to send updates to the right local DC
154 mname = talloc_asprintf(mem_ctx, "%s.%s",
155 lpcfg_netbios_name(state->lp), lpcfg_dnsdomain(state->lp));
159 mname = strlower_talloc(mem_ctx, mname);
164 state->soa_serial = rec->data.soa.serial;
166 *data = talloc_asprintf(mem_ctx, "%s %s %u %u %u %u %u",
169 rec->data.soa.serial,
170 rec->data.soa.refresh,
172 rec->data.soa.expire,
173 rec->data.soa.minimum);
178 state->log(ISC_LOG_ERROR, "samba b9_putrr: unhandled record type %u",
186 static const struct {
187 enum dns_record_type dns_type;
191 { DNS_TYPE_A, "A" , false},
192 { DNS_TYPE_AAAA, "AAAA" , false},
193 { DNS_TYPE_CNAME, "CNAME" , true},
194 { DNS_TYPE_TXT, "TXT" , false},
195 { DNS_TYPE_PTR, "PTR" , false},
196 { DNS_TYPE_SRV, "SRV" , false},
197 { DNS_TYPE_MX, "MX" , false},
198 { DNS_TYPE_HINFO, "HINFO" , false},
199 { DNS_TYPE_NS, "NS" , false},
200 { DNS_TYPE_SOA, "SOA" , true},
205 see if a DNS type is single valued
207 static bool b9_single_valued(enum dns_record_type dns_type)
210 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
211 if (dns_typemap[i].dns_type == dns_type) {
212 return dns_typemap[i].single_valued;
219 see if a DNS type is single valued
221 static bool b9_dns_type(const char *type, enum dns_record_type *dtype)
224 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
225 if (strcasecmp(dns_typemap[i].typestr, type) == 0) {
226 *dtype = dns_typemap[i].dns_type;
234 #define DNS_PARSE_STR(ret, str, sep, saveptr) do { \
235 (ret) = strtok_r(str, sep, &saveptr); \
236 if ((ret) == NULL) return false; \
239 #define DNS_PARSE_UINT(ret, str, sep, saveptr) do { \
240 char *istr = strtok_r(str, sep, &saveptr); \
241 if ((istr) == NULL) return false; \
242 (ret) = strtoul(istr, NULL, 10); \
246 parse a record from bind9
248 static bool b9_parse(struct dlz_bind9_data *state,
249 const char *rdatastr,
250 struct dnsp_DnssrvRpcRecord *rec)
252 char *full_name, *dclass, *type;
253 char *str, *saveptr=NULL;
256 str = talloc_strdup(rec, rdatastr);
261 /* parse the SDLZ string form */
262 DNS_PARSE_STR(full_name, str, "\t", saveptr);
263 DNS_PARSE_UINT(rec->dwTtlSeconds, NULL, "\t", saveptr);
264 DNS_PARSE_STR(dclass, NULL, "\t", saveptr);
265 DNS_PARSE_STR(type, NULL, "\t", saveptr);
267 /* construct the record */
268 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
269 if (strcasecmp(type, dns_typemap[i].typestr) == 0) {
270 rec->wType = dns_typemap[i].dns_type;
274 if (i == ARRAY_SIZE(dns_typemap)) {
275 state->log(ISC_LOG_ERROR, "samba_dlz: unsupported record type '%s' for '%s'",
280 switch (rec->wType) {
282 DNS_PARSE_STR(rec->data.ipv4, NULL, " ", saveptr);
286 DNS_PARSE_STR(rec->data.ipv6, NULL, " ", saveptr);
290 DNS_PARSE_STR(rec->data.cname, NULL, " ", saveptr);
294 DNS_PARSE_STR(rec->data.txt, NULL, "\t", saveptr);
298 DNS_PARSE_STR(rec->data.ptr, NULL, " ", saveptr);
302 DNS_PARSE_UINT(rec->data.srv.wPriority, NULL, " ", saveptr);
303 DNS_PARSE_UINT(rec->data.srv.wWeight, NULL, " ", saveptr);
304 DNS_PARSE_UINT(rec->data.srv.wPort, NULL, " ", saveptr);
305 DNS_PARSE_STR(rec->data.srv.nameTarget, NULL, " ", saveptr);
309 DNS_PARSE_UINT(rec->data.mx.wPriority, NULL, " ", saveptr);
310 DNS_PARSE_STR(rec->data.mx.nameTarget, NULL, " ", saveptr);
314 DNS_PARSE_STR(rec->data.hinfo.cpu, NULL, " ", saveptr);
315 DNS_PARSE_STR(rec->data.hinfo.os, NULL, " ", saveptr);
319 DNS_PARSE_STR(rec->data.ns, NULL, " ", saveptr);
323 DNS_PARSE_STR(rec->data.soa.mname, NULL, " ", saveptr);
324 DNS_PARSE_STR(rec->data.soa.rname, NULL, " ", saveptr);
325 DNS_PARSE_UINT(rec->data.soa.serial, NULL, " ", saveptr);
326 DNS_PARSE_UINT(rec->data.soa.refresh, NULL, " ", saveptr);
327 DNS_PARSE_UINT(rec->data.soa.retry, NULL, " ", saveptr);
328 DNS_PARSE_UINT(rec->data.soa.expire, NULL, " ", saveptr);
329 DNS_PARSE_UINT(rec->data.soa.minimum, NULL, " ", saveptr);
333 state->log(ISC_LOG_ERROR, "samba b9_parse: unhandled record type %u",
338 /* we should be at the end of the buffer now */
339 if (strtok_r(NULL, "\t ", &saveptr) != NULL) {
340 state->log(ISC_LOG_ERROR, "samba b9_parse: expected data at end of string for '%s'");
348 send a resource recond to bind9
350 static isc_result_t b9_putrr(struct dlz_bind9_data *state,
351 void *handle, struct dnsp_DnssrvRpcRecord *rec,
355 const char *type, *data;
356 TALLOC_CTX *tmp_ctx = talloc_new(state);
358 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
359 return ISC_R_FAILURE;
363 talloc_free(tmp_ctx);
364 return ISC_R_NOMEMORY;
369 for (i=0; types[i]; i++) {
370 if (strcmp(types[i], type) == 0) break;
372 if (types[i] == NULL) {
374 return ISC_R_SUCCESS;
378 result = state->putrr(handle, type, rec->dwTtlSeconds, data);
379 if (result != ISC_R_SUCCESS) {
380 state->log(ISC_LOG_ERROR, "Failed to put rr");
382 talloc_free(tmp_ctx);
388 send a named resource recond to bind9
390 static isc_result_t b9_putnamedrr(struct dlz_bind9_data *state,
391 void *handle, const char *name,
392 struct dnsp_DnssrvRpcRecord *rec)
395 const char *type, *data;
396 TALLOC_CTX *tmp_ctx = talloc_new(state);
398 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
399 return ISC_R_FAILURE;
403 talloc_free(tmp_ctx);
404 return ISC_R_NOMEMORY;
407 result = state->putnamedrr(handle, name, type, rec->dwTtlSeconds, data);
408 if (result != ISC_R_SUCCESS) {
409 state->log(ISC_LOG_ERROR, "Failed to put named rr '%s'", name);
411 talloc_free(tmp_ctx);
422 static isc_result_t parse_options(struct dlz_bind9_data *state,
423 unsigned int argc, char *argv[],
424 struct b9_options *options)
427 options->url = talloc_strdup(state, argv[1]);
428 if (options->url == NULL) {
429 return ISC_R_NOMEMORY;
431 state->log(ISC_LOG_INFO, "samba_dlz: Using samdb URL %s", options->url);
434 return ISC_R_SUCCESS;
439 called to initialise the driver
441 _PUBLIC_ isc_result_t dlz_create(const char *dlzname,
442 unsigned int argc, char *argv[],
445 struct dlz_bind9_data *state;
446 const char *helper_name;
451 struct b9_options options;
453 ZERO_STRUCT(options);
455 state = talloc_zero(NULL, struct dlz_bind9_data);
457 return ISC_R_NOMEMORY;
460 tmp_ctx = talloc_new(state);
462 /* fill in the helper functions */
463 va_start(ap, dbdata);
464 while ((helper_name = va_arg(ap, const char *)) != NULL) {
465 b9_add_helper(state, helper_name, va_arg(ap, void*));
469 state->ev_ctx = s4_event_context_init(state);
470 if (state->ev_ctx == NULL) {
471 result = ISC_R_NOMEMORY;
475 result = parse_options(state, argc, argv, &options);
476 if (result != ISC_R_SUCCESS) {
480 state->lp = loadparm_init_global(true);
481 if (state->lp == NULL) {
482 result = ISC_R_NOMEMORY;
486 if (options.url == NULL) {
487 options.url = lpcfg_private_path(tmp_ctx, state->lp, "dns/sam.ldb");
488 if (options.url == NULL) {
489 result = ISC_R_NOMEMORY;
494 /* Do not install samba signal handlers */
495 fault_setup_disable();
497 state->samdb = samdb_connect_url(state, state->ev_ctx, state->lp,
498 system_session(state->lp), 0, options.url);
499 if (state->samdb == NULL) {
500 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to connect to %s",
502 result = ISC_R_FAILURE;
506 dn = ldb_get_default_basedn(state->samdb);
508 state->log(ISC_LOG_ERROR, "samba_dlz: Unable to get basedn for %s - %s",
509 options.url, ldb_errstring(state->samdb));
510 result = ISC_R_FAILURE;
514 state->log(ISC_LOG_INFO, "samba_dlz: started for DN %s",
515 ldb_dn_get_linearized(dn));
519 talloc_free(tmp_ctx);
520 return ISC_R_SUCCESS;
530 _PUBLIC_ void dlz_destroy(void *dbdata)
532 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
533 state->log(ISC_LOG_INFO, "samba_dlz: shutting down");
539 return the base DN for a zone
541 static isc_result_t b9_find_zone_dn(struct dlz_bind9_data *state, const char *zone_name,
542 TALLOC_CTX *mem_ctx, struct ldb_dn **zone_dn)
545 TALLOC_CTX *tmp_ctx = talloc_new(state);
546 const char *attrs[] = { NULL };
549 for (i=0; zone_prefixes[i]; i++) {
551 struct ldb_result *res;
553 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
555 talloc_free(tmp_ctx);
556 return ISC_R_NOMEMORY;
559 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone_name, zone_prefixes[i])) {
560 talloc_free(tmp_ctx);
561 return ISC_R_NOMEMORY;
564 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsZone");
565 if (ret == LDB_SUCCESS) {
566 if (zone_dn != NULL) {
567 *zone_dn = talloc_steal(mem_ctx, dn);
569 talloc_free(tmp_ctx);
570 return ISC_R_SUCCESS;
575 talloc_free(tmp_ctx);
576 return ISC_R_NOTFOUND;
581 return the DN for a name. The record does not need to exist, but the
584 static isc_result_t b9_find_name_dn(struct dlz_bind9_data *state, const char *name,
585 TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
589 /* work through the name piece by piece, until we find a zone */
592 result = b9_find_zone_dn(state, p, mem_ctx, dn);
593 if (result == ISC_R_SUCCESS) {
594 /* we found a zone, now extend the DN to get
599 ret = ldb_dn_add_child_fmt(*dn, "DC=@");
601 ret = ldb_dn_add_child_fmt(*dn, "DC=%.*s", (int)(p-name)-1, name);
605 return ISC_R_NOMEMORY;
607 return ISC_R_SUCCESS;
615 return ISC_R_NOTFOUND;
620 see if we handle a given zone
622 _PUBLIC_ isc_result_t dlz_findzonedb(void *dbdata, const char *name)
624 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
625 return b9_find_zone_dn(state, name, NULL, NULL);
632 static isc_result_t dlz_lookup_types(struct dlz_bind9_data *state,
633 const char *zone, const char *name,
634 dns_sdlzlookup_t *lookup,
637 TALLOC_CTX *tmp_ctx = talloc_new(state);
638 const char *attrs[] = { "dnsRecord", NULL };
639 int ret = LDB_SUCCESS, i;
640 struct ldb_result *res;
641 struct ldb_message_element *el;
644 for (i=0; zone_prefixes[i]; i++) {
645 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
647 talloc_free(tmp_ctx);
648 return ISC_R_NOMEMORY;
651 if (!ldb_dn_add_child_fmt(dn, "DC=%s,DC=%s,%s", name, zone, zone_prefixes[i])) {
652 talloc_free(tmp_ctx);
653 return ISC_R_NOMEMORY;
656 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
657 attrs, "objectClass=dnsNode");
658 if (ret == LDB_SUCCESS) {
662 if (ret != LDB_SUCCESS) {
663 talloc_free(tmp_ctx);
664 return ISC_R_NOTFOUND;
667 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
668 if (el == NULL || el->num_values == 0) {
669 talloc_free(tmp_ctx);
670 return ISC_R_NOTFOUND;
673 for (i=0; i<el->num_values; i++) {
674 struct dnsp_DnssrvRpcRecord rec;
675 enum ndr_err_code ndr_err;
678 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec,
679 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
680 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
681 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
682 ldb_dn_get_linearized(dn));
683 talloc_free(tmp_ctx);
684 return ISC_R_FAILURE;
687 result = b9_putrr(state, lookup, &rec, types);
688 if (result != ISC_R_SUCCESS) {
689 talloc_free(tmp_ctx);
694 talloc_free(tmp_ctx);
695 return ISC_R_SUCCESS;
701 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name,
702 void *dbdata, dns_sdlzlookup_t *lookup)
704 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
705 return dlz_lookup_types(state, zone, name, lookup, NULL);
710 see if a zone transfer is allowed
712 _PUBLIC_ isc_result_t dlz_allowzonexfr(void *dbdata, const char *name, const char *client)
714 /* just say yes for all our zones for now */
715 return dlz_findzonedb(dbdata, name);
719 perform a zone transfer
721 _PUBLIC_ isc_result_t dlz_allnodes(const char *zone, void *dbdata,
722 dns_sdlzallnodes_t *allnodes)
724 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
725 const char *attrs[] = { "dnsRecord", NULL };
726 int ret = LDB_SUCCESS, i, j;
728 struct ldb_result *res;
729 TALLOC_CTX *tmp_ctx = talloc_new(state);
731 for (i=0; zone_prefixes[i]; i++) {
732 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
734 talloc_free(tmp_ctx);
735 return ISC_R_NOMEMORY;
738 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone, zone_prefixes[i])) {
739 talloc_free(tmp_ctx);
740 return ISC_R_NOMEMORY;
743 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
744 attrs, "objectClass=dnsNode");
745 if (ret == LDB_SUCCESS) {
749 if (ret != LDB_SUCCESS) {
750 talloc_free(tmp_ctx);
751 return ISC_R_NOTFOUND;
754 for (i=0; i<res->count; i++) {
755 struct ldb_message_element *el;
756 TALLOC_CTX *el_ctx = talloc_new(tmp_ctx);
757 const char *rdn, *name;
758 const struct ldb_val *v;
760 el = ldb_msg_find_element(res->msgs[i], "dnsRecord");
761 if (el == NULL || el->num_values == 0) {
762 state->log(ISC_LOG_INFO, "failed to find dnsRecord for %s",
763 ldb_dn_get_linearized(dn));
768 v = ldb_dn_get_rdn_val(res->msgs[i]->dn);
770 state->log(ISC_LOG_INFO, "failed to find RDN for %s",
771 ldb_dn_get_linearized(dn));
776 rdn = talloc_strndup(el_ctx, (char *)v->data, v->length);
778 talloc_free(tmp_ctx);
779 return ISC_R_NOMEMORY;
782 if (strcmp(rdn, "@") == 0) {
785 name = talloc_asprintf(el_ctx, "%s.%s", rdn, zone);
788 talloc_free(tmp_ctx);
789 return ISC_R_NOMEMORY;
792 for (j=0; j<el->num_values; j++) {
793 struct dnsp_DnssrvRpcRecord rec;
794 enum ndr_err_code ndr_err;
797 ndr_err = ndr_pull_struct_blob(&el->values[j], el_ctx, &rec,
798 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
799 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
800 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
801 ldb_dn_get_linearized(dn));
805 result = b9_putnamedrr(state, allnodes, name, &rec);
806 if (result != ISC_R_SUCCESS) {
812 talloc_free(tmp_ctx);
814 return ISC_R_SUCCESS;
821 _PUBLIC_ isc_result_t dlz_newversion(const char *zone, void *dbdata, void **versionp)
823 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
825 state->log(ISC_LOG_INFO, "samba_dlz: starting transaction on zone %s", zone);
827 if (state->transaction_token != NULL) {
828 state->log(ISC_LOG_INFO, "samba_dlz: transaction already started for zone %s", zone);
829 return ISC_R_FAILURE;
832 state->transaction_token = talloc_zero(state, int);
833 if (state->transaction_token == NULL) {
834 return ISC_R_NOMEMORY;
837 if (ldb_transaction_start(state->samdb) != LDB_SUCCESS) {
838 state->log(ISC_LOG_INFO, "samba_dlz: failed to start a transaction for zone %s", zone);
839 talloc_free(state->transaction_token);
840 state->transaction_token = NULL;
841 return ISC_R_FAILURE;
844 *versionp = (void *)state->transaction_token;
846 return ISC_R_SUCCESS;
852 _PUBLIC_ void dlz_closeversion(const char *zone, isc_boolean_t commit,
853 void *dbdata, void **versionp)
855 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
857 if (state->transaction_token != (int *)*versionp) {
858 state->log(ISC_LOG_INFO, "samba_dlz: transaction not started for zone %s", zone);
863 if (ldb_transaction_commit(state->samdb) != LDB_SUCCESS) {
864 state->log(ISC_LOG_INFO, "samba_dlz: failed to commit a transaction for zone %s", zone);
867 state->log(ISC_LOG_INFO, "samba_dlz: committed transaction on zone %s", zone);
869 if (ldb_transaction_cancel(state->samdb) != LDB_SUCCESS) {
870 state->log(ISC_LOG_INFO, "samba_dlz: failed to cancel a transaction for zone %s", zone);
873 state->log(ISC_LOG_INFO, "samba_dlz: cancelling transaction on zone %s", zone);
876 talloc_free(state->transaction_token);
877 state->transaction_token = NULL;
883 see if there is a SOA record for a zone
885 static bool b9_has_soa(struct dlz_bind9_data *state, struct ldb_dn *dn, const char *zone)
887 const char *attrs[] = { "dnsRecord", NULL };
888 struct ldb_result *res;
889 struct ldb_message_element *el;
890 TALLOC_CTX *tmp_ctx = talloc_new(state);
893 if (!ldb_dn_add_child_fmt(dn, "DC=@,DC=%s", zone)) {
894 talloc_free(tmp_ctx);
898 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
899 attrs, "objectClass=dnsNode");
900 if (ret != LDB_SUCCESS) {
901 talloc_free(tmp_ctx);
905 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
907 talloc_free(tmp_ctx);
910 for (i=0; i<el->num_values; i++) {
911 struct dnsp_DnssrvRpcRecord rec;
912 enum ndr_err_code ndr_err;
914 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec,
915 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
916 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
919 if (rec.wType == DNS_TYPE_SOA) {
920 talloc_free(tmp_ctx);
925 talloc_free(tmp_ctx);
930 configure a writeable zone
932 _PUBLIC_ isc_result_t dlz_configure(dns_view_t *view, void *dbdata)
934 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
939 state->log(ISC_LOG_INFO, "samba_dlz: starting configure");
940 if (state->writeable_zone == NULL) {
941 state->log(ISC_LOG_INFO, "samba_dlz: no writeable_zone method available");
942 return ISC_R_FAILURE;
945 tmp_ctx = talloc_new(state);
947 for (i=0; zone_prefixes[i]; i++) {
948 const char *attrs[] = { "name", NULL };
950 struct ldb_result *res;
952 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
954 talloc_free(tmp_ctx);
955 return ISC_R_NOMEMORY;
958 if (!ldb_dn_add_child_fmt(dn, "%s", zone_prefixes[i])) {
959 talloc_free(tmp_ctx);
960 return ISC_R_NOMEMORY;
963 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
964 attrs, "objectClass=dnsZone");
965 if (ret != LDB_SUCCESS) {
969 for (j=0; j<res->count; j++) {
971 const char *zone = ldb_msg_find_attr_as_string(res->msgs[j], "name", NULL);
972 struct ldb_dn *zone_dn;
977 zone_dn = ldb_dn_copy(tmp_ctx, dn);
978 if (zone_dn == NULL) {
979 talloc_free(tmp_ctx);
980 return ISC_R_NOMEMORY;
983 if (!b9_has_soa(state, zone_dn, zone)) {
986 result = state->writeable_zone(view, zone);
987 if (result != ISC_R_SUCCESS) {
988 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to configure zone '%s'",
990 talloc_free(tmp_ctx);
993 state->log(ISC_LOG_INFO, "samba_dlz: configured writeable zone '%s'", zone);
997 talloc_free(tmp_ctx);
998 return ISC_R_SUCCESS;
1002 authorize a zone update
1004 _PUBLIC_ isc_boolean_t dlz_ssumatch(const char *signer, const char *name, const char *tcpaddr,
1005 const char *type, const char *key, uint32_t keydatalen, uint8_t *keydata,
1008 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1010 state->log(ISC_LOG_INFO, "samba_dlz: allowing update of signer=%s name=%s tcpaddr=%s type=%s key=%s keydatalen=%u",
1011 signer, name, tcpaddr, type, key, keydatalen);
1019 static isc_result_t b9_add_record(struct dlz_bind9_data *state, const char *name,
1021 struct dnsp_DnssrvRpcRecord *rec)
1023 struct ldb_message *msg;
1024 enum ndr_err_code ndr_err;
1028 msg = ldb_msg_new(rec);
1030 return ISC_R_NOMEMORY;
1033 ret = ldb_msg_add_string(msg, "objectClass", "dnsNode");
1034 if (ret != LDB_SUCCESS) {
1035 return ISC_R_FAILURE;
1038 ndr_err = ndr_push_struct_blob(&v, rec, rec, (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
1039 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1040 return ISC_R_FAILURE;
1042 ret = ldb_msg_add_value(msg, "dnsRecord", &v, NULL);
1043 if (ret != LDB_SUCCESS) {
1044 return ISC_R_FAILURE;
1047 ret = ldb_add(state->samdb, msg);
1048 if (ret != LDB_SUCCESS) {
1049 return ISC_R_FAILURE;
1052 return ISC_R_SUCCESS;
1056 see if two DNS names are the same
1058 static bool dns_name_equal(const char *name1, const char *name2)
1060 size_t len1 = strlen(name1);
1061 size_t len2 = strlen(name2);
1062 if (name1[len1-1] == '.') len1--;
1063 if (name2[len2-1] == '.') len2--;
1067 return strncasecmp_m(name1, name2, len1) == 0;
1072 see if two dns records match
1074 static bool b9_record_match(struct dlz_bind9_data *state,
1075 struct dnsp_DnssrvRpcRecord *rec1, struct dnsp_DnssrvRpcRecord *rec2)
1077 if (rec1->wType != rec2->wType) {
1080 /* see if this type is single valued */
1081 if (b9_single_valued(rec1->wType)) {
1085 /* see if the data matches */
1086 switch (rec1->wType) {
1088 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
1090 return strcmp(rec1->data.ipv6, rec2->data.ipv6) == 0;
1091 case DNS_TYPE_CNAME:
1092 return dns_name_equal(rec1->data.cname, rec2->data.cname);
1094 return strcmp(rec1->data.txt, rec2->data.txt) == 0;
1096 return strcmp(rec1->data.ptr, rec2->data.ptr) == 0;
1098 return dns_name_equal(rec1->data.ns, rec2->data.ns);
1101 return rec1->data.srv.wPriority == rec2->data.srv.wPriority &&
1102 rec1->data.srv.wWeight == rec2->data.srv.wWeight &&
1103 rec1->data.srv.wPort == rec2->data.srv.wPort &&
1104 dns_name_equal(rec1->data.srv.nameTarget, rec2->data.srv.nameTarget);
1107 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
1108 dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
1110 case DNS_TYPE_HINFO:
1111 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
1112 strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
1115 return dns_name_equal(rec1->data.soa.mname, rec2->data.soa.mname) &&
1116 dns_name_equal(rec1->data.soa.rname, rec2->data.soa.rname) &&
1117 rec1->data.soa.serial == rec2->data.soa.serial &&
1118 rec1->data.soa.refresh == rec2->data.soa.refresh &&
1119 rec1->data.soa.retry == rec2->data.soa.retry &&
1120 rec1->data.soa.expire == rec2->data.soa.expire &&
1121 rec1->data.soa.minimum == rec2->data.soa.minimum;
1123 state->log(ISC_LOG_ERROR, "samba b9_putrr: unhandled record type %u",
1133 add or modify a rdataset
1135 _PUBLIC_ isc_result_t dlz_addrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1137 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1138 struct dnsp_DnssrvRpcRecord *rec;
1140 isc_result_t result;
1141 struct ldb_result *res;
1142 const char *attrs[] = { "dnsRecord", NULL };
1144 struct ldb_message_element *el;
1145 enum ndr_err_code ndr_err;
1148 if (state->transaction_token != (void*)version) {
1149 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1150 return ISC_R_FAILURE;
1153 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1155 return ISC_R_NOMEMORY;
1158 unix_to_nt_time(&t, time(NULL));
1159 t /= 10*1000*1000; /* convert to seconds (NT time is in 100ns units) */
1160 t /= 3600; /* convert to hours */
1162 rec->rank = DNS_RANK_ZONE;
1163 rec->dwSerial = state->soa_serial;
1164 rec->dwTimeStamp = (uint32_t)t;
1166 if (!b9_parse(state, rdatastr, rec)) {
1167 state->log(ISC_LOG_INFO, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1169 return ISC_R_FAILURE;
1172 /* find the DN of the record */
1173 result = b9_find_name_dn(state, name, rec, &dn);
1174 if (result != ISC_R_SUCCESS) {
1179 /* get any existing records */
1180 ret = ldb_search(state->samdb, rec, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1181 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1182 result = b9_add_record(state, name, dn, rec);
1184 if (result == ISC_R_SUCCESS) {
1185 state->log(ISC_LOG_ERROR, "samba_dlz: added %s %s", name, rdatastr);
1190 /* there are existing records. We need to see if this will
1191 * replace a record or add to it
1193 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1195 state->log(ISC_LOG_ERROR, "samba_dlz: no dnsRecord attribute for %s",
1196 ldb_dn_get_linearized(dn));
1198 return ISC_R_FAILURE;
1201 for (i=0; i<el->num_values; i++) {
1202 struct dnsp_DnssrvRpcRecord rec2;
1204 ndr_err = ndr_pull_struct_blob(&el->values[i], rec, &rec2,
1205 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1206 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1207 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1208 ldb_dn_get_linearized(dn));
1210 return ISC_R_FAILURE;
1213 if (b9_record_match(state, rec, &rec2)) {
1217 if (i == el->num_values) {
1218 /* adding a new value */
1219 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values+1);
1220 if (el->values == NULL) {
1222 return ISC_R_NOMEMORY;
1227 ndr_err = ndr_push_struct_blob(&el->values[i], rec, rec,
1228 (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
1229 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1230 state->log(ISC_LOG_ERROR, "samba_dlz: failed to push dnsRecord for %s",
1231 ldb_dn_get_linearized(dn));
1233 return ISC_R_FAILURE;
1236 /* modify the record */
1237 el->flags = LDB_FLAG_MOD_REPLACE;
1238 ret = ldb_modify(state->samdb, res->msgs[0]);
1239 if (ret != LDB_SUCCESS) {
1240 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1241 ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1243 return ISC_R_FAILURE;
1246 state->log(ISC_LOG_INFO, "samba_dlz: added rdataset %s '%s'", name, rdatastr);
1249 return ISC_R_SUCCESS;
1255 _PUBLIC_ isc_result_t dlz_subrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1257 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1258 struct dnsp_DnssrvRpcRecord *rec;
1260 isc_result_t result;
1261 struct ldb_result *res;
1262 const char *attrs[] = { "dnsRecord", NULL };
1264 struct ldb_message_element *el;
1265 enum ndr_err_code ndr_err;
1267 if (state->transaction_token != (void*)version) {
1268 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1269 return ISC_R_FAILURE;
1272 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1274 return ISC_R_NOMEMORY;
1277 if (!b9_parse(state, rdatastr, rec)) {
1278 state->log(ISC_LOG_INFO, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1280 return ISC_R_FAILURE;
1283 /* find the DN of the record */
1284 result = b9_find_name_dn(state, name, rec, &dn);
1285 if (result != ISC_R_SUCCESS) {
1290 /* get the existing records */
1291 ret = ldb_search(state->samdb, rec, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1292 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1294 return ISC_R_NOTFOUND;
1297 /* there are existing records. We need to see if any match
1299 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1300 if (el == NULL || el->num_values == 0) {
1301 state->log(ISC_LOG_ERROR, "samba_dlz: no dnsRecord attribute for %s",
1302 ldb_dn_get_linearized(dn));
1304 return ISC_R_FAILURE;
1307 for (i=0; i<el->num_values; i++) {
1308 struct dnsp_DnssrvRpcRecord rec2;
1310 ndr_err = ndr_pull_struct_blob(&el->values[i], rec, &rec2,
1311 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1312 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1313 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1314 ldb_dn_get_linearized(dn));
1316 return ISC_R_FAILURE;
1319 if (b9_record_match(state, rec, &rec2)) {
1323 if (i == el->num_values) {
1325 return ISC_R_NOTFOUND;
1328 if (i < el->num_values-1) {
1329 memmove(&el->values[i], &el->values[i+1], sizeof(el->values[0])*((el->num_values-1)-i));
1333 if (el->num_values == 0) {
1334 /* delete the record */
1335 ret = ldb_delete(state->samdb, dn);
1337 /* modify the record */
1338 el->flags = LDB_FLAG_MOD_REPLACE;
1339 ret = ldb_modify(state->samdb, res->msgs[0]);
1341 if (ret != LDB_SUCCESS) {
1342 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1343 ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1345 return ISC_R_FAILURE;
1348 state->log(ISC_LOG_INFO, "samba_dlz: subtracted rdataset %s '%s'", name, rdatastr);
1351 return ISC_R_SUCCESS;
1356 delete all records of the given type
1358 _PUBLIC_ isc_result_t dlz_delrdataset(const char *name, const char *type, void *dbdata, void *version)
1360 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1361 TALLOC_CTX *tmp_ctx;
1363 isc_result_t result;
1364 struct ldb_result *res;
1365 const char *attrs[] = { "dnsRecord", NULL };
1367 struct ldb_message_element *el;
1368 enum ndr_err_code ndr_err;
1369 enum dns_record_type dns_type;
1372 if (state->transaction_token != (void*)version) {
1373 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1374 return ISC_R_FAILURE;
1377 if (!b9_dns_type(type, &dns_type)) {
1378 state->log(ISC_LOG_INFO, "samba_dlz: bad dns type %s in delete", type);
1379 return ISC_R_FAILURE;
1382 tmp_ctx = talloc_new(state);
1384 /* find the DN of the record */
1385 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1386 if (result != ISC_R_SUCCESS) {
1387 talloc_free(tmp_ctx);
1391 /* get the existing records */
1392 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1393 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1394 talloc_free(tmp_ctx);
1395 return ISC_R_NOTFOUND;
1398 /* there are existing records. We need to see if any match the type
1400 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1401 if (el == NULL || el->num_values == 0) {
1402 talloc_free(tmp_ctx);
1403 return ISC_R_NOTFOUND;
1406 for (i=0; i<el->num_values; i++) {
1407 struct dnsp_DnssrvRpcRecord rec2;
1409 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec2,
1410 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1411 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1412 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1413 ldb_dn_get_linearized(dn));
1414 talloc_free(tmp_ctx);
1415 return ISC_R_FAILURE;
1418 if (dns_type == rec2.wType) {
1419 if (i < el->num_values-1) {
1420 memmove(&el->values[i], &el->values[i+1],
1421 sizeof(el->values[0])*((el->num_values-1)-i));
1430 talloc_free(tmp_ctx);
1431 return ISC_R_FAILURE;
1434 if (el->num_values == 0) {
1435 /* delete the record */
1436 ret = ldb_delete(state->samdb, dn);
1438 /* modify the record */
1439 el->flags = LDB_FLAG_MOD_REPLACE;
1440 ret = ldb_modify(state->samdb, res->msgs[0]);
1442 if (ret != LDB_SUCCESS) {
1443 state->log(ISC_LOG_ERROR, "samba_dlz: failed to delete type %s in %s - %s",
1444 type, ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1445 talloc_free(tmp_ctx);
1446 return ISC_R_FAILURE;
1449 state->log(ISC_LOG_INFO, "samba_dlz: deleted rdataset %s of type %s", name, type);
1451 talloc_free(tmp_ctx);
1452 return ISC_R_SUCCESS;