ctdb-scripts: Try to restart statd after every 10 failures
[Samba.git] / source4 / dns_server / dlz_bind9.c
blobd43b40478a6ee95b6146a49759265b78856e6cee
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 "lib/cmdline/popt_common.h"
39 #include "lib/util/dlinklist.h"
40 #include "dlz_minimal.h"
41 #include "dns_server/dnsserver_common.h"
43 struct b9_options {
44 const char *url;
45 const char *debug;
48 struct b9_zone {
49 char *name;
50 struct b9_zone *prev, *next;
53 struct dlz_bind9_data {
54 struct b9_options options;
55 struct ldb_context *samdb;
56 struct tevent_context *ev_ctx;
57 struct loadparm_context *lp;
58 int *transaction_token;
59 uint32_t soa_serial;
60 struct b9_zone *zonelist;
62 /* Used for dynamic update */
63 struct smb_krb5_context *smb_krb5_ctx;
64 struct auth4_context *auth_context;
65 struct auth_session_info *session_info;
66 char *update_name;
68 /* helper functions from the dlz_dlopen driver */
69 log_t *log;
70 dns_sdlz_putrr_t *putrr;
71 dns_sdlz_putnamedrr_t *putnamedrr;
72 dns_dlz_writeablezone_t *writeable_zone;
75 static struct dlz_bind9_data *dlz_bind9_state = NULL;
76 static int dlz_bind9_state_ref_count = 0;
78 static const char *zone_prefixes[] = {
79 "CN=MicrosoftDNS,DC=DomainDnsZones",
80 "CN=MicrosoftDNS,DC=ForestDnsZones",
81 "CN=MicrosoftDNS,CN=System",
82 NULL
86 return the version of the API
88 _PUBLIC_ int dlz_version(unsigned int *flags)
90 return DLZ_DLOPEN_VERSION;
94 remember a helper function from the bind9 dlz_dlopen driver
96 static void b9_add_helper(struct dlz_bind9_data *state, const char *helper_name, void *ptr)
98 if (strcmp(helper_name, "log") == 0) {
99 state->log = ptr;
101 if (strcmp(helper_name, "putrr") == 0) {
102 state->putrr = ptr;
104 if (strcmp(helper_name, "putnamedrr") == 0) {
105 state->putnamedrr = ptr;
107 if (strcmp(helper_name, "writeable_zone") == 0) {
108 state->writeable_zone = ptr;
113 format a record for bind9
115 static bool b9_format(struct dlz_bind9_data *state,
116 TALLOC_CTX *mem_ctx,
117 struct dnsp_DnssrvRpcRecord *rec,
118 const char **type, const char **data)
120 uint32_t i;
121 char *tmp;
123 switch (rec->wType) {
124 case DNS_TYPE_A:
125 *type = "a";
126 *data = rec->data.ipv4;
127 break;
129 case DNS_TYPE_AAAA:
130 *type = "aaaa";
131 *data = rec->data.ipv6;
132 break;
134 case DNS_TYPE_CNAME:
135 *type = "cname";
136 *data = rec->data.cname;
137 break;
139 case DNS_TYPE_TXT:
140 *type = "txt";
141 tmp = talloc_asprintf(mem_ctx, "\"%s\"", rec->data.txt.str[0]);
142 for (i=1; i<rec->data.txt.count; i++) {
143 tmp = talloc_asprintf_append(tmp, " \"%s\"", rec->data.txt.str[i]);
145 *data = tmp;
146 break;
148 case DNS_TYPE_PTR:
149 *type = "ptr";
150 *data = rec->data.ptr;
151 break;
153 case DNS_TYPE_SRV:
154 *type = "srv";
155 *data = talloc_asprintf(mem_ctx, "%u %u %u %s",
156 rec->data.srv.wPriority,
157 rec->data.srv.wWeight,
158 rec->data.srv.wPort,
159 rec->data.srv.nameTarget);
160 break;
162 case DNS_TYPE_MX:
163 *type = "mx";
164 *data = talloc_asprintf(mem_ctx, "%u %s",
165 rec->data.mx.wPriority,
166 rec->data.mx.nameTarget);
167 break;
169 case DNS_TYPE_HINFO:
170 *type = "hinfo";
171 *data = talloc_asprintf(mem_ctx, "%s %s",
172 rec->data.hinfo.cpu,
173 rec->data.hinfo.os);
174 break;
176 case DNS_TYPE_NS:
177 *type = "ns";
178 *data = rec->data.ns;
179 break;
181 case DNS_TYPE_SOA: {
182 const char *mname;
183 *type = "soa";
185 /* we need to fake the authoritative nameserver to
186 * point at ourselves. This is how AD DNS servers
187 * force clients to send updates to the right local DC
189 mname = talloc_asprintf(mem_ctx, "%s.%s",
190 lpcfg_netbios_name(state->lp), lpcfg_dnsdomain(state->lp));
191 if (mname == NULL) {
192 return false;
194 mname = strlower_talloc(mem_ctx, mname);
195 if (mname == NULL) {
196 return false;
199 state->soa_serial = rec->data.soa.serial;
201 *data = talloc_asprintf(mem_ctx, "%s %s %u %u %u %u %u",
202 mname,
203 rec->data.soa.rname,
204 rec->data.soa.serial,
205 rec->data.soa.refresh,
206 rec->data.soa.retry,
207 rec->data.soa.expire,
208 rec->data.soa.minimum);
209 break;
212 default:
213 state->log(ISC_LOG_ERROR, "samba_dlz b9_format: unhandled record type %u",
214 rec->wType);
215 return false;
218 return true;
221 static const struct {
222 enum dns_record_type dns_type;
223 const char *typestr;
224 bool single_valued;
225 } dns_typemap[] = {
226 { DNS_TYPE_A, "A" , false},
227 { DNS_TYPE_AAAA, "AAAA" , false},
228 { DNS_TYPE_CNAME, "CNAME" , true},
229 { DNS_TYPE_TXT, "TXT" , false},
230 { DNS_TYPE_PTR, "PTR" , false},
231 { DNS_TYPE_SRV, "SRV" , false},
232 { DNS_TYPE_MX, "MX" , false},
233 { DNS_TYPE_HINFO, "HINFO" , false},
234 { DNS_TYPE_NS, "NS" , false},
235 { DNS_TYPE_SOA, "SOA" , true},
240 see if a DNS type is single valued
242 static bool b9_single_valued(enum dns_record_type dns_type)
244 int i;
245 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
246 if (dns_typemap[i].dns_type == dns_type) {
247 return dns_typemap[i].single_valued;
250 return false;
254 see if a DNS type is single valued
256 static bool b9_dns_type(const char *type, enum dns_record_type *dtype)
258 int i;
259 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
260 if (strcasecmp(dns_typemap[i].typestr, type) == 0) {
261 *dtype = dns_typemap[i].dns_type;
262 return true;
265 return false;
269 #define DNS_PARSE_STR(ret, str, sep, saveptr) do { \
270 (ret) = strtok_r(str, sep, &saveptr); \
271 if ((ret) == NULL) return false; \
272 } while (0)
274 #define DNS_PARSE_UINT(ret, str, sep, saveptr) do { \
275 char *istr = strtok_r(str, sep, &saveptr); \
276 if ((istr) == NULL) return false; \
277 (ret) = strtoul(istr, NULL, 10); \
278 } while (0)
281 parse a record from bind9
283 static bool b9_parse(struct dlz_bind9_data *state,
284 const char *rdatastr,
285 struct dnsp_DnssrvRpcRecord *rec)
287 char *full_name, *dclass, *type;
288 char *str, *tmp, *saveptr=NULL;
289 int i;
291 str = talloc_strdup(rec, rdatastr);
292 if (str == NULL) {
293 return false;
296 /* parse the SDLZ string form */
297 DNS_PARSE_STR(full_name, str, "\t", saveptr);
298 DNS_PARSE_UINT(rec->dwTtlSeconds, NULL, "\t", saveptr);
299 DNS_PARSE_STR(dclass, NULL, "\t", saveptr);
300 DNS_PARSE_STR(type, NULL, "\t", saveptr);
302 /* construct the record */
303 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
304 if (strcasecmp(type, dns_typemap[i].typestr) == 0) {
305 rec->wType = dns_typemap[i].dns_type;
306 break;
309 if (i == ARRAY_SIZE(dns_typemap)) {
310 state->log(ISC_LOG_ERROR, "samba_dlz: unsupported record type '%s' for '%s'",
311 type, full_name);
312 return false;
315 switch (rec->wType) {
316 case DNS_TYPE_A:
317 DNS_PARSE_STR(rec->data.ipv4, NULL, " ", saveptr);
318 break;
320 case DNS_TYPE_AAAA:
321 DNS_PARSE_STR(rec->data.ipv6, NULL, " ", saveptr);
322 break;
324 case DNS_TYPE_CNAME:
325 DNS_PARSE_STR(rec->data.cname, NULL, " ", saveptr);
326 break;
328 case DNS_TYPE_TXT:
329 rec->data.txt.count = 0;
330 rec->data.txt.str = talloc_array(rec, const char *, rec->data.txt.count);
331 tmp = strtok_r(NULL, "\t", &saveptr);
332 while (tmp) {
333 rec->data.txt.str = talloc_realloc(rec, rec->data.txt.str, const char *,
334 rec->data.txt.count+1);
335 if (tmp[0] == '"') {
336 /* Strip quotes */
337 rec->data.txt.str[rec->data.txt.count] = talloc_strndup(rec, &tmp[1], strlen(tmp)-2);
338 } else {
339 rec->data.txt.str[rec->data.txt.count] = talloc_strdup(rec, tmp);
341 rec->data.txt.count++;
342 tmp = strtok_r(NULL, " ", &saveptr);
344 break;
346 case DNS_TYPE_PTR:
347 DNS_PARSE_STR(rec->data.ptr, NULL, " ", saveptr);
348 break;
350 case DNS_TYPE_SRV:
351 DNS_PARSE_UINT(rec->data.srv.wPriority, NULL, " ", saveptr);
352 DNS_PARSE_UINT(rec->data.srv.wWeight, NULL, " ", saveptr);
353 DNS_PARSE_UINT(rec->data.srv.wPort, NULL, " ", saveptr);
354 DNS_PARSE_STR(rec->data.srv.nameTarget, NULL, " ", saveptr);
355 break;
357 case DNS_TYPE_MX:
358 DNS_PARSE_UINT(rec->data.mx.wPriority, NULL, " ", saveptr);
359 DNS_PARSE_STR(rec->data.mx.nameTarget, NULL, " ", saveptr);
360 break;
362 case DNS_TYPE_HINFO:
363 DNS_PARSE_STR(rec->data.hinfo.cpu, NULL, " ", saveptr);
364 DNS_PARSE_STR(rec->data.hinfo.os, NULL, " ", saveptr);
365 break;
367 case DNS_TYPE_NS:
368 DNS_PARSE_STR(rec->data.ns, NULL, " ", saveptr);
369 break;
371 case DNS_TYPE_SOA:
372 DNS_PARSE_STR(rec->data.soa.mname, NULL, " ", saveptr);
373 DNS_PARSE_STR(rec->data.soa.rname, NULL, " ", saveptr);
374 DNS_PARSE_UINT(rec->data.soa.serial, NULL, " ", saveptr);
375 DNS_PARSE_UINT(rec->data.soa.refresh, NULL, " ", saveptr);
376 DNS_PARSE_UINT(rec->data.soa.retry, NULL, " ", saveptr);
377 DNS_PARSE_UINT(rec->data.soa.expire, NULL, " ", saveptr);
378 DNS_PARSE_UINT(rec->data.soa.minimum, NULL, " ", saveptr);
379 break;
381 default:
382 state->log(ISC_LOG_ERROR, "samba_dlz b9_parse: unhandled record type %u",
383 rec->wType);
384 return false;
387 /* we should be at the end of the buffer now */
388 if (strtok_r(NULL, "\t ", &saveptr) != NULL) {
389 state->log(ISC_LOG_ERROR, "samba_dlz b9_parse: unexpected data at end of string for '%s'",
390 rdatastr);
391 return false;
394 return true;
398 send a resource record to bind9
400 static isc_result_t b9_putrr(struct dlz_bind9_data *state,
401 void *handle, struct dnsp_DnssrvRpcRecord *rec,
402 const char **types)
404 isc_result_t result;
405 const char *type, *data;
406 TALLOC_CTX *tmp_ctx = talloc_new(state);
408 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
409 return ISC_R_FAILURE;
412 if (data == NULL) {
413 talloc_free(tmp_ctx);
414 return ISC_R_NOMEMORY;
417 if (types) {
418 int i;
419 for (i=0; types[i]; i++) {
420 if (strcmp(types[i], type) == 0) break;
422 if (types[i] == NULL) {
423 /* skip it */
424 return ISC_R_SUCCESS;
428 result = state->putrr(handle, type, rec->dwTtlSeconds, data);
429 if (result != ISC_R_SUCCESS) {
430 state->log(ISC_LOG_ERROR, "Failed to put rr");
432 talloc_free(tmp_ctx);
433 return result;
438 send a named resource record to bind9
440 static isc_result_t b9_putnamedrr(struct dlz_bind9_data *state,
441 void *handle, const char *name,
442 struct dnsp_DnssrvRpcRecord *rec)
444 isc_result_t result;
445 const char *type, *data;
446 TALLOC_CTX *tmp_ctx = talloc_new(state);
448 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
449 return ISC_R_FAILURE;
452 if (data == NULL) {
453 talloc_free(tmp_ctx);
454 return ISC_R_NOMEMORY;
457 result = state->putnamedrr(handle, name, type, rec->dwTtlSeconds, data);
458 if (result != ISC_R_SUCCESS) {
459 state->log(ISC_LOG_ERROR, "Failed to put named rr '%s'", name);
461 talloc_free(tmp_ctx);
462 return result;
466 parse options
468 static isc_result_t parse_options(struct dlz_bind9_data *state,
469 unsigned int argc, const char **argv,
470 struct b9_options *options)
472 int opt;
473 poptContext pc;
474 struct poptOption long_options[] = {
475 { "url", 'H', POPT_ARG_STRING, &options->url, 0, "database URL", "URL" },
476 { "debug", 'd', POPT_ARG_STRING, &options->debug, 0, "debug level", "DEBUG" },
477 { NULL }
480 pc = poptGetContext("dlz_bind9", argc, argv, long_options,
481 POPT_CONTEXT_KEEP_FIRST);
482 while ((opt = poptGetNextOpt(pc)) != -1) {
483 switch (opt) {
484 default:
485 state->log(ISC_LOG_ERROR, "dlz_bind9: Invalid option %s: %s",
486 poptBadOption(pc, 0), poptStrerror(opt));
487 return ISC_R_FAILURE;
491 return ISC_R_SUCCESS;
496 * Create session info from PAC
497 * This is called as auth_context->generate_session_info_pac()
499 static NTSTATUS b9_generate_session_info_pac(struct auth4_context *auth_context,
500 TALLOC_CTX *mem_ctx,
501 struct smb_krb5_context *smb_krb5_context,
502 DATA_BLOB *pac_blob,
503 const char *principal_name,
504 const struct tsocket_address *remote_addr,
505 uint32_t session_info_flags,
506 struct auth_session_info **session_info)
508 NTSTATUS status;
509 struct auth_user_info_dc *user_info_dc;
510 TALLOC_CTX *tmp_ctx;
512 tmp_ctx = talloc_new(mem_ctx);
513 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
515 status = kerberos_pac_blob_to_user_info_dc(tmp_ctx,
516 *pac_blob,
517 smb_krb5_context->krb5_context,
518 &user_info_dc,
519 NULL,
520 NULL);
521 if (!NT_STATUS_IS_OK(status)) {
522 talloc_free(tmp_ctx);
523 return status;
526 if (user_info_dc->info->authenticated) {
527 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
530 session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
532 status = auth_generate_session_info(mem_ctx, NULL, NULL, user_info_dc,
533 session_info_flags, session_info);
534 if (!NT_STATUS_IS_OK(status)) {
535 talloc_free(tmp_ctx);
536 return status;
539 talloc_free(tmp_ctx);
540 return status;
543 /* Callback for the DEBUG() system, to catch the remaining messages */
544 static void b9_debug(void *private_ptr, int msg_level, const char *msg)
546 static const int isc_log_map[] = {
547 ISC_LOG_CRITICAL, /* 0 */
548 ISC_LOG_ERROR, /* 1 */
549 ISC_LOG_WARNING, /* 2 */
550 ISC_LOG_NOTICE /* 3 */
552 struct dlz_bind9_data *state = private_ptr;
553 int isc_log_level;
555 if (msg_level >= ARRAY_SIZE(isc_log_map) || msg_level < 0) {
556 isc_log_level = ISC_LOG_INFO;
557 } else {
558 isc_log_level = isc_log_map[msg_level];
560 state->log(isc_log_level, "samba_dlz: %s", msg);
563 static int dlz_state_debug_unregister(struct dlz_bind9_data *state)
565 /* Stop logging (to the bind9 logs) */
566 debug_set_callback(NULL, NULL);
567 return 0;
571 called to initialise the driver
573 _PUBLIC_ isc_result_t dlz_create(const char *dlzname,
574 unsigned int argc, const char **argv,
575 void **dbdata, ...)
577 struct dlz_bind9_data *state;
578 const char *helper_name;
579 va_list ap;
580 isc_result_t result;
581 struct ldb_dn *dn;
582 NTSTATUS nt_status;
584 if (dlz_bind9_state != NULL) {
585 *dbdata = dlz_bind9_state;
586 dlz_bind9_state_ref_count++;
587 return ISC_R_SUCCESS;
590 state = talloc_zero(NULL, struct dlz_bind9_data);
591 if (state == NULL) {
592 return ISC_R_NOMEMORY;
595 talloc_set_destructor(state, dlz_state_debug_unregister);
597 /* fill in the helper functions */
598 va_start(ap, dbdata);
599 while ((helper_name = va_arg(ap, const char *)) != NULL) {
600 b9_add_helper(state, helper_name, va_arg(ap, void*));
602 va_end(ap);
604 /* Do not install samba signal handlers */
605 fault_setup_disable();
607 /* Start logging (to the bind9 logs) */
608 debug_set_callback(state, b9_debug);
610 state->ev_ctx = s4_event_context_init(state);
611 if (state->ev_ctx == NULL) {
612 result = ISC_R_NOMEMORY;
613 goto failed;
616 result = parse_options(state, argc, argv, &state->options);
617 if (result != ISC_R_SUCCESS) {
618 goto failed;
621 state->lp = loadparm_init_global(true);
622 if (state->lp == NULL) {
623 result = ISC_R_NOMEMORY;
624 goto failed;
627 if (state->options.debug) {
628 lpcfg_do_global_parameter(state->lp, "log level", state->options.debug);
629 } else {
630 lpcfg_do_global_parameter(state->lp, "log level", "0");
633 if (smb_krb5_init_context(state, state->lp, &state->smb_krb5_ctx) != 0) {
634 result = ISC_R_NOMEMORY;
635 goto failed;
638 nt_status = gensec_init();
639 if (!NT_STATUS_IS_OK(nt_status)) {
640 result = ISC_R_NOMEMORY;
641 goto failed;
644 state->auth_context = talloc_zero(state, struct auth4_context);
645 if (state->auth_context == NULL) {
646 result = ISC_R_NOMEMORY;
647 goto failed;
650 if (state->options.url == NULL) {
651 state->options.url = lpcfg_private_path(state, state->lp, "dns/sam.ldb");
652 if (state->options.url == NULL) {
653 result = ISC_R_NOMEMORY;
654 goto failed;
658 state->samdb = samdb_connect_url(state, state->ev_ctx, state->lp,
659 system_session(state->lp), 0, state->options.url);
660 if (state->samdb == NULL) {
661 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to connect to %s",
662 state->options.url);
663 result = ISC_R_FAILURE;
664 goto failed;
667 dn = ldb_get_default_basedn(state->samdb);
668 if (dn == NULL) {
669 state->log(ISC_LOG_ERROR, "samba_dlz: Unable to get basedn for %s - %s",
670 state->options.url, ldb_errstring(state->samdb));
671 result = ISC_R_FAILURE;
672 goto failed;
675 state->log(ISC_LOG_INFO, "samba_dlz: started for DN %s",
676 ldb_dn_get_linearized(dn));
678 state->auth_context->event_ctx = state->ev_ctx;
679 state->auth_context->lp_ctx = state->lp;
680 state->auth_context->sam_ctx = state->samdb;
681 state->auth_context->generate_session_info_pac = b9_generate_session_info_pac;
683 *dbdata = state;
684 dlz_bind9_state = state;
685 dlz_bind9_state_ref_count++;
687 return ISC_R_SUCCESS;
689 failed:
690 talloc_free(state);
691 return result;
695 shutdown the backend
697 _PUBLIC_ void dlz_destroy(void *dbdata)
699 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
700 state->log(ISC_LOG_INFO, "samba_dlz: shutting down");
702 dlz_bind9_state_ref_count--;
703 if (dlz_bind9_state_ref_count == 0) {
704 talloc_unlink(state, state->samdb);
705 talloc_free(state);
706 dlz_bind9_state = NULL;
712 return the base DN for a zone
714 static isc_result_t b9_find_zone_dn(struct dlz_bind9_data *state, const char *zone_name,
715 TALLOC_CTX *mem_ctx, struct ldb_dn **zone_dn)
717 int ret;
718 TALLOC_CTX *tmp_ctx = talloc_new(state);
719 const char *attrs[] = { NULL };
720 int i;
722 for (i=0; zone_prefixes[i]; i++) {
723 struct ldb_dn *dn;
724 struct ldb_result *res;
726 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
727 if (dn == NULL) {
728 talloc_free(tmp_ctx);
729 return ISC_R_NOMEMORY;
732 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone_name, zone_prefixes[i])) {
733 talloc_free(tmp_ctx);
734 return ISC_R_NOMEMORY;
737 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsZone");
738 if (ret == LDB_SUCCESS) {
739 if (zone_dn != NULL) {
740 *zone_dn = talloc_steal(mem_ctx, dn);
742 talloc_free(tmp_ctx);
743 return ISC_R_SUCCESS;
745 talloc_free(dn);
748 talloc_free(tmp_ctx);
749 return ISC_R_NOTFOUND;
754 return the DN for a name. The record does not need to exist, but the
755 zone must exist
757 static isc_result_t b9_find_name_dn(struct dlz_bind9_data *state, const char *name,
758 TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
760 const char *p;
762 /* work through the name piece by piece, until we find a zone */
763 for (p=name; p; ) {
764 isc_result_t result;
765 result = b9_find_zone_dn(state, p, mem_ctx, dn);
766 if (result == ISC_R_SUCCESS) {
767 /* we found a zone, now extend the DN to get
768 * the full DN
770 bool ret;
771 if (p == name) {
772 ret = ldb_dn_add_child_fmt(*dn, "DC=@");
773 } else {
774 ret = ldb_dn_add_child_fmt(*dn, "DC=%.*s", (int)(p-name)-1, name);
776 if (!ret) {
777 talloc_free(*dn);
778 return ISC_R_NOMEMORY;
780 return ISC_R_SUCCESS;
782 p = strchr(p, '.');
783 if (p == NULL) {
784 break;
786 p++;
788 return ISC_R_NOTFOUND;
793 see if we handle a given zone
795 #if DLZ_DLOPEN_VERSION < 3
796 _PUBLIC_ isc_result_t dlz_findzonedb(void *dbdata, const char *name)
797 #else
798 _PUBLIC_ isc_result_t dlz_findzonedb(void *dbdata, const char *name,
799 dns_clientinfomethods_t *methods,
800 dns_clientinfo_t *clientinfo)
801 #endif
803 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
804 return b9_find_zone_dn(state, name, NULL, NULL);
809 lookup one record
811 static isc_result_t dlz_lookup_types(struct dlz_bind9_data *state,
812 const char *zone, const char *name,
813 dns_sdlzlookup_t *lookup,
814 const char **types)
816 TALLOC_CTX *tmp_ctx = talloc_new(state);
817 struct ldb_dn *dn;
818 WERROR werr = WERR_DNS_ERROR_NAME_DOES_NOT_EXIST;
819 struct dnsp_DnssrvRpcRecord *records = NULL;
820 uint16_t num_records = 0, i;
822 for (i=0; zone_prefixes[i]; i++) {
823 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
824 if (dn == NULL) {
825 talloc_free(tmp_ctx);
826 return ISC_R_NOMEMORY;
829 if (!ldb_dn_add_child_fmt(dn, "DC=%s,DC=%s,%s", name, zone, zone_prefixes[i])) {
830 talloc_free(tmp_ctx);
831 return ISC_R_NOMEMORY;
834 werr = dns_common_lookup(state->samdb, tmp_ctx, dn,
835 &records, &num_records, NULL);
836 if (W_ERROR_IS_OK(werr)) {
837 break;
840 if (!W_ERROR_IS_OK(werr)) {
841 talloc_free(tmp_ctx);
842 return ISC_R_NOTFOUND;
845 for (i=0; i < num_records; i++) {
846 isc_result_t result;
848 result = b9_putrr(state, lookup, &records[i], types);
849 if (result != ISC_R_SUCCESS) {
850 talloc_free(tmp_ctx);
851 return result;
855 talloc_free(tmp_ctx);
856 return ISC_R_SUCCESS;
860 lookup one record
862 #if DLZ_DLOPEN_VERSION == 1
863 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name,
864 void *dbdata, dns_sdlzlookup_t *lookup)
865 #else
866 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name,
867 void *dbdata, dns_sdlzlookup_t *lookup,
868 dns_clientinfomethods_t *methods,
869 dns_clientinfo_t *clientinfo)
870 #endif
872 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
873 return dlz_lookup_types(state, zone, name, lookup, NULL);
878 see if a zone transfer is allowed
880 _PUBLIC_ isc_result_t dlz_allowzonexfr(void *dbdata, const char *name, const char *client)
882 /* just say yes for all our zones for now */
883 struct dlz_bind9_data *state = talloc_get_type(
884 dbdata, struct dlz_bind9_data);
885 return b9_find_zone_dn(state, name, NULL, NULL);
889 perform a zone transfer
891 _PUBLIC_ isc_result_t dlz_allnodes(const char *zone, void *dbdata,
892 dns_sdlzallnodes_t *allnodes)
894 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
895 const char *attrs[] = { "dnsRecord", NULL };
896 int ret = LDB_SUCCESS, i, j;
897 struct ldb_dn *dn;
898 struct ldb_result *res;
899 TALLOC_CTX *tmp_ctx = talloc_new(state);
901 for (i=0; zone_prefixes[i]; i++) {
902 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
903 if (dn == NULL) {
904 talloc_free(tmp_ctx);
905 return ISC_R_NOMEMORY;
908 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone, zone_prefixes[i])) {
909 talloc_free(tmp_ctx);
910 return ISC_R_NOMEMORY;
913 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
914 attrs, "objectClass=dnsNode");
915 if (ret == LDB_SUCCESS) {
916 break;
919 if (ret != LDB_SUCCESS) {
920 talloc_free(tmp_ctx);
921 return ISC_R_NOTFOUND;
924 for (i=0; i<res->count; i++) {
925 struct ldb_message_element *el;
926 TALLOC_CTX *el_ctx = talloc_new(tmp_ctx);
927 const char *rdn, *name;
928 const struct ldb_val *v;
929 WERROR werr;
930 struct dnsp_DnssrvRpcRecord *recs = NULL;
931 uint16_t num_recs = 0;
933 el = ldb_msg_find_element(res->msgs[i], "dnsRecord");
934 if (el == NULL || el->num_values == 0) {
935 state->log(ISC_LOG_INFO, "failed to find dnsRecord for %s",
936 ldb_dn_get_linearized(dn));
937 talloc_free(el_ctx);
938 continue;
941 v = ldb_dn_get_rdn_val(res->msgs[i]->dn);
942 if (v == NULL) {
943 state->log(ISC_LOG_INFO, "failed to find RDN for %s",
944 ldb_dn_get_linearized(dn));
945 talloc_free(el_ctx);
946 continue;
949 rdn = talloc_strndup(el_ctx, (char *)v->data, v->length);
950 if (rdn == NULL) {
951 talloc_free(tmp_ctx);
952 return ISC_R_NOMEMORY;
955 if (strcmp(rdn, "@") == 0) {
956 name = zone;
957 } else {
958 name = talloc_asprintf(el_ctx, "%s.%s", rdn, zone);
960 if (name == NULL) {
961 talloc_free(tmp_ctx);
962 return ISC_R_NOMEMORY;
965 werr = dns_common_extract(el, el_ctx, &recs, &num_recs);
966 if (!W_ERROR_IS_OK(werr)) {
967 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s, %s",
968 ldb_dn_get_linearized(dn), win_errstr(werr));
969 talloc_free(el_ctx);
970 continue;
973 for (j=0; j < num_recs; j++) {
974 isc_result_t result;
976 result = b9_putnamedrr(state, allnodes, name, &recs[j]);
977 if (result != ISC_R_SUCCESS) {
978 continue;
982 talloc_free(el_ctx);
985 talloc_free(tmp_ctx);
987 return ISC_R_SUCCESS;
992 start a transaction
994 _PUBLIC_ isc_result_t dlz_newversion(const char *zone, void *dbdata, void **versionp)
996 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
998 state->log(ISC_LOG_INFO, "samba_dlz: starting transaction on zone %s", zone);
1000 if (state->transaction_token != NULL) {
1001 state->log(ISC_LOG_INFO, "samba_dlz: transaction already started for zone %s", zone);
1002 return ISC_R_FAILURE;
1005 state->transaction_token = talloc_zero(state, int);
1006 if (state->transaction_token == NULL) {
1007 return ISC_R_NOMEMORY;
1010 if (ldb_transaction_start(state->samdb) != LDB_SUCCESS) {
1011 state->log(ISC_LOG_INFO, "samba_dlz: failed to start a transaction for zone %s", zone);
1012 talloc_free(state->transaction_token);
1013 state->transaction_token = NULL;
1014 return ISC_R_FAILURE;
1017 *versionp = (void *)state->transaction_token;
1019 return ISC_R_SUCCESS;
1023 end a transaction
1025 _PUBLIC_ void dlz_closeversion(const char *zone, isc_boolean_t commit,
1026 void *dbdata, void **versionp)
1028 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1030 if (state->transaction_token != (int *)*versionp) {
1031 state->log(ISC_LOG_INFO, "samba_dlz: transaction not started for zone %s", zone);
1032 return;
1035 if (commit) {
1036 if (ldb_transaction_commit(state->samdb) != LDB_SUCCESS) {
1037 state->log(ISC_LOG_INFO, "samba_dlz: failed to commit a transaction for zone %s", zone);
1038 return;
1040 state->log(ISC_LOG_INFO, "samba_dlz: committed transaction on zone %s", zone);
1041 } else {
1042 if (ldb_transaction_cancel(state->samdb) != LDB_SUCCESS) {
1043 state->log(ISC_LOG_INFO, "samba_dlz: failed to cancel a transaction for zone %s", zone);
1044 return;
1046 state->log(ISC_LOG_INFO, "samba_dlz: cancelling transaction on zone %s", zone);
1049 talloc_free(state->transaction_token);
1050 state->transaction_token = NULL;
1051 *versionp = NULL;
1056 see if there is a SOA record for a zone
1058 static bool b9_has_soa(struct dlz_bind9_data *state, struct ldb_dn *dn, const char *zone)
1060 TALLOC_CTX *tmp_ctx = talloc_new(state);
1061 WERROR werr;
1062 struct dnsp_DnssrvRpcRecord *records = NULL;
1063 uint16_t num_records = 0, i;
1065 if (!ldb_dn_add_child_fmt(dn, "DC=@,DC=%s", zone)) {
1066 talloc_free(tmp_ctx);
1067 return false;
1070 werr = dns_common_lookup(state->samdb, tmp_ctx, dn,
1071 &records, &num_records, NULL);
1072 if (!W_ERROR_IS_OK(werr)) {
1073 talloc_free(tmp_ctx);
1074 return false;
1077 for (i=0; i < num_records; i++) {
1078 if (records[i].wType == DNS_TYPE_SOA) {
1079 talloc_free(tmp_ctx);
1080 return true;
1084 talloc_free(tmp_ctx);
1085 return false;
1088 static bool b9_zone_add(struct dlz_bind9_data *state, const char *name)
1090 struct b9_zone *zone;
1092 zone = talloc_zero(state, struct b9_zone);
1093 if (zone == NULL) {
1094 return false;
1097 zone->name = talloc_strdup(zone, name);
1098 if (zone->name == NULL) {
1099 talloc_free(zone);
1100 return false;
1103 DLIST_ADD(state->zonelist, zone);
1104 return true;
1107 static bool b9_zone_exists(struct dlz_bind9_data *state, const char *name)
1109 struct b9_zone *zone = state->zonelist;
1110 bool found = false;
1112 while (zone != NULL) {
1113 if (strcasecmp(name, zone->name) == 0) {
1114 found = true;
1115 break;
1117 zone = zone->next;
1120 return found;
1125 configure a writeable zone
1127 #if DLZ_DLOPEN_VERSION < 3
1128 _PUBLIC_ isc_result_t dlz_configure(dns_view_t *view, void *dbdata)
1129 #else
1130 _PUBLIC_ isc_result_t dlz_configure(dns_view_t *view, dns_dlzdb_t *dlzdb,
1131 void *dbdata)
1132 #endif
1134 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1135 TALLOC_CTX *tmp_ctx;
1136 struct ldb_dn *dn;
1137 int i;
1139 state->log(ISC_LOG_INFO, "samba_dlz: starting configure");
1140 if (state->writeable_zone == NULL) {
1141 state->log(ISC_LOG_INFO, "samba_dlz: no writeable_zone method available");
1142 return ISC_R_FAILURE;
1145 tmp_ctx = talloc_new(state);
1147 for (i=0; zone_prefixes[i]; i++) {
1148 const char *attrs[] = { "name", NULL };
1149 int j, ret;
1150 struct ldb_result *res;
1152 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
1153 if (dn == NULL) {
1154 talloc_free(tmp_ctx);
1155 return ISC_R_NOMEMORY;
1158 if (!ldb_dn_add_child_fmt(dn, "%s", zone_prefixes[i])) {
1159 talloc_free(tmp_ctx);
1160 return ISC_R_NOMEMORY;
1163 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
1164 attrs, "objectClass=dnsZone");
1165 if (ret != LDB_SUCCESS) {
1166 continue;
1169 for (j=0; j<res->count; j++) {
1170 isc_result_t result;
1171 const char *zone = ldb_msg_find_attr_as_string(res->msgs[j], "name", NULL);
1172 struct ldb_dn *zone_dn;
1174 if (zone == NULL) {
1175 continue;
1177 /* Ignore zones that are not handled in BIND */
1178 if ((strcmp(zone, "RootDNSServers") == 0) ||
1179 (strcmp(zone, "..TrustAnchors") == 0)) {
1180 continue;
1182 zone_dn = ldb_dn_copy(tmp_ctx, dn);
1183 if (zone_dn == NULL) {
1184 talloc_free(tmp_ctx);
1185 return ISC_R_NOMEMORY;
1188 if (!b9_has_soa(state, zone_dn, zone)) {
1189 continue;
1192 if (b9_zone_exists(state, zone)) {
1193 state->log(ISC_LOG_WARNING, "samba_dlz: Ignoring duplicate zone '%s' from '%s'",
1194 zone, ldb_dn_get_linearized(zone_dn));
1195 continue;
1198 if (!b9_zone_add(state, zone)) {
1199 talloc_free(tmp_ctx);
1200 return ISC_R_NOMEMORY;
1203 #if DLZ_DLOPEN_VERSION < 3
1204 result = state->writeable_zone(view, zone);
1205 #else
1206 result = state->writeable_zone(view, dlzdb, zone);
1207 #endif
1208 if (result != ISC_R_SUCCESS) {
1209 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to configure zone '%s'",
1210 zone);
1211 talloc_free(tmp_ctx);
1212 return result;
1214 state->log(ISC_LOG_INFO, "samba_dlz: configured writeable zone '%s'", zone);
1218 talloc_free(tmp_ctx);
1219 return ISC_R_SUCCESS;
1223 authorize a zone update
1225 _PUBLIC_ isc_boolean_t dlz_ssumatch(const char *signer, const char *name, const char *tcpaddr,
1226 const char *type, const char *key, uint32_t keydatalen, uint8_t *keydata,
1227 void *dbdata)
1229 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1230 TALLOC_CTX *tmp_ctx;
1231 DATA_BLOB ap_req;
1232 struct cli_credentials *server_credentials;
1233 char *keytab_name;
1234 int ret;
1235 int ldb_ret;
1236 NTSTATUS nt_status;
1237 struct gensec_security *gensec_ctx;
1238 struct auth_session_info *session_info;
1239 struct ldb_dn *dn;
1240 isc_result_t result;
1241 struct ldb_result *res;
1242 const char * attrs[] = { NULL };
1243 uint32_t access_mask;
1245 /* Remove cached credentials, if any */
1246 if (state->session_info) {
1247 talloc_free(state->session_info);
1248 state->session_info = NULL;
1250 if (state->update_name) {
1251 talloc_free(state->update_name);
1252 state->update_name = NULL;
1255 tmp_ctx = talloc_new(NULL);
1256 if (tmp_ctx == NULL) {
1257 state->log(ISC_LOG_ERROR, "samba_dlz: no memory");
1258 return ISC_FALSE;
1261 ap_req = data_blob_const(keydata, keydatalen);
1262 server_credentials = cli_credentials_init(tmp_ctx);
1263 if (!server_credentials) {
1264 state->log(ISC_LOG_ERROR, "samba_dlz: failed to init server credentials");
1265 talloc_free(tmp_ctx);
1266 return ISC_FALSE;
1269 cli_credentials_set_krb5_context(server_credentials, state->smb_krb5_ctx);
1270 cli_credentials_set_conf(server_credentials, state->lp);
1272 keytab_name = talloc_asprintf(tmp_ctx, "file:%s/dns.keytab",
1273 lpcfg_private_dir(state->lp));
1274 ret = cli_credentials_set_keytab_name(server_credentials, state->lp, keytab_name,
1275 CRED_SPECIFIED);
1276 if (ret != 0) {
1277 state->log(ISC_LOG_ERROR, "samba_dlz: failed to obtain server credentials from %s",
1278 keytab_name);
1279 talloc_free(tmp_ctx);
1280 return ISC_FALSE;
1282 talloc_free(keytab_name);
1284 nt_status = gensec_server_start(tmp_ctx,
1285 lpcfg_gensec_settings(tmp_ctx, state->lp),
1286 state->auth_context, &gensec_ctx);
1287 if (!NT_STATUS_IS_OK(nt_status)) {
1288 state->log(ISC_LOG_ERROR, "samba_dlz: failed to start gensec server");
1289 talloc_free(tmp_ctx);
1290 return ISC_FALSE;
1293 gensec_set_credentials(gensec_ctx, server_credentials);
1295 nt_status = gensec_start_mech_by_name(gensec_ctx, "spnego");
1296 if (!NT_STATUS_IS_OK(nt_status)) {
1297 state->log(ISC_LOG_ERROR, "samba_dlz: failed to start spnego");
1298 talloc_free(tmp_ctx);
1299 return ISC_FALSE;
1302 nt_status = gensec_update_ev(gensec_ctx, tmp_ctx, state->ev_ctx, ap_req, &ap_req);
1303 if (!NT_STATUS_IS_OK(nt_status)) {
1304 state->log(ISC_LOG_ERROR, "samba_dlz: spnego update failed");
1305 talloc_free(tmp_ctx);
1306 return ISC_FALSE;
1309 nt_status = gensec_session_info(gensec_ctx, tmp_ctx, &session_info);
1310 if (!NT_STATUS_IS_OK(nt_status)) {
1311 state->log(ISC_LOG_ERROR, "samba_dlz: failed to create session info");
1312 talloc_free(tmp_ctx);
1313 return ISC_FALSE;
1316 /* Get the DN from name */
1317 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1318 if (result != ISC_R_SUCCESS) {
1319 state->log(ISC_LOG_ERROR, "samba_dlz: failed to find name %s", name);
1320 talloc_free(tmp_ctx);
1321 return ISC_FALSE;
1324 /* make sure the dn exists, or find parent dn in case new object is being added */
1325 ldb_ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
1326 attrs, "objectClass=dnsNode");
1327 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
1328 ldb_dn_remove_child_components(dn, 1);
1329 access_mask = SEC_ADS_CREATE_CHILD;
1330 talloc_free(res);
1331 } else if (ldb_ret == LDB_SUCCESS) {
1332 access_mask = SEC_STD_REQUIRED | SEC_ADS_SELF_WRITE;
1333 talloc_free(res);
1334 } else {
1335 talloc_free(tmp_ctx);
1336 return ISC_FALSE;
1339 /* Do ACL check */
1340 ldb_ret = dsdb_check_access_on_dn(state->samdb, tmp_ctx, dn,
1341 session_info->security_token,
1342 access_mask, NULL);
1343 if (ldb_ret != LDB_SUCCESS) {
1344 state->log(ISC_LOG_INFO,
1345 "samba_dlz: disallowing update of signer=%s name=%s type=%s error=%s",
1346 signer, name, type, ldb_strerror(ldb_ret));
1347 talloc_free(tmp_ctx);
1348 return ISC_FALSE;
1351 /* Cache session_info, so it can be used in the actual add/delete operation */
1352 state->update_name = talloc_strdup(state, name);
1353 if (state->update_name == NULL) {
1354 state->log(ISC_LOG_ERROR, "samba_dlz: memory allocation error");
1355 talloc_free(tmp_ctx);
1356 return ISC_FALSE;
1358 state->session_info = talloc_steal(state, session_info);
1360 state->log(ISC_LOG_INFO, "samba_dlz: allowing update of signer=%s name=%s tcpaddr=%s type=%s key=%s",
1361 signer, name, tcpaddr, type, key);
1363 talloc_free(tmp_ctx);
1364 return ISC_TRUE;
1368 see if two DNS names are the same
1370 static bool dns_name_equal(const char *name1, const char *name2)
1372 size_t len1 = strlen(name1);
1373 size_t len2 = strlen(name2);
1374 if (name1[len1-1] == '.') len1--;
1375 if (name2[len2-1] == '.') len2--;
1376 if (len1 != len2) {
1377 return false;
1379 return strncasecmp_m(name1, name2, len1) == 0;
1384 see if two dns records match
1386 static bool b9_record_match(struct dlz_bind9_data *state,
1387 struct dnsp_DnssrvRpcRecord *rec1, struct dnsp_DnssrvRpcRecord *rec2)
1389 bool status;
1390 int i;
1391 struct in6_addr rec1_in_addr6;
1392 struct in6_addr rec2_in_addr6;
1394 if (rec1->wType != rec2->wType) {
1395 return false;
1397 /* see if this type is single valued */
1398 if (b9_single_valued(rec1->wType)) {
1399 return true;
1402 /* see if the data matches */
1403 switch (rec1->wType) {
1404 case DNS_TYPE_A:
1405 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
1406 case DNS_TYPE_AAAA:
1407 inet_pton(AF_INET6, rec1->data.ipv6, &rec1_in_addr6);
1408 inet_pton(AF_INET6, rec2->data.ipv6, &rec2_in_addr6);
1409 return memcmp(&rec1_in_addr6, &rec2_in_addr6, sizeof(rec1_in_addr6)) == 0;
1410 case DNS_TYPE_CNAME:
1411 return dns_name_equal(rec1->data.cname, rec2->data.cname);
1412 case DNS_TYPE_TXT:
1413 status = (rec1->data.txt.count == rec2->data.txt.count);
1414 if (!status) return status;
1415 for (i=0; i<rec1->data.txt.count; i++) {
1416 status &= (strcmp(rec1->data.txt.str[i], rec2->data.txt.str[i]) == 0);
1418 return status;
1419 case DNS_TYPE_PTR:
1420 return dns_name_equal(rec1->data.ptr, rec2->data.ptr);
1421 case DNS_TYPE_NS:
1422 return dns_name_equal(rec1->data.ns, rec2->data.ns);
1424 case DNS_TYPE_SRV:
1425 return rec1->data.srv.wPriority == rec2->data.srv.wPriority &&
1426 rec1->data.srv.wWeight == rec2->data.srv.wWeight &&
1427 rec1->data.srv.wPort == rec2->data.srv.wPort &&
1428 dns_name_equal(rec1->data.srv.nameTarget, rec2->data.srv.nameTarget);
1430 case DNS_TYPE_MX:
1431 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
1432 dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
1434 case DNS_TYPE_HINFO:
1435 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
1436 strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
1438 case DNS_TYPE_SOA:
1439 return dns_name_equal(rec1->data.soa.mname, rec2->data.soa.mname) &&
1440 dns_name_equal(rec1->data.soa.rname, rec2->data.soa.rname) &&
1441 rec1->data.soa.serial == rec2->data.soa.serial &&
1442 rec1->data.soa.refresh == rec2->data.soa.refresh &&
1443 rec1->data.soa.retry == rec2->data.soa.retry &&
1444 rec1->data.soa.expire == rec2->data.soa.expire &&
1445 rec1->data.soa.minimum == rec2->data.soa.minimum;
1446 default:
1447 state->log(ISC_LOG_ERROR, "samba_dlz b9_record_match: unhandled record type %u",
1448 rec1->wType);
1449 break;
1452 return false;
1456 * Update session_info on samdb using the cached credentials
1458 static bool b9_set_session_info(struct dlz_bind9_data *state, const char *name)
1460 int ret;
1462 if (state->update_name == NULL || state->session_info == NULL) {
1463 state->log(ISC_LOG_ERROR, "samba_dlz: invalid credentials");
1464 return false;
1467 /* Do not use client credentials, if we're not updating the client specified name */
1468 if (strcmp(state->update_name, name) != 0) {
1469 return true;
1472 ret = ldb_set_opaque(state->samdb, "sessionInfo", state->session_info);
1473 if (ret != LDB_SUCCESS) {
1474 state->log(ISC_LOG_ERROR, "samba_dlz: unable to set session info");
1475 return false;
1478 return true;
1482 * Reset session_info on samdb as system session
1484 static void b9_reset_session_info(struct dlz_bind9_data *state)
1486 ldb_set_opaque(state->samdb, "sessionInfo", system_session(state->lp));
1490 add or modify a rdataset
1492 _PUBLIC_ isc_result_t dlz_addrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1494 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1495 struct dnsp_DnssrvRpcRecord *rec;
1496 struct ldb_dn *dn;
1497 isc_result_t result;
1498 bool tombstoned = false;
1499 bool needs_add = false;
1500 struct dnsp_DnssrvRpcRecord *recs = NULL;
1501 uint16_t num_recs = 0;
1502 uint16_t first = 0;
1503 uint16_t i;
1504 NTTIME t;
1505 WERROR werr;
1507 if (state->transaction_token != (void*)version) {
1508 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1509 return ISC_R_FAILURE;
1512 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1513 if (rec == NULL) {
1514 return ISC_R_NOMEMORY;
1517 unix_to_nt_time(&t, time(NULL));
1518 t /= 10*1000*1000; /* convert to seconds (NT time is in 100ns units) */
1519 t /= 3600; /* convert to hours */
1521 rec->rank = DNS_RANK_ZONE;
1522 rec->dwTimeStamp = (uint32_t)t;
1524 if (!b9_parse(state, rdatastr, rec)) {
1525 state->log(ISC_LOG_INFO, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1526 talloc_free(rec);
1527 return ISC_R_FAILURE;
1530 /* find the DN of the record */
1531 result = b9_find_name_dn(state, name, rec, &dn);
1532 if (result != ISC_R_SUCCESS) {
1533 talloc_free(rec);
1534 return result;
1537 /* get any existing records */
1538 werr = dns_common_lookup(state->samdb, rec, dn,
1539 &recs, &num_recs, &tombstoned);
1540 if (W_ERROR_EQUAL(werr, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
1541 needs_add = true;
1542 werr = WERR_OK;
1544 if (!W_ERROR_IS_OK(werr)) {
1545 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s, %s",
1546 ldb_dn_get_linearized(dn), win_errstr(werr));
1547 talloc_free(rec);
1548 return ISC_R_FAILURE;
1551 if (tombstoned) {
1553 * we need to keep the existing tombstone record
1554 * and ignore it
1556 first = num_recs;
1559 /* there are existing records. We need to see if this will
1560 * replace a record or add to it
1562 for (i=first; i < num_recs; i++) {
1563 if (b9_record_match(state, rec, &recs[i])) {
1564 break;
1567 if (i == UINT16_MAX) {
1568 state->log(ISC_LOG_ERROR, "samba_dlz: failed to already %u dnsRecord values for %s",
1569 i, ldb_dn_get_linearized(dn));
1570 talloc_free(rec);
1571 return ISC_R_FAILURE;
1574 if (i == num_recs) {
1575 /* adding a new value */
1576 recs = talloc_realloc(rec, recs,
1577 struct dnsp_DnssrvRpcRecord,
1578 num_recs + 1);
1579 if (recs == NULL) {
1580 talloc_free(rec);
1581 return ISC_R_NOMEMORY;
1583 num_recs++;
1586 recs[i] = *rec;
1588 if (!b9_set_session_info(state, name)) {
1589 talloc_free(rec);
1590 return ISC_R_FAILURE;
1593 /* modify the record */
1594 werr = dns_common_replace(state->samdb, rec, dn,
1595 needs_add,
1596 state->soa_serial,
1597 recs, num_recs);
1598 b9_reset_session_info(state);
1599 if (!W_ERROR_IS_OK(werr)) {
1600 state->log(ISC_LOG_ERROR, "samba_dlz: failed to %s %s - %s",
1601 needs_add ? "add" : "modify",
1602 ldb_dn_get_linearized(dn), win_errstr(werr));
1603 talloc_free(rec);
1604 return ISC_R_FAILURE;
1607 state->log(ISC_LOG_INFO, "samba_dlz: added rdataset %s '%s'", name, rdatastr);
1609 talloc_free(rec);
1610 return ISC_R_SUCCESS;
1614 remove a rdataset
1616 _PUBLIC_ isc_result_t dlz_subrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1618 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1619 struct dnsp_DnssrvRpcRecord *rec;
1620 struct ldb_dn *dn;
1621 isc_result_t result;
1622 struct dnsp_DnssrvRpcRecord *recs = NULL;
1623 uint16_t num_recs = 0;
1624 uint16_t i;
1625 WERROR werr;
1627 if (state->transaction_token != (void*)version) {
1628 state->log(ISC_LOG_ERROR, "samba_dlz: bad transaction version");
1629 return ISC_R_FAILURE;
1632 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1633 if (rec == NULL) {
1634 return ISC_R_NOMEMORY;
1637 if (!b9_parse(state, rdatastr, rec)) {
1638 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1639 talloc_free(rec);
1640 return ISC_R_FAILURE;
1643 /* find the DN of the record */
1644 result = b9_find_name_dn(state, name, rec, &dn);
1645 if (result != ISC_R_SUCCESS) {
1646 talloc_free(rec);
1647 return result;
1650 /* get the existing records */
1651 werr = dns_common_lookup(state->samdb, rec, dn,
1652 &recs, &num_recs, NULL);
1653 if (!W_ERROR_IS_OK(werr)) {
1654 talloc_free(rec);
1655 return ISC_R_NOTFOUND;
1658 for (i=0; i < num_recs; i++) {
1659 if (b9_record_match(state, rec, &recs[i])) {
1660 recs[i] = (struct dnsp_DnssrvRpcRecord) {
1661 .wType = DNS_TYPE_TOMBSTONE,
1663 break;
1666 if (i == num_recs) {
1667 talloc_free(rec);
1668 return ISC_R_NOTFOUND;
1671 if (!b9_set_session_info(state, name)) {
1672 talloc_free(rec);
1673 return ISC_R_FAILURE;
1676 /* modify the record */
1677 werr = dns_common_replace(state->samdb, rec, dn,
1678 false,/* needs_add */
1679 state->soa_serial,
1680 recs, num_recs);
1681 b9_reset_session_info(state);
1682 if (!W_ERROR_IS_OK(werr)) {
1683 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1684 ldb_dn_get_linearized(dn), win_errstr(werr));
1685 talloc_free(rec);
1686 return ISC_R_FAILURE;
1689 state->log(ISC_LOG_INFO, "samba_dlz: subtracted rdataset %s '%s'", name, rdatastr);
1691 talloc_free(rec);
1692 return ISC_R_SUCCESS;
1697 delete all records of the given type
1699 _PUBLIC_ isc_result_t dlz_delrdataset(const char *name, const char *type, void *dbdata, void *version)
1701 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1702 TALLOC_CTX *tmp_ctx;
1703 struct ldb_dn *dn;
1704 isc_result_t result;
1705 enum dns_record_type dns_type;
1706 bool found = false;
1707 struct dnsp_DnssrvRpcRecord *recs = NULL;
1708 uint16_t num_recs = 0;
1709 uint16_t ri = 0;
1710 WERROR werr;
1712 if (state->transaction_token != (void*)version) {
1713 state->log(ISC_LOG_ERROR, "samba_dlz: bad transaction version");
1714 return ISC_R_FAILURE;
1717 if (!b9_dns_type(type, &dns_type)) {
1718 state->log(ISC_LOG_ERROR, "samba_dlz: bad dns type %s in delete", type);
1719 return ISC_R_FAILURE;
1722 tmp_ctx = talloc_new(state);
1724 /* find the DN of the record */
1725 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1726 if (result != ISC_R_SUCCESS) {
1727 talloc_free(tmp_ctx);
1728 return result;
1731 /* get the existing records */
1732 werr = dns_common_lookup(state->samdb, tmp_ctx, dn,
1733 &recs, &num_recs, NULL);
1734 if (!W_ERROR_IS_OK(werr)) {
1735 talloc_free(tmp_ctx);
1736 return ISC_R_NOTFOUND;
1739 for (ri=0; ri < num_recs; ri++) {
1740 if (dns_type != recs[ri].wType) {
1741 continue;
1744 found = true;
1745 recs[ri] = (struct dnsp_DnssrvRpcRecord) {
1746 .wType = DNS_TYPE_TOMBSTONE,
1750 if (!found) {
1751 talloc_free(tmp_ctx);
1752 return ISC_R_FAILURE;
1755 if (!b9_set_session_info(state, name)) {
1756 talloc_free(tmp_ctx);
1757 return ISC_R_FAILURE;
1760 /* modify the record */
1761 werr = dns_common_replace(state->samdb, tmp_ctx, dn,
1762 false,/* needs_add */
1763 state->soa_serial,
1764 recs, num_recs);
1765 b9_reset_session_info(state);
1766 if (!W_ERROR_IS_OK(werr)) {
1767 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1768 ldb_dn_get_linearized(dn), win_errstr(werr));
1769 talloc_free(tmp_ctx);
1770 return ISC_R_FAILURE;
1773 state->log(ISC_LOG_INFO, "samba_dlz: deleted rdataset %s of type %s", name, type);
1775 talloc_free(tmp_ctx);
1776 return ISC_R_SUCCESS;