From d669962c40ca83a9912c1c977b09e7488794f4df Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Tue, 6 Dec 2011 15:02:37 +0100 Subject: [PATCH 5/6] Use the case sensitivity flag in the LDAP provider --- src/db/sysdb.c | 36 +++++++++++++++++++++++++--- src/db/sysdb.h | 1 + src/providers/ldap/sdap_async.c | 10 +++++--- src/providers/ldap/sdap_async.h | 1 + src/providers/ldap/sdap_async_groups.c | 4 +- src/providers/ldap/sdap_async_netgroups.c | 8 ++++++ src/providers/ldap/sdap_async_users.c | 2 +- 7 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/db/sysdb.c b/src/db/sysdb.c index c49399f..e557bf2 100644 --- a/src/db/sysdb.c +++ b/src/db/sysdb.c @@ -22,6 +22,7 @@ #include "util/util.h" #include "util/strtonum.h" +#include "util/sss_utf8.h" #include "db/sysdb_private.h" #include "confdb/confdb.h" #include @@ -1587,18 +1588,22 @@ done: * Given a primary name returned by sysdb_attrs_primary_name(), this function * returns the other SYSDB_NAME attribute values so they can be saved as * SYSDB_NAME_ALIAS into cache. + * + * If lowercase is set, all aliases are duplicated in lowercase as well. */ errno_t sysdb_attrs_get_aliases(TALLOC_CTX *mem_ctx, struct sysdb_attrs *attrs, const char *primary, + bool lowercase, const char ***_aliases) { TALLOC_CTX *tmp_ctx = NULL; struct ldb_message_element *sysdb_name_el; - size_t i, ai; + size_t i, ai, num; errno_t ret; const char **aliases = NULL; const char *name; + char *lower, *c; if (_aliases == NULL) return EINVAL; @@ -1615,8 +1620,8 @@ errno_t sysdb_attrs_get_aliases(TALLOC_CTX *mem_ctx, goto done; } - aliases = talloc_array(tmp_ctx, const char *, - sysdb_name_el->num_values); + num = lowercase ? 2 * sysdb_name_el->num_values : sysdb_name_el->num_values; + aliases = talloc_array(tmp_ctx, const char *, num+1); if (!aliases) { ret = ENOMEM; goto done; @@ -1626,11 +1631,34 @@ errno_t sysdb_attrs_get_aliases(TALLOC_CTX *mem_ctx, for (i=0; i < sysdb_name_el->num_values; i++) { name = (const char *)sysdb_name_el->values[i].data; if (strcmp(primary, name) != 0) { - aliases[ai] = name; + aliases[ai] = talloc_strdup(aliases, name); + if (!aliases[ai]) { + ret = ENOMEM; + goto done; + } ai++; } } + if (lowercase) { + DEBUG(SSSDBG_TRACE_INTERNAL, + ("Domain is case-insensitive; will add lowercased aliases\n")); + for (i=0; i < sysdb_name_el->num_values; i++) { + name = (const char *)sysdb_name_el->values[i].data; + lower = (char *) sss_utf8_tolower(tmp_ctx, (const uint8_t *) name); + if (!lower) { + ret = ENOMEM; + goto done; + } + + if (strcmp(name, lower) != 0) { + aliases[ai] = talloc_strdup(aliases, lower); + ai++; + } + talloc_free(lower); + } + } + aliases[ai] = NULL; ret = EOK; done: diff --git a/src/db/sysdb.h b/src/db/sysdb.h index 5fc36ab..82d1fd0 100644 --- a/src/db/sysdb.h +++ b/src/db/sysdb.h @@ -241,6 +241,7 @@ errno_t sysdb_attrs_primary_name(struct sysdb_ctx *sysdb, errno_t sysdb_attrs_get_aliases(TALLOC_CTX *mem_ctx, struct sysdb_attrs *attrs, const char *primary, + bool lowercase, const char ***_aliases); errno_t sysdb_attrs_primary_name_list(struct sysdb_ctx *sysdb, TALLOC_CTX *mem_ctx, diff --git a/src/providers/ldap/sdap_async.c b/src/providers/ldap/sdap_async.c index 98291e6..db9bf76 100644 --- a/src/providers/ldap/sdap_async.c +++ b/src/providers/ldap/sdap_async.c @@ -1920,7 +1920,8 @@ errno_t sdap_check_aliases(struct sysdb_ctx *sysdb, goto done; } - ret = sysdb_attrs_get_aliases(tmp_ctx, user_attrs, name, &aliases); + ret = sysdb_attrs_get_aliases(tmp_ctx, user_attrs, name, + !dom->case_sensitive, &aliases); if (ret != EOK) { DEBUG(1, ("Failed to get the alias list\n")); goto done; @@ -2024,16 +2025,17 @@ sdap_attrs_add_ldap_attr(struct sysdb_attrs *ldap_attrs, return EOK; } - errno_t sdap_save_all_names(const char *name, struct sysdb_attrs *ldap_attrs, + struct sss_domain_info *dom, struct sysdb_attrs *attrs) { const char **aliases = NULL; errno_t ret; TALLOC_CTX *tmp_ctx; int i; + char *lowername; tmp_ctx = talloc_new(NULL); if (!tmp_ctx) { @@ -2041,7 +2043,8 @@ sdap_save_all_names(const char *name, goto done; } - ret = sysdb_attrs_get_aliases(tmp_ctx, ldap_attrs, name, &aliases); + ret = sysdb_attrs_get_aliases(tmp_ctx, ldap_attrs, name, + !dom->case_sensitive, &aliases); if (ret != EOK) { DEBUG(SSSDBG_OP_FAILURE, ("Failed to get the alias list")); goto done; @@ -2062,4 +2065,3 @@ done: talloc_free(tmp_ctx); return ret; } - diff --git a/src/providers/ldap/sdap_async.h b/src/providers/ldap/sdap_async.h index f53af1e..8d3fd40 100644 --- a/src/providers/ldap/sdap_async.h +++ b/src/providers/ldap/sdap_async.h @@ -208,6 +208,7 @@ sdap_attrs_add_ldap_attr(struct sysdb_attrs *ldap_attrs, errno_t sdap_save_all_names(const char *name, struct sysdb_attrs *ldap_attrs, + struct sss_domain_info *dom, struct sysdb_attrs *attrs); #endif /* _SDAP_ASYNC_H_ */ diff --git a/src/providers/ldap/sdap_async_groups.c b/src/providers/ldap/sdap_async_groups.c index 1b26ec1..caa0129 100644 --- a/src/providers/ldap/sdap_async_groups.c +++ b/src/providers/ldap/sdap_async_groups.c @@ -350,9 +350,9 @@ static int sdap_save_group(TALLOC_CTX *memctx, } } - ret = sdap_save_all_names(name, attrs, group_attrs); + ret = sdap_save_all_names(name, attrs, dom, group_attrs); if (ret != EOK) { - DEBUG(1, ("Failed to save user names\n")); + DEBUG(1, ("Failed to save group names\n")); goto fail; } diff --git a/src/providers/ldap/sdap_async_netgroups.c b/src/providers/ldap/sdap_async_netgroups.c index 9c66963..fcc609e 100644 --- a/src/providers/ldap/sdap_async_netgroups.c +++ b/src/providers/ldap/sdap_async_netgroups.c @@ -38,6 +38,7 @@ bool is_dn(const char *str) static errno_t sdap_save_netgroup(TALLOC_CTX *memctx, struct sysdb_ctx *ctx, + struct sss_domain_info *dom, struct sdap_options *opts, struct sysdb_attrs *attrs, char **_timestamp, @@ -120,6 +121,12 @@ static errno_t sdap_save_netgroup(TALLOC_CTX *memctx, DEBUG(6, ("Storing info for netgroup %s\n", name)); + ret = sdap_save_all_names(name, attrs, dom, netgroup_attrs); + if (ret != EOK) { + DEBUG(1, ("Failed to save netgroup names\n")); + goto fail; + } + ret = sysdb_add_netgroup(ctx, name, NULL, netgroup_attrs, dp_opt_get_int(opts->basic, SDAP_ENTRY_CACHE_TIMEOUT), now); @@ -672,6 +679,7 @@ static void netgr_translate_members_done(struct tevent_req *subreq) now = time(NULL); for (c = 0; c < state->count; c++) { ret = sdap_save_netgroup(state, state->sysdb, + state->dom, state->opts, state->netgroups[c], &state->higher_timestamp, diff --git a/src/providers/ldap/sdap_async_users.c b/src/providers/ldap/sdap_async_users.c index 2d2a64b..8bd6409 100644 --- a/src/providers/ldap/sdap_async_users.c +++ b/src/providers/ldap/sdap_async_users.c @@ -237,7 +237,7 @@ int sdap_save_user(TALLOC_CTX *memctx, } } - ret = sdap_save_all_names(name, attrs, user_attrs); + ret = sdap_save_all_names(name, attrs, dom, user_attrs); if (ret != EOK) { DEBUG(1, ("Failed to save user names\n")); goto fail; -- 1.7.7.3