Blame SOURCES/0003-SYSDB-Add-functions-to-look-up-multiple-entries-incl.patch

6cf099
From 1d245226d88537123827dcda3fd0bd093275019d Mon Sep 17 00:00:00 2001
6cf099
From: Jakub Hrozek <jhrozek@redhat.com>
6cf099
Date: Tue, 24 Mar 2015 23:24:22 +0100
6cf099
Subject: [PATCH 03/13] SYSDB: Add functions to look up multiple entries
6cf099
 including name and custom filter
6cf099
MIME-Version: 1.0
6cf099
Content-Type: text/plain; charset=UTF-8
6cf099
Content-Transfer-Encoding: 8bit
6cf099
6cf099
Related:
6cf099
    https://fedorahosted.org/sssd/ticket/2553
6cf099
6cf099
Adds new sysdb function:
6cf099
    - sysdb_enumpwent_filter
6cf099
    - sysdb_enumpwent_filter_with_views
6cf099
    - sysdb_enumgrent_filter
6cf099
    - sysdb_enumgrent_filter_with_views
6cf099
6cf099
These are similar to enumeration functions, but optionally allow to
6cf099
specify a filter to be applied on user/group names. Also an additional
6cf099
custom filter can be applied.
6cf099
6cf099
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
6cf099
---
6cf099
 src/db/sysdb.h                      |  24 ++
6cf099
 src/db/sysdb_search.c               | 252 ++++++++++++------
6cf099
 src/tests/cmocka/test_sysdb_views.c | 494 ++++++++++++++++++++++++++++++++++++
6cf099
 3 files changed, 691 insertions(+), 79 deletions(-)
6cf099
6cf099
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
6cf099
index 48dd26dd294333b265b69b28cd3b5d37f1293b43..0f745ccb1a646d77ba4ad3d714d5f4dce0a51211 100644
6cf099
--- a/src/db/sysdb.h
6cf099
+++ b/src/db/sysdb.h
6cf099
@@ -601,10 +601,22 @@ int sysdb_enumpwent(TALLOC_CTX *mem_ctx,
6cf099
                     struct sss_domain_info *domain,
6cf099
                     struct ldb_result **res);
6cf099
 
6cf099
+int sysdb_enumpwent_filter(TALLOC_CTX *mem_ctx,
6cf099
+                           struct sss_domain_info *domain,
6cf099
+                           const char *name_filter,
6cf099
+                           const char *addtl_filter,
6cf099
+                           struct ldb_result **res);
6cf099
+
6cf099
 int sysdb_enumpwent_with_views(TALLOC_CTX *mem_ctx,
6cf099
                                struct sss_domain_info *domain,
6cf099
                                struct ldb_result **res);
6cf099
 
6cf099
+int sysdb_enumpwent_filter_with_views(TALLOC_CTX *mem_ctx,
6cf099
+                                      struct sss_domain_info *domain,
6cf099
+                                      const char *name_filter,
6cf099
+                                      const char *addtl_filter,
6cf099
+                                      struct ldb_result **res);
6cf099
+
6cf099
 int sysdb_getgrnam(TALLOC_CTX *mem_ctx,
6cf099
                    struct sss_domain_info *domain,
6cf099
                    const char *name,
6cf099
@@ -619,10 +631,22 @@ int sysdb_enumgrent(TALLOC_CTX *mem_ctx,
6cf099
                     struct sss_domain_info *domain,
6cf099
                     struct ldb_result **res);
6cf099
 
6cf099
+int sysdb_enumgrent_filter(TALLOC_CTX *mem_ctx,
6cf099
+                           struct sss_domain_info *domain,
6cf099
+                           const char *name_filter,
6cf099
+                           const char *addtl_filter,
6cf099
+                           struct ldb_result **res);
6cf099
+
6cf099
 int sysdb_enumgrent_with_views(TALLOC_CTX *mem_ctx,
6cf099
                                struct sss_domain_info *domain,
6cf099
                                struct ldb_result **res);
6cf099
 
6cf099
+int sysdb_enumgrent_filter_with_views(TALLOC_CTX *mem_ctx,
6cf099
+                                      struct sss_domain_info *domain,
6cf099
+                                      const char *name_filter,
6cf099
+                                      const char *addtl_filter,
6cf099
+                                      struct ldb_result **res);
6cf099
+
6cf099
 struct sysdb_netgroup_ctx {
6cf099
     enum {SYSDB_NETGROUP_TRIPLE_VAL, SYSDB_NETGROUP_GROUP_VAL} type;
6cf099
     union {
6cf099
diff --git a/src/db/sysdb_search.c b/src/db/sysdb_search.c
6cf099
index a8dcc9f8d6617be8e8fb82a1c6360c6df9726a37..4f617b841bf3b3760d9cb05a06f4b46ea0c58ff5 100644
6cf099
--- a/src/db/sysdb_search.c
6cf099
+++ b/src/db/sysdb_search.c
6cf099
@@ -255,44 +255,104 @@ done:
6cf099
     return ret;
6cf099
 }
6cf099
 
6cf099
+static char *enum_filter(TALLOC_CTX *mem_ctx,
6cf099
+                         const char *base_filter,
6cf099
+                         const char *name_filter,
6cf099
+                         const char *addtl_filter)
6cf099
+{
6cf099
+    char *filter;
6cf099
+    TALLOC_CTX *tmp_ctx = NULL;
6cf099
+
6cf099
+    tmp_ctx = talloc_new(NULL);
6cf099
+    if (tmp_ctx == NULL) {
6cf099
+        return NULL;
6cf099
+    }
6cf099
+
6cf099
+    if (name_filter == NULL && addtl_filter == NULL) {
6cf099
+        filter = talloc_strdup(tmp_ctx, base_filter);
6cf099
+    } else {
6cf099
+        filter = talloc_asprintf(tmp_ctx, "(&%s", base_filter);
6cf099
+
6cf099
+        if (filter != NULL && name_filter != NULL) {
6cf099
+            filter = talloc_asprintf_append(filter, "(%s=%s)",
6cf099
+                                            SYSDB_NAME, name_filter);
6cf099
+        }
6cf099
+
6cf099
+        if (filter != NULL && addtl_filter != NULL) {
6cf099
+            filter = talloc_asprintf_append(filter, "%s", addtl_filter);
6cf099
+        }
6cf099
+
6cf099
+        if (filter != NULL) {
6cf099
+            filter = talloc_asprintf_append(filter, ")");
6cf099
+        }
6cf099
+    }
6cf099
+
6cf099
+    if (filter) {
6cf099
+        talloc_steal(mem_ctx, filter);
6cf099
+    }
6cf099
+
6cf099
+    talloc_free(tmp_ctx);
6cf099
+    return filter;
6cf099
+}
6cf099
+
6cf099
+int sysdb_enumpwent_filter(TALLOC_CTX *mem_ctx,
6cf099
+                           struct sss_domain_info *domain,
6cf099
+                           const char *name_filter,
6cf099
+                           const char *addtl_filter,
6cf099
+                           struct ldb_result **_res)
6cf099
+{
6cf099
+    TALLOC_CTX *tmp_ctx;
6cf099
+    static const char *attrs[] = SYSDB_PW_ATTRS;
6cf099
+    char *filter = NULL;
6cf099
+    struct ldb_dn *base_dn;
6cf099
+    struct ldb_result *res;
6cf099
+    int ret;
6cf099
+
6cf099
+    tmp_ctx = talloc_new(NULL);
6cf099
+    if (!tmp_ctx) {
6cf099
+        return ENOMEM;
6cf099
+    }
6cf099
+
6cf099
+    base_dn = sysdb_user_base_dn(tmp_ctx, domain);
6cf099
+    if (!base_dn) {
6cf099
+        ret = ENOMEM;
6cf099
+        goto done;
6cf099
+    }
6cf099
+
6cf099
+    filter = enum_filter(tmp_ctx, SYSDB_PWENT_FILTER,
6cf099
+                         name_filter, addtl_filter);
6cf099
+    if (filter == NULL) {
6cf099
+        ret = ENOMEM;
6cf099
+        goto done;
6cf099
+    }
6cf099
+    DEBUG(SSSDBG_TRACE_LIBS, "Searching cache with [%s]\n", filter);
6cf099
+
6cf099
+    ret = ldb_search(domain->sysdb->ldb, tmp_ctx, &res, base_dn,
6cf099
+                     LDB_SCOPE_SUBTREE, attrs, "%s", filter);
6cf099
+    if (ret) {
6cf099
+        ret = sysdb_error_to_errno(ret);
6cf099
+        goto done;
6cf099
+    }
6cf099
+
6cf099
+    *_res = talloc_steal(mem_ctx, res);
6cf099
+
6cf099
+done:
6cf099
+    talloc_zfree(tmp_ctx);
6cf099
+    return ret;
6cf099
+}
6cf099
+
6cf099
 int sysdb_enumpwent(TALLOC_CTX *mem_ctx,
6cf099
                     struct sss_domain_info *domain,
6cf099
                     struct ldb_result **_res)
6cf099
 {
6cf099
-    TALLOC_CTX *tmp_ctx;
6cf099
-    static const char *attrs[] = SYSDB_PW_ATTRS;
6cf099
-    struct ldb_dn *base_dn;
6cf099
-    struct ldb_result *res;
6cf099
-    int ret;
6cf099
-
6cf099
-    tmp_ctx = talloc_new(NULL);
6cf099
-    if (!tmp_ctx) {
6cf099
-        return ENOMEM;
6cf099
-    }
6cf099
-
6cf099
-    base_dn = sysdb_user_base_dn(tmp_ctx, domain);
6cf099
-    if (!base_dn) {
6cf099
-        ret = ENOMEM;
6cf099
-        goto done;
6cf099
-    }
6cf099
-
6cf099
-    ret = ldb_search(domain->sysdb->ldb, tmp_ctx, &res, base_dn,
6cf099
-                     LDB_SCOPE_SUBTREE, attrs, SYSDB_PWENT_FILTER);
6cf099
-    if (ret) {
6cf099
-        ret = sysdb_error_to_errno(ret);
6cf099
-        goto done;
6cf099
-    }
6cf099
-
6cf099
-    *_res = talloc_steal(mem_ctx, res);
6cf099
-
6cf099
-done:
6cf099
-    talloc_zfree(tmp_ctx);
6cf099
-    return ret;
6cf099
+    return sysdb_enumpwent_filter(mem_ctx, domain, NULL, 0, _res);
6cf099
 }
6cf099
 
6cf099
-int sysdb_enumpwent_with_views(TALLOC_CTX *mem_ctx,
6cf099
-                               struct sss_domain_info *domain,
6cf099
-                               struct ldb_result **_res)
6cf099
+int sysdb_enumpwent_filter_with_views(TALLOC_CTX *mem_ctx,
6cf099
+                                      struct sss_domain_info *domain,
6cf099
+                                      const char *name_filter,
6cf099
+                                      const char *addtl_filter,
6cf099
+                                      struct ldb_result **_res)
6cf099
 {
6cf099
     TALLOC_CTX *tmp_ctx;
6cf099
     struct ldb_result *res;
6cf099
@@ -305,7 +365,7 @@ int sysdb_enumpwent_with_views(TALLOC_CTX *mem_ctx,
6cf099
         return ENOMEM;
6cf099
     }
6cf099
 
6cf099
-    ret = sysdb_enumpwent(tmp_ctx, domain, &res;;
6cf099
+    ret = sysdb_enumpwent_filter(tmp_ctx, domain, name_filter, addtl_filter, &res;;
6cf099
     if (ret != EOK) {
6cf099
         DEBUG(SSSDBG_OP_FAILURE, "sysdb_enumpwent failed.\n");
6cf099
         goto done;
6cf099
@@ -331,6 +391,13 @@ done:
6cf099
     return ret;
6cf099
 }
6cf099
 
6cf099
+int sysdb_enumpwent_with_views(TALLOC_CTX *mem_ctx,
6cf099
+                               struct sss_domain_info *domain,
6cf099
+                               struct ldb_result **_res)
6cf099
+{
6cf099
+    return sysdb_enumpwent_filter_with_views(mem_ctx, domain, NULL, NULL, _res);
6cf099
+}
6cf099
+
6cf099
 /* groups */
6cf099
 
6cf099
 static int mpg_convert(struct ldb_message *msg)
6cf099
@@ -662,57 +729,77 @@ done:
6cf099
     return ret;
6cf099
 }
6cf099
 
6cf099
+int sysdb_enumgrent_filter(TALLOC_CTX *mem_ctx,
6cf099
+                           struct sss_domain_info *domain,
6cf099
+                           const char *name_filter,
6cf099
+                           const char *addtl_filter,
6cf099
+                           struct ldb_result **_res)
6cf099
+{
6cf099
+    TALLOC_CTX *tmp_ctx;
6cf099
+    static const char *attrs[] = SYSDB_GRSRC_ATTRS;
6cf099
+    const char *filter = NULL;
6cf099
+    const char *base_filter;
6cf099
+    struct ldb_dn *base_dn;
6cf099
+    struct ldb_result *res;
6cf099
+    int ret;
6cf099
+
6cf099
+    tmp_ctx = talloc_new(NULL);
6cf099
+    if (!tmp_ctx) {
6cf099
+        return ENOMEM;
6cf099
+    }
6cf099
+
6cf099
+    if (domain->mpg) {
6cf099
+        base_filter = SYSDB_GRENT_MPG_FILTER;
6cf099
+        base_dn = ldb_dn_new_fmt(tmp_ctx, domain->sysdb->ldb,
6cf099
+                                 SYSDB_DOM_BASE, domain->name);
6cf099
+    } else {
6cf099
+        base_filter = SYSDB_GRENT_FILTER;
6cf099
+        base_dn = sysdb_group_base_dn(tmp_ctx, domain);
6cf099
+    }
6cf099
+    if (!base_dn) {
6cf099
+        ret = ENOMEM;
6cf099
+        goto done;
6cf099
+    }
6cf099
+
6cf099
+    filter = enum_filter(tmp_ctx, base_filter,
6cf099
+                         name_filter, addtl_filter);
6cf099
+    if (filter == NULL) {
6cf099
+        ret = ENOMEM;
6cf099
+        goto done;
6cf099
+    }
6cf099
+    DEBUG(SSSDBG_TRACE_LIBS, "Searching cache with [%s]\n", filter);
6cf099
+
6cf099
+    ret = ldb_search(domain->sysdb->ldb, tmp_ctx, &res, base_dn,
6cf099
+                     LDB_SCOPE_SUBTREE, attrs, "%s", filter);
6cf099
+    if (ret) {
6cf099
+        ret = sysdb_error_to_errno(ret);
6cf099
+        goto done;
6cf099
+    }
6cf099
+
6cf099
+    ret = mpg_res_convert(res);
6cf099
+    if (ret) {
6cf099
+        goto done;
6cf099
+    }
6cf099
+
6cf099
+    *_res = talloc_steal(mem_ctx, res);
6cf099
+
6cf099
+done:
6cf099
+    talloc_zfree(tmp_ctx);
6cf099
+    return ret;
6cf099
+}
6cf099
+
6cf099
 int sysdb_enumgrent(TALLOC_CTX *mem_ctx,
6cf099
                     struct sss_domain_info *domain,
6cf099
                     struct ldb_result **_res)
6cf099
 {
6cf099
-    TALLOC_CTX *tmp_ctx;
6cf099
-    static const char *attrs[] = SYSDB_GRSRC_ATTRS;
6cf099
-    const char *fmt_filter;
6cf099
-    struct ldb_dn *base_dn;
6cf099
-    struct ldb_result *res;
6cf099
-    int ret;
6cf099
-
6cf099
-    tmp_ctx = talloc_new(NULL);
6cf099
-    if (!tmp_ctx) {
6cf099
-        return ENOMEM;
6cf099
-    }
6cf099
-
6cf099
-    if (domain->mpg) {
6cf099
-        fmt_filter = SYSDB_GRENT_MPG_FILTER;
6cf099
-        base_dn = ldb_dn_new_fmt(tmp_ctx, domain->sysdb->ldb,
6cf099
-                                 SYSDB_DOM_BASE, domain->name);
6cf099
-    } else {
6cf099
-        fmt_filter = SYSDB_GRENT_FILTER;
6cf099
-        base_dn = sysdb_group_base_dn(tmp_ctx, domain);
6cf099
-    }
6cf099
-    if (!base_dn) {
6cf099
-        ret = ENOMEM;
6cf099
-        goto done;
6cf099
-    }
6cf099
-
6cf099
-    ret = ldb_search(domain->sysdb->ldb, tmp_ctx, &res, base_dn,
6cf099
-                     LDB_SCOPE_SUBTREE, attrs, "%s", fmt_filter);
6cf099
-    if (ret) {
6cf099
-        ret = sysdb_error_to_errno(ret);
6cf099
-        goto done;
6cf099
-    }
6cf099
-
6cf099
-    ret = mpg_res_convert(res);
6cf099
-    if (ret) {
6cf099
-        goto done;
6cf099
-    }
6cf099
-
6cf099
-    *_res = talloc_steal(mem_ctx, res);
6cf099
-
6cf099
-done:
6cf099
-    talloc_zfree(tmp_ctx);
6cf099
-    return ret;
6cf099
+    return sysdb_enumgrent_filter(mem_ctx, domain, NULL, 0, _res);
6cf099
 }
6cf099
 
6cf099
-int sysdb_enumgrent_with_views(TALLOC_CTX *mem_ctx,
6cf099
-                               struct sss_domain_info *domain,
6cf099
-                               struct ldb_result **_res)
6cf099
+int sysdb_enumgrent_filter_with_views(TALLOC_CTX *mem_ctx,
6cf099
+                                      struct sss_domain_info *domain,
6cf099
+                                      const char *name_filter,
6cf099
+                                      const char *addtl_filter,
6cf099
+                                      struct ldb_result **_res)
6cf099
 {
6cf099
     TALLOC_CTX *tmp_ctx;
6cf099
     struct ldb_result *res;
6cf099
@@ -725,7 +812,7 @@ int sysdb_enumgrent_with_views(TALLOC_CTX *mem_ctx,
6cf099
         return ENOMEM;
6cf099
     }
6cf099
 
6cf099
-    ret = sysdb_enumgrent(tmp_ctx, domain,&res;;
6cf099
+    ret = sysdb_enumgrent_filter(tmp_ctx, domain, name_filter, addtl_filter, &res;;
6cf099
     if (ret != EOK) {
6cf099
         DEBUG(SSSDBG_OP_FAILURE, "sysdb_enumgrent failed.\n");
6cf099
         goto done;
6cf099
@@ -759,6 +846,13 @@ done:
6cf099
     return ret;
6cf099
 }
6cf099
 
6cf099
+int sysdb_enumgrent_with_views(TALLOC_CTX *mem_ctx,
6cf099
+                               struct sss_domain_info *domain,
6cf099
+                               struct ldb_result **_res)
6cf099
+{
6cf099
+    return sysdb_enumgrent_filter_with_views(mem_ctx, domain, NULL, NULL, _res);
6cf099
+}
6cf099
+
6cf099
 int sysdb_initgroups(TALLOC_CTX *mem_ctx,
6cf099
                      struct sss_domain_info *domain,
6cf099
                      const char *name,
6cf099
diff --git a/src/tests/cmocka/test_sysdb_views.c b/src/tests/cmocka/test_sysdb_views.c
6cf099
index 69118cd87a172696f8220e1446df7a856e368cb6..1fb598219e9ee581e465ddbb32ba9f2544600c26 100644
6cf099
--- a/src/tests/cmocka/test_sysdb_views.c
6cf099
+++ b/src/tests/cmocka/test_sysdb_views.c
6cf099
@@ -45,6 +45,7 @@
6cf099
 #define TEST_USER_HOMEDIR "/home/home"
6cf099
 #define TEST_USER_SHELL "/bin/shell"
6cf099
 #define TEST_USER_SID "S-1-2-3-4"
6cf099
+#define TEST_GID_OVERRIDE_BASE 100
6cf099
 
6cf099
 struct sysdb_test_ctx {
6cf099
     struct sysdb_ctx *sysdb;
6cf099
@@ -108,6 +109,7 @@ static int _setup_sysdb_tests(struct sysdb_test_ctx **ctx, bool enumerate)
6cf099
                            TESTS_PATH, &test_ctx->domain);
6cf099
     assert_int_equal(ret, EOK);
6cf099
 
6cf099
+    test_ctx->domain->has_views = true;
6cf099
     test_ctx->sysdb = test_ctx->domain->sysdb;
6cf099
 
6cf099
     *ctx = test_ctx;
6cf099
@@ -115,6 +117,7 @@ static int _setup_sysdb_tests(struct sysdb_test_ctx **ctx, bool enumerate)
6cf099
 }
6cf099
 
6cf099
 #define setup_sysdb_tests(ctx) _setup_sysdb_tests((ctx), false)
6cf099
+#define setup_sysdb_enum_tests(ctx) _setup_sysdb_tests((ctx), true)
6cf099
 
6cf099
 static int test_sysdb_setup(void **state)
6cf099
 {
6cf099
@@ -426,6 +429,473 @@ void test_sysdb_invalidate_overrides(void **state)
6cf099
     assert_int_equal(ldb_msg_find_attr_as_uint64(msg, SYSDB_CACHE_EXPIRE, 0),
6cf099
                      1);
6cf099
     assert_null(ldb_msg_find_attr_as_string(msg, SYSDB_OVERRIDE_DN, NULL));
6cf099
+
6cf099
+    ret = sysdb_delete_user(test_ctx->domain, TEST_USER_NAME, 0);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+}
6cf099
+
6cf099
+static const char *users[] = { "alice", "bob", "barney", NULL };
6cf099
+
6cf099
+static void enum_test_user_override(struct sysdb_test_ctx *test_ctx,
6cf099
+                                    const char *name)
6cf099
+{
6cf099
+    int ret;
6cf099
+    struct sysdb_attrs *attrs;
6cf099
+    struct ldb_dn *dn;
6cf099
+    TALLOC_CTX *tmp_ctx;
6cf099
+    const char *anchor;
6cf099
+    const char *override_gecos;
6cf099
+
6cf099
+    tmp_ctx = talloc_new(test_ctx);
6cf099
+    assert_non_null(tmp_ctx);
6cf099
+
6cf099
+    attrs = sysdb_new_attrs(tmp_ctx);
6cf099
+    assert_non_null(attrs);
6cf099
+
6cf099
+    dn = sysdb_user_dn(tmp_ctx, test_ctx->domain, name);
6cf099
+    assert_non_null(dn);
6cf099
+
6cf099
+    anchor = talloc_asprintf(tmp_ctx, "%s%s", TEST_ANCHOR_PREFIX, name);
6cf099
+    ret = sysdb_attrs_add_string(attrs, SYSDB_OVERRIDE_ANCHOR_UUID, anchor);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+
6cf099
+    override_gecos = talloc_asprintf(attrs, "%s_GECOS_OVERRIDE", name);
6cf099
+    ret = sysdb_attrs_add_string(attrs, SYSDB_GECOS, override_gecos);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+
6cf099
+    ret = sysdb_store_override(test_ctx->domain, TEST_VIEW_NAME,
6cf099
+                               SYSDB_MEMBER_USER, attrs, dn);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+
6cf099
+    talloc_free(tmp_ctx);
6cf099
+}
6cf099
+
6cf099
+static void enum_test_add_users(struct sysdb_test_ctx *test_ctx,
6cf099
+                                const char *usernames[])
6cf099
+{
6cf099
+    int i;
6cf099
+    int ret;
6cf099
+    struct sysdb_attrs *attrs;
6cf099
+
6cf099
+    for (i = 0; usernames[i] != NULL; i++) {
6cf099
+        attrs = talloc(test_ctx, struct sysdb_attrs);
6cf099
+        assert_non_null(attrs);
6cf099
+
6cf099
+        ret = sysdb_store_user(test_ctx->domain, usernames[i],
6cf099
+                               NULL, 0, 0, usernames[i], "/", "/bin/sh",
6cf099
+                               NULL, NULL, NULL, 1, 1234 + i);
6cf099
+        assert_int_equal(ret, EOK);
6cf099
+
6cf099
+        enum_test_user_override(test_ctx, usernames[i]);
6cf099
+
6cf099
+        talloc_free(attrs);
6cf099
+    }
6cf099
+}
6cf099
+
6cf099
+static void enum_test_del_users(struct sss_domain_info *dom,
6cf099
+                               const char *usernames[])
6cf099
+{
6cf099
+    int i;
6cf099
+    int ret;
6cf099
+
6cf099
+    for (i = 0; usernames[i] != NULL; i++) {
6cf099
+        ret = sysdb_delete_user(dom, usernames[i], 0);
6cf099
+        if (ret != EOK && ret != ENOENT) {
6cf099
+            fail();
6cf099
+        }
6cf099
+    }
6cf099
+}
6cf099
+
6cf099
+static int test_enum_users_setup(void **state)
6cf099
+{
6cf099
+    int ret;
6cf099
+    struct sysdb_test_ctx *test_ctx;
6cf099
+
6cf099
+    assert_true(leak_check_setup());
6cf099
+
6cf099
+    ret = setup_sysdb_enum_tests(&test_ctx);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+
6cf099
+    enum_test_add_users(test_ctx, users);
6cf099
+
6cf099
+    *state = (void *) test_ctx;
6cf099
+    return 0;
6cf099
+}
6cf099
+
6cf099
+static void assert_user_attrs(struct ldb_message *msg,
6cf099
+                              const char *name,
6cf099
+                              bool has_views)
6cf099
+{
6cf099
+    const char *str;
6cf099
+
6cf099
+    str = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
6cf099
+    assert_string_equal(str, name);
6cf099
+    str = ldb_msg_find_attr_as_string(msg, SYSDB_GECOS, NULL);
6cf099
+    assert_string_equal(str, name);
6cf099
+
6cf099
+    str = ldb_msg_find_attr_as_string(msg, OVERRIDE_PREFIX SYSDB_GECOS, NULL);
6cf099
+    if (has_views) {
6cf099
+        char *override;
6cf099
+
6cf099
+        assert_non_null(str);
6cf099
+        override = talloc_asprintf(msg, "%s_GECOS_OVERRIDE", name);
6cf099
+        assert_non_null(override);
6cf099
+
6cf099
+        assert_string_equal(str, override);
6cf099
+        talloc_free(override);
6cf099
+    } else {
6cf099
+        assert_null(str);
6cf099
+    }
6cf099
+}
6cf099
+
6cf099
+static int test_enum_users_teardown(void **state)
6cf099
+{
6cf099
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
6cf099
+                                                        struct sysdb_test_ctx);
6cf099
+
6cf099
+    enum_test_del_users(test_ctx->domain, users);
6cf099
+    return test_sysdb_teardown(state);
6cf099
+}
6cf099
+
6cf099
+static void check_enumpwent(int ret, struct ldb_result *res, bool views)
6cf099
+{
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, N_ELEMENTS(users)-1);
6cf099
+    assert_user_attrs(res->msgs[0], "barney", views);
6cf099
+    assert_user_attrs(res->msgs[1], "alice", views);
6cf099
+    assert_user_attrs(res->msgs[2], "bob", views);
6cf099
+}
6cf099
+
6cf099
+static void test_sysdb_enumpwent(void **state)
6cf099
+{
6cf099
+    int ret;
6cf099
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
6cf099
+                                                        struct sysdb_test_ctx);
6cf099
+    struct ldb_result *res;
6cf099
+
6cf099
+    ret = sysdb_enumpwent(test_ctx, test_ctx->domain, &res;;
6cf099
+    check_enumpwent(ret, res, false);
6cf099
+}
6cf099
+
6cf099
+static void test_sysdb_enumpwent_views(void **state)
6cf099
+{
6cf099
+    int ret;
6cf099
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
6cf099
+                                                        struct sysdb_test_ctx);
6cf099
+    struct ldb_result *res;
6cf099
+
6cf099
+    ret = sysdb_enumpwent_with_views(test_ctx, test_ctx->domain, &res;;
6cf099
+    check_enumpwent(ret, res, true);
6cf099
+}
6cf099
+
6cf099
+static void test_sysdb_enumpwent_filter(void **state)
6cf099
+{
6cf099
+    int ret;
6cf099
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
6cf099
+                                                        struct sysdb_test_ctx);
6cf099
+    struct ldb_result *res;
6cf099
+    char *addtl_filter;
6cf099
+
6cf099
+    ret = sysdb_enumpwent_filter(test_ctx, test_ctx->domain, "a*", 0, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 1);
6cf099
+    assert_user_attrs(res->msgs[0], "alice", false);
6cf099
+
6cf099
+    ret = sysdb_enumpwent_filter(test_ctx, test_ctx->domain, "b*", 0, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 2);
6cf099
+    assert_user_attrs(res->msgs[0], "barney", false);
6cf099
+    assert_user_attrs(res->msgs[1], "bob", false);
6cf099
+
6cf099
+    ret = sysdb_enumpwent_filter(test_ctx, test_ctx->domain, "c*", 0, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 0);
6cf099
+
6cf099
+    ret = sysdb_enumpwent_filter(test_ctx, test_ctx->domain, "*", 0, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, N_ELEMENTS(users)-1);
6cf099
+
6cf099
+    /* Test searching based on time as well */
6cf099
+    addtl_filter = talloc_asprintf(test_ctx, "(%s<=%d)",
6cf099
+                                   SYSDB_LAST_UPDATE, 1233);
6cf099
+    ret = sysdb_enumpwent_filter(test_ctx, test_ctx->domain, "a*", addtl_filter, &res;;
6cf099
+    talloc_free(addtl_filter);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 0);
6cf099
+
6cf099
+    addtl_filter = talloc_asprintf(test_ctx, "(%s<=%d)",
6cf099
+                                   SYSDB_LAST_UPDATE, 1234);
6cf099
+    ret = sysdb_enumpwent_filter(test_ctx, test_ctx->domain, "a*", addtl_filter, &res;;
6cf099
+    talloc_free(addtl_filter);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 1);
6cf099
+    assert_user_attrs(res->msgs[0], "alice", false);
6cf099
+}
6cf099
+
6cf099
+static void test_sysdb_enumpwent_filter_views(void **state)
6cf099
+{
6cf099
+    int ret;
6cf099
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
6cf099
+                                                        struct sysdb_test_ctx);
6cf099
+    struct ldb_result *res;
6cf099
+    char *addtl_filter;
6cf099
+
6cf099
+    ret = sysdb_enumpwent_filter_with_views(test_ctx, test_ctx->domain,
6cf099
+                                            "a*", NULL, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 1);
6cf099
+    assert_user_attrs(res->msgs[0], "alice", true);
6cf099
+
6cf099
+    ret = sysdb_enumpwent_filter_with_views(test_ctx, test_ctx->domain,
6cf099
+                                            "b*", NULL, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 2);
6cf099
+    assert_user_attrs(res->msgs[0], "barney", true);
6cf099
+    assert_user_attrs(res->msgs[1], "bob", true);
6cf099
+
6cf099
+    addtl_filter = talloc_asprintf(test_ctx, "(%s<=%d)",
6cf099
+                                   SYSDB_LAST_UPDATE, 1235);
6cf099
+    ret = sysdb_enumpwent_filter_with_views(test_ctx, test_ctx->domain,
6cf099
+                                            "b*", addtl_filter, &res;;
6cf099
+    talloc_free(addtl_filter);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 1);
6cf099
+    assert_user_attrs(res->msgs[0], "bob", true);
6cf099
+
6cf099
+    ret = sysdb_enumpwent_filter_with_views(test_ctx,
6cf099
+                                            test_ctx->domain, "c*", NULL, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 0);
6cf099
+
6cf099
+    ret = sysdb_enumpwent_filter_with_views(test_ctx,
6cf099
+                                            test_ctx->domain, "*", NULL, &res;;
6cf099
+    check_enumpwent(ret, res, true);
6cf099
+}
6cf099
+
6cf099
+static const char *groups[] = { "one", "two", "three", NULL };
6cf099
+
6cf099
+static void enum_test_group_override(struct sysdb_test_ctx *test_ctx,
6cf099
+                                     const char *name,
6cf099
+                                     unsigned override_gid)
6cf099
+{
6cf099
+    int ret;
6cf099
+    struct sysdb_attrs *attrs;
6cf099
+    struct ldb_dn *dn;
6cf099
+    TALLOC_CTX *tmp_ctx;
6cf099
+    const char *anchor;
6cf099
+
6cf099
+    tmp_ctx = talloc_new(test_ctx);
6cf099
+    assert_non_null(tmp_ctx);
6cf099
+
6cf099
+    attrs = sysdb_new_attrs(tmp_ctx);
6cf099
+    assert_non_null(attrs);
6cf099
+
6cf099
+    dn = sysdb_group_dn(tmp_ctx, test_ctx->domain, name);
6cf099
+    assert_non_null(dn);
6cf099
+
6cf099
+    anchor = talloc_asprintf(tmp_ctx, "%s%s", TEST_ANCHOR_PREFIX, name);
6cf099
+    ret = sysdb_attrs_add_string(attrs, SYSDB_OVERRIDE_ANCHOR_UUID, anchor);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+
6cf099
+    ret = sysdb_attrs_add_uint32(attrs, SYSDB_GIDNUM, override_gid);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+
6cf099
+    ret = sysdb_store_override(test_ctx->domain, TEST_VIEW_NAME,
6cf099
+                               SYSDB_MEMBER_GROUP, attrs, dn);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+
6cf099
+    talloc_free(tmp_ctx);
6cf099
+}
6cf099
+
6cf099
+static void enum_test_add_groups(struct sysdb_test_ctx *test_ctx,
6cf099
+                                 const char *groupnames[])
6cf099
+{
6cf099
+    int i;
6cf099
+    int ret;
6cf099
+    struct sysdb_attrs *attrs;
6cf099
+
6cf099
+    for (i = 0; groupnames[i] != NULL; i++) {
6cf099
+        attrs = talloc(test_ctx, struct sysdb_attrs);
6cf099
+        assert_non_null(attrs);
6cf099
+
6cf099
+        ret = sysdb_store_group(test_ctx->domain, groupnames[i],
6cf099
+                                0, NULL, 1, 1234 + i);
6cf099
+        assert_int_equal(ret, EOK);
6cf099
+
6cf099
+        enum_test_group_override(test_ctx, groupnames[i],
6cf099
+                                 TEST_GID_OVERRIDE_BASE + i);
6cf099
+        talloc_free(attrs);
6cf099
+    }
6cf099
+}
6cf099
+
6cf099
+static void enum_test_del_groups(struct sss_domain_info *dom,
6cf099
+                                 const char *groupnames[])
6cf099
+{
6cf099
+    int i;
6cf099
+    int ret;
6cf099
+
6cf099
+    for (i = 0; groupnames[i] != NULL; i++) {
6cf099
+        ret = sysdb_delete_group(dom, groupnames[i], 0);
6cf099
+        if (ret != EOK && ret != ENOENT) {
6cf099
+            fail();
6cf099
+        }
6cf099
+    }
6cf099
+}
6cf099
+
6cf099
+static int test_enum_groups_setup(void **state)
6cf099
+{
6cf099
+    int ret;
6cf099
+    struct sysdb_test_ctx *test_ctx;
6cf099
+
6cf099
+    assert_true(leak_check_setup());
6cf099
+
6cf099
+    ret = setup_sysdb_enum_tests(&test_ctx);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+
6cf099
+    enum_test_add_groups(test_ctx, groups);
6cf099
+
6cf099
+    *state = (void *) test_ctx;
6cf099
+    return 0;
6cf099
+}
6cf099
+
6cf099
+static int test_enum_groups_teardown(void **state)
6cf099
+{
6cf099
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
6cf099
+                                                        struct sysdb_test_ctx);
6cf099
+
6cf099
+    enum_test_del_groups(test_ctx->domain, groups);
6cf099
+    return test_sysdb_teardown(state);
6cf099
+}
6cf099
+
6cf099
+static void assert_group_attrs(struct ldb_message *msg,
6cf099
+                               const char *name,
6cf099
+                               unsigned expected_override_gid)
6cf099
+{
6cf099
+    const char *str;
6cf099
+    unsigned gid;
6cf099
+
6cf099
+    str = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
6cf099
+    assert_string_equal(str, name);
6cf099
+
6cf099
+    if (expected_override_gid) {
6cf099
+        gid = ldb_msg_find_attr_as_uint64(msg,
6cf099
+                                          OVERRIDE_PREFIX SYSDB_GIDNUM, 0);
6cf099
+        assert_int_equal(gid, expected_override_gid);
6cf099
+    }
6cf099
+}
6cf099
+
6cf099
+static void check_enumgrent(int ret, struct ldb_result *res, bool views)
6cf099
+{
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, N_ELEMENTS(groups)-1);
6cf099
+    assert_group_attrs(res->msgs[0], "three", views ? TEST_GID_OVERRIDE_BASE + 2 : 0);
6cf099
+    assert_group_attrs(res->msgs[1], "one", views ? TEST_GID_OVERRIDE_BASE : 0);
6cf099
+    assert_group_attrs(res->msgs[2], "two", views ? TEST_GID_OVERRIDE_BASE + 1 : 0);
6cf099
+}
6cf099
+
6cf099
+static void test_sysdb_enumgrent(void **state)
6cf099
+{
6cf099
+    int ret;
6cf099
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
6cf099
+                                                        struct sysdb_test_ctx);
6cf099
+    struct ldb_result *res;
6cf099
+
6cf099
+    ret = sysdb_enumgrent(test_ctx, test_ctx->domain, &res;;
6cf099
+    check_enumgrent(ret, res, false);
6cf099
+}
6cf099
+
6cf099
+static void test_sysdb_enumgrent_views(void **state)
6cf099
+{
6cf099
+    int ret;
6cf099
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
6cf099
+                                                        struct sysdb_test_ctx);
6cf099
+    struct ldb_result *res;
6cf099
+
6cf099
+    ret = sysdb_enumgrent_with_views(test_ctx, test_ctx->domain, &res;;
6cf099
+    check_enumgrent(ret, res, true);
6cf099
+}
6cf099
+
6cf099
+static void test_sysdb_enumgrent_filter(void **state)
6cf099
+{
6cf099
+    int ret;
6cf099
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
6cf099
+                                                        struct sysdb_test_ctx);
6cf099
+    struct ldb_result *res;
6cf099
+    char *addtl_filter;
6cf099
+
6cf099
+    ret = sysdb_enumgrent_filter(test_ctx, test_ctx->domain, "o*", 0, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 1);
6cf099
+    assert_group_attrs(res->msgs[0], "one", 0);
6cf099
+
6cf099
+    ret = sysdb_enumgrent_filter(test_ctx, test_ctx->domain, "t*", 0, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 2);
6cf099
+    assert_group_attrs(res->msgs[0], "three", 0);
6cf099
+    assert_group_attrs(res->msgs[1], "two", 0);
6cf099
+
6cf099
+    ret = sysdb_enumgrent_filter(test_ctx, test_ctx->domain, "x*", 0, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 0);
6cf099
+
6cf099
+    ret = sysdb_enumgrent_filter(test_ctx, test_ctx->domain, "*", 0, &res;;
6cf099
+    check_enumgrent(ret, res, false);
6cf099
+
6cf099
+    addtl_filter = talloc_asprintf(test_ctx, "(%s<=%d)",
6cf099
+                                   SYSDB_LAST_UPDATE, 1233);
6cf099
+    ret = sysdb_enumgrent_filter(test_ctx, test_ctx->domain, "o*", addtl_filter, &res;;
6cf099
+    talloc_free(addtl_filter);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 0);
6cf099
+
6cf099
+    addtl_filter = talloc_asprintf(test_ctx, "(%s<=%d)",
6cf099
+                                   SYSDB_LAST_UPDATE, 1234);
6cf099
+    ret = sysdb_enumgrent_filter(test_ctx, test_ctx->domain, "o*", addtl_filter, &res;;
6cf099
+    talloc_free(addtl_filter);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 1);
6cf099
+    assert_group_attrs(res->msgs[0], "one", 0);
6cf099
+
6cf099
+}
6cf099
+
6cf099
+static void test_sysdb_enumgrent_filter_views(void **state)
6cf099
+{
6cf099
+    int ret;
6cf099
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
6cf099
+                                                        struct sysdb_test_ctx);
6cf099
+    struct ldb_result *res;
6cf099
+    char *addtl_filter;
6cf099
+
6cf099
+    ret = sysdb_enumgrent_filter_with_views(test_ctx, test_ctx->domain,
6cf099
+                                            "o*", NULL, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 1);
6cf099
+    assert_group_attrs(res->msgs[0], "one", TEST_GID_OVERRIDE_BASE);
6cf099
+
6cf099
+    ret = sysdb_enumgrent_filter_with_views(test_ctx, test_ctx->domain,
6cf099
+                                            "t*", NULL, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 2);
6cf099
+    assert_group_attrs(res->msgs[0], "three", TEST_GID_OVERRIDE_BASE + 2);
6cf099
+    assert_group_attrs(res->msgs[1], "two", TEST_GID_OVERRIDE_BASE + 1);
6cf099
+
6cf099
+    addtl_filter = talloc_asprintf(test_ctx, "(%s<=%d)",
6cf099
+                                   SYSDB_LAST_UPDATE, 1235);
6cf099
+    ret = sysdb_enumgrent_filter_with_views(test_ctx, test_ctx->domain,
6cf099
+                                            "t*", addtl_filter, &res;;
6cf099
+    talloc_free(addtl_filter);
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 1);
6cf099
+    assert_group_attrs(res->msgs[0], "two", TEST_GID_OVERRIDE_BASE + 1);
6cf099
+
6cf099
+    ret = sysdb_enumgrent_filter_with_views(test_ctx, test_ctx->domain,
6cf099
+                                            "x*", NULL, &res;;
6cf099
+    assert_int_equal(ret, EOK);
6cf099
+    assert_int_equal(res->count, 0);
6cf099
+
6cf099
+    ret = sysdb_enumgrent_filter_with_views(test_ctx, test_ctx->domain,
6cf099
+                                            "*", NULL, &res;;
6cf099
+    check_enumgrent(ret, res, true);
6cf099
 }
6cf099
 
6cf099
 int main(int argc, const char *argv[])
6cf099
@@ -453,6 +923,30 @@ int main(int argc, const char *argv[])
6cf099
                                         test_sysdb_setup, test_sysdb_teardown),
6cf099
         cmocka_unit_test_setup_teardown(test_sysdb_invalidate_overrides,
6cf099
                                         test_sysdb_setup, test_sysdb_teardown),
6cf099
+        cmocka_unit_test_setup_teardown(test_sysdb_enumpwent,
6cf099
+                                        test_enum_users_setup,
6cf099
+                                        test_enum_users_teardown),
6cf099
+        cmocka_unit_test_setup_teardown(test_sysdb_enumpwent_views,
6cf099
+                                        test_enum_users_setup,
6cf099
+                                        test_enum_users_teardown),
6cf099
+        cmocka_unit_test_setup_teardown(test_sysdb_enumpwent_filter,
6cf099
+                                        test_enum_users_setup,
6cf099
+                                        test_enum_users_teardown),
6cf099
+        cmocka_unit_test_setup_teardown(test_sysdb_enumpwent_filter_views,
6cf099
+                                        test_enum_users_setup,
6cf099
+                                        test_enum_users_teardown),
6cf099
+        cmocka_unit_test_setup_teardown(test_sysdb_enumgrent,
6cf099
+                                        test_enum_groups_setup,
6cf099
+                                        test_enum_groups_teardown),
6cf099
+        cmocka_unit_test_setup_teardown(test_sysdb_enumgrent_views,
6cf099
+                                        test_enum_groups_setup,
6cf099
+                                        test_enum_groups_teardown),
6cf099
+        cmocka_unit_test_setup_teardown(test_sysdb_enumgrent_filter,
6cf099
+                                        test_enum_groups_setup,
6cf099
+                                        test_enum_groups_teardown),
6cf099
+        cmocka_unit_test_setup_teardown(test_sysdb_enumgrent_filter_views,
6cf099
+                                        test_enum_groups_setup,
6cf099
+                                        test_enum_groups_teardown),
6cf099
     };
6cf099
 
6cf099
     /* Set debug level to invalid value so we can deside if -d 0 was used. */
6cf099
-- 
6cf099
2.4.3
6cf099