diff options
Diffstat (limited to 'src/mscorlib/src/Microsoft/Win32/RegistryKey.cs')
-rw-r--r-- | src/mscorlib/src/Microsoft/Win32/RegistryKey.cs | 758 |
1 files changed, 4 insertions, 754 deletions
diff --git a/src/mscorlib/src/Microsoft/Win32/RegistryKey.cs b/src/mscorlib/src/Microsoft/Win32/RegistryKey.cs index ff678f132c..f82b276059 100644 --- a/src/mscorlib/src/Microsoft/Win32/RegistryKey.cs +++ b/src/mscorlib/src/Microsoft/Win32/RegistryKey.cs @@ -55,7 +55,6 @@ namespace Microsoft.Win32 using System.Collections; using System.Collections.Generic; using System.Security; - using System.Security.Permissions; using System.Text; using System.Threading; using System.IO; @@ -68,21 +67,6 @@ namespace Microsoft.Win32 using System.Diagnostics.CodeAnalysis; /** - * Registry hive values. Useful only for GetRemoteBaseKey - */ - [Serializable] - [System.Runtime.InteropServices.ComVisible(true)] - public enum RegistryHive - { - ClassesRoot = unchecked((int)0x80000000), - CurrentUser = unchecked((int)0x80000001), - LocalMachine = unchecked((int)0x80000002), - Users = unchecked((int)0x80000003), - PerformanceData = unchecked((int)0x80000004), - CurrentConfig = unchecked((int)0x80000005), - } - - /** * Registry encapsulation. To get an instance of a RegistryKey use the * Registry class's static members then call OpenSubKey. * @@ -90,8 +74,7 @@ namespace Microsoft.Win32 * @security(checkDllCalls=off) * @security(checkClassLinking=on) */ - [ComVisible(true)] - public sealed class RegistryKey : MarshalByRefObject, IDisposable + internal sealed class RegistryKey : MarshalByRefObject, IDisposable { // We could use const here, if C# supported ELEMENT_TYPE_I fully. @@ -168,17 +151,6 @@ namespace Microsoft.Win32 * Creates a RegistryKey. * * This key is bound to hkey, if writable is <b>false</b> then no write operations - * will be allowed. - */ - private RegistryKey(SafeRegistryHandle hkey, bool writable, RegistryView view) - : this(hkey, writable, false, false, false, view) { - } - - - /** - * Creates a RegistryKey. - * - * This key is bound to hkey, if writable is <b>false</b> then no write operations * will be allowed. If systemkey is set then the hkey won't be released * when the object is GC'ed. * The remoteKey flag when set to true indicates that we are dealing with registry entries @@ -239,262 +211,11 @@ namespace Microsoft.Win32 } } - public void Flush() { - if (hkey != null) { - if (IsDirty()) { - Win32Native.RegFlushKey(hkey); - } - } - } - void IDisposable.Dispose() { Dispose(true); } - /** - * Creates a new subkey, or opens an existing one. - * - * @param subkey Name or path to subkey to create or open. - * - * @return the subkey, or <b>null</b> if the operation failed. - */ - [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "Reviewed for thread safety")] - public RegistryKey CreateSubKey(String subkey) { - return CreateSubKey(subkey, checkMode); - } - - [ComVisible(false)] - public RegistryKey CreateSubKey(String subkey, RegistryKeyPermissionCheck permissionCheck) - { - return CreateSubKeyInternal(subkey, permissionCheck, null, RegistryOptions.None); - } - - [ComVisible(false)] - public RegistryKey CreateSubKey(String subkey, RegistryKeyPermissionCheck permissionCheck, RegistryOptions options) - { - return CreateSubKeyInternal(subkey, permissionCheck, null, options); - } - - [ComVisible(false)] - public RegistryKey CreateSubKey(String subkey, bool writable) - { - return CreateSubKeyInternal(subkey, writable ? RegistryKeyPermissionCheck.ReadWriteSubTree : RegistryKeyPermissionCheck.ReadSubTree, null, RegistryOptions.None); - } - - [ComVisible(false)] - public RegistryKey CreateSubKey(String subkey, bool writable, RegistryOptions options) - { - return CreateSubKeyInternal(subkey, writable ? RegistryKeyPermissionCheck.ReadWriteSubTree : RegistryKeyPermissionCheck.ReadSubTree, null, options); - } - - [ComVisible(false)] - private unsafe RegistryKey CreateSubKeyInternal(String subkey, RegistryKeyPermissionCheck permissionCheck, object registrySecurityObj, RegistryOptions registryOptions) - { - ValidateKeyOptions(registryOptions); - ValidateKeyName(subkey); - ValidateKeyMode(permissionCheck); - EnsureWriteable(); - subkey = FixupName(subkey); // Fixup multiple slashes to a single slash - - // only keys opened under read mode is not writable - if (!remoteKey) { - RegistryKey key = InternalOpenSubKey(subkey, (permissionCheck != RegistryKeyPermissionCheck.ReadSubTree)); - if (key != null) { // Key already exits - CheckPermission(RegistryInternalCheck.CheckSubKeyWritePermission, subkey, false, RegistryKeyPermissionCheck.Default); - CheckPermission(RegistryInternalCheck.CheckSubTreePermission, subkey, false, permissionCheck); - key.checkMode = permissionCheck; - return key; - } - } - - CheckPermission(RegistryInternalCheck.CheckSubKeyCreatePermission, subkey, false, RegistryKeyPermissionCheck.Default); - - Win32Native.SECURITY_ATTRIBUTES secAttrs = null; - - int disposition = 0; - - // By default, the new key will be writable. - SafeRegistryHandle result = null; - int ret = Win32Native.RegCreateKeyEx(hkey, - subkey, - 0, - null, - (int)registryOptions /* specifies if the key is volatile */, - GetRegistryKeyAccess(permissionCheck != RegistryKeyPermissionCheck.ReadSubTree) | (int)regView, - secAttrs, - out result, - out disposition); - - if (ret == 0 && !result.IsInvalid) { - RegistryKey key = new RegistryKey(result, (permissionCheck != RegistryKeyPermissionCheck.ReadSubTree), false, remoteKey, false, regView); - CheckPermission(RegistryInternalCheck.CheckSubTreePermission, subkey, false, permissionCheck); - key.checkMode = permissionCheck; - - if (subkey.Length == 0) - key.keyName = keyName; - else - key.keyName = keyName + "\\" + subkey; - return key; - } - else if (ret != 0) // syscall failed, ret is an error code. - Win32Error(ret, keyName + "\\" + subkey); // Access denied? - - BCLDebug.Assert(false, "Unexpected code path in RegistryKey::CreateSubKey"); - return null; - } - - /** - * Deletes the specified subkey. Will throw an exception if the subkey has - * subkeys. To delete a tree of subkeys use, DeleteSubKeyTree. - * - * @param subkey SubKey to delete. - * - * @exception InvalidOperationException thrown if the subkey has child subkeys. - */ - public void DeleteSubKey(String subkey) { - DeleteSubKey(subkey, true); - } - - public void DeleteSubKey(String subkey, bool throwOnMissingSubKey) { - ValidateKeyName(subkey); - EnsureWriteable(); - subkey = FixupName(subkey); // Fixup multiple slashes to a single slash - CheckPermission(RegistryInternalCheck.CheckSubKeyWritePermission, subkey, false, RegistryKeyPermissionCheck.Default); - - // Open the key we are deleting and check for children. Be sure to - // explicitly call close to avoid keeping an extra HKEY open. - // - RegistryKey key = InternalOpenSubKey(subkey,false); - if (key != null) { - try { - if (key.InternalSubKeyCount() > 0) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_RegRemoveSubKey); - } - } - finally { - key.Close(); - } - - int ret; - - try { - ret = Win32Native.RegDeleteKeyEx(hkey, subkey, (int)regView, 0); - } - catch (EntryPointNotFoundException) { - ret = Win32Native.RegDeleteKey(hkey, subkey); - } - - if (ret!=0) { - if (ret == Win32Native.ERROR_FILE_NOT_FOUND) { - if (throwOnMissingSubKey) - ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegSubKeyAbsent); - } - else - Win32Error(ret, null); - } - } - else { // there is no key which also means there is no subkey - if (throwOnMissingSubKey) - ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegSubKeyAbsent); - } - } - - /** - * Recursively deletes a subkey and any child subkeys. - * - * @param subkey SubKey to delete. - */ - public void DeleteSubKeyTree(String subkey) { - DeleteSubKeyTree(subkey, true /*throwOnMissingSubKey*/); - } - - [ComVisible(false)] - public void DeleteSubKeyTree(String subkey, Boolean throwOnMissingSubKey) { - ValidateKeyName(subkey); - - // Security concern: Deleting a hive's "" subkey would delete all - // of that hive's contents. Don't allow "". - if (subkey.Length==0 && IsSystemKey()) { - ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegKeyDelHive); - } - - EnsureWriteable(); - - subkey = FixupName(subkey); // Fixup multiple slashes to a single slash - CheckPermission(RegistryInternalCheck.CheckSubTreeWritePermission, subkey, false, RegistryKeyPermissionCheck.Default); - - RegistryKey key = InternalOpenSubKey(subkey, true); - if (key != null) { - try { - if (key.InternalSubKeyCount() > 0) { - String[] keys = key.InternalGetSubKeyNames(); - - for (int i=0; i<keys.Length; i++) { - key.DeleteSubKeyTreeInternal(keys[i]); - } - } - } - finally { - key.Close(); - } - - int ret; - try { - ret = Win32Native.RegDeleteKeyEx(hkey, subkey, (int)regView, 0); - } - catch (EntryPointNotFoundException) { - ret = Win32Native.RegDeleteKey(hkey, subkey); - } - - if (ret!=0) Win32Error(ret, null); - } - else if(throwOnMissingSubKey) { - ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegSubKeyAbsent); - } - } - - // An internal version which does no security checks or argument checking. Skipping the - // security checks should give us a slight perf gain on large trees. - private void DeleteSubKeyTreeInternal(string subkey) { - RegistryKey key = InternalOpenSubKey(subkey, true); - if (key != null) { - try { - if (key.InternalSubKeyCount() > 0) { - String[] keys = key.InternalGetSubKeyNames(); - - for (int i=0; i<keys.Length; i++) { - key.DeleteSubKeyTreeInternal(keys[i]); - } - } - } - finally { - key.Close(); - } - - int ret; - try { - ret = Win32Native.RegDeleteKeyEx(hkey, subkey, (int)regView, 0); - } - catch (EntryPointNotFoundException) { - ret = Win32Native.RegDeleteKey(hkey, subkey); - } - if (ret!=0) Win32Error(ret, null); - } - else { - ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegSubKeyAbsent); - } - } - - /** - * Deletes the specified value from this key. - * - * @param name Name of value to delete. - */ - public void DeleteValue(String name) { - DeleteValue(name, true); - } - public void DeleteValue(String name, bool throwOnMissingValue) { EnsureWriteable(); CheckPermission(RegistryInternalCheck.CheckValueWritePermission, name, false, RegistryKeyPermissionCheck.Default); @@ -552,68 +273,6 @@ namespace Microsoft.Win32 return key; } - - [ComVisible(false)] - public static RegistryKey OpenBaseKey(RegistryHive hKey, RegistryView view) { - ValidateKeyView(view); - CheckUnmanagedCodePermission(); - return GetBaseKey((IntPtr)((int)hKey), view); - } - - /** - * Retrieves a new RegistryKey that represents the requested key on a foreign - * machine. Valid values for hKey are members of the RegistryHive enum, or - * Win32 integers such as: - * - * HKEY_CLASSES_ROOT, - * HKEY_CURRENT_USER, - * HKEY_LOCAL_MACHINE, - * HKEY_USERS, - * HKEY_PERFORMANCE_DATA, - * HKEY_CURRENT_CONFIG, - * HKEY_DYN_DATA. - * - * @param hKey HKEY_* to open. - * @param machineName the machine to connect to - * - * @return the RegistryKey requested. - */ - public static RegistryKey OpenRemoteBaseKey(RegistryHive hKey, String machineName) { - return OpenRemoteBaseKey(hKey, machineName, RegistryView.Default); - } - - [ComVisible(false)] - public static RegistryKey OpenRemoteBaseKey(RegistryHive hKey, String machineName, RegistryView view) { - if (machineName==null) - throw new ArgumentNullException(nameof(machineName)); - int index = (int)hKey & 0x0FFFFFFF; - if (index < 0 || index >= hkeyNames.Length || ((int)hKey & 0xFFFFFFF0) != 0x80000000) { - throw new ArgumentException(Environment.GetResourceString("Arg_RegKeyOutOfRange")); - } - ValidateKeyView(view); - - CheckUnmanagedCodePermission(); - // connect to the specified remote registry - SafeRegistryHandle foreignHKey = null; - int ret = Win32Native.RegConnectRegistry(machineName, new SafeRegistryHandle(new IntPtr((int)hKey), false), out foreignHKey); - - if (ret == Win32Native.ERROR_DLL_INIT_FAILED) - // return value indicates an error occurred - throw new ArgumentException(Environment.GetResourceString("Arg_DllInitFailure")); - - if (ret != 0) - Win32ErrorStatic(ret, null); - - if (foreignHKey.IsInvalid) - // return value indicates an error occurred - throw new ArgumentException(Environment.GetResourceString("Arg_RegKeyNoRemoteConnect", machineName)); - - RegistryKey key = new RegistryKey(foreignHKey, true, false, true, ((IntPtr) hKey) == HKEY_PERFORMANCE_DATA, view); - key.checkMode = RegistryKeyPermissionCheck.Default; - key.keyName = hkeyNames[index]; - return key; - } - /** * Retrieves a subkey. If readonly is <b>true</b>, then the subkey is opened with * read-only access. @@ -685,26 +344,6 @@ namespace Microsoft.Win32 return OpenSubKey(name, false); } - /** - * Retrieves the count of subkeys. - * - * @return a count of subkeys. - */ - public int SubKeyCount { - get { - CheckPermission(RegistryInternalCheck.CheckKeyReadPermission, null, false, RegistryKeyPermissionCheck.Default); - return InternalSubKeyCount(); - } - } - - [ComVisible(false)] - public RegistryView View { - get { - EnsureNotDisposed(); - return regView; - } - } - internal int InternalSubKeyCount() { EnsureNotDisposed(); @@ -770,18 +409,6 @@ namespace Microsoft.Win32 return names; } - /** - * Retrieves the count of values. - * - * @return a count of values. - */ - public int ValueCount { - get { - CheckPermission(RegistryInternalCheck.CheckKeyReadPermission, null, false, RegistryKeyPermissionCheck.Default); - return InternalValueCount(); - } - } - internal int InternalValueCount() { EnsureNotDisposed(); int values = 0; @@ -883,7 +510,6 @@ namespace Microsoft.Win32 return InternalGetValue(name, defaultValue, false, true); } - [ComVisible(false)] public Object GetValue(String name, Object defaultValue, RegistryValueOptions options) { if( options < RegistryValueOptions.None || options > RegistryValueOptions.DoNotExpandEnvironmentNames) { throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)options), nameof(options)); @@ -1110,37 +736,6 @@ namespace Microsoft.Win32 return data; } - - [ComVisible(false)] - public RegistryValueKind GetValueKind(string name) { - CheckPermission(RegistryInternalCheck.CheckValueReadPermission, name, false, RegistryKeyPermissionCheck.Default); - EnsureNotDisposed(); - - int type = 0; - int datasize = 0; - int ret = Win32Native.RegQueryValueEx(hkey, name, null, ref type, (byte[])null, ref datasize); - if (ret != 0) - Win32Error(ret, null); - if (type == Win32Native.REG_NONE) - return RegistryValueKind.None; - else if (!Enum.IsDefined(typeof(RegistryValueKind), type)) - return RegistryValueKind.Unknown; - else - return (RegistryValueKind) type; - } - - /** - * Retrieves the current state of the dirty property. - * - * A key is marked as dirty if any operation has occurred that modifies the - * contents of the key. - * - * @return <b>true</b> if the key has been modified. - */ - private bool IsDirty() { - return (this.state & STATE_DIRTY) != 0; - } - private bool IsSystemKey() { return (this.state & STATE_SYSTEMKEY) != 0; } @@ -1153,13 +748,6 @@ namespace Microsoft.Win32 return (this.state & STATE_PERF_DATA) != 0; } - public String Name { - get { - EnsureNotDisposed(); - return keyName; - } - } - private void SetDirty() { this.state |= STATE_DIRTY; } @@ -1174,7 +762,6 @@ namespace Microsoft.Win32 SetValue(name, value, RegistryValueKind.Unknown); } - [ComVisible(false)] public unsafe void SetValue(String name, Object value, RegistryValueKind valueKind) { if (value==null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); @@ -1392,19 +979,6 @@ namespace Microsoft.Win32 } } - internal static void Win32ErrorStatic(int errorCode, String str) { - switch (errorCode) { - case Win32Native.ERROR_ACCESS_DENIED: - if (str != null) - throw new UnauthorizedAccessException(Environment.GetResourceString("UnauthorizedAccess_RegistryKeyGeneric_Key", str)); - else - throw new UnauthorizedAccessException(); - - default: - throw new IOException(Win32Native.GetMessage(errorCode), errorCode); - } - } - internal static String FixupName(String name) { BCLDebug.Assert(name!=null,"[FixupName]name!=null"); @@ -1469,301 +1043,9 @@ namespace Microsoft.Win32 } - // - // Read/Write/Create SubKey Permission - // - private void GetSubKeyReadPermission(string subkeyName, out RegistryPermissionAccess access, out string path) { - access = RegistryPermissionAccess.Read; - path = keyName + "\\" + subkeyName + "\\."; - } - private void GetSubKeyWritePermission(string subkeyName, out RegistryPermissionAccess access, out string path) { - // If we want to open a subkey of a read-only key as writeable, we need to do the check. - access = RegistryPermissionAccess.Write; - path = keyName + "\\" + subkeyName + "\\."; - } - private void GetSubKeyCreatePermission(string subkeyName, out RegistryPermissionAccess access, out string path) { - access = RegistryPermissionAccess.Create; - path = keyName + "\\" + subkeyName + "\\."; - } - - // - // Read/Write/ReadWrite SubTree Permission - // - private void GetSubTreeReadPermission(string subkeyName, out RegistryPermissionAccess access, out string path) { - access = RegistryPermissionAccess.Read; - path = keyName + "\\" + subkeyName + "\\"; - } - private void GetSubTreeWritePermission(string subkeyName, out RegistryPermissionAccess access, out string path) { - access = RegistryPermissionAccess.Write; - path = keyName + "\\" + subkeyName + "\\"; - } - private void GetSubTreeReadWritePermission(string subkeyName, out RegistryPermissionAccess access, out string path) { - access = RegistryPermissionAccess.Write | RegistryPermissionAccess.Read; - path = keyName + "\\" + subkeyName; - } - - // - // Read/Write/Create Value Permission - // - private void GetValueReadPermission(string valueName, out RegistryPermissionAccess access, out string path) { - access = RegistryPermissionAccess.Read; - path = keyName+"\\"+valueName; - } - private void GetValueWritePermission(string valueName, out RegistryPermissionAccess access, out string path) { - access = RegistryPermissionAccess.Write; - path = keyName+"\\"+valueName; - } - private void GetValueCreatePermission(string valueName, out RegistryPermissionAccess access, out string path) { - access = RegistryPermissionAccess.Create; - path = keyName+"\\"+valueName; - } - - // Read Key Permission - private void GetKeyReadPermission(out RegistryPermissionAccess access, out string path) { - access = RegistryPermissionAccess.Read; - path = keyName + "\\."; - } - private void CheckPermission(RegistryInternalCheck check, string item, bool subKeyWritable, RegistryKeyPermissionCheck subKeyCheck) { - bool demand = false; - RegistryPermissionAccess access = RegistryPermissionAccess.NoAccess; - string path = null; - - switch (check) { - // - // Read/Write/Create SubKey Permission - // - case RegistryInternalCheck.CheckSubKeyReadPermission: - if (remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - BCLDebug.Assert(checkMode == RegistryKeyPermissionCheck.Default, "Should be called from a key opened under default mode only!"); - BCLDebug.Assert(subKeyWritable == false, "subKeyWritable should be false (unused)"); - BCLDebug.Assert(subKeyCheck == RegistryKeyPermissionCheck.Default, "subKeyCheck should be Default (unused)"); - demand = true; - GetSubKeyReadPermission(item, out access, out path); - } - break; - case RegistryInternalCheck.CheckSubKeyWritePermission: - if (remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - BCLDebug.Assert(checkMode != RegistryKeyPermissionCheck.ReadSubTree, "We shouldn't allow creating sub key under read-only key!"); - BCLDebug.Assert(subKeyWritable == false, "subKeyWritable should be false (unused)"); - BCLDebug.Assert(subKeyCheck == RegistryKeyPermissionCheck.Default, "subKeyCheck should be Default (unused)"); - if( checkMode == RegistryKeyPermissionCheck.Default) { - demand = true; - GetSubKeyWritePermission(item, out access, out path); - } - } - break; - case RegistryInternalCheck.CheckSubKeyCreatePermission: - if (remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - BCLDebug.Assert(checkMode != RegistryKeyPermissionCheck.ReadSubTree, "We shouldn't allow creating sub key under read-only key!"); - BCLDebug.Assert(subKeyWritable == false, "subKeyWritable should be false (unused)"); - BCLDebug.Assert(subKeyCheck == RegistryKeyPermissionCheck.Default, "subKeyCheck should be Default (unused)"); - if( checkMode == RegistryKeyPermissionCheck.Default) { - demand = true; - GetSubKeyCreatePermission(item, out access, out path); - } - } - break; - // - // Read/Write/ReadWrite SubTree Permission - // - case RegistryInternalCheck.CheckSubTreeReadPermission: - if (remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - BCLDebug.Assert(subKeyWritable == false, "subKeyWritable should be false (unused)"); - BCLDebug.Assert(subKeyCheck == RegistryKeyPermissionCheck.Default, "subKeyCheck should be Default (unused)"); - if( checkMode == RegistryKeyPermissionCheck.Default) { - demand = true; - GetSubTreeReadPermission(item, out access, out path); - } - } - break; - case RegistryInternalCheck.CheckSubTreeWritePermission: - if (remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - BCLDebug.Assert(checkMode != RegistryKeyPermissionCheck.ReadSubTree, "We shouldn't allow writing value to read-only key!"); - BCLDebug.Assert(subKeyWritable == false, "subKeyWritable should be false (unused)"); - BCLDebug.Assert(subKeyCheck == RegistryKeyPermissionCheck.Default, "subKeyCheck should be Default (unused)"); - if( checkMode == RegistryKeyPermissionCheck.Default) { - demand = true; - GetSubTreeWritePermission(item, out access, out path); - } - } - break; - case RegistryInternalCheck.CheckSubTreeReadWritePermission: - if (remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - BCLDebug.Assert(subKeyWritable == false, "subKeyWritable should be false (unused)"); - BCLDebug.Assert(subKeyCheck == RegistryKeyPermissionCheck.Default, "subKeyCheck should be Default (unused)"); - // If we want to open a subkey of a read-only key as writeable, we need to do the check. - demand = true; - GetSubTreeReadWritePermission(item, out access, out path); - } - break; - // - // Read/Write/Create Value Permission - // - case RegistryInternalCheck.CheckValueReadPermission: - ///*** no remoteKey check ***/// - BCLDebug.Assert(subKeyWritable == false, "subKeyWritable should be false (unused)"); - BCLDebug.Assert(subKeyCheck == RegistryKeyPermissionCheck.Default, "subKeyCheck should be Default (unused)"); - if( checkMode == RegistryKeyPermissionCheck.Default) { - // only need to check for default mode (dynamice check) - demand = true; - GetValueReadPermission(item, out access, out path); - } - break; - case RegistryInternalCheck.CheckValueWritePermission: - if (remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - BCLDebug.Assert(checkMode != RegistryKeyPermissionCheck.ReadSubTree, "We shouldn't allow writing value to read-only key!"); - BCLDebug.Assert(subKeyWritable == false, "subKeyWritable should be false (unused)"); - BCLDebug.Assert(subKeyCheck == RegistryKeyPermissionCheck.Default, "subKeyCheck should be Default (unused)"); - // skip the security check if the key is opened under write mode - if( checkMode == RegistryKeyPermissionCheck.Default) { - demand = true; - GetValueWritePermission(item, out access, out path); - } - } - break; - case RegistryInternalCheck.CheckValueCreatePermission: - if (remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - BCLDebug.Assert(checkMode != RegistryKeyPermissionCheck.ReadSubTree, "We shouldn't allow creating value under read-only key!"); - BCLDebug.Assert(subKeyWritable == false, "subKeyWritable should be false (unused)"); - BCLDebug.Assert(subKeyCheck == RegistryKeyPermissionCheck.Default, "subKeyCheck should be Default (unused)"); - // skip the security check if the key is opened under write mode - if( checkMode == RegistryKeyPermissionCheck.Default) { - demand = true; - GetValueCreatePermission(item, out access, out path); - } - } - break; - // - // CheckKeyReadPermission - // - case RegistryInternalCheck.CheckKeyReadPermission: - ///*** no remoteKey check ***/// - if( checkMode == RegistryKeyPermissionCheck.Default) { - BCLDebug.Assert(item == null, "CheckKeyReadPermission should never have a non-null item parameter!"); - BCLDebug.Assert(subKeyWritable == false, "subKeyWritable should be false (unused)"); - BCLDebug.Assert(subKeyCheck == RegistryKeyPermissionCheck.Default, "subKeyCheck should be Default (unused)"); - - // only need to check for default mode (dynamice check) - demand = true; - GetKeyReadPermission(out access, out path); - } - break; - // - // CheckSubTreePermission - // - case RegistryInternalCheck.CheckSubTreePermission: - BCLDebug.Assert(subKeyWritable == false, "subKeyWritable should be false (unused)"); - if( subKeyCheck == RegistryKeyPermissionCheck.ReadSubTree) { - if( checkMode == RegistryKeyPermissionCheck.Default) { - if( remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - demand = true; - GetSubTreeReadPermission(item, out access, out path); - } - } - } - else if(subKeyCheck == RegistryKeyPermissionCheck.ReadWriteSubTree) { - if( checkMode != RegistryKeyPermissionCheck.ReadWriteSubTree) { - if( remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - demand = true; - GetSubTreeReadWritePermission(item, out access, out path); - } - } - } - break; - - // - // CheckOpenSubKeyWithWritablePermission uses the 'subKeyWritable' parameter - // - case RegistryInternalCheck.CheckOpenSubKeyWithWritablePermission: - BCLDebug.Assert(subKeyCheck == RegistryKeyPermissionCheck.Default, "subKeyCheck should be Default (unused)"); - // If the parent key is not opened under default mode, we have access already. - // If the parent key is opened under default mode, we need to check for permission. - if(checkMode == RegistryKeyPermissionCheck.Default) { - if( remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - demand = true; - GetSubKeyReadPermission(item, out access, out path); - } - break; - } - if( subKeyWritable && (checkMode == RegistryKeyPermissionCheck.ReadSubTree)) { - if( remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - demand = true; - GetSubTreeReadWritePermission(item, out access, out path); - } - break; - } - break; - - // - // CheckOpenSubKeyPermission uses the 'subKeyCheck' parameter - // - case RegistryInternalCheck.CheckOpenSubKeyPermission: - BCLDebug.Assert(subKeyWritable == false, "subKeyWritable should be false (unused)"); - if(subKeyCheck == RegistryKeyPermissionCheck.Default) { - if( checkMode == RegistryKeyPermissionCheck.Default) { - if(remoteKey) { - CheckUnmanagedCodePermission(); - } - else { - demand = true; - GetSubKeyReadPermission(item, out access, out path); - } - } - } - break; - - default: - BCLDebug.Assert(false, "CheckPermission default switch case should never be hit!"); - break; - } - - if (demand) { - new RegistryPermission(access, path).Demand(); - } - } - - static private void CheckUnmanagedCodePermission() { -#pragma warning disable 618 - new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand(); -#pragma warning restore 618 + // TODO: Cleanup } private bool ContainsRegistryValue(string name) { @@ -1798,26 +1080,6 @@ namespace Microsoft.Win32 return winAccess; } - static int GetRegistryKeyAccess(RegistryKeyPermissionCheck mode) { - int winAccess = 0; - switch(mode) { - case RegistryKeyPermissionCheck.ReadSubTree: - case RegistryKeyPermissionCheck.Default: - winAccess = Win32Native.KEY_READ; - break; - - case RegistryKeyPermissionCheck.ReadWriteSubTree: - winAccess = Win32Native.KEY_READ| Win32Native.KEY_WRITE; - break; - - default: - BCLDebug.Assert(false, "unexpected code path"); - break; - } - - return winAccess; - } - private RegistryKeyPermissionCheck GetSubKeyPermissonCheck(bool subkeyWritable) { if( checkMode == RegistryKeyPermissionCheck.Default) { return checkMode; @@ -1851,18 +1113,6 @@ namespace Microsoft.Win32 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegKeyStrLenBug); } - - static private void ValidateKeyMode(RegistryKeyPermissionCheck mode) { - if( mode < RegistryKeyPermissionCheck.Default || mode > RegistryKeyPermissionCheck.ReadWriteSubTree) { - ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidRegistryKeyPermissionCheck, ExceptionArgument.mode); - } - } - - static private void ValidateKeyOptions(RegistryOptions options) { - if (options < RegistryOptions.None || options > RegistryOptions.Volatile) { - ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidRegistryOptionsCheck, ExceptionArgument.options); - } - } static private void ValidateKeyView(RegistryView view) { if (view != RegistryView.Default && view != RegistryView.Registry32 && view != RegistryView.Registry64) { @@ -1877,14 +1127,14 @@ namespace Microsoft.Win32 } [Flags] - public enum RegistryValueOptions { + internal enum RegistryValueOptions { None = 0, DoNotExpandEnvironmentNames = 1 } // the name for this API is meant to mimic FileMode, which has similar values - public enum RegistryKeyPermissionCheck { + internal enum RegistryKeyPermissionCheck { Default = 0, ReadSubTree = 1, ReadWriteSubTree = 2 |