Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
#55 copy config to user folder and path to registry. assimilate confi…
Browse files Browse the repository at this point in the history
…g to user system #95
  • Loading branch information
SailReal committed Aug 5, 2018
1 parent 081a2a3 commit 28f9400
Show file tree
Hide file tree
Showing 3 changed files with 6,855 additions and 228 deletions.
56 changes: 53 additions & 3 deletions SharkCage/CageServiceInstaller/CageServiceInstaller.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using System;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Security.Principal;

namespace CageServiceInstaller
{
Expand All @@ -20,6 +25,9 @@ public override void Commit(System.Collections.IDictionary saved_state)
base.Commit(saved_state);

string target_dir = Context.Parameters["DP_TargetDir"].ToString();

AssimilateConfigToCurrentSystem(target_dir);

InstallAndStartService(target_dir);
}

Expand All @@ -35,12 +43,54 @@ public override void Uninstall(System.Collections.IDictionary saved_state)
UninstallService();
}

private void InstallAndStartService(string path)
private void AssimilateConfigToCurrentSystem(string dir_path)
{
const string APPLICATION_PATH_PROPERTY = "application_path";
const string APPLICATION_HASH_PROPERTY = "binary_hash";
var path_to_config_exe = dir_path + "CageConfigurator.exe";

var config_path = Environment.ExpandEnvironmentVariables("%SystemDrive%\\Users\\Public\\Documents\\SharkCage\\CageConfigurator.sconfig");

var json = JObject.Parse(File.ReadAllText(config_path));
var application_path = json.GetValue(APPLICATION_PATH_PROPERTY).ToString();
json[APPLICATION_PATH_PROPERTY] = path_to_config_exe;
json[APPLICATION_HASH_PROPERTY] = GetSha512Hash(path_to_config_exe);
var output = Newtonsoft.Json.JsonConvert.SerializeObject(json, Newtonsoft.Json.Formatting.Indented);

File.WriteAllText(config_path, output);

// generate acl for config (only admin group has access)
IdentityReference built_in_administrators = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
var file_security = new FileSecurity();

file_security.SetOwner(built_in_administrators);
foreach (FileSystemAccessRule fs_access_rule in file_security.GetAccessRules(true, true, typeof(SecurityIdentifier)))
{
file_security.RemoveAccessRule(fs_access_rule);
}
file_security.AddAccessRule(new FileSystemAccessRule(built_in_administrators, FileSystemRights.FullControl, AccessControlType.Allow));
file_security.SetAccessRuleProtection(true, false);

File.SetAccessControl(config_path, file_security);
}

private string GetSha512Hash(string file_path)
{
const int buffer_size = 1048576; // ~ 1MB per read
using (var bs = new BufferedStream(File.OpenRead(file_path), buffer_size))
{
var sha = new SHA512Managed();
byte[] hash = sha.ComputeHash(bs);
return BitConverter.ToString(hash).Replace("-", String.Empty);
}
}

private void InstallAndStartService(string dir_path)
{
try
{
UninstallService();
ServiceInstaller.InstallAndStart(service_name, service_name, path);
ServiceInstaller.InstallAndStart(service_name, service_name, dir_path + "CageService.exe");
}
catch (Exception ex)
{
Expand Down
3 changes: 3 additions & 0 deletions SharkCage/CageServiceInstaller/CageServiceInstaller.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\netstandard2.0\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" />
Expand Down
Loading

0 comments on commit 28f9400

Please sign in to comment.