diff options
-rw-r--r-- | CMakeLists.txt | 26 | ||||
-rw-r--r-- | config.h.in | 2 | ||||
-rw-r--r-- | src/cfg_file.y | 2 | ||||
-rw-r--r-- | src/filter.c | 18 | ||||
-rw-r--r-- | src/lua-rspamd.h | 21 | ||||
-rw-r--r-- | src/lua.c | 171 | ||||
-rw-r--r-- | src/main.c | 9 |
7 files changed, 246 insertions, 3 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index c1c807382..536d27cc6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0 FATAL_ERROR) OPTION(DEBUG_MODE "Enable debug output [default: ON]" ON) OPTION(ENABLE_OPTIMIZATION "Enable optimization [default: OFF]" OFF) OPTION(ENABLE_PERL "Enable perl support [default: ON]" ON) +OPTION(ENABLE_LUA "Enable lua support [default: OFF]" OFF) OPTION(SKIP_RELINK_RPATH "Skip relinking and full RPATH for the install tree" OFF) OPTION(ENABLE_REDIRECTOR "Enable redirector install [default: OFF]" OFF) @@ -61,6 +62,25 @@ ELSE(ENABLE_PERL MATCHES "ON") SET(WITHOUT_PERL 1) ENDIF(ENABLE_PERL MATCHES "ON") +IF(ENABLE_LUA MATCHES "ON") + IF (ENABLE_PERL MATCHES "ON") + MESSAGE(FATAL_ERROR "Error: Perl and Lua support cannot be turned on together") + ENDIF (ENABLE_PERL MATCHES "ON") + + INCLUDE(FindLua51) + IF(NOT LUA_FOUND) + # Automatic check failed, check passed variable + IF(LUA_INCLUDE_DIR) + INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}") + ELSE(LUA_INCLUDE_DIR) + MESSAGE(FATAL_ERROR "Error: Lua not found but its support is enabled") + ENDIF(LUA_INCLUDE_DIR) + ELSE(NOT LUA_FOUND) + INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}") + ENDIF(NOT LUA_FOUND) + SET(WITH_LUA 1) +ENDIF(ENABLE_LUA MATCHES "ON") + # Lex and yacc FIND_PROGRAM(LEX_EXECUTABLE lex) FIND_PROGRAM(YACC_EXECUTABLE yacc) @@ -276,6 +296,9 @@ SET(RSPAMDSRC src/modules.c IF(ENABLE_PERL MATCHES "ON") LIST(APPEND RSPAMDSRC src/perl.c) ENDIF(ENABLE_PERL MATCHES "ON") +IF(ENABLE_LUA MATCHES "ON") + LIST(APPEND RSPAMDSRC src/lua.c) +ENDIF(ENABLE_LUA MATCHES "ON") SET(TOKENIZERSSRC src/tokenizers/tokenizers.c src/tokenizers/osb.c) @@ -366,6 +389,9 @@ ENDIF(LIBUTIL_LIBRARY) TARGET_LINK_LIBRARIES(rspamd event) TARGET_LINK_LIBRARIES(rspamd ${GLIB2_LIBRARIES}) TARGET_LINK_LIBRARIES(rspamd ${GMIME2_LIBRARIES}) +IF(ENABLE_LUA MATCHES "ON") + TARGET_LINK_LIBRARIES(rspamd "${LUA_LIBRARY}") +ENDIF(ENABLE_LUA MATCHES "ON") ADD_EXECUTABLE(test/rspamd-test ${TESTDEPENDS} ${CONTRIBSRC} ${TESTSRC}) SET_TARGET_PROPERTIES(test/rspamd-test PROPERTIES LINKER_LANGUAGE C) diff --git a/config.h.in b/config.h.in index 5aee03618..6d07ba8d3 100644 --- a/config.h.in +++ b/config.h.in @@ -96,6 +96,8 @@ #cmakedefine WITHOUT_PERL 1 +#cmakedefine WITH_LUA 1 + #define RVERSION "${RSPAMD_VERSION}" #define RSPAMD_MASTER_SITE_URL "${RSPAMD_MASTER_SITE_URL}" diff --git a/src/cfg_file.y b/src/cfg_file.y index a12e047c4..5cd98e9fc 100644 --- a/src/cfg_file.y +++ b/src/cfg_file.y @@ -385,7 +385,7 @@ requirebody: requirecmd: MODULE EQSIGN QUOTEDSTRING { -#ifndef WITHOUT_PERL +#if !defined(WITHOUT_PERL) || defined(WITH_LUA) struct stat st; struct perl_module *cur; if (stat ($3, &st) == -1) { diff --git a/src/filter.c b/src/filter.c index 801b2f8bb..a1679d499 100644 --- a/src/filter.c +++ b/src/filter.c @@ -40,6 +40,9 @@ #ifndef WITHOUT_PERL #include "perl.h" #endif +#ifdef WITH_LUA +#include "lua-rspamd.h" +#endif void insert_result (struct worker_task *task, const char *metric_name, const char *symbol, double flag, GList *opts) @@ -186,6 +189,21 @@ call_filter_by_name (struct worker_task *task, const char *name, enum filter_typ perl_call_message_filter (name, task); break; } +#elif defined(WITH_LUA) + switch (sc_type) { + case SCRIPT_HEADER: + lua_call_header_filter (name, task); + break; + case SCRIPT_MIME: + lua_call_mime_filter (name, task); + break; + case SCRIPT_URL: + lua_call_url_filter (name, task); + break; + case SCRIPT_MESSAGE: + lua_call_message_filter (name, task); + break; + } #else msg_err ("call_filter_by_name: trying to call perl function while perl support is disabled %s", name); #endif diff --git a/src/lua-rspamd.h b/src/lua-rspamd.h new file mode 100644 index 000000000..a99a121a3 --- /dev/null +++ b/src/lua-rspamd.h @@ -0,0 +1,21 @@ +#ifndef RSPAMD_LUA_H +#define RSPAMD_LUA_H + +#include "config.h" +#include <lua.h> +#include <lauxlib.h> +#include <lualib.h> + +struct uri; +struct worker_task; +struct config_file; + +void init_lua_filters (struct config_file *cfg); + +int lua_call_header_filter (const char *function, struct worker_task *task); +int lua_call_mime_filter (const char *function, struct worker_task *task); +int lua_call_message_filter (const char *function, struct worker_task *task); +int lua_call_url_filter (const char *function, struct worker_task *task); +int lua_call_chain_filter (const char *function, struct worker_task *task, int *marks, unsigned int number); + +#endif diff --git a/src/lua.c b/src/lua.c new file mode 100644 index 000000000..e92b4e999 --- /dev/null +++ b/src/lua.c @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2009, Rambler media + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY Rambler media ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Rambler BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "url.h" +#include "main.h" +#include "lua-rspamd.h" +#include "cfg_file.h" + +/* Lua module init function */ +#define MODULE_INIT_FUNC "module_init" + +lua_State *L = NULL; + +void +init_lua_filters (struct config_file *cfg) +{ + struct perl_module *module; + char *init_func; + size_t funclen; + + L = lua_open (); + luaL_openlibs (L); + + LIST_FOREACH (module, &cfg->perl_modules, next) { + if (module->path) { + luaL_loadfile (L, module->path); + + /* Call module init function */ + funclen = strlen (module->path) + sizeof ("::") + sizeof (MODULE_INIT_FUNC) - 1; + init_func = g_malloc (funclen); + snprintf (init_func, funclen, "%s::%s", module->path, MODULE_INIT_FUNC); + lua_getglobal (L, init_func); + lua_pushlightuserdata (L, cfg); + /* do the call (1 arguments, 1 result) */ + if (lua_pcall (L, 1, 1, 0) != 0) { + msg_info ("lua_init_filters: call to %s failed", init_func); + } + } + } +} + + +int +lua_call_header_filter (const char *function, struct worker_task *task) +{ + int result; + + lua_getglobal (L, function); + lua_pushlightuserdata (L, task); + + if (lua_pcall (L, 1, 1, 0) != 0) { + msg_info ("lua_init_filters: call to %s failed", function); + } + + /* retrieve result */ + if (!lua_isnumber (L, -1)) { + msg_info ("lua_call_header_filter: function %s must return a number", function); + } + result = lua_tonumber (L, -1); + lua_pop (L, 1); /* pop returned value */ + return result; +} + +int +lua_call_mime_filter (const char *function, struct worker_task *task) +{ + int result; + + lua_getglobal (L, function); + lua_pushlightuserdata (L, task); + + if (lua_pcall (L, 1, 1, 0) != 0) { + msg_info ("lua_init_filters: call to %s failed", function); + } + + /* retrieve result */ + if (!lua_isnumber (L, -1)) { + msg_info ("lua_call_header_filter: function %s must return a number", function); + } + result = lua_tonumber (L, -1); + lua_pop (L, 1); /* pop returned value */ + return result; +} + +int +lua_call_message_filter (const char *function, struct worker_task *task) +{ + int result; + + lua_getglobal (L, function); + lua_pushlightuserdata (L, task); + + if (lua_pcall (L, 1, 1, 0) != 0) { + msg_info ("lua_init_filters: call to %s failed", function); + } + + /* retrieve result */ + if (!lua_isnumber (L, -1)) { + msg_info ("lua_call_header_filter: function %s must return a number", function); + } + result = lua_tonumber (L, -1); + lua_pop (L, 1); /* pop returned value */ + return result; +} + +int +lua_call_url_filter (const char *function, struct worker_task *task) +{ + int result; + + lua_getglobal (L, function); + lua_pushlightuserdata (L, task); + + if (lua_pcall (L, 1, 1, 0) != 0) { + msg_info ("lua_init_filters: call to %s failed", function); + } + + /* retrieve result */ + if (!lua_isnumber (L, -1)) { + msg_info ("lua_call_header_filter: function %s must return a number", function); + } + result = lua_tonumber (L, -1); + lua_pop (L, 1); /* pop returned value */ + return result; +} + +int +lua_call_chain_filter (const char *function, struct worker_task *task, int *marks, unsigned int number) +{ + int result, i; + + lua_getglobal (L, function); + + for (i = 0; i < number; i ++) { + lua_pushnumber (L, marks[i]); + } + if (lua_pcall (L, number, 1, 0) != 0) { + msg_info ("lua_init_filters: call to %s failed", function); + } + + /* retrieve result */ + if (!lua_isnumber (L, -1)) { + msg_info ("lua_call_header_filter: function %s must return a number", function); + } + result = lua_tonumber (L, -1); + lua_pop (L, 1); /* pop returned value */ + return result; +} + diff --git a/src/main.c b/src/main.c index bbe24dd1b..7e78fce9a 100644 --- a/src/main.c +++ b/src/main.c @@ -30,6 +30,8 @@ #ifndef WITHOUT_PERL #include "perl.h" +#elif defined(WITH_LUA) +#include "lua-rspamd.h" #endif /* 2 seconds to fork new process in place of dead one */ @@ -435,9 +437,9 @@ main (int argc, char **argv, char **env) cfg->log_fd = STDERR_FILENO; g_log_set_default_handler (file_log_function, cfg); - #ifndef HAVE_SETPROCTITLE +#ifndef HAVE_SETPROCTITLE init_title (argc, argv, environ); - #endif +#endif f = fopen (rspamd->cfg->cfg_name , "r"); if (f == NULL) { @@ -541,6 +543,9 @@ main (int argc, char **argv, char **env) PERL_SET_CONTEXT (perl_interpreter); perl_construct (perl_interpreter); perl_parse (perl_interpreter, xs_init, 3, args, NULL); + init_perl_filters (cfg); +#elif defined(WITH_LUA) + init_lua_filters (cfg); #endif /* Block signals to use sigsuspend in future */ |