Xonotic
ipban.qh File Reference
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

float Ban_Delete (float i)
 
float Ban_Insert (string ip, float bantime, string reason, float dosync)
 
void Ban_KickBanClient (entity client, float bantime, float masksize, string reason)
 
void Ban_LoadBans ()
 
float Ban_MaybeEnforceBan (entity client)
 
float Ban_MaybeEnforceBanOnce (entity client)
 
void Ban_SaveBans ()
 
void Ban_View ()
 
float BanCommand (string command)
 
void OnlineBanList_URI_Get_Callback (float id, float status, string data)
 

Function Documentation

◆ Ban_Delete()

float Ban_Delete ( float  i)

Definition at line 286 of file ipban.qc.

References ban_count, ban_expire, ban_ip, Ban_SaveBans(), OnlineBanList_SendUnban(), and strunzone().

Referenced by Ban_Insert(), Ban_LoadBans(), and BanCommand_unban().

287 {
288  if(i < 0)
289  return false;
290  if(i >= ban_count)
291  return false;
292  if(ban_expire[i] == 0)
293  return false;
294  if(ban_expire[i] > 0)
295  {
297  strunzone(ban_ip[i]);
298  }
299  ban_expire[i] = 0;
300  ban_ip[i] = "";
301  Ban_SaveBans();
302  return true;
303 }
string ban_ip[BAN_MAX]
Definition: ipban.qc:253
float ban_expire[BAN_MAX]
Definition: ipban.qc:254
void OnlineBanList_SendUnban(string ip)
Definition: ipban.qc:55
void Ban_SaveBans()
Definition: ipban.qc:263
float ban_count
Definition: ipban.qc:255
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Ban_Insert()

float Ban_Insert ( string  ip,
float  bantime,
string  reason,
float  dosync 
)

Definition at line 505 of file ipban.qc.

References ban_count, Ban_Delete(), Ban_Enforce(), ban_expire, ban_ip, BAN_MAX, Ban_SaveBans(), ftos(), LOG_INFO, LOG_TRACE, max(), OnlineBanList_SendBan(), strzone(), substring(), and time.

Referenced by Ban_KickBanClient(), BanCommand_ban(), and OnlineBanList_URI_Get_Callback().

506 {
507  float i;
508  float j;
509  float bestscore;
510 
511  // already banned?
512  for(i = 0; i < ban_count; ++i)
513  if(ban_ip[i] == ip)
514  {
515  // prolong the ban
516  if(time + bantime > ban_expire[i])
517  {
518  ban_expire[i] = time + bantime;
519  LOG_TRACE(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now");
520  }
521  else
522  LOG_TRACE(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now");
523 
524  // and enforce
525  reason = Ban_Enforce(i, reason);
526 
527  // and abort
528  if(dosync)
529  if(reason != "")
530  if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
531  OnlineBanList_SendBan(ip, bantime, reason);
532 
533  return false;
534  }
535 
536  // do we have a free slot?
537  for(i = 0; i < ban_count; ++i)
538  if(time > ban_expire[i])
539  break;
540  // no free slot? Then look for the one who would get unbanned next
541  if(i >= BAN_MAX)
542  {
543  i = 0;
544  bestscore = ban_expire[i];
545  for(j = 1; j < ban_count; ++j)
546  {
547  if(ban_expire[j] < bestscore)
548  {
549  i = j;
550  bestscore = ban_expire[i];
551  }
552  }
553  }
554  // if we replace someone, will we be banned longer than him (so long-term
555  // bans never get overridden by short-term bans)
556  if(i < ban_count)
557  if(ban_expire[i] > time + bantime)
558  {
559  LOG_INFO(ip, " could not get banned due to no free ban slot");
560  return false;
561  }
562  // okay, insert our new victim as i
563  Ban_Delete(i);
564  LOG_TRACE(ip, " has been banned for ", ftos(bantime), " seconds");
565  ban_expire[i] = time + bantime;
566  ban_ip[i] = strzone(ip);
567  ban_count = max(ban_count, i + 1);
568 
569  Ban_SaveBans();
570 
571  reason = Ban_Enforce(i, reason);
572 
573  // and abort
574  if(dosync)
575  if(reason != "")
576  if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
577  OnlineBanList_SendBan(ip, bantime, reason);
578 
579  return true;
580 }
string ban_ip[BAN_MAX]
Definition: ipban.qc:253
string Ban_Enforce(float j, string reason)
Definition: ipban.qc:478
float ban_expire[BAN_MAX]
Definition: ipban.qc:254
void Ban_SaveBans()
Definition: ipban.qc:263
void OnlineBanList_SendBan(string ip, float bantime, string reason)
Definition: ipban.qc:33
#define LOG_INFO(...)
Definition: log.qh:70
float Ban_Delete(float i)
Definition: ipban.qc:286
#define LOG_TRACE(...)
Definition: log.qh:81
float ban_count
Definition: ipban.qc:255
const float BAN_MAX
Definition: ipban.qc:251
float time
Definition: csprogsdefs.qc:16
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Ban_KickBanClient()

void Ban_KickBanClient ( entity  client,
float  bantime,
float  masksize,
string  reason 
)

Definition at line 582 of file ipban.qc.

References Ban_GetClientIP(), ban_idfp, Ban_Insert(), ban_ip1, ban_ip2, ban_ip3, ban_ip4, sprint(), strcat(), and string_null.

Referenced by BanCommand_kickban().

583 {
584  string ip, id;
585  if(!Ban_GetClientIP(client))
586  {
587  sprint(client, strcat("Kickbanned: ", reason, "\n"));
588  dropclient(client);
589  return;
590  }
591 
592  // who to ban?
593  switch(masksize)
594  {
595  case 1:
596  ip = strcat1(ban_ip1);
597  break;
598  case 2:
599  ip = strcat1(ban_ip2);
600  break;
601  case 3:
602  ip = strcat1(ban_ip3);
603  break;
604  case 4:
605  default:
606  ip = strcat1(ban_ip4);
607  break;
608  }
609  if(ban_idfp)
610  id = strcat1(ban_idfp);
611  else
612  id = string_null;
613 
614  Ban_Insert(ip, bantime, reason, 1);
615  if(id)
616  Ban_Insert(id, bantime, reason, 1);
617  /*
618  * not needed, as we enforce the ban in Ban_Insert anyway
619  // and kick him
620  sprint(client, strcat("Kickbanned: ", reason, "\n"));
621  dropclient(client);
622  */
623 }
string string_null
Definition: nil.qh:9
float Ban_Insert(string ip, float bantime, string reason, float dosync)
Definition: ipban.qc:505
string ban_ip3
Definition: ipban.qc:259
string ban_idfp
Definition: ipban.qc:261
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1 s1 strcat(_("Level %s: "), "^BG%s\3\, _("^BGPress ^F2%s^BG to enter the game"))
float Ban_GetClientIP(entity client)
Definition: ipban.qc:353
string ban_ip4
Definition: ipban.qc:260
string ban_ip1
Definition: ipban.qc:257
string ban_ip2
Definition: ipban.qc:258
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Ban_LoadBans()

void Ban_LoadBans ( )

Definition at line 305 of file ipban.qc.

References argv(), autocvar_g_banned_list, ban_count, Ban_Delete(), ban_expire, ban_ip, ban_loaded, entity(), OnlineBanList_Think(), setthink, stof(), strzone(), time, and tokenize_console.

Referenced by Ban_IsClientBanned(), and spawnfunc().

306 {
307  float i, n;
308  for(i = 0; i < ban_count; ++i)
309  Ban_Delete(i);
310  ban_count = 0;
311  ban_loaded = true;
313  if(stof(argv(0)) == 1)
314  {
315  ban_count = (n - 1) / 2;
316  for(i = 0; i < ban_count; ++i)
317  {
318  ban_ip[i] = strzone(argv(2*i+1));
319  ban_expire[i] = time + stof(argv(2*i+2));
320  }
321  }
322 
323  entity e = new(bansyncer);
325  e.nextthink = time + 1;
326 }
string autocvar_g_banned_list
Definition: banning.qh:11
entity() spawn
string ban_ip[BAN_MAX]
Definition: ipban.qc:253
float ban_expire[BAN_MAX]
Definition: ipban.qc:254
float Ban_Delete(float i)
Definition: ipban.qc:286
#define tokenize_console
Definition: dpextensions.qh:24
float ban_count
Definition: ipban.qc:255
void OnlineBanList_Think(entity this)
Definition: ipban.qc:197
#define setthink(e, f)
float time
Definition: csprogsdefs.qc:16
float ban_loaded
Definition: ipban.qc:252
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Ban_MaybeEnforceBan()

float Ban_MaybeEnforceBan ( entity  client)

Definition at line 451 of file ipban.qc.

References autocvar_g_ban_telluser, Ban_IsClientBanned(), LOG_INFOF, and sprint().

Referenced by Ban_MaybeEnforceBanOnce().

452 {
453  if (Ban_IsClientBanned(client, -1))
454  {
455  if (!client.crypto_idfp)
456  LOG_INFOF("^1NOTE:^7 banned client %s just tried to enter\n",
457  client.netaddress);
458  else
459  LOG_INFOF("^1NOTE:^7 banned client %s (%s) just tried to enter\n",
460  client.netaddress, client.crypto_idfp);
461 
463  sprint(client, "You are banned from this server.\n");
464  dropclient(client);
465  return true;
466  }
467  return false;
468 }
bool autocvar_g_ban_telluser
Definition: banning.qh:10
float Ban_IsClientBanned(entity client, float idx)
Definition: ipban.qc:411
#define LOG_INFOF(...)
Definition: log.qh:71
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Ban_MaybeEnforceBanOnce()

float Ban_MaybeEnforceBanOnce ( entity  client)

Definition at line 471 of file ipban.qc.

References Ban_MaybeEnforceBan().

Referenced by ClientConnect(), and SV_ParseClientCommand().

472 {
473  if (client.ban_checked) return false;
474  client.ban_checked = true;
475  return Ban_MaybeEnforceBan(client);
476 }
bool Ban_MaybeEnforceBan(entity client)
Definition: ipban.qc:451
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Ban_SaveBans()

void Ban_SaveBans ( )

Definition at line 263 of file ipban.qc.

References ban_count, ban_expire, ban_ip, ban_loaded, cvar_set(), ftos(), strcat(), strlen(), and time.

Referenced by Ban_Delete(), Ban_Insert(), and Shutdown().

264 {
265  string out;
266  float i;
267 
268  if(!ban_loaded)
269  return;
270 
271  // version of list
272  out = "1";
273  for(i = 0; i < ban_count; ++i)
274  {
275  if(time > ban_expire[i])
276  continue;
277  out = strcat(out, " ", ban_ip[i]);
278  out = strcat(out, " ", ftos(ban_expire[i] - time));
279  }
280  if(strlen(out) <= 1) // no real entries
281  cvar_set("g_banned_list", "");
282  else
283  cvar_set("g_banned_list", out);
284 }
string ban_ip[BAN_MAX]
Definition: ipban.qc:253
float ban_expire[BAN_MAX]
Definition: ipban.qc:254
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1 s1 strcat(_("Level %s: "), "^BG%s\3\, _("^BGPress ^F2%s^BG to enter the game"))
float ban_count
Definition: ipban.qc:255
float time
Definition: csprogsdefs.qc:16
float ban_loaded
Definition: ipban.qc:252
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Ban_View()

void Ban_View ( )

Definition at line 328 of file ipban.qc.

References ban_count, ban_expire, ban_ip, ftos(), LOG_INFO, strcat(), and time.

Referenced by BanCommand_banlist().

329 {
330  float i, n;
331  string msg;
332 
333  LOG_INFO("^2Listing all existing active bans:");
334 
335  n = 0;
336  for(i = 0; i < ban_count; ++i)
337  {
338  if(time > ban_expire[i])
339  continue;
340 
341  ++n; // total number of existing bans
342 
343  msg = strcat("#", ftos(i), ": ");
344  msg = strcat(msg, ban_ip[i], " is still banned for ");
345  msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
346 
347  LOG_INFO(" ", msg);
348  }
349 
350  LOG_INFO("^2Done listing all active (", ftos(n), ") bans.");
351 }
string ban_ip[BAN_MAX]
Definition: ipban.qc:253
float ban_expire[BAN_MAX]
Definition: ipban.qc:254
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1 s1 strcat(_("Level %s: "), "^BG%s\3\, _("^BGPress ^F2%s^BG to enter the game"))
#define LOG_INFO(...)
Definition: log.qh:70
float ban_count
Definition: ipban.qc:255
float time
Definition: csprogsdefs.qc:16
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ BanCommand()

float BanCommand ( string  command)

Definition at line 316 of file banning.qc.

References BanCommand_macro_command(), and tokenize_console.

Referenced by GameCommand().

317 {
318  int argc = tokenize_console(command);
319 
320  // Guide for working with argc arguments by example:
321  // argc: 1 - 2 - 3 - 4
322  // argv: 0 - 1 - 2 - 3
323  // cmd vote - master - login - password
324 
325  if (BanCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
326  return true; // handled by one of the above GenericCommand_* functions
327 
328  return false;
329 }
float BanCommand_macro_command(int argc, string command)
Definition: banning.qc:285
#define tokenize_console
Definition: dpextensions.qh:24
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OnlineBanList_URI_Get_Callback()

void OnlineBanList_URI_Get_Callback ( float  id,
float  status,
string  data 
)

Definition at line 79 of file ipban.qc.

References argv(), autocvar_g_ban_sync_interval, autocvar_g_ban_sync_timeout, autocvar_g_ban_sync_trusted_servers_verify, autocvar_g_ban_sync_uri, Ban_Insert(), ftos(), LABEL, LOG_INFO, LOG_TRACE, MAX_IPBAN_URIS, min(), OnlineBanList_RequestWaiting, OnlineBanList_Servers, OnlineBanList_Timeout, stof(), strcat(), strlen(), strstrofs, substring(), time, tokenize_console, tokenizebyseparator, and URI_GET_IPBAN.

Referenced by URI_Get_Callback().

80 {
81  float n, i, j, l;
82  string ip;
83  float timeleft;
84  string reason;
85  string serverip;
86  float syncinterval;
87  string uri;
88 
89  id -= URI_GET_IPBAN;
90 
91  if(id >= MAX_IPBAN_URIS)
92  {
93  LOG_INFO("Received ban list for invalid ID");
94  return;
95  }
96 
98  uri = argv(id);
99 
100  string prelude = strcat("Received ban list from ", uri, ": ");
101 
102  if(OnlineBanList_RequestWaiting[id] == 0)
103  {
104  LOG_INFO(prelude, "rejected (unexpected)");
105  return;
106  }
107 
109 
111  {
112  LOG_INFO(prelude, "rejected (too late)");
113  return;
114  }
115 
116  syncinterval = autocvar_g_ban_sync_interval;
117  if(syncinterval == 0)
118  {
119  LOG_INFO(prelude, "rejected (syncing disabled)");
120  return;
121  }
122  if(syncinterval > 0)
123  syncinterval *= 60;
124 
125  if(status != 0)
126  {
127  LOG_INFO(prelude, "error: status is ", ftos(status));
128  return;
129  }
130 
131  if(substring(data, 0, 1) == "<")
132  {
133  LOG_INFO(prelude, "error: received HTML instead of a ban list");
134  return;
135  }
136 
137  if(strstrofs(data, "\r", 0) != -1)
138  {
139  LOG_INFO(prelude, "error: received carriage returns");
140  return;
141  }
142 
143  if(data == "")
144  n = 0;
145  else
146  n = tokenizebyseparator(data, "\n");
147 
148  if((n % 4) != 0)
149  {
150  LOG_INFO(prelude, "error: received invalid item count: ", ftos(n));
151  return;
152  }
153 
154  LOG_INFO(prelude, "OK, ", ftos(n / 4), " items");
155 
156  for(i = 0; i < n; i += 4)
157  {
158  ip = argv(i);
159  timeleft = stof(argv(i + 1));
160  reason = argv(i + 2);
161  serverip = argv(i + 3);
162 
163  LOG_TRACE("received ban list item ", ftos(i / 4), ": ip=", ip);
164  LOG_TRACE(" timeleft=", ftos(timeleft), " reason=", reason);
165  LOG_TRACE(" serverip=", serverip);
166 
167  timeleft -= 1.5 * autocvar_g_ban_sync_timeout;
168  if(timeleft < 0)
169  continue;
170 
171  l = strlen(ip);
172  if(l != 44) // length 44 is a cryptographic ID
173  {
174  for(j = 0; j < l; ++j)
175  if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
176  {
177  LOG_INFO("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.");
178  goto skip;
179  }
180  }
181 
183  if((strstrofs(strcat(";", OnlineBanList_Servers, ";"), strcat(";", serverip, ";"), 0) == -1))
184  continue;
185 
186  if(syncinterval > 0)
187  timeleft = min(syncinterval + (OnlineBanList_Timeout - time) + 5, timeleft);
188  // the ban will be prolonged on the next sync
189  // or expire 5 seconds after the next timeout
190  Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip, " at ", uri), 0);
191  LOG_INFO("Ban list syncing: accepted ban of ", ip, " by ", serverip, " at ", uri, ": ", reason);
192 
193 LABEL(skip)
194  }
195 }
float Ban_Insert(string ip, float bantime, string reason, float dosync)
Definition: ipban.qc:505
float OnlineBanList_Timeout
Definition: ipban.qc:76
float autocvar_g_ban_sync_interval
Definition: banning.qh:5
const int URI_GET_IPBAN
Definition: urllib.qh:5
bool autocvar_g_ban_sync_trusted_servers_verify
Definition: banning.qh:8
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1 s1 strcat(_("Level %s: "), "^BG%s\3\, _("^BGPress ^F2%s^BG to enter the game"))
#define LOG_INFO(...)
Definition: log.qh:70
float OnlineBanList_RequestWaiting[MAX_IPBAN_URIS]
Definition: ipban.qc:77
#define strstrofs
Definition: dpextensions.qh:42
#define tokenize_console
Definition: dpextensions.qh:24
string autocvar_g_ban_sync_uri
Definition: banning.qh:9
#define LOG_TRACE(...)
Definition: log.qh:81
#define tokenizebyseparator
Definition: dpextensions.qh:21
float autocvar_g_ban_sync_timeout
Definition: banning.qh:6
#define LABEL(id)
Definition: compiler.qh:36
#define MAX_IPBAN_URIS
Definition: ipban.qc:31
float time
Definition: csprogsdefs.qc:16
string OnlineBanList_Servers
Definition: ipban.qc:75
+ Here is the call graph for this function:
+ Here is the caller graph for this function: