s4:ldb Add new function to create a cut down list of controls
authorAndrew Bartlett <abartlet@samba.org>
Wed, 21 Oct 2009 04:20:26 +0000 (15:20 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 21 Oct 2009 11:43:56 +0000 (22:43 +1100)
This I hope will be useful for removing controls from the ldb_reply

Andrew Bartlett

source4/lib/ldb/common/ldb_controls.c
source4/lib/ldb/include/ldb_module.h

index 0ecb1eb62cfed8b7f0dda79922a33de2cfccf927..5276ac091da4b45d1428379652faa452fc0a5531 100644 (file)
@@ -102,6 +102,47 @@ int save_controls(struct ldb_control *exclude, struct ldb_request *req, struct l
        return 1;
 }
 
+/* Returns a list of controls, except the one specified.  Included
+ * controls become a child of returned list if they were children of
+ * controls_in */
+struct ldb_control **controls_except_specified(struct ldb_control **controls_in, 
+                                              TALLOC_CTX *mem_ctx, 
+                                              struct ldb_control *exclude)
+{
+       struct ldb_control **lcs = NULL;
+       int i, j;
+
+       for (i = 0; controls_in && controls_in[i]; i++);
+
+       if (i == 0) {
+               return NULL;
+       }
+
+       for (i = 0, j = 0; controls_in && controls_in[i]; i++) {
+               if (exclude == controls_in[i]) continue;
+
+               if (!lcs) {
+                       /* Allocate here so if we remove the only
+                        * control, or there were no controls, we
+                        * don't allocate at all, and just return
+                        * NULL */
+                       lcs = talloc_array(mem_ctx, struct ldb_control *, i);
+                       if (!lcs) {
+                               return NULL;
+                       }
+               }
+
+               lcs[j] = controls_in[i];
+               talloc_reparent(controls_in, lcs, lcs[j]);
+               j++;
+       }
+       if (lcs) {
+               lcs[j] = NULL;
+       }
+
+       return lcs;
+}
+
 /* check if there's any control marked as critical in the list */
 /* return True if any, False if none */
 int check_critical_controls(struct ldb_control **controls)
index 7a125ba211e8a95d92cc99f5de0b162c8c6fae3e..6061c4d240ab2a1cec1a2000e38bbbf664f4ed8a 100644 (file)
@@ -101,6 +101,12 @@ void ldb_schema_attribute_set_override_handler(struct ldb_context *ldb,
 /* The following definitions come from lib/ldb/common/ldb_controls.c  */
 struct ldb_control *get_control_from_list(struct ldb_control **controls, const char *oid);
 int save_controls(struct ldb_control *exclude, struct ldb_request *req, struct ldb_control ***saver);
+/* Returns a list of controls, except the one specified.  Included
+ * controls become a child of returned list if they were children of
+ * controls_in */
+struct ldb_control **controls_except_specified(struct ldb_control **controls_in, 
+                                              TALLOC_CTX *mem_ctx, 
+                                              struct ldb_control *exclude);
 int check_critical_controls(struct ldb_control **controls);
 
 /* The following definitions come from lib/ldb/common/ldb_ldif.c  */