diff options
-rw-r--r-- | src/libserver/cfg_rcl.c | 316 | ||||
-rw-r--r-- | src/lua/lua_common.c | 320 | ||||
-rw-r--r-- | src/lua/lua_common.h | 4 |
3 files changed, 325 insertions, 315 deletions
diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c index 62f9e3bef..abb0e9eef 100644 --- a/src/libserver/cfg_rcl.c +++ b/src/libserver/cfg_rcl.c @@ -623,320 +623,6 @@ rspamd_rcl_worker_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, return TRUE; } -#define RSPAMD_CONFDIR_INDEX "CONFDIR" -#define RSPAMD_RUNDIR_INDEX "RUNDIR" -#define RSPAMD_DBDIR_INDEX "DBDIR" -#define RSPAMD_LOGDIR_INDEX "LOGDIR" -#define RSPAMD_PLUGINSDIR_INDEX "PLUGINSDIR" -#define RSPAMD_RULESDIR_INDEX "RULESDIR" -#define RSPAMD_LUALIBDIR_INDEX "LUALIBDIR" -#define RSPAMD_WWWDIR_INDEX "WWWDIR" -#define RSPAMD_PREFIX_INDEX "PREFIX" -#define RSPAMD_VERSION_INDEX "VERSION" - -static gint -rspamd_rcl_cmp_components (const gchar *comp1, const gchar *comp2) -{ - guint v1, v2; - - v1 = strtoul (comp1, NULL, 10); - v2 = strtoul (comp2, NULL, 10); - - return v1 - v2; -} - -static int -rspamd_rcl_lua_version_cmp (lua_State *L) -{ - const gchar *ver; - gchar **components; - gint ret = 0; - - if (lua_type (L, 2) == LUA_TSTRING) { - ver = lua_tostring (L, 2); - - components = g_strsplit_set (ver, ".-_", -1); - - if (!components) { - return luaL_error (L, "invalid arguments to 'cmp': %s", ver); - } - - if (components[0]) { - ret = rspamd_rcl_cmp_components (components[0], RSPAMD_VERSION_MAJOR); - } - - if (ret) { - goto set; - } - - if (components[1]) { - ret = rspamd_rcl_cmp_components (components[1], RSPAMD_VERSION_MINOR); - } - - if (ret) { - goto set; - } - - if (components[2]) { - ret = rspamd_rcl_cmp_components (components[2], RSPAMD_VERSION_PATCH); - } - - /* - * XXX: we don't compare git releases assuming that it is meaningless - */ - } - else { - return luaL_error (L, "invalid arguments to 'cmp'"); - } - -set: - g_strfreev (components); - lua_pushnumber (L, ret); - - return 1; -} - -static int -rspamd_rcl_lua_version_numeric (lua_State *L) -{ - static gint64 version_num = RSPAMD_VERSION_NUM; - const gchar *type; - - if (lua_gettop (L) >= 2 && lua_type (L, 1) == LUA_TSTRING) { - type = lua_tostring (L, 1); - if (g_ascii_strcasecmp (type, "short") == 0) { - version_num = RSPAMD_VERSION_MAJOR_NUM * 1000 + - RSPAMD_VERSION_MINOR_NUM * 100 + RSPAMD_VERSION_PATCH_NUM * 10; - } - else if (g_ascii_strcasecmp (type, "main") == 0) { - version_num = RSPAMD_VERSION_MAJOR_NUM * 1000 + - RSPAMD_VERSION_MINOR_NUM * 100; - } - else if (g_ascii_strcasecmp (type, "major") == 0) { - version_num = RSPAMD_VERSION_MAJOR_NUM; - } - else if (g_ascii_strcasecmp (type, "minor") == 0) { - version_num = RSPAMD_VERSION_MINOR_NUM; - } - else if (g_ascii_strcasecmp (type, "patch") == 0) { - version_num = RSPAMD_VERSION_PATCH_NUM; - } - } - - lua_pushnumber (L, version_num); - - return 1; -} - -static int -rspamd_rcl_lua_version (lua_State *L) -{ - const gchar *result = NULL, *type; - - if (lua_gettop (L) == 0) { - result = RVERSION; - } - else if (lua_gettop (L) >= 1 && lua_type (L, 1) == LUA_TSTRING) { - /* We got something like string */ - type = lua_tostring (L, 1); - - if (g_ascii_strcasecmp (type, "short") == 0) { - result = RSPAMD_VERSION_MAJOR - "." RSPAMD_VERSION_MINOR - "." RSPAMD_VERSION_PATCH; - } - else if (g_ascii_strcasecmp (type, "main") == 0) { - result = RSPAMD_VERSION_MAJOR "." RSPAMD_VERSION_MINOR; - } - else if (g_ascii_strcasecmp (type, "major") == 0) { - result = RSPAMD_VERSION_MAJOR; - } - else if (g_ascii_strcasecmp (type, "minor") == 0) { - result = RSPAMD_VERSION_MINOR; - } - else if (g_ascii_strcasecmp (type, "patch") == 0) { - result = RSPAMD_VERSION_PATCH; - } - else if (g_ascii_strcasecmp (type, "id") == 0) { - result = RID; - } - else if (g_ascii_strcasecmp (type, "num") == 0) { - return rspamd_rcl_lua_version_numeric (L); - } - else if (g_ascii_strcasecmp (type, "cmp") == 0) { - return rspamd_rcl_lua_version_cmp (L); - } - } - - lua_pushstring (L, result); - - return 1; -} - -static void -rspamd_rcl_set_lua_globals (struct rspamd_config *cfg, lua_State *L, - GHashTable *vars) -{ - struct rspamd_config **pcfg; - - /* First check for global variable 'config' */ - lua_getglobal (L, "config"); - if (lua_isnil (L, -1)) { - /* Assign global table to set up attributes */ - lua_newtable (L); - lua_setglobal (L, "config"); - } - - lua_getglobal (L, "metrics"); - if (lua_isnil (L, -1)) { - lua_newtable (L); - lua_setglobal (L, "metrics"); - } - - lua_getglobal (L, "composites"); - if (lua_isnil (L, -1)) { - lua_newtable (L); - lua_setglobal (L, "composites"); - } - - lua_getglobal (L, "rspamd_classifiers"); - if (lua_isnil (L, -1)) { - lua_newtable (L); - lua_setglobal (L, "rspamd_classifiers"); - } - - lua_getglobal (L, "classifiers"); - if (lua_isnil (L, -1)) { - lua_newtable (L); - lua_setglobal (L, "classifiers"); - } - - lua_getglobal (L, "rspamd_version"); - if (lua_isnil (L, -1)) { - lua_pushcfunction (L, rspamd_rcl_lua_version); - lua_setglobal (L, "rspamd_version"); - } - - pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *)); - rspamd_lua_setclass (L, "rspamd{config}", -1); - *pcfg = cfg; - lua_setglobal (L, "rspamd_config"); - - /* Clear stack from globals */ - lua_pop (L, 4); - - rspamd_lua_set_path (L, cfg->rcl_obj, vars); - - /* Set known paths as rspamd_paths global */ - lua_getglobal (L, "rspamd_paths"); - if (lua_isnil (L, -1)) { - const gchar *confdir = RSPAMD_CONFDIR, *rundir = RSPAMD_RUNDIR, - *dbdir = RSPAMD_DBDIR, *logdir = RSPAMD_LOGDIR, - *wwwdir = RSPAMD_WWWDIR, *pluginsdir = RSPAMD_PLUGINSDIR, - *rulesdir = RSPAMD_RULESDIR, *lualibdir = RSPAMD_LUALIBDIR, - *prefix = RSPAMD_PREFIX; - const gchar *t; - - /* Try environment */ - t = getenv ("PLUGINSDIR"); - if (t) { - pluginsdir = t; - } - - t = getenv ("RULESDIR"); - if (t) { - rulesdir = t; - } - - t = getenv ("DBDIR"); - if (t) { - dbdir = t; - } - - t = getenv ("RUNDIR"); - if (t) { - rundir = t; - } - - t = getenv ("LUALIBDIR"); - if (t) { - lualibdir = t; - } - - t = getenv ("LOGDIR"); - if (t) { - logdir = t; - } - - t = getenv ("WWWDIR"); - if (t) { - wwwdir = t; - } - - t = getenv ("CONFDIR"); - if (t) { - confdir = t; - } - - - if (vars) { - t = g_hash_table_lookup (vars, "PLUGINSDIR"); - if (t) { - pluginsdir = t; - } - - t = g_hash_table_lookup (vars, "RULESDIR"); - if (t) { - rulesdir = t; - } - - t = g_hash_table_lookup (vars, "LUALIBDIR"); - if (t) { - lualibdir = t; - } - - t = g_hash_table_lookup (vars, "RUNDIR"); - if (t) { - rundir = t; - } - - t = g_hash_table_lookup (vars, "WWWDIR"); - if (t) { - wwwdir = t; - } - - t = g_hash_table_lookup (vars, "CONFDIR"); - if (t) { - confdir = t; - } - - t = g_hash_table_lookup (vars, "DBDIR"); - if (t) { - dbdir = t; - } - - t = g_hash_table_lookup (vars, "LOGDIR"); - if (t) { - logdir = t; - } - } - - lua_createtable (L, 0, 9); - - rspamd_lua_table_set (L, RSPAMD_CONFDIR_INDEX, confdir); - rspamd_lua_table_set (L, RSPAMD_RUNDIR_INDEX, rundir); - rspamd_lua_table_set (L, RSPAMD_DBDIR_INDEX, dbdir); - rspamd_lua_table_set (L, RSPAMD_LOGDIR_INDEX, logdir); - rspamd_lua_table_set (L, RSPAMD_WWWDIR_INDEX, wwwdir); - rspamd_lua_table_set (L, RSPAMD_PLUGINSDIR_INDEX, pluginsdir); - rspamd_lua_table_set (L, RSPAMD_RULESDIR_INDEX, rulesdir); - rspamd_lua_table_set (L, RSPAMD_LUALIBDIR_INDEX, lualibdir); - rspamd_lua_table_set (L, RSPAMD_PREFIX_INDEX, prefix); - - lua_setglobal (L, "rspamd_paths"); - } -} - static gboolean rspamd_rcl_lua_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, const gchar *key, gpointer ud, @@ -3740,7 +3426,7 @@ rspamd_config_read (struct rspamd_config *cfg, const gchar *filename, top = rspamd_rcl_config_init (cfg); rspamd_lua_set_path (cfg->lua_state, cfg->rcl_obj, vars); - rspamd_rcl_set_lua_globals (cfg, cfg->lua_state, vars); + rspamd_lua_set_globals (cfg, cfg->lua_state, vars); rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_rcl_section_free, top); err = NULL; diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index f33bc3daa..230c64b78 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -375,6 +375,326 @@ rspamd_lua_set_path (lua_State *L, const ucl_object_t *cfg_obj, GHashTable *vars lua_pop (L, 1); } +#define RSPAMD_CONFDIR_INDEX "CONFDIR" +#define RSPAMD_RUNDIR_INDEX "RUNDIR" +#define RSPAMD_DBDIR_INDEX "DBDIR" +#define RSPAMD_LOGDIR_INDEX "LOGDIR" +#define RSPAMD_PLUGINSDIR_INDEX "PLUGINSDIR" +#define RSPAMD_RULESDIR_INDEX "RULESDIR" +#define RSPAMD_LUALIBDIR_INDEX "LUALIBDIR" +#define RSPAMD_WWWDIR_INDEX "WWWDIR" +#define RSPAMD_PREFIX_INDEX "PREFIX" +#define RSPAMD_VERSION_INDEX "VERSION" + +static gint +rspamd_lua_cmp_version_components (const gchar *comp1, const gchar *comp2) +{ + guint v1, v2; + + v1 = strtoul (comp1, NULL, 10); + v2 = strtoul (comp2, NULL, 10); + + return v1 - v2; +} + +static int +rspamd_lua_rspamd_version_cmp (lua_State *L) +{ + const gchar *ver; + gchar **components; + gint ret = 0; + + if (lua_type (L, 2) == LUA_TSTRING) { + ver = lua_tostring (L, 2); + + components = g_strsplit_set (ver, ".-_", -1); + + if (!components) { + return luaL_error (L, "invalid arguments to 'cmp': %s", ver); + } + + if (components[0]) { + ret = rspamd_lua_cmp_version_components (components[0], + RSPAMD_VERSION_MAJOR); + } + + if (ret) { + goto set; + } + + if (components[1]) { + ret = rspamd_lua_cmp_version_components (components[1], + RSPAMD_VERSION_MINOR); + } + + if (ret) { + goto set; + } + + if (components[2]) { + ret = rspamd_lua_cmp_version_components (components[2], + RSPAMD_VERSION_PATCH); + } + + /* + * XXX: we don't compare git releases assuming that it is meaningless + */ + } + else { + return luaL_error (L, "invalid arguments to 'cmp'"); + } + +set: + g_strfreev (components); + lua_pushnumber (L, ret); + + return 1; +} + +static int +rspamd_lua_rspamd_version_numeric (lua_State *L) +{ + static gint64 version_num = RSPAMD_VERSION_NUM; + const gchar *type; + + if (lua_gettop (L) >= 2 && lua_type (L, 1) == LUA_TSTRING) { + type = lua_tostring (L, 1); + if (g_ascii_strcasecmp (type, "short") == 0) { + version_num = RSPAMD_VERSION_MAJOR_NUM * 1000 + + RSPAMD_VERSION_MINOR_NUM * 100 + + RSPAMD_VERSION_PATCH_NUM * 10; + } + else if (g_ascii_strcasecmp (type, "main") == 0) { + version_num = RSPAMD_VERSION_MAJOR_NUM * 1000 + + RSPAMD_VERSION_MINOR_NUM * 100; + } + else if (g_ascii_strcasecmp (type, "major") == 0) { + version_num = RSPAMD_VERSION_MAJOR_NUM; + } + else if (g_ascii_strcasecmp (type, "minor") == 0) { + version_num = RSPAMD_VERSION_MINOR_NUM; + } + else if (g_ascii_strcasecmp (type, "patch") == 0) { + version_num = RSPAMD_VERSION_PATCH_NUM; + } + } + + lua_pushnumber (L, version_num); + + return 1; +} + +static int +rspamd_lua_rspamd_version (lua_State *L) +{ + const gchar *result = NULL, *type; + + if (lua_gettop (L) == 0) { + result = RVERSION; + } + else if (lua_gettop (L) >= 1 && lua_type (L, 1) == LUA_TSTRING) { + /* We got something like string */ + type = lua_tostring (L, 1); + + if (g_ascii_strcasecmp (type, "short") == 0) { + result = RSPAMD_VERSION_MAJOR + "." RSPAMD_VERSION_MINOR + "." RSPAMD_VERSION_PATCH; + } + else if (g_ascii_strcasecmp (type, "main") == 0) { + result = RSPAMD_VERSION_MAJOR "." RSPAMD_VERSION_MINOR; + } + else if (g_ascii_strcasecmp (type, "major") == 0) { + result = RSPAMD_VERSION_MAJOR; + } + else if (g_ascii_strcasecmp (type, "minor") == 0) { + result = RSPAMD_VERSION_MINOR; + } + else if (g_ascii_strcasecmp (type, "patch") == 0) { + result = RSPAMD_VERSION_PATCH; + } + else if (g_ascii_strcasecmp (type, "id") == 0) { + result = RID; + } + else if (g_ascii_strcasecmp (type, "num") == 0) { + return rspamd_lua_rspamd_version_numeric (L); + } + else if (g_ascii_strcasecmp (type, "cmp") == 0) { + return rspamd_lua_rspamd_version_cmp (L); + } + } + + lua_pushstring (L, result); + + return 1; +} + +void +rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L, + GHashTable *vars) +{ + struct rspamd_config **pcfg; + gint orig_top = lua_gettop (L); + + /* First check for global variable 'config' */ + lua_getglobal (L, "config"); + if (lua_isnil (L, -1)) { + /* Assign global table to set up attributes */ + lua_newtable (L); + lua_setglobal (L, "config"); + } + + lua_getglobal (L, "metrics"); + if (lua_isnil (L, -1)) { + lua_newtable (L); + lua_setglobal (L, "metrics"); + } + + lua_getglobal (L, "composites"); + if (lua_isnil (L, -1)) { + lua_newtable (L); + lua_setglobal (L, "composites"); + } + + lua_getglobal (L, "rspamd_classifiers"); + if (lua_isnil (L, -1)) { + lua_newtable (L); + lua_setglobal (L, "rspamd_classifiers"); + } + + lua_getglobal (L, "classifiers"); + if (lua_isnil (L, -1)) { + lua_newtable (L); + lua_setglobal (L, "classifiers"); + } + + lua_getglobal (L, "rspamd_version"); + if (lua_isnil (L, -1)) { + lua_pushcfunction (L, rspamd_lua_rspamd_version); + lua_setglobal (L, "rspamd_version"); + } + + if (cfg != NULL) { + pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *)); + rspamd_lua_setclass (L, "rspamd{config}", -1); + *pcfg = cfg; + lua_setglobal (L, "rspamd_config"); + } + + lua_settop (L, orig_top); + + /* Set known paths as rspamd_paths global */ + lua_getglobal (L, "rspamd_paths"); + if (lua_isnil (L, -1)) { + const gchar *confdir = RSPAMD_CONFDIR, *rundir = RSPAMD_RUNDIR, + *dbdir = RSPAMD_DBDIR, *logdir = RSPAMD_LOGDIR, + *wwwdir = RSPAMD_WWWDIR, *pluginsdir = RSPAMD_PLUGINSDIR, + *rulesdir = RSPAMD_RULESDIR, *lualibdir = RSPAMD_LUALIBDIR, + *prefix = RSPAMD_PREFIX; + const gchar *t; + + /* Try environment */ + t = getenv ("PLUGINSDIR"); + if (t) { + pluginsdir = t; + } + + t = getenv ("RULESDIR"); + if (t) { + rulesdir = t; + } + + t = getenv ("DBDIR"); + if (t) { + dbdir = t; + } + + t = getenv ("RUNDIR"); + if (t) { + rundir = t; + } + + t = getenv ("LUALIBDIR"); + if (t) { + lualibdir = t; + } + + t = getenv ("LOGDIR"); + if (t) { + logdir = t; + } + + t = getenv ("WWWDIR"); + if (t) { + wwwdir = t; + } + + t = getenv ("CONFDIR"); + if (t) { + confdir = t; + } + + + if (vars) { + t = g_hash_table_lookup (vars, "PLUGINSDIR"); + if (t) { + pluginsdir = t; + } + + t = g_hash_table_lookup (vars, "RULESDIR"); + if (t) { + rulesdir = t; + } + + t = g_hash_table_lookup (vars, "LUALIBDIR"); + if (t) { + lualibdir = t; + } + + t = g_hash_table_lookup (vars, "RUNDIR"); + if (t) { + rundir = t; + } + + t = g_hash_table_lookup (vars, "WWWDIR"); + if (t) { + wwwdir = t; + } + + t = g_hash_table_lookup (vars, "CONFDIR"); + if (t) { + confdir = t; + } + + t = g_hash_table_lookup (vars, "DBDIR"); + if (t) { + dbdir = t; + } + + t = g_hash_table_lookup (vars, "LOGDIR"); + if (t) { + logdir = t; + } + } + + lua_createtable (L, 0, 9); + + rspamd_lua_table_set (L, RSPAMD_CONFDIR_INDEX, confdir); + rspamd_lua_table_set (L, RSPAMD_RUNDIR_INDEX, rundir); + rspamd_lua_table_set (L, RSPAMD_DBDIR_INDEX, dbdir); + rspamd_lua_table_set (L, RSPAMD_LOGDIR_INDEX, logdir); + rspamd_lua_table_set (L, RSPAMD_WWWDIR_INDEX, wwwdir); + rspamd_lua_table_set (L, RSPAMD_PLUGINSDIR_INDEX, pluginsdir); + rspamd_lua_table_set (L, RSPAMD_RULESDIR_INDEX, rulesdir); + rspamd_lua_table_set (L, RSPAMD_LUALIBDIR_INDEX, lualibdir); + rspamd_lua_table_set (L, RSPAMD_PREFIX_INDEX, prefix); + + lua_setglobal (L, "rspamd_paths"); + } + + lua_settop (L, orig_top); +} + lua_State * rspamd_lua_init () { diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 457f470e9..b4a241412 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -298,6 +298,10 @@ void rspamd_lua_dumpstack (lua_State *L); void rspamd_lua_set_path (lua_State *L, const ucl_object_t *cfg_obj, GHashTable *vars); +/* Set some lua globals */ +void rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L, + GHashTable *vars); + struct memory_pool_s * rspamd_lua_check_mempool (lua_State * L, gint pos); struct rspamd_config * lua_check_config (lua_State * L, gint pos); struct rspamd_async_session* lua_check_session (lua_State * L, gint pos); |