4 #include "../dpdefs/progsdefs.qh"
5 #include "../dpdefs/dpextensions.qh"
7 #include "../common/constants.qh"
8 #include "../common/util.qh"
9 #include "autocvars.qh"
11 #include "command/banning.qh"
16 * Protocol of online ban list:
19 * GET g_ban_sync_uri?action=ban&hostname=...&ip=xxx.xxx.xxx&duration=nnnn&reason=...................
20 * (IP 1, 2, 3, or 4 octets, 3 octets for example is a /24 mask)
22 * GET g_ban_sync_uri?action=unban&hostname=...&ip=xxx.xxx.xxx
23 * - Querying the ban list
24 * GET g_ban_sync_uri?action=list&hostname=...&servers=xxx.xxx.xxx.xxx;xxx.xxx.xxx.xxx;...
26 * shows the bans from the listed servers, and possibly others.
27 * Format of a ban is ASCII plain text, four lines per ban, delimited by
28 * newline ONLY (no carriage return):
30 * IP address (also 1, 2, 3, or 4 octets, delimited by dot)
31 * time left in seconds
33 * server IP that registered the ban
36 #define MAX_IPBAN_URIS (URI_GET_IPBAN_END - URI_GET_IPBAN + 1)
38 float Ban_Insert(string ip, float bantime, string reason, float dosync);
40 void OnlineBanList_SendBan(string ip, float bantime, string reason)
45 uri = strcat( "action=ban&hostname=", uri_escape(autocvar_hostname));
46 uri = strcat(uri, "&ip=", uri_escape(ip));
47 uri = strcat(uri, "&duration=", ftos(bantime));
48 uri = strcat(uri, "&reason=", uri_escape(reason));
50 n = tokenize_console(autocvar_g_ban_sync_uri);
51 if(n >= MAX_IPBAN_URIS)
53 for(i = 0; i < n; ++i)
55 if(strstrofs(argv(i), "?", 0) >= 0)
56 uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
58 uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
62 void OnlineBanList_SendUnban(string ip)
67 uri = strcat( "action=unban&hostname=", uri_escape(autocvar_hostname));
68 uri = strcat(uri, "&ip=", uri_escape(ip));
70 n = tokenize_console(autocvar_g_ban_sync_uri);
71 if(n >= MAX_IPBAN_URIS)
73 for(i = 0; i < n; ++i)
75 if(strstrofs(argv(i), "?", 0) >= 0)
76 uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
78 uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
82 string OnlineBanList_Servers;
83 float OnlineBanList_Timeout;
84 float OnlineBanList_RequestWaiting[MAX_IPBAN_URIS];
86 void OnlineBanList_URI_Get_Callback(float id, float status, string data)
98 if(id >= MAX_IPBAN_URIS)
100 print("Received ban list for invalid ID\n");
104 tokenize_console(autocvar_g_ban_sync_uri);
107 print("Received ban list from ", uri, ": ");
109 if(OnlineBanList_RequestWaiting[id] == 0)
111 print("rejected (unexpected)\n");
115 OnlineBanList_RequestWaiting[id] = 0;
117 if(time > OnlineBanList_Timeout)
119 print("rejected (too late)\n");
123 syncinterval = autocvar_g_ban_sync_interval;
124 if(syncinterval == 0)
126 print("rejected (syncing disabled)\n");
134 print("error: status is ", ftos(status), "\n");
138 if(substring(data, 0, 1) == "<")
140 print("error: received HTML instead of a ban list\n");
144 if(strstrofs(data, "\r", 0) != -1)
146 print("error: received carriage returns\n");
153 n = tokenizebyseparator(data, "\n");
157 print("error: received invalid item count: ", ftos(n), "\n");
161 print("OK, ", ftos(n / 4), " items\n");
163 for(i = 0; i < n; i += 4)
166 timeleft = stof(argv(i + 1));
167 reason = argv(i + 2);
168 serverip = argv(i + 3);
170 dprint("received ban list item ", ftos(i / 4), ": ip=", ip);
171 dprint(" timeleft=", ftos(timeleft), " reason=", reason);
172 dprint(" serverip=", serverip, "\n");
174 timeleft -= 1.5 * autocvar_g_ban_sync_timeout;
179 if(l != 44) // length 44 is a cryptographic ID
181 for(j = 0; j < l; ++j)
182 if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
184 print("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.\n");
189 if(autocvar_g_ban_sync_trusted_servers_verify)
190 if((strstrofs(strcat(";", OnlineBanList_Servers, ";"), strcat(";", serverip, ";"), 0) == -1))
194 timeleft = min(syncinterval + (OnlineBanList_Timeout - time) + 5, timeleft);
195 // the ban will be prolonged on the next sync
196 // or expire 5 seconds after the next timeout
197 Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip, " at ", uri), 0);
198 print("Ban list syncing: accepted ban of ", ip, " by ", serverip, " at ", uri, ": ");
205 void OnlineBanList_Think()
211 if(autocvar_g_ban_sync_uri == "")
213 if(autocvar_g_ban_sync_interval == 0) // < 0 is okay, it means "sync on level start only"
215 argc = tokenize_console(autocvar_g_ban_sync_trusted_servers);
219 if(OnlineBanList_Servers)
220 strunzone(OnlineBanList_Servers);
221 OnlineBanList_Servers = argv(0);
222 for(i = 1; i < argc; ++i)
223 OnlineBanList_Servers = strcat(OnlineBanList_Servers, ";", argv(i));
224 OnlineBanList_Servers = strzone(OnlineBanList_Servers);
226 uri = strcat( "action=list&hostname=", uri_escape(autocvar_hostname));
227 uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
229 OnlineBanList_Timeout = time + autocvar_g_ban_sync_timeout;
231 n = tokenize_console(autocvar_g_ban_sync_uri);
232 if(n >= MAX_IPBAN_URIS)
234 for(i = 0; i < n; ++i)
236 if(OnlineBanList_RequestWaiting[i])
238 OnlineBanList_RequestWaiting[i] = 1;
239 if(strstrofs(argv(i), "?", 0) >= 0)
240 uri_get(strcat(argv(i), "&", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
242 uri_get(strcat(argv(i), "?", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
245 if(autocvar_g_ban_sync_interval > 0)
246 self.nextthink = time + max(60, autocvar_g_ban_sync_interval * 60);
255 const float BAN_MAX = 256;
257 string ban_ip[BAN_MAX];
258 float ban_expire[BAN_MAX];
277 for(i = 0; i < ban_count; ++i)
279 if(time > ban_expire[i])
281 out = strcat(out, " ", ban_ip[i]);
282 out = strcat(out, " ", ftos(ban_expire[i] - time));
284 if(strlen(out) <= 1) // no real entries
285 cvar_set("g_banned_list", "");
287 cvar_set("g_banned_list", out);
290 float Ban_Delete(float i)
296 if(ban_expire[i] == 0)
298 if(ban_expire[i] > 0)
300 OnlineBanList_SendUnban(ban_ip[i]);
301 strunzone(ban_ip[i]);
312 for(i = 0; i < ban_count; ++i)
316 n = tokenize_console(autocvar_g_banned_list);
317 if(stof(argv(0)) == 1)
319 ban_count = (n - 1) / 2;
320 for(i = 0; i < ban_count; ++i)
322 ban_ip[i] = strzone(argv(2*i+1));
323 ban_expire[i] = time + stof(argv(2*i+2));
329 e.classname = "bansyncer";
330 e.think = OnlineBanList_Think;
331 e.nextthink = time + 1;
339 print("^2Listing all existing active bans:\n");
342 for(i = 0; i < ban_count; ++i)
344 if(time > ban_expire[i])
347 ++n; // total number of existing bans
349 msg = strcat("#", ftos(i), ": ");
350 msg = strcat(msg, ban_ip[i], " is still banned for ");
351 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
353 print(" ", msg, "\n");
356 print("^2Done listing all active (", ftos(n), ") bans.\n");
359 float Ban_GetClientIP(entity client)
361 // we can't use tokenizing here, as this is called during ban list parsing
362 float i1, i2, i3, i4;
365 if(client.crypto_keyfp)
366 ban_idfp = client.crypto_idfp;
368 ban_idfp = string_null;
370 s = client.netaddress;
372 i1 = strstrofs(s, ".", 0);
375 i2 = strstrofs(s, ".", i1 + 1);
378 i3 = strstrofs(s, ".", i2 + 1);
381 i4 = strstrofs(s, ".", i3 + 1);
383 s = substring(s, 0, i4);
385 ban_ip1 = substring(s, 0, i1); // 8
386 ban_ip2 = substring(s, 0, i2); // 16
387 ban_ip3 = substring(s, 0, i3); // 24
388 ban_ip4 = strcat1(s); // 32
392 i1 = strstrofs(s, ":", 0);
395 i1 = strstrofs(s, ":", i1 + 1);
398 i2 = strstrofs(s, ":", i1 + 1);
401 i3 = strstrofs(s, ":", i2 + 1);
405 ban_ip1 = strcat(substring(s, 0, i1), "::/32"); // 32
406 ban_ip2 = strcat(substring(s, 0, i2), "::/48"); // 48
407 ban_ip4 = strcat(substring(s, 0, i3), "::/64"); // 64
409 if(i3 - i2 > 3) // means there is more than 2 digits and a : in the range
410 ban_ip3 = strcat(substring(s, 0, i2), ":", substring(s, i2 + 1, i3 - i2 - 3), "00::/56");
412 ban_ip3 = strcat(substring(s, 0, i2), ":0::/56");
417 float Ban_IsClientBanned(entity client, float idx)
419 float i, b, e, ipbanned;
422 if(!Ban_GetClientIP(client))
435 for(i = b; i < e; ++i)
438 if(time > ban_expire[i])
441 if(ban_ip1 == s) ipbanned = true;
442 if(ban_ip2 == s) ipbanned = true;
443 if(ban_ip3 == s) ipbanned = true;
444 if(ban_ip4 == s) ipbanned = true;
445 if(ban_idfp == s) return true;
449 if(!autocvar_g_banned_list_idmode)
457 float Ban_MaybeEnforceBan(entity client)
459 if(Ban_IsClientBanned(client, -1))
462 s = strcat("^1NOTE:^7 banned client ", client.netaddress, " just tried to enter\n");
471 float Ban_MaybeEnforceBanOnce(entity client)
473 if(client.ban_checked)
475 client.ban_checked = true;
476 return Ban_MaybeEnforceBan(self);
479 string Ban_Enforce(float i, string reason)
484 // Enforce our new ban
486 FOR_EACH_REALCLIENT(e)
487 if(Ban_IsClientBanned(e, i))
492 reason = strcat(reason, ": affects ");
494 reason = strcat(reason, ", ");
495 reason = strcat(reason, e.netname);
497 s = strcat(s, "^1NOTE:^7 banned client ", e.netaddress, "^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 dprint(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now\n");
522 dprint(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now\n");
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 him (so long-term
555 // bans never get overridden by short-term bans)
557 if(ban_expire[i] > time + bantime)
559 print(ip, " could not get banned due to no free ban slot\n");
562 // okay, insert our new victim as i
564 dprint(ip, " has been banned for ", ftos(bantime), " seconds\n");
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"));