Blame SOURCES/0005-sysdb-add-attr_map-attribute-to-sysdb_ldb_msg_attr_t.patch

71e593
From 11878ac29dd0abaad1daad2772e32f1db6f84e3d Mon Sep 17 00:00:00 2001
71e593
From: Sumit Bose <sbose@redhat.com>
71e593
Date: Fri, 29 Jun 2018 18:13:59 +0200
71e593
Subject: [PATCH 05/19] sysdb: add attr_map attribute to
71e593
 sysdb_ldb_msg_attr_to_certmap_info()
71e593
71e593
Allow more flexible attribute mapping in
71e593
sysdb_ldb_msg_attr_to_certmap_info()
71e593
71e593
Related to https://pagure.io/SSSD/sssd/issue/3500
71e593
71e593
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
71e593
(cherry picked from commit 0bf709ad348ca115443bd21e4e369abd5d7698c4)
71e593
---
71e593
 src/db/sysdb.h         |  1 +
71e593
 src/db/sysdb_certmap.c | 39 +++++++++++++++++++++++++++++++--------
71e593
 2 files changed, 32 insertions(+), 8 deletions(-)
71e593
71e593
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
71e593
index cb04e1b60546bd5de968eaf67ea5d2fc2b5e24ba..2187947dcd74df0511b33ac5823df38a05713e4a 100644
71e593
--- a/src/db/sysdb.h
71e593
+++ b/src/db/sysdb.h
71e593
@@ -704,6 +704,7 @@ errno_t sysdb_update_certmap(struct sysdb_ctx *sysdb,
71e593
 
71e593
 errno_t sysdb_ldb_msg_attr_to_certmap_info(TALLOC_CTX *mem_ctx,
71e593
                                            struct ldb_message *msg,
71e593
+                                           const char **attr_map,
71e593
                                            struct certmap_info **certmap);
71e593
 
71e593
 errno_t sysdb_get_certmap(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
71e593
diff --git a/src/db/sysdb_certmap.c b/src/db/sysdb_certmap.c
71e593
index 0bb7ebcade649631ef50e4d62f4ba85fb32c7aa4..e37f1ba830f297137991c7757af9c7c4e17b2813 100644
71e593
--- a/src/db/sysdb_certmap.c
71e593
+++ b/src/db/sysdb_certmap.c
71e593
@@ -263,8 +263,19 @@ done:
71e593
     return ret;
71e593
 }
71e593
 
71e593
+enum certmap_info_member {
71e593
+    SSS_CMIM_NAME = 0,
71e593
+    SSS_CMIM_MAPPING_RULE,
71e593
+    SSS_CMIM_MATCHING_RULE,
71e593
+    SSS_CMIM_PRIORITY,
71e593
+    SSS_CMIM_DOMAINS,
71e593
+
71e593
+    SSS_CMIM_SENTINEL
71e593
+};
71e593
+
71e593
 errno_t sysdb_ldb_msg_attr_to_certmap_info(TALLOC_CTX *mem_ctx,
71e593
                                            struct ldb_message *msg,
71e593
+                                           const char **attr_map,
71e593
                                            struct certmap_info **certmap)
71e593
 {
71e593
     int ret;
71e593
@@ -275,13 +286,24 @@ errno_t sysdb_ldb_msg_attr_to_certmap_info(TALLOC_CTX *mem_ctx,
71e593
     uint64_t tmp_uint;
71e593
     struct ldb_message_element *tmp_el;
71e593
 
71e593
+    if (msg == NULL || attr_map == NULL || certmap == NULL) {
71e593
+        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid input.\n");
71e593
+        return EINVAL;
71e593
+    }
71e593
+
71e593
+    for (d = 0; d < SSS_CMIM_SENTINEL; d++) {
71e593
+        if (attr_map[d] == NULL) {
71e593
+            DEBUG(SSSDBG_CRIT_FAILURE, "Invalid attribute map");
71e593
+            return EINVAL;
71e593
+        }
71e593
+    }
71e593
 
71e593
     map = talloc_zero(mem_ctx, struct certmap_info);
71e593
     if (map == NULL) {
71e593
         return ENOMEM;
71e593
     }
71e593
 
71e593
-    tmp_str = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
71e593
+    tmp_str = ldb_msg_find_attr_as_string(msg, attr_map[SSS_CMIM_NAME], NULL);
71e593
     if (tmp_str == NULL) {
71e593
         DEBUG(SSSDBG_MINOR_FAILURE, "The object [%s] doesn't have a name.\n",
71e593
                                     ldb_dn_get_linearized(msg->dn));
71e593
@@ -295,7 +317,7 @@ errno_t sysdb_ldb_msg_attr_to_certmap_info(TALLOC_CTX *mem_ctx,
71e593
         goto done;
71e593
     }
71e593
 
71e593
-    tmp_str = ldb_msg_find_attr_as_string(msg, SYSDB_CERTMAP_MAPPING_RULE,
71e593
+    tmp_str = ldb_msg_find_attr_as_string(msg, attr_map[SSS_CMIM_MAPPING_RULE],
71e593
                                           NULL);
71e593
     if (tmp_str != NULL) {
71e593
         map->map_rule = talloc_strdup(map, tmp_str);
71e593
@@ -306,7 +328,7 @@ errno_t sysdb_ldb_msg_attr_to_certmap_info(TALLOC_CTX *mem_ctx,
71e593
         }
71e593
     }
71e593
 
71e593
-    tmp_str = ldb_msg_find_attr_as_string(msg, SYSDB_CERTMAP_MATCHING_RULE,
71e593
+    tmp_str = ldb_msg_find_attr_as_string(msg, attr_map[SSS_CMIM_MATCHING_RULE],
71e593
                                           NULL);
71e593
     if (tmp_str != NULL) {
71e593
         map->match_rule = talloc_strdup(map, tmp_str);
71e593
@@ -317,7 +339,7 @@ errno_t sysdb_ldb_msg_attr_to_certmap_info(TALLOC_CTX *mem_ctx,
71e593
         }
71e593
     }
71e593
 
71e593
-    tmp_uint = ldb_msg_find_attr_as_uint64(msg, SYSDB_CERTMAP_PRIORITY,
71e593
+    tmp_uint = ldb_msg_find_attr_as_uint64(msg, attr_map[SSS_CMIM_PRIORITY],
71e593
                                            (uint64_t) -1);
71e593
     if (tmp_uint != (uint64_t) -1) {
71e593
         if (tmp_uint > UINT32_MAX) {
71e593
@@ -332,7 +354,7 @@ errno_t sysdb_ldb_msg_attr_to_certmap_info(TALLOC_CTX *mem_ctx,
71e593
         map->priority = SSS_CERTMAP_MIN_PRIO;
71e593
     }
71e593
 
71e593
-    tmp_el = ldb_msg_find_element(msg, SYSDB_CERTMAP_DOMAINS);
71e593
+    tmp_el = ldb_msg_find_element(msg, attr_map[SSS_CMIM_DOMAINS]);
71e593
     if (tmp_el != NULL) {
71e593
         num_values = tmp_el->num_values;
71e593
     } else {
71e593
@@ -379,9 +401,9 @@ errno_t sysdb_get_certmap(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
71e593
     TALLOC_CTX *tmp_ctx = NULL;
71e593
     struct ldb_result *res;
71e593
     const char *attrs[] = {SYSDB_NAME,
71e593
-                           SYSDB_CERTMAP_PRIORITY,
71e593
-                           SYSDB_CERTMAP_MATCHING_RULE,
71e593
                            SYSDB_CERTMAP_MAPPING_RULE,
71e593
+                           SYSDB_CERTMAP_MATCHING_RULE,
71e593
+                           SYSDB_CERTMAP_PRIORITY,
71e593
                            SYSDB_CERTMAP_DOMAINS,
71e593
                            NULL};
71e593
     const char *config_attrs[] = {SYSDB_CERTMAP_USER_NAME_HINT,
71e593
@@ -434,7 +456,8 @@ errno_t sysdb_get_certmap(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
71e593
     }
71e593
 
71e593
     for (c = 0; c < res->count; c++) {
71e593
-        ret = sysdb_ldb_msg_attr_to_certmap_info(maps, res->msgs[c], &maps[c]);
71e593
+        ret = sysdb_ldb_msg_attr_to_certmap_info(maps, res->msgs[c], attrs,
71e593
+                                                 &maps[c]);
71e593
         if (ret != EOK) {
71e593
             DEBUG(SSSDBG_OP_FAILURE,
71e593
                   "sysdb_ldb_msg_attr_to_certmap_info failed.\n");
71e593
-- 
71e593
2.14.4
71e593