Blame SOURCES/0177-SYSDB-Make-the-usage-of-the-filter-more-generic-for-.patch

bb7cd1
From 7d926fb2e8fe21e3fa51bc341189d33658600daf Mon Sep 17 00:00:00 2001
bb7cd1
From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= <fidencio@redhat.com>
bb7cd1
Date: Wed, 24 May 2017 11:52:23 +0200
bb7cd1
Subject: [PATCH 177/181] SYSDB: Make the usage of the filter more generic for
bb7cd1
 search_ts_matches()
bb7cd1
MIME-Version: 1.0
bb7cd1
Content-Type: text/plain; charset=UTF-8
bb7cd1
Content-Transfer-Encoding: 8bit
bb7cd1
bb7cd1
In order to make this function re-usable in different parts of our code,
bb7cd1
let's start passing an already built filter to it instead of having the
bb7cd1
specific code building the name filter there.
bb7cd1
bb7cd1
Related:
bb7cd1
https://pagure.io/SSSD/sssd/issue/3369
bb7cd1
bb7cd1
Signed-off-by: Fabiano FidĂȘncio <fidencio@redhat.com>
bb7cd1
bb7cd1
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
bb7cd1
(cherry picked from commit 8ad57e17779b3ec60246ac58c1691ee15745084c)
bb7cd1
---
bb7cd1
 src/db/sysdb_search.c | 67 +++++++++++++++++++++++++++++++++++++++++----------
bb7cd1
 1 file changed, 54 insertions(+), 13 deletions(-)
bb7cd1
bb7cd1
diff --git a/src/db/sysdb_search.c b/src/db/sysdb_search.c
bb7cd1
index 0c04b84a584e047a0ba8243c9216547ea2791e60..f488442afcc6eef114437a7110722759f86fe19e 100644
bb7cd1
--- a/src/db/sysdb_search.c
bb7cd1
+++ b/src/db/sysdb_search.c
bb7cd1
@@ -489,7 +489,6 @@ errno_t sysdb_search_ts_matches(TALLOC_CTX *mem_ctx,
bb7cd1
                                 const char *filter,
bb7cd1
                                 struct ldb_result **_res)
bb7cd1
 {
bb7cd1
-    char *dn_filter;
bb7cd1
     TALLOC_CTX *tmp_ctx = NULL;
bb7cd1
     struct ldb_result *res;
bb7cd1
     errno_t ret;
bb7cd1
@@ -501,7 +500,7 @@ errno_t sysdb_search_ts_matches(TALLOC_CTX *mem_ctx,
bb7cd1
     }
bb7cd1
 
bb7cd1
     tmp_ctx = talloc_new(NULL);
bb7cd1
-    if (!tmp_ctx) {
bb7cd1
+    if (tmp_ctx == NULL) {
bb7cd1
         return ENOMEM;
bb7cd1
     }
bb7cd1
 
bb7cd1
@@ -511,7 +510,43 @@ errno_t sysdb_search_ts_matches(TALLOC_CTX *mem_ctx,
bb7cd1
         goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
-    dn_filter = talloc_asprintf(tmp_ctx, "(&(%s=%s)(|", SYSDB_NAME, filter);
bb7cd1
+    ret = ldb_search(sysdb->ldb, tmp_ctx, &res, NULL,
bb7cd1
+                     LDB_SCOPE_SUBTREE, attrs, "%s", filter);
bb7cd1
+    if (ret) {
bb7cd1
+        ret = sysdb_error_to_errno(ret);
bb7cd1
+        goto done;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    *_res = talloc_steal(mem_ctx, res);
bb7cd1
+    ret = EOK;
bb7cd1
+
bb7cd1
+done:
bb7cd1
+    talloc_zfree(tmp_ctx);
bb7cd1
+    return ret;
bb7cd1
+}
bb7cd1
+
bb7cd1
+static errno_t sysdb_enum_dn_filter(TALLOC_CTX *mem_ctx,
bb7cd1
+                                    struct ldb_result *ts_res,
bb7cd1
+                                    const char *name_filter,
bb7cd1
+                                    char **_dn_filter)
bb7cd1
+{
bb7cd1
+    TALLOC_CTX *tmp_ctx = NULL;
bb7cd1
+    char *dn_filter;
bb7cd1
+    errno_t ret;
bb7cd1
+
bb7cd1
+    if (ts_res->count == 0) {
bb7cd1
+        *_dn_filter = NULL;
bb7cd1
+        ret = EOK;
bb7cd1
+        goto done;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    tmp_ctx = talloc_new(NULL);
bb7cd1
+    if (tmp_ctx == NULL) {
bb7cd1
+        return ENOMEM;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    dn_filter = talloc_asprintf(tmp_ctx, "(&(%s=%s)(|", SYSDB_NAME,
bb7cd1
+                                name_filter);
bb7cd1
     if (dn_filter == NULL) {
bb7cd1
         ret = ENOMEM;
bb7cd1
         goto done;
bb7cd1
@@ -535,15 +570,9 @@ errno_t sysdb_search_ts_matches(TALLOC_CTX *mem_ctx,
bb7cd1
         goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
-    ret = ldb_search(sysdb->ldb, tmp_ctx, &res, NULL,
bb7cd1
-                     LDB_SCOPE_SUBTREE, attrs, "%s", dn_filter);
bb7cd1
-    if (ret) {
bb7cd1
-        ret = sysdb_error_to_errno(ret);
bb7cd1
-        goto done;
bb7cd1
-    }
bb7cd1
-
bb7cd1
+    *_dn_filter = talloc_steal(mem_ctx, dn_filter);
bb7cd1
     ret = EOK;
bb7cd1
-    *_res = talloc_steal(mem_ctx, res);
bb7cd1
+
bb7cd1
 done:
bb7cd1
     talloc_zfree(tmp_ctx);
bb7cd1
     return ret;
bb7cd1
@@ -558,6 +587,7 @@ int sysdb_enumpwent_filter(TALLOC_CTX *mem_ctx,
bb7cd1
     TALLOC_CTX *tmp_ctx;
bb7cd1
     static const char *attrs[] = SYSDB_PW_ATTRS;
bb7cd1
     char *filter = NULL;
bb7cd1
+    char *dn_filter = NULL;
bb7cd1
     const char *ts_filter = NULL;
bb7cd1
     struct ldb_dn *base_dn;
bb7cd1
     struct ldb_result *res;
bb7cd1
@@ -595,8 +625,13 @@ int sysdb_enumpwent_filter(TALLOC_CTX *mem_ctx,
bb7cd1
         goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
+    ret = sysdb_enum_dn_filter(tmp_ctx, &ts_res, name_filter, &dn_filter);
bb7cd1
+    if (ret != EOK) {
bb7cd1
+        goto done;
bb7cd1
+    }
bb7cd1
+
bb7cd1
     ret = sysdb_search_ts_matches(tmp_ctx, domain->sysdb, attrs, &ts_res,
bb7cd1
-                                  name_filter, &ts_cache_res);
bb7cd1
+                                  dn_filter, &ts_cache_res);
bb7cd1
     if (ret != EOK && ret != ENOENT) {
bb7cd1
         goto done;
bb7cd1
     }
bb7cd1
@@ -1052,6 +1087,7 @@ int sysdb_enumgrent_filter(TALLOC_CTX *mem_ctx,
bb7cd1
     const char *filter = NULL;
bb7cd1
     const char *ts_filter = NULL;
bb7cd1
     const char *base_filter;
bb7cd1
+    char *dn_filter = NULL;
bb7cd1
     struct ldb_dn *base_dn;
bb7cd1
     struct ldb_result *res;
bb7cd1
     struct ldb_result ts_res;
bb7cd1
@@ -1100,8 +1136,13 @@ int sysdb_enumgrent_filter(TALLOC_CTX *mem_ctx,
bb7cd1
         goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
+    ret = sysdb_enum_dn_filter(tmp_ctx, &ts_res, name_filter, &dn_filter);
bb7cd1
+    if (ret != EOK) {
bb7cd1
+        goto done;
bb7cd1
+    }
bb7cd1
+
bb7cd1
     ret = sysdb_search_ts_matches(tmp_ctx, domain->sysdb, attrs, &ts_res,
bb7cd1
-                                  name_filter, &ts_cache_res);
bb7cd1
+                                  dn_filter, &ts_cache_res);
bb7cd1
     if (ret != EOK && ret != ENOENT) {
bb7cd1
         goto done;
bb7cd1
     }
bb7cd1
-- 
bb7cd1
2.9.4
bb7cd1