3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
35 #include "AdminCache.h"
37 #include "ForwardSys.h"
38 #include "PlayerManager.h"
39 #include "ConCmdManager.h"
40 #include "TextParsers.h"
42 #include "sourcemod.h"
43 #include "sm_stringutil.h"
45 #define LEVEL_STATE_NONE 0
46 #define LEVEL_STATE_LEVELS 1
47 #define LEVEL_STATE_FLAGS 2
50 AdminFlag g_FlagLetters[26];
54 AdminFlag g_DefaultFlags[26] =
56 Admin_Reservation, Admin_Generic, Admin_Kick, Admin_Ban, Admin_Unban,
57 Admin_Slay, Admin_Changemap, Admin_Convars, Admin_Config, Admin_Chat,
58 Admin_Vote, Admin_Password, Admin_RCON, Admin_Cheats, Admin_Custom1,
59 Admin_Custom2, Admin_Custom3, Admin_Custom4, Admin_Custom5, Admin_Custom6,
60 Admin_Generic, Admin_Generic, Admin_Generic, Admin_Generic, Admin_Generic,
65 class FlagReader : public ITextListener_SMC
72 memcpy(g_FlagLetters, g_DefaultFlags, sizeof(AdminFlag) * 26);
73 for (unsigned int i=0; i<20; i++)
83 unsigned int line = 0;
87 m_bFileNameLogged = false;
88 g_SourceMod.BuildPath(Path_SM, m_File, sizeof(m_File), "configs/admin_levels.cfg");
90 if ((error = g_TextParser.ParseFile_SMC(m_File, this, &line, NULL))
93 const char *err_string = g_TextParser.GetSMCErrorString(error);
96 err_string = "Unknown error";
98 ParseError("Error %d (%s)", error, err_string);
104 void ReadSMC_ParseStart()
106 m_LevelState = LEVEL_STATE_NONE;
108 memset(g_FlagSet, 0, sizeof(g_FlagSet));
110 SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes)
115 return SMCParse_Continue;
118 if (m_LevelState == LEVEL_STATE_NONE)
120 if (strcmp(name, "Levels") == 0)
122 m_LevelState = LEVEL_STATE_LEVELS;
126 } else if (m_LevelState == LEVEL_STATE_LEVELS) {
127 if (strcmp(name, "Flags") == 0)
129 m_LevelState = LEVEL_STATE_FLAGS;
137 return SMCParse_Continue;
139 SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes)
141 if (m_LevelState != LEVEL_STATE_FLAGS || m_IgnoreLevel)
143 return SMCParse_Continue;
146 unsigned char c = (unsigned)value[0];
148 if (c < (unsigned)'a' || c > (unsigned)'z')
150 ParseError("Flag \"%c\" is not a lower-case ASCII letter", c);
151 return SMCParse_Continue;
156 assert(c >= 0 && c < 26);
158 if (!g_Admins.FindFlag(key, &g_FlagLetters[c]))
160 ParseError("Unrecognized admin level \"%s\"", key);
161 return SMCParse_Continue;
166 return SMCParse_Continue;
168 SMCParseResult ReadSMC_LeavingSection()
173 return SMCParse_Continue;
176 if (m_LevelState == LEVEL_STATE_FLAGS)
178 m_LevelState = LEVEL_STATE_LEVELS;
179 return SMCParse_Halt;
180 } else if (m_LevelState == LEVEL_STATE_LEVELS) {
181 m_LevelState = LEVEL_STATE_NONE;
184 return SMCParse_Continue;
186 SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline)
189 return SMCParse_Continue;
191 void ParseError(const char *message, ...)
196 va_start(ap, message);
197 UTIL_FormatArgs(buffer, sizeof(buffer), message, ap);
200 if (!m_bFileNameLogged)
202 g_Logger.LogError("[SM] Parse error(s) detected in file \"%s\":", m_File);
203 m_bFileNameLogged = true;
206 g_Logger.LogError("[SM] (Line %d): %s", m_Line, buffer);
209 bool m_bFileNameLogged;
210 char m_File[PLATFORM_MAX_PATH];
216 AdminCache::AdminCache()
218 m_pCmdOverrides = sm_trie_create();
219 m_pCmdGrpOverrides = sm_trie_create();
220 m_pStrings = new BaseStringTable(1024);
221 m_pMemory = m_pStrings->GetMemTable();
222 m_FreeGroupList = m_FirstGroup = m_LastGroup = INVALID_GROUP_ID;
223 m_FreeUserList = m_FirstUser = m_LastUser = INVALID_ADMIN_ID;
224 m_pGroups = sm_trie_create();
227 m_pAuthTables = sm_trie_create();
228 m_InvalidatingAdmins = false;
229 m_destroying = false;
230 m_pLevelNames = sm_trie_create();
233 AdminCache::~AdminCache()
236 DumpAdminCache(AdminCache_Overrides, false);
237 DumpAdminCache(AdminCache_Groups, false);
239 sm_trie_destroy(m_pCmdGrpOverrides);
240 sm_trie_destroy(m_pCmdOverrides);
244 sm_trie_destroy(m_pGroups);
247 List<AuthMethod>::iterator iter;
248 for (iter=m_AuthMethods.begin();
249 iter!=m_AuthMethods.end();
252 sm_trie_destroy((*iter).table);
255 sm_trie_destroy(m_pAuthTables);
259 sm_trie_destroy(m_pLevelNames);
262 void AdminCache::OnSourceModStartup(bool late)
264 RegisterAuthIdentType("steam");
265 RegisterAuthIdentType("name");
266 RegisterAuthIdentType("ip");
268 NameFlag("reservation", Admin_Reservation);
269 NameFlag("kick", Admin_Kick);
270 NameFlag("generic", Admin_Generic);
271 NameFlag("ban", Admin_Ban);
272 NameFlag("unban", Admin_Unban);
273 NameFlag("slay", Admin_Slay);
274 NameFlag("changemap", Admin_Changemap);
275 NameFlag("cvars", Admin_Convars);
276 NameFlag("config", Admin_Config);
277 NameFlag("chat", Admin_Chat);
278 NameFlag("vote", Admin_Vote);
279 NameFlag("password", Admin_Password);
280 NameFlag("rcon", Admin_RCON);
281 NameFlag("cheats", Admin_Cheats);
282 NameFlag("root", Admin_Root);
283 NameFlag("custom1", Admin_Custom1);
284 NameFlag("custom2", Admin_Custom2);
285 NameFlag("custom3", Admin_Custom3);
286 NameFlag("custom4", Admin_Custom4);
287 NameFlag("custom5", Admin_Custom5);
288 NameFlag("custom6", Admin_Custom6);
291 void AdminCache::OnSourceModAllInitialized()
293 m_pCacheFwd = g_Forwards.CreateForward("OnRebuildAdminCache", ET_Ignore, 1, NULL, Param_Cell);
294 g_ShareSys.AddInterface(NULL, this);
297 void AdminCache::OnSourceModLevelChange(const char *mapName)
299 /* For now, we only read these once per level. */
300 s_FlagReader.LoadLevels();
303 void AdminCache::OnSourceModShutdown()
305 g_Forwards.ReleaseForward(m_pCacheFwd);
309 void AdminCache::OnSourceModPluginsLoaded()
311 DumpAdminCache(AdminCache_Overrides, true);
312 DumpAdminCache(AdminCache_Groups, true);
315 void AdminCache::NameFlag(const char *str, AdminFlag flag)
317 sm_trie_insert(m_pLevelNames, str, (void *)flag);
320 bool AdminCache::FindFlag(const char *str, AdminFlag *pFlag)
323 if (!sm_trie_retrieve(m_pLevelNames, str, &obj))
330 *pFlag = (AdminFlag)(int)obj;
336 void AdminCache::AddCommandOverride(const char *cmd, OverrideType type, FlagBits flags)
339 if (type == Override_Command)
341 pTrie = m_pCmdOverrides;
342 } else if (type == Override_CommandGroup) {
343 pTrie = m_pCmdGrpOverrides;
348 sm_trie_insert(pTrie, cmd, (void *)(unsigned int)flags);
350 g_ConCmds.UpdateAdminCmdFlags(cmd, type, flags);
353 bool AdminCache::GetCommandOverride(const char *cmd, OverrideType type, FlagBits *pFlags)
357 if (type == Override_Command)
359 pTrie = m_pCmdOverrides;
360 } else if (type == Override_CommandGroup) {
361 pTrie = m_pCmdGrpOverrides;
367 if (sm_trie_retrieve(pTrie, cmd, &object))
371 *pFlags = (FlagBits)object;
379 void AdminCache::UnsetCommandOverride(const char *cmd, OverrideType type)
381 if (type == Override_Command)
383 return _UnsetCommandOverride(cmd);
384 } else if (type == Override_CommandGroup) {
385 return _UnsetCommandGroupOverride(cmd);
389 void AdminCache::_UnsetCommandGroupOverride(const char *group)
391 if (!m_pCmdGrpOverrides)
396 sm_trie_delete(m_pCmdGrpOverrides, group);
398 g_ConCmds.UpdateAdminCmdFlags(group, Override_CommandGroup, 0);
401 void AdminCache::_UnsetCommandOverride(const char *cmd)
403 if (!m_pCmdOverrides)
408 sm_trie_delete(m_pCmdOverrides, cmd);
410 g_ConCmds.UpdateAdminCmdFlags(cmd, Override_Command, 0);
413 void AdminCache::DumpCommandOverrideCache(OverrideType type)
415 if (type == Override_Command && m_pCmdOverrides)
417 sm_trie_clear(m_pCmdOverrides);
418 } else if (type == Override_CommandGroup && m_pCmdGrpOverrides) {
419 sm_trie_clear(m_pCmdGrpOverrides);
423 AdminId AdminCache::CreateAdmin(const char *name)
428 if (m_FreeUserList != INVALID_ADMIN_ID)
430 pUser = (AdminUser *)m_pMemory->GetAddress(m_FreeUserList);
431 assert(pUser->magic == USR_MAGIC_UNSET);
433 m_FreeUserList = pUser->next_user;
435 id = m_pMemory->CreateMem(sizeof(AdminUser), (void **)&pUser);
437 pUser->grp_table = -1;
442 pUser->grp_count = 0;
443 pUser->password = -1;
444 pUser->magic = USR_MAGIC_SET;
445 pUser->auth.identidx = -1;
446 pUser->auth.index = 0;
447 pUser->immune_default = false;
448 pUser->immune_global = false;
450 if (m_FirstUser == INVALID_ADMIN_ID)
455 AdminUser *pPrev = (AdminUser *)m_pMemory->GetAddress(m_LastUser);
456 pPrev->next_user = id;
457 pUser->prev_user = m_LastUser;
461 if (name && name[0] != '\0')
463 pUser->nameidx = m_pStrings->AddString(name);
469 GroupId AdminCache::AddGroup(const char *group_name)
471 if (sm_trie_retrieve(m_pGroups, group_name, NULL))
473 return INVALID_GROUP_ID;
478 if (m_FreeGroupList != INVALID_GROUP_ID)
480 pGroup = (AdminGroup *)m_pMemory->GetAddress(m_FreeGroupList);
481 assert(pGroup->magic == GRP_MAGIC_UNSET);
482 id = m_FreeGroupList;
483 m_FreeGroupList = pGroup->next_grp;
485 id = m_pMemory->CreateMem(sizeof(AdminGroup), (void **)&pGroup);
488 pGroup->immune_default = false;
489 pGroup->immune_global = false;
490 pGroup->immune_table = -1;
491 pGroup->magic = GRP_MAGIC_SET;
492 pGroup->next_grp = INVALID_GROUP_ID;
493 pGroup->pCmdGrpTable = NULL;
494 pGroup->pCmdTable = NULL;
495 pGroup->addflags = 0;
497 if (m_FirstGroup == INVALID_GROUP_ID)
501 pGroup->prev_grp = INVALID_GROUP_ID;
503 AdminGroup *pPrev = (AdminGroup *)m_pMemory->GetAddress(m_LastGroup);
504 assert(pPrev->magic == GRP_MAGIC_SET);
505 pPrev->next_grp = id;
506 pGroup->prev_grp = m_LastGroup;
510 pGroup->nameidx = m_pStrings->AddString(group_name);
512 sm_trie_insert(m_pGroups, group_name, (void *)id);
517 GroupId AdminCache::FindGroupByName(const char *group_name)
521 if (!sm_trie_retrieve(m_pGroups, group_name, &object))
523 return INVALID_GROUP_ID;
526 GroupId id = (GroupId)object;
527 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
529 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
531 return INVALID_GROUP_ID;
537 void AdminCache::SetGroupAddFlag(GroupId id, AdminFlag flag, bool enabled)
539 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
540 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
545 if (flag < Admin_Reservation || flag >= AdminFlags_TOTAL)
550 FlagBits bits = (1<<(FlagBits)flag);
554 pGroup->addflags |= bits;
556 pGroup->addflags &= ~bits;
560 bool AdminCache::GetGroupAddFlag(GroupId id, AdminFlag flag)
562 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
563 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
568 if (flag < Admin_Reservation || flag >= AdminFlags_TOTAL)
573 FlagBits bit = 1<<(FlagBits)flag;
574 return ((pGroup->addflags & bit) == bit);
577 FlagBits AdminCache::GetGroupAddFlags(GroupId id)
579 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
580 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
585 return pGroup->addflags;
588 void AdminCache::SetGroupGenericImmunity(GroupId id, ImmunityType type, bool enabled)
590 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
591 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
596 if (type == Immunity_Default)
598 pGroup->immune_default = enabled;
599 } else if (type == Immunity_Global) {
600 pGroup->immune_global = enabled;
604 bool AdminCache::GetGroupGenericImmunity(GroupId id, ImmunityType type)
606 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
607 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
612 if (type == Immunity_Default)
614 return pGroup->immune_default;
615 } else if (type == Immunity_Global) {
616 return pGroup->immune_global;
622 void AdminCache::AddGroupImmunity(GroupId id, GroupId other_id)
624 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
625 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
630 AdminGroup *pOther = (AdminGroup *)m_pMemory->GetAddress(id);
631 if (!pOther || pOther->magic != GRP_MAGIC_SET)
636 /* We always need to resize the immunity table */
638 if (pOther->immune_table == -1)
640 tblidx = m_pMemory->CreateMem(sizeof(int) * 2, (void **)&table);
643 int *old_table = (int *)m_pMemory->GetAddress(pOther->immune_table);
644 /* Break out if this group is already in the list */
645 for (int i=0; i<old_table[0]; i++)
647 if (old_table[1+i] == other_id)
652 tblidx = m_pMemory->CreateMem(sizeof(int) * (old_table[0] + 2), (void **)&table);
653 /* Get the old address again in case of resize */
654 old_table = (int *)m_pMemory->GetAddress(pOther->immune_table);
655 table[0] = old_table[0];
656 for (unsigned int i=1; i<=(unsigned int)old_table[0]; i++)
658 table[i] = old_table[i];
663 pOther->immune_table = tblidx;
665 /* Add to the array */
667 table[table[0]] = other_id;
670 unsigned int AdminCache::GetGroupImmunityCount(GroupId id)
672 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
673 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
678 if (pGroup->immune_table == -1)
683 int *table = (int *)m_pMemory->GetAddress(pGroup->immune_table);
688 GroupId AdminCache::GetGroupImmunity(GroupId id, unsigned int number)
690 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
691 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
693 return INVALID_GROUP_ID;
696 if (pGroup->immune_table == -1)
698 return INVALID_GROUP_ID;
701 int *table = (int *)m_pMemory->GetAddress(pGroup->immune_table);
702 if (number >= (unsigned int)table[0])
704 return INVALID_GROUP_ID;
707 return table[1 + number];
710 void AdminCache::AddGroupCommandOverride(GroupId id, const char *name, OverrideType type, OverrideRule rule)
712 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
713 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
719 if (type == Override_Command)
721 if (pGroup->pCmdTable == NULL)
723 pGroup->pCmdTable = sm_trie_create();
725 pTrie = pGroup->pCmdTable;
726 } else if (type == Override_CommandGroup) {
727 if (pGroup->pCmdGrpTable == NULL)
729 pGroup->pCmdGrpTable = sm_trie_create();
731 pTrie = pGroup->pCmdGrpTable;
736 sm_trie_insert(pTrie, name, (void *)(int)rule);
739 bool AdminCache::GetGroupCommandOverride(GroupId id, const char *name, OverrideType type, OverrideRule *pRule)
741 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
742 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
748 if (type == Override_Command)
750 if (pGroup->pCmdTable == NULL)
754 pTrie = pGroup->pCmdTable;
755 } else if (type == Override_CommandGroup) {
756 if (pGroup->pCmdGrpTable == NULL)
760 pTrie = pGroup->pCmdGrpTable;
766 if (!sm_trie_retrieve(pTrie, name, &object))
773 *pRule = (OverrideRule)(int)object;
779 Trie *AdminCache::GetMethodByIndex(unsigned int index)
781 List<AuthMethod>::iterator iter;
782 for (iter=m_AuthMethods.begin();
783 iter!=m_AuthMethods.end();
788 return (*iter).table;
795 bool AdminCache::InvalidateAdmin(AdminId id)
797 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
799 if (!pUser || pUser->magic != USR_MAGIC_SET)
804 if (!m_InvalidatingAdmins && !m_destroying)
806 g_Players.ClearAdminId(id);
809 /* Unlink from the dbl link list */
810 if (id == m_FirstUser && id == m_LastUser)
812 m_FirstUser = INVALID_ADMIN_ID;
813 m_LastUser = INVALID_ADMIN_ID;
814 } else if (id == m_FirstUser) {
815 m_FirstUser = pUser->next_user;
816 pOther = (AdminUser *)m_pMemory->GetAddress(m_FirstUser);
817 pOther->prev_user = INVALID_ADMIN_ID;
818 } else if (id == m_LastUser) {
819 m_LastUser = pUser->prev_user;
820 pOther = (AdminUser *)m_pMemory->GetAddress(m_LastUser);
821 pOther->next_user = INVALID_ADMIN_ID;
823 pOther = (AdminUser *)m_pMemory->GetAddress(pUser->prev_user);
824 pOther->next_user = pUser->next_user;
825 pOther = (AdminUser *)m_pMemory->GetAddress(pUser->next_user);
826 pOther->prev_user = pUser->prev_user;
829 /* Unlink from auth tables */
830 if (pUser->auth.identidx != -1)
832 Trie *pTrie = GetMethodByIndex(pUser->auth.index);
835 sm_trie_delete(pTrie, m_pStrings->GetString(pUser->auth.identidx));
839 /* Clear table counts */
840 pUser->grp_count = 0;
842 /* Link into free list */
843 pUser->magic = USR_MAGIC_UNSET;
844 pUser->next_user = m_FreeUserList;
851 void AdminCache::InvalidateGroup(GroupId id)
853 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
856 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
861 const char *str = m_pStrings->GetString(pGroup->nameidx);
862 sm_trie_delete(m_pGroups, str);
864 /* Unlink from the live dbllink list */
865 if (id == m_FirstGroup && id == m_LastGroup)
867 m_LastGroup = INVALID_GROUP_ID;
868 m_FirstGroup = INVALID_GROUP_ID;
869 } else if (id == m_FirstGroup) {
870 m_FirstGroup = pGroup->next_grp;
871 pOther = (AdminGroup *)m_pMemory->GetAddress(m_FirstGroup);
872 pOther->prev_grp = INVALID_GROUP_ID;
873 } else if (id == m_LastGroup) {
874 m_LastGroup = pGroup->prev_grp;
875 pOther = (AdminGroup *)m_pMemory->GetAddress(m_LastGroup);
876 pOther->next_grp = INVALID_GROUP_ID;
878 pOther = (AdminGroup *)m_pMemory->GetAddress(pGroup->prev_grp);
879 pOther->next_grp = pGroup->next_grp;
880 pOther = (AdminGroup *)m_pMemory->GetAddress(pGroup->next_grp);
881 pOther->prev_grp = pGroup->prev_grp;
884 /* Free any used memory to be safe */
885 if (pGroup->pCmdGrpTable)
887 sm_trie_destroy(pGroup->pCmdGrpTable);
888 pGroup->pCmdGrpTable = NULL;
890 if (pGroup->pCmdTable)
892 sm_trie_destroy(pGroup->pCmdTable);
893 pGroup->pCmdTable = NULL;
896 /* Link into the free list */
897 pGroup->magic = GRP_MAGIC_UNSET;
898 pGroup->next_grp = m_FreeGroupList;
899 m_FreeGroupList = id;
901 int idx = m_FirstUser;
904 while (idx != INVALID_ADMIN_ID)
906 pUser = (AdminUser *)m_pMemory->GetAddress(idx);
907 if (pUser->grp_count > 0)
909 table = (int *)m_pMemory->GetAddress(pUser->grp_table);
910 for (unsigned int i=0; i<pUser->grp_count; i++)
914 /* We have to remove this entry */
915 for (unsigned int j=i+1; j<pUser->grp_count; j++)
917 /* Move everything down by one */
918 table[j-1] = table[j];
922 /* Recalculate effective flags */
923 pUser->eflags = pUser->flags;
924 for (unsigned int j=0; j<pUser->grp_count; j++)
926 pOther = (AdminGroup *)m_pMemory->GetAddress(table[j]);
927 pUser->eflags |= pOther->addflags;
929 /* Break now, duplicates aren't allowed */
934 idx = pUser->next_user;
938 void AdminCache::InvalidateGroupCache()
940 /* Nuke the free list */
941 m_FreeGroupList = -1;
943 /* Nuke reverse lookups */
944 sm_trie_clear(m_pGroups);
946 /* Free memory on groups */
947 GroupId cur = m_FirstGroup;
949 while (cur != INVALID_GROUP_ID)
951 pGroup = (AdminGroup *)m_pMemory->GetAddress(cur);
952 assert(pGroup->magic == GRP_MAGIC_SET);
953 if (pGroup->pCmdGrpTable)
955 sm_trie_destroy(pGroup->pCmdGrpTable);
957 if (pGroup->pCmdTable)
959 sm_trie_destroy(pGroup->pCmdTable);
961 cur = pGroup->next_grp;
964 m_FirstGroup = INVALID_GROUP_ID;
965 m_LastGroup = INVALID_GROUP_ID;
967 InvalidateAdminCache(false);
969 /* Reset the memory table */
973 void AdminCache::AddAdminListener(IAdminListener *pListener)
975 m_hooks.push_back(pListener);
978 void AdminCache::RemoveAdminListener(IAdminListener *pListener)
980 m_hooks.remove(pListener);
983 void AdminCache::RegisterAuthIdentType(const char *name)
985 if (sm_trie_retrieve(m_pAuthTables, name, NULL))
990 Trie *pAuth = sm_trie_create();
993 method.name.assign(name);
994 method.table = pAuth;
996 m_AuthMethods.push_back(method);
998 sm_trie_insert(m_pAuthTables, name, pAuth);
1001 void AdminCache::InvalidateAdminCache(bool unlink_admins)
1003 m_InvalidatingAdmins = true;
1006 g_Players.ClearAllAdmins();
1008 /* Wipe the identity cache first */
1009 List<AuthMethod>::iterator iter;
1010 for (iter=m_AuthMethods.begin();
1011 iter!=m_AuthMethods.end();
1014 sm_trie_clear((*iter).table);
1019 while (m_FirstUser != INVALID_ADMIN_ID)
1021 InvalidateAdmin(m_FirstUser);
1026 m_FreeUserList = -1;
1028 m_InvalidatingAdmins = false;
1031 void AdminCache::DumpAdminCache(AdminCachePart part, bool rebuild)
1033 List<IAdminListener *>::iterator iter;
1034 IAdminListener *pListener;
1037 if (part == AdminCache_Overrides)
1039 DumpCommandOverrideCache(Override_Command);
1040 DumpCommandOverrideCache(Override_CommandGroup);
1041 if (rebuild && !m_destroying)
1043 for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
1045 pListener = (*iter);
1046 pListener->OnRebuildOverrideCache();
1048 m_pCacheFwd->PushCell(part);
1049 m_pCacheFwd->Execute(&result);
1051 } else if (part == AdminCache_Groups || part == AdminCache_Admins) {
1052 if (part == AdminCache_Groups)
1054 InvalidateGroupCache();
1055 if (rebuild && !m_destroying)
1057 for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
1059 pListener = (*iter);
1060 pListener->OnRebuildGroupCache();
1062 m_pCacheFwd->PushCell(part);
1063 m_pCacheFwd->Execute(&result);
1066 InvalidateAdminCache(true);
1067 if (rebuild && !m_destroying)
1069 for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
1071 pListener = (*iter);
1072 pListener->OnRebuildAdminCache((part == AdminCache_Groups));
1074 m_pCacheFwd->PushCell(AdminCache_Admins);
1075 m_pCacheFwd->Execute(&result);
1076 g_Players.RecheckAnyAdmins();
1081 const char *AdminCache::GetAdminName(AdminId id)
1083 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1084 if (!pUser || pUser->magic != USR_MAGIC_SET)
1089 return m_pStrings->GetString(pUser->nameidx);
1092 bool AdminCache::GetMethodIndex(const char *name, unsigned int *_index)
1094 List<AuthMethod>::iterator iter;
1095 unsigned int index = 0;
1096 for (iter=m_AuthMethods.begin();
1097 iter!=m_AuthMethods.end();
1100 if ((*iter).name.compare(name) == 0)
1110 bool AdminCache::BindAdminIdentity(AdminId id, const char *auth, const char *ident)
1112 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1113 if (!pUser || pUser->magic != USR_MAGIC_SET)
1119 if (!sm_trie_retrieve(m_pAuthTables, auth, (void **)&pTable))
1124 if (sm_trie_retrieve(pTable, ident, NULL))
1129 pUser->auth.identidx = m_pStrings->AddString(ident);
1130 GetMethodIndex(auth, &pUser->auth.index);
1132 return sm_trie_insert(pTable, ident, (void **)id);
1135 AdminId AdminCache::FindAdminByIdentity(const char *auth, const char *identity)
1138 if (!sm_trie_retrieve(m_pAuthTables, auth, (void **)&pTable))
1140 return INVALID_ADMIN_ID;
1144 if (!sm_trie_retrieve(pTable, identity, &object))
1146 return INVALID_ADMIN_ID;
1149 return (AdminId)object;
1152 void AdminCache::SetAdminFlag(AdminId id, AdminFlag flag, bool enabled)
1154 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1155 if (!pUser || pUser->magic != USR_MAGIC_SET)
1160 if (flag < Admin_Reservation
1161 || flag >= AdminFlags_TOTAL)
1166 FlagBits bits = (1<<(FlagBits)flag);
1170 pUser->flags |= bits;
1171 pUser->eflags |= bits;
1173 pUser->flags &= ~bits;
1174 pUser->eflags &= ~bits;
1178 bool AdminCache::GetAdminFlag(AdminId id, AdminFlag flag, AccessMode mode)
1180 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1181 if (!pUser || pUser->magic != USR_MAGIC_SET)
1186 if (flag < Admin_Reservation
1187 || flag >= AdminFlags_TOTAL)
1192 FlagBits bit = (1<<(FlagBits)flag);
1194 if (mode == Access_Real)
1196 return ((pUser->flags & bit) == bit);
1197 } else if (mode == Access_Effective) {
1198 bool has_bit = ((pUser->eflags & bit) == bit);
1199 if (!has_bit && flag != Admin_Root && ((pUser->eflags & ADMFLAG_ROOT) == ADMFLAG_ROOT))
1209 FlagBits AdminCache::GetAdminFlags(AdminId id, AccessMode mode)
1211 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1212 if (!pUser || pUser->magic != USR_MAGIC_SET)
1217 if (mode == Access_Real)
1219 return pUser->flags;
1220 } else if (mode == Access_Effective) {
1221 return pUser->eflags;
1227 void AdminCache::SetAdminFlags(AdminId id, AccessMode mode, FlagBits bits)
1229 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1230 if (!pUser || pUser->magic != USR_MAGIC_SET)
1235 if (mode == Access_Real)
1237 pUser->flags = bits;
1238 pUser->eflags = bits;
1239 } else if (mode == Access_Effective) {
1240 pUser->eflags = bits;
1244 bool AdminCache::AdminInheritGroup(AdminId id, GroupId gid)
1246 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1247 if (!pUser || pUser->magic != USR_MAGIC_SET)
1252 AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(gid);
1253 if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
1258 /* First check for duplicates */
1259 if (pUser->grp_count != 0)
1261 int *temp_table = (int *)m_pMemory->GetAddress(pUser->grp_table);
1262 for (unsigned int i=0; i<pUser->grp_count; i++)
1264 if (temp_table[i] == gid)
1272 if (pUser->grp_count + 1 > pUser->grp_size)
1277 if (pUser->grp_size == 0)
1281 new_size = pUser->grp_size * 2;
1284 /* Create and refresh pointers */
1285 tblidx = m_pMemory->CreateMem(new_size * sizeof(int), (void **)&table);
1286 pUser = (AdminUser *)m_pMemory->GetAddress(id);
1287 pGroup = (AdminGroup *)m_pMemory->GetAddress(gid);
1289 /* Copy old data if necessary */
1290 if (pUser->grp_table != -1)
1292 int *old_table = (int *)m_pMemory->GetAddress(pUser->grp_table);
1293 memcpy(table, old_table, sizeof(int) * pUser->grp_count);
1295 pUser->grp_table = tblidx;
1296 pUser->grp_size = new_size;
1298 table = (int *)m_pMemory->GetAddress(pUser->grp_table);
1301 table[pUser->grp_count] = gid;
1304 /* Compute new effective permissions */
1305 pUser->eflags |= pGroup->addflags;
1307 if (pGroup->immune_default)
1309 pUser->immune_default = true;
1311 if (pGroup->immune_global)
1313 pUser->immune_global = true;
1319 bool AdminCache::IsValidAdmin(AdminId id)
1321 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1322 return (pUser != NULL && pUser->magic == USR_MAGIC_SET);
1325 unsigned int AdminCache::GetAdminGroupCount(AdminId id)
1327 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1328 if (!pUser || pUser->magic != USR_MAGIC_SET)
1333 return pUser->grp_count;
1336 GroupId AdminCache::GetAdminGroup(AdminId id, unsigned int index, const char **name)
1338 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1339 if (!pUser || pUser->magic != USR_MAGIC_SET || index >= pUser->grp_count)
1341 return INVALID_GROUP_ID;
1344 int *table = (int *)m_pMemory->GetAddress(pUser->grp_table);
1346 return table[index];
1349 const char *AdminCache::GetAdminPassword(AdminId id)
1351 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1352 if (!pUser || pUser->magic != USR_MAGIC_SET)
1357 return m_pStrings->GetString(pUser->password);
1360 void AdminCache::SetAdminPassword(AdminId id, const char *password)
1362 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1363 if (!pUser || pUser->magic != USR_MAGIC_SET)
1368 pUser->password = m_pStrings->AddString(password);
1371 unsigned int AdminCache::FlagBitsToBitArray(FlagBits bits, bool array[], unsigned int maxSize)
1374 for (i=0; i<maxSize && i<AdminFlags_TOTAL; i++)
1376 array[i] = ((bits & (1<<i)) == (unsigned)(1<<i));
1382 FlagBits AdminCache::FlagBitArrayToBits(const bool array[], unsigned int maxSize)
1385 for (unsigned int i=0; i<maxSize && i<AdminFlags_TOTAL; i++)
1396 FlagBits AdminCache::FlagArrayToBits(const AdminFlag array[], unsigned int numFlags)
1399 for (unsigned int i=0; i<numFlags && i<AdminFlags_TOTAL; i++)
1401 bits |= (1 << (FlagBits)array[i]);
1407 unsigned int AdminCache::FlagBitsToArray(FlagBits bits, AdminFlag array[], unsigned int maxSize)
1409 unsigned int i, num=0;
1410 for (i=0; num<maxSize && i<AdminFlags_TOTAL; i++)
1412 if ((bits & (1<<i)) == (unsigned)(1<<i))
1414 array[num++] = (AdminFlag)i;
1421 bool AdminCache::CheckAdminFlags(AdminId id, FlagBits bits)
1423 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1424 if (!pUser || pUser->magic != USR_MAGIC_SET)
1429 return ((pUser->eflags & bits) == bits);
1432 bool AdminCache::CanAdminTarget(AdminId id, AdminId target)
1435 * Zeroth, if the targeting AdminId is INVALID_ADMIN_ID, targeting fails.
1436 * First, if the targeted AdminId is INVALID_ADMIN_ID, targeting succeeds.
1439 if (id == INVALID_ADMIN_ID)
1444 if (target == INVALID_ADMIN_ID)
1449 AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
1450 if (!pUser || pUser->magic != USR_MAGIC_SET)
1455 AdminUser *pTarget = (AdminUser *)m_pMemory->GetAddress(target);
1456 if (!pTarget || pTarget->magic != USR_MAGIC_SET)
1462 * Second, if the targeting admin is root, targeting succeeds.
1464 if (pUser->eflags & ADMFLAG_ROOT)
1469 /** Fourth, if the targeted admin has global immunity, targeting fails. */
1470 if (pTarget->immune_global)
1476 * Fifth, if the targeted admin has default immunity
1477 * and the admin belongs to no groups, targeting fails.
1479 if (pTarget->immune_default && pUser->grp_count < 1)
1485 * Sixth, if the targeted admin has specific immunity from the
1486 * targeting admin via group immunities, targeting fails.
1488 //:TODO: speed this up... maybe with trie hacks.
1489 //idea is to insert %d.%d in the trie after computing this and use it as a cache lookup.
1490 //problem is the trie cannot delete prefixes, so we'd have a problem with invalidations.
1491 if (pTarget->grp_count > 0 && pUser->grp_count > 0)
1493 int *grp_table = (int *)m_pMemory->GetAddress(pTarget->grp_table);
1494 int *src_table = (int *)m_pMemory->GetAddress(pUser->grp_table);
1497 for (unsigned int i=0; i<pTarget->grp_count; i++)
1500 num = GetGroupImmunityCount(id);
1501 for (unsigned int j=0; j<num; i++)
1503 other = GetGroupImmunity(id, j);
1504 for (unsigned int k=0; k<pUser->grp_count; k++)
1506 if (other == src_table[k])
1518 bool AdminCache::FindFlag(char c, AdminFlag *pAdmFlag)
1520 if (c < 'a' || c > 'z')
1527 *pAdmFlag = g_FlagLetters[(unsigned)c - (unsigned)'a'];
1533 FlagBits AdminCache::ReadFlagString(const char *flags, const char **end)
1537 while (flags && (*flags != '\0'))
1540 if (!FindFlag(*flags, &flag))
1544 bits |= FlagArrayToBits(&flag, 1);