Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove All Directory Permissions

In C# (2.0) How do I remove all permissions to a directory, so I can limit the access. I will be adding access back to a limited set of users.

like image 579
C. Ross Avatar asked Sep 18 '09 15:09

C. Ross


People also ask

How do I remove all permissions in Linux?

To remove all permissions for group and world you would type chmod 700 [filename]. To give the owner all permissions and world execute you would type chmod 701 [filename]. To give the owner all permissions and world read and execute you would type chmod 705 [filename].

How do I remove all permissions from a folder and subfolders in Linux?

To change directory permissions in Linux, use the following: chmod +rwx filename to add permissions. chmod -rwx directoryname to remove permissions.


2 Answers

Disclaimer: I realise this has already been answered and accepted, and I really wanted to post this as a comment to the accepted answer, however the inability of being able to format comments has forced me to post this as an answer (which, technically, it is)....

I was looking to do the same, and found your question. Stu's answer helped me come up with this solution. (Note that I'm only interested in removing explicit security).

private static DirectorySecurity RemoveExplicitSecurity(DirectorySecurity directorySecurity)
{
    AuthorizationRuleCollection rules = directorySecurity.GetAccessRules(true, false, typeof(System.Security.Principal.NTAccount));
    foreach (FileSystemAccessRule rule in rules)
        directorySecurity.RemoveAccessRule(rule);
    return directorySecurity;
}

And this is obviously used as follows:

DirectoryInfo directoryInfo = new DirectoryInfo(path);
DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
directorySecurity = RemoveExplicitSecurity(directorySecurity);
Directory.SetAccessControl(path, directorySecurity);
like image 97
Bryan Avatar answered Oct 11 '22 07:10

Bryan


Look at the classes in the System.Security.AccessControl namespace, and especially the DirectorySecurity.RemoveAccessRule method.

Also, if you remove all the permissions then you won't be able to add any back afterwards :-)

like image 27
Stu Mackellar Avatar answered Oct 11 '22 06:10

Stu Mackellar