Blame SOURCES/0193-IPA-fix-handling-of-certmap_ctx.patch

ecf709
From 2fca2f1b77c0e9ae82e1a24bbf89fbc3115a5e24 Mon Sep 17 00:00:00 2001
ecf709
From: Sumit Bose <sbose@redhat.com>
ecf709
Date: Wed, 6 Sep 2017 16:42:20 +0200
ecf709
Subject: [PATCH 193/194] IPA: fix handling of certmap_ctx
ecf709
MIME-Version: 1.0
ecf709
Content-Type: text/plain; charset=UTF-8
ecf709
Content-Transfer-Encoding: 8bit
ecf709
ecf709
This patch fixes a use-after-free in the AD provider part and
ecf709
initializes the certmap_ctx with data from the cache at startup.
ecf709
ecf709
Related to https://pagure.io/SSSD/sssd/issue/3508
ecf709
ecf709
Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
ecf709
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
ecf709
(cherry picked from commit f2e70ec742cd7aab82b74d7e4b424ba3258da7aa)
ecf709
---
ecf709
 Makefile.am                               |  20 +++
ecf709
 src/providers/ipa/ipa_init.c              |   7 +
ecf709
 src/providers/ipa/ipa_subdomains.c        |  53 +------
ecf709
 src/providers/ipa/ipa_subdomains_server.c |   4 +-
ecf709
 src/providers/ldap/ldap_common.h          |   5 +
ecf709
 src/providers/ldap/ldap_id.c              |   5 +-
ecf709
 src/providers/ldap/sdap.h                 |   4 +-
ecf709
 src/providers/ldap/sdap_certmap.c         | 152 +++++++++++++++++++
ecf709
 src/tests/cmocka/test_sdap_certmap.c      | 244 ++++++++++++++++++++++++++++++
ecf709
 9 files changed, 441 insertions(+), 53 deletions(-)
ecf709
 create mode 100644 src/providers/ldap/sdap_certmap.c
ecf709
 create mode 100644 src/tests/cmocka/test_sdap_certmap.c
ecf709
ecf709
diff --git a/Makefile.am b/Makefile.am
ecf709
index 503c8cfd795b503f566431c08a56a56147180322..907c3256a154ebe2aae5a1667744e1dfbe8abaae 100644
ecf709
--- a/Makefile.am
ecf709
+++ b/Makefile.am
ecf709
@@ -257,6 +257,7 @@ if HAVE_CMOCKA
ecf709
         test_search_bases \
ecf709
         test_ldap_auth \
ecf709
         test_sdap_access \
ecf709
+        test_sdap_certmap \
ecf709
         sdap-tests \
ecf709
         test_sysdb_ts_cache \
ecf709
         test_sysdb_views \
ecf709
@@ -2662,6 +2663,24 @@ test_sdap_access_LDADD = \
ecf709
     libdlopen_test_providers.la \
ecf709
     $(NULL)
ecf709
 
ecf709
+test_sdap_certmap_SOURCES = \
ecf709
+    src/tests/cmocka/test_sdap_certmap.c \
ecf709
+    src/providers/ldap/sdap_certmap.c \
ecf709
+    $(NULL)
ecf709
+test_sdap_certmap_CFLAGS = \
ecf709
+    $(AM_CFLAGS) \
ecf709
+    $(TALLOC_CFLAGS) \
ecf709
+    $(POPT_CFLAGS) \
ecf709
+    $(NULL)
ecf709
+test_sdap_certmap_LDADD = \
ecf709
+    $(CMOCKA_LIBS) \
ecf709
+    $(TALLOC_LIBS) \
ecf709
+    $(POPT_LIBS) \
ecf709
+    $(SSSD_INTERNAL_LTLIBS) \
ecf709
+    libsss_test_common.la \
ecf709
+    libsss_certmap.la \
ecf709
+    $(NULL)
ecf709
+
ecf709
 ad_access_filter_tests_SOURCES = \
ecf709
     src/tests/cmocka/test_ad_access_filter.c
ecf709
 ad_access_filter_tests_LDADD = \
ecf709
@@ -3706,6 +3725,7 @@ libsss_ldap_common_la_SOURCES = \
ecf709
     src/providers/ldap/sdap_child_helpers.c \
ecf709
     src/providers/ldap/sdap_fd_events.c \
ecf709
     src/providers/ldap/sdap_id_op.c \
ecf709
+    src/providers/ldap/sdap_certmap.c \
ecf709
     src/providers/ldap/sdap_idmap.c \
ecf709
     src/providers/ldap/sdap_idmap.h \
ecf709
     src/providers/ldap/sdap_range.c \
ecf709
diff --git a/src/providers/ipa/ipa_init.c b/src/providers/ipa/ipa_init.c
ecf709
index 7dec4d1fb8541a48470d4e44f10838e5bea67ad5..2b58b1341463f8947d51dee2076dbe92e3093558 100644
ecf709
--- a/src/providers/ipa/ipa_init.c
ecf709
+++ b/src/providers/ipa/ipa_init.c
ecf709
@@ -649,6 +649,13 @@ static errno_t ipa_init_misc(struct be_ctx *be_ctx,
ecf709
         return ENOMEM;
ecf709
     }
ecf709
 
ecf709
+    ret = sdap_init_certmap(sdap_id_ctx, sdap_id_ctx);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_CRIT_FAILURE,
ecf709
+              "Failed to initialized certificate mapping.\n");
ecf709
+        return ret;
ecf709
+    }
ecf709
+
ecf709
     return EOK;
ecf709
 }
ecf709
 
ecf709
diff --git a/src/providers/ipa/ipa_subdomains.c b/src/providers/ipa/ipa_subdomains.c
ecf709
index ef348adf4a36e870f44387bd700f5c2beea3bfd6..6f0ff50bde234f72d62f43635d9a787316c78430 100644
ecf709
--- a/src/providers/ipa/ipa_subdomains.c
ecf709
+++ b/src/providers/ipa/ipa_subdomains.c
ecf709
@@ -311,25 +311,6 @@ struct priv_sss_debug {
ecf709
     int level;
ecf709
 };
ecf709
 
ecf709
-void ext_debug(void *private, const char *file, long line, const char *function,
ecf709
-               const char *format, ...)
ecf709
-{
ecf709
-    va_list ap;
ecf709
-    struct priv_sss_debug *data = private;
ecf709
-    int level = SSSDBG_OP_FAILURE;
ecf709
-
ecf709
-    if (data != NULL) {
ecf709
-        level = data->level;
ecf709
-    }
ecf709
-
ecf709
-    if (DEBUG_IS_SET(level)) {
ecf709
-        va_start(ap, format);
ecf709
-        sss_vdebug_fn(file, line, function, level, APPEND_LINE_FEED,
ecf709
-                      format, ap);
ecf709
-        va_end(ap);
ecf709
-    }
ecf709
-}
ecf709
-
ecf709
 static errno_t ipa_certmap_parse_results(TALLOC_CTX *mem_ctx,
ecf709
                                          struct sss_domain_info *domain,
ecf709
                                          struct sdap_options *sdap_opts,
ecf709
@@ -344,7 +325,6 @@ static errno_t ipa_certmap_parse_results(TALLOC_CTX *mem_ctx,
ecf709
     size_t c;
ecf709
     size_t lc = 0;
ecf709
     int ret;
ecf709
-    struct sss_certmap_ctx *certmap_ctx = NULL;
ecf709
     const char **ocs = NULL;
ecf709
     bool user_name_hint = false;
ecf709
 
ecf709
@@ -444,50 +424,29 @@ static errno_t ipa_certmap_parse_results(TALLOC_CTX *mem_ctx,
ecf709
 
ecf709
     certmap_list[lc] = NULL;
ecf709
 
ecf709
-    ret = sss_certmap_init(mem_ctx, ext_debug, NULL, &certmap_ctx);
ecf709
-    if (ret != 0) {
ecf709
-        DEBUG(SSSDBG_OP_FAILURE, "sss_certmap_init failed.\n");
ecf709
+    ret = sdap_setup_certmap(sdap_opts->sdap_certmap_ctx, certmap_list);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "sdap_setup_certmap failed.\n");
ecf709
         goto done;
ecf709
     }
ecf709
 
ecf709
-    for (c = 0; certmap_list[c] != NULL; c++) {
ecf709
-        DEBUG(SSSDBG_TRACE_ALL, "Trying to add rule [%s][%d][%s][%s].\n",
ecf709
-                                certmap_list[c]->name,
ecf709
-                                certmap_list[c]->priority,
ecf709
-                                certmap_list[c]->match_rule,
ecf709
-                                certmap_list[c]->map_rule);
ecf709
-
ecf709
-        ret = sss_certmap_add_rule(certmap_ctx, certmap_list[c]->priority,
ecf709
-                                   certmap_list[c]->match_rule,
ecf709
-                                   certmap_list[c]->map_rule,
ecf709
-                                   certmap_list[c]->domains);
ecf709
-        if (ret != 0) {
ecf709
-            DEBUG(SSSDBG_CRIT_FAILURE,
ecf709
-                  "sss_certmap_add_rule failed for rule [%s], skipping. "
ecf709
-                  "Please check for typos and if rule syntax is supported.\n",
ecf709
-                  certmap_list[c]->name);
ecf709
-            goto done;
ecf709
-        }
ecf709
-    }
ecf709
-
ecf709
     ret = sysdb_update_certmap(domain->sysdb, certmap_list, user_name_hint);
ecf709
     if (ret != EOK) {
ecf709
         DEBUG(SSSDBG_OP_FAILURE, "sysdb_update_certmap failed");
ecf709
         goto done;
ecf709
     }
ecf709
 
ecf709
-    sss_certmap_free_ctx(sdap_opts->certmap_ctx);
ecf709
-    sdap_opts->certmap_ctx = talloc_steal(sdap_opts, certmap_ctx);
ecf709
-
ecf709
     if (_certmap_list != NULL) {
ecf709
         *_certmap_list = certmap_list;
ecf709
+    } else {
ecf709
+        talloc_free(certmap_list);
ecf709
     }
ecf709
+
ecf709
     ret = EOK;
ecf709
 
ecf709
 done:
ecf709
     talloc_free(ocs);
ecf709
     if (ret != EOK) {
ecf709
-        sss_certmap_free_ctx(certmap_ctx);
ecf709
         talloc_free(certmap_list);
ecf709
     }
ecf709
 
ecf709
diff --git a/src/providers/ipa/ipa_subdomains_server.c b/src/providers/ipa/ipa_subdomains_server.c
ecf709
index 443d83824f329b9d8c3d8e820113e1029f832240..56470ac824feaa59eecbd9f442682220237c2412 100644
ecf709
--- a/src/providers/ipa/ipa_subdomains_server.c
ecf709
+++ b/src/providers/ipa/ipa_subdomains_server.c
ecf709
@@ -361,8 +361,8 @@ ipa_ad_ctx_new(struct be_ctx *be_ctx,
ecf709
         id_ctx->sdap_id_ctx->opts->idmap_ctx;
ecf709
 
ecf709
     /* Set up the certificate mapping context */
ecf709
-    ad_id_ctx->sdap_id_ctx->opts->certmap_ctx =
ecf709
-        id_ctx->sdap_id_ctx->opts->certmap_ctx;
ecf709
+    ad_id_ctx->sdap_id_ctx->opts->sdap_certmap_ctx =
ecf709
+        id_ctx->sdap_id_ctx->opts->sdap_certmap_ctx;
ecf709
 
ecf709
     *_ad_id_ctx = ad_id_ctx;
ecf709
     return EOK;
ecf709
diff --git a/src/providers/ldap/ldap_common.h b/src/providers/ldap/ldap_common.h
ecf709
index 1acda4147576503b18f61e0bb56f8efd2263fd44..0510b7d5ab5121bd96f699e8e59520a2a18a604f 100644
ecf709
--- a/src/providers/ldap/ldap_common.h
ecf709
+++ b/src/providers/ldap/ldap_common.h
ecf709
@@ -362,4 +362,9 @@ sdap_id_ctx_new(TALLOC_CTX *mem_ctx, struct be_ctx *bectx,
ecf709
 errno_t sdap_refresh_init(struct be_refresh_ctx *refresh_ctx,
ecf709
                           struct sdap_id_ctx *id_ctx);
ecf709
 
ecf709
+errno_t sdap_init_certmap(TALLOC_CTX *mem_ctx, struct sdap_id_ctx *id_ctx);
ecf709
+
ecf709
+errno_t sdap_setup_certmap(struct sdap_certmap_ctx *sdap_certmap_ctx,
ecf709
+                           struct certmap_info **certmap_list);
ecf709
+struct sss_certmap_ctx *sdap_get_sss_certmap(struct sdap_certmap_ctx *ctx);
ecf709
 #endif /* _LDAP_COMMON_H_ */
ecf709
diff --git a/src/providers/ldap/ldap_id.c b/src/providers/ldap/ldap_id.c
ecf709
index 557712e8dc2b2bde664b4054fa2f8eb39df84d73..93204d35ea3782c9aa5d622a962c295869472631 100644
ecf709
--- a/src/providers/ldap/ldap_id.c
ecf709
+++ b/src/providers/ldap/ldap_id.c
ecf709
@@ -252,9 +252,8 @@ struct tevent_req *users_get_send(TALLOC_CTX *memctx,
ecf709
         }
ecf709
 
ecf709
         ret = sss_cert_derb64_to_ldap_filter(state, filter_value, attr_name,
ecf709
-                                             ctx->opts->certmap_ctx,
ecf709
-                                             state->domain,
ecf709
-                                             &user_filter);
ecf709
+                              sdap_get_sss_certmap(ctx->opts->sdap_certmap_ctx),
ecf709
+                              state->domain, &user_filter);
ecf709
         if (ret != EOK) {
ecf709
             DEBUG(SSSDBG_OP_FAILURE,
ecf709
                   "sss_cert_derb64_to_ldap_filter failed.\n");
ecf709
diff --git a/src/providers/ldap/sdap.h b/src/providers/ldap/sdap.h
ecf709
index afdc01948eefe9dda943c8c7ad01a42dd76a1da8..c85fbe9e78e5eefa7e33ea8055730118b0871a4c 100644
ecf709
--- a/src/providers/ldap/sdap.h
ecf709
+++ b/src/providers/ldap/sdap.h
ecf709
@@ -446,6 +446,8 @@ struct sdap_ext_member_ctx {
ecf709
     ext_member_recv_fn_t ext_member_resolve_recv;
ecf709
 };
ecf709
 
ecf709
+struct sdap_certmap_ctx;
ecf709
+
ecf709
 struct sdap_options {
ecf709
     struct dp_option *basic;
ecf709
     struct sdap_attr_map *gen_map;
ecf709
@@ -481,7 +483,7 @@ struct sdap_options {
ecf709
     enum dc_functional_level dc_functional_level;
ecf709
 
ecf709
     /* Certificate mapping support */
ecf709
-    struct sss_certmap_ctx *certmap_ctx;
ecf709
+    struct sdap_certmap_ctx *sdap_certmap_ctx;
ecf709
 };
ecf709
 
ecf709
 struct sdap_server_opts {
ecf709
diff --git a/src/providers/ldap/sdap_certmap.c b/src/providers/ldap/sdap_certmap.c
ecf709
new file mode 100644
ecf709
index 0000000000000000000000000000000000000000..fcf88a9c69482c8668d486cd2ab0ba37c847e46d
ecf709
--- /dev/null
ecf709
+++ b/src/providers/ldap/sdap_certmap.c
ecf709
@@ -0,0 +1,152 @@
ecf709
+
ecf709
+/*
ecf709
+    SSSD
ecf709
+
ecf709
+    Authors:
ecf709
+        Sumit Bose <sbose@redhat.com>
ecf709
+
ecf709
+    Copyright (C) 2017 Red Hat
ecf709
+
ecf709
+    This program is free software; you can redistribute it and/or modify
ecf709
+    it under the terms of the GNU General Public License as published by
ecf709
+    the Free Software Foundation; either version 3 of the License, or
ecf709
+    (at your option) any later version.
ecf709
+
ecf709
+    This program is distributed in the hope that it will be useful,
ecf709
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
ecf709
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ecf709
+    GNU General Public License for more details.
ecf709
+
ecf709
+    You should have received a copy of the GNU General Public License
ecf709
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
ecf709
+*/
ecf709
+
ecf709
+#include "util/util.h"
ecf709
+#include "lib/certmap/sss_certmap.h"
ecf709
+#include "providers/ldap/ldap_common.h"
ecf709
+
ecf709
+struct sdap_certmap_ctx {
ecf709
+    struct sss_certmap_ctx *certmap_ctx;
ecf709
+};
ecf709
+
ecf709
+struct priv_sss_debug {
ecf709
+    int level;
ecf709
+};
ecf709
+
ecf709
+static void ext_debug(void *private, const char *file, long line,
ecf709
+                      const char *function, const char *format, ...)
ecf709
+{
ecf709
+    va_list ap;
ecf709
+    struct priv_sss_debug *data = private;
ecf709
+    int level = SSSDBG_OP_FAILURE;
ecf709
+
ecf709
+    if (data != NULL) {
ecf709
+        level = data->level;
ecf709
+    }
ecf709
+
ecf709
+    if (DEBUG_IS_SET(level)) {
ecf709
+        va_start(ap, format);
ecf709
+        sss_vdebug_fn(file, line, function, level, APPEND_LINE_FEED,
ecf709
+                      format, ap);
ecf709
+        va_end(ap);
ecf709
+    }
ecf709
+}
ecf709
+
ecf709
+struct sss_certmap_ctx *sdap_get_sss_certmap(struct sdap_certmap_ctx *ctx)
ecf709
+{
ecf709
+    return ctx == NULL ? NULL : ctx->certmap_ctx;
ecf709
+}
ecf709
+
ecf709
+errno_t sdap_setup_certmap(struct sdap_certmap_ctx *sdap_certmap_ctx,
ecf709
+                           struct certmap_info **certmap_list)
ecf709
+{
ecf709
+    int ret;
ecf709
+    struct sss_certmap_ctx *sss_certmap_ctx = NULL;
ecf709
+    size_t c;
ecf709
+
ecf709
+    if (sdap_certmap_ctx == NULL) {
ecf709
+        DEBUG(SSSDBG_CRIT_FAILURE, "Missing sdap_certmap_ctx.\n");
ecf709
+        return EINVAL;
ecf709
+    }
ecf709
+
ecf709
+    if (certmap_list == NULL || *certmap_list == NULL) {
ecf709
+        DEBUG(SSSDBG_TRACE_ALL, "No certmap data, nothing to do.\n");
ecf709
+        ret = EOK;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    ret = sss_certmap_init(sdap_certmap_ctx, ext_debug, NULL, &sss_certmap_ctx);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "sss_certmap_init failed.\n");
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    for (c = 0; certmap_list[c] != NULL; c++) {
ecf709
+        DEBUG(SSSDBG_TRACE_ALL, "Trying to add rule [%s][%d][%s][%s].\n",
ecf709
+                                certmap_list[c]->name,
ecf709
+                                certmap_list[c]->priority,
ecf709
+                                certmap_list[c]->match_rule,
ecf709
+                                certmap_list[c]->map_rule);
ecf709
+
ecf709
+        ret = sss_certmap_add_rule(sss_certmap_ctx, certmap_list[c]->priority,
ecf709
+                                   certmap_list[c]->match_rule,
ecf709
+                                   certmap_list[c]->map_rule,
ecf709
+                                   certmap_list[c]->domains);
ecf709
+        if (ret != 0) {
ecf709
+            DEBUG(SSSDBG_CRIT_FAILURE,
ecf709
+                  "sss_certmap_add_rule failed for rule [%s] "
ecf709
+                  "with error [%d][%s], skipping. "
ecf709
+                  "Please check for typos and if rule syntax is supported.\n",
ecf709
+                  certmap_list[c]->name, ret, sss_strerror(ret));
ecf709
+            continue;
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
+    ret = EOK;
ecf709
+
ecf709
+done:
ecf709
+    if (ret == EOK) {
ecf709
+        sss_certmap_free_ctx(sdap_certmap_ctx->certmap_ctx);
ecf709
+        sdap_certmap_ctx->certmap_ctx = sss_certmap_ctx;
ecf709
+    } else {
ecf709
+        sss_certmap_free_ctx(sss_certmap_ctx);
ecf709
+    }
ecf709
+
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
+errno_t sdap_init_certmap(TALLOC_CTX *mem_ctx, struct sdap_id_ctx *id_ctx)
ecf709
+{
ecf709
+    int ret;
ecf709
+    bool hint;
ecf709
+    struct certmap_info **certmap_list = NULL;
ecf709
+
ecf709
+    if (id_ctx->opts->sdap_certmap_ctx == NULL) {
ecf709
+        id_ctx->opts->sdap_certmap_ctx = talloc_zero(mem_ctx,
ecf709
+                                                     struct sdap_certmap_ctx);
ecf709
+        if (id_ctx->opts->sdap_certmap_ctx == NULL) {
ecf709
+            DEBUG(SSSDBG_OP_FAILURE, "talloc_zero failed.\n");
ecf709
+            return ENOMEM;
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
+    ret = sysdb_get_certmap(mem_ctx, id_ctx->be->domain->sysdb,
ecf709
+                            &certmap_list, &hint);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "sysdb_get_certmap failed.\n");
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    ret = sdap_setup_certmap(id_ctx->opts->sdap_certmap_ctx, certmap_list);
ecf709
+    if (ret != EOK) {
ecf709
+            DEBUG(SSSDBG_OP_FAILURE, "sdap_setup_certmap failed.\n");
ecf709
+            goto done;
ecf709
+    }
ecf709
+
ecf709
+    ret = EOK;
ecf709
+
ecf709
+done:
ecf709
+    talloc_free(certmap_list);
ecf709
+
ecf709
+    return ret;
ecf709
+}
ecf709
diff --git a/src/tests/cmocka/test_sdap_certmap.c b/src/tests/cmocka/test_sdap_certmap.c
ecf709
new file mode 100644
ecf709
index 0000000000000000000000000000000000000000..9df5666844c8582a3fdb5b086720f1f2819f53f3
ecf709
--- /dev/null
ecf709
+++ b/src/tests/cmocka/test_sdap_certmap.c
ecf709
@@ -0,0 +1,244 @@
ecf709
+/*
ecf709
+    Authors:
ecf709
+        Sumit Bose <sbose@redhat.com>
ecf709
+
ecf709
+    Copyright (C) 2017 Red Hat
ecf709
+
ecf709
+    SSSD tests - sdap certmap
ecf709
+
ecf709
+    This program is free software; you can redistribute it and/or modify
ecf709
+    it under the terms of the GNU General Public License as published by
ecf709
+    the Free Software Foundation; either version 3 of the License, or
ecf709
+    (at your option) any later version.
ecf709
+
ecf709
+    This program is distributed in the hope that it will be useful,
ecf709
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
ecf709
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ecf709
+    GNU General Public License for more details.
ecf709
+
ecf709
+    You should have received a copy of the GNU General Public License
ecf709
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
ecf709
+*/
ecf709
+
ecf709
+#include <stdarg.h>
ecf709
+#include <stdbool.h>
ecf709
+#include <setjmp.h>
ecf709
+#include <unistd.h>
ecf709
+#include <cmocka.h>
ecf709
+#include <popt.h>
ecf709
+
ecf709
+#include "providers/ldap/ldap_common.h"
ecf709
+#include "tests/common.h"
ecf709
+#include "db/sysdb.h"
ecf709
+
ecf709
+#define TESTS_PATH "certmap_" BASE_FILE_STEM
ecf709
+#define TEST_CONF_DB "test_sysdb_certmap.ldb"
ecf709
+#define TEST_ID_PROVIDER "ldap"
ecf709
+#define TEST_DOM_NAME "certmap_test"
ecf709
+
ecf709
+struct certmap_info map_a = { discard_const("map_a"), 11,
ecf709
+                              NULL, discard_const("(abc=def)"),
ecf709
+                              NULL };
ecf709
+struct certmap_info map_b = { discard_const("map_b"), UINT_MAX,
ecf709
+                              NULL, NULL, NULL };
ecf709
+struct certmap_info *certmap[] = { &map_a, &map_b, NULL };
ecf709
+
ecf709
+struct certmap_test_ctx {
ecf709
+    struct sss_test_ctx *tctx;
ecf709
+    struct sdap_id_ctx *id_ctx;
ecf709
+};
ecf709
+
ecf709
+static int test_sysdb_setup(void **state)
ecf709
+{
ecf709
+    int ret;
ecf709
+    struct certmap_test_ctx *test_ctx;
ecf709
+    struct sss_test_conf_param params[] = {
ecf709
+        { NULL, NULL },             /* Sentinel */
ecf709
+    };
ecf709
+
ecf709
+    assert_true(leak_check_setup());
ecf709
+
ecf709
+    test_ctx = talloc_zero(global_talloc_context,
ecf709
+                           struct certmap_test_ctx);
ecf709
+    assert_non_null(test_ctx);
ecf709
+    check_leaks_push(test_ctx);
ecf709
+
ecf709
+    test_dom_suite_setup(TESTS_PATH);
ecf709
+
ecf709
+    test_ctx->tctx = create_dom_test_ctx(test_ctx, TESTS_PATH,
ecf709
+                                         TEST_CONF_DB, TEST_DOM_NAME,
ecf709
+                                         TEST_ID_PROVIDER, params);
ecf709
+    assert_non_null(test_ctx->tctx);
ecf709
+
ecf709
+    ret = sysdb_update_certmap(test_ctx->tctx->sysdb, certmap, false);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    test_ctx->id_ctx = talloc_zero(test_ctx->tctx, struct sdap_id_ctx);
ecf709
+    assert_non_null(test_ctx->id_ctx);
ecf709
+
ecf709
+    test_ctx->id_ctx->opts = talloc_zero(test_ctx->tctx, struct sdap_options);
ecf709
+    assert_non_null(test_ctx->id_ctx->opts);
ecf709
+
ecf709
+    test_ctx->id_ctx->be = talloc_zero(test_ctx->tctx, struct be_ctx);
ecf709
+    assert_non_null(test_ctx->id_ctx->be);
ecf709
+    test_ctx->id_ctx->be->domain = test_ctx->tctx->dom;
ecf709
+
ecf709
+    *state = test_ctx;
ecf709
+    return 0;
ecf709
+}
ecf709
+
ecf709
+static int test_sysdb_teardown(void **state)
ecf709
+{
ecf709
+    struct certmap_test_ctx *test_ctx =
ecf709
+        talloc_get_type(*state, struct certmap_test_ctx);
ecf709
+
ecf709
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
ecf709
+    talloc_free(test_ctx->tctx);
ecf709
+    assert_true(check_leaks_pop(test_ctx));
ecf709
+    talloc_free(test_ctx);
ecf709
+    assert_true(leak_check_teardown());
ecf709
+    return 0;
ecf709
+}
ecf709
+
ecf709
+static void test_sdap_certmap_init(void **state)
ecf709
+{
ecf709
+    int ret;
ecf709
+    struct certmap_test_ctx *test_ctx = talloc_get_type(*state,
ecf709
+                                                       struct certmap_test_ctx);
ecf709
+
ecf709
+    ret = sdap_init_certmap(test_ctx, test_ctx->id_ctx);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    talloc_free(test_ctx->id_ctx->opts->sdap_certmap_ctx);
ecf709
+}
ecf709
+
ecf709
+static void test_sdap_get_sss_certmap(void **state)
ecf709
+{
ecf709
+    int ret;
ecf709
+    struct certmap_test_ctx *test_ctx = talloc_get_type(*state,
ecf709
+                                                       struct certmap_test_ctx);
ecf709
+    struct sss_certmap_ctx *sss_certmap_ctx;
ecf709
+
ecf709
+    sss_certmap_ctx = sdap_get_sss_certmap(NULL);
ecf709
+    assert_null(sss_certmap_ctx);
ecf709
+
ecf709
+    ret = sdap_init_certmap(test_ctx, test_ctx->id_ctx);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    sss_certmap_ctx = sdap_get_sss_certmap(
ecf709
+                                      test_ctx->id_ctx->opts->sdap_certmap_ctx);
ecf709
+    assert_non_null(sss_certmap_ctx);
ecf709
+
ecf709
+    talloc_free(test_ctx->id_ctx->opts->sdap_certmap_ctx);
ecf709
+}
ecf709
+
ecf709
+static void test_sdap_certmap_init_twice(void **state)
ecf709
+{
ecf709
+    int ret;
ecf709
+    struct certmap_test_ctx *test_ctx = talloc_get_type(*state,
ecf709
+                                                       struct certmap_test_ctx);
ecf709
+    struct sdap_certmap_ctx *sdap_certmap_ref;
ecf709
+    struct sss_certmap_ctx *sss_certmap_ref;
ecf709
+
ecf709
+    ret = sdap_init_certmap(test_ctx, test_ctx->id_ctx);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    sdap_certmap_ref = test_ctx->id_ctx->opts->sdap_certmap_ctx;
ecf709
+    sss_certmap_ref = sdap_get_sss_certmap(sdap_certmap_ref);
ecf709
+
ecf709
+    ret = sdap_init_certmap(test_ctx, test_ctx->id_ctx);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    assert_ptr_equal(sdap_certmap_ref,
ecf709
+                     test_ctx->id_ctx->opts->sdap_certmap_ctx);
ecf709
+    assert_ptr_not_equal(sss_certmap_ref,
ecf709
+                         sdap_get_sss_certmap(sdap_certmap_ref));
ecf709
+
ecf709
+    talloc_free(test_ctx->id_ctx->opts->sdap_certmap_ctx);
ecf709
+}
ecf709
+
ecf709
+
ecf709
+static void test_sdap_setup_certmap(void **state)
ecf709
+{
ecf709
+    int ret;
ecf709
+    struct certmap_test_ctx *test_ctx = talloc_get_type(*state,
ecf709
+                                                       struct certmap_test_ctx);
ecf709
+    struct sdap_certmap_ctx *sdap_certmap_ref;
ecf709
+    struct sss_certmap_ctx *sss_certmap_ref;
ecf709
+
ecf709
+    ret = sdap_init_certmap(test_ctx, test_ctx->id_ctx);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    sdap_certmap_ref = test_ctx->id_ctx->opts->sdap_certmap_ctx;
ecf709
+    sss_certmap_ref = sdap_get_sss_certmap(sdap_certmap_ref);
ecf709
+
ecf709
+    ret = sdap_setup_certmap(NULL, NULL);
ecf709
+    assert_int_equal(ret, EINVAL);
ecf709
+    assert_ptr_equal(sdap_certmap_ref,
ecf709
+                     test_ctx->id_ctx->opts->sdap_certmap_ctx);
ecf709
+    assert_ptr_equal(sss_certmap_ref, sdap_get_sss_certmap(sdap_certmap_ref));
ecf709
+
ecf709
+    ret = sdap_setup_certmap(NULL, certmap);
ecf709
+    assert_int_equal(ret, EINVAL);
ecf709
+    assert_ptr_equal(sdap_certmap_ref,
ecf709
+                     test_ctx->id_ctx->opts->sdap_certmap_ctx);
ecf709
+    assert_ptr_equal(sss_certmap_ref, sdap_get_sss_certmap(sdap_certmap_ref));
ecf709
+
ecf709
+    ret = sdap_setup_certmap(sdap_certmap_ref, certmap);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+    assert_ptr_equal(sdap_certmap_ref,
ecf709
+                     test_ctx->id_ctx->opts->sdap_certmap_ctx);
ecf709
+    assert_ptr_not_equal(sss_certmap_ref,
ecf709
+                         sdap_get_sss_certmap(sdap_certmap_ref));
ecf709
+
ecf709
+    talloc_free(test_ctx->id_ctx->opts->sdap_certmap_ctx);
ecf709
+}
ecf709
+
ecf709
+int main(int argc, const char *argv[])
ecf709
+{
ecf709
+    int rv;
ecf709
+    poptContext pc;
ecf709
+    int opt;
ecf709
+    struct poptOption long_options[] = {
ecf709
+        POPT_AUTOHELP
ecf709
+        SSSD_DEBUG_OPTS
ecf709
+        POPT_TABLEEND
ecf709
+    };
ecf709
+
ecf709
+    const struct CMUnitTest tests[] = {
ecf709
+        cmocka_unit_test_setup_teardown(test_sdap_certmap_init,
ecf709
+                                        test_sysdb_setup,
ecf709
+                                        test_sysdb_teardown),
ecf709
+        cmocka_unit_test_setup_teardown(test_sdap_get_sss_certmap,
ecf709
+                                        test_sysdb_setup,
ecf709
+                                        test_sysdb_teardown),
ecf709
+        cmocka_unit_test_setup_teardown(test_sdap_certmap_init_twice,
ecf709
+                                        test_sysdb_setup,
ecf709
+                                        test_sysdb_teardown),
ecf709
+        cmocka_unit_test_setup_teardown(test_sdap_setup_certmap,
ecf709
+                                        test_sysdb_setup,
ecf709
+                                        test_sysdb_teardown),
ecf709
+    };
ecf709
+
ecf709
+    /* Set debug level to invalid value so we can deside if -d 0 was used. */
ecf709
+    debug_level = SSSDBG_INVALID;
ecf709
+
ecf709
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
ecf709
+    while((opt = poptGetNextOpt(pc)) != -1) {
ecf709
+        switch(opt) {
ecf709
+        default:
ecf709
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
ecf709
+                    poptBadOption(pc, 0), poptStrerror(opt));
ecf709
+            poptPrintUsage(pc, stderr, 0);
ecf709
+            return 1;
ecf709
+        }
ecf709
+    }
ecf709
+    poptFreeContext(pc);
ecf709
+
ecf709
+    DEBUG_CLI_INIT(debug_level);
ecf709
+
ecf709
+    tests_set_cwd();
ecf709
+    rv = cmocka_run_group_tests(tests, NULL, NULL);
ecf709
+
ecf709
+    return rv;
ecf709
+}
ecf709
-- 
ecf709
2.13.5
ecf709