選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

328 行
14 KiB

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