Module: XinetdParser

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

Instance Method Summary collapse

Instance Method Details

#parse_xinetd(raw) ⇒ Object

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



193
194
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
# File 'lib/utils/parser.rb', line 193

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?
    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
    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?
      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



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

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