Mirror of metasploit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

module_paths.rb 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: binary -*-
  2. #
  3. # Gems
  4. #
  5. require 'active_support/concern'
  6. require 'active_support/core_ext/hash'
  7. require 'active_support/core_ext/module'
  8. # Deals with module paths in the {Msf::ModuleManager}
  9. module Msf::ModuleManager::ModulePaths
  10. extend ActiveSupport::Concern
  11. # Adds a path to be searched for new modules.
  12. #
  13. # @param [String] path
  14. # @param [Hash] opts
  15. # @option opts [Array] whitelist An array of regex patterns to search for specific modules
  16. # @return (see Msf::Modules::Loader::Base#load_modules)
  17. def add_module_path(path, opts={})
  18. nested_paths = []
  19. # remove trailing file separator
  20. path_without_trailing_file_separator = path.sub(/#{File::SEPARATOR}$/, '')
  21. # Make the path completely canonical
  22. pathname = Pathname.new(path_without_trailing_file_separator).expand_path
  23. # Make sure the path is a valid directory
  24. unless pathname.directory?
  25. raise ArgumentError, "The path supplied is not a valid directory.", caller
  26. end
  27. nested_paths << pathname.to_s
  28. # Update the module paths appropriately
  29. self.module_paths = (module_paths + nested_paths).flatten.uniq
  30. # Load all of the modules from the nested paths
  31. count_by_type = {}
  32. nested_paths.each { |path|
  33. path_count_by_type = load_modules(path, opts.merge({:force => false}))
  34. # merge hashes
  35. path_count_by_type.each do |type, path_count|
  36. accumulated_count = count_by_type.fetch(type, 0)
  37. count_by_type[type] = accumulated_count + path_count
  38. end
  39. }
  40. return count_by_type
  41. end
  42. #
  43. # Removes a path from which to search for modules.
  44. #
  45. def remove_module_path(path)
  46. module_paths.delete(path)
  47. module_paths.delete(::File.expand_path(path))
  48. end
  49. protected
  50. attr_accessor :module_paths # :nodoc:
  51. end