Merge pull request #3647 from BrzVlad/fix-sgen-internal-alloc
[mono.git] / mcs / class / corlib / System.Security.AccessControl / NativeObjectSecurity.cs
index 8ee1e0e2bca6289550a6fdef6baa2663ba6ab07d..db43c8b549001e029973a5926fd63929792a1cd4 100644 (file)
@@ -38,7 +38,9 @@ namespace System.Security.AccessControl
        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,
@@ -47,7 +49,9 @@ namespace System.Security.AccessControl
                internal NativeObjectSecurity (CommonSecurityDescriptor securityDescriptor, ResourceType resourceType)
                        : base (securityDescriptor)
                {
+#if !MOBILE                    
                        resource_type = resourceType;
+#endif
                }
 
                protected NativeObjectSecurity (bool isContainer,
@@ -63,7 +67,9 @@ namespace System.Security.AccessControl
                        : base (isContainer)
                {
                        exception_from_error_code = exceptionFromErrorCode;
+#if !MOBILE                    
                        resource_type = resourceType;
+#endif
                }
                
                protected NativeObjectSecurity (bool isContainer,
@@ -129,7 +135,7 @@ namespace System.Security.AccessControl
                {
                        Persist (name, includeSections, null);
                }
-               
+       
                internal void PersistModifications (SafeHandle handle)
                {
                        WriteLock();
@@ -203,42 +209,61 @@ namespace System.Security.AccessControl
                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);
                }
@@ -249,8 +274,8 @@ namespace System.Security.AccessControl
                        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);
                }
                
@@ -258,9 +283,9 @@ namespace System.Security.AccessControl
                        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);
@@ -292,9 +317,12 @@ namespace System.Security.AccessControl
                        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;
                        
@@ -340,6 +368,17 @@ namespace System.Security.AccessControl
                        
                        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
@@ -378,13 +417,16 @@ namespace System.Security.AccessControl
                [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
        }
 }