Esempio n. 1
1
		internal ReplicationConnectionCollection GetOutboundConnectionsHelper()
		{
			string siteObjectName;
			string serverObjectName;
			if (this.outbound == null)
			{
				if (this as DomainController != null)
				{
					siteObjectName = ((DomainController)this).SiteObjectName;
				}
				else
				{
					siteObjectName = ((AdamInstance)this).SiteObjectName;
				}
				string str = siteObjectName;
				DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(Utils.GetNewDirectoryContext(this.Name, DirectoryContextType.DirectoryServer, this.context), str);
				if (this as DomainController != null)
				{
					serverObjectName = ((DomainController)this).ServerObjectName;
				}
				else
				{
					serverObjectName = ((AdamInstance)this).ServerObjectName;
				}
				string str1 = serverObjectName;
				string[] strArrays = new string[2];
				strArrays[0] = "objectClass";
				strArrays[1] = "cn";
				ADSearcher aDSearcher = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=nTDSConnection)(objectCategory=nTDSConnection)(fromServer=CN=NTDS Settings,", str1, "))"), strArrays, SearchScope.Subtree);
				SearchResultCollection searchResultCollections = null;
				DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(this.Name, DirectoryContextType.DirectoryServer, this.context);
				try
				{
					try
					{
						searchResultCollections = aDSearcher.FindAll();
						this.outbound = new ReplicationConnectionCollection();
						foreach (SearchResult searchResult in searchResultCollections)
						{
							ReplicationConnection replicationConnection = new ReplicationConnection(newDirectoryContext, searchResult.GetDirectoryEntry(), (string)searchResult.Properties["cn"][0]);
							this.outbound.Add(replicationConnection);
						}
					}
					catch (COMException cOMException1)
					{
						COMException cOMException = cOMException1;
						throw ExceptionHelper.GetExceptionFromCOMException(newDirectoryContext, cOMException);
					}
				}
				finally
				{
					if (searchResultCollections != null)
					{
						searchResultCollections.Dispose();
					}
					directoryEntry.Dispose();
				}
			}
			return this.outbound;
		}
Esempio n. 2
0
        public static ReplicationConnection FindByName(DirectoryContext context, string name)
        {
            ValidateArgument(context, name);

            //  work with copy of the context
            context = new DirectoryContext(context);

            // bind to the rootdse to get the servername property
            DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);

            try
            {
                string serverDN            = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ServerName);
                string connectionContainer = "CN=NTDS Settings," + serverDN;
                de = DirectoryEntryManager.GetDirectoryEntry(context, connectionContainer);
                // doing the search to find the connection object based on its name
                ADSearcher adSearcher = new ADSearcher(de,
                                                       "(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)(name=" + Utils.GetEscapedFilterValue(name) + "))",
                                                       new string[] { "distinguishedName" },
                                                       SearchScope.OneLevel,
                                                       false, /* no paged search */
                                                       false /* don't cache results */);
                SearchResult srchResult = null;
                try
                {
                    srchResult = adSearcher.FindOne();
                }
                catch (COMException e)
                {
                    if (e.ErrorCode == unchecked ((int)0x80072030))
                    {
                        // object is not found since we cannot even find the container in which to search
                        throw new ActiveDirectoryObjectNotFoundException(SR.DSNotFound, typeof(ReplicationConnection), name);
                    }
                    else
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                    }
                }

                if (srchResult == null)
                {
                    // no such connection object
                    Exception e = new ActiveDirectoryObjectNotFoundException(SR.DSNotFound, typeof(ReplicationConnection), name);
                    throw e;
                }
                else
                {
                    DirectoryEntry connectionEntry = srchResult.GetDirectoryEntry();
                    return(new ReplicationConnection(context, connectionEntry, name));
                }
            }
            finally
            {
                de.Dispose();
            }
        }
Esempio n. 3
0
        internal static ReadOnlyActiveDirectorySchemaPropertyCollection GetAllProperties(DirectoryContext context, DirectoryEntry schemaEntry, string filter)
        {
            ArrayList propertyList = new ArrayList();

            string[] propertiesToLoad = new string[3];
            propertiesToLoad[0] = PropertyManager.LdapDisplayName;
            propertiesToLoad[1] = PropertyManager.Cn;
            propertiesToLoad[2] = PropertyManager.IsDefunct;

            ADSearcher             searcher = new ADSearcher(schemaEntry, filter, propertiesToLoad, SearchScope.OneLevel);
            SearchResultCollection resCol   = null;

            try
            {
                resCol = searcher.FindAll();
                foreach (SearchResult res in resCol)
                {
                    string         ldapDisplayName = (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.LdapDisplayName);
                    DirectoryEntry directoryEntry  = res.GetDirectoryEntry();

                    directoryEntry.AuthenticationType = Utils.DefaultAuthType;
                    directoryEntry.Username           = context.UserName;
                    directoryEntry.Password           = context.Password;

                    bool isDefunct = false;

                    if ((res.Properties[PropertyManager.IsDefunct] != null) && (res.Properties[PropertyManager.IsDefunct].Count > 0))
                    {
                        isDefunct = (bool)res.Properties[PropertyManager.IsDefunct][0];
                    }

                    if (isDefunct)
                    {
                        string commonName = (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.Cn);
                        propertyList.Add(new ActiveDirectorySchemaProperty(context, commonName, ldapDisplayName, directoryEntry, schemaEntry));
                    }
                    else
                    {
                        propertyList.Add(new ActiveDirectorySchemaProperty(context, ldapDisplayName, directoryEntry, schemaEntry));
                    }
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                // dispose off the result collection
                if (resCol != null)
                {
                    resCol.Dispose();
                }
            }

            return(new ReadOnlyActiveDirectorySchemaPropertyCollection(propertyList));
        }
        private ArrayList GetApplicationPartitions()
        {
            ArrayList      list = new ArrayList();
            DirectoryEntry cachedDirectoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
            DirectoryEntry searchRoot           = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.PartitionsContainer);
            StringBuilder  builder = new StringBuilder(100);

            builder.Append("(&(");
            builder.Append(PropertyManager.ObjectCategory);
            builder.Append("=crossRef)(");
            builder.Append(PropertyManager.SystemFlags);
            builder.Append(":1.2.840.113556.1.4.804:=");
            builder.Append(1);
            builder.Append(")(!(");
            builder.Append(PropertyManager.SystemFlags);
            builder.Append(":1.2.840.113556.1.4.803:=");
            builder.Append(2);
            builder.Append(")))");
            string filter = builder.ToString();

            string[]               propertiesToLoad = new string[] { PropertyManager.NCName, PropertyManager.MsDSNCReplicaLocations };
            ADSearcher             searcher         = new ADSearcher(searchRoot, filter, propertiesToLoad, SearchScope.OneLevel);
            SearchResultCollection results          = null;

            try
            {
                results = searcher.FindAll();
                string str2 = (string)PropertyManager.GetPropertyValue(this.context, cachedDirectoryEntry, PropertyManager.SchemaNamingContext);
                string str3 = (string)PropertyManager.GetPropertyValue(this.context, cachedDirectoryEntry, PropertyManager.ConfigurationNamingContext);
                foreach (SearchResult result in results)
                {
                    string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.NCName);
                    if (!searchResultPropertyValue.Equals(str2) && !searchResultPropertyValue.Equals(str3))
                    {
                        ResultPropertyValueCollection values = result.Properties[PropertyManager.MsDSNCReplicaLocations];
                        if (values.Count > 0)
                        {
                            DirectoryContext context = Utils.GetNewDirectoryContext(Utils.GetAdamDnsHostNameFromNTDSA(this.context, (string)values[Utils.GetRandomIndex(values.Count)]), DirectoryContextType.DirectoryServer, this.context);
                            list.Add(new ApplicationPartition(context, searchResultPropertyValue, null, ApplicationPartitionType.ADAMApplicationPartition, new DirectoryEntryManager(context)));
                        }
                    }
                }
                return(list);
            }
            catch (COMException exception)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception);
            }
            finally
            {
                if (results != null)
                {
                    results.Dispose();
                }
            }
            return(list);
        }
Esempio n. 5
0
        public static ReplicationConnection FindByName(DirectoryContext context, string name)
        {
            ValidateArgument(context, name);

            //  work with copy of the context
            context = new DirectoryContext(context);

            // bind to the rootdse to get the servername property
            DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
            try
            {
                string serverDN = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ServerName);
                string connectionContainer = "CN=NTDS Settings," + serverDN;
                de = DirectoryEntryManager.GetDirectoryEntry(context, connectionContainer);
                // doing the search to find the connection object based on its name
                ADSearcher adSearcher = new ADSearcher(de,
                                                      "(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)(name=" + Utils.GetEscapedFilterValue(name) + "))",
                                                      new string[] { "distinguishedName" },
                                                      SearchScope.OneLevel,
                                                      false, /* no paged search */
                                                      false /* don't cache results */);
                SearchResult srchResult = null;
                try
                {
                    srchResult = adSearcher.FindOne();
                }
                catch (COMException e)
                {
                    if (e.ErrorCode == unchecked((int)0x80072030))
                    {
                        // object is not found since we cannot even find the container in which to search
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DSNotFound), typeof(ReplicationConnection), name);
                    }
                    else
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                    }
                }

                if (srchResult == null)
                {
                    // no such connection object
                    Exception e = new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DSNotFound), typeof(ReplicationConnection), name);
                    throw e;
                }
                else
                {
                    DirectoryEntry connectionEntry = srchResult.GetDirectoryEntry();
                    return new ReplicationConnection(context, connectionEntry, name);
                }
            }
            finally
            {
                de.Dispose();
            }
        }
        private ArrayList GetApplicationPartitions()
        {
            ArrayList      list           = new ArrayList();
            DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer));
            StringBuilder  builder        = new StringBuilder(15);

            builder.Append("(&(");
            builder.Append(PropertyManager.ObjectCategory);
            builder.Append("=crossRef)(");
            builder.Append(PropertyManager.SystemFlags);
            builder.Append(":1.2.840.113556.1.4.804:=");
            builder.Append(1);
            builder.Append(")(!(");
            builder.Append(PropertyManager.SystemFlags);
            builder.Append(":1.2.840.113556.1.4.803:=");
            builder.Append(2);
            builder.Append(")))");
            string filter = builder.ToString();

            string[]               propertiesToLoad = new string[] { PropertyManager.DnsRoot, PropertyManager.NCName };
            ADSearcher             searcher         = new ADSearcher(directoryEntry, filter, propertiesToLoad, SearchScope.OneLevel);
            SearchResultCollection results          = null;

            try
            {
                results = searcher.FindAll();
                string str2 = this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.SchemaNamingContext);
                string str3 = this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.ConfigurationNamingContext);
                foreach (SearchResult result in results)
                {
                    string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.NCName);
                    if (!searchResultPropertyValue.Equals(str2) && !searchResultPropertyValue.Equals(str3))
                    {
                        string           name    = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsRoot);
                        DirectoryContext context = Utils.GetNewDirectoryContext(name, DirectoryContextType.ApplicationPartition, this.context);
                        list.Add(new ApplicationPartition(context, searchResultPropertyValue, (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsRoot), ApplicationPartitionType.ADApplicationPartition, new DirectoryEntryManager(context)));
                    }
                }
                return(list);
            }
            catch (COMException exception)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception);
            }
            finally
            {
                if (results != null)
                {
                    results.Dispose();
                }
                directoryEntry.Dispose();
            }
            return(list);
        }
Esempio n. 7
0
        internal ReplicationConnectionCollection GetInboundConnectionsHelper()
        {
            string serverObjectName;

            if (this.inbound == null)
            {
                this.inbound = new ReplicationConnectionCollection();
                DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(this.Name, DirectoryContextType.DirectoryServer, this.context);
                if (this as DomainController != null)
                {
                    serverObjectName = ((DomainController)this).ServerObjectName;
                }
                else
                {
                    serverObjectName = ((AdamInstance)this).ServerObjectName;
                }
                string         str            = serverObjectName;
                string         str1           = string.Concat("CN=NTDS Settings,", str);
                DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(Utils.GetNewDirectoryContext(this.Name, DirectoryContextType.DirectoryServer, this.context), str1);
                string[]       strArrays      = new string[1];
                strArrays[0] = "cn";
                ADSearcher             aDSearcher = new ADSearcher(directoryEntry, "(&(objectClass=nTDSConnection)(objectCategory=nTDSConnection))", strArrays, SearchScope.OneLevel);
                SearchResultCollection searchResultCollections = null;
                try
                {
                    try
                    {
                        searchResultCollections = aDSearcher.FindAll();
                        foreach (SearchResult searchResult in searchResultCollections)
                        {
                            ReplicationConnection replicationConnection = new ReplicationConnection(newDirectoryContext, searchResult.GetDirectoryEntry(), (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.Cn));
                            this.inbound.Add(replicationConnection);
                        }
                    }
                    catch (COMException cOMException1)
                    {
                        COMException cOMException = cOMException1;
                        throw ExceptionHelper.GetExceptionFromCOMException(newDirectoryContext, cOMException);
                    }
                }
                finally
                {
                    if (searchResultCollections != null)
                    {
                        searchResultCollections.Dispose();
                    }
                    directoryEntry.Dispose();
                }
            }
            return(this.inbound);
        }
        public static ReplicationConnection FindByName(DirectoryContext context, string name)
        {
            ReplicationConnection replicationConnection;

            ReplicationConnection.ValidateArgument(context, name);
            context = new DirectoryContext(context);
            DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);

            try
            {
                string propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ServerName);
                string str           = string.Concat("CN=NTDS Settings,", propertyValue);
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, str);
                string[] strArrays = new string[1];
                strArrays[0] = "distinguishedName";
                ADSearcher   aDSearcher   = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)(name=", Utils.GetEscapedFilterValue(name), "))"), strArrays, SearchScope.OneLevel, false, false);
                SearchResult searchResult = null;
                try
                {
                    searchResult = aDSearcher.FindOne();
                }
                catch (COMException cOMException1)
                {
                    COMException cOMException = cOMException1;
                    if (cOMException.ErrorCode != -2147016656)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ReplicationConnection), name);
                    }
                }
                if (searchResult != null)
                {
                    DirectoryEntry directoryEntry1 = searchResult.GetDirectoryEntry();
                    replicationConnection = new ReplicationConnection(context, directoryEntry1, name);
                }
                else
                {
                    Exception activeDirectoryObjectNotFoundException = new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ReplicationConnection), name);
                    throw activeDirectoryObjectNotFoundException;
                }
            }
            finally
            {
                directoryEntry.Dispose();
            }
            return(replicationConnection);
        }
        internal static ReadOnlyActiveDirectorySchemaPropertyCollection GetAllProperties(DirectoryContext context, DirectoryEntry schemaEntry, string filter)
        {
            ArrayList values = new ArrayList();

            string[]               propertiesToLoad = new string[] { PropertyManager.LdapDisplayName, PropertyManager.Cn, PropertyManager.IsDefunct };
            ADSearcher             searcher         = new ADSearcher(schemaEntry, filter, propertiesToLoad, SearchScope.OneLevel);
            SearchResultCollection results          = null;

            try
            {
                results = searcher.FindAll();
                foreach (SearchResult result in results)
                {
                    string         searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.LdapDisplayName);
                    DirectoryEntry directoryEntry            = result.GetDirectoryEntry();
                    directoryEntry.AuthenticationType = Utils.DefaultAuthType;
                    directoryEntry.Username           = context.UserName;
                    directoryEntry.Password           = context.Password;
                    bool flag = false;
                    if ((result.Properties[PropertyManager.IsDefunct] != null) && (result.Properties[PropertyManager.IsDefunct].Count > 0))
                    {
                        flag = (bool)result.Properties[PropertyManager.IsDefunct][0];
                    }
                    if (flag)
                    {
                        string commonName = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.Cn);
                        values.Add(new ActiveDirectorySchemaProperty(context, commonName, searchResultPropertyValue, directoryEntry, schemaEntry));
                    }
                    else
                    {
                        values.Add(new ActiveDirectorySchemaProperty(context, searchResultPropertyValue, directoryEntry, schemaEntry));
                    }
                }
            }
            catch (COMException exception)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
            }
            finally
            {
                if (results != null)
                {
                    results.Dispose();
                }
            }
            return(new ReadOnlyActiveDirectorySchemaPropertyCollection(values));
        }
Esempio n. 10
0
        internal static ReadOnlyActiveDirectorySchemaPropertyCollection GetAllProperties(DirectoryContext context, DirectoryEntry schemaEntry, string filter)
        {
            ArrayList arrayLists = new ArrayList();

            string[] ldapDisplayName = new string[3];
            ldapDisplayName[0] = PropertyManager.LdapDisplayName;
            ldapDisplayName[1] = PropertyManager.Cn;
            ldapDisplayName[2] = PropertyManager.IsDefunct;
            ADSearcher             aDSearcher = new ADSearcher(schemaEntry, filter, ldapDisplayName, SearchScope.OneLevel);
            SearchResultCollection searchResultCollections = null;

            using (searchResultCollections)
            {
                try
                {
                    searchResultCollections = aDSearcher.FindAll();
                    foreach (SearchResult searchResult in searchResultCollections)
                    {
                        string         searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.LdapDisplayName);
                        DirectoryEntry directoryEntry            = searchResult.GetDirectoryEntry();
                        directoryEntry.AuthenticationType = Utils.DefaultAuthType;
                        directoryEntry.Username           = context.UserName;
                        directoryEntry.Password           = context.Password;
                        bool item = false;
                        if (searchResult.Properties[PropertyManager.IsDefunct] != null && searchResult.Properties[PropertyManager.IsDefunct].Count > 0)
                        {
                            item = (bool)searchResult.Properties[PropertyManager.IsDefunct][0];
                        }
                        if (!item)
                        {
                            arrayLists.Add(new ActiveDirectorySchemaProperty(context, searchResultPropertyValue, directoryEntry, schemaEntry));
                        }
                        else
                        {
                            string str = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.Cn);
                            arrayLists.Add(new ActiveDirectorySchemaProperty(context, str, searchResultPropertyValue, directoryEntry, schemaEntry));
                        }
                    }
                }
                catch (COMException cOMException1)
                {
                    COMException cOMException = cOMException1;
                    throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
                }
            }
            return(new ReadOnlyActiveDirectorySchemaPropertyCollection(arrayLists));
        }
Esempio n. 11
0
        internal ReplicationConnectionCollection GetOutboundConnectionsHelper()
        {
            // this is the first time that user tries to retrieve this property, so get it from the directory
            if (_outbound == null)
            {
                // search base is the site container
                string         siteName = (this is DomainController) ? ((DomainController)this).SiteObjectName : ((AdamInstance)this).SiteObjectName;
                DirectoryEntry de       = DirectoryEntryManager.GetDirectoryEntry(Utils.GetNewDirectoryContext(Name, DirectoryContextType.DirectoryServer, context), siteName);

                string     serverName = (this is DomainController) ? ((DomainController)this).ServerObjectName : ((AdamInstance)this).ServerObjectName;
                ADSearcher adSearcher = new ADSearcher(de,
                                                       "(&(objectClass=nTDSConnection)(objectCategory=nTDSConnection)(fromServer=CN=NTDS Settings," + serverName + "))",
                                                       new string[] { "objectClass", "cn" },
                                                       SearchScope.Subtree);

                SearchResultCollection results    = null;
                DirectoryContext       newContext = Utils.GetNewDirectoryContext(Name, DirectoryContextType.DirectoryServer, context);

                try
                {
                    results   = adSearcher.FindAll();
                    _outbound = new ReplicationConnectionCollection();

                    foreach (SearchResult result in results)
                    {
                        ReplicationConnection con = new ReplicationConnection(newContext, result.GetDirectoryEntry(), (string)result.Properties["cn"][0]);
                        _outbound.Add(con);
                    }
                }
                catch (COMException e)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(newContext, e);
                }
                finally
                {
                    if (results != null)
                    {
                        results.Dispose();
                    }

                    de.Dispose();
                }
            }

            return(_outbound);
        }
Esempio n. 12
0
        internal ReplicationConnectionCollection GetInboundConnectionsHelper()
        {
            if (_inbound == null)
            {
                // construct the replicationconnection collection
                _inbound = new ReplicationConnectionCollection();
                DirectoryContext newContext = Utils.GetNewDirectoryContext(Name, DirectoryContextType.DirectoryServer, context);

                // this is the first time that user tries to retrieve this property, so get it from the directory
                string         serverName = (this is DomainController) ? ((DomainController)this).ServerObjectName : ((AdamInstance)this).ServerObjectName;
                string         srchDN     = "CN=NTDS Settings," + serverName;
                DirectoryEntry de         = DirectoryEntryManager.GetDirectoryEntry(Utils.GetNewDirectoryContext(Name, DirectoryContextType.DirectoryServer, context), srchDN);

                ADSearcher adSearcher = new ADSearcher(de,
                                                       "(&(objectClass=nTDSConnection)(objectCategory=nTDSConnection))",
                                                       new string[] { "cn" },
                                                       SearchScope.OneLevel);
                SearchResultCollection srchResults = null;

                try
                {
                    srchResults = adSearcher.FindAll();
                    foreach (SearchResult r in srchResults)
                    {
                        ReplicationConnection con = new ReplicationConnection(newContext, r.GetDirectoryEntry(), (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.Cn));
                        _inbound.Add(con);
                    }
                }
                catch (COMException e)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(newContext, e);
                }
                finally
                {
                    if (srchResults != null)
                    {
                        srchResults.Dispose();
                    }

                    de.Dispose();
                }
            }

            return(_inbound);
        }
Esempio n. 13
0
        private ArrayList GetSites()
        {
            ArrayList      sites      = new ArrayList();
            DirectoryEntry sitesEntry = _directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.SitesContainer);

            // search for all the "site" objects
            // (one-level search is good enough)
            // setup the directory searcher object
            string filter = "(" + PropertyManager.ObjectCategory + "=site)";

            string[] propertiesToLoad = new string[1];

            propertiesToLoad[0] = PropertyManager.Cn;

            ADSearcher             searcher = new ADSearcher(sitesEntry, filter, propertiesToLoad, SearchScope.OneLevel);
            SearchResultCollection resCol   = null;

            try
            {
                resCol = searcher.FindAll();

                foreach (SearchResult res in resCol)
                {
                    // an existing site
                    sites.Add(new ActiveDirectorySite(_context, (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.Cn), true));
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(_context, e);
            }
            finally
            {
                if (resCol != null)
                {
                    // call dispose on search result collection
                    resCol.Dispose();
                }
            }
            return(sites);
        }
 private ArrayList GetApplicationPartitions()
 {
     ArrayList list = new ArrayList();
     DirectoryEntry cachedDirectoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
     DirectoryEntry searchRoot = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.PartitionsContainer);
     StringBuilder builder = new StringBuilder(100);
     builder.Append("(&(");
     builder.Append(PropertyManager.ObjectCategory);
     builder.Append("=crossRef)(");
     builder.Append(PropertyManager.SystemFlags);
     builder.Append(":1.2.840.113556.1.4.804:=");
     builder.Append(1);
     builder.Append(")(!(");
     builder.Append(PropertyManager.SystemFlags);
     builder.Append(":1.2.840.113556.1.4.803:=");
     builder.Append(2);
     builder.Append(")))");
     string filter = builder.ToString();
     string[] propertiesToLoad = new string[] { PropertyManager.NCName, PropertyManager.MsDSNCReplicaLocations };
     ADSearcher searcher = new ADSearcher(searchRoot, filter, propertiesToLoad, SearchScope.OneLevel);
     SearchResultCollection results = null;
     try
     {
         results = searcher.FindAll();
         string str2 = (string) PropertyManager.GetPropertyValue(this.context, cachedDirectoryEntry, PropertyManager.SchemaNamingContext);
         string str3 = (string) PropertyManager.GetPropertyValue(this.context, cachedDirectoryEntry, PropertyManager.ConfigurationNamingContext);
         foreach (SearchResult result in results)
         {
             string searchResultPropertyValue = (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.NCName);
             if (!searchResultPropertyValue.Equals(str2) && !searchResultPropertyValue.Equals(str3))
             {
                 ResultPropertyValueCollection values = result.Properties[PropertyManager.MsDSNCReplicaLocations];
                 if (values.Count > 0)
                 {
                     DirectoryContext context = Utils.GetNewDirectoryContext(Utils.GetAdamDnsHostNameFromNTDSA(this.context, (string) values[Utils.GetRandomIndex(values.Count)]), DirectoryContextType.DirectoryServer, this.context);
                     list.Add(new ApplicationPartition(context, searchResultPropertyValue, null, ApplicationPartitionType.ADAMApplicationPartition, new DirectoryEntryManager(context)));
                 }
             }
         }
         return list;
     }
     catch (COMException exception)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception);
     }
     finally
     {
         if (results != null)
         {
             results.Dispose();
         }
     }
     return list;
 }
Esempio n. 15
0
        internal static AdamInstance FindAnyAdamInstance(DirectoryContext context)
        {
            if (context.ContextType != DirectoryContextType.ConfigurationSet)
            {
                // assuming it's an ADAM Instance
                // check that it is an ADAM server only (not AD)
                DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
                DirectoryEntry        rootDSE           = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);

                if (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryApplicationMode))
                {
                    directoryEntryMgr.RemoveIfExists(directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDSE));
                    throw new ArgumentException(SR.TargetShouldBeServerORConfigSet, nameof(context));
                }

                string dnsHostName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.DnsHostName);

                return(new AdamInstance(context, dnsHostName, directoryEntryMgr));
            }

            // Now this is the case where context is a Config Set
            // Here we need to search for the service connection points in the forest
            // (if the forest object was created by specifying the server, we stick to that, else search in a GC)
            DirectoryEntry rootEntry         = GetSearchRootEntry(Forest.GetCurrentForest());
            ArrayList      adamInstanceNames = new ArrayList();

            try
            {
                string entryName = (string)rootEntry.Properties["distinguishedName"].Value;

                // Search for computer "serviceConnectionObjects" where the keywords attribute
                // contains the specified keyword
                // set up the searcher object

                // build the filter
                StringBuilder str = new StringBuilder(15);
                str.Append("(&(");
                str.Append(PropertyManager.ObjectCategory);
                str.Append("=serviceConnectionPoint)");
                str.Append("(");
                str.Append(PropertyManager.Keywords);
                str.Append("=1.2.840.113556.1.4.1851)(");
                str.Append(PropertyManager.Keywords);
                str.Append("=");
                str.Append(Utils.GetEscapedFilterValue(context.Name)); // target = config set name
                str.Append("))");

                string   filter           = str.ToString();
                string[] propertiesToLoad = new string[1];

                propertiesToLoad[0] = PropertyManager.ServiceBindingInformation;

                ADSearcher             searcher = new ADSearcher(rootEntry, filter, propertiesToLoad, SearchScope.Subtree, false /*not paged search*/, false /*no cached results*/);
                SearchResultCollection resCol   = searcher.FindAll();

                try
                {
                    foreach (SearchResult res in resCol)
                    {
                        // the binding info contains two values
                        // "ldap://hostname:ldapport"
                        // and "ldaps://hostname:sslport"
                        // we need the "hostname:ldapport" value
                        string prefix = "ldap://";

                        foreach (string bindingInfo in res.Properties[PropertyManager.ServiceBindingInformation])
                        {
                            if ((bindingInfo.Length > prefix.Length) && (string.Equals(bindingInfo.Substring(0, prefix.Length), prefix, StringComparison.OrdinalIgnoreCase)))
                            {
                                adamInstanceNames.Add(bindingInfo.Substring(prefix.Length));
                            }
                        }
                    }
                }
                finally
                {
                    resCol.Dispose();
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                rootEntry.Dispose();
            }

            //
            // we have all the adam instance names in teh form of server:port from the scp
            // now we need to find one that is alive
            //
            return(FindAliveAdamInstance(null, context, adamInstanceNames));
        }
Esempio n. 16
0
		private void GetAdjacentSites()
		{
			ActiveDirectoryTransportType activeDirectoryTransportType;
			DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, WellKnownDN.RootDSE);
			string item = (string)directoryEntry.Properties["configurationNamingContext"][0];
			string str = string.Concat("CN=Inter-Site Transports,CN=Sites,", item);
			directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, str);
			string[] strArrays = new string[2];
			strArrays[0] = "cn";
			strArrays[1] = "distinguishedName";
			ADSearcher aDSearcher = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=siteLink)(objectCategory=SiteLink)(siteList=", Utils.GetEscapedFilterValue((string)PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.DistinguishedName)), "))"), strArrays, SearchScope.Subtree);
			SearchResultCollection searchResultCollections = null;
			try
			{
				searchResultCollections = aDSearcher.FindAll();
			}
			catch (COMException cOMException1)
			{
				COMException cOMException = cOMException1;
				throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException);
			}
			try
			{
				ActiveDirectorySiteLink activeDirectorySiteLink = null;
				foreach (SearchResult searchResult in searchResultCollections)
				{
					string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DistinguishedName);
					string searchResultPropertyValue1 = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.Cn);
					string value = Utils.GetDNComponents(searchResultPropertyValue)[1].Value;
					if (string.Compare(value, "IP", StringComparison.OrdinalIgnoreCase) != 0)
					{
						if (string.Compare(value, "SMTP", StringComparison.OrdinalIgnoreCase) != 0)
						{
							object[] objArray = new object[1];
							objArray[0] = value;
							string str1 = Res.GetString("UnknownTransport", objArray);
							throw new ActiveDirectoryOperationException(str1);
						}
						else
						{
							activeDirectoryTransportType = ActiveDirectoryTransportType.Smtp;
						}
					}
					else
					{
						activeDirectoryTransportType = ActiveDirectoryTransportType.Rpc;
					}
					try
					{
						activeDirectorySiteLink = new ActiveDirectorySiteLink(this.context, searchResultPropertyValue1, activeDirectoryTransportType, true, searchResult.GetDirectoryEntry());
						foreach (ActiveDirectorySite site in activeDirectorySiteLink.Sites)
						{
							if (Utils.Compare(site.Name, this.Name) == 0 || this.adjacentSites.Contains(site))
							{
								continue;
							}
							this.adjacentSites.Add(site);
						}
					}
					finally
					{
						activeDirectorySiteLink.Dispose();
					}
				}
			}
			finally
			{
				searchResultCollections.Dispose();
				directoryEntry.Dispose();
			}
		}
Esempio n. 17
0
		private void GetSubnets()
		{
			DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, WellKnownDN.RootDSE);
			string propertyValue = (string)PropertyManager.GetPropertyValue(this.context, directoryEntry, PropertyManager.ConfigurationNamingContext);
			string str = string.Concat("CN=Subnets,CN=Sites,", propertyValue);
			directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, str);
			string[] strArrays = new string[2];
			strArrays[0] = "cn";
			strArrays[1] = "location";
			ADSearcher aDSearcher = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=subnet)(objectCategory=subnet)(siteObject=", Utils.GetEscapedFilterValue((string)PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.DistinguishedName)), "))"), strArrays, SearchScope.OneLevel);
			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.Cn);
					ActiveDirectorySubnet activeDirectorySubnet = new ActiveDirectorySubnet(this.context, searchResultPropertyValue, null, true);
					activeDirectorySubnet.cachedEntry = searchResult.GetDirectoryEntry();
					activeDirectorySubnet.Site = this;
					this.subnets.Add(activeDirectorySubnet);
				}
			}
			finally
			{
				searchResultCollections.Dispose();
				directoryEntry.Dispose();
			}
		}
Esempio n. 18
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();
			}
		}
Esempio n. 19
0
        public static ActiveDirectorySiteLink FindByName(DirectoryContext context, string siteLinkName, ActiveDirectoryTransportType transport)
        {
            ValidateArgument(context, siteLinkName, transport);

            //  work with copy of the context
            context = new DirectoryContext(context);

            // bind to the rootdse to get the configurationnamingcontext
            DirectoryEntry de;

            try
            {
                de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                string config      = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ConfigurationNamingContext);
                string containerDN = "CN=Inter-Site Transports,CN=Sites," + config;
                if (transport == ActiveDirectoryTransportType.Rpc)
                {
                    containerDN = "CN=IP," + containerDN;
                }
                else
                {
                    containerDN = "CN=SMTP," + containerDN;
                }
                de = DirectoryEntryManager.GetDirectoryEntry(context, containerDN);
            }
            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(String.Format(CultureInfo.CurrentCulture, SR.ADAMInstanceNotFoundInConfigSet, context.Name));
            }

            try
            {
                ADSearcher adSearcher = new ADSearcher(de,
                                                       "(&(objectClass=siteLink)(objectCategory=SiteLink)(name=" + Utils.GetEscapedFilterValue(siteLinkName) + "))",
                                                       new string[] { "distinguishedName" },
                                                       SearchScope.OneLevel,
                                                       false, /* don't need paged search */
                                                       false  /* don't need to cache result */
                                                       );
                SearchResult srchResult = adSearcher.FindOne();
                if (srchResult == null)
                {
                    // no such sitelink object
                    Exception e = new ActiveDirectoryObjectNotFoundException(SR.DSNotFound, typeof(ActiveDirectorySiteLink), siteLinkName);
                    throw e;
                }
                else
                {
                    DirectoryEntry connectionEntry = srchResult.GetDirectoryEntry();
                    // it is an existing site object
                    ActiveDirectorySiteLink link = new ActiveDirectorySiteLink(context, siteLinkName, transport, true, connectionEntry);
                    return(link);
                }
            }
            catch (COMException e)
            {
                if (e.ErrorCode == unchecked ((int)0x80072030))
                {
                    // if it is ADAM and transport type is SMTP, throw NotSupportedException.
                    DirectoryEntry tmpDE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                    if (Utils.CheckCapability(tmpDE, Capability.ActiveDirectoryApplicationMode) && transport == ActiveDirectoryTransportType.Smtp)
                    {
                        throw new NotSupportedException(SR.NotSupportTransportSMTP);
                    }
                    else
                    {
                        // object is not found since we cannot even find the container in which to search
                        throw new ActiveDirectoryObjectNotFoundException(SR.DSNotFound, typeof(ActiveDirectorySiteLink), siteLinkName);
                    }
                }

                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                de.Dispose();
            }
        }
        public static ActiveDirectorySiteLinkBridge FindByName(DirectoryContext context, string bridgeName, ActiveDirectoryTransportType transport)
        {
            DirectoryEntry directoryEntry;
            ActiveDirectorySiteLinkBridge bridge2;

            ValidateArgument(context, bridgeName, transport);
            context = new DirectoryContext(context);
            try
            {
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                string str = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext);
                string dn  = "CN=Inter-Site Transports,CN=Sites," + str;
                if (transport == ActiveDirectoryTransportType.Rpc)
                {
                    dn = "CN=IP," + dn;
                }
                else
                {
                    dn = "CN=SMTP," + dn;
                }
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, dn);
            }
            catch (COMException exception)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { context.Name }));
            }
            try
            {
                SearchResult result = new ADSearcher(directoryEntry, "(&(objectClass=siteLinkBridge)(objectCategory=SiteLinkBridge)(name=" + Utils.GetEscapedFilterValue(bridgeName) + "))", new string[] { "distinguishedName" }, SearchScope.OneLevel, false, false).FindOne();
                if (result == null)
                {
                    Exception exception2 = new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySiteLinkBridge), bridgeName);
                    throw exception2;
                }
                DirectoryEntry entry2 = result.GetDirectoryEntry();
                bridge2 = new ActiveDirectorySiteLinkBridge(context, bridgeName, transport, true)
                {
                    cachedEntry = entry2
                };
            }
            catch (COMException exception3)
            {
                if (exception3.ErrorCode != -2147016656)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, exception3);
                }
                if (Utils.CheckCapability(DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE), Capability.ActiveDirectoryApplicationMode) && (transport == ActiveDirectoryTransportType.Smtp))
                {
                    throw new NotSupportedException(Res.GetString("NotSupportTransportSMTP"));
                }
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySiteLinkBridge), bridgeName);
            }
            finally
            {
                directoryEntry.Dispose();
            }
            return(bridge2);
        }
Esempio n. 21
0
        internal static ReadOnlyActiveDirectorySchemaClassCollection GetAllClasses(DirectoryContext context, DirectoryEntry schemaEntry, string filter)
        {
            ArrayList classList = new ArrayList();

            string[] propertiesToLoad = new string[3];
            propertiesToLoad[0] = PropertyManager.LdapDisplayName;
            propertiesToLoad[1] = PropertyManager.Cn;
            propertiesToLoad[2] = PropertyManager.IsDefunct;

            ADSearcher searcher = new ADSearcher(schemaEntry, filter, propertiesToLoad, SearchScope.OneLevel);
            SearchResultCollection resCol = null;
            try
            {
                resCol = searcher.FindAll();
                foreach (SearchResult res in resCol)
                {
                    string ldapDisplayName = (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.LdapDisplayName);
                    DirectoryEntry directoryEntry = res.GetDirectoryEntry();

                    directoryEntry.AuthenticationType = Utils.DefaultAuthType;
                    directoryEntry.Username = context.UserName;
                    directoryEntry.Password = context.Password;

                    bool isDefunct = false;

                    if ((res.Properties[PropertyManager.IsDefunct] != null) && (res.Properties[PropertyManager.IsDefunct].Count > 0))
                    {
                        isDefunct = (bool)res.Properties[PropertyManager.IsDefunct][0];
                    }

                    if (isDefunct)
                    {
                        string commonName = (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.Cn);
                        classList.Add(new ActiveDirectorySchemaClass(context, commonName, ldapDisplayName, directoryEntry, schemaEntry));
                    }
                    else
                    {
                        classList.Add(new ActiveDirectorySchemaClass(context, ldapDisplayName, directoryEntry, schemaEntry));
                    }
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                // dispose off the result collection
                if (resCol != null)
                {
                    resCol.Dispose();
                }
            }

            return new ReadOnlyActiveDirectorySchemaClassCollection(classList);
        }
Esempio n. 22
0
        public static ActiveDirectorySubnet FindByName(DirectoryContext context, string subnetName)
        {
            DirectoryEntry        directoryEntry;
            ActiveDirectorySubnet activeDirectorySubnet;
            ActiveDirectorySubnet activeDirectorySubnet1;

            ActiveDirectorySubnet.ValidateArgument(context, subnetName);
            context = new DirectoryContext(context);
            try
            {
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                string propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext);
                string str           = string.Concat("CN=Subnets,CN=Sites,", propertyValue);
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, str);
            }
            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));
            }
            using (directoryEntry)
            {
                try
                {
                    string[] strArrays = new string[1];
                    strArrays[0] = "distinguishedName";
                    ADSearcher   aDSearcher   = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=subnet)(objectCategory=subnet)(name=", Utils.GetEscapedFilterValue(subnetName), "))"), strArrays, SearchScope.OneLevel, false, false);
                    SearchResult searchResult = aDSearcher.FindOne();
                    if (searchResult != null)
                    {
                        string         str1            = null;
                        DirectoryEntry directoryEntry1 = searchResult.GetDirectoryEntry();
                        if (directoryEntry1.Properties.Contains("siteObject"))
                        {
                            NativeComInterfaces.IAdsPathname pathname = (NativeComInterfaces.IAdsPathname)(new NativeComInterfaces.Pathname());
                            pathname.EscapedMode = 4;
                            string item = (string)directoryEntry1.Properties["siteObject"][0];
                            pathname.Set(item, 4);
                            string str2 = pathname.Retrieve(11);
                            str1 = str2.Substring(3);
                        }
                        if (str1 != null)
                        {
                            activeDirectorySubnet = new ActiveDirectorySubnet(context, subnetName, str1, true);
                        }
                        else
                        {
                            activeDirectorySubnet = new ActiveDirectorySubnet(context, subnetName, null, true);
                        }
                        activeDirectorySubnet.cachedEntry = directoryEntry1;
                        activeDirectorySubnet1            = activeDirectorySubnet;
                    }
                    else
                    {
                        Exception exception = new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySubnet), subnetName);
                        throw exception;
                    }
                }
                catch (COMException cOMException3)
                {
                    COMException cOMException2 = cOMException3;
                    if (cOMException2.ErrorCode != -2147016656)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException2);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySubnet), subnetName);
                    }
                }
            }
            return(activeDirectorySubnet1);
        }
Esempio n. 23
0
 internal static AdamInstance FindAnyAdamInstance(DirectoryContext context)
 {
     if (context.ContextType == DirectoryContextType.ConfigurationSet)
     {
         DirectoryEntry searchRootEntry = ConfigurationSet.GetSearchRootEntry(Forest.GetCurrentForest());
         ArrayList      arrayLists      = new ArrayList();
         try
         {
             try
             {
                 StringBuilder stringBuilder = new StringBuilder(15);
                 stringBuilder.Append("(&(");
                 stringBuilder.Append(PropertyManager.ObjectCategory);
                 stringBuilder.Append("=serviceConnectionPoint)");
                 stringBuilder.Append("(");
                 stringBuilder.Append(PropertyManager.Keywords);
                 stringBuilder.Append("=1.2.840.113556.1.4.1851)(");
                 stringBuilder.Append(PropertyManager.Keywords);
                 stringBuilder.Append("=");
                 stringBuilder.Append(Utils.GetEscapedFilterValue(context.Name));
                 stringBuilder.Append("))");
                 string   str = stringBuilder.ToString();
                 string[] serviceBindingInformation = new string[1];
                 serviceBindingInformation[0] = PropertyManager.ServiceBindingInformation;
                 ADSearcher             aDSearcher = new ADSearcher(searchRootEntry, str, serviceBindingInformation, SearchScope.Subtree, false, false);
                 SearchResultCollection searchResultCollections = aDSearcher.FindAll();
                 try
                 {
                     foreach (SearchResult item in searchResultCollections)
                     {
                         string      str1       = "ldap://";
                         IEnumerator enumerator = item.Properties[PropertyManager.ServiceBindingInformation].GetEnumerator();
                         try
                         {
                             while (enumerator.MoveNext())
                             {
                                 string str2 = item.ToString();
                                 if (str2.Length <= str1.Length || string.Compare(str2.Substring(0, str1.Length), str1, StringComparison.OrdinalIgnoreCase) != 0)
                                 {
                                     continue;
                                 }
                                 arrayLists.Add(str2.Substring(str1.Length));
                             }
                         }
                         finally
                         {
                             IDisposable disposable = enumerator as IDisposable;
                             if (disposable != null)
                             {
                                 disposable.Dispose();
                             }
                         }
                     }
                 }
                 finally
                 {
                     searchResultCollections.Dispose();
                 }
             }
             catch (COMException cOMException1)
             {
                 COMException cOMException = cOMException1;
                 throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
             }
         }
         finally
         {
             searchRootEntry.Dispose();
         }
         return(ConfigurationSet.FindAliveAdamInstance(null, context, arrayLists));
     }
     else
     {
         DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(context);
         DirectoryEntry        cachedDirectoryEntry  = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
         if (Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
         {
             string propertyValue = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.DnsHostName);
             return(new AdamInstance(context, propertyValue, directoryEntryManager));
         }
         else
         {
             directoryEntryManager.RemoveIfExists(directoryEntryManager.ExpandWellKnownDN(WellKnownDN.RootDSE));
             throw new ArgumentException(Res.GetString("TargetShouldBeServerORConfigSet"), "context");
         }
     }
 }
Esempio n. 24
0
        public static ActiveDirectorySiteLink FindByName(DirectoryContext context, string siteLinkName, ActiveDirectoryTransportType transport)
        {
            ValidateArgument(context, siteLinkName, transport);

            //  work with copy of the context
            context = new DirectoryContext(context);

            // bind to the rootdse to get the configurationnamingcontext
            DirectoryEntry de;

            try
            {
                de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                string config = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ConfigurationNamingContext);
                string containerDN = "CN=Inter-Site Transports,CN=Sites," + config;
                if (transport == ActiveDirectoryTransportType.Rpc)
                    containerDN = "CN=IP," + containerDN;
                else
                    containerDN = "CN=SMTP," + containerDN;
                de = DirectoryEntryManager.GetDirectoryEntry(context, containerDN);
            }
            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));
            }

            try
            {
                ADSearcher adSearcher = new ADSearcher(de,
                                                      "(&(objectClass=siteLink)(objectCategory=SiteLink)(name=" + Utils.GetEscapedFilterValue(siteLinkName) + "))",
                                                      new string[] { "distinguishedName" },
                                                      SearchScope.OneLevel,
                                                      false, /* don't need paged search */
                                                      false /* don't need to cache result */
                                                      );
                SearchResult srchResult = adSearcher.FindOne();
                if (srchResult == null)
                {
                    // no such sitelink object
                    Exception e = new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DSNotFound), typeof(ActiveDirectorySiteLink), siteLinkName);
                    throw e;
                }
                else
                {
                    DirectoryEntry connectionEntry = srchResult.GetDirectoryEntry();
                    // it is an existing site object
                    ActiveDirectorySiteLink link = new ActiveDirectorySiteLink(context, siteLinkName, transport, true, connectionEntry);
                    return link;
                }
            }
            catch (COMException e)
            {
                if (e.ErrorCode == unchecked((int)0x80072030))
                {
                    // if it is ADAM and transport type is SMTP, throw NotSupportedException.
                    DirectoryEntry tmpDE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                    if (Utils.CheckCapability(tmpDE, Capability.ActiveDirectoryApplicationMode) && transport == ActiveDirectoryTransportType.Smtp)
                    {
                        throw new NotSupportedException(Res.GetString(Res.NotSupportTransportSMTP));
                    }
                    else
                    {
                        // object is not found since we cannot even find the container in which to search
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DSNotFound), typeof(ActiveDirectorySiteLink), siteLinkName);
                    }
                }

                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                de.Dispose();
            }
        }
Esempio n. 25
0
        internal ReplicationConnectionCollection GetOutboundConnectionsHelper()
        {
            // this is the first time that user tries to retrieve this property, so get it from the directory
            if (_outbound == null)
            {
                // search base is the site container
                string siteName = (this is DomainController) ? ((DomainController)this).SiteObjectName : ((AdamInstance)this).SiteObjectName;
                DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(Utils.GetNewDirectoryContext(Name, DirectoryContextType.DirectoryServer, context), siteName);

                string serverName = (this is DomainController) ? ((DomainController)this).ServerObjectName : ((AdamInstance)this).ServerObjectName;
                ADSearcher adSearcher = new ADSearcher(de,
                                                               "(&(objectClass=nTDSConnection)(objectCategory=nTDSConnection)(fromServer=CN=NTDS Settings," + serverName + "))",
                                                               new string[] { "objectClass", "cn" },
                                                               SearchScope.Subtree);

                SearchResultCollection results = null;
                DirectoryContext newContext = Utils.GetNewDirectoryContext(Name, DirectoryContextType.DirectoryServer, context);

                try
                {
                    results = adSearcher.FindAll();
                    _outbound = new ReplicationConnectionCollection();

                    foreach (SearchResult result in results)
                    {
                        ReplicationConnection con = new ReplicationConnection(newContext, result.GetDirectoryEntry(), (string)result.Properties["cn"][0]);
                        _outbound.Add(con);
                    }
                }
                catch (COMException e)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(newContext, e);
                }
                finally
                {
                    if (results != null)
                        results.Dispose();

                    de.Dispose();
                }
            }

            return _outbound;
        }
Esempio n. 26
0
        internal ReplicationConnectionCollection GetInboundConnectionsHelper()
        {
            if (_inbound == null)
            {
                // construct the replicationconnection collection
                _inbound = new ReplicationConnectionCollection();
                DirectoryContext newContext = Utils.GetNewDirectoryContext(Name, DirectoryContextType.DirectoryServer, context);

                // this is the first time that user tries to retrieve this property, so get it from the directory   
                string serverName = (this is DomainController) ? ((DomainController)this).ServerObjectName : ((AdamInstance)this).ServerObjectName;
                string srchDN = "CN=NTDS Settings," + serverName;
                DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(Utils.GetNewDirectoryContext(Name, DirectoryContextType.DirectoryServer, context), srchDN);

                ADSearcher adSearcher = new ADSearcher(de,
                                                      "(&(objectClass=nTDSConnection)(objectCategory=nTDSConnection))",
                                                      new string[] { "cn" },
                                                      SearchScope.OneLevel);
                SearchResultCollection srchResults = null;

                try
                {
                    srchResults = adSearcher.FindAll();
                    foreach (SearchResult r in srchResults)
                    {
                        ReplicationConnection con = new ReplicationConnection(newContext, r.GetDirectoryEntry(), (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.Cn));
                        _inbound.Add(con);
                    }
                }
                catch (COMException e)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(newContext, e);
                }
                finally
                {
                    if (srchResults != null)
                        srchResults.Dispose();

                    de.Dispose();
                }
            }

            return _inbound;
        }
 private ArrayList GetSites()
 {
     ArrayList list = new ArrayList();
     DirectoryEntry cachedDirectoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.SitesContainer);
     string filter = "(" + PropertyManager.ObjectCategory + "=site)";
     string[] propertiesToLoad = new string[] { PropertyManager.Cn };
     ADSearcher searcher = new ADSearcher(cachedDirectoryEntry, filter, propertiesToLoad, SearchScope.OneLevel);
     SearchResultCollection results = null;
     try
     {
         results = searcher.FindAll();
         foreach (SearchResult result in results)
         {
             list.Add(new ActiveDirectorySite(this.context, (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.Cn), true));
         }
         return list;
     }
     catch (COMException exception)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception);
     }
     finally
     {
         if (results != null)
         {
             results.Dispose();
         }
     }
     return list;
 }
Esempio n. 28
0
		private ArrayList GetApplicationPartitions()
		{
			ArrayList arrayLists = new ArrayList();
			DirectoryEntry cachedDirectoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
			DirectoryEntry directoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.PartitionsContainer);
			StringBuilder stringBuilder = new StringBuilder(100);
			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(")))");
			string str = stringBuilder.ToString();
			string[] nCName = new string[2];
			nCName[0] = PropertyManager.NCName;
			nCName[1] = PropertyManager.MsDSNCReplicaLocations;
			ADSearcher aDSearcher = new ADSearcher(directoryEntry, str, nCName, SearchScope.OneLevel);
			SearchResultCollection searchResultCollections = null;
			using (searchResultCollections)
			{
				try
				{
					searchResultCollections = aDSearcher.FindAll();
					string propertyValue = (string)PropertyManager.GetPropertyValue(this.context, cachedDirectoryEntry, PropertyManager.SchemaNamingContext);
					string propertyValue1 = (string)PropertyManager.GetPropertyValue(this.context, cachedDirectoryEntry, PropertyManager.ConfigurationNamingContext);
					foreach (SearchResult searchResult in searchResultCollections)
					{
						string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.NCName);
						if (searchResultPropertyValue.Equals(propertyValue) || searchResultPropertyValue.Equals(propertyValue1))
						{
							continue;
						}
						ResultPropertyValueCollection item = searchResult.Properties[PropertyManager.MsDSNCReplicaLocations];
						if (item.Count <= 0)
						{
							continue;
						}
						string adamDnsHostNameFromNTDSA = Utils.GetAdamDnsHostNameFromNTDSA(this.context, (string)item[Utils.GetRandomIndex(item.Count)]);
						DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(adamDnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, this.context);
						arrayLists.Add(new ApplicationPartition(newDirectoryContext, searchResultPropertyValue, null, ApplicationPartitionType.ADAMApplicationPartition, new DirectoryEntryManager(newDirectoryContext)));
					}
				}
				catch (COMException cOMException1)
				{
					COMException cOMException = cOMException1;
					throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException);
				}
			}
			return arrayLists;
		}
Esempio n. 29
0
		private ArrayList GetSites()
		{
			ArrayList arrayLists = new ArrayList();
			DirectoryEntry cachedDirectoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.SitesContainer);
			string str = string.Concat("(", PropertyManager.ObjectCategory, "=site)");
			string[] cn = new string[1];
			cn[0] = PropertyManager.Cn;
			ADSearcher aDSearcher = new ADSearcher(cachedDirectoryEntry, str, cn, SearchScope.OneLevel);
			SearchResultCollection searchResultCollections = null;
			using (searchResultCollections)
			{
				try
				{
					searchResultCollections = aDSearcher.FindAll();
					foreach (SearchResult searchResult in searchResultCollections)
					{
						arrayLists.Add(new ActiveDirectorySite(this.context, (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.Cn), true));
					}
				}
				catch (COMException cOMException1)
				{
					COMException cOMException = cOMException1;
					throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException);
				}
			}
			return arrayLists;
		}
 private ArrayList GetDomains()
 {
     ArrayList list = new ArrayList();
     DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer));
     StringBuilder builder = new StringBuilder(15);
     builder.Append("(&(");
     builder.Append(PropertyManager.ObjectCategory);
     builder.Append("=crossRef)(");
     builder.Append(PropertyManager.SystemFlags);
     builder.Append(":1.2.840.113556.1.4.804:=");
     builder.Append(1);
     builder.Append(")(");
     builder.Append(PropertyManager.SystemFlags);
     builder.Append(":1.2.840.113556.1.4.804:=");
     builder.Append(2);
     builder.Append("))");
     string filter = builder.ToString();
     string[] propertiesToLoad = new string[] { PropertyManager.DnsRoot };
     ADSearcher searcher = new ADSearcher(directoryEntry, filter, propertiesToLoad, SearchScope.OneLevel);
     SearchResultCollection results = null;
     try
     {
         results = searcher.FindAll();
         foreach (SearchResult result in results)
         {
             string searchResultPropertyValue = (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.DnsRoot);
             DirectoryContext context = Utils.GetNewDirectoryContext(searchResultPropertyValue, DirectoryContextType.Domain, this.context);
             list.Add(new Domain(context, searchResultPropertyValue));
         }
         return list;
     }
     catch (COMException exception)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception);
     }
     finally
     {
         if (results != null)
         {
             results.Dispose();
         }
         directoryEntry.Dispose();
     }
     return list;
 }
Esempio n. 31
0
		private void LoadCrossRefAttributes()
		{
			DirectoryEntry directoryEntry = null;
			using (directoryEntry)
			{
				try
				{
					directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer));
					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.804:=");
					stringBuilder.Append(2);
					stringBuilder.Append(")(");
					stringBuilder.Append(PropertyManager.DnsRoot);
					stringBuilder.Append("=");
					stringBuilder.Append(Utils.GetEscapedFilterValue(this.partitionName));
					stringBuilder.Append("))");
					string str = stringBuilder.ToString();
					string[] distinguishedName = new string[2];
					distinguishedName[0] = PropertyManager.DistinguishedName;
					distinguishedName[1] = PropertyManager.TrustParent;
					ADSearcher aDSearcher = new ADSearcher(directoryEntry, str, distinguishedName, SearchScope.OneLevel, false, false);
					SearchResult searchResult = aDSearcher.FindOne();
					this.crossRefDN = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DistinguishedName);
					if (searchResult.Properties[PropertyManager.TrustParent].Count > 0)
					{
						this.trustParent = (string)searchResult.Properties[PropertyManager.TrustParent][0];
					}
				}
				catch (COMException cOMException1)
				{
					COMException cOMException = cOMException1;
					throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException);
				}
			}
		}
Esempio n. 32
0
        private ArrayList GetApplicationPartitions()
        {
            ArrayList      appNCs          = new ArrayList();
            DirectoryEntry rootDSE         = _directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
            DirectoryEntry partitionsEntry = _directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.PartitionsContainer);

            // search for all the "crossRef" objects that have the
            // ADS_SYSTEMFLAG_CR_NTDS_NC set and the SYSTEMFLAG_CR_NTDS_DOMAIN flag not set
            // (one-level search is good enough)
            // setup the directory searcher object

            // build the filter
            StringBuilder str = new StringBuilder(100);

            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(")))");

            string filter = str.ToString();

            string[] propertiesToLoad = new string[2];
            propertiesToLoad[0] = PropertyManager.NCName;
            propertiesToLoad[1] = PropertyManager.MsDSNCReplicaLocations;

            ADSearcher             searcher = new ADSearcher(partitionsEntry, filter, propertiesToLoad, SearchScope.OneLevel);
            SearchResultCollection resCol   = null;

            try
            {
                resCol = searcher.FindAll();

                string schemaNamingContext        = (string)PropertyManager.GetPropertyValue(_context, rootDSE, PropertyManager.SchemaNamingContext);
                string configurationNamingContext = (string)PropertyManager.GetPropertyValue(_context, rootDSE, PropertyManager.ConfigurationNamingContext);

                foreach (SearchResult res in resCol)
                {
                    // add the name of the appNC only if it is not
                    // the Schema or Configuration partition
                    string nCName = (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.NCName);

                    if ((!(nCName.Equals(schemaNamingContext))) && (!(nCName.Equals(configurationNamingContext))))
                    {
                        ResultPropertyValueCollection replicaLocations = res.Properties[PropertyManager.MsDSNCReplicaLocations];
                        if (replicaLocations.Count > 0)
                        {
                            string           replicaName  = Utils.GetAdamDnsHostNameFromNTDSA(_context, (string)replicaLocations[Utils.GetRandomIndex(replicaLocations.Count)]);
                            DirectoryContext appNCContext = Utils.GetNewDirectoryContext(replicaName, DirectoryContextType.DirectoryServer, _context);
                            appNCs.Add(new ApplicationPartition(appNCContext, nCName, null, ApplicationPartitionType.ADAMApplicationPartition, new DirectoryEntryManager(appNCContext)));
                        }
                    }
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(_context, e);
            }
            finally
            {
                if (resCol != null)
                {
                    // call dispose on search result collection
                    resCol.Dispose();
                }
            }
            return(appNCs);
        }
 internal static SearchResult GetPropertiesFromSchemaContainer(DirectoryContext context, DirectoryEntry schemaEntry, string name, bool isDefunctOnServer)
 {
     SearchResult result = null;
     StringBuilder builder = new StringBuilder(15);
     builder.Append("(&(");
     builder.Append(PropertyManager.ObjectCategory);
     builder.Append("=attributeSchema)");
     builder.Append("(");
     if (!isDefunctOnServer)
     {
         builder.Append(PropertyManager.LdapDisplayName);
     }
     else
     {
         builder.Append(PropertyManager.Cn);
     }
     builder.Append("=");
     builder.Append(Utils.GetEscapedFilterValue(name));
     builder.Append(")");
     if (!isDefunctOnServer)
     {
         builder.Append("(!(");
     }
     else
     {
         builder.Append("(");
     }
     builder.Append(PropertyManager.IsDefunct);
     if (!isDefunctOnServer)
     {
         builder.Append("=TRUE)))");
     }
     else
     {
         builder.Append("=TRUE))");
     }
     string[] propertiesToLoad = null;
     if (!isDefunctOnServer)
     {
         propertiesToLoad = new string[] { PropertyManager.DistinguishedName, PropertyManager.Cn, PropertyManager.AttributeSyntax, PropertyManager.OMSyntax, PropertyManager.OMObjectClass, PropertyManager.Description, PropertyManager.SearchFlags, PropertyManager.IsMemberOfPartialAttributeSet, PropertyManager.LinkID, PropertyManager.SchemaIDGuid, PropertyManager.RangeLower, PropertyManager.RangeUpper };
     }
     else
     {
         propertiesToLoad = new string[] { PropertyManager.DistinguishedName, PropertyManager.Cn, PropertyManager.AttributeSyntax, PropertyManager.OMSyntax, PropertyManager.OMObjectClass, PropertyManager.Description, PropertyManager.SearchFlags, PropertyManager.IsMemberOfPartialAttributeSet, PropertyManager.LinkID, PropertyManager.SchemaIDGuid, PropertyManager.AttributeID, PropertyManager.IsSingleValued, PropertyManager.RangeLower, PropertyManager.RangeUpper, PropertyManager.LdapDisplayName };
     }
     ADSearcher searcher = new ADSearcher(schemaEntry, builder.ToString(), propertiesToLoad, SearchScope.OneLevel, false, false);
     try
     {
         result = searcher.FindOne();
     }
     catch (COMException exception)
     {
         if (exception.ErrorCode == -2147016656)
         {
             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySchemaProperty), name);
         }
         throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
     }
     if (result == null)
     {
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySchemaProperty), name);
     }
     return result;
 }
        public static ActiveDirectorySubnet FindByName(DirectoryContext context, string subnetName)
        {
            DirectoryEntry        directoryEntry;
            ActiveDirectorySubnet subnet2;

            ValidateArgument(context, subnetName);
            context = new DirectoryContext(context);
            try
            {
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                string str = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext);
                string dn  = "CN=Subnets,CN=Sites," + str;
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, dn);
            }
            catch (COMException exception)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { context.Name }));
            }
            try
            {
                SearchResult result = new ADSearcher(directoryEntry, "(&(objectClass=subnet)(objectCategory=subnet)(name=" + Utils.GetEscapedFilterValue(subnetName) + "))", new string[] { "distinguishedName" }, SearchScope.OneLevel, false, false).FindOne();
                if (result == null)
                {
                    Exception exception2 = new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySubnet), subnetName);
                    throw exception2;
                }
                string         siteName = null;
                DirectoryEntry entry2   = result.GetDirectoryEntry();
                if (entry2.Properties.Contains("siteObject"))
                {
                    NativeComInterfaces.IAdsPathname pathname = (NativeComInterfaces.IAdsPathname) new NativeComInterfaces.Pathname();
                    pathname.EscapedMode = 4;
                    string bstrADsPath = (string)entry2.Properties["siteObject"][0];
                    pathname.Set(bstrADsPath, 4);
                    siteName = pathname.Retrieve(11).Substring(3);
                }
                ActiveDirectorySubnet subnet = null;
                if (siteName == null)
                {
                    subnet = new ActiveDirectorySubnet(context, subnetName, null, true);
                }
                else
                {
                    subnet = new ActiveDirectorySubnet(context, subnetName, siteName, true);
                }
                subnet.cachedEntry = entry2;
                subnet2            = subnet;
            }
            catch (COMException exception3)
            {
                if (exception3.ErrorCode == -2147016656)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySubnet), subnetName);
                }
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception3);
            }
            finally
            {
                if (directoryEntry != null)
                {
                    directoryEntry.Dispose();
                }
            }
            return(subnet2);
        }
Esempio n. 35
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();
			}
		}
Esempio n. 36
0
        public static ActiveDirectorySubnet FindByName(DirectoryContext context, string subnetName)
        {
            ValidateArgument(context, subnetName);

            //  work with copy of the context
            context = new DirectoryContext(context);

            // bind to the rootdse to get the configurationnamingcontext
            DirectoryEntry de;

            try
            {
                de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                string config   = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ConfigurationNamingContext);
                string subnetdn = "CN=Subnets,CN=Sites," + config;
                de = DirectoryEntryManager.GetDirectoryEntry(context, subnetdn);
            }
            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));
            }

            try
            {
                ADSearcher adSearcher = new ADSearcher(de,
                                                       "(&(objectClass=subnet)(objectCategory=subnet)(name=" + Utils.GetEscapedFilterValue(subnetName) + "))",
                                                       new string[] { "distinguishedName" },
                                                       SearchScope.OneLevel,
                                                       false, /* don't need paged search */
                                                       false /* don't need to cache result */);
                SearchResult srchResult = adSearcher.FindOne();
                if (srchResult == null)
                {
                    // no such subnet object
                    Exception e = new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DSNotFound), typeof(ActiveDirectorySubnet), subnetName);
                    throw e;
                }
                else
                {
                    string         siteName        = null;
                    DirectoryEntry connectionEntry = srchResult.GetDirectoryEntry();
                    // try to get the site that this subnet lives in
                    if (connectionEntry.Properties.Contains("siteObject"))
                    {
                        NativeComInterfaces.IAdsPathname pathCracker = (NativeComInterfaces.IAdsPathname) new NativeComInterfaces.Pathname();
                        // need to turn off the escaping for name
                        pathCracker.EscapedMode = NativeComInterfaces.ADS_ESCAPEDMODE_OFF_EX;

                        string tmp = (string)connectionEntry.Properties["siteObject"][0];
                        // escaping manipulation
                        pathCracker.Set(tmp, NativeComInterfaces.ADS_SETTYPE_DN);
                        string rdn = pathCracker.Retrieve(NativeComInterfaces.ADS_FORMAT_LEAF);
                        Debug.Assert(rdn != null && Utils.Compare(rdn, 0, 3, "CN=", 0, 3) == 0);
                        siteName = rdn.Substring(3);
                    }

                    // it is an existing subnet object
                    ActiveDirectorySubnet subnet = null;
                    if (siteName == null)
                    {
                        subnet = new ActiveDirectorySubnet(context, subnetName, null, true);
                    }
                    else
                    {
                        subnet = new ActiveDirectorySubnet(context, subnetName, siteName, true);
                    }

                    subnet.cachedEntry = connectionEntry;
                    return(subnet);
                }
            }
            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.DSNotFound), typeof(ActiveDirectorySubnet), subnetName);
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }
            finally
            {
                if (de != null)
                {
                    de.Dispose();
                }
            }
        }
Esempio n. 37
0
		public static ActiveDirectorySite FindByName(DirectoryContext context, string siteName)
		{
			DirectoryEntry directoryEntry;
			ActiveDirectorySite activeDirectorySite;
			ActiveDirectorySite.ValidateArgument(context, siteName);
			context = new DirectoryContext(context);
			try
			{
				directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
				string str = string.Concat("CN=Sites,", (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext));
				directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, str);
			}
			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));
			}
			try
			{
				try
				{
					string[] strArrays = new string[1];
					strArrays[0] = "distinguishedName";
					ADSearcher aDSearcher = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=site)(objectCategory=site)(name=", Utils.GetEscapedFilterValue(siteName), "))"), strArrays, SearchScope.OneLevel, false, false);
					SearchResult searchResult = aDSearcher.FindOne();
					if (searchResult != null)
					{
						ActiveDirectorySite activeDirectorySite1 = new ActiveDirectorySite(context, siteName, true);
						activeDirectorySite = activeDirectorySite1;
					}
					else
					{
						throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySite), siteName);
					}
				}
				catch (COMException cOMException3)
				{
					COMException cOMException2 = cOMException3;
					if (cOMException2.ErrorCode != -2147016656)
					{
						throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException2);
					}
					else
					{
						throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySite), siteName);
					}
				}
			}
			finally
			{
				directoryEntry.Dispose();
			}
			return activeDirectorySite;
		}
Esempio n. 38
0
        //
        // This method searches in the schema container for all non-defunct properties of the 
        // specified name (ldapDisplayName).
        //
        private ArrayList GetProperties(ICollection ldapDisplayNames)
        {
            ArrayList properties = new ArrayList();
            SearchResultCollection resCol = null;

            try
            {
                if (ldapDisplayNames.Count < 1)
                {
                    return properties;
                }

                if (_schemaEntry == null)
                {
                    _schemaEntry = DirectoryEntryManager.GetDirectoryEntry(_context, WellKnownDN.SchemaNamingContext);
                }

                // constructing the filter
                StringBuilder str = new StringBuilder(100);

                if (ldapDisplayNames.Count > 1)
                {
                    str.Append("(|");
                }
                foreach (string ldapDisplayName in ldapDisplayNames)
                {
                    str.Append("(");
                    str.Append(PropertyManager.LdapDisplayName);
                    str.Append("=");
                    str.Append(Utils.GetEscapedFilterValue(ldapDisplayName));
                    str.Append(")");
                }
                if (ldapDisplayNames.Count > 1)
                {
                    str.Append(")");
                }

                string filter = "(&(" + PropertyManager.ObjectCategory + "=attributeSchema)" + str.ToString() + "(!(" + PropertyManager.IsDefunct + "=TRUE)))";

                string[] propertiesToLoad = new String[1];
                propertiesToLoad[0] = PropertyManager.LdapDisplayName;

                ADSearcher searcher = new ADSearcher(_schemaEntry, filter, propertiesToLoad, SearchScope.OneLevel);
                resCol = searcher.FindAll();

                foreach (SearchResult res in resCol)
                {
                    string ldapDisplayName = (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.LdapDisplayName);
                    DirectoryEntry de = res.GetDirectoryEntry();

                    de.AuthenticationType = Utils.DefaultAuthType;
                    de.Username = _context.UserName;
                    de.Password = _context.Password;

                    ActiveDirectorySchemaProperty schemaProperty = new ActiveDirectorySchemaProperty(_context, ldapDisplayName, de, _schemaEntry);

                    properties.Add(schemaProperty);
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(_context, e);
            }
            finally
            {
                if (resCol != null)
                {
                    resCol.Dispose();
                }
            }

            return properties;
        }
Esempio n. 39
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;
		}
        internal static SearchResult GetPropertiesFromSchemaContainer(DirectoryContext context, DirectoryEntry schemaEntry, string name, bool isDefunctOnServer)
        {
            SearchResult  result  = null;
            StringBuilder builder = new StringBuilder(15);

            builder.Append("(&(");
            builder.Append(PropertyManager.ObjectCategory);
            builder.Append("=attributeSchema)");
            builder.Append("(");
            if (!isDefunctOnServer)
            {
                builder.Append(PropertyManager.LdapDisplayName);
            }
            else
            {
                builder.Append(PropertyManager.Cn);
            }
            builder.Append("=");
            builder.Append(Utils.GetEscapedFilterValue(name));
            builder.Append(")");
            if (!isDefunctOnServer)
            {
                builder.Append("(!(");
            }
            else
            {
                builder.Append("(");
            }
            builder.Append(PropertyManager.IsDefunct);
            if (!isDefunctOnServer)
            {
                builder.Append("=TRUE)))");
            }
            else
            {
                builder.Append("=TRUE))");
            }
            string[] propertiesToLoad = null;
            if (!isDefunctOnServer)
            {
                propertiesToLoad = new string[] { PropertyManager.DistinguishedName, PropertyManager.Cn, PropertyManager.AttributeSyntax, PropertyManager.OMSyntax, PropertyManager.OMObjectClass, PropertyManager.Description, PropertyManager.SearchFlags, PropertyManager.IsMemberOfPartialAttributeSet, PropertyManager.LinkID, PropertyManager.SchemaIDGuid, PropertyManager.RangeLower, PropertyManager.RangeUpper };
            }
            else
            {
                propertiesToLoad = new string[] { PropertyManager.DistinguishedName, PropertyManager.Cn, PropertyManager.AttributeSyntax, PropertyManager.OMSyntax, PropertyManager.OMObjectClass, PropertyManager.Description, PropertyManager.SearchFlags, PropertyManager.IsMemberOfPartialAttributeSet, PropertyManager.LinkID, PropertyManager.SchemaIDGuid, PropertyManager.AttributeID, PropertyManager.IsSingleValued, PropertyManager.RangeLower, PropertyManager.RangeUpper, PropertyManager.LdapDisplayName };
            }
            ADSearcher searcher = new ADSearcher(schemaEntry, builder.ToString(), propertiesToLoad, SearchScope.OneLevel, false, false);

            try
            {
                result = searcher.FindOne();
            }
            catch (COMException exception)
            {
                if (exception.ErrorCode == -2147016656)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySchemaProperty), name);
                }
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
            }
            if (result == null)
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySchemaProperty), name);
            }
            return(result);
        }
Esempio n. 41
0
		internal static AdamInstance FindAnyAdamInstance(DirectoryContext context)
		{
			if (context.ContextType == DirectoryContextType.ConfigurationSet)
			{
				DirectoryEntry searchRootEntry = ConfigurationSet.GetSearchRootEntry(Forest.GetCurrentForest());
				ArrayList arrayLists = new ArrayList();
				try
				{
					try
					{
						StringBuilder stringBuilder = new StringBuilder(15);
						stringBuilder.Append("(&(");
						stringBuilder.Append(PropertyManager.ObjectCategory);
						stringBuilder.Append("=serviceConnectionPoint)");
						stringBuilder.Append("(");
						stringBuilder.Append(PropertyManager.Keywords);
						stringBuilder.Append("=1.2.840.113556.1.4.1851)(");
						stringBuilder.Append(PropertyManager.Keywords);
						stringBuilder.Append("=");
						stringBuilder.Append(Utils.GetEscapedFilterValue(context.Name));
						stringBuilder.Append("))");
						string str = stringBuilder.ToString();
						string[] serviceBindingInformation = new string[1];
						serviceBindingInformation[0] = PropertyManager.ServiceBindingInformation;
						ADSearcher aDSearcher = new ADSearcher(searchRootEntry, str, serviceBindingInformation, SearchScope.Subtree, false, false);
						SearchResultCollection searchResultCollections = aDSearcher.FindAll();
						try
						{
							foreach (SearchResult item in searchResultCollections)
							{
								string str1 = "ldap://";
								IEnumerator enumerator = item.Properties[PropertyManager.ServiceBindingInformation].GetEnumerator();
								try
								{
									while (enumerator.MoveNext())
									{
										string str2 = item.ToString ();
										if (str2.Length <= str1.Length || string.Compare(str2.Substring(0, str1.Length), str1, StringComparison.OrdinalIgnoreCase) != 0)
										{
											continue;
										}
										arrayLists.Add(str2.Substring(str1.Length));
									}
								}
								finally
								{
									IDisposable disposable = enumerator as IDisposable;
									if (disposable != null)
									{
										disposable.Dispose();
									}
								}
							}
						}
						finally
						{
							searchResultCollections.Dispose();
						}
					}
					catch (COMException cOMException1)
					{
						COMException cOMException = cOMException1;
						throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
					}
				}
				finally
				{
					searchRootEntry.Dispose();
				}
				return ConfigurationSet.FindAliveAdamInstance(null, context, arrayLists);
			}
			else
			{
				DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(context);
				DirectoryEntry cachedDirectoryEntry = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
				if (Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
				{
					string propertyValue = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.DnsHostName);
					return new AdamInstance(context, propertyValue, directoryEntryManager);
				}
				else
				{
					directoryEntryManager.RemoveIfExists(directoryEntryManager.ExpandWellKnownDN(WellKnownDN.RootDSE));
					throw new ArgumentException(Res.GetString("TargetShouldBeServerORConfigSet"), "context");
				}
			}
		}
 internal static AdamInstance FindAnyAdamInstance(DirectoryContext context)
 {
     if (context.ContextType != DirectoryContextType.ConfigurationSet)
     {
         DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
         DirectoryEntry cachedDirectoryEntry = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
         if (!Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
         {
             directoryEntryMgr.RemoveIfExists(directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDSE));
             throw new ArgumentException(Res.GetString("TargetShouldBeServerORConfigSet"), "context");
         }
         return new AdamInstance(context, (string) PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.DnsHostName), directoryEntryMgr);
     }
     DirectoryEntry searchRootEntry = GetSearchRootEntry(Forest.GetCurrentForest());
     ArrayList adamInstanceNames = new ArrayList();
     try
     {
         string text1 = (string) searchRootEntry.Properties["distinguishedName"].Value;
         StringBuilder builder = new StringBuilder(15);
         builder.Append("(&(");
         builder.Append(PropertyManager.ObjectCategory);
         builder.Append("=serviceConnectionPoint)");
         builder.Append("(");
         builder.Append(PropertyManager.Keywords);
         builder.Append("=1.2.840.113556.1.4.1851)(");
         builder.Append(PropertyManager.Keywords);
         builder.Append("=");
         builder.Append(Utils.GetEscapedFilterValue(context.Name));
         builder.Append("))");
         string filter = builder.ToString();
         string[] propertiesToLoad = new string[] { PropertyManager.ServiceBindingInformation };
         ADSearcher searcher = new ADSearcher(searchRootEntry, filter, propertiesToLoad, SearchScope.Subtree, false, false);
         using (SearchResultCollection results = searcher.FindAll())
         {
             foreach (SearchResult result in results)
             {
                 string strB = "ldap://";
                 foreach (string str4 in result.Properties[PropertyManager.ServiceBindingInformation])
                 {
                     if ((str4.Length > strB.Length) && (string.Compare(str4.Substring(0, strB.Length), strB, StringComparison.OrdinalIgnoreCase) == 0))
                     {
                         adamInstanceNames.Add(str4.Substring(strB.Length));
                     }
                 }
             }
         }
     }
     catch (COMException exception)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
     }
     finally
     {
         searchRootEntry.Dispose();
     }
     return FindAliveAdamInstance(null, context, adamInstanceNames);
 }
Esempio n. 43
0
		private ArrayList GetSites ()
		{
			ArrayList arrayLists = new ArrayList ();



			try {
				DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry (this.context, this.directoryEntryMgr.ExpandWellKnownDN (WellKnownDN.SitesContainer));
				
				ADSearcher aDSearcher = new ADSearcher (directoryEntry, "(&(objectClass=site))", new string[] { "cn" }, SearchScope.OneLevel);
				SearchResultCollection searchResultCollections = null;
				searchResultCollections = aDSearcher.FindAll ();
				foreach(SearchResult result in searchResultCollections)
				{
					var entry = result.GetDirectoryEntry ();
					var site = new System.DirectoryServices.ActiveDirectory.ActiveDirectorySite(this.context, (string)entry.Properties["cn"].Value, true);
					arrayLists.Add (site);
				}
			} catch (Exception ex) 
			{
				var msg = ex.Message;
			}


			/*
			IntPtr zero = IntPtr.Zero;
			IntPtr intPtr = IntPtr.Zero;
			IntPtr zero1 = IntPtr.Zero;
			try
			{
				this.GetDSHandle(out zero, out intPtr);
				IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsListSitesW");
				if (procAddress != (IntPtr)0)
				{
					NativeMethods.DsListSites delegateForFunctionPointer = (NativeMethods.DsListSites)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(NativeMethods.DsListSites));
					int num = delegateForFunctionPointer(zero, out zero1);
					if (num != 0)
					{
						throw ExceptionHelper.GetExceptionFromErrorCode(num, this.context.GetServerName());
					}
					else
					{
						try
						{
							DsNameResult dsNameResult = new DsNameResult();
							Marshal.PtrToStructure(zero1, dsNameResult);
							IntPtr intPtr1 = dsNameResult.items;
							for (int i = 0; i < dsNameResult.itemCount; i++)
							{
								DsNameResultItem dsNameResultItem = new DsNameResultItem();
								Marshal.PtrToStructure(intPtr1, dsNameResultItem);
								if (dsNameResultItem.status == 0)
								{
									string value = Utils.GetDNComponents(dsNameResultItem.name)[0].Value;
									arrayLists.Add(new ActiveDirectorySite(this.context, value, true));
								}
								intPtr1 = (IntPtr)((long)intPtr1 + (long)Marshal.SizeOf(dsNameResultItem));
							}
						}
						finally
						{
							if (zero1 != IntPtr.Zero)
							{
								procAddress = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeNameResultW");
								if (procAddress != (IntPtr)0)
								{
									UnsafeNativeMethods.DsFreeNameResultW dsFreeNameResultW = (UnsafeNativeMethods.DsFreeNameResultW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsFreeNameResultW));
									dsFreeNameResultW(zero1);
								}
								else
								{
									throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
								}
							}
						}
					}
				}
				else
				{
					throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
				}
			}
			finally
			{
				if (zero != (IntPtr)0)
				{
					Utils.FreeDSHandle(zero, DirectoryContext.ADHandle);
				}
				if (intPtr != (IntPtr)0)
				{
					Utils.FreeAuthIdentity(intPtr, DirectoryContext.ADHandle);
				}
			}
			*/
			return arrayLists;
		}
Esempio n. 44
0
        public static ActiveDirectorySiteLink FindByName(DirectoryContext context, string siteLinkName, ActiveDirectoryTransportType transport)
        {
            DirectoryEntry          directoryEntry;
            ActiveDirectorySiteLink activeDirectorySiteLink;

            ActiveDirectorySiteLink.ValidateArgument(context, siteLinkName, transport);
            context = new DirectoryContext(context);
            try
            {
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                string propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext);
                string str           = string.Concat("CN=Inter-Site Transports,CN=Sites,", propertyValue);
                if (transport != ActiveDirectoryTransportType.Rpc)
                {
                    str = string.Concat("CN=SMTP,", str);
                }
                else
                {
                    str = string.Concat("CN=IP,", str);
                }
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, str);
            }
            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));
            }
            try
            {
                try
                {
                    string[] strArrays = new string[1];
                    strArrays[0] = "distinguishedName";
                    ADSearcher   aDSearcher   = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=siteLink)(objectCategory=SiteLink)(name=", Utils.GetEscapedFilterValue(siteLinkName), "))"), strArrays, SearchScope.OneLevel, false, false);
                    SearchResult searchResult = aDSearcher.FindOne();
                    if (searchResult != null)
                    {
                        DirectoryEntry          directoryEntry1          = searchResult.GetDirectoryEntry();
                        ActiveDirectorySiteLink activeDirectorySiteLink1 = new ActiveDirectorySiteLink(context, siteLinkName, transport, true, directoryEntry1);
                        activeDirectorySiteLink = activeDirectorySiteLink1;
                    }
                    else
                    {
                        Exception exception = new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySiteLink), siteLinkName);
                        throw exception;
                    }
                }
                catch (COMException cOMException3)
                {
                    COMException cOMException2 = cOMException3;
                    if (cOMException2.ErrorCode != -2147016656)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException2);
                    }
                    else
                    {
                        DirectoryEntry directoryEntry2 = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                        if (!Utils.CheckCapability(directoryEntry2, Capability.ActiveDirectoryApplicationMode) || transport != ActiveDirectoryTransportType.Smtp)
                        {
                            throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySiteLink), siteLinkName);
                        }
                        else
                        {
                            throw new NotSupportedException(Res.GetString("NotSupportTransportSMTP"));
                        }
                    }
                }
            }
            finally
            {
                directoryEntry.Dispose();
            }
            return(activeDirectorySiteLink);
        }
Esempio n. 45
0
		private ArrayList GetDomains()
		{
			ArrayList arrayLists = new ArrayList();
			var dn = this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer);
			DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, dn);
			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.804:=");
			stringBuilder.Append(2);
			stringBuilder.Append("))");
			string str = stringBuilder.ToString();
			string[] dnsRoot = new string[1];
			dnsRoot[0] = PropertyManager.DnsRoot;
			ADSearcher aDSearcher = new ADSearcher(directoryEntry, str, dnsRoot, SearchScope.OneLevel);
			SearchResultCollection searchResultCollections = null;
			try
			{
				try
				{
					searchResultCollections = aDSearcher.FindAll();
					foreach (SearchResult searchResult in searchResultCollections)
					{
						string searchResultPropertyValue = (string)PropertyManager.GetSearchResultPropertyValue(searchResult, PropertyManager.DnsRoot);
						DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(searchResultPropertyValue, DirectoryContextType.Domain, this.context);
						arrayLists.Add(new Domain(newDirectoryContext, searchResultPropertyValue));
					}
				}
				catch (COMException cOMException1)
				{
					COMException cOMException = cOMException1;
					throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException);
				}
			}
			finally
			{
				if (searchResultCollections != null)
				{
					searchResultCollections.Dispose();
				}
				directoryEntry.Dispose();
			}
			return arrayLists;
		}
Esempio n. 46
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;
        }
Esempio n. 47
0
		public static ActiveDirectorySiteLink FindByName(DirectoryContext context, string siteLinkName, ActiveDirectoryTransportType transport)
		{
			DirectoryEntry directoryEntry;
			ActiveDirectorySiteLink activeDirectorySiteLink;
			ActiveDirectorySiteLink.ValidateArgument(context, siteLinkName, transport);
			context = new DirectoryContext(context);
			try
			{
				directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
				string propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext);
				string str = string.Concat("CN=Inter-Site Transports,CN=Sites,", propertyValue);
				if (transport != ActiveDirectoryTransportType.Rpc)
				{
					str = string.Concat("CN=SMTP,", str);
				}
				else
				{
					str = string.Concat("CN=IP,", str);
				}
				directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, str);
			}
			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));
			}
			try
			{
				try
				{
					string[] strArrays = new string[1];
					strArrays[0] = "distinguishedName";
					ADSearcher aDSearcher = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=siteLink)(objectCategory=SiteLink)(name=", Utils.GetEscapedFilterValue(siteLinkName), "))"), strArrays, SearchScope.OneLevel, false, false);
					SearchResult searchResult = aDSearcher.FindOne();
					if (searchResult != null)
					{
						DirectoryEntry directoryEntry1 = searchResult.GetDirectoryEntry();
						ActiveDirectorySiteLink activeDirectorySiteLink1 = new ActiveDirectorySiteLink(context, siteLinkName, transport, true, directoryEntry1);
						activeDirectorySiteLink = activeDirectorySiteLink1;
					}
					else
					{
						Exception exception = new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySiteLink), siteLinkName);
						throw exception;
					}
				}
				catch (COMException cOMException3)
				{
					COMException cOMException2 = cOMException3;
					if (cOMException2.ErrorCode != -2147016656)
					{
						throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException2);
					}
					else
					{
						DirectoryEntry directoryEntry2 = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
						if (!Utils.CheckCapability(directoryEntry2, Capability.ActiveDirectoryApplicationMode) || transport != ActiveDirectoryTransportType.Smtp)
						{
							throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySiteLink), siteLinkName);
						}
						else
						{
							throw new NotSupportedException(Res.GetString("NotSupportTransportSMTP"));
						}
					}
				}
			}
			finally
			{
				directoryEntry.Dispose();
			}
			return activeDirectorySiteLink;
		}
Esempio n. 48
0
        private void LoadCrossRefAttributes()
        {
            DirectoryEntry partitionsEntry = null;
            try
            {
                partitionsEntry = DirectoryEntryManager.GetDirectoryEntry(context, directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer));

                // now within the partitions container search for the 
                // crossRef object that has it's "dnsRoot" attribute equal to the 
                // dns name of the current domain

                // 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.804:=");
                str.Append((int)SystemFlag.SystemFlagNtdsDomain);
                str.Append(")(");
                str.Append(PropertyManager.DnsRoot);
                str.Append("=");
                str.Append(Utils.GetEscapedFilterValue(partitionName));
                str.Append("))");

                string filter = str.ToString();
                string[] propertiesToLoad = new string[2];

                propertiesToLoad[0] = PropertyManager.DistinguishedName;
                propertiesToLoad[1] = PropertyManager.TrustParent;

                ADSearcher searcher = new ADSearcher(partitionsEntry, filter, propertiesToLoad, SearchScope.OneLevel, false /*not paged search*/, false /*no cached results*/);
                SearchResult res = searcher.FindOne();

                _crossRefDN = (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.DistinguishedName);

                // "trustParent" attribute may not be set
                if (res.Properties[PropertyManager.TrustParent].Count > 0)
                {
                    _trustParent = (string)res.Properties[PropertyManager.TrustParent][0];
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                if (partitionsEntry != null)
                {
                    partitionsEntry.Dispose();
                }
            }
        }
 public static ActiveDirectorySiteLink FindByName(DirectoryContext context, string siteLinkName, ActiveDirectoryTransportType transport)
 {
     DirectoryEntry directoryEntry;
     ActiveDirectorySiteLink link2;
     ValidateArgument(context, siteLinkName, transport);
     context = new DirectoryContext(context);
     try
     {
         directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
         string str = (string) PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext);
         string dn = "CN=Inter-Site Transports,CN=Sites," + str;
         if (transport == ActiveDirectoryTransportType.Rpc)
         {
             dn = "CN=IP," + dn;
         }
         else
         {
             dn = "CN=SMTP," + dn;
         }
         directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, dn);
     }
     catch (COMException exception)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
     }
     catch (ActiveDirectoryObjectNotFoundException)
     {
         throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { context.Name }));
     }
     try
     {
         SearchResult result = new ADSearcher(directoryEntry, "(&(objectClass=siteLink)(objectCategory=SiteLink)(name=" + Utils.GetEscapedFilterValue(siteLinkName) + "))", new string[] { "distinguishedName" }, SearchScope.OneLevel, false, false).FindOne();
         if (result == null)
         {
             Exception exception2 = new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySiteLink), siteLinkName);
             throw exception2;
         }
         DirectoryEntry entry = result.GetDirectoryEntry();
         link2 = new ActiveDirectorySiteLink(context, siteLinkName, transport, true, entry);
     }
     catch (COMException exception3)
     {
         if (exception3.ErrorCode != -2147016656)
         {
             throw ExceptionHelper.GetExceptionFromCOMException(context, exception3);
         }
         if (Utils.CheckCapability(DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE), Capability.ActiveDirectoryApplicationMode) && (transport == ActiveDirectoryTransportType.Smtp))
         {
             throw new NotSupportedException(Res.GetString("NotSupportTransportSMTP"));
         }
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySiteLink), siteLinkName);
     }
     finally
     {
         directoryEntry.Dispose();
     }
     return link2;
 }
Esempio n. 50
0
        private ArrayList GetChildDomains()
        {
            ArrayList childDomains = new ArrayList();

            if (_crossRefDN == null)
            {
                LoadCrossRefAttributes();
            }

            DirectoryEntry partitionsEntry = null;
            SearchResultCollection resCol = null;
            try
            {
                partitionsEntry = DirectoryEntryManager.GetDirectoryEntry(context, directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer));
                // search for all the "crossRef" objects that have the 
                // ADS_SYSTEMFLAG_CR_NTDS_NC and SYSTEMFLAG_CR_NTDS_DOMAIN flags set
                // (one-level search is good enough)

                // setup the directory searcher object

                // 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.804:=");
                str.Append((int)SystemFlag.SystemFlagNtdsDomain);
                str.Append(")(");
                str.Append(PropertyManager.TrustParent);
                str.Append("=");
                str.Append(Utils.GetEscapedFilterValue(_crossRefDN));
                str.Append("))");

                string filter = str.ToString();
                string[] propertiesToLoad = new string[1];
                propertiesToLoad[0] = PropertyManager.DnsRoot;

                ADSearcher searcher = new ADSearcher(partitionsEntry, filter, propertiesToLoad, SearchScope.OneLevel);
                resCol = searcher.FindAll();

                foreach (SearchResult res in resCol)
                {
                    string childDomainName = (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.DnsRoot);
                    DirectoryContext childContext = Utils.GetNewDirectoryContext(childDomainName, DirectoryContextType.Domain, context);
                    childDomains.Add(new Domain(childContext, childDomainName));
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                if (resCol != null)
                {
                    resCol.Dispose();
                }
                if (partitionsEntry != null)
                {
                    partitionsEntry.Dispose();
                }
            }
            return childDomains;
        }
 private ArrayList GetProperties(ICollection ldapDisplayNames)
 {
     ArrayList list = new ArrayList();
     SearchResultCollection results = null;
     try
     {
         if (ldapDisplayNames.Count >= 1)
         {
             if (this.schemaEntry == null)
             {
                 this.schemaEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, WellKnownDN.SchemaNamingContext);
             }
             StringBuilder builder = new StringBuilder(100);
             if (ldapDisplayNames.Count > 1)
             {
                 builder.Append("(|");
             }
             foreach (string str in ldapDisplayNames)
             {
                 builder.Append("(");
                 builder.Append(PropertyManager.LdapDisplayName);
                 builder.Append("=");
                 builder.Append(Utils.GetEscapedFilterValue(str));
                 builder.Append(")");
             }
             if (ldapDisplayNames.Count > 1)
             {
                 builder.Append(")");
             }
             string filter = "(&(" + PropertyManager.ObjectCategory + "=attributeSchema)" + builder.ToString() + "(!(" + PropertyManager.IsDefunct + "=TRUE)))";
             string[] propertiesToLoad = new string[] { PropertyManager.LdapDisplayName };
             results = new ADSearcher(this.schemaEntry, filter, propertiesToLoad, SearchScope.OneLevel).FindAll();
             foreach (SearchResult result in results)
             {
                 string searchResultPropertyValue = (string) PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.LdapDisplayName);
                 DirectoryEntry directoryEntry = result.GetDirectoryEntry();
                 directoryEntry.AuthenticationType = Utils.DefaultAuthType;
                 directoryEntry.Username = this.context.UserName;
                 directoryEntry.Password = this.context.Password;
                 ActiveDirectorySchemaProperty property = new ActiveDirectorySchemaProperty(this.context, searchResultPropertyValue, directoryEntry, this.schemaEntry);
                 list.Add(property);
             }
         }
         return list;
     }
     catch (COMException exception)
     {
         throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception);
     }
     finally
     {
         if (results != null)
         {
             results.Dispose();
         }
     }
     return list;
 }
        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));
        }