DiscretionaryAcl.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // System.Security.AccessControl.DiscretionaryAcl implementation
  3. //
  4. // Authors:
  5. // Dick Porter <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. // James Bellinger <[email protected]>
  8. //
  9. // Copyright (C) 2006-2007 Novell, Inc (http://www.novell.com)
  10. // Copyright (C) 2012 James Bellinger
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Security.Principal;
  32. namespace System.Security.AccessControl
  33. {
  34. public sealed class DiscretionaryAcl : CommonAcl
  35. {
  36. public DiscretionaryAcl (bool isContainer, bool isDS, int capacity)
  37. : base (isContainer, isDS, capacity)
  38. {
  39. }
  40. public DiscretionaryAcl (bool isContainer, bool isDS, RawAcl rawAcl)
  41. : base (isContainer, isDS, rawAcl)
  42. {
  43. }
  44. public DiscretionaryAcl (bool isContainer, bool isDS, byte revision, int capacity)
  45. : base (isContainer, isDS, revision, capacity)
  46. {
  47. }
  48. public void AddAccess (AccessControlType accessType,
  49. SecurityIdentifier sid, int accessMask,
  50. InheritanceFlags inheritanceFlags,
  51. PropagationFlags propagationFlags)
  52. {
  53. AddAce (GetAceQualifier (accessType), sid, accessMask,
  54. inheritanceFlags, propagationFlags, AuditFlags.None);
  55. }
  56. public void AddAccess (AccessControlType accessType,
  57. SecurityIdentifier sid, int accessMask,
  58. InheritanceFlags inheritanceFlags,
  59. PropagationFlags propagationFlags,
  60. ObjectAceFlags objectFlags,
  61. Guid objectType,
  62. Guid inheritedObjectType)
  63. {
  64. AddAce (GetAceQualifier (accessType), sid, accessMask,
  65. inheritanceFlags, propagationFlags, AuditFlags.None,
  66. objectFlags, objectType, inheritedObjectType);
  67. }
  68. public void AddAccess (AccessControlType accessType, SecurityIdentifier sid, ObjectAccessRule rule)
  69. {
  70. AddAccess (accessType, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags, rule.ObjectFlags, rule.ObjectType, rule.InheritedObjectType);
  71. }
  72. [MonoTODO]
  73. public bool RemoveAccess (AccessControlType accessType,
  74. SecurityIdentifier sid,
  75. int accessMask,
  76. InheritanceFlags inheritanceFlags,
  77. PropagationFlags propagationFlags)
  78. {
  79. throw new NotImplementedException ();
  80. }
  81. [MonoTODO]
  82. public bool RemoveAccess (AccessControlType accessType,
  83. SecurityIdentifier sid,
  84. int accessMask,
  85. InheritanceFlags inheritanceFlags,
  86. PropagationFlags propagationFlags,
  87. ObjectAceFlags objectFlags,
  88. Guid objectType,
  89. Guid inheritedObjectType)
  90. {
  91. throw new NotImplementedException ();
  92. }
  93. public bool RemoveAccess (AccessControlType accessType, SecurityIdentifier sid, ObjectAccessRule rule)
  94. {
  95. return RemoveAccess (accessType, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags, rule.ObjectFlags, rule.ObjectType, rule.InheritedObjectType);
  96. }
  97. public void RemoveAccessSpecific (AccessControlType accessType,
  98. SecurityIdentifier sid,
  99. int accessMask,
  100. InheritanceFlags inheritanceFlags,
  101. PropagationFlags propagationFlags)
  102. {
  103. RemoveAceSpecific (GetAceQualifier (accessType), sid, accessMask,
  104. inheritanceFlags, propagationFlags, AuditFlags.None);
  105. }
  106. public void RemoveAccessSpecific (AccessControlType accessType,
  107. SecurityIdentifier sid,
  108. int accessMask,
  109. InheritanceFlags inheritanceFlags,
  110. PropagationFlags propagationFlags,
  111. ObjectAceFlags objectFlags,
  112. Guid objectType,
  113. Guid inheritedObjectType)
  114. {
  115. RemoveAceSpecific (GetAceQualifier (accessType), sid, accessMask,
  116. inheritanceFlags, propagationFlags, AuditFlags.None,
  117. objectFlags, objectType, inheritedObjectType);
  118. }
  119. public void RemoveAccessSpecific (AccessControlType accessType, SecurityIdentifier sid, ObjectAccessRule rule)
  120. {
  121. RemoveAccessSpecific (accessType, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags, rule.ObjectFlags, rule.ObjectType, rule.InheritedObjectType);
  122. }
  123. public void SetAccess (AccessControlType accessType,
  124. SecurityIdentifier sid,
  125. int accessMask,
  126. InheritanceFlags inheritanceFlags,
  127. PropagationFlags propagationFlags)
  128. {
  129. SetAce (GetAceQualifier (accessType), sid, accessMask,
  130. inheritanceFlags, propagationFlags, AuditFlags.None);
  131. }
  132. public void SetAccess (AccessControlType accessType,
  133. SecurityIdentifier sid,
  134. int accessMask,
  135. InheritanceFlags inheritanceFlags,
  136. PropagationFlags propagationFlags,
  137. ObjectAceFlags objectFlags,
  138. Guid objectType,
  139. Guid inheritedObjectType)
  140. {
  141. SetAce (GetAceQualifier (accessType), sid, accessMask,
  142. inheritanceFlags, propagationFlags, AuditFlags.None,
  143. objectFlags, objectType, inheritedObjectType);
  144. }
  145. public void SetAccess (AccessControlType accessType, SecurityIdentifier sid, ObjectAccessRule rule)
  146. {
  147. SetAccess (accessType, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags, rule.ObjectFlags, rule.ObjectType, rule.InheritedObjectType);
  148. }
  149. internal override void ApplyCanonicalSortToExplicitAces ()
  150. {
  151. int explicitCount = GetCanonicalExplicitAceCount ();
  152. int explicitDenys = GetCanonicalExplicitDenyAceCount ();
  153. ApplyCanonicalSortToExplicitAces (0, explicitDenys);
  154. ApplyCanonicalSortToExplicitAces (explicitDenys, explicitCount - explicitDenys);
  155. }
  156. internal override int GetAceInsertPosition (AceQualifier aceQualifier)
  157. {
  158. // Canonical order for DACLs is explicit deny, explicit allow, inherited.
  159. if (AceQualifier.AccessAllowed == aceQualifier)
  160. return GetCanonicalExplicitDenyAceCount ();
  161. else
  162. return 0;
  163. }
  164. static AceQualifier GetAceQualifier (AccessControlType accessType)
  165. {
  166. if (AccessControlType.Allow == accessType)
  167. return AceQualifier.AccessAllowed;
  168. else if (AccessControlType.Deny == accessType)
  169. return AceQualifier.AccessDenied;
  170. else
  171. throw new ArgumentOutOfRangeException ("accessType");
  172. }
  173. internal override bool IsAceMeaningless (GenericAce ace)
  174. {
  175. if (base.IsAceMeaningless (ace)) return true;
  176. if (AuditFlags.None != ace.AuditFlags) return true;
  177. QualifiedAce qace = ace as QualifiedAce;
  178. if (null != qace) {
  179. if (!(AceQualifier.AccessAllowed == qace.AceQualifier ||
  180. AceQualifier.AccessDenied == qace.AceQualifier)) return true;
  181. }
  182. return false;
  183. }
  184. }
  185. }