Encountering a FileSystemRights value that isn't defined in enumeration

前端 未结 3 1124
我在风中等你
我在风中等你 2020-12-10 05:56

I\'ve written an application that examines all of the file system permissions on a directory.

A directory has a number of access rules (of type FileSystemAcces

3条回答
  •  佛祖请我去吃肉
    2020-12-10 06:22

    In some cases the FileSystemRights have bits set which do not contain any meaningfull information and can get removed. Some have a format which is not supported by the FileSystemRights class but can be converted. (The NTFS driver understands both formats). There are several documents at microsoft regarding this:

    • Access Mask Format
    • File Security and Access Rights
    • Winnt.h

    Based on this the method FileSystemRightsCorrector() cleans this data up make it "readable". There is a paremter bool removeSynchronizePermission = false which should be used with the default value, except you have the need to remove this flag also.

        public static FileSystemRights FileSystemRightsCorrector(FileSystemRights fsRights, bool removeSynchronizePermission = false)
        {
            // from: https://msdn.microsoft.com/en-us/library/aa374896%28v=vs.85%29.aspx
            const int C_BitGenericRead = (1 << 31);
            const int C_BitGenericWrite = (1 << 30);
            const int C_BitGenericExecute = (1 << 29);
            const int C_BitGenericAll = (1 << 28);
    
    
            // https://msdn.microsoft.com/en-us/library/aa364399.aspx
            // FILE_GENERIC_READ = FILE_READ_ATTRIBUTES | FILE_READ_DATA | FILE_READ_EA | STANDARD_RIGHTS_READ | SYNCHRONIZE 
            // FILE_GENERIC_WRITE = FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA | FILE_WRITE_EA | STANDARD_RIGHTS_WRITE | SYNCHRONIZE
            // FILE_GENERIC_EXECUTE  = FILE_EXECUTE | FILE_READ_ATTRIBUTES | STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE 
    
            //from Winnt.h
            //#define STANDARD_RIGHTS_READ             (READ_CONTROL)
            //#define STANDARD_RIGHTS_WRITE            (READ_CONTROL)
            //#define STANDARD_RIGHTS_EXECUTE          (READ_CONTROL)
    
            // from: https://msdn.microsoft.com/en-us/library/windows/desktop/aa379607%28v=vs.85%29.aspx
            // READ_CONTROL = "The right to read the information in the object's security descriptor,"
            // ==> STANDARD_RIGHTS_READ, STANDARD_RIGHTS_WRITE, STANDARD_RIGHTS_EXECUTE == FileSystemRights.ReadPermissions
    
            // translation for the generic rights to the FileSystemRights enum
            const FileSystemRights C_FsrGenericRead = FileSystemRights.ReadAttributes | FileSystemRights.ReadData | FileSystemRights.ReadExtendedAttributes | FileSystemRights.ReadPermissions | FileSystemRights.Synchronize;
            const FileSystemRights C_FsrGenericWrite = FileSystemRights.AppendData | FileSystemRights.WriteAttributes | FileSystemRights.WriteData | FileSystemRights.WriteExtendedAttributes | FileSystemRights.ReadPermissions | FileSystemRights.Synchronize;
            const FileSystemRights C_FsrGenericExecute = FileSystemRights.ExecuteFile | FileSystemRights.ReadAttributes | FileSystemRights.ReadPermissions | FileSystemRights.Synchronize;
    
            if (((int)fsRights & C_BitGenericRead) != 0)
            {
                fsRights |= C_FsrGenericRead;
            }
    
            if (((int)fsRights & C_BitGenericWrite) != 0)
            {
                fsRights |= C_FsrGenericWrite;
            }
    
            if (((int)fsRights & C_BitGenericExecute) != 0)
            {
                fsRights |= C_FsrGenericExecute;
            }
    
            if (((int)fsRights & C_BitGenericAll) != 0)
            {
                fsRights |= FileSystemRights.FullControl;
            }
    
            // delete the 4 highest bits if present
            fsRights = (FileSystemRights)((int)fsRights & ~(C_BitGenericRead | C_BitGenericWrite | C_BitGenericExecute | C_BitGenericAll));
    
            // For some purpouses the Synchronize flag needs to be deleted.
            // If you don't have trouble with that flag leave it untouched!
            if (removeSynchronizePermission == true)
            {
                fsRights = (FileSystemRights)((int)fsRights & ~((int)FileSystemRights.Synchronize));
            }
    
            return fsRights;
        }
    

提交回复
热议问题