forked from haithun/CoD4X17a_testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sv_main.c
2663 lines (2082 loc) · 74.6 KB
/
sv_main.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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
===========================================================================
Copyright (C) 2010-2013 Ninja and TheKelm of the IceOps-Team
Copyright (C) 1999-2005 Id Software, Inc.
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_mem.h"
#include "qcommon.h"
#include "cvar.h"
#include "cmd.h"
#include "msg.h"
#include "server.h"
#include "plugin_handler.h"
#include "net_game_conf.h"
#include "misc.h"
#include "g_sv_shared.h"
#include "g_shared.h"
#include "q_platform.h"
#include "punkbuster.h"
#include "sys_thread.h"
#include "sys_main.h"
#include "scr_vm.h"
#include "xassets.h"
#include "nvconfig.h"
#include "hl2rcon.h"
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
cvar_t *sv_protocol;
cvar_t *sv_privateClients; // number of clients reserved for password
cvar_t *sv_hostname;
cvar_t *sv_punkbuster;
cvar_t *sv_minPing;
cvar_t *sv_maxPing;
cvar_t *sv_queryIgnoreMegs;
cvar_t *sv_queryIgnoreTime;
cvar_t *sv_privatePassword; // password for the privateClient slots
cvar_t *sv_allowDownload;
cvar_t *sv_wwwDownload;
cvar_t *sv_wwwBaseURL;
cvar_t *sv_wwwDlDisconnected;
cvar_t *sv_voice;
cvar_t *sv_voiceQuality;
cvar_t *sv_cheats;
cvar_t *sv_rconPassword; // password for remote server commands
cvar_t *sv_reconnectlimit; // minimum seconds between connect messages
cvar_t *sv_padPackets; // add nop bytes to messages
cvar_t *sv_mapRotation;
cvar_t *sv_mapRotationCurrent;
cvar_t *sv_nextmap;
cvar_t *sv_paused;
cvar_t *sv_killserver; // menu system can set to 1 to shut server down
cvar_t *sv_timeout; // seconds without any message while in game
cvar_t *sv_connectTimeout; // seconds without any message while connecting
cvar_t *sv_zombieTime; // seconds to sink messages after disconnect
cvar_t *sv_consayname;
cvar_t *sv_contellname;
cvar_t *sv_password;
cvar_t *g_motd;
cvar_t *sv_modStats;
cvar_t *sv_authorizemode;
cvar_t *sv_showasranked;
cvar_t *sv_statusfile;
cvar_t *g_friendlyPlayerCanBlock;
cvar_t *g_FFAPlayerCanBlock;
cvar_t *sv_autodemorecord;
cvar_t *sv_demoCompletedCmd;
cvar_t *sv_master[MAX_MASTER_SERVERS]; // master server ip address
cvar_t *g_mapstarttime;
cvar_t *sv_uptime;
serverStaticExt_t svse; // persistant server info across maps
permServerStatic_t psvs; // persistant even if server does shutdown
#define SV_OUTPUTBUF_LENGTH 1024
/*
cvar_t *sv_fps = NULL; // time rate for running non-clients
cvar_t *sv_timeout; // seconds without any message
cvar_t *sv_zombietime; // seconds to sink messages after disconnect
cvar_t *sv_maxclients;
cvar_t *sv_showloss; // report when usercmds are lost
cvar_t *sv_mapname;
cvar_t *sv_mapChecksum;
cvar_t *sv_serverid;
cvar_t *sv_minRate;
cvar_t *sv_maxRate;
cvar_t *sv_gametype;
cvar_t *sv_pure;
cvar_t *sv_floodProtect;
cvar_t *sv_lanForceRate; // dedicated 1 (LAN) server forces local client rates to 99999 (bug #491)
#ifndef STANDALONE
cvar_t *sv_strictAuth;
#endif
cvar_t *sv_banFile;
serverBan_t serverBans[SERVER_MAXBANS];
int serverBansCount = 0;
*/
static netadr_t master_adr[MAX_MASTER_SERVERS][2];
/*
=============================================================================
EVENT MESSAGES
=============================================================================
*/
/*
void SV_DumpReliableCommands( client_t *client, const char* cmd) {
if(!com_developer || com_developer->integer < 2)
return;
char msg[1040];
Com_sprintf(msg, sizeof(msg), "Cl: %i, Seq: %i, Time: %i, NotAck: %i, Len: %i, Msg: %s\n",
client - svs.clients, client->reliableSequence, svs.time ,client->reliableSequence - client->reliableAcknowledge, strlen(cmd), cmd);
Com_Printf("^5%s", msg);
Sys_EnterCriticalSection(5);
if ( com_logfile && com_logfile->integer ) {
// TTimo: only open the qconsole.log if the filesystem is in an initialized state
// also, avoid recursing in the qconsole.log opening (i.e. if fs_debug is on)
if ( !reliabledump && FS_Initialized()) {
struct tm *newtime;
time_t aclock;
time( &aclock );
newtime = localtime( &aclock );
reliabledump = FS_FOpenFileWrite( "reliableCmds.log" );
if ( com_logfile->integer > 1 && reliabledump) {
// force it to not buffer so we get valid
// data even if we are crashing
FS_ForceFlush(reliabledump);
}
if ( reliabledump ) FS_Write(va("\nLogfile opened on %s\n", asctime( newtime )), strlen(va("\nLogfile opened on %s\n", asctime( newtime ))), reliabledump);
}
if ( reliabledump && FS_Initialized()) {
FS_Write(msg, strlen(msg), reliabledump);
}
}
Sys_LeaveCriticalSection(5);
}
*/
/*
======================
SV_AddServerCommand
The given command will be transmitted to the client, and is guaranteed to
not have future snapshot_t executed before it is executed
======================
*/
/*
__cdecl void SV_AddServerCommand( client_t *client, int type, const char *cmd ) {
// SV_DumpReliableCommands( client, cmd);
// Com_Printf("C: %s\n", cmd);
SV_AddServerCommand_old(client, type, cmd);
return;
int index, i;
if(client->canNotReliable)
return;
if(client->state < CS_CONNECTED)
return;
if( ! *cmd )
return;
// extclient_t* extcl = &svse.extclients[ client - svs.clients ];
client->reliableSequence++;
// extcl->reliableSequence++;
// Com_PrintNoRedirect("CMD: %i ^5%s\n", client->reliableSequence - client->reliableAcknowledge, cmd);
// if we would be losing an old command that hasn't been acknowledged,
// we must drop the connection
// we check == instead of >= so a broadcast print added by SV_DropClient()
// doesn't cause a recursive drop client
if ( client->reliableSequence - client->reliableAcknowledge == MAX_RELIABLE_COMMANDS + 1 ) {
Com_PrintNoRedirect("Client: %i Reliable commandbuffer overflow\n", client - svs.clients);
Com_PrintNoRedirect( "Client lost reliable commands\n");
Com_PrintNoRedirect( "===== pending server commands =====\n" );
for ( i = client->reliableAcknowledge + 1 ; i <= client->reliableSequence ; i++ ) {
// Com_DPrintNoRedirect( "cmd %5d: %s\n", i, extcl->reliableCommands[ i & (MAX_RELIABLE_COMMANDS-1) ].command );
Com_PrintNoRedirect( "cmd %5d: %s\n", i, client->reliableCommands[ i & (MAX_RELIABLE_COMMANDS-1) ].command );
}
Com_PrintNoRedirect("cmd: %s\n", cmd);
Com_PrintNoRedirect( "====================================\n" );
SV_DropClient( client, "Server command overflow" );
return;
}
index = client->reliableSequence & ( MAX_RELIABLE_COMMANDS - 1 );
// MSG_WriteReliableCommandToBuffer(cmd, extcl->reliableCommands[ index ].command, sizeof( extcl->reliableCommands[ index ].command ));
// extcl->reliableCommands[ index ].cmdTime = svs.time;
// extcl->reliableCommands[ index ].cmdType = 1;
MSG_WriteReliableCommandToBuffer(cmd, client->reliableCommands[ index ].command, sizeof( client->reliableCommands[ index ].command ));
client->reliableCommands[ index ].cmdTime = svs.time;
client->reliableCommands[ index ].cmdType = 1;
SV_DumpReliableCommands( client, cmd);
}
*/
/*
=================
SV_SendServerCommand
Sends a reliable command string to be interpreted by
the client game module: "cp", "print", "chat", etc
A NULL client will broadcast to all clients
=================
*/
/*known stuff
"t \" == open callvote screen
"h \" == chat
"c \" == print bold to players screen
"e \" == print to players console
*/
void QDECL SV_SendServerCommand(client_t *cl, const char *fmt, ...) {
va_list argptr;
byte message[MAX_MSGLEN];
client_t *client;
int j;
va_start (argptr,fmt);
Q_vsnprintf ((char *)message, sizeof(message), fmt,argptr);
va_end (argptr);
if ( strlen ((char *)message) > 1022 ) {
return;
}
if ( cl != NULL ){
SV_AddServerCommand_old(cl, 0, (char *)message );
return;
}
// hack to echo broadcast prints to console
if ( !strncmp( (char *)message, "say", 3) ) {
Com_Printf("broadcast: %s\n", SV_ExpandNewlines((char *)message) );
}
// send the data to all relevent clients
for (j = 0, client = svs.clients; j < sv_maxclients->integer; j++, client++) {
if ( client->state < CS_PRIMED ) {
continue;
}
SV_AddServerCommand_old(client, 0, (char *)message );
}
}
/*
==============================================================================
CONNECTIONLESS COMMANDS
==============================================================================
*/
typedef struct leakyBucket_s leakyBucket_t;
struct leakyBucket_s {
byte type;
union {
byte _4[4];
byte _6[16];
} ipv;
unsigned long long lastTime;
signed char burst;
long hash;
leakyBucket_t *prev, *next;
};
typedef struct{
int max_buckets;
int max_hashes;
leakyBucket_t *buckets;
leakyBucket_t **bucketHashes;
int queryLimitsEnabled;
leakyBucket_t infoBucket;
leakyBucket_t statusBucket;
leakyBucket_t rconBucket;
}queryLimit_t;
static queryLimit_t querylimit;
// This is deliberately quite large to make it more of an effort to DoS
/*
================
SVC_RateLimitInit
Init the rate limit system
================
*/
static void SVC_RateLimitInit( ){
int bytes;
if(!sv_queryIgnoreMegs->integer)
{
Com_Printf("QUERY LIMIT: Querylimiting is disabled\n");
querylimit.queryLimitsEnabled = 0;
return;
}
bytes = sv_queryIgnoreMegs->integer * 1024*1024;
querylimit.max_buckets = bytes / sizeof(leakyBucket_t);
querylimit.max_hashes = 4096; //static
int totalsize = querylimit.max_buckets * sizeof(leakyBucket_t) + querylimit.max_hashes * sizeof(leakyBucket_t*);
querylimit.buckets = Z_Malloc(totalsize);
if(!querylimit.buckets)
{
Com_PrintError("QUERY LIMIT: System is out of memory. All queries are disabled\n");
querylimit.queryLimitsEnabled = -1;
}
querylimit.bucketHashes = (leakyBucket_t**)&querylimit.buckets[querylimit.max_buckets];
Com_Printf("QUERY LIMIT: Querylimiting is enabled\n");
querylimit.queryLimitsEnabled = 1;
}
/*
================
SVC_HashForAddress
================
*/
__optimize3 __regparm1 static long SVC_HashForAddress( netadr_t *address ) {
byte *ip = NULL;
size_t size = 0;
int i;
long hash = 0;
switch ( address->type ) {
case NA_IP: ip = address->ip; size = 4; break;
case NA_IP6: ip = address->ip6; size = 16; break;
default: break;
}
for ( i = 0; i < size; i++ ) {
hash += (long)( ip[ i ] ) * ( i + 119 );
}
hash = ( hash ^ ( hash >> 10 ) ^ ( hash >> 20 ) ^ psvs.randint);
hash &= ( querylimit.max_hashes - 1 );
return hash;
}
/*
================
SVC_BucketForAddress
Find or allocate a bucket for an address
================
*/
__optimize3 __regparm3 static leakyBucket_t *SVC_BucketForAddress( netadr_t *address, int burst, int period ) {
leakyBucket_t *bucket = NULL;
int i;
long hash = SVC_HashForAddress( address );
unsigned long long now = com_uFrameTime;
for ( bucket = querylimit.bucketHashes[ hash ]; bucket; bucket = bucket->next ) {
switch ( bucket->type ) {
case NA_IP:
if ( memcmp( bucket->ipv._4, address->ip, 4 ) == 0 ) {
return bucket;
}
break;
case NA_IP6:
if ( memcmp( bucket->ipv._6, address->ip6, 16 ) == 0 ) {
return bucket;
}
break;
default:
break;
}
}
for ( i = 0; i < querylimit.max_buckets; i++ ) {
int interval;
bucket = &querylimit.buckets[ i ];
interval = now - bucket->lastTime;
// Reclaim expired buckets
if ( bucket->lastTime > 0 && ( interval > ( burst * period ) || interval < 0 ) )
{
if ( bucket->prev != NULL ) {
bucket->prev->next = bucket->next;
} else {
querylimit.bucketHashes[ bucket->hash ] = bucket->next;
}
if ( bucket->next != NULL ) {
bucket->next->prev = bucket->prev;
}
Com_Memset( bucket, 0, sizeof( leakyBucket_t ) );
}
if ( bucket->type == NA_BAD ) {
bucket->type = address->type;
switch ( address->type ) {
case NA_IP: bucket->ipv._4[0] = address->ip[0];
bucket->ipv._4[1] = address->ip[1];
bucket->ipv._4[2] = address->ip[2];
bucket->ipv._4[3] = address->ip[3];
break;
case NA_IP6: Com_Memcpy( bucket->ipv._6, address->ip6, 16 ); break;
default: return NULL;
}
bucket->lastTime = now;
bucket->burst = 0;
bucket->hash = hash;
// Add to the head of the relevant hash chain
bucket->next = querylimit.bucketHashes[ hash ];
if ( querylimit.bucketHashes[ hash ] != NULL ) {
querylimit.bucketHashes[ hash ]->prev = bucket;
}
bucket->prev = NULL;
querylimit.bucketHashes[ hash ] = bucket;
return bucket;
}
}
// Couldn't allocate a bucket for this address
return NULL;
}
/*
================
SVC_RateLimit
================
*/
/*__optimize3*/ __attribute__((always_inline)) static qboolean SVC_RateLimit( leakyBucket_t *bucket, int burst, int period ) {
if ( bucket != NULL ) {
unsigned long long now = com_uFrameTime;
int interval = now - bucket->lastTime;
int expired = interval / period;
int expiredRemainder = interval % period;
if ( expired > bucket->burst ) {
bucket->burst = 0;
bucket->lastTime = now;
} else {
bucket->burst -= expired;
bucket->lastTime = now - expiredRemainder;
}
if ( bucket->burst < burst ) {
bucket->burst++;
return qfalse;
}
}
return qtrue;
}
/*
================
SVC_RateLimitAddress
Rate limit for a particular address
================
*/
__optimize3 __regparm3 static qboolean SVC_RateLimitAddress( netadr_t *from, int burst, int period ) {
if(Sys_IsLANAddress(from))
return qfalse;
if(querylimit.queryLimitsEnabled == 1)
{
leakyBucket_t *bucket = SVC_BucketForAddress( from, burst, period );
return SVC_RateLimit( bucket, burst, period );
}else if(querylimit.queryLimitsEnabled == 0){
return qfalse;
}else{//Init error, to be save we deny everything
return qtrue;
}
}
/*
================
SVC_Status
Responds with all the info that qplug or qspy can see about the server
and all connected players. Used for getting detailed information after
the simple info query.
================
*/
__optimize3 __regparm1 void SVC_Status( netadr_t *from ) {
char player[1024];
char status[MAX_MSGLEN];
int i;
client_t *cl;
gclient_t *gclient;
int statusLength;
int playerLength;
char infostring[MAX_INFO_STRING];
// Allow getstatus to be DoSed relatively easily, but prevent
// excess outbound bandwidth usage when being flooded inbound
if ( SVC_RateLimit( &querylimit.statusBucket, 20, 20000 ) ) {
// Com_DPrintf( "SVC_Status: overall rate limit exceeded, dropping request\n" );
return;
}
// Prevent using getstatus as an amplifier
if ( SVC_RateLimitAddress( from, 2, sv_queryIgnoreTime->integer*1000 ) ) {
// Com_DPrintf( "SVC_Status: rate limit from %s exceeded, dropping request\n", NET_AdrToString( *from ) );
return;
}
if(strlen(SV_Cmd_Argv(1)) > 128)
return;
strcpy( infostring, Cvar_InfoString( 0, (CVAR_SERVERINFO | CVAR_NORESTART)) );
// echo back the parameter to status. so master servers can use it as a challenge
// to prevent timed spoofed reply packets that add ghost servers
Info_SetValueForKey( infostring, "challenge", SV_Cmd_Argv( 1 ) );
if(*sv_password->string)
Info_SetValueForKey( infostring, "pswrd", "1");
if(sv_authorizemode->integer == 1) //Backward compatibility
Info_SetValueForKey( infostring, "type", "1");
else
Info_SetValueForKey( infostring, "type", va("%i", sv_authorizemode->integer));
// add "demo" to the sv_keywords if restricted
status[0] = 0;
statusLength = 0;
for ( i = 0, gclient = level.clients ; i < sv_maxclients->integer ; i++, gclient++ ) {
cl = &svs.clients[i];
if ( cl->state >= CS_CONNECTED ) {
Com_sprintf( player, sizeof( player ), "%i %i \"%s\"\n",
gclient->pers.scoreboard.score, cl->ping, cl->name );
playerLength = strlen( player );
if ( statusLength + playerLength >= sizeof( status ) ) {
break; // can't hold any more
}
strcpy( status + statusLength, player );
statusLength += playerLength;
}
}
NET_OutOfBandPrint( NS_SERVER, from, "statusResponse\n%s\n%s", infostring, status );
}
/*
================
SVC_Info
Responds with a short info message that should be enough to determine
if a user is interested in a server to do a full status
================
*/
__optimize3 __regparm1 void SVC_Info( netadr_t *from ) {
int i, count, humans;
qboolean masterserver;
char infostring[MAX_INFO_STRING];
char* s;
s = SV_Cmd_Argv(1);
masterserver = qfalse;
if(from->type == NA_IP)
{
for(i = 0; i < MAX_MASTER_SERVERS; i++)
{
if(NET_CompareAdr( from, &master_adr[i][0]))
{
masterserver = qtrue;
break;
}
}
}else{
for(i = 0; i < MAX_MASTER_SERVERS; i++)
{
if(NET_CompareAdr( from, &master_adr[i][1]))
{
masterserver = qtrue;
break;
}
}
}
if(!masterserver)
{
// Allow getstatus to be DoSed relatively easily, but prevent
// excess outbound bandwidth usage when being flooded inbound
if ( SVC_RateLimit( &querylimit.infoBucket, 100, 100000 ) ) {
// Com_DPrintf( "SVC_Info: overall rate limit exceeded, dropping request\n" );
return;
}
// Prevent using getstatus as an amplifier
if ( SVC_RateLimitAddress( from, 4, sv_queryIgnoreTime->integer*1000 )) {
// Com_DPrintf( "SVC_Info: rate limit from %s exceeded, dropping request\n", NET_AdrToString( *from ) );
return;
}
infostring[0] = 0;
}else{
infostring[0] = 0;
Info_SetValueForKey( infostring, "server_id", va("%i", psvs.masterServer_id));
}
/*
* Check whether Cmd_Argv(1) has a sane length. This was not done in the original Quake3 version which led
* to the Infostring bug discovered by Luigi Auriemma. See http://aluigi.altervista.org/ for the advisory.
*/
// A maximum challenge length of 128 should be more than plenty.
if(strlen(SV_Cmd_Argv(1)) > 128)
return;
// don't count privateclients
count = humans = 0;
for ( i = 0 ; i < sv_maxclients->integer ; i++ )
{
if ( svs.clients[i].state >= CS_CONNECTED ) {
count++;
if (svs.clients[i].netchan.remoteAddress.type != NA_BOT) {
humans++;
}
}
}
// echo back the parameter to status. so servers can use it as a challenge
// to prevent timed spoofed reply packets that add ghost servers
Info_SetValueForKey( infostring, "challenge", s);
//Info_SetValueForKey( infostring, "gamename", com_gamename->string );
Info_SetValueForKey(infostring, "protocol", "6");
Info_SetValueForKey( infostring, "hostname", sv_hostname->string );
if(sv_authorizemode->integer == 1) //Backward compatibility
Info_SetValueForKey( infostring, "type", "1");
else
Info_SetValueForKey( infostring, "type", va("%i", sv_authorizemode->integer));
Info_SetValueForKey( infostring, "mapname", sv_mapname->string );
Info_SetValueForKey( infostring, "clients", va("%i", count) );
Info_SetValueForKey( infostring, "g_humanplayers", va("%i", humans));
Info_SetValueForKey( infostring, "sv_maxclients", va("%i", sv_maxclients->integer - sv_privateClients->integer ) );
Info_SetValueForKey( infostring, "gametype", g_gametype->string );
Info_SetValueForKey( infostring, "pure", va("%i", sv_pure->boolean ) );
Info_SetValueForKey( infostring, "build", va("%i", BUILD_NUMBER));
Info_SetValueForKey( infostring, "shortversion", Q3_VERSION );
if(*sv_password->string)
Info_SetValueForKey( infostring, "pswrd", "1");
else
Info_SetValueForKey( infostring, "pswrd", "0");
if(g_cvar_valueforkey("scr_team_fftype")){
Info_SetValueForKey( infostring, "ff", va("%i", g_cvar_valueforkey("scr_team_fftype")));
}
if(g_cvar_valueforkey("scr_game_allowkillcam")){
Info_SetValueForKey( infostring, "ki", "1");
}
if(g_cvar_valueforkey("scr_hardcore")){
Info_SetValueForKey( infostring, "hc", "1");
}
if(g_cvar_valueforkey("scr_oldschool")){
Info_SetValueForKey( infostring, "od", "1");
}
Info_SetValueForKey( infostring, "hw", "1");
if(fs_game->string[0] == '\0' || sv_showasranked->boolean){
Info_SetValueForKey( infostring, "mod", "0");
}else{
Info_SetValueForKey( infostring, "mod", "1");
}
Info_SetValueForKey( infostring, "voice", va("%i", sv_voice->boolean ) );
Info_SetValueForKey( infostring, "pb", va("%i", sv_punkbuster->boolean) );
if( sv_maxPing->integer ) {
Info_SetValueForKey( infostring, "sv_maxPing", va("%i", sv_maxPing->integer) );
}
if( fs_game->string[0] != '\0' ) {
Info_SetValueForKey( infostring, "game", fs_game->string );
}
NET_OutOfBandPrint( NS_SERVER, from, "infoResponse\n%s", infostring );
}
/*
================
SVC_FlushRedirect
================
*/
static void SV_FlushRedirect( char *outputbuf, qboolean lastcommand ) {
NET_OutOfBandPrint( NS_SERVER, &svse.redirectAddress, "print\n%s", outputbuf );
}
/*
===============
SVC_RemoteCommand
An rcon packet arrived from the network.
Shift down the remaining args
Redirect all printfs
===============
*/
__optimize3 __regparm2 static void SVC_RemoteCommand( netadr_t *from, msg_t *msg ) {
// TTimo - scaled down to accumulate, but not overflow anything network wise, print wise etc.
// (OOB messages are the bottleneck here)
char sv_outputbuf[SV_OUTPUTBUF_LENGTH];
char *cmd_aux;
svse.redirectAddress = *from;
if ( strcmp (SV_Cmd_Argv(1), sv_rconPassword->string )) {
//Send only one deny answer out in 100 ms
if ( SVC_RateLimit( &querylimit.rconBucket, 1, 100 ) ) {
// Com_DPrintf( "SVC_RemoteCommand: rate limit exceeded for bad rcon\n" );
return;
}
Com_Printf ("Bad rcon from %s\n", NET_AdrToString (from) );
Com_BeginRedirect (sv_outputbuf, SV_OUTPUTBUF_LENGTH, SV_FlushRedirect);
Com_Printf ("Bad rcon");
Com_EndRedirect ();
return;
}
if ( strlen( sv_rconPassword->string) < 8 ) {
Com_BeginRedirect (sv_outputbuf, SV_OUTPUTBUF_LENGTH, SV_FlushRedirect);
Com_Printf ("No rconpassword set on server or password is shorter than 8 characters.\n");
Com_EndRedirect ();
return;
}
//Everything fine, process the request
MSG_BeginReading(msg);
MSG_ReadLong(msg); //0xffffffff
MSG_ReadLong(msg); //rcon
cmd_aux = MSG_ReadStringLine(msg);
// https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=543
// get the command directly, "rcon <pass> <command>" to avoid quoting issues
// extract the command by walking
// since the cmd formatting can fuckup (amount of spaces), using a dumb step by step parsing
while(cmd_aux[0]==' ')//Skipping space before the password
cmd_aux++;
if(cmd_aux[0]== '"')//Skipping the password
{
cmd_aux++;
while(cmd_aux[0] != '"' && cmd_aux[0])
cmd_aux++;
cmd_aux++;
}else{
while(cmd_aux[0] != ' ' && cmd_aux[0])
cmd_aux++;
}
while(cmd_aux[0] == ' ')//Skipping space after the password
cmd_aux++;
Com_Printf ("Rcon from %s: %s\n", NET_AdrToString (from), cmd_aux );
Com_BeginRedirect (sv_outputbuf, SV_OUTPUTBUF_LENGTH, SV_FlushRedirect);
if(!Q_stricmpn(cmd_aux, "pb_sv_", 6)){
Q_strchrrepl(cmd_aux, '\"', ' ');
Cmd_ExecuteSingleCommand(0,0, cmd_aux);
PbServerForceProcess();
}else{
Cmd_ExecuteSingleCommand(0,0, cmd_aux);
}
Com_EndRedirect ();
}
/*
=================
SV_ConnectionlessPacket
A connectionless packet has four leading 0xff
characters to distinguish it from a game channel.
Clients that are in the game can still send
connectionless packets.
===========h======
*/
__optimize3 __regparm2 void SV_ConnectionlessPacket( netadr_t *from, msg_t *msg ) {
char *s;
char *c;
int clnum;
int i;
client_t *cl;
MSG_BeginReading( msg );
MSG_ReadLong( msg ); // skip the -1 marker
s = MSG_ReadStringLine( msg );
SV_Cmd_TokenizeString( s );
c = SV_Cmd_Argv(0);
Com_DPrintf ("SV packet %s : %s\n", NET_AdrToString(from), c);
//Most sensitive OOB commands first
if (!Q_stricmp(c, "getstatus")) {
SVC_Status( from );
} else if (!Q_stricmp(c, "getinfo")) {
SVC_Info( from );
} else if (!Q_stricmp(c, "rcon")) {
SVC_RemoteCommand( from, msg );
} else if (!Q_strncmp("PB_", (char *) &msg->data[4], 3)) {
//pb_sv_process here
SV_Cmd_EndTokenizeString();
if(msg->data[7] == 0x43 || msg->data[7] == 0x31 || msg->data[7] == 0x4a)
return;
for ( i = 0, clnum = -1, cl = svs.clients ; i < sv_maxclients->integer ; i++, cl++ ){
if ( !NET_CompareBaseAdr( from, &cl->netchan.remoteAddress ) ) continue;
if(cl->netchan.remoteAddress.port != from->port) continue;
clnum = i;
break;
}
if(NET_GetDefaultCommunicationSocket() == NULL)
NET_RegisterDefaultCommunicationSocket(from);
PbSvAddEvent(13, clnum, msg->cursize-4, (char *)&msg->data[4]);
return;
} else if (!Q_stricmp(c, "connect")) {
SV_DirectConnect( from );
} else if (!Q_stricmp(c, "ipAuthorize")) {
SV_AuthorizeIpPacket( from );
} else if (!Q_stricmp(c, "stats")) {
SV_ReceiveStats(from, msg);
} else if (!Q_stricmp(c, "rcon")) {
SVC_RemoteCommand( from, msg );
} else if (!Q_stricmp(c, "getchallenge")) {
SV_GetChallenge(from);
} else {
Com_DPrintf ("bad connectionless packet from %s\n", NET_AdrToString (from));
}
SV_Cmd_EndTokenizeString();
return;
}
//============================================================================
/*
=================
SV_ReadPackets
=================
*/
__optimize3 __regparm2 void SV_PacketEvent( netadr_t *from, msg_t *msg ) {
int i;
client_t *cl;
short qport;
if(!com_sv_running->boolean)
return;
// check for connectionless packet (0xffffffff) first
if ( msg->cursize >= 4 && *(int *)msg->data == -1 ) {
SV_ConnectionlessPacket( from, msg );
return;
}
SV_ResetSekeletonCache();
// read the qport out of the message so we can fix up
// stupid address translating routers
//InsertPluginEvent
/*
seqclient_t* clq = CL_AddrToServer(from);
if(clq)
{
CL_ReadPacket( clq ,msg );
return;
}
*/