s3:dbwrap: turn the fetch dbwrap method to NTSTATUS return code.
[Samba.git] / source4 / dns_server / dlz_bind9.c
blob87476d34446ed9a9ea45a4cc59c53d29b4c08164
1 /*
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/>.
22 #include "includes.h"
23 #include "talloc.h"
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 "dlz_minimal.h"
41 struct b9_options {
42 const char *url;
45 struct dlz_bind9_data {
46 struct b9_options options;
47 struct ldb_context *samdb;
48 struct tevent_context *ev_ctx;
49 struct loadparm_context *lp;
50 int *transaction_token;
51 uint32_t soa_serial;
53 /* Used for dynamic update */
54 struct smb_krb5_context *smb_krb5_ctx;
56 /* helper functions from the dlz_dlopen driver */
57 void (*log)(int level, const char *fmt, ...);
58 isc_result_t (*putrr)(dns_sdlzlookup_t *handle, const char *type,
59 dns_ttl_t ttl, const char *data);
60 isc_result_t (*putnamedrr)(dns_sdlzlookup_t *handle, const char *name,
61 const char *type, dns_ttl_t ttl, const char *data);
62 isc_result_t (*writeable_zone)(dns_view_t *view, const char *zone_name);
66 static const char *zone_prefixes[] = {
67 "CN=MicrosoftDNS,DC=DomainDnsZones",
68 "CN=MicrosoftDNS,DC=ForestDnsZones",
69 "CN=MicrosoftDNS,CN=System",
70 NULL
74 return the version of the API
76 _PUBLIC_ int dlz_version(unsigned int *flags)
78 return DLZ_DLOPEN_VERSION;
82 remember a helper function from the bind9 dlz_dlopen driver
84 static void b9_add_helper(struct dlz_bind9_data *state, const char *helper_name, void *ptr)
86 if (strcmp(helper_name, "log") == 0) {
87 state->log = ptr;
89 if (strcmp(helper_name, "putrr") == 0) {
90 state->putrr = ptr;
92 if (strcmp(helper_name, "putnamedrr") == 0) {
93 state->putnamedrr = ptr;
95 if (strcmp(helper_name, "writeable_zone") == 0) {
96 state->writeable_zone = ptr;
101 format a record for bind9
103 static bool b9_format(struct dlz_bind9_data *state,
104 TALLOC_CTX *mem_ctx,
105 struct dnsp_DnssrvRpcRecord *rec,
106 const char **type, const char **data)
108 switch (rec->wType) {
109 case DNS_TYPE_A:
110 *type = "a";
111 *data = rec->data.ipv4;
112 break;
114 case DNS_TYPE_AAAA:
115 *type = "aaaa";
116 *data = rec->data.ipv6;
117 break;
119 case DNS_TYPE_CNAME:
120 *type = "cname";
121 *data = rec->data.cname;
122 break;
124 case DNS_TYPE_TXT:
125 *type = "txt";
126 *data = rec->data.txt;
127 break;
129 case DNS_TYPE_PTR:
130 *type = "ptr";
131 *data = rec->data.ptr;
132 break;
134 case DNS_TYPE_SRV:
135 *type = "srv";
136 *data = talloc_asprintf(mem_ctx, "%u %u %u %s",
137 rec->data.srv.wPriority,
138 rec->data.srv.wWeight,
139 rec->data.srv.wPort,
140 rec->data.srv.nameTarget);
141 break;
143 case DNS_TYPE_MX:
144 *type = "mx";
145 *data = talloc_asprintf(mem_ctx, "%u %s",
146 rec->data.mx.wPriority,
147 rec->data.mx.nameTarget);
148 break;
150 case DNS_TYPE_HINFO:
151 *type = "hinfo";
152 *data = talloc_asprintf(mem_ctx, "%s %s",
153 rec->data.hinfo.cpu,
154 rec->data.hinfo.os);
155 break;
157 case DNS_TYPE_NS:
158 *type = "ns";
159 *data = rec->data.ns;
160 break;
162 case DNS_TYPE_SOA: {
163 const char *mname;
164 *type = "soa";
166 /* we need to fake the authoritative nameserver to
167 * point at ourselves. This is how AD DNS servers
168 * force clients to send updates to the right local DC
170 mname = talloc_asprintf(mem_ctx, "%s.%s",
171 lpcfg_netbios_name(state->lp), lpcfg_dnsdomain(state->lp));
172 if (mname == NULL) {
173 return false;
175 mname = strlower_talloc(mem_ctx, mname);
176 if (mname == NULL) {
177 return false;
180 state->soa_serial = rec->data.soa.serial;
182 *data = talloc_asprintf(mem_ctx, "%s %s %u %u %u %u %u",
183 mname,
184 rec->data.soa.rname,
185 rec->data.soa.serial,
186 rec->data.soa.refresh,
187 rec->data.soa.retry,
188 rec->data.soa.expire,
189 rec->data.soa.minimum);
190 break;
193 default:
194 state->log(ISC_LOG_ERROR, "samba b9_putrr: unhandled record type %u",
195 rec->wType);
196 return false;
199 return true;
202 static const struct {
203 enum dns_record_type dns_type;
204 const char *typestr;
205 bool single_valued;
206 } dns_typemap[] = {
207 { DNS_TYPE_A, "A" , false},
208 { DNS_TYPE_AAAA, "AAAA" , false},
209 { DNS_TYPE_CNAME, "CNAME" , true},
210 { DNS_TYPE_TXT, "TXT" , false},
211 { DNS_TYPE_PTR, "PTR" , false},
212 { DNS_TYPE_SRV, "SRV" , false},
213 { DNS_TYPE_MX, "MX" , false},
214 { DNS_TYPE_HINFO, "HINFO" , false},
215 { DNS_TYPE_NS, "NS" , false},
216 { DNS_TYPE_SOA, "SOA" , true},
221 see if a DNS type is single valued
223 static bool b9_single_valued(enum dns_record_type dns_type)
225 int i;
226 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
227 if (dns_typemap[i].dns_type == dns_type) {
228 return dns_typemap[i].single_valued;
231 return false;
235 see if a DNS type is single valued
237 static bool b9_dns_type(const char *type, enum dns_record_type *dtype)
239 int i;
240 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
241 if (strcasecmp(dns_typemap[i].typestr, type) == 0) {
242 *dtype = dns_typemap[i].dns_type;
243 return true;
246 return false;
250 #define DNS_PARSE_STR(ret, str, sep, saveptr) do { \
251 (ret) = strtok_r(str, sep, &saveptr); \
252 if ((ret) == NULL) return false; \
253 } while (0)
255 #define DNS_PARSE_UINT(ret, str, sep, saveptr) do { \
256 char *istr = strtok_r(str, sep, &saveptr); \
257 if ((istr) == NULL) return false; \
258 (ret) = strtoul(istr, NULL, 10); \
259 } while (0)
262 parse a record from bind9
264 static bool b9_parse(struct dlz_bind9_data *state,
265 const char *rdatastr,
266 struct dnsp_DnssrvRpcRecord *rec)
268 char *full_name, *dclass, *type;
269 char *str, *saveptr=NULL;
270 int i;
272 str = talloc_strdup(rec, rdatastr);
273 if (str == NULL) {
274 return false;
277 /* parse the SDLZ string form */
278 DNS_PARSE_STR(full_name, str, "\t", saveptr);
279 DNS_PARSE_UINT(rec->dwTtlSeconds, NULL, "\t", saveptr);
280 DNS_PARSE_STR(dclass, NULL, "\t", saveptr);
281 DNS_PARSE_STR(type, NULL, "\t", saveptr);
283 /* construct the record */
284 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
285 if (strcasecmp(type, dns_typemap[i].typestr) == 0) {
286 rec->wType = dns_typemap[i].dns_type;
287 break;
290 if (i == ARRAY_SIZE(dns_typemap)) {
291 state->log(ISC_LOG_ERROR, "samba_dlz: unsupported record type '%s' for '%s'",
292 type, full_name);
293 return false;
296 switch (rec->wType) {
297 case DNS_TYPE_A:
298 DNS_PARSE_STR(rec->data.ipv4, NULL, " ", saveptr);
299 break;
301 case DNS_TYPE_AAAA:
302 DNS_PARSE_STR(rec->data.ipv6, NULL, " ", saveptr);
303 break;
305 case DNS_TYPE_CNAME:
306 DNS_PARSE_STR(rec->data.cname, NULL, " ", saveptr);
307 break;
309 case DNS_TYPE_TXT:
310 DNS_PARSE_STR(rec->data.txt, NULL, "\t", saveptr);
311 break;
313 case DNS_TYPE_PTR:
314 DNS_PARSE_STR(rec->data.ptr, NULL, " ", saveptr);
315 break;
317 case DNS_TYPE_SRV:
318 DNS_PARSE_UINT(rec->data.srv.wPriority, NULL, " ", saveptr);
319 DNS_PARSE_UINT(rec->data.srv.wWeight, NULL, " ", saveptr);
320 DNS_PARSE_UINT(rec->data.srv.wPort, NULL, " ", saveptr);
321 DNS_PARSE_STR(rec->data.srv.nameTarget, NULL, " ", saveptr);
322 break;
324 case DNS_TYPE_MX:
325 DNS_PARSE_UINT(rec->data.mx.wPriority, NULL, " ", saveptr);
326 DNS_PARSE_STR(rec->data.mx.nameTarget, NULL, " ", saveptr);
327 break;
329 case DNS_TYPE_HINFO:
330 DNS_PARSE_STR(rec->data.hinfo.cpu, NULL, " ", saveptr);
331 DNS_PARSE_STR(rec->data.hinfo.os, NULL, " ", saveptr);
332 break;
334 case DNS_TYPE_NS:
335 DNS_PARSE_STR(rec->data.ns, NULL, " ", saveptr);
336 break;
338 case DNS_TYPE_SOA:
339 DNS_PARSE_STR(rec->data.soa.mname, NULL, " ", saveptr);
340 DNS_PARSE_STR(rec->data.soa.rname, NULL, " ", saveptr);
341 DNS_PARSE_UINT(rec->data.soa.serial, NULL, " ", saveptr);
342 DNS_PARSE_UINT(rec->data.soa.refresh, NULL, " ", saveptr);
343 DNS_PARSE_UINT(rec->data.soa.retry, NULL, " ", saveptr);
344 DNS_PARSE_UINT(rec->data.soa.expire, NULL, " ", saveptr);
345 DNS_PARSE_UINT(rec->data.soa.minimum, NULL, " ", saveptr);
346 break;
348 default:
349 state->log(ISC_LOG_ERROR, "samba b9_parse: unhandled record type %u",
350 rec->wType);
351 return false;
354 /* we should be at the end of the buffer now */
355 if (strtok_r(NULL, "\t ", &saveptr) != NULL) {
356 state->log(ISC_LOG_ERROR, "samba b9_parse: expected data at end of string for '%s'");
357 return false;
360 return true;
364 send a resource recond to bind9
366 static isc_result_t b9_putrr(struct dlz_bind9_data *state,
367 void *handle, struct dnsp_DnssrvRpcRecord *rec,
368 const char **types)
370 isc_result_t result;
371 const char *type, *data;
372 TALLOC_CTX *tmp_ctx = talloc_new(state);
374 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
375 return ISC_R_FAILURE;
378 if (data == NULL) {
379 talloc_free(tmp_ctx);
380 return ISC_R_NOMEMORY;
383 if (types) {
384 int i;
385 for (i=0; types[i]; i++) {
386 if (strcmp(types[i], type) == 0) break;
388 if (types[i] == NULL) {
389 /* skip it */
390 return ISC_R_SUCCESS;
394 result = state->putrr(handle, type, rec->dwTtlSeconds, data);
395 if (result != ISC_R_SUCCESS) {
396 state->log(ISC_LOG_ERROR, "Failed to put rr");
398 talloc_free(tmp_ctx);
399 return result;
404 send a named resource recond to bind9
406 static isc_result_t b9_putnamedrr(struct dlz_bind9_data *state,
407 void *handle, const char *name,
408 struct dnsp_DnssrvRpcRecord *rec)
410 isc_result_t result;
411 const char *type, *data;
412 TALLOC_CTX *tmp_ctx = talloc_new(state);
414 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
415 return ISC_R_FAILURE;
418 if (data == NULL) {
419 talloc_free(tmp_ctx);
420 return ISC_R_NOMEMORY;
423 result = state->putnamedrr(handle, name, type, rec->dwTtlSeconds, data);
424 if (result != ISC_R_SUCCESS) {
425 state->log(ISC_LOG_ERROR, "Failed to put named rr '%s'", name);
427 talloc_free(tmp_ctx);
428 return result;
432 parse options
434 static isc_result_t parse_options(struct dlz_bind9_data *state,
435 unsigned int argc, char *argv[],
436 struct b9_options *options)
438 if (argc == 2) {
439 options->url = talloc_strdup(state, argv[1]);
440 if (options->url == NULL) {
441 return ISC_R_NOMEMORY;
443 state->log(ISC_LOG_INFO, "samba_dlz: Using samdb URL %s", options->url);
446 return ISC_R_SUCCESS;
451 called to initialise the driver
453 _PUBLIC_ isc_result_t dlz_create(const char *dlzname,
454 unsigned int argc, char *argv[],
455 void **dbdata, ...)
457 struct dlz_bind9_data *state;
458 const char *helper_name;
459 va_list ap;
460 isc_result_t result;
461 TALLOC_CTX *tmp_ctx;
462 struct ldb_dn *dn;
463 NTSTATUS nt_status;
465 state = talloc_zero(NULL, struct dlz_bind9_data);
466 if (state == NULL) {
467 return ISC_R_NOMEMORY;
470 tmp_ctx = talloc_new(state);
472 /* fill in the helper functions */
473 va_start(ap, dbdata);
474 while ((helper_name = va_arg(ap, const char *)) != NULL) {
475 b9_add_helper(state, helper_name, va_arg(ap, void*));
477 va_end(ap);
479 state->ev_ctx = s4_event_context_init(state);
480 if (state->ev_ctx == NULL) {
481 result = ISC_R_NOMEMORY;
482 goto failed;
485 result = parse_options(state, argc, argv, &state->options);
486 if (result != ISC_R_SUCCESS) {
487 goto failed;
490 state->lp = loadparm_init_global(true);
491 if (state->lp == NULL) {
492 result = ISC_R_NOMEMORY;
493 goto failed;
496 if (smb_krb5_init_context(state, state->ev_ctx, state->lp, &state->smb_krb5_ctx) != 0) {
497 result = ISC_R_NOMEMORY;
498 goto failed;
501 nt_status = gensec_init();
502 if (!NT_STATUS_IS_OK(nt_status)) {
503 talloc_free(tmp_ctx);
504 return false;
507 if (state->options.url == NULL) {
508 state->options.url = lpcfg_private_path(state, state->lp, "dns/sam.ldb");
509 if (state->options.url == NULL) {
510 result = ISC_R_NOMEMORY;
511 goto failed;
515 /* Do not install samba signal handlers */
516 fault_setup_disable();
518 state->samdb = samdb_connect_url(state, state->ev_ctx, state->lp,
519 system_session(state->lp), 0, state->options.url);
520 if (state->samdb == NULL) {
521 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to connect to %s",
522 state->options.url);
523 result = ISC_R_FAILURE;
524 goto failed;
527 dn = ldb_get_default_basedn(state->samdb);
528 if (dn == NULL) {
529 state->log(ISC_LOG_ERROR, "samba_dlz: Unable to get basedn for %s - %s",
530 state->options.url, ldb_errstring(state->samdb));
531 result = ISC_R_FAILURE;
532 goto failed;
535 state->log(ISC_LOG_INFO, "samba_dlz: started for DN %s",
536 ldb_dn_get_linearized(dn));
538 *dbdata = state;
540 talloc_free(tmp_ctx);
541 return ISC_R_SUCCESS;
543 failed:
544 talloc_free(state);
545 return result;
549 shutdown the backend
551 _PUBLIC_ void dlz_destroy(void *dbdata)
553 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
554 state->log(ISC_LOG_INFO, "samba_dlz: shutting down");
555 talloc_free(state);
560 return the base DN for a zone
562 static isc_result_t b9_find_zone_dn(struct dlz_bind9_data *state, const char *zone_name,
563 TALLOC_CTX *mem_ctx, struct ldb_dn **zone_dn)
565 int ret;
566 TALLOC_CTX *tmp_ctx = talloc_new(state);
567 const char *attrs[] = { NULL };
568 int i;
570 for (i=0; zone_prefixes[i]; i++) {
571 struct ldb_dn *dn;
572 struct ldb_result *res;
574 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
575 if (dn == NULL) {
576 talloc_free(tmp_ctx);
577 return ISC_R_NOMEMORY;
580 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone_name, zone_prefixes[i])) {
581 talloc_free(tmp_ctx);
582 return ISC_R_NOMEMORY;
585 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsZone");
586 if (ret == LDB_SUCCESS) {
587 if (zone_dn != NULL) {
588 *zone_dn = talloc_steal(mem_ctx, dn);
590 talloc_free(tmp_ctx);
591 return ISC_R_SUCCESS;
593 talloc_free(dn);
596 talloc_free(tmp_ctx);
597 return ISC_R_NOTFOUND;
602 return the DN for a name. The record does not need to exist, but the
603 zone must exist
605 static isc_result_t b9_find_name_dn(struct dlz_bind9_data *state, const char *name,
606 TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
608 const char *p;
610 /* work through the name piece by piece, until we find a zone */
611 for (p=name; p; ) {
612 isc_result_t result;
613 result = b9_find_zone_dn(state, p, mem_ctx, dn);
614 if (result == ISC_R_SUCCESS) {
615 /* we found a zone, now extend the DN to get
616 * the full DN
618 bool ret;
619 if (p == name) {
620 ret = ldb_dn_add_child_fmt(*dn, "DC=@");
621 } else {
622 ret = ldb_dn_add_child_fmt(*dn, "DC=%.*s", (int)(p-name)-1, name);
624 if (!ret) {
625 talloc_free(*dn);
626 return ISC_R_NOMEMORY;
628 return ISC_R_SUCCESS;
630 p = strchr(p, '.');
631 if (p == NULL) {
632 break;
634 p++;
636 return ISC_R_NOTFOUND;
641 see if we handle a given zone
643 _PUBLIC_ isc_result_t dlz_findzonedb(void *dbdata, const char *name)
645 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
646 return b9_find_zone_dn(state, name, NULL, NULL);
651 lookup one record
653 static isc_result_t dlz_lookup_types(struct dlz_bind9_data *state,
654 const char *zone, const char *name,
655 dns_sdlzlookup_t *lookup,
656 const char **types)
658 TALLOC_CTX *tmp_ctx = talloc_new(state);
659 const char *attrs[] = { "dnsRecord", NULL };
660 int ret = LDB_SUCCESS, i;
661 struct ldb_result *res;
662 struct ldb_message_element *el;
663 struct ldb_dn *dn;
665 for (i=0; zone_prefixes[i]; i++) {
666 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
667 if (dn == NULL) {
668 talloc_free(tmp_ctx);
669 return ISC_R_NOMEMORY;
672 if (!ldb_dn_add_child_fmt(dn, "DC=%s,DC=%s,%s", name, zone, zone_prefixes[i])) {
673 talloc_free(tmp_ctx);
674 return ISC_R_NOMEMORY;
677 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
678 attrs, "objectClass=dnsNode");
679 if (ret == LDB_SUCCESS) {
680 break;
683 if (ret != LDB_SUCCESS) {
684 talloc_free(tmp_ctx);
685 return ISC_R_NOTFOUND;
688 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
689 if (el == NULL || el->num_values == 0) {
690 talloc_free(tmp_ctx);
691 return ISC_R_NOTFOUND;
694 for (i=0; i<el->num_values; i++) {
695 struct dnsp_DnssrvRpcRecord rec;
696 enum ndr_err_code ndr_err;
697 isc_result_t result;
699 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec,
700 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
701 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
702 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
703 ldb_dn_get_linearized(dn));
704 talloc_free(tmp_ctx);
705 return ISC_R_FAILURE;
708 result = b9_putrr(state, lookup, &rec, types);
709 if (result != ISC_R_SUCCESS) {
710 talloc_free(tmp_ctx);
711 return result;
715 talloc_free(tmp_ctx);
716 return ISC_R_SUCCESS;
720 lookup one record
722 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name,
723 void *dbdata, dns_sdlzlookup_t *lookup)
725 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
726 return dlz_lookup_types(state, zone, name, lookup, NULL);
731 see if a zone transfer is allowed
733 _PUBLIC_ isc_result_t dlz_allowzonexfr(void *dbdata, const char *name, const char *client)
735 /* just say yes for all our zones for now */
736 return dlz_findzonedb(dbdata, name);
740 perform a zone transfer
742 _PUBLIC_ isc_result_t dlz_allnodes(const char *zone, void *dbdata,
743 dns_sdlzallnodes_t *allnodes)
745 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
746 const char *attrs[] = { "dnsRecord", NULL };
747 int ret = LDB_SUCCESS, i, j;
748 struct ldb_dn *dn;
749 struct ldb_result *res;
750 TALLOC_CTX *tmp_ctx = talloc_new(state);
752 for (i=0; zone_prefixes[i]; i++) {
753 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
754 if (dn == NULL) {
755 talloc_free(tmp_ctx);
756 return ISC_R_NOMEMORY;
759 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone, zone_prefixes[i])) {
760 talloc_free(tmp_ctx);
761 return ISC_R_NOMEMORY;
764 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
765 attrs, "objectClass=dnsNode");
766 if (ret == LDB_SUCCESS) {
767 break;
770 if (ret != LDB_SUCCESS) {
771 talloc_free(tmp_ctx);
772 return ISC_R_NOTFOUND;
775 for (i=0; i<res->count; i++) {
776 struct ldb_message_element *el;
777 TALLOC_CTX *el_ctx = talloc_new(tmp_ctx);
778 const char *rdn, *name;
779 const struct ldb_val *v;
781 el = ldb_msg_find_element(res->msgs[i], "dnsRecord");
782 if (el == NULL || el->num_values == 0) {
783 state->log(ISC_LOG_INFO, "failed to find dnsRecord for %s",
784 ldb_dn_get_linearized(dn));
785 talloc_free(el_ctx);
786 continue;
789 v = ldb_dn_get_rdn_val(res->msgs[i]->dn);
790 if (v == NULL) {
791 state->log(ISC_LOG_INFO, "failed to find RDN for %s",
792 ldb_dn_get_linearized(dn));
793 talloc_free(el_ctx);
794 continue;
797 rdn = talloc_strndup(el_ctx, (char *)v->data, v->length);
798 if (rdn == NULL) {
799 talloc_free(tmp_ctx);
800 return ISC_R_NOMEMORY;
803 if (strcmp(rdn, "@") == 0) {
804 name = zone;
805 } else {
806 name = talloc_asprintf(el_ctx, "%s.%s", rdn, zone);
808 if (name == NULL) {
809 talloc_free(tmp_ctx);
810 return ISC_R_NOMEMORY;
813 for (j=0; j<el->num_values; j++) {
814 struct dnsp_DnssrvRpcRecord rec;
815 enum ndr_err_code ndr_err;
816 isc_result_t result;
818 ndr_err = ndr_pull_struct_blob(&el->values[j], el_ctx, &rec,
819 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
820 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
821 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
822 ldb_dn_get_linearized(dn));
823 continue;
826 result = b9_putnamedrr(state, allnodes, name, &rec);
827 if (result != ISC_R_SUCCESS) {
828 continue;
833 talloc_free(tmp_ctx);
835 return ISC_R_SUCCESS;
840 start a transaction
842 _PUBLIC_ isc_result_t dlz_newversion(const char *zone, void *dbdata, void **versionp)
844 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
846 state->log(ISC_LOG_INFO, "samba_dlz: starting transaction on zone %s", zone);
848 if (state->transaction_token != NULL) {
849 state->log(ISC_LOG_INFO, "samba_dlz: transaction already started for zone %s", zone);
850 return ISC_R_FAILURE;
853 state->transaction_token = talloc_zero(state, int);
854 if (state->transaction_token == NULL) {
855 return ISC_R_NOMEMORY;
858 if (ldb_transaction_start(state->samdb) != LDB_SUCCESS) {
859 state->log(ISC_LOG_INFO, "samba_dlz: failed to start a transaction for zone %s", zone);
860 talloc_free(state->transaction_token);
861 state->transaction_token = NULL;
862 return ISC_R_FAILURE;
865 *versionp = (void *)state->transaction_token;
867 return ISC_R_SUCCESS;
871 end a transaction
873 _PUBLIC_ void dlz_closeversion(const char *zone, isc_boolean_t commit,
874 void *dbdata, void **versionp)
876 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
878 if (state->transaction_token != (int *)*versionp) {
879 state->log(ISC_LOG_INFO, "samba_dlz: transaction not started for zone %s", zone);
880 return;
883 if (commit) {
884 if (ldb_transaction_commit(state->samdb) != LDB_SUCCESS) {
885 state->log(ISC_LOG_INFO, "samba_dlz: failed to commit a transaction for zone %s", zone);
886 return;
888 state->log(ISC_LOG_INFO, "samba_dlz: committed transaction on zone %s", zone);
889 } else {
890 if (ldb_transaction_cancel(state->samdb) != LDB_SUCCESS) {
891 state->log(ISC_LOG_INFO, "samba_dlz: failed to cancel a transaction for zone %s", zone);
892 return;
894 state->log(ISC_LOG_INFO, "samba_dlz: cancelling transaction on zone %s", zone);
897 talloc_free(state->transaction_token);
898 state->transaction_token = NULL;
899 *versionp = NULL;
904 see if there is a SOA record for a zone
906 static bool b9_has_soa(struct dlz_bind9_data *state, struct ldb_dn *dn, const char *zone)
908 const char *attrs[] = { "dnsRecord", NULL };
909 struct ldb_result *res;
910 struct ldb_message_element *el;
911 TALLOC_CTX *tmp_ctx = talloc_new(state);
912 int ret, i;
914 if (!ldb_dn_add_child_fmt(dn, "DC=@,DC=%s", zone)) {
915 talloc_free(tmp_ctx);
916 return false;
919 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
920 attrs, "objectClass=dnsNode");
921 if (ret != LDB_SUCCESS) {
922 talloc_free(tmp_ctx);
923 return false;
926 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
927 if (el == NULL) {
928 talloc_free(tmp_ctx);
929 return false;
931 for (i=0; i<el->num_values; i++) {
932 struct dnsp_DnssrvRpcRecord rec;
933 enum ndr_err_code ndr_err;
935 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec,
936 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
937 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
938 continue;
940 if (rec.wType == DNS_TYPE_SOA) {
941 talloc_free(tmp_ctx);
942 return true;
946 talloc_free(tmp_ctx);
947 return false;
951 configure a writeable zone
953 _PUBLIC_ isc_result_t dlz_configure(dns_view_t *view, void *dbdata)
955 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
956 TALLOC_CTX *tmp_ctx;
957 struct ldb_dn *dn;
958 int i;
960 state->log(ISC_LOG_INFO, "samba_dlz: starting configure");
961 if (state->writeable_zone == NULL) {
962 state->log(ISC_LOG_INFO, "samba_dlz: no writeable_zone method available");
963 return ISC_R_FAILURE;
966 tmp_ctx = talloc_new(state);
968 for (i=0; zone_prefixes[i]; i++) {
969 const char *attrs[] = { "name", NULL };
970 int j, ret;
971 struct ldb_result *res;
973 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
974 if (dn == NULL) {
975 talloc_free(tmp_ctx);
976 return ISC_R_NOMEMORY;
979 if (!ldb_dn_add_child_fmt(dn, "%s", zone_prefixes[i])) {
980 talloc_free(tmp_ctx);
981 return ISC_R_NOMEMORY;
984 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
985 attrs, "objectClass=dnsZone");
986 if (ret != LDB_SUCCESS) {
987 continue;
990 for (j=0; j<res->count; j++) {
991 isc_result_t result;
992 const char *zone = ldb_msg_find_attr_as_string(res->msgs[j], "name", NULL);
993 struct ldb_dn *zone_dn;
995 if (zone == NULL) {
996 continue;
998 zone_dn = ldb_dn_copy(tmp_ctx, dn);
999 if (zone_dn == NULL) {
1000 talloc_free(tmp_ctx);
1001 return ISC_R_NOMEMORY;
1004 if (!b9_has_soa(state, zone_dn, zone)) {
1005 continue;
1007 result = state->writeable_zone(view, zone);
1008 if (result != ISC_R_SUCCESS) {
1009 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to configure zone '%s'",
1010 zone);
1011 talloc_free(tmp_ctx);
1012 return result;
1014 state->log(ISC_LOG_INFO, "samba_dlz: configured writeable zone '%s'", zone);
1018 talloc_free(tmp_ctx);
1019 return ISC_R_SUCCESS;
1022 static char *strlower(char *str)
1024 int i;
1026 for (i=0; i<strlen(str); i++) {
1027 str[i] = (char) tolower(str[i]);
1030 return str;
1034 authorize a zone update
1036 _PUBLIC_ isc_boolean_t dlz_ssumatch(const char *signer, const char *name, const char *tcpaddr,
1037 const char *type, const char *key, uint32_t keydatalen, uint8_t *keydata,
1038 void *dbdata)
1040 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1041 TALLOC_CTX *tmp_ctx;
1042 DATA_BLOB ap_req;
1043 struct cli_credentials *server_credentials;
1044 char *keytab_name, *username;
1045 bool ret;
1046 int ldb_ret;
1047 NTSTATUS nt_status;
1048 struct gensec_security *gensec_ctx;
1049 struct auth_session_info *session_info;
1050 struct ldb_dn *dn;
1051 isc_result_t result;
1052 struct ldb_result *res;
1053 const char * attrs[] = { NULL };
1054 uint32_t access_mask;
1056 tmp_ctx = talloc_new(NULL);
1057 if (tmp_ctx == NULL) {
1058 state->log(ISC_LOG_ERROR, "samba_dlz: no memory");
1059 return false;
1062 ap_req = data_blob_const(keydata, keydatalen);
1063 server_credentials = cli_credentials_init(tmp_ctx);
1064 if (!server_credentials) {
1065 state->log(ISC_LOG_ERROR, "samba_dlz: failed to init server credentials");
1066 talloc_free(tmp_ctx);
1067 return false;
1070 cli_credentials_set_krb5_context(server_credentials, state->smb_krb5_ctx);
1071 cli_credentials_set_conf(server_credentials, state->lp);
1073 username = talloc_asprintf(tmp_ctx, "dns-%s", lpcfg_netbios_name(state->lp));
1074 username = strlower(username);
1075 cli_credentials_set_username(server_credentials, username, CRED_SPECIFIED);
1076 talloc_free(username);
1078 keytab_name = talloc_asprintf(tmp_ctx, "file:%s/dns.keytab",
1079 lpcfg_private_dir(state->lp));
1080 ret = cli_credentials_set_keytab_name(server_credentials, state->lp, keytab_name,
1081 CRED_SPECIFIED);
1082 talloc_free(keytab_name);
1083 if (ret != 0) {
1084 state->log(ISC_LOG_ERROR, "samba_dlz: failed to obtain server credentials for %s",
1085 username);
1086 talloc_free(tmp_ctx);
1087 return false;
1090 nt_status = gensec_server_start(tmp_ctx,
1091 lpcfg_gensec_settings(tmp_ctx, state->lp),
1092 NULL, &gensec_ctx);
1093 if (!NT_STATUS_IS_OK(nt_status)) {
1094 state->log(ISC_LOG_ERROR, "samba_dlz: failed to start gensec server");
1095 talloc_free(tmp_ctx);
1096 return false;
1099 gensec_set_credentials(gensec_ctx, server_credentials);
1100 gensec_set_target_service(gensec_ctx, "dns");
1102 nt_status = gensec_start_mech_by_name(gensec_ctx, "spnego");
1103 if (!NT_STATUS_IS_OK(nt_status)) {
1104 state->log(ISC_LOG_ERROR, "samba_dlz: failed to start spnego");
1105 talloc_free(tmp_ctx);
1106 return false;
1109 nt_status = gensec_update(gensec_ctx, tmp_ctx, state->ev_ctx, ap_req, &ap_req);
1110 if (!NT_STATUS_IS_OK(nt_status)) {
1111 state->log(ISC_LOG_ERROR, "samba_dlz: spnego update failed");
1112 talloc_free(tmp_ctx);
1113 return false;
1116 nt_status = gensec_session_info(gensec_ctx, tmp_ctx, &session_info);
1117 if (!NT_STATUS_IS_OK(nt_status)) {
1118 state->log(ISC_LOG_ERROR, "samba_dlz: failed to create session info");
1119 talloc_free(tmp_ctx);
1120 return false;
1123 /* Get the DN from name */
1124 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1125 if (result != ISC_R_SUCCESS) {
1126 state->log(ISC_LOG_ERROR, "samba_dlz: failed to find name %s", name);
1127 talloc_free(tmp_ctx);
1128 return false;
1131 /* make sure the dn exists, or find parent dn in case new object is being added */
1132 ldb_ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
1133 attrs, "objectClass=dnsNode");
1134 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
1135 ldb_dn_remove_child_components(dn, 1);
1136 access_mask = SEC_STD_REQUIRED | SEC_ADS_CREATE_CHILD;
1137 talloc_free(res);
1138 } else if (ldb_ret == LDB_SUCCESS) {
1139 access_mask = SEC_STD_REQUIRED | SEC_ADS_SELF_WRITE;
1140 talloc_free(res);
1141 } else {
1142 talloc_free(tmp_ctx);
1143 return false;
1146 /* Do ACL check */
1147 ldb_ret = dsdb_check_access_on_dn(state->samdb, tmp_ctx, dn,
1148 session_info->security_token,
1149 access_mask, NULL);
1150 if (ldb_ret != LDB_SUCCESS) {
1151 state->log(ISC_LOG_INFO,
1152 "samba_dlz: disallowing update of signer=%s name=%s type=%s error=%s",
1153 signer, name, type, ldb_strerror(ldb_ret));
1154 talloc_free(tmp_ctx);
1155 return false;
1158 state->log(ISC_LOG_INFO, "samba_dlz: allowing update of signer=%s name=%s tcpaddr=%s type=%s key=%s",
1159 signer, name, tcpaddr, type, key);
1161 talloc_free(tmp_ctx);
1162 return true;
1167 add a new record
1169 static isc_result_t b9_add_record(struct dlz_bind9_data *state, const char *name,
1170 struct ldb_dn *dn,
1171 struct dnsp_DnssrvRpcRecord *rec)
1173 struct ldb_message *msg;
1174 enum ndr_err_code ndr_err;
1175 struct ldb_val v;
1176 int ret;
1178 msg = ldb_msg_new(rec);
1179 if (msg == NULL) {
1180 return ISC_R_NOMEMORY;
1182 msg->dn = dn;
1183 ret = ldb_msg_add_string(msg, "objectClass", "dnsNode");
1184 if (ret != LDB_SUCCESS) {
1185 return ISC_R_FAILURE;
1188 ndr_err = ndr_push_struct_blob(&v, rec, rec, (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
1189 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1190 return ISC_R_FAILURE;
1192 ret = ldb_msg_add_value(msg, "dnsRecord", &v, NULL);
1193 if (ret != LDB_SUCCESS) {
1194 return ISC_R_FAILURE;
1197 ret = ldb_add(state->samdb, msg);
1198 if (ret != LDB_SUCCESS) {
1199 return ISC_R_FAILURE;
1202 return ISC_R_SUCCESS;
1206 see if two DNS names are the same
1208 static bool dns_name_equal(const char *name1, const char *name2)
1210 size_t len1 = strlen(name1);
1211 size_t len2 = strlen(name2);
1212 if (name1[len1-1] == '.') len1--;
1213 if (name2[len2-1] == '.') len2--;
1214 if (len1 != len2) {
1215 return false;
1217 return strncasecmp_m(name1, name2, len1) == 0;
1222 see if two dns records match
1224 static bool b9_record_match(struct dlz_bind9_data *state,
1225 struct dnsp_DnssrvRpcRecord *rec1, struct dnsp_DnssrvRpcRecord *rec2)
1227 if (rec1->wType != rec2->wType) {
1228 return false;
1230 /* see if this type is single valued */
1231 if (b9_single_valued(rec1->wType)) {
1232 return true;
1235 /* see if the data matches */
1236 switch (rec1->wType) {
1237 case DNS_TYPE_A:
1238 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
1239 case DNS_TYPE_AAAA:
1240 return strcmp(rec1->data.ipv6, rec2->data.ipv6) == 0;
1241 case DNS_TYPE_CNAME:
1242 return dns_name_equal(rec1->data.cname, rec2->data.cname);
1243 case DNS_TYPE_TXT:
1244 return strcmp(rec1->data.txt, rec2->data.txt) == 0;
1245 case DNS_TYPE_PTR:
1246 return strcmp(rec1->data.ptr, rec2->data.ptr) == 0;
1247 case DNS_TYPE_NS:
1248 return dns_name_equal(rec1->data.ns, rec2->data.ns);
1250 case DNS_TYPE_SRV:
1251 return rec1->data.srv.wPriority == rec2->data.srv.wPriority &&
1252 rec1->data.srv.wWeight == rec2->data.srv.wWeight &&
1253 rec1->data.srv.wPort == rec2->data.srv.wPort &&
1254 dns_name_equal(rec1->data.srv.nameTarget, rec2->data.srv.nameTarget);
1256 case DNS_TYPE_MX:
1257 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
1258 dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
1260 case DNS_TYPE_HINFO:
1261 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
1262 strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
1264 case DNS_TYPE_SOA:
1265 return dns_name_equal(rec1->data.soa.mname, rec2->data.soa.mname) &&
1266 dns_name_equal(rec1->data.soa.rname, rec2->data.soa.rname) &&
1267 rec1->data.soa.serial == rec2->data.soa.serial &&
1268 rec1->data.soa.refresh == rec2->data.soa.refresh &&
1269 rec1->data.soa.retry == rec2->data.soa.retry &&
1270 rec1->data.soa.expire == rec2->data.soa.expire &&
1271 rec1->data.soa.minimum == rec2->data.soa.minimum;
1272 default:
1273 state->log(ISC_LOG_ERROR, "samba b9_putrr: unhandled record type %u",
1274 rec1->wType);
1275 break;
1278 return false;
1283 add or modify a rdataset
1285 _PUBLIC_ isc_result_t dlz_addrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1287 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1288 struct dnsp_DnssrvRpcRecord *rec;
1289 struct ldb_dn *dn;
1290 isc_result_t result;
1291 struct ldb_result *res;
1292 const char *attrs[] = { "dnsRecord", NULL };
1293 int ret, i;
1294 struct ldb_message_element *el;
1295 enum ndr_err_code ndr_err;
1296 NTTIME t;
1298 if (state->transaction_token != (void*)version) {
1299 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1300 return ISC_R_FAILURE;
1303 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1304 if (rec == NULL) {
1305 return ISC_R_NOMEMORY;
1308 unix_to_nt_time(&t, time(NULL));
1309 t /= 10*1000*1000; /* convert to seconds (NT time is in 100ns units) */
1310 t /= 3600; /* convert to hours */
1312 rec->rank = DNS_RANK_ZONE;
1313 rec->dwSerial = state->soa_serial;
1314 rec->dwTimeStamp = (uint32_t)t;
1316 if (!b9_parse(state, rdatastr, rec)) {
1317 state->log(ISC_LOG_INFO, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1318 talloc_free(rec);
1319 return ISC_R_FAILURE;
1322 /* find the DN of the record */
1323 result = b9_find_name_dn(state, name, rec, &dn);
1324 if (result != ISC_R_SUCCESS) {
1325 talloc_free(rec);
1326 return result;
1329 /* get any existing records */
1330 ret = ldb_search(state->samdb, rec, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1331 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1332 result = b9_add_record(state, name, dn, rec);
1333 talloc_free(rec);
1334 if (result == ISC_R_SUCCESS) {
1335 state->log(ISC_LOG_ERROR, "samba_dlz: added %s %s", name, rdatastr);
1337 return result;
1340 /* there are existing records. We need to see if this will
1341 * replace a record or add to it
1343 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1344 if (el == NULL) {
1345 state->log(ISC_LOG_ERROR, "samba_dlz: no dnsRecord attribute for %s",
1346 ldb_dn_get_linearized(dn));
1347 talloc_free(rec);
1348 return ISC_R_FAILURE;
1351 for (i=0; i<el->num_values; i++) {
1352 struct dnsp_DnssrvRpcRecord rec2;
1354 ndr_err = ndr_pull_struct_blob(&el->values[i], rec, &rec2,
1355 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1356 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1357 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1358 ldb_dn_get_linearized(dn));
1359 talloc_free(rec);
1360 return ISC_R_FAILURE;
1363 if (b9_record_match(state, rec, &rec2)) {
1364 break;
1367 if (i == el->num_values) {
1368 /* adding a new value */
1369 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values+1);
1370 if (el->values == NULL) {
1371 talloc_free(rec);
1372 return ISC_R_NOMEMORY;
1374 el->num_values++;
1377 ndr_err = ndr_push_struct_blob(&el->values[i], rec, rec,
1378 (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
1379 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1380 state->log(ISC_LOG_ERROR, "samba_dlz: failed to push dnsRecord for %s",
1381 ldb_dn_get_linearized(dn));
1382 talloc_free(rec);
1383 return ISC_R_FAILURE;
1386 /* modify the record */
1387 el->flags = LDB_FLAG_MOD_REPLACE;
1388 ret = ldb_modify(state->samdb, res->msgs[0]);
1389 if (ret != LDB_SUCCESS) {
1390 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1391 ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1392 talloc_free(rec);
1393 return ISC_R_FAILURE;
1396 state->log(ISC_LOG_INFO, "samba_dlz: added rdataset %s '%s'", name, rdatastr);
1398 talloc_free(rec);
1399 return ISC_R_SUCCESS;
1403 remove a rdataset
1405 _PUBLIC_ isc_result_t dlz_subrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1407 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1408 struct dnsp_DnssrvRpcRecord *rec;
1409 struct ldb_dn *dn;
1410 isc_result_t result;
1411 struct ldb_result *res;
1412 const char *attrs[] = { "dnsRecord", NULL };
1413 int ret, i;
1414 struct ldb_message_element *el;
1415 enum ndr_err_code ndr_err;
1417 if (state->transaction_token != (void*)version) {
1418 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1419 return ISC_R_FAILURE;
1422 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1423 if (rec == NULL) {
1424 return ISC_R_NOMEMORY;
1427 if (!b9_parse(state, rdatastr, rec)) {
1428 state->log(ISC_LOG_INFO, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1429 talloc_free(rec);
1430 return ISC_R_FAILURE;
1433 /* find the DN of the record */
1434 result = b9_find_name_dn(state, name, rec, &dn);
1435 if (result != ISC_R_SUCCESS) {
1436 talloc_free(rec);
1437 return result;
1440 /* get the existing records */
1441 ret = ldb_search(state->samdb, rec, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1442 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1443 talloc_free(rec);
1444 return ISC_R_NOTFOUND;
1447 /* there are existing records. We need to see if any match
1449 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1450 if (el == NULL || el->num_values == 0) {
1451 state->log(ISC_LOG_ERROR, "samba_dlz: no dnsRecord attribute for %s",
1452 ldb_dn_get_linearized(dn));
1453 talloc_free(rec);
1454 return ISC_R_FAILURE;
1457 for (i=0; i<el->num_values; i++) {
1458 struct dnsp_DnssrvRpcRecord rec2;
1460 ndr_err = ndr_pull_struct_blob(&el->values[i], rec, &rec2,
1461 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1462 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1463 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1464 ldb_dn_get_linearized(dn));
1465 talloc_free(rec);
1466 return ISC_R_FAILURE;
1469 if (b9_record_match(state, rec, &rec2)) {
1470 break;
1473 if (i == el->num_values) {
1474 talloc_free(rec);
1475 return ISC_R_NOTFOUND;
1478 if (i < el->num_values-1) {
1479 memmove(&el->values[i], &el->values[i+1], sizeof(el->values[0])*((el->num_values-1)-i));
1481 el->num_values--;
1483 if (el->num_values == 0) {
1484 /* delete the record */
1485 ret = ldb_delete(state->samdb, dn);
1486 } else {
1487 /* modify the record */
1488 el->flags = LDB_FLAG_MOD_REPLACE;
1489 ret = ldb_modify(state->samdb, res->msgs[0]);
1491 if (ret != LDB_SUCCESS) {
1492 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1493 ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1494 talloc_free(rec);
1495 return ISC_R_FAILURE;
1498 state->log(ISC_LOG_INFO, "samba_dlz: subtracted rdataset %s '%s'", name, rdatastr);
1500 talloc_free(rec);
1501 return ISC_R_SUCCESS;
1506 delete all records of the given type
1508 _PUBLIC_ isc_result_t dlz_delrdataset(const char *name, const char *type, void *dbdata, void *version)
1510 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1511 TALLOC_CTX *tmp_ctx;
1512 struct ldb_dn *dn;
1513 isc_result_t result;
1514 struct ldb_result *res;
1515 const char *attrs[] = { "dnsRecord", NULL };
1516 int ret, i;
1517 struct ldb_message_element *el;
1518 enum ndr_err_code ndr_err;
1519 enum dns_record_type dns_type;
1520 bool found = false;
1522 if (state->transaction_token != (void*)version) {
1523 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1524 return ISC_R_FAILURE;
1527 if (!b9_dns_type(type, &dns_type)) {
1528 state->log(ISC_LOG_INFO, "samba_dlz: bad dns type %s in delete", type);
1529 return ISC_R_FAILURE;
1532 tmp_ctx = talloc_new(state);
1534 /* find the DN of the record */
1535 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1536 if (result != ISC_R_SUCCESS) {
1537 talloc_free(tmp_ctx);
1538 return result;
1541 /* get the existing records */
1542 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1543 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1544 talloc_free(tmp_ctx);
1545 return ISC_R_NOTFOUND;
1548 /* there are existing records. We need to see if any match the type
1550 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1551 if (el == NULL || el->num_values == 0) {
1552 talloc_free(tmp_ctx);
1553 return ISC_R_NOTFOUND;
1556 for (i=0; i<el->num_values; i++) {
1557 struct dnsp_DnssrvRpcRecord rec2;
1559 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec2,
1560 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1561 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1562 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1563 ldb_dn_get_linearized(dn));
1564 talloc_free(tmp_ctx);
1565 return ISC_R_FAILURE;
1568 if (dns_type == rec2.wType) {
1569 if (i < el->num_values-1) {
1570 memmove(&el->values[i], &el->values[i+1],
1571 sizeof(el->values[0])*((el->num_values-1)-i));
1573 el->num_values--;
1574 i--;
1575 found = true;
1579 if (!found) {
1580 talloc_free(tmp_ctx);
1581 return ISC_R_FAILURE;
1584 if (el->num_values == 0) {
1585 /* delete the record */
1586 ret = ldb_delete(state->samdb, dn);
1587 } else {
1588 /* modify the record */
1589 el->flags = LDB_FLAG_MOD_REPLACE;
1590 ret = ldb_modify(state->samdb, res->msgs[0]);
1592 if (ret != LDB_SUCCESS) {
1593 state->log(ISC_LOG_ERROR, "samba_dlz: failed to delete type %s in %s - %s",
1594 type, ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1595 talloc_free(tmp_ctx);
1596 return ISC_R_FAILURE;
1599 state->log(ISC_LOG_INFO, "samba_dlz: deleted rdataset %s of type %s", name, type);
1601 talloc_free(tmp_ctx);
1602 return ISC_R_SUCCESS;