s4:dsdb Make parentGUID handler use dsdb_module_search_dn()
authorAndrew Bartlett <abartlet@samba.org>
Thu, 3 Dec 2009 23:15:15 +0000 (10:15 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 7 Dec 2009 02:07:03 +0000 (13:07 +1100)
This avoids doing a new search from the top of the module stack.

This also removes the helper function dsdb_find_parentguid_by_dn()
which is now unused.

Andrew Bartlett

source4/dsdb/common/util.c
source4/dsdb/samdb/ldb_modules/operational.c

index 8c9c98201b33dba0a2de7d33e44df9392121a62e..d9e03cec3ef10d2f7163462580c6f9c7de8d00c2 100644 (file)
@@ -2286,64 +2286,6 @@ int dsdb_find_guid_by_dn(struct ldb_context *ldb,
 }
 
 
-/*
-  Use a DN to find it's parentGUID
-
-  Results
-   LDB_ERR_OPERATIONS_ERROR for out of memory
-   LDB_ERR_NO_SUCH_OBJECT if there is no parent object for the given DN
-   LDB_ERR_NO_SUCH_ATTRIBUTE if couldn't get the ObjectGUID from the parent
-   LDB_SUCCESS if it could find the parentGUID correctly
- */
-int dsdb_find_parentguid_by_dn(struct ldb_context *ldb,
-                       struct ldb_dn *dn,
-                       struct GUID *parent_guid)
-{
-
-       int ret;
-       struct ldb_result *res;
-       struct ldb_dn *parent_dn;
-       const char *attrs[] = { "objectGUID", NULL };
-       TALLOC_CTX *tmp_ctx = talloc_new(ldb);
-
-
-       parent_dn = ldb_dn_get_parent(tmp_ctx, dn);
-
-       if (parent_dn == NULL){
-               DEBUG(4,(__location__ ": Failed to find parent for dn %s\n",
-                                        ldb_dn_get_linearized(dn)));
-               ret = LDB_ERR_NO_SUCH_OBJECT;
-               goto done;
-       }
-
-       /*
-               The few lines of code bellow are very similar to the
-               dsdb_find_guid_by_dn() function implementation, but this way we can
-               differ situations when the parent_dn doesn't exist from when there is
-               an error on returning it's GUID.
-        */
-       ret = dsdb_search_dn_with_deleted(ldb, tmp_ctx, &res, parent_dn, attrs);
-       if (ret != LDB_SUCCESS) {
-               DEBUG(4,(__location__ ": Parent dn for %s does not exist \n",
-                                                        ldb_dn_get_linearized(dn)));
-               /* When there is no parent dn, it simply doesn't return a parentGUID  */
-               ret = LDB_ERR_NO_SUCH_OBJECT;
-               goto done;
-       }
-       if (res->count < 1) {
-               DEBUG(4,(__location__ ": Failed to find GUID for dn %s\n",
-                                        ldb_dn_get_linearized(parent_dn)));
-               ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
-               goto done;
-       }
-
-       *parent_guid = samdb_result_guid(res->msgs[0], "objectGUID");
-       ret = LDB_SUCCESS;
-
-done:
-       talloc_free(tmp_ctx);
-       return ret;
-}
 
 /*
  adds the given GUID to the given ldb_message. This value is added
index d43950e6bbe8e443b703fecbbb85cc9ea83b3b1c..671a178e335f891281d2bee6dbd466f7129f4f09 100644 (file)
@@ -70,6 +70,7 @@
 #include "librpc/gen_ndr/ndr_misc.h"
 #include "param/param.h"
 #include "dsdb/samdb/samdb.h"
+#include "dsdb/samdb/ldb_modules/util.h"
 
 #ifndef ARRAY_SIZE
 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
@@ -118,32 +119,42 @@ static int construct_primary_group_token(struct ldb_module *module,
 }
 
 static int construct_parent_guid(struct ldb_module *module,
-               struct ldb_message *msg)
+                                struct ldb_message *msg)
 {
-       struct ldb_context *ldb;
-       struct GUID parent_guid;
+       struct ldb_result *res;
+       const struct ldb_val *parent_guid;
+       const char *attrs[] = { "objectGUID", NULL };
        int ret;
 
-       ldb = ldb_module_get_ctx(module);
-
-       ret = dsdb_find_parentguid_by_dn(ldb, msg->dn, &parent_guid);
-
-
-       if (ret != LDB_SUCCESS){
-
-               /* if there is no parentGUID for this object, then return */
-               if (ret == LDB_ERR_NO_SUCH_OBJECT){
-                       return LDB_SUCCESS;
-               }else{
-                       return ret;
-               }
+       /* TODO:  In the future, this needs to honour the partition boundaries */
+       struct ldb_dn *parent_dn = ldb_dn_get_parent(msg, msg->dn);
 
+       if (parent_dn == NULL){
+               DEBUG(4,(__location__ ": Failed to find parent for dn %s\n",
+                                        ldb_dn_get_linearized(msg->dn)));
+               return LDB_SUCCESS;
        }
 
-       ret = dsdb_msg_add_guid(msg, &parent_guid, "parentGUID");
+       ret = dsdb_module_search_dn(module, msg, &res, parent_dn, attrs, DSDB_SEARCH_SHOW_DELETED);
+       talloc_free(parent_dn);
+       /* if there is no parentGUID for this object, then return */
+       if (ret == LDB_ERR_NO_SUCH_OBJECT){
+               DEBUG(4,(__location__ ": Parent dn for %s does not exist \n",
+                        ldb_dn_get_linearized(msg->dn)));
+               return LDB_SUCCESS;
+       } else if (ret != LDB_SUCCESS) {
+               return ret;
+       }
 
-       return ret;
+       parent_guid = ldb_msg_find_ldb_val(res->msgs[0], "objectGUID");
+       if (!parent_guid) {
+               talloc_free(res);
+               return LDB_SUCCESS;
+       }
 
+       talloc_steal(msg->elements, parent_guid->data);
+       talloc_free(res);
+       return ldb_msg_add_value(msg, "parentGUID", parent_guid, 0);
 }
 
 /*