commit e96cab2: [Rework] Another paths rework

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Dec 27 18:28:14 UTC 2018


Author: Vsevolod Stakhov
Date: 2018-12-26 14:21:47 +0000
URL: https://github.com/rspamd/rspamd/commit/e96cab20b996c1555ba0e5748057649c5ab82d8d

[Rework] Another paths rework

---
 CMakeLists.txt            | 14 +++----
 conf/common.conf          |  2 +-
 src/libserver/cfg_rcl.c   | 95 +++++++++++++++++++++++++++++++++++++++--------
 src/libserver/cfg_rcl.h   |  1 +
 src/lua/lua_common.c      |  9 +----
 src/rspamadm/confighelp.c |  2 +-
 6 files changed, 92 insertions(+), 31 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index fb907c85e..1e20c6f8d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -127,18 +127,18 @@ IF(NOT EXAMPLESDIR)
 	SET(EXAMPLESDIR "${CMAKE_INSTALL_PREFIX}/share/examples/rspamd")
 ENDIF(NOT EXAMPLESDIR)
 
+IF(NOT LUALIBDIR)
+	SET(LUALIBDIR "${SHAREDIR}/lualib")
+ENDIF(NOT LUALIBDIR)
+
 IF(NOT PLUGINSDIR)
-	SET(PLUGINSDIR "${SHAREDIR}/lualib/plugins")
+	SET(PLUGINSDIR "${SHAREDIR}/plugins")
 ENDIF(NOT PLUGINSDIR)
 
 IF(NOT RULESDIR)
 	SET(RULESDIR "${SHAREDIR}/rules")
 ENDIF(NOT RULESDIR)
 
-IF(NOT LUALIBDIR)
-	SET(LUALIBDIR "${SHAREDIR}/lualib")
-ENDIF(NOT LUALIBDIR)
-
 IF(NOT WWWDIR)
 	SET(WWWDIR "${SHAREDIR}/www")
 ENDIF(NOT WWWDIR)
@@ -1371,8 +1371,8 @@ FILE(GLOB_RECURSE LUA_LIBS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/lualib"
 		"${CMAKE_CURRENT_SOURCE_DIR}/lualib/*.lua")
 FOREACH(LUA_LIB ${LUA_LIBS})
 	GET_FILENAME_COMPONENT(_rp ${LUA_LIB} PATH)
-	INSTALL(CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}${LUALIBDIR}/rspamd/${_rp})")
-	INSTALL(FILES "lualib/${LUA_LIB}" DESTINATION ${LUALIBDIR}/rspamd/${_rp})
+	INSTALL(CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}${LUALIBDIR}/${_rp})")
+	INSTALL(FILES "lualib/${LUA_LIB}" DESTINATION ${LUALIBDIR}/${_rp})
 ENDFOREACH(LUA_LIB)
 
 # Install lua fun library
diff --git a/conf/common.conf b/conf/common.conf
index 6a537b3e5..6a7ec2670 100644
--- a/conf/common.conf
+++ b/conf/common.conf
@@ -35,5 +35,5 @@ lua = "$RULESDIR/rspamd.lua"
 
 modules {
   path = "${PLUGINSDIR}";
-  path = "${SHAREDIR}/lua"; # Legacy path
+  fallback_path = "${SHAREDIR}/lua"; # Legacy path
 }
diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c
index d6aadc03e..bb807d932 100644
--- a/src/libserver/cfg_rcl.c
+++ b/src/libserver/cfg_rcl.c
@@ -794,22 +794,31 @@ rspamd_rcl_lua_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
 gboolean
 rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg,
 		const gchar *path,
+		GHashTable *modules_seen,
 		GError **err)
 {
 	struct stat st;
-	struct script_module *cur_mod;
+	struct script_module *cur_mod, *seen_mod;
 	GPtrArray *paths;
 	gchar *fname, *ext_pos;
 	guint i;
 
 	if (stat (path, &st) == -1) {
-		g_set_error (err,
-			CFG_RCL_ERROR,
-			errno,
-			"cannot stat path %s, %s",
-			path,
-			strerror (errno));
-		return FALSE;
+
+		if (errno != ENOENT) {
+			g_set_error (err,
+					CFG_RCL_ERROR,
+					errno,
+					"cannot stat path %s, %s",
+					path,
+					strerror (errno));
+			return FALSE;
+		}
+		else {
+			msg_info_config ("plugins path %s is absent, skip it", path);
+
+			return TRUE;
+		}
 	}
 
 	/* Handle directory */
@@ -834,6 +843,16 @@ rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg,
 				*ext_pos = '\0';
 			}
 
+			if (modules_seen) {
+				seen_mod = g_hash_table_lookup (modules_seen, cur_mod->name);
+
+				if (seen_mod != NULL) {
+					msg_info_config ("already seen module %s at %s, skip %s",
+							cur_mod->name, seen_mod->path, cur_mod->path);
+					continue;
+				}
+			}
+
 			if (cfg->script_modules == NULL) {
 				cfg->script_modules = g_list_append (cfg->script_modules,
 						cur_mod);
@@ -844,6 +863,10 @@ rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg,
 				cfg->script_modules = g_list_append (cfg->script_modules,
 						cur_mod);
 			}
+
+			if (modules_seen) {
+				g_hash_table_insert (modules_seen, cur_mod->name, cur_mod);
+			}
 		}
 
 		g_ptr_array_free (paths, TRUE);
@@ -862,6 +885,17 @@ rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg,
 			*ext_pos = '\0';
 		}
 
+		if (modules_seen) {
+			seen_mod = g_hash_table_lookup (modules_seen, cur_mod->name);
+
+			if (seen_mod != NULL) {
+				msg_info_config ("already seen module %s at %s, skip %s",
+						cur_mod->name, seen_mod->path, cur_mod->path);
+
+				return TRUE;
+			}
+		}
+
 		if (cfg->script_modules == NULL) {
 			cfg->script_modules = g_list_append (cfg->script_modules,
 					cur_mod);
@@ -873,6 +907,10 @@ rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg,
 			cfg->script_modules = g_list_append (cfg->script_modules,
 					cur_mod);
 		}
+
+		if (modules_seen) {
+			g_hash_table_insert (modules_seen, cur_mod->name, cur_mod);
+		}
 	}
 
 	return TRUE;
@@ -888,21 +926,48 @@ rspamd_rcl_modules_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
 	const gchar *data;
 
 	if (obj->type == UCL_OBJECT) {
+		GHashTable *mods_seen = g_hash_table_new (rspamd_strcase_hash,
+				rspamd_strcase_equal);
 		val = ucl_object_lookup (obj, "path");
 
-		LL_FOREACH (val, cur)
-		{
-			if (ucl_object_tostring_safe (cur, &data)) {
-				if (!rspamd_rcl_add_lua_plugins_path (cfg,
-						rspamd_mempool_strdup (cfg->cfg_pool, data), err)) {
-					return FALSE;
+		if (val) {
+			LL_FOREACH (val, cur) {
+				if (ucl_object_tostring_safe (cur, &data)) {
+					if (!rspamd_rcl_add_lua_plugins_path (cfg,
+							rspamd_mempool_strdup (cfg->cfg_pool, data),
+							mods_seen,
+							err)) {
+						return FALSE;
+					}
+				}
+			}
+		}
+		else {
+			g_set_error (err,
+					CFG_RCL_ERROR,
+					EINVAL,
+					"path attribute is missing");
+			return FALSE;
+		}
+
+		val = ucl_object_lookup (obj, "fallback_path");
+
+		if (val) {
+			LL_FOREACH (val, cur) {
+				if (ucl_object_tostring_safe (cur, &data)) {
+					if (!rspamd_rcl_add_lua_plugins_path (cfg,
+							rspamd_mempool_strdup (cfg->cfg_pool, data),
+							mods_seen,
+							err)) {
+						return FALSE;
+					}
 				}
 			}
 		}
 	}
 	else if (ucl_object_tostring_safe (obj, &data)) {
 		if (!rspamd_rcl_add_lua_plugins_path (cfg,
-				rspamd_mempool_strdup (cfg->cfg_pool, data), err)) {
+				rspamd_mempool_strdup (cfg->cfg_pool, data), NULL, err)) {
 			return FALSE;
 		}
 	}
diff --git a/src/libserver/cfg_rcl.h b/src/libserver/cfg_rcl.h
index b14b98f8b..830a1bfe3 100644
--- a/src/libserver/cfg_rcl.h
+++ b/src/libserver/cfg_rcl.h
@@ -445,6 +445,7 @@ ucl_object_t *rspamd_rcl_add_doc_by_example (struct rspamd_config *cfg,
  */
 gboolean rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg,
 		const gchar *path,
+		GHashTable *modules_seen,
 		GError **err);
 
 
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c
index db1f5357e..a5ae37e2e 100644
--- a/src/lua/lua_common.c
+++ b/src/lua/lua_common.c
@@ -332,14 +332,11 @@ rspamd_lua_set_path (lua_State *L, const ucl_object_t *cfg_obj, GHashTable *vars
 				"%s/?.lua;"
 				"%s/?.lua;"
 				"%s/?/init.lua;"
-				"%s/rspamd/?.lua;"
 				"%s;"
 				"%s",
 				RSPAMD_CONFDIR,
 				rulesdir,
 				lualibdir, lualibdir,
-				/* Rspamd specific: lualib/rspamd */
-				lualibdir,
 				additional_path, old_path);
 	}
 	else {
@@ -348,12 +345,10 @@ rspamd_lua_set_path (lua_State *L, const ucl_object_t *cfg_obj, GHashTable *vars
 				"%s/?.lua;"
 				"%s/?.lua;"
 				"%s/?/init.lua;"
-				"%s/rspamd/?.lua;"
 				"%s",
-				RSPAMD_CONFDIR, rulesdir,
+				RSPAMD_CONFDIR,
+				rulesdir,
 				lualibdir, lualibdir,
-				/* Rspamd specific: lualib/rspamd */
-				lualibdir,
 				old_path);
 	}
 
diff --git a/src/rspamadm/confighelp.c b/src/rspamadm/confighelp.c
index d3461489e..a414545db 100644
--- a/src/rspamadm/confighelp.c
+++ b/src/rspamadm/confighelp.c
@@ -234,7 +234,7 @@ rspamadm_confighelp (gint argc, gchar **argv, const struct rspamadm_command *cmd
 	rspamd_rcl_config_init (cfg, NULL);
 	lua_pushboolean (cfg->lua_state, true);
 	lua_setglobal (cfg->lua_state, "confighelp");
-	rspamd_rcl_add_lua_plugins_path (cfg, plugins_path, NULL);
+	rspamd_rcl_add_lua_plugins_path (cfg, plugins_path, NULL, NULL);
 
 	/* Init modules to get documentation strings */
 	i = 0;


More information about the Commits mailing list