| 1 | #ifndef ENGINE_SERVER_AUTHMANAGER_H |
| 2 | #define ENGINE_SERVER_AUTHMANAGER_H |
| 3 | |
| 4 | #include <base/hash.h> |
| 5 | #include <base/system.h> |
| 6 | |
| 7 | #include <generated/protocol.h> |
| 8 | |
| 9 | #include <string> |
| 10 | #include <unordered_map> |
| 11 | #include <vector> |
| 12 | |
| 13 | #define SALT_BYTES 8 |
| 14 | |
| 15 | namespace RoleName |
| 16 | { |
| 17 | inline const char *const ADMIN = "admin" ; |
| 18 | inline const char *const MODERATOR = "moderator" ; |
| 19 | inline const char *const HELPER = "helper" ; |
| 20 | } // namespace RoleName |
| 21 | |
| 22 | namespace RoleRank |
| 23 | { |
| 24 | static constexpr int ADMIN = AUTHED_ADMIN; |
| 25 | static constexpr int MODERATOR = AUTHED_MOD; |
| 26 | static constexpr int HELPER = AUTHED_HELPER; |
| 27 | static constexpr int NONE = AUTHED_NO; |
| 28 | } // namespace RoleRank |
| 29 | |
| 30 | class CRconRole |
| 31 | { |
| 32 | char m_aName[64]; |
| 33 | int m_Rank = RoleRank::NONE; |
| 34 | |
| 35 | public: |
| 36 | // Name of the rcon role. For example "admin". |
| 37 | const char *Name() const { return m_aName; } |
| 38 | |
| 39 | // The rank determines how powerful the role is |
| 40 | // compared to other roles. |
| 41 | // Higher rank means more power. |
| 42 | // Roles with lower rank can never kick roles with higher rank. |
| 43 | // Roles with higher rank can see commands executed by roles with lower rank |
| 44 | // but not vice versa. |
| 45 | int Rank() const { return m_Rank; } |
| 46 | |
| 47 | CRconRole(const char *pName, int Rank) : |
| 48 | m_Rank(Rank) |
| 49 | { |
| 50 | str_copy(dst&: m_aName, src: pName); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | class CAuthManager |
| 55 | { |
| 56 | private: |
| 57 | class CKey |
| 58 | { |
| 59 | public: |
| 60 | char m_aIdent[64]; |
| 61 | MD5_DIGEST m_Pw; |
| 62 | unsigned char m_aSalt[SALT_BYTES]; |
| 63 | CRconRole *m_pRole = nullptr; |
| 64 | }; |
| 65 | std::vector<CKey> m_vKeys; |
| 66 | std::unordered_map<std::string, CRconRole> m_Roles; |
| 67 | |
| 68 | int m_aDefault[3]; |
| 69 | bool m_Generated; |
| 70 | |
| 71 | public: |
| 72 | static const char *AuthLevelToRoleName(int AuthLevel); |
| 73 | |
| 74 | typedef void (*FListCallback)(const char *pIdent, const char *pRoleName, void *pUser); |
| 75 | |
| 76 | CAuthManager(); |
| 77 | |
| 78 | void Init(); |
| 79 | int AddKeyHash(const char *pIdent, MD5_DIGEST Hash, const unsigned char *pSalt, const char *pRoleName); |
| 80 | int AddKey(const char *pIdent, const char *pPw, const char *pRoleName); |
| 81 | void RemoveKey(int Slot); |
| 82 | int FindKey(const char *pIdent) const; |
| 83 | bool CheckKey(int Slot, const char *pPw) const; |
| 84 | |
| 85 | private: |
| 86 | int DefaultIndex(int AuthLevel) const; |
| 87 | |
| 88 | public: |
| 89 | int DefaultKey(const char *pRoleName) const; |
| 90 | int KeyLevel(int Slot) const; |
| 91 | const char *KeyIdent(int Slot) const; |
| 92 | bool IsValidIdent(const char *pIdent) const; |
| 93 | void UpdateKeyHash(int Slot, MD5_DIGEST Hash, const unsigned char *pSalt, const char *pRoleName); |
| 94 | void UpdateKey(int Slot, const char *pPw, const char *pRoleName); |
| 95 | void ListKeys(FListCallback pfnListCallback, void *pUser); |
| 96 | void AddDefaultKey(const char *pRoleName, const char *pPw); |
| 97 | bool IsGenerated() const; |
| 98 | int NumNonDefaultKeys() const; |
| 99 | CRconRole *FindRole(const char *pName); |
| 100 | bool AddRole(const char *pName, int Rank); |
| 101 | }; |
| 102 | |
| 103 | #endif //ENGINE_SERVER_AUTHMANAGER_H |
| 104 | |