>From 4ebeea258ec01cf08bebfa4a5a110320c3c0898d Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Tue, 5 Aug 2014 10:12:34 +0200 Subject: [PATCH 1/6] Only replace space with the specified substitution https://fedorahosted.org/sssd/ticket/2397 - make sss_replace_whitespaces only replace space (' ') not any whitespace - make sss_replace_whitespaces only replace a single char, not the whole string - rename CONFDB_NSS_OVERRIDE_DEFAULT_WHITESPACE to CONFDB_NSS_OVERRIDE_DEFAULT_SPACE - rename the override_default_whitespace option to override_space - rename sss_replace_whitespaces() to sss_replace_space() - rename sss_reverse_replace_whitespaces() to sss_reverse_replace_space() - rename nctx->override_default_wsp_str to nctx->override_space - make the return value of sss_replace_space non-const to avoid freeing the result without compilation warnings --- src/confdb/confdb.h | 2 +- src/config/SSSDConfig/__init__.py.in | 2 +- src/config/etc/sssd.api.conf | 2 +- src/man/sssd.conf.5.xml | 10 +-- src/responder/nss/nsssrv.c | 4 +- src/responder/nss/nsssrv.h | 2 +- src/responder/nss/nsssrv_cmd.c | 37 +++++----- src/tests/cmocka/test_string_utils.c | 51 +++++-------- src/util/string_utils.c | 135 +++++++---------------------------- src/util/util.h | 12 ++-- 10 files changed, 74 insertions(+), 183 deletions(-) diff --git a/src/confdb/confdb.h b/src/confdb/confdb.h index b7395c1f31bfeb412d99b722e3708214c34d6c49..d5f7511d0cfe916242b5bfae506271e028a87edb 100644 --- a/src/confdb/confdb.h +++ b/src/confdb/confdb.h @@ -99,7 +99,7 @@ #define CONFDB_MEMCACHE_TIMEOUT "memcache_timeout" #define CONFDB_NSS_HOMEDIR_SUBSTRING "homedir_substring" #define CONFDB_DEFAULT_HOMEDIR_SUBSTRING "/home" -#define CONFDB_NSS_OVERRIDE_DEFAULT_WHITESPACE "override_default_whitespace" +#define CONFDB_NSS_OVERRIDE_SPACE "override_space" /* PAM */ #define CONFDB_PAM_CONF_ENTRY "config/pam" diff --git a/src/config/SSSDConfig/__init__.py.in b/src/config/SSSDConfig/__init__.py.in index c3482b3a4df6d0fd39b2fa93c19ee5316dde0d94..38111a86f02c12e2a6469f2ab671098ebfd84dcb 100644 --- a/src/config/SSSDConfig/__init__.py.in +++ b/src/config/SSSDConfig/__init__.py.in @@ -73,7 +73,7 @@ option_strings = { 'shell_fallback' : _('If a shell stored in central directory is allowed but not available, use this fallback'), 'default_shell': _('Shell to use if the provider does not list one'), 'memcache_timeout': _('How long will be in-memory cache records valid'), - 'override_default_whitespace': _('All white spaces in group or user names will be replaced with this string'), + 'override_space': _('All spaces in group or user names will be replaced with this character'), # [pam] 'offline_credentials_expiration' : _('How long to allow cached logins between online logins (days)'), diff --git a/src/config/etc/sssd.api.conf b/src/config/etc/sssd.api.conf index 1db9e220700e16b0feaa106838565eefbb2a65c6..a3398c8d5567df7d99795acc29e55563459e5733 100644 --- a/src/config/etc/sssd.api.conf +++ b/src/config/etc/sssd.api.conf @@ -44,7 +44,7 @@ shell_fallback = str, None, false default_shell = str, None, false get_domains_timeout = int, None, false memcache_timeout = int, None, false -override_default_whitespace = str, None, false +override_space = str, None, false [pam] # Authentication service diff --git a/src/man/sssd.conf.5.xml b/src/man/sssd.conf.5.xml index 2eb0b62221b966d7f16c56000fc1ebd2ec2b4f84..d2472ec78fc7e4f639fe9a002f8e81ad4fb40a58 100644 --- a/src/man/sssd.conf.5.xml +++ b/src/man/sssd.conf.5.xml @@ -646,19 +646,19 @@ fallback_homedir = /home/%u - override_default_whitespace (string) + override_space (string) - This parameter will replace white spaces (space bar) - with the given string for user and group names. + This parameter will replace spaces (space bar) + with the given character for user and group names. e.g. (_). User name "john doe" will be "john_doe" This feature was added to help compatibility with shell scripts that have - difficulty handling white spaces, due to the + difficulty handling spaces, due to the default field separator in the shell. - Default: not set (whitespaces will not be replaced) + Default: not set (spaces will not be replaced) diff --git a/src/responder/nss/nsssrv.c b/src/responder/nss/nsssrv.c index cf4525a7101834f26215ac269743f614ee8cc17f..9705878fa80d33d3bddfe7be7fcc97151b44a0f0 100644 --- a/src/responder/nss/nsssrv.c +++ b/src/responder/nss/nsssrv.c @@ -299,8 +299,8 @@ static int nss_get_config(struct nss_ctx *nctx, if (ret != EOK) goto done; ret = confdb_get_string(cdb, nctx, CONFDB_NSS_CONF_ENTRY, - CONFDB_NSS_OVERRIDE_DEFAULT_WHITESPACE, NULL, - &nctx->override_default_wsp_str); + CONFDB_NSS_OVERRIDE_SPACE, NULL, + &nctx->override_space); if (ret != EOK) goto done; ret = 0; diff --git a/src/responder/nss/nsssrv.h b/src/responder/nss/nsssrv.h index 07443027eaad4c3103aa6aaed197888e9fb22de2..f5238fb822ecc376d8dca06c00fb989276949688 100644 --- a/src/responder/nss/nsssrv.h +++ b/src/responder/nss/nsssrv.h @@ -69,7 +69,7 @@ struct nss_ctx { char **etc_shells; char *shell_fallback; char *default_shell; - char *override_default_wsp_str; + char *override_space; struct sss_mc_ctx *pwd_mc_ctx; struct sss_mc_ctx *grp_mc_ctx; diff --git a/src/responder/nss/nsssrv_cmd.c b/src/responder/nss/nsssrv_cmd.c index 3e1b470e5ef9ed8db3bd96092cdad2461b3513a0..8e9a7dbaf22af995646738f18ccad6168c10b29e 100644 --- a/src/responder/nss/nsssrv_cmd.c +++ b/src/responder/nss/nsssrv_cmd.c @@ -372,11 +372,10 @@ static int fill_pwent(struct sss_packet *packet, continue; } - tmpstr = sss_replace_whitespaces(tmp_ctx, tmpstr, - nctx->override_default_wsp_str); + tmpstr = sss_replace_space(tmp_ctx, tmpstr, nctx->override_space); if (tmpstr == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_replace_whitespaces failed, skipping\n"); + "sss_replace_space failed, skipping\n"); continue; } @@ -752,11 +751,11 @@ static int nss_cmd_getpwnam_search(struct nss_dom_ctx *dctx) name = sss_get_cased_name(cmdctx, cmdctx->name, dom->case_sensitive); if (!name) return ENOMEM; - name = sss_reverse_replace_whitespaces(dctx, name, - nctx->override_default_wsp_str); + name = sss_reverse_replace_space(dctx, name, + nctx->override_space); if (name == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_reverse_replace_whitespaces failed\n"); + "sss_reverse_replace_space failed\n"); return ENOMEM; } @@ -2361,11 +2360,10 @@ static int fill_members(struct sss_packet *packet, continue; } - tmpstr = sss_replace_whitespaces(tmp_ctx, tmpstr, - nctx->override_default_wsp_str); + tmpstr = sss_replace_space(tmp_ctx, tmpstr, nctx->override_space); if (tmpstr == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_replace_whitespaces failed\n"); + "sss_replace_space failed\n"); ret = ENOMEM; goto done; } @@ -2525,11 +2523,10 @@ static int fill_grent(struct sss_packet *packet, continue; } - tmpstr = sss_replace_whitespaces(tmp_ctx, tmpstr, - nctx->override_default_wsp_str); + tmpstr = sss_replace_space(tmp_ctx, tmpstr, nctx->override_space); if (tmpstr == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_replace_whitespaces failed, skipping\n"); + "sss_replace_space failed, skipping\n"); continue; } @@ -2727,11 +2724,10 @@ static int nss_cmd_getgrnam_search(struct nss_dom_ctx *dctx) name = sss_get_cased_name(dctx, cmdctx->name, dom->case_sensitive); if (!name) return ENOMEM; - name = sss_reverse_replace_whitespaces(dctx, name, - nctx->override_default_wsp_str); + name = sss_reverse_replace_space(dctx, name, nctx->override_space); if (name == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_reverse_replace_whitespaces failed\n"); + "sss_reverse_replace_space failed\n"); return ENOMEM; } @@ -3758,11 +3754,10 @@ static int nss_cmd_initgroups_search(struct nss_dom_ctx *dctx) name = sss_get_cased_name(dctx, cmdctx->name, dom->case_sensitive); if (!name) return ENOMEM; - name = sss_reverse_replace_whitespaces(dctx, name, - nctx->override_default_wsp_str); + name = sss_reverse_replace_space(dctx, name, nctx->override_space); if (name == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_reverse_replace_whitespaces failed\n"); + "sss_reverse_replace_space failed\n"); return ENOMEM; } @@ -3925,11 +3920,11 @@ static errno_t nss_cmd_getsidby_search(struct nss_dom_ctx *dctx) goto done; } - name = sss_reverse_replace_whitespaces(dctx, name, - nctx->override_default_wsp_str); + name = sss_reverse_replace_space(dctx, name, + nctx->override_space); if (name == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_reverse_replace_whitespaces failed\n"); + "sss_reverse_replace_space failed\n"); ret = ENOMEM; goto done; } diff --git a/src/tests/cmocka/test_string_utils.c b/src/tests/cmocka/test_string_utils.c index d10c3be47b83b97817e01af1e16cff987c49b2c5..dda9fac80be32b2dbbee8b365ca2f82c475a3b30 100644 --- a/src/tests/cmocka/test_string_utils.c +++ b/src/tests/cmocka/test_string_utils.c @@ -35,36 +35,18 @@ void test_replace_whitespaces(void **state) } data_set[] = { { "", "", "-" }, { " ", "-", "-" }, - { "\t", "-", "-" }, - { "\n", "-", "-" }, - { " \t\n ", "----", "-" }, { "abcd", "abcd", "-" }, { "a b c d", "a-b-c-d", "-" }, { " a b c d ", "-a-b-c-d-", "-" }, { " ", "^", "^" }, - { "\t", "^", "^" }, - { "\n", "^", "^" }, - { " \t\n ", "^^^^", "^" }, { "abcd", "abcd", "^" }, { "a b c d", "a^b^c^d", "^" }, { " a b c d ", "^a^b^c^d^", "^" }, { " ", "^", "^" }, - { "\t", ";-)", ";-)" }, - { "\n", ";-)", ";-)" }, - { " \t\n ", ";-);-);-);-)", ";-)" }, - { "abcd", "abcd", ";-)" }, - { "a b c d", "a;-)b;-)c;-)d", ";-)" }, - { " a b c d ", ";-)a;-)b;-)c;-)d;-)", ";-)" }, { " ", " ", " " }, { " ", " ", " " }, { "abcd", "abcd", " " }, { "a b c d", "a b c d", " " }, - { " a b c d ", " a b c d ", " " }, - { " ", " \t", " \t" }, - { " ", " \t \t \t \t", " \t" }, - { "abcd", "abcd", " \t" }, - { "a b c d", "a \tb \tc \td", " \t" }, - { " a b c d ", " \ta \tb \tc \td \t", " \t" }, { NULL, NULL, NULL }, }; @@ -72,15 +54,17 @@ void test_replace_whitespaces(void **state) assert_non_null(mem_ctx); check_leaks_push(mem_ctx); - res = sss_replace_whitespaces(mem_ctx, input_str, NULL); - assert_true(res == input_str); + res = sss_replace_space(mem_ctx, input_str, NULL); + assert_string_equal(res, input_str); + talloc_zfree(res); - res = sss_replace_whitespaces(mem_ctx, input_str, ""); - assert_true(res == input_str); + res = sss_replace_space(mem_ctx, input_str, ""); + assert_string_equal(res, input_str); + talloc_zfree(res); for (i=0; data_set[i].input != NULL; ++i) { - res = sss_replace_whitespaces(mem_ctx, data_set[i].input, - data_set[i].replace_string); + res = sss_replace_space(mem_ctx, data_set[i].input, + data_set[i].replace_string); assert_non_null(res); assert_string_equal(res, data_set[i].output); talloc_zfree(res); @@ -113,11 +97,6 @@ void test_reverse_replace_whitespaces(void **state) { "abcd", "abcd", "^" }, { "a^b^c^d", "a b c d", "^" }, { "^a^b^c^d^", " a b c d ", "^" }, - { ";-)", " ", ";-)" }, - { ";-);-);-);-)", " ", ";-)" }, - { "abcd", "abcd", ";-)" }, - { "a;-)b;-)c;-)d", "a b c d", ";-)" }, - { ";-)a;-)b;-)c;-)d;-)", " a b c d ", ";-)" }, { " ", " ", " " }, { " ", " ", " " }, { "abcd", "abcd", " " }, @@ -130,16 +109,18 @@ void test_reverse_replace_whitespaces(void **state) assert_non_null(mem_ctx); check_leaks_push(mem_ctx); - res = sss_reverse_replace_whitespaces(mem_ctx, input_str, NULL); - assert_true(res == input_str); + res = sss_reverse_replace_space(mem_ctx, input_str, NULL); + assert_string_equal(res, input_str); + talloc_free(res); - res = sss_reverse_replace_whitespaces(mem_ctx, input_str, ""); - assert_true(res == input_str); + res = sss_reverse_replace_space(mem_ctx, input_str, ""); + assert_string_equal(res, input_str); + talloc_free(res); for (i=0; data_set[i].input != NULL; ++i) { input_str = discard_const_p(char, data_set[i].input); - res = sss_reverse_replace_whitespaces(mem_ctx, input_str, - data_set[i].replace_string); + res = sss_reverse_replace_space(mem_ctx, input_str, + data_set[i].replace_string); assert_non_null(res); assert_string_equal(res, data_set[i].output); talloc_zfree(res); diff --git a/src/util/string_utils.c b/src/util/string_utils.c index 8d878923f92060e7db57214add3280ecdea27601..d639c6de97275e53e019b60c8578f33ca22ec37f 100644 --- a/src/util/string_utils.c +++ b/src/util/string_utils.c @@ -22,129 +22,44 @@ #include "util/util.h" -const char * sss_replace_whitespaces(TALLOC_CTX *mem_ctx, - const char *orig_name, - const char *replace_string) +static char *replace_char(TALLOC_CTX *mem_ctx, + const char *in, + const char match, + const char sub) { - char *new_name; - const char *ptr; - size_t replace_string_len; - TALLOC_CTX *tmp_ctx; + char *p; + char *out; - if (replace_string == NULL || replace_string[0] == '\0') { - return orig_name; - } - - replace_string_len = strlen(replace_string); - /* faster implementations without multiple allocations */ - if (replace_string_len == 1) { - char *p; - new_name = talloc_strdup(mem_ctx, orig_name); - if (new_name == NULL) { - return NULL; - } - - for (p = new_name; *p != '\0'; ++p) { - if (isspace(*p)) { - *p = replace_string[0]; - } - } - - return new_name; - } - - tmp_ctx = talloc_new(NULL); - if (tmp_ctx == NULL) { + out = talloc_strdup(mem_ctx, in); + if (out == NULL) { return NULL; } - new_name = talloc_strdup(tmp_ctx, ""); - if (new_name == NULL) { - goto done; - } - - ptr = orig_name; - while (*ptr != '\0') { - if (isspace(*ptr)) { - new_name = talloc_asprintf_append(new_name, "%s", replace_string); - } else { - new_name = talloc_asprintf_append(new_name, "%c", *ptr); - } - if (new_name == NULL) { - goto done;; + for (p = out; *p != '\0'; ++p) { + if (*p == match) { + *p = sub; } - - ++ptr; } - new_name = talloc_steal(mem_ctx, new_name); -done: - talloc_free(tmp_ctx); - return new_name; + return out; } -char * sss_reverse_replace_whitespaces(TALLOC_CTX *mem_ctx, - char *orig_name, - const char *replace_string) +char * sss_replace_space(TALLOC_CTX *mem_ctx, + const char *orig_name, + const char *replace_string) { - TALLOC_CTX *tmp_ctx; - char *substr; - char *new_name; - const char *ptr = orig_name; - size_t replace_string_len; - if (replace_string == NULL || replace_string[0] == '\0') { - return orig_name; - } - - replace_string_len = strlen(replace_string); - /* faster implementations without multiple allocations */ - if (replace_string_len == 1) { - char *p; - new_name = talloc_strdup(mem_ctx, orig_name); - if (new_name == NULL) { - return NULL; - } - - for (p = new_name; *p != '\0'; ++p) { - if (*p == replace_string[0] ) { - *p = ' '; - } - } - - return new_name; - } - - tmp_ctx = talloc_new(NULL); - if (tmp_ctx == NULL) { - return NULL; + return talloc_strdup(mem_ctx, orig_name); } + return replace_char(mem_ctx, orig_name, ' ', replace_string[0]); +} - new_name = talloc_strdup(tmp_ctx, ""); - if (new_name == NULL) { - goto done; +char * sss_reverse_replace_space(TALLOC_CTX *mem_ctx, + char *orig_name, + const char *replace_string) +{ + if (replace_string == NULL || replace_string[0] == '\0') { + return talloc_strdup(mem_ctx, orig_name); } - - do { - substr = strstr(ptr, replace_string); - if (substr != NULL) { - new_name = talloc_asprintf_append(new_name, "%.*s ", - (int)(substr - ptr), ptr); - if (new_name == NULL) { - goto done; - } - ptr += substr - ptr; - ptr += replace_string_len; - } else { - new_name = talloc_asprintf_append(new_name, "%s", ptr); - if (new_name == NULL) { - goto done; - } - } - } while (substr != NULL); - - new_name = talloc_steal(mem_ctx, new_name); -done: - talloc_free(tmp_ctx); - return new_name; + return replace_char(mem_ctx, orig_name, replace_string[0], ' '); } diff --git a/src/util/util.h b/src/util/util.h index b638df99aee00d8b133555ace1ee5bd290dfecf4..c58e3fde62928b0f6b43698b8e20c058fd856df1 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -566,11 +566,11 @@ errno_t name_to_well_known_sid(const char *dom, const char *name, const char **sid); /* from string_utils.c */ -const char * sss_replace_whitespaces(TALLOC_CTX *mem_ctx, - const char *orig_name, - const char *replace_string); -char * sss_reverse_replace_whitespaces(TALLOC_CTX *mem_ctx, - char *orig_name, - const char *replace_string); +char * sss_replace_space(TALLOC_CTX *mem_ctx, + const char *orig_name, + const char *replace_string); +char * sss_reverse_replace_space(TALLOC_CTX *mem_ctx, + char *orig_name, + const char *replace_string); #endif /* __SSSD_UTIL_H__ */ -- 1.9.3