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-08-28 19:27:35 +00:00
|
|
|
class Passwd < Vulcano.resource(1)
|
|
|
|
name 'passwd'
|
2015-07-14 22:47:17 +00:00
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
attr_accessor :uid
|
2015-07-14 22:47:17 +00:00
|
|
|
|
2015-08-28 19:27:35 +00:00
|
|
|
def initialize(path = nil, uid: nil)
|
|
|
|
@path = path || '/etc/passwd'
|
2015-08-30 02:33:15 +00:00
|
|
|
@content = vulcano.file(@path).content
|
2015-08-28 19:27:35 +00:00
|
|
|
@parsed = parse(@content)
|
|
|
|
end
|
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
def to_s
|
2015-08-28 19:27:35 +00:00
|
|
|
@path
|
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 determine_uid ()
|
|
|
|
uids = Array.new
|
2015-08-28 19:27:35 +00:00
|
|
|
@parsed.each {|x|
|
2015-07-26 10:30:12 +00:00
|
|
|
if ( x.at(2) == "#{@uid}") then
|
|
|
|
uids.push(x.at(0))
|
2015-07-14 22:47:17 +00:00
|
|
|
end
|
2015-07-26 10:30:12 +00:00
|
|
|
}
|
|
|
|
uids
|
|
|
|
end
|
2015-07-14 22:47:17 +00:00
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
def username
|
|
|
|
uids = determine_uid()
|
|
|
|
uids.at(0)
|
|
|
|
end
|
2015-07-14 22:50:19 +00:00
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
def count
|
|
|
|
arr = determine_uid()
|
|
|
|
arr.length
|
|
|
|
end
|
2015-07-15 13:15:53 +00:00
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
def map_data (id)
|
2015-08-28 19:27:35 +00:00
|
|
|
@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
|
2015-08-28 19:27:35 +00:00
|
|
|
@parsed.map {|x|
|
2015-07-26 10:30:12 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2015-07-14 22:47:17 +00:00
|
|
|
|
2015-08-28 19:27:35 +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
|