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

Commit

Permalink
Merge pull request #85 from SharkCagey/75-config-file-acl
Browse files Browse the repository at this point in the history
Check access rights of config files before opening them
  • Loading branch information
DonatJR authored Jul 30, 2018
2 parents fd1e245 + 69056c2 commit bc56a0d
Show file tree
Hide file tree
Showing 11 changed files with 383 additions and 292 deletions.
10 changes: 0 additions & 10 deletions SharkCage/CageChooser/App.config
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="CageChooser.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<userSettings>
<CageChooser.Properties.Settings>
<setting name="PersistentConfigPath" serializeAs="String">
<value />
</setting>
</CageChooser.Properties.Settings>
</userSettings>
</configuration>
92 changes: 32 additions & 60 deletions SharkCage/CageChooser/CageChooserForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

169 changes: 67 additions & 102 deletions SharkCage/CageChooser/CageChooserForm.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using CageChooser.Properties;
using Microsoft.Win32;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
Expand All @@ -10,6 +10,9 @@ namespace CageChooser
{
public partial class CageChooserForm : Form
{
private const string REGISTRY_KEY = @"HKEY_LOCAL_MACHINE\SOFTWARE\SharkCage";
private Dictionary<string, string> config_name_value_mapping = new Dictionary<string, string>();

private class NativeMethods
{
[DllImport("SharedFunctionality.dll", CallingConvention = CallingConvention.Cdecl)]
Expand All @@ -30,130 +33,76 @@ public CageChooserForm()

private void CageChooser_Load(object sender, EventArgs e)
{
if (!File.Exists(Settings.Default.PersistentConfigPath))
{
configPath.Text = String.Empty;
}
if (Settings.Default.PersistentLRUConfigs != null)
{
foreach (string lruConfig in Settings.Default.PersistentLRUConfigs)
{
if (File.Exists(lruConfig))
{
lruConfigs.Items.Add(lruConfig);
}
}
}
LoadConfigList();
}

private void CageChooser_FormClosed(object sender, FormClosedEventArgs e)
private void LoadConfigList()
{
Settings.Default.PersistentLRUConfigs = new System.Collections.Specialized.StringCollection();
foreach (string lruConfig in lruConfigs.Items)
{
Settings.Default.PersistentLRUConfigs.Add(lruConfig);
}
Settings.Default.Save();
}
config_name_value_mapping.Clear();
registeredConfigs.Items.Clear();

#endregion

#region Config
var subkey = REGISTRY_KEY.Replace("HKEY_LOCAL_MACHINE\\", "");
var config_registry_key = Path.Combine(subkey, "Configs");
var key = Registry.LocalMachine.OpenSubKey(config_registry_key, false);

private void configBrowseButton_Click(object sender, EventArgs e)
{
var file_dialog = new OpenFileDialog();
file_dialog.CheckFileExists = true;
file_dialog.Filter = "SharkCage configuration|*.sconfig";
var result = file_dialog.ShowDialog();
if (result == DialogResult.OK)
if (key != null)
{
var chosen_file = file_dialog.FileName;
CheckConfigPath(chosen_file, (string path) =>
foreach (var value_name in key.GetValueNames())
{
configPath.Text = path;
addToLRUconfigs(path);

configPath.SelectionStart = configPath.Text.Length;
configPath.ScrollToCaret();
});
}
}

private void configPath_Leave(object sender, EventArgs e)
{
CheckConfigPath(configPath.Text, addToLRUconfigs);
}

private void CheckConfigPath(string config_path, Action<string> onSuccess)
{
if (config_path.Length == 0)
{
return;
}

if (config_path.EndsWith(".sconfig") && File.Exists(config_path))
{
onSuccess(config_path);
}
else
{
MessageBox.Show("Can only choose existing .sconfig files!", "Shark Cage", MessageBoxButtons.OK, MessageBoxIcon.Warning);
configPath.Focus();
}
}

#endregion

#region Last Recently Used List

private void lruConfigs_SelectedIndexChanged(object sender, EventArgs e)
{
if (lruConfigs.SelectedItem != null)
{
var selected_item = lruConfigs.SelectedItem.ToString();
configPath.Text = selected_item;
var value = (key.GetValue(value_name) as string) ?? String.Empty;
config_name_value_mapping.Add(value_name, value);
registeredConfigs.Items.Insert(registeredConfigs.Items.Count, value_name);
}

configPath.SelectionStart = configPath.Text.Length;
configPath.ScrollToCaret();
if (registeredConfigs.Items.Count > 0)
{
registeredConfigs.SelectedIndex = 0;
}
}
}

private void addToLRUconfigs(string config_path)
{
if (lruConfigs.Items.Contains(config_path))
{
lruConfigs.Items.Remove(config_path);
}
while (lruConfigs.Items.Count > 9)
{
lruConfigs.Items.RemoveAt(lruConfigs.Items.Count - 1);
}
lruConfigs.Items.Insert(0, config_path);
openButton.Enabled = registeredConfigs.SelectedItem != null;
}

private void lruConfigs_KeyUp(object sender, KeyEventArgs e)
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (e.KeyCode == Keys.Delete && lruConfigs.SelectedItem != null)
bool bHandled = false;
switch (keyData)
{
lruConfigs.Items.Remove(lruConfigs.SelectedItem);
case Keys.Enter:
if (registeredConfigs.SelectedItem != null)
{
SendToCage();
}
break;
case Keys.F5:
LoadConfigList();

bHandled = true;
break;
default:
return base.ProcessCmdKey(ref msg, keyData);
}
return bHandled;
}

#endregion

#region Cage Service

private void openButton_Click(object sender, EventArgs e)
private void SendToCage()
{
try
{
if (configPath.Text != String.Empty)
var selected_item = registeredConfigs.SelectedItem?.ToString() ?? String.Empty;
var config_path = String.Empty;
config_name_value_mapping.TryGetValue(selected_item, out config_path);

if (config_path != String.Empty)
{
int capacity = 1000;
StringBuilder sb = new StringBuilder(capacity);

bool result = NativeMethods.SendConfigAndExternalProgram(configPath.Text, sb, capacity);
bool result = NativeMethods.SendConfigAndExternalProgram(config_path, sb, capacity);

if (!result)
{
Expand All @@ -170,27 +119,43 @@ private void openButton_Click(object sender, EventArgs e)
}
}

private void openButton_Click(object sender, EventArgs e)
{
SendToCage();
}

#endregion

#region CageConfigurator

private void openCageConfiguratorButton_Click(object sender, EventArgs e)
{
const string registry_key = @"HKEY_LOCAL_MACHINE\SOFTWARE\SharkCage";
var install_dir = Registry.GetValue(registry_key, "InstallDir", "") as string;
var install_dir = Registry.GetValue(REGISTRY_KEY, "InstallDir", "") as string ?? String.Empty;

if (install_dir.Length == 0)
if (install_dir == String.Empty)
{
MessageBox.Show("Could not read installation directory from registry, opening CageConfigurator not possible", "Shark Cage", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

var p = new System.Diagnostics.Process();
p.StartInfo.FileName = $@"{install_dir}\CageConfigurator.exe";
p.StartInfo.Arguments = $@"""{configPath.Text}""";

var selected_item = registeredConfigs.SelectedItem?.ToString() ?? String.Empty;
var config_path = String.Empty;
config_name_value_mapping.TryGetValue(selected_item, out config_path);
if (config_path != String.Empty)
{
p.StartInfo.Arguments = $@"""{config_path}""";
}
p.Start();
}

#endregion

private void registeredConfigs_SelectedIndexChanged(object sender, EventArgs e)
{
openButton.Enabled = registeredConfigs.SelectedItem != null;
}
}
}
Loading

0 comments on commit bc56a0d

Please sign in to comment.