robust: add login block by ID0. Note that like by mac this may even block wrong users; cosmetics

This commit is contained in:
UbitUmarov
2023-01-09 12:39:42 +00:00
parent 8d16d0b0ba
commit c821ef25cb
4 changed files with 410 additions and 337 deletions

View File

@@ -46,9 +46,7 @@ namespace OpenSim.Services.HypergridService
{
public class GatekeeperService : IGatekeeperService
{
private static readonly ILog m_log =
LogManager.GetLogger(
MethodBase.GetCurrentMethod().DeclaringType);
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static bool m_Initialized = false;
@@ -60,12 +58,13 @@ namespace OpenSim.Services.HypergridService
private static IGridUserService m_GridUserService;
private static IBansService m_BansService;
private static string m_AllowedClients = string.Empty;
private static string m_DeniedClients = string.Empty;
private static Regex m_AllowedClientsRegex = null;
private static Regex m_DeniedClientsRegex = null;
private static string m_DeniedMacs = string.Empty;
private static string m_DeniedID0s = string.Empty;
private static bool m_ForeignAgentsAllowed = true;
private static List<string> m_ForeignsAllowedExceptions = new List<string>();
private static List<string> m_ForeignsDisallowedExceptions = new List<string>();
private static readonly List<string> m_ForeignsAllowedExceptions = new();
private static readonly List<string> m_ForeignsDisallowedExceptions = new();
private static UUID m_ScopeID;
private static bool m_AllowTeleportsToAnyRegion;
@@ -85,7 +84,7 @@ namespace OpenSim.Services.HypergridService
m_Initialized = true;
IConfig serverConfig = config.Configs["GatekeeperService"];
if (serverConfig == null)
if (serverConfig is null)
throw new Exception(String.Format("No section GatekeeperService in config file"));
string accountService = serverConfig.GetString("UserAccountService", string.Empty);
@@ -124,11 +123,10 @@ namespace OpenSim.Services.HypergridService
string[] alias = gatekeeperURIAlias.Split(',');
for (int i = 0; i < alias.Length; ++i)
{
OSHHTPHost tmp = new OSHHTPHost(alias[i].Trim(), false);
OSHHTPHost tmp = new(alias[i].Trim(), false);
if (tmp.IsValidHost)
{
if (m_gateKeeperAlias == null)
m_gateKeeperAlias = new HashSet<OSHHTPHost>();
m_gateKeeperAlias ??= new HashSet<OSHHTPHost>();
m_gateKeeperAlias.Add(tmp);
}
}
@@ -138,43 +136,68 @@ namespace OpenSim.Services.HypergridService
m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args);
m_PresenceService = ServerUtils.LoadPlugin<IPresenceService>(presenceService, args);
if (accountService != string.Empty)
if (!string.IsNullOrEmpty(accountService))
m_UserAccountService = ServerUtils.LoadPlugin<IUserAccountService>(accountService, args);
if (homeUsersService != string.Empty)
if (!string.IsNullOrEmpty(homeUsersService))
m_UserAgentService = ServerUtils.LoadPlugin<IUserAgentService>(homeUsersService, args);
if (gridUserService != string.Empty)
if (!string.IsNullOrEmpty(gridUserService))
m_GridUserService = ServerUtils.LoadPlugin<IGridUserService>(gridUserService, args);
if (bansService != string.Empty)
if (!string.IsNullOrEmpty(bansService))
m_BansService = ServerUtils.LoadPlugin<IBansService>(bansService, args);
if (simService != null)
if (simService is not null)
m_SimulationService = simService;
else if (simulationService != string.Empty)
m_SimulationService = ServerUtils.LoadPlugin<ISimulationService>(simulationService, args);
string[] possibleAccessControlConfigSections = new string[] { "AccessControl", "GatekeeperService" };
m_AllowedClients = Util.GetConfigVarFromSections<string>(
config, "AllowedClients", possibleAccessControlConfigSections, string.Empty);
m_DeniedClients = Util.GetConfigVarFromSections<string>(
config, "DeniedClients", possibleAccessControlConfigSections, string.Empty);
m_DeniedMacs = Util.GetConfigVarFromSections<string>(
config, "DeniedMacs", possibleAccessControlConfigSections, string.Empty);
string AllowedClients = Util.GetConfigVarFromSections<string>(config, "AllowedClients", possibleAccessControlConfigSections, string.Empty);
if (!string.IsNullOrEmpty(AllowedClients))
{
try
{
m_AllowedClientsRegex = new Regex(AllowedClients, RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
catch
{
m_AllowedClientsRegex = null;
m_log.Error("[GATEKEEPER SERVICE]: failed to parse AllowedClients");
}
}
string DeniedClients = Util.GetConfigVarFromSections<string>(config, "DeniedClients", possibleAccessControlConfigSections, string.Empty);
if (!string.IsNullOrEmpty(DeniedClients))
{
try
{
m_DeniedClientsRegex = new Regex(DeniedClients, RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
catch
{
m_DeniedClientsRegex = null;
m_log.Error("[GATEKEEPER SERVICE]: failed to parse DeniedClients");
}
}
m_DeniedMacs = Util.GetConfigVarFromSections<string>(config, "DeniedMacs", possibleAccessControlConfigSections, string.Empty);
m_DeniedID0s = Util.GetConfigVarFromSections<string>(config, "DeniedID0s", possibleAccessControlConfigSections, string.Empty);
m_ForeignAgentsAllowed = serverConfig.GetBoolean("ForeignAgentsAllowed", true);
LoadDomainExceptionsFromConfig(serverConfig, "AllowExcept", m_ForeignsAllowedExceptions);
LoadDomainExceptionsFromConfig(serverConfig, "DisallowExcept", m_ForeignsDisallowedExceptions);
if (m_GridService == null || m_PresenceService == null || m_SimulationService == null)
if (m_GridService is null || m_PresenceService is null || m_SimulationService is null)
throw new Exception("Unable to load a required plugin, Gatekeeper Service cannot function.");
IConfig presenceConfig = config.Configs["PresenceService"];
if (presenceConfig != null)
if (presenceConfig is not null)
{
m_allowDuplicatePresences = presenceConfig.GetBoolean("AllowDuplicatePresences", m_allowDuplicatePresences);
}
IConfig messagingConfig = config.Configs["Messaging"];
if (messagingConfig != null)
if (messagingConfig is not null)
m_messageKey = messagingConfig.GetString("MessageKey", String.Empty);
m_log.Debug("[GATEKEEPER SERVICE]: Starting...");
}
@@ -190,8 +213,13 @@ namespace OpenSim.Services.HypergridService
string value = config.GetString(variable, string.Empty);
string[] parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in parts)
exceptions.Add(s.Trim());
foreach (string ps in parts)
{
string s = ps.Trim();
if(!s.EndsWith("/"))
s += '/';
exceptions.Add(s);
}
}
public bool LinkRegion(string regionName, out UUID regionID, out ulong regionHandle, out string externalName, out string imageURL, out string reason, out int sizeX, out int sizeY)
@@ -203,13 +231,13 @@ namespace OpenSim.Services.HypergridService
externalName = m_gatekeeperURL + ((regionName != string.Empty) ? " " + regionName : "");
imageURL = string.Empty;
reason = string.Empty;
GridRegion region = null;
GridRegion region;
//m_log.DebugFormat("[GATEKEEPER SERVICE]: Request to link to {0}", (regionName.Length == 0)? "default region" : regionName);
if (!m_AllowTeleportsToAnyRegion || regionName.Length == 0)
{
List<GridRegion> defs = m_GridService.GetDefaultHypergridRegions(m_ScopeID);
if (defs != null && defs.Count > 0)
if (defs is not null && defs.Count > 0)
{
region = defs[0];
m_DefaultGatewayRegion = region;
@@ -224,7 +252,7 @@ namespace OpenSim.Services.HypergridService
else
{
region = m_GridService.GetRegionByName(m_ScopeID, regionName);
if (region == null)
if (region is null)
{
reason = "Region not found";
return false;
@@ -256,7 +284,7 @@ namespace OpenSim.Services.HypergridService
m_DefaultGatewayRegion.RegionID,
m_DefaultGatewayRegion.ServerURI,
agentID,
agentHomeURI == null ? "" : " @ " + agentHomeURI);
agentHomeURI is null ? "" : " @ " + agentHomeURI);
message = "Teleporting to the default region.";
return m_DefaultGatewayRegion;
@@ -268,7 +296,7 @@ namespace OpenSim.Services.HypergridService
{
m_log.DebugFormat(
"[GATEKEEPER SERVICE]: Could not find region with ID {0} as requested by user {1}{2}. Returning null.",
regionID, agentID, (agentHomeURI == null) ? "" : " @ " + agentHomeURI);
regionID, agentID, (agentHomeURI is null) ? "" : " @ " + agentHomeURI);
message = "The teleport destination could not be found.";
return null;
@@ -280,7 +308,7 @@ namespace OpenSim.Services.HypergridService
region.RegionID,
region.ServerURI,
agentID,
agentHomeURI == null ? "" : " @ " + agentHomeURI);
agentHomeURI is null ? "" : " @ " + agentHomeURI);
return region;
}
@@ -306,35 +334,39 @@ namespace OpenSim.Services.HypergridService
//
// Check client
//
if (!String.IsNullOrWhiteSpace(m_AllowedClients))
if (m_AllowedClientsRegex is not null)
{
Regex arx = new Regex(m_AllowedClients);
Match am = arx.Match(curViewer);
if (!am.Success)
lock(m_AllowedClientsRegex)
{
reason = "Login failed: client " + curViewer + " is not allowed";
m_log.InfoFormat("[GATEKEEPER SERVICE]: Login failed, reason: client {0} is not allowed", curViewer);
return false;
Match am = m_AllowedClientsRegex.Match(curViewer);
if (!am.Success)
{
reason = "Login failed: client " + curViewer + " is not allowed";
m_log.InfoFormat("[GATEKEEPER SERVICE]: Login failed, reason: client {0} is not allowed", curViewer);
return false;
}
}
}
if (!String.IsNullOrWhiteSpace(m_DeniedClients))
if (m_DeniedClientsRegex is not null)
{
Regex drx = new Regex(m_DeniedClients);
Match dm = drx.Match(curViewer);
if (dm.Success)
lock(m_DeniedClientsRegex)
{
reason = "Login failed: client " + curViewer + " is denied";
m_log.InfoFormat("[GATEKEEPER SERVICE]: Login failed, reason: client {0} is denied", curViewer);
return false;
Match dm = m_DeniedClientsRegex.Match(curViewer);
if (dm.Success)
{
reason = "Login failed: client " + curViewer + " is denied";
m_log.InfoFormat("[GATEKEEPER SERVICE]: Login failed, reason: client {0} is denied", curViewer);
return false;
}
}
}
if (!String.IsNullOrWhiteSpace(m_DeniedMacs))
{
m_log.InfoFormat("[GATEKEEPER SERVICE]: Checking users Mac {0} against list of denied macs {1} ...", curMac, m_DeniedMacs);
//m_log.InfoFormat("[GATEKEEPER SERVICE]: Checking users Mac {0} against list of denied macs {1} ...", curMac, m_DeniedMacs);
if (m_DeniedMacs.Contains(curMac))
{
reason = "Login failed: client with Mac " + curMac + " is denied";
@@ -343,6 +375,17 @@ namespace OpenSim.Services.HypergridService
}
}
if (!string.IsNullOrWhiteSpace(m_DeniedID0s))
{
//m_log.InfoFormat("[GATEKEEPER SERVICE]: Checking users Mac {0} against list of denied macs {1} ...", curMac, m_DeniedMacs);
if (m_DeniedID0s.Contains(aCircuit.Id0))
{
reason = "Login failed: client with id0 " + aCircuit.Id0 + " is denied";
m_log.InfoFormat("[GATEKEEPER SERVICE]: Login failed, reason: client with mac {0} is denied", aCircuit.Id0);
return false;
}
}
//
// Authenticate the user
//
@@ -358,14 +401,14 @@ namespace OpenSim.Services.HypergridService
// Check for impersonations
//
UserAccount account = null;
if (m_UserAccountService != null)
if (m_UserAccountService is not null)
{
// Check to see if we have a local user with that UUID
account = m_UserAccountService.GetUserAccount(m_ScopeID, aCircuit.AgentID);
if (account != null)
if (account is not null)
{
// Make sure this is the user coming home, and not a foreign user with same UUID as a local user
if (m_UserAgentService != null)
if (m_UserAgentService is not null)
{
if (!m_UserAgentService.IsAgentComingHome(aCircuit.SessionID, m_gatekeeperURL))
{
@@ -383,7 +426,7 @@ namespace OpenSim.Services.HypergridService
//
// Foreign agents allowed? Exceptions?
//
if (account == null)
if (account is null)
{
bool allowed = m_ForeignAgentsAllowed;
@@ -406,8 +449,8 @@ namespace OpenSim.Services.HypergridService
// Is the user banned?
// This uses a Ban service that's more powerful than the configs
//
string uui = (account != null ? aCircuit.AgentID.ToString() : Util.ProduceUserUniversalIdentifier(aCircuit));
if (m_BansService != null && m_BansService.IsBanned(uui, aCircuit.IPAddress, aCircuit.Id0, authURL))
string uui = (account is not null ? aCircuit.AgentID.ToString() : Util.ProduceUserUniversalIdentifier(aCircuit));
if (m_BansService is not null && m_BansService.IsBanned(uui, aCircuit.IPAddress, aCircuit.Id0, authURL))
{
reason = "You are banned from this world";
m_log.InfoFormat("[GATEKEEPER SERVICE]: Login failed, reason: user {0} is banned", uui);
@@ -415,32 +458,33 @@ namespace OpenSim.Services.HypergridService
}
UUID agentID = aCircuit.AgentID;
if(agentID == new UUID("6571e388-6218-4574-87db-f9379718315e"))
if(agentID.Equals(Constants.servicesGodAgentID))
{
// really?
reason = "Invalid account ID";
return false;
}
if(m_GridUserService != null)
if(m_GridUserService is not null)
{
string PrincipalIDstr = agentID.ToString();
GridUserInfo guinfo = m_GridUserService.GetGridUserInfo(PrincipalIDstr);
if(!m_allowDuplicatePresences)
GridUserInfo guinfo = m_GridUserService.GetGridUserInfo(uui);
if (guinfo is not null)
{
if(guinfo != null && guinfo.Online && guinfo.LastRegionID != UUID.Zero)
if (!m_allowDuplicatePresences)
{
if(SendAgentGodKillToRegion(UUID.Zero, agentID, guinfo))
if (guinfo.Online && !guinfo.LastRegionID.IsZero())
{
if(account != null)
m_log.InfoFormat(
"[GATEKEEPER SERVICE]: Login failed for {0} {1}, reason: already logged in",
account.FirstName, account.LastName);
reason = "You appear to be already logged in on the destination grid " +
"Please wait a a minute or two and retry. " +
"If this takes longer than a few minutes please contact the grid owner.";
return false;
if (SendAgentGodKillToRegion(UUID.Zero, agentID, uui, guinfo))
{
if (account is not null)
m_log.InfoFormat(
"[GATEKEEPER SERVICE]: Login failed for {0} {1}, reason: already logged in",
account.FirstName, account.LastName);
reason = "You appear to be already logged in on the destination grid " +
"Please wait a a minute or two and retry. " +
"If this takes longer than a few minutes please contact the grid owner.";
return false;
}
}
}
}
@@ -453,7 +497,7 @@ namespace OpenSim.Services.HypergridService
// Login the presence, if it's not there yet (by the login service)
//
PresenceInfo presence = m_PresenceService.GetAgent(aCircuit.SessionID);
if (presence != null) // it has been placed there by the login service
if (presence is not null) // it has been placed there by the login service
isFirstLogin = true;
else
@@ -472,7 +516,7 @@ namespace OpenSim.Services.HypergridService
// Get the region
//
destination = m_GridService.GetRegionByUUID(m_ScopeID, destination.RegionID);
if (destination == null)
if (destination is null)
{
reason = "Destination region not found";
return false;
@@ -484,18 +528,18 @@ namespace OpenSim.Services.HypergridService
//
// Adjust the visible name
//
if (account != null)
if (account is not null)
{
aCircuit.firstname = account.FirstName;
aCircuit.lastname = account.LastName;
}
if (account == null)
if (account is null)
{
if (!aCircuit.lastname.StartsWith("@"))
aCircuit.firstname = aCircuit.firstname + "." + aCircuit.lastname;
try
{
Uri uri = new Uri(aCircuit.ServiceURLs["HomeURI"].ToString());
Uri uri = new(aCircuit.ServiceURLs["HomeURI"].ToString());
aCircuit.lastname = "@" + uri.Authority;
}
catch
@@ -515,7 +559,7 @@ namespace OpenSim.Services.HypergridService
m_log.DebugFormat("[GATEKEEPER SERVICE]: Launching {0}, Teleport Flags: {1}", aCircuit.Name, loginFlag);
EntityTransferContext ctx = new EntityTransferContext();
EntityTransferContext ctx = new();
if (!m_SimulationService.QueryAccess(
destination, aCircuit.AgentID, aCircuit.ServiceURLs["HomeURI"].ToString(),
@@ -528,7 +572,7 @@ namespace OpenSim.Services.HypergridService
{
m_log.DebugFormat("[GATEKEEPER SERVICE]: Login presence {0} is ok", aCircuit.Name);
if(!isFirstLogin && m_GridUserService != null && account == null)
if(!isFirstLogin && m_GridUserService is not null && account is null)
{
// Also login foreigners with GridUser service
string userId = aCircuit.AgentID.ToString();
@@ -566,7 +610,7 @@ namespace OpenSim.Services.HypergridService
if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
userURL = aCircuit.ServiceURLs["HomeURI"].ToString();
OSHHTPHost userHomeHost = new OSHHTPHost(userURL, true);
OSHHTPHost userHomeHost = new(userURL, true);
if(!userHomeHost.IsResolvedHost)
{
m_log.DebugFormat("[GATEKEEPER SERVICE]: Agent did not provide an authentication server URL");
@@ -601,7 +645,7 @@ namespace OpenSim.Services.HypergridService
if (parts.Length < 2)
return false;
OSHHTPHost reqGrid = new OSHHTPHost(parts[0], false);
OSHHTPHost reqGrid = new(parts[0], false);
if(!reqGrid.IsValidHost)
{
m_log.DebugFormat("[GATEKEEPER SERVICE]: Visitor provided malformed gird address {0}", parts[0]);
@@ -624,31 +668,30 @@ namespace OpenSim.Services.HypergridService
private bool IsException(AgentCircuitData aCircuit, List<string> exceptions)
{
bool exception = false;
if (exceptions.Count > 0) // we have exceptions
{
// Retrieve the visitor's origin
string userURL = aCircuit.ServiceURLs["HomeURI"].ToString();
string userURL = aCircuit.ServiceURLs["HomeURI"].ToString().Trim();
if (string.IsNullOrEmpty(userURL))
return false;
if (!userURL.EndsWith("/"))
userURL += "/";
if (exceptions.Find(delegate(string s)
{
if (!s.EndsWith("/"))
s += "/";
return s == userURL;
}) != null)
exception = true;
if(userURL.Equals(s))
return true;
}
}
return exception;
return false;
}
private bool SendAgentGodKillToRegion(UUID scopeID, UUID agentID , GridUserInfo guinfo)
private bool SendAgentGodKillToRegion(UUID scopeID, UUID agentID, string uui, GridUserInfo guinfo)
{
UUID regionID = guinfo.LastRegionID;
GridRegion regInfo = m_GridService.GetRegionByUUID(scopeID, regionID);
if(regInfo == null)
if(regInfo is null)
return false;
string regURL = regInfo.ServerURI;
@@ -671,7 +714,7 @@ namespace OpenSim.Services.HypergridService
msg.binaryBucket = new byte[1] {0};
InstantMessageServiceConnector.SendInstantMessage(regURL,msg, m_messageKey);
m_GridUserService.LoggedOut(agentID.ToString(),
m_GridUserService.LoggedOut(uui,
UUID.Zero, guinfo.LastRegionID, guinfo.LastPosition, guinfo.LastLookAt);
return true;