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; }
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 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; }