code_junkie

Getting access denied with GetEffectiveRightsFromAcl()..?

Nov 12th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. private enum TRUSTEE_FORM
  2. {
  3. TRUSTEE_IS_SID,
  4. TRUSTEE_IS_NAME,
  5. TRUSTEE_BAD_FORM,
  6. TRUSTEE_IS_OBJECTS_AND_SID,
  7. TRUSTEE_IS_OBJECTS_AND_NAME
  8. }
  9.  
  10. private enum TRUSTEE_TYPE
  11. {
  12. TRUSTEE_IS_UNKNOWN,
  13. TRUSTEE_IS_USER,
  14. TRUSTEE_IS_GROUP,
  15. TRUSTEE_IS_DOMAIN,
  16. TRUSTEE_IS_ALIAS,
  17. TRUSTEE_IS_WELL_KNOWN_GROUP,
  18. TRUSTEE_IS_DELETED,
  19. TRUSTEE_IS_INVALID,
  20. TRUSTEE_IS_COMPUTER
  21. }
  22.  
  23. private struct TRUSTEE
  24. {
  25. public IntPtr pMultipleTrustee;
  26. public MULTIPLE_TRUSTEE_OPERATION MultipleTrusteeOperation;
  27. public TRUSTEE_FORM TrusteeForm;
  28. public TRUSTEE_TYPE TrusteeType;
  29. public IntPtr ptstrName;
  30. }
  31.  
  32. [DllImport("advapi32.dll", SetLastError = true)]
  33. private static extern void BuildTrusteeWithSid(
  34. ref TRUSTEE pTrustee,
  35. byte[] sid
  36. );
  37.  
  38. [DllImport("advapi32.dll")]
  39. private static extern uint GetEffectiveRightsFromAcl(byte[] pacl, ref TRUSTEE
  40. pTrustee, ref uint pAccessRights);
  41.  
  42. public bool HasAccess(SecurityIdentifier sid)
  43. {
  44.  
  45. DiscretionaryAcl dacl = <DACL from somewhere>;
  46.  
  47. byte[] daclBuffer = new byte[dacl.BinaryLength];
  48. dacl.GetBinaryForm(daclBuffer, 0);
  49.  
  50. byte[] sidBuffer = new byte[sid.BinaryLength];
  51. sid.GetBinaryForm(sidBuffer, 0);
  52.  
  53. TRUSTEE t = new TRUSTEE();
  54. BuildTrusteeWithSid(ref t, sidBuffer);
  55.  
  56. uint access = 0;
  57. uint hr = GetEffectiveRightsFromAcl(daclBuffer, ref t, ref access);
  58.  
  59. int i = Marshal.Release(t.ptstrName);
  60.  
  61. return ((access | <Desired Access>) == <Desired Access>) ? true : false;
  62. }
  63. }
Add Comment
Please, Sign In to add comment