diff --git a/build/lua.m4 b/build/lua.m4 index b6b22dcfd1..d0288acfe4 100644 --- a/build/lua.m4 +++ b/build/lua.m4 @@ -6,7 +6,7 @@ AC_DEFUN([CHECK_LUA], [dnl # Possible names for the lua library/package (pkg-config) -LUA_POSSIBLE_LIB_NAMES="lua lua53 lua5.3 lua52 lua5.2" +LUA_POSSIBLE_LIB_NAMES="lua lua53 lua5.3 lua52 lua5.2 lua51 lua5.1" # Possible extensions for the library LUA_POSSIBLE_EXTENSIONS="so so0 la sl dll dylib so.0.0.0" @@ -88,10 +88,6 @@ if test -z "${LUA_CFLAGS}"; then LUA_FOUND=-1 fi else - if test "${lua_5_1}" = 1 && test "x${LUA_MANDATORY}" == "xyes" ; then - AC_MSG_ERROR([LUA was explicitly referenced but LUA v5.1 was found and it is not currently supported on libModSecurity. LUA_VERSION: ${LUA_VERSION}]) - LUA_FOUND=-1 - fi if test -z "${LUA_MANDATORY}" || test "x${LUA_MANDATORY}" == "xno"; then LUA_FOUND=1 AC_MSG_NOTICE([using LUA ${LUA_LDADD}]) @@ -113,15 +109,6 @@ else fi fi -if test "${lua_5_1}" = 1 ; then - AC_MSG_NOTICE([LUA 5.1 was found and it is not currently supported on libModSecurity. LUA_VERSION: ${LUA_VERSION}. LUA build disabled.]) - LUA_FOUND=2 - LUA_CFLAGS= - LUA_DISPLAY= - LUA_LDADD= - LUA_LDFLAGS= -fi - AC_SUBST(LUA_FOUND) ]) # AC_DEFUN [CHECK_LUA] @@ -179,6 +166,9 @@ AC_DEFUN([CHECK_FOR_LUA_AT], [ elif test -e "${path}/include/lua5.2/lua.h"; then lua_inc_path="${path}/include/lua5.2" LUA_VERSION=502 + elif test -e "${path}/include/lua5.1/lua.h"; then + lua_inc_path="${path}/include/lua5.1" + LUA_VERSION=501 fi if test -n "${lua_lib_path}"; then diff --git a/src/engine/lua.cc b/src/engine/lua.cc index cabdbeaac8..898d7cf38b 100644 --- a/src/engine/lua.cc +++ b/src/engine/lua.cc @@ -82,7 +82,7 @@ bool Lua::load(std::string script, std::string *err) { return false; } -#ifdef WITH_LUA_5_2 +#if defined (WITH_LUA_5_2) || defined (WITH_LUA_5_1) if (lua_dump(L, Lua::blob_keeper, reinterpret_cast(&m_blob))) { #else if (lua_dump(L, Lua::blob_keeper, reinterpret_cast(&m_blob), 0)) { @@ -138,8 +138,12 @@ int Lua::run(Transaction *t) { luaL_setfuncs(L, mscLuaLib, 0); lua_setglobal(L, "m"); +#ifdef WITH_LUA_5_1 + int rc = lua_load(L, Lua::blob_reader, &m_blob, m_scriptName.c_str()); +#else int rc = lua_load(L, Lua::blob_reader, &m_blob, m_scriptName.c_str(), NULL); +#endif if (rc != LUA_OK) { std::string e; e.assign("Failed to execute lua script: " + m_scriptName + ". "); @@ -150,9 +154,11 @@ int Lua::run(Transaction *t) { case LUA_ERRMEM: e.assign("Memory error. "); break; +#ifndef WITH_LUA_5_1 case LUA_ERRGCMM: e.assign("Garbage Collector error. "); break; +#endif } e.append(lua_tostring(L, -1)); #ifndef NO_LOGS @@ -402,7 +408,11 @@ std::string Lua::applyTransformations(lua_State *L, Transaction *t, if (lua_istable(L, idx)) { const char *name = NULL; +#ifdef WITH_LUA_5_1 + int i, n = lua_objlen(L, idx); +#else int i, n = lua_rawlen(L, idx); +#endif for (i = 1; i <= n; i++) { lua_rawgeti(L, idx, i); diff --git a/src/engine/lua.h b/src/engine/lua.h index 2658317827..b0972dfc10 100644 --- a/src/engine/lua.h +++ b/src/engine/lua.h @@ -101,4 +101,29 @@ static const struct luaL_Reg mscLuaLib[] = { } // namespace engine } // namespace modsecurity +#ifdef WITH_LUA +#if defined LUA_VERSION_NUM && LUA_VERSION_NUM < 502 +/* +** Adapted from Lua 5.2.0 +*/ +#define LUA_OK 0 + +static void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) { + luaL_checkstack(L, nup + 1, "too many upvalues"); + + for (; l->name != NULL; l++) { /* fill the table with given functions */ + int i; + lua_pushstring(L, l->name); + for (i = 0; i < nup; i++) /* copy upvalues to the top */ + lua_pushvalue(L, -(nup + 1)); + lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + lua_settable(L, -(nup + 3)); + } + + lua_pop(L, nup); /* remove upvalues */ +} +#endif +#endif + + #endif // SRC_ENGINE_LUA_H_