rpc_server: Fix CID 1311339 Error handling issues (CHECKED_RETURN)
[Samba.git] / source4 / dns_server / dlz_bind9.c
blob7a76fe57962d88fd2924e59a4f461c2a58f5e84d
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 <popt.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 * Add a trailing '.' if it's missing
115 static const char *b9_format_fqdn(TALLOC_CTX *mem_ctx, const char *str)
117 size_t len;
118 const char *tmp;
120 if (str == NULL || str[0] == '\0') {
121 return str;
124 len = strlen(str);
125 if (str[len-1] != '.') {
126 tmp = talloc_asprintf(mem_ctx, "%s.", str);
127 } else {
128 tmp = str;
130 return tmp;
134 format a record for bind9
136 static bool b9_format(struct dlz_bind9_data *state,
137 TALLOC_CTX *mem_ctx,
138 struct dnsp_DnssrvRpcRecord *rec,
139 const char **type, const char **data)
141 uint32_t i;
142 char *tmp;
143 const char *fqdn;
145 switch (rec->wType) {
146 case DNS_TYPE_A:
147 *type = "a";
148 *data = rec->data.ipv4;
149 break;
151 case DNS_TYPE_AAAA:
152 *type = "aaaa";
153 *data = rec->data.ipv6;
154 break;
156 case DNS_TYPE_CNAME:
157 *type = "cname";
158 *data = b9_format_fqdn(mem_ctx, rec->data.cname);
159 break;
161 case DNS_TYPE_TXT:
162 *type = "txt";
163 tmp = talloc_asprintf(mem_ctx, "\"%s\"", rec->data.txt.str[0]);
164 for (i=1; i<rec->data.txt.count; i++) {
165 tmp = talloc_asprintf_append(tmp, " \"%s\"", rec->data.txt.str[i]);
167 *data = tmp;
168 break;
170 case DNS_TYPE_PTR:
171 *type = "ptr";
172 *data = b9_format_fqdn(mem_ctx, rec->data.ptr);
173 break;
175 case DNS_TYPE_SRV:
176 *type = "srv";
177 fqdn = b9_format_fqdn(mem_ctx, rec->data.srv.nameTarget);
178 if (fqdn == NULL) {
179 return false;
181 *data = talloc_asprintf(mem_ctx, "%u %u %u %s",
182 rec->data.srv.wPriority,
183 rec->data.srv.wWeight,
184 rec->data.srv.wPort,
185 fqdn);
186 break;
188 case DNS_TYPE_MX:
189 *type = "mx";
190 fqdn = b9_format_fqdn(mem_ctx, rec->data.mx.nameTarget);
191 if (fqdn == NULL) {
192 return false;
194 *data = talloc_asprintf(mem_ctx, "%u %s",
195 rec->data.mx.wPriority, fqdn);
196 break;
198 case DNS_TYPE_HINFO:
199 *type = "hinfo";
200 *data = talloc_asprintf(mem_ctx, "%s %s",
201 rec->data.hinfo.cpu,
202 rec->data.hinfo.os);
203 break;
205 case DNS_TYPE_NS:
206 *type = "ns";
207 *data = b9_format_fqdn(mem_ctx, rec->data.ns);
208 break;
210 case DNS_TYPE_SOA: {
211 const char *mname;
212 *type = "soa";
214 /* we need to fake the authoritative nameserver to
215 * point at ourselves. This is how AD DNS servers
216 * force clients to send updates to the right local DC
218 mname = talloc_asprintf(mem_ctx, "%s.%s.",
219 lpcfg_netbios_name(state->lp),
220 lpcfg_dnsdomain(state->lp));
221 if (mname == NULL) {
222 return false;
224 mname = strlower_talloc(mem_ctx, mname);
225 if (mname == NULL) {
226 return false;
229 fqdn = b9_format_fqdn(mem_ctx, rec->data.soa.rname);
230 if (fqdn == NULL) {
231 return false;
234 state->soa_serial = rec->data.soa.serial;
236 *data = talloc_asprintf(mem_ctx, "%s %s %u %u %u %u %u",
237 mname, fqdn,
238 rec->data.soa.serial,
239 rec->data.soa.refresh,
240 rec->data.soa.retry,
241 rec->data.soa.expire,
242 rec->data.soa.minimum);
243 break;
246 default:
247 state->log(ISC_LOG_ERROR, "samba_dlz b9_format: unhandled record type %u",
248 rec->wType);
249 return false;
252 return true;
255 static const struct {
256 enum dns_record_type dns_type;
257 const char *typestr;
258 bool single_valued;
259 } dns_typemap[] = {
260 { DNS_TYPE_A, "A" , false},
261 { DNS_TYPE_AAAA, "AAAA" , false},
262 { DNS_TYPE_CNAME, "CNAME" , true},
263 { DNS_TYPE_TXT, "TXT" , false},
264 { DNS_TYPE_PTR, "PTR" , false},
265 { DNS_TYPE_SRV, "SRV" , false},
266 { DNS_TYPE_MX, "MX" , false},
267 { DNS_TYPE_HINFO, "HINFO" , false},
268 { DNS_TYPE_NS, "NS" , false},
269 { DNS_TYPE_SOA, "SOA" , true},
274 see if a DNS type is single valued
276 static bool b9_single_valued(enum dns_record_type dns_type)
278 int i;
279 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
280 if (dns_typemap[i].dns_type == dns_type) {
281 return dns_typemap[i].single_valued;
284 return false;
288 see if a DNS type is single valued
290 static bool b9_dns_type(const char *type, enum dns_record_type *dtype)
292 int i;
293 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
294 if (strcasecmp(dns_typemap[i].typestr, type) == 0) {
295 *dtype = dns_typemap[i].dns_type;
296 return true;
299 return false;
303 #define DNS_PARSE_STR(ret, str, sep, saveptr) do { \
304 (ret) = strtok_r(str, sep, &saveptr); \
305 if ((ret) == NULL) return false; \
306 } while (0)
308 #define DNS_PARSE_UINT(ret, str, sep, saveptr) do { \
309 char *istr = strtok_r(str, sep, &saveptr); \
310 if ((istr) == NULL) return false; \
311 (ret) = strtoul(istr, NULL, 10); \
312 } while (0)
315 parse a record from bind9
317 static bool b9_parse(struct dlz_bind9_data *state,
318 const char *rdatastr,
319 struct dnsp_DnssrvRpcRecord *rec)
321 char *full_name, *dclass, *type;
322 char *str, *tmp, *saveptr=NULL;
323 int i;
325 str = talloc_strdup(rec, rdatastr);
326 if (str == NULL) {
327 return false;
330 /* parse the SDLZ string form */
331 DNS_PARSE_STR(full_name, str, "\t", saveptr);
332 DNS_PARSE_UINT(rec->dwTtlSeconds, NULL, "\t", saveptr);
333 DNS_PARSE_STR(dclass, NULL, "\t", saveptr);
334 DNS_PARSE_STR(type, NULL, "\t", saveptr);
336 /* construct the record */
337 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
338 if (strcasecmp(type, dns_typemap[i].typestr) == 0) {
339 rec->wType = dns_typemap[i].dns_type;
340 break;
343 if (i == ARRAY_SIZE(dns_typemap)) {
344 state->log(ISC_LOG_ERROR, "samba_dlz: unsupported record type '%s' for '%s'",
345 type, full_name);
346 return false;
349 switch (rec->wType) {
350 case DNS_TYPE_A:
351 DNS_PARSE_STR(rec->data.ipv4, NULL, " ", saveptr);
352 break;
354 case DNS_TYPE_AAAA:
355 DNS_PARSE_STR(rec->data.ipv6, NULL, " ", saveptr);
356 break;
358 case DNS_TYPE_CNAME:
359 DNS_PARSE_STR(rec->data.cname, NULL, " ", saveptr);
360 break;
362 case DNS_TYPE_TXT:
363 rec->data.txt.count = 0;
364 rec->data.txt.str = talloc_array(rec, const char *, rec->data.txt.count);
365 tmp = strtok_r(NULL, "\t", &saveptr);
366 while (tmp) {
367 rec->data.txt.str = talloc_realloc(rec, rec->data.txt.str, const char *,
368 rec->data.txt.count+1);
369 if (tmp[0] == '"') {
370 /* Strip quotes */
371 rec->data.txt.str[rec->data.txt.count] = talloc_strndup(rec, &tmp[1], strlen(tmp)-2);
372 } else {
373 rec->data.txt.str[rec->data.txt.count] = talloc_strdup(rec, tmp);
375 rec->data.txt.count++;
376 tmp = strtok_r(NULL, " ", &saveptr);
378 break;
380 case DNS_TYPE_PTR:
381 DNS_PARSE_STR(rec->data.ptr, NULL, " ", saveptr);
382 break;
384 case DNS_TYPE_SRV:
385 DNS_PARSE_UINT(rec->data.srv.wPriority, NULL, " ", saveptr);
386 DNS_PARSE_UINT(rec->data.srv.wWeight, NULL, " ", saveptr);
387 DNS_PARSE_UINT(rec->data.srv.wPort, NULL, " ", saveptr);
388 DNS_PARSE_STR(rec->data.srv.nameTarget, NULL, " ", saveptr);
389 break;
391 case DNS_TYPE_MX:
392 DNS_PARSE_UINT(rec->data.mx.wPriority, NULL, " ", saveptr);
393 DNS_PARSE_STR(rec->data.mx.nameTarget, NULL, " ", saveptr);
394 break;
396 case DNS_TYPE_HINFO:
397 DNS_PARSE_STR(rec->data.hinfo.cpu, NULL, " ", saveptr);
398 DNS_PARSE_STR(rec->data.hinfo.os, NULL, " ", saveptr);
399 break;
401 case DNS_TYPE_NS:
402 DNS_PARSE_STR(rec->data.ns, NULL, " ", saveptr);
403 break;
405 case DNS_TYPE_SOA:
406 DNS_PARSE_STR(rec->data.soa.mname, NULL, " ", saveptr);
407 DNS_PARSE_STR(rec->data.soa.rname, NULL, " ", saveptr);
408 DNS_PARSE_UINT(rec->data.soa.serial, NULL, " ", saveptr);
409 DNS_PARSE_UINT(rec->data.soa.refresh, NULL, " ", saveptr);
410 DNS_PARSE_UINT(rec->data.soa.retry, NULL, " ", saveptr);
411 DNS_PARSE_UINT(rec->data.soa.expire, NULL, " ", saveptr);
412 DNS_PARSE_UINT(rec->data.soa.minimum, NULL, " ", saveptr);
413 break;
415 default:
416 state->log(ISC_LOG_ERROR, "samba_dlz b9_parse: unhandled record type %u",
417 rec->wType);
418 return false;
421 /* we should be at the end of the buffer now */
422 if (strtok_r(NULL, "\t ", &saveptr) != NULL) {
423 state->log(ISC_LOG_ERROR, "samba_dlz b9_parse: unexpected data at end of string for '%s'",
424 rdatastr);
425 return false;
428 return true;
432 send a resource record to bind9
434 static isc_result_t b9_putrr(struct dlz_bind9_data *state,
435 void *handle, struct dnsp_DnssrvRpcRecord *rec,
436 const char **types)
438 isc_result_t result;
439 const char *type, *data;
440 TALLOC_CTX *tmp_ctx = talloc_new(state);
442 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
443 return ISC_R_FAILURE;
446 if (data == NULL) {
447 talloc_free(tmp_ctx);
448 return ISC_R_NOMEMORY;
451 if (types) {
452 int i;
453 for (i=0; types[i]; i++) {
454 if (strcmp(types[i], type) == 0) break;
456 if (types[i] == NULL) {
457 /* skip it */
458 return ISC_R_SUCCESS;
462 result = state->putrr(handle, type, rec->dwTtlSeconds, data);
463 if (result != ISC_R_SUCCESS) {
464 state->log(ISC_LOG_ERROR, "Failed to put rr");
466 talloc_free(tmp_ctx);
467 return result;
472 send a named resource record to bind9
474 static isc_result_t b9_putnamedrr(struct dlz_bind9_data *state,
475 void *handle, const char *name,
476 struct dnsp_DnssrvRpcRecord *rec)
478 isc_result_t result;
479 const char *type, *data;
480 TALLOC_CTX *tmp_ctx = talloc_new(state);
482 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
483 return ISC_R_FAILURE;
486 if (data == NULL) {
487 talloc_free(tmp_ctx);
488 return ISC_R_NOMEMORY;
491 result = state->putnamedrr(handle, name, type, rec->dwTtlSeconds, data);
492 if (result != ISC_R_SUCCESS) {
493 state->log(ISC_LOG_ERROR, "Failed to put named rr '%s'", name);
495 talloc_free(tmp_ctx);
496 return result;
500 parse options
502 static isc_result_t parse_options(struct dlz_bind9_data *state,
503 unsigned int argc, const char **argv,
504 struct b9_options *options)
506 int opt;
507 poptContext pc;
508 struct poptOption long_options[] = {
509 { "url", 'H', POPT_ARG_STRING, &options->url, 0, "database URL", "URL" },
510 { "debug", 'd', POPT_ARG_STRING, &options->debug, 0, "debug level", "DEBUG" },
511 { NULL }
514 pc = poptGetContext("dlz_bind9", argc, argv, long_options,
515 POPT_CONTEXT_KEEP_FIRST);
516 while ((opt = poptGetNextOpt(pc)) != -1) {
517 switch (opt) {
518 default:
519 state->log(ISC_LOG_ERROR, "dlz_bind9: Invalid option %s: %s",
520 poptBadOption(pc, 0), poptStrerror(opt));
521 return ISC_R_FAILURE;
525 return ISC_R_SUCCESS;
530 * Create session info from PAC
531 * This is called as auth_context->generate_session_info_pac()
533 static NTSTATUS b9_generate_session_info_pac(struct auth4_context *auth_context,
534 TALLOC_CTX *mem_ctx,
535 struct smb_krb5_context *smb_krb5_context,
536 DATA_BLOB *pac_blob,
537 const char *principal_name,
538 const struct tsocket_address *remote_addr,
539 uint32_t session_info_flags,
540 struct auth_session_info **session_info)
542 NTSTATUS status;
543 struct auth_user_info_dc *user_info_dc;
544 TALLOC_CTX *tmp_ctx;
546 tmp_ctx = talloc_new(mem_ctx);
547 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
549 status = kerberos_pac_blob_to_user_info_dc(tmp_ctx,
550 *pac_blob,
551 smb_krb5_context->krb5_context,
552 &user_info_dc,
553 NULL,
554 NULL);
555 if (!NT_STATUS_IS_OK(status)) {
556 talloc_free(tmp_ctx);
557 return status;
560 if (user_info_dc->info->authenticated) {
561 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
564 session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
566 status = auth_generate_session_info(mem_ctx, NULL, NULL, user_info_dc,
567 session_info_flags, session_info);
568 if (!NT_STATUS_IS_OK(status)) {
569 talloc_free(tmp_ctx);
570 return status;
573 talloc_free(tmp_ctx);
574 return status;
577 /* Callback for the DEBUG() system, to catch the remaining messages */
578 static void b9_debug(void *private_ptr, int msg_level, const char *msg)
580 static const int isc_log_map[] = {
581 ISC_LOG_CRITICAL, /* 0 */
582 ISC_LOG_ERROR, /* 1 */
583 ISC_LOG_WARNING, /* 2 */
584 ISC_LOG_NOTICE /* 3 */
586 struct dlz_bind9_data *state = private_ptr;
587 int isc_log_level;
589 if (msg_level >= ARRAY_SIZE(isc_log_map) || msg_level < 0) {
590 isc_log_level = ISC_LOG_INFO;
591 } else {
592 isc_log_level = isc_log_map[msg_level];
594 state->log(isc_log_level, "samba_dlz: %s", msg);
597 static int dlz_state_debug_unregister(struct dlz_bind9_data *state)
599 /* Stop logging (to the bind9 logs) */
600 debug_set_callback(NULL, NULL);
601 return 0;
605 called to initialise the driver
607 _PUBLIC_ isc_result_t dlz_create(const char *dlzname,
608 unsigned int argc, const char **argv,
609 void **dbdata, ...)
611 struct dlz_bind9_data *state;
612 const char *helper_name;
613 va_list ap;
614 isc_result_t result;
615 struct ldb_dn *dn;
616 NTSTATUS nt_status;
618 if (dlz_bind9_state != NULL) {
619 *dbdata = dlz_bind9_state;
620 dlz_bind9_state_ref_count++;
621 return ISC_R_SUCCESS;
624 state = talloc_zero(NULL, struct dlz_bind9_data);
625 if (state == NULL) {
626 return ISC_R_NOMEMORY;
629 talloc_set_destructor(state, dlz_state_debug_unregister);
631 /* fill in the helper functions */
632 va_start(ap, dbdata);
633 while ((helper_name = va_arg(ap, const char *)) != NULL) {
634 b9_add_helper(state, helper_name, va_arg(ap, void*));
636 va_end(ap);
638 /* Do not install samba signal handlers */
639 fault_setup_disable();
641 /* Start logging (to the bind9 logs) */
642 debug_set_callback(state, b9_debug);
644 state->ev_ctx = s4_event_context_init(state);
645 if (state->ev_ctx == NULL) {
646 result = ISC_R_NOMEMORY;
647 goto failed;
650 result = parse_options(state, argc, argv, &state->options);
651 if (result != ISC_R_SUCCESS) {
652 goto failed;
655 state->lp = loadparm_init_global(true);
656 if (state->lp == NULL) {
657 result = ISC_R_NOMEMORY;
658 goto failed;
661 if (state->options.debug) {
662 lpcfg_do_global_parameter(state->lp, "log level", state->options.debug);
663 } else {
664 lpcfg_do_global_parameter(state->lp, "log level", "0");
667 if (smb_krb5_init_context(state, state->lp, &state->smb_krb5_ctx) != 0) {
668 result = ISC_R_NOMEMORY;
669 goto failed;
672 nt_status = gensec_init();
673 if (!NT_STATUS_IS_OK(nt_status)) {
674 result = ISC_R_NOMEMORY;
675 goto failed;
678 state->auth_context = talloc_zero(state, struct auth4_context);
679 if (state->auth_context == NULL) {
680 result = ISC_R_NOMEMORY;
681 goto failed;
684 if (state->options.url == NULL) {
685 state->options.url = lpcfg_private_path(state, state->lp, "dns/sam.ldb");
686 if (state->options.url == NULL) {
687 result = ISC_R_NOMEMORY;
688 goto failed;
692 state->samdb = samdb_connect_url(state, state->ev_ctx, state->lp,
693 system_session(state->lp), 0, state->options.url);
694 if (state->samdb == NULL) {
695 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to connect to %s",
696 state->options.url);
697 result = ISC_R_FAILURE;
698 goto failed;
701 dn = ldb_get_default_basedn(state->samdb);
702 if (dn == NULL) {
703 state->log(ISC_LOG_ERROR, "samba_dlz: Unable to get basedn for %s - %s",
704 state->options.url, ldb_errstring(state->samdb));
705 result = ISC_R_FAILURE;
706 goto failed;
709 state->log(ISC_LOG_INFO, "samba_dlz: started for DN %s",
710 ldb_dn_get_linearized(dn));
712 state->auth_context->event_ctx = state->ev_ctx;
713 state->auth_context->lp_ctx = state->lp;
714 state->auth_context->sam_ctx = state->samdb;
715 state->auth_context->generate_session_info_pac = b9_generate_session_info_pac;
717 *dbdata = state;
718 dlz_bind9_state = state;
719 dlz_bind9_state_ref_count++;
721 return ISC_R_SUCCESS;
723 failed:
724 talloc_free(state);
725 return result;
729 shutdown the backend
731 _PUBLIC_ void dlz_destroy(void *dbdata)
733 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
734 state->log(ISC_LOG_INFO, "samba_dlz: shutting down");
736 dlz_bind9_state_ref_count--;
737 if (dlz_bind9_state_ref_count == 0) {
738 talloc_unlink(state, state->samdb);
739 talloc_free(state);
740 dlz_bind9_state = NULL;
746 return the base DN for a zone
748 static isc_result_t b9_find_zone_dn(struct dlz_bind9_data *state, const char *zone_name,
749 TALLOC_CTX *mem_ctx, struct ldb_dn **zone_dn)
751 int ret;
752 TALLOC_CTX *tmp_ctx = talloc_new(state);
753 const char *attrs[] = { NULL };
754 int i;
756 for (i=0; zone_prefixes[i]; i++) {
757 struct ldb_dn *dn;
758 struct ldb_result *res;
760 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
761 if (dn == NULL) {
762 talloc_free(tmp_ctx);
763 return ISC_R_NOMEMORY;
766 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone_name, zone_prefixes[i])) {
767 talloc_free(tmp_ctx);
768 return ISC_R_NOMEMORY;
771 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsZone");
772 if (ret == LDB_SUCCESS) {
773 if (zone_dn != NULL) {
774 *zone_dn = talloc_steal(mem_ctx, dn);
776 talloc_free(tmp_ctx);
777 return ISC_R_SUCCESS;
779 talloc_free(dn);
782 talloc_free(tmp_ctx);
783 return ISC_R_NOTFOUND;
788 return the DN for a name. The record does not need to exist, but the
789 zone must exist
791 static isc_result_t b9_find_name_dn(struct dlz_bind9_data *state, const char *name,
792 TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
794 const char *p;
796 /* work through the name piece by piece, until we find a zone */
797 for (p=name; p; ) {
798 isc_result_t result;
799 result = b9_find_zone_dn(state, p, mem_ctx, dn);
800 if (result == ISC_R_SUCCESS) {
801 /* we found a zone, now extend the DN to get
802 * the full DN
804 bool ret;
805 if (p == name) {
806 ret = ldb_dn_add_child_fmt(*dn, "DC=@");
807 } else {
808 ret = ldb_dn_add_child_fmt(*dn, "DC=%.*s", (int)(p-name)-1, name);
810 if (!ret) {
811 talloc_free(*dn);
812 return ISC_R_NOMEMORY;
814 return ISC_R_SUCCESS;
816 p = strchr(p, '.');
817 if (p == NULL) {
818 break;
820 p++;
822 return ISC_R_NOTFOUND;
827 see if we handle a given zone
829 #if DLZ_DLOPEN_VERSION < 3
830 _PUBLIC_ isc_result_t dlz_findzonedb(void *dbdata, const char *name)
831 #else
832 _PUBLIC_ isc_result_t dlz_findzonedb(void *dbdata, const char *name,
833 dns_clientinfomethods_t *methods,
834 dns_clientinfo_t *clientinfo)
835 #endif
837 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
838 return b9_find_zone_dn(state, name, NULL, NULL);
843 lookup one record
845 static isc_result_t dlz_lookup_types(struct dlz_bind9_data *state,
846 const char *zone, const char *name,
847 dns_sdlzlookup_t *lookup,
848 const char **types)
850 TALLOC_CTX *tmp_ctx = talloc_new(state);
851 struct ldb_dn *dn;
852 WERROR werr = WERR_DNS_ERROR_NAME_DOES_NOT_EXIST;
853 struct dnsp_DnssrvRpcRecord *records = NULL;
854 uint16_t num_records = 0, i;
856 for (i=0; zone_prefixes[i]; i++) {
857 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
858 if (dn == NULL) {
859 talloc_free(tmp_ctx);
860 return ISC_R_NOMEMORY;
863 if (!ldb_dn_add_child_fmt(dn, "DC=%s,DC=%s,%s", name, zone, zone_prefixes[i])) {
864 talloc_free(tmp_ctx);
865 return ISC_R_NOMEMORY;
868 werr = dns_common_lookup(state->samdb, tmp_ctx, dn,
869 &records, &num_records, NULL);
870 if (W_ERROR_IS_OK(werr)) {
871 break;
874 if (!W_ERROR_IS_OK(werr)) {
875 talloc_free(tmp_ctx);
876 return ISC_R_NOTFOUND;
879 for (i=0; i < num_records; i++) {
880 isc_result_t result;
882 result = b9_putrr(state, lookup, &records[i], types);
883 if (result != ISC_R_SUCCESS) {
884 talloc_free(tmp_ctx);
885 return result;
889 talloc_free(tmp_ctx);
890 return ISC_R_SUCCESS;
894 lookup one record
896 #if DLZ_DLOPEN_VERSION == 1
897 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name,
898 void *dbdata, dns_sdlzlookup_t *lookup)
899 #else
900 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name,
901 void *dbdata, dns_sdlzlookup_t *lookup,
902 dns_clientinfomethods_t *methods,
903 dns_clientinfo_t *clientinfo)
904 #endif
906 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
907 return dlz_lookup_types(state, zone, name, lookup, NULL);
912 see if a zone transfer is allowed
914 _PUBLIC_ isc_result_t dlz_allowzonexfr(void *dbdata, const char *name, const char *client)
916 /* just say yes for all our zones for now */
917 struct dlz_bind9_data *state = talloc_get_type(
918 dbdata, struct dlz_bind9_data);
919 return b9_find_zone_dn(state, name, NULL, NULL);
923 perform a zone transfer
925 _PUBLIC_ isc_result_t dlz_allnodes(const char *zone, void *dbdata,
926 dns_sdlzallnodes_t *allnodes)
928 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
929 const char *attrs[] = { "dnsRecord", NULL };
930 int ret = LDB_SUCCESS, i, j;
931 struct ldb_dn *dn;
932 struct ldb_result *res;
933 TALLOC_CTX *tmp_ctx = talloc_new(state);
935 for (i=0; zone_prefixes[i]; i++) {
936 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
937 if (dn == NULL) {
938 talloc_free(tmp_ctx);
939 return ISC_R_NOMEMORY;
942 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone, zone_prefixes[i])) {
943 talloc_free(tmp_ctx);
944 return ISC_R_NOMEMORY;
947 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
948 attrs, "objectClass=dnsNode");
949 if (ret == LDB_SUCCESS) {
950 break;
953 if (ret != LDB_SUCCESS) {
954 talloc_free(tmp_ctx);
955 return ISC_R_NOTFOUND;
958 for (i=0; i<res->count; i++) {
959 struct ldb_message_element *el;
960 TALLOC_CTX *el_ctx = talloc_new(tmp_ctx);
961 const char *rdn, *name;
962 const struct ldb_val *v;
963 WERROR werr;
964 struct dnsp_DnssrvRpcRecord *recs = NULL;
965 uint16_t num_recs = 0;
967 el = ldb_msg_find_element(res->msgs[i], "dnsRecord");
968 if (el == NULL || el->num_values == 0) {
969 state->log(ISC_LOG_INFO, "failed to find dnsRecord for %s",
970 ldb_dn_get_linearized(dn));
971 talloc_free(el_ctx);
972 continue;
975 v = ldb_dn_get_rdn_val(res->msgs[i]->dn);
976 if (v == NULL) {
977 state->log(ISC_LOG_INFO, "failed to find RDN for %s",
978 ldb_dn_get_linearized(dn));
979 talloc_free(el_ctx);
980 continue;
983 rdn = talloc_strndup(el_ctx, (char *)v->data, v->length);
984 if (rdn == NULL) {
985 talloc_free(tmp_ctx);
986 return ISC_R_NOMEMORY;
989 if (strcmp(rdn, "@") == 0) {
990 name = zone;
991 } else {
992 name = talloc_asprintf(el_ctx, "%s.%s", rdn, zone);
994 name = b9_format_fqdn(el_ctx, name);
995 if (name == NULL) {
996 talloc_free(tmp_ctx);
997 return ISC_R_NOMEMORY;
1000 werr = dns_common_extract(el, el_ctx, &recs, &num_recs);
1001 if (!W_ERROR_IS_OK(werr)) {
1002 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s, %s",
1003 ldb_dn_get_linearized(dn), win_errstr(werr));
1004 talloc_free(el_ctx);
1005 continue;
1008 for (j=0; j < num_recs; j++) {
1009 isc_result_t result;
1011 result = b9_putnamedrr(state, allnodes, name, &recs[j]);
1012 if (result != ISC_R_SUCCESS) {
1013 continue;
1017 talloc_free(el_ctx);
1020 talloc_free(tmp_ctx);
1022 return ISC_R_SUCCESS;
1027 start a transaction
1029 _PUBLIC_ isc_result_t dlz_newversion(const char *zone, void *dbdata, void **versionp)
1031 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1033 state->log(ISC_LOG_INFO, "samba_dlz: starting transaction on zone %s", zone);
1035 if (state->transaction_token != NULL) {
1036 state->log(ISC_LOG_INFO, "samba_dlz: transaction already started for zone %s", zone);
1037 return ISC_R_FAILURE;
1040 state->transaction_token = talloc_zero(state, int);
1041 if (state->transaction_token == NULL) {
1042 return ISC_R_NOMEMORY;
1045 if (ldb_transaction_start(state->samdb) != LDB_SUCCESS) {
1046 state->log(ISC_LOG_INFO, "samba_dlz: failed to start a transaction for zone %s", zone);
1047 talloc_free(state->transaction_token);
1048 state->transaction_token = NULL;
1049 return ISC_R_FAILURE;
1052 *versionp = (void *)state->transaction_token;
1054 return ISC_R_SUCCESS;
1058 end a transaction
1060 _PUBLIC_ void dlz_closeversion(const char *zone, isc_boolean_t commit,
1061 void *dbdata, void **versionp)
1063 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1065 if (state->transaction_token != (int *)*versionp) {
1066 state->log(ISC_LOG_INFO, "samba_dlz: transaction not started for zone %s", zone);
1067 return;
1070 if (commit) {
1071 if (ldb_transaction_commit(state->samdb) != LDB_SUCCESS) {
1072 state->log(ISC_LOG_INFO, "samba_dlz: failed to commit a transaction for zone %s", zone);
1073 return;
1075 state->log(ISC_LOG_INFO, "samba_dlz: committed transaction on zone %s", zone);
1076 } else {
1077 if (ldb_transaction_cancel(state->samdb) != LDB_SUCCESS) {
1078 state->log(ISC_LOG_INFO, "samba_dlz: failed to cancel a transaction for zone %s", zone);
1079 return;
1081 state->log(ISC_LOG_INFO, "samba_dlz: cancelling transaction on zone %s", zone);
1084 talloc_free(state->transaction_token);
1085 state->transaction_token = NULL;
1086 *versionp = NULL;
1091 see if there is a SOA record for a zone
1093 static bool b9_has_soa(struct dlz_bind9_data *state, struct ldb_dn *dn, const char *zone)
1095 TALLOC_CTX *tmp_ctx = talloc_new(state);
1096 WERROR werr;
1097 struct dnsp_DnssrvRpcRecord *records = NULL;
1098 uint16_t num_records = 0, i;
1100 if (!ldb_dn_add_child_fmt(dn, "DC=@,DC=%s", zone)) {
1101 talloc_free(tmp_ctx);
1102 return false;
1105 werr = dns_common_lookup(state->samdb, tmp_ctx, dn,
1106 &records, &num_records, NULL);
1107 if (!W_ERROR_IS_OK(werr)) {
1108 talloc_free(tmp_ctx);
1109 return false;
1112 for (i=0; i < num_records; i++) {
1113 if (records[i].wType == DNS_TYPE_SOA) {
1114 talloc_free(tmp_ctx);
1115 return true;
1119 talloc_free(tmp_ctx);
1120 return false;
1123 static bool b9_zone_add(struct dlz_bind9_data *state, const char *name)
1125 struct b9_zone *zone;
1127 zone = talloc_zero(state, struct b9_zone);
1128 if (zone == NULL) {
1129 return false;
1132 zone->name = talloc_strdup(zone, name);
1133 if (zone->name == NULL) {
1134 talloc_free(zone);
1135 return false;
1138 DLIST_ADD(state->zonelist, zone);
1139 return true;
1142 static bool b9_zone_exists(struct dlz_bind9_data *state, const char *name)
1144 struct b9_zone *zone = state->zonelist;
1145 bool found = false;
1147 while (zone != NULL) {
1148 if (strcasecmp(name, zone->name) == 0) {
1149 found = true;
1150 break;
1152 zone = zone->next;
1155 return found;
1160 configure a writeable zone
1162 #if DLZ_DLOPEN_VERSION < 3
1163 _PUBLIC_ isc_result_t dlz_configure(dns_view_t *view, void *dbdata)
1164 #else
1165 _PUBLIC_ isc_result_t dlz_configure(dns_view_t *view, dns_dlzdb_t *dlzdb,
1166 void *dbdata)
1167 #endif
1169 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1170 TALLOC_CTX *tmp_ctx;
1171 struct ldb_dn *dn;
1172 int i;
1174 state->log(ISC_LOG_INFO, "samba_dlz: starting configure");
1175 if (state->writeable_zone == NULL) {
1176 state->log(ISC_LOG_INFO, "samba_dlz: no writeable_zone method available");
1177 return ISC_R_FAILURE;
1180 tmp_ctx = talloc_new(state);
1182 for (i=0; zone_prefixes[i]; i++) {
1183 const char *attrs[] = { "name", NULL };
1184 int j, ret;
1185 struct ldb_result *res;
1187 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
1188 if (dn == NULL) {
1189 talloc_free(tmp_ctx);
1190 return ISC_R_NOMEMORY;
1193 if (!ldb_dn_add_child_fmt(dn, "%s", zone_prefixes[i])) {
1194 talloc_free(tmp_ctx);
1195 return ISC_R_NOMEMORY;
1198 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
1199 attrs, "objectClass=dnsZone");
1200 if (ret != LDB_SUCCESS) {
1201 continue;
1204 for (j=0; j<res->count; j++) {
1205 isc_result_t result;
1206 const char *zone = ldb_msg_find_attr_as_string(res->msgs[j], "name", NULL);
1207 struct ldb_dn *zone_dn;
1209 if (zone == NULL) {
1210 continue;
1212 /* Ignore zones that are not handled in BIND */
1213 if ((strcmp(zone, "RootDNSServers") == 0) ||
1214 (strcmp(zone, "..TrustAnchors") == 0)) {
1215 continue;
1217 zone_dn = ldb_dn_copy(tmp_ctx, dn);
1218 if (zone_dn == NULL) {
1219 talloc_free(tmp_ctx);
1220 return ISC_R_NOMEMORY;
1223 if (!b9_has_soa(state, zone_dn, zone)) {
1224 continue;
1227 if (b9_zone_exists(state, zone)) {
1228 state->log(ISC_LOG_WARNING, "samba_dlz: Ignoring duplicate zone '%s' from '%s'",
1229 zone, ldb_dn_get_linearized(zone_dn));
1230 continue;
1233 if (!b9_zone_add(state, zone)) {
1234 talloc_free(tmp_ctx);
1235 return ISC_R_NOMEMORY;
1238 #if DLZ_DLOPEN_VERSION < 3
1239 result = state->writeable_zone(view, zone);
1240 #else
1241 result = state->writeable_zone(view, dlzdb, zone);
1242 #endif
1243 if (result != ISC_R_SUCCESS) {
1244 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to configure zone '%s'",
1245 zone);
1246 talloc_free(tmp_ctx);
1247 return result;
1249 state->log(ISC_LOG_INFO, "samba_dlz: configured writeable zone '%s'", zone);
1253 talloc_free(tmp_ctx);
1254 return ISC_R_SUCCESS;
1258 authorize a zone update
1260 _PUBLIC_ isc_boolean_t dlz_ssumatch(const char *signer, const char *name, const char *tcpaddr,
1261 const char *type, const char *key, uint32_t keydatalen, uint8_t *keydata,
1262 void *dbdata)
1264 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1265 TALLOC_CTX *tmp_ctx;
1266 DATA_BLOB ap_req;
1267 struct cli_credentials *server_credentials;
1268 char *keytab_name;
1269 int ret;
1270 int ldb_ret;
1271 NTSTATUS nt_status;
1272 struct gensec_security *gensec_ctx;
1273 struct auth_session_info *session_info;
1274 struct ldb_dn *dn;
1275 isc_result_t result;
1276 struct ldb_result *res;
1277 const char * attrs[] = { NULL };
1278 uint32_t access_mask;
1280 /* Remove cached credentials, if any */
1281 if (state->session_info) {
1282 talloc_free(state->session_info);
1283 state->session_info = NULL;
1285 if (state->update_name) {
1286 talloc_free(state->update_name);
1287 state->update_name = NULL;
1290 tmp_ctx = talloc_new(NULL);
1291 if (tmp_ctx == NULL) {
1292 state->log(ISC_LOG_ERROR, "samba_dlz: no memory");
1293 return ISC_FALSE;
1296 ap_req = data_blob_const(keydata, keydatalen);
1297 server_credentials = cli_credentials_init(tmp_ctx);
1298 if (!server_credentials) {
1299 state->log(ISC_LOG_ERROR, "samba_dlz: failed to init server credentials");
1300 talloc_free(tmp_ctx);
1301 return ISC_FALSE;
1304 cli_credentials_set_krb5_context(server_credentials, state->smb_krb5_ctx);
1305 cli_credentials_set_conf(server_credentials, state->lp);
1307 keytab_name = talloc_asprintf(tmp_ctx, "FILE:%s/dns.keytab",
1308 lpcfg_private_dir(state->lp));
1309 ret = cli_credentials_set_keytab_name(server_credentials, state->lp, keytab_name,
1310 CRED_SPECIFIED);
1311 if (ret != 0) {
1312 state->log(ISC_LOG_ERROR, "samba_dlz: failed to obtain server credentials from %s",
1313 keytab_name);
1314 talloc_free(tmp_ctx);
1315 return ISC_FALSE;
1317 talloc_free(keytab_name);
1319 nt_status = gensec_server_start(tmp_ctx,
1320 lpcfg_gensec_settings(tmp_ctx, state->lp),
1321 state->auth_context, &gensec_ctx);
1322 if (!NT_STATUS_IS_OK(nt_status)) {
1323 state->log(ISC_LOG_ERROR, "samba_dlz: failed to start gensec server");
1324 talloc_free(tmp_ctx);
1325 return ISC_FALSE;
1328 gensec_set_credentials(gensec_ctx, server_credentials);
1330 nt_status = gensec_start_mech_by_name(gensec_ctx, "spnego");
1331 if (!NT_STATUS_IS_OK(nt_status)) {
1332 state->log(ISC_LOG_ERROR, "samba_dlz: failed to start spnego");
1333 talloc_free(tmp_ctx);
1334 return ISC_FALSE;
1337 nt_status = gensec_update_ev(gensec_ctx, tmp_ctx, state->ev_ctx, ap_req, &ap_req);
1338 if (!NT_STATUS_IS_OK(nt_status)) {
1339 state->log(ISC_LOG_ERROR, "samba_dlz: spnego update failed");
1340 talloc_free(tmp_ctx);
1341 return ISC_FALSE;
1344 nt_status = gensec_session_info(gensec_ctx, tmp_ctx, &session_info);
1345 if (!NT_STATUS_IS_OK(nt_status)) {
1346 state->log(ISC_LOG_ERROR, "samba_dlz: failed to create session info");
1347 talloc_free(tmp_ctx);
1348 return ISC_FALSE;
1351 /* Get the DN from name */
1352 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1353 if (result != ISC_R_SUCCESS) {
1354 state->log(ISC_LOG_ERROR, "samba_dlz: failed to find name %s", name);
1355 talloc_free(tmp_ctx);
1356 return ISC_FALSE;
1359 /* make sure the dn exists, or find parent dn in case new object is being added */
1360 ldb_ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
1361 attrs, "objectClass=dnsNode");
1362 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
1363 ldb_dn_remove_child_components(dn, 1);
1364 access_mask = SEC_ADS_CREATE_CHILD;
1365 talloc_free(res);
1366 } else if (ldb_ret == LDB_SUCCESS) {
1367 access_mask = SEC_STD_REQUIRED | SEC_ADS_SELF_WRITE;
1368 talloc_free(res);
1369 } else {
1370 talloc_free(tmp_ctx);
1371 return ISC_FALSE;
1374 /* Do ACL check */
1375 ldb_ret = dsdb_check_access_on_dn(state->samdb, tmp_ctx, dn,
1376 session_info->security_token,
1377 access_mask, NULL);
1378 if (ldb_ret != LDB_SUCCESS) {
1379 state->log(ISC_LOG_INFO,
1380 "samba_dlz: disallowing update of signer=%s name=%s type=%s error=%s",
1381 signer, name, type, ldb_strerror(ldb_ret));
1382 talloc_free(tmp_ctx);
1383 return ISC_FALSE;
1386 /* Cache session_info, so it can be used in the actual add/delete operation */
1387 state->update_name = talloc_strdup(state, name);
1388 if (state->update_name == NULL) {
1389 state->log(ISC_LOG_ERROR, "samba_dlz: memory allocation error");
1390 talloc_free(tmp_ctx);
1391 return ISC_FALSE;
1393 state->session_info = talloc_steal(state, session_info);
1395 state->log(ISC_LOG_INFO, "samba_dlz: allowing update of signer=%s name=%s tcpaddr=%s type=%s key=%s",
1396 signer, name, tcpaddr, type, key);
1398 talloc_free(tmp_ctx);
1399 return ISC_TRUE;
1403 see if two DNS names are the same
1405 static bool dns_name_equal(const char *name1, const char *name2)
1407 size_t len1 = strlen(name1);
1408 size_t len2 = strlen(name2);
1409 if (name1[len1-1] == '.') len1--;
1410 if (name2[len2-1] == '.') len2--;
1411 if (len1 != len2) {
1412 return false;
1414 return strncasecmp_m(name1, name2, len1) == 0;
1419 see if two dns records match
1421 static bool b9_record_match(struct dlz_bind9_data *state,
1422 struct dnsp_DnssrvRpcRecord *rec1, struct dnsp_DnssrvRpcRecord *rec2)
1424 bool status;
1425 int i;
1426 struct in6_addr rec1_in_addr6;
1427 struct in6_addr rec2_in_addr6;
1429 if (rec1->wType != rec2->wType) {
1430 return false;
1432 /* see if this type is single valued */
1433 if (b9_single_valued(rec1->wType)) {
1434 return true;
1437 /* see if the data matches */
1438 switch (rec1->wType) {
1439 case DNS_TYPE_A:
1440 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
1441 case DNS_TYPE_AAAA:
1442 inet_pton(AF_INET6, rec1->data.ipv6, &rec1_in_addr6);
1443 inet_pton(AF_INET6, rec2->data.ipv6, &rec2_in_addr6);
1444 return memcmp(&rec1_in_addr6, &rec2_in_addr6, sizeof(rec1_in_addr6)) == 0;
1445 case DNS_TYPE_CNAME:
1446 return dns_name_equal(rec1->data.cname, rec2->data.cname);
1447 case DNS_TYPE_TXT:
1448 status = (rec1->data.txt.count == rec2->data.txt.count);
1449 if (!status) return status;
1450 for (i=0; i<rec1->data.txt.count; i++) {
1451 status &= (strcmp(rec1->data.txt.str[i], rec2->data.txt.str[i]) == 0);
1453 return status;
1454 case DNS_TYPE_PTR:
1455 return dns_name_equal(rec1->data.ptr, rec2->data.ptr);
1456 case DNS_TYPE_NS:
1457 return dns_name_equal(rec1->data.ns, rec2->data.ns);
1459 case DNS_TYPE_SRV:
1460 return rec1->data.srv.wPriority == rec2->data.srv.wPriority &&
1461 rec1->data.srv.wWeight == rec2->data.srv.wWeight &&
1462 rec1->data.srv.wPort == rec2->data.srv.wPort &&
1463 dns_name_equal(rec1->data.srv.nameTarget, rec2->data.srv.nameTarget);
1465 case DNS_TYPE_MX:
1466 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
1467 dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
1469 case DNS_TYPE_HINFO:
1470 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
1471 strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
1473 case DNS_TYPE_SOA:
1474 return dns_name_equal(rec1->data.soa.mname, rec2->data.soa.mname) &&
1475 dns_name_equal(rec1->data.soa.rname, rec2->data.soa.rname) &&
1476 rec1->data.soa.serial == rec2->data.soa.serial &&
1477 rec1->data.soa.refresh == rec2->data.soa.refresh &&
1478 rec1->data.soa.retry == rec2->data.soa.retry &&
1479 rec1->data.soa.expire == rec2->data.soa.expire &&
1480 rec1->data.soa.minimum == rec2->data.soa.minimum;
1481 default:
1482 state->log(ISC_LOG_ERROR, "samba_dlz b9_record_match: unhandled record type %u",
1483 rec1->wType);
1484 break;
1487 return false;
1491 * Update session_info on samdb using the cached credentials
1493 static bool b9_set_session_info(struct dlz_bind9_data *state, const char *name)
1495 int ret;
1497 if (state->update_name == NULL || state->session_info == NULL) {
1498 state->log(ISC_LOG_ERROR, "samba_dlz: invalid credentials");
1499 return false;
1502 /* Do not use client credentials, if we're not updating the client specified name */
1503 if (strcmp(state->update_name, name) != 0) {
1504 return true;
1507 ret = ldb_set_opaque(state->samdb, "sessionInfo", state->session_info);
1508 if (ret != LDB_SUCCESS) {
1509 state->log(ISC_LOG_ERROR, "samba_dlz: unable to set session info");
1510 return false;
1513 return true;
1517 * Reset session_info on samdb as system session
1519 static void b9_reset_session_info(struct dlz_bind9_data *state)
1521 ldb_set_opaque(state->samdb, "sessionInfo", system_session(state->lp));
1525 add or modify a rdataset
1527 _PUBLIC_ isc_result_t dlz_addrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1529 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1530 struct dnsp_DnssrvRpcRecord *rec;
1531 struct ldb_dn *dn;
1532 isc_result_t result;
1533 bool tombstoned = false;
1534 bool needs_add = false;
1535 struct dnsp_DnssrvRpcRecord *recs = NULL;
1536 uint16_t num_recs = 0;
1537 uint16_t first = 0;
1538 uint16_t i;
1539 NTTIME t;
1540 WERROR werr;
1542 if (state->transaction_token != (void*)version) {
1543 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1544 return ISC_R_FAILURE;
1547 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1548 if (rec == NULL) {
1549 return ISC_R_NOMEMORY;
1552 unix_to_nt_time(&t, time(NULL));
1553 t /= 10*1000*1000; /* convert to seconds (NT time is in 100ns units) */
1554 t /= 3600; /* convert to hours */
1556 rec->rank = DNS_RANK_ZONE;
1557 rec->dwTimeStamp = (uint32_t)t;
1559 if (!b9_parse(state, rdatastr, rec)) {
1560 state->log(ISC_LOG_INFO, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1561 talloc_free(rec);
1562 return ISC_R_FAILURE;
1565 /* find the DN of the record */
1566 result = b9_find_name_dn(state, name, rec, &dn);
1567 if (result != ISC_R_SUCCESS) {
1568 talloc_free(rec);
1569 return result;
1572 /* get any existing records */
1573 werr = dns_common_lookup(state->samdb, rec, dn,
1574 &recs, &num_recs, &tombstoned);
1575 if (W_ERROR_EQUAL(werr, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
1576 needs_add = true;
1577 werr = WERR_OK;
1579 if (!W_ERROR_IS_OK(werr)) {
1580 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s, %s",
1581 ldb_dn_get_linearized(dn), win_errstr(werr));
1582 talloc_free(rec);
1583 return ISC_R_FAILURE;
1586 if (tombstoned) {
1588 * we need to keep the existing tombstone record
1589 * and ignore it
1591 first = num_recs;
1594 /* there are existing records. We need to see if this will
1595 * replace a record or add to it
1597 for (i=first; i < num_recs; i++) {
1598 if (b9_record_match(state, rec, &recs[i])) {
1599 break;
1602 if (i == UINT16_MAX) {
1603 state->log(ISC_LOG_ERROR, "samba_dlz: failed to already %u dnsRecord values for %s",
1604 i, ldb_dn_get_linearized(dn));
1605 talloc_free(rec);
1606 return ISC_R_FAILURE;
1609 if (i == num_recs) {
1610 /* adding a new value */
1611 recs = talloc_realloc(rec, recs,
1612 struct dnsp_DnssrvRpcRecord,
1613 num_recs + 1);
1614 if (recs == NULL) {
1615 talloc_free(rec);
1616 return ISC_R_NOMEMORY;
1618 num_recs++;
1621 recs[i] = *rec;
1623 if (!b9_set_session_info(state, name)) {
1624 talloc_free(rec);
1625 return ISC_R_FAILURE;
1628 /* modify the record */
1629 werr = dns_common_replace(state->samdb, rec, dn,
1630 needs_add,
1631 state->soa_serial,
1632 recs, num_recs);
1633 b9_reset_session_info(state);
1634 if (!W_ERROR_IS_OK(werr)) {
1635 state->log(ISC_LOG_ERROR, "samba_dlz: failed to %s %s - %s",
1636 needs_add ? "add" : "modify",
1637 ldb_dn_get_linearized(dn), win_errstr(werr));
1638 talloc_free(rec);
1639 return ISC_R_FAILURE;
1642 state->log(ISC_LOG_INFO, "samba_dlz: added rdataset %s '%s'", name, rdatastr);
1644 talloc_free(rec);
1645 return ISC_R_SUCCESS;
1649 remove a rdataset
1651 _PUBLIC_ isc_result_t dlz_subrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1653 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1654 struct dnsp_DnssrvRpcRecord *rec;
1655 struct ldb_dn *dn;
1656 isc_result_t result;
1657 struct dnsp_DnssrvRpcRecord *recs = NULL;
1658 uint16_t num_recs = 0;
1659 uint16_t i;
1660 WERROR werr;
1662 if (state->transaction_token != (void*)version) {
1663 state->log(ISC_LOG_ERROR, "samba_dlz: bad transaction version");
1664 return ISC_R_FAILURE;
1667 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1668 if (rec == NULL) {
1669 return ISC_R_NOMEMORY;
1672 if (!b9_parse(state, rdatastr, rec)) {
1673 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1674 talloc_free(rec);
1675 return ISC_R_FAILURE;
1678 /* find the DN of the record */
1679 result = b9_find_name_dn(state, name, rec, &dn);
1680 if (result != ISC_R_SUCCESS) {
1681 talloc_free(rec);
1682 return result;
1685 /* get the existing records */
1686 werr = dns_common_lookup(state->samdb, rec, dn,
1687 &recs, &num_recs, NULL);
1688 if (!W_ERROR_IS_OK(werr)) {
1689 talloc_free(rec);
1690 return ISC_R_NOTFOUND;
1693 for (i=0; i < num_recs; i++) {
1694 if (b9_record_match(state, rec, &recs[i])) {
1695 recs[i] = (struct dnsp_DnssrvRpcRecord) {
1696 .wType = DNS_TYPE_TOMBSTONE,
1698 break;
1701 if (i == num_recs) {
1702 talloc_free(rec);
1703 return ISC_R_NOTFOUND;
1706 if (!b9_set_session_info(state, name)) {
1707 talloc_free(rec);
1708 return ISC_R_FAILURE;
1711 /* modify the record */
1712 werr = dns_common_replace(state->samdb, rec, dn,
1713 false,/* needs_add */
1714 state->soa_serial,
1715 recs, num_recs);
1716 b9_reset_session_info(state);
1717 if (!W_ERROR_IS_OK(werr)) {
1718 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1719 ldb_dn_get_linearized(dn), win_errstr(werr));
1720 talloc_free(rec);
1721 return ISC_R_FAILURE;
1724 state->log(ISC_LOG_INFO, "samba_dlz: subtracted rdataset %s '%s'", name, rdatastr);
1726 talloc_free(rec);
1727 return ISC_R_SUCCESS;
1732 delete all records of the given type
1734 _PUBLIC_ isc_result_t dlz_delrdataset(const char *name, const char *type, void *dbdata, void *version)
1736 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1737 TALLOC_CTX *tmp_ctx;
1738 struct ldb_dn *dn;
1739 isc_result_t result;
1740 enum dns_record_type dns_type;
1741 bool found = false;
1742 struct dnsp_DnssrvRpcRecord *recs = NULL;
1743 uint16_t num_recs = 0;
1744 uint16_t ri = 0;
1745 WERROR werr;
1747 if (state->transaction_token != (void*)version) {
1748 state->log(ISC_LOG_ERROR, "samba_dlz: bad transaction version");
1749 return ISC_R_FAILURE;
1752 if (!b9_dns_type(type, &dns_type)) {
1753 state->log(ISC_LOG_ERROR, "samba_dlz: bad dns type %s in delete", type);
1754 return ISC_R_FAILURE;
1757 tmp_ctx = talloc_new(state);
1759 /* find the DN of the record */
1760 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1761 if (result != ISC_R_SUCCESS) {
1762 talloc_free(tmp_ctx);
1763 return result;
1766 /* get the existing records */
1767 werr = dns_common_lookup(state->samdb, tmp_ctx, dn,
1768 &recs, &num_recs, NULL);
1769 if (!W_ERROR_IS_OK(werr)) {
1770 talloc_free(tmp_ctx);
1771 return ISC_R_NOTFOUND;
1774 for (ri=0; ri < num_recs; ri++) {
1775 if (dns_type != recs[ri].wType) {
1776 continue;
1779 found = true;
1780 recs[ri] = (struct dnsp_DnssrvRpcRecord) {
1781 .wType = DNS_TYPE_TOMBSTONE,
1785 if (!found) {
1786 talloc_free(tmp_ctx);
1787 return ISC_R_FAILURE;
1790 if (!b9_set_session_info(state, name)) {
1791 talloc_free(tmp_ctx);
1792 return ISC_R_FAILURE;
1795 /* modify the record */
1796 werr = dns_common_replace(state->samdb, tmp_ctx, dn,
1797 false,/* needs_add */
1798 state->soa_serial,
1799 recs, num_recs);
1800 b9_reset_session_info(state);
1801 if (!W_ERROR_IS_OK(werr)) {
1802 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1803 ldb_dn_get_linearized(dn), win_errstr(werr));
1804 talloc_free(tmp_ctx);
1805 return ISC_R_FAILURE;
1808 state->log(ISC_LOG_INFO, "samba_dlz: deleted rdataset %s of type %s", name, type);
1810 talloc_free(tmp_ctx);
1811 return ISC_R_SUCCESS;