puppet 3.0.1 → 3.0.2.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of puppet might be problematic. Click here for more details.

Files changed (97) hide show
  1. data/Gemfile +17 -3
  2. data/Rakefile +7 -5
  3. data/ext/build_defaults.yaml +1 -1
  4. data/ext/debian/puppet.init +33 -34
  5. data/ext/debian/puppet.logrotate +2 -1
  6. data/ext/debian/puppetmaster.init +1 -2
  7. data/ext/suse/client.init +1 -1
  8. data/ext/suse/puppet.spec +3 -0
  9. data/ext/suse/server.init +1 -1
  10. data/ext/windows/service/daemon.rb +1 -1
  11. data/install.rb +32 -53
  12. data/lib/hiera/backend/puppet_backend.rb +6 -5
  13. data/lib/puppet/agent.rb +3 -3
  14. data/lib/puppet/application/agent.rb +1 -2
  15. data/lib/puppet/application/cert.rb +4 -5
  16. data/lib/puppet/application/kick.rb +3 -0
  17. data/lib/puppet/defaults.rb +15 -2
  18. data/lib/puppet/indirector/exec.rb +1 -1
  19. data/lib/puppet/module_tool/skeleton/templates/generator/manifests/init.pp.erb +1 -1
  20. data/lib/puppet/parser/functions/hiera_include.rb +6 -4
  21. data/lib/puppet/parser/lexer.rb +38 -1
  22. data/lib/puppet/parser/relationship.rb +3 -1
  23. data/lib/puppet/provider.rb +1 -1
  24. data/lib/puppet/provider/augeas/augeas.rb +1 -1
  25. data/lib/puppet/provider/file/windows.rb +10 -29
  26. data/lib/puppet/provider/group/ldap.rb +1 -1
  27. data/lib/puppet/provider/group/windows_adsi.rb +1 -1
  28. data/lib/puppet/provider/ldap.rb +5 -1
  29. data/lib/puppet/provider/macauthorization/macauthorization.rb +2 -6
  30. data/lib/puppet/provider/package/dpkg.rb +8 -12
  31. data/lib/puppet/provider/package/macports.rb +2 -2
  32. data/lib/puppet/provider/package/msi.rb +2 -2
  33. data/lib/puppet/provider/package/sun.rb +1 -1
  34. data/lib/puppet/provider/package/windows.rb +2 -2
  35. data/lib/puppet/provider/package/yum.rb +5 -1
  36. data/lib/puppet/provider/scheduled_task/win32_taskscheduler.rb +2 -2
  37. data/lib/puppet/provider/service/freebsd.rb +1 -1
  38. data/lib/puppet/provider/user/directoryservice.rb +603 -65
  39. data/lib/puppet/provider/user/windows_adsi.rb +1 -1
  40. data/lib/puppet/provider/zpool/zpool.rb +1 -1
  41. data/lib/puppet/run.rb +2 -1
  42. data/lib/puppet/settings.rb +9 -5
  43. data/lib/puppet/ssl/certificate_authority.rb +2 -0
  44. data/lib/puppet/transaction.rb +1 -1
  45. data/lib/puppet/type/cron.rb +4 -4
  46. data/lib/puppet/type/exec.rb +10 -5
  47. data/lib/puppet/type/file.rb +1 -1
  48. data/lib/puppet/type/service.rb +3 -2
  49. data/lib/puppet/type/user.rb +24 -2
  50. data/lib/puppet/util.rb +3 -6
  51. data/lib/puppet/util/adsi.rb +3 -9
  52. data/lib/puppet/util/diff.rb +1 -1
  53. data/lib/puppet/util/execution.rb +13 -6
  54. data/lib/puppet/util/feature.rb +3 -1
  55. data/lib/puppet/util/log/destinations.rb +12 -16
  56. data/lib/puppet/util/selinux.rb +18 -2
  57. data/lib/puppet/util/windows.rb +1 -0
  58. data/lib/puppet/util/windows/security.rb +2 -39
  59. data/lib/puppet/util/windows/sid.rb +96 -0
  60. data/lib/puppet/version.rb +1 -1
  61. data/spec/integration/util/windows/security_spec.rb +3 -23
  62. data/spec/unit/agent_spec.rb +7 -3
  63. data/spec/unit/application/agent_spec.rb +13 -5
  64. data/spec/unit/daemon_spec.rb +2 -1
  65. data/spec/unit/hiera/backend/puppet_backend_spec.rb +49 -42
  66. data/spec/unit/indirector/exec_spec.rb +8 -6
  67. data/spec/unit/parser/functions/hiera_include_spec.rb +11 -4
  68. data/spec/unit/parser/lexer_spec.rb +120 -8
  69. data/spec/unit/parser/relationship_spec.rb +24 -0
  70. data/spec/unit/provider/file/windows_spec.rb +29 -29
  71. data/spec/unit/provider/group/windows_adsi_spec.rb +2 -2
  72. data/spec/unit/provider/nameservice/directoryservice_spec.rb +1 -1
  73. data/spec/unit/provider/package/dpkg_spec.rb +2 -2
  74. data/spec/unit/provider/package/macports_spec.rb +6 -5
  75. data/spec/unit/provider/package/msi_spec.rb +1 -1
  76. data/spec/unit/provider/package/pacman_spec.rb +1 -1
  77. data/spec/unit/provider/package/rpm_spec.rb +1 -1
  78. data/spec/unit/provider/package/sun_spec.rb +4 -4
  79. data/spec/unit/provider/package/windows_spec.rb +1 -1
  80. data/spec/unit/provider/scheduled_task/win32_taskscheduler_spec.rb +14 -11
  81. data/spec/unit/provider/user/directoryservice_spec.rb +943 -0
  82. data/spec/unit/provider/user/ldap_spec.rb +22 -8
  83. data/spec/unit/provider/user/windows_adsi_spec.rb +4 -4
  84. data/spec/unit/provider_spec.rb +1 -1
  85. data/spec/unit/run_spec.rb +1 -1
  86. data/spec/unit/settings_spec.rb +16 -0
  87. data/spec/unit/ssl/certificate_authority_spec.rb +24 -0
  88. data/spec/unit/util/adsi_spec.rb +4 -8
  89. data/spec/unit/util/diff_spec.rb +2 -2
  90. data/spec/unit/util/execution_spec.rb +78 -20
  91. data/spec/unit/util/feature_spec.rb +12 -1
  92. data/spec/unit/util/selinux_spec.rb +20 -0
  93. data/spec/unit/util/windows/sid_spec.rb +100 -0
  94. data/spec/unit/util_spec.rb +17 -0
  95. metadata +71 -48
  96. data/Gemfile.lock +0 -44
  97. data/lib/puppet/provider/interface/base.rb +0 -0
@@ -125,7 +125,7 @@ Puppet::Type.type(:scheduled_task).provide(:win32_taskscheduler) do
125
125
 
126
126
  # By comparing account SIDs we don't have to worry about case
127
127
  # sensitivity, or canonicalization of the account name.
128
- Puppet::Util::ADSI.sid_for_account(current) == Puppet::Util::ADSI.sid_for_account(should[0])
128
+ Puppet::Util::Windows::Security.name_to_sid(current) == Puppet::Util::Windows::Security.name_to_sid(should[0])
129
129
  end
130
130
 
131
131
  def trigger_insync?(current, should)
@@ -202,7 +202,7 @@ Puppet::Type.type(:scheduled_task).provide(:win32_taskscheduler) do
202
202
  end
203
203
 
204
204
  def user=(value)
205
- self.fail("Invalid user: #{value}") unless Puppet::Util::ADSI.sid_for_account(value)
205
+ self.fail("Invalid user: #{value}") unless Puppet::Util::Windows::Security.name_to_sid(value)
206
206
 
207
207
  if value.to_s.downcase != 'system'
208
208
  task.set_account_information(value, resource[:password])
@@ -16,7 +16,7 @@ Puppet::Type.type(:service).provide :freebsd, :parent => :init do
16
16
  # Executing an init script with the 'rcvar' argument returns
17
17
  # the service name, rcvar name and whether it's enabled/disabled
18
18
  def rcvar
19
- rcvar = execute([self.initscript, :rcvar], :failonfail => true, :squelch => false)
19
+ rcvar = execute([self.initscript, :rcvar], :failonfail => true, :combine => false, :squelch => false)
20
20
  rcvar = rcvar.split("\n")
21
21
  rcvar.delete_if {|str| str =~ /^#\s*$/}
22
22
  rcvar[1] = rcvar[1].gsub(/^\$/, '')
@@ -1,86 +1,624 @@
1
- require 'puppet/provider/nameservice/directoryservice'
1
+ require 'puppet'
2
+ require 'facter/util/plist'
3
+ require 'base64'
2
4
 
3
- Puppet::Type.type(:user).provide :directoryservice, :parent => Puppet::Provider::NameService::DirectoryService do
4
- desc "User management using DirectoryService on OS X."
5
+ Puppet::Type.type(:user).provide :directoryservice do
6
+ desc "User management on OS X."
5
7
 
6
- commands :dscl => "/usr/bin/dscl"
7
- confine :operatingsystem => :darwin
8
+ ## ##
9
+ ## Provider Settings ##
10
+ ## ##
11
+
12
+ # Provider command declarations
13
+ commands :uuidgen => '/usr/bin/uuidgen'
14
+ commands :dsimport => '/usr/bin/dsimport'
15
+ commands :dscl => '/usr/bin/dscl'
16
+ commands :plutil => '/usr/bin/plutil'
17
+ commands :dscacheutil => '/usr/bin/dscacheutil'
18
+
19
+ # Provider confines and defaults
20
+ confine :operatingsystem => :darwin
8
21
  defaultfor :operatingsystem => :darwin
9
22
 
10
- # JJM: DirectoryService can manage passwords.
11
- # This needs to be a special option to dscl though (-passwd)
23
+ # Need this to create getter/setter methods automagically
24
+ # This command creates methods that return @property_hash[:value]
25
+ mk_resource_methods
26
+
27
+ # JJM: OS X can manage passwords.
12
28
  has_feature :manages_passwords
13
29
 
14
- # JJM: comment matches up with the /etc/passwd concept of an user
15
- options :comment, :key => "realname"
16
- options :password, :key => "passwd"
30
+ # 10.8 Passwords use a PBKDF2 salt value
31
+ has_features :manages_password_salt
17
32
 
18
- autogen_defaults :home => "/var/empty", :shell => "/usr/bin/false"
33
+ ## ##
34
+ ## Class Methods ##
35
+ ## ##
19
36
 
20
- verify :gid, "GID must be an integer" do |value|
21
- value.is_a? Integer
37
+ def self.ds_to_ns_attribute_map
38
+ # This method exists to map the dscl values to the correct Puppet
39
+ # properties. This stays relatively consistent, but who knows what
40
+ # Apple will do next year...
41
+ {
42
+ 'RecordName' => :name,
43
+ 'PrimaryGroupID' => :gid,
44
+ 'NFSHomeDirectory' => :home,
45
+ 'UserShell' => :shell,
46
+ 'UniqueID' => :uid,
47
+ 'RealName' => :comment,
48
+ 'Password' => :password,
49
+ 'GeneratedUID' => :guid,
50
+ 'IPAddress' => :ip_address,
51
+ 'ENetAddress' => :en_address,
52
+ 'GroupMembership' => :members,
53
+ }
22
54
  end
23
55
 
24
- verify :uid, "UID must be an integer" do |value|
25
- value.is_a? Integer
56
+ def self.ns_to_ds_attribute_map
57
+ @ns_to_ds_attribute_map ||= ds_to_ns_attribute_map.invert
26
58
  end
27
59
 
28
- def autogen_comment
29
- @resource[:name].capitalize
60
+ def self.prefetch(resources)
61
+ # Prefetching is necessary to use @property_hash inside any setter methods.
62
+ # self.prefetch uses self.instances to gather an array of user instances
63
+ # on the system, and then populates the @property_hash instance variable
64
+ # with attribute data for the specific instance in question (i.e. it
65
+ # gathers the 'is' values of the resource into the @property_hash instance
66
+ # variable so you don't have to read from the system every time you need
67
+ # to gather the 'is' values for a resource. The downside here is that
68
+ # populating this instance variable for every resource on the system
69
+ # takes time and front-loads your Puppet run.
70
+ instances.each do |prov|
71
+ if resource = resources[prov.name]
72
+ resource.provider = prov
73
+ end
74
+ end
30
75
  end
31
76
 
32
- # The list of all groups the user is a member of.
33
- # JJM: FIXME: Override this method...
34
- def groups
35
- groups = []
36
- groups.join(",")
77
+ def self.instances
78
+ # This method assembles an array of provider instances containing
79
+ # information about every instance of the user type on the system (i.e.
80
+ # every user and its attributes). The `puppet resource` command relies
81
+ # on self.instances to gather an array of user instances in order to
82
+ # display its output.
83
+ get_all_users.collect do |user|
84
+ self.new(generate_attribute_hash(user))
85
+ end
37
86
  end
38
87
 
39
- # This is really lame. We have to iterate over each
40
- # of the groups and add us to them.
41
- def groups=(groups)
42
- # case groups
43
- # when Fixnum
44
- # groups = [groups.to_s]
45
- # when String
46
- # groups = groups.split(/\s*,\s*/)
47
- # else
48
- # raise Puppet::DevError, "got invalid groups value #{groups.class} of type #{groups}"
49
- # end
50
- # # Get just the groups we need to modify
51
- # diff = groups - (@is || [])
52
- #
53
- # data = {}
54
- # open("| #{command(:nireport)} / /groups name users") do |file|
55
- # file.each do |line|
56
- # name, members = line.split(/\s+/)
57
- #
58
- # if members.nil? or members =~ /NoValue/
59
- # data[name] = []
60
- # else
61
- # # Add each diff group's current members
62
- # data[name] = members.split(/,/)
63
- # end
64
- # end
65
- # end
88
+ def self.get_all_users
89
+ # Return an array of hashes containing information about every user on
90
+ # the system.
91
+ Plist.parse_xml(dscl '-plist', '.', 'readall', '/Users')
92
+ end
93
+
94
+ def self.generate_attribute_hash(input_hash)
95
+ # This method accepts an individual user plist, passed as a hash, and
96
+ # strips the dsAttrTypeStandard: prefix that dscl adds for each key.
97
+ # An attribute hash is assembled and returned from the properties
98
+ # supported by the user type.
99
+ attribute_hash = {}
100
+ input_hash.keys.each do |key|
101
+ ds_attribute = key.sub("dsAttrTypeStandard:", "")
102
+ next unless ds_to_ns_attribute_map.keys.include?(ds_attribute)
103
+ ds_value = input_hash[key]
104
+ case ds_to_ns_attribute_map[ds_attribute]
105
+ when :gid, :uid
106
+ # OS X stores objects like uid/gid as strings.
107
+ # Try casting to an integer for these cases to be
108
+ # consistent with the other providers and the group type
109
+ # validation
110
+ begin
111
+ ds_value = Integer(ds_value[0])
112
+ rescue ArgumentError
113
+ ds_value = ds_value[0]
114
+ end
115
+ else ds_value = ds_value[0]
116
+ end
117
+ attribute_hash[ds_to_ns_attribute_map[ds_attribute]] = ds_value
118
+ end
119
+ attribute_hash[:ensure] = :present
120
+ attribute_hash[:provider] = :directoryservice
121
+ attribute_hash[:shadowhashdata] = get_attribute_from_dscl('Users', attribute_hash[:name], 'ShadowHashData')
122
+
123
+ ##############
124
+ # Get Groups #
125
+ ##############
126
+ groups_array = []
127
+ get_list_of_groups.each do |group|
128
+ if group["dsAttrTypeStandard:GroupMembership"] and group["dsAttrTypeStandard:GroupMembership"].include?(attribute_hash[:name])
129
+ groups_array << group["dsAttrTypeStandard:RecordName"][0]
130
+ end
131
+
132
+ if group["dsAttrTypeStandard:GroupMembers"] and group["dsAttrTypeStandard:GroupMembers"].include?(attribute_hash[:guid])
133
+ groups_array << group["dsAttrTypeStandard:RecordName"][0]
134
+ end
135
+ end
136
+ attribute_hash[:groups] = groups_array.uniq.sort.join(',')
137
+
138
+ ################################
139
+ # Get Password/Salt/Iterations #
140
+ ################################
141
+ if (Puppet::Util::Package.versioncmp(Facter.value(:macosx_productversion_major), '10.7') == -1)
142
+ attribute_hash[:password] = get_sha1(attribute_hash[:guid])
143
+ else
144
+ if attribute_hash[:shadowhashdata].empty?
145
+ attribute_hash[:password] = '*'
146
+ else
147
+ embedded_binary_plist = get_embedded_binary_plist(attribute_hash[:shadowhashdata])
148
+ if embedded_binary_plist['SALTED-SHA512']
149
+ attribute_hash[:password] = get_salted_sha512(embedded_binary_plist)
150
+ else
151
+ attribute_hash[:password] = get_salted_sha512_pbkdf2('entropy', embedded_binary_plist)
152
+ attribute_hash[:salt] = get_salted_sha512_pbkdf2('salt', embedded_binary_plist)
153
+ attribute_hash[:iterations] = get_salted_sha512_pbkdf2('iterations', embedded_binary_plist)
154
+ end
155
+ end
156
+ end
157
+
158
+ attribute_hash
159
+ end
160
+
161
+ def self.get_list_of_groups
162
+ # Use dscl to retrieve an array of hashes containing attributes about all
163
+ # of the local groups on the machine.
164
+ @groups ||= Plist.parse_xml(dscl '-plist', '.', 'readall', '/Groups')
165
+ end
166
+
167
+ def self.get_attribute_from_dscl(path, username, keyname)
168
+ # Perform a dscl lookup at the path specified for the specific keyname
169
+ # value. The value returned is the first item within the array returned
170
+ # from dscl
171
+ Plist.parse_xml(dscl '-plist', '.', 'read', "/#{path}/#{username}", keyname)
172
+ end
173
+
174
+ def self.get_embedded_binary_plist(shadow_hash_data)
175
+ # The plist embedded in the ShadowHashData key is a binary plist. The
176
+ # facter/util/plist library doesn't read binary plists, so we need to
177
+ # extract the binary plist, convert it to XML, and return it.
178
+ embedded_binary_plist = Array(shadow_hash_data['dsAttrTypeNative:ShadowHashData'][0].delete(' ')).pack('H*')
179
+ convert_binary_to_xml(embedded_binary_plist)
180
+ end
181
+
182
+ def self.convert_xml_to_binary(plist_data)
183
+ # This method will accept a hash that has been returned from Plist::parse_xml
184
+ # and convert it to a binary plist (string value).
185
+ Puppet.debug('Converting XML plist to binary')
186
+ Puppet.debug('Executing: \'plutil -convert binary1 -o - -\'')
187
+ IO.popen('plutil -convert binary1 -o - -', mode='r+') do |io|
188
+ io.write Plist::Emit.dump(plist_data)
189
+ io.close_write
190
+ @converted_plist = io.read
191
+ end
192
+ @converted_plist
193
+ end
194
+
195
+ def self.convert_binary_to_xml(plist_data)
196
+ # This method will accept a binary plist (as a string) and convert it to a
197
+ # hash via Plist::parse_xml.
198
+ Puppet.debug('Converting binary plist to XML')
199
+ Puppet.debug('Executing: \'plutil -convert xml1 -o - -\'')
200
+ IO.popen('plutil -convert xml1 -o - -', mode='r+') do |io|
201
+ io.write plist_data
202
+ io.close_write
203
+ @converted_plist = io.read
204
+ end
205
+ Puppet.debug('Converting XML values to a hash.')
206
+ Plist::parse_xml(@converted_plist)
207
+ end
208
+
209
+ def self.get_salted_sha512(embedded_binary_plist)
210
+ # The salted-SHA512 password hash in 10.7 is stored in the 'SALTED-SHA512'
211
+ # key as binary data. That data is extracted and converted to a hex string.
212
+ embedded_binary_plist['SALTED-SHA512'].string.unpack("H*")[0]
213
+ end
214
+
215
+ def self.get_salted_sha512_pbkdf2(field, embedded_binary_plist)
216
+ # This method reads the passed embedded_binary_plist hash and returns values
217
+ # according to which field is passed. Arguments passed are the hash
218
+ # containing the value read from the 'ShadowHashData' key in the User's
219
+ # plist, and the field to be read (one of 'entropy', 'salt', or 'iterations')
220
+ case field
221
+ when 'salt', 'entropy'
222
+ embedded_binary_plist['SALTED-SHA512-PBKDF2'][field].string.unpack('H*').first
223
+ when 'iterations'
224
+ Integer(embedded_binary_plist['SALTED-SHA512-PBKDF2'][field])
225
+ else
226
+ raise Puppet::Error, 'Puppet has tried to read an incorrect value from the ' +
227
+ "SALTED-SHA512-PBKDF2 hash. Acceptable fields are 'salt', " +
228
+ "'entropy', or 'iterations'."
229
+ end
230
+ end
231
+
232
+ def self.get_sha1(guid)
233
+ # In versions 10.5 and 10.6 of OS X, the password hash is stored in a file
234
+ # in the /var/db/shadow/hash directory that matches the GUID of the user.
235
+ password_hash = nil
236
+ password_hash_file = "#{password_hash_dir}/#{guid}"
237
+ if File.exists?(password_hash_file) and File.file?(password_hash_file)
238
+ raise Puppet::Error, "Could not read password hash file at #{password_hash_file}" if not File.readable?(password_hash_file)
239
+ f = File.new(password_hash_file)
240
+ password_hash = f.read
241
+ f.close
242
+ end
243
+ password_hash
244
+ end
245
+
246
+
247
+ ## ##
248
+ ## Ensurable Methods ##
249
+ ## ##
250
+
251
+ def exists?
252
+ begin
253
+ dscl '.', 'read', "/Users/#{@resource.name}"
254
+ rescue Puppet::ExecutionFailure => e
255
+ Puppet.debug("User was not found, dscl returned: #{e.inspect}")
256
+ return false
257
+ end
258
+ true
259
+ end
260
+
261
+ def create
262
+ # This method is called if ensure => present is passed and the exists?
263
+ # method returns false. Dscl will directly set most values, but the
264
+ # setter methods will be used for any exceptions.
265
+ create_new_user(@resource.name)
266
+
267
+ # Retrieve the user's GUID
268
+ @guid = self.class.get_attribute_from_dscl('Users', @resource.name, 'GeneratedUID')['dsAttrTypeStandard:GeneratedUID'][0]
269
+
270
+ # Get an array of valid User type properties
271
+ valid_properties = Puppet::Type.type('User').validproperties
272
+
273
+ # Iterate through valid User type properties
274
+ valid_properties.each do |attribute|
275
+ next if attribute == :ensure
276
+ value = @resource.should(attribute)
277
+
278
+ # Value defaults
279
+ if value.nil?
280
+ value = case attribute
281
+ when :gid
282
+ '20'
283
+ when :uid
284
+ next_system_id
285
+ when :comment
286
+ @resource.name
287
+ when :shell
288
+ '/bin/bash'
289
+ when :home
290
+ "/Users/#{@resource.name}"
291
+ else
292
+ nil
293
+ end
294
+ end
295
+
296
+ # If a non-numerical gid value is passed, assume it is a group name and
297
+ # lookup that group's GID value to use when setting the GID
298
+ if (attribute == :gid) and value.class == 'Fixnum'
299
+ value = self.class.get_attribute_from_dscl('Groups', value, 'PrimaryGroupID')['dsAttrTypeStandard:PrimaryGroupID'][0]
300
+ end
301
+
302
+ ## Set values ##
303
+ # For the :password and :groups properties, call the setter methods
304
+ # to enforce those values. For everything else, use dscl with the
305
+ # ns_to_ds_attribute_map to set the appropriate values.
306
+ if value != "" and not value.nil?
307
+ case attribute
308
+ when :password
309
+ self.password = value
310
+ when :iterations
311
+ self.iterations = value
312
+ when :salt
313
+ self.salt = value
314
+ when :groups
315
+ value.split(',').each do |group|
316
+ merge_attribute_with_dscl('Groups', group, 'GroupMembership', @resource.name)
317
+ merge_attribute_with_dscl('Groups', group, 'GroupMembers', @guid)
318
+ end
319
+ else
320
+ merge_attribute_with_dscl('Users', @resource.name, self.class.ns_to_ds_attribute_map[attribute], value)
321
+ end
322
+ end
323
+ end
324
+ end
325
+
326
+ def delete
327
+ # This method is called when ensure => absent has been set.
328
+ # Deleting a user is handled by dscl
329
+ dscl '.', '-delete', "/Users/#{@resource.name}"
330
+ end
331
+
332
+ ## ##
333
+ ## Getter/Setter Methods ##
334
+ ## ##
335
+
336
+ def groups=(value)
337
+ # In the setter method we're only going to take action on groups for which
338
+ # the user is not currently a member.
339
+ guid = self.class.get_attribute_from_dscl('Users', @resource.name, 'GeneratedUID')['dsAttrTypeStandard:GeneratedUID'][0]
340
+ groups_to_add = value.split(',') - groups.split(',')
341
+ groups_to_add.each do |group|
342
+ merge_attribute_with_dscl('Groups', group, 'GroupMembership', @resource.name)
343
+ merge_attribute_with_dscl('Groups', group, 'GroupMembers', guid)
344
+ end
345
+ end
346
+
347
+ def password=(value)
348
+ # If you thought GETTING a password was bad, try SETTING it. This method
349
+ # makes me want to cry. A thousand tears...
66
350
  #
67
- # user = @resource[:name]
68
- # data.each do |name, members|
69
- # if members.include? user and groups.include? name
70
- # # I'm in the group and should be
71
- # next
72
- # elsif members.include? user
73
- # # I'm in the group and shouldn't be
74
- # setuserlist(name, members - [user])
75
- # elsif groups.include? name
76
- # # I'm not in the group and should be
77
- # setuserlist(name, members + [user])
78
- # else
79
- # # I'm not in the group and shouldn't be
80
- # next
81
- # end
82
- # end
351
+ # I've been unsuccessful in tracking down a way to set the password for
352
+ # a user using dscl that DOESN'T require passing it as plaintext. We were
353
+ # also unable to get dsimport to work like this. Due to these downfalls,
354
+ # the sanest method requires opening the user's plist, dropping in the
355
+ # password hash, and serializing it back to disk. The problems with THIS
356
+ # method revolve around dscl. Any time you directly modify a user's plist,
357
+ # you need to flush the cache that dscl maintains.
358
+ if (Puppet::Util::Package.versioncmp(Facter.value(:macosx_productversion_major), '10.7') == -1)
359
+ write_sha1_hash(value)
360
+ else
361
+ if Facter.value(:macosx_productversion_major) == '10.7'
362
+ if value.length != 136
363
+ raise Puppet::Error, "OS X 10.7 requires a Salted SHA512 hash password of 136 characters. Please check your password and try again."
364
+ end
365
+ else
366
+ if value.length != 256
367
+ raise Puppet::Error, "OS X versions > 10.7 require a Salted SHA512 PBKDF2 password hash of 256 characters. Please check your password and try again."
368
+ end
369
+ end
370
+
371
+ # Methods around setting the password on OS X are the ONLY methods that
372
+ # cannot use dscl (because the only way to set it via dscl is by passing
373
+ # a plaintext password - which is bad). Because of this, we have to change
374
+ # the user's plist directly. DSCL has its own caching mechanism, which
375
+ # means that every time we call dscl in this provider we're not directly
376
+ # changing values on disk (instead, those calls are cached and written
377
+ # to disk according to Apple's prioritization algorithms). When Puppet
378
+ # needs to set the password property on OS X > 10.6, the provider has to
379
+ # tell dscl to write its cache to disk before modifying the user's
380
+ # plist. The 'dscacheutil -flushcache' command does this. Another issue
381
+ # is how fast Puppet makes calls to dscl and how long it takes dscl to
382
+ # enter those calls into its cache. We have to sleep for 2 seconds before
383
+ # flushing the dscl cache to allow all dscl calls to get INTO the cache
384
+ # first. This could be made faster (and avoid a sleep call) by finding
385
+ # a way to enter calls into the dscl cache faster. A sleep time of 1
386
+ # second would intermittantly require a second Puppet run to set
387
+ # properties, so 2 seconds seems to be the minimum working value.
388
+ sleep 2
389
+ flush_dscl_cache
390
+ write_password_to_users_plist(value)
391
+
392
+ # Since we just modified the user's plist, we need to flush the ds cache
393
+ # again so dscl can pick up on the changes we made.
394
+ flush_dscl_cache
395
+ end
396
+ end
397
+
398
+ def iterations=(value)
399
+ # The iterations and salt properties, like the password property, can only
400
+ # be modified by directly changing the user's plist. Because of this fact,
401
+ # we have to treat the ds cache just like you would in the password=
402
+ # method.
403
+ if (Puppet::Util::Package.versioncmp(Facter.value(:macosx_productversion_major), '10.7') > 0)
404
+ sleep 2
405
+ flush_dscl_cache
406
+ users_plist = Plist::parse_xml(plutil '-convert', 'xml1', '-o', '/dev/stdout', "#{users_plist_dir}/#{@resource.name}.plist")
407
+ shadow_hash_data = get_shadow_hash_data(users_plist)
408
+ set_salted_pbkdf2(users_plist, shadow_hash_data, 'iterations', value)
409
+ flush_dscl_cache
410
+ end
411
+ end
412
+
413
+ def salt=(value)
414
+ # The iterations and salt properties, like the password property, can only
415
+ # be modified by directly changing the user's plist. Because of this fact,
416
+ # we have to treat the ds cache just like you would in the password=
417
+ # method.
418
+ if (Puppet::Util::Package.versioncmp(Facter.value(:macosx_productversion_major), '10.7') > 0)
419
+ sleep 2
420
+ flush_dscl_cache
421
+ users_plist = Plist::parse_xml(plutil '-convert', 'xml1', '-o', '/dev/stdout', "#{users_plist_dir}/#{@resource.name}.plist")
422
+ shadow_hash_data = get_shadow_hash_data(users_plist)
423
+ set_salted_pbkdf2(users_plist, shadow_hash_data, 'salt', value)
424
+ flush_dscl_cache
425
+ end
426
+ end
427
+
428
+ #####
429
+ # Dynamically create setter methods for dscl properties
430
+ #####
431
+ #
432
+ # Setter methods are only called when a resource currently has a value for
433
+ # that property and it needs changed (true here since all of these values
434
+ # have a default that is set in the create method). We don't want to merge
435
+ # in additional values if an incorrect value is set, we want to CHANGE it.
436
+ # When using the -change argument in dscl, the old value needs to be passed
437
+ # first (followed by the new value). Because of this, we get the current
438
+ # value from the @property_hash variable and then use the value passed as
439
+ # the new value. Because we're prefetching instances of the provider, it's
440
+ # possible that the value determined at the start of the run may be stale
441
+ # (i.e. someone changed the value by hand during a Puppet run) - if that's
442
+ # the case we rescue the error from dscl and alert the user.
443
+ #
444
+ # In the event that the user doesn't HAVE a value for the attribute, the
445
+ # provider should use the -merge option with dscl to add the attribute value
446
+ # for the user record
447
+ ['home', 'uid', 'gid', 'comment', 'shell'].each do |setter_method|
448
+ define_method("#{setter_method}=") do |value|
449
+ if @property_hash[setter_method.intern]
450
+ begin
451
+ dscl '.', '-change', "/Users/#{resource.name}", self.class.ns_to_ds_attribute_map[setter_method.intern], @property_hash[setter_method.intern], value
452
+ rescue Puppet::ExecutionFailure => e
453
+ raise Puppet::Error, "Cannot set the #{setter_method} value of '#{value}' for user " +
454
+ "#{@resource.name} due to the following error: #{e.inspect}"
455
+ end
456
+ else
457
+ begin
458
+ dscl '.', '-merge', "/Users/#{resource.name}", self.class.ns_to_ds_attribute_map[setter_method.intern], value
459
+ rescue Puppet::ExecutionFailure => e
460
+ raise Puppet::Error, "Cannot set the #{setter_method} value of '#{value}' for user " +
461
+ "#{@resource.name} due to the following error: #{e.inspect}"
462
+ end
463
+ end
464
+ end
465
+ end
466
+
467
+
468
+ ## ##
469
+ ## Helper Methods ##
470
+ ## ##
471
+
472
+ def users_plist_dir
473
+ '/var/db/dslocal/nodes/Default/users'
474
+ end
475
+
476
+ def self.password_hash_dir
477
+ '/var/db/shadow/hash'
478
+ end
479
+
480
+ def merge_attribute_with_dscl(path, username, keyname, value)
481
+ # This method will merge in a given value using dscl
482
+ begin
483
+ dscl '.', '-merge', "/#{path}/#{username}", keyname, value
484
+ rescue Puppet::ExecutionFailure => detail
485
+ raise Puppet::Error, "Could not set the dscl #{keyname} key with value: #{value} - #{detail.inspect}"
486
+ end
487
+ end
488
+
489
+ def create_new_user(username)
490
+ # Create the new user with dscl
491
+ dscl '.', '-create', "/Users/#{username}"
83
492
  end
84
493
 
494
+ def next_system_id(min_id=20)
495
+ # Get the next available uid on the system by getting a list of user ids,
496
+ # sorting them, grabbing the last one, and adding a 1. Scientific stuff here.
497
+ dscl_output = dscl '.', '-list', '/Users', 'uid'
498
+ # We're ok with throwing away negative uids here. Also, remove nil values.
499
+ user_ids = dscl_output.split.compact.collect { |l| l.to_i if l.match(/^\d+$/) }
500
+ ids = user_ids.compact!.sort! { |a,b| a.to_f <=> b.to_f }
501
+ # We're just looking for an unused id in our sorted array.
502
+ ids.each_index do |i|
503
+ next_id = ids[i] + 1
504
+ return next_id if ids[i+1] != next_id and next_id >= min_id
505
+ end
506
+ end
507
+
508
+ def write_password_to_users_plist(value)
509
+ # # This method is only called on version 10.7 or greater. On 10.7 machines,
510
+ # # passwords are set using a salted-SHA512 hash, and on 10.8 machines,
511
+ # # passwords are set using PBKDF2. It's possible to have users on 10.8
512
+ # # who have upgraded from 10.7 and thus have a salted-SHA512 password hash.
513
+ # # If we encounter this, do what 10.8 does - remove that key and give them
514
+ # # a 10.8-style PBKDF2 password.
515
+ users_plist = Plist::parse_xml(plutil '-convert', 'xml1', '-o', '/dev/stdout', "#{users_plist_dir}/#{@resource.name}.plist")
516
+ shadow_hash_data = get_shadow_hash_data(users_plist)
517
+ if Facter.value(:macosx_productversion_major) == '10.7'
518
+ set_salted_sha512(users_plist, shadow_hash_data, value)
519
+ else
520
+ shadow_hash_data.delete('SALTED-SHA512') if shadow_hash_data['SALTED-SHA512']
521
+ set_salted_pbkdf2(users_plist, shadow_hash_data, 'entropy', value)
522
+ end
523
+ end
524
+
525
+ def flush_dscl_cache
526
+ dscacheutil '-flushcache'
527
+ end
528
+
529
+ def get_shadow_hash_data(users_plist)
530
+ # This method will return the binary plist that's embedded in the
531
+ # ShadowHashData key of a user's plist, or false if it doesn't exist.
532
+ if users_plist['ShadowHashData']
533
+ password_hash_plist = users_plist['ShadowHashData'][0].string
534
+ self.class.convert_binary_to_xml(password_hash_plist)
535
+ else
536
+ false
537
+ end
538
+ end
539
+
540
+ def set_salted_sha512(users_plist, shadow_hash_data, value)
541
+ # Puppet requires a salted-sha512 password hash for 10.7 users to be passed
542
+ # in Hex, but the embedded plist stores that value as a Base64 encoded
543
+ # string. This method converts the string and calls the
544
+ # write_users_plist_to_disk method to serialize and write the plist to disk.
545
+ unless shadow_hash_data
546
+ shadow_hash_data = Hash.new
547
+ shadow_hash_data['SALTED-SHA512'] = StringIO.new
548
+ end
549
+ shadow_hash_data['SALTED-SHA512'].string = Base64.decode64([[value].pack("H*")].pack("m").strip)
550
+ binary_plist = self.class.convert_xml_to_binary(shadow_hash_data)
551
+ users_plist['ShadowHashData'][0].string = binary_plist
552
+ write_users_plist_to_disk(users_plist)
553
+ end
554
+
555
+ def set_salted_pbkdf2(users_plist, shadow_hash_data, field, value)
556
+ # This method accepts a passed value and one of three fields: 'salt',
557
+ # 'entropy', or 'iterations'. These fields correspond with the fields
558
+ # utilized in a PBKDF2 password hashing system
559
+ # (see http://en.wikipedia.org/wiki/PBKDF2 ) where 'entropy' is the
560
+ # password hash, 'salt' is the password hash salt value, and 'iterations'
561
+ # is an integer recommended to be > 10,000. The remaining arguments are
562
+ # the user's plist itself, and the shadow_hash_data hash containing the
563
+ # existing PBKDF2 values.
564
+ shadow_hash_data = Hash.new unless shadow_hash_data
565
+ shadow_hash_data['SALTED-SHA512-PBKDF2'] = Hash.new unless shadow_hash_data['SALTED-SHA512-PBKDF2']
566
+ case field
567
+ when 'salt', 'entropy'
568
+ shadow_hash_data['SALTED-SHA512-PBKDF2'][field] = StringIO.new unless shadow_hash_data['SALTED-SHA512-PBKDF2'][field]
569
+ shadow_hash_data['SALTED-SHA512-PBKDF2'][field].string = Base64.decode64([[value].pack("H*")].pack("m").strip)
570
+ when 'iterations'
571
+ shadow_hash_data['SALTED-SHA512-PBKDF2'][field] = Integer(value)
572
+ else
573
+ raise Puppet::Error "Puppet has tried to set an incorrect field for the 'SALTED-SHA512-PBKDF2' hash. Acceptable fields are 'salt', 'entropy', or 'iterations'."
574
+ end
575
+
576
+ # on 10.8, this field *must* contain 8 stars, or authentication will
577
+ # fail.
578
+ users_plist['passwd'] = ('*' * 8)
85
579
 
580
+ # Convert shadow_hash_data to a binary plist, write that value to the
581
+ # users_plist hash, and write the users_plist back to disk.
582
+ binary_plist = self.class.convert_xml_to_binary(shadow_hash_data)
583
+ users_plist['ShadowHashData'][0].string = binary_plist
584
+ write_users_plist_to_disk(users_plist)
585
+ end
586
+
587
+ def write_users_plist_to_disk(users_plist)
588
+ # This method will accept a plist in XML format, save it to disk, convert
589
+ # the plist to a binary format, and flush the dscl cache.
590
+ Plist::Emit.save_plist(users_plist, "#{users_plist_dir}/#{@resource.name}.plist")
591
+ plutil'-convert', 'binary1', "#{users_plist_dir}/#{@resource.name}.plist"
592
+ end
593
+
594
+ def write_to_file(filename, value)
595
+ # This is a simple wrapper method for writing values to a file.
596
+ begin
597
+ File.open(filename, 'w') { |f| f.write(value)}
598
+ rescue Errno::EACCES => detail
599
+ raise Puppet::Error, "Could not write to file #{filename}: #{detail}"
600
+ end
601
+ end
602
+
603
+ def write_sha1_hash(value)
604
+ users_guid = self.class.get_attribute_from_dscl('Users', @resource.name, 'GeneratedUID')['dsAttrTypeStandard:GeneratedUID'][0]
605
+ password_hash_file = "#{self.class.password_hash_dir}/#{users_guid}"
606
+ write_to_file(password_hash_file, value)
607
+
608
+ # NBK: For shadow hashes, the user AuthenticationAuthority must contain a value of
609
+ # ";ShadowHash;". The LKDC in 10.5 makes this more interesting though as it
610
+ # will dynamically generate ;Kerberosv5;;username@LKDC:SHA1 attributes if
611
+ # missing. Thus we make sure we only set ;ShadowHash; if it is missing, and
612
+ # we can do this with the merge command. This allows people to continue to
613
+ # use other custom AuthenticationAuthority attributes without stomping on them.
614
+ #
615
+ # There is a potential problem here in that we're only doing this when setting
616
+ # the password, and the attribute could get modified at other times while the
617
+ # hash doesn't change and so this doesn't get called at all... but
618
+ # without switching all the other attributes to merge instead of create I can't
619
+ # see a simple enough solution for this that doesn't modify the user record
620
+ # every single time. This should be a rather rare edge case. (famous last words)
621
+
622
+ merge_attribute_with_dscl('Users', @resource.name, 'AuthenticationAuthority', ';ShadowHash;')
623
+ end
86
624
  end