# class Chef::Win32::Registry

### Attributes

architecture[RW]
run_context[RW]

### Public Class Methods

new(run_context=nil, user_architecture=:machine) click to toggle source

    # File lib/chef/win32/registry.rb, line 34
    def initialize(run_context=nil, user_architecture=:machine)
      @run_context = run_context
      self.architecture = user_architecture
    end

### Public Instance Methods

architecture=(user_architecture) click to toggle source

    # File lib/chef/win32/registry.rb, line 39
    def architecture=(user_architecture)
      @architecture = user_architecture.to_sym
      assert_architecture!
    end

create_key(key_path, recursive) click to toggle source

    # File lib/chef/win32/registry.rb, line 93
    def create_key(key_path, recursive)
      Chef::Log.debug("Creating registry key #{key_path}")
      if keys_missing?(key_path)
        if recursive == true
          Chef::Log.debug("Registry key #{key_path} has missing subkeys, and recursive specified, creating them....")
          create_missing(key_path)
        else
          raise Chef::Exceptions::Win32RegNoRecursive, "Registry key #{key_path} has missing subkeys, and recursive not specified"
        end
      end
      if key_exists?(key_path)
        Chef::Log.debug("Registry key #{key_path} already exists, doing nothing")
      else
        hive, key = get_hive_and_key(key_path)
        hive.create(key, ::Win32::Registry::KEY_WRITE | registry_system_architecture)
        Chef::Log.debug("Registry key #{key_path} created")
      end
      true
    end

data_exists!(key_path, value) click to toggle source

    # File lib/chef/win32/registry.rb, line 234
    def data_exists!(key_path, value)
      unless data_exists?(key_path, value)
        raise Chef::Exceptions::Win32RegDataMissing, "Registry key #{key_path} has no value named #{value[:name]}, containing type #{value[:type]} and data #{value[:data]}"
      end
      true
    end

data_exists?(key_path, value) click to toggle source

    # File lib/chef/win32/registry.rb, line 212
    def data_exists?(key_path, value)
      key_exists!(key_path)
      hive, key = get_hive_and_key(key_path)
      hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |reg|
        reg.each do |val_name, val_type, val_data|
          if val_name == value[:name] &&
            val_type == get_type_from_name(value[:type]) &&
            val_data == value[:data]
            return true
          end
        end
      end
      return false
    end

delete_key(key_path, recursive) click to toggle source

    # File lib/chef/win32/registry.rb, line 113
    def delete_key(key_path, recursive)
      Chef::Log.debug("Deleting registry key #{key_path}")
      unless key_exists?(key_path)
        Chef::Log.debug("Registry key #{key_path}, does not exist, not deleting")
        return true
      end
      #key_path is in the form "HKLM\Software\Opscode" for example, extracting
      #hive = HKLM,
      #hive_namespace = ::Win32::Registry::HKEY_LOCAL_MACHINE
      hive = key_path.split("\\").shift
      hive_namespace, key_including_parent = get_hive_and_key(key_path)
      if has_subkeys?(key_path)
        if recursive == true
          subkeys = get_subkeys(key_path)
          subkeys.each do |key|
            keypath_to_check = hive+"\\"+key_including_parent+"\\"+key
            Chef::Log.debug("Deleting registry key #{key_path} recursively")
            delete_key(keypath_to_check, true)
          end
          delete_key_ex(hive_namespace, key_including_parent)
        else
          raise Chef::Exceptions::Win32RegNoRecursive, "Registry key #{key_path} has subkeys, and recursive not specified"
        end
      else
        delete_key_ex(hive_namespace, key_including_parent)
        return true
      end
      true
    end

delete_key_ex(hive, key) click to toggle source

Using the ‘RegDeleteKeyEx’ Windows API that correctly supports WOW64 systems (Win2003) instead of the ‘RegDeleteKey’

    # File lib/chef/win32/registry.rb, line 145
    def delete_key_ex(hive, key)
      regDeleteKeyEx = ::Win32::API.new('RegDeleteKeyEx', 'LPLL', 'L', 'advapi32')
      hive_num = hive.hkey - (1 << 32)
      regDeleteKeyEx.call(hive_num, key, ::Win32::Registry::KEY_WRITE | registry_system_architecture, 0)
    end

delete_value(key_path, value) click to toggle source

    # File lib/chef/win32/registry.rb, line 75
    def delete_value(key_path, value)
      Chef::Log.debug("Deleting value #{value[:name]} from registry key #{key_path}")
      if value_exists?(key_path, value)
        begin
          hive, key = get_hive_and_key(key_path)
        rescue Chef::Exceptions::Win32RegKeyMissing
          return true
        end
        hive.open(key, ::Win32::Registry::KEY_SET_VALUE | registry_system_architecture) do |reg|
          reg.delete_value(value[:name])
          Chef::Log.debug("Deleted value #{value[:name]} from registry key #{key_path}")
        end
      else
        Chef::Log.debug("Value #{value[:name]} in registry key #{key_path} does not exist, not updated")
      end
      true
    end

get_name_from_type(val_class) click to toggle source

    # File lib/chef/win32/registry.rb, line 287
    def get_name_from_type(val_class)
      _name_type_map[val_class]
    end

get_subkeys(key_path) click to toggle source

    # File lib/chef/win32/registry.rb, line 187
    def get_subkeys(key_path)
      subkeys = []
      key_exists!(key_path)
      hive, key = get_hive_and_key(key_path)
      hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |reg|
        reg.each_key{ |current_key| subkeys << current_key }
      end
      return subkeys
    end

get_type_from_name(val_type) click to toggle source

    # File lib/chef/win32/registry.rb, line 263
    def get_type_from_name(val_type)
      value = {
        :binary => ::Win32::Registry::REG_BINARY,
        :string => ::Win32::Registry::REG_SZ,
        :multi_string => ::Win32::Registry::REG_MULTI_SZ,
        :expand_string => ::Win32::Registry::REG_EXPAND_SZ,
        :dword => ::Win32::Registry::REG_DWORD,
        :dword_big_endian => ::Win32::Registry::REG_DWORD_BIG_ENDIAN,
        :qword => ::Win32::Registry::REG_QWORD
      }[val_type]
      return value
    end

get_values(key_path) click to toggle source

    # File lib/chef/win32/registry.rb, line 44
    def get_values(key_path)
      hive, key = get_hive_and_key(key_path)
      key_exists!(key_path)
      values = hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |reg|
        reg.map { |name, type, data| {:name=>name, :type=>get_name_from_type(type), :data=>data} }
      end
    end

has_subkeys?(key_path) click to toggle source

    # File lib/chef/win32/registry.rb, line 178
    def has_subkeys?(key_path)
      key_exists!(key_path)
      hive, key = get_hive_and_key(key_path)
      hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |reg|
        reg.each_key{ |key| return true }
      end
      return false
    end

hive_exists?(key_path) click to toggle source

    # File lib/chef/win32/registry.rb, line 169
    def hive_exists?(key_path)
      begin
        hive, key = get_hive_and_key(key_path)
      rescue Chef::Exceptions::Win32RegHiveMissing => e
        return false
      end
      return true
    end

key_exists!(key_path) click to toggle source

    # File lib/chef/win32/registry.rb, line 162
    def key_exists!(key_path)
      unless key_exists?(key_path)
        raise Chef::Exceptions::Win32RegKeyMissing, "Registry key #{key_path} does not exist"
      end
      true
    end

key_exists?(key_path) click to toggle source

    # File lib/chef/win32/registry.rb, line 151
    def key_exists?(key_path)
      hive, key = get_hive_and_key(key_path)
      begin
        hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |current_key|
          return true
        end
      rescue ::Win32::Registry::Error => e
        return false
      end
    end

keys_missing?(key_path) click to toggle source

    # File lib/chef/win32/registry.rb, line 276
    def keys_missing?(key_path)
      missing_key_arr = key_path.split("\\")
      missing_key_arr.pop
      key = missing_key_arr.join("\\")
      !key_exists?(key)
    end

registry_system_architecture() click to toggle source

32-bit chef clients running on 64-bit machines will default to reading the 64-bit registry

    # File lib/chef/win32/registry.rb, line 198
    def registry_system_architecture
      applied_arch = ( architecture == :machine ) ? machine_architecture : architecture
      ( applied_arch == :x86_64 ) ? 0x0100 : 0x0200
    end

set_value(key_path, value) click to toggle source

    # File lib/chef/win32/registry.rb, line 52
    def set_value(key_path, value)
      Chef::Log.debug("Updating value #{value[:name]} in registry key #{key_path} with type #{value[:type]} and data #{value[:data]}")
      key_exists!(key_path)
      hive, key = get_hive_and_key(key_path)
      if value_exists?(key_path, value)
        if data_exists?(key_path, value)
          Chef::Log.debug("Value #{value[:name]} in registry key #{key_path} already had those values, not updated")
          return false
        else
          hive.open(key, ::Win32::Registry::KEY_SET_VALUE | ::Win32::Registry::KEY_QUERY_VALUE | registry_system_architecture) do |reg|
            reg.write(value[:name], get_type_from_name(value[:type]), value[:data])
          end
          Chef::Log.debug("Value #{value[:name]} in registry key #{key_path} updated")
        end
      else
        hive.open(key, ::Win32::Registry::KEY_SET_VALUE | ::Win32::Registry::KEY_QUERY_VALUE | registry_system_architecture) do |reg|
          reg.write(value[:name], get_type_from_name(value[:type]), value[:data])
        end
        Chef::Log.debug("Value #{value[:name]} in registry key #{key_path} created")
      end
      true
    end

type_matches!(key_path, value) click to toggle source

    # File lib/chef/win32/registry.rb, line 257
    def type_matches!(key_path, value)
      unless type_matches?(key_path, value)
        raise Chef::Exceptions::Win32RegTypesMismatch, "Registry key #{key_path} has a value #{value[:name]} with a type that is not #{value[:type]}"
      end
    end

type_matches?(key_path, value) click to toggle source

    # File lib/chef/win32/registry.rb, line 241
    def type_matches?(key_path, value)
      value_exists!(key_path, value)
      hive, key = get_hive_and_key(key_path)
      hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |reg|
        reg.each do |val_name, val_type|
          if val_name == value[:name]
            type_new = get_type_from_name(value[:type])
            if val_type == type_new
              return true
            end
          end
        end
      end
      return false
    end

value_exists!(key_path, value) click to toggle source

    # File lib/chef/win32/registry.rb, line 227
    def value_exists!(key_path, value)
      unless value_exists?(key_path, value)
        raise Chef::Exceptions::Win32RegValueMissing, "Registry key #{key_path} has no value named #{value[:name]}"
      end
      true
    end

value_exists?(key_path, value) click to toggle source

    # File lib/chef/win32/registry.rb, line 203
    def value_exists?(key_path, value)
      key_exists!(key_path)
      hive, key = get_hive_and_key(key_path)
      hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |reg|
        return true if reg.any? {|val| val == value[:name] }
      end
      return false
    end