Makes lua optional

This commit is contained in:
Felipe Zimmerle
2017-11-06 00:00:38 -03:00
parent e52bd7d635
commit 7fa5ca9ba0
6 changed files with 55 additions and 17 deletions

View File

@@ -41,6 +41,7 @@ namespace engine {
bool Lua::isCompatible(std::string script, Lua *l, std::string *error) {
#ifdef WITH_LUA
std::string lua(".lua");
std::string err;
@@ -57,10 +58,15 @@ bool Lua::isCompatible(std::string script, Lua *l, std::string *error) {
}
return true;
#else
error->assign("Lua support was not enabled.");
return false;
#endif
}
bool Lua::load(std::string script, std::string *err) {
#ifdef WITH_LUA
lua_State *L = NULL;
L = luaL_newstate();
luaL_openlibs(L);
@@ -93,9 +99,13 @@ bool Lua::load(std::string script, std::string *err) {
lua_close(L);
return true;
#else
err->assign("Lua support was not enabled.");
return false;
#endif
}
#ifdef WITH_LUA
int Lua::blob_keeper(lua_State *L, const void *p, size_t sz, void *ud) {
LuaScriptBlob *lsb = static_cast<LuaScriptBlob *>(ud);
lsb->write(p, sz);
@@ -108,9 +118,10 @@ const char *Lua::blob_reader(lua_State *L, void *ud, size_t *size) {
const char *data = lsb->read(size);
return data;
}
#endif
int Lua::run(Transaction *t) {
#ifdef WITH_LUA
std::string luaRet;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
@@ -188,9 +199,14 @@ int Lua::run(Transaction *t) {
}
return true;
#else
t->debug(9, "Lua support was not enabled.");
return false;
#endif
}
#ifdef WITH_LUA
int Lua::log(lua_State *L) {
Transaction *t = NULL;
const char *text;
@@ -371,7 +387,7 @@ std::string Lua::applyTransformations(lua_State *L, Transaction *t, int idx, std
return newVar;
}
#endif
} // namespace engines
} // namespace modsecurity

View File

@@ -14,8 +14,8 @@
*/
#ifdef WITH_LUA
#endif
#include <lua.hpp>
#endif
#include <iostream>
#include <cstdint>
@@ -29,6 +29,7 @@ namespace modsecurity {
class Transaction;
namespace engine {
#ifdef WITH_LUA
class LuaScriptBlob {
public:
LuaScriptBlob() :
@@ -61,7 +62,7 @@ class LuaScriptBlob {
unsigned char *m_data;
size_t m_len;
};
#endif
class Lua {
public:
@@ -71,6 +72,7 @@ class Lua {
int run(Transaction *t);
static bool isCompatible(std::string script, Lua *l, std::string *error);
#ifdef WITH_LUA
static int blob_keeper(lua_State *L, const void *p, size_t sz, void *ud);
static const char *blob_reader(lua_State *L, void *us, size_t *size);
@@ -82,10 +84,11 @@ class Lua {
std::string var);
LuaScriptBlob m_blob;
#endif
std::string m_scriptName;
};
#ifdef WITH_LUA
static const struct luaL_Reg mscLuaLib[] = {
{ "log", Lua::log },
{ "getvar", Lua::getvar },
@@ -93,7 +96,7 @@ static const struct luaL_Reg mscLuaLib[] = {
{ "setvar", Lua::setvar },
{ NULL, NULL }
};
#endif
} // namespace engines
} // namespace modsecurity