1
0
Fork 0

(svn r21854) -Codechange: refactor the password setting methods to make it possible to change the password of other companies (on the server)

release/1.1
rubidium 2011-01-19 16:47:40 +00:00
parent e54a1f6721
commit 795a528328
9 changed files with 32 additions and 32 deletions

View File

@ -835,7 +835,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
assert(_local_company == COMPANY_SPECTATOR); assert(_local_company == COMPANY_SPECTATOR);
SetLocalCompany(c->index); SetLocalCompany(c->index);
if (!StrEmpty(_settings_client.network.default_company_pass)) { if (!StrEmpty(_settings_client.network.default_company_pass)) {
NetworkChangeCompanyPassword(_settings_client.network.default_company_pass); NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass);
} }
/* Now that we have a new company, broadcast our company settings to /* Now that we have a new company, broadcast our company settings to

View File

@ -1530,7 +1530,7 @@ DEF_CONSOLE_CMD(ConCompanyPassword)
return false; return false;
} }
const char *password = NetworkChangeCompanyPassword(argv[1]); const char *password = NetworkChangeCompanyPassword(_local_company, argv[1], false);
if (StrEmpty(password)) { if (StrEmpty(password)) {
IConsolePrintF(CC_WARNING, "Company password cleared"); IConsolePrintF(CC_WARNING, "Company password cleared");

View File

@ -151,18 +151,19 @@ byte NetworkSpectatorCount()
} }
/** /**
* Sets/resets company password * Change the company password of a given company.
* @param password new password, "" or "*" resets password * @param company_id ID of the company the password should be changed for.
* @return new password * @param password The unhashed password we like to set ('*' or '' resets the password)
* @return The password.
*/ */
const char *NetworkChangeCompanyPassword(const char *password) const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password, bool already_hashed)
{ {
if (strcmp(password, "*") == 0) password = ""; if (strcmp(password, "*") == 0) password = "";
if (!_network_server) { if (_network_server) {
NetworkClientSetCompanyPassword(password); NetworkServerSetCompanyPassword(company_id, password, already_hashed);
} else { } else {
HashCurrentCompanyPassword(password); NetworkClientSetCompanyPassword(password);
} }
return password; return password;

View File

@ -278,9 +278,9 @@ ClientNetworkGameSocketHandler * ClientNetworkGameSocketHandler::my_client = NUL
static uint32 last_ack_frame; static uint32 last_ack_frame;
/** One bit of 'entropy' used to generate a salt for the company passwords. */ /** One bit of 'entropy' used to generate a salt for the company passwords. */
uint32 _password_game_seed; static uint32 _password_game_seed;
/** The other bit of 'entropy' used to generate a salt for the company passwords. */ /** The other bit of 'entropy' used to generate a salt for the company passwords. */
char _password_server_id[NETWORK_SERVER_ID_LENGTH]; static char _password_server_id[NETWORK_SERVER_ID_LENGTH];
/** Maximum number of companies of the currently joined server. */ /** Maximum number of companies of the currently joined server. */
static uint8 _network_server_max_companies; static uint8 _network_server_max_companies;
@ -1178,6 +1178,10 @@ void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const
MyClient::SendChat(action, type, dest, msg, data); MyClient::SendChat(action, type, dest, msg, data);
} }
/**
* Set/Reset company password on the client side.
* @param password Password to be set.
*/
void NetworkClientSetCompanyPassword(const char *password) void NetworkClientSetCompanyPassword(const char *password)
{ {
MyClient::SendSetPassword(password); MyClient::SendSetPassword(password);

View File

@ -36,7 +36,7 @@ extern StringList _network_ban_list;
byte NetworkSpectatorCount(); byte NetworkSpectatorCount();
void NetworkUpdateClientName(); void NetworkUpdateClientName();
bool NetworkCompanyHasClients(CompanyID company); bool NetworkCompanyHasClients(CompanyID company);
const char *NetworkChangeCompanyPassword(const char *); const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password, bool already_hashed = true);
void NetworkReboot(); void NetworkReboot();
void NetworkDisconnect(bool blocking = false, bool close_admins = true); void NetworkDisconnect(bool blocking = false, bool close_admins = true);
void NetworkGameLoop(); void NetworkGameLoop();

View File

@ -2311,7 +2311,7 @@ struct NetworkCompanyPasswordWindow : public QueryStringBaseWindow {
snprintf(_settings_client.network.default_company_pass, lengthof(_settings_client.network.default_company_pass), "%s", this->edit_str_buf); snprintf(_settings_client.network.default_company_pass, lengthof(_settings_client.network.default_company_pass), "%s", this->edit_str_buf);
} }
NetworkChangeCompanyPassword(this->edit_str_buf); NetworkChangeCompanyPassword(_local_company, this->edit_str_buf);
} }
virtual void OnPaint() virtual void OnPaint()

View File

@ -1287,10 +1287,7 @@ DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_SET_PASSWORD)
p->Recv_string(password, sizeof(password)); p->Recv_string(password, sizeof(password));
ci = this->GetInfo(); ci = this->GetInfo();
if (Company::IsValidID(ci->client_playas)) { NetworkServerSetCompanyPassword(ci->client_playas, password);
strecpy(_network_company_states[ci->client_playas].password, password, lastof(_network_company_states[ci->client_playas].password));
NetworkServerUpdateCompanyPassworded(ci->client_playas, !StrEmpty(_network_company_states[ci->client_playas].password));
}
return NETWORK_RECV_STATUS_OKAY; return NETWORK_RECV_STATUS_OKAY;
} }
@ -1630,23 +1627,21 @@ bool NetworkServerChangeClientName(ClientID client_id, const char *new_name)
} }
/** /**
* Hash the current company password; used when the server 'company' sets his/her password. * Set/Reset a company password on the server end.
* @param password The password to hash. * @param company_id ID of the company the password should be changed for.
* @param password The new password.
* @param already_hashed Is the given password already hashed?
*/ */
void HashCurrentCompanyPassword(const char *password) void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed)
{ {
uint32 password_game_seed; if (!Company::IsValidHumanID(company_id)) return;
char password_server_id[NETWORK_SERVER_ID_LENGTH];
password_game_seed = _settings_game.game_creation.generation_seed; if (!already_hashed) {
strecpy(password_server_id, _settings_client.network.network_id, lastof(password_server_id)); password = GenerateCompanyPasswordHash(password, _settings_client.network.network_id, _settings_game.game_creation.generation_seed);
const char *new_pw = GenerateCompanyPasswordHash(password, password_server_id, password_game_seed);
strecpy(_network_company_states[_local_company].password, new_pw, lastof(_network_company_states[_local_company].password));
if (_network_server) {
NetworkServerUpdateCompanyPassworded(_local_company, !StrEmpty(_network_company_states[_local_company].password));
} }
strecpy(_network_company_states[company_id].password, password, lastof(_network_company_states[company_id].password));
NetworkServerUpdateCompanyPassworded(company_id, !StrEmpty(_network_company_states[company_id].password));
} }
/* Handle the local command-queue */ /* Handle the local command-queue */

View File

@ -118,7 +118,7 @@ public:
}; };
void NetworkServer_Tick(bool send_frame); void NetworkServer_Tick(bool send_frame);
void HashCurrentCompanyPassword(const char *password); void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed = true);
#define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start) #define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start)
#define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0) #define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0)

View File

@ -823,7 +823,7 @@ static void MakeNewGameDone()
/* We are the server, we start a new company (not dedicated), /* We are the server, we start a new company (not dedicated),
* so set the default password *if* needed. */ * so set the default password *if* needed. */
if (_network_server && !StrEmpty(_settings_client.network.default_company_pass)) { if (_network_server && !StrEmpty(_settings_client.network.default_company_pass)) {
NetworkChangeCompanyPassword(_settings_client.network.default_company_pass); NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass);
} }
#endif /* ENABLE_NETWORK */ #endif /* ENABLE_NETWORK */