Method: Inspec::Resources::LsofPorts#lsof_parser

Defined in:
lib/inspec/resources/port.rb

#lsof_parser(lsof_cmd) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/AbcSize



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
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
# File 'lib/inspec/resources/port.rb', line 201

def lsof_parser(lsof_cmd)
  procs = {}
  # build this with formatted output (-F) from lsof
  # procs = {
  #   '123:sshd' => [
  #      'ipv4:tcp:22:127.0.0.1',
  #      'ipv6:tcp:22:::1',
  #      'ipv4:tcp:*',
  #      'ipv6:tcp:*',
  #   ],
  #   '456:ntpd' => [
  #      'ipv4:udp:123:*',
  #      'ipv6:udp:123:*',
  #   ]
  # }
  proc_id = port_id = nil
  lsof_cmd.stdout.each_line do |line|
    line.chomp!
    key = line.slice!(0)
    case key
    when "p"
      proc_id = line
      port_id = nil
    when "c"
      proc_id += ":" + line
    when "t"
      port_id = line.downcase
    when "P"
      port_id += ":" + line.downcase
    when "n"
      src, dst = line.split("->")

      # skip active comm streams
      next if dst

      host, port = /^(\S+):(\d+|\*)$/.match(src)[1, 2]

      # skip channels from port 0 - what does this mean?
      next if port == "*"

      # create new array stub if !exist?
      procs[proc_id] = [] unless procs.key?(proc_id)

      # change address '*' to zero
      host = port_id =~ /^ipv6:/ ? "[::]" : "0.0.0.0" if host == "*"
      # entrust URI to scrub the host and port
      begin
        uri = URI("addr://#{host}:#{port}")
        uri.host && uri.port
      rescue => e
        warn "could not parse URI 'addr://#{host}:#{port}' - #{e}"
        next
      end

      # e.g. 'ipv4:tcp:22:127.0.0.1'
      #                             strip ipv6 squares for inspec
      port_id += ":" + port + ":" + host.gsub(/^\[|\]$/, "")

      # lsof will give us another port unless it's done
      procs[proc_id] << port_id
    end
  end

  procs
end