Esempio n. 1
0
        protected virtual ADServerInfo GetDefaultServerInfo(string partitionFqdn)
        {
            string serverFqdn = null;

            if (partitionFqdn.Equals(TopologyProvider.LocalForestFqdn, StringComparison.OrdinalIgnoreCase) && NativeHelpers.LocalMachineRoleIsDomainController())
            {
                serverFqdn = NativeHelpers.GetLocalComputerFqdn(false);
            }
            return(new ADServerInfo(serverFqdn, partitionFqdn, this.DefaultDCPort, null, 100, AuthType.Kerberos, true));
        }
        public override ADServerInfo GetRemoteServerFromDomainFqdn(string domainFqdn, NetworkCredential credential)
        {
            ExTraceGlobals.TopologyProviderTracer.TraceDebug <string, string>((long)this.GetHashCode(), "Need server from remote domain {0} {1} credentials.", domainFqdn, (credential == null) ? "without" : "with");
            if (credential == null)
            {
                return(this.GetServerFromDomainDN(NativeHelpers.DistinguishedNameFromCanonicalName(domainFqdn), credential));
            }
            LdapTopologyProvider ldapTopologyProvider = new LdapTopologyProvider();

            return(ldapTopologyProvider.GetRemoteServerFromDomainFqdn(domainFqdn, credential));
        }
        private static ADServerInfo FindDirectoryServerForForestOrDomain(string domainOrForestFqdn, NetworkCredential credential, bool requireGCs = false)
        {
            StringCollection stringCollection = requireGCs ? NativeHelpers.FindAllGlobalCatalogs(domainOrForestFqdn, null) : NativeHelpers.FindAllDomainControllers(domainOrForestFqdn, null);
            LocalizedString  value            = LocalizedString.Empty;
            LocalizedString  empty            = LocalizedString.Empty;
            string           writableNC       = null;
            string           siteName         = null;

            foreach (string text in stringCollection)
            {
                if (SuitabilityVerifier.IsServerSuitableIgnoreExceptions(text, requireGCs, credential, out writableNC, out siteName, out empty))
                {
                    if (!requireGCs)
                    {
                        Globals.LogEvent(DirectoryEventLogConstants.Tuple_DSC_EVENT_GET_DC_FROM_DOMAIN, domainOrForestFqdn, new object[]
                        {
                            domainOrForestFqdn,
                            text
                        });
                    }
                    return(new ADServerInfo(text, domainOrForestFqdn, requireGCs ? 3268 : 389, writableNC, 100, AuthType.Kerberos, true)
                    {
                        SiteName = siteName
                    });
                }
                ExTraceGlobals.TopologyProviderTracer.TraceError(0L, "{0} {1} {2} was found not suitable. Will try to find another {1} in the forest\\domain. Error message: {3}", new object[]
                {
                    domainOrForestFqdn,
                    requireGCs ? "Global Catalog" : "Domain Controller",
                    text,
                    empty
                });
                value = DirectoryStrings.AppendLocalizedStrings(value, empty);
            }
            if (requireGCs)
            {
                Globals.LogEvent(DirectoryEventLogConstants.Tuple_DSC_EVENT_ALL_GC_DOWN, string.Empty, new object[]
                {
                    domainOrForestFqdn,
                    string.Empty
                });
                throw new NoSuitableServerFoundException(DirectoryStrings.ErrorNoSuitableGCInForest(domainOrForestFqdn, value));
            }
            Globals.LogEvent(DirectoryEventLogConstants.Tuple_DSC_EVENT_GET_DC_FROM_DOMAIN_FAILED, domainOrForestFqdn, new object[]
            {
                domainOrForestFqdn
            });
            throw new NoSuitableServerFoundException(DirectoryStrings.ErrorNoSuitableDCInDomain(domainOrForestFqdn, value));
        }
Esempio n. 4
0
            // Token: 0x06000514 RID: 1300 RVA: 0x0001CA44 File Offset: 0x0001AC44
            private static bool IsDcInLocalSite(ADServerInfo serverInfo)
            {
                bool flag = false;

                if (!string.IsNullOrEmpty(serverInfo.PartitionFqdn) && !PartitionId.IsLocalForestPartition(serverInfo.PartitionFqdn))
                {
                    ExTraceGlobals.ServerSettingsProviderTracer.TraceDebug <string, string>((long)serverInfo.GetHashCode(), "Server {0} is in forest {1} which is not local forest, return IsDcInLocalSite == false", serverInfo.Fqdn, serverInfo.PartitionFqdn);
                    return(false);
                }
                if (ADRunspaceServerSettingsProvider.ADServerInfoState.localSiteName == null)
                {
                    try
                    {
                        ADRunspaceServerSettingsProvider.ADServerInfoState.localSiteName = NativeHelpers.GetSiteName(false);
                        ExTraceGlobals.ServerSettingsProviderTracer.TraceDebug <string>((long)serverInfo.GetHashCode(), "Setting the local site name to {0}", ADRunspaceServerSettingsProvider.ADServerInfoState.localSiteName);
                    }
                    catch (CannotGetSiteInfoException)
                    {
                        ExTraceGlobals.ServerSettingsProviderTracer.TraceError((long)serverInfo.GetHashCode(), "GetSiteName has thrown a CannotGetSiteInfoException");
                    }
                }
                if (ADRunspaceServerSettingsProvider.ADServerInfoState.localSiteName != null)
                {
                    if (Globals.IsMicrosoftHostedOnly && !string.IsNullOrEmpty(serverInfo.SiteName))
                    {
                        flag = string.Equals(serverInfo.SiteName, ADRunspaceServerSettingsProvider.ADServerInfoState.localSiteName, StringComparison.OrdinalIgnoreCase);
                        ExTraceGlobals.ServerSettingsProviderTracer.TraceDebug((long)serverInfo.GetHashCode(), "local site is: {0}, server {1} is in site {2}, return IsDcInLocalSite == {3}", new object[]
                        {
                            ADRunspaceServerSettingsProvider.ADServerInfoState.localSiteName,
                            serverInfo.Fqdn,
                            serverInfo.SiteName,
                            flag
                        });
                        return(flag);
                    }
                    try
                    {
                        StringCollection dcSiteCoverage = NativeHelpers.GetDcSiteCoverage(serverInfo.Fqdn);
                        flag = dcSiteCoverage.Contains(ADRunspaceServerSettingsProvider.ADServerInfoState.localSiteName);
                        ExTraceGlobals.ServerSettingsProviderTracer.TraceDebug <string, string>((long)serverInfo.GetHashCode(), "Server {0} {1} in the local site", serverInfo.Fqdn, flag ? "is" : "is not");
                    }
                    catch (CannotGetSiteInfoException)
                    {
                        ExTraceGlobals.ServerSettingsProviderTracer.TraceError((long)serverInfo.GetHashCode(), "GetDcSiteCoverage has thrown a CannotGetSiteInfoException");
                    }
                }
                return(flag);
            }
        private ADServerInfo GetDirectoryServer(string partitionFqdn, ADRole role)
        {
            ExTraceGlobals.TopologyProviderTracer.TraceDebug <string, ADRole>((long)this.GetHashCode(), "GetDirectoryServer PartitionFqdn {0}. Role {1}", partitionFqdn, role);
            LocatorFlags locatorFlags = LocatorFlags.ForceRediscovery | LocatorFlags.DirectoryServicesRequired | LocatorFlags.ReturnDnsName;
            string       text         = partitionFqdn;

            if (ADRole.GlobalCatalog == role)
            {
                ADObjectId rootDomainNamingContext = base.GetRootDomainNamingContext(partitionFqdn);
                ADObjectId domainNamingContext     = base.GetDomainNamingContext(partitionFqdn);
                if (!rootDomainNamingContext.DistinguishedName.Equals(domainNamingContext.DistinguishedName, StringComparison.OrdinalIgnoreCase))
                {
                    text = NativeHelpers.CanonicalNameFromDistinguishedName(rootDomainNamingContext.DistinguishedName);
                }
                locatorFlags |= LocatorFlags.GCRequired;
            }
            ExTraceGlobals.TopologyProviderTracer.TraceDebug <string, string, LocatorFlags>((long)this.GetHashCode(), "GetDirectoryServer. Partition Fqdn {0} Parent Domain {1}. Flags {2}", partitionFqdn, text, locatorFlags);
            ADServerInfo         serverInfo           = new ADServerInfo(null, text, (ADRole.GlobalCatalog == role) ? 3268 : 389, null, 100, AuthType.Kerberos, true);
            PooledLdapConnection pooledLdapConnection = null;
            ADServerInfo         adserverInfo         = null;

            try
            {
                pooledLdapConnection = LdapConnectionPool.CreateOneTimeConnection(null, serverInfo, locatorFlags);
                if (!string.IsNullOrEmpty(pooledLdapConnection.SessionOptions.HostName))
                {
                    adserverInfo = pooledLdapConnection.ADServerInfo.CloneWithServerNameResolved(pooledLdapConnection.SessionOptions.HostName);
                }
                ExTraceGlobals.TopologyProviderTracer.TraceDebug <string, string>((long)this.GetHashCode(), "GetDirectoryServer. Partition Fqdn {0}. Server {1}", partitionFqdn, pooledLdapConnection.SessionOptions.HostName ?? string.Empty);
            }
            finally
            {
                if (pooledLdapConnection != null)
                {
                    pooledLdapConnection.ReturnToPool();
                }
            }
            string          text2;
            LocalizedString localizedString;

            if (adserverInfo != null && SuitabilityVerifier.IsServerSuitableIgnoreExceptions(adserverInfo.Fqdn, ADRole.GlobalCatalog == role, null, out text2, out localizedString))
            {
                return(adserverInfo);
            }
            return(LdapTopologyProvider.FindDirectoryServerForForestOrDomain(text, null, ADRole.GlobalCatalog == role));
        }
Esempio n. 6
0
        public static string CanonicalNameFromDistinguishedName(string distinguishedName)
        {
            string text = null;
            uint   err  = 0U;

            if (!NativeHelpers.TryDsCrackNames(distinguishedName, ExtendedNameFormat.NameFullyQualifiedDN, ExtendedNameFormat.NameCanonical, null, out text, out err))
            {
                throw new NameConversionException(DirectoryStrings.ErrorConversionFailedWithError(distinguishedName, err));
            }
            if (text.IndexOf('/') == text.Length - 1)
            {
                text = text.TrimEnd(new char[]
                {
                    '/'
                });
            }
            return(text);
        }
 internal override bool InDomain()
 {
     if (this.inDomain == null)
     {
         if (ADSession.IsBoundToAdam)
         {
             this.inDomain = new bool?(false);
         }
         try
         {
             NativeHelpers.GetDomainName();
             this.inDomain = new bool?(true);
         }
         catch (CannotGetDomainInfoException)
         {
             this.inDomain = new bool?(false);
         }
     }
     return(this.inDomain.Value);
 }
Esempio n. 8
0
        public static string DistinguishedNameFromCanonicalName(string canonicalName, ADServerSettings serverSettings)
        {
            if (canonicalName.IndexOf('/') != canonicalName.LastIndexOf('/') && !canonicalName.EndsWith("\\/"))
            {
                canonicalName = canonicalName.TrimEnd(new char[]
                {
                    '/'
                });
            }
            if (!canonicalName.Contains("/"))
            {
                canonicalName += "/";
            }
            string text  = null;
            uint   err   = 0U;
            bool   flag  = NativeHelpers.TryDsCrackNames(canonicalName, ExtendedNameFormat.NameCanonical, ExtendedNameFormat.NameFullyQualifiedDN, serverSettings, out text, out err);
            bool   flag2 = ADObjectId.IsValidDistinguishedName(text);

            if (!flag || !flag2)
            {
                throw new NameConversionException(DirectoryStrings.ErrorConversionFailedWithError(canonicalName, err));
            }
            return(text);
        }
Esempio n. 9
0
 public static string DistinguishedNameFromCanonicalName(string canonicalName)
 {
     return(NativeHelpers.DistinguishedNameFromCanonicalName(canonicalName, null));
 }
Esempio n. 10
0
 public static bool LocalMachineRoleIsDomainController()
 {
     NativeMethods.InteropDsRolePrimaryDomainInfoBasic primaryDomainInformation = NativeHelpers.GetPrimaryDomainInformation(null);
     return(primaryDomainInformation.machineRole == NativeMethods.MachineRole.PrimaryDomainController || primaryDomainInformation.machineRole == NativeMethods.MachineRole.BackupDomainController);
 }
Esempio n. 11
0
        private StringCollection FindAllDirectoryServers(string forestFqdn, bool requireGCs, string siteName = null)
        {
            Hashtable hashtable = new Hashtable();
            SafeDsGetDcContextHandle safeDsGetDcContextHandle = null;
            SafeDnsHostNameHandle    safeDnsHostNameHandle    = null;
            IntPtr zero  = IntPtr.Zero;
            IntPtr zero2 = IntPtr.Zero;

            NativeMethods.DsGetDcOpenFlags dsGetDcOpenFlags = NativeMethods.DsGetDcOpenFlags.ForceRediscovery;
            if (requireGCs)
            {
                dsGetDcOpenFlags |= NativeMethods.DsGetDcOpenFlags.GCRequired;
            }
            try
            {
                int num = NativeMethods.DsGetDcOpen(forestFqdn, 0, siteName, IntPtr.Zero, null, (int)dsGetDcOpenFlags, out safeDsGetDcContextHandle);
                ExTraceGlobals.FaultInjectionTracer.TraceTest(3854970173U);
                if (num != 0)
                {
                    throw new ADTransientException(DirectoryStrings.ExceptionNativeErrorWhenLookingForServersInDomain(num, forestFqdn, NativeHelpers.GetErrorMessageFromNativeError(num)));
                }
                num = NativeMethods.DsGetDcNext(safeDsGetDcContextHandle, ref zero, out zero2, out safeDnsHostNameHandle);
                if (num != 0 && num != 1101 && num != 9003 && num != 259)
                {
                    throw new ADTransientException(DirectoryStrings.ExceptionNativeErrorWhenLookingForServersInDomain(num, forestFqdn, NativeHelpers.GetErrorMessageFromNativeError(num)));
                }
                while (num != 259)
                {
                    if (num != 1101 && num != 9003)
                    {
                        try
                        {
                            string text = Marshal.PtrToStringUni(safeDnsHostNameHandle.DangerousGetHandle());
                            string key  = text.ToLower(CultureInfo.InvariantCulture);
                            if (!hashtable.ContainsKey(key))
                            {
                                hashtable.Add(key, null);
                            }
                        }
                        finally
                        {
                            if (safeDnsHostNameHandle != null)
                            {
                                safeDnsHostNameHandle.Dispose();
                                safeDnsHostNameHandle = null;
                            }
                        }
                    }
                    num = NativeMethods.DsGetDcNext(safeDsGetDcContextHandle, ref zero, out zero2, out safeDnsHostNameHandle);
                    if (num != 0 && num != 1101 && num != 9003 && num != 259)
                    {
                        throw new ADTransientException(DirectoryStrings.ExceptionNativeErrorWhenLookingForServersInDomain(num, forestFqdn, NativeHelpers.GetErrorMessageFromNativeError(num)));
                    }
                }
            }
            finally
            {
                if (safeDsGetDcContextHandle != null)
                {
                    safeDsGetDcContextHandle.Dispose();
                }
                if (safeDnsHostNameHandle != null)
                {
                    safeDnsHostNameHandle.Dispose();
                }
            }
            StringCollection stringCollection = new StringCollection();

            foreach (object obj in hashtable.Keys)
            {
                string value = (string)obj;
                stringCollection.Add(value);
            }
            return(stringCollection);
        }
Esempio n. 12
0
 public string GetForestNameHookable()
 {
     NativeMethods.InteropDsRolePrimaryDomainInfoBasic primaryDomainInformation = NativeHelpers.GetPrimaryDomainInformation(null);
     return(primaryDomainInformation.domainForestName);
 }
Esempio n. 13
0
        private static bool TryDsCrackNames(string input, ExtendedNameFormat formatOffered, ExtendedNameFormat formatDesired, ADServerSettings serverSettings, out string result, out uint err)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            if (formatOffered != ExtendedNameFormat.NameCanonical && formatOffered != ExtendedNameFormat.NameFullyQualifiedDN)
            {
                throw new ArgumentException(DirectoryStrings.ExArgumentException("formatOffered", formatOffered.ToString()), "formatOffered");
            }
            if (formatDesired != ExtendedNameFormat.NameCanonical && formatDesired != ExtendedNameFormat.NameFullyQualifiedDN)
            {
                throw new ArgumentException(DirectoryStrings.ExArgumentException("formatDesired", formatDesired.ToString()), "formatDesired");
            }
            result = null;
            err    = 0U;
            SafeDsNameResultHandle safeDsNameResultHandle = null;
            SafeDsBindHandle       safeDsBindHandle       = null;

            NativeMethods.DsNameFlags dsNameFlags = NativeMethods.DsNameFlags.SyntacticalOnly;
            bool result2;

            try
            {
                if (formatOffered == ExtendedNameFormat.NameCanonical)
                {
                    string[] array = input.Split(new char[]
                    {
                        '/'
                    });
                    if (array.Length == 0)
                    {
                        return(false);
                    }
                    if (array.Length > 1 && array[1].Length > 0)
                    {
                        dsNameFlags = NativeMethods.DsNameFlags.NoFlags;
                        string text = array[0];
                        ExTraceGlobals.ADTopologyTracer.TraceDebug <string>(0L, "Calling DsBind for domain {0}", text ?? "<null>");
                        if (text.Length == 0)
                        {
                            return(false);
                        }
                        string domainControllerName = null;
                        string distinguishedName    = NativeHelpers.DistinguishedNameFromCanonicalName(text);
                        if (serverSettings == null)
                        {
                            serverSettings = ADSessionSettings.ExternalServerSettings;
                        }
                        if (serverSettings != null)
                        {
                            domainControllerName = serverSettings.GetPreferredDC(new ADObjectId(distinguishedName));
                        }
                        err = NativeMethods.DsBind(domainControllerName, text, out safeDsBindHandle);
                        if (err != 0U)
                        {
                            return(false);
                        }
                    }
                }
                if (safeDsBindHandle == null)
                {
                    safeDsBindHandle = new SafeDsBindHandle();
                }
                ExTraceGlobals.ADTopologyTracer.TraceDebug(0L, "Calling DsCrackNames with input={0}, formatOffered={1}, formatDesired={2}, flags={3}", new object[]
                {
                    input,
                    formatOffered,
                    formatDesired,
                    dsNameFlags
                });
                err = NativeMethods.DsCrackNames(safeDsBindHandle, dsNameFlags, formatOffered, formatDesired, 1U, new string[]
                {
                    input
                }, out safeDsNameResultHandle);
                if (err != 0U)
                {
                    result2 = false;
                }
                else
                {
                    NativeMethods.DsNameResult dsNameResult = new NativeMethods.DsNameResult();
                    Marshal.PtrToStructure(safeDsNameResultHandle.DangerousGetHandle(), dsNameResult);
                    uint cItems = dsNameResult.cItems;
                    if (cItems < 1U)
                    {
                        result2 = false;
                    }
                    else
                    {
                        NativeMethods.DsNameResultItem dsNameResultItem = new NativeMethods.DsNameResultItem();
                        Marshal.PtrToStructure(dsNameResult.rItems, dsNameResultItem);
                        if (dsNameResultItem.status != 0)
                        {
                            err     = (uint)dsNameResultItem.status;
                            result2 = false;
                        }
                        else if (dsNameResultItem.name == null)
                        {
                            result2 = false;
                        }
                        else
                        {
                            result  = dsNameResultItem.name;
                            result2 = true;
                        }
                    }
                }
            }
            finally
            {
                if (safeDsBindHandle != null)
                {
                    safeDsBindHandle.Dispose();
                }
                if (safeDsNameResultHandle != null)
                {
                    safeDsNameResultHandle.Dispose();
                }
            }
            return(result2);
        }
Esempio n. 14
0
 public static string GetLocalComputerFqdn(bool throwOnException)
 {
     return(NativeHelpers.GetComputerNameHelper(NativeMethods.ComputerNameFormat.ComputerNameDnsFullyQualified));
 }
Esempio n. 15
0
 public static string GetDomainForestNameFromServer(string serverFqdn)
 {
     if (string.IsNullOrEmpty(serverFqdn))
     {
         throw new ArgumentNullException("serverFqdn");
     }
     NativeMethods.InteropDsRolePrimaryDomainInfoBasic primaryDomainInformation = NativeHelpers.GetPrimaryDomainInformation(serverFqdn);
     return(primaryDomainInformation.domainForestName);
 }
        public override ADServerInfo GetServerFromDomainDN(string distinguishedName, NetworkCredential credential)
        {
            ExTraceGlobals.TopologyProviderTracer.TraceDebug <string, string>((long)this.GetHashCode(), "Need server from domain {0}. Credentials {1} NULL", distinguishedName, (credential == null) ? "are" : "are NOT");
            ADServerInfo remoteServerFromDomainFqdn = this.GetRemoteServerFromDomainFqdn(NativeHelpers.CanonicalNameFromDistinguishedName(distinguishedName), credential);

            ExTraceGlobals.TopologyProviderTracer.TraceDebug <string>((long)this.GetHashCode(), "GetServerFromDomainDN returning {0}", remoteServerFromDomainFqdn.FqdnPlusPort);
            return(remoteServerFromDomainFqdn);
        }
 internal LdapTopologyProvider()
 {
     this.localServerFqdn = NativeHelpers.GetLocalComputerFqdn(false);
     this.topologies      = new ConcurrentDictionary <string, LdapTopologyProvider.MiniTopology>(StringComparer.OrdinalIgnoreCase);
 }