From eb0c76d3ae240a773a066ebf6f0840df27c5f73c Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Tue, 9 May 2023 10:01:53 +0300 Subject: [PATCH] auth: db-lua - Use parameters structure to initialize script Simplifies next commit. --- src/auth/db-lua.c | 4 +++- src/auth/db-lua.h | 7 ++++++- src/auth/passdb-lua.c | 10 ++++++++-- src/auth/test-lua.c | 20 ++++++++++++++++---- src/auth/userdb-lua.c | 9 +++++++-- 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/auth/db-lua.c b/src/auth/db-lua.c index 0c454547c4..adfb9c3d50 100644 --- a/src/auth/db-lua.c +++ b/src/auth/db-lua.c @@ -393,8 +393,10 @@ static void auth_lua_dovecot_auth_register(lua_State *L) lua_pop(L, 1); } -int auth_lua_script_init(struct dlua_script *script, const char **error_r) +int auth_lua_script_init(const struct auth_lua_script_parameters *params, + const char **error_r) { + struct dlua_script *script = params->script; dlua_dovecot_register(script); auth_lua_dovecot_auth_register(script->L); auth_lua_auth_request_register(script->L); diff --git a/src/auth/db-lua.h b/src/auth/db-lua.h index ebb697ac1d..be08619224 100644 --- a/src/auth/db-lua.h +++ b/src/auth/db-lua.h @@ -9,7 +9,12 @@ struct dlua_script; -int auth_lua_script_init(struct dlua_script *script, const char **error_r); +struct auth_lua_script_parameters { + struct dlua_script *script; +}; + +int auth_lua_script_init(const struct auth_lua_script_parameters *params, + const char **error_r); int auth_lua_call_password_verify(struct dlua_script *script, struct auth_request *req, const char *password, diff --git a/src/auth/passdb-lua.c b/src/auth/passdb-lua.c index b67ab0667a..ad56e9983b 100644 --- a/src/auth/passdb-lua.c +++ b/src/auth/passdb-lua.c @@ -155,9 +155,15 @@ static void passdb_lua_init(struct passdb_module *_module) (struct dlua_passdb_module *)_module; const char *error; - if (dlua_script_create_file(module->file, &module->script, auth_event, &error) < 0 || - auth_lua_script_init(module->script, &error) < 0) + if (dlua_script_create_file(module->file, &module->script, auth_event, &error) < 0) i_fatal("passdb-lua: initialization failed: %s", error); + + const struct auth_lua_script_parameters params = { + .script = module->script, + }; + if (auth_lua_script_init(¶ms, &error) < 0) + i_fatal("passdb-lua: initialization failed: %s", error); + module->has_password_verify = dlua_script_has_function(module->script, AUTH_LUA_PASSWORD_VERIFY); } diff --git a/src/auth/test-lua.c b/src/auth/test-lua.c index 0b49f825e8..a15915adec 100644 --- a/src/auth/test-lua.c +++ b/src/auth/test-lua.c @@ -45,7 +45,10 @@ static void test_db_lua_auth_verify(void) test_assert(dlua_script_create_string(luascript, &script, NULL, &error) == 0); if (script != NULL) { - test_assert(auth_lua_script_init(script, &error) == 0); + const struct auth_lua_script_parameters params = { + .script = script, + }; + test_assert(auth_lua_script_init(¶ms, &error) == 0); test_assert(auth_lua_call_password_verify(script, req, "password", &error) == 1); dlua_script_unref(&script); } @@ -79,7 +82,10 @@ static void test_db_lua_auth_lookup_numberish_value(void) test_assert(dlua_script_create_string(luascript, &script, NULL, &error) == 0); if (script != NULL) { - test_assert(auth_lua_script_init(script, &error) == 0); + const struct auth_lua_script_parameters params = { + .script = script, + }; + test_assert(auth_lua_script_init(¶ms, &error) == 0); test_assert(auth_lua_call_passdb_lookup(script, req, &scheme, &pass, &error) == 1); test_assert(strcmp(req->fields.user, "01234") == 0); dlua_script_unref(&script); @@ -112,7 +118,10 @@ static void test_db_lua_auth_lookup(void) test_assert(dlua_script_create_string(luascript, &script, NULL, &error) == 0); if (script != NULL) { - test_assert(auth_lua_script_init(script, &error) == 0); + const struct auth_lua_script_parameters params = { + .script = script, + }; + test_assert(auth_lua_script_init(¶ms, &error) == 0); test_assert(auth_lua_call_passdb_lookup(script, req, &scheme, &pass, &error) == 1); dlua_script_unref(&script); test_assert_strcmp(pass, "pass"); @@ -150,7 +159,10 @@ static void test_db_lua_auth_lookup_bad_keyname(void) test_assert(dlua_script_create_string(luascript, &script, NULL, &error) == 0); if (script != NULL) { - test_assert(auth_lua_script_init(script, &error) == 0); + const struct auth_lua_script_parameters params = { + .script = script, + }; + test_assert(auth_lua_script_init(¶ms, &error) == 0); test_expect_error_string_n_times("db-lua: Field key", 3); test_assert(auth_lua_call_passdb_lookup(script, req, &scheme, &pass, &error) == 1); dlua_script_unref(&script); diff --git a/src/auth/userdb-lua.c b/src/auth/userdb-lua.c index 1712f70162..9453f1e8eb 100644 --- a/src/auth/userdb-lua.c +++ b/src/auth/userdb-lua.c @@ -79,8 +79,13 @@ static void userdb_lua_init(struct userdb_module *_module) (struct dlua_userdb_module *)_module; const char *error; - if (dlua_script_create_file(module->file, &module->script, auth_event, &error) < 0 || - auth_lua_script_init(module->script, &error) < 0) + if (dlua_script_create_file(module->file, &module->script, auth_event, &error) < 0) + i_fatal("userdb-lua: initialization failed: %s", error); + + const struct auth_lua_script_parameters params = { + .script = module->script, + }; + if (auth_lua_script_init(¶ms, &error) < 0) i_fatal("userdb-lua: initialization failed: %s", error); } -- 2.47.3