Skip to content

Commit

Permalink
Added ability to deny SYSTEM ability to set/delete registry keys.
Browse files Browse the repository at this point in the history
This is mostly for the InterleavePattern setting, which gets regularly deleted by NVIDIA processes.
  • Loading branch information
Eincrou committed Oct 12, 2015
1 parent a3f033b commit fabe080
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 165 deletions.
100 changes: 69 additions & 31 deletions Model/Stereo3DKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Principal;
using Microsoft.Win32;

namespace Advanced3DVConfig.Model
Expand All @@ -23,8 +25,13 @@ class Stereo3DKeys
"ToggleAutoConvergence", "ToggleAutoConvergenceRestore", "ToggleLaserSight", "ToggleMemo", "WriteConfig",
};
private readonly List<Stereo3DRegistryKey> _stereo3DSettings = new List<Stereo3DRegistryKey>();
public List<Stereo3DRegistryKey> Stereo3DSettings { get { return _stereo3DSettings; }
}
public List<Stereo3DRegistryKey> Stereo3DSettings { get { return _stereo3DSettings; }}
public bool SystemSetWriteDenied { get; private set; }

private readonly RegistryAccessRule _denyWrite = new RegistryAccessRule(
new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null),
RegistryRights.Delete | RegistryRights.SetValue, InheritanceFlags.None,
PropagationFlags.None, AccessControlType.Deny);
/// <summary>
/// Instantiates a new object to read and write NVIDIA 3D Vision registry keys
/// </summary>
Expand All @@ -36,9 +43,66 @@ public Stereo3DKeys() {
throw new Exception(exception.Message);
}
ReadStereo3DKeys();
var accessRules = _stereo3DKey.GetAccessControl().
GetAccessRules(true, true, typeof(SecurityIdentifier));
CheckSystemAccessRules(accessRules);
}


/// <summary>
/// Saves the current changes to the Windows registry.
/// </summary>
/// <returns></returns>
public void SaveSettingsToRegistry(List<Stereo3DRegistryKey> newSettings)
{
if (!newSettings.Any()) return;
foreach (var setting in newSettings)
{
_stereo3DKey.SetValue(setting.KeyName, (int)setting.KeyValue, RegistryValueKind.DWord);
_stereo3DSettings.Find(k => k.KeyName == setting.KeyName).KeyValue = setting.KeyValue;
}
}

private void GetStereo3DKey(){
public void ToggleSystemKeyPermissions()
{
var regSec = _stereo3DKey.GetAccessControl();
var accessRules = regSec.GetAccessRules(true, true, typeof(SecurityIdentifier));
CheckSystemAccessRules(accessRules);
if (SystemSetWriteDenied)
regSec.RemoveAccessRule(_denyWrite);
else
regSec.AddAccessRule(_denyWrite);
_stereo3DKey.SetAccessControl(regSec);
}

private void CheckSystemAccessRules(AuthorizationRuleCollection accessRules)
{
for (int i = 0; i < accessRules.Count & !SystemSetWriteDenied; i++)
{
var currentRule = accessRules[i] as RegistryAccessRule;
if ((currentRule.IdentityReference == _denyWrite.IdentityReference) &&
(currentRule.RegistryRights == _denyWrite.RegistryRights))
{
SystemSetWriteDenied = true;
}
}
}

/// <summary>
/// Saves all keys to a text file.
/// </summary>
/// <param name="filename">Location to save the text file</param>
public void SaveSettingsToFile(string filename)
{
var keysToWrite = new string[_stereo3DSettings.Count];
for (int i = 0; i < keysToWrite.Length; i++)
keysToWrite[i] = String.Format("{0} - {1}", _stereo3DSettings[i].KeyName,
_stereo3DSettings[i].KeyValue);
File.WriteAllLines(filename, keysToWrite);
}

private void GetStereo3DKey()
{
RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software");
if (softwareKey == null)
throw new Exception("Registry could not be accessed.");
Expand All @@ -54,7 +118,8 @@ private void GetStereo3DKey(){
_stereo3DKey = stereo3DKey;
}

private void ReadStereo3DKeys() {
private void ReadStereo3DKeys()
{
foreach (var reqKey in _requestedKeys)
_stereo3DSettings.Add(new Stereo3DRegistryKey(reqKey, ReadKeyValue(reqKey)));
}
Expand All @@ -70,33 +135,6 @@ private uint ReadKeyValue(string keyToRead)
int keyValueInt = Convert.ToInt32(keyValue);
return unchecked((uint)keyValueInt);
}
/// <summary>
/// Saves the current changes to the Windows registry.
/// </summary>
/// <returns></returns>
public void SaveSettingsToRegistry(List<Stereo3DRegistryKey> newSettings)
{
if (!newSettings.Any()) return;
foreach (var setting in newSettings)
{
_stereo3DKey.SetValue(setting.KeyName, (int)setting.KeyValue, RegistryValueKind.DWord);
_stereo3DSettings.Find(k => k.KeyName == setting.KeyName).KeyValue = setting.KeyValue;
}
}

/// <summary>
/// Saves all keys to a text file.
/// </summary>
/// <param name="filename">Location to save the text file</param>
public void SaveSettingsToFile(string filename)
{
var keysToWrite = new string[_stereo3DSettings.Count];
for (int i = 0; i < keysToWrite.Length; i++)
keysToWrite[i] = String.Format("{0} - {1}", _stereo3DSettings[i].KeyName,
_stereo3DSettings[i].KeyValue);
File.WriteAllLines(filename, keysToWrite);
}

private void DebugWriteAllStereo3DKeys ()
{
string keysfilename = String.Format(@"C:\Stereo3DKeys {0:h-mm-ss}.txt", DateTime.Now);
Expand Down
Loading

0 comments on commit fabe080

Please sign in to comment.