>From 289a56d27b9beff2e5272287a8abfed08ef45ede Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Wed, 29 May 2013 00:56:18 +0200 Subject: [PATCH 12/15] New utility function sss_get_domain_name Instead of copying a block of code that checks whether domain is a subdomain and uses only name of FQDN as appropriate, wrap the logic into a function. --- src/db/sysdb_search.c | 20 ++++---------------- src/tools/sss_cache.c | 13 ++++--------- src/util/usertools.c | 18 ++++++++++++++++++ src/util/util.h | 5 +++++ 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/db/sysdb_search.c b/src/db/sysdb_search.c index 843c746d2a724aa63d885ff40d025633ba401833..d70d0cc3eee6fe5fa55bee6a36d4e8382d69c3ab 100644 --- a/src/db/sysdb_search.c +++ b/src/db/sysdb_search.c @@ -55,15 +55,9 @@ int sysdb_getpwnam(TALLOC_CTX *mem_ctx, /* If this is a subomain we need to use fully qualified names for the * search as well by default */ - if (IS_SUBDOMAIN(domain) && domain->fqnames) { - ret = ENOMEM; - src_name = talloc_asprintf(tmp_ctx, domain->names->fq_fmt, - name, domain->name); - } else { - ret = EINVAL; - src_name = name; - } + src_name = sss_get_domain_name(tmp_ctx, name, domain); if (!src_name) { + ret = ENOMEM; goto done; } @@ -243,15 +237,9 @@ int sysdb_getgrnam(TALLOC_CTX *mem_ctx, /* If this is a subomain we need to use fully qualified names for the * search as well by default */ - if (IS_SUBDOMAIN(domain) && domain->fqnames) { - ret = ENOMEM; - src_name = talloc_asprintf(tmp_ctx, domain->names->fq_fmt, - name, domain->name); - } else { - ret = EINVAL; - src_name = name; - } + src_name = sss_get_domain_name(tmp_ctx, name, domain); if (!src_name) { + ret = ENOMEM; goto done; } diff --git a/src/tools/sss_cache.c b/src/tools/sss_cache.c index 9942e6a7f4a252f55bf2a01ec7c80b202db72f51..c9096fa91e562dc5c07bf14a4afacc692348f3b5 100644 --- a/src/tools/sss_cache.c +++ b/src/tools/sss_cache.c @@ -219,15 +219,10 @@ static errno_t update_filter(struct cache_tool_ctx *tctx, } if (parsed_domain) { - if (IS_SUBDOMAIN(dinfo)) { - /* Use fqdn for subdomains */ - use_name = talloc_asprintf(tmp_ctx, tctx->nctx->fq_fmt, use_name, - dinfo->name); - if (use_name == NULL) { - DEBUG(SSSDBG_CRIT_FAILURE, ("Out of memory\n")); - ret = ENOMEM; - goto done; - } + use_name = sss_get_domain_name(tmp_ctx, use_name, dinfo); + if (!use_name) { + ret = ENOMEM; + goto done; } if (!strcasecmp(dinfo->name, parsed_domain)) { diff --git a/src/util/usertools.c b/src/util/usertools.c index 91110f263657de9ba53ed305e7c4710eb006bec6..4dc1947a7597f3903e304414990cc618939c4ca7 100644 --- a/src/util/usertools.c +++ b/src/util/usertools.c @@ -507,3 +507,21 @@ sss_get_cased_name_list(TALLOC_CTX *mem_ctx, const char * const *orig, *_cased = out; return EOK; } + +char * +sss_get_domain_name(TALLOC_CTX *mem_ctx, + const char *orig_name, + struct sss_domain_info *dom) +{ + char *user_name; + + if (IS_SUBDOMAIN(dom) && dom->fqnames) { + /* we always use the fully qualified name for subdomain users */ + user_name = talloc_asprintf(mem_ctx, dom->names->fq_fmt, + orig_name, dom->name); + } else { + user_name = talloc_strdup(mem_ctx, orig_name); + } + + return user_name; +} diff --git a/src/util/util.h b/src/util/util.h index 8a15afba7a93e6914cdb713b4f759e70e94d7825..0314c5048bb96dc42bdcc8f9da6a641b8c7e0a11 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -43,6 +43,7 @@ #include #include +#include "confdb/confdb.h" #include "util/atomic_io.h" #include "util/util_errors.h" #include "util/util_safealign.h" @@ -365,6 +366,10 @@ errno_t sss_get_cased_name_list(TALLOC_CTX *mem_ctx, const char * const *orig, bool case_sensitive, const char ***_cased); +char * +sss_get_domain_name(TALLOC_CTX *mem_ctx, const char *orig_name, + struct sss_domain_info *dom); + /* from backup-file.c */ int backup_file(const char *src, int dbglvl); -- 1.8.2.1