mscorlib(4.0.0.0) API with additions
RawSecurityDescriptor.cs
1 using Microsoft.Win32;
4 
6 {
9  {
10  private SecurityIdentifier _owner;
11 
12  private SecurityIdentifier _group;
13 
14  private ControlFlags _flags;
15 
16  private RawAcl _sacl;
17 
18  private RawAcl _dacl;
19 
20  private byte _rmControl;
21 
22  internal override GenericAcl GenericSacl => _sacl;
23 
24  internal override GenericAcl GenericDacl => _dacl;
25 
28  public override ControlFlags ControlFlags => _flags;
29 
32  public override SecurityIdentifier Owner
33  {
34  get
35  {
36  return _owner;
37  }
38  set
39  {
40  _owner = value;
41  }
42  }
43 
46  public override SecurityIdentifier Group
47  {
48  get
49  {
50  return _group;
51  }
52  set
53  {
54  _group = value;
55  }
56  }
57 
60  public RawAcl SystemAcl
61  {
62  get
63  {
64  return _sacl;
65  }
66  set
67  {
68  _sacl = value;
69  }
70  }
71 
75  {
76  get
77  {
78  return _dacl;
79  }
80  set
81  {
82  _dacl = value;
83  }
84  }
85 
88  public byte ResourceManagerControl
89  {
90  get
91  {
92  return _rmControl;
93  }
94  set
95  {
96  _rmControl = value;
97  }
98  }
99 
100  private void CreateFromParts(ControlFlags flags, SecurityIdentifier owner, SecurityIdentifier group, RawAcl systemAcl, RawAcl discretionaryAcl)
101  {
102  SetFlags(flags);
103  Owner = owner;
104  Group = group;
105  SystemAcl = systemAcl;
106  DiscretionaryAcl = discretionaryAcl;
108  }
109 
116  public RawSecurityDescriptor(ControlFlags flags, SecurityIdentifier owner, SecurityIdentifier group, RawAcl systemAcl, RawAcl discretionaryAcl)
117  {
118  CreateFromParts(flags, owner, group, systemAcl, discretionaryAcl);
119  }
120 
123  [SecuritySafeCritical]
124  public RawSecurityDescriptor(string sddlForm)
125  : this(BinaryFormFromSddlForm(sddlForm), 0)
126  {
127  }
128 
132  public RawSecurityDescriptor(byte[] binaryForm, int offset)
133  {
134  if (binaryForm == null)
135  {
136  throw new ArgumentNullException("binaryForm");
137  }
138  if (offset < 0)
139  {
140  throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
141  }
142  if (binaryForm.Length - offset < 20)
143  {
144  throw new ArgumentOutOfRangeException("binaryForm", Environment.GetResourceString("ArgumentOutOfRange_ArrayTooSmall"));
145  }
146  if (binaryForm[offset + 0] != GenericSecurityDescriptor.Revision)
147  {
148  throw new ArgumentOutOfRangeException("binaryForm", Environment.GetResourceString("AccessControl_InvalidSecurityDescriptorRevision"));
149  }
150  byte resourceManagerControl = binaryForm[offset + 1];
151  ControlFlags controlFlags = (ControlFlags)(binaryForm[offset + 2] + (binaryForm[offset + 3] << 8));
152  if ((controlFlags & ControlFlags.SelfRelative) == ControlFlags.None)
153  {
154  throw new ArgumentException(Environment.GetResourceString("AccessControl_InvalidSecurityDescriptorSelfRelativeForm"), "binaryForm");
155  }
156  int num = GenericSecurityDescriptor.UnmarshalInt(binaryForm, offset + 4);
157  SecurityIdentifier owner = (num == 0) ? null : new SecurityIdentifier(binaryForm, offset + num);
158  int num2 = GenericSecurityDescriptor.UnmarshalInt(binaryForm, offset + 8);
159  SecurityIdentifier group = (num2 == 0) ? null : new SecurityIdentifier(binaryForm, offset + num2);
160  int num3 = GenericSecurityDescriptor.UnmarshalInt(binaryForm, offset + 12);
161  RawAcl systemAcl = ((controlFlags & ControlFlags.SystemAclPresent) == ControlFlags.None || num3 == 0) ? null : new RawAcl(binaryForm, offset + num3);
162  int num4 = GenericSecurityDescriptor.UnmarshalInt(binaryForm, offset + 16);
163  RawAcl discretionaryAcl = ((controlFlags & ControlFlags.DiscretionaryAclPresent) == ControlFlags.None || num4 == 0) ? null : new RawAcl(binaryForm, offset + num4);
164  CreateFromParts(controlFlags, owner, group, systemAcl, discretionaryAcl);
165  if ((controlFlags & ControlFlags.RMControlValid) != 0)
166  {
167  ResourceManagerControl = resourceManagerControl;
168  }
169  }
170 
171  [SecurityCritical]
172  private static byte[] BinaryFormFromSddlForm(string sddlForm)
173  {
174  if (sddlForm == null)
175  {
176  throw new ArgumentNullException("sddlForm");
177  }
178  IntPtr resultSd = IntPtr.Zero;
179  uint resultSdLength = 0u;
180  byte[] array = null;
181  try
182  {
183  if (1 != Win32Native.ConvertStringSdToSd(sddlForm, GenericSecurityDescriptor.Revision, out resultSd, ref resultSdLength))
184  {
185  switch (Marshal.GetLastWin32Error())
186  {
187  case 87:
188  case 1305:
189  case 1336:
190  case 1338:
191  throw new ArgumentException(Environment.GetResourceString("ArgumentException_InvalidSDSddlForm"), "sddlForm");
192  case 8:
193  throw new OutOfMemoryException();
194  case 1337:
195  throw new ArgumentException(Environment.GetResourceString("AccessControl_InvalidSidInSDDLString"), "sddlForm");
196  default:
197  throw new SystemException();
198  case 0:
199  break;
200  }
201  }
202  array = new byte[resultSdLength];
203  Marshal.Copy(resultSd, array, 0, (int)resultSdLength);
204  return array;
205  }
206  finally
207  {
208  if (resultSd != IntPtr.Zero)
209  {
210  Win32Native.LocalFree(resultSd);
211  }
212  }
213  }
214 
217  public void SetFlags(ControlFlags flags)
218  {
219  _flags = (flags | ControlFlags.SelfRelative);
220  }
221  }
222 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
override ControlFlags ControlFlags
Gets values that specify behavior of the T:System.Security.AccessControl.RawSecurityDescriptor object...
Serves as the base class for system exceptions namespace.
RawSecurityDescriptor(byte[] binaryForm, int offset)
Initializes a new instance of the T:System.Security.AccessControl.RawSecurityDescriptor class from th...
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
override SecurityIdentifier Group
Gets or sets the primary group for this T:System.Security.AccessControl.RawSecurityDescriptor object.
static byte Revision
Gets the revision level of the T:System.Security.AccessControl.GenericSecurityDescriptor object.
byte ResourceManagerControl
Gets or sets a byte value that represents the resource manager control bits associated with this T:Sy...
Represents an Access Control List (ACL).
Definition: RawAcl.cs:6
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static void Copy(int[] source, int startIndex, IntPtr destination, int length)
Copies data from a one-dimensional, managed 32-bit signed integer array to an unmanaged memory pointe...
Definition: Marshal.cs:301
Represents a security descriptor. A security descriptor includes an owner, a primary group,...
ControlFlags
These flags affect the security descriptor behavior.
Definition: ControlFlags.cs:5
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
The exception that is thrown when there is not enough memory to continue the execution of a program.
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
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.
void SetFlags(ControlFlags flags)
Sets the P:System.Security.AccessControl.RawSecurityDescriptor.ControlFlags property of this T:System...
Represents an access control list (ACL) and is the base class for the T:System.Security....
Definition: GenericAcl.cs:6
Represents a security identifier (SID) and provides marshaling and comparison operations for SIDs.
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Definition: IntPtr.cs:20
Represents a Discretionary Access Control List (DACL).
RawSecurityDescriptor(ControlFlags flags, SecurityIdentifier owner, SecurityIdentifier group, RawAcl systemAcl, RawAcl discretionaryAcl)
Initializes a new instance of the T:System.Security.AccessControl.RawSecurityDescriptor class with th...
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
RawSecurityDescriptor(string sddlForm)
Initializes a new instance of the T:System.Security.AccessControl.RawSecurityDescriptor class from th...
Represents a System Access Control List (SACL).
Definition: SystemAcl.cs:6