Debian Codegraph @

code/server/sv_ccmds.c

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