(svn r3469) - Fix: plug a possible memleak with subsequential strdup's without freeing the previous value and make it possible to ban offline clients

This commit is contained in:
Darkvater 2006-01-29 18:04:52 +00:00
parent 2de4331931
commit 20538e9b40
2 changed files with 28 additions and 22 deletions

View File

@ -362,22 +362,29 @@ DEF_CONSOLE_CMD(ConClearBuffer)
DEF_CONSOLE_CMD(ConBan) DEF_CONSOLE_CMD(ConBan)
{ {
NetworkClientInfo *ci; NetworkClientInfo *ci;
const char *banip = NULL;
uint32 index; uint32 index;
if (argc == 0) { if (argc == 0) {
IConsoleHelp("Ban a player from a network game. Usage: 'ban <ip | client-id>'"); IConsoleHelp("Ban a player from a network game. Usage: 'ban <ip | client-id>'");
IConsoleHelp("For client-id's, see the command 'clients'"); IConsoleHelp("For client-id's, see the command 'clients'");
IConsoleHelp("If the client is no longer online, you can still ban his/her IP");
return true; return true;
} }
if (argc != 2) return false; if (argc != 2) return false;
if (strchr(argv[1], '.') == NULL) { if (strchr(argv[1], '.') == NULL) { // banning with ID
index = atoi(argv[1]); index = atoi(argv[1]);
ci = NetworkFindClientInfoFromIndex(index); ci = NetworkFindClientInfoFromIndex(index);
} else { } else { // banning IP
ci = NetworkFindClientInfoFromIP(argv[1]); ci = NetworkFindClientInfoFromIP(argv[1]);
index = (ci == NULL) ? 0 : ci->client_index; if (ci == NULL) {
banip = argv[1];
index = (uint32)-1;
} else {
index = ci->client_index;
}
} }
if (index == NETWORK_SERVER_INDEX) { if (index == NETWORK_SERVER_INDEX) {
@ -385,25 +392,26 @@ DEF_CONSOLE_CMD(ConBan)
return true; return true;
} }
if (index == 0) { if (index == 0 || (ci == NULL && index != (uint32)-1)) {
IConsoleError("Invalid client"); IConsoleError("Invalid client");
return true; return true;
} }
if (ci != NULL) { if (ci != NULL) {
uint i; banip = inet_ntoa(*(struct in_addr *)&ci->client_ip);
SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(index), NETWORK_ERROR_KICKED);
IConsolePrint(_icolour_def, "Client banned");
} else
IConsolePrint(_icolour_def, "Client not online, banned IP");
/* Add user to ban-list */ /* Add user to ban-list */
for (i = 0; i < lengthof(_network_ban_list); i++) { for (index = 0; index < lengthof(_network_ban_list); index++) {
if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') { if (_network_ban_list[index] == NULL) {
_network_ban_list[i] = strdup(inet_ntoa(*(struct in_addr *)&ci->client_ip)); _network_ban_list[index] = strdup(banip);
break; break;
} }
} }
SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(index), NETWORK_ERROR_KICKED);
} else
IConsoleError("Client not found");
return true; return true;
} }
@ -423,11 +431,11 @@ DEF_CONSOLE_CMD(ConUnBan)
index--; index--;
for (i = 0; i < lengthof(_network_ban_list); i++) { for (i = 0; i < lengthof(_network_ban_list); i++) {
if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') if (_network_ban_list[i] == NULL) continue;
continue;
if (strncmp(_network_ban_list[i], argv[1], strlen(_network_ban_list[i])) == 0 || index == i) { if (strncmp(_network_ban_list[i], argv[1], strlen(_network_ban_list[i])) == 0 || index == i) {
_network_ban_list[i][0] = '\0'; free(_network_ban_list[i]);
_network_ban_list[i] = NULL;
IConsolePrint(_icolour_def, "IP unbanned."); IConsolePrint(_icolour_def, "IP unbanned.");
return true; return true;
} }
@ -449,9 +457,7 @@ DEF_CONSOLE_CMD(ConBanList)
IConsolePrint(_icolour_def, "Banlist: "); IConsolePrint(_icolour_def, "Banlist: ");
for (i = 0; i < lengthof(_network_ban_list); i++) { for (i = 0; i < lengthof(_network_ban_list); i++) {
if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') if (_network_ban_list[i] != NULL)
continue;
IConsolePrintF(_icolour_def, " %d) %s", i + 1, _network_ban_list[i]); IConsolePrintF(_icolour_def, " %d) %s", i + 1, _network_ban_list[i]);
} }

View File

@ -1068,7 +1068,7 @@ static void ClientList_Ban(byte client_no)
uint32 ip = NetworkFindClientInfo(client_no)->client_ip; uint32 ip = NetworkFindClientInfo(client_no)->client_ip;
for (i = 0; i < lengthof(_network_ban_list); i++) { for (i = 0; i < lengthof(_network_ban_list); i++) {
if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') { if (_network_ban_list[i] == NULL) {
_network_ban_list[i] = strdup(inet_ntoa(*(struct in_addr *)&ip)); _network_ban_list[i] = strdup(inet_ntoa(*(struct in_addr *)&ip));
break; break;
} }