2 // System.Security.AccessControl.GenericSecurityDescriptor implementation
5 // Dick Porter <dick@ximian.com>
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Globalization;
31 using System.Security.Principal;
34 namespace System.Security.AccessControl {
35 public abstract class GenericSecurityDescriptor {
37 protected GenericSecurityDescriptor ()
41 public int BinaryLength {
45 len += Owner.BinaryLength;
47 len += Group.BinaryLength;
48 if (DaclPresent && !DaclIsUnmodifiedAefa)
49 len += InternalDacl.BinaryLength;
51 len += InternalSacl.BinaryLength;
56 public abstract ControlFlags ControlFlags { get; }
58 public abstract SecurityIdentifier Group { get; set; }
60 public abstract SecurityIdentifier Owner { get; set; }
62 public static byte Revision {
66 internal virtual GenericAcl InternalDacl {
70 internal virtual GenericAcl InternalSacl {
74 internal virtual byte InternalReservedField {
78 public void GetBinaryForm (byte[] binaryForm, int offset)
80 if (null == binaryForm)
81 throw new ArgumentNullException ("binaryForm");
83 int binaryLength = BinaryLength;
84 if (offset < 0 || offset > binaryForm.Length - binaryLength)
85 throw new ArgumentOutOfRangeException ("offset");
87 ControlFlags controlFlags = ControlFlags;
88 if (DaclIsUnmodifiedAefa) { controlFlags &= ~ControlFlags.DiscretionaryAclPresent; }
89 binaryForm[offset + 0x00] = Revision;
90 binaryForm[offset + 0x01] = InternalReservedField;
91 WriteUShort ((ushort)controlFlags, binaryForm,
94 // Skip 'offset' fields (will fill later)
98 WriteInt (pos, binaryForm, offset + 0x04);
99 Owner.GetBinaryForm (binaryForm, offset + pos);
100 pos += Owner.BinaryLength;
102 WriteInt (0, binaryForm, offset + 0x04);
106 WriteInt (pos, binaryForm, offset + 0x08);
107 Group.GetBinaryForm (binaryForm, offset + pos);
108 pos += Group.BinaryLength;
110 WriteInt (0, binaryForm, offset + 0x08);
113 GenericAcl sysAcl = InternalSacl;
115 WriteInt (pos, binaryForm, offset + 0x0C);
116 sysAcl.GetBinaryForm (binaryForm, offset + pos);
117 pos += InternalSacl.BinaryLength;
119 WriteInt (0, binaryForm, offset + 0x0C);
122 GenericAcl discAcl = InternalDacl;
123 if (DaclPresent && !DaclIsUnmodifiedAefa) {
124 WriteInt (pos, binaryForm, offset + 0x10);
125 discAcl.GetBinaryForm (binaryForm, offset + pos);
126 pos += InternalDacl.BinaryLength;
128 WriteInt (0, binaryForm, offset + 0x10);
132 public string GetSddlForm (AccessControlSections includeSections)
134 StringBuilder result = new StringBuilder ();
136 if ((includeSections & AccessControlSections.Owner) != 0
138 result.AppendFormat (
139 CultureInfo.InvariantCulture,
140 "O:{0}", Owner.GetSddlForm ());
143 if ((includeSections & AccessControlSections.Group) != 0
145 result.AppendFormat (
146 CultureInfo.InvariantCulture,
147 "G:{0}", Group.GetSddlForm ());
150 if ((includeSections & AccessControlSections.Access) != 0
151 && DaclPresent && !DaclIsUnmodifiedAefa) {
152 result.AppendFormat (
153 CultureInfo.InvariantCulture,
155 InternalDacl.GetSddlForm (ControlFlags,
159 if ((includeSections & AccessControlSections.Audit) != 0
161 result.AppendFormat (
162 CultureInfo.InvariantCulture,
164 InternalSacl.GetSddlForm (ControlFlags,
168 return result.ToString ();
171 public static bool IsSddlConversionSupported ()
176 // See CommonSecurityDescriptor constructor regarding this persistence detail.
177 internal virtual bool DaclIsUnmodifiedAefa {
178 get { return false; }
183 return InternalDacl != null
184 && (ControlFlags & ControlFlags.DiscretionaryAclPresent) != 0;
190 return InternalSacl != null
191 && (ControlFlags & ControlFlags.SystemAclPresent) != 0;
195 void WriteUShort (ushort val, byte[] buffer, int offset)
197 buffer[offset] = (byte)val;
198 buffer[offset + 1] = (byte)(val >> 8);
201 void WriteInt (int val, byte[] buffer, int offset)
203 buffer[offset] = (byte)val;
204 buffer[offset + 1] = (byte)(val >> 8);
205 buffer[offset + 2] = (byte)(val >> 16);
206 buffer[offset + 3] = (byte)(val >> 24);