Exemplo n.º 1
1
 public static IEnumerable<DomainController> FindAllDomainControllers(string domain)
 {
     try
     {
         var context = new DirectoryContext(DirectoryContextType.Domain, domain);
         var searchResult = DomainController.FindAll(context);
         var dcs = new DomainController[searchResult.Count];
         searchResult.CopyTo(dcs, 0);
         return dcs.AsEnumerable();
     }
     catch (Exception)
     {
         return null;
     }
 }
        private DirectoryServer GetSchemaRoleOwner()
        {
            DirectoryServer server2;

            try
            {
                this.schemaEntry.RefreshCache();
                if (base.context.isADAMConfigSet())
                {
                    string adamDnsHostNameFromNTDSA = Utils.GetAdamDnsHostNameFromNTDSA(base.context, (string)PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
                    return(new AdamInstance(Utils.GetNewDirectoryContext(adamDnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, base.context), adamDnsHostNameFromNTDSA));
                }
                DirectoryServer server = null;
                if (Utils.CheckCapability(base.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE), Capability.ActiveDirectory))
                {
                    string dnsHostNameFromNTDSA = Utils.GetDnsHostNameFromNTDSA(base.context, (string)PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
                    server = new DomainController(Utils.GetNewDirectoryContext(dnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, base.context), dnsHostNameFromNTDSA);
                }
                else
                {
                    string adamInstanceName = Utils.GetAdamDnsHostNameFromNTDSA(base.context, (string)PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
                    server = new AdamInstance(Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, base.context), adamInstanceName);
                }
                server2 = server;
            }
            catch (COMException exception)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception);
            }
            return(server2);
        }
        internal static DomainController FindOneWithCredentialValidation(DirectoryContext context, string siteName, LocatorOptions flag)
        {
            bool flag2 = false;
            bool flag3 = false;

            context = new DirectoryContext(context);
            DomainController dc = FindOneInternal(context, context.Name, siteName, flag);

            try
            {
                ValidateCredential(dc, context);
                flag3 = true;
            }
            catch (COMException exception)
            {
                if (exception.ErrorCode != -2147016646)
                {
                    throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(context, exception);
                }
                if ((flag & LocatorOptions.ForceRediscovery) != 0L)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFoundInDomain", new object[] { context.Name }), typeof(DomainController), null);
                }
                flag2 = true;
            }
            finally
            {
                if (!flag3)
                {
                    dc.Dispose();
                }
            }
            if (flag2)
            {
                flag3 = false;
                dc    = FindOneInternal(context, context.Name, siteName, flag | LocatorOptions.ForceRediscovery);
                try
                {
                    ValidateCredential(dc, context);
                    flag3 = true;
                }
                catch (COMException exception2)
                {
                    if (exception2.ErrorCode == -2147016646)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFoundInDomain", new object[] { context.Name }), typeof(DomainController), null);
                    }
                    throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(context, exception2);
                }
                finally
                {
                    if (!flag3)
                    {
                        dc.Dispose();
                    }
                }
            }
            return(dc);
        }
        public int IndexOf(DomainController domainController)
        {
            Contract.Requires(domainController != null);
            Contract.Ensures(Contract.Result <int>() >= -1);
            Contract.Ensures(Contract.Result <int>() < this.Count);

            return(default(int));
        }
    public int IndexOf(DomainController domainController)
    {
      Contract.Requires(domainController != null);
      Contract.Ensures(Contract.Result<int>() >= -1);
      Contract.Ensures(Contract.Result<int>() < this.Count);

      return default(int);
    }
        internal static void ValidateCredential(DomainController dc, DirectoryContext context)
        {
            DirectoryEntry entry;

            if (DirectoryContext.ServerBindSupported)
            {
                entry = new DirectoryEntry("LDAP://" + dc.Name + "/RootDSE", context.UserName, context.Password, Utils.DefaultAuthType | AuthenticationTypes.ServerBind);
            }
            else
            {
                entry = new DirectoryEntry("LDAP://" + dc.Name + "/RootDSE", context.UserName, context.Password, Utils.DefaultAuthType);
            }
            entry.Bind(true);
        }
Exemplo n.º 7
0
        public int IndexOf(DomainController domainController)
        {
            if (domainController == null)
                throw new ArgumentNullException("domainController");

            for (int i = 0; i < InnerList.Count; i++)
            {
                DomainController tmp = (DomainController)InnerList[i];
                if (Utils.Compare(tmp.Name, domainController.Name) == 0)
                {
                    return i;
                }
            }
            return -1;
        }
Exemplo n.º 8
0
        public ADDomainController(ActiveDirectoryContext Context, DomainController DomainController, ADDomain Domain, bool IsSiteServer, bool IsWritable)
        {
            this.context = Context;

            this.Domain = Domain;
            this.DomainController = DomainController;

            this.Name = DomainController.Name;
            this.SiteName = DomainController.SiteName;

            this.IsSiteServer = IsSiteServer;
            this.IsWritable = IsWritable;

            this.AvailableWhen = null;
        }
 public bool Contains(DomainController domainController)
 {
     if (domainController == null)
     {
         throw new ArgumentNullException("domainController");
     }
     for (int i = 0; i < base.InnerList.Count; i++)
     {
         DomainController controller = (DomainController) base.InnerList[i];
         if (Utils.Compare(controller.Name, domainController.Name) == 0)
         {
             return true;
         }
     }
     return false;
 }
Exemplo n.º 10
0
 public int IndexOf(DomainController domainController)
 {
     if (domainController == null)
     {
         throw new ArgumentNullException("domainController");
     }
     for (int i = 0; i < base.InnerList.Count; i++)
     {
         DomainController controller = (DomainController)base.InnerList[i];
         if (Utils.Compare(controller.Name, domainController.Name) == 0)
         {
             return(i);
         }
     }
     return(-1);
 }
Exemplo n.º 11
0
        public int IndexOf(DomainController domainController)
        {
            if (domainController == null)
            {
                throw new ArgumentNullException(nameof(domainController));
            }

            for (int i = 0; i < InnerList.Count; i++)
            {
                DomainController tmp = (DomainController)InnerList[i];
                if (Utils.Compare(tmp.Name, domainController.Name) == 0)
                {
                    return(i);
                }
            }
            return(-1);
        }
Exemplo n.º 12
0
        public bool Contains(DomainController domainController)
        {
            if (domainController == null)
            {
                throw new ArgumentNullException("domainController");
            }

            for (int i = 0; i < InnerList.Count; i++)
            {
                DomainController tmp = (DomainController)InnerList[i];
                if (Utils.Compare(tmp.Name, domainController.Name) == 0)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 13
0
        private DirectoryServer GetSchemaRoleOwner()
        {
            try
            {
                _schemaEntry.RefreshCache();

                if (context.isADAMConfigSet())
                {
                    // ADAM
                    string           adamInstName    = Utils.GetAdamDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, _schemaEntry, PropertyManager.FsmoRoleOwner));
                    DirectoryContext adamInstContext = Utils.GetNewDirectoryContext(adamInstName, DirectoryContextType.DirectoryServer, context);
                    return(new AdamInstance(adamInstContext, adamInstName));
                }
                else
                {
                    // could be AD or adam server

                    DirectoryServer server  = null;
                    DirectoryEntry  rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);

                    if (Utils.CheckCapability(rootDSE, Capability.ActiveDirectory))
                    {
                        string           dcName    = Utils.GetDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, _schemaEntry, PropertyManager.FsmoRoleOwner));
                        DirectoryContext dcContext = Utils.GetNewDirectoryContext(dcName, DirectoryContextType.DirectoryServer, context);
                        server = new DomainController(dcContext, dcName);
                    }
                    else
                    {
                        // ADAM case again
                        string           adamInstName    = Utils.GetAdamDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, _schemaEntry, PropertyManager.FsmoRoleOwner));
                        DirectoryContext adamInstContext = Utils.GetNewDirectoryContext(adamInstName, DirectoryContextType.DirectoryServer, context);
                        server = new AdamInstance(adamInstContext, adamInstName);
                    }
                    return(server);
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
        }
Exemplo n.º 14
0
        private DirectoryServer GetSchemaRoleOwner()
        {
            DirectoryServer adamInstance;
            DirectoryServer directoryServer;

            try
            {
                this.schemaEntry.RefreshCache();
                if (!this.context.isADAMConfigSet())
                {
                    DirectoryEntry cachedDirectoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                    if (!Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectory))
                    {
                        string           adamDnsHostNameFromNTDSA = Utils.GetAdamDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
                        DirectoryContext newDirectoryContext      = Utils.GetNewDirectoryContext(adamDnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, this.context);
                        adamInstance = new AdamInstance(newDirectoryContext, adamDnsHostNameFromNTDSA);
                    }
                    else
                    {
                        string           dnsHostNameFromNTDSA = Utils.GetDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
                        DirectoryContext directoryContext     = Utils.GetNewDirectoryContext(dnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, this.context);
                        adamInstance = new DomainController(directoryContext, dnsHostNameFromNTDSA);
                    }
                    directoryServer = adamInstance;
                }
                else
                {
                    string           str = Utils.GetAdamDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
                    DirectoryContext newDirectoryContext1 = Utils.GetNewDirectoryContext(str, DirectoryContextType.DirectoryServer, this.context);
                    directoryServer = new AdamInstance(newDirectoryContext1, str);
                }
            }
            catch (COMException cOMException1)
            {
                COMException cOMException = cOMException1;
                throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException);
            }
            return(directoryServer);
        }
		public int IndexOf(DomainController domainController)
		{
			if (domainController != null)
			{
				int num = 0;
				while (num < base.InnerList.Count)
				{
					DomainController item = (DomainController)base.InnerList[num];
					if (Utils.Compare(item.Name, domainController.Name) != 0)
					{
						num++;
					}
					else
					{
						return num;
					}
				}
				return -1;
			}
			else
			{
				throw new ArgumentNullException("domainController");
			}
		}
 private DirectoryServer GetSchemaRoleOwner()
 {
     DirectoryServer server2;
     try
     {
         this.schemaEntry.RefreshCache();
         if (base.context.isADAMConfigSet())
         {
             string adamDnsHostNameFromNTDSA = Utils.GetAdamDnsHostNameFromNTDSA(base.context, (string) PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
             return new AdamInstance(Utils.GetNewDirectoryContext(adamDnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, base.context), adamDnsHostNameFromNTDSA);
         }
         DirectoryServer server = null;
         if (Utils.CheckCapability(base.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE), Capability.ActiveDirectory))
         {
             string dnsHostNameFromNTDSA = Utils.GetDnsHostNameFromNTDSA(base.context, (string) PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
             server = new DomainController(Utils.GetNewDirectoryContext(dnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, base.context), dnsHostNameFromNTDSA);
         }
         else
         {
             string adamInstanceName = Utils.GetAdamDnsHostNameFromNTDSA(base.context, (string) PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
             server = new AdamInstance(Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, base.context), adamInstanceName);
         }
         server2 = server;
     }
     catch (COMException exception)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception);
     }
     return server2;
 }
Exemplo n.º 17
0
		public void CopyTo(DomainController[] domainControllers, int index)
		{
			base.InnerList.CopyTo(domainControllers, index);
		}
Exemplo n.º 18
0
        private void GetServers()
        {
            ADSearcher adSearcher = new ADSearcher(cachedEntry,
                                                  "(&(objectClass=server)(objectCategory=server))",
                                                  new string[] { "dNSHostName" },
                                                  SearchScope.Subtree);
            SearchResultCollection results = null;
            try
            {
                results = adSearcher.FindAll();
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }

            try
            {
                foreach (SearchResult result in results)
                {
                    string hostName = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsHostName);
                    DirectoryEntry de = result.GetDirectoryEntry();
                    DirectoryEntry child = null;
                    DirectoryServer replica = null;
                    // make sure that the server is not demoted
                    try
                    {
                        child = de.Children.Find("CN=NTDS Settings", "nTDSDSA");
                    }
                    catch (COMException e)
                    {
                        if (e.ErrorCode == unchecked((int)0x80072030))
                        {
                            continue;
                        }
                        else
                            throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                    }
                    if (IsADAM)
                    {
                        int port = (int)PropertyManager.GetPropertyValue(context, child, PropertyManager.MsDSPortLDAP);
                        string fullHostName = hostName;
                        if (port != 389)
                        {
                            fullHostName = hostName + ":" + port;
                        }
                        replica = new AdamInstance(Utils.GetNewDirectoryContext(fullHostName, DirectoryContextType.DirectoryServer, context), fullHostName);
                    }
                    else
                        replica = new DomainController(Utils.GetNewDirectoryContext(hostName, DirectoryContextType.DirectoryServer, context), hostName);

                    _servers.Add(replica);
                }
            }
            finally
            {
                results.Dispose();
            }
        }
Exemplo n.º 19
0
		public int IndexOf (DomainController domainController)
		{
			throw new NotImplementedException ();
		}
Exemplo n.º 20
0
		private DirectoryServer FindDirectoryServerInternal(string siteName, bool forceRediscovery)
		{
			DomainControllerInfo domainControllerInfo = null;
			LocatorOptions locatorOption = 0;
			if (siteName == null || siteName.Length != 0)
			{
				if (this.committed)
				{
					if (forceRediscovery)
					{
						locatorOption = LocatorOptions.ForceRediscovery;
					}
					int num = Locator.DsGetDcNameWrapper(null, this.dnsName, siteName, (long)locatorOption | 0x8000, out domainControllerInfo);
					if (num != 0x54b)
					{
						if (num == 0)
						{
							string str = domainControllerInfo.DomainControllerName.Substring(2);
							DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(str, DirectoryContextType.DirectoryServer, this.context);
							DirectoryServer domainController = new DomainController(newDirectoryContext, str);
							return domainController;
						}
						else
						{
							throw ExceptionHelper.GetExceptionFromErrorCode(num);
						}
					}
					else
					{
						throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ReplicaNotFound"), typeof(DirectoryServer), null);
					}
				}
				else
				{
					throw new InvalidOperationException(Res.GetString("CannotPerformOperationOnUncommittedObject"));
				}
			}
			else
			{
				throw new ArgumentException(Res.GetString("EmptyStringParameter"), "siteName");
			}
		}
Exemplo n.º 21
0
		private void GetPreferredBridgeheadServers(ActiveDirectoryTransportType transport)
		{
			string str;
			DirectoryServer domainController;
			string str1 = string.Concat("CN=Servers,", PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.DistinguishedName));
			if (transport != ActiveDirectoryTransportType.Smtp)
			{
				str = string.Concat("CN=IP,CN=Inter-Site Transports,", this.siteDN);
			}
			else
			{
				str = string.Concat("CN=SMTP,CN=Inter-Site Transports,", this.siteDN);
			}
			DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, str1);
			string[] strArrays = new string[2];
			strArrays[0] = "dNSHostName";
			strArrays[1] = "distinguishedName";
			ADSearcher aDSearcher = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=server)(objectCategory=Server)(bridgeheadTransportList=", Utils.GetEscapedFilterValue(str), "))"), strArrays, SearchScope.OneLevel);
			SearchResultCollection searchResultCollections = null;
			try
			{
				searchResultCollections = aDSearcher.FindAll();
			}
			catch (COMException cOMException1)
			{
				COMException cOMException = cOMException1;
				throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException);
			}
			try
			{
				DirectoryEntry directoryEntry1 = null;
				foreach (SearchResult searchResult in searchResultCollections)
				{
					string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DnsHostName);
					DirectoryEntry directoryEntry2 = searchResult.GetDirectoryEntry();
					try
					{
						directoryEntry1 = directoryEntry2.Children.Find("CN=NTDS Settings", "nTDSDSA");
					}
					catch (COMException cOMException3)
					{
						COMException cOMException2 = cOMException3;
						throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException2);
					}
					if (!this.IsADAM)
					{
						domainController = new DomainController(Utils.GetNewDirectoryContext(searchResultPropertyValue, DirectoryContextType.DirectoryServer, this.context), searchResultPropertyValue);
					}
					else
					{
						int propertyValue = (int)PropertyManager.GetPropertyValue(this.context, directoryEntry1, PropertyManager.MsDSPortLDAP);
						string str2 = searchResultPropertyValue;
						if (propertyValue != 0x185)
						{
							str2 = string.Concat(searchResultPropertyValue, ":", propertyValue);
						}
						domainController = new AdamInstance(Utils.GetNewDirectoryContext(str2, DirectoryContextType.DirectoryServer, this.context), str2);
					}
					if (transport != ActiveDirectoryTransportType.Smtp)
					{
						this.RPCBridgeheadServers.Add(domainController);
					}
					else
					{
						this.SMTPBridgeheadServers.Add(domainController);
					}
				}
			}
			finally
			{
				directoryEntry.Dispose();
				searchResultCollections.Dispose();
			}
		}
Exemplo n.º 22
0
		private ReadOnlyDirectoryServerCollection GetBridgeheadServers()
		{
			DirectoryServer domainController;
			NativeComInterfaces.IAdsPathname pathname = (NativeComInterfaces.IAdsPathname)(new NativeComInterfaces.Pathname());
			pathname.EscapedMode = 4;
			ReadOnlyDirectoryServerCollection readOnlyDirectoryServerCollection = new ReadOnlyDirectoryServerCollection();
			if (this.existing)
			{
				Hashtable hashtables = new Hashtable();
				Hashtable hashtables1 = new Hashtable();
				Hashtable hashtables2 = new Hashtable();
				string str = string.Concat("CN=Servers,", (string)PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.DistinguishedName));
				DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, str);
				try
				{
					string[] strArrays = new string[4];
					strArrays[0] = "fromServer";
					strArrays[1] = "distinguishedName";
					strArrays[2] = "dNSHostName";
					strArrays[3] = "objectCategory";
					ADSearcher aDSearcher = new ADSearcher(directoryEntry, "(|(objectCategory=server)(objectCategory=NTDSConnection))", strArrays, SearchScope.Subtree, true, true);
					SearchResultCollection searchResultCollections = null;
					try
					{
						searchResultCollections = aDSearcher.FindAll();
					}
					catch (COMException cOMException1)
					{
						COMException cOMException = cOMException1;
						throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException);
					}
					try
					{
						foreach (SearchResult searchResult in searchResultCollections)
						{
							string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.ObjectCategory);
							if (Utils.Compare(searchResultPropertyValue, 0, "CN=Server".Length, "CN=Server", 0, "CN=Server".Length) != 0)
							{
								continue;
							}
							hashtables2.Add((string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DistinguishedName), (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DnsHostName));
						}
						foreach (SearchResult searchResult1 in searchResultCollections)
						{
							string searchResultPropertyValue1 = (string)PropertyManager.GetSearchResultPropertyValue(searchResult1, PropertyManager.ObjectCategory);
							if (Utils.Compare(searchResultPropertyValue1, 0, "CN=Server".Length, "CN=Server", 0, "CN=Server".Length) == 0)
							{
								continue;
							}
							string str1 = (string)PropertyManager.GetSearchResultPropertyValue(searchResult1, PropertyManager.FromServer);
							string partialDN = Utils.GetPartialDN(str1, 3);
							pathname.Set(partialDN, 4);
							partialDN = pathname.Retrieve(11);
							partialDN = partialDN.Substring(3);
							string partialDN1 = Utils.GetPartialDN((string)PropertyManager.GetSearchResultPropertyValue(searchResult1, PropertyManager.DistinguishedName), 2);
							if (hashtables.Contains(partialDN1))
							{
								continue;
							}
							string item = (string)hashtables2[partialDN1];
							if (!hashtables1.Contains(partialDN1))
							{
								hashtables1.Add(partialDN1, item);
							}
							if (Utils.Compare((string)PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.Cn), partialDN) == 0)
							{
								continue;
							}
							hashtables.Add(partialDN1, item);
							hashtables1.Remove(partialDN1);
						}
					}
					finally
					{
						searchResultCollections.Dispose();
					}
				}
				finally
				{
					directoryEntry.Dispose();
				}
				if (hashtables1.Count != 0)
				{
					DirectoryEntry directoryEntry1 = DirectoryEntryManager.GetDirectoryEntry(this.context, this.siteDN);
					StringBuilder stringBuilder = new StringBuilder(100);
					if (hashtables1.Count > 1)
					{
						stringBuilder.Append("(|");
					}
					foreach (DictionaryEntry hashtable in hashtables1)
					{
						stringBuilder.Append("(fromServer=");
						stringBuilder.Append("CN=NTDS Settings,");
						stringBuilder.Append(Utils.GetEscapedFilterValue((string)hashtable.Key));
						stringBuilder.Append(")");
					}
					if (hashtables1.Count > 1)
					{
						stringBuilder.Append(")");
					}
					string[] strArrays1 = new string[2];
					strArrays1[0] = "fromServer";
					strArrays1[1] = "distinguishedName";
					ADSearcher aDSearcher1 = new ADSearcher(directoryEntry1, string.Concat("(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)", stringBuilder.ToString(), ")"), strArrays1, SearchScope.Subtree);
					SearchResultCollection searchResultCollections1 = null;
					try
					{
						searchResultCollections1 = aDSearcher1.FindAll();
					}
					catch (COMException cOMException3)
					{
						COMException cOMException2 = cOMException3;
						throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException2);
					}
					try
					{
						foreach (SearchResult searchResult2 in searchResultCollections1)
						{
							string searchResultPropertyValue2 = (string)PropertyManager.GetSearchResultPropertyValue(searchResult2, PropertyManager.FromServer);
							string str2 = searchResultPropertyValue2.Substring(17);
							if (!hashtables1.Contains(str2))
							{
								continue;
							}
							string partialDN2 = Utils.GetPartialDN((string)PropertyManager.GetSearchResultPropertyValue(searchResult2, PropertyManager.DistinguishedName), 4);
							pathname.Set(partialDN2, 4);
							partialDN2 = pathname.Retrieve(11);
							partialDN2 = partialDN2.Substring(3);
							if (Utils.Compare(partialDN2, (string)PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.Cn)) == 0)
							{
								continue;
							}
							string item1 = (string)hashtables1[str2];
							hashtables1.Remove(str2);
							hashtables.Add(str2, item1);
						}
					}
					finally
					{
						searchResultCollections1.Dispose();
						directoryEntry1.Dispose();
					}
				}
				foreach (DictionaryEntry dictionaryEntry in hashtables)
				{
					string value = (string)dictionaryEntry.Value;
					if (!this.IsADAM)
					{
						domainController = new DomainController(Utils.GetNewDirectoryContext(value, DirectoryContextType.DirectoryServer, this.context), value);
					}
					else
					{
						DirectoryEntry directoryEntry2 = DirectoryEntryManager.GetDirectoryEntry(this.context, string.Concat("CN=NTDS Settings,", dictionaryEntry.Key));
						int propertyValue = (int)PropertyManager.GetPropertyValue(this.context, directoryEntry2, PropertyManager.MsDSPortLDAP);
						string str3 = value;
						if (propertyValue != 0x185)
						{
							str3 = string.Concat(value, ":", propertyValue);
						}
						domainController = new AdamInstance(Utils.GetNewDirectoryContext(str3, DirectoryContextType.DirectoryServer, this.context), str3);
					}
					readOnlyDirectoryServerCollection.Add(domainController);
				}
			}
			return readOnlyDirectoryServerCollection;
		}
 private ReadOnlyDirectoryServerCollection GetBridgeheadServers()
 {
     NativeComInterfaces.IAdsPathname pathname = (NativeComInterfaces.IAdsPathname) new NativeComInterfaces.Pathname();
     pathname.EscapedMode = 4;
     ReadOnlyDirectoryServerCollection servers = new ReadOnlyDirectoryServerCollection();
     if (this.existing)
     {
         Hashtable hashtable = new Hashtable();
         Hashtable hashtable2 = new Hashtable();
         Hashtable hashtable3 = new Hashtable();
         string dn = "CN=Servers," + ((string) PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.DistinguishedName));
         using (DirectoryEntry entry = DirectoryEntryManager.GetDirectoryEntry(this.context, dn))
         {
             ADSearcher searcher = new ADSearcher(entry, "(|(objectCategory=server)(objectCategory=NTDSConnection))", new string[] { "fromServer", "distinguishedName", "dNSHostName", "objectCategory" }, SearchScope.Subtree, true, true);
             SearchResultCollection results = null;
             try
             {
                 results = searcher.FindAll();
             }
             catch (COMException exception)
             {
                 throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception);
             }
             try
             {
                 foreach (SearchResult result in results)
                 {
                     string searchResultPropertyValue = (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.ObjectCategory);
                     if (Utils.Compare(searchResultPropertyValue, 0, "CN=Server".Length, "CN=Server", 0, "CN=Server".Length) == 0)
                     {
                         hashtable3.Add((string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DistinguishedName), (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsHostName));
                     }
                 }
                 foreach (SearchResult result2 in results)
                 {
                     string str3 = (string) PropertyManager.GetSearchResultPropertyValue(result2, PropertyManager.ObjectCategory);
                     if (Utils.Compare(str3, 0, "CN=Server".Length, "CN=Server", 0, "CN=Server".Length) != 0)
                     {
                         string distinguishedName = (string) PropertyManager.GetSearchResultPropertyValue(result2, PropertyManager.FromServer);
                         string partialDN = Utils.GetPartialDN(distinguishedName, 3);
                         pathname.Set(partialDN, 4);
                         partialDN = pathname.Retrieve(11).Substring(3);
                         string key = Utils.GetPartialDN((string) PropertyManager.GetSearchResultPropertyValue(result2, PropertyManager.DistinguishedName), 2);
                         if (!hashtable.Contains(key))
                         {
                             string str7 = (string) hashtable3[key];
                             if (!hashtable2.Contains(key))
                             {
                                 hashtable2.Add(key, str7);
                             }
                             if (Utils.Compare((string) PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.Cn), partialDN) != 0)
                             {
                                 hashtable.Add(key, str7);
                                 hashtable2.Remove(key);
                             }
                         }
                     }
                 }
             }
             finally
             {
                 results.Dispose();
             }
         }
         if (hashtable2.Count != 0)
         {
             DirectoryEntry searchRoot = DirectoryEntryManager.GetDirectoryEntry(this.context, this.siteDN);
             StringBuilder builder = new StringBuilder(100);
             if (hashtable2.Count > 1)
             {
                 builder.Append("(|");
             }
             foreach (DictionaryEntry entry3 in hashtable2)
             {
                 builder.Append("(fromServer=");
                 builder.Append("CN=NTDS Settings,");
                 builder.Append(Utils.GetEscapedFilterValue((string) entry3.Key));
                 builder.Append(")");
             }
             if (hashtable2.Count > 1)
             {
                 builder.Append(")");
             }
             ADSearcher searcher2 = new ADSearcher(searchRoot, "(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)" + builder.ToString() + ")", new string[] { "fromServer", "distinguishedName" }, SearchScope.Subtree);
             SearchResultCollection results2 = null;
             try
             {
                 results2 = searcher2.FindAll();
             }
             catch (COMException exception2)
             {
                 throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception2);
             }
             try
             {
                 foreach (SearchResult result3 in results2)
                 {
                     string str9 = ((string) PropertyManager.GetSearchResultPropertyValue(result3, PropertyManager.FromServer)).Substring(0x11);
                     if (hashtable2.Contains(str9))
                     {
                         string bstrADsPath = Utils.GetPartialDN((string) PropertyManager.GetSearchResultPropertyValue(result3, PropertyManager.DistinguishedName), 4);
                         pathname.Set(bstrADsPath, 4);
                         if (Utils.Compare(pathname.Retrieve(11).Substring(3), (string) PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.Cn)) != 0)
                         {
                             string str11 = (string) hashtable2[str9];
                             hashtable2.Remove(str9);
                             hashtable.Add(str9, str11);
                         }
                     }
                 }
             }
             finally
             {
                 results2.Dispose();
                 searchRoot.Dispose();
             }
         }
         DirectoryEntry directoryEntry = null;
         foreach (DictionaryEntry entry5 in hashtable)
         {
             DirectoryServer server = null;
             string domainControllerName = (string) entry5.Value;
             if (this.IsADAM)
             {
                 directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, "CN=NTDS Settings," + entry5.Key);
                 int num = (int) PropertyManager.GetPropertyValue(this.context, directoryEntry, PropertyManager.MsDSPortLDAP);
                 string adamInstanceName = domainControllerName;
                 if (num != 0x185)
                 {
                     adamInstanceName = domainControllerName + ":" + num;
                 }
                 server = new AdamInstance(Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, this.context), adamInstanceName);
             }
             else
             {
                 server = new DomainController(Utils.GetNewDirectoryContext(domainControllerName, DirectoryContextType.DirectoryServer, this.context), domainControllerName);
             }
             servers.Add(server);
         }
     }
     return servers;
 }
 public bool Contains(DomainController domainController)
 {
   Contract.Requires(domainController != null);
   return default(bool);
 }
Exemplo n.º 25
0
		internal static void ValidateCredential(DomainController dc, DirectoryContext context)
		{
			DirectoryEntry directoryEntry;
			if (!DirectoryContext.ServerBindSupported)
			{
				directoryEntry = new DirectoryEntry(string.Concat("LDAP://", dc.Name, "/RootDSE"), context.UserName, context.Password, Utils.DefaultAuthType);
			}
			else
			{
				directoryEntry = new DirectoryEntry(string.Concat("LDAP://", dc.Name, "/RootDSE"), context.UserName, context.Password, Utils.DefaultAuthType | AuthenticationTypes.ServerBind);
			}
			//TODO: REVIEW: URGENT!!: directoryEntry.Bind(true);
		}
 private void GetServers()
 {
     ADSearcher searcher = new ADSearcher(this.cachedEntry, "(&(objectClass=server)(objectCategory=server))", new string[] { "dNSHostName" }, SearchScope.Subtree);
     SearchResultCollection results = null;
     try
     {
         results = searcher.FindAll();
     }
     catch (COMException exception)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception);
     }
     try
     {
         foreach (SearchResult result in results)
         {
             string searchResultPropertyValue = (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsHostName);
             DirectoryEntry directoryEntry = result.GetDirectoryEntry();
             DirectoryEntry entry2 = null;
             DirectoryServer server = null;
             try
             {
                 entry2 = directoryEntry.Children.Find("CN=NTDS Settings", "nTDSDSA");
             }
             catch (COMException exception2)
             {
                 if (exception2.ErrorCode != -2147016656)
                 {
                     throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception2);
                 }
                 continue;
             }
             if (this.IsADAM)
             {
                 int num = (int) PropertyManager.GetPropertyValue(this.context, entry2, PropertyManager.MsDSPortLDAP);
                 string adamInstanceName = searchResultPropertyValue;
                 if (num != 0x185)
                 {
                     adamInstanceName = searchResultPropertyValue + ":" + num;
                 }
                 server = new AdamInstance(Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, this.context), adamInstanceName);
             }
             else
             {
                 server = new DomainController(Utils.GetNewDirectoryContext(searchResultPropertyValue, DirectoryContextType.DirectoryServer, this.context), searchResultPropertyValue);
             }
             this.servers.Add(server);
         }
     }
     finally
     {
         results.Dispose();
     }
 }
 public bool Contains(DomainController domainController)
 {
     Contract.Requires(domainController != null);
     return(default(bool));
 }
Exemplo n.º 28
0
        private ReadOnlyDirectoryServerCollection GetBridgeheadServers()
        {
            NativeComInterfaces.IAdsPathname pathCracker = (NativeComInterfaces.IAdsPathname)new NativeComInterfaces.Pathname();
            // need to turn off the escaping for name
            pathCracker.EscapedMode = NativeComInterfaces.ADS_ESCAPEDMODE_OFF_EX;

            ReadOnlyDirectoryServerCollection collection = new ReadOnlyDirectoryServerCollection();
            if (existing)
            {
                Hashtable bridgeHeadTable = new Hashtable();
                Hashtable nonBridgHeadTable = new Hashtable();
                Hashtable hostNameTable = new Hashtable();
                const string ocValue = "CN=Server";

                // get destination bridgehead servers

                // first go to the servers container under the current site and then do a search to get the all server objects.
                string serverContainer = "CN=Servers," + (string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.DistinguishedName);
                DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(context, serverContainer);

                try
                {
                    // go through connection objects and find out its fromServer property. 
                    ADSearcher adSearcher = new ADSearcher(de,
                                                          "(|(objectCategory=server)(objectCategory=NTDSConnection))",
                                                          new string[] { "fromServer", "distinguishedName", "dNSHostName", "objectCategory" },
                                                          SearchScope.Subtree,
                                                          true, /* need paged search */
                                                          true /* need cached result as we need to go back to the first record */);
                    SearchResultCollection conResults = null;
                    try
                    {
                        conResults = adSearcher.FindAll();
                    }
                    catch (COMException e)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                    }

                    try
                    {
                        // find out whether fromServer indicates replicating from a server in another site.
                        foreach (SearchResult r in conResults)
                        {
                            string objectCategoryValue = (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.ObjectCategory);
                            if (Utils.Compare(objectCategoryValue, 0, ocValue.Length, ocValue, 0, ocValue.Length) == 0)
                            {
                                hostNameTable.Add((string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.DistinguishedName), (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.DnsHostName));
                            }
                        }

                        foreach (SearchResult r in conResults)
                        {
                            string objectCategoryValue = (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.ObjectCategory);
                            if (Utils.Compare(objectCategoryValue, 0, ocValue.Length, ocValue, 0, ocValue.Length) != 0)
                            {
                                string fromServer = (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.FromServer);

                                // escaping manipulation                                
                                string fromSite = Utils.GetPartialDN(fromServer, 3);
                                pathCracker.Set(fromSite, NativeComInterfaces.ADS_SETTYPE_DN);
                                fromSite = pathCracker.Retrieve(NativeComInterfaces.ADS_FORMAT_LEAF);
                                Debug.Assert(fromSite != null && Utils.Compare(fromSite, 0, 3, "CN=", 0, 3) == 0);
                                fromSite = fromSite.Substring(3);

                                string serverObjectName = Utils.GetPartialDN((string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.DistinguishedName), 2);
                                // don't know whether it is a bridgehead server yet.
                                if (!bridgeHeadTable.Contains(serverObjectName))
                                {
                                    string hostName = (string)hostNameTable[serverObjectName];
                                    // add if not yet done
                                    if (!nonBridgHeadTable.Contains(serverObjectName))
                                        nonBridgHeadTable.Add(serverObjectName, hostName);

                                    // check whether from different site
                                    if (Utils.Compare((string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.Cn), fromSite) != 0)
                                    {
                                        // the server is a bridgehead server
                                        bridgeHeadTable.Add(serverObjectName, hostName);
                                        nonBridgHeadTable.Remove(serverObjectName);
                                    }
                                }
                            }
                        }
                    }
                    finally
                    {
                        conResults.Dispose();
                    }
                }
                finally
                {
                    de.Dispose();
                }

                // get source bridgehead server
                if (nonBridgHeadTable.Count != 0)
                {
                    // go to sites container to get all the connecdtion object that replicates from servers in the current sites that have
                    // not been determined whether it is a bridgehead server or not.
                    DirectoryEntry serverEntry = DirectoryEntryManager.GetDirectoryEntry(context, _siteDN);
                    // constructing the filter
                    StringBuilder str = new StringBuilder(100);
                    if (nonBridgHeadTable.Count > 1)
                        str.Append("(|");
                    foreach (DictionaryEntry val in nonBridgHeadTable)
                    {
                        str.Append("(fromServer=");
                        str.Append("CN=NTDS Settings,");
                        str.Append(Utils.GetEscapedFilterValue((string)val.Key));
                        str.Append(")");
                    }
                    if (nonBridgHeadTable.Count > 1)
                        str.Append(")");
                    ADSearcher adSearcher = new ADSearcher(serverEntry,
                                                          "(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)" + str.ToString() + ")",
                                                          new string[] { "fromServer", "distinguishedName" },
                                                          SearchScope.Subtree);
                    SearchResultCollection conResults = null;
                    try
                    {
                        conResults = adSearcher.FindAll();
                    }
                    catch (COMException e)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                    }

                    try
                    {
                        foreach (SearchResult r in conResults)
                        {
                            string fromServer = (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.FromServer);
                            string serverObject = fromServer.Substring(17);

                            if (nonBridgHeadTable.Contains(serverObject))
                            {
                                string otherSite = Utils.GetPartialDN((string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.DistinguishedName), 4);
                                // escaping manipulation
                                pathCracker.Set(otherSite, NativeComInterfaces.ADS_SETTYPE_DN);
                                otherSite = pathCracker.Retrieve(NativeComInterfaces.ADS_FORMAT_LEAF);
                                Debug.Assert(otherSite != null && Utils.Compare(otherSite, 0, 3, "CN=", 0, 3) == 0);
                                otherSite = otherSite.Substring(3);

                                // check whether from different sites
                                if (Utils.Compare(otherSite, (string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.Cn)) != 0)
                                {
                                    string val = (string)nonBridgHeadTable[serverObject];
                                    nonBridgHeadTable.Remove(serverObject);
                                    bridgeHeadTable.Add(serverObject, val);
                                }
                            }
                        }
                    }
                    finally
                    {
                        conResults.Dispose();
                        serverEntry.Dispose();
                    }
                }

                DirectoryEntry ADAMEntry = null;
                foreach (DictionaryEntry e in bridgeHeadTable)
                {
                    DirectoryServer replica = null;
                    string host = (string)e.Value;
                    // construct directoryreplica
                    if (IsADAM)
                    {
                        ADAMEntry = DirectoryEntryManager.GetDirectoryEntry(context, "CN=NTDS Settings," + e.Key);
                        int port = (int)PropertyManager.GetPropertyValue(context, ADAMEntry, PropertyManager.MsDSPortLDAP);
                        string fullhost = host;
                        if (port != 389)
                        {
                            fullhost = host + ":" + port;
                        }
                        replica = new AdamInstance(Utils.GetNewDirectoryContext(fullhost, DirectoryContextType.DirectoryServer, context), fullhost);
                    }
                    else
                    {
                        replica = new DomainController(Utils.GetNewDirectoryContext(host, DirectoryContextType.DirectoryServer, context), host);
                    }

                    collection.Add(replica);
                }
            }

            return collection;
        }
Exemplo n.º 29
0
        private void GetPreferredBridgeheadServers(ActiveDirectoryTransportType transport)
        {
            string serverContainerDN = "CN=Servers," + PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.DistinguishedName);
            string transportDN = null;
            if (transport == ActiveDirectoryTransportType.Smtp)
                transportDN = "CN=SMTP,CN=Inter-Site Transports," + _siteDN;
            else
                transportDN = "CN=IP,CN=Inter-Site Transports," + _siteDN;

            DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(context, serverContainerDN);
            ADSearcher adSearcher = new ADSearcher(de,
                                                  "(&(objectClass=server)(objectCategory=Server)(bridgeheadTransportList=" + Utils.GetEscapedFilterValue(transportDN) + "))",
                                                  new string[] { "dNSHostName", "distinguishedName" },
                                                  SearchScope.OneLevel);
            SearchResultCollection results = null;

            try
            {
                results = adSearcher.FindAll();
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }

            try
            {
                DirectoryEntry ADAMEntry = null;
                foreach (SearchResult result in results)
                {
                    string hostName = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsHostName);
                    DirectoryEntry resultEntry = result.GetDirectoryEntry();
                    DirectoryServer replica = null;

                    try
                    {
                        ADAMEntry = resultEntry.Children.Find("CN=NTDS Settings", "nTDSDSA");
                    }
                    catch (COMException e)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                    }

                    if (IsADAM)
                    {
                        int port = (int)PropertyManager.GetPropertyValue(context, ADAMEntry, PropertyManager.MsDSPortLDAP);
                        string fullHostName = hostName;
                        if (port != 389)
                        {
                            fullHostName = hostName + ":" + port;
                        }
                        replica = new AdamInstance(Utils.GetNewDirectoryContext(fullHostName, DirectoryContextType.DirectoryServer, context), fullHostName);
                    }
                    else
                        replica = new DomainController(Utils.GetNewDirectoryContext(hostName, DirectoryContextType.DirectoryServer, context), hostName);

                    if (transport == ActiveDirectoryTransportType.Smtp)
                        _SMTPBridgeheadServers.Add(replica);
                    else
                        _RPCBridgeheadServers.Add(replica);
                }
            }
            finally
            {
                de.Dispose();
                results.Dispose();
            }
        }
Exemplo n.º 30
0
        internal static GlobalCatalog FindOneWithCredentialValidation(DirectoryContext context, string siteName, LocatorOptions flag)
        {
            bool flag1 = false;
            bool flag2 = false;

            context = new DirectoryContext(context);
            GlobalCatalog globalCatalog = GlobalCatalog.FindOneInternal(context, context.Name, siteName, flag);

            using (globalCatalog)
            {
                if (flag2)
                {
                    try
                    {
                        DomainController.ValidateCredential(globalCatalog, context);
                    }
                    catch (COMException cOMException1)
                    {
                        COMException cOMException = cOMException1;
                        if (cOMException.ErrorCode != -2147016646)
                        {
                            throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
                        }
                        else
                        {
                            if ((flag & LocatorOptions.ForceRediscovery) != 0)
                            {
                                object[] name = new object[1];
                                name[0] = context.Name;
                                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("GCNotFoundInForest", name), typeof(GlobalCatalog), null);
                            }
                            else
                            {
                                flag1 = true;
                            }
                        }
                    }
                }
            }
            if (flag1)
            {
                flag2         = false;
                globalCatalog = GlobalCatalog.FindOneInternal(context, context.Name, siteName, flag | LocatorOptions.ForceRediscovery);
                using (globalCatalog)
                {
                    if (flag2)
                    {
                        try
                        {
                            DomainController.ValidateCredential(globalCatalog, context);
                        }
                        catch (COMException cOMException3)
                        {
                            COMException cOMException2 = cOMException3;
                            if (cOMException2.ErrorCode != -2147016646)
                            {
                                throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException2);
                            }
                            else
                            {
                                object[] objArray = new object[1];
                                objArray[0] = context.Name;
                                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("GCNotFoundInForest", objArray), typeof(GlobalCatalog), null);
                            }
                        }
                    }
                }
            }
            return(globalCatalog);
        }
Exemplo n.º 31
0
 public bool Contains(DomainController domainController)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 32
0
        internal static void ValidateCredential(DomainController dc, DirectoryContext context)
        {
            DirectoryEntry de;

            if (DirectoryContext.ServerBindSupported)
            {
                de = new DirectoryEntry("LDAP://" + dc.Name + "/RootDSE", context.UserName, context.Password, Utils.DefaultAuthType | AuthenticationTypes.ServerBind);
            }
            else
            {
                de = new DirectoryEntry("LDAP://" + dc.Name + "/RootDSE", context.UserName, context.Password, Utils.DefaultAuthType);
            }

            de.Bind(true);
        }
Exemplo n.º 33
0
		private DirectoryServer GetSchemaRoleOwner()
		{
			DirectoryServer adamInstance;
			DirectoryServer directoryServer;
			try
			{
				this.schemaEntry.RefreshCache();
				if (!this.context.isADAMConfigSet())
				{
					DirectoryEntry cachedDirectoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
					if (!Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectory))
					{
						string adamDnsHostNameFromNTDSA = Utils.GetAdamDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
						DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(adamDnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, this.context);
						adamInstance = new AdamInstance(newDirectoryContext, adamDnsHostNameFromNTDSA);
					}
					else
					{
						string dnsHostNameFromNTDSA = Utils.GetDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
						DirectoryContext directoryContext = Utils.GetNewDirectoryContext(dnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, this.context);
						adamInstance = new DomainController(directoryContext, dnsHostNameFromNTDSA);
					}
					directoryServer = adamInstance;
				}
				else
				{
					string str = Utils.GetAdamDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
					DirectoryContext newDirectoryContext1 = Utils.GetNewDirectoryContext(str, DirectoryContextType.DirectoryServer, this.context);
					directoryServer = new AdamInstance(newDirectoryContext1, str);
				}
			}
			catch (COMException cOMException1)
			{
				COMException cOMException = cOMException1;
				throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException);
			}
			return directoryServer;
		}
Exemplo n.º 34
0
		private void GetServers()
		{
			DirectoryServer domainController;
			string[] strArrays = new string[1];
			strArrays[0] = "dNSHostName";
			ADSearcher aDSearcher = new ADSearcher(this.cachedEntry, "(&(objectClass=server)(objectCategory=server))", strArrays, SearchScope.Subtree);
			SearchResultCollection searchResultCollections = null;
			try
			{
				searchResultCollections = aDSearcher.FindAll();
			}
			catch (COMException cOMException1)
			{
				COMException cOMException = cOMException1;
				throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException);
			}
			try
			{
				foreach (SearchResult searchResult in searchResultCollections)
				{
					string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DnsHostName);
					DirectoryEntry directoryEntry = searchResult.GetDirectoryEntry();
					DirectoryEntry directoryEntry1 = null;
					try
					{
						directoryEntry1 = directoryEntry.Children.Find("CN=NTDS Settings", "nTDSDSA");
					}
					catch (COMException cOMException3)
					{
						COMException cOMException2 = cOMException3;
						if (cOMException2.ErrorCode != -2147016656)
						{
							throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException2);
						}
						else
						{
							continue;
						}
					}
					if (!this.IsADAM)
					{
						domainController = new DomainController(Utils.GetNewDirectoryContext(searchResultPropertyValue, DirectoryContextType.DirectoryServer, this.context), searchResultPropertyValue);
					}
					else
					{
						int propertyValue = (int)PropertyManager.GetPropertyValue(this.context, directoryEntry1, PropertyManager.MsDSPortLDAP);
						string str = searchResultPropertyValue;
						if (propertyValue != 0x185)
						{
							str = string.Concat(searchResultPropertyValue, ":", propertyValue);
						}
						domainController = new AdamInstance(Utils.GetNewDirectoryContext(str, DirectoryContextType.DirectoryServer, this.context), str);
					}
					this.servers.Add(domainController);
				}
			}
			finally
			{
				searchResultCollections.Dispose();
			}
		}
Exemplo n.º 35
0
        internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle)
        {
            Guid             ntdsaObjectGuid;
            int              num              = Marshal.SizeOf(typeof(Guid));
            AdamInstance     adamInstance     = null;
            DomainController domainController = null;
            IntPtr           intPtr           = Marshal.AllocHGlobal(num);

            try
            {
                if (sourceServer != null)
                {
                    DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, this.context);
                    if (!isADAM)
                    {
                        domainController = DomainController.GetDomainController(newDirectoryContext);
                        ntdsaObjectGuid  = domainController.NtdsaObjectGuid;
                    }
                    else
                    {
                        adamInstance    = AdamInstance.GetAdamInstance(newDirectoryContext);
                        ntdsaObjectGuid = adamInstance.NtdsaObjectGuid;
                    }
                    Marshal.StructureToPtr(ntdsaObjectGuid, intPtr, false);
                }
                IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW");
                if (procAddress != (IntPtr)0)
                {
                    UnsafeNativeMethods.DsReplicaSyncW delegateForFunctionPointer = (UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsReplicaSyncW));
                    int num1 = delegateForFunctionPointer(dsHandle, partition, intPtr, option);
                    if (num1 != 0)
                    {
                        if (this.Partitions.Contains(partition))
                        {
                            string name = null;
                            if (num1 != ExceptionHelper.RPC_S_SERVER_UNAVAILABLE)
                            {
                                if (num1 == ExceptionHelper.RPC_S_CALL_FAILED)
                                {
                                    name = this.Name;
                                }
                            }
                            else
                            {
                                name = sourceServer;
                            }
                            throw ExceptionHelper.GetExceptionFromErrorCode(num1, name);
                        }
                        else
                        {
                            throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition");
                        }
                    }
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                }
            }
            finally
            {
                if (intPtr != (IntPtr)0)
                {
                    Marshal.FreeHGlobal(intPtr);
                }
                if (adamInstance != null)
                {
                    adamInstance.Dispose();
                }
                if (domainController != null)
                {
                    domainController.Dispose();
                }
            }
        }
Exemplo n.º 36
0
		public bool Contains (DomainController domainController)
		{
			throw new NotImplementedException ();
		}
Exemplo n.º 37
0
        private DirectoryServer GetSchemaRoleOwner()
        {
            try
            {
                _schemaEntry.RefreshCache();

                if (context.isADAMConfigSet())
                {
                    // ADAM
                    string adamInstName = Utils.GetAdamDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, _schemaEntry, PropertyManager.FsmoRoleOwner));
                    DirectoryContext adamInstContext = Utils.GetNewDirectoryContext(adamInstName, DirectoryContextType.DirectoryServer, context);
                    return new AdamInstance(adamInstContext, adamInstName);
                }
                else
                {
                    // could be AD or adam server

                    DirectoryServer server = null;
                    DirectoryEntry rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);

                    if (Utils.CheckCapability(rootDSE, Capability.ActiveDirectory))
                    {
                        string dcName = Utils.GetDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, _schemaEntry, PropertyManager.FsmoRoleOwner));
                        DirectoryContext dcContext = Utils.GetNewDirectoryContext(dcName, DirectoryContextType.DirectoryServer, context);
                        server = new DomainController(dcContext, dcName);
                    }
                    else
                    {
                        // ADAM case again
                        string adamInstName = Utils.GetAdamDnsHostNameFromNTDSA(context, (string)PropertyManager.GetPropertyValue(context, _schemaEntry, PropertyManager.FsmoRoleOwner));
                        DirectoryContext adamInstContext = Utils.GetNewDirectoryContext(adamInstName, DirectoryContextType.DirectoryServer, context);
                        server = new AdamInstance(adamInstContext, adamInstName);
                    }
                    return server;
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
        }
Exemplo n.º 38
0
		public void CopyTo (DomainController[] domainControllers, int index)
		{
			throw new NotImplementedException ();
		}
Exemplo n.º 39
0
        private DirectoryServer FindDirectoryServerInternal(string siteName, bool forceRediscovery)
        {
            DirectoryServer directoryServer = null;
            LocatorOptions flag = 0;
            int errorCode = 0;
            DomainControllerInfo domainControllerInfo;

            if (siteName != null && siteName.Length == 0)
            {
                throw new ArgumentException(Res.GetString(Res.EmptyStringParameter), "siteName");
            }

            // Check that the application partition has been committed
            if (!_committed)
            {
                throw new InvalidOperationException(Res.GetString(Res.CannotPerformOperationOnUncommittedObject));
            }

            // set the force rediscovery flag if required
            if (forceRediscovery)
            {
                flag = LocatorOptions.ForceRediscovery;
            }

            // call DsGetDcName
            errorCode = Locator.DsGetDcNameWrapper(null, _dnsName, siteName, (long)flag | (long)PrivateLocatorFlags.OnlyLDAPNeeded, out domainControllerInfo);

            if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.ReplicaNotFound), typeof(DirectoryServer), null);
            }
            else if (errorCode != 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
            }

            Debug.Assert(domainControllerInfo.DomainControllerName.Length > 2, "ApplicationPartition:FindDirectoryServerInternal - domainControllerInfo.DomainControllerName.Length <= 2");
            string dcName = domainControllerInfo.DomainControllerName.Substring(2);

            // create a new context object for the domain controller passing on only the 
            // credentials from the forest context
            DirectoryContext dcContext = Utils.GetNewDirectoryContext(dcName, DirectoryContextType.DirectoryServer, context);

            directoryServer = new DomainController(dcContext, dcName);
            return directoryServer;
        }
Exemplo n.º 40
0
        internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle)
        {
            int              structSize    = Marshal.SizeOf(typeof(Guid));
            IntPtr           unmanagedGuid = (IntPtr)0;
            Guid             guid          = Guid.Empty;
            AdamInstance     adamServer    = null;
            DomainController dcServer      = null;

            unmanagedGuid = Marshal.AllocHGlobal(structSize);
            try
            {
                if (sourceServer != null)
                {
                    DirectoryContext newContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, context);
                    if (isADAM)
                    {
                        adamServer = AdamInstance.GetAdamInstance(newContext);
                        guid       = adamServer.NtdsaObjectGuid;
                    }
                    else
                    {
                        dcServer = DomainController.GetDomainController(newContext);
                        guid     = dcServer.NtdsaObjectGuid;
                    }

                    Marshal.StructureToPtr(guid, unmanagedGuid, false);
                }

                // call DsReplicaSyncW
                IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW");
                if (functionPtr == (IntPtr)0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                }
                UnsafeNativeMethods.DsReplicaSyncW dsReplicaSyncW = (UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaSyncW));

                int result = dsReplicaSyncW(dsHandle, partition, unmanagedGuid, (int)option);

                // check the result
                if (result != 0)
                {
                    if (!Partitions.Contains(partition))
                    {
                        throw new ArgumentException(SR.ServerNotAReplica, "partition");
                    }

                    string serverDownName = null;
                    // this is the error returned when the server that we want to sync from is down
                    if (result == ExceptionHelper.RPC_S_SERVER_UNAVAILABLE)
                    {
                        serverDownName = sourceServer;
                    }
                    // this is the error returned when the server that we want to get synced is down
                    else if (result == ExceptionHelper.RPC_S_CALL_FAILED)
                    {
                        serverDownName = Name;
                    }

                    throw ExceptionHelper.GetExceptionFromErrorCode(result, serverDownName);
                }
            }
            finally
            {
                if (unmanagedGuid != (IntPtr)0)
                {
                    Marshal.FreeHGlobal(unmanagedGuid);
                }

                if (adamServer != null)
                {
                    adamServer.Dispose();
                }

                if (dcServer != null)
                {
                    dcServer.Dispose();
                }
            }
        }
        internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle)
        {
            int              cb               = Marshal.SizeOf(typeof(Guid));
            IntPtr           zero             = IntPtr.Zero;
            Guid             empty            = Guid.Empty;
            AdamInstance     adamInstance     = null;
            DomainController domainController = null;

            zero = Marshal.AllocHGlobal(cb);
            try
            {
                if (sourceServer != null)
                {
                    DirectoryContext context = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, this.context);
                    if (isADAM)
                    {
                        adamInstance = AdamInstance.GetAdamInstance(context);
                        empty        = adamInstance.NtdsaObjectGuid;
                    }
                    else
                    {
                        domainController = DomainController.GetDomainController(context);
                        empty            = domainController.NtdsaObjectGuid;
                    }
                    Marshal.StructureToPtr(empty, zero, false);
                }
                IntPtr procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW");
                if (procAddress == IntPtr.Zero)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                }
                System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaSyncW delegateForFunctionPointer = (System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaSyncW));
                int errorCode = delegateForFunctionPointer(dsHandle, partition, zero, option);
                if (errorCode != 0)
                {
                    if (!this.Partitions.Contains(partition))
                    {
                        throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition");
                    }
                    string targetName = null;
                    if (errorCode == ExceptionHelper.RPC_S_SERVER_UNAVAILABLE)
                    {
                        targetName = sourceServer;
                    }
                    else if (errorCode == ExceptionHelper.RPC_S_CALL_FAILED)
                    {
                        targetName = this.Name;
                    }
                    throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, targetName);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
                if (adamInstance != null)
                {
                    adamInstance.Dispose();
                }
                if (domainController != null)
                {
                    domainController.Dispose();
                }
            }
        }
 public void CopyTo(DomainController[] domainControllers, int index)
 {
   Contract.Requires(domainControllers != null);
 }
Exemplo n.º 43
0
 public int IndexOf(DomainController domainController)
 {
     throw new NotImplementedException();
 }
 private void GetPreferredBridgeheadServers(ActiveDirectoryTransportType transport)
 {
     string dn = "CN=Servers," + PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.DistinguishedName);
     string filterValue = null;
     if (transport == ActiveDirectoryTransportType.Smtp)
     {
         filterValue = "CN=SMTP,CN=Inter-Site Transports," + this.siteDN;
     }
     else
     {
         filterValue = "CN=IP,CN=Inter-Site Transports," + this.siteDN;
     }
     DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, dn);
     ADSearcher searcher = new ADSearcher(directoryEntry, "(&(objectClass=server)(objectCategory=Server)(bridgeheadTransportList=" + Utils.GetEscapedFilterValue(filterValue) + "))", new string[] { "dNSHostName", "distinguishedName" }, SearchScope.OneLevel);
     SearchResultCollection results = null;
     try
     {
         results = searcher.FindAll();
     }
     catch (COMException exception)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception);
     }
     try
     {
         DirectoryEntry entry2 = null;
         foreach (SearchResult result in results)
         {
             string searchResultPropertyValue = (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsHostName);
             DirectoryEntry entry3 = result.GetDirectoryEntry();
             DirectoryServer server = null;
             try
             {
                 entry2 = entry3.Children.Find("CN=NTDS Settings", "nTDSDSA");
             }
             catch (COMException exception2)
             {
                 throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception2);
             }
             if (this.IsADAM)
             {
                 int num = (int) PropertyManager.GetPropertyValue(this.context, entry2, PropertyManager.MsDSPortLDAP);
                 string adamInstanceName = searchResultPropertyValue;
                 if (num != 0x185)
                 {
                     adamInstanceName = searchResultPropertyValue + ":" + num;
                 }
                 server = new AdamInstance(Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, this.context), adamInstanceName);
             }
             else
             {
                 server = new DomainController(Utils.GetNewDirectoryContext(searchResultPropertyValue, DirectoryContextType.DirectoryServer, this.context), searchResultPropertyValue);
             }
             if (transport == ActiveDirectoryTransportType.Smtp)
             {
                 this.SMTPBridgeheadServers.Add(server);
             }
             else
             {
                 this.RPCBridgeheadServers.Add(server);
             }
         }
     }
     finally
     {
         directoryEntry.Dispose();
         results.Dispose();
     }
 }
Exemplo n.º 45
0
        internal unsafe void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string?sourceServer, int option, SafeLibraryHandle libHandle)
        {
            int              structSize    = Marshal.SizeOf(typeof(Guid));
            IntPtr           unmanagedGuid = (IntPtr)0;
            Guid             guid          = Guid.Empty;
            AdamInstance?    adamServer    = null;
            DomainController?dcServer      = null;

            unmanagedGuid = Marshal.AllocHGlobal(structSize);
            try
            {
                if (sourceServer != null)
                {
                    DirectoryContext newContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, context);
                    if (isADAM)
                    {
                        adamServer = AdamInstance.GetAdamInstance(newContext);
                        guid       = adamServer.NtdsaObjectGuid;
                    }
                    else
                    {
                        dcServer = DomainController.GetDomainController(newContext);
                        guid     = dcServer.NtdsaObjectGuid;
                    }

                    Marshal.StructureToPtr(guid, unmanagedGuid, false);
                }

                // call DsReplicaSyncW
                var dsReplicaSyncW = (delegate * unmanaged <IntPtr, char *, IntPtr, int, int>)global::Interop.Kernel32.GetProcAddress(libHandle, "DsReplicaSyncW");
                if (dsReplicaSyncW == null)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastPInvokeError());
                }

                fixed(char *partitionPtr = partition)
                {
                    int result = dsReplicaSyncW(dsHandle, partitionPtr, unmanagedGuid, (int)option);

                    // check the result
                    if (result != 0)
                    {
                        if (!Partitions.Contains(partition))
                        {
                            throw new ArgumentException(SR.ServerNotAReplica, nameof(partition));
                        }

                        string?serverDownName = null;
                        // this is the error returned when the server that we want to sync from is down
                        if (result == ExceptionHelper.RPC_S_SERVER_UNAVAILABLE)
                        {
                            serverDownName = sourceServer;
                        }
                        // this is the error returned when the server that we want to get synced is down
                        else if (result == ExceptionHelper.RPC_S_CALL_FAILED)
                        {
                            serverDownName = Name;
                        }

                        throw ExceptionHelper.GetExceptionFromErrorCode(result, serverDownName);
                    }
                }
            }
            finally
            {
                if (unmanagedGuid != (IntPtr)0)
                {
                    Marshal.FreeHGlobal(unmanagedGuid);
                }

                adamServer?.Dispose();
                dcServer?.Dispose();
            }
        }