2016-02-26 12:19:16 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
|
|
|
|
|
|
|
require 'utils/parser'
|
2016-03-12 17:20:58 +00:00
|
|
|
require 'utils/filter'
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
module Inspec::Resources
|
2016-03-12 17:20:58 +00:00
|
|
|
class XinetdConf < Inspec.resource(1)
|
2016-03-08 18:06:55 +00:00
|
|
|
name 'xinetd_conf'
|
|
|
|
desc 'Xinetd services configuration.'
|
|
|
|
example "
|
|
|
|
describe xinetd_conf.services('chargen') do
|
|
|
|
its('socket_types') { should include 'dgram' }
|
|
|
|
end
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
describe xinetd_conf.services('chargen').socket_types('dgram') do
|
|
|
|
it { should be_disabled }
|
|
|
|
end
|
|
|
|
"
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
include XinetdParser
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2016-03-12 17:20:58 +00:00
|
|
|
def initialize(conf_path = '/etc/xinetd.conf')
|
2016-03-08 18:06:55 +00:00
|
|
|
@conf_path = conf_path
|
2016-03-12 17:20:58 +00:00
|
|
|
@filters = ''
|
2016-03-08 18:06:55 +00:00
|
|
|
@contents = {}
|
|
|
|
end
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def to_s
|
2016-03-12 17:20:58 +00:00
|
|
|
"Xinetd config #{@conf_path}#{@filters}"
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2016-03-12 17:20:58 +00:00
|
|
|
def params
|
|
|
|
@params ||= read_params
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2016-03-12 17:20:58 +00:00
|
|
|
extend Inspec::Filter
|
|
|
|
add_filter 'service'
|
|
|
|
add_filter 'id'
|
|
|
|
add_filter 'socket_type'
|
|
|
|
add_filter 'type'
|
|
|
|
add_filter 'wait'
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def disabled?
|
2016-03-12 17:20:58 +00:00
|
|
|
where({ 'disable' => 'no' }).services.empty?
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def enabled?
|
2016-03-12 17:20:58 +00:00
|
|
|
where({ 'disable' => 'yes' }).services.empty?
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2016-03-12 17:20:58 +00:00
|
|
|
def where(conditions = {})
|
|
|
|
fields, filters = Inspec::Filter.where(service_lines, conditions)
|
|
|
|
res = clone
|
|
|
|
res.instance_variable_set(:@filters, @filters + filters)
|
|
|
|
res.instance_variable_set(:@services, fields)
|
|
|
|
res
|
2016-02-26 12:19:16 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
private
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def read_content(path = @conf_path)
|
|
|
|
return @contents[path] if @contents.key?(path)
|
|
|
|
file = inspec.file(path)
|
|
|
|
if !file.file?
|
|
|
|
return skip_resource "Can't find file \"#{path}\""
|
|
|
|
end
|
|
|
|
|
|
|
|
@contents[path] = file.content
|
|
|
|
if @contents[path].empty? && file.size > 0
|
|
|
|
return skip_resource "Can't read file \"#{path}\""
|
|
|
|
end
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
@contents[path]
|
|
|
|
end
|
2016-03-12 17:20:58 +00:00
|
|
|
|
|
|
|
def read_params
|
|
|
|
return {} if read_content.nil?
|
|
|
|
flat_params = parse_xinetd(read_content)
|
|
|
|
params = { 'services' => {} }
|
|
|
|
|
|
|
|
# parse services that were defined:
|
|
|
|
flat_params.each do |k, v|
|
|
|
|
name = k[/^service (.+)$/, 1]
|
|
|
|
if name.nil?
|
|
|
|
params[k] = v
|
|
|
|
else
|
|
|
|
params['services'][name] = v
|
|
|
|
# add the service identifier to its parameters
|
|
|
|
v.each { |service| service.params['service'] = name }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
|
|
|
def service_lines
|
|
|
|
@services ||= params['services'].values.flatten.map(&:params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_fields(*fields)
|
|
|
|
res = service_lines.map do |line|
|
|
|
|
fields.map { |f| line[f] }
|
|
|
|
end.flatten
|
|
|
|
return res unless fields == ['service']
|
|
|
|
res.uniq
|
|
|
|
end
|
2016-02-26 12:19:16 +00:00
|
|
|
end
|
|
|
|
end
|