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