]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/ipban.qc
Merge branch 'TimePath/hudpanels_registry' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / ipban.qc
index d3d7fdca6e93aa87ad5db6b41f5261891672ff0a..5bf648a0b5995b7269e7e10ceb09af334088ba44 100644 (file)
@@ -1,3 +1,14 @@
+#include "ipban.qh"
+#include "_all.qh"
+
+#include "autocvars.qh"
+#include "command/banning.qh"
+#include "defs.qh"
+#include "../common/constants.qh"
+#include "../common/util.qh"
+#include "../dpdefs/dpextensions.qh"
+#include "../dpdefs/progsdefs.qh"
+
 /*
  * Protocol of online ban list:
  *
@@ -83,18 +94,18 @@ void OnlineBanList_URI_Get_Callback(float id, float status, string data)
 
        if(id >= MAX_IPBAN_URIS)
        {
-               print("Received ban list for invalid ID\n");
+               LOG_INFO("Received ban list for invalid ID\n");
                return;
        }
 
        tokenize_console(autocvar_g_ban_sync_uri);
        uri = argv(id);
 
-       print("Received ban list from ", uri, ": ");
+       LOG_INFO("Received ban list from ", uri, ": ");
 
        if(OnlineBanList_RequestWaiting[id] == 0)
        {
-               print("rejected (unexpected)\n");
+               LOG_INFO("rejected (unexpected)\n");
                return;
        }
 
@@ -102,14 +113,14 @@ void OnlineBanList_URI_Get_Callback(float id, float status, string data)
 
        if(time > OnlineBanList_Timeout)
        {
-               print("rejected (too late)\n");
+               LOG_INFO("rejected (too late)\n");
                return;
        }
 
        syncinterval = autocvar_g_ban_sync_interval;
        if(syncinterval == 0)
        {
-               print("rejected (syncing disabled)\n");
+               LOG_INFO("rejected (syncing disabled)\n");
                return;
        }
        if(syncinterval > 0)
@@ -117,19 +128,19 @@ void OnlineBanList_URI_Get_Callback(float id, float status, string data)
 
        if(status != 0)
        {
-               print("error: status is ", ftos(status), "\n");
+               LOG_INFO("error: status is ", ftos(status), "\n");
                return;
        }
 
        if(substring(data, 0, 1) == "<")
        {
-               print("error: received HTML instead of a ban list\n");
+               LOG_INFO("error: received HTML instead of a ban list\n");
                return;
        }
 
        if(strstrofs(data, "\r", 0) != -1)
        {
-               print("error: received carriage returns\n");
+               LOG_INFO("error: received carriage returns\n");
                return;
        }
 
@@ -140,11 +151,11 @@ void OnlineBanList_URI_Get_Callback(float id, float status, string data)
 
        if((n % 4) != 0)
        {
-               print("error: received invalid item count: ", ftos(n), "\n");
+               LOG_INFO("error: received invalid item count: ", ftos(n), "\n");
                return;
        }
 
-       print("OK, ", ftos(n / 4), " items\n");
+       LOG_INFO("OK, ", ftos(n / 4), " items\n");
 
        for(i = 0; i < n; i += 4)
        {
@@ -153,9 +164,9 @@ void OnlineBanList_URI_Get_Callback(float id, float status, string data)
                reason = argv(i + 2);
                serverip = argv(i + 3);
 
-               dprint("received ban list item ", ftos(i / 4), ": ip=", ip);
-               dprint(" timeleft=", ftos(timeleft), " reason=", reason);
-               dprint(" serverip=", serverip, "\n");
+               LOG_TRACE("received ban list item ", ftos(i / 4), ": ip=", ip);
+               LOG_TRACE(" timeleft=", ftos(timeleft), " reason=", reason);
+               LOG_TRACE(" serverip=", serverip, "\n");
 
                timeleft -= 1.5 * autocvar_g_ban_sync_timeout;
                if(timeleft < 0)
@@ -167,7 +178,7 @@ void OnlineBanList_URI_Get_Callback(float id, float status, string data)
                        for(j = 0; j < l; ++j)
                                if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
                                {
-                                       print("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.\n");
+                                       LOG_INFO("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.\n");
                                        goto skip;
                                }
                }
@@ -181,15 +192,15 @@ void OnlineBanList_URI_Get_Callback(float id, float status, string data)
                        // the ban will be prolonged on the next sync
                        // or expire 5 seconds after the next timeout
                Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip, " at ", uri), 0);
-               print("Ban list syncing: accepted ban of ", ip, " by ", serverip, " at ", uri, ": ");
-               print(reason, "\n");
+               LOG_INFO("Ban list syncing: accepted ban of ", ip, " by ", serverip, " at ", uri, ": ");
+               LOG_INFO(reason, "\n");
 
 :skip
        }
 }
 
 void OnlineBanList_Think()
-{
+{SELFPARAM();
        float argc;
        string uri;
        float i, n;
@@ -238,7 +249,7 @@ void OnlineBanList_Think()
        remove(self);
 }
 
-#define BAN_MAX 256
+const float BAN_MAX = 256;
 float ban_loaded;
 string ban_ip[BAN_MAX];
 float ban_expire[BAN_MAX];
@@ -276,11 +287,11 @@ void Ban_SaveBans()
 float Ban_Delete(float i)
 {
        if(i < 0)
-               return FALSE;
+               return false;
        if(i >= ban_count)
-               return FALSE;
+               return false;
        if(ban_expire[i] == 0)
-               return FALSE;
+               return false;
        if(ban_expire[i] > 0)
        {
                OnlineBanList_SendUnban(ban_ip[i]);
@@ -289,7 +300,7 @@ float Ban_Delete(float i)
        ban_expire[i] = 0;
        ban_ip[i] = "";
        Ban_SaveBans();
-       return TRUE;
+       return true;
 }
 
 void Ban_LoadBans()
@@ -298,7 +309,7 @@ void Ban_LoadBans()
        for(i = 0; i < ban_count; ++i)
                Ban_Delete(i);
        ban_count = 0;
-       ban_loaded = TRUE;
+       ban_loaded = true;
        n = tokenize_console(autocvar_g_banned_list);
        if(stof(argv(0)) == 1)
        {
@@ -322,7 +333,7 @@ void Ban_View()
        float i, n;
        string msg;
 
-       print("^2Listing all existing active bans:\n");
+       LOG_INFO("^2Listing all existing active bans:\n");
 
        n = 0;
        for(i = 0; i < ban_count; ++i)
@@ -336,10 +347,10 @@ void Ban_View()
                msg = strcat(msg, ban_ip[i], " is still banned for ");
                msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
 
-               print("  ", msg, "\n");
+               LOG_INFO("  ", msg, "\n");
        }
 
-       print("^2Done listing all active (", ftos(n), ") bans.\n");
+       LOG_INFO("^2Done listing all active (", ftos(n), ") bans.\n");
 }
 
 float Ban_GetClientIP(entity client)
@@ -348,7 +359,7 @@ float Ban_GetClientIP(entity client)
        float i1, i2, i3, i4;
        string s;
 
-       if(client.crypto_keyfp)
+       if(client.crypto_idfp_signed)
                ban_idfp = client.crypto_idfp;
        else
                ban_idfp = string_null;
@@ -360,10 +371,10 @@ float Ban_GetClientIP(entity client)
                goto ipv6;
        i2 = strstrofs(s, ".", i1 + 1);
        if(i2 < 0)
-               return FALSE;
+               return false;
        i3 = strstrofs(s, ".", i2 + 1);
        if(i3 < 0)
-               return FALSE;
+               return false;
        i4 = strstrofs(s, ".", i3 + 1);
        if(i4 >= 0)
                s = substring(s, 0, i4);
@@ -372,21 +383,21 @@ float Ban_GetClientIP(entity client)
        ban_ip2 = substring(s, 0, i2); // 16
        ban_ip3 = substring(s, 0, i3); // 24
        ban_ip4 = strcat1(s); // 32
-       return TRUE;
+       return true;
 
 :ipv6
        i1 = strstrofs(s, ":", 0);
        if(i1 < 0)
-               return FALSE;
+               return false;
        i1 = strstrofs(s, ":", i1 + 1);
        if(i1 < 0)
-               return FALSE;
+               return false;
        i2 = strstrofs(s, ":", i1 + 1);
        if(i2 < 0)
-               return FALSE;
+               return false;
        i3 = strstrofs(s, ":", i2 + 1);
        if(i3 < 0)
-               return FALSE;
+               return false;
 
        ban_ip1 = strcat(substring(s, 0, i1), "::/32"); // 32
        ban_ip2 = strcat(substring(s, 0, i2), "::/48"); // 48
@@ -397,7 +408,7 @@ float Ban_GetClientIP(entity client)
        else
                ban_ip3 = strcat(substring(s, 0, i2), ":0::/56");
 
-       return TRUE;
+       return true;
 }
 
 float Ban_IsClientBanned(entity client, float idx)
@@ -406,7 +417,7 @@ float Ban_IsClientBanned(entity client, float idx)
        if(!ban_loaded)
                Ban_LoadBans();
        if(!Ban_GetClientIP(client))
-               return FALSE;
+               return false;
        if(idx < 0)
        {
                b = 0;
@@ -417,27 +428,27 @@ float Ban_IsClientBanned(entity client, float idx)
                b = idx;
                e = idx + 1;
        }
-       ipbanned = FALSE;
+       ipbanned = false;
        for(i = b; i < e; ++i)
        {
                string s;
                if(time > ban_expire[i])
                        continue;
                s = ban_ip[i];
-               if(ban_ip1 == s) ipbanned = TRUE;
-               if(ban_ip2 == s) ipbanned = TRUE;
-               if(ban_ip3 == s) ipbanned = TRUE;
-               if(ban_ip4 == s) ipbanned = TRUE;
-               if(ban_idfp == s) return TRUE;
+               if(ban_ip1 == s) ipbanned = true;
+               if(ban_ip2 == s) ipbanned = true;
+               if(ban_ip3 == s) ipbanned = true;
+               if(ban_ip4 == s) ipbanned = true;
+               if(ban_idfp == s) return true;
        }
        if(ipbanned)
        {
                if(!autocvar_g_banned_list_idmode)
-                       return TRUE;
+                       return true;
                if (!ban_idfp)
-                       return TRUE;
+                       return true;
        }
-       return FALSE;
+       return false;
 }
 
 float Ban_MaybeEnforceBan(entity client)
@@ -448,18 +459,18 @@ float Ban_MaybeEnforceBan(entity client)
                s = strcat("^1NOTE:^7 banned client ", client.netaddress, " just tried to enter\n");
                dropclient(client);
                bprint(s);
-               return TRUE;
+               return true;
        }
-       return FALSE;
+       return false;
 }
 
 .float ban_checked;
 float Ban_MaybeEnforceBanOnce(entity client)
 {
        if(client.ban_checked)
-               return FALSE;
-       client.ban_checked = TRUE;
-       return Ban_MaybeEnforceBan(self);
+               return false;
+       client.ban_checked = true;
+       return Ban_MaybeEnforceBan(client);
 }
 
 string Ban_Enforce(float i, string reason)
@@ -469,7 +480,8 @@ string Ban_Enforce(float i, string reason)
 
        // Enforce our new ban
        s = "";
-       FOR_EACH_REALCLIENT(e)
+       FOR_EACH_CLIENTSLOT(e)
+               if (IS_REAL_CLIENT(e))
                if(Ban_IsClientBanned(e, i))
                {
                        if(reason != "")
@@ -502,10 +514,10 @@ float Ban_Insert(string ip, float bantime, string reason, float dosync)
                        if(time + bantime > ban_expire[i])
                        {
                                ban_expire[i] = time + bantime;
-                               dprint(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now\n");
+                               LOG_TRACE(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now\n");
                        }
                        else
-                               dprint(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now\n");
+                               LOG_TRACE(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now\n");
 
                        // and enforce
                        reason = Ban_Enforce(i, reason);
@@ -516,7 +528,7 @@ float Ban_Insert(string ip, float bantime, string reason, float dosync)
                                        if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
                                                OnlineBanList_SendBan(ip, bantime, reason);
 
-                       return FALSE;
+                       return false;
                }
 
        // do we have a free slot?
@@ -542,12 +554,12 @@ float Ban_Insert(string ip, float bantime, string reason, float dosync)
        if(i < ban_count)
        if(ban_expire[i] > time + bantime)
        {
-               print(ip, " could not get banned due to no free ban slot\n");
-               return FALSE;
+               LOG_INFO(ip, " could not get banned due to no free ban slot\n");
+               return false;
        }
        // okay, insert our new victim as i
        Ban_Delete(i);
-       dprint(ip, " has been banned for ", ftos(bantime), " seconds\n");
+       LOG_TRACE(ip, " has been banned for ", ftos(bantime), " seconds\n");
        ban_expire[i] = time + bantime;
        ban_ip[i] = strzone(ip);
        ban_count = max(ban_count, i + 1);
@@ -562,7 +574,7 @@ float Ban_Insert(string ip, float bantime, string reason, float dosync)
                        if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
                                OnlineBanList_SendBan(ip, bantime, reason);
 
-       return TRUE;
+       return true;
 }
 
 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)