3 #include <common/constants.qh>
4 #include <common/stats.qh>
5 #include <common/util.qh>
6 #include <common/weapons/_all.qh>
7 #include <server/autocvars.qh>
8 #include <server/command/banning.qh>
9 #include <server/main.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 void OnlineBanList_SendBan(string ip, float bantime, string reason)
39 uri = strcat( "action=ban&hostname=", uri_escape(autocvar_hostname));
40 uri = strcat(uri, "&ip=", uri_escape(ip));
41 uri = strcat(uri, "&duration=", ftos(bantime));
42 uri = strcat(uri, "&reason=", uri_escape(reason));
44 n = tokenize_console(autocvar_g_ban_sync_uri);
45 if(n >= MAX_IPBAN_URIS)
47 for(i = 0; i < n; ++i)
49 if(strstrofs(argv(i), "?", 0) >= 0)
50 uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
52 uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
56 void OnlineBanList_SendUnban(string ip)
61 uri = strcat( "action=unban&hostname=", uri_escape(autocvar_hostname));
62 uri = strcat(uri, "&ip=", uri_escape(ip));
64 n = tokenize_console(autocvar_g_ban_sync_uri);
65 if(n >= MAX_IPBAN_URIS)
67 for(i = 0; i < n; ++i)
69 if(strstrofs(argv(i), "?", 0) >= 0)
70 uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
72 uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
76 string OnlineBanList_Servers;
77 float OnlineBanList_Timeout;
78 float OnlineBanList_RequestWaiting[MAX_IPBAN_URIS];
80 void OnlineBanList_URI_Get_Callback(float id, float status, string data)
92 if(id >= MAX_IPBAN_URIS)
94 LOG_INFO("Received ban list for invalid ID");
98 tokenize_console(autocvar_g_ban_sync_uri);
101 string prelude = strcat("Received ban list from ", uri, ": ");
103 if(OnlineBanList_RequestWaiting[id] == 0)
105 LOG_INFO(prelude, "rejected (unexpected)");
109 OnlineBanList_RequestWaiting[id] = 0;
111 if(time > OnlineBanList_Timeout)
113 LOG_INFO(prelude, "rejected (too late)");
117 syncinterval = autocvar_g_ban_sync_interval;
118 if(syncinterval == 0)
120 LOG_INFO(prelude, "rejected (syncing disabled)");
128 LOG_INFO(prelude, "error: status is ", ftos(status));
132 if(substring(data, 0, 1) == "<")
134 LOG_INFO(prelude, "error: received HTML instead of a ban list");
138 if(strstrofs(data, "\r", 0) != -1)
140 LOG_INFO(prelude, "error: received carriage returns");
147 n = tokenizebyseparator(data, "\n");
151 LOG_INFO(prelude, "error: received invalid item count: ", ftos(n));
155 LOG_INFO(prelude, "OK, ", ftos(n / 4), " items");
157 for(i = 0; i < n; i += 4)
160 timeleft = stof(argv(i + 1));
161 reason = argv(i + 2);
162 serverip = argv(i + 3);
164 LOG_TRACE("received ban list item ", ftos(i / 4), ": ip=", ip);
165 LOG_TRACE(" timeleft=", ftos(timeleft), " reason=", reason);
166 LOG_TRACE(" serverip=", serverip);
168 timeleft -= 1.5 * autocvar_g_ban_sync_timeout;
173 if(l != 44) // length 44 is a cryptographic ID
175 for(j = 0; j < l; ++j)
176 if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
178 LOG_INFO("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.");
183 if(autocvar_g_ban_sync_trusted_servers_verify)
184 if((strstrofs(strcat(";", OnlineBanList_Servers, ";"), strcat(";", serverip, ";"), 0) == -1))
188 timeleft = min(syncinterval + (OnlineBanList_Timeout - time) + 5, timeleft);
189 // the ban will be prolonged on the next sync
190 // or expire 5 seconds after the next timeout
191 Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip, " at ", uri), 0);
192 LOG_INFO("Ban list syncing: accepted ban of ", ip, " by ", serverip, " at ", uri, ": ", reason);
198 void OnlineBanList_Think(entity this)
204 if(autocvar_g_ban_sync_uri == "")
209 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);
221 string s = argv(0); for(i = 1; i < argc; ++i) s = strcat(s, ";", argv(i));
222 strcpy(OnlineBanList_Servers, s);
224 uri = strcat( "action=list&hostname=", uri_escape(autocvar_hostname));
225 uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
227 OnlineBanList_Timeout = time + autocvar_g_ban_sync_timeout;
229 n = tokenize_console(autocvar_g_ban_sync_uri);
230 if(n >= MAX_IPBAN_URIS)
232 for(i = 0; i < n; ++i)
234 if(OnlineBanList_RequestWaiting[i])
236 OnlineBanList_RequestWaiting[i] = 1;
237 if(strstrofs(argv(i), "?", 0) >= 0)
238 uri_get(strcat(argv(i), "&", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
240 uri_get(strcat(argv(i), "?", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
243 if(autocvar_g_ban_sync_interval <= 0)
249 this.nextthink = time + max(60, autocvar_g_ban_sync_interval * 60);
252 const float BAN_MAX = 256;
254 string ban_ip[BAN_MAX];
255 float ban_expire[BAN_MAX];
274 for(i = 0; i < ban_count; ++i)
276 if(time > ban_expire[i])
278 out = strcat(out, " ", ban_ip[i]);
279 out = strcat(out, " ", ftos(ban_expire[i] - time));
281 if(strlen(out) <= 1) // no real entries
282 cvar_set("g_banned_list", "");
284 cvar_set("g_banned_list", out);
287 float Ban_Delete(float i)
293 if(ban_expire[i] == 0)
295 if(ban_expire[i] > 0)
297 OnlineBanList_SendUnban(ban_ip[i]);
298 strunzone(ban_ip[i]);
309 for(i = 0; i < ban_count; ++i)
313 n = tokenize_console(autocvar_g_banned_list);
314 if(stof(argv(0)) == 1)
316 ban_count = (n - 1) / 2;
317 for(i = 0; i < ban_count; ++i)
319 ban_ip[i] = strzone(argv(2*i+1));
320 ban_expire[i] = time + stof(argv(2*i+2));
324 entity e = new(bansyncer);
325 setthink(e, OnlineBanList_Think);
326 e.nextthink = time + 1;
334 LOG_INFO("^2Listing all existing active bans:");
337 for(i = 0; i < ban_count; ++i)
339 if(time > ban_expire[i])
342 ++n; // total number of existing bans
344 msg = strcat("#", ftos(i), ": ");
345 msg = strcat(msg, ban_ip[i], " is still banned for ");
346 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
351 LOG_INFO("^2Done listing all active (", ftos(n), ") bans.");
354 float Ban_GetClientIP(entity client)
356 // we can't use tokenizing here, as this is called during ban list parsing
357 float i1, i2, i3, i4;
360 if(client.crypto_idfp_signed)
361 ban_idfp = client.crypto_idfp;
363 ban_idfp = string_null;
365 s = client.netaddress;
367 i1 = strstrofs(s, ".", 0);
370 i2 = strstrofs(s, ".", i1 + 1);
373 i3 = strstrofs(s, ".", i2 + 1);
376 i4 = strstrofs(s, ".", i3 + 1);
378 s = substring(s, 0, i4);
380 ban_ip1 = substring(s, 0, i1); // 8
381 ban_ip2 = substring(s, 0, i2); // 16
382 ban_ip3 = substring(s, 0, i3); // 24
383 ban_ip4 = strcat1(s); // 32
387 i1 = strstrofs(s, ":", 0);
390 i1 = strstrofs(s, ":", i1 + 1);
393 i2 = strstrofs(s, ":", i1 + 1);
396 i3 = strstrofs(s, ":", i2 + 1);
400 ban_ip1 = strcat(substring(s, 0, i1), "::/32"); // 32
401 ban_ip2 = strcat(substring(s, 0, i2), "::/48"); // 48
402 ban_ip4 = strcat(substring(s, 0, i3), "::/64"); // 64
404 if(i3 - i2 > 3) // means there is more than 2 digits and a : in the range
405 ban_ip3 = strcat(substring(s, 0, i2), ":", substring(s, i2 + 1, i3 - i2 - 3), "00::/56");
407 ban_ip3 = strcat(substring(s, 0, i2), ":0::/56");
412 float Ban_IsClientBanned(entity client, float idx)
414 float i, b, e, ipbanned;
417 if(!Ban_GetClientIP(client))
430 for(i = b; i < e; ++i)
433 if(time > ban_expire[i])
436 if(ban_ip1 == s) ipbanned = true;
437 if(ban_ip2 == s) ipbanned = true;
438 if(ban_ip3 == s) ipbanned = true;
439 if(ban_ip4 == s) ipbanned = true;
440 if(ban_idfp == s) return true;
444 if(!autocvar_g_banned_list_idmode)
452 bool Ban_MaybeEnforceBan(entity client)
454 if (Ban_IsClientBanned(client, -1))
456 if (!client.crypto_idfp)
457 LOG_INFOF("^1NOTE:^7 banned client %s just tried to enter\n",
460 LOG_INFOF("^1NOTE:^7 banned client %s (%s) just tried to enter\n",
461 client.netaddress, client.crypto_idfp);
463 if(autocvar_g_ban_telluser)
464 sprint(client, "You are banned from this server.\n");
472 bool Ban_MaybeEnforceBanOnce(entity client)
474 if (client.ban_checked) return false;
475 client.ban_checked = true;
476 return Ban_MaybeEnforceBan(client);
479 string Ban_Enforce(float j, string reason)
483 // Enforce our new ban
485 FOREACH_CLIENTSLOT(IS_REAL_CLIENT(it),
487 if(Ban_IsClientBanned(it, j))
492 reason = strcat(reason, ": affects ");
494 reason = strcat(reason, ", ");
495 reason = strcat(reason, it.netname);
497 s = strcat(s, "^1NOTE:^7 banned client ", it.netname, "^7 has to go\n");
506 float Ban_Insert(string ip, float bantime, string reason, float dosync)
513 for(i = 0; i < ban_count; ++i)
517 if(time + bantime > ban_expire[i])
519 ban_expire[i] = time + bantime;
520 LOG_TRACE(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now");
523 LOG_TRACE(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now");
526 reason = Ban_Enforce(i, reason);
531 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
532 OnlineBanList_SendBan(ip, bantime, reason);
537 // do we have a free slot?
538 for(i = 0; i < ban_count; ++i)
539 if(time > ban_expire[i])
541 // no free slot? Then look for the one who would get unbanned next
545 bestscore = ban_expire[i];
546 for(j = 1; j < ban_count; ++j)
548 if(ban_expire[j] < bestscore)
551 bestscore = ban_expire[i];
555 // if we replace someone, will we be banned longer than him (so long-term
556 // bans never get overridden by short-term bans)
558 if(ban_expire[i] > time + bantime)
560 LOG_INFO(ip, " could not get banned due to no free ban slot");
563 // okay, insert our new victim as i
565 LOG_TRACE(ip, " has been banned for ", ftos(bantime), " seconds");
566 ban_expire[i] = time + bantime;
567 ban_ip[i] = strzone(ip);
568 ban_count = max(ban_count, i + 1);
572 reason = Ban_Enforce(i, reason);
577 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
578 OnlineBanList_SendBan(ip, bantime, reason);
583 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
586 if(!Ban_GetClientIP(client))
588 sprint(client, strcat("Kickbanned: ", reason, "\n"));
597 ip = strcat1(ban_ip1);
600 ip = strcat1(ban_ip2);
603 ip = strcat1(ban_ip3);
607 ip = strcat1(ban_ip4);
611 id = strcat1(ban_idfp);
615 Ban_Insert(ip, bantime, reason, 1);
617 Ban_Insert(id, bantime, reason, 1);
619 * not needed, as we enforce the ban in Ban_Insert anyway
621 sprint(client, strcat("Kickbanned: ", reason, "\n"));