mscorlib(4.0.0.0) API with additions
CommonObjectSecurity.cs
3 
5 {
7  [PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
8  public abstract class CommonObjectSecurity : ObjectSecurity
9  {
13  protected CommonObjectSecurity(bool isContainer)
14  : base(isContainer, isDS: false)
15  {
16  }
17 
18  internal CommonObjectSecurity(CommonSecurityDescriptor securityDescriptor)
19  : base(securityDescriptor)
20  {
21  }
22 
23  private AuthorizationRuleCollection GetRules(bool access, bool includeExplicit, bool includeInherited, Type targetType)
24  {
25  ReadLock();
26  try
27  {
28  AuthorizationRuleCollection authorizationRuleCollection = new AuthorizationRuleCollection();
29  if (!SecurityIdentifier.IsValidTargetTypeStatic(targetType))
30  {
31  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeIdentityReferenceType"), "targetType");
32  }
33  CommonAcl commonAcl = null;
34  if (access)
35  {
36  if ((_securityDescriptor.ControlFlags & ControlFlags.DiscretionaryAclPresent) != 0)
37  {
38  commonAcl = _securityDescriptor.DiscretionaryAcl;
39  }
40  }
41  else if ((_securityDescriptor.ControlFlags & ControlFlags.SystemAclPresent) != 0)
42  {
43  commonAcl = _securityDescriptor.SystemAcl;
44  }
45  if (commonAcl == null)
46  {
47  return authorizationRuleCollection;
48  }
49  IdentityReferenceCollection identityReferenceCollection = null;
50  if (targetType != typeof(SecurityIdentifier))
51  {
52  IdentityReferenceCollection identityReferenceCollection2 = new IdentityReferenceCollection(commonAcl.Count);
53  for (int i = 0; i < commonAcl.Count; i++)
54  {
55  CommonAce commonAce = commonAcl[i] as CommonAce;
56  if (AceNeedsTranslation(commonAce, access, includeExplicit, includeInherited))
57  {
58  identityReferenceCollection2.Add(commonAce.SecurityIdentifier);
59  }
60  }
61  identityReferenceCollection = identityReferenceCollection2.Translate(targetType);
62  }
63  int num = 0;
64  for (int j = 0; j < commonAcl.Count; j++)
65  {
66  CommonAce commonAce2 = commonAcl[j] as CommonAce;
67  if (AceNeedsTranslation(commonAce2, access, includeExplicit, includeInherited))
68  {
69  IdentityReference identityReference = (targetType == typeof(SecurityIdentifier)) ? commonAce2.SecurityIdentifier : identityReferenceCollection[num++];
70  if (access)
71  {
72  authorizationRuleCollection.AddRule(AccessRuleFactory(type: (commonAce2.AceQualifier != 0) ? AccessControlType.Deny : AccessControlType.Allow, identityReference: identityReference, accessMask: commonAce2.AccessMask, isInherited: commonAce2.IsInherited, inheritanceFlags: commonAce2.InheritanceFlags, propagationFlags: commonAce2.PropagationFlags));
73  }
74  else
75  {
76  authorizationRuleCollection.AddRule(AuditRuleFactory(identityReference, commonAce2.AccessMask, commonAce2.IsInherited, commonAce2.InheritanceFlags, commonAce2.PropagationFlags, commonAce2.AuditFlags));
77  }
78  }
79  }
80  return authorizationRuleCollection;
81  }
82  finally
83  {
84  ReadUnlock();
85  }
86  }
87 
88  private bool AceNeedsTranslation(CommonAce ace, bool isAccessAce, bool includeExplicit, bool includeInherited)
89  {
90  if (ace == null)
91  {
92  return false;
93  }
94  if (isAccessAce)
95  {
96  if (ace.AceQualifier != 0 && ace.AceQualifier != AceQualifier.AccessDenied)
97  {
98  return false;
99  }
100  }
101  else if (ace.AceQualifier != AceQualifier.SystemAudit)
102  {
103  return false;
104  }
105  if ((includeExplicit && (ace.AceFlags & AceFlags.Inherited) == AceFlags.None) || (includeInherited && (ace.AceFlags & AceFlags.Inherited) != 0))
106  {
107  return true;
108  }
109  return false;
110  }
111 
119  protected override bool ModifyAccess(AccessControlModification modification, AccessRule rule, out bool modified)
120  {
121  if (rule == null)
122  {
123  throw new ArgumentNullException("rule");
124  }
125  WriteLock();
126  try
127  {
128  bool flag = true;
129  if (_securityDescriptor.DiscretionaryAcl == null)
130  {
131  if (modification == AccessControlModification.Remove || modification == AccessControlModification.RemoveAll || modification == AccessControlModification.RemoveSpecific)
132  {
133  modified = false;
134  return flag;
135  }
136  _securityDescriptor.DiscretionaryAcl = new DiscretionaryAcl(base.IsContainer, base.IsDS, GenericAcl.AclRevision, 1);
137  _securityDescriptor.AddControlFlags(ControlFlags.DiscretionaryAclPresent);
138  }
140  if (rule.AccessControlType == AccessControlType.Allow)
141  {
142  switch (modification)
143  {
144  case AccessControlModification.Add:
145  _securityDescriptor.DiscretionaryAcl.AddAccess(AccessControlType.Allow, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
146  break;
147  case AccessControlModification.Set:
148  _securityDescriptor.DiscretionaryAcl.SetAccess(AccessControlType.Allow, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
149  break;
150  case AccessControlModification.Reset:
151  _securityDescriptor.DiscretionaryAcl.RemoveAccess(AccessControlType.Deny, sid, -1, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None);
152  _securityDescriptor.DiscretionaryAcl.SetAccess(AccessControlType.Allow, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
153  break;
154  case AccessControlModification.Remove:
155  flag = _securityDescriptor.DiscretionaryAcl.RemoveAccess(AccessControlType.Allow, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
156  break;
157  case AccessControlModification.RemoveAll:
158  flag = _securityDescriptor.DiscretionaryAcl.RemoveAccess(AccessControlType.Allow, sid, -1, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None);
159  if (!flag)
160  {
161  throw new SystemException();
162  }
163  break;
164  case AccessControlModification.RemoveSpecific:
165  _securityDescriptor.DiscretionaryAcl.RemoveAccessSpecific(AccessControlType.Allow, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
166  break;
167  default:
168  throw new ArgumentOutOfRangeException("modification", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
169  }
170  }
171  else
172  {
173  if (rule.AccessControlType != AccessControlType.Deny)
174  {
175  throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)rule.AccessControlType), "rule.AccessControlType");
176  }
177  switch (modification)
178  {
179  case AccessControlModification.Add:
180  _securityDescriptor.DiscretionaryAcl.AddAccess(AccessControlType.Deny, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
181  break;
182  case AccessControlModification.Set:
183  _securityDescriptor.DiscretionaryAcl.SetAccess(AccessControlType.Deny, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
184  break;
185  case AccessControlModification.Reset:
186  _securityDescriptor.DiscretionaryAcl.RemoveAccess(AccessControlType.Allow, sid, -1, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None);
187  _securityDescriptor.DiscretionaryAcl.SetAccess(AccessControlType.Deny, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
188  break;
189  case AccessControlModification.Remove:
190  flag = _securityDescriptor.DiscretionaryAcl.RemoveAccess(AccessControlType.Deny, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
191  break;
192  case AccessControlModification.RemoveAll:
193  flag = _securityDescriptor.DiscretionaryAcl.RemoveAccess(AccessControlType.Deny, sid, -1, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None);
194  if (!flag)
195  {
196  throw new SystemException();
197  }
198  break;
199  case AccessControlModification.RemoveSpecific:
200  _securityDescriptor.DiscretionaryAcl.RemoveAccessSpecific(AccessControlType.Deny, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
201  break;
202  default:
203  throw new ArgumentOutOfRangeException("modification", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
204  }
205  }
206  modified = flag;
207  base.AccessRulesModified |= modified;
208  return flag;
209  }
210  finally
211  {
212  WriteUnlock();
213  }
214  }
215 
223  protected override bool ModifyAudit(AccessControlModification modification, AuditRule rule, out bool modified)
224  {
225  if (rule == null)
226  {
227  throw new ArgumentNullException("rule");
228  }
229  WriteLock();
230  try
231  {
232  bool flag = true;
233  if (_securityDescriptor.SystemAcl == null)
234  {
235  if (modification == AccessControlModification.Remove || modification == AccessControlModification.RemoveAll || modification == AccessControlModification.RemoveSpecific)
236  {
237  modified = false;
238  return flag;
239  }
240  _securityDescriptor.SystemAcl = new SystemAcl(base.IsContainer, base.IsDS, GenericAcl.AclRevision, 1);
241  _securityDescriptor.AddControlFlags(ControlFlags.SystemAclPresent);
242  }
244  switch (modification)
245  {
246  case AccessControlModification.Add:
247  _securityDescriptor.SystemAcl.AddAudit(rule.AuditFlags, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
248  break;
249  case AccessControlModification.Set:
250  _securityDescriptor.SystemAcl.SetAudit(rule.AuditFlags, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
251  break;
252  case AccessControlModification.Reset:
253  _securityDescriptor.SystemAcl.SetAudit(rule.AuditFlags, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
254  break;
255  case AccessControlModification.Remove:
256  flag = _securityDescriptor.SystemAcl.RemoveAudit(rule.AuditFlags, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
257  break;
258  case AccessControlModification.RemoveAll:
259  flag = _securityDescriptor.SystemAcl.RemoveAudit(AuditFlags.Success | AuditFlags.Failure, sid, -1, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None);
260  if (!flag)
261  {
262  throw new InvalidProgramException();
263  }
264  break;
265  case AccessControlModification.RemoveSpecific:
266  _securityDescriptor.SystemAcl.RemoveAuditSpecific(rule.AuditFlags, sid, rule.AccessMask, rule.InheritanceFlags, rule.PropagationFlags);
267  break;
268  default:
269  throw new ArgumentOutOfRangeException("modification", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
270  }
271  modified = flag;
272  base.AuditRulesModified |= modified;
273  return flag;
274  }
275  finally
276  {
277  WriteUnlock();
278  }
279  }
280 
283  protected void AddAccessRule(AccessRule rule)
284  {
285  if (rule == null)
286  {
287  throw new ArgumentNullException("rule");
288  }
289  WriteLock();
290  try
291  {
292  ModifyAccess(AccessControlModification.Add, rule, out bool _);
293  }
294  finally
295  {
296  WriteUnlock();
297  }
298  }
299 
302  protected void SetAccessRule(AccessRule rule)
303  {
304  if (rule == null)
305  {
306  throw new ArgumentNullException("rule");
307  }
308  WriteLock();
309  try
310  {
311  ModifyAccess(AccessControlModification.Set, rule, out bool _);
312  }
313  finally
314  {
315  WriteUnlock();
316  }
317  }
318 
321  protected void ResetAccessRule(AccessRule rule)
322  {
323  if (rule == null)
324  {
325  throw new ArgumentNullException("rule");
326  }
327  WriteLock();
328  try
329  {
330  ModifyAccess(AccessControlModification.Reset, rule, out bool _);
331  }
332  finally
333  {
334  WriteUnlock();
335  }
336  }
337 
342  protected bool RemoveAccessRule(AccessRule rule)
343  {
344  if (rule == null)
345  {
346  throw new ArgumentNullException("rule");
347  }
348  WriteLock();
349  try
350  {
351  if (_securityDescriptor == null)
352  {
353  return true;
354  }
355  bool modified;
356  return ModifyAccess(AccessControlModification.Remove, rule, out modified);
357  }
358  finally
359  {
360  WriteUnlock();
361  }
362  }
363 
366  protected void RemoveAccessRuleAll(AccessRule rule)
367  {
368  if (rule == null)
369  {
370  throw new ArgumentNullException("rule");
371  }
372  WriteLock();
373  try
374  {
375  if (_securityDescriptor != null)
376  {
377  ModifyAccess(AccessControlModification.RemoveAll, rule, out bool _);
378  }
379  }
380  finally
381  {
382  WriteUnlock();
383  }
384  }
385 
389  {
390  if (rule == null)
391  {
392  throw new ArgumentNullException("rule");
393  }
394  WriteLock();
395  try
396  {
397  if (_securityDescriptor != null)
398  {
399  ModifyAccess(AccessControlModification.RemoveSpecific, rule, out bool _);
400  }
401  }
402  finally
403  {
404  WriteUnlock();
405  }
406  }
407 
410  protected void AddAuditRule(AuditRule rule)
411  {
412  if (rule == null)
413  {
414  throw new ArgumentNullException("rule");
415  }
416  WriteLock();
417  try
418  {
419  ModifyAudit(AccessControlModification.Add, rule, out bool _);
420  }
421  finally
422  {
423  WriteUnlock();
424  }
425  }
426 
429  protected void SetAuditRule(AuditRule rule)
430  {
431  if (rule == null)
432  {
433  throw new ArgumentNullException("rule");
434  }
435  WriteLock();
436  try
437  {
438  ModifyAudit(AccessControlModification.Set, rule, out bool _);
439  }
440  finally
441  {
442  WriteUnlock();
443  }
444  }
445 
450  protected bool RemoveAuditRule(AuditRule rule)
451  {
452  if (rule == null)
453  {
454  throw new ArgumentNullException("rule");
455  }
456  WriteLock();
457  try
458  {
459  bool modified;
460  return ModifyAudit(AccessControlModification.Remove, rule, out modified);
461  }
462  finally
463  {
464  WriteUnlock();
465  }
466  }
467 
470  protected void RemoveAuditRuleAll(AuditRule rule)
471  {
472  if (rule == null)
473  {
474  throw new ArgumentNullException("rule");
475  }
476  WriteLock();
477  try
478  {
479  ModifyAudit(AccessControlModification.RemoveAll, rule, out bool _);
480  }
481  finally
482  {
483  WriteUnlock();
484  }
485  }
486 
489  protected void RemoveAuditRuleSpecific(AuditRule rule)
490  {
491  if (rule == null)
492  {
493  throw new ArgumentNullException("rule");
494  }
495  WriteLock();
496  try
497  {
498  ModifyAudit(AccessControlModification.RemoveSpecific, rule, out bool _);
499  }
500  finally
501  {
502  WriteUnlock();
503  }
504  }
505 
513  public AuthorizationRuleCollection GetAccessRules(bool includeExplicit, bool includeInherited, Type targetType)
514  {
515  return GetRules(access: true, includeExplicit, includeInherited, targetType);
516  }
517 
525  public AuthorizationRuleCollection GetAuditRules(bool includeExplicit, bool includeInherited, Type targetType)
526  {
527  return GetRules(access: false, includeExplicit, includeInherited, targetType);
528  }
529  }
530 }
void SetAuditRule(AuditRule rule)
Removes all audit rules that contain the same security identifier and qualifier as the specified audi...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
PropagationFlags
Specifies how Access Control Entries (ACEs) are propagated to child objects. These flags are signific...
Provides the ability to control access to objects without direct manipulation of Access Control Lists...
internal int AccessMask
Gets the access mask for this rule.
override bool ModifyAccess(AccessControlModification modification, AccessRule rule, out bool modified)
Applies the specified modification to the Discretionary Access Control List (DACL) associated with th...
void AddAuditRule(AuditRule rule)
Adds the specified audit rule to the System Access Control List (SACL) associated with this T:System....
void SetAccessRule(AccessRule rule)
Removes all access rules that contain the same security identifier and qualifier as the specified acc...
Represents an identity and is the base class for the T:System.Security.Principal.NTAccount and T:Syst...
The exception that is thrown when a program contains invalid Microsoft intermediate language (MSIL) o...
Serves as the base class for system exceptions namespace.
void ResetAccessRule(AccessRule rule)
Removes all access rules in the Discretionary Access Control List (DACL) associated with this T:Syste...
Represents a security descriptor. A security descriptor includes an owner, a primary group,...
AuthorizationRuleCollection GetAuditRules(bool includeExplicit, bool includeInherited, Type targetType)
Gets a collection of the audit rules associated with the specified security identifier.
void Add(IdentityReference identity)
Adds an T:System.Security.Principal.IdentityReference object to the T:System.Security....
Represents a combination of a user's identity, an access mask, and an access control type (allow or d...
Definition: AccessRule.cs:7
void AddAccessRule(AccessRule rule)
Adds the specified access rule to the Discretionary Access Control List (DACL) associated with this T...
IdentityReference IdentityReference
Gets the T:System.Security.Principal.IdentityReference to which this rule applies.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
bool RemoveAuditRule(AuditRule rule)
Removes audit rules that contain the same security identifier and access mask as the specified audit ...
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Represents a combination of a user’s identity and an access mask.
Definition: AuditRule.cs:7
AuditFlags AuditFlags
Gets the audit flags for this audit rule.
Definition: AuditRule.cs:65
Controls access to objects without direct manipulation of access control lists (ACLs)....
ControlFlags
These flags affect the security descriptor behavior.
Definition: ControlFlags.cs:5
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
void RemoveAuditRuleSpecific(AuditRule rule)
Removes all audit rules that exactly match the specified audit rule from the System Access Control Li...
Represents a collection of T:System.Security.Principal.IdentityReference objects and provides a means...
bool RemoveAccessRule(AccessRule rule)
Removes access rules that contain the same security identifier and access mask as the specified acces...
IdentityReferenceCollection Translate(Type targetType)
Converts the objects in the collection to the specified type. Calling this method is the same as call...
abstract IdentityReference Translate(Type targetType)
Translates the account name represented by the T:System.Security.Principal.IdentityReference object i...
override bool ModifyAudit(AccessControlModification modification, AuditRule rule, out bool modified)
Applies the specified modification to the System Access Control List (SACL) associated with this T:Sy...
AceFlags
Specifies the inheritance and auditing behavior of an access control entry (ACE).
Definition: AceFlags.cs:5
AccessControlType
Specifies whether an T:System.Security.AccessControl.AccessRule object is used to allow or deny acces...
Specifies the discretionary access control list (DACL).
Represents a collection of T:System.Security.AccessControl.AuthorizationRule objects.
InheritanceFlags InheritanceFlags
Gets the value of flags that determine how this rule is inherited by child objects.
The exception that is thrown when one of the arguments provided to a method is not valid.
CommonObjectSecurity(bool isContainer)
Initializes a new instance of the T:System.Security.AccessControl.CommonObjectSecurity class.
Represents an access control list (ACL) and is the base class for the T:System.Security....
Definition: GenericAcl.cs:6
AccessControlModification
Specifies the type of access control modification to perform. This enumeration is used by methods of ...
AceQualifier
Specifies the function of an access control entry (ACE).
Definition: AceQualifier.cs:4
Represents a security identifier (SID) and provides marshaling and comparison operations for SIDs.
Specifies the system access control list (SACL).
AuthorizationRuleCollection GetAccessRules(bool includeExplicit, bool includeInherited, Type targetType)
Gets a collection of the access rules associated with the specified security identifier.
static readonly byte AclRevision
The revision level of the current T:System.Security.AccessControl.GenericAcl. This value is returned ...
Definition: GenericAcl.cs:9
AccessControlType AccessControlType
Gets the T:System.Security.AccessControl.AccessControlType value associated with this T:System....
Definition: AccessRule.cs:65
void RemoveAccessRuleAll(AccessRule rule)
Removes all access rules that have the same security identifier as the specified access rule from the...
void RemoveAccessRuleSpecific(AccessRule rule)
Removes all access rules that exactly match the specified access rule from the Discretionary Access C...
InheritanceFlags
Inheritance flags specify the semantics of inheritance for access control entries (ACEs).
void RemoveAuditRuleAll(AuditRule rule)
Removes all audit rules that have the same security identifier as the specified audit rule from the S...
PropagationFlags PropagationFlags
Gets the value of the propagation flags, which determine how inheritance of this rule is propagated t...
AuditFlags
Specifies the conditions for auditing attempts to access a securable object.
Definition: AuditFlags.cs:5