public static string[] GetAllCentralAccessPolicies(PSObject instance) { unsafe { string[] strArrays; IntPtr zero = IntPtr.Zero; try { int num = 0; int num1 = NativeMethods.LsaQueryCAPs(null, 0, out zero, out num); if (num1 == 0) { if (num == 0 || zero == IntPtr.Zero) { strArrays = null; } else { string[] strArrays1 = new string[num]; IntPtr intPtr = zero; uint num2 = 0; while (num2 < num) { NativeMethods.CENTRAL_ACCESS_POLICY structure = (NativeMethods.CENTRAL_ACCESS_POLICY)Marshal.PtrToStructure(intPtr, typeof(NativeMethods.CENTRAL_ACCESS_POLICY)); strArrays1[num2] = string.Concat("\"", Marshal.PtrToStringUni(structure.Name.Buffer, structure.Name.Length / 2), "\""); IntPtr cAPID = structure.CAPID; bool flag = NativeMethods.IsValidSid(cAPID); if (flag) { SecurityIdentifier securityIdentifier = new SecurityIdentifier(cAPID); string[] strArrays2 = strArrays1; string[] strArrays3 = strArrays2; uint num3 = num2; IntPtr intPtr1 = (IntPtr)num3; strArrays2[num3] = string.Concat(strArrays3[intPtr1.ToInt32()], " (", securityIdentifier.ToString(), ")"); intPtr = intPtr + Marshal.SizeOf(structure); num2++; } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } } strArrays = strArrays1; } } else { throw new Win32Exception(num1); } } finally { NativeMethods.LsaFreeMemory(zero); } return(strArrays); } }
public static string GetCentralAccessPolicyName(PSObject instance) { string stringUni; SecurityIdentifier centralAccessPolicyId = SecurityDescriptorCommandsBase.GetCentralAccessPolicyId(instance); int binaryLength = centralAccessPolicyId.BinaryLength; byte[] numArray = new byte[binaryLength]; centralAccessPolicyId.GetBinaryForm(numArray, 0); IntPtr zero = IntPtr.Zero; IntPtr intPtr = Marshal.AllocHGlobal(binaryLength); try { Marshal.Copy(numArray, 0, intPtr, binaryLength); IntPtr[] intPtrArray = new IntPtr[1]; intPtrArray[0] = intPtr; int num = 0; int num1 = NativeMethods.LsaQueryCAPs(intPtrArray, 1, out zero, out num); if (num1 == 0) { if (num == 0 || zero == IntPtr.Zero) { stringUni = null; } else { NativeMethods.CENTRAL_ACCESS_POLICY structure = (NativeMethods.CENTRAL_ACCESS_POLICY)Marshal.PtrToStructure(zero, typeof(NativeMethods.CENTRAL_ACCESS_POLICY)); stringUni = Marshal.PtrToStringUni(structure.Name.Buffer, structure.Name.Length / 2); } } else { throw new Win32Exception(num1); } } finally { Marshal.FreeHGlobal(intPtr); NativeMethods.LsaFreeMemory(zero); } return(stringUni); }
private IntPtr GetSaclWithCapId(string capStr) { IntPtr zero; IntPtr cAPID = IntPtr.Zero; IntPtr intPtr = IntPtr.Zero; IntPtr zero1 = IntPtr.Zero; bool sid = true; bool flag = true; int num = 0; try { sid = NativeMethods.ConvertStringSidToSid(capStr, out cAPID); if (!sid) { flag = false; int num1 = 0; num = NativeMethods.LsaQueryCAPs(null, 0, out zero1, out num1); if (num == 0) { if (num1 == 0 || zero1 == IntPtr.Zero) { zero = IntPtr.Zero; return(zero); } else { IntPtr intPtr1 = zero1; uint num2 = 0; while (num2 < num1) { NativeMethods.CENTRAL_ACCESS_POLICY structure = (NativeMethods.CENTRAL_ACCESS_POLICY)Marshal.PtrToStructure(intPtr1, typeof(NativeMethods.CENTRAL_ACCESS_POLICY)); string stringUni = Marshal.PtrToStringUni(structure.Name.Buffer, structure.Name.Length / 2); if (!stringUni.Equals(capStr, StringComparison.OrdinalIgnoreCase)) { intPtr1 = intPtr1 + Marshal.SizeOf(structure); num2++; } else { cAPID = structure.CAPID; break; } } } } else { throw new Win32Exception(num); } } if (cAPID != IntPtr.Zero) { sid = NativeMethods.IsValidSid(cAPID); if (sid) { uint lengthSid = NativeMethods.GetLengthSid(cAPID); NativeMethods.ACL aCL = new NativeMethods.ACL(); NativeMethods.SYSTEM_AUDIT_ACE sYSTEMAUDITACE = new NativeMethods.SYSTEM_AUDIT_ACE(); int num3 = (int)((long)(Marshal.SizeOf(aCL) + Marshal.SizeOf(sYSTEMAUDITACE)) + (long)lengthSid - (long)1) & -4; intPtr = Marshal.AllocHGlobal(num3); sid = NativeMethods.InitializeAcl(intPtr, num3, 2); if (sid) { num = NativeMethods.RtlAddScopedPolicyIDAce(intPtr, 2, 3, 0, cAPID); if (num == 0) { return(intPtr); } else { if (num != -1073741811) { throw new Win32Exception(num); } else { throw new ArgumentException(UtilsStrings.InvalidCentralAccessPolicyIdentifier); } } } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } } else { Exception argumentException = new ArgumentException(UtilsStrings.InvalidCentralAccessPolicyIdentifier); base.WriteError(new ErrorRecord(argumentException, "SetAcl_CentralAccessPolicy", ErrorCategory.InvalidArgument, this.AclObject)); zero = IntPtr.Zero; } } finally { if (!sid || num != 0) { Marshal.FreeHGlobal(intPtr); } num = NativeMethods.LsaFreeMemory(zero1); if (flag) { NativeMethods.LocalFree(cAPID); } } return(zero); }