Module: XinetdParser

Included in:
Inspec::Resources::XinetdConf
Defined in:
lib/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



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
# File 'lib/utils/parser.rb', line 209

def parse_xinetd(raw) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  return {} if raw.nil?
  res = {}
  cur_group = nil
  simple_conf = []
  rest = raw
  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 { |ores|
        ores.each { |k, v|
          res[k] ||= []
          res[k].concat(v)
        }
      }
    else
      simple_conf.push(inner_line)
    end
  end

  res
end

#xinetd_include_dir(dir) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/utils/parser.rb', line 198

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

  unless inspec.file(dir).directory?
    return skip_resource "Cannot read folder in #{dir}"
  end

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