internal static AdamInstance FindAliveAdamInstance(string configSetName, DirectoryContext context, ArrayList adamInstanceNames)
        {
            bool         flag     = false;
            AdamInstance instance = null;
            DateTime     utcNow   = DateTime.UtcNow;

            foreach (string str in adamInstanceNames)
            {
                DirectoryContext      context2             = Utils.GetNewDirectoryContext(str, DirectoryContextType.DirectoryServer, context);
                DirectoryEntryManager directoryEntryMgr    = new DirectoryEntryManager(context2);
                DirectoryEntry        cachedDirectoryEntry = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                try
                {
                    cachedDirectoryEntry.Bind(true);
                    instance = new AdamInstance(context2, str, directoryEntryMgr, true);
                    flag     = true;
                }
                catch (COMException exception)
                {
                    if (((exception.ErrorCode != -2147016646) && (exception.ErrorCode != -2147016690)) && ((exception.ErrorCode != -2147016689) && (exception.ErrorCode != -2147023436)))
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
                    }
                    if (DateTime.UtcNow.Subtract(utcNow) > locationTimeout)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { (configSetName != null) ? configSetName : context.Name }), typeof(AdamInstance), null);
                    }
                }
                if (flag)
                {
                    return(instance);
                }
            }
            throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { (configSetName != null) ? configSetName : context.Name }), typeof(AdamInstance), null);
        }
        public static ApplicationPartition GetApplicationPartition(DirectoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (context.ContextType != DirectoryContextType.ApplicationPartition)
            {
                throw new ArgumentException(Res.GetString("TargetShouldBeAppNCDnsName"), "context");
            }
            if (!context.isNdnc())
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("NDNCNotFound"), typeof(ApplicationPartition), context.Name);
            }
            context = new DirectoryContext(context);
            string dNFromDnsName = Utils.GetDNFromDnsName(context.Name);
            DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);

            try
            {
                directoryEntryMgr.GetCachedDirectoryEntry(dNFromDnsName).Bind(true);
            }
            catch (COMException exception)
            {
                if (exception.ErrorCode == -2147016646)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("NDNCNotFound"), typeof(ApplicationPartition), context.Name);
                }
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
            }
            return(new ApplicationPartition(context, dNFromDnsName, context.Name, ApplicationPartitionType.ADApplicationPartition, directoryEntryMgr));
        }
        internal static AdamInstance FindAnyAdamInstance(DirectoryContext context)
        {
            if (context.ContextType != DirectoryContextType.ConfigurationSet)
            {
                DirectoryEntryManager directoryEntryMgr    = new DirectoryEntryManager(context);
                DirectoryEntry        cachedDirectoryEntry = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if (!Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
                {
                    directoryEntryMgr.RemoveIfExists(directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDSE));
                    throw new ArgumentException(Res.GetString("TargetShouldBeServerORConfigSet"), "context");
                }
                return(new AdamInstance(context, (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.DnsHostName), directoryEntryMgr));
            }
            DirectoryEntry searchRootEntry   = GetSearchRootEntry(Forest.GetCurrentForest());
            ArrayList      adamInstanceNames = new ArrayList();

            try
            {
                string        text1   = (string)searchRootEntry.Properties["distinguishedName"].Value;
                StringBuilder builder = new StringBuilder(15);
                builder.Append("(&(");
                builder.Append(PropertyManager.ObjectCategory);
                builder.Append("=serviceConnectionPoint)");
                builder.Append("(");
                builder.Append(PropertyManager.Keywords);
                builder.Append("=1.2.840.113556.1.4.1851)(");
                builder.Append(PropertyManager.Keywords);
                builder.Append("=");
                builder.Append(Utils.GetEscapedFilterValue(context.Name));
                builder.Append("))");
                string     filter           = builder.ToString();
                string[]   propertiesToLoad = new string[] { PropertyManager.ServiceBindingInformation };
                ADSearcher searcher         = new ADSearcher(searchRootEntry, filter, propertiesToLoad, SearchScope.Subtree, false, false);
                using (SearchResultCollection results = searcher.FindAll())
                {
                    foreach (SearchResult result in results)
                    {
                        string strB = "ldap://";
                        foreach (string str4 in result.Properties[PropertyManager.ServiceBindingInformation])
                        {
                            if ((str4.Length > strB.Length) && (string.Compare(str4.Substring(0, strB.Length), strB, StringComparison.OrdinalIgnoreCase) == 0))
                            {
                                adamInstanceNames.Add(str4.Substring(strB.Length));
                            }
                        }
                    }
                }
            }
            catch (COMException exception)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
            }
            finally
            {
                searchRootEntry.Dispose();
            }
            return(FindAliveAdamInstance(null, context, adamInstanceNames));
        }
Ejemplo n.º 4
0
        public static AdamInstance GetAdamInstance(DirectoryContext context)
        {
            DirectoryEntryManager directoryEntryMgr = null;
            string dnsHostName = null;

            // check that the context is not null
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // contexttype should be DirectoryServer
            if (context.ContextType != DirectoryContextType.DirectoryServer)
            {
                throw new ArgumentException(SR.TargetShouldBeADAMServer, nameof(context));
            }

            // target must be a server
            if ((!context.isServer()))
            {
                throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.AINotFound, context.Name), typeof(AdamInstance), context.Name);
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            // bind to the given adam instance
            try
            {
                directoryEntryMgr = new DirectoryEntryManager(context);
                DirectoryEntry rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);

                // This will ensure that we are talking to ADAM instance only
                if (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryApplicationMode))
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.AINotFound, context.Name), typeof(AdamInstance), context.Name);
                }
                dnsHostName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.DnsHostName);
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked ((int)0x8007203a))
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.AINotFound, context.Name), typeof(AdamInstance), context.Name);
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }

            return(new AdamInstance(context, dnsHostName, directoryEntryMgr));
        }
        public static ConfigurationSet GetConfigurationSet(DirectoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if ((context.ContextType != DirectoryContextType.ConfigurationSet) && (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(Res.GetString("TargetShouldBeServerORConfigSet"), "context");
            }
            if (!context.isServer() && !context.isADAMConfigSet())
            {
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ConfigurationSet), context.Name);
                }
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", new object[] { context.Name }), typeof(ConfigurationSet), null);
            }
            context = new DirectoryContext(context);
            DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
            DirectoryEntry        rootDSE           = null;
            string configSetName = null;

            try
            {
                rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if (context.isServer() && !Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryApplicationMode))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", new object[] { context.Name }), typeof(ConfigurationSet), null);
                }
                configSetName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.ConfigurationNamingContext);
            }
            catch (COMException exception)
            {
                if (exception.ErrorCode != -2147016646)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
                }
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ConfigurationSet), context.Name);
                }
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", new object[] { context.Name }), typeof(ConfigurationSet), null);
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ConfigurationSet), context.Name);
                }
                throw;
            }
            return(new ConfigurationSet(context, configSetName, directoryEntryMgr));
        }
Ejemplo n.º 6
0
 public ReplicationSecurityLevel GetSecurityLevel()
 {
     CheckIfDisposed();
     if (_cachedSecurityLevel == (ReplicationSecurityLevel)(-1))
     {
         DirectoryEntry configEntry = _directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.ConfigurationNamingContext);
         _cachedSecurityLevel = (ReplicationSecurityLevel)((int)PropertyManager.GetPropertyValue(_context, configEntry, PropertyManager.MsDSReplAuthenticationMode));
     }
     return(_cachedSecurityLevel);
 }
Ejemplo n.º 7
0
        public static Forest GetForest(DirectoryContext context)
        {
            DirectoryEntryManager directoryEntryMgr = null;
            DirectoryEntry        rootDSE           = null;
            string distinguishedName = null;

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if ((context.ContextType != DirectoryContextType.Forest) && (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(Res.GetString("TargetShouldBeServerORForest"), "context");
            }
            if ((context.Name == null) && !context.isRootDomain())
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ContextNotAssociatedWithDomain"), typeof(Forest), null);
            }
            if (((context.Name != null) && !context.isRootDomain()) && !context.isServer())
            {
                if (context.ContextType == DirectoryContextType.Forest)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(Forest), context.Name);
                }
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFound", new object[] { context.Name }), typeof(Forest), null);
            }
            context           = new DirectoryContext(context);
            directoryEntryMgr = new DirectoryEntryManager(context);
            try
            {
                rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if (context.isServer() && !Utils.CheckCapability(rootDSE, Capability.ActiveDirectory))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFound", new object[] { context.Name }), typeof(Forest), null);
                }
                distinguishedName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.RootDomainNamingContext);
            }
            catch (COMException exception)
            {
                if (exception.ErrorCode != -2147016646)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
                }
                if (context.ContextType == DirectoryContextType.Forest)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(Forest), context.Name);
                }
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFound", new object[] { context.Name }), typeof(Forest), null);
            }
            return(new Forest(context, Utils.GetDnsNameFromDN(distinguishedName), directoryEntryMgr));
        }
Ejemplo n.º 8
0
        //
        // The input to this function is a list of adam instance names in the form server:port
        // This function tries to bind to each of the instances in this list sequentially until one of the following occurs:
        // 1.  An ADAM instance responds to an ldap_bind - we return an ADAMInstance object for that adam instance
        // 2.  We exceed the timeout duration - we return an ActiveDirectoryObjectNotFoundException
        //
        internal static AdamInstance FindAliveAdamInstance(string configSetName, DirectoryContext context, ArrayList adamInstanceNames)
        {
            bool         foundAliveADAMInstance = false;
            AdamInstance adamInstance           = null;

            // record the start time so that we can determine if the timeout duration has been exceeded or not
            DateTime startTime = DateTime.UtcNow;

            // loop through each adam instance and try to bind to the rootdse
            foreach (string adamInstanceName in adamInstanceNames)
            {
                DirectoryContext      adamInstContext   = Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, context);
                DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(adamInstContext);
                DirectoryEntry        tempRootEntry     = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);

                try
                {
                    tempRootEntry.Bind(true);
                    adamInstance           = new AdamInstance(adamInstContext, adamInstanceName, directoryEntryMgr, true /* nameIncludesPort */);
                    foundAliveADAMInstance = true;
                }
                catch (COMException e)
                {
                    // if this is server down /server busy / server unavailable / timeout  exception we should just eat this up and try the next one
                    if ((e.ErrorCode == unchecked ((int)0x8007203a)) ||
                        (e.ErrorCode == unchecked ((int)0x8007200e)) ||
                        (e.ErrorCode == unchecked ((int)0x8007200f)) ||
                        (e.ErrorCode == unchecked ((int)0x800705b4)))
                    {
                        // if we are passed the timeout period, we should throw, else do nothing
                        if (DateTime.UtcNow.Subtract(startTime) > s_locationTimeout)
                        {
                            throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.ADAMInstanceNotFoundInConfigSet, (configSetName != null) ? configSetName : context.Name), typeof(AdamInstance), null);
                        }
                    }
                    else
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                    }
                }

                if (foundAliveADAMInstance)
                {
                    return(adamInstance);
                }
            }

            // if we reach here, we haven't found an adam instance
            throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.ADAMInstanceNotFoundInConfigSet, (configSetName != null) ? configSetName : context.Name), typeof(AdamInstance), null);
        }
        public static AdamInstance GetAdamInstance(DirectoryContext context)
        {
            DirectoryEntryManager directoryEntryMgr = null;
            string adamHostName = null;

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (context.ContextType != DirectoryContextType.DirectoryServer)
            {
                throw new ArgumentException(Res.GetString("TargetShouldBeADAMServer"), "context");
            }
            if (!context.isServer())
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", new object[] { context.Name }), typeof(AdamInstance), context.Name);
            }
            context = new DirectoryContext(context);
            try
            {
                directoryEntryMgr = new DirectoryEntryManager(context);
                DirectoryEntry cachedDirectoryEntry = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if (!Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", new object[] { context.Name }), typeof(AdamInstance), context.Name);
                }
                adamHostName = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.DnsHostName);
            }
            catch (COMException exception)
            {
                if (exception.ErrorCode == -2147016646)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", new object[] { context.Name }), typeof(AdamInstance), context.Name);
                }
                throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(context, exception);
            }
            return(new AdamInstance(context, adamHostName, directoryEntryMgr));
        }
Ejemplo n.º 10
0
        internal static AdamInstance FindAnyAdamInstance(DirectoryContext context)
        {
            if (context.ContextType != DirectoryContextType.ConfigurationSet)
            {
                // assuming it's an ADAM Instance 
                // check that it is an ADAM server only (not AD)
                DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
                DirectoryEntry rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);

                if (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryApplicationMode))
                {
                    directoryEntryMgr.RemoveIfExists(directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDSE));
                    throw new ArgumentException(Res.GetString(Res.TargetShouldBeServerORConfigSet), "context");
                }

                string dnsHostName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.DnsHostName);

                return new AdamInstance(context, dnsHostName, directoryEntryMgr);
            }

            // Now this is the case where context is a Config Set
            // Here we need to search for the service connection points in the forest
            // (if the forest object was created by specifying the server, we stick to that, else search in a GC)
            DirectoryEntry rootEntry = GetSearchRootEntry(Forest.GetCurrentForest());
            ArrayList adamInstanceNames = new ArrayList();

            try
            {
                string entryName = (string)rootEntry.Properties["distinguishedName"].Value;

                // Search for computer "serviceConnectionObjects" where the keywords attribute 
                // contains the specified keyword
                // set up the searcher object

                // build the filter
                StringBuilder str = new StringBuilder(15);
                str.Append("(&(");
                str.Append(PropertyManager.ObjectCategory);
                str.Append("=serviceConnectionPoint)");
                str.Append("(");
                str.Append(PropertyManager.Keywords);
                str.Append("=1.2.840.113556.1.4.1851)(");
                str.Append(PropertyManager.Keywords);
                str.Append("=");
                str.Append(Utils.GetEscapedFilterValue(context.Name)); // target = config set name 
                str.Append("))");

                string filter = str.ToString();
                string[] propertiesToLoad = new string[1];

                propertiesToLoad[0] = PropertyManager.ServiceBindingInformation;

                ADSearcher searcher = new ADSearcher(rootEntry, filter, propertiesToLoad, SearchScope.Subtree, false /*not paged search*/, false /*no cached results*/);
                SearchResultCollection resCol = searcher.FindAll();

                try
                {
                    foreach (SearchResult res in resCol)
                    {
                        // the binding info contains two values
                        // "ldap://hostname:ldapport"
                        // and "ldaps://hostname:sslport"
                        // we need the "hostname:ldapport" value
                        string prefix = "ldap://";

                        foreach (string bindingInfo in res.Properties[PropertyManager.ServiceBindingInformation])
                        {
                            if ((bindingInfo.Length > prefix.Length) && (String.Compare(bindingInfo.Substring(0, prefix.Length), prefix, StringComparison.OrdinalIgnoreCase) == 0))
                            {
                                adamInstanceNames.Add(bindingInfo.Substring(prefix.Length));
                            }
                        }
                    }
                }
                finally
                {
                    resCol.Dispose();
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                rootEntry.Dispose();
            }

            //
            // we have all the adam instance names in teh form of server:port from the scp
            // now we need to find one that is alive
            //
            return FindAliveAdamInstance(null, context, adamInstanceNames);
        }
Ejemplo n.º 11
0
        internal static AdamInstance FindAnyAdamInstance(DirectoryContext context)
        {
            if (context.ContextType != DirectoryContextType.ConfigurationSet)
            {
                // assuming it's an ADAM Instance
                // check that it is an ADAM server only (not AD)
                DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
                DirectoryEntry        rootDSE           = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);

                if (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryApplicationMode))
                {
                    directoryEntryMgr.RemoveIfExists(directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDSE));
                    throw new ArgumentException(SR.TargetShouldBeServerORConfigSet, nameof(context));
                }

                string dnsHostName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.DnsHostName);

                return(new AdamInstance(context, dnsHostName, directoryEntryMgr));
            }

            // Now this is the case where context is a Config Set
            // Here we need to search for the service connection points in the forest
            // (if the forest object was created by specifying the server, we stick to that, else search in a GC)
            DirectoryEntry rootEntry         = GetSearchRootEntry(Forest.GetCurrentForest());
            ArrayList      adamInstanceNames = new ArrayList();

            try
            {
                string entryName = (string)rootEntry.Properties["distinguishedName"].Value;

                // Search for computer "serviceConnectionObjects" where the keywords attribute
                // contains the specified keyword
                // set up the searcher object

                // build the filter
                StringBuilder str = new StringBuilder(15);
                str.Append("(&(");
                str.Append(PropertyManager.ObjectCategory);
                str.Append("=serviceConnectionPoint)");
                str.Append("(");
                str.Append(PropertyManager.Keywords);
                str.Append("=1.2.840.113556.1.4.1851)(");
                str.Append(PropertyManager.Keywords);
                str.Append("=");
                str.Append(Utils.GetEscapedFilterValue(context.Name)); // target = config set name
                str.Append("))");

                string   filter           = str.ToString();
                string[] propertiesToLoad = new string[1];

                propertiesToLoad[0] = PropertyManager.ServiceBindingInformation;

                ADSearcher             searcher = new ADSearcher(rootEntry, filter, propertiesToLoad, SearchScope.Subtree, false /*not paged search*/, false /*no cached results*/);
                SearchResultCollection resCol   = searcher.FindAll();

                try
                {
                    foreach (SearchResult res in resCol)
                    {
                        // the binding info contains two values
                        // "ldap://hostname:ldapport"
                        // and "ldaps://hostname:sslport"
                        // we need the "hostname:ldapport" value
                        string prefix = "ldap://";

                        foreach (string bindingInfo in res.Properties[PropertyManager.ServiceBindingInformation])
                        {
                            if ((bindingInfo.Length > prefix.Length) && (string.Equals(bindingInfo.Substring(0, prefix.Length), prefix, StringComparison.OrdinalIgnoreCase)))
                            {
                                adamInstanceNames.Add(bindingInfo.Substring(prefix.Length));
                            }
                        }
                    }
                }
                finally
                {
                    resCol.Dispose();
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                rootEntry.Dispose();
            }

            //
            // we have all the adam instance names in teh form of server:port from the scp
            // now we need to find one that is alive
            //
            return(FindAliveAdamInstance(null, context, adamInstanceNames));
        }
Ejemplo n.º 12
0
        public static DomainController GetDomainController(DirectoryContext context)
        {
            string dcDnsName = null;
            DirectoryEntryManager directoryEntryMgr = null;

            // check that the context argument is not null
            if (context == null)
                throw new ArgumentNullException("context");

            // target should be DC
            if (context.ContextType != DirectoryContextType.DirectoryServer)
            {
                throw new ArgumentException(Res.GetString(Res.TargetShouldBeDC), "context");
            }

            // target should be a server
            if (!(context.isServer()))
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DCNotFound, context.Name), typeof(DomainController), context.Name);
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            try
            {
                // Get dns name of the dc 
                // by binding to root dse and getting the "dnsHostName" attribute
                directoryEntryMgr = new DirectoryEntryManager(context);
                DirectoryEntry rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectory))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DCNotFound, context.Name), typeof(DomainController), context.Name);
                }
                dcDnsName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.DnsHostName);
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked((int)0x8007203a))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DCNotFound, context.Name), typeof(DomainController), context.Name);
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }

            return new DomainController(context, dcDnsName, directoryEntryMgr);
        }
Ejemplo n.º 13
0
        public static Forest GetForest(DirectoryContext context)
        {
            DirectoryEntryManager?directoryEntryMgr = null;
            DirectoryEntry?       rootDSE           = null;
            string?rootDomainNC = null;

            // check that the argument is not null
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // contexttype should be Forest or DirectoryServer
            if ((context.ContextType != DirectoryContextType.Forest) &&
                (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(SR.TargetShouldBeServerORForest, nameof(context));
            }

            if ((context.Name == null) && (!context.isRootDomain()))
            {
                throw new ActiveDirectoryObjectNotFoundException(SR.ContextNotAssociatedWithDomain, typeof(Forest), null);
            }

            if (context.Name != null)
            {
                // the target should be a valid forest name or a server
                if (!((context.isRootDomain()) || (context.isServer())))
                {
                    if (context.ContextType == DirectoryContextType.Forest)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.ForestNotFound, typeof(Forest), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.DCNotFound, context.Name), typeof(Forest), null);
                    }
                }
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            directoryEntryMgr = new DirectoryEntryManager(context);
            // at this point we know that the target is either a
            // valid forest name or a server (may be a bogus server name -- to check bind to rootdse)
            // bind to the rootDSE of the forest specified in the context
            try
            {
                rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if ((context.isServer()) && (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectory)))
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.DCNotFound, context.Name), typeof(Forest), null);
                }
                rootDomainNC = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.RootDomainNamingContext) !;
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked ((int)0x8007203a))
                {
                    if (context.ContextType == DirectoryContextType.Forest)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.ForestNotFound, typeof(Forest), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.DCNotFound, context.Name), typeof(Forest), null);
                    }
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }

            // return forest object

            return(new Forest(context, Utils.GetDnsNameFromDN(rootDomainNC), directoryEntryMgr));
        }
Ejemplo n.º 14
0
		public static ConfigurationSet GetConfigurationSet(DirectoryContext context)
		{
			if (context != null)
			{
				if (context.ContextType == DirectoryContextType.ConfigurationSet || context.ContextType == DirectoryContextType.DirectoryServer)
				{
					if (context.isServer() || context.isADAMConfigSet())
					{
						context = new DirectoryContext(context);
						DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(context);
						string propertyValue = null;
						try
						{
							DirectoryEntry cachedDirectoryEntry = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
							if (!context.isServer() || Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
							{
								propertyValue = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.ConfigurationNamingContext);
							}
							else
							{
								object[] name = new object[1];
								name[0] = context.Name;
								throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", name), typeof(ConfigurationSet), null);
							}
						}
						catch (COMException cOMException1)
						{
							COMException cOMException = cOMException1;
							int errorCode = cOMException.ErrorCode;
							if (errorCode != -2147016646)
							{
								throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
							}
							else
							{
								if (context.ContextType != DirectoryContextType.ConfigurationSet)
								{
									object[] objArray = new object[1];
									objArray[0] = context.Name;
									throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", objArray), typeof(ConfigurationSet), null);
								}
								else
								{
									throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ConfigurationSet), context.Name);
								}
							}
						}
						catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException)
						{
							if (context.ContextType != DirectoryContextType.ConfigurationSet)
							{
								throw;
							}
							else
							{
								throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ConfigurationSet), context.Name);
							}
						}
						return new ConfigurationSet(context, propertyValue, directoryEntryManager);
					}
					else
					{
						if (context.ContextType != DirectoryContextType.ConfigurationSet)
						{
							object[] name1 = new object[1];
							name1[0] = context.Name;
							throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", name1), typeof(ConfigurationSet), null);
						}
						else
						{
							throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ConfigurationSet), context.Name);
						}
					}
				}
				else
				{
					throw new ArgumentException(Res.GetString("TargetShouldBeServerORConfigSet"), "context");
				}
			}
			else
			{
				throw new ArgumentNullException("context");
			}
		}
Ejemplo n.º 15
0
		internal static AdamInstance FindAliveAdamInstance(string configSetName, DirectoryContext context, ArrayList adamInstanceNames)
		{
			AdamInstance adamInstance;
			object name;
			object obj;
			bool flag = false;
			AdamInstance adamInstance1 = null;
			DateTime utcNow = DateTime.UtcNow;
			IEnumerator enumerator = adamInstanceNames.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					string current = (string)enumerator.Current;
					DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(current, DirectoryContextType.DirectoryServer, context);
					DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(newDirectoryContext);
					DirectoryEntry cachedDirectoryEntry = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
					try
					{
						//TODO: REVIEW: URGENT!!: cachedDirectoryEntry.Bind(true);
						adamInstance1 = new AdamInstance(newDirectoryContext, current, directoryEntryManager, true);
						flag = true;
					}
					catch (COMException cOMException1)
					{
						COMException cOMException = cOMException1;
						if (cOMException.ErrorCode == -2147016646 || cOMException.ErrorCode == -2147016690 || cOMException.ErrorCode == -2147016689 || cOMException.ErrorCode == -2147023436)
						{
							DateTime dateTime = DateTime.UtcNow;
							if (dateTime.Subtract(utcNow) > ConfigurationSet.locationTimeout)
							{
								string str = "ADAMInstanceNotFoundInConfigSet";
								object[] objArray = new object[1];
								object[] objArray1 = objArray;
								int num = 0;
								if (configSetName != null)
								{
									obj = configSetName;
								}
								else
								{
									obj = context.Name;
								}
								objArray1[num] = obj;
								throw new ActiveDirectoryObjectNotFoundException(Res.GetString(str, objArray), typeof(AdamInstance), null);
							}
						}
						else
						{
							throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
						}
					}
					if (!flag)
					{
						continue;
					}
					adamInstance = adamInstance1;
					return adamInstance;
				}
				string str1 = "ADAMInstanceNotFoundInConfigSet";
				object[] objArray2 = new object[1];
				object[] objArray3 = objArray2;
				int num1 = 0;
				if (configSetName != null)
				{
					name = configSetName;
				}
				else
				{
					name = context.Name;
				}
				objArray3[num1] = name;
				throw new ActiveDirectoryObjectNotFoundException(Res.GetString(str1, objArray2), typeof(AdamInstance), null);
			}
			finally
			{
				IDisposable disposable = enumerator as IDisposable;
				if (disposable != null)
				{
					disposable.Dispose();
				}
			}
			return adamInstance;
		}
Ejemplo n.º 16
0
        public static ApplicationPartition GetApplicationPartition(DirectoryContext context)
        {
            // validate the context
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // contexttype should be ApplicationPartiton
            if (context.ContextType != DirectoryContextType.ApplicationPartition)
            {
                throw new ArgumentException(Res.GetString(Res.TargetShouldBeAppNCDnsName), "context");
            }

            // target must be ndnc dns name
            if (!context.isNdnc())
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.NDNCNotFound), typeof(ApplicationPartition), context.Name);
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            // bind to the application partition head (this will verify credentials)
            string distinguishedName = Utils.GetDNFromDnsName(context.Name);
            DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
            DirectoryEntry appNCHead = null;

            try
            {
                appNCHead = directoryEntryMgr.GetCachedDirectoryEntry(distinguishedName);
                // need to force the bind
                appNCHead.Bind(true);
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked((int)0x8007203a))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.NDNCNotFound), typeof(ApplicationPartition), context.Name);
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }

            return new ApplicationPartition(context, distinguishedName, context.Name, ApplicationPartitionType.ADApplicationPartition, directoryEntryMgr);
        }
 internal static AdamInstance FindAliveAdamInstance(string configSetName, DirectoryContext context, ArrayList adamInstanceNames)
 {
     bool flag = false;
     AdamInstance instance = null;
     DateTime utcNow = DateTime.UtcNow;
     foreach (string str in adamInstanceNames)
     {
         DirectoryContext context2 = Utils.GetNewDirectoryContext(str, DirectoryContextType.DirectoryServer, context);
         DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context2);
         DirectoryEntry cachedDirectoryEntry = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
         try
         {
             cachedDirectoryEntry.Bind(true);
             instance = new AdamInstance(context2, str, directoryEntryMgr, true);
             flag = true;
         }
         catch (COMException exception)
         {
             if (((exception.ErrorCode != -2147016646) && (exception.ErrorCode != -2147016690)) && ((exception.ErrorCode != -2147016689) && (exception.ErrorCode != -2147023436)))
             {
                 throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
             }
             if (DateTime.UtcNow.Subtract(utcNow) > locationTimeout)
             {
                 throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { (configSetName != null) ? configSetName : context.Name }), typeof(AdamInstance), null);
             }
         }
         if (flag)
         {
             return instance;
         }
     }
     throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { (configSetName != null) ? configSetName : context.Name }), typeof(AdamInstance), null);
 }
 public static AdamInstance GetAdamInstance(DirectoryContext context)
 {
     DirectoryEntryManager directoryEntryMgr = null;
     string adamHostName = null;
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     if (context.ContextType != DirectoryContextType.DirectoryServer)
     {
         throw new ArgumentException(Res.GetString("TargetShouldBeADAMServer"), "context");
     }
     if (!context.isServer())
     {
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", new object[] { context.Name }), typeof(AdamInstance), context.Name);
     }
     context = new DirectoryContext(context);
     try
     {
         directoryEntryMgr = new DirectoryEntryManager(context);
         DirectoryEntry cachedDirectoryEntry = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
         if (!Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", new object[] { context.Name }), typeof(AdamInstance), context.Name);
         }
         adamHostName = (string) PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.DnsHostName);
     }
     catch (COMException exception)
     {
         if (exception.ErrorCode == -2147016646)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", new object[] { context.Name }), typeof(AdamInstance), context.Name);
         }
         throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(context, exception);
     }
     return new AdamInstance(context, adamHostName, directoryEntryMgr);
 }
Ejemplo n.º 19
0
        public static AdamInstance GetAdamInstance(DirectoryContext context)
        {
            DirectoryEntryManager directoryEntryMgr = null;
            string dnsHostName = null;

            // check that the context is not null
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // contexttype should be DirectoryServer
            if (context.ContextType != DirectoryContextType.DirectoryServer)
            {
                throw new ArgumentException(Res.GetString(Res.TargetShouldBeADAMServer), "context");
            }

            // target must be a server
            if ((!context.isServer()))
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.AINotFound, context.Name), typeof(AdamInstance), context.Name);
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            // bind to the given adam instance
            try
            {
                directoryEntryMgr = new DirectoryEntryManager(context);
                DirectoryEntry rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);

                // This will ensure that we are talking to ADAM instance only
                if (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryApplicationMode))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.AINotFound, context.Name), typeof(AdamInstance), context.Name);
                }
                dnsHostName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.DnsHostName);
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked((int)0x8007203a))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.AINotFound, context.Name), typeof(AdamInstance), context.Name);
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }

            return new AdamInstance(context, dnsHostName, directoryEntryMgr);
        }
Ejemplo n.º 20
0
        public static ConfigurationSet GetConfigurationSet(DirectoryContext context)
        {
            // check that the argument is not null
            if (context == null)
                throw new ArgumentNullException("context");

            // target should ConfigurationSet or DirectoryServer
            if ((context.ContextType != DirectoryContextType.ConfigurationSet) &&
                (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(Res.GetString(Res.TargetShouldBeServerORConfigSet), "context");
            }

            // target should be an adam config set or server
            if (((!context.isServer()) && (!context.isADAMConfigSet())))
            {
                // the target should be a server or an ADAM Config Set
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ConfigSetNotFound), typeof(ConfigurationSet), context.Name);
                }
                else
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.AINotFound, context.Name), typeof(ConfigurationSet), null);
                }
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            //
            // bind to rootdse of an adam instance (if target is already a server, verify that it is an adam instance)
            //
            DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
            DirectoryEntry rootDSE = null;
            string configSetName = null;

            try
            {
                rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if ((context.isServer()) && (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryApplicationMode)))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.AINotFound, context.Name), typeof(ConfigurationSet), null);
                }

                configSetName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.ConfigurationNamingContext);
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked((int)0x8007203a))
                {
                    if (context.ContextType == DirectoryContextType.ConfigurationSet)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ConfigSetNotFound), typeof(ConfigurationSet), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.AINotFound, context.Name), typeof(ConfigurationSet), null);
                    }
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    // this is the case when we could not find an ADAM instance in that config set
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ConfigSetNotFound), typeof(ConfigurationSet), context.Name);
                }
                else
                    throw;
            }

            // return config set object
            return new ConfigurationSet(context, configSetName, directoryEntryMgr);
        }
Ejemplo n.º 21
0
        //
        // The input to this function is a list of adam instance names in the form server:port
        // This function tries to bind to each of the instances in this list sequentially until one of the following occurs:
        // 1.  An ADAM instance responds to an ldap_bind - we return an ADAMInstance object for that adam instance
        // 2.  We exceed the timeout duration - we return an ActiveDirectoryObjectNotFoundException
        //
        internal static AdamInstance FindAliveAdamInstance(string configSetName, DirectoryContext context, ArrayList adamInstanceNames)
        {
            bool foundAliveADAMInstance = false;
            AdamInstance adamInstance = null;

            // record the start time so that we can determine if the timeout duration has been exceeded or not
            DateTime startTime = DateTime.UtcNow;

            // loop through each adam instance and try to bind to the rootdse
            foreach (string adamInstanceName in adamInstanceNames)
            {
                DirectoryContext adamInstContext = Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, context);
                DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(adamInstContext);
                DirectoryEntry tempRootEntry = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);

                try
                {
                    tempRootEntry.Bind(true);
                    adamInstance = new AdamInstance(adamInstContext, adamInstanceName, directoryEntryMgr, true /* nameIncludesPort */);
                    foundAliveADAMInstance = true;
                }
                catch (COMException e)
                {
                    // if this is server down /server busy / server unavailable / timeout  exception we should just eat this up and try the next one
                    if ((e.ErrorCode == unchecked((int)0x8007203a)) ||
                        (e.ErrorCode == unchecked((int)0x8007200e)) ||
                        (e.ErrorCode == unchecked((int)0x8007200f)) ||
                        (e.ErrorCode == unchecked((int)0x800705b4)))
                    {
                        // if we are passed the timeout period, we should throw, else do nothing
                        if (DateTime.UtcNow.Subtract(startTime) > s_locationTimeout)
                            throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ADAMInstanceNotFoundInConfigSet, (configSetName != null) ? configSetName : context.Name), typeof(AdamInstance), null);
                    }
                    else
                        throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }

                if (foundAliveADAMInstance)
                {
                    return adamInstance;
                }
            }

            // if we reach here, we haven't found an adam instance
            throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ADAMInstanceNotFoundInConfigSet, (configSetName != null) ? configSetName : context.Name), typeof(AdamInstance), null);
        }
Ejemplo n.º 22
0
        public static AdamInstance GetAdamInstance(DirectoryContext context)
        {
            DirectoryEntryManager directoryEntryManager = null;
            string propertyValue = null;

            if (context != null)
            {
                if (context.ContextType == DirectoryContextType.DirectoryServer)
                {
                    if (context.isServer())
                    {
                        context = new DirectoryContext(context);
                        try
                        {
                            directoryEntryManager = new DirectoryEntryManager(context);
                            DirectoryEntry cachedDirectoryEntry = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                            if (Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
                            {
                                propertyValue = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.DnsHostName);
                            }
                            else
                            {
                                object[] name = new object[1];
                                name[0] = context.Name;
                                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", name), typeof(AdamInstance), context.Name);
                            }
                        }
                        catch (COMException cOMException1)
                        {
                            COMException cOMException = cOMException1;
                            int          errorCode    = cOMException.ErrorCode;
                            if (errorCode != -2147016646)
                            {
                                throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
                            }
                            else
                            {
                                object[] objArray = new object[1];
                                objArray[0] = context.Name;
                                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", objArray), typeof(AdamInstance), context.Name);
                            }
                        }
                        return(new AdamInstance(context, propertyValue, directoryEntryManager));
                    }
                    else
                    {
                        object[] name1 = new object[1];
                        name1[0] = context.Name;
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", name1), typeof(AdamInstance), context.Name);
                    }
                }
                else
                {
                    throw new ArgumentException(Res.GetString("TargetShouldBeADAMServer"), "context");
                }
            }
            else
            {
                throw new ArgumentNullException("context");
            }
        }
 internal static AdamInstance FindAnyAdamInstance(DirectoryContext context)
 {
     if (context.ContextType != DirectoryContextType.ConfigurationSet)
     {
         DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
         DirectoryEntry cachedDirectoryEntry = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
         if (!Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
         {
             directoryEntryMgr.RemoveIfExists(directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDSE));
             throw new ArgumentException(Res.GetString("TargetShouldBeServerORConfigSet"), "context");
         }
         return new AdamInstance(context, (string) PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.DnsHostName), directoryEntryMgr);
     }
     DirectoryEntry searchRootEntry = GetSearchRootEntry(Forest.GetCurrentForest());
     ArrayList adamInstanceNames = new ArrayList();
     try
     {
         string text1 = (string) searchRootEntry.Properties["distinguishedName"].Value;
         StringBuilder builder = new StringBuilder(15);
         builder.Append("(&(");
         builder.Append(PropertyManager.ObjectCategory);
         builder.Append("=serviceConnectionPoint)");
         builder.Append("(");
         builder.Append(PropertyManager.Keywords);
         builder.Append("=1.2.840.113556.1.4.1851)(");
         builder.Append(PropertyManager.Keywords);
         builder.Append("=");
         builder.Append(Utils.GetEscapedFilterValue(context.Name));
         builder.Append("))");
         string filter = builder.ToString();
         string[] propertiesToLoad = new string[] { PropertyManager.ServiceBindingInformation };
         ADSearcher searcher = new ADSearcher(searchRootEntry, filter, propertiesToLoad, SearchScope.Subtree, false, false);
         using (SearchResultCollection results = searcher.FindAll())
         {
             foreach (SearchResult result in results)
             {
                 string strB = "ldap://";
                 foreach (string str4 in result.Properties[PropertyManager.ServiceBindingInformation])
                 {
                     if ((str4.Length > strB.Length) && (string.Compare(str4.Substring(0, strB.Length), strB, StringComparison.OrdinalIgnoreCase) == 0))
                     {
                         adamInstanceNames.Add(str4.Substring(strB.Length));
                     }
                 }
             }
         }
     }
     catch (COMException exception)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
     }
     finally
     {
         searchRootEntry.Dispose();
     }
     return FindAliveAdamInstance(null, context, adamInstanceNames);
 }
 public static Forest GetForest(DirectoryContext context)
 {
     DirectoryEntryManager directoryEntryMgr = null;
     DirectoryEntry rootDSE = null;
     string distinguishedName = null;
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     if ((context.ContextType != DirectoryContextType.Forest) && (context.ContextType != DirectoryContextType.DirectoryServer))
     {
         throw new ArgumentException(Res.GetString("TargetShouldBeServerORForest"), "context");
     }
     if ((context.Name == null) && !context.isRootDomain())
     {
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ContextNotAssociatedWithDomain"), typeof(Forest), null);
     }
     if (((context.Name != null) && !context.isRootDomain()) && !context.isServer())
     {
         if (context.ContextType == DirectoryContextType.Forest)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(Forest), context.Name);
         }
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFound", new object[] { context.Name }), typeof(Forest), null);
     }
     context = new DirectoryContext(context);
     directoryEntryMgr = new DirectoryEntryManager(context);
     try
     {
         rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
         if (context.isServer() && !Utils.CheckCapability(rootDSE, Capability.ActiveDirectory))
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFound", new object[] { context.Name }), typeof(Forest), null);
         }
         distinguishedName = (string) PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.RootDomainNamingContext);
     }
     catch (COMException exception)
     {
         if (exception.ErrorCode != -2147016646)
         {
             throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
         }
         if (context.ContextType == DirectoryContextType.Forest)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(Forest), context.Name);
         }
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFound", new object[] { context.Name }), typeof(Forest), null);
     }
     return new Forest(context, Utils.GetDnsNameFromDN(distinguishedName), directoryEntryMgr);
 }
 public static ConfigurationSet GetConfigurationSet(DirectoryContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     if ((context.ContextType != DirectoryContextType.ConfigurationSet) && (context.ContextType != DirectoryContextType.DirectoryServer))
     {
         throw new ArgumentException(Res.GetString("TargetShouldBeServerORConfigSet"), "context");
     }
     if (!context.isServer() && !context.isADAMConfigSet())
     {
         if (context.ContextType == DirectoryContextType.ConfigurationSet)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ConfigurationSet), context.Name);
         }
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", new object[] { context.Name }), typeof(ConfigurationSet), null);
     }
     context = new DirectoryContext(context);
     DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
     DirectoryEntry rootDSE = null;
     string configSetName = null;
     try
     {
         rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
         if (context.isServer() && !Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryApplicationMode))
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", new object[] { context.Name }), typeof(ConfigurationSet), null);
         }
         configSetName = (string) PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.ConfigurationNamingContext);
     }
     catch (COMException exception)
     {
         if (exception.ErrorCode != -2147016646)
         {
             throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
         }
         if (context.ContextType == DirectoryContextType.ConfigurationSet)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ConfigurationSet), context.Name);
         }
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", new object[] { context.Name }), typeof(ConfigurationSet), null);
     }
     catch (ActiveDirectoryObjectNotFoundException)
     {
         if (context.ContextType == DirectoryContextType.ConfigurationSet)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ConfigurationSet), context.Name);
         }
         throw;
     }
     return new ConfigurationSet(context, configSetName, directoryEntryMgr);
 }
Ejemplo n.º 26
0
        public static Domain GetDomain(DirectoryContext context)
        {
            // check that the argument is not null
            if (context == null)
                throw new ArgumentNullException("context");

            // contexttype should be Domain or DirectoryServer
            if ((context.ContextType != DirectoryContextType.Domain) &&
                (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(Res.GetString(Res.TargetShouldBeServerORDomain), "context");
            }

            if ((context.Name == null) && (!context.isDomain()))
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ContextNotAssociatedWithDomain), typeof(Domain), null);
            }

            if (context.Name != null)
            {
                // the target should be a valid domain name or a server
                if (!((context.isDomain()) || (context.isServer())))
                {
                    if (context.ContextType == DirectoryContextType.Domain)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DomainNotFound), typeof(Domain), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DCNotFound, context.Name), typeof(Domain), null);
                    }
                }
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            // bind to the rootDSE of the domain specified in the context
            // and get the dns name
            DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
            DirectoryEntry rootDSE = null;
            string defaultDomainNC = null;
            try
            {
                rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if ((context.isServer()) && (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectory)))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DCNotFound, context.Name), typeof(Domain), null);
                }
                defaultDomainNC = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.DefaultNamingContext);
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked((int)0x8007203a))
                {
                    if (context.ContextType == DirectoryContextType.Domain)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DomainNotFound), typeof(Domain), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DCNotFound, context.Name), typeof(Domain), null);
                    }
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }

            // return domain object
            return new Domain(context, Utils.GetDnsNameFromDN(defaultDomainNC), directoryEntryMgr);
        }
Ejemplo n.º 27
0
		public static AdamInstance GetAdamInstance(DirectoryContext context)
		{
			DirectoryEntryManager directoryEntryManager = null;
			string propertyValue = null;
			if (context != null)
			{
				if (context.ContextType == DirectoryContextType.DirectoryServer)
				{
					if (context.isServer())
					{
						context = new DirectoryContext(context);
						try
						{
							directoryEntryManager = new DirectoryEntryManager(context);
							DirectoryEntry cachedDirectoryEntry = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
							if (Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
							{
								propertyValue = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.DnsHostName);
							}
							else
							{
								object[] name = new object[1];
								name[0] = context.Name;
								throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", name), typeof(AdamInstance), context.Name);
							}
						}
						catch (COMException cOMException1)
						{
							COMException cOMException = cOMException1;
							int errorCode = cOMException.ErrorCode;
							if (errorCode != -2147016646)
							{
								throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
							}
							else
							{
								object[] objArray = new object[1];
								objArray[0] = context.Name;
								throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", objArray), typeof(AdamInstance), context.Name);
							}
						}
						return new AdamInstance(context, propertyValue, directoryEntryManager);
					}
					else
					{
						object[] name1 = new object[1];
						name1[0] = context.Name;
						throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", name1), typeof(AdamInstance), context.Name);
					}
				}
				else
				{
					throw new ArgumentException(Res.GetString("TargetShouldBeADAMServer"), "context");
				}
			}
			else
			{
				throw new ArgumentNullException("context");
			}
		}
Ejemplo n.º 28
0
		internal static AdamInstance FindAnyAdamInstance(DirectoryContext context)
		{
			if (context.ContextType == DirectoryContextType.ConfigurationSet)
			{
				DirectoryEntry searchRootEntry = ConfigurationSet.GetSearchRootEntry(Forest.GetCurrentForest());
				ArrayList arrayLists = new ArrayList();
				try
				{
					try
					{
						StringBuilder stringBuilder = new StringBuilder(15);
						stringBuilder.Append("(&(");
						stringBuilder.Append(PropertyManager.ObjectCategory);
						stringBuilder.Append("=serviceConnectionPoint)");
						stringBuilder.Append("(");
						stringBuilder.Append(PropertyManager.Keywords);
						stringBuilder.Append("=1.2.840.113556.1.4.1851)(");
						stringBuilder.Append(PropertyManager.Keywords);
						stringBuilder.Append("=");
						stringBuilder.Append(Utils.GetEscapedFilterValue(context.Name));
						stringBuilder.Append("))");
						string str = stringBuilder.ToString();
						string[] serviceBindingInformation = new string[1];
						serviceBindingInformation[0] = PropertyManager.ServiceBindingInformation;
						ADSearcher aDSearcher = new ADSearcher(searchRootEntry, str, serviceBindingInformation, SearchScope.Subtree, false, false);
						SearchResultCollection searchResultCollections = aDSearcher.FindAll();
						try
						{
							foreach (SearchResult item in searchResultCollections)
							{
								string str1 = "ldap://";
								IEnumerator enumerator = item.Properties[PropertyManager.ServiceBindingInformation].GetEnumerator();
								try
								{
									while (enumerator.MoveNext())
									{
										string str2 = item.ToString ();
										if (str2.Length <= str1.Length || string.Compare(str2.Substring(0, str1.Length), str1, StringComparison.OrdinalIgnoreCase) != 0)
										{
											continue;
										}
										arrayLists.Add(str2.Substring(str1.Length));
									}
								}
								finally
								{
									IDisposable disposable = enumerator as IDisposable;
									if (disposable != null)
									{
										disposable.Dispose();
									}
								}
							}
						}
						finally
						{
							searchResultCollections.Dispose();
						}
					}
					catch (COMException cOMException1)
					{
						COMException cOMException = cOMException1;
						throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
					}
				}
				finally
				{
					searchRootEntry.Dispose();
				}
				return ConfigurationSet.FindAliveAdamInstance(null, context, arrayLists);
			}
			else
			{
				DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(context);
				DirectoryEntry cachedDirectoryEntry = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
				if (Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
				{
					string propertyValue = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.DnsHostName);
					return new AdamInstance(context, propertyValue, directoryEntryManager);
				}
				else
				{
					directoryEntryManager.RemoveIfExists(directoryEntryManager.ExpandWellKnownDN(WellKnownDN.RootDSE));
					throw new ArgumentException(Res.GetString("TargetShouldBeServerORConfigSet"), "context");
				}
			}
		}
        public static ActiveDirectorySchema GetSchema(DirectoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (((context.ContextType != DirectoryContextType.Forest) && (context.ContextType != DirectoryContextType.ConfigurationSet)) && (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(Res.GetString("NotADOrADAM"), "context");
            }
            if ((context.Name == null) && !context.isRootDomain())
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ContextNotAssociatedWithDomain"), typeof(ActiveDirectorySchema), null);
            }
            if (((context.Name != null) && !context.isRootDomain()) && (!context.isADAMConfigSet() && !context.isServer()))
            {
                if (context.ContextType == DirectoryContextType.Forest)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(ActiveDirectorySchema), context.Name);
                }
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ActiveDirectorySchema), context.Name);
                }
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ServerNotFound", new object[] { context.Name }), typeof(ActiveDirectorySchema), null);
            }
            context = new DirectoryContext(context);
            DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
            string distinguishedName = null;

            try
            {
                DirectoryEntry cachedDirectoryEntry = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if (context.isServer() && !Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryOrADAM))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ServerNotFound", new object[] { context.Name }), typeof(ActiveDirectorySchema), null);
                }
                distinguishedName = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.SchemaNamingContext);
            }
            catch (COMException exception)
            {
                if (exception.ErrorCode != -2147016646)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
                }
                if (context.ContextType == DirectoryContextType.Forest)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(ActiveDirectorySchema), context.Name);
                }
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ActiveDirectorySchema), context.Name);
                }
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ServerNotFound", new object[] { context.Name }), typeof(ActiveDirectorySchema), null);
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ActiveDirectorySchema), context.Name);
                }
                throw;
            }
            return(new ActiveDirectorySchema(context, distinguishedName, directoryEntryMgr));
        }
Ejemplo n.º 30
0
 public static ActiveDirectorySchema GetSchema(DirectoryContext context)
 {
     if (context != null)
     {
         if (context.ContextType == DirectoryContextType.Forest || context.ContextType == DirectoryContextType.ConfigurationSet || context.ContextType == DirectoryContextType.DirectoryServer)
         {
             if (context.Name != null || context.isRootDomain())
             {
                 if (context.Name == null || context.isRootDomain() || context.isADAMConfigSet() || context.isServer())
                 {
                     context = new DirectoryContext(context);
                     DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(context);
                     string propertyValue = null;
                     try
                     {
                         DirectoryEntry cachedDirectoryEntry = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                         if (!context.isServer() || Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryOrADAM))
                         {
                             propertyValue = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.SchemaNamingContext);
                         }
                         else
                         {
                             object[] name = new object[1];
                             name[0] = context.Name;
                             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ServerNotFound", name), typeof(ActiveDirectorySchema), null);
                         }
                     }
                     catch (COMException cOMException1)
                     {
                         COMException cOMException = cOMException1;
                         int          errorCode    = cOMException.ErrorCode;
                         if (errorCode != -2147016646)
                         {
                             throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
                         }
                         else
                         {
                             if (context.ContextType != DirectoryContextType.Forest)
                             {
                                 if (context.ContextType != DirectoryContextType.ConfigurationSet)
                                 {
                                     object[] objArray = new object[1];
                                     objArray[0] = context.Name;
                                     throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ServerNotFound", objArray), typeof(ActiveDirectorySchema), null);
                                 }
                                 else
                                 {
                                     throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ActiveDirectorySchema), context.Name);
                                 }
                             }
                             else
                             {
                                 throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(ActiveDirectorySchema), context.Name);
                             }
                         }
                     }
                     catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException)
                     {
                         if (context.ContextType != DirectoryContextType.ConfigurationSet)
                         {
                             throw;
                         }
                         else
                         {
                             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ActiveDirectorySchema), context.Name);
                         }
                     }
                     return(new ActiveDirectorySchema(context, propertyValue, directoryEntryManager));
                 }
                 else
                 {
                     if (context.ContextType != DirectoryContextType.Forest)
                     {
                         if (context.ContextType != DirectoryContextType.ConfigurationSet)
                         {
                             object[] name1 = new object[1];
                             name1[0] = context.Name;
                             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ServerNotFound", name1), typeof(ActiveDirectorySchema), null);
                         }
                         else
                         {
                             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ActiveDirectorySchema), context.Name);
                         }
                     }
                     else
                     {
                         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(ActiveDirectorySchema), context.Name);
                     }
                 }
             }
             else
             {
                 throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ContextNotAssociatedWithDomain"), typeof(ActiveDirectorySchema), null);
             }
         }
         else
         {
             throw new ArgumentException(Res.GetString("NotADOrADAM"), "context");
         }
     }
     else
     {
         throw new ArgumentNullException("context");
     }
 }
Ejemplo n.º 31
0
        public static ConfigurationSet GetConfigurationSet(DirectoryContext context)
        {
            // check that the argument is not null
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // target should ConfigurationSet or DirectoryServer
            if ((context.ContextType != DirectoryContextType.ConfigurationSet) &&
                (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(SR.TargetShouldBeServerORConfigSet, nameof(context));
            }

            // target should be an adam config set or server
            if (((!context.isServer()) && (!context.isADAMConfigSet())))
            {
                // the target should be a server or an ADAM Config Set
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.ConfigSetNotFound, typeof(ConfigurationSet), context.Name);
                }
                else
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.AINotFound, context.Name), typeof(ConfigurationSet), null);
                }
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            //
            // bind to rootdse of an adam instance (if target is already a server, verify that it is an adam instance)
            //
            DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
            DirectoryEntry        rootDSE           = null;
            string configSetName = null;

            try
            {
                rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if ((context.isServer()) && (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryApplicationMode)))
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.AINotFound, context.Name), typeof(ConfigurationSet), null);
                }

                configSetName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.ConfigurationNamingContext);
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked ((int)0x8007203a))
                {
                    if (context.ContextType == DirectoryContextType.ConfigurationSet)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.ConfigSetNotFound, typeof(ConfigurationSet), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.AINotFound, context.Name), typeof(ConfigurationSet), null);
                    }
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    // this is the case when we could not find an ADAM instance in that config set
                    throw new ActiveDirectoryObjectNotFoundException(SR.ConfigSetNotFound, typeof(ConfigurationSet), context.Name);
                }
                else
                {
                    throw;
                }
            }

            // return config set object
            return(new ConfigurationSet(context, configSetName, directoryEntryMgr));
        }
Ejemplo n.º 32
0
		public static ApplicationPartition GetApplicationPartition(DirectoryContext context)
		{
			if (context != null)
			{
				if (context.ContextType == DirectoryContextType.ApplicationPartition)
				{
					if (context.isNdnc())
					{
						context = new DirectoryContext(context);
						string dNFromDnsName = Utils.GetDNFromDnsName(context.Name);
						DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(context);
						try
						{
							DirectoryEntry cachedDirectoryEntry = directoryEntryManager.GetCachedDirectoryEntry(dNFromDnsName);
							//TODO: REVIEW: URGENT: cachedDirectoryEntry.Bind(true);
						}
						catch (COMException cOMException1)
						{
							COMException cOMException = cOMException1;
							int errorCode = cOMException.ErrorCode;
							if (errorCode != -2147016646)
							{
								throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
							}
							else
							{
								throw new ActiveDirectoryObjectNotFoundException(Res.GetString("NDNCNotFound"), typeof(ApplicationPartition), context.Name);
							}
						}
						return new ApplicationPartition(context, dNFromDnsName, context.Name, ApplicationPartitionType.ADApplicationPartition, directoryEntryManager);
					}
					else
					{
						throw new ActiveDirectoryObjectNotFoundException(Res.GetString("NDNCNotFound"), typeof(ApplicationPartition), context.Name);
					}
				}
				else
				{
					throw new ArgumentException(Res.GetString("TargetShouldBeAppNCDnsName"), "context");
				}
			}
			else
			{
				throw new ArgumentNullException("context");
			}
		}
Ejemplo n.º 33
0
		public static Forest GetForest(DirectoryContext context)
		{
			string propertyValue = null;
			if (context != null)
			{
				if (context.ContextType == DirectoryContextType.Forest || context.ContextType == DirectoryContextType.DirectoryServer)
				{
					if (context.Name != null || context.isRootDomain())
					{
						if (context.Name == null || context.isRootDomain() || context.isServer())
						{
							context = new DirectoryContext(context);
							DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(context);
							try
							{
								DirectoryEntry cachedDirectoryEntry = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
								if (!context.isServer() || Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectory))
								{
									propertyValue = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.RootDomainNamingContext);
								}
								else
								{
									object[] name = new object[1];
									name[0] = context.Name;
									throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFound", name), typeof(Forest), null);
								}
							}
							catch (COMException cOMException1)
							{
								COMException cOMException = cOMException1;
								int errorCode = cOMException.ErrorCode;
								if (errorCode != -2147016646)
								{
									throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
								}
								else
								{
									if (context.ContextType != DirectoryContextType.Forest)
									{
										object[] objArray = new object[1];
										objArray[0] = context.Name;
										throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFound", objArray), typeof(Forest), null);
									}
									else
									{
										throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(Forest), context.Name);
									}
								}
							}
							return new Forest(context, Utils.GetDnsNameFromDN(propertyValue), directoryEntryManager);
						}
						else
						{
							if (context.ContextType != DirectoryContextType.Forest)
							{
								object[] name1 = new object[1];
								name1[0] = context.Name;
								throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFound", name1), typeof(Forest), null);
							}
							else
							{
								throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(Forest), context.Name);
							}
						}
					}
					else
					{
						throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ContextNotAssociatedWithDomain"), typeof(Forest), null);
					}
				}
				else
				{
					throw new ArgumentException(Res.GetString("TargetShouldBeServerORForest"), "context");
				}
			}
			else
			{
				throw new ArgumentNullException("context");
			}
		}
Ejemplo n.º 34
0
 internal static AdamInstance FindAnyAdamInstance(DirectoryContext context)
 {
     if (context.ContextType == DirectoryContextType.ConfigurationSet)
     {
         DirectoryEntry searchRootEntry = ConfigurationSet.GetSearchRootEntry(Forest.GetCurrentForest());
         ArrayList      arrayLists      = new ArrayList();
         try
         {
             try
             {
                 StringBuilder stringBuilder = new StringBuilder(15);
                 stringBuilder.Append("(&(");
                 stringBuilder.Append(PropertyManager.ObjectCategory);
                 stringBuilder.Append("=serviceConnectionPoint)");
                 stringBuilder.Append("(");
                 stringBuilder.Append(PropertyManager.Keywords);
                 stringBuilder.Append("=1.2.840.113556.1.4.1851)(");
                 stringBuilder.Append(PropertyManager.Keywords);
                 stringBuilder.Append("=");
                 stringBuilder.Append(Utils.GetEscapedFilterValue(context.Name));
                 stringBuilder.Append("))");
                 string   str = stringBuilder.ToString();
                 string[] serviceBindingInformation = new string[1];
                 serviceBindingInformation[0] = PropertyManager.ServiceBindingInformation;
                 ADSearcher             aDSearcher = new ADSearcher(searchRootEntry, str, serviceBindingInformation, SearchScope.Subtree, false, false);
                 SearchResultCollection searchResultCollections = aDSearcher.FindAll();
                 try
                 {
                     foreach (SearchResult item in searchResultCollections)
                     {
                         string      str1       = "ldap://";
                         IEnumerator enumerator = item.Properties[PropertyManager.ServiceBindingInformation].GetEnumerator();
                         try
                         {
                             while (enumerator.MoveNext())
                             {
                                 string str2 = item.ToString();
                                 if (str2.Length <= str1.Length || string.Compare(str2.Substring(0, str1.Length), str1, StringComparison.OrdinalIgnoreCase) != 0)
                                 {
                                     continue;
                                 }
                                 arrayLists.Add(str2.Substring(str1.Length));
                             }
                         }
                         finally
                         {
                             IDisposable disposable = enumerator as IDisposable;
                             if (disposable != null)
                             {
                                 disposable.Dispose();
                             }
                         }
                     }
                 }
                 finally
                 {
                     searchResultCollections.Dispose();
                 }
             }
             catch (COMException cOMException1)
             {
                 COMException cOMException = cOMException1;
                 throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
             }
         }
         finally
         {
             searchRootEntry.Dispose();
         }
         return(ConfigurationSet.FindAliveAdamInstance(null, context, arrayLists));
     }
     else
     {
         DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(context);
         DirectoryEntry        cachedDirectoryEntry  = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
         if (Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
         {
             string propertyValue = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.DnsHostName);
             return(new AdamInstance(context, propertyValue, directoryEntryManager));
         }
         else
         {
             directoryEntryManager.RemoveIfExists(directoryEntryManager.ExpandWellKnownDN(WellKnownDN.RootDSE));
             throw new ArgumentException(Res.GetString("TargetShouldBeServerORConfigSet"), "context");
         }
     }
 }
Ejemplo n.º 35
0
        internal static AdamInstance FindAliveAdamInstance(string configSetName, DirectoryContext context, ArrayList adamInstanceNames)
        {
            AdamInstance adamInstance;
            object       name;
            object       obj;
            bool         flag          = false;
            AdamInstance adamInstance1 = null;
            DateTime     utcNow        = DateTime.UtcNow;
            IEnumerator  enumerator    = adamInstanceNames.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    string                current               = (string)enumerator.Current;
                    DirectoryContext      newDirectoryContext   = Utils.GetNewDirectoryContext(current, DirectoryContextType.DirectoryServer, context);
                    DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(newDirectoryContext);
                    DirectoryEntry        cachedDirectoryEntry  = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                    try
                    {
                        //TODO: REVIEW: URGENT!!: cachedDirectoryEntry.Bind(true);
                        adamInstance1 = new AdamInstance(newDirectoryContext, current, directoryEntryManager, true);
                        flag          = true;
                    }
                    catch (COMException cOMException1)
                    {
                        COMException cOMException = cOMException1;
                        if (cOMException.ErrorCode == -2147016646 || cOMException.ErrorCode == -2147016690 || cOMException.ErrorCode == -2147016689 || cOMException.ErrorCode == -2147023436)
                        {
                            DateTime dateTime = DateTime.UtcNow;
                            if (dateTime.Subtract(utcNow) > ConfigurationSet.locationTimeout)
                            {
                                string   str       = "ADAMInstanceNotFoundInConfigSet";
                                object[] objArray  = new object[1];
                                object[] objArray1 = objArray;
                                int      num       = 0;
                                if (configSetName != null)
                                {
                                    obj = configSetName;
                                }
                                else
                                {
                                    obj = context.Name;
                                }
                                objArray1[num] = obj;
                                throw new ActiveDirectoryObjectNotFoundException(Res.GetString(str, objArray), typeof(AdamInstance), null);
                            }
                        }
                        else
                        {
                            throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
                        }
                    }
                    if (!flag)
                    {
                        continue;
                    }
                    adamInstance = adamInstance1;
                    return(adamInstance);
                }
                string   str1      = "ADAMInstanceNotFoundInConfigSet";
                object[] objArray2 = new object[1];
                object[] objArray3 = objArray2;
                int      num1      = 0;
                if (configSetName != null)
                {
                    name = configSetName;
                }
                else
                {
                    name = context.Name;
                }
                objArray3[num1] = name;
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString(str1, objArray2), typeof(AdamInstance), null);
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
            return(adamInstance);
        }
 public static ApplicationPartition GetApplicationPartition(DirectoryContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     if (context.ContextType != DirectoryContextType.ApplicationPartition)
     {
         throw new ArgumentException(Res.GetString("TargetShouldBeAppNCDnsName"), "context");
     }
     if (!context.isNdnc())
     {
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("NDNCNotFound"), typeof(ApplicationPartition), context.Name);
     }
     context = new DirectoryContext(context);
     string dNFromDnsName = Utils.GetDNFromDnsName(context.Name);
     DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
     try
     {
         directoryEntryMgr.GetCachedDirectoryEntry(dNFromDnsName).Bind(true);
     }
     catch (COMException exception)
     {
         if (exception.ErrorCode == -2147016646)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("NDNCNotFound"), typeof(ApplicationPartition), context.Name);
         }
         throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
     }
     return new ApplicationPartition(context, dNFromDnsName, context.Name, ApplicationPartitionType.ADApplicationPartition, directoryEntryMgr);
 }
 public static ActiveDirectorySchema GetSchema(DirectoryContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     if (((context.ContextType != DirectoryContextType.Forest) && (context.ContextType != DirectoryContextType.ConfigurationSet)) && (context.ContextType != DirectoryContextType.DirectoryServer))
     {
         throw new ArgumentException(Res.GetString("NotADOrADAM"), "context");
     }
     if ((context.Name == null) && !context.isRootDomain())
     {
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ContextNotAssociatedWithDomain"), typeof(ActiveDirectorySchema), null);
     }
     if (((context.Name != null) && !context.isRootDomain()) && (!context.isADAMConfigSet() && !context.isServer()))
     {
         if (context.ContextType == DirectoryContextType.Forest)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(ActiveDirectorySchema), context.Name);
         }
         if (context.ContextType == DirectoryContextType.ConfigurationSet)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ActiveDirectorySchema), context.Name);
         }
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ServerNotFound", new object[] { context.Name }), typeof(ActiveDirectorySchema), null);
     }
     context = new DirectoryContext(context);
     DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
     string distinguishedName = null;
     try
     {
         DirectoryEntry cachedDirectoryEntry = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
         if (context.isServer() && !Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryOrADAM))
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ServerNotFound", new object[] { context.Name }), typeof(ActiveDirectorySchema), null);
         }
         distinguishedName = (string) PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.SchemaNamingContext);
     }
     catch (COMException exception)
     {
         if (exception.ErrorCode != -2147016646)
         {
             throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
         }
         if (context.ContextType == DirectoryContextType.Forest)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(ActiveDirectorySchema), context.Name);
         }
         if (context.ContextType == DirectoryContextType.ConfigurationSet)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ActiveDirectorySchema), context.Name);
         }
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ServerNotFound", new object[] { context.Name }), typeof(ActiveDirectorySchema), null);
     }
     catch (ActiveDirectoryObjectNotFoundException)
     {
         if (context.ContextType == DirectoryContextType.ConfigurationSet)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ActiveDirectorySchema), context.Name);
         }
         throw;
     }
     return new ActiveDirectorySchema(context, distinguishedName, directoryEntryMgr);
 }
Ejemplo n.º 38
0
        public void MoveToAnotherSite(string siteName)
        {
            CheckIfDisposed();

            // validate siteName
            if (siteName == null)
            {
                throw new ArgumentNullException("siteName");
            }

            if (siteName.Length == 0)
            {
                throw new ArgumentException(SR.EmptyStringParameter, "siteName");
            }

            // the dc is really being moved to a different site
            if (Utils.Compare(SiteName, siteName) != 0)
            {
                DirectoryEntry newParentEntry = null;
                try
                {
                    // Bind to the target site's server container
                    // Get the distinguished name for the site
                    string parentDN = "CN=Servers,CN=" + siteName + "," + directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.SitesContainer);
                    newParentEntry = DirectoryEntryManager.GetDirectoryEntry(context, parentDN);

                    string serverName = (this is DomainController) ? ((DomainController)this).ServerObjectName : ((AdamInstance)this).ServerObjectName;

                    DirectoryEntry serverEntry = directoryEntryMgr.GetCachedDirectoryEntry(serverName);

                    // force binding (needed otherwise S.DS throw an exception while releasing the COM interface pointer)
                    string dn = (string)PropertyManager.GetPropertyValue(context, serverEntry, PropertyManager.DistinguishedName);

                    // move the object to the servers container of the target site
                    serverEntry.MoveTo(newParentEntry);
                }
                catch (COMException e)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
                finally
                {
                    if (newParentEntry != null)
                    {
                        newParentEntry.Dispose();
                    }
                }

                // remove stale cached directory entries
                // invalidate the cached properties that get affected by this
                siteInfoModified = true;
                cachedSiteName   = null;

                if (cachedSiteObjectName != null)
                {
                    directoryEntryMgr.RemoveIfExists(cachedSiteObjectName);
                    cachedSiteObjectName = null;
                }

                if (cachedServerObjectName != null)
                {
                    directoryEntryMgr.RemoveIfExists(cachedServerObjectName);
                    cachedServerObjectName = null;
                }

                if (cachedNtdsaObjectName != null)
                {
                    directoryEntryMgr.RemoveIfExists(cachedNtdsaObjectName);
                    cachedNtdsaObjectName = null;
                }
            }
        }
Ejemplo n.º 39
0
        public static ActiveDirectorySchema GetSchema(DirectoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // contexttype should be Forest, DirectoryServer or ConfigurationSet
            if ((context.ContextType != DirectoryContextType.Forest) &&
                (context.ContextType != DirectoryContextType.ConfigurationSet) &&
                (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(Res.GetString(Res.NotADOrADAM), "context");
            }

            if ((context.Name == null) && (!context.isRootDomain()))
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ContextNotAssociatedWithDomain), typeof(ActiveDirectorySchema), null);
            }

            if (context.Name != null)
            {
                // the target should be a valid forest name or a server
                if (!((context.isRootDomain()) || (context.isADAMConfigSet()) || (context.isServer())))
                {
                    if (context.ContextType == DirectoryContextType.Forest)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ForestNotFound), typeof(ActiveDirectorySchema), context.Name);
                    }
                    else if (context.ContextType == DirectoryContextType.ConfigurationSet)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ConfigSetNotFound), typeof(ActiveDirectorySchema), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ServerNotFound, context.Name), typeof(ActiveDirectorySchema), null);
                    }
                }
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
            string schemaNC = null;
            try
            {
                DirectoryEntry rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);

                if ((context.isServer()) && (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryOrADAM)))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ServerNotFound, context.Name), typeof(ActiveDirectorySchema), null);
                }

                schemaNC = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.SchemaNamingContext);
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked((int)0x8007203a))
                {
                    if (context.ContextType == DirectoryContextType.Forest)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ForestNotFound), typeof(ActiveDirectorySchema), context.Name);
                    }
                    else if (context.ContextType == DirectoryContextType.ConfigurationSet)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ConfigSetNotFound), typeof(ActiveDirectorySchema), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ServerNotFound, context.Name), typeof(ActiveDirectorySchema), null);
                    }
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    // this is the case where the context is a config set and we could not find an ADAM instance in that config set
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ConfigSetNotFound), typeof(ActiveDirectorySchema), context.Name);
                }
                else
                    throw;
            }

            return new ActiveDirectorySchema(context, schemaNC, directoryEntryMgr);
        }
Ejemplo n.º 40
0
        public static ActiveDirectorySchema GetSchema(DirectoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // contexttype should be Forest, DirectoryServer or ConfigurationSet
            if ((context.ContextType != DirectoryContextType.Forest) &&
                (context.ContextType != DirectoryContextType.ConfigurationSet) &&
                (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(SR.NotADOrADAM, nameof(context));
            }

            if ((context.Name == null) && (!context.isRootDomain()))
            {
                throw new ActiveDirectoryObjectNotFoundException(SR.ContextNotAssociatedWithDomain, typeof(ActiveDirectorySchema), null);
            }

            if (context.Name != null)
            {
                // the target should be a valid forest name or a server
                if (!((context.isRootDomain()) || (context.isADAMConfigSet()) || (context.isServer())))
                {
                    if (context.ContextType == DirectoryContextType.Forest)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.ForestNotFound, typeof(ActiveDirectorySchema), context.Name);
                    }
                    else if (context.ContextType == DirectoryContextType.ConfigurationSet)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.ConfigSetNotFound, typeof(ActiveDirectorySchema), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.ServerNotFound, context.Name), typeof(ActiveDirectorySchema), null);
                    }
                }
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
            string schemaNC = null;

            try
            {
                DirectoryEntry rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);

                if ((context.isServer()) && (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryOrADAM)))
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.ServerNotFound, context.Name), typeof(ActiveDirectorySchema), null);
                }

                schemaNC = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.SchemaNamingContext);
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked ((int)0x8007203a))
                {
                    if (context.ContextType == DirectoryContextType.Forest)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.ForestNotFound, typeof(ActiveDirectorySchema), context.Name);
                    }
                    else if (context.ContextType == DirectoryContextType.ConfigurationSet)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.ConfigSetNotFound, typeof(ActiveDirectorySchema), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.ServerNotFound, context.Name), typeof(ActiveDirectorySchema), null);
                    }
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    // this is the case where the context is a config set and we could not find an ADAM instance in that config set
                    throw new ActiveDirectoryObjectNotFoundException(SR.ConfigSetNotFound, typeof(ActiveDirectorySchema), context.Name);
                }
                else
                {
                    throw;
                }
            }

            return(new ActiveDirectorySchema(context, schemaNC, directoryEntryMgr));
        }
Ejemplo n.º 41
0
        public static Forest GetForest(DirectoryContext context)
        {
            DirectoryEntryManager directoryEntryMgr = null;
            DirectoryEntry rootDSE = null;
            string rootDomainNC = null;

            // check that the argument is not null
            if (context == null)
                throw new ArgumentNullException("context");

            // contexttype should be Forest or DirectoryServer
            if ((context.ContextType != DirectoryContextType.Forest) &&
                (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(Res.GetString(Res.TargetShouldBeServerORForest), "context");
            }

            if ((context.Name == null) && (!context.isRootDomain()))
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ContextNotAssociatedWithDomain), typeof(Forest), null);
            }

            if (context.Name != null)
            {
                // the target should be a valid forest name or a server
                if (!((context.isRootDomain()) || (context.isServer())))
                {
                    if (context.ContextType == DirectoryContextType.Forest)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ForestNotFound), typeof(Forest), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DCNotFound, context.Name), typeof(Forest), null);
                    }
                }
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            directoryEntryMgr = new DirectoryEntryManager(context);
            // at this point we know that the target is either a 
            // valid forest name or a server (may be a bogus server name -- to check bind to rootdse)
            // bind to the rootDSE of the forest specified in the context
            try
            {
                rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if ((context.isServer()) && (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectory)))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DCNotFound, context.Name), typeof(Forest), null);
                }
                rootDomainNC = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.RootDomainNamingContext);
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked((int)0x8007203a))
                {
                    if (context.ContextType == DirectoryContextType.Forest)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ForestNotFound), typeof(Forest), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DCNotFound, context.Name), typeof(Forest), null);
                    }
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }

            // return forest object

            return new Forest(context, Utils.GetDnsNameFromDN(rootDomainNC), directoryEntryMgr);
        }