From: Markus Valentin Date: Fri, 15 Mar 2024 08:43:26 +0000 (+0100) Subject: auth|lib-lua: Replace auth_lua_config with newly added lua_settings X-Git-Tag: 2.4.1~920 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3c0fad37045c7480ce48e940c8af11d498d23acc;p=thirdparty%2Fdovecot%2Fcore.git auth|lib-lua: Replace auth_lua_config with newly added lua_settings This adds lua_settings setting. This allows to pass a key-value table to the lua script_init() call. This replaces the auth_lua_config which was previously used with passdb-lua and userdb-lua to pass parameters to it. --- diff --git a/src/auth/db-lua.c b/src/auth/db-lua.c index 88c05958b0..b7e211b07f 100644 --- a/src/auth/db-lua.c +++ b/src/auth/db-lua.c @@ -20,10 +20,8 @@ #include "auth-request-var-expand.h" #include "settings.h" -#define AUTH_LUA_PASSDB_INIT "auth_passdb_init" #define AUTH_LUA_PASSDB_LOOKUP "auth_passdb_lookup" #define AUTH_LUA_PASSDB_GET_CACHE_KEY "auth_passdb_get_cache_key" -#define AUTH_LUA_USERDB_INIT "auth_userdb_init" #define AUTH_LUA_USERDB_LOOKUP "auth_userdb_lookup" #define AUTH_LUA_USERDB_ITERATE "auth_userdb_iterate" #define AUTH_LUA_USERDB_GET_CACHE_KEY "auth_userdb_get_cache_key" @@ -48,15 +46,10 @@ struct auth_lua_userdb_iterate_context { static const struct setting_define auth_lua_setting_defines[] = { { .type = SET_FILTER_NAME, .key = "passdb_lua", }, { .type = SET_FILTER_NAME, .key = "userdb_lua", }, - DEF(STRLIST, auth_lua_config), SETTING_DEFINE_LIST_END }; -static const struct auth_lua_settings auth_lua_default_settings = { - .auth_lua_config = ARRAY_INIT, -}; - static const struct setting_keyvalue auth_lua_default_settings_keyvalue[] = { { "passdb_lua/passdb_use_worker", "yes"}, { "userdb_lua/userdb_use_worker", "yes"}, @@ -67,7 +60,6 @@ const struct setting_parser_info auth_lua_setting_parser_info = { .name = "auth_lua", .defines = auth_lua_setting_defines, - .defaults = &auth_lua_default_settings, .default_settings = auth_lua_default_settings_keyvalue, .struct_size = sizeof(struct auth_lua_settings), @@ -548,56 +540,6 @@ auth_lua_script_get_default_cache_key(const struct auth_lua_script_parameters *p return 0; } -int auth_lua_script_auth_db_init(const struct auth_lua_script_parameters *params, - const char **error_r) -{ - struct dlua_script *script = params->script; - const struct auth_lua_settings *set; - const char *fn; - - switch (params->stype) { - case AUTH_LUA_SCRIPT_TYPE_PASSDB: - fn = AUTH_LUA_PASSDB_INIT; - break; - case AUTH_LUA_SCRIPT_TYPE_USERDB: - fn = AUTH_LUA_USERDB_INIT; - break; - default: - i_unreached(); - } - if (!dlua_script_has_function(script, fn)) - return 0; - - if (settings_get(script->event, &auth_lua_setting_parser_info, 0, - &set, error_r) < 0) - return -1; - - i_assert(array_is_empty(&set->auth_lua_config) || - (array_count(&set->auth_lua_config) % 2 == 0)); - if (!array_is_empty(&set->auth_lua_config)) { - /* prepare a table for arguments */ - lua_createtable(script->L, 0, - array_count(&set->auth_lua_config) / 2); - unsigned int count; - const char *const *str_array = array_get(&set->auth_lua_config, - &count); - for (unsigned int i = 0; i < count; i += 2) { - lua_pushstring(script->L, str_array[i + 1]); - lua_setfield(script->L, -2, str_array[i]); - } - } else { - lua_newtable(script->L); - } - - settings_free(set); - /* call the function */ - if (dlua_pcall(script->L, fn, 1, 0, error_r) < 0) - return -1; - - i_assert(lua_gettop(script->L) == 0); - return 0; -} - static void auth_lua_export_fields(struct auth_request *req, const char *str, diff --git a/src/auth/db-lua.h b/src/auth/db-lua.h index 2b17bf0102..e1e8abfffa 100644 --- a/src/auth/db-lua.h +++ b/src/auth/db-lua.h @@ -9,7 +9,6 @@ struct dlua_script; struct auth_lua_settings { pool_t pool; - ARRAY_TYPE(const_string) auth_lua_config; }; extern const struct setting_parser_info auth_lua_setting_parser_info; @@ -41,9 +40,6 @@ struct auth_lua_script_parameters { int auth_lua_script_get_default_cache_key(const struct auth_lua_script_parameters *params, const char **error_r); -int -auth_lua_script_auth_db_init(const struct auth_lua_script_parameters *params, - const char **error_r); int auth_lua_script_init(const struct auth_lua_script_parameters *params, const char **error_r); diff --git a/src/auth/passdb-lua.c b/src/auth/passdb-lua.c index dd73d436f2..861dca7858 100644 --- a/src/auth/passdb-lua.c +++ b/src/auth/passdb-lua.c @@ -134,15 +134,6 @@ static void passdb_lua_init(struct passdb_module *_module) { struct dlua_passdb_module *module = (struct dlua_passdb_module *)_module; - const char *error; - - const struct auth_lua_script_parameters params = { - .script = module->script, - .stype = AUTH_LUA_SCRIPT_TYPE_PASSDB, - .passdb_module = module, - }; - if (auth_lua_script_auth_db_init(¶ms, &error) < 0) - i_fatal("passdb-lua: auth_passdb_init() failed: %s", error); module->has_password_verify = dlua_script_has_function(module->script, AUTH_LUA_PASSWORD_VERIFY); diff --git a/src/auth/userdb-lua.c b/src/auth/userdb-lua.c index 7f7dd65ce1..b1e9de8b0d 100644 --- a/src/auth/userdb-lua.c +++ b/src/auth/userdb-lua.c @@ -56,21 +56,6 @@ userdb_lua_preinit(pool_t pool, struct event *event, return 0; } -static void userdb_lua_init(struct userdb_module *_module) -{ - struct dlua_userdb_module *module = - (struct dlua_userdb_module *)_module; - const char *error; - - const struct auth_lua_script_parameters params = { - .script = module->script, - .stype = AUTH_LUA_SCRIPT_TYPE_USERDB, - .userdb_module = module, - }; - if (auth_lua_script_auth_db_init(¶ms, &error) < 0) - i_fatal("userdb-lua: auth_userdb_init() failed: %s", error); -} - static void userdb_lua_deinit(struct userdb_module *_module) { struct dlua_userdb_module *module = @@ -109,7 +94,6 @@ struct userdb_module_interface userdb_lua_plugin = .name = "lua", .preinit = userdb_lua_preinit, - .init = userdb_lua_init, .deinit = userdb_lua_deinit, .lookup = userdb_lua_lookup, diff --git a/src/lib-lua/dlua-script.c b/src/lib-lua/dlua-script.c index 7e88702939..4e25b23fc5 100644 --- a/src/lib-lua/dlua-script.c +++ b/src/lib-lua/dlua-script.c @@ -2,6 +2,7 @@ #include "lib.h" #include "llist.h" +#include "array.h" #include "istream.h" #include "sha1.h" #include "str.h" @@ -33,12 +34,14 @@ static struct dlua_script *dlua_scripts = NULL; static const struct setting_define dlua_setting_defines[] = { DEF(FILE, file), + DEF(STRLIST, settings), SETTING_DEFINE_LIST_END }; static const struct dlua_settings dlua_default_settings = { .file = "", + .settings = ARRAY_INIT, }; const struct setting_parser_info dlua_setting_parser_info = { @@ -187,6 +190,8 @@ static void dlua_call_deinit_function(struct dlua_script *script) int dlua_script_init(struct dlua_script *script, const char **error_r) { + const struct dlua_settings *set; + if (script->init) return 0; script->init = TRUE; @@ -200,9 +205,33 @@ int dlua_script_init(struct dlua_script *script, const char **error_r) int ret = 0; - if (dlua_pcall(script->L, LUA_SCRIPT_INIT_FN, 0, 0, error_r) < 0) + if (settings_get(script->event, &dlua_setting_parser_info, 0, &set, + error_r) < 0) + return -1; + + if (!array_is_empty(&set->settings)) { + i_assert((array_count(&set->settings) % 2 == 0)); + + /* prepare a table for arguments */ + lua_createtable(script->L, 0, + array_count(&set->settings) / 2); + unsigned int count; + const char *const *str_array = array_get(&set->settings, + &count); + for (unsigned int i = 0; i < count; i += 2) { + lua_pushstring(script->L, str_array[i + 1]); + lua_setfield(script->L, -2, str_array[i]); + } + } else { + lua_newtable(script->L); + } + + if (dlua_pcall(script->L, LUA_SCRIPT_INIT_FN, 1, 0, error_r) < 0) { + settings_free(set); return -1; + } + settings_free(set); i_assert(lua_gettop(script->L) == 0); return ret; } diff --git a/src/lib-lua/dlua-script.h b/src/lib-lua/dlua-script.h index d0a674e161..5290e06365 100644 --- a/src/lib-lua/dlua-script.h +++ b/src/lib-lua/dlua-script.h @@ -9,6 +9,7 @@ struct dlua_settings { pool_t pool; const char *file; + ARRAY_TYPE(const_string) settings; }; /* Parse and load a lua script, without actually running it. */