2016-02-26 12:19:16 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require 'utils/parser'
|
2016-03-12 17:20:58 +00:00
|
|
|
require 'utils/filter'
|
2018-03-22 12:25:45 +00:00
|
|
|
require 'utils/file_reader'
|
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'
|
2018-02-19 14:26:49 +00:00
|
|
|
supports platform: 'unix'
|
2016-03-08 18:06:55 +00:00
|
|
|
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
|
2018-03-22 12:25:45 +00:00
|
|
|
include FileReader
|
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
|
|
|
|
@contents = {}
|
2018-03-22 12:25:45 +00:00
|
|
|
read_content(@conf_path)
|
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 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-04-25 05:52:22 +00:00
|
|
|
filter = FilterTable.create
|
|
|
|
filter.add_accessor(:where)
|
|
|
|
.add_accessor(:entries)
|
2016-03-12 20:43:38 +00:00
|
|
|
.add(:services, field: 'service')
|
|
|
|
.add(:ids, field: 'id')
|
|
|
|
.add(:socket_types, field: 'socket_type')
|
|
|
|
.add(:types, field: 'type')
|
2017-01-31 12:37:43 +00:00
|
|
|
.add(:protocols, field: 'protocol')
|
2016-03-12 20:43:38 +00:00
|
|
|
.add(:wait, field: 'wait')
|
|
|
|
.add(:disabled?) { |x| x.where('disable' => 'no').services.empty? }
|
|
|
|
.add(:enabled?) { |x| x.where('disable' => 'yes').services.empty? }
|
2016-04-25 05:52:22 +00:00
|
|
|
.connect(self, :service_lines)
|
2016-02-26 12:19:16 +00:00
|
|
|
|
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)
|
2016-02-26 12:19:16 +00:00
|
|
|
|
2018-03-22 12:25:45 +00:00
|
|
|
@contents[path] = read_file_content(path)
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2016-03-12 17:20:58 +00:00
|
|
|
|
|
|
|
def read_params
|
|
|
|
return {} if read_content.nil?
|
|
|
|
flat_params = parse_xinetd(read_content)
|
2016-07-27 11:49:45 +00:00
|
|
|
# we need to map service data in order to use it with filtertable
|
2016-03-12 17:20:58 +00:00
|
|
|
params = { 'services' => {} }
|
2016-07-27 11:49:45 +00:00
|
|
|
# map services that were defined and map it to the service hash
|
2016-03-12 17:20:58 +00:00
|
|
|
flat_params.each do |k, v|
|
|
|
|
name = k[/^service (.+)$/, 1]
|
2016-07-27 11:49:45 +00:00
|
|
|
# its not a service, no change required
|
2016-03-12 17:20:58 +00:00
|
|
|
if name.nil?
|
|
|
|
params[k] = v
|
2016-07-27 11:49:45 +00:00
|
|
|
# handle service entries
|
2016-03-12 17:20:58 +00:00
|
|
|
else
|
2016-07-27 11:49:45 +00:00
|
|
|
# store service
|
2016-03-12 17:20:58 +00:00
|
|
|
params['services'][name] = v
|
2016-07-27 11:49:45 +00:00
|
|
|
|
2016-03-12 17:20:58 +00:00
|
|
|
# add the service identifier to its parameters
|
2016-07-27 11:49:45 +00:00
|
|
|
if v.is_a?(Array)
|
|
|
|
v.each { |service| service.params['service'] = name }
|
|
|
|
else
|
|
|
|
v.params['service'] = name
|
|
|
|
end
|
2016-03-12 17:20:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
2017-02-01 11:15:52 +00:00
|
|
|
# Method used to derive the default protocol used from the socket_type
|
|
|
|
def default_protocol(type)
|
|
|
|
case type
|
|
|
|
when 'stream'
|
|
|
|
'tcp'
|
|
|
|
when 'dgram'
|
|
|
|
'udp'
|
|
|
|
else
|
|
|
|
'unknown'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-12 17:20:58 +00:00
|
|
|
def service_lines
|
2017-02-01 11:15:52 +00:00
|
|
|
@services ||= params['services'].values.flatten.map { |service|
|
|
|
|
service.params['protocol'] ||= default_protocol(service.params['socket_type'])
|
|
|
|
service.params
|
|
|
|
}
|
2016-03-12 17:20:58 +00:00
|
|
|
end
|
2016-02-26 12:19:16 +00:00
|
|
|
end
|
|
|
|
end
|