4

I would like to know how to check the access permission for files and directories on a Linux system while using C#.

I want to know if i can read, write or excecute.

System.IO.Directory.GetAccessControl() does not work on Linux.

Unhandled Exception: System.PlatformNotSupportedExcpetion: Operation is not supported on this platform.

trace starts at: System.Security.AccessControl.NativeObjectSecurity.InternalGet

The Method I tried to use:

static bool HasDirectoryPermission(FileSystemRights right, string path) {
   var allowed = false;
   var denied = false;
   var accessControlList = System.IO.Directory.GetAccessControl(path);
   if (accessControlList == null)
      return false;
   var accessRules = accessControlList.GetAccessRules(true, true,
                                 typeof(System.Security.Principal.SecurityIdentifier));
   if (accessRules == null)
      return false;

   foreach (FileSystemAccessRule rule in accessRules) {
      if ((right & rule.FileSystemRights) != right)
         continue;

      if (rule.AccessControlType == AccessControlType.Allow)
         allowed = true;
      else if (rule.AccessControlType == AccessControlType.Deny)
         denied = true;
   }

   return (allowed && !denied);
}

The Method for file paths is pretty much the same.

globalenemy
  • 109
  • 2
  • 11
  • 1
    Maybe this answer will help. https://stackoverflow.com/a/41019841/758848 – Steve Mar 22 '18 at 13:47
  • oh lord. i hope theres another solution.. after installing the package: https://i.imgur.com/1dy92CS.png – globalenemy Mar 22 '18 at 14:09
  • when using framework 4.7 i don't need to install any additional packages... however I would like to use something lower. Like 4.5 – globalenemy Mar 22 '18 at 14:24
  • Only .NET Core is officially supported on Linux, so your statement of "I would like to use something lower like 4.5" is simply invalid. – Lex Li Mar 22 '18 at 14:46
  • no. this doesnt work. still end up with the same exception. I don't really get how to use it honestly. It adds some extension methods to `DirectoryInfo` but even without this package I have access to theese methods. – globalenemy Mar 22 '18 at 14:55
  • @ Lex Li : I don't understand what u mean. .Net Framework is working fine on linux together with mono. – globalenemy Mar 22 '18 at 14:59

1 Answers1

1

I came up with a kinda 'dirty' solution myself.

  • I start bash with redirected output
  • pass ls -l + the path to the file or directory as argument
  • find the line with the name of the file or directory at the end
  • and last I check if the 2nd/3rd/4th char of that line is r/w/x

https://pastebin.com/1PQNaB1m

globalenemy
  • 109
  • 2
  • 11