public abstract class NativeObjectSecurity : CommonObjectSecurity
{
ExceptionFromErrorCode exception_from_error_code;
+#if !MOBILE
ResourceType resource_type;
+#endif
protected internal delegate Exception ExceptionFromErrorCode (int errorCode,
string name, SafeHandle handle,
internal NativeObjectSecurity (CommonSecurityDescriptor securityDescriptor, ResourceType resourceType)
: base (securityDescriptor)
{
+#if !MOBILE
resource_type = resourceType;
+#endif
}
protected NativeObjectSecurity (bool isContainer,
: base (isContainer)
{
exception_from_error_code = exceptionFromErrorCode;
+#if !MOBILE
resource_type = resourceType;
+#endif
}
protected NativeObjectSecurity (bool isContainer,
{
Persist (name, includeSections, null);
}
-
+
internal void PersistModifications (SafeHandle handle)
{
WriteLock();
internal virtual int InternalGet (SafeHandle handle,
AccessControlSections includeSections)
{
+#if MOBILE
+ throw new PlatformNotSupportedException ();
+#else
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
throw new PlatformNotSupportedException ();
- return InternalGetHelper (delegate (SecurityInfos securityInfos,
- out IntPtr owner, out IntPtr group,
- out IntPtr dacl, out IntPtr sacl, out IntPtr descriptor)
+ return Win32GetHelper (delegate (SecurityInfos securityInfos,
+ out IntPtr owner, out IntPtr group,
+ out IntPtr dacl, out IntPtr sacl, out IntPtr descriptor)
{
return GetSecurityInfo (handle, ResourceType, securityInfos,
out owner, out group,
out dacl, out sacl, out descriptor);
}, includeSections);
+#endif
}
internal virtual int InternalGet (string name,
AccessControlSections includeSections)
{
+#if MOBILE
+ throw new PlatformNotSupportedException ();
+#else
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
throw new PlatformNotSupportedException ();
- return InternalGetHelper (delegate (SecurityInfos securityInfos,
- out IntPtr owner, out IntPtr group,
- out IntPtr dacl, out IntPtr sacl, out IntPtr descriptor)
+ return Win32GetHelper (delegate (SecurityInfos securityInfos,
+ out IntPtr owner, out IntPtr group,
+ out IntPtr dacl, out IntPtr sacl, out IntPtr descriptor)
{
- return GetNamedSecurityInfo (name, ResourceType, securityInfos,
+ return GetNamedSecurityInfo (Win32FixName (name), ResourceType, securityInfos,
out owner, out group,
out dacl, out sacl, out descriptor);
}, includeSections);
+#endif
}
+#if MOBILE
+ internal virtual int InternalSet (SafeHandle handle, AccessControlSections includeSections)
+ {
+ throw new PlatformNotSupportedException ();
+ }
+
+ internal virtual int InternalSet (string name, AccessControlSections includeSections)
+ {
+ throw new PlatformNotSupportedException ();
+ }
+#else
internal virtual int InternalSet (SafeHandle handle,
AccessControlSections includeSections)
{
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
throw new PlatformNotSupportedException ();
- return InternalSetHelper ((securityInfos, owner, group, dacl, sacl) =>
+ return Win32SetHelper ((securityInfos, owner, group, dacl, sacl) =>
SetSecurityInfo (handle, ResourceType, securityInfos, owner, group, dacl, sacl),
includeSections);
}
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
throw new PlatformNotSupportedException ();
- return InternalSetHelper ((securityInfos, owner, group, dacl, sacl) =>
- SetNamedSecurityInfo (name, ResourceType, securityInfos, owner, group, dacl, sacl),
+ return Win32SetHelper ((securityInfos, owner, group, dacl, sacl) =>
+ SetNamedSecurityInfo (Win32FixName (name), ResourceType, securityInfos, owner, group, dacl, sacl),
includeSections);
}
get { return resource_type; }
}
- #region Win32 Details
- int InternalGetHelper (GetSecurityInfoNativeCall nativeCall,
- AccessControlSections includeSections)
+ #region Win32 Details
+ int Win32GetHelper (GetSecurityInfoNativeCall nativeCall,
+ AccessControlSections includeSections)
{
bool getOwner = 0 != (includeSections & AccessControlSections.Owner);
bool getGroup = 0 != (includeSections & AccessControlSections.Group);
return 0;
}
- int InternalSetHelper (SetSecurityInfoNativeCall nativeCall,
- AccessControlSections includeSections)
+ int Win32SetHelper (SetSecurityInfoNativeCall nativeCall,
+ AccessControlSections includeSections)
{
+ // SE_REGISTRY_KEY will fail UnauthorizedAccessException without this check.
+ if (AccessControlSections.None == includeSections) return 0;
+
SecurityInfos securityInfos = 0;
byte[] owner = null, group = null, dacl = null, sacl = null;
return nativeCall (securityInfos, owner, group, dacl, sacl);
}
+
+ string Win32FixName (string name)
+ {
+ if (ResourceType == ResourceType.RegistryKey) {
+ // For (Get|Set)NamedSecurityInfo, registry paths lack the HKEY_ prefix.
+ if (!name.StartsWith ("HKEY_")) throw new InvalidOperationException ();
+ name = name.Substring ("HKEY_".Length);
+ }
+
+ return name;
+ }
#endregion
#region Win32 P/Invokes
[return: MarshalAs (UnmanagedType.Bool)]
static extern bool IsValidSecurityDescriptor (IntPtr descriptor);
+ /*
struct SecurityDescriptor
{
public byte Revision, Size;
public ushort ControlFlags;
public IntPtr Owner, Group, Sacl, Dacl;
}
+ */
#endregion
+#endif
}
}