s4:dsdb/common: add samdb_domain_guid() helper function
authorStefan Metzmacher <metze@samba.org>
Tue, 28 Aug 2018 09:52:27 +0000 (11:52 +0200)
committerStefan Metzmacher <metze@samba.org>
Wed, 5 Sep 2018 11:31:42 +0000 (13:31 +0200)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=11517

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
(cherry picked from commit 0e442e094240abbf79aaca00a9d1a053a200a7e8)

source4/dsdb/common/util.c

index 3b416051ac17c183572a947584c12b5b31255f1b..64f27c1565bc8f59b03718f92c7dedab4b9ed6e6 100644 (file)
@@ -1294,6 +1294,61 @@ failed:
        return false;
 }
 
+/*
+  work out the domain guid for the current open ldb
+*/
+const struct GUID *samdb_domain_guid(struct ldb_context *ldb)
+{
+       TALLOC_CTX *tmp_ctx = NULL;
+       struct GUID *domain_guid = NULL;
+       const char *attrs[] = {
+               "objectGUID",
+               NULL
+       };
+       struct ldb_result *res = NULL;
+       int ret;
+
+       /* see if we have a cached copy */
+       domain_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.domain_guid");
+       if (domain_guid) {
+               return domain_guid;
+       }
+
+       tmp_ctx = talloc_new(ldb);
+       if (tmp_ctx == NULL) {
+               goto failed;
+       }
+
+       ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectGUID=*");
+       if (ret != LDB_SUCCESS) {
+               goto failed;
+       }
+
+       if (res->count != 1) {
+               goto failed;
+       }
+
+       domain_guid = talloc(tmp_ctx, struct GUID);
+       if (domain_guid == NULL) {
+               goto failed;
+       }
+       *domain_guid = samdb_result_guid(res->msgs[0], "objectGUID");
+
+       /* cache the domain_sid in the ldb */
+       if (ldb_set_opaque(ldb, "cache.domain_guid", domain_guid) != LDB_SUCCESS) {
+               goto failed;
+       }
+
+       talloc_steal(ldb, domain_guid);
+       talloc_free(tmp_ctx);
+
+       return domain_guid;
+
+failed:
+       talloc_free(tmp_ctx);
+       return NULL;
+}
+
 bool samdb_set_ntds_settings_dn(struct ldb_context *ldb, struct ldb_dn *ntds_settings_dn_in)
 {
        TALLOC_CTX *tmp_ctx;