CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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