Class: Puppet::Provider::NameService::DirectoryService
Constant Summary
collapse
- @@ds_to_ns_attribute_map =
JJM 2007-07-25: This map is used to map NameService attributes to their
corresponding DirectoryService attribute names.
See: http://images.apple.com/server/docs.Open_Directory_v10.4.pdf
JJM: Note, this is de-coupled from the Puppet::Type, and must
be actively maintained. There may also be collisions with different
types (Users, Groups, Mounts, Hosts, etc...)
{
'RecordName' => :name,
'PrimaryGroupID' => :gid,
'NFSHomeDirectory' => :home,
'UserShell' => :shell,
'UniqueID' => :uid,
'RealName' => :comment,
'Password' => :password,
'GeneratedUID' => :guid,
'IPAddress' => :ip_address,
'ENetAddress' => :en_address,
'GroupMembership' => :members,
}
- @@ns_to_ds_attribute_map =
JJM The same table as above, inverted.
{
:name => 'RecordName',
:gid => 'PrimaryGroupID',
:home => 'NFSHomeDirectory',
:shell => 'UserShell',
:uid => 'UniqueID',
:comment => 'RealName',
:password => 'Password',
:guid => 'GeneratedUID',
:en_address => 'ENetAddress',
:ip_address => 'IPAddress',
:members => 'GroupMembership',
}
- @@password_hash_dir =
"/var/db/shadow/hash"
Class Attribute Summary collapse
#model, #resource
Attributes included from Util::Docs
#doc, #nodoc
Class Method Summary
collapse
Instance Method Summary
collapse
#autogen, autogen_default, autogen_defaults, #autogen_id, #delete, #ensure, #exists?, #get, #groups, #info2hash, #initialize, initvars, listbyname, option, options, resource_type=, section, validate, verify
#clear, command, #command, commands, declared_feature?, default?, defaultfor, #get, #initialize, initvars, make_command_methods, mk_resource_methods, mkmodelmethods, #name, optional_commands, specificity, supports_parameter?, #to_s
#send_log
Methods included from Util
activerecord_version, benchmark, chuser, classproxy, #execfail, #execpipe, execute, logmethods, memory, proxy, recmkdir, secure_open, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, which, withumask
#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Methods included from Util::Docs
#desc, #dochook, #doctable, #nodoc?, #pad, scrub
clear_warnings, notice_once, warnonce
Methods included from Confiner
#confine, #confine_collection, #suitable?
#adderrorcontext, #devfail, #error_context, #exceptwrap, #fail
Class Attribute Details
.ds_path=(value) ⇒ Object
JJM: This allows us to pass information when calling
Puppet::Type.type
e.g. Puppet::Type.type(:user).provide :directoryservice, :ds_path => "Users"
This is referenced in the get_ds_path class method
29
30
31
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 29
def ds_path=(value)
@ds_path = value
end
|
.macosx_version_major=(value) ⇒ Object
Sets the attribute macosx_version_major
30
31
32
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 30
def macosx_version_major=(value)
@macosx_version_major = value
end
|
Class Method Details
.generate_attribute_hash(input_hash, *type_properties) ⇒ Object
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
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 182
def self.generate_attribute_hash(input_hash, *type_properties)
attribute_hash = {}
input_hash.keys.each do |key|
ds_attribute = key.sub("dsAttrTypeStandard:", "")
next unless (@@ds_to_ns_attribute_map.keys.include?(ds_attribute) and type_properties.include? @@ds_to_ns_attribute_map[ds_attribute])
ds_value = input_hash[key]
case @@ds_to_ns_attribute_map[ds_attribute]
when :members
ds_value = ds_value when :gid, :uid
begin
ds_value = Integer(ds_value[0])
rescue ArgumentError
ds_value = ds_value[0]
end
else ds_value = ds_value[0]
end
attribute_hash[@@ds_to_ns_attribute_map[ds_attribute]] = ds_value
end
attribute_hash[:password] = self.get_password(attribute_hash[:guid]) if @resource_type.validproperties.include?(:password) and Puppet.features.root?
attribute_hash
end
|
.get_ds_path ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 91
def self.get_ds_path
return @ds_path if defined?(@ds_path)
@resource_type.name.to_s.capitalize + "s"
end
|
.get_exec_preamble(ds_action, resource_name = nil) ⇒ Object
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
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 249
def self.get_exec_preamble(ds_action, resource_name = nil)
if self.get_macosx_version_major > "10.4"
command_vector = [ command(:dscl), "-plist", "." ]
elsif self.get_macosx_version_major == "10.4"
command_vector = [ command(:dscl), "-url", "." ]
else
fail("Puppet does not support OS X versions < 10.4")
end
command_vector << ds_action
if resource_name
command_vector << "/#{get_ds_path}/#{resource_name}"
else
command_vector << "/#{get_ds_path}"
end
command_vector
end
|
.get_macosx_version_major ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 104
def self.get_macosx_version_major
return @macosx_version_major if defined?(@macosx_version_major)
begin
Facter.loadfacts
if Facter.value(:macosx_productversion_major)
product_version_major = Facter.value(:macosx_productversion_major)
else
Puppet.warning("DEPRECATION WARNING: Future versions of the directoryservice provider will require Facter 1.5.5 or newer.")
product_version = Facter.value(:macosx_productversion)
fail("Could not determine OS X version from Facter") if product_version.nil?
product_version_major = product_version.scan(/(\d+)\.(\d+)./).join(".")
end
fail("#{product_version_major} is not supported by the directoryservice provider") if %w{10.0 10.1 10.2 10.3}.include?(product_version_major)
@macosx_version_major = product_version_major
return @macosx_version_major
rescue Puppet::ExecutionFailure => detail
fail("Could not determine OS X version: #{detail}")
end
end
|
.get_password(guid) ⇒ Object
315
316
317
318
319
320
321
322
323
324
325
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 315
def self.get_password(guid)
password_hash = nil
password_hash_file = "#{@@password_hash_dir}/#{guid}"
if File.exists?(password_hash_file) and File.file?(password_hash_file)
fail("Could not read password hash file at #{password_hash_file}") if not File.readable?(password_hash_file)
f = File.new(password_hash_file)
password_hash = f.read
f.close
end
password_hash
end
|
.instances ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 78
def self.instances
type_property_array = [:name] + @resource_type.validproperties
list_all_present.collect do |name_string|
self.new(single_report(name_string, *type_property_array))
end
end
|
.list_all_present ⇒ Object
128
129
130
131
132
133
134
135
136
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 128
def self.list_all_present
begin
dscl_output = execute(get_exec_preamble("-list"))
rescue Puppet::ExecutionFailure => detail
fail("Could not get #{@resource_type.name} list from DirectoryService")
end
dscl_output.split("\n")
end
|
.parse_dscl_plist_data(dscl_output) ⇒ Object
178
179
180
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 178
def self.parse_dscl_plist_data(dscl_output)
Plist.parse_xml(dscl_output)
end
|
.parse_dscl_url_data(dscl_output) ⇒ Object
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
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 138
def self.parse_dscl_url_data(dscl_output)
dscl_plist = {}
dscl_output.split("\n").inject([]) do |array, line|
if line =~ /^\s+/ array[-1] << line else
array << line
end
array
end.compact
dscl_output.each do |line|
split_array = line.split(/:\s+/)
key = split_array.first
value = CGI::unescape(split_array.last.strip.chomp)
if key == "GroupMembership"
dscl_plist[key] = value.split(/\s/)
else
dscl_plist[key] = [value]
end
end
dscl_plist
end
|
.set_password(resource_name, guid, password_hash) ⇒ Object
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
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 284
def self.set_password(resource_name, guid, password_hash)
password_hash_file = "#{@@password_hash_dir}/#{guid}"
begin
File.open(password_hash_file, 'w') { |f| f.write(password_hash)}
rescue Errno::EACCES => detail
fail("Could not write to password hash file: #{detail}")
end
dscl_vector = self.get_exec_preamble("-merge", resource_name)
dscl_vector << "AuthenticationAuthority" << ";ShadowHash;"
begin
dscl_output = execute(dscl_vector)
rescue Puppet::ExecutionFailure => detail
fail("Could not set AuthenticationAuthority.")
end
end
|
.single_report(resource_name, *type_properties) ⇒ Object
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
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 214
def self.single_report(resource_name, *type_properties)
all_present_str_array = list_all_present
return nil unless all_present_str_array.include? resource_name
dscl_vector = get_exec_preamble("-read", resource_name)
begin
dscl_output = execute(dscl_vector)
rescue Puppet::ExecutionFailure => detail
fail("Could not get report. command execution failed.")
end
if self.get_macosx_version_major > "10.4"
dscl_plist = self.parse_dscl_plist_data(dscl_output)
elsif self.get_macosx_version_major == "10.4"
dscl_plist = self.parse_dscl_url_data(dscl_output)
else
fail("Puppet does not support OS X versions < 10.4")
end
self.generate_attribute_hash(dscl_plist, *type_properties)
end
|
Instance Method Details
#add_members(current_members, new_members) ⇒ Object
459
460
461
462
463
464
465
466
467
468
469
470
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 459
def add_members(current_members, new_members)
new_members.flatten.each do |new_member|
if current_members.nil? or not current_members.include?(new_member)
cmd = [:dseditgroup, "-o", "edit", "-n", ".", "-a", new_member, @resource[:name]]
begin
execute(cmd)
rescue Puppet::ExecutionFailure => detail
fail("Could not add #{new_member} to group: #{@resource.name}, #{detail}")
end
end
end
end
|
NBK: we override @parent.create as we need to execute a series of commands to create objects with dscl, rather than the single command nameservice.rb expects to be returned by addcmd. Thus we don’t bother defining addcmd.
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 399
def create
if exists?
info "already exists"
return nil
end
guid = %x{/usr/bin/uuidgen}.chomp
exec_arg_vector = self.class.get_exec_preamble("-create", @resource[:name])
exec_arg_vector << @@ns_to_ds_attribute_map[:guid] << guid
begin
execute(exec_arg_vector)
rescue Puppet::ExecutionFailure => detail
fail("Could not set GeneratedUID for #{@resource.class.name} #{@resource.name}: #{detail}")
end
if value = @resource.should(:password) and value != ""
self.class.set_password(@resource[:name], guid, value)
end
Puppet::Type.type(@resource.class.name).validproperties.each do |property|
next if property == :ensure
if value = @resource.should(property) and value != ""
if property == :members
add_members(nil, value)
else
exec_arg_vector = self.class.get_exec_preamble("-create", @resource[:name])
exec_arg_vector << @@ns_to_ds_attribute_map[symbolize(property)]
next if property == :password exec_arg_vector << value.to_s
begin
execute(exec_arg_vector)
rescue Puppet::ExecutionFailure => detail
fail("Could not create #{@resource.class.name} #{@resource.name}: #{detail}")
end
end
end
end
end
|
#deletecmd ⇒ Object
472
473
474
475
476
477
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 472
def deletecmd
self.class.get_exec_preamble("-delete", @resource[:name])
end
|
#ensure=(ensure_value) ⇒ Object
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 327
def ensure=(ensure_value)
super
if ensure_value == :present
@resource.class.validproperties.each do |name|
next if name == :ensure
if @resource.should(name)
@resource.property(name).sync
elsif value = autogen(name)
self.send(name.to_s + "=", value)
else
next
end
end
end
end
|
#getinfo(refresh = false) ⇒ Object
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 479
def getinfo(refresh = false)
if refresh or (! defined?(@property_value_cache_hash) or ! @property_value_cache_hash)
type_properties = [:name] + self.class.resource_type.validproperties
type_properties.delete(:ensure) if type_properties.include? :ensure
type_properties << :guid @property_value_cache_hash = self.class.single_report(@resource[:name], *type_properties)
[:uid, :gid].each do |param|
@property_value_cache_hash[param] = @property_value_cache_hash[param].to_i if @property_value_cache_hash and @property_value_cache_hash.include?(param)
end
end
@property_value_cache_hash
end
|
#password=(passphrase) ⇒ Object
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 350
def password=(passphrase)
exec_arg_vector = self.class.get_exec_preamble("-read", @resource.name)
exec_arg_vector << @@ns_to_ds_attribute_map[:guid]
begin
guid_output = execute(exec_arg_vector)
guid_plist = Plist.parse_xml(guid_output)
guid = guid_plist["dsAttrTypeStandard:#{@@ns_to_ds_attribute_map[:guid]}"][0]
self.class.set_password(@resource.name, guid, passphrase)
rescue Puppet::ExecutionFailure => detail
fail("Could not set #{param} on #{@resource.class.name}[#{@resource.name}]: #{detail}")
end
end
|
#remove_unwanted_members(current_members, new_members) ⇒ Object
446
447
448
449
450
451
452
453
454
455
456
457
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 446
def remove_unwanted_members(current_members, new_members)
current_members.each do |member|
if not new_members.flatten.include?(member)
cmd = [:dseditgroup, "-o", "edit", "-n", ".", "-d", member, @resource[:name]]
begin
execute(cmd)
rescue Puppet::ExecutionFailure => detail
fail("Could not remove #{member} from group: #{@resource.name}, #{detail}")
end
end
end
end
|
#set(param, value) ⇒ Object
NBK: we override @parent.set as we need to execute a series of commands to deal with array values, rather than the single command nameservice.rb expects to be returned by modifycmd. Thus we don’t bother defining modifycmd.
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
# File 'lib/puppet/provider/nameservice/directoryservice.rb', line 370
def set(param, value)
self.class.validate(param, value)
current_members = @property_value_cache_hash[:members]
if param == :members
remove_unwanted_members(current_members, value) if @resource[:auth_membership] and not current_members.nil?
add_members(current_members, value)
else
exec_arg_vector = self.class.get_exec_preamble("-create", @resource[:name])
exec_arg_vector << @@ns_to_ds_attribute_map[symbolize(param)]
exec_arg_vector << value.to_s
begin
execute(exec_arg_vector)
rescue Puppet::ExecutionFailure => detail
fail("Could not set #{param} on #{@resource.class.name}[#{@resource.name}]: #{detail}")
end
end
end
|