From: Marco Bettini Date: Thu, 17 Oct 2024 07:14:26 +0000 (+0000) Subject: dict-ldap: dict_ldap_map_settings - Change value_attribute into bool list values X-Git-Tag: 2.4.0~132 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2549894a7148c8f94cd6b8163424aa61c75fad9c;p=thirdparty%2Fdovecot%2Fcore.git dict-ldap: dict_ldap_map_settings - Change value_attribute into bool list values --- diff --git a/src/lib-dict-backend/dict-ldap-settings.c b/src/lib-dict-backend/dict-ldap-settings.c index e306d8c03a..3bce98ab82 100644 --- a/src/lib-dict-backend/dict-ldap-settings.c +++ b/src/lib-dict-backend/dict-ldap-settings.c @@ -28,7 +28,7 @@ static const struct setting_define dict_ldap_map_setting_defines[] = { DEFN(STR, filter, ldap_filter), DEFN(ENUM, scope, ldap_scope), DEF(STR, username_attribute), - DEF(STR, value_attribute), + DEF(BOOLLIST, values), DEF(STRLIST, fields), SETTING_DEFINE_LIST_END }; @@ -37,7 +37,7 @@ static const struct dict_ldap_map_settings dict_ldap_map_default_settings = { .pattern = "", .filter = "", .username_attribute = "cn", - .value_attribute = "", + .values = ARRAY_INIT, .base = "", .scope = "subtree:onelevel:base", .fields = ARRAY_INIT, @@ -101,9 +101,12 @@ dict_ldap_map_settings_postcheck(struct dict_ldap_map_settings *set, return -1; } - if (*set->value_attribute == '\0') { - *error_r = "value_attribute not set"; + if (array_is_empty(&set->values)) { + *error_r = "ldap_map_value not set"; return -1; + } else { + array_append_zero(&set->values); + array_pop_back(&set->values); } if (array_is_empty(&set->fields)) { diff --git a/src/lib-dict-backend/dict-ldap-settings.h b/src/lib-dict-backend/dict-ldap-settings.h index 6a1bb4c51c..edd81de78d 100644 --- a/src/lib-dict-backend/dict-ldap-settings.h +++ b/src/lib-dict-backend/dict-ldap-settings.h @@ -7,7 +7,7 @@ struct dict_ldap_map_settings { const char *pattern; const char *filter; const char *username_attribute; - const char *value_attribute; + ARRAY_TYPE(const_string) values; const char *base; const char *scope; ARRAY_TYPE(const_string) fields; diff --git a/src/lib-dict-backend/dict-ldap.c b/src/lib-dict-backend/dict-ldap.c index b3ecaad128..3954a0386d 100644 --- a/src/lib-dict-backend/dict-ldap.c +++ b/src/lib-dict-backend/dict-ldap.c @@ -269,6 +269,38 @@ void ldap_dict_lookup_done(const struct dict_lookup_result *result, void *ctx) res->error = t_strdup(result->error); } +static void +ldap_dict_lookup_cb_values(const struct ldap_entry *entry, struct dict_ldap_op *op) +{ + e_debug(op->event, "got dn %s", + ldap_entry_dn(entry)); + + unsigned int count = array_count(&op->map->values); + i_assert(count > 0); + + ARRAY_TYPE(const_string) resp_values; + p_array_init(&resp_values, op->pool, count + 1); + + const char *attribute; + array_foreach_elem(&op->map->values, attribute) { + const char *const *values = ldap_entry_get_attribute(entry, attribute); + bool no_attribute = values == NULL; + e_debug(op->event, "%s attribute %s", + no_attribute ? "dit not get" : "got", attribute); + if (no_attribute && array_is_empty(&resp_values)) + break; + const char *value = no_attribute ? "" : p_strdup(op->pool, values[0]); + array_push_back(&resp_values, &value); + } + + array_append_zero(&resp_values); + array_pop_back(&resp_values); + bool got_values = array_not_empty(&resp_values); + op->res.values = array_front(&resp_values); + op->res.value = got_values ? op->res.values[0] : NULL; + op->res.ret = got_values ? 1 : 0; +} + static void ldap_dict_lookup_callback(struct ldap_result *result, struct dict_ldap_op *op) { @@ -284,30 +316,9 @@ ldap_dict_lookup_callback(struct ldap_result *result, struct dict_ldap_op *op) } else { iter = ldap_search_iterator_init(result); entry = ldap_search_iterator_next(iter); - if (entry != NULL) { - e_debug(op->event, "ldap_dict_lookup_callback got dn %s", - ldap_entry_dn(entry)); - /* try extract value */ - const char *const *values = ldap_entry_get_attribute( - entry, op->map->value_attribute); - if (values != NULL) { - const char **new_values; - - e_debug(op->event, - "ldap_dict_lookup_callback got attribute %s", - op->map->value_attribute); - op->res.ret = 1; - new_values = p_new(op->pool, const char *, 2); - new_values[0] = p_strdup(op->pool, values[0]); - op->res.values = new_values; - op->res.value = op->res.values[0]; - } else { - e_debug(op->event, - "ldap_dict_lookup_callback dit not get attribute %s", - op->map->value_attribute); - op->res.value = NULL; - } - } + if (entry != NULL) + ldap_dict_lookup_cb_values(entry, op); + ldap_search_iterator_deinit(&iter); } if (op->dict->dict.prev_ioloop != NULL) @@ -408,13 +419,11 @@ void ldap_dict_lookup_async(struct dict *dict, /* key needs to be transformed into something else */ ARRAY_TYPE(const_string) values; - const char *attributes[2] = {0, 0}; t_array_init(&values, 8); const struct dict_ldap_map_settings *map = ldap_dict_find_map(ctx, key, &values); if (map != NULL) { op->map = map; - attributes[0] = map->value_attribute; /* build lookup */ i_zero(&input); input.base_dn = map->base; @@ -427,7 +436,9 @@ void ldap_dict_lookup_async(struct dict *dict, return; } input.filter = str_c(query); - input.attributes = attributes; + /* Guaranteed to be NULL-terminated by + dict_ldap_map_settings_postcheck() */ + input.attributes = array_front(&map->values); ctx->pending++; ldap_search_start(ctx->client, &input, ldap_dict_lookup_callback, op); } else {