示例#1
0
        public FileSecurity GetAccessControl()
        {
            IntPtr pSidOwner, pSidGroup, pDacl, pSacl;
            SafeGlobalMemoryBufferHandle pSecurityDescriptor;

            var lastError = (int)SecurityNativeMethods.GetSecurityInfo(SafeFileHandle, SE_OBJECT_TYPE.SE_FILE_OBJECT, SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION | SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SECURITY_INFORMATION.LABEL_SECURITY_INFORMATION | SECURITY_INFORMATION.DACL_SECURITY_INFORMATION | SECURITY_INFORMATION.SACL_SECURITY_INFORMATION, out pSidOwner, out pSidGroup, out pDacl, out pSacl, out pSecurityDescriptor);

            try
            {
                if (lastError != Win32Errors.ERROR_SUCCESS)
                {
                    NativeError.ThrowException(lastError);
                }

                if (null != pSecurityDescriptor && pSecurityDescriptor.IsInvalid)
                {
                    pSecurityDescriptor.Close();
                    pSecurityDescriptor = null;

                    throw new IOException(new Win32Exception((int)Win32Errors.ERROR_INVALID_SECURITY_DESCR).Message);
                }


                var length        = SecurityNativeMethods.GetSecurityDescriptorLength(pSecurityDescriptor);
                var managedBuffer = new byte[length];


                // .CopyTo() does not work there?
                if (null != pSecurityDescriptor)
                {
                    pSecurityDescriptor.CopyTo(managedBuffer, 0, (int)length);
                }


                var fs = new FileSecurity();
                fs.SetSecurityDescriptorBinaryForm(managedBuffer);

                return(fs);
            }
            finally
            {
                if (null != pSecurityDescriptor)
                {
                    pSecurityDescriptor.Close();
                }
            }
        }
        public FileSecurity GetAccessControl()
        {
            IntPtr pSidOwner, pSidGroup, pDacl, pSacl;
            SafeGlobalMemoryBufferHandle pSecurityDescriptor;

            uint lastError = SecurityNativeMethods.GetSecurityInfo(SafeFileHandle, ObjectType.FileObject,
                                                                   SecurityInformation.Group | SecurityInformation.Owner | SecurityInformation.Label | SecurityInformation.Dacl | SecurityInformation.Sacl,
                                                                   out pSidOwner, out pSidGroup, out pDacl, out pSacl, out pSecurityDescriptor);

            try
            {
                if (lastError != Win32Errors.ERROR_SUCCESS)
                {
                    NativeError.ThrowException((int)lastError);
                }

                if (pSecurityDescriptor != null && pSecurityDescriptor.IsInvalid)
                {
                    pSecurityDescriptor.Close();
                    throw new IOException(Resources.Returned_Invalid_Security_Descriptor);
                }

                uint length = SecurityNativeMethods.GetSecurityDescriptorLength(pSecurityDescriptor);

                byte[] managedBuffer = new byte[length];


                // .CopyTo() does not work there?
                if (pSecurityDescriptor != null)
                {
                    pSecurityDescriptor.CopyTo(managedBuffer, 0, (int)length);
                }


                var fs = new FileSecurity();
                fs.SetSecurityDescriptorBinaryForm(managedBuffer);

                return(fs);
            }
            finally
            {
                if (pSecurityDescriptor != null)
                {
                    pSecurityDescriptor.Close();
                }
            }
        }
示例#3
0
 /// <summary>When overridden in a derived class, executes the code required to free the handle.</summary>
 /// <returns>
 /// <c>true</c> if the handle is released successfully; otherwise, in the event of a catastrophic failure,
 /// <c>false</c>. In this case, it generates a ReleaseHandleFailed Managed Debugging Assistant.
 /// </returns>
 protected override bool ReleaseHandle()
 {
     return(handle == IntPtr.Zero || NativeMethods.LocalFree(handle) == IntPtr.Zero);
 }