>From d1293b63adcab4298228af225f3af5803759d34b Mon Sep 17 00:00:00 2001
From: Pavel Reichl <preichl@redhat.com>
Date: Fri, 11 Jul 2014 15:21:59 +0100
Subject: [PATCH] SYSDB: SSS_LDB_SEARCH - macro around ldb_search

This patch amends previous patch 5153e8b9793dea1e212ca08af0f77ea1d023cbb7.

Macro SSS_LDB_SEARCH is used instead of using fuction sss_ldb_search as
a wrapper around ldb_search which could lead to premature expansion of
variadic parameters.

Part of solution for:
https://fedorahosted.org/sssd/ticket/1991
---
 src/db/sysdb.h          | 19 +++++++++++++-----
 src/db/sysdb_ops.c      | 51 -------------------------------------------------
 src/db/sysdb_search.c   | 10 +++++-----
 src/tests/sysdb-tests.c | 38 ++++++++++++++++++------------------
 4 files changed, 38 insertions(+), 80 deletions(-)

diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 17cd5110c9bdafd8d7b18e621188523ed41e5c8a..a613c84ffa9e4ab6ebdc930a8c4876e69aa3cd13 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -481,11 +481,6 @@ int sysdb_get_netgroup_attr(TALLOC_CTX *mem_ctx,
                             const char **attributes,
                             struct ldb_result **res);
 
-errno_t sss_ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
-                       struct ldb_result **_result, struct ldb_dn *base,
-                       enum ldb_scope scope, const char * const *attrs,
-                       const char *exp_fmt, ...) SSS_ATTRIBUTE_PRINTF(7, 8);
-
 /* functions that modify the databse
  * they have to be called within a transaction
  * See sysdb_transaction_send()/_recv() */
@@ -864,4 +859,18 @@ errno_t sysdb_search_object_by_sid(TALLOC_CTX *mem_ctx,
                                    const char *sid_str,
                                    const char **attrs,
                                    struct ldb_result **msg);
+
+#define SSS_LDB_SEARCH(ret, ldb, mem_ctx, _result, base, scope, attrs,    \
+                       exp_fmt, ...)                                      \
+{                                                                         \
+    int lret;                                                             \
+                                                                          \
+    lret = ldb_search(ldb, mem_ctx, _result, base, scope, attrs, exp_fmt, \
+                      ##__VA_ARGS__);                                     \
+    ret = sysdb_error_to_errno(lret);                                     \
+    if (ret == EOK && (*_result)->count == 0) {                           \
+        ret = ENOENT;                                                     \
+    }                                                                     \
+} while(0)
+
 #endif /* __SYS_DB_H__ */
diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c
index d16cfb92059521e92a40b477ebe8e66cfdd39508..4d31cb6a0e83370a2c49a666f9474d93baf430b0 100644
--- a/src/db/sysdb_ops.c
+++ b/src/db/sysdb_ops.c
@@ -74,57 +74,6 @@ static uint32_t get_attr_as_uint32(struct ldb_message *msg, const char *attr)
     return l;
 }
 
-
-/* Wrapper around ldb_search to ensure that if zero results are found then
- * ENOENT is returned
- */
-errno_t sss_ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
-                       struct ldb_result **_result, struct ldb_dn *base,
-                       enum ldb_scope scope, const char * const *attrs,
-                       const char *exp_fmt, ...)
-{
-    char *s;
-    int lret;
-    va_list ap;
-    errno_t ret;
-    TALLOC_CTX *tmp_ctx = NULL;
-
-    if (exp_fmt != NULL) {
-        tmp_ctx = talloc_new(NULL);
-        if (tmp_ctx == NULL) {
-            ret = ENOMEM;
-            goto done;
-        }
-
-        va_start(ap, exp_fmt);
-        s = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
-        va_end(ap);
-
-        if (s == NULL) {
-            DEBUG(SSSDBG_MINOR_FAILURE, "Failed to process filter.\n");
-            ret = ENOMEM;
-            goto done;
-        }
-        lret = ldb_search(ldb, mem_ctx, _result, base, scope, attrs, "%s", s);
-    } else {
-        lret = ldb_search(ldb, mem_ctx, _result, base, scope, attrs, NULL);
-    }
-
-    ret = sysdb_error_to_errno(lret);
-    if (ret != EOK) {
-        goto done;
-    }
-
-    if ((*_result)->count == 0) {
-        ret = ENOENT;
-        goto done;
-    }
-
-done:
-    talloc_free(tmp_ctx);
-    return ret;
-}
-
 /*
  * The wrapper around ldb_modify that uses LDB_CONTROL_PERMISSIVE_MODIFY_OID
  * so that on adds entries that already exist are skipped and similarly
diff --git a/src/db/sysdb_search.c b/src/db/sysdb_search.c
index ff14c4a980d5421d3961fb66c31b6ab266b4cef4..3d789ae85daef5ea1a4a2c44255b5f7dda6af1ec 100644
--- a/src/db/sysdb_search.c
+++ b/src/db/sysdb_search.c
@@ -815,11 +815,11 @@ errno_t sysdb_getnetgr(TALLOC_CTX *mem_ctx,
         goto done;
     }
 
-    ret = sss_ldb_search(domain->sysdb->ldb, tmp_ctx, &result, base_dn,
-                         LDB_SCOPE_SUBTREE, attrs,
-                         SYSDB_NETGR_TRIPLES_FILTER, lc_sanitized_netgroup,
-                         sanitized_netgroup, sanitized_netgroup,
-                         netgroup_dn);
+    SSS_LDB_SEARCH(ret, domain->sysdb->ldb, tmp_ctx, &result, base_dn,
+                   LDB_SCOPE_SUBTREE, attrs,
+                   SYSDB_NETGR_TRIPLES_FILTER, lc_sanitized_netgroup,
+                   sanitized_netgroup, sanitized_netgroup,
+                   netgroup_dn);
 
     if (ret == EOK || ret == ENOENT) {
         *res = talloc_steal(mem_ctx, result);
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index d24bd4f7d4f0e8f85afa7c98da8edc0635b2e328..4061a462b1de3d94ca4ca3fef1b3ac585d670890 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -3979,7 +3979,7 @@ START_TEST(test_odd_characters)
 }
 END_TEST
 
-START_TEST(test_sss_ldb_search)
+START_TEST(test_SSS_LDB_SEARCH)
 {
     errno_t ret;
     struct sysdb_test_ctx *test_ctx;
@@ -4013,10 +4013,10 @@ START_TEST(test_sss_ldb_search)
     /* Retrieve */
 
     /* Empty filter */
-    ret = sss_ldb_search(test_ctx->sysdb->ldb, test_ctx, &res, group_dn,
-                         LDB_SCOPE_BASE, NULL, NULL);
+    SSS_LDB_SEARCH(ret, test_ctx->sysdb->ldb, test_ctx, &res, group_dn,
+                   LDB_SCOPE_BASE, NULL, NULL);
 
-    fail_unless(ret == EOK, "sss_ldb_search error [%d][%s]",
+    fail_unless(ret == EOK, "SSS_LDB_SEARCH error [%d][%s]",
                 ret, strerror(ret));
 
     fail_unless(res->count == 1, "Received [%d] responses",
@@ -4030,27 +4030,27 @@ START_TEST(test_sss_ldb_search)
     talloc_zfree(res);
 
     /* Non-empty filter */
-    ret = sss_ldb_search(test_ctx->sysdb->ldb, test_ctx, &res, group_dn,
-                         LDB_SCOPE_BASE, NULL, "objectClass=group");
+    SSS_LDB_SEARCH(ret, test_ctx->sysdb->ldb, test_ctx, &res, group_dn,
+                   LDB_SCOPE_BASE, NULL, "objectClass=group");
 
-    fail_unless(ret == EOK, "sss_ldb_search error [%d][%s]",
+    fail_unless(ret == EOK, "SSS_LDB_SEARCH error [%d][%s]",
                 ret, strerror(ret));
     talloc_zfree(res);
 
     /* Filter yeilding no results */
-    ret = sss_ldb_search(test_ctx->sysdb->ldb, test_ctx, &res, group_dn,
-                         LDB_SCOPE_BASE, NULL,
-                         "objectClass=nonExistingObjectClass");
+    SSS_LDB_SEARCH(ret, test_ctx->sysdb->ldb, test_ctx, &res, group_dn,
+                   LDB_SCOPE_BASE, NULL,
+                   "objectClass=nonExistingObjectClass");
 
     fail_unless(ret == ENOENT, "sss_ldb_search error [%d][%s]",
                 ret, strerror(ret));
     talloc_zfree(res);
 
     /* Non-existing dn */
-    ret = sss_ldb_search(test_ctx->sysdb->ldb, test_ctx, &res, nonexist_dn,
-                         LDB_SCOPE_BASE, NULL, NULL);
+    SSS_LDB_SEARCH(ret, test_ctx->sysdb->ldb, test_ctx, &res, nonexist_dn,
+                   LDB_SCOPE_BASE, NULL, NULL);
 
-    fail_unless(ret == ENOENT, "sss_ldb_search error [%d][%s]",
+    fail_unless(ret == ENOENT, "SSS_LDB_SEARCH error [%d][%s]",
                 ret, strerror(ret));
     talloc_zfree(res);
 
@@ -4626,13 +4626,13 @@ START_TEST (test_sysdb_search_return_ENOENT)
     talloc_zfree(msgs);
     talloc_zfree(user_dn);
 
-    /* sss_ldb_search */
+    /* SSS_LDB_SEARCH */
     user_dn = sysdb_user_dn(test_ctx, test_ctx->domain, "nonexisting_user");
     fail_if(user_dn == NULL, "sysdb_user_dn failed");
-    ret = sss_ldb_search(test_ctx->sysdb->ldb, test_ctx, &res, user_dn,
-                         LDB_SCOPE_BASE, NULL, "objectClass=user");
+    SSS_LDB_SEARCH(ret, test_ctx->sysdb->ldb, test_ctx, &res, user_dn,
+                   LDB_SCOPE_BASE, NULL, "objectClass=user");
 
-    fail_unless(ret == ENOENT, "sss_ldb_search failed: %d, %s",
+    fail_unless(ret == ENOENT, "SSS_LDB_SEARCH failed: %d, %s",
                                ret, strerror(ret));
 
     talloc_zfree(res);
@@ -5886,8 +5886,8 @@ Suite *create_sysdb_suite(void)
     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_check_nested_double_ghosts,
                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
 
-    /* sss_ldb_search */
-    tcase_add_test(tc_sysdb, test_sss_ldb_search);
+    /* SSS_LDB_SEARCH */
+    tcase_add_test(tc_sysdb, test_SSS_LDB_SEARCH);
 
     /* This loop counts backwards so the indexing is a little odd */
     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_mod_replace_keep,
-- 
1.9.3

