class Inspec::Resources::UnixUser

implements generic unix id handling

def identity(username)

extracts the identity
def identity(username)
  cmd = inspec.command("#{id_cmd} #{username}")
  return nil if cmd.exit_status != 0
  # parse words
  params = SimpleConfig.new(
    parse_id_entries(cmd.stdout.chomp),
    assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
    group_re: nil,
    multiple_values: false,
  ).params
  {
    uid: convert_to_i(parse_value(params['uid']).keys[0]),
    username: parse_value(params['uid']).values[0],
    gid: convert_to_i(parse_value(params['gid']).keys[0]),
    groupname: parse_value(params['gid']).values[0],
    groups: parse_value(params['groups']).values,
  }
end

def initialize(inspec)

def initialize(inspec)
  @inspec = inspec
  @id_cmd ||= 'id'
  @list_users_cmd ||= 'cut -d: -f1 /etc/passwd | grep -v "^#"'
  super
end

def list_users

returns a list of all local users on a system
def list_users
  cmd = inspec.command(list_users_cmd)
  return [] if cmd.exit_status != 0
  cmd.stdout.chomp.lines
end

def parse_id_entries(raw)

splits the results of id into seperate lines
def parse_id_entries(raw)
  data = []
  until (index = raw.index(/\)\s{1}/)).nil?
    data.push(raw[0, index+1]) # inclue closing )
    raw = raw[index+2, raw.length-index-2]
  end
  data.push(raw) if !raw.nil?
  data.join("\n")
end

def parse_value(line)

parse one id entry like '0(wheel)''
def parse_value(line)
  SimpleConfig.new(
    line,
    line_separator: ',',
    assignment_re: /^\s*([^\(]*?)\s*\(\s*(.*?)\)*$/,
    group_re: nil,
    multiple_values: false,
  ).params
end