Debian Codegraph @

code/server/sv_ccmds.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #include "server.h"
24
25 /*
26 ===============================================================================
27
28 OPERATOR CONSOLE ONLY COMMANDS
29
30 These commands can only be entered from stdin or by a remote operator datagram
31 ===============================================================================
32 */
33
34
35 /*
36 ==================
37 SV_GetPlayerByHandle
38
39 Returns the player with player id or name from Cmd_Argv(1)
40 ==================
41 */
42 static client_t *SV_GetPlayerByHandle( void ) {
43 client_t *cl;
44 int i;
45 char *s;
46 char cleanName[64];
47
48 // make sure server is running
49 if ( !com_sv_running->integer ) {
50 return NULL;
51 }
52
53 if ( Cmd_Argc() < 2 ) {
54 Com_Printf( "No player specified.\n" );
55 return NULL;
56 }
57
58 s = Cmd_Argv(1);
59
60 // Check whether this is a numeric player handle
61 for(i = 0; s[i] >= '0' && s[i] <= '9'; i++);
62
63 if(!s[i])
64 {
65 int plid = atoi(s);
66
67 // Check for numeric playerid match
68 if(plid >= 0 && plid < sv_maxclients->integer)
69 {
70 cl = &svs.clients[plid];
71
72 if(cl->state)
73 return cl;
74 }
75 }
76
77 // check for a name match
78 for ( i=0, cl=svs.clients ; i < sv_maxclients->integer ; i++,cl++ ) {
79 if ( !cl->state ) {
80 continue;
81 }
82 if ( !Q_stricmp( cl->name, s ) ) {
83 return cl;
84 }
85
86 Q_strncpyz( cleanName, cl->name, sizeof(cleanName) );
87 Q_CleanStr( cleanName );
88 if ( !Q_stricmp( cleanName, s ) ) {
89 return cl;
90 }
91 }
92
93 Com_Printf( "Player %s is not on the server\n", s );
94
95 return NULL;
96 }
97
98 /*
99 ==================
100 SV_GetPlayerByNum
101
102 Returns the player with idnum from Cmd_Argv(1)
103 ==================
104 */
105 static client_t *SV_GetPlayerByNum( void ) {
106 client_t *cl;
107 int i;
108 int idnum;
109 char *s;
110
111 // make sure server is running
112 if ( !com_sv_running->integer ) {
113 return NULL;
114 }
115
116 if ( Cmd_Argc() < 2 ) {
117 Com_Printf( "No player specified.\n" );
118 return NULL;
119 }
120
121 s = Cmd_Argv(1);
122
123 for (i = 0; s[i]; i++) {
124 if (s[i] < '0' || s[i] > '9') {
125 Com_Printf( "Bad slot number: %s\n", s);
126 return NULL;
127 }
128 }
129 idnum = atoi( s );
130 if ( idnum < 0 || idnum >= sv_maxclients->integer ) {
131 Com_Printf( "Bad client slot: %i\n", idnum );
132 return NULL;
133 }
134
135 cl = &svs.clients[idnum];
136 if ( !cl->state ) {
137 Com_Printf( "Client %i is not active\n", idnum );
138 return NULL;
139 }
140 return cl;
141 }
142
143 //=========================================================
144
145
146 /*
147 ==================
148 SV_Map_f
149
150 Restart the server on a different map
151 ==================
152 */
153 static void SV_Map_f( void ) {
154 char *cmd;
155 char *map;
156 qboolean killBots, cheat;
157 char expanded[MAX_QPATH];
158 char mapname[MAX_QPATH];
159
160 map = Cmd_Argv(1);
161 if ( !map ) {
162 return;
163 }
164
165 // make sure the level exists before trying to change, so that
166 // a typo at the server console won't end the game
167 Com_sprintf (expanded, sizeof(expanded), "maps/%s.bsp", map);
168 if ( FS_ReadFile (expanded, NULL) == -1 ) {
169 Com_Printf ("Can't find map %s\n", expanded);
170 return;
171 }
172
173 // force latched values to get set
174 Cvar_Get ("g_gametype", "0", CVAR_SERVERINFO | CVAR_USERINFO | CVAR_LATCH );
175
176 cmd = Cmd_Argv(0);
177 if( Q_stricmpn( cmd, "sp", 2 ) == 0 ) {
178 Cvar_SetValue( "g_gametype", GT_SINGLE_PLAYER );
179 Cvar_SetValue( "g_doWarmup", 0 );
180 // may not set sv_maxclients directly, always set latched
181 Cvar_SetLatched( "sv_maxclients", "8" );
182 cmd += 2;
183 if (!Q_stricmp( cmd, "devmap" ) ) {
184 cheat = qtrue;
185 } else {
186 cheat = qfalse;
187 }
188 killBots = qtrue;
189 }
190 else {
191 if ( !Q_stricmp( cmd, "devmap" ) ) {
192 cheat = qtrue;
193 killBots = qtrue;
194 } else {
195 cheat = qfalse;
196 killBots = qfalse;
197 }
198 if( sv_gametype->integer == GT_SINGLE_PLAYER ) {
199 Cvar_SetValue( "g_gametype", GT_FFA );
200 }
201 }
202
203 // save the map name here cause on a map restart we reload the q3config.cfg
204 // and thus nuke the arguments of the map command
205 Q_strncpyz(mapname, map, sizeof(mapname));
206
207 // start up the map
208 SV_SpawnServer( mapname, killBots );
209
210 // set the cheat value
211 // if the level was started with "map <levelname>", then
212 // cheats will not be allowed. If started with "devmap <levelname>"
213 // then cheats will be allowed
214 if ( cheat ) {
215 Cvar_Set( "sv_cheats", "1" );
216 } else {
217 Cvar_Set( "sv_cheats", "0" );
218 }
219 }
220
221 /*
222 ================
223 SV_MapRestart_f
224
225 Completely restarts a level, but doesn't send a new gamestate to the clients.
226 This allows fair starts with variable load times.
227 ================
228 */
229 static void SV_MapRestart_f( void ) {
230 int i;
231 client_t *client;
232 char *denied;
233 qboolean isBot;
234 int delay;
235
236 // make sure we aren't restarting twice in the same frame
237 if ( com_frameTime == sv.serverId ) {
238 return;
239 }
240
241 // make sure server is running
242 if ( !com_sv_running->integer ) {
243 Com_Printf( "Server is not running.\n" );
244 return;
245 }
246
247 if ( sv.restartTime ) {
248 return;
249 }
250
251 if (Cmd_Argc() > 1 ) {
252 delay = atoi( Cmd_Argv(1) );
253 }
254 else {
255 delay = 5;
256 }
257 if( delay && !Cvar_VariableValue("g_doWarmup") ) {
258 sv.restartTime = sv.time + delay * 1000;
259 SV_SetConfigstring( CS_WARMUP, va("%i", sv.restartTime) );
260 return;
261 }
262
263 // check for changes in variables that can't just be restarted
264 // check for maxclients change
265 if ( sv_maxclients->modified || sv_gametype->modified ) {
266 char mapname[MAX_QPATH];
267
268 Com_Printf( "variable change -- restarting.\n" );
269 // restart the map the slow way
270 Q_strncpyz( mapname, Cvar_VariableString( "mapname" ), sizeof( mapname ) );
271
272 SV_SpawnServer( mapname, qfalse );
273 return;
274 }
275
276 // toggle the server bit so clients can detect that a
277 // map_restart has happened
278 svs.snapFlagServerBit ^= SNAPFLAG_SERVERCOUNT;
279
280 // generate a new serverid
281 // TTimo - don't update restartedserverId there, otherwise we won't deal correctly with multiple map_restart
282 sv.serverId = com_frameTime;
283 Cvar_Set( "sv_serverid", va("%i", sv.serverId ) );
284
285 // if a map_restart occurs while a client is changing maps, we need
286 // to give them the correct time so that when they finish loading
287 // they don't violate the backwards time check in cl_cgame.c
288 for (i=0 ; i<sv_maxclients->integer ; i++) {
289 if (svs.clients[i].state == CS_PRIMED) {
290 svs.clients[i].oldServerTime = sv.restartTime;
291 }
292 }
293
294 // reset all the vm data in place without changing memory allocation
295 // note that we do NOT set sv.state = SS_LOADING, so configstrings that
296 // had been changed from their default values will generate broadcast updates
297 sv.state = SS_LOADING;
298 sv.restarting = qtrue;
299
300 SV_RestartGameProgs();
301
302 // run a few frames to allow everything to settle
303 for (i = 0; i < 3; i++)
304 {
305 VM_Call (gvm, GAME_RUN_FRAME, sv.time);
306 sv.time += 100;
307 svs.time += 100;
308 }
309
310 sv.state = SS_GAME;
311 sv.restarting = qfalse;
312
313 // connect and begin all the clients
314 for (i=0 ; i<sv_maxclients->integer ; i++) {
315 client = &svs.clients[i];
316
317 // send the new gamestate to all connected clients
318 if ( client->state < CS_CONNECTED) {
319 continue;
320 }
321
322 if ( client->netchan.remoteAddress.type == NA_BOT ) {
323 isBot = qtrue;
324 } else {
325 isBot = qfalse;
326 }
327
328 // add the map_restart command
329 SV_AddServerCommand( client, "map_restart\n" );
330
331 // connect the client again, without the firstTime flag
332 denied = VM_ExplicitArgPtr( gvm, VM_Call( gvm, GAME_CLIENT_CONNECT, i, qfalse, isBot ) );
333 if ( denied ) {
334 // this generally shouldn't happen, because the client
335 // was connected before the level change
336 SV_DropClient( client, denied );
337 Com_Printf( "SV_MapRestart_f(%d): dropped client %i - denied!\n", delay, i );
338 continue;
339 }
340
341 if(client->state == CS_ACTIVE)
342 SV_ClientEnterWorld(client, &client->lastUsercmd);
343 else
344 {
345 // If we don't reset client->lastUsercmd and are restarting during map load,
346 // the client will hang because we'll use the last Usercmd from the previous map,
347 // which is wrong obviously.
348 SV_ClientEnterWorld(client, NULL);
349 }
350 }
351
352 // run another frame to allow things to look at all the players
353 VM_Call (gvm, GAME_RUN_FRAME, sv.time);
354 sv.time += 100;
355 svs.time += 100;
356 }
357
358 //===============================================================
359
360 /*
361 ==================
362 SV_Kick_f
363
364 Kick a user off of the server
365 ==================
366 */
367 static void SV_Kick_f( void ) {
368 client_t *cl;
369 int i;
370
371 // make sure server is running
372 if ( !com_sv_running->integer ) {
373 Com_Printf( "Server is not running.\n" );
374 return;
375 }
376
377 if ( Cmd_Argc() != 2 ) {
378 Com_Printf ("Usage: kick <player name>\nkick all = kick everyone\nkick allbots = kick all bots\n");
379 return;
380 }
381
382 cl = SV_GetPlayerByHandle();
383 if ( !cl ) {
384 if ( !Q_stricmp(Cmd_Argv(1), "all") ) {
385 for ( i=0, cl=svs.clients ; i < sv_maxclients->integer ; i++,cl++ ) {
386 if ( !cl->state ) {
387 continue;
388 }
389 if( cl->netchan.remoteAddress.type == NA_LOOPBACK ) {
390 continue;
391 }
392 SV_DropClient( cl, "was kicked" );
393 cl->lastPacketTime = svs.time; // in case there is a funny zombie
394 }
395 }
396 else if ( !Q_stricmp(Cmd_Argv(1), "allbots") ) {
397 for ( i=0, cl=svs.clients ; i < sv_maxclients->integer ; i++,cl++ ) {
398 if ( !cl->state ) {
399 continue;
400 }
401 if( cl->netchan.remoteAddress.type != NA_BOT ) {
402 continue;
403 }
404 SV_DropClient( cl, "was kicked" );
405 cl->lastPacketTime = svs.time; // in case there is a funny zombie
406 }
407 }
408 return;
409 }
410 if( cl->netchan.remoteAddress.type == NA_LOOPBACK ) {
411 Com_Printf("Cannot kick host player\n");
412 return;
413 }
414
415 SV_DropClient( cl, "was kicked" );
416 cl->lastPacketTime = svs.time; // in case there is a funny zombie
417 }
418
419 /*
420 ==================
421 SV_KickBots_f
422
423 Kick all bots off of the server
424 ==================
425 */
426 static void SV_KickBots_f( void ) {
427 client_t *cl;
428 int i;
429
430 // make sure server is running
431 if( !com_sv_running->integer ) {
432 Com_Printf("Server is not running.\n");
433 return;
434 }
435
436 for( i = 0, cl = svs.clients; i < sv_maxclients->integer; i++, cl++ ) {
437 if( !cl->state ) {
438 continue;
439 }
440
441 if( cl->netchan.remoteAddress.type != NA_BOT ) {
442 continue;
443 }
444
445 SV_DropClient( cl, "was kicked" );
446 cl->lastPacketTime = svs.time; // in case there is a funny zombie
447 }
448 }
449 /*
450 ==================
451 SV_KickAll_f
452
453 Kick all users off of the server
454 ==================
455 */
456 static void SV_KickAll_f( void ) {
457 client_t *cl;
458 int i;
459
460 // make sure server is running
461 if( !com_sv_running->integer ) {
462 Com_Printf( "Server is not running.\n" );
463 return;
464 }
465
466 for( i = 0, cl = svs.clients; i < sv_maxclients->integer; i++, cl++ ) {
467 if( !cl->state ) {
468 continue;
469 }
470
471 if( cl->netchan.remoteAddress.type == NA_LOOPBACK ) {
472 continue;
473 }
474
475 SV_DropClient( cl, "was kicked" );
476 cl->lastPacketTime = svs.time; // in case there is a funny zombie
477 }
478 }
479
480 /*
481 ==================
482 SV_KickNum_f
483
484 Kick a user off of the server
485 ==================
486 */
487 static void SV_KickNum_f( void ) {
488 client_t *cl;
489
490 // make sure server is running
491 if ( !com_sv_running->integer ) {
492 Com_Printf( "Server is not running.\n" );
493 return;
494 }
495
496 if ( Cmd_Argc() != 2 ) {
497 Com_Printf ("Usage: %s <client number>\n", Cmd_Argv(0));
498 return;
499 }
500
501 cl = SV_GetPlayerByNum();
502 if ( !cl ) {
503 return;
504 }
505 if( cl->netchan.remoteAddress.type == NA_LOOPBACK ) {
506 Com_Printf("Cannot kick host player\n");
507 return;
508 }
509
510 SV_DropClient( cl, "was kicked" );
511 cl->lastPacketTime = svs.time; // in case there is a funny zombie
512 }
513
514 #ifndef STANDALONE
515 // these functions require the auth server which of course is not available anymore for stand-alone games.
516
517 /*
518 ==================
519 SV_Ban_f
520
521 Ban a user from being able to play on this server through the auth
522 server
523 ==================
524 */
525 static void SV_Ban_f( void ) {
526 client_t *cl;
527
528 // make sure server is running
529 if ( !com_sv_running->integer ) {
530 Com_Printf( "Server is not running.\n" );
531 return;
532 }
533
534 if ( Cmd_Argc() != 2 ) {
535 Com_Printf ("Usage: banUser <player name>\n");
536 return;
537 }
538
539 cl = SV_GetPlayerByHandle();
540
541 if (!cl) {
542 return;
543 }
544
545 if( cl->netchan.remoteAddress.type == NA_LOOPBACK ) {
546 Com_Printf("Cannot kick host player\n");
547 return;
548 }
549
550 // look up the authorize server's IP
551 if ( !svs.authorizeAddress.ip[0] && svs.authorizeAddress.type != NA_BAD ) {
552 Com_Printf( "Resolving %s\n", AUTHORIZE_SERVER_NAME );
553 if ( !NET_StringToAdr( AUTHORIZE_SERVER_NAME, &svs.authorizeAddress, NA_IP ) ) {
554 Com_Printf( "Couldn't resolve address\n" );
555 return;
556 }
557 svs.authorizeAddress.port = BigShort( PORT_AUTHORIZE );
558 Com_Printf( "%s resolved to %i.%i.%i.%i:%i\n", AUTHORIZE_SERVER_NAME,
559 svs.authorizeAddress.ip[0], svs.authorizeAddress.ip[1],
560 svs.authorizeAddress.ip[2], svs.authorizeAddress.ip[3],
561 BigShort( svs.authorizeAddress.port ) );
562 }
563
564 // otherwise send their ip to the authorize server
565 if ( svs.authorizeAddress.type != NA_BAD ) {
566 NET_OutOfBandPrint( NS_SERVER, svs.authorizeAddress,
567 "banUser %i.%i.%i.%i", cl->netchan.remoteAddress.ip[0], cl->netchan.remoteAddress.ip[1],
568 cl->netchan.remoteAddress.ip[2], cl->netchan.remoteAddress.ip[3] );
569 Com_Printf("%s was banned from coming back\n", cl->name);
570 }
571 }
572
573 /*
574 ==================
575 SV_BanNum_f
576
577 Ban a user from being able to play on this server through the auth
578 server
579 ==================
580 */
581 static void SV_BanNum_f( void ) {
582 client_t *cl;
583
584 // make sure server is running
585 if ( !com_sv_running->integer ) {
586 Com_Printf( "Server is not running.\n" );
587 return;
588 }
589
590 if ( Cmd_Argc() != 2 ) {
591 Com_Printf ("Usage: banClient <client number>\n");
592 return;
593 }
594
595 cl = SV_GetPlayerByNum();
596 if ( !cl ) {
597 return;
598 }
599 if( cl->netchan.remoteAddress.type == NA_LOOPBACK ) {
600 Com_Printf("Cannot kick host player\n");
601 return;
602 }
603
604 // look up the authorize server's IP
605 if ( !svs.authorizeAddress.ip[0] && svs.authorizeAddress.type != NA_BAD ) {
606 Com_Printf( "Resolving %s\n", AUTHORIZE_SERVER_NAME );
607 if ( !NET_StringToAdr( AUTHORIZE_SERVER_NAME, &svs.authorizeAddress, NA_IP ) ) {
608 Com_Printf( "Couldn't resolve address\n" );
609 return;
610 }
611 svs.authorizeAddress.port = BigShort( PORT_AUTHORIZE );
612 Com_Printf( "%s resolved to %i.%i.%i.%i:%i\n", AUTHORIZE_SERVER_NAME,
613 svs.authorizeAddress.ip[0], svs.authorizeAddress.ip[1],
614 svs.authorizeAddress.ip[2], svs.authorizeAddress.ip[3],
615 BigShort( svs.authorizeAddress.port ) );
616 }
617
618 // otherwise send their ip to the authorize server
619 if ( svs.authorizeAddress.type != NA_BAD ) {
620 NET_OutOfBandPrint( NS_SERVER, svs.authorizeAddress,
621 "banUser %i.%i.%i.%i", cl->netchan.remoteAddress.ip[0], cl->netchan.remoteAddress.ip[1],
622 cl->netchan.remoteAddress.ip[2], cl->netchan.remoteAddress.ip[3] );
623 Com_Printf("%s was banned from coming back\n", cl->name);
624 }
625 }
626 #endif
627
628 /*
629 ==================
630 SV_RehashBans_f
631
632 Load saved bans from file.
633 ==================
634 */
635 static void SV_RehashBans_f(void)
636 {
637 int index, filelen;
638 fileHandle_t readfrom;
639 char *textbuf, *curpos, *maskpos, *newlinepos, *endpos;
640 char filepath[MAX_QPATH];
641
642 serverBansCount = 0;
643
644 if(!sv_banFile->string || !*sv_banFile->string)
645 return;
646
647 Com_sprintf(filepath, sizeof(filepath), "%s/%s", FS_GetCurrentGameDir(), sv_banFile->string);
648
649 if((filelen = FS_BaseDir_FOpenFileRead(filepath, &readfrom)) >= 0)
650 {
651 if(filelen < 2)
652 {
653 // Don't bother if file is too short.
654 FS_FCloseFile(readfrom);
655 return;
656 }
657
658 curpos = textbuf = Z_Malloc(filelen);
659
660 filelen = FS_Read(textbuf, filelen, readfrom);
661 FS_FCloseFile(readfrom);
662
663 endpos = textbuf + filelen;
664
665 for(index = 0; index < SERVER_MAXBANS && curpos + 2 < endpos; index++)
666 {
667 // find the end of the address string
668 for(maskpos = curpos + 2; maskpos < endpos && *maskpos != ' '; maskpos++);
669
670 if(maskpos + 1 >= endpos)
671 break;
672
673 *maskpos = '\0';
674 maskpos++;
675
676 // find the end of the subnet specifier
677 for(newlinepos = maskpos; newlinepos < endpos && *newlinepos != '\n'; newlinepos++);
678
679 if(newlinepos >= endpos)
680 break;
681
682 *newlinepos = '\0';
683
684 if(NET_StringToAdr(curpos + 2, &serverBans[index].ip, NA_UNSPEC))
685 {
686 serverBans[index].isexception = (curpos[0] != '0');
687 serverBans[index].subnet = atoi(maskpos);
688
689 if(serverBans[index].ip.type == NA_IP &&
690 (serverBans[index].subnet < 1 || serverBans[index].subnet > 32))
691 {
692 serverBans[index].subnet = 32;
693 }
694 else if(serverBans[index].ip.type == NA_IP6 &&
695 (serverBans[index].subnet < 1 || serverBans[index].subnet > 128))
696 {
697 serverBans[index].subnet = 128;
698 }
699 }
700
701 curpos = newlinepos + 1;
702 }
703
704 serverBansCount = index;
705
706 Z_Free(textbuf);
707 }
708 }
709
710 /*
711 ==================
712 SV_WriteBans
713
714 Save bans to file.
715 ==================
716 */
717 static void SV_WriteBans(void)
718 {
719 int index;
720 fileHandle_t writeto;
721 char filepath[MAX_QPATH];
722
723 if(!sv_banFile->string || !*sv_banFile->string)
724 return;
725
726 Com_sprintf(filepath, sizeof(filepath), "%s/%s", FS_GetCurrentGameDir(), sv_banFile->string);
727
728 if((writeto = FS_BaseDir_FOpenFileWrite_HomeState(filepath)))
729 {
730 char writebuf[128];
731 serverBan_t *curban;
732
733 for(index = 0; index < serverBansCount; index++)
734 {
735 curban = &serverBans[index];
736
737 Com_sprintf(writebuf, sizeof(writebuf), "%d %s %d\n",
738 curban->isexception, NET_AdrToString(curban->ip), curban->subnet);
739 FS_Write(writebuf, strlen(writebuf), writeto);
740 }
741
742 FS_FCloseFile(writeto);
743 }
744 }
745
746 /*
747 ==================
748 SV_DelBanEntryFromList
749
750 Remove a ban or an exception from the list.
751 ==================
752 */
753
754 static qboolean SV_DelBanEntryFromList(int index)
755 {
756 if(index == serverBansCount - 1)
757 serverBansCount--;
758 else if(index < ARRAY_LEN(serverBans) - 1)
759 {
760 memmove(serverBans + index, serverBans + index + 1, (serverBansCount - index - 1) * sizeof(*serverBans));
761 serverBansCount--;
762 }
763 else
764 return qtrue;
765
766 return qfalse;
767 }
768
769 /*
770 ==================
771 SV_ParseCIDRNotation
772
773 Parse a CIDR notation type string and return a netadr_t and suffix by reference
774 ==================
775 */
776
777 static qboolean SV_ParseCIDRNotation(netadr_t *dest, int *mask, char *adrstr)
778 {
779 char *suffix;
780
781 suffix = strchr(adrstr, '/');
782 if(suffix)
783 {
784 *suffix = '\0';
785 suffix++;
786 }
787
788 if(!NET_StringToAdr(adrstr, dest, NA_UNSPEC))
789 return qtrue;
790
791 if(suffix)
792 {
793 *mask = atoi(suffix);
794
795 if(dest->type == NA_IP)
796 {
797 if(*mask < 1 || *mask > 32)
798 *mask = 32;
799 }
800 else
801 {
802 if(*mask < 1 || *mask > 128)
803 *mask = 128;
804 }
805 }
806 else if(dest->type == NA_IP)
807 *mask = 32;
808 else
809 *mask = 128;
810
811 return qfalse;
812 }
813
814 /*
815 ==================
816 SV_AddBanToList
817
818 Ban a user from being able to play on this server based on his ip address.
819 ==================
820 */
821
822 static void SV_AddBanToList(qboolean isexception)
823 {
824 char *banstring;
825 char addy2[NET_ADDRSTRMAXLEN];
826 netadr_t ip;
827 int index, argc, mask;
828 serverBan_t *curban;
829
830 // make sure server is running
831 if ( !com_sv_running->integer ) {
832 Com_Printf( "Server is not running.\n" );
833 return;
834 }
835
836 argc = Cmd_Argc();
837
838 if(argc < 2 || argc > 3)
839 {
840 Com_Printf ("Usage: %s (ip[/subnet] | clientnum [subnet])\n", Cmd_Argv(0));
841 return;
842 }
843
844 if(serverBansCount >= ARRAY_LEN(serverBans))
845 {
846 Com_Printf ("Error: Maximum number of bans/exceptions exceeded.\n");
847 return;
848 }
849
850 banstring = Cmd_Argv(1);
851
852 if(strchr(banstring, '.') || strchr(banstring, ':'))
853 {
854 // This is an ip address, not a client num.
855
856 if(SV_ParseCIDRNotation(&ip, &mask, banstring))
857 {
858 Com_Printf("Error: Invalid address %s\n", banstring);
859 return;
860 }
861 }
862 else
863 {
864 client_t *cl;
865
866 // client num.
867
868 cl = SV_GetPlayerByNum();
869
870 if(!cl)
871 {
872 Com_Printf("Error: Playernum %s does not exist.\n", Cmd_Argv(1));
873 return;
874 }
875
876 ip = cl->netchan.remoteAddress;
877
878 if(argc == 3)
879 {
880 mask = atoi(Cmd_Argv(2));
881
882 if(ip.type == NA_IP)
883 {
884 if(mask < 1 || mask > 32)
885 mask = 32;
886 }
887 else
888 {
889 if(mask < 1 || mask > 128)
890 mask = 128;
891 }
892 }
893 else
894 mask = (ip.type == NA_IP6) ? 128 : 32;
895 }
896
897 if(ip.type != NA_IP && ip.type != NA_IP6)
898 {
899 Com_Printf("Error: Can ban players connected via the internet only.\n");
900 return;
901 }
902
903 // first check whether a conflicting ban exists that would supersede the new one.
904 for(index = 0; index < serverBansCount; index++)
905 {
906 curban = &serverBans[index];
907
908 if(curban->subnet <= mask)
909 {
910 if((curban->isexception || !isexception) && NET_CompareBaseAdrMask(curban->ip, ip, curban->subnet))
911 {
912 Q_strncpyz(addy2, NET_AdrToString(ip), sizeof(addy2));
913
914 Com_Printf("Error: %s %s/%d supersedes %s %s/%d\n", curban->isexception ? "Exception" : "Ban",
915 NET_AdrToString(curban->ip), curban->subnet,
916 isexception ? "exception" : "ban", addy2, mask);
917 return;
918 }
919 }
920 if(curban->subnet >= mask)
921 {
922 if(!curban->isexception && isexception && NET_CompareBaseAdrMask(curban->ip, ip, mask))
923 {
924 Q_strncpyz(addy2, NET_AdrToString(curban->ip), sizeof(addy2));
925
926 Com_Printf("Error: %s %s/%d supersedes already existing %s %s/%d\n", isexception ? "Exception" : "Ban",
927 NET_AdrToString(ip), mask,
928 curban->isexception ? "exception" : "ban", addy2, curban->subnet);
929 return;
930 }
931 }
932 }
933
934 // now delete bans that are superseded by the new one
935 index = 0;
936 while(index < serverBansCount)
937 {
938 curban = &serverBans[index];
939
940 if(curban->subnet > mask && (!curban->isexception || isexception) && NET_CompareBaseAdrMask(curban->ip, ip, mask))
941 SV_DelBanEntryFromList(index);
942 else
943 index++;
944 }
945
946 serverBans[serverBansCount].ip = ip;
947 serverBans[serverBansCount].subnet = mask;
948 serverBans[serverBansCount].isexception = isexception;
949
950 serverBansCount++;
951
952 SV_WriteBans();
953
954 Com_Printf("Added %s: %s/%d\n", isexception ? "ban exception" : "ban",
955 NET_AdrToString(ip), mask);
956 }
957
958 /*
959 ==================
960 SV_DelBanFromList
961
962 Remove a ban or an exception from the list.
963 ==================
964 */
965
966 static void SV_DelBanFromList(qboolean isexception)
967 {
968 int index, count = 0, todel, mask;
969 netadr_t ip;
970 char *banstring;
971
972 // make sure server is running
973 if ( !com_sv_running->integer ) {
974 Com_Printf( "Server is not running.\n" );
975 return;
976 }
977
978 if(Cmd_Argc() != 2)
979 {
980 Com_Printf ("Usage: %s (ip[/subnet] | num)\n", Cmd_Argv(0));
981 return;
982 }
983
984 banstring = Cmd_Argv(1);
985
986 if(strchr(banstring, '.') || strchr(banstring, ':'))
987 {
988 serverBan_t *curban;
989
990 if(SV_ParseCIDRNotation(&ip, &mask, banstring))
991 {
992 Com_Printf("Error: Invalid address %s\n", banstring);
993 return;
994 }
995
996 index = 0;
997
998 while(index < serverBansCount)
999 {
1000 curban = &serverBans[index];
1001
1002 if(curban->isexception == isexception &&
1003 curban->subnet >= mask &&
1004 NET_CompareBaseAdrMask(curban->ip, ip, mask))
1005 {
1006 Com_Printf("Deleting %s %s/%d\n",
1007 isexception ? "exception" : "ban",
1008 NET_AdrToString(curban->ip), curban->subnet);
1009
1010 SV_DelBanEntryFromList(index);
1011 }
1012 else
1013 index++;
1014 }
1015 }
1016 else
1017 {
1018 todel = atoi(Cmd_Argv(1));
1019
1020 if(todel < 1 || todel > serverBansCount)
1021 {
1022 Com_Printf("Error: Invalid ban number given\n");
1023 return;
1024 }
1025
1026 for(index = 0; index < serverBansCount; index++)
1027 {
1028 if(serverBans[index].isexception == isexception)
1029 {
1030 count++;
1031
1032 if(count == todel)
1033 {
1034 Com_Printf("Deleting %s %s/%d\n",
1035 isexception ? "exception" : "ban",
1036 NET_AdrToString(serverBans[index].ip), serverBans[index].subnet);
1037
1038 SV_DelBanEntryFromList(index);
1039
1040 break;
1041 }
1042 }
1043 }
1044 }
1045
1046 SV_WriteBans();
1047 }
1048
1049
1050 /*
1051 ==================
1052 SV_ListBans_f
1053
1054 List all bans and exceptions on console
1055 ==================
1056 */
1057
1058 static void SV_ListBans_f(void)
1059 {
1060 int index, count;
1061 serverBan_t *ban;
1062
1063 // make sure server is running
1064 if ( !com_sv_running->integer ) {
1065 Com_Printf( "Server is not running.\n" );
1066 return;
1067 }
1068
1069 // List all bans
1070 for(index = count = 0; index < serverBansCount; index++)
1071 {
1072 ban = &serverBans[index];
1073 if(!ban->isexception)
1074 {
1075 count++;
1076
1077 Com_Printf("Ban #%d: %s/%d\n", count,
1078 NET_AdrToString(ban->ip), ban->subnet);
1079 }
1080 }
1081 // List all exceptions
1082 for(index = count = 0; index < serverBansCount; index++)
1083 {
1084 ban = &serverBans[index];
1085 if(ban->isexception)
1086 {
1087 count++;
1088
1089 Com_Printf("Except #%d: %s/%d\n", count,
1090 NET_AdrToString(ban->ip), ban->subnet);
1091 }
1092 }
1093 }
1094
1095 /*
1096 ==================
1097 SV_FlushBans_f
1098
1099 Delete all bans and exceptions.
1100 ==================
1101 */
1102
1103 static void SV_FlushBans_f(void)
1104 {
1105 // make sure server is running
1106 if ( !com_sv_running->integer ) {
1107 Com_Printf( "Server is not running.\n" );
1108 return;
1109 }
1110
1111 serverBansCount = 0;
1112
1113 // empty the ban file.
1114 SV_WriteBans();
1115
1116 Com_Printf("All bans and exceptions have been deleted.\n");
1117 }
1118
1119 static void SV_BanAddr_f(void)
1120 {
1121 SV_AddBanToList(qfalse);
1122 }
1123
1124 static void SV_ExceptAddr_f(void)
1125 {
1126 SV_AddBanToList(qtrue);
1127 }
1128
1129 static void SV_BanDel_f(void)
1130 {
1131 SV_DelBanFromList(qfalse);
1132 }
1133
1134 static void SV_ExceptDel_f(void)
1135 {
1136 SV_DelBanFromList(qtrue);
1137 }
1138
1139 /*
1140 ** SV_Strlen -- skips color escape codes
1141 */
1142 static int SV_Strlen( const char *str ) {
1143 const char *s = str;
1144 int count = 0;
1145
1146 while ( *s ) {
1147 if ( Q_IsColorString( s ) ) {
1148 s += 2;
1149 } else {
1150 count++;
1151 s++;
1152 }
1153 }
1154
1155 return count;
1156 }
1157
1158 /*
1159 ================
1160 SV_Status_f
1161 ================
1162 */
1163 static void SV_Status_f( void ) {
1164 int i, j, l;
1165 client_t *cl;
1166 playerState_t *ps;
1167 const char *s;
1168 int ping;
1169
1170 // make sure server is running
1171 if ( !com_sv_running->integer ) {
1172 Com_Printf( "Server is not running.\n" );
1173 return;
1174 }
1175
1176 Com_Printf ("map: %s\n", sv_mapname->string );
1177
1178 Com_Printf ("cl score ping name address rate \n");
1179 Com_Printf ("-- ----- ---- --------------- --------------------------------------- -----\n");
1180 for (i=0,cl=svs.clients ; i < sv_maxclients->integer ; i++,cl++)
1181 {
1182 if (!cl->state)
1183 continue;
1184 Com_Printf ("%2i ", i);
1185 ps = SV_GameClientNum( i );
1186 Com_Printf ("%5i ", ps->persistant[PERS_SCORE]);
1187
1188 if (cl->state == CS_CONNECTED)
1189 Com_Printf ("CON ");
1190 else if (cl->state == CS_ZOMBIE)
1191 Com_Printf ("ZMB ");
1192 else
1193 {
1194 ping = cl->ping < 9999 ? cl->ping : 9999;
1195 Com_Printf ("%4i ", ping);
1196 }
1197
1198 Com_Printf ("%s", cl->name);
1199
1200 l = 16 - SV_Strlen(cl->name);
1201 j = 0;
1202
1203 do
1204 {
1205 Com_Printf (" ");
1206 j++;
1207 } while(j < l);
1208
1209
1210 // TTimo adding a ^7 to reset the color
1211 s = NET_AdrToString( cl->netchan.remoteAddress );
1212 Com_Printf ("^7%s", s);
1213 l = 39 - strlen(s);
1214 j = 0;
1215
1216 do
1217 {
1218 Com_Printf(" ");
1219 j++;
1220 } while(j < l);
1221
1222 Com_Printf (" %5i", cl->rate);
1223
1224 Com_Printf ("\n");
1225 }
1226 Com_Printf ("\n");
1227 }
1228
1229 /*
1230 ==================
1231 SV_ConSay_f
1232 ==================
1233 */
1234 static void SV_ConSay_f(void) {
1235 char *p;
1236 char text[1024];
1237
1238 // make sure server is running
1239 if ( !com_sv_running->integer ) {
1240 Com_Printf( "Server is not running.\n" );
1241 return;
1242 }
1243
1244 if ( Cmd_Argc () < 2 ) {
1245 return;
1246 }
1247
1248 strcpy (text, "console: ");
1249 p = Cmd_Args();
1250
1251 if ( *p == '"' ) {
1252 p++;
1253 p[strlen(p)-1] = 0;
1254 }
1255
1256 strcat(text, p);
1257
1258 Com_Printf("%s\n", text);
1259 SV_SendServerCommand(NULL, "chat \"%s\"", text);
1260 }
1261
1262 /*
1263 ==================
1264 SV_ConTell_f
1265 ==================
1266 */
1267 static void SV_ConTell_f(void) {
1268 char *p;
1269 char text[1024];
1270 client_t *cl;
1271
1272 // make sure server is running
1273 if ( !com_sv_running->integer ) {
1274 Com_Printf( "Server is not running.\n" );
1275 return;
1276 }
1277
1278 if ( Cmd_Argc() < 3 ) {
1279 Com_Printf ("Usage: tell <client number> <text>\n");
1280 return;
1281 }
1282
1283 cl = SV_GetPlayerByNum();
1284 if ( !cl ) {
1285 return;
1286 }
1287
1288 strcpy (text, "console_tell: ");
1289 p = Cmd_ArgsFrom(2);
1290
1291 if ( *p == '"' ) {
1292 p++;
1293 p[strlen(p)-1] = 0;
1294 }
1295
1296 strcat(text, p);
1297
1298 Com_Printf("%s\n", text);
1299 SV_SendServerCommand(cl, "chat \"%s\"", text);
1300 }
1301
1302
1303 /*
1304 ==================
1305 SV_ConSayto_f
1306 ==================
1307 */
1308 static void SV_ConSayto_f(void) {
1309 char *p;
1310 char text[1024];
1311 client_t *cl;
1312 char *rawname;
1313 char name[MAX_NAME_LENGTH];
1314 char cleanName[MAX_NAME_LENGTH];
1315 client_t *saytocl;
1316 int i;
1317
1318 // make sure server is running
1319 if ( !com_sv_running->integer ) {
1320 Com_Printf( "Server is not running.\n" );
1321 return;
1322 }
1323
1324 if ( Cmd_Argc() < 3 ) {
1325 Com_Printf ("Usage: sayto <player name> <text>\n");
1326 return;
1327 }
1328
1329 rawname = Cmd_Argv(1);
1330
1331 //allowing special characters in the console
1332 //with hex strings for player names
1333 Com_FieldStringToPlayerName( name, MAX_NAME_LENGTH, rawname );
1334
1335 saytocl = NULL;
1336 for ( i=0, cl=svs.clients ; i < sv_maxclients->integer ; i++,cl++ ) {
1337 if ( !cl->state ) {
1338 continue;
1339 }
1340 Q_strncpyz( cleanName, cl->name, sizeof(cleanName) );
1341 Q_CleanStr( cleanName );
1342
1343 if ( !Q_stricmp( cleanName, name ) ) {
1344 saytocl = cl;
1345 break;
1346 }
1347 }
1348 if( !saytocl )
1349 {
1350 Com_Printf ("No such player name: %s.\n", name);
1351 return;
1352 }
1353
1354 strcpy (text, "console_sayto: ");
1355 p = Cmd_ArgsFrom(2);
1356
1357 if ( *p == '"' ) {
1358 p++;
1359 p[strlen(p)-1] = 0;
1360 }
1361
1362 strcat(text, p);
1363
1364 Com_Printf("%s\n", text);
1365 SV_SendServerCommand(saytocl, "chat \"%s\"", text);
1366 }
1367
1368
1369 /*
1370 ==================
1371 SV_Heartbeat_f
1372
1373 Also called by SV_DropClient, SV_DirectConnect, and SV_SpawnServer
1374 ==================
1375 */
1376 void SV_Heartbeat_f( void ) {
1377 svs.nextHeartbeatTime = -9999999;
1378 }
1379
1380
1381 /*
1382 ===========
1383 SV_Serverinfo_f
1384
1385 Examine the serverinfo string
1386 ===========
1387 */
1388 static void SV_Serverinfo_f( void ) {
1389 // make sure server is running
1390 if ( !com_sv_running->integer ) {
1391 Com_Printf( "Server is not running.\n" );
1392 return;
1393 }
1394
1395 Com_Printf ("Server info settings:\n");
1396 Info_Print ( Cvar_InfoString( CVAR_SERVERINFO ) );
1397 }
1398
1399
1400 /*
1401 ===========
1402 SV_Systeminfo_f
1403
1404 Examine the systeminfo string
1405 ===========
1406 */
1407 static void SV_Systeminfo_f( void ) {
1408 // make sure server is running
1409 if ( !com_sv_running->integer ) {
1410 Com_Printf( "Server is not running.\n" );
1411 return;
1412 }
1413
1414 Com_Printf ("System info settings:\n");
1415 Info_Print ( Cvar_InfoString_Big( CVAR_SYSTEMINFO ) );
1416 }
1417
1418
1419 /*
1420 ===========
1421 SV_DumpUser_f
1422
1423 Examine all a users info strings
1424 ===========
1425 */
1426 static void SV_DumpUser_f( void ) {
1427 client_t *cl;
1428
1429 // make sure server is running
1430 if ( !com_sv_running->integer ) {
1431 Com_Printf( "Server is not running.\n" );
1432 return;
1433 }
1434
1435 if ( Cmd_Argc() != 2 ) {
1436 Com_Printf ("Usage: dumpuser <userid>\n");
1437 return;
1438 }
1439
1440 cl = SV_GetPlayerByHandle();
1441 if ( !cl ) {
1442 return;
1443 }
1444
1445 Com_Printf( "userinfo\n" );
1446 Com_Printf( "--------\n" );
1447 Info_Print( cl->userinfo );
1448 }
1449
1450
1451 /*
1452 =================
1453 SV_KillServer
1454 =================
1455 */
1456 static void SV_KillServer_f( void ) {
1457 SV_Shutdown( "killserver" );
1458 }
1459
1460 //===========================================================
1461
1462 /*
1463 ==================
1464 SV_CompleteMapName
1465 ==================
1466 */
1467 static void SV_CompleteMapName( char *args, int argNum ) {
1468 if( argNum == 2 ) {
1469 Field_CompleteFilename( "maps", "bsp", NULL, qtrue, qfalse );
1470 }
1471 }
1472
1473 /*
1474 ==================
1475 SV_CompletePlayerName
1476 ==================
1477 */
1478 static void SV_CompletePlayerName( char *args, int argNum ) {
1479 if( argNum == 2 ) {
1480 char names[MAX_CLIENTS][MAX_NAME_LENGTH];
1481 const char *namesPtr[MAX_CLIENTS];
1482 client_t *cl;
1483 int i;
1484 int nameCount;
1485 int clientCount;
1486
1487 nameCount = 0;
1488 clientCount = sv_maxclients->integer;
1489
1490 for ( i=0, cl=svs.clients ; i < clientCount; i++,cl++ ) {
1491 if ( !cl->state ) {
1492 continue;
1493 }
1494 if( i >= MAX_CLIENTS ) {
1495 break;
1496 }
1497 Q_strncpyz( names[nameCount], cl->name, sizeof(names[nameCount]) );
1498 Q_CleanStr( names[nameCount] );
1499
1500 namesPtr[nameCount] = names[nameCount];
1501
1502 nameCount++;
1503 }
1504 qsort( (void*)namesPtr, nameCount, sizeof( namesPtr[0] ), Com_strCompare );
1505
1506 Field_CompletePlayerName( namesPtr, nameCount );
1507 }
1508 }
1509
1510 /*
1511 ==================
1512 SV_AddOperatorCommands
1513 ==================
1514 */
1515 void SV_AddOperatorCommands( void ) {
1516 static qboolean initialized;
1517
1518 if ( initialized ) {
1519 return;
1520 }
1521 initialized = qtrue;
1522
1523 Cmd_AddCommand ("heartbeat", SV_Heartbeat_f);
1524 Cmd_AddCommand ("kick", SV_Kick_f);
1525 #ifndef STANDALONE
1526 if(!com_standalone->integer)
1527 {
1528 Cmd_AddCommand ("banUser", SV_Ban_f);
1529 Cmd_AddCommand ("banClient", SV_BanNum_f);
1530 }
1531 #endif
1532 Cmd_AddCommand ("kickbots", SV_KickBots_f);
1533 Cmd_AddCommand ("kickall", SV_KickAll_f);
1534 Cmd_AddCommand ("kicknum", SV_KickNum_f);
1535 Cmd_AddCommand ("clientkick", SV_KickNum_f); // Legacy command
1536 Cmd_AddCommand ("status", SV_Status_f);
1537 Cmd_AddCommand ("serverinfo", SV_Serverinfo_f);
1538 Cmd_AddCommand ("systeminfo", SV_Systeminfo_f);
1539 Cmd_AddCommand ("dumpuser", SV_DumpUser_f);
1540 Cmd_AddCommand ("map_restart", SV_MapRestart_f);
1541 Cmd_AddCommand ("sectorlist", SV_SectorList_f);
1542 Cmd_AddCommand ("map", SV_Map_f);
1543 Cmd_SetCommandCompletionFunc( "map", SV_CompleteMapName );
1544 #ifndef PRE_RELEASE_DEMO
1545 Cmd_AddCommand ("devmap", SV_Map_f);
1546 Cmd_SetCommandCompletionFunc( "devmap", SV_CompleteMapName );
1547 Cmd_AddCommand ("spmap", SV_Map_f);
1548 Cmd_SetCommandCompletionFunc( "spmap", SV_CompleteMapName );
1549 Cmd_AddCommand ("spdevmap", SV_Map_f);
1550 Cmd_SetCommandCompletionFunc( "spdevmap", SV_CompleteMapName );
1551 #endif
1552 Cmd_AddCommand ("killserver", SV_KillServer_f);
1553 if( com_dedicated->integer ) {
1554 Cmd_AddCommand ("say", SV_ConSay_f);
1555 Cmd_AddCommand ("tell", SV_ConTell_f);
1556 Cmd_AddCommand ("sayto", SV_ConSayto_f);
1557 Cmd_SetCommandCompletionFunc( "sayto", SV_CompletePlayerName );
1558 }
1559
1560 Cmd_AddCommand("rehashbans", SV_RehashBans_f);
1561 Cmd_AddCommand("listbans", SV_ListBans_f);
1562 Cmd_AddCommand("banaddr", SV_BanAddr_f);
1563 Cmd_AddCommand("exceptaddr", SV_ExceptAddr_f);
1564 Cmd_AddCommand("bandel", SV_BanDel_f);
1565 Cmd_AddCommand("exceptdel", SV_ExceptDel_f);
1566 Cmd_AddCommand("flushbans", SV_FlushBans_f);
1567 }
1568
1569 /*
1570 ==================
1571 SV_RemoveOperatorCommands
1572 ==================
1573 */
1574 void SV_RemoveOperatorCommands( void ) {
1575 #if 0
1576 // removing these won't let the server start again
1577 Cmd_RemoveCommand ("heartbeat");
1578 Cmd_RemoveCommand ("kick");
1579 Cmd_RemoveCommand ("kicknum");
1580 Cmd_RemoveCommand ("clientkick");
1581 Cmd_RemoveCommand ("kickall");
1582 Cmd_RemoveCommand ("kickbots");
1583 Cmd_RemoveCommand ("banUser");
1584 Cmd_RemoveCommand ("banClient");
1585 Cmd_RemoveCommand ("status");
1586 Cmd_RemoveCommand ("serverinfo");
1587 Cmd_RemoveCommand ("systeminfo");
1588 Cmd_RemoveCommand ("dumpuser");
1589 Cmd_RemoveCommand ("map_restart");
1590 Cmd_RemoveCommand ("sectorlist");
1591 Cmd_RemoveCommand ("say");
1592 #endif
1593 }
1594