Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codechange: [Network] Use std::string for passwords or hashes thereof #9263

Merged
merged 5 commits into from May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/company_cmd.cpp
Expand Up @@ -844,7 +844,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
assert(_local_company == COMPANY_SPECTATOR);
SetLocalCompany(c->index);
if (!_settings_client.network.default_company_pass.empty()) {
NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass.c_str());
NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass);
}

/* Now that we have a new company, broadcast our company settings to
Expand Down
8 changes: 4 additions & 4 deletions src/console_cmds.cpp
Expand Up @@ -1635,7 +1635,7 @@ DEF_CONSOLE_CMD(ConCompanies)
if (c->is_ai) {
password_state = "AI";
} else if (_network_server) {
password_state = StrEmpty(_network_company_states[c->index].password) ? "unprotected" : "protected";
password_state = _network_company_states[c->index].password.empty() ? "unprotected" : "protected";
}

char colour[512];
Expand Down Expand Up @@ -1737,7 +1737,7 @@ DEF_CONSOLE_CMD(ConCompanyPassword)
}

CompanyID company_id;
const char *password;
std::string password;
const char *errormsg;

if (argc == 2) {
Expand All @@ -1759,10 +1759,10 @@ DEF_CONSOLE_CMD(ConCompanyPassword)

password = NetworkChangeCompanyPassword(company_id, password);

if (StrEmpty(password)) {
if (password.empty()) {
IConsolePrintF(CC_WARNING, "Company password cleared");
} else {
IConsolePrintF(CC_WARNING, "Company password changed to: %s", password);
IConsolePrintF(CC_WARNING, "Company password changed to: %s", password.c_str());
}

return true;
Expand Down
18 changes: 9 additions & 9 deletions src/network/network.cpp
Expand Up @@ -152,9 +152,9 @@ byte NetworkSpectatorCount()
* @param password The unhashed password we like to set ('*' or '' resets the password)
* @return The password.
*/
const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password)
std::string NetworkChangeCompanyPassword(CompanyID company_id, std::string password)
{
if (strcmp(password, "*") == 0) password = "";
if (password.compare("*") == 0) password = "";

if (_network_server) {
NetworkServerSetCompanyPassword(company_id, password, false);
Expand All @@ -172,13 +172,13 @@ const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *passw
* @param password_game_seed Game seed.
* @return The hashed password.
*/
const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed)
std::string GenerateCompanyPasswordHash(const std::string &password, const std::string &password_server_id, uint32 password_game_seed)
{
if (StrEmpty(password)) return password;
if (password.empty()) return password;

char salted_password[NETWORK_SERVER_ID_LENGTH];
size_t password_length = strlen(password);
size_t password_server_id_length = strlen(password_server_id);
size_t password_length = password.size();
size_t password_server_id_length = password_server_id.size();

/* Add the game seed and the server's ID as the salt. */
for (uint i = 0; i < NETWORK_SERVER_ID_LENGTH - 1; i++) {
Expand Down Expand Up @@ -601,7 +601,7 @@ void NetworkClose(bool close_admins)

NetworkFreeLocalCommandQueue();

free(_network_company_states);
delete[] _network_company_states;
_network_company_states = nullptr;

InitializeNetworkPools(close_admins);
Expand Down Expand Up @@ -787,7 +787,7 @@ class TCPClientConnecter : TCPConnecter {
* @param join_company_password The password for the company.
* @return Whether the join has started.
*/
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const char *join_server_password, const char *join_company_password)
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const std::string &join_server_password, const std::string &join_company_password)
{
CompanyID join_as = default_company;
std::string resolved_connection_string = ParseGameConnectionString(connection_string, NETWORK_DEFAULT_PORT, &join_as).GetAddressAsString(false);
Expand Down Expand Up @@ -896,7 +896,7 @@ bool NetworkServerStart()
DEBUG(net, 5, "Starting listeners for incoming server queries");
NetworkUDPServerListen();

_network_company_states = CallocT<NetworkCompanyState>(MAX_COMPANIES);
_network_company_states = new NetworkCompanyState[MAX_COMPANIES];
_network_server = true;
_networking = true;
_frame_counter = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/network/network_admin.cpp
Expand Up @@ -664,8 +664,7 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_JOIN(Packet *p)
{
if (this->status != ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);

char password[NETWORK_PASSWORD_LENGTH];
p->Recv_string(password, sizeof(password));
std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);

if (_settings_client.network.admin_password.empty() ||
_settings_client.network.admin_password.compare(password) != 0) {
Expand Down
32 changes: 15 additions & 17 deletions src/network/network_client.cpp
Expand Up @@ -319,7 +319,7 @@ static uint32 last_ack_frame;
/** One bit of 'entropy' used to generate a salt for the company passwords. */
static uint32 _password_game_seed;
/** The other bit of 'entropy' used to generate a salt for the company passwords. */
static char _password_server_id[NETWORK_SERVER_ID_LENGTH];
static std::string _password_server_id;

/** Maximum number of companies of the currently joined server. */
static uint8 _network_server_max_companies;
Expand Down Expand Up @@ -385,7 +385,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendNewGRFsOk()
* Set the game password as requested.
* @param password The game password.
*/
NetworkRecvStatus ClientNetworkGameSocketHandler::SendGamePassword(const char *password)
NetworkRecvStatus ClientNetworkGameSocketHandler::SendGamePassword(const std::string &password)
{
Packet *p = new Packet(PACKET_CLIENT_GAME_PASSWORD);
p->Send_string(password);
Expand All @@ -397,7 +397,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendGamePassword(const char *p
* Set the company password as requested.
* @param password The company password.
*/
NetworkRecvStatus ClientNetworkGameSocketHandler::SendCompanyPassword(const char *password)
NetworkRecvStatus ClientNetworkGameSocketHandler::SendCompanyPassword(const std::string &password)
{
Packet *p = new Packet(PACKET_CLIENT_COMPANY_PASSWORD);
p->Send_string(GenerateCompanyPasswordHash(password, _password_server_id, _password_game_seed));
Expand Down Expand Up @@ -478,7 +478,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendError(NetworkErrorCode err
* Tell the server that we like to change the password of the company.
* @param password The new password.
*/
NetworkRecvStatus ClientNetworkGameSocketHandler::SendSetPassword(const char *password)
NetworkRecvStatus ClientNetworkGameSocketHandler::SendSetPassword(const std::string &password)
{
Packet *p = new Packet(PACKET_CLIENT_SET_PASSWORD);

Expand Down Expand Up @@ -516,7 +516,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendQuit()
* @param pass The password for the remote command.
* @param command The actual command.
*/
NetworkRecvStatus ClientNetworkGameSocketHandler::SendRCon(const char *pass, const char *command)
NetworkRecvStatus ClientNetworkGameSocketHandler::SendRCon(const std::string &pass, const char *command)
{
Packet *p = new Packet(PACKET_CLIENT_RCON);
p->Send_string(pass);
Expand All @@ -530,7 +530,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendRCon(const char *pass, con
* @param company The company to move to.
* @param password The password of the company to move to.
*/
NetworkRecvStatus ClientNetworkGameSocketHandler::SendMove(CompanyID company, const char *password)
NetworkRecvStatus ClientNetworkGameSocketHandler::SendMove(CompanyID company, const std::string &password)
{
Packet *p = new Packet(PACKET_CLIENT_MOVE);
p->Send_uint8(company);
Expand Down Expand Up @@ -799,9 +799,8 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_NEED_GAME_PASSW
if (this->status < STATUS_JOIN || this->status >= STATUS_AUTH_GAME) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
this->status = STATUS_AUTH_GAME;

const char *password = _network_join.server_password;
if (!StrEmpty(password)) {
return SendGamePassword(password);
if (!_network_join.server_password.empty()) {
return SendGamePassword(_network_join.server_password);
}

ShowNetworkNeedPassword(NETWORK_GAME_PASSWORD);
Expand All @@ -815,12 +814,11 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_NEED_COMPANY_PA
this->status = STATUS_AUTH_COMPANY;

_password_game_seed = p->Recv_uint32();
p->Recv_string(_password_server_id, sizeof(_password_server_id));
_password_server_id = p->Recv_string(NETWORK_SERVER_ID_LENGTH);
if (this->HasClientQuit()) return NETWORK_RECV_STATUS_MALFORMED_PACKET;

const char *password = _network_join.company_password;
if (!StrEmpty(password)) {
return SendCompanyPassword(password);
if (!_network_join.company_password.empty()) {
return SendCompanyPassword(_network_join.company_password);
}

ShowNetworkNeedPassword(NETWORK_COMPANY_PASSWORD);
Expand All @@ -837,7 +835,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_WELCOME(Packet

/* Initialize the password hash salting variables, even if they were previously. */
_password_game_seed = p->Recv_uint32();
p->Recv_string(_password_server_id, sizeof(_password_server_id));
_password_server_id = p->Recv_string(NETWORK_SERVER_ID_LENGTH);

/* Start receiving the map */
return SendGetMap();
Expand Down Expand Up @@ -1263,7 +1261,7 @@ void NetworkClient_Connected()
* @param password The password.
* @param command The command to execute.
*/
void NetworkClientSendRcon(const char *password, const char *command)
void NetworkClientSendRcon(const std::string &password, const char *command)
{
MyClient::SendRCon(password, command);
}
Expand All @@ -1274,7 +1272,7 @@ void NetworkClientSendRcon(const char *password, const char *command)
* @param pass the password, is only checked on the server end if a password is needed.
* @return void
*/
void NetworkClientRequestMove(CompanyID company_id, const char *pass)
void NetworkClientRequestMove(CompanyID company_id, const std::string &pass)
{
MyClient::SendMove(company_id, pass);
}
Expand Down Expand Up @@ -1396,7 +1394,7 @@ void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const
* Set/Reset company password on the client side.
* @param password Password to be set.
*/
void NetworkClientSetCompanyPassword(const char *password)
void NetworkClientSetCompanyPassword(const std::string &password)
{
MyClient::SendSetPassword(password);
}
Expand Down
18 changes: 9 additions & 9 deletions src/network/network_client.h
Expand Up @@ -90,14 +90,14 @@ class ClientNetworkGameSocketHandler : public ZeroedMemoryAllocator, public Netw
static NetworkRecvStatus SendQuit();
static NetworkRecvStatus SendAck();

static NetworkRecvStatus SendGamePassword(const char *password);
static NetworkRecvStatus SendCompanyPassword(const char *password);
static NetworkRecvStatus SendGamePassword(const std::string &password);
static NetworkRecvStatus SendCompanyPassword(const std::string &password);

static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data);
static NetworkRecvStatus SendSetPassword(const char *password);
static NetworkRecvStatus SendSetPassword(const std::string &password);
static NetworkRecvStatus SendSetName(const char *name);
static NetworkRecvStatus SendRCon(const char *password, const char *command);
static NetworkRecvStatus SendMove(CompanyID company, const char *password);
static NetworkRecvStatus SendRCon(const std::string &password, const char *command);
static NetworkRecvStatus SendMove(CompanyID company, const std::string &password);

static bool IsConnected();

Expand All @@ -110,15 +110,15 @@ class ClientNetworkGameSocketHandler : public ZeroedMemoryAllocator, public Netw
typedef ClientNetworkGameSocketHandler MyClient;

void NetworkClient_Connected();
void NetworkClientSetCompanyPassword(const char *password);
void NetworkClientSetCompanyPassword(const std::string &password);

/** Information required to join a server. */
struct NetworkJoinInfo {
NetworkJoinInfo() : company(COMPANY_SPECTATOR), server_password(nullptr), company_password(nullptr) {}
NetworkJoinInfo() : company(COMPANY_SPECTATOR) {}
std::string connection_string; ///< The address of the server to join.
CompanyID company; ///< The company to join.
const char *server_password; ///< The password of the server to join.
const char *company_password; ///< The password of the company to join.
std::string server_password; ///< The password of the server to join.
std::string company_password; ///< The password of the company to join.
};

extern NetworkJoinInfo _network_join;
Expand Down
8 changes: 4 additions & 4 deletions src/network/network_func.h
Expand Up @@ -40,7 +40,7 @@ bool NetworkValidateClientName();
bool NetworkValidateClientName(std::string &client_name);
void NetworkUpdateClientName();
bool NetworkCompanyHasClients(CompanyID company);
const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password);
std::string NetworkChangeCompanyPassword(CompanyID company_id, std::string password);
void NetworkReboot();
void NetworkDisconnect(bool blocking = false, bool close_admins = true);
void NetworkGameLoop();
Expand All @@ -51,10 +51,10 @@ void NetworkPopulateCompanyStats(NetworkCompanyStats *stats);

void NetworkUpdateClientInfo(ClientID client_id);
void NetworkClientsToSpectators(CompanyID cid);
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const char *join_server_password = nullptr, const char *join_company_password = nullptr);
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const std::string &join_server_password = "", const std::string &join_company_password = "");
void NetworkClientJoinGame();
void NetworkClientRequestMove(CompanyID company, const char *pass = "");
void NetworkClientSendRcon(const char *password, const char *command);
void NetworkClientRequestMove(CompanyID company, const std::string &pass = "");
void NetworkClientSendRcon(const std::string &password, const char *command);
void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data = 0);
bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio);
bool NetworkCompanyIsPassworded(CompanyID company_id);
Expand Down
2 changes: 1 addition & 1 deletion src/network/network_internal.h
Expand Up @@ -118,7 +118,7 @@ void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send,
uint NetworkCalculateLag(const NetworkClientSocket *cs);
StringID GetNetworkErrorMsg(NetworkErrorCode err);
bool NetworkFindName(char *new_name, const char *last);
const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed);
std::string GenerateCompanyPasswordHash(const std::string &password, const std::string &password_server_id, uint32 password_game_seed);

NetworkAddress ParseConnectionString(const std::string &connection_string, uint16 default_port);
std::string NormalizeConnectionString(const std::string &connection_string, uint16 default_port);
Expand Down