Merge branch 'master' into Mario/stats_eloranking
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / ipban.qc
1 #include "ipban.qh"
2
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5 #include "autocvars.qh"
6 #include "command/banning.qh"
7 #include "defs.qh"
8 #include "../common/constants.qh"
9 #include "../common/util.qh"
10
11 /*
12  * Protocol of online ban list:
13  *
14  * - Reporting a ban:
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)
17  * - Removing a ban:
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;...
21  *
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):
25  *
26  *     IP address (also 1, 2, 3, or 4 octets, delimited by dot)
27  *     time left in seconds
28  *     reason of the ban
29  *     server IP that registered the ban
30  */
31
32 #define MAX_IPBAN_URIS (URI_GET_IPBAN_END - URI_GET_IPBAN + 1)
33
34 void OnlineBanList_SendBan(string ip, float bantime, string reason)
35 {
36         string uri;
37         float i, n;
38
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));
43
44         n = tokenize_console(autocvar_g_ban_sync_uri);
45         if(n >= MAX_IPBAN_URIS)
46                 n = MAX_IPBAN_URIS;
47         for(i = 0; i < n; ++i)
48         {
49                 if(strstrofs(argv(i), "?", 0) >= 0)
50                         uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
51                 else
52                         uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
53         }
54 }
55
56 void OnlineBanList_SendUnban(string ip)
57 {
58         string uri;
59         float i, n;
60
61         uri = strcat(     "action=unban&hostname=", uri_escape(autocvar_hostname));
62         uri = strcat(uri, "&ip=", uri_escape(ip));
63
64         n = tokenize_console(autocvar_g_ban_sync_uri);
65         if(n >= MAX_IPBAN_URIS)
66                 n = MAX_IPBAN_URIS;
67         for(i = 0; i < n; ++i)
68         {
69                 if(strstrofs(argv(i), "?", 0) >= 0)
70                         uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
71                 else
72                         uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
73         }
74 }
75
76 string OnlineBanList_Servers;
77 float OnlineBanList_Timeout;
78 float OnlineBanList_RequestWaiting[MAX_IPBAN_URIS];
79
80 void OnlineBanList_URI_Get_Callback(float id, float status, string data)
81 {
82         float n, i, j, l;
83         string ip;
84         float timeleft;
85         string reason;
86         string serverip;
87         float syncinterval;
88         string uri;
89
90         id -= URI_GET_IPBAN;
91
92         if(id >= MAX_IPBAN_URIS)
93         {
94                 LOG_INFO("Received ban list for invalid ID");
95                 return;
96         }
97
98         tokenize_console(autocvar_g_ban_sync_uri);
99         uri = argv(id);
100
101         string prelude = strcat("Received ban list from ", uri, ": ");
102
103         if(OnlineBanList_RequestWaiting[id] == 0)
104         {
105                 LOG_INFO(prelude, "rejected (unexpected)");
106                 return;
107         }
108
109         OnlineBanList_RequestWaiting[id] = 0;
110
111         if(time > OnlineBanList_Timeout)
112         {
113                 LOG_INFO(prelude, "rejected (too late)");
114                 return;
115         }
116
117         syncinterval = autocvar_g_ban_sync_interval;
118         if(syncinterval == 0)
119         {
120                 LOG_INFO(prelude, "rejected (syncing disabled)");
121                 return;
122         }
123         if(syncinterval > 0)
124                 syncinterval *= 60;
125
126         if(status != 0)
127         {
128                 LOG_INFO(prelude, "error: status is ", ftos(status));
129                 return;
130         }
131
132         if(substring(data, 0, 1) == "<")
133         {
134                 LOG_INFO(prelude, "error: received HTML instead of a ban list");
135                 return;
136         }
137
138         if(strstrofs(data, "\r", 0) != -1)
139         {
140                 LOG_INFO(prelude, "error: received carriage returns");
141                 return;
142         }
143
144         if(data == "")
145                 n = 0;
146         else
147                 n = tokenizebyseparator(data, "\n");
148
149         if((n % 4) != 0)
150         {
151                 LOG_INFO(prelude, "error: received invalid item count: ", ftos(n));
152                 return;
153         }
154
155         LOG_INFO(prelude, "OK, ", ftos(n / 4), " items");
156
157         for(i = 0; i < n; i += 4)
158         {
159                 ip = argv(i);
160                 timeleft = stof(argv(i + 1));
161                 reason = argv(i + 2);
162                 serverip = argv(i + 3);
163
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);
167
168                 timeleft -= 1.5 * autocvar_g_ban_sync_timeout;
169                 if(timeleft < 0)
170                         continue;
171
172                 l = strlen(ip);
173                 if(l != 44) // length 44 is a cryptographic ID
174                 {
175                         for(j = 0; j < l; ++j)
176                                 if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
177                                 {
178                                         LOG_INFO("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.");
179                                         goto skip;
180                                 }
181                 }
182
183                 if(autocvar_g_ban_sync_trusted_servers_verify)
184                         if((strstrofs(strcat(";", OnlineBanList_Servers, ";"), strcat(";", serverip, ";"), 0) == -1))
185                                 continue;
186
187                 if(syncinterval > 0)
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);
193
194 LABEL(skip)
195         }
196 }
197
198 void OnlineBanList_Think(entity this)
199 {
200         int argc;
201         string uri;
202         float i, n;
203
204         if(autocvar_g_ban_sync_uri == "")
205                 goto killme;
206         if(autocvar_g_ban_sync_interval == 0) // < 0 is okay, it means "sync on level start only"
207                 goto killme;
208         argc = tokenize_console(autocvar_g_ban_sync_trusted_servers);
209         if(argc == 0)
210                 goto killme;
211
212         string s = argv(0); for(i = 1; i < argc; ++i) s = strcat(s, ";", argv(i));
213         strcpy(OnlineBanList_Servers, s);
214
215         uri = strcat(     "action=list&hostname=", uri_escape(autocvar_hostname));
216         uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
217
218         OnlineBanList_Timeout = time + autocvar_g_ban_sync_timeout;
219
220         n = tokenize_console(autocvar_g_ban_sync_uri);
221         if(n >= MAX_IPBAN_URIS)
222                 n = MAX_IPBAN_URIS;
223         for(i = 0; i < n; ++i)
224         {
225                 if(OnlineBanList_RequestWaiting[i])
226                         continue;
227                 OnlineBanList_RequestWaiting[i] = 1;
228                 if(strstrofs(argv(i), "?", 0) >= 0)
229                         uri_get(strcat(argv(i), "&", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
230                 else
231                         uri_get(strcat(argv(i), "?", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
232         }
233
234         if(autocvar_g_ban_sync_interval > 0)
235                 this.nextthink = time + max(60, autocvar_g_ban_sync_interval * 60);
236         else
237                 goto killme;
238         return;
239
240 LABEL(killme)
241         delete(this);
242 }
243
244 const float BAN_MAX = 256;
245 float ban_loaded;
246 string ban_ip[BAN_MAX];
247 float ban_expire[BAN_MAX];
248 float ban_count;
249
250 string ban_ip1;
251 string ban_ip2;
252 string ban_ip3;
253 string ban_ip4;
254 string ban_idfp;
255
256 void Ban_SaveBans()
257 {
258         string out;
259         float i;
260
261         if(!ban_loaded)
262                 return;
263
264         // version of list
265         out = "1";
266         for(i = 0; i < ban_count; ++i)
267         {
268                 if(time > ban_expire[i])
269                         continue;
270                 out = strcat(out, " ", ban_ip[i]);
271                 out = strcat(out, " ", ftos(ban_expire[i] - time));
272         }
273         if(strlen(out) <= 1) // no real entries
274                 cvar_set("g_banned_list", "");
275         else
276                 cvar_set("g_banned_list", out);
277 }
278
279 float Ban_Delete(float i)
280 {
281         if(i < 0)
282                 return false;
283         if(i >= ban_count)
284                 return false;
285         if(ban_expire[i] == 0)
286                 return false;
287         if(ban_expire[i] > 0)
288         {
289                 OnlineBanList_SendUnban(ban_ip[i]);
290                 strunzone(ban_ip[i]);
291         }
292         ban_expire[i] = 0;
293         ban_ip[i] = "";
294         Ban_SaveBans();
295         return true;
296 }
297
298 void Ban_LoadBans()
299 {
300         float i, n;
301         for(i = 0; i < ban_count; ++i)
302                 Ban_Delete(i);
303         ban_count = 0;
304         ban_loaded = true;
305         n = tokenize_console(autocvar_g_banned_list);
306         if(stof(argv(0)) == 1)
307         {
308                 ban_count = (n - 1) / 2;
309                 for(i = 0; i < ban_count; ++i)
310                 {
311                         ban_ip[i] = strzone(argv(2*i+1));
312                         ban_expire[i] = time + stof(argv(2*i+2));
313                 }
314         }
315
316         entity e = new(bansyncer);
317         setthink(e, OnlineBanList_Think);
318         e.nextthink = time + 1;
319 }
320
321 void Ban_View()
322 {
323         float i, n;
324         string msg;
325
326         LOG_INFO("^2Listing all existing active bans:");
327
328         n = 0;
329         for(i = 0; i < ban_count; ++i)
330         {
331                 if(time > ban_expire[i])
332                         continue;
333
334                 ++n; // total number of existing bans
335
336                 msg = strcat("#", ftos(i), ": ");
337                 msg = strcat(msg, ban_ip[i], " is still banned for ");
338                 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
339
340                 LOG_INFO("  ", msg);
341         }
342
343         LOG_INFO("^2Done listing all active (", ftos(n), ") bans.");
344 }
345
346 float Ban_GetClientIP(entity client)
347 {
348         // we can't use tokenizing here, as this is called during ban list parsing
349         float i1, i2, i3, i4;
350         string s;
351
352         if(client.crypto_idfp_signed)
353                 ban_idfp = client.crypto_idfp;
354         else
355                 ban_idfp = string_null;
356
357         s = client.netaddress;
358
359         i1 = strstrofs(s, ".", 0);
360         if(i1 < 0)
361                 goto ipv6;
362         i2 = strstrofs(s, ".", i1 + 1);
363         if(i2 < 0)
364                 return false;
365         i3 = strstrofs(s, ".", i2 + 1);
366         if(i3 < 0)
367                 return false;
368         i4 = strstrofs(s, ".", i3 + 1);
369         if(i4 >= 0)
370                 s = substring(s, 0, i4);
371
372         ban_ip1 = substring(s, 0, i1); // 8
373         ban_ip2 = substring(s, 0, i2); // 16
374         ban_ip3 = substring(s, 0, i3); // 24
375         ban_ip4 = strcat1(s); // 32
376         return true;
377
378 LABEL(ipv6)
379         i1 = strstrofs(s, ":", 0);
380         if(i1 < 0)
381                 return false;
382         i1 = strstrofs(s, ":", i1 + 1);
383         if(i1 < 0)
384                 return false;
385         i2 = strstrofs(s, ":", i1 + 1);
386         if(i2 < 0)
387                 return false;
388         i3 = strstrofs(s, ":", i2 + 1);
389         if(i3 < 0)
390                 return false;
391
392         ban_ip1 = strcat(substring(s, 0, i1), "::/32"); // 32
393         ban_ip2 = strcat(substring(s, 0, i2), "::/48"); // 48
394         ban_ip4 = strcat(substring(s, 0, i3), "::/64"); // 64
395
396         if(i3 - i2 > 3) // means there is more than 2 digits and a : in the range
397                 ban_ip3 = strcat(substring(s, 0, i2), ":", substring(s, i2 + 1, i3 - i2 - 3), "00::/56");
398         else
399                 ban_ip3 = strcat(substring(s, 0, i2), ":0::/56");
400
401         return true;
402 }
403
404 float Ban_IsClientBanned(entity client, float idx)
405 {
406         float i, b, e, ipbanned;
407         if(!ban_loaded)
408                 Ban_LoadBans();
409         if(!Ban_GetClientIP(client))
410                 return false;
411         if(idx < 0)
412         {
413                 b = 0;
414                 e = ban_count;
415         }
416         else
417         {
418                 b = idx;
419                 e = idx + 1;
420         }
421         ipbanned = false;
422         for(i = b; i < e; ++i)
423         {
424                 string s;
425                 if(time > ban_expire[i])
426                         continue;
427                 s = ban_ip[i];
428                 if(ban_ip1 == s) ipbanned = true;
429                 if(ban_ip2 == s) ipbanned = true;
430                 if(ban_ip3 == s) ipbanned = true;
431                 if(ban_ip4 == s) ipbanned = true;
432                 if(ban_idfp == s) return true;
433         }
434         if(ipbanned)
435         {
436                 if(!autocvar_g_banned_list_idmode)
437                         return true;
438                 if (!ban_idfp)
439                         return true;
440         }
441         return false;
442 }
443
444 bool Ban_MaybeEnforceBan(entity client)
445 {
446         if (Ban_IsClientBanned(client, -1))
447         {
448                 string s = sprintf("^1NOTE:^7 banned client %s just tried to enter\n", client.netaddress);
449                 if(autocvar_g_ban_telluser)
450                         sprint(client, "You are banned from this server.\n");
451                 dropclient(client);
452                 bprint(s);
453                 return true;
454         }
455         return false;
456 }
457
458 .bool ban_checked;
459 bool Ban_MaybeEnforceBanOnce(entity client)
460 {
461         if (client.ban_checked) return false;
462         client.ban_checked = true;
463         return Ban_MaybeEnforceBan(client);
464 }
465
466 string Ban_Enforce(float j, string reason)
467 {
468         string s;
469
470         // Enforce our new ban
471         s = "";
472         FOREACH_CLIENTSLOT(IS_REAL_CLIENT(it),
473         {
474                 if(Ban_IsClientBanned(it, j))
475                 {
476                         if(reason != "")
477                         {
478                                 if(s == "")
479                                         reason = strcat(reason, ": affects ");
480                                 else
481                                         reason = strcat(reason, ", ");
482                                 reason = strcat(reason, it.netname);
483                         }
484                         s = strcat(s, "^1NOTE:^7 banned client ", it.netaddress, "^7 has to go\n");
485                         dropclient(it);
486                 }
487         });
488         bprint(s);
489
490         return reason;
491 }
492
493 float Ban_Insert(string ip, float bantime, string reason, float dosync)
494 {
495         float i;
496         float j;
497         float bestscore;
498
499         // already banned?
500         for(i = 0; i < ban_count; ++i)
501                 if(ban_ip[i] == ip)
502                 {
503                         // prolong the ban
504                         if(time + bantime > ban_expire[i])
505                         {
506                                 ban_expire[i] = time + bantime;
507                                 LOG_TRACE(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now");
508                         }
509                         else
510                                 LOG_TRACE(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now");
511
512                         // and enforce
513                         reason = Ban_Enforce(i, reason);
514
515                         // and abort
516                         if(dosync)
517                                 if(reason != "")
518                                         if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
519                                                 OnlineBanList_SendBan(ip, bantime, reason);
520
521                         return false;
522                 }
523
524         // do we have a free slot?
525         for(i = 0; i < ban_count; ++i)
526                 if(time > ban_expire[i])
527                         break;
528         // no free slot? Then look for the one who would get unbanned next
529         if(i >= BAN_MAX)
530         {
531                 i = 0;
532                 bestscore = ban_expire[i];
533                 for(j = 1; j < ban_count; ++j)
534                 {
535                         if(ban_expire[j] < bestscore)
536                         {
537                                 i = j;
538                                 bestscore = ban_expire[i];
539                         }
540                 }
541         }
542         // if we replace someone, will we be banned longer than him (so long-term
543         // bans never get overridden by short-term bans)
544         if(i < ban_count)
545         if(ban_expire[i] > time + bantime)
546         {
547                 LOG_INFO(ip, " could not get banned due to no free ban slot");
548                 return false;
549         }
550         // okay, insert our new victim as i
551         Ban_Delete(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);
556
557         Ban_SaveBans();
558
559         reason = Ban_Enforce(i, reason);
560
561         // and abort
562         if(dosync)
563                 if(reason != "")
564                         if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
565                                 OnlineBanList_SendBan(ip, bantime, reason);
566
567         return true;
568 }
569
570 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
571 {
572         string ip, id;
573         if(!Ban_GetClientIP(client))
574         {
575                 sprint(client, strcat("Kickbanned: ", reason, "\n"));
576                 dropclient(client);
577                 return;
578         }
579
580         // who to ban?
581         switch(masksize)
582         {
583                 case 1:
584                         ip = strcat1(ban_ip1);
585                         break;
586                 case 2:
587                         ip = strcat1(ban_ip2);
588                         break;
589                 case 3:
590                         ip = strcat1(ban_ip3);
591                         break;
592                 case 4:
593                 default:
594                         ip = strcat1(ban_ip4);
595                         break;
596         }
597         if(ban_idfp)
598                 id = strcat1(ban_idfp);
599         else
600                 id = string_null;
601
602         Ban_Insert(ip, bantime, reason, 1);
603         if(id)
604                 Ban_Insert(id, bantime, reason, 1);
605         /*
606          * not needed, as we enforce the ban in Ban_Insert anyway
607         // and kick him
608         sprint(client, strcat("Kickbanned: ", reason, "\n"));
609         dropclient(client);
610          */
611 }