internal ForestTrustRelationshipInformation(DirectoryContext context, string source, DS_DOMAIN_TRUSTS unmanagedTrust, TrustType type)
        {
            string tmpDNSName = null;
            string tmpNetBIOSName = null;

            // security context
            this.context = context;
            // source 
            this.source = source;
            // target
            if (unmanagedTrust.DnsDomainName != (IntPtr)0)
                tmpDNSName = Marshal.PtrToStringUni(unmanagedTrust.DnsDomainName);
            if (unmanagedTrust.NetbiosDomainName != (IntPtr)0)
                tmpNetBIOSName = Marshal.PtrToStringUni(unmanagedTrust.NetbiosDomainName);

            this.target = (tmpDNSName == null ? tmpNetBIOSName : tmpDNSName);
            // direction
            if ((unmanagedTrust.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_OUTBOUND) != 0 &&
                (unmanagedTrust.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_INBOUND) != 0)
                direction = TrustDirection.Bidirectional;
            else if ((unmanagedTrust.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_OUTBOUND) != 0)
                direction = TrustDirection.Outbound;
            else if ((unmanagedTrust.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_INBOUND) != 0)
                direction = TrustDirection.Inbound;
            // type
            this.type = type;
        }
		internal ForestTrustRelationshipInformation(DirectoryContext context, string source, DS_DOMAIN_TRUSTS unmanagedTrust, TrustType type)
		{
			string str;
			this.topLevelNames = new TopLevelNameCollection();
			this.excludedNames = new StringCollection();
			this.domainInfo = new ForestTrustDomainInfoCollection();
			this.binaryData = new ArrayList();
			this.excludedNameTime = new Hashtable();
			this.binaryDataTime = new ArrayList();
			string stringUni = null;
			string stringUni1 = null;
			this.context = context;
			this.source = source;
			if (unmanagedTrust.DnsDomainName != (IntPtr)0)
			{
				stringUni = Marshal.PtrToStringUni(unmanagedTrust.DnsDomainName);
			}
			if (unmanagedTrust.NetbiosDomainName != (IntPtr)0)
			{
				stringUni1 = Marshal.PtrToStringUni(unmanagedTrust.NetbiosDomainName);
			}
			ForestTrustRelationshipInformation forestTrustRelationshipInformation = this;
			if (stringUni == null)
			{
				str = stringUni1;
			}
			else
			{
				str = stringUni;
			}
			forestTrustRelationshipInformation.target = str;
			if ((unmanagedTrust.Flags & 2) == 0 || (unmanagedTrust.Flags & 32) == 0)
			{
				if ((unmanagedTrust.Flags & 2) == 0)
				{
					if ((unmanagedTrust.Flags & 32) != 0)
					{
						this.direction = TrustDirection.Inbound;
					}
				}
				else
				{
					this.direction = TrustDirection.Outbound;
				}
			}
			else
			{
				this.direction = TrustDirection.Bidirectional;
			}
			this.type = type;
		}
Example #3
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);
			}
		}
Example #4
0
		private ArrayList GetTrustsHelper(string targetDomainName)
		{
			string name;
			ArrayList arrayLists;
			IntPtr intPtr = (IntPtr)0;
			int num = 0;
			ArrayList arrayLists1 = new ArrayList();
			ArrayList arrayLists2 = new ArrayList();
			new TrustRelationshipInformationCollection();
			int num1 = 0;
			string stringUni = null;
			int num2 = 0;
			if (!this.context.isServer())
			{
				name = DomainController.FindOne(this.context).Name;
			}
			else
			{
				name = this.context.Name;
			}
			bool flag = Utils.Impersonate(this.context);
			try
			{
				try
				{
					num2 = UnsafeNativeMethods.DsEnumerateDomainTrustsW(name, 35, out intPtr, out num);
				}
				finally
				{
					if (flag)
					{
						Utils.Revert();
					}
				}
			}
			catch
			{
				throw;
			}
			if (num2 == 0)
			{
				try
				{
					if (intPtr != (IntPtr)0 && num != 0)
					{
						int num3 = 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);
							arrayLists1.Add(dSDOMAINTRUST);
						}
						for (int j = 0; j < arrayLists1.Count; j++)
						{
							DS_DOMAIN_TRUSTS item = (DS_DOMAIN_TRUSTS)arrayLists1[j];
							if ((item.Flags & 42) != 0 && item.TrustType != TrustHelper.TRUST_TYPE_DOWNLEVEL)
							{
								TrustObject trustObject = new TrustObject();
								trustObject.TrustType = TrustType.Unknown;
								if (item.DnsDomainName != (IntPtr)0)
								{
									trustObject.DnsDomainName = Marshal.PtrToStringUni(item.DnsDomainName);
								}
								if (item.NetbiosDomainName != (IntPtr)0)
								{
									trustObject.NetbiosDomainName = Marshal.PtrToStringUni(item.NetbiosDomainName);
								}
								trustObject.Flags = item.Flags;
								trustObject.TrustAttributes = item.TrustAttributes;
								trustObject.OriginalIndex = j;
								trustObject.ParentIndex = item.ParentIndex;
								if (targetDomainName != null)
								{
									bool flag1 = false;
									if (trustObject.DnsDomainName == null || Utils.Compare(targetDomainName, trustObject.DnsDomainName) != 0)
									{
										if (trustObject.NetbiosDomainName != null && Utils.Compare(targetDomainName, trustObject.NetbiosDomainName) == 0)
										{
											flag1 = true;
										}
									}
									else
									{
										flag1 = true;
									}
									if (!flag1 && (trustObject.Flags & 8) == 0)
									{
										goto Label0;
									}
								}
								if ((trustObject.Flags & 8) == 0)
								{
									if (item.TrustType == 3)
									{
										trustObject.TrustType = TrustType.Kerberos;
									}
								}
								else
								{
									num1 = num3;
									if ((trustObject.Flags & 4) == 0)
									{
										DS_DOMAIN_TRUSTS item1 = (DS_DOMAIN_TRUSTS)arrayLists1[trustObject.ParentIndex];
										if (item1.DnsDomainName != (IntPtr)0)
										{
											stringUni = Marshal.PtrToStringUni(item1.DnsDomainName);
										}
									}
									trustObject.TrustType = TrustType.ParentChild | TrustType.CrossLink | TrustType.External | TrustType.Forest | TrustType.Kerberos | TrustType.Unknown;
								}
								num3++;
								arrayLists2.Add(trustObject);
							}
						Label0:
							continue;
						}
						for (int k = 0; k < arrayLists2.Count; k++)
						{
							TrustObject trustObject1 = (TrustObject)arrayLists2[k];
							if (k != num1 && trustObject1.TrustType != TrustType.Kerberos)
							{
								if (stringUni == null || Utils.Compare(stringUni, trustObject1.DnsDomainName) != 0)
								{
									if ((trustObject1.Flags & 1) == 0)
									{
										if ((trustObject1.TrustAttributes & 8) == 0)
										{
											trustObject1.TrustType = TrustType.External;
										}
										else
										{
											trustObject1.TrustType = TrustType.Forest;
										}
									}
									else
									{
										if (trustObject1.ParentIndex != ((TrustObject)arrayLists2[num1]).OriginalIndex)
										{
											if ((trustObject1.Flags & 4) == 0 || (((TrustObject)arrayLists2[num1]).Flags & 4) == 0)
											{
												trustObject1.TrustType = TrustType.CrossLink;
											}
											else
											{
												string str = this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDomainNamingContext);
												string dnsNameFromDN = Utils.GetDnsNameFromDN(str);
												DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(this.context.Name, DirectoryContextType.Forest, this.context);
												if (newDirectoryContext.isRootDomain() || Utils.Compare(trustObject1.DnsDomainName, dnsNameFromDN) == 0)
												{
													trustObject1.TrustType = TrustType.TreeRoot;
												}
												else
												{
													trustObject1.TrustType = TrustType.CrossLink;
												}
											}
										}
										else
										{
											trustObject1.TrustType = TrustType.ParentChild;
										}
									}
								}
								else
								{
									trustObject1.TrustType = TrustType.ParentChild;
								}
							}
						}
					}
					arrayLists = arrayLists2;
				}
				finally
				{
					if (intPtr != (IntPtr)0)
					{
						UnsafeNativeMethods.NetApiBufferFree(intPtr);
					}
				}
				return arrayLists;
			}
			else
			{
				throw ExceptionHelper.GetExceptionFromErrorCode(num2, name);
			}
		}
Example #5
0
        internal ForestTrustRelationshipInformation(DirectoryContext context, string source, DS_DOMAIN_TRUSTS unmanagedTrust, TrustType type)
        {
            string tmpDNSName     = null;
            string tmpNetBIOSName = null;

            // security context
            this.context = context;
            // source
            this.source = source;
            // target
            if (unmanagedTrust.DnsDomainName != (IntPtr)0)
            {
                tmpDNSName = Marshal.PtrToStringUni(unmanagedTrust.DnsDomainName);
            }
            if (unmanagedTrust.NetbiosDomainName != (IntPtr)0)
            {
                tmpNetBIOSName = Marshal.PtrToStringUni(unmanagedTrust.NetbiosDomainName);
            }

            this.target = (tmpDNSName == null ? tmpNetBIOSName : tmpDNSName);
            // direction
            if ((unmanagedTrust.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_OUTBOUND) != 0 &&
                (unmanagedTrust.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_INBOUND) != 0)
            {
                direction = TrustDirection.Bidirectional;
            }
            else if ((unmanagedTrust.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_OUTBOUND) != 0)
            {
                direction = TrustDirection.Outbound;
            }
            else if ((unmanagedTrust.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_INBOUND) != 0)
            {
                direction = TrustDirection.Inbound;
            }
            // type
            this.type = type;
        }
Example #6
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);
            }
        }
Example #7
0
        internal ForestTrustRelationshipInformation(DirectoryContext context, string source, DS_DOMAIN_TRUSTS unmanagedTrust, TrustType type)
        {
            string str;

            this.topLevelNames    = new TopLevelNameCollection();
            this.excludedNames    = new StringCollection();
            this.domainInfo       = new ForestTrustDomainInfoCollection();
            this.binaryData       = new ArrayList();
            this.excludedNameTime = new Hashtable();
            this.binaryDataTime   = new ArrayList();
            string stringUni  = null;
            string stringUni1 = null;

            this.context = context;
            this.source  = source;
            if (unmanagedTrust.DnsDomainName != (IntPtr)0)
            {
                stringUni = Marshal.PtrToStringUni(unmanagedTrust.DnsDomainName);
            }
            if (unmanagedTrust.NetbiosDomainName != (IntPtr)0)
            {
                stringUni1 = Marshal.PtrToStringUni(unmanagedTrust.NetbiosDomainName);
            }
            ForestTrustRelationshipInformation forestTrustRelationshipInformation = this;

            if (stringUni == null)
            {
                str = stringUni1;
            }
            else
            {
                str = stringUni;
            }
            forestTrustRelationshipInformation.target = str;
            if ((unmanagedTrust.Flags & 2) == 0 || (unmanagedTrust.Flags & 32) == 0)
            {
                if ((unmanagedTrust.Flags & 2) == 0)
                {
                    if ((unmanagedTrust.Flags & 32) != 0)
                    {
                        this.direction = TrustDirection.Inbound;
                    }
                }
                else
                {
                    this.direction = TrustDirection.Outbound;
                }
            }
            else
            {
                this.direction = TrustDirection.Bidirectional;
            }
            this.type = type;
        }
 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;
 }
        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);
        }
 private ArrayList GetTrustsHelper(string targetDomainName)
 {
     string serverName = null;
     ArrayList list3;
     IntPtr zero = IntPtr.Zero;
     int count = 0;
     ArrayList list = new ArrayList();
     ArrayList list2 = new ArrayList();
     new TrustRelationshipInformationCollection();
     int num2 = 0;
     string str2 = null;
     int errorCode = 0;
     bool flag = false;
     if (base.context.isServer())
     {
         serverName = base.context.Name;
     }
     else
     {
         serverName = DomainController.FindOne(base.context).Name;
     }
     flag = Utils.Impersonate(base.context);
     try
     {
         try
         {
             errorCode = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsEnumerateDomainTrustsW(serverName, 0x23, 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;
             int num4 = 0;
             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);
                 list.Add(structure);
             }
             for (int j = 0; j < list.Count; j++)
             {
                 DS_DOMAIN_TRUSTS ds_domain_trusts2 = (DS_DOMAIN_TRUSTS) list[j];
                 if (((ds_domain_trusts2.Flags & 0x2a) != 0) && (ds_domain_trusts2.TrustType != TrustHelper.TRUST_TYPE_DOWNLEVEL))
                 {
                     TrustObject obj2 = new TrustObject {
                         TrustType = TrustType.Unknown
                     };
                     if (ds_domain_trusts2.DnsDomainName != IntPtr.Zero)
                     {
                         obj2.DnsDomainName = Marshal.PtrToStringUni(ds_domain_trusts2.DnsDomainName);
                     }
                     if (ds_domain_trusts2.NetbiosDomainName != IntPtr.Zero)
                     {
                         obj2.NetbiosDomainName = Marshal.PtrToStringUni(ds_domain_trusts2.NetbiosDomainName);
                     }
                     obj2.Flags = ds_domain_trusts2.Flags;
                     obj2.TrustAttributes = ds_domain_trusts2.TrustAttributes;
                     obj2.OriginalIndex = j;
                     obj2.ParentIndex = ds_domain_trusts2.ParentIndex;
                     if (targetDomainName != null)
                     {
                         bool flag2 = false;
                         if ((obj2.DnsDomainName != null) && (Utils.Compare(targetDomainName, obj2.DnsDomainName) == 0))
                         {
                             flag2 = true;
                         }
                         else if ((obj2.NetbiosDomainName != null) && (Utils.Compare(targetDomainName, obj2.NetbiosDomainName) == 0))
                         {
                             flag2 = true;
                         }
                         if (!flag2 && ((obj2.Flags & 8) == 0))
                         {
                             continue;
                         }
                     }
                     if ((obj2.Flags & 8) != 0)
                     {
                         num2 = num4;
                         if ((obj2.Flags & 4) == 0)
                         {
                             DS_DOMAIN_TRUSTS ds_domain_trusts3 = (DS_DOMAIN_TRUSTS) list[obj2.ParentIndex];
                             if (ds_domain_trusts3.DnsDomainName != IntPtr.Zero)
                             {
                                 str2 = Marshal.PtrToStringUni(ds_domain_trusts3.DnsDomainName);
                             }
                         }
                         obj2.TrustType = TrustType.Unknown | TrustType.ParentChild;
                     }
                     else if (ds_domain_trusts2.TrustType == 3)
                     {
                         obj2.TrustType = TrustType.Kerberos;
                     }
                     num4++;
                     list2.Add(obj2);
                 }
             }
             for (int k = 0; k < list2.Count; k++)
             {
                 TrustObject obj3 = (TrustObject) list2[k];
                 if ((k != num2) && (obj3.TrustType != TrustType.Kerberos))
                 {
                     if ((str2 != null) && (Utils.Compare(str2, obj3.DnsDomainName) == 0))
                     {
                         obj3.TrustType = TrustType.ParentChild;
                     }
                     else if ((obj3.Flags & 1) != 0)
                     {
                         if (obj3.ParentIndex == ((TrustObject) list2[num2]).OriginalIndex)
                         {
                             obj3.TrustType = TrustType.ParentChild;
                         }
                         else if (((obj3.Flags & 4) != 0) && ((((TrustObject) list2[num2]).Flags & 4) != 0))
                         {
                             string dnsNameFromDN = null;
                             dnsNameFromDN = Utils.GetDnsNameFromDN(base.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDomainNamingContext));
                             if (Utils.GetNewDirectoryContext(base.context.Name, DirectoryContextType.Forest, base.context).isRootDomain() || (Utils.Compare(obj3.DnsDomainName, dnsNameFromDN) == 0))
                             {
                                 obj3.TrustType = TrustType.TreeRoot;
                             }
                             else
                             {
                                 obj3.TrustType = TrustType.CrossLink;
                             }
                         }
                         else
                         {
                             obj3.TrustType = TrustType.CrossLink;
                         }
                     }
                     else if ((obj3.TrustAttributes & 8) != 0)
                     {
                         obj3.TrustType = TrustType.Forest;
                     }
                     else
                     {
                         obj3.TrustType = TrustType.External;
                     }
                 }
             }
         }
         list3 = list2;
     }
     finally
     {
         if (zero != IntPtr.Zero)
         {
             System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.NetApiBufferFree(zero);
         }
     }
     return list3;
 }
Example #11
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);
            }
        }
Example #12
0
        internal ForestTrustRelationshipInformation(DirectoryContext context, string source, DS_DOMAIN_TRUSTS unmanagedTrust, TrustType type)
        {
            string str  = null;
            string str2 = null;

            base.context = context;
            base.source  = source;
            if (unmanagedTrust.DnsDomainName != IntPtr.Zero)
            {
                str = Marshal.PtrToStringUni(unmanagedTrust.DnsDomainName);
            }
            if (unmanagedTrust.NetbiosDomainName != IntPtr.Zero)
            {
                str2 = Marshal.PtrToStringUni(unmanagedTrust.NetbiosDomainName);
            }
            base.target = (str == null) ? str2 : str;
            if (((unmanagedTrust.Flags & 2) != 0) && ((unmanagedTrust.Flags & 0x20) != 0))
            {
                base.direction = TrustDirection.Bidirectional;
            }
            else if ((unmanagedTrust.Flags & 2) != 0)
            {
                base.direction = TrustDirection.Outbound;
            }
            else if ((unmanagedTrust.Flags & 0x20) != 0)
            {
                base.direction = TrustDirection.Inbound;
            }
            base.type = type;
        }