Exemple #1
0
        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; }
        }
Exemple #2
0
        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;
            }
        }