2015-08-03 03:11:01 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Vulcano Security GmbH
|
2015-10-06 16:55:44 +00:00
|
|
|
# author: Dominik Richter
|
|
|
|
# author: Christoph Hartmann
|
2015-08-03 03:11:01 +00:00
|
|
|
|
2015-09-10 09:19:00 +00:00
|
|
|
module FindFiles
|
2015-08-03 03:11:01 +00:00
|
|
|
TYPES = {
|
|
|
|
block: 'b',
|
|
|
|
character: 'c',
|
|
|
|
directory: 'd',
|
|
|
|
pipe: 'p',
|
|
|
|
file: 'f',
|
|
|
|
link: 'l',
|
|
|
|
socket: 's',
|
2015-09-10 01:39:43 +00:00
|
|
|
door: 'D',
|
2016-01-15 02:59:00 +00:00
|
|
|
}.freeze
|
2015-08-03 03:11:01 +00:00
|
|
|
|
2015-12-04 09:44:52 +00:00
|
|
|
# ignores errors
|
2015-09-10 09:19:00 +00:00
|
|
|
def find_files(path, opts = {})
|
2018-05-03 13:53:20 +00:00
|
|
|
find_files_or_warn(path, opts) || []
|
2015-12-04 09:44:52 +00:00
|
|
|
end
|
|
|
|
|
2018-05-03 13:53:20 +00:00
|
|
|
def find_files_or_warn(path, opts = {})
|
2015-08-03 03:11:01 +00:00
|
|
|
depth = opts[:depth]
|
2015-12-04 13:27:32 +00:00
|
|
|
type = TYPES[opts[:type].to_sym] if opts[:type]
|
2015-08-03 03:11:01 +00:00
|
|
|
|
2018-05-03 13:53:20 +00:00
|
|
|
# If `path` contains a `'` we must modify how we quote the `sh -c` argument
|
|
|
|
quote = path.include?("'") ? '"' : '\''
|
|
|
|
|
|
|
|
cmd = "sh -c #{quote}find #{path}"
|
2015-08-03 03:11:01 +00:00
|
|
|
cmd += " -type #{type}" unless type.nil?
|
2017-05-29 19:04:03 +00:00
|
|
|
cmd += " -maxdepth #{depth.to_i}" if depth.to_i > 0
|
2018-05-03 13:53:20 +00:00
|
|
|
cmd += quote
|
2015-08-03 03:11:01 +00:00
|
|
|
|
2015-12-04 09:44:52 +00:00
|
|
|
result = inspec.command(cmd)
|
2015-09-10 09:19:00 +00:00
|
|
|
exit_status = result.exit_status
|
2015-08-03 03:11:01 +00:00
|
|
|
|
2015-12-04 09:44:52 +00:00
|
|
|
unless exit_status == 0
|
2016-02-01 07:54:24 +00:00
|
|
|
warn "find_files(): exit #{exit_status} from `#{cmd}`"
|
2015-12-04 09:44:52 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
result.stdout.split("\n")
|
2016-01-15 02:59:00 +00:00
|
|
|
.map(&:strip)
|
|
|
|
.find_all { |x| !x.empty? }
|
2015-08-03 03:11:01 +00:00
|
|
|
end
|
|
|
|
end
|