// Internal Constructors internal ApplicationPartition(DirectoryContext context, string distinguishedName, string dnsName, ApplicationPartitionType appType, DirectoryEntryManager directoryEntryMgr) : base(context, distinguishedName) { this.directoryEntryMgr = directoryEntryMgr; _appType = appType; _dnsName = dnsName; }
internal ConfigurationSet(DirectoryContext context, string configSetName, DirectoryEntryManager directoryEntryMgr) { this.cachedSecurityLevel = ~ReplicationSecurityLevel.NegotiatePassThrough; this.context = context; this.configSetName = configSetName; this.directoryEntryMgr = directoryEntryMgr; }
internal ApplicationPartition(DirectoryContext context, string distinguishedName, string dnsName, ApplicationPartitionType appType, DirectoryEntryManager directoryEntryMgr) : base(context, distinguishedName) { this.appType = ApplicationPartitionType.Unknown; this.committed = true; base.directoryEntryMgr = directoryEntryMgr; this.appType = appType; this.dnsName = dnsName; }
public ActiveDirectorySiteLink(DirectoryContext context, string siteLinkName, ActiveDirectoryTransportType transport, ActiveDirectorySchedule schedule) { DirectoryEntry directoryEntry; this.systemDefaultInterval = new TimeSpan(0, 15, 0); this.sites = new ActiveDirectorySiteCollection(); ValidateArgument(context, siteLinkName, transport); context = new DirectoryContext(context); this.context = context; this.name = siteLinkName; this.transport = transport; try { directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE); string str = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext); string dn = null; if (transport == ActiveDirectoryTransportType.Rpc) { dn = "CN=IP,CN=Inter-Site Transports,CN=Sites," + str; } else { dn = "CN=SMTP,CN=Inter-Site Transports,CN=Sites," + str; } directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, dn); } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(context, exception); } catch (ActiveDirectoryObjectNotFoundException) { throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { context.Name })); } try { string escapedPath = Utils.GetEscapedPath("cn=" + this.name); this.cachedEntry = directoryEntry.Children.Add(escapedPath, "siteLink"); this.cachedEntry.Properties["cost"].Value = 100; this.cachedEntry.Properties["replInterval"].Value = 180; if (schedule != null) { this.cachedEntry.Properties["schedule"].Value = schedule.GetUnmanagedSchedule(); } } catch (COMException exception2) { if (((exception2.ErrorCode == -2147016656) && Utils.CheckCapability(DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE), Capability.ActiveDirectoryApplicationMode)) && (transport == ActiveDirectoryTransportType.Smtp)) { throw new NotSupportedException(Res.GetString("NotSupportTransportSMTP")); } throw ExceptionHelper.GetExceptionFromCOMException(context, exception2); } finally { directoryEntry.Dispose(); } }
internal Forest(DirectoryContext context, string forestDnsName, DirectoryEntryManager directoryEntryMgr) { this.dsHandle = IntPtr.Zero; this.authIdentity = IntPtr.Zero; this.currentForestMode = ~System.DirectoryServices.ActiveDirectory.ForestMode.Windows2000Forest; this.context = context; this.directoryEntryMgr = directoryEntryMgr; this.forestDnsName = forestDnsName; }
public static ReplicationConnection FindByName(DirectoryContext context, string name) { ValidateArgument(context, name); // work with copy of the context context = new DirectoryContext(context); // bind to the rootdse to get the servername property DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE); try { string serverDN = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ServerName); string connectionContainer = "CN=NTDS Settings," + serverDN; de = DirectoryEntryManager.GetDirectoryEntry(context, connectionContainer); // doing the search to find the connection object based on its name ADSearcher adSearcher = new ADSearcher(de, "(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)(name=" + Utils.GetEscapedFilterValue(name) + "))", new string[] { "distinguishedName" }, SearchScope.OneLevel, false, /* no paged search */ false /* don't cache results */); SearchResult srchResult = null; try { srchResult = adSearcher.FindOne(); } catch (COMException e) { if (e.ErrorCode == unchecked ((int)0x80072030)) { // object is not found since we cannot even find the container in which to search throw new ActiveDirectoryObjectNotFoundException(SR.DSNotFound, typeof(ReplicationConnection), name); } else { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } } if (srchResult == null) { // no such connection object Exception e = new ActiveDirectoryObjectNotFoundException(SR.DSNotFound, typeof(ReplicationConnection), name); throw e; } else { DirectoryEntry connectionEntry = srchResult.GetDirectoryEntry(); return(new ReplicationConnection(context, connectionEntry, name)); } } finally { de.Dispose(); } }
public override DirectoryEntry GetDirectoryEntry() { base.CheckIfDisposed(); if (!this.committed) { throw new InvalidOperationException(Res.GetString("CannotGetObject")); } return(DirectoryEntryManager.GetDirectoryEntry(base.context, base.Name)); }
public DirectoryEntry GetDirectoryEntry() { if (_disposed) { throw new ObjectDisposedException(GetType().Name); } return(DirectoryEntryManager.GetDirectoryEntryInternal(_context, _cachedEntry.Path)); }
internal DirectoryEntry GetSchemaClassDirectoryEntry() { if (this.classEntry == null) { this.InitializePropertiesFromSchemaContainer(); this.classEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, (string)this.GetValueFromCache(PropertyManager.DistinguishedName, true)); } return(this.classEntry); }
private DirectoryEntry GetNewDirectoryEntry(string dn) { if (this.bindingPrefix == null) { this.bindingPrefix = string.Concat("LDAP://", this.context.GetServerName(), ":389", "/"); } //this.pathCracker.Set(dn, 4); //string str = this.pathCracker.Retrieve(7); return(DirectoryEntryManager.Bind(string.Concat(this.bindingPrefix, dn), this.context.UserName, this.context.Password, this.context.useServerBind())); }
public DirectoryEntry GetDirectoryEntry() { this.CheckIfDisposed(); if (!this.isBound) { throw new InvalidOperationException(Res.GetString("CannotGetObject")); } this.GetSchemaClassDirectoryEntry(); return(DirectoryEntryManager.GetDirectoryEntryInternal(this.context, this.classEntry.Path)); }
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)); }
private ArrayList GetApplicationPartitions() { ArrayList list = new ArrayList(); DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer)); StringBuilder builder = new StringBuilder(15); builder.Append("(&("); builder.Append(PropertyManager.ObjectCategory); builder.Append("=crossRef)("); builder.Append(PropertyManager.SystemFlags); builder.Append(":1.2.840.113556.1.4.804:="); builder.Append(1); builder.Append(")(!("); builder.Append(PropertyManager.SystemFlags); builder.Append(":1.2.840.113556.1.4.803:="); builder.Append(2); builder.Append(")))"); string filter = builder.ToString(); string[] propertiesToLoad = new string[] { PropertyManager.DnsRoot, PropertyManager.NCName }; ADSearcher searcher = new ADSearcher(directoryEntry, filter, propertiesToLoad, SearchScope.OneLevel); SearchResultCollection results = null; try { results = searcher.FindAll(); string str2 = this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.SchemaNamingContext); string str3 = this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.ConfigurationNamingContext); foreach (SearchResult result in results) { string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.NCName); if (!searchResultPropertyValue.Equals(str2) && !searchResultPropertyValue.Equals(str3)) { string name = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsRoot); DirectoryContext context = Utils.GetNewDirectoryContext(name, DirectoryContextType.ApplicationPartition, this.context); list.Add(new ApplicationPartition(context, searchResultPropertyValue, (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsRoot), ApplicationPartitionType.ADApplicationPartition, new DirectoryEntryManager(context))); } } return(list); } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception); } finally { if (results != null) { results.Dispose(); } directoryEntry.Dispose(); } return(list); }
internal DirectoryEntry GetCrossRefEntry() { if (this.crossRefEntry == null) { using (DirectoryEntry entry = DirectoryEntryManager.GetDirectoryEntry(base.context, base.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer))) { this.crossRefEntry = Utils.GetCrossRefEntry(base.context, entry, base.Name); } } return(this.crossRefEntry); }
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)); }
public DirectoryEntry GetDirectoryEntry() { if (!this.disposed) { return(DirectoryEntryManager.GetDirectoryEntryInternal(this.context, this.cachedEntry.Path)); } else { throw new ObjectDisposedException(this.GetType().Name); } }
internal string GetNamingRoleOwner() { using (DirectoryEntry entry = DirectoryEntryManager.GetDirectoryEntry(base.context, base.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer))) { if (this.appType == ApplicationPartitionType.ADApplicationPartition) { return(Utils.GetDnsHostNameFromNTDSA(base.context, (string)PropertyManager.GetPropertyValue(base.context, entry, PropertyManager.FsmoRoleOwner))); } return(Utils.GetAdamDnsHostNameFromNTDSA(base.context, (string)PropertyManager.GetPropertyValue(base.context, entry, PropertyManager.FsmoRoleOwner))); } }
private void InitializePropertiesFromSchemaContainer() { if (!this.propertiesFromSchemaContainerInitialized) { if (this.schemaEntry == null) { this.schemaEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, WellKnownDN.SchemaNamingContext); } this.propertyValuesFromServer = GetPropertiesFromSchemaContainer(this.context, this.schemaEntry, this.isDefunctOnServer ? this.commonName : this.ldapDisplayName, this.isDefunctOnServer); this.propertiesFromSchemaContainerInitialized = true; } }
public DirectoryEntry GetDirectoryEntry() { if (this.disposed) { throw new ObjectDisposedException(base.GetType().Name); } if (!this.existing) { throw new InvalidOperationException(Res.GetString("CannotGetObject")); } return(DirectoryEntryManager.GetDirectoryEntryInternal(this.context, this.cachedEntry.Path)); }
public void MoveToAnotherSite(string siteName) { this.CheckIfDisposed(); if (siteName == null) { throw new ArgumentNullException("siteName"); } if (siteName.Length == 0) { throw new ArgumentException(Res.GetString("EmptyStringParameter"), "siteName"); } if (Utils.Compare(this.SiteName, siteName) != 0) { DirectoryEntry newParent = null; try { string dn = "CN=Servers,CN=" + siteName + "," + this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.SitesContainer); newParent = DirectoryEntryManager.GetDirectoryEntry(this.context, dn); string distinguishedName = (this is DomainController) ? ((DomainController)this).ServerObjectName : ((AdamInstance)this).ServerObjectName; DirectoryEntry cachedDirectoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(distinguishedName); string text1 = (string)PropertyManager.GetPropertyValue(this.context, cachedDirectoryEntry, PropertyManager.DistinguishedName); cachedDirectoryEntry.MoveTo(newParent); } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception); } finally { if (newParent != null) { newParent.Dispose(); } } this.siteInfoModified = true; this.cachedSiteName = null; if (this.cachedSiteObjectName != null) { this.directoryEntryMgr.RemoveIfExists(this.cachedSiteObjectName); this.cachedSiteObjectName = null; } if (this.cachedServerObjectName != null) { this.directoryEntryMgr.RemoveIfExists(this.cachedServerObjectName); this.cachedServerObjectName = null; } if (this.cachedNtdsaObjectName != null) { this.directoryEntryMgr.RemoveIfExists(this.cachedNtdsaObjectName); this.cachedNtdsaObjectName = null; } } }
internal AdamInstance(DirectoryContext context, string adamInstanceName, DirectoryEntryManager directoryEntryMgr, bool nameIncludesPort) { this.cachedLdapPort = -1; this.cachedSslPort = -1; this.ADAMHandle = IntPtr.Zero; this.authIdentity = IntPtr.Zero; base.context = context; base.replicaName = adamInstanceName; base.directoryEntryMgr = directoryEntryMgr; this.becomeRoleOwnerAttrs = new string[] { PropertyManager.BecomeSchemaMaster, PropertyManager.BecomeDomainMaster }; this.syncAllFunctionPointer = new SyncReplicaFromAllServersCallback(this.SyncAllCallbackRoutine); }
internal ArrayList GetPartitions() { ArrayList partitionList = new ArrayList(); DirectoryEntry?rootDSE = null; DirectoryEntry?serverNtdsaEntry = null; try { // get the writable partitions rootDSE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE); // don't need range retrieval for root dse attributes foreach (string partition in rootDSE.Properties[PropertyManager.NamingContexts]) { partitionList.Add(partition); } // also the read only partitions string ntdsaName = (this is DomainController) ? ((DomainController)this).NtdsaObjectName : ((AdamInstance)this).NtdsaObjectName; serverNtdsaEntry = DirectoryEntryManager.GetDirectoryEntry(context, ntdsaName); // use range retrieval ArrayList propertyNames = new ArrayList(); propertyNames.Add(PropertyManager.HasPartialReplicaNCs); Hashtable?values = null; try { values = Utils.GetValuesWithRangeRetrieval(serverNtdsaEntry, null, propertyNames, SearchScope.Base); } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } ArrayList readOnlyPartitions = (ArrayList)values[PropertyManager.HasPartialReplicaNCs.ToLowerInvariant()] !; Debug.Assert(readOnlyPartitions != null); foreach (string readOnlyPartition in readOnlyPartitions) { partitionList.Add(readOnlyPartition); } } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } finally { rootDSE?.Dispose(); serverNtdsaEntry?.Dispose(); } return(partitionList); }
internal static DirectoryEntry GetDirectoryEntry(DirectoryContext context, string dn) { string str = string.Concat("LDAP://", context.GetServerName(), "/"); /* NativeComInterfaces.IAdsPathname pathname = (NativeComInterfaces.IAdsPathname)(new NativeComInterfaces.Pathname()); * pathname.EscapedMode = 2; * pathname.Set(dn, 4); * string str1 = pathname.Retrieve(7); */ string str1 = dn; return(DirectoryEntryManager.Bind(string.Concat(str, str1), context.UserName, context.Password, context.useServerBind())); }
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 void SeizeRoleOwnership(AdamRole role) { // set the "fsmoRoleOwner" attribute on the appropriate role object // to the NTDSAObjectName of this ADAM Instance string roleObjectDN = null; CheckIfDisposed(); switch (role) { case AdamRole.SchemaRole: { roleObjectDN = directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.SchemaNamingContext); break; } case AdamRole.NamingRole: { roleObjectDN = directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer); break; } default: { throw new InvalidEnumArgumentException(nameof(role), (int)role, typeof(AdamRole)); } } DirectoryEntry roleObjectEntry = null; try { roleObjectEntry = DirectoryEntryManager.GetDirectoryEntry(context, roleObjectDN); roleObjectEntry.Properties[PropertyManager.FsmoRoleOwner].Value = NtdsaObjectName; roleObjectEntry.CommitChanges(); } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } finally { if (roleObjectEntry != null) { roleObjectEntry.Dispose(); } } // invalidate the role collection so that it gets loaded again next time _cachedRoles = null; }
internal ReplicationConnectionCollection GetInboundConnectionsHelper() { string serverObjectName; if (this.inbound == null) { this.inbound = new ReplicationConnectionCollection(); DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(this.Name, DirectoryContextType.DirectoryServer, this.context); if (this as DomainController != null) { serverObjectName = ((DomainController)this).ServerObjectName; } else { serverObjectName = ((AdamInstance)this).ServerObjectName; } string str = serverObjectName; string str1 = string.Concat("CN=NTDS Settings,", str); DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(Utils.GetNewDirectoryContext(this.Name, DirectoryContextType.DirectoryServer, this.context), str1); string[] strArrays = new string[1]; strArrays[0] = "cn"; ADSearcher aDSearcher = new ADSearcher(directoryEntry, "(&(objectClass=nTDSConnection)(objectCategory=nTDSConnection))", strArrays, SearchScope.OneLevel); SearchResultCollection searchResultCollections = null; try { try { searchResultCollections = aDSearcher.FindAll(); foreach (SearchResult searchResult in searchResultCollections) { ReplicationConnection replicationConnection = new ReplicationConnection(newDirectoryContext, searchResult.GetDirectoryEntry(), (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.Cn)); this.inbound.Add(replicationConnection); } } catch (COMException cOMException1) { COMException cOMException = cOMException1; throw ExceptionHelper.GetExceptionFromCOMException(newDirectoryContext, cOMException); } } finally { if (searchResultCollections != null) { searchResultCollections.Dispose(); } directoryEntry.Dispose(); } } return(this.inbound); }
public void SeizeRoleOwnership(ActiveDirectoryRole role) { string dn = null; base.CheckIfDisposed(); switch (role) { case ActiveDirectoryRole.SchemaRole: dn = base.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.SchemaNamingContext); break; case ActiveDirectoryRole.NamingRole: dn = base.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer); break; case ActiveDirectoryRole.PdcRole: dn = base.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.DefaultNamingContext); break; case ActiveDirectoryRole.RidRole: dn = base.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RidManager); break; case ActiveDirectoryRole.InfrastructureRole: dn = base.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.Infrastructure); break; default: throw new InvalidEnumArgumentException("role", (int)role, typeof(ActiveDirectoryRole)); } DirectoryEntry directoryEntry = null; try { directoryEntry = DirectoryEntryManager.GetDirectoryEntry(base.context, dn); directoryEntry.Properties[PropertyManager.FsmoRoleOwner].Value = this.NtdsaObjectName; directoryEntry.CommitChanges(); } catch (COMException exception) { throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(base.context, exception); } finally { if (directoryEntry != null) { directoryEntry.Dispose(); } } this.cachedRoles = null; }
internal AdamInstance(DirectoryContext context, string adamInstanceName, DirectoryEntryManager directoryEntryMgr, bool nameIncludesPort) { this.context = context; this.replicaName = adamInstanceName; this.directoryEntryMgr = directoryEntryMgr; // initialize the transfer role owner attributes _becomeRoleOwnerAttrs = new String[2]; _becomeRoleOwnerAttrs[0] = PropertyManager.BecomeSchemaMaster; _becomeRoleOwnerAttrs[1] = PropertyManager.BecomeDomainMaster; // initialize the callback function _syncAllFunctionPointer = new SyncReplicaFromAllServersCallback(SyncAllCallbackRoutine); }
public static ReplicationConnection FindByName(DirectoryContext context, string name) { ReplicationConnection replicationConnection; ReplicationConnection.ValidateArgument(context, name); context = new DirectoryContext(context); DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE); try { string propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ServerName); string str = string.Concat("CN=NTDS Settings,", propertyValue); directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, str); string[] strArrays = new string[1]; strArrays[0] = "distinguishedName"; ADSearcher aDSearcher = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)(name=", Utils.GetEscapedFilterValue(name), "))"), strArrays, SearchScope.OneLevel, false, false); SearchResult searchResult = null; try { searchResult = aDSearcher.FindOne(); } catch (COMException cOMException1) { COMException cOMException = cOMException1; if (cOMException.ErrorCode != -2147016656) { throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException); } else { throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ReplicationConnection), name); } } if (searchResult != null) { DirectoryEntry directoryEntry1 = searchResult.GetDirectoryEntry(); replicationConnection = new ReplicationConnection(context, directoryEntry1, name); } else { Exception activeDirectoryObjectNotFoundException = new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ReplicationConnection), name); throw activeDirectoryObjectNotFoundException; } } finally { directoryEntry.Dispose(); } return(replicationConnection); }
internal ArrayList GetPartitions() { ArrayList list = new ArrayList(); DirectoryEntry directoryEntry = null; DirectoryEntry searchRootEntry = null; try { directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, WellKnownDN.RootDSE); foreach (string str in directoryEntry.Properties[PropertyManager.NamingContexts]) { list.Add(str); } string dn = (this is DomainController) ? ((DomainController)this).NtdsaObjectName : ((AdamInstance)this).NtdsaObjectName; searchRootEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, dn); ArrayList propertiesToLoad = new ArrayList(); propertiesToLoad.Add(PropertyManager.HasPartialReplicaNCs); Hashtable hashtable = null; try { hashtable = Utils.GetValuesWithRangeRetrieval(searchRootEntry, null, propertiesToLoad, SearchScope.Base); } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception); } ArrayList list3 = (ArrayList)hashtable[PropertyManager.HasPartialReplicaNCs.ToLower(CultureInfo.InvariantCulture)]; foreach (string str3 in list3) { list.Add(str3); } return(list); } catch (COMException exception2) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception2); } finally { if (directoryEntry != null) { directoryEntry.Dispose(); } if (searchRootEntry != null) { searchRootEntry.Dispose(); } } return(list); }
// // 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 DirectoryEntry GetDirectoryEntry() { if (_disposed) { throw new ObjectDisposedException(GetType().Name); } if (!existing) { throw new InvalidOperationException(Res.GetString(Res.CannotGetObject)); } else { return(DirectoryEntryManager.GetDirectoryEntryInternal(context, cachedEntry.Path)); } }
public void RaiseForestFunctionalityLevel(int forestMode) { CheckIfDisposed(); // check new functional level is valid or not if (forestMode < 0) { throw new ArgumentException(SR.InvalidMode, nameof(forestMode)); } // new functional level should be higher than the old one if (forestMode <= ForestModeLevel) { throw new ArgumentException(SR.InvalidMode, nameof(forestMode)); } // set the forest mode on AD DirectoryEntry partitionsEntry = DirectoryEntryManager.GetDirectoryEntry(_context, _directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer)); // NOTE: // If the domain is a W2K domain (W2K schema) then the msDS-Behavior-Version attribute will not be present. // If that is the case, the forest functionality cannot be raised. try { partitionsEntry.Properties[PropertyManager.MsDSBehaviorVersion].Value = forestMode; partitionsEntry.CommitChanges(); } catch (System.Runtime.InteropServices.COMException e) { if (e.ErrorCode == unchecked ((int)0x8007200A)) { throw new ArgumentException(SR.NoW2K3DCsInForest, nameof(forestMode)); } else { throw ExceptionHelper.GetExceptionFromCOMException(_context, e); } } finally { partitionsEntry.Dispose(); } // at this point the raise forest function has succeeded // invalidate the cached entry so that we will go to the server next time _forestModeLevel = -1; }
internal DomainController(DirectoryContext context, string domainControllerName, DirectoryEntryManager directoryEntryMgr) { this.context = context; this.replicaName = domainControllerName; this.directoryEntryMgr = directoryEntryMgr; // initialize the transfer role owner attributes _becomeRoleOwnerAttrs = new String[5]; _becomeRoleOwnerAttrs[0] = PropertyManager.BecomeSchemaMaster; _becomeRoleOwnerAttrs[1] = PropertyManager.BecomeDomainMaster; _becomeRoleOwnerAttrs[2] = PropertyManager.BecomePdc; _becomeRoleOwnerAttrs[3] = PropertyManager.BecomeRidMaster; _becomeRoleOwnerAttrs[4] = PropertyManager.BecomeInfrastructureMaster; // initialize the callback function syncAllFunctionPointer = new SyncReplicaFromAllServersCallback(SyncAllCallbackRoutine); }
internal AdamInstance(DirectoryContext context, string adamHostName, DirectoryEntryManager directoryEntryMgr) { string str; this.cachedLdapPort = -1; this.cachedSslPort = -1; this.ADAMHandle = IntPtr.Zero; this.authIdentity = IntPtr.Zero; base.context = context; base.replicaName = adamHostName; Utils.SplitServerNameAndPortNumber(context.Name, out str); if (str != null) { base.replicaName = base.replicaName + ":" + str; } base.directoryEntryMgr = directoryEntryMgr; this.becomeRoleOwnerAttrs = new string[] { PropertyManager.BecomeSchemaMaster, PropertyManager.BecomeDomainMaster }; this.syncAllFunctionPointer = new SyncReplicaFromAllServersCallback(this.SyncAllCallbackRoutine); }
internal AdamInstance(DirectoryContext context, string adamHostName, DirectoryEntryManager directoryEntryMgr) { this.context = context; // the replica name should be in the form dnshostname:port this.replicaName = adamHostName; string portNumber; Utils.SplitServerNameAndPortNumber(context.Name, out portNumber); if (portNumber != null) { this.replicaName = this.replicaName + ":" + portNumber; } // initialize the directory entry manager this.directoryEntryMgr = directoryEntryMgr; // initialize the transfer role owner attributes _becomeRoleOwnerAttrs = new String[2]; _becomeRoleOwnerAttrs[0] = PropertyManager.BecomeSchemaMaster; _becomeRoleOwnerAttrs[1] = PropertyManager.BecomeDomainMaster; // initialize the callback function _syncAllFunctionPointer = new SyncReplicaFromAllServersCallback(SyncAllCallbackRoutine); }
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); }
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); }
internal ActiveDirectorySchema(DirectoryContext context, string distinguishedName, DirectoryEntryManager directoryEntryMgr) : base(context, distinguishedName) { this.directoryEntryMgr = directoryEntryMgr; _schemaEntry = DirectoryEntryManager.GetDirectoryEntry(context, distinguishedName); }
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"); } }
public static GlobalCatalog GetGlobalCatalog(DirectoryContext context) { string propertyValue = null; DirectoryEntryManager directoryEntryManager = null; if (context != null) { if (context.ContextType == DirectoryContextType.DirectoryServer) { if (context.isServer()) { context = new DirectoryContext(context); try { directoryEntryManager = new DirectoryEntryManager(context); DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE); if (Utils.CheckCapability(directoryEntry, Capability.ActiveDirectory)) { propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.DnsHostName); bool flag = bool.Parse((string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.IsGlobalCatalogReady)); if (!flag) { object[] name = new object[1]; name[0] = context.Name; throw new ActiveDirectoryObjectNotFoundException(Res.GetString("GCNotFound", name), typeof(GlobalCatalog), context.Name); } } else { object[] objArray = new object[1]; objArray[0] = context.Name; throw new ActiveDirectoryObjectNotFoundException(Res.GetString("GCNotFound", objArray), typeof(GlobalCatalog), context.Name); } } catch (COMException cOMException1) { COMException cOMException = cOMException1; int errorCode = cOMException.ErrorCode; if (errorCode != -2147016646) { throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException); } else { object[] name1 = new object[1]; name1[0] = context.Name; throw new ActiveDirectoryObjectNotFoundException(Res.GetString("GCNotFound", name1), typeof(GlobalCatalog), context.Name); } } return new GlobalCatalog(context, propertyValue, directoryEntryManager); } else { object[] objArray1 = new object[1]; objArray1[0] = context.Name; throw new ActiveDirectoryObjectNotFoundException(Res.GetString("GCNotFound", objArray1), typeof(GlobalCatalog), context.Name); } } else { throw new ArgumentException(Res.GetString("TargetShouldBeGC"), "context"); } } else { throw new ArgumentNullException("context"); } }
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"); } }
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; }
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"); } } }
internal ConfigurationSet(DirectoryContext context, string configSetName, DirectoryEntryManager directoryEntryMgr) { this.cachedSecurityLevel = ReplicationSecurityLevel.MutualAuthentication | ReplicationSecurityLevel.Negotiate; this.context = context; this.configSetName = configSetName; this.directoryEntryMgr = directoryEntryMgr; }
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); }
// internal constructors internal Domain(DirectoryContext context, string domainName, DirectoryEntryManager directoryEntryMgr) : base(context, domainName) { this.directoryEntryMgr = directoryEntryMgr; }
public static ApplicationPartition FindByName(DirectoryContext context, string distinguishedName) { ApplicationPartition partition = null; DirectoryEntryManager directoryEntryMgr = null; DirectoryContext appNCContext = null; // check that the argument is not null if (context == null) throw new ArgumentNullException("context"); if ((context.Name == null) && (!context.isRootDomain())) { throw new ArgumentException(Res.GetString(Res.ContextNotAssociatedWithDomain), "context"); } if (context.Name != null) { // the target should be a valid forest name, configset name or a server if (!((context.isRootDomain()) || (context.isADAMConfigSet()) || context.isServer())) { throw new ArgumentException(Res.GetString(Res.NotADOrADAM), "context"); } } // check that the distingushed name of the application partition is not null or empty if (distinguishedName == null) throw new ArgumentNullException("distinguishedName"); if (distinguishedName.Length == 0) throw new ArgumentException(Res.GetString(Res.EmptyStringParameter), "distinguishedName"); if (!Utils.IsValidDNFormat(distinguishedName)) throw new ArgumentException(Res.GetString(Res.InvalidDNFormat), "distinguishedName"); // work with copy of the context context = new DirectoryContext(context); // search in the partitions container of the forest for // crossRef objects that have their nCName set to the specified distinguishedName directoryEntryMgr = new DirectoryEntryManager(context); DirectoryEntry partitionsEntry = null; try { partitionsEntry = DirectoryEntryManager.GetDirectoryEntry(context, directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer)); } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } catch (ActiveDirectoryObjectNotFoundException) { // 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 ActiveDirectoryOperationException(Res.GetString(Res.ADAMInstanceNotFoundInConfigSet, context.Name)); } // build the filter StringBuilder str = new StringBuilder(15); str.Append("(&("); str.Append(PropertyManager.ObjectCategory); str.Append("=crossRef)("); str.Append(PropertyManager.SystemFlags); str.Append(":1.2.840.113556.1.4.804:="); str.Append((int)SystemFlag.SystemFlagNtdsNC); str.Append(")(!("); str.Append(PropertyManager.SystemFlags); str.Append(":1.2.840.113556.1.4.803:="); str.Append((int)SystemFlag.SystemFlagNtdsDomain); str.Append("))("); str.Append(PropertyManager.NCName); str.Append("="); str.Append(Utils.GetEscapedFilterValue(distinguishedName)); str.Append("))"); string filter = str.ToString(); string[] propertiesToLoad = new string[2]; propertiesToLoad[0] = PropertyManager.DnsRoot; propertiesToLoad[1] = PropertyManager.NCName; ADSearcher searcher = new ADSearcher(partitionsEntry, filter, propertiesToLoad, SearchScope.OneLevel, false /*not paged search*/, false /*no cached results*/); SearchResult res = null; try { res = searcher.FindOne(); } catch (COMException e) { if (e.ErrorCode == unchecked((int)0x80072030)) { // object is not found since we cannot even find the container in which to search throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.AppNCNotFound), typeof(ApplicationPartition), distinguishedName); } else { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } } finally { partitionsEntry.Dispose(); } if (res == null) { // the specified application partition could not be found in the given forest throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.AppNCNotFound), typeof(ApplicationPartition), distinguishedName); } string appNCDnsName = null; try { appNCDnsName = (res.Properties[PropertyManager.DnsRoot].Count > 0) ? (string)res.Properties[PropertyManager.DnsRoot][0] : null; } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } // verify that if the target is a server, then this partition is a naming context on it ApplicationPartitionType appType = GetApplicationPartitionType(context); if (context.ContextType == DirectoryContextType.DirectoryServer) { bool hostsCurrentPartition = false; DistinguishedName appNCDN = new DistinguishedName(distinguishedName); DirectoryEntry rootDSE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE); try { foreach (string namingContext in rootDSE.Properties[PropertyManager.NamingContexts]) { DistinguishedName dn = new DistinguishedName(namingContext); if (dn.Equals(appNCDN)) { hostsCurrentPartition = true; break; } } } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } finally { rootDSE.Dispose(); } if (!hostsCurrentPartition) { throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.AppNCNotFound), typeof(ApplicationPartition), distinguishedName); } appNCContext = context; } else { // we need to find a server which hosts this application partition if (appType == ApplicationPartitionType.ADApplicationPartition) { int errorCode = 0; DomainControllerInfo domainControllerInfo; errorCode = Locator.DsGetDcNameWrapper(null, appNCDnsName, null, (long)PrivateLocatorFlags.OnlyLDAPNeeded, out domainControllerInfo); if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN) { throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.AppNCNotFound), typeof(ApplicationPartition), distinguishedName); } else if (errorCode != 0) { throw ExceptionHelper.GetExceptionFromErrorCode(errorCode); } Debug.Assert(domainControllerInfo.DomainControllerName.Length > 2, "ApplicationPartition:FindByName - domainControllerInfo.DomainControllerName.Length <= 2"); string serverName = domainControllerInfo.DomainControllerName.Substring(2); appNCContext = Utils.GetNewDirectoryContext(serverName, DirectoryContextType.DirectoryServer, context); } else { // this will find an adam instance that hosts this partition and which is alive and responding. string adamInstName = ConfigurationSet.FindOneAdamInstance(context.Name, context, distinguishedName, null).Name; appNCContext = Utils.GetNewDirectoryContext(adamInstName, DirectoryContextType.DirectoryServer, context); } } partition = new ApplicationPartition(appNCContext, (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.NCName), appNCDnsName, appType, directoryEntryMgr); return partition; }
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 GlobalCatalog(DirectoryContext context, string globalCatalogName, DirectoryEntryManager directoryEntryMgr) : base(context, globalCatalogName, directoryEntryMgr) { }
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 Forest(DirectoryContext context, string forestDnsName, DirectoryEntryManager directoryEntryMgr) { this.dsHandle = IntPtr.Zero; this.authIdentity = IntPtr.Zero; this.currentForestMode = ForestMode.Windows2003InterimForest | ForestMode.Windows2003Forest | ForestMode.Windows2008Forest | ForestMode.Windows2008R2Forest | ForestMode.Windows8Forest | ForestMode.Windows2012Forest; this.context = context; this.directoryEntryMgr = directoryEntryMgr; this.forestDnsName = forestDnsName; }
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); }
internal Domain(DirectoryContext context, string domainName, DirectoryEntryManager directoryEntryMgr) : base(context, domainName) { this.currentDomainMode = DomainMode.Windows2000NativeDomain | DomainMode.Windows2003InterimDomain | DomainMode.Windows2003Domain | DomainMode.Windows2008Domain | DomainMode.Windows2008R2Domain | DomainMode.Windows8Domain; this.directoryEntryMgr = directoryEntryMgr; }
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); }
internal ApplicationPartition(DirectoryContext context, string distinguishedName, string dnsName, DirectoryEntryManager directoryEntryMgr) : this(context, distinguishedName, dnsName, GetApplicationPartitionType(context), directoryEntryMgr) { }
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); }