If no rcon password is set, generate one

After the generation (using the OS random number generator), the password is
displayed to the user on stdout.
This commit is contained in:
heinrich5991 2015-06-21 19:46:55 +02:00 committed by oy
parent 903878f4a2
commit 7af6e6ae5b
2 changed files with 45 additions and 0 deletions

View file

@ -285,6 +285,8 @@ CServer::CServer() : m_DemoRecorder(&m_SnapshotDelta)
m_RconClientID = IServer::RCON_CID_SERV;
m_RconAuthLevel = AUTHED_ADMIN;
m_GeneratedRconPassword = 0;
Init();
}
@ -508,6 +510,36 @@ int CServer::MaxClients() const
return m_NetServer.MaxClients();
}
void CServer::InitRconPasswordIfEmpty()
{
if(g_Config.m_SvRconPassword[0])
{
return;
}
static const char VALUES[] = "ABCDEFGHKLMNPRSTUVWXYZabcdefghjkmnopqt23456789";
static const size_t NUM_VALUES = sizeof(VALUES) - 1; // Disregard the '\0'.
static const size_t PASSWORD_LENGTH = 6;
dbg_assert(NUM_VALUES * NUM_VALUES >= 2048, "need at least 2048 possibilities for 2-character sequences");
// With 6 characters, we get a password entropy of log(2048) * 6/2 = 33bit.
dbg_assert(PASSWORD_LENGTH % 2 == 0, "need an even password length");
unsigned short aRandom[PASSWORD_LENGTH / 2];
char aRandomPassword[PASSWORD_LENGTH+1];
aRandomPassword[PASSWORD_LENGTH] = 0;
secure_random_fill(aRandom, sizeof(aRandom));
for(size_t i = 0; i < PASSWORD_LENGTH / 2; i++)
{
unsigned short RandomNumber = aRandom[i] % 2048;
aRandomPassword[2 * i + 0] = VALUES[RandomNumber / NUM_VALUES];
aRandomPassword[2 * i + 1] = VALUES[RandomNumber % NUM_VALUES];
}
str_copy(g_Config.m_SvRconPassword, aRandomPassword, sizeof(g_Config.m_SvRconPassword));
m_GeneratedRconPassword = 1;
}
int CServer::SendMsg(CMsgPacker *pMsg, int Flags, int ClientID)
{
CNetChunk Packet;
@ -1296,6 +1328,13 @@ int CServer::Run()
// process pending commands
m_pConsole->StoreCommands(false);
if(m_GeneratedRconPassword)
{
dbg_msg("server", "+-------------------------+");
dbg_msg("server", "| rcon password: '%s' |", g_Config.m_SvRconPassword);
dbg_msg("server", "+-------------------------+");
}
// start game
{
int64 ReportTime = time_get();
@ -1741,6 +1780,8 @@ int main(int argc, const char **argv) // ignore_convention
pEngine->InitLogfile();
pServer->InitRconPasswordIfEmpty();
// run the server
dbg_msg("server", "starting...");
pServer->Run();

View file

@ -161,6 +161,8 @@ public:
int m_CurrentMapSize;
int m_MapChunksPerRequest;
int m_GeneratedRconPassword;
CDemoRecorder m_DemoRecorder;
CRegister m_Register;
CMapChecker m_MapChecker;
@ -183,6 +185,8 @@ public:
int Init();
void InitRconPasswordIfEmpty();
void SetRconCID(int ClientID);
bool IsAuthed(int ClientID) const;
bool IsBanned(int ClientID) const;