dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0014-sysdb-add-functions-to-get-set-client-site.patch

ced1f5
From fafc90b8c225fd77e30e94d985c72f5f2980e59e Mon Sep 17 00:00:00 2001
ced1f5
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
ced1f5
Date: Tue, 24 Oct 2017 12:09:39 +0200
ced1f5
Subject: [PATCH 14/21] sysdb: add functions to get/set client site
ced1f5
ced1f5
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
ced1f5
(cherry picked from commit e16539779668dacff868999bd59dbf33e3eab872)
ced1f5
---
ced1f5
 src/db/sysdb.h                           |  10 +++
ced1f5
 src/db/sysdb_subdomains.c                | 108 +++++++++++++++++++++++++++++++
ced1f5
 src/tests/cmocka/test_sysdb_subdomains.c |  28 ++++++++
ced1f5
 3 files changed, 146 insertions(+)
ced1f5
ced1f5
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
ced1f5
index fbbe321072385bd43353ef2f7d0e30667887d128..4192f9085d941814eccd2ac60ce8fb6d4e1bfa67 100644
ced1f5
--- a/src/db/sysdb.h
ced1f5
+++ b/src/db/sysdb.h
ced1f5
@@ -154,6 +154,7 @@
ced1f5
 #define SYSDB_SUBDOMAIN_FOREST "memberOfForest"
ced1f5
 #define SYSDB_SUBDOMAIN_TRUST_DIRECTION "trustDirection"
ced1f5
 #define SYSDB_UPN_SUFFIXES "upnSuffixes"
ced1f5
+#define SYSDB_SITE "site"
ced1f5
 
ced1f5
 #define SYSDB_BASE_ID "baseID"
ced1f5
 #define SYSDB_ID_RANGE_SIZE "idRangeSize"
ced1f5
@@ -509,6 +510,15 @@ errno_t sysdb_domain_update_domain_resolution_order(
ced1f5
                                         const char *domain_name,
ced1f5
                                         const char *domain_resolution_order);
ced1f5
 
ced1f5
+errno_t
ced1f5
+sysdb_get_site(TALLOC_CTX *mem_ctx,
ced1f5
+               struct sss_domain_info *dom,
ced1f5
+               const char **_site);
ced1f5
+
ced1f5
+errno_t
ced1f5
+sysdb_set_site(struct sss_domain_info *dom,
ced1f5
+               const char *site);
ced1f5
+
ced1f5
 errno_t sysdb_subdomain_store(struct sysdb_ctx *sysdb,
ced1f5
                               const char *name, const char *realm,
ced1f5
                               const char *flat_name, const char *domain_id,
ced1f5
diff --git a/src/db/sysdb_subdomains.c b/src/db/sysdb_subdomains.c
ced1f5
index 2789cc4949fb7be9ad272d7613ed18a64fa8a20a..cb5de1afe3e8c9692789c5d2679eb3a4e6e1cdb2 100644
ced1f5
--- a/src/db/sysdb_subdomains.c
ced1f5
+++ b/src/db/sysdb_subdomains.c
ced1f5
@@ -1284,3 +1284,111 @@ done:
ced1f5
     talloc_free(tmp_ctx);
ced1f5
     return ret;
ced1f5
 }
ced1f5
+
ced1f5
+errno_t
ced1f5
+sysdb_get_site(TALLOC_CTX *mem_ctx,
ced1f5
+               struct sss_domain_info *dom,
ced1f5
+               const char **_site)
ced1f5
+{
ced1f5
+    TALLOC_CTX *tmp_ctx;
ced1f5
+    struct ldb_res *res;
ced1f5
+    struct ldb_dn *dn;
ced1f5
+    const char *attrs[] = { SYSDB_SITE, NULL };
ced1f5
+    errno_t ret;
ced1f5
+
ced1f5
+    tmp_ctx = talloc_new(NULL);
ced1f5
+    if (tmp_ctx == NULL) {
ced1f5
+        return ENOMEM;
ced1f5
+    }
ced1f5
+
ced1f5
+    dn = ldb_dn_new_fmt(tmp_ctx, dom->sysdb->ldb, SYSDB_DOM_BASE, dom->name);
ced1f5
+    if (dn == NULL) {
ced1f5
+        ret = ENOMEM;
ced1f5
+        goto done;
ced1f5
+    }
ced1f5
+
ced1f5
+    ret = ldb_search(dom->sysdb->ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
ced1f5
+                     attrs, NULL);
ced1f5
+    if (ret != LDB_SUCCESS) {
ced1f5
+        ret = sysdb_error_to_errno(ret);
ced1f5
+        goto done;
ced1f5
+    }
ced1f5
+
ced1f5
+    if (res->count == 0) {
ced1f5
+        *_site = NULL;
ced1f5
+        ret = EOK;
ced1f5
+        goto done;
ced1f5
+    } else if (res->count != 1) {
ced1f5
+        DEBUG(SSSDBG_CRIT_FAILURE,
ced1f5
+              "Got more than one reply for base search!\n");
ced1f5
+        ret = EIO;
ced1f5
+        goto done;
ced1f5
+    }
ced1f5
+
ced1f5
+    *_site = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_SITE, NULL);
ced1f5
+    talloc_steal(mem_ctx, *_site);
ced1f5
+
ced1f5
+    ret = EOK;
ced1f5
+
ced1f5
+done:
ced1f5
+    talloc_free(tmp_ctx);
ced1f5
+    return ret;
ced1f5
+}
ced1f5
+
ced1f5
+errno_t
ced1f5
+sysdb_set_site(struct sss_domain_info *dom,
ced1f5
+               const char *site)
ced1f5
+{
ced1f5
+    TALLOC_CTX *tmp_ctx;
ced1f5
+    struct ldb_message *msg;
ced1f5
+    struct ldb_dn *dn;
ced1f5
+    errno_t ret;
ced1f5
+
ced1f5
+    tmp_ctx = talloc_new(NULL);
ced1f5
+    if (tmp_ctx == NULL) {
ced1f5
+        return ENOMEM;
ced1f5
+    }
ced1f5
+
ced1f5
+    dn = ldb_dn_new_fmt(tmp_ctx, dom->sysdb->ldb, SYSDB_DOM_BASE, dom->name);
ced1f5
+    if (dn == NULL) {
ced1f5
+        ret = ENOMEM;
ced1f5
+        goto done;
ced1f5
+    }
ced1f5
+
ced1f5
+    msg = ldb_msg_new(tmp_ctx);
ced1f5
+    if (msg == NULL) {
ced1f5
+        ret = ENOMEM;
ced1f5
+        goto done;
ced1f5
+    }
ced1f5
+
ced1f5
+    msg->dn = dn;
ced1f5
+
ced1f5
+    ret = ldb_msg_add_empty(msg, SYSDB_SITE, LDB_FLAG_MOD_REPLACE, NULL);
ced1f5
+    if (ret != LDB_SUCCESS) {
ced1f5
+        ret = sysdb_error_to_errno(ret);
ced1f5
+        goto done;
ced1f5
+    }
ced1f5
+
ced1f5
+    if (site != NULL) {
ced1f5
+        ret = ldb_msg_add_string(msg, SYSDB_SITE, site);
ced1f5
+        if (ret != LDB_SUCCESS) {
ced1f5
+            ret = sysdb_error_to_errno(ret);
ced1f5
+            goto done;
ced1f5
+        }
ced1f5
+    }
ced1f5
+
ced1f5
+    ret = ldb_modify(dom->sysdb->ldb, msg);
ced1f5
+    if (ret != LDB_SUCCESS) {
ced1f5
+        DEBUG(SSSDBG_OP_FAILURE,
ced1f5
+              "ldb_modify()_failed: [%s][%d][%s]\n",
ced1f5
+              ldb_strerror(ret), ret, ldb_errstring(dom->sysdb->ldb));
ced1f5
+        ret = sysdb_error_to_errno(ret);
ced1f5
+        goto done;
ced1f5
+    }
ced1f5
+
ced1f5
+    ret = EOK;
ced1f5
+
ced1f5
+done:
ced1f5
+    talloc_free(tmp_ctx);
ced1f5
+    return ret;
ced1f5
+}
ced1f5
diff --git a/src/tests/cmocka/test_sysdb_subdomains.c b/src/tests/cmocka/test_sysdb_subdomains.c
ced1f5
index 84bcdc17b39dbc8822097c2006f157a09ea5e466..f8e3e1d915dba0f3a79adbf5af733980bf23a265 100644
ced1f5
--- a/src/tests/cmocka/test_sysdb_subdomains.c
ced1f5
+++ b/src/tests/cmocka/test_sysdb_subdomains.c
ced1f5
@@ -513,6 +513,31 @@ static void test_sysdb_link_ad_multidom(void **state)
ced1f5
 
ced1f5
 }
ced1f5
 
ced1f5
+static void test_sysdb_set_and_get_site(void **state)
ced1f5
+{
ced1f5
+    TALLOC_CTX *tmp_ctx;
ced1f5
+    struct subdom_test_ctx *test_ctx =
ced1f5
+        talloc_get_type(*state, struct subdom_test_ctx);
ced1f5
+    const char *site;
ced1f5
+    errno_t ret;
ced1f5
+
ced1f5
+    tmp_ctx = talloc_new(NULL);
ced1f5
+    assert_non_null(test_ctx);
ced1f5
+
ced1f5
+    ret = sysdb_get_site(test_ctx, test_ctx->tctx->dom, &site);
ced1f5
+    assert_int_equal(ret, EOK);
ced1f5
+    assert_null(site);
ced1f5
+
ced1f5
+    ret = sysdb_set_site(test_ctx->tctx->dom, "TestSite");
ced1f5
+    assert_int_equal(ret, EOK);
ced1f5
+
ced1f5
+    ret = sysdb_get_site(tmp_ctx, test_ctx->tctx->dom, &site);
ced1f5
+    assert_int_equal(ret, EOK);
ced1f5
+    assert_string_equal(site, "TestSite");
ced1f5
+
ced1f5
+    talloc_free(tmp_ctx);
ced1f5
+}
ced1f5
+
ced1f5
 int main(int argc, const char *argv[])
ced1f5
 {
ced1f5
     int rv;
ced1f5
@@ -546,6 +571,9 @@ int main(int argc, const char *argv[])
ced1f5
         cmocka_unit_test_setup_teardown(test_sysdb_link_ad_multidom,
ced1f5
                                         test_sysdb_subdom_setup,
ced1f5
                                         test_sysdb_subdom_teardown),
ced1f5
+        cmocka_unit_test_setup_teardown(test_sysdb_set_and_get_site,
ced1f5
+                                        test_sysdb_subdom_setup,
ced1f5
+                                        test_sysdb_subdom_teardown),
ced1f5
     };
ced1f5
 
ced1f5
     /* Set debug level to invalid value so we can deside if -d 0 was used. */
ced1f5
-- 
ced1f5
2.13.5
ced1f5