From accff8ebe158251b1d25a95b3b035fe7e08fd1ee Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Sun, 19 Jun 2016 07:19:15 +0200 Subject: UTIL: expand_homedir_template manages usernames internally expand_homedir_template() can be considered an outward-facing interface, therefore the function and its input structure will accept the internal name format and parse it internally into a username and domain component. Reviewed-by: Sumit Bose --- src/providers/ipa/ipa_s2n_exop.c | 1 + src/providers/ipa/ipa_subdomains_id.c | 15 ++------------- src/responder/nss/nsssrv_cmd.c | 11 ++++++----- src/tests/cmocka/test_utils.c | 14 ++++++++++---- src/util/sss_nss.c | 25 ++++++++++++++++++++----- src/util/sss_nss.h | 2 ++ 6 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/providers/ipa/ipa_s2n_exop.c b/src/providers/ipa/ipa_s2n_exop.c index 84f1c5ad0..828dbf3dc 100644 --- a/src/providers/ipa/ipa_s2n_exop.c +++ b/src/providers/ipa/ipa_s2n_exop.c @@ -1947,6 +1947,7 @@ static errno_t ipa_s2n_save_objects(struct sss_domain_info *dom, attrs->a.user.pw_dir = expand_homedir_template(attrs, dom->subdomain_homedir, + dom->case_preserve, &homedir_ctx); if (attrs->a.user.pw_dir == NULL) { ret = ENOMEM; diff --git a/src/providers/ipa/ipa_subdomains_id.c b/src/providers/ipa/ipa_subdomains_id.c index f06eff7a7..7e53dd8dd 100644 --- a/src/providers/ipa/ipa_subdomains_id.c +++ b/src/providers/ipa/ipa_subdomains_id.c @@ -742,7 +742,6 @@ get_subdomain_homedir_of_user(TALLOC_CTX *mem_ctx, struct sss_domain_info *dom, const char *original, const char **_homedir) { errno_t ret; - const char *name; const char *homedir; TALLOC_CTX *tmp_ctx; struct sss_nss_homedir_ctx homedir_ctx; @@ -767,22 +766,11 @@ get_subdomain_homedir_of_user(TALLOC_CTX *mem_ctx, struct sss_domain_info *dom, homedir_ctx.flatname = dom->flat_name; homedir_ctx.config_homedir_substr = dom->homedir_substr; homedir_ctx.original = original; - ret = sss_parse_name_const(tmp_ctx, dom->names, fqname, - NULL, &name); - if (ret != EOK) { - goto done; - } /* To be compatible with the old winbind based user lookups and IPA * clients the user name in the home directory path will be lower-case. */ - homedir_ctx.username = sss_tc_utf8_str_tolower(tmp_ctx, name); - if (homedir_ctx.username == NULL) { - ret = ENOMEM; - goto done; - } - homedir = expand_homedir_template(tmp_ctx, dom->subdomain_homedir, - &homedir_ctx); + false, &homedir_ctx); if (homedir == NULL) { DEBUG(SSSDBG_OP_FAILURE, "expand_homedir_template failed\n"); ret = ENOMEM; @@ -795,6 +783,7 @@ get_subdomain_homedir_of_user(TALLOC_CTX *mem_ctx, struct sss_domain_info *dom, } *_homedir = talloc_steal(mem_ctx, homedir); + ret = EOK; done: talloc_free(tmp_ctx); return ret; diff --git a/src/responder/nss/nsssrv_cmd.c b/src/responder/nss/nsssrv_cmd.c index 9ba81a6aa..a54d3d8ba 100644 --- a/src/responder/nss/nsssrv_cmd.c +++ b/src/responder/nss/nsssrv_cmd.c @@ -221,10 +221,10 @@ static const char *get_homedir_override(TALLOC_CTX *mem_ctx, */ if (dom->override_homedir) { return expand_homedir_template(mem_ctx, dom->override_homedir, - homedir_ctx); + dom->case_preserve, homedir_ctx); } else if (nctx->override_homedir) { return expand_homedir_template(mem_ctx, nctx->override_homedir, - homedir_ctx); + dom->case_preserve, homedir_ctx); } if (!homedir || *homedir == '\0') { @@ -233,15 +233,16 @@ static const char *get_homedir_override(TALLOC_CTX *mem_ctx, */ if (dom->fallback_homedir) { return expand_homedir_template(mem_ctx, dom->fallback_homedir, - homedir_ctx); + dom->case_preserve, homedir_ctx); } else if (nctx->fallback_homedir) { return expand_homedir_template(mem_ctx, nctx->fallback_homedir, - homedir_ctx); + dom->case_preserve, homedir_ctx); } } /* Provider can also return template, try to expand it.*/ - return expand_homedir_template(mem_ctx, homedir, homedir_ctx); + return expand_homedir_template(mem_ctx, homedir, + dom->case_preserve, homedir_ctx); } static const char *get_shell_override(TALLOC_CTX *mem_ctx, diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c index e171848f1..ef3e894aa 100644 --- a/src/tests/cmocka/test_utils.c +++ b/src/tests/cmocka/test_utils.c @@ -963,7 +963,7 @@ void check_expanded_value(TALLOC_CTX *tmp_ctx, { char *homedir; - homedir = expand_homedir_template(tmp_ctx, template, homedir_ctx); + homedir = expand_homedir_template(tmp_ctx, template, false, homedir_ctx); if (exp_val != NULL) { assert_string_equal(homedir, exp_val); } else { @@ -983,7 +983,13 @@ static int setup_homedir_ctx(void **state) struct sss_nss_homedir_ctx); assert_non_null(homedir_ctx); - homedir_ctx->username = USERNAME; + homedir_ctx->username = sss_create_internal_fqname(homedir_ctx, + USERNAME, DOMAIN); + if (homedir_ctx->username == NULL) { + talloc_free(homedir_ctx); + return 1; + } + homedir_ctx->uid = UID; homedir_ctx->original = ORIGINAL_HOME; homedir_ctx->domain = DOMAIN; @@ -1027,10 +1033,10 @@ void test_expand_homedir_template_NULL(void **state) homedir_ctx = talloc_zero(tmp_ctx, struct sss_nss_homedir_ctx); assert_non_null(homedir_ctx); - homedir = expand_homedir_template(tmp_ctx, NULL, NULL); + homedir = expand_homedir_template(tmp_ctx, NULL, false, NULL); assert_null(homedir); - homedir = expand_homedir_template(tmp_ctx, "template", NULL); + homedir = expand_homedir_template(tmp_ctx, "template", false, NULL); assert_null(homedir); /* missing data in homedir_ctx */ diff --git a/src/util/sss_nss.c b/src/util/sss_nss.c index 208b57842..d1ad5433f 100644 --- a/src/util/sss_nss.c +++ b/src/util/sss_nss.c @@ -22,7 +22,9 @@ #include "util/util.h" #include "util/sss_nss.h" -char *expand_homedir_template(TALLOC_CTX *mem_ctx, const char *template, +char *expand_homedir_template(TALLOC_CTX *mem_ctx, + const char *template, + bool case_sensitive, struct sss_nss_homedir_ctx *homedir_ctx) { char *copy; @@ -32,6 +34,7 @@ char *expand_homedir_template(TALLOC_CTX *mem_ctx, const char *template, char *res = NULL; TALLOC_CTX *tmp_ctx = NULL; const char *orig = NULL; + char *username = NULL; if (template == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "Missing template.\n"); @@ -75,8 +78,14 @@ char *expand_homedir_template(TALLOC_CTX *mem_ctx, const char *template, "is empty.\n"); goto done; } - result = talloc_asprintf_append(result, "%s%s", p, - homedir_ctx->username); + username = sss_output_name(tmp_ctx, homedir_ctx->username, + case_sensitive, 0); + if (username == NULL) { + goto done; + } + + result = talloc_asprintf_append(result, "%s%s", p, username); + talloc_free(username); break; case 'U': @@ -108,9 +117,15 @@ char *expand_homedir_template(TALLOC_CTX *mem_ctx, const char *template, "or user name is empty.\n"); goto done; } + username = sss_output_name(tmp_ctx, homedir_ctx->username, + case_sensitive, 0); + if (username == NULL) { + goto done; + } + result = talloc_asprintf_append(result, "%s%s@%s", p, - homedir_ctx->username, - homedir_ctx->domain); + username, homedir_ctx->domain); + talloc_free(username); break; case 'o': diff --git a/src/util/sss_nss.h b/src/util/sss_nss.h index 30359fc7c..2b8a5ae26 100644 --- a/src/util/sss_nss.h +++ b/src/util/sss_nss.h @@ -22,6 +22,7 @@ #ifndef __SSS_NSS_H__ #define __SSS_NSS_H__ +#include #include #include @@ -36,5 +37,6 @@ struct sss_nss_homedir_ctx { }; char *expand_homedir_template(TALLOC_CTX *mem_ctx, const char *template, + bool case_sensitive, struct sss_nss_homedir_ctx *homedir_ctx); #endif -- cgit