forked from haithun/CoD4X17a_testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsv_banlist.c
681 lines (521 loc) · 18.4 KB
/
sv_banlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/*
===========================================================================
Copyright (C) 2010-2013 Ninja and TheKelm of the IceOps-Team
This file is part of CoD4X17a-Server source code.
CoD4X17a-Server source code is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
CoD4X17a-Server source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
===========================================================================
*/
#include "q_shared.h"
#include "qcommon_io.h"
#include "qcommon.h"
#include "filesystem.h"
#include "cvar.h"
#include "sys_net.h"
#include "sys_main.h"
#include "server.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BANLIST_DEFAULT_SIZE sizeof(banList_t)*128
#define MAX_IPBANS 32
//Don't ban IPs for more than MAX_IPBAN_MINUTES minutes as they can be shared (Carrier-grade NAT)
#define MAX_IPBAN_MINUTES 30
cvar_t *banlistfile;
static int current_banlist_size;
static int current_banindex;
typedef struct { //It is only for timelimited tempbans to prevent happy reconnecting and sitting in server for 2 minutes until player is detected or keep to advertise with playernick while reconnecting
netadr_t remote;
char banmsg[128];
int uid; //Needed to delete or update bans
char guid[9];
unsigned int timeout;
int expire;
int systime;
int adminuid;
}ipBanList_t;
#define BANLIST_PBGUID_LENGTH 9
typedef struct banList_s {
time_t expire;
int playeruid;
int adminuid;
char pbguid[BANLIST_PBGUID_LENGTH];
char reason[128];
char playername[MAX_NAME_LENGTH];
}banList_t;
banList_t *banlist;
ipBanList_t ipBans[MAX_IPBANS];
qboolean SV_OversizeBanlistAlign(){
banList_t *new_blist;
int newSize;
if(current_banlist_size <= (current_banindex + 1) * sizeof(banList_t)){//Memory extension
newSize = current_banlist_size + current_banlist_size / 4;
new_blist = realloc(banlist, newSize);
if(new_blist){
banlist = new_blist;
current_banlist_size = newSize;
}else{
Com_PrintError("Could not allocate enougth memory to extend the size of banlist. Failed to add new bans\n");
return qfalse;
}
}
return qtrue;
}
qboolean SV_ParseBanlist(char* line, time_t aclock, int linenumber){
banList_t *this;
int playeruid = 0;
int adminuid = 0;
time_t expire = 0;
char reason[128];
char guid[9];
guid[8] = 0;
char playername[MAX_NAME_LENGTH];
int i;
playeruid = atoi(Info_ValueForKey(line, "uid"));
adminuid = atoi(Info_ValueForKey(line, "auid"));
expire = atoi(Info_ValueForKey(line, "exp"));
Q_strncpyz(reason, Info_ValueForKey(line, "rsn"), sizeof(reason));
Q_strncpyz(guid, Info_ValueForKey(line, "guid"), sizeof(guid));
Q_strncpyz(playername, Info_ValueForKey(line, "nick"), sizeof(guid));
if(expire < aclock && expire != (time_t)-1){
return qtrue;
}
this = banlist;
if(!this)
return qfalse;
if(playeruid){
for(i = 0; i < current_banindex; this++, i++){
if(this->playeruid == playeruid){
Com_Printf("Error: This player with UID: %i is already banned onto this server (line: %d)\n",playeruid, linenumber);
return qfalse;
}
}
}else if(guid[7]){
for(i = 0; i < current_banindex; this++, i++){
if(!Q_stricmp(this->pbguid, guid)){
Com_Printf("Error: This player with GUID: %s is already banned onto this server (line: %d)\n",guid, linenumber);
return qfalse;
}
}
}else{
Com_Printf("Error: This player has no uid/guid (line: %d)\n",linenumber);
return qfalse; //Bad entry: No Id
}
if(!SV_OversizeBanlistAlign())
return qfalse;
this = &banlist[current_banindex];
this->playeruid = playeruid;
this->adminuid = adminuid;
this->expire = expire;
Q_strncpyz(this->reason, reason, sizeof(this->reason));
Q_strncpyz(this->pbguid, guid, sizeof(this->pbguid));
Q_strncpyz(this->playername, playername, sizeof(this->playername));
current_banindex++; //Rise the array index
return qtrue;
}
void SV_LoadBanlist(){
time_t aclock;
time(&aclock);
char buf[256];
buf[0] = 0;
fileHandle_t file;
int read;
int error;
int i;
FS_SV_FOpenFileRead(banlistfile->string,&file);
if(!file){
Com_DPrintf("SV_ReadBanlist: Can not open %s for reading\n",banlistfile->string);
return;
}
for(i = 0, error = 0 ;error < 32 ;i++){
read = FS_ReadLine(buf,sizeof(buf),file);
if(read == 0){
Com_Printf("%i lines parsed from %s, %i errors occured\n",i,banlistfile->string,error);
FS_FCloseFile(file);
return;
}
if(read == -1){
Com_Printf("Can not read from %s\n",banlistfile->string);
FS_FCloseFile(file);
return;
}
if(!*buf || *buf == '/' || *buf == '\n'){
continue;
}
if(!SV_ParseBanlist(buf, aclock, i+1)) error++; //Executes the function given as argument in execute
}
Com_PrintWarning("More than 32 errors occured by reading from %s\n",banlistfile->string);
FS_FCloseFile(file);
}
void SV_WriteBanlist(){
banList_t *this;
time_t aclock;
time(&aclock);
fileHandle_t file;
char infostring[1024];
int i;
file = FS_SV_FOpenFileWrite(va("%s.tmp", banlistfile->string));
if(!file){
Com_PrintError("SV_WriteBanlist: Can not open %s for writing\n",banlistfile->string);
return;
}
this = banlist;
if(!this)
return;
for(i = 0 ; i < current_banindex; this++, i++){
if(this->expire == (time_t)-1 || this->expire > aclock){
*infostring = 0;
if(this->playeruid > 0){
Info_SetValueForKey(infostring, "uid", va("%i", this->playeruid));
}else if(this->pbguid[7]){
Info_SetValueForKey(infostring, "guid", this->pbguid);
}else{
continue;
}
Info_SetValueForKey(infostring, "nick", this->playername);
Info_SetValueForKey(infostring, "rsn", this->reason);
Info_SetValueForKey(infostring, "exp", va("%i", this->expire));
Info_SetValueForKey(infostring, "auid", va("%i", this->adminuid));
Q_strcat(infostring, sizeof(infostring), "\\\n");
FS_Write(infostring,strlen(infostring),file);
}
}
FS_FCloseFile(file);
FS_SV_HomeCopyFile(va("%s.tmp", banlistfile->string) ,banlistfile->string);
// FS_SV_Rename(va("%s.tmp", banlist->string),banlist->string);
}
char* SV_PlayerBannedByip(netadr_t *netadr){ //Gets called in SV_DirectConnect
ipBanList_t *this;
int i;
for(this = &ipBans[0], i = 0; i < 1024; this++, i++){
if(NET_CompareBaseAdr(netadr, &this->remote)){
if(Com_GetRealtime() < this->timeout)
{
if(this->expire == -1){
return va("\nEnforcing prior ban\nPermanent ban issued onto this gameserver\nYou will be never allowed to join this gameserver again\n Your UID is: %i Banning admin UID is: %i\nReason for this ban:\n%s\n",
this->uid,this->adminuid,this->banmsg);
}else{
int remaining = (int)(this->expire - Com_GetRealtime()) +1; //in seconds (+1 for fixing up a display error when only some seconds are remaining)
int d = remaining/(60*60*24);
remaining = remaining%(60*60*24);
int h = remaining/(60*60);
remaining = remaining%(60*60);
int m = remaining/60;
return va("\nEnforcing prior kick/ban\nTemporary ban issued onto this gameserver\nYou are not allowed to rejoin this gameserver for another\n %i days %i hours %i minutes\n Your UID is: %i Banning admin UID is: %i\nReason for this ban:\n%s\n",
d,h,m,this->uid,this->adminuid,this->banmsg);
}
}
}
}
return NULL;
}
//duration is in minutes
void SV_PlayerAddBanByip(netadr_t *remote, char *reason, int uid, char* guid, int adminuid, int expire){ //Gets called by future implemented ban-commands and if a prior ban got enforced again
ipBanList_t *list;
int i;
int oldest = 0;
unsigned int oldestTime = 0;
int duration;
if(!remote)
{
Com_PrintError("SV_PlayerAddBanByip: IP address is NULL\n");
return;
}
for(list = &ipBans[0], i = 0; i < 1024; list++, i++){ //At first check whether we have already an entry for this player
if(NET_CompareBaseAdr(remote, &list->remote)){
break;
}
if (list->systime < oldestTime) {
oldestTime = list->systime;
oldest = i;
}
}
if(i == 1024){
list = &ipBans[oldest];
}
list->remote = *remote;
Q_strncpyz(list->banmsg, reason, 128);
if(guid && strlen(guid) == 32)
guid += 24;
if(guid && strlen(guid) == 8)
{
Q_strncpyz(list->guid, guid, sizeof(list->guid));
}
list->expire = expire;
list->uid = uid;
list->adminuid = adminuid;
duration = expire - Com_GetRealtime();
if(duration > MAX_IPBAN_MINUTES*60 || expire == -1)
duration = MAX_IPBAN_MINUTES*60; //Don't ban IPs for more than MAX_IPBAN_MINUTES minutes as they can be shared (Carrier-grade NAT)
list->systime = Sys_Milliseconds();
list->timeout = Com_GetRealtime() + duration;
}
void SV_RemoveBanByip(netadr_t *remote, int uid, char* guid)
{
ipBanList_t *thisipban;
int i;
if(uid > 0)
{
for(thisipban = ipBans, i = 0; i < 1024; thisipban++, i++)
{
if(uid == thisipban->uid)
{
Com_Memset(thisipban,0,sizeof(ipBanList_t));
return;
}
}
}
if(guid && strlen(guid) == 32)
guid += 24;
if(guid && strlen(guid) == 8)
{
for(thisipban = ipBans, i = 0; i < 1024; thisipban++, i++)
{
if(!Q_stricmp(guid, thisipban->guid))
{
Com_Memset(thisipban,0,sizeof(ipBanList_t));
return;
}
}
}
if(remote != NULL)
{
for(thisipban = ipBans, i = 0; i < 1024; thisipban++, i++)
{
if(NET_CompareBaseAdr(remote, &thisipban->remote))
{
Com_Memset(thisipban,0,sizeof(ipBanList_t));
return;
}
}
}
}
char* SV_PlayerIsBanned(int uid, char* pbguid, netadr_t *addr){
banList_t *this;
int i;
this = banlist;
if(!this)
return NULL;
if(uid > 0){
for(i = 0 ; i < current_banindex; this++, i++){
if(this->playeruid == uid){
if(this->expire == (time_t)-1){
SV_PlayerAddBanByip(addr, this->reason, this->playeruid, pbguid, this->adminuid, -1);
return va("\nEnforcing prior ban\nPermanent ban issued onto this gameserver\nYou will be never allowed to join this gameserver again\n Your UID is: %i Banning admin UID is: %i\nReason for this ban:\n%s\n",
this->playeruid,this->adminuid,this->reason);
}
if(this->expire > Com_GetRealtime()){
int remaining = (int)(this->expire - Com_GetRealtime());
SV_PlayerAddBanByip(addr, this->reason, this->playeruid, pbguid, this->adminuid, this->expire);
int d = remaining/(60*60*24);
remaining = remaining%(60*60*24);
int h = remaining/(60*60);
remaining = remaining%(60*60);
int m = remaining/60;
return va("\nEnforcing prior kick/ban\nTemporary ban issued onto this gameserver\nYou are not allowed to rejoin this gameserver for another\n %i days %i hours %i minutes\n Your UID is: %i Banning admin UID is: %i\nReason for this ban:\n%s\n",
d,h,m,this->playeruid,this->adminuid,this->reason);
}
}
}
}else if(strlen(pbguid) == 32){
for(i = 0 ; i < current_banindex; this++, i++){
if(!Q_strncmp(this->pbguid, &pbguid[24], 8)){
if(this->expire == (time_t)-1){
return va("Permanent ban issued onto this gameserver\nYou will be never allowed to join this gameserver again\n Your GUID is: %s\nReason for this ban:\n%s\n",
this->pbguid, this->reason);
}
if(this->expire > Com_GetRealtime()){
int remaining = (int)(this->expire - Com_GetRealtime());
int d = remaining/(60*60*24);
remaining = remaining%(60*60*24);
int h = remaining/(60*60);
remaining = remaining%(60*60);
int m = remaining/60;
return va("Temporary ban issued onto this gameserver\nYou are not allowed to rejoin this gameserver for another\n %i days %i hours %i minutes\n Your GUID is: %s\nReason for this ban:\n%s\n",
d,h,m, this->pbguid, this->reason);
}
}
}
}
return NULL;
}
void SV_InitBanlist(){
Com_Memset(ipBans,0,sizeof(ipBans));
banlistfile = Cvar_RegisterString("banlistfile", "banlist.dat", CVAR_INIT, "Name of the file which holds the banlist");
current_banlist_size = BANLIST_DEFAULT_SIZE;
current_banindex = 0;
banlist = realloc(NULL, current_banlist_size);//Test for NULL ?
if(banlist){
SV_LoadBanlist();
}else{
Com_PrintError("Failed to allocate memory for the banlist. Banlist is disabled\n");
}
}
qboolean SV_ReloadBanlist(){
banList_t *this;
this = banlist;
if(!this)
return qfalse;
Com_Memset(this, 0, current_banlist_size);
current_banindex = 0; //Reset the index!
SV_LoadBanlist();
this = banlist;
if(!this)
return qfalse;
else
return qtrue;
}
qboolean SV_AddBan(int uid, int auid, char* guid, char* name, time_t expire, char* banreason){
if(!SV_OversizeBanlistAlign())
return qfalse;
banList_t *this;
int i;
this = banlist;
if(!this)
return qfalse;
int type;
if(uid > 0){
type = 0;
}else if(guid && strlen(guid) == 8){
type = 1;
}else{
return qfalse;
}
if(!SV_ReloadBanlist())
return qfalse;
for(i = 0 ; i < current_banindex; this++, i++){
switch(type)
{
case 0:
if(uid != this->playeruid)
continue;
break;
case 1:
if(Q_stricmp(guid, this->pbguid))
continue;
}
break;
}
if(i == current_banindex){
current_banindex++; //Rise the array index
}else{
if(type == 0){
Com_Printf( "Modifying banrecord for player uid: %i\n", uid);
SV_PrintAdministrativeLog( "modified banrecord of player uid: %i:", uid);
}else{
Com_Printf( "Modifying banrecord for player guid: %s\n", guid);
SV_PrintAdministrativeLog( "modified banrecord of player guid: %s:", guid);
}
}
this->playeruid = uid;
this->adminuid = auid;
this->expire = expire;
if(banreason)
Q_strncpyz(this->reason, banreason, sizeof(this->reason));
else
*this->reason = 0;
if(guid && type)
Q_strncpyz(this->pbguid, guid, sizeof(this->pbguid));
else
*this->pbguid = 0;
if(name)
Q_strncpyz(this->playername, name, sizeof(this->playername));
else
*this->playername = 0;
SV_WriteBanlist();
return qtrue;
}
qboolean SV_RemoveBan(int uid, char* guid, char* name){
banList_t *this;
int i;
int type;
qboolean succ = qfalse;
char* printguid;
char* banreason;
char* printnick;
if(uid > 0){
type = 0;
}else if(strlen(guid) == 8){
type = 1;
}else if(strlen(name) > 2){
type = 2;
}else{
return qfalse;
}
this = banlist;
if(!this)
return qfalse;
if(!SV_ReloadBanlist())
return qfalse;
for(i = 0 ; i < current_banindex; this++, i++)
{
switch(type)
{
case 0:
if(uid != this->playeruid)
continue;
break;
case 1:
if(Q_stricmp(guid, this->pbguid))
continue;
break;
case 2:
if(!Q_stricmp(name, this->playername))
continue;
}
this->expire = (time_t) 0;
SV_RemoveBanByip(NULL, this->playeruid, this->pbguid);
succ = qtrue;
if(!*this->pbguid){
printguid = "N/A";
}else{
printguid = this->pbguid;
}
if(!*this->reason){
banreason = "N/A";
}else{
banreason = this->reason;
}
if(!*this->playername){
printnick = "N/A";
}else{
printnick = this->playername;
}
Com_Printf("Removing ban for Nick: %s, UID: %i, GUID: %s, Banreason: %s\n", printnick, this->playeruid, printguid, banreason);
SV_PrintAdministrativeLog("Removing ban for Nick: %s, UID: %i, GUID: %s, Banreason: %s\n", printnick, this->playeruid, printguid, banreason);
}
if(succ)
SV_WriteBanlist();
return succ;
}
void SV_DumpBanlist(){
banList_t *this;
time_t aclock;
time(&aclock);
int i, k;
char *timestr;
this = banlist;
if(!this)
return;
for(i = 0, k = 0; i < current_banindex; this++, i++){
if(this->expire == (time_t)-1 || this->expire > aclock){
k++;
if(this->expire == (time_t)-1){
timestr = "Never";
}else{
timestr = ctime(&this->expire);
timestr[strlen(timestr) -1] = 0;
}
Com_Printf("%i uid: %i; nick: %s; guid: %s; adminuid: %i; expire: %s; reason: %s\n", i, this->playeruid, this->playername, this->pbguid, this->adminuid, timestr, this->reason);
}
}
Com_Printf("%i Active bans\n", k);
}