Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

315 rindas
13 KiB

  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. }