]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/ipban.qc
Merge branch 'master' into Mario/qc_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / ipban.qc
index fc889ecf2d0ad9e683e82e2045ab9764b0fa10d9..bd9880839d69f33ab220fd54a40eed7f87281460 100644 (file)
@@ -1,3 +1,16 @@
+#if defined(CSQC)
+#elif defined(MENUQC)
+#elif defined(SVQC)
+       #include "../dpdefs/progsdefs.qh"
+    #include "../dpdefs/dpextensions.qh"
+    #include "../common/constants.qh"
+    #include "../common/util.qh"
+    #include "autocvars.qh"
+    #include "defs.qh"
+    #include "command/banning.qh"
+    #include "ipban.qh"
+#endif
+
 /*
  * Protocol of online ban list:
  *
@@ -8,7 +21,7 @@
  *     GET g_ban_sync_uri?action=unban&hostname=...&ip=xxx.xxx.xxx
  * - Querying the ban list
  *     GET g_ban_sync_uri?action=list&hostname=...&servers=xxx.xxx.xxx.xxx;xxx.xxx.xxx.xxx;...
- *     
+ *
  *     shows the bans from the listed servers, and possibly others.
  *     Format of a ban is ASCII plain text, four lines per ban, delimited by
  *     newline ONLY (no carriage return):
@@ -114,7 +127,7 @@ void OnlineBanList_URI_Get_Callback(float id, float status, string data)
        }
        if(syncinterval > 0)
                syncinterval *= 60;
-       
+
        if(status != 0)
        {
                print("error: status is ", ftos(status), "\n");
@@ -138,7 +151,7 @@ void OnlineBanList_URI_Get_Callback(float id, float status, string data)
        else
                n = tokenizebyseparator(data, "\n");
 
-       if(mod(n, 4) != 0)
+       if((n % 4) != 0)
        {
                print("error: received invalid item count: ", ftos(n), "\n");
                return;
@@ -193,7 +206,7 @@ void OnlineBanList_Think()
        float argc;
        string uri;
        float i, n;
-       
+
        if(autocvar_g_ban_sync_uri == "")
                goto killme;
        if(autocvar_g_ban_sync_interval == 0) // < 0 is okay, it means "sync on level start only"
@@ -208,7 +221,7 @@ void OnlineBanList_Think()
        for(i = 1; i < argc; ++i)
                OnlineBanList_Servers = strcat(OnlineBanList_Servers, ";", argv(i));
        OnlineBanList_Servers = strzone(OnlineBanList_Servers);
-       
+
        uri = strcat(     "action=list&hostname=", uri_escape(autocvar_hostname));
        uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
 
@@ -227,7 +240,7 @@ void OnlineBanList_Think()
                else
                        uri_get(strcat(argv(i), "?", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
        }
-       
+
        if(autocvar_g_ban_sync_interval > 0)
                self.nextthink = time + max(60, autocvar_g_ban_sync_interval * 60);
        else
@@ -238,7 +251,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 +289,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 +302,7 @@ float Ban_Delete(float i)
        ban_expire[i] = 0;
        ban_ip[i] = "";
        Ban_SaveBans();
-       return TRUE;
+       return true;
 }
 
 void Ban_LoadBans()
@@ -298,7 +311,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)
        {
@@ -321,23 +334,24 @@ void Ban_View()
 {
        float i, n;
        string msg;
-       
+
        print("^2Listing all existing active bans:\n");
-       
+
+       n = 0;
        for(i = 0; i < ban_count; ++i)
        {
                if(time > ban_expire[i])
                        continue;
-                       
+
                ++n; // total number of existing bans
-                       
+
                msg = strcat("#", ftos(i), ": ");
                msg = strcat(msg, ban_ip[i], " is still banned for ");
                msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
-               
+
                print("  ", msg, "\n");
        }
-       
+
        print("^2Done listing all active (", ftos(n), ") bans.\n");
 }
 
@@ -359,33 +373,33 @@ 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);
-       
+
        ban_ip1 = substring(s, 0, i1); // 8
        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
@@ -396,7 +410,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)
@@ -405,7 +419,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;
@@ -416,23 +430,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 || !ban_idfp)
-                       return TRUE;
-       return FALSE;
+       {
+               if(!autocvar_g_banned_list_idmode)
+                       return true;
+               if (!ban_idfp)
+                       return true;
+       }
+       return false;
 }
 
 float Ban_MaybeEnforceBan(entity client)
@@ -443,9 +461,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(client);
 }
 
 string Ban_Enforce(float i, string reason)
@@ -455,7 +482,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 != "")
@@ -466,7 +494,7 @@ string Ban_Enforce(float i, string reason)
                                        reason = strcat(reason, ", ");
                                reason = strcat(reason, e.netname);
                        }
-                       s = strcat(s, "^1NOTE:^7 banned client ", e.netname, "^7 has to go\n");
+                       s = strcat(s, "^1NOTE:^7 banned client ", e.netaddress, "^7 has to go\n");
                        dropclient(e);
                }
        bprint(s);
@@ -502,7 +530,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?
@@ -529,7 +557,7 @@ float Ban_Insert(string ip, float bantime, string reason, float dosync)
        if(ban_expire[i] > time + bantime)
        {
                print(ip, " could not get banned due to no free ban slot\n");
-               return FALSE;
+               return false;
        }
        // okay, insert our new victim as i
        Ban_Delete(i);
@@ -548,7 +576,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)