/// <summary>
        /// Get all domains in the forest.
        /// </summary>
        /// <param name="dc">The DC in the forest.</param>
        /// <returns>All domain objects in the forest.</returns>
        public DsDomain[] ListDomains(DsServer dc)
        {
            RootDSE rootDse     = LdapUtility.GetRootDSE(dc);
            string  partitionDn = "CN=Partitions," + rootDse.configurationNamingContext;
            SearchResultEntryCollection results = null;

            ResultCode re = LdapUtility.Search(
                dc,
                partitionDn,
                "(&(objectClass=crossRef)(systemFlags:1.2.840.113556.1.4.804:=2))",
                System.DirectoryServices.Protocols.SearchScope.Subtree,
                new string[] { "nCName" },
                out results);

            if (re != ResultCode.Success)
            {
                return(null);
            }

            List <DsDomain> domains = new List <DsDomain>();

            foreach (SearchResultEntry e in results)
            {
                DirectoryAttribute attr   = e.Attributes["nCName"];
                string             dn     = (string)attr[0];
                DsDomain           domain = new AddsDomain();
                domain.Name = dn;
                domains.Add(domain);
            }

            return(domains.ToArray());
        }
        /// <summary>
        /// init objects for DomainStore
        /// </summary>
        static void InitDomainObjects()
        {
            #region domain objects

            DsDomain primaryDomain = ldapAd.GetDomainInfo(
                ADCommonServerAdapter.Instance(testSite).PDCNetbiosName + "." + ADCommonServerAdapter.Instance(testSite).PrimaryDomainDnsName + (testDS == true ? "" : ":" + testSite.Properties[Machine.WritableDC1.ToString() + ".LDSPort"]),
                UserStore[User.ParentDomainAdmin]
                );
            DomainStore.Add(DomainEnum.PrimaryDomain, primaryDomain);

            UserStore[User.ParentDomainAdmin].Domain = primaryDomain;

            //Should initialize child domain and trust domain object here
            if (testDS)
            {
                DsDomain InvalidDomain = new AddsDomain();
                InvalidDomain.FsmoRoleOwners = new Dictionary <FSMORoles, string>();
                InvalidDomain.DNSName        = testSite.Properties[DomainEnum.InvalidDomain.ToString() + ".DS.DNSName"];
                DomainStore.Add(DomainEnum.InvalidDomain, InvalidDomain);
            }
            else
            {
                DsDomain InvalidDomain = new AdldsDomain();
                InvalidDomain.FsmoRoleOwners = new Dictionary <FSMORoles, string>();
                InvalidDomain.DNSName        = testSite.Properties[DomainEnum.InvalidDomain.ToString() + ".LDS.DNSName"];
                DomainStore.Add(DomainEnum.InvalidDomain, InvalidDomain);
            }

            #endregion
        }
        public DsSite GetSite(DsServer dc, string dn)
        {
            DsSite site = new DsSite();

            site.DN = dn;

            // servers
            // find all "server" objects under the site dn
            SearchResultEntryCollection results = null;
            ResultCode ret = Search(
                dc,
                dn,
                "(objectClass=server)",
                System.DirectoryServices.Protocols.SearchScope.Subtree,
                null,
                out results
                );

            List <DsServer> servers = new List <DsServer>();

            foreach (SearchResultEntry e in results)
            {
                DsServer srv = new DsServer();
                srv.NtdsDsaObjectName = e.DistinguishedName;
                servers.Add(srv);
            }
            site.Servers = servers.ToArray();

            // Look into every NTDS DSA object to find the domain it is in.
            List <string> domainNcs = new List <string>();

            foreach (DsServer s in site.Servers)
            {
                string[] ncs = LdapUtility.GetAttributeValuesString(
                    dc,
                    s.NtdsDsaObjectName,
                    "hasMasterNCs",
                    "(objectClass=nTDSDSA)",
                    System.DirectoryServices.Protocols.SearchScope.Subtree);
                if (ncs == null)
                {
                    continue;
                }
                foreach (string nc in ncs)
                {
                    bool newNc = true;
                    foreach (string oldNc in domainNcs)
                    {
                        if (oldNc == nc)
                        {
                            newNc = false;
                            break;
                        }
                    }
                    if (newNc)
                    {
                        domainNcs.Add(nc);
                    }
                }
            }

            /*
             * string[] domainNcs = LdapUtility.GetAttributeValuesString(
             *  dc,
             *  site.DN,
             *  "hasMasterNCs", //"msDS-HasDomainNCs",
             *  "(objectClass=nTDSDSA)",
             *  System.DirectoryServices.Protocols.SearchScope.Subtree);
             */

            bool isAdlds = !EnvironmentConfig.TestDS;

            if (domainNcs == null && isAdlds)
            {
                return(site);
            }

            // Eliminate ConfigNC and SchemaNC
            List <string> filteredDomainNcs = new List <string>();

            foreach (string d in domainNcs)
            {
                if (d.StartsWith("CN=Configuration") || d.StartsWith("CN=Schema"))
                {
                    continue;
                }
                filteredDomainNcs.Add(d);
            }

            List <DsDomain> domains = new List <DsDomain>();

            foreach (string tdn in filteredDomainNcs)
            {
                bool n = true;
                foreach (DsDomain d in domains)
                {
                    if (d.Name == tdn)
                    {
                        n = false;
                        break;
                    }
                }

                if (n)
                {
                    DsDomain nd;
                    if (isAdlds)
                    {
                        nd = new AdldsDomain();
                    }
                    else
                    {
                        nd = new AddsDomain();
                    }
                    nd.Name = tdn;
                    domains.Add(nd);
                }
            }
            site.Domains = domains.ToArray();
            return(site);
        }