Catch several ways ban.txt could be malformed by a well-meaning server host, and report it back via the log.

This commit is contained in:
toaster 2022-07-04 14:00:12 +01:00
parent 36e44f2f6f
commit fbb613b2d6

View file

@ -2790,6 +2790,7 @@ void D_LoadBan(boolean warning)
time_t unbanTime = NO_BAN_TIME; time_t unbanTime = NO_BAN_TIME;
char buffer[MAX_WADPATH]; char buffer[MAX_WADPATH];
UINT8 banmode = 0; UINT8 banmode = 0;
boolean malformed = false;
if (!I_ClearBans) if (!I_ClearBans)
return; return;
@ -2815,7 +2816,10 @@ void D_LoadBan(boolean warning)
if (i == 0 && !strncmp(address, "BANFORMAT", 9)) if (i == 0 && !strncmp(address, "BANFORMAT", 9))
{ {
banmode = atoi(mask); if (mask)
{
banmode = atoi(mask);
}
switch (banmode) switch (banmode)
{ {
case BANFORMAT: // currently supported format case BANFORMAT: // currently supported format
@ -2824,32 +2828,56 @@ void D_LoadBan(boolean warning)
default: default:
{ {
fclose(f); fclose(f);
CONS_Alert(CONS_WARNING, "Could not load unknown ban.txt for ban list (BANFORMAT %d, expected %d)\n", banmode, BANFORMAT); CONS_Alert(CONS_WARNING, "Could not load unknown ban.txt for ban list (BANFORMAT %s, expected %d)\n", mask, BANFORMAT);
return; return;
} }
} }
continue; continue;
} }
if (I_SetBanAddress(address, mask) == false) // invalid IP input?
{
CONS_Alert(CONS_WARNING, "\"%s/%s\" is not a valid IP address, discarding...\n", address, mask);
continue;
}
// One-way legacy format conversion -- the game will crash otherwise // One-way legacy format conversion -- the game will crash otherwise
if (banmode == 0) if (banmode == 0)
{ {
unbanTime = NO_BAN_TIME; unbanTime = NO_BAN_TIME;
username = NULL; // not guaranteed to be accurate, but only sane substitute username = NULL; // not guaranteed to be accurate, but only sane substitute
reason = strtok(NULL, "\r\n"); reason = strtok(NULL, "\r\n");
if (reason[0] == 'N' && reason[1] == 'A' && reason[2] == '\0') if (reason && reason[0] == 'N' && reason[1] == 'A' && reason[2] == '\0')
{ {
reason = NULL; reason = NULL;
} }
} }
else else
{ {
unbanTime = atoi(strtok(NULL, " \"\t\r\n")); reason = strtok(NULL, " \"\t\r\n");
if (reason)
{
unbanTime = atoi(reason);
reason = NULL;
}
else
{
unbanTime = NO_BAN_TIME;
malformed = true;
}
username = strtok(NULL, "\"\t\r\n"); // go until next " username = strtok(NULL, "\"\t\r\n"); // go until next "
if (!username)
{
malformed = true;
}
strtok(NULL, "\"\t\r\n"); // remove first " strtok(NULL, "\"\t\r\n"); // remove first "
reason = strtok(NULL, "\"\r\n"); // go until next " reason = strtok(NULL, "\"\r\n"); // go until next "
if (!reason)
{
malformed = true;
}
} }
// Enforce MAX_REASONLENGTH. // Enforce MAX_REASONLENGTH.
@ -2865,8 +2893,6 @@ void D_LoadBan(boolean warning)
} }
} }
I_SetBanAddress(address, mask);
if (I_SetUnbanTime) if (I_SetUnbanTime)
I_SetUnbanTime(unbanTime); I_SetUnbanTime(unbanTime);
@ -2877,6 +2903,11 @@ void D_LoadBan(boolean warning)
I_SetBanReason(reason); I_SetBanReason(reason);
} }
if (malformed)
{
CONS_Alert(CONS_WARNING, "One or more lines of ban.txt are malformed. The game can correct for this, but some data may be lost.\n");
}
fclose(f); fclose(f);
} }