static int auth_cache_parse_key_exclude(pool_t pool, const char *query,
const char *exclude_driver,
- char **cache_key_r,
+ const char **cache_key_r,
const char **error_r)
{
string_t *str;
return 0;
}
-char *auth_cache_parse_key_and_fields(pool_t pool, const char *query,
- const ARRAY_TYPE(const_string) *fields,
- const char *exclude_driver)
+int auth_cache_parse_key_and_fields(pool_t pool, const char *query,
+ const ARRAY_TYPE(const_string) *fields,
+ const char *exclude_driver,
+ const char **cache_key_r,
+ const char **error_r)
{
if (fields != NULL && !array_is_empty(fields)) {
unsigned int i, count;
query = str_c(full_query);
}
- char *cache_key;
const char *error;
if (auth_cache_parse_key_exclude(pool, query, exclude_driver,
- &cache_key, &error) < 0)
- i_fatal("auth-cache: %s", error);
- return cache_key;
+ cache_key_r, &error) < 0) {
+ *error_r = t_strdup_printf("auth-cache: %s", error);
+ return -1;
+ }
+ return 0;
}
static void
/* Parses all %variables from query and compresses them into tab-separated
list, so it can be used as a cache key. Adds also variables from "fields",
except variables prefixed with <exclude_driver>":" */
-char *auth_cache_parse_key_and_fields(pool_t pool, const char *query,
- const ARRAY_TYPE(const_string) *fields,
- const char *exclude_driver);
+int auth_cache_parse_key_and_fields(pool_t pool, const char *query,
+ const ARRAY_TYPE(const_string) *fields,
+ const char *exclude_driver,
+ const char **cache_key_r,
+ const char **error_r);
/* 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
switch (params->stype) {
case AUTH_LUA_SCRIPT_TYPE_PASSDB:
i_assert(params->passdb_module != NULL);
- params->passdb_module->module.default_cache_key =
- auth_cache_parse_key_and_fields(params->pool,
- lua_tostring(script->L, -1),
- &post_set->fields, "lua");
+ if (auth_cache_parse_key_and_fields(params->pool,
+ lua_tostring(script->L, -1),
+ &post_set->fields, "lua",
+ ¶ms->passdb_module->module.default_cache_key,
+ error_r) < 0)
+ return -1;
break;
case AUTH_LUA_SCRIPT_TYPE_USERDB:
i_assert(params->userdb_module != NULL);
- params->userdb_module->module.default_cache_key =
- auth_cache_parse_key_and_fields(params->pool,
- lua_tostring(script->L, -1),
- &post_set->fields, "lua");
+ if (auth_cache_parse_key_and_fields(params->pool,
+ lua_tostring(script->L, -1),
+ &post_set->fields, "lua",
+ ¶ms->userdb_module->module.default_cache_key,
+ error_r) < 0)
+ return -1;
break;
default:
i_unreached();
const struct passdb_parameters *passdb_params,
pool_t pool, const char *query,
const ARRAY_TYPE(const_string) *fields,
- const char *exclude_driver, const char **error_r ATTR_UNUSED)
+ const char *exclude_driver, const char **error_r)
{
if (!passdb_params->use_cache)
return 0;
- module->default_cache_key =
- auth_cache_parse_key_and_fields(pool, query, fields,
- exclude_driver);
- return 0;
+ return auth_cache_parse_key_and_fields(pool, query, fields,
+ exclude_driver, &module->default_cache_key, error_r);
}
struct passdb_module *
/* test that other providers are dropped */
{ "%{a}%{provider:user}", "%{a}" },
};
- const char *cache_key;
+ const char *cache_key, *error;
unsigned int i;
test_begin("auth cache parse key");
for (i = 0; i < N_ELEMENTS(tests); i++) {
- cache_key = auth_cache_parse_key_and_fields(pool_datastack_create(),
- tests[i].in, NULL, NULL);
+ test_assert_idx(auth_cache_parse_key_and_fields(
+ pool_datastack_create(), tests[i].in, NULL, NULL,
+ &cache_key, &error) == 0, i);
test_assert_strcmp_idx(cache_key, tests[i].out, i);
}
test_end();
}
-static enum fatal_test_state test_cache_key_missing_variable(unsigned int i)
+static void test_auth_cache_parse_key_errors(void)
{
- if (i == 0)
- test_begin("auth cache missing variable");
-
- /* ensure that we do not accept static string */
static const struct {
const char *in, *out;
} tests_bad[] = {
"failed: syntax error, unexpected end of file, " \
"expecting CCBRACE or PIPE" },
};
+ const char *cache_key, *error;
+ unsigned int i;
+
+ test_begin("auth cache parse key errors");
- if (i < N_ELEMENTS(tests_bad)) {
- test_expect_fatal_string(tests_bad[i].out);
- (void)auth_cache_parse_key_and_fields(pool_datastack_create(),
- tests_bad[i].in, NULL, NULL);
- return FATAL_TEST_FAILURE;
+ for (i = 0; i < N_ELEMENTS(tests_bad); i++) {
+ test_assert_idx(auth_cache_parse_key_and_fields(
+ pool_datastack_create(), tests_bad[i].in, NULL, NULL,
+ &cache_key, &error) < 0, i);
+ test_assert_strcmp_idx(error, tests_bad[i].out, i);
}
test_end();
- return FATAL_TEST_FINISHED;
}
int main(void)
auth_event = event_create(NULL);
static void (*const test_functions[])(void) = {
test_auth_cache_parse_key,
+ test_auth_cache_parse_key_errors,
NULL
};
- static test_fatal_func_t *const fatal_functions[] = {
- test_cache_key_missing_variable,
- NULL,
- };
-
- int ret = test_run_with_fatals(test_functions, fatal_functions);
+ int ret = test_run(test_functions);
event_unref(&auth_event);
return ret;
const struct userdb_parameters *userdb_params,
pool_t pool, const char *query,
const ARRAY_TYPE(const_string) *fields,
- const char *exclude_driver, const char **error_r ATTR_UNUSED)
+ const char *exclude_driver, const char **error_r)
{
if (!userdb_params->use_cache)
return 0;
- module->default_cache_key =
- auth_cache_parse_key_and_fields(pool, query, fields,
- exclude_driver);
- return 0;
+ return auth_cache_parse_key_and_fields(pool, query, fields,
+ exclude_driver, &module->default_cache_key, error_r);
}
struct userdb_module *