#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"
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"},
.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),
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,
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;
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);
{
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);
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 =
.name = "lua",
.preinit = userdb_lua_preinit,
- .init = userdb_lua_init,
.deinit = userdb_lua_deinit,
.lookup = userdb_lua_lookup,
#include "lib.h"
#include "llist.h"
+#include "array.h"
#include "istream.h"
#include "sha1.h"
#include "str.h"
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 = {
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;
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;
}
pool_t pool;
const char *file;
+ ARRAY_TYPE(const_string) settings;
};
/* Parse and load a lua script, without actually running it. */