mscorlib(4.0.0.0) API with additions
CommonSecurityDescriptor.cs
2 
4 {
7  {
8  private bool _isContainer;
9 
10  private bool _isDS;
11 
12  private RawSecurityDescriptor _rawSd;
13 
14  private SystemAcl _sacl;
15 
16  private DiscretionaryAcl _dacl;
17 
18  internal sealed override GenericAcl GenericSacl => _sacl;
19 
20  internal sealed override GenericAcl GenericDacl => _dacl;
21 
25  public bool IsContainer => _isContainer;
26 
30  public bool IsDS => _isDS;
31 
34  public override ControlFlags ControlFlags => _rawSd.ControlFlags;
35 
38  public override SecurityIdentifier Owner
39  {
40  get
41  {
42  return _rawSd.Owner;
43  }
44  set
45  {
46  _rawSd.Owner = value;
47  }
48  }
49 
52  public override SecurityIdentifier Group
53  {
54  get
55  {
56  return _rawSd.Group;
57  }
58  set
59  {
60  _rawSd.Group = value;
61  }
62  }
63 
66  public SystemAcl SystemAcl
67  {
68  get
69  {
70  return _sacl;
71  }
72  set
73  {
74  if (value != null)
75  {
76  if (value.IsContainer != IsContainer)
77  {
78  throw new ArgumentException(Environment.GetResourceString(IsContainer ? "AccessControl_MustSpecifyContainerAcl" : "AccessControl_MustSpecifyLeafObjectAcl"), "value");
79  }
80  if (value.IsDS != IsDS)
81  {
82  throw new ArgumentException(Environment.GetResourceString(IsDS ? "AccessControl_MustSpecifyDirectoryObjectAcl" : "AccessControl_MustSpecifyNonDirectoryObjectAcl"), "value");
83  }
84  }
85  _sacl = value;
86  if (_sacl != null)
87  {
88  _rawSd.SystemAcl = _sacl.RawAcl;
89  AddControlFlags(ControlFlags.SystemAclPresent);
90  }
91  else
92  {
93  _rawSd.SystemAcl = null;
94  RemoveControlFlags(ControlFlags.SystemAclPresent);
95  }
96  }
97  }
98 
102  {
103  get
104  {
105  return _dacl;
106  }
107  set
108  {
109  if (value != null)
110  {
111  if (value.IsContainer != IsContainer)
112  {
113  throw new ArgumentException(Environment.GetResourceString(IsContainer ? "AccessControl_MustSpecifyContainerAcl" : "AccessControl_MustSpecifyLeafObjectAcl"), "value");
114  }
115  if (value.IsDS != IsDS)
116  {
117  throw new ArgumentException(Environment.GetResourceString(IsDS ? "AccessControl_MustSpecifyDirectoryObjectAcl" : "AccessControl_MustSpecifyNonDirectoryObjectAcl"), "value");
118  }
119  }
120  if (value == null)
121  {
122  _dacl = DiscretionaryAcl.CreateAllowEveryoneFullAccess(IsDS, IsContainer);
123  }
124  else
125  {
126  _dacl = value;
127  }
128  _rawSd.DiscretionaryAcl = _dacl.RawAcl;
129  AddControlFlags(ControlFlags.DiscretionaryAclPresent);
130  }
131  }
132 
136  public bool IsSystemAclCanonical
137  {
138  get
139  {
140  if (SystemAcl != null)
141  {
142  return SystemAcl.IsCanonical;
143  }
144  return true;
145  }
146  }
147 
151  public bool IsDiscretionaryAclCanonical
152  {
153  get
154  {
155  if (DiscretionaryAcl != null)
156  {
158  }
159  return true;
160  }
161  }
162 
163  internal bool IsSystemAclPresent => (_rawSd.ControlFlags & ControlFlags.SystemAclPresent) != ControlFlags.None;
164 
165  internal bool IsDiscretionaryAclPresent => (_rawSd.ControlFlags & ControlFlags.DiscretionaryAclPresent) != ControlFlags.None;
166 
167  private void CreateFromParts(bool isContainer, bool isDS, ControlFlags flags, SecurityIdentifier owner, SecurityIdentifier group, SystemAcl systemAcl, DiscretionaryAcl discretionaryAcl)
168  {
169  if (systemAcl != null && systemAcl.IsContainer != isContainer)
170  {
171  throw new ArgumentException(Environment.GetResourceString(isContainer ? "AccessControl_MustSpecifyContainerAcl" : "AccessControl_MustSpecifyLeafObjectAcl"), "systemAcl");
172  }
173  if (discretionaryAcl != null && discretionaryAcl.IsContainer != isContainer)
174  {
175  throw new ArgumentException(Environment.GetResourceString(isContainer ? "AccessControl_MustSpecifyContainerAcl" : "AccessControl_MustSpecifyLeafObjectAcl"), "discretionaryAcl");
176  }
177  _isContainer = isContainer;
178  if (systemAcl != null && systemAcl.IsDS != isDS)
179  {
180  throw new ArgumentException(Environment.GetResourceString(isDS ? "AccessControl_MustSpecifyDirectoryObjectAcl" : "AccessControl_MustSpecifyNonDirectoryObjectAcl"), "systemAcl");
181  }
182  if (discretionaryAcl != null && discretionaryAcl.IsDS != isDS)
183  {
184  throw new ArgumentException(Environment.GetResourceString(isDS ? "AccessControl_MustSpecifyDirectoryObjectAcl" : "AccessControl_MustSpecifyNonDirectoryObjectAcl"), "discretionaryAcl");
185  }
186  _isDS = isDS;
187  _sacl = systemAcl;
188  if (discretionaryAcl == null)
189  {
190  discretionaryAcl = DiscretionaryAcl.CreateAllowEveryoneFullAccess(_isDS, _isContainer);
191  }
192  _dacl = discretionaryAcl;
193  ControlFlags controlFlags = flags | ControlFlags.DiscretionaryAclPresent;
194  controlFlags = ((systemAcl != null) ? (controlFlags | ControlFlags.SystemAclPresent) : (controlFlags & ~ControlFlags.SystemAclPresent));
195  _rawSd = new RawSecurityDescriptor(controlFlags, owner, group, systemAcl?.RawAcl, discretionaryAcl.RawAcl);
196  }
197 
208  public CommonSecurityDescriptor(bool isContainer, bool isDS, ControlFlags flags, SecurityIdentifier owner, SecurityIdentifier group, SystemAcl systemAcl, DiscretionaryAcl discretionaryAcl)
209  {
210  CreateFromParts(isContainer, isDS, flags, owner, group, systemAcl, discretionaryAcl);
211  }
212 
213  private CommonSecurityDescriptor(bool isContainer, bool isDS, ControlFlags flags, SecurityIdentifier owner, SecurityIdentifier group, RawAcl systemAcl, RawAcl discretionaryAcl)
214  : this(isContainer, isDS, flags, owner, group, (systemAcl == null) ? null : new SystemAcl(isContainer, isDS, systemAcl), (discretionaryAcl == null) ? null : new DiscretionaryAcl(isContainer, isDS, discretionaryAcl))
215  {
216  }
217 
224  public CommonSecurityDescriptor(bool isContainer, bool isDS, RawSecurityDescriptor rawSecurityDescriptor)
225  : this(isContainer, isDS, rawSecurityDescriptor, trusted: false)
226  {
227  }
228 
229  internal CommonSecurityDescriptor(bool isContainer, bool isDS, RawSecurityDescriptor rawSecurityDescriptor, bool trusted)
230  {
231  if (rawSecurityDescriptor == null)
232  {
233  throw new ArgumentNullException("rawSecurityDescriptor");
234  }
235  CreateFromParts(isContainer, isDS, rawSecurityDescriptor.ControlFlags, rawSecurityDescriptor.Owner, rawSecurityDescriptor.Group, (rawSecurityDescriptor.SystemAcl == null) ? null : new SystemAcl(isContainer, isDS, rawSecurityDescriptor.SystemAcl, trusted), (rawSecurityDescriptor.DiscretionaryAcl == null) ? null : new DiscretionaryAcl(isContainer, isDS, rawSecurityDescriptor.DiscretionaryAcl, trusted));
236  }
237 
244  public CommonSecurityDescriptor(bool isContainer, bool isDS, string sddlForm)
245  : this(isContainer, isDS, new RawSecurityDescriptor(sddlForm), trusted: true)
246  {
247  }
248 
256  public CommonSecurityDescriptor(bool isContainer, bool isDS, byte[] binaryForm, int offset)
257  : this(isContainer, isDS, new RawSecurityDescriptor(binaryForm, offset), trusted: true)
258  {
259  }
260 
266  public void SetSystemAclProtection(bool isProtected, bool preserveInheritance)
267  {
268  if (!isProtected)
269  {
270  RemoveControlFlags(ControlFlags.SystemAclProtected);
271  return;
272  }
273  if (!preserveInheritance && SystemAcl != null)
274  {
276  }
277  AddControlFlags(ControlFlags.SystemAclProtected);
278  }
279 
285  public void SetDiscretionaryAclProtection(bool isProtected, bool preserveInheritance)
286  {
287  if (!isProtected)
288  {
289  RemoveControlFlags(ControlFlags.DiscretionaryAclProtected);
290  }
291  else
292  {
293  if (!preserveInheritance && DiscretionaryAcl != null)
294  {
296  }
297  AddControlFlags(ControlFlags.DiscretionaryAclProtected);
298  }
299  if (DiscretionaryAcl != null && DiscretionaryAcl.EveryOneFullAccessForNullDacl)
300  {
301  DiscretionaryAcl.EveryOneFullAccessForNullDacl = false;
302  }
303  }
304 
308  {
309  if (sid == null)
310  {
311  throw new ArgumentNullException("sid");
312  }
313  if (DiscretionaryAcl != null)
314  {
315  DiscretionaryAcl.Purge(sid);
316  }
317  }
318 
322  {
323  if (sid == null)
324  {
325  throw new ArgumentNullException("sid");
326  }
327  if (SystemAcl != null)
328  {
329  SystemAcl.Purge(sid);
330  }
331  }
332 
336  public void AddDiscretionaryAcl(byte revision, int trusted)
337  {
338  DiscretionaryAcl = new DiscretionaryAcl(IsContainer, IsDS, revision, trusted);
339  AddControlFlags(ControlFlags.DiscretionaryAclPresent);
340  }
341 
345  public void AddSystemAcl(byte revision, int trusted)
346  {
347  SystemAcl = new SystemAcl(IsContainer, IsDS, revision, trusted);
348  AddControlFlags(ControlFlags.SystemAclPresent);
349  }
350 
351  internal void UpdateControlFlags(ControlFlags flagsToUpdate, ControlFlags newFlags)
352  {
353  ControlFlags flags = newFlags | (_rawSd.ControlFlags & ~flagsToUpdate);
354  _rawSd.SetFlags(flags);
355  }
356 
357  internal void AddControlFlags(ControlFlags flags)
358  {
359  _rawSd.SetFlags(_rawSd.ControlFlags | flags);
360  }
361 
362  internal void RemoveControlFlags(ControlFlags flags)
363  {
364  _rawSd.SetFlags(_rawSd.ControlFlags & ~flags);
365  }
366  }
367 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
bool IsCanonical
Gets a Boolean value that specifies whether the access control entries (ACEs) in the current T:System...
Definition: CommonAcl.cs:81
override ControlFlags ControlFlags
Gets values that specify behavior of the T:System.Security.AccessControl.RawSecurityDescriptor object...
void SetDiscretionaryAclProtection(bool isProtected, bool preserveInheritance)
Sets the inheritance protection for the Discretionary Access Control List (DACL) associated with this...
Represents a security descriptor. A security descriptor includes an owner, a primary group,...
SystemAcl?? SystemAcl
Gets or sets the System Access Control List (SACL) for this T:System.Security.AccessControl....
bool IsDiscretionaryAclCanonical
Gets a Boolean value that specifies whether the Discretionary Access Control List (DACL) associated w...
Definition: __Canon.cs:3
override ControlFlags ControlFlags
Gets values that specify behavior of the T:System.Security.AccessControl.CommonSecurityDescriptor obj...
override SecurityIdentifier Group
Gets or sets the primary group for this T:System.Security.AccessControl.RawSecurityDescriptor object.
void PurgeAudit(SecurityIdentifier sid)
Removes all audit rules for the specified security identifier from the System Access Control List (SA...
void PurgeAccessControl(SecurityIdentifier sid)
Removes all access rules for the specified security identifier from the Discretionary Access Control ...
Represents an Access Control List (ACL).
Definition: RawAcl.cs:6
DiscretionaryAcl?? DiscretionaryAcl
Gets or sets the discretionary access control list (DACL) for this T:System.Security....
override SecurityIdentifier Owner
Gets or sets the owner of the object associated with this T:System.Security.AccessControl....
CommonSecurityDescriptor(bool isContainer, bool isDS, ControlFlags flags, SecurityIdentifier owner, SecurityIdentifier group, SystemAcl systemAcl, DiscretionaryAcl discretionaryAcl)
Initializes a new instance of the T:System.Security.AccessControl.CommonSecurityDescriptor class from...
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
void AddSystemAcl(byte revision, int trusted)
Sets the P:System.Security.AccessControl.CommonSecurityDescriptor.SystemAcl property for this T:Syste...
Represents a security descriptor. A security descriptor includes an owner, a primary group,...
CommonSecurityDescriptor(bool isContainer, bool isDS, RawSecurityDescriptor rawSecurityDescriptor)
Initializes a new instance of the T:System.Security.AccessControl.CommonSecurityDescriptor class from...
RawAcl SystemAcl
Gets or sets the System Access Control List (SACL) for this T:System.Security.AccessControl....
void AddDiscretionaryAcl(byte revision, int trusted)
Sets the P:System.Security.AccessControl.CommonSecurityDescriptor.DiscretionaryAcl property for this ...
ControlFlags
These flags affect the security descriptor behavior.
Definition: ControlFlags.cs:5
void SetSystemAclProtection(bool isProtected, bool preserveInheritance)
Sets the inheritance protection for the System Access Control List (SACL) associated with this T:Syst...
void RemoveInheritedAces()
Removes all inherited access control entries (ACEs) from this T:System.Security.AccessControl....
Definition: CommonAcl.cs:1139
override SecurityIdentifier Owner
Gets or sets the owner of the object associated with this T:System.Security.AccessControl....
Represents a security descriptor. A security descriptor includes an owner, a primary group,...
The exception that is thrown when one of the arguments provided to a method is not valid.
RawAcl DiscretionaryAcl
Gets or sets the Discretionary Access Control List (DACL) for this T:System.Security....
bool IsDS
Gets a Boolean value that specifies whether the object associated with this T:System....
void SetFlags(ControlFlags flags)
Sets the P:System.Security.AccessControl.RawSecurityDescriptor.ControlFlags property of this T:System...
CommonSecurityDescriptor(bool isContainer, bool isDS, string sddlForm)
Initializes a new instance of the T:System.Security.AccessControl.CommonSecurityDescriptor class from...
Represents an access control list (ACL) and is the base class for the T:System.Security....
Definition: GenericAcl.cs:6
bool IsSystemAclCanonical
Gets a Boolean value that specifies whether the System Access Control List (SACL) associated with thi...
Represents a security identifier (SID) and provides marshaling and comparison operations for SIDs.
bool IsContainer
Sets whether the T:System.Security.AccessControl.CommonAcl object is a container.
Definition: CommonAcl.cs:86
override SecurityIdentifier Group
Gets or sets the primary group for this T:System.Security.AccessControl.CommonSecurityDescriptor obje...
Represents a Discretionary Access Control List (DACL).
void Purge(SecurityIdentifier sid)
Removes all access control entries (ACEs) contained by this T:System.Security.AccessControl....
Definition: CommonAcl.cs:1155
bool IsDS
Sets whether the current T:System.Security.AccessControl.CommonAcl object is a directory object acces...
Definition: CommonAcl.cs:91
CommonSecurityDescriptor(bool isContainer, bool isDS, byte[] binaryForm, int offset)
Initializes a new instance of the T:System.Security.AccessControl.CommonSecurityDescriptor class from...
Represents a System Access Control List (SACL).
Definition: SystemAcl.cs:6
bool IsContainer
Gets a Boolean value that specifies whether the object associated with this T:System....