Beispiel #1
0
 public Domain()
     : base()
 {
     trustCollection = null;
     parent = null;
     children = new DomainCollection();
 }
Beispiel #2
0
        public ForestTrustRelationshipInformation GetTrustRelationship(string targetForestName)
        {
            CheckIfDisposed();

            if (targetForestName == null)
            {
                throw new ArgumentNullException(nameof(targetForestName));
            }

            if (targetForestName.Length == 0)
            {
                throw new ArgumentException(SR.EmptyStringParameter, nameof(targetForestName));
            }

            TrustRelationshipInformationCollection collection = GetTrustsHelper(targetForestName);

            if (collection.Count != 0)
            {
                Debug.Assert(collection.Count == 1);
                return((ForestTrustRelationshipInformation)collection[0]);
            }
            else
            {
                // trust relationship does not exist
                throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.ForestTrustDoesNotExist, Name, targetForestName), typeof(TrustRelationshipInformation), null);
            }
        }
Beispiel #3
0
 public Domain()
     : base()
 {
     trustCollection = null;
     parent          = null;
     children        = new DomainCollection();
 }
        public ForestTrustRelationshipInformation GetTrustRelationship(string targetForestName)
        {
            this.CheckIfDisposed();
            if (targetForestName == null)
            {
                throw new ArgumentNullException("targetForestName");
            }
            if (targetForestName.Length == 0)
            {
                throw new ArgumentException(Res.GetString("EmptyStringParameter"), "targetForestName");
            }
            TrustRelationshipInformationCollection trustsHelper = this.GetTrustsHelper(targetForestName);

            if (trustsHelper.Count == 0)
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestTrustDoesNotExist", new object[] { this.Name, targetForestName }), typeof(TrustRelationshipInformation), null);
            }
            return((ForestTrustRelationshipInformation)trustsHelper[0]);
        }
Beispiel #5
0
        public TrustRelationshipInformationCollection GetAllTrustRelationships()
        {
            TrustRelationshipInformationCollection trustcollection = new TrustRelationshipInformationCollection();

            DomainCollection dmc = this.Domains;

            if (dmc != null && dmc.Count > 0)
            {
                foreach (Domain dm in dmc)
                {
                    TrustRelationshipInformationCollection dmtrusts = dm.GetAllTrustRelationships();
                    if (dmtrusts != null && dmtrusts.Count > 0)
                    {
                        foreach (TrustRelationshipInformation trust in dmtrusts)
                        {
                            trustcollection.Add(trust);
                        }
                    }
                }
            }

            return(trustcollection);
        }
Beispiel #6
0
		public TrustRelationshipInformationCollection GetAllTrustRelationships()
		{
			base.CheckIfDisposed();
			ArrayList trustsHelper = this.GetTrustsHelper(null);
			TrustRelationshipInformationCollection trustRelationshipInformationCollection = new TrustRelationshipInformationCollection(this.context, base.Name, trustsHelper);
			return trustRelationshipInformationCollection;
		}
Beispiel #7
0
        public TrustRelationshipInformation GetTrustRelationship(string targetDomainName)
        {
            CheckIfDisposed();

            if (targetDomainName == null)
                throw new ArgumentNullException("targetDomainName");

            if (targetDomainName.Length == 0)
                throw new ArgumentException(Res.GetString(Res.EmptyStringParameter), "targetDomainName");

            ArrayList trusts = GetTrustsHelper(targetDomainName);
            TrustRelationshipInformationCollection collection = new TrustRelationshipInformationCollection(context, Name, trusts);
            if (collection.Count == 0)
            {
                // trust relationship does not exist
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DomainTrustDoesNotExist, Name, targetDomainName), typeof(TrustRelationshipInformation), null);
            }
            else
            {
                Debug.Assert(collection.Count == 1);
                return collection[0];
            }
        }
Beispiel #8
0
        public TrustRelationshipInformationCollection GetAllTrustRelationships()
        {
            CheckIfDisposed();

            ArrayList trusts = GetTrustsHelper(null);
            TrustRelationshipInformationCollection collection = new TrustRelationshipInformationCollection(context, Name, trusts);
            return collection;
        }
Beispiel #9
0
        private ArrayList GetTrustsHelper(string targetDomainName)
        {
            string serverName = null;
            IntPtr domains = (IntPtr)0;
            int count = 0;
            ArrayList unmanagedTrustList = new ArrayList();
            ArrayList tmpTrustList = new ArrayList();
            TrustRelationshipInformationCollection collection = new TrustRelationshipInformationCollection();
            int localDomainIndex = 0;
            string localDomainParent = null;
            int error = 0;
            bool impersonated = false;

            // first decide which server to go to
            if (context.isServer())
            {
                serverName = context.Name;
            }
            else
            {
                serverName = DomainController.FindOne(context).Name;
            }

            // impersonate appropriately
            impersonated = Utils.Impersonate(context);

            // call the DS API to get trust domain information
            try
            {
                try
                {
                    error = UnsafeNativeMethods.DsEnumerateDomainTrustsW(serverName, (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_IN_FOREST | (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_OUTBOUND | (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_INBOUND, out domains, out count);
                }
                finally
                {
                    if (impersonated)
                        Utils.Revert();
                }
            }
            catch { throw; }

            // check the result
            if (error != 0)
                throw ExceptionHelper.GetExceptionFromErrorCode(error, serverName);

            try
            {
                // now enumerate through the collection
                if (domains != (IntPtr)0 && count != 0)
                {
                    IntPtr addr = (IntPtr)0;
                    int j = 0;
                    for (int i = 0; i < count; i++)
                    {
                        // get the unmanaged trust object
                        addr = IntPtr.Add(domains, +i * Marshal.SizeOf(typeof(DS_DOMAIN_TRUSTS)));
                        DS_DOMAIN_TRUSTS unmanagedTrust = new DS_DOMAIN_TRUSTS();
                        Marshal.PtrToStructure(addr, unmanagedTrust);

                        unmanagedTrustList.Add(unmanagedTrust);
                    }

                    for (int i = 0; i < unmanagedTrustList.Count; i++)
                    {
                        DS_DOMAIN_TRUSTS unmanagedTrust = (DS_DOMAIN_TRUSTS)unmanagedTrustList[i];

                        // make sure this is the trust object that we want
                        if ((unmanagedTrust.Flags & (int)(DS_DOMAINTRUST_FLAG.DS_DOMAIN_PRIMARY | DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_OUTBOUND | DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_INBOUND)) == 0)
                        {
                            // Not interested in indirectly trusted domains.
                            continue;
                        }

                        // we don't want to have the NT4 trust to be returned
                        if (unmanagedTrust.TrustType == TrustHelper.TRUST_TYPE_DOWNLEVEL)
                            continue;

                        TrustObject obj = new TrustObject();
                        obj.TrustType = TrustType.Unknown;
                        if (unmanagedTrust.DnsDomainName != (IntPtr)0)
                            obj.DnsDomainName = Marshal.PtrToStringUni(unmanagedTrust.DnsDomainName);
                        if (unmanagedTrust.NetbiosDomainName != (IntPtr)0)
                            obj.NetbiosDomainName = Marshal.PtrToStringUni(unmanagedTrust.NetbiosDomainName);
                        obj.Flags = unmanagedTrust.Flags;
                        obj.TrustAttributes = unmanagedTrust.TrustAttributes;
                        obj.OriginalIndex = i;
                        obj.ParentIndex = unmanagedTrust.ParentIndex;

                        // check whether it is the case that we are only interested in the trust with target as specified
                        if (targetDomainName != null)
                        {
                            bool sameTarget = false;

                            // check whether it is the same target
                            if (obj.DnsDomainName != null && Utils.Compare(targetDomainName, obj.DnsDomainName) == 0)
                                sameTarget = true;
                            else if (obj.NetbiosDomainName != null && Utils.Compare(targetDomainName, obj.NetbiosDomainName) == 0)
                                sameTarget = true;

                            // we only want to need local domain and specified target domain trusts
                            if (!sameTarget && (obj.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_PRIMARY) == 0)
                                continue;
                        }

                        // local domain case
                        if ((obj.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_PRIMARY) != 0)
                        {
                            localDomainIndex = j;

                            // verify whether this is already the root
                            if ((obj.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_TREE_ROOT) == 0)
                            {
                                // get the parent domain name
                                DS_DOMAIN_TRUSTS parentTrust = (DS_DOMAIN_TRUSTS)unmanagedTrustList[obj.ParentIndex];
                                if (parentTrust.DnsDomainName != (IntPtr)0)
                                    localDomainParent = Marshal.PtrToStringUni(parentTrust.DnsDomainName);
                            }

                            // this is the trust type SELF
                            obj.TrustType = (TrustType)7;
                        }
                        // this is the case of MIT kerberos trust
                        else if (unmanagedTrust.TrustType == 3)
                        {
                            obj.TrustType = TrustType.Kerberos;
                        }

                        j++;
                        tmpTrustList.Add(obj);
                    }

                    // now determine the trust type
                    for (int i = 0; i < tmpTrustList.Count; i++)
                    {
                        TrustObject tmpObject = (TrustObject)tmpTrustList[i];
                        // local domain case, trust type has been determined
                        if (i == localDomainIndex)
                            continue;

                        if (tmpObject.TrustType == TrustType.Kerberos)
                            continue;

                        // parent domain
                        if (localDomainParent != null && Utils.Compare(localDomainParent, tmpObject.DnsDomainName) == 0)
                        {
                            tmpObject.TrustType = TrustType.ParentChild;
                            continue;
                        }

                        if ((tmpObject.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_IN_FOREST) != 0)
                        {
                            // child domain                                  
                            if (tmpObject.ParentIndex == ((TrustObject)tmpTrustList[localDomainIndex]).OriginalIndex)
                            {
                                tmpObject.TrustType = TrustType.ParentChild;
                            }
                            // tree root
                            else if ((tmpObject.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_TREE_ROOT) != 0 &&
                              (((TrustObject)tmpTrustList[localDomainIndex]).Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_TREE_ROOT) != 0)
                            {
                                string tmpForestName = null;
                                string rootDomainNC = directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDomainNamingContext);
                                tmpForestName = Utils.GetDnsNameFromDN(rootDomainNC);

                                // only if either the local domain or tmpObject is the tree root, will this trust relationship be a Root, otherwise it is cross link
                                DirectoryContext tmpContext = Utils.GetNewDirectoryContext(context.Name, DirectoryContextType.Forest, context);
                                if (tmpContext.isRootDomain() || Utils.Compare(tmpObject.DnsDomainName, tmpForestName) == 0)
                                {
                                    tmpObject.TrustType = TrustType.TreeRoot;
                                }
                                else
                                {
                                    tmpObject.TrustType = TrustType.CrossLink;
                                }
                            }
                            else
                            {
                                tmpObject.TrustType = TrustType.CrossLink;
                            }

                            continue;
                        }

                        // external trust or forest trust
                        if ((tmpObject.TrustAttributes & (int)TRUST_ATTRIBUTE.TRUST_ATTRIBUTE_FOREST_TRANSITIVE) != 0)
                        {
                            // should not happen as we specify DS_DOMAIN_IN_FOREST when enumerating the trust, so forest trust will not be returned
                            tmpObject.TrustType = TrustType.Forest;
                        }
                        else
                        {
                            tmpObject.TrustType = TrustType.External;
                        }
                    }
                }

                return tmpTrustList;
            }
            finally
            {
                if (domains != (IntPtr)0)
                    UnsafeNativeMethods.NetApiBufferFree(domains);
            }
        }
 private TrustRelationshipInformationCollection GetTrustsHelper(string targetForestName)
 {
     string serverName = null;
     TrustRelationshipInformationCollection informations2;
     IntPtr zero = IntPtr.Zero;
     int count = 0;
     TrustRelationshipInformationCollection informations = new TrustRelationshipInformationCollection();
     bool flag = false;
     int errorCode = 0;
     serverName = Utils.GetPolicyServerName(this.context, true, false, this.Name);
     flag = Utils.Impersonate(this.context);
     try
     {
         try
         {
             errorCode = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsEnumerateDomainTrustsW(serverName, 0x2a, out zero, out count);
         }
         finally
         {
             if (flag)
             {
                 Utils.Revert();
             }
         }
     }
     catch
     {
         throw;
     }
     if (errorCode != 0)
     {
         throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, serverName);
     }
     try
     {
         if ((zero != IntPtr.Zero) && (count != 0))
         {
             IntPtr ptr = IntPtr.Zero;
             for (int i = 0; i < count; i++)
             {
                 ptr = (IntPtr) (((long) zero) + (i * Marshal.SizeOf(typeof(DS_DOMAIN_TRUSTS))));
                 DS_DOMAIN_TRUSTS structure = new DS_DOMAIN_TRUSTS();
                 Marshal.PtrToStructure(ptr, structure);
                 if (targetForestName != null)
                 {
                     bool flag2 = false;
                     string str2 = null;
                     string str3 = null;
                     if (structure.DnsDomainName != IntPtr.Zero)
                     {
                         str2 = Marshal.PtrToStringUni(structure.DnsDomainName);
                     }
                     if (structure.NetbiosDomainName != IntPtr.Zero)
                     {
                         str3 = Marshal.PtrToStringUni(structure.NetbiosDomainName);
                     }
                     if ((str2 != null) && (Utils.Compare(targetForestName, str2) == 0))
                     {
                         flag2 = true;
                     }
                     else if ((str3 != null) && (Utils.Compare(targetForestName, str3) == 0))
                     {
                         flag2 = true;
                     }
                     if (!flag2)
                     {
                         continue;
                     }
                 }
                 if (((structure.TrustType == TrustHelper.TRUST_TYPE_UPLEVEL) && ((structure.TrustAttributes & 8) != 0)) && ((structure.Flags & 8) == 0))
                 {
                     TrustRelationshipInformation info = new ForestTrustRelationshipInformation(this.context, this.Name, structure, TrustType.Forest);
                     informations.Add(info);
                 }
             }
         }
         informations2 = informations;
     }
     finally
     {
         if (zero != IntPtr.Zero)
         {
             System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.NetApiBufferFree(zero);
         }
     }
     return informations2;
 }
Beispiel #11
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;
        }
Beispiel #12
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);
        }
        private TrustRelationshipInformationCollection GetTrustsHelper(string targetForestName)
        {
            string serverName = null;
            TrustRelationshipInformationCollection informations2;
            IntPtr zero  = IntPtr.Zero;
            int    count = 0;
            TrustRelationshipInformationCollection informations = new TrustRelationshipInformationCollection();
            bool flag      = false;
            int  errorCode = 0;

            serverName = Utils.GetPolicyServerName(this.context, true, false, this.Name);
            flag       = Utils.Impersonate(this.context);
            try
            {
                try
                {
                    errorCode = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsEnumerateDomainTrustsW(serverName, 0x2a, out zero, out count);
                }
                finally
                {
                    if (flag)
                    {
                        Utils.Revert();
                    }
                }
            }
            catch
            {
                throw;
            }
            if (errorCode != 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, serverName);
            }
            try
            {
                if ((zero != IntPtr.Zero) && (count != 0))
                {
                    IntPtr ptr = IntPtr.Zero;
                    for (int i = 0; i < count; i++)
                    {
                        ptr = (IntPtr)(((long)zero) + (i * Marshal.SizeOf(typeof(DS_DOMAIN_TRUSTS))));
                        DS_DOMAIN_TRUSTS structure = new DS_DOMAIN_TRUSTS();
                        Marshal.PtrToStructure(ptr, structure);
                        if (targetForestName != null)
                        {
                            bool   flag2 = false;
                            string str2  = null;
                            string str3  = null;
                            if (structure.DnsDomainName != IntPtr.Zero)
                            {
                                str2 = Marshal.PtrToStringUni(structure.DnsDomainName);
                            }
                            if (structure.NetbiosDomainName != IntPtr.Zero)
                            {
                                str3 = Marshal.PtrToStringUni(structure.NetbiosDomainName);
                            }
                            if ((str2 != null) && (Utils.Compare(targetForestName, str2) == 0))
                            {
                                flag2 = true;
                            }
                            else if ((str3 != null) && (Utils.Compare(targetForestName, str3) == 0))
                            {
                                flag2 = true;
                            }
                            if (!flag2)
                            {
                                continue;
                            }
                        }
                        if (((structure.TrustType == TrustHelper.TRUST_TYPE_UPLEVEL) && ((structure.TrustAttributes & 8) != 0)) && ((structure.Flags & 8) == 0))
                        {
                            TrustRelationshipInformation info = new ForestTrustRelationshipInformation(this.context, this.Name, structure, TrustType.Forest);
                            informations.Add(info);
                        }
                    }
                }
                informations2 = informations;
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.NetApiBufferFree(zero);
                }
            }
            return(informations2);
        }
 public TrustRelationshipInformation GetTrustRelationship(string targetDomainName)
 {
     base.CheckIfDisposed();
     if (targetDomainName == null)
     {
         throw new ArgumentNullException("targetDomainName");
     }
     if (targetDomainName.Length == 0)
     {
         throw new ArgumentException(Res.GetString("EmptyStringParameter"), "targetDomainName");
     }
     ArrayList trustsHelper = this.GetTrustsHelper(targetDomainName);
     TrustRelationshipInformationCollection informations = new TrustRelationshipInformationCollection(base.context, base.Name, trustsHelper);
     if (informations.Count == 0)
     {
         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DomainTrustDoesNotExist", new object[] { base.Name, targetDomainName }), typeof(TrustRelationshipInformation), null);
     }
     return informations[0];
 }
Beispiel #15
0
		public TrustRelationshipInformation GetTrustRelationship(string targetDomainName)
		{
			base.CheckIfDisposed();
			if (targetDomainName != null)
			{
				if (targetDomainName.Length != 0)
				{
					ArrayList trustsHelper = this.GetTrustsHelper(targetDomainName);
					TrustRelationshipInformationCollection trustRelationshipInformationCollection = new TrustRelationshipInformationCollection(this.context, base.Name, trustsHelper);
					if (trustRelationshipInformationCollection.Count != 0)
					{
						return trustRelationshipInformationCollection[0];
					}
					else
					{
						object[] name = new object[2];
						name[0] = base.Name;
						name[1] = targetDomainName;
						throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DomainTrustDoesNotExist", name), typeof(TrustRelationshipInformation), null);
					}
				}
				else
				{
					throw new ArgumentException(Res.GetString("EmptyStringParameter"), "targetDomainName");
				}
			}
			else
			{
				throw new ArgumentNullException("targetDomainName");
			}
		}
Beispiel #16
0
        public TrustRelationshipInformationCollection GetAllTrustRelationships()
        {
            TrustRelationshipInformationCollection trustcollection = new TrustRelationshipInformationCollection();

            DomainCollection dmc = this.Domains;

            if (dmc != null && dmc.Count > 0)
            {
                foreach (Domain dm in dmc)
                {
                    TrustRelationshipInformationCollection dmtrusts = dm.GetAllTrustRelationships();
                    if (dmtrusts != null && dmtrusts.Count > 0)
                    {
                        foreach (TrustRelationshipInformation trust in dmtrusts)
                            trustcollection.Add(trust);
                    }
                }
            }

            return trustcollection;
        }
Beispiel #17
0
		private TrustRelationshipInformationCollection GetTrustsHelper(string targetForestName)
		{
			TrustRelationshipInformationCollection trustRelationshipInformationCollection;
			IntPtr intPtr = (IntPtr)0;
			int num = 0;
			TrustRelationshipInformationCollection trustRelationshipInformationCollection1 = new TrustRelationshipInformationCollection();
			int num1 = 0;
			string policyServerName = Utils.GetPolicyServerName(this.context, true, false, this.Name);
			bool flag = Utils.Impersonate(this.context);
			try
			{
				try
				{
					num1 = UnsafeNativeMethods.DsEnumerateDomainTrustsW(policyServerName, 42, out intPtr, out num);
				}
				finally
				{
					if (flag)
					{
						Utils.Revert();
					}
				}
			}
			catch
			{
				throw;
			}
			if (num1 == 0)
			{
				try
				{
					if (intPtr != (IntPtr)0 && num != 0)
					{
						for (int i = 0; i < num; i++)
						{
							IntPtr intPtr1 = (IntPtr)((long)intPtr + (long)(i * Marshal.SizeOf(typeof(DS_DOMAIN_TRUSTS))));
							DS_DOMAIN_TRUSTS dSDOMAINTRUST = new DS_DOMAIN_TRUSTS();
							Marshal.PtrToStructure(intPtr1, dSDOMAINTRUST);
							if (targetForestName != null)
							{
								bool flag1 = false;
								string stringUni = null;
								string str = null;
								if (dSDOMAINTRUST.DnsDomainName != (IntPtr)0)
								{
									stringUni = Marshal.PtrToStringUni(dSDOMAINTRUST.DnsDomainName);
								}
								if (dSDOMAINTRUST.NetbiosDomainName != (IntPtr)0)
								{
									str = Marshal.PtrToStringUni(dSDOMAINTRUST.NetbiosDomainName);
								}
								if (stringUni == null || Utils.Compare(targetForestName, stringUni) != 0)
								{
									if (str != null && Utils.Compare(targetForestName, str) == 0)
									{
										flag1 = true;
									}
								}
								else
								{
									flag1 = true;
								}
								if (!flag1)
								{
									goto Label0;
								}
							}
							if (dSDOMAINTRUST.TrustType == TrustHelper.TRUST_TYPE_UPLEVEL && (dSDOMAINTRUST.TrustAttributes & 8) != 0 && (dSDOMAINTRUST.Flags & 8) == 0)
							{
								TrustRelationshipInformation forestTrustRelationshipInformation = new ForestTrustRelationshipInformation(this.context, this.Name, dSDOMAINTRUST, TrustType.Forest);
								trustRelationshipInformationCollection1.Add(forestTrustRelationshipInformation);
							}
						Label0:
							continue;
						}
					}
					trustRelationshipInformationCollection = trustRelationshipInformationCollection1;
				}
				finally
				{
					if (intPtr != (IntPtr)0)
					{
						UnsafeNativeMethods.NetApiBufferFree(intPtr);
					}
				}
				return trustRelationshipInformationCollection;
			}
			else
			{
				throw ExceptionHelper.GetExceptionFromErrorCode(num1, policyServerName);
			}
		}
Beispiel #18
0
        private TrustRelationshipInformationCollection GetTrustsHelper(string targetForestName)
        {
            string serverName = null;
            IntPtr domains = (IntPtr)0;
            int count = 0;
            TrustRelationshipInformationCollection collection = new TrustRelationshipInformationCollection();
            bool impersonated = false;
            int error = 0;

            // first decide which server to go to
            serverName = Utils.GetPolicyServerName(_context, true, false, Name);

            // impersonate appropriately
            impersonated = Utils.Impersonate(_context);

            // call the DS API to get trust domain information
            try
            {
                try
                {
                    error = UnsafeNativeMethods.DsEnumerateDomainTrustsW(serverName, (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_PRIMARY | (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_OUTBOUND | (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_INBOUND, out domains, out count);
                }
                finally
                {
                    if (impersonated)
                        Utils.Revert();
                }
            }
            catch { throw; }

            // check the result
            if (error != 0)
                throw ExceptionHelper.GetExceptionFromErrorCode(error, serverName);

            try
            {
                // now enumerate through the collection
                if (domains != (IntPtr)0 && count != 0)
                {
                    IntPtr addr = (IntPtr)0;
                    for (int i = 0; i < count; i++)
                    {
                        addr = IntPtr.Add(domains, i * Marshal.SizeOf(typeof(DS_DOMAIN_TRUSTS)));
                        DS_DOMAIN_TRUSTS unmanagedTrust = new DS_DOMAIN_TRUSTS();
                        Marshal.PtrToStructure(addr, unmanagedTrust);

                        // whether this is the case that a paticular forest trust info is needed
                        if (targetForestName != null)
                        {
                            bool sameTarget = false;
                            string tmpDNSName = null;
                            string tmpNetBIOSName = null;

                            if (unmanagedTrust.DnsDomainName != (IntPtr)0)
                                tmpDNSName = Marshal.PtrToStringUni(unmanagedTrust.DnsDomainName);
                            if (unmanagedTrust.NetbiosDomainName != (IntPtr)0)
                                tmpNetBIOSName = Marshal.PtrToStringUni(unmanagedTrust.NetbiosDomainName);

                            // check whether it is the same target
                            if (tmpDNSName != null && Utils.Compare(targetForestName, tmpDNSName) == 0)
                                sameTarget = true;
                            else if (tmpNetBIOSName != null && Utils.Compare(targetForestName, tmpNetBIOSName) == 0)
                                sameTarget = true;

                            if (!sameTarget)
                                continue;
                        }

                        // if it is up level trust and forest attribute is set
                        if (unmanagedTrust.TrustType == TrustHelper.TRUST_TYPE_UPLEVEL && ((unmanagedTrust.TrustAttributes & (int)TRUST_ATTRIBUTE.TRUST_ATTRIBUTE_FOREST_TRANSITIVE) != 0))
                        {
                            // we don't want to include self
                            if ((unmanagedTrust.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_PRIMARY) != 0)
                                continue;

                            TrustRelationshipInformation trust = new ForestTrustRelationshipInformation(_context, Name, unmanagedTrust, TrustType.Forest);
                            collection.Add(trust);
                        }
                    }
                }
                return collection;
            }
            finally
            {
                if (domains != (IntPtr)0)
                    UnsafeNativeMethods.NetApiBufferFree(domains);
            }
        }