internal void Persist(SafeRegistryHandle hKey, string keyName)
 {
     new RegistryPermission(RegistryPermissionAccess.NoAccess, AccessControlActions.Change, keyName).Demand();
     base.WriteLock();
     try
     {
         AccessControlSections accessControlSectionsFromChanges = this.GetAccessControlSectionsFromChanges();
         if (accessControlSectionsFromChanges != AccessControlSections.None)
         {
             bool flag;
             bool flag2;
             base.Persist(hKey, accessControlSectionsFromChanges);
             base.AccessRulesModified = flag = false;
             base.AuditRulesModified = flag2 = flag;
             base.OwnerModified = base.GroupModified = flag2;
         }
     }
     finally
     {
         base.WriteUnlock();
     }
 }
Esempio n. 2
0
 private TransactedRegistryKey(SafeRegistryHandle hkey, bool writable, bool systemkey, Transaction transaction, SafeTransactionHandle txHandle)
 {
     this.hkey = hkey;
     this.keyName = "";
     if (systemkey)
     {
         this.state |= 2;
     }
     if (writable)
     {
         this.state |= 4;
     }
     if (null != transaction)
     {
         this.myTransaction = transaction.Clone();
         this.myTransactionHandle = txHandle;
     }
     else
     {
         this.myTransaction = null;
         this.myTransactionHandle = null;
     }
 }
Esempio n. 3
0
 internal static extern int RegFlushKey(SafeRegistryHandle hKey);
Esempio n. 4
0
 internal static extern int RegEnumValue(SafeRegistryHandle hKey, int dwIndex, StringBuilder lpValueName, ref int lpcbValueName, IntPtr lpReserved_MustBeZero, int[] lpType, byte[] lpData, int[] lpcbData);
Esempio n. 5
0
        private void Dispose(bool disposing)
        {
            if (_hkey != null)
            {
                if (!IsSystemKey())
                {
                    try
                    {
                        _hkey.Dispose();
                    }
                    catch (IOException)
                    {
                        // we don't really care if the handle is invalid at this point
                    }
                    finally
                    {
                        _hkey = null;
                    }
                }
            }

            if (null != _myTransaction)
            {
                // Dispose the transaction because we cloned it.
                try
                {
                    _myTransaction.Dispose();
                }
                catch (TransactionException)
                {
                    //ignore.
                }
                finally
                {
                    _myTransaction = null;
                }
            }
        }
Esempio n. 6
0
        /**
         * After calling GetLastWin32Error(), it clears the last error field,
         * so you must save the HResult and pass it to this method.  This method
         * will determine the appropriate exception to throw dependent on your
         * error, and depending on the error, insert a string into the message
         * gotten from the ResourceManager.
         */
        internal void Win32Error(int errorCode, String str)
        {
            switch (errorCode)
            {
                case Win32Native.ERROR_ACCESS_DENIED:
                    if (str != null)
                    {
                        string resourceTemplate = RegistryProviderStrings.UnauthorizedAccess_RegistryKeyGeneric_Key;
                        string resource = String.Format(CultureInfo.CurrentCulture, resourceTemplate, str);
                        throw new UnauthorizedAccessException(resource);
                    }
                    else
                        throw new UnauthorizedAccessException();

                case Win32Native.ERROR_INVALID_HANDLE:
                    // **
                    //* For normal RegistryKey instances we dispose the SafeRegHandle and throw IOException.
                    //* However, for HKEY_PERFORMANCE_DATA (on a local or remote machine) we avoid disposing the
                    //* SafeRegHandle and only throw the IOException.  This is to workaround reentrancy issues
                    //* in PerformanceCounter.NextValue() where the API could throw {NullReference, ObjectDisposed, ArgumentNull}Exception
                    //* on reentrant calls because of this error code path in RegistryKey
                    //*
                    //* Normally we'd make our caller synchronize access to a shared RegistryKey instead of doing something like this,
                    //* however we shipped PerformanceCounter.NextValue() un-synchronized in v2.0RTM and customers have taken a dependency on 
                    //* this behavior (being able to simultaneously query multiple remote-machine counters on multiple threads, instead of 
                    //* having serialized access).
                    //*
                    //* FUTURE: Consider changing PerformanceCounterLib to handle its own Win32 RegistryKey API calls instead of depending
                    //* on Microsoft.Win32.RegistryKey, so that RegistryKey can be clean of special-cases for HKEY_PERFORMANCE_DATA.
                    //
                    _hkey.SetHandleAsInvalid();
                    _hkey = null;
                    goto default;

                case Win32Native.ERROR_FILE_NOT_FOUND:
                    {
                        string resourceTemplate = RegistryProviderStrings.Arg_RegKeyNotFound;
                        string resource = String.Format(CultureInfo.CurrentCulture, resourceTemplate, errorCode.ToString(System.Globalization.CultureInfo.InvariantCulture));
                        throw new IOException(resource);
                    }

                default:
                    throw new IOException(Win32Native.GetMessage(errorCode), errorCode);
            }
        }
Esempio n. 7
0
 internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, int Reserved, RegistryValueKind dwType, ref long lpData, int cbData);
Esempio n. 8
0
 internal static extern int RegQueryInfoKey(SafeRegistryHandle hKey, StringBuilder lpClass, int[] lpcbClass, IntPtr lpReserved_MustBeZero, ref int lpcSubKeys, int[] lpcbMaxSubKeyLen, int[] lpcbMaxClassLen, ref int lpcValues, int[] lpcbMaxValueNameLen, int[] lpcbMaxValueLen, int[] lpcbSecurityDescriptor, int[] lpftLastWriteTime);
Esempio n. 9
0
 internal static extern int RegCreateKeyEx(SafeRegistryHandle hKey, string lpSubKey, int Reserved, string lpClass, int dwOptions, int samDesigner, SECURITY_ATTRIBUTES lpSecurityAttributes, out SafeRegistryHandle hkResult, out int lpdwDisposition);
Esempio n. 10
0
 internal static extern int RegConnectRegistry(string machineName, SafeRegistryHandle key, out SafeRegistryHandle result);
Esempio n. 11
0
 internal TransactedRegistrySecurity(SafeRegistryHandle hKey, string name, AccessControlSections includeSections) : base(true, ResourceType.RegistryKey, hKey, includeSections, new NativeObjectSecurity.ExceptionFromErrorCode(TransactedRegistrySecurity._HandleErrorCode), null)
 {
     new RegistryPermission(RegistryPermissionAccess.NoAccess, AccessControlActions.View, name).Demand();
 }
Esempio n. 12
0
        internal void Persist(SafeRegistryHandle hKey, String keyName)
        {
            new RegistryPermission(RegistryPermissionAccess.NoAccess, AccessControlActions.Change, keyName).Demand();

            WriteLock();

            try
            {
                AccessControlSections persistRules = GetAccessControlSectionsFromChanges();
                if (persistRules == AccessControlSections.None)
                    return;  // Don't need to persist anything.

                base.Persist(hKey, persistRules);
                OwnerModified = GroupModified = AuditRulesModified = AccessRulesModified = false;
            }
            finally
            {
                WriteUnlock();
            }
        }
Esempio n. 13
0
 private int RegOpenKeyTransactedWrapper(SafeRegistryHandle hKey, string lpSubKey, int ulOptions, int samDesired, out SafeRegistryHandle hkResult, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter)
 {
     int num = 0;
     SafeRegistryHandle handle = null;
     num = Microsoft.PowerShell.Commands.Internal.Win32Native.RegOpenKeyTransacted(this.hkey, lpSubKey, ulOptions, samDesired, out handle, hTransaction, pExtendedParameter);
     if ((num == 0) && !handle.IsInvalid)
     {
         int lpcSubKeys = 0;
         int lpcValues = 0;
         num = Microsoft.PowerShell.Commands.Internal.Win32Native.RegQueryInfoKey(handle, null, null, Microsoft.PowerShell.Commands.Internal.Win32Native.NULL, ref lpcSubKeys, null, null, ref lpcValues, null, null, null, null);
         if (0x1a2c == num)
         {
             SafeRegistryHandle handle2 = null;
             SafeRegistryHandle handle3 = null;
             num = Microsoft.PowerShell.Commands.Internal.Win32Native.RegOpenKeyEx(this.hkey, lpSubKey, ulOptions, samDesired, out handle2);
             if (num == 0)
             {
                 num = Microsoft.PowerShell.Commands.Internal.Win32Native.RegOpenKeyTransacted(handle2, null, ulOptions, samDesired, out handle3, hTransaction, pExtendedParameter);
                 if (num == 0)
                 {
                     handle.Dispose();
                     handle = handle3;
                 }
                 handle2.Dispose();
                 handle2 = null;
             }
         }
     }
     hkResult = handle;
     return num;
 }
Esempio n. 14
0
 private void Dispose(bool disposing)
 {
     if ((this.hkey != null) && !this.IsSystemKey())
     {
         try
         {
             this.hkey.Dispose();
         }
         catch (IOException)
         {
         }
         finally
         {
             this.hkey = null;
         }
     }
     if (null != this.myTransaction)
     {
         try
         {
             this.myTransaction.Dispose();
         }
         catch (TransactionException)
         {
         }
         finally
         {
             this.myTransaction = null;
         }
     }
 }
Esempio n. 15
0
 internal static extern int RegOpenKeyEx(SafeRegistryHandle hKey, string lpSubKey, int ulOptions, int samDesired, out SafeRegistryHandle hkResult);
Esempio n. 16
0
 internal static extern int RegCreateKeyTransacted(SafeRegistryHandle hKey, string lpSubKey, int Reserved, string lpClass, int dwOptions, int samDesigner, SECURITY_ATTRIBUTES lpSecurityAttributes, out SafeRegistryHandle hkResult, out int lpdwDisposition, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter);
Esempio n. 17
0
 internal static extern int RegOpenKeyTransacted(SafeRegistryHandle hKey, string lpSubKey, int ulOptions, int samDesired, out SafeRegistryHandle hkResult, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter);
Esempio n. 18
0
 internal static extern int RegDeleteKey(SafeRegistryHandle hKey, string lpSubKey);
Esempio n. 19
0
 internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, int[] lpReserved, ref int lpType, [Out] byte[] lpData, ref int lpcbData);
Esempio n. 20
0
 internal static extern int RegDeleteKeyTransacted(SafeRegistryHandle hKey, string lpSubKey, int samDesired, int reserved, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter);
Esempio n. 21
0
        private int RegOpenKeyTransactedWrapper(SafeRegistryHandle hKey, String lpSubKey,
                    int ulOptions, int samDesired, out SafeRegistryHandle hkResult,
                    SafeTransactionHandle hTransaction, IntPtr pExtendedParameter)
        {
            int error = Win32Native.ERROR_SUCCESS;
            SafeRegistryHandle hKeyToReturn = null;

            error = Win32Native.RegOpenKeyTransacted(_hkey, lpSubKey, ulOptions, samDesired, out hKeyToReturn, hTransaction, pExtendedParameter);

            if (Win32Native.ERROR_SUCCESS == error && !hKeyToReturn.IsInvalid)
            {
                // This is a check and workaround for TxR bug 181242. If we try to use the transacted hKey we just opened
                // for a call to RegQueryInfoKey and get back a ERROR_INVALID_TRANSACTION error, then the key might be a symbolic link and TxR didn't
                // do the open correctly. The workaround is to open it non-transacted, then open it again transacted without
                // a subkey string. If we get some error other than ERROR_INVALID_TRANSACTION from RegQueryInfoKey, just ignore it for now.
                int subkeyCount = 0;
                int valueCount = 0;
                error = Win32Native.RegQueryInfoKey(hKeyToReturn,
                                          null,
                                          null,
                                          Win32Native.NULL,
                                          ref subkeyCount,  // subkeys
                                          null,
                                          null,
                                          ref valueCount,     // values
                                          null,
                                          null,
                                          null,
                                          null);
                if (Win32Native.ERROR_INVALID_TRANSACTION == error)
                {
                    SafeRegistryHandle nonTxKey = null;
                    SafeRegistryHandle txKey = null;
                    error = Win32Native.RegOpenKeyEx(_hkey, lpSubKey, ulOptions, samDesired, out nonTxKey);
                    // If we got some error on this open, just ignore it and continue on with the handle
                    // we got on the original RegOpenKeyTransacted.
                    if (Win32Native.ERROR_SUCCESS == error)
                    {
                        // Now do an RegOpenKeyTransacted with the non-transacted key and no "subKey" parameter.
                        error = Win32Native.RegOpenKeyTransacted(nonTxKey, null, ulOptions, samDesired, out txKey, hTransaction, pExtendedParameter);
                        if (Win32Native.ERROR_SUCCESS == error)
                        {
                            // Let's use this hkey instead.
                            hKeyToReturn.Dispose();
                            hKeyToReturn = txKey;
                        }
                        nonTxKey.Dispose();
                        nonTxKey = null;
                    }
                }
            }
            hkResult = hKeyToReturn;
            return error;
        }
Esempio n. 22
0
 internal static extern int RegDeleteValue(SafeRegistryHandle hKey, string lpValueName);
Esempio n. 23
0
 /**
  * Creates a TransactedRegistryKey.
  *
  * 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.
  */
 private TransactedRegistryKey(SafeRegistryHandle hkey, bool writable, bool systemkey,
                               System.Transactions.Transaction transaction, SafeTransactionHandle txHandle)
 {
     _hkey = hkey;
     _keyName = "";
     if (systemkey)
     {
         _state |= STATE_SYSTEMKEY;
     }
     if (writable)
     {
         _state |= STATE_WRITEACCESS;
     }
     // We want to take our own clone so we can dispose it when we want and
     // aren't susceptible to the caller disposing it.
     if (null != transaction)
     {
         _myTransaction = transaction.Clone();
         _myTransactionHandle = txHandle;
     }
     else
     {
         _myTransaction = null;
         _myTransactionHandle = null;
     }
 }
Esempio n. 24
0
 internal static extern int RegEnumKeyEx(SafeRegistryHandle hKey, int dwIndex, StringBuilder lpName, out int lpcbName, int[] lpReserved, StringBuilder lpClass, int[] lpcbClass, long[] lpftLastWriteTime);
Esempio n. 25
0
        /**
         * Retrieves a new TransactedRegistryKey that represents the requested key. Valid
         * values are:
         *
         * 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.
         *
         * @return the TransactedRegistryKey requested.
         */
        internal static TransactedRegistryKey GetBaseKey(IntPtr hKey)
        {
            int index = ((int)hKey) & 0x0FFFFFFF;
            BCLDebug.Assert(index >= 0 && index < s_hkeyNames.Length, "index is out of range!");
            BCLDebug.Assert((((int)hKey) & 0xFFFFFFF0) == 0x80000000, "Invalid hkey value!");

            SafeRegistryHandle srh = new SafeRegistryHandle(hKey, false);

            // For Base keys, there is no transaction associated with the HKEY.
            TransactedRegistryKey key = new TransactedRegistryKey(srh, true, true, null, null);
            key._checkMode = RegistryKeyPermissionCheck.Default;
            key._keyName = s_hkeyNames[index];
            return key;
        }
Esempio n. 26
0
        internal void Win32Error(int errorCode, string str)
        {
            switch (errorCode)
            {
                case 2:
                {
                    string format = RegistryProviderStrings.Arg_RegKeyNotFound;
                    throw new IOException(string.Format(CultureInfo.CurrentCulture, format, new object[] { errorCode.ToString(CultureInfo.InvariantCulture) }));
                }
                case 5:
                    if (str != null)
                    {
                        string str2 = RegistryProviderStrings.UnauthorizedAccess_RegistryKeyGeneric_Key;
                        throw new UnauthorizedAccessException(string.Format(CultureInfo.CurrentCulture, str2, new object[] { str }));
                    }
                    throw new UnauthorizedAccessException();

                case 6:
                    this.hkey.SetHandleAsInvalid();
                    this.hkey = null;
                    break;
            }
            throw new IOException(Microsoft.PowerShell.Commands.Internal.Win32Native.GetMessage(errorCode), errorCode);
        }