mscorlib(4.0.0.0) API with additions
FileSystemSecurity.cs
1 using Microsoft.Win32.SafeHandles;
2 using System.IO;
6 
8 {
10  public abstract class FileSystemSecurity : NativeObjectSecurity
11  {
12  private const ResourceType s_ResourceType = ResourceType.FileObject;
13 
16  public override Type AccessRightType => typeof(FileSystemRights);
17 
20  public override Type AccessRuleType => typeof(FileSystemAccessRule);
21 
24  public override Type AuditRuleType => typeof(FileSystemAuditRule);
25 
26  [SecurityCritical]
27  internal FileSystemSecurity(bool isContainer)
28  : base(isContainer, ResourceType.FileObject, _HandleErrorCode, isContainer)
29  {
30  }
31 
32  [SecurityCritical]
33  internal FileSystemSecurity(bool isContainer, string name, AccessControlSections includeSections, bool isDirectory)
34  : base(isContainer, ResourceType.FileObject, name, includeSections, _HandleErrorCode, isDirectory)
35  {
36  }
37 
38  [SecurityCritical]
39  internal FileSystemSecurity(bool isContainer, SafeFileHandle handle, AccessControlSections includeSections, bool isDirectory)
40  : base(isContainer, ResourceType.FileObject, handle, includeSections, _HandleErrorCode, isDirectory)
41  {
42  }
43 
44  [SecurityCritical]
45  private static Exception _HandleErrorCode(int errorCode, string name, SafeHandle handle, object context)
46  {
47  Exception result = null;
48  switch (errorCode)
49  {
50  case 123:
51  result = new ArgumentException(Environment.GetResourceString("Argument_InvalidName"), "name");
52  break;
53  case 6:
54  result = new ArgumentException(Environment.GetResourceString("AccessControl_InvalidHandle"));
55  break;
56  case 2:
57  result = ((context != null && context is bool && (bool)context) ? ((IOException)((name == null || name.Length == 0) ? new DirectoryNotFoundException() : new DirectoryNotFoundException(name))) : ((IOException)((name == null || name.Length == 0) ? new FileNotFoundException() : new FileNotFoundException(name))));
58  break;
59  }
60  return result;
61  }
62 
75  public sealed override AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
76  {
77  return new FileSystemAccessRule(identityReference, accessMask, isInherited, inheritanceFlags, propagationFlags, type);
78  }
79 
92  public sealed override AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags)
93  {
94  return new FileSystemAuditRule(identityReference, accessMask, isInherited, inheritanceFlags, propagationFlags, flags);
95  }
96 
97  internal AccessControlSections GetAccessControlSectionsFromChanges()
98  {
99  AccessControlSections accessControlSections = AccessControlSections.None;
100  if (base.AccessRulesModified)
101  {
102  accessControlSections = AccessControlSections.Access;
103  }
104  if (base.AuditRulesModified)
105  {
106  accessControlSections |= AccessControlSections.Audit;
107  }
108  if (base.OwnerModified)
109  {
110  accessControlSections |= AccessControlSections.Owner;
111  }
112  if (base.GroupModified)
113  {
114  accessControlSections |= AccessControlSections.Group;
115  }
116  return accessControlSections;
117  }
118 
119  [SecurityCritical]
120  [SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)]
121  internal void Persist(string fullPath)
122  {
123  FileIOPermission.QuickDemand(FileIOPermissionAccess.NoAccess, AccessControlActions.Change, fullPath);
124  WriteLock();
125  try
126  {
127  AccessControlSections accessControlSectionsFromChanges = GetAccessControlSectionsFromChanges();
128  Persist(fullPath, accessControlSectionsFromChanges);
129  bool flag2 = base.AccessRulesModified = false;
130  bool flag4 = base.AuditRulesModified = flag2;
131  bool ownerModified = base.GroupModified = flag4;
132  base.OwnerModified = ownerModified;
133  }
134  finally
135  {
136  WriteUnlock();
137  }
138  }
139 
140  [SecuritySafeCritical]
141  [SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)]
142  internal void Persist(SafeFileHandle handle, string fullPath)
143  {
144  if (fullPath != null)
145  {
146  FileIOPermission.QuickDemand(FileIOPermissionAccess.NoAccess, AccessControlActions.Change, fullPath);
147  }
148  else
149  {
150  FileIOPermission.QuickDemand(PermissionState.Unrestricted);
151  }
152  WriteLock();
153  try
154  {
155  AccessControlSections accessControlSectionsFromChanges = GetAccessControlSectionsFromChanges();
156  Persist(handle, accessControlSectionsFromChanges);
157  bool flag2 = base.AccessRulesModified = false;
158  bool flag4 = base.AuditRulesModified = flag2;
159  bool ownerModified = base.GroupModified = flag4;
160  base.OwnerModified = ownerModified;
161  }
162  finally
163  {
164  WriteUnlock();
165  }
166  }
167 
172  {
173  AddAccessRule((AccessRule)rule);
174  }
175 
180  {
181  SetAccessRule((AccessRule)rule);
182  }
183 
188  {
190  }
191 
198  {
199  if (rule == null)
200  {
201  throw new ArgumentNullException("rule");
202  }
203  AuthorizationRuleCollection accessRules = GetAccessRules(includeExplicit: true, includeInherited: true, rule.IdentityReference.GetType());
204  for (int i = 0; i < accessRules.Count; i++)
205  {
206  FileSystemAccessRule fileSystemAccessRule = accessRules[i] as FileSystemAccessRule;
207  if (fileSystemAccessRule != null && fileSystemAccessRule.FileSystemRights == rule.FileSystemRights && fileSystemAccessRule.IdentityReference == rule.IdentityReference && fileSystemAccessRule.AccessControlType == rule.AccessControlType)
208  {
209  return RemoveAccessRule((AccessRule)rule);
210  }
211  }
213  return RemoveAccessRule((AccessRule)rule2);
214  }
215 
220  {
222  }
223 
228  {
229  if (rule == null)
230  {
231  throw new ArgumentNullException("rule");
232  }
233  AuthorizationRuleCollection accessRules = GetAccessRules(includeExplicit: true, includeInherited: true, rule.IdentityReference.GetType());
234  for (int i = 0; i < accessRules.Count; i++)
235  {
236  FileSystemAccessRule fileSystemAccessRule = accessRules[i] as FileSystemAccessRule;
237  if (fileSystemAccessRule != null && fileSystemAccessRule.FileSystemRights == rule.FileSystemRights && fileSystemAccessRule.IdentityReference == rule.IdentityReference && fileSystemAccessRule.AccessControlType == rule.AccessControlType)
238  {
240  return;
241  }
242  }
245  }
246 
251  {
252  AddAuditRule((AuditRule)rule);
253  }
254 
259  {
260  SetAuditRule((AuditRule)rule);
261  }
262 
269  {
270  return RemoveAuditRule((AuditRule)rule);
271  }
272 
277  {
279  }
280 
285  {
287  }
288  }
289 }
void AddAuditRule(FileSystemAuditRule rule)
Adds the specified audit rule to the current file or directory.
FileSystemRights
Defines the access rights to use when creating access and audit rules.
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...
Describes a set of security permissions applied to code. This class cannot be inherited.
FileIOPermissionAccess
Specifies the type of file access requested.
void AddAccessRule(FileSystemAccessRule rule)
Adds the specified access control list (ACL) permission to the current file or directory.
FileSystemRights FileSystemRights
Gets the T:System.Security.AccessControl.FileSystemRights flags associated with the current T:System....
Represents an identity and is the base class for the T:System.Security.Principal.NTAccount and T:Syst...
Represents a combination of a user's identity, an access mask, and an access control type (allow or d...
Definition: AccessRule.cs:7
void RemoveAccessRuleAll(FileSystemAccessRule rule)
Removes all access control list (ACL) permissions for the specified user from the current file or dir...
Represents the access control and audit security for a file or directory.
override Type AccessRuleType
Gets the enumeration that the T:System.Security.AccessControl.FileSystemSecurity class uses to repres...
IdentityReference IdentityReference
Gets the T:System.Security.Principal.IdentityReference to which this rule applies.
Definition: __Canon.cs:3
bool IsInherited
Gets a value indicating whether this rule is explicitly set or is inherited from a parent container o...
Represents a wrapper class for operating system handles. This class must be inherited.
Definition: SafeHandle.cs:12
sealed override AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
Initializes a new instance of the T:System.Security.AccessControl.FileSystemAccessRule class that rep...
bool RemoveAccessRule(FileSystemAccessRule rule)
Removes all matching allow or deny access control list (ACL) permissions from the current file or dir...
Provides the ability to control access to native objects without direct manipulation of Access Contro...
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
The exception that is thrown when an I/O error occurs.
Definition: IOException.cs:10
void RemoveAuditRuleAll(FileSystemAuditRule rule)
Removes all audit rules for the specified user from the current file or directory.
virtual int Count
Gets the number of elements contained in the T:System.Collections.ReadOnlyCollectionBase instance.
override Type AuditRuleType
Gets the type that the T:System.Security.AccessControl.FileSystemSecurity class uses to represent aud...
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
sealed override AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags)
Initializes a new instance of the T:System.Security.AccessControl.FileSystemAuditRule class represent...
ResourceType
Specifies the defined native object types.
Definition: ResourceType.cs:4
Represents an abstraction of an access control entry (ACE) that defines an access rule for a file or ...
AccessControlType
Specifies whether an T:System.Security.AccessControl.AccessRule object is used to allow or deny acces...
Represents a collection of T:System.Security.AccessControl.AuthorizationRule objects.
AccessControlActions
Specifies the actions that are permitted for securable 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.
The exception that is thrown when an attempt to access a file that does not exist on disk fails.
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
bool RemoveAuditRule(FileSystemAuditRule rule)
Removes all matching allow or deny audit rules from the current file or directory.
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
void SetAuditRule(FileSystemAuditRule rule)
Sets the specified audit rule for the current file or directory.
void RemoveAuditRuleSpecific(FileSystemAuditRule rule)
Removes a single matching allow or deny audit rule from the current file or directory.
void SetAccessRule(FileSystemAccessRule rule)
Sets the specified access control list (ACL) permission for the current file or directory.
AuthorizationRuleCollection GetAccessRules(bool includeExplicit, bool includeInherited, Type targetType)
Gets a collection of the access rules associated with the specified security identifier.
Controls the ability to access files and folders. This class cannot be inherited.
AccessControlType AccessControlType
Gets the T:System.Security.AccessControl.AccessControlType value associated with this T:System....
Definition: AccessRule.cs:65
The exception that is thrown when part of a file or directory cannot be found.
InheritanceFlags
Inheritance flags specify the semantics of inheritance for access control entries (ACEs).
void RemoveAccessRuleSpecific(FileSystemAccessRule rule)
Removes a single matching allow or deny access control list (ACL) permission from the current file or...
void ResetAccessRule(FileSystemAccessRule rule)
Adds the specified access control list (ACL) permission to the current file or directory and removes ...
AccessControlSections
Specifies which sections of a security descriptor to save or load.
override Type AccessRightType
Gets the enumeration that the T:System.Security.AccessControl.FileSystemSecurity class uses to repres...
PropagationFlags PropagationFlags
Gets the value of the propagation flags, which determine how inheritance of this rule is propagated t...
Represents an abstraction of an access control entry (ACE) that defines an audit rule for a file or d...
AuditFlags
Specifies the conditions for auditing attempts to access a securable object.
Definition: AuditFlags.cs:5