forked from haithun/CoD4X17a_testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsv_ingameadmin.c
691 lines (529 loc) · 17.8 KB
/
sv_ingameadmin.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
/*
===========================================================================
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.h"
#include "qcommon_io.h"
#include "qcommon_mem.h"
#include "qcommon_logprint.h"
#include "cvar.h"
#include "cmd.h"
#include "server.h"
#include "punkbuster.h"
#include "nvconfig.h"
#include <string.h>
#include <stdarg.h>
#define SV_OUTPUTBUF_LENGTH 1024
#ifndef MAXPRINTMSG
#define MAXPRINTMSG 1024
#endif
static cvar_t* sv_rconsys;
typedef struct adminPower_s {
struct adminPower_s *next;
char name[16];
int uid;
char guid[9];
int power;
}adminPower_t;
typedef struct{
int currentCmdPower; //used to set an execution permissionlevel - Default is 100 but if users execute commands it will be the users level
int currentCmdInvoker; //used to set an Invoker UID - Default is 0 but if users execute commands it will be his own UID
char currentCmdInvokerGuid[9]; //Same as above but if guid is used
int clientnum; //Clientnum will be -1 if rcon is used
qboolean authserver;
}cmdInvoker_t;
static cmdInvoker_t cmdInvoker;
static adminPower_t *adminpower;
static client_t *redirectClient;
static qboolean cmdSystemInitialized;
int SV_RemoteCmdGetInvokerUid()
{
return cmdInvoker.currentCmdInvoker;
}
const char* SV_RemoteCmdGetInvokerGuid()
{
return cmdInvoker.currentCmdInvokerGuid;
}
int SV_RemoteCmdGetInvokerClnum()
{
return cmdInvoker.clientnum;
}
int SV_RemoteCmdGetInvokerPower()
{
return cmdInvoker.currentCmdPower;
}
void SV_RemoteCmdInit(){
sv_rconsys = Cvar_RegisterBool("sv_rconsys", qtrue, CVAR_ARCHIVE, "Disable/enable the internal remote-command-system");
if(!sv_rconsys->boolean) return;
//Init the permission table with default values
Cmd_ResetPower();
Cmd_SetPower("cmdlist", 1);
Cmd_SetPower("serverinfo", 1);
Cmd_SetPower("systeminfo", 1);
Cmd_SetPower("ministatus", 1);
Cmd_SetPower("status", 30);
Cmd_SetPower("dumpuser", 40);
Cmd_SetPower("kick", 35);
Cmd_SetPower("tempban", 50);
Cmd_SetPower("unban", 50);
Cmd_SetPower("permban", 80);
Cmd_SetPower("btempban", 80);
Cmd_SetPower("bpermban", 70);
Cmd_SetPower("map", 60);
Cmd_SetPower("map_restart", 50);
Cmd_SetPower("adminlist", 90);
Cmd_SetPower("cmdpowerlist", 90);
Cmd_SetPower("tell", 60);
Cmd_SetPower("say", 60);
Cmd_SetPower("screentell", 70);
Cmd_SetPower("screensay", 70);
Cmd_SetPower("dumpbanlist", 30);
Cmd_SetPower("setcmdminpower", 95);
Cmd_SetPower("setadmin", 95);
Cmd_SetPower("unsetadmin", 95);
Cbuf_AddText(0, "addCommand gametype \"set g_gametype $arg; map_restart\"");
Cmd_SetPower("gametype", 60);
cmdInvoker.currentCmdPower = 100; //Set the default to 100 to prevent any blocking on local system. If a command gets remotely executed it will be set temporarely to the expected power
//Now read the rest from file - Changed this will happen by executing nvconfig.cfg (nonvolatile config)
cmdSystemInitialized = qtrue;
}
void SV_RemoteCmdClearAdminList()
{
adminPower_t *admin, **this;
for ( this = &adminpower, admin = *this; admin ; admin = *this ){
*this = admin->next;
Z_Free(admin);
}
}
/*
============
SV_RemoteCmdGetClPower
============
*/
int SV_RemoteCmdGetClPower(client_t* cl){
adminPower_t *admin;
int uid;
char* guid;
guid = &cl->pbguid[24];
uid = cl->uid;
if(SV_UseUids()){
if(uid < 1) return 1;
for(admin = adminpower; admin ; admin = admin->next){
if(admin->uid == uid){
return admin->power;
}
}
}else{
if(cl->authentication != 1) return 1;
for(admin = adminpower; admin ; admin = admin->next){
if(!Q_stricmp(admin->guid, guid)){
return admin->power;
}
}
}
return 1;
}
/*
============
SV_RemoteCmdGetClPowerByUID
============
*/
int SV_RemoteCmdGetClPowerByUID(int uid){
adminPower_t *admin;
if(uid < 1) return 0;
for(admin = adminpower; admin ; admin = admin->next){
if(admin->uid == uid){
return admin->power;
}
}
return 1;
}
/*
=================
SV_ReliableSendRedirect
Sends redirected text of console to client as reliable commands
This is used for RemoteCommand responses while client is connected to server
=================
*/
void SV_ReliableSendRedirect(char *sendbuf, qboolean lastcommand){
int lastlinebreak;
int remaining = strlen(sendbuf);
int maxlength;
int i;
char outputbuf[244];
for(; remaining > 0;){ //We have to split the string into smaller packages of max 240 bytes
//This function tries to ensure that every package ends on the last possible linebreak for better formating
maxlength = remaining;
if(maxlength > 240) maxlength = 240;
for(lastlinebreak=0 ,i=0; i < maxlength; i++){
outputbuf[i] = sendbuf[i];
if(outputbuf[i] == '"') outputbuf[i] = 0x27; //replace all " with ' because no " are accepted
if(outputbuf[i] == 0x0a) lastlinebreak = i; //search for the last linebreak
}
if(lastlinebreak > 0){
i = lastlinebreak; //found a linebreak and send everything until that position
remaining -= i-1;
sendbuf += i+1;
outputbuf[i+1] = 0x00;
} else { //Not a linebreak found send full 240 chars
remaining -= i;
sendbuf += i;
outputbuf[i] = 0x00;
}
SV_SendServerCommand(redirectClient, "e \"%s\"", outputbuf);
}
}
void SV_ExecuteRemoteCmd(int clientnum, const char *msg){
char sv_outputbuf[SV_OUTPUTBUF_LENGTH];
char cmd[30];
char buffer[256];
char oldinvokerguid[9];
int i = 0;
int j = 0;
int powercmd;
int power;
client_t *cl;
if(!cmdSystemInitialized){
SV_SendServerCommand(redirectClient, "e \"Error: Remote control system is not initialized\n\"");
Com_Printf("Error: Remote control system is not initialized\n");
return;
}
if(clientnum < 0 || clientnum > 63) return;
cl = &svs.clients[clientnum];
redirectClient = cl;
while ( msg[i] != ' ' && msg[i] != '\0' && msg[i] != '\n' && i < 32 ){
i++;
}
if(i > 29 || i < 3) return;
Q_strncpyz(cmd,msg,i+1);
//Prevent buffer overflow as well as prevent the execution of priveleged commands by using seperator characters
Q_strncpyz(buffer,msg,256);
Q_strchrrepl(buffer,';','\0');
Q_strchrrepl(buffer,'\n','\0');
Q_strchrrepl(buffer,'\r','\0');
// start redirecting all print outputs to the packet
power = SV_RemoteCmdGetClPower(cl);
powercmd = Cmd_GetPower(cmd);
if(powercmd == -1){
SV_SendServerCommand(redirectClient, "e \"^5Command^2: %s\n^3Command execution failed - Invalid command invoked - Type ^2$cmdlist ^3to get a list of all available commands\"", buffer);
return;
}
if(powercmd > power){
SV_SendServerCommand(redirectClient, "e \"^5Command^2: %s\n^3Command execution failed - Insufficient power to execute this command.\n^3You need at least ^6%i ^3powerpoints to invoke this command.\n^3Type ^2$cmdlist ^3to get a list of all available commands\"",
buffer, powercmd);
return;
}
if(SV_UseUids())
Com_Printf( "Command execution: %s Invoked by: %s InvokerUID: %i Power: %i\n", buffer, cl->name, cl->uid, power);
else
Com_Printf( "Command execution: %s Invoked by: %s InvokerGUID: %s Power: %i\n", buffer, cl->name, cl->pbguid, power);
Com_BeginRedirect(sv_outputbuf, SV_OUTPUTBUF_LENGTH, SV_ReliableSendRedirect);
i = cmdInvoker.currentCmdPower;
cmdInvoker.currentCmdPower = power;
cmdInvoker.authserver = qfalse;
if(SV_UseUids()){
j = cmdInvoker.currentCmdInvoker;
cmdInvoker.currentCmdInvoker = cl->uid;
}else{
Q_strncpyz(oldinvokerguid, cmdInvoker.currentCmdInvokerGuid, sizeof(oldinvokerguid));
Q_strncpyz(cmdInvoker.currentCmdInvokerGuid, &cl->pbguid[24], sizeof(cmdInvoker.currentCmdInvokerGuid));
}
cmdInvoker.clientnum = clientnum;
Cmd_ExecuteSingleCommand( 0, 0, buffer );
if(!Q_stricmpn(buffer, "pb_sv_", 6)) PbServerForceProcess();
SV_SendServerCommand(redirectClient, "e \"^5Command^2: %s\"", buffer);
cmdInvoker.currentCmdPower = i;
if(SV_UseUids()){
cmdInvoker.currentCmdInvoker = j;
}else{
//Q_strncpyz(cmdInvoker.currentCmdInvokerGuid, oldinvokerguid, sizeof(cmdInvoker.currentCmdInvokerGuid));
Q_strncpyz(cmdInvoker.currentCmdInvokerGuid, "N/A", sizeof(cmdInvoker.currentCmdInvokerGuid));
}
cmdInvoker.clientnum = -1;
Com_EndRedirect();
}
void SV_ExecuteBroadcastedCmd(int uid, const char *msg){
int i = 0;
int j = 0;
int powercmd;
int power;
while ( msg[i] != ' ' && msg[i] != '\0' && msg[i] != '\n' && i < 32 ){
i++;
}
char cmd[30];
char buffer[256];
if(i > 29 || i < 3) return;
Q_strncpyz(cmd,msg,i+1);
//Prevent buffer overflow as well as prevent the execution of priveleged commands by using seperator characters
Q_strncpyz(buffer,msg,256);
Q_strchrrepl(buffer,';','\0');
Q_strchrrepl(buffer,'\n','\0');
Q_strchrrepl(buffer,'\r','\0');
if(!uid){
power = 100;
}else{
power = SV_RemoteCmdGetClPowerByUID(uid);
}
powercmd = Cmd_GetPower(cmd);
if(powercmd == -1){
return;
}
if(powercmd > power){
Com_Printf( "Broadcasted command execution: %s InvokerUID: %i Power: %i Insufficient Power\n", buffer, uid, power);
return;
}
Com_Printf( "Broadcasted command execution: %s Invoked by: BroadcastMessage InvokerUID: %i Power: %i\n", buffer, uid, power);
i = cmdInvoker.currentCmdPower;
j = cmdInvoker.currentCmdInvoker;
cmdInvoker.currentCmdPower = power;
cmdInvoker.currentCmdInvoker = uid;
cmdInvoker.clientnum = -1;
cmdInvoker.authserver = qtrue;
Cmd_ExecuteSingleCommand( 0, 0, buffer );
cmdInvoker.authserver = qfalse;
cmdInvoker.currentCmdPower = i;
cmdInvoker.currentCmdInvoker = j;
cmdInvoker.clientnum = -1;
}
qboolean SV_RemoteCmdAddAdmin(int uid, char* guid, int power)
{
adminPower_t *admin;
if((uid < 1 && strlen(guid) != 8)){
Com_Printf("Error: Invalid uid / guid\n");
return qfalse;
}
if(power < 1){
Com_Printf("Error: Invalid powerlevel(%i). Powerlevel can not be less than 1.\n");
return qfalse;
}
admin = Z_Malloc(sizeof(adminPower_t));
if(admin)
{
admin->uid = uid;
admin->power = power;
admin->next = adminpower;
adminpower = admin;
Q_strncpyz(admin->guid, guid, sizeof(admin->guid));
return qtrue;
}else{
return qfalse;
}
}
qboolean SV_RemoteCmdInfoAddAdmin(const char* infostring)
{
char guid[16];
int uid;
int power;
uid = atoi(Info_ValueForKey(infostring, "uid"));
Q_strncpyz(guid, Info_ValueForKey(infostring, "guid"), sizeof(guid));
power = atoi(Info_ValueForKey(infostring, "power"));
return SV_RemoteCmdAddAdmin(uid, guid, power);
}
void SV_RemoteCmdWriteAdminConfig(char* buffer, int size)
{
char infostring[1024];
adminPower_t *admin;
Q_strcat(buffer, size, "\n//Admins | Users power settings\n");
for ( admin = adminpower ; admin ; admin = admin->next )
{
*infostring = 0;
Info_SetValueForKey(infostring, "type", "admin");
if(admin->uid != 0)
Info_SetValueForKey(infostring, "uid", va("%i",admin->uid));
else
Info_SetValueForKey(infostring, "guid", admin->guid);
Info_SetValueForKey(infostring, "power", va("%i",admin->power));
Q_strcat(buffer, size, infostring);
Q_strcat(buffer, size, "\\\n");
}
}
void QDECL SV_PrintAdministrativeLog( const char *fmt, ... ) {
va_list argptr;
char msg[MAXPRINTMSG];
char inputmsg[MAXPRINTMSG];
struct tm *newtime;
char* ltime;
time_t realtime;
va_start (argptr,fmt);
Q_vsnprintf (inputmsg, sizeof(inputmsg), fmt, argptr);
va_end (argptr);
Com_UpdateRealtime();
realtime = Com_GetRealtime();
newtime = localtime( &realtime );
ltime = asctime( newtime );
ltime[strlen(ltime)-1] = 0;
if(SV_UseUids())
Com_sprintf(msg, sizeof(msg), "%s - Admin %i with %i power %s\n", ltime, cmdInvoker.currentCmdInvoker, cmdInvoker.currentCmdPower, inputmsg);
else
Com_sprintf(msg, sizeof(msg), "%s - Admin %s with %i power %s\n", ltime, cmdInvoker.currentCmdInvokerGuid, cmdInvoker.currentCmdPower, inputmsg);
Com_PrintAdministrativeLog( msg );
}
/*
============
Cmd_RemoteSetAdmin_f
============
*/
void SV_RemoteCmdSetAdmin(int uid, char* guid, int power)
{
adminPower_t *admin;
adminPower_t *this;
if(SV_UseUids()){
if(uid < 1){
Com_Printf("No such player\n");
return;
}
NV_ProcessBegin();
for(admin = adminpower ; admin ; admin = admin->next){
if(admin->uid == uid){
if(admin->power != power){
admin->power = power;
Com_Printf( "Admin power changed for: uid: %i to level: %i\n", uid, power);
SV_PrintAdministrativeLog( "changed power of admin with uid: %i to new power: %i", uid, power);
}
NV_ProcessEnd();
return;
}
}
this = Z_Malloc(sizeof(adminPower_t));
if(this){
this->uid = uid;
this->power = power;
this->next = adminpower;
adminpower = this;
Com_Printf( "Admin added: uid: %i level: %i\n", uid, power);
SV_PrintAdministrativeLog( "added a new admin with uid: %i and power: %i", uid, power);
}
}else{
if(guid && strlen(guid) == 32)
{
guid += 24;
}
if(!guid || strlen(guid) != 8)
{
Com_Printf("Error: No such player\n");
return;
}
NV_ProcessBegin();
for(admin = adminpower ; admin ; admin = admin->next)
{
if(!Q_stricmp(admin->guid, guid)){
if(admin->power != power){
admin->power = power;
Com_Printf( "Admin power changed for: guid: %s to level: %i\n", guid, power);
SV_PrintAdministrativeLog( "changed power of admin with guid: %s to new power: %i", guid, power);
}
NV_ProcessEnd();
return;
}
}
this = Z_Malloc(sizeof(adminPower_t));
if(this)
{
Q_strncpyz(this->guid, guid, sizeof(this->guid));
this->power = power;
this->next = adminpower;
adminpower = this;
Com_Printf( "Admin added: guid: %s level: %i\n", guid, power);
SV_PrintAdministrativeLog( "added a new admin with guid: %s and power: %i", guid, power);
}
}
NV_ProcessEnd();
}
/*
============
Cmd_RemoteUnsetAdmin_f
============
*/
void SV_RemoteCmdUnsetAdmin(int uid, char* guid)
{
adminPower_t *admin, **this;
if(SV_UseUids()){
if(uid < 1){
Com_Printf("No such player\n");
return;
}
NV_ProcessBegin();
for(this = &adminpower, admin = *this; admin ; admin = *this)
{
if(admin->uid == uid){
*this = admin->next;
Z_Free(admin);
NV_ProcessEnd();
Com_Printf( "User removed: uid: %i\n", uid);
SV_PrintAdministrativeLog( "removed admin with uid: %i", uid);
return;
}
this = &admin->next;
}
}else{
if(guid && strlen(guid) == 32)
{
guid += 24;
}
if(!guid || strlen(guid) != 8)
{
Com_Printf("Error: No such player\n");
return;
}
NV_ProcessBegin();
for(this = &adminpower, admin = *this; admin ; admin = *this)
{
if(!Q_stricmp(admin->guid, guid)){
*this = admin->next;
Z_Free(admin);
NV_ProcessEnd();
Com_Printf( "User removed: guid: %s\n", guid);
SV_PrintAdministrativeLog( "removed admin with guid: %s", guid);
return;
}
this = &admin->next;
}
}
Com_Printf( "Error: No such user in database\n");
NV_ProcessEnd();
}
/*
============
Cmd_RemoteSetPermission
Changes minimum-PowerLevel of a command
============
*/
void SV_RemoteCmdSetPermission(char* command, int power)
{
NV_ProcessBegin();
if(Cmd_SetPower(command, power))
{
SV_PrintAdministrativeLog("changed required power of cmd: %s to new power: %i", command, power);
Com_Printf("changed required power of cmd: %s to new power: %i\n", command, power);
}else{
Com_Printf("Failed to change power of cmd: %s Maybe this is not a valid command.\n", command);
}
NV_ProcessEnd();
}
/*
============
Cmd_RemoteListAdmin_f
============
*/
void SV_RemoteCmdListAdmins()
{
adminPower_t *admin;
int i;
for(i = 0, admin = adminpower ; admin ; i++, admin = admin->next){
Com_Printf( "Admin : uid: %i to: level: %i\n", admin->uid, admin->power);
}
Com_Printf( "%i registered Admins\n", i);
}