Debian Codegraph @

code/game/g_svcmds.c

raw
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
24 // this file holds commands that can be executed by the server console, but not remote clients
25
26 #include "g_local.h"
27
28
29 /*
30 ==============================================================================
31
32 PACKET FILTERING
33
34
35 You can add or remove addresses from the filter list with:
36
37 addip <ip>
38 removeip <ip>
39
40 The ip address is specified in dot format, and you can use '*' to match any value
41 so you can specify an entire class C network with "addip 192.246.40.*"
42
43 Removeip will only remove an address specified exactly the same way. You cannot addip a subnet, then removeip a single host.
44
45 listip
46 Prints the current list of filters.
47
48 g_filterban <0 or 1>
49
50 If 1 (the default), then ip addresses matching the current list will be prohibited from entering the game. This is the default setting.
51
52 If 0, then only addresses matching the list will be allowed. This lets you easily set up a private game, or a game that only allows players from your local network.
53
54 TTimo NOTE: for persistence, bans are stored in g_banIPs cvar MAX_CVAR_VALUE_STRING
55 The size of the cvar string buffer is limiting the banning to around 20 masks
56 this could be improved by putting some g_banIPs2 g_banIps3 etc. maybe
57 still, you should rely on PB for banning instead
58
59 ==============================================================================
60 */
61
62 typedef struct ipFilter_s
63 {
64 unsigned mask;
65 unsigned compare;
66 } ipFilter_t;
67
68 #define MAX_IPFILTERS 1024
69
70 static ipFilter_t ipFilters[MAX_IPFILTERS];
71 static int numIPFilters;
72
73 /*
74 =================
75 StringToFilter
76 =================
77 */
78 static qboolean StringToFilter (char *s, ipFilter_t *f)
79 {
80 char num[128];
81 int i, j;
82 byte b[4];
83 byte m[4];
84
85 for (i=0 ; i<4 ; i++)
86 {
87 b[i] = 0;
88 m[i] = 0;
89 }
90
91 for (i=0 ; i<4 ; i++)
92 {
93 if (*s < '0' || *s > '9')
94 {
95 if (*s == '*') // 'match any'
96 {
97 // b[i] and m[i] to 0
98 s++;
99 if (!*s)
100 break;
101 s++;
102 continue;
103 }
104 G_Printf( "Bad filter address: %s\n", s );
105 return qfalse;
106 }
107
108 j = 0;
109 while (*s >= '0' && *s <= '9')
110 {
111 num[j++] = *s++;
112 }
113 num[j] = 0;
114 b[i] = atoi(num);
115 m[i] = 255;
116
117 if (!*s)
118 break;
119 s++;
120 }
121
122 f->mask = *(unsigned *)m;
123 f->compare = *(unsigned *)b;
124
125 return qtrue;
126 }
127
128 /*
129 =================
130 UpdateIPBans
131 =================
132 */
133 static void UpdateIPBans (void)
134 {
135 byte b[4];
136 byte m[4];
137 int i,j;
138 char iplist_final[MAX_CVAR_VALUE_STRING];
139 char ip[64];
140
141 *iplist_final = 0;
142 for (i = 0 ; i < numIPFilters ; i++)
143 {
144 if (ipFilters[i].compare == 0xffffffff)
145 continue;
146
147 *(unsigned *)b = ipFilters[i].compare;
148 *(unsigned *)m = ipFilters[i].mask;
149 *ip = 0;
150 for (j = 0 ; j < 4 ; j++)
151 {
152 if (m[j]!=255)
153 Q_strcat(ip, sizeof(ip), "*");
154 else
155 Q_strcat(ip, sizeof(ip), va("%i", b[j]));
156 Q_strcat(ip, sizeof(ip), (j<3) ? "." : " ");
157 }
158 if (strlen(iplist_final)+strlen(ip) < MAX_CVAR_VALUE_STRING)
159 {
160 Q_strcat( iplist_final, sizeof(iplist_final), ip);
161 }
162 else
163 {
164 Com_Printf("g_banIPs overflowed at MAX_CVAR_VALUE_STRING\n");
165 break;
166 }
167 }
168
169 trap_Cvar_Set( "g_banIPs", iplist_final );
170 }
171
172 /*
173 =================
174 G_FilterPacket
175 =================
176 */
177 qboolean G_FilterPacket (char *from)
178 {
179 int i;
180 unsigned in;
181 byte m[4];
182 char *p;
183
184 i = 0;
185 p = from;
186 while (*p && i < 4) {
187 m[i] = 0;
188 while (*p >= '0' && *p <= '9') {
189 m[i] = m[i]*10 + (*p - '0');
190 p++;
191 }
192 if (!*p || *p == ':')
193 break;
194 i++, p++;
195 }
196
197 in = *(unsigned *)m;
198
199 for (i=0 ; i<numIPFilters ; i++)
200 if ( (in & ipFilters[i].mask) == ipFilters[i].compare)
201 return g_filterBan.integer != 0;
202
203 return g_filterBan.integer == 0;
204 }
205
206 /*
207 =================
208 AddIP
209 =================
210 */
211 static void AddIP( char *str )
212 {
213 int i;
214
215 for (i = 0 ; i < numIPFilters ; i++)
216 if (ipFilters[i].compare == 0xffffffff)
217 break; // free spot
218 if (i == numIPFilters)
219 {
220 if (numIPFilters == MAX_IPFILTERS)
221 {
222 G_Printf ("IP filter list is full\n");
223 return;
224 }
225 numIPFilters++;
226 }
227
228 if (!StringToFilter (str, &ipFilters[i]))
229 ipFilters[i].compare = 0xffffffffu;
230
231 UpdateIPBans();
232 }
233
234 /*
235 =================
236 G_ProcessIPBans
237 =================
238 */
239 void G_ProcessIPBans(void)
240 {
241 char *s, *t;
242 char str[MAX_CVAR_VALUE_STRING];
243
244 Q_strncpyz( str, g_banIPs.string, sizeof(str) );
245
246 for (t = s = g_banIPs.string; *t; /* */ ) {
247 s = strchr(s, ' ');
248 if (!s)
249 break;
250 while (*s == ' ')
251 *s++ = 0;
252 if (*t)
253 AddIP( t );
254 t = s;
255 }
256 }
257
258
259 /*
260 =================
261 Svcmd_AddIP_f
262 =================
263 */
264 void Svcmd_AddIP_f (void)
265 {
266 char str[MAX_TOKEN_CHARS];
267
268 if ( trap_Argc() < 2 ) {
269 G_Printf("Usage: addip <ip-mask>\n");
270 return;
271 }
272
273 trap_Argv( 1, str, sizeof( str ) );
274
275 AddIP( str );
276
277 }
278
279 /*
280 =================
281 Svcmd_RemoveIP_f
282 =================
283 */
284 void Svcmd_RemoveIP_f (void)
285 {
286 ipFilter_t f;
287 int i;
288 char str[MAX_TOKEN_CHARS];
289
290 if ( trap_Argc() < 2 ) {
291 G_Printf("Usage: sv removeip <ip-mask>\n");
292 return;
293 }
294
295 trap_Argv( 1, str, sizeof( str ) );
296
297 if (!StringToFilter (str, &f))
298 return;
299
300 for (i=0 ; i<numIPFilters ; i++) {
301 if (ipFilters[i].mask == f.mask &&
302 ipFilters[i].compare == f.compare) {
303 ipFilters[i].compare = 0xffffffffu;
304 G_Printf ("Removed.\n");
305
306 UpdateIPBans();
307 return;
308 }
309 }
310 G_Printf ( "Didn't find %s.\n", str );
311 }
312
313 /*
314 ===================
315 Svcmd_EntityList_f
316 ===================
317 */
318 void Svcmd_EntityList_f (void) {
319 int e;
320 gentity_t *check;
321
322 check = g_entities+1;
323 for (e = 1; e < level.num_entities ; e++, check++) {
324 if ( !check->inuse ) {
325 continue;
326 }
327 G_Printf("%3i:", e);
328 switch ( check->s.eType ) {
329 case ET_GENERAL:
330 G_Printf("ET_GENERAL ");
331 break;
332 case ET_PLAYER:
333 G_Printf("ET_PLAYER ");
334 break;
335 case ET_ITEM:
336 G_Printf("ET_ITEM ");
337 break;
338 case ET_MISSILE:
339 G_Printf("ET_MISSILE ");
340 break;
341 case ET_MOVER:
342 G_Printf("ET_MOVER ");
343 break;
344 case ET_BEAM:
345 G_Printf("ET_BEAM ");
346 break;
347 case ET_PORTAL:
348 G_Printf("ET_PORTAL ");
349 break;
350 case ET_SPEAKER:
351 G_Printf("ET_SPEAKER ");
352 break;
353 case ET_PUSH_TRIGGER:
354 G_Printf("ET_PUSH_TRIGGER ");
355 break;
356 case ET_TELEPORT_TRIGGER:
357 G_Printf("ET_TELEPORT_TRIGGER ");
358 break;
359 case ET_INVISIBLE:
360 G_Printf("ET_INVISIBLE ");
361 break;
362 case ET_GRAPPLE:
363 G_Printf("ET_GRAPPLE ");
364 break;
365 default:
366 G_Printf("%3i ", check->s.eType);
367 break;
368 }
369 if ( check->classname ) {
370 G_Printf("%s", check->classname);
371 }
372 G_Printf("\n");
373 }
374 }
375
376 gclient_t *ClientForString( const char *s ) {
377 gclient_t *cl;
378 int i;
379 int idnum;
380
381 // numeric values are just slot numbers
382 if ( s[0] >= '0' && s[0] <= '9' ) {
383 idnum = atoi( s );
384 if ( idnum < 0 || idnum >= level.maxclients ) {
385 Com_Printf( "Bad client slot: %i\n", idnum );
386 return NULL;
387 }
388
389 cl = &level.clients[idnum];
390 if ( cl->pers.connected == CON_DISCONNECTED ) {
391 G_Printf( "Client %i is not connected\n", idnum );
392 return NULL;
393 }
394 return cl;
395 }
396
397 // check for a name match
398 for ( i=0 ; i < level.maxclients ; i++ ) {
399 cl = &level.clients[i];
400 if ( cl->pers.connected == CON_DISCONNECTED ) {
401 continue;
402 }
403 if ( !Q_stricmp( cl->pers.netname, s ) ) {
404 return cl;
405 }
406 }
407 G_Printf( "User %s is not on the server\n", s );
408
409 return NULL;
410 }
411
412 /*
413 ===================
414 Svcmd_ForceTeam_f
415
416 forceteam <player> <team>
417 ===================
418 */
419 void Svcmd_ForceTeam_f( void ) {
420 gclient_t *cl;
421 char str[MAX_TOKEN_CHARS];
422
423 // find the player
424 trap_Argv( 1, str, sizeof( str ) );
425 cl = ClientForString( str );
426 if ( !cl ) {
427 return;
428 }
429
430 // set the team
431 trap_Argv( 2, str, sizeof( str ) );
432 SetTeam( &g_entities[cl - level.clients], str );
433 }
434
435 void ClientKick_f( void ) {
436 int idnum, i;
437 char str[MAX_TOKEN_CHARS];
438
439 trap_Argv( 1, str, sizeof( str ) );
440
441 for (i = 0; str[i]; i++) {
442 if (str[i] < '0' || str[i] > '9') {
443 G_Printf("not a valid client number: \"%s\"\n",str);
444 return;
445 }
446 }
447
448 idnum = atoi( str );
449
450 //Local client
451 if( !strcmp( level.clients[idnum].pers.ip, "localhost" ) ) {
452 G_Printf("Kick failed - local player\n");
453 return;
454 }
455
456 //Now clientkick has been moved into game, but we still need to find the idnum the server expects....
457 //FIXME: To fix this, we need a relieble way to generate difference between the server's client number and the game's client numbers
458 //FIXME: This should not depend on the engine's clientkick at all
459 trap_DropClient( idnum, "was kicked" );
460 //trap_SendConsoleCommand( EXEC_INSERT, va("clientkick %d\n", level.clients[idnum].ps.clientNum) );
461
462 }
463
464 void EndGame_f ( void ) {
465 ExitLevel();
466 }
467
468 //KK-OAX Moved this Declaration to g_local.h
469 //char *ConcatArgs( int start );
470
471 /*KK-OAX
472 ===============
473 Server Command Table
474 Not Worth Listing Elsewhere
475 ================
476 */
477 struct
478 {
479 char *cmd;
480 qboolean dedicated; //if it has to be entered from a dedicated server or RCON
481 void ( *function )( void );
482 } svcmds[ ] = {
483
484 { "entityList", qfalse, Svcmd_EntityList_f },
485 { "forceTeam", qfalse, Svcmd_ForceTeam_f },
486 { "game_memory", qfalse, Svcmd_GameMem_f },
487 { "addbot", qfalse, Svcmd_AddBot_f },
488 { "botlist", qfalse, Svcmd_BotList_f },
489 { "abort_podium", qfalse, Svcmd_AbortPodium_f },
490 { "addip", qfalse, Svcmd_AddIP_f },
491 { "removeip", qfalse, Svcmd_RemoveIP_f },
492
493 //KK-OAX Uses wrapper in g_svccmds_ext.c
494 { "listip", qfalse, Svcmd_ListIP_f },
495 //KK-OAX New
496 { "status", qfalse, Svcmd_Status_f },
497 { "eject", qfalse, Svcmd_EjectClient_f },
498 { "dumpuser", qfalse, Svcmd_DumpUser_f },
499 // don't handle communication commands unless dedicated
500 { "cp", qtrue, Svcmd_CenterPrint_f },
501 { "say_team", qtrue, Svcmd_TeamMessage_f },
502 { "say", qtrue, Svcmd_MessageWrapper },
503 { "chat", qtrue, Svcmd_Chat_f },
504 /*{ "m", qtrue, Svcmd_MessageWrapper },
505 { "a", qtrue, Svcmd_MessageWrapper },
506 { "bp", qtrue, Svcmd_BannerPrint_f }, */
507 //Shuffle the teams
508 { "shuffle", qfalse, ShuffleTeams },
509 //Kicks a player by number in the game logic rather than the server number
510 { "clientkick_game", qfalse, ClientKick_f },
511 { "endgamenow", qfalse, EndGame_f },
512 };
513
514 /*
515 =================
516 ConsoleCommand
517
518 =================
519 */
520 qboolean ConsoleCommand( void )
521 {
522 char cmd[ MAX_TOKEN_CHARS ];
523 int i;
524
525 trap_Argv( 0, cmd, sizeof( cmd ) );
526
527 for( i = 0; i < sizeof( svcmds ) / sizeof( svcmds[ 0 ] ); i++ )
528 {
529 if( !Q_stricmp( cmd, svcmds[ i ].cmd ) )
530 {
531 if( svcmds[ i ].dedicated && !g_dedicated.integer )
532 return qfalse;
533 svcmds[ i ].function( );
534 return qtrue;
535 }
536 }
537 // KK-OAX Will be enabled when admin is added.
538 // see if this is an admin command
539 if( G_admin_cmd_check( NULL, qfalse ) )
540 return qtrue;
541
542 if( g_dedicated.integer )
543 G_Printf( "unknown command: %s\n", cmd );
544
545 return qfalse;
546 }
547