waf: Fix com_err detection with MIT krb5.
[Samba/id10ts.git] / source4 / dns_server / dlz_bind9.c
blob224f242b879d0b5da7024e6247c87bdd1dabab4a
1 /*
2 Unix SMB/CIFS implementation.
4 bind9 dlz driver for Samba
6 Copyright (C) 2010 Andrew Tridgell
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "talloc.h"
24 #include "param/param.h"
25 #include "lib/events/events.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "dsdb/common/util.h"
28 #include "auth/auth.h"
29 #include "auth/session.h"
30 #include "auth/gensec/gensec.h"
31 #include "librpc/gen_ndr/security.h"
32 #include "auth/credentials/credentials.h"
33 #include "system/kerberos.h"
34 #include "auth/kerberos/kerberos.h"
35 #include "gen_ndr/ndr_dnsp.h"
36 #include "gen_ndr/server_id.h"
37 #include "messaging/messaging.h"
38 #include "lib/cmdline/popt_common.h"
39 #include "dlz_minimal.h"
42 struct b9_options {
43 const char *url;
44 const char *debug;
47 struct dlz_bind9_data {
48 struct b9_options options;
49 struct ldb_context *samdb;
50 struct tevent_context *ev_ctx;
51 struct loadparm_context *lp;
52 int *transaction_token;
53 uint32_t soa_serial;
55 /* Used for dynamic update */
56 struct smb_krb5_context *smb_krb5_ctx;
57 struct auth4_context *auth_context;
58 struct auth_session_info *session_info;
59 char *update_name;
61 /* helper functions from the dlz_dlopen driver */
62 log_t *log;
63 dns_sdlz_putrr_t *putrr;
64 dns_sdlz_putnamedrr_t *putnamedrr;
65 dns_dlz_writeablezone_t *writeable_zone;
69 static const char *zone_prefixes[] = {
70 "CN=MicrosoftDNS,DC=DomainDnsZones",
71 "CN=MicrosoftDNS,DC=ForestDnsZones",
72 "CN=MicrosoftDNS,CN=System",
73 NULL
77 return the version of the API
79 _PUBLIC_ int dlz_version(unsigned int *flags)
81 return DLZ_DLOPEN_VERSION;
85 remember a helper function from the bind9 dlz_dlopen driver
87 static void b9_add_helper(struct dlz_bind9_data *state, const char *helper_name, void *ptr)
89 if (strcmp(helper_name, "log") == 0) {
90 state->log = ptr;
92 if (strcmp(helper_name, "putrr") == 0) {
93 state->putrr = ptr;
95 if (strcmp(helper_name, "putnamedrr") == 0) {
96 state->putnamedrr = ptr;
98 if (strcmp(helper_name, "writeable_zone") == 0) {
99 state->writeable_zone = ptr;
104 format a record for bind9
106 static bool b9_format(struct dlz_bind9_data *state,
107 TALLOC_CTX *mem_ctx,
108 struct dnsp_DnssrvRpcRecord *rec,
109 const char **type, const char **data)
111 uint32_t i;
112 char *tmp;
114 switch (rec->wType) {
115 case DNS_TYPE_A:
116 *type = "a";
117 *data = rec->data.ipv4;
118 break;
120 case DNS_TYPE_AAAA:
121 *type = "aaaa";
122 *data = rec->data.ipv6;
123 break;
125 case DNS_TYPE_CNAME:
126 *type = "cname";
127 *data = rec->data.cname;
128 break;
130 case DNS_TYPE_TXT:
131 *type = "txt";
132 tmp = talloc_asprintf(mem_ctx, "\"%s\"", rec->data.txt.str[0]);
133 for (i=1; i<rec->data.txt.count; i++) {
134 tmp = talloc_asprintf_append(tmp, " \"%s\"", rec->data.txt.str[i]);
136 *data = tmp;
137 break;
139 case DNS_TYPE_PTR:
140 *type = "ptr";
141 *data = rec->data.ptr;
142 break;
144 case DNS_TYPE_SRV:
145 *type = "srv";
146 *data = talloc_asprintf(mem_ctx, "%u %u %u %s",
147 rec->data.srv.wPriority,
148 rec->data.srv.wWeight,
149 rec->data.srv.wPort,
150 rec->data.srv.nameTarget);
151 break;
153 case DNS_TYPE_MX:
154 *type = "mx";
155 *data = talloc_asprintf(mem_ctx, "%u %s",
156 rec->data.mx.wPriority,
157 rec->data.mx.nameTarget);
158 break;
160 case DNS_TYPE_HINFO:
161 *type = "hinfo";
162 *data = talloc_asprintf(mem_ctx, "%s %s",
163 rec->data.hinfo.cpu,
164 rec->data.hinfo.os);
165 break;
167 case DNS_TYPE_NS:
168 *type = "ns";
169 *data = rec->data.ns;
170 break;
172 case DNS_TYPE_SOA: {
173 const char *mname;
174 *type = "soa";
176 /* we need to fake the authoritative nameserver to
177 * point at ourselves. This is how AD DNS servers
178 * force clients to send updates to the right local DC
180 mname = talloc_asprintf(mem_ctx, "%s.%s",
181 lpcfg_netbios_name(state->lp), lpcfg_dnsdomain(state->lp));
182 if (mname == NULL) {
183 return false;
185 mname = strlower_talloc(mem_ctx, mname);
186 if (mname == NULL) {
187 return false;
190 state->soa_serial = rec->data.soa.serial;
192 *data = talloc_asprintf(mem_ctx, "%s %s %u %u %u %u %u",
193 mname,
194 rec->data.soa.rname,
195 rec->data.soa.serial,
196 rec->data.soa.refresh,
197 rec->data.soa.retry,
198 rec->data.soa.expire,
199 rec->data.soa.minimum);
200 break;
203 default:
204 state->log(ISC_LOG_ERROR, "samba b9_putrr: unhandled record type %u",
205 rec->wType);
206 return false;
209 return true;
212 static const struct {
213 enum dns_record_type dns_type;
214 const char *typestr;
215 bool single_valued;
216 } dns_typemap[] = {
217 { DNS_TYPE_A, "A" , false},
218 { DNS_TYPE_AAAA, "AAAA" , false},
219 { DNS_TYPE_CNAME, "CNAME" , true},
220 { DNS_TYPE_TXT, "TXT" , false},
221 { DNS_TYPE_PTR, "PTR" , false},
222 { DNS_TYPE_SRV, "SRV" , false},
223 { DNS_TYPE_MX, "MX" , false},
224 { DNS_TYPE_HINFO, "HINFO" , false},
225 { DNS_TYPE_NS, "NS" , false},
226 { DNS_TYPE_SOA, "SOA" , true},
231 see if a DNS type is single valued
233 static bool b9_single_valued(enum dns_record_type dns_type)
235 int i;
236 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
237 if (dns_typemap[i].dns_type == dns_type) {
238 return dns_typemap[i].single_valued;
241 return false;
245 see if a DNS type is single valued
247 static bool b9_dns_type(const char *type, enum dns_record_type *dtype)
249 int i;
250 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
251 if (strcasecmp(dns_typemap[i].typestr, type) == 0) {
252 *dtype = dns_typemap[i].dns_type;
253 return true;
256 return false;
260 #define DNS_PARSE_STR(ret, str, sep, saveptr) do { \
261 (ret) = strtok_r(str, sep, &saveptr); \
262 if ((ret) == NULL) return false; \
263 } while (0)
265 #define DNS_PARSE_UINT(ret, str, sep, saveptr) do { \
266 char *istr = strtok_r(str, sep, &saveptr); \
267 if ((istr) == NULL) return false; \
268 (ret) = strtoul(istr, NULL, 10); \
269 } while (0)
272 parse a record from bind9
274 static bool b9_parse(struct dlz_bind9_data *state,
275 const char *rdatastr,
276 struct dnsp_DnssrvRpcRecord *rec)
278 char *full_name, *dclass, *type;
279 char *str, *tmp, *saveptr=NULL;
280 int i;
282 str = talloc_strdup(rec, rdatastr);
283 if (str == NULL) {
284 return false;
287 /* parse the SDLZ string form */
288 DNS_PARSE_STR(full_name, str, "\t", saveptr);
289 DNS_PARSE_UINT(rec->dwTtlSeconds, NULL, "\t", saveptr);
290 DNS_PARSE_STR(dclass, NULL, "\t", saveptr);
291 DNS_PARSE_STR(type, NULL, "\t", saveptr);
293 /* construct the record */
294 for (i=0; i<ARRAY_SIZE(dns_typemap); i++) {
295 if (strcasecmp(type, dns_typemap[i].typestr) == 0) {
296 rec->wType = dns_typemap[i].dns_type;
297 break;
300 if (i == ARRAY_SIZE(dns_typemap)) {
301 state->log(ISC_LOG_ERROR, "samba_dlz: unsupported record type '%s' for '%s'",
302 type, full_name);
303 return false;
306 switch (rec->wType) {
307 case DNS_TYPE_A:
308 DNS_PARSE_STR(rec->data.ipv4, NULL, " ", saveptr);
309 break;
311 case DNS_TYPE_AAAA:
312 DNS_PARSE_STR(rec->data.ipv6, NULL, " ", saveptr);
313 break;
315 case DNS_TYPE_CNAME:
316 DNS_PARSE_STR(rec->data.cname, NULL, " ", saveptr);
317 break;
319 case DNS_TYPE_TXT:
320 rec->data.txt.count = 0;
321 rec->data.txt.str = talloc_array(rec, const char *, rec->data.txt.count);
322 tmp = strtok_r(NULL, "\t", &saveptr);
323 while (tmp) {
324 rec->data.txt.str = talloc_realloc(rec, rec->data.txt.str, const char *,
325 rec->data.txt.count+1);
326 if (tmp[0] == '"') {
327 /* Strip quotes */
328 rec->data.txt.str[rec->data.txt.count] = talloc_strndup(rec, &tmp[1], strlen(tmp)-2);
329 } else {
330 rec->data.txt.str[rec->data.txt.count] = talloc_strdup(rec, tmp);
332 rec->data.txt.count++;
333 tmp = strtok_r(NULL, " ", &saveptr);
335 break;
337 case DNS_TYPE_PTR:
338 DNS_PARSE_STR(rec->data.ptr, NULL, " ", saveptr);
339 break;
341 case DNS_TYPE_SRV:
342 DNS_PARSE_UINT(rec->data.srv.wPriority, NULL, " ", saveptr);
343 DNS_PARSE_UINT(rec->data.srv.wWeight, NULL, " ", saveptr);
344 DNS_PARSE_UINT(rec->data.srv.wPort, NULL, " ", saveptr);
345 DNS_PARSE_STR(rec->data.srv.nameTarget, NULL, " ", saveptr);
346 break;
348 case DNS_TYPE_MX:
349 DNS_PARSE_UINT(rec->data.mx.wPriority, NULL, " ", saveptr);
350 DNS_PARSE_STR(rec->data.mx.nameTarget, NULL, " ", saveptr);
351 break;
353 case DNS_TYPE_HINFO:
354 DNS_PARSE_STR(rec->data.hinfo.cpu, NULL, " ", saveptr);
355 DNS_PARSE_STR(rec->data.hinfo.os, NULL, " ", saveptr);
356 break;
358 case DNS_TYPE_NS:
359 DNS_PARSE_STR(rec->data.ns, NULL, " ", saveptr);
360 break;
362 case DNS_TYPE_SOA:
363 DNS_PARSE_STR(rec->data.soa.mname, NULL, " ", saveptr);
364 DNS_PARSE_STR(rec->data.soa.rname, NULL, " ", saveptr);
365 DNS_PARSE_UINT(rec->data.soa.serial, NULL, " ", saveptr);
366 DNS_PARSE_UINT(rec->data.soa.refresh, NULL, " ", saveptr);
367 DNS_PARSE_UINT(rec->data.soa.retry, NULL, " ", saveptr);
368 DNS_PARSE_UINT(rec->data.soa.expire, NULL, " ", saveptr);
369 DNS_PARSE_UINT(rec->data.soa.minimum, NULL, " ", saveptr);
370 break;
372 default:
373 state->log(ISC_LOG_ERROR, "samba b9_parse: unhandled record type %u",
374 rec->wType);
375 return false;
378 /* we should be at the end of the buffer now */
379 if (strtok_r(NULL, "\t ", &saveptr) != NULL) {
380 state->log(ISC_LOG_ERROR, "samba b9_parse: expected data at end of string for '%s'");
381 return false;
384 return true;
388 send a resource recond to bind9
390 static isc_result_t b9_putrr(struct dlz_bind9_data *state,
391 void *handle, struct dnsp_DnssrvRpcRecord *rec,
392 const char **types)
394 isc_result_t result;
395 const char *type, *data;
396 TALLOC_CTX *tmp_ctx = talloc_new(state);
398 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
399 return ISC_R_FAILURE;
402 if (data == NULL) {
403 talloc_free(tmp_ctx);
404 return ISC_R_NOMEMORY;
407 if (types) {
408 int i;
409 for (i=0; types[i]; i++) {
410 if (strcmp(types[i], type) == 0) break;
412 if (types[i] == NULL) {
413 /* skip it */
414 return ISC_R_SUCCESS;
418 result = state->putrr(handle, type, rec->dwTtlSeconds, data);
419 if (result != ISC_R_SUCCESS) {
420 state->log(ISC_LOG_ERROR, "Failed to put rr");
422 talloc_free(tmp_ctx);
423 return result;
428 send a named resource recond to bind9
430 static isc_result_t b9_putnamedrr(struct dlz_bind9_data *state,
431 void *handle, const char *name,
432 struct dnsp_DnssrvRpcRecord *rec)
434 isc_result_t result;
435 const char *type, *data;
436 TALLOC_CTX *tmp_ctx = talloc_new(state);
438 if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
439 return ISC_R_FAILURE;
442 if (data == NULL) {
443 talloc_free(tmp_ctx);
444 return ISC_R_NOMEMORY;
447 result = state->putnamedrr(handle, name, type, rec->dwTtlSeconds, data);
448 if (result != ISC_R_SUCCESS) {
449 state->log(ISC_LOG_ERROR, "Failed to put named rr '%s'", name);
451 talloc_free(tmp_ctx);
452 return result;
456 parse options
458 static isc_result_t parse_options(struct dlz_bind9_data *state,
459 unsigned int argc, char *argv[],
460 struct b9_options *options)
462 int opt;
463 poptContext pc;
464 struct poptOption long_options[] = {
465 { "url", 'H', POPT_ARG_STRING, &options->url, 0, "database URL", "URL" },
466 { "debug", 'd', POPT_ARG_STRING, &options->debug, 0, "debug level", "DEBUG" },
467 { NULL }
470 pc = poptGetContext("dlz_bind9", argc, (const char **)argv, long_options,
471 POPT_CONTEXT_KEEP_FIRST);
472 while ((opt = poptGetNextOpt(pc)) != -1) {
473 switch (opt) {
474 default:
475 state->log(ISC_LOG_ERROR, "dlz_bind9: Invalid option %s: %s",
476 poptBadOption(pc, 0), poptStrerror(opt));
477 return ISC_R_FAILURE;
481 return ISC_R_SUCCESS;
486 * Create session info from PAC
487 * This is called as auth_context->generate_session_info_pac()
489 static NTSTATUS b9_generate_session_info_pac(struct auth4_context *auth_context,
490 TALLOC_CTX *mem_ctx,
491 struct smb_krb5_context *smb_krb5_context,
492 DATA_BLOB *pac_blob,
493 const char *principal_name,
494 const struct tsocket_address *remote_addr,
495 uint32_t session_info_flags,
496 struct auth_session_info **session_info)
498 NTSTATUS status;
499 struct auth_user_info_dc *user_info_dc;
500 TALLOC_CTX *tmp_ctx;
502 tmp_ctx = talloc_new(mem_ctx);
503 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
505 status = kerberos_pac_blob_to_user_info_dc(tmp_ctx,
506 *pac_blob,
507 smb_krb5_context->krb5_context,
508 &user_info_dc,
509 NULL,
510 NULL);
511 if (!NT_STATUS_IS_OK(status)) {
512 talloc_free(tmp_ctx);
513 return status;
516 if (user_info_dc->info->authenticated) {
517 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
520 session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
522 status = auth_generate_session_info(mem_ctx, NULL, NULL, user_info_dc,
523 session_info_flags, session_info);
524 if (!NT_STATUS_IS_OK(status)) {
525 talloc_free(tmp_ctx);
526 return status;
529 talloc_free(tmp_ctx);
530 return status;
533 /* Callback for the DEBUG() system, to catch the remaining messages */
534 static void b9_debug(void *private_ptr, int msg_level, const char *msg)
536 static const int isc_log_map[] = {
537 ISC_LOG_CRITICAL, /* 0 */
538 ISC_LOG_ERROR, /* 1 */
539 ISC_LOG_WARNING, /* 2 */
540 ISC_LOG_NOTICE /* 3 */
542 struct dlz_bind9_data *state = private_ptr;
543 int isc_log_level;
545 if (msg_level >= ARRAY_SIZE(isc_log_map) || msg_level < 0) {
546 isc_log_level = ISC_LOG_INFO;
547 } else {
548 isc_log_level = isc_log_map[msg_level];
550 state->log(isc_log_level, "samba_dlz: %s", msg);
553 int dlz_state_debug_unregister(struct dlz_bind9_data *state)
555 /* Stop logging (to the bind9 logs) */
556 debug_set_callback(NULL, NULL);
560 called to initialise the driver
562 _PUBLIC_ isc_result_t dlz_create(const char *dlzname,
563 unsigned int argc, char *argv[],
564 void **dbdata, ...)
566 struct dlz_bind9_data *state;
567 const char *helper_name;
568 va_list ap;
569 isc_result_t result;
570 struct ldb_dn *dn;
571 NTSTATUS nt_status;
573 state = talloc_zero(NULL, struct dlz_bind9_data);
574 if (state == NULL) {
575 return ISC_R_NOMEMORY;
578 talloc_set_destructor(state, dlz_state_debug_unregister);
580 /* fill in the helper functions */
581 va_start(ap, dbdata);
582 while ((helper_name = va_arg(ap, const char *)) != NULL) {
583 b9_add_helper(state, helper_name, va_arg(ap, void*));
585 va_end(ap);
587 /* Do not install samba signal handlers */
588 fault_setup_disable();
590 /* Start logging (to the bind9 logs) */
591 debug_set_callback(state, b9_debug);
593 state->ev_ctx = s4_event_context_init(state);
594 if (state->ev_ctx == NULL) {
595 result = ISC_R_NOMEMORY;
596 goto failed;
599 result = parse_options(state, argc, argv, &state->options);
600 if (result != ISC_R_SUCCESS) {
601 goto failed;
604 state->lp = loadparm_init_global(true);
605 if (state->lp == NULL) {
606 result = ISC_R_NOMEMORY;
607 goto failed;
610 if (state->options.debug) {
611 lpcfg_do_global_parameter(state->lp, "log level", state->options.debug);
612 } else {
613 lpcfg_do_global_parameter(state->lp, "log level", "0");
616 if (smb_krb5_init_context(state, state->ev_ctx, state->lp, &state->smb_krb5_ctx) != 0) {
617 result = ISC_R_NOMEMORY;
618 goto failed;
621 nt_status = gensec_init();
622 if (!NT_STATUS_IS_OK(nt_status)) {
623 result = ISC_R_NOMEMORY;
624 goto failed;
627 state->auth_context = talloc_zero(state, struct auth4_context);
628 if (state->auth_context == NULL) {
629 result = ISC_R_NOMEMORY;
630 goto failed;
633 if (state->options.url == NULL) {
634 state->options.url = lpcfg_private_path(state, state->lp, "dns/sam.ldb");
635 if (state->options.url == NULL) {
636 result = ISC_R_NOMEMORY;
637 goto failed;
641 state->samdb = samdb_connect_url(state, state->ev_ctx, state->lp,
642 system_session(state->lp), 0, state->options.url);
643 if (state->samdb == NULL) {
644 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to connect to %s",
645 state->options.url);
646 result = ISC_R_FAILURE;
647 goto failed;
650 dn = ldb_get_default_basedn(state->samdb);
651 if (dn == NULL) {
652 state->log(ISC_LOG_ERROR, "samba_dlz: Unable to get basedn for %s - %s",
653 state->options.url, ldb_errstring(state->samdb));
654 result = ISC_R_FAILURE;
655 goto failed;
658 state->log(ISC_LOG_INFO, "samba_dlz: started for DN %s",
659 ldb_dn_get_linearized(dn));
661 state->auth_context->event_ctx = state->ev_ctx;
662 state->auth_context->lp_ctx = state->lp;
663 state->auth_context->sam_ctx = state->samdb;
664 state->auth_context->generate_session_info_pac = b9_generate_session_info_pac;
666 *dbdata = state;
668 return ISC_R_SUCCESS;
670 failed:
671 talloc_free(state);
672 return result;
676 shutdown the backend
678 _PUBLIC_ void dlz_destroy(void *dbdata)
680 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
681 state->log(ISC_LOG_INFO, "samba_dlz: shutting down");
682 talloc_free(state);
687 return the base DN for a zone
689 static isc_result_t b9_find_zone_dn(struct dlz_bind9_data *state, const char *zone_name,
690 TALLOC_CTX *mem_ctx, struct ldb_dn **zone_dn)
692 int ret;
693 TALLOC_CTX *tmp_ctx = talloc_new(state);
694 const char *attrs[] = { NULL };
695 int i;
697 for (i=0; zone_prefixes[i]; i++) {
698 struct ldb_dn *dn;
699 struct ldb_result *res;
701 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
702 if (dn == NULL) {
703 talloc_free(tmp_ctx);
704 return ISC_R_NOMEMORY;
707 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone_name, zone_prefixes[i])) {
708 talloc_free(tmp_ctx);
709 return ISC_R_NOMEMORY;
712 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsZone");
713 if (ret == LDB_SUCCESS) {
714 if (zone_dn != NULL) {
715 *zone_dn = talloc_steal(mem_ctx, dn);
717 talloc_free(tmp_ctx);
718 return ISC_R_SUCCESS;
720 talloc_free(dn);
723 talloc_free(tmp_ctx);
724 return ISC_R_NOTFOUND;
729 return the DN for a name. The record does not need to exist, but the
730 zone must exist
732 static isc_result_t b9_find_name_dn(struct dlz_bind9_data *state, const char *name,
733 TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
735 const char *p;
737 /* work through the name piece by piece, until we find a zone */
738 for (p=name; p; ) {
739 isc_result_t result;
740 result = b9_find_zone_dn(state, p, mem_ctx, dn);
741 if (result == ISC_R_SUCCESS) {
742 /* we found a zone, now extend the DN to get
743 * the full DN
745 bool ret;
746 if (p == name) {
747 ret = ldb_dn_add_child_fmt(*dn, "DC=@");
748 } else {
749 ret = ldb_dn_add_child_fmt(*dn, "DC=%.*s", (int)(p-name)-1, name);
751 if (!ret) {
752 talloc_free(*dn);
753 return ISC_R_NOMEMORY;
755 return ISC_R_SUCCESS;
757 p = strchr(p, '.');
758 if (p == NULL) {
759 break;
761 p++;
763 return ISC_R_NOTFOUND;
768 see if we handle a given zone
770 _PUBLIC_ isc_result_t dlz_findzonedb(void *dbdata, const char *name)
772 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
773 return b9_find_zone_dn(state, name, NULL, NULL);
778 lookup one record
780 static isc_result_t dlz_lookup_types(struct dlz_bind9_data *state,
781 const char *zone, const char *name,
782 dns_sdlzlookup_t *lookup,
783 const char **types)
785 TALLOC_CTX *tmp_ctx = talloc_new(state);
786 const char *attrs[] = { "dnsRecord", NULL };
787 int ret = LDB_SUCCESS, i;
788 struct ldb_result *res;
789 struct ldb_message_element *el;
790 struct ldb_dn *dn;
792 for (i=0; zone_prefixes[i]; i++) {
793 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
794 if (dn == NULL) {
795 talloc_free(tmp_ctx);
796 return ISC_R_NOMEMORY;
799 if (!ldb_dn_add_child_fmt(dn, "DC=%s,DC=%s,%s", name, zone, zone_prefixes[i])) {
800 talloc_free(tmp_ctx);
801 return ISC_R_NOMEMORY;
804 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
805 attrs, "objectClass=dnsNode");
806 if (ret == LDB_SUCCESS) {
807 break;
810 if (ret != LDB_SUCCESS) {
811 talloc_free(tmp_ctx);
812 return ISC_R_NOTFOUND;
815 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
816 if (el == NULL || el->num_values == 0) {
817 talloc_free(tmp_ctx);
818 return ISC_R_NOTFOUND;
821 for (i=0; i<el->num_values; i++) {
822 struct dnsp_DnssrvRpcRecord rec;
823 enum ndr_err_code ndr_err;
824 isc_result_t result;
826 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec,
827 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
828 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
829 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
830 ldb_dn_get_linearized(dn));
831 talloc_free(tmp_ctx);
832 return ISC_R_FAILURE;
835 result = b9_putrr(state, lookup, &rec, types);
836 if (result != ISC_R_SUCCESS) {
837 talloc_free(tmp_ctx);
838 return result;
842 talloc_free(tmp_ctx);
843 return ISC_R_SUCCESS;
847 lookup one record
849 #ifdef BIND_VERSION_9_8
850 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name,
851 void *dbdata, dns_sdlzlookup_t *lookup)
852 #else
853 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name,
854 void *dbdata, dns_sdlzlookup_t *lookup,
855 dns_clientinfomethods_t *methods,
856 dns_clientinfo_t *clientinfo)
857 #endif
859 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
860 return dlz_lookup_types(state, zone, name, lookup, NULL);
865 see if a zone transfer is allowed
867 _PUBLIC_ isc_result_t dlz_allowzonexfr(void *dbdata, const char *name, const char *client)
869 /* just say yes for all our zones for now */
870 return dlz_findzonedb(dbdata, name);
874 perform a zone transfer
876 _PUBLIC_ isc_result_t dlz_allnodes(const char *zone, void *dbdata,
877 dns_sdlzallnodes_t *allnodes)
879 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
880 const char *attrs[] = { "dnsRecord", NULL };
881 int ret = LDB_SUCCESS, i, j;
882 struct ldb_dn *dn;
883 struct ldb_result *res;
884 TALLOC_CTX *tmp_ctx = talloc_new(state);
886 for (i=0; zone_prefixes[i]; i++) {
887 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
888 if (dn == NULL) {
889 talloc_free(tmp_ctx);
890 return ISC_R_NOMEMORY;
893 if (!ldb_dn_add_child_fmt(dn, "DC=%s,%s", zone, zone_prefixes[i])) {
894 talloc_free(tmp_ctx);
895 return ISC_R_NOMEMORY;
898 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
899 attrs, "objectClass=dnsNode");
900 if (ret == LDB_SUCCESS) {
901 break;
904 if (ret != LDB_SUCCESS) {
905 talloc_free(tmp_ctx);
906 return ISC_R_NOTFOUND;
909 for (i=0; i<res->count; i++) {
910 struct ldb_message_element *el;
911 TALLOC_CTX *el_ctx = talloc_new(tmp_ctx);
912 const char *rdn, *name;
913 const struct ldb_val *v;
915 el = ldb_msg_find_element(res->msgs[i], "dnsRecord");
916 if (el == NULL || el->num_values == 0) {
917 state->log(ISC_LOG_INFO, "failed to find dnsRecord for %s",
918 ldb_dn_get_linearized(dn));
919 talloc_free(el_ctx);
920 continue;
923 v = ldb_dn_get_rdn_val(res->msgs[i]->dn);
924 if (v == NULL) {
925 state->log(ISC_LOG_INFO, "failed to find RDN for %s",
926 ldb_dn_get_linearized(dn));
927 talloc_free(el_ctx);
928 continue;
931 rdn = talloc_strndup(el_ctx, (char *)v->data, v->length);
932 if (rdn == NULL) {
933 talloc_free(tmp_ctx);
934 return ISC_R_NOMEMORY;
937 if (strcmp(rdn, "@") == 0) {
938 name = zone;
939 } else {
940 name = talloc_asprintf(el_ctx, "%s.%s", rdn, zone);
942 if (name == NULL) {
943 talloc_free(tmp_ctx);
944 return ISC_R_NOMEMORY;
947 for (j=0; j<el->num_values; j++) {
948 struct dnsp_DnssrvRpcRecord rec;
949 enum ndr_err_code ndr_err;
950 isc_result_t result;
952 ndr_err = ndr_pull_struct_blob(&el->values[j], el_ctx, &rec,
953 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
954 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
955 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
956 ldb_dn_get_linearized(dn));
957 continue;
960 result = b9_putnamedrr(state, allnodes, name, &rec);
961 if (result != ISC_R_SUCCESS) {
962 continue;
967 talloc_free(tmp_ctx);
969 return ISC_R_SUCCESS;
974 start a transaction
976 _PUBLIC_ isc_result_t dlz_newversion(const char *zone, void *dbdata, void **versionp)
978 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
980 state->log(ISC_LOG_INFO, "samba_dlz: starting transaction on zone %s", zone);
982 if (state->transaction_token != NULL) {
983 state->log(ISC_LOG_INFO, "samba_dlz: transaction already started for zone %s", zone);
984 return ISC_R_FAILURE;
987 state->transaction_token = talloc_zero(state, int);
988 if (state->transaction_token == NULL) {
989 return ISC_R_NOMEMORY;
992 if (ldb_transaction_start(state->samdb) != LDB_SUCCESS) {
993 state->log(ISC_LOG_INFO, "samba_dlz: failed to start a transaction for zone %s", zone);
994 talloc_free(state->transaction_token);
995 state->transaction_token = NULL;
996 return ISC_R_FAILURE;
999 *versionp = (void *)state->transaction_token;
1001 return ISC_R_SUCCESS;
1005 end a transaction
1007 _PUBLIC_ void dlz_closeversion(const char *zone, isc_boolean_t commit,
1008 void *dbdata, void **versionp)
1010 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1012 if (state->transaction_token != (int *)*versionp) {
1013 state->log(ISC_LOG_INFO, "samba_dlz: transaction not started for zone %s", zone);
1014 return;
1017 if (commit) {
1018 if (ldb_transaction_commit(state->samdb) != LDB_SUCCESS) {
1019 state->log(ISC_LOG_INFO, "samba_dlz: failed to commit a transaction for zone %s", zone);
1020 return;
1022 state->log(ISC_LOG_INFO, "samba_dlz: committed transaction on zone %s", zone);
1023 } else {
1024 if (ldb_transaction_cancel(state->samdb) != LDB_SUCCESS) {
1025 state->log(ISC_LOG_INFO, "samba_dlz: failed to cancel a transaction for zone %s", zone);
1026 return;
1028 state->log(ISC_LOG_INFO, "samba_dlz: cancelling transaction on zone %s", zone);
1031 talloc_free(state->transaction_token);
1032 state->transaction_token = NULL;
1033 *versionp = NULL;
1038 see if there is a SOA record for a zone
1040 static bool b9_has_soa(struct dlz_bind9_data *state, struct ldb_dn *dn, const char *zone)
1042 const char *attrs[] = { "dnsRecord", NULL };
1043 struct ldb_result *res;
1044 struct ldb_message_element *el;
1045 TALLOC_CTX *tmp_ctx = talloc_new(state);
1046 int ret, i;
1048 if (!ldb_dn_add_child_fmt(dn, "DC=@,DC=%s", zone)) {
1049 talloc_free(tmp_ctx);
1050 return false;
1053 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
1054 attrs, "objectClass=dnsNode");
1055 if (ret != LDB_SUCCESS) {
1056 talloc_free(tmp_ctx);
1057 return false;
1060 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1061 if (el == NULL) {
1062 talloc_free(tmp_ctx);
1063 return false;
1065 for (i=0; i<el->num_values; i++) {
1066 struct dnsp_DnssrvRpcRecord rec;
1067 enum ndr_err_code ndr_err;
1069 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec,
1070 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1071 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1072 continue;
1074 if (rec.wType == DNS_TYPE_SOA) {
1075 talloc_free(tmp_ctx);
1076 return true;
1080 talloc_free(tmp_ctx);
1081 return false;
1085 configure a writeable zone
1087 _PUBLIC_ isc_result_t dlz_configure(dns_view_t *view, void *dbdata)
1089 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1090 TALLOC_CTX *tmp_ctx;
1091 struct ldb_dn *dn;
1092 int i;
1094 state->log(ISC_LOG_INFO, "samba_dlz: starting configure");
1095 if (state->writeable_zone == NULL) {
1096 state->log(ISC_LOG_INFO, "samba_dlz: no writeable_zone method available");
1097 return ISC_R_FAILURE;
1100 tmp_ctx = talloc_new(state);
1102 for (i=0; zone_prefixes[i]; i++) {
1103 const char *attrs[] = { "name", NULL };
1104 int j, ret;
1105 struct ldb_result *res;
1107 dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
1108 if (dn == NULL) {
1109 talloc_free(tmp_ctx);
1110 return ISC_R_NOMEMORY;
1113 if (!ldb_dn_add_child_fmt(dn, "%s", zone_prefixes[i])) {
1114 talloc_free(tmp_ctx);
1115 return ISC_R_NOMEMORY;
1118 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
1119 attrs, "objectClass=dnsZone");
1120 if (ret != LDB_SUCCESS) {
1121 continue;
1124 for (j=0; j<res->count; j++) {
1125 isc_result_t result;
1126 const char *zone = ldb_msg_find_attr_as_string(res->msgs[j], "name", NULL);
1127 struct ldb_dn *zone_dn;
1129 if (zone == NULL) {
1130 continue;
1132 zone_dn = ldb_dn_copy(tmp_ctx, dn);
1133 if (zone_dn == NULL) {
1134 talloc_free(tmp_ctx);
1135 return ISC_R_NOMEMORY;
1138 if (!b9_has_soa(state, zone_dn, zone)) {
1139 continue;
1141 result = state->writeable_zone(view, zone);
1142 if (result != ISC_R_SUCCESS) {
1143 state->log(ISC_LOG_ERROR, "samba_dlz: Failed to configure zone '%s'",
1144 zone);
1145 talloc_free(tmp_ctx);
1146 return result;
1148 state->log(ISC_LOG_INFO, "samba_dlz: configured writeable zone '%s'", zone);
1152 talloc_free(tmp_ctx);
1153 return ISC_R_SUCCESS;
1157 authorize a zone update
1159 _PUBLIC_ isc_boolean_t dlz_ssumatch(const char *signer, const char *name, const char *tcpaddr,
1160 const char *type, const char *key, uint32_t keydatalen, uint8_t *keydata,
1161 void *dbdata)
1163 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1164 TALLOC_CTX *tmp_ctx;
1165 DATA_BLOB ap_req;
1166 struct cli_credentials *server_credentials;
1167 char *keytab_name;
1168 int ret;
1169 int ldb_ret;
1170 NTSTATUS nt_status;
1171 struct gensec_security *gensec_ctx;
1172 struct auth_session_info *session_info;
1173 struct ldb_dn *dn;
1174 isc_result_t result;
1175 struct ldb_result *res;
1176 const char * attrs[] = { NULL };
1177 uint32_t access_mask;
1179 /* Remove cached credentials, if any */
1180 if (state->session_info) {
1181 talloc_free(state->session_info);
1182 state->session_info = NULL;
1184 if (state->update_name) {
1185 talloc_free(state->update_name);
1186 state->update_name = NULL;
1189 tmp_ctx = talloc_new(NULL);
1190 if (tmp_ctx == NULL) {
1191 state->log(ISC_LOG_ERROR, "samba_dlz: no memory");
1192 return ISC_FALSE;
1195 ap_req = data_blob_const(keydata, keydatalen);
1196 server_credentials = cli_credentials_init(tmp_ctx);
1197 if (!server_credentials) {
1198 state->log(ISC_LOG_ERROR, "samba_dlz: failed to init server credentials");
1199 talloc_free(tmp_ctx);
1200 return ISC_FALSE;
1203 cli_credentials_set_krb5_context(server_credentials, state->smb_krb5_ctx);
1204 cli_credentials_set_conf(server_credentials, state->lp);
1206 keytab_name = talloc_asprintf(tmp_ctx, "file:%s/dns.keytab",
1207 lpcfg_private_dir(state->lp));
1208 ret = cli_credentials_set_keytab_name(server_credentials, state->lp, keytab_name,
1209 CRED_SPECIFIED);
1210 if (ret != 0) {
1211 state->log(ISC_LOG_ERROR, "samba_dlz: failed to obtain server credentials from %s",
1212 keytab_name);
1213 talloc_free(tmp_ctx);
1214 return ISC_FALSE;
1216 talloc_free(keytab_name);
1218 nt_status = gensec_server_start(tmp_ctx,
1219 lpcfg_gensec_settings(tmp_ctx, state->lp),
1220 state->auth_context, &gensec_ctx);
1221 if (!NT_STATUS_IS_OK(nt_status)) {
1222 state->log(ISC_LOG_ERROR, "samba_dlz: failed to start gensec server");
1223 talloc_free(tmp_ctx);
1224 return ISC_FALSE;
1227 gensec_set_credentials(gensec_ctx, server_credentials);
1229 nt_status = gensec_start_mech_by_name(gensec_ctx, "spnego");
1230 if (!NT_STATUS_IS_OK(nt_status)) {
1231 state->log(ISC_LOG_ERROR, "samba_dlz: failed to start spnego");
1232 talloc_free(tmp_ctx);
1233 return ISC_FALSE;
1236 nt_status = gensec_update(gensec_ctx, tmp_ctx, state->ev_ctx, ap_req, &ap_req);
1237 if (!NT_STATUS_IS_OK(nt_status)) {
1238 state->log(ISC_LOG_ERROR, "samba_dlz: spnego update failed");
1239 talloc_free(tmp_ctx);
1240 return ISC_FALSE;
1243 nt_status = gensec_session_info(gensec_ctx, tmp_ctx, &session_info);
1244 if (!NT_STATUS_IS_OK(nt_status)) {
1245 state->log(ISC_LOG_ERROR, "samba_dlz: failed to create session info");
1246 talloc_free(tmp_ctx);
1247 return ISC_FALSE;
1250 /* Get the DN from name */
1251 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1252 if (result != ISC_R_SUCCESS) {
1253 state->log(ISC_LOG_ERROR, "samba_dlz: failed to find name %s", name);
1254 talloc_free(tmp_ctx);
1255 return ISC_FALSE;
1258 /* make sure the dn exists, or find parent dn in case new object is being added */
1259 ldb_ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
1260 attrs, "objectClass=dnsNode");
1261 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
1262 ldb_dn_remove_child_components(dn, 1);
1263 access_mask = SEC_ADS_CREATE_CHILD;
1264 talloc_free(res);
1265 } else if (ldb_ret == LDB_SUCCESS) {
1266 access_mask = SEC_STD_REQUIRED | SEC_ADS_SELF_WRITE;
1267 talloc_free(res);
1268 } else {
1269 talloc_free(tmp_ctx);
1270 return ISC_FALSE;
1273 /* Do ACL check */
1274 ldb_ret = dsdb_check_access_on_dn(state->samdb, tmp_ctx, dn,
1275 session_info->security_token,
1276 access_mask, NULL);
1277 if (ldb_ret != LDB_SUCCESS) {
1278 state->log(ISC_LOG_INFO,
1279 "samba_dlz: disallowing update of signer=%s name=%s type=%s error=%s",
1280 signer, name, type, ldb_strerror(ldb_ret));
1281 talloc_free(tmp_ctx);
1282 return ISC_FALSE;
1285 /* Cache session_info, so it can be used in the actual add/delete operation */
1286 state->update_name = talloc_strdup(state, name);
1287 if (state->update_name == NULL) {
1288 state->log(ISC_LOG_ERROR, "samba_dlz: memory allocation error");
1289 talloc_free(tmp_ctx);
1290 return ISC_FALSE;
1292 state->session_info = talloc_steal(state, session_info);
1294 state->log(ISC_LOG_INFO, "samba_dlz: allowing update of signer=%s name=%s tcpaddr=%s type=%s key=%s",
1295 signer, name, tcpaddr, type, key);
1297 talloc_free(tmp_ctx);
1298 return ISC_TRUE;
1303 add a new record
1305 static isc_result_t b9_add_record(struct dlz_bind9_data *state, const char *name,
1306 struct ldb_dn *dn,
1307 struct dnsp_DnssrvRpcRecord *rec)
1309 struct ldb_message *msg;
1310 enum ndr_err_code ndr_err;
1311 struct ldb_val v;
1312 int ret;
1314 msg = ldb_msg_new(rec);
1315 if (msg == NULL) {
1316 return ISC_R_NOMEMORY;
1318 msg->dn = dn;
1319 ret = ldb_msg_add_string(msg, "objectClass", "dnsNode");
1320 if (ret != LDB_SUCCESS) {
1321 return ISC_R_FAILURE;
1324 ndr_err = ndr_push_struct_blob(&v, rec, rec, (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
1325 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1326 return ISC_R_FAILURE;
1328 ret = ldb_msg_add_value(msg, "dnsRecord", &v, NULL);
1329 if (ret != LDB_SUCCESS) {
1330 return ISC_R_FAILURE;
1333 ret = ldb_add(state->samdb, msg);
1334 if (ret != LDB_SUCCESS) {
1335 return ISC_R_FAILURE;
1338 return ISC_R_SUCCESS;
1342 see if two DNS names are the same
1344 static bool dns_name_equal(const char *name1, const char *name2)
1346 size_t len1 = strlen(name1);
1347 size_t len2 = strlen(name2);
1348 if (name1[len1-1] == '.') len1--;
1349 if (name2[len2-1] == '.') len2--;
1350 if (len1 != len2) {
1351 return false;
1353 return strncasecmp_m(name1, name2, len1) == 0;
1358 see if two dns records match
1360 static bool b9_record_match(struct dlz_bind9_data *state,
1361 struct dnsp_DnssrvRpcRecord *rec1, struct dnsp_DnssrvRpcRecord *rec2)
1363 bool status;
1364 int i;
1366 if (rec1->wType != rec2->wType) {
1367 return false;
1369 /* see if this type is single valued */
1370 if (b9_single_valued(rec1->wType)) {
1371 return true;
1374 /* see if the data matches */
1375 switch (rec1->wType) {
1376 case DNS_TYPE_A:
1377 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
1378 case DNS_TYPE_AAAA:
1379 return strcmp(rec1->data.ipv6, rec2->data.ipv6) == 0;
1380 case DNS_TYPE_CNAME:
1381 return dns_name_equal(rec1->data.cname, rec2->data.cname);
1382 case DNS_TYPE_TXT:
1383 status = (rec1->data.txt.count == rec2->data.txt.count);
1384 if (!status) return status;
1385 for (i=0; i<rec1->data.txt.count; i++) {
1386 status &= (strcmp(rec1->data.txt.str[i], rec2->data.txt.str[i]) == 0);
1388 return status;
1389 case DNS_TYPE_PTR:
1390 return dns_name_equal(rec1->data.ptr, rec2->data.ptr);
1391 case DNS_TYPE_NS:
1392 return dns_name_equal(rec1->data.ns, rec2->data.ns);
1394 case DNS_TYPE_SRV:
1395 return rec1->data.srv.wPriority == rec2->data.srv.wPriority &&
1396 rec1->data.srv.wWeight == rec2->data.srv.wWeight &&
1397 rec1->data.srv.wPort == rec2->data.srv.wPort &&
1398 dns_name_equal(rec1->data.srv.nameTarget, rec2->data.srv.nameTarget);
1400 case DNS_TYPE_MX:
1401 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
1402 dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
1404 case DNS_TYPE_HINFO:
1405 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
1406 strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
1408 case DNS_TYPE_SOA:
1409 return dns_name_equal(rec1->data.soa.mname, rec2->data.soa.mname) &&
1410 dns_name_equal(rec1->data.soa.rname, rec2->data.soa.rname) &&
1411 rec1->data.soa.serial == rec2->data.soa.serial &&
1412 rec1->data.soa.refresh == rec2->data.soa.refresh &&
1413 rec1->data.soa.retry == rec2->data.soa.retry &&
1414 rec1->data.soa.expire == rec2->data.soa.expire &&
1415 rec1->data.soa.minimum == rec2->data.soa.minimum;
1416 default:
1417 state->log(ISC_LOG_ERROR, "samba b9_putrr: unhandled record type %u",
1418 rec1->wType);
1419 break;
1422 return false;
1426 * Update session_info on samdb using the cached credentials
1428 static bool b9_set_session_info(struct dlz_bind9_data *state, const char *name)
1430 int ret;
1432 if (state->update_name == NULL || state->session_info == NULL) {
1433 state->log(ISC_LOG_ERROR, "samba_dlz: invalid credentials");
1434 return false;
1437 /* Do not use client credentials, if we not updating the client specified name */
1438 if (strcmp(state->update_name, name) != 0) {
1439 return true;
1442 ret = ldb_set_opaque(state->samdb, "sessionInfo", state->session_info);
1443 if (ret != LDB_SUCCESS) {
1444 state->log(ISC_LOG_ERROR, "samba_dlz: unable to set session info");
1445 return false;
1448 return true;
1452 * Reset session_info on samdb as system session
1454 static void b9_reset_session_info(struct dlz_bind9_data *state)
1456 ldb_set_opaque(state->samdb, "sessionInfo", system_session(state->lp));
1460 add or modify a rdataset
1462 _PUBLIC_ isc_result_t dlz_addrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1464 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1465 struct dnsp_DnssrvRpcRecord *rec;
1466 struct ldb_dn *dn;
1467 isc_result_t result;
1468 struct ldb_result *res;
1469 const char *attrs[] = { "dnsRecord", NULL };
1470 int ret, i;
1471 struct ldb_message_element *el;
1472 enum ndr_err_code ndr_err;
1473 NTTIME t;
1475 if (state->transaction_token != (void*)version) {
1476 state->log(ISC_LOG_INFO, "samba_dlz: bad transaction version");
1477 return ISC_R_FAILURE;
1480 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1481 if (rec == NULL) {
1482 return ISC_R_NOMEMORY;
1485 unix_to_nt_time(&t, time(NULL));
1486 t /= 10*1000*1000; /* convert to seconds (NT time is in 100ns units) */
1487 t /= 3600; /* convert to hours */
1489 rec->rank = DNS_RANK_ZONE;
1490 rec->dwSerial = state->soa_serial;
1491 rec->dwTimeStamp = (uint32_t)t;
1493 if (!b9_parse(state, rdatastr, rec)) {
1494 state->log(ISC_LOG_INFO, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1495 talloc_free(rec);
1496 return ISC_R_FAILURE;
1499 /* find the DN of the record */
1500 result = b9_find_name_dn(state, name, rec, &dn);
1501 if (result != ISC_R_SUCCESS) {
1502 talloc_free(rec);
1503 return result;
1506 /* get any existing records */
1507 ret = ldb_search(state->samdb, rec, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1508 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1509 if (!b9_set_session_info(state, name)) {
1510 talloc_free(rec);
1511 return ISC_R_FAILURE;
1513 result = b9_add_record(state, name, dn, rec);
1514 b9_reset_session_info(state);
1515 talloc_free(rec);
1516 if (result == ISC_R_SUCCESS) {
1517 state->log(ISC_LOG_INFO, "samba_dlz: added %s %s", name, rdatastr);
1519 return result;
1522 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1523 if (el == NULL) {
1524 ret = ldb_msg_add_empty(res->msgs[0], "dnsRecord", LDB_FLAG_MOD_ADD, &el);
1525 if (ret != LDB_SUCCESS) {
1526 state->log(ISC_LOG_ERROR, "samba_dlz: failed to add dnsRecord for %s",
1527 ldb_dn_get_linearized(dn));
1528 talloc_free(rec);
1529 return ISC_R_FAILURE;
1533 /* there are existing records. We need to see if this will
1534 * replace a record or add to it
1536 for (i=0; i<el->num_values; i++) {
1537 struct dnsp_DnssrvRpcRecord rec2;
1539 ndr_err = ndr_pull_struct_blob(&el->values[i], rec, &rec2,
1540 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1541 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1542 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1543 ldb_dn_get_linearized(dn));
1544 talloc_free(rec);
1545 return ISC_R_FAILURE;
1548 if (b9_record_match(state, rec, &rec2)) {
1549 break;
1552 if (i == el->num_values) {
1553 /* adding a new value */
1554 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values+1);
1555 if (el->values == NULL) {
1556 talloc_free(rec);
1557 return ISC_R_NOMEMORY;
1559 el->num_values++;
1562 ndr_err = ndr_push_struct_blob(&el->values[i], rec, rec,
1563 (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
1564 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1565 state->log(ISC_LOG_ERROR, "samba_dlz: failed to push dnsRecord for %s",
1566 ldb_dn_get_linearized(dn));
1567 talloc_free(rec);
1568 return ISC_R_FAILURE;
1572 if (!b9_set_session_info(state, name)) {
1573 talloc_free(rec);
1574 return ISC_R_FAILURE;
1577 /* modify the record */
1578 el->flags = LDB_FLAG_MOD_REPLACE;
1579 ret = ldb_modify(state->samdb, res->msgs[0]);
1580 b9_reset_session_info(state);
1581 if (ret != LDB_SUCCESS) {
1582 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1583 ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1584 talloc_free(rec);
1585 return ISC_R_FAILURE;
1588 state->log(ISC_LOG_INFO, "samba_dlz: added rdataset %s '%s'", name, rdatastr);
1590 talloc_free(rec);
1591 return ISC_R_SUCCESS;
1595 remove a rdataset
1597 _PUBLIC_ isc_result_t dlz_subrdataset(const char *name, const char *rdatastr, void *dbdata, void *version)
1599 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1600 struct dnsp_DnssrvRpcRecord *rec;
1601 struct ldb_dn *dn;
1602 isc_result_t result;
1603 struct ldb_result *res;
1604 const char *attrs[] = { "dnsRecord", NULL };
1605 int ret, i;
1606 struct ldb_message_element *el;
1607 enum ndr_err_code ndr_err;
1609 if (state->transaction_token != (void*)version) {
1610 state->log(ISC_LOG_ERROR, "samba_dlz: bad transaction version");
1611 return ISC_R_FAILURE;
1614 rec = talloc_zero(state, struct dnsp_DnssrvRpcRecord);
1615 if (rec == NULL) {
1616 return ISC_R_NOMEMORY;
1619 if (!b9_parse(state, rdatastr, rec)) {
1620 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse rdataset '%s'", rdatastr);
1621 talloc_free(rec);
1622 return ISC_R_FAILURE;
1625 /* find the DN of the record */
1626 result = b9_find_name_dn(state, name, rec, &dn);
1627 if (result != ISC_R_SUCCESS) {
1628 talloc_free(rec);
1629 return result;
1632 /* get the existing records */
1633 ret = ldb_search(state->samdb, rec, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1634 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1635 talloc_free(rec);
1636 return ISC_R_NOTFOUND;
1639 /* there are existing records. We need to see if any match
1641 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1642 if (el == NULL || el->num_values == 0) {
1643 state->log(ISC_LOG_ERROR, "samba_dlz: no dnsRecord attribute for %s",
1644 ldb_dn_get_linearized(dn));
1645 talloc_free(rec);
1646 return ISC_R_FAILURE;
1649 for (i=0; i<el->num_values; i++) {
1650 struct dnsp_DnssrvRpcRecord rec2;
1652 ndr_err = ndr_pull_struct_blob(&el->values[i], rec, &rec2,
1653 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1654 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1655 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1656 ldb_dn_get_linearized(dn));
1657 talloc_free(rec);
1658 return ISC_R_FAILURE;
1661 if (b9_record_match(state, rec, &rec2)) {
1662 break;
1665 if (i == el->num_values) {
1666 talloc_free(rec);
1667 return ISC_R_NOTFOUND;
1670 if (i < el->num_values-1) {
1671 memmove(&el->values[i], &el->values[i+1], sizeof(el->values[0])*((el->num_values-1)-i));
1673 el->num_values--;
1675 if (!b9_set_session_info(state, name)) {
1676 talloc_free(rec);
1677 return ISC_R_FAILURE;
1680 if (el->num_values == 0) {
1681 el->flags = LDB_FLAG_MOD_DELETE;
1682 } else {
1683 el->flags = LDB_FLAG_MOD_REPLACE;
1685 ret = ldb_modify(state->samdb, res->msgs[0]);
1687 b9_reset_session_info(state);
1688 if (ret != LDB_SUCCESS) {
1689 state->log(ISC_LOG_ERROR, "samba_dlz: failed to modify %s - %s",
1690 ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1691 talloc_free(rec);
1692 return ISC_R_FAILURE;
1695 state->log(ISC_LOG_INFO, "samba_dlz: subtracted rdataset %s '%s'", name, rdatastr);
1697 talloc_free(rec);
1698 return ISC_R_SUCCESS;
1703 delete all records of the given type
1705 _PUBLIC_ isc_result_t dlz_delrdataset(const char *name, const char *type, void *dbdata, void *version)
1707 struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
1708 TALLOC_CTX *tmp_ctx;
1709 struct ldb_dn *dn;
1710 isc_result_t result;
1711 struct ldb_result *res;
1712 const char *attrs[] = { "dnsRecord", NULL };
1713 int ret, i;
1714 struct ldb_message_element *el;
1715 enum ndr_err_code ndr_err;
1716 enum dns_record_type dns_type;
1717 bool found = false;
1719 if (state->transaction_token != (void*)version) {
1720 state->log(ISC_LOG_ERROR, "samba_dlz: bad transaction version");
1721 return ISC_R_FAILURE;
1724 if (!b9_dns_type(type, &dns_type)) {
1725 state->log(ISC_LOG_ERROR, "samba_dlz: bad dns type %s in delete", type);
1726 return ISC_R_FAILURE;
1729 tmp_ctx = talloc_new(state);
1731 /* find the DN of the record */
1732 result = b9_find_name_dn(state, name, tmp_ctx, &dn);
1733 if (result != ISC_R_SUCCESS) {
1734 talloc_free(tmp_ctx);
1735 return result;
1738 /* get the existing records */
1739 ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, "objectClass=dnsNode");
1740 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1741 talloc_free(tmp_ctx);
1742 return ISC_R_NOTFOUND;
1745 /* there are existing records. We need to see if any match the type
1747 el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
1748 if (el == NULL || el->num_values == 0) {
1749 talloc_free(tmp_ctx);
1750 return ISC_R_NOTFOUND;
1753 for (i=0; i<el->num_values; i++) {
1754 struct dnsp_DnssrvRpcRecord rec2;
1756 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec2,
1757 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
1758 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1759 state->log(ISC_LOG_ERROR, "samba_dlz: failed to parse dnsRecord for %s",
1760 ldb_dn_get_linearized(dn));
1761 talloc_free(tmp_ctx);
1762 return ISC_R_FAILURE;
1765 if (dns_type == rec2.wType) {
1766 if (i < el->num_values-1) {
1767 memmove(&el->values[i], &el->values[i+1],
1768 sizeof(el->values[0])*((el->num_values-1)-i));
1770 el->num_values--;
1771 i--;
1772 found = true;
1776 if (!found) {
1777 talloc_free(tmp_ctx);
1778 return ISC_R_FAILURE;
1781 if (!b9_set_session_info(state, name)) {
1782 talloc_free(tmp_ctx);
1783 return ISC_R_FAILURE;
1786 if (el->num_values == 0) {
1787 el->flags = LDB_FLAG_MOD_DELETE;
1788 } else {
1789 el->flags = LDB_FLAG_MOD_REPLACE;
1791 ret = ldb_modify(state->samdb, res->msgs[0]);
1793 b9_reset_session_info(state);
1794 if (ret != LDB_SUCCESS) {
1795 state->log(ISC_LOG_ERROR, "samba_dlz: failed to delete type %s in %s - %s",
1796 type, ldb_dn_get_linearized(dn), ldb_errstring(state->samdb));
1797 talloc_free(tmp_ctx);
1798 return ISC_R_FAILURE;
1801 state->log(ISC_LOG_INFO, "samba_dlz: deleted rdataset %s of type %s", name, type);
1803 talloc_free(tmp_ctx);
1804 return ISC_R_SUCCESS;