inspec/lib/resources/passwd.rb

94 lines
1.4 KiB
Ruby
Raw Normal View History

2015-07-15 13:15:18 +00:00
# encoding: utf-8
# copyright: 2015, Vulcano Security GmbH
2015-10-06 16:55:44 +00:00
# author: Christoph Hartmann
# author: Dominik Richter
2015-07-15 13:15:18 +00:00
# license: All rights reserved
2015-07-15 13:15:53 +00:00
# The file format consists of
# - username
# - password
# - userid
# - groupid
# - user id info
# - home directory
# - command
2015-09-05 17:05:18 +00:00
# usage:
#
# describe passwd do
# its(:usernames) { should eq ['root'] }
# its(:uids) { should eq [0] }
2015-09-05 17:05:18 +00:00
# end
#
# describe passwd.uid(0) do
# its(:username) { should eq 'root' }
# its(:count) { should eq 1 }
# end
2015-10-04 15:59:13 +00:00
require 'utils/parser'
2015-10-26 03:04:18 +00:00
class Passwd < Inspec.resource(1)
name 'passwd'
2015-07-14 22:47:17 +00:00
2015-10-04 15:59:13 +00:00
include ContentParser
2015-09-05 17:05:18 +00:00
attr_reader :uid
attr_reader :parsed
2015-07-14 22:47:17 +00:00
2015-09-05 17:05:18 +00:00
def initialize(path = nil)
@path = path || '/etc/passwd'
2015-10-26 03:04:18 +00:00
@content = inspec.file(@path).content
@parsed = parse_passwd(@content)
end
2015-09-05 17:05:18 +00:00
# call passwd().uid(0)
# returns a seperate object with reference to this object
def uid(uid)
PasswdUid.new(self, uid)
2015-07-26 10:30:12 +00:00
end
2015-07-15 13:15:53 +00:00
2015-07-26 10:30:12 +00:00
def usernames
map_data('name')
2015-07-26 10:30:12 +00:00
end
2015-07-15 13:15:53 +00:00
2015-07-26 10:30:12 +00:00
def passwords
map_data('password')
2015-07-26 10:30:12 +00:00
end
2015-07-15 13:15:53 +00:00
2015-07-26 10:30:12 +00:00
def uids
map_data('uid')
2015-07-26 10:30:12 +00:00
end
2015-07-14 22:47:17 +00:00
2015-07-26 10:30:12 +00:00
def gids
map_data('gid')
2015-07-26 10:30:12 +00:00
end
2015-07-14 22:47:17 +00:00
def to_s
'/etc/passwd'
end
private
def map_data(id)
@parsed.map {|x|
x[id]
}
end
2015-07-26 10:30:12 +00:00
end
2015-09-05 17:05:18 +00:00
# object that hold a specifc uid view on passwd
class PasswdUid
def initialize(passwd, uid)
@passwd = passwd
@users = @passwd.parsed.select { |x| x['uid'] == "#{uid}" }
2015-09-05 17:05:18 +00:00
end
def username
@users.at(0)['name']
2015-09-05 17:05:18 +00:00
end
def count
@users.size
2015-09-05 17:05:18 +00:00
end
end