Skip to content

Commit 2465c4d

Browse files
Synchronize changes from 1.6 master branch [ci skip]
a3f6d01 Re-introduce "Read file from path - #3490 appendum (#3620)"
2 parents 8dc53ac + a3f6d01 commit 2465c4d

File tree

8 files changed

+294
-68
lines changed

8 files changed

+294
-68
lines changed

Client/mods/deathmatch/logic/CScriptFile.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ long CScriptFile::Read(unsigned long ulSize, SString& outBuffer)
158158
DoResourceFileCheck();
159159

160160
// If read size is large, limit it to how many bytes can be read (avoid memory problems with over allocation)
161-
if (ulSize > 10000)
161+
// large : >10KB
162+
if (ulSize > 10240)
162163
{
163164
long lCurrentPos = m_pFile->FTell();
164165
m_pFile->FSeek(0, SEEK_END);
@@ -177,7 +178,18 @@ long CScriptFile::Read(unsigned long ulSize, SString& outBuffer)
177178
return -2;
178179
}
179180

180-
return m_pFile->FRead(outBuffer.data(), ulSize);
181+
auto bytesRead = m_pFile->FRead(outBuffer.data(), ulSize);
182+
183+
// EOF reached?
184+
// Cant check for error as binary interface
185+
// is pure virtual class with no definitions
186+
// available (CNetServer)
187+
if (m_pFile->FEof())
188+
{
189+
// if so, truncate the data to the amount of bytes read
190+
outBuffer.resize(bytesRead + 1);
191+
}
192+
return bytesRead;
181193
}
182194

183195
long CScriptFile::Write(unsigned long ulSize, const char* pData)

Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,11 @@ void CheckCanModifyOtherResource(CScriptArgReader& argStream, CResource* pThisRe
12681268
// No operation on the client
12691269
}
12701270

1271+
std::pair<bool, std::string> CheckCanModifyOtherResource(CResource* pThisResource, CResource* pOtherResource) noexcept
1272+
{
1273+
return {true, ""};
1274+
}
1275+
12711276
//
12721277
// Set error if pThisResource does not have permission to modify every resource in resourceList
12731278
//
@@ -1276,6 +1281,11 @@ void CheckCanModifyOtherResources(CScriptArgReader& argStream, CResource* pThisR
12761281
// No operation on the client
12771282
}
12781283

1284+
std::pair<bool, std::string> CheckCanModifyOtherResources(CResource* pThisResource, std::initializer_list<CResource*> resourceList) noexcept
1285+
{
1286+
return {true, ""};
1287+
}
1288+
12791289
//
12801290
// Set error if resource file access is blocked due to reasons
12811291
//
@@ -1284,3 +1294,8 @@ void CheckCanAccessOtherResourceFile(CScriptArgReader& argStream, CResource* pTh
12841294
{
12851295
// No operation on the client
12861296
}
1297+
1298+
std::pair<bool, std::string> CheckCanAccessOtherResourceFile(CResource* pThisResource, CResource* pOtherResource, const SString& strAbsPath, bool* pbReadOnly) noexcept
1299+
{
1300+
return {true, ""};
1301+
}

Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,13 @@ void ReadPregFlags(CScriptArgReader& argStream, pcrecpp::RE_Options& pOptions);
590590
// Resource access helpers
591591
//
592592
void CheckCanModifyOtherResource(CScriptArgReader& argStream, CResource* pThisResource, CResource* pOtherResource);
593+
std::pair<bool, std::string> CheckCanModifyOtherResource(CResource* pThisResource, CResource* pOtherResource) noexcept;
593594
void CheckCanModifyOtherResources(CScriptArgReader& argStream, CResource* pThisResource, std::initializer_list<CResource*> resourceList);
594-
void CheckCanAccessOtherResourceFile(CScriptArgReader& argStream, CResource* pThisResource, CResource* pOtherResource, const SString& strAbsPath,
595+
std::pair<bool, std::string> CheckCanModifyOtherResources(CResource* pThisResource, std::initializer_list<CResource*> resourceList) noexcept;
596+
void CheckCanAccessOtherResourceFile(CScriptArgReader& argStream, CResource* pThisResource, CResource* pOtherResource, const SString& strAbsPath,
595597
bool* pbReadOnly = nullptr);
598+
std::pair<bool, std::string> CheckCanAccessOtherResourceFile(CResource* pThisResource, CResource* pOtherResource, const SString& strAbsPath,
599+
bool* pbReadOnly = nullptr) noexcept;
596600

597601
//
598602
// Other misc helpers

Server/mods/deathmatch/logic/CScriptFile.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ long CScriptFile::Read(unsigned long ulSize, SString& outBuffer)
178178
return -1;
179179

180180
// If read size is large, limit it to how many bytes can be read (avoid memory problems with over allocation)
181-
if (ulSize > 10000)
181+
// large : >10KB
182+
if (ulSize > 10240)
182183
{
183184
long lCurrentPos = ftell(m_pFile);
184185
fseek(m_pFile, 0, SEEK_END);
@@ -197,7 +198,15 @@ long CScriptFile::Read(unsigned long ulSize, SString& outBuffer)
197198
return -2;
198199
}
199200

200-
return fread(outBuffer.data(), 1, ulSize, m_pFile);
201+
auto bytesRead = fread(outBuffer.data(), 1, ulSize, m_pFile);
202+
203+
// EOF reached or error was thrown?
204+
if (feof(m_pFile) || ferror(m_pFile))
205+
{
206+
// if so, truncate the data to the amount of bytes read
207+
outBuffer.resize(bytesRead + 1);
208+
}
209+
return bytesRead;
201210
}
202211

203212
long CScriptFile::Write(unsigned long ulSize, const char* pData)

Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,17 @@ void CheckCanModifyOtherResource(CScriptArgReader& argStream, CResource* pThisRe
746746
"Access denied");
747747
}
748748

749+
std::pair<bool, SString> CheckCanModifyOtherResource(CResource* pThisResource, CResource* pOtherResource) noexcept
750+
{
751+
if (GetResourceModifyScope(pThisResource, pOtherResource) != eResourceModifyScope::NONE)
752+
return {true, ""};
753+
754+
SString str("ModifyOtherObjects in ACL denied resource %s to access %s",
755+
pThisResource->GetName().c_str(), pOtherResource->GetName().c_str()
756+
);
757+
return {false, str};
758+
}
759+
749760
//
750761
// Set error if pThisResource does not have permission to modify every resource in resourceList
751762
//
@@ -787,6 +798,46 @@ void CheckCanModifyOtherResources(CScriptArgReader& argStream, CResource* pThisR
787798
SString("ModifyOtherObjects in ACL denied resource %s to access %s", pThisResource->GetName().c_str(), ssResourceNames.str().c_str()), "Access denied");
788799
}
789800

801+
std::pair<bool, SString> CheckCanModifyOtherResources(CResource* pThisResource, std::initializer_list<CResource*> resourceList) noexcept
802+
{
803+
// std::unordered_set only allows unique values and resourceList can contain duplicates
804+
std::unordered_set<CResource*> setNoPermissionResources;
805+
806+
for (const auto& pOtherResource : resourceList)
807+
{
808+
eResourceModifyScope modifyScope = GetResourceModifyScope(pThisResource, pOtherResource);
809+
810+
if (modifyScope == eResourceModifyScope::SINGLE_RESOURCE)
811+
continue;
812+
813+
if (modifyScope == eResourceModifyScope::EVERY_RESOURCE)
814+
return {true, ""};
815+
816+
setNoPermissionResources.emplace(pOtherResource);
817+
}
818+
819+
if (setNoPermissionResources.empty())
820+
return {true, ""};
821+
822+
std::stringstream ssResourceNames;
823+
std::size_t remainingElements = setNoPermissionResources.size();
824+
825+
for (const auto& pResource : setNoPermissionResources)
826+
{
827+
ssResourceNames << pResource->GetName();
828+
829+
if (remainingElements > 1)
830+
ssResourceNames << ", ";
831+
832+
--remainingElements;
833+
}
834+
835+
SString str("ModifyOtherObjects in ACL denied resource %s to access %s",
836+
pThisResource->GetName().c_str(), ssResourceNames.str().c_str()
837+
);
838+
return {false, str};
839+
}
840+
790841
//
791842
// Set error if resource file access is blocked due to reasons
792843
//
@@ -813,3 +864,28 @@ void CheckCanAccessOtherResourceFile(CScriptArgReader& argStream, CResource* pTh
813864
SString("Database credentials protection denied resource %s to access %s", *pThisResource->GetName(), *pOtherResource->GetName()), "Access denied");
814865
}
815866
}
867+
868+
std::pair<bool, SString> CheckCanAccessOtherResourceFile(CResource* pThisResource, CResource* pOtherResource, const SString& strAbsPath, bool* pbReadOnly) noexcept
869+
{
870+
if (!g_pGame->GetConfig()->IsDatabaseCredentialsProtectionEnabled())
871+
return {true, ""};
872+
873+
// Is other resource different and requested access denied
874+
if (pThisResource == pOtherResource)
875+
return {true, ""};
876+
877+
if (!pOtherResource->IsFileDbConnectMysqlProtected(strAbsPath, pbReadOnly ? *pbReadOnly : false))
878+
return {true, ""};
879+
880+
// No access - See if we can change to readonly
881+
if (pbReadOnly && !(*pbReadOnly) && !pOtherResource->IsFileDbConnectMysqlProtected(strAbsPath, true)) {
882+
// Yes readonly access
883+
*pbReadOnly = true;
884+
return {true, ""};
885+
}
886+
887+
SString str("Database credentials protection denied resource %s to access %s",
888+
*pThisResource->GetName(), *pOtherResource->GetName()
889+
);
890+
return {false, str};
891+
}

Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,12 @@ enum class eResourceModifyScope
403403

404404
eResourceModifyScope GetResourceModifyScope(CResource* pThisResource, CResource* pOtherResource);
405405
void CheckCanModifyOtherResource(CScriptArgReader& argStream, CResource* pThisResource, CResource* pOtherResource);
406-
void CheckCanModifyOtherResources(CScriptArgReader& argStream, CResource* pThisResource, std::initializer_list<CResource*> resourceList);
406+
std::pair<bool, SString> CheckCanModifyOtherResource(CResource* pThisResource, CResource* pOtherResource) noexcept;
407+
void CheckCanModifyOtherResources(CScriptArgReader& argStream, CResource* pThisResource, std::initializer_list<CResource*> resourceList);
408+
std::pair<bool, SString> CheckCanModifyOtherResources(CResource* pThisResource, std::initializer_list<CResource*> resourceList) noexcept;
407409
void CheckCanAccessOtherResourceFile(CScriptArgReader& argStream, CResource* pThisResource, CResource* pOtherResource, const SString& strAbsPath,
408410
bool* pbReadOnly = nullptr);
411+
std::pair<bool, SString> CheckCanAccessOtherResourceFile(CResource* pThisResource, CResource* pOtherResource, const SString& strAbsPath, bool* pbReadOnly = nullptr) noexcept;
409412

410413
//
411414
// Other misc helpers

0 commit comments

Comments
 (0)