Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

FileSystemSecurity2.cs 13 KiB

8 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using Alphaleonis.Win32.Filesystem;
  2. using System;
  3. using System.Security.AccessControl;
  4. namespace Security2
  5. {
  6. public class FileSystemSecurity2
  7. {
  8. protected FileSecurity fileSecurityDescriptor;
  9. protected DirectorySecurity directorySecurityDescriptor;
  10. protected FileSystemInfo item;
  11. protected FileSystemSecurity sd;
  12. protected AccessControlSections sections;
  13. protected bool isFile = false;
  14. public FileSystemInfo Item
  15. {
  16. get { return item; }
  17. set { item = value; }
  18. }
  19. public string FullName { get { return item.FullName; } }
  20. public string Name { get { return item.Name; } }
  21. public bool IsFile { get { return isFile; } }
  22. public FileSystemSecurity2(FileSystemInfo item, AccessControlSections sections)
  23. {
  24. this.sections = sections;
  25. if (item is FileInfo)
  26. {
  27. this.item = (FileInfo)item;
  28. sd = ((FileInfo)this.item).GetAccessControl(sections);
  29. isFile = true;
  30. }
  31. else
  32. {
  33. this.item = (DirectoryInfo)item;
  34. sd = ((DirectoryInfo)this.item).GetAccessControl(sections);
  35. }
  36. }
  37. public FileSystemSecurity2(FileSystemInfo item)
  38. {
  39. if (item is FileInfo)
  40. {
  41. this.item = (FileInfo)item;
  42. try
  43. {
  44. sd = ((FileInfo)this.item).GetAccessControl(AccessControlSections.All);
  45. }
  46. catch
  47. {
  48. try
  49. {
  50. sd = ((FileInfo)this.item).GetAccessControl(AccessControlSections.Access | AccessControlSections.Owner | AccessControlSections.Group);
  51. }
  52. catch
  53. {
  54. sd = ((FileInfo)this.item).GetAccessControl(AccessControlSections.Access);
  55. }
  56. }
  57. isFile = true;
  58. }
  59. else
  60. {
  61. this.item = (DirectoryInfo)item;
  62. try
  63. {
  64. sd = ((DirectoryInfo)this.item).GetAccessControl(AccessControlSections.All);
  65. }
  66. catch
  67. {
  68. try
  69. {
  70. sd = ((DirectoryInfo)this.item).GetAccessControl(AccessControlSections.Access | AccessControlSections.Owner | AccessControlSections.Group);
  71. }
  72. catch
  73. {
  74. sd = ((DirectoryInfo)this.item).GetAccessControl(AccessControlSections.Access);
  75. }
  76. }
  77. }
  78. }
  79. public FileSystemSecurity SecurityDescriptor
  80. {
  81. get
  82. {
  83. return sd;
  84. }
  85. }
  86. public void Write()
  87. {
  88. if (isFile)
  89. {
  90. ((FileInfo)item).SetAccessControl((FileSecurity)sd);
  91. }
  92. else
  93. {
  94. ((DirectoryInfo)item).SetAccessControl((DirectorySecurity)sd);
  95. }
  96. }
  97. public void Write(FileSystemInfo item)
  98. {
  99. if (item is FileInfo)
  100. {
  101. ((FileInfo)item).SetAccessControl((FileSecurity)sd);
  102. }
  103. else
  104. {
  105. ((DirectoryInfo)item).SetAccessControl((DirectorySecurity)sd);
  106. }
  107. }
  108. public void Write(string path)
  109. {
  110. FileSystemInfo item = null;
  111. if (File.Exists(path))
  112. {
  113. item = new FileInfo(path);
  114. }
  115. else if (Directory.Exists(path))
  116. {
  117. item = new DirectoryInfo(path);
  118. }
  119. else
  120. {
  121. throw new System.IO.FileNotFoundException("File not found", path);
  122. }
  123. Write(item);
  124. }
  125. #region Conversion
  126. public static implicit operator FileSecurity(FileSystemSecurity2 fs2)
  127. {
  128. return fs2.fileSecurityDescriptor;
  129. }
  130. public static implicit operator FileSystemSecurity2(FileSecurity fs)
  131. {
  132. return new FileSystemSecurity2(new FileInfo(""));
  133. }
  134. public static implicit operator DirectorySecurity(FileSystemSecurity2 fs2)
  135. {
  136. return fs2.directorySecurityDescriptor;
  137. }
  138. public static implicit operator FileSystemSecurity2(DirectorySecurity fs)
  139. {
  140. return new FileSystemSecurity2(new DirectoryInfo(""));
  141. }
  142. //REQUIRED BECAUSE OF CONVERSION OPERATORS
  143. public override bool Equals(object obj)
  144. {
  145. return this.fileSecurityDescriptor == (FileSecurity)obj;
  146. }
  147. public override int GetHashCode()
  148. {
  149. return fileSecurityDescriptor.GetHashCode();
  150. }
  151. #endregion
  152. public static void ConvertToFileSystemFlags(ApplyTo ApplyTo, out InheritanceFlags inheritanceFlags, out PropagationFlags propagationFlags)
  153. {
  154. inheritanceFlags = InheritanceFlags.None;
  155. propagationFlags = PropagationFlags.None;
  156. switch (ApplyTo)
  157. {
  158. case ApplyTo.FilesOnly:
  159. inheritanceFlags = InheritanceFlags.ObjectInherit;
  160. propagationFlags = PropagationFlags.InheritOnly;
  161. break;
  162. case ApplyTo.SubfoldersAndFilesOnly:
  163. inheritanceFlags = InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit;
  164. propagationFlags = PropagationFlags.InheritOnly;
  165. break;
  166. case ApplyTo.SubfoldersOnly:
  167. inheritanceFlags = InheritanceFlags.ContainerInherit;
  168. propagationFlags = PropagationFlags.InheritOnly;
  169. break;
  170. case ApplyTo.ThisFolderAndFiles:
  171. inheritanceFlags = InheritanceFlags.ObjectInherit;
  172. propagationFlags = PropagationFlags.None;
  173. break;
  174. case ApplyTo.ThisFolderAndSubfolders:
  175. inheritanceFlags = InheritanceFlags.ContainerInherit;
  176. propagationFlags = PropagationFlags.None;
  177. break;
  178. case ApplyTo.ThisFolderOnly:
  179. inheritanceFlags = InheritanceFlags.None;
  180. propagationFlags = PropagationFlags.None;
  181. break;
  182. case ApplyTo.ThisFolderSubfoldersAndFiles:
  183. inheritanceFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
  184. propagationFlags = PropagationFlags.None;
  185. break;
  186. case ApplyTo.FilesOnlyOneLevel:
  187. inheritanceFlags = InheritanceFlags.ObjectInherit;
  188. propagationFlags = PropagationFlags.InheritOnly | PropagationFlags.NoPropagateInherit;
  189. break;
  190. case ApplyTo.SubfoldersAndFilesOnlyOneLevel:
  191. inheritanceFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
  192. propagationFlags = PropagationFlags.InheritOnly | PropagationFlags.NoPropagateInherit;
  193. break;
  194. case ApplyTo.SubfoldersOnlyOneLevel:
  195. inheritanceFlags = InheritanceFlags.ContainerInherit;
  196. propagationFlags = PropagationFlags.InheritOnly | PropagationFlags.NoPropagateInherit;
  197. break;
  198. case ApplyTo.ThisFolderAndFilesOneLevel:
  199. inheritanceFlags = InheritanceFlags.ObjectInherit;
  200. propagationFlags = PropagationFlags.NoPropagateInherit;
  201. break;
  202. case ApplyTo.ThisFolderAndSubfoldersOneLevel:
  203. inheritanceFlags = InheritanceFlags.ContainerInherit;
  204. propagationFlags = PropagationFlags.NoPropagateInherit;
  205. break;
  206. case ApplyTo.ThisFolderSubfoldersAndFilesOneLevel:
  207. inheritanceFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
  208. propagationFlags = PropagationFlags.NoPropagateInherit;
  209. break;
  210. }
  211. }
  212. public static ApplyTo ConvertToApplyTo(InheritanceFlags InheritanceFlags, PropagationFlags PropagationFlags)
  213. {
  214. if (InheritanceFlags == InheritanceFlags.ObjectInherit & PropagationFlags == PropagationFlags.InheritOnly)
  215. return ApplyTo.FilesOnly;
  216. else if (InheritanceFlags == (InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit) & PropagationFlags == PropagationFlags.InheritOnly)
  217. return ApplyTo.SubfoldersAndFilesOnly;
  218. else if (InheritanceFlags == InheritanceFlags.ContainerInherit & PropagationFlags == PropagationFlags.InheritOnly)
  219. return ApplyTo.SubfoldersOnly;
  220. else if (InheritanceFlags == InheritanceFlags.ObjectInherit & PropagationFlags == PropagationFlags.None)
  221. return ApplyTo.ThisFolderAndFiles;
  222. else if (InheritanceFlags == InheritanceFlags.ContainerInherit & PropagationFlags == PropagationFlags.None)
  223. return ApplyTo.ThisFolderAndSubfolders;
  224. else if (InheritanceFlags == InheritanceFlags.None & PropagationFlags == PropagationFlags.None)
  225. return ApplyTo.ThisFolderOnly;
  226. else if (InheritanceFlags == (InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit) & PropagationFlags == PropagationFlags.None)
  227. return ApplyTo.ThisFolderSubfoldersAndFiles;
  228. else if (InheritanceFlags == (InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit) & PropagationFlags == PropagationFlags.NoPropagateInherit)
  229. return ApplyTo.ThisFolderSubfoldersAndFilesOneLevel;
  230. else if (InheritanceFlags == InheritanceFlags.ContainerInherit & PropagationFlags == PropagationFlags.NoPropagateInherit)
  231. return ApplyTo.ThisFolderAndSubfoldersOneLevel;
  232. else if (InheritanceFlags == InheritanceFlags.ObjectInherit & PropagationFlags == PropagationFlags.NoPropagateInherit)
  233. return ApplyTo.ThisFolderAndFilesOneLevel;
  234. else if (InheritanceFlags == (InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit) & PropagationFlags == (PropagationFlags.InheritOnly | PropagationFlags.NoPropagateInherit))
  235. return ApplyTo.SubfoldersAndFilesOnlyOneLevel;
  236. else if (InheritanceFlags == InheritanceFlags.ContainerInherit & PropagationFlags == (PropagationFlags.InheritOnly | PropagationFlags.NoPropagateInherit))
  237. return ApplyTo.SubfoldersOnlyOneLevel;
  238. else if (InheritanceFlags == InheritanceFlags.ObjectInherit & PropagationFlags == (PropagationFlags.InheritOnly | PropagationFlags.NoPropagateInherit))
  239. return ApplyTo.FilesOnlyOneLevel;
  240. throw new RightsConverionException("The combination of InheritanceFlags and PropagationFlags could not be translated");
  241. }
  242. public static FileSystemRights MapGenericRightsToFileSystemRights(uint originalRights)
  243. {
  244. try
  245. {
  246. var r = Enum.Parse(typeof(FileSystemRights), (originalRights).ToString());
  247. if (r.ToString() == originalRights.ToString())
  248. {
  249. throw new ArgumentOutOfRangeException();
  250. }
  251. var fileSystemRights = (FileSystemRights)originalRights;
  252. return fileSystemRights;
  253. }
  254. catch (Exception)
  255. {
  256. FileSystemRights rights = 0;
  257. if (Convert.ToBoolean(originalRights & (uint)GenericRights.GENERIC_EXECUTE))
  258. {
  259. rights |= (FileSystemRights)MappedGenericRights.FILE_GENERIC_EXECUTE;
  260. originalRights ^= (uint)GenericRights.GENERIC_EXECUTE;
  261. }
  262. if (Convert.ToBoolean(originalRights & (uint)GenericRights.GENERIC_READ))
  263. {
  264. rights |= (FileSystemRights)MappedGenericRights.FILE_GENERIC_READ;
  265. originalRights ^= (uint)GenericRights.GENERIC_READ;
  266. }
  267. if (Convert.ToBoolean(originalRights & (uint)GenericRights.GENERIC_WRITE))
  268. {
  269. rights |= (FileSystemRights)MappedGenericRights.FILE_GENERIC_WRITE;
  270. originalRights ^= (uint)GenericRights.GENERIC_WRITE;
  271. }
  272. if (Convert.ToBoolean(originalRights & (uint)GenericRights.GENERIC_ALL))
  273. {
  274. rights |= (FileSystemRights)MappedGenericRights.FILE_GENERIC_ALL;
  275. originalRights ^= (uint)GenericRights.GENERIC_ALL;
  276. }
  277. //throw new RightsConverionException("Cannot convert GenericRights into FileSystemRights");
  278. var remainingRights = (FileSystemRights)Enum.Parse(typeof(FileSystemRights), (originalRights).ToString());
  279. rights |= remainingRights;
  280. return rights;
  281. }
  282. }
  283. }
  284. }