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