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 void GetForestTrustInfoHelper() { IntPtr forestTrustInfo = (IntPtr)0; PolicySafeHandle handle = null; LSA_UNICODE_STRING tmpName = null; bool impersonated = false; IntPtr targetPtr = (IntPtr)0; string serverName = null; TopLevelNameCollection tmpTLNs = new TopLevelNameCollection(); StringCollection tmpExcludedTLNs = new StringCollection(); ForestTrustDomainInfoCollection tmpDomainInformation = new ForestTrustDomainInfoCollection(); // internal members ArrayList tmpBinaryData = new ArrayList(); Hashtable tmpExcludedNameTime = new Hashtable(); ArrayList tmpBinaryDataTime = new ArrayList(); try { try { // get the target name tmpName = new LSA_UNICODE_STRING(); targetPtr = Marshal.StringToHGlobalUni(TargetName); UnsafeNativeMethods.RtlInitUnicodeString(tmpName, targetPtr); serverName = Utils.GetPolicyServerName(context, true, false, source); // do impersonation impersonated = Utils.Impersonate(context); // get the policy handle handle = new PolicySafeHandle(Utils.GetPolicyHandle(serverName)); int result = UnsafeNativeMethods.LsaQueryForestTrustInformation(handle, tmpName, ref forestTrustInfo); // check the result if (result != 0) { int win32Error = UnsafeNativeMethods.LsaNtStatusToWinError(result); if (win32Error != 0) { throw ExceptionHelper.GetExceptionFromErrorCode(win32Error, serverName); } } try { if (forestTrustInfo != (IntPtr)0) { LSA_FOREST_TRUST_INFORMATION trustInfo = new LSA_FOREST_TRUST_INFORMATION(); Marshal.PtrToStructure(forestTrustInfo, trustInfo); int count = trustInfo.RecordCount; IntPtr addr = (IntPtr)0; for (int i = 0; i < count; i++) { addr = Marshal.ReadIntPtr(trustInfo.Entries, i * Marshal.SizeOf(typeof(IntPtr))); LSA_FOREST_TRUST_RECORD record = new LSA_FOREST_TRUST_RECORD(); Marshal.PtrToStructure(addr, record); if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelName) { IntPtr myPtr = IntPtr.Add(addr, 16); Marshal.PtrToStructure(myPtr, record.TopLevelName); TopLevelName TLN = new TopLevelName(record.Flags, record.TopLevelName, record.Time); tmpTLNs.Add(TLN); } else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelNameEx) { // get the excluded TLN and put it in our collection IntPtr myPtr = IntPtr.Add(addr, 16); Marshal.PtrToStructure(myPtr, record.TopLevelName); string excludedName = Marshal.PtrToStringUni(record.TopLevelName.Buffer, record.TopLevelName.Length / 2); tmpExcludedTLNs.Add(excludedName); tmpExcludedNameTime.Add(excludedName, record.Time); } else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustDomainInfo) { ForestTrustDomainInformation dom = new ForestTrustDomainInformation(record.Flags, record.DomainInfo, record.Time); tmpDomainInformation.Add(dom); } else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustRecordTypeLast) { // enumeration is done, but we might still have some unrecognized entries after that continue; } else { int length = record.Data.Length; byte[] byteArray = new byte[length]; if ((record.Data.Buffer != (IntPtr)0) && (length != 0)) { Marshal.Copy(record.Data.Buffer, byteArray, 0, length); } tmpBinaryData.Add(byteArray); tmpBinaryDataTime.Add(record.Time); } } } } finally { UnsafeNativeMethods.LsaFreeMemory(forestTrustInfo); } _topLevelNames = tmpTLNs; _excludedNames = tmpExcludedTLNs; _domainInfo = tmpDomainInformation; _binaryData = tmpBinaryData; _excludedNameTime = tmpExcludedNameTime; _binaryDataTime = tmpBinaryDataTime; // mark it as retrieved retrieved = true; } finally { if (impersonated) { Utils.Revert(); } if (targetPtr != (IntPtr)0) { Marshal.FreeHGlobal(targetPtr); } } } catch { throw; } }
private void GetForestTrustInfoHelper() { IntPtr intPtr = (IntPtr)0; bool flag = false; IntPtr hGlobalUni = (IntPtr)0; TopLevelNameCollection topLevelNameCollection = new TopLevelNameCollection(); StringCollection stringCollections = new StringCollection(); ForestTrustDomainInfoCollection forestTrustDomainInfoCollection = new ForestTrustDomainInfoCollection(); ArrayList arrayLists = new ArrayList(); Hashtable hashtables = new Hashtable(); ArrayList arrayLists1 = new ArrayList(); try { try { LSA_UNICODE_STRING lSAUNICODESTRING = new LSA_UNICODE_STRING(); hGlobalUni = Marshal.StringToHGlobalUni(base.TargetName); UnsafeNativeMethods.RtlInitUnicodeString(lSAUNICODESTRING, hGlobalUni); string policyServerName = Utils.GetPolicyServerName(this.context, true, false, this.source); flag = Utils.Impersonate(this.context); PolicySafeHandle policySafeHandle = new PolicySafeHandle(Utils.GetPolicyHandle(policyServerName)); int num = UnsafeNativeMethods.LsaQueryForestTrustInformation(policySafeHandle, lSAUNICODESTRING, ref intPtr); if (num != 0) { int winError = UnsafeNativeMethods.LsaNtStatusToWinError(num); if (winError != 0) { throw ExceptionHelper.GetExceptionFromErrorCode(winError, policyServerName); } } try { if (intPtr != (IntPtr)0) { LSA_FOREST_TRUST_INFORMATION lSAFORESTTRUSTINFORMATION = new LSA_FOREST_TRUST_INFORMATION(); Marshal.PtrToStructure(intPtr, lSAFORESTTRUSTINFORMATION); int recordCount = lSAFORESTTRUSTINFORMATION.RecordCount; for (int i = 0; i < recordCount; i++) { IntPtr intPtr1 = Marshal.ReadIntPtr(lSAFORESTTRUSTINFORMATION.Entries, i * Marshal.SizeOf(typeof(IntPtr))); LSA_FOREST_TRUST_RECORD lSAFORESTTRUSTRECORD = new LSA_FOREST_TRUST_RECORD(); Marshal.PtrToStructure(intPtr1, lSAFORESTTRUSTRECORD); if (lSAFORESTTRUSTRECORD.ForestTrustType != LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelName) { if (lSAFORESTTRUSTRECORD.ForestTrustType != LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelNameEx) { if (lSAFORESTTRUSTRECORD.ForestTrustType != LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustDomainInfo) { if (lSAFORESTTRUSTRECORD.ForestTrustType != LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustRecordTypeLast) { int length = lSAFORESTTRUSTRECORD.Data.Length; byte[] numArray = new byte[length]; if (lSAFORESTTRUSTRECORD.Data.Buffer != (IntPtr)0 && length != 0) { Marshal.Copy(lSAFORESTTRUSTRECORD.Data.Buffer, numArray, 0, length); } arrayLists.Add(numArray); arrayLists1.Add(lSAFORESTTRUSTRECORD.Time); } } else { ForestTrustDomainInformation forestTrustDomainInformation = new ForestTrustDomainInformation(lSAFORESTTRUSTRECORD.Flags, lSAFORESTTRUSTRECORD.DomainInfo, lSAFORESTTRUSTRECORD.Time); forestTrustDomainInfoCollection.Add(forestTrustDomainInformation); } } else { IntPtr intPtr2 = (IntPtr)((long)intPtr1 + (long)16); Marshal.PtrToStructure(intPtr2, lSAFORESTTRUSTRECORD.TopLevelName); string stringUni = Marshal.PtrToStringUni(lSAFORESTTRUSTRECORD.TopLevelName.Buffer, lSAFORESTTRUSTRECORD.TopLevelName.Length / 2); stringCollections.Add(stringUni); hashtables.Add(stringUni, lSAFORESTTRUSTRECORD.Time); } } else { IntPtr intPtr3 = (IntPtr)((long)intPtr1 + (long)16); Marshal.PtrToStructure(intPtr3, lSAFORESTTRUSTRECORD.TopLevelName); TopLevelName topLevelName = new TopLevelName(lSAFORESTTRUSTRECORD.Flags, lSAFORESTTRUSTRECORD.TopLevelName, lSAFORESTTRUSTRECORD.Time); topLevelNameCollection.Add(topLevelName); } } } } finally { UnsafeNativeMethods.LsaFreeMemory(intPtr); } this.topLevelNames = topLevelNameCollection; this.excludedNames = stringCollections; this.domainInfo = forestTrustDomainInfoCollection; this.binaryData = arrayLists; this.excludedNameTime = hashtables; this.binaryDataTime = arrayLists1; this.retrieved = true; } finally { if (flag) { Utils.Revert(); } if (hGlobalUni != (IntPtr)0) { Marshal.FreeHGlobal(hGlobalUni); } } } catch { throw; } }
private void GetForestTrustInfoHelper() { IntPtr forestTrustInfo = (IntPtr)0; PolicySafeHandle handle = null; LSA_UNICODE_STRING tmpName = null; bool impersonated = false; IntPtr targetPtr = (IntPtr)0; string serverName = null; TopLevelNameCollection tmpTLNs = new TopLevelNameCollection(); StringCollection tmpExcludedTLNs = new StringCollection(); ForestTrustDomainInfoCollection tmpDomainInformation = new ForestTrustDomainInfoCollection(); // internal members ArrayList tmpBinaryData = new ArrayList(); Hashtable tmpExcludedNameTime = new Hashtable(); ArrayList tmpBinaryDataTime = new ArrayList(); try { try { // get the target name tmpName = new LSA_UNICODE_STRING(); targetPtr = Marshal.StringToHGlobalUni(TargetName); UnsafeNativeMethods.RtlInitUnicodeString(tmpName, targetPtr); serverName = Utils.GetPolicyServerName(context, true, false, source); // do impersonation impersonated = Utils.Impersonate(context); // get the policy handle handle = new PolicySafeHandle(Utils.GetPolicyHandle(serverName)); int result = UnsafeNativeMethods.LsaQueryForestTrustInformation(handle, tmpName, ref forestTrustInfo); // check the result if (result != 0) { int win32Error = UnsafeNativeMethods.LsaNtStatusToWinError(result); if (win32Error != 0) { throw ExceptionHelper.GetExceptionFromErrorCode(win32Error, serverName); } } try { if (forestTrustInfo != (IntPtr)0) { LSA_FOREST_TRUST_INFORMATION trustInfo = new LSA_FOREST_TRUST_INFORMATION(); Marshal.PtrToStructure(forestTrustInfo, trustInfo); int count = trustInfo.RecordCount; IntPtr addr = (IntPtr)0; for (int i = 0; i < count; i++) { addr = Marshal.ReadIntPtr(trustInfo.Entries, i * Marshal.SizeOf(typeof(IntPtr))); LSA_FOREST_TRUST_RECORD record = new LSA_FOREST_TRUST_RECORD(); Marshal.PtrToStructure(addr, record); if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelName) { IntPtr myPtr = IntPtr.Add(addr, 16); Marshal.PtrToStructure(myPtr, record.TopLevelName); TopLevelName TLN = new TopLevelName(record.Flags, record.TopLevelName, record.Time); tmpTLNs.Add(TLN); } else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelNameEx) { // get the excluded TLN and put it in our collection IntPtr myPtr = IntPtr.Add(addr, 16); Marshal.PtrToStructure(myPtr, record.TopLevelName); string excludedName = Marshal.PtrToStringUni(record.TopLevelName.Buffer, record.TopLevelName.Length / 2); tmpExcludedTLNs.Add(excludedName); tmpExcludedNameTime.Add(excludedName, record.Time); } else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustDomainInfo) { ForestTrustDomainInformation dom = new ForestTrustDomainInformation(record.Flags, record.DomainInfo, record.Time); tmpDomainInformation.Add(dom); } else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustRecordTypeLast) { // enumeration is done, but we might still have some unrecognized entries after that continue; } else { int length = record.Data.Length; byte[] byteArray = new byte[length]; if ((record.Data.Buffer != (IntPtr)0) && (length != 0)) { Marshal.Copy(record.Data.Buffer, byteArray, 0, length); } tmpBinaryData.Add(byteArray); tmpBinaryDataTime.Add(record.Time); } } } } finally { UnsafeNativeMethods.LsaFreeMemory(forestTrustInfo); } _topLevelNames = tmpTLNs; _excludedNames = tmpExcludedTLNs; _domainInfo = tmpDomainInformation; _binaryData = tmpBinaryData; _excludedNameTime = tmpExcludedNameTime; _binaryDataTime = tmpBinaryDataTime; // mark it as retrieved retrieved = true; } finally { if (impersonated) Utils.Revert(); if (targetPtr != (IntPtr)0) { Marshal.FreeHGlobal(targetPtr); } } } catch { throw; } }
private void GetForestTrustInfoHelper() { IntPtr zero = IntPtr.Zero; PolicySafeHandle handle = null; LSA_UNICODE_STRING result = null; bool flag = false; IntPtr s = IntPtr.Zero; string serverName = null; TopLevelNameCollection names = new TopLevelNameCollection(); StringCollection strings = new StringCollection(); ForestTrustDomainInfoCollection infos = new ForestTrustDomainInfoCollection(); ArrayList list = new ArrayList(); Hashtable hashtable = new Hashtable(); ArrayList list2 = new ArrayList(); try { try { result = new LSA_UNICODE_STRING(); s = Marshal.StringToHGlobalUni(base.TargetName); System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.RtlInitUnicodeString(result, s); serverName = Utils.GetPolicyServerName(base.context, true, false, base.source); flag = Utils.Impersonate(base.context); handle = new PolicySafeHandle(Utils.GetPolicyHandle(serverName)); int status = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.LsaQueryForestTrustInformation(handle, result, ref zero); if (status != 0) { int errorCode = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.LsaNtStatusToWinError(status); if (errorCode != 0) { throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, serverName); } } try { if (zero != IntPtr.Zero) { LSA_FOREST_TRUST_INFORMATION structure = new LSA_FOREST_TRUST_INFORMATION(); Marshal.PtrToStructure(zero, structure); int recordCount = structure.RecordCount; IntPtr ptr = IntPtr.Zero; for (int i = 0; i < recordCount; i++) { ptr = Marshal.ReadIntPtr(structure.Entries, i * Marshal.SizeOf(typeof(IntPtr))); LSA_FOREST_TRUST_RECORD lsa_forest_trust_record = new LSA_FOREST_TRUST_RECORD(); Marshal.PtrToStructure(ptr, lsa_forest_trust_record); if (lsa_forest_trust_record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelName) { IntPtr ptr4 = (IntPtr)(((long)ptr) + 0x10L); Marshal.PtrToStructure(ptr4, lsa_forest_trust_record.TopLevelName); TopLevelName name = new TopLevelName(lsa_forest_trust_record.Flags, lsa_forest_trust_record.TopLevelName, lsa_forest_trust_record.Time); names.Add(name); } else if (lsa_forest_trust_record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelNameEx) { IntPtr ptr5 = (IntPtr)(((long)ptr) + 0x10L); Marshal.PtrToStructure(ptr5, lsa_forest_trust_record.TopLevelName); string str2 = Marshal.PtrToStringUni(lsa_forest_trust_record.TopLevelName.Buffer, lsa_forest_trust_record.TopLevelName.Length / 2); strings.Add(str2); hashtable.Add(str2, lsa_forest_trust_record.Time); } else if (lsa_forest_trust_record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustDomainInfo) { ForestTrustDomainInformation info = new ForestTrustDomainInformation(lsa_forest_trust_record.Flags, lsa_forest_trust_record.DomainInfo, lsa_forest_trust_record.Time); infos.Add(info); } else if (lsa_forest_trust_record.ForestTrustType != LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustRecordTypeLast) { int length = lsa_forest_trust_record.Data.Length; byte[] destination = new byte[length]; if ((lsa_forest_trust_record.Data.Buffer != IntPtr.Zero) && (length != 0)) { Marshal.Copy(lsa_forest_trust_record.Data.Buffer, destination, 0, length); } list.Add(destination); list2.Add(lsa_forest_trust_record.Time); } } } } finally { System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.LsaFreeMemory(zero); } this.topLevelNames = names; this.excludedNames = strings; this.domainInfo = infos; this.binaryData = list; this.excludedNameTime = hashtable; this.binaryDataTime = list2; this.retrieved = true; } finally { if (flag) { Utils.Revert(); } if (s != IntPtr.Zero) { Marshal.FreeHGlobal(s); } } } catch { throw; } }