From dc6f03b4e474558cb614b95c320eaa7f1300d06e Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sun, 7 Jul 2024 02:09:48 +0300 Subject: [PATCH] engine: server: add function SV_HavePassword that correctly checks whether this server have set up password --- engine/server/server.h | 1 + engine/server/sv_client.c | 26 +++++++++++++++++++++----- engine/server/sv_query.c | 10 +++++----- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/engine/server/server.h b/engine/server/server.h index 7847b7fd..4d7fa41e 100644 --- a/engine/server/server.h +++ b/engine/server/server.h @@ -548,6 +548,7 @@ void SV_UpdateServerInfo( void ); void SV_EndRedirect( host_redirect_t *rd ); void SV_RejectConnection( netadr_t from, const char *fmt, ... ) _format( 2 ); void SV_GetPlayerCount( int *clients, int *bots ); +qboolean SV_HavePassword( void ); // // sv_cmds.c diff --git a/engine/server/sv_client.c b/engine/server/sv_client.c index 5ed66e09..78f00623 100644 --- a/engine/server/sv_client.c +++ b/engine/server/sv_client.c @@ -45,6 +45,20 @@ static int g_userid = 1; static void SV_UserinfoChanged( sv_client_t *cl ); static void SV_ExecuteClientCommand( sv_client_t *cl, const char *s ); +/* +================= +SV_HavePassword + +================= +*/ +qboolean SV_HavePassword( void ) +{ + if( COM_CheckStringEmpty( sv_password.string ) && Q_stricmp( sv_password.string, "none" )) + return true; + + return false; +} + /* ================= SV_GetPlayerCount @@ -370,10 +384,13 @@ static void SV_ConnectClient( netadr_t from ) Q_strncpy( userinfo, s, sizeof( userinfo )); // check connection password (don't verify local client) - if( !NET_IsLocalAddress( from ) && sv_password.string[0] && Q_stricmp( sv_password.string, Info_ValueForKey( userinfo, "password" ))) + if( !NET_IsLocalAddress( from ) && SV_HavePassword( )) { - SV_RejectConnection( from, "invalid password\n" ); - return; + if( Q_stricmp( sv_password.string, Info_ValueForKey( userinfo, "password" ))) + { + SV_RejectConnection( from, "invalid password\n" ); + return; + } } // if there is already a slot for this ip, reuse it @@ -915,7 +932,6 @@ static void SV_Info( netadr_t from, int protocolVersion ) int bots; int remaining; char temp[sizeof( s )]; - qboolean have_password = COM_CheckStringEmpty( sv_password.string ); SV_GetPlayerCount( &count, &bots ); @@ -928,7 +944,7 @@ static void SV_Info( netadr_t from, int protocolVersion ) Info_SetValueForKeyf( s, "numcl", sizeof( s ), "%i", count ); Info_SetValueForKeyf( s, "maxcl", sizeof( s ), "%i", svs.maxclients ); Info_SetValueForKey( s, "gamedir", GI->gamefolder, sizeof( s )); - Info_SetValueForKey( s, "password", have_password ? "1" : "0", sizeof( s )); + Info_SetValueForKey( s, "password", SV_HavePassword() ? "1" : "0", sizeof( s )); // write host last so we can try to cut off too long hostnames // TODO: value size limit for infostrings diff --git a/engine/server/sv_query.c b/engine/server/sv_query.c index 17b1e930..f138a0b4 100644 --- a/engine/server/sv_query.c +++ b/engine/server/sv_query.c @@ -37,12 +37,9 @@ static void SV_SourceQuery_Details( netadr_t from ) sizebuf_t buf; char answer[2048]; int bot_count, client_count; - int is_private = 0; SV_GetPlayerCount( &client_count, &bot_count ); client_count += bot_count; // bots are counted as players in this reply - if( COM_CheckStringEmpty( sv_password.string ) && Q_stricmp( sv_password.string, "none" )) - is_private = 1; MSG_Init( &buf, "TSourceEngineQuery", answer, sizeof( answer )); @@ -67,7 +64,10 @@ static void SV_SourceQuery_Details( netadr_t from ) #else MSG_WriteByte( &buf, 'l' ); #endif - MSG_WriteByte( &buf, is_private ); + + if( SV_HavePassword( )) + MSG_WriteByte( &buf, 1 ); + else MSG_WriteByte( &buf, 0 ); MSG_WriteByte( &buf, GI->secure ); MSG_WriteString( &buf, XASH_VERSION ); @@ -130,7 +130,7 @@ static void SV_SourceQuery_Players( netadr_t from ) client_count += bot_count; // bots are counted as players in this reply // respect players privacy - if( client_count <= 0 || !sv_expose_player_list.value || !COM_CheckStringEmpty( sv_password.string )) + if( client_count <= 0 || !sv_expose_player_list.value || SV_HavePassword( )) return; MSG_Init( &buf, "TSourceEngineQueryPlayers", answer, sizeof( answer ));