class UCMT::Discovery::LocalUsers

Constants

GROUPS_KEYS_MAPPING
INTEGER_KEYS
SYSTEM_USER_LIMIT
USERS_KEYS_MAPPING

Public Class Methods

new() click to toggle source

TODO: remote machine

# File lib/ucmt/discovery/local_users.rb, line 8
def initialize
end

Public Instance Methods

read_data() click to toggle source
# File lib/ucmt/discovery/local_users.rb, line 11
def read_data
  {
    "local_users" => {
      "add" => read
    }
  }
end

Private Instance Methods

read() click to toggle source
# File lib/ucmt/discovery/local_users.rb, line 57
def read
  # reading shadow need root permissions
  # TODO: for remote check needs to be different
  if Process.euid == 0
    passwd = read_passwords
  else
    passwd = {}
  end

  groups = read_groups

  users = read_users.map do |dk, dv|
    USERS_KEYS_MAPPING.each_with_object({"name" => dk}) { |(k, v), r| r[k] = dv[v] }
  end
  users.each { |u| INTEGER_KEYS.each { |i| u[i] = u[i].to_i } }
  users.select! { |v| v["uid"] == 0 || v["uid"] > SYSTEM_USER_LIMIT } # select only non system users
  users.each do |user|
    user["groups"] = []

    groups.each do |name, group|
      gid = group[GROUPS_KEYS_MAPPING["gid"]].to_i
      group_users = group[GROUPS_KEYS_MAPPING["users"]].split(",") # see man group

      if user["gid"] == gid || group_users.include?(user["name"])
        user["groups"] << name
      end

      if user["gid"] == gid
        user["primary_group"] = name
        user.delete("gid")
      end
    end
    user["password"] = passwd[user["name"]].first if passwd[user["name"]]
  end

  users
end
read_groups() click to toggle source
# File lib/ucmt/discovery/local_users.rb, line 28
def read_groups
  output = Cheetah.run("ansible", "localhost", "-m", "getent", "-a", "database=group", stdout: :capture)

  res = JSON.parse(output.sub(/^.*=>/, ""))
  res["ansible_facts"]["getent_group"]
end
read_passwords() click to toggle source
# File lib/ucmt/discovery/local_users.rb, line 35
def read_passwords
  output = Cheetah.run("ansible", "localhost", "-m", "getent", "-a", "database=shadow", stdout: :capture)

  res = JSON.parse(output.sub(/^.*=>/, ""))
  res["ansible_facts"]["getent_shadow"]
end
read_users() click to toggle source
# File lib/ucmt/discovery/local_users.rb, line 21
def read_users
  output = Cheetah.run("ansible", "localhost", "-m", "getent", "-a", "database=passwd", stdout: :capture)

  res = JSON.parse(output.sub(/^.*=>/, ""))
  res["ansible_facts"]["getent_passwd"]
end