1#include "authmanager.h"
2
3#include <base/dbg.h>
4#include <base/hash_ctxt.h>
5#include <base/mem.h>
6#include <base/secure.h>
7#include <base/str.h>
8
9#include <engine/shared/config.h>
10
11#include <generated/protocol.h>
12
13#include <optional>
14
15#define ADMIN_IDENT "default_admin"
16#define MOD_IDENT "default_mod"
17#define HELPER_IDENT "default_helper"
18
19const char *CAuthManager::AuthLevelToRoleName(int AuthLevel)
20{
21 switch(AuthLevel)
22 {
23 case AUTHED_ADMIN:
24 return RoleName::ADMIN;
25 case AUTHED_MOD:
26 return RoleName::MODERATOR;
27 case AUTHED_HELPER:
28 return RoleName::HELPER;
29 }
30
31 dbg_assert_failed("Invalid auth level: %d", AuthLevel);
32}
33
34std::optional<int> CAuthManager::RoleNameToAuthLevel(const char *pRoleName)
35{
36 if(!str_comp(a: pRoleName, b: RoleName::ADMIN))
37 return AUTHED_ADMIN;
38 if(!str_comp(a: pRoleName, b: RoleName::MODERATOR))
39 return AUTHED_MOD;
40 if(!str_comp(a: pRoleName, b: RoleName::HELPER))
41 return AUTHED_HELPER;
42
43 return std::nullopt;
44}
45
46static MD5_DIGEST HashPassword(const char *pPassword, const unsigned char aSalt[SALT_BYTES])
47{
48 // Hash the password and the salt
49 MD5_CTX Md5;
50 md5_init(ctxt: &Md5);
51 md5_update(ctxt: &Md5, data: (unsigned char *)pPassword, data_len: str_length(str: pPassword));
52 md5_update(ctxt: &Md5, data: aSalt, SALT_BYTES);
53 return md5_finish(ctxt: &Md5);
54}
55
56CAuthManager::CAuthManager()
57{
58 m_aDefault[0] = -1;
59 m_aDefault[1] = -1;
60 m_aDefault[2] = -1;
61 m_Generated = false;
62
63 AddRole(pName: RoleName::ADMIN, Rank: RoleRank::ADMIN);
64 AddRole(pName: RoleName::MODERATOR, Rank: RoleRank::MODERATOR);
65 AddRole(pName: RoleName::HELPER, Rank: RoleRank::HELPER);
66}
67
68void CAuthManager::Init()
69{
70 size_t NumDefaultKeys = 0;
71 if(g_Config.m_SvRconPassword[0])
72 NumDefaultKeys++;
73 if(g_Config.m_SvRconModPassword[0])
74 NumDefaultKeys++;
75 if(g_Config.m_SvRconHelperPassword[0])
76 NumDefaultKeys++;
77
78 auto It = std::find_if(first: m_vKeys.begin(), last: m_vKeys.end(), pred: [](const CKey &Key) { return str_comp(a: Key.m_aIdent, b: DEFAULT_SAVED_RCON_USER) == 0; });
79 if(It != m_vKeys.end())
80 NumDefaultKeys++;
81
82 if(m_vKeys.size() == NumDefaultKeys && !g_Config.m_SvRconPassword[0])
83 {
84 secure_random_password(buffer: g_Config.m_SvRconPassword, length: sizeof(g_Config.m_SvRconPassword), pw_length: 6);
85 AddDefaultKey(pRoleName: RoleName::ADMIN, pPw: g_Config.m_SvRconPassword);
86 m_Generated = true;
87 }
88}
89
90int CAuthManager::AddKeyHash(const char *pIdent, MD5_DIGEST Hash, const unsigned char *pSalt, const char *pRoleName)
91{
92 if(FindKey(pIdent) >= 0)
93 return -1;
94
95 CKey Key;
96 str_copy(dst&: Key.m_aIdent, src: pIdent);
97 Key.m_Pw = Hash;
98 mem_copy(dest: Key.m_aSalt, source: pSalt, SALT_BYTES);
99 Key.m_pRole = FindRole(pName: pRoleName);
100 dbg_assert(Key.m_pRole != nullptr, "Invalid role name '%s'.", pRoleName);
101
102 m_vKeys.push_back(x: Key);
103 return m_vKeys.size() - 1;
104}
105
106int CAuthManager::AddKey(const char *pIdent, const char *pPw, const char *pRoleName)
107{
108 // Generate random salt
109 unsigned char aSalt[SALT_BYTES];
110 secure_random_fill(bytes: aSalt, SALT_BYTES);
111 return AddKeyHash(pIdent, Hash: HashPassword(pPassword: pPw, aSalt), pSalt: aSalt, pRoleName);
112}
113
114void CAuthManager::RemoveKey(int Slot)
115{
116 m_vKeys.erase(position: m_vKeys.begin() + Slot);
117 // Update indices of default keys
118 for(int &Default : m_aDefault)
119 {
120 if(Default == Slot)
121 {
122 Default = -1;
123 }
124 else if(Default > Slot)
125 {
126 --Default;
127 }
128 }
129}
130
131int CAuthManager::FindKey(const char *pIdent) const
132{
133 for(size_t i = 0; i < m_vKeys.size(); i++)
134 if(!str_comp(a: m_vKeys[i].m_aIdent, b: pIdent))
135 return i;
136
137 return -1;
138}
139
140bool CAuthManager::CheckKey(int Slot, const char *pPw) const
141{
142 if(Slot < 0 || Slot >= (int)m_vKeys.size())
143 return false;
144 return m_vKeys[Slot].m_Pw == HashPassword(pPassword: pPw, aSalt: m_vKeys[Slot].m_aSalt);
145}
146
147int CAuthManager::DefaultIndex(int AuthLevel) const
148{
149 return std::size(m_aDefault) - AuthLevel;
150}
151
152int CAuthManager::DefaultKey(const char *pRoleName) const
153{
154 if(!str_comp(a: pRoleName, b: RoleName::ADMIN))
155 return m_aDefault[DefaultIndex(AuthLevel: AUTHED_ADMIN)];
156 if(!str_comp(a: pRoleName, b: RoleName::MODERATOR))
157 return m_aDefault[DefaultIndex(AuthLevel: AUTHED_MOD)];
158 if(!str_comp(a: pRoleName, b: RoleName::HELPER))
159 return m_aDefault[DefaultIndex(AuthLevel: AUTHED_HELPER)];
160 return 0;
161}
162
163int CAuthManager::KeyLevel(int Slot) const
164{
165 if(Slot < 0 || Slot >= (int)m_vKeys.size())
166 return false;
167 return m_vKeys[Slot].m_pRole->Rank();
168}
169
170const char *CAuthManager::KeyIdent(int Slot) const
171{
172 if(Slot < 0 || Slot >= (int)m_vKeys.size())
173 return nullptr;
174 return m_vKeys[Slot].m_aIdent;
175}
176
177bool CAuthManager::IsValidIdent(const char *pIdent) const
178{
179 return str_length(str: pIdent) < (int)sizeof(CKey().m_aIdent);
180}
181
182void CAuthManager::UpdateKeyHash(int Slot, MD5_DIGEST Hash, const unsigned char *pSalt, const char *pRoleName)
183{
184 if(Slot < 0 || Slot >= (int)m_vKeys.size())
185 return;
186
187 CKey *pKey = &m_vKeys[Slot];
188 pKey->m_Pw = Hash;
189 mem_copy(dest: pKey->m_aSalt, source: pSalt, SALT_BYTES);
190 pKey->m_pRole = FindRole(pName: pRoleName);
191}
192
193void CAuthManager::UpdateKey(int Slot, const char *pPw, const char *pRoleName)
194{
195 if(Slot < 0 || Slot >= (int)m_vKeys.size())
196 return;
197
198 // Generate random salt
199 unsigned char aSalt[SALT_BYTES];
200 secure_random_fill(bytes: aSalt, SALT_BYTES);
201 UpdateKeyHash(Slot, Hash: HashPassword(pPassword: pPw, aSalt), pSalt: aSalt, pRoleName);
202}
203
204void CAuthManager::ListKeys(FListCallback pfnListCallback, void *pUser)
205{
206 for(auto &Key : m_vKeys)
207 pfnListCallback(Key.m_aIdent, Key.m_pRole->Name(), pUser);
208}
209
210void CAuthManager::AddDefaultKey(const char *pRoleName, const char *pPw)
211{
212 int Level = RoleNameToAuthLevel(pRoleName).value_or(u: -1);
213 dbg_assert(AUTHED_HELPER <= Level && Level <= AUTHED_ADMIN, "default role with name '%s' not found.", pRoleName);
214
215 static const char s_aaIdents[3][sizeof(HELPER_IDENT)] = {ADMIN_IDENT, MOD_IDENT, HELPER_IDENT};
216 int Index = AUTHED_ADMIN - Level;
217 if(m_aDefault[Index] >= 0)
218 return; // already exists
219 m_aDefault[Index] = AddKey(pIdent: s_aaIdents[Index], pPw, pRoleName: AuthLevelToRoleName(AuthLevel: Level));
220}
221
222bool CAuthManager::IsGenerated() const
223{
224 return m_Generated;
225}
226
227int CAuthManager::NumNonDefaultKeys() const
228{
229 int DefaultCount = std::count_if(first: std::begin(arr: m_aDefault), last: std::end(arr: m_aDefault), pred: [](int Slot) {
230 return Slot >= 0;
231 });
232 return m_vKeys.size() - DefaultCount;
233}
234
235CRconRole *CAuthManager::FindRole(const char *pName)
236{
237 auto It = m_Roles.find(key: pName);
238 if(It == m_Roles.end())
239 return nullptr;
240 return &It->second;
241}
242
243bool CAuthManager::AddRole(const char *pName, int Rank)
244{
245 if(FindRole(pName))
246 return false;
247
248 m_Roles.insert(x: {pName, CRconRole(pName, Rank)});
249 return true;
250}
251