def get_pending_component_servicing(): ''' Determine whether there are pending Component Based Servicing tasks that require a reboot. :return: A boolean representing whether there are pending Component Based Servicing tasks. :rtype: bool .. versionadded:: Carbon CLI Example: .. code-block:: bash salt '*' system.get_pending_component_servicing ''' vname = '(Default)' key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending' reg_ret = read_value('HKLM', key, vname) # So long as the registry key exists, a reboot is pending. if reg_ret['success']: log.debug('Found key: %s', key) return True else: log.debug('Unable to access key: %s', key) return False
def get_pending_computer_name(): ''' Get a pending computer name. If the computer name has been changed, and the change is pending a system reboot, this function will return the pending computer name. Otherwise, ``None`` will be returned. If there was an error retrieving the pending computer name, ``False`` will be returned, and an error message will be logged to the minion log. :return: Returns the pending name if pending restart. Returns none if not pending restart. CLI Example: .. code-block:: bash salt 'minion-id' system.get_pending_computer_name ''' current = get_computer_name() pending = read_value( 'HKLM', r'SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName', 'ComputerName')['vdata'] if pending: return pending if pending != current else None return False
def test_set_value_unicode_value(self): """ Test the set_value function on a unicode value """ try: self.assertTrue( reg.set_value(hive="HKLM", key=FAKE_KEY, vname="fake_unicode", vdata=UNICODE_VALUE)) expected = { "hive": "HKLM", "key": FAKE_KEY, "success": True, "vdata": UNICODE_VALUE, "vname": "fake_unicode", "vtype": "REG_SZ", } self.assertEqual( reg.read_value(hive="HKLM", key=FAKE_KEY, vname="fake_unicode"), expected, ) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_read_value_multi_sz_empty_list(self): """ An empty REG_MULTI_SZ value should return an empty list, not None """ try: self.assertTrue( reg.set_value( hive="HKLM", key=FAKE_KEY, vname="empty_list", vdata=[], vtype="REG_MULTI_SZ", )) expected = { "hive": "HKLM", "key": FAKE_KEY, "success": True, "vdata": [], "vname": "empty_list", "vtype": "REG_MULTI_SZ", } self.assertEqual( reg.read_value( hive="HKLM", key=FAKE_KEY, vname="empty_list", ), expected, ) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_read_value_multi_sz_empty_list(self): ''' An empty REG_MULTI_SZ value should return an empty list, not None ''' try: self.assertTrue( reg.set_value(hive='HKLM', key=FAKE_KEY, vname='empty_list', vdata=[], vtype='REG_MULTI_SZ')) expected = { 'hive': 'HKLM', 'key': FAKE_KEY, 'success': True, 'vdata': [], 'vname': 'empty_list', 'vtype': 'REG_MULTI_SZ' } self.assertEqual( reg.read_value( hive='HKLM', key=FAKE_KEY, vname='empty_list', ), expected) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
def test_set_value_reg_qword(self): """ Test the set_value function on a REG_QWORD value """ try: self.assertTrue( reg.set_value( hive="HKLM", key=FAKE_KEY, vname="qword_value", vdata=123, vtype="REG_QWORD", )) expected = { "hive": "HKLM", "key": FAKE_KEY, "success": True, "vdata": 123, "vname": "qword_value", "vtype": "REG_QWORD", } self.assertEqual( reg.read_value(hive="HKLM", key=FAKE_KEY, vname="qword_value"), expected) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def get_pending_servermanager(): ''' Determine whether there are pending Server Manager tasks that require a reboot. :return: A boolean representing whether there are pending Server Manager tasks. :rtype: bool .. versionadded:: Carbon CLI Example: .. code-block:: bash salt '*' system.get_pending_servermanager ''' vname = 'CurrentRebootAttempts' key = r'SOFTWARE\Microsoft\ServerManager' # There are situations where it's possible to have '(value not set)' as # the value data, and since an actual reboot wont be pending in that # instance, just catch instances where we try unsuccessfully to cast as int. reg_ret = read_value('HKLM', key, vname) if reg_ret['success']: log.debug('Found key: %s', key) try: if int(reg_ret['vdata']) > 0: return True except ValueError: pass else: log.debug('Unable to access key: %s', key) return False
def get_pending_computer_name(): ''' Get a pending computer name. If the computer name has been changed, and the change is pending a system reboot, this function will return the pending computer name. Otherwise, ``None`` will be returned. If there was an error retrieving the pending computer name, ``False`` will be returned, and an error message will be logged to the minion log. :return: Returns the pending name if pending restart. Returns none if not pending restart. CLI Example: .. code-block:: bash salt 'minion-id' system.get_pending_computer_name ''' current = get_computer_name() pending = read_value('HKLM', r'SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName', 'ComputerName')['vdata'] if pending: return pending if pending != current else None return False
def test_read_value_non_existing(self): """ Test the read_value function using a non existing value pair """ expected = { "comment": ("Cannot find fake_name in HKLM\\SOFTWARE\\Microsoft\\" "Windows\\CurrentVersion"), "vdata": None, "vname": "fake_name", "success": False, "hive": "HKLM", "key": "SOFTWARE\\Microsoft\\Windows\\CurrentVersion", } self.assertDictEqual( reg.read_value( hive="HKLM", key="SOFTWARE\\Microsoft\\Windows\\CurrentVersion", vname="fake_name", ), expected, )
def get_pending_file_rename(): ''' Determine whether there are pending file rename operations that require a reboot. :return: A boolean representing whether there are pending file rename operations. :rtype: bool .. versionadded:: Carbon CLI Example: .. code-block:: bash salt '*' system.get_pending_file_rename ''' vnames = ('PendingFileRenameOperations', 'PendingFileRenameOperations2') key = r'SYSTEM\CurrentControlSet\Control\Session Manager' # If any of the value names exist and have value data set, # then a reboot is pending. for vname in vnames: reg_ret = read_value('HKLM', key, vname) if reg_ret['success']: log.debug('Found key: %s', key) if reg_ret['vdata'] and (reg_ret['vdata'] != '(value not set)'): return True else: log.debug('Unable to access key: %s', key) return False
def test_read_value_default(self): ''' Test the read_value function reading the default value using a well known registry key ''' ret = reg.read_value( hive='HKLM', key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion') self.assertEqual(ret['vdata'], '(value not set)')
def test_read_value_default(self): """ Test the read_value function reading the default value using a well known registry key """ ret = reg.read_value( hive="HKLM", key="SOFTWARE\\Microsoft\\Windows\\CurrentVersion") self.assertEqual(ret["vdata"], "(value not set)")
def test_read_value_existing(self): ''' Test the read_value function using a well known registry value ''' ret = reg.read_value( hive='HKLM', key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion', vname='ProgramFilesPath') self.assertEqual(ret['vdata'], '%ProgramFiles%')
def test_read_value_existing(self): """ Test the read_value function using a well known registry value """ ret = reg.read_value( hive="HKLM", key="SOFTWARE\\Microsoft\\Windows\\CurrentVersion", vname="ProgramFilesPath", ) self.assertEqual(ret["vdata"], "%ProgramFiles%")
def get_pending_domain_join(): ''' Determine whether there is a pending domain join action that requires a reboot. :return: A boolean representing whether there is a pending domain join action. :rtype: bool .. versionadded:: Carbon CLI Example: .. code-block:: bash salt '*' system.get_pending_domain_join ''' vname = '(Default)' base_key = r'SYSTEM\CurrentControlSet\Services\Netlogon' avoid_key = r'{0}\AvoidSpnSet'.format(base_key) join_key = r'{0}\JoinDomain'.format(base_key) # If either the avoid_key or join_key is present, # then there is a reboot pending. avoid_reg_ret = read_value('HKLM', avoid_key, vname) if avoid_reg_ret['success']: log.debug('Found key: %s', avoid_key) return True else: log.debug('Unable to access key: %s', avoid_key) join_reg_ret = read_value('HKLM', join_key, vname) if join_reg_ret['success']: log.debug('Found key: %s', join_key) return True else: log.debug('Unable to access key: %s', join_key) return False
def test_read_value_non_existing_key(self): """ Test the read_value function using a non existing registry key """ expected = { "comment": "Cannot find key: HKLM\\{}".format(FAKE_KEY), "vdata": None, "vname": "fake_name", "success": False, "hive": "HKLM", "key": FAKE_KEY, } self.assertDictEqual( reg.read_value(hive="HKLM", key=FAKE_KEY, vname="fake_name"), expected)
def test_read_value_non_existing_key(self): ''' Test the read_value function using a non existing registry key ''' expected = { 'comment': 'Cannot find key: HKLM\\{0}'.format(FAKE_KEY), 'vdata': None, 'vname': 'fake_name', 'success': False, 'hive': 'HKLM', 'key': FAKE_KEY } self.assertDictEqual( reg.read_value(hive='HKLM', key=FAKE_KEY, vname='fake_name'), expected)
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 test_read_value_non_existing(self): ''' Test the read_value function using a non existing value pair ''' expected = { 'comment': 'Cannot find fake_name in HKLM\\SOFTWARE\\Microsoft\\' 'Windows\\CurrentVersion', 'vdata': None, 'vname': 'fake_name', 'success': False, 'hive': 'HKLM', 'key': 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion' } self.assertDictEqual( reg.read_value(hive='HKLM', key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion', vname='fake_name'), expected)
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 test_set_value_default(self): """ Test the set_value function on the default value """ try: self.assertTrue( reg.set_value(hive="HKLM", key=FAKE_KEY, vdata="fake_default_data") ) expected = { "hive": "HKLM", "key": FAKE_KEY, "success": True, "vdata": "fake_default_data", "vname": "(Default)", "vtype": "REG_SZ", } self.assertEqual(reg.read_value(hive="HKLM", key=FAKE_KEY), expected) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_set_value_default(self): ''' Test the set_value function on the default value ''' try: self.assertTrue( reg.set_value(hive='HKLM', key=FAKE_KEY, vdata='fake_default_data')) expected = { 'hive': 'HKLM', 'key': FAKE_KEY, 'success': True, 'vdata': 'fake_default_data', 'vname': '(Default)', 'vtype': 'REG_SZ' } self.assertEqual(reg.read_value(hive='HKLM', key=FAKE_KEY), expected) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
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 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 test_set_value_unicode_value(self): ''' Test the set_value function on a unicode value ''' try: self.assertTrue( reg.set_value(hive='HKLM', key=FAKE_KEY, vname='fake_unicode', vdata=UNICODE_VALUE)) expected = { 'hive': 'HKLM', 'key': FAKE_KEY, 'success': True, 'vdata': UNICODE_VALUE, 'vname': 'fake_unicode', 'vtype': 'REG_SZ' } self.assertEqual( reg.read_value(hive='HKLM', key=FAKE_KEY, vname='fake_unicode'), expected) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
def test_set_value_reg_qword(self): ''' Test the set_value function on a REG_QWORD value ''' try: self.assertTrue( reg.set_value(hive='HKLM', key=FAKE_KEY, vname='qword_value', vdata=123, vtype='REG_QWORD')) expected = { 'hive': 'HKLM', 'key': FAKE_KEY, 'success': True, 'vdata': 123, 'vname': 'qword_value', 'vtype': 'REG_QWORD' } self.assertEqual( reg.read_value(hive='HKLM', key=FAKE_KEY, vname='qword_value'), expected) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)