Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- namespace access_test
- {
- class Program {
- static void Main(string[] args) {
- if (args.Length > 1) {
- if (args[0] == "-r") {
- if (HasPermission(Permission.Read, args[1]))
- Console.WriteLine("YAY!");
- else
- Console.WriteLine("NEY!");
- }
- else if (args[0] == "-w") {
- if (HasPermission(Permission.Write, args[1]))
- Console.WriteLine("YAY!");
- else
- Console.WriteLine("NEY!");
- }
- else if (args[0] == "-x") {
- if (HasPermission(Permission.Excecute, args[1]))
- Console.WriteLine("YAY!");
- else
- Console.WriteLine("NEY!");
- }
- }
- }
- static bool HasPermission(Permission permission, string path) {
- if (path[path.Length - 1] == '/')
- path = path.Remove(path.Length - 1);
- string p, f;
- if (path.LastIndexOf('/') != -1) {
- p = path.Remove(path.LastIndexOf('/'));
- f = path.Substring(path.LastIndexOf('/') + 1);
- }
- else {
- p = "";
- f = path;
- }
- string cmd = "ls -l \"" + p + "\"";
- string output = Bash(cmd);
- if (output.Length < 4)
- return false;
- if (output.Substring(0, 2) == "ls")
- return false;
- string result;
- do {
- int br = output.IndexOf("\n");
- if (br == -1)
- result = output;
- else
- result = output.Substring(0, br);
- if (result.Length > (f.Length + 1)) {
- if (result.Substring(result.Length - (f.Length + 1), f.Length + 1) == " " + f)
- break;
- else
- output = output.Remove(0, br + 1);
- }
- else
- output = output.Remove(0, br + 1);
- } while (output.Contains("\n"));
- if (result.Substring(result.Length - (f.Length + 1), f.Length + 1) != " " + f)
- return false;
- if (result.Length < 4)
- return false;
- if (result.Substring(0, 2) == "ls")
- return false;
- switch (permission) {
- case Permission.Read:
- return (result[1] == 'r');
- case Permission.Write:
- return (result[2] == 'w');
- case Permission.Excecute:
- return (result[3] == 'x');
- default:
- return false;
- }
- }
- static string Bash(string cmd) {
- string args = cmd.Replace("\"", "\\\"");
- Process p = new Process();
- p.StartInfo.FileName = "/bin/bash";
- p.StartInfo.Arguments = $"-c \"{args}\"";
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.RedirectStandardError = true;
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.CreateNoWindow = true;
- p.Start();
- string result = p.StandardOutput.ReadToEnd();
- p.WaitForExit();
- return result;
- }
- }
- public enum Permission { Read, Write, Excecute }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement