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); }
private DirectoryServer GetSchemaRoleOwner() { DirectoryServer server2; try { this.schemaEntry.RefreshCache(); if (base.context.isADAMConfigSet()) { string adamDnsHostNameFromNTDSA = Utils.GetAdamDnsHostNameFromNTDSA(base.context, (string)PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner)); return(new AdamInstance(Utils.GetNewDirectoryContext(adamDnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, base.context), adamDnsHostNameFromNTDSA)); } DirectoryServer server = null; if (Utils.CheckCapability(base.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE), Capability.ActiveDirectory)) { string dnsHostNameFromNTDSA = Utils.GetDnsHostNameFromNTDSA(base.context, (string)PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner)); server = new DomainController(Utils.GetNewDirectoryContext(dnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, base.context), dnsHostNameFromNTDSA); } else { string adamInstanceName = Utils.GetAdamDnsHostNameFromNTDSA(base.context, (string)PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner)); server = new AdamInstance(Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, base.context), adamInstanceName); } server2 = server; } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception); } return(server2); }
public int IndexOf(AdamInstance adamInstance) { Contract.Requires(adamInstance != null); Contract.Ensures(Contract.Result<int>() >= 0); Contract.Ensures(Contract.Result<int>() < this.Count); return default(int); }
public int IndexOf(AdamInstance adamInstance) { Contract.Requires(adamInstance != null); Contract.Ensures(Contract.Result <int>() >= 0); Contract.Ensures(Contract.Result <int>() < this.Count); return(default(int)); }
internal string GetServerName() { if (this.serverName == null) { DirectoryContextType directoryContextType = this.contextType; switch (directoryContextType) { case DirectoryContextType.Domain: case DirectoryContextType.Forest: { if (this.name == null || this.contextType == DirectoryContextType.Forest && this.isCurrentForest()) { this.serverName = DirectoryContext.GetLoggedOnDomain(); break; } else { this.serverName = DirectoryContext.GetDnsDomainName(this.name); break; } } case DirectoryContextType.DirectoryServer: { this.serverName = this.name; break; } case DirectoryContextType.ConfigurationSet: { AdamInstance adamInstance = ConfigurationSet.FindAnyAdamInstance(this); try { this.serverName = adamInstance.Name; break; } finally { adamInstance.Dispose(); } } case DirectoryContextType.ApplicationPartition: { this.serverName = this.name; break; } } } return(this.serverName); }
// // 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 int IndexOf(AdamInstance adamInstance) { if (adamInstance == null) { throw new ArgumentNullException("adamInstance"); } for (int i = 0; i < base.InnerList.Count; i++) { AdamInstance instance = (AdamInstance)base.InnerList[i]; if (Utils.Compare(instance.Name, adamInstance.Name) == 0) { return(i); } } return(-1); }
public bool Contains(AdamInstance adamInstance) { if (adamInstance == null) { throw new ArgumentNullException("adamInstance"); } for (int i = 0; i < base.InnerList.Count; i++) { AdamInstance instance = (AdamInstance)base.InnerList[i]; if (Utils.Compare(instance.Name, adamInstance.Name) == 0) { return(true); } } return(false); }
public bool Contains(AdamInstance adamInstance) { if (adamInstance == null) { throw new ArgumentNullException("adamInstance"); } for (int i = 0; i < base.InnerList.Count; i++) { AdamInstance instance = (AdamInstance) base.InnerList[i]; if (Utils.Compare(instance.Name, adamInstance.Name) == 0) { return true; } } return false; }
public int IndexOf(AdamInstance adamInstance) { if (adamInstance == null) { throw new ArgumentNullException("adamInstance"); } for (int i = 0; i < base.InnerList.Count; i++) { AdamInstance instance = (AdamInstance) base.InnerList[i]; if (Utils.Compare(instance.Name, adamInstance.Name) == 0) { return i; } } return -1; }
public bool Contains(AdamInstance adamInstance) { if (adamInstance == null) { throw new ArgumentNullException(nameof(adamInstance)); } for (int i = 0; i < InnerList.Count; i++) { AdamInstance tmp = (AdamInstance)InnerList[i] !; if (Utils.Compare(tmp.Name, adamInstance.Name) == 0) { return(true); } } return(false); }
public int IndexOf(AdamInstance adamInstance) { if (adamInstance == null) { throw new ArgumentNullException(nameof(adamInstance)); } for (int i = 0; i < InnerList.Count; i++) { AdamInstance tmp = (AdamInstance)InnerList[i]; if (Utils.Compare(tmp.Name, adamInstance.Name) == 0) { return(i); } } return(-1); }
private DirectoryServer GetSchemaRoleOwner() { try { _schemaEntry.RefreshCache(); if (context.isADAMConfigSet()) { // ADAM string adamInstName = Utils.GetAdamDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, _schemaEntry, PropertyManager.FsmoRoleOwner)); DirectoryContext adamInstContext = Utils.GetNewDirectoryContext(adamInstName, DirectoryContextType.DirectoryServer, context); return(new AdamInstance(adamInstContext, adamInstName)); } else { // could be AD or adam server DirectoryServer server = null; DirectoryEntry rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE); if (Utils.CheckCapability(rootDSE, Capability.ActiveDirectory)) { string dcName = Utils.GetDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, _schemaEntry, PropertyManager.FsmoRoleOwner)); DirectoryContext dcContext = Utils.GetNewDirectoryContext(dcName, DirectoryContextType.DirectoryServer, context); server = new DomainController(dcContext, dcName); } else { // ADAM case again string adamInstName = Utils.GetAdamDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, _schemaEntry, PropertyManager.FsmoRoleOwner)); DirectoryContext adamInstContext = Utils.GetNewDirectoryContext(adamInstName, DirectoryContextType.DirectoryServer, context); server = new AdamInstance(adamInstContext, adamInstName); } return(server); } } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } }
internal string GetServerName() { if (this.serverName == null) { switch (this.contextType) { case DirectoryContextType.Domain: case DirectoryContextType.Forest: goto Label_004A; case DirectoryContextType.DirectoryServer: this.serverName = this.name; break; case DirectoryContextType.ConfigurationSet: using (AdamInstance instance = ConfigurationSet.FindAnyAdamInstance(this)) { this.serverName = instance.Name; break; } goto Label_004A; case DirectoryContextType.ApplicationPartition: this.serverName = this.name; break; } } goto Label_009D; Label_004A: if ((this.name == null) || ((this.contextType == DirectoryContextType.Forest) && this.isCurrentForest())) { this.serverName = GetLoggedOnDomain(); } else { this.serverName = GetDnsDomainName(this.name); } Label_009D: return(this.serverName); }
private DirectoryServer GetSchemaRoleOwner() { DirectoryServer adamInstance; DirectoryServer directoryServer; try { this.schemaEntry.RefreshCache(); if (!this.context.isADAMConfigSet()) { DirectoryEntry cachedDirectoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE); if (!Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectory)) { string adamDnsHostNameFromNTDSA = Utils.GetAdamDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, this.schemaEntry, PropertyManager.FsmoRoleOwner)); DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(adamDnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, this.context); adamInstance = new AdamInstance(newDirectoryContext, adamDnsHostNameFromNTDSA); } else { string dnsHostNameFromNTDSA = Utils.GetDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, this.schemaEntry, PropertyManager.FsmoRoleOwner)); DirectoryContext directoryContext = Utils.GetNewDirectoryContext(dnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, this.context); adamInstance = new DomainController(directoryContext, dnsHostNameFromNTDSA); } directoryServer = adamInstance; } else { string str = Utils.GetAdamDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, this.schemaEntry, PropertyManager.FsmoRoleOwner)); DirectoryContext newDirectoryContext1 = Utils.GetNewDirectoryContext(str, DirectoryContextType.DirectoryServer, this.context); directoryServer = new AdamInstance(newDirectoryContext1, str); } } catch (COMException cOMException1) { COMException cOMException = cOMException1; throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException); } return(directoryServer); }
public int IndexOf(AdamInstance adamInstance) { if (adamInstance != null) { int num = 0; while (num < base.InnerList.Count) { AdamInstance item = (AdamInstance)base.InnerList[num]; if (Utils.Compare(item.Name, adamInstance.Name) != 0) { num++; } else { return(num); } } return(-1); } else { throw new ArgumentNullException("adamInstance"); } }
public int IndexOf(AdamInstance adamInstance) { if (adamInstance != null) { int num = 0; while (num < base.InnerList.Count) { AdamInstance item = (AdamInstance)base.InnerList[num]; if (Utils.Compare(item.Name, adamInstance.Name) != 0) { num++; } else { return num; } } return -1; } else { throw new ArgumentNullException("adamInstance"); } }
private void GetServers() { ADSearcher adSearcher = new ADSearcher(cachedEntry, "(&(objectClass=server)(objectCategory=server))", new string[] { "dNSHostName" }, SearchScope.Subtree); SearchResultCollection results = null; try { results = adSearcher.FindAll(); } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } try { foreach (SearchResult result in results) { string hostName = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsHostName); DirectoryEntry de = result.GetDirectoryEntry(); DirectoryEntry child = null; DirectoryServer replica = null; // make sure that the server is not demoted try { child = de.Children.Find("CN=NTDS Settings", "nTDSDSA"); } catch (COMException e) { if (e.ErrorCode == unchecked((int)0x80072030)) { continue; } else throw ExceptionHelper.GetExceptionFromCOMException(context, e); } if (IsADAM) { int port = (int)PropertyManager.GetPropertyValue(context, child, PropertyManager.MsDSPortLDAP); string fullHostName = hostName; if (port != 389) { fullHostName = hostName + ":" + port; } replica = new AdamInstance(Utils.GetNewDirectoryContext(fullHostName, DirectoryContextType.DirectoryServer, context), fullHostName); } else replica = new DomainController(Utils.GetNewDirectoryContext(hostName, DirectoryContextType.DirectoryServer, context), hostName); _servers.Add(replica); } } finally { results.Dispose(); } }
internal unsafe void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string?sourceServer, int option, SafeLibraryHandle libHandle) { int structSize = Marshal.SizeOf(typeof(Guid)); IntPtr unmanagedGuid = (IntPtr)0; Guid guid = Guid.Empty; AdamInstance? adamServer = null; DomainController?dcServer = null; unmanagedGuid = Marshal.AllocHGlobal(structSize); try { if (sourceServer != null) { DirectoryContext newContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, context); if (isADAM) { adamServer = AdamInstance.GetAdamInstance(newContext); guid = adamServer.NtdsaObjectGuid; } else { dcServer = DomainController.GetDomainController(newContext); guid = dcServer.NtdsaObjectGuid; } Marshal.StructureToPtr(guid, unmanagedGuid, false); } // call DsReplicaSyncW var dsReplicaSyncW = (delegate * unmanaged <IntPtr, char *, IntPtr, int, int>)global::Interop.Kernel32.GetProcAddress(libHandle, "DsReplicaSyncW"); if (dsReplicaSyncW == null) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastPInvokeError()); } fixed(char *partitionPtr = partition) { int result = dsReplicaSyncW(dsHandle, partitionPtr, unmanagedGuid, (int)option); // check the result if (result != 0) { if (!Partitions.Contains(partition)) { throw new ArgumentException(SR.ServerNotAReplica, nameof(partition)); } string?serverDownName = null; // this is the error returned when the server that we want to sync from is down if (result == ExceptionHelper.RPC_S_SERVER_UNAVAILABLE) { serverDownName = sourceServer; } // this is the error returned when the server that we want to get synced is down else if (result == ExceptionHelper.RPC_S_CALL_FAILED) { serverDownName = Name; } throw ExceptionHelper.GetExceptionFromErrorCode(result, serverDownName); } } } finally { if (unmanagedGuid != (IntPtr)0) { Marshal.FreeHGlobal(unmanagedGuid); } adamServer?.Dispose(); dcServer?.Dispose(); } }
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 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; }
private ReadOnlyDirectoryServerCollection GetBridgeheadServers() { NativeComInterfaces.IAdsPathname pathCracker = (NativeComInterfaces.IAdsPathname)new NativeComInterfaces.Pathname(); // need to turn off the escaping for name pathCracker.EscapedMode = NativeComInterfaces.ADS_ESCAPEDMODE_OFF_EX; ReadOnlyDirectoryServerCollection collection = new ReadOnlyDirectoryServerCollection(); if (existing) { Hashtable bridgeHeadTable = new Hashtable(); Hashtable nonBridgHeadTable = new Hashtable(); Hashtable hostNameTable = new Hashtable(); const string ocValue = "CN=Server"; // get destination bridgehead servers // first go to the servers container under the current site and then do a search to get the all server objects. string serverContainer = "CN=Servers," + (string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.DistinguishedName); DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(context, serverContainer); try { // go through connection objects and find out its fromServer property. ADSearcher adSearcher = new ADSearcher(de, "(|(objectCategory=server)(objectCategory=NTDSConnection))", new string[] { "fromServer", "distinguishedName", "dNSHostName", "objectCategory" }, SearchScope.Subtree, true, /* need paged search */ true /* need cached result as we need to go back to the first record */); SearchResultCollection conResults = null; try { conResults = adSearcher.FindAll(); } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } try { // find out whether fromServer indicates replicating from a server in another site. foreach (SearchResult r in conResults) { string objectCategoryValue = (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.ObjectCategory); if (Utils.Compare(objectCategoryValue, 0, ocValue.Length, ocValue, 0, ocValue.Length) == 0) { hostNameTable.Add((string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.DistinguishedName), (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.DnsHostName)); } } foreach (SearchResult r in conResults) { string objectCategoryValue = (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.ObjectCategory); if (Utils.Compare(objectCategoryValue, 0, ocValue.Length, ocValue, 0, ocValue.Length) != 0) { string fromServer = (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.FromServer); // escaping manipulation string fromSite = Utils.GetPartialDN(fromServer, 3); pathCracker.Set(fromSite, NativeComInterfaces.ADS_SETTYPE_DN); fromSite = pathCracker.Retrieve(NativeComInterfaces.ADS_FORMAT_LEAF); Debug.Assert(fromSite != null && Utils.Compare(fromSite, 0, 3, "CN=", 0, 3) == 0); fromSite = fromSite.Substring(3); string serverObjectName = Utils.GetPartialDN((string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.DistinguishedName), 2); // don't know whether it is a bridgehead server yet. if (!bridgeHeadTable.Contains(serverObjectName)) { string hostName = (string)hostNameTable[serverObjectName]; // add if not yet done if (!nonBridgHeadTable.Contains(serverObjectName)) nonBridgHeadTable.Add(serverObjectName, hostName); // check whether from different site if (Utils.Compare((string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.Cn), fromSite) != 0) { // the server is a bridgehead server bridgeHeadTable.Add(serverObjectName, hostName); nonBridgHeadTable.Remove(serverObjectName); } } } } } finally { conResults.Dispose(); } } finally { de.Dispose(); } // get source bridgehead server if (nonBridgHeadTable.Count != 0) { // go to sites container to get all the connecdtion object that replicates from servers in the current sites that have // not been determined whether it is a bridgehead server or not. DirectoryEntry serverEntry = DirectoryEntryManager.GetDirectoryEntry(context, _siteDN); // constructing the filter StringBuilder str = new StringBuilder(100); if (nonBridgHeadTable.Count > 1) str.Append("(|"); foreach (DictionaryEntry val in nonBridgHeadTable) { str.Append("(fromServer="); str.Append("CN=NTDS Settings,"); str.Append(Utils.GetEscapedFilterValue((string)val.Key)); str.Append(")"); } if (nonBridgHeadTable.Count > 1) str.Append(")"); ADSearcher adSearcher = new ADSearcher(serverEntry, "(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)" + str.ToString() + ")", new string[] { "fromServer", "distinguishedName" }, SearchScope.Subtree); SearchResultCollection conResults = null; try { conResults = adSearcher.FindAll(); } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } try { foreach (SearchResult r in conResults) { string fromServer = (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.FromServer); string serverObject = fromServer.Substring(17); if (nonBridgHeadTable.Contains(serverObject)) { string otherSite = Utils.GetPartialDN((string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.DistinguishedName), 4); // escaping manipulation pathCracker.Set(otherSite, NativeComInterfaces.ADS_SETTYPE_DN); otherSite = pathCracker.Retrieve(NativeComInterfaces.ADS_FORMAT_LEAF); Debug.Assert(otherSite != null && Utils.Compare(otherSite, 0, 3, "CN=", 0, 3) == 0); otherSite = otherSite.Substring(3); // check whether from different sites if (Utils.Compare(otherSite, (string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.Cn)) != 0) { string val = (string)nonBridgHeadTable[serverObject]; nonBridgHeadTable.Remove(serverObject); bridgeHeadTable.Add(serverObject, val); } } } } finally { conResults.Dispose(); serverEntry.Dispose(); } } DirectoryEntry ADAMEntry = null; foreach (DictionaryEntry e in bridgeHeadTable) { DirectoryServer replica = null; string host = (string)e.Value; // construct directoryreplica if (IsADAM) { ADAMEntry = DirectoryEntryManager.GetDirectoryEntry(context, "CN=NTDS Settings," + e.Key); int port = (int)PropertyManager.GetPropertyValue(context, ADAMEntry, PropertyManager.MsDSPortLDAP); string fullhost = host; if (port != 389) { fullhost = host + ":" + port; } replica = new AdamInstance(Utils.GetNewDirectoryContext(fullhost, DirectoryContextType.DirectoryServer, context), fullhost); } else { replica = new DomainController(Utils.GetNewDirectoryContext(host, DirectoryContextType.DirectoryServer, context), host); } collection.Add(replica); } } return collection; }
internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle) { int cb = Marshal.SizeOf(typeof(Guid)); IntPtr zero = IntPtr.Zero; Guid empty = Guid.Empty; AdamInstance adamInstance = null; DomainController domainController = null; zero = Marshal.AllocHGlobal(cb); try { if (sourceServer != null) { DirectoryContext context = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, this.context); if (isADAM) { adamInstance = AdamInstance.GetAdamInstance(context); empty = adamInstance.NtdsaObjectGuid; } else { domainController = DomainController.GetDomainController(context); empty = domainController.NtdsaObjectGuid; } Marshal.StructureToPtr(empty, zero, false); } IntPtr procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW"); if (procAddress == IntPtr.Zero) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaSyncW delegateForFunctionPointer = (System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaSyncW)); int errorCode = delegateForFunctionPointer(dsHandle, partition, zero, option); if (errorCode != 0) { if (!this.Partitions.Contains(partition)) { throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition"); } string targetName = null; if (errorCode == ExceptionHelper.RPC_S_SERVER_UNAVAILABLE) { targetName = sourceServer; } else if (errorCode == ExceptionHelper.RPC_S_CALL_FAILED) { targetName = this.Name; } throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, targetName); } } finally { if (zero != IntPtr.Zero) { Marshal.FreeHGlobal(zero); } if (adamInstance != null) { adamInstance.Dispose(); } if (domainController != null) { domainController.Dispose(); } } }
private void GetPreferredBridgeheadServers(ActiveDirectoryTransportType transport) { string str; DirectoryServer domainController; string str1 = string.Concat("CN=Servers,", PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.DistinguishedName)); if (transport != ActiveDirectoryTransportType.Smtp) { str = string.Concat("CN=IP,CN=Inter-Site Transports,", this.siteDN); } else { str = string.Concat("CN=SMTP,CN=Inter-Site Transports,", this.siteDN); } DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, str1); string[] strArrays = new string[2]; strArrays[0] = "dNSHostName"; strArrays[1] = "distinguishedName"; ADSearcher aDSearcher = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=server)(objectCategory=Server)(bridgeheadTransportList=", Utils.GetEscapedFilterValue(str), "))"), strArrays, SearchScope.OneLevel); SearchResultCollection searchResultCollections = null; try { searchResultCollections = aDSearcher.FindAll(); } catch (COMException cOMException1) { COMException cOMException = cOMException1; throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException); } try { DirectoryEntry directoryEntry1 = null; foreach (SearchResult searchResult in searchResultCollections) { string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DnsHostName); DirectoryEntry directoryEntry2 = searchResult.GetDirectoryEntry(); try { directoryEntry1 = directoryEntry2.Children.Find("CN=NTDS Settings", "nTDSDSA"); } catch (COMException cOMException3) { COMException cOMException2 = cOMException3; throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException2); } if (!this.IsADAM) { domainController = new DomainController(Utils.GetNewDirectoryContext(searchResultPropertyValue, DirectoryContextType.DirectoryServer, this.context), searchResultPropertyValue); } else { int propertyValue = (int)PropertyManager.GetPropertyValue(this.context, directoryEntry1, PropertyManager.MsDSPortLDAP); string str2 = searchResultPropertyValue; if (propertyValue != 0x185) { str2 = string.Concat(searchResultPropertyValue, ":", propertyValue); } domainController = new AdamInstance(Utils.GetNewDirectoryContext(str2, DirectoryContextType.DirectoryServer, this.context), str2); } if (transport != ActiveDirectoryTransportType.Smtp) { this.RPCBridgeheadServers.Add(domainController); } else { this.SMTPBridgeheadServers.Add(domainController); } } } finally { directoryEntry.Dispose(); searchResultCollections.Dispose(); } }
internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle) { Guid ntdsaObjectGuid; int num = Marshal.SizeOf(typeof(Guid)); AdamInstance adamInstance = null; DomainController domainController = null; IntPtr intPtr = Marshal.AllocHGlobal(num); try { if (sourceServer != null) { DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, this.context); if (!isADAM) { domainController = DomainController.GetDomainController(newDirectoryContext); ntdsaObjectGuid = domainController.NtdsaObjectGuid; } else { adamInstance = AdamInstance.GetAdamInstance(newDirectoryContext); ntdsaObjectGuid = adamInstance.NtdsaObjectGuid; } Marshal.StructureToPtr(ntdsaObjectGuid, intPtr, false); } IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW"); if (procAddress != (IntPtr)0) { UnsafeNativeMethods.DsReplicaSyncW delegateForFunctionPointer = (UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsReplicaSyncW)); int num1 = delegateForFunctionPointer(dsHandle, partition, intPtr, option); if (num1 != 0) { if (this.Partitions.Contains(partition)) { string name = null; if (num1 != ExceptionHelper.RPC_S_SERVER_UNAVAILABLE) { if (num1 == ExceptionHelper.RPC_S_CALL_FAILED) { name = this.Name; } } else { name = sourceServer; } throw ExceptionHelper.GetExceptionFromErrorCode(num1, name); } else { throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition"); } } } else { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } } finally { if (intPtr != (IntPtr)0) { Marshal.FreeHGlobal(intPtr); } if (adamInstance != null) { adamInstance.Dispose(); } if (domainController != null) { domainController.Dispose(); } } }
private DirectoryServer GetSchemaRoleOwner() { DirectoryServer server2; try { this.schemaEntry.RefreshCache(); if (base.context.isADAMConfigSet()) { string adamDnsHostNameFromNTDSA = Utils.GetAdamDnsHostNameFromNTDSA(base.context, (string) PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner)); return new AdamInstance(Utils.GetNewDirectoryContext(adamDnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, base.context), adamDnsHostNameFromNTDSA); } DirectoryServer server = null; if (Utils.CheckCapability(base.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE), Capability.ActiveDirectory)) { string dnsHostNameFromNTDSA = Utils.GetDnsHostNameFromNTDSA(base.context, (string) PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner)); server = new DomainController(Utils.GetNewDirectoryContext(dnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, base.context), dnsHostNameFromNTDSA); } else { string adamInstanceName = Utils.GetAdamDnsHostNameFromNTDSA(base.context, (string) PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner)); server = new AdamInstance(Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, base.context), adamInstanceName); } server2 = server; } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception); } return server2; }
public bool Contains(AdamInstance adamInstance) { Contract.Requires(adamInstance != null); return(default(bool)); }
private void GetPreferredBridgeheadServers(ActiveDirectoryTransportType transport) { string dn = "CN=Servers," + PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.DistinguishedName); string filterValue = null; if (transport == ActiveDirectoryTransportType.Smtp) { filterValue = "CN=SMTP,CN=Inter-Site Transports," + this.siteDN; } else { filterValue = "CN=IP,CN=Inter-Site Transports," + this.siteDN; } DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, dn); ADSearcher searcher = new ADSearcher(directoryEntry, "(&(objectClass=server)(objectCategory=Server)(bridgeheadTransportList=" + Utils.GetEscapedFilterValue(filterValue) + "))", new string[] { "dNSHostName", "distinguishedName" }, SearchScope.OneLevel); SearchResultCollection results = null; try { results = searcher.FindAll(); } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception); } try { DirectoryEntry entry2 = null; foreach (SearchResult result in results) { string searchResultPropertyValue = (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsHostName); DirectoryEntry entry3 = result.GetDirectoryEntry(); DirectoryServer server = null; try { entry2 = entry3.Children.Find("CN=NTDS Settings", "nTDSDSA"); } catch (COMException exception2) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception2); } if (this.IsADAM) { int num = (int) PropertyManager.GetPropertyValue(this.context, entry2, PropertyManager.MsDSPortLDAP); string adamInstanceName = searchResultPropertyValue; if (num != 0x185) { adamInstanceName = searchResultPropertyValue + ":" + num; } server = new AdamInstance(Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, this.context), adamInstanceName); } else { server = new DomainController(Utils.GetNewDirectoryContext(searchResultPropertyValue, DirectoryContextType.DirectoryServer, this.context), searchResultPropertyValue); } if (transport == ActiveDirectoryTransportType.Smtp) { this.SMTPBridgeheadServers.Add(server); } else { this.RPCBridgeheadServers.Add(server); } } } finally { directoryEntry.Dispose(); results.Dispose(); } }
internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle) { int structSize = Marshal.SizeOf(typeof(Guid)); IntPtr unmanagedGuid = (IntPtr)0; Guid guid = Guid.Empty; AdamInstance adamServer = null; DomainController dcServer = null; unmanagedGuid = Marshal.AllocHGlobal(structSize); try { if (sourceServer != null) { DirectoryContext newContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, context); if (isADAM) { adamServer = AdamInstance.GetAdamInstance(newContext); guid = adamServer.NtdsaObjectGuid; } else { dcServer = DomainController.GetDomainController(newContext); guid = dcServer.NtdsaObjectGuid; } Marshal.StructureToPtr(guid, unmanagedGuid, false); } // call DsReplicaSyncW IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW"); if (functionPtr == (IntPtr)0) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } UnsafeNativeMethods.DsReplicaSyncW dsReplicaSyncW = (UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaSyncW)); int result = dsReplicaSyncW(dsHandle, partition, unmanagedGuid, (int)option); // check the result if (result != 0) { if (!Partitions.Contains(partition)) { throw new ArgumentException(SR.ServerNotAReplica, "partition"); } string serverDownName = null; // this is the error returned when the server that we want to sync from is down if (result == ExceptionHelper.RPC_S_SERVER_UNAVAILABLE) { serverDownName = sourceServer; } // this is the error returned when the server that we want to get synced is down else if (result == ExceptionHelper.RPC_S_CALL_FAILED) { serverDownName = Name; } throw ExceptionHelper.GetExceptionFromErrorCode(result, serverDownName); } } finally { if (unmanagedGuid != (IntPtr)0) { Marshal.FreeHGlobal(unmanagedGuid); } if (adamServer != null) { adamServer.Dispose(); } if (dcServer != null) { dcServer.Dispose(); } } }
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 void CopyTo(AdamInstance[] adamInstances, int index) { base.InnerList.CopyTo(adamInstances, index); }
private ReadOnlyDirectoryServerCollection GetBridgeheadServers() { DirectoryServer domainController; NativeComInterfaces.IAdsPathname pathname = (NativeComInterfaces.IAdsPathname)(new NativeComInterfaces.Pathname()); pathname.EscapedMode = 4; ReadOnlyDirectoryServerCollection readOnlyDirectoryServerCollection = new ReadOnlyDirectoryServerCollection(); if (this.existing) { Hashtable hashtables = new Hashtable(); Hashtable hashtables1 = new Hashtable(); Hashtable hashtables2 = new Hashtable(); string str = string.Concat("CN=Servers,", (string)PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.DistinguishedName)); DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, str); try { string[] strArrays = new string[4]; strArrays[0] = "fromServer"; strArrays[1] = "distinguishedName"; strArrays[2] = "dNSHostName"; strArrays[3] = "objectCategory"; ADSearcher aDSearcher = new ADSearcher(directoryEntry, "(|(objectCategory=server)(objectCategory=NTDSConnection))", strArrays, SearchScope.Subtree, true, true); SearchResultCollection searchResultCollections = null; try { searchResultCollections = aDSearcher.FindAll(); } catch (COMException cOMException1) { COMException cOMException = cOMException1; throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException); } try { foreach (SearchResult searchResult in searchResultCollections) { string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.ObjectCategory); if (Utils.Compare(searchResultPropertyValue, 0, "CN=Server".Length, "CN=Server", 0, "CN=Server".Length) != 0) { continue; } hashtables2.Add((string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DistinguishedName), (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DnsHostName)); } foreach (SearchResult searchResult1 in searchResultCollections) { string searchResultPropertyValue1 = (string)PropertyManager.GetSearchResultPropertyValue(searchResult1, PropertyManager.ObjectCategory); if (Utils.Compare(searchResultPropertyValue1, 0, "CN=Server".Length, "CN=Server", 0, "CN=Server".Length) == 0) { continue; } string str1 = (string)PropertyManager.GetSearchResultPropertyValue(searchResult1, PropertyManager.FromServer); string partialDN = Utils.GetPartialDN(str1, 3); pathname.Set(partialDN, 4); partialDN = pathname.Retrieve(11); partialDN = partialDN.Substring(3); string partialDN1 = Utils.GetPartialDN((string)PropertyManager.GetSearchResultPropertyValue(searchResult1, PropertyManager.DistinguishedName), 2); if (hashtables.Contains(partialDN1)) { continue; } string item = (string)hashtables2[partialDN1]; if (!hashtables1.Contains(partialDN1)) { hashtables1.Add(partialDN1, item); } if (Utils.Compare((string)PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.Cn), partialDN) == 0) { continue; } hashtables.Add(partialDN1, item); hashtables1.Remove(partialDN1); } } finally { searchResultCollections.Dispose(); } } finally { directoryEntry.Dispose(); } if (hashtables1.Count != 0) { DirectoryEntry directoryEntry1 = DirectoryEntryManager.GetDirectoryEntry(this.context, this.siteDN); StringBuilder stringBuilder = new StringBuilder(100); if (hashtables1.Count > 1) { stringBuilder.Append("(|"); } foreach (DictionaryEntry hashtable in hashtables1) { stringBuilder.Append("(fromServer="); stringBuilder.Append("CN=NTDS Settings,"); stringBuilder.Append(Utils.GetEscapedFilterValue((string)hashtable.Key)); stringBuilder.Append(")"); } if (hashtables1.Count > 1) { stringBuilder.Append(")"); } string[] strArrays1 = new string[2]; strArrays1[0] = "fromServer"; strArrays1[1] = "distinguishedName"; ADSearcher aDSearcher1 = new ADSearcher(directoryEntry1, string.Concat("(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)", stringBuilder.ToString(), ")"), strArrays1, SearchScope.Subtree); SearchResultCollection searchResultCollections1 = null; try { searchResultCollections1 = aDSearcher1.FindAll(); } catch (COMException cOMException3) { COMException cOMException2 = cOMException3; throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException2); } try { foreach (SearchResult searchResult2 in searchResultCollections1) { string searchResultPropertyValue2 = (string)PropertyManager.GetSearchResultPropertyValue(searchResult2, PropertyManager.FromServer); string str2 = searchResultPropertyValue2.Substring(17); if (!hashtables1.Contains(str2)) { continue; } string partialDN2 = Utils.GetPartialDN((string)PropertyManager.GetSearchResultPropertyValue(searchResult2, PropertyManager.DistinguishedName), 4); pathname.Set(partialDN2, 4); partialDN2 = pathname.Retrieve(11); partialDN2 = partialDN2.Substring(3); if (Utils.Compare(partialDN2, (string)PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.Cn)) == 0) { continue; } string item1 = (string)hashtables1[str2]; hashtables1.Remove(str2); hashtables.Add(str2, item1); } } finally { searchResultCollections1.Dispose(); directoryEntry1.Dispose(); } } foreach (DictionaryEntry dictionaryEntry in hashtables) { string value = (string)dictionaryEntry.Value; if (!this.IsADAM) { domainController = new DomainController(Utils.GetNewDirectoryContext(value, DirectoryContextType.DirectoryServer, this.context), value); } else { DirectoryEntry directoryEntry2 = DirectoryEntryManager.GetDirectoryEntry(this.context, string.Concat("CN=NTDS Settings,", dictionaryEntry.Key)); int propertyValue = (int)PropertyManager.GetPropertyValue(this.context, directoryEntry2, PropertyManager.MsDSPortLDAP); string str3 = value; if (propertyValue != 0x185) { str3 = string.Concat(value, ":", propertyValue); } domainController = new AdamInstance(Utils.GetNewDirectoryContext(str3, DirectoryContextType.DirectoryServer, this.context), str3); } readOnlyDirectoryServerCollection.Add(domainController); } } return readOnlyDirectoryServerCollection; }
private DirectoryServer GetSchemaRoleOwner() { DirectoryServer adamInstance; DirectoryServer directoryServer; try { this.schemaEntry.RefreshCache(); if (!this.context.isADAMConfigSet()) { DirectoryEntry cachedDirectoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE); if (!Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectory)) { string adamDnsHostNameFromNTDSA = Utils.GetAdamDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, this.schemaEntry, PropertyManager.FsmoRoleOwner)); DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(adamDnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, this.context); adamInstance = new AdamInstance(newDirectoryContext, adamDnsHostNameFromNTDSA); } else { string dnsHostNameFromNTDSA = Utils.GetDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, this.schemaEntry, PropertyManager.FsmoRoleOwner)); DirectoryContext directoryContext = Utils.GetNewDirectoryContext(dnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, this.context); adamInstance = new DomainController(directoryContext, dnsHostNameFromNTDSA); } directoryServer = adamInstance; } else { string str = Utils.GetAdamDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, this.schemaEntry, PropertyManager.FsmoRoleOwner)); DirectoryContext newDirectoryContext1 = Utils.GetNewDirectoryContext(str, DirectoryContextType.DirectoryServer, this.context); directoryServer = new AdamInstance(newDirectoryContext1, str); } } catch (COMException cOMException1) { COMException cOMException = cOMException1; throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException); } return directoryServer; }
// // 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); }
private void GetServers() { ADSearcher searcher = new ADSearcher(this.cachedEntry, "(&(objectClass=server)(objectCategory=server))", new string[] { "dNSHostName" }, SearchScope.Subtree); SearchResultCollection results = null; try { results = searcher.FindAll(); } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception); } try { foreach (SearchResult result in results) { string searchResultPropertyValue = (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsHostName); DirectoryEntry directoryEntry = result.GetDirectoryEntry(); DirectoryEntry entry2 = null; DirectoryServer server = null; try { entry2 = directoryEntry.Children.Find("CN=NTDS Settings", "nTDSDSA"); } catch (COMException exception2) { if (exception2.ErrorCode != -2147016656) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception2); } continue; } if (this.IsADAM) { int num = (int) PropertyManager.GetPropertyValue(this.context, entry2, PropertyManager.MsDSPortLDAP); string adamInstanceName = searchResultPropertyValue; if (num != 0x185) { adamInstanceName = searchResultPropertyValue + ":" + num; } server = new AdamInstance(Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, this.context), adamInstanceName); } else { server = new DomainController(Utils.GetNewDirectoryContext(searchResultPropertyValue, DirectoryContextType.DirectoryServer, this.context), searchResultPropertyValue); } this.servers.Add(server); } } finally { results.Dispose(); } }
internal string GetServerName() { if (serverName == null) { switch (_contextType) { case DirectoryContextType.ConfigurationSet: { AdamInstance adamInst = ConfigurationSet.FindAnyAdamInstance(this); try { serverName = adamInst.Name; } finally { adamInst.Dispose(); } break; } case DirectoryContextType.Domain: case DirectoryContextType.Forest: { // // if the target is not specified OR // if the forest name was explicitly specified and the forest is the same as the current forest // we want to find a DC in the current domain // if ((_name == null) || ((_contextType == DirectoryContextType.Forest) && (isCurrentForest()))) { serverName = GetLoggedOnDomain(); } else { serverName = GetDnsDomainName(_name); } break; } case DirectoryContextType.ApplicationPartition: { // if this is an appNC the target should not be null Debug.Assert(_name != null); serverName = _name; break; } case DirectoryContextType.DirectoryServer: { // this should not happen (We should have checks for this earlier itself) Debug.Assert(_name != null); serverName = _name; break; } default: { Debug.Fail("DirectoryContext::GetServerName - Unknown contextType"); break; } } } return(serverName); }
private void GetServers() { DirectoryServer domainController; string[] strArrays = new string[1]; strArrays[0] = "dNSHostName"; ADSearcher aDSearcher = new ADSearcher(this.cachedEntry, "(&(objectClass=server)(objectCategory=server))", strArrays, SearchScope.Subtree); SearchResultCollection searchResultCollections = null; try { searchResultCollections = aDSearcher.FindAll(); } catch (COMException cOMException1) { COMException cOMException = cOMException1; throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException); } try { foreach (SearchResult searchResult in searchResultCollections) { string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DnsHostName); DirectoryEntry directoryEntry = searchResult.GetDirectoryEntry(); DirectoryEntry directoryEntry1 = null; try { directoryEntry1 = directoryEntry.Children.Find("CN=NTDS Settings", "nTDSDSA"); } catch (COMException cOMException3) { COMException cOMException2 = cOMException3; if (cOMException2.ErrorCode != -2147016656) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException2); } else { continue; } } if (!this.IsADAM) { domainController = new DomainController(Utils.GetNewDirectoryContext(searchResultPropertyValue, DirectoryContextType.DirectoryServer, this.context), searchResultPropertyValue); } else { int propertyValue = (int)PropertyManager.GetPropertyValue(this.context, directoryEntry1, PropertyManager.MsDSPortLDAP); string str = searchResultPropertyValue; if (propertyValue != 0x185) { str = string.Concat(searchResultPropertyValue, ":", propertyValue); } domainController = new AdamInstance(Utils.GetNewDirectoryContext(str, DirectoryContextType.DirectoryServer, this.context), str); } this.servers.Add(domainController); } } finally { searchResultCollections.Dispose(); } }
private DirectoryServer GetSchemaRoleOwner() { try { _schemaEntry.RefreshCache(); if (context.isADAMConfigSet()) { // ADAM string adamInstName = Utils.GetAdamDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, _schemaEntry, PropertyManager.FsmoRoleOwner)); DirectoryContext adamInstContext = Utils.GetNewDirectoryContext(adamInstName, DirectoryContextType.DirectoryServer, context); return new AdamInstance(adamInstContext, adamInstName); } else { // could be AD or adam server DirectoryServer server = null; DirectoryEntry rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE); if (Utils.CheckCapability(rootDSE, Capability.ActiveDirectory)) { string dcName = Utils.GetDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, _schemaEntry, PropertyManager.FsmoRoleOwner)); DirectoryContext dcContext = Utils.GetNewDirectoryContext(dcName, DirectoryContextType.DirectoryServer, context); server = new DomainController(dcContext, dcName); } else { // ADAM case again string adamInstName = Utils.GetAdamDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, _schemaEntry, PropertyManager.FsmoRoleOwner)); DirectoryContext adamInstContext = Utils.GetNewDirectoryContext(adamInstName, DirectoryContextType.DirectoryServer, context); server = new AdamInstance(adamInstContext, adamInstName); } return server; } } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } }
public bool Contains(AdamInstance adamInstance) { Contract.Requires(adamInstance != null); return default(bool); }
private void GetPreferredBridgeheadServers(ActiveDirectoryTransportType transport) { string serverContainerDN = "CN=Servers," + PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.DistinguishedName); string transportDN = null; if (transport == ActiveDirectoryTransportType.Smtp) transportDN = "CN=SMTP,CN=Inter-Site Transports," + _siteDN; else transportDN = "CN=IP,CN=Inter-Site Transports," + _siteDN; DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(context, serverContainerDN); ADSearcher adSearcher = new ADSearcher(de, "(&(objectClass=server)(objectCategory=Server)(bridgeheadTransportList=" + Utils.GetEscapedFilterValue(transportDN) + "))", new string[] { "dNSHostName", "distinguishedName" }, SearchScope.OneLevel); SearchResultCollection results = null; try { results = adSearcher.FindAll(); } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } try { DirectoryEntry ADAMEntry = null; foreach (SearchResult result in results) { string hostName = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsHostName); DirectoryEntry resultEntry = result.GetDirectoryEntry(); DirectoryServer replica = null; try { ADAMEntry = resultEntry.Children.Find("CN=NTDS Settings", "nTDSDSA"); } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } if (IsADAM) { int port = (int)PropertyManager.GetPropertyValue(context, ADAMEntry, PropertyManager.MsDSPortLDAP); string fullHostName = hostName; if (port != 389) { fullHostName = hostName + ":" + port; } replica = new AdamInstance(Utils.GetNewDirectoryContext(fullHostName, DirectoryContextType.DirectoryServer, context), fullHostName); } else replica = new DomainController(Utils.GetNewDirectoryContext(hostName, DirectoryContextType.DirectoryServer, context), hostName); if (transport == ActiveDirectoryTransportType.Smtp) _SMTPBridgeheadServers.Add(replica); else _RPCBridgeheadServers.Add(replica); } } finally { de.Dispose(); results.Dispose(); } }
public void CopyTo(AdamInstance[] adamInstances, int index) { Contract.Requires(adamInstances != null); }
private ReadOnlyDirectoryServerCollection GetBridgeheadServers() { NativeComInterfaces.IAdsPathname pathname = (NativeComInterfaces.IAdsPathname) new NativeComInterfaces.Pathname(); pathname.EscapedMode = 4; ReadOnlyDirectoryServerCollection servers = new ReadOnlyDirectoryServerCollection(); if (this.existing) { Hashtable hashtable = new Hashtable(); Hashtable hashtable2 = new Hashtable(); Hashtable hashtable3 = new Hashtable(); string dn = "CN=Servers," + ((string) PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.DistinguishedName)); using (DirectoryEntry entry = DirectoryEntryManager.GetDirectoryEntry(this.context, dn)) { ADSearcher searcher = new ADSearcher(entry, "(|(objectCategory=server)(objectCategory=NTDSConnection))", new string[] { "fromServer", "distinguishedName", "dNSHostName", "objectCategory" }, SearchScope.Subtree, true, true); SearchResultCollection results = null; try { results = searcher.FindAll(); } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception); } try { foreach (SearchResult result in results) { string searchResultPropertyValue = (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.ObjectCategory); if (Utils.Compare(searchResultPropertyValue, 0, "CN=Server".Length, "CN=Server", 0, "CN=Server".Length) == 0) { hashtable3.Add((string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DistinguishedName), (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsHostName)); } } foreach (SearchResult result2 in results) { string str3 = (string) PropertyManager.GetSearchResultPropertyValue(result2, PropertyManager.ObjectCategory); if (Utils.Compare(str3, 0, "CN=Server".Length, "CN=Server", 0, "CN=Server".Length) != 0) { string distinguishedName = (string) PropertyManager.GetSearchResultPropertyValue(result2, PropertyManager.FromServer); string partialDN = Utils.GetPartialDN(distinguishedName, 3); pathname.Set(partialDN, 4); partialDN = pathname.Retrieve(11).Substring(3); string key = Utils.GetPartialDN((string) PropertyManager.GetSearchResultPropertyValue(result2, PropertyManager.DistinguishedName), 2); if (!hashtable.Contains(key)) { string str7 = (string) hashtable3[key]; if (!hashtable2.Contains(key)) { hashtable2.Add(key, str7); } if (Utils.Compare((string) PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.Cn), partialDN) != 0) { hashtable.Add(key, str7); hashtable2.Remove(key); } } } } } finally { results.Dispose(); } } if (hashtable2.Count != 0) { DirectoryEntry searchRoot = DirectoryEntryManager.GetDirectoryEntry(this.context, this.siteDN); StringBuilder builder = new StringBuilder(100); if (hashtable2.Count > 1) { builder.Append("(|"); } foreach (DictionaryEntry entry3 in hashtable2) { builder.Append("(fromServer="); builder.Append("CN=NTDS Settings,"); builder.Append(Utils.GetEscapedFilterValue((string) entry3.Key)); builder.Append(")"); } if (hashtable2.Count > 1) { builder.Append(")"); } ADSearcher searcher2 = new ADSearcher(searchRoot, "(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)" + builder.ToString() + ")", new string[] { "fromServer", "distinguishedName" }, SearchScope.Subtree); SearchResultCollection results2 = null; try { results2 = searcher2.FindAll(); } catch (COMException exception2) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception2); } try { foreach (SearchResult result3 in results2) { string str9 = ((string) PropertyManager.GetSearchResultPropertyValue(result3, PropertyManager.FromServer)).Substring(0x11); if (hashtable2.Contains(str9)) { string bstrADsPath = Utils.GetPartialDN((string) PropertyManager.GetSearchResultPropertyValue(result3, PropertyManager.DistinguishedName), 4); pathname.Set(bstrADsPath, 4); if (Utils.Compare(pathname.Retrieve(11).Substring(3), (string) PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.Cn)) != 0) { string str11 = (string) hashtable2[str9]; hashtable2.Remove(str9); hashtable.Add(str9, str11); } } } } finally { results2.Dispose(); searchRoot.Dispose(); } } DirectoryEntry directoryEntry = null; foreach (DictionaryEntry entry5 in hashtable) { DirectoryServer server = null; string domainControllerName = (string) entry5.Value; if (this.IsADAM) { directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, "CN=NTDS Settings," + entry5.Key); int num = (int) PropertyManager.GetPropertyValue(this.context, directoryEntry, PropertyManager.MsDSPortLDAP); string adamInstanceName = domainControllerName; if (num != 0x185) { adamInstanceName = domainControllerName + ":" + num; } server = new AdamInstance(Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, this.context), adamInstanceName); } else { server = new DomainController(Utils.GetNewDirectoryContext(domainControllerName, DirectoryContextType.DirectoryServer, this.context), domainControllerName); } servers.Add(server); } } return servers; }