Check for sudo privileges

This commit is contained in:
ProfessionalUwU 2023-06-27 01:36:30 +02:00
parent 177e8e1190
commit 99cf9bd2bd
Signed by: ProfessionalUwU
GPG Key ID: 9F28CB1645C4BFB5
2 changed files with 36 additions and 3 deletions

View File

@ -14,6 +14,8 @@ public class Program {
}
WhereisUtils utils = new WhereisUtils();
utils.prepare();
string fileName = Path.GetFileNameWithoutExtension(args[0]);
string fileExtension = Path.GetExtension(args[0]);;
@ -25,6 +27,7 @@ public class Program {
break;
}
Console.ResetColor();
ConcurrentBag<string> files = utils.FindFiles(fileName, fileExtension, searchPath, maxDepth: 5);
bool foundFiles = false;

View File

@ -2,9 +2,39 @@
namespace Whereis.Utils;
public class WhereisUtils {
private readonly string[] ExcludedDirectories = { "/dev", "/proc", "/tmp", "/boot", "/run", "/root", "/sys", "/etc", "/var", "/lib", "/lib64", "/usr", "/sbin", "/lost+found" };
public ConcurrentBag<string> FindFiles(string fileName, string fileExtension, string searchPath, int depth = 0, int maxDepth = 5) {
public string[] ExcludedDirectories = new string[] {};
public string[] prepare() {
if (wasExecutedWithSudo() == false) {
ExcludedDirectories = new string[] { "/dev", "/proc", "/tmp", "/boot", "/run", "/root", "/sys", "/etc", "/var", "/lib", "/lib64", "/usr", "/sbin", "/lost+found" };
} else {
ExcludedDirectories = new string[] { "/dev", "/proc" };
}
return ExcludedDirectories;
}
private bool wasExecutedWithSudo() {
bool sudo = false;
try {
string protectedDirectoryPath = "/root";
// Attempt to open the protected directory
Directory.GetFiles(protectedDirectoryPath);
// Access succeeded
Console.WriteLine("Program executed with sudo privileges.");
sudo = true;
}
catch (UnauthorizedAccessException) {
// Access denied
Console.WriteLine("Program executed without sudo privileges.");
}
return sudo;
}
public ConcurrentBag<string> FindFiles(string fileName, string fileExtension, string searchPath, int depth = 0, int maxDepth = 5) {
var foundFiles = new ConcurrentBag<string>();
if (depth > maxDepth) {
@ -52,4 +82,4 @@ public class WhereisUtils {
return false;
}
}
}