3 #include <common/constants.qh>
4 #include <common/stats.qh>
5 #include <common/util.qh>
6 #include <common/weapons/_all.qh>
7 #include <server/command/banning.qh>
8 #include <server/main.qh>
11 * Protocol of online ban list:
14 * GET g_ban_sync_uri?action=ban&hostname=...&ip=xxx.xxx.xxx&duration=nnnn&reason=...................
15 * (IP 1, 2, 3, or 4 octets, 3 octets for example is a /24 mask)
17 * GET g_ban_sync_uri?action=unban&hostname=...&ip=xxx.xxx.xxx
18 * - Querying the ban list
19 * GET g_ban_sync_uri?action=list&hostname=...&servers=xxx.xxx.xxx.xxx;xxx.xxx.xxx.xxx;...
21 * shows the bans from the listed servers, and possibly others.
22 * Format of a ban is ASCII plain text, four lines per ban, delimited by
23 * newline ONLY (no carriage return):
25 * IP address (also 1, 2, 3, or 4 octets, delimited by dot)
26 * time left in seconds
28 * server IP that registered the ban
31 #define MAX_IPBAN_URIS (URI_GET_IPBAN_END - URI_GET_IPBAN + 1)
33 void OnlineBanList_SendBan(string ip, float bantime, string reason)
38 uri = strcat( "action=ban&hostname=", uri_escape(autocvar_hostname));
39 uri = strcat(uri, "&ip=", uri_escape(ip));
40 uri = strcat(uri, "&duration=", ftos(bantime));
41 uri = strcat(uri, "&reason=", uri_escape(reason));
43 n = tokenize_console(autocvar_g_ban_sync_uri);
44 if(n >= MAX_IPBAN_URIS)
46 for(i = 0; i < n; ++i)
48 if(strstrofs(argv(i), "?", 0) >= 0)
49 uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
51 uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
55 void OnlineBanList_SendUnban(string ip)
60 uri = strcat( "action=unban&hostname=", uri_escape(autocvar_hostname));
61 uri = strcat(uri, "&ip=", uri_escape(ip));
63 n = tokenize_console(autocvar_g_ban_sync_uri);
64 if(n >= MAX_IPBAN_URIS)
66 for(i = 0; i < n; ++i)
68 if(strstrofs(argv(i), "?", 0) >= 0)
69 uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
71 uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
75 string OnlineBanList_Servers;
76 float OnlineBanList_Timeout;
77 float OnlineBanList_RequestWaiting[MAX_IPBAN_URIS];
79 void OnlineBanList_URI_Get_Callback(float id, float status, string data)
91 if(id >= MAX_IPBAN_URIS)
93 LOG_INFO("Received ban list for invalid ID");
97 tokenize_console(autocvar_g_ban_sync_uri);
100 string prelude = strcat("Received ban list from ", uri, ": ");
102 if(OnlineBanList_RequestWaiting[id] == 0)
104 LOG_INFO(prelude, "rejected (unexpected)");
108 OnlineBanList_RequestWaiting[id] = 0;
110 if(time > OnlineBanList_Timeout)
112 LOG_INFO(prelude, "rejected (too late)");
116 syncinterval = autocvar_g_ban_sync_interval;
117 if(syncinterval == 0)
119 LOG_INFO(prelude, "rejected (syncing disabled)");
127 LOG_INFO(prelude, "error: status is ", ftos(status));
131 if(substring(data, 0, 1) == "<")
133 LOG_INFO(prelude, "error: received HTML instead of a ban list");
137 if(strstrofs(data, "\r", 0) != -1)
139 LOG_INFO(prelude, "error: received carriage returns");
146 n = tokenizebyseparator(data, "\n");
150 LOG_INFO(prelude, "error: received invalid item count: ", ftos(n));
154 LOG_INFO(prelude, "OK, ", ftos(n / 4), " items");
156 for(i = 0; i < n; i += 4)
159 timeleft = stof(argv(i + 1));
160 reason = argv(i + 2);
161 serverip = argv(i + 3);
163 LOG_TRACE("received ban list item ", ftos(i / 4), ": ip=", ip);
164 LOG_TRACE(" timeleft=", ftos(timeleft), " reason=", reason);
165 LOG_TRACE(" serverip=", serverip);
167 timeleft -= 1.5 * autocvar_g_ban_sync_timeout;
172 if(l != 44) // length 44 is a cryptographic ID
174 for(j = 0; j < l; ++j)
175 if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
177 LOG_INFO("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.");
182 if(autocvar_g_ban_sync_trusted_servers_verify)
183 if((strstrofs(strcat(";", OnlineBanList_Servers, ";"), strcat(";", serverip, ";"), 0) == -1))
187 timeleft = min(syncinterval + (OnlineBanList_Timeout - time) + 5, timeleft);
188 // the ban will be prolonged on the next sync
189 // or expire 5 seconds after the next timeout
190 Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip, " at ", uri), 0);
191 LOG_INFO("Ban list syncing: accepted ban of ", ip, " by ", serverip, " at ", uri, ": ", reason);
197 void OnlineBanList_Think(entity this)
203 if(autocvar_g_ban_sync_uri == "")
208 if(autocvar_g_ban_sync_interval == 0) // < 0 is okay, it means "sync on level start only"
213 argc = tokenize_console(autocvar_g_ban_sync_trusted_servers);
220 string s = argv(0); for(i = 1; i < argc; ++i) s = strcat(s, ";", argv(i));
221 strcpy(OnlineBanList_Servers, s);
223 uri = strcat( "action=list&hostname=", uri_escape(autocvar_hostname));
224 uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
226 OnlineBanList_Timeout = time + autocvar_g_ban_sync_timeout;
228 n = tokenize_console(autocvar_g_ban_sync_uri);
229 if(n >= MAX_IPBAN_URIS)
231 for(i = 0; i < n; ++i)
233 if(OnlineBanList_RequestWaiting[i])
235 OnlineBanList_RequestWaiting[i] = 1;
236 if(strstrofs(argv(i), "?", 0) >= 0)
237 uri_get(strcat(argv(i), "&", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
239 uri_get(strcat(argv(i), "?", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
242 if(autocvar_g_ban_sync_interval <= 0)
248 this.nextthink = time + max(60, autocvar_g_ban_sync_interval * 60);
251 const float BAN_MAX = 256;
253 string ban_ip[BAN_MAX];
254 float ban_expire[BAN_MAX];
273 for(i = 0; i < ban_count; ++i)
275 if(time > ban_expire[i])
277 out = strcat(out, " ", ban_ip[i]);
278 out = strcat(out, " ", ftos(ban_expire[i] - time));
280 if(strlen(out) <= 1) // no real entries
281 cvar_set("g_banned_list", "");
283 cvar_set("g_banned_list", out);
286 float Ban_Delete(float i)
292 if(ban_expire[i] == 0)
294 if(ban_expire[i] > 0)
296 OnlineBanList_SendUnban(ban_ip[i]);
297 strunzone(ban_ip[i]);
308 for(i = 0; i < ban_count; ++i)
312 n = tokenize_console(autocvar_g_banned_list);
313 if(stof(argv(0)) == 1)
315 ban_count = (n - 1) / 2;
316 for(i = 0; i < ban_count; ++i)
318 ban_ip[i] = strzone(argv(2*i+1));
319 ban_expire[i] = time + stof(argv(2*i+2));
323 entity e = new(bansyncer);
324 setthink(e, OnlineBanList_Think);
325 e.nextthink = time + 1;
333 LOG_INFO("^2Listing all existing active bans:");
336 for(i = 0; i < ban_count; ++i)
338 if(time > ban_expire[i])
341 ++n; // total number of existing bans
343 msg = strcat("#", ftos(i), ": ");
344 msg = strcat(msg, ban_ip[i], " is still banned for ");
345 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
350 LOG_INFO("^2Done listing all active (", ftos(n), ") bans.");
353 float Ban_GetClientIP(entity client)
355 // we can't use tokenizing here, as this is called during ban list parsing
356 float i1, i2, i3, i4;
359 if(client.crypto_idfp_signed)
360 ban_idfp = client.crypto_idfp;
362 ban_idfp = string_null;
364 s = client.netaddress;
366 i1 = strstrofs(s, ".", 0);
369 i2 = strstrofs(s, ".", i1 + 1);
372 i3 = strstrofs(s, ".", i2 + 1);
375 i4 = strstrofs(s, ".", i3 + 1);
377 s = substring(s, 0, i4);
379 ban_ip1 = substring(s, 0, i1); // 8
380 ban_ip2 = substring(s, 0, i2); // 16
381 ban_ip3 = substring(s, 0, i3); // 24
382 ban_ip4 = strcat1(s); // 32
386 i1 = strstrofs(s, ":", 0);
389 i1 = strstrofs(s, ":", i1 + 1);
392 i2 = strstrofs(s, ":", i1 + 1);
395 i3 = strstrofs(s, ":", i2 + 1);
399 ban_ip1 = strcat(substring(s, 0, i1), "::/32"); // 32
400 ban_ip2 = strcat(substring(s, 0, i2), "::/48"); // 48
401 ban_ip4 = strcat(substring(s, 0, i3), "::/64"); // 64
403 if(i3 - i2 > 3) // means there is more than 2 digits and a : in the range
404 ban_ip3 = strcat(substring(s, 0, i2), ":", substring(s, i2 + 1, i3 - i2 - 3), "00::/56");
406 ban_ip3 = strcat(substring(s, 0, i2), ":0::/56");
411 float Ban_IsClientBanned(entity client, float idx)
413 float i, b, e, ipbanned;
416 if(!Ban_GetClientIP(client))
429 for(i = b; i < e; ++i)
432 if(time > ban_expire[i])
435 if(ban_ip1 == s) ipbanned = true;
436 if(ban_ip2 == s) ipbanned = true;
437 if(ban_ip3 == s) ipbanned = true;
438 if(ban_ip4 == s) ipbanned = true;
439 if(ban_idfp == s) return true;
443 if(!autocvar_g_banned_list_idmode)
451 bool Ban_MaybeEnforceBan(entity client)
453 if (Ban_IsClientBanned(client, -1))
455 if (!client.crypto_idfp)
456 LOG_INFOF("^1NOTE:^7 banned client %s just tried to enter\n",
459 LOG_INFOF("^1NOTE:^7 banned client %s (%s) just tried to enter\n",
460 client.netaddress, client.crypto_idfp);
462 if(autocvar_g_ban_telluser)
463 sprint(client, "You are banned from this server.\n");
471 bool Ban_MaybeEnforceBanOnce(entity client)
473 if (client.ban_checked) return false;
474 client.ban_checked = true;
475 return Ban_MaybeEnforceBan(client);
478 string Ban_Enforce(float j, string reason)
482 // Enforce our new ban
484 FOREACH_CLIENTSLOT(IS_REAL_CLIENT(it),
486 if(Ban_IsClientBanned(it, j))
491 reason = strcat(reason, ": affects ");
493 reason = strcat(reason, ", ");
494 reason = strcat(reason, it.netname);
496 s = strcat(s, "^1NOTE:^7 banned client ", it.netname, "^7 has to go\n");
505 float Ban_Insert(string ip, float bantime, string reason, float dosync)
512 for(i = 0; i < ban_count; ++i)
516 if(time + bantime > ban_expire[i])
518 ban_expire[i] = time + bantime;
519 LOG_TRACE(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now");
522 LOG_TRACE(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now");
525 reason = Ban_Enforce(i, reason);
530 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
531 OnlineBanList_SendBan(ip, bantime, reason);
536 // do we have a free slot?
537 for(i = 0; i < ban_count; ++i)
538 if(time > ban_expire[i])
540 // no free slot? Then look for the one who would get unbanned next
544 bestscore = ban_expire[i];
545 for(j = 1; j < ban_count; ++j)
547 if(ban_expire[j] < bestscore)
550 bestscore = ban_expire[i];
554 // if we replace someone, will we be banned longer than them (so long-term
555 // bans never get overridden by short-term bans)
557 if(ban_expire[i] > time + bantime)
559 LOG_INFO(ip, " could not get banned due to no free ban slot");
562 // okay, insert our new victim as i
564 LOG_TRACE(ip, " has been banned for ", ftos(bantime), " seconds");
565 ban_expire[i] = time + bantime;
566 ban_ip[i] = strzone(ip);
567 ban_count = max(ban_count, i + 1);
571 reason = Ban_Enforce(i, reason);
576 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
577 OnlineBanList_SendBan(ip, bantime, reason);
582 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
585 if(!Ban_GetClientIP(client))
587 sprint(client, strcat("Kickbanned: ", reason, "\n"));
596 ip = strcat1(ban_ip1);
599 ip = strcat1(ban_ip2);
602 ip = strcat1(ban_ip3);
606 ip = strcat1(ban_ip4);
610 id = strcat1(ban_idfp);
614 Ban_Insert(ip, bantime, reason, 1);
616 Ban_Insert(id, bantime, reason, 1);
618 * not needed, as we enforce the ban in Ban_Insert anyway
620 sprint(client, strcat("Kickbanned: ", reason, "\n"));