static string GetRegistryKeyBase(string machineName, RegistryHive registryHive)
        {
            string registryBase = Utilities.IsLocalMachineName(machineName) ? string.Empty : SR.GetString(SR.RemoteRegistryFormat, machineName);

            switch (registryHive)
            {
            case RegistryHive.ClassesRoot:
                registryBase += Registry.ClassesRoot.Name;
                break;

            case RegistryHive.CurrentUser:
                registryBase += Registry.CurrentUser.Name;
                break;

            case RegistryHive.LocalMachine:
                registryBase += Registry.LocalMachine.Name;
                break;

            default:
                // We do not support other values here
                System.Diagnostics.Debug.Assert(false, "registryHive is not supported");
                break;
            }
            RegistryExceptionHelper.EnsureEndsWithSlash(ref registryBase);
            return(registryBase);
        }
 StdRegProviderWrapper(uint hiveValue, string subKey, ManagementClass regClassInstance)
 {
     this.hiveValue = hiveValue;
     this.subKey    = subKey;
     this.registryExceptionHelper = new RegistryExceptionHelper(subKey);
     this.regClassInstance        = new ManagementClass(regClassInstance.Path, regClassInstance.Options);
 }
        ClusterRegistryConfigurationProvider(SafeHKey key, string registryKey)
        {
            this.hKey = key;

            this.registryExceptionHelper = new RegistryExceptionHelper(registryKey);
            this.registryKey             = registryKey;
            RegistryExceptionHelper.EnsureEndsWithSlash(ref registryKey);
        }
        ClusterRegistryConfigurationProvider(SafeHKey key, string registryKey)
        {
            this.hKey = key;

            this.registryExceptionHelper = new RegistryExceptionHelper(registryKey);
            this.registryKey = registryKey;
            RegistryExceptionHelper.EnsureEndsWithSlash(ref registryKey);
        }
        internal StdRegProviderWrapper OpenKey(string subKey)
        {
            string s = this.subKey;

            RegistryExceptionHelper.EnsureEndsWithSlash(ref s);

            s += subKey;

            return(new StdRegProviderWrapper(this.hiveValue, s, regClassInstance));
        }
        public StdRegProviderWrapper(RegistryHive registryHive, string subKey, string machineName)
        {
            switch (registryHive)
            {
            case RegistryHive.ClassesRoot:
                this.hiveValue = 0x80000000;
                break;

            case RegistryHive.CurrentUser:
                this.hiveValue = 0x80000001;
                break;

            case RegistryHive.LocalMachine:
                this.hiveValue = 0x80000002;
                break;

            default:
                // We do not support other values here
                throw new ArgumentException("remoteHive");
            }

            registryExceptionHelper = new RegistryExceptionHelper(machineName, registryHive, subKey);

            try
            {
                ConnectionOptions co = null;

                if (Utilities.IsLocalMachineName(machineName))
                {
                    machineName = "localhost";
                }
                else
                {
                    co = new ConnectionOptions();
                    co.Authentication = AuthenticationLevel.PacketPrivacy;
                    co.Impersonation  = ImpersonationLevel.Impersonate;
                }

                ManagementScope  managementScope = new ManagementScope("\\\\" + machineName + "\\root\\DEFAULT", co);
                ManagementPath   managementPath  = new ManagementPath("StdRegProv");
                ObjectGetOptions options         = new ObjectGetOptions(new ManagementNamedValueCollection(), TimeSpan.FromSeconds(15), false);
                this.regClassInstance = new ManagementClass(managementScope, managementPath, options);
                this.subKey           = subKey;
            }
            catch (ManagementException e)
            {
                throw registryExceptionHelper.CreateRegistryAccessException(e);
            }
            catch (COMException e) // for RPC_S_SERVER_UNAVAILABLE sort of errors
            {
                throw registryExceptionHelper.CreateRegistryAccessException(e);
            }
        }
Beispiel #7
0
        public StdRegProviderWrapper(RegistryHive registryHive, string subKey, string machineName)
        {
            switch (registryHive)
            {
                case RegistryHive.ClassesRoot:
                    this.hiveValue = 0x80000000;
                    break;
                case RegistryHive.CurrentUser:
                    this.hiveValue = 0x80000001;
                    break;
                case RegistryHive.LocalMachine:
                    this.hiveValue = 0x80000002;
                    break;
                default:
                    // We do not support other values here
                    throw new ArgumentException("remoteHive");
            }

            registryExceptionHelper = new RegistryExceptionHelper(machineName, registryHive, subKey);

            try
            {
                ConnectionOptions co = null;

                if (Utilities.IsLocalMachineName(machineName))
                {
                    machineName = "localhost";
                }
                else
                {
                    co = new ConnectionOptions();
                    co.Authentication = AuthenticationLevel.PacketPrivacy;
                    co.Impersonation = ImpersonationLevel.Impersonate;
                }

                ManagementScope managementScope = new ManagementScope("\\\\" + machineName + "\\root\\DEFAULT", co);
                ManagementPath managementPath = new ManagementPath("StdRegProv");
                ObjectGetOptions options = new ObjectGetOptions(new ManagementNamedValueCollection(), TimeSpan.FromSeconds(15), false);
                this.regClassInstance = new ManagementClass(managementScope, managementPath, options);
                this.subKey = subKey;
            }
            catch (ManagementException e)
            {
                throw registryExceptionHelper.CreateRegistryAccessException(e);
            }
            catch (COMException e) // for RPC_S_SERVER_UNAVAILABLE sort of errors
            {
                throw registryExceptionHelper.CreateRegistryAccessException(e);
            }
        }
Beispiel #8
0
        internal static void DeleteKey(string key, string subKeyToDelete)
        {
            RegistryExceptionHelper registryExceptionHelper = new RegistryExceptionHelper(string.Empty, RegistryHive.LocalMachine, key);

            try
            {
                RegistryKey regKey = Registry.LocalMachine.OpenSubKey(
                    key,
                    RegistryKeyPermissionCheck.ReadWriteSubTree,
                    RegistryRights.FullControl);

                if (regKey != null)
                {
                    using (regKey)
                    {
                        string[] subKeys = regKey.GetSubKeyNames();

                        if (subKeys != null && Array.FindIndex <string>(subKeys, new StringFinder(subKeyToDelete).IsTarget) != -1)
                        {
                            regKey.DeleteSubKeyTree(subKeyToDelete);
                        }
                    }
                }
            }
            catch (SecurityException e)
            {
                throw registryExceptionHelper.CreateRegistryWriteException(subKeyToDelete, e);
            }
            catch (ObjectDisposedException e)
            {
                throw registryExceptionHelper.CreateRegistryWriteException(subKeyToDelete, e);
            }
            catch (ArgumentNullException e)
            {
                throw registryExceptionHelper.CreateRegistryWriteException(subKeyToDelete, e);
            }
            catch (ArgumentException e)
            {
                throw registryExceptionHelper.CreateRegistryWriteException(subKeyToDelete, e);
            }
            catch (UnauthorizedAccessException e)
            {
                throw registryExceptionHelper.CreateRegistryWriteException(subKeyToDelete, e);
            }
        }
Beispiel #9
0
        internal static void DeleteKey(string key, string subKeyToDelete)
        {
            RegistryExceptionHelper registryExceptionHelper = new RegistryExceptionHelper(string.Empty, RegistryHive.LocalMachine, key);

            try
            {
                RegistryKey regKey = Registry.LocalMachine.OpenSubKey(
                                            key,
                                            RegistryKeyPermissionCheck.ReadWriteSubTree,
                                            RegistryRights.FullControl);

                if (regKey != null)
                {
                    using (regKey)
                    {
                        string[] subKeys = regKey.GetSubKeyNames();

                        if (subKeys != null && Array.FindIndex<string>(subKeys, new StringFinder(subKeyToDelete).IsTarget) != -1)
                        {
                            regKey.DeleteSubKeyTree(subKeyToDelete);
                        }
                    }
                }
            }
            catch (SecurityException e)
            {
                throw registryExceptionHelper.CreateRegistryWriteException(subKeyToDelete, e); 
            }
            catch (ObjectDisposedException e)
            {
                throw registryExceptionHelper.CreateRegistryWriteException(subKeyToDelete, e); 
            }
            catch (ArgumentNullException e)
            {
                throw registryExceptionHelper.CreateRegistryWriteException(subKeyToDelete, e); 
            }
            catch (ArgumentException e)
            {
                throw registryExceptionHelper.CreateRegistryWriteException(subKeyToDelete, e); 
            }
            catch (UnauthorizedAccessException e)
            {
                throw registryExceptionHelper.CreateRegistryWriteException(subKeyToDelete, e); 
            }
        }
        internal ClusterRegistryConfigurationProvider(SafeHResource hResource, string key)
        {
            SafeHKey rootKey = SafeNativeMethods.GetClusterResourceKey(hResource,
                                                                       RegistryRights.ReadKey |
                                                                       RegistryRights.EnumerateSubKeys |
                                                                       RegistryRights.QueryValues);

            if (rootKey.IsInvalid)
            {
                int lastError = Marshal.GetLastWin32Error();
                throw new WsatAdminException(WsatAdminErrorCode.REGISTRY_ACCESS, SR.GetString(SR.CannotOpenClusterRegistry, lastError));
            }

            if (string.IsNullOrEmpty(key))
            {
                hKey = rootKey;
            }
            else
            {
                using (rootKey)
                {
                    int disposition;
                    int ret = SafeNativeMethods.ClusterRegCreateKey(rootKey,
                                                                    key,
                                                                    SafeNativeMethods.REG_OPTION_NON_VOLATILE,
                                                                    RegistryRights.FullControl,
                                                                    IntPtr.Zero,
                                                                    out this.hKey,
                                                                    out disposition);
                    if (ret != SafeNativeMethods.ERROR_SUCCESS)
                    {
                        throw new WsatAdminException(WsatAdminErrorCode.REGISTRY_ACCESS, SR.GetString(SR.CannotOpenClusterRegistry, ret));
                    }
                }
            }

            registryExceptionHelper = new RegistryExceptionHelper(key);
            this.registryKey        = key;
            RegistryExceptionHelper.EnsureEndsWithSlash(ref this.registryKey);
        }
        internal ClusterRegistryConfigurationProvider(SafeHResource hResource, string key)
        {            
            SafeHKey rootKey = SafeNativeMethods.GetClusterResourceKey(hResource,
                                                                RegistryRights.ReadKey |
                                                                RegistryRights.EnumerateSubKeys |
                                                                RegistryRights.QueryValues);
            if (rootKey.IsInvalid)
            {
                int lastError = Marshal.GetLastWin32Error();
                throw new WsatAdminException(WsatAdminErrorCode.REGISTRY_ACCESS, SR.GetString(SR.CannotOpenClusterRegistry, lastError));
            }

            if (string.IsNullOrEmpty(key))
            {
                hKey = rootKey;
            }
            else
            {
                using (rootKey)
                {
                    int disposition;
                    int ret = SafeNativeMethods.ClusterRegCreateKey(rootKey,
                                                                    key,
                                                                    SafeNativeMethods.REG_OPTION_NON_VOLATILE,
                                                                    RegistryRights.FullControl,
                                                                    IntPtr.Zero,
                                                                    out this.hKey,
                                                                    out disposition);
                    if (ret != SafeNativeMethods.ERROR_SUCCESS)
                    {
                        throw new WsatAdminException(WsatAdminErrorCode.REGISTRY_ACCESS, SR.GetString(SR.CannotOpenClusterRegistry, ret));
                    }
                }
            }

            registryExceptionHelper = new RegistryExceptionHelper(key);
            this.registryKey = key;
            RegistryExceptionHelper.EnsureEndsWithSlash(ref this.registryKey);
        }
        void DoWriteData(string name, string valueKey, object value, string writeMethod)
        {
            EnsureSubKeyExists();
            EnsureWriteAccess();

            try
            {
                ManagementBaseObject inParams = regClassInstance.GetMethodParameters(writeMethod);

                inParams[InputParameters.DefKey]     = this.hiveValue;
                inParams[InputParameters.SubKeyName] = subKey;
                inParams[InputParameters.ValueName]  = name;
                inParams[valueKey] = value;

                ManagementBaseObject outParams = regClassInstance.InvokeMethod(writeMethod,
                                                                               inParams, null);
                uint ret = (uint)outParams[OutputParameters.ReturnValue];
                if (ret != 0) // zero means success
                {
                    string registryKey = this.subKey;
                    RegistryExceptionHelper.EnsureEndsWithSlash(ref registryKey);
                    registryKey += name;

                    registryExceptionHelper.CreateRegistryWriteException(registryKey, null);
                }
            }
#pragma warning suppress 56500
            catch (Exception e)
            {
                // MSDN does not have a spec of possible exceptions for the APIs used above.
                // To be safe, we should be a bit more generic in catching exceptions
                if (Utilities.IsCriticalException(e))
                {
                    throw;
                }
                throw registryExceptionHelper.CreateRegistryAccessException(name, e);
            }
        }
Beispiel #13
0
        //
        // LH: Cluster\Resources\GUID_OF_DTC\MSDTCPrivate\MSDTC
        // W2k3: Cluster\Resources\GUID_OF_DTC\SOME_GUID\, here SOME_GUID is the default value of GUID_OF_DTC\DataPointer\
        //
        string GetClusterMstdcRegistryKey()
        {
            Debug.Assert(IsClustered);

            if (Utilities.OSMajor > 5)
            {
                return(WsatKeys.MsdtcClusterRegKey_OS6);
            }
            ClusterRegistryConfigurationProvider clusterReg = new ClusterRegistryConfigurationProvider(this.hClusterDtcResource, WsatKeys.MsdtcClusterDataPointerRegKey_OS5);

            using (clusterReg)
            {
                //the default value
                string subKey = clusterReg.ReadString(string.Empty, string.Empty);
                if (!string.IsNullOrEmpty(subKey))
                {
                    return(subKey);
                }
            }
            RegistryExceptionHelper registryExceptionHelper = new RegistryExceptionHelper(WsatKeys.MsdtcClusterDataPointerRegKey_OS5);

            throw registryExceptionHelper.CreateRegistryAccessException(null);
        }
Beispiel #14
0
 StdRegProviderWrapper(uint hiveValue, string subKey, ManagementClass regClassInstance)
 {
     this.hiveValue = hiveValue;
     this.subKey = subKey;
     this.registryExceptionHelper = new RegistryExceptionHelper(subKey);
     this.regClassInstance = new ManagementClass(regClassInstance.Path, regClassInstance.Options);
 }
 public RegistryExceptionHelper(string machineName, RegistryHive registryHive, string registryKeyRelativeToHive)
     : this(RegistryExceptionHelper.GetRegistryKeyBase(machineName, registryHive) + registryKeyRelativeToHive)
 {
 }
Beispiel #16
0
        internal static SafeCertificateStore GetCertificateStorePointer(string machineName)
        {
            SafeCertificateStore storeHandle;

            RegistryExceptionHelper registryExceptionHelper = new RegistryExceptionHelper(machineName, RegistryHive.LocalMachine, certificateStore);

            if (Utilities.IsLocalMachineName(machineName))
            {
                SafeRegistryKey hive = new SafeRegistryKey(new IntPtr((int)Microsoft.Win32.RegistryHive.LocalMachine), false);
                SafeRegistryKey regKey = null;

                try
                {
                    int ret = SafeNativeMethods.RegOpenKeyEx(
                        hive,
                        certificateStore,
                        0,
                        SafeNativeMethods.KEY_READ,
                        out regKey);
                    if (ret != SafeNativeMethods.ERROR_SUCCESS)
                    {
                        throw registryExceptionHelper.CreateRegistryAccessException(ret);
                    }

                    storeHandle = SafeNativeMethods.CertOpenStore_ptr(
                            SafeNativeMethods.CERT_STORE_PROV_REG,
                            0,
                            0,
                            SafeNativeMethods.CERT_STORE_OPEN_EXISTING_FLAG |
                            SafeNativeMethods.CERT_STORE_READONLY_FLAG,
                            regKey);
                    if (storeHandle.IsInvalid)
                    {
                        throw new WsatAdminException(WsatAdminErrorCode.CERT_STORE_ACCESS, SR.GetString(SR.ErrorAccessCertStore, Marshal.GetLastWin32Error()));
                    }
                    return storeHandle;
                }
                finally
                {
                    if (regKey != null)
                    {
                        regKey.Close();
                    }
                    hive.Close();
                }
            }
#if WSAT_UI
            else
            {
                SafeRegistryKey remoteBase = null;
                SafeRegistryKey finalKey = null;

                try
                {
                    int ret = SafeNativeMethods.RegConnectRegistry(
                        machineName,
                        new SafeRegistryKey(new IntPtr((int)Microsoft.Win32.RegistryHive.LocalMachine), false),
                        out remoteBase);
                    if (ret != SafeNativeMethods.ERROR_SUCCESS)
                    {
                        throw registryExceptionHelper.CreateRegistryAccessException(ret);
                    }

                    ret = SafeNativeMethods.RegOpenKeyEx(
                        remoteBase,
                        certificateStore,
                        0,
                        SafeNativeMethods.KEY_READ,
                        out finalKey);
                    if (ret != SafeNativeMethods.ERROR_SUCCESS)
                    {
                        throw registryExceptionHelper.CreateRegistryAccessException(ret);
                    }

                    storeHandle = SafeNativeMethods.CertOpenStore_ptr(
                                                    SafeNativeMethods.CERT_STORE_PROV_REG,
                                                    0, 0,
                                                    SafeNativeMethods.CERT_REGISTRY_STORE_REMOTE_FLAG |
                                                    SafeNativeMethods.CERT_STORE_READONLY_FLAG |
                                                    SafeNativeMethods.CERT_STORE_OPEN_EXISTING_FLAG,
                                                    finalKey);
                    if (storeHandle.IsInvalid)
                    {
                        throw new WsatAdminException(WsatAdminErrorCode.CERT_STORE_ACCESS, SR.GetString(SR.ErrorAccessCertStore, Marshal.GetLastWin32Error()));
                    }
                    return storeHandle;                    
                }
                finally
                {
                    if (remoteBase != null)
                    {
                        remoteBase.Close();
                    }
                    if (finalKey != null)
                    {
                        finalKey.Close();
                    }
                }
            }
#else
            else
            {
                throw new WsatAdminException(WsatAdminErrorCode.CERT_STORE_ACCESS, SR.GetString(SR.ErrorAccessCertStore, 0));
            }
#endif
        }
Beispiel #17
0
        internal static SafeCertificateStore GetCertificateStorePointer(string machineName)
        {
            SafeCertificateStore storeHandle;

            RegistryExceptionHelper registryExceptionHelper = new RegistryExceptionHelper(machineName, RegistryHive.LocalMachine, certificateStore);

            if (Utilities.IsLocalMachineName(machineName))
            {
                SafeRegistryKey hive   = new SafeRegistryKey(new IntPtr((int)Microsoft.Win32.RegistryHive.LocalMachine), false);
                SafeRegistryKey regKey = null;

                try
                {
                    int ret = SafeNativeMethods.RegOpenKeyEx(
                        hive,
                        certificateStore,
                        0,
                        SafeNativeMethods.KEY_READ,
                        out regKey);
                    if (ret != SafeNativeMethods.ERROR_SUCCESS)
                    {
                        throw registryExceptionHelper.CreateRegistryAccessException(ret);
                    }

                    storeHandle = SafeNativeMethods.CertOpenStore_ptr(
                        SafeNativeMethods.CERT_STORE_PROV_REG,
                        0,
                        0,
                        SafeNativeMethods.CERT_STORE_OPEN_EXISTING_FLAG |
                        SafeNativeMethods.CERT_STORE_READONLY_FLAG,
                        regKey);
                    if (storeHandle.IsInvalid)
                    {
                        throw new WsatAdminException(WsatAdminErrorCode.CERT_STORE_ACCESS, SR.GetString(SR.ErrorAccessCertStore, Marshal.GetLastWin32Error()));
                    }
                    return(storeHandle);
                }
                finally
                {
                    if (regKey != null)
                    {
                        regKey.Close();
                    }
                    hive.Close();
                }
            }
#if WSAT_UI
            else
            {
                SafeRegistryKey remoteBase = null;
                SafeRegistryKey finalKey   = null;

                try
                {
                    int ret = SafeNativeMethods.RegConnectRegistry(
                        machineName,
                        new SafeRegistryKey(new IntPtr((int)Microsoft.Win32.RegistryHive.LocalMachine), false),
                        out remoteBase);
                    if (ret != SafeNativeMethods.ERROR_SUCCESS)
                    {
                        throw registryExceptionHelper.CreateRegistryAccessException(ret);
                    }

                    ret = SafeNativeMethods.RegOpenKeyEx(
                        remoteBase,
                        certificateStore,
                        0,
                        SafeNativeMethods.KEY_READ,
                        out finalKey);
                    if (ret != SafeNativeMethods.ERROR_SUCCESS)
                    {
                        throw registryExceptionHelper.CreateRegistryAccessException(ret);
                    }

                    storeHandle = SafeNativeMethods.CertOpenStore_ptr(
                        SafeNativeMethods.CERT_STORE_PROV_REG,
                        0, 0,
                        SafeNativeMethods.CERT_REGISTRY_STORE_REMOTE_FLAG |
                        SafeNativeMethods.CERT_STORE_READONLY_FLAG |
                        SafeNativeMethods.CERT_STORE_OPEN_EXISTING_FLAG,
                        finalKey);
                    if (storeHandle.IsInvalid)
                    {
                        throw new WsatAdminException(WsatAdminErrorCode.CERT_STORE_ACCESS, SR.GetString(SR.ErrorAccessCertStore, Marshal.GetLastWin32Error()));
                    }
                    return(storeHandle);
                }
                finally
                {
                    if (remoteBase != null)
                    {
                        remoteBase.Close();
                    }
                    if (finalKey != null)
                    {
                        finalKey.Close();
                    }
                }
            }
        //
        // LH: Cluster\Resources\GUID_OF_DTC\MSDTCPrivate\MSDTC
        // W2k3: Cluster\Resources\GUID_OF_DTC\SOME_GUID\, here SOME_GUID is the default value of GUID_OF_DTC\DataPointer\
        //       
        string GetClusterMstdcRegistryKey()
        {
            Debug.Assert(IsClustered);

            if (Utilities.OSMajor > 5)
            {
                return WsatKeys.MsdtcClusterRegKey_OS6;
            }
            ClusterRegistryConfigurationProvider clusterReg = new ClusterRegistryConfigurationProvider(this.hClusterDtcResource, WsatKeys.MsdtcClusterDataPointerRegKey_OS5);
            using (clusterReg)
            {
                //the default value 
                string subKey = clusterReg.ReadString(string.Empty, string.Empty);
                if (!string.IsNullOrEmpty(subKey))
                {
                    return subKey;
                }
            }
            RegistryExceptionHelper registryExceptionHelper = new RegistryExceptionHelper(WsatKeys.MsdtcClusterDataPointerRegKey_OS5);
            throw registryExceptionHelper.CreateRegistryAccessException(null);
        }