ObjectSecurity.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. //
  2. // System.Security.AccessControl.ObjectSecurity implementation
  3. //
  4. // Authors:
  5. // Dick Porter <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. // James Bellinger <[email protected]>
  8. //
  9. // Copyright (C) 2005-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.Collections.Generic;
  32. using System.Security.Principal;
  33. using System.Runtime.InteropServices;
  34. using System.Threading;
  35. using System.Runtime.ExceptionServices;
  36. namespace System.Security.AccessControl
  37. {
  38. public abstract class ObjectSecurity
  39. {
  40. protected ObjectSecurity ()
  41. {
  42. }
  43. protected ObjectSecurity (CommonSecurityDescriptor securityDescriptor)
  44. {
  45. if (securityDescriptor == null)
  46. throw new ArgumentNullException ("securityDescriptor");
  47. descriptor = securityDescriptor;
  48. rw_lock = new ReaderWriterLock ();
  49. }
  50. protected ObjectSecurity (bool isContainer, bool isDS)
  51. : this (new CommonSecurityDescriptor
  52. (isContainer, isDS, ControlFlags.None, null, null, null,
  53. new DiscretionaryAcl (isContainer, isDS, 0)))
  54. {
  55. }
  56. internal CommonSecurityDescriptor descriptor;
  57. AccessControlSections sections_modified;
  58. ReaderWriterLock rw_lock;
  59. public abstract Type AccessRightType { get; }
  60. public abstract Type AccessRuleType { get; }
  61. public abstract Type AuditRuleType { get; }
  62. public bool AreAccessRulesCanonical {
  63. get {
  64. ReadLock ();
  65. try {
  66. return descriptor.IsDiscretionaryAclCanonical;
  67. } finally {
  68. ReadUnlock ();
  69. }
  70. }
  71. }
  72. public bool AreAccessRulesProtected {
  73. get {
  74. ReadLock ();
  75. try {
  76. return 0 != (descriptor.ControlFlags & ControlFlags.DiscretionaryAclProtected);
  77. } finally {
  78. ReadUnlock ();
  79. }
  80. }
  81. }
  82. public bool AreAuditRulesCanonical {
  83. get {
  84. ReadLock ();
  85. try {
  86. return descriptor.IsSystemAclCanonical;
  87. } finally {
  88. ReadUnlock ();
  89. }
  90. }
  91. }
  92. public bool AreAuditRulesProtected {
  93. get {
  94. ReadLock ();
  95. try {
  96. return 0 != (descriptor.ControlFlags & ControlFlags.SystemAclProtected);
  97. } finally {
  98. ReadUnlock ();
  99. }
  100. }
  101. }
  102. internal AccessControlSections AccessControlSectionsModified {
  103. get { Reading (); return sections_modified; }
  104. set { Writing (); sections_modified = value; }
  105. }
  106. protected bool AccessRulesModified {
  107. get { return AreAccessControlSectionsModified (AccessControlSections.Access); }
  108. set { SetAccessControlSectionsModified (AccessControlSections.Access, value); }
  109. }
  110. protected bool AuditRulesModified {
  111. get { return AreAccessControlSectionsModified (AccessControlSections.Audit); }
  112. set { SetAccessControlSectionsModified (AccessControlSections.Audit, value); }
  113. }
  114. protected bool GroupModified {
  115. get { return AreAccessControlSectionsModified (AccessControlSections.Group); }
  116. set { SetAccessControlSectionsModified (AccessControlSections.Group, value); }
  117. }
  118. protected bool IsContainer {
  119. get { return descriptor.IsContainer; }
  120. }
  121. protected bool IsDS {
  122. get { return descriptor.IsDS; }
  123. }
  124. protected bool OwnerModified {
  125. get { return AreAccessControlSectionsModified (AccessControlSections.Owner); }
  126. set { SetAccessControlSectionsModified (AccessControlSections.Owner, value); }
  127. }
  128. public abstract AccessRule AccessRuleFactory (IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type);
  129. public abstract AuditRule AuditRuleFactory (IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags);
  130. public IdentityReference GetGroup (Type targetType)
  131. {
  132. ReadLock ();
  133. try {
  134. if (descriptor.Group == null)
  135. return null;
  136. return descriptor.Group.Translate (targetType);
  137. } finally {
  138. ReadUnlock ();
  139. }
  140. }
  141. public IdentityReference GetOwner (Type targetType)
  142. {
  143. ReadLock ();
  144. try {
  145. if (descriptor.Owner == null)
  146. return null;
  147. return descriptor.Owner.Translate (targetType);
  148. } finally {
  149. ReadUnlock ();
  150. }
  151. }
  152. public byte[] GetSecurityDescriptorBinaryForm ()
  153. {
  154. ReadLock ();
  155. try {
  156. byte[] binaryForm = new byte[descriptor.BinaryLength];
  157. descriptor.GetBinaryForm (binaryForm, 0);
  158. return binaryForm;
  159. } finally {
  160. ReadUnlock ();
  161. }
  162. }
  163. public string GetSecurityDescriptorSddlForm (AccessControlSections includeSections)
  164. {
  165. ReadLock ();
  166. try {
  167. return descriptor.GetSddlForm (includeSections);
  168. } finally {
  169. ReadUnlock ();
  170. }
  171. }
  172. public static bool IsSddlConversionSupported ()
  173. {
  174. return GenericSecurityDescriptor.IsSddlConversionSupported ();
  175. }
  176. public virtual bool ModifyAccessRule (AccessControlModification modification, AccessRule rule, out bool modified)
  177. {
  178. if (rule == null)
  179. throw new ArgumentNullException ("rule");
  180. if (!AccessRuleType.IsAssignableFrom (rule.GetType()))
  181. throw new ArgumentException ("rule");
  182. return ModifyAccess (modification, rule, out modified);
  183. }
  184. public virtual bool ModifyAuditRule (AccessControlModification modification, AuditRule rule, out bool modified)
  185. {
  186. if (rule == null)
  187. throw new ArgumentNullException ("rule");
  188. if (!AuditRuleType.IsAssignableFrom (rule.GetType()))
  189. throw new ArgumentException ("rule");
  190. return ModifyAudit (modification, rule, out modified);
  191. }
  192. public virtual void PurgeAccessRules (IdentityReference identity)
  193. {
  194. if (null == identity)
  195. throw new ArgumentNullException ("identity");
  196. WriteLock ();
  197. try {
  198. descriptor.PurgeAccessControl (SidFromIR (identity));
  199. } finally {
  200. WriteUnlock ();
  201. }
  202. }
  203. public virtual void PurgeAuditRules (IdentityReference identity)
  204. {
  205. if (null == identity)
  206. throw new ArgumentNullException ("identity");
  207. WriteLock ();
  208. try {
  209. descriptor.PurgeAudit (SidFromIR (identity));
  210. } finally {
  211. WriteUnlock ();
  212. }
  213. }
  214. public void SetAccessRuleProtection (bool isProtected,
  215. bool preserveInheritance)
  216. {
  217. WriteLock ();
  218. try {
  219. descriptor.SetDiscretionaryAclProtection (isProtected, preserveInheritance);
  220. } finally {
  221. WriteUnlock();
  222. }
  223. }
  224. public void SetAuditRuleProtection (bool isProtected,
  225. bool preserveInheritance)
  226. {
  227. WriteLock ();
  228. try {
  229. descriptor.SetSystemAclProtection (isProtected, preserveInheritance);
  230. } finally {
  231. WriteUnlock ();
  232. }
  233. }
  234. public void SetGroup (IdentityReference identity)
  235. {
  236. WriteLock ();
  237. try {
  238. descriptor.Group = SidFromIR (identity);
  239. GroupModified = true;
  240. } finally {
  241. WriteUnlock ();
  242. }
  243. }
  244. public void SetOwner (IdentityReference identity)
  245. {
  246. WriteLock ();
  247. try {
  248. descriptor.Owner = SidFromIR (identity);
  249. OwnerModified = true;
  250. } finally {
  251. WriteUnlock ();
  252. }
  253. }
  254. public void SetSecurityDescriptorBinaryForm (byte[] binaryForm)
  255. {
  256. SetSecurityDescriptorBinaryForm (binaryForm, AccessControlSections.All);
  257. }
  258. public void SetSecurityDescriptorBinaryForm (byte[] binaryForm, AccessControlSections includeSections)
  259. {
  260. CopySddlForm (new CommonSecurityDescriptor (IsContainer, IsDS, binaryForm, 0), includeSections);
  261. }
  262. public void SetSecurityDescriptorSddlForm (string sddlForm)
  263. {
  264. SetSecurityDescriptorSddlForm (sddlForm, AccessControlSections.All);
  265. }
  266. public void SetSecurityDescriptorSddlForm (string sddlForm, AccessControlSections includeSections)
  267. {
  268. CopySddlForm (new CommonSecurityDescriptor (IsContainer, IsDS, sddlForm), includeSections);
  269. }
  270. void CopySddlForm (CommonSecurityDescriptor sourceDescriptor, AccessControlSections includeSections)
  271. {
  272. WriteLock ();
  273. try {
  274. AccessControlSectionsModified |= includeSections;
  275. if (0 != (includeSections & AccessControlSections.Audit))
  276. descriptor.SystemAcl = sourceDescriptor.SystemAcl;
  277. if (0 != (includeSections & AccessControlSections.Access))
  278. descriptor.DiscretionaryAcl = sourceDescriptor.DiscretionaryAcl;
  279. if (0 != (includeSections & AccessControlSections.Owner))
  280. descriptor.Owner = sourceDescriptor.Owner;
  281. if (0 != (includeSections & AccessControlSections.Group))
  282. descriptor.Group = sourceDescriptor.Group;
  283. } finally {
  284. WriteUnlock ();
  285. }
  286. }
  287. protected abstract bool ModifyAccess (AccessControlModification modification, AccessRule rule, out bool modified);
  288. protected abstract bool ModifyAudit (AccessControlModification modification, AuditRule rule, out bool modified);
  289. // For MoMA. NotImplementedException is correct for this base class.
  290. Exception GetNotImplementedException ()
  291. {
  292. return new NotImplementedException ();
  293. }
  294. protected virtual void Persist (SafeHandle handle, AccessControlSections includeSections)
  295. {
  296. throw GetNotImplementedException ();
  297. }
  298. protected virtual void Persist (string name, AccessControlSections includeSections)
  299. {
  300. throw GetNotImplementedException ();
  301. }
  302. [MonoTODO]
  303. [HandleProcessCorruptedStateExceptions]
  304. protected virtual void Persist (bool enableOwnershipPrivilege, string name, AccessControlSections includeSections)
  305. {
  306. throw new NotImplementedException ();
  307. }
  308. void Reading ()
  309. {
  310. if (!rw_lock.IsReaderLockHeld && !rw_lock.IsWriterLockHeld)
  311. throw new InvalidOperationException ("Either a read or a write lock must be held.");
  312. }
  313. protected void ReadLock ()
  314. {
  315. rw_lock.AcquireReaderLock (Timeout.Infinite);
  316. }
  317. protected void ReadUnlock ()
  318. {
  319. rw_lock.ReleaseReaderLock ();
  320. }
  321. void Writing ()
  322. {
  323. if (!rw_lock.IsWriterLockHeld)
  324. throw new InvalidOperationException ("Write lock must be held.");
  325. }
  326. protected void WriteLock ()
  327. {
  328. rw_lock.AcquireWriterLock (Timeout.Infinite);
  329. }
  330. protected void WriteUnlock ()
  331. {
  332. rw_lock.ReleaseWriterLock ();
  333. }
  334. internal AuthorizationRuleCollection InternalGetAccessRules (bool includeExplicit,
  335. bool includeInherited,
  336. Type targetType)
  337. {
  338. List<AuthorizationRule> rules = new List<AuthorizationRule> ();
  339. ReadLock ();
  340. try {
  341. foreach (GenericAce genericAce in descriptor.DiscretionaryAcl) {
  342. QualifiedAce ace = genericAce as QualifiedAce;
  343. if (null == ace) continue;
  344. if (ace.IsInherited && !includeInherited) continue;
  345. if (!ace.IsInherited && !includeExplicit) continue;
  346. AccessControlType type;
  347. if (AceQualifier.AccessAllowed == ace.AceQualifier)
  348. type = AccessControlType.Allow;
  349. else if (AceQualifier.AccessDenied == ace.AceQualifier)
  350. type = AccessControlType.Deny;
  351. else
  352. continue;
  353. AccessRule rule = InternalAccessRuleFactory (ace, targetType, type);
  354. rules.Add (rule);
  355. }
  356. } finally {
  357. ReadUnlock ();
  358. }
  359. return new AuthorizationRuleCollection (rules.ToArray ());
  360. }
  361. internal virtual AccessRule InternalAccessRuleFactory (QualifiedAce ace, Type targetType,
  362. AccessControlType type)
  363. {
  364. return AccessRuleFactory (ace.SecurityIdentifier.Translate (targetType),
  365. ace.AccessMask, ace.IsInherited,
  366. ace.InheritanceFlags, ace.PropagationFlags, type);
  367. }
  368. internal AuthorizationRuleCollection InternalGetAuditRules (bool includeExplicit,
  369. bool includeInherited,
  370. Type targetType)
  371. {
  372. List<AuthorizationRule> rules = new List<AuthorizationRule> ();
  373. ReadLock ();
  374. try {
  375. if (null != descriptor.SystemAcl) {
  376. foreach (GenericAce genericAce in descriptor.SystemAcl) {
  377. QualifiedAce ace = genericAce as QualifiedAce;
  378. if (null == ace) continue;
  379. if (ace.IsInherited && !includeInherited) continue;
  380. if (!ace.IsInherited && !includeExplicit) continue;
  381. if (AceQualifier.SystemAudit != ace.AceQualifier) continue;
  382. AuditRule rule = InternalAuditRuleFactory (ace, targetType);
  383. rules.Add (rule);
  384. }
  385. }
  386. } finally {
  387. ReadUnlock ();
  388. }
  389. return new AuthorizationRuleCollection (rules.ToArray ());
  390. }
  391. internal virtual AuditRule InternalAuditRuleFactory (QualifiedAce ace, Type targetType)
  392. {
  393. return AuditRuleFactory (ace.SecurityIdentifier.Translate (targetType),
  394. ace.AccessMask, ace.IsInherited,
  395. ace.InheritanceFlags, ace.PropagationFlags, ace.AuditFlags);
  396. }
  397. internal static SecurityIdentifier SidFromIR (IdentityReference identity)
  398. {
  399. if (null == identity)
  400. throw new ArgumentNullException ("identity");
  401. return (SecurityIdentifier)identity.Translate (typeof (SecurityIdentifier));
  402. }
  403. bool AreAccessControlSectionsModified (AccessControlSections mask)
  404. {
  405. return 0 != (AccessControlSectionsModified & mask);
  406. }
  407. void SetAccessControlSectionsModified(AccessControlSections mask, bool modified)
  408. {
  409. if (modified)
  410. AccessControlSectionsModified |= mask;
  411. else
  412. AccessControlSectionsModified &= ~mask;
  413. }
  414. }
  415. }