inspec/lib/resources/passwd.rb

121 lines
1.8 KiB
Ruby
Raw Normal View History

2015-07-15 13:15:18 +00:00
# encoding: utf-8
# copyright: 2015, Vulcano Security GmbH
# 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 1 }
# end
#
# describe passwd.uid(0) do
# its(:username) { should eq 'root' }
# its(:count) { should eq 1 }
# end
class Passwd < Vulcano.resource(1)
name 'passwd'
2015-07-14 22:47:17 +00:00
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'
@content = vulcano.file(@path).content
@parsed = parse(@content)
end
2015-07-26 10:30:12 +00:00
def to_s
@path
2015-07-26 10:30:12 +00:00
end
2015-07-14 22:47:17 +00:00
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-09-05 17:05:18 +00:00
# works without uid parameter
2015-09-03 18:43:58 +00:00
def map_data(id)
@parsed.map {|x|
2015-07-26 10:30:12 +00:00
x.at(id)
}
end
2015-07-15 13:15:53 +00:00
2015-07-26 10:30:12 +00:00
def usernames
map_data(0)
end
2015-07-15 13:15:53 +00:00
2015-07-26 10:30:12 +00:00
def passwords
map_data(1)
end
2015-07-15 13:15:53 +00:00
2015-07-26 10:30:12 +00:00
def uids
map_data(2)
end
2015-07-14 22:47:17 +00:00
2015-07-26 10:30:12 +00:00
def gids
map_data(3)
end
2015-07-14 22:47:17 +00:00
2015-07-26 10:30:12 +00:00
def users
@parsed.map {|x|
2015-07-26 10:30:12 +00:00
{
2015-09-03 21:18:28 +00:00
'name' => x.at(0),
'password' => x.at(1),
'uid' => x.at(2),
'gid' => x.at(3),
'desc' => x.at(4),
'home' => x.at(5),
'shell' => x.at(6)
2015-07-26 10:30:12 +00:00
}
}
end
2015-07-14 22:47:17 +00:00
private
def parse(content)
content.split("\n").map do |line|
line.split(':')
2015-07-14 22:47:17 +00:00
end
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
@uid = uid
end
def determine_uid
uids = []
@passwd.parsed.each {|x|
if (x.at(2) == "#{@uid}")
uids.push(x.at(0))
end
}
uids
end
def username
uids = determine_uid
uids.at(0)
end
def count
arr = determine_uid
arr.length
end
end