GetSecurityInfo() static private method

static private GetSecurityInfo ( Microsoft.Win32.ResourceType resourceType, string name, SafeHandle handle, AccessControlSections accessControlSections, RawSecurityDescriptor &resultSd ) : int
resourceType Microsoft.Win32.ResourceType
name string
handle System.Runtime.InteropServices.SafeHandle
accessControlSections AccessControlSections
resultSd RawSecurityDescriptor
return int
Example #1
0
        private static CommonSecurityDescriptor CreateInternal(ResourceType resourceType, bool isContainer, string name, SafeHandle handle, AccessControlSections includeSections, bool createByName, NativeObjectSecurity.ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
        {
            if (createByName && name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (!createByName && handle == null)
            {
                throw new ArgumentNullException("handle");
            }
            RawSecurityDescriptor resultSd;
            int securityInfo = Win32.GetSecurityInfo(resourceType, name, handle, includeSections, out resultSd);

            if (securityInfo != 0)
            {
                Exception exception = (Exception)null;
                if (exceptionFromErrorCode != null)
                {
                    exception = exceptionFromErrorCode(securityInfo, name, handle, exceptionContext);
                }
                if (exception == null)
                {
                    if (securityInfo == 5)
                    {
                        exception = (Exception) new UnauthorizedAccessException();
                    }
                    else if (securityInfo == 1307)
                    {
                        exception = (Exception) new InvalidOperationException(Environment.GetResourceString("AccessControl_InvalidOwner"));
                    }
                    else if (securityInfo == 1308)
                    {
                        exception = (Exception) new InvalidOperationException(Environment.GetResourceString("AccessControl_InvalidGroup"));
                    }
                    else if (securityInfo == 87)
                    {
                        exception = (Exception) new InvalidOperationException(Environment.GetResourceString("AccessControl_UnexpectedError", (object)securityInfo));
                    }
                    else if (securityInfo == 123)
                    {
                        exception = (Exception) new ArgumentException(Environment.GetResourceString("Argument_InvalidName"), "name");
                    }
                    else if (securityInfo == 2)
                    {
                        exception = name == null ? (Exception) new FileNotFoundException() : (Exception) new FileNotFoundException(name);
                    }
                    else if (securityInfo == 1350)
                    {
                        exception = (Exception) new NotSupportedException(Environment.GetResourceString("AccessControl_NoAssociatedSecurity"));
                    }
                    else
                    {
                        exception = (Exception) new InvalidOperationException(Environment.GetResourceString("AccessControl_UnexpectedError", (object)securityInfo));
                    }
                }
                throw exception;
            }
            return(new CommonSecurityDescriptor(isContainer, false, resultSd, true));
        }
Example #2
0
        private static CommonSecurityDescriptor CreateInternal(ResourceType resourceType, bool isContainer, string name, SafeHandle handle, AccessControlSections includeSections, bool createByName, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
        {
            int error;
            RawSecurityDescriptor rawSD;

            if (createByName && name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            else if (!createByName && handle == null)
            {
                throw new ArgumentNullException(nameof(handle));
            }

            error = Win32.GetSecurityInfo(resourceType, name, handle, includeSections, out rawSD);

            if (error != Interop.Errors.ERROR_SUCCESS)
            {
                System.Exception exception = null;

                if (exceptionFromErrorCode != null)
                {
                    exception = exceptionFromErrorCode(error, name, handle, exceptionContext);
                }

                if (exception == null)
                {
                    if (error == Interop.Errors.ERROR_ACCESS_DENIED)
                    {
                        exception = new UnauthorizedAccessException();
                    }
                    else if (error == Interop.Errors.ERROR_INVALID_OWNER)
                    {
                        exception = new InvalidOperationException(SR.AccessControl_InvalidOwner);
                    }
                    else if (error == Interop.Errors.ERROR_INVALID_PRIMARY_GROUP)
                    {
                        exception = new InvalidOperationException(SR.AccessControl_InvalidGroup);
                    }
                    else if (error == Interop.Errors.ERROR_INVALID_PARAMETER)
                    {
                        exception = new InvalidOperationException(SR.Format(SR.AccessControl_UnexpectedError, error));
                    }
                    else if (error == Interop.Errors.ERROR_INVALID_NAME)
                    {
                        exception = new ArgumentException(SR.Argument_InvalidName, nameof(name));
                    }
                    else if (error == Interop.Errors.ERROR_FILE_NOT_FOUND)
                    {
                        exception = (name == null ? new FileNotFoundException() : new FileNotFoundException(name));
                    }
                    else if (error == Interop.Errors.ERROR_NO_SECURITY_ON_OBJECT)
                    {
                        exception = new NotSupportedException(SR.AccessControl_NoAssociatedSecurity);
                    }
                    else if (error == Interop.Errors.ERROR_PIPE_NOT_CONNECTED)
                    {
                        exception = new InvalidOperationException(SR.InvalidOperation_DisconnectedPipe);
                    }
                    else
                    {
                        Debug.Fail($"Win32GetSecurityInfo() failed with unexpected error code {error}");
                        exception = new InvalidOperationException(SR.Format(SR.AccessControl_UnexpectedError, error));
                    }
                }

                throw exception;
            }

            return(new CommonSecurityDescriptor(isContainer, false /* isDS */, rawSD, true));
        }
Example #3
0
        [System.Security.SecurityCritical]  // auto-generated
        private static CommonSecurityDescriptor CreateInternal(ResourceType resourceType, bool isContainer, string name, SafeHandle handle, AccessControlSections includeSections, bool createByName, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
        {
            int error;
            RawSecurityDescriptor rawSD;

            if (createByName && name == null)
            {
                throw new ArgumentNullException("name");
            }
            else if (!createByName && handle == null)
            {
                throw new ArgumentNullException("handle");
            }

            error = Win32.GetSecurityInfo(resourceType, name, handle, includeSections, out rawSD);

            if (error != Win32Native.ERROR_SUCCESS)
            {
                System.Exception exception = null;

                if (exceptionFromErrorCode != null)
                {
                    exception = exceptionFromErrorCode(error, name, handle, exceptionContext);
                }

                if (exception == null)
                {
                    if (error == Win32Native.ERROR_ACCESS_DENIED)
                    {
                        exception = new UnauthorizedAccessException();
                    }
                    else if (error == Win32Native.ERROR_INVALID_OWNER)
                    {
                        exception = new InvalidOperationException(Environment.GetResourceString("AccessControl_InvalidOwner"));
                    }
                    else if (error == Win32Native.ERROR_INVALID_PRIMARY_GROUP)
                    {
                        exception = new InvalidOperationException(Environment.GetResourceString("AccessControl_InvalidGroup"));
                    }
                    else if (error == Win32Native.ERROR_INVALID_PARAMETER)
                    {
                        exception = new InvalidOperationException(Environment.GetResourceString("AccessControl_UnexpectedError", error));
                    }
                    else if (error == Win32Native.ERROR_INVALID_NAME)
                    {
                        exception = new ArgumentException(
                            Environment.GetResourceString("Argument_InvalidName"),
                            "name");
                    }
                    else if (error == Win32Native.ERROR_FILE_NOT_FOUND)
                    {
                        exception = (name == null ? new FileNotFoundException() : new FileNotFoundException(name));
                    }
                    else if (error == Win32Native.ERROR_NO_SECURITY_ON_OBJECT)
                    {
                        exception = new NotSupportedException(Environment.GetResourceString("AccessControl_NoAssociatedSecurity"));
                    }
                    else
                    {
                        Contract.Assert(false, string.Format(CultureInfo.InvariantCulture, "Win32GetSecurityInfo() failed with unexpected error code {0}", error));
                        exception = new InvalidOperationException(Environment.GetResourceString("AccessControl_UnexpectedError", error));
                    }
                }

                throw exception;
            }

            return(new CommonSecurityDescriptor(isContainer, false /* isDS */, rawSD, true));
        }