mscorlib(4.0.0.0) API with additions
NativeObjectSecurity.cs
1 using System.IO;
4 
6 {
8  public abstract class NativeObjectSecurity : CommonObjectSecurity
9  {
16  [SecuritySafeCritical]
17  protected internal delegate Exception ExceptionFromErrorCode(int errorCode, string name, SafeHandle handle, object context);
18 
19  private readonly ResourceType _resourceType;
20 
21  private ExceptionFromErrorCode _exceptionFromErrorCode;
22 
23  private object _exceptionContext;
24 
25  private readonly uint ProtectedDiscretionaryAcl = 2147483648u;
26 
27  private readonly uint ProtectedSystemAcl = 1073741824u;
28 
29  private readonly uint UnprotectedDiscretionaryAcl = 536870912u;
30 
31  private readonly uint UnprotectedSystemAcl = 268435456u;
32 
37  protected NativeObjectSecurity(bool isContainer, ResourceType resourceType)
38  : base(isContainer)
39  {
40  _resourceType = resourceType;
41  }
42 
49  protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
50  : this(isContainer, resourceType)
51  {
52  _exceptionContext = exceptionContext;
53  _exceptionFromErrorCode = exceptionFromErrorCode;
54  }
55 
56  [SecurityCritical]
57  internal NativeObjectSecurity(ResourceType resourceType, CommonSecurityDescriptor securityDescriptor)
58  : this(resourceType, securityDescriptor, null)
59  {
60  }
61 
62  [SecurityCritical]
63  internal NativeObjectSecurity(ResourceType resourceType, CommonSecurityDescriptor securityDescriptor, ExceptionFromErrorCode exceptionFromErrorCode)
64  : base(securityDescriptor)
65  {
66  _resourceType = resourceType;
67  _exceptionFromErrorCode = exceptionFromErrorCode;
68  }
69 
78  [SecuritySafeCritical]
79  protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, string name, AccessControlSections includeSections, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
80  : this(resourceType, CreateInternal(resourceType, isContainer, name, null, includeSections, createByName: true, exceptionFromErrorCode, exceptionContext), exceptionFromErrorCode)
81  {
82  }
83 
90  protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, string name, AccessControlSections includeSections)
91  : this(isContainer, resourceType, name, includeSections, null, null)
92  {
93  }
94 
103  [SecuritySafeCritical]
104  protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle handle, AccessControlSections includeSections, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
105  : this(resourceType, CreateInternal(resourceType, isContainer, null, handle, includeSections, createByName: false, exceptionFromErrorCode, exceptionContext), exceptionFromErrorCode)
106  {
107  }
108 
115  [SecuritySafeCritical]
116  protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle handle, AccessControlSections includeSections)
117  : this(isContainer, resourceType, handle, includeSections, null, null)
118  {
119  }
120 
121  [SecurityCritical]
122  private static CommonSecurityDescriptor CreateInternal(ResourceType resourceType, bool isContainer, string name, SafeHandle handle, AccessControlSections includeSections, bool createByName, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
123  {
124  if (createByName && name == null)
125  {
126  throw new ArgumentNullException("name");
127  }
128  if (!createByName && handle == null)
129  {
130  throw new ArgumentNullException("handle");
131  }
132  RawSecurityDescriptor resultSd;
133  int securityInfo = Win32.GetSecurityInfo(resourceType, name, handle, includeSections, out resultSd);
134  if (securityInfo != 0)
135  {
136  Exception ex = null;
137  if (exceptionFromErrorCode != null)
138  {
139  ex = exceptionFromErrorCode(securityInfo, name, handle, exceptionContext);
140  }
141  if (ex == null)
142  {
143  switch (securityInfo)
144  {
145  case 5:
146  ex = new UnauthorizedAccessException();
147  break;
148  case 1307:
149  ex = new InvalidOperationException(Environment.GetResourceString("AccessControl_InvalidOwner"));
150  break;
151  case 1308:
152  ex = new InvalidOperationException(Environment.GetResourceString("AccessControl_InvalidGroup"));
153  break;
154  case 87:
155  ex = new InvalidOperationException(Environment.GetResourceString("AccessControl_UnexpectedError", securityInfo));
156  break;
157  case 123:
158  ex = new ArgumentException(Environment.GetResourceString("Argument_InvalidName"), "name");
159  break;
160  case 2:
161  ex = ((name == null) ? new FileNotFoundException() : new FileNotFoundException(name));
162  break;
163  case 1350:
164  ex = new NotSupportedException(Environment.GetResourceString("AccessControl_NoAssociatedSecurity"));
165  break;
166  default:
167  ex = new InvalidOperationException(Environment.GetResourceString("AccessControl_UnexpectedError", securityInfo));
168  break;
169  }
170  }
171  throw ex;
172  }
173  return new CommonSecurityDescriptor(isContainer, isDS: false, resultSd, trusted: true);
174  }
175 
176  [SecurityCritical]
177  private void Persist(string name, SafeHandle handle, AccessControlSections includeSections, object exceptionContext)
178  {
179  WriteLock();
180  try
181  {
182  SecurityInfos securityInfos = (SecurityInfos)0;
183  SecurityIdentifier owner = null;
184  SecurityIdentifier group = null;
185  SystemAcl sacl = null;
186  DiscretionaryAcl dacl = null;
187  if ((includeSections & AccessControlSections.Owner) != 0 && _securityDescriptor.Owner != null)
188  {
189  securityInfos |= SecurityInfos.Owner;
190  owner = _securityDescriptor.Owner;
191  }
192  if ((includeSections & AccessControlSections.Group) != 0 && _securityDescriptor.Group != null)
193  {
194  securityInfos |= SecurityInfos.Group;
195  group = _securityDescriptor.Group;
196  }
197  if ((includeSections & AccessControlSections.Audit) != 0)
198  {
199  securityInfos |= SecurityInfos.SystemAcl;
200  sacl = ((!_securityDescriptor.IsSystemAclPresent || _securityDescriptor.SystemAcl == null || _securityDescriptor.SystemAcl.Count <= 0) ? null : _securityDescriptor.SystemAcl);
201  securityInfos = (SecurityInfos)(((_securityDescriptor.ControlFlags & ControlFlags.SystemAclProtected) == ControlFlags.None) ? ((int)securityInfos | (int)UnprotectedSystemAcl) : ((int)securityInfos | (int)ProtectedSystemAcl));
202  }
203  if ((includeSections & AccessControlSections.Access) != 0 && _securityDescriptor.IsDiscretionaryAclPresent)
204  {
205  securityInfos |= SecurityInfos.DiscretionaryAcl;
206  dacl = ((!_securityDescriptor.DiscretionaryAcl.EveryOneFullAccessForNullDacl) ? _securityDescriptor.DiscretionaryAcl : null);
207  securityInfos = (SecurityInfos)(((_securityDescriptor.ControlFlags & ControlFlags.DiscretionaryAclProtected) == ControlFlags.None) ? ((int)securityInfos | (int)UnprotectedDiscretionaryAcl) : ((int)securityInfos | (int)ProtectedDiscretionaryAcl));
208  }
209  if (securityInfos != 0)
210  {
211  int num = Win32.SetSecurityInfo(_resourceType, name, handle, securityInfos, owner, group, sacl, dacl);
212  if (num != 0)
213  {
214  Exception ex = null;
215  if (_exceptionFromErrorCode != null)
216  {
217  ex = _exceptionFromErrorCode(num, name, handle, exceptionContext);
218  }
219  if (ex == null)
220  {
221  switch (num)
222  {
223  case 5:
224  ex = new UnauthorizedAccessException();
225  break;
226  case 1307:
227  ex = new InvalidOperationException(Environment.GetResourceString("AccessControl_InvalidOwner"));
228  break;
229  case 1308:
230  ex = new InvalidOperationException(Environment.GetResourceString("AccessControl_InvalidGroup"));
231  break;
232  case 123:
233  ex = new ArgumentException(Environment.GetResourceString("Argument_InvalidName"), "name");
234  break;
235  case 6:
236  ex = new NotSupportedException(Environment.GetResourceString("AccessControl_InvalidHandle"));
237  break;
238  case 2:
239  ex = new FileNotFoundException();
240  break;
241  case 1350:
242  ex = new NotSupportedException(Environment.GetResourceString("AccessControl_NoAssociatedSecurity"));
243  break;
244  default:
245  ex = new InvalidOperationException(Environment.GetResourceString("AccessControl_UnexpectedError", num));
246  break;
247  }
248  }
249  throw ex;
250  }
251  base.OwnerModified = false;
252  base.GroupModified = false;
253  base.AccessRulesModified = false;
254  base.AuditRulesModified = false;
255  }
256  }
257  finally
258  {
259  WriteUnlock();
260  }
261  }
262 
267  protected sealed override void Persist(string name, AccessControlSections includeSections)
268  {
269  Persist(name, includeSections, _exceptionContext);
270  }
271 
277  [SecuritySafeCritical]
278  protected void Persist(string name, AccessControlSections includeSections, object exceptionContext)
279  {
280  if (name == null)
281  {
282  throw new ArgumentNullException("name");
283  }
284  Persist(name, null, includeSections, exceptionContext);
285  }
286 
291  [SecuritySafeCritical]
292  protected sealed override void Persist(SafeHandle handle, AccessControlSections includeSections)
293  {
294  Persist(handle, includeSections, _exceptionContext);
295  }
296 
302  [SecuritySafeCritical]
303  protected void Persist(SafeHandle handle, AccessControlSections includeSections, object exceptionContext)
304  {
305  if (handle == null)
306  {
307  throw new ArgumentNullException("handle");
308  }
309  Persist(null, handle, includeSections, exceptionContext);
310  }
311  }
312 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Represents a security descriptor. A security descriptor includes an owner, a primary group,...
Definition: __Canon.cs:3
NativeObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle handle, AccessControlSections includeSections, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
Initializes a new instance of the T:System.Security.AccessControl.NativeObjectSecurity class with the...
SecurityInfos
Specifies the section of a security descriptor to be queried or set.
Definition: SecurityInfos.cs:5
Represents a wrapper class for operating system handles. This class must be inherited.
Definition: SafeHandle.cs:12
Provides the ability to control access to native objects without direct manipulation of Access Contro...
NativeObjectSecurity(bool isContainer, ResourceType resourceType, string name, AccessControlSections includeSections, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
Initializes a new instance of the T:System.Security.AccessControl.NativeObjectSecurity class with the...
sealed override void Persist(SafeHandle handle, AccessControlSections includeSections)
Saves the specified sections of the security descriptor associated with this T:System....
sealed override void Persist(string name, AccessControlSections includeSections)
Saves the specified sections of the security descriptor associated with this T:System....
Controls access to objects without direct manipulation of access control lists (ACLs)....
ControlFlags
These flags affect the security descriptor behavior.
Definition: ControlFlags.cs:5
void Persist(SafeHandle handle, AccessControlSections includeSections, object exceptionContext)
Saves the specified sections of the security descriptor associated with this T:System....
NativeObjectSecurity(bool isContainer, ResourceType resourceType)
Initializes a new instance of the T:System.Security.AccessControl.NativeObjectSecurity class with the...
ResourceType
Specifies the defined native object types.
Definition: ResourceType.cs:4
void Persist(string name, AccessControlSections includeSections, object exceptionContext)
Saves the specified sections of the security descriptor associated with this T:System....
Specifies the discretionary access control list (DACL).
The exception that is thrown when an attempt to access a file that does not exist on disk fails.
NativeObjectSecurity(bool isContainer, ResourceType resourceType, string name, AccessControlSections includeSections)
Initializes a new instance of the T:System.Security.AccessControl.NativeObjectSecurity class with the...
internal delegate Exception ExceptionFromErrorCode(int errorCode, string name, SafeHandle handle, object context)
Provides a way for integrators to map numeric error codes to specific exceptions that they create.
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
Represents a security identifier (SID) and provides marshaling and comparison operations for SIDs.
Specifies the system access control list (SACL).
NativeObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle handle, AccessControlSections includeSections)
Initializes a new instance of the T:System.Security.AccessControl.NativeObjectSecurity class with the...
AccessControlSections
Specifies which sections of a security descriptor to save or load.
NativeObjectSecurity(bool isContainer, ResourceType resourceType, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
Initializes a new instance of the T:System.Security.AccessControl.NativeObjectSecurity class by using...