2007-02-15 07:28:52 +00:00
|
|
|
require 'find'
|
2009-03-17 21:48:17 +00:00
|
|
|
require 'rex/compat'
|
2010-10-06 17:57:56 +00:00
|
|
|
require 'tempfile'
|
2007-02-15 05:32:48 +00:00
|
|
|
|
2005-11-02 14:18:50 +00:00
|
|
|
module Rex
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
2009-02-21 19:22:06 +00:00
|
|
|
# This class provides helper methods for dealing with files that are not
|
2005-11-02 14:18:50 +00:00
|
|
|
# supplied by the standard ruby API.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module FileUtils
|
|
|
|
|
2011-01-25 02:24:37 +00:00
|
|
|
#
|
|
|
|
# This methods cleans the supplied path of directory traversal sequences
|
|
|
|
# It must accept path/with/..a/folder../starting/or/ending/in/two/dots
|
|
|
|
# but clean ../something as well as path/with/..\traversal
|
|
|
|
#
|
|
|
|
def self.clean_path(old)
|
|
|
|
path = old
|
|
|
|
while(path.index(/\/..\/|\/..\\|\\..\\|\\..\/|\A..\\|\A..\//) != nil)
|
|
|
|
path.gsub!(/\A..\\|\A..\//,'') #eliminate starting ..\ or ../
|
|
|
|
path.gsub!(/\/..\/|\/..\\/,'/') #clean linux style
|
|
|
|
path.gsub!(/\\..\\|\\..\//,'\\') #clean windows style
|
|
|
|
end
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
2005-11-02 14:18:50 +00:00
|
|
|
#
|
|
|
|
# This method searches the PATH environment variable for
|
|
|
|
# a fully qualified path to the supplied file name.
|
|
|
|
#
|
|
|
|
def self.find_full_path(file_name)
|
2009-10-02 14:17:03 +00:00
|
|
|
|
|
|
|
# Check for the absolute fast first
|
2010-02-22 17:27:54 +00:00
|
|
|
if (file_name[0,1] == "/" and ::File.exists?(file_name) and ::File::Stat.new(file_name))
|
2009-10-02 14:17:03 +00:00
|
|
|
return file_name
|
|
|
|
end
|
|
|
|
|
2008-11-10 21:44:58 +00:00
|
|
|
path = Rex::Compat.getenv('PATH')
|
|
|
|
if (path)
|
|
|
|
path.split(::File::PATH_SEPARATOR).each { |base|
|
2008-10-13 14:22:38 +00:00
|
|
|
begin
|
2010-10-20 09:50:12 +00:00
|
|
|
# Deal with Windows paths surrounded by quotes. Prevents
|
|
|
|
# silliness like trying to look for
|
|
|
|
# '"C:\\framework\\nmap"\\nmap.exe' which will always fail.
|
|
|
|
base = $1 if base =~ /^"(.*)"$/
|
2008-10-13 14:22:38 +00:00
|
|
|
path = base + ::File::SEPARATOR + file_name
|
2010-09-17 16:02:10 +00:00
|
|
|
if (::File::Stat.new(path) and not ::File.directory?(path))
|
2008-10-13 14:22:38 +00:00
|
|
|
return path
|
2005-11-02 14:18:50 +00:00
|
|
|
end
|
2008-10-13 14:22:38 +00:00
|
|
|
rescue
|
|
|
|
end
|
|
|
|
}
|
2005-11-02 14:18:50 +00:00
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2010-10-06 17:57:56 +00:00
|
|
|
class Quickfile < ::Tempfile
|
|
|
|
def initialize(*args)
|
|
|
|
super(*args)
|
2010-10-12 01:42:08 +00:00
|
|
|
self.binmode
|
2010-10-06 17:57:56 +00:00
|
|
|
ObjectSpace.undefine_finalizer(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-15 05:32:48 +00:00
|
|
|
module Find
|
|
|
|
#
|
|
|
|
# Identical to Find.find from Ruby, but follows symlinks to directories.
|
|
|
|
# See http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/68671
|
|
|
|
#
|
|
|
|
def self.find(*paths)
|
|
|
|
paths.collect!{|d| d.dup}
|
|
|
|
while file = paths.shift
|
|
|
|
catch(:prune) do
|
|
|
|
yield file.dup.taint
|
|
|
|
next unless File.exist? file
|
|
|
|
begin
|
|
|
|
if File.stat(file).directory? then
|
|
|
|
d = Dir.open(file)
|
|
|
|
begin
|
|
|
|
for f in d
|
|
|
|
next if f == "." or f == ".."
|
|
|
|
if File::ALT_SEPARATOR and file =~ /^(?:[\/\\]|[A-Za-z]:[\/\\]?)$/ then
|
|
|
|
f = file + f
|
|
|
|
elsif file == "/" then
|
|
|
|
f = "/" + f
|
|
|
|
else
|
|
|
|
f = File.join(file, f)
|
|
|
|
end
|
|
|
|
paths.unshift f.untaint
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
d.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Errno::ENOENT, Errno::EACCES
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.prune
|
|
|
|
throw :prune
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2008-11-10 21:44:58 +00:00
|
|
|
end
|