]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/ipban.qc
Merge branch 'master' into terencehill/glowmod_color_fix
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / ipban.qc
1 #include "ipban.qh"
2
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>
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                 if (!client.crypto_idfp)
457                         LOG_INFOF("^1NOTE:^7 banned client %s just tried to enter\n",
458                                 client.netaddress);
459                 else
460                         LOG_INFOF("^1NOTE:^7 banned client %s (%s) just tried to enter\n",
461                                 client.netaddress, client.crypto_idfp);
462
463                 if(autocvar_g_ban_telluser)
464                         sprint(client, "You are banned from this server.\n");
465                 dropclient(client);
466                 return true;
467         }
468         return false;
469 }
470
471 .bool ban_checked;
472 bool Ban_MaybeEnforceBanOnce(entity client)
473 {
474         if (client.ban_checked) return false;
475         client.ban_checked = true;
476         return Ban_MaybeEnforceBan(client);
477 }
478
479 string Ban_Enforce(float j, string reason)
480 {
481         string s;
482
483         // Enforce our new ban
484         s = "";
485         FOREACH_CLIENTSLOT(IS_REAL_CLIENT(it),
486         {
487                 if(Ban_IsClientBanned(it, j))
488                 {
489                         if(reason != "")
490                         {
491                                 if(s == "")
492                                         reason = strcat(reason, ": affects ");
493                                 else
494                                         reason = strcat(reason, ", ");
495                                 reason = strcat(reason, it.netname);
496                         }
497                         s = strcat(s, "^1NOTE:^7 banned client ", it.netname, "^7 has to go\n");
498                         dropclient(it);
499                 }
500         });
501         bprint(s);
502
503         return reason;
504 }
505
506 float Ban_Insert(string ip, float bantime, string reason, float dosync)
507 {
508         float i;
509         float j;
510         float bestscore;
511
512         // already banned?
513         for(i = 0; i < ban_count; ++i)
514                 if(ban_ip[i] == ip)
515                 {
516                         // prolong the ban
517                         if(time + bantime > ban_expire[i])
518                         {
519                                 ban_expire[i] = time + bantime;
520                                 LOG_TRACE(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now");
521                         }
522                         else
523                                 LOG_TRACE(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now");
524
525                         // and enforce
526                         reason = Ban_Enforce(i, reason);
527
528                         // and abort
529                         if(dosync)
530                                 if(reason != "")
531                                         if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
532                                                 OnlineBanList_SendBan(ip, bantime, reason);
533
534                         return false;
535                 }
536
537         // do we have a free slot?
538         for(i = 0; i < ban_count; ++i)
539                 if(time > ban_expire[i])
540                         break;
541         // no free slot? Then look for the one who would get unbanned next
542         if(i >= BAN_MAX)
543         {
544                 i = 0;
545                 bestscore = ban_expire[i];
546                 for(j = 1; j < ban_count; ++j)
547                 {
548                         if(ban_expire[j] < bestscore)
549                         {
550                                 i = j;
551                                 bestscore = ban_expire[i];
552                         }
553                 }
554         }
555         // if we replace someone, will we be banned longer than him (so long-term
556         // bans never get overridden by short-term bans)
557         if(i < ban_count)
558         if(ban_expire[i] > time + bantime)
559         {
560                 LOG_INFO(ip, " could not get banned due to no free ban slot");
561                 return false;
562         }
563         // okay, insert our new victim as i
564         Ban_Delete(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);
569
570         Ban_SaveBans();
571
572         reason = Ban_Enforce(i, reason);
573
574         // and abort
575         if(dosync)
576                 if(reason != "")
577                         if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
578                                 OnlineBanList_SendBan(ip, bantime, reason);
579
580         return true;
581 }
582
583 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
584 {
585         string ip, id;
586         if(!Ban_GetClientIP(client))
587         {
588                 sprint(client, strcat("Kickbanned: ", reason, "\n"));
589                 dropclient(client);
590                 return;
591         }
592
593         // who to ban?
594         switch(masksize)
595         {
596                 case 1:
597                         ip = strcat1(ban_ip1);
598                         break;
599                 case 2:
600                         ip = strcat1(ban_ip2);
601                         break;
602                 case 3:
603                         ip = strcat1(ban_ip3);
604                         break;
605                 case 4:
606                 default:
607                         ip = strcat1(ban_ip4);
608                         break;
609         }
610         if(ban_idfp)
611                 id = strcat1(ban_idfp);
612         else
613                 id = string_null;
614
615         Ban_Insert(ip, bantime, reason, 1);
616         if(id)
617                 Ban_Insert(id, bantime, reason, 1);
618         /*
619          * not needed, as we enforce the ban in Ban_Insert anyway
620         // and kick him
621         sprint(client, strcat("Kickbanned: ", reason, "\n"));
622         dropclient(client);
623          */
624 }