Exemplo n.º 1
0
        public TrustRelationshipInformationCollection GetAllTrustRelationships()
        {
            if (trustCollection == null)
            {
                try
                {
                    DirectoryEntry rootDse = new DirectoryEntry(string.Format("LDAP://{0}/RootDSE", dName), dc.UserName, dc.Password);

                    string defaultName = rootDse.DirContext.DefaultNamingContext;

                    if (defaultName == null || defaultName == "")
                    {
                        trustCollection = null;
                        return(trustCollection);
                    }

                    DirectoryEntry sys = new DirectoryEntry(string.Format("LDAP://{0}/CN=System,{1}", SDSUtils.DNToDomainName(defaultName), defaultName), dc.UserName, dc.Password);

                    DirectorySearcher ds = new DirectorySearcher(sys);
                    ds.Filter      = "(objectClass=trustedDomain)";
                    ds.SearchScope = SearchScope.Subtree;

                    SearchResultCollection src = ds.FindAll();

                    if (src != null && src.Count > 0)
                    {
                        trustCollection = new TrustRelationshipInformationCollection();

                        foreach (SearchResult sr in src)
                        {
                            string sProtocol, sServer, sCNs, sDCs;
                            SDSUtils.CrackPath(sr.Path, out sProtocol, out sServer, out sCNs, out sDCs);

                            /*Console.WriteLine("sProtocol " + sProtocol);
                             * Console.WriteLine("sServer " + sServer);
                             * Console.WriteLine("sCNs " + sCNs);
                             * Console.WriteLine("sDCs " + sDCs);*/

                            string         sourcename, targetname;
                            TrustDirection trustdirection;
                            TrustType      trusttype = TrustType.Unknown;

                            DirectoryEntry trustEntry = new DirectoryEntry(sr.Path, dc.UserName, dc.Password);

                            int trustdir = (int)trustEntry.Properties["trustDirection"].Value;

                            string   trustDn = trustEntry.Properties["distinguishedName"].Value.ToString();
                            string[] splits  = trustDn.Split(',');
                            trustDn = splits[0].Substring(3);

                            int trustattr = (int)trustEntry.Properties["trustAttributes"].Value;

                            int trusttp = (int)trustEntry.Properties["trustType"].Value;

                            //Note:the following implementation of how to determine the TrustType is still under investigation
                            if (trusttp == (int)ADTrustType.TYPE_UPLEVEL) //windows 2003 trust
                            {
                                switch (trustattr)
                                {
                                case 0:
                                    trusttype = TrustType.External;     //this trust is non-transitive
                                    break;

                                case 1:       //ATTRIBUTES_NON_TRANSITIVE
                                    break;

                                case 2:     //ATTRIBUTES_UPLEVEL_ONLY
                                    break;

                                case 4:     //ATTRIBUTES_QUARANTINED_DOMAIN
                                    trusttype = TrustType.External;
                                    break;

                                case 8:                           //ATTRIBUTES_FOREST_TRANSITIVE
                                    trusttype = TrustType.Forest; //and this trust is transitive
                                    break;

                                case 16:     //ATTRIBUTES_CROSS_ORGANIZATION
                                    trusttype = TrustType.CrossLink;
                                    break;

                                case 32:    //ATTRIBUTES_WITHIN_FOREST
                                    if (trustDn.ToLower().Contains(dName.ToLower()))
                                    {
                                        trusttype = TrustType.ParentChild;
                                    }
                                    else
                                    {
                                        trusttype = TrustType.External;      //this trust is non-transitive
                                    }
                                    break;

                                case 64:     //ATTRIBUTES_TREAT_AS_EXTERNAL
                                    trusttype = TrustType.External;
                                    break;

                                default:
                                    trusttype = TrustType.Unknown;
                                    break;
                                }
                            }
                            else if (trusttp == (int)ADTrustType.TYPE_MIT)
                            {
                                trusttype = TrustType.Kerberos;
                            }

                            switch (trustdir)
                            {
                            case 1:
                                trustdirection = TrustDirection.Inbound;
                                sourcename     = dName;
                                targetname     = trustDn;
                                break;

                            case 2:
                                trustdirection = TrustDirection.Outbound;
                                sourcename     = trustDn;
                                targetname     = dName;
                                break;

                            case 3:
                                trustdirection = TrustDirection.Bidirectional;
                                sourcename     = dName;
                                targetname     = trustDn;
                                break;

                            default:
                                trustdirection = TrustDirection.Disabled;
                                sourcename     = targetname = "";
                                break;
                            }

                            TrustRelationshipInformation trustinfo = new TrustRelationshipInformation(sourcename, targetname, trusttype, trustdirection);
                            trustCollection.Add(trustinfo);
                        }
                    }
                }
                catch
                {
                    return(null);
                }
            }

            return(trustCollection);
        }
Exemplo n.º 2
0
        private void FindChildrenDomains()
        {
            DirectoryEntry rootDse = new DirectoryEntry(string.Format("LDAP://{0}/RootDSE", dName), dc.UserName, dc.Password);

            string configureName = rootDse.DirContext.ConfigurationNamingContext;

            if (configureName == null || configureName == "")
            {
                return;
            }

            DirectoryEntry sys = new DirectoryEntry(string.Format("LDAP://{0}/CN=Partitions,{1}", SDSUtils.DNToDomainName(configureName), configureName), dc.UserName, dc.Password);

            DirectorySearcher ds = new DirectorySearcher(sys);

            ds.Filter      = "(objectClass=crossRef)";
            ds.SearchScope = SearchScope.OneLevel;

            SearchResultCollection src = ds.FindAll();

            if (src != null && src.Count > 0)
            {
                foreach (SearchResult sr in src)
                {
                    string sProtocol, sServer, sCNs, sDCs;
                    SDSUtils.CrackPath(sr.Path, out sProtocol, out sServer, out sCNs, out sDCs);

                    DirectoryEntry partEntry = new DirectoryEntry(sr.Path, dc.UserName, dc.Password);

                    string parentDomainDN = partEntry.Properties["trustParent"].Value as string;

                    if (parentDomainDN != null && parentDomainDN != "" &&
                        dName.Equals(SDSUtils.DNToDomainName(parentDomainDN), StringComparison.InvariantCultureIgnoreCase))
                    {
                        children.Add(new Domain(SDSUtils.DNToDomainName(partEntry.Properties["nCName"].Value as string)));
                    }
                }
            }

            return;
        }