ldb: added ldb_dn_replace_components()
authorAndrew Tridgell <tridge@samba.org>
Mon, 1 Aug 2011 02:24:13 +0000 (12:24 +1000)
committerAndrew Tridgell <tridge@samba.org>
Thu, 4 Aug 2011 06:17:24 +0000 (16:17 +1000)
this allows you to replace the string part of a DN with the string
part from another DN. This is useful when you want to fix a DN that
has the right GUID but the wrong string part, because the target
object has moved.

Pair-Programmed-With: Andrew Bartlett <abartlet@samba.org>
Pair-Programmed-With: Amitay Isaacs <amitay@gmail.com>

lib/ldb/common/ldb_dn.c
lib/ldb/include/ldb_module.h

index 6b1ef0306f23e00633f015c013a4762a2b9f2349..b910489a8e8f1bcf4b9bef9cf57530381f8003d8 100644 (file)
@@ -1684,6 +1684,62 @@ bool ldb_dn_remove_child_components(struct ldb_dn *dn, unsigned int num)
        return true;
 }
 
+
+/* replace the components of a DN with those from another DN, without
+ * touching the extended components
+ *
+ * return true if successful and false if not
+ * if false is returned the dn may be marked invalid
+ */
+bool ldb_dn_replace_components(struct ldb_dn *dn, struct ldb_dn *new_dn)
+{
+       int i;
+
+       if ( ! ldb_dn_validate(dn) || ! ldb_dn_validate(new_dn)) {
+               return false;
+       }
+
+       /* free components */
+       for (i = 0; i < dn->comp_num; i++) {
+               LDB_FREE(dn->components[i].name);
+               LDB_FREE(dn->components[i].value.data);
+               LDB_FREE(dn->components[i].cf_name);
+               LDB_FREE(dn->components[i].cf_value.data);
+       }
+
+       dn->components = talloc_realloc(dn,
+                                       dn->components,
+                                       struct ldb_dn_component,
+                                       new_dn->comp_num);
+       if (dn->components == NULL) {
+               ldb_dn_mark_invalid(dn);
+               return false;
+       }
+
+       dn->comp_num = new_dn->comp_num;
+       dn->valid_case = new_dn->valid_case;
+
+       for (i = 0; i < dn->comp_num; i++) {
+               dn->components[i] = ldb_dn_copy_component(dn->components, &new_dn->components[i]);
+               if (dn->components[i].name == NULL) {
+                       ldb_dn_mark_invalid(dn);
+                       return false;
+               }
+       }
+       if (new_dn->linearized == NULL) {
+               dn->linearized = NULL;
+       } else {
+               dn->linearized = talloc_strdup(dn, new_dn->linearized);
+               if (dn->linearized == NULL) {
+                       ldb_dn_mark_invalid(dn);
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+
 struct ldb_dn *ldb_dn_get_parent(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
 {
        struct ldb_dn *new_dn;
index 6d6fff251cd50064f6e455932d5afd971386d51a..22d24dbdeae3f6efa45394be23cdcdf0b0317e52 100644 (file)
@@ -340,5 +340,12 @@ int ldb_module_init_chain(struct ldb_context *ldb, struct ldb_module *module);
  */
 int ldb_init_module(const char *version);
 
+/* replace the components of a DN with those from another DN, without
+ * touching the extended components
+ *
+ * return true if successful and false if not
+ * if false is returned the dn may be marked invalid
+ */
+bool ldb_dn_replace_components(struct ldb_dn *dn, struct ldb_dn *new_dn);
 
 #endif