inspec/lib/resources/passwd.rb

103 lines
1.6 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-07-14 22:47:17 +00:00
module Serverspec
module Type
class Passwd < File
attr_accessor :uid
def to_s
2015-07-15 13:15:53 +00:00
%Q[/etc/passwd]
2015-07-14 22:47:17 +00:00
end
def determine_uid ()
parsed = parse()
uids = Array.new
parsed.each {|x|
if ( x.at(2) == "#{@uid}") then
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
2015-07-15 13:15:53 +00:00
def map_data (id)
2015-07-14 22:50:19 +00:00
parsed = parse()
parsed.map {|x|
2015-07-15 13:15:53 +00:00
x.at(id)
2015-07-14 22:50:19 +00:00
}
end
2015-07-15 13:15:53 +00:00
def usernames
map_data(0)
end
def passwords
map_data(1)
end
def uids
map_data(2)
end
def gids
map_data(3)
end
def users
2015-07-14 22:47:17 +00:00
parsed = parse()
parsed.map {|x|
2015-07-15 13:15:53 +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-14 22:47:17 +00:00
}
end
def parse
entries = Array.new
content().split("\n").each do |line|
entries.push(line.split(':'))
end
entries
end
end
2015-07-15 13:15:53 +00:00
def passwd(uid=nil)
i = Passwd.new('/etc/passwd')
2015-07-14 22:47:17 +00:00
i.uid = uid
i
end
end
end
include Serverspec::Type