mirror of
https://github.com/inspec/inspec
synced 2024-11-23 13:13:22 +00:00
rewrite passwd resource to extract parser
This commit is contained in:
parent
3ff4a5d769
commit
535fc10b5d
1 changed files with 30 additions and 38 deletions
|
@ -32,7 +32,7 @@ class Passwd < Vulcano.resource(1)
|
||||||
def initialize(path = nil)
|
def initialize(path = nil)
|
||||||
@path = path || '/etc/passwd'
|
@path = path || '/etc/passwd'
|
||||||
@content = vulcano.file(@path).content
|
@content = vulcano.file(@path).content
|
||||||
@parsed = parse(@content)
|
@parsed = parse_passwd(@content)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
@ -45,76 +45,68 @@ class Passwd < Vulcano.resource(1)
|
||||||
PasswdUid.new(self, uid)
|
PasswdUid.new(self, uid)
|
||||||
end
|
end
|
||||||
|
|
||||||
# works without uid parameter
|
|
||||||
def map_data(id)
|
|
||||||
@parsed.map {|x|
|
|
||||||
x.at(id)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def usernames
|
def usernames
|
||||||
map_data(0)
|
map_data('name')
|
||||||
end
|
end
|
||||||
|
|
||||||
def passwords
|
def passwords
|
||||||
map_data(1)
|
map_data('password')
|
||||||
end
|
end
|
||||||
|
|
||||||
def uids
|
def uids
|
||||||
map_data(2)
|
map_data('uid')
|
||||||
end
|
end
|
||||||
|
|
||||||
def gids
|
def gids
|
||||||
map_data(3)
|
map_data('gid')
|
||||||
end
|
end
|
||||||
|
|
||||||
def users
|
def users
|
||||||
@parsed.map {|x|
|
@parsed.map {|x|
|
||||||
{
|
x['name']
|
||||||
'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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def parse(content)
|
def map_data(id)
|
||||||
|
@parsed.map {|x|
|
||||||
|
x[id]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_passwd(content)
|
||||||
content.split("\n").map do |line|
|
content.split("\n").map do |line|
|
||||||
line.split(':')
|
parse_passwd_line(line)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parse_passwd_line(line)
|
||||||
|
x = line.split(':')
|
||||||
|
{
|
||||||
|
'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),
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# object that hold a specifc uid view on passwd
|
# object that hold a specifc uid view on passwd
|
||||||
class PasswdUid
|
class PasswdUid
|
||||||
def initialize(passwd, uid)
|
def initialize(passwd, uid)
|
||||||
@passwd = passwd
|
@passwd = passwd
|
||||||
@uid = uid
|
@users = @passwd.parsed.select { |x| x['uid'] == "#{uid}" }
|
||||||
end
|
|
||||||
|
|
||||||
def determine_uid
|
|
||||||
uids = []
|
|
||||||
@passwd.parsed.each {|x|
|
|
||||||
if (x.at(2) == "#{@uid}")
|
|
||||||
uids.push(x.at(0))
|
|
||||||
end
|
|
||||||
}
|
|
||||||
uids
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def username
|
def username
|
||||||
uids = determine_uid
|
@users.at(0)['name']
|
||||||
uids.at(0)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def count
|
def count
|
||||||
arr = determine_uid
|
@users.size
|
||||||
arr.length
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue