Class: Puppet::Provider::NameService

Inherits:
Puppet::Provider show all
Defined in:
lib/vendor/puppet/provider/nameservice.rb,
lib/vendor/puppet/provider/nameservice/pw.rb,
lib/vendor/puppet/provider/nameservice/objectadd.rb,
lib/vendor/puppet/provider/nameservice/directoryservice.rb

Overview

This is the parent class of all NSS classes. They’re very different in their backend, but they’re pretty similar on the front-end. This class provides a way for them all to be as similar as possible.

Direct Known Subclasses

DirectoryService, ObjectAdd

Defined Under Namespace

Classes: DirectoryService, ObjectAdd, PW

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Instance Attribute Summary

Attributes inherited from Puppet::Provider

#model, #resource

Attributes included from Util::Docs

#doc, #nodoc

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Puppet::Provider

#<=>, #clear, command, #command, commands, declared_feature?, default?, defaultfor, make_command_methods, mk_resource_methods, mkmodelmethods, #name, optional_commands, specificity, supports_parameter?, #to_s

Methods included from Util::Logging

#clear_deprecation_warnings, #deprecation_warning, #send_log

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Methods included from Util::Warnings

clear_warnings, notice_once, warnonce

Methods included from Confiner

#confine, #confine_collection, #suitable?

Methods included from Util::Errors

#adderrorcontext, #devfail, #error_context, #exceptwrap, #fail

Constructor Details

#initialize(resource) ⇒ NameService

Returns a new instance of NameService.



259
260
261
262
263
# File 'lib/vendor/puppet/provider/nameservice.rb', line 259

def initialize(resource)
  super

  @objectinfo = nil
end

Class Method Details

.autogen_default(param) ⇒ Object



8
9
10
# File 'lib/vendor/puppet/provider/nameservice.rb', line 8

def autogen_default(param)
  defined?(@autogen_defaults) ? @autogen_defaults[param.intern] : nil
end

.autogen_defaults(hash) ⇒ Object



12
13
14
15
16
17
# File 'lib/vendor/puppet/provider/nameservice.rb', line 12

def autogen_defaults(hash)
  @autogen_defaults ||= {}
  hash.each do |param, value|
    @autogen_defaults[param.intern] = value
  end
end

.initvarsObject



19
20
21
22
# File 'lib/vendor/puppet/provider/nameservice.rb', line 19

def initvars
  @checks = {}
  super
end

.instancesObject



24
25
26
27
28
29
30
31
# File 'lib/vendor/puppet/provider/nameservice.rb', line 24

def instances
  objects = []
  listbyname do |name|
    objects << new(:name => name, :ensure => :present)
  end

  objects
end

.listbynameObject

List everything out by name. Abstracted a bit so that it works for both users and groups.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vendor/puppet/provider/nameservice.rb', line 52

def listbyname
  names = []
  Etc.send("set#{section()}ent")
  begin
    while ent = Etc.send("get#{section()}ent")
      names << ent.name
      yield ent.name if block_given?
    end
  ensure
    Etc.send("end#{section()}ent")
  end

  names
end

.option(name, option) ⇒ Object



33
34
35
36
# File 'lib/vendor/puppet/provider/nameservice.rb', line 33

def option(name, option)
  name = name.intern if name.is_a? String
  (defined?(@options) and @options.include? name and @options[name].include? option) ? @options[name][option] : nil
end

.options(name, hash) ⇒ Object

Raises:



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vendor/puppet/provider/nameservice.rb', line 38

def options(name, hash)
  raise Puppet::DevError, "#{name} is not a valid attribute for #{resource_type.name}" unless resource_type.valid_parameter?(name)
  @options ||= {}
  @options[name] ||= {}

  # Set options individually, so we can call the options method
  # multiple times.
  hash.each do |param, value|
    @options[name][param] = value
  end
end

.resource_type=(resource_type) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/vendor/puppet/provider/nameservice.rb', line 67

def resource_type=(resource_type)
  super
  @resource_type.validproperties.each do |prop|
    next if prop == :ensure
    define_method(prop) { get(prop) || :absent} unless public_method_defined?(prop)
    define_method(prop.to_s + "=") { |*vals| set(prop, *vals) } unless public_method_defined?(prop.to_s + "=")
  end
end

.sectionObject

This is annoying, but there really aren’t that many options, and this is built into Ruby.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vendor/puppet/provider/nameservice.rb', line 78

def section
  unless defined?(@resource_type)
    raise Puppet::DevError,
      "Cannot determine Etc section without a resource type"

  end

  if @resource_type.name == :group
    "gr"
  else
    "pw"
  end
end

.validate(name, value) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/vendor/puppet/provider/nameservice.rb', line 92

def validate(name, value)
  name = name.intern if name.is_a? String
  if @checks.include? name
    block = @checks[name][:block]
    raise ArgumentError, "Invalid value #{value}: #{@checks[name][:error]}" unless block.call(value)
  end
end

.verify(name, error, &block) ⇒ Object



100
101
102
103
# File 'lib/vendor/puppet/provider/nameservice.rb', line 100

def verify(name, error, &block)
  name = name.intern if name.is_a? String
  @checks[name] = {:error => error, :block => block}
end

Instance Method Details

#autogen(field) ⇒ Object

Autogenerate a value. Mostly used for uid/gid, but also used heavily with DirectoryServices, because DirectoryServices is stupid.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/vendor/puppet/provider/nameservice.rb', line 114

def autogen(field)
  field = field.intern
  id_generators = {:user => :uid, :group => :gid}
  if id_generators[@resource.class.name] == field
    return autogen_id(field)
  else
    if value = self.class.autogen_default(field)
      return value
    elsif respond_to?("autogen_#{field}")
      return send("autogen_#{field}")
    else
      return nil
    end
  end
end

#autogen_id(field) ⇒ Object

Autogenerate either a uid or a gid. This is hard-coded: we can only generate one field type per class.



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
# File 'lib/vendor/puppet/provider/nameservice.rb', line 132

def autogen_id(field)
  highest = 0

  group = method = nil
  case @resource.class.name
  when :user; group = :passwd; method = :uid
  when :group; group = :group; method = :gid
  else
    raise Puppet::DevError, "Invalid resource name #{resource}"
  end

  # Make sure we don't use the same value multiple times
  if defined?(@@prevauto)
    @@prevauto += 1
  else
    Etc.send(group) { |obj|
      if obj.gid > highest
        highest = obj.send(method) unless obj.send(method) > 65000
      end
    }

    @@prevauto = highest + 1
  end

  @@prevauto
end

#createObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/vendor/puppet/provider/nameservice.rb', line 159

def create
  if exists?
    info "already exists"
    # The object already exists
    return nil
  end

  begin
    execute(self.addcmd)
    if feature?(:manages_password_age) && (cmd = passcmd)
      execute(cmd)
    end
  rescue Puppet::ExecutionFailure => detail
    raise Puppet::Error, "Could not create #{@resource.class.name} #{@resource.name}: #{detail}"
  end
end

#deleteObject



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/vendor/puppet/provider/nameservice.rb', line 176

def delete
  unless exists?
    info "already absent"
    # the object already doesn't exist
    return nil
  end

  begin
    execute(self.deletecmd)
  rescue Puppet::ExecutionFailure => detail
    raise Puppet::Error, "Could not delete #{@resource.class.name} #{@resource.name}: #{detail}"
  end
end

#ensureObject



190
191
192
193
194
195
196
# File 'lib/vendor/puppet/provider/nameservice.rb', line 190

def ensure
  if exists?
    :present
  else
    :absent
  end
end

#exists?Boolean

Does our object exist?

Returns:

  • (Boolean)


199
200
201
# File 'lib/vendor/puppet/provider/nameservice.rb', line 199

def exists?
  !!getinfo(true)
end

#get(param) ⇒ Object

Retrieve a specific value by name.



204
205
206
# File 'lib/vendor/puppet/provider/nameservice.rb', line 204

def get(param)
  (hash = getinfo(false)) ? hash[param] : nil
end

#getinfo(refresh) ⇒ Object

Retrieve what we can about our object



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/vendor/puppet/provider/nameservice.rb', line 209

def getinfo(refresh)
  if @objectinfo.nil? or refresh == true
    @etcmethod ||= ("get" + self.class.section.to_s + "nam").intern
    begin
      @objectinfo = Etc.send(@etcmethod, @resource[:name])
    rescue ArgumentError => detail
      @objectinfo = nil
    end
  end

  # Now convert our Etc struct into a hash.
  @objectinfo ? info2hash(@objectinfo) : nil
end

#groupsObject

The list of all groups the user is a member of. Different user mgmt systems will need to override this method.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/vendor/puppet/provider/nameservice.rb', line 225

def groups
  groups = []

  # Reset our group list
  Etc.setgrent

  user = @resource[:name]

  # Now iterate across all of the groups, adding each one our
  # user is a member of
  while group = Etc.getgrent
    members = group.mem

    groups << group.name if members.include? user
  end

  # We have to close the file, so each listing is a separate
  # reading of the file.
  Etc.endgrent

  groups.join(",")
end

#info2hash(info) ⇒ Object

Convert the Etc struct into a hash.



249
250
251
252
253
254
255
256
257
# File 'lib/vendor/puppet/provider/nameservice.rb', line 249

def info2hash(info)
  hash = {}
  self.class.resource_type.validproperties.each do |param|
    method = posixmethod(param)
    hash[param] = info.send(posixmethod(param)) if info.respond_to? method
  end

  hash
end

#set(param, value) ⇒ Object

Raises:



265
266
267
268
269
270
271
272
273
274
# File 'lib/vendor/puppet/provider/nameservice.rb', line 265

def set(param, value)
  self.class.validate(param, value)
  cmd = modifycmd(param, value)
  raise Puppet::DevError, "Nameservice command must be an array" unless cmd.is_a?(Array)
  begin
    execute(cmd)
  rescue Puppet::ExecutionFailure => detail
    raise Puppet::Error, "Could not set #{param} on #{@resource.class.name}[#{@resource.name}]: #{detail}"
  end
end