3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5 #include "autocvars.qh"
6 #include "command/banning.qh"
8 #include "../common/constants.qh"
9 #include "../common/util.qh"
12 * Protocol of online ban list:
15 * GET g_ban_sync_uri?action=ban&hostname=...&ip=xxx.xxx.xxx&duration=nnnn&reason=...................
16 * (IP 1, 2, 3, or 4 octets, 3 octets for example is a /24 mask)
18 * GET g_ban_sync_uri?action=unban&hostname=...&ip=xxx.xxx.xxx
19 * - Querying the ban list
20 * GET g_ban_sync_uri?action=list&hostname=...&servers=xxx.xxx.xxx.xxx;xxx.xxx.xxx.xxx;...
22 * shows the bans from the listed servers, and possibly others.
23 * Format of a ban is ASCII plain text, four lines per ban, delimited by
24 * newline ONLY (no carriage return):
26 * IP address (also 1, 2, 3, or 4 octets, delimited by dot)
27 * time left in seconds
29 * server IP that registered the ban
32 #define MAX_IPBAN_URIS (URI_GET_IPBAN_END - URI_GET_IPBAN + 1)
34 float Ban_Insert(string ip, float bantime, string reason, float dosync);
36 void OnlineBanList_SendBan(string ip, float bantime, string reason)
41 uri = strcat( "action=ban&hostname=", uri_escape(autocvar_hostname));
42 uri = strcat(uri, "&ip=", uri_escape(ip));
43 uri = strcat(uri, "&duration=", ftos(bantime));
44 uri = strcat(uri, "&reason=", uri_escape(reason));
46 n = tokenize_console(autocvar_g_ban_sync_uri);
47 if(n >= MAX_IPBAN_URIS)
49 for(i = 0; i < n; ++i)
51 if(strstrofs(argv(i), "?", 0) >= 0)
52 uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
54 uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
58 void OnlineBanList_SendUnban(string ip)
63 uri = strcat( "action=unban&hostname=", uri_escape(autocvar_hostname));
64 uri = strcat(uri, "&ip=", uri_escape(ip));
66 n = tokenize_console(autocvar_g_ban_sync_uri);
67 if(n >= MAX_IPBAN_URIS)
69 for(i = 0; i < n; ++i)
71 if(strstrofs(argv(i), "?", 0) >= 0)
72 uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
74 uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
78 string OnlineBanList_Servers;
79 float OnlineBanList_Timeout;
80 float OnlineBanList_RequestWaiting[MAX_IPBAN_URIS];
82 void OnlineBanList_URI_Get_Callback(float id, float status, string data)
94 if(id >= MAX_IPBAN_URIS)
96 LOG_INFO("Received ban list for invalid ID");
100 tokenize_console(autocvar_g_ban_sync_uri);
103 string prelude = strcat("Received ban list from ", uri, ": ");
105 if(OnlineBanList_RequestWaiting[id] == 0)
107 LOG_INFO(prelude, "rejected (unexpected)");
111 OnlineBanList_RequestWaiting[id] = 0;
113 if(time > OnlineBanList_Timeout)
115 LOG_INFO(prelude, "rejected (too late)");
119 syncinterval = autocvar_g_ban_sync_interval;
120 if(syncinterval == 0)
122 LOG_INFO(prelude, "rejected (syncing disabled)");
130 LOG_INFO(prelude, "error: status is ", ftos(status));
134 if(substring(data, 0, 1) == "<")
136 LOG_INFO(prelude, "error: received HTML instead of a ban list");
140 if(strstrofs(data, "\r", 0) != -1)
142 LOG_INFO(prelude, "error: received carriage returns");
149 n = tokenizebyseparator(data, "\n");
153 LOG_INFO(prelude, "error: received invalid item count: ", ftos(n));
157 LOG_INFO(prelude, "OK, ", ftos(n / 4), " items");
159 for(i = 0; i < n; i += 4)
162 timeleft = stof(argv(i + 1));
163 reason = argv(i + 2);
164 serverip = argv(i + 3);
166 LOG_TRACE("received ban list item ", ftos(i / 4), ": ip=", ip);
167 LOG_TRACE(" timeleft=", ftos(timeleft), " reason=", reason);
168 LOG_TRACE(" serverip=", serverip);
170 timeleft -= 1.5 * autocvar_g_ban_sync_timeout;
175 if(l != 44) // length 44 is a cryptographic ID
177 for(j = 0; j < l; ++j)
178 if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
180 LOG_INFO("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.");
185 if(autocvar_g_ban_sync_trusted_servers_verify)
186 if((strstrofs(strcat(";", OnlineBanList_Servers, ";"), strcat(";", serverip, ";"), 0) == -1))
190 timeleft = min(syncinterval + (OnlineBanList_Timeout - time) + 5, timeleft);
191 // the ban will be prolonged on the next sync
192 // or expire 5 seconds after the next timeout
193 Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip, " at ", uri), 0);
194 LOG_INFO("Ban list syncing: accepted ban of ", ip, " by ", serverip, " at ", uri, ": ", reason);
200 void OnlineBanList_Think(entity this)
206 if(autocvar_g_ban_sync_uri == "")
208 if(autocvar_g_ban_sync_interval == 0) // < 0 is okay, it means "sync on level start only"
210 argc = tokenize_console(autocvar_g_ban_sync_trusted_servers);
214 string s = argv(0); for(i = 1; i < argc; ++i) s = strcat(s, ";", argv(i));
215 strcpy(OnlineBanList_Servers, s);
217 uri = strcat( "action=list&hostname=", uri_escape(autocvar_hostname));
218 uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
220 OnlineBanList_Timeout = time + autocvar_g_ban_sync_timeout;
222 n = tokenize_console(autocvar_g_ban_sync_uri);
223 if(n >= MAX_IPBAN_URIS)
225 for(i = 0; i < n; ++i)
227 if(OnlineBanList_RequestWaiting[i])
229 OnlineBanList_RequestWaiting[i] = 1;
230 if(strstrofs(argv(i), "?", 0) >= 0)
231 uri_get(strcat(argv(i), "&", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
233 uri_get(strcat(argv(i), "?", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
236 if(autocvar_g_ban_sync_interval > 0)
237 this.nextthink = time + max(60, autocvar_g_ban_sync_interval * 60);
246 const float BAN_MAX = 256;
248 string ban_ip[BAN_MAX];
249 float ban_expire[BAN_MAX];
268 for(i = 0; i < ban_count; ++i)
270 if(time > ban_expire[i])
272 out = strcat(out, " ", ban_ip[i]);
273 out = strcat(out, " ", ftos(ban_expire[i] - time));
275 if(strlen(out) <= 1) // no real entries
276 cvar_set("g_banned_list", "");
278 cvar_set("g_banned_list", out);
281 float Ban_Delete(float i)
287 if(ban_expire[i] == 0)
289 if(ban_expire[i] > 0)
291 OnlineBanList_SendUnban(ban_ip[i]);
292 strunzone(ban_ip[i]);
303 for(i = 0; i < ban_count; ++i)
307 n = tokenize_console(autocvar_g_banned_list);
308 if(stof(argv(0)) == 1)
310 ban_count = (n - 1) / 2;
311 for(i = 0; i < ban_count; ++i)
313 ban_ip[i] = strzone(argv(2*i+1));
314 ban_expire[i] = time + stof(argv(2*i+2));
318 entity e = new(bansyncer);
319 setthink(e, OnlineBanList_Think);
320 e.nextthink = time + 1;
328 LOG_INFO("^2Listing all existing active bans:");
331 for(i = 0; i < ban_count; ++i)
333 if(time > ban_expire[i])
336 ++n; // total number of existing bans
338 msg = strcat("#", ftos(i), ": ");
339 msg = strcat(msg, ban_ip[i], " is still banned for ");
340 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
345 LOG_INFO("^2Done listing all active (", ftos(n), ") bans.");
348 float Ban_GetClientIP(entity client)
350 // we can't use tokenizing here, as this is called during ban list parsing
351 float i1, i2, i3, i4;
354 if(client.crypto_idfp_signed)
355 ban_idfp = client.crypto_idfp;
357 ban_idfp = string_null;
359 s = client.netaddress;
361 i1 = strstrofs(s, ".", 0);
364 i2 = strstrofs(s, ".", i1 + 1);
367 i3 = strstrofs(s, ".", i2 + 1);
370 i4 = strstrofs(s, ".", i3 + 1);
372 s = substring(s, 0, i4);
374 ban_ip1 = substring(s, 0, i1); // 8
375 ban_ip2 = substring(s, 0, i2); // 16
376 ban_ip3 = substring(s, 0, i3); // 24
377 ban_ip4 = strcat1(s); // 32
381 i1 = strstrofs(s, ":", 0);
384 i1 = strstrofs(s, ":", i1 + 1);
387 i2 = strstrofs(s, ":", i1 + 1);
390 i3 = strstrofs(s, ":", i2 + 1);
394 ban_ip1 = strcat(substring(s, 0, i1), "::/32"); // 32
395 ban_ip2 = strcat(substring(s, 0, i2), "::/48"); // 48
396 ban_ip4 = strcat(substring(s, 0, i3), "::/64"); // 64
398 if(i3 - i2 > 3) // means there is more than 2 digits and a : in the range
399 ban_ip3 = strcat(substring(s, 0, i2), ":", substring(s, i2 + 1, i3 - i2 - 3), "00::/56");
401 ban_ip3 = strcat(substring(s, 0, i2), ":0::/56");
406 float Ban_IsClientBanned(entity client, float idx)
408 float i, b, e, ipbanned;
411 if(!Ban_GetClientIP(client))
424 for(i = b; i < e; ++i)
427 if(time > ban_expire[i])
430 if(ban_ip1 == s) ipbanned = true;
431 if(ban_ip2 == s) ipbanned = true;
432 if(ban_ip3 == s) ipbanned = true;
433 if(ban_ip4 == s) ipbanned = true;
434 if(ban_idfp == s) return true;
438 if(!autocvar_g_banned_list_idmode)
446 bool Ban_MaybeEnforceBan(entity client)
448 if (Ban_IsClientBanned(client, -1))
450 string s = sprintf("^1NOTE:^7 banned client %s just tried to enter\n", client.netaddress);
459 bool Ban_MaybeEnforceBanOnce(entity client)
461 if (client.ban_checked) return false;
462 client.ban_checked = true;
463 return Ban_MaybeEnforceBan(client);
466 string Ban_Enforce(float j, string reason)
470 // Enforce our new ban
472 FOREACH_CLIENTSLOT(IS_REAL_CLIENT(it),
474 if(Ban_IsClientBanned(it, j))
479 reason = strcat(reason, ": affects ");
481 reason = strcat(reason, ", ");
482 reason = strcat(reason, it.netname);
484 s = strcat(s, "^1NOTE:^7 banned client ", it.netaddress, "^7 has to go\n");
493 float Ban_Insert(string ip, float bantime, string reason, float dosync)
500 for(i = 0; i < ban_count; ++i)
504 if(time + bantime > ban_expire[i])
506 ban_expire[i] = time + bantime;
507 LOG_TRACE(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now");
510 LOG_TRACE(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now");
513 reason = Ban_Enforce(i, reason);
518 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
519 OnlineBanList_SendBan(ip, bantime, reason);
524 // do we have a free slot?
525 for(i = 0; i < ban_count; ++i)
526 if(time > ban_expire[i])
528 // no free slot? Then look for the one who would get unbanned next
532 bestscore = ban_expire[i];
533 for(j = 1; j < ban_count; ++j)
535 if(ban_expire[j] < bestscore)
538 bestscore = ban_expire[i];
542 // if we replace someone, will we be banned longer than him (so long-term
543 // bans never get overridden by short-term bans)
545 if(ban_expire[i] > time + bantime)
547 LOG_INFO(ip, " could not get banned due to no free ban slot");
550 // okay, insert our new victim as i
552 LOG_TRACE(ip, " has been banned for ", ftos(bantime), " seconds");
553 ban_expire[i] = time + bantime;
554 ban_ip[i] = strzone(ip);
555 ban_count = max(ban_count, i + 1);
559 reason = Ban_Enforce(i, reason);
564 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
565 OnlineBanList_SendBan(ip, bantime, reason);
570 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
573 if(!Ban_GetClientIP(client))
575 sprint(client, strcat("Kickbanned: ", reason, "\n"));
584 ip = strcat1(ban_ip1);
587 ip = strcat1(ban_ip2);
590 ip = strcat1(ban_ip3);
594 ip = strcat1(ban_ip4);
598 id = strcat1(ban_idfp);
602 Ban_Insert(ip, bantime, reason, 1);
604 Ban_Insert(id, bantime, reason, 1);
606 * not needed, as we enforce the ban in Ban_Insert anyway
608 sprint(client, strcat("Kickbanned: ", reason, "\n"));