summaryrefslogtreecommitdiff
path: root/lib/ohai/plugins/passwd.rb
blob: fd7d0500f7e7d23bfbb7a98b66ba9baca2992a25 (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

require 'etc'

Ohai.plugin do
  provides 'etc', 'current_user'

  def fix_encoding(str)
    str.force_encoding(Encoding.default_external) if str.respond_to?(:force_encoding)
    str
  end

  collect_data do
    unless etc
      etc Mash.new

      etc[:passwd] = Mash.new
      etc[:group] = Mash.new

      Etc.passwd do |entry|
        user_passwd_entry = Mash.new(:dir => entry.dir, :gid => entry.gid, :uid => entry.uid, :shell => entry.shell, :gecos => entry.gecos)
        user_passwd_entry.each_value {|v| fix_encoding(v)}
        etc[:passwd][fix_encoding(entry.name)] = user_passwd_entry
      end

      Etc.group do |entry|
        group_entry = Mash.new(:gid => entry.gid,
                               :members => entry.mem.map {|u| fix_encoding(u)})

        etc[:group][fix_encoding(entry.name)] = group_entry
      end
    end

    unless current_user
      current_user fix_encoding(Etc.getlogin)
    end
  end
end