Hello,

Generalize attribute_name<->rdata_type conversions.

Attribute names are generated on-the-fly: String "Record" is appended
to textual representation of DNS RDATA type.

String "Record" is cut down from the attribute name during
attribute name to rdata type conversion.

From now, the plugin doesn't add artificial limitation to supported
record types.

--
Petr^2 Spacek
From 5d2a61fa9d98fc8eb7f7b68da4cba71a03ad0b33 Mon Sep 17 00:00:00 2001
From: Petr Spacek <pspa...@redhat.com>
Date: Mon, 8 Apr 2013 19:03:43 +0200
Subject: [PATCH] Generalize attribute_name<->rdata_type conversions.

Attribute names are generated on-the-fly: String "Record" is appended
to textual representation of DNS RDATA type.

String "Record" is cut down from the attribute name during
attribute name to rdata type conversion.

From now, the plugin doesn't add artificial limitation to supported
record types.

Signed-off-by: Petr Spacek <pspa...@redhat.com>
---
 src/ldap_convert.c | 69 ++++++++++++++++++++----------------------------------
 src/ldap_convert.h | 10 ++++++--
 src/ldap_helper.c  | 30 ++++++++++--------------
 3 files changed, 47 insertions(+), 62 deletions(-)

diff --git a/src/ldap_convert.c b/src/ldap_convert.c
index b1d9a18b4afa83a6fc10b348b1657d73a667a027..d4e5206e0a7f17500e57fe65f8d1f5d04c97b501 100644
--- a/src/ldap_convert.c
+++ b/src/ldap_convert.c
@@ -24,7 +24,6 @@
 #include <isc/string.h>
 
 #include <dns/name.h>
-#include <dns/rdatatype.h>
 #include <dns/result.h>
 #include <dns/types.h>
 
@@ -41,28 +40,6 @@
 #include "util.h"
 #include "zone_register.h"
 
-/*
- * Consistency must be preserved in these tables.
- * ldap_dns_records[i] must always corespond to dns_records[i]
- */
-const char *ldap_dns_records[] = {
-	"ARecord",     "AAAARecord",  "A6Record",    "NSRecord",
-	"CNAMERecord", "PTRRecord",   "SRVRecord",   "TXTRecord",   "MXRecord",
-	"MDRecord",    "HINFORecord", "MINFORecord", "AFSDBRecord", "SIGRecord",
-	"KEYRecord",   "LOCRecord",   "NXTRecord",   "NAPTRRecord", "KXRecord",
-	"CERTRecord",  "DNAMERecord", "DSRecord",    "SSHFPRecord",
-	"RRSIGRecord", "NSECRecord",  NULL
-};
-
-const char *dns_records[] = {
-	"A",     "AAAA",  "A6",    "NS",
-	"CNAME", "PTR",   "SRV",   "TXT",   "MX",
-	"MD",    "HINFO", "MINFO", "AFSDB", "SIG",
-	"KEY",   "LOC",   "NXT",   "NAPTR", "KX",
-	"CERT",  "DNAME", "DS",    "SSHFP",
-	"RRSIG", "NSEC",  NULL
-};
-
 static isc_result_t explode_dn(const char *dn, char ***explodedp, int notypes);
 static isc_result_t explode_rdn(const char *rdn, char ***explodedp,
 				int notypes);
@@ -436,45 +413,51 @@ cleanup:
 	return result;
 }
 
+/**
+ * Convert attribute name to dns_rdatatype.
+ *
+ * @param[in]  ldap_attribute String with attribute name terminated by \0.
+ * @param[out] rdtype
+ */
 isc_result_t
 ldap_attribute_to_rdatatype(const char *ldap_attribute, dns_rdatatype_t *rdtype)
 {
 	isc_result_t result;
-	unsigned i;
+	unsigned len;
 	isc_consttextregion_t region;
 
-	for (i = 0; ldap_dns_records[i] != NULL; i++) {
-		if (!strcasecmp(ldap_attribute, ldap_dns_records[i]))
-			break;
-	}
-	if (dns_records[i] == NULL)
-		return ISC_R_NOTFOUND;
+	len = strlen(ldap_attribute);
+	if (len <= LDAP_RDATATYPE_SUFFIX_LEN)
+		return ISC_R_UNEXPECTEDEND;
 
-	region.base = dns_records[i];
-	region.length = strlen(region.base);
+	/* Does attribute name end with RECORD_SUFFIX? */
+	if (strcasecmp(ldap_attribute + len - LDAP_RDATATYPE_SUFFIX_LEN, LDAP_RDATATYPE_SUFFIX))
+		return ISC_R_UNEXPECTED;
+
+	region.base = ldap_attribute;
+	region.length = len - LDAP_RDATATYPE_SUFFIX_LEN;
 	result = dns_rdatatype_fromtext(rdtype, (isc_textregion_t *)&region);
 	if (result != ISC_R_SUCCESS)
-		log_error("dns_rdatatype_fromtext() failed");
+		log_error_r("dns_rdatatype_fromtext() failed for attribute '%s'",
+			    ldap_attribute);
 
 	return result;
 }
 
 isc_result_t
-rdatatype_to_ldap_attribute(dns_rdatatype_t rdtype, const char **target)
+rdatatype_to_ldap_attribute(dns_rdatatype_t rdtype, char *target,
+			    unsigned int size)
 {
-	unsigned i;
+	isc_result_t result;
 	char rdtype_str[DNS_RDATATYPE_FORMATSIZE];
 
 	dns_rdatatype_format(rdtype, rdtype_str, DNS_RDATATYPE_FORMATSIZE);
-	for (i = 0; dns_records[i] != NULL; i++) {
-		if (!strcmp(rdtype_str, dns_records[i]))
-			break;
-	}
-	if (ldap_dns_records[i] == NULL)
-		return ISC_R_NOTFOUND;
-
-	*target = ldap_dns_records[i];
+	CHECK(isc_string_copy(target, size, rdtype_str));
+	CHECK(isc_string_append(target, size, LDAP_RDATATYPE_SUFFIX));
 
 	return ISC_R_SUCCESS;
+
+cleanup:
+	return result;
 }
 
diff --git a/src/ldap_convert.h b/src/ldap_convert.h
index bcbe395dc3de684de62341f436310e34ee0d90ee..29f6f7fa41d69575e012b847fcef2b268331c2e6 100644
--- a/src/ldap_convert.h
+++ b/src/ldap_convert.h
@@ -22,10 +22,15 @@
 #define _LD_LDAP_CONVERT_H_
 
 #include <dns/types.h>
+#include <dns/rdatatype.h>
 
 #include "str.h"
 #include "zone_register.h"
 
+#define LDAP_RDATATYPE_SUFFIX     "Record"
+#define LDAP_RDATATYPE_SUFFIX_LEN (sizeof(LDAP_RDATATYPE_SUFFIX) - 1)
+#define LDAP_RDATATYPE_FORMATSIZE DNS_RDATATYPE_FORMATSIZE + LDAP_RDATATYPE_SUFFIX_LEN + 1
+
 /*
  * Convert LDAP DN 'dn', to dns_name_t 'target'. 'target' needs to be
  * initialized with dns_name_init() before the call and freed by the caller
@@ -41,8 +46,9 @@ isc_result_t dnsname_to_dn(zone_register_t *zr, dns_name_t *name,
 isc_result_t ldap_attribute_to_rdatatype(const char *ldap_record,
 				      dns_rdatatype_t *rdtype);
 
-isc_result_t rdatatype_to_ldap_attribute(dns_rdatatype_t rdtype,
-					 const char **target);
+isc_result_t
+rdatatype_to_ldap_attribute(dns_rdatatype_t rdtype, char *target,
+			    unsigned int size);
 
 isc_result_t dn_to_text(const char *dn, ld_string_t *target,
 			ld_string_t *origin);
diff --git a/src/ldap_helper.c b/src/ldap_helper.c
index b59fd8e90cd225f7e45ce49a379d77dbc277a6c9..385bc4710e9c431904ab99b2405b34c69ea8775d 100644
--- a/src/ldap_helper.c
+++ b/src/ldap_helper.c
@@ -2601,31 +2601,26 @@ ldap_rdatalist_to_ldapmod(isc_mem_t *mctx, dns_rdatalist_t *rdlist,
 	isc_result_t result;
 	LDAPMod *change = NULL;
 	char **vals = NULL;
-	const char *attr_name_c;
-	char *attr_name;
-
 
 	REQUIRE(changep != NULL && *changep == NULL);
 
 	CHECKED_MEM_GET_PTR(mctx, change);
 	ZERO_PTR(change);
+	CHECKED_MEM_GET(mctx, change->mod_type, LDAP_RDATATYPE_FORMATSIZE);
 
-	result = rdatatype_to_ldap_attribute(rdlist->type, &attr_name_c);
-	if (result != ISC_R_SUCCESS) {
-		result = ISC_R_FAILURE;
-		goto cleanup;
-	}
-	DE_CONST(attr_name_c, attr_name);
+	CHECK(rdatatype_to_ldap_attribute(rdlist->type, change->mod_type,
+					  LDAP_RDATATYPE_FORMATSIZE));
 	CHECK(ldap_rdata_to_char_array(mctx, HEAD(rdlist->rdata), &vals));
 
 	change->mod_op = mod_op;
-	change->mod_type = attr_name;
 	change->mod_values = vals;
 
 	*changep = change;
 	return ISC_R_SUCCESS;
 
 cleanup:
+	if (change != NULL && change->mod_type != NULL)
+		SAFE_MEM_PUT(mctx, change->mod_type, LDAP_RDATATYPE_FORMATSIZE);
 	free_ldapmod(mctx, &change);
 
 	return result;
@@ -3011,15 +3006,12 @@ modify_ldap_common(dns_name_t *owner, ldap_instance_t *ldap_inst,
 		char **vals = NULL;
 		CHECKED_MEM_GET_PTR(mctx, change_ptr);
 		ZERO_PTR(change_ptr);
+		CHECKED_MEM_GET(mctx, change_ptr->mod_type, LDAP_RDATATYPE_FORMATSIZE);
 
 		/* Do the same action what has been done with A/AAAA record. */	
 		change_ptr->mod_op = mod_op;
-		char *attr_name;
-		const char *attr_name_c;
-		CHECK(rdatatype_to_ldap_attribute(dns_rdatatype_ptr, &attr_name_c));
-		
-		DE_CONST(attr_name_c, attr_name);
-		change_ptr->mod_type = attr_name;  
+		CHECK(rdatatype_to_ldap_attribute(dns_rdatatype_ptr, change_ptr->mod_type,
+						  LDAP_RDATATYPE_FORMATSIZE));
 
 		CHECKED_MEM_ALLOCATE(mctx, vals, 2 * sizeof(char *));
 		memset(vals, 0, 2 * sizeof(char *));
@@ -3046,9 +3038,13 @@ cleanup:
 	str_destroy(&owner_dn_ptr);
 	str_destroy(&owner_dn);
 	str_destroy(&str_ptr);
+	if (change[0] != NULL)
+		SAFE_MEM_PUT(mctx, change[0]->mod_type, LDAP_RDATATYPE_FORMATSIZE);
 	free_ldapmod(mctx, &change[0]);
 	free_ldapmod(mctx, &change[1]);
-	if (change_ptr != NULL) free_ldapmod(mctx, &change_ptr);
+	if (change_ptr != NULL)
+		SAFE_MEM_PUT(mctx, change_ptr->mod_type, LDAP_RDATATYPE_FORMATSIZE);
+	free_ldapmod(mctx, &change_ptr);
 	ldapdb_rdatalist_destroy(mctx, &rdlist_search);
 	free_char_array(mctx, &vals);
 	dns_name_free(&zone_name, mctx);
-- 
1.7.11.7

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to