]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/ipban.qc
Merge branch 'master' into Mario/qcphysics
[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         {
206                 delete(this);
207                 return;
208         }
209         if(autocvar_g_ban_sync_interval == 0) // < 0 is okay, it means "sync on level start only"
210         {
211                 delete(this);
212                 return;
213         }
214         argc = tokenize_console(autocvar_g_ban_sync_trusted_servers);
215         if(argc == 0)
216         {
217                 delete(this);
218                 return;
219         }
220
221         string s = argv(0); for(i = 1; i < argc; ++i) s = strcat(s, ";", argv(i));
222         strcpy(OnlineBanList_Servers, s);
223
224         uri = strcat(     "action=list&hostname=", uri_escape(autocvar_hostname));
225         uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
226
227         OnlineBanList_Timeout = time + autocvar_g_ban_sync_timeout;
228
229         n = tokenize_console(autocvar_g_ban_sync_uri);
230         if(n >= MAX_IPBAN_URIS)
231                 n = MAX_IPBAN_URIS;
232         for(i = 0; i < n; ++i)
233         {
234                 if(OnlineBanList_RequestWaiting[i])
235                         continue;
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
239                 else
240                         uri_get(strcat(argv(i), "?", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
241         }
242
243         if(autocvar_g_ban_sync_interval <= 0)
244         {
245                 delete(this);
246                 return;
247         }
248
249         this.nextthink = time + max(60, autocvar_g_ban_sync_interval * 60);
250 }
251
252 const float BAN_MAX = 256;
253 float ban_loaded;
254 string ban_ip[BAN_MAX];
255 float ban_expire[BAN_MAX];
256 float ban_count;
257
258 string ban_ip1;
259 string ban_ip2;
260 string ban_ip3;
261 string ban_ip4;
262 string ban_idfp;
263
264 void Ban_SaveBans()
265 {
266         string out;
267         float i;
268
269         if(!ban_loaded)
270                 return;
271
272         // version of list
273         out = "1";
274         for(i = 0; i < ban_count; ++i)
275         {
276                 if(time > ban_expire[i])
277                         continue;
278                 out = strcat(out, " ", ban_ip[i]);
279                 out = strcat(out, " ", ftos(ban_expire[i] - time));
280         }
281         if(strlen(out) <= 1) // no real entries
282                 cvar_set("g_banned_list", "");
283         else
284                 cvar_set("g_banned_list", out);
285 }
286
287 float Ban_Delete(float i)
288 {
289         if(i < 0)
290                 return false;
291         if(i >= ban_count)
292                 return false;
293         if(ban_expire[i] == 0)
294                 return false;
295         if(ban_expire[i] > 0)
296         {
297                 OnlineBanList_SendUnban(ban_ip[i]);
298                 strunzone(ban_ip[i]);
299         }
300         ban_expire[i] = 0;
301         ban_ip[i] = "";
302         Ban_SaveBans();
303         return true;
304 }
305
306 void Ban_LoadBans()
307 {
308         float i, n;
309         for(i = 0; i < ban_count; ++i)
310                 Ban_Delete(i);
311         ban_count = 0;
312         ban_loaded = true;
313         n = tokenize_console(autocvar_g_banned_list);
314         if(stof(argv(0)) == 1)
315         {
316                 ban_count = (n - 1) / 2;
317                 for(i = 0; i < ban_count; ++i)
318                 {
319                         ban_ip[i] = strzone(argv(2*i+1));
320                         ban_expire[i] = time + stof(argv(2*i+2));
321                 }
322         }
323
324         entity e = new(bansyncer);
325         setthink(e, OnlineBanList_Think);
326         e.nextthink = time + 1;
327 }
328
329 void Ban_View()
330 {
331         float i, n;
332         string msg;
333
334         LOG_INFO("^2Listing all existing active bans:");
335
336         n = 0;
337         for(i = 0; i < ban_count; ++i)
338         {
339                 if(time > ban_expire[i])
340                         continue;
341
342                 ++n; // total number of existing bans
343
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");
347
348                 LOG_INFO("  ", msg);
349         }
350
351         LOG_INFO("^2Done listing all active (", ftos(n), ") bans.");
352 }
353
354 float Ban_GetClientIP(entity client)
355 {
356         // we can't use tokenizing here, as this is called during ban list parsing
357         float i1, i2, i3, i4;
358         string s;
359
360         if(client.crypto_idfp_signed)
361                 ban_idfp = client.crypto_idfp;
362         else
363                 ban_idfp = string_null;
364
365         s = client.netaddress;
366
367         i1 = strstrofs(s, ".", 0);
368         if(i1 < 0)
369                 goto ipv6;
370         i2 = strstrofs(s, ".", i1 + 1);
371         if(i2 < 0)
372                 return false;
373         i3 = strstrofs(s, ".", i2 + 1);
374         if(i3 < 0)
375                 return false;
376         i4 = strstrofs(s, ".", i3 + 1);
377         if(i4 >= 0)
378                 s = substring(s, 0, i4);
379
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
384         return true;
385
386 LABEL(ipv6)
387         i1 = strstrofs(s, ":", 0);
388         if(i1 < 0)
389                 return false;
390         i1 = strstrofs(s, ":", i1 + 1);
391         if(i1 < 0)
392                 return false;
393         i2 = strstrofs(s, ":", i1 + 1);
394         if(i2 < 0)
395                 return false;
396         i3 = strstrofs(s, ":", i2 + 1);
397         if(i3 < 0)
398                 return false;
399
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
403
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");
406         else
407                 ban_ip3 = strcat(substring(s, 0, i2), ":0::/56");
408
409         return true;
410 }
411
412 float Ban_IsClientBanned(entity client, float idx)
413 {
414         float i, b, e, ipbanned;
415         if(!ban_loaded)
416                 Ban_LoadBans();
417         if(!Ban_GetClientIP(client))
418                 return false;
419         if(idx < 0)
420         {
421                 b = 0;
422                 e = ban_count;
423         }
424         else
425         {
426                 b = idx;
427                 e = idx + 1;
428         }
429         ipbanned = false;
430         for(i = b; i < e; ++i)
431         {
432                 string s;
433                 if(time > ban_expire[i])
434                         continue;
435                 s = ban_ip[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;
441         }
442         if(ipbanned)
443         {
444                 if(!autocvar_g_banned_list_idmode)
445                         return true;
446                 if (!ban_idfp)
447                         return true;
448         }
449         return false;
450 }
451
452 bool Ban_MaybeEnforceBan(entity client)
453 {
454         if (Ban_IsClientBanned(client, -1))
455         {
456                 string s = sprintf("^1NOTE:^7 banned client %s just tried to enter\n", client.netaddress);
457                 if(autocvar_g_ban_telluser)
458                         sprint(client, "You are banned from this server.\n");
459                 dropclient(client);
460                 bprint(s);
461                 return true;
462         }
463         return false;
464 }
465
466 .bool ban_checked;
467 bool Ban_MaybeEnforceBanOnce(entity client)
468 {
469         if (client.ban_checked) return false;
470         client.ban_checked = true;
471         return Ban_MaybeEnforceBan(client);
472 }
473
474 string Ban_Enforce(float j, string reason)
475 {
476         string s;
477
478         // Enforce our new ban
479         s = "";
480         FOREACH_CLIENTSLOT(IS_REAL_CLIENT(it),
481         {
482                 if(Ban_IsClientBanned(it, j))
483                 {
484                         if(reason != "")
485                         {
486                                 if(s == "")
487                                         reason = strcat(reason, ": affects ");
488                                 else
489                                         reason = strcat(reason, ", ");
490                                 reason = strcat(reason, it.netname);
491                         }
492                         s = strcat(s, "^1NOTE:^7 banned client ", it.netaddress, "^7 has to go\n");
493                         dropclient(it);
494                 }
495         });
496         bprint(s);
497
498         return reason;
499 }
500
501 float Ban_Insert(string ip, float bantime, string reason, float dosync)
502 {
503         float i;
504         float j;
505         float bestscore;
506
507         // already banned?
508         for(i = 0; i < ban_count; ++i)
509                 if(ban_ip[i] == ip)
510                 {
511                         // prolong the ban
512                         if(time + bantime > ban_expire[i])
513                         {
514                                 ban_expire[i] = time + bantime;
515                                 LOG_TRACE(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now");
516                         }
517                         else
518                                 LOG_TRACE(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now");
519
520                         // and enforce
521                         reason = Ban_Enforce(i, reason);
522
523                         // and abort
524                         if(dosync)
525                                 if(reason != "")
526                                         if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
527                                                 OnlineBanList_SendBan(ip, bantime, reason);
528
529                         return false;
530                 }
531
532         // do we have a free slot?
533         for(i = 0; i < ban_count; ++i)
534                 if(time > ban_expire[i])
535                         break;
536         // no free slot? Then look for the one who would get unbanned next
537         if(i >= BAN_MAX)
538         {
539                 i = 0;
540                 bestscore = ban_expire[i];
541                 for(j = 1; j < ban_count; ++j)
542                 {
543                         if(ban_expire[j] < bestscore)
544                         {
545                                 i = j;
546                                 bestscore = ban_expire[i];
547                         }
548                 }
549         }
550         // if we replace someone, will we be banned longer than him (so long-term
551         // bans never get overridden by short-term bans)
552         if(i < ban_count)
553         if(ban_expire[i] > time + bantime)
554         {
555                 LOG_INFO(ip, " could not get banned due to no free ban slot");
556                 return false;
557         }
558         // okay, insert our new victim as i
559         Ban_Delete(i);
560         LOG_TRACE(ip, " has been banned for ", ftos(bantime), " seconds");
561         ban_expire[i] = time + bantime;
562         ban_ip[i] = strzone(ip);
563         ban_count = max(ban_count, i + 1);
564
565         Ban_SaveBans();
566
567         reason = Ban_Enforce(i, reason);
568
569         // and abort
570         if(dosync)
571                 if(reason != "")
572                         if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
573                                 OnlineBanList_SendBan(ip, bantime, reason);
574
575         return true;
576 }
577
578 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
579 {
580         string ip, id;
581         if(!Ban_GetClientIP(client))
582         {
583                 sprint(client, strcat("Kickbanned: ", reason, "\n"));
584                 dropclient(client);
585                 return;
586         }
587
588         // who to ban?
589         switch(masksize)
590         {
591                 case 1:
592                         ip = strcat1(ban_ip1);
593                         break;
594                 case 2:
595                         ip = strcat1(ban_ip2);
596                         break;
597                 case 3:
598                         ip = strcat1(ban_ip3);
599                         break;
600                 case 4:
601                 default:
602                         ip = strcat1(ban_ip4);
603                         break;
604         }
605         if(ban_idfp)
606                 id = strcat1(ban_idfp);
607         else
608                 id = string_null;
609
610         Ban_Insert(ip, bantime, reason, 1);
611         if(id)
612                 Ban_Insert(id, bantime, reason, 1);
613         /*
614          * not needed, as we enforce the ban in Ban_Insert anyway
615         // and kick him
616         sprint(client, strcat("Kickbanned: ", reason, "\n"));
617         dropclient(client);
618          */
619 }