a7203a
From 88096745d1ef1798854e0c8319b5ae015f045fe3 Mon Sep 17 00:00:00 2001
a7203a
From: Alexander Bokovoy <abokovoy@redhat.com>
a7203a
Date: Wed, 1 May 2019 09:24:24 +0300
a7203a
Subject: [PATCH] Move recognition of a templated attribute to
a7203a
 ldap_attribute_to_rdatatype
a7203a
a7203a
When substitution of a templated entry attribute fails, we need to fall
a7203a
back to a static definition of the attribute from the same entry. This
a7203a
means, however, that ldap_attribute_to_rdatatype() will attempt to parse
a7203a
an attribute value anyway and will be confused by the templating prefix,
a7203a
thus reporting in named's logs:
a7203a
a7203a
unsupported operation: object class in resource record template DN
a7203a
'idnsname=$NAME,idnsname=$ZONE.,cn=dns,$BASEDN' changed:
a7203a
rndc reload might be necessary
a7203a
a7203a
Move recognition of a template attribute name to
a7203a
ldap_attribute_to_rdatatype() so that a proper attribute class is
a7203a
correctly derived and ignore templated attribute in the fallback code
a7203a
if case that template expansion is failed.
a7203a
a7203a
Resolves: rhbz#1705072
a7203a
---
a7203a
 src/ldap_convert.c | 33 +++++++++++++++++++++++++--------
a7203a
 src/ldap_convert.h |  2 ++
a7203a
 src/ldap_helper.c  | 21 ++++++++++++++-------
a7203a
 3 files changed, 41 insertions(+), 15 deletions(-)
a7203a
a7203a
diff --git a/src/ldap_convert.c b/src/ldap_convert.c
a7203a
index 002a679..6e24c81 100644
a7203a
--- a/src/ldap_convert.c
a7203a
+++ b/src/ldap_convert.c
a7203a
@@ -372,23 +372,40 @@ ldap_attribute_to_rdatatype(const char *ldap_attribute, dns_rdatatype_t *rdtype)
a7203a
 {
a7203a
 	isc_result_t result;
a7203a
 	unsigned len;
a7203a
+	const char *attribute = NULL;
a7203a
 	isc_consttextregion_t region;
a7203a
 
a7203a
 	len = strlen(ldap_attribute);
a7203a
 	if (len <= LDAP_RDATATYPE_SUFFIX_LEN)
a7203a
 		return ISC_R_UNEXPECTEDEND;
a7203a
 
a7203a
+
a7203a
+	/* Before looking up rdtype, we need to see if rdtype is
a7203a
+	 * an LDAP subtype (type;subtype) and if so, strip one of
a7203a
+	 * the known prefixes. We also need to remove 'record' suffix
a7203a
+	 * if it exists. The resulting rdtype text name should have no
a7203a
+	 * 'extra' details: A, AAAA, CNAME, etc. */
a7203a
+	attribute = ldap_attribute;
a7203a
+
a7203a
+	/* Does attribute name start with with TEMPLATE_PREFIX? */
a7203a
+	if (strncasecmp(LDAP_RDATATYPE_TEMPLATE_PREFIX,
a7203a
+			ldap_attribute,
a7203a
+			LDAP_RDATATYPE_TEMPLATE_PREFIX_LEN) == 0) {
a7203a
+		attribute = ldap_attribute + LDAP_RDATATYPE_TEMPLATE_PREFIX_LEN;
a7203a
+		len -= LDAP_RDATATYPE_TEMPLATE_PREFIX_LEN;
a7203a
+	/* Does attribute name start with with UNKNOWN_PREFIX? */
a7203a
+	} else if (strncasecmp(LDAP_RDATATYPE_UNKNOWN_PREFIX,
a7203a
+			       ldap_attribute,
a7203a
+			       LDAP_RDATATYPE_UNKNOWN_PREFIX_LEN) == 0) {
a7203a
+		attribute = ldap_attribute + LDAP_RDATATYPE_UNKNOWN_PREFIX_LEN;
a7203a
+		len -= LDAP_RDATATYPE_UNKNOWN_PREFIX_LEN;
a7203a
+	}
a7203a
+
a7203a
 	/* Does attribute name end with RECORD_SUFFIX? */
a7203a
-	if (strcasecmp(ldap_attribute + len - LDAP_RDATATYPE_SUFFIX_LEN,
a7203a
+	if (strcasecmp(attribute + len - LDAP_RDATATYPE_SUFFIX_LEN,
a7203a
 		       LDAP_RDATATYPE_SUFFIX) == 0) {
a7203a
-		region.base = ldap_attribute;
a7203a
+		region.base = attribute;
a7203a
 		region.length = len - LDAP_RDATATYPE_SUFFIX_LEN;
a7203a
-	/* Does attribute name start with with UNKNOWN_PREFIX? */
a7203a
-	} else if (strncasecmp(ldap_attribute,
a7203a
-			      LDAP_RDATATYPE_UNKNOWN_PREFIX,
a7203a
-			      LDAP_RDATATYPE_UNKNOWN_PREFIX_LEN) == 0) {
a7203a
-		region.base = ldap_attribute + LDAP_RDATATYPE_UNKNOWN_PREFIX_LEN;
a7203a
-		region.length = len - LDAP_RDATATYPE_UNKNOWN_PREFIX_LEN;
a7203a
 	} else
a7203a
 		return ISC_R_UNEXPECTED;
a7203a
 
a7203a
diff --git a/src/ldap_convert.h b/src/ldap_convert.h
a7203a
index 47ac947..fcd575b 100644
a7203a
--- a/src/ldap_convert.h
a7203a
+++ b/src/ldap_convert.h
a7203a
@@ -17,6 +17,8 @@
a7203a
 #define LDAP_RDATATYPE_SUFFIX_LEN	(sizeof(LDAP_RDATATYPE_SUFFIX) - 1)
a7203a
 #define LDAP_RDATATYPE_UNKNOWN_PREFIX	"UnknownRecord;"
a7203a
 #define LDAP_RDATATYPE_UNKNOWN_PREFIX_LEN	(sizeof(LDAP_RDATATYPE_UNKNOWN_PREFIX) - 1)
a7203a
+#define LDAP_RDATATYPE_TEMPLATE_PREFIX "idnsTemplateAttribute;"
a7203a
+#define LDAP_RDATATYPE_TEMPLATE_PREFIX_LEN	(sizeof(LDAP_RDATATYPE_TEMPLATE_PREFIX) - 1)
a7203a
 
a7203a
 /*
a7203a
  * Convert LDAP DN 'dn', to dns_name_t 'target'. 'target' needs to be
a7203a
diff --git a/src/ldap_helper.c b/src/ldap_helper.c
a7203a
index 8b486ae..7f70ee3 100644
a7203a
--- a/src/ldap_helper.c
a7203a
+++ b/src/ldap_helper.c
a7203a
@@ -2396,7 +2396,7 @@ ldap_substitute_rr_template(isc_mem_t *mctx, const settings_set_t * set,
a7203a
 		result = setting_find(setting_name, set, isc_boolean_true,
a7203a
 				      isc_boolean_true, &setting);
a7203a
 		if (result != ISC_R_SUCCESS) {
a7203a
-			log_debug(3, "setting '%s' is not defined so it "
a7203a
+			log_debug(5, "setting '%s' is not defined so it "
a7203a
 				  "cannot be substituted into template '%s'",
a7203a
 				  setting_name, str_buf(orig_val));
a7203a
 			CLEANUP_WITH(ISC_R_IGNORE);
a7203a
@@ -2459,23 +2459,22 @@ ldap_parse_rrentry_template(isc_mem_t *mctx, ldap_entry_t *entry,
a7203a
 	dns_rdatatype_t rdtype;
a7203a
 	dns_rdatalist_t *rdlist = NULL;
a7203a
 	isc_boolean_t did_something = ISC_FALSE;
a7203a
-	static const char prefix[] = "idnsTemplateAttribute;";
a7203a
-	static const char prefix_len = sizeof(prefix) - 1;
a7203a
 
a7203a
 	CHECK(str_new(mctx, &orig_val));
a7203a
 	rdclass = ldap_entry_getrdclass(entry);
a7203a
 	ttl = ldap_entry_getttl(entry, settings);
a7203a
 
a7203a
 	while ((attr = ldap_entry_nextattr(entry)) != NULL) {
a7203a
-		if (strncasecmp(prefix, attr->name, prefix_len) != 0)
a7203a
+		if (strncasecmp(LDAP_RDATATYPE_TEMPLATE_PREFIX,
a7203a
+				attr->name,
a7203a
+				LDAP_RDATATYPE_TEMPLATE_PREFIX_LEN) != 0)
a7203a
 			continue;
a7203a
 
a7203a
-		result = ldap_attribute_to_rdatatype(attr->name + prefix_len,
a7203a
-						     &rdtype);
a7203a
+		result = ldap_attribute_to_rdatatype(attr->name, &rdtype);
a7203a
 		if (result != ISC_R_SUCCESS) {
a7203a
 			log_bug("%s: substitution into '%s' is not supported",
a7203a
 				ldap_entry_logname(entry),
a7203a
-				attr->name + prefix_len);
a7203a
+				attr->name + LDAP_RDATATYPE_TEMPLATE_PREFIX_LEN);
a7203a
 			continue;
a7203a
 		}
a7203a
 
a7203a
@@ -2559,6 +2558,14 @@ ldap_parse_rrentry(isc_mem_t *mctx, ldap_entry_t *entry, dns_name_t *origin,
a7203a
 	for (result = ldap_entry_firstrdtype(entry, &attr, &rdtype);
a7203a
 	     result == ISC_R_SUCCESS;
a7203a
 	     result = ldap_entry_nextrdtype(entry, &attr, &rdtype)) {
a7203a
+		/* If we reached this point and found a template attribute,
a7203a
+		 * skip it because it was not translated above due to missing
a7203a
+		 * defaults or some other errors. */
a7203a
+		if (((entry->class & LDAP_ENTRYCLASS_TEMPLATE) != 0) &&
a7203a
+		    strncasecmp(LDAP_RDATATYPE_TEMPLATE_PREFIX,
a7203a
+				attr->name,
a7203a
+				LDAP_RDATATYPE_TEMPLATE_PREFIX_LEN) == 0)
a7203a
+			continue;
a7203a
 
a7203a
 		CHECK(findrdatatype_or_create(mctx, rdatalist, rdclass,
a7203a
 					      rdtype, ttl, &rdlist));
a7203a
-- 
a7203a
2.21.0
a7203a