From d7d3ee1b8ab7a05129c83da8a185351d7c751c1c Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Sun, 21 Sep 2014 16:55:21 +0200 Subject: SYSDB: move sysdb_get_real_name() from sysdb.c to sysdb_search.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The sysdb.c should be reserved for utility and setup functions. Search functions belong to sysdb_search.c Keeping functions in specialized modules helps to maintain nice dependencies and in overall makes unit testing easier. Moreover, the function was not unit tested, which needed fixing. Reviewed-by: Lukáš Slebodník --- src/db/sysdb.c | 52 ------------------------------------------------- src/db/sysdb.h | 2 +- src/db/sysdb_search.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 53 deletions(-) (limited to 'src/db') diff --git a/src/db/sysdb.c b/src/db/sysdb.c index 3bc27c84a..88cff241f 100644 --- a/src/db/sysdb.c +++ b/src/db/sysdb.c @@ -1882,58 +1882,6 @@ done: return ret; } -errno_t sysdb_get_real_name(TALLOC_CTX *mem_ctx, - struct sss_domain_info *domain, - const char *name, - const char **_cname) -{ - errno_t ret; - TALLOC_CTX *tmp_ctx; - struct ldb_result *res; - const char *cname; - struct ldb_message *msg; - - tmp_ctx = talloc_new(NULL); - if (!tmp_ctx) { - return ENOMEM; - } - - ret = sysdb_getpwnam(tmp_ctx, domain, name, &res); - if (ret != EOK) { - DEBUG(SSSDBG_OP_FAILURE, "Cannot canonicalize username\n"); - goto done; - } - - if (res->count == 0) { - ret = sysdb_search_user_by_upn(tmp_ctx, domain, name, NULL, &msg); - if (ret != EOK) { - /* User cannot be found in cache */ - DEBUG(SSSDBG_OP_FAILURE, "Cannot find user [%s] in cache\n", name); - goto done; - } - } else if (res->count == 1) { - msg = res->msgs[0]; - } else { - DEBUG(SSSDBG_CRIT_FAILURE, - "sysdb_getpwnam returned count: [%d]\n", res->count); - ret = EIO; - goto done; - } - - cname = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL); - if (!cname) { - DEBUG(SSSDBG_CRIT_FAILURE, "A user with no name?\n"); - ret = ENOENT; - goto done; - } - - ret = EOK; - *_cname = talloc_steal(mem_ctx, cname); -done: - talloc_free(tmp_ctx); - return ret; -} - errno_t sysdb_msg2attrs(TALLOC_CTX *mem_ctx, size_t count, struct ldb_message **msgs, struct sysdb_attrs ***attrs) diff --git a/src/db/sysdb.h b/src/db/sysdb.h index 81b39252c..2c5e8316f 100644 --- a/src/db/sysdb.h +++ b/src/db/sysdb.h @@ -329,7 +329,7 @@ errno_t sysdb_attrs_primary_name_list(struct sysdb_ctx *sysdb, char ***name_list); errno_t sysdb_get_real_name(TALLOC_CTX *mem_ctx, struct sss_domain_info *domain, - const char *name, + const char *name_or_upn, const char **_cname); errno_t sysdb_msg2attrs(TALLOC_CTX *mem_ctx, size_t count, diff --git a/src/db/sysdb_search.c b/src/db/sysdb_search.c index 3d789ae85..2ae215055 100644 --- a/src/db/sysdb_search.c +++ b/src/db/sysdb_search.c @@ -985,3 +985,57 @@ done: talloc_free(tmp_ctx); return ret; } + +errno_t sysdb_get_real_name(TALLOC_CTX *mem_ctx, + struct sss_domain_info *domain, + const char *name_or_upn, + const char **_cname) +{ + errno_t ret; + TALLOC_CTX *tmp_ctx; + struct ldb_result *res; + const char *cname; + struct ldb_message *msg; + + tmp_ctx = talloc_new(NULL); + if (!tmp_ctx) { + return ENOMEM; + } + + ret = sysdb_getpwnam(tmp_ctx, domain, name_or_upn, &res); + if (ret != EOK) { + DEBUG(SSSDBG_OP_FAILURE, "Cannot canonicalize username\n"); + goto done; + } + + if (res->count == 0) { + ret = sysdb_search_user_by_upn(tmp_ctx, domain, name_or_upn, NULL, + &msg); + if (ret != EOK) { + /* User cannot be found in cache */ + DEBUG(SSSDBG_OP_FAILURE, "Cannot find user [%s] in cache\n", + name_or_upn); + goto done; + } + } else if (res->count == 1) { + msg = res->msgs[0]; + } else { + DEBUG(SSSDBG_CRIT_FAILURE, + "sysdb_getpwnam returned count: [%d]\n", res->count); + ret = EIO; + goto done; + } + + cname = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL); + if (!cname) { + DEBUG(SSSDBG_CRIT_FAILURE, "A user with no name?\n"); + ret = ENOENT; + goto done; + } + + ret = EOK; + *_cname = talloc_steal(mem_ctx, cname); +done: + talloc_free(tmp_ctx); + return ret; +} -- cgit