Exemple #1
0
 def test_del_key_recursive_machine(self):
     '''
     This is a DESTRUCTIVE TEST it creates a new registry entry.
     And then destroys the registry entry recusively , however it is completed in its own space
     within the registry. We mark this as destructiveTest as it has the potential
     to detroy a machine if salt reg code has a large error in it.
     '''
     subkey = 'Software\\SaltStackTest'
     vname = UNICODE_TEST_KEY_DEL
     vdata = 'I will be deleted recursive'
     if PY2:
         handle = _winreg.CreateKeyEx(_winreg.HKEY_LOCAL_MACHINE,
                                      subkey.encode('mbcs'), 0,
                                      _winreg.KEY_ALL_ACCESS)
         _winreg.SetValueEx(handle, vname.encode('mbcs'), 0, _winreg.REG_SZ,
                            vdata.encode('mbcs'))
     else:
         handle = _winreg.CreateKeyEx(_winreg.HKEY_LOCAL_MACHINE, subkey, 0,
                                      _winreg.KEY_ALL_ACCESS)
         _winreg.SetValueEx(handle, vname, 0, _winreg.REG_SZ, vdata)
     _winreg.CloseKey(handle)
     # time.sleep(15) # delays for 15 seconds so you can run regedit and watch it happen
     test_success = win_mod_reg.delete_key_recursive(
         'HKEY_LOCAL_MACHINE', subkey)
     self.assertTrue(test_success)
Exemple #2
0
 def test_del_key_recursive_user(self):
     '''
     Test - Create directly key/value pair and Delete recusivly with salt
     '''
     subkey = 'Software\\SaltStackTest'
     vname = UNICODE_TEST_KEY_DEL
     vdata = 'I will be deleted recursive'
     if PY2:
         handle = _winreg.CreateKeyEx(
                     _winreg.HKEY_CURRENT_USER,
                     subkey.encode('mbcs'),
                     0,
                     _winreg.KEY_ALL_ACCESS
                     )
         _winreg.SetValueEx(
             handle,
             vname.encode('mbcs'),
             0,
             _winreg.REG_SZ,
             vdata.encode('mbcs')
             )
     else:
         handle = _winreg.CreateKeyEx(
                     _winreg.HKEY_CURRENT_USER,
                     subkey,
                     0,
                     _winreg.KEY_ALL_ACCESS
                     )
         _winreg.SetValueEx(handle, vname, 0, _winreg.REG_SZ, vdata)
     _winreg.CloseKey(handle)
     # time.sleep(15) # delays for 15 seconds so you can run regedit & watch it happen
     test_success = win_mod_reg.delete_key_recursive('HKEY_CURRENT_USER', subkey)
     self.assertTrue(test_success)
Exemple #3
0
def set_key(hkey, path, key, value, vtype='REG_DWORD', reflection=True):
    '''
    Set a registry key
    vtype: http://docs.python.org/2/library/_winreg.html#value-types

    CLI Example:

    .. code-block:: bash

        salt '*' reg.set_key HKEY_CURRENT_USER 'SOFTWARE\\Salt' 'version' '0.97' REG_DWORD
    '''
    registry = Registry()
    hkey2 = getattr(registry, hkey)
    access_mask = registry.reflection_mask[reflection]

    try:
        _type = getattr(_winreg, vtype)
    except AttributeError:
        return False

    try:
        handle = _winreg.OpenKey(hkey2, path, 0, access_mask)
        _winreg.SetValueEx(handle, key, 0, _type, value)
        _winreg.CloseKey(handle)
        return True
    except Exception:
        handle = _winreg.CreateKeyEx(hkey2, path, 0, access_mask)
        _winreg.SetValueEx(handle, key, 0, _type, value)
        _winreg.CloseKey(handle)
    return True
Exemple #4
0
def set_value(hive,
              key,
              vname=None,
              vdata=None,
              vtype='REG_SZ',
              reflection=True):
    '''
    Sets a registry value.

    :param hive: string
    The name of the hive. Can be one of the following
    - HKEY_LOCAL_MACHINE or HKLM
    - HKEY_CURRENT_USER or HKCU
    - HKEY_USER or HKU

    :param key: string
    The key (looks like a path) to the value name.

    :param vname: string
    The value name. These are the individual name/data pairs under the key. If
    not passed, the key (Default) value will be set.

    :param vdata: string
    The value data to be set.

    :param vtype: string
    The value type. Can be one of the following:
    - REG_BINARY
    - REG_DWORD
    - REG_EXPAND_SZ
    - REG_MULTI_SZ
    - REG_SZ

    :param reflection: boolean
    A boolean value indicating that the value should also be set in the
    Wow6432Node portion of the registry. Only applies to 64 bit Windows. This
    setting is ignored for 32 bit Windows.

    :return: boolean
    Returns True if successful, False if not

    CLI Example:

    .. code-block:: bash

        salt '*' reg.set_value HKEY_LOCAL_MACHINE 'SOFTWARE\\Salt' 'version' '2015.5.2'
    '''
    registry = Registry()
    hive = registry.hkeys[hive]
    vtype = registry.vtype[vtype]
    access_mask = registry.reflection_mask[reflection]

    try:
        handle = _winreg.CreateKeyEx(hive, key, 0, access_mask)
        _winreg.SetValueEx(handle, vname, 0, vtype, vdata)
        _winreg.CloseKey(handle)
        return True
    except WindowsError as exc:  # pylint: disable=E0602
        log.error(exc)
        return False
Exemple #5
0
def set_value(hive,
              key,
              vname=None,
              vdata=None,
              vtype='REG_SZ',
              use_32bit_registry=False):
    '''
    Sets a registry value entry or the default value for a key.

    :param str hive: The name of the hive. Can be one of the following

        - HKEY_LOCAL_MACHINE or HKLM
        - HKEY_CURRENT_USER or HKCU
        - HKEY_USER or HKU

    :param str key: The key (looks like a path) to the value name.

    :param str vname: The value name. These are the individual name/data pairs
    under the key. If not passed, the key (Default) value will be set.

    :param str vdata: The value data to be set.

    :param str vtype: The value type. Can be one of the following:

        - REG_BINARY
        - REG_DWORD
        - REG_EXPAND_SZ
        - REG_MULTI_SZ
        - REG_SZ

    :param bool use_32bit_registry: Sets the 32bit portion of the registry on
    64bit installations. On 32bit machines this is ignored.

    :return: Returns True if successful, False if not
    :rtype: bool

    CLI Example:

    .. code-block:: bash

        salt '*' reg.set_value HKEY_LOCAL_MACHINE 'SOFTWARE\\Salt' 'version' '2015.5.2'
    '''
    registry = Registry()
    hkey = registry.hkeys[hive]
    vtype = registry.vtype[vtype]
    access_mask = registry.registry_32[use_32bit_registry]

    try:
        handle = _winreg.CreateKeyEx(hkey, key, 0, access_mask)
        if vtype == registry.vtype['REG_SZ']\
                or vtype == registry.vtype['REG_BINARY']:
            vdata = str(vdata)
        _winreg.SetValueEx(handle, vname, 0, vtype, vdata)
        _winreg.FlushKey(handle)
        _winreg.CloseKey(handle)
        broadcast_change()
        return True
    except (WindowsError, ValueError, TypeError) as exc:  # pylint: disable=E0602
        log.error(exc, exc_info=True)
        return False
Exemple #6
0
 def test_del_value(self):
     '''
     Test - Create Directly and Delete with salt a registry value
     '''
     subkey = 'Software\\SaltStackTest'
     vname = UNICODE_TEST_KEY_DEL
     vdata = 'I will be deleted'
     if PY2:
         handle = _winreg.CreateKeyEx(_winreg.HKEY_LOCAL_MACHINE,
                                      subkey.encode('mbcs'), 0,
                                      _winreg.KEY_ALL_ACCESS)
         _winreg.SetValueEx(handle, vname.encode('mbcs'), 0, _winreg.REG_SZ,
                            vdata.encode('mbcs'))
     else:
         handle = _winreg.CreateKeyEx(_winreg.HKEY_LOCAL_MACHINE, subkey, 0,
                                      _winreg.KEY_ALL_ACCESS)
         _winreg.SetValueEx(handle, vname, 0, _winreg.REG_SZ, vdata)
     _winreg.CloseKey(handle)
     # time.sleep(15) # delays for 15 seconds
     test_success = win_mod_reg.delete_value('HKEY_LOCAL_MACHINE', subkey,
                                             vname)
     self.assertTrue(test_success)
Exemple #7
0
def create_key(hkey, path, key=None, value=None, reflection=True):
    '''
    *** Incorrect Usage ***
    The name of this function is misleading and will be changed to reflect
    proper usage in the Boron release of Salt. The path option will be removed
    and the key will be the actual key. See the following issue:

    https://github.com/saltstack/salt/issues/25618

    In order to not break existing state files this function will call the
    set_value function if key is passed. Key will be passed as the value name.
    If key is not passed, this function will return the default value for the
    key.

    In the Boron release path will be removed and key will be the path. You will
    not pass value.
    ***

    Create a registry key

    CLI Example:

    .. code-block:: bash

        salt '*' reg.create_key HKEY_CURRENT_USER 'SOFTWARE\\Salt' 'version' '0.97'
    '''
    if key:  # This if statement will be removed in Boron
        salt.utils.warn_until(
            'Boron', 'Use reg.set_value to set a registry '
            'value. This functionality will be '
            'removed in Salt Boron')
        return set_value(hive=hkey,
                         key=path,
                         vname=key,
                         vdata=value,
                         vtype='REG_SZ')

    registry = Registry()
    hive = registry.hkeys[hkey]
    key = path
    access_mask = registry.reflection_mask[reflection]

    try:
        handle = _winreg.CreateKeyEx(hive, key, 0, access_mask)
        _winreg.CloseKey(handle)
        return True
    except WindowsError as exc:  # pylint: disable=E0602
        log.error(exc)
        return False
Exemple #8
0
def create_key(hkey, path, key, value=None, reflection=True):
    '''
    Create a registry key

    CLI Example:

    .. code-block:: bash

        salt '*' reg.create_key HKEY_CURRENT_USER 'SOFTWARE\\Salt' 'version' '0.97'
    '''
    registry = Registry()
    hkey2 = getattr(registry, hkey)
    access_mask = registry.reflection_mask[reflection]

    try:
        handle = _winreg.OpenKey(hkey2, path, 0, access_mask)
        _winreg.CloseKey(handle)
        return True
    except Exception:
        handle = _winreg.CreateKeyEx(hkey2, path, 0, access_mask)
        if value:
            _winreg.SetValueEx(handle, key, 0, _winreg.REG_DWORD, value)
        _winreg.CloseKey(handle)
    return True
Exemple #9
0
def set_value(hive,
              key,
              vname=None,
              vdata=None,
              vtype='REG_SZ',
              reflection=True,
              use_32bit_registry=False):
    '''
    Sets a registry value entry or the default value for a key.

    :param str hive: The name of the hive. Can be one of the following

        - HKEY_LOCAL_MACHINE or HKLM
        - HKEY_CURRENT_USER or HKCU
        - HKEY_USER or HKU

    :param str key: The key (looks like a path) to the value name.

    :param str vname: The value name. These are the individual name/data pairs
      under the key. If not passed, the key (Default) value will be set.

    :param str vdata: The value data to be set.

    :param str vtype: The value type. Can be one of the following:

        - REG_BINARY
        - REG_DWORD
        - REG_EXPAND_SZ
        - REG_MULTI_SZ
        - REG_SZ

    :param bool reflection: A boolean value indicating that the value should
      also be set in the Wow6432Node portion of the registry. Only applies to 64
      bit Windows. This setting is ignored for 32 bit Windows.

    .. deprecated:: 2015.8.2
       Use ``use_32bit_registry`` instead. The parameter seems to have no effect
       since Windows 7 / Windows 2008R2 removed support for reflection. The
       parameter will be removed in Boron.

    :return: Returns True if successful, False if not

    :rtype: bool

    CLI Example:

    .. code-block:: bash

        salt '*' reg.set_value HKEY_LOCAL_MACHINE 'SOFTWARE\\Salt' 'version' '2015.5.2'
    '''
    registry = Registry()
    hkey = registry.hkeys[hive]
    vtype = registry.vtype[vtype]
    access_mask = registry.registry_32[use_32bit_registry]

    try:
        handle = _winreg.CreateKeyEx(hkey, key, 0, access_mask)
        if vtype == registry.vtype['REG_SZ']\
                or vtype == registry.vtype['REG_BINARY']:
            vdata = str(vdata)
        _winreg.SetValueEx(handle, vname, 0, vtype, vdata)
        _winreg.FlushKey(handle)
        _winreg.CloseKey(handle)
        return True
    except (WindowsError, ValueError, TypeError) as exc:  # pylint: disable=E0602
        log.error(exc, exc_info=True)
        return False