Advertisement
Guest User

C# Access Permission Check on Linux System

a guest
Mar 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. //  start bash with redirected output
  5. //  pass ls -l + the path to the file or directory as argument
  6. //  find the line with the name of the file or directory at the end
  7. //  and last check if the 2nd/3rd/4th char of that line is r/w/x
  8.  
  9. namespace access_test
  10. {
  11.    class Program {
  12.  
  13.       static void Main(string[] args) {
  14.          if (args.Length > 1) {
  15.             if (args[0] == "-r") {
  16.                if (HasPermission(Permission.Read, args[1]))
  17.                   Console.WriteLine("YAY!");
  18.                else
  19.                   Console.WriteLine("NEY!");
  20.             }
  21.             else if (args[0] == "-w") {
  22.                if (HasPermission(Permission.Write, args[1]))
  23.                   Console.WriteLine("YAY!");
  24.                else
  25.                   Console.WriteLine("NEY!");
  26.             }
  27.             else if (args[0] == "-x") {
  28.                if (HasPermission(Permission.Excecute, args[1]))
  29.                   Console.WriteLine("YAY!");
  30.                else
  31.                   Console.WriteLine("NEY!");
  32.             }
  33.          }
  34.       }
  35.  
  36.       static bool HasPermission(Permission permission, string path) {
  37.          if (path[path.Length - 1] == '/')
  38.             path = path.Remove(path.Length - 1);
  39.  
  40.          string p, f;
  41.          if (path.LastIndexOf('/') != -1) {
  42.             p = path.Remove(path.LastIndexOf('/'));
  43.             f = path.Substring(path.LastIndexOf('/') + 1);
  44.          }
  45.          else {
  46.             p = "";
  47.             f = path;
  48.          }
  49.  
  50.          string cmd = "ls -l \"" + p + "\"";
  51.          string output = Bash(cmd);
  52.  
  53.          if (output.Length < 4)
  54.             return false;
  55.  
  56.          if (output.Substring(0, 2) == "ls")
  57.             return false;
  58.  
  59.          string result;
  60.          do {
  61.             int br = output.IndexOf("\n");
  62.             if (br == -1)
  63.                result = output;
  64.             else
  65.                result = output.Substring(0, br);
  66.             if (result.Length > (f.Length + 1)) {
  67.                if (result.Substring(result.Length - (f.Length + 1), f.Length + 1) == " " + f)
  68.                   break;
  69.                else
  70.                   output = output.Remove(0, br + 1);
  71.             }
  72.             else
  73.                output = output.Remove(0, br + 1);
  74.          } while (output.Contains("\n"));
  75.          
  76.          if (result.Substring(result.Length - (f.Length + 1), f.Length + 1) != " " + f)
  77.             return false;
  78.  
  79.          if (result.Length < 4)
  80.             return false;
  81.  
  82.          if (result.Substring(0, 2) == "ls")
  83.             return false;
  84.  
  85.          switch (permission) {
  86.             case Permission.Read:
  87.                return (result[1] == 'r');
  88.             case Permission.Write:
  89.                return (result[2] == 'w');
  90.             case Permission.Excecute:
  91.                return (result[3] == 'x');
  92.             default:
  93.                return false;
  94.          }
  95.       }
  96.  
  97.       static string Bash(string cmd) {
  98.          string args = cmd.Replace("\"", "\\\"");
  99.  
  100.          Process p = new Process();
  101.          p.StartInfo.FileName = "/bin/bash";
  102.          p.StartInfo.Arguments = $"-c \"{args}\"";
  103.          p.StartInfo.RedirectStandardOutput = true;
  104.          p.StartInfo.RedirectStandardError = true;
  105.          p.StartInfo.UseShellExecute = false;
  106.          p.StartInfo.CreateNoWindow = true;
  107.  
  108.          p.Start();
  109.          string result = p.StandardOutput.ReadToEnd();
  110.          p.WaitForExit();
  111.          return result;
  112.       }
  113.    }
  114.    public enum Permission { Read, Write, Excecute }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement