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