Exemple #1
0
        private static byte[] BinaryFormFromSddlForm(string sddlForm)
        {
            if (sddlForm == null)
            {
                throw new ArgumentNullException("sddlForm");
            }
            IntPtr resultSd       = IntPtr.Zero;
            uint   resultSdLength = 0;

            byte[] destination = (byte[])null;
            try
            {
                if (1 != Win32Native.ConvertStringSdToSd(sddlForm, (uint)GenericSecurityDescriptor.Revision, out resultSd, ref resultSdLength))
                {
                    switch (Marshal.GetLastWin32Error())
                    {
                    case 87:
                    case 1336:
                    case 1338:
                    case 1305:
                        throw new ArgumentException(Environment.GetResourceString("ArgumentException_InvalidSDSddlForm"), "sddlForm");

                    case 8:
                        throw new OutOfMemoryException();

                    case 1337:
                        throw new ArgumentException(Environment.GetResourceString("AccessControl_InvalidSidInSDDLString"), "sddlForm");

                    case 0:
                        break;

                    default:
                        throw new SystemException();
                    }
                }
                destination = new byte[(int)resultSdLength];
                Marshal.Copy(resultSd, destination, 0, (int)resultSdLength);
            }
            finally
            {
                if (resultSd != IntPtr.Zero)
                {
                    Win32Native.LocalFree(resultSd);
                }
            }
            return(destination);
        }
        private static byte[] BinaryFormFromSddlForm(string sddlForm)
        {
            if (sddlForm == null)
            {
                throw new ArgumentNullException("sddlForm");
            }
            IntPtr zero           = IntPtr.Zero;
            uint   resultSdLength = 0;

            byte[] destination = null;
            try
            {
                if (1 != Win32Native.ConvertStringSdToSd(sddlForm, GenericSecurityDescriptor.Revision, out zero, ref resultSdLength))
                {
                    switch (Marshal.GetLastWin32Error())
                    {
                    case 0x57:
                    case 0x538:
                    case 0x53a:
                    case 0x519:
                        throw new ArgumentException(Environment.GetResourceString("ArgumentException_InvalidSDSddlForm"), "sddlForm");

                    case 8:
                        throw new OutOfMemoryException();

                    case 0x539:
                        throw new ArgumentException(Environment.GetResourceString("AccessControl_InvalidSidInSDDLString"), "sddlForm");

                    case 0:
                        goto Label_0092;
                    }
                    throw new SystemException();
                }
Label_0092:
                destination = new byte[resultSdLength];
                Marshal.Copy(zero, destination, 0, (int)resultSdLength);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Win32Native.LocalFree(zero);
                }
            }
            return(destination);
        }
Exemple #3
0
        private static byte[] BinaryFormFromSddlForm(string sddlForm)
        {
            if (sddlForm == null)
            {
                throw new ArgumentNullException("sddlForm");
            }
            IntPtr zero = IntPtr.Zero;
            uint   num  = 0U;

            byte[] array = null;
            try
            {
                if (1 != Win32Native.ConvertStringSdToSd(sddlForm, (uint)GenericSecurityDescriptor.Revision, out zero, ref num))
                {
                    int lastWin32Error = Marshal.GetLastWin32Error();
                    if (lastWin32Error == 87 || lastWin32Error == 1336 || lastWin32Error == 1338 || lastWin32Error == 1305)
                    {
                        throw new ArgumentException(Environment.GetResourceString("ArgumentException_InvalidSDSddlForm"), "sddlForm");
                    }
                    if (lastWin32Error == 8)
                    {
                        throw new OutOfMemoryException();
                    }
                    if (lastWin32Error == 1337)
                    {
                        throw new ArgumentException(Environment.GetResourceString("AccessControl_InvalidSidInSDDLString"), "sddlForm");
                    }
                    if (lastWin32Error != 0)
                    {
                        throw new SystemException();
                    }
                }
                array = new byte[num];
                Marshal.Copy(zero, array, 0, (int)num);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Win32Native.LocalFree(zero);
                }
            }
            return(array);
        }
Exemple #4
0
        [System.Security.SecurityCritical]  // auto-generated
        private static byte[] BinaryFormFromSddlForm(string sddlForm)
        {
            if (sddlForm == null)
            {
                throw new ArgumentNullException("sddlForm");
            }
            Contract.EndContractBlock();

            int                error;
            IntPtr             byteArray     = IntPtr.Zero;
            uint               byteArraySize = 0;
            const System.Int32 TRUE          = 1;

            byte[] binaryForm = null;

            try
            {
                if (TRUE != Win32Native.ConvertStringSdToSd(
                        sddlForm,
                        GenericSecurityDescriptor.Revision,
                        out byteArray,
                        ref byteArraySize))
                {
                    error = Marshal.GetLastWin32Error();

                    if (error == Win32Native.ERROR_INVALID_PARAMETER ||
                        error == Win32Native.ERROR_INVALID_ACL ||
                        error == Win32Native.ERROR_INVALID_SECURITY_DESCR ||
                        error == Win32Native.ERROR_UNKNOWN_REVISION)
                    {
                        throw new ArgumentException(
                                  Environment.GetResourceString("ArgumentException_InvalidSDSddlForm"),
                                  "sddlForm");
                    }
                    else if (error == Win32Native.ERROR_NOT_ENOUGH_MEMORY)
                    {
                        throw new OutOfMemoryException();
                    }
                    else if (error == Win32Native.ERROR_INVALID_SID)
                    {
                        throw new ArgumentException(
                                  Environment.GetResourceString("AccessControl_InvalidSidInSDDLString"),
                                  "sddlForm");
                    }
                    else if (error != Win32Native.ERROR_SUCCESS)
                    {
                        Contract.Assert(false, string.Format(CultureInfo.InvariantCulture, "Unexpected error out of Win32.ConvertStringSdToSd: {0}", error));
                        throw new SystemException();
                    }
                }

                binaryForm = new byte[byteArraySize];

                //
                // Extract the data from the returned pointer
                //

                Marshal.Copy(byteArray, binaryForm, 0, ( int )byteArraySize);
            }
            finally
            {
                //
                // Now is a good time to get rid of the returned pointer
                //
                if (byteArray != IntPtr.Zero)
                {
                    Win32Native.LocalFree(byteArray);
                }
            }

            return(binaryForm);
        }