2014-07-30 15:07:53 +00:00
|
|
|
# @note needs to use explicit nesting. so this file can be loaded directly without loading 'metasploit/framework', this
|
|
|
|
# file can be used prior to Bundler.require.
|
|
|
|
module Metasploit
|
|
|
|
module Framework
|
|
|
|
# Extension to `Kernel#require` behavior.
|
|
|
|
module Require
|
|
|
|
#
|
|
|
|
# Module Methods
|
|
|
|
#
|
|
|
|
|
|
|
|
# Tries to require `name`. If a `LoadError` occurs, then `without_warning` is printed to standard error using
|
|
|
|
# `Kernel#warn`, along with instructions for reinstalling the bundle. If a `LoadError` does not occur, then
|
|
|
|
# `with_block` is called.
|
|
|
|
#
|
|
|
|
# @param name [String] the name of the library to `Kernel#require`.
|
|
|
|
# @param without_warning [String] warning to print if `name` cannot be required.
|
|
|
|
# @yield block to run when `name` requires successfully
|
|
|
|
# @yieldreturn [void]
|
|
|
|
# @return [void]
|
|
|
|
def self.optionally(name, without_warning)
|
|
|
|
begin
|
|
|
|
require name
|
|
|
|
rescue LoadError
|
|
|
|
warn without_warning
|
|
|
|
warn "Bundle installed '--without #{Bundler.settings.without.join(' ')}'"
|
|
|
|
warn "To clear the without option do `bundle install --without ''` " \
|
|
|
|
"(the --without flag with an empty string) or " \
|
|
|
|
"`rm -rf .bundle` to remove the .bundle/config manually and " \
|
|
|
|
"then `bundle install`"
|
|
|
|
else
|
|
|
|
if block_given?
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tries to `require 'active_record/railtie'` to define the activerecord Rails initializers and rake tasks.
|
|
|
|
#
|
|
|
|
# @example Optionally requiring 'active_record/railtie'
|
|
|
|
# require 'metasploit/framework/require'
|
|
|
|
#
|
|
|
|
# class MyClass
|
|
|
|
# def setup
|
|
|
|
# if database_enabled
|
|
|
|
# Metasploit::Framework::Require.optionally_active_record_railtie
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# @return [void]
|
|
|
|
def self.optionally_active_record_railtie
|
2014-08-22 15:33:50 +00:00
|
|
|
if ::File.exist?(Rails.application.config.paths['config/database'].first)
|
|
|
|
optionally(
|
2014-07-30 15:07:53 +00:00
|
|
|
'active_record/railtie',
|
|
|
|
'activerecord not in the bundle, so database support will be disabled.'
|
2014-08-22 15:33:50 +00:00
|
|
|
)
|
|
|
|
else
|
|
|
|
warn 'Could not find database.yml, so database support will be disabled.'
|
|
|
|
end
|
2014-07-30 15:07:53 +00:00
|
|
|
end
|
|
|
|
|
2015-04-09 14:05:38 +00:00
|
|
|
# Tries to `require 'metasploit/credential'` and include `Metasploit::Credential::Creation` in the
|
|
|
|
# `including_module`.
|
2014-07-30 15:07:53 +00:00
|
|
|
#
|
|
|
|
# @param including_module [Module] `Class` or `Module` that wants to `include Metasploit::Credential::Creation`.
|
|
|
|
# @return [void]
|
|
|
|
def self.optionally_include_metasploit_credential_creation(including_module)
|
|
|
|
optionally(
|
2015-04-09 14:05:38 +00:00
|
|
|
'metasploit/credential',
|
|
|
|
"metasploit-credential not in the bundle, so Metasploit::Credential creation will fail for #{including_module.name}"
|
2014-07-30 15:07:53 +00:00
|
|
|
) do
|
|
|
|
including_module.send(:include, Metasploit::Credential::Creation)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-03 18:49:39 +00:00
|
|
|
# Tries to require gems necessary for using a database with the framework.
|
2014-10-02 19:46:56 +00:00
|
|
|
#
|
|
|
|
# @example
|
2014-10-03 18:49:39 +00:00
|
|
|
# Metasploit::Framework::Require.optionally_require_metasploit_db_gems
|
2014-10-02 19:46:56 +00:00
|
|
|
#
|
|
|
|
# @return [void]
|
2014-10-03 19:06:13 +00:00
|
|
|
def self.optionally_require_metasploit_db_gem_engines
|
2014-10-02 19:46:56 +00:00
|
|
|
optionally(
|
2014-11-05 15:25:50 +00:00
|
|
|
'metasploit/credential',
|
|
|
|
'metasploit-credential not in the bundle',
|
|
|
|
) do
|
|
|
|
require 'metasploit/credential/engine'
|
|
|
|
end
|
|
|
|
|
2014-10-03 18:49:39 +00:00
|
|
|
optionally(
|
2014-11-05 15:25:50 +00:00
|
|
|
'metasploit_data_models',
|
|
|
|
'metasploit_data_models not in the bundle'
|
|
|
|
) do
|
|
|
|
require 'metasploit_data_models/engine'
|
|
|
|
end
|
2014-10-02 19:46:56 +00:00
|
|
|
end
|
|
|
|
|
2014-07-30 15:07:53 +00:00
|
|
|
#
|
|
|
|
# Instance Methods
|
|
|
|
#
|
|
|
|
|
|
|
|
# Tries to `require 'metasploit/credential/creation'` and include it in this `Class` or `Module`.
|
|
|
|
#
|
|
|
|
# @example Using in a `Module`
|
|
|
|
# require 'metasploit/framework/require'
|
|
|
|
#
|
|
|
|
# module MyModule
|
|
|
|
# extend Metasploit::Framework::Require
|
|
|
|
#
|
|
|
|
# optionally_include_metasploit_credential_creation
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# @return [void]
|
|
|
|
def optionally_include_metasploit_credential_creation
|
|
|
|
Metasploit::Framework::Require.optionally_include_metasploit_credential_creation(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-08-22 15:33:50 +00:00
|
|
|
end
|