def _get_product_information(reg_hive, reg_key, reg_handle): # This is a list of default OS reg entries that don't seem to be installed # software and no version information exists on any of these items # Also, some MS Office updates don't register a product name which means # their information is useless. ignore_list = ['AddressBook', 'Connection Manager', 'DirectDrawEx', 'Fontcore', 'IE40', 'IE4Data', 'IE5BAKEX', 'IEData', 'MobileOptionPack', 'SchedulingAgent', 'WIC', 'Not Found'] encoding = locale.getpreferredencoding() products = {} try: i = 0 while True: product_key = None asubkey = _winreg.EnumKey(reg_handle, i) rd_uninst_key = "\\".join([reg_key, asubkey]) displayName = '' displayVersion = '' try: # these are not garuanteed to exist product_key = _open_registry_key(reg_hive, rd_uninst_key) displayName, value_type = _winreg.QueryValueEx(product_key, "DisplayName") try: displayName = displayName.decode(encoding) except Exception: pass displayVersion, value_type = _winreg.QueryValueEx(product_key, "DisplayVersion") except Exception: pass if product_key is not None: _winreg.CloseKey(product_key) i += 1 if displayName not in ignore_list: # some MS Office updates don't register a product name which means # their information is useless if displayName != '': products[displayName] = displayVersion except WindowsError: # pylint: disable=E0602 pass return products
def test_set_value_unicode(self): ''' Test - set a registry plain text subkey name to a unicode string value ''' vname = 'TestUniccodeString' subkey = 'Software\\SaltStackTest' test1_success = False test2_success = False test1_success = win_mod_reg.set_value( 'HKEY_LOCAL_MACHINE', subkey, vname, UNICODETEST_WITH_SIGNS ) # Now use _winreg direct to see if it worked as expected if test1_success: handle = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, subkey, 0, _winreg.KEY_ALL_ACCESS ) (current_vdata, dummy_current_vtype) = _winreg.QueryValueEx(handle, vname) _winreg.CloseKey(handle) test2_success = (current_vdata == UNICODETEST_WITH_SIGNS) self.assertTrue(test1_success and test2_success)
def read_value(hive, key, vname=None): r''' Reads a registry value or the default value for a key. :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 returned :return: dict A dictionary containing the passed settings as well as the value_data if successful. If unsuccessful, sets success to False CLI Example: .. code-block:: bash salt '*' reg.read_value HKEY_LOCAL_MACHINE 'SOFTWARE\Salt' 'version' ''' # Setup the return array ret = { 'hive': hive, 'key': key, 'vname': vname, 'vdata': None, 'success': True } # If no name is passed, the default value of the key will be returned # The value name is Default if not vname: ret['vname'] = '(Default)' registry = Registry() hive = registry.hkeys[hive] try: handle = _winreg.OpenKey(hive, key) value, vtype = _winreg.QueryValueEx(handle, vname) if value: ret['vdata'] = value ret['vtype'] = registry.vtype_reverse[vtype] else: ret['comment'] = 'Empty Value' except WindowsError as exc: # pylint: disable=E0602 log.debug(exc) ret['comment'] = '{0}'.format(exc) ret['success'] = False return ret
def __virtual__(): """ Verify RabbitMQ is installed. """ global RABBITMQCTL global RABBITMQ_PLUGINS if salt.utils.platform.is_windows(): from salt.ext.six.moves import winreg key = None try: key = winreg.OpenKeyEx( winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\VMware, Inc.\\RabbitMQ Server", 0, winreg.KEY_READ | winreg.KEY_WOW64_32KEY, ) (dir_path, value_type) = winreg.QueryValueEx(key, "Install_Dir") if value_type != winreg.REG_SZ: raise TypeError( "Invalid RabbitMQ Server directory type: {0}".format( value_type)) if not os.path.isdir(dir_path): raise IOError( "RabbitMQ directory not found: {0}".format(dir_path)) subdir_match = "" for name in os.listdir(dir_path): if name.startswith("rabbitmq_server-"): subdir_path = os.path.join(dir_path, name) # Get the matching entry that is last in ASCII order. if os.path.isdir( subdir_path) and subdir_path > subdir_match: subdir_match = subdir_path if not subdir_match: raise IOError( '"rabbitmq_server-*" subdirectory not found in: {0}'. format(dir_path)) RABBITMQCTL = os.path.join(subdir_match, "sbin", "rabbitmqctl.bat") RABBITMQ_PLUGINS = os.path.join(subdir_match, "sbin", "rabbitmq-plugins.bat") except Exception: # pylint: disable=broad-except pass finally: if key is not None: winreg.CloseKey(key) else: RABBITMQCTL = salt.utils.path.which("rabbitmqctl") RABBITMQ_PLUGINS = salt.utils.path.which("rabbitmq-plugins") if not RABBITMQCTL: return ( False, "Module rabbitmq: module only works when RabbitMQ is installed") return True
def _get_windows_skyline_appdata_dir(): # pylint: disable=invalid-name ''' Return the Skyline AppData directory on Windows. This looks like: 'C:\\ProgramData\\National Instruments\\Skyline' ''' with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, NI_INSTALLERS_REG_PATH, 0, winreg.KEY_READ) as hkey: (appdata_dir, _) = winreg.QueryValueEx(hkey, NI_INSTALLERS_REG_KEY_APP_DATA) return os.path.join(appdata_dir, 'Skyline')
def __virtual__(): ''' Verify RabbitMQ is installed. ''' global RABBITMQCTL global RABBITMQ_PLUGINS if salt.utils.is_windows(): from salt.ext.six.moves import winreg key = None try: key = winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\VMware, Inc.\\RabbitMQ Server', 0, winreg.KEY_READ | winreg.KEY_WOW64_32KEY) (dir_path, value_type) = winreg.QueryValueEx(key, 'Install_Dir') if value_type != winreg.REG_SZ: raise TypeError( 'Invalid RabbitMQ Server directory type: {0}'.format( value_type)) if not os.path.isdir(dir_path): raise IOError( 'RabbitMQ directory not found: {0}'.format(dir_path)) subdir_match = '' for name in os.listdir(dir_path): if name.startswith('rabbitmq_server-'): subdir_path = os.path.join(dir_path, name) # Get the matching entry that is last in ASCII order. if os.path.isdir( subdir_path) and subdir_path > subdir_match: subdir_match = subdir_path if not subdir_match: raise IOError( '"rabbitmq_server-*" subdirectory not found in: {0}'. format(dir_path)) RABBITMQCTL = os.path.join(subdir_match, 'sbin', 'rabbitmqctl.bat') RABBITMQ_PLUGINS = os.path.join(subdir_match, 'sbin', 'rabbitmq-plugins.bat') except Exception: pass finally: if key is not None: winreg.CloseKey(key) else: RABBITMQCTL = salt.utils.which('rabbitmqctl') RABBITMQ_PLUGINS = salt.utils.which('rabbitmq-plugins') if not RABBITMQCTL: return ( False, 'Module rabbitmq: module only works when RabbitMQ is installed') return True
def test_read_reg_unicode(self): ''' Test - Read a registry value from a subkey using Pythen 2 Unicode or Pythen 3 Str i.e. Unicode ''' subkey = 'Software\\Microsoft\\Windows NT\\CurrentVersion' vname = 'PathName' handle = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey, 0, _winreg.KEY_ALL_ACCESS) (current_vdata, dummy_current_vtype) = _winreg.QueryValueEx(handle, vname) _winreg.CloseKey(handle) test_vdata = win_mod_reg.read_value('HKEY_LOCAL_MACHINE', subkey, vname)['vdata'] self.assertEqual(test_vdata, current_vdata)
def read_key(hkey, path, key, reflection=True): ''' Read registry key value CLI Example: .. code-block:: bash salt '*' reg.read_key HKEY_LOCAL_MACHINE 'SOFTWARE\\Salt' 'version' ''' registry = Registry() hkey2 = getattr(registry, hkey) access_mask = registry.reflection_mask[reflection] try: handle = _winreg.OpenKeyEx(hkey2, path, 0, access_mask) return _winreg.QueryValueEx(handle, key)[0] except Exception: return None
def test_read_reg_plain(self): ''' Test - Read a registry value from a subkey using Pythen 2 Strings or Pythen 3 Bytes ''' if not PY2: self.skipTest('Invalid for Python Version 2') subkey = b'Software\\Microsoft\\Windows NT\\CurrentVersion' vname = b'PathName' handle = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey, 0, _winreg.KEY_ALL_ACCESS) (current_vdata, dummy_current_vtype) = _winreg.QueryValueEx(handle, vname) _winreg.CloseKey(handle) test_vdata = win_mod_reg.read_value(b'HKEY_LOCAL_MACHINE', subkey, vname)[b'vdata'] self.assertEqual(test_vdata, current_vdata)
def read_value(hive, key, vname=None, use_32bit_registry=False): r''' Reads 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 returned :param bool use_32bit_registry: Accesses the 32bit portion of the registry on 64bit installations. On 32bit machines this is ignored. :return: A dictionary containing the passed settings as well as the value_data if successful. If unsuccessful, sets success to False. :rtype: dict If vname is not passed: - Returns the first unnamed value (Default) as a string. - Returns none if first unnamed value is empty. - Returns False if key not found. CLI Example: .. code-block:: bash salt '*' reg.read_value HKEY_LOCAL_MACHINE 'SOFTWARE\Salt' 'version' ''' # If no name is passed, the default value of the key will be returned # The value name is Default # Setup the return array if PY2: ret = { 'hive': _mbcs_to_unicode(hive), 'key': _mbcs_to_unicode(key), 'vname': _mbcs_to_unicode(vname), 'vdata': None, 'success': True } local_hive = _mbcs_to_unicode(hive) local_key = _unicode_to_mbcs(key) local_vname = _unicode_to_mbcs(vname) else: ret = { 'hive': hive, 'key': key, 'vname': vname, 'vdata': None, 'success': True } local_hive = hive local_key = key local_vname = vname if not vname: ret['vname'] = '(Default)' registry = Registry() hkey = registry.hkeys[local_hive] access_mask = registry.registry_32[use_32bit_registry] try: handle = _winreg.OpenKey(hkey, local_key, 0, access_mask) try: # QueryValueEx returns unicode data vdata, vtype = _winreg.QueryValueEx(handle, local_vname) if vdata or vdata in [0, '']: ret['vtype'] = registry.vtype_reverse[vtype] ret['vdata'] = vdata else: ret['comment'] = 'Empty Value' except WindowsError: # pylint: disable=E0602 ret['vdata'] = ('(value not set)') ret['vtype'] = 'REG_SZ' except WindowsError as exc: # pylint: disable=E0602 log.debug(exc) log.debug('Cannot find key: {0}\\{1}'.format(local_hive, local_key)) ret['comment'] = 'Cannot find key: {0}\\{1}'.format( local_hive, local_key) ret['success'] = False return ret
def list_values(hive, key=None, use_32bit_registry=False, include_default=True): ''' Enumerates the values in a registry key or hive. :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. If a key is not passed, the values under the hive will be returned. :param bool use_32bit_registry: Accesses the 32bit portion of the registry on 64 bit installations. On 32bit machines this is ignored. :param bool include_default: Toggle whether to include the '(Default)' value. :return: A list of values under the hive or key. :rtype: list CLI Example: .. code-block:: bash salt '*' reg.list_values HKLM 'SYSTEM\\CurrentControlSet\\Services\\Tcpip' ''' if PY2: local_hive = _mbcs_to_unicode(hive) local_key = _unicode_to_mbcs(key) else: local_hive = hive local_key = key registry = Registry() hkey = registry.hkeys[local_hive] access_mask = registry.registry_32[use_32bit_registry] handle = None values = list() try: handle = _winreg.OpenKey(hkey, local_key, 0, access_mask) for i in range(_winreg.QueryInfoKey(handle)[1]): vname, vdata, vtype = _winreg.EnumValue(handle, i) value = { 'hive': local_hive, 'key': local_key, 'vname': vname, 'vdata': vdata, 'vtype': registry.vtype_reverse[vtype], 'success': True } values.append(value) if include_default: # Get the default value for the key value = { 'hive': local_hive, 'key': local_key, 'vname': '(Default)', 'vdata': None, 'success': True } try: # QueryValueEx returns unicode data vdata, vtype = _winreg.QueryValueEx(handle, '(Default)') if vdata or vdata in [0, '']: value['vtype'] = registry.vtype_reverse[vtype] value['vdata'] = vdata else: value['comment'] = 'Empty Value' except WindowsError: # pylint: disable=E0602 value['vdata'] = ('(value not set)') value['vtype'] = 'REG_SZ' values.append(value) except WindowsError as exc: # pylint: disable=E0602 log.debug(exc) log.debug(r'Cannot find key: {0}\{1}'.format(hive, key)) return False, r'Cannot find key: {0}\{1}'.format(hive, key) finally: if handle: handle.Close() return values
def read_value(hive, key, vname=None): r''' Reads 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 returned :return: A dictionary containing the passed settings as well as the value_data if successful. If unsuccessful, sets success to False If vname is not passed: - Returns the first unnamed value (Default) as a string. - Returns none if first unnamed value is empty. - Returns False if key not found. :rtype: dict CLI Example: .. code-block:: bash salt '*' reg.read_value HKEY_LOCAL_MACHINE 'SOFTWARE\Salt' 'version' ''' # Setup the return array ret = {'hive': hive, 'key': key, 'vname': vname, 'vdata': None, 'success': True} # If no name is passed, the default value of the key will be returned # The value name is Default if not vname: ret['vname'] = '(Default)' registry = Registry() hkey = registry.hkeys[hive] try: handle = _winreg.OpenKey(hkey, key) try: vdata, vtype = _winreg.QueryValueEx(handle, vname) if vdata or vdata in [0, '']: ret['vtype'] = registry.vtype_reverse[vtype] ret['vdata'] = vdata else: ret['comment'] = 'Empty Value' except WindowsError as exc: # pylint: disable=E0602 ret['vdata'] = ('(value not set)') ret['vtype'] = 'REG_SZ' ret['success'] = True except WindowsError as exc: # pylint: disable=E0602 log.debug(exc) log.debug('Cannot find key: {0}\\{1}'.format(hive, key)) ret['comment'] = 'Cannot find key: {0}\\{1}'.format(hive, key) ret['success'] = False return ret