Skip to content

Commit dee9898

Browse files
p0pr0ck5victorhora
authored andcommitted
Implement support for Lua 5.1
1 parent eed6b5f commit dee9898

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

build/lua.m4

+4-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ AC_DEFUN([CHECK_LUA],
66
[dnl
77
88
# Possible names for the lua library/package (pkg-config)
9-
LUA_POSSIBLE_LIB_NAMES="lua lua53 lua5.3 lua52 lua5.2"
9+
LUA_POSSIBLE_LIB_NAMES="lua lua53 lua5.3 lua52 lua5.2 lua51 lua5.1"
1010
1111
# Possible extensions for the library
1212
LUA_POSSIBLE_EXTENSIONS="so so0 la sl dll dylib so.0.0.0"
@@ -88,10 +88,6 @@ if test -z "${LUA_CFLAGS}"; then
8888
LUA_FOUND=-1
8989
fi
9090
else
91-
if test "${lua_5_1}" = 1 && test "x${LUA_MANDATORY}" == "xyes" ; then
92-
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}])
93-
LUA_FOUND=-1
94-
fi
9591
if test -z "${LUA_MANDATORY}" || test "x${LUA_MANDATORY}" == "xno"; then
9692
LUA_FOUND=1
9793
AC_MSG_NOTICE([using LUA ${LUA_LDADD}])
@@ -113,15 +109,6 @@ else
113109
fi
114110
fi
115111
116-
if test "${lua_5_1}" = 1 ; then
117-
AC_MSG_NOTICE([LUA 5.1 was found and it is not currently supported on libModSecurity. LUA_VERSION: ${LUA_VERSION}. LUA build disabled.])
118-
LUA_FOUND=2
119-
LUA_CFLAGS=
120-
LUA_DISPLAY=
121-
LUA_LDADD=
122-
LUA_LDFLAGS=
123-
fi
124-
125112
AC_SUBST(LUA_FOUND)
126113
127114
]) # AC_DEFUN [CHECK_LUA]
@@ -179,6 +166,9 @@ AC_DEFUN([CHECK_FOR_LUA_AT], [
179166
elif test -e "${path}/include/lua5.2/lua.h"; then
180167
lua_inc_path="${path}/include/lua5.2"
181168
LUA_VERSION=502
169+
elif test -e "${path}/include/lua5.1/lua.h"; then
170+
lua_inc_path="${path}/include/lua5.1"
171+
LUA_VERSION=501
182172
fi
183173
184174
if test -n "${lua_lib_path}"; then

src/engine/lua.cc

+11-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ bool Lua::load(std::string script, std::string *err) {
8282
return false;
8383
}
8484

85-
#ifdef WITH_LUA_5_2
85+
#if defined (WITH_LUA_5_2) || defined (WITH_LUA_5_1)
8686
if (lua_dump(L, Lua::blob_keeper, reinterpret_cast<void *>(&m_blob))) {
8787
#else
8888
if (lua_dump(L, Lua::blob_keeper, reinterpret_cast<void *>(&m_blob), 0)) {
@@ -138,8 +138,12 @@ int Lua::run(Transaction *t) {
138138
luaL_setfuncs(L, mscLuaLib, 0);
139139
lua_setglobal(L, "m");
140140

141+
#ifdef WITH_LUA_5_1
142+
int rc = lua_load(L, Lua::blob_reader, &m_blob, m_scriptName.c_str());
143+
#else
141144
int rc = lua_load(L, Lua::blob_reader, &m_blob, m_scriptName.c_str(),
142145
NULL);
146+
#endif
143147
if (rc != LUA_OK) {
144148
std::string e;
145149
e.assign("Failed to execute lua script: " + m_scriptName + ". ");
@@ -150,9 +154,11 @@ int Lua::run(Transaction *t) {
150154
case LUA_ERRMEM:
151155
e.assign("Memory error. ");
152156
break;
157+
#ifndef WITH_LUA_5_1
153158
case LUA_ERRGCMM:
154159
e.assign("Garbage Collector error. ");
155160
break;
161+
#endif
156162
}
157163
e.append(lua_tostring(L, -1));
158164
#ifndef NO_LOGS
@@ -402,7 +408,11 @@ std::string Lua::applyTransformations(lua_State *L, Transaction *t,
402408

403409
if (lua_istable(L, idx)) {
404410
const char *name = NULL;
411+
#ifdef WITH_LUA_5_1
412+
int i, n = lua_objlen(L, idx);
413+
#else
405414
int i, n = lua_rawlen(L, idx);
415+
#endif
406416

407417
for (i = 1; i <= n; i++) {
408418
lua_rawgeti(L, idx, i);

src/engine/lua.h

+25
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,29 @@ static const struct luaL_Reg mscLuaLib[] = {
101101
} // namespace engine
102102
} // namespace modsecurity
103103

104+
#ifdef WITH_LUA
105+
#if defined LUA_VERSION_NUM && LUA_VERSION_NUM < 502
106+
/*
107+
** Adapted from Lua 5.2.0
108+
*/
109+
#define LUA_OK 0
110+
111+
static void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) {
112+
luaL_checkstack(L, nup + 1, "too many upvalues");
113+
114+
for (; l->name != NULL; l++) { /* fill the table with given functions */
115+
int i;
116+
lua_pushstring(L, l->name);
117+
for (i = 0; i < nup; i++) /* copy upvalues to the top */
118+
lua_pushvalue(L, -(nup + 1));
119+
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
120+
lua_settable(L, -(nup + 3));
121+
}
122+
123+
lua_pop(L, nup); /* remove upvalues */
124+
}
125+
#endif
126+
#endif
127+
128+
104129
#endif // SRC_ENGINE_LUA_H_

0 commit comments

Comments
 (0)