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.

base.rb 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #
  2. # Standard Library
  3. #
  4. require 'optparse'
  5. #
  6. # Gems
  7. #
  8. require 'active_support/ordered_options'
  9. #
  10. # Project
  11. #
  12. require 'metasploit/framework/database'
  13. require 'metasploit/framework/parsed_options'
  14. # Options parsed from the command line that can be used to change the
  15. # `Metasploit::Framework::Application.config` and `Rails.env`
  16. class Metasploit::Framework::ParsedOptions::Base
  17. #
  18. # CONSTANTS
  19. #
  20. # msfconsole boots in production mode instead of the normal rails default of
  21. # development.
  22. DEFAULT_ENVIRONMENT = 'production'
  23. #
  24. # Attributes
  25. #
  26. attr_reader :positional
  27. #
  28. # Instance Methods
  29. #
  30. def initialize(arguments=ARGV)
  31. begin
  32. @positional = option_parser.parse(arguments)
  33. rescue OptionParser::InvalidOption
  34. puts "ERROR: Invalid command line option provided."
  35. puts option_parser
  36. exit(1)
  37. end
  38. end
  39. # Translates {#options} to the `application`'s config
  40. #
  41. # @param application [Rails::Application]
  42. # @return [void]
  43. def configure(application)
  44. application.config['config/database'] = options.database.config
  45. end
  46. # Sets the `RAILS_ENV` environment variable.
  47. #
  48. # 1. If the -E/--environment option is given, then its value is used.
  49. # 2. The default value, 'production', is used.
  50. #
  51. # @return [void]
  52. def environment!
  53. if defined?(Rails) && Rails.instance_variable_defined?(:@_env) && Rails.env != options.environment
  54. raise "#{self.class}##{__method__} called too late to set RAILS_ENV: Rails.env already memoized"
  55. end
  56. ENV['RAILS_ENV'] = options.environment
  57. end
  58. # Options parsed from
  59. #
  60. # @return [ActiveSupport::OrderedOptions]
  61. def options
  62. unless @options
  63. options = ActiveSupport::OrderedOptions.new
  64. options.database = ActiveSupport::OrderedOptions.new
  65. options.database.config = Metasploit::Framework::Database.configurations_pathname.try(:to_path)
  66. options.database.disable = false
  67. options.database.migrations_paths = []
  68. # If RAILS_ENV is set, then it will be used, but if RAILS_ENV is set and the --environment option is given, then
  69. # --environment value will be used to reset ENV[RAILS_ENV].
  70. options.environment = ENV['RAILS_ENV'] || DEFAULT_ENVIRONMENT
  71. options.framework = ActiveSupport::OrderedOptions.new
  72. options.framework.config = nil
  73. options.modules = ActiveSupport::OrderedOptions.new
  74. options.modules.defer_loads = false
  75. options.modules.path = nil
  76. @options = options
  77. end
  78. @options
  79. end
  80. private
  81. # Parses arguments into {#options}.
  82. #
  83. # @return [OptionParser]
  84. def option_parser
  85. @option_parser ||= OptionParser.new { |option_parser|
  86. option_parser.separator ''
  87. option_parser.separator 'Common options:'
  88. option_parser.on(
  89. '-E',
  90. '--environment ENVIRONMENT',
  91. %w{development production test},
  92. "Set Rails environment, defaults to RAIL_ENV environment variable or 'production'"
  93. ) do |environment|
  94. options.environment = environment
  95. end
  96. option_parser.separator ''
  97. option_parser.separator 'Database options:'
  98. option_parser.on(
  99. '-M',
  100. '--migration-path DIRECTORY',
  101. 'Specify a directory containing additional DB migrations'
  102. ) do |directory|
  103. options.database.migrations_paths << directory
  104. end
  105. option_parser.on('-n', '--no-database', 'Disable database support') do
  106. options.database.disable = true
  107. end
  108. option_parser.on(
  109. '-y',
  110. '--yaml PATH',
  111. 'Specify a YAML file containing database settings'
  112. ) do |path|
  113. options.database.config = path
  114. end
  115. option_parser.separator ''
  116. option_parser.separator 'Framework options:'
  117. option_parser.on('-c', '-c FILE', 'Load the specified configuration file') do |file|
  118. options.framework.config = file
  119. end
  120. option_parser.on(
  121. '-v','-V',
  122. '--version',
  123. 'Show version'
  124. ) do
  125. options.subcommand = :version
  126. end
  127. option_parser.separator ''
  128. option_parser.separator 'Module options:'
  129. option_parser.on(
  130. '--defer-module-loads',
  131. 'Defer module loading unless explicitly asked.'
  132. ) do
  133. options.modules.defer_loads = true
  134. end
  135. option_parser.on(
  136. '-m',
  137. '--module-path DIRECTORY',
  138. 'Load an additional module path'
  139. ) do |directory|
  140. options.modules.path = directory
  141. end
  142. #
  143. # Tail
  144. #
  145. option_parser.separator ''
  146. option_parser.on_tail('-h', '--help', 'Show this message') do
  147. puts option_parser
  148. exit
  149. end
  150. }
  151. end
  152. end