2015-07-15 13:16:10 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Vulcano Security GmbH
|
|
|
|
# license: All rights reserved
|
|
|
|
|
|
|
|
# The file format consists of
|
|
|
|
# - group name
|
|
|
|
# - password
|
|
|
|
# - gid
|
|
|
|
# - group list, comma seperated list
|
|
|
|
|
2015-08-28 19:52:59 +00:00
|
|
|
class EtcGroup < Vulcano.resource(1)
|
|
|
|
name 'etc_group'
|
2015-07-15 13:16:10 +00:00
|
|
|
|
2015-08-28 19:52:59 +00:00
|
|
|
attr_accessor :gid, :entries
|
|
|
|
def initialize(path = nil)
|
|
|
|
@path = path || '/etc/group'
|
|
|
|
@entries = parse(@path)
|
2015-07-26 10:30:12 +00:00
|
|
|
end
|
2015-07-15 13:16:10 +00:00
|
|
|
|
2015-08-28 19:52:59 +00:00
|
|
|
def to_s
|
|
|
|
@path
|
2015-07-26 10:30:12 +00:00
|
|
|
end
|
2015-07-15 13:16:10 +00:00
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
def groups
|
2015-09-04 07:59:30 +00:00
|
|
|
entries.map { |x| x[0] }
|
2015-07-26 10:30:12 +00:00
|
|
|
end
|
2015-07-15 13:16:10 +00:00
|
|
|
|
2015-08-03 01:42:05 +00:00
|
|
|
def gids
|
2015-09-04 07:59:30 +00:00
|
|
|
entries.map { |x| x[2] }
|
2015-07-26 10:30:12 +00:00
|
|
|
end
|
2015-07-15 13:16:10 +00:00
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
def users
|
2015-09-04 07:59:30 +00:00
|
|
|
entries.map { |x| x[3].split(',') }.flatten
|
2015-07-26 10:30:12 +00:00
|
|
|
end
|
2015-07-15 13:16:10 +00:00
|
|
|
|
2015-09-03 18:43:58 +00:00
|
|
|
def where(conditions = {})
|
2015-08-03 01:42:05 +00:00
|
|
|
return if conditions.empty?
|
|
|
|
fields = {
|
|
|
|
name: 0,
|
|
|
|
group_name: 0,
|
|
|
|
password: 1,
|
|
|
|
gid: 2,
|
|
|
|
group_id: 2,
|
|
|
|
group_list: 3,
|
2015-09-04 07:59:30 +00:00
|
|
|
users: 3
|
2015-08-03 01:42:05 +00:00
|
|
|
}
|
|
|
|
res = entries
|
2015-09-04 07:59:30 +00:00
|
|
|
conditions.each do |k, v|
|
2015-08-03 01:49:05 +00:00
|
|
|
idx = fields[k.to_sym]
|
2015-08-03 01:42:05 +00:00
|
|
|
next if idx.nil?
|
2015-09-04 07:59:30 +00:00
|
|
|
res = res.map { |x| x[idx] == v.to_s }
|
2015-08-03 01:42:05 +00:00
|
|
|
end
|
|
|
|
@entries = res
|
|
|
|
self
|
2015-07-15 13:16:10 +00:00
|
|
|
end
|
2015-08-03 01:42:05 +00:00
|
|
|
|
2015-08-28 19:52:59 +00:00
|
|
|
private
|
2015-08-03 00:40:08 +00:00
|
|
|
|
2015-08-28 19:52:59 +00:00
|
|
|
def parse(path)
|
2015-08-30 02:33:15 +00:00
|
|
|
@content = vulcano.file(path).content
|
2015-08-29 09:10:36 +00:00
|
|
|
@content.split("\n").map do |line|
|
2015-08-28 19:52:59 +00:00
|
|
|
line.split(':')
|
|
|
|
end
|
2015-08-03 00:40:08 +00:00
|
|
|
end
|
2015-08-28 19:52:59 +00:00
|
|
|
end
|