Module: Puppet::Util::POSIX

Included in:
Puppet::Util
Defined in:
lib/puppet/util/posix.rb

Overview

Utility methods for interacting with POSIX objects; mostly user and group

Constant Summary collapse

LOCALE_ENV_VARS =

This is a list of environment variables that we will set when we want to override the POSIX locale

%w[LANG LC_ALL LC_MESSAGES LANGUAGE
LC_COLLATE LC_CTYPE LC_MONETARY LC_NUMERIC LC_TIME]
USER_ENV_VARS =

This is a list of user-related environment variables that we will unset when we want to provide a pristine environment for “exec” runs

%w[HOME USER LOGNAME]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.groups_of(user) ⇒ Object

Returns an array of all the groups that the user’s a member of.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/util/posix.rb', line 15

def groups_of(user)
  begin
    require_relative '../../puppet/ffi/posix'
    groups = get_groups_list(user)
  rescue StandardError, LoadError => e
    Puppet.debug("Falling back to Puppet::Etc.group: #{e.message}")

    groups = []
    Puppet::Etc.group do |group|
      groups << group.name if group.mem.include?(user)
    end
  end

  uniq_groups = groups.uniq
  if uniq_groups != groups
    Puppet.debug(_('Removing any duplicate group entries'))
  end

  uniq_groups
end

Instance Method Details

#get_posix_field(space, field, id) ⇒ Object

Retrieve a field from a POSIX Etc object. The id can be either an integer or a name. This only works for users and groups. It’s also broken on some platforms, unfortunately, which is why we fall back to the other method search_posix_field in the gid and uid methods if a sanity check fails

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/puppet/util/posix.rb', line 76

def get_posix_field(space, field, id)
  raise Puppet::DevError, _("Did not get id from caller") unless id

  if id.is_a?(Integer)
    if id > Puppet[:maximum_uid].to_i
      Puppet.err _("Tried to get %{field} field for silly id %{id}") % { field: field, id: id }
      return nil
    end
    method = methodbyid(space)
  else
    method = methodbyname(space)
  end

  begin
    Etc.send(method, id).send(field)
  rescue NoMethodError, ArgumentError
    # ignore it; we couldn't find the object
    nil
  end
end

#gid(group) ⇒ Object

Get the GID



159
160
161
# File 'lib/puppet/util/posix.rb', line 159

def gid(group)
  get_posix_value(:group, :gid, group)
end

#idfield(space) ⇒ Object

Determine what the field name is for users and groups.



129
130
131
132
133
134
135
136
# File 'lib/puppet/util/posix.rb', line 129

def idfield(space)
  case space.intern
  when :gr, :group; :gid
  when :pw, :user, :passwd; :uid
  else
    raise ArgumentError, _("Can only handle users and groups")
  end
end

#methodbyid(space) ⇒ Object

Determine what the method is to get users and groups by id



139
140
141
142
143
144
145
146
# File 'lib/puppet/util/posix.rb', line 139

def methodbyid(space)
  case space.intern
  when :gr, :group; :getgrgid
  when :pw, :user, :passwd; :getpwuid
  else
    raise ArgumentError, _("Can only handle users and groups")
  end
end

#methodbyname(space) ⇒ Object

Determine what the method is to get users and groups by name



149
150
151
152
153
154
155
156
# File 'lib/puppet/util/posix.rb', line 149

def methodbyname(space)
  case space.intern
  when :gr, :group; :getgrnam
  when :pw, :user, :passwd; :getpwnam
  else
    raise ArgumentError, _("Can only handle users and groups")
  end
end

#search_posix_field(type, field, id) ⇒ Object

A degenerate method of retrieving name/id mappings. The job of this method is to retrieve all objects of a certain type, search for a specific entry and then return a given field from that entry.



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
# File 'lib/puppet/util/posix.rb', line 100

def search_posix_field(type, field, id)
  idmethod = idfield(type)
  integer = false
  if id.is_a?(Integer)
    integer = true
    if id > Puppet[:maximum_uid].to_i
      Puppet.err _("Tried to get %{field} field for silly id %{id}") % { field: field, id: id }
      return nil
    end
  end

  Etc.send(type) do |object|
    if integer and object.send(idmethod) == id
      return object.send(field)
    elsif object.name == id
      return object.send(field)
    end
  end

  # Apparently the group/passwd methods need to get reset; if we skip
  # this call, then new users aren't found.
  case type
  when :passwd; Etc.send(:endpwent)
  when :group; Etc.send(:endgrent)
  end
  nil
end

#uid(user) ⇒ Object

Get the UID



164
165
166
# File 'lib/puppet/util/posix.rb', line 164

def uid(user)
  get_posix_value(:passwd, :uid, user)
end