Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a593163
Reimplemented Lua packages in C++
darkdreamingdan Aug 12, 2016
a1d0cd7
Code cleanup
darkdreamingdan Aug 12, 2016
b8ec9e2
Added more useful error messages to require
darkdreamingdan Aug 13, 2016
90dc2cd
Lua C modules now require ACL access
darkdreamingdan Aug 13, 2016
09319f3
Added clientside support for require (only for pure lua modules)
darkdreamingdan Aug 13, 2016
c5babeb
Adjusted mtaserver.conf comments to annoy developers and users
darkdreamingdan Aug 13, 2016
eb03d0c
Small lua packages fix
darkdreamingdan Sep 26, 2016
8d74aa2
Merge branch 'master' into feature/lua-packages
qaisjp Oct 31, 2018
aae6ebf
Move duplicate package methods to CLuaMain.Shared
qaisjp Oct 31, 2018
3ed2191
Merge branch 'master' into feature/lua-packages
qaisjp Feb 20, 2019
4f299a1
Improved warnings for lua-package (#683)
CrosRoad95 Feb 20, 2019
8b72581
Merge remote-tracking branch 'upstream/master' into feature/lua-packages
patrikjuvonen Feb 22, 2019
7333c22
fix google-breakpad refresh_binaries.bat line endings
patrikjuvonen May 1, 2019
3dbd541
Merge remote-tracking branch 'upstream/master' into feature/lua-packages
patrikjuvonen May 1, 2019
22f731f
Merge remote-tracking branch 'upstream/master' into feature/lua-packages
patrikjuvonen May 12, 2019
4333626
Merge branch 'master' into feature/lua-packages
qaisjp Jun 10, 2019
f075ff6
Merge branch 'master' into feature/lua-packages
qaisjp Apr 2, 2020
cc51f28
Merge branch 'master' into feature/lua-packages
qaisjp Apr 9, 2020
3796e9e
Merge branch 'master' into feature/lua-packages
patrikjuvonen Dec 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Client/mods/deathmatch/logic/lua/CLuaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ CLuaMain::CLuaMain(CLuaManager* pLuaManager, CResource* pResourceOwner, bool bEn

m_pResource = pResourceOwner;

m_iPackageLoadedRef = -1;

m_bEnableOOP = bEnableOOP;

CClientPerfStatLuaMemory::GetSingleton()->OnLuaMainCreate(this);
Expand Down Expand Up @@ -158,6 +160,9 @@ void CLuaMain::InitVM()
luaopen_utf8(m_luaVM);
luaopen_os(m_luaVM);

// Initialize packages system
InitPackageStorage(m_luaVM);

// Initialize security restrictions. Very important to prevent lua trojans and viruses!
InitSecurity();

Expand Down Expand Up @@ -212,7 +217,8 @@ void CLuaMain::InstructionCountHook(lua_State* luaVM, lua_Debug* pDebug)
}
}

bool CLuaMain::LoadScriptFromBuffer(const char* cpInBuffer, unsigned int uiInSize, const char* szFileName)

bool CLuaMain::LoadScriptFromBuffer(const char* cpInBuffer, unsigned int uiInSize, const char* szFileName, bool bClearReturnValues)
{
SString strNiceFilename = ConformResourcePath(szFileName);

Expand Down Expand Up @@ -276,7 +282,7 @@ bool CLuaMain::LoadScriptFromBuffer(const char* cpInBuffer, unsigned int uiInSiz
g_pClientGame->GetScriptDebugging()->LogPCallError(m_luaVM, strRes, true);
}
// Cleanup any return values
if (lua_gettop(m_luaVM) > luaSavedTop)
if (bClearReturnValues && lua_gettop(m_luaVM) > luaSavedTop)
lua_settop(m_luaVM, luaSavedTop);
return true;
}
Expand Down
8 changes: 7 additions & 1 deletion Client/mods/deathmatch/logic/lua/CLuaMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CLuaMain //: public CClient
CLuaMain(class CLuaManager* pLuaManager, CResource* pResourceOwner, bool bEnableOOP);
~CLuaMain();

bool LoadScriptFromBuffer(const char* cpBuffer, unsigned int uiSize, const char* szFileName);
bool LoadScriptFromBuffer(const char* cpBuffer, unsigned int uiSize, const char* szFileName, bool bClearReturnValues = true);
bool LoadScript(const char* szLUAScript);
void UnloadScript();

Expand Down Expand Up @@ -79,6 +79,11 @@ class CLuaMain //: public CClient

bool IsOOPEnabled() { return m_bEnableOOP; }

void InitPackageStorage(lua_State* L); // Create a psuedo package.loaded table
void GetPackage(lua_State* L, SString& strName); // Push the package value to the top of the stack
void SetPackage(lua_State* L, SString& strName); // Set the package to the value at the top of the stack
bool LoadLuaLib(lua_State* L, SString strName, SString& strError, bool& bEmpty); // Load a lua library of a given name

private:
void InitSecurity();

Expand All @@ -88,6 +93,7 @@ class CLuaMain //: public CClient

lua_State* m_luaVM;
CLuaTimerManager* m_pLuaTimerManager;
int m_iPackageLoadedRef;

bool m_bBeingDeleted; // prevent it being deleted twice

Expand Down
9 changes: 9 additions & 0 deletions Server/mods/deathmatch/logic/CAccessControlListManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ bool CAccessControlListManager::Load()
{
pRight = pACL->AddRight(&szRightName[8], CAccessControlListRight::RIGHT_TYPE_GENERAL, bAccess);
}
else if (StringBeginsWith(szRightName, "module."))
{
pRight = pACL->AddRight(&szRightName[7], CAccessControlListRight::RIGHT_TYPE_MODULE, bAccess);
}
else
continue;

Expand Down Expand Up @@ -536,6 +540,11 @@ const char* CAccessControlListManager::ExtractRightName(const char* szRightName,
eType = CAccessControlListRight::RIGHT_TYPE_GENERAL;
return szRightName + 8;
}
else if ( StringBeginsWith( szRightName, "module." ) )
{
eType = CAccessControlListRight::RIGHT_TYPE_MODULE;
return szRightName + 7;
}

// Failed
return NULL;
Expand Down
3 changes: 2 additions & 1 deletion Server/mods/deathmatch/logic/CAccessControlListRight.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class CAccessControlListRight
RIGHT_TYPE_COMMAND,
RIGHT_TYPE_FUNCTION,
RIGHT_TYPE_RESOURCE,
RIGHT_TYPE_GENERAL
RIGHT_TYPE_GENERAL,
RIGHT_TYPE_MODULE
};

public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ ADD_ENUM(CAccessControlListRight::RIGHT_TYPE_COMMAND, "command")
ADD_ENUM(CAccessControlListRight::RIGHT_TYPE_FUNCTION, "function")
ADD_ENUM(CAccessControlListRight::RIGHT_TYPE_RESOURCE, "resource")
ADD_ENUM(CAccessControlListRight::RIGHT_TYPE_GENERAL, "general")
ADD_ENUM(CAccessControlListRight::RIGHT_TYPE_MODULE, "module")
IMPLEMENT_ENUM_END("right-type")

IMPLEMENT_ENUM_BEGIN(CElement::EElementType)
Expand Down
58 changes: 55 additions & 3 deletions Server/mods/deathmatch/logic/lua/CLuaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ void CLuaMain::InitSecurity()

lua_register(m_luaVM, "dofile", CLuaUtilDefs::DisabledFunction);
lua_register(m_luaVM, "loadfile", CLuaUtilDefs::DisabledFunction);
lua_register(m_luaVM, "require", CLuaUtilDefs::DisabledFunction);
lua_register(m_luaVM, "loadlib", CLuaUtilDefs::DisabledFunction);
lua_register(m_luaVM, "getfenv", CLuaUtilDefs::DisabledFunction);
lua_register(m_luaVM, "newproxy", CLuaUtilDefs::DisabledFunction);
Expand Down Expand Up @@ -187,6 +186,9 @@ void CLuaMain::InitVM()
luaopen_utf8(m_luaVM);
luaopen_os(m_luaVM);

// Initialize packages system
InitPackageStorage(m_luaVM);

// Initialize security restrictions. Very important to prevent lua trojans and viruses!
InitSecurity();

Expand Down Expand Up @@ -241,7 +243,7 @@ void CLuaMain::InstructionCountHook(lua_State* luaVM, lua_Debug* pDebug)
}
}

bool CLuaMain::LoadScriptFromBuffer(const char* cpInBuffer, unsigned int uiInSize, const char* szFileName)
bool CLuaMain::LoadScriptFromBuffer(const char* cpInBuffer, unsigned int uiInSize, const char* szFileName, bool bClearReturnValues)
{
SString strNiceFilename = ConformResourcePath(szFileName);

Expand Down Expand Up @@ -311,7 +313,7 @@ bool CLuaMain::LoadScriptFromBuffer(const char* cpInBuffer, unsigned int uiInSiz
g_pGame->GetScriptDebugging()->LogPCallError(m_luaVM, strRes, true);
}
// Cleanup any return values
if (lua_gettop(m_luaVM) > luaSavedTop)
if (bClearReturnValues && lua_gettop(m_luaVM) > luaSavedTop)
lua_settop(m_luaVM, luaSavedTop);
return true;
}
Expand Down Expand Up @@ -661,3 +663,53 @@ int CLuaMain::OnUndump(const char* p, size_t n)
}
return 1;
}

///////////////////////////////////////////////////////////////
//
// CLuaMain::LoadClib
//
// Load a C lib of a given name
//
///////////////////////////////////////////////////////////////
bool CLuaMain::LoadClib(lua_State* L, SString strName, SString& strError)
{
SString strPath = strName;
// Name format shouldn't include slashes. Subdirs are dots.
ReplaceOccurrencesInString(strPath, "\\", "");
ReplaceOccurrencesInString(strPath, "/", "");
ReplaceOccurrencesInString(strPath, ".", "/");

strPath = PathJoin(g_pServerInterface->GetModManager()->GetServerPath(), SERVER_BIN_PATH_MOD, "modules", strPath);

#ifdef WIN32
strPath += ".dll";
#else
strPath += ".so";
#endif

if (!FileExists(strPath))
{
strError += "#3: " + ConformPath(strPath, "modules/");
return false;
}

// Check ACL permissions
if (!g_pGame->GetACLManager()->CanObjectUseRight(m_pResource->GetName().c_str(), CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE, "*",
CAccessControlListRight::RIGHT_TYPE_MODULE, false) &&
!g_pGame->GetACLManager()->CanObjectUseRight(m_pResource->GetName().c_str(), CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE, strName.c_str(),
CAccessControlListRight::RIGHT_TYPE_MODULE, false))
{
strError = "could not load module '" + strName + "' from file " + ConformPath(strPath, "modules/") + ":\n\t ACL access denied. Grant \"module." +
strName + "\" right to resource " + m_pResource->GetName();
return false;
}

if (!luaL_loader_C(L, strName.c_str(), strPath.c_str()) || lua_type(L, -1) == LUA_TNIL)
{
strError = "failed to load module '" + strName + "' from file " + ConformPath(strPath, "modules/");
return false;
}

SetPackage(L, strName);
return true;
}
9 changes: 8 additions & 1 deletion Server/mods/deathmatch/logic/lua/CLuaMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CLuaMain //: public CClient

~CLuaMain();

bool LoadScriptFromBuffer(const char* cpBuffer, unsigned int uiSize, const char* szFileName);
bool LoadScriptFromBuffer(const char* cpBuffer, unsigned int uiSize, const char* szFileName, bool bClearReturnValues = true);
bool LoadScript(const char* szLUAScript);
void UnloadScript();

Expand Down Expand Up @@ -109,6 +109,12 @@ class CLuaMain //: public CClient
static int LuaLoadBuffer(lua_State* L, const char* buff, size_t sz, const char* name);
static int OnUndump(const char* p, size_t n);

void InitPackageStorage(lua_State* L); // Create a psuedo package.loaded table
void GetPackage(lua_State* L, SString& strName); // Push the package value to the top of the stack
void SetPackage(lua_State* L, SString& strName); // Set the package to the value at the top of the stack
bool LoadLuaLib(lua_State* L, SString strName, SString& strError, bool& bEmpty); // Load a lua library of a given name
bool LoadClib(lua_State* L, SString strName, SString& strError); // Load a C Lib of a given name

private:
void InitSecurity();
void InitClasses(lua_State* luaVM);
Expand All @@ -123,6 +129,7 @@ class CLuaMain //: public CClient

lua_State* m_luaVM;
CLuaTimerManager* m_pLuaTimerManager;
int m_iPackageLoadedRef;

class CResource* m_pResource;
class CResourceFile* m_pResourceFile;
Expand Down
Loading