Module: Inspec::Utils::XinetdParser

Included in:
Resources::XinetdConf
Defined in:
lib/inspec/utils/parser.rb

Overview

This parser for xinetd (extended Internet daemon) configuration files

Instance Method Summary collapse

Instance Method Details

#parse_xinetd(raw) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/inspec/utils/parser.rb', line 229

def parse_xinetd(raw) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  return {} if raw.nil?

  require "inspec/utils/simpleconfig"

  res = {}
  cur_group = nil
  simple_conf = []
  rest = raw + "\n"
  until rest.empty?
    # extract content line
    nl = rest.index("\n") || (rest.length - 1)
    comment = rest.index("#") || (rest.length - 1)
    dst_idx = comment < nl ? comment : nl
    inner_line = dst_idx == 0 ? "" : rest[0..dst_idx - 1].strip
    # update unparsed content
    rest = rest[nl + 1..-1]
    next if inner_line.empty?

    if inner_line == "}"
      if cur_group == "defaults"
        res[cur_group] = SimpleConfig.new(simple_conf.join("\n"))
      else
        res[cur_group] ||= []
        res[cur_group].push(SimpleConfig.new(simple_conf.join("\n")))
      end
      cur_group = nil
    elsif rest.lstrip[0] == "{"
      cur_group = inner_line
      simple_conf = []
      rest = rest[rest.index("\n") + 1..-1]
    elsif cur_group.nil?
      # parse all included files
      others = xinetd_include_dir(inner_line[/includedir (.+)/, 1])

      # complex merging of included configurations, as multiple services
      # may be defined with the same name but different configuration
      others.each do |ores|
        ores.each do |k, v|
          res[k] ||= []
          res[k].concat(v)
        end
      end
    else
      simple_conf.push(inner_line)
    end
  end

  res
end

#xinetd_include_dir(dir) ⇒ Object



218
219
220
221
222
223
224
225
226
227
# File 'lib/inspec/utils/parser.rb', line 218

def xinetd_include_dir(dir)
  return [] if dir.nil?

  unless inspec.file(dir).directory?
    raise Inspec::Exceptions::ResourceSkipped, "Can't find folder: #{dir}"
  end

  files = inspec.command("find #{dir} -type f").stdout.split("\n")
  files.map { |file| parse_xinetd(read_content(file)) }
end