1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
//
// System.Security.AccessControl.RawAcl implementation
//
// Authors:
// Dick Porter <dick@ximian.com>
// Atsushi Enomoto <atsushi@ximian.com>
// Kenneth Bell
//
// Copyright (C) 2006-2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using System.Text;
namespace System.Security.AccessControl {
public sealed class RawAcl : GenericAcl
{
private byte revision;
private List<GenericAce> list;
public RawAcl (byte revision, int capacity)
{
this.revision = revision;
list = new List<GenericAce> (capacity);
}
public RawAcl (byte [] binaryForm, int offset)
{
if (binaryForm == null)
throw new ArgumentNullException("binaryForm");
if (offset < 0 || offset > binaryForm.Length - 8)
throw new ArgumentOutOfRangeException("offset", offset, "Offset out of range");
revision = binaryForm[offset];
if (revision != AclRevision && revision != AclRevisionDS)
throw new ArgumentException("Invalid ACL - unknown revision", "binaryForm");
int binaryLength = ReadUShort(binaryForm, offset + 2);
if (offset > binaryForm.Length - binaryLength)
throw new ArgumentException("Invalid ACL - truncated", "binaryForm");
int pos = offset + 8;
int numAces = ReadUShort(binaryForm, offset + 4);
list = new List<GenericAce>(numAces);
for (int i = 0; i < numAces; ++i) {
GenericAce newAce = GenericAce.CreateFromBinaryForm(binaryForm, pos);
list.Add(newAce);
pos += newAce.BinaryLength;
}
}
internal RawAcl(byte revision, List<GenericAce> aces)
{
this.revision = revision;
this.list = aces;
}
public override int BinaryLength
{
get {
int len = 8;
foreach(var ace in list)
{
len += ace.BinaryLength;
}
return len;
}
}
public override int Count {
get { return list.Count; }
}
public override GenericAce this [int index]
{
get { return list [index]; }
set { list [index] = value; }
}
public override byte Revision {
get { return revision; }
}
public override void GetBinaryForm (byte[] binaryForm,
int offset)
{
if(binaryForm == null)
throw new ArgumentNullException("binaryForm");
if(offset < 0
|| offset > binaryForm.Length - BinaryLength)
throw new ArgumentException("Offset out of range", "offset");
binaryForm[offset] = Revision;
binaryForm[offset + 1] = 0;
WriteUShort((ushort)BinaryLength, binaryForm,
offset + 2);
WriteUShort((ushort)list.Count, binaryForm,
offset + 4);
WriteUShort(0, binaryForm, offset + 6);
int pos = offset + 8;
foreach(var ace in list)
{
ace.GetBinaryForm(binaryForm, pos);
pos += ace.BinaryLength;
}
}
public void InsertAce (int index, GenericAce ace)
{
if (ace == null)
throw new ArgumentNullException ("ace");
list.Insert (index, ace);
}
public void RemoveAce (int index)
{
list.RemoveAt (index);
}
internal override string GetSddlForm(ControlFlags sdFlags,
bool isDacl)
{
StringBuilder result = new StringBuilder();
if(isDacl) {
if((sdFlags & ControlFlags.DiscretionaryAclProtected) != 0)
result.Append("P");
if((sdFlags & ControlFlags.DiscretionaryAclAutoInheritRequired) != 0)
result.Append("AR");
if((sdFlags & ControlFlags.DiscretionaryAclAutoInherited) != 0)
result.Append("AI");
} else {
if((sdFlags & ControlFlags.SystemAclProtected) != 0)
result.Append("P");
if((sdFlags & ControlFlags.SystemAclAutoInheritRequired) != 0)
result.Append("AR");
if((sdFlags & ControlFlags.SystemAclAutoInherited) != 0)
result.Append("AI");
}
foreach(var ace in list)
{
result.Append(ace.GetSddlForm());
}
return result.ToString();
}
internal static RawAcl ParseSddlForm(string sddlForm,
bool isDacl,
ref ControlFlags sdFlags,
ref int pos)
{
ParseFlags(sddlForm, isDacl, ref sdFlags, ref pos);
byte revision = GenericAcl.AclRevision;
List<GenericAce> aces = new List<GenericAce>();
while(pos < sddlForm.Length && sddlForm[pos] == '(') {
GenericAce ace = GenericAce.CreateFromSddlForm(
sddlForm, ref pos);
if ((ace as ObjectAce) != null)
revision = GenericAcl.AclRevisionDS;
aces.Add(ace);
}
return new RawAcl(revision, aces);
}
private static void ParseFlags(string sddlForm,
bool isDacl,
ref ControlFlags sdFlags,
ref int pos)
{
char ch = Char.ToUpperInvariant(sddlForm[pos]);
while(ch == 'P' || ch == 'A') {
if(ch == 'P') {
if (isDacl)
sdFlags |= ControlFlags.DiscretionaryAclProtected;
else
sdFlags |= ControlFlags.SystemAclProtected;
pos++;
} else if(sddlForm.Length > pos + 1) {
ch = Char.ToUpperInvariant(sddlForm[pos + 1]);
if(ch == 'R') {
if (isDacl)
sdFlags |= ControlFlags.DiscretionaryAclAutoInheritRequired;
else
sdFlags |= ControlFlags.SystemAclAutoInheritRequired;
pos += 2;
} else if (ch == 'I') {
if (isDacl)
sdFlags |= ControlFlags.DiscretionaryAclAutoInherited;
else
sdFlags |= ControlFlags.SystemAclAutoInherited;
pos += 2;
} else {
throw new ArgumentException("Invalid SDDL string.", "sddlForm");
}
} else {
throw new ArgumentException("Invalid SDDL string.", "sddlForm");
}
ch = Char.ToUpperInvariant(sddlForm[pos]);
}
}
private void WriteUShort (ushort val, byte[] buffer, int offset)
{
buffer[offset] = (byte)val;
buffer[offset + 1] = (byte)(val >> 8);
}
private ushort ReadUShort (byte[] buffer, int offset)
{
return (ushort)((((int)buffer[offset + 0]) << 0)
| (((int)buffer[offset + 1]) << 8));
}
}
}
|