From 71494a5acfb62d6995816a51ed7fb57e2c32eb10 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Thu, 21 Sep 2017 12:33:33 -0400 Subject: [PATCH] Added an out parameter indicating an UnauthorizedAccessException --- TGCommandLine/ConfigCommands.cs | 6 +++--- TGServerService/Config.cs | 22 +++++++++++++++++++--- TGServiceInterface/Config.cs | 9 ++++++--- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/TGCommandLine/ConfigCommands.cs b/TGCommandLine/ConfigCommands.cs index 2d1b4e0a22..47dfee862d 100644 --- a/TGCommandLine/ConfigCommands.cs +++ b/TGCommandLine/ConfigCommands.cs @@ -36,7 +36,7 @@ namespace TGCommandLine } protected override ExitCode Run(IList parameters) { - var list = Server.GetComponent().ListStaticDirectory(parameters.Count > 0 ? parameters[0] : null, out string error); + var list = Server.GetComponent().ListStaticDirectory(parameters.Count > 0 ? parameters[0] : null, out string error, out bool unauthorized); if(list == null) { OutputProc(error); @@ -80,7 +80,7 @@ namespace TGCommandLine protected override ExitCode Run(IList parameters) { - var bytes = Server.GetComponent().ReadText(parameters[0], parameters.Count > 2 && parameters[2].ToLower() == "--repo", out string error); + var bytes = Server.GetComponent().ReadText(parameters[0], parameters.Count > 2 && parameters[2].ToLower() == "--repo", out string error, out bool unauthorized); if(bytes == null) { OutputProc("Error: " + error); @@ -120,7 +120,7 @@ namespace TGCommandLine { try { - var res = Server.GetComponent().WriteText(parameters[0], File.ReadAllText(parameters[1])); + var res = Server.GetComponent().WriteText(parameters[0], File.ReadAllText(parameters[1]), out bool unauthorized); if (res != null) { OutputProc("Error: " + res); diff --git a/TGServerService/Config.cs b/TGServerService/Config.cs index a705bec2d1..58a9d9d6f4 100644 --- a/TGServerService/Config.cs +++ b/TGServerService/Config.cs @@ -20,7 +20,7 @@ namespace TGServerService //public api [OperationBehavior(Impersonation = ImpersonationOption.Required)] - public string ReadText(string staticRelativePath, bool repo, out string error) + public string ReadText(string staticRelativePath, bool repo, out string error, out bool unauthorized) { try { @@ -37,6 +37,7 @@ namespace TGServerService if (Config == null) { error = "Unable to load static directory configuration"; + unauthorized = false; return null; } var Found = false; @@ -51,6 +52,7 @@ namespace TGServerService if (!Found) { error = "File is not in a configured static directory!"; + unauthorized = false; return null; } } @@ -71,10 +73,12 @@ namespace TGServerService if (!good) { error = "Cannot read above static directories!"; + unauthorized = false; return null; } error = null; + unauthorized = false; return File.ReadAllText(path); } } @@ -82,16 +86,18 @@ namespace TGServerService { //no need for the full stacktrace error = e.Message; + unauthorized = true; return null; } catch (Exception e) { error = e.ToString(); + unauthorized = false; return null; } } [OperationBehavior(Impersonation = ImpersonationOption.Required)] - public string WriteText(string staticRelativePath, string data) + public string WriteText(string staticRelativePath, string data, out bool unauthorized) { try { @@ -114,25 +120,32 @@ namespace TGServerService } if (!good) + { + unauthorized = false; return "Cannot write above static directories!"; + } + Directory.CreateDirectory(destdir); File.WriteAllText(path, data); + unauthorized = false; return null; } } catch (UnauthorizedAccessException e) { //no need for the full stacktrace + unauthorized = true; return e.Message; } catch (Exception e) { + unauthorized = false; return e.ToString(); } } [OperationBehavior(Impersonation = ImpersonationOption.Required)] - public IList ListStaticDirectory(string subDir, out string error) + public IList ListStaticDirectory(string subDir, out string error, out bool unauthorized) { try { @@ -143,17 +156,20 @@ namespace TGServerService foreach (var I in dirToEnum.GetDirectories()) result.Add('/' + I.Name); error = null; + unauthorized = false; return result; } catch (UnauthorizedAccessException e) { //no need for the full stacktrace error = e.Message; + unauthorized = true; return null; } catch (Exception e) { error = e.ToString(); + unauthorized = false; return null; } } diff --git a/TGServiceInterface/Config.cs b/TGServiceInterface/Config.cs index 6af5f6c675..16a868304f 100644 --- a/TGServiceInterface/Config.cs +++ b/TGServiceInterface/Config.cs @@ -24,9 +24,10 @@ namespace TGServiceInterface /// /// Subdirectory to enumerate, enumerates the root directory if null /// null on success, error message on failure + /// This will be true if error is set to a message that indicates the current user does not have access to the specified file /// A list of files in the enumerated static directory on success, null on failure [OperationContract] - IList ListStaticDirectory(string subpath, out string error); + IList ListStaticDirectory(string subpath, out string error, out bool unauthorized); /// /// For when you really just need to see the raw data of the config @@ -34,17 +35,19 @@ namespace TGServiceInterface /// The path from the Static dir. E.g. config/config.txt /// if true, the file will be read from the repository instead of the static dir /// null on success, error message on failure + /// This will be true if error is set to a message that indicates the current user does not have access to the specified file /// The full text of the file on success, null on failure [OperationContract] - string ReadText(string staticRelativePath, bool repo, out string error); + string ReadText(string staticRelativePath, bool repo, out string error, out bool unauthorized); /// /// For when you really just need to set the raw data of the config /// /// The path from the configDir. E.g. config.txt /// The full text of the config file + /// This will be true if error is set to a message that indicates the current user does not have access to the specified file /// null on success, error message on failure [OperationContract] - string WriteText(string staticRelativePath, string data); + string WriteText(string staticRelativePath, string data, out bool unauthorized); } }