From: Timo Sirainen Date: Tue, 12 Mar 2024 20:52:39 +0000 (+0200) Subject: auth: Add auth_cache_parse_key_and_fields() X-Git-Tag: 2.4.1~941 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=64dc734d6063aa54c2ceb0cc983f3e32a7195b32;p=thirdparty%2Fdovecot%2Fcore.git auth: Add auth_cache_parse_key_and_fields() --- diff --git a/src/auth/auth-cache.c b/src/auth/auth-cache.c index 42ac687d1a..d6cbe8bb52 100644 --- a/src/auth/auth-cache.c +++ b/src/auth/auth-cache.c @@ -80,14 +80,17 @@ static void auth_cache_key_add_tab_idx(string_t *str, unsigned int i) } } -char *auth_cache_parse_key(pool_t pool, const char *query) +static char *auth_cache_parse_key_exclude(pool_t pool, const char *query, + const char *exclude_driver) { string_t *str; bool key_seen[AUTH_REQUEST_VAR_TAB_COUNT]; const char *extra_vars; - unsigned int i, idx, size, tab_idx; + unsigned int i, idx, size, tab_idx, exclude_driver_len; memset(key_seen, 0, sizeof(key_seen)); + exclude_driver_len = exclude_driver == NULL ? 0 : + strlen(exclude_driver); str = t_str_new(32); for (; *query != '\0'; ) { @@ -107,7 +110,10 @@ char *auth_cache_parse_key(pool_t pool, const char *query) /* just add the key. it would be nice to prevent duplicates here as well, but that's just too much trouble and probably very rare. */ - auth_cache_key_add_var(str, query, size); + if (exclude_driver_len == 0 || + size < exclude_driver_len || + memcmp(query, exclude_driver, exclude_driver_len) != 0) + auth_cache_key_add_var(str, query, size); } else { i_assert(tab_idx < N_ELEMENTS(key_seen)); key_seen[tab_idx] = TRUE; @@ -144,6 +150,31 @@ char *auth_cache_parse_key(pool_t pool, const char *query) return p_strdup(pool, str_c(str)); } +char *auth_cache_parse_key(pool_t pool, const char *query) +{ + return auth_cache_parse_key_exclude(pool, query, NULL); +} + +char *auth_cache_parse_key_and_fields(pool_t pool, const char *query, + const ARRAY_TYPE(const_string) *fields, + const char *exclude_driver) +{ + if (array_is_empty(fields)) + return auth_cache_parse_key_exclude(pool, query, exclude_driver); + + string_t *full_query = t_str_new(128); + str_append(full_query, query); + + unsigned int i, count; + const char *const *str = array_get(fields, &count); + for (i = 0; i < count; i += 2) { + str_append_c(full_query, '\t'); + str_append(full_query, str[i + 1]); + } + return auth_cache_parse_key_exclude(pool, str_c(full_query), + exclude_driver); +} + static void auth_cache_node_unlink(struct auth_cache *cache, struct auth_cache_node *node) { diff --git a/src/auth/auth-cache.h b/src/auth/auth-cache.h index ab02fe57c0..9bdb918517 100644 --- a/src/auth/auth-cache.h +++ b/src/auth/auth-cache.h @@ -19,6 +19,11 @@ struct auth_request; /* Parses all %x variables from query and compresses them into tab-separated list, so it can be used as a cache key. */ char *auth_cache_parse_key(pool_t pool, const char *query); +/* Same as auth_cache_parse_key(), but add also variables from "fields", + except variables prefixed with ":" */ +char *auth_cache_parse_key_and_fields(pool_t pool, const char *query, + const ARRAY_TYPE(const_string) *fields, + const char *exclude_driver); /* Create a new cache. max_size specifies the maximum amount of memory in bytes to use for cache (it's not fully exact). ttl_secs specifies time to