From 87bea3731d9d379a9cbc57e664b5e2c00ffb5cb9 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Tue, 5 Aug 2014 10:12:34 +0200 Subject: Only replace space with the specified substitution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Pavel Březina Reviewed-by: Lukáš Slebodník (cherry picked from commit 1f3127e88a87953f059c9a70d3582ae1719594b1) --- 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_nss_srv.c | 1 + src/tests/cmocka/test_string_utils.c | 119 +++++++++++++----------------- src/util/string_utils.c | 137 +++++++---------------------------- src/util/util.h | 12 +-- 11 files changed, 110 insertions(+), 218 deletions(-) diff --git a/src/confdb/confdb.h b/src/confdb/confdb.h index 28ca7b3d8..2bee9c82c 100644 --- a/src/confdb/confdb.h +++ b/src/confdb/confdb.h @@ -98,7 +98,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 bbd18356c..48db1eb8f 100644 --- a/src/config/SSSDConfig/__init__.py.in +++ b/src/config/SSSDConfig/__init__.py.in @@ -72,7 +72,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 13130b567..0514d8609 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 47465f568..9f6ec69f3 100644 --- a/src/man/sssd.conf.5.xml +++ b/src/man/sssd.conf.5.xml @@ -608,19 +608,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 313815e5d..5f58bf640 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 e8db17f35..83eed37bf 100644 --- a/src/responder/nss/nsssrv.h +++ b/src/responder/nss/nsssrv.h @@ -67,7 +67,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 79e3aa904..fa6536de1 100644 --- a/src/responder/nss/nsssrv_cmd.c +++ b/src/responder/nss/nsssrv_cmd.c @@ -378,11 +378,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[0]); if (tmpstr == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_replace_whitespaces failed, skipping\n"); + "sss_replace_space failed, skipping\n"); continue; } @@ -766,11 +765,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[0]); if (name == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_reverse_replace_whitespaces failed\n"); + "sss_reverse_replace_space failed\n"); return ENOMEM; } @@ -2315,11 +2314,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[0]); if (tmpstr == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_replace_whitespaces failed\n"); + "sss_replace_space failed\n"); ret = ENOMEM; goto done; } @@ -2499,11 +2497,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[0]); if (tmpstr == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_replace_whitespaces failed, skipping\n"); + "sss_replace_space failed, skipping\n"); continue; } @@ -2710,11 +2707,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[0]); if (name == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_reverse_replace_whitespaces failed\n"); + "sss_reverse_replace_space failed\n"); return ENOMEM; } @@ -3745,11 +3741,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[0]); if (name == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, - "sss_reverse_replace_whitespaces failed\n"); + "sss_reverse_replace_space failed\n"); return ENOMEM; } @@ -3913,11 +3908,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[0]); 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_nss_srv.c b/src/tests/cmocka/test_nss_srv.c index 3ef6da87c..d93b1872a 100644 --- a/src/tests/cmocka/test_nss_srv.c +++ b/src/tests/cmocka/test_nss_srv.c @@ -71,6 +71,7 @@ mock_nctx(TALLOC_CTX *mem_ctx) } nctx->neg_timeout = 10; nctx->pwfield = discard_const("*"); + nctx->override_space = discard_const("\0"); return nctx; } diff --git a/src/tests/cmocka/test_string_utils.c b/src/tests/cmocka/test_string_utils.c index d10c3be47..93d7d716f 100644 --- a/src/tests/cmocka/test_string_utils.c +++ b/src/tests/cmocka/test_string_utils.c @@ -31,56 +31,40 @@ void test_replace_whitespaces(void **state) struct { const char *input; const char *output; - const char *replace_string; + const char replace_char; } 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 }, + { "", "", '-' }, + { " ", "-", '-' }, + { "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", ' ' }, + { "a b c d", "a b c d", ' ' }, + { NULL, NULL, '\0' }, }; mem_ctx = talloc_new(NULL); 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, '\0'); + 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, '\0'); + 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_char); assert_non_null(res); assert_string_equal(res, data_set[i].output); talloc_zfree(res); @@ -100,46 +84,43 @@ void test_reverse_replace_whitespaces(void **state) struct { const char *input; const char *output; - const char *replace_string; + const char replace_char; } data_set[] = { - { "", "", "-" }, - { "-", " ", "-" }, - { "----", " ", "-" }, - { "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", ";-)" }, - { "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 ", " " }, - { NULL, NULL, NULL }, + { "", "", '-' }, + { "-", " ", '-' }, + { "----", " ", '-' }, + { "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", ' ' }, + { "a b c d", "a b c d", ' ' }, + { " a b c d ", " a b c d ", ' ' }, + { NULL, NULL, '\0' }, }; mem_ctx = talloc_new(NULL); 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, '\0'); + 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, '\0'); + 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_char); 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 8d878923f..ec4cc687e 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 subst) { - 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; + if (subst == '\0') { + return talloc_strdup(mem_ctx, orig_name); } + return replace_char(mem_ctx, orig_name, ' ', subst); +} - 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 subst) +{ + if (subst == '\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, subst, ' '); } diff --git a/src/util/util.h b/src/util/util.h index 7b53972de..66b3e87db 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -536,11 +536,11 @@ errno_t sss_br_lock_file(int fd, size_t start, size_t len, #endif /* 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_char); +char * sss_reverse_replace_space(TALLOC_CTX *mem_ctx, + char *orig_name, + const char replace_char); #endif /* __SSSD_UTIL_H__ */ -- cgit