From 690ac99ff9f4c95ce9e810002ed3c3b14e5b802b Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 14 Aug 2018 16:10:18 -0400 Subject: [PATCH] Fix some windows SID stuff --- .../Security/WindowsSystemIdentity.cs | 4 +++ .../Security/WindowsSystemIdentityFactory.cs | 32 +++++++++++-------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Tgstation.Server.Host/Security/WindowsSystemIdentity.cs b/src/Tgstation.Server.Host/Security/WindowsSystemIdentity.cs index 6f1ab56cba..deb07e49f9 100644 --- a/src/Tgstation.Server.Host/Security/WindowsSystemIdentity.cs +++ b/src/Tgstation.Server.Host/Security/WindowsSystemIdentity.cs @@ -45,7 +45,11 @@ namespace Tgstation.Server.Host.Security if (identity != null) identity.Dispose(); else + { + var context = userPrincipal.Context; userPrincipal.Dispose(); + context.Dispose(); + } } /// diff --git a/src/Tgstation.Server.Host/Security/WindowsSystemIdentityFactory.cs b/src/Tgstation.Server.Host/Security/WindowsSystemIdentityFactory.cs index f89bdf48b2..54aff4fe40 100644 --- a/src/Tgstation.Server.Host/Security/WindowsSystemIdentityFactory.cs +++ b/src/Tgstation.Server.Host/Security/WindowsSystemIdentityFactory.cs @@ -1,6 +1,7 @@ using Microsoft.Win32.SafeHandles; using System; using System.DirectoryServices.AccountManagement; +using System.Runtime.InteropServices; using System.Security.Principal; using System.Threading; using System.Threading.Tasks; @@ -27,26 +28,31 @@ namespace Tgstation.Server.Host.Security //machine logon first cause it's faster try { - pc = new PrincipalContext(ContextType.Machine, Environment.UserDomainName); + pc = new PrincipalContext(ContextType.Machine); principal = UserPrincipal.FindByIdentity(pc, user.SystemIdentifier); - - if (principal == null) - { - pc.Dispose(); - pc = new PrincipalContext(ContextType.Domain, Environment.UserDomainName); - principal = UserPrincipal.FindByIdentity(pc, user.SystemIdentifier); - if (principal == null) - pc.Dispose(); - } } - catch + catch(COMException) { pc?.Dispose(); - throw; } if (principal == null) - return null; + { + //try domain now + try + { + var udn = Environment.UserDomainName; + pc = new PrincipalContext(ContextType.Domain, udn); + principal = UserPrincipal.FindByIdentity(pc, user.SystemIdentifier); + } + catch (PrincipalServerDownException) { } + + if(principal == null) + { + pc?.Dispose(); + return null; + } + } return (ISystemIdentity)new WindowsSystemIdentity(principal); }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);