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 245
|
//
// System.Security.AccessControl.ObjectAce implementation
//
// Authors:
// Dick Porter <dick@ximian.com>
// Atsushi Enomoto <atsushi@ximian.com>
// Kenneth Bell
// James Bellinger <jfb@zer7.com>
//
// Copyright (C) 2006-2007 Novell, Inc (http://www.novell.com)
// Copyright (C) 2012 James Bellinger
//
// 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.Security.Principal;
using System.Globalization;
namespace System.Security.AccessControl
{
public sealed class ObjectAce : QualifiedAce
{
private Guid object_ace_type;
private Guid inherited_object_type;
private ObjectAceFlags object_ace_flags;
public ObjectAce (AceFlags aceFlags, AceQualifier qualifier,
int accessMask, SecurityIdentifier sid,
ObjectAceFlags flags, Guid type,
Guid inheritedType, bool isCallback,
byte[] opaque)
: base (ConvertType(qualifier, isCallback), aceFlags, opaque)
{
AccessMask = accessMask;
SecurityIdentifier = sid;
ObjectAceFlags = flags;
ObjectAceType = type;
InheritedObjectAceType = inheritedType;
}
internal ObjectAce (AceType type, AceFlags flags, int accessMask,
SecurityIdentifier sid, ObjectAceFlags objFlags,
Guid objType, Guid inheritedType, byte[] opaque)
: base(type, flags, opaque)
{
AccessMask = accessMask;
SecurityIdentifier = sid;
ObjectAceFlags = objFlags;
ObjectAceType = objType;
InheritedObjectAceType = inheritedType;
}
internal ObjectAce(byte[] binaryForm, int offset)
: base(binaryForm, offset)
{
int len = ReadUShort(binaryForm, offset + 2);
int lenMinimum = 12 + SecurityIdentifier.MinBinaryLength;
if (offset > binaryForm.Length - len)
throw new ArgumentException("Invalid ACE - truncated", "binaryForm");
if (len < lenMinimum)
throw new ArgumentException("Invalid ACE", "binaryForm");
AccessMask = ReadInt(binaryForm, offset + 4);
ObjectAceFlags = (ObjectAceFlags)ReadInt(binaryForm, offset + 8);
if (ObjectAceTypePresent) lenMinimum += 16;
if (InheritedObjectAceTypePresent) lenMinimum += 16;
if (len < lenMinimum)
throw new ArgumentException("Invalid ACE", "binaryForm");
int pos = 12;
if (ObjectAceTypePresent) {
ObjectAceType = ReadGuid(binaryForm, offset + pos); pos += 16;
}
if (InheritedObjectAceTypePresent) {
InheritedObjectAceType = ReadGuid(binaryForm, offset + pos); pos += 16;
}
SecurityIdentifier = new SecurityIdentifier(binaryForm, offset + pos);
pos += SecurityIdentifier.BinaryLength;
int opaqueLen = len - pos;
if (opaqueLen > 0) {
byte[] opaque = new byte[opaqueLen];
Array.Copy(binaryForm, offset + pos, opaque, 0, opaqueLen);
SetOpaque (opaque);
}
}
public override int BinaryLength
{
get {
int length = 12 + SecurityIdentifier.BinaryLength + OpaqueLength;
if (ObjectAceTypePresent) length += 16;
if (InheritedObjectAceTypePresent) length += 16;
return length;
}
}
public Guid InheritedObjectAceType {
get { return inherited_object_type; }
set { inherited_object_type = value; }
}
bool InheritedObjectAceTypePresent {
get { return 0 != (ObjectAceFlags & ObjectAceFlags.InheritedObjectAceTypePresent); }
}
public ObjectAceFlags ObjectAceFlags {
get { return object_ace_flags; }
set { object_ace_flags = value; }
}
public Guid ObjectAceType {
get { return object_ace_type; }
set { object_ace_type = value; }
}
bool ObjectAceTypePresent {
get { return 0 != (ObjectAceFlags & ObjectAceFlags.ObjectAceTypePresent); }
}
public override void GetBinaryForm (byte[] binaryForm, int offset)
{
int len = BinaryLength;
binaryForm[offset++] = (byte)this.AceType;
binaryForm[offset++] = (byte)this.AceFlags;
WriteUShort ((ushort)len, binaryForm, offset); offset += 2;
WriteInt (AccessMask, binaryForm, offset); offset += 4;
WriteInt ((int)ObjectAceFlags, binaryForm, offset); offset += 4;
if (0 != (ObjectAceFlags & ObjectAceFlags.ObjectAceTypePresent)) {
WriteGuid (ObjectAceType, binaryForm, offset); offset += 16;
}
if (0 != (ObjectAceFlags & ObjectAceFlags.InheritedObjectAceTypePresent)) {
WriteGuid (InheritedObjectAceType, binaryForm, offset); offset += 16;
}
SecurityIdentifier.GetBinaryForm (binaryForm, offset);
offset += SecurityIdentifier.BinaryLength;
byte[] opaque = GetOpaque ();
if (opaque != null) {
Array.Copy (opaque, 0, binaryForm, offset, opaque.Length);
offset += opaque.Length;
}
}
public static int MaxOpaqueLength (bool isCallback)
{
// Varies by platform?
return 65423;
}
internal override string GetSddlForm()
{
if (OpaqueLength != 0)
throw new NotImplementedException (
"Unable to convert conditional ACEs to SDDL");
string objType = "";
if ((ObjectAceFlags & ObjectAceFlags.ObjectAceTypePresent) != 0)
objType = object_ace_type.ToString("D");
string inhObjType = "";
if ((ObjectAceFlags & ObjectAceFlags.InheritedObjectAceTypePresent) != 0)
inhObjType = inherited_object_type.ToString("D");
return string.Format (CultureInfo.InvariantCulture,
"({0};{1};{2};{3};{4};{5})",
GetSddlAceType (AceType),
GetSddlAceFlags (AceFlags),
GetSddlAccessRights (AccessMask),
objType,
inhObjType,
SecurityIdentifier.GetSddlForm ());
}
private static AceType ConvertType(AceQualifier qualifier, bool isCallback)
{
switch(qualifier)
{
case AceQualifier.AccessAllowed:
if (isCallback)
return AceType.AccessAllowedCallbackObject;
else
return AceType.AccessAllowedObject;
case AceQualifier.AccessDenied:
if (isCallback)
return AceType.AccessDeniedCallbackObject;
else
return AceType.AccessDeniedObject;
case AceQualifier.SystemAlarm:
if (isCallback)
return AceType.SystemAlarmCallbackObject;
else
return AceType.SystemAlarmObject;
case AceQualifier.SystemAudit:
if (isCallback)
return AceType.SystemAuditCallbackObject;
else
return AceType.SystemAuditObject;
default:
throw new ArgumentException("Unrecognized ACE qualifier: " + qualifier, "qualifier");
}
}
private void WriteGuid (Guid val, byte[] buffer,
int offset)
{
byte[] guidData = val.ToByteArray();
Array.Copy(guidData, 0, buffer, offset, 16);
}
private Guid ReadGuid(byte[] buffer, int offset)
{
byte[] temp = new byte[16];
Array.Copy(buffer, offset, temp, 0, 16);
return new Guid(temp);
}
}
}
|