Example #1
0
		public bool Equals(DistinguishedName dn)
		{
			bool flag = true;
			if (dn == null || this.components.GetLength(0) != dn.Components.GetLength(0))
			{
				flag = false;
			}
			else
			{
				int num = 0;
				while (num < this.components.GetLength(0))
				{
					if (Utils.Compare(this.components[num].Name, dn.Components[num].Name) != 0 || Utils.Compare(this.components[num].Value, dn.Components[num].Value) != 0)
					{
						flag = false;
						break;
					}
					else
					{
						num++;
					}
				}
			}
			return flag;
		}
 public bool Equals(DistinguishedName dn)
 {
     if ((dn == null) || (this.components.GetLength(0) != dn.Components.GetLength(0)))
     {
         return false;
     }
     for (int i = 0; i < this.components.GetLength(0); i++)
     {
         if ((Utils.Compare(this.components[i].Name, dn.Components[i].Name) != 0) || (Utils.Compare(this.components[i].Value, dn.Components[i].Value) != 0))
         {
             return false;
         }
     }
     return true;
 }
Example #3
0
 public bool Equals(DistinguishedName dn)
 {
     bool result = true;
     if ((dn == null) || (_components.GetLength(0) != dn.Components.GetLength(0)))
     {
         result = false;
     }
     else
     {
         for (int i = 0; i < _components.GetLength(0); i++)
         {
             if ((Utils.Compare(_components[i].Name, dn.Components[i].Name) != 0)
                 || (Utils.Compare(_components[i].Value, dn.Components[i].Value) != 0))
             {
                 result = false;
                 break;
             }
         }
     }
     return result;
 }
Example #4
0
        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;
        }
Example #5
0
        internal void RemoveIfExists(string distinguishedName)
        {
            // check if it's not RootDSE
            Object dn = distinguishedName;

            //
            // NOTE: Currently only comparing against "rootdse", but in the future if we are going to 
            //           remove any other entries that are not in dn format (such as schema), we need to add the 
            //           special casing here.
            //

            if (String.Compare(distinguishedName, "rootdse", StringComparison.OrdinalIgnoreCase) != 0)
            {
                dn = new DistinguishedName(distinguishedName);
            }

            if (_directoryEntries.ContainsKey(dn))
            {
                DirectoryEntry tmp = (DirectoryEntry)_directoryEntries[dn];
                if (tmp != null)
                {
                    _directoryEntries.Remove(dn);
                    tmp.Dispose();
                }
            }
        }
Example #6
0
        internal DirectoryEntry GetCachedDirectoryEntry(string distinguishedName)
        {
            // check if it's not RootDSE
            Object dn = distinguishedName;

            if ((String.Compare(distinguishedName, "rootdse", StringComparison.OrdinalIgnoreCase) != 0)
            && (String.Compare(distinguishedName, "schema", StringComparison.OrdinalIgnoreCase) != 0))
            {
                dn = new DistinguishedName(distinguishedName);
            }

            if (!_directoryEntries.ContainsKey(dn))
            {
                // directory entry does not exist
                // create a new one and cache it
                DirectoryEntry de = GetNewDirectoryEntry(distinguishedName);
                // add it to the cache 
                _directoryEntries.Add(dn, de);
            }
            return (DirectoryEntry)_directoryEntries[dn];
        }
Example #7
0
		internal static ArrayList GetReplicaList(DirectoryContext context, string partitionName, string siteName, bool isDefaultNC, bool isADAM, bool isGC)
		{
			string dnsHostNameFromNTDSA;
			string str = null;
			string str1 = null;
			string str2 = null;
			string str3 = null;
			ArrayList arrayLists;
			string[] objectCategory;
			IDisposable disposable;
			IEnumerator enumerator;
			object[] msDSHasInstantiatedNCs;
			ArrayList arrayLists1 = new ArrayList();
			ArrayList arrayLists2 = new ArrayList();
			Hashtable hashtables = new Hashtable();
			Hashtable hashtables1 = new Hashtable();
			StringBuilder stringBuilder = new StringBuilder(10);
			StringBuilder stringBuilder1 = new StringBuilder(10);
			StringBuilder stringBuilder2 = new StringBuilder(10);
			StringBuilder stringBuilder3 = new StringBuilder(10);
			bool flag = false;
			string str4 = null;
			try
			{
				str4 = DirectoryEntryManager.ExpandWellKnownDN(context, WellKnownDN.ConfigurationNamingContext);
			}
			catch (COMException cOMException1)
			{
				COMException cOMException = cOMException1;
				throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
			}
			if (partitionName != null && !isDefaultNC)
			{
				DistinguishedName distinguishedName = new DistinguishedName(partitionName);
				DistinguishedName distinguishedName1 = new DistinguishedName(str4);
				DistinguishedName distinguishedName2 = new DistinguishedName(string.Concat("CN=Schema,", str4));
				if (!distinguishedName1.Equals(distinguishedName) && !distinguishedName2.Equals(distinguishedName))
				{
					flag = true;
				}
			}
			if (flag)
			{
				DirectoryEntry directoryEntry = null;
				DirectoryEntry directoryEntry1 = null;
				try
				{
					try
					{
						directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, string.Concat("CN=Partitions,", str4));
						if (!isADAM)
						{
							dnsHostNameFromNTDSA = Utils.GetDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.FsmoRoleOwner));
						}
						else
						{
							dnsHostNameFromNTDSA = Utils.GetAdamDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.FsmoRoleOwner));
						}
						DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(dnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, context);
						directoryEntry1 = DirectoryEntryManager.GetDirectoryEntry(newDirectoryContext, string.Concat("CN=Partitions,", str4));
						objectCategory = new string[7];
						objectCategory[0] = "(&(";
						objectCategory[1] = PropertyManager.ObjectCategory;
						objectCategory[2] = "=crossRef)(";
						objectCategory[3] = PropertyManager.NCName;
						objectCategory[4] = "=";
						objectCategory[5] = Utils.GetEscapedFilterValue(partitionName);
						objectCategory[6] = "))";
						string str5 = string.Concat(objectCategory);
						ArrayList arrayLists3 = new ArrayList();
						arrayLists3.Add(PropertyManager.MsDSNCReplicaLocations);
						arrayLists3.Add(PropertyManager.MsDSNCROReplicaLocations);
						Hashtable valuesWithRangeRetrieval = null;
						try
						{
							valuesWithRangeRetrieval = Utils.GetValuesWithRangeRetrieval(directoryEntry1, str5, arrayLists3, SearchScope.OneLevel);
						}
						catch (COMException cOMException3)
						{
							COMException cOMException2 = cOMException3;
							throw ExceptionHelper.GetExceptionFromCOMException(newDirectoryContext, cOMException2);
						}
						catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException)
						{
							arrayLists = arrayLists2;
							return arrayLists;
						}
						ArrayList item = (ArrayList)valuesWithRangeRetrieval[PropertyManager.MsDSNCReplicaLocations.ToLower(CultureInfo.InvariantCulture)];
						ArrayList item1 = (ArrayList)valuesWithRangeRetrieval[PropertyManager.MsDSNCROReplicaLocations.ToLower(CultureInfo.InvariantCulture)];
						if (item.Count != 0)
						{
							foreach (string str6 in item)
							{
								stringBuilder.Append("(");
								stringBuilder.Append(PropertyManager.DistinguishedName);
								stringBuilder.Append("=");
								stringBuilder.Append(Utils.GetEscapedFilterValue(str6));
								stringBuilder.Append(")");
								stringBuilder1.Append("(");
								stringBuilder1.Append(PropertyManager.DistinguishedName);
								stringBuilder1.Append("=");
								stringBuilder1.Append(Utils.GetEscapedFilterValue(Utils.GetPartialDN(str6, 1)));
								stringBuilder1.Append(")");
							}
							foreach (string str7 in item1)
							{
								stringBuilder2.Append("(");
								stringBuilder2.Append(PropertyManager.DistinguishedName);
								stringBuilder2.Append("=");
								stringBuilder2.Append(Utils.GetEscapedFilterValue(str7));
								stringBuilder2.Append(")");
								stringBuilder3.Append("(");
								stringBuilder3.Append(PropertyManager.DistinguishedName);
								stringBuilder3.Append("=");
								stringBuilder3.Append(Utils.GetEscapedFilterValue(Utils.GetPartialDN(str7, 1)));
								stringBuilder3.Append(")");
							}
						}
						else
						{
							arrayLists = arrayLists2;
							return arrayLists;
						}
					}
					catch (COMException cOMException5)
					{
						COMException cOMException4 = cOMException5;
						throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException4);
					}
				}
				finally
				{
					if (directoryEntry != null)
					{
						directoryEntry.Dispose();
					}
					if (directoryEntry1 != null)
					{
						directoryEntry1.Dispose();
					}
				}
			}
			DirectoryEntry directoryEntry2 = null;
			using (directoryEntry2)
			{
				if (siteName == null)
				{
					str = string.Concat("CN=Sites,", str4);
				}
				else
				{
					str = string.Concat("CN=Servers,CN=", siteName, ",CN=Sites,", str4);
				}
				directoryEntry2 = DirectoryEntryManager.GetDirectoryEntry(context, str);
				if (stringBuilder.ToString().Length != 0)
				{
					if (!isGC)
					{
						if (stringBuilder2.Length <= 0)
						{
							objectCategory = new string[13];
							objectCategory[0] = "(|(&(";
							objectCategory[1] = PropertyManager.ObjectCategory;
							objectCategory[2] = "=nTDSDSA)(";
							objectCategory[3] = PropertyManager.MsDSHasMasterNCs;
							objectCategory[4] = "=";
							objectCategory[5] = Utils.GetEscapedFilterValue(partitionName);
							objectCategory[6] = ")(|";
							objectCategory[7] = stringBuilder.ToString();
							objectCategory[8] = "))(&(";
							objectCategory[9] = PropertyManager.ObjectCategory;
							objectCategory[10] = "=server)(|";
							objectCategory[11] = stringBuilder1.ToString();
							objectCategory[12] = ")))";
							str1 = string.Concat(objectCategory);
						}
						else
						{
							objectCategory = new string[21];
							objectCategory[0] = "(|(&(";
							objectCategory[1] = PropertyManager.ObjectCategory;
							objectCategory[2] = "=nTDSDSA)(";
							objectCategory[3] = PropertyManager.MsDSHasMasterNCs;
							objectCategory[4] = "=";
							objectCategory[5] = Utils.GetEscapedFilterValue(partitionName);
							objectCategory[6] = ")(|";
							objectCategory[7] = stringBuilder.ToString();
							objectCategory[8] = "))(&(";
							objectCategory[9] = PropertyManager.ObjectCategory;
							objectCategory[10] = "=nTDSDSARO)(|";
							objectCategory[11] = stringBuilder2.ToString();
							objectCategory[12] = "))(&(";
							objectCategory[13] = PropertyManager.ObjectCategory;
							objectCategory[14] = "=server)(|";
							objectCategory[15] = stringBuilder1.ToString();
							objectCategory[16] = "))(&(";
							objectCategory[17] = PropertyManager.ObjectCategory;
							objectCategory[18] = "=server)(|";
							objectCategory[19] = stringBuilder3.ToString();
							objectCategory[20] = ")))";
							str1 = string.Concat(objectCategory);
						}
					}
					else
					{
						if (stringBuilder2.Length <= 0)
						{
							objectCategory = new string[15];
							objectCategory[0] = "(|(&(";
							objectCategory[1] = PropertyManager.ObjectCategory;
							objectCategory[2] = "=nTDSDSA)(";
							objectCategory[3] = PropertyManager.Options;
							objectCategory[4] = ":1.2.840.113556.1.4.804:=1)(";
							objectCategory[5] = PropertyManager.MsDSHasMasterNCs;
							objectCategory[6] = "=";
							objectCategory[7] = Utils.GetEscapedFilterValue(partitionName);
							objectCategory[8] = ")(|";
							objectCategory[9] = stringBuilder.ToString();
							objectCategory[10] = "))(&(";
							objectCategory[11] = PropertyManager.ObjectCategory;
							objectCategory[12] = "=server)(|";
							objectCategory[13] = stringBuilder1.ToString();
							objectCategory[14] = ")))";
							str1 = string.Concat(objectCategory);
						}
						else
						{
							objectCategory = new string[25];
							objectCategory[0] = "(|(&(";
							objectCategory[1] = PropertyManager.ObjectCategory;
							objectCategory[2] = "=nTDSDSA)(";
							objectCategory[3] = PropertyManager.Options;
							objectCategory[4] = ":1.2.840.113556.1.4.804:=1)(";
							objectCategory[5] = PropertyManager.MsDSHasMasterNCs;
							objectCategory[6] = "=";
							objectCategory[7] = Utils.GetEscapedFilterValue(partitionName);
							objectCategory[8] = ")(|";
							objectCategory[9] = stringBuilder.ToString();
							objectCategory[10] = "))(&(";
							objectCategory[11] = PropertyManager.ObjectCategory;
							objectCategory[12] = "=nTDSDSARO)(";
							objectCategory[13] = PropertyManager.Options;
							objectCategory[14] = ":1.2.840.113556.1.4.804:=1)(|";
							objectCategory[15] = stringBuilder2.ToString();
							objectCategory[16] = "))(&(";
							objectCategory[17] = PropertyManager.ObjectCategory;
							objectCategory[18] = "=server)(|";
							objectCategory[19] = stringBuilder1.ToString();
							objectCategory[20] = "))(&(";
							objectCategory[21] = PropertyManager.ObjectCategory;
							objectCategory[22] = "=server)(|";
							objectCategory[23] = stringBuilder3.ToString();
							objectCategory[24] = ")))";
							str1 = string.Concat(objectCategory);
						}
					}
				}
				else
				{
					if (!isDefaultNC)
					{
						if (!isGC)
						{
							objectCategory = new string[7];
							objectCategory[0] = "(|(";
							objectCategory[1] = PropertyManager.ObjectCategory;
							objectCategory[2] = "=nTDSDSA)(";
							objectCategory[3] = PropertyManager.ObjectCategory;
							objectCategory[4] = "=nTDSDSARO)(";
							objectCategory[5] = PropertyManager.ObjectCategory;
							objectCategory[6] = "=server))";
							str1 = string.Concat(objectCategory);
						}
						else
						{
							objectCategory = new string[11];
							objectCategory[0] = "(|(&(";
							objectCategory[1] = PropertyManager.ObjectCategory;
							objectCategory[2] = "=nTDSDSA)(";
							objectCategory[3] = PropertyManager.Options;
							objectCategory[4] = ":1.2.840.113556.1.4.804:=1))(&(";
							objectCategory[5] = PropertyManager.ObjectCategory;
							objectCategory[6] = "=nTDSDSARO)(";
							objectCategory[7] = PropertyManager.Options;
							objectCategory[8] = ":1.2.840.113556.1.4.804:=1))(";
							objectCategory[9] = PropertyManager.ObjectCategory;
							objectCategory[10] = "=server))";
							str1 = string.Concat(objectCategory);
						}
					}
					else
					{
						objectCategory = new string[15];
						objectCategory[0] = "(|(&(";
						objectCategory[1] = PropertyManager.ObjectCategory;
						objectCategory[2] = "=nTDSDSA)(";
						objectCategory[3] = PropertyManager.HasMasterNCs;
						objectCategory[4] = "=";
						objectCategory[5] = Utils.GetEscapedFilterValue(partitionName);
						objectCategory[6] = "))(&(";
						objectCategory[7] = PropertyManager.ObjectCategory;
						objectCategory[8] = "=nTDSDSARO)(";
						objectCategory[9] = PropertyManager.MsDSHasFullReplicaNCs;
						objectCategory[10] = "=";
						objectCategory[11] = Utils.GetEscapedFilterValue(partitionName);
						objectCategory[12] = "))(";
						objectCategory[13] = PropertyManager.ObjectCategory;
						objectCategory[14] = "=server))";
						str1 = string.Concat(objectCategory);
					}
				}
				ADSearcher aDSearcher = new ADSearcher(directoryEntry2, str1, new string[0], SearchScope.Subtree);
				bool flag1 = false;
				ArrayList arrayLists4 = new ArrayList();
				int num = 0;
				string str8 = string.Concat(PropertyManager.MsDSHasInstantiatedNCs, ";range=0-*");
				aDSearcher.PropertiesToLoad.Add(PropertyManager.DistinguishedName);
				aDSearcher.PropertiesToLoad.Add(PropertyManager.DnsHostName);
				aDSearcher.PropertiesToLoad.Add(str8);
				aDSearcher.PropertiesToLoad.Add(PropertyManager.ObjectCategory);
				if (isADAM)
				{
					aDSearcher.PropertiesToLoad.Add(PropertyManager.MsDSPortLDAP);
				}
				try
				{
					string str9 = "CN=NTDS-DSA";
					string str10 = "CN=NTDS-DSA-RO";
					SearchResultCollection searchResultCollections = aDSearcher.FindAll();
					using (searchResultCollections)
					{
						foreach (SearchResult searchResult in searchResultCollections)
						{
							string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.ObjectCategory);
							if (searchResultPropertyValue.Length < str9.Length || Utils.Compare(searchResultPropertyValue, 0, str9.Length, str9, 0, str9.Length) != 0)
							{
								if (!searchResult.Properties.Contains(PropertyManager.DnsHostName))
								{
									continue;
								}
								hashtables.Add(string.Concat("CN=NTDS Settings,", (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DistinguishedName)), (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DnsHostName));
							}
							else
							{
								string searchResultPropertyValue1 = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DistinguishedName);
								if (!flag)
								{
									arrayLists1.Add(searchResultPropertyValue1);
									if (!isADAM)
									{
										continue;
									}
									hashtables1.Add(searchResultPropertyValue1, (int)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.MsDSPortLDAP));
								}
								else
								{
									if (searchResultPropertyValue.Length < str10.Length || Utils.Compare(searchResultPropertyValue, 0, str10.Length, str10, 0, str10.Length) != 0)
									{
										if (searchResult.Properties.Contains(str8))
										{
											str2 = str8;
										}
										else
										{
											foreach (string propertyName in searchResult.Properties.PropertyNames)
											{
												if (propertyName.Length < PropertyManager.MsDSHasInstantiatedNCs.Length || Utils.Compare(propertyName, 0, PropertyManager.MsDSHasInstantiatedNCs.Length, PropertyManager.MsDSHasInstantiatedNCs, 0, PropertyManager.MsDSHasInstantiatedNCs.Length) != 0)
												{
													continue;
												}
												str2 = propertyName;
												break;
											}
										}
										if (str2 == null)
										{
											continue;
										}
										bool flag2 = false;
										int num1 = 0;
										foreach (string item2 in searchResult.Properties[str2])
										{
											if (item2.Length - 13 >= partitionName.Length && Utils.Compare(item2, 13, partitionName.Length, partitionName, 0, partitionName.Length) == 0)
											{
												flag2 = true;
												if (string.Compare(item2, 10, "0", 0, 1, StringComparison.OrdinalIgnoreCase) == 0)
												{
													arrayLists1.Add(searchResultPropertyValue1);
													if (!isADAM)
													{
														break;
													}
													hashtables1.Add(searchResultPropertyValue1, (int)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.MsDSPortLDAP));
													break;
												}
											}
											num1++;
										}
										if (flag2 || str2.Length < str8.Length || Utils.Compare(str2, 0, str8.Length, str8, 0, str8.Length) == 0)
										{
											continue;
										}
										flag1 = true;
										arrayLists4.Add(searchResultPropertyValue1);
										num = num1;
									}
									else
									{
										arrayLists1.Add(searchResultPropertyValue1);
										if (!isADAM)
										{
											continue;
										}
										hashtables1.Add(searchResultPropertyValue1, (int)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.MsDSPortLDAP));
									}
								}
							}
						}
					}
					if (flag1)
					{
						do
						{
							StringBuilder stringBuilder4 = new StringBuilder(20);
							if (arrayLists4.Count > 1)
							{
								stringBuilder4.Append("(|");
							}
							foreach (string arrayList in arrayLists4)
							{
								stringBuilder4.Append("(");
								stringBuilder4.Append(PropertyManager.NCName);
								stringBuilder4.Append("=");
								stringBuilder4.Append(Utils.GetEscapedFilterValue(arrayList));
								stringBuilder4.Append(")");
							}
							if (arrayLists4.Count > 1)
							{
								stringBuilder4.Append(")");
							}
							arrayLists4.Clear();
							flag1 = false;
							objectCategory = new string[5];
							objectCategory[0] = "(&(";
							objectCategory[1] = PropertyManager.ObjectCategory;
							objectCategory[2] = "=nTDSDSA)";
							objectCategory[3] = stringBuilder4.ToString();
							objectCategory[4] = ")";
							aDSearcher.Filter = string.Concat(objectCategory);
							msDSHasInstantiatedNCs = new object[4];
							msDSHasInstantiatedNCs[0] = PropertyManager.MsDSHasInstantiatedNCs;
							msDSHasInstantiatedNCs[1] = ";range=";
							msDSHasInstantiatedNCs[2] = num;
							msDSHasInstantiatedNCs[3] = "-*";
							string str11 = string.Concat(msDSHasInstantiatedNCs);
							aDSearcher.PropertiesToLoad.Clear();
							aDSearcher.PropertiesToLoad.Add(str11);
							aDSearcher.PropertiesToLoad.Add(PropertyManager.DistinguishedName);
							SearchResultCollection searchResultCollections1 = aDSearcher.FindAll();
							try
							{
								foreach (SearchResult searchResult1 in searchResultCollections1)
								{
									string searchResultPropertyValue2 = (string)PropertyManager.GetSearchResultPropertyValue(searchResult1, PropertyManager.DistinguishedName);
									str3 = null;
									if (searchResult1.Properties.Contains(str11))
									{
										str3 = str11;
									}
									else
									{
										enumerator = searchResult1.Properties.PropertyNames.GetEnumerator();
										try
										{
											while (enumerator.MoveNext())
											{
												string str12 = searchResult1.ToString ();
												if (string.Compare(str12, 0, PropertyManager.MsDSHasInstantiatedNCs, 0, PropertyManager.MsDSHasInstantiatedNCs.Length, StringComparison.OrdinalIgnoreCase) != 0)
												{
													continue;
												}
												str3 = str12;
												break;
											}
										}
										finally
										{
											disposable = enumerator as IDisposable;
											if (disposable != null)
											{
												disposable.Dispose();
											}
										}
									}
									if (str3 == null)
									{
										continue;
									}
									bool flag3 = false;
									int num2 = 0;
									enumerator = searchResult1.Properties[str3].GetEnumerator();
									try
									{
										while (enumerator.MoveNext())
										{
											string str13 = searchResult1.ToString ();
											if (str13.Length - 13 >= partitionName.Length && Utils.Compare(str13, 13, partitionName.Length, partitionName, 0, partitionName.Length) == 0)
											{
												if (string.Compare(str13, 10, "0", 0, 1, StringComparison.OrdinalIgnoreCase) == 0)
												{
													arrayLists1.Add(searchResultPropertyValue2);
													if (!isADAM)
													{
														break;
													}
													hashtables1.Add(searchResultPropertyValue2, (int)PropertyManager.GetSearchResultPropertyValue(searchResult1, PropertyManager.MsDSPortLDAP));
													break;
												}
											}
											num2++;
										}
									}
									finally
									{
										disposable = enumerator as IDisposable;
										if (disposable != null)
										{
											disposable.Dispose();
										}
									}
									if (flag3 || str3.Length < str11.Length || Utils.Compare(str3, 0, str11.Length, str11, 0, str11.Length) == 0)
									{
										continue;
									}
									flag1 = true;
									arrayLists4.Add(searchResultPropertyValue2);
									num = num + num2;
								}
							}
							finally
							{
								searchResultCollections1.Dispose();
							}
						}
						while (flag1);
					}
				}
				catch (COMException cOMException7)
				{
					COMException cOMException6 = cOMException7;
					if (cOMException6.ErrorCode != -2147016656 || siteName == null)
					{
						throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException6);
					}
					else
					{
						arrayLists = arrayLists2;
						return arrayLists;
					}
				}
				foreach (string arrayList1 in arrayLists1)
				{
					string item3 = (string)hashtables[arrayList1];
					if (item3 != null)
					{
						if (!isADAM || hashtables1[arrayList1] != null)
						{
							if (!isADAM)
							{
								arrayLists2.Add(item3);
							}
							else
							{
								arrayLists2.Add(string.Concat(item3, ":", (object)((int)hashtables1[arrayList1])));
							}
						}
						else
						{
							msDSHasInstantiatedNCs = new object[1];
							msDSHasInstantiatedNCs[0] = arrayList1;
							throw new ActiveDirectoryOperationException(Res.GetString("NoHostNameOrPortNumber", msDSHasInstantiatedNCs));
						}
					}
					else
					{
						if (!isADAM)
						{
							msDSHasInstantiatedNCs = new object[1];
							msDSHasInstantiatedNCs[0] = arrayList1;
							throw new ActiveDirectoryOperationException(Res.GetString("NoHostName", msDSHasInstantiatedNCs));
						}
						else
						{
							msDSHasInstantiatedNCs = new object[1];
							msDSHasInstantiatedNCs[0] = arrayList1;
							throw new ActiveDirectoryOperationException(Res.GetString("NoHostNameOrPortNumber", msDSHasInstantiatedNCs));
						}
					}
				}
				return arrayLists2;
			}
			return arrayLists;
		}
Example #8
0
		internal void RemoveIfExists(string distinguishedName)
		{
			object obj = distinguishedName;
			if (string.Compare(distinguishedName, "rootdse", StringComparison.OrdinalIgnoreCase) != 0)
			{
				obj = new DistinguishedName(distinguishedName);
			}
			if (this.directoryEntries.ContainsKey(obj))
			{
				DirectoryEntry item = (DirectoryEntry)this.directoryEntries[obj];
				if (item != null)
				{
					this.directoryEntries.Remove(obj);
					item.Dispose();
				}
			}
		}
Example #9
0
		internal DirectoryEntry GetCachedDirectoryEntry(string distinguishedName)
		{
			object obj = distinguishedName;
			if (string.Compare(distinguishedName, "rootdse", StringComparison.OrdinalIgnoreCase) != 0 && string.Compare(distinguishedName, "schema", StringComparison.OrdinalIgnoreCase) != 0)
			{
				obj = new DistinguishedName(distinguishedName);
			}
			if (!this.directoryEntries.ContainsKey(obj))
			{
				DirectoryEntry newDirectoryEntry = this.GetNewDirectoryEntry(distinguishedName);
				this.directoryEntries.Add(obj, newDirectoryEntry);
			}
			return (DirectoryEntry)this.directoryEntries[obj];
		}
 internal static ArrayList GetReplicaList(DirectoryContext context, string partitionName, string siteName, bool isDefaultNC, bool isADAM, bool isGC)
 {
     ArrayList list = new ArrayList();
     ArrayList list2 = new ArrayList();
     Hashtable hashtable = new Hashtable();
     Hashtable hashtable2 = new Hashtable();
     StringBuilder builder = new StringBuilder(10);
     StringBuilder builder2 = new StringBuilder(10);
     StringBuilder builder3 = new StringBuilder(10);
     StringBuilder builder4 = new StringBuilder(10);
     bool flag = false;
     string dn = null;
     try
     {
         dn = DirectoryEntryManager.ExpandWellKnownDN(context, WellKnownDN.ConfigurationNamingContext);
     }
     catch (COMException exception)
     {
         throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(context, exception);
     }
     if ((partitionName != null) && !isDefaultNC)
     {
         DistinguishedName name = new DistinguishedName(partitionName);
         DistinguishedName name2 = new DistinguishedName(dn);
         DistinguishedName name3 = new DistinguishedName("CN=Schema," + dn);
         if (!name2.Equals(name) && !name3.Equals(name))
         {
             flag = true;
         }
     }
     if (flag)
     {
         DirectoryEntry directoryEntry = null;
         DirectoryEntry searchRootEntry = null;
         try
         {
             directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, "CN=Partitions," + dn);
             string adamDnsHostNameFromNTDSA = null;
             if (isADAM)
             {
                 adamDnsHostNameFromNTDSA = GetAdamDnsHostNameFromNTDSA(context, (string) PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.FsmoRoleOwner));
             }
             else
             {
                 adamDnsHostNameFromNTDSA = GetDnsHostNameFromNTDSA(context, (string) PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.FsmoRoleOwner));
             }
             DirectoryContext context2 = GetNewDirectoryContext(adamDnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, context);
             searchRootEntry = DirectoryEntryManager.GetDirectoryEntry(context2, "CN=Partitions," + dn);
             string filter = "(&(" + PropertyManager.ObjectCategory + "=crossRef)(" + PropertyManager.NCName + "=" + GetEscapedFilterValue(partitionName) + "))";
             ArrayList propertiesToLoad = new ArrayList();
             propertiesToLoad.Add(PropertyManager.MsDSNCReplicaLocations);
             propertiesToLoad.Add(PropertyManager.MsDSNCROReplicaLocations);
             Hashtable hashtable3 = null;
             try
             {
                 hashtable3 = GetValuesWithRangeRetrieval(searchRootEntry, filter, propertiesToLoad, SearchScope.OneLevel);
             }
             catch (COMException exception2)
             {
                 throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(context2, exception2);
             }
             catch (ActiveDirectoryObjectNotFoundException)
             {
                 return list2;
             }
             ArrayList list4 = (ArrayList) hashtable3[PropertyManager.MsDSNCReplicaLocations.ToLower(CultureInfo.InvariantCulture)];
             ArrayList list5 = (ArrayList) hashtable3[PropertyManager.MsDSNCROReplicaLocations.ToLower(CultureInfo.InvariantCulture)];
             if (list4.Count == 0)
             {
                 return list2;
             }
             foreach (string str4 in list4)
             {
                 builder.Append("(");
                 builder.Append(PropertyManager.DistinguishedName);
                 builder.Append("=");
                 builder.Append(GetEscapedFilterValue(str4));
                 builder.Append(")");
                 builder2.Append("(");
                 builder2.Append(PropertyManager.DistinguishedName);
                 builder2.Append("=");
                 builder2.Append(GetEscapedFilterValue(GetPartialDN(str4, 1)));
                 builder2.Append(")");
             }
             foreach (string str5 in list5)
             {
                 builder3.Append("(");
                 builder3.Append(PropertyManager.DistinguishedName);
                 builder3.Append("=");
                 builder3.Append(GetEscapedFilterValue(str5));
                 builder3.Append(")");
                 builder4.Append("(");
                 builder4.Append(PropertyManager.DistinguishedName);
                 builder4.Append("=");
                 builder4.Append(GetEscapedFilterValue(GetPartialDN(str5, 1)));
                 builder4.Append(")");
             }
         }
         catch (COMException exception3)
         {
             throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(context, exception3);
         }
         finally
         {
             if (directoryEntry != null)
             {
                 directoryEntry.Dispose();
             }
             if (searchRootEntry != null)
             {
                 searchRootEntry.Dispose();
             }
         }
     }
     string str6 = null;
     using (DirectoryEntry entry3 = null)
     {
         if (siteName != null)
         {
             str6 = "CN=Servers,CN=" + siteName + ",CN=Sites," + dn;
         }
         else
         {
             str6 = "CN=Sites," + dn;
         }
         entry3 = DirectoryEntryManager.GetDirectoryEntry(context, str6);
         string str7 = null;
         if (builder.ToString().Length == 0)
         {
             if (isDefaultNC)
             {
                 str7 = "(|(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" + PropertyManager.HasMasterNCs + "=" + GetEscapedFilterValue(partitionName) + "))(&(" + PropertyManager.ObjectCategory + "=nTDSDSARO)(" + PropertyManager.MsDSHasFullReplicaNCs + "=" + GetEscapedFilterValue(partitionName) + "))(" + PropertyManager.ObjectCategory + "=server))";
             }
             else if (isGC)
             {
                 str7 = "(|(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" + PropertyManager.Options + ":1.2.840.113556.1.4.804:=1))(&(" + PropertyManager.ObjectCategory + "=nTDSDSARO)(" + PropertyManager.Options + ":1.2.840.113556.1.4.804:=1))(" + PropertyManager.ObjectCategory + "=server))";
             }
             else
             {
                 str7 = "(|(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" + PropertyManager.ObjectCategory + "=nTDSDSARO)(" + PropertyManager.ObjectCategory + "=server))";
             }
         }
         else if (isGC)
         {
             if (builder3.Length > 0)
             {
                 str7 = "(|(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" + PropertyManager.Options + ":1.2.840.113556.1.4.804:=1)(" + PropertyManager.MsDSHasMasterNCs + "=" + GetEscapedFilterValue(partitionName) + ")(|" + builder.ToString() + "))(&(" + PropertyManager.ObjectCategory + "=nTDSDSARO)(" + PropertyManager.Options + ":1.2.840.113556.1.4.804:=1)(|" + builder3.ToString() + "))(&(" + PropertyManager.ObjectCategory + "=server)(|" + builder2.ToString() + "))(&(" + PropertyManager.ObjectCategory + "=server)(|" + builder4.ToString() + ")))";
             }
             else
             {
                 str7 = "(|(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" + PropertyManager.Options + ":1.2.840.113556.1.4.804:=1)(" + PropertyManager.MsDSHasMasterNCs + "=" + GetEscapedFilterValue(partitionName) + ")(|" + builder.ToString() + "))(&(" + PropertyManager.ObjectCategory + "=server)(|" + builder2.ToString() + ")))";
             }
         }
         else if (builder3.Length > 0)
         {
             str7 = "(|(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" + PropertyManager.MsDSHasMasterNCs + "=" + GetEscapedFilterValue(partitionName) + ")(|" + builder.ToString() + "))(&(" + PropertyManager.ObjectCategory + "=nTDSDSARO)(|" + builder3.ToString() + "))(&(" + PropertyManager.ObjectCategory + "=server)(|" + builder2.ToString() + "))(&(" + PropertyManager.ObjectCategory + "=server)(|" + builder4.ToString() + ")))";
         }
         else
         {
             str7 = "(|(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" + PropertyManager.MsDSHasMasterNCs + "=" + GetEscapedFilterValue(partitionName) + ")(|" + builder.ToString() + "))(&(" + PropertyManager.ObjectCategory + "=server)(|" + builder2.ToString() + ")))";
         }
         ADSearcher searcher = new ADSearcher(entry3, str7, new string[0], SearchScope.Subtree);
         SearchResultCollection results = null;
         bool flag2 = false;
         ArrayList list6 = new ArrayList();
         int num = 0;
         string str8 = PropertyManager.MsDSHasInstantiatedNCs + ";range=0-*";
         searcher.PropertiesToLoad.Add(PropertyManager.DistinguishedName);
         searcher.PropertiesToLoad.Add(PropertyManager.DnsHostName);
         searcher.PropertiesToLoad.Add(str8);
         searcher.PropertiesToLoad.Add(PropertyManager.ObjectCategory);
         if (isADAM)
         {
             searcher.PropertiesToLoad.Add(PropertyManager.MsDSPortLDAP);
         }
         try
         {
             string str9 = "CN=NTDS-DSA";
             string str10 = "CN=NTDS-DSA-RO";
             using (results = searcher.FindAll())
             {
                 foreach (SearchResult result in results)
                 {
                     string searchResultPropertyValue = (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.ObjectCategory);
                     if ((searchResultPropertyValue.Length >= str9.Length) && (Compare(searchResultPropertyValue, 0, str9.Length, str9, 0, str9.Length) == 0))
                     {
                         string str12 = (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DistinguishedName);
                         if (flag)
                         {
                             if ((searchResultPropertyValue.Length >= str10.Length) && (Compare(searchResultPropertyValue, 0, str10.Length, str10, 0, str10.Length) == 0))
                             {
                                 list.Add(str12);
                                 if (isADAM)
                                 {
                                     hashtable2.Add(str12, (int) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.MsDSPortLDAP));
                                 }
                                 continue;
                             }
                             string str13 = null;
                             if (!result.Properties.Contains(str8))
                             {
                                 foreach (string str14 in result.Properties.PropertyNames)
                                 {
                                     if ((str14.Length >= PropertyManager.MsDSHasInstantiatedNCs.Length) && (Compare(str14, 0, PropertyManager.MsDSHasInstantiatedNCs.Length, PropertyManager.MsDSHasInstantiatedNCs, 0, PropertyManager.MsDSHasInstantiatedNCs.Length) == 0))
                                     {
                                         str13 = str14;
                                         break;
                                     }
                                 }
                             }
                             else
                             {
                                 str13 = str8;
                             }
                             if (str13 == null)
                             {
                                 continue;
                             }
                             bool flag3 = false;
                             int num2 = 0;
                             foreach (string str15 in result.Properties[str13])
                             {
                                 if (((str15.Length - 13) >= partitionName.Length) && (Compare(str15, 13, partitionName.Length, partitionName, 0, partitionName.Length) == 0))
                                 {
                                     flag3 = true;
                                     if (string.Compare(str15, 10, "0", 0, 1, StringComparison.OrdinalIgnoreCase) == 0)
                                     {
                                         list.Add(str12);
                                         if (isADAM)
                                         {
                                             hashtable2.Add(str12, (int) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.MsDSPortLDAP));
                                         }
                                         break;
                                     }
                                 }
                                 num2++;
                             }
                             if ((!flag3 && (str13.Length >= str8.Length)) && (Compare(str13, 0, str8.Length, str8, 0, str8.Length) != 0))
                             {
                                 flag2 = true;
                                 list6.Add(str12);
                                 num = num2;
                             }
                             continue;
                         }
                         list.Add(str12);
                         if (isADAM)
                         {
                             hashtable2.Add(str12, (int) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.MsDSPortLDAP));
                         }
                         continue;
                     }
                     if (result.Properties.Contains(PropertyManager.DnsHostName))
                     {
                         hashtable.Add("CN=NTDS Settings," + ((string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DistinguishedName)), (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsHostName));
                     }
                 }
             }
             if (flag2)
             {
                 do
                 {
                     StringBuilder builder5 = new StringBuilder(20);
                     if (list6.Count > 1)
                     {
                         builder5.Append("(|");
                     }
                     foreach (string str16 in list6)
                     {
                         builder5.Append("(");
                         builder5.Append(PropertyManager.NCName);
                         builder5.Append("=");
                         builder5.Append(GetEscapedFilterValue(str16));
                         builder5.Append(")");
                     }
                     if (list6.Count > 1)
                     {
                         builder5.Append(")");
                     }
                     list6.Clear();
                     flag2 = false;
                     searcher.Filter = "(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)" + builder5.ToString() + ")";
                     string str17 = string.Concat(new object[] { PropertyManager.MsDSHasInstantiatedNCs, ";range=", num, "-*" });
                     searcher.PropertiesToLoad.Clear();
                     searcher.PropertiesToLoad.Add(str17);
                     searcher.PropertiesToLoad.Add(PropertyManager.DistinguishedName);
                     using (SearchResultCollection results2 = searcher.FindAll())
                     {
                         foreach (SearchResult result2 in results2)
                         {
                             string str18 = (string) PropertyManager.GetSearchResultPropertyValue(result2, PropertyManager.DistinguishedName);
                             string str19 = null;
                             if (!result2.Properties.Contains(str17))
                             {
                                 foreach (string str20 in result2.Properties.PropertyNames)
                                 {
                                     if (string.Compare(str20, 0, PropertyManager.MsDSHasInstantiatedNCs, 0, PropertyManager.MsDSHasInstantiatedNCs.Length, StringComparison.OrdinalIgnoreCase) == 0)
                                     {
                                         str19 = str20;
                                         break;
                                     }
                                 }
                             }
                             else
                             {
                                 str19 = str17;
                             }
                             if (str19 != null)
                             {
                                 bool flag4 = false;
                                 int num3 = 0;
                                 foreach (string str21 in result2.Properties[str19])
                                 {
                                     if (((str21.Length - 13) >= partitionName.Length) && (Compare(str21, 13, partitionName.Length, partitionName, 0, partitionName.Length) == 0))
                                     {
                                         flag4 = true;
                                         if (string.Compare(str21, 10, "0", 0, 1, StringComparison.OrdinalIgnoreCase) == 0)
                                         {
                                             list.Add(str18);
                                             if (isADAM)
                                             {
                                                 hashtable2.Add(str18, (int) PropertyManager.GetSearchResultPropertyValue(result2, PropertyManager.MsDSPortLDAP));
                                             }
                                             break;
                                         }
                                     }
                                     num3++;
                                 }
                                 if ((!flag4 && (str19.Length >= str17.Length)) && (Compare(str19, 0, str17.Length, str17, 0, str17.Length) != 0))
                                 {
                                     flag2 = true;
                                     list6.Add(str18);
                                     num += num3;
                                 }
                             }
                         }
                     }
                 }
                 while (flag2);
             }
         }
         catch (COMException exception4)
         {
             if ((exception4.ErrorCode != -2147016656) || (siteName == null))
             {
                 throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(context, exception4);
             }
             return list2;
         }
     }
     foreach (string str22 in list)
     {
         string str23 = (string) hashtable[str22];
         if (str23 == null)
         {
             if (isADAM)
             {
                 throw new ActiveDirectoryOperationException(Res.GetString("NoHostNameOrPortNumber", new object[] { str22 }));
             }
             throw new ActiveDirectoryOperationException(Res.GetString("NoHostName", new object[] { str22 }));
         }
         if (isADAM && (hashtable2[str22] == null))
         {
             throw new ActiveDirectoryOperationException(Res.GetString("NoHostNameOrPortNumber", new object[] { str22 }));
         }
         if (isADAM)
         {
             list2.Add(str23 + ":" + ((int) hashtable2[str22]));
         }
         else
         {
             list2.Add(str23);
         }
     }
     return list2;
 }
Example #11
0
        internal static ArrayList GetReplicaList(DirectoryContext context, string partitionName, string siteName, bool isDefaultNC, bool isADAM, bool isGC)
        {
            ArrayList ntdsaNames = new ArrayList();
            ArrayList dnsNames = new ArrayList();

            //
            // The algorithm is as follows:
            // 1. Search for the crossRef entry of this partition and retrieve the msDS-NC-Replica-Locations  and
            //     msDS_NC-RO-Replica-Locations for a list of the replicas (using range retrieval). This is needed 
            //     in the case of application partition only.
            // 2. Search for the ntdsa objects of these replicas which have the partition in the Has-MasterNCs attribute (if partition name is specified
            //     else search for all ntdsa objects)
            // 3. For each replica in the resulting set, check if the msDS-Has-InstantiatedNCs attribute is of the form B:8:00000005:<DN of partition>
            //     where the second nibble from the least significant side is 0, B:8:00000015 would signify that the partition is still being replicated in
            //     and B:8:00000025 would indicate the partition is being replicated out (replica deletion) (again, this is only if partitionName is specified).
            //     This step is needed only for application partitions. This will be ignored for read-only NCs as it will ONLY be populated locally to each RODC.
            //

            Hashtable serverNames = new Hashtable();
            Hashtable serverPorts = new Hashtable();
            StringBuilder ntdsaFilter = new StringBuilder(10);
            StringBuilder serverFilter = new StringBuilder(10);
            StringBuilder roNtdsaFilter = new StringBuilder(10);
            StringBuilder roServerFilter = new StringBuilder(10);

            bool useReplicaInfo = false;
            string configurationNamingContext = null;

            try
            {
                configurationNamingContext = DirectoryEntryManager.ExpandWellKnownDN(context, WellKnownDN.ConfigurationNamingContext);
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }

            //
            // If partition name is not null and is not Configuration/Schema/defaultNC , we need to get the list of the 
            // msDS-NC-Replica-Locations and msDS-NC-RO-Replica-Locations (for Configuration/Schema, these attributes are 
            // not populated, so we just return a list of all the servers)
            //
            if (partitionName != null && !isDefaultNC)
            {
                DistinguishedName dn = new DistinguishedName(partitionName);
                DistinguishedName configDn = new DistinguishedName(configurationNamingContext);
                DistinguishedName schemaDn = new DistinguishedName("CN=Schema," + configurationNamingContext);

                if ((!(configDn.Equals(dn))) && (!(schemaDn.Equals(dn))))
                {
                    useReplicaInfo = true;
                }
            }

            if (useReplicaInfo)
            {
                DirectoryEntry partitionsEntry = null;
                DirectoryEntry fsmoPartitionsEntry = null;

                try
                {
                    //
                    // get the partitions entry on the naming master
                    //
                    partitionsEntry = DirectoryEntryManager.GetDirectoryEntry(context, "CN=Partitions," + configurationNamingContext);
                    string fsmoRoleOwnerName = null;
                    if (isADAM)
                    {
                        fsmoRoleOwnerName = Utils.GetAdamDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, partitionsEntry, PropertyManager.FsmoRoleOwner));
                    }
                    else
                    {
                        fsmoRoleOwnerName = Utils.GetDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, partitionsEntry, PropertyManager.FsmoRoleOwner));
                    }

                    DirectoryContext fsmoContext = Utils.GetNewDirectoryContext(fsmoRoleOwnerName, DirectoryContextType.DirectoryServer, context);
                    fsmoPartitionsEntry = DirectoryEntryManager.GetDirectoryEntry(fsmoContext, "CN=Partitions," + configurationNamingContext);

                    // get the properties using range retrieval
                    // (since msDS-NC-Replica-Locations and msDS-NC-RO-Replica-Locations are multi-valued)
                    string filter = "(&(" + PropertyManager.ObjectCategory + "=crossRef)(" + PropertyManager.NCName + "=" + Utils.GetEscapedFilterValue(partitionName) + "))";
                    ArrayList propertyNames = new ArrayList();
                    propertyNames.Add(PropertyManager.MsDSNCReplicaLocations);
                    propertyNames.Add(PropertyManager.MsDSNCROReplicaLocations);

                    Hashtable values = null;
                    try
                    {
                        values = Utils.GetValuesWithRangeRetrieval(fsmoPartitionsEntry, filter, propertyNames, SearchScope.OneLevel);
                    }
                    catch (COMException e)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(fsmoContext, e);
                    }
                    catch (ActiveDirectoryObjectNotFoundException)
                    {
                        // this means that this partition does not exist, so we return an empty collection
                        return dnsNames;
                    }

                    // extract the property values
                    ArrayList replicaLocations = (ArrayList)values[PropertyManager.MsDSNCReplicaLocations.ToLower(CultureInfo.InvariantCulture)];
                    ArrayList roReplicaLocations = (ArrayList)values[PropertyManager.MsDSNCROReplicaLocations.ToLower(CultureInfo.InvariantCulture)];
                    Debug.Assert(replicaLocations != null);

                    if (replicaLocations.Count == 0)
                    {
                        // At this point we find that there are no replica locations, so we return an empty collection.
                        return dnsNames;
                    }

                    foreach (string replicaLocation in replicaLocations)
                    {
                        ntdsaFilter.Append("(");
                        ntdsaFilter.Append(PropertyManager.DistinguishedName);
                        ntdsaFilter.Append("=");
                        ntdsaFilter.Append(Utils.GetEscapedFilterValue(replicaLocation));
                        ntdsaFilter.Append(")");

                        serverFilter.Append("(");
                        serverFilter.Append(PropertyManager.DistinguishedName);
                        serverFilter.Append("=");
                        serverFilter.Append(Utils.GetEscapedFilterValue(Utils.GetPartialDN(replicaLocation, 1)));
                        serverFilter.Append(")");
                    }

                    foreach (string roReplicaLocation in roReplicaLocations)
                    {
                        roNtdsaFilter.Append("(");
                        roNtdsaFilter.Append(PropertyManager.DistinguishedName);
                        roNtdsaFilter.Append("=");
                        roNtdsaFilter.Append(Utils.GetEscapedFilterValue(roReplicaLocation));
                        roNtdsaFilter.Append(")");

                        roServerFilter.Append("(");
                        roServerFilter.Append(PropertyManager.DistinguishedName);
                        roServerFilter.Append("=");
                        roServerFilter.Append(Utils.GetEscapedFilterValue(Utils.GetPartialDN(roReplicaLocation, 1)));
                        roServerFilter.Append(")");
                    }
                }
                catch (COMException e)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
                finally
                {
                    if (partitionsEntry != null)
                    {
                        partitionsEntry.Dispose();
                    }
                    if (fsmoPartitionsEntry != null)
                    {
                        fsmoPartitionsEntry.Dispose();
                    }
                }
            }

            string searchRootDN = null;
            DirectoryEntry searchRootEntry = null;
            try
            {
                // check whether we can narrow down our search within a specific site
                if (siteName != null)
                {
                    searchRootDN = "CN=Servers,CN=" + siteName + ",CN=Sites," + configurationNamingContext;
                }
                else
                {
                    searchRootDN = "CN=Sites," + configurationNamingContext;
                }
                searchRootEntry = DirectoryEntryManager.GetDirectoryEntry(context, searchRootDN);

                // set up searcher object
                string filter2 = null;
                if (ntdsaFilter.ToString().Length == 0)
                {
                    // either this is the case when we want all the servers (partitionName = null or partitionName is Configuration/Schema)
                    // or this is the case when partitionName is the defaultNamingContext
                    // for the latter we want to restrict the search to only that naming context

                    if (isDefaultNC)
                    {
                        Debug.Assert(partitionName != null);
                        Debug.Assert(!isGC);

                        filter2 = "(|(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" + PropertyManager.HasMasterNCs +
                            "=" + Utils.GetEscapedFilterValue(partitionName) + "))(&(" + PropertyManager.ObjectCategory + "=nTDSDSARO)(" +
                            PropertyManager.MsDSHasFullReplicaNCs + "=" + Utils.GetEscapedFilterValue(partitionName) + "))(" +
                            PropertyManager.ObjectCategory + "=server))";
                    }
                    else
                    {
                        if (isGC)
                        {
                            filter2 = "(|(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" +
                                PropertyManager.Options + ":1.2.840.113556.1.4.804:=1))(&(" +
                                PropertyManager.ObjectCategory + "=nTDSDSARO)(" +
                                PropertyManager.Options + ":1.2.840.113556.1.4.804:=1))(" +
                                PropertyManager.ObjectCategory + "=server))";
                        }
                        else
                        {
                            filter2 = "(|" + "(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" +
                                PropertyManager.ObjectCategory + "=nTDSDSARO)(" +
                                PropertyManager.ObjectCategory + "=server))";
                        }
                    }
                }
                else
                {
                    Debug.Assert(partitionName != null);
                    // resctrict the search to the servers that were listed in the crossRef
                    if (isGC)
                    {
                        if (roNtdsaFilter.Length > 0)
                        {
                            //for read-only NCs, msDS-hasFullReplicaNCs is equivalent of msDS-hasMasterNCs. But since msDS-hasFullReplicaNCs will be
                            //populated ONLY on each RODC, it can't be used. Since roNtdsaFilter is populated using input partitionName, we should
                            //be fine.
                            filter2 = "(|(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" + PropertyManager.Options +
                                ":1.2.840.113556.1.4.804:=1)(" + PropertyManager.MsDSHasMasterNCs + "=" + Utils.GetEscapedFilterValue(partitionName) +
                                ")(|" + ntdsaFilter.ToString() + "))" + "(&(" + PropertyManager.ObjectCategory + "=nTDSDSARO)(" + PropertyManager.Options +
                                ":1.2.840.113556.1.4.804:=1)(|" + roNtdsaFilter.ToString() + "))" +
                                "(&(" + PropertyManager.ObjectCategory + "=server)(|" + serverFilter.ToString() + "))" +
                                "(&(" + PropertyManager.ObjectCategory + "=server)(|" + roServerFilter.ToString() + ")))";
                        }
                        else
                        {
                            filter2 = "(|(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" + PropertyManager.Options +
                                ":1.2.840.113556.1.4.804:=1)(" + PropertyManager.MsDSHasMasterNCs + "=" + Utils.GetEscapedFilterValue(partitionName) +
                                ")(|" + ntdsaFilter.ToString() + "))" + "(&(" + PropertyManager.ObjectCategory + "=server)(|" + serverFilter.ToString() + ")))";
                        }
                    }
                    else
                    {
                        if (roNtdsaFilter.Length > 0)
                        {
                            //for read-only NCs, msDS-hasFullReplicaNCs is equivalent of msDS-hasMasterNCs. But since msDS-hasFullReplicaNCs will be
                            //populated ONLY on each RODC, it can't be used. Since roNtdsaFilter is populated using input partitionName, we should
                            //be fine.
                            filter2 = "(|(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" + PropertyManager.MsDSHasMasterNCs + "=" + Utils.GetEscapedFilterValue(partitionName) + ")(|" + ntdsaFilter.ToString() + "))"
                                + "(&(" + PropertyManager.ObjectCategory + "=nTDSDSARO)(|" + roNtdsaFilter.ToString() + "))"
                                + "(&(" + PropertyManager.ObjectCategory + "=server)(|" + serverFilter.ToString() + "))"
                                + "(&(" + PropertyManager.ObjectCategory + "=server)(|" + roServerFilter.ToString() + ")))";
                        }
                        else
                        {
                            filter2 = "(|(&(" + PropertyManager.ObjectCategory + "=nTDSDSA)(" + PropertyManager.MsDSHasMasterNCs + "=" + Utils.GetEscapedFilterValue(partitionName) + ")(|" + ntdsaFilter.ToString() + "))"
                                + "(&(" + PropertyManager.ObjectCategory + "=server)(|" + serverFilter.ToString() + ")))";
                        }
                    }
                }

                string[] propertiesToLoad2 = new string[5];

                ADSearcher searcher2 = new ADSearcher(searchRootEntry, filter2, new string[] { }, SearchScope.Subtree);
                SearchResultCollection resCol = null;
                bool needToContinueRangeRetrieval = false;
                ArrayList ntdsaNamesForRangeRetrieval = new ArrayList();
                int rangeStart = 0;

                string propertyWithRangeInfo = PropertyManager.MsDSHasInstantiatedNCs + ";range=0-*";
                searcher2.PropertiesToLoad.Add(PropertyManager.DistinguishedName);
                searcher2.PropertiesToLoad.Add(PropertyManager.DnsHostName);
                searcher2.PropertiesToLoad.Add(propertyWithRangeInfo);
                searcher2.PropertiesToLoad.Add(PropertyManager.ObjectCategory);
                if (isADAM)
                {
                    searcher2.PropertiesToLoad.Add(PropertyManager.MsDSPortLDAP);
                }

                try
                {
                    string objectCategoryValue = "CN=NTDS-DSA";
                    string roObjectCategoryValue = "CN=NTDS-DSA-RO";

                    resCol = searcher2.FindAll();

                    try
                    {
                        foreach (SearchResult res in resCol)
                        {
                            string objectCategory = (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.ObjectCategory);
                            if ((objectCategory.Length >= objectCategoryValue.Length) && (Utils.Compare(objectCategory, 0, objectCategoryValue.Length, objectCategoryValue, 0, objectCategoryValue.Length) == 0))
                            {
                                //
                                // ntdsa objects (return only those servers which have the partition fully instantiated)
                                //
                                string ntdsaName = (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.DistinguishedName);
                                if (useReplicaInfo)
                                {
                                    if ((objectCategory.Length >= roObjectCategoryValue.Length) && (Utils.Compare(objectCategory, 0, roObjectCategoryValue.Length, roObjectCategoryValue, 0, roObjectCategoryValue.Length) == 0))
                                    {
                                        //for read-only NCs, msDS-HasInstantiatedNCs will be populated ONLY on each RODC and it will NOT be 
                                        //replicated to other DCs. So it can't be used, provided we connect to each RODC and verify it which is not
                                        //really required as msDS-NC-RO-Replica-Locations should provide the correct information.
                                        ntdsaNames.Add(ntdsaName);
                                        if (isADAM)
                                        {
                                            serverPorts.Add(ntdsaName, (int)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.MsDSPortLDAP));
                                        }
                                        continue;
                                    }

                                    // Here we need to check if we retrieved all the msDS-HasInstantiatedNCs values
                                    // if not we need to continue with the range retrieval (in parallel for the various ntdsa objects)

                                    string propertyName = null;
                                    if (!res.Properties.Contains(propertyWithRangeInfo))
                                    {
                                        // find the property name with the range info
                                        foreach (string property in res.Properties.PropertyNames)
                                        {
                                            if ((property.Length >= PropertyManager.MsDSHasInstantiatedNCs.Length) && (Utils.Compare(property, 0, PropertyManager.MsDSHasInstantiatedNCs.Length, PropertyManager.MsDSHasInstantiatedNCs, 0, PropertyManager.MsDSHasInstantiatedNCs.Length) == 0))
                                            {
                                                propertyName = property;
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        propertyName = propertyWithRangeInfo;
                                    }

                                    if (propertyName == null)
                                    {
                                        // property does not exist, possiblyno values, so continue
                                        continue;
                                    }

                                    bool foundPartitionEntry = false;
                                    int valueCount = 0;

                                    foreach (string dnString in res.Properties[propertyName])
                                    {
                                        Debug.Assert(dnString.Length > 10, "ConfigurationSet::GetReplicaList - dnWithBinary is not in the expected format.");

                                        if (((dnString.Length - 13) >= partitionName.Length) && (Utils.Compare(dnString, 13, partitionName.Length, partitionName, 0, partitionName.Length) == 0))
                                        {
                                            // found the entry that corresponds to this partition so even if we didn't get all the values of the 
                                            // multivalues attribute we can stop here.
                                            foundPartitionEntry = true;

                                            if (String.Compare(dnString, 10, "0", 0, 1, StringComparison.OrdinalIgnoreCase) == 0)
                                            {
                                                // this server has the partition fully instantiated
                                                ntdsaNames.Add(ntdsaName);
                                                if (isADAM)
                                                {
                                                    serverPorts.Add(ntdsaName, (int)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.MsDSPortLDAP));
                                                }
                                                break;
                                            }
                                        }

                                        valueCount++;
                                    }

                                    if ((!foundPartitionEntry) && ((propertyName.Length >= propertyWithRangeInfo.Length) && (Utils.Compare(propertyName, 0, propertyWithRangeInfo.Length, propertyWithRangeInfo, 0, propertyWithRangeInfo.Length) != 0)))
                                    {
                                        needToContinueRangeRetrieval = true;
                                        ntdsaNamesForRangeRetrieval.Add(ntdsaName);
                                        rangeStart = valueCount;
                                    }
                                }
                                else
                                {
                                    // schema or configuration partition, so we add all the servers
                                    ntdsaNames.Add(ntdsaName);
                                    if (isADAM)
                                    {
                                        serverPorts.Add(ntdsaName, (int)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.MsDSPortLDAP));
                                    }
                                }
                            }
                            else
                            {
                                // server objects, just keep infor regarding the dns name (to be used later), if not available we will throw an error later
                                // when we try to retrieve this info for a valid DC/GC
                                if (res.Properties.Contains(PropertyManager.DnsHostName))
                                {
                                    serverNames.Add("CN=NTDS Settings," + (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.DistinguishedName),
                                                (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.DnsHostName));
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (resCol != null)
                        {
                            resCol.Dispose();
                        }
                    }

                    if (needToContinueRangeRetrieval)
                    {
                        // Now continue with range retrieval if necessary for msDS-HasInstantiatedNCs 
                        do
                        {
                            // Here we only need the NTDS settings objects of the ntdsaNames that need range retrieval

                            // this should be greater than 0, since needToContinueRangeRetrieval is true
                            Debug.Assert(ntdsaNamesForRangeRetrieval.Count > 0);

                            StringBuilder str = new StringBuilder(20);
                            if (ntdsaNamesForRangeRetrieval.Count > 1)
                            {
                                str.Append("(|");
                            }

                            foreach (string name in ntdsaNamesForRangeRetrieval)
                            {
                                str.Append("(");
                                str.Append(PropertyManager.NCName);
                                str.Append("=");
                                str.Append(Utils.GetEscapedFilterValue(name));
                                str.Append(")");
                            }

                            if (ntdsaNamesForRangeRetrieval.Count > 1)
                            {
                                str.Append(")");
                            }

                            // Clear it for the next round of range retrieval 
                            ntdsaNamesForRangeRetrieval.Clear();
                            needToContinueRangeRetrieval = false;

                            searcher2.Filter = "(&" + "(" + PropertyManager.ObjectCategory + "=nTDSDSA)" + str.ToString() + ")";

                            string propertyWithRangeInfo2 = PropertyManager.MsDSHasInstantiatedNCs + ";range=" + rangeStart + "-*";
                            searcher2.PropertiesToLoad.Clear();
                            searcher2.PropertiesToLoad.Add(propertyWithRangeInfo2);
                            searcher2.PropertiesToLoad.Add(PropertyManager.DistinguishedName);

                            SearchResultCollection resCol2 = searcher2.FindAll();

                            try
                            {
                                foreach (SearchResult res in resCol2)
                                {
                                    string ntdsaName = (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.DistinguishedName);
                                    // Here we need to check if we retrieved all the msDS-HasInstantiatedNCs values
                                    // if not we need to continue with the range retrieval (in parallel for the various ntdsa objects)
                                    string propertyName = null;
                                    if (!res.Properties.Contains(propertyWithRangeInfo2))
                                    {
                                        // find the property name with the range info
                                        foreach (string property in res.Properties.PropertyNames)
                                        {
                                            if (String.Compare(property, 0, PropertyManager.MsDSHasInstantiatedNCs, 0, PropertyManager.MsDSHasInstantiatedNCs.Length, StringComparison.OrdinalIgnoreCase) == 0)
                                            {
                                                propertyName = property;
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        propertyName = propertyWithRangeInfo2;
                                    }

                                    if (propertyName == null)
                                    {
                                        // property does not exist, possiblyno values, so continue
                                        continue;
                                    }

                                    bool foundPartitionEntry = false;
                                    int valueCount = 0;

                                    foreach (string dnString in res.Properties[propertyName])
                                    {
                                        Debug.Assert(dnString.Length > 10, "ConfigurationSet::GetReplicaList - dnWithBinary is not in the expected format.");

                                        if (((dnString.Length - 13) >= partitionName.Length) && (Utils.Compare(dnString, 13, partitionName.Length, partitionName, 0, partitionName.Length) == 0))
                                        {
                                            foundPartitionEntry = true;

                                            if (String.Compare(dnString, 10, "0", 0, 1, StringComparison.OrdinalIgnoreCase) == 0)
                                            {
                                                ntdsaNames.Add(ntdsaName);
                                                if (isADAM)
                                                {
                                                    serverPorts.Add(ntdsaName, (int)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.MsDSPortLDAP));
                                                }
                                                break;
                                            }
                                        }

                                        valueCount++;
                                    }

                                    if ((!foundPartitionEntry) && ((propertyName.Length >= propertyWithRangeInfo2.Length) && (Utils.Compare(propertyName, 0, propertyWithRangeInfo2.Length, propertyWithRangeInfo2, 0, propertyWithRangeInfo2.Length) != 0)))
                                    {
                                        needToContinueRangeRetrieval = true;
                                        ntdsaNamesForRangeRetrieval.Add(ntdsaName);
                                        rangeStart += valueCount;
                                    }
                                }
                            }
                            finally
                            {
                                resCol2.Dispose();
                            }
                        } while (needToContinueRangeRetrieval);
                    }
                }
                catch (COMException e)
                {
                    if (e.ErrorCode == unchecked((int)0x80072030) && siteName != null)
                    {
                        // this means that the site object does not exist, so we return an empty collection
                        return dnsNames;
                    }
                    else
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                    }
                }
            }
            finally
            {
                if (searchRootEntry != null)
                {
                    searchRootEntry.Dispose();
                }
            }

            // convert the ntdsa object names to server:port
            foreach (string ntdsaName in ntdsaNames)
            {
                string hostName = (string)serverNames[ntdsaName];

                if (hostName == null)
                {
                    Debug.Fail(string.Format(CultureInfo.InvariantCulture, "ConfigurationSet::GetReplicaList - no dnsHostName information for replica {0}", ntdsaName));
                    if (isADAM)
                    {
                        throw new ActiveDirectoryOperationException(Res.GetString(Res.NoHostNameOrPortNumber, ntdsaName));
                    }
                    else
                    {
                        throw new ActiveDirectoryOperationException(Res.GetString(Res.NoHostName, ntdsaName));
                    }
                }

                if (isADAM)
                {
                    if (serverPorts[ntdsaName] == null)
                    {
                        Debug.Fail(string.Format(CultureInfo.InvariantCulture, "ConfigurationSet::GetReplicaList - no port number  information for replica {0}", ntdsaName));
                        throw new ActiveDirectoryOperationException(Res.GetString(Res.NoHostNameOrPortNumber, ntdsaName));
                    }
                }

                if (isADAM)
                {
                    dnsNames.Add(hostName + ":" + (int)serverPorts[ntdsaName]);
                }
                else
                {
                    dnsNames.Add(hostName);
                }
            }

            return dnsNames;
        }
Example #12
0
		public static ApplicationPartition FindByName(DirectoryContext context, string distinguishedName)
		{
			DomainControllerInfo domainControllerInfo = null;
			string item;
			DirectoryContext newDirectoryContext = null;
			if (context != null)
			{
				if (context.Name != null || context.isRootDomain())
				{
					if (context.Name == null || context.isRootDomain() || context.isADAMConfigSet() || context.isServer())
					{
						if (distinguishedName != null)
						{
							if (distinguishedName.Length != 0)
							{
								if (Utils.IsValidDNFormat(distinguishedName))
								{
									context = new DirectoryContext(context);
									DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(context);
									DirectoryEntry directoryEntry = null;
									try
									{
										directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, directoryEntryManager.ExpandWellKnownDN(WellKnownDN.PartitionsContainer));
									}
									catch (COMException cOMException1)
									{
										COMException cOMException = cOMException1;
										throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
									}
									catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException)
									{
										object[] name = new object[1];
										name[0] = context.Name;
										throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", name));
									}
									StringBuilder stringBuilder = new StringBuilder(15);
									stringBuilder.Append("(&(");
									stringBuilder.Append(PropertyManager.ObjectCategory);
									stringBuilder.Append("=crossRef)(");
									stringBuilder.Append(PropertyManager.SystemFlags);
									stringBuilder.Append(":1.2.840.113556.1.4.804:=");
									stringBuilder.Append(1);
									stringBuilder.Append(")(!(");
									stringBuilder.Append(PropertyManager.SystemFlags);
									stringBuilder.Append(":1.2.840.113556.1.4.803:=");
									stringBuilder.Append(2);
									stringBuilder.Append("))(");
									stringBuilder.Append(PropertyManager.NCName);
									stringBuilder.Append("=");
									stringBuilder.Append(Utils.GetEscapedFilterValue(distinguishedName));
									stringBuilder.Append("))");
									string str = stringBuilder.ToString();
									string[] dnsRoot = new string[2];
									dnsRoot[0] = PropertyManager.DnsRoot;
									dnsRoot[1] = PropertyManager.NCName;
									ADSearcher aDSearcher = new ADSearcher(directoryEntry, str, dnsRoot, SearchScope.OneLevel, false, false);
									SearchResult searchResult = null;
									try
									{
										try
										{
											searchResult = aDSearcher.FindOne();
										}
										catch (COMException cOMException3)
										{
											COMException cOMException2 = cOMException3;
											if (cOMException2.ErrorCode != -2147016656)
											{
												throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException2);
											}
											else
											{
												throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
											}
										}
									}
									finally
									{
										directoryEntry.Dispose();
									}
									if (searchResult != null)
									{
										string str1 = null;
										try
										{
											if (searchResult.Properties[PropertyManager.DnsRoot].Count > 0)
											{
												item = (string)searchResult.Properties[PropertyManager.DnsRoot][0];
											}
											else
											{
												item = null;
											}
											str1 = item;
										}
										catch (COMException cOMException5)
										{
											COMException cOMException4 = cOMException5;
											throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException4);
										}
										ApplicationPartitionType applicationPartitionType = ApplicationPartition.GetApplicationPartitionType(context);
										if (context.ContextType != DirectoryContextType.DirectoryServer)
										{
											if (applicationPartitionType != ApplicationPartitionType.ADApplicationPartition)
											{
												string name1 = ConfigurationSet.FindOneAdamInstance(context.Name, context, distinguishedName, null).Name;
												newDirectoryContext = Utils.GetNewDirectoryContext(name1, DirectoryContextType.DirectoryServer, context);
											}
											else
											{
												int num = Locator.DsGetDcNameWrapper(null, str1, null, (long)0x8000, out domainControllerInfo);
												if (num != 0x54b)
												{
													if (num == 0)
													{
														string str2 = domainControllerInfo.DomainControllerName.Substring(2);
														newDirectoryContext = Utils.GetNewDirectoryContext(str2, DirectoryContextType.DirectoryServer, context);
													}
													else
													{
														throw ExceptionHelper.GetExceptionFromErrorCode(num);
													}
												}
												else
												{
													throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
												}
											}
										}
										else
										{
											bool flag = false;
											DistinguishedName distinguishedName1 = new DistinguishedName(distinguishedName);
											DirectoryEntry directoryEntry1 = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
											try
											{
												try
												{
													foreach (string item1 in directoryEntry1.Properties[PropertyManager.NamingContexts])
													{
														DistinguishedName distinguishedName2 = new DistinguishedName(item1);
														if (!distinguishedName2.Equals(distinguishedName1))
														{
															continue;
														}
														flag = true;
														break;
													}
												}
												catch (COMException cOMException7)
												{
													COMException cOMException6 = cOMException7;
													throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException6);
												}
											}
											finally
											{
												directoryEntry1.Dispose();
											}
											if (flag)
											{
												newDirectoryContext = context;
											}
											else
											{
												throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
											}
										}
										ApplicationPartition applicationPartition = new ApplicationPartition(newDirectoryContext, (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.NCName), str1, applicationPartitionType, directoryEntryManager);
										return applicationPartition;
									}
									else
									{
										throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
									}
								}
								else
								{
									throw new ArgumentException(Res.GetString("InvalidDNFormat"), "distinguishedName");
								}
							}
							else
							{
								throw new ArgumentException(Res.GetString("EmptyStringParameter"), "distinguishedName");
							}
						}
						else
						{
							throw new ArgumentNullException("distinguishedName");
						}
					}
					else
					{
						throw new ArgumentException(Res.GetString("NotADOrADAM"), "context");
					}
				}
				else
				{
					throw new ArgumentException(Res.GetString("ContextNotAssociatedWithDomain"), "context");
				}
			}
			else
			{
				throw new ArgumentNullException("context");
			}
		}
 internal void RemoveIfExists(string distinguishedName)
 {
     object key = distinguishedName;
     if (string.Compare(distinguishedName, "rootdse", StringComparison.OrdinalIgnoreCase) != 0)
     {
         key = new DistinguishedName(distinguishedName);
     }
     if (this.directoryEntries.ContainsKey(key))
     {
         DirectoryEntry entry = (DirectoryEntry) this.directoryEntries[key];
         if (entry != null)
         {
             this.directoryEntries.Remove(key);
             entry.Dispose();
         }
     }
 }
 public static ApplicationPartition FindByName(DirectoryContext context, string distinguishedName)
 {
     DirectoryEntryManager directoryEntryMgr = null;
     DirectoryContext context2 = null;
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     if ((context.Name == null) && !context.isRootDomain())
     {
         throw new ArgumentException(Res.GetString("ContextNotAssociatedWithDomain"), "context");
     }
     if (((context.Name != null) && !context.isRootDomain()) && (!context.isADAMConfigSet() && !context.isServer()))
     {
         throw new ArgumentException(Res.GetString("NotADOrADAM"), "context");
     }
     if (distinguishedName == null)
     {
         throw new ArgumentNullException("distinguishedName");
     }
     if (distinguishedName.Length == 0)
     {
         throw new ArgumentException(Res.GetString("EmptyStringParameter"), "distinguishedName");
     }
     if (!Utils.IsValidDNFormat(distinguishedName))
     {
         throw new ArgumentException(Res.GetString("InvalidDNFormat"), "distinguishedName");
     }
     context = new DirectoryContext(context);
     directoryEntryMgr = new DirectoryEntryManager(context);
     DirectoryEntry searchRoot = null;
     try
     {
         searchRoot = DirectoryEntryManager.GetDirectoryEntry(context, directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer));
     }
     catch (COMException exception)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
     }
     catch (ActiveDirectoryObjectNotFoundException)
     {
         throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { context.Name }));
     }
     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("))(");
     builder.Append(PropertyManager.NCName);
     builder.Append("=");
     builder.Append(Utils.GetEscapedFilterValue(distinguishedName));
     builder.Append("))");
     string filter = builder.ToString();
     string[] propertiesToLoad = new string[] { PropertyManager.DnsRoot, PropertyManager.NCName };
     ADSearcher searcher = new ADSearcher(searchRoot, filter, propertiesToLoad, SearchScope.OneLevel, false, false);
     SearchResult res = null;
     try
     {
         res = searcher.FindOne();
     }
     catch (COMException exception2)
     {
         if (exception2.ErrorCode == -2147016656)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
         }
         throw ExceptionHelper.GetExceptionFromCOMException(context, exception2);
     }
     finally
     {
         searchRoot.Dispose();
     }
     if (res == null)
     {
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
     }
     string domainName = null;
     try
     {
         domainName = (res.Properties[PropertyManager.DnsRoot].Count > 0) ? ((string) res.Properties[PropertyManager.DnsRoot][0]) : null;
     }
     catch (COMException exception3)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(context, exception3);
     }
     ApplicationPartitionType applicationPartitionType = GetApplicationPartitionType(context);
     if (context.ContextType != DirectoryContextType.DirectoryServer)
     {
         if (applicationPartitionType == ApplicationPartitionType.ADApplicationPartition)
         {
             DomainControllerInfo info;
             int errorCode = 0;
             errorCode = Locator.DsGetDcNameWrapper(null, domainName, null, 0x8000L, out info);
             if (errorCode == 0x54b)
             {
                 throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
             }
             if (errorCode != 0)
             {
                 throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
             }
             context2 = Utils.GetNewDirectoryContext(info.DomainControllerName.Substring(2), DirectoryContextType.DirectoryServer, context);
         }
         else
         {
             context2 = Utils.GetNewDirectoryContext(ConfigurationSet.FindOneAdamInstance(context.Name, context, distinguishedName, null).Name, DirectoryContextType.DirectoryServer, context);
         }
         goto Label_03FC;
     }
     bool flag = false;
     DistinguishedName dn = new DistinguishedName(distinguishedName);
     DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
     try
     {
         foreach (string str3 in directoryEntry.Properties[PropertyManager.NamingContexts])
         {
             DistinguishedName name2 = new DistinguishedName(str3);
             if (name2.Equals(dn))
             {
                 flag = true;
                 goto Label_0352;
             }
         }
     }
     catch (COMException exception4)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(context, exception4);
     }
     finally
     {
         directoryEntry.Dispose();
     }
 Label_0352:
     if (!flag)
     {
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
     }
     context2 = context;
 Label_03FC:
     return new ApplicationPartition(context2, (string) PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.NCName), domainName, applicationPartitionType, directoryEntryMgr);
 }
        public static ApplicationPartition FindByName(DirectoryContext context, string distinguishedName)
        {
            DirectoryEntryManager directoryEntryMgr = null;
            DirectoryContext      context2          = null;

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if ((context.Name == null) && !context.isRootDomain())
            {
                throw new ArgumentException(Res.GetString("ContextNotAssociatedWithDomain"), "context");
            }
            if (((context.Name != null) && !context.isRootDomain()) && (!context.isADAMConfigSet() && !context.isServer()))
            {
                throw new ArgumentException(Res.GetString("NotADOrADAM"), "context");
            }
            if (distinguishedName == null)
            {
                throw new ArgumentNullException("distinguishedName");
            }
            if (distinguishedName.Length == 0)
            {
                throw new ArgumentException(Res.GetString("EmptyStringParameter"), "distinguishedName");
            }
            if (!Utils.IsValidDNFormat(distinguishedName))
            {
                throw new ArgumentException(Res.GetString("InvalidDNFormat"), "distinguishedName");
            }
            context           = new DirectoryContext(context);
            directoryEntryMgr = new DirectoryEntryManager(context);
            DirectoryEntry searchRoot = null;

            try
            {
                searchRoot = DirectoryEntryManager.GetDirectoryEntry(context, directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer));
            }
            catch (COMException exception)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { context.Name }));
            }
            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("))(");
            builder.Append(PropertyManager.NCName);
            builder.Append("=");
            builder.Append(Utils.GetEscapedFilterValue(distinguishedName));
            builder.Append("))");
            string filter = builder.ToString();

            string[]     propertiesToLoad = new string[] { PropertyManager.DnsRoot, PropertyManager.NCName };
            ADSearcher   searcher         = new ADSearcher(searchRoot, filter, propertiesToLoad, SearchScope.OneLevel, false, false);
            SearchResult res = null;

            try
            {
                res = searcher.FindOne();
            }
            catch (COMException exception2)
            {
                if (exception2.ErrorCode == -2147016656)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
                }
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception2);
            }
            finally
            {
                searchRoot.Dispose();
            }
            if (res == null)
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
            }
            string domainName = null;

            try
            {
                domainName = (res.Properties[PropertyManager.DnsRoot].Count > 0) ? ((string)res.Properties[PropertyManager.DnsRoot][0]) : null;
            }
            catch (COMException exception3)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception3);
            }
            ApplicationPartitionType applicationPartitionType = GetApplicationPartitionType(context);

            if (context.ContextType != DirectoryContextType.DirectoryServer)
            {
                if (applicationPartitionType == ApplicationPartitionType.ADApplicationPartition)
                {
                    DomainControllerInfo info;
                    int errorCode = 0;
                    errorCode = Locator.DsGetDcNameWrapper(null, domainName, null, 0x8000L, out info);
                    if (errorCode == 0x54b)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
                    }
                    if (errorCode != 0)
                    {
                        throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                    }
                    context2 = Utils.GetNewDirectoryContext(info.DomainControllerName.Substring(2), DirectoryContextType.DirectoryServer, context);
                }
                else
                {
                    context2 = Utils.GetNewDirectoryContext(ConfigurationSet.FindOneAdamInstance(context.Name, context, distinguishedName, null).Name, DirectoryContextType.DirectoryServer, context);
                }
                goto Label_03FC;
            }
            bool flag                        = false;
            DistinguishedName dn             = new DistinguishedName(distinguishedName);
            DirectoryEntry    directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);

            try
            {
                foreach (string str3 in directoryEntry.Properties[PropertyManager.NamingContexts])
                {
                    DistinguishedName name2 = new DistinguishedName(str3);
                    if (name2.Equals(dn))
                    {
                        flag = true;
                        goto Label_0352;
                    }
                }
            }
            catch (COMException exception4)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception4);
            }
            finally
            {
                directoryEntry.Dispose();
            }
Label_0352:
            if (!flag)
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
            }
            context2 = context;
Label_03FC:
            return(new ApplicationPartition(context2, (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.NCName), domainName, applicationPartitionType, directoryEntryMgr));
        }