[PATCH] ldb: remove "magic" string in ldb_controls, replace them with constants defined in ldb.h

Matthieu Patou mat at matws.net
Mon Jan 24 12:36:04 MST 2011


Allow to have less magic value in the control code and will allow not to
duplicate names when doing a function that marshal a control to it's
string representation
---
 source4/lib/ldb/common/ldb_controls.c |   52 +++++++++++++++++++--------------
 source4/lib/ldb/include/ldb.h         |   22 ++++++++++++++
 2 files changed, 52 insertions(+), 22 deletions(-)

diff --git a/source4/lib/ldb/common/ldb_controls.c b/source4/lib/ldb/common/ldb_controls.c
index e3b2870..499d4d9 100644
--- a/source4/lib/ldb/common/ldb_controls.c
+++ b/source4/lib/ldb/common/ldb_controls.c
@@ -256,8 +256,15 @@ int ldb_request_replace_control(struct ldb_request *req, const char *oid, bool c
 	return LDB_ERR_OPERATIONS_ERROR;
 }
 
-/* Parse controls from the format used on the command line and in ejs */
+/*
+ * A little trick to allow to use constants defined in headers rather than
+ * hardwritten in the file hardwritten in the file
+ * sizeof will return the \0 char as well so it will take the place of ":" in the
+ * length of the string
+ */
+#define TEST_CONTROL_IS_NAME(control, NAME) strncmp(control, NAME ":", sizeof(NAME))
 
+/* Parse controls from the format used on the command line and in ejs */
 struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char **control_strings)
 {
 	unsigned int i;
@@ -273,7 +280,8 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 	ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
 
 	for (i = 0; control_strings[i]; i++) {
-		if (strncmp(control_strings[i], "vlv:", 4) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i],
+					LDB_CONTROL_VLV_REQ_NAME) == 0) {
 			struct ldb_vlv_req_control *control;
 			const char *p;
 			char attr[1024];
@@ -330,7 +338,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "dirsync:", 8) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_DIRSYNC_NAME) == 0) {
 			struct ldb_dirsync_control *control;
 			const char *p;
 			char cookie[1024];
@@ -374,7 +382,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "asq:", 4) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_ASQ_NAME) == 0) {
 			struct ldb_asq_control *control;
 			const char *p;
 			char attr[256];
@@ -408,7 +416,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "extended_dn:", 12) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_EXTENDED_DN_NAME) == 0) {
 			struct ldb_extended_dn_control *control;
 			const char *p;
 			int crit, type, ret;
@@ -446,7 +454,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "sd_flags:", 9) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_SD_FLAGS_NAME) == 0) {
 			struct ldb_sd_flags_control *control;
 			const char *p;
 			int crit, ret;
@@ -477,7 +485,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "search_options:", 15) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_SEARCH_OPTIONS_NAME) == 0) {
 			struct ldb_search_options_control *control;
 			const char *p;
 			int crit, ret;
@@ -508,7 +516,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "bypassoperational:", 18) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_BYPASS_OPERATIONAL_OID) == 0) {
 			const char *p;
 			int crit, ret;
 
@@ -535,7 +543,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "relax:", 6) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_RELAX_NAME) == 0) {
 			const char *p;
 			int crit, ret;
 
@@ -562,7 +570,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "recalculate_sd:", 15) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_RECALCULATE_SD_NAME) == 0) {
 			const char *p;
 			int crit, ret;
 
@@ -589,7 +597,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "domain_scope:", 13) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_DOMAIN_SCOPE_NAME) == 0) {
 			const char *p;
 			int crit, ret;
 
@@ -616,7 +624,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "paged_results:", 14) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_PAGED_RESULTS_NAME) == 0) {
 			struct ldb_paged_control *control;
 			const char *p;
 			int crit, size, ret;
@@ -649,7 +657,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "server_sort:", 12) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_SERVER_SORT_NAME) == 0) {
 			struct ldb_server_sort_control **control;
 			const char *p;
 			char attr[256];
@@ -689,7 +697,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "notification:", 13) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_NOTIFICATION_NAME) == 0) {
 			const char *p;
 			int crit, ret;
 
@@ -716,7 +724,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "tree_delete:", 12) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_TREE_DELETE_NAME) == 0) {
 			const char *p;
 			int crit, ret;
 
@@ -743,7 +751,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "show_deleted:", 13) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_SHOW_DELETED_NAME) == 0) {
 			const char *p;
 			int crit, ret;
 
@@ -770,7 +778,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "show_deactivated_link:", 22) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME) == 0) {
 			const char *p;
 			int crit, ret;
 
@@ -797,7 +805,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "show_recycled:", 14) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_SHOW_RECYCLED_NAME) == 0) {
 			const char *p;
 			int crit, ret;
 
@@ -824,7 +832,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "permissive_modify:", 18) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_PERMISSIVE_MODIFY_NAME) == 0) {
 			const char *p;
 			int crit, ret;
 
@@ -851,7 +859,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "reveal_internals:", 17) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_REVEAL_INTERNALS_NAME) == 0) {
 			const char *p;
 			int crit, ret;
 
@@ -912,7 +920,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "rodc_join:", 10) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_RODC_DCPROMO_NAME) == 0) {
 			const char *p;
 			int crit, ret;
 
@@ -939,7 +947,7 @@ struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_C
 			continue;
 		}
 
-		if (strncmp(control_strings[i], "provision:", 10) == 0) {
+		if (TEST_CONTROL_IS_NAME(control_strings[i], LDB_CONTROL_PROVISION_NAME) == 0) {
 			const char *p;
 			int crit, ret;
 
diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h
index 4b1a5fb..280dba4 100644
--- a/source4/lib/ldb/include/ldb.h
+++ b/source4/lib/ldb/include/ldb.h
@@ -496,12 +496,14 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
 
 */
 #define LDB_CONTROL_RECALCULATE_SD_OID "1.3.6.1.4.1.7165.4.3.5"
+#define LDB_CONTROL_RECALCULATE_SD_NAME	"recalculate_sd"
 
 /**
    REVEAL_INTERNALS is used to reveal internal attributes and DN
    components which are not normally shown to the user
 */
 #define LDB_CONTROL_REVEAL_INTERNALS "1.3.6.1.4.1.7165.4.3.6"
+#define LDB_CONTROL_REVEAL_INTERNALS_NAME	"reveal_internals"
 
 /**
    LDB_CONTROL_AS_SYSTEM is used to skip access checks on operations
@@ -515,6 +517,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    mainly thought to be used for the provisioning.
 */
 #define LDB_CONTROL_PROVISION_OID "1.3.6.1.4.1.7165.4.3.16"
+#define LDB_CONTROL_PROVISION_NAME	"provision"
 
 /* AD controls */
 
@@ -527,6 +530,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://www.ietf.org/rfc/rfc2696.txt">RFC 2696</a>.
 */
 #define LDB_CONTROL_PAGED_RESULTS_OID	"1.2.840.113556.1.4.319"
+#define LDB_CONTROL_PAGED_RESULTS_NAME	"paged_result"
 
 /**
    OID for specifying the returned elements of the ntSecurityDescriptor
@@ -534,6 +538,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ldap/ldap/ldap_server_sd_flags_oid.asp">Microsoft documentation of this OID</a>
 */
 #define LDB_CONTROL_SD_FLAGS_OID	"1.2.840.113556.1.4.801"
+#define LDB_CONTROL_SD_FLAGS_NAME	"sd_flags"
 
 /**
    OID for specifying an advanced scope for the search (one partition)
@@ -541,6 +546,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ldap/ldap/ldap_server_domain_scope_oid.asp">Microsoft documentation of this OID</a>
 */
 #define LDB_CONTROL_DOMAIN_SCOPE_OID	"1.2.840.113556.1.4.1339"
+#define LDB_CONTROL_DOMAIN_SCOPE_NAME	"domain_scope"
 
 /**
    OID for specifying an advanced scope for a search
@@ -548,6 +554,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ldap/ldap/ldap_server_search_options_oid.asp">Microsoft documentation of this OID</a>
 */
 #define LDB_CONTROL_SEARCH_OPTIONS_OID	"1.2.840.113556.1.4.1340"
+#define LDB_CONTROL_SEARCH_OPTIONS_NAME	"search_options"
 
 /**
    OID for notification
@@ -555,6 +562,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ldap/ldap/ldap_server_notification_oid.asp">Microsoft documentation of this OID</a>
 */
 #define LDB_CONTROL_NOTIFICATION_OID	"1.2.840.113556.1.4.528"
+#define LDB_CONTROL_NOTIFICATION_NAME	"notification"
 
 /**
    OID for performing subtree deletes
@@ -562,6 +570,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://msdn.microsoft.com/en-us/library/aa366991(v=VS.85).aspx">Microsoft documentation of this OID</a>
 */
 #define LDB_CONTROL_TREE_DELETE_OID	"1.2.840.113556.1.4.805"
+#define LDB_CONTROL_TREE_DELETE_NAME	"tree_delete"
 
 /**
    OID for getting deleted objects
@@ -569,6 +578,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ldap/ldap/ldap_server_show_deleted_oid.asp">Microsoft documentation of this OID</a>
 */
 #define LDB_CONTROL_SHOW_DELETED_OID	"1.2.840.113556.1.4.417"
+#define LDB_CONTROL_SHOW_DELETED_NAME	"show_deleted"
 
 /**
    OID for getting recycled objects
@@ -576,6 +586,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://msdn.microsoft.com/en-us/library/dd304621(PROT.13).aspx">Microsoft documentation of this OID</a>
 */
 #define LDB_CONTROL_SHOW_RECYCLED_OID         "1.2.840.113556.1.4.2064"
+#define LDB_CONTROL_SHOW_RECYCLED_NAME	"show_recycled"
 
 /**
    OID for getting deactivated linked attributes
@@ -583,6 +594,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://msdn.microsoft.com/en-us/library/dd302781(PROT.13).aspx">Microsoft documentation of this OID</a>
 */
 #define LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID "1.2.840.113556.1.4.2065"
+#define LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME	"show_deactivated_link"
 
 /**
    OID for extended DN
@@ -590,6 +602,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ldap/ldap/ldap_server_extended_dn_oid.asp">Microsoft documentation of this OID</a>
 */
 #define LDB_CONTROL_EXTENDED_DN_OID	"1.2.840.113556.1.4.529"
+#define LDB_CONTROL_EXTENDED_DN_NAME	"extended_dn"
 
 /**
    OID for LDAP server sort result extension.
@@ -604,6 +617,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://www.ietf.org/rfc/rfc2891.txt">RFC 2891</a>.
 */
 #define LDB_CONTROL_SERVER_SORT_OID	"1.2.840.113556.1.4.473"
+#define LDB_CONTROL_SERVER_SORT_NAME	"server_sort"
 
 /**
    OID for LDAP server sort result response extension.
@@ -615,6 +629,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://www.ietf.org/rfc/rfc2891.txt">RFC 2891</a>.
 */
 #define LDB_CONTROL_SORT_RESP_OID	"1.2.840.113556.1.4.474"
+#define LDB_CONTROL_SORT_RESP_NAME	"server_sort_resp"
 
 /**
    OID for LDAP Attribute Scoped Query extension.
@@ -623,6 +638,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    messages as part of the controls field of the LDAPMessage.
 */
 #define LDB_CONTROL_ASQ_OID		"1.2.840.113556.1.4.1504"
+#define LDB_CONTROL_ASQ_NAME	"asq"
 
 /**
    OID for LDAP Directory Sync extension. 
@@ -631,6 +647,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    messages as part of the controls field of the LDAPMessage.
 */
 #define LDB_CONTROL_DIRSYNC_OID		"1.2.840.113556.1.4.841"
+#define LDB_CONTROL_DIRSYNC_NAME	"dirsync"
 
 
 /**
@@ -640,6 +657,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    as part of the controls field of the LDAPMessage.
 */
 #define LDB_CONTROL_VLV_REQ_OID		"2.16.840.1.113730.3.4.9"
+#define LDB_CONTROL_VLV_REQ_NAME	"vlv"
 
 /**
    OID for LDAP Virtual List View Response extension.
@@ -648,6 +666,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    as part of the controls field of the LDAPMessage.
 */
 #define LDB_CONTROL_VLV_RESP_OID	"2.16.840.1.113730.3.4.10"
+#define LDB_CONTROL_VLV_RESP_NAME	"vlv_resp"
 
 /**
    OID to let modifies don't give an error when adding an existing
@@ -656,6 +675,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ldap/ldap/ldap_server_permissive_modify_oid.asp">Microsoft documentation of this OID</a>
 */
 #define LDB_CONTROL_PERMISSIVE_MODIFY_OID	"1.2.840.113556.1.4.1413"
+#define LDB_CONTROL_PERMISSIVE_MODIFY_NAME	"permissive_modify"
 
 /** 
     OID to allow the server to be more 'fast and loose' with the data being added.  
@@ -670,6 +690,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="">Microsoft documentation of this OID</a>
 */
 #define LDB_CONTROL_RODC_DCPROMO_OID "1.2.840.113556.1.4.1341"
+#define LDB_CONTROL_RODC_DCPROMO_NAME	"rodc_join"
 
 /* Other standardised controls */
 
@@ -682,6 +703,7 @@ typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
    \sa <a href="http://opends.dev.java.net/public/standards/draft-zeilenga-ldap-managedit.txt">draft managedit</a>.
 */
 #define LDB_CONTROL_RELAX_OID "1.3.6.1.4.1.4203.666.5.12"
+#define LDB_CONTROL_RELAX_NAME	"relax"
 
 /* Extended operations */
 
-- 
1.7.1


--------------090707070100000105080709--


More information about the samba-technical mailing list