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