4 #include "../dpdefs/progsdefs.qh"
5 #include "../dpdefs/dpextensions.qh"
6 #include "../common/constants.qh"
7 #include "../common/util.qh"
8 #include "autocvars.qh"
10 #include "command/banning.qh"
15 * Protocol of online ban list:
18 * GET g_ban_sync_uri?action=ban&hostname=...&ip=xxx.xxx.xxx&duration=nnnn&reason=...................
19 * (IP 1, 2, 3, or 4 octets, 3 octets for example is a /24 mask)
21 * GET g_ban_sync_uri?action=unban&hostname=...&ip=xxx.xxx.xxx
22 * - Querying the ban list
23 * GET g_ban_sync_uri?action=list&hostname=...&servers=xxx.xxx.xxx.xxx;xxx.xxx.xxx.xxx;...
25 * shows the bans from the listed servers, and possibly others.
26 * Format of a ban is ASCII plain text, four lines per ban, delimited by
27 * newline ONLY (no carriage return):
29 * IP address (also 1, 2, 3, or 4 octets, delimited by dot)
30 * time left in seconds
32 * server IP that registered the ban
35 #define MAX_IPBAN_URIS (URI_GET_IPBAN_END - URI_GET_IPBAN + 1)
37 float Ban_Insert(string ip, float bantime, string reason, float dosync);
39 void OnlineBanList_SendBan(string ip, float bantime, string reason)
44 uri = strcat( "action=ban&hostname=", uri_escape(autocvar_hostname));
45 uri = strcat(uri, "&ip=", uri_escape(ip));
46 uri = strcat(uri, "&duration=", ftos(bantime));
47 uri = strcat(uri, "&reason=", uri_escape(reason));
49 n = tokenize_console(autocvar_g_ban_sync_uri);
50 if(n >= MAX_IPBAN_URIS)
52 for(i = 0; i < n; ++i)
54 if(strstrofs(argv(i), "?", 0) >= 0)
55 uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
57 uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
61 void OnlineBanList_SendUnban(string ip)
66 uri = strcat( "action=unban&hostname=", uri_escape(autocvar_hostname));
67 uri = strcat(uri, "&ip=", uri_escape(ip));
69 n = tokenize_console(autocvar_g_ban_sync_uri);
70 if(n >= MAX_IPBAN_URIS)
72 for(i = 0; i < n; ++i)
74 if(strstrofs(argv(i), "?", 0) >= 0)
75 uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
77 uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
81 string OnlineBanList_Servers;
82 float OnlineBanList_Timeout;
83 float OnlineBanList_RequestWaiting[MAX_IPBAN_URIS];
85 void OnlineBanList_URI_Get_Callback(float id, float status, string data)
97 if(id >= MAX_IPBAN_URIS)
99 print("Received ban list for invalid ID\n");
103 tokenize_console(autocvar_g_ban_sync_uri);
106 print("Received ban list from ", uri, ": ");
108 if(OnlineBanList_RequestWaiting[id] == 0)
110 print("rejected (unexpected)\n");
114 OnlineBanList_RequestWaiting[id] = 0;
116 if(time > OnlineBanList_Timeout)
118 print("rejected (too late)\n");
122 syncinterval = autocvar_g_ban_sync_interval;
123 if(syncinterval == 0)
125 print("rejected (syncing disabled)\n");
133 print("error: status is ", ftos(status), "\n");
137 if(substring(data, 0, 1) == "<")
139 print("error: received HTML instead of a ban list\n");
143 if(strstrofs(data, "\r", 0) != -1)
145 print("error: received carriage returns\n");
152 n = tokenizebyseparator(data, "\n");
156 print("error: received invalid item count: ", ftos(n), "\n");
160 print("OK, ", ftos(n / 4), " items\n");
162 for(i = 0; i < n; i += 4)
165 timeleft = stof(argv(i + 1));
166 reason = argv(i + 2);
167 serverip = argv(i + 3);
169 dprint("received ban list item ", ftos(i / 4), ": ip=", ip);
170 dprint(" timeleft=", ftos(timeleft), " reason=", reason);
171 dprint(" serverip=", serverip, "\n");
173 timeleft -= 1.5 * autocvar_g_ban_sync_timeout;
178 if(l != 44) // length 44 is a cryptographic ID
180 for(j = 0; j < l; ++j)
181 if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
183 print("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.\n");
188 if(autocvar_g_ban_sync_trusted_servers_verify)
189 if((strstrofs(strcat(";", OnlineBanList_Servers, ";"), strcat(";", serverip, ";"), 0) == -1))
193 timeleft = min(syncinterval + (OnlineBanList_Timeout - time) + 5, timeleft);
194 // the ban will be prolonged on the next sync
195 // or expire 5 seconds after the next timeout
196 Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip, " at ", uri), 0);
197 print("Ban list syncing: accepted ban of ", ip, " by ", serverip, " at ", uri, ": ");
204 void OnlineBanList_Think()
210 if(autocvar_g_ban_sync_uri == "")
212 if(autocvar_g_ban_sync_interval == 0) // < 0 is okay, it means "sync on level start only"
214 argc = tokenize_console(autocvar_g_ban_sync_trusted_servers);
218 if(OnlineBanList_Servers)
219 strunzone(OnlineBanList_Servers);
220 OnlineBanList_Servers = argv(0);
221 for(i = 1; i < argc; ++i)
222 OnlineBanList_Servers = strcat(OnlineBanList_Servers, ";", argv(i));
223 OnlineBanList_Servers = strzone(OnlineBanList_Servers);
225 uri = strcat( "action=list&hostname=", uri_escape(autocvar_hostname));
226 uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
228 OnlineBanList_Timeout = time + autocvar_g_ban_sync_timeout;
230 n = tokenize_console(autocvar_g_ban_sync_uri);
231 if(n >= MAX_IPBAN_URIS)
233 for(i = 0; i < n; ++i)
235 if(OnlineBanList_RequestWaiting[i])
237 OnlineBanList_RequestWaiting[i] = 1;
238 if(strstrofs(argv(i), "?", 0) >= 0)
239 uri_get(strcat(argv(i), "&", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
241 uri_get(strcat(argv(i), "?", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
244 if(autocvar_g_ban_sync_interval > 0)
245 self.nextthink = time + max(60, autocvar_g_ban_sync_interval * 60);
254 const float BAN_MAX = 256;
256 string ban_ip[BAN_MAX];
257 float ban_expire[BAN_MAX];
276 for(i = 0; i < ban_count; ++i)
278 if(time > ban_expire[i])
280 out = strcat(out, " ", ban_ip[i]);
281 out = strcat(out, " ", ftos(ban_expire[i] - time));
283 if(strlen(out) <= 1) // no real entries
284 cvar_set("g_banned_list", "");
286 cvar_set("g_banned_list", out);
289 float Ban_Delete(float i)
295 if(ban_expire[i] == 0)
297 if(ban_expire[i] > 0)
299 OnlineBanList_SendUnban(ban_ip[i]);
300 strunzone(ban_ip[i]);
311 for(i = 0; i < ban_count; ++i)
315 n = tokenize_console(autocvar_g_banned_list);
316 if(stof(argv(0)) == 1)
318 ban_count = (n - 1) / 2;
319 for(i = 0; i < ban_count; ++i)
321 ban_ip[i] = strzone(argv(2*i+1));
322 ban_expire[i] = time + stof(argv(2*i+2));
328 e.classname = "bansyncer";
329 e.think = OnlineBanList_Think;
330 e.nextthink = time + 1;
338 print("^2Listing all existing active bans:\n");
341 for(i = 0; i < ban_count; ++i)
343 if(time > ban_expire[i])
346 ++n; // total number of existing bans
348 msg = strcat("#", ftos(i), ": ");
349 msg = strcat(msg, ban_ip[i], " is still banned for ");
350 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
352 print(" ", msg, "\n");
355 print("^2Done listing all active (", ftos(n), ") bans.\n");
358 float Ban_GetClientIP(entity client)
360 // we can't use tokenizing here, as this is called during ban list parsing
361 float i1, i2, i3, i4;
364 if(client.crypto_keyfp)
365 ban_idfp = client.crypto_idfp;
367 ban_idfp = string_null;
369 s = client.netaddress;
371 i1 = strstrofs(s, ".", 0);
374 i2 = strstrofs(s, ".", i1 + 1);
377 i3 = strstrofs(s, ".", i2 + 1);
380 i4 = strstrofs(s, ".", i3 + 1);
382 s = substring(s, 0, i4);
384 ban_ip1 = substring(s, 0, i1); // 8
385 ban_ip2 = substring(s, 0, i2); // 16
386 ban_ip3 = substring(s, 0, i3); // 24
387 ban_ip4 = strcat1(s); // 32
391 i1 = strstrofs(s, ":", 0);
394 i1 = strstrofs(s, ":", i1 + 1);
397 i2 = strstrofs(s, ":", i1 + 1);
400 i3 = strstrofs(s, ":", i2 + 1);
404 ban_ip1 = strcat(substring(s, 0, i1), "::/32"); // 32
405 ban_ip2 = strcat(substring(s, 0, i2), "::/48"); // 48
406 ban_ip4 = strcat(substring(s, 0, i3), "::/64"); // 64
408 if(i3 - i2 > 3) // means there is more than 2 digits and a : in the range
409 ban_ip3 = strcat(substring(s, 0, i2), ":", substring(s, i2 + 1, i3 - i2 - 3), "00::/56");
411 ban_ip3 = strcat(substring(s, 0, i2), ":0::/56");
416 float Ban_IsClientBanned(entity client, float idx)
418 float i, b, e, ipbanned;
421 if(!Ban_GetClientIP(client))
434 for(i = b; i < e; ++i)
437 if(time > ban_expire[i])
440 if(ban_ip1 == s) ipbanned = true;
441 if(ban_ip2 == s) ipbanned = true;
442 if(ban_ip3 == s) ipbanned = true;
443 if(ban_ip4 == s) ipbanned = true;
444 if(ban_idfp == s) return true;
448 if(!autocvar_g_banned_list_idmode)
456 float Ban_MaybeEnforceBan(entity client)
458 if(Ban_IsClientBanned(client, -1))
461 s = strcat("^1NOTE:^7 banned client ", client.netaddress, " just tried to enter\n");
470 float Ban_MaybeEnforceBanOnce(entity client)
472 if(client.ban_checked)
474 client.ban_checked = true;
475 return Ban_MaybeEnforceBan(self);
478 string Ban_Enforce(float i, string reason)
483 // Enforce our new ban
485 FOR_EACH_REALCLIENT(e)
486 if(Ban_IsClientBanned(e, i))
491 reason = strcat(reason, ": affects ");
493 reason = strcat(reason, ", ");
494 reason = strcat(reason, e.netname);
496 s = strcat(s, "^1NOTE:^7 banned client ", e.netaddress, "^7 has to go\n");
504 float Ban_Insert(string ip, float bantime, string reason, float dosync)
511 for(i = 0; i < ban_count; ++i)
515 if(time + bantime > ban_expire[i])
517 ban_expire[i] = time + bantime;
518 dprint(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now\n");
521 dprint(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now\n");
524 reason = Ban_Enforce(i, reason);
529 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
530 OnlineBanList_SendBan(ip, bantime, reason);
535 // do we have a free slot?
536 for(i = 0; i < ban_count; ++i)
537 if(time > ban_expire[i])
539 // no free slot? Then look for the one who would get unbanned next
543 bestscore = ban_expire[i];
544 for(j = 1; j < ban_count; ++j)
546 if(ban_expire[j] < bestscore)
549 bestscore = ban_expire[i];
553 // if we replace someone, will we be banned longer than him (so long-term
554 // bans never get overridden by short-term bans)
556 if(ban_expire[i] > time + bantime)
558 print(ip, " could not get banned due to no free ban slot\n");
561 // okay, insert our new victim as i
563 dprint(ip, " has been banned for ", ftos(bantime), " seconds\n");
564 ban_expire[i] = time + bantime;
565 ban_ip[i] = strzone(ip);
566 ban_count = max(ban_count, i + 1);
570 reason = Ban_Enforce(i, reason);
575 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
576 OnlineBanList_SendBan(ip, bantime, reason);
581 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
584 if(!Ban_GetClientIP(client))
586 sprint(client, strcat("Kickbanned: ", reason, "\n"));
595 ip = strcat1(ban_ip1);
598 ip = strcat1(ban_ip2);
601 ip = strcat1(ban_ip3);
605 ip = strcat1(ban_ip4);
609 id = strcat1(ban_idfp);
613 Ban_Insert(ip, bantime, reason, 1);
615 Ban_Insert(id, bantime, reason, 1);
617 * not needed, as we enforce the ban in Ban_Insert anyway
619 sprint(client, strcat("Kickbanned: ", reason, "\n"));