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

Constructor Details

#initialize(inspec) ⇒ UnixUser

Returns a new instance of UnixUser.



384
385
386
387
388
389
# File 'lib/inspec/resources/users.rb', line 384

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.



383
384
385
# File 'lib/inspec/resources/users.rb', line 383

def id_cmd
  @id_cmd
end

#inspecObject (readonly)

Returns the value of attribute inspec.



383
384
385
# File 'lib/inspec/resources/users.rb', line 383

def inspec
  @inspec
end

#list_users_cmdObject (readonly)

Returns the value of attribute list_users_cmd.



383
384
385
# File 'lib/inspec/resources/users.rb', line 383

def list_users_cmd
  @list_users_cmd
end

Instance Method Details

#identity(username) ⇒ Object

extracts the identity



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/inspec/resources/users.rb', line 411

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



392
393
394
395
396
397
# File 'lib/inspec/resources/users.rb', line 392

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



433
434
435
436
437
438
439
440
441
# File 'lib/inspec/resources/users.rb', line 433

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)”



400
401
402
403
404
405
406
407
408
# File 'lib/inspec/resources/users.rb', line 400

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