Class: Inspec::Resources::UnixUser

Inherits:
UserInfo
  • Object
show all
Defined in:
lib/inspec/resources/users.rb

Overview

implements generic unix id handling

Direct Known Subclasses

AixUser, DarwinUser, FreeBSDUser, HpuxUser, LinuxUser

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from UserInfo

#collect_user_details, #credentials, #meta_info, #user_details

Methods included from Converter

#convert_to_i, to_boolean

Constructor Details

#initialize(inspec) ⇒ UnixUser

Returns a new instance of UnixUser.



467
468
469
470
471
472
# File 'lib/inspec/resources/users.rb', line 467

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

Instance Attribute Details

#id_cmdObject (readonly)

Returns the value of attribute id_cmd.



466
467
468
# File 'lib/inspec/resources/users.rb', line 466

def id_cmd
  @id_cmd
end

#inspecObject (readonly)

Returns the value of attribute inspec.



466
467
468
# File 'lib/inspec/resources/users.rb', line 466

def inspec
  @inspec
end

#list_users_cmdObject (readonly)

Returns the value of attribute list_users_cmd.



466
467
468
# File 'lib/inspec/resources/users.rb', line 466

def list_users_cmd
  @list_users_cmd
end

Instance Method Details

#identity(username) ⇒ Object

extracts the identity



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/inspec/resources/users.rb', line 494

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_regex: /^\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

#list_usersObject

returns a list of all local users on a system



475
476
477
478
479
480
# File 'lib/inspec/resources/users.rb', line 475

def list_users
  cmd = inspec.command(list_users_cmd)
  return [] if cmd.exit_status != 0

  cmd.stdout.chomp.lines
end

#parse_id_entries(raw) ⇒ Object

splits the results of id into seperate lines



516
517
518
519
520
521
522
523
524
# File 'lib/inspec/resources/users.rb', line 516

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) unless raw.nil?
  data.join("\n")
end

#parse_value(line) ⇒ Object

parse one id entry like ‘0(wheel)”



483
484
485
486
487
488
489
490
491
# File 'lib/inspec/resources/users.rb', line 483

def parse_value(line)
  SimpleConfig.new(
    line,
    line_separator: ",",
    assignment_regex: /^\s*([^\(]*?)\s*\(\s*(.*?)\)*$/,
    group_re: nil,
    multiple_values: false
  ).params
end