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) { 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); } }
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 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); } }