dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0010-sysdb-add-certmap-related-calls.patch

ecf709
From cfb6a115568ab24fe5df365d1436419b504111ec Mon Sep 17 00:00:00 2001
ecf709
From: Sumit Bose <sbose@redhat.com>
ecf709
Date: Mon, 6 Feb 2017 10:27:22 +0100
ecf709
Subject: [PATCH 10/15] sysdb: add certmap related calls
ecf709
MIME-Version: 1.0
ecf709
Content-Type: text/plain; charset=UTF-8
ecf709
Content-Transfer-Encoding: 8bit
ecf709
ecf709
Add sysdb calls to write and read data for the certificate mapping
ecf709
library to the cache.
ecf709
ecf709
Related to https://pagure.io/SSSD/sssd/issue/3050
ecf709
ecf709
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
ecf709
Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
ecf709
---
ecf709
 Makefile.am                           |  17 ++
ecf709
 src/db/sysdb.h                        |  27 +++
ecf709
 src/db/sysdb_certmap.c                | 425 ++++++++++++++++++++++++++++++++++
ecf709
 src/tests/cmocka/test_sysdb_certmap.c | 260 +++++++++++++++++++++
ecf709
 4 files changed, 729 insertions(+)
ecf709
 create mode 100644 src/db/sysdb_certmap.c
ecf709
 create mode 100644 src/tests/cmocka/test_sysdb_certmap.c
ecf709
ecf709
diff --git a/Makefile.am b/Makefile.am
ecf709
index f262cc24832358910dbb92ccd46f93c9eda8a295..bd0ca0d303e1742ad26c7648cd24e2c0135af34e 100644
ecf709
--- a/Makefile.am
ecf709
+++ b/Makefile.am
ecf709
@@ -254,6 +254,7 @@ if HAVE_CMOCKA
ecf709
         test_sysdb_ts_cache \
ecf709
         test_sysdb_views \
ecf709
         test_sysdb_subdomains \
ecf709
+        test_sysdb_certmap \
ecf709
         test_sysdb_sudo \
ecf709
         test_sysdb_utils \
ecf709
         test_wbc_calls \
ecf709
@@ -974,6 +975,7 @@ libsss_util_la_SOURCES = \
ecf709
     src/db/sysdb_ranges.c \
ecf709
     src/db/sysdb_idmap.c \
ecf709
     src/db/sysdb_gpo.c \
ecf709
+    src/db/sysdb_certmap.c \
ecf709
     src/monitor/monitor_sbus.c \
ecf709
     src/providers/dp_auth_util.c \
ecf709
     src/providers/dp_pam_data_util.c \
ecf709
@@ -2773,6 +2775,21 @@ test_sysdb_subdomains_LDADD = \
ecf709
     libsss_test_common.la \
ecf709
     $(NULL)
ecf709
 
ecf709
+test_sysdb_certmap_SOURCES = \
ecf709
+    src/tests/cmocka/test_sysdb_certmap.c \
ecf709
+    $(NULL)
ecf709
+test_sysdb_certmap_CFLAGS = \
ecf709
+    $(AM_CFLAGS) \
ecf709
+    $(NULL)
ecf709
+test_sysdb_certmap_LDADD = \
ecf709
+    $(CMOCKA_LIBS) \
ecf709
+    $(LDB_LIBS) \
ecf709
+    $(POPT_LIBS) \
ecf709
+    $(TALLOC_LIBS) \
ecf709
+    $(SSSD_INTERNAL_LTLIBS) \
ecf709
+    libsss_test_common.la \
ecf709
+    $(NULL)
ecf709
+
ecf709
 test_sysdb_sudo_SOURCES = \
ecf709
     src/tests/cmocka/test_sysdb_sudo.c \
ecf709
     $(NULL)
ecf709
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
ecf709
index 3db22b3689bf6ffd9a48e29c229916e3fac9ca1b..0cbb2c5c02355e9e9a4e73b075f92d16e4855045 100644
ecf709
--- a/src/db/sysdb.h
ecf709
+++ b/src/db/sysdb.h
ecf709
@@ -39,6 +39,7 @@
ecf709
 #define SYSDB_NETGROUP_CONTAINER "cn=Netgroups"
ecf709
 #define SYSDB_RANGE_CONTAINER "cn=ranges"
ecf709
 #define SYSDB_VIEW_CONTAINER "cn=views"
ecf709
+#define SYSDB_CERTMAP_CONTAINER "cn=certmap"
ecf709
 #define SYSDB_TMPL_USER_BASE SYSDB_USERS_CONTAINER","SYSDB_DOM_BASE
ecf709
 #define SYSDB_TMPL_GROUP_BASE SYSDB_GROUPS_CONTAINER","SYSDB_DOM_BASE
ecf709
 #define SYSDB_TMPL_CUSTOM_BASE SYSDB_CUSTOM_CONTAINER","SYSDB_DOM_BASE
ecf709
@@ -46,6 +47,7 @@
ecf709
 #define SYSDB_TMPL_RANGE_BASE SYSDB_RANGE_CONTAINER","SYSDB_BASE
ecf709
 #define SYSDB_TMPL_VIEW_BASE SYSDB_VIEW_CONTAINER","SYSDB_BASE
ecf709
 #define SYSDB_TMPL_VIEW_SEARCH_BASE "cn=%s,"SYSDB_TMPL_VIEW_BASE
ecf709
+#define SYSDB_TMPL_CERTMAP_BASE SYSDB_CERTMAP_CONTAINER","SYSDB_BASE
ecf709
 
ecf709
 #define SYSDB_SUBDOMAIN_CLASS "subdomain"
ecf709
 #define SYSDB_USER_CLASS "user"
ecf709
@@ -58,6 +60,7 @@
ecf709
 #define SYSDB_ID_RANGE_CLASS "idRange"
ecf709
 #define SYSDB_DOMAIN_ID_RANGE_CLASS "domainIDRange"
ecf709
 #define SYSDB_TRUSTED_AD_DOMAIN_RANGE_CLASS "TrustedADDomainRange"
ecf709
+#define SYSDB_CERTMAP_CLASS "certificateMappingRule"
ecf709
 
ecf709
 #define SYSDB_DN "dn"
ecf709
 #define SYSDB_NAME "name"
ecf709
@@ -158,6 +161,12 @@
ecf709
 #define SYSDB_DOMAIN_ID "domainID"
ecf709
 #define SYSDB_ID_RANGE_TYPE "idRangeType"
ecf709
 
ecf709
+#define SYSDB_CERTMAP_PRIORITY "priority"
ecf709
+#define SYSDB_CERTMAP_MATCHING_RULE "matchingRule"
ecf709
+#define SYSDB_CERTMAP_MAPPING_RULE "mappingRule"
ecf709
+#define SYSDB_CERTMAP_DOMAINS "domains"
ecf709
+#define SYSDB_CERTMAP_USER_NAME_HINT "userNameHint"
ecf709
+
ecf709
 #define ORIGINALAD_PREFIX "originalAD"
ecf709
 #define OVERRIDE_PREFIX "override"
ecf709
 #define SYSDB_DEFAULT_OVERRIDE_NAME "defaultOverrideName"
ecf709
@@ -264,6 +273,7 @@
ecf709
 #define SYSDB_TMPL_CUSTOM SYSDB_NAME"=%s,cn=%s,"SYSDB_TMPL_CUSTOM_BASE
ecf709
 #define SYSDB_TMPL_RANGE SYSDB_NAME"=%s,"SYSDB_TMPL_RANGE_BASE
ecf709
 #define SYSDB_TMPL_OVERRIDE SYSDB_OVERRIDE_ANCHOR_UUID"=%s,"SYSDB_TMPL_VIEW_SEARCH_BASE
ecf709
+#define SYSDB_TMPL_CERTMAP SYSDB_NAME"=%s,"SYSDB_TMPL_CERTMAP_BASE
ecf709
 
ecf709
 #define SYSDB_MOD_ADD LDB_FLAG_MOD_ADD
ecf709
 #define SYSDB_MOD_DEL LDB_FLAG_MOD_DELETE
ecf709
@@ -320,6 +330,15 @@ struct range_info {
ecf709
     char *range_type;
ecf709
 };
ecf709
 
ecf709
+struct certmap_info {
ecf709
+    char *name;
ecf709
+    uint32_t priority;
ecf709
+    char *match_rule;
ecf709
+    char *map_rule;
ecf709
+    const char **domains;
ecf709
+};
ecf709
+
ecf709
+
ecf709
 /* These attributes are stored in the timestamp cache */
ecf709
 extern const char *sysdb_ts_cache_attrs[];
ecf709
 
ecf709
@@ -619,6 +638,14 @@ uint64_t sss_view_ldb_msg_find_attr_as_uint64(struct sss_domain_info *dom,
ecf709
                                               const char *attr_name,
ecf709
                                               uint64_t default_value);
ecf709
 
ecf709
+errno_t sysdb_update_certmap(struct sysdb_ctx *sysdb,
ecf709
+                             struct certmap_info **certmaps,
ecf709
+                             bool user_name_hint);
ecf709
+
ecf709
+errno_t sysdb_get_certmap(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
ecf709
+                          struct certmap_info ***certmaps,
ecf709
+                          bool *user_name_hint);
ecf709
+
ecf709
 /* Sysdb initialization.
ecf709
  * call this function *only* once to initialize the database and get
ecf709
  * the sysdb ctx */
ecf709
diff --git a/src/db/sysdb_certmap.c b/src/db/sysdb_certmap.c
ecf709
new file mode 100644
ecf709
index 0000000000000000000000000000000000000000..4917796b11c3967b4d147ebee7c7e83f09b872ce
ecf709
--- /dev/null
ecf709
+++ b/src/db/sysdb_certmap.c
ecf709
@@ -0,0 +1,425 @@
ecf709
+/*
ecf709
+   SSSD
ecf709
+
ecf709
+   System Database - certificate mapping rules related calls
ecf709
+
ecf709
+   Copyright (C) 2017 Sumit Bose <sbose@redhat.com>
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
+
ecf709
+#include "util/util.h"
ecf709
+#include "db/sysdb_private.h"
ecf709
+
ecf709
+static errno_t sysdb_create_certmap_container(struct sysdb_ctx *sysdb,
ecf709
+                                              bool user_name_hint)
ecf709
+{
ecf709
+    struct ldb_message *msg = NULL;
ecf709
+    errno_t ret;
ecf709
+
ecf709
+    msg = ldb_msg_new(sysdb);
ecf709
+    if (msg == NULL) {
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+    msg->dn = ldb_dn_new(msg, sysdb->ldb, SYSDB_TMPL_CERTMAP_BASE);
ecf709
+    if (msg->dn == NULL) {
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+    ret = ldb_msg_add_string(msg, "cn", "certmap");
ecf709
+    if (ret != LDB_SUCCESS) {
ecf709
+        ret = sysdb_error_to_errno(ret);
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    ret = ldb_msg_add_string(msg, SYSDB_CERTMAP_USER_NAME_HINT,
ecf709
+                             user_name_hint ? "TRUE" : "FALSE");
ecf709
+    if (ret != LDB_SUCCESS) {
ecf709
+        ret = sysdb_error_to_errno(ret);
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    /* do a synchronous add */
ecf709
+    ret = ldb_add(sysdb->ldb, msg);
ecf709
+    if (ret != LDB_SUCCESS) {
ecf709
+        DEBUG(SSSDBG_FATAL_FAILURE,
ecf709
+              "Failed to add certmap container (%d, [%s])!\n",
ecf709
+               ret, ldb_errstring(sysdb->ldb));
ecf709
+        ret = EIO;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    ret = EOK;
ecf709
+
ecf709
+done:
ecf709
+    talloc_free(msg);
ecf709
+
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
+static errno_t sysdb_certmap_add(struct sysdb_ctx *sysdb,
ecf709
+                                 struct certmap_info *certmap)
ecf709
+{
ecf709
+    struct ldb_message *msg;
ecf709
+    struct ldb_message_element *el;
ecf709
+    int ret;
ecf709
+    TALLOC_CTX *tmp_ctx;
ecf709
+    size_t c;
ecf709
+
ecf709
+    tmp_ctx = talloc_new(NULL);
ecf709
+    if (tmp_ctx == NULL) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed");
ecf709
+        return ENOMEM;
ecf709
+    }
ecf709
+
ecf709
+    msg = ldb_msg_new(tmp_ctx);
ecf709
+    if (msg == NULL) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "ldb_msg_new failed.\n");
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    msg->dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
ecf709
+                             SYSDB_TMPL_CERTMAP, certmap->name);
ecf709
+    if (msg->dn == NULL) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "ldb_dn_new_fmt failed.\n");
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_CERTMAP_CLASS);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "sysdb_add_string failed.\n");
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    ret = sysdb_add_string(msg, SYSDB_NAME, certmap->name);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "sysdb_add_string failed.\n");
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    if (certmap->map_rule != NULL) {
ecf709
+        ret = sysdb_add_string(msg, SYSDB_CERTMAP_MAPPING_RULE,
ecf709
+                               certmap->map_rule);
ecf709
+        if (ret != EOK) {
ecf709
+            DEBUG(SSSDBG_OP_FAILURE, "sysdb_add_string failed.\n");
ecf709
+            goto done;
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
+    if (certmap->match_rule != NULL) {
ecf709
+        ret = sysdb_add_string(msg, SYSDB_CERTMAP_MATCHING_RULE,
ecf709
+                               certmap->match_rule);
ecf709
+        if (ret != EOK) {
ecf709
+            DEBUG(SSSDBG_OP_FAILURE, "sysdb_add_string failed.\n");
ecf709
+            goto done;
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
+    if (certmap->domains != NULL) {
ecf709
+        for (c = 0; certmap->domains[c] != NULL; c++);
ecf709
+        el = talloc_zero(tmp_ctx, struct ldb_message_element);
ecf709
+        if (el == NULL) {
ecf709
+            DEBUG(SSSDBG_OP_FAILURE, "talloc_zero failed.\n");
ecf709
+            ret = ENOMEM;
ecf709
+            goto done;
ecf709
+        }
ecf709
+
ecf709
+        el->name = talloc_strdup(el, SYSDB_CERTMAP_DOMAINS);
ecf709
+        if(el->name == NULL) {
ecf709
+            DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n");
ecf709
+            ret = ENOMEM;
ecf709
+            goto done;
ecf709
+        }
ecf709
+        el->num_values = c;
ecf709
+        el->values = talloc_zero_array(el, struct ldb_val, c + 1);
ecf709
+        if (el->values == NULL) {
ecf709
+            DEBUG(SSSDBG_OP_FAILURE, "talloc_zero_array failed.\n");
ecf709
+            ret = ENOMEM;
ecf709
+            goto done;
ecf709
+        }
ecf709
+
ecf709
+        for (c = 0; certmap->domains[c] != NULL; c++) {
ecf709
+            el->values[c].data = (uint8_t *) talloc_strdup(el->values,
ecf709
+                                                           certmap->domains[c]);
ecf709
+            if (el->values[c].data == NULL) {
ecf709
+                DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n");
ecf709
+                ret = ENOMEM;
ecf709
+                goto done;
ecf709
+            }
ecf709
+            el->values[c].length = strlen(certmap->domains[c]);
ecf709
+        }
ecf709
+
ecf709
+        ret = ldb_msg_add(msg, el, LDB_FLAG_MOD_ADD);
ecf709
+        if (ret != LDB_SUCCESS) {
ecf709
+            DEBUG(SSSDBG_OP_FAILURE, "ldb_msg_add failed.\n");
ecf709
+            ret = sysdb_error_to_errno(ret);
ecf709
+            goto done;
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
+    ret = sysdb_add_ulong(msg, SYSDB_CERTMAP_PRIORITY,
ecf709
+                          (unsigned long)certmap->priority);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "sysdb_add_ulong failed.\n");
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    ret = ldb_add(sysdb->ldb, msg);
ecf709
+    if (ret != LDB_SUCCESS) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "ldb_add failed.\n");
ecf709
+        ret = sysdb_error_to_errno(ret);
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    ret = EOK;
ecf709
+
ecf709
+done:
ecf709
+    if (ret) {
ecf709
+        DEBUG(SSSDBG_TRACE_FUNC, "Error: %d (%s)\n", ret, sss_strerror(ret));
ecf709
+    }
ecf709
+    talloc_zfree(tmp_ctx);
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
+errno_t sysdb_update_certmap(struct sysdb_ctx *sysdb,
ecf709
+                             struct certmap_info **certmaps,
ecf709
+                             bool user_name_hint)
ecf709
+{
ecf709
+    size_t c;
ecf709
+    struct ldb_dn *container_dn = NULL;
ecf709
+    bool in_transaction = false;
ecf709
+    int ret;
ecf709
+    int sret;
ecf709
+
ecf709
+    if (certmaps == NULL) {
ecf709
+        return EINVAL;
ecf709
+    }
ecf709
+
ecf709
+    container_dn = ldb_dn_new(sysdb, sysdb->ldb, SYSDB_TMPL_CERTMAP_BASE);
ecf709
+    if (container_dn == NULL) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "ldb_dn_new failed.\n");
ecf709
+        return ENOMEM;
ecf709
+    }
ecf709
+
ecf709
+    ret = sysdb_transaction_start(sysdb);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "sysdb_transaction_start failed.\n");
ecf709
+        goto done;
ecf709
+    }
ecf709
+    in_transaction = true;
ecf709
+
ecf709
+    ret = sysdb_delete_recursive(sysdb, container_dn, true);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "sysdb_delete_recursive failed.\n");
ecf709
+        goto done;
ecf709
+    }
ecf709
+    ret = sysdb_create_certmap_container(sysdb, user_name_hint);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "sysdb_create_certmap_container failed.\n");
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    for (c = 0; certmaps[c] != NULL; c++) {
ecf709
+        ret = sysdb_certmap_add(sysdb, certmaps[c]);
ecf709
+        if (ret != EOK) {
ecf709
+            DEBUG(SSSDBG_OP_FAILURE, "sysdb_certmap_add failed.\n");
ecf709
+            goto done;
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
+    ret = sysdb_transaction_commit(sysdb);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_CRIT_FAILURE, "sysdb_transaction_commit failed.\n");
ecf709
+        goto done;
ecf709
+    }
ecf709
+    in_transaction = false;
ecf709
+
ecf709
+done:
ecf709
+    if (in_transaction) {
ecf709
+        sret = sysdb_transaction_cancel(sysdb);
ecf709
+        if (sret != EOK) {
ecf709
+            DEBUG(SSSDBG_CRIT_FAILURE, "Could not cancel transaction.\n");
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
+    talloc_free(container_dn);
ecf709
+
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
+errno_t sysdb_get_certmap(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
ecf709
+                          struct certmap_info ***certmaps, bool *user_name_hint)
ecf709
+{
ecf709
+    size_t c;
ecf709
+    size_t d;
ecf709
+    struct ldb_dn *container_dn = NULL;
ecf709
+    int ret;
ecf709
+    struct certmap_info **maps;
ecf709
+    TALLOC_CTX *tmp_ctx = NULL;
ecf709
+    struct ldb_result *res;
ecf709
+    const char *tmp_str;
ecf709
+    uint64_t tmp_uint;
ecf709
+    struct ldb_message_element *tmp_el;
ecf709
+    const char *attrs[] = {SYSDB_NAME,
ecf709
+                           SYSDB_CERTMAP_PRIORITY,
ecf709
+                           SYSDB_CERTMAP_MATCHING_RULE,
ecf709
+                           SYSDB_CERTMAP_MAPPING_RULE,
ecf709
+                           SYSDB_CERTMAP_DOMAINS,
ecf709
+                           NULL};
ecf709
+    const char *config_attrs[] = {SYSDB_CERTMAP_USER_NAME_HINT,
ecf709
+                                  NULL};
ecf709
+    size_t num_values;
ecf709
+    bool hint = false;
ecf709
+
ecf709
+    tmp_ctx = talloc_new(NULL);
ecf709
+    if (tmp_ctx == NULL) {
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    container_dn = ldb_dn_new(tmp_ctx, sysdb->ldb, SYSDB_TMPL_CERTMAP_BASE);
ecf709
+    if (container_dn == NULL) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "ldb_dn_new failed.\n");
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    ret = ldb_search(sysdb->ldb, tmp_ctx, &res, container_dn, LDB_SCOPE_BASE,
ecf709
+                     config_attrs, SYSDB_CERTMAP_USER_NAME_HINT"=*");
ecf709
+    if (ret != LDB_SUCCESS || res->count != 1) {
ecf709
+        DEBUG(SSSDBG_CRIT_FAILURE,
ecf709
+              "Failed to read certmap config, skipping.\n");
ecf709
+    } else {
ecf709
+        hint = ldb_msg_find_attr_as_bool(res->msgs[0],
ecf709
+                                         SYSDB_CERTMAP_USER_NAME_HINT, false);
ecf709
+    }
ecf709
+
ecf709
+    ret = ldb_search(sysdb->ldb, tmp_ctx, &res,
ecf709
+                     container_dn, LDB_SCOPE_SUBTREE,
ecf709
+                     attrs, "objectclass=%s", SYSDB_CERTMAP_CLASS);
ecf709
+    if (ret != LDB_SUCCESS) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "ldb_search failed.\n");
ecf709
+        ret = EIO;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    if (res->count == 0) {
ecf709
+        DEBUG(SSSDBG_TRACE_FUNC, "No certificate maps found.\n");
ecf709
+        ret = ENOENT;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    maps = talloc_zero_array(tmp_ctx, struct certmap_info *, res->count + 1);
ecf709
+    if (maps == NULL) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "talloc_zero_array failed.\n");
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    for (c = 0; c < res->count; c++) {
ecf709
+        maps[c] = talloc_zero(maps, struct certmap_info);
ecf709
+        if (maps[c] == NULL) {
ecf709
+            ret = ENOMEM;
ecf709
+            goto done;
ecf709
+        }
ecf709
+        tmp_str = ldb_msg_find_attr_as_string(res->msgs[c], SYSDB_NAME, NULL);
ecf709
+        if (tmp_str == NULL) {
ecf709
+            DEBUG(SSSDBG_MINOR_FAILURE, "The object [%s] doesn't have a name.\n",
ecf709
+                                       ldb_dn_get_linearized(res->msgs[c]->dn));
ecf709
+            ret = EINVAL;
ecf709
+            goto done;
ecf709
+        }
ecf709
+
ecf709
+        maps[c]->name = talloc_strdup(maps, tmp_str);
ecf709
+        if (maps[c]->name == NULL) {
ecf709
+            ret = ENOMEM;
ecf709
+            goto done;
ecf709
+        }
ecf709
+
ecf709
+        tmp_str = ldb_msg_find_attr_as_string(res->msgs[c],
ecf709
+                                              SYSDB_CERTMAP_MAPPING_RULE, NULL);
ecf709
+        if (tmp_str != NULL) {
ecf709
+            maps[c]->map_rule = talloc_strdup(maps, tmp_str);
ecf709
+            if (maps[c]->map_rule == NULL) {
ecf709
+                DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n");
ecf709
+                ret = ENOMEM;
ecf709
+                goto done;
ecf709
+            }
ecf709
+        }
ecf709
+
ecf709
+        tmp_str = ldb_msg_find_attr_as_string(res->msgs[c],
ecf709
+                                              SYSDB_CERTMAP_MATCHING_RULE, NULL);
ecf709
+        if (tmp_str != NULL) {
ecf709
+            maps[c]->match_rule = talloc_strdup(maps, tmp_str);
ecf709
+            if (maps[c]->match_rule == NULL) {
ecf709
+                DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n");
ecf709
+                ret = ENOMEM;
ecf709
+                goto done;
ecf709
+            }
ecf709
+        }
ecf709
+
ecf709
+        tmp_uint = ldb_msg_find_attr_as_uint64(res->msgs[c],
ecf709
+                                               SYSDB_CERTMAP_PRIORITY,
ecf709
+                                               (uint64_t) -1);
ecf709
+        if (tmp_uint != (uint64_t) -1) {
ecf709
+            if (tmp_uint >= UINT32_MAX) {
ecf709
+                DEBUG(SSSDBG_OP_FAILURE, "Priority value [%lu] too large.\n",
ecf709
+                                         (unsigned long) tmp_uint);
ecf709
+                ret = EINVAL;
ecf709
+                goto done;
ecf709
+            }
ecf709
+
ecf709
+            maps[c]->priority = (uint32_t) tmp_uint;
ecf709
+        }
ecf709
+
ecf709
+        tmp_el = ldb_msg_find_element(res->msgs[c], SYSDB_CERTMAP_DOMAINS);
ecf709
+        if (tmp_el != NULL) {
ecf709
+            num_values = tmp_el->num_values;
ecf709
+        } else {
ecf709
+            num_values = 0;
ecf709
+        }
ecf709
+
ecf709
+        maps[c]->domains = talloc_zero_array(maps[c], const char *,
ecf709
+                                             num_values + 1);
ecf709
+        if (maps[c]->domains == NULL) {
ecf709
+            DEBUG(SSSDBG_OP_FAILURE, "talloc_zero_array failed.\n");
ecf709
+            ret = ENOMEM;
ecf709
+            goto done;
ecf709
+        }
ecf709
+
ecf709
+        for (d = 0; d < num_values; d++) {
ecf709
+            maps[c]->domains[d] = talloc_strndup(maps[c]->domains,
ecf709
+                                            (char *) tmp_el->values[d].data,
ecf709
+                                            tmp_el->values[d].length);
ecf709
+            if (maps[c]->domains[d] == NULL) {
ecf709
+                DEBUG(SSSDBG_OP_FAILURE, "talloc_strndup failed.\n");
ecf709
+                ret = ENOMEM;
ecf709
+                goto done;
ecf709
+            }
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
+    *certmaps = talloc_steal(mem_ctx, maps);
ecf709
+    *user_name_hint = hint;
ecf709
+    ret = EOK;
ecf709
+
ecf709
+done:
ecf709
+    talloc_free(tmp_ctx);
ecf709
+
ecf709
+    return ret;
ecf709
+}
ecf709
diff --git a/src/tests/cmocka/test_sysdb_certmap.c b/src/tests/cmocka/test_sysdb_certmap.c
ecf709
new file mode 100644
ecf709
index 0000000000000000000000000000000000000000..fb07165561779226935f436c308c85abfc305635
ecf709
--- /dev/null
ecf709
+++ b/src/tests/cmocka/test_sysdb_certmap.c
ecf709
@@ -0,0 +1,260 @@
ecf709
+/*
ecf709
+    SSSD
ecf709
+
ecf709
+    sysdb_certmap - Tests for sysdb certmap realted calls
ecf709
+
ecf709
+    Authors:
ecf709
+        Jakub Hrozek <jhrozek@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 <stdarg.h>
ecf709
+#include <stddef.h>
ecf709
+#include <setjmp.h>
ecf709
+#include <cmocka.h>
ecf709
+#include <popt.h>
ecf709
+
ecf709
+#include "tests/cmocka/common_mock.h"
ecf709
+#include "tests/common.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_test_ctx {
ecf709
+    struct sss_test_ctx *tctx;
ecf709
+};
ecf709
+
ecf709
+static int test_sysdb_setup(void **state)
ecf709
+{
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
+    *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_sysdb_get_certmap_not_exists(void **state)
ecf709
+{
ecf709
+    int ret;
ecf709
+    struct certmap_info **certmap;
ecf709
+    bool user_name_hint;
ecf709
+    struct certmap_test_ctx *ctctx = talloc_get_type(*state,
ecf709
+                                                     struct certmap_test_ctx);
ecf709
+
ecf709
+    ret = sysdb_get_certmap(ctctx, ctctx->tctx->sysdb, &certmap,
ecf709
+                            &user_name_hint);
ecf709
+    assert_int_equal(ret, ENOENT);
ecf709
+
ecf709
+}
ecf709
+
ecf709
+static void check_certmap(struct certmap_info *m, struct certmap_info *r,
ecf709
+                          size_t exp_domains)
ecf709
+{
ecf709
+    size_t d;
ecf709
+
ecf709
+    assert_non_null(r);
ecf709
+    assert_non_null(m);
ecf709
+    assert_string_equal(m->name, r->name);
ecf709
+
ecf709
+    if (r->map_rule == NULL) {
ecf709
+        assert_null(m->map_rule);
ecf709
+    } else {
ecf709
+        assert_string_equal(m->map_rule, r->map_rule);
ecf709
+    }
ecf709
+
ecf709
+    if (r->match_rule == NULL) {
ecf709
+        assert_null(m->match_rule);
ecf709
+    } else {
ecf709
+        assert_string_equal(m->match_rule, r->match_rule);
ecf709
+    }
ecf709
+
ecf709
+    assert_int_equal(m->priority, r->priority);
ecf709
+    assert_non_null(m->domains);
ecf709
+    if (r->domains == NULL) {
ecf709
+        assert_null(m->domains[0]);
ecf709
+    } else {
ecf709
+        for (d = 0; r->domains[d]; d++) {
ecf709
+            assert_non_null(m->domains[d]);
ecf709
+            assert_true(string_in_list(m->domains[d], discard_const(r->domains),
ecf709
+                                       true));
ecf709
+        }
ecf709
+
ecf709
+        assert_int_equal(d, exp_domains);
ecf709
+    }
ecf709
+
ecf709
+}
ecf709
+
ecf709
+static void test_sysdb_update_certmap(void **state)
ecf709
+{
ecf709
+    int ret;
ecf709
+    const char *domains[] = { "dom1.test", "dom2.test", "dom3.test", NULL };
ecf709
+    struct certmap_info map_a = { discard_const("map_a"), 11, discard_const("abc"), discard_const("def"), NULL };
ecf709
+    struct certmap_info map_b = { discard_const("map_b"), 22, discard_const("abc"), NULL, domains };
ecf709
+    struct certmap_info *certmap_empty[] = { NULL };
ecf709
+    struct certmap_info *certmap_a[] = { &map_a, NULL };
ecf709
+    struct certmap_info *certmap_b[] = { &map_b, NULL };
ecf709
+    struct certmap_info *certmap_ab[] = { &map_a, &map_b, NULL };
ecf709
+    struct certmap_info **certmap;
ecf709
+    struct certmap_test_ctx *ctctx = talloc_get_type(*state,
ecf709
+                                                     struct certmap_test_ctx);
ecf709
+    bool user_name_hint;
ecf709
+
ecf709
+    ret = sysdb_update_certmap(ctctx->tctx->sysdb, NULL, false);
ecf709
+    assert_int_equal(ret, EINVAL);
ecf709
+
ecf709
+    ret = sysdb_update_certmap(ctctx->tctx->sysdb, certmap_empty, false);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    ret = sysdb_get_certmap(ctctx, ctctx->tctx->sysdb, &certmap,
ecf709
+                            &user_name_hint);
ecf709
+    assert_int_equal(ret, ENOENT);
ecf709
+
ecf709
+    ret = sysdb_update_certmap(ctctx->tctx->sysdb, certmap_a, false);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    ret = sysdb_get_certmap(ctctx, ctctx->tctx->sysdb, &certmap,
ecf709
+                            &user_name_hint);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+    assert_false(user_name_hint);
ecf709
+    assert_non_null(certmap);
ecf709
+    assert_non_null(certmap[0]);
ecf709
+    assert_string_equal(certmap[0]->name, map_a.name);
ecf709
+    assert_string_equal(certmap[0]->map_rule, map_a.map_rule);
ecf709
+    assert_string_equal(certmap[0]->match_rule, map_a.match_rule);
ecf709
+    assert_int_equal(certmap[0]->priority, map_a.priority);
ecf709
+    assert_non_null(certmap[0]->domains);
ecf709
+    assert_null(certmap[0]->domains[0]);
ecf709
+    assert_null(certmap[1]);
ecf709
+    check_certmap(certmap[0], &map_a, 0);
ecf709
+    talloc_free(certmap);
ecf709
+
ecf709
+    ret = sysdb_update_certmap(ctctx->tctx->sysdb, certmap_b, true);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    ret = sysdb_get_certmap(ctctx, ctctx->tctx->sysdb, &certmap,
ecf709
+                            &user_name_hint);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+    assert_true(user_name_hint);
ecf709
+    assert_non_null(certmap);
ecf709
+    assert_non_null(certmap[0]);
ecf709
+
ecf709
+    check_certmap(certmap[0], &map_b, 3);
ecf709
+    assert_null(certmap[1]);
ecf709
+    talloc_free(certmap);
ecf709
+
ecf709
+    ret = sysdb_update_certmap(ctctx->tctx->sysdb, certmap_ab, false);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    ret = sysdb_get_certmap(ctctx, ctctx->tctx->sysdb, &certmap,
ecf709
+                            &user_name_hint);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+    assert_false(user_name_hint);
ecf709
+    assert_non_null(certmap);
ecf709
+    assert_non_null(certmap[0]);
ecf709
+    assert_non_null(certmap[1]);
ecf709
+    assert_null(certmap[2]);
ecf709
+    if (strcmp(certmap[0]->name, "map_a") == 0) {
ecf709
+        check_certmap(certmap[0], &map_a, 0);
ecf709
+        check_certmap(certmap[1], &map_b, 3);
ecf709
+    } else {
ecf709
+        check_certmap(certmap[0], &map_b, 3);
ecf709
+        check_certmap(certmap[1], &map_a, 0);
ecf709
+    }
ecf709
+    talloc_free(certmap);
ecf709
+}
ecf709
+
ecf709
+int main(int argc, const char *argv[])
ecf709
+{
ecf709
+    int rv;
ecf709
+    int no_cleanup = 0;
ecf709
+    poptContext pc;
ecf709
+    int opt;
ecf709
+    struct poptOption long_options[] = {
ecf709
+        POPT_AUTOHELP
ecf709
+        SSSD_DEBUG_OPTS
ecf709
+        {"no-cleanup", 'n', POPT_ARG_NONE, &no_cleanup, 0,
ecf709
+         _("Do not delete the test database after a test run"), NULL },
ecf709
+        POPT_TABLEEND
ecf709
+    };
ecf709
+
ecf709
+    const struct CMUnitTest tests[] = {
ecf709
+        cmocka_unit_test_setup_teardown(test_sysdb_get_certmap_not_exists,
ecf709
+                                        test_sysdb_setup,
ecf709
+                                        test_sysdb_teardown),
ecf709
+        cmocka_unit_test_setup_teardown(test_sysdb_update_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
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, LOCAL_SYSDB_FILE);
ecf709
+    test_dom_suite_setup(TESTS_PATH);
ecf709
+    rv = cmocka_run_group_tests(tests, NULL, NULL);
ecf709
+
ecf709
+    if (rv == 0 && no_cleanup == 0) {
ecf709
+        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, LOCAL_SYSDB_FILE);
ecf709
+    }
ecf709
+    return rv;
ecf709
+}
ecf709
-- 
ecf709
2.9.3
ecf709