2 Unix SMB/CIFS implementation.
4 bind9 dlz driver for Samba
6 Copyright (C) 2010 Andrew Tridgell
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "param/param.h"
25 #include "lib/events/events.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "dsdb/common/util.h"
28 #include "auth/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"
39 #include "lib/util/dlinklist.h"
40 #include "dlz_minimal.h"
41 #include "dnsserver_common.h"
42 #include "lib/util/smb_strtox.h"
43 #include "lib/util/access.h"
54 struct b9_zone
*prev
, *next
;
57 struct dlz_bind9_data
{
58 struct b9_options options
;
59 struct ldb_context
*samdb
;
60 struct tevent_context
*ev_ctx
;
61 struct loadparm_context
*lp
;
62 int *transaction_token
;
64 struct b9_zone
*zonelist
;
66 /* Used for dynamic update */
67 struct smb_krb5_context
*smb_krb5_ctx
;
68 struct auth4_context
*auth_context
;
69 struct auth_session_info
*session_info
;
72 /* helper functions from the dlz_dlopen driver */
74 dns_sdlz_putrr_t
*putrr
;
75 dns_sdlz_putnamedrr_t
*putnamedrr
;
76 dns_dlz_writeablezone_t
*writeable_zone
;
79 static struct dlz_bind9_data
*dlz_bind9_state
= NULL
;
80 static int dlz_bind9_state_ref_count
= 0;
82 static const char *zone_prefixes
[] = {
83 "CN=MicrosoftDNS,DC=DomainDnsZones",
84 "CN=MicrosoftDNS,DC=ForestDnsZones",
85 "CN=MicrosoftDNS,CN=System",
90 * Get a printable string representation of an isc_result_t
92 static const char *isc_result_str( const isc_result_t result
) {
95 return "ISC_R_SUCCESS";
97 return "ISC_R_NOMEMORY";
99 return "ISC_R_NOPERM";
101 return "ISC_R_NOSPACE";
103 return "ISC_R_NOTFOUND";
105 return "ISC_R_FAILURE";
106 case ISC_R_NOTIMPLEMENTED
:
107 return "ISC_R_NOTIMPLEMENTED";
109 return "ISC_R_NOMORE";
110 case ISC_R_INVALIDFILE
:
111 return "ISC_R_INVALIDFILE";
112 case ISC_R_UNEXPECTED
:
113 return "ISC_R_UNEXPECTED";
114 case ISC_R_FILENOTFOUND
:
115 return "ISC_R_FILENOTFOUND";
122 return the version of the API
124 _PUBLIC_
int dlz_version(unsigned int *flags
)
126 return DLZ_DLOPEN_VERSION
;
130 remember a helper function from the bind9 dlz_dlopen driver
132 static void b9_add_helper(struct dlz_bind9_data
*state
, const char *helper_name
, void *ptr
)
134 if (strcmp(helper_name
, "log") == 0) {
137 if (strcmp(helper_name
, "putrr") == 0) {
140 if (strcmp(helper_name
, "putnamedrr") == 0) {
141 state
->putnamedrr
= ptr
;
143 if (strcmp(helper_name
, "writeable_zone") == 0) {
144 state
->writeable_zone
= ptr
;
149 * Add a trailing '.' if it's missing
151 static const char *b9_format_fqdn(TALLOC_CTX
*mem_ctx
, const char *str
)
156 if (str
== NULL
|| str
[0] == '\0') {
161 if (str
[len
-1] != '.') {
162 tmp
= talloc_asprintf(mem_ctx
, "%s.", str
);
170 * Format a record for bind9.
172 * On failure/error returns false, OR sets *data to NULL.
173 * Callers should check for both!
175 static bool b9_format(struct dlz_bind9_data
*state
,
177 struct dnsp_DnssrvRpcRecord
*rec
,
178 const char **type
, const char **data
)
184 switch (rec
->wType
) {
187 *data
= rec
->data
.ipv4
;
192 *data
= rec
->data
.ipv6
;
197 *data
= b9_format_fqdn(mem_ctx
, rec
->data
.cname
);
202 tmp
= talloc_asprintf(mem_ctx
, "\"%s\"", rec
->data
.txt
.str
[0]);
203 for (i
=1; i
<rec
->data
.txt
.count
; i
++) {
204 talloc_asprintf_addbuf(&tmp
, " \"%s\"", rec
->data
.txt
.str
[i
]);
211 *data
= b9_format_fqdn(mem_ctx
, rec
->data
.ptr
);
216 fqdn
= b9_format_fqdn(mem_ctx
, rec
->data
.srv
.nameTarget
);
220 *data
= talloc_asprintf(mem_ctx
, "%u %u %u %s",
221 rec
->data
.srv
.wPriority
,
222 rec
->data
.srv
.wWeight
,
229 fqdn
= b9_format_fqdn(mem_ctx
, rec
->data
.mx
.nameTarget
);
233 *data
= talloc_asprintf(mem_ctx
, "%u %s",
234 rec
->data
.mx
.wPriority
, fqdn
);
239 *data
= b9_format_fqdn(mem_ctx
, rec
->data
.ns
);
243 const char *dns_hostname
= NULL
;
247 /* we need to fake the authoritative nameserver to
248 * point at ourselves. This is how AD DNS servers
249 * force clients to send updates to the right local DC
251 dns_hostname
= lpcfg_dns_hostname(state
->lp
);
252 if (dns_hostname
== NULL
) {
255 mname
= talloc_asprintf(mem_ctx
, "%s.", dns_hostname
);
260 fqdn
= b9_format_fqdn(mem_ctx
, rec
->data
.soa
.rname
);
265 state
->soa_serial
= rec
->data
.soa
.serial
;
267 *data
= talloc_asprintf(mem_ctx
, "%s %s %u %u %u %u %u",
269 rec
->data
.soa
.serial
,
270 rec
->data
.soa
.refresh
,
272 rec
->data
.soa
.expire
,
273 rec
->data
.soa
.minimum
);
278 state
->log(ISC_LOG_ERROR
, "samba_dlz b9_format: unhandled record type %u",
286 static const struct {
287 enum dns_record_type dns_type
;
291 { DNS_TYPE_A
, "A" , false},
292 { DNS_TYPE_AAAA
, "AAAA" , false},
293 { DNS_TYPE_CNAME
, "CNAME" , true},
294 { DNS_TYPE_TXT
, "TXT" , false},
295 { DNS_TYPE_PTR
, "PTR" , false},
296 { DNS_TYPE_SRV
, "SRV" , false},
297 { DNS_TYPE_MX
, "MX" , false},
298 { DNS_TYPE_NS
, "NS" , false},
299 { DNS_TYPE_SOA
, "SOA" , true},
304 see if a DNS type is single valued
306 static bool b9_single_valued(enum dns_record_type dns_type
)
309 for (i
=0; i
<ARRAY_SIZE(dns_typemap
); i
++) {
310 if (dns_typemap
[i
].dns_type
== dns_type
) {
311 return dns_typemap
[i
].single_valued
;
318 get a DNS_TYPE_* value from the corresponding string
320 static bool b9_dns_type(const char *type
, enum dns_record_type
*dtype
)
323 for (i
=0; i
<ARRAY_SIZE(dns_typemap
); i
++) {
324 if (strcasecmp(dns_typemap
[i
].typestr
, type
) == 0) {
325 *dtype
= dns_typemap
[i
].dns_type
;
333 #define DNS_PARSE_STR(ret, str, sep, saveptr) do { \
334 (ret) = strtok_r(str, sep, &saveptr); \
335 if ((ret) == NULL) return false; \
338 #define DNS_PARSE_UINT(ret, str, sep, saveptr) do { \
339 char *istr = strtok_r(str, sep, &saveptr); \
341 if ((istr) == NULL) return false; \
342 (ret) = smb_strtoul(istr, NULL, 10, &error, SMB_STR_STANDARD); \
349 parse a record from bind9
351 static bool b9_parse(struct dlz_bind9_data
*state
,
352 const char *rdatastr
,
353 struct dnsp_DnssrvRpcRecord
*rec
)
355 char *full_name
, *dclass
, *type
;
356 char *str
, *tmp
, *saveptr
=NULL
;
359 str
= talloc_strdup(rec
, rdatastr
);
364 /* parse the SDLZ string form */
365 DNS_PARSE_STR(full_name
, str
, "\t", saveptr
);
366 DNS_PARSE_UINT(rec
->dwTtlSeconds
, NULL
, "\t", saveptr
);
367 DNS_PARSE_STR(dclass
, NULL
, "\t", saveptr
);
368 DNS_PARSE_STR(type
, NULL
, "\t", saveptr
);
370 /* construct the record */
371 for (i
=0; i
<ARRAY_SIZE(dns_typemap
); i
++) {
372 if (strcasecmp(type
, dns_typemap
[i
].typestr
) == 0) {
373 rec
->wType
= dns_typemap
[i
].dns_type
;
377 if (i
== ARRAY_SIZE(dns_typemap
)) {
378 state
->log(ISC_LOG_ERROR
, "samba_dlz: unsupported record type '%s' for '%s'",
383 switch (rec
->wType
) {
385 DNS_PARSE_STR(rec
->data
.ipv4
, NULL
, " ", saveptr
);
389 DNS_PARSE_STR(rec
->data
.ipv6
, NULL
, " ", saveptr
);
393 DNS_PARSE_STR(rec
->data
.cname
, NULL
, " ", saveptr
);
397 rec
->data
.txt
.count
= 0;
398 rec
->data
.txt
.str
= talloc_array(rec
, const char *, rec
->data
.txt
.count
);
399 tmp
= strtok_r(NULL
, "\t", &saveptr
);
401 rec
->data
.txt
.str
= talloc_realloc(rec
, rec
->data
.txt
.str
, const char *,
402 rec
->data
.txt
.count
+1);
405 rec
->data
.txt
.str
[rec
->data
.txt
.count
] = talloc_strndup(rec
, &tmp
[1], strlen(tmp
)-2);
407 rec
->data
.txt
.str
[rec
->data
.txt
.count
] = talloc_strdup(rec
, tmp
);
409 rec
->data
.txt
.count
++;
410 tmp
= strtok_r(NULL
, " ", &saveptr
);
415 DNS_PARSE_STR(rec
->data
.ptr
, NULL
, " ", saveptr
);
419 DNS_PARSE_UINT(rec
->data
.srv
.wPriority
, NULL
, " ", saveptr
);
420 DNS_PARSE_UINT(rec
->data
.srv
.wWeight
, NULL
, " ", saveptr
);
421 DNS_PARSE_UINT(rec
->data
.srv
.wPort
, NULL
, " ", saveptr
);
422 DNS_PARSE_STR(rec
->data
.srv
.nameTarget
, NULL
, " ", saveptr
);
426 DNS_PARSE_UINT(rec
->data
.mx
.wPriority
, NULL
, " ", saveptr
);
427 DNS_PARSE_STR(rec
->data
.mx
.nameTarget
, NULL
, " ", saveptr
);
431 DNS_PARSE_STR(rec
->data
.ns
, NULL
, " ", saveptr
);
435 DNS_PARSE_STR(rec
->data
.soa
.mname
, NULL
, " ", saveptr
);
436 DNS_PARSE_STR(rec
->data
.soa
.rname
, NULL
, " ", saveptr
);
437 DNS_PARSE_UINT(rec
->data
.soa
.serial
, NULL
, " ", saveptr
);
438 DNS_PARSE_UINT(rec
->data
.soa
.refresh
, NULL
, " ", saveptr
);
439 DNS_PARSE_UINT(rec
->data
.soa
.retry
, NULL
, " ", saveptr
);
440 DNS_PARSE_UINT(rec
->data
.soa
.expire
, NULL
, " ", saveptr
);
441 DNS_PARSE_UINT(rec
->data
.soa
.minimum
, NULL
, " ", saveptr
);
445 state
->log(ISC_LOG_ERROR
, "samba_dlz b9_parse: unhandled record type %u",
450 /* we should be at the end of the buffer now */
451 if (strtok_r(NULL
, "\t ", &saveptr
) != NULL
) {
452 state
->log(ISC_LOG_ERROR
, "samba_dlz b9_parse: unexpected data at end of string for '%s'",
461 send a resource record to bind9
463 static isc_result_t
b9_putrr(struct dlz_bind9_data
*state
,
464 void *handle
, struct dnsp_DnssrvRpcRecord
*rec
,
468 const char *type
, *data
;
469 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
471 if (!b9_format(state
, tmp_ctx
, rec
, &type
, &data
)) {
472 return ISC_R_FAILURE
;
476 talloc_free(tmp_ctx
);
477 return ISC_R_NOMEMORY
;
482 for (i
=0; types
[i
]; i
++) {
483 if (strcmp(types
[i
], type
) == 0) break;
485 if (types
[i
] == NULL
) {
487 return ISC_R_SUCCESS
;
491 result
= state
->putrr(handle
, type
, rec
->dwTtlSeconds
, data
);
492 if (result
!= ISC_R_SUCCESS
) {
493 state
->log(ISC_LOG_ERROR
, "Failed to put rr");
495 talloc_free(tmp_ctx
);
501 send a named resource record to bind9
503 static isc_result_t
b9_putnamedrr(struct dlz_bind9_data
*state
,
504 void *handle
, const char *name
,
505 struct dnsp_DnssrvRpcRecord
*rec
)
508 const char *type
, *data
;
509 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
511 if (!b9_format(state
, tmp_ctx
, rec
, &type
, &data
)) {
512 return ISC_R_FAILURE
;
516 talloc_free(tmp_ctx
);
517 return ISC_R_NOMEMORY
;
520 result
= state
->putnamedrr(handle
, name
, type
, rec
->dwTtlSeconds
, data
);
521 if (result
!= ISC_R_SUCCESS
) {
522 state
->log(ISC_LOG_ERROR
, "Failed to put named rr '%s'", name
);
524 talloc_free(tmp_ctx
);
531 static isc_result_t
parse_options(struct dlz_bind9_data
*state
,
532 unsigned int argc
, const char **argv
,
533 struct b9_options
*options
)
537 struct poptOption long_options
[] = {
538 { "url", 'H', POPT_ARG_STRING
, &options
->url
, 0, "database URL", "URL" },
539 { "debug", 'd', POPT_ARG_STRING
, &options
->debug
, 0, "debug level", "DEBUG" },
543 pc
= poptGetContext("dlz_bind9", argc
, argv
, long_options
,
544 POPT_CONTEXT_KEEP_FIRST
);
545 while ((opt
= poptGetNextOpt(pc
)) != -1) {
548 state
->log(ISC_LOG_ERROR
, "dlz_bind9: Invalid option %s: %s",
549 poptBadOption(pc
, 0), poptStrerror(opt
));
551 return ISC_R_FAILURE
;
556 return ISC_R_SUCCESS
;
561 * Create session info from PAC
562 * This is called as auth_context->generate_session_info_pac()
564 static NTSTATUS
b9_generate_session_info_pac(struct auth4_context
*auth_context
,
566 struct smb_krb5_context
*smb_krb5_context
,
568 const char *principal_name
,
569 const struct tsocket_address
*remote_addr
,
570 uint32_t session_info_flags
,
571 struct auth_session_info
**session_info
)
574 struct auth_user_info_dc
*user_info_dc
;
577 tmp_ctx
= talloc_new(mem_ctx
);
578 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
580 status
= kerberos_pac_blob_to_user_info_dc(tmp_ctx
,
582 smb_krb5_context
->krb5_context
,
586 if (!NT_STATUS_IS_OK(status
)) {
587 talloc_free(tmp_ctx
);
591 if (!(user_info_dc
->info
->user_flags
& NETLOGON_GUEST
)) {
592 session_info_flags
|= AUTH_SESSION_INFO_AUTHENTICATED
;
595 session_info_flags
|= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES
;
597 status
= auth_generate_session_info(mem_ctx
, auth_context
->lp_ctx
, NULL
, user_info_dc
,
598 session_info_flags
, session_info
);
599 if (!NT_STATUS_IS_OK(status
)) {
600 talloc_free(tmp_ctx
);
604 talloc_free(tmp_ctx
);
608 /* Callback for the DEBUG() system, to catch the remaining messages */
609 static void b9_debug(void *private_ptr
, int msg_level
, const char *msg
)
611 static const int isc_log_map
[] = {
612 ISC_LOG_CRITICAL
, /* 0 */
613 ISC_LOG_ERROR
, /* 1 */
614 ISC_LOG_WARNING
, /* 2 */
615 ISC_LOG_NOTICE
/* 3 */
617 struct dlz_bind9_data
*state
= private_ptr
;
620 if (msg_level
>= ARRAY_SIZE(isc_log_map
) || msg_level
< 0) {
621 isc_log_level
= ISC_LOG_INFO
;
623 isc_log_level
= isc_log_map
[msg_level
];
625 state
->log(isc_log_level
, "samba_dlz: %s", msg
);
628 static int dlz_state_debug_unregister(struct dlz_bind9_data
*state
)
630 /* Stop logging (to the bind9 logs) */
631 debug_set_callback(NULL
, NULL
);
636 called to initialise the driver
638 _PUBLIC_ isc_result_t
dlz_create(const char *dlzname
,
639 unsigned int argc
, const char **argv
,
642 struct dlz_bind9_data
*state
;
643 const char *helper_name
;
649 char *errstring
= NULL
;
651 if (dlz_bind9_state
!= NULL
) {
652 dlz_bind9_state
->log(ISC_LOG_ERROR
,
653 "samba_dlz: dlz_create ignored, #refs=%d",
654 dlz_bind9_state_ref_count
);
655 *dbdata
= dlz_bind9_state
;
656 dlz_bind9_state_ref_count
++;
657 return ISC_R_SUCCESS
;
660 state
= talloc_zero(NULL
, struct dlz_bind9_data
);
662 return ISC_R_NOMEMORY
;
665 talloc_set_destructor(state
, dlz_state_debug_unregister
);
667 /* fill in the helper functions */
668 va_start(ap
, dbdata
);
669 while ((helper_name
= va_arg(ap
, const char *)) != NULL
) {
670 b9_add_helper(state
, helper_name
, va_arg(ap
, void*));
674 /* Do not install samba signal handlers */
675 fault_setup_disable();
677 /* Start logging (to the bind9 logs) */
678 debug_set_callback(state
, b9_debug
);
680 state
->ev_ctx
= s4_event_context_init(state
);
681 if (state
->ev_ctx
== NULL
) {
682 result
= ISC_R_NOMEMORY
;
686 result
= parse_options(state
, argc
, argv
, &state
->options
);
687 if (result
!= ISC_R_SUCCESS
) {
691 state
->lp
= loadparm_init_global(true);
692 if (state
->lp
== NULL
) {
693 result
= ISC_R_NOMEMORY
;
697 if (state
->options
.debug
) {
698 lpcfg_do_global_parameter(state
->lp
, "log level", state
->options
.debug
);
700 lpcfg_do_global_parameter(state
->lp
, "log level", "0");
703 if (smb_krb5_init_context(state
, state
->lp
, &state
->smb_krb5_ctx
) != 0) {
704 result
= ISC_R_NOMEMORY
;
708 nt_status
= gensec_init();
709 if (!NT_STATUS_IS_OK(nt_status
)) {
710 result
= ISC_R_NOMEMORY
;
714 state
->auth_context
= talloc_zero(state
, struct auth4_context
);
715 if (state
->auth_context
== NULL
) {
716 result
= ISC_R_NOMEMORY
;
720 if (state
->options
.url
== NULL
) {
721 state
->options
.url
= talloc_asprintf(state
,
723 lpcfg_binddns_dir(state
->lp
));
724 if (state
->options
.url
== NULL
) {
725 result
= ISC_R_NOMEMORY
;
729 if (!file_exist(state
->options
.url
)) {
730 state
->options
.url
= talloc_asprintf(state
,
732 lpcfg_private_dir(state
->lp
));
733 if (state
->options
.url
== NULL
) {
734 result
= ISC_R_NOMEMORY
;
740 ret
= samdb_connect_url(state
,
743 system_session(state
->lp
),
749 if (ret
!= LDB_SUCCESS
) {
750 state
->log(ISC_LOG_ERROR
,
751 "samba_dlz: Failed to connect to %s: %s",
752 errstring
, ldb_strerror(ret
));
753 result
= ISC_R_FAILURE
;
757 dn
= ldb_get_default_basedn(state
->samdb
);
759 state
->log(ISC_LOG_ERROR
, "samba_dlz: Unable to get basedn for %s - %s",
760 state
->options
.url
, ldb_errstring(state
->samdb
));
761 result
= ISC_R_FAILURE
;
765 state
->log(ISC_LOG_INFO
, "samba_dlz: started for DN %s",
766 ldb_dn_get_linearized(dn
));
768 state
->auth_context
->event_ctx
= state
->ev_ctx
;
769 state
->auth_context
->lp_ctx
= state
->lp
;
770 state
->auth_context
->sam_ctx
= state
->samdb
;
771 state
->auth_context
->generate_session_info_pac
= b9_generate_session_info_pac
;
774 dlz_bind9_state
= state
;
775 dlz_bind9_state_ref_count
++;
777 return ISC_R_SUCCESS
;
780 state
->log(ISC_LOG_INFO
,
781 "samba_dlz: FAILED dlz_create call result=%d #refs=%d",
783 dlz_bind9_state_ref_count
);
791 _PUBLIC_
void dlz_destroy(void *dbdata
)
793 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
795 dlz_bind9_state_ref_count
--;
796 if (dlz_bind9_state_ref_count
== 0) {
797 state
->log(ISC_LOG_INFO
, "samba_dlz: shutting down");
798 talloc_unlink(state
, state
->samdb
);
800 dlz_bind9_state
= NULL
;
802 state
->log(ISC_LOG_INFO
,
803 "samba_dlz: dlz_destroy called. %d refs remaining.",
804 dlz_bind9_state_ref_count
);
810 return the base DN for a zone
812 static isc_result_t
b9_find_zone_dn(struct dlz_bind9_data
*state
, const char *zone_name
,
813 TALLOC_CTX
*mem_ctx
, struct ldb_dn
**zone_dn
)
816 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
817 const char *attrs
[] = { NULL
};
820 for (i
=0; zone_prefixes
[i
]; i
++) {
821 const char *casefold
;
823 struct ldb_result
*res
;
824 struct ldb_val zone_name_val
825 = data_blob_string_const(zone_name
);
827 dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(state
->samdb
));
829 talloc_free(tmp_ctx
);
830 return ISC_R_NOMEMORY
;
834 * This dance ensures that it is not possible to put
835 * (eg) an extra DC=x, into the DNS name being
839 if (!ldb_dn_add_child_fmt(dn
,
842 talloc_free(tmp_ctx
);
843 return ISC_R_NOMEMORY
;
846 ret
= ldb_dn_set_component(dn
,
850 if (ret
!= LDB_SUCCESS
) {
851 talloc_free(tmp_ctx
);
852 return ISC_R_NOMEMORY
;
856 * Check if this is a plausibly valid DN early
857 * (time spent here will be saved during the
858 * search due to an internal cache)
860 casefold
= ldb_dn_get_casefold(dn
);
862 if (casefold
== NULL
) {
863 talloc_free(tmp_ctx
);
864 return ISC_R_NOTFOUND
;
867 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, "objectClass=dnsZone");
868 if (ret
== LDB_SUCCESS
) {
869 if (zone_dn
!= NULL
) {
870 *zone_dn
= talloc_steal(mem_ctx
, dn
);
872 talloc_free(tmp_ctx
);
873 return ISC_R_SUCCESS
;
878 talloc_free(tmp_ctx
);
879 return ISC_R_NOTFOUND
;
884 return the DN for a name. The record does not need to exist, but the
887 static isc_result_t
b9_find_name_dn(struct dlz_bind9_data
*state
, const char *name
,
888 TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
892 /* work through the name piece by piece, until we find a zone */
895 result
= b9_find_zone_dn(state
, p
, mem_ctx
, dn
);
896 if (result
== ISC_R_SUCCESS
) {
897 const char *casefold
;
899 /* we found a zone, now extend the DN to get
904 ret
= ldb_dn_add_child_fmt(*dn
, "DC=@");
907 return ISC_R_NOMEMORY
;
910 struct ldb_val name_val
911 = data_blob_const(name
,
914 if (!ldb_dn_add_child_val(*dn
,
918 return ISC_R_NOMEMORY
;
923 * Check if this is a plausibly valid DN early
924 * (time spent here will be saved during the
925 * search due to an internal cache)
927 casefold
= ldb_dn_get_casefold(*dn
);
929 if (casefold
== NULL
) {
930 return ISC_R_NOTFOUND
;
933 return ISC_R_SUCCESS
;
941 return ISC_R_NOTFOUND
;
946 see if we handle a given zone
948 _PUBLIC_ isc_result_t
dlz_findzonedb(void *dbdata
, const char *name
,
949 dns_clientinfomethods_t
*methods
,
950 dns_clientinfo_t
*clientinfo
)
952 struct timeval start
= timeval_current();
953 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
954 isc_result_t result
= ISC_R_SUCCESS
;
956 result
= b9_find_zone_dn(state
, name
, NULL
, NULL
);
957 DNS_COMMON_LOG_OPERATION(
958 isc_result_str(result
),
970 static isc_result_t
dlz_lookup_types(struct dlz_bind9_data
*state
,
971 const char *zone
, const char *name
,
972 dns_sdlzlookup_t
*lookup
,
975 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
977 WERROR werr
= WERR_DNS_ERROR_NAME_DOES_NOT_EXIST
;
978 struct dnsp_DnssrvRpcRecord
*records
= NULL
;
979 uint16_t num_records
= 0, i
;
980 struct ldb_val zone_name_val
981 = data_blob_string_const(zone
);
982 struct ldb_val name_val
983 = data_blob_string_const(name
);
985 for (i
=0; zone_prefixes
[i
]; i
++) {
987 const char *casefold
;
988 dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(state
->samdb
));
990 talloc_free(tmp_ctx
);
991 return ISC_R_NOMEMORY
;
995 * This dance ensures that it is not possible to put
996 * (eg) an extra DC=x, into the DNS name being
1000 if (!ldb_dn_add_child_fmt(dn
,
1002 zone_prefixes
[i
])) {
1003 talloc_free(tmp_ctx
);
1004 return ISC_R_NOMEMORY
;
1007 ret
= ldb_dn_set_component(dn
,
1011 if (ret
!= LDB_SUCCESS
) {
1012 talloc_free(tmp_ctx
);
1013 return ISC_R_NOMEMORY
;
1016 ret
= ldb_dn_set_component(dn
,
1020 if (ret
!= LDB_SUCCESS
) {
1021 talloc_free(tmp_ctx
);
1022 return ISC_R_NOMEMORY
;
1026 * Check if this is a plausibly valid DN early
1027 * (time spent here will be saved during the
1028 * search due to an internal cache)
1030 casefold
= ldb_dn_get_casefold(dn
);
1032 if (casefold
== NULL
) {
1033 talloc_free(tmp_ctx
);
1034 return ISC_R_NOTFOUND
;
1037 werr
= dns_common_wildcard_lookup(state
->samdb
, tmp_ctx
, dn
,
1038 &records
, &num_records
);
1039 if (W_ERROR_IS_OK(werr
)) {
1043 if (!W_ERROR_IS_OK(werr
)) {
1044 talloc_free(tmp_ctx
);
1045 return ISC_R_NOTFOUND
;
1048 for (i
=0; i
< num_records
; i
++) {
1049 isc_result_t result
;
1051 result
= b9_putrr(state
, lookup
, &records
[i
], types
);
1052 if (result
!= ISC_R_SUCCESS
) {
1053 talloc_free(tmp_ctx
);
1058 talloc_free(tmp_ctx
);
1059 return ISC_R_SUCCESS
;
1065 _PUBLIC_ isc_result_t
dlz_lookup(const char *zone
, const char *name
,
1066 void *dbdata
, dns_sdlzlookup_t
*lookup
,
1067 dns_clientinfomethods_t
*methods
,
1068 dns_clientinfo_t
*clientinfo
)
1070 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1071 isc_result_t result
= ISC_R_SUCCESS
;
1072 struct timeval start
= timeval_current();
1074 result
= dlz_lookup_types(state
, zone
, name
, lookup
, NULL
);
1075 DNS_COMMON_LOG_OPERATION(
1076 isc_result_str(result
),
1087 see if a zone transfer is allowed
1089 _PUBLIC_ isc_result_t
dlz_allowzonexfr(void *dbdata
, const char *name
, const char *client
)
1091 struct dlz_bind9_data
*state
= talloc_get_type(
1092 dbdata
, struct dlz_bind9_data
);
1094 const char **authorized_clients
, **denied_clients
;
1095 const char *cname
="";
1097 /* check that the zone is known */
1098 ret
= b9_find_zone_dn(state
, name
, NULL
, NULL
);
1099 if (ret
!= ISC_R_SUCCESS
) {
1103 /* default is to deny all transfers */
1105 authorized_clients
= lpcfg_dns_zone_transfer_clients_allow(state
->lp
);
1106 denied_clients
= lpcfg_dns_zone_transfer_clients_deny(state
->lp
);
1108 /* The logic of allow_access() when both allow and deny lists are given
1109 * does not match our expectation here: it would allow clients that are
1110 * neither allowed nor denied.
1111 * Here, we want to deny clients by default.
1112 * Using the allow_access() function is still useful as it takes care of
1113 * parsing IP addresses and subnets in a consistent way with other options
1116 * We will then check the deny list first, then the allow list, so that
1117 * we accept only clients that are explicitly allowed AND not explicitly
1120 if ((authorized_clients
== NULL
) && (denied_clients
== NULL
)) {
1121 /* No "allow" or "deny" lists given. Deny by default. */
1122 return ISC_R_NOPERM
;
1125 if (denied_clients
!= NULL
) {
1126 bool ok
= allow_access(denied_clients
, NULL
, cname
, client
);
1128 /* client on deny list. Deny. */
1129 return ISC_R_NOPERM
;
1133 if (authorized_clients
!= NULL
) {
1134 bool ok
= allow_access(NULL
, authorized_clients
, cname
, client
);
1137 * client is not on deny list and is on allow list.
1138 * This is the only place we should return "allow".
1140 return ISC_R_SUCCESS
;
1143 /* We shouldn't get here, but deny by default. */
1144 return ISC_R_NOPERM
;
1148 perform a zone transfer
1150 _PUBLIC_ isc_result_t
dlz_allnodes(const char *zone
, void *dbdata
,
1151 dns_sdlzallnodes_t
*allnodes
)
1153 struct timeval start
= timeval_current();
1154 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1155 const char *attrs
[] = { "dnsRecord", NULL
};
1156 int ret
= LDB_ERR_NO_SUCH_OBJECT
;
1158 struct ldb_dn
*dn
= NULL
;
1159 struct ldb_result
*res
;
1160 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
1161 struct ldb_val zone_name_val
= data_blob_string_const(zone
);
1162 isc_result_t result
= ISC_R_SUCCESS
;
1164 for (i
=0; zone_prefixes
[i
]; i
++) {
1165 const char *casefold
;
1167 dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(state
->samdb
));
1169 talloc_free(tmp_ctx
);
1170 result
= ISC_R_NOMEMORY
;
1175 * This dance ensures that it is not possible to put
1176 * (eg) an extra DC=x, into the DNS name being
1180 if (!ldb_dn_add_child_fmt(dn
,
1182 zone_prefixes
[i
])) {
1183 talloc_free(tmp_ctx
);
1184 result
= ISC_R_NOMEMORY
;
1188 ret
= ldb_dn_set_component(dn
,
1192 if (ret
!= LDB_SUCCESS
) {
1193 talloc_free(tmp_ctx
);
1194 result
= ISC_R_NOMEMORY
;
1199 * Check if this is a plausibly valid DN early
1200 * (time spent here will be saved during the
1201 * search due to an internal cache)
1203 casefold
= ldb_dn_get_casefold(dn
);
1205 if (casefold
== NULL
) {
1206 result
= ISC_R_NOTFOUND
;
1210 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_SUBTREE
,
1211 attrs
, "objectClass=dnsNode");
1212 if (ret
== LDB_SUCCESS
) {
1216 if (ret
!= LDB_SUCCESS
|| dn
== NULL
) {
1217 talloc_free(tmp_ctx
);
1218 result
= ISC_R_NOTFOUND
;
1222 for (i
=0; i
<res
->count
; i
++) {
1223 struct ldb_message_element
*el
;
1224 TALLOC_CTX
*el_ctx
= talloc_new(tmp_ctx
);
1225 const char *rdn
, *name
;
1226 const struct ldb_val
*v
;
1228 struct dnsp_DnssrvRpcRecord
*recs
= NULL
;
1229 uint16_t num_recs
= 0;
1231 el
= ldb_msg_find_element(res
->msgs
[i
], "dnsRecord");
1232 if (el
== NULL
|| el
->num_values
== 0) {
1233 state
->log(ISC_LOG_INFO
, "failed to find dnsRecord for %s",
1234 ldb_dn_get_linearized(dn
));
1235 talloc_free(el_ctx
);
1239 v
= ldb_dn_get_rdn_val(res
->msgs
[i
]->dn
);
1241 state
->log(ISC_LOG_INFO
, "failed to find RDN for %s",
1242 ldb_dn_get_linearized(dn
));
1243 talloc_free(el_ctx
);
1247 rdn
= talloc_strndup(el_ctx
, (char *)v
->data
, v
->length
);
1249 talloc_free(tmp_ctx
);
1250 result
= ISC_R_NOMEMORY
;
1254 if (strcmp(rdn
, "@") == 0) {
1257 name
= talloc_asprintf(el_ctx
, "%s.%s", rdn
, zone
);
1259 name
= b9_format_fqdn(el_ctx
, name
);
1261 talloc_free(tmp_ctx
);
1262 result
= ISC_R_NOMEMORY
;
1266 werr
= dns_common_extract(state
->samdb
, el
, el_ctx
, &recs
, &num_recs
);
1267 if (!W_ERROR_IS_OK(werr
)) {
1268 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse dnsRecord for %s, %s",
1269 ldb_dn_get_linearized(dn
), win_errstr(werr
));
1270 talloc_free(el_ctx
);
1274 for (j
=0; j
< num_recs
; j
++) {
1277 rc
= b9_putnamedrr(state
, allnodes
, name
, &recs
[j
]);
1278 if (rc
!= ISC_R_SUCCESS
) {
1283 talloc_free(el_ctx
);
1286 talloc_free(tmp_ctx
);
1288 DNS_COMMON_LOG_OPERATION(
1289 isc_result_str(result
),
1301 _PUBLIC_ isc_result_t
dlz_newversion(const char *zone
, void *dbdata
, void **versionp
)
1303 struct timeval start
= timeval_current();
1304 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1305 isc_result_t result
= ISC_R_SUCCESS
;
1307 state
->log(ISC_LOG_INFO
, "samba_dlz: starting transaction on zone %s", zone
);
1309 if (state
->transaction_token
!= NULL
) {
1310 state
->log(ISC_LOG_INFO
, "samba_dlz: transaction already started for zone %s", zone
);
1311 result
= ISC_R_FAILURE
;
1315 state
->transaction_token
= talloc_zero(state
, int);
1316 if (state
->transaction_token
== NULL
) {
1317 result
= ISC_R_NOMEMORY
;
1321 if (ldb_transaction_start(state
->samdb
) != LDB_SUCCESS
) {
1322 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to start a transaction for zone %s", zone
);
1323 talloc_free(state
->transaction_token
);
1324 state
->transaction_token
= NULL
;
1325 result
= ISC_R_FAILURE
;
1329 *versionp
= (void *)state
->transaction_token
;
1331 DNS_COMMON_LOG_OPERATION(
1332 isc_result_str(result
),
1343 _PUBLIC_
void dlz_closeversion(const char *zone
, isc_boolean_t commit
,
1344 void *dbdata
, void **versionp
)
1346 struct timeval start
= timeval_current();
1347 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1348 const char *data
= NULL
;
1350 data
= commit
? "commit" : "cancel";
1352 if (state
->transaction_token
!= (int *)*versionp
) {
1353 state
->log(ISC_LOG_INFO
, "samba_dlz: transaction not started for zone %s", zone
);
1358 if (ldb_transaction_commit(state
->samdb
) != LDB_SUCCESS
) {
1359 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to commit a transaction for zone %s", zone
);
1362 state
->log(ISC_LOG_INFO
, "samba_dlz: committed transaction on zone %s", zone
);
1364 if (ldb_transaction_cancel(state
->samdb
) != LDB_SUCCESS
) {
1365 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to cancel a transaction for zone %s", zone
);
1368 state
->log(ISC_LOG_INFO
, "samba_dlz: cancelling transaction on zone %s", zone
);
1371 talloc_free(state
->transaction_token
);
1372 state
->transaction_token
= NULL
;
1376 DNS_COMMON_LOG_OPERATION(
1377 isc_result_str(ISC_R_SUCCESS
),
1386 see if there is a SOA record for a zone
1388 static bool b9_has_soa(struct dlz_bind9_data
*state
, struct ldb_dn
*dn
, const char *zone
)
1390 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
1392 struct dnsp_DnssrvRpcRecord
*records
= NULL
;
1393 uint16_t num_records
= 0, i
;
1394 struct ldb_val zone_name_val
1395 = data_blob_string_const(zone
);
1398 * This dance ensures that it is not possible to put
1399 * (eg) an extra DC=x, into the DNS name being
1403 if (!ldb_dn_add_child_val(dn
,
1406 talloc_free(tmp_ctx
);
1411 * The SOA record is always stored under DC=@,DC=zonename
1412 * This can probably be removed when dns_common_lookup makes a fallback
1413 * lookup on @ pseudo record
1416 if (!ldb_dn_add_child_fmt(dn
,"DC=@")) {
1417 talloc_free(tmp_ctx
);
1421 werr
= dns_common_lookup(state
->samdb
, tmp_ctx
, dn
,
1422 &records
, &num_records
, NULL
);
1423 if (!W_ERROR_IS_OK(werr
)) {
1424 talloc_free(tmp_ctx
);
1428 for (i
=0; i
< num_records
; i
++) {
1429 if (records
[i
].wType
== DNS_TYPE_SOA
) {
1430 talloc_free(tmp_ctx
);
1435 talloc_free(tmp_ctx
);
1439 static bool b9_zone_add(struct dlz_bind9_data
*state
, const char *name
)
1441 struct b9_zone
*zone
;
1443 zone
= talloc_zero(state
, struct b9_zone
);
1448 zone
->name
= talloc_strdup(zone
, name
);
1449 if (zone
->name
== NULL
) {
1454 DLIST_ADD(state
->zonelist
, zone
);
1458 static bool b9_zone_exists(struct dlz_bind9_data
*state
, const char *name
)
1460 struct b9_zone
*zone
= state
->zonelist
;
1463 while (zone
!= NULL
) {
1464 if (strcasecmp(name
, zone
->name
) == 0) {
1476 configure a writeable zone
1478 _PUBLIC_ isc_result_t
dlz_configure(dns_view_t
*view
, dns_dlzdb_t
*dlzdb
,
1481 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1482 TALLOC_CTX
*tmp_ctx
;
1486 state
->log(ISC_LOG_INFO
, "samba_dlz: starting configure");
1487 if (state
->writeable_zone
== NULL
) {
1488 state
->log(ISC_LOG_INFO
, "samba_dlz: no writeable_zone method available");
1489 return ISC_R_FAILURE
;
1492 tmp_ctx
= talloc_new(state
);
1494 for (i
=0; zone_prefixes
[i
]; i
++) {
1495 const char *attrs
[] = { "name", NULL
};
1497 struct ldb_result
*res
;
1499 dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(state
->samdb
));
1501 talloc_free(tmp_ctx
);
1502 return ISC_R_NOMEMORY
;
1505 if (!ldb_dn_add_child_fmt(dn
, "%s", zone_prefixes
[i
])) {
1506 talloc_free(tmp_ctx
);
1507 return ISC_R_NOMEMORY
;
1510 ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_SUBTREE
,
1511 attrs
, "objectClass=dnsZone");
1512 if (ret
!= LDB_SUCCESS
) {
1516 for (j
=0; j
<res
->count
; j
++) {
1517 isc_result_t result
;
1518 const char *zone
= ldb_msg_find_attr_as_string(res
->msgs
[j
], "name", NULL
);
1519 struct ldb_dn
*zone_dn
;
1524 /* Ignore zones that are not handled in BIND */
1525 if ((strcmp(zone
, "RootDNSServers") == 0) ||
1526 (strcmp(zone
, "..TrustAnchors") == 0)) {
1529 zone_dn
= ldb_dn_copy(tmp_ctx
, dn
);
1530 if (zone_dn
== NULL
) {
1531 talloc_free(tmp_ctx
);
1532 return ISC_R_NOMEMORY
;
1535 if (!b9_has_soa(state
, zone_dn
, zone
)) {
1539 if (b9_zone_exists(state
, zone
)) {
1540 state
->log(ISC_LOG_WARNING
, "samba_dlz: Ignoring duplicate zone '%s' from '%s'",
1541 zone
, ldb_dn_get_linearized(zone_dn
));
1545 if (!b9_zone_add(state
, zone
)) {
1546 talloc_free(tmp_ctx
);
1547 return ISC_R_NOMEMORY
;
1550 result
= state
->writeable_zone(view
, dlzdb
, zone
);
1551 if (result
!= ISC_R_SUCCESS
) {
1552 state
->log(ISC_LOG_ERROR
, "samba_dlz: Failed to configure zone '%s'",
1554 talloc_free(tmp_ctx
);
1557 state
->log(ISC_LOG_INFO
, "samba_dlz: configured writeable zone '%s'", zone
);
1561 talloc_free(tmp_ctx
);
1562 return ISC_R_SUCCESS
;
1566 authorize a zone update
1568 _PUBLIC_ isc_boolean_t
dlz_ssumatch(const char *signer
, const char *name
, const char *tcpaddr
,
1569 const char *type
, const char *key
, uint32_t keydatalen
, uint8_t *keydata
,
1572 struct timeval start
= timeval_current();
1573 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1574 TALLOC_CTX
*tmp_ctx
;
1576 struct cli_credentials
*server_credentials
;
1578 char *keytab_file
= NULL
;
1582 struct gensec_security
*gensec_ctx
;
1583 struct auth_session_info
*session_info
;
1586 struct ldb_result
*res
;
1587 const char * attrs
[] = { NULL
};
1588 uint32_t access_mask
;
1589 struct gensec_settings
*settings
= NULL
;
1590 const struct gensec_security_ops
**backends
= NULL
;
1592 isc_boolean_t result
= ISC_FALSE
;
1596 /* Remove cached credentials, if any */
1597 if (state
->session_info
) {
1598 talloc_free(state
->session_info
);
1599 state
->session_info
= NULL
;
1601 if (state
->update_name
) {
1602 talloc_free(state
->update_name
);
1603 state
->update_name
= NULL
;
1606 tmp_ctx
= talloc_new(state
);
1607 if (tmp_ctx
== NULL
) {
1608 state
->log(ISC_LOG_ERROR
, "samba_dlz: no memory");
1613 ap_req
= data_blob_const(keydata
, keydatalen
);
1614 server_credentials
= cli_credentials_init(tmp_ctx
);
1615 if (!server_credentials
) {
1616 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to init server credentials");
1617 talloc_free(tmp_ctx
);
1622 status
= cli_credentials_set_krb5_context(server_credentials
,
1623 state
->smb_krb5_ctx
);
1624 if (!NT_STATUS_IS_OK(status
)) {
1625 state
->log(ISC_LOG_ERROR
,
1626 "samba_dlz: failed to set krb5 context");
1627 talloc_free(tmp_ctx
);
1632 ok
= cli_credentials_set_conf(server_credentials
, state
->lp
);
1634 state
->log(ISC_LOG_ERROR
,
1635 "samba_dlz: failed to load smb.conf");
1636 talloc_free(tmp_ctx
);
1641 keytab_file
= talloc_asprintf(tmp_ctx
,
1643 lpcfg_binddns_dir(state
->lp
));
1644 if (keytab_file
== NULL
) {
1645 state
->log(ISC_LOG_ERROR
, "samba_dlz: Out of memory!");
1646 talloc_free(tmp_ctx
);
1651 if (!file_exist(keytab_file
)) {
1652 keytab_file
= talloc_asprintf(tmp_ctx
,
1654 lpcfg_private_dir(state
->lp
));
1655 if (keytab_file
== NULL
) {
1656 state
->log(ISC_LOG_ERROR
, "samba_dlz: Out of memory!");
1657 talloc_free(tmp_ctx
);
1663 keytab_name
= talloc_asprintf(tmp_ctx
, "FILE:%s", keytab_file
);
1664 if (keytab_name
== NULL
) {
1665 state
->log(ISC_LOG_ERROR
, "samba_dlz: Out of memory!");
1666 talloc_free(tmp_ctx
);
1671 ret
= cli_credentials_set_keytab_name(server_credentials
, state
->lp
, keytab_name
,
1674 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to obtain server credentials from %s",
1676 talloc_free(tmp_ctx
);
1680 talloc_free(keytab_name
);
1682 settings
= lpcfg_gensec_settings(tmp_ctx
, state
->lp
);
1683 if (settings
== NULL
) {
1684 state
->log(ISC_LOG_ERROR
, "samba_dlz: lpcfg_gensec_settings failed");
1685 talloc_free(tmp_ctx
);
1689 backends
= talloc_zero_array(settings
,
1690 const struct gensec_security_ops
*, 3);
1691 if (backends
== NULL
) {
1692 state
->log(ISC_LOG_ERROR
, "samba_dlz: talloc_zero_array gensec_security_ops failed");
1693 talloc_free(tmp_ctx
);
1697 settings
->backends
= backends
;
1701 backends
[idx
++] = gensec_security_by_oid(NULL
, GENSEC_OID_KERBEROS5
);
1702 backends
[idx
++] = gensec_security_by_oid(NULL
, GENSEC_OID_SPNEGO
);
1704 nt_status
= gensec_server_start(tmp_ctx
, settings
,
1705 state
->auth_context
, &gensec_ctx
);
1706 if (!NT_STATUS_IS_OK(nt_status
)) {
1707 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to start gensec server");
1708 talloc_free(tmp_ctx
);
1713 gensec_set_credentials(gensec_ctx
, server_credentials
);
1715 nt_status
= gensec_start_mech_by_oid(gensec_ctx
, GENSEC_OID_SPNEGO
);
1716 if (!NT_STATUS_IS_OK(nt_status
)) {
1717 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to start spnego");
1718 talloc_free(tmp_ctx
);
1724 * We only allow SPNEGO/KRB5 and make sure the backend
1725 * to is RPC/IPC free.
1727 * See gensec_gssapi_update_internal() as
1730 * It allows gensec_update() not to block.
1732 * If that changes in future we need to use
1733 * gensec_update_send/recv here!
1735 nt_status
= gensec_update(gensec_ctx
, tmp_ctx
, ap_req
, &ap_req
);
1736 if (!NT_STATUS_IS_OK(nt_status
)) {
1737 state
->log(ISC_LOG_ERROR
, "samba_dlz: spnego update failed");
1738 talloc_free(tmp_ctx
);
1743 nt_status
= gensec_session_info(gensec_ctx
, tmp_ctx
, &session_info
);
1744 if (!NT_STATUS_IS_OK(nt_status
)) {
1745 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to create session info");
1746 talloc_free(tmp_ctx
);
1751 /* Get the DN from name */
1752 rc
= b9_find_name_dn(state
, name
, tmp_ctx
, &dn
);
1753 if (rc
!= ISC_R_SUCCESS
) {
1754 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to find name %s", name
);
1755 talloc_free(tmp_ctx
);
1760 /* make sure the dn exists, or find parent dn in case new object is being added */
1761 ldb_ret
= ldb_search(state
->samdb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
,
1762 attrs
, "objectClass=dnsNode");
1763 if (ldb_ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1764 ldb_dn_remove_child_components(dn
, 1);
1765 access_mask
= SEC_ADS_CREATE_CHILD
;
1767 } else if (ldb_ret
== LDB_SUCCESS
) {
1768 access_mask
= SEC_STD_REQUIRED
| SEC_ADS_SELF_WRITE
;
1771 talloc_free(tmp_ctx
);
1777 ldb_ret
= dsdb_check_access_on_dn(state
->samdb
, tmp_ctx
, dn
,
1778 session_info
->security_token
,
1780 if (ldb_ret
!= LDB_SUCCESS
) {
1781 state
->log(ISC_LOG_INFO
,
1782 "samba_dlz: disallowing update of signer=%s name=%s type=%s error=%s",
1783 signer
, name
, type
, ldb_strerror(ldb_ret
));
1784 talloc_free(tmp_ctx
);
1789 /* Cache session_info, so it can be used in the actual add/delete operation */
1790 state
->update_name
= talloc_strdup(state
, name
);
1791 if (state
->update_name
== NULL
) {
1792 state
->log(ISC_LOG_ERROR
, "samba_dlz: memory allocation error");
1793 talloc_free(tmp_ctx
);
1797 state
->session_info
= talloc_steal(state
, session_info
);
1799 state
->log(ISC_LOG_INFO
, "samba_dlz: allowing update of signer=%s name=%s tcpaddr=%s type=%s key=%s",
1800 signer
, name
, tcpaddr
, type
, key
);
1802 talloc_free(tmp_ctx
);
1805 DNS_COMMON_LOG_OPERATION(
1806 isc_result_str(result
),
1816 see if two dns records match
1818 static bool b9_record_match(struct dnsp_DnssrvRpcRecord
*rec1
,
1819 struct dnsp_DnssrvRpcRecord
*rec2
)
1821 if (rec1
->wType
!= rec2
->wType
) {
1824 /* see if this type is single valued */
1825 if (b9_single_valued(rec1
->wType
)) {
1829 return dns_record_match(rec1
, rec2
);
1833 * Update session_info on samdb using the cached credentials
1835 static bool b9_set_session_info(struct dlz_bind9_data
*state
, const char *name
)
1839 if (state
->update_name
== NULL
|| state
->session_info
== NULL
) {
1840 state
->log(ISC_LOG_ERROR
, "samba_dlz: invalid credentials");
1844 /* Do not use client credentials, if we're not updating the client specified name */
1845 if (strcmp(state
->update_name
, name
) != 0) {
1849 ret
= ldb_set_opaque(
1852 state
->session_info
);
1853 if (ret
!= LDB_SUCCESS
) {
1854 state
->log(ISC_LOG_ERROR
, "samba_dlz: unable to set session info");
1862 * Reset session_info on samdb as system session
1864 static void b9_reset_session_info(struct dlz_bind9_data
*state
)
1869 system_session(state
->lp
));
1873 add or modify a rdataset
1875 _PUBLIC_ isc_result_t
dlz_addrdataset(const char *name
, const char *rdatastr
, void *dbdata
, void *version
)
1877 struct timeval start
= timeval_current();
1878 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
1879 struct dnsp_DnssrvRpcRecord
*rec
;
1881 isc_result_t result
= ISC_R_SUCCESS
;
1882 bool tombstoned
= false;
1883 bool needs_add
= false;
1884 struct dnsp_DnssrvRpcRecord
*recs
= NULL
;
1885 uint16_t num_recs
= 0;
1890 if (state
->transaction_token
!= (void*)version
) {
1891 state
->log(ISC_LOG_INFO
, "samba_dlz: bad transaction version");
1892 result
= ISC_R_FAILURE
;
1896 rec
= talloc_zero(state
, struct dnsp_DnssrvRpcRecord
);
1898 result
= ISC_R_NOMEMORY
;
1902 rec
->rank
= DNS_RANK_ZONE
;
1904 if (!b9_parse(state
, rdatastr
, rec
)) {
1905 state
->log(ISC_LOG_INFO
, "samba_dlz: failed to parse rdataset '%s'", rdatastr
);
1907 result
= ISC_R_FAILURE
;
1911 /* find the DN of the record */
1912 result
= b9_find_name_dn(state
, name
, rec
, &dn
);
1913 if (result
!= ISC_R_SUCCESS
) {
1918 /* get any existing records */
1919 werr
= dns_common_lookup(state
->samdb
, rec
, dn
,
1920 &recs
, &num_recs
, &tombstoned
);
1921 if (W_ERROR_EQUAL(werr
, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST
)) {
1925 if (!W_ERROR_IS_OK(werr
)) {
1926 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse dnsRecord for %s, %s",
1927 ldb_dn_get_linearized(dn
), win_errstr(werr
));
1929 result
= ISC_R_FAILURE
;
1935 * we need to keep the existing tombstone record
1941 /* there may be existing records. We need to see if this will
1942 * replace a record or add to it
1944 for (i
=first
; i
< num_recs
; i
++) {
1945 if (b9_record_match(rec
, &recs
[i
])) {
1949 if (i
== UINT16_MAX
) {
1950 state
->log(ISC_LOG_ERROR
,
1951 "samba_dlz: failed to find record to modify, and "
1952 "there are already %u dnsRecord values for %s",
1953 i
, ldb_dn_get_linearized(dn
));
1955 result
= ISC_R_FAILURE
;
1959 if (i
== num_recs
) {
1960 /* set dwTimeStamp before increasing num_recs */
1961 if (dns_name_is_static(recs
, num_recs
)) {
1962 rec
->dwTimeStamp
= 0;
1964 rec
->dwTimeStamp
= unix_to_dns_timestamp(time(NULL
));
1966 /* adding space for a new value */
1967 recs
= talloc_realloc(rec
, recs
,
1968 struct dnsp_DnssrvRpcRecord
,
1972 result
= ISC_R_NOMEMORY
;
1978 * We are updating a record. Depending on whether aging is
1979 * enabled, and how old the old timestamp is,
1980 * dns_common_replace() will work out whether to bump the
1981 * timestamp or not. But to do that, we need to tell it the
1984 if (! dns_name_is_static(recs
, num_recs
)) {
1985 rec
->dwTimeStamp
= recs
[i
].dwTimeStamp
;
1991 if (!b9_set_session_info(state
, name
)) {
1993 result
= ISC_R_FAILURE
;
1997 /* modify the record */
1998 werr
= dns_common_replace(state
->samdb
, rec
, dn
,
2002 b9_reset_session_info(state
);
2003 if (!W_ERROR_IS_OK(werr
)) {
2004 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to %s %s - %s",
2005 needs_add
? "add" : "modify",
2006 ldb_dn_get_linearized(dn
), win_errstr(werr
));
2008 result
= ISC_R_FAILURE
;
2012 state
->log(ISC_LOG_INFO
, "samba_dlz: added rdataset %s '%s'", name
, rdatastr
);
2016 DNS_COMMON_LOG_OPERATION(
2017 isc_result_str(result
),
2028 _PUBLIC_ isc_result_t
dlz_subrdataset(const char *name
, const char *rdatastr
, void *dbdata
, void *version
)
2030 struct timeval start
= timeval_current();
2031 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
2032 struct dnsp_DnssrvRpcRecord
*rec
;
2034 isc_result_t result
= ISC_R_SUCCESS
;
2035 struct dnsp_DnssrvRpcRecord
*recs
= NULL
;
2036 uint16_t num_recs
= 0;
2040 if (state
->transaction_token
!= (void*)version
) {
2041 state
->log(ISC_LOG_ERROR
, "samba_dlz: bad transaction version");
2042 result
= ISC_R_FAILURE
;
2046 rec
= talloc_zero(state
, struct dnsp_DnssrvRpcRecord
);
2048 result
= ISC_R_NOMEMORY
;
2052 if (!b9_parse(state
, rdatastr
, rec
)) {
2053 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to parse rdataset '%s'", rdatastr
);
2055 result
= ISC_R_FAILURE
;
2059 /* find the DN of the record */
2060 result
= b9_find_name_dn(state
, name
, rec
, &dn
);
2061 if (result
!= ISC_R_SUCCESS
) {
2066 /* get the existing records */
2067 werr
= dns_common_lookup(state
->samdb
, rec
, dn
,
2068 &recs
, &num_recs
, NULL
);
2069 if (!W_ERROR_IS_OK(werr
)) {
2071 result
= ISC_R_NOTFOUND
;
2075 for (i
=0; i
< num_recs
; i
++) {
2076 if (b9_record_match(rec
, &recs
[i
])) {
2077 recs
[i
] = (struct dnsp_DnssrvRpcRecord
) {
2078 .wType
= DNS_TYPE_TOMBSTONE
,
2083 if (i
== num_recs
) {
2085 result
= ISC_R_NOTFOUND
;
2089 if (!b9_set_session_info(state
, name
)) {
2091 result
= ISC_R_FAILURE
;
2095 /* modify the record */
2096 werr
= dns_common_replace(state
->samdb
, rec
, dn
,
2097 false,/* needs_add */
2100 b9_reset_session_info(state
);
2101 if (!W_ERROR_IS_OK(werr
)) {
2102 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to modify %s - %s",
2103 ldb_dn_get_linearized(dn
), win_errstr(werr
));
2105 result
= ISC_R_FAILURE
;
2109 state
->log(ISC_LOG_INFO
, "samba_dlz: subtracted rdataset %s '%s'", name
, rdatastr
);
2113 DNS_COMMON_LOG_OPERATION(
2114 isc_result_str(result
),
2124 delete all records of the given type
2126 _PUBLIC_ isc_result_t
dlz_delrdataset(const char *name
, const char *type
, void *dbdata
, void *version
)
2128 struct timeval start
= timeval_current();
2129 struct dlz_bind9_data
*state
= talloc_get_type_abort(dbdata
, struct dlz_bind9_data
);
2130 TALLOC_CTX
*tmp_ctx
;
2132 isc_result_t result
= ISC_R_SUCCESS
;
2133 enum dns_record_type dns_type
;
2135 struct dnsp_DnssrvRpcRecord
*recs
= NULL
;
2136 uint16_t num_recs
= 0;
2140 if (state
->transaction_token
!= (void*)version
) {
2141 state
->log(ISC_LOG_ERROR
, "samba_dlz: bad transaction version");
2142 result
= ISC_R_FAILURE
;
2146 if (!b9_dns_type(type
, &dns_type
)) {
2147 state
->log(ISC_LOG_ERROR
, "samba_dlz: bad dns type %s in delete", type
);
2148 result
= ISC_R_FAILURE
;
2152 tmp_ctx
= talloc_new(state
);
2154 /* find the DN of the record */
2155 result
= b9_find_name_dn(state
, name
, tmp_ctx
, &dn
);
2156 if (result
!= ISC_R_SUCCESS
) {
2157 talloc_free(tmp_ctx
);
2161 /* get the existing records */
2162 werr
= dns_common_lookup(state
->samdb
, tmp_ctx
, dn
,
2163 &recs
, &num_recs
, NULL
);
2164 if (!W_ERROR_IS_OK(werr
)) {
2165 talloc_free(tmp_ctx
);
2166 result
= ISC_R_NOTFOUND
;
2170 for (ri
=0; ri
< num_recs
; ri
++) {
2171 if (dns_type
!= recs
[ri
].wType
) {
2176 recs
[ri
] = (struct dnsp_DnssrvRpcRecord
) {
2177 .wType
= DNS_TYPE_TOMBSTONE
,
2182 talloc_free(tmp_ctx
);
2183 result
= ISC_R_FAILURE
;
2187 if (!b9_set_session_info(state
, name
)) {
2188 talloc_free(tmp_ctx
);
2189 result
= ISC_R_FAILURE
;
2193 /* modify the record */
2194 werr
= dns_common_replace(state
->samdb
, tmp_ctx
, dn
,
2195 false,/* needs_add */
2198 b9_reset_session_info(state
);
2199 if (!W_ERROR_IS_OK(werr
)) {
2200 state
->log(ISC_LOG_ERROR
, "samba_dlz: failed to modify %s - %s",
2201 ldb_dn_get_linearized(dn
), win_errstr(werr
));
2202 talloc_free(tmp_ctx
);
2203 result
= ISC_R_FAILURE
;
2207 state
->log(ISC_LOG_INFO
, "samba_dlz: deleted rdataset %s of type %s", name
, type
);
2209 talloc_free(tmp_ctx
);
2211 DNS_COMMON_LOG_OPERATION(
2212 isc_result_str(result
),