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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
# File 'lib/utils/parser.rb', line 195

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 == '}'
      res[cur_group] = SimpleConfig.new(simple_conf.join("\n"))
      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].push(v)
        }
      }
    else
      simple_conf.push(inner_line)
    end
  end

  res
end

#xinetd_include_dir(dir) ⇒ Object



184
185
186
187
188
189
190
191
192
193
# File 'lib/utils/parser.rb', line 184

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