Class: Inspec::Resources::UnixUser

Inherits:
UserInfo
  • Object
show all
Defined in:
lib/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.



338
339
340
341
342
343
# File 'lib/resources/users.rb', line 338

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.



337
338
339
# File 'lib/resources/users.rb', line 337

def id_cmd
  @id_cmd
end

#inspecObject (readonly)

Returns the value of attribute inspec.



337
338
339
# File 'lib/resources/users.rb', line 337

def inspec
  @inspec
end

#list_users_cmdObject (readonly)

Returns the value of attribute list_users_cmd.



337
338
339
# File 'lib/resources/users.rb', line 337

def list_users_cmd
  @list_users_cmd
end

Instance Method Details

#identity(username) ⇒ Object

extracts the identity



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/resources/users.rb', line 364

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



346
347
348
349
350
# File 'lib/resources/users.rb', line 346

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



386
387
388
389
390
391
392
393
394
# File 'lib/resources/users.rb', line 386

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

#parse_value(line) ⇒ Object

parse one id entry like ‘0(wheel)”



353
354
355
356
357
358
359
360
361
# File 'lib/resources/users.rb', line 353

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