Advertisement
Guest User

C# Access Permission Check on Linux System

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