Example #1
0
        internal static IntPtr GetMachineDomainSid()
        {
            IntPtr pPolicyHandle = IntPtr.Zero;
            IntPtr pBuffer       = IntPtr.Zero;
            IntPtr pOA           = IntPtr.Zero;

            try
            {
                UnsafeNativeMethods.LSA_OBJECT_ATTRIBUTES oa = new UnsafeNativeMethods.LSA_OBJECT_ATTRIBUTES();

                pOA = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UnsafeNativeMethods.LSA_OBJECT_ATTRIBUTES)));
                Marshal.StructureToPtr(oa, pOA, false);
                int err = UnsafeNativeMethods.LsaOpenPolicy(
                    IntPtr.Zero,
                    pOA,
                    1,                      // POLICY_VIEW_LOCAL_INFORMATION
                    ref pPolicyHandle);

                if (err != 0)
                {
                    GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "GetMachineDomainSid: LsaOpenPolicy failed, gle=" + SafeNativeMethods.LsaNtStatusToWinError(err));

                    throw new PrincipalOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                        StringResources.UnableToRetrievePolicy,
                                                                        SafeNativeMethods.LsaNtStatusToWinError(err)));
                }

                Debug.Assert(pPolicyHandle != IntPtr.Zero);
                err = UnsafeNativeMethods.LsaQueryInformationPolicy(
                    pPolicyHandle,
                    5,                          // PolicyAccountDomainInformation
                    ref pBuffer);

                if (err != 0)
                {
                    GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "GetMachineDomainSid: LsaQueryInformationPolicy failed, gle=" + SafeNativeMethods.LsaNtStatusToWinError(err));

                    throw new PrincipalOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                        StringResources.UnableToRetrievePolicy,
                                                                        SafeNativeMethods.LsaNtStatusToWinError(err)));
                }

                Debug.Assert(pBuffer != IntPtr.Zero);
                UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO info = (UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO)
                                                                      Marshal.PtrToStructure(pBuffer, typeof(UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO));

                Debug.Assert(UnsafeNativeMethods.IsValidSid(info.domainSid));

                // Now we make a copy of the SID to return
                int    sidLength  = UnsafeNativeMethods.GetLengthSid(info.domainSid);
                IntPtr pCopyOfSid = Marshal.AllocHGlobal(sidLength);
                bool   success    = UnsafeNativeMethods.CopySid(sidLength, pCopyOfSid, info.domainSid);
                if (!success)
                {
                    int lastError = Marshal.GetLastWin32Error();
                    GlobalDebug.WriteLineIf(GlobalDebug.Error,
                                            "Utils",
                                            "GetMachineDomainSid: CopySid failed, errorcode=" + lastError);

                    throw new PrincipalOperationException(
                              String.Format(CultureInfo.CurrentCulture, StringResources.UnableToRetrievePolicy, lastError));
                }

                return(pCopyOfSid);
            }
            finally
            {
                if (pPolicyHandle != IntPtr.Zero)
                {
                    UnsafeNativeMethods.LsaClose(pPolicyHandle);
                }

                if (pBuffer != IntPtr.Zero)
                {
                    UnsafeNativeMethods.LsaFreeMemory(pBuffer);
                }

                if (pOA != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pOA);
                }
            }
        }
Example #2
0
        internal static IntPtr GetMachineDomainSid()
        {
            IntPtr intPtr;
            IntPtr zero    = IntPtr.Zero;
            IntPtr zero1   = IntPtr.Zero;
            IntPtr intPtr1 = IntPtr.Zero;

            try
            {
                UnsafeNativeMethods.LSA_OBJECT_ATTRIBUTES lSAOBJECTATTRIBUTE = new UnsafeNativeMethods.LSA_OBJECT_ATTRIBUTES();
                intPtr1 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UnsafeNativeMethods.LSA_OBJECT_ATTRIBUTES)));
                Marshal.StructureToPtr(lSAOBJECTATTRIBUTE, intPtr1, false);
                int num = UnsafeNativeMethods.LsaOpenPolicy(IntPtr.Zero, intPtr1, 1, ref zero);
                if (num == 0)
                {
                    num = UnsafeNativeMethods.LsaQueryInformationPolicy(zero, 5, ref zero1);
                    if (num == 0)
                    {
                        UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO structure = (UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO)Marshal.PtrToStructure(zero1, typeof(UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO));
                        int    lengthSid = UnsafeNativeMethods.GetLengthSid(structure.domainSid);
                        IntPtr intPtr2   = Marshal.AllocHGlobal(lengthSid);
                        bool   flag      = UnsafeNativeMethods.CopySid(lengthSid, intPtr2, structure.domainSid);
                        if (flag)
                        {
                            intPtr = intPtr2;
                        }
                        else
                        {
                            int      lastWin32Error = Marshal.GetLastWin32Error();
                            object[] objArray       = new object[1];
                            objArray[0] = lastWin32Error;
                            throw new PrincipalOperationException(string.Format(CultureInfo.CurrentCulture, StringResources.UnableToRetrievePolicy, objArray));
                        }
                    }
                    else
                    {
                        object[] winError = new object[1];
                        winError[0] = SafeNativeMethods.LsaNtStatusToWinError(num);
                        throw new PrincipalOperationException(string.Format(CultureInfo.CurrentCulture, StringResources.UnableToRetrievePolicy, winError));
                    }
                }
                else
                {
                    object[] winError1 = new object[1];
                    winError1[0] = SafeNativeMethods.LsaNtStatusToWinError(num);
                    throw new PrincipalOperationException(string.Format(CultureInfo.CurrentCulture, StringResources.UnableToRetrievePolicy, winError1));
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    UnsafeNativeMethods.LsaClose(zero);
                }
                if (zero1 != IntPtr.Zero)
                {
                    UnsafeNativeMethods.LsaFreeMemory(zero1);
                }
                if (intPtr1 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(intPtr1);
                }
            }
            return(intPtr);
        }