home *** CD-ROM | disk | FTP | other *** search
/ Game Level Design / GLDesign.bin / Software / UnrealEngine2Runtime / UE2Runtime-22262001_Demo.exe / Engine / Classes / AccessControl.uc next >
Text File  |  2003-06-23  |  4KB  |  198 lines

  1. //=============================================================================
  2. // AccessControl.
  3. //
  4. // AccessControl is a helper class for GameInfo.
  5. // The AccessControl class determines whether or not the player is allowed to 
  6. // login in the PreLogin() function, and also controls whether or not a player 
  7. // can enter as a spectator or a game administrator.
  8. //
  9. //=============================================================================
  10. class AccessControl extends Info;
  11.  
  12. var globalconfig string     IPPolicies[50];
  13. var    localized string          IPBanned;
  14. var    localized string          WrongPassword;
  15. var    localized string          NeedPassword;
  16. var class<Admin> AdminClass;
  17.  
  18. var private globalconfig string AdminPassword;        // Password to receive bAdmin privileges.
  19. var private globalconfig string GamePassword;            // Password to enter game.
  20.  
  21. var bool bDontAddDefaultAdmin;
  22.  
  23.  
  24. function SetAdminPassword(string P)
  25. {
  26.     AdminPassword = P;
  27. }
  28.  
  29. function SetGamePassword(string P)
  30. {
  31.     GamePassword = P;
  32. }
  33.  
  34. function bool RequiresPassword()
  35. {
  36.     return GamePassword != "";
  37. }
  38.  
  39. function Kick( string S ) 
  40. {
  41.     local PlayerController P;
  42.  
  43.     ForEach DynamicActors(class'PlayerController', P)
  44.         if ( P.PlayerReplicationInfo.PlayerName~=S 
  45.             &&    (NetConnection(P.Player)!=None) )
  46.         {
  47.             P.Destroy();
  48.             return;
  49.         }
  50. }
  51.  
  52. function KickBan( string S ) 
  53. {
  54.     local PlayerController P;
  55.     local string IP;
  56.     local int j;
  57.  
  58.     ForEach DynamicActors(class'PlayerController', P)
  59.         if ( P.PlayerReplicationInfo.PlayerName~=S 
  60.             &&    (NetConnection(P.Player)!=None) )
  61.         {
  62.             IP = P.GetPlayerNetworkAddress();
  63.             if( CheckIPPolicy(IP) )
  64.             {
  65.                 IP = Left(IP, InStr(IP, ":"));
  66.                 Log("Adding IP Ban for: "$IP);
  67.                 for(j=0;j<50;j++)
  68.                     if( IPPolicies[j] == "" )
  69.                         break;
  70.                 if(j < 50)
  71.                     IPPolicies[j] = "DENY,"$IP;
  72.                 SaveConfig();
  73.             }
  74.             P.Destroy();
  75.             return;
  76.         }
  77. }
  78.  
  79. function bool AdminLogin( PlayerController P, string Password )
  80. {
  81.     if (AdminPassword == "")
  82.         return false;
  83.  
  84.     if (Password == AdminPassword)
  85.     {
  86.         Log("Administrator logged in.");
  87.         Level.Game.Broadcast( P, P.PlayerReplicationInfo.PlayerName$"logged in as a server administrator." );
  88.         return true;
  89.     }
  90.     return false;
  91. }
  92.  
  93. //
  94. // Accept or reject a player on the server.
  95. // Fails login if you set the Error to a non-empty string.
  96. //
  97. event PreLogin
  98. (
  99.     string Options,
  100.     string Address,
  101.     out string Error,
  102.     out string FailCode,
  103.     bool bSpectator
  104. )
  105.  
  106. {
  107.     // Do any name or password or name validation here.
  108.     local string InPassword;
  109.  
  110.     Error="";
  111.     InPassword = Level.Game.ParseOption( Options, "Password" );
  112.  
  113.     if( (Level.NetMode != NM_Standalone) && Level.Game.AtCapacity(bSpectator) )
  114.     {
  115.         Error=Level.Game.GameMessageClass.Default.MaxedOutMessage;
  116.     }
  117.     else if
  118.     (    GamePassword!=""
  119.     &&    caps(InPassword)!=caps(GamePassword)
  120.     &&    (AdminPassword=="" || caps(InPassword)!=caps(AdminPassword)) )
  121.     {
  122.         if( InPassword == "" )
  123.         {
  124.             Error = NeedPassword;
  125.             FailCode = "NEEDPW";
  126.         }
  127.         else
  128.         {
  129.             Error = WrongPassword;
  130.             FailCode = "WRONGPW";
  131.         }
  132.     }
  133.  
  134.     if(!CheckIPPolicy(Address))
  135.         Error = IPBanned;
  136. }
  137.  
  138.  
  139. function bool CheckIPPolicy(string Address)
  140. {
  141.     local int i, j, LastMatchingPolicy;
  142.     local string Policy, Mask;
  143.     local bool bAcceptAddress, bAcceptPolicy;
  144.     
  145.     // strip port number
  146.     j = InStr(Address, ":");
  147.     if(j != -1)
  148.         Address = Left(Address, j);
  149.  
  150.     bAcceptAddress = True;
  151.     for(i=0; i<50 && IPPolicies[i] != ""; i++)
  152.     {
  153.         j = InStr(IPPolicies[i], ",");
  154.         if(j==-1)
  155.             continue;
  156.         Policy = Left(IPPolicies[i], j);
  157.         Mask = Mid(IPPolicies[i], j+1);
  158.         if(Policy ~= "ACCEPT") 
  159.             bAcceptPolicy = True;
  160.         else
  161.         if(Policy ~= "DENY") 
  162.             bAcceptPolicy = False;
  163.         else
  164.             continue;
  165.  
  166.         j = InStr(Mask, "*");
  167.         if(j != -1)
  168.         {
  169.             if(Left(Mask, j) == Left(Address, j))
  170.             {
  171.                 bAcceptAddress = bAcceptPolicy;
  172.                 LastMatchingPolicy = i;
  173.             }
  174.         }
  175.         else
  176.         {
  177.             if(Mask == Address)
  178.             {
  179.                 bAcceptAddress = bAcceptPolicy;
  180.                 LastMatchingPolicy = i;
  181.             }
  182.         }
  183.     }
  184.  
  185.     if(!bAcceptAddress)
  186.         Log("Denied connection for "$Address$" with IP policy "$IPPolicies[LastMatchingPolicy]);
  187.         
  188.     return bAcceptAddress;
  189. }
  190.  
  191. defaultproperties
  192. {
  193.     WrongPassword="The password you entered is incorrect."
  194.     NeedPassword="You need to enter a password to join this game."
  195.     IPBanned="Your IP address has been banned on this server."
  196.     IPPolicies(0)="ACCEPT,*"
  197.     AdminClass=class'Engine.Admin'
  198. }