From 32a3d248aee2f5355e57440e382916b4aa389eb8 Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Fri, 26 Sep 2014 20:09:25 +0200 Subject: [PATCH 06/12] sysdb: add sss_view_ldb_msg_find_element/attr_as_string/uint64 Override-aware replacements for the corresponding ldb_msg_find_* calls. First it is check if an override value is available before the original value is returned. --- src/db/sysdb.h | 15 +++++++ src/db/sysdb_views.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) diff --git a/src/db/sysdb.h b/src/db/sysdb.h index 2db1a1c..8d2894a 100644 --- a/src/db/sysdb.h +++ b/src/db/sysdb.h @@ -484,6 +484,21 @@ errno_t sysdb_getpwuid_with_views(TALLOC_CTX *mem_ctx, uid_t uid, struct ldb_result **res); +struct ldb_message_element * +sss_view_ldb_msg_find_element(struct sss_domain_info *dom, + const struct ldb_message *msg, + const char *attr_name); + +const char *sss_view_ldb_msg_find_attr_as_string(struct sss_domain_info *dom, + const struct ldb_message *msg, + const char *attr_name, + const char * default_value); + +uint64_t sss_view_ldb_msg_find_attr_as_uint64(struct sss_domain_info *dom, + const struct ldb_message *msg, + const char *attr_name, + uint64_t default_value); + /* Sysdb initialization. * call this function *only* once to initialize the database and get * the sysdb ctx */ diff --git a/src/db/sysdb_views.c b/src/db/sysdb_views.c index f182391..d778f9d 100644 --- a/src/db/sysdb_views.c +++ b/src/db/sysdb_views.c @@ -1057,3 +1057,119 @@ done: return ret; } +struct ldb_message_element * +sss_view_ldb_msg_find_element(struct sss_domain_info *dom, + const struct ldb_message *msg, + const char *attr_name) +{ + TALLOC_CTX *tmp_ctx = NULL; + struct ldb_message_element *val; + char *override_attr_name; + + if (DOM_HAS_VIEWS(dom)) { + tmp_ctx = talloc_new(NULL); + if (tmp_ctx == NULL) { + DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n"); + val = NULL; + goto done; + } + + override_attr_name = talloc_asprintf(tmp_ctx, "%s%s", OVERRIDE_PREFIX, + attr_name); + if (override_attr_name == NULL) { + DEBUG(SSSDBG_OP_FAILURE, "talloc_asprintf failed.\n"); + val = NULL; + goto done; + } + + val = ldb_msg_find_element(msg, override_attr_name); + if (val != NULL) { + goto done; + } + } + + val = ldb_msg_find_element(msg, attr_name); + +done: + talloc_free(tmp_ctx); + return val; +} + +uint64_t sss_view_ldb_msg_find_attr_as_uint64(struct sss_domain_info *dom, + const struct ldb_message *msg, + const char *attr_name, + uint64_t default_value) +{ + TALLOC_CTX *tmp_ctx = NULL; + uint64_t val; + char *override_attr_name; + + if (DOM_HAS_VIEWS(dom)) { + tmp_ctx = talloc_new(NULL); + if (tmp_ctx == NULL) { + DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n"); + val = default_value; + goto done; + } + + override_attr_name = talloc_asprintf(tmp_ctx, "%s%s", OVERRIDE_PREFIX, + attr_name); + if (override_attr_name == NULL) { + DEBUG(SSSDBG_OP_FAILURE, "talloc_asprintf failed.\n"); + val = default_value; + goto done; + } + + if (ldb_msg_find_element(msg, override_attr_name) != NULL) { + val = ldb_msg_find_attr_as_uint64(msg, override_attr_name, + default_value); + goto done; + } + } + + val = ldb_msg_find_attr_as_uint64(msg, attr_name, default_value); + +done: + talloc_free(tmp_ctx); + return val; +} + +const char *sss_view_ldb_msg_find_attr_as_string(struct sss_domain_info *dom, + const struct ldb_message *msg, + const char *attr_name, + const char * default_value) +{ + TALLOC_CTX *tmp_ctx = NULL; + const char *val; + char *override_attr_name; + + if (DOM_HAS_VIEWS(dom)) { + tmp_ctx = talloc_new(NULL); + if (tmp_ctx == NULL) { + DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n"); + val = default_value; + goto done; + } + + override_attr_name = talloc_asprintf(tmp_ctx, "%s%s", OVERRIDE_PREFIX, + attr_name); + if (override_attr_name == NULL) { + DEBUG(SSSDBG_OP_FAILURE, "talloc_asprintf failed.\n"); + val = default_value; + goto done; + } + + if (ldb_msg_find_element(msg, override_attr_name) != NULL) { + val = ldb_msg_find_attr_as_string(msg, override_attr_name, + default_value); + goto done; + } + } + + val = ldb_msg_find_attr_as_string(msg, attr_name, default_value); + +done: + talloc_free(tmp_ctx); + return val; +} + -- 1.8.3.1