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.



341
342
343
344
345
346
# File 'lib/inspec/resources/users.rb', line 341

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.



340
341
342
# File 'lib/inspec/resources/users.rb', line 340

def id_cmd
  @id_cmd
end

#inspecObject (readonly)

Returns the value of attribute inspec.



340
341
342
# File 'lib/inspec/resources/users.rb', line 340

def inspec
  @inspec
end

#list_users_cmdObject (readonly)

Returns the value of attribute list_users_cmd.



340
341
342
# File 'lib/inspec/resources/users.rb', line 340

def list_users_cmd
  @list_users_cmd
end

Instance Method Details

#identity(username) ⇒ Object

extracts the identity



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/inspec/resources/users.rb', line 368

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



349
350
351
352
353
354
# File 'lib/inspec/resources/users.rb', line 349

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



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

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



357
358
359
360
361
362
363
364
365
# File 'lib/inspec/resources/users.rb', line 357

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