Merge pull request #7237 from Robyt3/Server-AuthKey-Removal-Fixes

Correctly update auth keys when using `auth_remove`
This commit is contained in:
Dennis Felsing 2023-09-22 20:40:13 +00:00 committed by GitHub
commit ab7fefe7bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 11 deletions

View file

@ -65,21 +65,21 @@ int CAuthManager::AddKey(const char *pIdent, const char *pPw, int AuthLevel)
return AddKeyHash(pIdent, HashPassword(pPw, aSalt), aSalt, AuthLevel);
}
int CAuthManager::RemoveKey(int Slot)
void CAuthManager::RemoveKey(int Slot)
{
m_vKeys.erase(m_vKeys.begin() + Slot);
// Update indices of default keys
for(int &Default : m_aDefault)
{
if(Default == Slot)
{
Default = -1;
}
else if(Default == (int)m_vKeys.size())
else if(Default > Slot)
{
Default = Slot;
--Default;
}
}
return m_vKeys.size();
}
int CAuthManager::FindKey(const char *pIdent) const

View file

@ -30,7 +30,7 @@ public:
void Init();
int AddKeyHash(const char *pIdent, MD5_DIGEST Hash, const unsigned char *pSalt, int AuthLevel);
int AddKey(const char *pIdent, const char *pPw, int AuthLevel);
int RemoveKey(int Slot); // Returns the old key slot that is now in the named one.
void RemoveKey(int Slot);
int FindKey(const char *pIdent) const;
bool CheckKey(int Slot, const char *pPw) const;
int DefaultKey(int AuthLevel) const;

View file

@ -3107,16 +3107,20 @@ static int GetAuthLevel(const char *pLevel)
void CServer::AuthRemoveKey(int KeySlot)
{
int NewKeySlot = KeySlot;
int OldKeySlot = m_AuthManager.RemoveKey(KeySlot);
m_AuthManager.RemoveKey(KeySlot);
LogoutKey(KeySlot, "key removal");
// Update indices.
if(OldKeySlot != NewKeySlot)
{
for(auto &Client : m_aClients)
if(Client.m_AuthKey == OldKeySlot)
Client.m_AuthKey = NewKeySlot;
{
if(Client.m_AuthKey == KeySlot)
{
Client.m_AuthKey = -1;
}
else if(Client.m_AuthKey > KeySlot)
{
--Client.m_AuthKey;
}
}
}