/fstmerge/examples/iFolder/rev7341-7353/left-trunk-7353/UI/Windows/iFolderShell/Win32Security.cs

https://github.com/RoDaniel/featurehouse · C# · 137 lines · 137 code · 0 blank · 0 comment · 10 complexity · 960a0043f72a4573a8d6f194ac597dd8 MD5 · raw file

  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Novell.Win32Util
  4. {
  5. [ComVisible(false)]
  6. public class Win32Security
  7. {
  8. private const int OWNER_SECURITY_INFORMATION = 0x00000001;
  9. private const int GROUP_SECURITY_INFORMATION = 0x00000002;
  10. private const int DACL_SECURITY_INFORMATION = 0x00000004;
  11. private const int ERROR_INSUFFICIENT_BUFFER = 122;
  12. private const int ERROR_ACCESS_DENIED = 5;
  13. private const int TOKEN_ALL_ACCESS = 0x000F01FF;
  14. private enum SECURITY_IMPERSONATION_LEVEL
  15. {
  16. SecurityAnonymous,
  17. SecurityIdentification,
  18. SecurityImpersonation,
  19. SecurityDelegation
  20. }
  21. public static bool AccessAllowed(string path)
  22. {
  23. bool accessAllowed = false;
  24. int lastError;
  25. IntPtr fileSD = IntPtr.Zero;
  26. IntPtr token = IntPtr.Zero;
  27. try
  28. {
  29. int sdLength = 0;
  30. if (!GetFileSecurity(path, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, fileSD, 0, out sdLength))
  31. {
  32. lastError = Marshal.GetLastWin32Error();
  33. if (lastError != ERROR_INSUFFICIENT_BUFFER)
  34. {
  35. return accessAllowed;
  36. }
  37. fileSD = Marshal.AllocHGlobal(sdLength);
  38. if (!GetFileSecurity(path, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, fileSD, sdLength, out sdLength))
  39. {
  40. lastError = Marshal.GetLastWin32Error();
  41. return accessAllowed;
  42. }
  43. GENERIC_MAPPING genericMapping = new GENERIC_MAPPING();
  44. PRIVILEGE_SET privSet = new PRIVILEGE_SET();
  45. uint grantedAccess = 0;
  46. genericMapping.GenericRead = 0x01;
  47. genericMapping.GenericWrite = 0x02;
  48. genericMapping.GenericExecute = 0;
  49. genericMapping.GenericAll = 0x03;
  50. uint desiredAccess = 3;
  51. privSet.Control = 0;
  52. privSet.PrivilegeCount = 0;
  53. int privLen = Marshal.SizeOf(privSet);
  54. if (!ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation))
  55. {
  56. lastError = Marshal.GetLastWin32Error();
  57. return accessAllowed;
  58. }
  59. try
  60. {
  61. if (!OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, true, ref token))
  62. {
  63. lastError = Marshal.GetLastWin32Error();
  64. return accessAllowed;
  65. }
  66. if (!AccessCheck(fileSD, token, desiredAccess, ref genericMapping, ref privSet, ref privLen, ref grantedAccess, ref accessAllowed))
  67. {
  68. lastError = Marshal.GetLastWin32Error();
  69. }
  70. }
  71. finally
  72. {
  73. RevertToSelf();
  74. }
  75. }
  76. }
  77. finally
  78. {
  79. if (fileSD != IntPtr.Zero)
  80. {
  81. Marshal.FreeHGlobal(fileSD);
  82. }
  83. if (!token.Equals(IntPtr.Zero))
  84. {
  85. CloseHandle(token);
  86. token = IntPtr.Zero;
  87. }
  88. }
  89. return accessAllowed;
  90. }
  91. [DllImport("advapi32.dll", CallingConvention=CallingConvention.Winapi, SetLastError=true, CharSet=CharSet.Unicode)]
  92. static extern bool GetFileSecurity(string fileName, int RequestedInformation, IntPtr pSecurityDescriptor, int length, out int lengthNeeded);
  93. [DllImport("advapi32.dll", SetLastError=true)]
  94. static extern bool ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel);
  95. [DllImport("advapi32.dll", SetLastError=true)]
  96. static extern bool RevertToSelf();
  97. [DllImport("advapi32.dll", SetLastError=true)]
  98. static extern bool OpenThreadToken(IntPtr ThreadHandle, uint DesiredAccess, bool OpenAsSelf, ref IntPtr TokenHandle);
  99. [DllImport("advapi32.dll", SetLastError=true)]
  100. static extern bool AccessCheck(IntPtr pSecurityDescriptor, IntPtr ClientToken, uint DesiredAccess, ref GENERIC_MAPPING GenericMapping, ref PRIVILEGE_SET PrivilegeSet, ref int PrivilegeSetLength, ref uint GrantedAccess, ref bool AccessStatus);
  101. [DllImport("kernel32.dll")]
  102. static extern IntPtr GetCurrentThread();
  103. [DllImport("kernel32.dll")]
  104. static extern bool CloseHandle(IntPtr hObject);
  105. [StructLayout(LayoutKind.Sequential)]
  106. [ComVisible(false)]
  107. public struct GENERIC_MAPPING
  108. {
  109. public uint GenericRead;
  110. public uint GenericWrite;
  111. public uint GenericExecute;
  112. public uint GenericAll;
  113. }
  114. [StructLayout(LayoutKind.Sequential)]
  115. [ComVisible(false)]
  116. public struct PRIVILEGE_SET
  117. {
  118. public uint PrivilegeCount;
  119. public uint Control;
  120. LUID_AND_ATTRIBUTES Privilege;
  121. }
  122. [StructLayout(LayoutKind.Sequential)]
  123. [ComVisible(false)]
  124. public struct LUID_AND_ATTRIBUTES
  125. {
  126. public LUID Luid;
  127. public uint Attributes;
  128. }
  129. [StructLayout(LayoutKind.Sequential)]
  130. [ComVisible(false)]
  131. public struct LUID
  132. {
  133. public uint LowPart;
  134. public long HighPart;
  135. }
  136. }
  137. }