summaryrefslogtreecommitdiff
path: root/libraries/registry_helper.rb
blob: 27304fa14552b68653cb7798583e99f98fe4c9c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#
# Author:: Doug MacEachern (<dougm@vmware.com>)
# Author:: Seth Chisamore (<schisamo@opscode.com>)
# Author:: Paul Morotn (<pmorton@biaprotect.com>)
# Cookbook Name:: windows
# Provider:: registry
#
# Copyright:: 2010, VMware, Inc.
# Copyright:: 2011, Opscode, Inc.
# Copyright:: 2011, Business Intelligence Associates, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

if RUBY_PLATFORM =~ /mswin|mingw32|windows/
  require 'win32/registry'
  require 'ruby-wmi'
end

module Windows
  module RegistryHelper

    @@native_registry_constant = ENV['PROCESSOR_ARCHITEW6432'] == 'AMD64' ? 0x0100 : 0x0200

    def get_hive_name(path)
      Chef::Log.debug("Resolving registry shortcuts to full names")

      reg_path = path.split("\\")
      hive_name = reg_path.shift

      hkey = {
        "HKLM" => "HKEY_LOCAL_MACHINE",
        "HKCU" => "HKEY_CURRENT_USER",
        "HKU"  => "HKEY_USERS"
      }[hive_name] || hive_name

      Chef::Log.debug("Hive resolved to #{hkey}")
      return hkey
    end

    def get_hive(path)

      Chef::Log.debug("Getting hive for #{path}")
      reg_path = path.split("\\")
      hive_name = reg_path.shift

      hkey = get_hive_name(path)

      hive = {
        "HKEY_LOCAL_MACHINE" => Win32::Registry::HKEY_LOCAL_MACHINE,
        "HKEY_USERS" => Win32::Registry::HKEY_USERS,
        "HKEY_CURRENT_USER" => Win32::Registry::HKEY_CURRENT_USER
        }[hkey]

      unless hive
        Chef::Application.fatal!("Unsupported registry hive '#{hive_name}'")
      end


      Chef::Log.debug("Registry hive resolved to #{hkey}")
      return hive
    end

    def unload_hive(path)
      hive = get_hive(path)
      if hive == Win32::Registry::HKEY_USERS
        reg_path = path.split("\\")
        priv = Chef::WindowsPrivileged.new
        begin
          priv.reg_unload_key(reg_path[1])
        rescue
        end
      end
    end

    def set_value(mode,path,values,type=nil)
      hive, reg_path, hive_name, root_key, hive_loaded = get_reg_path_info(path)
      key_name = reg_path.join("\\")

      Chef::Log.debug("Creating #{path})")

      if !key_exists?(path,true)
        create_key(path)
      end

      hive.send(mode, key_name, Win32::Registry::KEY_ALL_ACCESS | @@native_registry_constant) do |reg|
        values.each do |k,val|
          key = "#{k}" #wtf. avoid "can't modify frozen string" in win32/registry.rb
          cur_val = nil
          begin
            cur_val = reg[key]
          rescue
            #subkey does not exist (ok)
          end
          if cur_val != val
            Chef::Log.debug("setting #{key}=#{val}")
            if type.nil?
              reg[key] = val
            else
              reg[key, REG_BINARY] = val
            end

            ensure_hive_unloaded(hive_loaded)

            return true
          end
        end
      end
      return false
    end

    def get_value(path,value)
      hive, reg_path, hive_name, root_key, hive_loaded = get_reg_path_info(path)
      key = reg_path.join("\\")

      hive.open(key, Win32::Registry::KEY_ALL_ACCESS | @@native_registry_constant) do | reg |
        begin
          return reg[value]
        rescue
          return nil
        ensure
          ensure_hive_unloaded(hive_loaded)
        end
      end
    end

    def get_values(path)
      hive, reg_path, hive_name, root_key, hive_loaded = get_reg_path_info(path)
      key = reg_path.join("\\")
      hive.open(key, Win32::Registry::KEY_ALL_ACCESS | @@native_registry_constant) do | reg |
        values = []
        begin
        reg.each_value do |name, type, data|
          values << [name, type, data]
        end
        rescue
        ensure
          ensure_hive_unloaded(hive_loaded)
        end
        values
      end
    end

    def delete_value(path,values)
      hive, reg_path, hive_name, root_key, hive_loaded = get_reg_path_info(path)
      key = reg_path.join("\\")
      Chef::Log.debug("Deleting values in #{path}")
      hive.open(key, Win32::Registry::KEY_ALL_ACCESS | @@native_registry_constant) do | reg |
        values.each_key { |key|
          name = "#{key}"
          Chef::Log.debug("Deleting value #{name} in #{path}")
          reg.delete_value(name)
        }
      end

    end

    def create_key(path)
      hive, reg_path, hive_name, root_key, hive_loaded = get_reg_path_info(path)
      key = reg_path.join("\\")
      Chef::Log.debug("Creating registry key #{path}")
      hive.create(key)
    end

    def value_exists?(path,value)
      if key_exists?(path,true)

        hive, reg_path, hive_name, root_key , hive_loaded = get_reg_path_info(path)
        key = reg_path.join("\\")

        Chef::Log.debug("Attempting to open #{key}");
        Chef::Log.debug("Native Constant #{@@native_registry_constant}")
        Chef::Log.debug("Hive #{hive}")

        hive.open(key, Win32::Registry::KEY_READ | @@native_registry_constant) do | reg |
          begin 
            rtn_value = reg[value]
            return true
          rescue
            return false
          ensure 
            ensure_hive_unloaded(hive_loaded)
          end
        end

      end
      return false
    end

    # TODO: Does not load user registry...
    def key_exists?(path, load_hive = false)
      if load_hive
        hive, reg_path, hive_name, root_key , hive_loaded = get_reg_path_info(path)
        key = reg_path.join("\\")
      else
        hive = get_hive(path)
        reg_path = path.split("\\")
        hive_name = reg_path.shift
        root_key = reg_path[0]
        key = reg_path.join("\\")
        hive_loaded = false
      end

      begin
        hive.open(key, Win32::Registry::Constants::KEY_READ | @@native_registry_constant )
        return true
      rescue
        return false
      ensure 
        ensure_hive_unloaded(hive_loaded)
      end
    end

    def get_user_hive_location(sid)
      reg_key = "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\#{sid}"
      Chef::Log.debug("Looking for profile at #{reg_key}")
      if key_exists?(reg_key)
        return get_value(reg_key,'ProfileImagePath')
      else
        return nil
      end

    end

    def resolve_user_to_sid(username)
      begin 
        sid = WMI::Win32_UserAccount.find(:first, :conditions => {:name => username}).sid
        Chef::Log.debug("Resolved user SID to #{sid}")
        return sid
      rescue
        return nil
      end
    end

    def hive_loaded?(path)
      hive = get_hive(path)
      reg_path = path.split("\\")
      hive_name = reg_path.shift
      user_hive = path[0]

      if is_user_hive?(hive)
        return key_exists?("#{hive_name}\\#{user_hive}")
      else
        return true
      end
    end

    def is_user_hive?(hive)
      if hive == Win32::Registry::HKEY_USERS
        return true
      else
        return true
      end
    end

    def get_reg_path_info(path)
      hive = get_hive(path)
      reg_path = path.split("\\")
      hive_name = reg_path.shift
      root_key = reg_path[0]
      hive_loaded = false

      if is_user_hive?(hive) && !key_exists?("#{hive_name}\\#{root_key}")
        reg_path, hive_loaded = load_user_hive(hive,reg_path,root_key)
        root_key = reg_path[0]
        Chef::Log.debug("Resolved user (#{path}) to (#{reg_path.join('/')})")
      end

      return hive, reg_path, hive_name, root_key, hive_loaded
    end

    def load_user_hive(hive,reg_path,user_hive)
      Chef::Log.debug("Reg Path #{reg_path}")
      # See if the hive is loaded. Logged in users will have a key that is named their SID
      # if the user has specified the a path by SID and the user is logged in, this function 
      # should not be executed.
      if is_user_hive?(hive) && !key_exists?("HKU\\#{user_hive}")
        Chef::Log.debug("The user is not logged in and has not been specified by SID")
        sid = resolve_user_to_sid(user_hive)
        Chef::Log.debug("User SID resolved to (#{sid})")
        # Now that the user has been resolved to a SID, check and see if the hive exists.
        # If this exists by SID, the user is logged in and we should use that key.
        # TODO: Replace the username with the sid and send it back because the username
        # does not exist as the key location.
        load_reg = false
        if key_exists?("HKU\\#{sid}")
          reg_path[0] = sid #use the active profile (user is logged on)
          Chef::Log.debug("HKEY_USERS Mapped: #{user_hive} -> #{sid}")
        else
          Chef::Log.debug("User is not logged in")
          load_reg = true
        end

        # The user is not logged in, so we should load the registry from disk
        if load_reg
          profile_path = get_user_hive_location(sid)
          if profile_path != nil
            ntuser_dat = "#{profile_path}\\NTUSER.DAT"
            if ::File.exists?(ntuser_dat)
              priv = Chef::WindowsPrivileged.new
              if priv.reg_load_key(sid,ntuser_dat)
                Chef::Log.debug("RegLoadKey(#{sid}, #{user_hive}, #{ntuser_dat})")
                reg_path[0] = sid
              else
                Chef::Log.debug("Failed RegLoadKey(#{sid}, #{user_hive}, #{ntuser_dat})")
              end
            end
          end
        end
      end

      return reg_path, load_reg

    end

    private
    def ensure_hive_unloaded(hive_loaded=false)
      if(hive_loaded)
        Chef::Log.debug("Hive was loaded, we really should unload it")
        unload_hive(path)
      end
    end
  end
end

module Registry
  module_function
  extend Windows::RegistryHelper
end