dokan-dev / dokan-dotnet

Dokan DotNet Wrapper
http://dokan-dev.github.io
MIT License
462 stars 116 forks source link

Prevent deletion of Directory and Files #278

Closed abhilashca closed 2 years ago

abhilashca commented 2 years ago

Hello,

I am using Dokan to show the files and directories that are on my remote server (or cloud) based on the target endpoint the app points to. I want to prevent the user from deleting Directory and Files that are listed in their virtual file system.

To prevent deletion of files and folder, I am checking for DeleteOnClose in CreateFile. Below is the code snippet I have in CreateFile.

public NtStatus CreateFile(...)
{
    string path = GetPath(fileName);

    if (options == FileOptions.DeleteOnClose)
    {
        info.DeleteOnClose = false;
        return NtStatus.AccessDenied;
    }

    // rest of the code follows
}

Now, when I try to delete a directory, I get below dialogs or prompts which is as expected.

image

Now, when I try to delete a file, I get below dialog with message - "Invalid MS-DOS function":

image

I think, the message "Invalid MS-DOS function" is shown because I haven't configured something properly.

Can anyone please help on how to show proper access denied error instead of "Invalid MS-DOS function".

Thanks in advance.

Liryna commented 2 years ago

Hi @abhilashca ,

You could look at the Procmon logs filtered on the file to see if anything looks suspicious. It might also be a good idea to implement GetFileSecurity and deny the delete action. You can even set the filesystem readonly at mount if you also do not want the user to write

abhilashca commented 2 years ago

Thank you for the reply @Liryna. Much appreciated. I have enabled DokanOptions.WriteProtection at the time of mounting the disk. This has taken care of all the edit/delete scenarios.

Now, when I try to save a text file, I am getting below error message:

image

Any idea why the error message talks about bad image instead of access denied.

Liryna commented 2 years ago

It looks like you have an invalid installation of dokan. The path for dokannp1 should be in system32.

abhilashca commented 2 years ago

I figured out the issue. Within ReadFile(), if any the files are outside my remote files, then I was returning Not Implemented.

I found this in my console - 12/17/2021 20:41:14 - [DokanNet] ReadFileProxy : \System32\System32\dokannp1.dll Return : NotImplemented ReadLength : 0

I think, I am good for now. Will reopen the ticket if I get stuck again. Thank you for the help. Much appreciated.